advapi32/tests: Changed an invalid comment.
[wine.git] / dlls / wined3d / glsl_shader.c
blob515e815eb334cb4ce4f1b4c0679eb5df9409753c
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_constants);
45 WINE_DECLARE_DEBUG_CHANNEL(d3d);
46 WINE_DECLARE_DEBUG_CHANNEL(winediag);
48 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
49 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
50 #define WINED3D_GLSL_SAMPLE_LOD 0x4
51 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
53 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 const char *name;
68 DWORD coord_mask;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_shader_buffer shader_buffer;
95 struct wine_rb_tree program_lookup;
96 struct constant_heap vconst_heap;
97 struct constant_heap pconst_heap;
98 unsigned char *stack;
99 GLhandleARB depth_blt_program_full[tex_type_count];
100 GLhandleARB depth_blt_program_masked[tex_type_count];
101 UINT next_constant_version;
103 const struct wined3d_vertex_pipe_ops *vertex_pipe;
104 const struct fragment_pipeline *fragment_pipe;
105 struct wine_rb_tree ffp_vertex_shaders;
106 struct wine_rb_tree ffp_fragment_shaders;
107 BOOL ffp_proj_control;
110 struct glsl_vs_program
112 struct list shader_entry;
113 GLhandleARB id;
114 GLenum vertex_color_clamp;
115 GLint *uniform_f_locations;
116 GLint uniform_i_locations[MAX_CONST_I];
117 GLint pos_fixup_location;
120 struct glsl_gs_program
122 struct list shader_entry;
123 GLhandleARB id;
126 struct glsl_ps_program
128 struct list shader_entry;
129 GLhandleARB id;
130 GLint *uniform_f_locations;
131 GLint uniform_i_locations[MAX_CONST_I];
132 GLint bumpenv_mat_location[MAX_TEXTURES];
133 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
134 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
135 GLint tss_constant_location[MAX_TEXTURES];
136 GLint tex_factor_location;
137 GLint specular_enable_location;
138 GLint ycorrection_location;
139 GLint np2_fixup_location;
140 const struct ps_np2fixup_info *np2_fixup_info;
143 /* Struct to maintain data about a linked GLSL program */
144 struct glsl_shader_prog_link
146 struct wine_rb_entry program_lookup_entry;
147 struct glsl_vs_program vs;
148 struct glsl_gs_program gs;
149 struct glsl_ps_program ps;
150 GLhandleARB programId;
151 DWORD constant_update_mask;
152 UINT constant_version;
155 struct glsl_program_key
157 GLhandleARB vs_id;
158 GLhandleARB gs_id;
159 GLhandleARB ps_id;
162 struct shader_glsl_ctx_priv {
163 const struct vs_compile_args *cur_vs_args;
164 const struct ps_compile_args *cur_ps_args;
165 struct ps_np2fixup_info *cur_np2fixup_info;
168 struct glsl_context_data
170 struct glsl_shader_prog_link *glsl_program;
173 struct glsl_ps_compiled_shader
175 struct ps_compile_args args;
176 struct ps_np2fixup_info np2fixup;
177 GLhandleARB prgId;
180 struct glsl_vs_compiled_shader
182 struct vs_compile_args args;
183 GLhandleARB prgId;
186 struct glsl_gs_compiled_shader
188 GLhandleARB id;
191 struct glsl_shader_private
193 union
195 struct glsl_vs_compiled_shader *vs;
196 struct glsl_gs_compiled_shader *gs;
197 struct glsl_ps_compiled_shader *ps;
198 } gl_shaders;
199 UINT num_gl_shaders, shader_array_size;
202 struct glsl_ffp_vertex_shader
204 struct wined3d_ffp_vs_desc desc;
205 GLhandleARB id;
206 struct list linked_programs;
209 struct glsl_ffp_fragment_shader
211 struct ffp_frag_desc entry;
212 GLhandleARB id;
213 struct list linked_programs;
216 struct glsl_ffp_destroy_ctx
218 struct shader_glsl_priv *priv;
219 const struct wined3d_gl_info *gl_info;
222 static const char *debug_gl_shader_type(GLenum type)
224 switch (type)
226 #define WINED3D_TO_STR(u) case u: return #u
227 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
228 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
229 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
230 #undef WINED3D_TO_STR
231 default:
232 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
236 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
238 switch (type)
240 case WINED3D_SHADER_TYPE_VERTEX:
241 return "vs";
243 case WINED3D_SHADER_TYPE_GEOMETRY:
244 return "gs";
246 case WINED3D_SHADER_TYPE_PIXEL:
247 return "ps";
249 default:
250 FIXME("Unhandled shader type %#x.\n", type);
251 return "unknown";
255 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
257 char str[4][17];
259 wined3d_ftoa(values[0], str[0]);
260 wined3d_ftoa(values[1], str[1]);
261 wined3d_ftoa(values[2], str[2]);
262 wined3d_ftoa(values[3], str[3]);
263 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
266 /* Extract a line from the info log.
267 * Note that this modifies the source string. */
268 static char *get_info_log_line(char **ptr)
270 char *p, *q;
272 p = *ptr;
273 if (!(q = strstr(p, "\n")))
275 if (!*p) return NULL;
276 *ptr += strlen(p);
277 return p;
279 *q = '\0';
280 *ptr = q + 1;
282 return p;
285 /* Context activation is done by the caller. */
286 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
288 int infologLength = 0;
289 char *infoLog;
291 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
292 return;
294 GL_EXTCALL(glGetObjectParameterivARB(obj,
295 GL_OBJECT_INFO_LOG_LENGTH_ARB,
296 &infologLength));
298 /* A size of 1 is just a null-terminated string, so the log should be bigger than
299 * that if there are errors. */
300 if (infologLength > 1)
302 char *ptr, *line;
304 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
305 /* The info log is supposed to be zero-terminated, but at least some
306 * versions of fglrx don't terminate the string properly. The reported
307 * length does include the terminator, so explicitly set it to zero
308 * here. */
309 infoLog[infologLength - 1] = 0;
310 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
312 ptr = infoLog;
313 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
315 WARN("Info log received from GLSL shader #%u:\n", obj);
316 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
318 else
320 FIXME("Info log received from GLSL shader #%u:\n", obj);
321 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
323 HeapFree(GetProcessHeap(), 0, infoLog);
327 /* Context activation is done by the caller. */
328 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
330 TRACE("Compiling shader object %u.\n", shader);
331 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
332 checkGLcall("glShaderSourceARB");
333 GL_EXTCALL(glCompileShaderARB(shader));
334 checkGLcall("glCompileShaderARB");
335 print_glsl_info_log(gl_info, shader);
338 /* Context activation is done by the caller. */
339 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
341 GLint i, object_count, source_size = -1;
342 GLhandleARB *objects;
343 char *source = NULL;
345 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
346 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
347 if (!objects)
349 ERR("Failed to allocate object array memory.\n");
350 return;
353 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
354 for (i = 0; i < object_count; ++i)
356 char *ptr, *line;
357 GLint tmp;
359 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
361 if (source_size < tmp)
363 HeapFree(GetProcessHeap(), 0, source);
365 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
366 if (!source)
368 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
369 HeapFree(GetProcessHeap(), 0, objects);
370 return;
372 source_size = tmp;
375 FIXME("Object %u:\n", objects[i]);
376 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
377 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
378 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
379 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
380 FIXME("\n");
382 ptr = source;
383 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
384 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
385 FIXME("\n");
388 HeapFree(GetProcessHeap(), 0, source);
389 HeapFree(GetProcessHeap(), 0, objects);
392 /* Context activation is done by the caller. */
393 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
395 GLint tmp;
397 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
399 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
400 if (tmp == GL_PROGRAM_OBJECT_ARB)
402 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
403 if (!tmp)
405 FIXME("Program %u link status invalid.\n", program);
406 shader_glsl_dump_program_source(gl_info, program);
410 print_glsl_info_log(gl_info, program);
413 /* Context activation is done by the caller. */
414 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
415 const DWORD *tex_unit_map, GLhandleARB programId)
417 GLint name_loc;
418 char sampler_name[20];
419 unsigned int i;
421 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
423 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
424 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
425 if (name_loc != -1) {
426 DWORD mapped_unit = tex_unit_map[i];
427 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
429 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
430 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
431 checkGLcall("glUniform1iARB");
432 } else {
433 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
439 /* Context activation is done by the caller. */
440 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
441 const DWORD *tex_unit_map, GLhandleARB programId)
443 GLint name_loc;
444 char sampler_name[20];
445 unsigned int i;
447 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
449 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
450 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
451 if (name_loc != -1) {
452 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
453 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
455 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
456 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
457 checkGLcall("glUniform1iARB");
458 } else {
459 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
465 /* Context activation is done by the caller. */
466 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
467 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
469 unsigned int start = ~0U, end = 0;
470 int stack_idx = 0;
471 unsigned int heap_idx = 1;
472 unsigned int idx;
474 if (heap->entries[heap_idx].version <= version) return;
476 idx = heap->entries[heap_idx].idx;
477 if (constant_locations[idx] != -1)
478 start = end = idx;
479 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
481 while (stack_idx >= 0)
483 /* Note that we fall through to the next case statement. */
484 switch(stack[stack_idx])
486 case HEAP_NODE_TRAVERSE_LEFT:
488 unsigned int left_idx = heap_idx << 1;
489 if (left_idx < heap->size && heap->entries[left_idx].version > version)
491 heap_idx = left_idx;
492 idx = heap->entries[heap_idx].idx;
493 if (constant_locations[idx] != -1)
495 if (start > idx)
496 start = idx;
497 if (end < idx)
498 end = idx;
501 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
502 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
503 break;
507 case HEAP_NODE_TRAVERSE_RIGHT:
509 unsigned int right_idx = (heap_idx << 1) + 1;
510 if (right_idx < heap->size && heap->entries[right_idx].version > version)
512 heap_idx = right_idx;
513 idx = heap->entries[heap_idx].idx;
514 if (constant_locations[idx] != -1)
516 if (start > idx)
517 start = idx;
518 if (end < idx)
519 end = idx;
522 stack[stack_idx++] = HEAP_NODE_POP;
523 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
524 break;
528 case HEAP_NODE_POP:
529 heap_idx >>= 1;
530 --stack_idx;
531 break;
534 if (start <= end)
535 GL_EXTCALL(glUniform4fvARB(constant_locations[start], end - start + 1, &constants[start * 4]));
536 checkGLcall("walk_constant_heap()");
539 /* Context activation is done by the caller. */
540 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
542 GLfloat clamped_constant[4];
544 if (location == -1) return;
546 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
547 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
548 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
549 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
551 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
554 /* Context activation is done by the caller. */
555 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
556 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
558 int stack_idx = 0;
559 unsigned int heap_idx = 1;
560 unsigned int idx;
562 if (heap->entries[heap_idx].version <= version) return;
564 idx = heap->entries[heap_idx].idx;
565 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
566 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
568 while (stack_idx >= 0)
570 /* Note that we fall through to the next case statement. */
571 switch(stack[stack_idx])
573 case HEAP_NODE_TRAVERSE_LEFT:
575 unsigned int left_idx = heap_idx << 1;
576 if (left_idx < heap->size && heap->entries[left_idx].version > version)
578 heap_idx = left_idx;
579 idx = heap->entries[heap_idx].idx;
580 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
582 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
583 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
584 break;
588 case HEAP_NODE_TRAVERSE_RIGHT:
590 unsigned int right_idx = (heap_idx << 1) + 1;
591 if (right_idx < heap->size && heap->entries[right_idx].version > version)
593 heap_idx = right_idx;
594 idx = heap->entries[heap_idx].idx;
595 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
597 stack[stack_idx++] = HEAP_NODE_POP;
598 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
599 break;
603 case HEAP_NODE_POP:
604 heap_idx >>= 1;
605 --stack_idx;
606 break;
609 checkGLcall("walk_constant_heap_clamped()");
612 /* Context activation is done by the caller. */
613 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
614 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
615 unsigned char *stack, UINT version)
617 const struct wined3d_shader_lconst *lconst;
619 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
620 if (shader->reg_maps.shader_version.major == 1
621 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
622 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
623 else
624 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
626 if (!shader->load_local_constsF)
628 TRACE("No need to load local float constants for this shader\n");
629 return;
632 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
633 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
635 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
637 checkGLcall("glUniform4fvARB()");
640 /* Context activation is done by the caller. */
641 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
642 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
644 unsigned int i;
645 struct list* ptr;
647 for (i = 0; constants_set; constants_set >>= 1, ++i)
649 if (!(constants_set & 1)) continue;
651 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
652 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
654 /* We found this uniform name in the program - go ahead and send the data */
655 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
656 checkGLcall("glUniform4ivARB");
659 /* Load immediate constants */
660 ptr = list_head(&shader->constantsI);
661 while (ptr)
663 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
664 unsigned int idx = lconst->idx;
665 const GLint *values = (const GLint *)lconst->value;
667 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
668 values[0], values[1], values[2], values[3]);
670 /* We found this uniform name in the program - go ahead and send the data */
671 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
672 checkGLcall("glUniform4ivARB");
673 ptr = list_next(&shader->constantsI, ptr);
677 /* Context activation is done by the caller. */
678 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
679 GLhandleARB programId, const BOOL *constants, WORD constants_set)
681 GLint tmp_loc;
682 unsigned int i;
683 char tmp_name[10];
684 const char *prefix;
685 struct list* ptr;
687 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
689 /* TODO: Benchmark and see if it would be beneficial to store the
690 * locations of the constants to avoid looking up each time */
691 for (i = 0; constants_set; constants_set >>= 1, ++i)
693 if (!(constants_set & 1)) continue;
695 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
697 /* TODO: Benchmark and see if it would be beneficial to store the
698 * locations of the constants to avoid looking up each time */
699 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
700 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
701 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
704 /* Load immediate constants */
705 ptr = list_head(&shader->constantsB);
706 while (ptr)
708 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
709 unsigned int idx = lconst->idx;
710 const GLint *values = (const GLint *)lconst->value;
712 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
714 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
715 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
716 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
717 ptr = list_next(&shader->constantsB, ptr);
720 checkGLcall("shader_glsl_load_constantsB()");
723 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
725 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
728 /* Context activation is done by the caller (state handler). */
729 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
730 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
732 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
733 UINT fixup = ps->np2_fixup_info->active;
734 UINT i;
736 for (i = 0; fixup; fixup >>= 1, ++i)
738 const struct wined3d_texture *tex = state->textures[i];
739 unsigned char idx = ps->np2_fixup_info->idx[i];
740 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
742 if (!tex)
744 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
745 continue;
748 if (idx % 2)
750 tex_dim[2] = tex->pow2_matrix[0];
751 tex_dim[3] = tex->pow2_matrix[5];
753 else
755 tex_dim[0] = tex->pow2_matrix[0];
756 tex_dim[1] = tex->pow2_matrix[5];
760 GL_EXTCALL(glUniform4fvARB(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
763 /* Context activation is done by the caller (state handler). */
764 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
765 const struct wined3d_state *state)
767 const struct glsl_context_data *ctx_data = context->shader_backend_data;
768 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
769 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
770 const struct wined3d_gl_info *gl_info = context->gl_info;
771 struct shader_glsl_priv *priv = shader_priv;
772 float position_fixup[4];
773 DWORD update_mask = 0;
775 GLhandleARB programId;
776 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
777 UINT constant_version;
778 int i;
780 if (!prog) {
781 /* No GLSL program set - nothing to do. */
782 return;
784 programId = prog->programId;
785 constant_version = prog->constant_version;
786 update_mask = context->constant_update_mask & prog->constant_update_mask;
788 if (update_mask & WINED3D_SHADER_CONST_VS_F)
789 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
790 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
792 if (update_mask & WINED3D_SHADER_CONST_VS_I)
793 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
794 vshader->reg_maps.integer_constants);
796 if (update_mask & WINED3D_SHADER_CONST_VS_B)
797 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
798 vshader->reg_maps.boolean_constants);
800 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
802 shader_get_position_fixup(context, state, position_fixup);
803 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
804 checkGLcall("glUniform4fvARB");
807 if (update_mask & WINED3D_SHADER_CONST_PS_F)
808 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
809 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
811 if (update_mask & WINED3D_SHADER_CONST_PS_I)
812 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
813 pshader->reg_maps.integer_constants);
815 if (update_mask & WINED3D_SHADER_CONST_PS_B)
816 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
817 pshader->reg_maps.boolean_constants);
819 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
821 for (i = 0; i < MAX_TEXTURES; ++i)
823 if (prog->ps.bumpenv_mat_location[i] == -1)
824 continue;
826 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
827 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
829 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
831 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1,
832 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
833 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1,
834 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
838 checkGLcall("bump env uniforms");
841 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
843 float correction_params[4];
845 if (context->render_offscreen)
847 correction_params[0] = 0.0f;
848 correction_params[1] = 1.0f;
849 } else {
850 /* position is window relative, not viewport relative */
851 correction_params[0] = (float) context->current_rt->resource.height;
852 correction_params[1] = -1.0f;
854 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
857 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
858 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
860 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
862 float col[4];
864 if (prog->ps.tex_factor_location != -1)
866 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
867 GL_EXTCALL(glUniform4fvARB(prog->ps.tex_factor_location, 1, col));
870 if (state->render_states[WINED3D_RS_SPECULARENABLE])
871 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
872 else
873 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
875 for (i = 0; i < MAX_TEXTURES; ++i)
877 if (prog->ps.tss_constant_location[i] == -1)
878 continue;
880 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
881 GL_EXTCALL(glUniform4fvARB(prog->ps.tss_constant_location[i], 1, col));
884 checkGLcall("fixed function uniforms");
887 if (priv->next_constant_version == UINT_MAX)
889 TRACE("Max constant version reached, resetting to 0.\n");
890 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
891 priv->next_constant_version = 1;
893 else
895 prog->constant_version = priv->next_constant_version++;
899 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
901 struct constant_entry *entries = heap->entries;
902 unsigned int *positions = heap->positions;
903 unsigned int heap_idx, parent_idx;
905 if (!heap->contained[idx])
907 heap_idx = heap->size++;
908 heap->contained[idx] = TRUE;
910 else
912 heap_idx = positions[idx];
915 while (heap_idx > 1)
917 parent_idx = heap_idx >> 1;
919 if (new_version <= entries[parent_idx].version) break;
921 entries[heap_idx] = entries[parent_idx];
922 positions[entries[parent_idx].idx] = heap_idx;
923 heap_idx = parent_idx;
926 entries[heap_idx].version = new_version;
927 entries[heap_idx].idx = idx;
928 positions[idx] = heap_idx;
931 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
933 struct shader_glsl_priv *priv = device->shader_priv;
934 struct constant_heap *heap = &priv->vconst_heap;
935 UINT i;
937 for (i = start; i < count + start; ++i)
939 update_heap_entry(heap, i, priv->next_constant_version);
942 for (i = 0; i < device->context_count; ++i)
944 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
948 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
950 struct shader_glsl_priv *priv = device->shader_priv;
951 struct constant_heap *heap = &priv->pconst_heap;
952 UINT i;
954 for (i = start; i < count + start; ++i)
956 update_heap_entry(heap, i, priv->next_constant_version);
959 for (i = 0; i < device->context_count; ++i)
961 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
965 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
967 unsigned int ret = gl_info->limits.glsl_varyings / 4;
968 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
969 if(shader_major > 3) return ret;
971 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
972 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
973 return ret;
976 /** Generate the variable & register declarations for the GLSL output target */
977 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
978 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
979 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
981 const struct wined3d_shader_version *version = &reg_maps->shader_version;
982 const struct wined3d_state *state = &shader->device->state;
983 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
984 const struct wined3d_gl_info *gl_info = context->gl_info;
985 const struct wined3d_fb_state *fb = &shader->device->fb;
986 unsigned int i, extra_constants_needed = 0;
987 const struct wined3d_shader_lconst *lconst;
988 const char *prefix;
989 DWORD map;
991 prefix = shader_glsl_get_prefix(version->type);
993 /* Prototype the subroutines */
994 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
996 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
999 /* Declare the constants (aka uniforms) */
1000 if (shader->limits.constant_float > 0)
1002 unsigned max_constantsF;
1004 /* Unless the shader uses indirect addressing, always declare the
1005 * maximum array size and ignore that we need some uniforms privately.
1006 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1007 * and immediate values, still declare VC[256]. If the shader needs
1008 * more uniforms than we have it won't work in any case. If it uses
1009 * less, the compiler will figure out which uniforms are really used
1010 * and strip them out. This allows a shader to use c255 on a dx9 card,
1011 * as long as it doesn't also use all the other constants.
1013 * If the shader uses indirect addressing the compiler must assume
1014 * that all declared uniforms are used. In this case, declare only the
1015 * amount that we're assured to have.
1017 * Thus we run into problems in these two cases:
1018 * 1) The shader really uses more uniforms than supported.
1019 * 2) The shader uses indirect addressing, less constants than
1020 * supported, but uses a constant index > #supported consts. */
1021 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1023 /* No indirect addressing here. */
1024 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1026 else
1028 if (reg_maps->usesrelconstF)
1030 /* Subtract the other potential uniforms from the max
1031 * available (bools, ints, and 1 row of projection matrix).
1032 * Subtract another uniform for immediate values, which have
1033 * to be loaded via uniform by the driver as well. The shader
1034 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1035 * shader code, so one vec4 should be enough. (Unfortunately
1036 * the Nvidia driver doesn't store 128 and -128 in one float).
1038 * Writing gl_ClipVertex requires one uniform for each
1039 * clipplane as well. */
1040 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1041 if(ctx_priv->cur_vs_args->clip_enabled)
1043 max_constantsF -= gl_info->limits.clipplanes;
1045 max_constantsF -= count_bits(reg_maps->integer_constants);
1046 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1047 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1048 * for now take this into account when calculating the number of available constants
1050 max_constantsF -= count_bits(reg_maps->boolean_constants);
1051 /* Set by driver quirks in directx.c */
1052 max_constantsF -= gl_info->reserved_glsl_constants;
1054 if (max_constantsF < shader->limits.constant_float)
1056 static unsigned int once;
1058 if (!once++)
1059 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1060 " it may not render correctly.\n");
1061 else
1062 WARN("The hardware does not support enough uniform components to run this shader.\n");
1065 else
1067 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1070 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1071 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1074 /* Always declare the full set of constants, the compiler can remove the
1075 * unused ones because d3d doesn't (yet) support indirect int and bool
1076 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1077 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1078 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1080 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1081 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1083 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1085 if (reg_maps->cb_sizes[i])
1086 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1089 /* Declare texture samplers */
1090 for (i = 0; i < shader->limits.sampler; ++i)
1092 if (reg_maps->sampler_type[i])
1094 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1095 BOOL tex_rect;
1097 switch (reg_maps->sampler_type[i])
1099 case WINED3DSTT_1D:
1100 if (shadow_sampler)
1101 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1102 else
1103 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1104 break;
1105 case WINED3DSTT_2D:
1106 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1107 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1108 if (shadow_sampler)
1110 if (tex_rect)
1111 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1112 else
1113 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1115 else
1117 if (tex_rect)
1118 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1119 else
1120 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1122 break;
1123 case WINED3DSTT_CUBE:
1124 if (shadow_sampler)
1125 FIXME("Unsupported Cube shadow sampler.\n");
1126 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1127 break;
1128 case WINED3DSTT_VOLUME:
1129 if (shadow_sampler)
1130 FIXME("Unsupported 3D shadow sampler.\n");
1131 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1132 break;
1133 default:
1134 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1135 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1136 break;
1141 /* Declare uniforms for NP2 texcoord fixup:
1142 * This is NOT done inside the loop that declares the texture samplers
1143 * since the NP2 fixup code is currently only used for the GeforceFX
1144 * series and when forcing the ARB_npot extension off. Modern cards just
1145 * skip the code anyway, so put it inside a separate loop. */
1146 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1148 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1149 UINT cur = 0;
1151 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1152 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1153 * samplerNP2Fixup stores texture dimensions and is updated through
1154 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1156 for (i = 0; i < shader->limits.sampler; ++i)
1158 if (reg_maps->sampler_type[i])
1160 if (!(ps_args->np2_fixup & (1 << i))) continue;
1162 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1163 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1164 continue;
1167 fixup->idx[i] = cur++;
1171 fixup->num_consts = (cur + 1) >> 1;
1172 fixup->active = ps_args->np2_fixup;
1173 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1176 /* Declare address variables */
1177 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1179 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1182 /* Declare texture coordinate temporaries and initialize them */
1183 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1185 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1188 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1190 /* Declare attributes. */
1191 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1193 if (map & 1)
1194 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1197 shader_addline(buffer, "uniform vec4 posFixup;\n");
1198 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1200 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1202 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1204 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1206 if (version->major >= 3)
1208 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1210 if (use_vs(state))
1211 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1212 else
1213 /* TODO: Write a replacement shader for the fixed function
1214 * vertex pipeline, so this isn't needed. For fixed function
1215 * vertex processing + 3.0 pixel shader we need a separate
1216 * function in the pixel shader that reads the fixed function
1217 * color into the packed input registers. */
1218 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1221 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1223 if (!(map & 1))
1224 continue;
1226 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1228 if (reg_maps->luminanceparams & (1 << i))
1230 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1231 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1232 extra_constants_needed++;
1235 extra_constants_needed++;
1238 if (ps_args->srgb_correction)
1240 shader_addline(buffer, "const vec4 srgb_const0 = ");
1241 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1242 shader_addline(buffer, ";\n");
1243 shader_addline(buffer, "const vec4 srgb_const1 = ");
1244 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1245 shader_addline(buffer, ";\n");
1247 if (reg_maps->vpos || reg_maps->usesdsy)
1249 if (shader->limits.constant_float + extra_constants_needed
1250 + 1 < gl_info->limits.glsl_ps_float_constants)
1252 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1253 extra_constants_needed++;
1255 else
1257 float ycorrection[] =
1259 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1260 context->render_offscreen ? 1.0f : -1.0f,
1261 0.0f,
1262 0.0f,
1265 /* This happens because we do not have proper tracking of the
1266 * constant registers that are actually used, only the max
1267 * limit of the shader version. */
1268 FIXME("Cannot find a free uniform for vpos correction params\n");
1269 shader_addline(buffer, "const vec4 ycorrection = ");
1270 shader_glsl_append_imm_vec4(buffer, ycorrection);
1271 shader_addline(buffer, ";\n");
1273 shader_addline(buffer, "vec4 vpos;\n");
1277 /* Declare output register temporaries */
1278 if (shader->limits.packed_output)
1279 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1281 /* Declare temporary variables */
1282 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1284 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1287 /* Declare loop registers aLx */
1288 if (version->major < 4)
1290 for (i = 0; i < reg_maps->loop_depth; ++i)
1292 shader_addline(buffer, "int aL%u;\n", i);
1293 shader_addline(buffer, "int tmpInt%u;\n", i);
1297 /* Temporary variables for matrix operations */
1298 shader_addline(buffer, "vec4 tmp0;\n");
1299 shader_addline(buffer, "vec4 tmp1;\n");
1301 if (!shader->load_local_constsF)
1303 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1305 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1306 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1307 shader_addline(buffer, ";\n");
1311 /* Start the main program. */
1312 shader_addline(buffer, "void main()\n{\n");
1314 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1315 * add approximately 0.5. This causes off-by-one problems as spotted by
1316 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1317 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1318 * causes precision troubles when we just subtract 0.5.
1320 * To deal with that, just floor() the position. This will eliminate the
1321 * fraction on all cards.
1323 * TODO: Test how this behaves with multisampling.
1325 * An advantage of floor is that it works even if the driver doesn't add
1326 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1327 * to return in gl_FragCoord, even though coordinates specify the pixel
1328 * centers instead of the pixel corners. This code will behave correctly
1329 * on drivers that returns integer values. */
1330 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1331 shader_addline(buffer,
1332 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1335 /*****************************************************************************
1336 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1338 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1339 ****************************************************************************/
1341 /* Prototypes */
1342 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1343 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1345 /** Used for opcode modifiers - They multiply the result by the specified amount */
1346 static const char * const shift_glsl_tab[] = {
1347 "", /* 0 (none) */
1348 "2.0 * ", /* 1 (x2) */
1349 "4.0 * ", /* 2 (x4) */
1350 "8.0 * ", /* 3 (x8) */
1351 "16.0 * ", /* 4 (x16) */
1352 "32.0 * ", /* 5 (x32) */
1353 "", /* 6 (x64) */
1354 "", /* 7 (x128) */
1355 "", /* 8 (d256) */
1356 "", /* 9 (d128) */
1357 "", /* 10 (d64) */
1358 "", /* 11 (d32) */
1359 "0.0625 * ", /* 12 (d16) */
1360 "0.125 * ", /* 13 (d8) */
1361 "0.25 * ", /* 14 (d4) */
1362 "0.5 * " /* 15 (d2) */
1365 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1366 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1367 const char *in_reg, const char *in_regswizzle, char *out_str)
1369 out_str[0] = 0;
1371 switch (src_modifier)
1373 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1374 case WINED3DSPSM_DW:
1375 case WINED3DSPSM_NONE:
1376 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1377 break;
1378 case WINED3DSPSM_NEG:
1379 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1380 break;
1381 case WINED3DSPSM_NOT:
1382 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1383 break;
1384 case WINED3DSPSM_BIAS:
1385 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1386 break;
1387 case WINED3DSPSM_BIASNEG:
1388 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1389 break;
1390 case WINED3DSPSM_SIGN:
1391 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1392 break;
1393 case WINED3DSPSM_SIGNNEG:
1394 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1395 break;
1396 case WINED3DSPSM_COMP:
1397 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1398 break;
1399 case WINED3DSPSM_X2:
1400 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1401 break;
1402 case WINED3DSPSM_X2NEG:
1403 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1404 break;
1405 case WINED3DSPSM_ABS:
1406 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1407 break;
1408 case WINED3DSPSM_ABSNEG:
1409 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1410 break;
1411 default:
1412 FIXME("Unhandled modifier %u\n", src_modifier);
1413 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1417 /** Writes the GLSL variable name that corresponds to the register that the
1418 * DX opcode parameter is trying to access */
1419 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1420 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1422 /* oPos, oFog and oPts in D3D */
1423 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1425 const struct wined3d_shader *shader = ins->ctx->shader;
1426 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1427 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1428 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1429 const char *prefix = shader_glsl_get_prefix(version->type);
1430 struct glsl_src_param rel_param0, rel_param1;
1431 char imm_str[4][17];
1433 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1434 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1435 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1436 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1437 *is_color = FALSE;
1439 switch (reg->type)
1441 case WINED3DSPR_TEMP:
1442 sprintf(register_name, "R%u", reg->idx[0].offset);
1443 break;
1445 case WINED3DSPR_INPUT:
1446 /* vertex shaders */
1447 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1449 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1450 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1451 *is_color = TRUE;
1452 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1453 break;
1456 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1458 if (reg->idx[0].rel_addr)
1460 if (reg->idx[1].rel_addr)
1461 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1462 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1463 else
1464 sprintf(register_name, "gs_in[%s + %u][%u]",
1465 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1467 else if (reg->idx[1].rel_addr)
1468 sprintf(register_name, "gs_in[%u][%s + %u]",
1469 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1470 else
1471 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1472 break;
1475 /* pixel shaders >= 3.0 */
1476 if (version->major >= 3)
1478 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1479 unsigned int in_count = vec4_varyings(version->major, gl_info);
1481 if (reg->idx[0].rel_addr)
1483 /* Removing a + 0 would be an obvious optimization, but
1484 * OS X doesn't see the NOP operation there. */
1485 if (idx)
1487 if (shader->u.ps.declared_in_count > in_count)
1489 sprintf(register_name,
1490 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1491 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1492 prefix, rel_param0.param_str, idx);
1494 else
1496 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1499 else
1501 if (shader->u.ps.declared_in_count > in_count)
1503 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1504 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1505 prefix, rel_param0.param_str);
1507 else
1509 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1513 else
1515 if (idx == in_count) sprintf(register_name, "gl_Color");
1516 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1517 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1520 else
1522 if (!reg->idx[0].offset)
1523 strcpy(register_name, "gl_Color");
1524 else
1525 strcpy(register_name, "gl_SecondaryColor");
1526 break;
1528 break;
1530 case WINED3DSPR_CONST:
1532 /* Relative addressing */
1533 if (reg->idx[0].rel_addr)
1535 if (reg->idx[0].offset)
1536 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1537 else
1538 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1540 else
1542 if (shader_constant_is_local(shader, reg->idx[0].offset))
1543 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1544 else
1545 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1548 break;
1550 case WINED3DSPR_CONSTINT:
1551 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1552 break;
1554 case WINED3DSPR_CONSTBOOL:
1555 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1556 break;
1558 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1559 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1560 sprintf(register_name, "T%u", reg->idx[0].offset);
1561 else
1562 sprintf(register_name, "A%u", reg->idx[0].offset);
1563 break;
1565 case WINED3DSPR_LOOP:
1566 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1567 break;
1569 case WINED3DSPR_SAMPLER:
1570 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1571 break;
1573 case WINED3DSPR_COLOROUT:
1574 if (reg->idx[0].offset >= gl_info->limits.buffers)
1575 WARN("Write to render target %u, only %d supported.\n",
1576 reg->idx[0].offset, gl_info->limits.buffers);
1578 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1579 break;
1581 case WINED3DSPR_RASTOUT:
1582 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1583 break;
1585 case WINED3DSPR_DEPTHOUT:
1586 sprintf(register_name, "gl_FragDepth");
1587 break;
1589 case WINED3DSPR_ATTROUT:
1590 if (!reg->idx[0].offset)
1591 sprintf(register_name, "%s_out[8]", prefix);
1592 else
1593 sprintf(register_name, "%s_out[9]", prefix);
1594 break;
1596 case WINED3DSPR_TEXCRDOUT:
1597 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1598 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1599 break;
1601 case WINED3DSPR_MISCTYPE:
1602 if (!reg->idx[0].offset)
1604 /* vPos */
1605 sprintf(register_name, "vpos");
1607 else if (reg->idx[0].offset == 1)
1609 /* Note that gl_FrontFacing is a bool, while vFace is
1610 * a float for which the sign determines front/back */
1611 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1613 else
1615 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1616 sprintf(register_name, "unrecognized_register");
1618 break;
1620 case WINED3DSPR_IMMCONST:
1621 switch (reg->immconst_type)
1623 case WINED3D_IMMCONST_SCALAR:
1624 switch (reg->data_type)
1626 case WINED3D_DATA_FLOAT:
1627 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1628 break;
1629 case WINED3D_DATA_INT:
1630 sprintf(register_name, "%#x", reg->immconst_data[0]);
1631 break;
1632 case WINED3D_DATA_RESOURCE:
1633 case WINED3D_DATA_SAMPLER:
1634 case WINED3D_DATA_UINT:
1635 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1636 break;
1637 default:
1638 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1639 break;
1641 break;
1643 case WINED3D_IMMCONST_VEC4:
1644 switch (reg->data_type)
1646 case WINED3D_DATA_FLOAT:
1647 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1648 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1649 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1650 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1651 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1652 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1653 break;
1654 case WINED3D_DATA_INT:
1655 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1656 reg->immconst_data[0], reg->immconst_data[1],
1657 reg->immconst_data[2], reg->immconst_data[3]);
1658 break;
1659 case WINED3D_DATA_RESOURCE:
1660 case WINED3D_DATA_SAMPLER:
1661 case WINED3D_DATA_UINT:
1662 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1663 reg->immconst_data[0], reg->immconst_data[1],
1664 reg->immconst_data[2], reg->immconst_data[3]);
1665 break;
1666 default:
1667 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1668 break;
1670 break;
1672 default:
1673 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1674 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1676 break;
1678 case WINED3DSPR_CONSTBUFFER:
1679 if (reg->idx[1].rel_addr)
1680 sprintf(register_name, "%s_cb%u[%s + %u]",
1681 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1682 else
1683 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1684 break;
1686 case WINED3DSPR_PRIMID:
1687 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1688 break;
1690 default:
1691 FIXME("Unhandled register type %#x.\n", reg->type);
1692 sprintf(register_name, "unrecognized_register");
1693 break;
1697 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1699 *str++ = '.';
1700 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1701 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1702 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1703 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1704 *str = '\0';
1707 /* Get the GLSL write mask for the destination register */
1708 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1710 DWORD mask = param->write_mask;
1712 if (shader_is_scalar(&param->reg))
1714 mask = WINED3DSP_WRITEMASK_0;
1715 *write_mask = '\0';
1717 else
1719 shader_glsl_write_mask_to_str(mask, write_mask);
1722 return mask;
1725 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1726 unsigned int size = 0;
1728 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1729 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1730 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1731 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1733 return size;
1736 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1738 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1739 * but addressed as "rgba". To fix this we need to swap the register's x
1740 * and z components. */
1741 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1743 *str++ = '.';
1744 /* swizzle bits fields: wwzzyyxx */
1745 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1746 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1747 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1748 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1749 *str = '\0';
1752 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1753 BOOL fixup, DWORD mask, char *swizzle_str)
1755 if (shader_is_scalar(&param->reg))
1756 *swizzle_str = '\0';
1757 else
1758 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1761 /* From a given parameter token, generate the corresponding GLSL string.
1762 * Also, return the actual register name and swizzle in case the
1763 * caller needs this information as well. */
1764 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1765 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1767 BOOL is_color = FALSE;
1768 char swizzle_str[6];
1770 glsl_src->reg_name[0] = '\0';
1771 glsl_src->param_str[0] = '\0';
1772 swizzle_str[0] = '\0';
1774 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1775 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1777 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1779 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1781 else
1783 char param_str[200];
1785 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1787 switch (wined3d_src->reg.data_type)
1789 case WINED3D_DATA_FLOAT:
1790 sprintf(glsl_src->param_str, "%s", param_str);
1791 break;
1792 case WINED3D_DATA_INT:
1793 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1794 break;
1795 case WINED3D_DATA_RESOURCE:
1796 case WINED3D_DATA_SAMPLER:
1797 case WINED3D_DATA_UINT:
1798 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1799 break;
1800 default:
1801 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1802 sprintf(glsl_src->param_str, "%s", param_str);
1803 break;
1808 /* From a given parameter token, generate the corresponding GLSL string.
1809 * Also, return the actual register name and swizzle in case the
1810 * caller needs this information as well. */
1811 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1812 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1814 BOOL is_color = FALSE;
1816 glsl_dst->mask_str[0] = '\0';
1817 glsl_dst->reg_name[0] = '\0';
1819 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1820 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1823 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1824 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1825 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1827 struct glsl_dst_param glsl_dst;
1828 DWORD mask;
1830 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1832 switch (dst->reg.data_type)
1834 case WINED3D_DATA_FLOAT:
1835 shader_addline(buffer, "%s%s = %s(",
1836 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1837 break;
1838 case WINED3D_DATA_INT:
1839 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1840 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1841 break;
1842 case WINED3D_DATA_RESOURCE:
1843 case WINED3D_DATA_SAMPLER:
1844 case WINED3D_DATA_UINT:
1845 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1846 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1847 break;
1848 default:
1849 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1850 shader_addline(buffer, "%s%s = %s(",
1851 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1852 break;
1856 return mask;
1859 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1860 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1862 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1865 /** Process GLSL instruction modifiers */
1866 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1868 struct glsl_dst_param dst_param;
1869 DWORD modifiers;
1871 if (!ins->dst_count) return;
1873 modifiers = ins->dst[0].modifiers;
1874 if (!modifiers) return;
1876 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1878 if (modifiers & WINED3DSPDM_SATURATE)
1880 /* _SAT means to clamp the value of the register to between 0 and 1 */
1881 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1882 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1885 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1887 FIXME("_centroid modifier not handled\n");
1890 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1892 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1896 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1898 switch (op)
1900 case WINED3D_SHADER_REL_OP_GT: return ">";
1901 case WINED3D_SHADER_REL_OP_EQ: return "==";
1902 case WINED3D_SHADER_REL_OP_GE: return ">=";
1903 case WINED3D_SHADER_REL_OP_LT: return "<";
1904 case WINED3D_SHADER_REL_OP_NE: return "!=";
1905 case WINED3D_SHADER_REL_OP_LE: return "<=";
1906 default:
1907 FIXME("Unrecognized operator %#x.\n", op);
1908 return "(\?\?)";
1912 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1913 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1915 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1916 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1917 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1918 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1919 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1920 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1921 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1922 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1924 /* Note that there's no such thing as a projected cube texture. */
1925 switch(sampler_type) {
1926 case WINED3DSTT_1D:
1927 if (shadow)
1929 if (lod)
1931 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1933 else if (grad)
1935 if (gl_info->supported[EXT_GPU_SHADER4])
1936 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1937 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1938 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1939 else
1941 FIXME("Unsupported 1D shadow grad function.\n");
1942 sample_function->name = "unsupported1DGrad";
1945 else
1947 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1949 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1951 else
1953 if (lod)
1955 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1957 else if (grad)
1959 if (gl_info->supported[EXT_GPU_SHADER4])
1960 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1961 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1962 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1963 else
1965 FIXME("Unsupported 1D grad function.\n");
1966 sample_function->name = "unsupported1DGrad";
1969 else
1971 sample_function->name = projected ? "texture1DProj" : "texture1D";
1973 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1975 break;
1977 case WINED3DSTT_2D:
1978 if (shadow)
1980 if (texrect)
1982 if (lod)
1984 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1986 else if (grad)
1988 if (gl_info->supported[EXT_GPU_SHADER4])
1989 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1990 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1991 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1992 else
1994 FIXME("Unsupported RECT shadow grad function.\n");
1995 sample_function->name = "unsupported2DRectGrad";
1998 else
2000 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2003 else
2005 if (lod)
2007 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2009 else if (grad)
2011 if (gl_info->supported[EXT_GPU_SHADER4])
2012 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2013 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2014 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2015 else
2017 FIXME("Unsupported 2D shadow grad function.\n");
2018 sample_function->name = "unsupported2DGrad";
2021 else
2023 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2026 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2028 else
2030 if (texrect)
2032 if (lod)
2034 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2036 else if (grad)
2038 if (gl_info->supported[EXT_GPU_SHADER4])
2039 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2040 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2041 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2042 else
2044 FIXME("Unsupported RECT grad function.\n");
2045 sample_function->name = "unsupported2DRectGrad";
2048 else
2050 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2053 else
2055 if (lod)
2057 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2059 else if (grad)
2061 if (gl_info->supported[EXT_GPU_SHADER4])
2062 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2063 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2064 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2065 else
2067 FIXME("Unsupported 2D grad function.\n");
2068 sample_function->name = "unsupported2DGrad";
2071 else
2073 sample_function->name = projected ? "texture2DProj" : "texture2D";
2076 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2078 break;
2080 case WINED3DSTT_CUBE:
2081 if (shadow)
2083 FIXME("Unsupported Cube shadow function.\n");
2084 sample_function->name = "unsupportedCubeShadow";
2085 sample_function->coord_mask = 0;
2087 else
2089 if (lod)
2091 sample_function->name = "textureCubeLod";
2093 else if (grad)
2095 if (gl_info->supported[EXT_GPU_SHADER4])
2096 sample_function->name = "textureCubeGrad";
2097 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2098 sample_function->name = "textureCubeGradARB";
2099 else
2101 FIXME("Unsupported Cube grad function.\n");
2102 sample_function->name = "unsupportedCubeGrad";
2105 else
2107 sample_function->name = "textureCube";
2109 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2111 break;
2113 case WINED3DSTT_VOLUME:
2114 if (shadow)
2116 FIXME("Unsupported 3D shadow function.\n");
2117 sample_function->name = "unsupported3DShadow";
2118 sample_function->coord_mask = 0;
2120 else
2122 if (lod)
2124 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2126 else if (grad)
2128 if (gl_info->supported[EXT_GPU_SHADER4])
2129 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2130 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2131 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2132 else
2134 FIXME("Unsupported 3D grad function.\n");
2135 sample_function->name = "unsupported3DGrad";
2138 else
2140 sample_function->name = projected ? "texture3DProj" : "texture3D";
2142 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2144 break;
2146 default:
2147 sample_function->name = "";
2148 sample_function->coord_mask = 0;
2149 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2150 break;
2154 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2155 BOOL sign_fixup, enum fixup_channel_source channel_source)
2157 switch(channel_source)
2159 case CHANNEL_SOURCE_ZERO:
2160 strcat(arguments, "0.0");
2161 break;
2163 case CHANNEL_SOURCE_ONE:
2164 strcat(arguments, "1.0");
2165 break;
2167 case CHANNEL_SOURCE_X:
2168 strcat(arguments, reg_name);
2169 strcat(arguments, ".x");
2170 break;
2172 case CHANNEL_SOURCE_Y:
2173 strcat(arguments, reg_name);
2174 strcat(arguments, ".y");
2175 break;
2177 case CHANNEL_SOURCE_Z:
2178 strcat(arguments, reg_name);
2179 strcat(arguments, ".z");
2180 break;
2182 case CHANNEL_SOURCE_W:
2183 strcat(arguments, reg_name);
2184 strcat(arguments, ".w");
2185 break;
2187 default:
2188 FIXME("Unhandled channel source %#x\n", channel_source);
2189 strcat(arguments, "undefined");
2190 break;
2193 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2196 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2197 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2199 unsigned int mask_size, remaining;
2200 DWORD fixup_mask = 0;
2201 char arguments[256];
2202 char mask_str[6];
2204 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2205 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2206 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2207 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2208 if (!(mask &= fixup_mask))
2209 return;
2211 if (is_complex_fixup(fixup))
2213 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2214 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2215 return;
2218 shader_glsl_write_mask_to_str(mask, mask_str);
2219 mask_size = shader_glsl_get_write_mask_size(mask);
2221 arguments[0] = '\0';
2222 remaining = mask_size;
2223 if (mask & WINED3DSP_WRITEMASK_0)
2225 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2226 if (--remaining) strcat(arguments, ", ");
2228 if (mask & WINED3DSP_WRITEMASK_1)
2230 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2231 if (--remaining) strcat(arguments, ", ");
2233 if (mask & WINED3DSP_WRITEMASK_2)
2235 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2236 if (--remaining) strcat(arguments, ", ");
2238 if (mask & WINED3DSP_WRITEMASK_3)
2240 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2241 if (--remaining) strcat(arguments, ", ");
2244 if (mask_size > 1)
2245 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2246 else
2247 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2250 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2252 char reg_name[256];
2253 BOOL is_color;
2255 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2256 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2259 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2260 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2261 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2263 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2264 char dst_swizzle[6];
2265 struct color_fixup_desc fixup;
2266 BOOL np2_fixup = FALSE;
2267 va_list args;
2269 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2271 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2273 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2274 fixup = priv->cur_ps_args->color_fixup[sampler];
2276 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2277 if(bias) {
2278 FIXME("Biased sampling from NP2 textures is unsupported\n");
2279 } else {
2280 np2_fixup = TRUE;
2284 else
2286 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2289 shader_glsl_append_dst(ins->ctx->buffer, ins);
2291 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2292 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2294 va_start(args, coord_reg_fmt);
2295 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2296 va_end(args);
2298 if(bias) {
2299 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2300 } else {
2301 if (np2_fixup) {
2302 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2303 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2305 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2306 (idx % 2) ? "zw" : "xy", dst_swizzle);
2307 } else if(dx && dy) {
2308 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2309 } else {
2310 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2314 if(!is_identity_fixup(fixup)) {
2315 shader_glsl_color_correction(ins, fixup);
2319 /*****************************************************************************
2320 * Begin processing individual instruction opcodes
2321 ****************************************************************************/
2323 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2325 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2326 struct glsl_src_param src0_param;
2327 struct glsl_src_param src1_param;
2328 DWORD write_mask;
2329 const char *op;
2331 /* Determine the GLSL operator to use based on the opcode */
2332 switch (ins->handler_idx)
2334 case WINED3DSIH_ADD: op = "+"; break;
2335 case WINED3DSIH_AND: op = "&"; break;
2336 case WINED3DSIH_DIV: op = "/"; break;
2337 case WINED3DSIH_IADD: op = "+"; break;
2338 case WINED3DSIH_ISHL: op = "<<"; break;
2339 case WINED3DSIH_MUL: op = "*"; break;
2340 case WINED3DSIH_SUB: op = "-"; break;
2341 case WINED3DSIH_USHR: op = ">>"; break;
2342 case WINED3DSIH_XOR: op = "^"; break;
2343 default:
2344 op = "<unhandled operator>";
2345 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2346 break;
2349 write_mask = shader_glsl_append_dst(buffer, ins);
2350 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2351 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2352 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2355 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2357 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2358 struct glsl_src_param src0_param;
2359 struct glsl_src_param src1_param;
2360 unsigned int mask_size;
2361 DWORD write_mask;
2362 const char *op;
2364 write_mask = shader_glsl_append_dst(buffer, ins);
2365 mask_size = shader_glsl_get_write_mask_size(write_mask);
2366 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2367 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2369 if (mask_size > 1)
2371 switch (ins->handler_idx)
2373 case WINED3DSIH_EQ: op = "equal"; break;
2374 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2375 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2376 case WINED3DSIH_LT: op = "lessThan"; break;
2377 default:
2378 op = "<unhandled operator>";
2379 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2380 break;
2383 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2384 mask_size, op, src0_param.param_str, src1_param.param_str);
2386 else
2388 switch (ins->handler_idx)
2390 case WINED3DSIH_EQ: op = "=="; break;
2391 case WINED3DSIH_GE: op = ">="; break;
2392 case WINED3DSIH_IGE: op = ">="; break;
2393 case WINED3DSIH_LT: op = "<"; break;
2394 default:
2395 op = "<unhandled operator>";
2396 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2397 break;
2400 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2401 src0_param.param_str, op, src1_param.param_str);
2405 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2407 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2408 struct glsl_src_param src0_param;
2409 struct glsl_src_param src1_param;
2410 DWORD write_mask;
2412 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2413 * not, we can emulate it. */
2414 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2415 FIXME("64-bit integer multiplies not implemented.\n");
2417 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2419 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2420 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2421 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2423 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2424 src0_param.param_str, src1_param.param_str);
2428 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2430 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2431 struct glsl_src_param src0_param, src1_param;
2432 DWORD write_mask;
2434 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2437 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2439 char dst_mask[6];
2441 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2442 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2443 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2444 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2445 dst_mask, src0_param.param_str, src1_param.param_str);
2447 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2448 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2449 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2450 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2452 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2453 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2455 else
2457 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2458 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2459 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2460 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2463 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2465 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2466 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2467 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2468 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2472 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2473 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2475 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2476 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2477 struct glsl_src_param src0_param;
2478 DWORD write_mask;
2480 write_mask = shader_glsl_append_dst(buffer, ins);
2481 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2483 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2484 * shader versions WINED3DSIO_MOVA is used for this. */
2485 if (ins->ctx->reg_maps->shader_version.major == 1
2486 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2487 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2489 /* This is a simple floor() */
2490 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2491 if (mask_size > 1) {
2492 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2493 } else {
2494 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2497 else if(ins->handler_idx == WINED3DSIH_MOVA)
2499 /* We need to *round* to the nearest int here. */
2500 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2502 if (gl_info->supported[EXT_GPU_SHADER4])
2504 if (mask_size > 1)
2505 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2506 else
2507 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2509 else
2511 if (mask_size > 1)
2512 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2513 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2514 else
2515 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2516 src0_param.param_str, src0_param.param_str);
2519 else
2521 shader_addline(buffer, "%s);\n", src0_param.param_str);
2525 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2526 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2528 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2529 struct glsl_src_param src0_param;
2530 struct glsl_src_param src1_param;
2531 DWORD dst_write_mask, src_write_mask;
2532 unsigned int dst_size = 0;
2534 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2535 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2537 /* dp3 works on vec3, dp4 on vec4 */
2538 if (ins->handler_idx == WINED3DSIH_DP4)
2540 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2541 } else {
2542 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2545 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2546 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2548 if (dst_size > 1) {
2549 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2550 } else {
2551 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2555 /* Note that this instruction has some restrictions. The destination write mask
2556 * can't contain the w component, and the source swizzles have to be .xyzw */
2557 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2559 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2560 struct glsl_src_param src0_param;
2561 struct glsl_src_param src1_param;
2562 char dst_mask[6];
2564 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2565 shader_glsl_append_dst(ins->ctx->buffer, ins);
2566 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2567 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2568 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2571 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2573 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2576 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2577 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2578 * GLSL uses the value as-is. */
2579 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2581 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2582 struct glsl_src_param src0_param;
2583 struct glsl_src_param src1_param;
2584 DWORD dst_write_mask;
2585 unsigned int dst_size;
2587 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2588 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2590 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2591 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2593 if (dst_size > 1)
2595 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2596 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2598 else
2600 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2601 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2605 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2606 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2608 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2609 struct glsl_src_param src_param;
2610 const char *instruction;
2611 DWORD write_mask;
2612 unsigned i;
2614 /* Determine the GLSL function to use based on the opcode */
2615 /* TODO: Possibly make this a table for faster lookups */
2616 switch (ins->handler_idx)
2618 case WINED3DSIH_MIN: instruction = "min"; break;
2619 case WINED3DSIH_MAX: instruction = "max"; break;
2620 case WINED3DSIH_ABS: instruction = "abs"; break;
2621 case WINED3DSIH_FRC: instruction = "fract"; break;
2622 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2623 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2624 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2625 default: instruction = "";
2626 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2627 break;
2630 write_mask = shader_glsl_append_dst(buffer, ins);
2632 shader_addline(buffer, "%s(", instruction);
2634 if (ins->src_count)
2636 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2637 shader_addline(buffer, "%s", src_param.param_str);
2638 for (i = 1; i < ins->src_count; ++i)
2640 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2641 shader_addline(buffer, ", %s", src_param.param_str);
2645 shader_addline(buffer, "));\n");
2648 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2650 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2652 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2653 struct glsl_src_param src_param;
2654 unsigned int mask_size;
2655 DWORD write_mask;
2656 char dst_mask[6];
2658 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2659 mask_size = shader_glsl_get_write_mask_size(write_mask);
2660 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2662 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2663 src_param.param_str, src_param.param_str);
2664 shader_glsl_append_dst(buffer, ins);
2666 if (mask_size > 1)
2668 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2669 mask_size, src_param.param_str);
2671 else
2673 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2674 src_param.param_str);
2678 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2680 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2681 struct glsl_src_param src0_param;
2682 const char *prefix, *suffix;
2683 unsigned int dst_size;
2684 DWORD dst_write_mask;
2686 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2687 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2689 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2691 switch (ins->handler_idx)
2693 case WINED3DSIH_EXP:
2694 case WINED3DSIH_EXPP:
2695 prefix = "exp2(";
2696 suffix = ")";
2697 break;
2699 case WINED3DSIH_LOG:
2700 case WINED3DSIH_LOGP:
2701 prefix = "log2(abs(";
2702 suffix = "))";
2703 break;
2705 case WINED3DSIH_RCP:
2706 prefix = "1.0 / ";
2707 suffix = "";
2708 break;
2710 case WINED3DSIH_RSQ:
2711 prefix = "inversesqrt(abs(";
2712 suffix = "))";
2713 break;
2715 default:
2716 prefix = "";
2717 suffix = "";
2718 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2719 break;
2722 if (dst_size > 1)
2723 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2724 else
2725 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2728 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2729 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2730 * dst.x = 2^(floor(src))
2731 * dst.y = src - floor(src)
2732 * dst.z = 2^src (partial precision is allowed, but optional)
2733 * dst.w = 1.0;
2734 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2735 * dst = 2^src; (partial precision is allowed, but optional)
2737 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2739 if (ins->ctx->reg_maps->shader_version.major < 2)
2741 struct glsl_src_param src_param;
2742 char dst_mask[6];
2744 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2746 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2747 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2748 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2749 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2751 shader_glsl_append_dst(ins->ctx->buffer, ins);
2752 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2753 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2754 return;
2757 shader_glsl_scalar_op(ins);
2760 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2762 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2763 struct glsl_src_param src_param;
2764 unsigned int mask_size;
2765 DWORD write_mask;
2767 write_mask = shader_glsl_append_dst(buffer, ins);
2768 mask_size = shader_glsl_get_write_mask_size(write_mask);
2769 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2771 if (mask_size > 1)
2772 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2773 else
2774 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2777 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2779 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2780 struct glsl_src_param src_param;
2781 unsigned int mask_size;
2782 DWORD write_mask;
2784 write_mask = shader_glsl_append_dst(buffer, ins);
2785 mask_size = shader_glsl_get_write_mask_size(write_mask);
2786 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2788 if (mask_size > 1)
2789 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2790 else
2791 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2794 /** Process signed comparison opcodes in GLSL. */
2795 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2797 struct glsl_src_param src0_param;
2798 struct glsl_src_param src1_param;
2799 DWORD write_mask;
2800 unsigned int mask_size;
2802 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2803 mask_size = shader_glsl_get_write_mask_size(write_mask);
2804 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2805 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2807 if (mask_size > 1) {
2808 const char *compare;
2810 switch(ins->handler_idx)
2812 case WINED3DSIH_SLT: compare = "lessThan"; break;
2813 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2814 default: compare = "";
2815 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2818 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2819 src0_param.param_str, src1_param.param_str);
2820 } else {
2821 switch(ins->handler_idx)
2823 case WINED3DSIH_SLT:
2824 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2825 * to return 0.0 but step returns 1.0 because step is not < x
2826 * An alternative is a bvec compare padded with an unused second component.
2827 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2828 * issue. Playing with not() is not possible either because not() does not accept
2829 * a scalar.
2831 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2832 src0_param.param_str, src1_param.param_str);
2833 break;
2834 case WINED3DSIH_SGE:
2835 /* Here we can use the step() function and safe a conditional */
2836 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2837 break;
2838 default:
2839 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2845 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2847 const char *condition_prefix, *condition_suffix;
2848 struct wined3d_shader_dst_param dst;
2849 struct glsl_src_param src0_param;
2850 struct glsl_src_param src1_param;
2851 struct glsl_src_param src2_param;
2852 BOOL temp_destination = FALSE;
2853 DWORD cmp_channel = 0;
2854 unsigned int i, j;
2855 char mask_char[6];
2856 DWORD write_mask;
2858 switch (ins->handler_idx)
2860 case WINED3DSIH_CMP:
2861 condition_prefix = "";
2862 condition_suffix = " >= 0.0";
2863 break;
2865 case WINED3DSIH_CND:
2866 condition_prefix = "";
2867 condition_suffix = " > 0.5";
2868 break;
2870 case WINED3DSIH_MOVC:
2871 condition_prefix = "bool(";
2872 condition_suffix = ")";
2873 break;
2875 default:
2876 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2877 condition_prefix = "<unhandled prefix>";
2878 condition_suffix = "<unhandled suffix>";
2879 break;
2882 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2884 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2885 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2886 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2887 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2889 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2890 condition_prefix, src0_param.param_str, condition_suffix,
2891 src1_param.param_str, src2_param.param_str);
2892 return;
2895 dst = ins->dst[0];
2897 /* Splitting the instruction up in multiple lines imposes a problem:
2898 * The first lines may overwrite source parameters of the following lines.
2899 * Deal with that by using a temporary destination register if needed. */
2900 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2901 && ins->src[0].reg.type == dst.reg.type)
2902 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2903 && ins->src[1].reg.type == dst.reg.type)
2904 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2905 && ins->src[2].reg.type == dst.reg.type))
2906 temp_destination = TRUE;
2908 /* Cycle through all source0 channels. */
2909 for (i = 0; i < 4; ++i)
2911 write_mask = 0;
2912 /* Find the destination channels which use the current source0 channel. */
2913 for (j = 0; j < 4; ++j)
2915 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2917 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2918 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2921 dst.write_mask = ins->dst[0].write_mask & write_mask;
2923 if (temp_destination)
2925 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2926 continue;
2927 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2929 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2930 continue;
2932 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2933 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2934 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2936 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2937 condition_prefix, src0_param.param_str, condition_suffix,
2938 src1_param.param_str, src2_param.param_str);
2941 if (temp_destination)
2943 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2944 shader_glsl_append_dst(ins->ctx->buffer, ins);
2945 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2949 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2950 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2951 * the compare is done per component of src0. */
2952 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2954 struct glsl_src_param src0_param;
2955 struct glsl_src_param src1_param;
2956 struct glsl_src_param src2_param;
2957 DWORD write_mask;
2958 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2959 ins->ctx->reg_maps->shader_version.minor);
2961 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2963 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2964 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2965 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2966 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2968 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2969 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2970 else
2971 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2972 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2973 return;
2976 shader_glsl_conditional_move(ins);
2979 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2980 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2982 struct glsl_src_param src0_param;
2983 struct glsl_src_param src1_param;
2984 struct glsl_src_param src2_param;
2985 DWORD write_mask;
2987 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2988 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2989 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2990 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2991 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2992 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2995 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2996 Vertex shaders to GLSL codes */
2997 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2999 int i;
3000 int nComponents = 0;
3001 struct wined3d_shader_dst_param tmp_dst = {{0}};
3002 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3003 struct wined3d_shader_instruction tmp_ins;
3005 memset(&tmp_ins, 0, sizeof(tmp_ins));
3007 /* Set constants for the temporary argument */
3008 tmp_ins.ctx = ins->ctx;
3009 tmp_ins.dst_count = 1;
3010 tmp_ins.dst = &tmp_dst;
3011 tmp_ins.src_count = 2;
3012 tmp_ins.src = tmp_src;
3014 switch(ins->handler_idx)
3016 case WINED3DSIH_M4x4:
3017 nComponents = 4;
3018 tmp_ins.handler_idx = WINED3DSIH_DP4;
3019 break;
3020 case WINED3DSIH_M4x3:
3021 nComponents = 3;
3022 tmp_ins.handler_idx = WINED3DSIH_DP4;
3023 break;
3024 case WINED3DSIH_M3x4:
3025 nComponents = 4;
3026 tmp_ins.handler_idx = WINED3DSIH_DP3;
3027 break;
3028 case WINED3DSIH_M3x3:
3029 nComponents = 3;
3030 tmp_ins.handler_idx = WINED3DSIH_DP3;
3031 break;
3032 case WINED3DSIH_M3x2:
3033 nComponents = 2;
3034 tmp_ins.handler_idx = WINED3DSIH_DP3;
3035 break;
3036 default:
3037 break;
3040 tmp_dst = ins->dst[0];
3041 tmp_src[0] = ins->src[0];
3042 tmp_src[1] = ins->src[1];
3043 for (i = 0; i < nComponents; ++i)
3045 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3046 shader_glsl_dot(&tmp_ins);
3047 ++tmp_src[1].reg.idx[0].offset;
3052 The LRP instruction performs a component-wise linear interpolation
3053 between the second and third operands using the first operand as the
3054 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3055 This is equivalent to mix(src2, src1, src0);
3057 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3059 struct glsl_src_param src0_param;
3060 struct glsl_src_param src1_param;
3061 struct glsl_src_param src2_param;
3062 DWORD write_mask;
3064 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3066 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3067 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3068 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3070 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3071 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3074 /** Process the WINED3DSIO_LIT instruction in GLSL:
3075 * dst.x = dst.w = 1.0
3076 * dst.y = (src0.x > 0) ? src0.x
3077 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3078 * where src.w is clamped at +- 128
3080 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3082 struct glsl_src_param src0_param;
3083 struct glsl_src_param src1_param;
3084 struct glsl_src_param src3_param;
3085 char dst_mask[6];
3087 shader_glsl_append_dst(ins->ctx->buffer, ins);
3088 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3090 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3091 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3094 /* The sdk specifies the instruction like this
3095 * dst.x = 1.0;
3096 * if(src.x > 0.0) dst.y = src.x
3097 * else dst.y = 0.0.
3098 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3099 * else dst.z = 0.0;
3100 * dst.w = 1.0;
3101 * (where power = src.w clamped between -128 and 128)
3103 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3104 * dst.x = 1.0 ... No further explanation needed
3105 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3106 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3107 * dst.w = 1.0. ... Nothing fancy.
3109 * So we still have one conditional in there. So do this:
3110 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3112 * 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),
3113 * 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.
3114 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3116 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3117 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3118 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3120 shader_addline(ins->ctx->buffer,
3121 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3122 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3123 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3124 src0_param.param_str, src3_param.param_str, dst_mask);
3127 /** Process the WINED3DSIO_DST instruction in GLSL:
3128 * dst.x = 1.0
3129 * dst.y = src0.x * src0.y
3130 * dst.z = src0.z
3131 * dst.w = src1.w
3133 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3135 struct glsl_src_param src0y_param;
3136 struct glsl_src_param src0z_param;
3137 struct glsl_src_param src1y_param;
3138 struct glsl_src_param src1w_param;
3139 char dst_mask[6];
3141 shader_glsl_append_dst(ins->ctx->buffer, ins);
3142 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3144 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3145 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3146 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3147 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3149 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3150 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3153 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3154 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3155 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3157 * dst.x = cos(src0.?)
3158 * dst.y = sin(src0.?)
3159 * dst.z = dst.z
3160 * dst.w = dst.w
3162 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3164 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3165 struct glsl_src_param src0_param;
3166 DWORD write_mask;
3168 if (ins->ctx->reg_maps->shader_version.major < 4)
3170 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3172 write_mask = shader_glsl_append_dst(buffer, ins);
3173 switch (write_mask)
3175 case WINED3DSP_WRITEMASK_0:
3176 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3177 break;
3179 case WINED3DSP_WRITEMASK_1:
3180 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3181 break;
3183 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3184 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3185 src0_param.param_str, src0_param.param_str);
3186 break;
3188 default:
3189 ERR("Write mask should be .x, .y or .xy\n");
3190 break;
3193 return;
3196 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3199 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3201 char dst_mask[6];
3203 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3204 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3205 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3207 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3208 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3209 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3211 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3212 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3214 else
3216 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3217 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3218 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3221 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3223 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3224 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3225 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3229 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3230 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3231 * generate invalid code
3233 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3235 struct glsl_src_param src0_param;
3236 DWORD write_mask;
3238 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3239 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3241 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3244 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3245 * Start a for() loop where src1.y is the initial value of aL,
3246 * increment aL by src1.z for a total of src1.x iterations.
3247 * Need to use a temporary variable for this operation.
3249 /* FIXME: I don't think nested loops will work correctly this way. */
3250 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3252 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3253 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3254 const struct wined3d_shader *shader = ins->ctx->shader;
3255 const struct wined3d_shader_lconst *constant;
3256 struct glsl_src_param src1_param;
3257 const DWORD *control_values = NULL;
3259 if (ins->ctx->reg_maps->shader_version.major < 4)
3261 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3263 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3264 * class hardware doesn't support real varying indexing, but Microsoft
3265 * designed this feature for Shader model 2.x+. If the loop control is
3266 * known at compile time, the GLSL compiler can unroll the loop, and
3267 * replace indirect addressing with direct addressing. */
3268 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3270 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3272 if (constant->idx == ins->src[1].reg.idx[0].offset)
3274 control_values = constant->value;
3275 break;
3280 if (control_values)
3282 struct wined3d_shader_loop_control loop_control;
3283 loop_control.count = control_values[0];
3284 loop_control.start = control_values[1];
3285 loop_control.step = (int)control_values[2];
3287 if (loop_control.step > 0)
3289 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3290 loop_state->current_depth, loop_control.start,
3291 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3292 loop_state->current_depth, loop_control.step);
3294 else if (loop_control.step < 0)
3296 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3297 loop_state->current_depth, loop_control.start,
3298 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3299 loop_state->current_depth, loop_control.step);
3301 else
3303 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3304 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3305 loop_state->current_depth, loop_control.count,
3306 loop_state->current_depth);
3309 else
3311 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3312 loop_state->current_depth, loop_state->current_reg,
3313 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3314 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3317 ++loop_state->current_reg;
3319 else
3321 shader_addline(buffer, "for (;;)\n{\n");
3324 ++loop_state->current_depth;
3327 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3329 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3331 shader_addline(ins->ctx->buffer, "}\n");
3333 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3335 --loop_state->current_depth;
3336 --loop_state->current_reg;
3339 if (ins->handler_idx == WINED3DSIH_ENDREP)
3341 --loop_state->current_depth;
3345 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3347 const struct wined3d_shader *shader = ins->ctx->shader;
3348 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3349 const struct wined3d_shader_lconst *constant;
3350 struct glsl_src_param src0_param;
3351 const DWORD *control_values = NULL;
3353 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3354 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3356 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3358 if (constant->idx == ins->src[0].reg.idx[0].offset)
3360 control_values = constant->value;
3361 break;
3366 if (control_values)
3368 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3369 loop_state->current_depth, loop_state->current_depth,
3370 control_values[0], loop_state->current_depth);
3372 else
3374 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3375 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3376 loop_state->current_depth, loop_state->current_depth,
3377 src0_param.param_str, loop_state->current_depth);
3380 ++loop_state->current_depth;
3383 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3385 struct glsl_src_param src0_param;
3387 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3388 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3391 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3393 struct glsl_src_param src0_param;
3394 struct glsl_src_param src1_param;
3396 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3397 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3399 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3400 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3403 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3405 shader_addline(ins->ctx->buffer, "} else {\n");
3408 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3410 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3413 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3415 shader_addline(ins->ctx->buffer, "break;\n");
3418 /* FIXME: According to MSDN the compare is done per component. */
3419 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3421 struct glsl_src_param src0_param;
3422 struct glsl_src_param src1_param;
3424 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3425 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3427 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3428 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3431 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3433 struct glsl_src_param src_param;
3435 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3436 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3439 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3441 shader_addline(ins->ctx->buffer, "}\n");
3442 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3445 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3447 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3450 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3452 struct glsl_src_param src1_param;
3454 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3455 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3456 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3459 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3461 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3462 * function only suppresses the unhandled instruction warning
3466 /*********************************************
3467 * Pixel Shader Specific Code begins here
3468 ********************************************/
3469 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3471 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3472 ins->ctx->reg_maps->shader_version.minor);
3473 struct glsl_sample_function sample_function;
3474 DWORD sample_flags = 0;
3475 DWORD sampler_idx;
3476 DWORD mask = 0, swizzle;
3477 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3479 /* 1.0-1.4: Use destination register as sampler source.
3480 * 2.0+: Use provided sampler source. */
3481 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3482 sampler_idx = ins->dst[0].reg.idx[0].offset;
3483 else
3484 sampler_idx = ins->src[1].reg.idx[0].offset;
3486 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3488 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3489 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3490 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3492 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3493 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3495 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3496 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3498 case WINED3D_TTFF_COUNT1:
3499 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3500 break;
3501 case WINED3D_TTFF_COUNT2:
3502 mask = WINED3DSP_WRITEMASK_1;
3503 break;
3504 case WINED3D_TTFF_COUNT3:
3505 mask = WINED3DSP_WRITEMASK_2;
3506 break;
3507 case WINED3D_TTFF_COUNT4:
3508 case WINED3D_TTFF_DISABLE:
3509 mask = WINED3DSP_WRITEMASK_3;
3510 break;
3514 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3516 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3518 if (src_mod == WINED3DSPSM_DZ) {
3519 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3520 mask = WINED3DSP_WRITEMASK_2;
3521 } else if (src_mod == WINED3DSPSM_DW) {
3522 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3523 mask = WINED3DSP_WRITEMASK_3;
3526 else
3528 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3529 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3531 /* ps 2.0 texldp instruction always divides by the fourth component. */
3532 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3533 mask = WINED3DSP_WRITEMASK_3;
3537 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3538 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3540 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3541 mask |= sample_function.coord_mask;
3543 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3544 else swizzle = ins->src[1].swizzle;
3546 /* 1.0-1.3: Use destination register as coordinate source.
3547 1.4+: Use provided coordinate source register. */
3548 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3550 char coord_mask[6];
3551 shader_glsl_write_mask_to_str(mask, coord_mask);
3552 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3553 "T%u%s", sampler_idx, coord_mask);
3555 else
3557 struct glsl_src_param coord_param;
3558 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3559 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3561 struct glsl_src_param bias;
3562 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3563 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3564 "%s", coord_param.param_str);
3565 } else {
3566 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3567 "%s", coord_param.param_str);
3572 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3574 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3575 struct glsl_src_param coord_param, dx_param, dy_param;
3576 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3577 struct glsl_sample_function sample_function;
3578 DWORD sampler_idx;
3579 DWORD swizzle = ins->src[1].swizzle;
3580 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3582 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3584 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3585 shader_glsl_tex(ins);
3586 return;
3589 sampler_idx = ins->src[1].reg.idx[0].offset;
3590 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3591 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3593 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3594 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3595 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3596 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3598 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3599 "%s", coord_param.param_str);
3602 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3604 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3605 struct glsl_src_param coord_param, lod_param;
3606 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3607 struct glsl_sample_function sample_function;
3608 DWORD sampler_idx;
3609 DWORD swizzle = ins->src[1].swizzle;
3610 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3612 sampler_idx = ins->src[1].reg.idx[0].offset;
3613 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3614 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3615 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3617 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3618 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3620 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3622 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3623 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3625 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3626 * However, the NVIDIA drivers allow them in fragment shaders as well,
3627 * even without the appropriate extension. */
3628 WARN("Using %s in fragment shader.\n", sample_function.name);
3630 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3631 "%s", coord_param.param_str);
3634 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3636 /* FIXME: Make this work for more than just 2D textures */
3637 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3638 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3640 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3642 char dst_mask[6];
3644 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3645 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3646 ins->dst[0].reg.idx[0].offset, dst_mask);
3648 else
3650 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3651 DWORD reg = ins->src[0].reg.idx[0].offset;
3652 char dst_swizzle[6];
3654 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3656 if (src_mod == WINED3DSPSM_DZ)
3658 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3659 struct glsl_src_param div_param;
3661 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3663 if (mask_size > 1) {
3664 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3665 } else {
3666 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3669 else if (src_mod == WINED3DSPSM_DW)
3671 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3672 struct glsl_src_param div_param;
3674 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3676 if (mask_size > 1) {
3677 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3678 } else {
3679 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3681 } else {
3682 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3687 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3688 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3689 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3690 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3692 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3693 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3694 struct glsl_sample_function sample_function;
3695 struct glsl_src_param src0_param;
3696 UINT mask_size;
3698 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3700 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3701 * scalar, and projected sampling would require 4.
3703 * It is a dependent read - not valid with conditional NP2 textures
3705 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3706 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3708 switch(mask_size)
3710 case 1:
3711 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3712 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3713 break;
3715 case 2:
3716 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3717 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3718 break;
3720 case 3:
3721 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3722 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3723 break;
3725 default:
3726 FIXME("Unexpected mask size %u\n", mask_size);
3727 break;
3731 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3732 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3733 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3735 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3736 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3737 struct glsl_src_param src0_param;
3738 DWORD dst_mask;
3739 unsigned int mask_size;
3741 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3742 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3743 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3745 if (mask_size > 1) {
3746 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3747 } else {
3748 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3752 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3753 * Calculate the depth as dst.x / dst.y */
3754 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3756 struct glsl_dst_param dst_param;
3758 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3760 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3761 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3762 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3763 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3764 * >= 1.0 or < 0.0
3766 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3767 dst_param.reg_name, dst_param.reg_name);
3770 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3771 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3772 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3773 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3775 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3777 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3778 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3779 struct glsl_src_param src0_param;
3781 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3783 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3784 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3787 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3788 * Calculate the 1st of a 2-row matrix multiplication. */
3789 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3791 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3792 DWORD reg = ins->dst[0].reg.idx[0].offset;
3793 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3794 struct glsl_src_param src0_param;
3796 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3797 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3800 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3801 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3802 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3804 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3805 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3806 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3807 DWORD reg = ins->dst[0].reg.idx[0].offset;
3808 struct glsl_src_param src0_param;
3810 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3811 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3812 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3815 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3817 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3818 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3819 struct glsl_sample_function sample_function;
3820 DWORD reg = ins->dst[0].reg.idx[0].offset;
3821 struct glsl_src_param src0_param;
3823 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3824 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3826 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3828 /* Sample the texture using the calculated coordinates */
3829 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3832 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3833 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3834 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3836 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3837 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3838 struct glsl_sample_function sample_function;
3839 DWORD reg = ins->dst[0].reg.idx[0].offset;
3840 struct glsl_src_param src0_param;
3842 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3843 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3845 /* Dependent read, not valid with conditional NP2 */
3846 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3848 /* Sample the texture using the calculated coordinates */
3849 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3851 tex_mx->current_row = 0;
3854 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3855 * Perform the 3rd row of a 3x3 matrix multiply */
3856 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3858 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3859 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3860 DWORD reg = ins->dst[0].reg.idx[0].offset;
3861 struct glsl_src_param src0_param;
3862 char dst_mask[6];
3864 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3866 shader_glsl_append_dst(ins->ctx->buffer, ins);
3867 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3868 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3870 tex_mx->current_row = 0;
3873 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3874 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3875 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3877 struct glsl_src_param src0_param;
3878 struct glsl_src_param src1_param;
3879 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3880 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3881 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3882 struct glsl_sample_function sample_function;
3883 DWORD reg = ins->dst[0].reg.idx[0].offset;
3884 char coord_mask[6];
3886 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3887 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3889 /* Perform the last matrix multiply operation */
3890 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3891 /* Reflection calculation */
3892 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3894 /* Dependent read, not valid with conditional NP2 */
3895 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3896 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3898 /* Sample the texture */
3899 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3900 NULL, NULL, NULL, "tmp0%s", coord_mask);
3902 tex_mx->current_row = 0;
3905 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3906 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3907 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3909 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3910 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3911 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3912 struct glsl_sample_function sample_function;
3913 DWORD reg = ins->dst[0].reg.idx[0].offset;
3914 struct glsl_src_param src0_param;
3915 char coord_mask[6];
3917 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3919 /* Perform the last matrix multiply operation */
3920 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3922 /* Construct the eye-ray vector from w coordinates */
3923 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3924 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3925 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3927 /* Dependent read, not valid with conditional NP2 */
3928 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3929 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3931 /* Sample the texture using the calculated coordinates */
3932 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3933 NULL, NULL, NULL, "tmp0%s", coord_mask);
3935 tex_mx->current_row = 0;
3938 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3939 * Apply a fake bump map transform.
3940 * texbem is pshader <= 1.3 only, this saves a few version checks
3942 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3944 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3945 struct glsl_sample_function sample_function;
3946 struct glsl_src_param coord_param;
3947 DWORD sampler_idx;
3948 DWORD mask;
3949 DWORD flags;
3950 char coord_mask[6];
3952 sampler_idx = ins->dst[0].reg.idx[0].offset;
3953 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3954 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3956 /* Dependent read, not valid with conditional NP2 */
3957 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3958 mask = sample_function.coord_mask;
3960 shader_glsl_write_mask_to_str(mask, coord_mask);
3962 /* With projected textures, texbem only divides the static texture coord,
3963 * not the displacement, so we can't let GL handle this. */
3964 if (flags & WINED3D_PSARGS_PROJECTED)
3966 DWORD div_mask=0;
3967 char coord_div_mask[3];
3968 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3970 case WINED3D_TTFF_COUNT1:
3971 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3972 break;
3973 case WINED3D_TTFF_COUNT2:
3974 div_mask = WINED3DSP_WRITEMASK_1;
3975 break;
3976 case WINED3D_TTFF_COUNT3:
3977 div_mask = WINED3DSP_WRITEMASK_2;
3978 break;
3979 case WINED3D_TTFF_COUNT4:
3980 case WINED3D_TTFF_DISABLE:
3981 div_mask = WINED3DSP_WRITEMASK_3;
3982 break;
3984 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3985 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3988 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3990 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3991 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3992 coord_param.param_str, coord_mask);
3994 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3996 struct glsl_src_param luminance_param;
3997 struct glsl_dst_param dst_param;
3999 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4000 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4002 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4003 dst_param.reg_name, dst_param.mask_str,
4004 luminance_param.param_str, sampler_idx, sampler_idx);
4008 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4010 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4011 struct glsl_src_param src0_param, src1_param;
4013 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4014 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4016 shader_glsl_append_dst(ins->ctx->buffer, ins);
4017 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4018 src0_param.param_str, sampler_idx, src1_param.param_str);
4021 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4022 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4023 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4025 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4026 struct glsl_sample_function sample_function;
4027 struct glsl_src_param src0_param;
4029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4031 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4032 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4033 "%s.wx", src0_param.reg_name);
4036 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4037 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4038 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4040 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4041 struct glsl_sample_function sample_function;
4042 struct glsl_src_param src0_param;
4044 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4046 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4047 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4048 "%s.yz", src0_param.reg_name);
4051 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4052 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4053 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4055 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4056 struct glsl_sample_function sample_function;
4057 struct glsl_src_param src0_param;
4059 /* Dependent read, not valid with conditional NP2 */
4060 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4061 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4063 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4064 "%s", src0_param.param_str);
4067 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4068 * If any of the first 3 components are < 0, discard this pixel */
4069 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4071 struct glsl_dst_param dst_param;
4073 /* The argument is a destination parameter, and no writemasks are allowed */
4074 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4075 if (ins->ctx->reg_maps->shader_version.major >= 2)
4077 if (ins->ctx->reg_maps->shader_version.major >= 4)
4078 FIXME("SM4 discard not implemented.\n");
4079 /* 2.0 shaders compare all 4 components in texkill */
4080 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4081 } else {
4082 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4083 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4084 * 4 components are defined, only the first 3 are used
4086 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4090 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4091 * dst = dot2(src0, src1) + src2 */
4092 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4094 struct glsl_src_param src0_param;
4095 struct glsl_src_param src1_param;
4096 struct glsl_src_param src2_param;
4097 DWORD write_mask;
4098 unsigned int mask_size;
4100 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4101 mask_size = shader_glsl_get_write_mask_size(write_mask);
4103 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4104 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4105 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4107 if (mask_size > 1) {
4108 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4109 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4110 } else {
4111 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4112 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4116 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4117 const struct wined3d_shader_signature_element *input_signature,
4118 const struct wined3d_shader_reg_maps *reg_maps,
4119 enum vertexprocessing_mode vertexprocessing)
4121 WORD map = reg_maps->input_registers;
4122 unsigned int i;
4124 for (i = 0; map; map >>= 1, ++i)
4126 const char *semantic_name;
4127 UINT semantic_idx;
4128 char reg_mask[6];
4130 /* Unused */
4131 if (!(map & 1)) continue;
4133 semantic_name = input_signature[i].semantic_name;
4134 semantic_idx = input_signature[i].semantic_idx;
4135 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4137 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4139 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4140 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4141 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4142 else
4143 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4144 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4146 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4148 if (!semantic_idx)
4149 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4150 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4151 else if (semantic_idx == 1)
4152 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4153 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4154 else
4155 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4156 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4158 else
4160 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4161 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4166 /*********************************************
4167 * Vertex Shader Specific Code begins here
4168 ********************************************/
4170 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4172 struct glsl_program_key key;
4174 key.vs_id = entry->vs.id;
4175 key.gs_id = entry->gs.id;
4176 key.ps_id = entry->ps.id;
4178 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4180 ERR("Failed to insert program entry.\n");
4184 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4185 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4187 struct wine_rb_entry *entry;
4188 struct glsl_program_key key;
4190 key.vs_id = vs_id;
4191 key.gs_id = gs_id;
4192 key.ps_id = ps_id;
4194 entry = wine_rb_get(&priv->program_lookup, &key);
4195 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4198 /* Context activation is done by the caller. */
4199 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4200 struct glsl_shader_prog_link *entry)
4202 struct glsl_program_key key;
4204 key.vs_id = entry->vs.id;
4205 key.gs_id = entry->gs.id;
4206 key.ps_id = entry->ps.id;
4207 wine_rb_remove(&priv->program_lookup, &key);
4209 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4210 if (entry->vs.id)
4211 list_remove(&entry->vs.shader_entry);
4212 if (entry->gs.id)
4213 list_remove(&entry->gs.shader_entry);
4214 if (entry->ps.id)
4215 list_remove(&entry->ps.shader_entry);
4216 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4217 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4218 HeapFree(GetProcessHeap(), 0, entry);
4221 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4222 const struct wined3d_gl_info *gl_info, const DWORD *map,
4223 const struct wined3d_shader_signature_element *input_signature,
4224 const struct wined3d_shader_reg_maps *reg_maps_in,
4225 const struct wined3d_shader_signature_element *output_signature,
4226 const struct wined3d_shader_reg_maps *reg_maps_out)
4228 unsigned int i, j;
4229 const char *semantic_name_in;
4230 UINT semantic_idx_in;
4231 DWORD *set;
4232 DWORD in_idx;
4233 unsigned int in_count = vec4_varyings(3, gl_info);
4234 char reg_mask[6];
4235 char destination[50];
4236 WORD input_map, output_map;
4238 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4240 input_map = reg_maps_in->input_registers;
4241 for (i = 0; input_map; input_map >>= 1, ++i)
4243 if (!(input_map & 1)) continue;
4245 in_idx = map[i];
4246 /* Declared, but not read register */
4247 if (in_idx == ~0U) continue;
4248 if (in_idx >= (in_count + 2))
4250 FIXME("More input varyings declared than supported, expect issues.\n");
4251 continue;
4254 if (in_idx == in_count)
4255 sprintf(destination, "gl_FrontColor");
4256 else if (in_idx == in_count + 1)
4257 sprintf(destination, "gl_FrontSecondaryColor");
4258 else
4259 sprintf(destination, "ps_in[%u]", in_idx);
4261 semantic_name_in = input_signature[i].semantic_name;
4262 semantic_idx_in = input_signature[i].semantic_idx;
4263 set[in_idx] = ~0U;
4265 output_map = reg_maps_out->output_registers;
4266 for (j = 0; output_map; output_map >>= 1, ++j)
4268 DWORD mask;
4270 if (!(output_map & 1)
4271 || semantic_idx_in != output_signature[j].semantic_idx
4272 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4273 || !(mask = input_signature[i].mask & output_signature[j].mask))
4274 continue;
4276 set[in_idx] = mask;
4277 shader_glsl_write_mask_to_str(mask, reg_mask);
4279 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4280 destination, reg_mask, j, reg_mask);
4284 for (i = 0; i < in_count + 2; ++i)
4286 unsigned int size;
4288 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4289 continue;
4291 if (set[i] == ~0U) set[i] = 0;
4293 size = 0;
4294 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4295 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4296 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4297 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4298 reg_mask[size] = '\0';
4300 if (i == in_count)
4301 sprintf(destination, "gl_FrontColor");
4302 else if (i == in_count + 1)
4303 sprintf(destination, "gl_FrontSecondaryColor");
4304 else
4305 sprintf(destination, "ps_in[%u]", i);
4307 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4308 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4311 HeapFree(GetProcessHeap(), 0, set);
4314 /* Context activation is done by the caller. */
4315 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4316 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4317 const struct wined3d_gl_info *gl_info)
4319 GLhandleARB ret = 0;
4320 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4321 unsigned int i;
4322 const char *semantic_name;
4323 UINT semantic_idx;
4324 char reg_mask[6];
4325 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4326 WORD map = vs->reg_maps.output_registers;
4328 shader_buffer_clear(buffer);
4330 shader_addline(buffer, "#version 120\n");
4332 if (ps_major < 3)
4334 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4336 for (i = 0; map; map >>= 1, ++i)
4338 DWORD write_mask;
4340 if (!(map & 1)) continue;
4342 semantic_name = output_signature[i].semantic_name;
4343 semantic_idx = output_signature[i].semantic_idx;
4344 write_mask = output_signature[i].mask;
4345 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4347 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4349 if (!semantic_idx)
4350 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4351 reg_mask, i, reg_mask);
4352 else if (semantic_idx == 1)
4353 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4354 reg_mask, i, reg_mask);
4356 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4358 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4359 reg_mask, i, reg_mask);
4361 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4363 if (semantic_idx < 8)
4365 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4366 write_mask |= WINED3DSP_WRITEMASK_3;
4368 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4369 semantic_idx, reg_mask, i, reg_mask);
4370 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4371 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4374 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4376 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4378 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4380 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4383 shader_addline(buffer, "}\n");
4385 else
4387 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4388 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4389 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4390 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4392 /* First, sort out position and point size. Those are not passed to the pixel shader */
4393 for (i = 0; map; map >>= 1, ++i)
4395 if (!(map & 1)) continue;
4397 semantic_name = output_signature[i].semantic_name;
4398 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4400 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4402 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4403 reg_mask, i, reg_mask);
4405 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4407 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4411 /* Then, fix the pixel shader input */
4412 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4413 &ps->reg_maps, output_signature, &vs->reg_maps);
4415 shader_addline(buffer, "}\n");
4418 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4419 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4420 shader_glsl_compile(gl_info, ret, buffer->buffer);
4422 return ret;
4425 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4427 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4428 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4429 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4430 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4431 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4432 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4435 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4437 switch (mode)
4439 case WINED3D_FFP_PS_FOG_OFF:
4440 return;
4442 case WINED3D_FFP_PS_FOG_LINEAR:
4443 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4444 break;
4446 case WINED3D_FFP_PS_FOG_EXP:
4447 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4448 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4449 break;
4451 case WINED3D_FFP_PS_FOG_EXP2:
4452 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4453 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4454 break;
4456 default:
4457 ERR("Invalid fog mode %#x.\n", mode);
4458 return;
4461 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4464 /* Context activation is done by the caller. */
4465 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4466 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4467 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4469 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4470 const struct wined3d_gl_info *gl_info = context->gl_info;
4471 const DWORD *function = shader->function;
4472 struct shader_glsl_ctx_priv priv_ctx;
4474 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4475 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4477 memset(&priv_ctx, 0, sizeof(priv_ctx));
4478 priv_ctx.cur_ps_args = args;
4479 priv_ctx.cur_np2fixup_info = np2fixup_info;
4481 shader_addline(buffer, "#version 120\n");
4483 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4484 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4485 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4486 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4487 /* The spec says that it doesn't have to be explicitly enabled, but the
4488 * nvidia drivers write a warning if we don't do so. */
4489 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4490 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4491 if (gl_info->supported[EXT_GPU_SHADER4])
4492 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4494 /* Base Declarations */
4495 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4497 /* Pack 3.0 inputs */
4498 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4499 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4501 /* Base Shader Body */
4502 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4504 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4505 if (reg_maps->shader_version.major < 2)
4507 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4508 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4511 if (args->srgb_correction)
4512 shader_glsl_generate_srgb_write_correction(buffer);
4514 /* SM < 3 does not replace the fog stage. */
4515 if (reg_maps->shader_version.major < 3)
4516 shader_glsl_generate_fog_code(buffer, args->fog);
4518 shader_addline(buffer, "}\n");
4520 TRACE("Compiling shader object %u\n", shader_obj);
4521 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4523 /* Store the shader object */
4524 return shader_obj;
4527 /* Context activation is done by the caller. */
4528 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4529 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4530 const struct vs_compile_args *args)
4532 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4533 const struct wined3d_gl_info *gl_info = context->gl_info;
4534 const DWORD *function = shader->function;
4535 struct shader_glsl_ctx_priv priv_ctx;
4537 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4538 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4540 shader_addline(buffer, "#version 120\n");
4542 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4543 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4544 if (gl_info->supported[EXT_GPU_SHADER4])
4545 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4547 memset(&priv_ctx, 0, sizeof(priv_ctx));
4548 priv_ctx.cur_vs_args = args;
4550 /* Base Declarations */
4551 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4553 /* Base Shader Body */
4554 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4556 /* Unpack outputs */
4557 shader_addline(buffer, "order_ps_input(vs_out);\n");
4559 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4560 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4561 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4562 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4564 if (args->fog_src == VS_FOG_Z)
4565 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4566 else if (!reg_maps->fog)
4567 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4569 /* We always store the clipplanes without y inversion */
4570 if (args->clip_enabled)
4571 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4573 /* Write the final position.
4575 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4576 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4577 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4578 * contains 1.0 to allow a mad.
4580 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4581 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4583 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4585 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4586 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4587 * which is the same as z = z * 2 - w.
4589 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4591 shader_addline(buffer, "}\n");
4593 TRACE("Compiling shader object %u\n", shader_obj);
4594 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4596 return shader_obj;
4599 /* Context activation is done by the caller. */
4600 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4601 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4603 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4604 const struct wined3d_gl_info *gl_info = context->gl_info;
4605 const DWORD *function = shader->function;
4606 struct shader_glsl_ctx_priv priv_ctx;
4607 GLhandleARB shader_id;
4609 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4611 shader_addline(buffer, "#version 120\n");
4613 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4614 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4615 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4616 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4617 if (gl_info->supported[EXT_GPU_SHADER4])
4618 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4620 memset(&priv_ctx, 0, sizeof(priv_ctx));
4621 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4622 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4623 shader_addline(buffer, "}\n");
4625 TRACE("Compiling shader object %u.\n", shader_id);
4626 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4628 return shader_id;
4631 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4632 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4633 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4635 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4636 struct glsl_shader_private *shader_data;
4637 struct ps_np2fixup_info *np2fixup;
4638 UINT i;
4639 DWORD new_size;
4640 GLhandleARB ret;
4642 if (!shader->backend_data)
4644 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4645 if (!shader->backend_data)
4647 ERR("Failed to allocate backend data.\n");
4648 return 0;
4651 shader_data = shader->backend_data;
4652 gl_shaders = shader_data->gl_shaders.ps;
4654 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4655 * so a linear search is more performant than a hashmap or a binary search
4656 * (cache coherency etc)
4658 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4660 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4662 if (args->np2_fixup)
4663 *np2fixup_info = &gl_shaders[i].np2fixup;
4664 return gl_shaders[i].prgId;
4668 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4669 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4670 if (shader_data->num_gl_shaders)
4672 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4673 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4674 new_size * sizeof(*gl_shaders));
4676 else
4678 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4679 new_size = 1;
4682 if(!new_array) {
4683 ERR("Out of memory\n");
4684 return 0;
4686 shader_data->gl_shaders.ps = new_array;
4687 shader_data->shader_array_size = new_size;
4688 gl_shaders = new_array;
4691 gl_shaders[shader_data->num_gl_shaders].args = *args;
4693 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4694 memset(np2fixup, 0, sizeof(*np2fixup));
4695 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4697 pixelshader_update_samplers(shader, args->tex_types);
4699 shader_buffer_clear(buffer);
4700 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4701 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4703 return ret;
4706 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4707 const DWORD use_map) {
4708 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4709 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4710 return stored->fog_src == new->fog_src;
4713 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4714 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4715 const struct vs_compile_args *args)
4717 UINT i;
4718 DWORD new_size;
4719 DWORD use_map = context->stream_info.use_map;
4720 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4721 struct glsl_shader_private *shader_data;
4722 GLhandleARB ret;
4724 if (!shader->backend_data)
4726 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4727 if (!shader->backend_data)
4729 ERR("Failed to allocate backend data.\n");
4730 return 0;
4733 shader_data = shader->backend_data;
4734 gl_shaders = shader_data->gl_shaders.vs;
4736 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4737 * so a linear search is more performant than a hashmap or a binary search
4738 * (cache coherency etc)
4740 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4742 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4743 return gl_shaders[i].prgId;
4746 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4748 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4749 if (shader_data->num_gl_shaders)
4751 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4752 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4753 new_size * sizeof(*gl_shaders));
4755 else
4757 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4758 new_size = 1;
4761 if(!new_array) {
4762 ERR("Out of memory\n");
4763 return 0;
4765 shader_data->gl_shaders.vs = new_array;
4766 shader_data->shader_array_size = new_size;
4767 gl_shaders = new_array;
4770 gl_shaders[shader_data->num_gl_shaders].args = *args;
4772 shader_buffer_clear(buffer);
4773 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4774 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4776 return ret;
4779 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4780 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4782 struct glsl_gs_compiled_shader *gl_shaders;
4783 struct glsl_shader_private *shader_data;
4784 GLhandleARB ret;
4786 if (!shader->backend_data)
4788 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4790 ERR("Failed to allocate backend data.\n");
4791 return 0;
4794 shader_data = shader->backend_data;
4795 gl_shaders = shader_data->gl_shaders.gs;
4797 if (shader_data->num_gl_shaders)
4798 return gl_shaders[0].id;
4800 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4802 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4804 ERR("Failed to allocate GL shader array.\n");
4805 return 0;
4807 shader_data->shader_array_size = 1;
4808 gl_shaders = shader_data->gl_shaders.gs;
4810 shader_buffer_clear(buffer);
4811 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4812 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4814 return ret;
4817 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4819 switch (mcs)
4821 case WINED3D_MCS_MATERIAL:
4822 return material;
4823 case WINED3D_MCS_COLOR1:
4824 return "gl_Color";
4825 case WINED3D_MCS_COLOR2:
4826 return "gl_SecondaryColor";
4827 default:
4828 ERR("Invalid material color source %#x.\n", mcs);
4829 return "<invalid>";
4833 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4834 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4836 const char *diffuse, *specular, *emission, *ambient;
4837 enum wined3d_light_type light_type;
4838 unsigned int i;
4840 if (!settings->lighting)
4842 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4843 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4844 return;
4847 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4848 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4849 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4850 shader_addline(buffer, "vec3 dir, dst;\n");
4851 shader_addline(buffer, "float att, t;\n");
4853 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4854 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4855 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4856 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4858 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4860 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4861 switch (light_type)
4863 case WINED3D_LIGHT_POINT:
4864 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4865 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4866 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4867 shader_addline(buffer, "dst.x = 1.0;\n");
4868 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4869 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4870 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4871 if (!settings->normal)
4872 break;
4873 shader_addline(buffer, "dir = normalize(dir);\n");
4874 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4875 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4876 if (settings->localviewer)
4877 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4878 else
4879 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4880 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4881 " * gl_LightSource[%u].specular) / att;\n", i);
4882 break;
4884 case WINED3D_LIGHT_SPOT:
4885 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4886 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4887 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4888 shader_addline(buffer, "dst.x = 1.0;\n");
4889 shader_addline(buffer, "dir = normalize(dir);\n");
4890 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4891 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4892 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4893 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4894 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4895 i, i, i, i);
4896 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4897 if (!settings->normal)
4898 break;
4899 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4900 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4901 if (settings->localviewer)
4902 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4903 else
4904 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4905 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4906 " * gl_LightSource[%u].specular) * att;\n", i);
4907 break;
4909 case WINED3D_LIGHT_DIRECTIONAL:
4910 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4911 if (!settings->normal)
4912 break;
4913 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4914 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4915 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4916 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4917 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4918 " * gl_LightSource[%u].specular;\n", i);
4919 break;
4921 default:
4922 if (light_type)
4923 FIXME("Unhandled light type %#x.\n", light_type);
4924 continue;
4928 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4929 ambient, diffuse, emission);
4930 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4931 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4934 /* Context activation is done by the caller. */
4935 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4936 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4938 GLhandleARB shader_obj;
4939 unsigned int i;
4941 shader_buffer_clear(buffer);
4943 shader_addline(buffer, "#version 120\n");
4944 shader_addline(buffer, "\n");
4945 shader_addline(buffer, "void main()\n{\n");
4946 shader_addline(buffer, "float m;\n");
4947 shader_addline(buffer, "vec3 r;\n");
4949 if (settings->transformed)
4951 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4952 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4953 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4955 else
4957 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4958 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4959 if (settings->clipping)
4960 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4961 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4964 if (!settings->normal)
4965 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4966 else if (settings->normalize)
4967 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4968 else
4969 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4971 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4973 for (i = 0; i < MAX_TEXTURES; ++i)
4975 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4977 case WINED3DTSS_TCI_PASSTHRU:
4978 if (settings->texcoords & (1 << i))
4979 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4980 i, i, i);
4981 break;
4983 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4984 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4985 break;
4987 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4988 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4989 break;
4991 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4992 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4993 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
4994 break;
4996 case WINED3DTSS_TCI_SPHEREMAP:
4997 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
4998 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
4999 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5000 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5001 break;
5003 default:
5004 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5005 break;
5009 switch (settings->fog_mode)
5011 case WINED3D_FFP_VS_FOG_OFF:
5012 break;
5014 case WINED3D_FFP_VS_FOG_FOGCOORD:
5015 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5016 break;
5018 case WINED3D_FFP_VS_FOG_RANGE:
5019 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5020 break;
5022 case WINED3D_FFP_VS_FOG_DEPTH:
5023 if (settings->ortho_fog)
5024 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5025 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5026 else
5027 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5028 break;
5030 default:
5031 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5032 break;
5035 if (settings->point_size)
5037 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5038 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5039 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5040 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5043 shader_addline(buffer, "}\n");
5045 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5046 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5048 return shader_obj;
5051 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5052 DWORD argnum, unsigned int stage, DWORD arg)
5054 const char *ret;
5056 if (arg == ARG_UNUSED)
5057 return "<unused arg>";
5059 switch (arg & WINED3DTA_SELECTMASK)
5061 case WINED3DTA_DIFFUSE:
5062 ret = "gl_Color";
5063 break;
5065 case WINED3DTA_CURRENT:
5066 if (!stage)
5067 ret = "gl_Color";
5068 else
5069 ret = "ret";
5070 break;
5072 case WINED3DTA_TEXTURE:
5073 switch (stage)
5075 case 0: ret = "tex0"; break;
5076 case 1: ret = "tex1"; break;
5077 case 2: ret = "tex2"; break;
5078 case 3: ret = "tex3"; break;
5079 case 4: ret = "tex4"; break;
5080 case 5: ret = "tex5"; break;
5081 case 6: ret = "tex6"; break;
5082 case 7: ret = "tex7"; break;
5083 default:
5084 ret = "<invalid texture>";
5085 break;
5087 break;
5089 case WINED3DTA_TFACTOR:
5090 ret = "tex_factor";
5091 break;
5093 case WINED3DTA_SPECULAR:
5094 ret = "gl_SecondaryColor";
5095 break;
5097 case WINED3DTA_TEMP:
5098 ret = "temp_reg";
5099 break;
5101 case WINED3DTA_CONSTANT:
5102 switch (stage)
5104 case 0: ret = "tss_const0"; break;
5105 case 1: ret = "tss_const1"; break;
5106 case 2: ret = "tss_const2"; break;
5107 case 3: ret = "tss_const3"; break;
5108 case 4: ret = "tss_const4"; break;
5109 case 5: ret = "tss_const5"; break;
5110 case 6: ret = "tss_const6"; break;
5111 case 7: ret = "tss_const7"; break;
5112 default:
5113 ret = "<invalid constant>";
5114 break;
5116 break;
5118 default:
5119 return "<unhandled arg>";
5122 if (arg & WINED3DTA_COMPLEMENT)
5124 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5125 if (argnum == 0)
5126 ret = "arg0";
5127 else if (argnum == 1)
5128 ret = "arg1";
5129 else if (argnum == 2)
5130 ret = "arg2";
5133 if (arg & WINED3DTA_ALPHAREPLICATE)
5135 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5136 if (argnum == 0)
5137 ret = "arg0";
5138 else if (argnum == 1)
5139 ret = "arg1";
5140 else if (argnum == 2)
5141 ret = "arg2";
5144 return ret;
5147 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5148 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5150 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5152 if (color && alpha)
5153 dstmask = "";
5154 else if (color)
5155 dstmask = ".xyz";
5156 else
5157 dstmask = ".w";
5159 if (dst == tempreg)
5160 dstreg = "temp_reg";
5161 else
5162 dstreg = "ret";
5164 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5165 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5166 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5168 switch (op)
5170 case WINED3D_TOP_DISABLE:
5171 if (!stage)
5172 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5173 break;
5175 case WINED3D_TOP_SELECT_ARG1:
5176 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5177 break;
5179 case WINED3D_TOP_SELECT_ARG2:
5180 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5181 break;
5183 case WINED3D_TOP_MODULATE:
5184 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5185 break;
5187 case WINED3D_TOP_MODULATE_4X:
5188 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5189 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5190 break;
5192 case WINED3D_TOP_MODULATE_2X:
5193 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5194 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5195 break;
5197 case WINED3D_TOP_ADD:
5198 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5199 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5200 break;
5202 case WINED3D_TOP_ADD_SIGNED:
5203 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5204 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5205 break;
5207 case WINED3D_TOP_ADD_SIGNED_2X:
5208 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5209 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5210 break;
5212 case WINED3D_TOP_SUBTRACT:
5213 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5214 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5215 break;
5217 case WINED3D_TOP_ADD_SMOOTH:
5218 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5219 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5220 break;
5222 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5223 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5224 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5225 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5226 break;
5228 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5229 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5230 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5231 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5232 break;
5234 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5235 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5236 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5237 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5238 break;
5240 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5241 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5242 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5243 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5244 break;
5246 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5247 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5248 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5249 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5250 break;
5252 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5253 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5254 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5255 break;
5257 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5258 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5259 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5260 break;
5262 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5263 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5264 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5265 break;
5266 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5267 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5268 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5269 break;
5271 case WINED3D_TOP_BUMPENVMAP:
5272 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5273 /* These are handled in the first pass, nothing to do. */
5274 break;
5276 case WINED3D_TOP_DOTPRODUCT3:
5277 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5278 dstreg, dstmask, arg1, arg2, dstmask);
5279 break;
5281 case WINED3D_TOP_MULTIPLY_ADD:
5282 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5283 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5284 break;
5286 case WINED3D_TOP_LERP:
5287 /* MSDN isn't quite right here. */
5288 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5289 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5290 break;
5292 default:
5293 FIXME("Unhandled operation %#x.\n", op);
5294 break;
5298 /* Context activation is done by the caller. */
5299 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5300 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5302 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5303 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5304 const char *final_combiner_src = "ret";
5305 UINT lowest_disabled_stage;
5306 GLhandleARB shader_obj;
5307 DWORD arg0, arg1, arg2;
5308 unsigned int stage;
5310 shader_buffer_clear(buffer);
5312 /* Find out which textures are read */
5313 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5315 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5316 break;
5318 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5319 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5320 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5322 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5323 tex_map |= 1 << stage;
5324 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5325 tfactor_used = TRUE;
5326 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5327 tempreg_used = TRUE;
5328 if (settings->op[stage].dst == tempreg)
5329 tempreg_used = TRUE;
5330 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5331 tss_const_map |= 1 << stage;
5333 switch (settings->op[stage].cop)
5335 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5336 lum_map |= 1 << stage;
5337 /* fall through */
5338 case WINED3D_TOP_BUMPENVMAP:
5339 bump_map |= 1 << stage;
5340 /* fall through */
5341 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5342 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5343 tex_map |= 1 << stage;
5344 break;
5346 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5347 tfactor_used = TRUE;
5348 break;
5350 default:
5351 break;
5354 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5355 continue;
5357 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5358 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5359 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5361 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5362 tex_map |= 1 << stage;
5363 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5364 tfactor_used = TRUE;
5365 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5366 tempreg_used = TRUE;
5367 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5368 tss_const_map |= 1 << stage;
5370 lowest_disabled_stage = stage;
5372 shader_addline(buffer, "#version 120\n");
5374 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5375 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5377 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5378 shader_addline(buffer, "vec4 ret;\n");
5379 if (tempreg_used || settings->sRGB_write)
5380 shader_addline(buffer, "vec4 temp_reg;\n");
5381 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5383 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5385 if (tss_const_map & (1 << stage))
5386 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5388 if (!(tex_map & (1 << stage)))
5389 continue;
5391 switch (settings->op[stage].tex_type)
5393 case tex_1d:
5394 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5395 break;
5396 case tex_2d:
5397 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5398 break;
5399 case tex_3d:
5400 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5401 break;
5402 case tex_cube:
5403 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5404 break;
5405 case tex_rect:
5406 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5407 break;
5408 default:
5409 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5410 break;
5413 shader_addline(buffer, "vec4 tex%u;\n", stage);
5415 if (!(bump_map & (1 << stage)))
5416 continue;
5417 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5419 if (!(lum_map & (1 << stage)))
5420 continue;
5421 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5422 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5424 if (tfactor_used)
5425 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5426 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5428 if (settings->sRGB_write)
5430 shader_addline(buffer, "const vec4 srgb_const0 = ");
5431 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5432 shader_addline(buffer, ";\n");
5433 shader_addline(buffer, "const vec4 srgb_const1 = ");
5434 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5435 shader_addline(buffer, ";\n");
5438 shader_addline(buffer, "void main()\n{\n");
5440 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5441 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5443 /* Generate texture sampling instructions) */
5444 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5446 const char *texture_function, *coord_mask;
5447 char tex_reg_name[8];
5448 BOOL proj;
5450 if (!(tex_map & (1 << stage)))
5451 continue;
5453 if (settings->op[stage].projected == proj_none)
5455 proj = FALSE;
5457 else if (settings->op[stage].projected == proj_count4
5458 || settings->op[stage].projected == proj_count3)
5460 proj = TRUE;
5462 else
5464 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5465 proj = TRUE;
5468 switch (settings->op[stage].tex_type)
5470 case tex_1d:
5471 if (proj)
5473 texture_function = "texture1DProj";
5474 coord_mask = "xw";
5476 else
5478 texture_function = "texture1D";
5479 coord_mask = "x";
5481 break;
5482 case tex_2d:
5483 if (proj)
5485 texture_function = "texture2DProj";
5486 coord_mask = "xyw";
5488 else
5490 texture_function = "texture2D";
5491 coord_mask = "xy";
5493 break;
5494 case tex_3d:
5495 if (proj)
5497 texture_function = "texture3DProj";
5498 coord_mask = "xyzw";
5500 else
5502 texture_function = "texture3D";
5503 coord_mask = "xyz";
5505 break;
5506 case tex_cube:
5507 texture_function = "textureCube";
5508 coord_mask = "xyz";
5509 break;
5510 case tex_rect:
5511 if (proj)
5513 texture_function = "texture2DRectProj";
5514 coord_mask = "xyw";
5516 else
5518 texture_function = "texture2DRect";
5519 coord_mask = "xy";
5521 break;
5522 default:
5523 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5524 texture_function = "";
5525 coord_mask = "xyzw";
5526 break;
5529 if (stage > 0
5530 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5531 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5533 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5535 /* With projective textures, texbem only divides the static
5536 * texture coord, not the displacement, so multiply the
5537 * displacement with the dividing parameter before passing it to
5538 * TXP. */
5539 if (settings->op[stage].projected != proj_none)
5541 if (settings->op[stage].projected == proj_count4)
5543 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5544 stage, stage);
5545 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5547 else
5549 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5550 stage, stage);
5551 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5554 else
5556 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5559 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5560 stage, texture_function, stage, coord_mask);
5562 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5563 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5564 stage, stage - 1, stage - 1, stage - 1);
5566 else if (settings->op[stage].projected == proj_count3)
5568 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5569 stage, texture_function, stage, stage);
5571 else
5573 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5574 stage, texture_function, stage, stage, coord_mask);
5577 sprintf(tex_reg_name, "tex%u", stage);
5578 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5579 settings->op[stage].color_fixup);
5582 /* Generate the main shader */
5583 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5585 BOOL op_equal;
5587 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5589 if (!stage)
5590 final_combiner_src = "gl_Color";
5591 break;
5594 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5595 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5596 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5597 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5598 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5599 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5600 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5601 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5602 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5603 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5604 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5605 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5606 else
5607 op_equal = settings->op[stage].aop == settings->op[stage].cop
5608 && settings->op[stage].carg0 == settings->op[stage].aarg0
5609 && settings->op[stage].carg1 == settings->op[stage].aarg1
5610 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5612 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5614 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5615 settings->op[stage].cop, settings->op[stage].carg0,
5616 settings->op[stage].carg1, settings->op[stage].carg2);
5617 if (!stage)
5618 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5620 else if (op_equal)
5622 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5623 settings->op[stage].cop, settings->op[stage].carg0,
5624 settings->op[stage].carg1, settings->op[stage].carg2);
5626 else
5628 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5629 settings->op[stage].cop, settings->op[stage].carg0,
5630 settings->op[stage].carg1, settings->op[stage].carg2);
5631 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5632 settings->op[stage].aop, settings->op[stage].aarg0,
5633 settings->op[stage].aarg1, settings->op[stage].aarg2);
5637 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5639 if (settings->sRGB_write)
5640 shader_glsl_generate_srgb_write_correction(buffer);
5642 shader_glsl_generate_fog_code(buffer, settings->fog);
5644 shader_addline(buffer, "}\n");
5646 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5647 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5648 return shader_obj;
5651 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5652 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5654 struct glsl_ffp_vertex_shader *shader;
5655 const struct wine_rb_entry *entry;
5657 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5658 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5660 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5661 return NULL;
5663 shader->desc.settings = *settings;
5664 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5665 list_init(&shader->linked_programs);
5666 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5667 ERR("Failed to insert ffp vertex shader.\n");
5669 return shader;
5672 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5673 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5675 struct glsl_ffp_fragment_shader *glsl_desc;
5676 const struct ffp_frag_desc *desc;
5678 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5679 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5681 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5682 return NULL;
5684 glsl_desc->entry.settings = *args;
5685 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5686 list_init(&glsl_desc->linked_programs);
5687 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5689 return glsl_desc;
5693 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5694 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5696 unsigned int i;
5697 char name[32];
5699 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5700 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5701 for (i = 0; i < vs_c_count; ++i)
5703 snprintf(name, sizeof(name), "vs_c[%u]", i);
5704 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5706 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5707 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5709 for (i = 0; i < MAX_CONST_I; ++i)
5711 snprintf(name, sizeof(name), "vs_i[%u]", i);
5712 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5715 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5718 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5719 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5721 unsigned int i;
5722 char name[32];
5724 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5725 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5726 for (i = 0; i < ps_c_count; ++i)
5728 snprintf(name, sizeof(name), "ps_c[%u]", i);
5729 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5731 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5732 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5734 for (i = 0; i < MAX_CONST_I; ++i)
5736 snprintf(name, sizeof(name), "ps_i[%u]", i);
5737 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5740 for (i = 0; i < MAX_TEXTURES; ++i)
5742 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5743 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5744 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5745 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5746 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5747 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5748 snprintf(name, sizeof(name), "tss_const%u", i);
5749 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5752 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5753 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5754 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5755 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5758 /* Context activation is done by the caller. */
5759 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5760 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5762 const struct wined3d_gl_info *gl_info = context->gl_info;
5763 const struct ps_np2fixup_info *np2fixup_info = NULL;
5764 struct glsl_shader_prog_link *entry = NULL;
5765 struct wined3d_shader *vshader = NULL;
5766 struct wined3d_shader *gshader = NULL;
5767 struct wined3d_shader *pshader = NULL;
5768 GLhandleARB programId = 0;
5769 GLhandleARB reorder_shader_id = 0;
5770 unsigned int i;
5771 GLhandleARB vs_id = 0;
5772 GLhandleARB gs_id = 0;
5773 GLhandleARB ps_id = 0;
5774 struct list *ps_list, *vs_list;
5776 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5778 vs_id = ctx_data->glsl_program->vs.id;
5779 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5781 if (use_vs(state))
5783 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5784 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5786 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5787 && ctx_data->glsl_program->gs.id)
5788 gs_id = ctx_data->glsl_program->gs.id;
5789 else if (gshader)
5790 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5793 else if (use_vs(state))
5795 struct vs_compile_args vs_compile_args;
5796 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5798 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5799 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5800 vs_list = &vshader->linked_programs;
5802 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5803 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5805 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5807 struct glsl_ffp_vertex_shader *ffp_shader;
5808 struct wined3d_ffp_vs_settings settings;
5810 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5811 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5812 vs_id = ffp_shader->id;
5813 vs_list = &ffp_shader->linked_programs;
5816 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5818 ps_id = ctx_data->glsl_program->ps.id;
5819 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5821 if (use_ps(state))
5822 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5824 else if (use_ps(state))
5826 struct ps_compile_args ps_compile_args;
5827 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5828 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5829 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5830 pshader, &ps_compile_args, &np2fixup_info);
5831 ps_list = &pshader->linked_programs;
5833 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5835 struct glsl_ffp_fragment_shader *ffp_shader;
5836 struct ffp_frag_settings settings;
5838 gen_ffp_frag_op(context, state, &settings, FALSE);
5839 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5840 ps_id = ffp_shader->id;
5841 ps_list = &ffp_shader->linked_programs;
5844 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5846 ctx_data->glsl_program = entry;
5847 return;
5850 /* If we get to this point, then no matching program exists, so we create one */
5851 programId = GL_EXTCALL(glCreateProgramObjectARB());
5852 TRACE("Created new GLSL shader program %u\n", programId);
5854 /* Create the entry */
5855 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5856 entry->programId = programId;
5857 entry->vs.id = vs_id;
5858 entry->gs.id = gs_id;
5859 entry->ps.id = ps_id;
5860 entry->constant_version = 0;
5861 entry->ps.np2_fixup_info = np2fixup_info;
5862 /* Add the hash table entry */
5863 add_glsl_program_entry(priv, entry);
5865 /* Set the current program */
5866 ctx_data->glsl_program = entry;
5868 /* Attach GLSL vshader */
5869 if (vs_id)
5871 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5872 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5873 checkGLcall("glAttachObjectARB");
5875 list_add_head(vs_list, &entry->vs.shader_entry);
5878 if (vshader)
5880 WORD map = vshader->reg_maps.input_registers;
5881 char tmp_name[10];
5883 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5884 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5885 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5886 checkGLcall("glAttachObjectARB");
5887 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5888 * is destroyed
5890 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5892 /* Bind vertex attributes to a corresponding index number to match
5893 * the same index numbers as ARB_vertex_programs (makes loading
5894 * vertex attributes simpler). With this method, we can use the
5895 * exact same code to load the attributes later for both ARB and
5896 * GLSL shaders.
5898 * We have to do this here because we need to know the Program ID
5899 * in order to make the bindings work, and it has to be done prior
5900 * to linking the GLSL program. */
5901 for (i = 0; map; map >>= 1, ++i)
5903 if (!(map & 1)) continue;
5905 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5906 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5908 checkGLcall("glBindAttribLocationARB");
5911 if (gshader)
5913 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5914 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5915 checkGLcall("glAttachObjectARB");
5917 TRACE("input type %s, output type %s, vertices out %u.\n",
5918 debug_d3dprimitivetype(gshader->u.gs.input_type),
5919 debug_d3dprimitivetype(gshader->u.gs.output_type),
5920 gshader->u.gs.vertices_out);
5921 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5922 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5923 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5924 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5925 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5926 gshader->u.gs.vertices_out));
5927 checkGLcall("glProgramParameteriARB");
5929 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5932 /* Attach GLSL pshader */
5933 if (ps_id)
5935 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5936 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5937 checkGLcall("glAttachObjectARB");
5939 list_add_head(ps_list, &entry->ps.shader_entry);
5942 /* Link the program */
5943 TRACE("Linking GLSL shader program %u\n", programId);
5944 GL_EXTCALL(glLinkProgramARB(programId));
5945 shader_glsl_validate_link(gl_info, programId);
5947 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5948 vshader ? vshader->limits.constant_float : 0);
5949 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5950 pshader ? pshader->limits.constant_float : 0);
5951 checkGLcall("Find glsl program uniform locations");
5953 if (pshader && pshader->reg_maps.shader_version.major >= 3
5954 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5956 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5957 entry->vs.vertex_color_clamp = GL_FALSE;
5959 else
5961 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5964 /* Set the shader to allow uniform loading on it */
5965 GL_EXTCALL(glUseProgramObjectARB(programId));
5966 checkGLcall("glUseProgramObjectARB(programId)");
5968 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5969 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5970 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5971 * vertex shader with fixed function pixel processing is used we make sure that the card
5972 * supports enough samplers to allow the max number of vertex samplers with all possible
5973 * fixed function fragment processing setups. So once the program is linked these samplers
5974 * won't change.
5976 shader_glsl_load_vsamplers(gl_info, context->tex_unit_map, programId);
5977 shader_glsl_load_psamplers(gl_info, context->tex_unit_map, programId);
5979 entry->constant_update_mask = 0;
5980 if (vshader)
5982 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
5983 if (vshader->reg_maps.integer_constants)
5984 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
5985 if (vshader->reg_maps.boolean_constants)
5986 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
5987 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
5990 if (ps_id)
5992 if (pshader)
5994 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
5995 if (pshader->reg_maps.integer_constants)
5996 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
5997 if (pshader->reg_maps.boolean_constants)
5998 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
5999 if (entry->ps.ycorrection_location != -1)
6000 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6002 else
6004 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6007 for (i = 0; i < MAX_TEXTURES; ++i)
6009 if (entry->ps.bumpenv_mat_location[i] != -1)
6011 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6012 break;
6016 if (entry->ps.np2_fixup_location != -1)
6017 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6021 /* Context activation is done by the caller. */
6022 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6024 GLhandleARB program_id;
6025 GLhandleARB vshader_id, pshader_id;
6026 const char *blt_pshader;
6028 static const char blt_vshader[] =
6029 "#version 120\n"
6030 "void main(void)\n"
6031 "{\n"
6032 " gl_Position = gl_Vertex;\n"
6033 " gl_FrontColor = vec4(1.0);\n"
6034 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6035 "}\n";
6037 static const char * const blt_pshaders_full[tex_type_count] =
6039 /* tex_1d */
6040 NULL,
6041 /* tex_2d */
6042 "#version 120\n"
6043 "uniform sampler2D sampler;\n"
6044 "void main(void)\n"
6045 "{\n"
6046 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6047 "}\n",
6048 /* tex_3d */
6049 NULL,
6050 /* tex_cube */
6051 "#version 120\n"
6052 "uniform samplerCube sampler;\n"
6053 "void main(void)\n"
6054 "{\n"
6055 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6056 "}\n",
6057 /* tex_rect */
6058 "#version 120\n"
6059 "#extension GL_ARB_texture_rectangle : enable\n"
6060 "uniform sampler2DRect sampler;\n"
6061 "void main(void)\n"
6062 "{\n"
6063 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6064 "}\n",
6067 static const char * const blt_pshaders_masked[tex_type_count] =
6069 /* tex_1d */
6070 NULL,
6071 /* tex_2d */
6072 "#version 120\n"
6073 "uniform sampler2D sampler;\n"
6074 "uniform vec4 mask;\n"
6075 "void main(void)\n"
6076 "{\n"
6077 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6078 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6079 "}\n",
6080 /* tex_3d */
6081 NULL,
6082 /* tex_cube */
6083 "#version 120\n"
6084 "uniform samplerCube sampler;\n"
6085 "uniform vec4 mask;\n"
6086 "void main(void)\n"
6087 "{\n"
6088 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6089 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6090 "}\n",
6091 /* tex_rect */
6092 "#version 120\n"
6093 "#extension GL_ARB_texture_rectangle : enable\n"
6094 "uniform sampler2DRect sampler;\n"
6095 "uniform vec4 mask;\n"
6096 "void main(void)\n"
6097 "{\n"
6098 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6099 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6100 "}\n",
6103 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6104 if (!blt_pshader)
6106 FIXME("tex_type %#x not supported\n", tex_type);
6107 return 0;
6110 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6111 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6113 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6114 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6116 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6117 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6118 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6119 GL_EXTCALL(glLinkProgramARB(program_id));
6121 shader_glsl_validate_link(gl_info, program_id);
6123 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6124 * is destroyed
6126 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6127 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6128 return program_id;
6131 /* Context activation is done by the caller. */
6132 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6133 const struct wined3d_state *state)
6135 struct glsl_context_data *ctx_data = context->shader_backend_data;
6136 const struct wined3d_gl_info *gl_info = context->gl_info;
6137 struct shader_glsl_priv *priv = shader_priv;
6138 GLhandleARB program_id = 0, prev_id = 0;
6139 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6141 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6142 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6144 if (ctx_data->glsl_program)
6146 prev_id = ctx_data->glsl_program->programId;
6147 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6149 else
6151 prev_id = 0;
6152 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6155 set_glsl_shader_program(context, state, priv, ctx_data);
6157 if (ctx_data->glsl_program)
6159 program_id = ctx_data->glsl_program->programId;
6160 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6162 else
6164 program_id = 0;
6165 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6168 if (old_vertex_color_clamp != current_vertex_color_clamp)
6170 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6172 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6173 checkGLcall("glClampColorARB");
6175 else
6177 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6181 TRACE("Using GLSL program %u.\n", program_id);
6183 if (prev_id != program_id)
6185 GL_EXTCALL(glUseProgramObjectARB(program_id));
6186 checkGLcall("glUseProgramObjectARB");
6188 if (program_id)
6189 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6193 /* "context" is not necessarily the currently active context. */
6194 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6196 struct glsl_context_data *ctx_data = context->shader_backend_data;
6198 ctx_data->glsl_program = NULL;
6199 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6200 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6201 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6204 /* Context activation is done by the caller. */
6205 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6207 const struct wined3d_gl_info *gl_info = context->gl_info;
6208 struct shader_glsl_priv *priv = shader_priv;
6210 shader_glsl_invalidate_current_program(context);
6211 GL_EXTCALL(glUseProgramObjectARB(0));
6212 checkGLcall("glUseProgramObjectARB");
6214 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6215 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6217 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6219 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6220 checkGLcall("glClampColorARB");
6224 /* Context activation is done by the caller. */
6225 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6226 enum tex_types tex_type, const SIZE *ds_mask_size)
6228 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6229 struct shader_glsl_priv *priv = shader_priv;
6230 GLhandleARB *blt_program;
6231 GLint loc;
6233 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6234 if (!*blt_program)
6236 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6237 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6238 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6239 GL_EXTCALL(glUniform1iARB(loc, 0));
6241 else
6243 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6246 if (masked)
6248 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6249 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6253 /* Context activation is done by the caller. */
6254 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6256 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6257 GLhandleARB program_id;
6259 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6260 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6262 GL_EXTCALL(glUseProgramObjectARB(program_id));
6263 checkGLcall("glUseProgramObjectARB");
6266 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6267 const struct glsl_shader_prog_link *program)
6269 const struct glsl_context_data *ctx_data;
6270 struct wined3d_context *context;
6271 unsigned int i;
6273 for (i = 0; i < device->context_count; ++i)
6275 context = device->contexts[i];
6276 ctx_data = context->shader_backend_data;
6278 if (ctx_data->glsl_program == program)
6279 shader_glsl_invalidate_current_program(context);
6283 static void shader_glsl_destroy(struct wined3d_shader *shader)
6285 struct glsl_shader_private *shader_data = shader->backend_data;
6286 struct wined3d_device *device = shader->device;
6287 struct shader_glsl_priv *priv = device->shader_priv;
6288 const struct wined3d_gl_info *gl_info;
6289 const struct list *linked_programs;
6290 struct wined3d_context *context;
6292 if (!shader_data || !shader_data->num_gl_shaders)
6294 HeapFree(GetProcessHeap(), 0, shader_data);
6295 shader->backend_data = NULL;
6296 return;
6299 context = context_acquire(device, NULL);
6300 gl_info = context->gl_info;
6302 TRACE("Deleting linked programs.\n");
6303 linked_programs = &shader->linked_programs;
6304 if (linked_programs->next)
6306 struct glsl_shader_prog_link *entry, *entry2;
6307 UINT i;
6309 switch (shader->reg_maps.shader_version.type)
6311 case WINED3D_SHADER_TYPE_PIXEL:
6313 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6315 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6317 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6318 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6319 checkGLcall("glDeleteObjectARB");
6321 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6323 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6324 struct glsl_shader_prog_link, ps.shader_entry)
6326 shader_glsl_invalidate_contexts_program(device, entry);
6327 delete_glsl_program_entry(priv, gl_info, entry);
6330 break;
6333 case WINED3D_SHADER_TYPE_VERTEX:
6335 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6337 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6339 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6340 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6341 checkGLcall("glDeleteObjectARB");
6343 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6345 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6346 struct glsl_shader_prog_link, vs.shader_entry)
6348 shader_glsl_invalidate_contexts_program(device, entry);
6349 delete_glsl_program_entry(priv, gl_info, entry);
6352 break;
6355 case WINED3D_SHADER_TYPE_GEOMETRY:
6357 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6359 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6361 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6362 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6363 checkGLcall("glDeleteObjectARB");
6365 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6367 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6368 struct glsl_shader_prog_link, gs.shader_entry)
6370 shader_glsl_invalidate_contexts_program(device, entry);
6371 delete_glsl_program_entry(priv, gl_info, entry);
6374 break;
6377 default:
6378 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6379 break;
6383 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6384 shader->backend_data = NULL;
6386 context_release(context);
6389 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6391 const struct glsl_program_key *k = key;
6392 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6393 const struct glsl_shader_prog_link, program_lookup_entry);
6395 if (k->vs_id > prog->vs.id) return 1;
6396 else if (k->vs_id < prog->vs.id) return -1;
6398 if (k->gs_id > prog->gs.id) return 1;
6399 else if (k->gs_id < prog->gs.id) return -1;
6401 if (k->ps_id > prog->ps.id) return 1;
6402 else if (k->ps_id < prog->ps.id) return -1;
6404 return 0;
6407 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6409 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6410 + constant_count * sizeof(*heap->contained)
6411 + constant_count * sizeof(*heap->positions);
6412 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6414 if (!mem)
6416 ERR("Failed to allocate memory\n");
6417 return FALSE;
6420 heap->entries = mem;
6421 heap->entries[1].version = 0;
6422 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6423 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6424 heap->positions = (unsigned int *)(heap->contained + constant_count);
6425 heap->size = 1;
6427 return TRUE;
6430 static void constant_heap_free(struct constant_heap *heap)
6432 HeapFree(GetProcessHeap(), 0, heap->entries);
6435 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6437 wined3d_rb_alloc,
6438 wined3d_rb_realloc,
6439 wined3d_rb_free,
6440 glsl_program_key_compare,
6443 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6444 const struct fragment_pipeline *fragment_pipe)
6446 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6447 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6448 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6449 gl_info->limits.glsl_ps_float_constants)) + 1;
6450 struct fragment_caps fragment_caps;
6451 void *vertex_priv, *fragment_priv;
6453 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6455 ERR("Failed to initialize vertex pipe.\n");
6456 HeapFree(GetProcessHeap(), 0, priv);
6457 return E_FAIL;
6460 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6462 ERR("Failed to initialize fragment pipe.\n");
6463 vertex_pipe->vp_free(device);
6464 HeapFree(GetProcessHeap(), 0, priv);
6465 return E_FAIL;
6468 if (!shader_buffer_init(&priv->shader_buffer))
6470 ERR("Failed to initialize shader buffer.\n");
6471 goto fail;
6474 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6475 if (!priv->stack)
6477 ERR("Failed to allocate memory.\n");
6478 goto fail;
6481 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6483 ERR("Failed to initialize vertex shader constant heap\n");
6484 goto fail;
6487 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6489 ERR("Failed to initialize pixel shader constant heap\n");
6490 goto fail;
6493 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6495 ERR("Failed to initialize rbtree.\n");
6496 goto fail;
6499 priv->next_constant_version = 1;
6500 priv->vertex_pipe = vertex_pipe;
6501 priv->fragment_pipe = fragment_pipe;
6502 fragment_pipe->get_caps(gl_info, &fragment_caps);
6503 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6505 device->vertex_priv = vertex_priv;
6506 device->fragment_priv = fragment_priv;
6507 device->shader_priv = priv;
6509 return WINED3D_OK;
6511 fail:
6512 constant_heap_free(&priv->pconst_heap);
6513 constant_heap_free(&priv->vconst_heap);
6514 HeapFree(GetProcessHeap(), 0, priv->stack);
6515 shader_buffer_free(&priv->shader_buffer);
6516 fragment_pipe->free_private(device);
6517 vertex_pipe->vp_free(device);
6518 HeapFree(GetProcessHeap(), 0, priv);
6519 return E_OUTOFMEMORY;
6522 /* Context activation is done by the caller. */
6523 static void shader_glsl_free(struct wined3d_device *device)
6525 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6526 struct shader_glsl_priv *priv = device->shader_priv;
6527 int i;
6529 for (i = 0; i < tex_type_count; ++i)
6531 if (priv->depth_blt_program_full[i])
6533 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6535 if (priv->depth_blt_program_masked[i])
6537 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6541 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6542 constant_heap_free(&priv->pconst_heap);
6543 constant_heap_free(&priv->vconst_heap);
6544 HeapFree(GetProcessHeap(), 0, priv->stack);
6545 shader_buffer_free(&priv->shader_buffer);
6546 priv->fragment_pipe->free_private(device);
6547 priv->vertex_pipe->vp_free(device);
6549 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6550 device->shader_priv = NULL;
6553 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6555 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6556 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6559 static void shader_glsl_free_context_data(struct wined3d_context *context)
6561 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6564 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6566 UINT shader_model;
6568 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6569 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6570 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6571 && gl_info->supported[ARB_TEXTURE_RG])
6572 shader_model = 4;
6573 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6574 * texldd and texldl instructions. */
6575 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6576 shader_model = 3;
6577 else
6578 shader_model = 2;
6579 TRACE("Shader model %u.\n", shader_model);
6581 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6582 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6583 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6585 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6586 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6588 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6589 * Direct3D minimum requirement.
6591 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6592 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6594 * The problem is that the refrast clamps temporary results in the shader to
6595 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6596 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6597 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6598 * offer a way to query this.
6600 if (shader_model >= 4)
6601 caps->ps_1x_max_value = FLT_MAX;
6602 else
6603 caps->ps_1x_max_value = 1024.0f;
6605 /* Ideally we'd only set caps like sRGB writes here if supported by both
6606 * the shader backend and the fragment pipe, but we can get called before
6607 * shader_glsl_alloc(). */
6608 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6609 | WINED3D_SHADER_CAP_SRGB_WRITE;
6612 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6614 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6616 TRACE("Checking support for fixup:\n");
6617 dump_color_fixup_desc(fixup);
6620 /* We support everything except YUV conversions. */
6621 if (!is_complex_fixup(fixup))
6623 TRACE("[OK]\n");
6624 return TRUE;
6627 TRACE("[FAILED]\n");
6628 return FALSE;
6631 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6633 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6634 /* WINED3DSIH_ADD */ shader_glsl_binop,
6635 /* WINED3DSIH_AND */ shader_glsl_binop,
6636 /* WINED3DSIH_BEM */ shader_glsl_bem,
6637 /* WINED3DSIH_BREAK */ shader_glsl_break,
6638 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6639 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6640 /* WINED3DSIH_CALL */ shader_glsl_call,
6641 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6642 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6643 /* WINED3DSIH_CND */ shader_glsl_cnd,
6644 /* WINED3DSIH_CRS */ shader_glsl_cross,
6645 /* WINED3DSIH_CUT */ shader_glsl_cut,
6646 /* WINED3DSIH_DCL */ shader_glsl_nop,
6647 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6648 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6649 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6650 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6651 /* WINED3DSIH_DEF */ shader_glsl_nop,
6652 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6653 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6654 /* WINED3DSIH_DIV */ shader_glsl_binop,
6655 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6656 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6657 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6658 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6659 /* WINED3DSIH_DST */ shader_glsl_dst,
6660 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6661 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6662 /* WINED3DSIH_ELSE */ shader_glsl_else,
6663 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6664 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6665 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6666 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6667 /* WINED3DSIH_EQ */ shader_glsl_relop,
6668 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6669 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6670 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6671 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6672 /* WINED3DSIH_GE */ shader_glsl_relop,
6673 /* WINED3DSIH_IADD */ shader_glsl_binop,
6674 /* WINED3DSIH_IEQ */ NULL,
6675 /* WINED3DSIH_IF */ shader_glsl_if,
6676 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6677 /* WINED3DSIH_IGE */ shader_glsl_relop,
6678 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6679 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6680 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6681 /* WINED3DSIH_LABEL */ shader_glsl_label,
6682 /* WINED3DSIH_LD */ NULL,
6683 /* WINED3DSIH_LIT */ shader_glsl_lit,
6684 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6685 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6686 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6687 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6688 /* WINED3DSIH_LT */ shader_glsl_relop,
6689 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6690 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6691 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6692 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6693 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6694 /* WINED3DSIH_MAD */ shader_glsl_mad,
6695 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6696 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6697 /* WINED3DSIH_MOV */ shader_glsl_mov,
6698 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6699 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6700 /* WINED3DSIH_MUL */ shader_glsl_binop,
6701 /* WINED3DSIH_NOP */ shader_glsl_nop,
6702 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6703 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6704 /* WINED3DSIH_POW */ shader_glsl_pow,
6705 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6706 /* WINED3DSIH_REP */ shader_glsl_rep,
6707 /* WINED3DSIH_RET */ shader_glsl_ret,
6708 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6709 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6710 /* WINED3DSIH_SAMPLE */ NULL,
6711 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6712 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6713 /* WINED3DSIH_SETP */ NULL,
6714 /* WINED3DSIH_SGE */ shader_glsl_compare,
6715 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6716 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6717 /* WINED3DSIH_SLT */ shader_glsl_compare,
6718 /* WINED3DSIH_SQRT */ NULL,
6719 /* WINED3DSIH_SUB */ shader_glsl_binop,
6720 /* WINED3DSIH_TEX */ shader_glsl_tex,
6721 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6722 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6723 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6724 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6725 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6726 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6727 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6728 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6729 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6730 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6731 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6732 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6733 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6734 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6735 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6736 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6737 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6738 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6739 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6740 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6741 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6742 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6743 /* WINED3DSIH_USHR */ shader_glsl_binop,
6744 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6745 /* WINED3DSIH_XOR */ shader_glsl_binop,
6748 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6749 SHADER_HANDLER hw_fct;
6751 /* Select handler */
6752 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6754 /* Unhandled opcode */
6755 if (!hw_fct)
6757 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6758 return;
6760 hw_fct(ins);
6762 shader_glsl_add_instruction_modifiers(ins);
6765 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6767 struct shader_glsl_priv *priv = shader_priv;
6769 return priv->ffp_proj_control;
6772 const struct wined3d_shader_backend_ops glsl_shader_backend =
6774 shader_glsl_handle_instruction,
6775 shader_glsl_select,
6776 shader_glsl_disable,
6777 shader_glsl_select_depth_blt,
6778 shader_glsl_deselect_depth_blt,
6779 shader_glsl_update_float_vertex_constants,
6780 shader_glsl_update_float_pixel_constants,
6781 shader_glsl_load_constants,
6782 shader_glsl_destroy,
6783 shader_glsl_alloc,
6784 shader_glsl_free,
6785 shader_glsl_allocate_context_data,
6786 shader_glsl_free_context_data,
6787 shader_glsl_get_caps,
6788 shader_glsl_color_fixup_supported,
6789 shader_glsl_has_ffp_proj_control,
6792 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6794 if (enable)
6795 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6796 else
6797 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6798 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6801 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6803 caps->xyzrhw = TRUE;
6804 caps->max_active_lights = gl_info->limits.lights;
6805 caps->max_vertex_blend_matrices = 1;
6806 caps->max_vertex_blend_matrix_index = 0;
6807 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6808 | WINED3DVTXPCAPS_MATERIALSOURCE7
6809 | WINED3DVTXPCAPS_VERTEXFOG
6810 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6811 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6812 | WINED3DVTXPCAPS_LOCALVIEWER
6813 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6814 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6815 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6816 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6819 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6821 struct shader_glsl_priv *priv;
6823 if (shader_backend == &glsl_shader_backend)
6825 priv = shader_priv;
6827 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6829 ERR("Failed to initialize rbtree.\n");
6830 return NULL;
6833 return priv;
6836 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6838 return NULL;
6841 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6843 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6844 struct glsl_ffp_vertex_shader, desc.entry);
6845 struct glsl_shader_prog_link *program, *program2;
6846 struct glsl_ffp_destroy_ctx *ctx = context;
6848 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6849 struct glsl_shader_prog_link, vs.shader_entry)
6851 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6853 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6854 HeapFree(GetProcessHeap(), 0, shader);
6857 /* Context activation is done by the caller. */
6858 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6860 struct shader_glsl_priv *priv = device->vertex_priv;
6861 struct glsl_ffp_destroy_ctx ctx;
6863 ctx.priv = priv;
6864 ctx.gl_info = &device->adapter->gl_info;
6865 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6868 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6869 const struct wined3d_state *state, DWORD state_id)
6871 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6874 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6875 const struct wined3d_state *state, DWORD state_id)
6877 /* Table fog behavior depends on the projection matrix. */
6878 if (state->render_states[WINED3D_RS_FOGENABLE]
6879 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6880 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6881 transform_projection(context, state, state_id);
6884 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6886 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6887 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6888 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6889 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6890 /* Clip planes */
6891 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6892 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6893 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6894 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6895 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6896 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6897 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6898 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6899 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6900 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6901 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6902 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6903 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6904 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6905 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6906 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6907 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6908 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6909 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6910 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6911 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6912 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6913 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6914 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6915 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6916 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6917 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6918 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6919 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6920 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6921 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6922 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6923 /* Lights */
6924 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6925 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6926 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6927 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6928 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6929 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6930 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6931 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6932 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6933 /* Viewport */
6934 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6935 /* Transform states */
6936 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6937 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6938 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6939 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6940 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6941 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6942 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6943 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6944 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6945 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6946 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6947 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6948 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6949 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6950 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6951 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6952 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6953 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6954 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6955 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6956 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6957 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6958 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6959 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6960 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6961 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6962 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6963 /* Fog */
6964 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6965 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6966 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6967 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6968 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6969 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6970 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6971 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6972 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6973 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6974 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6975 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6976 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6977 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6978 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6979 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6980 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6981 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
6982 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
6983 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
6984 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
6985 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
6986 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
6987 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6988 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6989 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6990 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
6991 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
6992 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
6993 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6994 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6995 /* Samplers for NP2 texture matrix adjustions. They are not needed if
6996 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
6997 * handler in that case to get the vertex part of sampler() skipped (VTF
6998 * is handled in the misc states). Otherwise, register
6999 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7000 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7001 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7002 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7003 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7004 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7005 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7006 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7007 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7008 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7009 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7010 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7011 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7012 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7013 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7014 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7015 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7016 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7017 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7018 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7019 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7020 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7021 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7022 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7023 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7024 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7025 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7028 /* TODO:
7029 * - This currently depends on GL fixed function functions to set things
7030 * like light parameters. Ideally we'd use regular uniforms for that.
7031 * - In part because of the previous point, much of this is modelled after
7032 * GL fixed function, and has much of the same limitations. For example,
7033 * D3D spot lights are slightly different from GL spot lights.
7034 * - We can now implement drawing transformed vertices using the GLSL pipe,
7035 * instead of using the immediate mode fallback.
7036 * - Similarly, we don't need the fallback for certain combinations of
7037 * material sources anymore.
7038 * - Implement vertex blending and vertex tweening.
7039 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7040 * attribute arrays in load_tex_coords().
7041 * - Per-vertex point sizes. */
7042 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7044 glsl_vertex_pipe_vp_enable,
7045 glsl_vertex_pipe_vp_get_caps,
7046 glsl_vertex_pipe_vp_alloc,
7047 glsl_vertex_pipe_vp_free,
7048 glsl_vertex_pipe_vp_states,
7051 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7053 /* Nothing to do. */
7056 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7058 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7059 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7060 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7061 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7062 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7063 | WINED3DTEXOPCAPS_SELECTARG1
7064 | WINED3DTEXOPCAPS_SELECTARG2
7065 | WINED3DTEXOPCAPS_MODULATE4X
7066 | WINED3DTEXOPCAPS_MODULATE2X
7067 | WINED3DTEXOPCAPS_MODULATE
7068 | WINED3DTEXOPCAPS_ADDSIGNED2X
7069 | WINED3DTEXOPCAPS_ADDSIGNED
7070 | WINED3DTEXOPCAPS_ADD
7071 | WINED3DTEXOPCAPS_SUBTRACT
7072 | WINED3DTEXOPCAPS_ADDSMOOTH
7073 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7074 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7075 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7076 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7077 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7078 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7079 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7080 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7081 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7082 | WINED3DTEXOPCAPS_DOTPRODUCT3
7083 | WINED3DTEXOPCAPS_MULTIPLYADD
7084 | WINED3DTEXOPCAPS_LERP
7085 | WINED3DTEXOPCAPS_BUMPENVMAP
7086 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7087 caps->MaxTextureBlendStages = 8;
7088 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7091 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7093 struct shader_glsl_priv *priv;
7095 if (shader_backend == &glsl_shader_backend)
7097 priv = shader_priv;
7099 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7101 ERR("Failed to initialize rbtree.\n");
7102 return NULL;
7105 return priv;
7108 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7110 return NULL;
7113 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7115 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7116 struct glsl_ffp_fragment_shader, entry.entry);
7117 struct glsl_shader_prog_link *program, *program2;
7118 struct glsl_ffp_destroy_ctx *ctx = context;
7120 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7121 struct glsl_shader_prog_link, ps.shader_entry)
7123 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7125 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7126 HeapFree(GetProcessHeap(), 0, shader);
7129 /* Context activation is done by the caller. */
7130 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7132 struct shader_glsl_priv *priv = device->fragment_priv;
7133 struct glsl_ffp_destroy_ctx ctx;
7135 ctx.priv = priv;
7136 ctx.gl_info = &device->adapter->gl_info;
7137 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7140 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7141 const struct wined3d_state *state, DWORD state_id)
7143 context->last_was_pshader = use_ps(state);
7145 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7148 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7149 const struct wined3d_state *state, DWORD state_id)
7151 BOOL use_vshader = use_vs(state);
7152 enum fogsource new_source;
7153 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7154 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7156 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7158 if (!state->render_states[WINED3D_RS_FOGENABLE])
7159 return;
7161 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7163 if (use_vshader)
7164 new_source = FOGSOURCE_VS;
7165 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7166 new_source = FOGSOURCE_COORD;
7167 else
7168 new_source = FOGSOURCE_FFP;
7170 else
7172 new_source = FOGSOURCE_FFP;
7175 if (new_source != context->fog_source || fogstart == fogend)
7177 context->fog_source = new_source;
7178 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7182 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7183 const struct wined3d_state *state, DWORD state_id)
7185 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7188 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7189 const struct wined3d_state *state, DWORD state_id)
7191 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7194 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7196 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7197 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7198 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7199 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7200 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7201 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7202 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7203 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7204 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7205 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7206 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7207 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7208 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7209 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7210 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7211 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7212 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7213 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7214 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7215 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7216 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7217 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7218 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7219 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7220 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7221 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7223 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7225 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7226 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7227 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7228 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7229 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7230 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7231 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7232 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7233 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7234 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7250 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7252 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7253 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7254 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7255 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7256 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7257 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7259 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7260 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7261 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7263 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7264 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7265 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7266 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7267 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7268 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7269 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7270 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7271 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7274 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7276 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7278 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7279 {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 },
7280 {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 },
7281 {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 },
7282 {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 },
7283 {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 },
7284 {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 },
7285 {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 },
7286 {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 },
7287 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7295 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7296 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7299 const struct fragment_pipeline glsl_fragment_pipe =
7301 glsl_fragment_pipe_enable,
7302 glsl_fragment_pipe_get_caps,
7303 glsl_fragment_pipe_alloc,
7304 glsl_fragment_pipe_free,
7305 shader_glsl_color_fixup_supported,
7306 glsl_fragment_pipe_state_template,