wined3d: Pass position_transformed and gl_info to find_ps_compile_args.
[wine.git] / dlls / wined3d / glsl_shader.c
blob24130957eeaef346111917a016cf925444955373
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>
38 #include "wined3d_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
42 WINE_DECLARE_DEBUG_CHANNEL(d3d);
43 WINE_DECLARE_DEBUG_CHANNEL(winediag);
45 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
46 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
47 #define WINED3D_GLSL_SAMPLE_LOD 0x4
48 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
50 struct glsl_dst_param
52 char reg_name[150];
53 char mask_str[6];
56 struct glsl_src_param
58 char reg_name[150];
59 char param_str[200];
62 struct glsl_sample_function
64 const char *name;
65 DWORD coord_mask;
68 enum heap_node_op
70 HEAP_NODE_TRAVERSE_LEFT,
71 HEAP_NODE_TRAVERSE_RIGHT,
72 HEAP_NODE_POP,
75 struct constant_entry
77 unsigned int idx;
78 unsigned int version;
81 struct constant_heap
83 struct constant_entry *entries;
84 BOOL *contained;
85 unsigned int *positions;
86 unsigned int size;
89 /* GLSL shader private data */
90 struct shader_glsl_priv {
91 struct wined3d_shader_buffer shader_buffer;
92 struct wine_rb_tree program_lookup;
93 struct constant_heap vconst_heap;
94 struct constant_heap pconst_heap;
95 unsigned char *stack;
96 GLhandleARB depth_blt_program_full[tex_type_count];
97 GLhandleARB depth_blt_program_masked[tex_type_count];
98 UINT next_constant_version;
100 const struct wined3d_vertex_pipe_ops *vertex_pipe;
101 const struct fragment_pipeline *fragment_pipe;
102 struct wine_rb_tree ffp_vertex_shaders;
103 struct wine_rb_tree ffp_fragment_shaders;
104 BOOL ffp_proj_control;
107 struct glsl_vs_program
109 struct list shader_entry;
110 GLhandleARB id;
111 GLenum vertex_color_clamp;
112 GLint *uniform_f_locations;
113 GLint uniform_i_locations[MAX_CONST_I];
114 GLint pos_fixup_location;
117 struct glsl_gs_program
119 struct list shader_entry;
120 GLhandleARB id;
123 struct glsl_ps_program
125 struct list shader_entry;
126 GLhandleARB id;
127 GLint *uniform_f_locations;
128 GLint uniform_i_locations[MAX_CONST_I];
129 GLint bumpenv_mat_location[MAX_TEXTURES];
130 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
131 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
132 GLint tex_factor_location;
133 GLint specular_enable_location;
134 GLint ycorrection_location;
135 GLint np2_fixup_location;
136 const struct ps_np2fixup_info *np2_fixup_info;
139 /* Struct to maintain data about a linked GLSL program */
140 struct glsl_shader_prog_link
142 struct wine_rb_entry program_lookup_entry;
143 struct glsl_vs_program vs;
144 struct glsl_gs_program gs;
145 struct glsl_ps_program ps;
146 GLhandleARB programId;
147 DWORD constant_update_mask;
148 UINT constant_version;
151 struct glsl_program_key
153 GLhandleARB vs_id;
154 GLhandleARB gs_id;
155 GLhandleARB ps_id;
158 struct shader_glsl_ctx_priv {
159 const struct vs_compile_args *cur_vs_args;
160 const struct ps_compile_args *cur_ps_args;
161 struct ps_np2fixup_info *cur_np2fixup_info;
164 struct glsl_context_data
166 struct glsl_shader_prog_link *glsl_program;
169 struct glsl_ps_compiled_shader
171 struct ps_compile_args args;
172 struct ps_np2fixup_info np2fixup;
173 GLhandleARB prgId;
176 struct glsl_vs_compiled_shader
178 struct vs_compile_args args;
179 GLhandleARB prgId;
182 struct glsl_gs_compiled_shader
184 GLhandleARB id;
187 struct glsl_shader_private
189 union
191 struct glsl_vs_compiled_shader *vs;
192 struct glsl_gs_compiled_shader *gs;
193 struct glsl_ps_compiled_shader *ps;
194 } gl_shaders;
195 UINT num_gl_shaders, shader_array_size;
198 struct glsl_ffp_vertex_shader
200 struct wined3d_ffp_vs_desc desc;
201 GLhandleARB id;
202 struct list linked_programs;
205 struct glsl_ffp_fragment_shader
207 struct ffp_frag_desc entry;
208 GLhandleARB id;
209 struct list linked_programs;
212 struct glsl_ffp_destroy_ctx
214 struct shader_glsl_priv *priv;
215 const struct wined3d_gl_info *gl_info;
218 static const char *debug_gl_shader_type(GLenum type)
220 switch (type)
222 #define WINED3D_TO_STR(u) case u: return #u
223 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
224 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
225 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
226 #undef WINED3D_TO_STR
227 default:
228 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
232 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
234 switch (type)
236 case WINED3D_SHADER_TYPE_VERTEX:
237 return "vs";
239 case WINED3D_SHADER_TYPE_GEOMETRY:
240 return "gs";
242 case WINED3D_SHADER_TYPE_PIXEL:
243 return "ps";
245 default:
246 FIXME("Unhandled shader type %#x.\n", type);
247 return "unknown";
251 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
253 char str[4][16];
255 wined3d_ftoa(values[0], str[0]);
256 wined3d_ftoa(values[1], str[1]);
257 wined3d_ftoa(values[2], str[2]);
258 wined3d_ftoa(values[3], str[3]);
259 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
262 /* Extract a line from the info log.
263 * Note that this modifies the source string. */
264 static char *get_info_log_line(char **ptr)
266 char *p, *q;
268 p = *ptr;
269 if (!(q = strstr(p, "\n")))
271 if (!*p) return NULL;
272 *ptr += strlen(p);
273 return p;
275 *q = '\0';
276 *ptr = q + 1;
278 return p;
281 /* Context activation is done by the caller. */
282 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
284 int infologLength = 0;
285 char *infoLog;
287 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
288 return;
290 GL_EXTCALL(glGetObjectParameterivARB(obj,
291 GL_OBJECT_INFO_LOG_LENGTH_ARB,
292 &infologLength));
294 /* A size of 1 is just a null-terminated string, so the log should be bigger than
295 * that if there are errors. */
296 if (infologLength > 1)
298 char *ptr, *line;
300 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
301 /* The info log is supposed to be zero-terminated, but at least some
302 * versions of fglrx don't terminate the string properly. The reported
303 * length does include the terminator, so explicitly set it to zero
304 * here. */
305 infoLog[infologLength - 1] = 0;
306 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
308 ptr = infoLog;
309 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
311 WARN("Info log received from GLSL shader #%u:\n", obj);
312 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
314 else
316 FIXME("Info log received from GLSL shader #%u:\n", obj);
317 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
319 HeapFree(GetProcessHeap(), 0, infoLog);
323 /* Context activation is done by the caller. */
324 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
326 TRACE("Compiling shader object %u.\n", shader);
327 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
328 checkGLcall("glShaderSourceARB");
329 GL_EXTCALL(glCompileShaderARB(shader));
330 checkGLcall("glCompileShaderARB");
331 print_glsl_info_log(gl_info, shader);
334 /* Context activation is done by the caller. */
335 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
337 GLint i, object_count, source_size = -1;
338 GLhandleARB *objects;
339 char *source = NULL;
341 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
342 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
343 if (!objects)
345 ERR("Failed to allocate object array memory.\n");
346 return;
349 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
350 for (i = 0; i < object_count; ++i)
352 char *ptr, *line;
353 GLint tmp;
355 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
357 if (source_size < tmp)
359 HeapFree(GetProcessHeap(), 0, source);
361 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
362 if (!source)
364 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
365 HeapFree(GetProcessHeap(), 0, objects);
366 return;
368 source_size = tmp;
371 FIXME("Object %u:\n", objects[i]);
372 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
373 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
374 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
375 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
376 FIXME("\n");
378 ptr = source;
379 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
380 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
381 FIXME("\n");
384 HeapFree(GetProcessHeap(), 0, source);
385 HeapFree(GetProcessHeap(), 0, objects);
388 /* Context activation is done by the caller. */
389 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
391 GLint tmp;
393 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
395 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
396 if (tmp == GL_PROGRAM_OBJECT_ARB)
398 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
399 if (!tmp)
401 FIXME("Program %u link status invalid.\n", program);
402 shader_glsl_dump_program_source(gl_info, program);
406 print_glsl_info_log(gl_info, program);
409 /* Context activation is done by the caller. */
410 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
411 const DWORD *tex_unit_map, GLhandleARB programId)
413 GLint name_loc;
414 char sampler_name[20];
415 unsigned int i;
417 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
419 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
420 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
421 if (name_loc != -1) {
422 DWORD mapped_unit = tex_unit_map[i];
423 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
425 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
426 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
427 checkGLcall("glUniform1iARB");
428 } else {
429 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
435 /* Context activation is done by the caller. */
436 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
437 const DWORD *tex_unit_map, GLhandleARB programId)
439 GLint name_loc;
440 char sampler_name[20];
441 unsigned int i;
443 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
445 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
446 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
447 if (name_loc != -1) {
448 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
449 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
451 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
452 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
453 checkGLcall("glUniform1iARB");
454 } else {
455 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
461 /* Context activation is done by the caller. */
462 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
463 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
465 unsigned int start = ~0U, end = 0;
466 int stack_idx = 0;
467 unsigned int heap_idx = 1;
468 unsigned int idx;
470 if (heap->entries[heap_idx].version <= version) return;
472 idx = heap->entries[heap_idx].idx;
473 if (constant_locations[idx] != -1)
474 start = end = idx;
475 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
477 while (stack_idx >= 0)
479 /* Note that we fall through to the next case statement. */
480 switch(stack[stack_idx])
482 case HEAP_NODE_TRAVERSE_LEFT:
484 unsigned int left_idx = heap_idx << 1;
485 if (left_idx < heap->size && heap->entries[left_idx].version > version)
487 heap_idx = left_idx;
488 idx = heap->entries[heap_idx].idx;
489 if (constant_locations[idx] != -1)
491 if (start > idx)
492 start = idx;
493 if (end < idx)
494 end = idx;
497 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
498 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
499 break;
503 case HEAP_NODE_TRAVERSE_RIGHT:
505 unsigned int right_idx = (heap_idx << 1) + 1;
506 if (right_idx < heap->size && heap->entries[right_idx].version > version)
508 heap_idx = right_idx;
509 idx = heap->entries[heap_idx].idx;
510 if (constant_locations[idx] != -1)
512 if (start > idx)
513 start = idx;
514 if (end < idx)
515 end = idx;
518 stack[stack_idx++] = HEAP_NODE_POP;
519 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
520 break;
524 case HEAP_NODE_POP:
525 heap_idx >>= 1;
526 --stack_idx;
527 break;
530 if (start <= end)
531 GL_EXTCALL(glUniform4fvARB(constant_locations[start], end - start + 1, &constants[start * 4]));
532 checkGLcall("walk_constant_heap()");
535 /* Context activation is done by the caller. */
536 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
538 GLfloat clamped_constant[4];
540 if (location == -1) return;
542 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
543 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
544 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
545 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
547 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
550 /* Context activation is done by the caller. */
551 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
552 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
554 int stack_idx = 0;
555 unsigned int heap_idx = 1;
556 unsigned int idx;
558 if (heap->entries[heap_idx].version <= version) return;
560 idx = heap->entries[heap_idx].idx;
561 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
562 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
564 while (stack_idx >= 0)
566 /* Note that we fall through to the next case statement. */
567 switch(stack[stack_idx])
569 case HEAP_NODE_TRAVERSE_LEFT:
571 unsigned int left_idx = heap_idx << 1;
572 if (left_idx < heap->size && heap->entries[left_idx].version > version)
574 heap_idx = left_idx;
575 idx = heap->entries[heap_idx].idx;
576 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
578 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
579 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
580 break;
584 case HEAP_NODE_TRAVERSE_RIGHT:
586 unsigned int right_idx = (heap_idx << 1) + 1;
587 if (right_idx < heap->size && heap->entries[right_idx].version > version)
589 heap_idx = right_idx;
590 idx = heap->entries[heap_idx].idx;
591 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
593 stack[stack_idx++] = HEAP_NODE_POP;
594 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
595 break;
599 case HEAP_NODE_POP:
600 heap_idx >>= 1;
601 --stack_idx;
602 break;
605 checkGLcall("walk_constant_heap_clamped()");
608 /* Context activation is done by the caller. */
609 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
610 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
611 unsigned char *stack, UINT version)
613 const struct wined3d_shader_lconst *lconst;
615 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
616 if (shader->reg_maps.shader_version.major == 1
617 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
618 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
619 else
620 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
622 if (!shader->load_local_constsF)
624 TRACE("No need to load local float constants for this shader\n");
625 return;
628 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
629 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
631 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
633 checkGLcall("glUniform4fvARB()");
636 /* Context activation is done by the caller. */
637 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
638 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
640 unsigned int i;
641 struct list* ptr;
643 for (i = 0; constants_set; constants_set >>= 1, ++i)
645 if (!(constants_set & 1)) continue;
647 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
648 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
650 /* We found this uniform name in the program - go ahead and send the data */
651 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
652 checkGLcall("glUniform4ivARB");
655 /* Load immediate constants */
656 ptr = list_head(&shader->constantsI);
657 while (ptr)
659 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
660 unsigned int idx = lconst->idx;
661 const GLint *values = (const GLint *)lconst->value;
663 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
664 values[0], values[1], values[2], values[3]);
666 /* We found this uniform name in the program - go ahead and send the data */
667 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
668 checkGLcall("glUniform4ivARB");
669 ptr = list_next(&shader->constantsI, ptr);
673 /* Context activation is done by the caller. */
674 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
675 GLhandleARB programId, const BOOL *constants, WORD constants_set)
677 GLint tmp_loc;
678 unsigned int i;
679 char tmp_name[10];
680 const char *prefix;
681 struct list* ptr;
683 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
685 /* TODO: Benchmark and see if it would be beneficial to store the
686 * locations of the constants to avoid looking up each time */
687 for (i = 0; constants_set; constants_set >>= 1, ++i)
689 if (!(constants_set & 1)) continue;
691 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
693 /* TODO: Benchmark and see if it would be beneficial to store the
694 * locations of the constants to avoid looking up each time */
695 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
696 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
697 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
700 /* Load immediate constants */
701 ptr = list_head(&shader->constantsB);
702 while (ptr)
704 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
705 unsigned int idx = lconst->idx;
706 const GLint *values = (const GLint *)lconst->value;
708 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
710 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
711 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
712 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
713 ptr = list_next(&shader->constantsB, ptr);
716 checkGLcall("shader_glsl_load_constantsB()");
719 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
721 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
724 /* Context activation is done by the caller (state handler). */
725 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
726 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
728 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
729 UINT fixup = ps->np2_fixup_info->active;
730 UINT i;
732 for (i = 0; fixup; fixup >>= 1, ++i)
734 const struct wined3d_texture *tex = state->textures[i];
735 unsigned char idx = ps->np2_fixup_info->idx[i];
736 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
738 if (!tex)
740 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
741 continue;
744 if (idx % 2)
746 tex_dim[2] = tex->pow2_matrix[0];
747 tex_dim[3] = tex->pow2_matrix[5];
749 else
751 tex_dim[0] = tex->pow2_matrix[0];
752 tex_dim[1] = tex->pow2_matrix[5];
756 GL_EXTCALL(glUniform4fvARB(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
759 /* Context activation is done by the caller (state handler). */
760 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
761 const struct wined3d_state *state)
763 const struct glsl_context_data *ctx_data = context->shader_backend_data;
764 const struct wined3d_shader *vshader = state->vertex_shader;
765 const struct wined3d_shader *pshader = state->pixel_shader;
766 const struct wined3d_gl_info *gl_info = context->gl_info;
767 struct shader_glsl_priv *priv = shader_priv;
768 float position_fixup[4];
769 DWORD update_mask = 0;
771 GLhandleARB programId;
772 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
773 UINT constant_version;
774 int i;
776 if (!prog) {
777 /* No GLSL program set - nothing to do. */
778 return;
780 programId = prog->programId;
781 constant_version = prog->constant_version;
782 update_mask = context->constant_update_mask & prog->constant_update_mask;
784 if (update_mask & WINED3D_SHADER_CONST_VS_F)
785 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
786 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
788 if (update_mask & WINED3D_SHADER_CONST_VS_I)
789 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
790 vshader->reg_maps.integer_constants);
792 if (update_mask & WINED3D_SHADER_CONST_VS_B)
793 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
794 vshader->reg_maps.boolean_constants);
796 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
798 shader_get_position_fixup(context, state, position_fixup);
799 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
800 checkGLcall("glUniform4fvARB");
803 if (update_mask & WINED3D_SHADER_CONST_PS_F)
804 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
805 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
807 if (update_mask & WINED3D_SHADER_CONST_PS_I)
808 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
809 pshader->reg_maps.integer_constants);
811 if (update_mask & WINED3D_SHADER_CONST_PS_B)
812 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
813 pshader->reg_maps.boolean_constants);
815 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
817 for (i = 0; i < MAX_TEXTURES; ++i)
819 if (prog->ps.bumpenv_mat_location[i] == -1)
820 continue;
822 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
823 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
825 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
827 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1,
828 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
829 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1,
830 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
834 checkGLcall("bump env uniforms");
837 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
839 float correction_params[4];
841 if (context->render_offscreen)
843 correction_params[0] = 0.0f;
844 correction_params[1] = 1.0f;
845 } else {
846 /* position is window relative, not viewport relative */
847 correction_params[0] = (float) context->current_rt->resource.height;
848 correction_params[1] = -1.0f;
850 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
853 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
854 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
856 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
858 float col[4];
860 if (prog->ps.tex_factor_location != -1)
862 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
863 GL_EXTCALL(glUniform4fvARB(prog->ps.tex_factor_location, 1, col));
866 if (state->render_states[WINED3D_RS_SPECULARENABLE])
867 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
868 else
869 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
871 checkGLcall("fixed function uniforms");
874 if (priv->next_constant_version == UINT_MAX)
876 TRACE("Max constant version reached, resetting to 0.\n");
877 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
878 priv->next_constant_version = 1;
880 else
882 prog->constant_version = priv->next_constant_version++;
886 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
888 struct constant_entry *entries = heap->entries;
889 unsigned int *positions = heap->positions;
890 unsigned int heap_idx, parent_idx;
892 if (!heap->contained[idx])
894 heap_idx = heap->size++;
895 heap->contained[idx] = TRUE;
897 else
899 heap_idx = positions[idx];
902 while (heap_idx > 1)
904 parent_idx = heap_idx >> 1;
906 if (new_version <= entries[parent_idx].version) break;
908 entries[heap_idx] = entries[parent_idx];
909 positions[entries[parent_idx].idx] = heap_idx;
910 heap_idx = parent_idx;
913 entries[heap_idx].version = new_version;
914 entries[heap_idx].idx = idx;
915 positions[idx] = heap_idx;
918 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
920 struct shader_glsl_priv *priv = device->shader_priv;
921 struct constant_heap *heap = &priv->vconst_heap;
922 UINT i;
924 for (i = start; i < count + start; ++i)
926 update_heap_entry(heap, i, priv->next_constant_version);
929 for (i = 0; i < device->context_count; ++i)
931 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
935 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
937 struct shader_glsl_priv *priv = device->shader_priv;
938 struct constant_heap *heap = &priv->pconst_heap;
939 UINT i;
941 for (i = start; i < count + start; ++i)
943 update_heap_entry(heap, i, priv->next_constant_version);
946 for (i = 0; i < device->context_count; ++i)
948 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
952 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
954 unsigned int ret = gl_info->limits.glsl_varyings / 4;
955 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
956 if(shader_major > 3) return ret;
958 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
959 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
960 return ret;
963 /** Generate the variable & register declarations for the GLSL output target */
964 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
965 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
966 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
968 const struct wined3d_shader_version *version = &reg_maps->shader_version;
969 const struct wined3d_state *state = &shader->device->state;
970 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
971 const struct wined3d_gl_info *gl_info = context->gl_info;
972 const struct wined3d_fb_state *fb = &shader->device->fb;
973 unsigned int i, extra_constants_needed = 0;
974 const struct wined3d_shader_lconst *lconst;
975 const char *prefix;
976 DWORD map;
978 prefix = shader_glsl_get_prefix(version->type);
980 /* Prototype the subroutines */
981 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
983 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
986 /* Declare the constants (aka uniforms) */
987 if (shader->limits.constant_float > 0)
989 unsigned max_constantsF;
991 /* Unless the shader uses indirect addressing, always declare the
992 * maximum array size and ignore that we need some uniforms privately.
993 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
994 * and immediate values, still declare VC[256]. If the shader needs
995 * more uniforms than we have it won't work in any case. If it uses
996 * less, the compiler will figure out which uniforms are really used
997 * and strip them out. This allows a shader to use c255 on a dx9 card,
998 * as long as it doesn't also use all the other constants.
1000 * If the shader uses indirect addressing the compiler must assume
1001 * that all declared uniforms are used. In this case, declare only the
1002 * amount that we're assured to have.
1004 * Thus we run into problems in these two cases:
1005 * 1) The shader really uses more uniforms than supported.
1006 * 2) The shader uses indirect addressing, less constants than
1007 * supported, but uses a constant index > #supported consts. */
1008 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1010 /* No indirect addressing here. */
1011 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1013 else
1015 if (reg_maps->usesrelconstF)
1017 /* Subtract the other potential uniforms from the max
1018 * available (bools, ints, and 1 row of projection matrix).
1019 * Subtract another uniform for immediate values, which have
1020 * to be loaded via uniform by the driver as well. The shader
1021 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1022 * shader code, so one vec4 should be enough. (Unfortunately
1023 * the Nvidia driver doesn't store 128 and -128 in one float).
1025 * Writing gl_ClipVertex requires one uniform for each
1026 * clipplane as well. */
1027 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1028 if(ctx_priv->cur_vs_args->clip_enabled)
1030 max_constantsF -= gl_info->limits.clipplanes;
1032 max_constantsF -= count_bits(reg_maps->integer_constants);
1033 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1034 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1035 * for now take this into account when calculating the number of available constants
1037 max_constantsF -= count_bits(reg_maps->boolean_constants);
1038 /* Set by driver quirks in directx.c */
1039 max_constantsF -= gl_info->reserved_glsl_constants;
1041 if (max_constantsF < shader->limits.constant_float)
1043 static unsigned int once;
1045 if (!once++)
1046 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1047 " it may not render correctly.\n");
1048 else
1049 WARN("The hardware does not support enough uniform components to run this shader.\n");
1052 else
1054 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1057 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1058 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1061 /* Always declare the full set of constants, the compiler can remove the
1062 * unused ones because d3d doesn't (yet) support indirect int and bool
1063 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1064 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1065 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1067 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1068 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1070 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1072 if (reg_maps->cb_sizes[i])
1073 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1076 /* Declare texture samplers */
1077 for (i = 0; i < shader->limits.sampler; ++i)
1079 if (reg_maps->sampler_type[i])
1081 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1082 BOOL tex_rect;
1084 switch (reg_maps->sampler_type[i])
1086 case WINED3DSTT_1D:
1087 if (shadow_sampler)
1088 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1089 else
1090 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1091 break;
1092 case WINED3DSTT_2D:
1093 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1094 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1095 if (shadow_sampler)
1097 if (tex_rect)
1098 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1099 else
1100 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1102 else
1104 if (tex_rect)
1105 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1106 else
1107 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1109 break;
1110 case WINED3DSTT_CUBE:
1111 if (shadow_sampler)
1112 FIXME("Unsupported Cube shadow sampler.\n");
1113 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1114 break;
1115 case WINED3DSTT_VOLUME:
1116 if (shadow_sampler)
1117 FIXME("Unsupported 3D shadow sampler.\n");
1118 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1119 break;
1120 default:
1121 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1122 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1123 break;
1128 /* Declare uniforms for NP2 texcoord fixup:
1129 * This is NOT done inside the loop that declares the texture samplers
1130 * since the NP2 fixup code is currently only used for the GeforceFX
1131 * series and when forcing the ARB_npot extension off. Modern cards just
1132 * skip the code anyway, so put it inside a separate loop. */
1133 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1135 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1136 UINT cur = 0;
1138 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1139 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1140 * samplerNP2Fixup stores texture dimensions and is updated through
1141 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1143 for (i = 0; i < shader->limits.sampler; ++i)
1145 if (reg_maps->sampler_type[i])
1147 if (!(ps_args->np2_fixup & (1 << i))) continue;
1149 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1150 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1151 continue;
1154 fixup->idx[i] = cur++;
1158 fixup->num_consts = (cur + 1) >> 1;
1159 fixup->active = ps_args->np2_fixup;
1160 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1163 /* Declare address variables */
1164 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1166 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1169 /* Declare texture coordinate temporaries and initialize them */
1170 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1172 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1175 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1177 /* Declare attributes. */
1178 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1180 if (map & 1)
1181 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1184 shader_addline(buffer, "uniform vec4 posFixup;\n");
1185 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1187 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1189 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1191 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1193 if (version->major >= 3)
1195 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1197 if (use_vs(state))
1198 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1199 else
1200 /* TODO: Write a replacement shader for the fixed function
1201 * vertex pipeline, so this isn't needed. For fixed function
1202 * vertex processing + 3.0 pixel shader we need a separate
1203 * function in the pixel shader that reads the fixed function
1204 * color into the packed input registers. */
1205 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1208 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1210 if (!(map & 1))
1211 continue;
1213 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1215 if (reg_maps->luminanceparams & (1 << i))
1217 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1218 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1219 extra_constants_needed++;
1222 extra_constants_needed++;
1225 if (ps_args->srgb_correction)
1227 shader_addline(buffer, "const vec4 srgb_const0 = ");
1228 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1229 shader_addline(buffer, ";\n");
1230 shader_addline(buffer, "const vec4 srgb_const1 = ");
1231 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1232 shader_addline(buffer, ";\n");
1234 if (reg_maps->vpos || reg_maps->usesdsy)
1236 if (shader->limits.constant_float + extra_constants_needed
1237 + 1 < gl_info->limits.glsl_ps_float_constants)
1239 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1240 extra_constants_needed++;
1242 else
1244 float ycorrection[] =
1246 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1247 context->render_offscreen ? 1.0f : -1.0f,
1248 0.0f,
1249 0.0f,
1252 /* This happens because we do not have proper tracking of the
1253 * constant registers that are actually used, only the max
1254 * limit of the shader version. */
1255 FIXME("Cannot find a free uniform for vpos correction params\n");
1256 shader_addline(buffer, "const vec4 ycorrection = ");
1257 shader_glsl_append_imm_vec4(buffer, ycorrection);
1258 shader_addline(buffer, ";\n");
1260 shader_addline(buffer, "vec4 vpos;\n");
1264 /* Declare output register temporaries */
1265 if (shader->limits.packed_output)
1266 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1268 /* Declare temporary variables */
1269 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1271 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1274 /* Declare loop registers aLx */
1275 if (version->major < 4)
1277 for (i = 0; i < reg_maps->loop_depth; ++i)
1279 shader_addline(buffer, "int aL%u;\n", i);
1280 shader_addline(buffer, "int tmpInt%u;\n", i);
1284 /* Temporary variables for matrix operations */
1285 shader_addline(buffer, "vec4 tmp0;\n");
1286 shader_addline(buffer, "vec4 tmp1;\n");
1288 if (!shader->load_local_constsF)
1290 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1292 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1293 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1294 shader_addline(buffer, ";\n");
1298 /* Start the main program. */
1299 shader_addline(buffer, "void main()\n{\n");
1301 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1302 * add approximately 0.5. This causes off-by-one problems as spotted by
1303 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1304 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1305 * causes precision troubles when we just subtract 0.5.
1307 * To deal with that, just floor() the position. This will eliminate the
1308 * fraction on all cards.
1310 * TODO: Test how this behaves with multisampling.
1312 * An advantage of floor is that it works even if the driver doesn't add
1313 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1314 * to return in gl_FragCoord, even though coordinates specify the pixel
1315 * centers instead of the pixel corners. This code will behave correctly
1316 * on drivers that returns integer values. */
1317 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1318 shader_addline(buffer,
1319 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1322 /*****************************************************************************
1323 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1325 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1326 ****************************************************************************/
1328 /* Prototypes */
1329 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1330 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1332 /** Used for opcode modifiers - They multiply the result by the specified amount */
1333 static const char * const shift_glsl_tab[] = {
1334 "", /* 0 (none) */
1335 "2.0 * ", /* 1 (x2) */
1336 "4.0 * ", /* 2 (x4) */
1337 "8.0 * ", /* 3 (x8) */
1338 "16.0 * ", /* 4 (x16) */
1339 "32.0 * ", /* 5 (x32) */
1340 "", /* 6 (x64) */
1341 "", /* 7 (x128) */
1342 "", /* 8 (d256) */
1343 "", /* 9 (d128) */
1344 "", /* 10 (d64) */
1345 "", /* 11 (d32) */
1346 "0.0625 * ", /* 12 (d16) */
1347 "0.125 * ", /* 13 (d8) */
1348 "0.25 * ", /* 14 (d4) */
1349 "0.5 * " /* 15 (d2) */
1352 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1353 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1354 const char *in_reg, const char *in_regswizzle, char *out_str)
1356 out_str[0] = 0;
1358 switch (src_modifier)
1360 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1361 case WINED3DSPSM_DW:
1362 case WINED3DSPSM_NONE:
1363 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1364 break;
1365 case WINED3DSPSM_NEG:
1366 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1367 break;
1368 case WINED3DSPSM_NOT:
1369 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1370 break;
1371 case WINED3DSPSM_BIAS:
1372 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1373 break;
1374 case WINED3DSPSM_BIASNEG:
1375 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1376 break;
1377 case WINED3DSPSM_SIGN:
1378 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1379 break;
1380 case WINED3DSPSM_SIGNNEG:
1381 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1382 break;
1383 case WINED3DSPSM_COMP:
1384 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1385 break;
1386 case WINED3DSPSM_X2:
1387 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1388 break;
1389 case WINED3DSPSM_X2NEG:
1390 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1391 break;
1392 case WINED3DSPSM_ABS:
1393 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1394 break;
1395 case WINED3DSPSM_ABSNEG:
1396 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1397 break;
1398 default:
1399 FIXME("Unhandled modifier %u\n", src_modifier);
1400 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1404 /** Writes the GLSL variable name that corresponds to the register that the
1405 * DX opcode parameter is trying to access */
1406 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1407 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1409 /* oPos, oFog and oPts in D3D */
1410 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1412 const struct wined3d_shader *shader = ins->ctx->shader;
1413 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1414 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1415 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1416 const char *prefix = shader_glsl_get_prefix(version->type);
1417 struct glsl_src_param rel_param0, rel_param1;
1418 char imm_str[4][16];
1420 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1421 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1422 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1423 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1424 *is_color = FALSE;
1426 switch (reg->type)
1428 case WINED3DSPR_TEMP:
1429 sprintf(register_name, "R%u", reg->idx[0].offset);
1430 break;
1432 case WINED3DSPR_INPUT:
1433 /* vertex shaders */
1434 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1436 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1437 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1438 *is_color = TRUE;
1439 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1440 break;
1443 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1445 if (reg->idx[0].rel_addr)
1447 if (reg->idx[1].rel_addr)
1448 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1449 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1450 else
1451 sprintf(register_name, "gs_in[%s + %u][%u]",
1452 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1454 else if (reg->idx[1].rel_addr)
1455 sprintf(register_name, "gs_in[%u][%s + %u]",
1456 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1457 else
1458 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1459 break;
1462 /* pixel shaders >= 3.0 */
1463 if (version->major >= 3)
1465 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1466 unsigned int in_count = vec4_varyings(version->major, gl_info);
1468 if (reg->idx[0].rel_addr)
1470 /* Removing a + 0 would be an obvious optimization, but
1471 * OS X doesn't see the NOP operation there. */
1472 if (idx)
1474 if (shader->u.ps.declared_in_count > in_count)
1476 sprintf(register_name,
1477 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1478 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1479 prefix, rel_param0.param_str, idx);
1481 else
1483 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1486 else
1488 if (shader->u.ps.declared_in_count > in_count)
1490 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1491 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1492 prefix, rel_param0.param_str);
1494 else
1496 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1500 else
1502 if (idx == in_count) sprintf(register_name, "gl_Color");
1503 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1504 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1507 else
1509 if (!reg->idx[0].offset)
1510 strcpy(register_name, "gl_Color");
1511 else
1512 strcpy(register_name, "gl_SecondaryColor");
1513 break;
1515 break;
1517 case WINED3DSPR_CONST:
1519 /* Relative addressing */
1520 if (reg->idx[0].rel_addr)
1522 if (reg->idx[0].offset)
1523 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1524 else
1525 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1527 else
1529 if (shader_constant_is_local(shader, reg->idx[0].offset))
1530 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1531 else
1532 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1535 break;
1537 case WINED3DSPR_CONSTINT:
1538 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1539 break;
1541 case WINED3DSPR_CONSTBOOL:
1542 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1543 break;
1545 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1546 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1547 sprintf(register_name, "T%u", reg->idx[0].offset);
1548 else
1549 sprintf(register_name, "A%u", reg->idx[0].offset);
1550 break;
1552 case WINED3DSPR_LOOP:
1553 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1554 break;
1556 case WINED3DSPR_SAMPLER:
1557 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1558 break;
1560 case WINED3DSPR_COLOROUT:
1561 if (reg->idx[0].offset >= gl_info->limits.buffers)
1562 WARN("Write to render target %u, only %d supported.\n",
1563 reg->idx[0].offset, gl_info->limits.buffers);
1565 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1566 break;
1568 case WINED3DSPR_RASTOUT:
1569 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1570 break;
1572 case WINED3DSPR_DEPTHOUT:
1573 sprintf(register_name, "gl_FragDepth");
1574 break;
1576 case WINED3DSPR_ATTROUT:
1577 if (!reg->idx[0].offset)
1578 sprintf(register_name, "%s_out[8]", prefix);
1579 else
1580 sprintf(register_name, "%s_out[9]", prefix);
1581 break;
1583 case WINED3DSPR_TEXCRDOUT:
1584 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1585 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1586 break;
1588 case WINED3DSPR_MISCTYPE:
1589 if (!reg->idx[0].offset)
1591 /* vPos */
1592 sprintf(register_name, "vpos");
1594 else if (reg->idx[0].offset == 1)
1596 /* Note that gl_FrontFacing is a bool, while vFace is
1597 * a float for which the sign determines front/back */
1598 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1600 else
1602 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1603 sprintf(register_name, "unrecognized_register");
1605 break;
1607 case WINED3DSPR_IMMCONST:
1608 switch (reg->immconst_type)
1610 case WINED3D_IMMCONST_SCALAR:
1611 switch (reg->data_type)
1613 case WINED3D_DATA_FLOAT:
1614 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1615 break;
1616 case WINED3D_DATA_INT:
1617 sprintf(register_name, "%#x", reg->immconst_data[0]);
1618 break;
1619 case WINED3D_DATA_RESOURCE:
1620 case WINED3D_DATA_SAMPLER:
1621 case WINED3D_DATA_UINT:
1622 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1623 break;
1624 default:
1625 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1626 break;
1628 break;
1630 case WINED3D_IMMCONST_VEC4:
1631 switch (reg->data_type)
1633 case WINED3D_DATA_FLOAT:
1634 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1635 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1636 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1637 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1638 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1639 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1640 break;
1641 case WINED3D_DATA_INT:
1642 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1643 reg->immconst_data[0], reg->immconst_data[1],
1644 reg->immconst_data[2], reg->immconst_data[3]);
1645 break;
1646 case WINED3D_DATA_RESOURCE:
1647 case WINED3D_DATA_SAMPLER:
1648 case WINED3D_DATA_UINT:
1649 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1650 reg->immconst_data[0], reg->immconst_data[1],
1651 reg->immconst_data[2], reg->immconst_data[3]);
1652 break;
1653 default:
1654 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1655 break;
1657 break;
1659 default:
1660 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1661 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1663 break;
1665 case WINED3DSPR_CONSTBUFFER:
1666 if (reg->idx[1].rel_addr)
1667 sprintf(register_name, "%s_cb%u[%s + %u]",
1668 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1669 else
1670 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1671 break;
1673 case WINED3DSPR_PRIMID:
1674 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1675 break;
1677 default:
1678 FIXME("Unhandled register type %#x.\n", reg->type);
1679 sprintf(register_name, "unrecognized_register");
1680 break;
1684 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1686 *str++ = '.';
1687 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1688 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1689 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1690 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1691 *str = '\0';
1694 /* Get the GLSL write mask for the destination register */
1695 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1697 DWORD mask = param->write_mask;
1699 if (shader_is_scalar(&param->reg))
1701 mask = WINED3DSP_WRITEMASK_0;
1702 *write_mask = '\0';
1704 else
1706 shader_glsl_write_mask_to_str(mask, write_mask);
1709 return mask;
1712 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1713 unsigned int size = 0;
1715 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1716 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1717 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1718 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1720 return size;
1723 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1725 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1726 * but addressed as "rgba". To fix this we need to swap the register's x
1727 * and z components. */
1728 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1730 *str++ = '.';
1731 /* swizzle bits fields: wwzzyyxx */
1732 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1733 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1734 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1735 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1736 *str = '\0';
1739 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1740 BOOL fixup, DWORD mask, char *swizzle_str)
1742 if (shader_is_scalar(&param->reg))
1743 *swizzle_str = '\0';
1744 else
1745 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1748 /* From a given parameter token, generate the corresponding GLSL string.
1749 * Also, return the actual register name and swizzle in case the
1750 * caller needs this information as well. */
1751 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1752 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1754 BOOL is_color = FALSE;
1755 char swizzle_str[6];
1757 glsl_src->reg_name[0] = '\0';
1758 glsl_src->param_str[0] = '\0';
1759 swizzle_str[0] = '\0';
1761 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1762 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1764 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1766 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1768 else
1770 char param_str[200];
1772 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1774 switch (wined3d_src->reg.data_type)
1776 case WINED3D_DATA_FLOAT:
1777 sprintf(glsl_src->param_str, "%s", param_str);
1778 break;
1779 case WINED3D_DATA_INT:
1780 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1781 break;
1782 case WINED3D_DATA_RESOURCE:
1783 case WINED3D_DATA_SAMPLER:
1784 case WINED3D_DATA_UINT:
1785 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1786 break;
1787 default:
1788 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1789 sprintf(glsl_src->param_str, "%s", param_str);
1790 break;
1795 /* From a given parameter token, generate the corresponding GLSL string.
1796 * Also, return the actual register name and swizzle in case the
1797 * caller needs this information as well. */
1798 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1799 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1801 BOOL is_color = FALSE;
1803 glsl_dst->mask_str[0] = '\0';
1804 glsl_dst->reg_name[0] = '\0';
1806 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1807 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1810 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1811 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1812 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1814 struct glsl_dst_param glsl_dst;
1815 DWORD mask;
1817 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1819 switch (dst->reg.data_type)
1821 case WINED3D_DATA_FLOAT:
1822 shader_addline(buffer, "%s%s = %s(",
1823 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1824 break;
1825 case WINED3D_DATA_INT:
1826 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1827 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1828 break;
1829 case WINED3D_DATA_RESOURCE:
1830 case WINED3D_DATA_SAMPLER:
1831 case WINED3D_DATA_UINT:
1832 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1833 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1834 break;
1835 default:
1836 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1837 shader_addline(buffer, "%s%s = %s(",
1838 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1839 break;
1843 return mask;
1846 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1847 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1849 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1852 /** Process GLSL instruction modifiers */
1853 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1855 struct glsl_dst_param dst_param;
1856 DWORD modifiers;
1858 if (!ins->dst_count) return;
1860 modifiers = ins->dst[0].modifiers;
1861 if (!modifiers) return;
1863 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1865 if (modifiers & WINED3DSPDM_SATURATE)
1867 /* _SAT means to clamp the value of the register to between 0 and 1 */
1868 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1869 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1872 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1874 FIXME("_centroid modifier not handled\n");
1877 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1879 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1883 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1885 switch (op)
1887 case WINED3D_SHADER_REL_OP_GT: return ">";
1888 case WINED3D_SHADER_REL_OP_EQ: return "==";
1889 case WINED3D_SHADER_REL_OP_GE: return ">=";
1890 case WINED3D_SHADER_REL_OP_LT: return "<";
1891 case WINED3D_SHADER_REL_OP_NE: return "!=";
1892 case WINED3D_SHADER_REL_OP_LE: return "<=";
1893 default:
1894 FIXME("Unrecognized operator %#x.\n", op);
1895 return "(\?\?)";
1899 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1900 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1902 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1903 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1904 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1905 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1906 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1907 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1908 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1909 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1911 /* Note that there's no such thing as a projected cube texture. */
1912 switch(sampler_type) {
1913 case WINED3DSTT_1D:
1914 if (shadow)
1916 if (lod)
1918 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1920 else if (grad)
1922 if (gl_info->supported[EXT_GPU_SHADER4])
1923 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1924 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1925 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1926 else
1928 FIXME("Unsupported 1D shadow grad function.\n");
1929 sample_function->name = "unsupported1DGrad";
1932 else
1934 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1936 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1938 else
1940 if (lod)
1942 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1944 else if (grad)
1946 if (gl_info->supported[EXT_GPU_SHADER4])
1947 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1948 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1949 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1950 else
1952 FIXME("Unsupported 1D grad function.\n");
1953 sample_function->name = "unsupported1DGrad";
1956 else
1958 sample_function->name = projected ? "texture1DProj" : "texture1D";
1960 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1962 break;
1964 case WINED3DSTT_2D:
1965 if (shadow)
1967 if (texrect)
1969 if (lod)
1971 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1973 else if (grad)
1975 if (gl_info->supported[EXT_GPU_SHADER4])
1976 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1977 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1978 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1979 else
1981 FIXME("Unsupported RECT shadow grad function.\n");
1982 sample_function->name = "unsupported2DRectGrad";
1985 else
1987 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1990 else
1992 if (lod)
1994 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1996 else if (grad)
1998 if (gl_info->supported[EXT_GPU_SHADER4])
1999 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2000 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2001 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2002 else
2004 FIXME("Unsupported 2D shadow grad function.\n");
2005 sample_function->name = "unsupported2DGrad";
2008 else
2010 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2013 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2015 else
2017 if (texrect)
2019 if (lod)
2021 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2023 else if (grad)
2025 if (gl_info->supported[EXT_GPU_SHADER4])
2026 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2027 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2028 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2029 else
2031 FIXME("Unsupported RECT grad function.\n");
2032 sample_function->name = "unsupported2DRectGrad";
2035 else
2037 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2040 else
2042 if (lod)
2044 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2046 else if (grad)
2048 if (gl_info->supported[EXT_GPU_SHADER4])
2049 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2050 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2051 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2052 else
2054 FIXME("Unsupported 2D grad function.\n");
2055 sample_function->name = "unsupported2DGrad";
2058 else
2060 sample_function->name = projected ? "texture2DProj" : "texture2D";
2063 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2065 break;
2067 case WINED3DSTT_CUBE:
2068 if (shadow)
2070 FIXME("Unsupported Cube shadow function.\n");
2071 sample_function->name = "unsupportedCubeShadow";
2072 sample_function->coord_mask = 0;
2074 else
2076 if (lod)
2078 sample_function->name = "textureCubeLod";
2080 else if (grad)
2082 if (gl_info->supported[EXT_GPU_SHADER4])
2083 sample_function->name = "textureCubeGrad";
2084 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2085 sample_function->name = "textureCubeGradARB";
2086 else
2088 FIXME("Unsupported Cube grad function.\n");
2089 sample_function->name = "unsupportedCubeGrad";
2092 else
2094 sample_function->name = "textureCube";
2096 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2098 break;
2100 case WINED3DSTT_VOLUME:
2101 if (shadow)
2103 FIXME("Unsupported 3D shadow function.\n");
2104 sample_function->name = "unsupported3DShadow";
2105 sample_function->coord_mask = 0;
2107 else
2109 if (lod)
2111 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2113 else if (grad)
2115 if (gl_info->supported[EXT_GPU_SHADER4])
2116 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2117 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2118 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2119 else
2121 FIXME("Unsupported 3D grad function.\n");
2122 sample_function->name = "unsupported3DGrad";
2125 else
2127 sample_function->name = projected ? "texture3DProj" : "texture3D";
2129 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2131 break;
2133 default:
2134 sample_function->name = "";
2135 sample_function->coord_mask = 0;
2136 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2137 break;
2141 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2142 BOOL sign_fixup, enum fixup_channel_source channel_source)
2144 switch(channel_source)
2146 case CHANNEL_SOURCE_ZERO:
2147 strcat(arguments, "0.0");
2148 break;
2150 case CHANNEL_SOURCE_ONE:
2151 strcat(arguments, "1.0");
2152 break;
2154 case CHANNEL_SOURCE_X:
2155 strcat(arguments, reg_name);
2156 strcat(arguments, ".x");
2157 break;
2159 case CHANNEL_SOURCE_Y:
2160 strcat(arguments, reg_name);
2161 strcat(arguments, ".y");
2162 break;
2164 case CHANNEL_SOURCE_Z:
2165 strcat(arguments, reg_name);
2166 strcat(arguments, ".z");
2167 break;
2169 case CHANNEL_SOURCE_W:
2170 strcat(arguments, reg_name);
2171 strcat(arguments, ".w");
2172 break;
2174 default:
2175 FIXME("Unhandled channel source %#x\n", channel_source);
2176 strcat(arguments, "undefined");
2177 break;
2180 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2183 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2184 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2186 unsigned int mask_size, remaining;
2187 DWORD fixup_mask = 0;
2188 char arguments[256];
2189 char mask_str[6];
2191 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2192 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2193 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2194 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2195 if (!(mask &= fixup_mask))
2196 return;
2198 if (is_complex_fixup(fixup))
2200 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2201 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2202 return;
2205 shader_glsl_write_mask_to_str(mask, mask_str);
2206 mask_size = shader_glsl_get_write_mask_size(mask);
2208 arguments[0] = '\0';
2209 remaining = mask_size;
2210 if (mask & WINED3DSP_WRITEMASK_0)
2212 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2213 if (--remaining) strcat(arguments, ", ");
2215 if (mask & WINED3DSP_WRITEMASK_1)
2217 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2218 if (--remaining) strcat(arguments, ", ");
2220 if (mask & WINED3DSP_WRITEMASK_2)
2222 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2223 if (--remaining) strcat(arguments, ", ");
2225 if (mask & WINED3DSP_WRITEMASK_3)
2227 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2228 if (--remaining) strcat(arguments, ", ");
2231 if (mask_size > 1)
2232 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2233 else
2234 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2237 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2239 char reg_name[256];
2240 BOOL is_color;
2242 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2243 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2246 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2247 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2248 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2250 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2251 char dst_swizzle[6];
2252 struct color_fixup_desc fixup;
2253 BOOL np2_fixup = FALSE;
2254 va_list args;
2256 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2258 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2260 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2261 fixup = priv->cur_ps_args->color_fixup[sampler];
2263 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2264 if(bias) {
2265 FIXME("Biased sampling from NP2 textures is unsupported\n");
2266 } else {
2267 np2_fixup = TRUE;
2271 else
2273 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2276 shader_glsl_append_dst(ins->ctx->buffer, ins);
2278 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2279 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2281 va_start(args, coord_reg_fmt);
2282 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2283 va_end(args);
2285 if(bias) {
2286 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2287 } else {
2288 if (np2_fixup) {
2289 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2290 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2292 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2293 (idx % 2) ? "zw" : "xy", dst_swizzle);
2294 } else if(dx && dy) {
2295 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2296 } else {
2297 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2301 if(!is_identity_fixup(fixup)) {
2302 shader_glsl_color_correction(ins, fixup);
2306 /*****************************************************************************
2307 * Begin processing individual instruction opcodes
2308 ****************************************************************************/
2310 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2312 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2313 struct glsl_src_param src0_param;
2314 struct glsl_src_param src1_param;
2315 DWORD write_mask;
2316 const char *op;
2318 /* Determine the GLSL operator to use based on the opcode */
2319 switch (ins->handler_idx)
2321 case WINED3DSIH_ADD: op = "+"; break;
2322 case WINED3DSIH_AND: op = "&"; break;
2323 case WINED3DSIH_DIV: op = "/"; break;
2324 case WINED3DSIH_IADD: op = "+"; break;
2325 case WINED3DSIH_MUL: op = "*"; break;
2326 case WINED3DSIH_SUB: op = "-"; break;
2327 case WINED3DSIH_USHR: op = ">>"; break;
2328 case WINED3DSIH_XOR: op = "^"; break;
2329 default:
2330 op = "<unhandled operator>";
2331 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2332 break;
2335 write_mask = shader_glsl_append_dst(buffer, ins);
2336 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2337 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2338 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2341 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2343 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2344 struct glsl_src_param src0_param;
2345 struct glsl_src_param src1_param;
2346 unsigned int mask_size;
2347 DWORD write_mask;
2348 const char *op;
2350 write_mask = shader_glsl_append_dst(buffer, ins);
2351 mask_size = shader_glsl_get_write_mask_size(write_mask);
2352 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2353 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2355 if (mask_size > 1)
2357 switch (ins->handler_idx)
2359 case WINED3DSIH_EQ: op = "equal"; break;
2360 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2361 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2362 case WINED3DSIH_LT: op = "lessThan"; break;
2363 default:
2364 op = "<unhandled operator>";
2365 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2366 break;
2369 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2370 mask_size, op, src0_param.param_str, src1_param.param_str);
2372 else
2374 switch (ins->handler_idx)
2376 case WINED3DSIH_EQ: op = "=="; break;
2377 case WINED3DSIH_GE: op = ">="; break;
2378 case WINED3DSIH_IGE: op = ">="; break;
2379 case WINED3DSIH_LT: op = "<"; break;
2380 default:
2381 op = "<unhandled operator>";
2382 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2383 break;
2386 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2387 src0_param.param_str, op, src1_param.param_str);
2391 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2393 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2394 struct glsl_src_param src0_param;
2395 struct glsl_src_param src1_param;
2396 DWORD write_mask;
2398 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2399 * not, we can emulate it. */
2400 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2401 FIXME("64-bit integer multiplies not implemented.\n");
2403 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2405 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2406 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2407 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2409 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2410 src0_param.param_str, src1_param.param_str);
2414 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2416 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2417 struct glsl_src_param src0_param, src1_param;
2418 DWORD write_mask;
2420 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2423 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2425 char dst_mask[6];
2427 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2428 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2429 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2430 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2431 dst_mask, src0_param.param_str, src1_param.param_str);
2433 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2434 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2435 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2436 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2438 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2439 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2441 else
2443 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2444 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2445 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2446 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2449 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2451 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2452 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2453 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2454 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2458 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2459 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2461 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2462 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2463 struct glsl_src_param src0_param;
2464 DWORD write_mask;
2466 write_mask = shader_glsl_append_dst(buffer, ins);
2467 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2469 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2470 * shader versions WINED3DSIO_MOVA is used for this. */
2471 if (ins->ctx->reg_maps->shader_version.major == 1
2472 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2473 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2475 /* This is a simple floor() */
2476 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2477 if (mask_size > 1) {
2478 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2479 } else {
2480 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2483 else if(ins->handler_idx == WINED3DSIH_MOVA)
2485 /* We need to *round* to the nearest int here. */
2486 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2488 if (gl_info->supported[EXT_GPU_SHADER4])
2490 if (mask_size > 1)
2491 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2492 else
2493 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2495 else
2497 if (mask_size > 1)
2498 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2499 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2500 else
2501 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2502 src0_param.param_str, src0_param.param_str);
2505 else
2507 shader_addline(buffer, "%s);\n", src0_param.param_str);
2511 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2512 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2514 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2515 struct glsl_src_param src0_param;
2516 struct glsl_src_param src1_param;
2517 DWORD dst_write_mask, src_write_mask;
2518 unsigned int dst_size = 0;
2520 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2521 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2523 /* dp3 works on vec3, dp4 on vec4 */
2524 if (ins->handler_idx == WINED3DSIH_DP4)
2526 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2527 } else {
2528 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2531 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2532 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2534 if (dst_size > 1) {
2535 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2536 } else {
2537 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2541 /* Note that this instruction has some restrictions. The destination write mask
2542 * can't contain the w component, and the source swizzles have to be .xyzw */
2543 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2545 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2546 struct glsl_src_param src0_param;
2547 struct glsl_src_param src1_param;
2548 char dst_mask[6];
2550 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2551 shader_glsl_append_dst(ins->ctx->buffer, ins);
2552 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2553 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2554 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2557 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2559 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2562 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2563 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2564 * GLSL uses the value as-is. */
2565 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2567 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2568 struct glsl_src_param src0_param;
2569 struct glsl_src_param src1_param;
2570 DWORD dst_write_mask;
2571 unsigned int dst_size;
2573 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2574 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2576 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2577 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2579 if (dst_size > 1)
2581 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2582 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2584 else
2586 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2587 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2591 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2592 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2593 * GLSL uses the value as-is. */
2594 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2596 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2597 struct glsl_src_param src0_param;
2598 DWORD dst_write_mask;
2599 unsigned int dst_size;
2601 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2602 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2606 if (dst_size > 1)
2608 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2609 dst_size, src0_param.param_str);
2611 else
2613 shader_addline(buffer, "log2(abs(%s)));\n",
2614 src0_param.param_str);
2618 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2619 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2621 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2622 struct glsl_src_param src_param;
2623 const char *instruction;
2624 DWORD write_mask;
2625 unsigned i;
2627 /* Determine the GLSL function to use based on the opcode */
2628 /* TODO: Possibly make this a table for faster lookups */
2629 switch (ins->handler_idx)
2631 case WINED3DSIH_MIN: instruction = "min"; break;
2632 case WINED3DSIH_MAX: instruction = "max"; break;
2633 case WINED3DSIH_ABS: instruction = "abs"; break;
2634 case WINED3DSIH_FRC: instruction = "fract"; break;
2635 case WINED3DSIH_EXP: instruction = "exp2"; break;
2636 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2637 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2638 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2639 default: instruction = "";
2640 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2641 break;
2644 write_mask = shader_glsl_append_dst(buffer, ins);
2646 shader_addline(buffer, "%s(", instruction);
2648 if (ins->src_count)
2650 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2651 shader_addline(buffer, "%s", src_param.param_str);
2652 for (i = 1; i < ins->src_count; ++i)
2654 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2655 shader_addline(buffer, ", %s", src_param.param_str);
2659 shader_addline(buffer, "));\n");
2662 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2664 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2666 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2667 struct glsl_src_param src_param;
2668 unsigned int mask_size;
2669 DWORD write_mask;
2670 char dst_mask[6];
2672 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2673 mask_size = shader_glsl_get_write_mask_size(write_mask);
2674 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2676 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2677 src_param.param_str, src_param.param_str);
2678 shader_glsl_append_dst(buffer, ins);
2680 if (mask_size > 1)
2682 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2683 mask_size, src_param.param_str);
2685 else
2687 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2688 src_param.param_str);
2692 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2693 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2694 * dst.x = 2^(floor(src))
2695 * dst.y = src - floor(src)
2696 * dst.z = 2^src (partial precision is allowed, but optional)
2697 * dst.w = 1.0;
2698 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2699 * dst = 2^src; (partial precision is allowed, but optional)
2701 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2703 struct glsl_src_param src_param;
2705 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2707 if (ins->ctx->reg_maps->shader_version.major < 2)
2709 char dst_mask[6];
2711 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2712 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2713 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2714 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2716 shader_glsl_append_dst(ins->ctx->buffer, ins);
2717 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2718 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2719 } else {
2720 DWORD write_mask;
2721 unsigned int mask_size;
2723 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2724 mask_size = shader_glsl_get_write_mask_size(write_mask);
2726 if (mask_size > 1) {
2727 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2728 } else {
2729 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2734 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2736 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2737 struct glsl_src_param src_param;
2738 unsigned int mask_size;
2739 DWORD write_mask;
2741 write_mask = shader_glsl_append_dst(buffer, ins);
2742 mask_size = shader_glsl_get_write_mask_size(write_mask);
2743 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2745 if (mask_size > 1)
2746 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2747 else
2748 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2751 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2753 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2754 struct glsl_src_param src_param;
2755 unsigned int mask_size;
2756 DWORD write_mask;
2758 write_mask = shader_glsl_append_dst(buffer, ins);
2759 mask_size = shader_glsl_get_write_mask_size(write_mask);
2760 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2762 if (mask_size > 1)
2763 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2764 else
2765 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2768 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2769 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2771 struct glsl_src_param src_param;
2772 DWORD write_mask;
2773 unsigned int mask_size;
2775 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2776 mask_size = shader_glsl_get_write_mask_size(write_mask);
2777 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2779 if (mask_size > 1)
2781 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2782 mask_size, src_param.param_str);
2784 else
2786 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2787 src_param.param_str);
2791 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2793 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2794 struct glsl_src_param src_param;
2795 DWORD write_mask;
2796 unsigned int mask_size;
2798 write_mask = shader_glsl_append_dst(buffer, ins);
2799 mask_size = shader_glsl_get_write_mask_size(write_mask);
2801 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2803 if (mask_size > 1)
2805 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2806 mask_size, src_param.param_str);
2808 else
2810 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2811 src_param.param_str);
2815 /** Process signed comparison opcodes in GLSL. */
2816 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2818 struct glsl_src_param src0_param;
2819 struct glsl_src_param src1_param;
2820 DWORD write_mask;
2821 unsigned int mask_size;
2823 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2824 mask_size = shader_glsl_get_write_mask_size(write_mask);
2825 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2826 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2828 if (mask_size > 1) {
2829 const char *compare;
2831 switch(ins->handler_idx)
2833 case WINED3DSIH_SLT: compare = "lessThan"; break;
2834 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2835 default: compare = "";
2836 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2839 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2840 src0_param.param_str, src1_param.param_str);
2841 } else {
2842 switch(ins->handler_idx)
2844 case WINED3DSIH_SLT:
2845 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2846 * to return 0.0 but step returns 1.0 because step is not < x
2847 * An alternative is a bvec compare padded with an unused second component.
2848 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2849 * issue. Playing with not() is not possible either because not() does not accept
2850 * a scalar.
2852 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2853 src0_param.param_str, src1_param.param_str);
2854 break;
2855 case WINED3DSIH_SGE:
2856 /* Here we can use the step() function and safe a conditional */
2857 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2858 break;
2859 default:
2860 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2866 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2868 const char *condition_prefix, *condition_suffix;
2869 struct wined3d_shader_dst_param dst;
2870 struct glsl_src_param src0_param;
2871 struct glsl_src_param src1_param;
2872 struct glsl_src_param src2_param;
2873 BOOL temp_destination = FALSE;
2874 DWORD cmp_channel = 0;
2875 unsigned int i, j;
2876 char mask_char[6];
2877 DWORD write_mask;
2879 switch (ins->handler_idx)
2881 case WINED3DSIH_CMP:
2882 condition_prefix = "";
2883 condition_suffix = " >= 0.0";
2884 break;
2886 case WINED3DSIH_CND:
2887 condition_prefix = "";
2888 condition_suffix = " > 0.5";
2889 break;
2891 case WINED3DSIH_MOVC:
2892 condition_prefix = "bool(";
2893 condition_suffix = ")";
2894 break;
2896 default:
2897 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2898 condition_prefix = "<unhandled prefix>";
2899 condition_suffix = "<unhandled suffix>";
2900 break;
2903 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2905 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2906 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2907 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2908 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2910 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2911 condition_prefix, src0_param.param_str, condition_suffix,
2912 src1_param.param_str, src2_param.param_str);
2913 return;
2916 dst = ins->dst[0];
2918 /* Splitting the instruction up in multiple lines imposes a problem:
2919 * The first lines may overwrite source parameters of the following lines.
2920 * Deal with that by using a temporary destination register if needed. */
2921 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2922 && ins->src[0].reg.type == dst.reg.type)
2923 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2924 && ins->src[1].reg.type == dst.reg.type)
2925 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2926 && ins->src[2].reg.type == dst.reg.type))
2927 temp_destination = TRUE;
2929 /* Cycle through all source0 channels. */
2930 for (i = 0; i < 4; ++i)
2932 write_mask = 0;
2933 /* Find the destination channels which use the current source0 channel. */
2934 for (j = 0; j < 4; ++j)
2936 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2938 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2939 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2942 dst.write_mask = ins->dst[0].write_mask & write_mask;
2944 if (temp_destination)
2946 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2947 continue;
2948 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2950 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2951 continue;
2953 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2954 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2955 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2957 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2958 condition_prefix, src0_param.param_str, condition_suffix,
2959 src1_param.param_str, src2_param.param_str);
2962 if (temp_destination)
2964 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2965 shader_glsl_append_dst(ins->ctx->buffer, ins);
2966 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2970 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2971 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2972 * the compare is done per component of src0. */
2973 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2975 struct glsl_src_param src0_param;
2976 struct glsl_src_param src1_param;
2977 struct glsl_src_param src2_param;
2978 DWORD write_mask;
2979 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2980 ins->ctx->reg_maps->shader_version.minor);
2982 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2984 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2985 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2986 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2987 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2989 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2990 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2991 else
2992 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2993 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2994 return;
2997 shader_glsl_conditional_move(ins);
3000 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3001 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3003 struct glsl_src_param src0_param;
3004 struct glsl_src_param src1_param;
3005 struct glsl_src_param src2_param;
3006 DWORD write_mask;
3008 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3009 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3010 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3011 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3012 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3013 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3016 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3017 Vertex shaders to GLSL codes */
3018 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3020 int i;
3021 int nComponents = 0;
3022 struct wined3d_shader_dst_param tmp_dst = {{0}};
3023 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3024 struct wined3d_shader_instruction tmp_ins;
3026 memset(&tmp_ins, 0, sizeof(tmp_ins));
3028 /* Set constants for the temporary argument */
3029 tmp_ins.ctx = ins->ctx;
3030 tmp_ins.dst_count = 1;
3031 tmp_ins.dst = &tmp_dst;
3032 tmp_ins.src_count = 2;
3033 tmp_ins.src = tmp_src;
3035 switch(ins->handler_idx)
3037 case WINED3DSIH_M4x4:
3038 nComponents = 4;
3039 tmp_ins.handler_idx = WINED3DSIH_DP4;
3040 break;
3041 case WINED3DSIH_M4x3:
3042 nComponents = 3;
3043 tmp_ins.handler_idx = WINED3DSIH_DP4;
3044 break;
3045 case WINED3DSIH_M3x4:
3046 nComponents = 4;
3047 tmp_ins.handler_idx = WINED3DSIH_DP3;
3048 break;
3049 case WINED3DSIH_M3x3:
3050 nComponents = 3;
3051 tmp_ins.handler_idx = WINED3DSIH_DP3;
3052 break;
3053 case WINED3DSIH_M3x2:
3054 nComponents = 2;
3055 tmp_ins.handler_idx = WINED3DSIH_DP3;
3056 break;
3057 default:
3058 break;
3061 tmp_dst = ins->dst[0];
3062 tmp_src[0] = ins->src[0];
3063 tmp_src[1] = ins->src[1];
3064 for (i = 0; i < nComponents; ++i)
3066 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3067 shader_glsl_dot(&tmp_ins);
3068 ++tmp_src[1].reg.idx[0].offset;
3073 The LRP instruction performs a component-wise linear interpolation
3074 between the second and third operands using the first operand as the
3075 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3076 This is equivalent to mix(src2, src1, src0);
3078 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3080 struct glsl_src_param src0_param;
3081 struct glsl_src_param src1_param;
3082 struct glsl_src_param src2_param;
3083 DWORD write_mask;
3085 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3087 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3088 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3089 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3091 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3092 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3095 /** Process the WINED3DSIO_LIT instruction in GLSL:
3096 * dst.x = dst.w = 1.0
3097 * dst.y = (src0.x > 0) ? src0.x
3098 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3099 * where src.w is clamped at +- 128
3101 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3103 struct glsl_src_param src0_param;
3104 struct glsl_src_param src1_param;
3105 struct glsl_src_param src3_param;
3106 char dst_mask[6];
3108 shader_glsl_append_dst(ins->ctx->buffer, ins);
3109 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3111 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3112 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3113 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3115 /* The sdk specifies the instruction like this
3116 * dst.x = 1.0;
3117 * if(src.x > 0.0) dst.y = src.x
3118 * else dst.y = 0.0.
3119 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3120 * else dst.z = 0.0;
3121 * dst.w = 1.0;
3122 * (where power = src.w clamped between -128 and 128)
3124 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3125 * dst.x = 1.0 ... No further explanation needed
3126 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3127 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3128 * dst.w = 1.0. ... Nothing fancy.
3130 * So we still have one conditional in there. So do this:
3131 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3133 * 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),
3134 * 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.
3135 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3137 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3138 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3139 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3141 shader_addline(ins->ctx->buffer,
3142 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3143 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3144 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3145 src0_param.param_str, src3_param.param_str, dst_mask);
3148 /** Process the WINED3DSIO_DST instruction in GLSL:
3149 * dst.x = 1.0
3150 * dst.y = src0.x * src0.y
3151 * dst.z = src0.z
3152 * dst.w = src1.w
3154 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3156 struct glsl_src_param src0y_param;
3157 struct glsl_src_param src0z_param;
3158 struct glsl_src_param src1y_param;
3159 struct glsl_src_param src1w_param;
3160 char dst_mask[6];
3162 shader_glsl_append_dst(ins->ctx->buffer, ins);
3163 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3165 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3166 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3167 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3168 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3170 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3171 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3174 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3175 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3176 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3178 * dst.x = cos(src0.?)
3179 * dst.y = sin(src0.?)
3180 * dst.z = dst.z
3181 * dst.w = dst.w
3183 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3185 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3186 struct glsl_src_param src0_param;
3187 DWORD write_mask;
3189 if (ins->ctx->reg_maps->shader_version.major < 4)
3191 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3193 write_mask = shader_glsl_append_dst(buffer, ins);
3194 switch (write_mask)
3196 case WINED3DSP_WRITEMASK_0:
3197 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3198 break;
3200 case WINED3DSP_WRITEMASK_1:
3201 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3202 break;
3204 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3205 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3206 src0_param.param_str, src0_param.param_str);
3207 break;
3209 default:
3210 ERR("Write mask should be .x, .y or .xy\n");
3211 break;
3214 return;
3217 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3220 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3222 char dst_mask[6];
3224 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3225 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3226 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3228 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3229 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3230 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3232 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3233 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3235 else
3237 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3238 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3239 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3242 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3244 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3245 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3246 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3250 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3251 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3252 * generate invalid code
3254 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3256 struct glsl_src_param src0_param;
3257 DWORD write_mask;
3259 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3260 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3262 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3265 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3266 * Start a for() loop where src1.y is the initial value of aL,
3267 * increment aL by src1.z for a total of src1.x iterations.
3268 * Need to use a temporary variable for this operation.
3270 /* FIXME: I don't think nested loops will work correctly this way. */
3271 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3273 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3274 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3275 const struct wined3d_shader *shader = ins->ctx->shader;
3276 const struct wined3d_shader_lconst *constant;
3277 struct glsl_src_param src1_param;
3278 const DWORD *control_values = NULL;
3280 if (ins->ctx->reg_maps->shader_version.major < 4)
3282 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3284 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3285 * class hardware doesn't support real varying indexing, but Microsoft
3286 * designed this feature for Shader model 2.x+. If the loop control is
3287 * known at compile time, the GLSL compiler can unroll the loop, and
3288 * replace indirect addressing with direct addressing. */
3289 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3291 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3293 if (constant->idx == ins->src[1].reg.idx[0].offset)
3295 control_values = constant->value;
3296 break;
3301 if (control_values)
3303 struct wined3d_shader_loop_control loop_control;
3304 loop_control.count = control_values[0];
3305 loop_control.start = control_values[1];
3306 loop_control.step = (int)control_values[2];
3308 if (loop_control.step > 0)
3310 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3311 loop_state->current_depth, loop_control.start,
3312 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3313 loop_state->current_depth, loop_control.step);
3315 else if (loop_control.step < 0)
3317 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3318 loop_state->current_depth, loop_control.start,
3319 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3320 loop_state->current_depth, loop_control.step);
3322 else
3324 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3325 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3326 loop_state->current_depth, loop_control.count,
3327 loop_state->current_depth);
3330 else
3332 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3333 loop_state->current_depth, loop_state->current_reg,
3334 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3335 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3338 ++loop_state->current_reg;
3340 else
3342 shader_addline(buffer, "for (;;)\n{\n");
3345 ++loop_state->current_depth;
3348 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3350 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3352 shader_addline(ins->ctx->buffer, "}\n");
3354 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3356 --loop_state->current_depth;
3357 --loop_state->current_reg;
3360 if (ins->handler_idx == WINED3DSIH_ENDREP)
3362 --loop_state->current_depth;
3366 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3368 const struct wined3d_shader *shader = ins->ctx->shader;
3369 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3370 const struct wined3d_shader_lconst *constant;
3371 struct glsl_src_param src0_param;
3372 const DWORD *control_values = NULL;
3374 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3375 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3377 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3379 if (constant->idx == ins->src[0].reg.idx[0].offset)
3381 control_values = constant->value;
3382 break;
3387 if (control_values)
3389 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3390 loop_state->current_depth, loop_state->current_depth,
3391 control_values[0], loop_state->current_depth);
3393 else
3395 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3396 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3397 loop_state->current_depth, loop_state->current_depth,
3398 src0_param.param_str, loop_state->current_depth);
3401 ++loop_state->current_depth;
3404 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3406 struct glsl_src_param src0_param;
3408 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3409 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3412 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3414 struct glsl_src_param src0_param;
3415 struct glsl_src_param src1_param;
3417 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3418 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3420 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3421 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3424 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3426 shader_addline(ins->ctx->buffer, "} else {\n");
3429 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3431 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3434 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3436 shader_addline(ins->ctx->buffer, "break;\n");
3439 /* FIXME: According to MSDN the compare is done per component. */
3440 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3442 struct glsl_src_param src0_param;
3443 struct glsl_src_param src1_param;
3445 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3446 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3448 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3449 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3452 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3454 struct glsl_src_param src_param;
3456 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3457 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3460 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3462 shader_addline(ins->ctx->buffer, "}\n");
3463 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3466 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3468 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3471 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3473 struct glsl_src_param src1_param;
3475 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3476 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3477 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3480 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3482 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3483 * function only suppresses the unhandled instruction warning
3487 /*********************************************
3488 * Pixel Shader Specific Code begins here
3489 ********************************************/
3490 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3492 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3493 ins->ctx->reg_maps->shader_version.minor);
3494 struct glsl_sample_function sample_function;
3495 DWORD sample_flags = 0;
3496 DWORD sampler_idx;
3497 DWORD mask = 0, swizzle;
3498 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3500 /* 1.0-1.4: Use destination register as sampler source.
3501 * 2.0+: Use provided sampler source. */
3502 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3503 sampler_idx = ins->dst[0].reg.idx[0].offset;
3504 else
3505 sampler_idx = ins->src[1].reg.idx[0].offset;
3507 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3509 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3510 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3511 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3513 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3514 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3516 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3517 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3519 case WINED3D_TTFF_COUNT1:
3520 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3521 break;
3522 case WINED3D_TTFF_COUNT2:
3523 mask = WINED3DSP_WRITEMASK_1;
3524 break;
3525 case WINED3D_TTFF_COUNT3:
3526 mask = WINED3DSP_WRITEMASK_2;
3527 break;
3528 case WINED3D_TTFF_COUNT4:
3529 case WINED3D_TTFF_DISABLE:
3530 mask = WINED3DSP_WRITEMASK_3;
3531 break;
3535 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3537 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3539 if (src_mod == WINED3DSPSM_DZ) {
3540 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3541 mask = WINED3DSP_WRITEMASK_2;
3542 } else if (src_mod == WINED3DSPSM_DW) {
3543 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3544 mask = WINED3DSP_WRITEMASK_3;
3547 else
3549 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3550 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3552 /* ps 2.0 texldp instruction always divides by the fourth component. */
3553 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3554 mask = WINED3DSP_WRITEMASK_3;
3558 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3559 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3561 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3562 mask |= sample_function.coord_mask;
3564 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3565 else swizzle = ins->src[1].swizzle;
3567 /* 1.0-1.3: Use destination register as coordinate source.
3568 1.4+: Use provided coordinate source register. */
3569 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3571 char coord_mask[6];
3572 shader_glsl_write_mask_to_str(mask, coord_mask);
3573 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3574 "T%u%s", sampler_idx, coord_mask);
3576 else
3578 struct glsl_src_param coord_param;
3579 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3580 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3582 struct glsl_src_param bias;
3583 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3584 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3585 "%s", coord_param.param_str);
3586 } else {
3587 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3588 "%s", coord_param.param_str);
3593 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3595 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3596 struct glsl_src_param coord_param, dx_param, dy_param;
3597 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3598 struct glsl_sample_function sample_function;
3599 DWORD sampler_idx;
3600 DWORD swizzle = ins->src[1].swizzle;
3601 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3603 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3605 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3606 shader_glsl_tex(ins);
3607 return;
3610 sampler_idx = ins->src[1].reg.idx[0].offset;
3611 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3612 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3614 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3615 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3616 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3617 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3619 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3620 "%s", coord_param.param_str);
3623 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3625 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3626 struct glsl_src_param coord_param, lod_param;
3627 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3628 struct glsl_sample_function sample_function;
3629 DWORD sampler_idx;
3630 DWORD swizzle = ins->src[1].swizzle;
3631 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3633 sampler_idx = ins->src[1].reg.idx[0].offset;
3634 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3635 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3636 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3638 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3639 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3641 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3643 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3644 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3646 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3647 * However, the NVIDIA drivers allow them in fragment shaders as well,
3648 * even without the appropriate extension. */
3649 WARN("Using %s in fragment shader.\n", sample_function.name);
3651 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3652 "%s", coord_param.param_str);
3655 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3657 /* FIXME: Make this work for more than just 2D textures */
3658 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3659 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3661 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3663 char dst_mask[6];
3665 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3666 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3667 ins->dst[0].reg.idx[0].offset, dst_mask);
3669 else
3671 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3672 DWORD reg = ins->src[0].reg.idx[0].offset;
3673 char dst_swizzle[6];
3675 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3677 if (src_mod == WINED3DSPSM_DZ)
3679 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3680 struct glsl_src_param div_param;
3682 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3684 if (mask_size > 1) {
3685 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3686 } else {
3687 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3690 else if (src_mod == WINED3DSPSM_DW)
3692 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3693 struct glsl_src_param div_param;
3695 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3697 if (mask_size > 1) {
3698 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3699 } else {
3700 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3702 } else {
3703 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3708 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3709 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3710 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3711 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3713 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3714 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3715 struct glsl_sample_function sample_function;
3716 struct glsl_src_param src0_param;
3717 UINT mask_size;
3719 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3721 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3722 * scalar, and projected sampling would require 4.
3724 * It is a dependent read - not valid with conditional NP2 textures
3726 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3727 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3729 switch(mask_size)
3731 case 1:
3732 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3733 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3734 break;
3736 case 2:
3737 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3738 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3739 break;
3741 case 3:
3742 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3743 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3744 break;
3746 default:
3747 FIXME("Unexpected mask size %u\n", mask_size);
3748 break;
3752 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3753 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3754 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3756 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3757 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3758 struct glsl_src_param src0_param;
3759 DWORD dst_mask;
3760 unsigned int mask_size;
3762 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3763 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3764 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3766 if (mask_size > 1) {
3767 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3768 } else {
3769 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3773 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3774 * Calculate the depth as dst.x / dst.y */
3775 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3777 struct glsl_dst_param dst_param;
3779 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3781 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3782 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3783 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3784 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3785 * >= 1.0 or < 0.0
3787 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3788 dst_param.reg_name, dst_param.reg_name);
3791 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3792 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3793 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3794 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3796 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3798 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3799 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3800 struct glsl_src_param src0_param;
3802 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3804 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3805 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3808 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3809 * Calculate the 1st of a 2-row matrix multiplication. */
3810 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3812 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3813 DWORD reg = ins->dst[0].reg.idx[0].offset;
3814 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3815 struct glsl_src_param src0_param;
3817 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3818 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3821 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3822 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3823 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3825 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3826 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3827 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3828 DWORD reg = ins->dst[0].reg.idx[0].offset;
3829 struct glsl_src_param src0_param;
3831 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3832 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3833 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3836 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3838 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3839 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3840 struct glsl_sample_function sample_function;
3841 DWORD reg = ins->dst[0].reg.idx[0].offset;
3842 struct glsl_src_param src0_param;
3844 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3845 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
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.xy");
3853 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3854 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3855 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3857 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3858 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3859 struct glsl_sample_function sample_function;
3860 DWORD reg = ins->dst[0].reg.idx[0].offset;
3861 struct glsl_src_param src0_param;
3863 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3864 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3866 /* Dependent read, not valid with conditional NP2 */
3867 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3869 /* Sample the texture using the calculated coordinates */
3870 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3872 tex_mx->current_row = 0;
3875 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3876 * Perform the 3rd row of a 3x3 matrix multiply */
3877 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3879 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3880 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3881 DWORD reg = ins->dst[0].reg.idx[0].offset;
3882 struct glsl_src_param src0_param;
3883 char dst_mask[6];
3885 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3887 shader_glsl_append_dst(ins->ctx->buffer, ins);
3888 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3889 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3891 tex_mx->current_row = 0;
3894 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3895 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3896 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3898 struct glsl_src_param src0_param;
3899 struct glsl_src_param src1_param;
3900 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3901 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3902 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3903 struct glsl_sample_function sample_function;
3904 DWORD reg = ins->dst[0].reg.idx[0].offset;
3905 char coord_mask[6];
3907 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3908 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3910 /* Perform the last matrix multiply operation */
3911 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3912 /* Reflection calculation */
3913 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3915 /* Dependent read, not valid with conditional NP2 */
3916 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3917 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3919 /* Sample the texture */
3920 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3921 NULL, NULL, NULL, "tmp0%s", coord_mask);
3923 tex_mx->current_row = 0;
3926 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3927 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3928 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3930 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3931 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3932 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3933 struct glsl_sample_function sample_function;
3934 DWORD reg = ins->dst[0].reg.idx[0].offset;
3935 struct glsl_src_param src0_param;
3936 char coord_mask[6];
3938 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3940 /* Perform the last matrix multiply operation */
3941 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3943 /* Construct the eye-ray vector from w coordinates */
3944 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3945 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3946 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3948 /* Dependent read, not valid with conditional NP2 */
3949 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3950 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3952 /* Sample the texture using the calculated coordinates */
3953 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3954 NULL, NULL, NULL, "tmp0%s", coord_mask);
3956 tex_mx->current_row = 0;
3959 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3960 * Apply a fake bump map transform.
3961 * texbem is pshader <= 1.3 only, this saves a few version checks
3963 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3965 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3966 struct glsl_sample_function sample_function;
3967 struct glsl_src_param coord_param;
3968 DWORD sampler_idx;
3969 DWORD mask;
3970 DWORD flags;
3971 char coord_mask[6];
3973 sampler_idx = ins->dst[0].reg.idx[0].offset;
3974 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3975 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3977 /* Dependent read, not valid with conditional NP2 */
3978 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3979 mask = sample_function.coord_mask;
3981 shader_glsl_write_mask_to_str(mask, coord_mask);
3983 /* With projected textures, texbem only divides the static texture coord,
3984 * not the displacement, so we can't let GL handle this. */
3985 if (flags & WINED3D_PSARGS_PROJECTED)
3987 DWORD div_mask=0;
3988 char coord_div_mask[3];
3989 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3991 case WINED3D_TTFF_COUNT1:
3992 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3993 break;
3994 case WINED3D_TTFF_COUNT2:
3995 div_mask = WINED3DSP_WRITEMASK_1;
3996 break;
3997 case WINED3D_TTFF_COUNT3:
3998 div_mask = WINED3DSP_WRITEMASK_2;
3999 break;
4000 case WINED3D_TTFF_COUNT4:
4001 case WINED3D_TTFF_DISABLE:
4002 div_mask = WINED3DSP_WRITEMASK_3;
4003 break;
4005 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4006 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4009 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4011 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4012 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4013 coord_param.param_str, coord_mask);
4015 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4017 struct glsl_src_param luminance_param;
4018 struct glsl_dst_param dst_param;
4020 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4021 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4023 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4024 dst_param.reg_name, dst_param.mask_str,
4025 luminance_param.param_str, sampler_idx, sampler_idx);
4029 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4031 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4032 struct glsl_src_param src0_param, src1_param;
4034 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4035 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4037 shader_glsl_append_dst(ins->ctx->buffer, ins);
4038 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4039 src0_param.param_str, sampler_idx, src1_param.param_str);
4042 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4043 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4044 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4046 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4047 struct glsl_sample_function sample_function;
4048 struct glsl_src_param src0_param;
4050 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4052 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4053 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4054 "%s.wx", src0_param.reg_name);
4057 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4058 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4059 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4061 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4062 struct glsl_sample_function sample_function;
4063 struct glsl_src_param src0_param;
4065 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4067 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4068 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4069 "%s.yz", src0_param.reg_name);
4072 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4073 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4074 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4076 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4077 struct glsl_sample_function sample_function;
4078 struct glsl_src_param src0_param;
4080 /* Dependent read, not valid with conditional NP2 */
4081 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4082 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4084 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4085 "%s", src0_param.param_str);
4088 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4089 * If any of the first 3 components are < 0, discard this pixel */
4090 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4092 struct glsl_dst_param dst_param;
4094 /* The argument is a destination parameter, and no writemasks are allowed */
4095 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4096 if (ins->ctx->reg_maps->shader_version.major >= 2)
4098 /* 2.0 shaders compare all 4 components in texkill */
4099 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4100 } else {
4101 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4102 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4103 * 4 components are defined, only the first 3 are used
4105 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4109 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4110 * dst = dot2(src0, src1) + src2 */
4111 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4113 struct glsl_src_param src0_param;
4114 struct glsl_src_param src1_param;
4115 struct glsl_src_param src2_param;
4116 DWORD write_mask;
4117 unsigned int mask_size;
4119 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4120 mask_size = shader_glsl_get_write_mask_size(write_mask);
4122 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4123 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4124 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4126 if (mask_size > 1) {
4127 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4128 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4129 } else {
4130 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4131 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4135 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4136 const struct wined3d_shader_signature_element *input_signature,
4137 const struct wined3d_shader_reg_maps *reg_maps,
4138 enum vertexprocessing_mode vertexprocessing)
4140 WORD map = reg_maps->input_registers;
4141 unsigned int i;
4143 for (i = 0; map; map >>= 1, ++i)
4145 const char *semantic_name;
4146 UINT semantic_idx;
4147 char reg_mask[6];
4149 /* Unused */
4150 if (!(map & 1)) continue;
4152 semantic_name = input_signature[i].semantic_name;
4153 semantic_idx = input_signature[i].semantic_idx;
4154 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4156 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4158 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4159 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4160 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4161 else
4162 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4163 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4165 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4167 if (!semantic_idx)
4168 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4169 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4170 else if (semantic_idx == 1)
4171 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4172 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4173 else
4174 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4175 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4177 else
4179 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4180 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4185 /*********************************************
4186 * Vertex Shader Specific Code begins here
4187 ********************************************/
4189 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4191 struct glsl_program_key key;
4193 key.vs_id = entry->vs.id;
4194 key.gs_id = entry->gs.id;
4195 key.ps_id = entry->ps.id;
4197 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4199 ERR("Failed to insert program entry.\n");
4203 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4204 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4206 struct wine_rb_entry *entry;
4207 struct glsl_program_key key;
4209 key.vs_id = vs_id;
4210 key.gs_id = gs_id;
4211 key.ps_id = ps_id;
4213 entry = wine_rb_get(&priv->program_lookup, &key);
4214 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4217 /* Context activation is done by the caller. */
4218 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4219 struct glsl_shader_prog_link *entry)
4221 struct glsl_program_key key;
4223 key.vs_id = entry->vs.id;
4224 key.gs_id = entry->gs.id;
4225 key.ps_id = entry->ps.id;
4226 wine_rb_remove(&priv->program_lookup, &key);
4228 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4229 if (entry->vs.id)
4230 list_remove(&entry->vs.shader_entry);
4231 if (entry->gs.id)
4232 list_remove(&entry->gs.shader_entry);
4233 if (entry->ps.id)
4234 list_remove(&entry->ps.shader_entry);
4235 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4236 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4237 HeapFree(GetProcessHeap(), 0, entry);
4240 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4241 const struct wined3d_gl_info *gl_info, const DWORD *map,
4242 const struct wined3d_shader_signature_element *input_signature,
4243 const struct wined3d_shader_reg_maps *reg_maps_in,
4244 const struct wined3d_shader_signature_element *output_signature,
4245 const struct wined3d_shader_reg_maps *reg_maps_out)
4247 unsigned int i, j;
4248 const char *semantic_name_in;
4249 UINT semantic_idx_in;
4250 DWORD *set;
4251 DWORD in_idx;
4252 unsigned int in_count = vec4_varyings(3, gl_info);
4253 char reg_mask[6];
4254 char destination[50];
4255 WORD input_map, output_map;
4257 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4259 input_map = reg_maps_in->input_registers;
4260 for (i = 0; input_map; input_map >>= 1, ++i)
4262 if (!(input_map & 1)) continue;
4264 in_idx = map[i];
4265 /* Declared, but not read register */
4266 if (in_idx == ~0U) continue;
4267 if (in_idx >= (in_count + 2))
4269 FIXME("More input varyings declared than supported, expect issues.\n");
4270 continue;
4273 if (in_idx == in_count)
4274 sprintf(destination, "gl_FrontColor");
4275 else if (in_idx == in_count + 1)
4276 sprintf(destination, "gl_FrontSecondaryColor");
4277 else
4278 sprintf(destination, "ps_in[%u]", in_idx);
4280 semantic_name_in = input_signature[i].semantic_name;
4281 semantic_idx_in = input_signature[i].semantic_idx;
4282 set[in_idx] = ~0U;
4284 output_map = reg_maps_out->output_registers;
4285 for (j = 0; output_map; output_map >>= 1, ++j)
4287 DWORD mask;
4289 if (!(output_map & 1)
4290 || semantic_idx_in != output_signature[j].semantic_idx
4291 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4292 || !(mask = input_signature[i].mask & output_signature[j].mask))
4293 continue;
4295 set[in_idx] = mask;
4296 shader_glsl_write_mask_to_str(mask, reg_mask);
4298 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4299 destination, reg_mask, j, reg_mask);
4303 for (i = 0; i < in_count + 2; ++i)
4305 unsigned int size;
4307 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4308 continue;
4310 if (set[i] == ~0U) set[i] = 0;
4312 size = 0;
4313 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4314 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4315 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4316 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4317 reg_mask[size] = '\0';
4319 if (i == in_count)
4320 sprintf(destination, "gl_FrontColor");
4321 else if (i == in_count + 1)
4322 sprintf(destination, "gl_FrontSecondaryColor");
4323 else
4324 sprintf(destination, "ps_in[%u]", i);
4326 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4327 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4330 HeapFree(GetProcessHeap(), 0, set);
4333 /* Context activation is done by the caller. */
4334 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4335 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4336 const struct wined3d_gl_info *gl_info)
4338 GLhandleARB ret = 0;
4339 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4340 unsigned int i;
4341 const char *semantic_name;
4342 UINT semantic_idx;
4343 char reg_mask[6];
4344 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4345 WORD map = vs->reg_maps.output_registers;
4347 shader_buffer_clear(buffer);
4349 shader_addline(buffer, "#version 120\n");
4351 if (ps_major < 3)
4353 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4355 for (i = 0; map; map >>= 1, ++i)
4357 DWORD write_mask;
4359 if (!(map & 1)) continue;
4361 semantic_name = output_signature[i].semantic_name;
4362 semantic_idx = output_signature[i].semantic_idx;
4363 write_mask = output_signature[i].mask;
4364 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4366 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4368 if (!semantic_idx)
4369 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4370 reg_mask, i, reg_mask);
4371 else if (semantic_idx == 1)
4372 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4373 reg_mask, i, reg_mask);
4375 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4377 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4378 reg_mask, i, reg_mask);
4380 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4382 if (semantic_idx < 8)
4384 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4385 write_mask |= WINED3DSP_WRITEMASK_3;
4387 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4388 semantic_idx, reg_mask, i, reg_mask);
4389 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4390 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4393 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4395 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4397 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4399 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4402 shader_addline(buffer, "}\n");
4404 else
4406 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4407 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4408 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4409 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4411 /* First, sort out position and point size. Those are not passed to the pixel shader */
4412 for (i = 0; map; map >>= 1, ++i)
4414 if (!(map & 1)) continue;
4416 semantic_name = output_signature[i].semantic_name;
4417 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4419 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4421 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4422 reg_mask, i, reg_mask);
4424 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4426 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4430 /* Then, fix the pixel shader input */
4431 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4432 &ps->reg_maps, output_signature, &vs->reg_maps);
4434 shader_addline(buffer, "}\n");
4437 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4438 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4439 shader_glsl_compile(gl_info, ret, buffer->buffer);
4441 return ret;
4444 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4446 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4447 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4448 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4449 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4450 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4451 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4454 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4456 switch (mode)
4458 case WINED3D_FFP_PS_FOG_OFF:
4459 return;
4461 case WINED3D_FFP_PS_FOG_LINEAR:
4462 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4463 break;
4465 case WINED3D_FFP_PS_FOG_EXP:
4466 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4467 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4468 break;
4470 case WINED3D_FFP_PS_FOG_EXP2:
4471 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4472 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4473 break;
4475 default:
4476 ERR("Invalid fog mode %#x.\n", mode);
4477 return;
4480 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4483 /* Context activation is done by the caller. */
4484 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4485 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4486 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4488 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4489 const struct wined3d_gl_info *gl_info = context->gl_info;
4490 const DWORD *function = shader->function;
4491 struct shader_glsl_ctx_priv priv_ctx;
4493 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4494 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4496 memset(&priv_ctx, 0, sizeof(priv_ctx));
4497 priv_ctx.cur_ps_args = args;
4498 priv_ctx.cur_np2fixup_info = np2fixup_info;
4500 shader_addline(buffer, "#version 120\n");
4502 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4503 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4504 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4505 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4506 /* The spec says that it doesn't have to be explicitly enabled, but the
4507 * nvidia drivers write a warning if we don't do so. */
4508 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4509 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4510 if (gl_info->supported[EXT_GPU_SHADER4])
4511 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4513 /* Base Declarations */
4514 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4516 /* Pack 3.0 inputs */
4517 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4518 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4520 /* Base Shader Body */
4521 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4523 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4524 if (reg_maps->shader_version.major < 2)
4526 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4527 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4530 if (args->srgb_correction)
4531 shader_glsl_generate_srgb_write_correction(buffer);
4533 /* SM < 3 does not replace the fog stage. */
4534 if (reg_maps->shader_version.major < 3)
4535 shader_glsl_generate_fog_code(buffer, args->fog);
4537 shader_addline(buffer, "}\n");
4539 TRACE("Compiling shader object %u\n", shader_obj);
4540 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4542 /* Store the shader object */
4543 return shader_obj;
4546 /* Context activation is done by the caller. */
4547 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4548 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4549 const struct vs_compile_args *args)
4551 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4552 const struct wined3d_gl_info *gl_info = context->gl_info;
4553 const DWORD *function = shader->function;
4554 struct shader_glsl_ctx_priv priv_ctx;
4556 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4557 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4559 shader_addline(buffer, "#version 120\n");
4561 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4562 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4563 if (gl_info->supported[EXT_GPU_SHADER4])
4564 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4566 memset(&priv_ctx, 0, sizeof(priv_ctx));
4567 priv_ctx.cur_vs_args = args;
4569 /* Base Declarations */
4570 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4572 /* Base Shader Body */
4573 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4575 /* Unpack outputs */
4576 shader_addline(buffer, "order_ps_input(vs_out);\n");
4578 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4579 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4580 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4581 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4583 if (args->fog_src == VS_FOG_Z)
4584 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4585 else if (!reg_maps->fog)
4586 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4588 /* We always store the clipplanes without y inversion */
4589 if (args->clip_enabled)
4590 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4592 /* Write the final position.
4594 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4595 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4596 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4597 * contains 1.0 to allow a mad.
4599 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4600 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4602 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4604 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4605 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4606 * which is the same as z = z * 2 - w.
4608 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4610 shader_addline(buffer, "}\n");
4612 TRACE("Compiling shader object %u\n", shader_obj);
4613 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4615 return shader_obj;
4618 /* Context activation is done by the caller. */
4619 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4620 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4622 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4623 const struct wined3d_gl_info *gl_info = context->gl_info;
4624 const DWORD *function = shader->function;
4625 struct shader_glsl_ctx_priv priv_ctx;
4626 GLhandleARB shader_id;
4628 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4630 shader_addline(buffer, "#version 120\n");
4632 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4633 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4634 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4635 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4636 if (gl_info->supported[EXT_GPU_SHADER4])
4637 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4639 memset(&priv_ctx, 0, sizeof(priv_ctx));
4640 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4641 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4642 shader_addline(buffer, "}\n");
4644 TRACE("Compiling shader object %u.\n", shader_id);
4645 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4647 return shader_id;
4650 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4651 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4652 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4654 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4655 struct glsl_shader_private *shader_data;
4656 struct ps_np2fixup_info *np2fixup;
4657 UINT i;
4658 DWORD new_size;
4659 GLhandleARB ret;
4661 if (!shader->backend_data)
4663 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4664 if (!shader->backend_data)
4666 ERR("Failed to allocate backend data.\n");
4667 return 0;
4670 shader_data = shader->backend_data;
4671 gl_shaders = shader_data->gl_shaders.ps;
4673 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4674 * so a linear search is more performant than a hashmap or a binary search
4675 * (cache coherency etc)
4677 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4679 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4681 if (args->np2_fixup)
4682 *np2fixup_info = &gl_shaders[i].np2fixup;
4683 return gl_shaders[i].prgId;
4687 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4688 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4689 if (shader_data->num_gl_shaders)
4691 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4692 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4693 new_size * sizeof(*gl_shaders));
4695 else
4697 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4698 new_size = 1;
4701 if(!new_array) {
4702 ERR("Out of memory\n");
4703 return 0;
4705 shader_data->gl_shaders.ps = new_array;
4706 shader_data->shader_array_size = new_size;
4707 gl_shaders = new_array;
4710 gl_shaders[shader_data->num_gl_shaders].args = *args;
4712 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4713 memset(np2fixup, 0, sizeof(*np2fixup));
4714 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4716 pixelshader_update_samplers(shader, args->tex_types);
4718 shader_buffer_clear(buffer);
4719 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4720 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4722 return ret;
4725 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4726 const DWORD use_map) {
4727 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4728 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4729 return stored->fog_src == new->fog_src;
4732 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4733 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4734 const struct vs_compile_args *args)
4736 UINT i;
4737 DWORD new_size;
4738 DWORD use_map = shader->device->stream_info.use_map;
4739 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4740 struct glsl_shader_private *shader_data;
4741 GLhandleARB ret;
4743 if (!shader->backend_data)
4745 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4746 if (!shader->backend_data)
4748 ERR("Failed to allocate backend data.\n");
4749 return 0;
4752 shader_data = shader->backend_data;
4753 gl_shaders = shader_data->gl_shaders.vs;
4755 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4756 * so a linear search is more performant than a hashmap or a binary search
4757 * (cache coherency etc)
4759 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4761 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4762 return gl_shaders[i].prgId;
4765 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4767 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4768 if (shader_data->num_gl_shaders)
4770 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4771 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4772 new_size * sizeof(*gl_shaders));
4774 else
4776 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4777 new_size = 1;
4780 if(!new_array) {
4781 ERR("Out of memory\n");
4782 return 0;
4784 shader_data->gl_shaders.vs = new_array;
4785 shader_data->shader_array_size = new_size;
4786 gl_shaders = new_array;
4789 gl_shaders[shader_data->num_gl_shaders].args = *args;
4791 shader_buffer_clear(buffer);
4792 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4793 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4795 return ret;
4798 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4799 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4801 struct glsl_gs_compiled_shader *gl_shaders;
4802 struct glsl_shader_private *shader_data;
4803 GLhandleARB ret;
4805 if (!shader->backend_data)
4807 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4809 ERR("Failed to allocate backend data.\n");
4810 return 0;
4813 shader_data = shader->backend_data;
4814 gl_shaders = shader_data->gl_shaders.gs;
4816 if (shader_data->num_gl_shaders)
4817 return gl_shaders[0].id;
4819 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4821 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4823 ERR("Failed to allocate GL shader array.\n");
4824 return 0;
4826 shader_data->shader_array_size = 1;
4827 gl_shaders = shader_data->gl_shaders.gs;
4829 shader_buffer_clear(buffer);
4830 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4831 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4833 return ret;
4836 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4838 switch (mcs)
4840 case WINED3D_MCS_MATERIAL:
4841 return material;
4842 case WINED3D_MCS_COLOR1:
4843 return "gl_Color";
4844 case WINED3D_MCS_COLOR2:
4845 return "gl_SecondaryColor";
4846 default:
4847 ERR("Invalid material color source %#x.\n", mcs);
4848 return "<invalid>";
4852 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4853 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4855 const char *diffuse, *specular, *emission, *ambient;
4856 enum wined3d_light_type light_type;
4857 unsigned int i;
4859 if (!settings->lighting)
4861 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4862 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4863 return;
4866 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4867 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4868 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4869 shader_addline(buffer, "vec3 dir, dst;\n");
4870 shader_addline(buffer, "float att, t;\n");
4872 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4873 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4874 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4875 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4877 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4879 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4880 switch (light_type)
4882 case WINED3D_LIGHT_POINT:
4883 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4884 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4885 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4886 shader_addline(buffer, "dst.x = 1.0;\n");
4887 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4888 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4889 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4890 if (!settings->normal)
4891 break;
4892 shader_addline(buffer, "dir = normalize(dir);\n");
4893 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4894 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4895 if (settings->localviewer)
4896 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4897 else
4898 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4899 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4900 " * gl_LightSource[%u].specular) / att;\n", i);
4901 break;
4903 case WINED3D_LIGHT_SPOT:
4904 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4905 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4906 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4907 shader_addline(buffer, "dst.x = 1.0;\n");
4908 shader_addline(buffer, "dir = normalize(dir);\n");
4909 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4910 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4911 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4912 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4913 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4914 i, i, i, i);
4915 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4916 if (!settings->normal)
4917 break;
4918 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4919 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4920 if (settings->localviewer)
4921 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4922 else
4923 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4924 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4925 " * gl_LightSource[%u].specular) * att;\n", i);
4926 break;
4928 case WINED3D_LIGHT_DIRECTIONAL:
4929 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4930 if (!settings->normal)
4931 break;
4932 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4933 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4934 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4935 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4936 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4937 " * gl_LightSource[%u].specular;\n", i);
4938 break;
4940 default:
4941 if (light_type)
4942 FIXME("Unhandled light type %#x.\n", light_type);
4943 continue;
4947 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4948 ambient, diffuse, emission);
4949 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4950 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4953 /* Context activation is done by the caller. */
4954 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4955 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4957 GLhandleARB shader_obj;
4958 unsigned int i;
4960 shader_buffer_clear(buffer);
4962 shader_addline(buffer, "#version 120\n");
4963 shader_addline(buffer, "\n");
4964 shader_addline(buffer, "void main()\n{\n");
4965 shader_addline(buffer, "float m;\n");
4966 shader_addline(buffer, "vec3 r;\n");
4968 if (settings->transformed)
4970 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4971 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4972 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4974 else
4976 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4977 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4978 if (settings->clipping)
4979 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4980 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4983 if (!settings->normal)
4984 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4985 else if (settings->normalize)
4986 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4987 else
4988 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4990 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4992 for (i = 0; i < MAX_TEXTURES; ++i)
4994 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4996 case WINED3DTSS_TCI_PASSTHRU:
4997 if (settings->texcoords & (1 << i))
4998 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4999 i, i, i);
5000 break;
5002 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5003 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
5004 break;
5006 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5007 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
5008 break;
5010 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5011 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5012 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5013 break;
5015 case WINED3DTSS_TCI_SPHEREMAP:
5016 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5017 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5018 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
5019 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5020 break;
5022 default:
5023 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5024 break;
5028 switch (settings->fog_mode)
5030 case WINED3D_FFP_VS_FOG_OFF:
5031 break;
5033 case WINED3D_FFP_VS_FOG_FOGCOORD:
5034 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5035 break;
5037 case WINED3D_FFP_VS_FOG_RANGE:
5038 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5039 break;
5041 case WINED3D_FFP_VS_FOG_DEPTH:
5042 if (settings->ortho_fog)
5043 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5044 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5045 else
5046 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5047 break;
5049 default:
5050 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5051 break;
5054 if (settings->point_size)
5056 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5057 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5058 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5059 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5062 shader_addline(buffer, "}\n");
5064 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5065 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5067 return shader_obj;
5070 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5071 DWORD argnum, unsigned int stage, DWORD arg)
5073 const char *ret;
5075 if (arg == ARG_UNUSED)
5076 return "<unused arg>";
5078 switch (arg & WINED3DTA_SELECTMASK)
5080 case WINED3DTA_DIFFUSE:
5081 ret = "gl_Color";
5082 break;
5084 case WINED3DTA_CURRENT:
5085 if (!stage)
5086 ret = "gl_Color";
5087 else
5088 ret = "ret";
5089 break;
5091 case WINED3DTA_TEXTURE:
5092 switch (stage)
5094 case 0: ret = "tex0"; break;
5095 case 1: ret = "tex1"; break;
5096 case 2: ret = "tex2"; break;
5097 case 3: ret = "tex3"; break;
5098 case 4: ret = "tex4"; break;
5099 case 5: ret = "tex5"; break;
5100 case 6: ret = "tex6"; break;
5101 case 7: ret = "tex7"; break;
5102 default:
5103 ret = "<invalid texture>";
5104 break;
5106 break;
5108 case WINED3DTA_TFACTOR:
5109 ret = "tex_factor";
5110 break;
5112 case WINED3DTA_SPECULAR:
5113 ret = "gl_SecondaryColor";
5114 break;
5116 case WINED3DTA_TEMP:
5117 ret = "temp_reg";
5118 break;
5120 case WINED3DTA_CONSTANT:
5121 FIXME("Per-stage constants not implemented.\n");
5122 switch (stage)
5124 case 0: ret = "const0"; break;
5125 case 1: ret = "const1"; break;
5126 case 2: ret = "const2"; break;
5127 case 3: ret = "const3"; break;
5128 case 4: ret = "const4"; break;
5129 case 5: ret = "const5"; break;
5130 case 6: ret = "const6"; break;
5131 case 7: ret = "const7"; break;
5132 default:
5133 ret = "<invalid constant>";
5134 break;
5136 break;
5138 default:
5139 return "<unhandled arg>";
5142 if (arg & WINED3DTA_COMPLEMENT)
5144 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5145 if (argnum == 0)
5146 ret = "arg0";
5147 else if (argnum == 1)
5148 ret = "arg1";
5149 else if (argnum == 2)
5150 ret = "arg2";
5153 if (arg & WINED3DTA_ALPHAREPLICATE)
5155 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5156 if (argnum == 0)
5157 ret = "arg0";
5158 else if (argnum == 1)
5159 ret = "arg1";
5160 else if (argnum == 2)
5161 ret = "arg2";
5164 return ret;
5167 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5168 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5170 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5172 if (color && alpha)
5173 dstmask = "";
5174 else if (color)
5175 dstmask = ".xyz";
5176 else
5177 dstmask = ".w";
5179 if (dst == tempreg)
5180 dstreg = "temp_reg";
5181 else
5182 dstreg = "ret";
5184 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5185 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5186 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5188 switch (op)
5190 case WINED3D_TOP_DISABLE:
5191 if (!stage)
5192 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5193 break;
5195 case WINED3D_TOP_SELECT_ARG1:
5196 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5197 break;
5199 case WINED3D_TOP_SELECT_ARG2:
5200 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5201 break;
5203 case WINED3D_TOP_MODULATE:
5204 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5205 break;
5207 case WINED3D_TOP_MODULATE_4X:
5208 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5209 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5210 break;
5212 case WINED3D_TOP_MODULATE_2X:
5213 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5214 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5215 break;
5217 case WINED3D_TOP_ADD:
5218 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5219 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5220 break;
5222 case WINED3D_TOP_ADD_SIGNED:
5223 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5224 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5225 break;
5227 case WINED3D_TOP_ADD_SIGNED_2X:
5228 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5229 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5230 break;
5232 case WINED3D_TOP_SUBTRACT:
5233 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5234 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5235 break;
5237 case WINED3D_TOP_ADD_SMOOTH:
5238 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5239 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5240 break;
5242 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5243 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5244 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5245 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5246 break;
5248 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5249 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5250 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5251 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5252 break;
5254 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5255 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5256 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5257 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5258 break;
5260 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5261 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5262 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5263 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5264 break;
5266 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5267 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5268 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5269 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5270 break;
5272 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5273 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5274 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5275 break;
5277 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5278 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5279 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5280 break;
5282 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5283 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5284 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5285 break;
5286 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5287 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5288 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5289 break;
5291 case WINED3D_TOP_BUMPENVMAP:
5292 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5293 /* These are handled in the first pass, nothing to do. */
5294 break;
5296 case WINED3D_TOP_DOTPRODUCT3:
5297 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5298 dstreg, dstmask, arg1, arg2, dstmask);
5299 break;
5301 case WINED3D_TOP_MULTIPLY_ADD:
5302 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5303 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5304 break;
5306 case WINED3D_TOP_LERP:
5307 /* MSDN isn't quite right here. */
5308 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5309 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5310 break;
5312 default:
5313 FIXME("Unhandled operation %#x.\n", op);
5314 break;
5318 /* Context activation is done by the caller. */
5319 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5320 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5322 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5323 BYTE lum_map = 0, bump_map = 0, tex_map = 0;
5324 const char *final_combiner_src = "ret";
5325 UINT lowest_disabled_stage;
5326 GLhandleARB shader_obj;
5327 DWORD arg0, arg1, arg2;
5328 unsigned int stage;
5330 shader_buffer_clear(buffer);
5332 /* Find out which textures are read */
5333 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5335 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5336 break;
5338 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5339 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5340 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5342 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5343 tex_map |= 1 << stage;
5344 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5345 tfactor_used = TRUE;
5346 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5347 tempreg_used = TRUE;
5348 if (settings->op[stage].dst == tempreg)
5349 tempreg_used = TRUE;
5351 switch (settings->op[stage].cop)
5353 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5354 lum_map |= 1 << stage;
5355 /* fall through */
5356 case WINED3D_TOP_BUMPENVMAP:
5357 bump_map |= 1 << stage;
5358 /* fall through */
5359 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5360 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5361 tex_map |= 1 << stage;
5362 break;
5364 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5365 tfactor_used = TRUE;
5366 break;
5368 default:
5369 break;
5372 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5373 continue;
5375 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5376 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5377 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5379 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5380 tex_map |= 1 << stage;
5381 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5382 tfactor_used = TRUE;
5383 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5384 tempreg_used = TRUE;
5386 lowest_disabled_stage = stage;
5388 shader_addline(buffer, "#version 120\n");
5390 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5391 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5393 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5394 shader_addline(buffer, "vec4 ret;\n");
5395 if (tempreg_used || settings->sRGB_write)
5396 shader_addline(buffer, "vec4 temp_reg;\n");
5397 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5399 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5401 if (!(tex_map & (1 << stage)))
5402 continue;
5404 switch (settings->op[stage].tex_type)
5406 case tex_1d:
5407 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5408 break;
5409 case tex_2d:
5410 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5411 break;
5412 case tex_3d:
5413 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5414 break;
5415 case tex_cube:
5416 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5417 break;
5418 case tex_rect:
5419 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5420 break;
5421 default:
5422 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5423 break;
5426 shader_addline(buffer, "vec4 tex%u;\n", stage);
5428 if (!(bump_map & (1 << stage)))
5429 continue;
5430 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5432 if (!(lum_map & (1 << stage)))
5433 continue;
5434 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5435 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5437 if (tfactor_used)
5438 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5439 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5441 if (settings->sRGB_write)
5443 shader_addline(buffer, "const vec4 srgb_const0 = ");
5444 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5445 shader_addline(buffer, ";\n");
5446 shader_addline(buffer, "const vec4 srgb_const1 = ");
5447 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5448 shader_addline(buffer, ";\n");
5451 shader_addline(buffer, "void main()\n{\n");
5453 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5454 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5456 /* Generate texture sampling instructions) */
5457 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5459 const char *texture_function, *coord_mask;
5460 char tex_reg_name[8];
5461 BOOL proj;
5463 if (!(tex_map & (1 << stage)))
5464 continue;
5466 if (settings->op[stage].projected == proj_none)
5468 proj = FALSE;
5470 else if (settings->op[stage].projected == proj_count4
5471 || settings->op[stage].projected == proj_count3)
5473 proj = TRUE;
5475 else
5477 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5478 proj = TRUE;
5481 switch (settings->op[stage].tex_type)
5483 case tex_1d:
5484 if (proj)
5486 texture_function = "texture1DProj";
5487 coord_mask = "xw";
5489 else
5491 texture_function = "texture1D";
5492 coord_mask = "x";
5494 break;
5495 case tex_2d:
5496 if (proj)
5498 texture_function = "texture2DProj";
5499 coord_mask = "xyw";
5501 else
5503 texture_function = "texture2D";
5504 coord_mask = "xy";
5506 break;
5507 case tex_3d:
5508 if (proj)
5510 texture_function = "texture3DProj";
5511 coord_mask = "xyzw";
5513 else
5515 texture_function = "texture3D";
5516 coord_mask = "xyz";
5518 break;
5519 case tex_cube:
5520 texture_function = "textureCube";
5521 coord_mask = "xyz";
5522 break;
5523 case tex_rect:
5524 if (proj)
5526 texture_function = "texture2DRectProj";
5527 coord_mask = "xyw";
5529 else
5531 texture_function = "texture2DRect";
5532 coord_mask = "xy";
5534 break;
5535 default:
5536 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5537 texture_function = "";
5538 coord_mask = "xyzw";
5539 break;
5542 if (stage > 0
5543 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5544 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5546 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5548 /* With projective textures, texbem only divides the static
5549 * texture coord, not the displacement, so multiply the
5550 * displacement with the dividing parameter before passing it to
5551 * TXP. */
5552 if (settings->op[stage].projected != proj_none)
5554 if (settings->op[stage].projected == proj_count4)
5556 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5557 stage, stage);
5558 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5560 else
5562 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5563 stage, stage);
5564 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5567 else
5569 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5572 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5573 stage, texture_function, stage, coord_mask);
5575 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5576 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5577 stage, stage - 1, stage - 1, stage - 1);
5579 else if (settings->op[stage].projected == proj_count3)
5581 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5582 stage, texture_function, stage, stage);
5584 else
5586 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5587 stage, texture_function, stage, stage, coord_mask);
5590 sprintf(tex_reg_name, "tex%u", stage);
5591 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5592 settings->op[stage].color_fixup);
5595 /* Generate the main shader */
5596 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5598 BOOL op_equal;
5600 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5602 if (!stage)
5603 final_combiner_src = "gl_Color";
5604 break;
5607 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5608 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5609 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5610 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5611 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5612 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5613 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5614 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5615 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5616 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5617 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5618 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5619 else
5620 op_equal = settings->op[stage].aop == settings->op[stage].cop
5621 && settings->op[stage].carg0 == settings->op[stage].aarg0
5622 && settings->op[stage].carg1 == settings->op[stage].aarg1
5623 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5625 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5627 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5628 settings->op[stage].cop, settings->op[stage].carg0,
5629 settings->op[stage].carg1, settings->op[stage].carg2);
5630 if (!stage)
5631 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5633 else if (op_equal)
5635 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5636 settings->op[stage].cop, settings->op[stage].carg0,
5637 settings->op[stage].carg1, settings->op[stage].carg2);
5639 else
5641 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5642 settings->op[stage].cop, settings->op[stage].carg0,
5643 settings->op[stage].carg1, settings->op[stage].carg2);
5644 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5645 settings->op[stage].aop, settings->op[stage].aarg0,
5646 settings->op[stage].aarg1, settings->op[stage].aarg2);
5650 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5652 if (settings->sRGB_write)
5653 shader_glsl_generate_srgb_write_correction(buffer);
5655 shader_glsl_generate_fog_code(buffer, settings->fog);
5657 shader_addline(buffer, "}\n");
5659 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5660 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5661 return shader_obj;
5664 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5665 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5667 struct glsl_ffp_vertex_shader *shader;
5668 const struct wine_rb_entry *entry;
5670 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5671 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5673 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5674 return NULL;
5676 shader->desc.settings = *settings;
5677 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5678 list_init(&shader->linked_programs);
5679 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5680 ERR("Failed to insert ffp vertex shader.\n");
5682 return shader;
5685 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5686 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5688 struct glsl_ffp_fragment_shader *glsl_desc;
5689 const struct ffp_frag_desc *desc;
5691 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5692 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5694 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5695 return NULL;
5697 glsl_desc->entry.settings = *args;
5698 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5699 list_init(&glsl_desc->linked_programs);
5700 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5702 return glsl_desc;
5706 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5707 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5709 unsigned int i;
5710 char name[32];
5712 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5713 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5714 for (i = 0; i < vs_c_count; ++i)
5716 snprintf(name, sizeof(name), "vs_c[%u]", i);
5717 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5719 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5720 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5722 for (i = 0; i < MAX_CONST_I; ++i)
5724 snprintf(name, sizeof(name), "vs_i[%u]", i);
5725 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5728 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5731 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5732 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5734 unsigned int i;
5735 char name[32];
5737 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5738 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5739 for (i = 0; i < ps_c_count; ++i)
5741 snprintf(name, sizeof(name), "ps_c[%u]", i);
5742 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5744 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5745 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5747 for (i = 0; i < MAX_CONST_I; ++i)
5749 snprintf(name, sizeof(name), "ps_i[%u]", i);
5750 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5753 for (i = 0; i < MAX_TEXTURES; ++i)
5755 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5756 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5757 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5758 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5759 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5760 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5763 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5764 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5765 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5766 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5769 /* Context activation is done by the caller. */
5770 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5771 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5773 const struct wined3d_gl_info *gl_info = context->gl_info;
5774 const struct ps_np2fixup_info *np2fixup_info = NULL;
5775 struct glsl_shader_prog_link *entry = NULL;
5776 struct wined3d_shader *vshader = NULL;
5777 struct wined3d_shader *gshader = NULL;
5778 struct wined3d_shader *pshader = NULL;
5779 GLhandleARB programId = 0;
5780 GLhandleARB reorder_shader_id = 0;
5781 unsigned int i;
5782 GLhandleARB vs_id = 0;
5783 GLhandleARB gs_id = 0;
5784 GLhandleARB ps_id = 0;
5785 struct list *ps_list, *vs_list;
5786 struct wined3d_device *device = context->swapchain->device;
5788 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5790 vs_id = ctx_data->glsl_program->vs.id;
5791 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5793 if (use_vs(state))
5795 vshader = state->vertex_shader;
5796 gshader = state->geometry_shader;
5798 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5799 && ctx_data->glsl_program->gs.id)
5800 gs_id = ctx_data->glsl_program->gs.id;
5801 else if (gshader)
5802 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5805 else if (use_vs(state))
5807 struct vs_compile_args vs_compile_args;
5808 vshader = state->vertex_shader;
5810 find_vs_compile_args(state, vshader, device->stream_info.swizzle_map, &vs_compile_args);
5811 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5812 vs_list = &vshader->linked_programs;
5814 if ((gshader = state->geometry_shader))
5815 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5817 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5819 struct glsl_ffp_vertex_shader *ffp_shader;
5820 struct wined3d_ffp_vs_settings settings;
5822 wined3d_ffp_get_vs_settings(state, &device->stream_info, &settings);
5823 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5824 vs_id = ffp_shader->id;
5825 vs_list = &ffp_shader->linked_programs;
5828 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5830 ps_id = ctx_data->glsl_program->ps.id;
5831 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5833 if (use_ps(state))
5834 pshader = state->pixel_shader;
5836 else if (use_ps(state))
5838 struct ps_compile_args ps_compile_args;
5839 pshader = state->pixel_shader;
5840 find_ps_compile_args(state, pshader, device->stream_info.position_transformed, &ps_compile_args, gl_info);
5841 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5842 pshader, &ps_compile_args, &np2fixup_info);
5843 ps_list = &pshader->linked_programs;
5845 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5847 struct glsl_ffp_fragment_shader *ffp_shader;
5848 struct ffp_frag_settings settings;
5850 gen_ffp_frag_op(context, state, &settings, FALSE);
5851 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5852 ps_id = ffp_shader->id;
5853 ps_list = &ffp_shader->linked_programs;
5856 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5858 ctx_data->glsl_program = entry;
5859 return;
5862 /* If we get to this point, then no matching program exists, so we create one */
5863 programId = GL_EXTCALL(glCreateProgramObjectARB());
5864 TRACE("Created new GLSL shader program %u\n", programId);
5866 /* Create the entry */
5867 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5868 entry->programId = programId;
5869 entry->vs.id = vs_id;
5870 entry->gs.id = gs_id;
5871 entry->ps.id = ps_id;
5872 entry->constant_version = 0;
5873 entry->ps.np2_fixup_info = np2fixup_info;
5874 /* Add the hash table entry */
5875 add_glsl_program_entry(priv, entry);
5877 /* Set the current program */
5878 ctx_data->glsl_program = entry;
5880 /* Attach GLSL vshader */
5881 if (vs_id)
5883 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5884 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5885 checkGLcall("glAttachObjectARB");
5887 list_add_head(vs_list, &entry->vs.shader_entry);
5890 if (vshader)
5892 WORD map = vshader->reg_maps.input_registers;
5893 char tmp_name[10];
5895 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5896 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5897 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5898 checkGLcall("glAttachObjectARB");
5899 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5900 * is destroyed
5902 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5904 /* Bind vertex attributes to a corresponding index number to match
5905 * the same index numbers as ARB_vertex_programs (makes loading
5906 * vertex attributes simpler). With this method, we can use the
5907 * exact same code to load the attributes later for both ARB and
5908 * GLSL shaders.
5910 * We have to do this here because we need to know the Program ID
5911 * in order to make the bindings work, and it has to be done prior
5912 * to linking the GLSL program. */
5913 for (i = 0; map; map >>= 1, ++i)
5915 if (!(map & 1)) continue;
5917 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5918 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5920 checkGLcall("glBindAttribLocationARB");
5923 if (gshader)
5925 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5926 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5927 checkGLcall("glAttachObjectARB");
5929 TRACE("input type %s, output type %s, vertices out %u.\n",
5930 debug_d3dprimitivetype(gshader->u.gs.input_type),
5931 debug_d3dprimitivetype(gshader->u.gs.output_type),
5932 gshader->u.gs.vertices_out);
5933 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5934 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5935 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5936 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5937 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5938 gshader->u.gs.vertices_out));
5939 checkGLcall("glProgramParameteriARB");
5941 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5944 /* Attach GLSL pshader */
5945 if (ps_id)
5947 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5948 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5949 checkGLcall("glAttachObjectARB");
5951 list_add_head(ps_list, &entry->ps.shader_entry);
5954 /* Link the program */
5955 TRACE("Linking GLSL shader program %u\n", programId);
5956 GL_EXTCALL(glLinkProgramARB(programId));
5957 shader_glsl_validate_link(gl_info, programId);
5959 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5960 vshader ? vshader->limits.constant_float : 0);
5961 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5962 pshader ? pshader->limits.constant_float : 0);
5963 checkGLcall("Find glsl program uniform locations");
5965 if (pshader && pshader->reg_maps.shader_version.major >= 3
5966 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5968 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5969 entry->vs.vertex_color_clamp = GL_FALSE;
5971 else
5973 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5976 /* Set the shader to allow uniform loading on it */
5977 GL_EXTCALL(glUseProgramObjectARB(programId));
5978 checkGLcall("glUseProgramObjectARB(programId)");
5980 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5981 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5982 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5983 * vertex shader with fixed function pixel processing is used we make sure that the card
5984 * supports enough samplers to allow the max number of vertex samplers with all possible
5985 * fixed function fragment processing setups. So once the program is linked these samplers
5986 * won't change.
5988 shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
5989 shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
5991 entry->constant_update_mask = 0;
5992 if (vshader)
5994 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
5995 if (vshader->reg_maps.integer_constants)
5996 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
5997 if (vshader->reg_maps.boolean_constants)
5998 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
5999 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6002 if (ps_id)
6004 if (pshader)
6006 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6007 if (pshader->reg_maps.integer_constants)
6008 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6009 if (pshader->reg_maps.boolean_constants)
6010 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6011 if (entry->ps.ycorrection_location != -1)
6012 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6014 else
6016 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6019 for (i = 0; i < MAX_TEXTURES; ++i)
6021 if (entry->ps.bumpenv_mat_location[i] != -1)
6023 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6024 break;
6028 if (entry->ps.np2_fixup_location != -1)
6029 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6033 /* Context activation is done by the caller. */
6034 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6036 GLhandleARB program_id;
6037 GLhandleARB vshader_id, pshader_id;
6038 const char *blt_pshader;
6040 static const char *blt_vshader =
6041 "#version 120\n"
6042 "void main(void)\n"
6043 "{\n"
6044 " gl_Position = gl_Vertex;\n"
6045 " gl_FrontColor = vec4(1.0);\n"
6046 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6047 "}\n";
6049 static const char * const blt_pshaders_full[tex_type_count] =
6051 /* tex_1d */
6052 NULL,
6053 /* tex_2d */
6054 "#version 120\n"
6055 "uniform sampler2D sampler;\n"
6056 "void main(void)\n"
6057 "{\n"
6058 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6059 "}\n",
6060 /* tex_3d */
6061 NULL,
6062 /* tex_cube */
6063 "#version 120\n"
6064 "uniform samplerCube sampler;\n"
6065 "void main(void)\n"
6066 "{\n"
6067 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6068 "}\n",
6069 /* tex_rect */
6070 "#version 120\n"
6071 "#extension GL_ARB_texture_rectangle : enable\n"
6072 "uniform sampler2DRect sampler;\n"
6073 "void main(void)\n"
6074 "{\n"
6075 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6076 "}\n",
6079 static const char * const blt_pshaders_masked[tex_type_count] =
6081 /* tex_1d */
6082 NULL,
6083 /* tex_2d */
6084 "#version 120\n"
6085 "uniform sampler2D sampler;\n"
6086 "uniform vec4 mask;\n"
6087 "void main(void)\n"
6088 "{\n"
6089 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6090 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6091 "}\n",
6092 /* tex_3d */
6093 NULL,
6094 /* tex_cube */
6095 "#version 120\n"
6096 "uniform samplerCube sampler;\n"
6097 "uniform vec4 mask;\n"
6098 "void main(void)\n"
6099 "{\n"
6100 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6101 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6102 "}\n",
6103 /* tex_rect */
6104 "#version 120\n"
6105 "#extension GL_ARB_texture_rectangle : enable\n"
6106 "uniform sampler2DRect sampler;\n"
6107 "uniform vec4 mask;\n"
6108 "void main(void)\n"
6109 "{\n"
6110 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6111 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6112 "}\n",
6115 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6116 if (!blt_pshader)
6118 FIXME("tex_type %#x not supported\n", tex_type);
6119 return 0;
6122 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6123 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6125 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6126 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6128 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6129 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6130 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6131 GL_EXTCALL(glLinkProgramARB(program_id));
6133 shader_glsl_validate_link(gl_info, program_id);
6135 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6136 * is destroyed
6138 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6139 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6140 return program_id;
6143 /* Context activation is done by the caller. */
6144 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6145 const struct wined3d_state *state)
6147 struct glsl_context_data *ctx_data = context->shader_backend_data;
6148 const struct wined3d_gl_info *gl_info = context->gl_info;
6149 struct shader_glsl_priv *priv = shader_priv;
6150 GLhandleARB program_id = 0, prev_id = 0;
6151 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6153 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6154 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6156 if (ctx_data->glsl_program)
6158 prev_id = ctx_data->glsl_program->programId;
6159 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6161 else
6163 prev_id = 0;
6164 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6167 set_glsl_shader_program(context, state, priv, ctx_data);
6169 if (ctx_data->glsl_program)
6171 program_id = ctx_data->glsl_program->programId;
6172 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6174 else
6176 program_id = 0;
6177 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6180 if (old_vertex_color_clamp != current_vertex_color_clamp)
6182 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6184 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6185 checkGLcall("glClampColorARB");
6187 else
6189 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6193 TRACE("Using GLSL program %u.\n", program_id);
6195 if (prev_id != program_id)
6197 GL_EXTCALL(glUseProgramObjectARB(program_id));
6198 checkGLcall("glUseProgramObjectARB");
6200 if (program_id)
6201 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6205 /* "context" is not necessarily the currently active context. */
6206 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6208 struct glsl_context_data *ctx_data = context->shader_backend_data;
6210 ctx_data->glsl_program = NULL;
6211 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6212 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6213 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6216 /* Context activation is done by the caller. */
6217 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6219 const struct wined3d_gl_info *gl_info = context->gl_info;
6220 struct shader_glsl_priv *priv = shader_priv;
6222 shader_glsl_invalidate_current_program(context);
6223 GL_EXTCALL(glUseProgramObjectARB(0));
6224 checkGLcall("glUseProgramObjectARB");
6226 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6227 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6229 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6231 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6232 checkGLcall("glClampColorARB");
6236 /* Context activation is done by the caller. */
6237 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6238 enum tex_types tex_type, const SIZE *ds_mask_size)
6240 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6241 struct shader_glsl_priv *priv = shader_priv;
6242 GLhandleARB *blt_program;
6243 GLint loc;
6245 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6246 if (!*blt_program)
6248 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6249 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6250 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6251 GL_EXTCALL(glUniform1iARB(loc, 0));
6253 else
6255 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6258 if (masked)
6260 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6261 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6265 /* Context activation is done by the caller. */
6266 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6268 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6269 GLhandleARB program_id;
6271 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6272 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6274 GL_EXTCALL(glUseProgramObjectARB(program_id));
6275 checkGLcall("glUseProgramObjectARB");
6278 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6279 const struct glsl_shader_prog_link *program)
6281 const struct glsl_context_data *ctx_data;
6282 struct wined3d_context *context;
6283 unsigned int i;
6285 for (i = 0; i < device->context_count; ++i)
6287 context = device->contexts[i];
6288 ctx_data = context->shader_backend_data;
6290 if (ctx_data->glsl_program == program)
6291 shader_glsl_invalidate_current_program(context);
6295 static void shader_glsl_destroy(struct wined3d_shader *shader)
6297 struct glsl_shader_private *shader_data = shader->backend_data;
6298 struct wined3d_device *device = shader->device;
6299 struct shader_glsl_priv *priv = device->shader_priv;
6300 const struct wined3d_gl_info *gl_info;
6301 const struct list *linked_programs;
6302 struct wined3d_context *context;
6304 if (!shader_data || !shader_data->num_gl_shaders)
6306 HeapFree(GetProcessHeap(), 0, shader_data);
6307 shader->backend_data = NULL;
6308 return;
6311 context = context_acquire(device, NULL);
6312 gl_info = context->gl_info;
6314 TRACE("Deleting linked programs.\n");
6315 linked_programs = &shader->linked_programs;
6316 if (linked_programs->next)
6318 struct glsl_shader_prog_link *entry, *entry2;
6319 UINT i;
6321 switch (shader->reg_maps.shader_version.type)
6323 case WINED3D_SHADER_TYPE_PIXEL:
6325 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6327 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6329 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6330 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6331 checkGLcall("glDeleteObjectARB");
6333 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6335 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6336 struct glsl_shader_prog_link, ps.shader_entry)
6338 shader_glsl_invalidate_contexts_program(device, entry);
6339 delete_glsl_program_entry(priv, gl_info, entry);
6342 break;
6345 case WINED3D_SHADER_TYPE_VERTEX:
6347 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6349 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6351 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6352 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6353 checkGLcall("glDeleteObjectARB");
6355 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6357 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6358 struct glsl_shader_prog_link, vs.shader_entry)
6360 shader_glsl_invalidate_contexts_program(device, entry);
6361 delete_glsl_program_entry(priv, gl_info, entry);
6364 break;
6367 case WINED3D_SHADER_TYPE_GEOMETRY:
6369 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6371 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6373 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6374 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6375 checkGLcall("glDeleteObjectARB");
6377 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6379 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6380 struct glsl_shader_prog_link, gs.shader_entry)
6382 shader_glsl_invalidate_contexts_program(device, entry);
6383 delete_glsl_program_entry(priv, gl_info, entry);
6386 break;
6389 default:
6390 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6391 break;
6395 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6396 shader->backend_data = NULL;
6398 context_release(context);
6401 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6403 const struct glsl_program_key *k = key;
6404 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6405 const struct glsl_shader_prog_link, program_lookup_entry);
6407 if (k->vs_id > prog->vs.id) return 1;
6408 else if (k->vs_id < prog->vs.id) return -1;
6410 if (k->gs_id > prog->gs.id) return 1;
6411 else if (k->gs_id < prog->gs.id) return -1;
6413 if (k->ps_id > prog->ps.id) return 1;
6414 else if (k->ps_id < prog->ps.id) return -1;
6416 return 0;
6419 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6421 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6422 + constant_count * sizeof(*heap->contained)
6423 + constant_count * sizeof(*heap->positions);
6424 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6426 if (!mem)
6428 ERR("Failed to allocate memory\n");
6429 return FALSE;
6432 heap->entries = mem;
6433 heap->entries[1].version = 0;
6434 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6435 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6436 heap->positions = (unsigned int *)(heap->contained + constant_count);
6437 heap->size = 1;
6439 return TRUE;
6442 static void constant_heap_free(struct constant_heap *heap)
6444 HeapFree(GetProcessHeap(), 0, heap->entries);
6447 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6449 wined3d_rb_alloc,
6450 wined3d_rb_realloc,
6451 wined3d_rb_free,
6452 glsl_program_key_compare,
6455 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6456 const struct fragment_pipeline *fragment_pipe)
6458 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6459 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6460 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6461 gl_info->limits.glsl_ps_float_constants)) + 1;
6462 struct fragment_caps fragment_caps;
6463 void *vertex_priv, *fragment_priv;
6465 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6467 ERR("Failed to initialize vertex pipe.\n");
6468 HeapFree(GetProcessHeap(), 0, priv);
6469 return E_FAIL;
6472 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6474 ERR("Failed to initialize fragment pipe.\n");
6475 vertex_pipe->vp_free(device);
6476 HeapFree(GetProcessHeap(), 0, priv);
6477 return E_FAIL;
6480 if (!shader_buffer_init(&priv->shader_buffer))
6482 ERR("Failed to initialize shader buffer.\n");
6483 goto fail;
6486 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6487 if (!priv->stack)
6489 ERR("Failed to allocate memory.\n");
6490 goto fail;
6493 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6495 ERR("Failed to initialize vertex shader constant heap\n");
6496 goto fail;
6499 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6501 ERR("Failed to initialize pixel shader constant heap\n");
6502 goto fail;
6505 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6507 ERR("Failed to initialize rbtree.\n");
6508 goto fail;
6511 priv->next_constant_version = 1;
6512 priv->vertex_pipe = vertex_pipe;
6513 priv->fragment_pipe = fragment_pipe;
6514 fragment_pipe->get_caps(gl_info, &fragment_caps);
6515 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6517 device->vertex_priv = vertex_priv;
6518 device->fragment_priv = fragment_priv;
6519 device->shader_priv = priv;
6521 return WINED3D_OK;
6523 fail:
6524 constant_heap_free(&priv->pconst_heap);
6525 constant_heap_free(&priv->vconst_heap);
6526 HeapFree(GetProcessHeap(), 0, priv->stack);
6527 shader_buffer_free(&priv->shader_buffer);
6528 fragment_pipe->free_private(device);
6529 vertex_pipe->vp_free(device);
6530 HeapFree(GetProcessHeap(), 0, priv);
6531 return E_OUTOFMEMORY;
6534 /* Context activation is done by the caller. */
6535 static void shader_glsl_free(struct wined3d_device *device)
6537 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6538 struct shader_glsl_priv *priv = device->shader_priv;
6539 int i;
6541 for (i = 0; i < tex_type_count; ++i)
6543 if (priv->depth_blt_program_full[i])
6545 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6547 if (priv->depth_blt_program_masked[i])
6549 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6553 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6554 constant_heap_free(&priv->pconst_heap);
6555 constant_heap_free(&priv->vconst_heap);
6556 HeapFree(GetProcessHeap(), 0, priv->stack);
6557 shader_buffer_free(&priv->shader_buffer);
6558 priv->fragment_pipe->free_private(device);
6559 priv->vertex_pipe->vp_free(device);
6561 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6562 device->shader_priv = NULL;
6565 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6567 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6568 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6571 static void shader_glsl_free_context_data(struct wined3d_context *context)
6573 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6576 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6578 UINT shader_model;
6580 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6581 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6582 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED])
6583 shader_model = 4;
6584 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6585 * texldd and texldl instructions. */
6586 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6587 shader_model = 3;
6588 else
6589 shader_model = 2;
6590 TRACE("Shader model %u.\n", shader_model);
6592 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6593 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6594 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6596 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6597 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6599 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6600 * Direct3D minimum requirement.
6602 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6603 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6605 * The problem is that the refrast clamps temporary results in the shader to
6606 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6607 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6608 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6609 * offer a way to query this.
6611 caps->ps_1x_max_value = 8.0;
6613 /* Ideally we'd only set caps like sRGB writes here if supported by both
6614 * the shader backend and the fragment pipe, but we can get called before
6615 * shader_glsl_alloc(). */
6616 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6617 | WINED3D_SHADER_CAP_SRGB_WRITE;
6620 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6622 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6624 TRACE("Checking support for fixup:\n");
6625 dump_color_fixup_desc(fixup);
6628 /* We support everything except YUV conversions. */
6629 if (!is_complex_fixup(fixup))
6631 TRACE("[OK]\n");
6632 return TRUE;
6635 TRACE("[FAILED]\n");
6636 return FALSE;
6639 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6641 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6642 /* WINED3DSIH_ADD */ shader_glsl_binop,
6643 /* WINED3DSIH_AND */ shader_glsl_binop,
6644 /* WINED3DSIH_BEM */ shader_glsl_bem,
6645 /* WINED3DSIH_BREAK */ shader_glsl_break,
6646 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6647 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6648 /* WINED3DSIH_CALL */ shader_glsl_call,
6649 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6650 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6651 /* WINED3DSIH_CND */ shader_glsl_cnd,
6652 /* WINED3DSIH_CRS */ shader_glsl_cross,
6653 /* WINED3DSIH_CUT */ shader_glsl_cut,
6654 /* WINED3DSIH_DCL */ shader_glsl_nop,
6655 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6656 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6657 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6658 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6659 /* WINED3DSIH_DEF */ shader_glsl_nop,
6660 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6661 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6662 /* WINED3DSIH_DIV */ shader_glsl_binop,
6663 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6664 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6665 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6666 /* WINED3DSIH_DST */ shader_glsl_dst,
6667 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6668 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6669 /* WINED3DSIH_ELSE */ shader_glsl_else,
6670 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6671 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6672 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6673 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6674 /* WINED3DSIH_EQ */ shader_glsl_relop,
6675 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
6676 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6677 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6678 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6679 /* WINED3DSIH_GE */ shader_glsl_relop,
6680 /* WINED3DSIH_IADD */ shader_glsl_binop,
6681 /* WINED3DSIH_IEQ */ NULL,
6682 /* WINED3DSIH_IF */ shader_glsl_if,
6683 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6684 /* WINED3DSIH_IGE */ shader_glsl_relop,
6685 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6686 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6687 /* WINED3DSIH_LABEL */ shader_glsl_label,
6688 /* WINED3DSIH_LD */ NULL,
6689 /* WINED3DSIH_LIT */ shader_glsl_lit,
6690 /* WINED3DSIH_LOG */ shader_glsl_log,
6691 /* WINED3DSIH_LOGP */ shader_glsl_log,
6692 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6693 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6694 /* WINED3DSIH_LT */ shader_glsl_relop,
6695 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6696 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6697 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6698 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6699 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6700 /* WINED3DSIH_MAD */ shader_glsl_mad,
6701 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6702 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6703 /* WINED3DSIH_MOV */ shader_glsl_mov,
6704 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6705 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6706 /* WINED3DSIH_MUL */ shader_glsl_binop,
6707 /* WINED3DSIH_NOP */ shader_glsl_nop,
6708 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6709 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6710 /* WINED3DSIH_POW */ shader_glsl_pow,
6711 /* WINED3DSIH_RCP */ shader_glsl_rcp,
6712 /* WINED3DSIH_REP */ shader_glsl_rep,
6713 /* WINED3DSIH_RET */ shader_glsl_ret,
6714 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6715 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
6716 /* WINED3DSIH_SAMPLE */ NULL,
6717 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6718 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6719 /* WINED3DSIH_SETP */ NULL,
6720 /* WINED3DSIH_SGE */ shader_glsl_compare,
6721 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6722 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6723 /* WINED3DSIH_SLT */ shader_glsl_compare,
6724 /* WINED3DSIH_SQRT */ NULL,
6725 /* WINED3DSIH_SUB */ shader_glsl_binop,
6726 /* WINED3DSIH_TEX */ shader_glsl_tex,
6727 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6728 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6729 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6730 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6731 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6732 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6733 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6734 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6735 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6736 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6737 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6738 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6739 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6740 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6741 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6742 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6743 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6744 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6745 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6746 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6747 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6748 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6749 /* WINED3DSIH_USHR */ shader_glsl_binop,
6750 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6751 /* WINED3DSIH_XOR */ shader_glsl_binop,
6754 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6755 SHADER_HANDLER hw_fct;
6757 /* Select handler */
6758 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6760 /* Unhandled opcode */
6761 if (!hw_fct)
6763 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6764 return;
6766 hw_fct(ins);
6768 shader_glsl_add_instruction_modifiers(ins);
6771 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6773 struct shader_glsl_priv *priv = shader_priv;
6775 return priv->ffp_proj_control;
6778 const struct wined3d_shader_backend_ops glsl_shader_backend =
6780 shader_glsl_handle_instruction,
6781 shader_glsl_select,
6782 shader_glsl_disable,
6783 shader_glsl_select_depth_blt,
6784 shader_glsl_deselect_depth_blt,
6785 shader_glsl_update_float_vertex_constants,
6786 shader_glsl_update_float_pixel_constants,
6787 shader_glsl_load_constants,
6788 shader_glsl_destroy,
6789 shader_glsl_alloc,
6790 shader_glsl_free,
6791 shader_glsl_allocate_context_data,
6792 shader_glsl_free_context_data,
6793 shader_glsl_get_caps,
6794 shader_glsl_color_fixup_supported,
6795 shader_glsl_has_ffp_proj_control,
6798 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6800 if (enable)
6801 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6802 else
6803 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6804 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6807 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6809 caps->xyzrhw = TRUE;
6810 caps->max_active_lights = gl_info->limits.lights;
6811 caps->max_vertex_blend_matrices = 1;
6812 caps->max_vertex_blend_matrix_index = 0;
6813 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6814 | WINED3DVTXPCAPS_MATERIALSOURCE7
6815 | WINED3DVTXPCAPS_VERTEXFOG
6816 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6817 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6818 | WINED3DVTXPCAPS_LOCALVIEWER
6819 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6820 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6821 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6822 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6825 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6827 struct shader_glsl_priv *priv;
6829 if (shader_backend == &glsl_shader_backend)
6831 priv = shader_priv;
6833 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6835 ERR("Failed to initialize rbtree.\n");
6836 return NULL;
6839 return priv;
6842 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6844 return NULL;
6847 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6849 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6850 struct glsl_ffp_vertex_shader, desc.entry);
6851 struct glsl_shader_prog_link *program, *program2;
6852 struct glsl_ffp_destroy_ctx *ctx = context;
6854 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6855 struct glsl_shader_prog_link, vs.shader_entry)
6857 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6859 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6860 HeapFree(GetProcessHeap(), 0, shader);
6863 /* Context activation is done by the caller. */
6864 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6866 struct shader_glsl_priv *priv = device->vertex_priv;
6867 struct glsl_ffp_destroy_ctx ctx;
6869 ctx.priv = priv;
6870 ctx.gl_info = &device->adapter->gl_info;
6871 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6874 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6875 const struct wined3d_state *state, DWORD state_id)
6877 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6880 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6881 const struct wined3d_state *state, DWORD state_id)
6883 /* Table fog behavior depends on the projection matrix. */
6884 if (state->render_states[WINED3D_RS_FOGENABLE]
6885 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6886 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6887 transform_projection(context, state, state_id);
6890 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6892 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6893 {STATE_VSHADER, {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6894 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6895 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6896 /* Clip planes */
6897 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6898 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6899 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6900 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6901 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6902 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6903 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6904 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6905 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6906 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6907 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6908 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6909 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6910 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6911 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6912 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6913 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6914 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6915 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6916 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6917 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6918 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6919 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6920 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6921 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6922 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6923 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6924 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6925 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6926 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6927 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6928 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6929 /* Lights */
6930 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6931 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6932 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6933 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6934 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6935 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6936 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6937 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6938 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6939 /* Viewport */
6940 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6941 /* Transform states */
6942 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6943 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6944 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6945 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6946 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6947 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6948 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6949 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6950 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6951 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6952 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6953 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6954 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6955 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6956 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6957 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6958 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6959 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6960 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6961 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6962 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6963 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6964 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6965 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6966 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6967 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6968 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6969 /* Fog */
6970 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6971 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6972 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6973 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6974 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6975 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6976 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6977 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6978 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6979 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6980 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6981 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6982 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6983 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6984 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6985 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6986 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6987 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
6988 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
6989 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
6990 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
6991 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
6992 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
6993 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6994 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6995 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
6996 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
6997 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
6998 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
6999 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7000 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7001 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7002 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7003 * handler in that case to get the vertex part of sampler() skipped (VTF
7004 * is handled in the misc states). Otherwise, register
7005 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7006 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7007 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7008 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7009 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7010 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7011 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7012 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7013 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7014 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7015 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7016 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7017 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7018 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7019 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7020 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7021 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7022 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7023 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7024 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7025 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7026 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7027 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7028 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7029 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7030 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7031 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7034 /* TODO:
7035 * - This currently depends on GL fixed function functions to set things
7036 * like light parameters. Ideally we'd use regular uniforms for that.
7037 * - In part because of the previous point, much of this is modelled after
7038 * GL fixed function, and has much of the same limitations. For example,
7039 * D3D spot lights are slightly different from GL spot lights.
7040 * - We can now implement drawing transformed vertices using the GLSL pipe,
7041 * instead of using the immediate mode fallback.
7042 * - Similarly, we don't need the fallback for certain combinations of
7043 * material sources anymore.
7044 * - Implement vertex blending and vertex tweening.
7045 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7046 * attribute arrays in load_tex_coords().
7047 * - Per-vertex point sizes. */
7048 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7050 glsl_vertex_pipe_vp_enable,
7051 glsl_vertex_pipe_vp_get_caps,
7052 glsl_vertex_pipe_vp_alloc,
7053 glsl_vertex_pipe_vp_free,
7054 glsl_vertex_pipe_vp_states,
7057 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7059 /* Nothing to do. */
7062 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7064 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7065 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7066 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP;
7067 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7068 | WINED3DTEXOPCAPS_SELECTARG1
7069 | WINED3DTEXOPCAPS_SELECTARG2
7070 | WINED3DTEXOPCAPS_MODULATE4X
7071 | WINED3DTEXOPCAPS_MODULATE2X
7072 | WINED3DTEXOPCAPS_MODULATE
7073 | WINED3DTEXOPCAPS_ADDSIGNED2X
7074 | WINED3DTEXOPCAPS_ADDSIGNED
7075 | WINED3DTEXOPCAPS_ADD
7076 | WINED3DTEXOPCAPS_SUBTRACT
7077 | WINED3DTEXOPCAPS_ADDSMOOTH
7078 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7079 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7080 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7081 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7082 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7083 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7084 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7085 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7086 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7087 | WINED3DTEXOPCAPS_DOTPRODUCT3
7088 | WINED3DTEXOPCAPS_MULTIPLYADD
7089 | WINED3DTEXOPCAPS_LERP
7090 | WINED3DTEXOPCAPS_BUMPENVMAP
7091 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7092 caps->MaxTextureBlendStages = 8;
7093 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7096 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7098 struct shader_glsl_priv *priv;
7100 if (shader_backend == &glsl_shader_backend)
7102 priv = shader_priv;
7104 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7106 ERR("Failed to initialize rbtree.\n");
7107 return NULL;
7110 return priv;
7113 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7115 return NULL;
7118 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7120 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7121 struct glsl_ffp_fragment_shader, entry.entry);
7122 struct glsl_shader_prog_link *program, *program2;
7123 struct glsl_ffp_destroy_ctx *ctx = context;
7125 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7126 struct glsl_shader_prog_link, ps.shader_entry)
7128 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7130 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7131 HeapFree(GetProcessHeap(), 0, shader);
7134 /* Context activation is done by the caller. */
7135 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7137 struct shader_glsl_priv *priv = device->fragment_priv;
7138 struct glsl_ffp_destroy_ctx ctx;
7140 ctx.priv = priv;
7141 ctx.gl_info = &device->adapter->gl_info;
7142 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7145 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7146 const struct wined3d_state *state, DWORD state_id)
7148 context->last_was_pshader = use_ps(state);
7150 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7153 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7154 const struct wined3d_state *state, DWORD state_id)
7156 BOOL use_vshader = use_vs(state);
7157 enum fogsource new_source;
7158 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7159 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7161 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7163 if (!state->render_states[WINED3D_RS_FOGENABLE])
7164 return;
7166 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7168 if (use_vshader)
7169 new_source = FOGSOURCE_VS;
7170 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7171 new_source = FOGSOURCE_COORD;
7172 else
7173 new_source = FOGSOURCE_FFP;
7175 else
7177 new_source = FOGSOURCE_FFP;
7180 if (new_source != context->fog_source || fogstart == fogend)
7182 context->fog_source = new_source;
7183 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7187 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7188 const struct wined3d_state *state, DWORD state_id)
7190 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7193 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7194 const struct wined3d_state *state, DWORD state_id)
7196 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7199 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7201 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7202 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7203 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7204 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7205 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7206 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7207 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7208 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7209 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7210 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7211 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7212 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7213 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7214 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7215 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7216 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7217 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7218 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7219 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7220 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7221 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7223 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7225 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7226 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7227 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7228 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7229 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7230 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7231 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7232 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7233 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7234 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7250 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7252 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7253 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7254 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7255 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7256 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7257 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7259 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7260 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7261 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7263 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7264 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7265 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7266 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7267 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7268 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7269 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7270 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7271 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_PIXELSHADER, {STATE_PIXELSHADER, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7275 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7276 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7279 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7281 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_PIXELSHADER, NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7283 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7284 {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 },
7285 {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 },
7286 {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 },
7287 {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 },
7288 {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 },
7289 {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 },
7290 {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 },
7291 {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 },
7292 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7293 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7296 const struct fragment_pipeline glsl_fragment_pipe =
7298 glsl_fragment_pipe_enable,
7299 glsl_fragment_pipe_get_caps,
7300 glsl_fragment_pipe_alloc,
7301 glsl_fragment_pipe_free,
7302 shader_glsl_color_fixup_supported,
7303 glsl_fragment_pipe_state_template,