wined3d: Enforce a stable texture units mapping.
[wine.git] / dlls / wined3d / glsl_shader.c
blobabfaef8742ae5f64bd557cbe0c4001ae19182dd0
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 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 struct wined3d_string_buffer *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
69 BOOL output_single_component;
72 enum heap_node_op
74 HEAP_NODE_TRAVERSE_LEFT,
75 HEAP_NODE_TRAVERSE_RIGHT,
76 HEAP_NODE_POP,
79 struct constant_entry
81 unsigned int idx;
82 unsigned int version;
85 struct constant_heap
87 struct constant_entry *entries;
88 BOOL *contained;
89 unsigned int *positions;
90 unsigned int size;
93 /* GLSL shader private data */
94 struct shader_glsl_priv {
95 struct wined3d_string_buffer shader_buffer;
96 struct wined3d_string_buffer_list string_buffers;
97 struct wine_rb_tree program_lookup;
98 struct constant_heap vconst_heap;
99 struct constant_heap pconst_heap;
100 unsigned char *stack;
101 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
102 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
103 UINT next_constant_version;
105 const struct wined3d_vertex_pipe_ops *vertex_pipe;
106 const struct fragment_pipeline *fragment_pipe;
107 struct wine_rb_tree ffp_vertex_shaders;
108 struct wine_rb_tree ffp_fragment_shaders;
109 BOOL ffp_proj_control;
110 BOOL legacy_lighting;
113 struct glsl_vs_program
115 struct list shader_entry;
116 GLuint id;
117 GLenum vertex_color_clamp;
118 GLint *uniform_f_locations;
119 GLint uniform_i_locations[MAX_CONST_I];
120 GLint uniform_b_locations[MAX_CONST_B];
121 GLint pos_fixup_location;
123 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
124 GLint projection_matrix_location;
125 GLint normal_matrix_location;
126 GLint texture_matrix_location[MAX_TEXTURES];
127 GLint material_ambient_location;
128 GLint material_diffuse_location;
129 GLint material_specular_location;
130 GLint material_emissive_location;
131 GLint material_shininess_location;
132 GLint light_ambient_location;
133 struct
135 GLint diffuse;
136 GLint specular;
137 GLint ambient;
138 GLint position;
139 GLint direction;
140 GLint range;
141 GLint falloff;
142 GLint c_att;
143 GLint l_att;
144 GLint q_att;
145 GLint cos_htheta;
146 GLint cos_hphi;
147 } light_location[MAX_ACTIVE_LIGHTS];
148 GLint pointsize_location;
149 GLint pointsize_min_location;
150 GLint pointsize_max_location;
151 GLint pointsize_c_att_location;
152 GLint pointsize_l_att_location;
153 GLint pointsize_q_att_location;
156 struct glsl_gs_program
158 struct list shader_entry;
159 GLuint id;
162 struct glsl_ps_program
164 struct list shader_entry;
165 GLuint id;
166 GLint *uniform_f_locations;
167 GLint uniform_i_locations[MAX_CONST_I];
168 GLint uniform_b_locations[MAX_CONST_B];
169 GLint bumpenv_mat_location[MAX_TEXTURES];
170 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
171 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
172 GLint tss_constant_location[MAX_TEXTURES];
173 GLint tex_factor_location;
174 GLint specular_enable_location;
175 GLint fog_color_location;
176 GLint fog_density_location;
177 GLint fog_end_location;
178 GLint fog_scale_location;
179 GLint ycorrection_location;
180 GLint np2_fixup_location;
181 GLint color_key_location;
182 const struct ps_np2fixup_info *np2_fixup_info;
185 /* Struct to maintain data about a linked GLSL program */
186 struct glsl_shader_prog_link
188 struct wine_rb_entry program_lookup_entry;
189 struct glsl_vs_program vs;
190 struct glsl_gs_program gs;
191 struct glsl_ps_program ps;
192 GLuint id;
193 DWORD constant_update_mask;
194 UINT constant_version;
197 struct glsl_program_key
199 GLuint vs_id;
200 GLuint gs_id;
201 GLuint ps_id;
204 struct shader_glsl_ctx_priv {
205 const struct vs_compile_args *cur_vs_args;
206 const struct ps_compile_args *cur_ps_args;
207 struct ps_np2fixup_info *cur_np2fixup_info;
208 struct wined3d_string_buffer_list *string_buffers;
211 struct glsl_context_data
213 struct glsl_shader_prog_link *glsl_program;
216 struct glsl_ps_compiled_shader
218 struct ps_compile_args args;
219 struct ps_np2fixup_info np2fixup;
220 GLuint id;
223 struct glsl_vs_compiled_shader
225 struct vs_compile_args args;
226 GLuint id;
229 struct glsl_gs_compiled_shader
231 GLuint id;
234 struct glsl_shader_private
236 union
238 struct glsl_vs_compiled_shader *vs;
239 struct glsl_gs_compiled_shader *gs;
240 struct glsl_ps_compiled_shader *ps;
241 } gl_shaders;
242 UINT num_gl_shaders, shader_array_size;
245 struct glsl_ffp_vertex_shader
247 struct wined3d_ffp_vs_desc desc;
248 GLuint id;
249 struct list linked_programs;
252 struct glsl_ffp_fragment_shader
254 struct ffp_frag_desc entry;
255 GLuint id;
256 struct list linked_programs;
259 struct glsl_ffp_destroy_ctx
261 struct shader_glsl_priv *priv;
262 const struct wined3d_gl_info *gl_info;
265 static const char *debug_gl_shader_type(GLenum type)
267 switch (type)
269 #define WINED3D_TO_STR(u) case u: return #u
270 WINED3D_TO_STR(GL_VERTEX_SHADER);
271 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
272 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
273 #undef WINED3D_TO_STR
274 default:
275 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
279 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
281 switch (type)
283 case WINED3D_SHADER_TYPE_VERTEX:
284 return "vs";
286 case WINED3D_SHADER_TYPE_GEOMETRY:
287 return "gs";
289 case WINED3D_SHADER_TYPE_PIXEL:
290 return "ps";
292 default:
293 FIXME("Unhandled shader type %#x.\n", type);
294 return "unknown";
298 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
299 const struct wined3d_shader_version *version)
301 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
302 return "#version 150";
303 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
304 return "#version 130";
305 else
306 return "#version 120";
309 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
311 char str[4][17];
313 wined3d_ftoa(values[0], str[0]);
314 wined3d_ftoa(values[1], str[1]);
315 wined3d_ftoa(values[2], str[2]);
316 wined3d_ftoa(values[3], str[3]);
317 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
320 static const char *get_info_log_line(const char **ptr)
322 const char *p, *q;
324 p = *ptr;
325 if (!(q = strstr(p, "\n")))
327 if (!*p) return NULL;
328 *ptr += strlen(p);
329 return p;
331 *ptr = q + 1;
333 return p;
336 /* Context activation is done by the caller. */
337 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
339 int length = 0;
340 char *log;
342 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
343 return;
345 if (program)
346 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
347 else
348 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
350 /* A size of 1 is just a null-terminated string, so the log should be bigger than
351 * that if there are errors. */
352 if (length > 1)
354 const char *ptr, *line;
356 log = HeapAlloc(GetProcessHeap(), 0, length);
357 /* The info log is supposed to be zero-terminated, but at least some
358 * versions of fglrx don't terminate the string properly. The reported
359 * length does include the terminator, so explicitly set it to zero
360 * here. */
361 log[length - 1] = 0;
362 if (program)
363 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
364 else
365 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
367 ptr = log;
368 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
370 WARN("Info log received from GLSL shader #%u:\n", id);
371 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
373 else
375 FIXME("Info log received from GLSL shader #%u:\n", id);
376 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
378 HeapFree(GetProcessHeap(), 0, log);
382 /* Context activation is done by the caller. */
383 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
385 const char *ptr, *line;
387 TRACE("Compiling shader object %u.\n", shader);
389 if (TRACE_ON(d3d_shader))
391 ptr = src;
392 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
395 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
396 checkGLcall("glShaderSource");
397 GL_EXTCALL(glCompileShader(shader));
398 checkGLcall("glCompileShader");
399 print_glsl_info_log(gl_info, shader, FALSE);
402 /* Context activation is done by the caller. */
403 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
405 GLint i, shader_count, source_size = -1;
406 GLuint *shaders;
407 char *source = NULL;
409 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
410 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
411 if (!shaders)
413 ERR("Failed to allocate shader array memory.\n");
414 return;
417 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
418 for (i = 0; i < shader_count; ++i)
420 const char *ptr, *line;
421 GLint tmp;
423 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
425 if (source_size < tmp)
427 HeapFree(GetProcessHeap(), 0, source);
429 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
430 if (!source)
432 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
433 HeapFree(GetProcessHeap(), 0, shaders);
434 return;
436 source_size = tmp;
439 FIXME("Shader %u:\n", shaders[i]);
440 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
441 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
442 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
443 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
444 FIXME("\n");
446 ptr = source;
447 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
448 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
449 FIXME("\n");
452 HeapFree(GetProcessHeap(), 0, source);
453 HeapFree(GetProcessHeap(), 0, shaders);
456 /* Context activation is done by the caller. */
457 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
459 GLint tmp;
461 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
462 return;
464 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
465 if (!tmp)
467 FIXME("Program %u link status invalid.\n", program);
468 shader_glsl_dump_program_source(gl_info, program);
471 print_glsl_info_log(gl_info, program, TRUE);
474 /* Context activation is done by the caller. */
475 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
476 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
478 unsigned int mapped_unit;
479 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
480 const char *prefix;
481 unsigned int i, j;
482 GLint name_loc;
484 static const struct
486 enum wined3d_shader_type type;
487 unsigned int base_idx;
488 unsigned int count;
490 sampler_info[] =
492 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
493 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
496 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
498 prefix = shader_glsl_get_prefix(sampler_info[i].type);
500 for (j = 0; j < sampler_info[i].count; ++j)
502 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
503 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
504 if (name_loc == -1)
505 continue;
507 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
508 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
510 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
511 continue;
514 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
515 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
518 checkGLcall("glUniform1i");
519 string_buffer_release(&priv->string_buffers, sampler_name);
522 /* Context activation is done by the caller. */
523 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
524 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
526 unsigned int start = ~0U, end = 0;
527 int stack_idx = 0;
528 unsigned int heap_idx = 1;
529 unsigned int idx;
531 if (heap->entries[heap_idx].version <= version) return;
533 idx = heap->entries[heap_idx].idx;
534 if (constant_locations[idx] != -1)
535 start = end = idx;
536 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
538 while (stack_idx >= 0)
540 /* Note that we fall through to the next case statement. */
541 switch(stack[stack_idx])
543 case HEAP_NODE_TRAVERSE_LEFT:
545 unsigned int left_idx = heap_idx << 1;
546 if (left_idx < heap->size && heap->entries[left_idx].version > version)
548 heap_idx = left_idx;
549 idx = heap->entries[heap_idx].idx;
550 if (constant_locations[idx] != -1)
552 if (start > idx)
553 start = idx;
554 if (end < idx)
555 end = idx;
558 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
559 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
560 break;
564 case HEAP_NODE_TRAVERSE_RIGHT:
566 unsigned int right_idx = (heap_idx << 1) + 1;
567 if (right_idx < heap->size && heap->entries[right_idx].version > version)
569 heap_idx = right_idx;
570 idx = heap->entries[heap_idx].idx;
571 if (constant_locations[idx] != -1)
573 if (start > idx)
574 start = idx;
575 if (end < idx)
576 end = idx;
579 stack[stack_idx++] = HEAP_NODE_POP;
580 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
581 break;
585 case HEAP_NODE_POP:
586 heap_idx >>= 1;
587 --stack_idx;
588 break;
591 if (start <= end)
592 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
593 checkGLcall("walk_constant_heap()");
596 /* Context activation is done by the caller. */
597 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
599 GLfloat clamped_constant[4];
601 if (location == -1) return;
603 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
604 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
605 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
606 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
608 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
611 /* Context activation is done by the caller. */
612 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
613 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
615 int stack_idx = 0;
616 unsigned int heap_idx = 1;
617 unsigned int idx;
619 if (heap->entries[heap_idx].version <= version) return;
621 idx = heap->entries[heap_idx].idx;
622 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
623 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
625 while (stack_idx >= 0)
627 /* Note that we fall through to the next case statement. */
628 switch(stack[stack_idx])
630 case HEAP_NODE_TRAVERSE_LEFT:
632 unsigned int left_idx = heap_idx << 1;
633 if (left_idx < heap->size && heap->entries[left_idx].version > version)
635 heap_idx = left_idx;
636 idx = heap->entries[heap_idx].idx;
637 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
639 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
640 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
641 break;
645 case HEAP_NODE_TRAVERSE_RIGHT:
647 unsigned int right_idx = (heap_idx << 1) + 1;
648 if (right_idx < heap->size && heap->entries[right_idx].version > version)
650 heap_idx = right_idx;
651 idx = heap->entries[heap_idx].idx;
652 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
654 stack[stack_idx++] = HEAP_NODE_POP;
655 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
656 break;
660 case HEAP_NODE_POP:
661 heap_idx >>= 1;
662 --stack_idx;
663 break;
666 checkGLcall("walk_constant_heap_clamped()");
669 /* Context activation is done by the caller. */
670 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
671 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
672 unsigned char *stack, UINT version)
674 const struct wined3d_shader_lconst *lconst;
676 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
677 if (shader->reg_maps.shader_version.major == 1
678 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
679 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
680 else
681 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
683 if (!shader->load_local_constsF)
685 TRACE("No need to load local float constants for this shader\n");
686 return;
689 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
690 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
692 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
694 checkGLcall("glUniform4fv()");
697 /* Context activation is done by the caller. */
698 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
699 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
701 unsigned int i;
702 struct list* ptr;
704 for (i = 0; constants_set; constants_set >>= 1, ++i)
706 if (!(constants_set & 1)) continue;
708 /* We found this uniform name in the program - go ahead and send the data */
709 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
712 /* Load immediate constants */
713 ptr = list_head(&shader->constantsI);
714 while (ptr)
716 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
717 unsigned int idx = lconst->idx;
718 const GLint *values = (const GLint *)lconst->value;
720 /* We found this uniform name in the program - go ahead and send the data */
721 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
722 ptr = list_next(&shader->constantsI, ptr);
724 checkGLcall("glUniform4iv()");
727 /* Context activation is done by the caller. */
728 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
729 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
731 unsigned int i;
732 struct list* ptr;
734 for (i = 0; constants_set; constants_set >>= 1, ++i)
736 if (!(constants_set & 1)) continue;
738 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
741 /* Load immediate constants */
742 ptr = list_head(&shader->constantsB);
743 while (ptr)
745 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
746 unsigned int idx = lconst->idx;
747 const GLint *values = (const GLint *)lconst->value;
749 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
750 ptr = list_next(&shader->constantsB, ptr);
752 checkGLcall("glUniform1iv()");
755 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
757 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
760 /* Context activation is done by the caller (state handler). */
761 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
762 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
764 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
765 UINT fixup = ps->np2_fixup_info->active;
766 UINT i;
768 for (i = 0; fixup; fixup >>= 1, ++i)
770 const struct wined3d_texture *tex = state->textures[i];
771 unsigned char idx = ps->np2_fixup_info->idx[i];
772 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
774 if (!tex)
776 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
777 continue;
780 if (idx % 2)
782 tex_dim[2] = tex->pow2_matrix[0];
783 tex_dim[3] = tex->pow2_matrix[5];
785 else
787 tex_dim[0] = tex->pow2_matrix[0];
788 tex_dim[1] = tex->pow2_matrix[5];
792 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
795 /* Taken and adapted from Mesa. */
796 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
798 float pos, neg, t, det;
799 struct wined3d_matrix temp;
801 /* Calculate the determinant of upper left 3x3 submatrix and
802 * determine if the matrix is singular. */
803 pos = neg = 0.0f;
804 t = in->_11 * in->_22 * in->_33;
805 if (t >= 0.0f)
806 pos += t;
807 else
808 neg += t;
810 t = in->_21 * in->_32 * in->_13;
811 if (t >= 0.0f)
812 pos += t;
813 else
814 neg += t;
815 t = in->_31 * in->_12 * in->_23;
816 if (t >= 0.0f)
817 pos += t;
818 else
819 neg += t;
821 t = -in->_31 * in->_22 * in->_13;
822 if (t >= 0.0f)
823 pos += t;
824 else
825 neg += t;
826 t = -in->_21 * in->_12 * in->_33;
827 if (t >= 0.0f)
828 pos += t;
829 else
830 neg += t;
832 t = -in->_11 * in->_32 * in->_23;
833 if (t >= 0.0f)
834 pos += t;
835 else
836 neg += t;
838 det = pos + neg;
840 if (fabsf(det) < 1e-25f)
841 return FALSE;
843 det = 1.0f / det;
844 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
845 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
846 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
847 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
848 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
849 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
850 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
851 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
852 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
854 *out = temp;
855 return TRUE;
858 static void swap_rows(float **a, float **b)
860 float *tmp = *a;
862 *a = *b;
863 *b = tmp;
866 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
868 float wtmp[4][8];
869 float m0, m1, m2, m3, s;
870 float *r0, *r1, *r2, *r3;
872 r0 = wtmp[0];
873 r1 = wtmp[1];
874 r2 = wtmp[2];
875 r3 = wtmp[3];
877 r0[0] = m->_11;
878 r0[1] = m->_12;
879 r0[2] = m->_13;
880 r0[3] = m->_14;
881 r0[4] = 1.0f;
882 r0[5] = r0[6] = r0[7] = 0.0f;
884 r1[0] = m->_21;
885 r1[1] = m->_22;
886 r1[2] = m->_23;
887 r1[3] = m->_24;
888 r1[5] = 1.0f;
889 r1[4] = r1[6] = r1[7] = 0.0f;
891 r2[0] = m->_31;
892 r2[1] = m->_32;
893 r2[2] = m->_33;
894 r2[3] = m->_34;
895 r2[6] = 1.0f;
896 r2[4] = r2[5] = r2[7] = 0.0f;
898 r3[0] = m->_41;
899 r3[1] = m->_42;
900 r3[2] = m->_43;
901 r3[3] = m->_44;
902 r3[7] = 1.0f;
903 r3[4] = r3[5] = r3[6] = 0.0f;
905 /* Choose pivot - or die. */
906 if (fabsf(r3[0]) > fabsf(r2[0]))
907 swap_rows(&r3, &r2);
908 if (fabsf(r2[0]) > fabsf(r1[0]))
909 swap_rows(&r2, &r1);
910 if (fabsf(r1[0]) > fabsf(r0[0]))
911 swap_rows(&r1, &r0);
912 if (r0[0] == 0.0f)
913 return FALSE;
915 /* Eliminate first variable. */
916 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
917 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
918 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
919 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
920 s = r0[4];
921 if (s != 0.0f)
923 r1[4] -= m1 * s;
924 r2[4] -= m2 * s;
925 r3[4] -= m3 * s;
927 s = r0[5];
928 if (s != 0.0f)
930 r1[5] -= m1 * s;
931 r2[5] -= m2 * s;
932 r3[5] -= m3 * s;
934 s = r0[6];
935 if (s != 0.0f)
937 r1[6] -= m1 * s;
938 r2[6] -= m2 * s;
939 r3[6] -= m3 * s;
941 s = r0[7];
942 if (s != 0.0f)
944 r1[7] -= m1 * s;
945 r2[7] -= m2 * s;
946 r3[7] -= m3 * s;
949 /* Choose pivot - or die. */
950 if (fabsf(r3[1]) > fabsf(r2[1]))
951 swap_rows(&r3, &r2);
952 if (fabsf(r2[1]) > fabsf(r1[1]))
953 swap_rows(&r2, &r1);
954 if (r1[1] == 0.0f)
955 return FALSE;
957 /* Eliminate second variable. */
958 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
959 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
960 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
961 s = r1[4];
962 if (s != 0.0f)
964 r2[4] -= m2 * s;
965 r3[4] -= m3 * s;
967 s = r1[5];
968 if (s != 0.0f)
970 r2[5] -= m2 * s;
971 r3[5] -= m3 * s;
973 s = r1[6];
974 if (s != 0.0f)
976 r2[6] -= m2 * s;
977 r3[6] -= m3 * s;
979 s = r1[7];
980 if (s != 0.0f)
982 r2[7] -= m2 * s;
983 r3[7] -= m3 * s;
986 /* Choose pivot - or die. */
987 if (fabsf(r3[2]) > fabsf(r2[2]))
988 swap_rows(&r3, &r2);
989 if (r2[2] == 0.0f)
990 return FALSE;
992 /* Eliminate third variable. */
993 m3 = r3[2] / r2[2];
994 r3[3] -= m3 * r2[3];
995 r3[4] -= m3 * r2[4];
996 r3[5] -= m3 * r2[5];
997 r3[6] -= m3 * r2[6];
998 r3[7] -= m3 * r2[7];
1000 /* Last check. */
1001 if (r3[3] == 0.0f)
1002 return FALSE;
1004 /* Back substitute row 3. */
1005 s = 1.0f / r3[3];
1006 r3[4] *= s;
1007 r3[5] *= s;
1008 r3[6] *= s;
1009 r3[7] *= s;
1011 /* Back substitute row 2. */
1012 m2 = r2[3];
1013 s = 1.0f / r2[2];
1014 r2[4] = s * (r2[4] - r3[4] * m2);
1015 r2[5] = s * (r2[5] - r3[5] * m2);
1016 r2[6] = s * (r2[6] - r3[6] * m2);
1017 r2[7] = s * (r2[7] - r3[7] * m2);
1018 m1 = r1[3];
1019 r1[4] -= r3[4] * m1;
1020 r1[5] -= r3[5] * m1;
1021 r1[6] -= r3[6] * m1;
1022 r1[7] -= r3[7] * m1;
1023 m0 = r0[3];
1024 r0[4] -= r3[4] * m0;
1025 r0[5] -= r3[5] * m0;
1026 r0[6] -= r3[6] * m0;
1027 r0[7] -= r3[7] * m0;
1029 /* Back substitute row 1. */
1030 m1 = r1[2];
1031 s = 1.0f / r1[1];
1032 r1[4] = s * (r1[4] - r2[4] * m1);
1033 r1[5] = s * (r1[5] - r2[5] * m1);
1034 r1[6] = s * (r1[6] - r2[6] * m1);
1035 r1[7] = s * (r1[7] - r2[7] * m1);
1036 m0 = r0[2];
1037 r0[4] -= r2[4] * m0;
1038 r0[5] -= r2[5] * m0;
1039 r0[6] -= r2[6] * m0;
1040 r0[7] -= r2[7] * m0;
1042 /* Back substitute row 0. */
1043 m0 = r0[1];
1044 s = 1.0f / r0[0];
1045 r0[4] = s * (r0[4] - r1[4] * m0);
1046 r0[5] = s * (r0[5] - r1[5] * m0);
1047 r0[6] = s * (r0[6] - r1[6] * m0);
1048 r0[7] = s * (r0[7] - r1[7] * m0);
1050 out->_11 = r0[4];
1051 out->_12 = r0[5];
1052 out->_13 = r0[6];
1053 out->_14 = r0[7];
1054 out->_21 = r1[4];
1055 out->_22 = r1[5];
1056 out->_23 = r1[6];
1057 out->_24 = r1[7];
1058 out->_31 = r2[4];
1059 out->_32 = r2[5];
1060 out->_33 = r2[6];
1061 out->_34 = r2[7];
1062 out->_41 = r3[4];
1063 out->_42 = r3[5];
1064 out->_43 = r3[6];
1065 out->_44 = r3[7];
1067 return TRUE;
1070 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1071 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1073 const struct wined3d_gl_info *gl_info = context->gl_info;
1074 float mat[3 * 3];
1075 struct wined3d_matrix mv;
1076 unsigned int i, j;
1078 if (prog->vs.normal_matrix_location == -1)
1079 return;
1081 get_modelview_matrix(context, state, 0, &mv);
1082 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1083 invert_matrix_3d(&mv, &mv);
1084 else
1085 invert_matrix(&mv, &mv);
1086 /* Tests show that singular modelview matrices are used unchanged as normal
1087 * matrices on D3D3 and older. There seems to be no clearly consistent
1088 * behavior on newer D3D versions so always follow older ddraw behavior. */
1089 for (i = 0; i < 3; ++i)
1090 for (j = 0; j < 3; ++j)
1091 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1093 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1094 checkGLcall("glUniformMatrix3fv");
1097 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1098 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1100 const struct wined3d_gl_info *gl_info = context->gl_info;
1101 struct wined3d_matrix mat;
1103 if (tex >= MAX_TEXTURES)
1104 return;
1105 if (prog->vs.texture_matrix_location[tex] == -1)
1106 return;
1108 get_texture_matrix(context, state, tex, &mat);
1109 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1110 checkGLcall("glUniformMatrix4fv");
1113 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1114 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1116 const struct wined3d_gl_info *gl_info = context->gl_info;
1118 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1120 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1121 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1123 else
1125 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1127 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1129 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1130 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1131 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1132 checkGLcall("setting FFP material uniforms");
1135 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1136 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1138 const struct wined3d_gl_info *gl_info = context->gl_info;
1139 float col[4];
1141 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1142 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1143 checkGLcall("glUniform3fv");
1146 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1147 const struct wined3d_matrix *src2)
1149 struct wined3d_vec4 temp;
1151 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1152 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1153 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1154 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1156 *dest = temp;
1159 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1160 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1162 const struct wined3d_gl_info *gl_info = context->gl_info;
1163 const struct wined3d_light_info *light_info = state->lights[light];
1164 struct wined3d_vec4 vec4;
1165 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1167 if (!light_info)
1168 return;
1170 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1171 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1172 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1174 switch (light_info->OriginalParms.type)
1176 case WINED3D_LIGHT_POINT:
1177 multiply_vector_matrix(&vec4, &light_info->position, view);
1178 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1179 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1180 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1181 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1182 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1183 break;
1185 case WINED3D_LIGHT_SPOT:
1186 multiply_vector_matrix(&vec4, &light_info->position, view);
1187 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1189 multiply_vector_matrix(&vec4, &light_info->direction, view);
1190 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1192 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1193 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1194 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1195 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1196 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1197 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1198 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1199 break;
1201 case WINED3D_LIGHT_DIRECTIONAL:
1202 multiply_vector_matrix(&vec4, &light_info->direction, view);
1203 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1204 break;
1206 case WINED3D_LIGHT_PARALLELPOINT:
1207 multiply_vector_matrix(&vec4, &light_info->position, view);
1208 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1209 break;
1211 default:
1212 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1214 checkGLcall("setting FFP lights uniforms");
1217 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1218 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1220 const struct wined3d_gl_info *gl_info = context->gl_info;
1221 float min, max;
1222 float size, att[3];
1224 get_pointsize_minmax(context, state, &min, &max);
1226 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1227 checkGLcall("glUniform1f");
1228 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1229 checkGLcall("glUniform1f");
1231 get_pointsize(context, state, &size, att);
1233 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1234 checkGLcall("glUniform1f");
1235 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1236 checkGLcall("glUniform1f");
1237 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1238 checkGLcall("glUniform1f");
1239 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1240 checkGLcall("glUniform1f");
1243 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1244 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1246 const struct wined3d_gl_info *gl_info = context->gl_info;
1247 float start, end, scale;
1248 union
1250 DWORD d;
1251 float f;
1252 } tmpvalue;
1253 float col[4];
1255 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_FOGCOLOR], col);
1256 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, col));
1257 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1258 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1259 get_fog_start_end(context, state, &start, &end);
1260 scale = 1.0f / (end - start);
1261 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1262 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1263 checkGLcall("fog emulation uniforms");
1266 /* Context activation is done by the caller (state handler). */
1267 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1268 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1270 struct wined3d_color float_key[2];
1271 const struct wined3d_texture *texture = state->textures[0];
1273 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1274 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1277 /* Context activation is done by the caller (state handler). */
1278 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1279 const struct wined3d_state *state)
1281 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1282 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1283 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1284 const struct wined3d_gl_info *gl_info = context->gl_info;
1285 struct shader_glsl_priv *priv = shader_priv;
1286 float position_fixup[4];
1287 DWORD update_mask = 0;
1289 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1290 UINT constant_version;
1291 int i;
1293 if (!prog) {
1294 /* No GLSL program set - nothing to do. */
1295 return;
1297 constant_version = prog->constant_version;
1298 update_mask = context->constant_update_mask & prog->constant_update_mask;
1300 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1301 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1302 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1304 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1305 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1306 vshader->reg_maps.integer_constants);
1308 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1309 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1310 vshader->reg_maps.boolean_constants);
1312 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1313 shader_glsl_pointsize_uniform(context, state, prog);
1315 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1317 shader_get_position_fixup(context, state, position_fixup);
1318 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1319 checkGLcall("glUniform4fv");
1322 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1324 struct wined3d_matrix mat;
1326 get_modelview_matrix(context, state, 0, &mat);
1327 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1328 checkGLcall("glUniformMatrix4fv");
1330 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1333 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1335 struct wined3d_matrix mat;
1337 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1339 if (prog->vs.modelview_matrix_location[i] == -1)
1340 break;
1342 get_modelview_matrix(context, state, i, &mat);
1343 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1344 checkGLcall("glUniformMatrix4fv");
1348 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1350 struct wined3d_matrix projection;
1352 get_projection_matrix(context, state, &projection);
1353 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1354 checkGLcall("glUniformMatrix4fv");
1357 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1359 for (i = 0; i < MAX_TEXTURES; ++i)
1360 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1363 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1364 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1366 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1368 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1369 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1370 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1373 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1374 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1375 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1377 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1378 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1379 pshader->reg_maps.integer_constants);
1381 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1382 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1383 pshader->reg_maps.boolean_constants);
1385 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1387 for (i = 0; i < MAX_TEXTURES; ++i)
1389 if (prog->ps.bumpenv_mat_location[i] == -1)
1390 continue;
1392 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1393 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1395 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1397 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1398 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1399 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1400 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1404 checkGLcall("bump env uniforms");
1407 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1409 float correction_params[] =
1411 /* position is window relative, not viewport relative */
1412 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1413 context->render_offscreen ? 1.0f : -1.0f,
1414 0.0f,
1415 0.0f,
1418 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1421 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1422 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1423 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1424 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1426 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1428 float col[4];
1430 if (prog->ps.tex_factor_location != -1)
1432 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1433 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1436 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1437 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1438 else
1439 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1441 for (i = 0; i < MAX_TEXTURES; ++i)
1443 if (prog->ps.tss_constant_location[i] == -1)
1444 continue;
1446 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1447 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1450 checkGLcall("fixed function uniforms");
1453 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1454 shader_glsl_load_fog_uniform(context, state, prog);
1456 if (priv->next_constant_version == UINT_MAX)
1458 TRACE("Max constant version reached, resetting to 0.\n");
1459 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1460 priv->next_constant_version = 1;
1462 else
1464 prog->constant_version = priv->next_constant_version++;
1468 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1470 struct constant_entry *entries = heap->entries;
1471 unsigned int *positions = heap->positions;
1472 unsigned int heap_idx, parent_idx;
1474 if (!heap->contained[idx])
1476 heap_idx = heap->size++;
1477 heap->contained[idx] = TRUE;
1479 else
1481 heap_idx = positions[idx];
1484 while (heap_idx > 1)
1486 parent_idx = heap_idx >> 1;
1488 if (new_version <= entries[parent_idx].version) break;
1490 entries[heap_idx] = entries[parent_idx];
1491 positions[entries[parent_idx].idx] = heap_idx;
1492 heap_idx = parent_idx;
1495 entries[heap_idx].version = new_version;
1496 entries[heap_idx].idx = idx;
1497 positions[idx] = heap_idx;
1500 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1502 struct shader_glsl_priv *priv = device->shader_priv;
1503 struct constant_heap *heap = &priv->vconst_heap;
1504 UINT i;
1506 for (i = start; i < count + start; ++i)
1508 update_heap_entry(heap, i, priv->next_constant_version);
1511 for (i = 0; i < device->context_count; ++i)
1513 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1517 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1519 struct shader_glsl_priv *priv = device->shader_priv;
1520 struct constant_heap *heap = &priv->pconst_heap;
1521 UINT i;
1523 for (i = start; i < count + start; ++i)
1525 update_heap_entry(heap, i, priv->next_constant_version);
1528 for (i = 0; i < device->context_count; ++i)
1530 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1534 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1536 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1537 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1538 if(shader_major > 3) return ret;
1540 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1541 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1542 return ret;
1545 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1547 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1550 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1551 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1553 va_list args;
1554 int ret;
1556 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1557 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1558 for (;;)
1560 va_start(args, format);
1561 ret = shader_vaddline(buffer, format, args);
1562 va_end(args);
1563 if (!ret)
1564 return;
1565 if (!string_buffer_resize(buffer, ret))
1566 return;
1570 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1571 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1573 va_list args;
1574 int ret;
1576 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1577 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1578 for (;;)
1580 va_start(args, format);
1581 ret = shader_vaddline(buffer, format, args);
1582 va_end(args);
1583 if (!ret)
1584 return;
1585 if (!string_buffer_resize(buffer, ret))
1586 return;
1590 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1592 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1593 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1594 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1595 unsigned int i;
1597 if (reg_maps->shader_version.major < 3)
1598 return input_reg_used[idx];
1600 for (i = 0; i < input_signature->element_count; ++i)
1602 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1604 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1605 continue;
1607 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1608 && input->semantic_idx == idx)
1610 if (input_reg_used[input->register_idx])
1611 return TRUE;
1612 else
1613 return FALSE;
1616 return FALSE;
1619 /** Generate the variable & register declarations for the GLSL output target */
1620 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1621 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1622 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1624 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1625 const struct wined3d_state *state = &shader->device->state;
1626 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1627 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1628 const struct wined3d_gl_info *gl_info = context->gl_info;
1629 const struct wined3d_fb_state *fb = &shader->device->fb;
1630 unsigned int i, extra_constants_needed = 0;
1631 const struct wined3d_shader_lconst *lconst;
1632 const char *prefix;
1633 DWORD map;
1635 prefix = shader_glsl_get_prefix(version->type);
1637 /* Prototype the subroutines */
1638 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1640 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1643 /* Declare the constants (aka uniforms) */
1644 if (shader->limits->constant_float > 0)
1646 unsigned max_constantsF;
1648 /* Unless the shader uses indirect addressing, always declare the
1649 * maximum array size and ignore that we need some uniforms privately.
1650 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1651 * and immediate values, still declare VC[256]. If the shader needs
1652 * more uniforms than we have it won't work in any case. If it uses
1653 * less, the compiler will figure out which uniforms are really used
1654 * and strip them out. This allows a shader to use c255 on a dx9 card,
1655 * as long as it doesn't also use all the other constants.
1657 * If the shader uses indirect addressing the compiler must assume
1658 * that all declared uniforms are used. In this case, declare only the
1659 * amount that we're assured to have.
1661 * Thus we run into problems in these two cases:
1662 * 1) The shader really uses more uniforms than supported.
1663 * 2) The shader uses indirect addressing, less constants than
1664 * supported, but uses a constant index > #supported consts. */
1665 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1667 /* No indirect addressing here. */
1668 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1670 else
1672 if (reg_maps->usesrelconstF)
1674 /* Subtract the other potential uniforms from the max
1675 * available (bools, ints, and 1 row of projection matrix).
1676 * Subtract another uniform for immediate values, which have
1677 * to be loaded via uniform by the driver as well. The shader
1678 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1679 * shader code, so one vec4 should be enough. (Unfortunately
1680 * the Nvidia driver doesn't store 128 and -128 in one float).
1682 * Writing gl_ClipVertex requires one uniform for each
1683 * clipplane as well. */
1684 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1685 if(ctx_priv->cur_vs_args->clip_enabled)
1687 max_constantsF -= gl_info->limits.clipplanes;
1689 max_constantsF -= count_bits(reg_maps->integer_constants);
1690 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1691 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1692 * for now take this into account when calculating the number of available constants
1694 max_constantsF -= count_bits(reg_maps->boolean_constants);
1695 /* Set by driver quirks in directx.c */
1696 max_constantsF -= gl_info->reserved_glsl_constants;
1698 if (max_constantsF < shader->limits->constant_float)
1700 static unsigned int once;
1702 if (!once++)
1703 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1704 " it may not render correctly.\n");
1705 else
1706 WARN("The hardware does not support enough uniform components to run this shader.\n");
1709 else
1711 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1714 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1715 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1718 /* Always declare the full set of constants, the compiler can remove the
1719 * unused ones because d3d doesn't (yet) support indirect int and bool
1720 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1721 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1722 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1724 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1725 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1727 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1729 if (reg_maps->cb_sizes[i])
1730 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1731 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1734 /* Declare texture samplers */
1735 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1737 struct wined3d_shader_sampler_map_entry *entry;
1738 BOOL shadow_sampler, tex_rect;
1739 const char *sampler_type;
1741 entry = &reg_maps->sampler_map.entries[i];
1743 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1745 ERR("Invalid resource index %u.\n", entry->resource_idx);
1746 continue;
1749 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << entry->sampler_idx));
1750 switch (reg_maps->resource_info[entry->resource_idx].type)
1752 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1753 if (shadow_sampler)
1754 sampler_type = "sampler1DShadow";
1755 else
1756 sampler_type = "sampler1D";
1757 break;
1759 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1760 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1761 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1762 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1763 if (shadow_sampler)
1765 if (tex_rect)
1766 sampler_type = "sampler2DRectShadow";
1767 else
1768 sampler_type = "sampler2DShadow";
1770 else
1772 if (tex_rect)
1773 sampler_type = "sampler2DRect";
1774 else
1775 sampler_type = "sampler2D";
1777 break;
1779 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1780 if (shadow_sampler)
1781 FIXME("Unsupported 3D shadow sampler.\n");
1782 sampler_type = "sampler3D";
1783 break;
1785 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1786 if (shadow_sampler)
1787 FIXME("Unsupported Cube shadow sampler.\n");
1788 sampler_type = "samplerCube";
1789 break;
1791 default:
1792 sampler_type = "unsupported_sampler";
1793 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1794 break;
1796 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1799 /* Declare uniforms for NP2 texcoord fixup:
1800 * This is NOT done inside the loop that declares the texture samplers
1801 * since the NP2 fixup code is currently only used for the GeforceFX
1802 * series and when forcing the ARB_npot extension off. Modern cards just
1803 * skip the code anyway, so put it inside a separate loop. */
1804 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1806 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1807 UINT cur = 0;
1809 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1810 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1811 * samplerNP2Fixup stores texture dimensions and is updated through
1812 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1814 for (i = 0; i < shader->limits->sampler; ++i)
1816 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1817 continue;
1819 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1821 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1822 continue;
1825 fixup->idx[i] = cur++;
1828 fixup->num_consts = (cur + 1) >> 1;
1829 fixup->active = ps_args->np2_fixup;
1830 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1833 /* Declare address variables */
1834 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1836 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1839 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1841 for (i = 0; i < shader->input_signature.element_count; ++i)
1843 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1844 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1845 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1846 prefix, e->register_idx);
1847 else
1848 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1851 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1853 shader_addline(buffer, "uniform struct\n{\n");
1854 shader_addline(buffer, " float size;\n");
1855 shader_addline(buffer, " float size_min;\n");
1856 shader_addline(buffer, " float size_max;\n");
1857 shader_addline(buffer, "} ffp_point;\n");
1860 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1862 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
1863 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
1864 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1865 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1868 shader_addline(buffer, "uniform vec4 posFixup;\n");
1869 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1871 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1873 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1875 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1877 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1879 shader_addline(buffer, "uniform struct\n{\n");
1880 shader_addline(buffer, " vec4 color;\n");
1881 shader_addline(buffer, " float density;\n");
1882 shader_addline(buffer, " float end;\n");
1883 shader_addline(buffer, " float scale;\n");
1884 shader_addline(buffer, "} ffp_fog;\n");
1886 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1888 if (glsl_is_color_reg_read(shader, 0))
1889 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1890 if (glsl_is_color_reg_read(shader, 1))
1891 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1892 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1893 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1895 else
1897 if (glsl_is_color_reg_read(shader, 0))
1898 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
1899 if (glsl_is_color_reg_read(shader, 1))
1900 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
1901 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1902 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1903 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1907 if (version->major >= 3)
1909 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1911 if (use_vs(state))
1912 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1913 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1916 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1918 if (!(map & 1))
1919 continue;
1921 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1923 if (reg_maps->luminanceparams & (1u << i))
1925 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1926 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1927 extra_constants_needed++;
1930 extra_constants_needed++;
1933 if (ps_args->srgb_correction)
1935 shader_addline(buffer, "const vec4 srgb_const0 = ");
1936 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1937 shader_addline(buffer, ";\n");
1938 shader_addline(buffer, "const vec4 srgb_const1 = ");
1939 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1940 shader_addline(buffer, ";\n");
1942 if (reg_maps->vpos || reg_maps->usesdsy)
1944 if (shader->limits->constant_float + extra_constants_needed
1945 + 1 < gl_info->limits.glsl_ps_float_constants)
1947 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1948 extra_constants_needed++;
1950 else
1952 float ycorrection[] =
1954 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1955 context->render_offscreen ? 1.0f : -1.0f,
1956 0.0f,
1957 0.0f,
1960 /* This happens because we do not have proper tracking of the
1961 * constant registers that are actually used, only the max
1962 * limit of the shader version. */
1963 FIXME("Cannot find a free uniform for vpos correction params\n");
1964 shader_addline(buffer, "const vec4 ycorrection = ");
1965 shader_glsl_append_imm_vec4(buffer, ycorrection);
1966 shader_addline(buffer, ";\n");
1968 shader_addline(buffer, "vec4 vpos;\n");
1972 /* Declare output register temporaries */
1973 if (shader->limits->packed_output)
1974 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1976 /* Declare temporary variables */
1977 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1979 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1982 /* Declare loop registers aLx */
1983 if (version->major < 4)
1985 for (i = 0; i < reg_maps->loop_depth; ++i)
1987 shader_addline(buffer, "int aL%u;\n", i);
1988 shader_addline(buffer, "int tmpInt%u;\n", i);
1992 /* Temporary variables for matrix operations */
1993 shader_addline(buffer, "vec4 tmp0;\n");
1994 shader_addline(buffer, "vec4 tmp1;\n");
1996 if (!shader->load_local_constsF)
1998 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2000 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2001 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2002 shader_addline(buffer, ";\n");
2006 /* Start the main program. */
2007 shader_addline(buffer, "void main()\n{\n");
2009 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2010 * add approximately 0.5. This causes off-by-one problems as spotted by
2011 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2012 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2013 * causes precision troubles when we just subtract 0.5.
2015 * To deal with that, just floor() the position. This will eliminate the
2016 * fraction on all cards.
2018 * TODO: Test how this behaves with multisampling.
2020 * An advantage of floor is that it works even if the driver doesn't add
2021 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2022 * to return in gl_FragCoord, even though coordinates specify the pixel
2023 * centers instead of the pixel corners. This code will behave correctly
2024 * on drivers that returns integer values. */
2025 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2027 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2028 shader_addline(buffer,
2029 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2030 else
2031 shader_addline(buffer,
2032 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2036 /*****************************************************************************
2037 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2039 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2040 ****************************************************************************/
2042 /* Prototypes */
2043 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2044 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2046 /** Used for opcode modifiers - They multiply the result by the specified amount */
2047 static const char * const shift_glsl_tab[] = {
2048 "", /* 0 (none) */
2049 "2.0 * ", /* 1 (x2) */
2050 "4.0 * ", /* 2 (x4) */
2051 "8.0 * ", /* 3 (x8) */
2052 "16.0 * ", /* 4 (x16) */
2053 "32.0 * ", /* 5 (x32) */
2054 "", /* 6 (x64) */
2055 "", /* 7 (x128) */
2056 "", /* 8 (d256) */
2057 "", /* 9 (d128) */
2058 "", /* 10 (d64) */
2059 "", /* 11 (d32) */
2060 "0.0625 * ", /* 12 (d16) */
2061 "0.125 * ", /* 13 (d8) */
2062 "0.25 * ", /* 14 (d4) */
2063 "0.5 * " /* 15 (d2) */
2066 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2067 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2068 const char *in_reg, const char *in_regswizzle, char *out_str)
2070 out_str[0] = 0;
2072 switch (src_modifier)
2074 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2075 case WINED3DSPSM_DW:
2076 case WINED3DSPSM_NONE:
2077 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2078 break;
2079 case WINED3DSPSM_NEG:
2080 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2081 break;
2082 case WINED3DSPSM_NOT:
2083 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2084 break;
2085 case WINED3DSPSM_BIAS:
2086 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2087 break;
2088 case WINED3DSPSM_BIASNEG:
2089 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2090 break;
2091 case WINED3DSPSM_SIGN:
2092 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2093 break;
2094 case WINED3DSPSM_SIGNNEG:
2095 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2096 break;
2097 case WINED3DSPSM_COMP:
2098 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2099 break;
2100 case WINED3DSPSM_X2:
2101 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2102 break;
2103 case WINED3DSPSM_X2NEG:
2104 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2105 break;
2106 case WINED3DSPSM_ABS:
2107 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2108 break;
2109 case WINED3DSPSM_ABSNEG:
2110 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2111 break;
2112 default:
2113 FIXME("Unhandled modifier %u\n", src_modifier);
2114 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2118 /** Writes the GLSL variable name that corresponds to the register that the
2119 * DX opcode parameter is trying to access */
2120 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2121 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2123 /* oPos, oFog and oPts in D3D */
2124 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2126 const struct wined3d_shader *shader = ins->ctx->shader;
2127 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2128 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2129 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2130 const char *prefix = shader_glsl_get_prefix(version->type);
2131 struct glsl_src_param rel_param0, rel_param1;
2132 char imm_str[4][17];
2134 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2135 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2136 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2137 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2138 *is_color = FALSE;
2140 switch (reg->type)
2142 case WINED3DSPR_TEMP:
2143 sprintf(register_name, "R%u", reg->idx[0].offset);
2144 break;
2146 case WINED3DSPR_INPUT:
2147 /* vertex shaders */
2148 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2150 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2151 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2152 *is_color = TRUE;
2153 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2154 break;
2157 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2159 if (reg->idx[0].rel_addr)
2161 if (reg->idx[1].rel_addr)
2162 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2163 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2164 else
2165 sprintf(register_name, "gs_in[%s + %u][%u]",
2166 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2168 else if (reg->idx[1].rel_addr)
2169 sprintf(register_name, "gs_in[%u][%s + %u]",
2170 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2171 else
2172 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2173 break;
2176 /* pixel shaders >= 3.0 */
2177 if (version->major >= 3)
2179 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2180 unsigned int in_count = vec4_varyings(version->major, gl_info);
2182 if (reg->idx[0].rel_addr)
2184 /* Removing a + 0 would be an obvious optimization, but
2185 * OS X doesn't see the NOP operation there. */
2186 if (idx)
2188 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2189 && shader->u.ps.declared_in_count > in_count)
2191 sprintf(register_name,
2192 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2193 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2194 prefix, rel_param0.param_str, idx);
2196 else
2198 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2201 else
2203 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2204 && shader->u.ps.declared_in_count > in_count)
2206 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2207 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2208 prefix, rel_param0.param_str);
2210 else
2212 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2216 else
2218 if (idx == in_count) sprintf(register_name, "gl_Color");
2219 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2220 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2223 else
2225 if (!reg->idx[0].offset)
2226 strcpy(register_name, "ffp_varying_diffuse");
2227 else
2228 strcpy(register_name, "ffp_varying_specular");
2229 break;
2231 break;
2233 case WINED3DSPR_CONST:
2235 /* Relative addressing */
2236 if (reg->idx[0].rel_addr)
2238 if (reg->idx[0].offset)
2239 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2240 else
2241 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2243 else
2245 if (shader_constant_is_local(shader, reg->idx[0].offset))
2246 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2247 else
2248 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2251 break;
2253 case WINED3DSPR_CONSTINT:
2254 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2255 break;
2257 case WINED3DSPR_CONSTBOOL:
2258 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2259 break;
2261 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2262 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2263 sprintf(register_name, "T%u", reg->idx[0].offset);
2264 else
2265 sprintf(register_name, "A%u", reg->idx[0].offset);
2266 break;
2268 case WINED3DSPR_LOOP:
2269 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2270 break;
2272 case WINED3DSPR_SAMPLER:
2273 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2274 break;
2276 case WINED3DSPR_COLOROUT:
2277 if (reg->idx[0].offset >= gl_info->limits.buffers)
2278 WARN("Write to render target %u, only %d supported.\n",
2279 reg->idx[0].offset, gl_info->limits.buffers);
2281 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2282 break;
2284 case WINED3DSPR_RASTOUT:
2285 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2286 break;
2288 case WINED3DSPR_DEPTHOUT:
2289 sprintf(register_name, "gl_FragDepth");
2290 break;
2292 case WINED3DSPR_ATTROUT:
2293 if (!reg->idx[0].offset)
2294 sprintf(register_name, "%s_out[8]", prefix);
2295 else
2296 sprintf(register_name, "%s_out[9]", prefix);
2297 break;
2299 case WINED3DSPR_TEXCRDOUT:
2300 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2301 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2302 break;
2304 case WINED3DSPR_MISCTYPE:
2305 if (!reg->idx[0].offset)
2307 /* vPos */
2308 sprintf(register_name, "vpos");
2310 else if (reg->idx[0].offset == 1)
2312 /* Note that gl_FrontFacing is a bool, while vFace is
2313 * a float for which the sign determines front/back */
2314 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2316 else
2318 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2319 sprintf(register_name, "unrecognized_register");
2321 break;
2323 case WINED3DSPR_IMMCONST:
2324 switch (reg->immconst_type)
2326 case WINED3D_IMMCONST_SCALAR:
2327 switch (reg->data_type)
2329 case WINED3D_DATA_FLOAT:
2330 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2331 break;
2332 case WINED3D_DATA_INT:
2333 sprintf(register_name, "%#x", reg->immconst_data[0]);
2334 break;
2335 case WINED3D_DATA_RESOURCE:
2336 case WINED3D_DATA_SAMPLER:
2337 case WINED3D_DATA_UINT:
2338 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2339 break;
2340 default:
2341 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2342 break;
2344 break;
2346 case WINED3D_IMMCONST_VEC4:
2347 switch (reg->data_type)
2349 case WINED3D_DATA_FLOAT:
2350 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2351 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2352 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2353 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2354 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2355 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2356 break;
2357 case WINED3D_DATA_INT:
2358 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2359 reg->immconst_data[0], reg->immconst_data[1],
2360 reg->immconst_data[2], reg->immconst_data[3]);
2361 break;
2362 case WINED3D_DATA_RESOURCE:
2363 case WINED3D_DATA_SAMPLER:
2364 case WINED3D_DATA_UINT:
2365 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2366 reg->immconst_data[0], reg->immconst_data[1],
2367 reg->immconst_data[2], reg->immconst_data[3]);
2368 break;
2369 default:
2370 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2371 break;
2373 break;
2375 default:
2376 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2377 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2379 break;
2381 case WINED3DSPR_CONSTBUFFER:
2382 if (reg->idx[1].rel_addr)
2383 sprintf(register_name, "%s_cb%u[%s + %u]",
2384 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2385 else
2386 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2387 break;
2389 case WINED3DSPR_PRIMID:
2390 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2391 break;
2393 default:
2394 FIXME("Unhandled register type %#x.\n", reg->type);
2395 sprintf(register_name, "unrecognized_register");
2396 break;
2400 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2402 *str++ = '.';
2403 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2404 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2405 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2406 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2407 *str = '\0';
2410 /* Get the GLSL write mask for the destination register */
2411 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2413 DWORD mask = param->write_mask;
2415 if (shader_is_scalar(&param->reg))
2417 mask = WINED3DSP_WRITEMASK_0;
2418 *write_mask = '\0';
2420 else
2422 shader_glsl_write_mask_to_str(mask, write_mask);
2425 return mask;
2428 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2429 unsigned int size = 0;
2431 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2432 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2433 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2434 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2436 return size;
2439 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2441 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2442 * but addressed as "rgba". To fix this we need to swap the register's x
2443 * and z components. */
2444 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2446 *str++ = '.';
2447 /* swizzle bits fields: wwzzyyxx */
2448 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2449 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2450 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2451 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2452 *str = '\0';
2455 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2456 BOOL fixup, DWORD mask, char *swizzle_str)
2458 if (shader_is_scalar(&param->reg))
2459 *swizzle_str = '\0';
2460 else
2461 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2464 /* From a given parameter token, generate the corresponding GLSL string.
2465 * Also, return the actual register name and swizzle in case the
2466 * caller needs this information as well. */
2467 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2468 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2470 BOOL is_color = FALSE;
2471 char swizzle_str[6];
2473 glsl_src->reg_name[0] = '\0';
2474 glsl_src->param_str[0] = '\0';
2475 swizzle_str[0] = '\0';
2477 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2478 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2480 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2482 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2484 else
2486 char reg_name[200];
2488 switch (wined3d_src->reg.data_type)
2490 case WINED3D_DATA_FLOAT:
2491 sprintf(reg_name, "%s", glsl_src->reg_name);
2492 break;
2493 case WINED3D_DATA_INT:
2494 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2495 break;
2496 case WINED3D_DATA_RESOURCE:
2497 case WINED3D_DATA_SAMPLER:
2498 case WINED3D_DATA_UINT:
2499 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2500 break;
2501 default:
2502 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2503 sprintf(reg_name, "%s", glsl_src->reg_name);
2504 break;
2507 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2511 /* From a given parameter token, generate the corresponding GLSL string.
2512 * Also, return the actual register name and swizzle in case the
2513 * caller needs this information as well. */
2514 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2515 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2517 BOOL is_color = FALSE;
2519 glsl_dst->mask_str[0] = '\0';
2520 glsl_dst->reg_name[0] = '\0';
2522 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2523 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2526 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2527 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2528 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2529 enum wined3d_data_type data_type)
2531 struct glsl_dst_param glsl_dst;
2532 DWORD mask;
2534 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2536 switch (data_type)
2538 case WINED3D_DATA_FLOAT:
2539 shader_addline(buffer, "%s%s = %s(",
2540 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2541 break;
2542 case WINED3D_DATA_INT:
2543 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2544 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2545 break;
2546 case WINED3D_DATA_RESOURCE:
2547 case WINED3D_DATA_SAMPLER:
2548 case WINED3D_DATA_UINT:
2549 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2550 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2551 break;
2552 default:
2553 FIXME("Unhandled data type %#x.\n", data_type);
2554 shader_addline(buffer, "%s%s = %s(",
2555 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2556 break;
2560 return mask;
2563 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2564 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2566 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2569 /** Process GLSL instruction modifiers */
2570 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2572 struct glsl_dst_param dst_param;
2573 DWORD modifiers;
2575 if (!ins->dst_count) return;
2577 modifiers = ins->dst[0].modifiers;
2578 if (!modifiers) return;
2580 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2582 if (modifiers & WINED3DSPDM_SATURATE)
2584 /* _SAT means to clamp the value of the register to between 0 and 1 */
2585 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2586 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2589 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2591 FIXME("_centroid modifier not handled\n");
2594 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2596 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2600 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2602 switch (op)
2604 case WINED3D_SHADER_REL_OP_GT: return ">";
2605 case WINED3D_SHADER_REL_OP_EQ: return "==";
2606 case WINED3D_SHADER_REL_OP_GE: return ">=";
2607 case WINED3D_SHADER_REL_OP_LT: return "<";
2608 case WINED3D_SHADER_REL_OP_NE: return "!=";
2609 case WINED3D_SHADER_REL_OP_LE: return "<=";
2610 default:
2611 FIXME("Unrecognized operator %#x.\n", op);
2612 return "(\?\?)";
2616 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2617 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2619 static const struct
2621 unsigned int coord_size;
2622 const char *type_part;
2624 resource_types[] =
2626 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2627 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2628 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2629 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2630 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2631 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2632 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2633 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2634 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2635 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2637 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2638 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2639 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2640 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2641 && (priv->cur_ps_args->shadow & (1u << resource_idx));
2642 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2643 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2644 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2645 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2646 const char *base = "texture", *type_part = "", *suffix = "";
2647 unsigned int coord_size;
2649 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2651 if (resource_type >= ARRAY_SIZE(resource_types))
2653 ERR("Unexpected resource type %#x.\n", resource_type);
2654 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2657 /* Note that there's no such thing as a projected cube texture. */
2658 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2659 projected = FALSE;
2661 if (needs_legacy_glsl_syntax(gl_info))
2663 if (shadow)
2664 base = "shadow";
2666 type_part = resource_types[resource_type].type_part;
2667 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2668 type_part = "2DRect";
2669 if (!type_part[0])
2670 FIXME("Unhandled resource type %#x.\n", resource_type);
2672 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2674 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2675 suffix = "ARB";
2676 else
2677 FIXME("Unsupported grad function.\n");
2681 sample_function->name = string_buffer_get(priv->string_buffers);
2682 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2683 lod ? "Lod" : grad ? "Grad" : "", suffix);
2685 coord_size = resource_types[resource_type].coord_size;
2686 if (shadow)
2687 ++coord_size;
2688 sample_function->coord_mask = (1u << coord_size) - 1;
2689 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2692 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2693 struct glsl_sample_function *sample_function)
2695 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2697 string_buffer_release(priv->string_buffers, sample_function->name);
2700 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2701 BOOL sign_fixup, enum fixup_channel_source channel_source)
2703 switch(channel_source)
2705 case CHANNEL_SOURCE_ZERO:
2706 strcat(arguments, "0.0");
2707 break;
2709 case CHANNEL_SOURCE_ONE:
2710 strcat(arguments, "1.0");
2711 break;
2713 case CHANNEL_SOURCE_X:
2714 strcat(arguments, reg_name);
2715 strcat(arguments, ".x");
2716 break;
2718 case CHANNEL_SOURCE_Y:
2719 strcat(arguments, reg_name);
2720 strcat(arguments, ".y");
2721 break;
2723 case CHANNEL_SOURCE_Z:
2724 strcat(arguments, reg_name);
2725 strcat(arguments, ".z");
2726 break;
2728 case CHANNEL_SOURCE_W:
2729 strcat(arguments, reg_name);
2730 strcat(arguments, ".w");
2731 break;
2733 default:
2734 FIXME("Unhandled channel source %#x\n", channel_source);
2735 strcat(arguments, "undefined");
2736 break;
2739 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2742 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2743 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2745 unsigned int mask_size, remaining;
2746 DWORD fixup_mask = 0;
2747 char arguments[256];
2748 char mask_str[6];
2750 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2751 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2752 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2753 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2754 if (!(mask &= fixup_mask))
2755 return;
2757 if (is_complex_fixup(fixup))
2759 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2760 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2761 return;
2764 shader_glsl_write_mask_to_str(mask, mask_str);
2765 mask_size = shader_glsl_get_write_mask_size(mask);
2767 arguments[0] = '\0';
2768 remaining = mask_size;
2769 if (mask & WINED3DSP_WRITEMASK_0)
2771 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2772 if (--remaining) strcat(arguments, ", ");
2774 if (mask & WINED3DSP_WRITEMASK_1)
2776 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2777 if (--remaining) strcat(arguments, ", ");
2779 if (mask & WINED3DSP_WRITEMASK_2)
2781 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2782 if (--remaining) strcat(arguments, ", ");
2784 if (mask & WINED3DSP_WRITEMASK_3)
2786 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2787 if (--remaining) strcat(arguments, ", ");
2790 if (mask_size > 1)
2791 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2792 else
2793 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2796 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2798 char reg_name[256];
2799 BOOL is_color;
2801 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2802 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2805 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2806 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2807 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2809 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2810 char dst_swizzle[6];
2811 struct color_fixup_desc fixup;
2812 BOOL np2_fixup = FALSE;
2813 va_list args;
2814 int ret;
2816 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2818 /* FIXME: We currently don't support fixups for vertex shaders or anything
2819 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2820 * the resource index. */
2821 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2823 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2824 fixup = priv->cur_ps_args->color_fixup[sampler];
2826 if (priv->cur_ps_args->np2_fixup & (1u << sampler))
2827 np2_fixup = TRUE;
2829 else
2831 fixup = COLOR_FIXUP_IDENTITY;
2834 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2836 if (sample_function->output_single_component)
2837 shader_addline(ins->ctx->buffer, "vec4(");
2839 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2840 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler);
2842 for (;;)
2844 va_start(args, coord_reg_fmt);
2845 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2846 va_end(args);
2847 if (!ret)
2848 break;
2849 if (!string_buffer_resize(ins->ctx->buffer, ret))
2850 break;
2853 if (np2_fixup)
2855 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2856 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2858 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2860 case 1:
2861 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2862 idx >> 1, (idx % 2) ? "z" : "x");
2863 break;
2864 case 2:
2865 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2866 idx >> 1, (idx % 2) ? "zw" : "xy");
2867 break;
2868 case 3:
2869 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2870 idx >> 1, (idx % 2) ? "zw" : "xy");
2871 break;
2872 case 4:
2873 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2874 idx >> 1, (idx % 2) ? "zw" : "xy");
2875 break;
2878 if (dx && dy)
2879 shader_addline(ins->ctx->buffer, ", %s, %s)", dx, dy);
2880 else if (bias)
2881 shader_addline(ins->ctx->buffer, ", %s)", bias);
2882 else
2883 shader_addline(ins->ctx->buffer, ")");
2885 if (sample_function->output_single_component)
2886 shader_addline(ins->ctx->buffer, ")");
2888 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2890 if (!is_identity_fixup(fixup))
2891 shader_glsl_color_correction(ins, fixup);
2894 /*****************************************************************************
2895 * Begin processing individual instruction opcodes
2896 ****************************************************************************/
2898 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2900 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2901 struct glsl_src_param src0_param;
2902 struct glsl_src_param src1_param;
2903 DWORD write_mask;
2904 const char *op;
2906 /* Determine the GLSL operator to use based on the opcode */
2907 switch (ins->handler_idx)
2909 case WINED3DSIH_ADD: op = "+"; break;
2910 case WINED3DSIH_AND: op = "&"; break;
2911 case WINED3DSIH_DIV: op = "/"; break;
2912 case WINED3DSIH_IADD: op = "+"; break;
2913 case WINED3DSIH_ISHL: op = "<<"; break;
2914 case WINED3DSIH_MUL: op = "*"; break;
2915 case WINED3DSIH_OR: op = "|"; break;
2916 case WINED3DSIH_SUB: op = "-"; break;
2917 case WINED3DSIH_USHR: op = ">>"; break;
2918 case WINED3DSIH_XOR: op = "^"; break;
2919 default:
2920 op = "<unhandled operator>";
2921 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2922 break;
2925 write_mask = shader_glsl_append_dst(buffer, ins);
2926 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2927 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2928 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2931 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2933 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2934 struct glsl_src_param src0_param;
2935 struct glsl_src_param src1_param;
2936 unsigned int mask_size;
2937 DWORD write_mask;
2938 const char *op;
2940 write_mask = shader_glsl_append_dst(buffer, ins);
2941 mask_size = shader_glsl_get_write_mask_size(write_mask);
2942 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2943 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2945 if (mask_size > 1)
2947 switch (ins->handler_idx)
2949 case WINED3DSIH_EQ: op = "equal"; break;
2950 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2951 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2952 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2953 case WINED3DSIH_LT: op = "lessThan"; break;
2954 case WINED3DSIH_NE: op = "notEqual"; break;
2955 default:
2956 op = "<unhandled operator>";
2957 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2958 break;
2961 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2962 mask_size, op, src0_param.param_str, src1_param.param_str);
2964 else
2966 switch (ins->handler_idx)
2968 case WINED3DSIH_EQ: op = "=="; break;
2969 case WINED3DSIH_GE: op = ">="; break;
2970 case WINED3DSIH_IGE: op = ">="; break;
2971 case WINED3DSIH_UGE: op = ">="; break;
2972 case WINED3DSIH_LT: op = "<"; break;
2973 case WINED3DSIH_NE: op = "!="; break;
2974 default:
2975 op = "<unhandled operator>";
2976 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2977 break;
2980 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2981 src0_param.param_str, op, src1_param.param_str);
2985 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2987 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2988 struct glsl_src_param src0_param;
2989 struct glsl_src_param src1_param;
2990 DWORD write_mask;
2992 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2993 * not, we can emulate it. */
2994 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2995 FIXME("64-bit integer multiplies not implemented.\n");
2997 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2999 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3000 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3001 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3003 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3004 src0_param.param_str, src1_param.param_str);
3008 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3010 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3011 struct glsl_src_param src0_param, src1_param;
3012 DWORD write_mask;
3014 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3017 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3019 char dst_mask[6];
3021 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3022 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3023 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3024 shader_addline(buffer, "tmp0%s = %s / %s;\n",
3025 dst_mask, src0_param.param_str, src1_param.param_str);
3027 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3028 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3029 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3030 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
3032 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3033 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3035 else
3037 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3038 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3039 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3040 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3043 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3045 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3046 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3047 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3048 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3052 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3053 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3055 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3056 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3057 struct glsl_src_param src0_param;
3058 DWORD write_mask;
3060 write_mask = shader_glsl_append_dst(buffer, ins);
3061 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3063 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3064 * shader versions WINED3DSIO_MOVA is used for this. */
3065 if (ins->ctx->reg_maps->shader_version.major == 1
3066 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3067 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3069 /* This is a simple floor() */
3070 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3071 if (mask_size > 1) {
3072 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3073 } else {
3074 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3077 else if(ins->handler_idx == WINED3DSIH_MOVA)
3079 /* We need to *round* to the nearest int here. */
3080 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3082 if (gl_info->supported[EXT_GPU_SHADER4])
3084 if (mask_size > 1)
3085 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3086 else
3087 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3089 else
3091 if (mask_size > 1)
3092 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3093 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3094 else
3095 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3096 src0_param.param_str, src0_param.param_str);
3099 else
3101 shader_addline(buffer, "%s);\n", src0_param.param_str);
3105 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3106 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3108 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3109 struct glsl_src_param src0_param;
3110 struct glsl_src_param src1_param;
3111 DWORD dst_write_mask, src_write_mask;
3112 unsigned int dst_size = 0;
3114 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3115 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3117 /* dp4 works on vec4, dp3 on vec3, etc. */
3118 if (ins->handler_idx == WINED3DSIH_DP4)
3119 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3120 else if (ins->handler_idx == WINED3DSIH_DP3)
3121 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3122 else
3123 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3125 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3126 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3128 if (dst_size > 1) {
3129 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3130 } else {
3131 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3135 /* Note that this instruction has some restrictions. The destination write mask
3136 * can't contain the w component, and the source swizzles have to be .xyzw */
3137 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3139 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3140 struct glsl_src_param src0_param;
3141 struct glsl_src_param src1_param;
3142 char dst_mask[6];
3144 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3145 shader_glsl_append_dst(ins->ctx->buffer, ins);
3146 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3147 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3148 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3151 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3153 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3156 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3157 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3158 * GLSL uses the value as-is. */
3159 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3161 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3162 struct glsl_src_param src0_param;
3163 struct glsl_src_param src1_param;
3164 DWORD dst_write_mask;
3165 unsigned int dst_size;
3167 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3168 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3170 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3171 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3173 if (dst_size > 1)
3175 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3176 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3178 else
3180 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3181 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3185 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3186 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3188 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3189 struct glsl_src_param src_param;
3190 const char *instruction;
3191 DWORD write_mask;
3192 unsigned i;
3194 /* Determine the GLSL function to use based on the opcode */
3195 /* TODO: Possibly make this a table for faster lookups */
3196 switch (ins->handler_idx)
3198 case WINED3DSIH_MIN: instruction = "min"; break;
3199 case WINED3DSIH_MAX: instruction = "max"; break;
3200 case WINED3DSIH_ABS: instruction = "abs"; break;
3201 case WINED3DSIH_FRC: instruction = "fract"; break;
3202 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3203 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3204 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3205 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3206 default: instruction = "";
3207 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3208 break;
3211 write_mask = shader_glsl_append_dst(buffer, ins);
3213 shader_addline(buffer, "%s(", instruction);
3215 if (ins->src_count)
3217 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3218 shader_addline(buffer, "%s", src_param.param_str);
3219 for (i = 1; i < ins->src_count; ++i)
3221 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3222 shader_addline(buffer, ", %s", src_param.param_str);
3226 shader_addline(buffer, "));\n");
3229 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3231 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3233 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3234 struct glsl_src_param src_param;
3235 unsigned int mask_size;
3236 DWORD write_mask;
3237 char dst_mask[6];
3239 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3240 mask_size = shader_glsl_get_write_mask_size(write_mask);
3241 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3243 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3244 src_param.param_str, src_param.param_str);
3245 shader_glsl_append_dst(buffer, ins);
3247 if (mask_size > 1)
3249 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3250 mask_size, src_param.param_str);
3252 else
3254 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3255 src_param.param_str);
3259 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3261 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3262 struct glsl_src_param src0_param;
3263 const char *prefix, *suffix;
3264 unsigned int dst_size;
3265 DWORD dst_write_mask;
3267 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3268 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3270 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3272 switch (ins->handler_idx)
3274 case WINED3DSIH_EXP:
3275 case WINED3DSIH_EXPP:
3276 prefix = "exp2(";
3277 suffix = ")";
3278 break;
3280 case WINED3DSIH_LOG:
3281 case WINED3DSIH_LOGP:
3282 prefix = "log2(abs(";
3283 suffix = "))";
3284 break;
3286 case WINED3DSIH_RCP:
3287 prefix = "1.0 / ";
3288 suffix = "";
3289 break;
3291 case WINED3DSIH_RSQ:
3292 prefix = "inversesqrt(abs(";
3293 suffix = "))";
3294 break;
3296 default:
3297 prefix = "";
3298 suffix = "";
3299 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3300 break;
3303 if (dst_size > 1)
3304 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3305 else
3306 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3309 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3310 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3311 * dst.x = 2^(floor(src))
3312 * dst.y = src - floor(src)
3313 * dst.z = 2^src (partial precision is allowed, but optional)
3314 * dst.w = 1.0;
3315 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3316 * dst = 2^src; (partial precision is allowed, but optional)
3318 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3320 if (ins->ctx->reg_maps->shader_version.major < 2)
3322 struct glsl_src_param src_param;
3323 char dst_mask[6];
3325 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3327 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3328 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3329 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3330 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3332 shader_glsl_append_dst(ins->ctx->buffer, ins);
3333 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3334 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3335 return;
3338 shader_glsl_scalar_op(ins);
3341 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3343 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3344 struct glsl_src_param src_param;
3345 unsigned int mask_size;
3346 DWORD write_mask;
3348 write_mask = shader_glsl_append_dst(buffer, ins);
3349 mask_size = shader_glsl_get_write_mask_size(write_mask);
3350 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3352 if (mask_size > 1)
3353 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3354 else
3355 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3358 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3360 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3361 struct glsl_src_param src_param;
3362 unsigned int mask_size;
3363 DWORD write_mask;
3365 write_mask = shader_glsl_append_dst(buffer, ins);
3366 mask_size = shader_glsl_get_write_mask_size(write_mask);
3367 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3369 if (mask_size > 1)
3370 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3371 else
3372 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3375 /** Process signed comparison opcodes in GLSL. */
3376 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3378 struct glsl_src_param src0_param;
3379 struct glsl_src_param src1_param;
3380 DWORD write_mask;
3381 unsigned int mask_size;
3383 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3384 mask_size = shader_glsl_get_write_mask_size(write_mask);
3385 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3386 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3388 if (mask_size > 1) {
3389 const char *compare;
3391 switch(ins->handler_idx)
3393 case WINED3DSIH_SLT: compare = "lessThan"; break;
3394 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3395 default: compare = "";
3396 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3399 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3400 src0_param.param_str, src1_param.param_str);
3401 } else {
3402 switch(ins->handler_idx)
3404 case WINED3DSIH_SLT:
3405 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3406 * to return 0.0 but step returns 1.0 because step is not < x
3407 * An alternative is a bvec compare padded with an unused second component.
3408 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3409 * issue. Playing with not() is not possible either because not() does not accept
3410 * a scalar.
3412 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3413 src0_param.param_str, src1_param.param_str);
3414 break;
3415 case WINED3DSIH_SGE:
3416 /* Here we can use the step() function and safe a conditional */
3417 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3418 break;
3419 default:
3420 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3426 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3428 const char *condition_prefix, *condition_suffix;
3429 struct wined3d_shader_dst_param dst;
3430 struct glsl_src_param src0_param;
3431 struct glsl_src_param src1_param;
3432 struct glsl_src_param src2_param;
3433 BOOL temp_destination = FALSE;
3434 DWORD cmp_channel = 0;
3435 unsigned int i, j;
3436 char mask_char[6];
3437 DWORD write_mask;
3439 switch (ins->handler_idx)
3441 case WINED3DSIH_CMP:
3442 condition_prefix = "";
3443 condition_suffix = " >= 0.0";
3444 break;
3446 case WINED3DSIH_CND:
3447 condition_prefix = "";
3448 condition_suffix = " > 0.5";
3449 break;
3451 case WINED3DSIH_MOVC:
3452 condition_prefix = "bool(";
3453 condition_suffix = ")";
3454 break;
3456 default:
3457 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3458 condition_prefix = "<unhandled prefix>";
3459 condition_suffix = "<unhandled suffix>";
3460 break;
3463 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3465 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3466 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3467 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3468 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3470 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3471 condition_prefix, src0_param.param_str, condition_suffix,
3472 src1_param.param_str, src2_param.param_str);
3473 return;
3476 dst = ins->dst[0];
3478 /* Splitting the instruction up in multiple lines imposes a problem:
3479 * The first lines may overwrite source parameters of the following lines.
3480 * Deal with that by using a temporary destination register if needed. */
3481 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3482 && ins->src[0].reg.type == dst.reg.type)
3483 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3484 && ins->src[1].reg.type == dst.reg.type)
3485 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3486 && ins->src[2].reg.type == dst.reg.type))
3487 temp_destination = TRUE;
3489 /* Cycle through all source0 channels. */
3490 for (i = 0; i < 4; ++i)
3492 write_mask = 0;
3493 /* Find the destination channels which use the current source0 channel. */
3494 for (j = 0; j < 4; ++j)
3496 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3498 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3499 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3502 dst.write_mask = ins->dst[0].write_mask & write_mask;
3504 if (temp_destination)
3506 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3507 continue;
3508 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3510 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3511 continue;
3513 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3514 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3515 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3517 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3518 condition_prefix, src0_param.param_str, condition_suffix,
3519 src1_param.param_str, src2_param.param_str);
3522 if (temp_destination)
3524 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3525 shader_glsl_append_dst(ins->ctx->buffer, ins);
3526 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3530 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3531 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3532 * the compare is done per component of src0. */
3533 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3535 struct glsl_src_param src0_param;
3536 struct glsl_src_param src1_param;
3537 struct glsl_src_param src2_param;
3538 DWORD write_mask;
3539 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3540 ins->ctx->reg_maps->shader_version.minor);
3542 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3544 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3546 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3547 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3549 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3550 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3551 else
3552 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3553 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3554 return;
3557 shader_glsl_conditional_move(ins);
3560 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3561 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3563 struct glsl_src_param src0_param;
3564 struct glsl_src_param src1_param;
3565 struct glsl_src_param src2_param;
3566 DWORD write_mask;
3568 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3569 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3570 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3571 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3572 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3573 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3576 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3577 Vertex shaders to GLSL codes */
3578 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3580 int i;
3581 int nComponents = 0;
3582 struct wined3d_shader_dst_param tmp_dst = {{0}};
3583 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3584 struct wined3d_shader_instruction tmp_ins;
3586 memset(&tmp_ins, 0, sizeof(tmp_ins));
3588 /* Set constants for the temporary argument */
3589 tmp_ins.ctx = ins->ctx;
3590 tmp_ins.dst_count = 1;
3591 tmp_ins.dst = &tmp_dst;
3592 tmp_ins.src_count = 2;
3593 tmp_ins.src = tmp_src;
3595 switch(ins->handler_idx)
3597 case WINED3DSIH_M4x4:
3598 nComponents = 4;
3599 tmp_ins.handler_idx = WINED3DSIH_DP4;
3600 break;
3601 case WINED3DSIH_M4x3:
3602 nComponents = 3;
3603 tmp_ins.handler_idx = WINED3DSIH_DP4;
3604 break;
3605 case WINED3DSIH_M3x4:
3606 nComponents = 4;
3607 tmp_ins.handler_idx = WINED3DSIH_DP3;
3608 break;
3609 case WINED3DSIH_M3x3:
3610 nComponents = 3;
3611 tmp_ins.handler_idx = WINED3DSIH_DP3;
3612 break;
3613 case WINED3DSIH_M3x2:
3614 nComponents = 2;
3615 tmp_ins.handler_idx = WINED3DSIH_DP3;
3616 break;
3617 default:
3618 break;
3621 tmp_dst = ins->dst[0];
3622 tmp_src[0] = ins->src[0];
3623 tmp_src[1] = ins->src[1];
3624 for (i = 0; i < nComponents; ++i)
3626 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3627 shader_glsl_dot(&tmp_ins);
3628 ++tmp_src[1].reg.idx[0].offset;
3633 The LRP instruction performs a component-wise linear interpolation
3634 between the second and third operands using the first operand as the
3635 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3636 This is equivalent to mix(src2, src1, src0);
3638 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3640 struct glsl_src_param src0_param;
3641 struct glsl_src_param src1_param;
3642 struct glsl_src_param src2_param;
3643 DWORD write_mask;
3645 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3647 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &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, "mix(%s, %s, %s));\n",
3652 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3655 /** Process the WINED3DSIO_LIT instruction in GLSL:
3656 * dst.x = dst.w = 1.0
3657 * dst.y = (src0.x > 0) ? src0.x
3658 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3659 * where src.w is clamped at +- 128
3661 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3663 struct glsl_src_param src0_param;
3664 struct glsl_src_param src1_param;
3665 struct glsl_src_param src3_param;
3666 char dst_mask[6];
3668 shader_glsl_append_dst(ins->ctx->buffer, ins);
3669 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3671 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3672 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3673 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3675 /* The sdk specifies the instruction like this
3676 * dst.x = 1.0;
3677 * if(src.x > 0.0) dst.y = src.x
3678 * else dst.y = 0.0.
3679 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3680 * else dst.z = 0.0;
3681 * dst.w = 1.0;
3682 * (where power = src.w clamped between -128 and 128)
3684 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3685 * dst.x = 1.0 ... No further explanation needed
3686 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3687 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3688 * dst.w = 1.0. ... Nothing fancy.
3690 * So we still have one conditional in there. So do this:
3691 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3693 * 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),
3694 * 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.
3695 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3697 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3698 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3699 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3701 shader_addline(ins->ctx->buffer,
3702 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3703 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3704 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3705 src0_param.param_str, src3_param.param_str, dst_mask);
3708 /** Process the WINED3DSIO_DST instruction in GLSL:
3709 * dst.x = 1.0
3710 * dst.y = src0.x * src0.y
3711 * dst.z = src0.z
3712 * dst.w = src1.w
3714 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3716 struct glsl_src_param src0y_param;
3717 struct glsl_src_param src0z_param;
3718 struct glsl_src_param src1y_param;
3719 struct glsl_src_param src1w_param;
3720 char dst_mask[6];
3722 shader_glsl_append_dst(ins->ctx->buffer, ins);
3723 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3725 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3726 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3727 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3728 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3730 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3731 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3734 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3735 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3736 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3738 * dst.x = cos(src0.?)
3739 * dst.y = sin(src0.?)
3740 * dst.z = dst.z
3741 * dst.w = dst.w
3743 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3745 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3746 struct glsl_src_param src0_param;
3747 DWORD write_mask;
3749 if (ins->ctx->reg_maps->shader_version.major < 4)
3751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3753 write_mask = shader_glsl_append_dst(buffer, ins);
3754 switch (write_mask)
3756 case WINED3DSP_WRITEMASK_0:
3757 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3758 break;
3760 case WINED3DSP_WRITEMASK_1:
3761 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3762 break;
3764 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3765 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3766 src0_param.param_str, src0_param.param_str);
3767 break;
3769 default:
3770 ERR("Write mask should be .x, .y or .xy\n");
3771 break;
3774 return;
3777 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3780 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3782 char dst_mask[6];
3784 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3785 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3786 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3788 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3789 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3790 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3792 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3793 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3795 else
3797 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3798 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3799 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3802 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3804 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3805 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3806 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3810 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3811 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3812 * generate invalid code
3814 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3816 struct glsl_src_param src0_param;
3817 DWORD write_mask;
3819 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3820 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3822 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3825 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3826 * Start a for() loop where src1.y is the initial value of aL,
3827 * increment aL by src1.z for a total of src1.x iterations.
3828 * Need to use a temporary variable for this operation.
3830 /* FIXME: I don't think nested loops will work correctly this way. */
3831 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3833 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3834 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3835 const struct wined3d_shader *shader = ins->ctx->shader;
3836 const struct wined3d_shader_lconst *constant;
3837 struct glsl_src_param src1_param;
3838 const DWORD *control_values = NULL;
3840 if (ins->ctx->reg_maps->shader_version.major < 4)
3842 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3844 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3845 * class hardware doesn't support real varying indexing, but Microsoft
3846 * designed this feature for Shader model 2.x+. If the loop control is
3847 * known at compile time, the GLSL compiler can unroll the loop, and
3848 * replace indirect addressing with direct addressing. */
3849 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3851 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3853 if (constant->idx == ins->src[1].reg.idx[0].offset)
3855 control_values = constant->value;
3856 break;
3861 if (control_values)
3863 struct wined3d_shader_loop_control loop_control;
3864 loop_control.count = control_values[0];
3865 loop_control.start = control_values[1];
3866 loop_control.step = (int)control_values[2];
3868 if (loop_control.step > 0)
3870 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3871 loop_state->current_depth, loop_control.start,
3872 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3873 loop_state->current_depth, loop_control.step);
3875 else if (loop_control.step < 0)
3877 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3878 loop_state->current_depth, loop_control.start,
3879 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3880 loop_state->current_depth, loop_control.step);
3882 else
3884 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3885 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3886 loop_state->current_depth, loop_control.count,
3887 loop_state->current_depth);
3890 else
3892 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3893 loop_state->current_depth, loop_state->current_reg,
3894 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3895 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3898 ++loop_state->current_reg;
3900 else
3902 shader_addline(buffer, "for (;;)\n{\n");
3905 ++loop_state->current_depth;
3908 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3910 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3912 shader_addline(ins->ctx->buffer, "}\n");
3914 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3916 --loop_state->current_depth;
3917 --loop_state->current_reg;
3920 if (ins->handler_idx == WINED3DSIH_ENDREP)
3922 --loop_state->current_depth;
3926 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3928 const struct wined3d_shader *shader = ins->ctx->shader;
3929 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3930 const struct wined3d_shader_lconst *constant;
3931 struct glsl_src_param src0_param;
3932 const DWORD *control_values = NULL;
3934 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3935 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3937 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3939 if (constant->idx == ins->src[0].reg.idx[0].offset)
3941 control_values = constant->value;
3942 break;
3947 if (control_values)
3949 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3950 loop_state->current_depth, loop_state->current_depth,
3951 control_values[0], loop_state->current_depth);
3953 else
3955 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3956 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3957 loop_state->current_depth, loop_state->current_depth,
3958 src0_param.param_str, loop_state->current_depth);
3961 ++loop_state->current_depth;
3964 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3966 struct glsl_src_param src0_param;
3968 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3969 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3972 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3974 struct glsl_src_param src0_param;
3975 struct glsl_src_param src1_param;
3977 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3978 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3980 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3981 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3984 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3986 shader_addline(ins->ctx->buffer, "} else {\n");
3989 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3991 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3994 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3996 shader_addline(ins->ctx->buffer, "break;\n");
3999 /* FIXME: According to MSDN the compare is done per component. */
4000 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4002 struct glsl_src_param src0_param;
4003 struct glsl_src_param src1_param;
4005 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4006 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4008 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4009 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4012 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4014 struct glsl_src_param src_param;
4016 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4017 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4020 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4022 shader_addline(ins->ctx->buffer, "}\n");
4023 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4026 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4028 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4031 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4033 struct glsl_src_param src1_param;
4035 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4036 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4037 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4040 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4042 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4043 * function only suppresses the unhandled instruction warning
4047 /*********************************************
4048 * Pixel Shader Specific Code begins here
4049 ********************************************/
4050 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4052 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4053 ins->ctx->reg_maps->shader_version.minor);
4054 struct glsl_sample_function sample_function;
4055 DWORD sample_flags = 0;
4056 DWORD resource_idx;
4057 DWORD mask = 0, swizzle;
4058 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4060 /* 1.0-1.4: Use destination register as sampler source.
4061 * 2.0+: Use provided sampler source. */
4062 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4063 resource_idx = ins->dst[0].reg.idx[0].offset;
4064 else
4065 resource_idx = ins->src[1].reg.idx[0].offset;
4067 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4069 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4070 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4071 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4073 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4074 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4076 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4077 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4079 case WINED3D_TTFF_COUNT1:
4080 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4081 break;
4082 case WINED3D_TTFF_COUNT2:
4083 mask = WINED3DSP_WRITEMASK_1;
4084 break;
4085 case WINED3D_TTFF_COUNT3:
4086 mask = WINED3DSP_WRITEMASK_2;
4087 break;
4088 case WINED3D_TTFF_COUNT4:
4089 case WINED3D_TTFF_DISABLE:
4090 mask = WINED3DSP_WRITEMASK_3;
4091 break;
4095 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4097 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4099 if (src_mod == WINED3DSPSM_DZ) {
4100 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4101 mask = WINED3DSP_WRITEMASK_2;
4102 } else if (src_mod == WINED3DSPSM_DW) {
4103 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4104 mask = WINED3DSP_WRITEMASK_3;
4107 else
4109 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4110 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4112 /* ps 2.0 texldp instruction always divides by the fourth component. */
4113 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4114 mask = WINED3DSP_WRITEMASK_3;
4118 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4119 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4121 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4122 mask |= sample_function.coord_mask;
4123 sample_function.coord_mask = mask;
4125 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4126 else swizzle = ins->src[1].swizzle;
4128 /* 1.0-1.3: Use destination register as coordinate source.
4129 1.4+: Use provided coordinate source register. */
4130 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4132 char coord_mask[6];
4133 shader_glsl_write_mask_to_str(mask, coord_mask);
4134 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4135 "T%u%s", resource_idx, coord_mask);
4137 else
4139 struct glsl_src_param coord_param;
4140 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4141 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4143 struct glsl_src_param bias;
4144 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4145 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4146 "%s", coord_param.param_str);
4147 } else {
4148 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4149 "%s", coord_param.param_str);
4152 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4155 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4157 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4158 struct glsl_src_param coord_param, dx_param, dy_param;
4159 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4160 struct glsl_sample_function sample_function;
4161 DWORD sampler_idx;
4162 DWORD swizzle = ins->src[1].swizzle;
4163 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4165 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4167 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4168 shader_glsl_tex(ins);
4169 return;
4172 sampler_idx = ins->src[1].reg.idx[0].offset;
4173 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4174 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4176 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4177 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4178 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4179 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4181 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4182 "%s", coord_param.param_str);
4183 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4186 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4188 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4189 struct glsl_src_param coord_param, lod_param;
4190 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4191 struct glsl_sample_function sample_function;
4192 DWORD sampler_idx;
4193 DWORD swizzle = ins->src[1].swizzle;
4194 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4196 sampler_idx = ins->src[1].reg.idx[0].offset;
4197 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4198 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4199 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4201 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4202 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4204 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4206 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4207 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4209 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4210 * However, the NVIDIA drivers allow them in fragment shaders as well,
4211 * even without the appropriate extension. */
4212 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4214 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4215 "%s", coord_param.param_str);
4216 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4219 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4220 unsigned int resource_idx, unsigned int sampler_idx)
4222 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4223 unsigned int i;
4225 for (i = 0; i < sampler_map->count; ++i)
4227 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4228 return entries[i].bind_idx;
4231 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4233 return ~0u;
4236 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4238 struct glsl_sample_function sample_function;
4239 struct glsl_src_param coord_param;
4240 unsigned int sampler_idx;
4242 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4243 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4244 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4245 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4246 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4247 NULL, NULL, NULL, "%s", coord_param.param_str);
4248 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4251 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4253 /* FIXME: Make this work for more than just 2D textures */
4254 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4255 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4257 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4259 char dst_mask[6];
4261 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4262 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4263 ins->dst[0].reg.idx[0].offset, dst_mask);
4265 else
4267 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4268 DWORD reg = ins->src[0].reg.idx[0].offset;
4269 char dst_swizzle[6];
4271 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4273 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4275 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4276 struct glsl_src_param div_param;
4277 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4279 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4281 if (mask_size > 1)
4282 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4283 else
4284 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4286 else
4288 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4293 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4294 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4295 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4296 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4298 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4299 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4300 struct glsl_sample_function sample_function;
4301 struct glsl_src_param src0_param;
4302 UINT mask_size;
4304 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4306 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4307 * scalar, and projected sampling would require 4.
4309 * It is a dependent read - not valid with conditional NP2 textures
4311 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4312 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4314 switch(mask_size)
4316 case 1:
4317 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4318 "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4319 break;
4321 case 2:
4322 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4323 "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4324 break;
4326 case 3:
4327 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4328 "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4329 break;
4331 default:
4332 FIXME("Unexpected mask size %u\n", mask_size);
4333 break;
4335 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4338 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4339 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4340 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4342 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4343 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4344 struct glsl_src_param src0_param;
4345 DWORD dst_mask;
4346 unsigned int mask_size;
4348 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4349 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4350 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4352 if (mask_size > 1) {
4353 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4354 } else {
4355 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4359 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4360 * Calculate the depth as dst.x / dst.y */
4361 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4363 struct glsl_dst_param dst_param;
4365 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4367 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4368 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4369 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4370 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4371 * >= 1.0 or < 0.0
4373 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4374 dst_param.reg_name, dst_param.reg_name);
4377 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4378 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4379 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4380 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4382 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4384 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4385 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4386 struct glsl_src_param src0_param;
4388 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4390 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4391 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4394 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4395 * Calculate the 1st of a 2-row matrix multiplication. */
4396 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4398 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4399 DWORD reg = ins->dst[0].reg.idx[0].offset;
4400 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4401 struct glsl_src_param src0_param;
4403 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4404 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4407 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4408 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4409 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4411 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4412 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4413 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4414 DWORD reg = ins->dst[0].reg.idx[0].offset;
4415 struct glsl_src_param src0_param;
4417 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4418 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4419 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4422 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4424 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4425 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4426 struct glsl_sample_function sample_function;
4427 DWORD reg = ins->dst[0].reg.idx[0].offset;
4428 struct glsl_src_param src0_param;
4430 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4431 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4433 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4435 /* Sample the texture using the calculated coordinates */
4436 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4437 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4440 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4441 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4442 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4444 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4445 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4446 struct glsl_sample_function sample_function;
4447 DWORD reg = ins->dst[0].reg.idx[0].offset;
4448 struct glsl_src_param src0_param;
4450 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4451 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4453 /* Dependent read, not valid with conditional NP2 */
4454 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4456 /* Sample the texture using the calculated coordinates */
4457 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4458 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4460 tex_mx->current_row = 0;
4463 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4464 * Perform the 3rd row of a 3x3 matrix multiply */
4465 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4467 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4468 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4469 DWORD reg = ins->dst[0].reg.idx[0].offset;
4470 struct glsl_src_param src0_param;
4471 char dst_mask[6];
4473 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4475 shader_glsl_append_dst(ins->ctx->buffer, ins);
4476 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4477 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4479 tex_mx->current_row = 0;
4482 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4483 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4484 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4486 struct glsl_src_param src0_param;
4487 struct glsl_src_param src1_param;
4488 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4489 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4490 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4491 struct glsl_sample_function sample_function;
4492 DWORD reg = ins->dst[0].reg.idx[0].offset;
4493 char coord_mask[6];
4495 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4496 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4498 /* Perform the last matrix multiply operation */
4499 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4500 /* Reflection calculation */
4501 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4503 /* Dependent read, not valid with conditional NP2 */
4504 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4505 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4507 /* Sample the texture */
4508 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4509 NULL, NULL, NULL, "tmp0%s", coord_mask);
4510 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4512 tex_mx->current_row = 0;
4515 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4516 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4517 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4519 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4520 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4521 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4522 struct glsl_sample_function sample_function;
4523 DWORD reg = ins->dst[0].reg.idx[0].offset;
4524 struct glsl_src_param src0_param;
4525 char coord_mask[6];
4527 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4529 /* Perform the last matrix multiply operation */
4530 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4532 /* Construct the eye-ray vector from w coordinates */
4533 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4534 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4535 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4537 /* Dependent read, not valid with conditional NP2 */
4538 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4539 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4541 /* Sample the texture using the calculated coordinates */
4542 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4543 NULL, NULL, NULL, "tmp0%s", coord_mask);
4544 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4546 tex_mx->current_row = 0;
4549 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4550 * Apply a fake bump map transform.
4551 * texbem is pshader <= 1.3 only, this saves a few version checks
4553 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4555 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4556 struct glsl_sample_function sample_function;
4557 struct glsl_src_param coord_param;
4558 DWORD sampler_idx;
4559 DWORD mask;
4560 DWORD flags;
4561 char coord_mask[6];
4563 sampler_idx = ins->dst[0].reg.idx[0].offset;
4564 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4565 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4567 /* Dependent read, not valid with conditional NP2 */
4568 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4569 mask = sample_function.coord_mask;
4571 shader_glsl_write_mask_to_str(mask, coord_mask);
4573 /* With projected textures, texbem only divides the static texture coord,
4574 * not the displacement, so we can't let GL handle this. */
4575 if (flags & WINED3D_PSARGS_PROJECTED)
4577 DWORD div_mask=0;
4578 char coord_div_mask[3];
4579 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4581 case WINED3D_TTFF_COUNT1:
4582 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4583 break;
4584 case WINED3D_TTFF_COUNT2:
4585 div_mask = WINED3DSP_WRITEMASK_1;
4586 break;
4587 case WINED3D_TTFF_COUNT3:
4588 div_mask = WINED3DSP_WRITEMASK_2;
4589 break;
4590 case WINED3D_TTFF_COUNT4:
4591 case WINED3D_TTFF_DISABLE:
4592 div_mask = WINED3DSP_WRITEMASK_3;
4593 break;
4595 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4596 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4599 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4601 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4602 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4603 coord_param.param_str, coord_mask);
4605 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4607 struct glsl_src_param luminance_param;
4608 struct glsl_dst_param dst_param;
4610 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4611 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4613 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4614 dst_param.reg_name, dst_param.mask_str,
4615 luminance_param.param_str, sampler_idx, sampler_idx);
4617 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4620 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4622 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4623 struct glsl_src_param src0_param, src1_param;
4625 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4626 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4628 shader_glsl_append_dst(ins->ctx->buffer, ins);
4629 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4630 src0_param.param_str, sampler_idx, src1_param.param_str);
4633 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4634 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4635 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4637 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4638 struct glsl_sample_function sample_function;
4639 struct glsl_src_param src0_param;
4641 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4643 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4644 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4645 "%s.wx", src0_param.reg_name);
4646 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4649 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4650 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4651 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4653 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4654 struct glsl_sample_function sample_function;
4655 struct glsl_src_param src0_param;
4657 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4659 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4660 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4661 "%s.yz", src0_param.reg_name);
4662 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4665 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4666 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4667 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4669 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4670 struct glsl_sample_function sample_function;
4671 struct glsl_src_param src0_param;
4673 /* Dependent read, not valid with conditional NP2 */
4674 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4675 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4677 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4678 "%s", src0_param.param_str);
4679 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4682 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4683 * If any of the first 3 components are < 0, discard this pixel */
4684 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4686 if (ins->ctx->reg_maps->shader_version.major >= 4)
4688 struct glsl_src_param src_param;
4690 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4691 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4693 else
4695 struct glsl_dst_param dst_param;
4697 /* The argument is a destination parameter, and no writemasks are allowed */
4698 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4700 /* 2.0 shaders compare all 4 components in texkill. */
4701 if (ins->ctx->reg_maps->shader_version.major >= 2)
4702 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4703 /* 1.x shaders only compare the first 3 components, probably due to
4704 * the nature of the texkill instruction as a tex* instruction, and
4705 * phase, which kills all .w components. Even if all 4 components are
4706 * defined, only the first 3 are used. */
4707 else
4708 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4712 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4713 * dst = dot2(src0, src1) + src2 */
4714 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4716 struct glsl_src_param src0_param;
4717 struct glsl_src_param src1_param;
4718 struct glsl_src_param src2_param;
4719 DWORD write_mask;
4720 unsigned int mask_size;
4722 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4723 mask_size = shader_glsl_get_write_mask_size(write_mask);
4725 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4726 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4727 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4729 if (mask_size > 1) {
4730 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4731 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4732 } else {
4733 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4734 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4738 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4739 const struct wined3d_shader_signature *input_signature,
4740 const struct wined3d_shader_reg_maps *reg_maps,
4741 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
4743 unsigned int i;
4745 for (i = 0; i < input_signature->element_count; ++i)
4747 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4748 const char *semantic_name;
4749 UINT semantic_idx;
4750 char reg_mask[6];
4752 /* Unused */
4753 if (!(reg_maps->input_registers & (1u << input->register_idx)))
4754 continue;
4756 semantic_name = input->semantic_name;
4757 semantic_idx = input->semantic_idx;
4758 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4760 if (args->vp_mode == vertexshader)
4762 if (input->sysval_semantic == WINED3D_SV_POSITION)
4763 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4764 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4765 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4766 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
4767 else
4768 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4769 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4770 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4772 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4774 if (args->pointsprite)
4775 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
4776 shader->u.ps.input_reg_map[input->register_idx]);
4777 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
4778 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
4779 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4780 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
4781 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
4782 else
4783 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4784 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4786 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4788 if (!semantic_idx)
4789 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
4790 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4791 else if (semantic_idx == 1)
4792 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
4793 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4794 else
4795 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4796 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4798 else
4800 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4801 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4806 /*********************************************
4807 * Vertex Shader Specific Code begins here
4808 ********************************************/
4810 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4812 struct glsl_program_key key;
4814 key.vs_id = entry->vs.id;
4815 key.gs_id = entry->gs.id;
4816 key.ps_id = entry->ps.id;
4818 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4820 ERR("Failed to insert program entry.\n");
4824 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4825 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4827 struct wine_rb_entry *entry;
4828 struct glsl_program_key key;
4830 key.vs_id = vs_id;
4831 key.gs_id = gs_id;
4832 key.ps_id = ps_id;
4834 entry = wine_rb_get(&priv->program_lookup, &key);
4835 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4838 /* Context activation is done by the caller. */
4839 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4840 struct glsl_shader_prog_link *entry)
4842 struct glsl_program_key key;
4844 key.vs_id = entry->vs.id;
4845 key.gs_id = entry->gs.id;
4846 key.ps_id = entry->ps.id;
4847 wine_rb_remove(&priv->program_lookup, &key);
4849 GL_EXTCALL(glDeleteProgram(entry->id));
4850 if (entry->vs.id)
4851 list_remove(&entry->vs.shader_entry);
4852 if (entry->gs.id)
4853 list_remove(&entry->gs.shader_entry);
4854 if (entry->ps.id)
4855 list_remove(&entry->ps.shader_entry);
4856 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4857 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4858 HeapFree(GetProcessHeap(), 0, entry);
4861 static void handle_ps3_input(struct shader_glsl_priv *priv,
4862 const struct wined3d_gl_info *gl_info, const DWORD *map,
4863 const struct wined3d_shader_signature *input_signature,
4864 const struct wined3d_shader_reg_maps *reg_maps_in,
4865 const struct wined3d_shader_signature *output_signature,
4866 const struct wined3d_shader_reg_maps *reg_maps_out)
4868 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4869 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
4870 unsigned int i, j;
4871 DWORD *set;
4872 DWORD in_idx;
4873 unsigned int in_count = vec4_varyings(3, gl_info);
4874 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
4875 char reg_mask[6];
4876 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4878 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
4880 for (i = 0; i < input_signature->element_count; ++i)
4882 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4884 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
4885 continue;
4887 in_idx = map[input->register_idx];
4888 /* Declared, but not read register */
4889 if (in_idx == ~0u)
4890 continue;
4891 if (in_idx >= max_varyings)
4893 FIXME("More input varyings declared than supported, expect issues.\n");
4894 continue;
4897 if (in_idx == in_count)
4898 string_buffer_sprintf(destination, "gl_FrontColor");
4899 else if (in_idx == in_count + 1)
4900 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4901 else
4902 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
4904 if (!set[in_idx])
4905 set[in_idx] = ~0u;
4907 for (j = 0; j < output_signature->element_count; ++j)
4909 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4910 DWORD mask;
4912 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
4913 || input->semantic_idx != output->semantic_idx
4914 || strcmp(input->semantic_name, output->semantic_name)
4915 || !(mask = input->mask & output->mask))
4916 continue;
4918 if (set[in_idx] == ~0u)
4919 set[in_idx] = 0;
4920 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
4921 shader_glsl_write_mask_to_str(mask, reg_mask);
4923 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4924 destination->buffer, reg_mask, output->register_idx, reg_mask);
4928 for (i = 0; i < max_varyings; ++i)
4930 unsigned int size;
4932 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4933 continue;
4935 if (set[i] == ~0U) set[i] = 0;
4937 size = 0;
4938 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4939 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4940 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4941 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4942 reg_mask[size] = '\0';
4944 if (i == in_count)
4945 string_buffer_sprintf(destination, "gl_FrontColor");
4946 else if (i == in_count + 1)
4947 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4948 else
4949 string_buffer_sprintf(destination, "ps_link[%u]", i);
4951 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
4952 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
4955 HeapFree(GetProcessHeap(), 0, set);
4956 string_buffer_release(&priv->string_buffers, destination);
4959 /* Context activation is done by the caller. */
4960 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
4961 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4962 BOOL per_vertex_point_size, const struct wined3d_gl_info *gl_info)
4964 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4965 GLuint ret = 0;
4966 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4967 unsigned int i;
4968 const char *semantic_name;
4969 UINT semantic_idx;
4970 char reg_mask[6];
4971 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
4973 string_buffer_clear(buffer);
4975 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4977 if (per_vertex_point_size)
4979 shader_addline(buffer, "uniform struct\n{\n");
4980 shader_addline(buffer, " float size_min;\n");
4981 shader_addline(buffer, " float size_max;\n");
4982 shader_addline(buffer, "} ffp_point;\n");
4985 if (ps_major < 3)
4987 DWORD colors_written_mask[2] = {0};
4988 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
4990 if (!legacy_context)
4992 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
4993 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
4994 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
4995 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
4998 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5000 for (i = 0; i < vs->output_signature.element_count; ++i)
5002 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5003 DWORD write_mask;
5005 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5006 continue;
5008 semantic_name = output->semantic_name;
5009 semantic_idx = output->semantic_idx;
5010 write_mask = output->mask;
5011 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5013 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5015 if (legacy_context)
5016 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5017 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5018 else
5019 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5020 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5022 colors_written_mask[semantic_idx] = write_mask;
5024 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5026 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5027 reg_mask, output->register_idx, reg_mask);
5029 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5031 if (semantic_idx < MAX_TEXTURES)
5033 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5034 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5035 semantic_idx, reg_mask, output->register_idx, reg_mask);
5036 texcoords_written_mask[semantic_idx] = write_mask;
5039 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5041 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5042 output->register_idx, reg_mask[1]);
5044 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5046 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5047 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5048 output->register_idx, reg_mask[1]);
5052 for (i = 0; i < 2; ++i)
5054 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5056 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5057 if (!i)
5058 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5059 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5060 reg_mask, reg_mask);
5061 else
5062 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5063 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5064 reg_mask, reg_mask);
5067 for (i = 0; i < MAX_TEXTURES; ++i)
5069 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5070 continue;
5072 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5074 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5075 && !texcoords_written_mask[i])
5076 continue;
5078 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5079 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5080 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5084 else
5086 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5088 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5089 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5091 /* First, sort out position and point size. Those are not passed to the pixel shader */
5092 for (i = 0; i < vs->output_signature.element_count; ++i)
5094 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5096 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5097 continue;
5099 semantic_name = output->semantic_name;
5100 semantic_idx = output->semantic_idx;
5101 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5103 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5105 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5106 reg_mask, output->register_idx, reg_mask);
5108 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5110 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5111 output->register_idx, reg_mask[1]);
5115 /* Then, fix the pixel shader input */
5116 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5117 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5120 shader_addline(buffer, "}\n");
5122 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5123 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5124 shader_glsl_compile(gl_info, ret, buffer->buffer);
5126 return ret;
5129 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
5131 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5132 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5133 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5134 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5135 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5136 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5139 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5141 switch (mode)
5143 case WINED3D_FFP_PS_FOG_OFF:
5144 return;
5146 case WINED3D_FFP_PS_FOG_LINEAR:
5147 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5148 break;
5150 case WINED3D_FFP_PS_FOG_EXP:
5151 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5152 break;
5154 case WINED3D_FFP_PS_FOG_EXP2:
5155 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5156 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5157 break;
5159 default:
5160 ERR("Invalid fog mode %#x.\n", mode);
5161 return;
5164 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5165 " clamp(fog, 0.0, 1.0));\n");
5168 /* Context activation is done by the caller. */
5169 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5170 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5171 const struct wined3d_shader *shader,
5172 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5174 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5175 const struct wined3d_gl_info *gl_info = context->gl_info;
5176 const DWORD *function = shader->function;
5177 struct shader_glsl_ctx_priv priv_ctx;
5178 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5180 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5181 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5183 memset(&priv_ctx, 0, sizeof(priv_ctx));
5184 priv_ctx.cur_ps_args = args;
5185 priv_ctx.cur_np2fixup_info = np2fixup_info;
5186 priv_ctx.string_buffers = string_buffers;
5188 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5190 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5191 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5192 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5193 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5194 /* The spec says that it doesn't have to be explicitly enabled, but the
5195 * nvidia drivers write a warning if we don't do so. */
5196 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5197 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5198 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5199 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5200 if (gl_info->supported[EXT_GPU_SHADER4])
5201 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5203 /* Base Declarations */
5204 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5206 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5208 unsigned int i;
5209 WORD map = reg_maps->texcoord;
5211 if (legacy_context)
5213 if (glsl_is_color_reg_read(shader, 0))
5214 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5215 if (glsl_is_color_reg_read(shader, 1))
5216 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5219 for (i = 0; map; map >>= 1, ++i)
5221 if (map & 1)
5223 if (args->pointsprite)
5224 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5225 else if (args->texcoords_initialized & (1u << i))
5226 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5227 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5228 else
5229 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5230 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5234 if (legacy_context)
5235 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5238 /* Pack 3.0 inputs */
5239 if (reg_maps->shader_version.major >= 3)
5240 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5242 /* Base Shader Body */
5243 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5245 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5246 if (reg_maps->shader_version.major < 2)
5248 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5249 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5252 if (args->srgb_correction)
5253 shader_glsl_generate_srgb_write_correction(buffer);
5255 /* SM < 3 does not replace the fog stage. */
5256 if (reg_maps->shader_version.major < 3)
5257 shader_glsl_generate_fog_code(buffer, args->fog);
5259 shader_addline(buffer, "}\n");
5261 TRACE("Compiling shader object %u.\n", shader_id);
5262 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5264 return shader_id;
5267 /* Context activation is done by the caller. */
5268 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5269 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5270 const struct wined3d_shader *shader,
5271 const struct vs_compile_args *args)
5273 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5274 const struct wined3d_gl_info *gl_info = context->gl_info;
5275 const DWORD *function = shader->function;
5276 struct shader_glsl_ctx_priv priv_ctx;
5277 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5279 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5280 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5282 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5284 if (gl_info->supported[ARB_DRAW_INSTANCED])
5285 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5286 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5287 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5288 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5289 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5290 if (gl_info->supported[EXT_GPU_SHADER4])
5291 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5293 memset(&priv_ctx, 0, sizeof(priv_ctx));
5294 priv_ctx.cur_vs_args = args;
5295 priv_ctx.string_buffers = string_buffers;
5297 /* Base Declarations */
5298 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5300 /* Base Shader Body */
5301 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5303 /* Unpack outputs */
5304 shader_addline(buffer, "order_ps_input(vs_out);\n");
5306 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5307 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5308 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5309 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5311 if (reg_maps->shader_version.major < 3)
5313 if (args->fog_src == VS_FOG_Z)
5314 shader_addline(buffer, "%s = gl_Position.z;\n",
5315 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5316 else if (!reg_maps->fog)
5317 shader_addline(buffer, "%s = 0.0;\n",
5318 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5321 /* We always store the clipplanes without y inversion */
5322 if (args->clip_enabled)
5323 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5325 if (args->point_size && !args->per_vertex_point_size)
5326 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5328 /* Write the final position.
5330 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5331 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5332 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5333 * contains 1.0 to allow a mad.
5335 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5336 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5338 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5340 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5341 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5342 * which is the same as z = z * 2 - w.
5344 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5346 shader_addline(buffer, "}\n");
5348 TRACE("Compiling shader object %u.\n", shader_id);
5349 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5351 return shader_id;
5354 /* Context activation is done by the caller. */
5355 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5356 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5357 const struct wined3d_shader *shader)
5359 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5360 const struct wined3d_gl_info *gl_info = context->gl_info;
5361 const DWORD *function = shader->function;
5362 struct shader_glsl_ctx_priv priv_ctx;
5363 GLuint shader_id;
5365 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5367 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5369 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5370 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5371 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5372 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5373 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5374 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5375 if (gl_info->supported[EXT_GPU_SHADER4])
5376 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5378 memset(&priv_ctx, 0, sizeof(priv_ctx));
5379 priv_ctx.string_buffers = string_buffers;
5380 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5381 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5382 shader_addline(buffer, "}\n");
5384 TRACE("Compiling shader object %u.\n", shader_id);
5385 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5387 return shader_id;
5390 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5391 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5392 struct wined3d_shader *shader,
5393 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5395 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5396 struct glsl_shader_private *shader_data;
5397 struct ps_np2fixup_info *np2fixup;
5398 UINT i;
5399 DWORD new_size;
5400 GLuint ret;
5402 if (!shader->backend_data)
5404 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5405 if (!shader->backend_data)
5407 ERR("Failed to allocate backend data.\n");
5408 return 0;
5411 shader_data = shader->backend_data;
5412 gl_shaders = shader_data->gl_shaders.ps;
5414 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5415 * so a linear search is more performant than a hashmap or a binary search
5416 * (cache coherency etc)
5418 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5420 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5422 if (args->np2_fixup)
5423 *np2fixup_info = &gl_shaders[i].np2fixup;
5424 return gl_shaders[i].id;
5428 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5429 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5430 if (shader_data->num_gl_shaders)
5432 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5433 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5434 new_size * sizeof(*gl_shaders));
5436 else
5438 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5439 new_size = 1;
5442 if(!new_array) {
5443 ERR("Out of memory\n");
5444 return 0;
5446 shader_data->gl_shaders.ps = new_array;
5447 shader_data->shader_array_size = new_size;
5448 gl_shaders = new_array;
5451 gl_shaders[shader_data->num_gl_shaders].args = *args;
5453 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5454 memset(np2fixup, 0, sizeof(*np2fixup));
5455 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5457 pixelshader_update_resource_types(shader, args->tex_types);
5459 string_buffer_clear(buffer);
5460 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5461 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5463 return ret;
5466 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5467 const DWORD use_map)
5469 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5470 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5471 if (stored->point_size != new->point_size)
5472 return FALSE;
5473 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5474 return FALSE;
5475 return stored->fog_src == new->fog_src;
5478 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5479 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5480 struct wined3d_shader *shader,
5481 const struct vs_compile_args *args)
5483 UINT i;
5484 DWORD new_size;
5485 DWORD use_map = context->stream_info.use_map;
5486 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5487 struct glsl_shader_private *shader_data;
5488 GLuint ret;
5490 if (!shader->backend_data)
5492 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5493 if (!shader->backend_data)
5495 ERR("Failed to allocate backend data.\n");
5496 return 0;
5499 shader_data = shader->backend_data;
5500 gl_shaders = shader_data->gl_shaders.vs;
5502 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5503 * so a linear search is more performant than a hashmap or a binary search
5504 * (cache coherency etc)
5506 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5508 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5509 return gl_shaders[i].id;
5512 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5514 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5515 if (shader_data->num_gl_shaders)
5517 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5518 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5519 new_size * sizeof(*gl_shaders));
5521 else
5523 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5524 new_size = 1;
5527 if(!new_array) {
5528 ERR("Out of memory\n");
5529 return 0;
5531 shader_data->gl_shaders.vs = new_array;
5532 shader_data->shader_array_size = new_size;
5533 gl_shaders = new_array;
5536 gl_shaders[shader_data->num_gl_shaders].args = *args;
5538 string_buffer_clear(buffer);
5539 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5540 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5542 return ret;
5545 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5546 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5547 struct wined3d_shader *shader)
5549 struct glsl_gs_compiled_shader *gl_shaders;
5550 struct glsl_shader_private *shader_data;
5551 GLuint ret;
5553 if (!shader->backend_data)
5555 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5557 ERR("Failed to allocate backend data.\n");
5558 return 0;
5561 shader_data = shader->backend_data;
5562 gl_shaders = shader_data->gl_shaders.gs;
5564 if (shader_data->num_gl_shaders)
5565 return gl_shaders[0].id;
5567 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5569 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5571 ERR("Failed to allocate GL shader array.\n");
5572 return 0;
5574 shader_data->shader_array_size = 1;
5575 gl_shaders = shader_data->gl_shaders.gs;
5577 string_buffer_clear(buffer);
5578 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5579 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5581 return ret;
5584 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5586 switch (mcs)
5588 case WINED3D_MCS_MATERIAL:
5589 return material;
5590 case WINED3D_MCS_COLOR1:
5591 return "ffp_attrib_diffuse";
5592 case WINED3D_MCS_COLOR2:
5593 return "ffp_attrib_specular";
5594 default:
5595 ERR("Invalid material color source %#x.\n", mcs);
5596 return "<invalid>";
5600 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5601 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5603 const char *diffuse, *specular, *emissive, *ambient;
5604 enum wined3d_light_type light_type;
5605 unsigned int i;
5607 if (!settings->lighting)
5609 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5610 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5611 return;
5614 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5615 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5616 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5617 shader_addline(buffer, "vec3 dir, dst;\n");
5618 shader_addline(buffer, "float att, t;\n");
5620 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5621 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5622 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5623 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5625 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5627 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5628 switch (light_type)
5630 case WINED3D_LIGHT_POINT:
5631 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5632 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5633 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5634 shader_addline(buffer, "dst.x = 1.0;\n");
5635 if (legacy_lighting)
5637 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5638 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5640 else
5642 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5644 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5645 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5646 if (!legacy_lighting)
5647 shader_addline(buffer, "att = 1.0 / att;\n");
5648 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5649 if (!settings->normal)
5651 if (!legacy_lighting)
5652 shader_addline(buffer, "}\n");
5653 break;
5655 shader_addline(buffer, "dir = normalize(dir);\n");
5656 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5657 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5658 if (settings->localviewer)
5659 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5660 else
5661 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5662 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5663 " * ffp_light[%u].specular) * att;\n", i);
5664 if (!legacy_lighting)
5665 shader_addline(buffer, "}\n");
5666 break;
5668 case WINED3D_LIGHT_SPOT:
5669 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5670 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5671 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5672 shader_addline(buffer, "dst.x = 1.0;\n");
5673 if (legacy_lighting)
5675 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5676 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5678 else
5680 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5682 shader_addline(buffer, "dir = normalize(dir);\n");
5683 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5684 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5685 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5686 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5687 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5688 i, i, i, i);
5689 if (legacy_lighting)
5690 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5691 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5692 i, i, i);
5693 else
5694 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5695 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5696 i, i, i);
5697 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5698 if (!settings->normal)
5700 if (!legacy_lighting)
5701 shader_addline(buffer, "}\n");
5702 break;
5704 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5705 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5706 if (settings->localviewer)
5707 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5708 else
5709 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5710 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5711 " * ffp_light[%u].specular) * att;\n", i);
5712 if (!legacy_lighting)
5713 shader_addline(buffer, "}\n");
5714 break;
5716 case WINED3D_LIGHT_DIRECTIONAL:
5717 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5718 if (!settings->normal)
5719 break;
5720 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5721 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5722 " * ffp_light[%u].diffuse.xyz;\n", i);
5723 /* TODO: In the non-local viewer case the halfvector is constant
5724 * and could be precomputed and stored in a uniform. */
5725 if (settings->localviewer)
5726 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5727 else
5728 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5729 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5730 " * ffp_light[%u].specular;\n", i);
5731 break;
5733 case WINED3D_LIGHT_PARALLELPOINT:
5734 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5735 if (!settings->normal)
5736 break;
5737 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5738 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5739 " * ffp_light[%u].diffuse.xyz;\n", i);
5740 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5741 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5742 " * ffp_light[%u].specular;\n", i);
5743 break;
5745 default:
5746 if (light_type)
5747 FIXME("Unhandled light type %#x.\n", light_type);
5748 continue;
5752 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5753 ambient, diffuse, emissive);
5754 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
5755 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
5758 /* Context activation is done by the caller. */
5759 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
5760 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5762 static const struct attrib_info
5764 const char type[6];
5765 const char name[24];
5767 attrib_info[] =
5769 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5770 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5771 /* TODO: Indexed vertex blending */
5772 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5773 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5774 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5775 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5776 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5778 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5779 BOOL legacy_lighting = priv->legacy_lighting;
5780 GLuint shader_obj;
5781 unsigned int i;
5782 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5783 BOOL output_legacy_fogcoord = legacy_context;
5785 string_buffer_clear(buffer);
5787 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
5789 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
5791 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
5793 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
5795 shader_addline(buffer, "\n");
5797 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
5798 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5799 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5800 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5802 shader_addline(buffer, "uniform struct\n{\n");
5803 shader_addline(buffer, " vec4 emissive;\n");
5804 shader_addline(buffer, " vec4 ambient;\n");
5805 shader_addline(buffer, " vec4 diffuse;\n");
5806 shader_addline(buffer, " vec4 specular;\n");
5807 shader_addline(buffer, " float shininess;\n");
5808 shader_addline(buffer, "} ffp_material;\n");
5810 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5811 shader_addline(buffer, "uniform struct\n{\n");
5812 shader_addline(buffer, " vec4 diffuse;\n");
5813 shader_addline(buffer, " vec4 specular;\n");
5814 shader_addline(buffer, " vec4 ambient;\n");
5815 shader_addline(buffer, " vec4 position;\n");
5816 shader_addline(buffer, " vec3 direction;\n");
5817 shader_addline(buffer, " float range;\n");
5818 shader_addline(buffer, " float falloff;\n");
5819 shader_addline(buffer, " float c_att;\n");
5820 shader_addline(buffer, " float l_att;\n");
5821 shader_addline(buffer, " float q_att;\n");
5822 shader_addline(buffer, " float cos_htheta;\n");
5823 shader_addline(buffer, " float cos_hphi;\n");
5824 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5826 if (settings->point_size)
5828 shader_addline(buffer, "uniform struct\n{\n");
5829 shader_addline(buffer, " float size;\n");
5830 shader_addline(buffer, " float size_min;\n");
5831 shader_addline(buffer, " float size_max;\n");
5832 shader_addline(buffer, " float c_att;\n");
5833 shader_addline(buffer, " float l_att;\n");
5834 shader_addline(buffer, " float q_att;\n");
5835 shader_addline(buffer, "} ffp_point;\n");
5838 if (legacy_context)
5840 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
5841 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
5842 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5843 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
5845 else
5847 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
5848 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
5849 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5850 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5853 shader_addline(buffer, "\nvoid main()\n{\n");
5854 shader_addline(buffer, "float m;\n");
5855 shader_addline(buffer, "vec3 r;\n");
5857 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
5859 if (attrib_info[i].name[0])
5860 shader_addline(buffer, "%s %s = vs_in%u;\n",
5861 attrib_info[i].type, attrib_info[i].name, i);
5863 for (i = 0; i < MAX_TEXTURES; ++i)
5865 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
5866 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
5868 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
5872 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
5874 if (settings->transformed)
5876 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5877 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5878 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
5880 else
5882 for (i = 0; i < settings->vertexblends; ++i)
5883 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
5885 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
5886 for (i = 0; i < settings->vertexblends + 1; ++i)
5887 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
5889 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5890 if (settings->clipping)
5891 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5892 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5895 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5896 if (settings->normal)
5898 if (!settings->vertexblends)
5900 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
5902 else
5904 for (i = 0; i < settings->vertexblends + 1; ++i)
5905 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
5908 if (settings->normalize)
5909 shader_addline(buffer, "normal = normalize(normal);\n");
5912 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
5913 if (legacy_context)
5915 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
5916 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
5919 for (i = 0; i < MAX_TEXTURES; ++i)
5921 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5923 switch (settings->texgen[i] & 0xffff0000)
5925 case WINED3DTSS_TCI_PASSTHRU:
5926 if (settings->texcoords & (1u << i))
5927 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
5928 i, i, i);
5929 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
5930 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
5931 else
5932 output_legacy_texcoord = FALSE;
5933 break;
5935 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5936 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5937 break;
5939 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5940 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5941 break;
5943 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5944 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5945 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5946 break;
5948 case WINED3DTSS_TCI_SPHEREMAP:
5949 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5950 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5951 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
5952 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5953 break;
5955 default:
5956 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5957 break;
5959 if (output_legacy_texcoord)
5960 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
5963 switch (settings->fog_mode)
5965 case WINED3D_FFP_VS_FOG_OFF:
5966 output_legacy_fogcoord = FALSE;
5967 break;
5969 case WINED3D_FFP_VS_FOG_FOGCOORD:
5970 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
5971 break;
5973 case WINED3D_FFP_VS_FOG_RANGE:
5974 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
5975 break;
5977 case WINED3D_FFP_VS_FOG_DEPTH:
5978 if (settings->ortho_fog)
5979 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5980 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
5981 else if (settings->transformed)
5982 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
5983 else
5984 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
5985 break;
5987 default:
5988 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5989 break;
5991 if (output_legacy_fogcoord)
5992 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
5994 if (settings->point_size)
5996 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
5997 " + ffp_point.l_att * length(ec_pos.xyz)"
5998 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
5999 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6000 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6003 shader_addline(buffer, "}\n");
6005 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6006 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6008 return shader_obj;
6011 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6012 DWORD argnum, unsigned int stage, DWORD arg)
6014 const char *ret;
6016 if (arg == ARG_UNUSED)
6017 return "<unused arg>";
6019 switch (arg & WINED3DTA_SELECTMASK)
6021 case WINED3DTA_DIFFUSE:
6022 ret = "ffp_varying_diffuse";
6023 break;
6025 case WINED3DTA_CURRENT:
6026 if (!stage)
6027 ret = "ffp_varying_diffuse";
6028 else
6029 ret = "ret";
6030 break;
6032 case WINED3DTA_TEXTURE:
6033 switch (stage)
6035 case 0: ret = "tex0"; break;
6036 case 1: ret = "tex1"; break;
6037 case 2: ret = "tex2"; break;
6038 case 3: ret = "tex3"; break;
6039 case 4: ret = "tex4"; break;
6040 case 5: ret = "tex5"; break;
6041 case 6: ret = "tex6"; break;
6042 case 7: ret = "tex7"; break;
6043 default:
6044 ret = "<invalid texture>";
6045 break;
6047 break;
6049 case WINED3DTA_TFACTOR:
6050 ret = "tex_factor";
6051 break;
6053 case WINED3DTA_SPECULAR:
6054 ret = "ffp_varying_specular";
6055 break;
6057 case WINED3DTA_TEMP:
6058 ret = "temp_reg";
6059 break;
6061 case WINED3DTA_CONSTANT:
6062 switch (stage)
6064 case 0: ret = "tss_const0"; break;
6065 case 1: ret = "tss_const1"; break;
6066 case 2: ret = "tss_const2"; break;
6067 case 3: ret = "tss_const3"; break;
6068 case 4: ret = "tss_const4"; break;
6069 case 5: ret = "tss_const5"; break;
6070 case 6: ret = "tss_const6"; break;
6071 case 7: ret = "tss_const7"; break;
6072 default:
6073 ret = "<invalid constant>";
6074 break;
6076 break;
6078 default:
6079 return "<unhandled arg>";
6082 if (arg & WINED3DTA_COMPLEMENT)
6084 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6085 if (argnum == 0)
6086 ret = "arg0";
6087 else if (argnum == 1)
6088 ret = "arg1";
6089 else if (argnum == 2)
6090 ret = "arg2";
6093 if (arg & WINED3DTA_ALPHAREPLICATE)
6095 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6096 if (argnum == 0)
6097 ret = "arg0";
6098 else if (argnum == 1)
6099 ret = "arg1";
6100 else if (argnum == 2)
6101 ret = "arg2";
6104 return ret;
6107 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6108 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6110 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6112 if (color && alpha)
6113 dstmask = "";
6114 else if (color)
6115 dstmask = ".xyz";
6116 else
6117 dstmask = ".w";
6119 if (dst == tempreg)
6120 dstreg = "temp_reg";
6121 else
6122 dstreg = "ret";
6124 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6125 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6126 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6128 switch (op)
6130 case WINED3D_TOP_DISABLE:
6131 if (!stage)
6132 shader_addline(buffer, "%s%s = ffp_varying_diffuse%s;\n", dstreg, dstmask, dstmask);
6133 break;
6135 case WINED3D_TOP_SELECT_ARG1:
6136 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6137 break;
6139 case WINED3D_TOP_SELECT_ARG2:
6140 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6141 break;
6143 case WINED3D_TOP_MODULATE:
6144 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6145 break;
6147 case WINED3D_TOP_MODULATE_4X:
6148 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6149 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6150 break;
6152 case WINED3D_TOP_MODULATE_2X:
6153 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6154 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6155 break;
6157 case WINED3D_TOP_ADD:
6158 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6159 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6160 break;
6162 case WINED3D_TOP_ADD_SIGNED:
6163 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6164 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6165 break;
6167 case WINED3D_TOP_ADD_SIGNED_2X:
6168 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6169 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6170 break;
6172 case WINED3D_TOP_SUBTRACT:
6173 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6174 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6175 break;
6177 case WINED3D_TOP_ADD_SMOOTH:
6178 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6179 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6180 break;
6182 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6183 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6184 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6185 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6186 break;
6188 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6189 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6190 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6191 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6192 break;
6194 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6195 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6196 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6197 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6198 break;
6200 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6201 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6202 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6203 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6204 break;
6206 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6207 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6208 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6209 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6210 break;
6212 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6213 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6214 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6215 break;
6217 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6218 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6219 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6220 break;
6222 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6223 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6224 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6225 break;
6226 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6227 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6228 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6229 break;
6231 case WINED3D_TOP_BUMPENVMAP:
6232 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6233 /* These are handled in the first pass, nothing to do. */
6234 break;
6236 case WINED3D_TOP_DOTPRODUCT3:
6237 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6238 dstreg, dstmask, arg1, arg2, dstmask);
6239 break;
6241 case WINED3D_TOP_MULTIPLY_ADD:
6242 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6243 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6244 break;
6246 case WINED3D_TOP_LERP:
6247 /* MSDN isn't quite right here. */
6248 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6249 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6250 break;
6252 default:
6253 FIXME("Unhandled operation %#x.\n", op);
6254 break;
6258 /* Context activation is done by the caller. */
6259 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6260 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6262 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6263 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6264 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6265 const char *final_combiner_src = "ret";
6266 UINT lowest_disabled_stage;
6267 GLuint shader_id;
6268 DWORD arg0, arg1, arg2;
6269 unsigned int stage;
6270 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6271 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6273 string_buffer_clear(buffer);
6275 /* Find out which textures are read */
6276 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6278 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6279 break;
6281 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6282 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6283 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6285 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6286 || (stage == 0 && settings->color_key_enabled))
6287 tex_map |= 1u << stage;
6288 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6289 tfactor_used = TRUE;
6290 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6291 tempreg_used = TRUE;
6292 if (settings->op[stage].dst == tempreg)
6293 tempreg_used = TRUE;
6294 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6295 tss_const_map |= 1u << stage;
6297 switch (settings->op[stage].cop)
6299 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6300 lum_map |= 1u << stage;
6301 /* fall through */
6302 case WINED3D_TOP_BUMPENVMAP:
6303 bump_map |= 1u << stage;
6304 /* fall through */
6305 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6306 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6307 tex_map |= 1u << stage;
6308 break;
6310 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6311 tfactor_used = TRUE;
6312 break;
6314 default:
6315 break;
6318 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6319 continue;
6321 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6322 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6323 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6325 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6326 tex_map |= 1u << stage;
6327 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6328 tfactor_used = TRUE;
6329 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6330 tempreg_used = TRUE;
6331 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6332 tss_const_map |= 1u << stage;
6334 lowest_disabled_stage = stage;
6336 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6338 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6339 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6341 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6342 shader_addline(buffer, "vec4 ret;\n");
6343 if (tempreg_used || settings->sRGB_write)
6344 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6345 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6347 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6349 if (tss_const_map & (1u << stage))
6350 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6352 if (!(tex_map & (1u << stage)))
6353 continue;
6355 switch (settings->op[stage].tex_type)
6357 case WINED3D_GL_RES_TYPE_TEX_1D:
6358 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6359 break;
6360 case WINED3D_GL_RES_TYPE_TEX_2D:
6361 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6362 break;
6363 case WINED3D_GL_RES_TYPE_TEX_3D:
6364 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6365 break;
6366 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6367 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6368 break;
6369 case WINED3D_GL_RES_TYPE_TEX_RECT:
6370 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6371 break;
6372 default:
6373 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6374 break;
6377 shader_addline(buffer, "vec4 tex%u;\n", stage);
6379 if (!(bump_map & (1u << stage)))
6380 continue;
6381 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6383 if (!(lum_map & (1u << stage)))
6384 continue;
6385 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6386 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6388 if (tfactor_used)
6389 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6390 if (settings->color_key_enabled)
6391 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6392 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6394 if (settings->sRGB_write)
6396 shader_addline(buffer, "const vec4 srgb_const0 = ");
6397 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6398 shader_addline(buffer, ";\n");
6399 shader_addline(buffer, "const vec4 srgb_const1 = ");
6400 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6401 shader_addline(buffer, ";\n");
6404 shader_addline(buffer, "uniform struct\n{\n");
6405 shader_addline(buffer, " vec4 color;\n");
6406 shader_addline(buffer, " float density;\n");
6407 shader_addline(buffer, " float end;\n");
6408 shader_addline(buffer, " float scale;\n");
6409 shader_addline(buffer, "} ffp_fog;\n");
6411 if (legacy_context)
6413 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6414 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6415 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6416 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6417 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6419 else
6421 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_diffuse;\n");
6422 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_specular;\n");
6423 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6424 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6425 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6428 shader_addline(buffer, "void main()\n{\n");
6430 if (legacy_context)
6432 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6433 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6436 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6438 if (tex_map & (1u << stage))
6440 if (settings->pointsprite)
6441 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6442 else if (settings->texcoords_initialized & (1u << stage))
6443 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6444 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6445 else
6446 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6450 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6451 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6453 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6454 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6456 /* Generate texture sampling instructions) */
6457 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6459 const char *texture_function, *coord_mask;
6460 BOOL proj;
6462 if (!(tex_map & (1u << stage)))
6463 continue;
6465 if (settings->op[stage].projected == proj_none)
6467 proj = FALSE;
6469 else if (settings->op[stage].projected == proj_count4
6470 || settings->op[stage].projected == proj_count3)
6472 proj = TRUE;
6474 else
6476 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6477 proj = TRUE;
6480 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6481 proj = FALSE;
6483 switch (settings->op[stage].tex_type)
6485 case WINED3D_GL_RES_TYPE_TEX_1D:
6486 if (proj)
6488 texture_function = "texture1DProj";
6489 coord_mask = "xw";
6491 else
6493 texture_function = "texture1D";
6494 coord_mask = "x";
6496 break;
6497 case WINED3D_GL_RES_TYPE_TEX_2D:
6498 if (proj)
6500 texture_function = "texture2DProj";
6501 coord_mask = "xyw";
6503 else
6505 texture_function = "texture2D";
6506 coord_mask = "xy";
6508 break;
6509 case WINED3D_GL_RES_TYPE_TEX_3D:
6510 if (proj)
6512 texture_function = "texture3DProj";
6513 coord_mask = "xyzw";
6515 else
6517 texture_function = "texture3D";
6518 coord_mask = "xyz";
6520 break;
6521 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6522 texture_function = "textureCube";
6523 coord_mask = "xyz";
6524 break;
6525 case WINED3D_GL_RES_TYPE_TEX_RECT:
6526 if (proj)
6528 texture_function = "texture2DRectProj";
6529 coord_mask = "xyw";
6531 else
6533 texture_function = "texture2DRect";
6534 coord_mask = "xy";
6536 break;
6537 default:
6538 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6539 texture_function = "";
6540 coord_mask = "xyzw";
6541 break;
6543 if (!needs_legacy_glsl_syntax(gl_info))
6544 texture_function = proj ? "textureProj" : "texture";
6546 if (stage > 0
6547 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6548 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6550 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6552 /* With projective textures, texbem only divides the static
6553 * texture coord, not the displacement, so multiply the
6554 * displacement with the dividing parameter before passing it to
6555 * TXP. */
6556 if (settings->op[stage].projected != proj_none)
6558 if (settings->op[stage].projected == proj_count4)
6560 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6561 stage, stage);
6562 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6564 else
6566 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6567 stage, stage);
6568 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6571 else
6573 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6576 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6577 stage, texture_function, stage, coord_mask);
6579 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6580 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6581 stage, stage - 1, stage - 1, stage - 1);
6583 else if (settings->op[stage].projected == proj_count3)
6585 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6586 stage, texture_function, stage, stage);
6588 else
6590 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6591 stage, texture_function, stage, stage, coord_mask);
6594 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6595 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6596 settings->op[stage].color_fixup);
6599 if (settings->color_key_enabled)
6601 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6602 shader_addline(buffer, " discard;\n");
6605 /* Generate the main shader */
6606 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6608 BOOL op_equal;
6610 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6612 if (!stage)
6613 final_combiner_src = "ffp_varying_diffuse";
6614 break;
6617 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6618 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6619 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6620 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6621 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6622 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6623 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6624 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6625 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6626 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6627 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6628 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6629 else
6630 op_equal = settings->op[stage].aop == settings->op[stage].cop
6631 && settings->op[stage].carg0 == settings->op[stage].aarg0
6632 && settings->op[stage].carg1 == settings->op[stage].aarg1
6633 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6635 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6637 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6638 settings->op[stage].cop, settings->op[stage].carg0,
6639 settings->op[stage].carg1, settings->op[stage].carg2);
6640 if (!stage)
6641 shader_addline(buffer, "ret.w = ffp_varying_diffuse.w;\n");
6643 else if (op_equal)
6645 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6646 settings->op[stage].cop, settings->op[stage].carg0,
6647 settings->op[stage].carg1, settings->op[stage].carg2);
6649 else
6651 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6652 settings->op[stage].cop, settings->op[stage].carg0,
6653 settings->op[stage].carg1, settings->op[stage].carg2);
6654 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6655 settings->op[stage].aop, settings->op[stage].aarg0,
6656 settings->op[stage].aarg1, settings->op[stage].aarg2);
6660 shader_addline(buffer, "gl_FragData[0] = ffp_varying_specular * specular_enable + %s;\n", final_combiner_src);
6662 if (settings->sRGB_write)
6663 shader_glsl_generate_srgb_write_correction(buffer);
6665 shader_glsl_generate_fog_code(buffer, settings->fog);
6667 shader_addline(buffer, "}\n");
6669 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6670 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6672 string_buffer_release(&priv->string_buffers, tex_reg_name);
6673 return shader_id;
6676 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6677 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6679 struct glsl_ffp_vertex_shader *shader;
6680 const struct wine_rb_entry *entry;
6682 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6683 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6685 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6686 return NULL;
6688 shader->desc.settings = *settings;
6689 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6690 list_init(&shader->linked_programs);
6691 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6692 ERR("Failed to insert ffp vertex shader.\n");
6694 return shader;
6697 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6698 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6700 struct glsl_ffp_fragment_shader *glsl_desc;
6701 const struct ffp_frag_desc *desc;
6703 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6704 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6706 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6707 return NULL;
6709 glsl_desc->entry.settings = *args;
6710 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6711 list_init(&glsl_desc->linked_programs);
6712 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6714 return glsl_desc;
6718 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6719 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6721 unsigned int i;
6722 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6724 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6725 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6726 for (i = 0; i < vs_c_count; ++i)
6728 string_buffer_sprintf(name, "vs_c[%u]", i);
6729 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6731 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6732 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6734 for (i = 0; i < MAX_CONST_I; ++i)
6736 string_buffer_sprintf(name, "vs_i[%u]", i);
6737 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6740 for (i = 0; i < MAX_CONST_B; ++i)
6742 string_buffer_sprintf(name, "vs_b[%u]", i);
6743 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6746 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6748 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
6750 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
6751 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6753 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6754 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6755 for (i = 0; i < MAX_TEXTURES; ++i)
6757 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6758 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6760 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6761 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6762 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6763 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
6764 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6765 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6766 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6768 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6769 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6770 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6771 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6772 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6773 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6774 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6775 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6776 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6777 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6778 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6779 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6780 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6781 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6782 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6783 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6784 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6785 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6786 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6787 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6788 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6789 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6790 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6791 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6793 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
6794 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
6795 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
6796 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
6797 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
6798 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
6800 string_buffer_release(&priv->string_buffers, name);
6803 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6804 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6806 unsigned int i;
6807 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6809 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6810 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6811 for (i = 0; i < ps_c_count; ++i)
6813 string_buffer_sprintf(name, "ps_c[%u]", i);
6814 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6816 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6817 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6819 for (i = 0; i < MAX_CONST_I; ++i)
6821 string_buffer_sprintf(name, "ps_i[%u]", i);
6822 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6825 for (i = 0; i < MAX_CONST_B; ++i)
6827 string_buffer_sprintf(name, "ps_b[%u]", i);
6828 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6831 for (i = 0; i < MAX_TEXTURES; ++i)
6833 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6834 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6835 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6836 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6837 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6838 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6839 string_buffer_sprintf(name, "tss_const%u", i);
6840 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6843 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6844 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6846 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
6847 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
6848 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
6849 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
6851 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6852 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6853 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6855 string_buffer_release(&priv->string_buffers, name);
6858 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6859 struct shader_glsl_priv *priv, GLuint program_id,
6860 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6862 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6863 GLuint block_idx;
6864 unsigned int i;
6865 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6867 for (i = 0; i < count; ++i)
6869 if (!reg_maps->cb_sizes[i])
6870 continue;
6872 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6873 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6874 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6876 checkGLcall("glUniformBlockBinding");
6877 string_buffer_release(&priv->string_buffers, name);
6880 /* Context activation is done by the caller. */
6881 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6882 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6884 const struct wined3d_gl_info *gl_info = context->gl_info;
6885 const struct ps_np2fixup_info *np2fixup_info = NULL;
6886 struct glsl_shader_prog_link *entry = NULL;
6887 struct wined3d_shader *vshader = NULL;
6888 struct wined3d_shader *gshader = NULL;
6889 struct wined3d_shader *pshader = NULL;
6890 GLuint program_id = 0;
6891 GLuint reorder_shader_id = 0;
6892 unsigned int i;
6893 GLuint vs_id = 0;
6894 GLuint gs_id = 0;
6895 GLuint ps_id = 0;
6896 struct list *ps_list, *vs_list;
6897 WORD attribs_map;
6898 struct wined3d_string_buffer *tmp_name;
6900 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
6902 vs_id = ctx_data->glsl_program->vs.id;
6903 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6905 if (use_vs(state))
6907 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6908 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6910 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
6911 && ctx_data->glsl_program->gs.id)
6912 gs_id = ctx_data->glsl_program->gs.id;
6913 else if (gshader)
6914 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6917 else if (use_vs(state))
6919 struct vs_compile_args vs_compile_args;
6920 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6922 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6923 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
6924 vs_list = &vshader->linked_programs;
6926 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6927 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
6929 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6931 struct glsl_ffp_vertex_shader *ffp_shader;
6932 struct wined3d_ffp_vs_settings settings;
6934 wined3d_ffp_get_vs_settings(context, state, &settings);
6935 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6936 vs_id = ffp_shader->id;
6937 vs_list = &ffp_shader->linked_programs;
6940 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
6942 ps_id = ctx_data->glsl_program->ps.id;
6943 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6945 if (use_ps(state))
6946 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6948 else if (use_ps(state))
6950 struct ps_compile_args ps_compile_args;
6951 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6952 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
6953 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
6954 pshader, &ps_compile_args, &np2fixup_info);
6955 ps_list = &pshader->linked_programs;
6957 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6959 struct glsl_ffp_fragment_shader *ffp_shader;
6960 struct ffp_frag_settings settings;
6962 gen_ffp_frag_op(context, state, &settings, FALSE);
6963 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6964 ps_id = ffp_shader->id;
6965 ps_list = &ffp_shader->linked_programs;
6968 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6970 ctx_data->glsl_program = entry;
6971 return;
6974 /* If we get to this point, then no matching program exists, so we create one */
6975 program_id = GL_EXTCALL(glCreateProgram());
6976 TRACE("Created new GLSL shader program %u.\n", program_id);
6978 /* Create the entry */
6979 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6980 entry->id = program_id;
6981 entry->vs.id = vs_id;
6982 entry->gs.id = gs_id;
6983 entry->ps.id = ps_id;
6984 entry->constant_version = 0;
6985 entry->ps.np2_fixup_info = np2fixup_info;
6986 /* Add the hash table entry */
6987 add_glsl_program_entry(priv, entry);
6989 /* Set the current program */
6990 ctx_data->glsl_program = entry;
6992 /* Attach GLSL vshader */
6993 if (vs_id)
6995 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6996 GL_EXTCALL(glAttachShader(program_id, vs_id));
6997 checkGLcall("glAttachShader");
6999 list_add_head(vs_list, &entry->vs.shader_entry);
7002 if (vshader)
7004 attribs_map = vshader->reg_maps.input_registers;
7005 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7006 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size, gl_info);
7007 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7008 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7009 checkGLcall("glAttachShader");
7010 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7011 * is destroyed
7013 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7015 else
7017 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7020 /* Bind vertex attributes to a corresponding index number to match
7021 * the same index numbers as ARB_vertex_programs (makes loading
7022 * vertex attributes simpler). With this method, we can use the
7023 * exact same code to load the attributes later for both ARB and
7024 * GLSL shaders.
7026 * We have to do this here because we need to know the Program ID
7027 * in order to make the bindings work, and it has to be done prior
7028 * to linking the GLSL program. */
7029 tmp_name = string_buffer_get(&priv->string_buffers);
7030 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7032 if (!(attribs_map & 1))
7033 continue;
7035 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7036 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7038 checkGLcall("glBindAttribLocation");
7039 string_buffer_release(&priv->string_buffers, tmp_name);
7041 if (gshader)
7043 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7044 GL_EXTCALL(glAttachShader(program_id, gs_id));
7045 checkGLcall("glAttachShader");
7047 TRACE("input type %s, output type %s, vertices out %u.\n",
7048 debug_d3dprimitivetype(gshader->u.gs.input_type),
7049 debug_d3dprimitivetype(gshader->u.gs.output_type),
7050 gshader->u.gs.vertices_out);
7051 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7052 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7053 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7054 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7055 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7056 gshader->u.gs.vertices_out));
7057 checkGLcall("glProgramParameteriARB");
7059 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7062 /* Attach GLSL pshader */
7063 if (ps_id)
7065 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7066 GL_EXTCALL(glAttachShader(program_id, ps_id));
7067 checkGLcall("glAttachShader");
7069 list_add_head(ps_list, &entry->ps.shader_entry);
7072 /* Link the program */
7073 TRACE("Linking GLSL shader program %u.\n", program_id);
7074 GL_EXTCALL(glLinkProgram(program_id));
7075 shader_glsl_validate_link(gl_info, program_id);
7077 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7078 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7079 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7080 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7081 checkGLcall("Find glsl program uniform locations");
7083 if (pshader && pshader->reg_maps.shader_version.major >= 3
7084 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7086 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7087 entry->vs.vertex_color_clamp = GL_FALSE;
7089 else
7091 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7094 /* Set the shader to allow uniform loading on it */
7095 GL_EXTCALL(glUseProgram(program_id));
7096 checkGLcall("glUseProgram");
7098 /* Texture unit mapping is set up to be the same each time the shader
7099 * program is used so we can hardcode the sampler uniform values. */
7100 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7102 entry->constant_update_mask = 0;
7103 if (vshader)
7105 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7106 if (vshader->reg_maps.integer_constants)
7107 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7108 if (vshader->reg_maps.boolean_constants)
7109 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7110 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7112 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7113 0, gl_info->limits.vertex_uniform_blocks);
7115 else
7117 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7118 | WINED3D_SHADER_CONST_FFP_PROJ;
7120 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7122 if (entry->vs.modelview_matrix_location[i] != -1)
7124 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7125 break;
7129 for (i = 0; i < MAX_TEXTURES; ++i)
7131 if (entry->vs.texture_matrix_location[i] != -1)
7133 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7134 break;
7137 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7138 || entry->vs.material_specular_location != -1
7139 || entry->vs.material_emissive_location != -1
7140 || entry->vs.material_shininess_location != -1)
7141 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7142 if (entry->vs.light_ambient_location != -1)
7143 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7145 if (entry->vs.pointsize_min_location != -1)
7146 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7148 if (gshader)
7149 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7150 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7152 if (ps_id)
7154 if (pshader)
7156 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7157 if (pshader->reg_maps.integer_constants)
7158 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7159 if (pshader->reg_maps.boolean_constants)
7160 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7161 if (entry->ps.ycorrection_location != -1)
7162 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7164 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7165 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7166 gl_info->limits.fragment_uniform_blocks);
7168 else
7170 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7173 for (i = 0; i < MAX_TEXTURES; ++i)
7175 if (entry->ps.bumpenv_mat_location[i] != -1)
7177 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7178 break;
7182 if (entry->ps.fog_color_location != -1)
7183 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7184 if (entry->ps.np2_fixup_location != -1)
7185 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7186 if (entry->ps.color_key_location != -1)
7187 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7191 /* Context activation is done by the caller. */
7192 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7193 BOOL masked)
7195 GLuint program_id;
7196 GLuint vshader_id, pshader_id;
7197 const char *blt_pshader;
7199 static const char blt_vshader[] =
7200 "#version 120\n"
7201 "void main(void)\n"
7202 "{\n"
7203 " gl_Position = gl_Vertex;\n"
7204 " gl_FrontColor = vec4(1.0);\n"
7205 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7206 "}\n";
7208 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7210 /* WINED3D_GL_RES_TYPE_TEX_1D */
7211 NULL,
7212 /* WINED3D_GL_RES_TYPE_TEX_2D */
7213 "#version 120\n"
7214 "uniform sampler2D sampler;\n"
7215 "void main(void)\n"
7216 "{\n"
7217 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7218 "}\n",
7219 /* WINED3D_GL_RES_TYPE_TEX_3D */
7220 NULL,
7221 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7222 "#version 120\n"
7223 "uniform samplerCube sampler;\n"
7224 "void main(void)\n"
7225 "{\n"
7226 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7227 "}\n",
7228 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7229 "#version 120\n"
7230 "#extension GL_ARB_texture_rectangle : enable\n"
7231 "uniform sampler2DRect sampler;\n"
7232 "void main(void)\n"
7233 "{\n"
7234 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7235 "}\n",
7236 /* WINED3D_GL_RES_TYPE_BUFFER */
7237 NULL,
7238 /* WINED3D_GL_RES_TYPE_RB */
7239 NULL,
7242 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7244 /* WINED3D_GL_RES_TYPE_TEX_1D */
7245 NULL,
7246 /* WINED3D_GL_RES_TYPE_TEX_2D */
7247 "#version 120\n"
7248 "uniform sampler2D sampler;\n"
7249 "uniform vec4 mask;\n"
7250 "void main(void)\n"
7251 "{\n"
7252 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7253 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7254 "}\n",
7255 /* WINED3D_GL_RES_TYPE_TEX_3D */
7256 NULL,
7257 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7258 "#version 120\n"
7259 "uniform samplerCube sampler;\n"
7260 "uniform vec4 mask;\n"
7261 "void main(void)\n"
7262 "{\n"
7263 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7264 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7265 "}\n",
7266 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7267 "#version 120\n"
7268 "#extension GL_ARB_texture_rectangle : enable\n"
7269 "uniform sampler2DRect sampler;\n"
7270 "uniform vec4 mask;\n"
7271 "void main(void)\n"
7272 "{\n"
7273 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7274 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7275 "}\n",
7276 /* WINED3D_GL_RES_TYPE_BUFFER */
7277 NULL,
7278 /* WINED3D_GL_RES_TYPE_RB */
7279 NULL,
7282 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7283 if (!blt_pshader)
7285 FIXME("tex_type %#x not supported\n", tex_type);
7286 return 0;
7289 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7290 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7292 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7293 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7295 program_id = GL_EXTCALL(glCreateProgram());
7296 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7297 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7298 GL_EXTCALL(glLinkProgram(program_id));
7300 shader_glsl_validate_link(gl_info, program_id);
7302 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7303 * is destroyed
7305 GL_EXTCALL(glDeleteShader(vshader_id));
7306 GL_EXTCALL(glDeleteShader(pshader_id));
7307 return program_id;
7310 /* Context activation is done by the caller. */
7311 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7312 const struct wined3d_state *state)
7314 struct glsl_context_data *ctx_data = context->shader_backend_data;
7315 const struct wined3d_gl_info *gl_info = context->gl_info;
7316 struct shader_glsl_priv *priv = shader_priv;
7317 GLuint program_id = 0, prev_id = 0;
7318 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7320 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7321 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7323 if (ctx_data->glsl_program)
7325 prev_id = ctx_data->glsl_program->id;
7326 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7328 else
7330 prev_id = 0;
7331 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7334 set_glsl_shader_program(context, state, priv, ctx_data);
7336 if (ctx_data->glsl_program)
7338 program_id = ctx_data->glsl_program->id;
7339 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7341 else
7343 program_id = 0;
7344 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7347 if (old_vertex_color_clamp != current_vertex_color_clamp)
7349 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7351 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7352 checkGLcall("glClampColorARB");
7354 else
7356 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7360 TRACE("Using GLSL program %u.\n", program_id);
7362 if (prev_id != program_id)
7364 GL_EXTCALL(glUseProgram(program_id));
7365 checkGLcall("glUseProgram");
7367 if (program_id)
7368 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7372 /* "context" is not necessarily the currently active context. */
7373 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7375 struct glsl_context_data *ctx_data = context->shader_backend_data;
7377 ctx_data->glsl_program = NULL;
7378 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7379 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7380 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7383 /* Context activation is done by the caller. */
7384 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7386 const struct wined3d_gl_info *gl_info = context->gl_info;
7387 struct shader_glsl_priv *priv = shader_priv;
7389 shader_glsl_invalidate_current_program(context);
7390 GL_EXTCALL(glUseProgram(0));
7391 checkGLcall("glUseProgram");
7393 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7394 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7396 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7398 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7399 checkGLcall("glClampColorARB");
7403 /* Context activation is done by the caller. */
7404 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7405 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7407 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7408 struct shader_glsl_priv *priv = shader_priv;
7409 GLuint *blt_program;
7410 GLint loc;
7412 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7413 if (!*blt_program)
7415 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7416 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7417 GL_EXTCALL(glUseProgram(*blt_program));
7418 GL_EXTCALL(glUniform1i(loc, 0));
7420 else
7422 GL_EXTCALL(glUseProgram(*blt_program));
7425 if (masked)
7427 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7428 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7432 /* Context activation is done by the caller. */
7433 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7435 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7436 GLuint program_id;
7438 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7439 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7441 GL_EXTCALL(glUseProgram(program_id));
7442 checkGLcall("glUseProgram");
7445 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7446 const struct glsl_shader_prog_link *program)
7448 const struct glsl_context_data *ctx_data;
7449 struct wined3d_context *context;
7450 unsigned int i;
7452 for (i = 0; i < device->context_count; ++i)
7454 context = device->contexts[i];
7455 ctx_data = context->shader_backend_data;
7457 if (ctx_data->glsl_program == program)
7458 shader_glsl_invalidate_current_program(context);
7462 static void shader_glsl_destroy(struct wined3d_shader *shader)
7464 struct glsl_shader_private *shader_data = shader->backend_data;
7465 struct wined3d_device *device = shader->device;
7466 struct shader_glsl_priv *priv = device->shader_priv;
7467 const struct wined3d_gl_info *gl_info;
7468 const struct list *linked_programs;
7469 struct wined3d_context *context;
7471 if (!shader_data || !shader_data->num_gl_shaders)
7473 HeapFree(GetProcessHeap(), 0, shader_data);
7474 shader->backend_data = NULL;
7475 return;
7478 context = context_acquire(device, NULL);
7479 gl_info = context->gl_info;
7481 TRACE("Deleting linked programs.\n");
7482 linked_programs = &shader->linked_programs;
7483 if (linked_programs->next)
7485 struct glsl_shader_prog_link *entry, *entry2;
7486 UINT i;
7488 switch (shader->reg_maps.shader_version.type)
7490 case WINED3D_SHADER_TYPE_PIXEL:
7492 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7494 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7496 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7497 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7498 checkGLcall("glDeleteShader");
7500 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7502 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7503 struct glsl_shader_prog_link, ps.shader_entry)
7505 shader_glsl_invalidate_contexts_program(device, entry);
7506 delete_glsl_program_entry(priv, gl_info, entry);
7509 break;
7512 case WINED3D_SHADER_TYPE_VERTEX:
7514 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7516 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7518 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7519 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7520 checkGLcall("glDeleteShader");
7522 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7524 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7525 struct glsl_shader_prog_link, vs.shader_entry)
7527 shader_glsl_invalidate_contexts_program(device, entry);
7528 delete_glsl_program_entry(priv, gl_info, entry);
7531 break;
7534 case WINED3D_SHADER_TYPE_GEOMETRY:
7536 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7538 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7540 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7541 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7542 checkGLcall("glDeleteShader");
7544 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7546 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7547 struct glsl_shader_prog_link, gs.shader_entry)
7549 shader_glsl_invalidate_contexts_program(device, entry);
7550 delete_glsl_program_entry(priv, gl_info, entry);
7553 break;
7556 default:
7557 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7558 break;
7562 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7563 shader->backend_data = NULL;
7565 context_release(context);
7568 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7570 const struct glsl_program_key *k = key;
7571 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7572 const struct glsl_shader_prog_link, program_lookup_entry);
7574 if (k->vs_id > prog->vs.id) return 1;
7575 else if (k->vs_id < prog->vs.id) return -1;
7577 if (k->gs_id > prog->gs.id) return 1;
7578 else if (k->gs_id < prog->gs.id) return -1;
7580 if (k->ps_id > prog->ps.id) return 1;
7581 else if (k->ps_id < prog->ps.id) return -1;
7583 return 0;
7586 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7588 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7589 + constant_count * sizeof(*heap->contained)
7590 + constant_count * sizeof(*heap->positions);
7591 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7593 if (!mem)
7595 ERR("Failed to allocate memory\n");
7596 return FALSE;
7599 heap->entries = mem;
7600 heap->entries[1].version = 0;
7601 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7602 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7603 heap->positions = (unsigned int *)(heap->contained + constant_count);
7604 heap->size = 1;
7606 return TRUE;
7609 static void constant_heap_free(struct constant_heap *heap)
7611 HeapFree(GetProcessHeap(), 0, heap->entries);
7614 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7616 wined3d_rb_alloc,
7617 wined3d_rb_realloc,
7618 wined3d_rb_free,
7619 glsl_program_key_compare,
7622 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7623 const struct fragment_pipeline *fragment_pipe)
7625 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7626 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7627 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7628 gl_info->limits.glsl_ps_float_constants)) + 1;
7629 struct fragment_caps fragment_caps;
7630 void *vertex_priv, *fragment_priv;
7632 string_buffer_list_init(&priv->string_buffers);
7634 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7636 ERR("Failed to initialize vertex pipe.\n");
7637 HeapFree(GetProcessHeap(), 0, priv);
7638 return E_FAIL;
7641 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7643 ERR("Failed to initialize fragment pipe.\n");
7644 vertex_pipe->vp_free(device);
7645 HeapFree(GetProcessHeap(), 0, priv);
7646 return E_FAIL;
7649 if (!string_buffer_init(&priv->shader_buffer))
7651 ERR("Failed to initialize shader buffer.\n");
7652 goto fail;
7655 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7656 if (!priv->stack)
7658 ERR("Failed to allocate memory.\n");
7659 goto fail;
7662 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7664 ERR("Failed to initialize vertex shader constant heap\n");
7665 goto fail;
7668 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7670 ERR("Failed to initialize pixel shader constant heap\n");
7671 goto fail;
7674 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7676 ERR("Failed to initialize rbtree.\n");
7677 goto fail;
7680 priv->next_constant_version = 1;
7681 priv->vertex_pipe = vertex_pipe;
7682 priv->fragment_pipe = fragment_pipe;
7683 fragment_pipe->get_caps(gl_info, &fragment_caps);
7684 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7685 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7687 device->vertex_priv = vertex_priv;
7688 device->fragment_priv = fragment_priv;
7689 device->shader_priv = priv;
7691 return WINED3D_OK;
7693 fail:
7694 constant_heap_free(&priv->pconst_heap);
7695 constant_heap_free(&priv->vconst_heap);
7696 HeapFree(GetProcessHeap(), 0, priv->stack);
7697 string_buffer_free(&priv->shader_buffer);
7698 fragment_pipe->free_private(device);
7699 vertex_pipe->vp_free(device);
7700 HeapFree(GetProcessHeap(), 0, priv);
7701 return E_OUTOFMEMORY;
7704 /* Context activation is done by the caller. */
7705 static void shader_glsl_free(struct wined3d_device *device)
7707 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7708 struct shader_glsl_priv *priv = device->shader_priv;
7709 int i;
7711 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7713 if (priv->depth_blt_program_full[i])
7715 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7717 if (priv->depth_blt_program_masked[i])
7719 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7723 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7724 constant_heap_free(&priv->pconst_heap);
7725 constant_heap_free(&priv->vconst_heap);
7726 HeapFree(GetProcessHeap(), 0, priv->stack);
7727 string_buffer_list_cleanup(&priv->string_buffers);
7728 string_buffer_free(&priv->shader_buffer);
7729 priv->fragment_pipe->free_private(device);
7730 priv->vertex_pipe->vp_free(device);
7732 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7733 device->shader_priv = NULL;
7736 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7738 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7739 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7742 static void shader_glsl_free_context_data(struct wined3d_context *context)
7744 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7747 static void shader_glsl_init_context_state(struct wined3d_context *context)
7749 const struct wined3d_gl_info *gl_info = context->gl_info;
7751 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
7752 checkGLcall("GL_PROGRAM_POINT_SIZE");
7755 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7757 UINT shader_model;
7759 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
7760 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7761 shader_model = 4;
7762 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7763 * texldd and texldl instructions. */
7764 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7765 shader_model = 3;
7766 else
7767 shader_model = 2;
7768 TRACE("Shader model %u.\n", shader_model);
7770 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7771 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7772 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7774 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7775 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7776 caps->varying_count = gl_info->limits.glsl_varyings;
7778 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7779 * Direct3D minimum requirement.
7781 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7782 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7784 * The problem is that the refrast clamps temporary results in the shader to
7785 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7786 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7787 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7788 * offer a way to query this.
7790 if (shader_model >= 4)
7791 caps->ps_1x_max_value = FLT_MAX;
7792 else
7793 caps->ps_1x_max_value = 1024.0f;
7795 /* Ideally we'd only set caps like sRGB writes here if supported by both
7796 * the shader backend and the fragment pipe, but we can get called before
7797 * shader_glsl_alloc(). */
7798 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7799 | WINED3D_SHADER_CAP_SRGB_WRITE;
7802 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7804 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7806 TRACE("Checking support for fixup:\n");
7807 dump_color_fixup_desc(fixup);
7810 /* We support everything except YUV conversions. */
7811 if (!is_complex_fixup(fixup))
7813 TRACE("[OK]\n");
7814 return TRUE;
7817 TRACE("[FAILED]\n");
7818 return FALSE;
7821 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7823 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7824 /* WINED3DSIH_ADD */ shader_glsl_binop,
7825 /* WINED3DSIH_AND */ shader_glsl_binop,
7826 /* WINED3DSIH_BEM */ shader_glsl_bem,
7827 /* WINED3DSIH_BREAK */ shader_glsl_break,
7828 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7829 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7830 /* WINED3DSIH_CALL */ shader_glsl_call,
7831 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7832 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7833 /* WINED3DSIH_CND */ shader_glsl_cnd,
7834 /* WINED3DSIH_CRS */ shader_glsl_cross,
7835 /* WINED3DSIH_CUT */ shader_glsl_cut,
7836 /* WINED3DSIH_DCL */ shader_glsl_nop,
7837 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7838 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7839 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7840 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7841 /* WINED3DSIH_DEF */ shader_glsl_nop,
7842 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7843 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7844 /* WINED3DSIH_DIV */ shader_glsl_binop,
7845 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7846 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7847 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7848 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7849 /* WINED3DSIH_DST */ shader_glsl_dst,
7850 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7851 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7852 /* WINED3DSIH_ELSE */ shader_glsl_else,
7853 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7854 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7855 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7856 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7857 /* WINED3DSIH_EQ */ shader_glsl_relop,
7858 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7859 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7860 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7861 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7862 /* WINED3DSIH_GE */ shader_glsl_relop,
7863 /* WINED3DSIH_IADD */ shader_glsl_binop,
7864 /* WINED3DSIH_IEQ */ NULL,
7865 /* WINED3DSIH_IF */ shader_glsl_if,
7866 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7867 /* WINED3DSIH_IGE */ shader_glsl_relop,
7868 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7869 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7870 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7871 /* WINED3DSIH_LABEL */ shader_glsl_label,
7872 /* WINED3DSIH_LD */ NULL,
7873 /* WINED3DSIH_LIT */ shader_glsl_lit,
7874 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7875 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7876 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7877 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7878 /* WINED3DSIH_LT */ shader_glsl_relop,
7879 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7880 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7881 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7882 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7883 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7884 /* WINED3DSIH_MAD */ shader_glsl_mad,
7885 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7886 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7887 /* WINED3DSIH_MOV */ shader_glsl_mov,
7888 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7889 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7890 /* WINED3DSIH_MUL */ shader_glsl_binop,
7891 /* WINED3DSIH_NE */ shader_glsl_relop,
7892 /* WINED3DSIH_NOP */ shader_glsl_nop,
7893 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7894 /* WINED3DSIH_OR */ shader_glsl_binop,
7895 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7896 /* WINED3DSIH_POW */ shader_glsl_pow,
7897 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7898 /* WINED3DSIH_REP */ shader_glsl_rep,
7899 /* WINED3DSIH_RET */ shader_glsl_ret,
7900 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7901 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7902 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7903 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7904 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7905 /* WINED3DSIH_SETP */ NULL,
7906 /* WINED3DSIH_SGE */ shader_glsl_compare,
7907 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7908 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7909 /* WINED3DSIH_SLT */ shader_glsl_compare,
7910 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7911 /* WINED3DSIH_SUB */ shader_glsl_binop,
7912 /* WINED3DSIH_TEX */ shader_glsl_tex,
7913 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7914 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7915 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7916 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7917 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7918 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7919 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7920 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7921 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7922 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7923 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7924 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7925 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7926 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7927 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7928 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7929 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7930 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7931 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7932 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7933 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7934 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7935 /* WINED3DSIH_UGE */ shader_glsl_relop,
7936 /* WINED3DSIH_USHR */ shader_glsl_binop,
7937 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7938 /* WINED3DSIH_XOR */ shader_glsl_binop,
7941 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7942 SHADER_HANDLER hw_fct;
7944 /* Select handler */
7945 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7947 /* Unhandled opcode */
7948 if (!hw_fct)
7950 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7951 return;
7953 hw_fct(ins);
7955 shader_glsl_add_instruction_modifiers(ins);
7958 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7960 struct shader_glsl_priv *priv = shader_priv;
7962 return priv->ffp_proj_control;
7965 const struct wined3d_shader_backend_ops glsl_shader_backend =
7967 shader_glsl_handle_instruction,
7968 shader_glsl_select,
7969 shader_glsl_disable,
7970 shader_glsl_select_depth_blt,
7971 shader_glsl_deselect_depth_blt,
7972 shader_glsl_update_float_vertex_constants,
7973 shader_glsl_update_float_pixel_constants,
7974 shader_glsl_load_constants,
7975 shader_glsl_destroy,
7976 shader_glsl_alloc,
7977 shader_glsl_free,
7978 shader_glsl_allocate_context_data,
7979 shader_glsl_free_context_data,
7980 shader_glsl_init_context_state,
7981 shader_glsl_get_caps,
7982 shader_glsl_color_fixup_supported,
7983 shader_glsl_has_ffp_proj_control,
7986 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
7988 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7990 caps->xyzrhw = TRUE;
7991 caps->ffp_generic_attributes = TRUE;
7992 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7993 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
7994 caps->max_vertex_blend_matrix_index = 0;
7995 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7996 | WINED3DVTXPCAPS_MATERIALSOURCE7
7997 | WINED3DVTXPCAPS_VERTEXFOG
7998 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7999 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8000 | WINED3DVTXPCAPS_LOCALVIEWER
8001 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8002 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8003 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8004 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8007 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8009 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8010 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8011 return 0;
8014 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8016 struct shader_glsl_priv *priv;
8018 if (shader_backend == &glsl_shader_backend)
8020 priv = shader_priv;
8022 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8024 ERR("Failed to initialize rbtree.\n");
8025 return NULL;
8028 return priv;
8031 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8033 return NULL;
8036 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8038 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8039 struct glsl_ffp_vertex_shader, desc.entry);
8040 struct glsl_shader_prog_link *program, *program2;
8041 struct glsl_ffp_destroy_ctx *ctx = context;
8043 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8044 struct glsl_shader_prog_link, vs.shader_entry)
8046 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8048 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8049 HeapFree(GetProcessHeap(), 0, shader);
8052 /* Context activation is done by the caller. */
8053 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8055 struct shader_glsl_priv *priv = device->vertex_priv;
8056 struct glsl_ffp_destroy_ctx ctx;
8058 ctx.priv = priv;
8059 ctx.gl_info = &device->adapter->gl_info;
8060 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8063 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8064 const struct wined3d_state *state, DWORD state_id)
8066 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8069 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8070 const struct wined3d_state *state, DWORD state_id)
8072 const struct wined3d_gl_info *gl_info = context->gl_info;
8073 BOOL transformed = context->stream_info.position_transformed;
8074 BOOL wasrhw = context->last_was_rhw;
8075 unsigned int i;
8077 context->last_was_rhw = transformed;
8079 /* If the vertex declaration contains a transformed position attribute,
8080 * the draw uses the fixed function vertex pipeline regardless of any
8081 * vertex shader set by the application. */
8082 if (transformed != wasrhw)
8083 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8085 if (!use_vs(state))
8087 if (context->last_was_vshader)
8089 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8090 clipplane(context, state, STATE_CLIPPLANE(i));
8093 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8095 /* Because of settings->texcoords, we have to regenerate the vertex
8096 * shader on a vdecl change if there aren't enough varyings to just
8097 * always output all the texture coordinates. */
8098 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info))
8099 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8101 if (use_ps(state)
8102 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8103 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8104 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8106 else
8108 if (!context->last_was_vshader)
8110 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8111 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8112 clipplane(context, state, STATE_CLIPPLANE(i));
8116 context->last_was_vshader = use_vs(state);
8119 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8120 const struct wined3d_state *state, DWORD state_id)
8122 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8123 /* Different vertex shaders potentially require a different vertex attributes setup. */
8124 if (!isStateDirty(context, STATE_VDECL))
8125 context_apply_state(context, state, STATE_VDECL);
8128 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8129 const struct wined3d_state *state, DWORD state_id)
8131 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8134 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8135 const struct wined3d_state *state, DWORD state_id)
8137 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8140 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8142 const struct wined3d_gl_info *gl_info = context->gl_info;
8143 unsigned int k;
8145 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8146 | WINED3D_SHADER_CONST_FFP_LIGHTS
8147 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8149 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8151 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8152 clipplane(context, state, STATE_CLIPPLANE(k));
8156 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8157 const struct wined3d_state *state, DWORD state_id)
8159 /* Table fog behavior depends on the projection matrix. */
8160 if (state->render_states[WINED3D_RS_FOGENABLE]
8161 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8162 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8163 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8166 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8167 const struct wined3d_state *state, DWORD state_id)
8169 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8170 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8171 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8172 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8173 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8174 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8177 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8178 const struct wined3d_state *state, DWORD state_id)
8180 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8183 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8184 const struct wined3d_state *state, DWORD state_id)
8186 DWORD sampler = state_id - STATE_SAMPLER(0);
8187 const struct wined3d_texture *texture = state->textures[sampler];
8188 BOOL np2;
8190 if (!texture)
8191 return;
8193 if (sampler >= MAX_TEXTURES)
8194 return;
8196 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8197 || context->lastWasPow2Texture & (1u << sampler))
8199 if (np2)
8200 context->lastWasPow2Texture |= 1u << sampler;
8201 else
8202 context->lastWasPow2Texture &= ~(1u << sampler);
8204 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8208 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8209 const struct wined3d_state *state, DWORD state_id)
8211 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8214 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8215 const struct wined3d_state *state, DWORD state_id)
8217 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8220 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8221 const struct wined3d_state *state, DWORD state_id)
8223 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8226 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8227 const struct wined3d_state *state, DWORD state_id)
8229 if (!use_vs(state))
8230 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8233 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8235 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8236 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8237 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8238 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8239 /* Clip planes */
8240 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8241 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8242 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8243 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8244 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8245 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8246 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8247 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8248 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8249 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8250 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8251 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8252 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8253 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8254 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8255 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8256 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8257 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8258 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8259 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8260 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8261 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8262 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8263 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8264 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8265 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8266 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8267 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8268 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8269 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8270 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8271 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8272 /* Lights */
8273 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8274 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8275 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8276 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8277 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8278 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8279 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8280 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8281 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8282 /* Viewport */
8283 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8284 /* Transform states */
8285 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8286 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8287 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8288 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8289 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8290 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8291 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8292 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8293 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8294 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8295 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8296 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8297 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8298 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8299 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8300 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8301 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8302 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8303 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8304 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8305 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8306 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8307 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8308 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8309 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8310 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8311 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8312 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8313 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8314 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8315 /* Fog */
8316 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8317 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8318 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8319 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8320 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8321 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8322 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8323 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8324 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8325 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8326 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8327 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8328 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8329 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8330 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8331 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8332 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8333 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8334 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8335 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8336 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8337 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8338 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8339 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8340 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8341 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8342 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8343 /* NP2 texture matrix fixups. They are not needed if
8344 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8345 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8346 * matrix. */
8347 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8348 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8349 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8350 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8351 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8352 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8353 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8354 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8355 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8356 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8357 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8358 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8359 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8360 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8361 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8362 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8363 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8364 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8365 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8366 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8367 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8368 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8369 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8370 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8371 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8372 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8375 /* TODO:
8376 * - Implement vertex tweening. */
8377 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8379 glsl_vertex_pipe_vp_enable,
8380 glsl_vertex_pipe_vp_get_caps,
8381 glsl_vertex_pipe_vp_get_emul_mask,
8382 glsl_vertex_pipe_vp_alloc,
8383 glsl_vertex_pipe_vp_free,
8384 glsl_vertex_pipe_vp_states,
8387 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8389 /* Nothing to do. */
8392 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8394 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8395 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8396 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8397 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8398 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8399 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8400 | WINED3DTEXOPCAPS_SELECTARG1
8401 | WINED3DTEXOPCAPS_SELECTARG2
8402 | WINED3DTEXOPCAPS_MODULATE4X
8403 | WINED3DTEXOPCAPS_MODULATE2X
8404 | WINED3DTEXOPCAPS_MODULATE
8405 | WINED3DTEXOPCAPS_ADDSIGNED2X
8406 | WINED3DTEXOPCAPS_ADDSIGNED
8407 | WINED3DTEXOPCAPS_ADD
8408 | WINED3DTEXOPCAPS_SUBTRACT
8409 | WINED3DTEXOPCAPS_ADDSMOOTH
8410 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8411 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8412 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8413 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8414 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8415 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8416 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8417 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8418 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8419 | WINED3DTEXOPCAPS_DOTPRODUCT3
8420 | WINED3DTEXOPCAPS_MULTIPLYADD
8421 | WINED3DTEXOPCAPS_LERP
8422 | WINED3DTEXOPCAPS_BUMPENVMAP
8423 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8424 caps->MaxTextureBlendStages = 8;
8425 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8428 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8430 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8431 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8432 return 0;
8435 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8437 struct shader_glsl_priv *priv;
8439 if (shader_backend == &glsl_shader_backend)
8441 priv = shader_priv;
8443 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8445 ERR("Failed to initialize rbtree.\n");
8446 return NULL;
8449 return priv;
8452 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8454 return NULL;
8457 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8459 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8460 struct glsl_ffp_fragment_shader, entry.entry);
8461 struct glsl_shader_prog_link *program, *program2;
8462 struct glsl_ffp_destroy_ctx *ctx = context;
8464 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8465 struct glsl_shader_prog_link, ps.shader_entry)
8467 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8469 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8470 HeapFree(GetProcessHeap(), 0, shader);
8473 /* Context activation is done by the caller. */
8474 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8476 struct shader_glsl_priv *priv = device->fragment_priv;
8477 struct glsl_ffp_destroy_ctx ctx;
8479 ctx.priv = priv;
8480 ctx.gl_info = &device->adapter->gl_info;
8481 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8484 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8485 const struct wined3d_state *state, DWORD state_id)
8487 context->last_was_pshader = use_ps(state);
8489 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8492 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8493 const struct wined3d_state *state, DWORD state_id)
8495 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8498 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8499 const struct wined3d_state *state, DWORD state_id)
8501 BOOL use_vshader = use_vs(state);
8502 enum fogsource new_source;
8503 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8504 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8506 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8508 if (!state->render_states[WINED3D_RS_FOGENABLE])
8509 return;
8511 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8513 if (use_vshader)
8514 new_source = FOGSOURCE_VS;
8515 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8516 new_source = FOGSOURCE_COORD;
8517 else
8518 new_source = FOGSOURCE_FFP;
8520 else
8522 new_source = FOGSOURCE_FFP;
8525 if (new_source != context->fog_source || fogstart == fogend)
8527 context->fog_source = new_source;
8528 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8532 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8533 const struct wined3d_state *state, DWORD state_id)
8535 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8536 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8537 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8539 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8540 glsl_fragment_pipe_fog(context, state, state_id);
8543 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8544 const struct wined3d_state *state, DWORD state_id)
8546 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8547 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8548 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8551 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8552 const struct wined3d_state *state, DWORD state_id)
8554 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8557 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8558 const struct wined3d_state *state, DWORD state_id)
8560 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8563 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8564 const struct wined3d_state *state, DWORD state_id)
8566 const struct wined3d_gl_info *gl_info = context->gl_info;
8567 int glParm;
8568 float ref;
8570 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8572 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8574 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8575 checkGLcall("glEnable GL_ALPHA_TEST");
8577 else
8579 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8580 checkGLcall("glDisable GL_ALPHA_TEST");
8581 return;
8584 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8585 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8587 if (glParm)
8589 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8590 checkGLcall("glAlphaFunc");
8594 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8595 const struct wined3d_state *state, DWORD state_id)
8597 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8600 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8602 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8603 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8604 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8605 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8606 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8607 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8608 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8609 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8610 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8611 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8612 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8613 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8614 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8615 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8616 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8617 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8618 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8619 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8620 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8621 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8622 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8623 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8624 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8625 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8626 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8627 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8628 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8629 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8630 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8631 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8632 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8633 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8634 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8635 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8636 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8637 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8638 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8639 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8640 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8641 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8642 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8643 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8644 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8645 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8646 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8647 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8648 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8649 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8650 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8651 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8652 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8653 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8654 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8655 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8656 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8657 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8658 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8659 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8660 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8661 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8662 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8663 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8664 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8665 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8666 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8667 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8668 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8669 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8670 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8671 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8672 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8673 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8674 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8675 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8676 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8677 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8678 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8679 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8680 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8681 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8682 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8683 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8684 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8685 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8686 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8687 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8688 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8689 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8690 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8691 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8692 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8693 {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 },
8694 {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 },
8695 {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 },
8696 {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 },
8697 {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 },
8698 {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 },
8699 {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 },
8700 {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 },
8701 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8702 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8703 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8704 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8705 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8706 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8707 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8708 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8709 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8710 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8711 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8714 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8716 return TRUE;
8719 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8723 const struct fragment_pipeline glsl_fragment_pipe =
8725 glsl_fragment_pipe_enable,
8726 glsl_fragment_pipe_get_caps,
8727 glsl_fragment_pipe_get_emul_mask,
8728 glsl_fragment_pipe_alloc,
8729 glsl_fragment_pipe_free,
8730 glsl_fragment_pipe_alloc_context_data,
8731 glsl_fragment_pipe_free_context_data,
8732 shader_glsl_color_fixup_supported,
8733 glsl_fragment_pipe_state_template,