comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / wined3d / glsl_shader.c
blobf8c4a99bffd8c7aa046b691f2d45f71abc2a8f46
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 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 struct wined3d_string_buffer *name;
68 DWORD coord_mask;
69 enum wined3d_data_type data_type;
70 BOOL output_single_component;
71 unsigned int offset_size;
74 enum heap_node_op
76 HEAP_NODE_TRAVERSE_LEFT,
77 HEAP_NODE_TRAVERSE_RIGHT,
78 HEAP_NODE_POP,
81 struct constant_entry
83 unsigned int idx;
84 unsigned int version;
87 struct constant_heap
89 struct constant_entry *entries;
90 BOOL *contained;
91 unsigned int *positions;
92 unsigned int size;
95 /* GLSL shader private data */
96 struct shader_glsl_priv {
97 struct wined3d_string_buffer shader_buffer;
98 struct wined3d_string_buffer_list string_buffers;
99 struct wine_rb_tree program_lookup;
100 struct constant_heap vconst_heap;
101 struct constant_heap pconst_heap;
102 unsigned char *stack;
103 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
104 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
105 UINT next_constant_version;
107 const struct wined3d_vertex_pipe_ops *vertex_pipe;
108 const struct fragment_pipeline *fragment_pipe;
109 struct wine_rb_tree ffp_vertex_shaders;
110 struct wine_rb_tree ffp_fragment_shaders;
111 BOOL ffp_proj_control;
112 BOOL legacy_lighting;
115 struct glsl_vs_program
117 struct list shader_entry;
118 GLuint id;
119 GLenum vertex_color_clamp;
120 GLint *uniform_f_locations;
121 GLint uniform_i_locations[MAX_CONST_I];
122 GLint uniform_b_locations[MAX_CONST_B];
123 GLint pos_fixup_location;
125 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
126 GLint projection_matrix_location;
127 GLint normal_matrix_location;
128 GLint texture_matrix_location[MAX_TEXTURES];
129 GLint material_ambient_location;
130 GLint material_diffuse_location;
131 GLint material_specular_location;
132 GLint material_emissive_location;
133 GLint material_shininess_location;
134 GLint light_ambient_location;
135 struct
137 GLint diffuse;
138 GLint specular;
139 GLint ambient;
140 GLint position;
141 GLint direction;
142 GLint range;
143 GLint falloff;
144 GLint c_att;
145 GLint l_att;
146 GLint q_att;
147 GLint cos_htheta;
148 GLint cos_hphi;
149 } light_location[MAX_ACTIVE_LIGHTS];
150 GLint pointsize_location;
151 GLint pointsize_min_location;
152 GLint pointsize_max_location;
153 GLint pointsize_c_att_location;
154 GLint pointsize_l_att_location;
155 GLint pointsize_q_att_location;
158 struct glsl_gs_program
160 struct list shader_entry;
161 GLuint id;
164 struct glsl_ps_program
166 struct list shader_entry;
167 GLuint id;
168 GLint *uniform_f_locations;
169 GLint uniform_i_locations[MAX_CONST_I];
170 GLint uniform_b_locations[MAX_CONST_B];
171 GLint bumpenv_mat_location[MAX_TEXTURES];
172 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
173 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
174 GLint tss_constant_location[MAX_TEXTURES];
175 GLint tex_factor_location;
176 GLint specular_enable_location;
177 GLint fog_color_location;
178 GLint fog_density_location;
179 GLint fog_end_location;
180 GLint fog_scale_location;
181 GLint ycorrection_location;
182 GLint np2_fixup_location;
183 GLint color_key_location;
184 const struct ps_np2fixup_info *np2_fixup_info;
187 /* Struct to maintain data about a linked GLSL program */
188 struct glsl_shader_prog_link
190 struct wine_rb_entry program_lookup_entry;
191 struct glsl_vs_program vs;
192 struct glsl_gs_program gs;
193 struct glsl_ps_program ps;
194 GLuint id;
195 DWORD constant_update_mask;
196 UINT constant_version;
199 struct glsl_program_key
201 GLuint vs_id;
202 GLuint gs_id;
203 GLuint ps_id;
206 struct shader_glsl_ctx_priv {
207 const struct vs_compile_args *cur_vs_args;
208 const struct ps_compile_args *cur_ps_args;
209 struct ps_np2fixup_info *cur_np2fixup_info;
210 struct wined3d_string_buffer_list *string_buffers;
213 struct glsl_context_data
215 struct glsl_shader_prog_link *glsl_program;
218 struct glsl_ps_compiled_shader
220 struct ps_compile_args args;
221 struct ps_np2fixup_info np2fixup;
222 GLuint id;
225 struct glsl_vs_compiled_shader
227 struct vs_compile_args args;
228 GLuint id;
231 struct glsl_gs_compiled_shader
233 GLuint id;
236 struct glsl_shader_private
238 union
240 struct glsl_vs_compiled_shader *vs;
241 struct glsl_gs_compiled_shader *gs;
242 struct glsl_ps_compiled_shader *ps;
243 } gl_shaders;
244 UINT num_gl_shaders, shader_array_size;
247 struct glsl_ffp_vertex_shader
249 struct wined3d_ffp_vs_desc desc;
250 GLuint id;
251 struct list linked_programs;
254 struct glsl_ffp_fragment_shader
256 struct ffp_frag_desc entry;
257 GLuint id;
258 struct list linked_programs;
261 struct glsl_ffp_destroy_ctx
263 struct shader_glsl_priv *priv;
264 const struct wined3d_gl_info *gl_info;
267 static const char *debug_gl_shader_type(GLenum type)
269 switch (type)
271 #define WINED3D_TO_STR(u) case u: return #u
272 WINED3D_TO_STR(GL_VERTEX_SHADER);
273 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
274 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
275 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
276 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
277 #undef WINED3D_TO_STR
278 default:
279 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
283 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
285 switch (type)
287 case WINED3D_SHADER_TYPE_VERTEX:
288 return "vs";
290 case WINED3D_SHADER_TYPE_HULL:
291 return "hs";
293 case WINED3D_SHADER_TYPE_DOMAIN:
294 return "ds";
296 case WINED3D_SHADER_TYPE_GEOMETRY:
297 return "gs";
299 case WINED3D_SHADER_TYPE_PIXEL:
300 return "ps";
302 default:
303 FIXME("Unhandled shader type %#x.\n", type);
304 return "unknown";
308 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
309 const struct wined3d_shader_version *version)
311 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
312 return "#version 150";
313 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
314 return "#version 130";
315 else
316 return "#version 120";
319 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
321 char str[4][17];
323 wined3d_ftoa(values[0], str[0]);
324 wined3d_ftoa(values[1], str[1]);
325 wined3d_ftoa(values[2], str[2]);
326 wined3d_ftoa(values[3], str[3]);
327 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
330 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
331 const int *values, unsigned int size)
333 int i;
335 if (!size || size > 4)
337 ERR("Invalid vector size %u.\n", size);
338 return;
341 if (size > 1)
342 shader_addline(buffer, "ivec%u(", size);
344 for (i = 0; i < size; ++i)
345 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
347 if (size > 1)
348 shader_addline(buffer, ")");
351 static const char *get_info_log_line(const char **ptr)
353 const char *p, *q;
355 p = *ptr;
356 if (!(q = strstr(p, "\n")))
358 if (!*p) return NULL;
359 *ptr += strlen(p);
360 return p;
362 *ptr = q + 1;
364 return p;
367 /* Context activation is done by the caller. */
368 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
370 int length = 0;
371 char *log;
373 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
374 return;
376 if (program)
377 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
378 else
379 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
381 /* A size of 1 is just a null-terminated string, so the log should be bigger than
382 * that if there are errors. */
383 if (length > 1)
385 const char *ptr, *line;
387 log = HeapAlloc(GetProcessHeap(), 0, length);
388 /* The info log is supposed to be zero-terminated, but at least some
389 * versions of fglrx don't terminate the string properly. The reported
390 * length does include the terminator, so explicitly set it to zero
391 * here. */
392 log[length - 1] = 0;
393 if (program)
394 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
395 else
396 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
398 ptr = log;
399 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
401 WARN("Info log received from GLSL shader #%u:\n", id);
402 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
404 else
406 FIXME("Info log received from GLSL shader #%u:\n", id);
407 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
409 HeapFree(GetProcessHeap(), 0, log);
413 /* Context activation is done by the caller. */
414 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
416 const char *ptr, *line;
418 TRACE("Compiling shader object %u.\n", shader);
420 if (TRACE_ON(d3d_shader))
422 ptr = src;
423 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
426 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
427 checkGLcall("glShaderSource");
428 GL_EXTCALL(glCompileShader(shader));
429 checkGLcall("glCompileShader");
430 print_glsl_info_log(gl_info, shader, FALSE);
433 /* Context activation is done by the caller. */
434 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
436 GLint i, shader_count, source_size = -1;
437 GLuint *shaders;
438 char *source = NULL;
440 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
441 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
442 if (!shaders)
444 ERR("Failed to allocate shader array memory.\n");
445 return;
448 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
449 for (i = 0; i < shader_count; ++i)
451 const char *ptr, *line;
452 GLint tmp;
454 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
456 if (source_size < tmp)
458 HeapFree(GetProcessHeap(), 0, source);
460 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
461 if (!source)
463 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
464 HeapFree(GetProcessHeap(), 0, shaders);
465 return;
467 source_size = tmp;
470 FIXME("Shader %u:\n", shaders[i]);
471 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
472 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
473 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
474 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
475 FIXME("\n");
477 ptr = source;
478 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
479 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
480 FIXME("\n");
483 HeapFree(GetProcessHeap(), 0, source);
484 HeapFree(GetProcessHeap(), 0, shaders);
487 /* Context activation is done by the caller. */
488 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
490 GLint tmp;
492 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
493 return;
495 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
496 if (!tmp)
498 FIXME("Program %u link status invalid.\n", program);
499 shader_glsl_dump_program_source(gl_info, program);
502 print_glsl_info_log(gl_info, program, TRUE);
505 /* Context activation is done by the caller. */
506 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
507 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
509 unsigned int mapped_unit;
510 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
511 const char *prefix;
512 unsigned int i, j;
513 GLint name_loc;
515 static const struct
517 enum wined3d_shader_type type;
518 unsigned int base_idx;
519 unsigned int count;
521 sampler_info[] =
523 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
524 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
527 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
529 prefix = shader_glsl_get_prefix(sampler_info[i].type);
531 for (j = 0; j < sampler_info[i].count; ++j)
533 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
534 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
535 if (name_loc == -1)
536 continue;
538 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
539 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
541 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
542 continue;
545 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
546 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
549 checkGLcall("glUniform1i");
550 string_buffer_release(&priv->string_buffers, sampler_name);
553 /* Context activation is done by the caller. */
554 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
555 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
557 unsigned int start = ~0U, end = 0;
558 int stack_idx = 0;
559 unsigned int heap_idx = 1;
560 unsigned int idx;
562 if (heap->entries[heap_idx].version <= version) return;
564 idx = heap->entries[heap_idx].idx;
565 if (constant_locations[idx] != -1)
566 start = end = idx;
567 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
569 while (stack_idx >= 0)
571 /* Note that we fall through to the next case statement. */
572 switch(stack[stack_idx])
574 case HEAP_NODE_TRAVERSE_LEFT:
576 unsigned int left_idx = heap_idx << 1;
577 if (left_idx < heap->size && heap->entries[left_idx].version > version)
579 heap_idx = left_idx;
580 idx = heap->entries[heap_idx].idx;
581 if (constant_locations[idx] != -1)
583 if (start > idx)
584 start = idx;
585 if (end < idx)
586 end = idx;
589 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
590 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
591 break;
595 case HEAP_NODE_TRAVERSE_RIGHT:
597 unsigned int right_idx = (heap_idx << 1) + 1;
598 if (right_idx < heap->size && heap->entries[right_idx].version > version)
600 heap_idx = right_idx;
601 idx = heap->entries[heap_idx].idx;
602 if (constant_locations[idx] != -1)
604 if (start > idx)
605 start = idx;
606 if (end < idx)
607 end = idx;
610 stack[stack_idx++] = HEAP_NODE_POP;
611 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
612 break;
616 case HEAP_NODE_POP:
617 heap_idx >>= 1;
618 --stack_idx;
619 break;
622 if (start <= end)
623 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
624 checkGLcall("walk_constant_heap()");
627 /* Context activation is done by the caller. */
628 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
629 GLint location, const struct wined3d_vec4 *data)
631 GLfloat clamped_constant[4];
633 if (location == -1) return;
635 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
636 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
637 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
638 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
640 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
643 /* Context activation is done by the caller. */
644 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
645 const struct wined3d_vec4 *constants, const GLint *constant_locations,
646 const struct constant_heap *heap, unsigned char *stack, DWORD version)
648 int stack_idx = 0;
649 unsigned int heap_idx = 1;
650 unsigned int idx;
652 if (heap->entries[heap_idx].version <= version) return;
654 idx = heap->entries[heap_idx].idx;
655 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
656 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
658 while (stack_idx >= 0)
660 /* Note that we fall through to the next case statement. */
661 switch(stack[stack_idx])
663 case HEAP_NODE_TRAVERSE_LEFT:
665 unsigned int left_idx = heap_idx << 1;
666 if (left_idx < heap->size && heap->entries[left_idx].version > version)
668 heap_idx = left_idx;
669 idx = heap->entries[heap_idx].idx;
670 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
672 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
673 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
674 break;
678 case HEAP_NODE_TRAVERSE_RIGHT:
680 unsigned int right_idx = (heap_idx << 1) + 1;
681 if (right_idx < heap->size && heap->entries[right_idx].version > version)
683 heap_idx = right_idx;
684 idx = heap->entries[heap_idx].idx;
685 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
687 stack[stack_idx++] = HEAP_NODE_POP;
688 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
689 break;
693 case HEAP_NODE_POP:
694 heap_idx >>= 1;
695 --stack_idx;
696 break;
699 checkGLcall("walk_constant_heap_clamped()");
702 /* Context activation is done by the caller. */
703 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
704 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
705 unsigned char *stack, unsigned int version)
707 const struct wined3d_shader_lconst *lconst;
709 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
710 if (shader->reg_maps.shader_version.major == 1
711 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
712 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
713 else
714 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
716 if (!shader->load_local_constsF)
718 TRACE("No need to load local float constants for this shader\n");
719 return;
722 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
723 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
725 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
727 checkGLcall("glUniform4fv()");
730 /* Context activation is done by the caller. */
731 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
732 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
734 unsigned int i;
735 struct list* ptr;
737 for (i = 0; constants_set; constants_set >>= 1, ++i)
739 if (!(constants_set & 1)) continue;
741 /* We found this uniform name in the program - go ahead and send the data */
742 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
745 /* Load immediate constants */
746 ptr = list_head(&shader->constantsI);
747 while (ptr)
749 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
750 unsigned int idx = lconst->idx;
751 const GLint *values = (const GLint *)lconst->value;
753 /* We found this uniform name in the program - go ahead and send the data */
754 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
755 ptr = list_next(&shader->constantsI, ptr);
757 checkGLcall("glUniform4iv()");
760 /* Context activation is done by the caller. */
761 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
762 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
764 unsigned int i;
765 struct list* ptr;
767 for (i = 0; constants_set; constants_set >>= 1, ++i)
769 if (!(constants_set & 1)) continue;
771 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
774 /* Load immediate constants */
775 ptr = list_head(&shader->constantsB);
776 while (ptr)
778 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
779 unsigned int idx = lconst->idx;
780 const GLint *values = (const GLint *)lconst->value;
782 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
783 ptr = list_next(&shader->constantsB, ptr);
785 checkGLcall("glUniform1iv()");
788 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
790 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
793 /* Context activation is done by the caller (state handler). */
794 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
795 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
797 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
798 UINT fixup = ps->np2_fixup_info->active;
799 UINT i;
801 for (i = 0; fixup; fixup >>= 1, ++i)
803 const struct wined3d_texture *tex = state->textures[i];
804 unsigned char idx = ps->np2_fixup_info->idx[i];
805 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
807 if (!tex)
809 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
810 continue;
813 if (idx % 2)
815 tex_dim[2] = tex->pow2_matrix[0];
816 tex_dim[3] = tex->pow2_matrix[5];
818 else
820 tex_dim[0] = tex->pow2_matrix[0];
821 tex_dim[1] = tex->pow2_matrix[5];
825 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
828 /* Taken and adapted from Mesa. */
829 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
831 float pos, neg, t, det;
832 struct wined3d_matrix temp;
834 /* Calculate the determinant of upper left 3x3 submatrix and
835 * determine if the matrix is singular. */
836 pos = neg = 0.0f;
837 t = in->_11 * in->_22 * in->_33;
838 if (t >= 0.0f)
839 pos += t;
840 else
841 neg += t;
843 t = in->_21 * in->_32 * in->_13;
844 if (t >= 0.0f)
845 pos += t;
846 else
847 neg += t;
848 t = in->_31 * in->_12 * in->_23;
849 if (t >= 0.0f)
850 pos += t;
851 else
852 neg += t;
854 t = -in->_31 * in->_22 * in->_13;
855 if (t >= 0.0f)
856 pos += t;
857 else
858 neg += t;
859 t = -in->_21 * in->_12 * in->_33;
860 if (t >= 0.0f)
861 pos += t;
862 else
863 neg += t;
865 t = -in->_11 * in->_32 * in->_23;
866 if (t >= 0.0f)
867 pos += t;
868 else
869 neg += t;
871 det = pos + neg;
873 if (fabsf(det) < 1e-25f)
874 return FALSE;
876 det = 1.0f / det;
877 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
878 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
879 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
880 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
881 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
882 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
883 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
884 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
885 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
887 *out = temp;
888 return TRUE;
891 static void swap_rows(float **a, float **b)
893 float *tmp = *a;
895 *a = *b;
896 *b = tmp;
899 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
901 float wtmp[4][8];
902 float m0, m1, m2, m3, s;
903 float *r0, *r1, *r2, *r3;
905 r0 = wtmp[0];
906 r1 = wtmp[1];
907 r2 = wtmp[2];
908 r3 = wtmp[3];
910 r0[0] = m->_11;
911 r0[1] = m->_12;
912 r0[2] = m->_13;
913 r0[3] = m->_14;
914 r0[4] = 1.0f;
915 r0[5] = r0[6] = r0[7] = 0.0f;
917 r1[0] = m->_21;
918 r1[1] = m->_22;
919 r1[2] = m->_23;
920 r1[3] = m->_24;
921 r1[5] = 1.0f;
922 r1[4] = r1[6] = r1[7] = 0.0f;
924 r2[0] = m->_31;
925 r2[1] = m->_32;
926 r2[2] = m->_33;
927 r2[3] = m->_34;
928 r2[6] = 1.0f;
929 r2[4] = r2[5] = r2[7] = 0.0f;
931 r3[0] = m->_41;
932 r3[1] = m->_42;
933 r3[2] = m->_43;
934 r3[3] = m->_44;
935 r3[7] = 1.0f;
936 r3[4] = r3[5] = r3[6] = 0.0f;
938 /* Choose pivot - or die. */
939 if (fabsf(r3[0]) > fabsf(r2[0]))
940 swap_rows(&r3, &r2);
941 if (fabsf(r2[0]) > fabsf(r1[0]))
942 swap_rows(&r2, &r1);
943 if (fabsf(r1[0]) > fabsf(r0[0]))
944 swap_rows(&r1, &r0);
945 if (r0[0] == 0.0f)
946 return FALSE;
948 /* Eliminate first variable. */
949 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
950 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
951 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
952 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
953 s = r0[4];
954 if (s != 0.0f)
956 r1[4] -= m1 * s;
957 r2[4] -= m2 * s;
958 r3[4] -= m3 * s;
960 s = r0[5];
961 if (s != 0.0f)
963 r1[5] -= m1 * s;
964 r2[5] -= m2 * s;
965 r3[5] -= m3 * s;
967 s = r0[6];
968 if (s != 0.0f)
970 r1[6] -= m1 * s;
971 r2[6] -= m2 * s;
972 r3[6] -= m3 * s;
974 s = r0[7];
975 if (s != 0.0f)
977 r1[7] -= m1 * s;
978 r2[7] -= m2 * s;
979 r3[7] -= m3 * s;
982 /* Choose pivot - or die. */
983 if (fabsf(r3[1]) > fabsf(r2[1]))
984 swap_rows(&r3, &r2);
985 if (fabsf(r2[1]) > fabsf(r1[1]))
986 swap_rows(&r2, &r1);
987 if (r1[1] == 0.0f)
988 return FALSE;
990 /* Eliminate second variable. */
991 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
992 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
993 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
994 s = r1[4];
995 if (s != 0.0f)
997 r2[4] -= m2 * s;
998 r3[4] -= m3 * s;
1000 s = r1[5];
1001 if (s != 0.0f)
1003 r2[5] -= m2 * s;
1004 r3[5] -= m3 * s;
1006 s = r1[6];
1007 if (s != 0.0f)
1009 r2[6] -= m2 * s;
1010 r3[6] -= m3 * s;
1012 s = r1[7];
1013 if (s != 0.0f)
1015 r2[7] -= m2 * s;
1016 r3[7] -= m3 * s;
1019 /* Choose pivot - or die. */
1020 if (fabsf(r3[2]) > fabsf(r2[2]))
1021 swap_rows(&r3, &r2);
1022 if (r2[2] == 0.0f)
1023 return FALSE;
1025 /* Eliminate third variable. */
1026 m3 = r3[2] / r2[2];
1027 r3[3] -= m3 * r2[3];
1028 r3[4] -= m3 * r2[4];
1029 r3[5] -= m3 * r2[5];
1030 r3[6] -= m3 * r2[6];
1031 r3[7] -= m3 * r2[7];
1033 /* Last check. */
1034 if (r3[3] == 0.0f)
1035 return FALSE;
1037 /* Back substitute row 3. */
1038 s = 1.0f / r3[3];
1039 r3[4] *= s;
1040 r3[5] *= s;
1041 r3[6] *= s;
1042 r3[7] *= s;
1044 /* Back substitute row 2. */
1045 m2 = r2[3];
1046 s = 1.0f / r2[2];
1047 r2[4] = s * (r2[4] - r3[4] * m2);
1048 r2[5] = s * (r2[5] - r3[5] * m2);
1049 r2[6] = s * (r2[6] - r3[6] * m2);
1050 r2[7] = s * (r2[7] - r3[7] * m2);
1051 m1 = r1[3];
1052 r1[4] -= r3[4] * m1;
1053 r1[5] -= r3[5] * m1;
1054 r1[6] -= r3[6] * m1;
1055 r1[7] -= r3[7] * m1;
1056 m0 = r0[3];
1057 r0[4] -= r3[4] * m0;
1058 r0[5] -= r3[5] * m0;
1059 r0[6] -= r3[6] * m0;
1060 r0[7] -= r3[7] * m0;
1062 /* Back substitute row 1. */
1063 m1 = r1[2];
1064 s = 1.0f / r1[1];
1065 r1[4] = s * (r1[4] - r2[4] * m1);
1066 r1[5] = s * (r1[5] - r2[5] * m1);
1067 r1[6] = s * (r1[6] - r2[6] * m1);
1068 r1[7] = s * (r1[7] - r2[7] * m1);
1069 m0 = r0[2];
1070 r0[4] -= r2[4] * m0;
1071 r0[5] -= r2[5] * m0;
1072 r0[6] -= r2[6] * m0;
1073 r0[7] -= r2[7] * m0;
1075 /* Back substitute row 0. */
1076 m0 = r0[1];
1077 s = 1.0f / r0[0];
1078 r0[4] = s * (r0[4] - r1[4] * m0);
1079 r0[5] = s * (r0[5] - r1[5] * m0);
1080 r0[6] = s * (r0[6] - r1[6] * m0);
1081 r0[7] = s * (r0[7] - r1[7] * m0);
1083 out->_11 = r0[4];
1084 out->_12 = r0[5];
1085 out->_13 = r0[6];
1086 out->_14 = r0[7];
1087 out->_21 = r1[4];
1088 out->_22 = r1[5];
1089 out->_23 = r1[6];
1090 out->_24 = r1[7];
1091 out->_31 = r2[4];
1092 out->_32 = r2[5];
1093 out->_33 = r2[6];
1094 out->_34 = r2[7];
1095 out->_41 = r3[4];
1096 out->_42 = r3[5];
1097 out->_43 = r3[6];
1098 out->_44 = r3[7];
1100 return TRUE;
1103 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1104 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1106 const struct wined3d_gl_info *gl_info = context->gl_info;
1107 float mat[3 * 3];
1108 struct wined3d_matrix mv;
1109 unsigned int i, j;
1111 if (prog->vs.normal_matrix_location == -1)
1112 return;
1114 get_modelview_matrix(context, state, 0, &mv);
1115 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1116 invert_matrix_3d(&mv, &mv);
1117 else
1118 invert_matrix(&mv, &mv);
1119 /* Tests show that singular modelview matrices are used unchanged as normal
1120 * matrices on D3D3 and older. There seems to be no clearly consistent
1121 * behavior on newer D3D versions so always follow older ddraw behavior. */
1122 for (i = 0; i < 3; ++i)
1123 for (j = 0; j < 3; ++j)
1124 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1126 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1127 checkGLcall("glUniformMatrix3fv");
1130 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1131 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1133 const struct wined3d_gl_info *gl_info = context->gl_info;
1134 struct wined3d_matrix mat;
1136 if (tex >= MAX_TEXTURES)
1137 return;
1138 if (prog->vs.texture_matrix_location[tex] == -1)
1139 return;
1141 get_texture_matrix(context, state, tex, &mat);
1142 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1143 checkGLcall("glUniformMatrix4fv");
1146 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1147 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1149 const struct wined3d_gl_info *gl_info = context->gl_info;
1151 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1153 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1154 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1156 else
1158 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1160 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1162 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1163 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1164 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1165 checkGLcall("setting FFP material uniforms");
1168 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1169 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1171 const struct wined3d_gl_info *gl_info = context->gl_info;
1172 struct wined3d_color color;
1174 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1175 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1176 checkGLcall("glUniform3fv");
1179 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1180 const struct wined3d_matrix *src2)
1182 struct wined3d_vec4 temp;
1184 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1185 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1186 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1187 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1189 *dest = temp;
1192 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1193 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1195 const struct wined3d_gl_info *gl_info = context->gl_info;
1196 const struct wined3d_light_info *light_info = state->lights[light];
1197 struct wined3d_vec4 vec4;
1198 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1200 if (!light_info)
1201 return;
1203 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1204 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1205 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1207 switch (light_info->OriginalParms.type)
1209 case WINED3D_LIGHT_POINT:
1210 multiply_vector_matrix(&vec4, &light_info->position, view);
1211 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1212 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1213 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1214 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1215 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1216 break;
1218 case WINED3D_LIGHT_SPOT:
1219 multiply_vector_matrix(&vec4, &light_info->position, view);
1220 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1222 multiply_vector_matrix(&vec4, &light_info->direction, view);
1223 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1225 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1226 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1227 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1228 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1229 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1230 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1231 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1232 break;
1234 case WINED3D_LIGHT_DIRECTIONAL:
1235 multiply_vector_matrix(&vec4, &light_info->direction, view);
1236 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1237 break;
1239 case WINED3D_LIGHT_PARALLELPOINT:
1240 multiply_vector_matrix(&vec4, &light_info->position, view);
1241 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1242 break;
1244 default:
1245 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1247 checkGLcall("setting FFP lights uniforms");
1250 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1251 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1253 const struct wined3d_gl_info *gl_info = context->gl_info;
1254 float min, max;
1255 float size, att[3];
1257 get_pointsize_minmax(context, state, &min, &max);
1259 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1260 checkGLcall("glUniform1f");
1261 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1262 checkGLcall("glUniform1f");
1264 get_pointsize(context, state, &size, att);
1266 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1267 checkGLcall("glUniform1f");
1268 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1269 checkGLcall("glUniform1f");
1270 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1271 checkGLcall("glUniform1f");
1272 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1273 checkGLcall("glUniform1f");
1276 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1277 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1279 const struct wined3d_gl_info *gl_info = context->gl_info;
1280 struct wined3d_color color;
1281 float start, end, scale;
1282 union
1284 DWORD d;
1285 float f;
1286 } tmpvalue;
1288 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1289 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1290 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1291 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1292 get_fog_start_end(context, state, &start, &end);
1293 scale = 1.0f / (end - start);
1294 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1295 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1296 checkGLcall("fog emulation uniforms");
1299 /* Context activation is done by the caller (state handler). */
1300 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1301 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1303 struct wined3d_color float_key[2];
1304 const struct wined3d_texture *texture = state->textures[0];
1306 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1307 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1310 /* Context activation is done by the caller (state handler). */
1311 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1312 const struct wined3d_state *state)
1314 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1315 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1316 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1317 const struct wined3d_gl_info *gl_info = context->gl_info;
1318 struct shader_glsl_priv *priv = shader_priv;
1319 float position_fixup[4];
1320 DWORD update_mask;
1322 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1323 UINT constant_version;
1324 int i;
1326 if (!prog) {
1327 /* No GLSL program set - nothing to do. */
1328 return;
1330 constant_version = prog->constant_version;
1331 update_mask = context->constant_update_mask & prog->constant_update_mask;
1333 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1334 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1335 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1337 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1338 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1339 vshader->reg_maps.integer_constants);
1341 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1342 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1343 vshader->reg_maps.boolean_constants);
1345 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1346 shader_glsl_pointsize_uniform(context, state, prog);
1348 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1350 shader_get_position_fixup(context, state, position_fixup);
1351 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1352 checkGLcall("glUniform4fv");
1355 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1357 struct wined3d_matrix mat;
1359 get_modelview_matrix(context, state, 0, &mat);
1360 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1361 checkGLcall("glUniformMatrix4fv");
1363 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1366 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1368 struct wined3d_matrix mat;
1370 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1372 if (prog->vs.modelview_matrix_location[i] == -1)
1373 break;
1375 get_modelview_matrix(context, state, i, &mat);
1376 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1377 checkGLcall("glUniformMatrix4fv");
1381 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1383 struct wined3d_matrix projection;
1385 get_projection_matrix(context, state, &projection);
1386 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1387 checkGLcall("glUniformMatrix4fv");
1390 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1392 for (i = 0; i < MAX_TEXTURES; ++i)
1393 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1396 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1397 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1399 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1401 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1402 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1403 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1406 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1407 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1408 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1410 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1411 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1412 pshader->reg_maps.integer_constants);
1414 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1415 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1416 pshader->reg_maps.boolean_constants);
1418 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1420 for (i = 0; i < MAX_TEXTURES; ++i)
1422 if (prog->ps.bumpenv_mat_location[i] == -1)
1423 continue;
1425 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1426 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1428 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1430 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1431 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1432 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1433 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1437 checkGLcall("bump env uniforms");
1440 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1442 const struct wined3d_vec4 correction_params =
1444 /* Position is relative to the framebuffer, not the viewport. */
1445 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1446 context->render_offscreen ? 1.0f : -1.0f,
1447 0.0f,
1448 0.0f,
1451 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1454 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1455 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1456 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1457 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1459 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1461 struct wined3d_color color;
1463 if (prog->ps.tex_factor_location != -1)
1465 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1466 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1469 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1470 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1471 else
1472 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1474 for (i = 0; i < MAX_TEXTURES; ++i)
1476 if (prog->ps.tss_constant_location[i] == -1)
1477 continue;
1479 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1480 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1483 checkGLcall("fixed function uniforms");
1486 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1487 shader_glsl_load_fog_uniform(context, state, prog);
1489 if (priv->next_constant_version == UINT_MAX)
1491 TRACE("Max constant version reached, resetting to 0.\n");
1492 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1493 priv->next_constant_version = 1;
1495 else
1497 prog->constant_version = priv->next_constant_version++;
1501 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1503 struct constant_entry *entries = heap->entries;
1504 unsigned int *positions = heap->positions;
1505 unsigned int heap_idx, parent_idx;
1507 if (!heap->contained[idx])
1509 heap_idx = heap->size++;
1510 heap->contained[idx] = TRUE;
1512 else
1514 heap_idx = positions[idx];
1517 while (heap_idx > 1)
1519 parent_idx = heap_idx >> 1;
1521 if (new_version <= entries[parent_idx].version) break;
1523 entries[heap_idx] = entries[parent_idx];
1524 positions[entries[parent_idx].idx] = heap_idx;
1525 heap_idx = parent_idx;
1528 entries[heap_idx].version = new_version;
1529 entries[heap_idx].idx = idx;
1530 positions[idx] = heap_idx;
1533 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1535 struct shader_glsl_priv *priv = device->shader_priv;
1536 struct constant_heap *heap = &priv->vconst_heap;
1537 UINT i;
1539 for (i = start; i < count + start; ++i)
1541 update_heap_entry(heap, i, priv->next_constant_version);
1544 for (i = 0; i < device->context_count; ++i)
1546 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1550 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1552 struct shader_glsl_priv *priv = device->shader_priv;
1553 struct constant_heap *heap = &priv->pconst_heap;
1554 UINT i;
1556 for (i = start; i < count + start; ++i)
1558 update_heap_entry(heap, i, priv->next_constant_version);
1561 for (i = 0; i < device->context_count; ++i)
1563 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1567 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1569 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1570 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1571 if(shader_major > 3) return ret;
1573 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1574 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1575 return ret;
1578 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1580 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1583 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1585 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1588 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1589 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1591 va_list args;
1592 int ret;
1594 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1595 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1596 for (;;)
1598 va_start(args, format);
1599 ret = shader_vaddline(buffer, format, args);
1600 va_end(args);
1601 if (!ret)
1602 return;
1603 if (!string_buffer_resize(buffer, ret))
1604 return;
1608 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1609 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1611 va_list args;
1612 int ret;
1614 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1615 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1616 for (;;)
1618 va_start(args, format);
1619 ret = shader_vaddline(buffer, format, args);
1620 va_end(args);
1621 if (!ret)
1622 return;
1623 if (!string_buffer_resize(buffer, ret))
1624 return;
1628 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1630 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1633 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1635 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1636 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1637 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1638 unsigned int i;
1640 if (reg_maps->shader_version.major < 3)
1641 return input_reg_used[idx];
1643 for (i = 0; i < input_signature->element_count; ++i)
1645 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1647 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1648 continue;
1650 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1651 && input->semantic_idx == idx)
1653 if (input_reg_used[input->register_idx])
1654 return TRUE;
1655 else
1656 return FALSE;
1659 return FALSE;
1662 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1663 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1665 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1667 if (version->major >= 4)
1668 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1669 else
1670 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1673 /** Generate the variable & register declarations for the GLSL output target */
1674 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1675 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1676 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1678 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1679 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1680 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1681 const struct wined3d_gl_info *gl_info = context->gl_info;
1682 unsigned int i, extra_constants_needed = 0;
1683 const struct wined3d_shader_lconst *lconst;
1684 const char *prefix;
1685 DWORD map;
1687 prefix = shader_glsl_get_prefix(version->type);
1689 /* Prototype the subroutines */
1690 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1692 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1695 /* Declare the constants (aka uniforms) */
1696 if (shader->limits->constant_float > 0)
1698 unsigned max_constantsF;
1700 /* Unless the shader uses indirect addressing, always declare the
1701 * maximum array size and ignore that we need some uniforms privately.
1702 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1703 * and immediate values, still declare VC[256]. If the shader needs
1704 * more uniforms than we have it won't work in any case. If it uses
1705 * less, the compiler will figure out which uniforms are really used
1706 * and strip them out. This allows a shader to use c255 on a dx9 card,
1707 * as long as it doesn't also use all the other constants.
1709 * If the shader uses indirect addressing the compiler must assume
1710 * that all declared uniforms are used. In this case, declare only the
1711 * amount that we're assured to have.
1713 * Thus we run into problems in these two cases:
1714 * 1) The shader really uses more uniforms than supported.
1715 * 2) The shader uses indirect addressing, less constants than
1716 * supported, but uses a constant index > #supported consts. */
1717 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1719 /* No indirect addressing here. */
1720 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1722 else
1724 if (reg_maps->usesrelconstF)
1726 /* Subtract the other potential uniforms from the max
1727 * available (bools, ints, and 1 row of projection matrix).
1728 * Subtract another uniform for immediate values, which have
1729 * to be loaded via uniform by the driver as well. The shader
1730 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1731 * shader code, so one vec4 should be enough. (Unfortunately
1732 * the Nvidia driver doesn't store 128 and -128 in one float).
1734 * Writing gl_ClipVertex requires one uniform for each
1735 * clipplane as well. */
1736 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1737 if (vs_args->clip_enabled)
1738 max_constantsF -= gl_info->limits.clipplanes;
1739 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1740 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1741 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1742 * for now take this into account when calculating the number of available constants
1744 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
1745 /* Set by driver quirks in directx.c */
1746 max_constantsF -= gl_info->reserved_glsl_constants;
1748 if (max_constantsF < shader->limits->constant_float)
1750 static unsigned int once;
1752 if (!once++)
1753 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1754 " it may not render correctly.\n");
1755 else
1756 WARN("The hardware does not support enough uniform components to run this shader.\n");
1759 else
1761 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1764 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1765 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1768 /* Always declare the full set of constants, the compiler can remove the
1769 * unused ones because d3d doesn't (yet) support indirect int and bool
1770 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1771 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1772 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1774 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1775 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1777 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1779 if (reg_maps->cb_sizes[i])
1780 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1781 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1784 /* Declare texture samplers */
1785 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1787 struct wined3d_shader_sampler_map_entry *entry;
1788 const char *sampler_type_prefix, *sampler_type;
1789 BOOL shadow_sampler, tex_rect;
1791 entry = &reg_maps->sampler_map.entries[i];
1793 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1795 ERR("Invalid resource index %u.\n", entry->resource_idx);
1796 continue;
1799 switch (reg_maps->resource_info[entry->resource_idx].data_type)
1801 case WINED3D_DATA_FLOAT:
1802 case WINED3D_DATA_UNORM:
1803 case WINED3D_DATA_SNORM:
1804 sampler_type_prefix = "";
1805 break;
1807 case WINED3D_DATA_INT:
1808 sampler_type_prefix = "i";
1809 break;
1811 case WINED3D_DATA_UINT:
1812 sampler_type_prefix = "u";
1813 break;
1815 default:
1816 sampler_type_prefix = "";
1817 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
1818 break;
1821 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
1822 switch (reg_maps->resource_info[entry->resource_idx].type)
1824 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1825 if (shadow_sampler)
1826 sampler_type = "sampler1DShadow";
1827 else
1828 sampler_type = "sampler1D";
1829 break;
1831 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1832 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1833 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1834 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1835 if (shadow_sampler)
1837 if (tex_rect)
1838 sampler_type = "sampler2DRectShadow";
1839 else
1840 sampler_type = "sampler2DShadow";
1842 else
1844 if (tex_rect)
1845 sampler_type = "sampler2DRect";
1846 else
1847 sampler_type = "sampler2D";
1849 break;
1851 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1852 if (shadow_sampler)
1853 FIXME("Unsupported 3D shadow sampler.\n");
1854 sampler_type = "sampler3D";
1855 break;
1857 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1858 if (shadow_sampler)
1859 FIXME("Unsupported Cube shadow sampler.\n");
1860 sampler_type = "samplerCube";
1861 break;
1863 default:
1864 sampler_type = "unsupported_sampler";
1865 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
1866 break;
1868 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
1869 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
1872 /* Declare uniforms for NP2 texcoord fixup:
1873 * This is NOT done inside the loop that declares the texture samplers
1874 * since the NP2 fixup code is currently only used for the GeforceFX
1875 * series and when forcing the ARB_npot extension off. Modern cards just
1876 * skip the code anyway, so put it inside a separate loop. */
1877 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1879 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1880 UINT cur = 0;
1882 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1883 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1884 * samplerNP2Fixup stores texture dimensions and is updated through
1885 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1887 for (i = 0; i < shader->limits->sampler; ++i)
1889 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1890 continue;
1892 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1894 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1895 continue;
1898 fixup->idx[i] = cur++;
1901 fixup->num_consts = (cur + 1) >> 1;
1902 fixup->active = ps_args->np2_fixup;
1903 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1906 /* Declare address variables */
1907 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1909 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1912 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1914 for (i = 0; i < shader->input_signature.element_count; ++i)
1916 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1917 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1918 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1919 prefix, e->register_idx);
1920 else
1921 shader_addline(buffer, "%s vec4 %s_in%u;\n",
1922 get_attribute_keyword(gl_info), prefix, e->register_idx);
1925 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1927 shader_addline(buffer, "uniform struct\n{\n");
1928 shader_addline(buffer, " float size;\n");
1929 shader_addline(buffer, " float size_min;\n");
1930 shader_addline(buffer, " float size_max;\n");
1931 shader_addline(buffer, "} ffp_point;\n");
1934 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1936 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1937 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1938 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1939 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1942 shader_addline(buffer, "uniform vec4 posFixup;\n");
1943 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1945 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1947 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1949 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1951 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1953 shader_addline(buffer, "uniform struct\n{\n");
1954 shader_addline(buffer, " vec4 color;\n");
1955 shader_addline(buffer, " float density;\n");
1956 shader_addline(buffer, " float end;\n");
1957 shader_addline(buffer, " float scale;\n");
1958 shader_addline(buffer, "} ffp_fog;\n");
1960 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1962 if (glsl_is_color_reg_read(shader, 0))
1963 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1964 if (glsl_is_color_reg_read(shader, 1))
1965 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1966 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1967 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1969 else
1971 if (glsl_is_color_reg_read(shader, 0))
1972 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1973 if (glsl_is_color_reg_read(shader, 1))
1974 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1975 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1976 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1977 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1981 if (version->major >= 3)
1983 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1985 if (ps_args->vp_mode == vertexshader)
1986 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1987 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1990 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1992 if (!(map & 1))
1993 continue;
1995 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1997 if (reg_maps->luminanceparams & (1u << i))
1999 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2000 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2001 extra_constants_needed++;
2004 extra_constants_needed++;
2007 if (ps_args->srgb_correction)
2009 shader_addline(buffer, "const vec4 srgb_const0 = ");
2010 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2011 shader_addline(buffer, ";\n");
2012 shader_addline(buffer, "const vec4 srgb_const1 = ");
2013 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2014 shader_addline(buffer, ";\n");
2016 if (reg_maps->vpos || reg_maps->usesdsy)
2018 ++extra_constants_needed;
2019 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2020 shader_addline(buffer, "vec4 vpos;\n");
2023 if (!needs_legacy_glsl_syntax(gl_info))
2024 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2026 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2027 FIXME("Insufficient uniforms to run this shader.\n");
2030 /* Declare output register temporaries */
2031 if (shader->limits->packed_output)
2032 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2034 /* Declare temporary variables */
2035 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2037 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
2040 /* Declare loop registers aLx */
2041 if (version->major < 4)
2043 for (i = 0; i < reg_maps->loop_depth; ++i)
2045 shader_addline(buffer, "int aL%u;\n", i);
2046 shader_addline(buffer, "int tmpInt%u;\n", i);
2050 /* Temporary variables for matrix operations */
2051 shader_addline(buffer, "vec4 tmp0;\n");
2052 shader_addline(buffer, "vec4 tmp1;\n");
2054 if (!shader->load_local_constsF)
2056 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2058 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2059 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2060 shader_addline(buffer, ";\n");
2064 /* Start the main program. */
2065 shader_addline(buffer, "void main()\n{\n");
2067 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2068 * add approximately 0.5. This causes off-by-one problems as spotted by
2069 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2070 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2071 * causes precision troubles when we just subtract 0.5.
2073 * To deal with that, just floor() the position. This will eliminate the
2074 * fraction on all cards.
2076 * TODO: Test how this behaves with multisampling.
2078 * An advantage of floor is that it works even if the driver doesn't add
2079 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2080 * to return in gl_FragCoord, even though coordinates specify the pixel
2081 * centers instead of the pixel corners. This code will behave correctly
2082 * on drivers that returns integer values. */
2083 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2085 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2086 shader_addline(buffer,
2087 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2088 else
2089 shader_addline(buffer,
2090 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2094 /*****************************************************************************
2095 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2097 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2098 ****************************************************************************/
2100 /* Prototypes */
2101 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2102 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2104 /** Used for opcode modifiers - They multiply the result by the specified amount */
2105 static const char * const shift_glsl_tab[] = {
2106 "", /* 0 (none) */
2107 "2.0 * ", /* 1 (x2) */
2108 "4.0 * ", /* 2 (x4) */
2109 "8.0 * ", /* 3 (x8) */
2110 "16.0 * ", /* 4 (x16) */
2111 "32.0 * ", /* 5 (x32) */
2112 "", /* 6 (x64) */
2113 "", /* 7 (x128) */
2114 "", /* 8 (d256) */
2115 "", /* 9 (d128) */
2116 "", /* 10 (d64) */
2117 "", /* 11 (d32) */
2118 "0.0625 * ", /* 12 (d16) */
2119 "0.125 * ", /* 13 (d8) */
2120 "0.25 * ", /* 14 (d4) */
2121 "0.5 * " /* 15 (d2) */
2124 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2125 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2126 const char *in_reg, const char *in_regswizzle, char *out_str)
2128 switch (src_modifier)
2130 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2131 case WINED3DSPSM_DW:
2132 case WINED3DSPSM_NONE:
2133 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2134 break;
2135 case WINED3DSPSM_NEG:
2136 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2137 break;
2138 case WINED3DSPSM_NOT:
2139 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2140 break;
2141 case WINED3DSPSM_BIAS:
2142 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2143 break;
2144 case WINED3DSPSM_BIASNEG:
2145 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2146 break;
2147 case WINED3DSPSM_SIGN:
2148 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2149 break;
2150 case WINED3DSPSM_SIGNNEG:
2151 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2152 break;
2153 case WINED3DSPSM_COMP:
2154 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2155 break;
2156 case WINED3DSPSM_X2:
2157 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2158 break;
2159 case WINED3DSPSM_X2NEG:
2160 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2161 break;
2162 case WINED3DSPSM_ABS:
2163 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2164 break;
2165 case WINED3DSPSM_ABSNEG:
2166 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2167 break;
2168 default:
2169 FIXME("Unhandled modifier %u\n", src_modifier);
2170 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2174 /** Writes the GLSL variable name that corresponds to the register that the
2175 * DX opcode parameter is trying to access */
2176 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2177 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2179 /* oPos, oFog and oPts in D3D */
2180 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2182 const struct wined3d_shader *shader = ins->ctx->shader;
2183 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2184 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2185 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2186 const char *prefix = shader_glsl_get_prefix(version->type);
2187 struct glsl_src_param rel_param0, rel_param1;
2188 char imm_str[4][17];
2190 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2191 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2192 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2193 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2194 *is_color = FALSE;
2196 switch (reg->type)
2198 case WINED3DSPR_TEMP:
2199 sprintf(register_name, "R%u", reg->idx[0].offset);
2200 break;
2202 case WINED3DSPR_INPUT:
2203 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2205 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2207 if (reg->idx[0].rel_addr)
2208 FIXME("VS3+ input registers relative addressing.\n");
2209 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2210 *is_color = TRUE;
2211 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2212 break;
2215 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2217 if (reg->idx[0].rel_addr)
2219 if (reg->idx[1].rel_addr)
2220 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2221 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2222 else
2223 sprintf(register_name, "gs_in[%s + %u][%u]",
2224 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2226 else if (reg->idx[1].rel_addr)
2227 sprintf(register_name, "gs_in[%u][%s + %u]",
2228 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2229 else
2230 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2231 break;
2234 /* pixel shaders >= 3.0 */
2235 if (version->major >= 3)
2237 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2238 unsigned int in_count = vec4_varyings(version->major, gl_info);
2240 if (reg->idx[0].rel_addr)
2242 /* Removing a + 0 would be an obvious optimization, but
2243 * OS X doesn't see the NOP operation there. */
2244 if (idx)
2246 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2247 && shader->u.ps.declared_in_count > in_count)
2249 sprintf(register_name,
2250 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2251 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2252 prefix, rel_param0.param_str, idx);
2254 else
2256 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2259 else
2261 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2262 && shader->u.ps.declared_in_count > in_count)
2264 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2265 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2266 prefix, rel_param0.param_str);
2268 else
2270 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2274 else
2276 if (idx == in_count) sprintf(register_name, "gl_Color");
2277 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2278 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2281 else
2283 if (!reg->idx[0].offset)
2284 strcpy(register_name, "ffp_varying_diffuse");
2285 else
2286 strcpy(register_name, "ffp_varying_specular");
2287 break;
2289 break;
2291 case WINED3DSPR_CONST:
2293 /* Relative addressing */
2294 if (reg->idx[0].rel_addr)
2296 if (wined3d_settings.check_float_constants)
2297 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2298 rel_param0.param_str, reg->idx[0].offset,
2299 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2300 prefix, rel_param0.param_str, reg->idx[0].offset);
2301 else if (reg->idx[0].offset)
2302 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2303 else
2304 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2306 else
2308 if (shader_constant_is_local(shader, reg->idx[0].offset))
2309 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2310 else
2311 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2314 break;
2316 case WINED3DSPR_CONSTINT:
2317 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2318 break;
2320 case WINED3DSPR_CONSTBOOL:
2321 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2322 break;
2324 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2325 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2326 sprintf(register_name, "T%u", reg->idx[0].offset);
2327 else
2328 sprintf(register_name, "A%u", reg->idx[0].offset);
2329 break;
2331 case WINED3DSPR_LOOP:
2332 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2333 break;
2335 case WINED3DSPR_SAMPLER:
2336 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2337 break;
2339 case WINED3DSPR_COLOROUT:
2340 if (reg->idx[0].offset >= gl_info->limits.buffers)
2341 WARN("Write to render target %u, only %d supported.\n",
2342 reg->idx[0].offset, gl_info->limits.buffers);
2344 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2345 break;
2347 case WINED3DSPR_RASTOUT:
2348 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2349 break;
2351 case WINED3DSPR_DEPTHOUT:
2352 sprintf(register_name, "gl_FragDepth");
2353 break;
2355 case WINED3DSPR_ATTROUT:
2356 if (!reg->idx[0].offset)
2357 sprintf(register_name, "%s_out[8]", prefix);
2358 else
2359 sprintf(register_name, "%s_out[9]", prefix);
2360 break;
2362 case WINED3DSPR_TEXCRDOUT:
2363 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2364 if (reg->idx[0].rel_addr)
2365 FIXME("VS3 output registers relative addressing.\n");
2366 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2367 break;
2369 case WINED3DSPR_MISCTYPE:
2370 if (!reg->idx[0].offset)
2372 /* vPos */
2373 sprintf(register_name, "vpos");
2375 else if (reg->idx[0].offset == 1)
2377 /* Note that gl_FrontFacing is a bool, while vFace is
2378 * a float for which the sign determines front/back */
2379 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2381 else
2383 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2384 sprintf(register_name, "unrecognized_register");
2386 break;
2388 case WINED3DSPR_IMMCONST:
2389 switch (reg->immconst_type)
2391 case WINED3D_IMMCONST_SCALAR:
2392 switch (reg->data_type)
2394 case WINED3D_DATA_FLOAT:
2395 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2396 break;
2397 case WINED3D_DATA_INT:
2398 sprintf(register_name, "%#x", reg->immconst_data[0]);
2399 break;
2400 case WINED3D_DATA_RESOURCE:
2401 case WINED3D_DATA_SAMPLER:
2402 case WINED3D_DATA_UINT:
2403 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2404 break;
2405 default:
2406 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2407 break;
2409 break;
2411 case WINED3D_IMMCONST_VEC4:
2412 switch (reg->data_type)
2414 case WINED3D_DATA_FLOAT:
2415 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2416 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2417 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2418 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2419 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2420 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2421 break;
2422 case WINED3D_DATA_INT:
2423 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2424 reg->immconst_data[0], reg->immconst_data[1],
2425 reg->immconst_data[2], reg->immconst_data[3]);
2426 break;
2427 case WINED3D_DATA_RESOURCE:
2428 case WINED3D_DATA_SAMPLER:
2429 case WINED3D_DATA_UINT:
2430 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2431 reg->immconst_data[0], reg->immconst_data[1],
2432 reg->immconst_data[2], reg->immconst_data[3]);
2433 break;
2434 default:
2435 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2436 break;
2438 break;
2440 default:
2441 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2442 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2444 break;
2446 case WINED3DSPR_CONSTBUFFER:
2447 if (reg->idx[1].rel_addr)
2448 sprintf(register_name, "%s_cb%u[%s + %u]",
2449 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2450 else
2451 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2452 break;
2454 case WINED3DSPR_IMMCONSTBUFFER:
2455 if (reg->idx[0].rel_addr)
2456 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2457 else
2458 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2459 break;
2461 case WINED3DSPR_PRIMID:
2462 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2463 break;
2465 default:
2466 FIXME("Unhandled register type %#x.\n", reg->type);
2467 sprintf(register_name, "unrecognized_register");
2468 break;
2472 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2474 *str++ = '.';
2475 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2476 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2477 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2478 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2479 *str = '\0';
2482 /* Get the GLSL write mask for the destination register */
2483 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2485 DWORD mask = param->write_mask;
2487 if (shader_is_scalar(&param->reg))
2489 mask = WINED3DSP_WRITEMASK_0;
2490 *write_mask = '\0';
2492 else
2494 shader_glsl_write_mask_to_str(mask, write_mask);
2497 return mask;
2500 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2501 unsigned int size = 0;
2503 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2504 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2505 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2506 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2508 return size;
2511 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2513 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2514 * but addressed as "rgba". To fix this we need to swap the register's x
2515 * and z components. */
2516 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2518 *str++ = '.';
2519 /* swizzle bits fields: wwzzyyxx */
2520 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2521 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2522 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2523 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2524 *str = '\0';
2527 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2528 BOOL fixup, DWORD mask, char *swizzle_str)
2530 if (shader_is_scalar(&param->reg))
2531 *swizzle_str = '\0';
2532 else
2533 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2536 /* From a given parameter token, generate the corresponding GLSL string.
2537 * Also, return the actual register name and swizzle in case the
2538 * caller needs this information as well. */
2539 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2540 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2542 BOOL is_color = FALSE;
2543 char swizzle_str[6];
2545 glsl_src->reg_name[0] = '\0';
2546 glsl_src->param_str[0] = '\0';
2547 swizzle_str[0] = '\0';
2549 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2550 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2552 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2554 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2556 else
2558 char reg_name[200];
2560 switch (wined3d_src->reg.data_type)
2562 case WINED3D_DATA_FLOAT:
2563 sprintf(reg_name, "%s", glsl_src->reg_name);
2564 break;
2565 case WINED3D_DATA_INT:
2566 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2567 break;
2568 case WINED3D_DATA_RESOURCE:
2569 case WINED3D_DATA_SAMPLER:
2570 case WINED3D_DATA_UINT:
2571 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2572 break;
2573 default:
2574 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2575 sprintf(reg_name, "%s", glsl_src->reg_name);
2576 break;
2579 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2583 /* From a given parameter token, generate the corresponding GLSL string.
2584 * Also, return the actual register name and swizzle in case the
2585 * caller needs this information as well. */
2586 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2587 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2589 BOOL is_color = FALSE;
2591 glsl_dst->mask_str[0] = '\0';
2592 glsl_dst->reg_name[0] = '\0';
2594 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2595 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2598 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2599 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2600 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2601 enum wined3d_data_type data_type)
2603 struct glsl_dst_param glsl_dst;
2604 DWORD mask;
2606 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2608 switch (data_type)
2610 case WINED3D_DATA_FLOAT:
2611 shader_addline(buffer, "%s%s = %s(",
2612 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2613 break;
2614 case WINED3D_DATA_INT:
2615 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2616 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2617 break;
2618 case WINED3D_DATA_RESOURCE:
2619 case WINED3D_DATA_SAMPLER:
2620 case WINED3D_DATA_UINT:
2621 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2622 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2623 break;
2624 default:
2625 FIXME("Unhandled data type %#x.\n", data_type);
2626 shader_addline(buffer, "%s%s = %s(",
2627 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2628 break;
2632 return mask;
2635 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2636 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2638 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2641 /** Process GLSL instruction modifiers */
2642 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2644 struct glsl_dst_param dst_param;
2645 DWORD modifiers;
2647 if (!ins->dst_count) return;
2649 modifiers = ins->dst[0].modifiers;
2650 if (!modifiers) return;
2652 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2654 if (modifiers & WINED3DSPDM_SATURATE)
2656 /* _SAT means to clamp the value of the register to between 0 and 1 */
2657 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2658 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2661 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2663 FIXME("_centroid modifier not handled\n");
2666 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2668 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2672 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2674 switch (op)
2676 case WINED3D_SHADER_REL_OP_GT: return ">";
2677 case WINED3D_SHADER_REL_OP_EQ: return "==";
2678 case WINED3D_SHADER_REL_OP_GE: return ">=";
2679 case WINED3D_SHADER_REL_OP_LT: return "<";
2680 case WINED3D_SHADER_REL_OP_NE: return "!=";
2681 case WINED3D_SHADER_REL_OP_LE: return "<=";
2682 default:
2683 FIXME("Unrecognized operator %#x.\n", op);
2684 return "(\?\?)";
2688 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2689 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
2691 static const struct
2693 unsigned int coord_size;
2694 unsigned int offset_size;
2695 const char *type_part;
2697 resource_types[] =
2699 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2700 {1, 0, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2701 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2702 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2703 {2, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2704 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2705 {3, 0, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2706 {2, 1, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2707 {3, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2708 {3, 0, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2710 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2711 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2712 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2713 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
2714 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2715 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2716 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
2717 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2718 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2719 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2720 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
2721 const char *base = "texture", *type_part = "", *suffix = "";
2722 unsigned int coord_size;
2724 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2726 if (resource_type >= ARRAY_SIZE(resource_types))
2728 ERR("Unexpected resource type %#x.\n", resource_type);
2729 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2732 /* Note that there's no such thing as a projected cube texture. */
2733 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2734 projected = FALSE;
2736 if (needs_legacy_glsl_syntax(gl_info))
2738 if (shadow)
2739 base = "shadow";
2741 type_part = resource_types[resource_type].type_part;
2742 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2743 type_part = "2DRect";
2744 if (!type_part[0])
2745 FIXME("Unhandled resource type %#x.\n", resource_type);
2747 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2749 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2750 suffix = "ARB";
2751 else
2752 FIXME("Unsupported grad function.\n");
2756 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2758 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
2759 if (flags & ~texel_fetch_flags)
2760 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
2762 base = "texelFetch";
2763 type_part = "";
2766 sample_function->offset_size = offset ? resource_types[resource_type].offset_size : 0;
2767 if (offset && !sample_function->offset_size)
2769 FIXME("Offset not supported for resource type %#x.\n", resource_type);
2770 offset = FALSE;
2773 sample_function->name = string_buffer_get(priv->string_buffers);
2774 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2775 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
2777 coord_size = resource_types[resource_type].coord_size;
2778 if (shadow)
2779 ++coord_size;
2780 sample_function->coord_mask = (1u << coord_size) - 1;
2781 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2784 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2785 struct glsl_sample_function *sample_function)
2787 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2789 string_buffer_release(priv->string_buffers, sample_function->name);
2792 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2793 BOOL sign_fixup, enum fixup_channel_source channel_source)
2795 switch(channel_source)
2797 case CHANNEL_SOURCE_ZERO:
2798 strcat(arguments, "0.0");
2799 break;
2801 case CHANNEL_SOURCE_ONE:
2802 strcat(arguments, "1.0");
2803 break;
2805 case CHANNEL_SOURCE_X:
2806 strcat(arguments, reg_name);
2807 strcat(arguments, ".x");
2808 break;
2810 case CHANNEL_SOURCE_Y:
2811 strcat(arguments, reg_name);
2812 strcat(arguments, ".y");
2813 break;
2815 case CHANNEL_SOURCE_Z:
2816 strcat(arguments, reg_name);
2817 strcat(arguments, ".z");
2818 break;
2820 case CHANNEL_SOURCE_W:
2821 strcat(arguments, reg_name);
2822 strcat(arguments, ".w");
2823 break;
2825 default:
2826 FIXME("Unhandled channel source %#x\n", channel_source);
2827 strcat(arguments, "undefined");
2828 break;
2831 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2834 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2835 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2837 unsigned int mask_size, remaining;
2838 DWORD fixup_mask = 0;
2839 char arguments[256];
2840 char mask_str[6];
2842 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2843 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2844 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2845 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2846 if (!(mask &= fixup_mask))
2847 return;
2849 if (is_complex_fixup(fixup))
2851 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2852 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2853 return;
2856 shader_glsl_write_mask_to_str(mask, mask_str);
2857 mask_size = shader_glsl_get_write_mask_size(mask);
2859 arguments[0] = '\0';
2860 remaining = mask_size;
2861 if (mask & WINED3DSP_WRITEMASK_0)
2863 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2864 if (--remaining) strcat(arguments, ", ");
2866 if (mask & WINED3DSP_WRITEMASK_1)
2868 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2869 if (--remaining) strcat(arguments, ", ");
2871 if (mask & WINED3DSP_WRITEMASK_2)
2873 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2874 if (--remaining) strcat(arguments, ", ");
2876 if (mask & WINED3DSP_WRITEMASK_3)
2878 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2879 if (--remaining) strcat(arguments, ", ");
2882 if (mask_size > 1)
2883 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2884 else
2885 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2888 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2890 char reg_name[256];
2891 BOOL is_color;
2893 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2894 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2897 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2898 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
2899 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
2900 const char *coord_reg_fmt, ...)
2902 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2903 char dst_swizzle[6];
2904 struct color_fixup_desc fixup;
2905 BOOL np2_fixup = FALSE;
2906 va_list args;
2907 int ret;
2909 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2911 /* If ARB_texture_swizzle is supported we don't need to do anything here.
2912 * We actually rely on it for vertex shaders and SM4+. */
2913 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2915 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2916 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
2918 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
2919 np2_fixup = TRUE;
2921 else
2923 fixup = COLOR_FIXUP_IDENTITY;
2926 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2928 if (sample_function->output_single_component)
2929 shader_addline(ins->ctx->buffer, "vec4(");
2931 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2932 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
2934 for (;;)
2936 va_start(args, coord_reg_fmt);
2937 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2938 va_end(args);
2939 if (!ret)
2940 break;
2941 if (!string_buffer_resize(ins->ctx->buffer, ret))
2942 break;
2945 if (np2_fixup)
2947 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2948 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
2950 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2952 case 1:
2953 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2954 idx >> 1, (idx % 2) ? "z" : "x");
2955 break;
2956 case 2:
2957 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2958 idx >> 1, (idx % 2) ? "zw" : "xy");
2959 break;
2960 case 3:
2961 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2962 idx >> 1, (idx % 2) ? "zw" : "xy");
2963 break;
2964 case 4:
2965 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2966 idx >> 1, (idx % 2) ? "zw" : "xy");
2967 break;
2970 if (dx && dy)
2971 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
2972 else if (bias)
2973 shader_addline(ins->ctx->buffer, ", %s", bias);
2974 if (sample_function->offset_size)
2976 int offset_immdata[4] = {offset->u, offset->v, offset->w};
2977 shader_addline(ins->ctx->buffer, ", ");
2978 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
2980 shader_addline(ins->ctx->buffer, ")");
2982 if (sample_function->output_single_component)
2983 shader_addline(ins->ctx->buffer, ")");
2985 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2987 if (!is_identity_fixup(fixup))
2988 shader_glsl_color_correction(ins, fixup);
2991 /*****************************************************************************
2992 * Begin processing individual instruction opcodes
2993 ****************************************************************************/
2995 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2997 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2998 struct glsl_src_param src0_param;
2999 struct glsl_src_param src1_param;
3000 DWORD write_mask;
3001 const char *op;
3003 /* Determine the GLSL operator to use based on the opcode */
3004 switch (ins->handler_idx)
3006 case WINED3DSIH_ADD: op = "+"; break;
3007 case WINED3DSIH_AND: op = "&"; break;
3008 case WINED3DSIH_DIV: op = "/"; break;
3009 case WINED3DSIH_IADD: op = "+"; break;
3010 case WINED3DSIH_ISHL: op = "<<"; break;
3011 case WINED3DSIH_MUL: op = "*"; break;
3012 case WINED3DSIH_OR: op = "|"; break;
3013 case WINED3DSIH_SUB: op = "-"; break;
3014 case WINED3DSIH_USHR: op = ">>"; break;
3015 case WINED3DSIH_XOR: op = "^"; break;
3016 default:
3017 op = "<unhandled operator>";
3018 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3019 break;
3022 write_mask = shader_glsl_append_dst(buffer, ins);
3023 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3024 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3025 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3028 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3030 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3031 struct glsl_src_param src0_param;
3032 struct glsl_src_param src1_param;
3033 unsigned int mask_size;
3034 DWORD write_mask;
3035 const char *op;
3037 write_mask = shader_glsl_append_dst(buffer, ins);
3038 mask_size = shader_glsl_get_write_mask_size(write_mask);
3039 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3040 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3042 if (mask_size > 1)
3044 switch (ins->handler_idx)
3046 case WINED3DSIH_EQ: op = "equal"; break;
3047 case WINED3DSIH_IEQ: op = "equal"; break;
3048 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3049 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3050 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3051 case WINED3DSIH_LT: op = "lessThan"; break;
3052 case WINED3DSIH_ILT: op = "lessThan"; break;
3053 case WINED3DSIH_ULT: op = "lessThan"; break;
3054 case WINED3DSIH_NE: op = "notEqual"; break;
3055 case WINED3DSIH_INE: op = "notEqual"; break;
3056 default:
3057 op = "<unhandled operator>";
3058 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3059 break;
3062 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3063 mask_size, op, src0_param.param_str, src1_param.param_str);
3065 else
3067 switch (ins->handler_idx)
3069 case WINED3DSIH_EQ: op = "=="; break;
3070 case WINED3DSIH_IEQ: op = "=="; break;
3071 case WINED3DSIH_GE: op = ">="; break;
3072 case WINED3DSIH_IGE: op = ">="; break;
3073 case WINED3DSIH_UGE: op = ">="; break;
3074 case WINED3DSIH_LT: op = "<"; break;
3075 case WINED3DSIH_ILT: op = "<"; break;
3076 case WINED3DSIH_ULT: op = "<"; break;
3077 case WINED3DSIH_NE: op = "!="; break;
3078 case WINED3DSIH_INE: op = "!="; break;
3079 default:
3080 op = "<unhandled operator>";
3081 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3082 break;
3085 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3086 src0_param.param_str, op, src1_param.param_str);
3090 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3092 struct glsl_src_param src_param;
3093 DWORD write_mask;
3094 const char *op;
3096 switch (ins->handler_idx)
3098 case WINED3DSIH_INEG: op = "-"; break;
3099 case WINED3DSIH_NOT: op = "~"; break;
3100 default:
3101 op = "<unhandled operator>";
3102 ERR("Unhandled opcode %s.\n",
3103 debug_d3dshaderinstructionhandler(ins->handler_idx));
3104 break;
3107 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3108 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3109 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3112 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3114 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3115 struct glsl_src_param src0_param;
3116 struct glsl_src_param src1_param;
3117 DWORD write_mask;
3119 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3120 * not, we can emulate it. */
3121 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3122 FIXME("64-bit integer multiplies not implemented.\n");
3124 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3126 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3127 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3128 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3130 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3131 src0_param.param_str, src1_param.param_str);
3135 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3137 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3138 struct glsl_src_param src0_param, src1_param;
3139 DWORD write_mask;
3141 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3143 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3145 char dst_mask[6];
3147 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3148 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3149 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3150 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3151 dst_mask, src0_param.param_str, src1_param.param_str);
3153 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3154 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3155 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3156 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3158 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3159 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3161 else
3163 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3164 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3165 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3166 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3169 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3171 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3172 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3173 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3174 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3178 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3179 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3181 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3182 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3183 struct glsl_src_param src0_param;
3184 DWORD write_mask;
3186 write_mask = shader_glsl_append_dst(buffer, ins);
3187 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3189 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3190 * shader versions WINED3DSIO_MOVA is used for this. */
3191 if (ins->ctx->reg_maps->shader_version.major == 1
3192 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3193 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3195 /* This is a simple floor() */
3196 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3197 if (mask_size > 1) {
3198 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3199 } else {
3200 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3203 else if(ins->handler_idx == WINED3DSIH_MOVA)
3205 /* We need to *round* to the nearest int here. */
3206 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3208 if (gl_info->supported[EXT_GPU_SHADER4])
3210 if (mask_size > 1)
3211 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3212 else
3213 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3215 else
3217 if (mask_size > 1)
3218 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3219 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3220 else
3221 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3222 src0_param.param_str, src0_param.param_str);
3225 else
3227 shader_addline(buffer, "%s);\n", src0_param.param_str);
3231 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3232 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3234 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3235 struct glsl_src_param src0_param;
3236 struct glsl_src_param src1_param;
3237 DWORD dst_write_mask, src_write_mask;
3238 unsigned int dst_size;
3240 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3241 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3243 /* dp4 works on vec4, dp3 on vec3, etc. */
3244 if (ins->handler_idx == WINED3DSIH_DP4)
3245 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3246 else if (ins->handler_idx == WINED3DSIH_DP3)
3247 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3248 else
3249 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3251 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3252 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3254 if (dst_size > 1) {
3255 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3256 } else {
3257 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3261 /* Note that this instruction has some restrictions. The destination write mask
3262 * can't contain the w component, and the source swizzles have to be .xyzw */
3263 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3265 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3266 struct glsl_src_param src0_param;
3267 struct glsl_src_param src1_param;
3268 char dst_mask[6];
3270 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3271 shader_glsl_append_dst(ins->ctx->buffer, ins);
3272 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3273 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3274 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3277 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3279 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3282 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3283 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3284 * GLSL uses the value as-is. */
3285 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3287 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3288 struct glsl_src_param src0_param;
3289 struct glsl_src_param src1_param;
3290 DWORD dst_write_mask;
3291 unsigned int dst_size;
3293 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3294 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3296 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3297 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3299 if (dst_size > 1)
3301 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3302 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3304 else
3306 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3307 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3311 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3312 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3314 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3315 struct glsl_src_param src_param;
3316 const char *instruction;
3317 DWORD write_mask;
3318 unsigned i;
3320 /* Determine the GLSL function to use based on the opcode */
3321 /* TODO: Possibly make this a table for faster lookups */
3322 switch (ins->handler_idx)
3324 case WINED3DSIH_ABS: instruction = "abs"; break;
3325 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3326 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3327 case WINED3DSIH_FRC: instruction = "fract"; break;
3328 case WINED3DSIH_IMAX: instruction = "max"; break;
3329 case WINED3DSIH_IMIN: instruction = "min"; break;
3330 case WINED3DSIH_MAX: instruction = "max"; break;
3331 case WINED3DSIH_MIN: instruction = "min"; break;
3332 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3333 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3334 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3335 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3336 default: instruction = "";
3337 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3338 break;
3341 write_mask = shader_glsl_append_dst(buffer, ins);
3343 shader_addline(buffer, "%s(", instruction);
3345 if (ins->src_count)
3347 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3348 shader_addline(buffer, "%s", src_param.param_str);
3349 for (i = 1; i < ins->src_count; ++i)
3351 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3352 shader_addline(buffer, ", %s", src_param.param_str);
3356 shader_addline(buffer, "));\n");
3359 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3361 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3363 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3364 struct glsl_src_param src_param;
3365 unsigned int mask_size;
3366 DWORD write_mask;
3367 char dst_mask[6];
3369 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3370 mask_size = shader_glsl_get_write_mask_size(write_mask);
3371 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3373 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3374 src_param.param_str, src_param.param_str);
3375 shader_glsl_append_dst(buffer, ins);
3377 if (mask_size > 1)
3379 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3380 mask_size, src_param.param_str);
3382 else
3384 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3385 src_param.param_str);
3389 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3391 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3392 ins->ctx->reg_maps->shader_version.minor);
3393 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3394 struct glsl_src_param src0_param;
3395 const char *prefix, *suffix;
3396 unsigned int dst_size;
3397 DWORD dst_write_mask;
3399 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3400 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3402 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3403 dst_write_mask = WINED3DSP_WRITEMASK_3;
3405 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3407 switch (ins->handler_idx)
3409 case WINED3DSIH_EXP:
3410 case WINED3DSIH_EXPP:
3411 prefix = "exp2(";
3412 suffix = ")";
3413 break;
3415 case WINED3DSIH_LOG:
3416 case WINED3DSIH_LOGP:
3417 prefix = "log2(abs(";
3418 suffix = "))";
3419 break;
3421 case WINED3DSIH_RCP:
3422 prefix = "1.0 / ";
3423 suffix = "";
3424 break;
3426 case WINED3DSIH_RSQ:
3427 prefix = "inversesqrt(abs(";
3428 suffix = "))";
3429 break;
3431 default:
3432 prefix = "";
3433 suffix = "";
3434 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3435 break;
3438 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3439 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3440 else
3441 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3444 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3445 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3446 * dst.x = 2^(floor(src))
3447 * dst.y = src - floor(src)
3448 * dst.z = 2^src (partial precision is allowed, but optional)
3449 * dst.w = 1.0;
3450 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3451 * dst = 2^src; (partial precision is allowed, but optional)
3453 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3455 if (ins->ctx->reg_maps->shader_version.major < 2)
3457 struct glsl_src_param src_param;
3458 char dst_mask[6];
3460 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3462 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3463 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3464 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3465 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3467 shader_glsl_append_dst(ins->ctx->buffer, ins);
3468 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3469 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3470 return;
3473 shader_glsl_scalar_op(ins);
3476 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3477 const char *vector_constructor, const char *scalar_constructor)
3479 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3480 struct glsl_src_param src_param;
3481 unsigned int mask_size;
3482 DWORD write_mask;
3484 write_mask = shader_glsl_append_dst(buffer, ins);
3485 mask_size = shader_glsl_get_write_mask_size(write_mask);
3486 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3488 if (mask_size > 1)
3489 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3490 else
3491 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3494 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3496 shader_glsl_cast(ins, "ivec", "int");
3499 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3501 shader_glsl_cast(ins, "uvec", "uint");
3504 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3506 shader_glsl_cast(ins, "vec", "float");
3509 /** Process signed comparison opcodes in GLSL. */
3510 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3512 struct glsl_src_param src0_param;
3513 struct glsl_src_param src1_param;
3514 DWORD write_mask;
3515 unsigned int mask_size;
3517 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3518 mask_size = shader_glsl_get_write_mask_size(write_mask);
3519 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3520 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3522 if (mask_size > 1) {
3523 const char *compare;
3525 switch(ins->handler_idx)
3527 case WINED3DSIH_SLT: compare = "lessThan"; break;
3528 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3529 default: compare = "";
3530 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3533 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3534 src0_param.param_str, src1_param.param_str);
3535 } else {
3536 switch(ins->handler_idx)
3538 case WINED3DSIH_SLT:
3539 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3540 * to return 0.0 but step returns 1.0 because step is not < x
3541 * An alternative is a bvec compare padded with an unused second component.
3542 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3543 * issue. Playing with not() is not possible either because not() does not accept
3544 * a scalar.
3546 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3547 src0_param.param_str, src1_param.param_str);
3548 break;
3549 case WINED3DSIH_SGE:
3550 /* Here we can use the step() function and safe a conditional */
3551 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3552 break;
3553 default:
3554 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3560 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3562 const char *condition_prefix, *condition_suffix;
3563 struct wined3d_shader_dst_param dst;
3564 struct glsl_src_param src0_param;
3565 struct glsl_src_param src1_param;
3566 struct glsl_src_param src2_param;
3567 BOOL temp_destination = FALSE;
3568 DWORD cmp_channel = 0;
3569 unsigned int i, j;
3570 char mask_char[6];
3571 DWORD write_mask;
3573 switch (ins->handler_idx)
3575 case WINED3DSIH_CMP:
3576 condition_prefix = "";
3577 condition_suffix = " >= 0.0";
3578 break;
3580 case WINED3DSIH_CND:
3581 condition_prefix = "";
3582 condition_suffix = " > 0.5";
3583 break;
3585 case WINED3DSIH_MOVC:
3586 condition_prefix = "bool(";
3587 condition_suffix = ")";
3588 break;
3590 default:
3591 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3592 condition_prefix = "<unhandled prefix>";
3593 condition_suffix = "<unhandled suffix>";
3594 break;
3597 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3599 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3600 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3601 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3602 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3604 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3605 condition_prefix, src0_param.param_str, condition_suffix,
3606 src1_param.param_str, src2_param.param_str);
3607 return;
3610 dst = ins->dst[0];
3612 /* Splitting the instruction up in multiple lines imposes a problem:
3613 * The first lines may overwrite source parameters of the following lines.
3614 * Deal with that by using a temporary destination register if needed. */
3615 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3616 && ins->src[0].reg.type == dst.reg.type)
3617 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3618 && ins->src[1].reg.type == dst.reg.type)
3619 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3620 && ins->src[2].reg.type == dst.reg.type))
3621 temp_destination = TRUE;
3623 /* Cycle through all source0 channels. */
3624 for (i = 0; i < 4; ++i)
3626 write_mask = 0;
3627 /* Find the destination channels which use the current source0 channel. */
3628 for (j = 0; j < 4; ++j)
3630 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3632 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3633 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3636 dst.write_mask = ins->dst[0].write_mask & write_mask;
3638 if (temp_destination)
3640 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3641 continue;
3642 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3644 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3645 continue;
3647 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3648 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3649 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3651 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3652 condition_prefix, src0_param.param_str, condition_suffix,
3653 src1_param.param_str, src2_param.param_str);
3656 if (temp_destination)
3658 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3659 shader_glsl_append_dst(ins->ctx->buffer, ins);
3660 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3664 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3665 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3666 * the compare is done per component of src0. */
3667 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3669 struct glsl_src_param src0_param;
3670 struct glsl_src_param src1_param;
3671 struct glsl_src_param src2_param;
3672 DWORD write_mask;
3673 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3674 ins->ctx->reg_maps->shader_version.minor);
3676 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3678 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3679 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3680 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3681 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3683 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3684 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3685 else
3686 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3687 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3688 return;
3691 shader_glsl_conditional_move(ins);
3694 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3695 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3697 struct glsl_src_param src0_param;
3698 struct glsl_src_param src1_param;
3699 struct glsl_src_param src2_param;
3700 DWORD write_mask;
3702 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3703 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3704 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3705 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3706 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3707 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3710 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3711 Vertex shaders to GLSL codes */
3712 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3714 int i;
3715 int nComponents = 0;
3716 struct wined3d_shader_dst_param tmp_dst = {{0}};
3717 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3718 struct wined3d_shader_instruction tmp_ins;
3720 memset(&tmp_ins, 0, sizeof(tmp_ins));
3722 /* Set constants for the temporary argument */
3723 tmp_ins.ctx = ins->ctx;
3724 tmp_ins.dst_count = 1;
3725 tmp_ins.dst = &tmp_dst;
3726 tmp_ins.src_count = 2;
3727 tmp_ins.src = tmp_src;
3729 switch(ins->handler_idx)
3731 case WINED3DSIH_M4x4:
3732 nComponents = 4;
3733 tmp_ins.handler_idx = WINED3DSIH_DP4;
3734 break;
3735 case WINED3DSIH_M4x3:
3736 nComponents = 3;
3737 tmp_ins.handler_idx = WINED3DSIH_DP4;
3738 break;
3739 case WINED3DSIH_M3x4:
3740 nComponents = 4;
3741 tmp_ins.handler_idx = WINED3DSIH_DP3;
3742 break;
3743 case WINED3DSIH_M3x3:
3744 nComponents = 3;
3745 tmp_ins.handler_idx = WINED3DSIH_DP3;
3746 break;
3747 case WINED3DSIH_M3x2:
3748 nComponents = 2;
3749 tmp_ins.handler_idx = WINED3DSIH_DP3;
3750 break;
3751 default:
3752 break;
3755 tmp_dst = ins->dst[0];
3756 tmp_src[0] = ins->src[0];
3757 tmp_src[1] = ins->src[1];
3758 for (i = 0; i < nComponents; ++i)
3760 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3761 shader_glsl_dot(&tmp_ins);
3762 ++tmp_src[1].reg.idx[0].offset;
3767 The LRP instruction performs a component-wise linear interpolation
3768 between the second and third operands using the first operand as the
3769 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3770 This is equivalent to mix(src2, src1, src0);
3772 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3774 struct glsl_src_param src0_param;
3775 struct glsl_src_param src1_param;
3776 struct glsl_src_param src2_param;
3777 DWORD write_mask;
3779 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3781 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3782 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3783 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3785 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3786 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3789 /** Process the WINED3DSIO_LIT instruction in GLSL:
3790 * dst.x = dst.w = 1.0
3791 * dst.y = (src0.x > 0) ? src0.x
3792 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3793 * where src.w is clamped at +- 128
3795 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3797 struct glsl_src_param src0_param;
3798 struct glsl_src_param src1_param;
3799 struct glsl_src_param src3_param;
3800 char dst_mask[6];
3802 shader_glsl_append_dst(ins->ctx->buffer, ins);
3803 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3805 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3806 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3807 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3809 /* The sdk specifies the instruction like this
3810 * dst.x = 1.0;
3811 * if(src.x > 0.0) dst.y = src.x
3812 * else dst.y = 0.0.
3813 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3814 * else dst.z = 0.0;
3815 * dst.w = 1.0;
3816 * (where power = src.w clamped between -128 and 128)
3818 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3819 * dst.x = 1.0 ... No further explanation needed
3820 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3821 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3822 * dst.w = 1.0. ... Nothing fancy.
3824 * So we still have one conditional in there. So do this:
3825 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3827 * 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),
3828 * 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.
3829 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3831 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3832 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3833 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3835 shader_addline(ins->ctx->buffer,
3836 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3837 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3838 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3839 src0_param.param_str, src3_param.param_str, dst_mask);
3842 /** Process the WINED3DSIO_DST instruction in GLSL:
3843 * dst.x = 1.0
3844 * dst.y = src0.x * src0.y
3845 * dst.z = src0.z
3846 * dst.w = src1.w
3848 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3850 struct glsl_src_param src0y_param;
3851 struct glsl_src_param src0z_param;
3852 struct glsl_src_param src1y_param;
3853 struct glsl_src_param src1w_param;
3854 char dst_mask[6];
3856 shader_glsl_append_dst(ins->ctx->buffer, ins);
3857 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3859 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3860 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3861 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3862 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3864 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3865 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3868 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3869 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3870 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3872 * dst.x = cos(src0.?)
3873 * dst.y = sin(src0.?)
3874 * dst.z = dst.z
3875 * dst.w = dst.w
3877 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3879 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3880 struct glsl_src_param src0_param;
3881 DWORD write_mask;
3883 if (ins->ctx->reg_maps->shader_version.major < 4)
3885 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3887 write_mask = shader_glsl_append_dst(buffer, ins);
3888 switch (write_mask)
3890 case WINED3DSP_WRITEMASK_0:
3891 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3892 break;
3894 case WINED3DSP_WRITEMASK_1:
3895 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3896 break;
3898 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3899 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3900 src0_param.param_str, src0_param.param_str);
3901 break;
3903 default:
3904 ERR("Write mask should be .x, .y or .xy\n");
3905 break;
3908 return;
3911 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3914 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3916 char dst_mask[6];
3918 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3919 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3920 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3922 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3923 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3924 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3926 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3927 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3929 else
3931 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3932 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3933 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3936 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3938 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3939 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3940 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3944 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3945 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3946 * generate invalid code
3948 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3950 struct glsl_src_param src0_param;
3951 DWORD write_mask;
3953 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3956 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3959 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3960 * Start a for() loop where src1.y is the initial value of aL,
3961 * increment aL by src1.z for a total of src1.x iterations.
3962 * Need to use a temporary variable for this operation.
3964 /* FIXME: I don't think nested loops will work correctly this way. */
3965 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3967 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3968 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3969 const struct wined3d_shader *shader = ins->ctx->shader;
3970 const struct wined3d_shader_lconst *constant;
3971 struct glsl_src_param src1_param;
3972 const DWORD *control_values = NULL;
3974 if (ins->ctx->reg_maps->shader_version.major < 4)
3976 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3978 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3979 * class hardware doesn't support real varying indexing, but Microsoft
3980 * designed this feature for Shader model 2.x+. If the loop control is
3981 * known at compile time, the GLSL compiler can unroll the loop, and
3982 * replace indirect addressing with direct addressing. */
3983 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3985 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3987 if (constant->idx == ins->src[1].reg.idx[0].offset)
3989 control_values = constant->value;
3990 break;
3995 if (control_values)
3997 struct wined3d_shader_loop_control loop_control;
3998 loop_control.count = control_values[0];
3999 loop_control.start = control_values[1];
4000 loop_control.step = (int)control_values[2];
4002 if (loop_control.step > 0)
4004 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4005 loop_state->current_depth, loop_control.start,
4006 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4007 loop_state->current_depth, loop_control.step);
4009 else if (loop_control.step < 0)
4011 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4012 loop_state->current_depth, loop_control.start,
4013 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
4014 loop_state->current_depth, loop_control.step);
4016 else
4018 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4019 loop_state->current_depth, loop_control.start, loop_state->current_depth,
4020 loop_state->current_depth, loop_control.count,
4021 loop_state->current_depth);
4024 else
4026 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4027 loop_state->current_depth, loop_state->current_reg,
4028 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
4029 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
4032 ++loop_state->current_reg;
4034 else
4036 shader_addline(buffer, "for (;;)\n{\n");
4039 ++loop_state->current_depth;
4042 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4044 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4046 shader_addline(ins->ctx->buffer, "}\n");
4048 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4050 --loop_state->current_depth;
4051 --loop_state->current_reg;
4054 if (ins->handler_idx == WINED3DSIH_ENDREP)
4056 --loop_state->current_depth;
4060 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4062 const struct wined3d_shader *shader = ins->ctx->shader;
4063 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
4064 const struct wined3d_shader_lconst *constant;
4065 struct glsl_src_param src0_param;
4066 const DWORD *control_values = NULL;
4068 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4069 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4071 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4073 if (constant->idx == ins->src[0].reg.idx[0].offset)
4075 control_values = constant->value;
4076 break;
4081 if (control_values)
4083 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4084 loop_state->current_depth, loop_state->current_depth,
4085 control_values[0], loop_state->current_depth);
4087 else
4089 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4090 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4091 loop_state->current_depth, loop_state->current_depth,
4092 src0_param.param_str, loop_state->current_depth);
4095 ++loop_state->current_depth;
4098 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4100 struct glsl_src_param src0_param;
4102 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4103 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
4106 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4108 struct glsl_src_param src0_param;
4109 struct glsl_src_param src1_param;
4111 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4112 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4114 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4115 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4118 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4120 shader_addline(ins->ctx->buffer, "} else {\n");
4123 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4125 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4128 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4130 shader_addline(ins->ctx->buffer, "break;\n");
4133 /* FIXME: According to MSDN the compare is done per component. */
4134 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4136 struct glsl_src_param src0_param;
4137 struct glsl_src_param src1_param;
4139 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4140 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4142 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4143 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4146 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4148 struct glsl_src_param src_param;
4150 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4151 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4154 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4156 shader_addline(ins->ctx->buffer, "}\n");
4157 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4160 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4162 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4165 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4167 struct glsl_src_param src1_param;
4169 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4170 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4171 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4174 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4176 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4177 * function only suppresses the unhandled instruction warning
4181 /*********************************************
4182 * Pixel Shader Specific Code begins here
4183 ********************************************/
4184 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4186 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4187 ins->ctx->reg_maps->shader_version.minor);
4188 struct glsl_sample_function sample_function;
4189 DWORD sample_flags = 0;
4190 DWORD resource_idx;
4191 DWORD mask = 0, swizzle;
4192 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4194 /* 1.0-1.4: Use destination register as sampler source.
4195 * 2.0+: Use provided sampler source. */
4196 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4197 resource_idx = ins->dst[0].reg.idx[0].offset;
4198 else
4199 resource_idx = ins->src[1].reg.idx[0].offset;
4201 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4203 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4204 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4205 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4207 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4208 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4210 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4211 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4213 case WINED3D_TTFF_COUNT1:
4214 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4215 break;
4216 case WINED3D_TTFF_COUNT2:
4217 mask = WINED3DSP_WRITEMASK_1;
4218 break;
4219 case WINED3D_TTFF_COUNT3:
4220 mask = WINED3DSP_WRITEMASK_2;
4221 break;
4222 case WINED3D_TTFF_COUNT4:
4223 case WINED3D_TTFF_DISABLE:
4224 mask = WINED3DSP_WRITEMASK_3;
4225 break;
4229 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4231 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4233 if (src_mod == WINED3DSPSM_DZ) {
4234 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4235 mask = WINED3DSP_WRITEMASK_2;
4236 } else if (src_mod == WINED3DSPSM_DW) {
4237 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4238 mask = WINED3DSP_WRITEMASK_3;
4241 else
4243 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4244 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4246 /* ps 2.0 texldp instruction always divides by the fourth component. */
4247 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4248 mask = WINED3DSP_WRITEMASK_3;
4252 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4253 mask |= sample_function.coord_mask;
4254 sample_function.coord_mask = mask;
4256 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4257 else swizzle = ins->src[1].swizzle;
4259 /* 1.0-1.3: Use destination register as coordinate source.
4260 1.4+: Use provided coordinate source register. */
4261 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4263 char coord_mask[6];
4264 shader_glsl_write_mask_to_str(mask, coord_mask);
4265 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4266 "T%u%s", resource_idx, coord_mask);
4268 else
4270 struct glsl_src_param coord_param;
4271 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4272 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4274 struct glsl_src_param bias;
4275 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4276 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4277 NULL, "%s", coord_param.param_str);
4278 } else {
4279 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4280 "%s", coord_param.param_str);
4283 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4286 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4288 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4289 struct glsl_src_param coord_param, dx_param, dy_param;
4290 struct glsl_sample_function sample_function;
4291 DWORD sampler_idx;
4292 DWORD swizzle = ins->src[1].swizzle;
4294 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4296 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4297 shader_glsl_tex(ins);
4298 return;
4301 sampler_idx = ins->src[1].reg.idx[0].offset;
4303 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4304 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4305 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4306 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4308 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4309 NULL, NULL, "%s", coord_param.param_str);
4310 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4313 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4315 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4316 struct glsl_src_param coord_param, lod_param;
4317 struct glsl_sample_function sample_function;
4318 DWORD sampler_idx;
4319 DWORD swizzle = ins->src[1].swizzle;
4321 sampler_idx = ins->src[1].reg.idx[0].offset;
4323 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4324 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4326 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4328 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4329 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4331 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4332 * However, the NVIDIA drivers allow them in fragment shaders as well,
4333 * even without the appropriate extension. */
4334 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4336 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4337 "%s", coord_param.param_str);
4338 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4341 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4342 unsigned int resource_idx, unsigned int sampler_idx)
4344 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4345 unsigned int i;
4347 for (i = 0; i < sampler_map->count; ++i)
4349 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4350 return entries[i].bind_idx;
4353 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4355 return ~0u;
4358 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4360 static const unsigned int texture_size_component_count[] =
4362 0, /* WINED3D_SHADER_RESOURCE_NONE */
4363 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4364 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4365 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4366 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4367 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4368 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4369 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4370 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4371 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4374 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4375 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4376 enum wined3d_shader_resource_type resource_type;
4377 unsigned int resource_idx, sampler_bind_idx, i;
4378 enum wined3d_data_type dst_data_type;
4379 struct glsl_src_param lod_param;
4380 char dst_swizzle[6];
4381 DWORD write_mask;
4383 dst_data_type = ins->dst[0].reg.data_type;
4384 if (ins->flags == WINED3DSI_RESINFO_UINT)
4385 dst_data_type = WINED3D_DATA_UINT;
4386 else if (ins->flags)
4387 FIXME("Unhandled flags %#x.\n", ins->flags);
4389 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4390 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4392 resource_idx = ins->src[1].reg.idx[0].offset;
4393 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4394 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4395 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4396 resource_idx, WINED3D_SAMPLER_DEFAULT);
4398 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4400 ERR("Unexpected resource type %#x.\n", resource_type);
4401 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4404 if (dst_data_type == WINED3D_DATA_UINT)
4405 shader_addline(ins->ctx->buffer, "uvec4(");
4406 else
4407 shader_addline(ins->ctx->buffer, "vec4(");
4409 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4410 shader_glsl_get_prefix(version->type), sampler_bind_idx, lod_param.param_str);
4412 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4413 shader_addline(ins->ctx->buffer, "0, ");
4415 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4417 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4418 shader_glsl_get_prefix(version->type), sampler_bind_idx);
4420 else
4422 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4423 shader_addline(ins->ctx->buffer, "1");
4426 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4429 /* FIXME: The current implementation does not handle multisample textures correctly. */
4430 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4432 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4433 struct glsl_src_param coord_param, lod_param;
4434 struct glsl_sample_function sample_function;
4435 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4437 if (wined3d_shader_instruction_has_texel_offset(ins))
4438 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4440 resource_idx = ins->src[1].reg.idx[0].offset;
4441 sampler_idx = WINED3D_SAMPLER_DEFAULT;
4443 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4444 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4445 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4446 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4447 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4448 NULL, NULL, lod_param.param_str, &ins->texel_offset, "%s", coord_param.param_str);
4449 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4452 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4454 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
4455 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
4456 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4457 struct glsl_sample_function sample_function;
4458 DWORD flags = 0;
4460 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
4461 flags |= WINED3D_GLSL_SAMPLE_GRAD;
4462 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
4463 flags |= WINED3D_GLSL_SAMPLE_LOD;
4464 if (wined3d_shader_instruction_has_texel_offset(ins))
4465 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4467 resource_idx = ins->src[1].reg.idx[0].offset;
4468 sampler_idx = ins->src[2].reg.idx[0].offset;
4470 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4471 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4473 switch (ins->handler_idx)
4475 case WINED3DSIH_SAMPLE:
4476 break;
4477 case WINED3DSIH_SAMPLE_B:
4478 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4479 lod_param_str = lod_param.param_str;
4480 break;
4481 case WINED3DSIH_SAMPLE_GRAD:
4482 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dx_param);
4483 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.coord_mask, &dy_param);
4484 dx_param_str = dx_param.param_str;
4485 dy_param_str = dy_param.param_str;
4486 break;
4487 case WINED3DSIH_SAMPLE_LOD:
4488 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4489 lod_param_str = lod_param.param_str;
4490 break;
4491 default:
4492 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4493 break;
4496 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4497 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
4498 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
4499 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4502 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
4504 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4505 struct glsl_src_param coord_param, compare_param;
4506 struct glsl_sample_function sample_function;
4507 const char *lod_param = NULL;
4508 DWORD flags = 0;
4509 UINT coord_size;
4511 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
4513 lod_param = "0";
4514 flags |= WINED3D_GLSL_SAMPLE_LOD;
4517 if (wined3d_shader_instruction_has_texel_offset(ins))
4518 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
4520 resource_idx = ins->src[1].reg.idx[0].offset;
4521 sampler_idx = ins->src[2].reg.idx[0].offset;
4523 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
4524 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4525 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
4526 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
4527 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
4528 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4529 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
4530 coord_size, coord_param.param_str, compare_param.param_str);
4531 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4534 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4536 /* FIXME: Make this work for more than just 2D textures */
4537 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4538 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4540 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4542 char dst_mask[6];
4544 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4545 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4546 ins->dst[0].reg.idx[0].offset, dst_mask);
4548 else
4550 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4551 DWORD reg = ins->src[0].reg.idx[0].offset;
4552 char dst_swizzle[6];
4554 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4556 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4558 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4559 struct glsl_src_param div_param;
4560 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4562 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4564 if (mask_size > 1)
4565 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4566 else
4567 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4569 else
4571 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4576 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4577 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4578 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4579 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4581 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4582 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4583 struct glsl_sample_function sample_function;
4584 struct glsl_src_param src0_param;
4585 UINT mask_size;
4587 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4589 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4590 * scalar, and projected sampling would require 4.
4592 * It is a dependent read - not valid with conditional NP2 textures
4594 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4595 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4597 switch(mask_size)
4599 case 1:
4600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4601 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4602 break;
4604 case 2:
4605 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4606 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4607 break;
4609 case 3:
4610 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4611 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4612 break;
4614 default:
4615 FIXME("Unexpected mask size %u\n", mask_size);
4616 break;
4618 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4621 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4622 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4623 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4625 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4626 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4627 struct glsl_src_param src0_param;
4628 DWORD dst_mask;
4629 unsigned int mask_size;
4631 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4632 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4633 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4635 if (mask_size > 1) {
4636 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4637 } else {
4638 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4642 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4643 * Calculate the depth as dst.x / dst.y */
4644 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4646 struct glsl_dst_param dst_param;
4648 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4650 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4651 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4652 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4653 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4654 * >= 1.0 or < 0.0
4656 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4657 dst_param.reg_name, dst_param.reg_name);
4660 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4661 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4662 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4663 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4665 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4667 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4668 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4669 struct glsl_src_param src0_param;
4671 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4673 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4674 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4677 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4678 * Calculate the 1st of a 2-row matrix multiplication. */
4679 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4681 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4682 DWORD reg = ins->dst[0].reg.idx[0].offset;
4683 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4684 struct glsl_src_param src0_param;
4686 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4687 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4690 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4691 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4692 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4694 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4695 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4696 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4697 DWORD reg = ins->dst[0].reg.idx[0].offset;
4698 struct glsl_src_param src0_param;
4700 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4701 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4702 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4705 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4707 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4708 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4709 struct glsl_sample_function sample_function;
4710 DWORD reg = ins->dst[0].reg.idx[0].offset;
4711 struct glsl_src_param src0_param;
4713 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4714 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4716 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4718 /* Sample the texture using the calculated coordinates */
4719 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
4720 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4723 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4724 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4725 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4727 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4728 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4729 struct glsl_sample_function sample_function;
4730 DWORD reg = ins->dst[0].reg.idx[0].offset;
4731 struct glsl_src_param src0_param;
4733 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4734 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4736 /* Dependent read, not valid with conditional NP2 */
4737 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4739 /* Sample the texture using the calculated coordinates */
4740 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
4741 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4743 tex_mx->current_row = 0;
4746 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4747 * Perform the 3rd row of a 3x3 matrix multiply */
4748 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4750 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4751 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4752 DWORD reg = ins->dst[0].reg.idx[0].offset;
4753 struct glsl_src_param src0_param;
4754 char dst_mask[6];
4756 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4758 shader_glsl_append_dst(ins->ctx->buffer, ins);
4759 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4760 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4762 tex_mx->current_row = 0;
4765 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4766 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4767 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4769 struct glsl_src_param src0_param;
4770 struct glsl_src_param src1_param;
4771 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4772 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4773 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4774 struct glsl_sample_function sample_function;
4775 DWORD reg = ins->dst[0].reg.idx[0].offset;
4776 char coord_mask[6];
4778 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4779 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4781 /* Perform the last matrix multiply operation */
4782 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4783 /* Reflection calculation */
4784 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4786 /* Dependent read, not valid with conditional NP2 */
4787 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4788 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4790 /* Sample the texture */
4791 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4792 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4793 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4795 tex_mx->current_row = 0;
4798 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4799 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4800 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4802 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4803 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4804 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4805 struct glsl_sample_function sample_function;
4806 DWORD reg = ins->dst[0].reg.idx[0].offset;
4807 struct glsl_src_param src0_param;
4808 char coord_mask[6];
4810 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4812 /* Perform the last matrix multiply operation */
4813 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4815 /* Construct the eye-ray vector from w coordinates */
4816 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4817 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4818 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4820 /* Dependent read, not valid with conditional NP2 */
4821 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
4822 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4824 /* Sample the texture using the calculated coordinates */
4825 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4826 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
4827 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4829 tex_mx->current_row = 0;
4832 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4833 * Apply a fake bump map transform.
4834 * texbem is pshader <= 1.3 only, this saves a few version checks
4836 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4838 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4839 struct glsl_sample_function sample_function;
4840 struct glsl_src_param coord_param;
4841 DWORD sampler_idx;
4842 DWORD mask;
4843 DWORD flags;
4844 char coord_mask[6];
4846 sampler_idx = ins->dst[0].reg.idx[0].offset;
4847 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4848 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4850 /* Dependent read, not valid with conditional NP2 */
4851 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4852 mask = sample_function.coord_mask;
4854 shader_glsl_write_mask_to_str(mask, coord_mask);
4856 /* With projected textures, texbem only divides the static texture coord,
4857 * not the displacement, so we can't let GL handle this. */
4858 if (flags & WINED3D_PSARGS_PROJECTED)
4860 DWORD div_mask=0;
4861 char coord_div_mask[3];
4862 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4864 case WINED3D_TTFF_COUNT1:
4865 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4866 break;
4867 case WINED3D_TTFF_COUNT2:
4868 div_mask = WINED3DSP_WRITEMASK_1;
4869 break;
4870 case WINED3D_TTFF_COUNT3:
4871 div_mask = WINED3DSP_WRITEMASK_2;
4872 break;
4873 case WINED3D_TTFF_COUNT4:
4874 case WINED3D_TTFF_DISABLE:
4875 div_mask = WINED3DSP_WRITEMASK_3;
4876 break;
4878 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4879 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4882 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4884 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4885 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4886 coord_param.param_str, coord_mask);
4888 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4890 struct glsl_src_param luminance_param;
4891 struct glsl_dst_param dst_param;
4893 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4894 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4896 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4897 dst_param.reg_name, dst_param.mask_str,
4898 luminance_param.param_str, sampler_idx, sampler_idx);
4900 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4903 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4905 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4906 struct glsl_src_param src0_param, src1_param;
4908 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4909 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4911 shader_glsl_append_dst(ins->ctx->buffer, ins);
4912 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4913 src0_param.param_str, sampler_idx, src1_param.param_str);
4916 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4917 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4918 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4920 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4921 struct glsl_sample_function sample_function;
4922 struct glsl_src_param src0_param;
4924 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4926 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4927 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4928 "%s.wx", src0_param.reg_name);
4929 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4932 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4933 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4934 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4936 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4937 struct glsl_sample_function sample_function;
4938 struct glsl_src_param src0_param;
4940 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4942 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4943 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4944 "%s.yz", src0_param.reg_name);
4945 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4948 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4949 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4950 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4952 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4953 struct glsl_sample_function sample_function;
4954 struct glsl_src_param src0_param;
4956 /* Dependent read, not valid with conditional NP2 */
4957 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
4958 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4960 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
4961 "%s", src0_param.param_str);
4962 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4965 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4966 * If any of the first 3 components are < 0, discard this pixel */
4967 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4969 if (ins->ctx->reg_maps->shader_version.major >= 4)
4971 struct glsl_src_param src_param;
4973 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4974 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4976 else
4978 struct glsl_dst_param dst_param;
4980 /* The argument is a destination parameter, and no writemasks are allowed */
4981 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4983 /* 2.0 shaders compare all 4 components in texkill. */
4984 if (ins->ctx->reg_maps->shader_version.major >= 2)
4985 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4986 /* 1.x shaders only compare the first 3 components, probably due to
4987 * the nature of the texkill instruction as a tex* instruction, and
4988 * phase, which kills all .w components. Even if all 4 components are
4989 * defined, only the first 3 are used. */
4990 else
4991 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4995 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4996 * dst = dot2(src0, src1) + src2 */
4997 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4999 struct glsl_src_param src0_param;
5000 struct glsl_src_param src1_param;
5001 struct glsl_src_param src2_param;
5002 DWORD write_mask;
5003 unsigned int mask_size;
5005 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5006 mask_size = shader_glsl_get_write_mask_size(write_mask);
5008 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5009 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5010 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5012 if (mask_size > 1) {
5013 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5014 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5015 } else {
5016 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5017 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5021 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5022 const struct wined3d_shader_signature *input_signature,
5023 const struct wined3d_shader_reg_maps *reg_maps,
5024 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5026 unsigned int i;
5028 for (i = 0; i < input_signature->element_count; ++i)
5030 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5031 const char *semantic_name;
5032 UINT semantic_idx;
5033 char reg_mask[6];
5035 /* Unused */
5036 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5037 continue;
5039 semantic_name = input->semantic_name;
5040 semantic_idx = input->semantic_idx;
5041 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5043 if (args->vp_mode == vertexshader)
5045 if (input->sysval_semantic == WINED3D_SV_POSITION)
5046 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5047 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5048 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5049 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5050 else
5051 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5052 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5053 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5055 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5057 if (args->pointsprite)
5058 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5059 shader->u.ps.input_reg_map[input->register_idx]);
5060 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5061 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5062 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5063 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5064 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5065 else
5066 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5067 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5069 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5071 if (!semantic_idx)
5072 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5073 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5074 else if (semantic_idx == 1)
5075 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5076 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5077 else
5078 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5079 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5081 else
5083 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5084 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5089 /*********************************************
5090 * Vertex Shader Specific Code begins here
5091 ********************************************/
5093 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5095 struct glsl_program_key key;
5097 key.vs_id = entry->vs.id;
5098 key.gs_id = entry->gs.id;
5099 key.ps_id = entry->ps.id;
5101 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5103 ERR("Failed to insert program entry.\n");
5107 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5108 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5110 struct wine_rb_entry *entry;
5111 struct glsl_program_key key;
5113 key.vs_id = vs_id;
5114 key.gs_id = gs_id;
5115 key.ps_id = ps_id;
5117 entry = wine_rb_get(&priv->program_lookup, &key);
5118 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5121 /* Context activation is done by the caller. */
5122 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5123 struct glsl_shader_prog_link *entry)
5125 struct glsl_program_key key;
5127 key.vs_id = entry->vs.id;
5128 key.gs_id = entry->gs.id;
5129 key.ps_id = entry->ps.id;
5130 wine_rb_remove(&priv->program_lookup, &key);
5132 GL_EXTCALL(glDeleteProgram(entry->id));
5133 if (entry->vs.id)
5134 list_remove(&entry->vs.shader_entry);
5135 if (entry->gs.id)
5136 list_remove(&entry->gs.shader_entry);
5137 if (entry->ps.id)
5138 list_remove(&entry->ps.shader_entry);
5139 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
5140 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
5141 HeapFree(GetProcessHeap(), 0, entry);
5144 static void handle_ps3_input(struct shader_glsl_priv *priv,
5145 const struct wined3d_gl_info *gl_info, const DWORD *map,
5146 const struct wined3d_shader_signature *input_signature,
5147 const struct wined3d_shader_reg_maps *reg_maps_in,
5148 const struct wined3d_shader_signature *output_signature,
5149 const struct wined3d_shader_reg_maps *reg_maps_out)
5151 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5152 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5153 unsigned int i, j;
5154 DWORD *set;
5155 DWORD in_idx;
5156 unsigned int in_count = vec4_varyings(3, gl_info);
5157 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5158 char reg_mask[6];
5159 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5161 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
5163 for (i = 0; i < input_signature->element_count; ++i)
5165 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5167 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5168 continue;
5170 in_idx = map[input->register_idx];
5171 /* Declared, but not read register */
5172 if (in_idx == ~0u)
5173 continue;
5174 if (in_idx >= max_varyings)
5176 FIXME("More input varyings declared than supported, expect issues.\n");
5177 continue;
5180 if (in_idx == in_count)
5181 string_buffer_sprintf(destination, "gl_FrontColor");
5182 else if (in_idx == in_count + 1)
5183 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5184 else
5185 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5187 if (!set[in_idx])
5188 set[in_idx] = ~0u;
5190 for (j = 0; j < output_signature->element_count; ++j)
5192 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5193 DWORD mask;
5195 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5196 || input->semantic_idx != output->semantic_idx
5197 || strcmp(input->semantic_name, output->semantic_name)
5198 || !(mask = input->mask & output->mask))
5199 continue;
5201 if (set[in_idx] == ~0u)
5202 set[in_idx] = 0;
5203 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5204 shader_glsl_write_mask_to_str(mask, reg_mask);
5206 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5207 destination->buffer, reg_mask, output->register_idx, reg_mask);
5211 for (i = 0; i < max_varyings; ++i)
5213 unsigned int size;
5215 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5216 continue;
5218 if (set[i] == ~0U) set[i] = 0;
5220 size = 0;
5221 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5222 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5223 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5224 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5225 reg_mask[size] = '\0';
5227 if (i == in_count)
5228 string_buffer_sprintf(destination, "gl_FrontColor");
5229 else if (i == in_count + 1)
5230 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5231 else
5232 string_buffer_sprintf(destination, "ps_link[%u]", i);
5234 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5235 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5238 HeapFree(GetProcessHeap(), 0, set);
5239 string_buffer_release(&priv->string_buffers, destination);
5242 /* Context activation is done by the caller. */
5243 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5244 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5245 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5247 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5248 GLuint ret;
5249 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5250 unsigned int i;
5251 const char *semantic_name;
5252 UINT semantic_idx;
5253 char reg_mask[6];
5254 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5256 string_buffer_clear(buffer);
5258 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5260 if (per_vertex_point_size)
5262 shader_addline(buffer, "uniform struct\n{\n");
5263 shader_addline(buffer, " float size_min;\n");
5264 shader_addline(buffer, " float size_max;\n");
5265 shader_addline(buffer, "} ffp_point;\n");
5268 if (ps_major < 3)
5270 DWORD colors_written_mask[2] = {0};
5271 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5273 if (!legacy_context)
5275 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5276 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5277 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5278 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5281 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5283 for (i = 0; i < vs->output_signature.element_count; ++i)
5285 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5286 DWORD write_mask;
5288 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5289 continue;
5291 semantic_name = output->semantic_name;
5292 semantic_idx = output->semantic_idx;
5293 write_mask = output->mask;
5294 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5296 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5298 if (legacy_context)
5299 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5300 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5301 else
5302 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5303 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5305 colors_written_mask[semantic_idx] = write_mask;
5307 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5309 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5310 reg_mask, output->register_idx, reg_mask);
5312 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5314 if (semantic_idx < MAX_TEXTURES)
5316 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5317 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5318 semantic_idx, reg_mask, output->register_idx, reg_mask);
5319 texcoords_written_mask[semantic_idx] = write_mask;
5322 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5324 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5325 output->register_idx, reg_mask[1]);
5327 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5329 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5330 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5331 output->register_idx, reg_mask[1]);
5335 for (i = 0; i < 2; ++i)
5337 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5339 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5340 if (!i)
5341 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5342 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5343 reg_mask, reg_mask);
5344 else
5345 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5346 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5347 reg_mask, reg_mask);
5350 for (i = 0; i < MAX_TEXTURES; ++i)
5352 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5353 continue;
5355 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5357 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5358 && !texcoords_written_mask[i])
5359 continue;
5361 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5362 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5363 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5367 else
5369 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5371 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5372 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5374 /* First, sort out position and point size. Those are not passed to the pixel shader */
5375 for (i = 0; i < vs->output_signature.element_count; ++i)
5377 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5379 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5380 continue;
5382 semantic_name = output->semantic_name;
5383 semantic_idx = output->semantic_idx;
5384 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5386 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5388 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5389 reg_mask, output->register_idx, reg_mask);
5391 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5393 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5394 output->register_idx, reg_mask[1]);
5398 /* Then, fix the pixel shader input */
5399 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5400 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5403 shader_addline(buffer, "}\n");
5405 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5406 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5407 shader_glsl_compile(gl_info, ret, buffer->buffer);
5409 return ret;
5412 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
5413 const struct wined3d_gl_info *gl_info)
5415 const char *output = get_fragment_output(gl_info);
5417 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
5418 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5419 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
5420 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
5421 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
5422 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
5425 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
5426 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
5428 const char *output = get_fragment_output(gl_info);
5430 switch (mode)
5432 case WINED3D_FFP_PS_FOG_OFF:
5433 return;
5435 case WINED3D_FFP_PS_FOG_LINEAR:
5436 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5437 break;
5439 case WINED3D_FFP_PS_FOG_EXP:
5440 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5441 break;
5443 case WINED3D_FFP_PS_FOG_EXP2:
5444 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5445 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5446 break;
5448 default:
5449 ERR("Invalid fog mode %#x.\n", mode);
5450 return;
5453 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
5454 output, output);
5457 /* Context activation is done by the caller. */
5458 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5459 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5460 const struct wined3d_shader *shader,
5461 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5463 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5464 const struct wined3d_gl_info *gl_info = context->gl_info;
5465 const DWORD *function = shader->function;
5466 struct shader_glsl_ctx_priv priv_ctx;
5467 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5469 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5470 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5472 memset(&priv_ctx, 0, sizeof(priv_ctx));
5473 priv_ctx.cur_ps_args = args;
5474 priv_ctx.cur_np2fixup_info = np2fixup_info;
5475 priv_ctx.string_buffers = string_buffers;
5477 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5479 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5480 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5481 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5482 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5483 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5484 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5485 /* The spec says that it doesn't have to be explicitly enabled, but the
5486 * nvidia drivers write a warning if we don't do so. */
5487 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5488 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5489 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5490 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5491 if (gl_info->supported[EXT_GPU_SHADER4])
5492 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5494 /* Base Declarations */
5495 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5497 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5499 unsigned int i;
5500 WORD map = reg_maps->texcoord;
5502 if (legacy_context)
5504 if (glsl_is_color_reg_read(shader, 0))
5505 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5506 if (glsl_is_color_reg_read(shader, 1))
5507 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5510 for (i = 0; map; map >>= 1, ++i)
5512 if (map & 1)
5514 if (args->pointsprite)
5515 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5516 else if (args->texcoords_initialized & (1u << i))
5517 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5518 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5519 else
5520 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5521 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5525 if (legacy_context)
5526 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5529 /* Pack 3.0 inputs */
5530 if (reg_maps->shader_version.major >= 3)
5531 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5533 /* Base Shader Body */
5534 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5536 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5537 if (reg_maps->shader_version.major < 2)
5538 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
5540 if (args->srgb_correction)
5541 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
5543 /* SM < 3 does not replace the fog stage. */
5544 if (reg_maps->shader_version.major < 3)
5545 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
5547 shader_addline(buffer, "}\n");
5549 TRACE("Compiling shader object %u.\n", shader_id);
5550 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5552 return shader_id;
5555 /* Context activation is done by the caller. */
5556 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5557 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5558 const struct wined3d_shader *shader,
5559 const struct vs_compile_args *args)
5561 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5562 const struct wined3d_gl_info *gl_info = context->gl_info;
5563 const DWORD *function = shader->function;
5564 struct shader_glsl_ctx_priv priv_ctx;
5565 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5567 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5568 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5570 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5572 if (gl_info->supported[ARB_DRAW_INSTANCED])
5573 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5574 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5575 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5576 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5577 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5578 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5579 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5580 if (gl_info->supported[EXT_GPU_SHADER4])
5581 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5583 memset(&priv_ctx, 0, sizeof(priv_ctx));
5584 priv_ctx.cur_vs_args = args;
5585 priv_ctx.string_buffers = string_buffers;
5587 /* Base Declarations */
5588 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5590 /* Base Shader Body */
5591 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5593 /* Unpack outputs */
5594 shader_addline(buffer, "order_ps_input(vs_out);\n");
5596 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5597 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5598 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5599 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5601 if (reg_maps->shader_version.major < 3)
5603 if (args->fog_src == VS_FOG_Z)
5604 shader_addline(buffer, "%s = gl_Position.z;\n",
5605 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5606 else if (!reg_maps->fog)
5607 shader_addline(buffer, "%s = 0.0;\n",
5608 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5611 /* We always store the clipplanes without y inversion */
5612 if (args->clip_enabled)
5613 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5615 if (args->point_size && !args->per_vertex_point_size)
5616 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5618 /* Write the final position.
5620 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5621 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5622 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5623 * contains 1.0 to allow a mad.
5625 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5626 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5628 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5630 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5631 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5632 * which is the same as z = z * 2 - w.
5634 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5636 shader_addline(buffer, "}\n");
5638 TRACE("Compiling shader object %u.\n", shader_id);
5639 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5641 return shader_id;
5644 /* Context activation is done by the caller. */
5645 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5646 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5647 const struct wined3d_shader *shader)
5649 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5650 const struct wined3d_gl_info *gl_info = context->gl_info;
5651 const DWORD *function = shader->function;
5652 struct shader_glsl_ctx_priv priv_ctx;
5653 GLuint shader_id;
5655 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5657 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5659 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5660 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5661 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5662 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5663 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5664 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5665 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5666 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5667 if (gl_info->supported[EXT_GPU_SHADER4])
5668 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5670 memset(&priv_ctx, 0, sizeof(priv_ctx));
5671 priv_ctx.string_buffers = string_buffers;
5672 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5673 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5674 shader_addline(buffer, "}\n");
5676 TRACE("Compiling shader object %u.\n", shader_id);
5677 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5679 return shader_id;
5682 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5683 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5684 struct wined3d_shader *shader,
5685 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5687 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5688 struct glsl_shader_private *shader_data;
5689 struct ps_np2fixup_info *np2fixup;
5690 UINT i;
5691 DWORD new_size;
5692 GLuint ret;
5694 if (!shader->backend_data)
5696 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5697 if (!shader->backend_data)
5699 ERR("Failed to allocate backend data.\n");
5700 return 0;
5703 shader_data = shader->backend_data;
5704 gl_shaders = shader_data->gl_shaders.ps;
5706 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5707 * so a linear search is more performant than a hashmap or a binary search
5708 * (cache coherency etc)
5710 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5712 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5714 if (args->np2_fixup)
5715 *np2fixup_info = &gl_shaders[i].np2fixup;
5716 return gl_shaders[i].id;
5720 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5721 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5722 if (shader_data->num_gl_shaders)
5724 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5725 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5726 new_size * sizeof(*gl_shaders));
5728 else
5730 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5731 new_size = 1;
5734 if(!new_array) {
5735 ERR("Out of memory\n");
5736 return 0;
5738 shader_data->gl_shaders.ps = new_array;
5739 shader_data->shader_array_size = new_size;
5740 gl_shaders = new_array;
5743 gl_shaders[shader_data->num_gl_shaders].args = *args;
5745 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5746 memset(np2fixup, 0, sizeof(*np2fixup));
5747 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5749 pixelshader_update_resource_types(shader, args->tex_types);
5751 string_buffer_clear(buffer);
5752 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5753 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5755 return ret;
5758 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5759 const DWORD use_map)
5761 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5762 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5763 if (stored->point_size != new->point_size)
5764 return FALSE;
5765 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5766 return FALSE;
5767 if (stored->flatshading != new->flatshading)
5768 return FALSE;
5769 return stored->fog_src == new->fog_src;
5772 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5773 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5774 struct wined3d_shader *shader,
5775 const struct vs_compile_args *args)
5777 UINT i;
5778 DWORD new_size;
5779 DWORD use_map = context->stream_info.use_map;
5780 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5781 struct glsl_shader_private *shader_data;
5782 GLuint ret;
5784 if (!shader->backend_data)
5786 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5787 if (!shader->backend_data)
5789 ERR("Failed to allocate backend data.\n");
5790 return 0;
5793 shader_data = shader->backend_data;
5794 gl_shaders = shader_data->gl_shaders.vs;
5796 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5797 * so a linear search is more performant than a hashmap or a binary search
5798 * (cache coherency etc)
5800 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5802 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5803 return gl_shaders[i].id;
5806 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5808 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5809 if (shader_data->num_gl_shaders)
5811 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5812 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5813 new_size * sizeof(*gl_shaders));
5815 else
5817 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5818 new_size = 1;
5821 if(!new_array) {
5822 ERR("Out of memory\n");
5823 return 0;
5825 shader_data->gl_shaders.vs = new_array;
5826 shader_data->shader_array_size = new_size;
5827 gl_shaders = new_array;
5830 gl_shaders[shader_data->num_gl_shaders].args = *args;
5832 string_buffer_clear(buffer);
5833 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5834 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5836 return ret;
5839 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5840 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5841 struct wined3d_shader *shader)
5843 struct glsl_gs_compiled_shader *gl_shaders;
5844 struct glsl_shader_private *shader_data;
5845 GLuint ret;
5847 if (!shader->backend_data)
5849 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5851 ERR("Failed to allocate backend data.\n");
5852 return 0;
5855 shader_data = shader->backend_data;
5856 gl_shaders = shader_data->gl_shaders.gs;
5858 if (shader_data->num_gl_shaders)
5859 return gl_shaders[0].id;
5861 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5863 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5865 ERR("Failed to allocate GL shader array.\n");
5866 return 0;
5868 shader_data->shader_array_size = 1;
5869 gl_shaders = shader_data->gl_shaders.gs;
5871 string_buffer_clear(buffer);
5872 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5873 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5875 return ret;
5878 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5880 switch (mcs)
5882 case WINED3D_MCS_MATERIAL:
5883 return material;
5884 case WINED3D_MCS_COLOR1:
5885 return "ffp_attrib_diffuse";
5886 case WINED3D_MCS_COLOR2:
5887 return "ffp_attrib_specular";
5888 default:
5889 ERR("Invalid material color source %#x.\n", mcs);
5890 return "<invalid>";
5894 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5895 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5897 const char *diffuse, *specular, *emissive, *ambient;
5898 enum wined3d_light_type light_type;
5899 unsigned int i;
5901 if (!settings->lighting)
5903 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5904 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5905 return;
5908 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5909 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5910 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5911 shader_addline(buffer, "vec3 dir, dst;\n");
5912 shader_addline(buffer, "float att, t;\n");
5914 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5915 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5916 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5917 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5919 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5921 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5922 switch (light_type)
5924 case WINED3D_LIGHT_POINT:
5925 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5926 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5927 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5928 shader_addline(buffer, "dst.x = 1.0;\n");
5929 if (legacy_lighting)
5931 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5932 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5934 else
5936 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5938 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5939 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5940 if (!legacy_lighting)
5941 shader_addline(buffer, "att = 1.0 / att;\n");
5942 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5943 if (!settings->normal)
5945 if (!legacy_lighting)
5946 shader_addline(buffer, "}\n");
5947 break;
5949 shader_addline(buffer, "dir = normalize(dir);\n");
5950 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5951 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5952 if (settings->localviewer)
5953 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5954 else
5955 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5956 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5957 " * ffp_light[%u].specular) * att;\n", i);
5958 if (!legacy_lighting)
5959 shader_addline(buffer, "}\n");
5960 break;
5962 case WINED3D_LIGHT_SPOT:
5963 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5964 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5965 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5966 shader_addline(buffer, "dst.x = 1.0;\n");
5967 if (legacy_lighting)
5969 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5970 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5972 else
5974 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5976 shader_addline(buffer, "dir = normalize(dir);\n");
5977 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5978 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5979 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5980 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5981 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5982 i, i, i, i);
5983 if (legacy_lighting)
5984 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5985 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5986 i, i, i);
5987 else
5988 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5989 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5990 i, i, i);
5991 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5992 if (!settings->normal)
5994 if (!legacy_lighting)
5995 shader_addline(buffer, "}\n");
5996 break;
5998 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5999 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
6000 if (settings->localviewer)
6001 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6002 else
6003 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6004 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6005 " * ffp_light[%u].specular) * att;\n", i);
6006 if (!legacy_lighting)
6007 shader_addline(buffer, "}\n");
6008 break;
6010 case WINED3D_LIGHT_DIRECTIONAL:
6011 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6012 if (!settings->normal)
6013 break;
6014 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
6015 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6016 " * ffp_light[%u].diffuse.xyz;\n", i);
6017 /* TODO: In the non-local viewer case the halfvector is constant
6018 * and could be precomputed and stored in a uniform. */
6019 if (settings->localviewer)
6020 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6021 else
6022 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6023 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6024 " * ffp_light[%u].specular;\n", i);
6025 break;
6027 case WINED3D_LIGHT_PARALLELPOINT:
6028 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
6029 if (!settings->normal)
6030 break;
6031 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
6032 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6033 " * ffp_light[%u].diffuse.xyz;\n", i);
6034 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6035 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6036 " * ffp_light[%u].specular;\n", i);
6037 break;
6039 default:
6040 if (light_type)
6041 FIXME("Unhandled light type %#x.\n", light_type);
6042 continue;
6046 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6047 ambient, diffuse, emissive);
6048 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6049 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6052 /* Context activation is done by the caller. */
6053 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6054 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6056 static const struct attrib_info
6058 const char type[6];
6059 const char name[24];
6061 attrib_info[] =
6063 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6064 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6065 /* TODO: Indexed vertex blending */
6066 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6067 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6068 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6069 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6070 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6072 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6073 BOOL legacy_lighting = priv->legacy_lighting;
6074 GLuint shader_obj;
6075 unsigned int i;
6076 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6077 BOOL output_legacy_fogcoord = legacy_context;
6079 string_buffer_clear(buffer);
6081 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6083 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6085 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6087 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6089 shader_addline(buffer, "\n");
6091 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6092 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6093 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6094 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6096 shader_addline(buffer, "uniform struct\n{\n");
6097 shader_addline(buffer, " vec4 emissive;\n");
6098 shader_addline(buffer, " vec4 ambient;\n");
6099 shader_addline(buffer, " vec4 diffuse;\n");
6100 shader_addline(buffer, " vec4 specular;\n");
6101 shader_addline(buffer, " float shininess;\n");
6102 shader_addline(buffer, "} ffp_material;\n");
6104 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6105 shader_addline(buffer, "uniform struct\n{\n");
6106 shader_addline(buffer, " vec4 diffuse;\n");
6107 shader_addline(buffer, " vec4 specular;\n");
6108 shader_addline(buffer, " vec4 ambient;\n");
6109 shader_addline(buffer, " vec4 position;\n");
6110 shader_addline(buffer, " vec3 direction;\n");
6111 shader_addline(buffer, " float range;\n");
6112 shader_addline(buffer, " float falloff;\n");
6113 shader_addline(buffer, " float c_att;\n");
6114 shader_addline(buffer, " float l_att;\n");
6115 shader_addline(buffer, " float q_att;\n");
6116 shader_addline(buffer, " float cos_htheta;\n");
6117 shader_addline(buffer, " float cos_hphi;\n");
6118 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6120 if (settings->point_size)
6122 shader_addline(buffer, "uniform struct\n{\n");
6123 shader_addline(buffer, " float size;\n");
6124 shader_addline(buffer, " float size_min;\n");
6125 shader_addline(buffer, " float size_max;\n");
6126 shader_addline(buffer, " float c_att;\n");
6127 shader_addline(buffer, " float l_att;\n");
6128 shader_addline(buffer, " float q_att;\n");
6129 shader_addline(buffer, "} ffp_point;\n");
6132 if (legacy_context)
6134 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6135 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6136 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6137 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6139 else
6141 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6142 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6143 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6144 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6147 shader_addline(buffer, "\nvoid main()\n{\n");
6148 shader_addline(buffer, "float m;\n");
6149 shader_addline(buffer, "vec3 r;\n");
6151 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6153 if (attrib_info[i].name[0])
6154 shader_addline(buffer, "%s %s = vs_in%u;\n",
6155 attrib_info[i].type, attrib_info[i].name, i);
6157 for (i = 0; i < MAX_TEXTURES; ++i)
6159 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6160 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6161 && settings->texcoords & (1u << i))
6162 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6165 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6167 if (settings->transformed)
6169 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6170 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6171 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6173 else
6175 for (i = 0; i < settings->vertexblends; ++i)
6176 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6178 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6179 for (i = 0; i < settings->vertexblends + 1; ++i)
6180 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6182 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6183 if (settings->clipping)
6184 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6185 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6188 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6189 if (settings->normal)
6191 if (!settings->vertexblends)
6193 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6195 else
6197 for (i = 0; i < settings->vertexblends + 1; ++i)
6198 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6201 if (settings->normalize)
6202 shader_addline(buffer, "normal = normalize(normal);\n");
6205 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6206 if (legacy_context)
6208 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6209 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6211 else
6213 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
6214 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
6217 for (i = 0; i < MAX_TEXTURES; ++i)
6219 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6221 switch (settings->texgen[i] & 0xffff0000)
6223 case WINED3DTSS_TCI_PASSTHRU:
6224 if (settings->texcoords & (1u << i))
6225 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6226 i, i, i);
6227 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6228 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6229 else
6230 output_legacy_texcoord = FALSE;
6231 break;
6233 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6234 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6235 break;
6237 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6238 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6239 break;
6241 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6242 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6243 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6244 break;
6246 case WINED3DTSS_TCI_SPHEREMAP:
6247 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6248 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6249 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6250 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6251 break;
6253 default:
6254 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6255 break;
6257 if (output_legacy_texcoord)
6258 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6261 switch (settings->fog_mode)
6263 case WINED3D_FFP_VS_FOG_OFF:
6264 output_legacy_fogcoord = FALSE;
6265 break;
6267 case WINED3D_FFP_VS_FOG_FOGCOORD:
6268 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6269 break;
6271 case WINED3D_FFP_VS_FOG_RANGE:
6272 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6273 break;
6275 case WINED3D_FFP_VS_FOG_DEPTH:
6276 if (settings->ortho_fog)
6277 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6278 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6279 else if (settings->transformed)
6280 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6281 else
6282 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6283 break;
6285 default:
6286 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6287 break;
6289 if (output_legacy_fogcoord)
6290 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6292 if (settings->point_size)
6294 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6295 " + ffp_point.l_att * length(ec_pos.xyz)"
6296 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6297 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6298 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6301 shader_addline(buffer, "}\n");
6303 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6304 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6306 return shader_obj;
6309 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6310 DWORD argnum, unsigned int stage, DWORD arg)
6312 const char *ret;
6314 if (arg == ARG_UNUSED)
6315 return "<unused arg>";
6317 switch (arg & WINED3DTA_SELECTMASK)
6319 case WINED3DTA_DIFFUSE:
6320 ret = "ffp_varying_diffuse";
6321 break;
6323 case WINED3DTA_CURRENT:
6324 ret = "ret";
6325 break;
6327 case WINED3DTA_TEXTURE:
6328 switch (stage)
6330 case 0: ret = "tex0"; break;
6331 case 1: ret = "tex1"; break;
6332 case 2: ret = "tex2"; break;
6333 case 3: ret = "tex3"; break;
6334 case 4: ret = "tex4"; break;
6335 case 5: ret = "tex5"; break;
6336 case 6: ret = "tex6"; break;
6337 case 7: ret = "tex7"; break;
6338 default:
6339 ret = "<invalid texture>";
6340 break;
6342 break;
6344 case WINED3DTA_TFACTOR:
6345 ret = "tex_factor";
6346 break;
6348 case WINED3DTA_SPECULAR:
6349 ret = "ffp_varying_specular";
6350 break;
6352 case WINED3DTA_TEMP:
6353 ret = "temp_reg";
6354 break;
6356 case WINED3DTA_CONSTANT:
6357 switch (stage)
6359 case 0: ret = "tss_const0"; break;
6360 case 1: ret = "tss_const1"; break;
6361 case 2: ret = "tss_const2"; break;
6362 case 3: ret = "tss_const3"; break;
6363 case 4: ret = "tss_const4"; break;
6364 case 5: ret = "tss_const5"; break;
6365 case 6: ret = "tss_const6"; break;
6366 case 7: ret = "tss_const7"; break;
6367 default:
6368 ret = "<invalid constant>";
6369 break;
6371 break;
6373 default:
6374 return "<unhandled arg>";
6377 if (arg & WINED3DTA_COMPLEMENT)
6379 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6380 if (argnum == 0)
6381 ret = "arg0";
6382 else if (argnum == 1)
6383 ret = "arg1";
6384 else if (argnum == 2)
6385 ret = "arg2";
6388 if (arg & WINED3DTA_ALPHAREPLICATE)
6390 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6391 if (argnum == 0)
6392 ret = "arg0";
6393 else if (argnum == 1)
6394 ret = "arg1";
6395 else if (argnum == 2)
6396 ret = "arg2";
6399 return ret;
6402 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6403 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6405 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6407 if (color && alpha)
6408 dstmask = "";
6409 else if (color)
6410 dstmask = ".xyz";
6411 else
6412 dstmask = ".w";
6414 if (dst == tempreg)
6415 dstreg = "temp_reg";
6416 else
6417 dstreg = "ret";
6419 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6420 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6421 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6423 switch (op)
6425 case WINED3D_TOP_DISABLE:
6426 break;
6428 case WINED3D_TOP_SELECT_ARG1:
6429 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6430 break;
6432 case WINED3D_TOP_SELECT_ARG2:
6433 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6434 break;
6436 case WINED3D_TOP_MODULATE:
6437 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6438 break;
6440 case WINED3D_TOP_MODULATE_4X:
6441 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6442 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6443 break;
6445 case WINED3D_TOP_MODULATE_2X:
6446 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6447 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6448 break;
6450 case WINED3D_TOP_ADD:
6451 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6452 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6453 break;
6455 case WINED3D_TOP_ADD_SIGNED:
6456 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6457 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6458 break;
6460 case WINED3D_TOP_ADD_SIGNED_2X:
6461 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6462 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6463 break;
6465 case WINED3D_TOP_SUBTRACT:
6466 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6467 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6468 break;
6470 case WINED3D_TOP_ADD_SMOOTH:
6471 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6472 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6473 break;
6475 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6476 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6477 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6478 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6479 break;
6481 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6482 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6483 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6484 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6485 break;
6487 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6488 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6489 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6490 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6491 break;
6493 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6494 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6495 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6496 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6497 break;
6499 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6500 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6501 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6502 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6503 break;
6505 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6506 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6507 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6508 break;
6510 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6511 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6512 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6513 break;
6515 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6516 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6517 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6518 break;
6519 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6520 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6521 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6522 break;
6524 case WINED3D_TOP_BUMPENVMAP:
6525 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6526 /* These are handled in the first pass, nothing to do. */
6527 break;
6529 case WINED3D_TOP_DOTPRODUCT3:
6530 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6531 dstreg, dstmask, arg1, arg2, dstmask);
6532 break;
6534 case WINED3D_TOP_MULTIPLY_ADD:
6535 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6536 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6537 break;
6539 case WINED3D_TOP_LERP:
6540 /* MSDN isn't quite right here. */
6541 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6542 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6543 break;
6545 default:
6546 FIXME("Unhandled operation %#x.\n", op);
6547 break;
6551 /* Context activation is done by the caller. */
6552 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6553 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6555 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6556 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6557 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6558 UINT lowest_disabled_stage;
6559 GLuint shader_id;
6560 DWORD arg0, arg1, arg2;
6561 unsigned int stage;
6562 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6563 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6565 string_buffer_clear(buffer);
6567 /* Find out which textures are read */
6568 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6570 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6571 break;
6573 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6574 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6575 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6577 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6578 || (stage == 0 && settings->color_key_enabled))
6579 tex_map |= 1u << stage;
6580 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6581 tfactor_used = TRUE;
6582 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6583 tempreg_used = TRUE;
6584 if (settings->op[stage].dst == tempreg)
6585 tempreg_used = TRUE;
6586 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6587 tss_const_map |= 1u << stage;
6589 switch (settings->op[stage].cop)
6591 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6592 lum_map |= 1u << stage;
6593 /* fall through */
6594 case WINED3D_TOP_BUMPENVMAP:
6595 bump_map |= 1u << stage;
6596 /* fall through */
6597 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6598 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6599 tex_map |= 1u << stage;
6600 break;
6602 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6603 tfactor_used = TRUE;
6604 break;
6606 default:
6607 break;
6610 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6611 continue;
6613 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6614 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6615 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6617 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6618 tex_map |= 1u << stage;
6619 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6620 tfactor_used = TRUE;
6621 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6622 tempreg_used = TRUE;
6623 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6624 tss_const_map |= 1u << stage;
6626 lowest_disabled_stage = stage;
6628 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6630 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6631 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6633 if (!needs_legacy_glsl_syntax(gl_info))
6634 shader_addline(buffer, "out vec4 ps_out[1];\n");
6636 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6637 shader_addline(buffer, "vec4 ret;\n");
6638 if (tempreg_used || settings->sRGB_write)
6639 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6640 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6642 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6644 if (tss_const_map & (1u << stage))
6645 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6647 if (!(tex_map & (1u << stage)))
6648 continue;
6650 switch (settings->op[stage].tex_type)
6652 case WINED3D_GL_RES_TYPE_TEX_1D:
6653 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6654 break;
6655 case WINED3D_GL_RES_TYPE_TEX_2D:
6656 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6657 break;
6658 case WINED3D_GL_RES_TYPE_TEX_3D:
6659 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6660 break;
6661 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6662 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6663 break;
6664 case WINED3D_GL_RES_TYPE_TEX_RECT:
6665 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6666 break;
6667 default:
6668 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6669 break;
6672 shader_addline(buffer, "vec4 tex%u;\n", stage);
6674 if (!(bump_map & (1u << stage)))
6675 continue;
6676 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6678 if (!(lum_map & (1u << stage)))
6679 continue;
6680 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6681 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6683 if (tfactor_used)
6684 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6685 if (settings->color_key_enabled)
6686 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6687 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6689 if (settings->sRGB_write)
6691 shader_addline(buffer, "const vec4 srgb_const0 = ");
6692 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6693 shader_addline(buffer, ";\n");
6694 shader_addline(buffer, "const vec4 srgb_const1 = ");
6695 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6696 shader_addline(buffer, ";\n");
6699 shader_addline(buffer, "uniform struct\n{\n");
6700 shader_addline(buffer, " vec4 color;\n");
6701 shader_addline(buffer, " float density;\n");
6702 shader_addline(buffer, " float end;\n");
6703 shader_addline(buffer, " float scale;\n");
6704 shader_addline(buffer, "} ffp_fog;\n");
6706 if (legacy_context)
6708 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6709 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6710 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6711 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6712 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6714 else
6716 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6717 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6718 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6719 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6720 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6723 shader_addline(buffer, "void main()\n{\n");
6725 if (legacy_context)
6727 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6728 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6731 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6733 if (tex_map & (1u << stage))
6735 if (settings->pointsprite)
6736 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6737 else if (settings->texcoords_initialized & (1u << stage))
6738 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6739 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6740 else
6741 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6745 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6746 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6748 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6749 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6751 /* Generate texture sampling instructions */
6752 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6754 const char *texture_function, *coord_mask;
6755 BOOL proj;
6757 if (!(tex_map & (1u << stage)))
6758 continue;
6760 if (settings->op[stage].projected == proj_none)
6762 proj = FALSE;
6764 else if (settings->op[stage].projected == proj_count4
6765 || settings->op[stage].projected == proj_count3)
6767 proj = TRUE;
6769 else
6771 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6772 proj = TRUE;
6775 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6776 proj = FALSE;
6778 switch (settings->op[stage].tex_type)
6780 case WINED3D_GL_RES_TYPE_TEX_1D:
6781 if (proj)
6783 texture_function = "texture1DProj";
6784 coord_mask = "xw";
6786 else
6788 texture_function = "texture1D";
6789 coord_mask = "x";
6791 break;
6792 case WINED3D_GL_RES_TYPE_TEX_2D:
6793 if (proj)
6795 texture_function = "texture2DProj";
6796 coord_mask = "xyw";
6798 else
6800 texture_function = "texture2D";
6801 coord_mask = "xy";
6803 break;
6804 case WINED3D_GL_RES_TYPE_TEX_3D:
6805 if (proj)
6807 texture_function = "texture3DProj";
6808 coord_mask = "xyzw";
6810 else
6812 texture_function = "texture3D";
6813 coord_mask = "xyz";
6815 break;
6816 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6817 texture_function = "textureCube";
6818 coord_mask = "xyz";
6819 break;
6820 case WINED3D_GL_RES_TYPE_TEX_RECT:
6821 if (proj)
6823 texture_function = "texture2DRectProj";
6824 coord_mask = "xyw";
6826 else
6828 texture_function = "texture2DRect";
6829 coord_mask = "xy";
6831 break;
6832 default:
6833 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6834 texture_function = "";
6835 coord_mask = "xyzw";
6836 break;
6838 if (!needs_legacy_glsl_syntax(gl_info))
6839 texture_function = proj ? "textureProj" : "texture";
6841 if (stage > 0
6842 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6843 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6845 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6847 /* With projective textures, texbem only divides the static
6848 * texture coord, not the displacement, so multiply the
6849 * displacement with the dividing parameter before passing it to
6850 * TXP. */
6851 if (settings->op[stage].projected != proj_none)
6853 if (settings->op[stage].projected == proj_count4)
6855 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6856 stage, stage);
6857 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6859 else
6861 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6862 stage, stage);
6863 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6866 else
6868 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6871 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6872 stage, texture_function, stage, coord_mask);
6874 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6875 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6876 stage, stage - 1, stage - 1, stage - 1);
6878 else if (settings->op[stage].projected == proj_count3)
6880 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6881 stage, texture_function, stage, stage);
6883 else
6885 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6886 stage, texture_function, stage, stage, coord_mask);
6889 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6890 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6891 settings->op[stage].color_fixup);
6894 if (settings->color_key_enabled)
6896 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6897 shader_addline(buffer, " discard;\n");
6900 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6902 /* Generate the main shader */
6903 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6905 BOOL op_equal;
6907 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6908 break;
6910 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6911 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6912 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6913 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6914 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6915 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6916 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6917 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6918 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6919 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6920 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6921 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6922 else
6923 op_equal = settings->op[stage].aop == settings->op[stage].cop
6924 && settings->op[stage].carg0 == settings->op[stage].aarg0
6925 && settings->op[stage].carg1 == settings->op[stage].aarg1
6926 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6928 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6930 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6931 settings->op[stage].cop, settings->op[stage].carg0,
6932 settings->op[stage].carg1, settings->op[stage].carg2);
6934 else if (op_equal)
6936 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6937 settings->op[stage].cop, settings->op[stage].carg0,
6938 settings->op[stage].carg1, settings->op[stage].carg2);
6940 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6941 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6943 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6944 settings->op[stage].cop, settings->op[stage].carg0,
6945 settings->op[stage].carg1, settings->op[stage].carg2);
6946 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6947 settings->op[stage].aop, settings->op[stage].aarg0,
6948 settings->op[stage].aarg1, settings->op[stage].aarg2);
6952 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
6953 get_fragment_output(gl_info));
6955 if (settings->sRGB_write)
6956 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6958 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
6960 shader_addline(buffer, "}\n");
6962 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6963 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6965 string_buffer_release(&priv->string_buffers, tex_reg_name);
6966 return shader_id;
6969 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6970 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6972 struct glsl_ffp_vertex_shader *shader;
6973 const struct wine_rb_entry *entry;
6975 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6976 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6978 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6979 return NULL;
6981 shader->desc.settings = *settings;
6982 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6983 list_init(&shader->linked_programs);
6984 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6985 ERR("Failed to insert ffp vertex shader.\n");
6987 return shader;
6990 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6991 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6993 struct glsl_ffp_fragment_shader *glsl_desc;
6994 const struct ffp_frag_desc *desc;
6996 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6997 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6999 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7000 return NULL;
7002 glsl_desc->entry.settings = *args;
7003 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7004 list_init(&glsl_desc->linked_programs);
7005 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7007 return glsl_desc;
7011 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7012 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7014 unsigned int i;
7015 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7017 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7018 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
7019 for (i = 0; i < vs_c_count; ++i)
7021 string_buffer_sprintf(name, "vs_c[%u]", i);
7022 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7024 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
7025 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
7027 for (i = 0; i < MAX_CONST_I; ++i)
7029 string_buffer_sprintf(name, "vs_i[%u]", i);
7030 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7033 for (i = 0; i < MAX_CONST_B; ++i)
7035 string_buffer_sprintf(name, "vs_b[%u]", i);
7036 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7039 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
7041 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7043 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7044 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7046 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7047 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7048 for (i = 0; i < MAX_TEXTURES; ++i)
7050 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7051 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7053 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7054 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7055 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7056 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7057 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7058 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7059 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7061 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7062 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7063 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7064 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7065 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7066 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7067 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7068 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7069 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7070 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7071 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7072 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7073 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7074 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7075 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7076 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7077 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7078 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7079 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7080 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7081 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7082 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7083 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7084 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7086 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7087 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7088 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7089 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7090 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7091 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7093 string_buffer_release(&priv->string_buffers, name);
7096 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7097 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7099 unsigned int i;
7100 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7102 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
7103 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
7104 for (i = 0; i < ps_c_count; ++i)
7106 string_buffer_sprintf(name, "ps_c[%u]", i);
7107 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7109 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
7110 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
7112 for (i = 0; i < MAX_CONST_I; ++i)
7114 string_buffer_sprintf(name, "ps_i[%u]", i);
7115 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7118 for (i = 0; i < MAX_CONST_B; ++i)
7120 string_buffer_sprintf(name, "ps_b[%u]", i);
7121 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7124 for (i = 0; i < MAX_TEXTURES; ++i)
7126 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7127 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7128 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7129 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7130 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7131 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7132 string_buffer_sprintf(name, "tss_const%u", i);
7133 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7136 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7137 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7139 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7140 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7141 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7142 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7144 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7145 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7146 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7148 string_buffer_release(&priv->string_buffers, name);
7151 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7152 struct shader_glsl_priv *priv, GLuint program_id,
7153 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
7155 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7156 GLuint block_idx;
7157 unsigned int i;
7158 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7160 for (i = 0; i < count; ++i)
7162 if (!reg_maps->cb_sizes[i])
7163 continue;
7165 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7166 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7167 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
7169 checkGLcall("glUniformBlockBinding");
7170 string_buffer_release(&priv->string_buffers, name);
7173 /* Context activation is done by the caller. */
7174 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
7175 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
7177 const struct wined3d_gl_info *gl_info = context->gl_info;
7178 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
7179 const struct ps_np2fixup_info *np2fixup_info = NULL;
7180 struct glsl_shader_prog_link *entry = NULL;
7181 struct wined3d_shader *vshader = NULL;
7182 struct wined3d_shader *gshader = NULL;
7183 struct wined3d_shader *pshader = NULL;
7184 GLuint program_id;
7185 GLuint reorder_shader_id = 0;
7186 unsigned int i;
7187 GLuint vs_id = 0;
7188 GLuint gs_id = 0;
7189 GLuint ps_id = 0;
7190 struct list *ps_list, *vs_list;
7191 WORD attribs_map;
7192 struct wined3d_string_buffer *tmp_name;
7194 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7196 vs_id = ctx_data->glsl_program->vs.id;
7197 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7199 if (use_vs(state))
7201 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7202 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7204 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7205 && ctx_data->glsl_program->gs.id)
7206 gs_id = ctx_data->glsl_program->gs.id;
7207 else if (gshader)
7208 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7211 else if (use_vs(state))
7213 struct vs_compile_args vs_compile_args;
7215 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7217 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7218 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7219 vs_list = &vshader->linked_programs;
7221 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7222 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7224 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7226 struct glsl_ffp_vertex_shader *ffp_shader;
7227 struct wined3d_ffp_vs_settings settings;
7229 wined3d_ffp_get_vs_settings(context, state, &settings);
7230 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7231 vs_id = ffp_shader->id;
7232 vs_list = &ffp_shader->linked_programs;
7235 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7237 ps_id = ctx_data->glsl_program->ps.id;
7238 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7240 if (use_ps(state))
7241 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7243 else if (use_ps(state))
7245 struct ps_compile_args ps_compile_args;
7246 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7247 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7248 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7249 pshader, &ps_compile_args, &np2fixup_info);
7250 ps_list = &pshader->linked_programs;
7252 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7254 struct glsl_ffp_fragment_shader *ffp_shader;
7255 struct ffp_frag_settings settings;
7257 gen_ffp_frag_op(context, state, &settings, FALSE);
7258 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7259 ps_id = ffp_shader->id;
7260 ps_list = &ffp_shader->linked_programs;
7263 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7265 ctx_data->glsl_program = entry;
7266 return;
7269 /* If we get to this point, then no matching program exists, so we create one */
7270 program_id = GL_EXTCALL(glCreateProgram());
7271 TRACE("Created new GLSL shader program %u.\n", program_id);
7273 /* Create the entry */
7274 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7275 entry->id = program_id;
7276 entry->vs.id = vs_id;
7277 entry->gs.id = gs_id;
7278 entry->ps.id = ps_id;
7279 entry->constant_version = 0;
7280 entry->ps.np2_fixup_info = np2fixup_info;
7281 /* Add the hash table entry */
7282 add_glsl_program_entry(priv, entry);
7284 /* Set the current program */
7285 ctx_data->glsl_program = entry;
7287 /* Attach GLSL vshader */
7288 if (vs_id)
7290 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7291 GL_EXTCALL(glAttachShader(program_id, vs_id));
7292 checkGLcall("glAttachShader");
7294 list_add_head(vs_list, &entry->vs.shader_entry);
7297 if (vshader)
7299 attribs_map = vshader->reg_maps.input_registers;
7300 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7301 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7302 d3d_info->emulated_flatshading
7303 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7304 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7305 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7306 checkGLcall("glAttachShader");
7307 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7308 * is destroyed
7310 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7312 else
7314 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7317 /* Bind vertex attributes to a corresponding index number to match
7318 * the same index numbers as ARB_vertex_programs (makes loading
7319 * vertex attributes simpler). With this method, we can use the
7320 * exact same code to load the attributes later for both ARB and
7321 * GLSL shaders.
7323 * We have to do this here because we need to know the Program ID
7324 * in order to make the bindings work, and it has to be done prior
7325 * to linking the GLSL program. */
7326 tmp_name = string_buffer_get(&priv->string_buffers);
7327 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7329 if (!(attribs_map & 1))
7330 continue;
7332 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7333 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7335 checkGLcall("glBindAttribLocation");
7336 string_buffer_release(&priv->string_buffers, tmp_name);
7338 if (gshader)
7340 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7341 GL_EXTCALL(glAttachShader(program_id, gs_id));
7342 checkGLcall("glAttachShader");
7344 TRACE("input type %s, output type %s, vertices out %u.\n",
7345 debug_d3dprimitivetype(gshader->u.gs.input_type),
7346 debug_d3dprimitivetype(gshader->u.gs.output_type),
7347 gshader->u.gs.vertices_out);
7348 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7349 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7350 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7351 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7352 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7353 gshader->u.gs.vertices_out));
7354 checkGLcall("glProgramParameteriARB");
7356 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7359 /* Attach GLSL pshader */
7360 if (ps_id)
7362 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7363 GL_EXTCALL(glAttachShader(program_id, ps_id));
7364 checkGLcall("glAttachShader");
7366 list_add_head(ps_list, &entry->ps.shader_entry);
7369 /* Link the program */
7370 TRACE("Linking GLSL shader program %u.\n", program_id);
7371 GL_EXTCALL(glLinkProgram(program_id));
7372 shader_glsl_validate_link(gl_info, program_id);
7374 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7375 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7376 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7377 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7378 checkGLcall("Find glsl program uniform locations");
7380 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7382 if (pshader && pshader->reg_maps.shader_version.major >= 3
7383 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7385 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7386 entry->vs.vertex_color_clamp = GL_FALSE;
7388 else
7390 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7393 else
7395 /* With core profile we never change vertex_color_clamp from
7396 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
7397 * glClampColorARB(). */
7398 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7401 /* Set the shader to allow uniform loading on it */
7402 GL_EXTCALL(glUseProgram(program_id));
7403 checkGLcall("glUseProgram");
7405 /* Texture unit mapping is set up to be the same each time the shader
7406 * program is used so we can hardcode the sampler uniform values. */
7407 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7409 entry->constant_update_mask = 0;
7410 if (vshader)
7412 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7413 if (vshader->reg_maps.integer_constants)
7414 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7415 if (vshader->reg_maps.boolean_constants)
7416 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7417 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7419 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7420 0, gl_info->limits.vertex_uniform_blocks);
7422 else
7424 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7425 | WINED3D_SHADER_CONST_FFP_PROJ;
7427 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7429 if (entry->vs.modelview_matrix_location[i] != -1)
7431 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7432 break;
7436 for (i = 0; i < MAX_TEXTURES; ++i)
7438 if (entry->vs.texture_matrix_location[i] != -1)
7440 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7441 break;
7444 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7445 || entry->vs.material_specular_location != -1
7446 || entry->vs.material_emissive_location != -1
7447 || entry->vs.material_shininess_location != -1)
7448 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7449 if (entry->vs.light_ambient_location != -1)
7450 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7452 if (entry->vs.pointsize_min_location != -1)
7453 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7455 if (gshader)
7456 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7457 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7459 if (ps_id)
7461 if (pshader)
7463 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7464 if (pshader->reg_maps.integer_constants)
7465 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7466 if (pshader->reg_maps.boolean_constants)
7467 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7468 if (entry->ps.ycorrection_location != -1)
7469 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7471 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7472 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7473 gl_info->limits.fragment_uniform_blocks);
7475 else
7477 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7480 for (i = 0; i < MAX_TEXTURES; ++i)
7482 if (entry->ps.bumpenv_mat_location[i] != -1)
7484 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7485 break;
7489 if (entry->ps.fog_color_location != -1)
7490 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7491 if (entry->ps.np2_fixup_location != -1)
7492 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7493 if (entry->ps.color_key_location != -1)
7494 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7498 /* Context activation is done by the caller. */
7499 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7500 BOOL masked)
7502 GLuint program_id;
7503 GLuint vshader_id, pshader_id;
7504 const char *blt_pshader;
7506 static const char blt_vshader[] =
7507 "#version 120\n"
7508 "void main(void)\n"
7509 "{\n"
7510 " gl_Position = gl_Vertex;\n"
7511 " gl_FrontColor = vec4(1.0);\n"
7512 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7513 "}\n";
7515 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7517 /* WINED3D_GL_RES_TYPE_TEX_1D */
7518 NULL,
7519 /* WINED3D_GL_RES_TYPE_TEX_2D */
7520 "#version 120\n"
7521 "uniform sampler2D sampler;\n"
7522 "void main(void)\n"
7523 "{\n"
7524 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7525 "}\n",
7526 /* WINED3D_GL_RES_TYPE_TEX_3D */
7527 NULL,
7528 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7529 "#version 120\n"
7530 "uniform samplerCube sampler;\n"
7531 "void main(void)\n"
7532 "{\n"
7533 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7534 "}\n",
7535 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7536 "#version 120\n"
7537 "#extension GL_ARB_texture_rectangle : enable\n"
7538 "uniform sampler2DRect sampler;\n"
7539 "void main(void)\n"
7540 "{\n"
7541 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7542 "}\n",
7543 /* WINED3D_GL_RES_TYPE_BUFFER */
7544 NULL,
7545 /* WINED3D_GL_RES_TYPE_RB */
7546 NULL,
7549 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7551 /* WINED3D_GL_RES_TYPE_TEX_1D */
7552 NULL,
7553 /* WINED3D_GL_RES_TYPE_TEX_2D */
7554 "#version 120\n"
7555 "uniform sampler2D sampler;\n"
7556 "uniform vec4 mask;\n"
7557 "void main(void)\n"
7558 "{\n"
7559 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7560 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7561 "}\n",
7562 /* WINED3D_GL_RES_TYPE_TEX_3D */
7563 NULL,
7564 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7565 "#version 120\n"
7566 "uniform samplerCube sampler;\n"
7567 "uniform vec4 mask;\n"
7568 "void main(void)\n"
7569 "{\n"
7570 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7571 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7572 "}\n",
7573 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7574 "#version 120\n"
7575 "#extension GL_ARB_texture_rectangle : enable\n"
7576 "uniform sampler2DRect sampler;\n"
7577 "uniform vec4 mask;\n"
7578 "void main(void)\n"
7579 "{\n"
7580 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7581 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7582 "}\n",
7583 /* WINED3D_GL_RES_TYPE_BUFFER */
7584 NULL,
7585 /* WINED3D_GL_RES_TYPE_RB */
7586 NULL,
7589 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7590 if (!blt_pshader)
7592 FIXME("tex_type %#x not supported\n", tex_type);
7593 return 0;
7596 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7597 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7599 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7600 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7602 program_id = GL_EXTCALL(glCreateProgram());
7603 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7604 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7605 GL_EXTCALL(glLinkProgram(program_id));
7607 shader_glsl_validate_link(gl_info, program_id);
7609 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7610 * is destroyed
7612 GL_EXTCALL(glDeleteShader(vshader_id));
7613 GL_EXTCALL(glDeleteShader(pshader_id));
7614 return program_id;
7617 /* Context activation is done by the caller. */
7618 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7619 const struct wined3d_state *state)
7621 struct glsl_context_data *ctx_data = context->shader_backend_data;
7622 const struct wined3d_gl_info *gl_info = context->gl_info;
7623 struct shader_glsl_priv *priv = shader_priv;
7624 GLuint program_id = 0, prev_id = 0;
7625 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7627 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7628 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7630 if (ctx_data->glsl_program)
7632 prev_id = ctx_data->glsl_program->id;
7633 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7635 else
7637 prev_id = 0;
7638 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7641 set_glsl_shader_program(context, state, priv, ctx_data);
7643 if (ctx_data->glsl_program)
7645 program_id = ctx_data->glsl_program->id;
7646 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7648 else
7650 program_id = 0;
7651 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7654 if (old_vertex_color_clamp != current_vertex_color_clamp)
7656 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7658 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7659 checkGLcall("glClampColorARB");
7661 else
7663 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7667 TRACE("Using GLSL program %u.\n", program_id);
7669 if (prev_id != program_id)
7671 GL_EXTCALL(glUseProgram(program_id));
7672 checkGLcall("glUseProgram");
7674 if (program_id)
7675 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7679 /* "context" is not necessarily the currently active context. */
7680 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7682 struct glsl_context_data *ctx_data = context->shader_backend_data;
7684 ctx_data->glsl_program = NULL;
7685 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7686 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7687 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
7688 | (1u << WINED3D_SHADER_TYPE_HULL)
7689 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
7692 /* Context activation is done by the caller. */
7693 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7695 const struct wined3d_gl_info *gl_info = context->gl_info;
7696 struct shader_glsl_priv *priv = shader_priv;
7698 shader_glsl_invalidate_current_program(context);
7699 GL_EXTCALL(glUseProgram(0));
7700 checkGLcall("glUseProgram");
7702 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7703 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7705 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7707 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7708 checkGLcall("glClampColorARB");
7712 /* Context activation is done by the caller. */
7713 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7714 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7716 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7717 struct shader_glsl_priv *priv = shader_priv;
7718 GLuint *blt_program;
7719 GLint loc;
7721 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7722 if (!*blt_program)
7724 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7725 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7726 GL_EXTCALL(glUseProgram(*blt_program));
7727 GL_EXTCALL(glUniform1i(loc, 0));
7729 else
7731 GL_EXTCALL(glUseProgram(*blt_program));
7734 if (masked)
7736 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7737 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7741 /* Context activation is done by the caller. */
7742 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7744 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7745 GLuint program_id;
7747 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7748 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7750 GL_EXTCALL(glUseProgram(program_id));
7751 checkGLcall("glUseProgram");
7754 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7755 const struct glsl_shader_prog_link *program)
7757 const struct glsl_context_data *ctx_data;
7758 struct wined3d_context *context;
7759 unsigned int i;
7761 for (i = 0; i < device->context_count; ++i)
7763 context = device->contexts[i];
7764 ctx_data = context->shader_backend_data;
7766 if (ctx_data->glsl_program == program)
7767 shader_glsl_invalidate_current_program(context);
7771 static void shader_glsl_destroy(struct wined3d_shader *shader)
7773 struct glsl_shader_private *shader_data = shader->backend_data;
7774 struct wined3d_device *device = shader->device;
7775 struct shader_glsl_priv *priv = device->shader_priv;
7776 const struct wined3d_gl_info *gl_info;
7777 const struct list *linked_programs;
7778 struct wined3d_context *context;
7780 if (!shader_data || !shader_data->num_gl_shaders)
7782 HeapFree(GetProcessHeap(), 0, shader_data);
7783 shader->backend_data = NULL;
7784 return;
7787 context = context_acquire(device, NULL);
7788 gl_info = context->gl_info;
7790 TRACE("Deleting linked programs.\n");
7791 linked_programs = &shader->linked_programs;
7792 if (linked_programs->next)
7794 struct glsl_shader_prog_link *entry, *entry2;
7795 UINT i;
7797 switch (shader->reg_maps.shader_version.type)
7799 case WINED3D_SHADER_TYPE_PIXEL:
7801 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7803 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7805 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7806 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7807 checkGLcall("glDeleteShader");
7809 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7811 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7812 struct glsl_shader_prog_link, ps.shader_entry)
7814 shader_glsl_invalidate_contexts_program(device, entry);
7815 delete_glsl_program_entry(priv, gl_info, entry);
7818 break;
7821 case WINED3D_SHADER_TYPE_VERTEX:
7823 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7825 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7827 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7828 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7829 checkGLcall("glDeleteShader");
7831 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7833 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7834 struct glsl_shader_prog_link, vs.shader_entry)
7836 shader_glsl_invalidate_contexts_program(device, entry);
7837 delete_glsl_program_entry(priv, gl_info, entry);
7840 break;
7843 case WINED3D_SHADER_TYPE_GEOMETRY:
7845 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7847 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7849 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7850 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7851 checkGLcall("glDeleteShader");
7853 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7855 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7856 struct glsl_shader_prog_link, gs.shader_entry)
7858 shader_glsl_invalidate_contexts_program(device, entry);
7859 delete_glsl_program_entry(priv, gl_info, entry);
7862 break;
7865 default:
7866 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7867 break;
7871 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7872 shader->backend_data = NULL;
7874 context_release(context);
7877 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7879 const struct glsl_program_key *k = key;
7880 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7881 const struct glsl_shader_prog_link, program_lookup_entry);
7883 if (k->vs_id > prog->vs.id) return 1;
7884 else if (k->vs_id < prog->vs.id) return -1;
7886 if (k->gs_id > prog->gs.id) return 1;
7887 else if (k->gs_id < prog->gs.id) return -1;
7889 if (k->ps_id > prog->ps.id) return 1;
7890 else if (k->ps_id < prog->ps.id) return -1;
7892 return 0;
7895 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7897 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7898 + constant_count * sizeof(*heap->contained)
7899 + constant_count * sizeof(*heap->positions);
7900 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7902 if (!mem)
7904 ERR("Failed to allocate memory\n");
7905 return FALSE;
7908 heap->entries = mem;
7909 heap->entries[1].version = 0;
7910 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7911 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7912 heap->positions = (unsigned int *)(heap->contained + constant_count);
7913 heap->size = 1;
7915 return TRUE;
7918 static void constant_heap_free(struct constant_heap *heap)
7920 HeapFree(GetProcessHeap(), 0, heap->entries);
7923 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7925 wined3d_rb_alloc,
7926 wined3d_rb_realloc,
7927 wined3d_rb_free,
7928 glsl_program_key_compare,
7931 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7932 const struct fragment_pipeline *fragment_pipe)
7934 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7935 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7936 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7937 gl_info->limits.glsl_ps_float_constants)) + 1;
7938 struct fragment_caps fragment_caps;
7939 void *vertex_priv, *fragment_priv;
7941 string_buffer_list_init(&priv->string_buffers);
7943 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7945 ERR("Failed to initialize vertex pipe.\n");
7946 HeapFree(GetProcessHeap(), 0, priv);
7947 return E_FAIL;
7950 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7952 ERR("Failed to initialize fragment pipe.\n");
7953 vertex_pipe->vp_free(device);
7954 HeapFree(GetProcessHeap(), 0, priv);
7955 return E_FAIL;
7958 if (!string_buffer_init(&priv->shader_buffer))
7960 ERR("Failed to initialize shader buffer.\n");
7961 goto fail;
7964 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7965 if (!priv->stack)
7967 ERR("Failed to allocate memory.\n");
7968 goto fail;
7971 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7973 ERR("Failed to initialize vertex shader constant heap\n");
7974 goto fail;
7977 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7979 ERR("Failed to initialize pixel shader constant heap\n");
7980 goto fail;
7983 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7985 ERR("Failed to initialize rbtree.\n");
7986 goto fail;
7989 priv->next_constant_version = 1;
7990 priv->vertex_pipe = vertex_pipe;
7991 priv->fragment_pipe = fragment_pipe;
7992 fragment_pipe->get_caps(gl_info, &fragment_caps);
7993 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7994 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7996 device->vertex_priv = vertex_priv;
7997 device->fragment_priv = fragment_priv;
7998 device->shader_priv = priv;
8000 return WINED3D_OK;
8002 fail:
8003 constant_heap_free(&priv->pconst_heap);
8004 constant_heap_free(&priv->vconst_heap);
8005 HeapFree(GetProcessHeap(), 0, priv->stack);
8006 string_buffer_free(&priv->shader_buffer);
8007 fragment_pipe->free_private(device);
8008 vertex_pipe->vp_free(device);
8009 HeapFree(GetProcessHeap(), 0, priv);
8010 return E_OUTOFMEMORY;
8013 /* Context activation is done by the caller. */
8014 static void shader_glsl_free(struct wined3d_device *device)
8016 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8017 struct shader_glsl_priv *priv = device->shader_priv;
8018 int i;
8020 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
8022 if (priv->depth_blt_program_full[i])
8024 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
8026 if (priv->depth_blt_program_masked[i])
8028 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
8032 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8033 constant_heap_free(&priv->pconst_heap);
8034 constant_heap_free(&priv->vconst_heap);
8035 HeapFree(GetProcessHeap(), 0, priv->stack);
8036 string_buffer_list_cleanup(&priv->string_buffers);
8037 string_buffer_free(&priv->shader_buffer);
8038 priv->fragment_pipe->free_private(device);
8039 priv->vertex_pipe->vp_free(device);
8041 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8042 device->shader_priv = NULL;
8045 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8047 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8048 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8051 static void shader_glsl_free_context_data(struct wined3d_context *context)
8053 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8056 static void shader_glsl_init_context_state(struct wined3d_context *context)
8058 const struct wined3d_gl_info *gl_info = context->gl_info;
8060 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8061 checkGLcall("GL_PROGRAM_POINT_SIZE");
8064 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8066 UINT shader_model;
8068 /* FIXME: Check for the specific extensions required for SM5 support
8069 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8070 * soon as we introduce them, adjusting the GL / GLSL version checks
8071 * accordingly. */
8072 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3])
8073 shader_model = 5;
8074 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8075 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8076 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8077 shader_model = 4;
8078 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
8079 * texldd and texldl instructions. */
8080 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
8081 shader_model = 3;
8082 else
8083 shader_model = 2;
8084 TRACE("Shader model %u.\n", shader_model);
8086 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8087 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8088 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8089 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8090 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8092 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
8093 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
8094 caps->varying_count = gl_info->limits.glsl_varyings;
8096 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8097 * Direct3D minimum requirement.
8099 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8100 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8102 * The problem is that the refrast clamps temporary results in the shader to
8103 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8104 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8105 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8106 * offer a way to query this.
8108 if (shader_model >= 4)
8109 caps->ps_1x_max_value = FLT_MAX;
8110 else
8111 caps->ps_1x_max_value = 1024.0f;
8113 /* Ideally we'd only set caps like sRGB writes here if supported by both
8114 * the shader backend and the fragment pipe, but we can get called before
8115 * shader_glsl_alloc(). */
8116 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8117 | WINED3D_SHADER_CAP_SRGB_WRITE;
8120 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8122 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8124 TRACE("Checking support for fixup:\n");
8125 dump_color_fixup_desc(fixup);
8128 /* We support everything except YUV conversions. */
8129 if (!is_complex_fixup(fixup))
8131 TRACE("[OK]\n");
8132 return TRUE;
8135 TRACE("[FAILED]\n");
8136 return FALSE;
8139 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8141 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8142 /* WINED3DSIH_ADD */ shader_glsl_binop,
8143 /* WINED3DSIH_AND */ shader_glsl_binop,
8144 /* WINED3DSIH_BEM */ shader_glsl_bem,
8145 /* WINED3DSIH_BREAK */ shader_glsl_break,
8146 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8147 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8148 /* WINED3DSIH_CALL */ shader_glsl_call,
8149 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8150 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8151 /* WINED3DSIH_CND */ shader_glsl_cnd,
8152 /* WINED3DSIH_CRS */ shader_glsl_cross,
8153 /* WINED3DSIH_CUT */ shader_glsl_cut,
8154 /* WINED3DSIH_DCL */ shader_glsl_nop,
8155 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8156 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8157 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8158 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8159 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ NULL,
8160 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8161 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8162 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8163 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8164 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8165 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8166 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8167 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8168 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8169 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8170 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8171 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8172 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8173 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8174 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8175 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8176 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8177 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8178 /* WINED3DSIH_DCL_UAV_TYPED */ NULL,
8179 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8180 /* WINED3DSIH_DEF */ shader_glsl_nop,
8181 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8182 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8183 /* WINED3DSIH_DIV */ shader_glsl_binop,
8184 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8185 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8186 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8187 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8188 /* WINED3DSIH_DST */ shader_glsl_dst,
8189 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8190 /* WINED3DSIH_DSX_COARSE */ NULL,
8191 /* WINED3DSIH_DSX_FINE */ NULL,
8192 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8193 /* WINED3DSIH_DSY_COARSE */ NULL,
8194 /* WINED3DSIH_DSY_FINE */ NULL,
8195 /* WINED3DSIH_ELSE */ shader_glsl_else,
8196 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8197 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8198 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8199 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8200 /* WINED3DSIH_EQ */ shader_glsl_relop,
8201 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8202 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8203 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8204 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8205 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8206 /* WINED3DSIH_GE */ shader_glsl_relop,
8207 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8208 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8209 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8210 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8211 /* WINED3DSIH_IADD */ shader_glsl_binop,
8212 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8213 /* WINED3DSIH_IF */ shader_glsl_if,
8214 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8215 /* WINED3DSIH_IGE */ shader_glsl_relop,
8216 /* WINED3DSIH_ILT */ shader_glsl_relop,
8217 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8218 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8219 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8220 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8221 /* WINED3DSIH_INE */ shader_glsl_relop,
8222 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8223 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8224 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8225 /* WINED3DSIH_LABEL */ shader_glsl_label,
8226 /* WINED3DSIH_LD */ shader_glsl_ld,
8227 /* WINED3DSIH_LD2DMS */ NULL,
8228 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8229 /* WINED3DSIH_LIT */ shader_glsl_lit,
8230 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8231 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8232 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8233 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8234 /* WINED3DSIH_LT */ shader_glsl_relop,
8235 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8236 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8237 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8238 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8239 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8240 /* WINED3DSIH_MAD */ shader_glsl_mad,
8241 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8242 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8243 /* WINED3DSIH_MOV */ shader_glsl_mov,
8244 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8245 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8246 /* WINED3DSIH_MUL */ shader_glsl_binop,
8247 /* WINED3DSIH_NE */ shader_glsl_relop,
8248 /* WINED3DSIH_NOP */ shader_glsl_nop,
8249 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8250 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8251 /* WINED3DSIH_OR */ shader_glsl_binop,
8252 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8253 /* WINED3DSIH_POW */ shader_glsl_pow,
8254 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8255 /* WINED3DSIH_REP */ shader_glsl_rep,
8256 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8257 /* WINED3DSIH_RET */ shader_glsl_ret,
8258 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8259 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
8260 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
8261 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8262 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8263 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
8264 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
8265 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
8266 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
8267 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
8268 /* WINED3DSIH_SETP */ NULL,
8269 /* WINED3DSIH_SGE */ shader_glsl_compare,
8270 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8271 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8272 /* WINED3DSIH_SLT */ shader_glsl_compare,
8273 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8274 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
8275 /* WINED3DSIH_SUB */ shader_glsl_binop,
8276 /* WINED3DSIH_TEX */ shader_glsl_tex,
8277 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8278 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8279 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8280 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8281 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8282 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8283 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8284 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8285 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8286 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8287 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8288 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8289 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8290 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8291 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8292 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8293 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8294 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8295 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8296 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8297 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8298 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8299 /* WINED3DSIH_UGE */ shader_glsl_relop,
8300 /* WINED3DSIH_ULT */ shader_glsl_relop,
8301 /* WINED3DSIH_USHR */ shader_glsl_binop,
8302 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8303 /* WINED3DSIH_XOR */ shader_glsl_binop,
8306 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8307 SHADER_HANDLER hw_fct;
8309 /* Select handler */
8310 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8312 /* Unhandled opcode */
8313 if (!hw_fct)
8315 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8316 return;
8318 hw_fct(ins);
8320 shader_glsl_add_instruction_modifiers(ins);
8323 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8325 struct shader_glsl_priv *priv = shader_priv;
8327 return priv->ffp_proj_control;
8330 const struct wined3d_shader_backend_ops glsl_shader_backend =
8332 shader_glsl_handle_instruction,
8333 shader_glsl_select,
8334 shader_glsl_disable,
8335 shader_glsl_select_depth_blt,
8336 shader_glsl_deselect_depth_blt,
8337 shader_glsl_update_float_vertex_constants,
8338 shader_glsl_update_float_pixel_constants,
8339 shader_glsl_load_constants,
8340 shader_glsl_destroy,
8341 shader_glsl_alloc,
8342 shader_glsl_free,
8343 shader_glsl_allocate_context_data,
8344 shader_glsl_free_context_data,
8345 shader_glsl_init_context_state,
8346 shader_glsl_get_caps,
8347 shader_glsl_color_fixup_supported,
8348 shader_glsl_has_ffp_proj_control,
8351 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8353 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8355 caps->xyzrhw = TRUE;
8356 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8357 caps->ffp_generic_attributes = TRUE;
8358 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8359 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8360 caps->max_vertex_blend_matrix_index = 0;
8361 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8362 | WINED3DVTXPCAPS_MATERIALSOURCE7
8363 | WINED3DVTXPCAPS_VERTEXFOG
8364 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8365 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8366 | WINED3DVTXPCAPS_LOCALVIEWER
8367 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8368 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8369 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8370 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8373 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8375 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8376 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8377 return 0;
8380 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8382 struct shader_glsl_priv *priv;
8384 if (shader_backend == &glsl_shader_backend)
8386 priv = shader_priv;
8388 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8390 ERR("Failed to initialize rbtree.\n");
8391 return NULL;
8394 return priv;
8397 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8399 return NULL;
8402 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8404 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8405 struct glsl_ffp_vertex_shader, desc.entry);
8406 struct glsl_shader_prog_link *program, *program2;
8407 struct glsl_ffp_destroy_ctx *ctx = context;
8409 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8410 struct glsl_shader_prog_link, vs.shader_entry)
8412 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8414 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8415 HeapFree(GetProcessHeap(), 0, shader);
8418 /* Context activation is done by the caller. */
8419 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8421 struct shader_glsl_priv *priv = device->vertex_priv;
8422 struct glsl_ffp_destroy_ctx ctx;
8424 ctx.priv = priv;
8425 ctx.gl_info = &device->adapter->gl_info;
8426 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8429 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8430 const struct wined3d_state *state, DWORD state_id) {}
8432 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8433 const struct wined3d_state *state, DWORD state_id)
8435 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8438 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8439 const struct wined3d_state *state, DWORD state_id)
8441 const struct wined3d_gl_info *gl_info = context->gl_info;
8442 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8443 BOOL transformed = context->stream_info.position_transformed;
8444 BOOL wasrhw = context->last_was_rhw;
8445 unsigned int i;
8447 context->last_was_rhw = transformed;
8449 /* If the vertex declaration contains a transformed position attribute,
8450 * the draw uses the fixed function vertex pipeline regardless of any
8451 * vertex shader set by the application. */
8452 if (transformed != wasrhw)
8453 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8455 if (!use_vs(state))
8457 if (context->last_was_vshader)
8459 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8460 clipplane(context, state, STATE_CLIPPLANE(i));
8463 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8465 /* Because of settings->texcoords, we have to regenerate the vertex
8466 * shader on a vdecl change if there aren't enough varyings to just
8467 * always output all the texture coordinates. */
8468 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8469 || normal != context->last_was_normal)
8470 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8472 if (use_ps(state)
8473 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8474 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8475 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8477 else
8479 if (!context->last_was_vshader)
8481 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8482 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8483 clipplane(context, state, STATE_CLIPPLANE(i));
8487 context->last_was_vshader = use_vs(state);
8488 context->last_was_normal = normal;
8491 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8492 const struct wined3d_state *state, DWORD state_id)
8494 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8495 /* Different vertex shaders potentially require a different vertex attributes setup. */
8496 if (!isStateDirty(context, STATE_VDECL))
8497 context_apply_state(context, state, STATE_VDECL);
8500 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8501 const struct wined3d_state *state, DWORD state_id)
8503 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8506 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8507 const struct wined3d_state *state, DWORD state_id)
8509 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8512 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8514 const struct wined3d_gl_info *gl_info = context->gl_info;
8515 unsigned int k;
8517 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8518 | WINED3D_SHADER_CONST_FFP_LIGHTS
8519 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8521 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8523 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8524 clipplane(context, state, STATE_CLIPPLANE(k));
8528 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8529 const struct wined3d_state *state, DWORD state_id)
8531 /* Table fog behavior depends on the projection matrix. */
8532 if (state->render_states[WINED3D_RS_FOGENABLE]
8533 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8534 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8535 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8538 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8539 const struct wined3d_state *state, DWORD state_id)
8541 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8542 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8543 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8544 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8545 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8546 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8549 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8550 const struct wined3d_state *state, DWORD state_id)
8552 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8555 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8556 const struct wined3d_state *state, DWORD state_id)
8558 DWORD sampler = state_id - STATE_SAMPLER(0);
8559 const struct wined3d_texture *texture = state->textures[sampler];
8560 BOOL np2;
8562 if (!texture)
8563 return;
8565 if (sampler >= MAX_TEXTURES)
8566 return;
8568 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8569 || context->lastWasPow2Texture & (1u << sampler))
8571 if (np2)
8572 context->lastWasPow2Texture |= 1u << sampler;
8573 else
8574 context->lastWasPow2Texture &= ~(1u << sampler);
8576 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8580 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8581 const struct wined3d_state *state, DWORD state_id)
8583 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8586 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8587 const struct wined3d_state *state, DWORD state_id)
8589 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8592 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8593 const struct wined3d_state *state, DWORD state_id)
8595 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8598 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8599 const struct wined3d_state *state, DWORD state_id)
8601 if (!use_vs(state))
8602 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8605 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
8606 const struct wined3d_state *state, DWORD state_id)
8608 static unsigned int once;
8610 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
8611 FIXME("Non-point sprite points not supported in core profile.\n");
8614 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8615 const struct wined3d_state *state, DWORD state_id)
8617 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8620 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8622 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8623 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8624 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8625 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8626 /* Clip planes */
8627 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8628 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8629 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8630 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8631 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8632 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8633 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8634 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8635 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8636 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8637 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8638 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8639 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8640 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8641 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8642 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8643 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8644 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8645 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8646 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8647 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8648 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8649 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8650 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8651 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8652 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8653 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8654 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8655 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8656 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8657 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8658 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8659 /* Lights */
8660 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8661 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8662 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8663 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8664 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8665 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8666 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8667 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8668 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8669 /* Viewport */
8670 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8671 /* Transform states */
8672 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8673 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8674 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8675 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8676 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8677 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8678 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8679 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8680 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8681 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8682 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8683 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8684 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8685 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8686 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8687 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8688 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8689 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8690 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8691 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8692 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8693 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8694 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8695 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8696 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8697 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8698 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8699 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8700 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8701 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8702 /* Fog */
8703 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8704 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8705 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8706 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8707 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8708 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8709 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8710 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8711 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8712 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8713 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8714 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8715 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8716 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8717 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8718 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8719 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8720 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8721 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8722 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
8723 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
8724 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8725 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8726 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8727 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8728 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8729 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8730 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8731 /* NP2 texture matrix fixups. They are not needed if
8732 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8733 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8734 * matrix. */
8735 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8736 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8737 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8738 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8739 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8740 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8741 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8742 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8743 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8744 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8745 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8746 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8747 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8748 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8749 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8750 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8751 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8752 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8753 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8754 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8755 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8756 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8757 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8758 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8759 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8760 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8761 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8762 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8765 /* TODO:
8766 * - Implement vertex tweening. */
8767 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8769 glsl_vertex_pipe_vp_enable,
8770 glsl_vertex_pipe_vp_get_caps,
8771 glsl_vertex_pipe_vp_get_emul_mask,
8772 glsl_vertex_pipe_vp_alloc,
8773 glsl_vertex_pipe_vp_free,
8774 glsl_vertex_pipe_vp_states,
8777 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8779 /* Nothing to do. */
8782 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8784 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8785 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8786 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8787 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8788 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8789 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8790 | WINED3DTEXOPCAPS_SELECTARG1
8791 | WINED3DTEXOPCAPS_SELECTARG2
8792 | WINED3DTEXOPCAPS_MODULATE4X
8793 | WINED3DTEXOPCAPS_MODULATE2X
8794 | WINED3DTEXOPCAPS_MODULATE
8795 | WINED3DTEXOPCAPS_ADDSIGNED2X
8796 | WINED3DTEXOPCAPS_ADDSIGNED
8797 | WINED3DTEXOPCAPS_ADD
8798 | WINED3DTEXOPCAPS_SUBTRACT
8799 | WINED3DTEXOPCAPS_ADDSMOOTH
8800 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8801 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8802 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8803 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8804 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8805 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8806 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8807 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8808 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8809 | WINED3DTEXOPCAPS_DOTPRODUCT3
8810 | WINED3DTEXOPCAPS_MULTIPLYADD
8811 | WINED3DTEXOPCAPS_LERP
8812 | WINED3DTEXOPCAPS_BUMPENVMAP
8813 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8814 caps->MaxTextureBlendStages = 8;
8815 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8818 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8820 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8821 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8822 return 0;
8825 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8827 struct shader_glsl_priv *priv;
8829 if (shader_backend == &glsl_shader_backend)
8831 priv = shader_priv;
8833 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8835 ERR("Failed to initialize rbtree.\n");
8836 return NULL;
8839 return priv;
8842 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8844 return NULL;
8847 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8849 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8850 struct glsl_ffp_fragment_shader, entry.entry);
8851 struct glsl_shader_prog_link *program, *program2;
8852 struct glsl_ffp_destroy_ctx *ctx = context;
8854 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8855 struct glsl_shader_prog_link, ps.shader_entry)
8857 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8859 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8860 HeapFree(GetProcessHeap(), 0, shader);
8863 /* Context activation is done by the caller. */
8864 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8866 struct shader_glsl_priv *priv = device->fragment_priv;
8867 struct glsl_ffp_destroy_ctx ctx;
8869 ctx.priv = priv;
8870 ctx.gl_info = &device->adapter->gl_info;
8871 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8874 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8875 const struct wined3d_state *state, DWORD state_id)
8877 context->last_was_pshader = use_ps(state);
8879 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8882 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8883 const struct wined3d_state *state, DWORD state_id)
8885 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8888 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8889 const struct wined3d_state *state, DWORD state_id)
8891 BOOL use_vshader = use_vs(state);
8892 enum fogsource new_source;
8893 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8894 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8896 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8898 if (!state->render_states[WINED3D_RS_FOGENABLE])
8899 return;
8901 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8903 if (use_vshader)
8904 new_source = FOGSOURCE_VS;
8905 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8906 new_source = FOGSOURCE_COORD;
8907 else
8908 new_source = FOGSOURCE_FFP;
8910 else
8912 new_source = FOGSOURCE_FFP;
8915 if (new_source != context->fog_source || fogstart == fogend)
8917 context->fog_source = new_source;
8918 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8922 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8923 const struct wined3d_state *state, DWORD state_id)
8925 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8926 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8927 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8929 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8930 glsl_fragment_pipe_fog(context, state, state_id);
8933 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8934 const struct wined3d_state *state, DWORD state_id)
8936 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8937 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8938 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8941 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8942 const struct wined3d_state *state, DWORD state_id)
8944 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8947 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8948 const struct wined3d_state *state, DWORD state_id)
8950 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8953 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8954 const struct wined3d_state *state, DWORD state_id)
8956 const struct wined3d_gl_info *gl_info = context->gl_info;
8957 int glParm;
8958 float ref;
8960 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8962 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8964 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8965 checkGLcall("glEnable GL_ALPHA_TEST");
8967 else
8969 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8970 checkGLcall("glDisable GL_ALPHA_TEST");
8971 return;
8974 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8975 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8977 if (glParm)
8979 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8980 checkGLcall("glAlphaFunc");
8984 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8985 const struct wined3d_state *state, DWORD state_id)
8987 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8990 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8991 const struct wined3d_state *state, DWORD state_id)
8993 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8996 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8998 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8999 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9000 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9001 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9002 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9003 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9004 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9005 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9006 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9007 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9008 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9009 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9010 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9011 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9012 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9013 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9014 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9015 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9016 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9017 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9018 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9019 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9020 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9021 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9022 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9023 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9024 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9025 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9026 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9027 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9028 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9029 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9030 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9031 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9032 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9033 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9034 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9035 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9036 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9037 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9038 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9039 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9040 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9041 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9042 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9043 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9044 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9045 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9046 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9047 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9048 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9049 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9050 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9051 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9052 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9053 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9054 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9055 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9056 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9057 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9058 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9059 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9060 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9061 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9062 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9063 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9064 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9065 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9066 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9067 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9068 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9069 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9070 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9071 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9072 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9073 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9074 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9075 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9076 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
9077 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9078 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9079 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9080 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9081 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9082 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9083 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9084 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9085 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9086 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9087 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9088 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9089 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9090 {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 },
9091 {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 },
9092 {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 },
9093 {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 },
9094 {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 },
9095 {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 },
9096 {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 },
9097 {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 },
9098 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9099 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9100 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9101 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9102 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9103 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9104 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9105 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9106 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9107 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9108 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9109 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9110 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9113 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9115 return TRUE;
9118 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9122 const struct fragment_pipeline glsl_fragment_pipe =
9124 glsl_fragment_pipe_enable,
9125 glsl_fragment_pipe_get_caps,
9126 glsl_fragment_pipe_get_emul_mask,
9127 glsl_fragment_pipe_alloc,
9128 glsl_fragment_pipe_free,
9129 glsl_fragment_pipe_alloc_context_data,
9130 glsl_fragment_pipe_free_context_data,
9131 shader_glsl_color_fixup_supported,
9132 glsl_fragment_pipe_state_template,