wined3d: Use rendertarget views for color output instead of surfaces.
[wine.git] / dlls / wined3d / glsl_shader.c
blobbebcfd43caa551886a920856ca52d4a4cbae36f3
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, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1087 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1090 /* Declare texture samplers */
1091 for (i = 0; i < shader->limits.sampler; ++i)
1093 if (reg_maps->sampler_type[i])
1095 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1096 BOOL tex_rect;
1098 switch (reg_maps->sampler_type[i])
1100 case WINED3DSTT_1D:
1101 if (shadow_sampler)
1102 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1103 else
1104 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1105 break;
1106 case WINED3DSTT_2D:
1107 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1108 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1109 if (shadow_sampler)
1111 if (tex_rect)
1112 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1113 else
1114 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1116 else
1118 if (tex_rect)
1119 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1120 else
1121 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1123 break;
1124 case WINED3DSTT_CUBE:
1125 if (shadow_sampler)
1126 FIXME("Unsupported Cube shadow sampler.\n");
1127 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1128 break;
1129 case WINED3DSTT_VOLUME:
1130 if (shadow_sampler)
1131 FIXME("Unsupported 3D shadow sampler.\n");
1132 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1133 break;
1134 default:
1135 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1136 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1137 break;
1142 /* Declare uniforms for NP2 texcoord fixup:
1143 * This is NOT done inside the loop that declares the texture samplers
1144 * since the NP2 fixup code is currently only used for the GeforceFX
1145 * series and when forcing the ARB_npot extension off. Modern cards just
1146 * skip the code anyway, so put it inside a separate loop. */
1147 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1149 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1150 UINT cur = 0;
1152 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1153 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1154 * samplerNP2Fixup stores texture dimensions and is updated through
1155 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1157 for (i = 0; i < shader->limits.sampler; ++i)
1159 if (reg_maps->sampler_type[i])
1161 if (!(ps_args->np2_fixup & (1 << i))) continue;
1163 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1164 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1165 continue;
1168 fixup->idx[i] = cur++;
1172 fixup->num_consts = (cur + 1) >> 1;
1173 fixup->active = ps_args->np2_fixup;
1174 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1177 /* Declare address variables */
1178 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1180 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1183 /* Declare texture coordinate temporaries and initialize them */
1184 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1186 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1189 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1191 /* Declare attributes. */
1192 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1194 if (map & 1)
1195 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1198 shader_addline(buffer, "uniform vec4 posFixup;\n");
1199 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1201 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1203 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1205 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1207 if (version->major >= 3)
1209 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1211 if (use_vs(state))
1212 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1213 else
1214 /* TODO: Write a replacement shader for the fixed function
1215 * vertex pipeline, so this isn't needed. For fixed function
1216 * vertex processing + 3.0 pixel shader we need a separate
1217 * function in the pixel shader that reads the fixed function
1218 * color into the packed input registers. */
1219 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1222 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1224 if (!(map & 1))
1225 continue;
1227 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1229 if (reg_maps->luminanceparams & (1 << i))
1231 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1232 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1233 extra_constants_needed++;
1236 extra_constants_needed++;
1239 if (ps_args->srgb_correction)
1241 shader_addline(buffer, "const vec4 srgb_const0 = ");
1242 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1243 shader_addline(buffer, ";\n");
1244 shader_addline(buffer, "const vec4 srgb_const1 = ");
1245 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1246 shader_addline(buffer, ";\n");
1248 if (reg_maps->vpos || reg_maps->usesdsy)
1250 if (shader->limits.constant_float + extra_constants_needed
1251 + 1 < gl_info->limits.glsl_ps_float_constants)
1253 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1254 extra_constants_needed++;
1256 else
1258 float ycorrection[] =
1260 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1261 context->render_offscreen ? 1.0f : -1.0f,
1262 0.0f,
1263 0.0f,
1266 /* This happens because we do not have proper tracking of the
1267 * constant registers that are actually used, only the max
1268 * limit of the shader version. */
1269 FIXME("Cannot find a free uniform for vpos correction params\n");
1270 shader_addline(buffer, "const vec4 ycorrection = ");
1271 shader_glsl_append_imm_vec4(buffer, ycorrection);
1272 shader_addline(buffer, ";\n");
1274 shader_addline(buffer, "vec4 vpos;\n");
1278 /* Declare output register temporaries */
1279 if (shader->limits.packed_output)
1280 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1282 /* Declare temporary variables */
1283 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1285 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1288 /* Declare loop registers aLx */
1289 if (version->major < 4)
1291 for (i = 0; i < reg_maps->loop_depth; ++i)
1293 shader_addline(buffer, "int aL%u;\n", i);
1294 shader_addline(buffer, "int tmpInt%u;\n", i);
1298 /* Temporary variables for matrix operations */
1299 shader_addline(buffer, "vec4 tmp0;\n");
1300 shader_addline(buffer, "vec4 tmp1;\n");
1302 if (!shader->load_local_constsF)
1304 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1306 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1307 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1308 shader_addline(buffer, ";\n");
1312 /* Start the main program. */
1313 shader_addline(buffer, "void main()\n{\n");
1315 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1316 * add approximately 0.5. This causes off-by-one problems as spotted by
1317 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1318 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1319 * causes precision troubles when we just subtract 0.5.
1321 * To deal with that, just floor() the position. This will eliminate the
1322 * fraction on all cards.
1324 * TODO: Test how this behaves with multisampling.
1326 * An advantage of floor is that it works even if the driver doesn't add
1327 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1328 * to return in gl_FragCoord, even though coordinates specify the pixel
1329 * centers instead of the pixel corners. This code will behave correctly
1330 * on drivers that returns integer values. */
1331 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1332 shader_addline(buffer,
1333 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1336 /*****************************************************************************
1337 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1339 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1340 ****************************************************************************/
1342 /* Prototypes */
1343 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1344 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1346 /** Used for opcode modifiers - They multiply the result by the specified amount */
1347 static const char * const shift_glsl_tab[] = {
1348 "", /* 0 (none) */
1349 "2.0 * ", /* 1 (x2) */
1350 "4.0 * ", /* 2 (x4) */
1351 "8.0 * ", /* 3 (x8) */
1352 "16.0 * ", /* 4 (x16) */
1353 "32.0 * ", /* 5 (x32) */
1354 "", /* 6 (x64) */
1355 "", /* 7 (x128) */
1356 "", /* 8 (d256) */
1357 "", /* 9 (d128) */
1358 "", /* 10 (d64) */
1359 "", /* 11 (d32) */
1360 "0.0625 * ", /* 12 (d16) */
1361 "0.125 * ", /* 13 (d8) */
1362 "0.25 * ", /* 14 (d4) */
1363 "0.5 * " /* 15 (d2) */
1366 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1367 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1368 const char *in_reg, const char *in_regswizzle, char *out_str)
1370 out_str[0] = 0;
1372 switch (src_modifier)
1374 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1375 case WINED3DSPSM_DW:
1376 case WINED3DSPSM_NONE:
1377 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1378 break;
1379 case WINED3DSPSM_NEG:
1380 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1381 break;
1382 case WINED3DSPSM_NOT:
1383 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1384 break;
1385 case WINED3DSPSM_BIAS:
1386 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1387 break;
1388 case WINED3DSPSM_BIASNEG:
1389 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1390 break;
1391 case WINED3DSPSM_SIGN:
1392 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1393 break;
1394 case WINED3DSPSM_SIGNNEG:
1395 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1396 break;
1397 case WINED3DSPSM_COMP:
1398 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1399 break;
1400 case WINED3DSPSM_X2:
1401 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1402 break;
1403 case WINED3DSPSM_X2NEG:
1404 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1405 break;
1406 case WINED3DSPSM_ABS:
1407 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1408 break;
1409 case WINED3DSPSM_ABSNEG:
1410 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1411 break;
1412 default:
1413 FIXME("Unhandled modifier %u\n", src_modifier);
1414 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1418 /** Writes the GLSL variable name that corresponds to the register that the
1419 * DX opcode parameter is trying to access */
1420 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1421 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1423 /* oPos, oFog and oPts in D3D */
1424 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1426 const struct wined3d_shader *shader = ins->ctx->shader;
1427 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1428 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1429 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1430 const char *prefix = shader_glsl_get_prefix(version->type);
1431 struct glsl_src_param rel_param0, rel_param1;
1432 char imm_str[4][17];
1434 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1435 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1436 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1437 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1438 *is_color = FALSE;
1440 switch (reg->type)
1442 case WINED3DSPR_TEMP:
1443 sprintf(register_name, "R%u", reg->idx[0].offset);
1444 break;
1446 case WINED3DSPR_INPUT:
1447 /* vertex shaders */
1448 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1450 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1451 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1452 *is_color = TRUE;
1453 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1454 break;
1457 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1459 if (reg->idx[0].rel_addr)
1461 if (reg->idx[1].rel_addr)
1462 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1463 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1464 else
1465 sprintf(register_name, "gs_in[%s + %u][%u]",
1466 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1468 else if (reg->idx[1].rel_addr)
1469 sprintf(register_name, "gs_in[%u][%s + %u]",
1470 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1471 else
1472 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1473 break;
1476 /* pixel shaders >= 3.0 */
1477 if (version->major >= 3)
1479 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1480 unsigned int in_count = vec4_varyings(version->major, gl_info);
1482 if (reg->idx[0].rel_addr)
1484 /* Removing a + 0 would be an obvious optimization, but
1485 * OS X doesn't see the NOP operation there. */
1486 if (idx)
1488 if (shader->u.ps.declared_in_count > in_count)
1490 sprintf(register_name,
1491 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1492 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1493 prefix, rel_param0.param_str, idx);
1495 else
1497 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1500 else
1502 if (shader->u.ps.declared_in_count > in_count)
1504 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1505 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1506 prefix, rel_param0.param_str);
1508 else
1510 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1514 else
1516 if (idx == in_count) sprintf(register_name, "gl_Color");
1517 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1518 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1521 else
1523 if (!reg->idx[0].offset)
1524 strcpy(register_name, "gl_Color");
1525 else
1526 strcpy(register_name, "gl_SecondaryColor");
1527 break;
1529 break;
1531 case WINED3DSPR_CONST:
1533 /* Relative addressing */
1534 if (reg->idx[0].rel_addr)
1536 if (reg->idx[0].offset)
1537 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1538 else
1539 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1541 else
1543 if (shader_constant_is_local(shader, reg->idx[0].offset))
1544 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1545 else
1546 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1549 break;
1551 case WINED3DSPR_CONSTINT:
1552 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1553 break;
1555 case WINED3DSPR_CONSTBOOL:
1556 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1557 break;
1559 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1560 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1561 sprintf(register_name, "T%u", reg->idx[0].offset);
1562 else
1563 sprintf(register_name, "A%u", reg->idx[0].offset);
1564 break;
1566 case WINED3DSPR_LOOP:
1567 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1568 break;
1570 case WINED3DSPR_SAMPLER:
1571 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1572 break;
1574 case WINED3DSPR_COLOROUT:
1575 if (reg->idx[0].offset >= gl_info->limits.buffers)
1576 WARN("Write to render target %u, only %d supported.\n",
1577 reg->idx[0].offset, gl_info->limits.buffers);
1579 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1580 break;
1582 case WINED3DSPR_RASTOUT:
1583 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1584 break;
1586 case WINED3DSPR_DEPTHOUT:
1587 sprintf(register_name, "gl_FragDepth");
1588 break;
1590 case WINED3DSPR_ATTROUT:
1591 if (!reg->idx[0].offset)
1592 sprintf(register_name, "%s_out[8]", prefix);
1593 else
1594 sprintf(register_name, "%s_out[9]", prefix);
1595 break;
1597 case WINED3DSPR_TEXCRDOUT:
1598 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1599 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1600 break;
1602 case WINED3DSPR_MISCTYPE:
1603 if (!reg->idx[0].offset)
1605 /* vPos */
1606 sprintf(register_name, "vpos");
1608 else if (reg->idx[0].offset == 1)
1610 /* Note that gl_FrontFacing is a bool, while vFace is
1611 * a float for which the sign determines front/back */
1612 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1614 else
1616 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1617 sprintf(register_name, "unrecognized_register");
1619 break;
1621 case WINED3DSPR_IMMCONST:
1622 switch (reg->immconst_type)
1624 case WINED3D_IMMCONST_SCALAR:
1625 switch (reg->data_type)
1627 case WINED3D_DATA_FLOAT:
1628 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1629 break;
1630 case WINED3D_DATA_INT:
1631 sprintf(register_name, "%#x", reg->immconst_data[0]);
1632 break;
1633 case WINED3D_DATA_RESOURCE:
1634 case WINED3D_DATA_SAMPLER:
1635 case WINED3D_DATA_UINT:
1636 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1637 break;
1638 default:
1639 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1640 break;
1642 break;
1644 case WINED3D_IMMCONST_VEC4:
1645 switch (reg->data_type)
1647 case WINED3D_DATA_FLOAT:
1648 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1649 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1650 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1651 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1652 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1653 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1654 break;
1655 case WINED3D_DATA_INT:
1656 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1657 reg->immconst_data[0], reg->immconst_data[1],
1658 reg->immconst_data[2], reg->immconst_data[3]);
1659 break;
1660 case WINED3D_DATA_RESOURCE:
1661 case WINED3D_DATA_SAMPLER:
1662 case WINED3D_DATA_UINT:
1663 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1664 reg->immconst_data[0], reg->immconst_data[1],
1665 reg->immconst_data[2], reg->immconst_data[3]);
1666 break;
1667 default:
1668 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1669 break;
1671 break;
1673 default:
1674 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1675 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1677 break;
1679 case WINED3DSPR_CONSTBUFFER:
1680 if (reg->idx[1].rel_addr)
1681 sprintf(register_name, "%s_cb%u[%s + %u]",
1682 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1683 else
1684 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1685 break;
1687 case WINED3DSPR_PRIMID:
1688 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1689 break;
1691 default:
1692 FIXME("Unhandled register type %#x.\n", reg->type);
1693 sprintf(register_name, "unrecognized_register");
1694 break;
1698 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1700 *str++ = '.';
1701 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1702 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1703 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1704 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1705 *str = '\0';
1708 /* Get the GLSL write mask for the destination register */
1709 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1711 DWORD mask = param->write_mask;
1713 if (shader_is_scalar(&param->reg))
1715 mask = WINED3DSP_WRITEMASK_0;
1716 *write_mask = '\0';
1718 else
1720 shader_glsl_write_mask_to_str(mask, write_mask);
1723 return mask;
1726 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1727 unsigned int size = 0;
1729 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1730 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1731 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1732 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1734 return size;
1737 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1739 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1740 * but addressed as "rgba". To fix this we need to swap the register's x
1741 * and z components. */
1742 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1744 *str++ = '.';
1745 /* swizzle bits fields: wwzzyyxx */
1746 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1747 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1748 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1749 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1750 *str = '\0';
1753 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1754 BOOL fixup, DWORD mask, char *swizzle_str)
1756 if (shader_is_scalar(&param->reg))
1757 *swizzle_str = '\0';
1758 else
1759 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1762 /* From a given parameter token, generate the corresponding GLSL string.
1763 * Also, return the actual register name and swizzle in case the
1764 * caller needs this information as well. */
1765 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1766 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1768 BOOL is_color = FALSE;
1769 char swizzle_str[6];
1771 glsl_src->reg_name[0] = '\0';
1772 glsl_src->param_str[0] = '\0';
1773 swizzle_str[0] = '\0';
1775 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1776 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1778 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1780 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1782 else
1784 char param_str[200];
1786 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1788 switch (wined3d_src->reg.data_type)
1790 case WINED3D_DATA_FLOAT:
1791 sprintf(glsl_src->param_str, "%s", param_str);
1792 break;
1793 case WINED3D_DATA_INT:
1794 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1795 break;
1796 case WINED3D_DATA_RESOURCE:
1797 case WINED3D_DATA_SAMPLER:
1798 case WINED3D_DATA_UINT:
1799 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1800 break;
1801 default:
1802 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1803 sprintf(glsl_src->param_str, "%s", param_str);
1804 break;
1809 /* From a given parameter token, generate the corresponding GLSL string.
1810 * Also, return the actual register name and swizzle in case the
1811 * caller needs this information as well. */
1812 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1813 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1815 BOOL is_color = FALSE;
1817 glsl_dst->mask_str[0] = '\0';
1818 glsl_dst->reg_name[0] = '\0';
1820 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1821 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1824 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1825 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1826 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1828 struct glsl_dst_param glsl_dst;
1829 DWORD mask;
1831 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1833 switch (dst->reg.data_type)
1835 case WINED3D_DATA_FLOAT:
1836 shader_addline(buffer, "%s%s = %s(",
1837 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1838 break;
1839 case WINED3D_DATA_INT:
1840 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1841 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1842 break;
1843 case WINED3D_DATA_RESOURCE:
1844 case WINED3D_DATA_SAMPLER:
1845 case WINED3D_DATA_UINT:
1846 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1847 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1848 break;
1849 default:
1850 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1851 shader_addline(buffer, "%s%s = %s(",
1852 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1853 break;
1857 return mask;
1860 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1861 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1863 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1866 /** Process GLSL instruction modifiers */
1867 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1869 struct glsl_dst_param dst_param;
1870 DWORD modifiers;
1872 if (!ins->dst_count) return;
1874 modifiers = ins->dst[0].modifiers;
1875 if (!modifiers) return;
1877 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1879 if (modifiers & WINED3DSPDM_SATURATE)
1881 /* _SAT means to clamp the value of the register to between 0 and 1 */
1882 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1883 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1886 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1888 FIXME("_centroid modifier not handled\n");
1891 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1893 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1897 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1899 switch (op)
1901 case WINED3D_SHADER_REL_OP_GT: return ">";
1902 case WINED3D_SHADER_REL_OP_EQ: return "==";
1903 case WINED3D_SHADER_REL_OP_GE: return ">=";
1904 case WINED3D_SHADER_REL_OP_LT: return "<";
1905 case WINED3D_SHADER_REL_OP_NE: return "!=";
1906 case WINED3D_SHADER_REL_OP_LE: return "<=";
1907 default:
1908 FIXME("Unrecognized operator %#x.\n", op);
1909 return "(\?\?)";
1913 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1914 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1916 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1917 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1918 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1919 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1920 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1921 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1922 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1923 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1925 /* Note that there's no such thing as a projected cube texture. */
1926 switch(sampler_type) {
1927 case WINED3DSTT_1D:
1928 if (shadow)
1930 if (lod)
1932 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1934 else if (grad)
1936 if (gl_info->supported[EXT_GPU_SHADER4])
1937 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1938 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1939 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1940 else
1942 FIXME("Unsupported 1D shadow grad function.\n");
1943 sample_function->name = "unsupported1DGrad";
1946 else
1948 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1950 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1952 else
1954 if (lod)
1956 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1958 else if (grad)
1960 if (gl_info->supported[EXT_GPU_SHADER4])
1961 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1962 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1963 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1964 else
1966 FIXME("Unsupported 1D grad function.\n");
1967 sample_function->name = "unsupported1DGrad";
1970 else
1972 sample_function->name = projected ? "texture1DProj" : "texture1D";
1974 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1976 break;
1978 case WINED3DSTT_2D:
1979 if (shadow)
1981 if (texrect)
1983 if (lod)
1985 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1987 else if (grad)
1989 if (gl_info->supported[EXT_GPU_SHADER4])
1990 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1991 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1992 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1993 else
1995 FIXME("Unsupported RECT shadow grad function.\n");
1996 sample_function->name = "unsupported2DRectGrad";
1999 else
2001 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2004 else
2006 if (lod)
2008 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2010 else if (grad)
2012 if (gl_info->supported[EXT_GPU_SHADER4])
2013 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2014 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2015 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2016 else
2018 FIXME("Unsupported 2D shadow grad function.\n");
2019 sample_function->name = "unsupported2DGrad";
2022 else
2024 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2027 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2029 else
2031 if (texrect)
2033 if (lod)
2035 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2037 else if (grad)
2039 if (gl_info->supported[EXT_GPU_SHADER4])
2040 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2041 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2042 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2043 else
2045 FIXME("Unsupported RECT grad function.\n");
2046 sample_function->name = "unsupported2DRectGrad";
2049 else
2051 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2054 else
2056 if (lod)
2058 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2060 else if (grad)
2062 if (gl_info->supported[EXT_GPU_SHADER4])
2063 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2064 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2065 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2066 else
2068 FIXME("Unsupported 2D grad function.\n");
2069 sample_function->name = "unsupported2DGrad";
2072 else
2074 sample_function->name = projected ? "texture2DProj" : "texture2D";
2077 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2079 break;
2081 case WINED3DSTT_CUBE:
2082 if (shadow)
2084 FIXME("Unsupported Cube shadow function.\n");
2085 sample_function->name = "unsupportedCubeShadow";
2086 sample_function->coord_mask = 0;
2088 else
2090 if (lod)
2092 sample_function->name = "textureCubeLod";
2094 else if (grad)
2096 if (gl_info->supported[EXT_GPU_SHADER4])
2097 sample_function->name = "textureCubeGrad";
2098 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2099 sample_function->name = "textureCubeGradARB";
2100 else
2102 FIXME("Unsupported Cube grad function.\n");
2103 sample_function->name = "unsupportedCubeGrad";
2106 else
2108 sample_function->name = "textureCube";
2110 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2112 break;
2114 case WINED3DSTT_VOLUME:
2115 if (shadow)
2117 FIXME("Unsupported 3D shadow function.\n");
2118 sample_function->name = "unsupported3DShadow";
2119 sample_function->coord_mask = 0;
2121 else
2123 if (lod)
2125 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2127 else if (grad)
2129 if (gl_info->supported[EXT_GPU_SHADER4])
2130 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2131 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2132 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2133 else
2135 FIXME("Unsupported 3D grad function.\n");
2136 sample_function->name = "unsupported3DGrad";
2139 else
2141 sample_function->name = projected ? "texture3DProj" : "texture3D";
2143 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2145 break;
2147 default:
2148 sample_function->name = "";
2149 sample_function->coord_mask = 0;
2150 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2151 break;
2155 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2156 BOOL sign_fixup, enum fixup_channel_source channel_source)
2158 switch(channel_source)
2160 case CHANNEL_SOURCE_ZERO:
2161 strcat(arguments, "0.0");
2162 break;
2164 case CHANNEL_SOURCE_ONE:
2165 strcat(arguments, "1.0");
2166 break;
2168 case CHANNEL_SOURCE_X:
2169 strcat(arguments, reg_name);
2170 strcat(arguments, ".x");
2171 break;
2173 case CHANNEL_SOURCE_Y:
2174 strcat(arguments, reg_name);
2175 strcat(arguments, ".y");
2176 break;
2178 case CHANNEL_SOURCE_Z:
2179 strcat(arguments, reg_name);
2180 strcat(arguments, ".z");
2181 break;
2183 case CHANNEL_SOURCE_W:
2184 strcat(arguments, reg_name);
2185 strcat(arguments, ".w");
2186 break;
2188 default:
2189 FIXME("Unhandled channel source %#x\n", channel_source);
2190 strcat(arguments, "undefined");
2191 break;
2194 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2197 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2198 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2200 unsigned int mask_size, remaining;
2201 DWORD fixup_mask = 0;
2202 char arguments[256];
2203 char mask_str[6];
2205 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2206 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2207 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2208 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2209 if (!(mask &= fixup_mask))
2210 return;
2212 if (is_complex_fixup(fixup))
2214 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2215 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2216 return;
2219 shader_glsl_write_mask_to_str(mask, mask_str);
2220 mask_size = shader_glsl_get_write_mask_size(mask);
2222 arguments[0] = '\0';
2223 remaining = mask_size;
2224 if (mask & WINED3DSP_WRITEMASK_0)
2226 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2227 if (--remaining) strcat(arguments, ", ");
2229 if (mask & WINED3DSP_WRITEMASK_1)
2231 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2232 if (--remaining) strcat(arguments, ", ");
2234 if (mask & WINED3DSP_WRITEMASK_2)
2236 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2237 if (--remaining) strcat(arguments, ", ");
2239 if (mask & WINED3DSP_WRITEMASK_3)
2241 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2242 if (--remaining) strcat(arguments, ", ");
2245 if (mask_size > 1)
2246 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2247 else
2248 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2251 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2253 char reg_name[256];
2254 BOOL is_color;
2256 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2257 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2260 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2261 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2262 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2264 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2265 char dst_swizzle[6];
2266 struct color_fixup_desc fixup;
2267 BOOL np2_fixup = FALSE;
2268 va_list args;
2270 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2272 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2274 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2275 fixup = priv->cur_ps_args->color_fixup[sampler];
2277 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2278 if(bias) {
2279 FIXME("Biased sampling from NP2 textures is unsupported\n");
2280 } else {
2281 np2_fixup = TRUE;
2285 else
2287 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2290 shader_glsl_append_dst(ins->ctx->buffer, ins);
2292 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2293 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2295 va_start(args, coord_reg_fmt);
2296 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2297 va_end(args);
2299 if(bias) {
2300 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2301 } else {
2302 if (np2_fixup) {
2303 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2304 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2306 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2307 (idx % 2) ? "zw" : "xy", dst_swizzle);
2308 } else if(dx && dy) {
2309 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2310 } else {
2311 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2315 if(!is_identity_fixup(fixup)) {
2316 shader_glsl_color_correction(ins, fixup);
2320 /*****************************************************************************
2321 * Begin processing individual instruction opcodes
2322 ****************************************************************************/
2324 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2326 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2327 struct glsl_src_param src0_param;
2328 struct glsl_src_param src1_param;
2329 DWORD write_mask;
2330 const char *op;
2332 /* Determine the GLSL operator to use based on the opcode */
2333 switch (ins->handler_idx)
2335 case WINED3DSIH_ADD: op = "+"; break;
2336 case WINED3DSIH_AND: op = "&"; break;
2337 case WINED3DSIH_DIV: op = "/"; break;
2338 case WINED3DSIH_IADD: op = "+"; break;
2339 case WINED3DSIH_ISHL: op = "<<"; break;
2340 case WINED3DSIH_MUL: op = "*"; break;
2341 case WINED3DSIH_SUB: op = "-"; break;
2342 case WINED3DSIH_USHR: op = ">>"; break;
2343 case WINED3DSIH_XOR: op = "^"; break;
2344 default:
2345 op = "<unhandled operator>";
2346 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2347 break;
2350 write_mask = shader_glsl_append_dst(buffer, ins);
2351 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2352 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2353 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2356 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2358 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2359 struct glsl_src_param src0_param;
2360 struct glsl_src_param src1_param;
2361 unsigned int mask_size;
2362 DWORD write_mask;
2363 const char *op;
2365 write_mask = shader_glsl_append_dst(buffer, ins);
2366 mask_size = shader_glsl_get_write_mask_size(write_mask);
2367 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2368 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2370 if (mask_size > 1)
2372 switch (ins->handler_idx)
2374 case WINED3DSIH_EQ: op = "equal"; break;
2375 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2376 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2377 case WINED3DSIH_LT: op = "lessThan"; break;
2378 default:
2379 op = "<unhandled operator>";
2380 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2381 break;
2384 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2385 mask_size, op, src0_param.param_str, src1_param.param_str);
2387 else
2389 switch (ins->handler_idx)
2391 case WINED3DSIH_EQ: op = "=="; break;
2392 case WINED3DSIH_GE: op = ">="; break;
2393 case WINED3DSIH_IGE: op = ">="; break;
2394 case WINED3DSIH_LT: op = "<"; break;
2395 default:
2396 op = "<unhandled operator>";
2397 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2398 break;
2401 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2402 src0_param.param_str, op, src1_param.param_str);
2406 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2408 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2409 struct glsl_src_param src0_param;
2410 struct glsl_src_param src1_param;
2411 DWORD write_mask;
2413 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2414 * not, we can emulate it. */
2415 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2416 FIXME("64-bit integer multiplies not implemented.\n");
2418 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2420 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2421 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2422 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2424 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2425 src0_param.param_str, src1_param.param_str);
2429 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2431 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2432 struct glsl_src_param src0_param, src1_param;
2433 DWORD write_mask;
2435 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2438 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2440 char dst_mask[6];
2442 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2443 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2444 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2445 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2446 dst_mask, src0_param.param_str, src1_param.param_str);
2448 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2449 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2450 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2451 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2453 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2454 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2456 else
2458 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2459 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2460 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2461 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2464 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2466 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2467 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2468 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2469 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2473 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2474 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2476 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2477 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2478 struct glsl_src_param src0_param;
2479 DWORD write_mask;
2481 write_mask = shader_glsl_append_dst(buffer, ins);
2482 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2484 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2485 * shader versions WINED3DSIO_MOVA is used for this. */
2486 if (ins->ctx->reg_maps->shader_version.major == 1
2487 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2488 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2490 /* This is a simple floor() */
2491 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2492 if (mask_size > 1) {
2493 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2494 } else {
2495 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2498 else if(ins->handler_idx == WINED3DSIH_MOVA)
2500 /* We need to *round* to the nearest int here. */
2501 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2503 if (gl_info->supported[EXT_GPU_SHADER4])
2505 if (mask_size > 1)
2506 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2507 else
2508 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2510 else
2512 if (mask_size > 1)
2513 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2514 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2515 else
2516 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2517 src0_param.param_str, src0_param.param_str);
2520 else
2522 shader_addline(buffer, "%s);\n", src0_param.param_str);
2526 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2527 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2529 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2530 struct glsl_src_param src0_param;
2531 struct glsl_src_param src1_param;
2532 DWORD dst_write_mask, src_write_mask;
2533 unsigned int dst_size = 0;
2535 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2536 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2538 /* dp3 works on vec3, dp4 on vec4 */
2539 if (ins->handler_idx == WINED3DSIH_DP4)
2541 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2542 } else {
2543 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2546 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2547 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2549 if (dst_size > 1) {
2550 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2551 } else {
2552 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2556 /* Note that this instruction has some restrictions. The destination write mask
2557 * can't contain the w component, and the source swizzles have to be .xyzw */
2558 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2560 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2561 struct glsl_src_param src0_param;
2562 struct glsl_src_param src1_param;
2563 char dst_mask[6];
2565 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2566 shader_glsl_append_dst(ins->ctx->buffer, ins);
2567 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2568 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2569 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2572 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2574 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2577 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2578 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2579 * GLSL uses the value as-is. */
2580 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2582 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2583 struct glsl_src_param src0_param;
2584 struct glsl_src_param src1_param;
2585 DWORD dst_write_mask;
2586 unsigned int dst_size;
2588 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2589 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2591 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2592 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2594 if (dst_size > 1)
2596 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2597 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2599 else
2601 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2602 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2606 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2607 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2609 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2610 struct glsl_src_param src_param;
2611 const char *instruction;
2612 DWORD write_mask;
2613 unsigned i;
2615 /* Determine the GLSL function to use based on the opcode */
2616 /* TODO: Possibly make this a table for faster lookups */
2617 switch (ins->handler_idx)
2619 case WINED3DSIH_MIN: instruction = "min"; break;
2620 case WINED3DSIH_MAX: instruction = "max"; break;
2621 case WINED3DSIH_ABS: instruction = "abs"; break;
2622 case WINED3DSIH_FRC: instruction = "fract"; break;
2623 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2624 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2625 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2626 default: instruction = "";
2627 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2628 break;
2631 write_mask = shader_glsl_append_dst(buffer, ins);
2633 shader_addline(buffer, "%s(", instruction);
2635 if (ins->src_count)
2637 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2638 shader_addline(buffer, "%s", src_param.param_str);
2639 for (i = 1; i < ins->src_count; ++i)
2641 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2642 shader_addline(buffer, ", %s", src_param.param_str);
2646 shader_addline(buffer, "));\n");
2649 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2651 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2653 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2654 struct glsl_src_param src_param;
2655 unsigned int mask_size;
2656 DWORD write_mask;
2657 char dst_mask[6];
2659 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2660 mask_size = shader_glsl_get_write_mask_size(write_mask);
2661 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2663 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2664 src_param.param_str, src_param.param_str);
2665 shader_glsl_append_dst(buffer, ins);
2667 if (mask_size > 1)
2669 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2670 mask_size, src_param.param_str);
2672 else
2674 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2675 src_param.param_str);
2679 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2681 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2682 struct glsl_src_param src0_param;
2683 const char *prefix, *suffix;
2684 unsigned int dst_size;
2685 DWORD dst_write_mask;
2687 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2688 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2690 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2692 switch (ins->handler_idx)
2694 case WINED3DSIH_EXP:
2695 case WINED3DSIH_EXPP:
2696 prefix = "exp2(";
2697 suffix = ")";
2698 break;
2700 case WINED3DSIH_LOG:
2701 case WINED3DSIH_LOGP:
2702 prefix = "log2(abs(";
2703 suffix = "))";
2704 break;
2706 case WINED3DSIH_RCP:
2707 prefix = "1.0 / ";
2708 suffix = "";
2709 break;
2711 case WINED3DSIH_RSQ:
2712 prefix = "inversesqrt(abs(";
2713 suffix = "))";
2714 break;
2716 default:
2717 prefix = "";
2718 suffix = "";
2719 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2720 break;
2723 if (dst_size > 1)
2724 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2725 else
2726 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2729 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2730 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2731 * dst.x = 2^(floor(src))
2732 * dst.y = src - floor(src)
2733 * dst.z = 2^src (partial precision is allowed, but optional)
2734 * dst.w = 1.0;
2735 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2736 * dst = 2^src; (partial precision is allowed, but optional)
2738 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2740 if (ins->ctx->reg_maps->shader_version.major < 2)
2742 struct glsl_src_param src_param;
2743 char dst_mask[6];
2745 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2747 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2748 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2749 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2750 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2752 shader_glsl_append_dst(ins->ctx->buffer, ins);
2753 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2754 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2755 return;
2758 shader_glsl_scalar_op(ins);
2761 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2763 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2764 struct glsl_src_param src_param;
2765 unsigned int mask_size;
2766 DWORD write_mask;
2768 write_mask = shader_glsl_append_dst(buffer, ins);
2769 mask_size = shader_glsl_get_write_mask_size(write_mask);
2770 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2772 if (mask_size > 1)
2773 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2774 else
2775 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2778 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2780 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2781 struct glsl_src_param src_param;
2782 unsigned int mask_size;
2783 DWORD write_mask;
2785 write_mask = shader_glsl_append_dst(buffer, ins);
2786 mask_size = shader_glsl_get_write_mask_size(write_mask);
2787 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2789 if (mask_size > 1)
2790 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2791 else
2792 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2795 /** Process signed comparison opcodes in GLSL. */
2796 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2798 struct glsl_src_param src0_param;
2799 struct glsl_src_param src1_param;
2800 DWORD write_mask;
2801 unsigned int mask_size;
2803 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2804 mask_size = shader_glsl_get_write_mask_size(write_mask);
2805 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2806 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2808 if (mask_size > 1) {
2809 const char *compare;
2811 switch(ins->handler_idx)
2813 case WINED3DSIH_SLT: compare = "lessThan"; break;
2814 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2815 default: compare = "";
2816 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2819 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2820 src0_param.param_str, src1_param.param_str);
2821 } else {
2822 switch(ins->handler_idx)
2824 case WINED3DSIH_SLT:
2825 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2826 * to return 0.0 but step returns 1.0 because step is not < x
2827 * An alternative is a bvec compare padded with an unused second component.
2828 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2829 * issue. Playing with not() is not possible either because not() does not accept
2830 * a scalar.
2832 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2833 src0_param.param_str, src1_param.param_str);
2834 break;
2835 case WINED3DSIH_SGE:
2836 /* Here we can use the step() function and safe a conditional */
2837 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2838 break;
2839 default:
2840 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2846 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2848 const char *condition_prefix, *condition_suffix;
2849 struct wined3d_shader_dst_param dst;
2850 struct glsl_src_param src0_param;
2851 struct glsl_src_param src1_param;
2852 struct glsl_src_param src2_param;
2853 BOOL temp_destination = FALSE;
2854 DWORD cmp_channel = 0;
2855 unsigned int i, j;
2856 char mask_char[6];
2857 DWORD write_mask;
2859 switch (ins->handler_idx)
2861 case WINED3DSIH_CMP:
2862 condition_prefix = "";
2863 condition_suffix = " >= 0.0";
2864 break;
2866 case WINED3DSIH_CND:
2867 condition_prefix = "";
2868 condition_suffix = " > 0.5";
2869 break;
2871 case WINED3DSIH_MOVC:
2872 condition_prefix = "bool(";
2873 condition_suffix = ")";
2874 break;
2876 default:
2877 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2878 condition_prefix = "<unhandled prefix>";
2879 condition_suffix = "<unhandled suffix>";
2880 break;
2883 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2885 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2886 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2887 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2888 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2890 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2891 condition_prefix, src0_param.param_str, condition_suffix,
2892 src1_param.param_str, src2_param.param_str);
2893 return;
2896 dst = ins->dst[0];
2898 /* Splitting the instruction up in multiple lines imposes a problem:
2899 * The first lines may overwrite source parameters of the following lines.
2900 * Deal with that by using a temporary destination register if needed. */
2901 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2902 && ins->src[0].reg.type == dst.reg.type)
2903 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2904 && ins->src[1].reg.type == dst.reg.type)
2905 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2906 && ins->src[2].reg.type == dst.reg.type))
2907 temp_destination = TRUE;
2909 /* Cycle through all source0 channels. */
2910 for (i = 0; i < 4; ++i)
2912 write_mask = 0;
2913 /* Find the destination channels which use the current source0 channel. */
2914 for (j = 0; j < 4; ++j)
2916 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2918 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2919 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2922 dst.write_mask = ins->dst[0].write_mask & write_mask;
2924 if (temp_destination)
2926 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2927 continue;
2928 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2930 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2931 continue;
2933 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2934 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2935 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2937 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2938 condition_prefix, src0_param.param_str, condition_suffix,
2939 src1_param.param_str, src2_param.param_str);
2942 if (temp_destination)
2944 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2945 shader_glsl_append_dst(ins->ctx->buffer, ins);
2946 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2950 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2951 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2952 * the compare is done per component of src0. */
2953 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2955 struct glsl_src_param src0_param;
2956 struct glsl_src_param src1_param;
2957 struct glsl_src_param src2_param;
2958 DWORD write_mask;
2959 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2960 ins->ctx->reg_maps->shader_version.minor);
2962 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2964 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2965 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2966 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2967 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2969 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2970 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2971 else
2972 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2973 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2974 return;
2977 shader_glsl_conditional_move(ins);
2980 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2981 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2983 struct glsl_src_param src0_param;
2984 struct glsl_src_param src1_param;
2985 struct glsl_src_param src2_param;
2986 DWORD write_mask;
2988 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2989 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2990 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2991 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2992 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2993 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2996 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2997 Vertex shaders to GLSL codes */
2998 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3000 int i;
3001 int nComponents = 0;
3002 struct wined3d_shader_dst_param tmp_dst = {{0}};
3003 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3004 struct wined3d_shader_instruction tmp_ins;
3006 memset(&tmp_ins, 0, sizeof(tmp_ins));
3008 /* Set constants for the temporary argument */
3009 tmp_ins.ctx = ins->ctx;
3010 tmp_ins.dst_count = 1;
3011 tmp_ins.dst = &tmp_dst;
3012 tmp_ins.src_count = 2;
3013 tmp_ins.src = tmp_src;
3015 switch(ins->handler_idx)
3017 case WINED3DSIH_M4x4:
3018 nComponents = 4;
3019 tmp_ins.handler_idx = WINED3DSIH_DP4;
3020 break;
3021 case WINED3DSIH_M4x3:
3022 nComponents = 3;
3023 tmp_ins.handler_idx = WINED3DSIH_DP4;
3024 break;
3025 case WINED3DSIH_M3x4:
3026 nComponents = 4;
3027 tmp_ins.handler_idx = WINED3DSIH_DP3;
3028 break;
3029 case WINED3DSIH_M3x3:
3030 nComponents = 3;
3031 tmp_ins.handler_idx = WINED3DSIH_DP3;
3032 break;
3033 case WINED3DSIH_M3x2:
3034 nComponents = 2;
3035 tmp_ins.handler_idx = WINED3DSIH_DP3;
3036 break;
3037 default:
3038 break;
3041 tmp_dst = ins->dst[0];
3042 tmp_src[0] = ins->src[0];
3043 tmp_src[1] = ins->src[1];
3044 for (i = 0; i < nComponents; ++i)
3046 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3047 shader_glsl_dot(&tmp_ins);
3048 ++tmp_src[1].reg.idx[0].offset;
3053 The LRP instruction performs a component-wise linear interpolation
3054 between the second and third operands using the first operand as the
3055 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3056 This is equivalent to mix(src2, src1, src0);
3058 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3060 struct glsl_src_param src0_param;
3061 struct glsl_src_param src1_param;
3062 struct glsl_src_param src2_param;
3063 DWORD write_mask;
3065 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3067 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3068 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3069 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3071 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3072 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3075 /** Process the WINED3DSIO_LIT instruction in GLSL:
3076 * dst.x = dst.w = 1.0
3077 * dst.y = (src0.x > 0) ? src0.x
3078 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3079 * where src.w is clamped at +- 128
3081 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3083 struct glsl_src_param src0_param;
3084 struct glsl_src_param src1_param;
3085 struct glsl_src_param src3_param;
3086 char dst_mask[6];
3088 shader_glsl_append_dst(ins->ctx->buffer, ins);
3089 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3091 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3092 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3093 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3095 /* The sdk specifies the instruction like this
3096 * dst.x = 1.0;
3097 * if(src.x > 0.0) dst.y = src.x
3098 * else dst.y = 0.0.
3099 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3100 * else dst.z = 0.0;
3101 * dst.w = 1.0;
3102 * (where power = src.w clamped between -128 and 128)
3104 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3105 * dst.x = 1.0 ... No further explanation needed
3106 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3107 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3108 * dst.w = 1.0. ... Nothing fancy.
3110 * So we still have one conditional in there. So do this:
3111 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3113 * 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),
3114 * 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.
3115 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3117 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3118 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3119 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3121 shader_addline(ins->ctx->buffer,
3122 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3123 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3124 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3125 src0_param.param_str, src3_param.param_str, dst_mask);
3128 /** Process the WINED3DSIO_DST instruction in GLSL:
3129 * dst.x = 1.0
3130 * dst.y = src0.x * src0.y
3131 * dst.z = src0.z
3132 * dst.w = src1.w
3134 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3136 struct glsl_src_param src0y_param;
3137 struct glsl_src_param src0z_param;
3138 struct glsl_src_param src1y_param;
3139 struct glsl_src_param src1w_param;
3140 char dst_mask[6];
3142 shader_glsl_append_dst(ins->ctx->buffer, ins);
3143 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3145 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3146 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3147 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3148 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3150 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3151 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3154 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3155 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3156 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3158 * dst.x = cos(src0.?)
3159 * dst.y = sin(src0.?)
3160 * dst.z = dst.z
3161 * dst.w = dst.w
3163 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3165 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3166 struct glsl_src_param src0_param;
3167 DWORD write_mask;
3169 if (ins->ctx->reg_maps->shader_version.major < 4)
3171 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3173 write_mask = shader_glsl_append_dst(buffer, ins);
3174 switch (write_mask)
3176 case WINED3DSP_WRITEMASK_0:
3177 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3178 break;
3180 case WINED3DSP_WRITEMASK_1:
3181 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3182 break;
3184 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3185 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3186 src0_param.param_str, src0_param.param_str);
3187 break;
3189 default:
3190 ERR("Write mask should be .x, .y or .xy\n");
3191 break;
3194 return;
3197 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3200 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3202 char dst_mask[6];
3204 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3205 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3206 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3208 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3209 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3210 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3212 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3213 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3215 else
3217 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3218 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3219 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3222 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3224 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3225 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3226 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3230 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3231 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3232 * generate invalid code
3234 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3236 struct glsl_src_param src0_param;
3237 DWORD write_mask;
3239 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3240 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3242 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3245 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3246 * Start a for() loop where src1.y is the initial value of aL,
3247 * increment aL by src1.z for a total of src1.x iterations.
3248 * Need to use a temporary variable for this operation.
3250 /* FIXME: I don't think nested loops will work correctly this way. */
3251 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3253 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3254 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3255 const struct wined3d_shader *shader = ins->ctx->shader;
3256 const struct wined3d_shader_lconst *constant;
3257 struct glsl_src_param src1_param;
3258 const DWORD *control_values = NULL;
3260 if (ins->ctx->reg_maps->shader_version.major < 4)
3262 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3264 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3265 * class hardware doesn't support real varying indexing, but Microsoft
3266 * designed this feature for Shader model 2.x+. If the loop control is
3267 * known at compile time, the GLSL compiler can unroll the loop, and
3268 * replace indirect addressing with direct addressing. */
3269 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3271 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3273 if (constant->idx == ins->src[1].reg.idx[0].offset)
3275 control_values = constant->value;
3276 break;
3281 if (control_values)
3283 struct wined3d_shader_loop_control loop_control;
3284 loop_control.count = control_values[0];
3285 loop_control.start = control_values[1];
3286 loop_control.step = (int)control_values[2];
3288 if (loop_control.step > 0)
3290 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3291 loop_state->current_depth, loop_control.start,
3292 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3293 loop_state->current_depth, loop_control.step);
3295 else if (loop_control.step < 0)
3297 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3298 loop_state->current_depth, loop_control.start,
3299 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3300 loop_state->current_depth, loop_control.step);
3302 else
3304 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3305 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3306 loop_state->current_depth, loop_control.count,
3307 loop_state->current_depth);
3310 else
3312 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3313 loop_state->current_depth, loop_state->current_reg,
3314 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3315 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3318 ++loop_state->current_reg;
3320 else
3322 shader_addline(buffer, "for (;;)\n{\n");
3325 ++loop_state->current_depth;
3328 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3330 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3332 shader_addline(ins->ctx->buffer, "}\n");
3334 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3336 --loop_state->current_depth;
3337 --loop_state->current_reg;
3340 if (ins->handler_idx == WINED3DSIH_ENDREP)
3342 --loop_state->current_depth;
3346 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3348 const struct wined3d_shader *shader = ins->ctx->shader;
3349 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3350 const struct wined3d_shader_lconst *constant;
3351 struct glsl_src_param src0_param;
3352 const DWORD *control_values = NULL;
3354 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3355 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3357 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3359 if (constant->idx == ins->src[0].reg.idx[0].offset)
3361 control_values = constant->value;
3362 break;
3367 if (control_values)
3369 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3370 loop_state->current_depth, loop_state->current_depth,
3371 control_values[0], loop_state->current_depth);
3373 else
3375 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3376 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3377 loop_state->current_depth, loop_state->current_depth,
3378 src0_param.param_str, loop_state->current_depth);
3381 ++loop_state->current_depth;
3384 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3386 struct glsl_src_param src0_param;
3388 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3389 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3392 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3394 struct glsl_src_param src0_param;
3395 struct glsl_src_param src1_param;
3397 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3398 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3400 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3401 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3404 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3406 shader_addline(ins->ctx->buffer, "} else {\n");
3409 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3411 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3414 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3416 shader_addline(ins->ctx->buffer, "break;\n");
3419 /* FIXME: According to MSDN the compare is done per component. */
3420 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3422 struct glsl_src_param src0_param;
3423 struct glsl_src_param src1_param;
3425 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3426 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3428 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3429 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3432 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3434 struct glsl_src_param src_param;
3436 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3437 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3440 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3442 shader_addline(ins->ctx->buffer, "}\n");
3443 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3446 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3448 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3451 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3453 struct glsl_src_param src1_param;
3455 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3456 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3457 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3460 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3462 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3463 * function only suppresses the unhandled instruction warning
3467 /*********************************************
3468 * Pixel Shader Specific Code begins here
3469 ********************************************/
3470 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3472 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3473 ins->ctx->reg_maps->shader_version.minor);
3474 struct glsl_sample_function sample_function;
3475 DWORD sample_flags = 0;
3476 DWORD sampler_idx;
3477 DWORD mask = 0, swizzle;
3478 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3480 /* 1.0-1.4: Use destination register as sampler source.
3481 * 2.0+: Use provided sampler source. */
3482 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3483 sampler_idx = ins->dst[0].reg.idx[0].offset;
3484 else
3485 sampler_idx = ins->src[1].reg.idx[0].offset;
3487 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3489 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3490 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3491 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3493 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3494 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3496 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3497 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3499 case WINED3D_TTFF_COUNT1:
3500 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3501 break;
3502 case WINED3D_TTFF_COUNT2:
3503 mask = WINED3DSP_WRITEMASK_1;
3504 break;
3505 case WINED3D_TTFF_COUNT3:
3506 mask = WINED3DSP_WRITEMASK_2;
3507 break;
3508 case WINED3D_TTFF_COUNT4:
3509 case WINED3D_TTFF_DISABLE:
3510 mask = WINED3DSP_WRITEMASK_3;
3511 break;
3515 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3517 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3519 if (src_mod == WINED3DSPSM_DZ) {
3520 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3521 mask = WINED3DSP_WRITEMASK_2;
3522 } else if (src_mod == WINED3DSPSM_DW) {
3523 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3524 mask = WINED3DSP_WRITEMASK_3;
3527 else
3529 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3530 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3532 /* ps 2.0 texldp instruction always divides by the fourth component. */
3533 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3534 mask = WINED3DSP_WRITEMASK_3;
3538 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3539 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3541 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3542 mask |= sample_function.coord_mask;
3544 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3545 else swizzle = ins->src[1].swizzle;
3547 /* 1.0-1.3: Use destination register as coordinate source.
3548 1.4+: Use provided coordinate source register. */
3549 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3551 char coord_mask[6];
3552 shader_glsl_write_mask_to_str(mask, coord_mask);
3553 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3554 "T%u%s", sampler_idx, coord_mask);
3556 else
3558 struct glsl_src_param coord_param;
3559 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3560 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3562 struct glsl_src_param bias;
3563 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3564 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3565 "%s", coord_param.param_str);
3566 } else {
3567 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3568 "%s", coord_param.param_str);
3573 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3575 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3576 struct glsl_src_param coord_param, dx_param, dy_param;
3577 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3578 struct glsl_sample_function sample_function;
3579 DWORD sampler_idx;
3580 DWORD swizzle = ins->src[1].swizzle;
3581 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3583 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3585 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3586 shader_glsl_tex(ins);
3587 return;
3590 sampler_idx = ins->src[1].reg.idx[0].offset;
3591 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3592 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3594 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3595 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3596 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3597 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3599 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3600 "%s", coord_param.param_str);
3603 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3605 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3606 struct glsl_src_param coord_param, lod_param;
3607 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3608 struct glsl_sample_function sample_function;
3609 DWORD sampler_idx;
3610 DWORD swizzle = ins->src[1].swizzle;
3611 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3613 sampler_idx = ins->src[1].reg.idx[0].offset;
3614 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3615 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3616 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3618 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3619 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3621 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3623 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3624 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3626 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3627 * However, the NVIDIA drivers allow them in fragment shaders as well,
3628 * even without the appropriate extension. */
3629 WARN("Using %s in fragment shader.\n", sample_function.name);
3631 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3632 "%s", coord_param.param_str);
3635 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3637 /* FIXME: Make this work for more than just 2D textures */
3638 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3639 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3641 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3643 char dst_mask[6];
3645 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3646 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3647 ins->dst[0].reg.idx[0].offset, dst_mask);
3649 else
3651 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3652 DWORD reg = ins->src[0].reg.idx[0].offset;
3653 char dst_swizzle[6];
3655 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3657 if (src_mod == WINED3DSPSM_DZ)
3659 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3660 struct glsl_src_param div_param;
3662 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3664 if (mask_size > 1) {
3665 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3666 } else {
3667 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3670 else if (src_mod == WINED3DSPSM_DW)
3672 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3673 struct glsl_src_param div_param;
3675 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3677 if (mask_size > 1) {
3678 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3679 } else {
3680 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3682 } else {
3683 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3688 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3689 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3690 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3691 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3693 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3694 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3695 struct glsl_sample_function sample_function;
3696 struct glsl_src_param src0_param;
3697 UINT mask_size;
3699 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3701 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3702 * scalar, and projected sampling would require 4.
3704 * It is a dependent read - not valid with conditional NP2 textures
3706 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3707 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3709 switch(mask_size)
3711 case 1:
3712 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3713 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3714 break;
3716 case 2:
3717 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3718 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3719 break;
3721 case 3:
3722 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3723 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3724 break;
3726 default:
3727 FIXME("Unexpected mask size %u\n", mask_size);
3728 break;
3732 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3733 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3734 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3736 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3737 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3738 struct glsl_src_param src0_param;
3739 DWORD dst_mask;
3740 unsigned int mask_size;
3742 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3743 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3744 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3746 if (mask_size > 1) {
3747 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3748 } else {
3749 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3753 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3754 * Calculate the depth as dst.x / dst.y */
3755 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3757 struct glsl_dst_param dst_param;
3759 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3761 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3762 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3763 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3764 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3765 * >= 1.0 or < 0.0
3767 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3768 dst_param.reg_name, dst_param.reg_name);
3771 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3772 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3773 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3774 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3776 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3778 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3779 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3780 struct glsl_src_param src0_param;
3782 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3784 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3785 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3788 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3789 * Calculate the 1st of a 2-row matrix multiplication. */
3790 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3792 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3793 DWORD reg = ins->dst[0].reg.idx[0].offset;
3794 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3795 struct glsl_src_param src0_param;
3797 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3798 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3801 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3802 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3803 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3805 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3806 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3807 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3808 DWORD reg = ins->dst[0].reg.idx[0].offset;
3809 struct glsl_src_param src0_param;
3811 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3812 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3813 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3816 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3818 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3819 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3820 struct glsl_sample_function sample_function;
3821 DWORD reg = ins->dst[0].reg.idx[0].offset;
3822 struct glsl_src_param src0_param;
3824 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3825 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3827 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3829 /* Sample the texture using the calculated coordinates */
3830 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3833 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3834 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3835 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3837 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3838 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3839 struct glsl_sample_function sample_function;
3840 DWORD reg = ins->dst[0].reg.idx[0].offset;
3841 struct glsl_src_param src0_param;
3843 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3844 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3846 /* Dependent read, not valid with conditional NP2 */
3847 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3849 /* Sample the texture using the calculated coordinates */
3850 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3852 tex_mx->current_row = 0;
3855 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3856 * Perform the 3rd row of a 3x3 matrix multiply */
3857 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3859 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3860 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3861 DWORD reg = ins->dst[0].reg.idx[0].offset;
3862 struct glsl_src_param src0_param;
3863 char dst_mask[6];
3865 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3867 shader_glsl_append_dst(ins->ctx->buffer, ins);
3868 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3869 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3871 tex_mx->current_row = 0;
3874 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3875 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3876 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3878 struct glsl_src_param src0_param;
3879 struct glsl_src_param src1_param;
3880 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3881 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3882 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3883 struct glsl_sample_function sample_function;
3884 DWORD reg = ins->dst[0].reg.idx[0].offset;
3885 char coord_mask[6];
3887 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3888 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3890 /* Perform the last matrix multiply operation */
3891 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3892 /* Reflection calculation */
3893 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3895 /* Dependent read, not valid with conditional NP2 */
3896 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3897 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3899 /* Sample the texture */
3900 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3901 NULL, NULL, NULL, "tmp0%s", coord_mask);
3903 tex_mx->current_row = 0;
3906 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3907 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3908 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3910 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3911 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3912 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3913 struct glsl_sample_function sample_function;
3914 DWORD reg = ins->dst[0].reg.idx[0].offset;
3915 struct glsl_src_param src0_param;
3916 char coord_mask[6];
3918 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3920 /* Perform the last matrix multiply operation */
3921 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3923 /* Construct the eye-ray vector from w coordinates */
3924 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3925 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3926 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3928 /* Dependent read, not valid with conditional NP2 */
3929 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3930 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3932 /* Sample the texture using the calculated coordinates */
3933 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3934 NULL, NULL, NULL, "tmp0%s", coord_mask);
3936 tex_mx->current_row = 0;
3939 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3940 * Apply a fake bump map transform.
3941 * texbem is pshader <= 1.3 only, this saves a few version checks
3943 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3945 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3946 struct glsl_sample_function sample_function;
3947 struct glsl_src_param coord_param;
3948 DWORD sampler_idx;
3949 DWORD mask;
3950 DWORD flags;
3951 char coord_mask[6];
3953 sampler_idx = ins->dst[0].reg.idx[0].offset;
3954 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3955 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3957 /* Dependent read, not valid with conditional NP2 */
3958 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3959 mask = sample_function.coord_mask;
3961 shader_glsl_write_mask_to_str(mask, coord_mask);
3963 /* With projected textures, texbem only divides the static texture coord,
3964 * not the displacement, so we can't let GL handle this. */
3965 if (flags & WINED3D_PSARGS_PROJECTED)
3967 DWORD div_mask=0;
3968 char coord_div_mask[3];
3969 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3971 case WINED3D_TTFF_COUNT1:
3972 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3973 break;
3974 case WINED3D_TTFF_COUNT2:
3975 div_mask = WINED3DSP_WRITEMASK_1;
3976 break;
3977 case WINED3D_TTFF_COUNT3:
3978 div_mask = WINED3DSP_WRITEMASK_2;
3979 break;
3980 case WINED3D_TTFF_COUNT4:
3981 case WINED3D_TTFF_DISABLE:
3982 div_mask = WINED3DSP_WRITEMASK_3;
3983 break;
3985 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3986 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3989 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3991 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3992 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3993 coord_param.param_str, coord_mask);
3995 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3997 struct glsl_src_param luminance_param;
3998 struct glsl_dst_param dst_param;
4000 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4001 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4003 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4004 dst_param.reg_name, dst_param.mask_str,
4005 luminance_param.param_str, sampler_idx, sampler_idx);
4009 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4011 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4012 struct glsl_src_param src0_param, src1_param;
4014 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4015 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4017 shader_glsl_append_dst(ins->ctx->buffer, ins);
4018 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4019 src0_param.param_str, sampler_idx, src1_param.param_str);
4022 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4023 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4024 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4026 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4027 struct glsl_sample_function sample_function;
4028 struct glsl_src_param src0_param;
4030 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4032 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4033 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4034 "%s.wx", src0_param.reg_name);
4037 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4038 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4039 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4041 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4042 struct glsl_sample_function sample_function;
4043 struct glsl_src_param src0_param;
4045 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4047 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4048 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4049 "%s.yz", src0_param.reg_name);
4052 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4053 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4054 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4056 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4057 struct glsl_sample_function sample_function;
4058 struct glsl_src_param src0_param;
4060 /* Dependent read, not valid with conditional NP2 */
4061 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4062 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4064 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4065 "%s", src0_param.param_str);
4068 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4069 * If any of the first 3 components are < 0, discard this pixel */
4070 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4072 struct glsl_dst_param dst_param;
4074 /* The argument is a destination parameter, and no writemasks are allowed */
4075 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4076 if (ins->ctx->reg_maps->shader_version.major >= 2)
4078 if (ins->ctx->reg_maps->shader_version.major >= 4)
4079 FIXME("SM4 discard not implemented.\n");
4080 /* 2.0 shaders compare all 4 components in texkill */
4081 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4082 } else {
4083 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4084 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4085 * 4 components are defined, only the first 3 are used
4087 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4091 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4092 * dst = dot2(src0, src1) + src2 */
4093 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4095 struct glsl_src_param src0_param;
4096 struct glsl_src_param src1_param;
4097 struct glsl_src_param src2_param;
4098 DWORD write_mask;
4099 unsigned int mask_size;
4101 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4102 mask_size = shader_glsl_get_write_mask_size(write_mask);
4104 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4105 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4106 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4108 if (mask_size > 1) {
4109 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4110 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4111 } else {
4112 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4113 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4117 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4118 const struct wined3d_shader_signature_element *input_signature,
4119 const struct wined3d_shader_reg_maps *reg_maps,
4120 enum vertexprocessing_mode vertexprocessing)
4122 WORD map = reg_maps->input_registers;
4123 unsigned int i;
4125 for (i = 0; map; map >>= 1, ++i)
4127 const char *semantic_name;
4128 UINT semantic_idx;
4129 char reg_mask[6];
4131 /* Unused */
4132 if (!(map & 1)) continue;
4134 semantic_name = input_signature[i].semantic_name;
4135 semantic_idx = input_signature[i].semantic_idx;
4136 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4138 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4140 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4141 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4142 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4143 else
4144 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4145 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4147 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4149 if (!semantic_idx)
4150 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4151 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4152 else if (semantic_idx == 1)
4153 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4154 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4155 else
4156 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4157 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4159 else
4161 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4162 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4167 /*********************************************
4168 * Vertex Shader Specific Code begins here
4169 ********************************************/
4171 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4173 struct glsl_program_key key;
4175 key.vs_id = entry->vs.id;
4176 key.gs_id = entry->gs.id;
4177 key.ps_id = entry->ps.id;
4179 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4181 ERR("Failed to insert program entry.\n");
4185 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4186 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4188 struct wine_rb_entry *entry;
4189 struct glsl_program_key key;
4191 key.vs_id = vs_id;
4192 key.gs_id = gs_id;
4193 key.ps_id = ps_id;
4195 entry = wine_rb_get(&priv->program_lookup, &key);
4196 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4199 /* Context activation is done by the caller. */
4200 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4201 struct glsl_shader_prog_link *entry)
4203 struct glsl_program_key key;
4205 key.vs_id = entry->vs.id;
4206 key.gs_id = entry->gs.id;
4207 key.ps_id = entry->ps.id;
4208 wine_rb_remove(&priv->program_lookup, &key);
4210 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4211 if (entry->vs.id)
4212 list_remove(&entry->vs.shader_entry);
4213 if (entry->gs.id)
4214 list_remove(&entry->gs.shader_entry);
4215 if (entry->ps.id)
4216 list_remove(&entry->ps.shader_entry);
4217 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4218 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4219 HeapFree(GetProcessHeap(), 0, entry);
4222 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4223 const struct wined3d_gl_info *gl_info, const DWORD *map,
4224 const struct wined3d_shader_signature_element *input_signature,
4225 const struct wined3d_shader_reg_maps *reg_maps_in,
4226 const struct wined3d_shader_signature_element *output_signature,
4227 const struct wined3d_shader_reg_maps *reg_maps_out)
4229 unsigned int i, j;
4230 const char *semantic_name_in;
4231 UINT semantic_idx_in;
4232 DWORD *set;
4233 DWORD in_idx;
4234 unsigned int in_count = vec4_varyings(3, gl_info);
4235 char reg_mask[6];
4236 char destination[50];
4237 WORD input_map, output_map;
4239 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4241 input_map = reg_maps_in->input_registers;
4242 for (i = 0; input_map; input_map >>= 1, ++i)
4244 if (!(input_map & 1)) continue;
4246 in_idx = map[i];
4247 /* Declared, but not read register */
4248 if (in_idx == ~0U) continue;
4249 if (in_idx >= (in_count + 2))
4251 FIXME("More input varyings declared than supported, expect issues.\n");
4252 continue;
4255 if (in_idx == in_count)
4256 sprintf(destination, "gl_FrontColor");
4257 else if (in_idx == in_count + 1)
4258 sprintf(destination, "gl_FrontSecondaryColor");
4259 else
4260 sprintf(destination, "ps_in[%u]", in_idx);
4262 semantic_name_in = input_signature[i].semantic_name;
4263 semantic_idx_in = input_signature[i].semantic_idx;
4264 set[in_idx] = ~0U;
4266 output_map = reg_maps_out->output_registers;
4267 for (j = 0; output_map; output_map >>= 1, ++j)
4269 DWORD mask;
4271 if (!(output_map & 1)
4272 || semantic_idx_in != output_signature[j].semantic_idx
4273 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4274 || !(mask = input_signature[i].mask & output_signature[j].mask))
4275 continue;
4277 set[in_idx] = mask;
4278 shader_glsl_write_mask_to_str(mask, reg_mask);
4280 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4281 destination, reg_mask, j, reg_mask);
4285 for (i = 0; i < in_count + 2; ++i)
4287 unsigned int size;
4289 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4290 continue;
4292 if (set[i] == ~0U) set[i] = 0;
4294 size = 0;
4295 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4296 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4297 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4298 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4299 reg_mask[size] = '\0';
4301 if (i == in_count)
4302 sprintf(destination, "gl_FrontColor");
4303 else if (i == in_count + 1)
4304 sprintf(destination, "gl_FrontSecondaryColor");
4305 else
4306 sprintf(destination, "ps_in[%u]", i);
4308 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4309 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4312 HeapFree(GetProcessHeap(), 0, set);
4315 /* Context activation is done by the caller. */
4316 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4317 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4318 const struct wined3d_gl_info *gl_info)
4320 GLhandleARB ret = 0;
4321 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4322 unsigned int i;
4323 const char *semantic_name;
4324 UINT semantic_idx;
4325 char reg_mask[6];
4326 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4327 WORD map = vs->reg_maps.output_registers;
4329 shader_buffer_clear(buffer);
4331 shader_addline(buffer, "#version 120\n");
4333 if (ps_major < 3)
4335 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4337 for (i = 0; map; map >>= 1, ++i)
4339 DWORD write_mask;
4341 if (!(map & 1)) continue;
4343 semantic_name = output_signature[i].semantic_name;
4344 semantic_idx = output_signature[i].semantic_idx;
4345 write_mask = output_signature[i].mask;
4346 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4348 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4350 if (!semantic_idx)
4351 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4352 reg_mask, i, reg_mask);
4353 else if (semantic_idx == 1)
4354 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4355 reg_mask, i, reg_mask);
4357 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4359 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4360 reg_mask, i, reg_mask);
4362 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4364 if (semantic_idx < 8)
4366 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4367 write_mask |= WINED3DSP_WRITEMASK_3;
4369 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4370 semantic_idx, reg_mask, i, reg_mask);
4371 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4372 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4375 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4377 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4379 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4381 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4384 shader_addline(buffer, "}\n");
4386 else
4388 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4389 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4390 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4391 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4393 /* First, sort out position and point size. Those are not passed to the pixel shader */
4394 for (i = 0; map; map >>= 1, ++i)
4396 if (!(map & 1)) continue;
4398 semantic_name = output_signature[i].semantic_name;
4399 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4401 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4403 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4404 reg_mask, i, reg_mask);
4406 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4408 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4412 /* Then, fix the pixel shader input */
4413 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4414 &ps->reg_maps, output_signature, &vs->reg_maps);
4416 shader_addline(buffer, "}\n");
4419 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4420 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4421 shader_glsl_compile(gl_info, ret, buffer->buffer);
4423 return ret;
4426 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4428 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4429 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4430 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4431 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4432 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4433 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4436 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4438 switch (mode)
4440 case WINED3D_FFP_PS_FOG_OFF:
4441 return;
4443 case WINED3D_FFP_PS_FOG_LINEAR:
4444 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4445 break;
4447 case WINED3D_FFP_PS_FOG_EXP:
4448 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4449 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4450 break;
4452 case WINED3D_FFP_PS_FOG_EXP2:
4453 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4454 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4455 break;
4457 default:
4458 ERR("Invalid fog mode %#x.\n", mode);
4459 return;
4462 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4465 /* Context activation is done by the caller. */
4466 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4467 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4468 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4470 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4471 const struct wined3d_gl_info *gl_info = context->gl_info;
4472 const DWORD *function = shader->function;
4473 struct shader_glsl_ctx_priv priv_ctx;
4475 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4476 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4478 memset(&priv_ctx, 0, sizeof(priv_ctx));
4479 priv_ctx.cur_ps_args = args;
4480 priv_ctx.cur_np2fixup_info = np2fixup_info;
4482 shader_addline(buffer, "#version 120\n");
4484 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4485 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4486 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4487 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4488 /* The spec says that it doesn't have to be explicitly enabled, but the
4489 * nvidia drivers write a warning if we don't do so. */
4490 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4491 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4492 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4493 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4494 if (gl_info->supported[EXT_GPU_SHADER4])
4495 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4497 /* Base Declarations */
4498 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4500 /* Pack 3.0 inputs */
4501 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4502 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4504 /* Base Shader Body */
4505 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4507 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4508 if (reg_maps->shader_version.major < 2)
4510 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4511 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4514 if (args->srgb_correction)
4515 shader_glsl_generate_srgb_write_correction(buffer);
4517 /* SM < 3 does not replace the fog stage. */
4518 if (reg_maps->shader_version.major < 3)
4519 shader_glsl_generate_fog_code(buffer, args->fog);
4521 shader_addline(buffer, "}\n");
4523 TRACE("Compiling shader object %u\n", shader_obj);
4524 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4526 /* Store the shader object */
4527 return shader_obj;
4530 /* Context activation is done by the caller. */
4531 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4532 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4533 const struct vs_compile_args *args)
4535 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4536 const struct wined3d_gl_info *gl_info = context->gl_info;
4537 const DWORD *function = shader->function;
4538 struct shader_glsl_ctx_priv priv_ctx;
4540 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4541 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4543 shader_addline(buffer, "#version 120\n");
4545 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4546 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4547 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4548 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4549 if (gl_info->supported[EXT_GPU_SHADER4])
4550 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4552 memset(&priv_ctx, 0, sizeof(priv_ctx));
4553 priv_ctx.cur_vs_args = args;
4555 /* Base Declarations */
4556 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4558 /* Base Shader Body */
4559 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4561 /* Unpack outputs */
4562 shader_addline(buffer, "order_ps_input(vs_out);\n");
4564 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4565 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4566 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4567 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4569 if (args->fog_src == VS_FOG_Z)
4570 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4571 else if (!reg_maps->fog)
4572 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4574 /* We always store the clipplanes without y inversion */
4575 if (args->clip_enabled)
4576 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4578 /* Write the final position.
4580 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4581 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4582 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4583 * contains 1.0 to allow a mad.
4585 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4586 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4588 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4590 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4591 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4592 * which is the same as z = z * 2 - w.
4594 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4596 shader_addline(buffer, "}\n");
4598 TRACE("Compiling shader object %u\n", shader_obj);
4599 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4601 return shader_obj;
4604 /* Context activation is done by the caller. */
4605 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4606 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4608 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4609 const struct wined3d_gl_info *gl_info = context->gl_info;
4610 const DWORD *function = shader->function;
4611 struct shader_glsl_ctx_priv priv_ctx;
4612 GLhandleARB shader_id;
4614 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4616 shader_addline(buffer, "#version 120\n");
4618 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4619 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4620 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4621 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4622 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4623 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4624 if (gl_info->supported[EXT_GPU_SHADER4])
4625 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4627 memset(&priv_ctx, 0, sizeof(priv_ctx));
4628 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4629 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4630 shader_addline(buffer, "}\n");
4632 TRACE("Compiling shader object %u.\n", shader_id);
4633 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4635 return shader_id;
4638 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4639 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4640 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4642 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4643 struct glsl_shader_private *shader_data;
4644 struct ps_np2fixup_info *np2fixup;
4645 UINT i;
4646 DWORD new_size;
4647 GLhandleARB ret;
4649 if (!shader->backend_data)
4651 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4652 if (!shader->backend_data)
4654 ERR("Failed to allocate backend data.\n");
4655 return 0;
4658 shader_data = shader->backend_data;
4659 gl_shaders = shader_data->gl_shaders.ps;
4661 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4662 * so a linear search is more performant than a hashmap or a binary search
4663 * (cache coherency etc)
4665 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4667 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4669 if (args->np2_fixup)
4670 *np2fixup_info = &gl_shaders[i].np2fixup;
4671 return gl_shaders[i].prgId;
4675 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4676 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4677 if (shader_data->num_gl_shaders)
4679 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4680 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4681 new_size * sizeof(*gl_shaders));
4683 else
4685 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4686 new_size = 1;
4689 if(!new_array) {
4690 ERR("Out of memory\n");
4691 return 0;
4693 shader_data->gl_shaders.ps = new_array;
4694 shader_data->shader_array_size = new_size;
4695 gl_shaders = new_array;
4698 gl_shaders[shader_data->num_gl_shaders].args = *args;
4700 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4701 memset(np2fixup, 0, sizeof(*np2fixup));
4702 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4704 pixelshader_update_samplers(shader, args->tex_types);
4706 shader_buffer_clear(buffer);
4707 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4708 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4710 return ret;
4713 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4714 const DWORD use_map) {
4715 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4716 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4717 return stored->fog_src == new->fog_src;
4720 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4721 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4722 const struct vs_compile_args *args)
4724 UINT i;
4725 DWORD new_size;
4726 DWORD use_map = context->stream_info.use_map;
4727 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4728 struct glsl_shader_private *shader_data;
4729 GLhandleARB ret;
4731 if (!shader->backend_data)
4733 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4734 if (!shader->backend_data)
4736 ERR("Failed to allocate backend data.\n");
4737 return 0;
4740 shader_data = shader->backend_data;
4741 gl_shaders = shader_data->gl_shaders.vs;
4743 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4744 * so a linear search is more performant than a hashmap or a binary search
4745 * (cache coherency etc)
4747 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4749 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4750 return gl_shaders[i].prgId;
4753 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4755 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4756 if (shader_data->num_gl_shaders)
4758 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4759 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4760 new_size * sizeof(*gl_shaders));
4762 else
4764 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4765 new_size = 1;
4768 if(!new_array) {
4769 ERR("Out of memory\n");
4770 return 0;
4772 shader_data->gl_shaders.vs = new_array;
4773 shader_data->shader_array_size = new_size;
4774 gl_shaders = new_array;
4777 gl_shaders[shader_data->num_gl_shaders].args = *args;
4779 shader_buffer_clear(buffer);
4780 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4781 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4783 return ret;
4786 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4787 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4789 struct glsl_gs_compiled_shader *gl_shaders;
4790 struct glsl_shader_private *shader_data;
4791 GLhandleARB ret;
4793 if (!shader->backend_data)
4795 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4797 ERR("Failed to allocate backend data.\n");
4798 return 0;
4801 shader_data = shader->backend_data;
4802 gl_shaders = shader_data->gl_shaders.gs;
4804 if (shader_data->num_gl_shaders)
4805 return gl_shaders[0].id;
4807 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4809 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4811 ERR("Failed to allocate GL shader array.\n");
4812 return 0;
4814 shader_data->shader_array_size = 1;
4815 gl_shaders = shader_data->gl_shaders.gs;
4817 shader_buffer_clear(buffer);
4818 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4819 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4821 return ret;
4824 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4826 switch (mcs)
4828 case WINED3D_MCS_MATERIAL:
4829 return material;
4830 case WINED3D_MCS_COLOR1:
4831 return "gl_Color";
4832 case WINED3D_MCS_COLOR2:
4833 return "gl_SecondaryColor";
4834 default:
4835 ERR("Invalid material color source %#x.\n", mcs);
4836 return "<invalid>";
4840 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4841 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4843 const char *diffuse, *specular, *emission, *ambient;
4844 enum wined3d_light_type light_type;
4845 unsigned int i;
4847 if (!settings->lighting)
4849 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4850 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4851 return;
4854 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4855 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4856 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4857 shader_addline(buffer, "vec3 dir, dst;\n");
4858 shader_addline(buffer, "float att, t;\n");
4860 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4861 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4862 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4863 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4865 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4867 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4868 switch (light_type)
4870 case WINED3D_LIGHT_POINT:
4871 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4872 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4873 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4874 shader_addline(buffer, "dst.x = 1.0;\n");
4875 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4876 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4877 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4878 if (!settings->normal)
4879 break;
4880 shader_addline(buffer, "dir = normalize(dir);\n");
4881 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4882 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4883 if (settings->localviewer)
4884 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4885 else
4886 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4887 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4888 " * gl_LightSource[%u].specular) / att;\n", i);
4889 break;
4891 case WINED3D_LIGHT_SPOT:
4892 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4893 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4894 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4895 shader_addline(buffer, "dst.x = 1.0;\n");
4896 shader_addline(buffer, "dir = normalize(dir);\n");
4897 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4898 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4899 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4900 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4901 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4902 i, i, i, i);
4903 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4904 if (!settings->normal)
4905 break;
4906 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4907 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4908 if (settings->localviewer)
4909 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4910 else
4911 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4912 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4913 " * gl_LightSource[%u].specular) * att;\n", i);
4914 break;
4916 case WINED3D_LIGHT_DIRECTIONAL:
4917 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4918 if (!settings->normal)
4919 break;
4920 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4921 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4922 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4923 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4924 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4925 " * gl_LightSource[%u].specular;\n", i);
4926 break;
4928 default:
4929 if (light_type)
4930 FIXME("Unhandled light type %#x.\n", light_type);
4931 continue;
4935 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4936 ambient, diffuse, emission);
4937 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4938 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4941 /* Context activation is done by the caller. */
4942 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4943 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4945 GLhandleARB shader_obj;
4946 unsigned int i;
4948 shader_buffer_clear(buffer);
4950 shader_addline(buffer, "#version 120\n");
4951 shader_addline(buffer, "\n");
4952 shader_addline(buffer, "void main()\n{\n");
4953 shader_addline(buffer, "float m;\n");
4954 shader_addline(buffer, "vec3 r;\n");
4956 if (settings->transformed)
4958 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4959 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4960 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4962 else
4964 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4965 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4966 if (settings->clipping)
4967 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4968 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4971 if (!settings->normal)
4972 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4973 else if (settings->normalize)
4974 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4975 else
4976 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4978 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4980 for (i = 0; i < MAX_TEXTURES; ++i)
4982 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4984 case WINED3DTSS_TCI_PASSTHRU:
4985 if (settings->texcoords & (1 << i))
4986 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4987 i, i, i);
4988 break;
4990 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4991 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4992 break;
4994 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4995 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4996 break;
4998 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4999 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5000 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5001 break;
5003 case WINED3DTSS_TCI_SPHEREMAP:
5004 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5005 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5006 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5007 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5008 break;
5010 default:
5011 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5012 break;
5016 switch (settings->fog_mode)
5018 case WINED3D_FFP_VS_FOG_OFF:
5019 break;
5021 case WINED3D_FFP_VS_FOG_FOGCOORD:
5022 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5023 break;
5025 case WINED3D_FFP_VS_FOG_RANGE:
5026 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5027 break;
5029 case WINED3D_FFP_VS_FOG_DEPTH:
5030 if (settings->ortho_fog)
5031 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5032 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5033 else
5034 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5035 break;
5037 default:
5038 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5039 break;
5042 if (settings->point_size)
5044 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5045 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5046 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5047 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5050 shader_addline(buffer, "}\n");
5052 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5053 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5055 return shader_obj;
5058 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5059 DWORD argnum, unsigned int stage, DWORD arg)
5061 const char *ret;
5063 if (arg == ARG_UNUSED)
5064 return "<unused arg>";
5066 switch (arg & WINED3DTA_SELECTMASK)
5068 case WINED3DTA_DIFFUSE:
5069 ret = "gl_Color";
5070 break;
5072 case WINED3DTA_CURRENT:
5073 if (!stage)
5074 ret = "gl_Color";
5075 else
5076 ret = "ret";
5077 break;
5079 case WINED3DTA_TEXTURE:
5080 switch (stage)
5082 case 0: ret = "tex0"; break;
5083 case 1: ret = "tex1"; break;
5084 case 2: ret = "tex2"; break;
5085 case 3: ret = "tex3"; break;
5086 case 4: ret = "tex4"; break;
5087 case 5: ret = "tex5"; break;
5088 case 6: ret = "tex6"; break;
5089 case 7: ret = "tex7"; break;
5090 default:
5091 ret = "<invalid texture>";
5092 break;
5094 break;
5096 case WINED3DTA_TFACTOR:
5097 ret = "tex_factor";
5098 break;
5100 case WINED3DTA_SPECULAR:
5101 ret = "gl_SecondaryColor";
5102 break;
5104 case WINED3DTA_TEMP:
5105 ret = "temp_reg";
5106 break;
5108 case WINED3DTA_CONSTANT:
5109 switch (stage)
5111 case 0: ret = "tss_const0"; break;
5112 case 1: ret = "tss_const1"; break;
5113 case 2: ret = "tss_const2"; break;
5114 case 3: ret = "tss_const3"; break;
5115 case 4: ret = "tss_const4"; break;
5116 case 5: ret = "tss_const5"; break;
5117 case 6: ret = "tss_const6"; break;
5118 case 7: ret = "tss_const7"; break;
5119 default:
5120 ret = "<invalid constant>";
5121 break;
5123 break;
5125 default:
5126 return "<unhandled arg>";
5129 if (arg & WINED3DTA_COMPLEMENT)
5131 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5132 if (argnum == 0)
5133 ret = "arg0";
5134 else if (argnum == 1)
5135 ret = "arg1";
5136 else if (argnum == 2)
5137 ret = "arg2";
5140 if (arg & WINED3DTA_ALPHAREPLICATE)
5142 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5143 if (argnum == 0)
5144 ret = "arg0";
5145 else if (argnum == 1)
5146 ret = "arg1";
5147 else if (argnum == 2)
5148 ret = "arg2";
5151 return ret;
5154 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5155 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5157 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5159 if (color && alpha)
5160 dstmask = "";
5161 else if (color)
5162 dstmask = ".xyz";
5163 else
5164 dstmask = ".w";
5166 if (dst == tempreg)
5167 dstreg = "temp_reg";
5168 else
5169 dstreg = "ret";
5171 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5172 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5173 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5175 switch (op)
5177 case WINED3D_TOP_DISABLE:
5178 if (!stage)
5179 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5180 break;
5182 case WINED3D_TOP_SELECT_ARG1:
5183 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5184 break;
5186 case WINED3D_TOP_SELECT_ARG2:
5187 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5188 break;
5190 case WINED3D_TOP_MODULATE:
5191 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5192 break;
5194 case WINED3D_TOP_MODULATE_4X:
5195 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5196 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5197 break;
5199 case WINED3D_TOP_MODULATE_2X:
5200 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5201 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5202 break;
5204 case WINED3D_TOP_ADD:
5205 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5206 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5207 break;
5209 case WINED3D_TOP_ADD_SIGNED:
5210 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5211 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5212 break;
5214 case WINED3D_TOP_ADD_SIGNED_2X:
5215 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5216 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5217 break;
5219 case WINED3D_TOP_SUBTRACT:
5220 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5221 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5222 break;
5224 case WINED3D_TOP_ADD_SMOOTH:
5225 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5226 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5227 break;
5229 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5230 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5231 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5232 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5233 break;
5235 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5236 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5237 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5238 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5239 break;
5241 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5242 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5243 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5244 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5245 break;
5247 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5248 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5249 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5250 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5251 break;
5253 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5254 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5255 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5256 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5257 break;
5259 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5260 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5261 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5262 break;
5264 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5265 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5266 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5267 break;
5269 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5270 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5271 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5272 break;
5273 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5274 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5275 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5276 break;
5278 case WINED3D_TOP_BUMPENVMAP:
5279 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5280 /* These are handled in the first pass, nothing to do. */
5281 break;
5283 case WINED3D_TOP_DOTPRODUCT3:
5284 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5285 dstreg, dstmask, arg1, arg2, dstmask);
5286 break;
5288 case WINED3D_TOP_MULTIPLY_ADD:
5289 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5290 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5291 break;
5293 case WINED3D_TOP_LERP:
5294 /* MSDN isn't quite right here. */
5295 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5296 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5297 break;
5299 default:
5300 FIXME("Unhandled operation %#x.\n", op);
5301 break;
5305 /* Context activation is done by the caller. */
5306 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5307 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5309 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5310 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5311 const char *final_combiner_src = "ret";
5312 UINT lowest_disabled_stage;
5313 GLhandleARB shader_obj;
5314 DWORD arg0, arg1, arg2;
5315 unsigned int stage;
5317 shader_buffer_clear(buffer);
5319 /* Find out which textures are read */
5320 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5322 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5323 break;
5325 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5326 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5327 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5329 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5330 tex_map |= 1 << stage;
5331 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5332 tfactor_used = TRUE;
5333 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5334 tempreg_used = TRUE;
5335 if (settings->op[stage].dst == tempreg)
5336 tempreg_used = TRUE;
5337 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5338 tss_const_map |= 1 << stage;
5340 switch (settings->op[stage].cop)
5342 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5343 lum_map |= 1 << stage;
5344 /* fall through */
5345 case WINED3D_TOP_BUMPENVMAP:
5346 bump_map |= 1 << stage;
5347 /* fall through */
5348 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5349 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5350 tex_map |= 1 << stage;
5351 break;
5353 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5354 tfactor_used = TRUE;
5355 break;
5357 default:
5358 break;
5361 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5362 continue;
5364 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5365 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5366 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5368 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5369 tex_map |= 1 << stage;
5370 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5371 tfactor_used = TRUE;
5372 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5373 tempreg_used = TRUE;
5374 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5375 tss_const_map |= 1 << stage;
5377 lowest_disabled_stage = stage;
5379 shader_addline(buffer, "#version 120\n");
5381 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5382 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5384 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5385 shader_addline(buffer, "vec4 ret;\n");
5386 if (tempreg_used || settings->sRGB_write)
5387 shader_addline(buffer, "vec4 temp_reg;\n");
5388 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5390 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5392 if (tss_const_map & (1 << stage))
5393 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5395 if (!(tex_map & (1 << stage)))
5396 continue;
5398 switch (settings->op[stage].tex_type)
5400 case tex_1d:
5401 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5402 break;
5403 case tex_2d:
5404 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5405 break;
5406 case tex_3d:
5407 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5408 break;
5409 case tex_cube:
5410 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5411 break;
5412 case tex_rect:
5413 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5414 break;
5415 default:
5416 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5417 break;
5420 shader_addline(buffer, "vec4 tex%u;\n", stage);
5422 if (!(bump_map & (1 << stage)))
5423 continue;
5424 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5426 if (!(lum_map & (1 << stage)))
5427 continue;
5428 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5429 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5431 if (tfactor_used)
5432 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5433 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5435 if (settings->sRGB_write)
5437 shader_addline(buffer, "const vec4 srgb_const0 = ");
5438 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5439 shader_addline(buffer, ";\n");
5440 shader_addline(buffer, "const vec4 srgb_const1 = ");
5441 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5442 shader_addline(buffer, ";\n");
5445 shader_addline(buffer, "void main()\n{\n");
5447 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5448 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5450 /* Generate texture sampling instructions) */
5451 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5453 const char *texture_function, *coord_mask;
5454 char tex_reg_name[8];
5455 BOOL proj;
5457 if (!(tex_map & (1 << stage)))
5458 continue;
5460 if (settings->op[stage].projected == proj_none)
5462 proj = FALSE;
5464 else if (settings->op[stage].projected == proj_count4
5465 || settings->op[stage].projected == proj_count3)
5467 proj = TRUE;
5469 else
5471 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5472 proj = TRUE;
5475 switch (settings->op[stage].tex_type)
5477 case tex_1d:
5478 if (proj)
5480 texture_function = "texture1DProj";
5481 coord_mask = "xw";
5483 else
5485 texture_function = "texture1D";
5486 coord_mask = "x";
5488 break;
5489 case tex_2d:
5490 if (proj)
5492 texture_function = "texture2DProj";
5493 coord_mask = "xyw";
5495 else
5497 texture_function = "texture2D";
5498 coord_mask = "xy";
5500 break;
5501 case tex_3d:
5502 if (proj)
5504 texture_function = "texture3DProj";
5505 coord_mask = "xyzw";
5507 else
5509 texture_function = "texture3D";
5510 coord_mask = "xyz";
5512 break;
5513 case tex_cube:
5514 texture_function = "textureCube";
5515 coord_mask = "xyz";
5516 break;
5517 case tex_rect:
5518 if (proj)
5520 texture_function = "texture2DRectProj";
5521 coord_mask = "xyw";
5523 else
5525 texture_function = "texture2DRect";
5526 coord_mask = "xy";
5528 break;
5529 default:
5530 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5531 texture_function = "";
5532 coord_mask = "xyzw";
5533 break;
5536 if (stage > 0
5537 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5538 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5540 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5542 /* With projective textures, texbem only divides the static
5543 * texture coord, not the displacement, so multiply the
5544 * displacement with the dividing parameter before passing it to
5545 * TXP. */
5546 if (settings->op[stage].projected != proj_none)
5548 if (settings->op[stage].projected == proj_count4)
5550 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5551 stage, stage);
5552 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5554 else
5556 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5557 stage, stage);
5558 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5561 else
5563 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5566 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5567 stage, texture_function, stage, coord_mask);
5569 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5570 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5571 stage, stage - 1, stage - 1, stage - 1);
5573 else if (settings->op[stage].projected == proj_count3)
5575 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5576 stage, texture_function, stage, stage);
5578 else
5580 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5581 stage, texture_function, stage, stage, coord_mask);
5584 sprintf(tex_reg_name, "tex%u", stage);
5585 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5586 settings->op[stage].color_fixup);
5589 /* Generate the main shader */
5590 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5592 BOOL op_equal;
5594 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5596 if (!stage)
5597 final_combiner_src = "gl_Color";
5598 break;
5601 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5602 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5603 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5604 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5605 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5606 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5607 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5608 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5609 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5610 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5611 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5612 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5613 else
5614 op_equal = settings->op[stage].aop == settings->op[stage].cop
5615 && settings->op[stage].carg0 == settings->op[stage].aarg0
5616 && settings->op[stage].carg1 == settings->op[stage].aarg1
5617 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5619 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5621 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5622 settings->op[stage].cop, settings->op[stage].carg0,
5623 settings->op[stage].carg1, settings->op[stage].carg2);
5624 if (!stage)
5625 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5627 else if (op_equal)
5629 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5630 settings->op[stage].cop, settings->op[stage].carg0,
5631 settings->op[stage].carg1, settings->op[stage].carg2);
5633 else
5635 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5636 settings->op[stage].cop, settings->op[stage].carg0,
5637 settings->op[stage].carg1, settings->op[stage].carg2);
5638 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5639 settings->op[stage].aop, settings->op[stage].aarg0,
5640 settings->op[stage].aarg1, settings->op[stage].aarg2);
5644 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5646 if (settings->sRGB_write)
5647 shader_glsl_generate_srgb_write_correction(buffer);
5649 shader_glsl_generate_fog_code(buffer, settings->fog);
5651 shader_addline(buffer, "}\n");
5653 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5654 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5655 return shader_obj;
5658 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5659 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5661 struct glsl_ffp_vertex_shader *shader;
5662 const struct wine_rb_entry *entry;
5664 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5665 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5667 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5668 return NULL;
5670 shader->desc.settings = *settings;
5671 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5672 list_init(&shader->linked_programs);
5673 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5674 ERR("Failed to insert ffp vertex shader.\n");
5676 return shader;
5679 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5680 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5682 struct glsl_ffp_fragment_shader *glsl_desc;
5683 const struct ffp_frag_desc *desc;
5685 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5686 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5688 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5689 return NULL;
5691 glsl_desc->entry.settings = *args;
5692 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5693 list_init(&glsl_desc->linked_programs);
5694 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5696 return glsl_desc;
5700 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5701 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5703 unsigned int i;
5704 char name[32];
5706 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5707 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5708 for (i = 0; i < vs_c_count; ++i)
5710 snprintf(name, sizeof(name), "vs_c[%u]", i);
5711 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5713 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5714 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5716 for (i = 0; i < MAX_CONST_I; ++i)
5718 snprintf(name, sizeof(name), "vs_i[%u]", i);
5719 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5722 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5725 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5726 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5728 unsigned int i;
5729 char name[32];
5731 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5732 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5733 for (i = 0; i < ps_c_count; ++i)
5735 snprintf(name, sizeof(name), "ps_c[%u]", i);
5736 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5738 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5739 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5741 for (i = 0; i < MAX_CONST_I; ++i)
5743 snprintf(name, sizeof(name), "ps_i[%u]", i);
5744 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5747 for (i = 0; i < MAX_TEXTURES; ++i)
5749 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5750 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5751 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5752 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5753 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5754 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5755 snprintf(name, sizeof(name), "tss_const%u", i);
5756 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5759 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5760 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5761 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5762 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5765 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLhandleARB program_id,
5766 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5768 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5769 GLuint block_idx;
5770 unsigned int i;
5771 char name[16];
5773 for (i = 0; i < count; ++i)
5775 if (!reg_maps->cb_sizes[i])
5776 continue;
5778 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5779 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5780 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5782 checkGLcall("glUniformBlockBinding");
5785 /* Context activation is done by the caller. */
5786 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5787 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5789 const struct wined3d_gl_info *gl_info = context->gl_info;
5790 const struct ps_np2fixup_info *np2fixup_info = NULL;
5791 struct glsl_shader_prog_link *entry = NULL;
5792 struct wined3d_shader *vshader = NULL;
5793 struct wined3d_shader *gshader = NULL;
5794 struct wined3d_shader *pshader = NULL;
5795 GLhandleARB programId = 0;
5796 GLhandleARB reorder_shader_id = 0;
5797 unsigned int i;
5798 GLhandleARB vs_id = 0;
5799 GLhandleARB gs_id = 0;
5800 GLhandleARB ps_id = 0;
5801 struct list *ps_list, *vs_list;
5803 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5805 vs_id = ctx_data->glsl_program->vs.id;
5806 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5808 if (use_vs(state))
5810 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5811 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5813 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5814 && ctx_data->glsl_program->gs.id)
5815 gs_id = ctx_data->glsl_program->gs.id;
5816 else if (gshader)
5817 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5820 else if (use_vs(state))
5822 struct vs_compile_args vs_compile_args;
5823 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5825 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5826 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5827 vs_list = &vshader->linked_programs;
5829 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5830 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5832 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5834 struct glsl_ffp_vertex_shader *ffp_shader;
5835 struct wined3d_ffp_vs_settings settings;
5837 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5838 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5839 vs_id = ffp_shader->id;
5840 vs_list = &ffp_shader->linked_programs;
5843 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5845 ps_id = ctx_data->glsl_program->ps.id;
5846 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5848 if (use_ps(state))
5849 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5851 else if (use_ps(state))
5853 struct ps_compile_args ps_compile_args;
5854 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5855 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5856 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5857 pshader, &ps_compile_args, &np2fixup_info);
5858 ps_list = &pshader->linked_programs;
5860 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5862 struct glsl_ffp_fragment_shader *ffp_shader;
5863 struct ffp_frag_settings settings;
5865 gen_ffp_frag_op(context, state, &settings, FALSE);
5866 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5867 ps_id = ffp_shader->id;
5868 ps_list = &ffp_shader->linked_programs;
5871 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5873 ctx_data->glsl_program = entry;
5874 return;
5877 /* If we get to this point, then no matching program exists, so we create one */
5878 programId = GL_EXTCALL(glCreateProgramObjectARB());
5879 TRACE("Created new GLSL shader program %u\n", programId);
5881 /* Create the entry */
5882 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5883 entry->programId = programId;
5884 entry->vs.id = vs_id;
5885 entry->gs.id = gs_id;
5886 entry->ps.id = ps_id;
5887 entry->constant_version = 0;
5888 entry->ps.np2_fixup_info = np2fixup_info;
5889 /* Add the hash table entry */
5890 add_glsl_program_entry(priv, entry);
5892 /* Set the current program */
5893 ctx_data->glsl_program = entry;
5895 /* Attach GLSL vshader */
5896 if (vs_id)
5898 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5899 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5900 checkGLcall("glAttachObjectARB");
5902 list_add_head(vs_list, &entry->vs.shader_entry);
5905 if (vshader)
5907 WORD map = vshader->reg_maps.input_registers;
5908 char tmp_name[10];
5910 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5911 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5912 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5913 checkGLcall("glAttachObjectARB");
5914 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5915 * is destroyed
5917 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5919 /* Bind vertex attributes to a corresponding index number to match
5920 * the same index numbers as ARB_vertex_programs (makes loading
5921 * vertex attributes simpler). With this method, we can use the
5922 * exact same code to load the attributes later for both ARB and
5923 * GLSL shaders.
5925 * We have to do this here because we need to know the Program ID
5926 * in order to make the bindings work, and it has to be done prior
5927 * to linking the GLSL program. */
5928 for (i = 0; map; map >>= 1, ++i)
5930 if (!(map & 1)) continue;
5932 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5933 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5935 checkGLcall("glBindAttribLocationARB");
5938 if (gshader)
5940 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5941 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5942 checkGLcall("glAttachObjectARB");
5944 TRACE("input type %s, output type %s, vertices out %u.\n",
5945 debug_d3dprimitivetype(gshader->u.gs.input_type),
5946 debug_d3dprimitivetype(gshader->u.gs.output_type),
5947 gshader->u.gs.vertices_out);
5948 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5949 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5950 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5951 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5952 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5953 gshader->u.gs.vertices_out));
5954 checkGLcall("glProgramParameteriARB");
5956 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5959 /* Attach GLSL pshader */
5960 if (ps_id)
5962 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5963 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5964 checkGLcall("glAttachObjectARB");
5966 list_add_head(ps_list, &entry->ps.shader_entry);
5969 /* Link the program */
5970 TRACE("Linking GLSL shader program %u\n", programId);
5971 GL_EXTCALL(glLinkProgramARB(programId));
5972 shader_glsl_validate_link(gl_info, programId);
5974 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5975 vshader ? vshader->limits.constant_float : 0);
5976 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5977 pshader ? pshader->limits.constant_float : 0);
5978 checkGLcall("Find glsl program uniform locations");
5980 if (pshader && pshader->reg_maps.shader_version.major >= 3
5981 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5983 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5984 entry->vs.vertex_color_clamp = GL_FALSE;
5986 else
5988 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5991 /* Set the shader to allow uniform loading on it */
5992 GL_EXTCALL(glUseProgramObjectARB(programId));
5993 checkGLcall("glUseProgramObjectARB(programId)");
5995 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5996 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5997 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5998 * vertex shader with fixed function pixel processing is used we make sure that the card
5999 * supports enough samplers to allow the max number of vertex samplers with all possible
6000 * fixed function fragment processing setups. So once the program is linked these samplers
6001 * won't change.
6003 shader_glsl_load_vsamplers(gl_info, context->tex_unit_map, programId);
6004 shader_glsl_load_psamplers(gl_info, context->tex_unit_map, programId);
6006 entry->constant_update_mask = 0;
6007 if (vshader)
6009 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6010 if (vshader->reg_maps.integer_constants)
6011 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6012 if (vshader->reg_maps.boolean_constants)
6013 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6014 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6016 shader_glsl_init_uniform_block_bindings(gl_info, programId, &vshader->reg_maps,
6017 0, gl_info->limits.vertex_uniform_blocks);
6020 if (gshader)
6021 shader_glsl_init_uniform_block_bindings(gl_info, programId, &gshader->reg_maps,
6022 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6024 if (ps_id)
6026 if (pshader)
6028 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6029 if (pshader->reg_maps.integer_constants)
6030 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6031 if (pshader->reg_maps.boolean_constants)
6032 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6033 if (entry->ps.ycorrection_location != -1)
6034 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6036 shader_glsl_init_uniform_block_bindings(gl_info, programId, &pshader->reg_maps,
6037 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6038 gl_info->limits.fragment_uniform_blocks);
6040 else
6042 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6045 for (i = 0; i < MAX_TEXTURES; ++i)
6047 if (entry->ps.bumpenv_mat_location[i] != -1)
6049 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6050 break;
6054 if (entry->ps.np2_fixup_location != -1)
6055 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6059 /* Context activation is done by the caller. */
6060 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6062 GLhandleARB program_id;
6063 GLhandleARB vshader_id, pshader_id;
6064 const char *blt_pshader;
6066 static const char blt_vshader[] =
6067 "#version 120\n"
6068 "void main(void)\n"
6069 "{\n"
6070 " gl_Position = gl_Vertex;\n"
6071 " gl_FrontColor = vec4(1.0);\n"
6072 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6073 "}\n";
6075 static const char * const blt_pshaders_full[tex_type_count] =
6077 /* tex_1d */
6078 NULL,
6079 /* tex_2d */
6080 "#version 120\n"
6081 "uniform sampler2D sampler;\n"
6082 "void main(void)\n"
6083 "{\n"
6084 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6085 "}\n",
6086 /* tex_3d */
6087 NULL,
6088 /* tex_cube */
6089 "#version 120\n"
6090 "uniform samplerCube sampler;\n"
6091 "void main(void)\n"
6092 "{\n"
6093 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6094 "}\n",
6095 /* tex_rect */
6096 "#version 120\n"
6097 "#extension GL_ARB_texture_rectangle : enable\n"
6098 "uniform sampler2DRect sampler;\n"
6099 "void main(void)\n"
6100 "{\n"
6101 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6102 "}\n",
6105 static const char * const blt_pshaders_masked[tex_type_count] =
6107 /* tex_1d */
6108 NULL,
6109 /* tex_2d */
6110 "#version 120\n"
6111 "uniform sampler2D sampler;\n"
6112 "uniform vec4 mask;\n"
6113 "void main(void)\n"
6114 "{\n"
6115 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6116 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6117 "}\n",
6118 /* tex_3d */
6119 NULL,
6120 /* tex_cube */
6121 "#version 120\n"
6122 "uniform samplerCube sampler;\n"
6123 "uniform vec4 mask;\n"
6124 "void main(void)\n"
6125 "{\n"
6126 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6127 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6128 "}\n",
6129 /* tex_rect */
6130 "#version 120\n"
6131 "#extension GL_ARB_texture_rectangle : enable\n"
6132 "uniform sampler2DRect sampler;\n"
6133 "uniform vec4 mask;\n"
6134 "void main(void)\n"
6135 "{\n"
6136 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6137 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6138 "}\n",
6141 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6142 if (!blt_pshader)
6144 FIXME("tex_type %#x not supported\n", tex_type);
6145 return 0;
6148 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6149 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6151 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6152 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6154 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6155 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6156 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6157 GL_EXTCALL(glLinkProgramARB(program_id));
6159 shader_glsl_validate_link(gl_info, program_id);
6161 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6162 * is destroyed
6164 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6165 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6166 return program_id;
6169 /* Context activation is done by the caller. */
6170 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6171 const struct wined3d_state *state)
6173 struct glsl_context_data *ctx_data = context->shader_backend_data;
6174 const struct wined3d_gl_info *gl_info = context->gl_info;
6175 struct shader_glsl_priv *priv = shader_priv;
6176 GLhandleARB program_id = 0, prev_id = 0;
6177 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6179 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6180 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6182 if (ctx_data->glsl_program)
6184 prev_id = ctx_data->glsl_program->programId;
6185 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6187 else
6189 prev_id = 0;
6190 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6193 set_glsl_shader_program(context, state, priv, ctx_data);
6195 if (ctx_data->glsl_program)
6197 program_id = ctx_data->glsl_program->programId;
6198 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6200 else
6202 program_id = 0;
6203 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6206 if (old_vertex_color_clamp != current_vertex_color_clamp)
6208 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6210 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6211 checkGLcall("glClampColorARB");
6213 else
6215 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6219 TRACE("Using GLSL program %u.\n", program_id);
6221 if (prev_id != program_id)
6223 GL_EXTCALL(glUseProgramObjectARB(program_id));
6224 checkGLcall("glUseProgramObjectARB");
6226 if (program_id)
6227 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6231 /* "context" is not necessarily the currently active context. */
6232 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6234 struct glsl_context_data *ctx_data = context->shader_backend_data;
6236 ctx_data->glsl_program = NULL;
6237 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6238 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6239 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6242 /* Context activation is done by the caller. */
6243 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6245 const struct wined3d_gl_info *gl_info = context->gl_info;
6246 struct shader_glsl_priv *priv = shader_priv;
6248 shader_glsl_invalidate_current_program(context);
6249 GL_EXTCALL(glUseProgramObjectARB(0));
6250 checkGLcall("glUseProgramObjectARB");
6252 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6253 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6255 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6257 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6258 checkGLcall("glClampColorARB");
6262 /* Context activation is done by the caller. */
6263 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6264 enum tex_types tex_type, const SIZE *ds_mask_size)
6266 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6267 struct shader_glsl_priv *priv = shader_priv;
6268 GLhandleARB *blt_program;
6269 GLint loc;
6271 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6272 if (!*blt_program)
6274 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6275 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6276 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6277 GL_EXTCALL(glUniform1iARB(loc, 0));
6279 else
6281 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6284 if (masked)
6286 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6287 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6291 /* Context activation is done by the caller. */
6292 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6294 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6295 GLhandleARB program_id;
6297 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6298 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6300 GL_EXTCALL(glUseProgramObjectARB(program_id));
6301 checkGLcall("glUseProgramObjectARB");
6304 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6305 const struct glsl_shader_prog_link *program)
6307 const struct glsl_context_data *ctx_data;
6308 struct wined3d_context *context;
6309 unsigned int i;
6311 for (i = 0; i < device->context_count; ++i)
6313 context = device->contexts[i];
6314 ctx_data = context->shader_backend_data;
6316 if (ctx_data->glsl_program == program)
6317 shader_glsl_invalidate_current_program(context);
6321 static void shader_glsl_destroy(struct wined3d_shader *shader)
6323 struct glsl_shader_private *shader_data = shader->backend_data;
6324 struct wined3d_device *device = shader->device;
6325 struct shader_glsl_priv *priv = device->shader_priv;
6326 const struct wined3d_gl_info *gl_info;
6327 const struct list *linked_programs;
6328 struct wined3d_context *context;
6330 if (!shader_data || !shader_data->num_gl_shaders)
6332 HeapFree(GetProcessHeap(), 0, shader_data);
6333 shader->backend_data = NULL;
6334 return;
6337 context = context_acquire(device, NULL);
6338 gl_info = context->gl_info;
6340 TRACE("Deleting linked programs.\n");
6341 linked_programs = &shader->linked_programs;
6342 if (linked_programs->next)
6344 struct glsl_shader_prog_link *entry, *entry2;
6345 UINT i;
6347 switch (shader->reg_maps.shader_version.type)
6349 case WINED3D_SHADER_TYPE_PIXEL:
6351 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6353 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6355 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6356 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6357 checkGLcall("glDeleteObjectARB");
6359 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6361 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6362 struct glsl_shader_prog_link, ps.shader_entry)
6364 shader_glsl_invalidate_contexts_program(device, entry);
6365 delete_glsl_program_entry(priv, gl_info, entry);
6368 break;
6371 case WINED3D_SHADER_TYPE_VERTEX:
6373 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6375 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6377 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6378 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6379 checkGLcall("glDeleteObjectARB");
6381 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6383 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6384 struct glsl_shader_prog_link, vs.shader_entry)
6386 shader_glsl_invalidate_contexts_program(device, entry);
6387 delete_glsl_program_entry(priv, gl_info, entry);
6390 break;
6393 case WINED3D_SHADER_TYPE_GEOMETRY:
6395 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6397 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6399 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6400 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6401 checkGLcall("glDeleteObjectARB");
6403 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6405 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6406 struct glsl_shader_prog_link, gs.shader_entry)
6408 shader_glsl_invalidate_contexts_program(device, entry);
6409 delete_glsl_program_entry(priv, gl_info, entry);
6412 break;
6415 default:
6416 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6417 break;
6421 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6422 shader->backend_data = NULL;
6424 context_release(context);
6427 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6429 const struct glsl_program_key *k = key;
6430 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6431 const struct glsl_shader_prog_link, program_lookup_entry);
6433 if (k->vs_id > prog->vs.id) return 1;
6434 else if (k->vs_id < prog->vs.id) return -1;
6436 if (k->gs_id > prog->gs.id) return 1;
6437 else if (k->gs_id < prog->gs.id) return -1;
6439 if (k->ps_id > prog->ps.id) return 1;
6440 else if (k->ps_id < prog->ps.id) return -1;
6442 return 0;
6445 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6447 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6448 + constant_count * sizeof(*heap->contained)
6449 + constant_count * sizeof(*heap->positions);
6450 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6452 if (!mem)
6454 ERR("Failed to allocate memory\n");
6455 return FALSE;
6458 heap->entries = mem;
6459 heap->entries[1].version = 0;
6460 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6461 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6462 heap->positions = (unsigned int *)(heap->contained + constant_count);
6463 heap->size = 1;
6465 return TRUE;
6468 static void constant_heap_free(struct constant_heap *heap)
6470 HeapFree(GetProcessHeap(), 0, heap->entries);
6473 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6475 wined3d_rb_alloc,
6476 wined3d_rb_realloc,
6477 wined3d_rb_free,
6478 glsl_program_key_compare,
6481 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6482 const struct fragment_pipeline *fragment_pipe)
6484 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6485 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6486 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6487 gl_info->limits.glsl_ps_float_constants)) + 1;
6488 struct fragment_caps fragment_caps;
6489 void *vertex_priv, *fragment_priv;
6491 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6493 ERR("Failed to initialize vertex pipe.\n");
6494 HeapFree(GetProcessHeap(), 0, priv);
6495 return E_FAIL;
6498 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6500 ERR("Failed to initialize fragment pipe.\n");
6501 vertex_pipe->vp_free(device);
6502 HeapFree(GetProcessHeap(), 0, priv);
6503 return E_FAIL;
6506 if (!shader_buffer_init(&priv->shader_buffer))
6508 ERR("Failed to initialize shader buffer.\n");
6509 goto fail;
6512 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6513 if (!priv->stack)
6515 ERR("Failed to allocate memory.\n");
6516 goto fail;
6519 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6521 ERR("Failed to initialize vertex shader constant heap\n");
6522 goto fail;
6525 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6527 ERR("Failed to initialize pixel shader constant heap\n");
6528 goto fail;
6531 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6533 ERR("Failed to initialize rbtree.\n");
6534 goto fail;
6537 priv->next_constant_version = 1;
6538 priv->vertex_pipe = vertex_pipe;
6539 priv->fragment_pipe = fragment_pipe;
6540 fragment_pipe->get_caps(gl_info, &fragment_caps);
6541 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6543 device->vertex_priv = vertex_priv;
6544 device->fragment_priv = fragment_priv;
6545 device->shader_priv = priv;
6547 return WINED3D_OK;
6549 fail:
6550 constant_heap_free(&priv->pconst_heap);
6551 constant_heap_free(&priv->vconst_heap);
6552 HeapFree(GetProcessHeap(), 0, priv->stack);
6553 shader_buffer_free(&priv->shader_buffer);
6554 fragment_pipe->free_private(device);
6555 vertex_pipe->vp_free(device);
6556 HeapFree(GetProcessHeap(), 0, priv);
6557 return E_OUTOFMEMORY;
6560 /* Context activation is done by the caller. */
6561 static void shader_glsl_free(struct wined3d_device *device)
6563 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6564 struct shader_glsl_priv *priv = device->shader_priv;
6565 int i;
6567 for (i = 0; i < tex_type_count; ++i)
6569 if (priv->depth_blt_program_full[i])
6571 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6573 if (priv->depth_blt_program_masked[i])
6575 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6579 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6580 constant_heap_free(&priv->pconst_heap);
6581 constant_heap_free(&priv->vconst_heap);
6582 HeapFree(GetProcessHeap(), 0, priv->stack);
6583 shader_buffer_free(&priv->shader_buffer);
6584 priv->fragment_pipe->free_private(device);
6585 priv->vertex_pipe->vp_free(device);
6587 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6588 device->shader_priv = NULL;
6591 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6593 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6594 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6597 static void shader_glsl_free_context_data(struct wined3d_context *context)
6599 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6602 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6604 UINT shader_model;
6606 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6607 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6608 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6609 && gl_info->supported[ARB_TEXTURE_RG])
6610 shader_model = 4;
6611 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6612 * texldd and texldl instructions. */
6613 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6614 shader_model = 3;
6615 else
6616 shader_model = 2;
6617 TRACE("Shader model %u.\n", shader_model);
6619 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6620 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6621 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6623 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6624 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6626 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6627 * Direct3D minimum requirement.
6629 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6630 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6632 * The problem is that the refrast clamps temporary results in the shader to
6633 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6634 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6635 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6636 * offer a way to query this.
6638 if (shader_model >= 4)
6639 caps->ps_1x_max_value = FLT_MAX;
6640 else
6641 caps->ps_1x_max_value = 1024.0f;
6643 /* Ideally we'd only set caps like sRGB writes here if supported by both
6644 * the shader backend and the fragment pipe, but we can get called before
6645 * shader_glsl_alloc(). */
6646 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6647 | WINED3D_SHADER_CAP_SRGB_WRITE;
6650 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6652 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6654 TRACE("Checking support for fixup:\n");
6655 dump_color_fixup_desc(fixup);
6658 /* We support everything except YUV conversions. */
6659 if (!is_complex_fixup(fixup))
6661 TRACE("[OK]\n");
6662 return TRUE;
6665 TRACE("[FAILED]\n");
6666 return FALSE;
6669 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6671 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6672 /* WINED3DSIH_ADD */ shader_glsl_binop,
6673 /* WINED3DSIH_AND */ shader_glsl_binop,
6674 /* WINED3DSIH_BEM */ shader_glsl_bem,
6675 /* WINED3DSIH_BREAK */ shader_glsl_break,
6676 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6677 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6678 /* WINED3DSIH_CALL */ shader_glsl_call,
6679 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6680 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6681 /* WINED3DSIH_CND */ shader_glsl_cnd,
6682 /* WINED3DSIH_CRS */ shader_glsl_cross,
6683 /* WINED3DSIH_CUT */ shader_glsl_cut,
6684 /* WINED3DSIH_DCL */ shader_glsl_nop,
6685 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6686 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6687 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6688 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6689 /* WINED3DSIH_DEF */ shader_glsl_nop,
6690 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6691 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6692 /* WINED3DSIH_DIV */ shader_glsl_binop,
6693 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6694 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6695 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6696 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6697 /* WINED3DSIH_DST */ shader_glsl_dst,
6698 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6699 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6700 /* WINED3DSIH_ELSE */ shader_glsl_else,
6701 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6702 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6703 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6704 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6705 /* WINED3DSIH_EQ */ shader_glsl_relop,
6706 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6707 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6708 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6709 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6710 /* WINED3DSIH_GE */ shader_glsl_relop,
6711 /* WINED3DSIH_IADD */ shader_glsl_binop,
6712 /* WINED3DSIH_IEQ */ NULL,
6713 /* WINED3DSIH_IF */ shader_glsl_if,
6714 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6715 /* WINED3DSIH_IGE */ shader_glsl_relop,
6716 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6717 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6718 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6719 /* WINED3DSIH_LABEL */ shader_glsl_label,
6720 /* WINED3DSIH_LD */ NULL,
6721 /* WINED3DSIH_LIT */ shader_glsl_lit,
6722 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6723 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6724 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6725 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6726 /* WINED3DSIH_LT */ shader_glsl_relop,
6727 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6728 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6729 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6730 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6731 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6732 /* WINED3DSIH_MAD */ shader_glsl_mad,
6733 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6734 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6735 /* WINED3DSIH_MOV */ shader_glsl_mov,
6736 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6737 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6738 /* WINED3DSIH_MUL */ shader_glsl_binop,
6739 /* WINED3DSIH_NOP */ shader_glsl_nop,
6740 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6741 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6742 /* WINED3DSIH_POW */ shader_glsl_pow,
6743 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6744 /* WINED3DSIH_REP */ shader_glsl_rep,
6745 /* WINED3DSIH_RET */ shader_glsl_ret,
6746 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6747 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6748 /* WINED3DSIH_SAMPLE */ NULL,
6749 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6750 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6751 /* WINED3DSIH_SETP */ NULL,
6752 /* WINED3DSIH_SGE */ shader_glsl_compare,
6753 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6754 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6755 /* WINED3DSIH_SLT */ shader_glsl_compare,
6756 /* WINED3DSIH_SQRT */ NULL,
6757 /* WINED3DSIH_SUB */ shader_glsl_binop,
6758 /* WINED3DSIH_TEX */ shader_glsl_tex,
6759 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6760 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6761 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6762 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6763 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6764 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6765 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6766 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6767 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6768 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6769 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6770 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6771 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6772 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6773 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6774 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6775 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6776 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6777 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6778 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6779 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6780 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6781 /* WINED3DSIH_USHR */ shader_glsl_binop,
6782 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6783 /* WINED3DSIH_XOR */ shader_glsl_binop,
6786 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6787 SHADER_HANDLER hw_fct;
6789 /* Select handler */
6790 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6792 /* Unhandled opcode */
6793 if (!hw_fct)
6795 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6796 return;
6798 hw_fct(ins);
6800 shader_glsl_add_instruction_modifiers(ins);
6803 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6805 struct shader_glsl_priv *priv = shader_priv;
6807 return priv->ffp_proj_control;
6810 const struct wined3d_shader_backend_ops glsl_shader_backend =
6812 shader_glsl_handle_instruction,
6813 shader_glsl_select,
6814 shader_glsl_disable,
6815 shader_glsl_select_depth_blt,
6816 shader_glsl_deselect_depth_blt,
6817 shader_glsl_update_float_vertex_constants,
6818 shader_glsl_update_float_pixel_constants,
6819 shader_glsl_load_constants,
6820 shader_glsl_destroy,
6821 shader_glsl_alloc,
6822 shader_glsl_free,
6823 shader_glsl_allocate_context_data,
6824 shader_glsl_free_context_data,
6825 shader_glsl_get_caps,
6826 shader_glsl_color_fixup_supported,
6827 shader_glsl_has_ffp_proj_control,
6830 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6832 if (enable)
6833 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6834 else
6835 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6836 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6839 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6841 caps->xyzrhw = TRUE;
6842 caps->max_active_lights = gl_info->limits.lights;
6843 caps->max_vertex_blend_matrices = 1;
6844 caps->max_vertex_blend_matrix_index = 0;
6845 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6846 | WINED3DVTXPCAPS_MATERIALSOURCE7
6847 | WINED3DVTXPCAPS_VERTEXFOG
6848 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6849 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6850 | WINED3DVTXPCAPS_LOCALVIEWER
6851 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6852 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6853 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6854 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6857 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6859 struct shader_glsl_priv *priv;
6861 if (shader_backend == &glsl_shader_backend)
6863 priv = shader_priv;
6865 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6867 ERR("Failed to initialize rbtree.\n");
6868 return NULL;
6871 return priv;
6874 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6876 return NULL;
6879 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6881 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6882 struct glsl_ffp_vertex_shader, desc.entry);
6883 struct glsl_shader_prog_link *program, *program2;
6884 struct glsl_ffp_destroy_ctx *ctx = context;
6886 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6887 struct glsl_shader_prog_link, vs.shader_entry)
6889 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6891 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6892 HeapFree(GetProcessHeap(), 0, shader);
6895 /* Context activation is done by the caller. */
6896 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6898 struct shader_glsl_priv *priv = device->vertex_priv;
6899 struct glsl_ffp_destroy_ctx ctx;
6901 ctx.priv = priv;
6902 ctx.gl_info = &device->adapter->gl_info;
6903 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6906 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6907 const struct wined3d_state *state, DWORD state_id)
6909 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6912 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6913 const struct wined3d_state *state, DWORD state_id)
6915 /* Table fog behavior depends on the projection matrix. */
6916 if (state->render_states[WINED3D_RS_FOGENABLE]
6917 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6918 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6919 transform_projection(context, state, state_id);
6922 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6924 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6925 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6926 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6927 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6928 /* Clip planes */
6929 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6930 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6931 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6932 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6933 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6934 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6935 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6936 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6937 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6938 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6939 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6940 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6941 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6942 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6943 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6944 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6945 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6946 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6947 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6948 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6949 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6950 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6951 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6952 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6953 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6954 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6955 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6956 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6957 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6958 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6959 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6960 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6961 /* Lights */
6962 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6963 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6964 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6965 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6966 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6967 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6968 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6969 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6970 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6971 /* Viewport */
6972 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6973 /* Transform states */
6974 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6975 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6976 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6977 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6978 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6979 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6980 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6981 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6982 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6983 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6984 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6985 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6986 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6987 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6988 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6989 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6990 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6991 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6992 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6993 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6994 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6995 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6996 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6997 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6998 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6999 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7000 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7001 /* Fog */
7002 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7003 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7004 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7005 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7006 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7007 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7008 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7009 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7010 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7011 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7012 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7013 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7014 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7015 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7016 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7017 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7018 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7019 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7020 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7021 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7022 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7023 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7024 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7025 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7026 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7027 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7028 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7029 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7030 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7031 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7032 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7033 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7034 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7035 * handler in that case to get the vertex part of sampler() skipped (VTF
7036 * is handled in the misc states). Otherwise, register
7037 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7038 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7039 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7040 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7041 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7042 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7043 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7044 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7045 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7046 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7047 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7048 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7049 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7050 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7051 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7052 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7053 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7054 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7055 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7056 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7057 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7058 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7059 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7060 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7061 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7062 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7063 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7066 /* TODO:
7067 * - This currently depends on GL fixed function functions to set things
7068 * like light parameters. Ideally we'd use regular uniforms for that.
7069 * - In part because of the previous point, much of this is modelled after
7070 * GL fixed function, and has much of the same limitations. For example,
7071 * D3D spot lights are slightly different from GL spot lights.
7072 * - We can now implement drawing transformed vertices using the GLSL pipe,
7073 * instead of using the immediate mode fallback.
7074 * - Similarly, we don't need the fallback for certain combinations of
7075 * material sources anymore.
7076 * - Implement vertex blending and vertex tweening.
7077 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7078 * attribute arrays in load_tex_coords().
7079 * - Per-vertex point sizes. */
7080 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7082 glsl_vertex_pipe_vp_enable,
7083 glsl_vertex_pipe_vp_get_caps,
7084 glsl_vertex_pipe_vp_alloc,
7085 glsl_vertex_pipe_vp_free,
7086 glsl_vertex_pipe_vp_states,
7089 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7091 /* Nothing to do. */
7094 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7096 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7097 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7098 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7099 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7100 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7101 | WINED3DTEXOPCAPS_SELECTARG1
7102 | WINED3DTEXOPCAPS_SELECTARG2
7103 | WINED3DTEXOPCAPS_MODULATE4X
7104 | WINED3DTEXOPCAPS_MODULATE2X
7105 | WINED3DTEXOPCAPS_MODULATE
7106 | WINED3DTEXOPCAPS_ADDSIGNED2X
7107 | WINED3DTEXOPCAPS_ADDSIGNED
7108 | WINED3DTEXOPCAPS_ADD
7109 | WINED3DTEXOPCAPS_SUBTRACT
7110 | WINED3DTEXOPCAPS_ADDSMOOTH
7111 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7112 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7113 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7114 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7115 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7116 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7117 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7118 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7119 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7120 | WINED3DTEXOPCAPS_DOTPRODUCT3
7121 | WINED3DTEXOPCAPS_MULTIPLYADD
7122 | WINED3DTEXOPCAPS_LERP
7123 | WINED3DTEXOPCAPS_BUMPENVMAP
7124 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7125 caps->MaxTextureBlendStages = 8;
7126 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7129 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7131 struct shader_glsl_priv *priv;
7133 if (shader_backend == &glsl_shader_backend)
7135 priv = shader_priv;
7137 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7139 ERR("Failed to initialize rbtree.\n");
7140 return NULL;
7143 return priv;
7146 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7148 return NULL;
7151 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7153 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7154 struct glsl_ffp_fragment_shader, entry.entry);
7155 struct glsl_shader_prog_link *program, *program2;
7156 struct glsl_ffp_destroy_ctx *ctx = context;
7158 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7159 struct glsl_shader_prog_link, ps.shader_entry)
7161 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7163 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7164 HeapFree(GetProcessHeap(), 0, shader);
7167 /* Context activation is done by the caller. */
7168 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7170 struct shader_glsl_priv *priv = device->fragment_priv;
7171 struct glsl_ffp_destroy_ctx ctx;
7173 ctx.priv = priv;
7174 ctx.gl_info = &device->adapter->gl_info;
7175 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7178 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7179 const struct wined3d_state *state, DWORD state_id)
7181 context->last_was_pshader = use_ps(state);
7183 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7186 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7187 const struct wined3d_state *state, DWORD state_id)
7189 BOOL use_vshader = use_vs(state);
7190 enum fogsource new_source;
7191 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7192 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7194 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7196 if (!state->render_states[WINED3D_RS_FOGENABLE])
7197 return;
7199 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7201 if (use_vshader)
7202 new_source = FOGSOURCE_VS;
7203 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7204 new_source = FOGSOURCE_COORD;
7205 else
7206 new_source = FOGSOURCE_FFP;
7208 else
7210 new_source = FOGSOURCE_FFP;
7213 if (new_source != context->fog_source || fogstart == fogend)
7215 context->fog_source = new_source;
7216 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7220 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7221 const struct wined3d_state *state, DWORD state_id)
7223 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7226 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7227 const struct wined3d_state *state, DWORD state_id)
7229 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7232 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7234 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7250 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7252 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7253 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7254 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7255 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7256 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7257 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7259 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7260 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7261 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7263 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7264 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7265 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7266 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7267 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7268 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7269 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7270 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7271 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7276 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7295 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7296 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7297 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7298 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7300 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7302 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7303 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7304 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7305 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7306 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7307 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7308 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7309 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7310 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7311 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7312 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7313 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7314 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7315 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7316 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7317 {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 },
7318 {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 },
7319 {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 },
7320 {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 },
7321 {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 },
7322 {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 },
7323 {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 },
7324 {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 },
7325 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7326 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7327 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7328 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7329 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7330 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7331 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7332 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7333 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7334 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7337 const struct fragment_pipeline glsl_fragment_pipe =
7339 glsl_fragment_pipe_enable,
7340 glsl_fragment_pipe_get_caps,
7341 glsl_fragment_pipe_alloc,
7342 glsl_fragment_pipe_free,
7343 shader_glsl_color_fixup_supported,
7344 glsl_fragment_pipe_state_template,