wined3d: Use string_buffer_sprintf() in shader_glsl_sprintf_cast().
[wine.git] / dlls / wined3d / glsl_shader.c
blobeefc248797a5fb1d81476f4841204c87314eec86
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 const char *prefix;
596 unsigned int i;
598 static const struct
600 enum wined3d_shader_type type;
601 unsigned int base_idx;
602 unsigned int count;
604 sampler_info[] =
606 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
607 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
610 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
612 prefix = shader_glsl_get_prefix(sampler_info[i].type);
613 shader_glsl_load_samplers(gl_info, priv, prefix,
614 sampler_info[i].base_idx, sampler_info[i].count, tex_unit_map, program_id);
618 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
619 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
621 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
623 if (icb)
625 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
626 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
627 GLint icb_location;
629 string_buffer_sprintf(icb_name, "%s_icb", prefix);
630 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
631 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
632 checkGLcall("Load immediate constant buffer");
634 string_buffer_release(&priv->string_buffers, icb_name);
638 /* Context activation is done by the caller. */
639 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
640 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
642 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
643 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
644 GLint location;
645 unsigned int i;
647 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
649 if (!reg_maps->uav_resource_info[i].type)
650 continue;
652 string_buffer_sprintf(name, "%s_image%u", prefix, i);
653 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
654 if (location == -1)
655 continue;
657 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
658 GL_EXTCALL(glUniform1i(location, i));
660 checkGLcall("Load image bindings");
661 string_buffer_release(&priv->string_buffers, name);
664 /* Context activation is done by the caller. */
665 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
666 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
668 unsigned int start = ~0U, end = 0;
669 int stack_idx = 0;
670 unsigned int heap_idx = 1;
671 unsigned int idx;
673 if (heap->entries[heap_idx].version <= version) return;
675 idx = heap->entries[heap_idx].idx;
676 if (constant_locations[idx] != -1)
677 start = end = idx;
678 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
680 while (stack_idx >= 0)
682 /* Note that we fall through to the next case statement. */
683 switch(stack[stack_idx])
685 case HEAP_NODE_TRAVERSE_LEFT:
687 unsigned int left_idx = heap_idx << 1;
688 if (left_idx < heap->size && heap->entries[left_idx].version > version)
690 heap_idx = left_idx;
691 idx = heap->entries[heap_idx].idx;
692 if (constant_locations[idx] != -1)
694 if (start > idx)
695 start = idx;
696 if (end < idx)
697 end = idx;
700 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
701 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
702 break;
706 case HEAP_NODE_TRAVERSE_RIGHT:
708 unsigned int right_idx = (heap_idx << 1) + 1;
709 if (right_idx < heap->size && heap->entries[right_idx].version > version)
711 heap_idx = right_idx;
712 idx = heap->entries[heap_idx].idx;
713 if (constant_locations[idx] != -1)
715 if (start > idx)
716 start = idx;
717 if (end < idx)
718 end = idx;
721 stack[stack_idx++] = HEAP_NODE_POP;
722 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
723 break;
727 case HEAP_NODE_POP:
728 heap_idx >>= 1;
729 --stack_idx;
730 break;
733 if (start <= end)
734 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
735 checkGLcall("walk_constant_heap()");
738 /* Context activation is done by the caller. */
739 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
740 GLint location, const struct wined3d_vec4 *data)
742 GLfloat clamped_constant[4];
744 if (location == -1) return;
746 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
747 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
748 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
749 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
751 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
754 /* Context activation is done by the caller. */
755 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
756 const struct wined3d_vec4 *constants, const GLint *constant_locations,
757 const struct constant_heap *heap, unsigned char *stack, DWORD version)
759 int stack_idx = 0;
760 unsigned int heap_idx = 1;
761 unsigned int idx;
763 if (heap->entries[heap_idx].version <= version) return;
765 idx = heap->entries[heap_idx].idx;
766 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
767 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
769 while (stack_idx >= 0)
771 /* Note that we fall through to the next case statement. */
772 switch(stack[stack_idx])
774 case HEAP_NODE_TRAVERSE_LEFT:
776 unsigned int left_idx = heap_idx << 1;
777 if (left_idx < heap->size && heap->entries[left_idx].version > version)
779 heap_idx = left_idx;
780 idx = heap->entries[heap_idx].idx;
781 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
783 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
784 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
785 break;
789 case HEAP_NODE_TRAVERSE_RIGHT:
791 unsigned int right_idx = (heap_idx << 1) + 1;
792 if (right_idx < heap->size && heap->entries[right_idx].version > version)
794 heap_idx = right_idx;
795 idx = heap->entries[heap_idx].idx;
796 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
798 stack[stack_idx++] = HEAP_NODE_POP;
799 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
800 break;
804 case HEAP_NODE_POP:
805 heap_idx >>= 1;
806 --stack_idx;
807 break;
810 checkGLcall("walk_constant_heap_clamped()");
813 /* Context activation is done by the caller. */
814 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
815 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
816 unsigned char *stack, unsigned int version)
818 const struct wined3d_shader_lconst *lconst;
820 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
821 if (shader->reg_maps.shader_version.major == 1
822 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
823 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
824 else
825 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
827 if (!shader->load_local_constsF)
829 TRACE("No need to load local float constants for this shader.\n");
830 return;
833 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
834 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
836 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
838 checkGLcall("glUniform4fv()");
841 /* Context activation is done by the caller. */
842 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
843 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
845 unsigned int i;
846 struct list* ptr;
848 for (i = 0; constants_set; constants_set >>= 1, ++i)
850 if (!(constants_set & 1)) continue;
852 /* We found this uniform name in the program - go ahead and send the data */
853 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
856 /* Load immediate constants */
857 ptr = list_head(&shader->constantsI);
858 while (ptr)
860 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
861 unsigned int idx = lconst->idx;
862 const GLint *values = (const GLint *)lconst->value;
864 /* We found this uniform name in the program - go ahead and send the data */
865 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
866 ptr = list_next(&shader->constantsI, ptr);
868 checkGLcall("glUniform4iv()");
871 /* Context activation is done by the caller. */
872 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
873 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
875 unsigned int i;
876 struct list* ptr;
878 for (i = 0; constants_set; constants_set >>= 1, ++i)
880 if (!(constants_set & 1)) continue;
882 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
885 /* Load immediate constants */
886 ptr = list_head(&shader->constantsB);
887 while (ptr)
889 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
890 unsigned int idx = lconst->idx;
891 const GLint *values = (const GLint *)lconst->value;
893 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
894 ptr = list_next(&shader->constantsB, ptr);
896 checkGLcall("glUniform1iv()");
899 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
901 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
904 /* Context activation is done by the caller (state handler). */
905 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
906 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
908 struct
910 float sx, sy;
912 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
913 UINT fixup = ps->np2_fixup_info->active;
914 UINT i;
916 for (i = 0; fixup; fixup >>= 1, ++i)
918 const struct wined3d_texture *tex = state->textures[i];
919 unsigned char idx = ps->np2_fixup_info->idx[i];
921 if (!tex)
923 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
924 continue;
927 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
928 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
931 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
934 /* Taken and adapted from Mesa. */
935 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
937 float pos, neg, t, det;
938 struct wined3d_matrix temp;
940 /* Calculate the determinant of upper left 3x3 submatrix and
941 * determine if the matrix is singular. */
942 pos = neg = 0.0f;
943 t = in->_11 * in->_22 * in->_33;
944 if (t >= 0.0f)
945 pos += t;
946 else
947 neg += t;
949 t = in->_21 * in->_32 * in->_13;
950 if (t >= 0.0f)
951 pos += t;
952 else
953 neg += t;
954 t = in->_31 * in->_12 * in->_23;
955 if (t >= 0.0f)
956 pos += t;
957 else
958 neg += t;
960 t = -in->_31 * in->_22 * in->_13;
961 if (t >= 0.0f)
962 pos += t;
963 else
964 neg += t;
965 t = -in->_21 * in->_12 * in->_33;
966 if (t >= 0.0f)
967 pos += t;
968 else
969 neg += t;
971 t = -in->_11 * in->_32 * in->_23;
972 if (t >= 0.0f)
973 pos += t;
974 else
975 neg += t;
977 det = pos + neg;
979 if (fabsf(det) < 1e-25f)
980 return FALSE;
982 det = 1.0f / det;
983 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
984 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
985 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
986 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
987 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
988 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
989 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
990 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
991 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
993 *out = temp;
994 return TRUE;
997 static void swap_rows(float **a, float **b)
999 float *tmp = *a;
1001 *a = *b;
1002 *b = tmp;
1005 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1007 float wtmp[4][8];
1008 float m0, m1, m2, m3, s;
1009 float *r0, *r1, *r2, *r3;
1011 r0 = wtmp[0];
1012 r1 = wtmp[1];
1013 r2 = wtmp[2];
1014 r3 = wtmp[3];
1016 r0[0] = m->_11;
1017 r0[1] = m->_12;
1018 r0[2] = m->_13;
1019 r0[3] = m->_14;
1020 r0[4] = 1.0f;
1021 r0[5] = r0[6] = r0[7] = 0.0f;
1023 r1[0] = m->_21;
1024 r1[1] = m->_22;
1025 r1[2] = m->_23;
1026 r1[3] = m->_24;
1027 r1[5] = 1.0f;
1028 r1[4] = r1[6] = r1[7] = 0.0f;
1030 r2[0] = m->_31;
1031 r2[1] = m->_32;
1032 r2[2] = m->_33;
1033 r2[3] = m->_34;
1034 r2[6] = 1.0f;
1035 r2[4] = r2[5] = r2[7] = 0.0f;
1037 r3[0] = m->_41;
1038 r3[1] = m->_42;
1039 r3[2] = m->_43;
1040 r3[3] = m->_44;
1041 r3[7] = 1.0f;
1042 r3[4] = r3[5] = r3[6] = 0.0f;
1044 /* Choose pivot - or die. */
1045 if (fabsf(r3[0]) > fabsf(r2[0]))
1046 swap_rows(&r3, &r2);
1047 if (fabsf(r2[0]) > fabsf(r1[0]))
1048 swap_rows(&r2, &r1);
1049 if (fabsf(r1[0]) > fabsf(r0[0]))
1050 swap_rows(&r1, &r0);
1051 if (r0[0] == 0.0f)
1052 return FALSE;
1054 /* Eliminate first variable. */
1055 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1056 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1057 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1058 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1059 s = r0[4];
1060 if (s != 0.0f)
1062 r1[4] -= m1 * s;
1063 r2[4] -= m2 * s;
1064 r3[4] -= m3 * s;
1066 s = r0[5];
1067 if (s != 0.0f)
1069 r1[5] -= m1 * s;
1070 r2[5] -= m2 * s;
1071 r3[5] -= m3 * s;
1073 s = r0[6];
1074 if (s != 0.0f)
1076 r1[6] -= m1 * s;
1077 r2[6] -= m2 * s;
1078 r3[6] -= m3 * s;
1080 s = r0[7];
1081 if (s != 0.0f)
1083 r1[7] -= m1 * s;
1084 r2[7] -= m2 * s;
1085 r3[7] -= m3 * s;
1088 /* Choose pivot - or die. */
1089 if (fabsf(r3[1]) > fabsf(r2[1]))
1090 swap_rows(&r3, &r2);
1091 if (fabsf(r2[1]) > fabsf(r1[1]))
1092 swap_rows(&r2, &r1);
1093 if (r1[1] == 0.0f)
1094 return FALSE;
1096 /* Eliminate second variable. */
1097 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1098 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1099 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1100 s = r1[4];
1101 if (s != 0.0f)
1103 r2[4] -= m2 * s;
1104 r3[4] -= m3 * s;
1106 s = r1[5];
1107 if (s != 0.0f)
1109 r2[5] -= m2 * s;
1110 r3[5] -= m3 * s;
1112 s = r1[6];
1113 if (s != 0.0f)
1115 r2[6] -= m2 * s;
1116 r3[6] -= m3 * s;
1118 s = r1[7];
1119 if (s != 0.0f)
1121 r2[7] -= m2 * s;
1122 r3[7] -= m3 * s;
1125 /* Choose pivot - or die. */
1126 if (fabsf(r3[2]) > fabsf(r2[2]))
1127 swap_rows(&r3, &r2);
1128 if (r2[2] == 0.0f)
1129 return FALSE;
1131 /* Eliminate third variable. */
1132 m3 = r3[2] / r2[2];
1133 r3[3] -= m3 * r2[3];
1134 r3[4] -= m3 * r2[4];
1135 r3[5] -= m3 * r2[5];
1136 r3[6] -= m3 * r2[6];
1137 r3[7] -= m3 * r2[7];
1139 /* Last check. */
1140 if (r3[3] == 0.0f)
1141 return FALSE;
1143 /* Back substitute row 3. */
1144 s = 1.0f / r3[3];
1145 r3[4] *= s;
1146 r3[5] *= s;
1147 r3[6] *= s;
1148 r3[7] *= s;
1150 /* Back substitute row 2. */
1151 m2 = r2[3];
1152 s = 1.0f / r2[2];
1153 r2[4] = s * (r2[4] - r3[4] * m2);
1154 r2[5] = s * (r2[5] - r3[5] * m2);
1155 r2[6] = s * (r2[6] - r3[6] * m2);
1156 r2[7] = s * (r2[7] - r3[7] * m2);
1157 m1 = r1[3];
1158 r1[4] -= r3[4] * m1;
1159 r1[5] -= r3[5] * m1;
1160 r1[6] -= r3[6] * m1;
1161 r1[7] -= r3[7] * m1;
1162 m0 = r0[3];
1163 r0[4] -= r3[4] * m0;
1164 r0[5] -= r3[5] * m0;
1165 r0[6] -= r3[6] * m0;
1166 r0[7] -= r3[7] * m0;
1168 /* Back substitute row 1. */
1169 m1 = r1[2];
1170 s = 1.0f / r1[1];
1171 r1[4] = s * (r1[4] - r2[4] * m1);
1172 r1[5] = s * (r1[5] - r2[5] * m1);
1173 r1[6] = s * (r1[6] - r2[6] * m1);
1174 r1[7] = s * (r1[7] - r2[7] * m1);
1175 m0 = r0[2];
1176 r0[4] -= r2[4] * m0;
1177 r0[5] -= r2[5] * m0;
1178 r0[6] -= r2[6] * m0;
1179 r0[7] -= r2[7] * m0;
1181 /* Back substitute row 0. */
1182 m0 = r0[1];
1183 s = 1.0f / r0[0];
1184 r0[4] = s * (r0[4] - r1[4] * m0);
1185 r0[5] = s * (r0[5] - r1[5] * m0);
1186 r0[6] = s * (r0[6] - r1[6] * m0);
1187 r0[7] = s * (r0[7] - r1[7] * m0);
1189 out->_11 = r0[4];
1190 out->_12 = r0[5];
1191 out->_13 = r0[6];
1192 out->_14 = r0[7];
1193 out->_21 = r1[4];
1194 out->_22 = r1[5];
1195 out->_23 = r1[6];
1196 out->_24 = r1[7];
1197 out->_31 = r2[4];
1198 out->_32 = r2[5];
1199 out->_33 = r2[6];
1200 out->_34 = r2[7];
1201 out->_41 = r3[4];
1202 out->_42 = r3[5];
1203 out->_43 = r3[6];
1204 out->_44 = r3[7];
1206 return TRUE;
1209 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1210 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1212 const struct wined3d_gl_info *gl_info = context->gl_info;
1213 float mat[3 * 3];
1214 struct wined3d_matrix mv;
1215 unsigned int i, j;
1217 if (prog->vs.normal_matrix_location == -1)
1218 return;
1220 get_modelview_matrix(context, state, 0, &mv);
1221 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1222 invert_matrix_3d(&mv, &mv);
1223 else
1224 invert_matrix(&mv, &mv);
1225 /* Tests show that singular modelview matrices are used unchanged as normal
1226 * matrices on D3D3 and older. There seems to be no clearly consistent
1227 * behavior on newer D3D versions so always follow older ddraw behavior. */
1228 for (i = 0; i < 3; ++i)
1229 for (j = 0; j < 3; ++j)
1230 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1232 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1233 checkGLcall("glUniformMatrix3fv");
1236 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1237 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1239 const struct wined3d_gl_info *gl_info = context->gl_info;
1240 struct wined3d_matrix mat;
1242 if (tex >= MAX_TEXTURES)
1243 return;
1244 if (prog->vs.texture_matrix_location[tex] == -1)
1245 return;
1247 get_texture_matrix(context, state, tex, &mat);
1248 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1249 checkGLcall("glUniformMatrix4fv");
1252 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1253 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1255 const struct wined3d_gl_info *gl_info = context->gl_info;
1257 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1259 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1260 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1262 else
1264 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1266 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1268 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1269 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1270 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1271 checkGLcall("setting FFP material uniforms");
1274 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1275 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1277 const struct wined3d_gl_info *gl_info = context->gl_info;
1278 struct wined3d_color color;
1280 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1281 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1282 checkGLcall("glUniform3fv");
1285 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1286 const struct wined3d_matrix *src2)
1288 struct wined3d_vec4 temp;
1290 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1291 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1292 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1293 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1295 *dest = temp;
1298 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1299 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1300 struct glsl_shader_prog_link *prog)
1302 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1303 const struct wined3d_gl_info *gl_info = context->gl_info;
1304 struct wined3d_vec4 vec4;
1306 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1307 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1308 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1310 switch (light_info->OriginalParms.type)
1312 case WINED3D_LIGHT_POINT:
1313 multiply_vector_matrix(&vec4, &light_info->position, view);
1314 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 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].c_att, light_info->OriginalParms.attenuation0));
1317 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1318 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1319 break;
1321 case WINED3D_LIGHT_SPOT:
1322 multiply_vector_matrix(&vec4, &light_info->position, view);
1323 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1325 multiply_vector_matrix(&vec4, &light_info->direction, view);
1326 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1328 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1329 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1330 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1331 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1332 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1333 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1334 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1335 break;
1337 case WINED3D_LIGHT_DIRECTIONAL:
1338 multiply_vector_matrix(&vec4, &light_info->direction, view);
1339 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1340 break;
1342 case WINED3D_LIGHT_PARALLELPOINT:
1343 multiply_vector_matrix(&vec4, &light_info->position, view);
1344 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1345 break;
1347 default:
1348 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1350 checkGLcall("setting FFP lights uniforms");
1353 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1354 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1356 const struct wined3d_gl_info *gl_info = context->gl_info;
1357 float min, max;
1358 float size, att[3];
1360 get_pointsize_minmax(context, state, &min, &max);
1362 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1363 checkGLcall("glUniform1f");
1364 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1365 checkGLcall("glUniform1f");
1367 get_pointsize(context, state, &size, att);
1369 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1370 checkGLcall("glUniform1f");
1371 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1372 checkGLcall("glUniform1f");
1373 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1374 checkGLcall("glUniform1f");
1375 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1376 checkGLcall("glUniform1f");
1379 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1380 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1382 const struct wined3d_gl_info *gl_info = context->gl_info;
1383 struct wined3d_color color;
1384 float start, end, scale;
1385 union
1387 DWORD d;
1388 float f;
1389 } tmpvalue;
1391 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1392 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1393 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1394 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1395 get_fog_start_end(context, state, &start, &end);
1396 scale = 1.0f / (end - start);
1397 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1398 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1399 checkGLcall("fog emulation uniforms");
1402 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1403 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1405 const struct wined3d_gl_info *gl_info = context->gl_info;
1406 struct wined3d_vec4 plane;
1408 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1409 if (!use_vs(state))
1410 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1411 else
1412 plane = state->clip_planes[index];
1414 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1417 /* Context activation is done by the caller (state handler). */
1418 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1419 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1421 struct wined3d_color float_key[2];
1422 const struct wined3d_texture *texture = state->textures[0];
1424 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1425 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1428 /* Context activation is done by the caller (state handler). */
1429 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1430 const struct wined3d_state *state)
1432 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1433 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1434 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1435 const struct wined3d_gl_info *gl_info = context->gl_info;
1436 struct shader_glsl_priv *priv = shader_priv;
1437 float position_fixup[4];
1438 DWORD update_mask;
1440 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1441 UINT constant_version;
1442 int i;
1444 if (!prog) {
1445 /* No GLSL program set - nothing to do. */
1446 return;
1448 constant_version = prog->constant_version;
1449 update_mask = context->constant_update_mask & prog->constant_update_mask;
1451 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1452 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1453 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1455 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1456 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1457 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1459 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1460 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1461 vshader->reg_maps.boolean_constants);
1463 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1465 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1466 shader_glsl_clip_plane_uniform(context, state, i, prog);
1469 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1470 shader_glsl_pointsize_uniform(context, state, prog);
1472 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1474 shader_get_position_fixup(context, state, position_fixup);
1475 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1476 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1477 else
1478 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1479 checkGLcall("glUniform4fv");
1482 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1484 struct wined3d_matrix mat;
1486 get_modelview_matrix(context, state, 0, &mat);
1487 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1488 checkGLcall("glUniformMatrix4fv");
1490 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1493 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1495 struct wined3d_matrix mat;
1497 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1499 if (prog->vs.modelview_matrix_location[i] == -1)
1500 break;
1502 get_modelview_matrix(context, state, i, &mat);
1503 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1504 checkGLcall("glUniformMatrix4fv");
1508 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1510 struct wined3d_matrix projection;
1512 get_projection_matrix(context, state, &projection);
1513 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1514 checkGLcall("glUniformMatrix4fv");
1517 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1519 for (i = 0; i < MAX_TEXTURES; ++i)
1520 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1523 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1524 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1526 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1528 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1529 DWORD point_count = 0;
1530 DWORD spot_count = 0;
1531 DWORD directional_count = 0;
1532 DWORD parallel_point_count = 0;
1534 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1536 if (!state->lights[i])
1537 continue;
1539 switch (state->lights[i]->OriginalParms.type)
1541 case WINED3D_LIGHT_POINT:
1542 ++point_count;
1543 break;
1544 case WINED3D_LIGHT_SPOT:
1545 ++spot_count;
1546 break;
1547 case WINED3D_LIGHT_DIRECTIONAL:
1548 ++directional_count;
1549 break;
1550 case WINED3D_LIGHT_PARALLELPOINT:
1551 ++parallel_point_count;
1552 break;
1553 default:
1554 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1555 break;
1558 point_idx = 0;
1559 spot_idx = point_idx + point_count;
1560 directional_idx = spot_idx + spot_count;
1561 parallel_point_idx = directional_idx + directional_count;
1563 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1564 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1566 const struct wined3d_light_info *light_info = state->lights[i];
1567 unsigned int idx;
1569 if (!light_info)
1570 continue;
1572 switch (light_info->OriginalParms.type)
1574 case WINED3D_LIGHT_POINT:
1575 idx = point_idx++;
1576 break;
1577 case WINED3D_LIGHT_SPOT:
1578 idx = spot_idx++;
1579 break;
1580 case WINED3D_LIGHT_DIRECTIONAL:
1581 idx = directional_idx++;
1582 break;
1583 case WINED3D_LIGHT_PARALLELPOINT:
1584 idx = parallel_point_idx++;
1585 break;
1586 default:
1587 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1588 continue;
1590 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1594 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1595 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1596 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1598 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1599 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1600 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1602 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1603 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1604 pshader->reg_maps.boolean_constants);
1606 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1608 for (i = 0; i < MAX_TEXTURES; ++i)
1610 if (prog->ps.bumpenv_mat_location[i] == -1)
1611 continue;
1613 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1614 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1616 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1618 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1619 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1620 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1621 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1625 checkGLcall("bump env uniforms");
1628 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1630 const struct wined3d_vec4 correction_params =
1632 /* Position is relative to the framebuffer, not the viewport. */
1633 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1634 context->render_offscreen ? 1.0f : -1.0f,
1635 0.0f,
1636 0.0f,
1639 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1642 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1643 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1644 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1645 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1647 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1649 struct wined3d_color color;
1651 if (prog->ps.tex_factor_location != -1)
1653 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1654 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1657 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1658 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1659 else
1660 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1662 for (i = 0; i < MAX_TEXTURES; ++i)
1664 if (prog->ps.tss_constant_location[i] == -1)
1665 continue;
1667 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1668 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1671 checkGLcall("fixed function uniforms");
1674 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1675 shader_glsl_load_fog_uniform(context, state, prog);
1677 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1679 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1681 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1682 checkGLcall("alpha test emulation uniform");
1685 if (priv->next_constant_version == UINT_MAX)
1687 TRACE("Max constant version reached, resetting to 0.\n");
1688 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1689 priv->next_constant_version = 1;
1691 else
1693 prog->constant_version = priv->next_constant_version++;
1697 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1699 struct constant_entry *entries = heap->entries;
1700 unsigned int *positions = heap->positions;
1701 unsigned int heap_idx, parent_idx;
1703 if (!heap->contained[idx])
1705 heap_idx = heap->size++;
1706 heap->contained[idx] = TRUE;
1708 else
1710 heap_idx = positions[idx];
1713 while (heap_idx > 1)
1715 parent_idx = heap_idx >> 1;
1717 if (new_version <= entries[parent_idx].version) break;
1719 entries[heap_idx] = entries[parent_idx];
1720 positions[entries[parent_idx].idx] = heap_idx;
1721 heap_idx = parent_idx;
1724 entries[heap_idx].version = new_version;
1725 entries[heap_idx].idx = idx;
1726 positions[idx] = heap_idx;
1729 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1731 struct shader_glsl_priv *priv = device->shader_priv;
1732 struct constant_heap *heap = &priv->vconst_heap;
1733 UINT i;
1735 for (i = start; i < count + start; ++i)
1737 update_heap_entry(heap, i, priv->next_constant_version);
1741 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1743 struct shader_glsl_priv *priv = device->shader_priv;
1744 struct constant_heap *heap = &priv->pconst_heap;
1745 UINT i;
1747 for (i = start; i < count + start; ++i)
1749 update_heap_entry(heap, i, priv->next_constant_version);
1753 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1755 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1756 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1757 if(shader_major > 3) return ret;
1759 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1760 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1761 return ret;
1764 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1766 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1769 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1771 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1774 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1776 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1779 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1780 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1782 va_list args;
1783 int ret;
1785 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1786 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1787 for (;;)
1789 va_start(args, format);
1790 ret = shader_vaddline(buffer, format, args);
1791 va_end(args);
1792 if (!ret)
1793 return;
1794 if (!string_buffer_resize(buffer, ret))
1795 return;
1799 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1800 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1802 va_list args;
1803 int ret;
1805 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1806 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1807 for (;;)
1809 va_start(args, format);
1810 ret = shader_vaddline(buffer, format, args);
1811 va_end(args);
1812 if (!ret)
1813 return;
1814 if (!string_buffer_resize(buffer, ret))
1815 return;
1819 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1821 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1824 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1826 switch (primitive_type)
1828 case WINED3D_PT_POINTLIST:
1829 return "points";
1831 case WINED3D_PT_LINELIST:
1832 return "lines";
1834 case WINED3D_PT_LINESTRIP:
1835 return "line_strip";
1837 case WINED3D_PT_TRIANGLELIST:
1838 return "triangles";
1840 case WINED3D_PT_TRIANGLESTRIP:
1841 return "triangle_strip";
1843 case WINED3D_PT_LINELIST_ADJ:
1844 return "lines_adjacency";
1846 case WINED3D_PT_TRIANGLELIST_ADJ:
1847 return "triangles_adjacency";
1849 default:
1850 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1851 return "";
1855 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1857 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1858 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1859 DWORD input_reg_used = shader->u.ps.input_reg_used;
1860 unsigned int i;
1862 if (reg_maps->shader_version.major < 3)
1863 return input_reg_used & (1u << idx);
1865 for (i = 0; i < input_signature->element_count; ++i)
1867 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1869 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1870 continue;
1872 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1873 && input->semantic_idx == idx)
1874 return input_reg_used & (1u << input->register_idx);
1876 return FALSE;
1879 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1880 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1882 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1884 if (version->major >= 4)
1885 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1886 else
1887 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1890 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1891 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1892 unsigned int index)
1894 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1895 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1896 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1897 index, scalar_type, scalar_type, index);
1900 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1901 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1903 unsigned int index = e->register_idx;
1905 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1907 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1908 index);
1909 return;
1911 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1913 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1914 index);
1915 return;
1917 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1918 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1920 if (shader_glsl_use_explicit_attrib_location(gl_info))
1921 shader_addline(buffer, "layout(location = %u) ", index);
1923 switch (e->component_type)
1925 case WINED3D_TYPE_UINT:
1926 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1927 break;
1928 case WINED3D_TYPE_INT:
1929 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1930 break;
1932 default:
1933 FIXME("Unhandled type %#x.\n", e->component_type);
1934 /* Fall through. */
1935 case WINED3D_TYPE_UNKNOWN:
1936 case WINED3D_TYPE_FLOAT:
1937 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1938 break;
1942 /** Generate the variable & register declarations for the GLSL output target */
1943 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1944 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1945 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1947 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1948 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1949 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1950 const struct wined3d_gl_info *gl_info = context->gl_info;
1951 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1952 unsigned int i, extra_constants_needed = 0;
1953 const struct wined3d_shader_lconst *lconst;
1954 const char *prefix;
1955 DWORD map;
1957 prefix = shader_glsl_get_prefix(version->type);
1959 /* Prototype the subroutines */
1960 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1962 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1965 /* Declare the constants (aka uniforms) */
1966 if (shader->limits->constant_float > 0)
1968 unsigned max_constantsF;
1970 /* Unless the shader uses indirect addressing, always declare the
1971 * maximum array size and ignore that we need some uniforms privately.
1972 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1973 * and immediate values, still declare VC[256]. If the shader needs
1974 * more uniforms than we have it won't work in any case. If it uses
1975 * less, the compiler will figure out which uniforms are really used
1976 * and strip them out. This allows a shader to use c255 on a dx9 card,
1977 * as long as it doesn't also use all the other constants.
1979 * If the shader uses indirect addressing the compiler must assume
1980 * that all declared uniforms are used. In this case, declare only the
1981 * amount that we're assured to have.
1983 * Thus we run into problems in these two cases:
1984 * 1) The shader really uses more uniforms than supported.
1985 * 2) The shader uses indirect addressing, less constants than
1986 * supported, but uses a constant index > #supported consts. */
1987 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1989 /* No indirect addressing here. */
1990 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1992 else
1994 if (reg_maps->usesrelconstF)
1996 /* Subtract the other potential uniforms from the max
1997 * available (bools, ints, and 1 row of projection matrix).
1998 * Subtract another uniform for immediate values, which have
1999 * to be loaded via uniform by the driver as well. The shader
2000 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2001 * shader code, so one vec4 should be enough. (Unfortunately
2002 * the Nvidia driver doesn't store 128 and -128 in one float).
2004 * Writing gl_ClipVertex requires one uniform for each
2005 * clipplane as well. */
2006 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2007 if (vs_args->clip_enabled)
2008 max_constantsF -= gl_info->limits.user_clip_distances;
2009 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2010 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2011 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2012 * for now take this into account when calculating the number of available constants
2014 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2015 /* Set by driver quirks in directx.c */
2016 max_constantsF -= gl_info->reserved_glsl_constants;
2018 if (max_constantsF < shader->limits->constant_float)
2020 static unsigned int once;
2022 if (!once++)
2023 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2024 " it may not render correctly.\n");
2025 else
2026 WARN("The hardware does not support enough uniform components to run this shader.\n");
2029 else
2031 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2034 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2035 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2038 /* Always declare the full set of constants, the compiler can remove the
2039 * unused ones because d3d doesn't (yet) support indirect int and bool
2040 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2041 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2042 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2044 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2045 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2047 /* Declare immediate constant buffer */
2048 if (reg_maps->icb)
2049 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2051 /* Declare constant buffers */
2052 for (i = 0; i < WINED3D_MAX_CBS; ++i)
2054 if (reg_maps->cb_sizes[i])
2055 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2056 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2059 /* Declare texture samplers */
2060 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2062 struct wined3d_shader_sampler_map_entry *entry;
2063 const char *sampler_type_prefix, *sampler_type;
2064 BOOL shadow_sampler, tex_rect;
2066 entry = &reg_maps->sampler_map.entries[i];
2068 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2070 ERR("Invalid resource index %u.\n", entry->resource_idx);
2071 continue;
2074 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2076 case WINED3D_DATA_FLOAT:
2077 case WINED3D_DATA_UNORM:
2078 case WINED3D_DATA_SNORM:
2079 sampler_type_prefix = "";
2080 break;
2082 case WINED3D_DATA_INT:
2083 sampler_type_prefix = "i";
2084 break;
2086 case WINED3D_DATA_UINT:
2087 sampler_type_prefix = "u";
2088 break;
2090 default:
2091 sampler_type_prefix = "";
2092 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2093 break;
2096 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2097 switch (reg_maps->resource_info[entry->resource_idx].type)
2099 case WINED3D_SHADER_RESOURCE_BUFFER:
2100 sampler_type = "samplerBuffer";
2101 break;
2103 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2104 if (shadow_sampler)
2105 sampler_type = "sampler1DShadow";
2106 else
2107 sampler_type = "sampler1D";
2108 break;
2110 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2111 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2112 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2113 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2114 if (shadow_sampler)
2116 if (tex_rect)
2117 sampler_type = "sampler2DRectShadow";
2118 else
2119 sampler_type = "sampler2DShadow";
2121 else
2123 if (tex_rect)
2124 sampler_type = "sampler2DRect";
2125 else
2126 sampler_type = "sampler2D";
2128 break;
2130 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2131 if (shadow_sampler)
2132 FIXME("Unsupported 3D shadow sampler.\n");
2133 sampler_type = "sampler3D";
2134 break;
2136 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2137 if (shadow_sampler)
2138 FIXME("Unsupported Cube shadow sampler.\n");
2139 sampler_type = "samplerCube";
2140 break;
2142 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2143 if (shadow_sampler)
2144 sampler_type = "sampler2DArrayShadow";
2145 else
2146 sampler_type = "sampler2DArray";
2147 break;
2149 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2150 if (shadow_sampler)
2151 FIXME("Unsupported Cube array shadow sampler.\n");
2152 sampler_type = "samplerCubeArray";
2153 break;
2155 default:
2156 sampler_type = "unsupported_sampler";
2157 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2158 break;
2160 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2161 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2164 /* Declare images */
2165 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2167 const char *image_type_prefix, *image_type, *read_format;
2169 if (!reg_maps->uav_resource_info[i].type)
2170 continue;
2172 switch (reg_maps->uav_resource_info[i].data_type)
2174 case WINED3D_DATA_FLOAT:
2175 case WINED3D_DATA_UNORM:
2176 case WINED3D_DATA_SNORM:
2177 image_type_prefix = "";
2178 read_format = "r32f";
2179 break;
2181 case WINED3D_DATA_INT:
2182 image_type_prefix = "i";
2183 read_format = "r32i";
2184 break;
2186 case WINED3D_DATA_UINT:
2187 image_type_prefix = "u";
2188 read_format = "r32ui";
2189 break;
2191 default:
2192 image_type_prefix = "";
2193 read_format = "";
2194 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2195 break;
2198 switch (reg_maps->uav_resource_info[i].type)
2200 case WINED3D_SHADER_RESOURCE_BUFFER:
2201 image_type = "imageBuffer";
2202 break;
2204 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2205 image_type = "image2D";
2206 break;
2208 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2209 image_type = "image3D";
2210 break;
2212 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2213 image_type = "image2DArray";
2214 break;
2216 default:
2217 image_type = "unsupported_image";
2218 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2219 break;
2222 if (reg_maps->uav_read_mask & (1u << i))
2223 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2224 read_format, image_type_prefix, image_type, prefix, i);
2225 else
2226 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2227 image_type_prefix, image_type, prefix, i);
2230 /* Declare uniforms for NP2 texcoord fixup:
2231 * This is NOT done inside the loop that declares the texture samplers
2232 * since the NP2 fixup code is currently only used for the GeforceFX
2233 * series and when forcing the ARB_npot extension off. Modern cards just
2234 * skip the code anyway, so put it inside a separate loop. */
2235 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2237 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2238 UINT cur = 0;
2240 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2241 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2242 * samplerNP2Fixup stores texture dimensions and is updated through
2243 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2245 for (i = 0; i < shader->limits->sampler; ++i)
2247 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2248 continue;
2250 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2252 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2253 continue;
2256 fixup->idx[i] = cur++;
2259 fixup->num_consts = (cur + 1) >> 1;
2260 fixup->active = ps_args->np2_fixup;
2261 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2264 /* Declare address variables */
2265 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2267 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2270 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2272 for (i = 0; i < shader->input_signature.element_count; ++i)
2273 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2275 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2277 shader_addline(buffer, "uniform struct\n{\n");
2278 shader_addline(buffer, " float size;\n");
2279 shader_addline(buffer, " float size_min;\n");
2280 shader_addline(buffer, " float size_max;\n");
2281 shader_addline(buffer, "} ffp_point;\n");
2284 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2286 if (vs_args->clip_enabled)
2287 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2289 if (version->major < 3)
2291 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2292 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2293 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2294 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2298 if (version->major < 4)
2299 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2301 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2303 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2305 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2307 else
2309 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2310 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2311 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2312 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2315 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2317 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2319 shader_addline(buffer, "uniform struct\n{\n");
2320 shader_addline(buffer, " vec4 color;\n");
2321 shader_addline(buffer, " float density;\n");
2322 shader_addline(buffer, " float end;\n");
2323 shader_addline(buffer, " float scale;\n");
2324 shader_addline(buffer, "} ffp_fog;\n");
2326 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2328 if (glsl_is_color_reg_read(shader, 0))
2329 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2330 if (glsl_is_color_reg_read(shader, 1))
2331 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2332 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2333 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2335 else
2337 if (glsl_is_color_reg_read(shader, 0))
2338 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2339 if (glsl_is_color_reg_read(shader, 1))
2340 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2341 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2342 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2343 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2347 if (version->major >= 3)
2349 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2351 if (ps_args->vp_mode == vertexshader)
2352 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2353 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2356 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2358 if (!(map & 1))
2359 continue;
2361 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2363 if (reg_maps->luminanceparams & (1u << i))
2365 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2366 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2367 extra_constants_needed++;
2370 extra_constants_needed++;
2373 if (ps_args->srgb_correction)
2375 shader_addline(buffer, "const vec4 srgb_const0 = ");
2376 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2377 shader_addline(buffer, ";\n");
2378 shader_addline(buffer, "const vec4 srgb_const1 = ");
2379 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2380 shader_addline(buffer, ";\n");
2382 if (reg_maps->vpos || reg_maps->usesdsy)
2384 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2386 ++extra_constants_needed;
2387 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2389 if (reg_maps->vpos)
2391 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2393 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2394 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2395 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2396 else if (!ps_args->render_offscreen)
2397 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2399 shader_addline(buffer, "vec4 vpos;\n");
2403 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2404 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2406 if (!needs_legacy_glsl_syntax(gl_info))
2407 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2409 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2410 FIXME("Insufficient uniforms to run this shader.\n");
2413 /* Declare output register temporaries */
2414 if (shader->limits->packed_output)
2415 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2417 /* Declare temporary variables */
2418 if (reg_maps->temporary_count)
2420 for (i = 0; i < reg_maps->temporary_count; ++i)
2421 shader_addline(buffer, "vec4 R%u;\n", i);
2423 else
2425 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2427 if (map & 1)
2428 shader_addline(buffer, "vec4 R%u;\n", i);
2432 /* Declare indexable temporary variables */
2433 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2435 if (idx_temp_reg->component_count != 4)
2436 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2437 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2440 /* Declare loop registers aLx */
2441 if (version->major < 4)
2443 for (i = 0; i < reg_maps->loop_depth; ++i)
2445 shader_addline(buffer, "int aL%u;\n", i);
2446 shader_addline(buffer, "int tmpInt%u;\n", i);
2450 /* Temporary variables for matrix operations */
2451 shader_addline(buffer, "vec4 tmp0;\n");
2452 shader_addline(buffer, "vec4 tmp1;\n");
2454 if (!shader->load_local_constsF)
2456 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2458 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2459 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2460 shader_addline(buffer, ";\n");
2465 /*****************************************************************************
2466 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2468 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2469 ****************************************************************************/
2471 /* Prototypes */
2472 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2473 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2475 /** Used for opcode modifiers - They multiply the result by the specified amount */
2476 static const char * const shift_glsl_tab[] = {
2477 "", /* 0 (none) */
2478 "2.0 * ", /* 1 (x2) */
2479 "4.0 * ", /* 2 (x4) */
2480 "8.0 * ", /* 3 (x8) */
2481 "16.0 * ", /* 4 (x16) */
2482 "32.0 * ", /* 5 (x32) */
2483 "", /* 6 (x64) */
2484 "", /* 7 (x128) */
2485 "", /* 8 (d256) */
2486 "", /* 9 (d128) */
2487 "", /* 10 (d64) */
2488 "", /* 11 (d32) */
2489 "0.0625 * ", /* 12 (d16) */
2490 "0.125 * ", /* 13 (d8) */
2491 "0.25 * ", /* 14 (d4) */
2492 "0.5 * " /* 15 (d2) */
2495 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2496 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2497 const char *in_reg, const char *in_regswizzle, char *out_str)
2499 switch (src_modifier)
2501 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2502 case WINED3DSPSM_DW:
2503 case WINED3DSPSM_NONE:
2504 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2505 break;
2506 case WINED3DSPSM_NEG:
2507 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2508 break;
2509 case WINED3DSPSM_NOT:
2510 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2511 break;
2512 case WINED3DSPSM_BIAS:
2513 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2514 break;
2515 case WINED3DSPSM_BIASNEG:
2516 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2517 break;
2518 case WINED3DSPSM_SIGN:
2519 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2520 break;
2521 case WINED3DSPSM_SIGNNEG:
2522 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2523 break;
2524 case WINED3DSPSM_COMP:
2525 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2526 break;
2527 case WINED3DSPSM_X2:
2528 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2529 break;
2530 case WINED3DSPSM_X2NEG:
2531 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2532 break;
2533 case WINED3DSPSM_ABS:
2534 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2535 break;
2536 case WINED3DSPSM_ABSNEG:
2537 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2538 break;
2539 default:
2540 FIXME("Unhandled modifier %u\n", src_modifier);
2541 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2545 /** Writes the GLSL variable name that corresponds to the register that the
2546 * DX opcode parameter is trying to access */
2547 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2548 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2550 /* oPos, oFog and oPts in D3D */
2551 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2553 const struct wined3d_shader *shader = ins->ctx->shader;
2554 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2555 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2556 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2557 const char *prefix = shader_glsl_get_prefix(version->type);
2558 struct glsl_src_param rel_param0, rel_param1;
2559 char imm_str[4][17];
2561 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2562 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2563 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2564 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2565 *is_color = FALSE;
2567 switch (reg->type)
2569 case WINED3DSPR_TEMP:
2570 sprintf(register_name, "R%u", reg->idx[0].offset);
2571 break;
2573 case WINED3DSPR_INPUT:
2574 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2576 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2578 if (reg->idx[0].rel_addr)
2579 FIXME("VS3+ input registers relative addressing.\n");
2580 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2581 *is_color = TRUE;
2582 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2583 break;
2586 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2588 if (reg->idx[0].rel_addr)
2590 if (reg->idx[1].rel_addr)
2591 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2592 rel_param0.param_str, reg->idx[0].offset,
2593 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2594 rel_param1.param_str, reg->idx[1].offset);
2595 else
2596 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2597 rel_param0.param_str, reg->idx[0].offset,
2598 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2599 reg->idx[1].offset);
2601 else if (reg->idx[1].rel_addr)
2602 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2603 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2604 rel_param1.param_str, reg->idx[1].offset);
2605 else
2606 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2607 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2608 reg->idx[1].offset);
2609 break;
2612 /* pixel shaders >= 3.0 */
2613 if (version->major >= 3)
2615 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2616 unsigned int in_count = vec4_varyings(version->major, gl_info);
2618 if (reg->idx[0].rel_addr)
2620 /* Removing a + 0 would be an obvious optimization, but
2621 * OS X doesn't see the NOP operation there. */
2622 if (idx)
2624 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2625 && shader->u.ps.declared_in_count > in_count)
2627 sprintf(register_name,
2628 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2629 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2630 prefix, rel_param0.param_str, idx);
2632 else
2634 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2637 else
2639 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2640 && shader->u.ps.declared_in_count > in_count)
2642 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2643 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2644 prefix, rel_param0.param_str);
2646 else
2648 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2652 else
2654 if (idx == in_count) sprintf(register_name, "gl_Color");
2655 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2656 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2659 else
2661 if (!reg->idx[0].offset)
2662 strcpy(register_name, "ffp_varying_diffuse");
2663 else
2664 strcpy(register_name, "ffp_varying_specular");
2665 break;
2667 break;
2669 case WINED3DSPR_CONST:
2671 /* Relative addressing */
2672 if (reg->idx[0].rel_addr)
2674 if (wined3d_settings.check_float_constants)
2675 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2676 rel_param0.param_str, reg->idx[0].offset,
2677 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2678 prefix, rel_param0.param_str, reg->idx[0].offset);
2679 else if (reg->idx[0].offset)
2680 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2681 else
2682 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2684 else
2686 if (shader_constant_is_local(shader, reg->idx[0].offset))
2687 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2688 else
2689 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2692 break;
2694 case WINED3DSPR_CONSTINT:
2695 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2696 break;
2698 case WINED3DSPR_CONSTBOOL:
2699 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2700 break;
2702 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2703 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2704 sprintf(register_name, "T%u", reg->idx[0].offset);
2705 else
2706 sprintf(register_name, "A%u", reg->idx[0].offset);
2707 break;
2709 case WINED3DSPR_LOOP:
2710 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2711 break;
2713 case WINED3DSPR_SAMPLER:
2714 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2715 break;
2717 case WINED3DSPR_COLOROUT:
2718 if (reg->idx[0].offset >= gl_info->limits.buffers)
2719 WARN("Write to render target %u, only %d supported.\n",
2720 reg->idx[0].offset, gl_info->limits.buffers);
2722 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2723 break;
2725 case WINED3DSPR_RASTOUT:
2726 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2727 break;
2729 case WINED3DSPR_DEPTHOUT:
2730 sprintf(register_name, "gl_FragDepth");
2731 break;
2733 case WINED3DSPR_ATTROUT:
2734 if (!reg->idx[0].offset)
2735 sprintf(register_name, "%s_out[8]", prefix);
2736 else
2737 sprintf(register_name, "%s_out[9]", prefix);
2738 break;
2740 case WINED3DSPR_TEXCRDOUT:
2741 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2742 if (reg->idx[0].rel_addr)
2743 FIXME("VS3 output registers relative addressing.\n");
2744 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2745 break;
2747 case WINED3DSPR_MISCTYPE:
2748 if (!reg->idx[0].offset)
2750 /* vPos */
2751 sprintf(register_name, "vpos");
2753 else if (reg->idx[0].offset == 1)
2755 /* Note that gl_FrontFacing is a bool, while vFace is
2756 * a float for which the sign determines front/back */
2757 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2759 else
2761 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2762 sprintf(register_name, "unrecognized_register");
2764 break;
2766 case WINED3DSPR_IMMCONST:
2767 switch (reg->immconst_type)
2769 case WINED3D_IMMCONST_SCALAR:
2770 switch (reg->data_type)
2772 case WINED3D_DATA_FLOAT:
2773 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2774 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2775 else
2776 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2777 break;
2778 case WINED3D_DATA_INT:
2779 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2780 break;
2781 case WINED3D_DATA_RESOURCE:
2782 case WINED3D_DATA_SAMPLER:
2783 case WINED3D_DATA_UINT:
2784 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2785 break;
2786 default:
2787 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2788 break;
2790 break;
2792 case WINED3D_IMMCONST_VEC4:
2793 switch (reg->data_type)
2795 case WINED3D_DATA_FLOAT:
2796 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2798 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2799 reg->u.immconst_data[0], reg->u.immconst_data[1],
2800 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2802 else
2804 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2805 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2806 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2807 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2808 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2809 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2811 break;
2812 case WINED3D_DATA_INT:
2813 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2814 reg->u.immconst_data[0], reg->u.immconst_data[1],
2815 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2816 break;
2817 case WINED3D_DATA_RESOURCE:
2818 case WINED3D_DATA_SAMPLER:
2819 case WINED3D_DATA_UINT:
2820 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2821 reg->u.immconst_data[0], reg->u.immconst_data[1],
2822 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2823 break;
2824 default:
2825 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2826 break;
2828 break;
2830 default:
2831 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2832 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2834 break;
2836 case WINED3DSPR_CONSTBUFFER:
2837 if (reg->idx[1].rel_addr)
2838 sprintf(register_name, "%s_cb%u[%s + %u]",
2839 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2840 else
2841 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2842 break;
2844 case WINED3DSPR_IMMCONSTBUFFER:
2845 if (reg->idx[0].rel_addr)
2846 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2847 else
2848 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2849 break;
2851 case WINED3DSPR_PRIMID:
2852 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2853 break;
2855 case WINED3DSPR_IDXTEMP:
2856 if (reg->idx[1].rel_addr)
2857 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2858 else
2859 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2860 break;
2862 case WINED3DSPR_LOCALTHREADINDEX:
2863 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2864 break;
2866 case WINED3DSPR_THREADID:
2867 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2868 break;
2870 case WINED3DSPR_THREADGROUPID:
2871 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2872 break;
2874 case WINED3DSPR_LOCALTHREADID:
2875 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2876 break;
2878 default:
2879 FIXME("Unhandled register type %#x.\n", reg->type);
2880 sprintf(register_name, "unrecognized_register");
2881 break;
2885 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2887 *str++ = '.';
2888 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2889 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2890 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2891 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2892 *str = '\0';
2895 /* Get the GLSL write mask for the destination register */
2896 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2898 DWORD mask = param->write_mask;
2900 if (shader_is_scalar(&param->reg))
2902 mask = WINED3DSP_WRITEMASK_0;
2903 *write_mask = '\0';
2905 else
2907 shader_glsl_write_mask_to_str(mask, write_mask);
2910 return mask;
2913 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
2915 unsigned int size = 0;
2917 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2918 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2919 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2920 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2922 return size;
2925 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
2926 unsigned int component_idx)
2928 /* swizzle bits fields: wwzzyyxx */
2929 return (swizzle >> (2 * component_idx)) & 0x3;
2932 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2934 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2935 * but addressed as "rgba". To fix this we need to swap the register's x
2936 * and z components. */
2937 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2938 unsigned int i;
2940 *str++ = '.';
2941 for (i = 0; i < 4; ++i)
2943 if (mask & (WINED3DSP_WRITEMASK_0 << i))
2944 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
2946 *str = '\0';
2949 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2950 BOOL fixup, DWORD mask, char *swizzle_str)
2952 if (shader_is_scalar(&param->reg))
2953 *swizzle_str = '\0';
2954 else
2955 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2958 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
2959 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
2961 if (dst_data_type == src_data_type)
2963 string_buffer_sprintf(dst_param, "%s", src_param);
2964 return;
2967 if (src_data_type == WINED3D_DATA_FLOAT)
2969 switch (dst_data_type)
2971 case WINED3D_DATA_INT:
2972 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
2973 return;
2974 case WINED3D_DATA_RESOURCE:
2975 case WINED3D_DATA_SAMPLER:
2976 case WINED3D_DATA_UINT:
2977 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
2978 return;
2979 default:
2980 break;
2984 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
2986 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
2987 return;
2990 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
2992 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
2993 return;
2996 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
2997 string_buffer_sprintf(dst_param, "%s", src_param);
3000 /* From a given parameter token, generate the corresponding GLSL string.
3001 * Also, return the actual register name and swizzle in case the
3002 * caller needs this information as well. */
3003 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3004 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3005 enum wined3d_data_type data_type)
3007 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3008 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3009 enum wined3d_data_type param_data_type;
3010 BOOL is_color = FALSE;
3011 char swizzle_str[6];
3013 glsl_src->reg_name[0] = '\0';
3014 glsl_src->param_str[0] = '\0';
3015 swizzle_str[0] = '\0';
3017 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3018 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3020 switch (wined3d_src->reg.type)
3022 case WINED3DSPR_IMMCONST:
3023 param_data_type = data_type;
3024 break;
3025 case WINED3DSPR_PRIMID:
3026 param_data_type = WINED3D_DATA_UINT;
3027 break;
3028 case WINED3DSPR_LOCALTHREADINDEX:
3029 case WINED3DSPR_THREADID:
3030 case WINED3DSPR_THREADGROUPID:
3031 case WINED3DSPR_LOCALTHREADID:
3032 param_data_type = WINED3D_DATA_INT;
3033 break;
3034 default:
3035 param_data_type = WINED3D_DATA_FLOAT;
3036 break;
3039 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3040 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3042 string_buffer_release(priv->string_buffers, reg_name);
3045 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3046 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3048 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3051 /* From a given parameter token, generate the corresponding GLSL string.
3052 * Also, return the actual register name and swizzle in case the
3053 * caller needs this information as well. */
3054 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3055 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3057 BOOL is_color = FALSE;
3059 glsl_dst->mask_str[0] = '\0';
3060 glsl_dst->reg_name[0] = '\0';
3062 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3063 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3066 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3067 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3068 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3069 enum wined3d_data_type data_type)
3071 struct glsl_dst_param glsl_dst;
3072 DWORD mask;
3074 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3076 switch (data_type)
3078 case WINED3D_DATA_FLOAT:
3079 shader_addline(buffer, "%s%s = %s(",
3080 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3081 break;
3082 case WINED3D_DATA_INT:
3083 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3084 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3085 break;
3086 case WINED3D_DATA_RESOURCE:
3087 case WINED3D_DATA_SAMPLER:
3088 case WINED3D_DATA_UINT:
3089 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3090 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3091 break;
3092 default:
3093 FIXME("Unhandled data type %#x.\n", data_type);
3094 shader_addline(buffer, "%s%s = %s(",
3095 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3096 break;
3100 return mask;
3103 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3104 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3106 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3109 /** Process GLSL instruction modifiers */
3110 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3112 struct glsl_dst_param dst_param;
3113 DWORD modifiers;
3115 if (!ins->dst_count) return;
3117 modifiers = ins->dst[0].modifiers;
3118 if (!modifiers) return;
3120 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3122 if (modifiers & WINED3DSPDM_SATURATE)
3124 /* _SAT means to clamp the value of the register to between 0 and 1 */
3125 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3126 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3129 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3131 FIXME("_centroid modifier not handled\n");
3134 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3136 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3140 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3142 switch (op)
3144 case WINED3D_SHADER_REL_OP_GT: return ">";
3145 case WINED3D_SHADER_REL_OP_EQ: return "==";
3146 case WINED3D_SHADER_REL_OP_GE: return ">=";
3147 case WINED3D_SHADER_REL_OP_LT: return "<";
3148 case WINED3D_SHADER_REL_OP_NE: return "!=";
3149 case WINED3D_SHADER_REL_OP_LE: return "<=";
3150 default:
3151 FIXME("Unrecognized operator %#x.\n", op);
3152 return "(\?\?)";
3156 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3157 const struct wined3d_shader_version *version)
3159 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3162 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3163 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3165 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3166 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3167 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3168 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3169 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3170 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3171 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3172 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3173 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3174 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3175 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3176 const char *base = "texture", *type_part = "", *suffix = "";
3177 unsigned int coord_size, deriv_size;
3178 BOOL array;
3180 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3182 if (resource_type >= ARRAY_SIZE(resource_type_info))
3184 ERR("Unexpected resource type %#x.\n", resource_type);
3185 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3187 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3188 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3190 /* Note that there's no such thing as a projected cube texture. */
3191 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3192 projected = FALSE;
3194 if (needs_legacy_glsl_syntax(gl_info))
3196 if (shadow)
3197 base = "shadow";
3199 type_part = resource_type_info[resource_type].type_part;
3200 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3201 type_part = "2DRect";
3202 if (!type_part[0])
3203 FIXME("Unhandled resource type %#x.\n", resource_type);
3205 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3207 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3208 suffix = "ARB";
3209 else
3210 FIXME("Unsupported grad function.\n");
3214 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3216 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3217 if (flags & ~texel_fetch_flags)
3218 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3220 base = "texelFetch";
3221 type_part = "";
3224 sample_function->name = string_buffer_get(priv->string_buffers);
3225 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3226 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3228 coord_size = resource_type_info[resource_type].coord_size;
3229 deriv_size = coord_size;
3230 if (shadow)
3231 ++coord_size;
3232 if (array)
3233 --deriv_size;
3234 sample_function->offset_size = offset ? deriv_size : 0;
3235 sample_function->coord_mask = (1u << coord_size) - 1;
3236 sample_function->deriv_mask = (1u << deriv_size) - 1;
3237 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3240 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3241 struct glsl_sample_function *sample_function)
3243 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3245 string_buffer_release(priv->string_buffers, sample_function->name);
3248 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3249 BOOL sign_fixup, enum fixup_channel_source channel_source)
3251 switch(channel_source)
3253 case CHANNEL_SOURCE_ZERO:
3254 strcat(arguments, "0.0");
3255 break;
3257 case CHANNEL_SOURCE_ONE:
3258 strcat(arguments, "1.0");
3259 break;
3261 case CHANNEL_SOURCE_X:
3262 strcat(arguments, reg_name);
3263 strcat(arguments, ".x");
3264 break;
3266 case CHANNEL_SOURCE_Y:
3267 strcat(arguments, reg_name);
3268 strcat(arguments, ".y");
3269 break;
3271 case CHANNEL_SOURCE_Z:
3272 strcat(arguments, reg_name);
3273 strcat(arguments, ".z");
3274 break;
3276 case CHANNEL_SOURCE_W:
3277 strcat(arguments, reg_name);
3278 strcat(arguments, ".w");
3279 break;
3281 default:
3282 FIXME("Unhandled channel source %#x\n", channel_source);
3283 strcat(arguments, "undefined");
3284 break;
3287 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3290 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3291 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3293 unsigned int mask_size, remaining;
3294 DWORD fixup_mask = 0;
3295 char arguments[256];
3296 char mask_str[6];
3298 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3299 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3300 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3301 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3302 if (!(mask &= fixup_mask))
3303 return;
3305 if (is_complex_fixup(fixup))
3307 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3308 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3309 return;
3312 shader_glsl_write_mask_to_str(mask, mask_str);
3313 mask_size = shader_glsl_get_write_mask_size(mask);
3315 arguments[0] = '\0';
3316 remaining = mask_size;
3317 if (mask & WINED3DSP_WRITEMASK_0)
3319 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3320 if (--remaining) strcat(arguments, ", ");
3322 if (mask & WINED3DSP_WRITEMASK_1)
3324 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3325 if (--remaining) strcat(arguments, ", ");
3327 if (mask & WINED3DSP_WRITEMASK_2)
3329 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3330 if (--remaining) strcat(arguments, ", ");
3332 if (mask & WINED3DSP_WRITEMASK_3)
3334 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3335 if (--remaining) strcat(arguments, ", ");
3338 if (mask_size > 1)
3339 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3340 else
3341 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3344 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3346 char reg_name[256];
3347 BOOL is_color;
3349 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3350 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3353 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3354 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3355 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3356 const char *coord_reg_fmt, ...)
3358 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3359 char dst_swizzle[6];
3360 struct color_fixup_desc fixup;
3361 BOOL np2_fixup = FALSE;
3362 va_list args;
3363 int ret;
3365 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3367 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3368 * We actually rely on it for vertex shaders and SM4+. */
3369 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3371 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3372 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3374 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3375 np2_fixup = TRUE;
3377 else
3379 fixup = COLOR_FIXUP_IDENTITY;
3382 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3384 if (sample_function->output_single_component)
3385 shader_addline(ins->ctx->buffer, "vec4(");
3387 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3388 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3390 for (;;)
3392 va_start(args, coord_reg_fmt);
3393 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3394 va_end(args);
3395 if (!ret)
3396 break;
3397 if (!string_buffer_resize(ins->ctx->buffer, ret))
3398 break;
3401 if (np2_fixup)
3403 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3404 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3406 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3408 case 1:
3409 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3410 idx >> 1, (idx % 2) ? "z" : "x");
3411 break;
3412 case 2:
3413 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3414 idx >> 1, (idx % 2) ? "zw" : "xy");
3415 break;
3416 case 3:
3417 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3418 idx >> 1, (idx % 2) ? "zw" : "xy");
3419 break;
3420 case 4:
3421 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3422 idx >> 1, (idx % 2) ? "zw" : "xy");
3423 break;
3426 if (dx && dy)
3427 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3428 else if (bias)
3429 shader_addline(ins->ctx->buffer, ", %s", bias);
3430 if (sample_function->offset_size)
3432 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3433 shader_addline(ins->ctx->buffer, ", ");
3434 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3436 shader_addline(ins->ctx->buffer, ")");
3438 if (sample_function->output_single_component)
3439 shader_addline(ins->ctx->buffer, ")");
3441 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3443 if (!is_identity_fixup(fixup))
3444 shader_glsl_color_correction(ins, fixup);
3447 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3449 /* Write the final position.
3451 * OpenGL coordinates specify the center of the pixel while D3D coords
3452 * specify the corner. The offsets are stored in z and w in
3453 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3454 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3455 * a MAD. */
3456 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3457 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3459 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3460 * in utils.c
3462 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3463 * shaders are run before the homogeneous divide, so we have to take the w
3464 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3465 * z = z * 2 - w. */
3466 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3469 /*****************************************************************************
3470 * Begin processing individual instruction opcodes
3471 ****************************************************************************/
3473 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3475 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3476 struct glsl_src_param src0_param;
3477 struct glsl_src_param src1_param;
3478 DWORD write_mask;
3479 const char *op;
3481 /* Determine the GLSL operator to use based on the opcode */
3482 switch (ins->handler_idx)
3484 case WINED3DSIH_ADD: op = "+"; break;
3485 case WINED3DSIH_AND: op = "&"; break;
3486 case WINED3DSIH_DIV: op = "/"; break;
3487 case WINED3DSIH_IADD: op = "+"; break;
3488 case WINED3DSIH_ISHL: op = "<<"; break;
3489 case WINED3DSIH_ISHR: op = ">>"; break;
3490 case WINED3DSIH_MUL: op = "*"; break;
3491 case WINED3DSIH_OR: op = "|"; break;
3492 case WINED3DSIH_SUB: op = "-"; break;
3493 case WINED3DSIH_USHR: op = ">>"; break;
3494 case WINED3DSIH_XOR: op = "^"; break;
3495 default:
3496 op = "<unhandled operator>";
3497 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3498 break;
3501 write_mask = shader_glsl_append_dst(buffer, ins);
3502 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3503 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3504 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3507 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3509 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3510 struct glsl_src_param src0_param;
3511 struct glsl_src_param src1_param;
3512 unsigned int mask_size;
3513 DWORD write_mask;
3514 const char *op;
3516 write_mask = shader_glsl_append_dst(buffer, ins);
3517 mask_size = shader_glsl_get_write_mask_size(write_mask);
3518 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3519 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3521 if (mask_size > 1)
3523 switch (ins->handler_idx)
3525 case WINED3DSIH_EQ: op = "equal"; break;
3526 case WINED3DSIH_IEQ: op = "equal"; break;
3527 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3528 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3529 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3530 case WINED3DSIH_LT: op = "lessThan"; break;
3531 case WINED3DSIH_ILT: op = "lessThan"; break;
3532 case WINED3DSIH_ULT: op = "lessThan"; break;
3533 case WINED3DSIH_NE: op = "notEqual"; break;
3534 case WINED3DSIH_INE: op = "notEqual"; break;
3535 default:
3536 op = "<unhandled operator>";
3537 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3538 break;
3541 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3542 mask_size, op, src0_param.param_str, src1_param.param_str);
3544 else
3546 switch (ins->handler_idx)
3548 case WINED3DSIH_EQ: op = "=="; break;
3549 case WINED3DSIH_IEQ: op = "=="; break;
3550 case WINED3DSIH_GE: op = ">="; break;
3551 case WINED3DSIH_IGE: op = ">="; break;
3552 case WINED3DSIH_UGE: op = ">="; break;
3553 case WINED3DSIH_LT: op = "<"; break;
3554 case WINED3DSIH_ILT: op = "<"; break;
3555 case WINED3DSIH_ULT: op = "<"; break;
3556 case WINED3DSIH_NE: op = "!="; break;
3557 case WINED3DSIH_INE: op = "!="; break;
3558 default:
3559 op = "<unhandled operator>";
3560 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3561 break;
3564 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3565 src0_param.param_str, op, src1_param.param_str);
3569 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3571 struct glsl_src_param src_param;
3572 DWORD write_mask;
3573 const char *op;
3575 switch (ins->handler_idx)
3577 case WINED3DSIH_INEG: op = "-"; break;
3578 case WINED3DSIH_NOT: op = "~"; break;
3579 default:
3580 op = "<unhandled operator>";
3581 ERR("Unhandled opcode %s.\n",
3582 debug_d3dshaderinstructionhandler(ins->handler_idx));
3583 break;
3586 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3587 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3588 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3591 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3593 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3594 struct glsl_src_param src0_param;
3595 struct glsl_src_param src1_param;
3596 DWORD write_mask;
3598 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3599 * not, we can emulate it. */
3600 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3601 FIXME("64-bit integer multiplies not implemented.\n");
3603 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3605 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3606 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3607 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3609 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3610 src0_param.param_str, src1_param.param_str);
3614 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3616 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3617 struct glsl_src_param src0_param, src1_param;
3618 DWORD write_mask;
3620 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3622 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3624 char dst_mask[6];
3626 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
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, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3630 dst_mask, src0_param.param_str, src1_param.param_str);
3632 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3633 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3634 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3635 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3637 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3638 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3640 else
3642 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3643 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3644 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3645 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3648 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3650 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3651 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3652 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3653 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3657 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3658 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3660 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3661 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3662 struct glsl_src_param src0_param;
3663 DWORD write_mask;
3665 write_mask = shader_glsl_append_dst(buffer, ins);
3666 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3668 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3669 * shader versions WINED3DSIO_MOVA is used for this. */
3670 if (ins->ctx->reg_maps->shader_version.major == 1
3671 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3672 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3674 /* This is a simple floor() */
3675 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3676 if (mask_size > 1) {
3677 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3678 } else {
3679 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3682 else if (ins->handler_idx == WINED3DSIH_MOVA)
3684 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3685 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3687 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3689 if (mask_size > 1)
3690 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3691 else
3692 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3694 else
3696 if (mask_size > 1)
3697 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3698 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3699 else
3700 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3701 src0_param.param_str, src0_param.param_str);
3704 else
3706 shader_addline(buffer, "%s);\n", src0_param.param_str);
3710 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3711 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3713 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3714 struct glsl_src_param src0_param;
3715 struct glsl_src_param src1_param;
3716 DWORD dst_write_mask, src_write_mask;
3717 unsigned int dst_size;
3719 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3720 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3722 /* dp4 works on vec4, dp3 on vec3, etc. */
3723 if (ins->handler_idx == WINED3DSIH_DP4)
3724 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3725 else if (ins->handler_idx == WINED3DSIH_DP3)
3726 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3727 else
3728 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3730 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3731 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3733 if (dst_size > 1) {
3734 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3735 } else {
3736 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3740 /* Note that this instruction has some restrictions. The destination write mask
3741 * can't contain the w component, and the source swizzles have to be .xyzw */
3742 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3744 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3745 struct glsl_src_param src0_param;
3746 struct glsl_src_param src1_param;
3747 char dst_mask[6];
3749 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3750 shader_glsl_append_dst(ins->ctx->buffer, ins);
3751 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3752 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3753 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3756 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3758 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3760 if (!stream)
3761 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3762 else
3763 FIXME("Unhandled primitive stream %u.\n", stream);
3766 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3767 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3768 * GLSL uses the value as-is. */
3769 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3771 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3772 struct glsl_src_param src0_param;
3773 struct glsl_src_param src1_param;
3774 DWORD dst_write_mask;
3775 unsigned int dst_size;
3777 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3778 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3780 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3781 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3783 if (dst_size > 1)
3785 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3786 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3788 else
3790 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3791 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3795 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3796 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3798 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3799 struct glsl_src_param src_param;
3800 const char *instruction;
3801 DWORD write_mask;
3802 unsigned i;
3804 /* Determine the GLSL function to use based on the opcode */
3805 /* TODO: Possibly make this a table for faster lookups */
3806 switch (ins->handler_idx)
3808 case WINED3DSIH_ABS: instruction = "abs"; break;
3809 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3810 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3811 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3812 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3813 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3814 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3815 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3816 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3817 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3818 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3819 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3820 case WINED3DSIH_FRC: instruction = "fract"; break;
3821 case WINED3DSIH_IMAX: instruction = "max"; break;
3822 case WINED3DSIH_IMIN: instruction = "min"; break;
3823 case WINED3DSIH_MAX: instruction = "max"; break;
3824 case WINED3DSIH_MIN: instruction = "min"; break;
3825 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3826 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3827 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3828 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3829 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3830 case WINED3DSIH_UMAX: instruction = "max"; break;
3831 case WINED3DSIH_UMIN: instruction = "min"; break;
3832 default: instruction = "";
3833 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3834 break;
3837 write_mask = shader_glsl_append_dst(buffer, ins);
3839 /* In D3D bits are numbered from the most significant bit. */
3840 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3841 shader_addline(buffer, "31 - ");
3842 shader_addline(buffer, "%s(", instruction);
3844 if (ins->src_count)
3846 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3847 shader_addline(buffer, "%s", src_param.param_str);
3848 for (i = 1; i < ins->src_count; ++i)
3850 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3851 shader_addline(buffer, ", %s", src_param.param_str);
3855 shader_addline(buffer, "));\n");
3858 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3860 struct wined3d_shader_dst_param dst;
3861 struct glsl_src_param src;
3862 DWORD write_mask;
3863 const char *fmt;
3864 unsigned int i;
3866 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3867 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3869 dst = ins->dst[0];
3870 for (i = 0; i < 4; ++i)
3872 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3873 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3874 &dst, dst.reg.data_type)))
3875 continue;
3877 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3878 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3882 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3884 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3885 struct wined3d_shader_dst_param dst;
3886 struct glsl_src_param src[4];
3887 const char *instruction;
3888 unsigned int i, j;
3889 DWORD write_mask;
3891 switch (ins->handler_idx)
3893 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3894 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3895 default:
3896 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3897 return;
3900 dst = ins->dst[0];
3901 for (i = 0; i < 4; ++i)
3903 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3904 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3905 &dst, dst.reg.data_type)))
3906 continue;
3908 for (j = 0; j < ins->src_count; ++j)
3909 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3910 shader_addline(buffer, "%s(", instruction);
3911 for (j = 0; j < ins->src_count - 2; ++j)
3912 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3913 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3917 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3919 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3921 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3922 struct glsl_src_param src_param;
3923 unsigned int mask_size;
3924 DWORD write_mask;
3925 char dst_mask[6];
3927 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3928 mask_size = shader_glsl_get_write_mask_size(write_mask);
3929 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3931 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3932 src_param.param_str, src_param.param_str);
3933 shader_glsl_append_dst(buffer, ins);
3935 if (mask_size > 1)
3937 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3938 mask_size, src_param.param_str);
3940 else
3942 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3943 src_param.param_str);
3947 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3949 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3950 ins->ctx->reg_maps->shader_version.minor);
3951 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3952 struct glsl_src_param src0_param;
3953 const char *prefix, *suffix;
3954 unsigned int dst_size;
3955 DWORD dst_write_mask;
3957 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3958 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3960 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3961 dst_write_mask = WINED3DSP_WRITEMASK_3;
3963 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3965 switch (ins->handler_idx)
3967 case WINED3DSIH_EXP:
3968 case WINED3DSIH_EXPP:
3969 prefix = "exp2(";
3970 suffix = ")";
3971 break;
3973 case WINED3DSIH_LOG:
3974 case WINED3DSIH_LOGP:
3975 prefix = "log2(abs(";
3976 suffix = "))";
3977 break;
3979 case WINED3DSIH_RCP:
3980 prefix = "1.0 / ";
3981 suffix = "";
3982 break;
3984 case WINED3DSIH_RSQ:
3985 prefix = "inversesqrt(abs(";
3986 suffix = "))";
3987 break;
3989 default:
3990 prefix = "";
3991 suffix = "";
3992 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3993 break;
3996 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3997 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3998 else
3999 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4002 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4003 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4004 * dst.x = 2^(floor(src))
4005 * dst.y = src - floor(src)
4006 * dst.z = 2^src (partial precision is allowed, but optional)
4007 * dst.w = 1.0;
4008 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4009 * dst = 2^src; (partial precision is allowed, but optional)
4011 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4013 if (ins->ctx->reg_maps->shader_version.major < 2)
4015 struct glsl_src_param src_param;
4016 char dst_mask[6];
4018 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4020 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4021 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4022 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4023 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4025 shader_glsl_append_dst(ins->ctx->buffer, ins);
4026 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4027 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4028 return;
4031 shader_glsl_scalar_op(ins);
4034 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4035 const char *vector_constructor, const char *scalar_constructor)
4037 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4038 struct glsl_src_param src_param;
4039 unsigned int mask_size;
4040 DWORD write_mask;
4042 write_mask = shader_glsl_append_dst(buffer, ins);
4043 mask_size = shader_glsl_get_write_mask_size(write_mask);
4044 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4046 if (mask_size > 1)
4047 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4048 else
4049 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4052 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4054 shader_glsl_cast(ins, "ivec", "int");
4057 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4059 shader_glsl_cast(ins, "uvec", "uint");
4062 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4064 shader_glsl_cast(ins, "vec", "float");
4067 /** Process signed comparison opcodes in GLSL. */
4068 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4070 struct glsl_src_param src0_param;
4071 struct glsl_src_param src1_param;
4072 DWORD write_mask;
4073 unsigned int mask_size;
4075 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4076 mask_size = shader_glsl_get_write_mask_size(write_mask);
4077 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4078 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4080 if (mask_size > 1) {
4081 const char *compare;
4083 switch(ins->handler_idx)
4085 case WINED3DSIH_SLT: compare = "lessThan"; break;
4086 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4087 default: compare = "";
4088 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4091 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4092 src0_param.param_str, src1_param.param_str);
4093 } else {
4094 switch(ins->handler_idx)
4096 case WINED3DSIH_SLT:
4097 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4098 * to return 0.0 but step returns 1.0 because step is not < x
4099 * An alternative is a bvec compare padded with an unused second component.
4100 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4101 * issue. Playing with not() is not possible either because not() does not accept
4102 * a scalar.
4104 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4105 src0_param.param_str, src1_param.param_str);
4106 break;
4107 case WINED3DSIH_SGE:
4108 /* Here we can use the step() function and safe a conditional */
4109 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4110 break;
4111 default:
4112 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4118 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4120 const char *condition_prefix, *condition_suffix;
4121 struct wined3d_shader_dst_param dst;
4122 struct glsl_src_param src0_param;
4123 struct glsl_src_param src1_param;
4124 struct glsl_src_param src2_param;
4125 BOOL temp_destination = FALSE;
4126 DWORD cmp_channel = 0;
4127 unsigned int i, j;
4128 char mask_char[6];
4129 DWORD write_mask;
4131 switch (ins->handler_idx)
4133 case WINED3DSIH_CMP:
4134 condition_prefix = "";
4135 condition_suffix = " >= 0.0";
4136 break;
4138 case WINED3DSIH_CND:
4139 condition_prefix = "";
4140 condition_suffix = " > 0.5";
4141 break;
4143 case WINED3DSIH_MOVC:
4144 condition_prefix = "bool(";
4145 condition_suffix = ")";
4146 break;
4148 default:
4149 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4150 condition_prefix = "<unhandled prefix>";
4151 condition_suffix = "<unhandled suffix>";
4152 break;
4155 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4157 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4158 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4159 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4160 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4162 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4163 condition_prefix, src0_param.param_str, condition_suffix,
4164 src1_param.param_str, src2_param.param_str);
4165 return;
4168 dst = ins->dst[0];
4170 /* Splitting the instruction up in multiple lines imposes a problem:
4171 * The first lines may overwrite source parameters of the following lines.
4172 * Deal with that by using a temporary destination register if needed. */
4173 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4174 && ins->src[0].reg.type == dst.reg.type)
4175 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4176 && ins->src[1].reg.type == dst.reg.type)
4177 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4178 && ins->src[2].reg.type == dst.reg.type))
4179 temp_destination = TRUE;
4181 /* Cycle through all source0 channels. */
4182 for (i = 0; i < 4; ++i)
4184 write_mask = 0;
4185 /* Find the destination channels which use the current source0 channel. */
4186 for (j = 0; j < 4; ++j)
4188 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4190 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4191 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4194 dst.write_mask = ins->dst[0].write_mask & write_mask;
4196 if (temp_destination)
4198 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4199 continue;
4200 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4202 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4203 continue;
4205 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4206 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4207 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4209 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4210 condition_prefix, src0_param.param_str, condition_suffix,
4211 src1_param.param_str, src2_param.param_str);
4214 if (temp_destination)
4216 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4217 shader_glsl_append_dst(ins->ctx->buffer, ins);
4218 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4222 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4223 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4224 * the compare is done per component of src0. */
4225 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4227 struct glsl_src_param src0_param;
4228 struct glsl_src_param src1_param;
4229 struct glsl_src_param src2_param;
4230 DWORD write_mask;
4231 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4232 ins->ctx->reg_maps->shader_version.minor);
4234 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4236 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4237 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4238 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4239 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4241 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4242 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4243 else
4244 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4245 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4246 return;
4249 shader_glsl_conditional_move(ins);
4252 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4253 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4255 struct glsl_src_param src0_param;
4256 struct glsl_src_param src1_param;
4257 struct glsl_src_param src2_param;
4258 DWORD write_mask;
4260 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4261 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4262 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4263 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4264 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4265 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4268 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4269 Vertex shaders to GLSL codes */
4270 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4272 int i;
4273 int nComponents = 0;
4274 struct wined3d_shader_dst_param tmp_dst = {{0}};
4275 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4276 struct wined3d_shader_instruction tmp_ins;
4278 memset(&tmp_ins, 0, sizeof(tmp_ins));
4280 /* Set constants for the temporary argument */
4281 tmp_ins.ctx = ins->ctx;
4282 tmp_ins.dst_count = 1;
4283 tmp_ins.dst = &tmp_dst;
4284 tmp_ins.src_count = 2;
4285 tmp_ins.src = tmp_src;
4287 switch(ins->handler_idx)
4289 case WINED3DSIH_M4x4:
4290 nComponents = 4;
4291 tmp_ins.handler_idx = WINED3DSIH_DP4;
4292 break;
4293 case WINED3DSIH_M4x3:
4294 nComponents = 3;
4295 tmp_ins.handler_idx = WINED3DSIH_DP4;
4296 break;
4297 case WINED3DSIH_M3x4:
4298 nComponents = 4;
4299 tmp_ins.handler_idx = WINED3DSIH_DP3;
4300 break;
4301 case WINED3DSIH_M3x3:
4302 nComponents = 3;
4303 tmp_ins.handler_idx = WINED3DSIH_DP3;
4304 break;
4305 case WINED3DSIH_M3x2:
4306 nComponents = 2;
4307 tmp_ins.handler_idx = WINED3DSIH_DP3;
4308 break;
4309 default:
4310 break;
4313 tmp_dst = ins->dst[0];
4314 tmp_src[0] = ins->src[0];
4315 tmp_src[1] = ins->src[1];
4316 for (i = 0; i < nComponents; ++i)
4318 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4319 shader_glsl_dot(&tmp_ins);
4320 ++tmp_src[1].reg.idx[0].offset;
4325 The LRP instruction performs a component-wise linear interpolation
4326 between the second and third operands using the first operand as the
4327 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4328 This is equivalent to mix(src2, src1, src0);
4330 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4332 struct glsl_src_param src0_param;
4333 struct glsl_src_param src1_param;
4334 struct glsl_src_param src2_param;
4335 DWORD write_mask;
4337 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4339 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4340 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4341 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4343 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4344 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4347 /** Process the WINED3DSIO_LIT instruction in GLSL:
4348 * dst.x = dst.w = 1.0
4349 * dst.y = (src0.x > 0) ? src0.x
4350 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4351 * where src.w is clamped at +- 128
4353 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4355 struct glsl_src_param src0_param;
4356 struct glsl_src_param src1_param;
4357 struct glsl_src_param src3_param;
4358 char dst_mask[6];
4360 shader_glsl_append_dst(ins->ctx->buffer, ins);
4361 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4363 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4364 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4365 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4367 /* The sdk specifies the instruction like this
4368 * dst.x = 1.0;
4369 * if(src.x > 0.0) dst.y = src.x
4370 * else dst.y = 0.0.
4371 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4372 * else dst.z = 0.0;
4373 * dst.w = 1.0;
4374 * (where power = src.w clamped between -128 and 128)
4376 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4377 * dst.x = 1.0 ... No further explanation needed
4378 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4379 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4380 * dst.w = 1.0. ... Nothing fancy.
4382 * So we still have one conditional in there. So do this:
4383 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4385 * 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),
4386 * 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.
4387 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4389 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4390 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4391 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4393 shader_addline(ins->ctx->buffer,
4394 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4395 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4396 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4397 src0_param.param_str, src3_param.param_str, dst_mask);
4400 /** Process the WINED3DSIO_DST instruction in GLSL:
4401 * dst.x = 1.0
4402 * dst.y = src0.x * src0.y
4403 * dst.z = src0.z
4404 * dst.w = src1.w
4406 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4408 struct glsl_src_param src0y_param;
4409 struct glsl_src_param src0z_param;
4410 struct glsl_src_param src1y_param;
4411 struct glsl_src_param src1w_param;
4412 char dst_mask[6];
4414 shader_glsl_append_dst(ins->ctx->buffer, ins);
4415 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4417 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4418 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4419 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4420 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4422 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4423 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4426 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4427 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4428 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4430 * dst.x = cos(src0.?)
4431 * dst.y = sin(src0.?)
4432 * dst.z = dst.z
4433 * dst.w = dst.w
4435 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4437 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4438 struct glsl_src_param src0_param;
4439 DWORD write_mask;
4441 if (ins->ctx->reg_maps->shader_version.major < 4)
4443 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4445 write_mask = shader_glsl_append_dst(buffer, ins);
4446 switch (write_mask)
4448 case WINED3DSP_WRITEMASK_0:
4449 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4450 break;
4452 case WINED3DSP_WRITEMASK_1:
4453 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4454 break;
4456 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4457 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4458 src0_param.param_str, src0_param.param_str);
4459 break;
4461 default:
4462 ERR("Write mask should be .x, .y or .xy\n");
4463 break;
4466 return;
4469 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4472 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4474 char dst_mask[6];
4476 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4477 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4478 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4480 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4481 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4482 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4484 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4485 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4487 else
4489 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4490 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4491 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4494 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4496 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4497 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4498 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4502 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4503 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4504 * generate invalid code
4506 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4508 struct glsl_src_param src0_param;
4509 DWORD write_mask;
4511 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4512 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4514 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4517 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4518 * Start a for() loop where src1.y is the initial value of aL,
4519 * increment aL by src1.z for a total of src1.x iterations.
4520 * Need to use a temporary variable for this operation.
4522 /* FIXME: I don't think nested loops will work correctly this way. */
4523 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4525 struct wined3d_shader_parser_state *state = ins->ctx->state;
4526 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4527 const struct wined3d_shader *shader = ins->ctx->shader;
4528 const struct wined3d_shader_lconst *constant;
4529 struct glsl_src_param src1_param;
4530 const DWORD *control_values = NULL;
4532 if (ins->ctx->reg_maps->shader_version.major < 4)
4534 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4536 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4537 * class hardware doesn't support real varying indexing, but Microsoft
4538 * designed this feature for Shader model 2.x+. If the loop control is
4539 * known at compile time, the GLSL compiler can unroll the loop, and
4540 * replace indirect addressing with direct addressing. */
4541 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4543 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4545 if (constant->idx == ins->src[1].reg.idx[0].offset)
4547 control_values = constant->value;
4548 break;
4553 if (control_values)
4555 struct wined3d_shader_loop_control loop_control;
4556 loop_control.count = control_values[0];
4557 loop_control.start = control_values[1];
4558 loop_control.step = (int)control_values[2];
4560 if (loop_control.step > 0)
4562 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4563 state->current_loop_depth, loop_control.start,
4564 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4565 state->current_loop_depth, loop_control.step);
4567 else if (loop_control.step < 0)
4569 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4570 state->current_loop_depth, loop_control.start,
4571 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4572 state->current_loop_depth, loop_control.step);
4574 else
4576 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4577 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4578 state->current_loop_depth, loop_control.count,
4579 state->current_loop_depth);
4582 else
4584 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4585 state->current_loop_depth, state->current_loop_reg,
4586 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4587 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4590 ++state->current_loop_reg;
4592 else
4594 shader_addline(buffer, "for (;;)\n{\n");
4597 ++state->current_loop_depth;
4600 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4602 struct wined3d_shader_parser_state *state = ins->ctx->state;
4604 shader_addline(ins->ctx->buffer, "}\n");
4606 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4608 --state->current_loop_depth;
4609 --state->current_loop_reg;
4612 if (ins->handler_idx == WINED3DSIH_ENDREP)
4614 --state->current_loop_depth;
4618 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4620 struct wined3d_shader_parser_state *state = ins->ctx->state;
4621 const struct wined3d_shader *shader = ins->ctx->shader;
4622 const struct wined3d_shader_lconst *constant;
4623 struct glsl_src_param src0_param;
4624 const DWORD *control_values = NULL;
4626 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4627 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4629 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4631 if (constant->idx == ins->src[0].reg.idx[0].offset)
4633 control_values = constant->value;
4634 break;
4639 if (control_values)
4641 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4642 state->current_loop_depth, state->current_loop_depth,
4643 control_values[0], state->current_loop_depth);
4645 else
4647 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4648 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4649 state->current_loop_depth, state->current_loop_depth,
4650 src0_param.param_str, state->current_loop_depth);
4653 ++state->current_loop_depth;
4656 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4658 struct glsl_src_param src0_param;
4660 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4661 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4664 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4666 struct glsl_src_param src0_param;
4668 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4669 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4672 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4674 shader_addline(ins->ctx->buffer, "default:\n");
4677 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4679 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4680 struct glsl_src_param src0_param;
4682 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4683 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4686 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4688 struct glsl_src_param src0_param;
4689 struct glsl_src_param src1_param;
4691 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4692 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4694 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4695 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4698 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4700 shader_addline(ins->ctx->buffer, "} else {\n");
4703 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4705 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4707 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4708 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4709 shader_glsl_fixup_position(ins->ctx->buffer);
4711 if (!stream)
4712 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4713 else
4714 FIXME("Unhandled primitive stream %u.\n", stream);
4717 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4719 shader_addline(ins->ctx->buffer, "break;\n");
4722 /* FIXME: According to MSDN the compare is done per component. */
4723 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4725 struct glsl_src_param src0_param;
4726 struct glsl_src_param src1_param;
4728 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4729 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4731 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4732 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4735 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4737 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4738 struct glsl_src_param src_param;
4740 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4741 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4744 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4746 shader_addline(ins->ctx->buffer, "continue;\n");
4749 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4751 shader_addline(ins->ctx->buffer, "}\n");
4752 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4754 /* Subroutines appear at the end of the shader. */
4755 ins->ctx->state->in_subroutine = TRUE;
4758 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4760 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4763 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4765 struct glsl_src_param src1_param;
4767 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4768 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4769 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4772 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4774 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4776 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4778 shader_glsl_generate_shader_epilogue(ins->ctx);
4779 shader_addline(ins->ctx->buffer, "return;\n");
4783 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4785 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4786 ins->ctx->reg_maps->shader_version.minor);
4787 struct glsl_sample_function sample_function;
4788 DWORD sample_flags = 0;
4789 DWORD resource_idx;
4790 DWORD mask = 0, swizzle;
4791 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4793 /* 1.0-1.4: Use destination register as sampler source.
4794 * 2.0+: Use provided sampler source. */
4795 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4796 resource_idx = ins->dst[0].reg.idx[0].offset;
4797 else
4798 resource_idx = ins->src[1].reg.idx[0].offset;
4800 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4802 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4803 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4804 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4806 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4807 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4809 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4810 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4812 case WINED3D_TTFF_COUNT1:
4813 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4814 break;
4815 case WINED3D_TTFF_COUNT2:
4816 mask = WINED3DSP_WRITEMASK_1;
4817 break;
4818 case WINED3D_TTFF_COUNT3:
4819 mask = WINED3DSP_WRITEMASK_2;
4820 break;
4821 case WINED3D_TTFF_COUNT4:
4822 case WINED3D_TTFF_DISABLE:
4823 mask = WINED3DSP_WRITEMASK_3;
4824 break;
4828 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4830 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4832 if (src_mod == WINED3DSPSM_DZ) {
4833 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4834 mask = WINED3DSP_WRITEMASK_2;
4835 } else if (src_mod == WINED3DSPSM_DW) {
4836 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4837 mask = WINED3DSP_WRITEMASK_3;
4840 else
4842 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4843 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4845 /* ps 2.0 texldp instruction always divides by the fourth component. */
4846 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4847 mask = WINED3DSP_WRITEMASK_3;
4851 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4852 mask |= sample_function.coord_mask;
4853 sample_function.coord_mask = mask;
4855 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4856 else swizzle = ins->src[1].swizzle;
4858 /* 1.0-1.3: Use destination register as coordinate source.
4859 1.4+: Use provided coordinate source register. */
4860 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4862 char coord_mask[6];
4863 shader_glsl_write_mask_to_str(mask, coord_mask);
4864 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4865 "T%u%s", resource_idx, coord_mask);
4867 else
4869 struct glsl_src_param coord_param;
4870 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4871 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4873 struct glsl_src_param bias;
4874 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4875 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4876 NULL, "%s", coord_param.param_str);
4877 } else {
4878 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4879 "%s", coord_param.param_str);
4882 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4885 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4887 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4888 struct glsl_src_param coord_param, dx_param, dy_param;
4889 struct glsl_sample_function sample_function;
4890 DWORD sampler_idx;
4891 DWORD swizzle = ins->src[1].swizzle;
4893 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4894 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4896 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4897 shader_glsl_tex(ins);
4898 return;
4901 sampler_idx = ins->src[1].reg.idx[0].offset;
4903 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4904 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4905 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4906 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4908 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4909 NULL, NULL, "%s", coord_param.param_str);
4910 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4913 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4915 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4916 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4917 struct glsl_src_param coord_param, lod_param;
4918 struct glsl_sample_function sample_function;
4919 DWORD swizzle = ins->src[1].swizzle;
4920 DWORD sampler_idx;
4922 sampler_idx = ins->src[1].reg.idx[0].offset;
4924 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4925 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4927 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4929 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4930 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4932 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4933 * However, the NVIDIA drivers allow them in fragment shaders as well,
4934 * even without the appropriate extension. */
4935 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4937 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4938 "%s", coord_param.param_str);
4939 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4942 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4943 unsigned int resource_idx, unsigned int sampler_idx)
4945 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4946 unsigned int i;
4948 for (i = 0; i < sampler_map->count; ++i)
4950 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4951 return entries[i].bind_idx;
4954 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4956 return ~0u;
4959 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
4961 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
4962 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
4963 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4964 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4965 struct glsl_src_param coord_param, data_param, data_param2;
4966 enum wined3d_shader_resource_type resource_type;
4967 enum wined3d_data_type data_type;
4968 unsigned int uav_idx;
4969 DWORD coord_mask;
4970 const char *op;
4972 uav_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
4973 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4974 if (resource_type >= ARRAY_SIZE(resource_type_info))
4976 ERR("Unexpected resource type %#x.\n", resource_type);
4977 return;
4979 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4980 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4982 switch (ins->handler_idx)
4984 case WINED3DSIH_ATOMIC_AND:
4985 case WINED3DSIH_IMM_ATOMIC_AND:
4986 op = "imageAtomicAnd";
4987 break;
4988 case WINED3DSIH_ATOMIC_CMP_STORE:
4989 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
4990 op = "imageAtomicCompSwap";
4991 break;
4992 case WINED3DSIH_ATOMIC_IADD:
4993 case WINED3DSIH_IMM_ATOMIC_IADD:
4994 op = "imageAtomicAdd";
4995 break;
4996 case WINED3DSIH_ATOMIC_OR:
4997 case WINED3DSIH_IMM_ATOMIC_OR:
4998 op = "imageAtomicOr";
4999 break;
5000 case WINED3DSIH_ATOMIC_XOR:
5001 case WINED3DSIH_IMM_ATOMIC_XOR:
5002 op = "imageAtomicXor";
5003 break;
5004 case WINED3DSIH_IMM_ATOMIC_EXCH:
5005 op = "imageAtomicExchange";
5006 break;
5007 default:
5008 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5009 return;
5012 if (is_imm_instruction)
5013 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5015 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &coord_param);
5017 if (reg_maps->uav_resource_info[uav_idx].flags & WINED3D_VIEW_BUFFER_RAW)
5018 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s / 4, ",
5019 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
5020 else
5021 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s, ",
5022 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
5024 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data_param, data_type);
5025 shader_addline(ins->ctx->buffer, "%s", data_param.param_str);
5026 if (ins->src_count >= 3)
5028 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data_param2, data_type);
5029 shader_addline(ins->ctx->buffer, ", %s", data_param2.param_str);
5032 if (is_imm_instruction)
5033 shader_addline(ins->ctx->buffer, ")");
5034 shader_addline(ins->ctx->buffer, ");\n");
5037 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5039 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5040 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5041 enum wined3d_shader_resource_type resource_type;
5042 struct glsl_src_param image_coord_param;
5043 enum wined3d_data_type data_type;
5044 DWORD coord_mask, write_mask;
5045 unsigned int uav_idx;
5046 char dst_swizzle[6];
5048 uav_idx = ins->src[1].reg.idx[0].offset;
5049 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5051 ERR("Invalid UAV index %u.\n", uav_idx);
5052 return;
5054 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5055 if (resource_type >= ARRAY_SIZE(resource_type_info))
5057 ERR("Unexpected resource type %#x.\n", resource_type);
5058 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5060 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5061 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5063 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5064 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5066 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5067 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5068 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5071 static void shader_glsl_ld_raw(const struct wined3d_shader_instruction *ins)
5073 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5074 const struct wined3d_shader_src_param *src = &ins->src[1];
5075 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5076 struct wined3d_shader_dst_param dst;
5077 const char *function, *resource;
5078 struct glsl_src_param offset;
5079 unsigned int i, swizzle;
5081 if (src->reg.type == WINED3DSPR_RESOURCE)
5083 function = "texelFetch";
5084 resource = "sampler";
5086 else
5088 function = "imageLoad";
5089 resource = "image";
5092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &offset);
5094 dst = ins->dst[0];
5095 for (i = 0; i < 4; ++i)
5097 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5098 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5099 continue;
5101 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5102 shader_addline(buffer, "%s(%s_%s%u, %s / 4 + %u).x);\n",
5103 function, prefix, resource, src->reg.idx[0].offset, offset.param_str, swizzle);
5107 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5109 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5110 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5111 struct glsl_src_param image_coord_param, image_data_param;
5112 enum wined3d_shader_resource_type resource_type;
5113 enum wined3d_data_type data_type;
5114 unsigned int uav_idx;
5115 DWORD coord_mask;
5117 uav_idx = ins->dst[0].reg.idx[0].offset;
5118 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5120 ERR("Invalid UAV index %u.\n", uav_idx);
5121 return;
5123 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5124 if (resource_type >= ARRAY_SIZE(resource_type_info))
5126 ERR("Unexpected resource type %#x.\n", resource_type);
5127 return;
5129 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5130 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5132 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5133 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5134 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5135 shader_glsl_get_prefix(version->type), uav_idx,
5136 image_coord_param.param_str, image_data_param.param_str);
5139 static void shader_glsl_store_buffer(const struct wined3d_shader_instruction *ins)
5141 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5142 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5143 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5144 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5145 struct glsl_src_param structure_idx, offset, data;
5146 struct wined3d_string_buffer *address;
5147 unsigned int i, uav_idx, src_idx = 0;
5148 DWORD write_mask;
5150 uav_idx = ins->dst[0].reg.idx[0].offset;
5151 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5153 ERR("Invalid UAV index %u.\n", uav_idx);
5154 return;
5157 address = string_buffer_get(priv->string_buffers);
5158 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5160 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5161 shader_addline(address, "%s * %u + ", structure_idx.param_str,
5162 reg_maps->uav_resource_info[uav_idx].stride);
5164 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5165 shader_addline(address, "%s / 4", offset.param_str);
5167 for (i = 0; i < 4; ++i)
5169 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5170 continue;
5172 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5174 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5175 prefix, uav_idx, address->buffer, i, data.param_str);
5178 string_buffer_release(priv->string_buffers, address);
5181 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5183 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5184 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5185 enum wined3d_shader_resource_type resource_type;
5186 enum wined3d_shader_register_type reg_type;
5187 unsigned int resource_idx, bind_idx, i;
5188 enum wined3d_data_type dst_data_type;
5189 struct glsl_src_param lod_param;
5190 char dst_swizzle[6];
5191 DWORD write_mask;
5193 dst_data_type = ins->dst[0].reg.data_type;
5194 if (ins->flags == WINED3DSI_RESINFO_UINT)
5195 dst_data_type = WINED3D_DATA_UINT;
5196 else if (ins->flags)
5197 FIXME("Unhandled flags %#x.\n", ins->flags);
5199 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5200 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5202 reg_type = ins->src[1].reg.type;
5203 resource_idx = ins->src[1].reg.idx[0].offset;
5204 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5205 if (reg_type == WINED3DSPR_RESOURCE)
5207 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5208 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5209 resource_idx, WINED3D_SAMPLER_DEFAULT);
5211 else
5213 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5214 bind_idx = resource_idx;
5217 if (resource_type >= ARRAY_SIZE(resource_type_info))
5219 ERR("Unexpected resource type %#x.\n", resource_type);
5220 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5223 if (dst_data_type == WINED3D_DATA_UINT)
5224 shader_addline(ins->ctx->buffer, "uvec4(");
5225 else
5226 shader_addline(ins->ctx->buffer, "vec4(");
5228 if (reg_type == WINED3DSPR_RESOURCE)
5230 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5231 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5233 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5234 shader_addline(ins->ctx->buffer, "0, ");
5236 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5238 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5239 shader_glsl_get_prefix(version->type), bind_idx);
5241 else
5243 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5244 shader_addline(ins->ctx->buffer, "1");
5247 else
5249 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5250 shader_glsl_get_prefix(version->type), bind_idx);
5252 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5253 shader_addline(ins->ctx->buffer, "0, ");
5255 /* For UAVs the returned miplevel count is always 1. */
5256 shader_addline(ins->ctx->buffer, "1");
5259 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5262 /* FIXME: The current implementation does not handle multisample textures correctly. */
5263 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5265 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5266 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5267 struct glsl_src_param coord_param, lod_param;
5268 struct glsl_sample_function sample_function;
5269 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5270 BOOL has_lod_param;
5272 if (wined3d_shader_instruction_has_texel_offset(ins))
5273 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5275 resource_idx = ins->src[1].reg.idx[0].offset;
5276 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5278 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5280 ERR("Invalid resource index %u.\n", resource_idx);
5281 return;
5283 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5285 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5286 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5287 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5288 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5289 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5290 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5291 "%s", coord_param.param_str);
5292 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5295 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5297 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5298 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5299 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5300 struct glsl_sample_function sample_function;
5301 DWORD flags = 0;
5303 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5304 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5305 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5306 flags |= WINED3D_GLSL_SAMPLE_LOD;
5307 if (wined3d_shader_instruction_has_texel_offset(ins))
5308 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5310 resource_idx = ins->src[1].reg.idx[0].offset;
5311 sampler_idx = ins->src[2].reg.idx[0].offset;
5313 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5314 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5316 switch (ins->handler_idx)
5318 case WINED3DSIH_SAMPLE:
5319 break;
5320 case WINED3DSIH_SAMPLE_B:
5321 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5322 lod_param_str = lod_param.param_str;
5323 break;
5324 case WINED3DSIH_SAMPLE_GRAD:
5325 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5326 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5327 dx_param_str = dx_param.param_str;
5328 dy_param_str = dy_param.param_str;
5329 break;
5330 case WINED3DSIH_SAMPLE_LOD:
5331 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5332 lod_param_str = lod_param.param_str;
5333 break;
5334 default:
5335 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5336 break;
5339 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5340 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5341 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5342 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5345 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5347 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5348 struct glsl_src_param coord_param, compare_param;
5349 struct glsl_sample_function sample_function;
5350 const char *lod_param = NULL;
5351 DWORD flags = 0;
5352 UINT coord_size;
5354 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5356 lod_param = "0";
5357 flags |= WINED3D_GLSL_SAMPLE_LOD;
5360 if (wined3d_shader_instruction_has_texel_offset(ins))
5361 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5363 resource_idx = ins->src[1].reg.idx[0].offset;
5364 sampler_idx = ins->src[2].reg.idx[0].offset;
5366 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5367 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5368 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5369 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5370 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5371 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5372 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5373 coord_size, coord_param.param_str, compare_param.param_str);
5374 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5377 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5379 /* FIXME: Make this work for more than just 2D textures */
5380 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5381 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5383 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5385 char dst_mask[6];
5387 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5388 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5389 ins->dst[0].reg.idx[0].offset, dst_mask);
5391 else
5393 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5394 DWORD reg = ins->src[0].reg.idx[0].offset;
5395 char dst_swizzle[6];
5397 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5399 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5401 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5402 struct glsl_src_param div_param;
5403 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5405 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5407 if (mask_size > 1)
5408 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5409 else
5410 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5412 else
5414 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5419 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5420 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5421 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5422 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5424 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5425 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5426 struct glsl_sample_function sample_function;
5427 struct glsl_src_param src0_param;
5428 UINT mask_size;
5430 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5432 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5433 * scalar, and projected sampling would require 4.
5435 * It is a dependent read - not valid with conditional NP2 textures
5437 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5438 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5440 switch(mask_size)
5442 case 1:
5443 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5444 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5445 break;
5447 case 2:
5448 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5449 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5450 break;
5452 case 3:
5453 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5454 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5455 break;
5457 default:
5458 FIXME("Unexpected mask size %u\n", mask_size);
5459 break;
5461 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5464 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5465 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5466 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5468 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5469 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5470 struct glsl_src_param src0_param;
5471 DWORD dst_mask;
5472 unsigned int mask_size;
5474 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5475 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5476 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5478 if (mask_size > 1) {
5479 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5480 } else {
5481 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5485 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5486 * Calculate the depth as dst.x / dst.y */
5487 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5489 struct glsl_dst_param dst_param;
5491 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5493 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5494 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5495 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5496 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5497 * >= 1.0 or < 0.0
5499 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5500 dst_param.reg_name, dst_param.reg_name);
5503 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5504 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5505 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5506 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5508 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5510 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5511 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5512 struct glsl_src_param src0_param;
5514 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5516 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5517 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5520 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5521 * Calculate the 1st of a 2-row matrix multiplication. */
5522 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5524 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5525 DWORD reg = ins->dst[0].reg.idx[0].offset;
5526 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5527 struct glsl_src_param src0_param;
5529 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5530 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5533 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5534 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5535 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5537 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5538 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5539 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5540 DWORD reg = ins->dst[0].reg.idx[0].offset;
5541 struct glsl_src_param src0_param;
5543 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5544 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5545 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5548 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5550 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5551 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5552 struct glsl_sample_function sample_function;
5553 DWORD reg = ins->dst[0].reg.idx[0].offset;
5554 struct glsl_src_param src0_param;
5556 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5557 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5559 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5561 /* Sample the texture using the calculated coordinates */
5562 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5563 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5566 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5567 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5568 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5570 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5571 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5572 struct glsl_sample_function sample_function;
5573 DWORD reg = ins->dst[0].reg.idx[0].offset;
5574 struct glsl_src_param src0_param;
5576 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5577 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5579 /* Dependent read, not valid with conditional NP2 */
5580 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5582 /* Sample the texture using the calculated coordinates */
5583 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5584 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5586 tex_mx->current_row = 0;
5589 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5590 * Perform the 3rd row of a 3x3 matrix multiply */
5591 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5593 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5594 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5595 DWORD reg = ins->dst[0].reg.idx[0].offset;
5596 struct glsl_src_param src0_param;
5597 char dst_mask[6];
5599 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5601 shader_glsl_append_dst(ins->ctx->buffer, ins);
5602 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5603 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5605 tex_mx->current_row = 0;
5608 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5609 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5610 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5612 struct glsl_src_param src0_param;
5613 struct glsl_src_param src1_param;
5614 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5615 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5616 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5617 struct glsl_sample_function sample_function;
5618 DWORD reg = ins->dst[0].reg.idx[0].offset;
5619 char coord_mask[6];
5621 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5622 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5624 /* Perform the last matrix multiply operation */
5625 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5626 /* Reflection calculation */
5627 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5629 /* Dependent read, not valid with conditional NP2 */
5630 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5631 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5633 /* Sample the texture */
5634 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5635 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5636 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5638 tex_mx->current_row = 0;
5641 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5642 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5643 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5645 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5646 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5647 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5648 struct glsl_sample_function sample_function;
5649 DWORD reg = ins->dst[0].reg.idx[0].offset;
5650 struct glsl_src_param src0_param;
5651 char coord_mask[6];
5653 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5655 /* Perform the last matrix multiply operation */
5656 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5658 /* Construct the eye-ray vector from w coordinates */
5659 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5660 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5661 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5663 /* Dependent read, not valid with conditional NP2 */
5664 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5665 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5667 /* Sample the texture using the calculated coordinates */
5668 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5669 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5670 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5672 tex_mx->current_row = 0;
5675 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5676 * Apply a fake bump map transform.
5677 * texbem is pshader <= 1.3 only, this saves a few version checks
5679 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5681 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5682 struct glsl_sample_function sample_function;
5683 struct glsl_src_param coord_param;
5684 DWORD sampler_idx;
5685 DWORD mask;
5686 DWORD flags;
5687 char coord_mask[6];
5689 sampler_idx = ins->dst[0].reg.idx[0].offset;
5690 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5691 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5693 /* Dependent read, not valid with conditional NP2 */
5694 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5695 mask = sample_function.coord_mask;
5697 shader_glsl_write_mask_to_str(mask, coord_mask);
5699 /* With projected textures, texbem only divides the static texture coord,
5700 * not the displacement, so we can't let GL handle this. */
5701 if (flags & WINED3D_PSARGS_PROJECTED)
5703 DWORD div_mask=0;
5704 char coord_div_mask[3];
5705 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5707 case WINED3D_TTFF_COUNT1:
5708 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5709 break;
5710 case WINED3D_TTFF_COUNT2:
5711 div_mask = WINED3DSP_WRITEMASK_1;
5712 break;
5713 case WINED3D_TTFF_COUNT3:
5714 div_mask = WINED3DSP_WRITEMASK_2;
5715 break;
5716 case WINED3D_TTFF_COUNT4:
5717 case WINED3D_TTFF_DISABLE:
5718 div_mask = WINED3DSP_WRITEMASK_3;
5719 break;
5721 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5722 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5725 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5727 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5728 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5729 coord_param.param_str, coord_mask);
5731 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5733 struct glsl_src_param luminance_param;
5734 struct glsl_dst_param dst_param;
5736 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5737 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5739 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5740 dst_param.reg_name, dst_param.mask_str,
5741 luminance_param.param_str, sampler_idx, sampler_idx);
5743 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5746 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5748 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5749 struct glsl_src_param src0_param, src1_param;
5751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5752 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5754 shader_glsl_append_dst(ins->ctx->buffer, ins);
5755 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5756 src0_param.param_str, sampler_idx, src1_param.param_str);
5759 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5760 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5761 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5763 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5764 struct glsl_sample_function sample_function;
5765 struct glsl_src_param src0_param;
5767 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5769 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5770 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5771 "%s.wx", src0_param.reg_name);
5772 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5775 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5776 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5777 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5779 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5780 struct glsl_sample_function sample_function;
5781 struct glsl_src_param src0_param;
5783 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5785 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5786 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5787 "%s.yz", src0_param.reg_name);
5788 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5791 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5792 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5793 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5795 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5796 struct glsl_sample_function sample_function;
5797 struct glsl_src_param src0_param;
5799 /* Dependent read, not valid with conditional NP2 */
5800 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5801 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5803 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5804 "%s", src0_param.param_str);
5805 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5808 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5809 * If any of the first 3 components are < 0, discard this pixel */
5810 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5812 if (ins->ctx->reg_maps->shader_version.major >= 4)
5814 struct glsl_src_param src_param;
5816 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5817 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
5819 else
5821 struct glsl_dst_param dst_param;
5823 /* The argument is a destination parameter, and no writemasks are allowed */
5824 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5826 /* 2.0 shaders compare all 4 components in texkill. */
5827 if (ins->ctx->reg_maps->shader_version.major >= 2)
5828 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5829 /* 1.x shaders only compare the first 3 components, probably due to
5830 * the nature of the texkill instruction as a tex* instruction, and
5831 * phase, which kills all .w components. Even if all 4 components are
5832 * defined, only the first 3 are used. */
5833 else
5834 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5838 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5839 * dst = dot2(src0, src1) + src2 */
5840 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5842 struct glsl_src_param src0_param;
5843 struct glsl_src_param src1_param;
5844 struct glsl_src_param src2_param;
5845 DWORD write_mask;
5846 unsigned int mask_size;
5848 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5849 mask_size = shader_glsl_get_write_mask_size(write_mask);
5851 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5852 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5853 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5855 if (mask_size > 1) {
5856 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5857 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5858 } else {
5859 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5860 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5864 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5865 const struct wined3d_shader_signature *input_signature,
5866 const struct wined3d_shader_reg_maps *reg_maps,
5867 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5869 unsigned int i;
5871 for (i = 0; i < input_signature->element_count; ++i)
5873 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5874 const char *semantic_name;
5875 UINT semantic_idx;
5876 char reg_mask[6];
5878 /* Unused */
5879 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5880 continue;
5882 semantic_name = input->semantic_name;
5883 semantic_idx = input->semantic_idx;
5884 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5886 if (args->vp_mode == vertexshader)
5888 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5890 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5891 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5893 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5895 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5897 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5899 shader_addline(buffer, "ps_in[%u] = vec4("
5900 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5901 input->register_idx);
5903 else
5905 if (input->sysval_semantic)
5906 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5907 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5908 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5909 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5912 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5914 if (args->pointsprite)
5915 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5916 shader->u.ps.input_reg_map[input->register_idx]);
5917 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5918 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5919 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5920 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5921 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5922 else
5923 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5924 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5926 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5928 if (!semantic_idx)
5929 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5930 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5931 else if (semantic_idx == 1)
5932 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5933 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5934 else
5935 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5936 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5938 else
5940 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5941 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5946 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5948 struct glsl_program_key key;
5950 key.vs_id = entry->vs.id;
5951 key.gs_id = entry->gs.id;
5952 key.ps_id = entry->ps.id;
5953 key.cs_id = entry->cs.id;
5955 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5957 ERR("Failed to insert program entry.\n");
5961 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5962 const struct glsl_program_key *key)
5964 struct wine_rb_entry *entry;
5966 entry = wine_rb_get(&priv->program_lookup, key);
5967 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5970 /* Context activation is done by the caller. */
5971 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5972 struct glsl_shader_prog_link *entry)
5974 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
5976 GL_EXTCALL(glDeleteProgram(entry->id));
5977 if (entry->vs.id)
5978 list_remove(&entry->vs.shader_entry);
5979 if (entry->gs.id)
5980 list_remove(&entry->gs.shader_entry);
5981 if (entry->ps.id)
5982 list_remove(&entry->ps.shader_entry);
5983 if (entry->cs.id)
5984 list_remove(&entry->cs.shader_entry);
5985 HeapFree(GetProcessHeap(), 0, entry);
5988 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5989 const struct wined3d_gl_info *gl_info, const DWORD *map,
5990 const struct wined3d_shader_signature *input_signature,
5991 const struct wined3d_shader_reg_maps *reg_maps_in,
5992 const struct wined3d_shader_signature *output_signature,
5993 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5995 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5996 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5997 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5998 unsigned int in_count = vec4_varyings(3, gl_info);
5999 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
6000 DWORD in_idx, *set = NULL;
6001 unsigned int i, j;
6002 char reg_mask[6];
6004 set = wined3d_calloc(max_varyings, sizeof(*set));
6006 for (i = 0; i < input_signature->element_count; ++i)
6008 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6010 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6011 continue;
6013 in_idx = map[input->register_idx];
6014 /* Declared, but not read register */
6015 if (in_idx == ~0u)
6016 continue;
6017 if (in_idx >= max_varyings)
6019 FIXME("More input varyings declared than supported, expect issues.\n");
6020 continue;
6023 if (in_idx == in_count)
6024 string_buffer_sprintf(destination, "gl_FrontColor");
6025 else if (in_idx == in_count + 1)
6026 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6027 else
6028 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6030 if (!set[in_idx])
6031 set[in_idx] = ~0u;
6033 for (j = 0; j < output_signature->element_count; ++j)
6035 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6036 DWORD mask;
6038 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6039 || input->semantic_idx != output->semantic_idx
6040 || strcmp(input->semantic_name, output->semantic_name)
6041 || !(mask = input->mask & output->mask))
6042 continue;
6044 if (set[in_idx] == ~0u)
6045 set[in_idx] = 0;
6046 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6047 shader_glsl_write_mask_to_str(mask, reg_mask);
6049 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6050 destination->buffer, reg_mask, output->register_idx, reg_mask);
6054 for (i = 0; i < max_varyings; ++i)
6056 unsigned int size;
6058 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6059 continue;
6061 if (set[i] == ~0u)
6062 set[i] = 0;
6064 size = 0;
6065 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6066 reg_mask[size++] = 'x';
6067 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6068 reg_mask[size++] = 'y';
6069 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6070 reg_mask[size++] = 'z';
6071 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6072 reg_mask[size++] = 'w';
6073 reg_mask[size] = '\0';
6075 if (i == in_count)
6076 string_buffer_sprintf(destination, "gl_FrontColor");
6077 else if (i == in_count + 1)
6078 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6079 else
6080 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6082 if (size == 1)
6083 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6084 else
6085 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6088 HeapFree(GetProcessHeap(), 0, set);
6089 string_buffer_release(&priv->string_buffers, destination);
6092 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6093 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6094 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6096 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6097 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6098 char reg_mask[6];
6099 unsigned int i;
6101 for (i = 0; i < output_signature->element_count; ++i)
6103 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6105 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6106 continue;
6108 if (output->register_idx >= input_count)
6109 continue;
6111 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6113 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6115 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6116 destination->buffer, reg_mask, output->register_idx, reg_mask);
6119 string_buffer_release(&priv->string_buffers, destination);
6122 /* Context activation is done by the caller. */
6123 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6124 const struct wined3d_shader *vs, unsigned int input_count,
6125 const struct wined3d_gl_info *gl_info)
6127 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6128 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6130 if (legacy_context)
6131 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6132 else
6133 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6134 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6136 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6137 legacy_context ? "gs_in" : "gs_in.gs_in");
6139 shader_addline(buffer, "}\n");
6142 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6143 const struct wined3d_gl_info *gl_info, const DWORD *map,
6144 const struct wined3d_shader_signature *input_signature,
6145 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6146 const struct wined3d_shader_signature *output_signature,
6147 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6149 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6150 const char *semantic_name;
6151 UINT semantic_idx;
6152 char reg_mask[6];
6153 unsigned int i;
6155 /* First, sort out position and point size system values. */
6156 for (i = 0; i < output_signature->element_count; ++i)
6158 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6160 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6161 continue;
6163 semantic_name = output->semantic_name;
6164 semantic_idx = output->semantic_idx;
6165 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6167 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6169 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6170 reg_mask, output->register_idx, reg_mask);
6172 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6174 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6175 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6177 else if (output->sysval_semantic)
6179 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6183 /* Then, setup the pixel shader input. */
6184 if (reg_maps_out->shader_version.major < 4)
6185 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6186 output_signature, reg_maps_out, "ps_link");
6187 else
6188 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6191 /* Context activation is done by the caller. */
6192 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6193 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6194 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6196 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6197 GLuint ret;
6198 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6199 unsigned int i;
6200 const char *semantic_name;
6201 UINT semantic_idx;
6202 char reg_mask[6];
6203 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6205 string_buffer_clear(buffer);
6207 shader_glsl_add_version_declaration(buffer, gl_info, &vs->reg_maps.shader_version);
6209 if (per_vertex_point_size)
6211 shader_addline(buffer, "uniform struct\n{\n");
6212 shader_addline(buffer, " float size_min;\n");
6213 shader_addline(buffer, " float size_max;\n");
6214 shader_addline(buffer, "} ffp_point;\n");
6217 if (ps_major < 3)
6219 DWORD colors_written_mask[2] = {0};
6220 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6222 if (!legacy_context)
6224 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6225 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6226 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6227 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6230 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6232 for (i = 0; i < vs->output_signature.element_count; ++i)
6234 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6235 DWORD write_mask;
6237 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6238 continue;
6240 semantic_name = output->semantic_name;
6241 semantic_idx = output->semantic_idx;
6242 write_mask = output->mask;
6243 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6245 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6247 if (legacy_context)
6248 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6249 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6250 else
6251 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6252 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6254 colors_written_mask[semantic_idx] = write_mask;
6256 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6258 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6259 reg_mask, output->register_idx, reg_mask);
6261 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6263 if (semantic_idx < MAX_TEXTURES)
6265 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6266 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6267 semantic_idx, reg_mask, output->register_idx, reg_mask);
6268 texcoords_written_mask[semantic_idx] = write_mask;
6271 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6273 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6274 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6276 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6278 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6279 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6280 output->register_idx, reg_mask[1]);
6284 for (i = 0; i < 2; ++i)
6286 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6288 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6289 if (!i)
6290 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6291 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6292 reg_mask, reg_mask);
6293 else
6294 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6295 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6296 reg_mask, reg_mask);
6299 for (i = 0; i < MAX_TEXTURES; ++i)
6301 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6302 continue;
6304 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6306 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6307 && !texcoords_written_mask[i])
6308 continue;
6310 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6311 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6312 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6316 else
6318 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6320 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6321 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6322 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6323 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6326 shader_addline(buffer, "}\n");
6328 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6329 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6330 shader_glsl_compile(gl_info, ret, buffer->buffer);
6332 return ret;
6335 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6336 const struct wined3d_shader *shader, unsigned int input_count,
6337 const struct wined3d_gl_info *gl_info)
6339 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6341 if (input_count)
6342 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6344 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6345 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6347 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6348 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6350 shader_addline(buffer, "}\n");
6353 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6354 const struct wined3d_gl_info *gl_info)
6356 const char *output = get_fragment_output(gl_info);
6358 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6359 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6360 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6361 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6362 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6363 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6366 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6367 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6369 const char *output = get_fragment_output(gl_info);
6371 switch (mode)
6373 case WINED3D_FFP_PS_FOG_OFF:
6374 return;
6376 case WINED3D_FFP_PS_FOG_LINEAR:
6377 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6378 break;
6380 case WINED3D_FFP_PS_FOG_EXP:
6381 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6382 break;
6384 case WINED3D_FFP_PS_FOG_EXP2:
6385 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6386 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6387 break;
6389 default:
6390 ERR("Invalid fog mode %#x.\n", mode);
6391 return;
6394 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6395 output, output);
6398 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6399 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6401 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6402 * flipping all the operators here, just negate the comparison below. */
6403 static const char * const comparison_operator[] =
6405 "", /* WINED3D_CMP_NEVER */
6406 "<", /* WINED3D_CMP_LESS */
6407 "==", /* WINED3D_CMP_EQUAL */
6408 "<=", /* WINED3D_CMP_LESSEQUAL */
6409 ">", /* WINED3D_CMP_GREATER */
6410 "!=", /* WINED3D_CMP_NOTEQUAL */
6411 ">=", /* WINED3D_CMP_GREATEREQUAL */
6412 "" /* WINED3D_CMP_ALWAYS */
6415 if (alpha_func == WINED3D_CMP_ALWAYS)
6416 return;
6418 if (alpha_func != WINED3D_CMP_NEVER)
6419 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6420 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6421 shader_addline(buffer, " discard;\n");
6424 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6425 const struct wined3d_gl_info *gl_info)
6427 if (gl_info->supported[ARB_GPU_SHADER5])
6428 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6429 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6430 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6431 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6432 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6433 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6434 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6435 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6436 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6437 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6438 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6439 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6440 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6441 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6442 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6443 if (gl_info->supported[EXT_GPU_SHADER4])
6444 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6445 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6446 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6449 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6450 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6451 const struct ps_compile_args *args)
6453 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6455 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6456 if (reg_maps->shader_version.major < 2)
6457 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6459 if (args->srgb_correction)
6460 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6462 /* SM < 3 does not replace the fog stage. */
6463 if (reg_maps->shader_version.major < 3)
6464 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6466 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6469 /* Context activation is done by the caller. */
6470 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6471 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6472 const struct wined3d_shader *shader,
6473 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6475 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6476 const struct wined3d_gl_info *gl_info = context->gl_info;
6477 struct shader_glsl_ctx_priv priv_ctx;
6478 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6480 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6481 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6483 memset(&priv_ctx, 0, sizeof(priv_ctx));
6484 priv_ctx.cur_ps_args = args;
6485 priv_ctx.cur_np2fixup_info = np2fixup_info;
6486 priv_ctx.string_buffers = string_buffers;
6488 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6490 shader_glsl_enable_extensions(buffer, gl_info);
6491 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6492 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6493 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6494 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6495 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6496 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6497 /* The spec says that it doesn't have to be explicitly enabled, but the
6498 * nvidia drivers write a warning if we don't do so. */
6499 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6500 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6502 /* Base Declarations */
6503 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6505 shader_addline(buffer, "void main()\n{\n");
6507 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6508 * add approximately 0.5. This causes off-by-one problems as spotted by
6509 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6510 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6511 * causes precision troubles when we just subtract 0.5.
6513 * To deal with that, just floor() the position. This will eliminate the
6514 * fraction on all cards.
6516 * TODO: Test how this behaves with multisampling.
6518 * An advantage of floor is that it works even if the driver doesn't add
6519 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6520 * to return in gl_FragCoord, even though coordinates specify the pixel
6521 * centers instead of the pixel corners. This code will behave correctly
6522 * on drivers that returns integer values. */
6523 if (reg_maps->vpos)
6525 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6526 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6527 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6528 shader_addline(buffer,
6529 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6530 else
6531 shader_addline(buffer,
6532 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6535 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6537 unsigned int i;
6538 WORD map = reg_maps->texcoord;
6540 if (legacy_context)
6542 if (glsl_is_color_reg_read(shader, 0))
6543 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6544 if (glsl_is_color_reg_read(shader, 1))
6545 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6548 for (i = 0; map; map >>= 1, ++i)
6550 if (map & 1)
6552 if (args->pointsprite)
6553 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6554 else if (args->texcoords_initialized & (1u << i))
6555 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6556 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6557 else
6558 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6559 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6563 if (legacy_context)
6564 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6567 /* Pack 3.0 inputs */
6568 if (reg_maps->shader_version.major >= 3)
6569 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6571 /* Base Shader Body */
6572 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6574 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6575 if (reg_maps->shader_version.major < 4)
6576 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6578 shader_addline(buffer, "}\n");
6580 TRACE("Compiling shader object %u.\n", shader_id);
6581 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6583 return shader_id;
6586 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6587 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6588 const struct vs_compile_args *args)
6590 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6591 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6592 unsigned int i;
6594 /* Unpack outputs. */
6595 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6597 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6598 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6599 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6600 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6602 if (reg_maps->shader_version.major < 3)
6604 if (args->fog_src == VS_FOG_Z)
6605 shader_addline(buffer, "%s = gl_Position.z;\n",
6606 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6607 else if (!reg_maps->fog)
6608 shader_addline(buffer, "%s = 0.0;\n",
6609 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6612 /* We always store the clipplanes without y inversion. */
6613 if (args->clip_enabled)
6615 if (legacy_context)
6616 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6617 else
6618 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6619 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6622 if (args->point_size && !args->per_vertex_point_size)
6623 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6625 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6626 shader_glsl_fixup_position(buffer);
6629 /* Context activation is done by the caller. */
6630 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6631 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6633 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6634 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6635 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6636 const struct wined3d_gl_info *gl_info = context->gl_info;
6637 struct shader_glsl_ctx_priv priv_ctx;
6639 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6640 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6642 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6644 shader_glsl_enable_extensions(buffer, gl_info);
6645 if (gl_info->supported[ARB_DRAW_INSTANCED])
6646 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6647 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6648 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6650 memset(&priv_ctx, 0, sizeof(priv_ctx));
6651 priv_ctx.cur_vs_args = args;
6652 priv_ctx.string_buffers = string_buffers;
6654 /* Base Declarations */
6655 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6657 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6658 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6660 if (reg_maps->shader_version.major >= 4)
6662 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6663 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6664 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6665 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6668 shader_addline(buffer, "void main()\n{\n");
6670 /* Base Shader Body */
6671 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6673 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6674 if (reg_maps->shader_version.major < 4)
6675 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6677 shader_addline(buffer, "}\n");
6679 TRACE("Compiling shader object %u.\n", shader_id);
6680 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6682 return shader_id;
6685 /* Context activation is done by the caller. */
6686 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6687 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6689 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6690 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6691 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6692 const struct wined3d_gl_info *gl_info = context->gl_info;
6693 struct shader_glsl_ctx_priv priv_ctx;
6694 GLuint shader_id;
6696 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6698 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6700 shader_glsl_enable_extensions(buffer, gl_info);
6701 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6702 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6704 memset(&priv_ctx, 0, sizeof(priv_ctx));
6705 priv_ctx.string_buffers = string_buffers;
6706 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6707 if (!gl_info->supported[ARB_CLIP_CONTROL])
6708 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6709 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6710 shader_addline(buffer, "void main()\n{\n");
6711 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6712 shader_addline(buffer, "}\n");
6714 TRACE("Compiling shader object %u.\n", shader_id);
6715 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6717 return shader_id;
6720 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6722 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6723 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6724 const struct wined3d_shader *shader = ctx->shader;
6726 switch (shader->reg_maps.shader_version.type)
6728 case WINED3D_SHADER_TYPE_PIXEL:
6729 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6730 break;
6731 case WINED3D_SHADER_TYPE_VERTEX:
6732 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
6733 break;
6734 case WINED3D_SHADER_TYPE_GEOMETRY:
6735 case WINED3D_SHADER_TYPE_COMPUTE:
6736 break;
6737 default:
6738 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6739 break;
6743 /* Context activation is done by the caller. */
6744 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
6745 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6746 const struct wined3d_shader *shader)
6748 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
6749 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6750 const struct wined3d_gl_info *gl_info = context->gl_info;
6751 struct shader_glsl_ctx_priv priv_ctx;
6752 GLuint shader_id;
6754 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
6756 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6758 shader_glsl_enable_extensions(buffer, gl_info);
6759 if (gl_info->supported[ARB_COMPUTE_SHADER])
6760 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
6762 memset(&priv_ctx, 0, sizeof(priv_ctx));
6763 priv_ctx.string_buffers = string_buffers;
6764 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6766 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
6767 thread_group_size->x, thread_group_size->y, thread_group_size->z);
6769 shader_addline(buffer, "void main()\n{\n");
6770 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6771 shader_addline(buffer, "}\n");
6773 TRACE("Compiling shader object %u.\n", shader_id);
6774 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6776 return shader_id;
6779 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6780 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6781 struct wined3d_shader *shader,
6782 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6784 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6785 struct glsl_shader_private *shader_data;
6786 struct ps_np2fixup_info *np2fixup;
6787 UINT i;
6788 DWORD new_size;
6789 GLuint ret;
6791 if (!shader->backend_data)
6793 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6794 if (!shader->backend_data)
6796 ERR("Failed to allocate backend data.\n");
6797 return 0;
6800 shader_data = shader->backend_data;
6801 gl_shaders = shader_data->gl_shaders.ps;
6803 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6804 * so a linear search is more performant than a hashmap or a binary search
6805 * (cache coherency etc)
6807 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6809 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6811 if (args->np2_fixup)
6812 *np2fixup_info = &gl_shaders[i].np2fixup;
6813 return gl_shaders[i].id;
6817 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6818 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6819 if (shader_data->num_gl_shaders)
6821 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6822 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6823 new_size * sizeof(*gl_shaders));
6825 else
6827 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6828 new_size = 1;
6831 if(!new_array) {
6832 ERR("Out of memory\n");
6833 return 0;
6835 shader_data->gl_shaders.ps = new_array;
6836 shader_data->shader_array_size = new_size;
6837 gl_shaders = new_array;
6840 gl_shaders[shader_data->num_gl_shaders].args = *args;
6842 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6843 memset(np2fixup, 0, sizeof(*np2fixup));
6844 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6846 pixelshader_update_resource_types(shader, args->tex_types);
6848 string_buffer_clear(buffer);
6849 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6850 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6852 return ret;
6855 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6856 const DWORD use_map)
6858 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6859 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6860 if (stored->point_size != new->point_size)
6861 return FALSE;
6862 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6863 return FALSE;
6864 if (stored->flatshading != new->flatshading)
6865 return FALSE;
6866 if (stored->next_shader_type != new->next_shader_type)
6867 return FALSE;
6868 if (stored->next_shader_input_count != new->next_shader_input_count)
6869 return FALSE;
6870 return stored->fog_src == new->fog_src;
6873 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6874 struct wined3d_shader *shader, const struct vs_compile_args *args)
6876 UINT i;
6877 DWORD new_size;
6878 DWORD use_map = context->stream_info.use_map;
6879 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6880 struct glsl_shader_private *shader_data;
6881 GLuint ret;
6883 if (!shader->backend_data)
6885 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6886 if (!shader->backend_data)
6888 ERR("Failed to allocate backend data.\n");
6889 return 0;
6892 shader_data = shader->backend_data;
6893 gl_shaders = shader_data->gl_shaders.vs;
6895 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6896 * so a linear search is more performant than a hashmap or a binary search
6897 * (cache coherency etc)
6899 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6901 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6902 return gl_shaders[i].id;
6905 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6907 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6908 if (shader_data->num_gl_shaders)
6910 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6911 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6912 new_size * sizeof(*gl_shaders));
6914 else
6916 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6917 new_size = 1;
6920 if(!new_array) {
6921 ERR("Out of memory\n");
6922 return 0;
6924 shader_data->gl_shaders.vs = new_array;
6925 shader_data->shader_array_size = new_size;
6926 gl_shaders = new_array;
6929 gl_shaders[shader_data->num_gl_shaders].args = *args;
6931 string_buffer_clear(&priv->shader_buffer);
6932 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6933 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6935 return ret;
6938 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6939 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6941 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6942 struct glsl_shader_private *shader_data;
6943 unsigned int i, new_size;
6944 GLuint ret;
6946 if (!shader->backend_data)
6948 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6950 ERR("Failed to allocate backend data.\n");
6951 return 0;
6954 shader_data = shader->backend_data;
6955 gl_shaders = shader_data->gl_shaders.gs;
6957 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6959 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6960 return gl_shaders[i].id;
6963 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6965 if (shader_data->num_gl_shaders)
6967 new_size = shader_data->shader_array_size + 1;
6968 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6969 new_size * sizeof(*new_array));
6971 else
6973 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6974 new_size = 1;
6977 if (!new_array)
6979 ERR("Failed to allocate GL shaders array.\n");
6980 return 0;
6982 shader_data->gl_shaders.gs = new_array;
6983 shader_data->shader_array_size = new_size;
6984 gl_shaders = new_array;
6986 string_buffer_clear(&priv->shader_buffer);
6987 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6988 gl_shaders[shader_data->num_gl_shaders].args = *args;
6989 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6991 return ret;
6994 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
6995 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6996 struct wined3d_shader *shader)
6998 struct glsl_cs_compiled_shader *gl_shaders;
6999 struct glsl_shader_private *shader_data;
7000 GLuint ret;
7002 if (!shader->backend_data)
7004 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7006 ERR("Failed to allocate backend data.\n");
7007 return 0;
7010 shader_data = shader->backend_data;
7011 gl_shaders = shader_data->gl_shaders.cs;
7013 /* No shader variants are used for compute shaders. */
7014 if (shader_data->num_gl_shaders)
7015 return gl_shaders[0].id;
7017 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7019 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
7021 ERR("Failed to allocate GL shader array.\n");
7022 return 0;
7024 shader_data->shader_array_size = 1;
7025 gl_shaders = shader_data->gl_shaders.cs;
7027 string_buffer_clear(buffer);
7028 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
7029 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7031 return ret;
7034 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
7036 switch (mcs)
7038 case WINED3D_MCS_MATERIAL:
7039 return material;
7040 case WINED3D_MCS_COLOR1:
7041 return "ffp_attrib_diffuse";
7042 case WINED3D_MCS_COLOR2:
7043 return "ffp_attrib_specular";
7044 default:
7045 ERR("Invalid material color source %#x.\n", mcs);
7046 return "<invalid>";
7050 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
7051 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
7053 const char *diffuse, *specular, *emissive, *ambient;
7054 unsigned int i, idx;
7056 if (!settings->lighting)
7058 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7059 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
7060 return;
7063 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
7064 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
7065 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
7066 shader_addline(buffer, "vec3 dir, dst;\n");
7067 shader_addline(buffer, "float att, t;\n");
7069 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
7070 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
7071 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
7072 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
7074 idx = 0;
7075 for (i = 0; i < settings->point_light_count; ++i, ++idx)
7077 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7078 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7079 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7080 shader_addline(buffer, "dst.x = 1.0;\n");
7081 if (legacy_lighting)
7083 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7084 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7086 else
7088 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7090 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7091 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7092 if (!legacy_lighting)
7093 shader_addline(buffer, "att = 1.0 / att;\n");
7094 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7095 if (!settings->normal)
7097 if (!legacy_lighting)
7098 shader_addline(buffer, "}\n");
7099 continue;
7101 shader_addline(buffer, "dir = normalize(dir);\n");
7102 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7103 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7104 if (settings->localviewer)
7105 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7106 else
7107 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7108 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7109 " * ffp_light[%u].specular) * att;\n", idx);
7110 if (!legacy_lighting)
7111 shader_addline(buffer, "}\n");
7114 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7116 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7117 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7118 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7119 shader_addline(buffer, "dst.x = 1.0;\n");
7120 if (legacy_lighting)
7122 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7123 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7125 else
7127 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7129 shader_addline(buffer, "dir = normalize(dir);\n");
7130 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7131 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7132 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7133 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7134 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7135 idx, idx, idx, idx);
7136 if (legacy_lighting)
7137 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7138 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7139 idx, idx, idx);
7140 else
7141 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7142 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7143 idx, idx, idx);
7144 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7145 if (!settings->normal)
7147 if (!legacy_lighting)
7148 shader_addline(buffer, "}\n");
7149 continue;
7151 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7152 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7153 if (settings->localviewer)
7154 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7155 else
7156 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7157 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7158 " * ffp_light[%u].specular) * att;\n", idx);
7159 if (!legacy_lighting)
7160 shader_addline(buffer, "}\n");
7163 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7165 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7166 if (!settings->normal)
7167 continue;
7168 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7169 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7170 " * ffp_light[%u].diffuse.xyz;\n", idx);
7171 /* TODO: In the non-local viewer case the halfvector is constant
7172 * and could be precomputed and stored in a uniform. */
7173 if (settings->localviewer)
7174 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7175 else
7176 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7177 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7178 " * ffp_light[%u].specular;\n", idx);
7181 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7183 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7184 if (!settings->normal)
7185 continue;
7186 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7187 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7188 " * ffp_light[%u].diffuse.xyz;\n", idx);
7189 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7190 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7191 " * ffp_light[%u].specular;\n", idx);
7194 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7195 ambient, diffuse, emissive);
7196 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7197 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7200 /* Context activation is done by the caller. */
7201 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7202 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7204 static const struct attrib_info
7206 const char type[6];
7207 const char name[24];
7209 attrib_info[] =
7211 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7212 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7213 /* TODO: Indexed vertex blending */
7214 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7215 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7216 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7217 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7218 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7220 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7221 BOOL legacy_lighting = priv->legacy_lighting;
7222 GLuint shader_obj;
7223 unsigned int i;
7224 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7225 BOOL output_legacy_fogcoord = legacy_context;
7227 string_buffer_clear(buffer);
7229 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7231 if (shader_glsl_use_explicit_attrib_location(gl_info))
7232 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7234 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7236 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7238 if (shader_glsl_use_explicit_attrib_location(gl_info))
7239 shader_addline(buffer, "layout(location = %u) ", i);
7240 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7242 shader_addline(buffer, "\n");
7244 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7245 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7246 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7247 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7249 shader_addline(buffer, "uniform struct\n{\n");
7250 shader_addline(buffer, " vec4 emissive;\n");
7251 shader_addline(buffer, " vec4 ambient;\n");
7252 shader_addline(buffer, " vec4 diffuse;\n");
7253 shader_addline(buffer, " vec4 specular;\n");
7254 shader_addline(buffer, " float shininess;\n");
7255 shader_addline(buffer, "} ffp_material;\n");
7257 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7258 shader_addline(buffer, "uniform struct\n{\n");
7259 shader_addline(buffer, " vec4 diffuse;\n");
7260 shader_addline(buffer, " vec4 specular;\n");
7261 shader_addline(buffer, " vec4 ambient;\n");
7262 shader_addline(buffer, " vec4 position;\n");
7263 shader_addline(buffer, " vec3 direction;\n");
7264 shader_addline(buffer, " float range;\n");
7265 shader_addline(buffer, " float falloff;\n");
7266 shader_addline(buffer, " float c_att;\n");
7267 shader_addline(buffer, " float l_att;\n");
7268 shader_addline(buffer, " float q_att;\n");
7269 shader_addline(buffer, " float cos_htheta;\n");
7270 shader_addline(buffer, " float cos_hphi;\n");
7271 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7273 if (settings->point_size)
7275 shader_addline(buffer, "uniform struct\n{\n");
7276 shader_addline(buffer, " float size;\n");
7277 shader_addline(buffer, " float size_min;\n");
7278 shader_addline(buffer, " float size_max;\n");
7279 shader_addline(buffer, " float c_att;\n");
7280 shader_addline(buffer, " float l_att;\n");
7281 shader_addline(buffer, " float q_att;\n");
7282 shader_addline(buffer, "} ffp_point;\n");
7285 if (legacy_context)
7287 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7288 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7289 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7290 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7292 else
7294 if (settings->clipping)
7295 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7297 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7298 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7299 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7300 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7303 shader_addline(buffer, "\nvoid main()\n{\n");
7304 shader_addline(buffer, "float m;\n");
7305 shader_addline(buffer, "vec3 r;\n");
7307 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7309 if (attrib_info[i].name[0])
7310 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7311 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7313 for (i = 0; i < MAX_TEXTURES; ++i)
7315 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7316 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7317 && settings->texcoords & (1u << i))
7318 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7321 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7323 if (settings->transformed)
7325 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7326 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7327 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7329 else
7331 for (i = 0; i < settings->vertexblends; ++i)
7332 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7334 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7335 for (i = 0; i < settings->vertexblends + 1; ++i)
7336 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7338 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7339 if (settings->clipping)
7341 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7342 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7343 else
7344 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7345 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7347 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7350 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7351 if (settings->normal)
7353 if (!settings->vertexblends)
7355 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7357 else
7359 for (i = 0; i < settings->vertexblends + 1; ++i)
7360 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7363 if (settings->normalize)
7364 shader_addline(buffer, "normal = normalize(normal);\n");
7367 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7368 if (legacy_context)
7370 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7371 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7373 else
7375 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7376 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7379 for (i = 0; i < MAX_TEXTURES; ++i)
7381 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7383 switch (settings->texgen[i] & 0xffff0000)
7385 case WINED3DTSS_TCI_PASSTHRU:
7386 if (settings->texcoords & (1u << i))
7387 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7388 i, i, i);
7389 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7390 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7391 else
7392 output_legacy_texcoord = FALSE;
7393 break;
7395 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7396 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7397 break;
7399 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7400 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7401 break;
7403 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7404 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7405 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7406 break;
7408 case WINED3DTSS_TCI_SPHEREMAP:
7409 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7410 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7411 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7412 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7413 break;
7415 default:
7416 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7417 break;
7419 if (output_legacy_texcoord)
7420 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7423 switch (settings->fog_mode)
7425 case WINED3D_FFP_VS_FOG_OFF:
7426 output_legacy_fogcoord = FALSE;
7427 break;
7429 case WINED3D_FFP_VS_FOG_FOGCOORD:
7430 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7431 break;
7433 case WINED3D_FFP_VS_FOG_RANGE:
7434 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7435 break;
7437 case WINED3D_FFP_VS_FOG_DEPTH:
7438 if (settings->ortho_fog)
7440 if (gl_info->supported[ARB_CLIP_CONTROL])
7441 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7442 else
7443 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7444 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7446 else if (settings->transformed)
7448 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7450 else
7452 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7454 break;
7456 default:
7457 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7458 break;
7460 if (output_legacy_fogcoord)
7461 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7463 if (settings->point_size)
7465 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7466 " + ffp_point.l_att * length(ec_pos.xyz)"
7467 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7468 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7469 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7472 shader_addline(buffer, "}\n");
7474 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7475 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7477 return shader_obj;
7480 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7481 DWORD argnum, unsigned int stage, DWORD arg)
7483 const char *ret;
7485 if (arg == ARG_UNUSED)
7486 return "<unused arg>";
7488 switch (arg & WINED3DTA_SELECTMASK)
7490 case WINED3DTA_DIFFUSE:
7491 ret = "ffp_varying_diffuse";
7492 break;
7494 case WINED3DTA_CURRENT:
7495 ret = "ret";
7496 break;
7498 case WINED3DTA_TEXTURE:
7499 switch (stage)
7501 case 0: ret = "tex0"; break;
7502 case 1: ret = "tex1"; break;
7503 case 2: ret = "tex2"; break;
7504 case 3: ret = "tex3"; break;
7505 case 4: ret = "tex4"; break;
7506 case 5: ret = "tex5"; break;
7507 case 6: ret = "tex6"; break;
7508 case 7: ret = "tex7"; break;
7509 default:
7510 ret = "<invalid texture>";
7511 break;
7513 break;
7515 case WINED3DTA_TFACTOR:
7516 ret = "tex_factor";
7517 break;
7519 case WINED3DTA_SPECULAR:
7520 ret = "ffp_varying_specular";
7521 break;
7523 case WINED3DTA_TEMP:
7524 ret = "temp_reg";
7525 break;
7527 case WINED3DTA_CONSTANT:
7528 switch (stage)
7530 case 0: ret = "tss_const0"; break;
7531 case 1: ret = "tss_const1"; break;
7532 case 2: ret = "tss_const2"; break;
7533 case 3: ret = "tss_const3"; break;
7534 case 4: ret = "tss_const4"; break;
7535 case 5: ret = "tss_const5"; break;
7536 case 6: ret = "tss_const6"; break;
7537 case 7: ret = "tss_const7"; break;
7538 default:
7539 ret = "<invalid constant>";
7540 break;
7542 break;
7544 default:
7545 return "<unhandled arg>";
7548 if (arg & WINED3DTA_COMPLEMENT)
7550 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7551 if (argnum == 0)
7552 ret = "arg0";
7553 else if (argnum == 1)
7554 ret = "arg1";
7555 else if (argnum == 2)
7556 ret = "arg2";
7559 if (arg & WINED3DTA_ALPHAREPLICATE)
7561 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7562 if (argnum == 0)
7563 ret = "arg0";
7564 else if (argnum == 1)
7565 ret = "arg1";
7566 else if (argnum == 2)
7567 ret = "arg2";
7570 return ret;
7573 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7574 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7576 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7578 if (color && alpha)
7579 dstmask = "";
7580 else if (color)
7581 dstmask = ".xyz";
7582 else
7583 dstmask = ".w";
7585 if (dst == tempreg)
7586 dstreg = "temp_reg";
7587 else
7588 dstreg = "ret";
7590 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7591 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7592 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7594 switch (op)
7596 case WINED3D_TOP_DISABLE:
7597 break;
7599 case WINED3D_TOP_SELECT_ARG1:
7600 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7601 break;
7603 case WINED3D_TOP_SELECT_ARG2:
7604 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7605 break;
7607 case WINED3D_TOP_MODULATE:
7608 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7609 break;
7611 case WINED3D_TOP_MODULATE_4X:
7612 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7613 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7614 break;
7616 case WINED3D_TOP_MODULATE_2X:
7617 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7618 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7619 break;
7621 case WINED3D_TOP_ADD:
7622 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7623 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7624 break;
7626 case WINED3D_TOP_ADD_SIGNED:
7627 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7628 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7629 break;
7631 case WINED3D_TOP_ADD_SIGNED_2X:
7632 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7633 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7634 break;
7636 case WINED3D_TOP_SUBTRACT:
7637 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7638 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7639 break;
7641 case WINED3D_TOP_ADD_SMOOTH:
7642 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7643 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7644 break;
7646 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7647 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7648 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7649 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7650 break;
7652 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7653 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7654 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7655 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7656 break;
7658 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7659 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7660 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7661 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7662 break;
7664 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7665 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7666 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7667 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7668 break;
7670 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7671 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7672 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7673 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7674 break;
7676 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7677 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7678 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7679 break;
7681 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7682 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7683 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7684 break;
7686 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7687 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7688 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7689 break;
7690 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7691 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7692 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7693 break;
7695 case WINED3D_TOP_BUMPENVMAP:
7696 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7697 /* These are handled in the first pass, nothing to do. */
7698 break;
7700 case WINED3D_TOP_DOTPRODUCT3:
7701 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7702 dstreg, dstmask, arg1, arg2, dstmask);
7703 break;
7705 case WINED3D_TOP_MULTIPLY_ADD:
7706 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7707 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7708 break;
7710 case WINED3D_TOP_LERP:
7711 /* MSDN isn't quite right here. */
7712 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7713 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7714 break;
7716 default:
7717 FIXME("Unhandled operation %#x.\n", op);
7718 break;
7722 /* Context activation is done by the caller. */
7723 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7724 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7726 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7727 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7728 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7729 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7730 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7731 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7732 UINT lowest_disabled_stage;
7733 GLuint shader_id;
7734 DWORD arg0, arg1, arg2;
7735 unsigned int stage;
7737 string_buffer_clear(buffer);
7739 /* Find out which textures are read */
7740 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7742 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7743 break;
7745 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7746 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7747 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7749 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7750 || (stage == 0 && settings->color_key_enabled))
7751 tex_map |= 1u << stage;
7752 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7753 tfactor_used = TRUE;
7754 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7755 tempreg_used = TRUE;
7756 if (settings->op[stage].dst == tempreg)
7757 tempreg_used = TRUE;
7758 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7759 tss_const_map |= 1u << stage;
7761 switch (settings->op[stage].cop)
7763 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7764 lum_map |= 1u << stage;
7765 /* fall through */
7766 case WINED3D_TOP_BUMPENVMAP:
7767 bump_map |= 1u << stage;
7768 /* fall through */
7769 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7770 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7771 tex_map |= 1u << stage;
7772 break;
7774 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7775 tfactor_used = TRUE;
7776 break;
7778 default:
7779 break;
7782 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7783 continue;
7785 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7786 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7787 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7789 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7790 tex_map |= 1u << stage;
7791 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7792 tfactor_used = TRUE;
7793 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7794 tempreg_used = TRUE;
7795 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7796 tss_const_map |= 1u << stage;
7798 lowest_disabled_stage = stage;
7800 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7802 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7803 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7805 if (!needs_legacy_glsl_syntax(gl_info))
7806 shader_addline(buffer, "out vec4 ps_out[1];\n");
7808 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7809 shader_addline(buffer, "vec4 ret;\n");
7810 if (tempreg_used || settings->sRGB_write)
7811 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7812 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7814 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7816 if (tss_const_map & (1u << stage))
7817 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7819 if (!(tex_map & (1u << stage)))
7820 continue;
7822 switch (settings->op[stage].tex_type)
7824 case WINED3D_GL_RES_TYPE_TEX_1D:
7825 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7826 break;
7827 case WINED3D_GL_RES_TYPE_TEX_2D:
7828 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7829 break;
7830 case WINED3D_GL_RES_TYPE_TEX_3D:
7831 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7832 break;
7833 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7834 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7835 break;
7836 case WINED3D_GL_RES_TYPE_TEX_RECT:
7837 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7838 break;
7839 default:
7840 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7841 break;
7844 shader_addline(buffer, "vec4 tex%u;\n", stage);
7846 if (!(bump_map & (1u << stage)))
7847 continue;
7848 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7850 if (!(lum_map & (1u << stage)))
7851 continue;
7852 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7853 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7855 if (tfactor_used)
7856 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7857 if (settings->color_key_enabled)
7858 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7859 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7861 if (settings->sRGB_write)
7863 shader_addline(buffer, "const vec4 srgb_const0 = ");
7864 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7865 shader_addline(buffer, ";\n");
7866 shader_addline(buffer, "const vec4 srgb_const1 = ");
7867 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7868 shader_addline(buffer, ";\n");
7871 shader_addline(buffer, "uniform struct\n{\n");
7872 shader_addline(buffer, " vec4 color;\n");
7873 shader_addline(buffer, " float density;\n");
7874 shader_addline(buffer, " float end;\n");
7875 shader_addline(buffer, " float scale;\n");
7876 shader_addline(buffer, "} ffp_fog;\n");
7878 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7879 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7881 if (legacy_context)
7883 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7884 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7885 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7886 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7887 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7889 else
7891 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7892 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7893 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7894 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7895 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7898 shader_addline(buffer, "void main()\n{\n");
7900 if (legacy_context)
7902 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7903 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7906 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7908 if (tex_map & (1u << stage))
7910 if (settings->pointsprite)
7911 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7912 else if (settings->texcoords_initialized & (1u << stage))
7913 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7914 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7915 else
7916 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7920 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7921 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7923 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7924 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7926 /* Generate texture sampling instructions */
7927 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7929 const char *texture_function, *coord_mask;
7930 BOOL proj;
7932 if (!(tex_map & (1u << stage)))
7933 continue;
7935 if (settings->op[stage].projected == proj_none)
7937 proj = FALSE;
7939 else if (settings->op[stage].projected == proj_count4
7940 || settings->op[stage].projected == proj_count3)
7942 proj = TRUE;
7944 else
7946 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7947 proj = TRUE;
7950 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7951 proj = FALSE;
7953 switch (settings->op[stage].tex_type)
7955 case WINED3D_GL_RES_TYPE_TEX_1D:
7956 if (proj)
7958 texture_function = "texture1DProj";
7959 coord_mask = "xw";
7961 else
7963 texture_function = "texture1D";
7964 coord_mask = "x";
7966 break;
7967 case WINED3D_GL_RES_TYPE_TEX_2D:
7968 if (proj)
7970 texture_function = "texture2DProj";
7971 coord_mask = "xyw";
7973 else
7975 texture_function = "texture2D";
7976 coord_mask = "xy";
7978 break;
7979 case WINED3D_GL_RES_TYPE_TEX_3D:
7980 if (proj)
7982 texture_function = "texture3DProj";
7983 coord_mask = "xyzw";
7985 else
7987 texture_function = "texture3D";
7988 coord_mask = "xyz";
7990 break;
7991 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7992 texture_function = "textureCube";
7993 coord_mask = "xyz";
7994 break;
7995 case WINED3D_GL_RES_TYPE_TEX_RECT:
7996 if (proj)
7998 texture_function = "texture2DRectProj";
7999 coord_mask = "xyw";
8001 else
8003 texture_function = "texture2DRect";
8004 coord_mask = "xy";
8006 break;
8007 default:
8008 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
8009 texture_function = "";
8010 coord_mask = "xyzw";
8011 break;
8013 if (!needs_legacy_glsl_syntax(gl_info))
8014 texture_function = proj ? "textureProj" : "texture";
8016 if (stage > 0
8017 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
8018 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
8020 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
8022 /* With projective textures, texbem only divides the static
8023 * texture coord, not the displacement, so multiply the
8024 * displacement with the dividing parameter before passing it to
8025 * TXP. */
8026 if (settings->op[stage].projected != proj_none)
8028 if (settings->op[stage].projected == proj_count4)
8030 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8031 stage, stage);
8032 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
8034 else
8036 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8037 stage, stage);
8038 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
8041 else
8043 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
8046 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8047 stage, texture_function, stage, coord_mask);
8049 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8050 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8051 stage, stage - 1, stage - 1, stage - 1);
8053 else if (settings->op[stage].projected == proj_count3)
8055 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8056 stage, texture_function, stage, stage);
8058 else
8060 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8061 stage, texture_function, stage, stage, coord_mask);
8064 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
8065 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
8066 settings->op[stage].color_fixup);
8069 if (settings->color_key_enabled)
8071 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8072 shader_addline(buffer, " discard;\n");
8075 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
8077 /* Generate the main shader */
8078 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8080 BOOL op_equal;
8082 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8083 break;
8085 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8086 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8087 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8088 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8089 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8090 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8091 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8092 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8093 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8094 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8095 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8096 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8097 else
8098 op_equal = settings->op[stage].aop == settings->op[stage].cop
8099 && settings->op[stage].carg0 == settings->op[stage].aarg0
8100 && settings->op[stage].carg1 == settings->op[stage].aarg1
8101 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8103 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8105 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8106 settings->op[stage].cop, settings->op[stage].carg0,
8107 settings->op[stage].carg1, settings->op[stage].carg2);
8109 else if (op_equal)
8111 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8112 settings->op[stage].cop, settings->op[stage].carg0,
8113 settings->op[stage].carg1, settings->op[stage].carg2);
8115 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8116 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8118 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8119 settings->op[stage].cop, settings->op[stage].carg0,
8120 settings->op[stage].carg1, settings->op[stage].carg2);
8121 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8122 settings->op[stage].aop, settings->op[stage].aarg0,
8123 settings->op[stage].aarg1, settings->op[stage].aarg2);
8127 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8128 get_fragment_output(gl_info));
8130 if (settings->sRGB_write)
8131 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8133 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8135 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8137 shader_addline(buffer, "}\n");
8139 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8140 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8142 string_buffer_release(&priv->string_buffers, tex_reg_name);
8143 return shader_id;
8146 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8147 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8149 struct glsl_ffp_vertex_shader *shader;
8150 const struct wine_rb_entry *entry;
8152 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8153 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8155 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8156 return NULL;
8158 shader->desc.settings = *settings;
8159 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8160 list_init(&shader->linked_programs);
8161 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8162 ERR("Failed to insert ffp vertex shader.\n");
8164 return shader;
8167 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8168 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8170 struct glsl_ffp_fragment_shader *glsl_desc;
8171 const struct ffp_frag_desc *desc;
8173 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8174 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8176 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8177 return NULL;
8179 glsl_desc->entry.settings = *args;
8180 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8181 list_init(&glsl_desc->linked_programs);
8182 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8184 return glsl_desc;
8188 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8189 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8191 unsigned int i;
8192 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8194 for (i = 0; i < vs_c_count; ++i)
8196 string_buffer_sprintf(name, "vs_c[%u]", i);
8197 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8199 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8201 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8203 string_buffer_sprintf(name, "vs_i[%u]", i);
8204 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8207 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8209 string_buffer_sprintf(name, "vs_b[%u]", i);
8210 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8213 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8215 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8217 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8218 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8220 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8221 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8222 for (i = 0; i < MAX_TEXTURES; ++i)
8224 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8225 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8227 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8228 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8229 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8230 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8231 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8232 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8233 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8235 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8236 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8237 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8238 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8239 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8240 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8241 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8242 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8243 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8244 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8245 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8246 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8247 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8248 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8249 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8250 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8251 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8252 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8253 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8254 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8255 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8256 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8257 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8258 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8260 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8261 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8262 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8263 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8264 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8265 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8266 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8268 string_buffer_release(&priv->string_buffers, name);
8271 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8272 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8274 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8277 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8278 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8280 unsigned int i;
8281 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8283 for (i = 0; i < ps_c_count; ++i)
8285 string_buffer_sprintf(name, "ps_c[%u]", i);
8286 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8288 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8290 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8292 string_buffer_sprintf(name, "ps_i[%u]", i);
8293 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8296 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8298 string_buffer_sprintf(name, "ps_b[%u]", i);
8299 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8302 for (i = 0; i < MAX_TEXTURES; ++i)
8304 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8305 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8306 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8307 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8308 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8309 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8310 string_buffer_sprintf(name, "tss_const%u", i);
8311 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8314 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8315 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8317 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8318 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8319 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8320 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8322 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8324 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8325 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8326 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8328 string_buffer_release(&priv->string_buffers, name);
8331 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
8332 struct shader_glsl_priv *priv, GLuint program_id,
8333 const struct wined3d_shader_reg_maps *reg_maps)
8335 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8336 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
8337 unsigned int i, base, count;
8338 GLuint block_idx;
8340 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
8341 for (i = 0; i < count; ++i)
8343 if (!reg_maps->cb_sizes[i])
8344 continue;
8346 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
8347 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
8348 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8350 checkGLcall("glUniformBlockBinding");
8351 string_buffer_release(&priv->string_buffers, name);
8354 /* Context activation is done by the caller. */
8355 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8356 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8358 const struct wined3d_gl_info *gl_info = context->gl_info;
8359 unsigned int base_sampler_idx, sampler_count;
8360 struct glsl_shader_prog_link *entry = NULL;
8361 struct wined3d_shader *shader;
8362 struct glsl_program_key key;
8363 GLuint program_id, cs_id;
8365 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8366 return;
8368 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8370 WARN("Compute shader is NULL.\n");
8371 ctx_data->glsl_program = NULL;
8372 return;
8375 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8376 memset(&key, 0, sizeof(key));
8377 key.cs_id = cs_id;
8378 if ((entry = get_glsl_program_entry(priv, &key)))
8380 ctx_data->glsl_program = entry;
8381 return;
8384 program_id = GL_EXTCALL(glCreateProgram());
8385 TRACE("Created new GLSL shader program %u.\n", program_id);
8387 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8389 ERR("Out of memory.\n");
8390 return;
8392 entry->id = program_id;
8393 entry->vs.id = 0;
8394 entry->gs.id = 0;
8395 entry->ps.id = 0;
8396 entry->cs.id = cs_id;
8397 entry->constant_version = 0;
8398 entry->ps.np2_fixup_info = NULL;
8399 add_glsl_program_entry(priv, entry);
8401 ctx_data->glsl_program = entry;
8403 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8404 GL_EXTCALL(glAttachShader(program_id, cs_id));
8405 checkGLcall("glAttachShader");
8407 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8409 TRACE("Linking GLSL shader program %u.\n", program_id);
8410 GL_EXTCALL(glLinkProgram(program_id));
8411 shader_glsl_validate_link(gl_info, program_id);
8413 GL_EXTCALL(glUseProgram(program_id));
8414 checkGLcall("glUseProgram");
8415 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &shader->reg_maps);
8416 shader_glsl_load_icb(gl_info, priv, program_id, &shader->reg_maps);
8417 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8418 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_COMPUTE,
8419 &base_sampler_idx, &sampler_count);
8420 shader_glsl_load_samplers(gl_info, priv, shader_glsl_get_prefix(WINED3D_SHADER_TYPE_COMPUTE),
8421 base_sampler_idx, sampler_count, NULL, program_id);
8423 entry->constant_update_mask = 0;
8426 /* Context activation is done by the caller. */
8427 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8428 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8430 const struct wined3d_gl_info *gl_info = context->gl_info;
8431 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8432 const struct ps_np2fixup_info *np2fixup_info = NULL;
8433 struct glsl_shader_prog_link *entry = NULL;
8434 struct wined3d_shader *vshader = NULL;
8435 struct wined3d_shader *gshader = NULL;
8436 struct wined3d_shader *pshader = NULL;
8437 GLuint reorder_shader_id = 0;
8438 struct glsl_program_key key;
8439 GLuint program_id;
8440 unsigned int i;
8441 GLuint vs_id = 0;
8442 GLuint gs_id = 0;
8443 GLuint ps_id = 0;
8444 struct list *ps_list, *vs_list;
8445 WORD attribs_map;
8446 struct wined3d_string_buffer *tmp_name;
8448 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8450 vs_id = ctx_data->glsl_program->vs.id;
8451 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8453 if (use_vs(state))
8455 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8456 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8458 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8459 && ctx_data->glsl_program->gs.id)
8461 gs_id = ctx_data->glsl_program->gs.id;
8463 else if (gshader)
8465 struct gs_compile_args args;
8467 find_gs_compile_args(state, gshader, &args);
8468 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8472 else if (use_vs(state))
8474 struct vs_compile_args vs_compile_args;
8476 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8477 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8479 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8480 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8481 vs_list = &vshader->linked_programs;
8483 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8484 && ctx_data->glsl_program->gs.id)
8486 gs_id = ctx_data->glsl_program->gs.id;
8488 else if (gshader)
8490 struct gs_compile_args gs_compile_args;
8492 find_gs_compile_args(state, gshader, &gs_compile_args);
8493 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8496 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8498 struct glsl_ffp_vertex_shader *ffp_shader;
8499 struct wined3d_ffp_vs_settings settings;
8501 wined3d_ffp_get_vs_settings(context, state, &settings);
8502 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8503 vs_id = ffp_shader->id;
8504 vs_list = &ffp_shader->linked_programs;
8507 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8509 ps_id = ctx_data->glsl_program->ps.id;
8510 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8512 if (use_ps(state))
8513 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8515 else if (use_ps(state))
8517 struct ps_compile_args ps_compile_args;
8518 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8519 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8520 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8521 pshader, &ps_compile_args, &np2fixup_info);
8522 ps_list = &pshader->linked_programs;
8524 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8526 struct glsl_ffp_fragment_shader *ffp_shader;
8527 struct ffp_frag_settings settings;
8529 gen_ffp_frag_op(context, state, &settings, FALSE);
8530 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8531 ps_id = ffp_shader->id;
8532 ps_list = &ffp_shader->linked_programs;
8535 key.vs_id = vs_id;
8536 key.gs_id = gs_id;
8537 key.ps_id = ps_id;
8538 key.cs_id = 0;
8539 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8541 ctx_data->glsl_program = entry;
8542 return;
8545 /* If we get to this point, then no matching program exists, so we create one */
8546 program_id = GL_EXTCALL(glCreateProgram());
8547 TRACE("Created new GLSL shader program %u.\n", program_id);
8549 /* Create the entry */
8550 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8551 entry->id = program_id;
8552 entry->vs.id = vs_id;
8553 entry->gs.id = gs_id;
8554 entry->ps.id = ps_id;
8555 entry->cs.id = 0;
8556 entry->constant_version = 0;
8557 entry->ps.np2_fixup_info = np2fixup_info;
8558 /* Add the hash table entry */
8559 add_glsl_program_entry(priv, entry);
8561 /* Set the current program */
8562 ctx_data->glsl_program = entry;
8564 /* Attach GLSL vshader */
8565 if (vs_id)
8567 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8568 GL_EXTCALL(glAttachShader(program_id, vs_id));
8569 checkGLcall("glAttachShader");
8571 list_add_head(vs_list, &entry->vs.shader_entry);
8574 if (vshader)
8576 attribs_map = vshader->reg_maps.input_registers;
8577 if (vshader->reg_maps.shader_version.major < 4)
8579 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8580 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8581 d3d_info->emulated_flatshading
8582 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8583 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8584 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8585 checkGLcall("glAttachShader");
8586 /* Flag the reorder function for deletion, it will be freed
8587 * automatically when the program is destroyed. */
8588 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8591 else
8593 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8596 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8598 /* Bind vertex attributes to a corresponding index number to match
8599 * the same index numbers as ARB_vertex_programs (makes loading
8600 * vertex attributes simpler). With this method, we can use the
8601 * exact same code to load the attributes later for both ARB and
8602 * GLSL shaders.
8604 * We have to do this here because we need to know the Program ID
8605 * in order to make the bindings work, and it has to be done prior
8606 * to linking the GLSL program. */
8607 tmp_name = string_buffer_get(&priv->string_buffers);
8608 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8610 if (!(attribs_map & 1))
8611 continue;
8613 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8614 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8615 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8617 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8618 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8619 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8620 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8623 checkGLcall("glBindAttribLocation");
8624 string_buffer_release(&priv->string_buffers, tmp_name);
8627 if (gshader)
8629 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8630 GL_EXTCALL(glAttachShader(program_id, gs_id));
8631 checkGLcall("glAttachShader");
8633 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8635 TRACE("input type %s, output type %s, vertices out %u.\n",
8636 debug_d3dprimitivetype(gshader->u.gs.input_type),
8637 debug_d3dprimitivetype(gshader->u.gs.output_type),
8638 gshader->u.gs.vertices_out);
8639 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8640 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8641 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8642 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8643 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8644 gshader->u.gs.vertices_out));
8645 checkGLcall("glProgramParameteriARB");
8648 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8651 /* Attach GLSL pshader */
8652 if (ps_id)
8654 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8655 GL_EXTCALL(glAttachShader(program_id, ps_id));
8656 checkGLcall("glAttachShader");
8658 list_add_head(ps_list, &entry->ps.shader_entry);
8661 /* Link the program */
8662 TRACE("Linking GLSL shader program %u.\n", program_id);
8663 GL_EXTCALL(glLinkProgram(program_id));
8664 shader_glsl_validate_link(gl_info, program_id);
8666 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8667 vshader ? vshader->limits->constant_float : 0);
8668 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8669 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8670 pshader ? pshader->limits->constant_float : 0);
8671 checkGLcall("Find glsl program uniform locations");
8673 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8675 if (pshader && pshader->reg_maps.shader_version.major >= 3
8676 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8678 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8679 entry->vs.vertex_color_clamp = GL_FALSE;
8681 else
8683 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8686 else
8688 /* With core profile we never change vertex_color_clamp from
8689 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8690 * glClampColorARB(). */
8691 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8694 /* Set the shader to allow uniform loading on it */
8695 GL_EXTCALL(glUseProgram(program_id));
8696 checkGLcall("glUseProgram");
8698 /* Texture unit mapping is set up to be the same each time the shader
8699 * program is used so we can hardcode the sampler uniform values. */
8700 shader_glsl_load_graphics_samplers(gl_info, priv, context->tex_unit_map, program_id);
8702 entry->constant_update_mask = 0;
8703 if (vshader)
8705 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8706 if (vshader->reg_maps.integer_constants)
8707 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8708 if (vshader->reg_maps.boolean_constants)
8709 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8710 if (entry->vs.pos_fixup_location != -1)
8711 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8713 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8714 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8716 else
8718 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8719 | WINED3D_SHADER_CONST_FFP_PROJ;
8721 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8723 if (entry->vs.modelview_matrix_location[i] != -1)
8725 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8726 break;
8730 for (i = 0; i < MAX_TEXTURES; ++i)
8732 if (entry->vs.texture_matrix_location[i] != -1)
8734 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8735 break;
8738 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
8739 || entry->vs.material_specular_location != -1
8740 || entry->vs.material_emissive_location != -1
8741 || entry->vs.material_shininess_location != -1)
8742 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8743 if (entry->vs.light_ambient_location != -1)
8744 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8746 if (entry->vs.clip_planes_location != -1)
8747 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8748 if (entry->vs.pointsize_min_location != -1)
8749 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8751 if (gshader)
8753 if (entry->gs.pos_fixup_location != -1)
8754 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8755 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8756 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8759 if (ps_id)
8761 if (pshader)
8763 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8764 if (pshader->reg_maps.integer_constants)
8765 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8766 if (pshader->reg_maps.boolean_constants)
8767 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8768 if (entry->ps.ycorrection_location != -1)
8769 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8771 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8772 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8773 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
8775 else
8777 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8780 for (i = 0; i < MAX_TEXTURES; ++i)
8782 if (entry->ps.bumpenv_mat_location[i] != -1)
8784 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8785 break;
8789 if (entry->ps.fog_color_location != -1)
8790 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8791 if (entry->ps.alpha_test_ref_location != -1)
8792 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8793 if (entry->ps.np2_fixup_location != -1)
8794 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8795 if (entry->ps.color_key_location != -1)
8796 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8800 /* Context activation is done by the caller. */
8801 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8802 const struct wined3d_state *state)
8804 struct glsl_context_data *ctx_data = context->shader_backend_data;
8805 const struct wined3d_gl_info *gl_info = context->gl_info;
8806 struct shader_glsl_priv *priv = shader_priv;
8807 GLenum current_vertex_color_clamp;
8808 GLuint program_id, prev_id;
8810 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8811 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8813 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8815 set_glsl_shader_program(context, state, priv, ctx_data);
8817 if (ctx_data->glsl_program)
8819 program_id = ctx_data->glsl_program->id;
8820 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8822 else
8824 program_id = 0;
8825 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8828 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
8830 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
8831 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8833 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8834 checkGLcall("glClampColorARB");
8836 else
8838 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
8842 TRACE("Using GLSL program %u.\n", program_id);
8844 if (prev_id != program_id)
8846 GL_EXTCALL(glUseProgram(program_id));
8847 checkGLcall("glUseProgram");
8849 if (program_id)
8850 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8853 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
8856 /* Context activation is done by the caller. */
8857 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
8858 const struct wined3d_state *state)
8860 struct glsl_context_data *ctx_data = context->shader_backend_data;
8861 const struct wined3d_gl_info *gl_info = context->gl_info;
8862 struct shader_glsl_priv *priv = shader_priv;
8863 GLuint program_id, prev_id;
8865 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8866 set_glsl_compute_shader_program(context, state, priv, ctx_data);
8867 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8869 TRACE("Using GLSL program %u.\n", program_id);
8871 if (prev_id != program_id)
8873 GL_EXTCALL(glUseProgram(program_id));
8874 checkGLcall("glUseProgram");
8877 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
8878 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8879 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8880 | (1u << WINED3D_SHADER_TYPE_HULL)
8881 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
8884 /* "context" is not necessarily the currently active context. */
8885 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8887 struct glsl_context_data *ctx_data = context->shader_backend_data;
8889 ctx_data->glsl_program = NULL;
8890 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8891 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8892 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8893 | (1u << WINED3D_SHADER_TYPE_HULL)
8894 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8895 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8898 /* Context activation is done by the caller. */
8899 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8901 struct glsl_context_data *ctx_data = context->shader_backend_data;
8902 const struct wined3d_gl_info *gl_info = context->gl_info;
8903 struct shader_glsl_priv *priv = shader_priv;
8905 shader_glsl_invalidate_current_program(context);
8906 GL_EXTCALL(glUseProgram(0));
8907 checkGLcall("glUseProgram");
8909 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8910 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8912 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8914 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
8915 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8916 checkGLcall("glClampColorARB");
8920 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8921 const struct glsl_shader_prog_link *program)
8923 const struct glsl_context_data *ctx_data;
8924 struct wined3d_context *context;
8925 unsigned int i;
8927 for (i = 0; i < device->context_count; ++i)
8929 context = device->contexts[i];
8930 ctx_data = context->shader_backend_data;
8932 if (ctx_data->glsl_program == program)
8933 shader_glsl_invalidate_current_program(context);
8937 static void shader_glsl_destroy(struct wined3d_shader *shader)
8939 struct glsl_shader_private *shader_data = shader->backend_data;
8940 struct wined3d_device *device = shader->device;
8941 struct shader_glsl_priv *priv = device->shader_priv;
8942 const struct wined3d_gl_info *gl_info;
8943 const struct list *linked_programs;
8944 struct wined3d_context *context;
8946 if (!shader_data || !shader_data->num_gl_shaders)
8948 HeapFree(GetProcessHeap(), 0, shader_data);
8949 shader->backend_data = NULL;
8950 return;
8953 context = context_acquire(device, NULL, 0);
8954 gl_info = context->gl_info;
8956 TRACE("Deleting linked programs.\n");
8957 linked_programs = &shader->linked_programs;
8958 if (linked_programs->next)
8960 struct glsl_shader_prog_link *entry, *entry2;
8961 UINT i;
8963 switch (shader->reg_maps.shader_version.type)
8965 case WINED3D_SHADER_TYPE_PIXEL:
8967 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8969 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8971 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8972 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8973 checkGLcall("glDeleteShader");
8975 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8977 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8978 struct glsl_shader_prog_link, ps.shader_entry)
8980 shader_glsl_invalidate_contexts_program(device, entry);
8981 delete_glsl_program_entry(priv, gl_info, entry);
8984 break;
8987 case WINED3D_SHADER_TYPE_VERTEX:
8989 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8991 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8993 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8994 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8995 checkGLcall("glDeleteShader");
8997 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8999 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9000 struct glsl_shader_prog_link, vs.shader_entry)
9002 shader_glsl_invalidate_contexts_program(device, entry);
9003 delete_glsl_program_entry(priv, gl_info, entry);
9006 break;
9009 case WINED3D_SHADER_TYPE_GEOMETRY:
9011 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
9013 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9015 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
9016 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9017 checkGLcall("glDeleteShader");
9019 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
9021 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9022 struct glsl_shader_prog_link, gs.shader_entry)
9024 shader_glsl_invalidate_contexts_program(device, entry);
9025 delete_glsl_program_entry(priv, gl_info, entry);
9028 break;
9031 case WINED3D_SHADER_TYPE_COMPUTE:
9033 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
9035 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9037 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
9038 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9039 checkGLcall("glDeleteShader");
9041 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
9043 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9044 struct glsl_shader_prog_link, cs.shader_entry)
9046 shader_glsl_invalidate_contexts_program(device, entry);
9047 delete_glsl_program_entry(priv, gl_info, entry);
9050 break;
9053 default:
9054 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
9055 break;
9059 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9060 shader->backend_data = NULL;
9062 context_release(context);
9065 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
9067 const struct glsl_program_key *k = key;
9068 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
9069 const struct glsl_shader_prog_link, program_lookup_entry);
9071 if (k->vs_id > prog->vs.id) return 1;
9072 else if (k->vs_id < prog->vs.id) return -1;
9074 if (k->gs_id > prog->gs.id) return 1;
9075 else if (k->gs_id < prog->gs.id) return -1;
9077 if (k->ps_id > prog->ps.id) return 1;
9078 else if (k->ps_id < prog->ps.id) return -1;
9080 if (k->cs_id > prog->cs.id) return 1;
9081 else if (k->cs_id < prog->cs.id) return -1;
9083 return 0;
9086 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
9088 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
9089 + constant_count * sizeof(*heap->contained)
9090 + constant_count * sizeof(*heap->positions);
9091 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9093 if (!mem)
9095 ERR("Failed to allocate memory\n");
9096 return FALSE;
9099 heap->entries = mem;
9100 heap->entries[1].version = 0;
9101 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9102 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9103 heap->positions = (unsigned int *)(heap->contained + constant_count);
9104 heap->size = 1;
9106 return TRUE;
9109 static void constant_heap_free(struct constant_heap *heap)
9111 HeapFree(GetProcessHeap(), 0, heap->entries);
9114 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9115 const struct fragment_pipeline *fragment_pipe)
9117 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9118 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9119 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9120 struct fragment_caps fragment_caps;
9121 void *vertex_priv, *fragment_priv;
9123 string_buffer_list_init(&priv->string_buffers);
9125 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9127 ERR("Failed to initialize vertex pipe.\n");
9128 HeapFree(GetProcessHeap(), 0, priv);
9129 return E_FAIL;
9132 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9134 ERR("Failed to initialize fragment pipe.\n");
9135 vertex_pipe->vp_free(device);
9136 HeapFree(GetProcessHeap(), 0, priv);
9137 return E_FAIL;
9140 if (!string_buffer_init(&priv->shader_buffer))
9142 ERR("Failed to initialize shader buffer.\n");
9143 goto fail;
9146 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9148 ERR("Failed to allocate memory.\n");
9149 goto fail;
9152 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9154 ERR("Failed to initialize vertex shader constant heap\n");
9155 goto fail;
9158 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9160 ERR("Failed to initialize pixel shader constant heap\n");
9161 goto fail;
9164 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9166 priv->next_constant_version = 1;
9167 priv->vertex_pipe = vertex_pipe;
9168 priv->fragment_pipe = fragment_pipe;
9169 fragment_pipe->get_caps(gl_info, &fragment_caps);
9170 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9171 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9173 device->vertex_priv = vertex_priv;
9174 device->fragment_priv = fragment_priv;
9175 device->shader_priv = priv;
9177 return WINED3D_OK;
9179 fail:
9180 constant_heap_free(&priv->pconst_heap);
9181 constant_heap_free(&priv->vconst_heap);
9182 HeapFree(GetProcessHeap(), 0, priv->stack);
9183 string_buffer_free(&priv->shader_buffer);
9184 fragment_pipe->free_private(device);
9185 vertex_pipe->vp_free(device);
9186 HeapFree(GetProcessHeap(), 0, priv);
9187 return E_OUTOFMEMORY;
9190 /* Context activation is done by the caller. */
9191 static void shader_glsl_free(struct wined3d_device *device)
9193 struct shader_glsl_priv *priv = device->shader_priv;
9195 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9196 constant_heap_free(&priv->pconst_heap);
9197 constant_heap_free(&priv->vconst_heap);
9198 HeapFree(GetProcessHeap(), 0, priv->stack);
9199 string_buffer_list_cleanup(&priv->string_buffers);
9200 string_buffer_free(&priv->shader_buffer);
9201 priv->fragment_pipe->free_private(device);
9202 priv->vertex_pipe->vp_free(device);
9204 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9205 device->shader_priv = NULL;
9208 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9210 struct glsl_context_data *ctx_data;
9211 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9212 return FALSE;
9213 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9214 context->shader_backend_data = ctx_data;
9215 return TRUE;
9218 static void shader_glsl_free_context_data(struct wined3d_context *context)
9220 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9223 static void shader_glsl_init_context_state(struct wined3d_context *context)
9225 const struct wined3d_gl_info *gl_info = context->gl_info;
9227 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9228 checkGLcall("GL_PROGRAM_POINT_SIZE");
9231 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9233 UINT shader_model;
9235 /* FIXME: Check for the specific extensions required for SM5 support
9236 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9237 * soon as we introduce them, adjusting the GL / GLSL version checks
9238 * accordingly. */
9239 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9240 && gl_info->supported[ARB_COMPUTE_SHADER]
9241 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9242 && gl_info->supported[ARB_GPU_SHADER5]
9243 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9244 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9245 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9246 shader_model = 5;
9247 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9248 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9249 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9250 shader_model = 4;
9251 /* Support for texldd and texldl instructions in pixel shaders is required
9252 * for SM3. */
9253 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9254 shader_model = 3;
9255 else
9256 shader_model = 2;
9257 TRACE("Shader model %u.\n", shader_model);
9259 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9260 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9261 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9262 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9263 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9264 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9266 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9267 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9268 caps->varying_count = gl_info->limits.glsl_varyings;
9270 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9271 * Direct3D minimum requirement.
9273 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9274 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9276 * The problem is that the refrast clamps temporary results in the shader to
9277 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9278 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9279 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9280 * offer a way to query this.
9282 if (shader_model >= 4)
9283 caps->ps_1x_max_value = FLT_MAX;
9284 else
9285 caps->ps_1x_max_value = 1024.0f;
9287 /* Ideally we'd only set caps like sRGB writes here if supported by both
9288 * the shader backend and the fragment pipe, but we can get called before
9289 * shader_glsl_alloc(). */
9290 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9291 | WINED3D_SHADER_CAP_SRGB_WRITE;
9294 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9296 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9298 TRACE("Checking support for fixup:\n");
9299 dump_color_fixup_desc(fixup);
9302 /* We support everything except YUV conversions. */
9303 if (!is_complex_fixup(fixup))
9305 TRACE("[OK]\n");
9306 return TRUE;
9309 TRACE("[FAILED]\n");
9310 return FALSE;
9313 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9315 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9316 /* WINED3DSIH_ADD */ shader_glsl_binop,
9317 /* WINED3DSIH_AND */ shader_glsl_binop,
9318 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9319 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9320 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9321 /* WINED3DSIH_ATOMIC_IMAX */ NULL,
9322 /* WINED3DSIH_ATOMIC_IMIN */ NULL,
9323 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9324 /* WINED3DSIH_ATOMIC_UMAX */ NULL,
9325 /* WINED3DSIH_ATOMIC_UMIN */ NULL,
9326 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9327 /* WINED3DSIH_BEM */ shader_glsl_bem,
9328 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9329 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9330 /* WINED3DSIH_BREAK */ shader_glsl_break,
9331 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9332 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9333 /* WINED3DSIH_BUFINFO */ NULL,
9334 /* WINED3DSIH_CALL */ shader_glsl_call,
9335 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9336 /* WINED3DSIH_CASE */ shader_glsl_case,
9337 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9338 /* WINED3DSIH_CND */ shader_glsl_cnd,
9339 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9340 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9341 /* WINED3DSIH_CRS */ shader_glsl_cross,
9342 /* WINED3DSIH_CUT */ shader_glsl_cut,
9343 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9344 /* WINED3DSIH_DCL */ shader_glsl_nop,
9345 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9346 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9347 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9348 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9349 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9350 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9351 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9352 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9353 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9354 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9355 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9356 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9357 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9358 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9359 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9360 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9361 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9362 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9363 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9364 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9365 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9366 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9367 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
9368 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9369 /* WINED3DSIH_DCL_STREAM */ NULL,
9370 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9371 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9372 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9373 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9374 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
9375 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
9376 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9377 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9378 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
9379 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9380 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9381 /* WINED3DSIH_DEF */ shader_glsl_nop,
9382 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9383 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9384 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9385 /* WINED3DSIH_DIV */ shader_glsl_binop,
9386 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9387 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9388 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9389 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9390 /* WINED3DSIH_DST */ shader_glsl_dst,
9391 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9392 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9393 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9394 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9395 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9396 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9397 /* WINED3DSIH_ELSE */ shader_glsl_else,
9398 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9399 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9400 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9401 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9402 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9403 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9404 /* WINED3DSIH_EQ */ shader_glsl_relop,
9405 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9406 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9407 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9408 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9409 /* WINED3DSIH_FCALL */ NULL,
9410 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9411 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9412 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9413 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9414 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9415 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9416 /* WINED3DSIH_GATHER4 */ NULL,
9417 /* WINED3DSIH_GATHER4_C */ NULL,
9418 /* WINED3DSIH_GE */ shader_glsl_relop,
9419 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9420 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9421 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9422 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9423 /* WINED3DSIH_IADD */ shader_glsl_binop,
9424 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9425 /* WINED3DSIH_IF */ shader_glsl_if,
9426 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9427 /* WINED3DSIH_IGE */ shader_glsl_relop,
9428 /* WINED3DSIH_ILT */ shader_glsl_relop,
9429 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9430 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9431 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9432 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
9433 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9434 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9435 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
9436 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9437 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9438 /* WINED3DSIH_IMM_ATOMIC_IMAX */ NULL,
9439 /* WINED3DSIH_IMM_ATOMIC_IMIN */ NULL,
9440 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9441 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL,
9442 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL,
9443 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9444 /* WINED3DSIH_IMUL */ shader_glsl_imul,
9445 /* WINED3DSIH_INE */ shader_glsl_relop,
9446 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9447 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9448 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9449 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9450 /* WINED3DSIH_LABEL */ shader_glsl_label,
9451 /* WINED3DSIH_LD */ shader_glsl_ld,
9452 /* WINED3DSIH_LD2DMS */ NULL,
9453 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_raw,
9454 /* WINED3DSIH_LD_STRUCTURED */ NULL,
9455 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9456 /* WINED3DSIH_LIT */ shader_glsl_lit,
9457 /* WINED3DSIH_LOD */ NULL,
9458 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9459 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9460 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9461 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9462 /* WINED3DSIH_LT */ shader_glsl_relop,
9463 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9464 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9465 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9466 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9467 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9468 /* WINED3DSIH_MAD */ shader_glsl_mad,
9469 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9470 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9471 /* WINED3DSIH_MOV */ shader_glsl_mov,
9472 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9473 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9474 /* WINED3DSIH_MUL */ shader_glsl_binop,
9475 /* WINED3DSIH_NE */ shader_glsl_relop,
9476 /* WINED3DSIH_NOP */ shader_glsl_nop,
9477 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9478 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9479 /* WINED3DSIH_OR */ shader_glsl_binop,
9480 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9481 /* WINED3DSIH_POW */ shader_glsl_pow,
9482 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9483 /* WINED3DSIH_REP */ shader_glsl_rep,
9484 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9485 /* WINED3DSIH_RET */ shader_glsl_ret,
9486 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9487 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9488 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9489 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9490 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9491 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9492 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9493 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9494 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9495 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9496 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9497 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9498 /* WINED3DSIH_SAMPLE_POS */ NULL,
9499 /* WINED3DSIH_SETP */ NULL,
9500 /* WINED3DSIH_SGE */ shader_glsl_compare,
9501 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9502 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9503 /* WINED3DSIH_SLT */ shader_glsl_compare,
9504 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9505 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_buffer,
9506 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_buffer,
9507 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9508 /* WINED3DSIH_SUB */ shader_glsl_binop,
9509 /* WINED3DSIH_SWAPC */ NULL,
9510 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9511 /* WINED3DSIH_SYNC */ NULL,
9512 /* WINED3DSIH_TEX */ shader_glsl_tex,
9513 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9514 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9515 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9516 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9517 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9518 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9519 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9520 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9521 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9522 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9523 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9524 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9525 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9526 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9527 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9528 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9529 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9530 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9531 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9532 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9533 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9534 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9535 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9536 /* WINED3DSIH_UGE */ shader_glsl_relop,
9537 /* WINED3DSIH_ULT */ shader_glsl_relop,
9538 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9539 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9540 /* WINED3DSIH_USHR */ shader_glsl_binop,
9541 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9542 /* WINED3DSIH_XOR */ shader_glsl_binop,
9545 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9546 SHADER_HANDLER hw_fct;
9548 /* Select handler */
9549 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9551 /* Unhandled opcode */
9552 if (!hw_fct)
9554 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9555 return;
9557 hw_fct(ins);
9559 shader_glsl_add_instruction_modifiers(ins);
9562 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9564 struct shader_glsl_priv *priv = shader_priv;
9566 return priv->ffp_proj_control;
9569 const struct wined3d_shader_backend_ops glsl_shader_backend =
9571 shader_glsl_handle_instruction,
9572 shader_glsl_select,
9573 shader_glsl_select_compute,
9574 shader_glsl_disable,
9575 shader_glsl_update_float_vertex_constants,
9576 shader_glsl_update_float_pixel_constants,
9577 shader_glsl_load_constants,
9578 shader_glsl_destroy,
9579 shader_glsl_alloc,
9580 shader_glsl_free,
9581 shader_glsl_allocate_context_data,
9582 shader_glsl_free_context_data,
9583 shader_glsl_init_context_state,
9584 shader_glsl_get_caps,
9585 shader_glsl_color_fixup_supported,
9586 shader_glsl_has_ffp_proj_control,
9589 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9591 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9593 caps->xyzrhw = TRUE;
9594 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9595 caps->ffp_generic_attributes = TRUE;
9596 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9597 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9598 caps->max_vertex_blend_matrix_index = 0;
9599 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9600 | WINED3DVTXPCAPS_MATERIALSOURCE7
9601 | WINED3DVTXPCAPS_VERTEXFOG
9602 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9603 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9604 | WINED3DVTXPCAPS_LOCALVIEWER
9605 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9606 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9607 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9608 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9611 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9613 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9614 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9615 return 0;
9618 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9620 struct shader_glsl_priv *priv;
9622 if (shader_backend == &glsl_shader_backend)
9624 priv = shader_priv;
9625 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9626 return priv;
9629 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9631 return NULL;
9634 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9636 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9637 struct glsl_ffp_vertex_shader, desc.entry);
9638 struct glsl_shader_prog_link *program, *program2;
9639 struct glsl_ffp_destroy_ctx *ctx = context;
9641 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9642 struct glsl_shader_prog_link, vs.shader_entry)
9644 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9646 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9647 HeapFree(GetProcessHeap(), 0, shader);
9650 /* Context activation is done by the caller. */
9651 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9653 struct shader_glsl_priv *priv = device->vertex_priv;
9654 struct glsl_ffp_destroy_ctx ctx;
9656 ctx.priv = priv;
9657 ctx.gl_info = &device->adapter->gl_info;
9658 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9661 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9662 const struct wined3d_state *state, DWORD state_id) {}
9664 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9665 const struct wined3d_state *state, DWORD state_id)
9667 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9670 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9671 const struct wined3d_state *state, DWORD state_id)
9673 const struct wined3d_gl_info *gl_info = context->gl_info;
9674 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9675 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9676 BOOL transformed = context->stream_info.position_transformed;
9677 BOOL wasrhw = context->last_was_rhw;
9678 unsigned int i;
9680 context->last_was_rhw = transformed;
9682 /* If the vertex declaration contains a transformed position attribute,
9683 * the draw uses the fixed function vertex pipeline regardless of any
9684 * vertex shader set by the application. */
9685 if (transformed != wasrhw
9686 || context->stream_info.swizzle_map != context->last_swizzle_map)
9687 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9689 context->last_swizzle_map = context->stream_info.swizzle_map;
9691 if (!use_vs(state))
9693 if (context->last_was_vshader)
9695 if (legacy_context)
9696 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9697 clipplane(context, state, STATE_CLIPPLANE(i));
9698 else
9699 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9702 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9704 /* Because of settings->texcoords, we have to regenerate the vertex
9705 * shader on a vdecl change if there aren't enough varyings to just
9706 * always output all the texture coordinates. */
9707 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9708 || normal != context->last_was_normal)
9709 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9711 if (use_ps(state)
9712 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9713 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9714 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9716 else
9718 if (!context->last_was_vshader)
9720 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9721 if (legacy_context)
9722 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9723 clipplane(context, state, STATE_CLIPPLANE(i));
9724 else
9725 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9729 context->last_was_vshader = use_vs(state);
9730 context->last_was_normal = normal;
9733 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9734 const struct wined3d_state *state, DWORD state_id)
9736 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9737 /* Different vertex shaders potentially require a different vertex attributes setup. */
9738 if (!isStateDirty(context, STATE_VDECL))
9739 context_apply_state(context, state, STATE_VDECL);
9742 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9743 const struct wined3d_state *state, DWORD state_id)
9745 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9746 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9747 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9750 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9751 const struct wined3d_state *state, DWORD state_id)
9753 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9754 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9755 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9756 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9757 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9760 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9761 const struct wined3d_state *state, DWORD state_id)
9763 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9766 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9767 const struct wined3d_state *state, DWORD state_id)
9769 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9772 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9774 const struct wined3d_gl_info *gl_info = context->gl_info;
9775 unsigned int k;
9777 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9778 | WINED3D_SHADER_CONST_FFP_LIGHTS
9779 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9781 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9783 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9785 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9786 clipplane(context, state, STATE_CLIPPLANE(k));
9789 else
9791 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9795 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9796 const struct wined3d_state *state, DWORD state_id)
9798 /* Table fog behavior depends on the projection matrix. */
9799 if (state->render_states[WINED3D_RS_FOGENABLE]
9800 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9801 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9802 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9805 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9806 const struct wined3d_state *state, DWORD state_id)
9808 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9809 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9810 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9811 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9812 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9813 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9816 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9817 const struct wined3d_state *state, DWORD state_id)
9819 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9822 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9823 const struct wined3d_state *state, DWORD state_id)
9825 DWORD sampler = state_id - STATE_SAMPLER(0);
9826 const struct wined3d_texture *texture = state->textures[sampler];
9827 BOOL np2;
9829 if (!texture)
9830 return;
9832 if (sampler >= MAX_TEXTURES)
9833 return;
9835 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9836 || context->lastWasPow2Texture & (1u << sampler))
9838 if (np2)
9839 context->lastWasPow2Texture |= 1u << sampler;
9840 else
9841 context->lastWasPow2Texture &= ~(1u << sampler);
9843 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9847 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9848 const struct wined3d_state *state, DWORD state_id)
9850 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9853 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9854 const struct wined3d_state *state, DWORD state_id)
9856 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9859 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9860 const struct wined3d_state *state, DWORD state_id)
9862 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9865 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9866 const struct wined3d_state *state, DWORD state_id)
9868 if (!use_vs(state))
9869 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9872 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9873 const struct wined3d_state *state, DWORD state_id)
9875 static unsigned int once;
9877 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9878 FIXME("Non-point sprite points not supported in core profile.\n");
9881 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9882 const struct wined3d_state *state, DWORD state_id)
9884 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9887 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9888 const struct wined3d_state *state, DWORD state_id)
9890 const struct wined3d_gl_info *gl_info = context->gl_info;
9891 UINT index = state_id - STATE_CLIPPLANE(0);
9893 if (index >= gl_info->limits.user_clip_distances)
9894 return;
9896 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9899 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9901 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9902 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9903 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9904 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9905 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9906 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9907 /* Clip planes */
9908 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9909 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9910 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9911 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9912 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9913 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9914 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9915 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9916 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9917 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9918 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9919 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9920 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9921 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9922 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9923 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9924 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9925 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9926 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9927 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9928 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9929 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9930 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9931 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9932 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9933 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9934 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9935 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9936 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9937 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9938 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9939 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9940 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9941 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9942 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9943 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9944 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9945 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9946 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9947 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9948 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9949 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9950 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9951 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9952 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9953 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9954 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9955 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9956 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9957 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9958 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9959 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9960 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9961 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9962 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9963 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9964 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9965 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9966 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9967 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9968 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9969 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9970 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9971 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9972 /* Lights */
9973 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9974 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9975 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9976 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9977 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9978 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9979 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9980 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9981 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9982 /* Viewport */
9983 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9984 /* Transform states */
9985 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9986 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9987 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9988 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9989 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9990 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9991 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9992 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9993 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9994 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9995 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9996 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9997 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9998 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9999 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10000 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10001 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10002 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10003 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10004 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10005 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10006 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10007 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10008 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10009 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10010 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10011 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10012 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10013 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10014 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10015 /* Fog */
10016 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10017 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10018 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10019 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10020 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
10021 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
10022 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10023 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10024 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10025 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10026 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10027 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10028 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10029 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10030 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10031 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10032 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10033 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
10034 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
10035 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
10036 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
10037 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
10038 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10039 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10040 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10041 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10042 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10043 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10044 /* NP2 texture matrix fixups. They are not needed if
10045 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10046 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10047 * matrix. */
10048 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10049 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10050 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10051 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10052 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10053 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10054 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10055 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10056 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10057 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10058 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10059 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10060 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10061 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10062 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10063 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10064 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10065 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10066 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10067 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10068 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10069 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10070 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10071 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10072 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10073 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
10074 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
10075 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
10078 /* TODO:
10079 * - Implement vertex tweening. */
10080 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
10082 glsl_vertex_pipe_vp_enable,
10083 glsl_vertex_pipe_vp_get_caps,
10084 glsl_vertex_pipe_vp_get_emul_mask,
10085 glsl_vertex_pipe_vp_alloc,
10086 glsl_vertex_pipe_vp_free,
10087 glsl_vertex_pipe_vp_states,
10090 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10092 /* Nothing to do. */
10095 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10097 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10098 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10099 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10100 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10101 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10102 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10103 | WINED3DTEXOPCAPS_SELECTARG1
10104 | WINED3DTEXOPCAPS_SELECTARG2
10105 | WINED3DTEXOPCAPS_MODULATE4X
10106 | WINED3DTEXOPCAPS_MODULATE2X
10107 | WINED3DTEXOPCAPS_MODULATE
10108 | WINED3DTEXOPCAPS_ADDSIGNED2X
10109 | WINED3DTEXOPCAPS_ADDSIGNED
10110 | WINED3DTEXOPCAPS_ADD
10111 | WINED3DTEXOPCAPS_SUBTRACT
10112 | WINED3DTEXOPCAPS_ADDSMOOTH
10113 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10114 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10115 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10116 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10117 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10118 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10119 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10120 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10121 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10122 | WINED3DTEXOPCAPS_DOTPRODUCT3
10123 | WINED3DTEXOPCAPS_MULTIPLYADD
10124 | WINED3DTEXOPCAPS_LERP
10125 | WINED3DTEXOPCAPS_BUMPENVMAP
10126 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10127 caps->MaxTextureBlendStages = MAX_TEXTURES;
10128 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10131 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10133 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10134 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10135 return 0;
10138 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10140 struct shader_glsl_priv *priv;
10142 if (shader_backend == &glsl_shader_backend)
10144 priv = shader_priv;
10145 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10146 return priv;
10149 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10151 return NULL;
10154 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10156 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10157 struct glsl_ffp_fragment_shader, entry.entry);
10158 struct glsl_shader_prog_link *program, *program2;
10159 struct glsl_ffp_destroy_ctx *ctx = context;
10161 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10162 struct glsl_shader_prog_link, ps.shader_entry)
10164 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10166 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10167 HeapFree(GetProcessHeap(), 0, shader);
10170 /* Context activation is done by the caller. */
10171 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10173 struct shader_glsl_priv *priv = device->fragment_priv;
10174 struct glsl_ffp_destroy_ctx ctx;
10176 ctx.priv = priv;
10177 ctx.gl_info = &device->adapter->gl_info;
10178 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10181 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10182 const struct wined3d_state *state, DWORD state_id)
10184 context->last_was_pshader = use_ps(state);
10186 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10189 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10190 const struct wined3d_state *state, DWORD state_id)
10192 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10195 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10196 const struct wined3d_state *state, DWORD state_id)
10198 BOOL use_vshader = use_vs(state);
10199 enum fogsource new_source;
10200 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10201 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10203 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10205 if (!state->render_states[WINED3D_RS_FOGENABLE])
10206 return;
10208 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10210 if (use_vshader)
10211 new_source = FOGSOURCE_VS;
10212 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10213 new_source = FOGSOURCE_COORD;
10214 else
10215 new_source = FOGSOURCE_FFP;
10217 else
10219 new_source = FOGSOURCE_FFP;
10222 if (new_source != context->fog_source || fogstart == fogend)
10224 context->fog_source = new_source;
10225 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10229 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10230 const struct wined3d_state *state, DWORD state_id)
10232 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10233 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10234 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10236 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10237 glsl_fragment_pipe_fog(context, state, state_id);
10240 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10241 const struct wined3d_state *state, DWORD state_id)
10243 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10244 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10245 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10248 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10249 const struct wined3d_state *state, DWORD state_id)
10251 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10254 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10255 const struct wined3d_state *state, DWORD state_id)
10257 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10260 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10261 const struct wined3d_state *state, DWORD state_id)
10263 const struct wined3d_gl_info *gl_info = context->gl_info;
10264 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10265 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10267 if (func)
10269 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10270 checkGLcall("glAlphaFunc");
10274 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10275 const struct wined3d_state *state, DWORD state_id)
10277 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10280 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10281 const struct wined3d_state *state, DWORD state_id)
10283 const struct wined3d_gl_info *gl_info = context->gl_info;
10285 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10287 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10288 checkGLcall("glEnable(GL_ALPHA_TEST)");
10290 else
10292 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10293 checkGLcall("glDisable(GL_ALPHA_TEST)");
10297 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10298 const struct wined3d_state *state, DWORD state_id)
10300 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10303 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10304 const struct wined3d_state *state, DWORD state_id)
10306 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10309 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10310 const struct wined3d_state *state, DWORD state_id)
10312 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10315 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10317 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10318 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10319 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10320 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10321 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10322 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10323 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10324 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10325 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10326 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10327 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10328 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10329 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10330 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10331 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10332 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10333 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10334 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10335 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10336 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10337 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10338 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10339 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10340 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10341 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10342 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10343 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10344 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10345 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10346 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10347 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10348 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10349 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10350 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10351 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10352 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10353 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10354 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10355 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10356 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10357 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10358 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10359 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10360 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10361 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10362 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10363 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10364 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10365 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10366 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10367 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10368 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10369 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10370 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10371 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10372 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10373 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10374 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10375 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10376 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10377 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10378 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10379 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10380 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10381 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10382 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10383 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10384 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10385 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10386 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10387 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10388 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10389 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10390 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10391 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10392 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10393 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10394 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10395 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10396 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10397 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10398 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10399 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10400 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10401 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10402 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10403 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10404 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10405 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10406 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10407 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10408 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10409 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10410 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10411 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10412 {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 },
10413 {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 },
10414 {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 },
10415 {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 },
10416 {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 },
10417 {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 },
10418 {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 },
10419 {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 },
10420 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10421 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10422 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10423 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10424 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10425 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10426 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10427 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10428 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10429 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10430 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10431 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10432 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10435 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10437 return TRUE;
10440 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10444 const struct fragment_pipeline glsl_fragment_pipe =
10446 glsl_fragment_pipe_enable,
10447 glsl_fragment_pipe_get_caps,
10448 glsl_fragment_pipe_get_emul_mask,
10449 glsl_fragment_pipe_alloc,
10450 glsl_fragment_pipe_free,
10451 glsl_fragment_pipe_alloc_context_data,
10452 glsl_fragment_pipe_free_context_data,
10453 shader_glsl_color_fixup_supported,
10454 glsl_fragment_pipe_state_template,