dbghelp: Fix compiler warnings with flag -Wunused-macros.
[wine.git] / dlls / wined3d / glsl_shader.c
blob0ab591f8598906090400c4d9500117b017ad2606
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 const char *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_shader_buffer shader_buffer;
95 struct wine_rb_tree program_lookup;
96 struct constant_heap vconst_heap;
97 struct constant_heap pconst_heap;
98 unsigned char *stack;
99 GLhandleARB depth_blt_program_full[tex_type_count];
100 GLhandleARB depth_blt_program_masked[tex_type_count];
101 UINT next_constant_version;
103 const struct wined3d_vertex_pipe_ops *vertex_pipe;
104 const struct fragment_pipeline *fragment_pipe;
105 struct wine_rb_tree ffp_vertex_shaders;
106 struct wine_rb_tree ffp_fragment_shaders;
107 BOOL ffp_proj_control;
110 struct glsl_vs_program
112 struct list shader_entry;
113 GLhandleARB id;
114 GLenum vertex_color_clamp;
115 GLint *uniform_f_locations;
116 GLint uniform_i_locations[MAX_CONST_I];
117 GLint uniform_b_locations[MAX_CONST_B];
118 GLint pos_fixup_location;
121 struct glsl_gs_program
123 struct list shader_entry;
124 GLhandleARB id;
127 struct glsl_ps_program
129 struct list shader_entry;
130 GLhandleARB id;
131 GLint *uniform_f_locations;
132 GLint uniform_i_locations[MAX_CONST_I];
133 GLint uniform_b_locations[MAX_CONST_B];
134 GLint bumpenv_mat_location[MAX_TEXTURES];
135 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
136 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
137 GLint tss_constant_location[MAX_TEXTURES];
138 GLint tex_factor_location;
139 GLint specular_enable_location;
140 GLint ycorrection_location;
141 GLint np2_fixup_location;
142 const struct ps_np2fixup_info *np2_fixup_info;
145 /* Struct to maintain data about a linked GLSL program */
146 struct glsl_shader_prog_link
148 struct wine_rb_entry program_lookup_entry;
149 struct glsl_vs_program vs;
150 struct glsl_gs_program gs;
151 struct glsl_ps_program ps;
152 GLhandleARB programId;
153 DWORD constant_update_mask;
154 UINT constant_version;
157 struct glsl_program_key
159 GLhandleARB vs_id;
160 GLhandleARB gs_id;
161 GLhandleARB ps_id;
164 struct shader_glsl_ctx_priv {
165 const struct vs_compile_args *cur_vs_args;
166 const struct ps_compile_args *cur_ps_args;
167 struct ps_np2fixup_info *cur_np2fixup_info;
170 struct glsl_context_data
172 struct glsl_shader_prog_link *glsl_program;
175 struct glsl_ps_compiled_shader
177 struct ps_compile_args args;
178 struct ps_np2fixup_info np2fixup;
179 GLhandleARB prgId;
182 struct glsl_vs_compiled_shader
184 struct vs_compile_args args;
185 GLhandleARB prgId;
188 struct glsl_gs_compiled_shader
190 GLhandleARB id;
193 struct glsl_shader_private
195 union
197 struct glsl_vs_compiled_shader *vs;
198 struct glsl_gs_compiled_shader *gs;
199 struct glsl_ps_compiled_shader *ps;
200 } gl_shaders;
201 UINT num_gl_shaders, shader_array_size;
204 struct glsl_ffp_vertex_shader
206 struct wined3d_ffp_vs_desc desc;
207 GLhandleARB id;
208 struct list linked_programs;
211 struct glsl_ffp_fragment_shader
213 struct ffp_frag_desc entry;
214 GLhandleARB id;
215 struct list linked_programs;
218 struct glsl_ffp_destroy_ctx
220 struct shader_glsl_priv *priv;
221 const struct wined3d_gl_info *gl_info;
224 static const char *debug_gl_shader_type(GLenum type)
226 switch (type)
228 #define WINED3D_TO_STR(u) case u: return #u
229 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
230 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
231 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
232 #undef WINED3D_TO_STR
233 default:
234 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
238 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
240 switch (type)
242 case WINED3D_SHADER_TYPE_VERTEX:
243 return "vs";
245 case WINED3D_SHADER_TYPE_GEOMETRY:
246 return "gs";
248 case WINED3D_SHADER_TYPE_PIXEL:
249 return "ps";
251 default:
252 FIXME("Unhandled shader type %#x.\n", type);
253 return "unknown";
257 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
259 char str[4][17];
261 wined3d_ftoa(values[0], str[0]);
262 wined3d_ftoa(values[1], str[1]);
263 wined3d_ftoa(values[2], str[2]);
264 wined3d_ftoa(values[3], str[3]);
265 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
268 /* Extract a line from the info log.
269 * Note that this modifies the source string. */
270 static char *get_info_log_line(char **ptr)
272 char *p, *q;
274 p = *ptr;
275 if (!(q = strstr(p, "\n")))
277 if (!*p) return NULL;
278 *ptr += strlen(p);
279 return p;
281 *q = '\0';
282 *ptr = q + 1;
284 return p;
287 /* Context activation is done by the caller. */
288 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
290 int infologLength = 0;
291 char *infoLog;
293 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
294 return;
296 GL_EXTCALL(glGetObjectParameterivARB(obj,
297 GL_OBJECT_INFO_LOG_LENGTH_ARB,
298 &infologLength));
300 /* A size of 1 is just a null-terminated string, so the log should be bigger than
301 * that if there are errors. */
302 if (infologLength > 1)
304 char *ptr, *line;
306 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
307 /* The info log is supposed to be zero-terminated, but at least some
308 * versions of fglrx don't terminate the string properly. The reported
309 * length does include the terminator, so explicitly set it to zero
310 * here. */
311 infoLog[infologLength - 1] = 0;
312 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
314 ptr = infoLog;
315 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
317 WARN("Info log received from GLSL shader #%u:\n", obj);
318 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
320 else
322 FIXME("Info log received from GLSL shader #%u:\n", obj);
323 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
325 HeapFree(GetProcessHeap(), 0, infoLog);
329 /* Context activation is done by the caller. */
330 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
332 TRACE("Compiling shader object %u.\n", shader);
333 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
334 checkGLcall("glShaderSourceARB");
335 GL_EXTCALL(glCompileShaderARB(shader));
336 checkGLcall("glCompileShaderARB");
337 print_glsl_info_log(gl_info, shader);
340 /* Context activation is done by the caller. */
341 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
343 GLint i, object_count, source_size = -1;
344 GLhandleARB *objects;
345 char *source = NULL;
347 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
348 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
349 if (!objects)
351 ERR("Failed to allocate object array memory.\n");
352 return;
355 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
356 for (i = 0; i < object_count; ++i)
358 char *ptr, *line;
359 GLint tmp;
361 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
363 if (source_size < tmp)
365 HeapFree(GetProcessHeap(), 0, source);
367 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
368 if (!source)
370 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
371 HeapFree(GetProcessHeap(), 0, objects);
372 return;
374 source_size = tmp;
377 FIXME("Object %u:\n", objects[i]);
378 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
379 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
380 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
381 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
382 FIXME("\n");
384 ptr = source;
385 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
386 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
387 FIXME("\n");
390 HeapFree(GetProcessHeap(), 0, source);
391 HeapFree(GetProcessHeap(), 0, objects);
394 /* Context activation is done by the caller. */
395 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
397 GLint tmp;
399 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
401 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
402 if (tmp == GL_PROGRAM_OBJECT_ARB)
404 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
405 if (!tmp)
407 FIXME("Program %u link status invalid.\n", program);
408 shader_glsl_dump_program_source(gl_info, program);
412 print_glsl_info_log(gl_info, program);
415 /* Context activation is done by the caller. */
416 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
417 const DWORD *tex_unit_map, GLhandleARB programId)
419 GLint name_loc;
420 char sampler_name[20];
421 unsigned int i;
423 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
425 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
426 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
427 if (name_loc != -1) {
428 DWORD mapped_unit = tex_unit_map[i];
429 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
431 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
432 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
433 checkGLcall("glUniform1iARB");
434 } else {
435 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
441 /* Context activation is done by the caller. */
442 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
443 const DWORD *tex_unit_map, GLhandleARB programId)
445 GLint name_loc;
446 char sampler_name[20];
447 unsigned int i;
449 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
451 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
452 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
453 if (name_loc != -1) {
454 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
455 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
457 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
458 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
459 checkGLcall("glUniform1iARB");
460 } else {
461 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
467 /* Context activation is done by the caller. */
468 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
469 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
471 unsigned int start = ~0U, end = 0;
472 int stack_idx = 0;
473 unsigned int heap_idx = 1;
474 unsigned int idx;
476 if (heap->entries[heap_idx].version <= version) return;
478 idx = heap->entries[heap_idx].idx;
479 if (constant_locations[idx] != -1)
480 start = end = idx;
481 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
483 while (stack_idx >= 0)
485 /* Note that we fall through to the next case statement. */
486 switch(stack[stack_idx])
488 case HEAP_NODE_TRAVERSE_LEFT:
490 unsigned int left_idx = heap_idx << 1;
491 if (left_idx < heap->size && heap->entries[left_idx].version > version)
493 heap_idx = left_idx;
494 idx = heap->entries[heap_idx].idx;
495 if (constant_locations[idx] != -1)
497 if (start > idx)
498 start = idx;
499 if (end < idx)
500 end = idx;
503 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
504 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
505 break;
509 case HEAP_NODE_TRAVERSE_RIGHT:
511 unsigned int right_idx = (heap_idx << 1) + 1;
512 if (right_idx < heap->size && heap->entries[right_idx].version > version)
514 heap_idx = right_idx;
515 idx = heap->entries[heap_idx].idx;
516 if (constant_locations[idx] != -1)
518 if (start > idx)
519 start = idx;
520 if (end < idx)
521 end = idx;
524 stack[stack_idx++] = HEAP_NODE_POP;
525 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
526 break;
530 case HEAP_NODE_POP:
531 heap_idx >>= 1;
532 --stack_idx;
533 break;
536 if (start <= end)
537 GL_EXTCALL(glUniform4fvARB(constant_locations[start], end - start + 1, &constants[start * 4]));
538 checkGLcall("walk_constant_heap()");
541 /* Context activation is done by the caller. */
542 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
544 GLfloat clamped_constant[4];
546 if (location == -1) return;
548 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
549 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
550 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
551 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
553 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
556 /* Context activation is done by the caller. */
557 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
558 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
560 int stack_idx = 0;
561 unsigned int heap_idx = 1;
562 unsigned int idx;
564 if (heap->entries[heap_idx].version <= version) return;
566 idx = heap->entries[heap_idx].idx;
567 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
568 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
570 while (stack_idx >= 0)
572 /* Note that we fall through to the next case statement. */
573 switch(stack[stack_idx])
575 case HEAP_NODE_TRAVERSE_LEFT:
577 unsigned int left_idx = heap_idx << 1;
578 if (left_idx < heap->size && heap->entries[left_idx].version > version)
580 heap_idx = left_idx;
581 idx = heap->entries[heap_idx].idx;
582 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
584 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
585 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
586 break;
590 case HEAP_NODE_TRAVERSE_RIGHT:
592 unsigned int right_idx = (heap_idx << 1) + 1;
593 if (right_idx < heap->size && heap->entries[right_idx].version > version)
595 heap_idx = right_idx;
596 idx = heap->entries[heap_idx].idx;
597 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
599 stack[stack_idx++] = HEAP_NODE_POP;
600 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
601 break;
605 case HEAP_NODE_POP:
606 heap_idx >>= 1;
607 --stack_idx;
608 break;
611 checkGLcall("walk_constant_heap_clamped()");
614 /* Context activation is done by the caller. */
615 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
616 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
617 unsigned char *stack, UINT version)
619 const struct wined3d_shader_lconst *lconst;
621 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
622 if (shader->reg_maps.shader_version.major == 1
623 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
624 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
625 else
626 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
628 if (!shader->load_local_constsF)
630 TRACE("No need to load local float constants for this shader\n");
631 return;
634 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
635 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
637 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
639 checkGLcall("glUniform4fvARB()");
642 /* Context activation is done by the caller. */
643 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
644 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
646 unsigned int i;
647 struct list* ptr;
649 for (i = 0; constants_set; constants_set >>= 1, ++i)
651 if (!(constants_set & 1)) continue;
653 /* We found this uniform name in the program - go ahead and send the data */
654 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
657 /* Load immediate constants */
658 ptr = list_head(&shader->constantsI);
659 while (ptr)
661 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
662 unsigned int idx = lconst->idx;
663 const GLint *values = (const GLint *)lconst->value;
665 /* We found this uniform name in the program - go ahead and send the data */
666 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
667 ptr = list_next(&shader->constantsI, ptr);
669 checkGLcall("glUniform4ivARB()");
672 /* Context activation is done by the caller. */
673 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
674 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
676 unsigned int i;
677 struct list* ptr;
679 for (i = 0; constants_set; constants_set >>= 1, ++i)
681 if (!(constants_set & 1)) continue;
683 GL_EXTCALL(glUniform1ivARB(locations[i], 1, &constants[i]));
686 /* Load immediate constants */
687 ptr = list_head(&shader->constantsB);
688 while (ptr)
690 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
691 unsigned int idx = lconst->idx;
692 const GLint *values = (const GLint *)lconst->value;
694 GL_EXTCALL(glUniform1ivARB(locations[idx], 1, values));
695 ptr = list_next(&shader->constantsB, ptr);
697 checkGLcall("glUniform1ivARB()");
700 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
702 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
705 /* Context activation is done by the caller (state handler). */
706 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
707 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
709 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
710 UINT fixup = ps->np2_fixup_info->active;
711 UINT i;
713 for (i = 0; fixup; fixup >>= 1, ++i)
715 const struct wined3d_texture *tex = state->textures[i];
716 unsigned char idx = ps->np2_fixup_info->idx[i];
717 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
719 if (!tex)
721 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
722 continue;
725 if (idx % 2)
727 tex_dim[2] = tex->pow2_matrix[0];
728 tex_dim[3] = tex->pow2_matrix[5];
730 else
732 tex_dim[0] = tex->pow2_matrix[0];
733 tex_dim[1] = tex->pow2_matrix[5];
737 GL_EXTCALL(glUniform4fvARB(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
740 /* Context activation is done by the caller (state handler). */
741 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
742 const struct wined3d_state *state)
744 const struct glsl_context_data *ctx_data = context->shader_backend_data;
745 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
746 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
747 const struct wined3d_gl_info *gl_info = context->gl_info;
748 struct shader_glsl_priv *priv = shader_priv;
749 float position_fixup[4];
750 DWORD update_mask = 0;
752 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
753 UINT constant_version;
754 int i;
756 if (!prog) {
757 /* No GLSL program set - nothing to do. */
758 return;
760 constant_version = prog->constant_version;
761 update_mask = context->constant_update_mask & prog->constant_update_mask;
763 if (update_mask & WINED3D_SHADER_CONST_VS_F)
764 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
765 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
767 if (update_mask & WINED3D_SHADER_CONST_VS_I)
768 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
769 vshader->reg_maps.integer_constants);
771 if (update_mask & WINED3D_SHADER_CONST_VS_B)
772 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
773 vshader->reg_maps.boolean_constants);
775 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
777 shader_get_position_fixup(context, state, position_fixup);
778 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
779 checkGLcall("glUniform4fvARB");
782 if (update_mask & WINED3D_SHADER_CONST_PS_F)
783 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
784 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
786 if (update_mask & WINED3D_SHADER_CONST_PS_I)
787 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
788 pshader->reg_maps.integer_constants);
790 if (update_mask & WINED3D_SHADER_CONST_PS_B)
791 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
792 pshader->reg_maps.boolean_constants);
794 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
796 for (i = 0; i < MAX_TEXTURES; ++i)
798 if (prog->ps.bumpenv_mat_location[i] == -1)
799 continue;
801 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
802 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
804 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
806 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1,
807 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
808 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1,
809 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
813 checkGLcall("bump env uniforms");
816 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
818 float correction_params[4];
820 if (context->render_offscreen)
822 correction_params[0] = 0.0f;
823 correction_params[1] = 1.0f;
824 } else {
825 /* position is window relative, not viewport relative */
826 correction_params[0] = (float) context->current_rt->resource.height;
827 correction_params[1] = -1.0f;
829 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
832 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
833 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
835 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
837 float col[4];
839 if (prog->ps.tex_factor_location != -1)
841 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
842 GL_EXTCALL(glUniform4fvARB(prog->ps.tex_factor_location, 1, col));
845 if (state->render_states[WINED3D_RS_SPECULARENABLE])
846 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
847 else
848 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
850 for (i = 0; i < MAX_TEXTURES; ++i)
852 if (prog->ps.tss_constant_location[i] == -1)
853 continue;
855 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
856 GL_EXTCALL(glUniform4fvARB(prog->ps.tss_constant_location[i], 1, col));
859 checkGLcall("fixed function uniforms");
862 if (priv->next_constant_version == UINT_MAX)
864 TRACE("Max constant version reached, resetting to 0.\n");
865 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
866 priv->next_constant_version = 1;
868 else
870 prog->constant_version = priv->next_constant_version++;
874 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
876 struct constant_entry *entries = heap->entries;
877 unsigned int *positions = heap->positions;
878 unsigned int heap_idx, parent_idx;
880 if (!heap->contained[idx])
882 heap_idx = heap->size++;
883 heap->contained[idx] = TRUE;
885 else
887 heap_idx = positions[idx];
890 while (heap_idx > 1)
892 parent_idx = heap_idx >> 1;
894 if (new_version <= entries[parent_idx].version) break;
896 entries[heap_idx] = entries[parent_idx];
897 positions[entries[parent_idx].idx] = heap_idx;
898 heap_idx = parent_idx;
901 entries[heap_idx].version = new_version;
902 entries[heap_idx].idx = idx;
903 positions[idx] = heap_idx;
906 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
908 struct shader_glsl_priv *priv = device->shader_priv;
909 struct constant_heap *heap = &priv->vconst_heap;
910 UINT i;
912 for (i = start; i < count + start; ++i)
914 update_heap_entry(heap, i, priv->next_constant_version);
917 for (i = 0; i < device->context_count; ++i)
919 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
923 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
925 struct shader_glsl_priv *priv = device->shader_priv;
926 struct constant_heap *heap = &priv->pconst_heap;
927 UINT i;
929 for (i = start; i < count + start; ++i)
931 update_heap_entry(heap, i, priv->next_constant_version);
934 for (i = 0; i < device->context_count; ++i)
936 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
940 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
942 unsigned int ret = gl_info->limits.glsl_varyings / 4;
943 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
944 if(shader_major > 3) return ret;
946 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
947 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
948 return ret;
951 /** Generate the variable & register declarations for the GLSL output target */
952 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
953 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
954 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
956 const struct wined3d_shader_version *version = &reg_maps->shader_version;
957 const struct wined3d_state *state = &shader->device->state;
958 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
959 const struct wined3d_gl_info *gl_info = context->gl_info;
960 const struct wined3d_fb_state *fb = &shader->device->fb;
961 unsigned int i, extra_constants_needed = 0;
962 const struct wined3d_shader_lconst *lconst;
963 const char *prefix;
964 DWORD map;
966 prefix = shader_glsl_get_prefix(version->type);
968 /* Prototype the subroutines */
969 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
971 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
974 /* Declare the constants (aka uniforms) */
975 if (shader->limits->constant_float > 0)
977 unsigned max_constantsF;
979 /* Unless the shader uses indirect addressing, always declare the
980 * maximum array size and ignore that we need some uniforms privately.
981 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
982 * and immediate values, still declare VC[256]. If the shader needs
983 * more uniforms than we have it won't work in any case. If it uses
984 * less, the compiler will figure out which uniforms are really used
985 * and strip them out. This allows a shader to use c255 on a dx9 card,
986 * as long as it doesn't also use all the other constants.
988 * If the shader uses indirect addressing the compiler must assume
989 * that all declared uniforms are used. In this case, declare only the
990 * amount that we're assured to have.
992 * Thus we run into problems in these two cases:
993 * 1) The shader really uses more uniforms than supported.
994 * 2) The shader uses indirect addressing, less constants than
995 * supported, but uses a constant index > #supported consts. */
996 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
998 /* No indirect addressing here. */
999 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1001 else
1003 if (reg_maps->usesrelconstF)
1005 /* Subtract the other potential uniforms from the max
1006 * available (bools, ints, and 1 row of projection matrix).
1007 * Subtract another uniform for immediate values, which have
1008 * to be loaded via uniform by the driver as well. The shader
1009 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1010 * shader code, so one vec4 should be enough. (Unfortunately
1011 * the Nvidia driver doesn't store 128 and -128 in one float).
1013 * Writing gl_ClipVertex requires one uniform for each
1014 * clipplane as well. */
1015 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1016 if(ctx_priv->cur_vs_args->clip_enabled)
1018 max_constantsF -= gl_info->limits.clipplanes;
1020 max_constantsF -= count_bits(reg_maps->integer_constants);
1021 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1022 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1023 * for now take this into account when calculating the number of available constants
1025 max_constantsF -= count_bits(reg_maps->boolean_constants);
1026 /* Set by driver quirks in directx.c */
1027 max_constantsF -= gl_info->reserved_glsl_constants;
1029 if (max_constantsF < shader->limits->constant_float)
1031 static unsigned int once;
1033 if (!once++)
1034 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1035 " it may not render correctly.\n");
1036 else
1037 WARN("The hardware does not support enough uniform components to run this shader.\n");
1040 else
1042 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1045 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1046 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1049 /* Always declare the full set of constants, the compiler can remove the
1050 * unused ones because d3d doesn't (yet) support indirect int and bool
1051 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1052 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1053 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1055 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1056 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1058 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1060 if (reg_maps->cb_sizes[i])
1061 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1062 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1065 /* Declare texture samplers */
1066 for (i = 0; i < shader->limits->sampler; ++i)
1068 BOOL shadow_sampler, tex_rect;
1070 if (!reg_maps->resource_info[i].type)
1071 continue;
1073 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1074 switch (reg_maps->resource_info[i].type)
1076 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1077 if (shadow_sampler)
1078 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1079 else
1080 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1081 break;
1083 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1084 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1085 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1086 if (shadow_sampler)
1088 if (tex_rect)
1089 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1090 else
1091 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1093 else
1095 if (tex_rect)
1096 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1097 else
1098 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1100 break;
1102 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1103 if (shadow_sampler)
1104 FIXME("Unsupported 3D shadow sampler.\n");
1105 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1106 break;
1108 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1109 if (shadow_sampler)
1110 FIXME("Unsupported Cube shadow sampler.\n");
1111 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1112 break;
1114 default:
1115 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1116 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1117 break;
1121 /* Declare uniforms for NP2 texcoord fixup:
1122 * This is NOT done inside the loop that declares the texture samplers
1123 * since the NP2 fixup code is currently only used for the GeforceFX
1124 * series and when forcing the ARB_npot extension off. Modern cards just
1125 * skip the code anyway, so put it inside a separate loop. */
1126 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1128 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1129 UINT cur = 0;
1131 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1132 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1133 * samplerNP2Fixup stores texture dimensions and is updated through
1134 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1136 for (i = 0; i < shader->limits->sampler; ++i)
1138 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1139 continue;
1141 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1143 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1144 continue;
1147 fixup->idx[i] = cur++;
1150 fixup->num_consts = (cur + 1) >> 1;
1151 fixup->active = ps_args->np2_fixup;
1152 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1155 /* Declare address variables */
1156 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1158 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1161 /* Declare texture coordinate temporaries and initialize them */
1162 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1164 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1167 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1169 /* Declare attributes. */
1170 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1172 if (map & 1)
1173 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1176 shader_addline(buffer, "uniform vec4 posFixup;\n");
1177 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1179 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1181 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1183 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1185 if (version->major >= 3)
1187 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1189 if (use_vs(state))
1190 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1191 else
1192 /* TODO: Write a replacement shader for the fixed function
1193 * vertex pipeline, so this isn't needed. For fixed function
1194 * vertex processing + 3.0 pixel shader we need a separate
1195 * function in the pixel shader that reads the fixed function
1196 * color into the packed input registers. */
1197 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1200 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1202 if (!(map & 1))
1203 continue;
1205 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1207 if (reg_maps->luminanceparams & (1 << i))
1209 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1210 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1211 extra_constants_needed++;
1214 extra_constants_needed++;
1217 if (ps_args->srgb_correction)
1219 shader_addline(buffer, "const vec4 srgb_const0 = ");
1220 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1221 shader_addline(buffer, ";\n");
1222 shader_addline(buffer, "const vec4 srgb_const1 = ");
1223 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1224 shader_addline(buffer, ";\n");
1226 if (reg_maps->vpos || reg_maps->usesdsy)
1228 if (shader->limits->constant_float + extra_constants_needed
1229 + 1 < gl_info->limits.glsl_ps_float_constants)
1231 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1232 extra_constants_needed++;
1234 else
1236 float ycorrection[] =
1238 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1239 context->render_offscreen ? 1.0f : -1.0f,
1240 0.0f,
1241 0.0f,
1244 /* This happens because we do not have proper tracking of the
1245 * constant registers that are actually used, only the max
1246 * limit of the shader version. */
1247 FIXME("Cannot find a free uniform for vpos correction params\n");
1248 shader_addline(buffer, "const vec4 ycorrection = ");
1249 shader_glsl_append_imm_vec4(buffer, ycorrection);
1250 shader_addline(buffer, ";\n");
1252 shader_addline(buffer, "vec4 vpos;\n");
1256 /* Declare output register temporaries */
1257 if (shader->limits->packed_output)
1258 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1260 /* Declare temporary variables */
1261 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1263 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1266 /* Declare loop registers aLx */
1267 if (version->major < 4)
1269 for (i = 0; i < reg_maps->loop_depth; ++i)
1271 shader_addline(buffer, "int aL%u;\n", i);
1272 shader_addline(buffer, "int tmpInt%u;\n", i);
1276 /* Temporary variables for matrix operations */
1277 shader_addline(buffer, "vec4 tmp0;\n");
1278 shader_addline(buffer, "vec4 tmp1;\n");
1280 if (!shader->load_local_constsF)
1282 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1284 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1285 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1286 shader_addline(buffer, ";\n");
1290 /* Start the main program. */
1291 shader_addline(buffer, "void main()\n{\n");
1293 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1294 * add approximately 0.5. This causes off-by-one problems as spotted by
1295 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1296 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1297 * causes precision troubles when we just subtract 0.5.
1299 * To deal with that, just floor() the position. This will eliminate the
1300 * fraction on all cards.
1302 * TODO: Test how this behaves with multisampling.
1304 * An advantage of floor is that it works even if the driver doesn't add
1305 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1306 * to return in gl_FragCoord, even though coordinates specify the pixel
1307 * centers instead of the pixel corners. This code will behave correctly
1308 * on drivers that returns integer values. */
1309 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1310 shader_addline(buffer,
1311 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1314 /*****************************************************************************
1315 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1317 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1318 ****************************************************************************/
1320 /* Prototypes */
1321 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1322 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1324 /** Used for opcode modifiers - They multiply the result by the specified amount */
1325 static const char * const shift_glsl_tab[] = {
1326 "", /* 0 (none) */
1327 "2.0 * ", /* 1 (x2) */
1328 "4.0 * ", /* 2 (x4) */
1329 "8.0 * ", /* 3 (x8) */
1330 "16.0 * ", /* 4 (x16) */
1331 "32.0 * ", /* 5 (x32) */
1332 "", /* 6 (x64) */
1333 "", /* 7 (x128) */
1334 "", /* 8 (d256) */
1335 "", /* 9 (d128) */
1336 "", /* 10 (d64) */
1337 "", /* 11 (d32) */
1338 "0.0625 * ", /* 12 (d16) */
1339 "0.125 * ", /* 13 (d8) */
1340 "0.25 * ", /* 14 (d4) */
1341 "0.5 * " /* 15 (d2) */
1344 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1345 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1346 const char *in_reg, const char *in_regswizzle, char *out_str)
1348 out_str[0] = 0;
1350 switch (src_modifier)
1352 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1353 case WINED3DSPSM_DW:
1354 case WINED3DSPSM_NONE:
1355 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1356 break;
1357 case WINED3DSPSM_NEG:
1358 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1359 break;
1360 case WINED3DSPSM_NOT:
1361 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1362 break;
1363 case WINED3DSPSM_BIAS:
1364 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1365 break;
1366 case WINED3DSPSM_BIASNEG:
1367 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1368 break;
1369 case WINED3DSPSM_SIGN:
1370 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1371 break;
1372 case WINED3DSPSM_SIGNNEG:
1373 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1374 break;
1375 case WINED3DSPSM_COMP:
1376 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1377 break;
1378 case WINED3DSPSM_X2:
1379 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1380 break;
1381 case WINED3DSPSM_X2NEG:
1382 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1383 break;
1384 case WINED3DSPSM_ABS:
1385 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1386 break;
1387 case WINED3DSPSM_ABSNEG:
1388 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1389 break;
1390 default:
1391 FIXME("Unhandled modifier %u\n", src_modifier);
1392 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1396 /** Writes the GLSL variable name that corresponds to the register that the
1397 * DX opcode parameter is trying to access */
1398 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1399 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1401 /* oPos, oFog and oPts in D3D */
1402 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1404 const struct wined3d_shader *shader = ins->ctx->shader;
1405 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1406 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1407 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1408 const char *prefix = shader_glsl_get_prefix(version->type);
1409 struct glsl_src_param rel_param0, rel_param1;
1410 char imm_str[4][17];
1412 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1413 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1414 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1415 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1416 *is_color = FALSE;
1418 switch (reg->type)
1420 case WINED3DSPR_TEMP:
1421 sprintf(register_name, "R%u", reg->idx[0].offset);
1422 break;
1424 case WINED3DSPR_INPUT:
1425 /* vertex shaders */
1426 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1428 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1429 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1430 *is_color = TRUE;
1431 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1432 break;
1435 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1437 if (reg->idx[0].rel_addr)
1439 if (reg->idx[1].rel_addr)
1440 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1441 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1442 else
1443 sprintf(register_name, "gs_in[%s + %u][%u]",
1444 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1446 else if (reg->idx[1].rel_addr)
1447 sprintf(register_name, "gs_in[%u][%s + %u]",
1448 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1449 else
1450 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1451 break;
1454 /* pixel shaders >= 3.0 */
1455 if (version->major >= 3)
1457 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1458 unsigned int in_count = vec4_varyings(version->major, gl_info);
1460 if (reg->idx[0].rel_addr)
1462 /* Removing a + 0 would be an obvious optimization, but
1463 * OS X doesn't see the NOP operation there. */
1464 if (idx)
1466 if (shader->u.ps.declared_in_count > in_count)
1468 sprintf(register_name,
1469 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1470 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1471 prefix, rel_param0.param_str, idx);
1473 else
1475 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1478 else
1480 if (shader->u.ps.declared_in_count > in_count)
1482 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1483 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1484 prefix, rel_param0.param_str);
1486 else
1488 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1492 else
1494 if (idx == in_count) sprintf(register_name, "gl_Color");
1495 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1496 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1499 else
1501 if (!reg->idx[0].offset)
1502 strcpy(register_name, "gl_Color");
1503 else
1504 strcpy(register_name, "gl_SecondaryColor");
1505 break;
1507 break;
1509 case WINED3DSPR_CONST:
1511 /* Relative addressing */
1512 if (reg->idx[0].rel_addr)
1514 if (reg->idx[0].offset)
1515 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1516 else
1517 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1519 else
1521 if (shader_constant_is_local(shader, reg->idx[0].offset))
1522 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1523 else
1524 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1527 break;
1529 case WINED3DSPR_CONSTINT:
1530 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1531 break;
1533 case WINED3DSPR_CONSTBOOL:
1534 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1535 break;
1537 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1538 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1539 sprintf(register_name, "T%u", reg->idx[0].offset);
1540 else
1541 sprintf(register_name, "A%u", reg->idx[0].offset);
1542 break;
1544 case WINED3DSPR_LOOP:
1545 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1546 break;
1548 case WINED3DSPR_SAMPLER:
1549 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1550 break;
1552 case WINED3DSPR_COLOROUT:
1553 if (reg->idx[0].offset >= gl_info->limits.buffers)
1554 WARN("Write to render target %u, only %d supported.\n",
1555 reg->idx[0].offset, gl_info->limits.buffers);
1557 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1558 break;
1560 case WINED3DSPR_RASTOUT:
1561 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1562 break;
1564 case WINED3DSPR_DEPTHOUT:
1565 sprintf(register_name, "gl_FragDepth");
1566 break;
1568 case WINED3DSPR_ATTROUT:
1569 if (!reg->idx[0].offset)
1570 sprintf(register_name, "%s_out[8]", prefix);
1571 else
1572 sprintf(register_name, "%s_out[9]", prefix);
1573 break;
1575 case WINED3DSPR_TEXCRDOUT:
1576 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1577 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1578 break;
1580 case WINED3DSPR_MISCTYPE:
1581 if (!reg->idx[0].offset)
1583 /* vPos */
1584 sprintf(register_name, "vpos");
1586 else if (reg->idx[0].offset == 1)
1588 /* Note that gl_FrontFacing is a bool, while vFace is
1589 * a float for which the sign determines front/back */
1590 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1592 else
1594 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1595 sprintf(register_name, "unrecognized_register");
1597 break;
1599 case WINED3DSPR_IMMCONST:
1600 switch (reg->immconst_type)
1602 case WINED3D_IMMCONST_SCALAR:
1603 switch (reg->data_type)
1605 case WINED3D_DATA_FLOAT:
1606 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1607 break;
1608 case WINED3D_DATA_INT:
1609 sprintf(register_name, "%#x", reg->immconst_data[0]);
1610 break;
1611 case WINED3D_DATA_RESOURCE:
1612 case WINED3D_DATA_SAMPLER:
1613 case WINED3D_DATA_UINT:
1614 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1615 break;
1616 default:
1617 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1618 break;
1620 break;
1622 case WINED3D_IMMCONST_VEC4:
1623 switch (reg->data_type)
1625 case WINED3D_DATA_FLOAT:
1626 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1627 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1628 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1629 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1630 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1631 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1632 break;
1633 case WINED3D_DATA_INT:
1634 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1635 reg->immconst_data[0], reg->immconst_data[1],
1636 reg->immconst_data[2], reg->immconst_data[3]);
1637 break;
1638 case WINED3D_DATA_RESOURCE:
1639 case WINED3D_DATA_SAMPLER:
1640 case WINED3D_DATA_UINT:
1641 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1642 reg->immconst_data[0], reg->immconst_data[1],
1643 reg->immconst_data[2], reg->immconst_data[3]);
1644 break;
1645 default:
1646 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1647 break;
1649 break;
1651 default:
1652 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1653 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1655 break;
1657 case WINED3DSPR_CONSTBUFFER:
1658 if (reg->idx[1].rel_addr)
1659 sprintf(register_name, "%s_cb%u[%s + %u]",
1660 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1661 else
1662 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1663 break;
1665 case WINED3DSPR_PRIMID:
1666 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1667 break;
1669 default:
1670 FIXME("Unhandled register type %#x.\n", reg->type);
1671 sprintf(register_name, "unrecognized_register");
1672 break;
1676 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1678 *str++ = '.';
1679 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1680 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1681 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1682 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1683 *str = '\0';
1686 /* Get the GLSL write mask for the destination register */
1687 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1689 DWORD mask = param->write_mask;
1691 if (shader_is_scalar(&param->reg))
1693 mask = WINED3DSP_WRITEMASK_0;
1694 *write_mask = '\0';
1696 else
1698 shader_glsl_write_mask_to_str(mask, write_mask);
1701 return mask;
1704 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1705 unsigned int size = 0;
1707 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1708 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1709 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1710 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1712 return size;
1715 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1717 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1718 * but addressed as "rgba". To fix this we need to swap the register's x
1719 * and z components. */
1720 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1722 *str++ = '.';
1723 /* swizzle bits fields: wwzzyyxx */
1724 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1725 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1726 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1727 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1728 *str = '\0';
1731 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1732 BOOL fixup, DWORD mask, char *swizzle_str)
1734 if (shader_is_scalar(&param->reg))
1735 *swizzle_str = '\0';
1736 else
1737 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1740 /* From a given parameter token, generate the corresponding GLSL string.
1741 * Also, return the actual register name and swizzle in case the
1742 * caller needs this information as well. */
1743 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1744 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1746 BOOL is_color = FALSE;
1747 char swizzle_str[6];
1749 glsl_src->reg_name[0] = '\0';
1750 glsl_src->param_str[0] = '\0';
1751 swizzle_str[0] = '\0';
1753 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1754 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1756 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1758 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1760 else
1762 char param_str[200];
1764 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1766 switch (wined3d_src->reg.data_type)
1768 case WINED3D_DATA_FLOAT:
1769 sprintf(glsl_src->param_str, "%s", param_str);
1770 break;
1771 case WINED3D_DATA_INT:
1772 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1773 break;
1774 case WINED3D_DATA_RESOURCE:
1775 case WINED3D_DATA_SAMPLER:
1776 case WINED3D_DATA_UINT:
1777 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1778 break;
1779 default:
1780 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1781 sprintf(glsl_src->param_str, "%s", param_str);
1782 break;
1787 /* From a given parameter token, generate the corresponding GLSL string.
1788 * Also, return the actual register name and swizzle in case the
1789 * caller needs this information as well. */
1790 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1791 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1793 BOOL is_color = FALSE;
1795 glsl_dst->mask_str[0] = '\0';
1796 glsl_dst->reg_name[0] = '\0';
1798 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1799 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1802 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1803 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1804 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
1805 enum wined3d_data_type data_type)
1807 struct glsl_dst_param glsl_dst;
1808 DWORD mask;
1810 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1812 switch (data_type)
1814 case WINED3D_DATA_FLOAT:
1815 shader_addline(buffer, "%s%s = %s(",
1816 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1817 break;
1818 case WINED3D_DATA_INT:
1819 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1820 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1821 break;
1822 case WINED3D_DATA_RESOURCE:
1823 case WINED3D_DATA_SAMPLER:
1824 case WINED3D_DATA_UINT:
1825 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1826 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1827 break;
1828 default:
1829 FIXME("Unhandled data type %#x.\n", data_type);
1830 shader_addline(buffer, "%s%s = %s(",
1831 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1832 break;
1836 return mask;
1839 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1840 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1842 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
1845 /** Process GLSL instruction modifiers */
1846 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1848 struct glsl_dst_param dst_param;
1849 DWORD modifiers;
1851 if (!ins->dst_count) return;
1853 modifiers = ins->dst[0].modifiers;
1854 if (!modifiers) return;
1856 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1858 if (modifiers & WINED3DSPDM_SATURATE)
1860 /* _SAT means to clamp the value of the register to between 0 and 1 */
1861 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1862 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1865 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1867 FIXME("_centroid modifier not handled\n");
1870 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1872 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1876 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1878 switch (op)
1880 case WINED3D_SHADER_REL_OP_GT: return ">";
1881 case WINED3D_SHADER_REL_OP_EQ: return "==";
1882 case WINED3D_SHADER_REL_OP_GE: return ">=";
1883 case WINED3D_SHADER_REL_OP_LT: return "<";
1884 case WINED3D_SHADER_REL_OP_NE: return "!=";
1885 case WINED3D_SHADER_REL_OP_LE: return "<=";
1886 default:
1887 FIXME("Unrecognized operator %#x.\n", op);
1888 return "(\?\?)";
1892 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1893 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
1895 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
1896 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1897 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1898 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
1899 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1900 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1901 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1902 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1904 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
1906 /* Note that there's no such thing as a projected cube texture. */
1907 switch (resource_type)
1909 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1910 if (shadow)
1912 if (lod)
1914 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1916 else if (grad)
1918 if (gl_info->supported[EXT_GPU_SHADER4])
1919 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1920 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1921 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1922 else
1924 FIXME("Unsupported 1D shadow grad function.\n");
1925 sample_function->name = "unsupported1DGrad";
1928 else
1930 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1932 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1934 else
1936 if (lod)
1938 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1940 else if (grad)
1942 if (gl_info->supported[EXT_GPU_SHADER4])
1943 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1944 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1945 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1946 else
1948 FIXME("Unsupported 1D grad function.\n");
1949 sample_function->name = "unsupported1DGrad";
1952 else
1954 sample_function->name = projected ? "texture1DProj" : "texture1D";
1956 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1958 break;
1960 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1961 if (shadow)
1963 if (texrect)
1965 if (lod)
1967 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1969 else if (grad)
1971 if (gl_info->supported[EXT_GPU_SHADER4])
1972 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1973 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1974 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1975 else
1977 FIXME("Unsupported RECT shadow grad function.\n");
1978 sample_function->name = "unsupported2DRectGrad";
1981 else
1983 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1986 else
1988 if (lod)
1990 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1992 else if (grad)
1994 if (gl_info->supported[EXT_GPU_SHADER4])
1995 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1996 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1997 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1998 else
2000 FIXME("Unsupported 2D shadow grad function.\n");
2001 sample_function->name = "unsupported2DGrad";
2004 else
2006 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2009 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2011 else
2013 if (texrect)
2015 if (lod)
2017 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2019 else if (grad)
2021 if (gl_info->supported[EXT_GPU_SHADER4])
2022 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2023 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2024 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2025 else
2027 FIXME("Unsupported RECT grad function.\n");
2028 sample_function->name = "unsupported2DRectGrad";
2031 else
2033 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2036 else
2038 if (lod)
2040 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2042 else if (grad)
2044 if (gl_info->supported[EXT_GPU_SHADER4])
2045 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2046 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2047 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2048 else
2050 FIXME("Unsupported 2D grad function.\n");
2051 sample_function->name = "unsupported2DGrad";
2054 else
2056 sample_function->name = projected ? "texture2DProj" : "texture2D";
2059 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2061 break;
2063 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2064 if (shadow)
2066 FIXME("Unsupported 3D shadow function.\n");
2067 sample_function->name = "unsupported3DShadow";
2068 sample_function->coord_mask = 0;
2070 else
2072 if (lod)
2074 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2076 else if (grad)
2078 if (gl_info->supported[EXT_GPU_SHADER4])
2079 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2080 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2081 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2082 else
2084 FIXME("Unsupported 3D grad function.\n");
2085 sample_function->name = "unsupported3DGrad";
2088 else
2090 sample_function->name = projected ? "texture3DProj" : "texture3D";
2092 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2094 break;
2096 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2097 if (shadow)
2099 FIXME("Unsupported Cube shadow function.\n");
2100 sample_function->name = "unsupportedCubeShadow";
2101 sample_function->coord_mask = 0;
2103 else
2105 if (lod)
2107 sample_function->name = "textureCubeLod";
2109 else if (grad)
2111 if (gl_info->supported[EXT_GPU_SHADER4])
2112 sample_function->name = "textureCubeGrad";
2113 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2114 sample_function->name = "textureCubeGradARB";
2115 else
2117 FIXME("Unsupported Cube grad function.\n");
2118 sample_function->name = "unsupportedCubeGrad";
2121 else
2123 sample_function->name = "textureCube";
2125 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2127 break;
2129 default:
2130 sample_function->name = "";
2131 sample_function->coord_mask = 0;
2132 FIXME("Unhandled resource type %#x.\n", resource_type);
2133 break;
2137 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2138 BOOL sign_fixup, enum fixup_channel_source channel_source)
2140 switch(channel_source)
2142 case CHANNEL_SOURCE_ZERO:
2143 strcat(arguments, "0.0");
2144 break;
2146 case CHANNEL_SOURCE_ONE:
2147 strcat(arguments, "1.0");
2148 break;
2150 case CHANNEL_SOURCE_X:
2151 strcat(arguments, reg_name);
2152 strcat(arguments, ".x");
2153 break;
2155 case CHANNEL_SOURCE_Y:
2156 strcat(arguments, reg_name);
2157 strcat(arguments, ".y");
2158 break;
2160 case CHANNEL_SOURCE_Z:
2161 strcat(arguments, reg_name);
2162 strcat(arguments, ".z");
2163 break;
2165 case CHANNEL_SOURCE_W:
2166 strcat(arguments, reg_name);
2167 strcat(arguments, ".w");
2168 break;
2170 default:
2171 FIXME("Unhandled channel source %#x\n", channel_source);
2172 strcat(arguments, "undefined");
2173 break;
2176 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2179 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2180 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2182 unsigned int mask_size, remaining;
2183 DWORD fixup_mask = 0;
2184 char arguments[256];
2185 char mask_str[6];
2187 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2188 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2189 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2190 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2191 if (!(mask &= fixup_mask))
2192 return;
2194 if (is_complex_fixup(fixup))
2196 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2197 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2198 return;
2201 shader_glsl_write_mask_to_str(mask, mask_str);
2202 mask_size = shader_glsl_get_write_mask_size(mask);
2204 arguments[0] = '\0';
2205 remaining = mask_size;
2206 if (mask & WINED3DSP_WRITEMASK_0)
2208 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2209 if (--remaining) strcat(arguments, ", ");
2211 if (mask & WINED3DSP_WRITEMASK_1)
2213 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2214 if (--remaining) strcat(arguments, ", ");
2216 if (mask & WINED3DSP_WRITEMASK_2)
2218 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2219 if (--remaining) strcat(arguments, ", ");
2221 if (mask & WINED3DSP_WRITEMASK_3)
2223 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2224 if (--remaining) strcat(arguments, ", ");
2227 if (mask_size > 1)
2228 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2229 else
2230 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2233 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2235 char reg_name[256];
2236 BOOL is_color;
2238 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2239 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2242 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2243 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2244 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2246 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2247 char dst_swizzle[6];
2248 struct color_fixup_desc fixup;
2249 BOOL np2_fixup = FALSE;
2250 va_list args;
2252 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2254 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2256 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2257 fixup = priv->cur_ps_args->color_fixup[sampler];
2259 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2260 if(bias) {
2261 FIXME("Biased sampling from NP2 textures is unsupported\n");
2262 } else {
2263 np2_fixup = TRUE;
2267 else
2269 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2272 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2274 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2275 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2277 va_start(args, coord_reg_fmt);
2278 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2279 va_end(args);
2281 if(bias) {
2282 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2283 } else {
2284 if (np2_fixup) {
2285 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2286 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2288 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2289 (idx % 2) ? "zw" : "xy", dst_swizzle);
2290 } else if(dx && dy) {
2291 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2292 } else {
2293 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2297 if(!is_identity_fixup(fixup)) {
2298 shader_glsl_color_correction(ins, fixup);
2302 /*****************************************************************************
2303 * Begin processing individual instruction opcodes
2304 ****************************************************************************/
2306 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2308 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2309 struct glsl_src_param src0_param;
2310 struct glsl_src_param src1_param;
2311 DWORD write_mask;
2312 const char *op;
2314 /* Determine the GLSL operator to use based on the opcode */
2315 switch (ins->handler_idx)
2317 case WINED3DSIH_ADD: op = "+"; break;
2318 case WINED3DSIH_AND: op = "&"; break;
2319 case WINED3DSIH_DIV: op = "/"; break;
2320 case WINED3DSIH_IADD: op = "+"; break;
2321 case WINED3DSIH_ISHL: op = "<<"; break;
2322 case WINED3DSIH_MUL: op = "*"; break;
2323 case WINED3DSIH_SUB: op = "-"; break;
2324 case WINED3DSIH_USHR: op = ">>"; break;
2325 case WINED3DSIH_XOR: op = "^"; break;
2326 default:
2327 op = "<unhandled operator>";
2328 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2329 break;
2332 write_mask = shader_glsl_append_dst(buffer, ins);
2333 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2334 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2335 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2338 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2340 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2341 struct glsl_src_param src0_param;
2342 struct glsl_src_param src1_param;
2343 unsigned int mask_size;
2344 DWORD write_mask;
2345 const char *op;
2347 write_mask = shader_glsl_append_dst(buffer, ins);
2348 mask_size = shader_glsl_get_write_mask_size(write_mask);
2349 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2350 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2352 if (mask_size > 1)
2354 switch (ins->handler_idx)
2356 case WINED3DSIH_EQ: op = "equal"; break;
2357 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2358 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2359 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2360 case WINED3DSIH_LT: op = "lessThan"; break;
2361 default:
2362 op = "<unhandled operator>";
2363 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2364 break;
2367 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2368 mask_size, op, src0_param.param_str, src1_param.param_str);
2370 else
2372 switch (ins->handler_idx)
2374 case WINED3DSIH_EQ: op = "=="; break;
2375 case WINED3DSIH_GE: op = ">="; break;
2376 case WINED3DSIH_IGE: op = ">="; break;
2377 case WINED3DSIH_UGE: op = ">="; break;
2378 case WINED3DSIH_LT: op = "<"; break;
2379 default:
2380 op = "<unhandled operator>";
2381 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2382 break;
2385 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2386 src0_param.param_str, op, src1_param.param_str);
2390 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2392 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2393 struct glsl_src_param src0_param;
2394 struct glsl_src_param src1_param;
2395 DWORD write_mask;
2397 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2398 * not, we can emulate it. */
2399 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2400 FIXME("64-bit integer multiplies not implemented.\n");
2402 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2404 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2405 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2406 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2408 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2409 src0_param.param_str, src1_param.param_str);
2413 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2415 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2416 struct glsl_src_param src0_param, src1_param;
2417 DWORD write_mask;
2419 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2422 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2424 char dst_mask[6];
2426 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2427 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2428 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2429 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2430 dst_mask, src0_param.param_str, src1_param.param_str);
2432 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2433 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2434 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2435 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2437 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2438 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2440 else
2442 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2443 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2444 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2445 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2448 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2450 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2451 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2452 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2453 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2457 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2458 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2460 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2461 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2462 struct glsl_src_param src0_param;
2463 DWORD write_mask;
2465 write_mask = shader_glsl_append_dst(buffer, ins);
2466 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2468 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2469 * shader versions WINED3DSIO_MOVA is used for this. */
2470 if (ins->ctx->reg_maps->shader_version.major == 1
2471 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2472 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2474 /* This is a simple floor() */
2475 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2476 if (mask_size > 1) {
2477 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2478 } else {
2479 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2482 else if(ins->handler_idx == WINED3DSIH_MOVA)
2484 /* We need to *round* to the nearest int here. */
2485 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2487 if (gl_info->supported[EXT_GPU_SHADER4])
2489 if (mask_size > 1)
2490 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2491 else
2492 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2494 else
2496 if (mask_size > 1)
2497 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2498 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2499 else
2500 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2501 src0_param.param_str, src0_param.param_str);
2504 else
2506 shader_addline(buffer, "%s);\n", src0_param.param_str);
2510 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2511 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2513 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2514 struct glsl_src_param src0_param;
2515 struct glsl_src_param src1_param;
2516 DWORD dst_write_mask, src_write_mask;
2517 unsigned int dst_size = 0;
2519 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2520 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2522 /* dp3 works on vec3, dp4 on vec4 */
2523 if (ins->handler_idx == WINED3DSIH_DP4)
2525 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2526 } else {
2527 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2530 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2531 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2533 if (dst_size > 1) {
2534 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2535 } else {
2536 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2540 /* Note that this instruction has some restrictions. The destination write mask
2541 * can't contain the w component, and the source swizzles have to be .xyzw */
2542 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2544 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2545 struct glsl_src_param src0_param;
2546 struct glsl_src_param src1_param;
2547 char dst_mask[6];
2549 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2550 shader_glsl_append_dst(ins->ctx->buffer, ins);
2551 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2552 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2553 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2556 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2558 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2561 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2562 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2563 * GLSL uses the value as-is. */
2564 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2566 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2567 struct glsl_src_param src0_param;
2568 struct glsl_src_param src1_param;
2569 DWORD dst_write_mask;
2570 unsigned int dst_size;
2572 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2573 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2575 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2576 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2578 if (dst_size > 1)
2580 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2581 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2583 else
2585 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2586 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2590 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2591 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2593 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2594 struct glsl_src_param src_param;
2595 const char *instruction;
2596 DWORD write_mask;
2597 unsigned i;
2599 /* Determine the GLSL function to use based on the opcode */
2600 /* TODO: Possibly make this a table for faster lookups */
2601 switch (ins->handler_idx)
2603 case WINED3DSIH_MIN: instruction = "min"; break;
2604 case WINED3DSIH_MAX: instruction = "max"; break;
2605 case WINED3DSIH_ABS: instruction = "abs"; break;
2606 case WINED3DSIH_FRC: instruction = "fract"; break;
2607 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2608 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2609 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2610 default: instruction = "";
2611 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2612 break;
2615 write_mask = shader_glsl_append_dst(buffer, ins);
2617 shader_addline(buffer, "%s(", instruction);
2619 if (ins->src_count)
2621 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2622 shader_addline(buffer, "%s", src_param.param_str);
2623 for (i = 1; i < ins->src_count; ++i)
2625 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2626 shader_addline(buffer, ", %s", src_param.param_str);
2630 shader_addline(buffer, "));\n");
2633 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2635 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2637 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2638 struct glsl_src_param src_param;
2639 unsigned int mask_size;
2640 DWORD write_mask;
2641 char dst_mask[6];
2643 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2644 mask_size = shader_glsl_get_write_mask_size(write_mask);
2645 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2647 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2648 src_param.param_str, src_param.param_str);
2649 shader_glsl_append_dst(buffer, ins);
2651 if (mask_size > 1)
2653 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2654 mask_size, src_param.param_str);
2656 else
2658 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2659 src_param.param_str);
2663 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2665 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2666 struct glsl_src_param src0_param;
2667 const char *prefix, *suffix;
2668 unsigned int dst_size;
2669 DWORD dst_write_mask;
2671 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2672 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2674 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2676 switch (ins->handler_idx)
2678 case WINED3DSIH_EXP:
2679 case WINED3DSIH_EXPP:
2680 prefix = "exp2(";
2681 suffix = ")";
2682 break;
2684 case WINED3DSIH_LOG:
2685 case WINED3DSIH_LOGP:
2686 prefix = "log2(abs(";
2687 suffix = "))";
2688 break;
2690 case WINED3DSIH_RCP:
2691 prefix = "1.0 / ";
2692 suffix = "";
2693 break;
2695 case WINED3DSIH_RSQ:
2696 prefix = "inversesqrt(abs(";
2697 suffix = "))";
2698 break;
2700 default:
2701 prefix = "";
2702 suffix = "";
2703 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2704 break;
2707 if (dst_size > 1)
2708 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2709 else
2710 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2713 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2714 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2715 * dst.x = 2^(floor(src))
2716 * dst.y = src - floor(src)
2717 * dst.z = 2^src (partial precision is allowed, but optional)
2718 * dst.w = 1.0;
2719 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2720 * dst = 2^src; (partial precision is allowed, but optional)
2722 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2724 if (ins->ctx->reg_maps->shader_version.major < 2)
2726 struct glsl_src_param src_param;
2727 char dst_mask[6];
2729 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2731 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2732 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2733 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2734 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2736 shader_glsl_append_dst(ins->ctx->buffer, ins);
2737 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2738 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2739 return;
2742 shader_glsl_scalar_op(ins);
2745 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2747 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2748 struct glsl_src_param src_param;
2749 unsigned int mask_size;
2750 DWORD write_mask;
2752 write_mask = shader_glsl_append_dst(buffer, ins);
2753 mask_size = shader_glsl_get_write_mask_size(write_mask);
2754 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2756 if (mask_size > 1)
2757 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2758 else
2759 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2762 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2764 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2765 struct glsl_src_param src_param;
2766 unsigned int mask_size;
2767 DWORD write_mask;
2769 write_mask = shader_glsl_append_dst(buffer, ins);
2770 mask_size = shader_glsl_get_write_mask_size(write_mask);
2771 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2773 if (mask_size > 1)
2774 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2775 else
2776 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2779 /** Process signed comparison opcodes in GLSL. */
2780 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2782 struct glsl_src_param src0_param;
2783 struct glsl_src_param src1_param;
2784 DWORD write_mask;
2785 unsigned int mask_size;
2787 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2788 mask_size = shader_glsl_get_write_mask_size(write_mask);
2789 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2790 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2792 if (mask_size > 1) {
2793 const char *compare;
2795 switch(ins->handler_idx)
2797 case WINED3DSIH_SLT: compare = "lessThan"; break;
2798 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2799 default: compare = "";
2800 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2803 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2804 src0_param.param_str, src1_param.param_str);
2805 } else {
2806 switch(ins->handler_idx)
2808 case WINED3DSIH_SLT:
2809 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2810 * to return 0.0 but step returns 1.0 because step is not < x
2811 * An alternative is a bvec compare padded with an unused second component.
2812 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2813 * issue. Playing with not() is not possible either because not() does not accept
2814 * a scalar.
2816 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2817 src0_param.param_str, src1_param.param_str);
2818 break;
2819 case WINED3DSIH_SGE:
2820 /* Here we can use the step() function and safe a conditional */
2821 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2822 break;
2823 default:
2824 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2830 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2832 const char *condition_prefix, *condition_suffix;
2833 struct wined3d_shader_dst_param dst;
2834 struct glsl_src_param src0_param;
2835 struct glsl_src_param src1_param;
2836 struct glsl_src_param src2_param;
2837 BOOL temp_destination = FALSE;
2838 DWORD cmp_channel = 0;
2839 unsigned int i, j;
2840 char mask_char[6];
2841 DWORD write_mask;
2843 switch (ins->handler_idx)
2845 case WINED3DSIH_CMP:
2846 condition_prefix = "";
2847 condition_suffix = " >= 0.0";
2848 break;
2850 case WINED3DSIH_CND:
2851 condition_prefix = "";
2852 condition_suffix = " > 0.5";
2853 break;
2855 case WINED3DSIH_MOVC:
2856 condition_prefix = "bool(";
2857 condition_suffix = ")";
2858 break;
2860 default:
2861 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2862 condition_prefix = "<unhandled prefix>";
2863 condition_suffix = "<unhandled suffix>";
2864 break;
2867 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2869 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2870 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2871 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2872 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2874 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2875 condition_prefix, src0_param.param_str, condition_suffix,
2876 src1_param.param_str, src2_param.param_str);
2877 return;
2880 dst = ins->dst[0];
2882 /* Splitting the instruction up in multiple lines imposes a problem:
2883 * The first lines may overwrite source parameters of the following lines.
2884 * Deal with that by using a temporary destination register if needed. */
2885 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2886 && ins->src[0].reg.type == dst.reg.type)
2887 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2888 && ins->src[1].reg.type == dst.reg.type)
2889 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2890 && ins->src[2].reg.type == dst.reg.type))
2891 temp_destination = TRUE;
2893 /* Cycle through all source0 channels. */
2894 for (i = 0; i < 4; ++i)
2896 write_mask = 0;
2897 /* Find the destination channels which use the current source0 channel. */
2898 for (j = 0; j < 4; ++j)
2900 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2902 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2903 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2906 dst.write_mask = ins->dst[0].write_mask & write_mask;
2908 if (temp_destination)
2910 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2911 continue;
2912 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2914 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
2915 continue;
2917 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2918 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2919 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2921 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2922 condition_prefix, src0_param.param_str, condition_suffix,
2923 src1_param.param_str, src2_param.param_str);
2926 if (temp_destination)
2928 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2929 shader_glsl_append_dst(ins->ctx->buffer, ins);
2930 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2934 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2935 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2936 * the compare is done per component of src0. */
2937 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2939 struct glsl_src_param src0_param;
2940 struct glsl_src_param src1_param;
2941 struct glsl_src_param src2_param;
2942 DWORD write_mask;
2943 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2944 ins->ctx->reg_maps->shader_version.minor);
2946 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2948 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2949 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2950 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2951 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2953 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2954 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2955 else
2956 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2957 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2958 return;
2961 shader_glsl_conditional_move(ins);
2964 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2965 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2967 struct glsl_src_param src0_param;
2968 struct glsl_src_param src1_param;
2969 struct glsl_src_param src2_param;
2970 DWORD write_mask;
2972 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2973 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2974 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2975 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2976 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2977 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2980 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2981 Vertex shaders to GLSL codes */
2982 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2984 int i;
2985 int nComponents = 0;
2986 struct wined3d_shader_dst_param tmp_dst = {{0}};
2987 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2988 struct wined3d_shader_instruction tmp_ins;
2990 memset(&tmp_ins, 0, sizeof(tmp_ins));
2992 /* Set constants for the temporary argument */
2993 tmp_ins.ctx = ins->ctx;
2994 tmp_ins.dst_count = 1;
2995 tmp_ins.dst = &tmp_dst;
2996 tmp_ins.src_count = 2;
2997 tmp_ins.src = tmp_src;
2999 switch(ins->handler_idx)
3001 case WINED3DSIH_M4x4:
3002 nComponents = 4;
3003 tmp_ins.handler_idx = WINED3DSIH_DP4;
3004 break;
3005 case WINED3DSIH_M4x3:
3006 nComponents = 3;
3007 tmp_ins.handler_idx = WINED3DSIH_DP4;
3008 break;
3009 case WINED3DSIH_M3x4:
3010 nComponents = 4;
3011 tmp_ins.handler_idx = WINED3DSIH_DP3;
3012 break;
3013 case WINED3DSIH_M3x3:
3014 nComponents = 3;
3015 tmp_ins.handler_idx = WINED3DSIH_DP3;
3016 break;
3017 case WINED3DSIH_M3x2:
3018 nComponents = 2;
3019 tmp_ins.handler_idx = WINED3DSIH_DP3;
3020 break;
3021 default:
3022 break;
3025 tmp_dst = ins->dst[0];
3026 tmp_src[0] = ins->src[0];
3027 tmp_src[1] = ins->src[1];
3028 for (i = 0; i < nComponents; ++i)
3030 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3031 shader_glsl_dot(&tmp_ins);
3032 ++tmp_src[1].reg.idx[0].offset;
3037 The LRP instruction performs a component-wise linear interpolation
3038 between the second and third operands using the first operand as the
3039 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3040 This is equivalent to mix(src2, src1, src0);
3042 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3044 struct glsl_src_param src0_param;
3045 struct glsl_src_param src1_param;
3046 struct glsl_src_param src2_param;
3047 DWORD write_mask;
3049 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3051 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3052 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3053 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3055 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3056 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3059 /** Process the WINED3DSIO_LIT instruction in GLSL:
3060 * dst.x = dst.w = 1.0
3061 * dst.y = (src0.x > 0) ? src0.x
3062 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3063 * where src.w is clamped at +- 128
3065 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3067 struct glsl_src_param src0_param;
3068 struct glsl_src_param src1_param;
3069 struct glsl_src_param src3_param;
3070 char dst_mask[6];
3072 shader_glsl_append_dst(ins->ctx->buffer, ins);
3073 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3075 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3076 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3079 /* The sdk specifies the instruction like this
3080 * dst.x = 1.0;
3081 * if(src.x > 0.0) dst.y = src.x
3082 * else dst.y = 0.0.
3083 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3084 * else dst.z = 0.0;
3085 * dst.w = 1.0;
3086 * (where power = src.w clamped between -128 and 128)
3088 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3089 * dst.x = 1.0 ... No further explanation needed
3090 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3091 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3092 * dst.w = 1.0. ... Nothing fancy.
3094 * So we still have one conditional in there. So do this:
3095 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3097 * 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),
3098 * 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.
3099 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3101 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3102 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3103 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3105 shader_addline(ins->ctx->buffer,
3106 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3107 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3108 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3109 src0_param.param_str, src3_param.param_str, dst_mask);
3112 /** Process the WINED3DSIO_DST instruction in GLSL:
3113 * dst.x = 1.0
3114 * dst.y = src0.x * src0.y
3115 * dst.z = src0.z
3116 * dst.w = src1.w
3118 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3120 struct glsl_src_param src0y_param;
3121 struct glsl_src_param src0z_param;
3122 struct glsl_src_param src1y_param;
3123 struct glsl_src_param src1w_param;
3124 char dst_mask[6];
3126 shader_glsl_append_dst(ins->ctx->buffer, ins);
3127 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3129 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3130 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3131 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3132 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3134 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3135 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3138 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3139 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3140 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3142 * dst.x = cos(src0.?)
3143 * dst.y = sin(src0.?)
3144 * dst.z = dst.z
3145 * dst.w = dst.w
3147 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3149 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3150 struct glsl_src_param src0_param;
3151 DWORD write_mask;
3153 if (ins->ctx->reg_maps->shader_version.major < 4)
3155 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3157 write_mask = shader_glsl_append_dst(buffer, ins);
3158 switch (write_mask)
3160 case WINED3DSP_WRITEMASK_0:
3161 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3162 break;
3164 case WINED3DSP_WRITEMASK_1:
3165 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3166 break;
3168 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3169 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3170 src0_param.param_str, src0_param.param_str);
3171 break;
3173 default:
3174 ERR("Write mask should be .x, .y or .xy\n");
3175 break;
3178 return;
3181 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3184 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3186 char dst_mask[6];
3188 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3189 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3190 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3192 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3193 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3194 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3196 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3197 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3199 else
3201 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3202 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3203 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3206 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3208 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3209 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3210 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3214 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3215 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3216 * generate invalid code
3218 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3220 struct glsl_src_param src0_param;
3221 DWORD write_mask;
3223 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3224 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3226 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3229 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3230 * Start a for() loop where src1.y is the initial value of aL,
3231 * increment aL by src1.z for a total of src1.x iterations.
3232 * Need to use a temporary variable for this operation.
3234 /* FIXME: I don't think nested loops will work correctly this way. */
3235 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3237 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3238 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3239 const struct wined3d_shader *shader = ins->ctx->shader;
3240 const struct wined3d_shader_lconst *constant;
3241 struct glsl_src_param src1_param;
3242 const DWORD *control_values = NULL;
3244 if (ins->ctx->reg_maps->shader_version.major < 4)
3246 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3248 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3249 * class hardware doesn't support real varying indexing, but Microsoft
3250 * designed this feature for Shader model 2.x+. If the loop control is
3251 * known at compile time, the GLSL compiler can unroll the loop, and
3252 * replace indirect addressing with direct addressing. */
3253 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3255 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3257 if (constant->idx == ins->src[1].reg.idx[0].offset)
3259 control_values = constant->value;
3260 break;
3265 if (control_values)
3267 struct wined3d_shader_loop_control loop_control;
3268 loop_control.count = control_values[0];
3269 loop_control.start = control_values[1];
3270 loop_control.step = (int)control_values[2];
3272 if (loop_control.step > 0)
3274 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3275 loop_state->current_depth, loop_control.start,
3276 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3277 loop_state->current_depth, loop_control.step);
3279 else if (loop_control.step < 0)
3281 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3282 loop_state->current_depth, loop_control.start,
3283 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3284 loop_state->current_depth, loop_control.step);
3286 else
3288 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3289 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3290 loop_state->current_depth, loop_control.count,
3291 loop_state->current_depth);
3294 else
3296 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3297 loop_state->current_depth, loop_state->current_reg,
3298 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3299 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3302 ++loop_state->current_reg;
3304 else
3306 shader_addline(buffer, "for (;;)\n{\n");
3309 ++loop_state->current_depth;
3312 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3314 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3316 shader_addline(ins->ctx->buffer, "}\n");
3318 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3320 --loop_state->current_depth;
3321 --loop_state->current_reg;
3324 if (ins->handler_idx == WINED3DSIH_ENDREP)
3326 --loop_state->current_depth;
3330 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3332 const struct wined3d_shader *shader = ins->ctx->shader;
3333 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3334 const struct wined3d_shader_lconst *constant;
3335 struct glsl_src_param src0_param;
3336 const DWORD *control_values = NULL;
3338 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3339 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3341 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3343 if (constant->idx == ins->src[0].reg.idx[0].offset)
3345 control_values = constant->value;
3346 break;
3351 if (control_values)
3353 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3354 loop_state->current_depth, loop_state->current_depth,
3355 control_values[0], loop_state->current_depth);
3357 else
3359 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3360 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3361 loop_state->current_depth, loop_state->current_depth,
3362 src0_param.param_str, loop_state->current_depth);
3365 ++loop_state->current_depth;
3368 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3370 struct glsl_src_param src0_param;
3372 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3373 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3376 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3378 struct glsl_src_param src0_param;
3379 struct glsl_src_param src1_param;
3381 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3382 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3384 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3385 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3388 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3390 shader_addline(ins->ctx->buffer, "} else {\n");
3393 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3395 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3398 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3400 shader_addline(ins->ctx->buffer, "break;\n");
3403 /* FIXME: According to MSDN the compare is done per component. */
3404 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3406 struct glsl_src_param src0_param;
3407 struct glsl_src_param src1_param;
3409 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3410 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3412 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3413 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3416 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3418 struct glsl_src_param src_param;
3420 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3421 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3424 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3426 shader_addline(ins->ctx->buffer, "}\n");
3427 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3430 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3432 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3435 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3437 struct glsl_src_param src1_param;
3439 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3440 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3441 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3444 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3446 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3447 * function only suppresses the unhandled instruction warning
3451 /*********************************************
3452 * Pixel Shader Specific Code begins here
3453 ********************************************/
3454 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3456 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3457 ins->ctx->reg_maps->shader_version.minor);
3458 struct glsl_sample_function sample_function;
3459 DWORD sample_flags = 0;
3460 DWORD resource_idx;
3461 DWORD mask = 0, swizzle;
3462 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3464 /* 1.0-1.4: Use destination register as sampler source.
3465 * 2.0+: Use provided sampler source. */
3466 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3467 resource_idx = ins->dst[0].reg.idx[0].offset;
3468 else
3469 resource_idx = ins->src[1].reg.idx[0].offset;
3471 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3473 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3474 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3475 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
3477 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3478 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3480 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3481 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3483 case WINED3D_TTFF_COUNT1:
3484 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3485 break;
3486 case WINED3D_TTFF_COUNT2:
3487 mask = WINED3DSP_WRITEMASK_1;
3488 break;
3489 case WINED3D_TTFF_COUNT3:
3490 mask = WINED3DSP_WRITEMASK_2;
3491 break;
3492 case WINED3D_TTFF_COUNT4:
3493 case WINED3D_TTFF_DISABLE:
3494 mask = WINED3DSP_WRITEMASK_3;
3495 break;
3499 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3501 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3503 if (src_mod == WINED3DSPSM_DZ) {
3504 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3505 mask = WINED3DSP_WRITEMASK_2;
3506 } else if (src_mod == WINED3DSPSM_DW) {
3507 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3508 mask = WINED3DSP_WRITEMASK_3;
3511 else
3513 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3514 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3516 /* ps 2.0 texldp instruction always divides by the fourth component. */
3517 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3518 mask = WINED3DSP_WRITEMASK_3;
3522 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
3523 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3525 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
3526 mask |= sample_function.coord_mask;
3528 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3529 else swizzle = ins->src[1].swizzle;
3531 /* 1.0-1.3: Use destination register as coordinate source.
3532 1.4+: Use provided coordinate source register. */
3533 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3535 char coord_mask[6];
3536 shader_glsl_write_mask_to_str(mask, coord_mask);
3537 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3538 "T%u%s", resource_idx, coord_mask);
3540 else
3542 struct glsl_src_param coord_param;
3543 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3544 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3546 struct glsl_src_param bias;
3547 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3548 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3549 "%s", coord_param.param_str);
3550 } else {
3551 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
3552 "%s", coord_param.param_str);
3557 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3559 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3560 struct glsl_src_param coord_param, dx_param, dy_param;
3561 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3562 struct glsl_sample_function sample_function;
3563 DWORD sampler_idx;
3564 DWORD swizzle = ins->src[1].swizzle;
3565 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3567 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3569 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3570 shader_glsl_tex(ins);
3571 return;
3574 sampler_idx = ins->src[1].reg.idx[0].offset;
3575 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3576 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3578 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3579 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3580 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3581 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3583 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3584 "%s", coord_param.param_str);
3587 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3589 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3590 struct glsl_src_param coord_param, lod_param;
3591 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3592 struct glsl_sample_function sample_function;
3593 DWORD sampler_idx;
3594 DWORD swizzle = ins->src[1].swizzle;
3595 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3597 sampler_idx = ins->src[1].reg.idx[0].offset;
3598 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3599 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3600 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3602 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3603 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3605 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3607 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3608 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3610 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3611 * However, the NVIDIA drivers allow them in fragment shaders as well,
3612 * even without the appropriate extension. */
3613 WARN("Using %s in fragment shader.\n", sample_function.name);
3615 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3616 "%s", coord_param.param_str);
3619 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3621 /* FIXME: Make this work for more than just 2D textures */
3622 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3623 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3625 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3627 char dst_mask[6];
3629 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3630 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3631 ins->dst[0].reg.idx[0].offset, dst_mask);
3633 else
3635 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3636 DWORD reg = ins->src[0].reg.idx[0].offset;
3637 char dst_swizzle[6];
3639 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3641 if (src_mod == WINED3DSPSM_DZ)
3643 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3644 struct glsl_src_param div_param;
3646 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3648 if (mask_size > 1) {
3649 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3650 } else {
3651 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3654 else if (src_mod == WINED3DSPSM_DW)
3656 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3657 struct glsl_src_param div_param;
3659 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3661 if (mask_size > 1) {
3662 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3663 } else {
3664 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3666 } else {
3667 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3672 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3673 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3674 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3675 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3677 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3678 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3679 struct glsl_sample_function sample_function;
3680 struct glsl_src_param src0_param;
3681 UINT mask_size;
3683 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3685 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3686 * scalar, and projected sampling would require 4.
3688 * It is a dependent read - not valid with conditional NP2 textures
3690 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3691 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3693 switch(mask_size)
3695 case 1:
3696 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3697 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3698 break;
3700 case 2:
3701 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3702 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3703 break;
3705 case 3:
3706 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3707 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3708 break;
3710 default:
3711 FIXME("Unexpected mask size %u\n", mask_size);
3712 break;
3716 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3717 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3718 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3720 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3721 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3722 struct glsl_src_param src0_param;
3723 DWORD dst_mask;
3724 unsigned int mask_size;
3726 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3727 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3728 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3730 if (mask_size > 1) {
3731 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3732 } else {
3733 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3737 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3738 * Calculate the depth as dst.x / dst.y */
3739 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3741 struct glsl_dst_param dst_param;
3743 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3745 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3746 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3747 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3748 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3749 * >= 1.0 or < 0.0
3751 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3752 dst_param.reg_name, dst_param.reg_name);
3755 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3756 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3757 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3758 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3760 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3762 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3763 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3764 struct glsl_src_param src0_param;
3766 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3768 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3769 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3772 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3773 * Calculate the 1st of a 2-row matrix multiplication. */
3774 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3776 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3777 DWORD reg = ins->dst[0].reg.idx[0].offset;
3778 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3779 struct glsl_src_param src0_param;
3781 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3782 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3785 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3786 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3787 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3789 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3790 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3791 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3792 DWORD reg = ins->dst[0].reg.idx[0].offset;
3793 struct glsl_src_param src0_param;
3795 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3796 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3797 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3800 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3802 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3803 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3804 struct glsl_sample_function sample_function;
3805 DWORD reg = ins->dst[0].reg.idx[0].offset;
3806 struct glsl_src_param src0_param;
3808 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3809 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3811 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3813 /* Sample the texture using the calculated coordinates */
3814 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3817 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3818 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3819 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3821 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3822 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3823 struct glsl_sample_function sample_function;
3824 DWORD reg = ins->dst[0].reg.idx[0].offset;
3825 struct glsl_src_param src0_param;
3827 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3828 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3830 /* Dependent read, not valid with conditional NP2 */
3831 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3833 /* Sample the texture using the calculated coordinates */
3834 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3836 tex_mx->current_row = 0;
3839 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3840 * Perform the 3rd row of a 3x3 matrix multiply */
3841 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3843 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3844 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3845 DWORD reg = ins->dst[0].reg.idx[0].offset;
3846 struct glsl_src_param src0_param;
3847 char dst_mask[6];
3849 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3851 shader_glsl_append_dst(ins->ctx->buffer, ins);
3852 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3853 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3855 tex_mx->current_row = 0;
3858 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3859 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3860 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3862 struct glsl_src_param src0_param;
3863 struct glsl_src_param src1_param;
3864 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3865 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3866 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3867 struct glsl_sample_function sample_function;
3868 DWORD reg = ins->dst[0].reg.idx[0].offset;
3869 char coord_mask[6];
3871 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3872 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3874 /* Perform the last matrix multiply operation */
3875 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3876 /* Reflection calculation */
3877 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3879 /* Dependent read, not valid with conditional NP2 */
3880 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3881 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3883 /* Sample the texture */
3884 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3885 NULL, NULL, NULL, "tmp0%s", coord_mask);
3887 tex_mx->current_row = 0;
3890 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3891 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3892 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3894 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3895 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3896 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3897 struct glsl_sample_function sample_function;
3898 DWORD reg = ins->dst[0].reg.idx[0].offset;
3899 struct glsl_src_param src0_param;
3900 char coord_mask[6];
3902 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3904 /* Perform the last matrix multiply operation */
3905 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3907 /* Construct the eye-ray vector from w coordinates */
3908 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3909 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3910 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3912 /* Dependent read, not valid with conditional NP2 */
3913 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3914 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3916 /* Sample the texture using the calculated coordinates */
3917 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3918 NULL, NULL, NULL, "tmp0%s", coord_mask);
3920 tex_mx->current_row = 0;
3923 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3924 * Apply a fake bump map transform.
3925 * texbem is pshader <= 1.3 only, this saves a few version checks
3927 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3929 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3930 struct glsl_sample_function sample_function;
3931 struct glsl_src_param coord_param;
3932 DWORD sampler_idx;
3933 DWORD mask;
3934 DWORD flags;
3935 char coord_mask[6];
3937 sampler_idx = ins->dst[0].reg.idx[0].offset;
3938 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3939 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3941 /* Dependent read, not valid with conditional NP2 */
3942 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3943 mask = sample_function.coord_mask;
3945 shader_glsl_write_mask_to_str(mask, coord_mask);
3947 /* With projected textures, texbem only divides the static texture coord,
3948 * not the displacement, so we can't let GL handle this. */
3949 if (flags & WINED3D_PSARGS_PROJECTED)
3951 DWORD div_mask=0;
3952 char coord_div_mask[3];
3953 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3955 case WINED3D_TTFF_COUNT1:
3956 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3957 break;
3958 case WINED3D_TTFF_COUNT2:
3959 div_mask = WINED3DSP_WRITEMASK_1;
3960 break;
3961 case WINED3D_TTFF_COUNT3:
3962 div_mask = WINED3DSP_WRITEMASK_2;
3963 break;
3964 case WINED3D_TTFF_COUNT4:
3965 case WINED3D_TTFF_DISABLE:
3966 div_mask = WINED3DSP_WRITEMASK_3;
3967 break;
3969 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3970 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3973 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3975 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3976 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3977 coord_param.param_str, coord_mask);
3979 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3981 struct glsl_src_param luminance_param;
3982 struct glsl_dst_param dst_param;
3984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3985 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3987 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
3988 dst_param.reg_name, dst_param.mask_str,
3989 luminance_param.param_str, sampler_idx, sampler_idx);
3993 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3995 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3996 struct glsl_src_param src0_param, src1_param;
3998 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3999 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4001 shader_glsl_append_dst(ins->ctx->buffer, ins);
4002 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4003 src0_param.param_str, sampler_idx, src1_param.param_str);
4006 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4007 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4008 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4010 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4011 struct glsl_sample_function sample_function;
4012 struct glsl_src_param src0_param;
4014 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4016 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4017 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4018 "%s.wx", src0_param.reg_name);
4021 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4022 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4023 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4025 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4026 struct glsl_sample_function sample_function;
4027 struct glsl_src_param src0_param;
4029 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4031 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4032 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4033 "%s.yz", src0_param.reg_name);
4036 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4037 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4038 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4040 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4041 struct glsl_sample_function sample_function;
4042 struct glsl_src_param src0_param;
4044 /* Dependent read, not valid with conditional NP2 */
4045 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4046 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4048 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4049 "%s", src0_param.param_str);
4052 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4053 * If any of the first 3 components are < 0, discard this pixel */
4054 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4056 struct glsl_dst_param dst_param;
4058 /* The argument is a destination parameter, and no writemasks are allowed */
4059 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4060 if (ins->ctx->reg_maps->shader_version.major >= 2)
4062 if (ins->ctx->reg_maps->shader_version.major >= 4)
4063 FIXME("SM4 discard not implemented.\n");
4064 /* 2.0 shaders compare all 4 components in texkill */
4065 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4066 } else {
4067 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4068 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4069 * 4 components are defined, only the first 3 are used
4071 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4075 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4076 * dst = dot2(src0, src1) + src2 */
4077 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4079 struct glsl_src_param src0_param;
4080 struct glsl_src_param src1_param;
4081 struct glsl_src_param src2_param;
4082 DWORD write_mask;
4083 unsigned int mask_size;
4085 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4086 mask_size = shader_glsl_get_write_mask_size(write_mask);
4088 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4089 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4090 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4092 if (mask_size > 1) {
4093 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4094 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4095 } else {
4096 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4097 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4101 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4102 const struct wined3d_shader_signature_element *input_signature,
4103 const struct wined3d_shader_reg_maps *reg_maps,
4104 enum vertexprocessing_mode vertexprocessing)
4106 WORD map = reg_maps->input_registers;
4107 unsigned int i;
4109 for (i = 0; map; map >>= 1, ++i)
4111 const char *semantic_name;
4112 UINT semantic_idx;
4113 char reg_mask[6];
4115 /* Unused */
4116 if (!(map & 1)) continue;
4118 semantic_name = input_signature[i].semantic_name;
4119 semantic_idx = input_signature[i].semantic_idx;
4120 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4122 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4124 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4125 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4126 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4127 else
4128 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4129 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4131 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4133 if (!semantic_idx)
4134 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4135 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4136 else if (semantic_idx == 1)
4137 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4138 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4139 else
4140 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4141 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4143 else
4145 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4146 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4151 /*********************************************
4152 * Vertex Shader Specific Code begins here
4153 ********************************************/
4155 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4157 struct glsl_program_key key;
4159 key.vs_id = entry->vs.id;
4160 key.gs_id = entry->gs.id;
4161 key.ps_id = entry->ps.id;
4163 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4165 ERR("Failed to insert program entry.\n");
4169 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4170 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4172 struct wine_rb_entry *entry;
4173 struct glsl_program_key key;
4175 key.vs_id = vs_id;
4176 key.gs_id = gs_id;
4177 key.ps_id = ps_id;
4179 entry = wine_rb_get(&priv->program_lookup, &key);
4180 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4183 /* Context activation is done by the caller. */
4184 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4185 struct glsl_shader_prog_link *entry)
4187 struct glsl_program_key key;
4189 key.vs_id = entry->vs.id;
4190 key.gs_id = entry->gs.id;
4191 key.ps_id = entry->ps.id;
4192 wine_rb_remove(&priv->program_lookup, &key);
4194 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4195 if (entry->vs.id)
4196 list_remove(&entry->vs.shader_entry);
4197 if (entry->gs.id)
4198 list_remove(&entry->gs.shader_entry);
4199 if (entry->ps.id)
4200 list_remove(&entry->ps.shader_entry);
4201 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4202 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4203 HeapFree(GetProcessHeap(), 0, entry);
4206 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4207 const struct wined3d_gl_info *gl_info, const DWORD *map,
4208 const struct wined3d_shader_signature_element *input_signature,
4209 const struct wined3d_shader_reg_maps *reg_maps_in,
4210 const struct wined3d_shader_signature_element *output_signature,
4211 const struct wined3d_shader_reg_maps *reg_maps_out)
4213 unsigned int i, j;
4214 const char *semantic_name_in;
4215 UINT semantic_idx_in;
4216 DWORD *set;
4217 DWORD in_idx;
4218 unsigned int in_count = vec4_varyings(3, gl_info);
4219 char reg_mask[6];
4220 char destination[50];
4221 WORD input_map, output_map;
4223 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4225 input_map = reg_maps_in->input_registers;
4226 for (i = 0; input_map; input_map >>= 1, ++i)
4228 if (!(input_map & 1)) continue;
4230 in_idx = map[i];
4231 /* Declared, but not read register */
4232 if (in_idx == ~0U) continue;
4233 if (in_idx >= (in_count + 2))
4235 FIXME("More input varyings declared than supported, expect issues.\n");
4236 continue;
4239 if (in_idx == in_count)
4240 sprintf(destination, "gl_FrontColor");
4241 else if (in_idx == in_count + 1)
4242 sprintf(destination, "gl_FrontSecondaryColor");
4243 else
4244 sprintf(destination, "ps_in[%u]", in_idx);
4246 semantic_name_in = input_signature[i].semantic_name;
4247 semantic_idx_in = input_signature[i].semantic_idx;
4248 set[in_idx] = ~0U;
4250 output_map = reg_maps_out->output_registers;
4251 for (j = 0; output_map; output_map >>= 1, ++j)
4253 DWORD mask;
4255 if (!(output_map & 1)
4256 || semantic_idx_in != output_signature[j].semantic_idx
4257 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4258 || !(mask = input_signature[i].mask & output_signature[j].mask))
4259 continue;
4261 set[in_idx] = mask;
4262 shader_glsl_write_mask_to_str(mask, reg_mask);
4264 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4265 destination, reg_mask, j, reg_mask);
4269 for (i = 0; i < in_count + 2; ++i)
4271 unsigned int size;
4273 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4274 continue;
4276 if (set[i] == ~0U) set[i] = 0;
4278 size = 0;
4279 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4280 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4281 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4282 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4283 reg_mask[size] = '\0';
4285 if (i == in_count)
4286 sprintf(destination, "gl_FrontColor");
4287 else if (i == in_count + 1)
4288 sprintf(destination, "gl_FrontSecondaryColor");
4289 else
4290 sprintf(destination, "ps_in[%u]", i);
4292 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4293 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4296 HeapFree(GetProcessHeap(), 0, set);
4299 /* Context activation is done by the caller. */
4300 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4301 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4302 const struct wined3d_gl_info *gl_info)
4304 GLhandleARB ret = 0;
4305 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4306 unsigned int i;
4307 const char *semantic_name;
4308 UINT semantic_idx;
4309 char reg_mask[6];
4310 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4311 WORD map = vs->reg_maps.output_registers;
4313 shader_buffer_clear(buffer);
4315 shader_addline(buffer, "#version 120\n");
4317 if (ps_major < 3)
4319 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4321 for (i = 0; map; map >>= 1, ++i)
4323 DWORD write_mask;
4325 if (!(map & 1)) continue;
4327 semantic_name = output_signature[i].semantic_name;
4328 semantic_idx = output_signature[i].semantic_idx;
4329 write_mask = output_signature[i].mask;
4330 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4332 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4334 if (!semantic_idx)
4335 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4336 reg_mask, i, reg_mask);
4337 else if (semantic_idx == 1)
4338 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4339 reg_mask, i, reg_mask);
4341 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4343 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4344 reg_mask, i, reg_mask);
4346 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4348 if (semantic_idx < 8)
4350 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4351 write_mask |= WINED3DSP_WRITEMASK_3;
4353 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4354 semantic_idx, reg_mask, i, reg_mask);
4355 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4356 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4359 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4361 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4363 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4365 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4368 shader_addline(buffer, "}\n");
4370 else
4372 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4373 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4374 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4375 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4377 /* First, sort out position and point size. Those are not passed to the pixel shader */
4378 for (i = 0; map; map >>= 1, ++i)
4380 if (!(map & 1)) continue;
4382 semantic_name = output_signature[i].semantic_name;
4383 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4385 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4387 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4388 reg_mask, i, reg_mask);
4390 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4392 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4396 /* Then, fix the pixel shader input */
4397 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4398 &ps->reg_maps, output_signature, &vs->reg_maps);
4400 shader_addline(buffer, "}\n");
4403 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4404 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4405 shader_glsl_compile(gl_info, ret, buffer->buffer);
4407 return ret;
4410 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4412 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4413 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4414 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4415 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4416 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4417 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4420 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4422 switch (mode)
4424 case WINED3D_FFP_PS_FOG_OFF:
4425 return;
4427 case WINED3D_FFP_PS_FOG_LINEAR:
4428 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4429 break;
4431 case WINED3D_FFP_PS_FOG_EXP:
4432 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4433 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4434 break;
4436 case WINED3D_FFP_PS_FOG_EXP2:
4437 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4438 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4439 break;
4441 default:
4442 ERR("Invalid fog mode %#x.\n", mode);
4443 return;
4446 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4449 /* Context activation is done by the caller. */
4450 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4451 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4452 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4454 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4455 const struct wined3d_gl_info *gl_info = context->gl_info;
4456 const DWORD *function = shader->function;
4457 struct shader_glsl_ctx_priv priv_ctx;
4459 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4460 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4462 memset(&priv_ctx, 0, sizeof(priv_ctx));
4463 priv_ctx.cur_ps_args = args;
4464 priv_ctx.cur_np2fixup_info = np2fixup_info;
4466 shader_addline(buffer, "#version 120\n");
4468 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4469 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4470 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4471 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4472 /* The spec says that it doesn't have to be explicitly enabled, but the
4473 * nvidia drivers write a warning if we don't do so. */
4474 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4475 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4476 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4477 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4478 if (gl_info->supported[EXT_GPU_SHADER4])
4479 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4481 /* Base Declarations */
4482 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4484 /* Pack 3.0 inputs */
4485 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4486 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4488 /* Base Shader Body */
4489 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4491 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4492 if (reg_maps->shader_version.major < 2)
4494 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4495 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4498 if (args->srgb_correction)
4499 shader_glsl_generate_srgb_write_correction(buffer);
4501 /* SM < 3 does not replace the fog stage. */
4502 if (reg_maps->shader_version.major < 3)
4503 shader_glsl_generate_fog_code(buffer, args->fog);
4505 shader_addline(buffer, "}\n");
4507 TRACE("Compiling shader object %u\n", shader_obj);
4508 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4510 /* Store the shader object */
4511 return shader_obj;
4514 /* Context activation is done by the caller. */
4515 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4516 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4517 const struct vs_compile_args *args)
4519 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4520 const struct wined3d_gl_info *gl_info = context->gl_info;
4521 const DWORD *function = shader->function;
4522 struct shader_glsl_ctx_priv priv_ctx;
4524 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4525 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4527 shader_addline(buffer, "#version 120\n");
4529 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4530 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4531 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4532 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4533 if (gl_info->supported[EXT_GPU_SHADER4])
4534 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4536 memset(&priv_ctx, 0, sizeof(priv_ctx));
4537 priv_ctx.cur_vs_args = args;
4539 /* Base Declarations */
4540 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4542 /* Base Shader Body */
4543 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4545 /* Unpack outputs */
4546 shader_addline(buffer, "order_ps_input(vs_out);\n");
4548 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4549 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4550 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4551 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4553 if (args->fog_src == VS_FOG_Z)
4554 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4555 else if (!reg_maps->fog)
4556 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4558 /* We always store the clipplanes without y inversion */
4559 if (args->clip_enabled)
4560 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4562 /* Write the final position.
4564 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4565 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4566 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4567 * contains 1.0 to allow a mad.
4569 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4570 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4572 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4574 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4575 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4576 * which is the same as z = z * 2 - w.
4578 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4580 shader_addline(buffer, "}\n");
4582 TRACE("Compiling shader object %u\n", shader_obj);
4583 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4585 return shader_obj;
4588 /* Context activation is done by the caller. */
4589 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4590 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4592 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4593 const struct wined3d_gl_info *gl_info = context->gl_info;
4594 const DWORD *function = shader->function;
4595 struct shader_glsl_ctx_priv priv_ctx;
4596 GLhandleARB shader_id;
4598 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4600 shader_addline(buffer, "#version 120\n");
4602 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4603 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4604 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4605 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4606 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4607 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4608 if (gl_info->supported[EXT_GPU_SHADER4])
4609 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4611 memset(&priv_ctx, 0, sizeof(priv_ctx));
4612 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4613 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4614 shader_addline(buffer, "}\n");
4616 TRACE("Compiling shader object %u.\n", shader_id);
4617 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4619 return shader_id;
4622 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4623 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4624 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4626 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4627 struct glsl_shader_private *shader_data;
4628 struct ps_np2fixup_info *np2fixup;
4629 UINT i;
4630 DWORD new_size;
4631 GLhandleARB ret;
4633 if (!shader->backend_data)
4635 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4636 if (!shader->backend_data)
4638 ERR("Failed to allocate backend data.\n");
4639 return 0;
4642 shader_data = shader->backend_data;
4643 gl_shaders = shader_data->gl_shaders.ps;
4645 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4646 * so a linear search is more performant than a hashmap or a binary search
4647 * (cache coherency etc)
4649 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4651 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4653 if (args->np2_fixup)
4654 *np2fixup_info = &gl_shaders[i].np2fixup;
4655 return gl_shaders[i].prgId;
4659 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4660 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4661 if (shader_data->num_gl_shaders)
4663 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4664 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4665 new_size * sizeof(*gl_shaders));
4667 else
4669 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4670 new_size = 1;
4673 if(!new_array) {
4674 ERR("Out of memory\n");
4675 return 0;
4677 shader_data->gl_shaders.ps = new_array;
4678 shader_data->shader_array_size = new_size;
4679 gl_shaders = new_array;
4682 gl_shaders[shader_data->num_gl_shaders].args = *args;
4684 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4685 memset(np2fixup, 0, sizeof(*np2fixup));
4686 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4688 pixelshader_update_resource_types(shader, args->tex_types);
4690 shader_buffer_clear(buffer);
4691 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4692 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4694 return ret;
4697 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4698 const DWORD use_map) {
4699 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4700 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4701 return stored->fog_src == new->fog_src;
4704 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4705 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4706 const struct vs_compile_args *args)
4708 UINT i;
4709 DWORD new_size;
4710 DWORD use_map = context->stream_info.use_map;
4711 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4712 struct glsl_shader_private *shader_data;
4713 GLhandleARB ret;
4715 if (!shader->backend_data)
4717 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4718 if (!shader->backend_data)
4720 ERR("Failed to allocate backend data.\n");
4721 return 0;
4724 shader_data = shader->backend_data;
4725 gl_shaders = shader_data->gl_shaders.vs;
4727 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4728 * so a linear search is more performant than a hashmap or a binary search
4729 * (cache coherency etc)
4731 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4733 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4734 return gl_shaders[i].prgId;
4737 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4739 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4740 if (shader_data->num_gl_shaders)
4742 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4743 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4744 new_size * sizeof(*gl_shaders));
4746 else
4748 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4749 new_size = 1;
4752 if(!new_array) {
4753 ERR("Out of memory\n");
4754 return 0;
4756 shader_data->gl_shaders.vs = new_array;
4757 shader_data->shader_array_size = new_size;
4758 gl_shaders = new_array;
4761 gl_shaders[shader_data->num_gl_shaders].args = *args;
4763 shader_buffer_clear(buffer);
4764 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4765 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4767 return ret;
4770 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4771 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4773 struct glsl_gs_compiled_shader *gl_shaders;
4774 struct glsl_shader_private *shader_data;
4775 GLhandleARB ret;
4777 if (!shader->backend_data)
4779 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4781 ERR("Failed to allocate backend data.\n");
4782 return 0;
4785 shader_data = shader->backend_data;
4786 gl_shaders = shader_data->gl_shaders.gs;
4788 if (shader_data->num_gl_shaders)
4789 return gl_shaders[0].id;
4791 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4793 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4795 ERR("Failed to allocate GL shader array.\n");
4796 return 0;
4798 shader_data->shader_array_size = 1;
4799 gl_shaders = shader_data->gl_shaders.gs;
4801 shader_buffer_clear(buffer);
4802 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4803 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4805 return ret;
4808 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4810 switch (mcs)
4812 case WINED3D_MCS_MATERIAL:
4813 return material;
4814 case WINED3D_MCS_COLOR1:
4815 return "gl_Color";
4816 case WINED3D_MCS_COLOR2:
4817 return "gl_SecondaryColor";
4818 default:
4819 ERR("Invalid material color source %#x.\n", mcs);
4820 return "<invalid>";
4824 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4825 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4827 const char *diffuse, *specular, *emission, *ambient;
4828 enum wined3d_light_type light_type;
4829 unsigned int i;
4831 if (!settings->lighting)
4833 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4834 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4835 return;
4838 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4839 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4840 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4841 shader_addline(buffer, "vec3 dir, dst;\n");
4842 shader_addline(buffer, "float att, t;\n");
4844 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4845 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4846 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4847 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4849 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4851 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4852 switch (light_type)
4854 case WINED3D_LIGHT_POINT:
4855 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4856 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4857 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4858 shader_addline(buffer, "dst.x = 1.0;\n");
4859 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4860 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4861 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4862 if (!settings->normal)
4863 break;
4864 shader_addline(buffer, "dir = normalize(dir);\n");
4865 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4866 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4867 if (settings->localviewer)
4868 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4869 else
4870 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4871 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4872 " * gl_LightSource[%u].specular) / att;\n", i);
4873 break;
4875 case WINED3D_LIGHT_SPOT:
4876 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4877 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4878 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4879 shader_addline(buffer, "dst.x = 1.0;\n");
4880 shader_addline(buffer, "dir = normalize(dir);\n");
4881 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4882 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4883 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4884 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4885 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4886 i, i, i, i);
4887 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4888 if (!settings->normal)
4889 break;
4890 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4891 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4892 if (settings->localviewer)
4893 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4894 else
4895 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4896 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4897 " * gl_LightSource[%u].specular) * att;\n", i);
4898 break;
4900 case WINED3D_LIGHT_DIRECTIONAL:
4901 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4902 if (!settings->normal)
4903 break;
4904 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4905 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4906 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4907 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4908 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4909 " * gl_LightSource[%u].specular;\n", i);
4910 break;
4912 default:
4913 if (light_type)
4914 FIXME("Unhandled light type %#x.\n", light_type);
4915 continue;
4919 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4920 ambient, diffuse, emission);
4921 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4922 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4925 /* Context activation is done by the caller. */
4926 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4927 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4929 GLhandleARB shader_obj;
4930 unsigned int i;
4932 shader_buffer_clear(buffer);
4934 shader_addline(buffer, "#version 120\n");
4935 shader_addline(buffer, "\n");
4936 shader_addline(buffer, "void main()\n{\n");
4937 shader_addline(buffer, "float m;\n");
4938 shader_addline(buffer, "vec3 r;\n");
4940 if (settings->transformed)
4942 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4943 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4944 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4946 else
4948 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4949 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4950 if (settings->clipping)
4951 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4952 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4955 if (!settings->normal)
4956 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4957 else if (settings->normalize)
4958 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4959 else
4960 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4962 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4964 for (i = 0; i < MAX_TEXTURES; ++i)
4966 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4968 case WINED3DTSS_TCI_PASSTHRU:
4969 if (settings->texcoords & (1 << i))
4970 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4971 i, i, i);
4972 break;
4974 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4975 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4976 break;
4978 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4979 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4980 break;
4982 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4983 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4984 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
4985 break;
4987 case WINED3DTSS_TCI_SPHEREMAP:
4988 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
4989 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
4990 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4991 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
4992 break;
4994 default:
4995 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
4996 break;
5000 switch (settings->fog_mode)
5002 case WINED3D_FFP_VS_FOG_OFF:
5003 break;
5005 case WINED3D_FFP_VS_FOG_FOGCOORD:
5006 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5007 break;
5009 case WINED3D_FFP_VS_FOG_RANGE:
5010 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5011 break;
5013 case WINED3D_FFP_VS_FOG_DEPTH:
5014 if (settings->ortho_fog)
5015 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5016 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5017 else if (settings->transformed)
5018 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5019 else
5020 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5021 break;
5023 default:
5024 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5025 break;
5028 if (settings->point_size)
5030 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5031 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5032 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5033 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5036 shader_addline(buffer, "}\n");
5038 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5039 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5041 return shader_obj;
5044 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5045 DWORD argnum, unsigned int stage, DWORD arg)
5047 const char *ret;
5049 if (arg == ARG_UNUSED)
5050 return "<unused arg>";
5052 switch (arg & WINED3DTA_SELECTMASK)
5054 case WINED3DTA_DIFFUSE:
5055 ret = "gl_Color";
5056 break;
5058 case WINED3DTA_CURRENT:
5059 if (!stage)
5060 ret = "gl_Color";
5061 else
5062 ret = "ret";
5063 break;
5065 case WINED3DTA_TEXTURE:
5066 switch (stage)
5068 case 0: ret = "tex0"; break;
5069 case 1: ret = "tex1"; break;
5070 case 2: ret = "tex2"; break;
5071 case 3: ret = "tex3"; break;
5072 case 4: ret = "tex4"; break;
5073 case 5: ret = "tex5"; break;
5074 case 6: ret = "tex6"; break;
5075 case 7: ret = "tex7"; break;
5076 default:
5077 ret = "<invalid texture>";
5078 break;
5080 break;
5082 case WINED3DTA_TFACTOR:
5083 ret = "tex_factor";
5084 break;
5086 case WINED3DTA_SPECULAR:
5087 ret = "gl_SecondaryColor";
5088 break;
5090 case WINED3DTA_TEMP:
5091 ret = "temp_reg";
5092 break;
5094 case WINED3DTA_CONSTANT:
5095 switch (stage)
5097 case 0: ret = "tss_const0"; break;
5098 case 1: ret = "tss_const1"; break;
5099 case 2: ret = "tss_const2"; break;
5100 case 3: ret = "tss_const3"; break;
5101 case 4: ret = "tss_const4"; break;
5102 case 5: ret = "tss_const5"; break;
5103 case 6: ret = "tss_const6"; break;
5104 case 7: ret = "tss_const7"; break;
5105 default:
5106 ret = "<invalid constant>";
5107 break;
5109 break;
5111 default:
5112 return "<unhandled arg>";
5115 if (arg & WINED3DTA_COMPLEMENT)
5117 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5118 if (argnum == 0)
5119 ret = "arg0";
5120 else if (argnum == 1)
5121 ret = "arg1";
5122 else if (argnum == 2)
5123 ret = "arg2";
5126 if (arg & WINED3DTA_ALPHAREPLICATE)
5128 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5129 if (argnum == 0)
5130 ret = "arg0";
5131 else if (argnum == 1)
5132 ret = "arg1";
5133 else if (argnum == 2)
5134 ret = "arg2";
5137 return ret;
5140 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5141 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5143 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5145 if (color && alpha)
5146 dstmask = "";
5147 else if (color)
5148 dstmask = ".xyz";
5149 else
5150 dstmask = ".w";
5152 if (dst == tempreg)
5153 dstreg = "temp_reg";
5154 else
5155 dstreg = "ret";
5157 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5158 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5159 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5161 switch (op)
5163 case WINED3D_TOP_DISABLE:
5164 if (!stage)
5165 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5166 break;
5168 case WINED3D_TOP_SELECT_ARG1:
5169 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5170 break;
5172 case WINED3D_TOP_SELECT_ARG2:
5173 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5174 break;
5176 case WINED3D_TOP_MODULATE:
5177 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5178 break;
5180 case WINED3D_TOP_MODULATE_4X:
5181 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5182 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5183 break;
5185 case WINED3D_TOP_MODULATE_2X:
5186 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5187 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5188 break;
5190 case WINED3D_TOP_ADD:
5191 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5192 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5193 break;
5195 case WINED3D_TOP_ADD_SIGNED:
5196 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5197 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5198 break;
5200 case WINED3D_TOP_ADD_SIGNED_2X:
5201 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5202 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5203 break;
5205 case WINED3D_TOP_SUBTRACT:
5206 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5207 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5208 break;
5210 case WINED3D_TOP_ADD_SMOOTH:
5211 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5212 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5213 break;
5215 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5216 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5217 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5218 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5219 break;
5221 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5222 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5223 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5224 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5225 break;
5227 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5228 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5229 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5230 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5231 break;
5233 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5234 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5235 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5236 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5237 break;
5239 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5240 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5241 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5242 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5243 break;
5245 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5246 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5247 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5248 break;
5250 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5251 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5252 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5253 break;
5255 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5256 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5257 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5258 break;
5259 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5260 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5261 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5262 break;
5264 case WINED3D_TOP_BUMPENVMAP:
5265 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5266 /* These are handled in the first pass, nothing to do. */
5267 break;
5269 case WINED3D_TOP_DOTPRODUCT3:
5270 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5271 dstreg, dstmask, arg1, arg2, dstmask);
5272 break;
5274 case WINED3D_TOP_MULTIPLY_ADD:
5275 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5276 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5277 break;
5279 case WINED3D_TOP_LERP:
5280 /* MSDN isn't quite right here. */
5281 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5282 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5283 break;
5285 default:
5286 FIXME("Unhandled operation %#x.\n", op);
5287 break;
5291 /* Context activation is done by the caller. */
5292 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5293 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5295 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5296 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5297 const char *final_combiner_src = "ret";
5298 UINT lowest_disabled_stage;
5299 GLhandleARB shader_obj;
5300 DWORD arg0, arg1, arg2;
5301 unsigned int stage;
5303 shader_buffer_clear(buffer);
5305 /* Find out which textures are read */
5306 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5308 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5309 break;
5311 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5312 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5313 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5315 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5316 tex_map |= 1 << stage;
5317 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5318 tfactor_used = TRUE;
5319 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5320 tempreg_used = TRUE;
5321 if (settings->op[stage].dst == tempreg)
5322 tempreg_used = TRUE;
5323 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5324 tss_const_map |= 1 << stage;
5326 switch (settings->op[stage].cop)
5328 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5329 lum_map |= 1 << stage;
5330 /* fall through */
5331 case WINED3D_TOP_BUMPENVMAP:
5332 bump_map |= 1 << stage;
5333 /* fall through */
5334 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5335 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5336 tex_map |= 1 << stage;
5337 break;
5339 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5340 tfactor_used = TRUE;
5341 break;
5343 default:
5344 break;
5347 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5348 continue;
5350 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5351 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5352 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5354 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5355 tex_map |= 1 << stage;
5356 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5357 tfactor_used = TRUE;
5358 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5359 tempreg_used = TRUE;
5360 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5361 tss_const_map |= 1 << stage;
5363 lowest_disabled_stage = stage;
5365 shader_addline(buffer, "#version 120\n");
5367 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5368 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5370 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5371 shader_addline(buffer, "vec4 ret;\n");
5372 if (tempreg_used || settings->sRGB_write)
5373 shader_addline(buffer, "vec4 temp_reg;\n");
5374 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5376 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5378 if (tss_const_map & (1 << stage))
5379 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5381 if (!(tex_map & (1 << stage)))
5382 continue;
5384 switch (settings->op[stage].tex_type)
5386 case tex_1d:
5387 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5388 break;
5389 case tex_2d:
5390 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5391 break;
5392 case tex_3d:
5393 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5394 break;
5395 case tex_cube:
5396 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5397 break;
5398 case tex_rect:
5399 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5400 break;
5401 default:
5402 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5403 break;
5406 shader_addline(buffer, "vec4 tex%u;\n", stage);
5408 if (!(bump_map & (1 << stage)))
5409 continue;
5410 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5412 if (!(lum_map & (1 << stage)))
5413 continue;
5414 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5415 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5417 if (tfactor_used)
5418 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5419 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5421 if (settings->sRGB_write)
5423 shader_addline(buffer, "const vec4 srgb_const0 = ");
5424 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5425 shader_addline(buffer, ";\n");
5426 shader_addline(buffer, "const vec4 srgb_const1 = ");
5427 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5428 shader_addline(buffer, ";\n");
5431 shader_addline(buffer, "void main()\n{\n");
5433 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5434 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5436 /* Generate texture sampling instructions) */
5437 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5439 const char *texture_function, *coord_mask;
5440 char tex_reg_name[8];
5441 BOOL proj;
5443 if (!(tex_map & (1 << stage)))
5444 continue;
5446 if (settings->op[stage].projected == proj_none)
5448 proj = FALSE;
5450 else if (settings->op[stage].projected == proj_count4
5451 || settings->op[stage].projected == proj_count3)
5453 proj = TRUE;
5455 else
5457 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5458 proj = TRUE;
5461 switch (settings->op[stage].tex_type)
5463 case tex_1d:
5464 if (proj)
5466 texture_function = "texture1DProj";
5467 coord_mask = "xw";
5469 else
5471 texture_function = "texture1D";
5472 coord_mask = "x";
5474 break;
5475 case tex_2d:
5476 if (proj)
5478 texture_function = "texture2DProj";
5479 coord_mask = "xyw";
5481 else
5483 texture_function = "texture2D";
5484 coord_mask = "xy";
5486 break;
5487 case tex_3d:
5488 if (proj)
5490 texture_function = "texture3DProj";
5491 coord_mask = "xyzw";
5493 else
5495 texture_function = "texture3D";
5496 coord_mask = "xyz";
5498 break;
5499 case tex_cube:
5500 texture_function = "textureCube";
5501 coord_mask = "xyz";
5502 break;
5503 case tex_rect:
5504 if (proj)
5506 texture_function = "texture2DRectProj";
5507 coord_mask = "xyw";
5509 else
5511 texture_function = "texture2DRect";
5512 coord_mask = "xy";
5514 break;
5515 default:
5516 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5517 texture_function = "";
5518 coord_mask = "xyzw";
5519 break;
5522 if (stage > 0
5523 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5524 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5526 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5528 /* With projective textures, texbem only divides the static
5529 * texture coord, not the displacement, so multiply the
5530 * displacement with the dividing parameter before passing it to
5531 * TXP. */
5532 if (settings->op[stage].projected != proj_none)
5534 if (settings->op[stage].projected == proj_count4)
5536 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5537 stage, stage);
5538 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5540 else
5542 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5543 stage, stage);
5544 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5547 else
5549 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5552 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5553 stage, texture_function, stage, coord_mask);
5555 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5556 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5557 stage, stage - 1, stage - 1, stage - 1);
5559 else if (settings->op[stage].projected == proj_count3)
5561 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5562 stage, texture_function, stage, stage);
5564 else
5566 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5567 stage, texture_function, stage, stage, coord_mask);
5570 sprintf(tex_reg_name, "tex%u", stage);
5571 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5572 settings->op[stage].color_fixup);
5575 /* Generate the main shader */
5576 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5578 BOOL op_equal;
5580 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5582 if (!stage)
5583 final_combiner_src = "gl_Color";
5584 break;
5587 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5588 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5589 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5590 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5591 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5592 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5593 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5594 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5595 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5596 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5597 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5598 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5599 else
5600 op_equal = settings->op[stage].aop == settings->op[stage].cop
5601 && settings->op[stage].carg0 == settings->op[stage].aarg0
5602 && settings->op[stage].carg1 == settings->op[stage].aarg1
5603 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5605 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5607 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5608 settings->op[stage].cop, settings->op[stage].carg0,
5609 settings->op[stage].carg1, settings->op[stage].carg2);
5610 if (!stage)
5611 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5613 else if (op_equal)
5615 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5616 settings->op[stage].cop, settings->op[stage].carg0,
5617 settings->op[stage].carg1, settings->op[stage].carg2);
5619 else
5621 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5622 settings->op[stage].cop, settings->op[stage].carg0,
5623 settings->op[stage].carg1, settings->op[stage].carg2);
5624 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5625 settings->op[stage].aop, settings->op[stage].aarg0,
5626 settings->op[stage].aarg1, settings->op[stage].aarg2);
5630 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5632 if (settings->sRGB_write)
5633 shader_glsl_generate_srgb_write_correction(buffer);
5635 shader_glsl_generate_fog_code(buffer, settings->fog);
5637 shader_addline(buffer, "}\n");
5639 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5640 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5641 return shader_obj;
5644 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5645 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5647 struct glsl_ffp_vertex_shader *shader;
5648 const struct wine_rb_entry *entry;
5650 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5651 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5653 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5654 return NULL;
5656 shader->desc.settings = *settings;
5657 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5658 list_init(&shader->linked_programs);
5659 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5660 ERR("Failed to insert ffp vertex shader.\n");
5662 return shader;
5665 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5666 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5668 struct glsl_ffp_fragment_shader *glsl_desc;
5669 const struct ffp_frag_desc *desc;
5671 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5672 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5674 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5675 return NULL;
5677 glsl_desc->entry.settings = *args;
5678 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5679 list_init(&glsl_desc->linked_programs);
5680 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5682 return glsl_desc;
5686 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5687 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5689 unsigned int i;
5690 char name[32];
5692 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5693 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5694 for (i = 0; i < vs_c_count; ++i)
5696 snprintf(name, sizeof(name), "vs_c[%u]", i);
5697 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5699 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5700 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5702 for (i = 0; i < MAX_CONST_I; ++i)
5704 snprintf(name, sizeof(name), "vs_i[%u]", i);
5705 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5708 for (i = 0; i < MAX_CONST_B; ++i)
5710 snprintf(name, sizeof(name), "vs_b[%u]", i);
5711 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5714 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5717 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5718 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5720 unsigned int i;
5721 char name[32];
5723 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5724 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5725 for (i = 0; i < ps_c_count; ++i)
5727 snprintf(name, sizeof(name), "ps_c[%u]", i);
5728 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5730 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5731 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5733 for (i = 0; i < MAX_CONST_I; ++i)
5735 snprintf(name, sizeof(name), "ps_i[%u]", i);
5736 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5739 for (i = 0; i < MAX_CONST_B; ++i)
5741 snprintf(name, sizeof(name), "ps_b[%u]", i);
5742 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5745 for (i = 0; i < MAX_TEXTURES; ++i)
5747 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5748 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5749 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5750 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5751 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5752 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5753 snprintf(name, sizeof(name), "tss_const%u", i);
5754 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5757 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5758 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5759 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5760 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5763 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLhandleARB program_id,
5764 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5766 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5767 GLuint block_idx;
5768 unsigned int i;
5769 char name[16];
5771 for (i = 0; i < count; ++i)
5773 if (!reg_maps->cb_sizes[i])
5774 continue;
5776 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5777 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5778 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5780 checkGLcall("glUniformBlockBinding");
5783 /* Context activation is done by the caller. */
5784 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5785 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5787 const struct wined3d_gl_info *gl_info = context->gl_info;
5788 const struct ps_np2fixup_info *np2fixup_info = NULL;
5789 struct glsl_shader_prog_link *entry = NULL;
5790 struct wined3d_shader *vshader = NULL;
5791 struct wined3d_shader *gshader = NULL;
5792 struct wined3d_shader *pshader = NULL;
5793 GLhandleARB programId = 0;
5794 GLhandleARB reorder_shader_id = 0;
5795 unsigned int i;
5796 GLhandleARB vs_id = 0;
5797 GLhandleARB gs_id = 0;
5798 GLhandleARB ps_id = 0;
5799 struct list *ps_list, *vs_list;
5801 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5803 vs_id = ctx_data->glsl_program->vs.id;
5804 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5806 if (use_vs(state))
5808 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5809 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5811 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5812 && ctx_data->glsl_program->gs.id)
5813 gs_id = ctx_data->glsl_program->gs.id;
5814 else if (gshader)
5815 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5818 else if (use_vs(state))
5820 struct vs_compile_args vs_compile_args;
5821 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5823 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5824 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5825 vs_list = &vshader->linked_programs;
5827 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5828 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5830 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5832 struct glsl_ffp_vertex_shader *ffp_shader;
5833 struct wined3d_ffp_vs_settings settings;
5835 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5836 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5837 vs_id = ffp_shader->id;
5838 vs_list = &ffp_shader->linked_programs;
5841 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5843 ps_id = ctx_data->glsl_program->ps.id;
5844 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5846 if (use_ps(state))
5847 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5849 else if (use_ps(state))
5851 struct ps_compile_args ps_compile_args;
5852 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5853 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5854 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5855 pshader, &ps_compile_args, &np2fixup_info);
5856 ps_list = &pshader->linked_programs;
5858 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5860 struct glsl_ffp_fragment_shader *ffp_shader;
5861 struct ffp_frag_settings settings;
5863 gen_ffp_frag_op(context, state, &settings, FALSE);
5864 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5865 ps_id = ffp_shader->id;
5866 ps_list = &ffp_shader->linked_programs;
5869 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5871 ctx_data->glsl_program = entry;
5872 return;
5875 /* If we get to this point, then no matching program exists, so we create one */
5876 programId = GL_EXTCALL(glCreateProgramObjectARB());
5877 TRACE("Created new GLSL shader program %u\n", programId);
5879 /* Create the entry */
5880 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5881 entry->programId = programId;
5882 entry->vs.id = vs_id;
5883 entry->gs.id = gs_id;
5884 entry->ps.id = ps_id;
5885 entry->constant_version = 0;
5886 entry->ps.np2_fixup_info = np2fixup_info;
5887 /* Add the hash table entry */
5888 add_glsl_program_entry(priv, entry);
5890 /* Set the current program */
5891 ctx_data->glsl_program = entry;
5893 /* Attach GLSL vshader */
5894 if (vs_id)
5896 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5897 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5898 checkGLcall("glAttachObjectARB");
5900 list_add_head(vs_list, &entry->vs.shader_entry);
5903 if (vshader)
5905 WORD map = vshader->reg_maps.input_registers;
5906 char tmp_name[10];
5908 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5909 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5910 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5911 checkGLcall("glAttachObjectARB");
5912 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5913 * is destroyed
5915 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5917 /* Bind vertex attributes to a corresponding index number to match
5918 * the same index numbers as ARB_vertex_programs (makes loading
5919 * vertex attributes simpler). With this method, we can use the
5920 * exact same code to load the attributes later for both ARB and
5921 * GLSL shaders.
5923 * We have to do this here because we need to know the Program ID
5924 * in order to make the bindings work, and it has to be done prior
5925 * to linking the GLSL program. */
5926 for (i = 0; map; map >>= 1, ++i)
5928 if (!(map & 1)) continue;
5930 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5931 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5933 checkGLcall("glBindAttribLocationARB");
5936 if (gshader)
5938 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5939 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5940 checkGLcall("glAttachObjectARB");
5942 TRACE("input type %s, output type %s, vertices out %u.\n",
5943 debug_d3dprimitivetype(gshader->u.gs.input_type),
5944 debug_d3dprimitivetype(gshader->u.gs.output_type),
5945 gshader->u.gs.vertices_out);
5946 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5947 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5948 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5949 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5950 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5951 gshader->u.gs.vertices_out));
5952 checkGLcall("glProgramParameteriARB");
5954 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5957 /* Attach GLSL pshader */
5958 if (ps_id)
5960 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5961 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5962 checkGLcall("glAttachObjectARB");
5964 list_add_head(ps_list, &entry->ps.shader_entry);
5967 /* Link the program */
5968 TRACE("Linking GLSL shader program %u\n", programId);
5969 GL_EXTCALL(glLinkProgramARB(programId));
5970 shader_glsl_validate_link(gl_info, programId);
5972 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5973 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
5974 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5975 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
5976 checkGLcall("Find glsl program uniform locations");
5978 if (pshader && pshader->reg_maps.shader_version.major >= 3
5979 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5981 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5982 entry->vs.vertex_color_clamp = GL_FALSE;
5984 else
5986 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5989 /* Set the shader to allow uniform loading on it */
5990 GL_EXTCALL(glUseProgramObjectARB(programId));
5991 checkGLcall("glUseProgramObjectARB(programId)");
5993 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5994 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5995 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5996 * vertex shader with fixed function pixel processing is used we make sure that the card
5997 * supports enough samplers to allow the max number of vertex samplers with all possible
5998 * fixed function fragment processing setups. So once the program is linked these samplers
5999 * won't change.
6001 shader_glsl_load_vsamplers(gl_info, context->tex_unit_map, programId);
6002 shader_glsl_load_psamplers(gl_info, context->tex_unit_map, programId);
6004 entry->constant_update_mask = 0;
6005 if (vshader)
6007 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6008 if (vshader->reg_maps.integer_constants)
6009 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6010 if (vshader->reg_maps.boolean_constants)
6011 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6012 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6014 shader_glsl_init_uniform_block_bindings(gl_info, programId, &vshader->reg_maps,
6015 0, gl_info->limits.vertex_uniform_blocks);
6018 if (gshader)
6019 shader_glsl_init_uniform_block_bindings(gl_info, programId, &gshader->reg_maps,
6020 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6022 if (ps_id)
6024 if (pshader)
6026 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6027 if (pshader->reg_maps.integer_constants)
6028 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6029 if (pshader->reg_maps.boolean_constants)
6030 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6031 if (entry->ps.ycorrection_location != -1)
6032 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6034 shader_glsl_init_uniform_block_bindings(gl_info, programId, &pshader->reg_maps,
6035 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6036 gl_info->limits.fragment_uniform_blocks);
6038 else
6040 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6043 for (i = 0; i < MAX_TEXTURES; ++i)
6045 if (entry->ps.bumpenv_mat_location[i] != -1)
6047 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6048 break;
6052 if (entry->ps.np2_fixup_location != -1)
6053 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6057 /* Context activation is done by the caller. */
6058 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6060 GLhandleARB program_id;
6061 GLhandleARB vshader_id, pshader_id;
6062 const char *blt_pshader;
6064 static const char blt_vshader[] =
6065 "#version 120\n"
6066 "void main(void)\n"
6067 "{\n"
6068 " gl_Position = gl_Vertex;\n"
6069 " gl_FrontColor = vec4(1.0);\n"
6070 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6071 "}\n";
6073 static const char * const blt_pshaders_full[tex_type_count] =
6075 /* tex_1d */
6076 NULL,
6077 /* tex_2d */
6078 "#version 120\n"
6079 "uniform sampler2D sampler;\n"
6080 "void main(void)\n"
6081 "{\n"
6082 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6083 "}\n",
6084 /* tex_3d */
6085 NULL,
6086 /* tex_cube */
6087 "#version 120\n"
6088 "uniform samplerCube sampler;\n"
6089 "void main(void)\n"
6090 "{\n"
6091 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6092 "}\n",
6093 /* tex_rect */
6094 "#version 120\n"
6095 "#extension GL_ARB_texture_rectangle : enable\n"
6096 "uniform sampler2DRect sampler;\n"
6097 "void main(void)\n"
6098 "{\n"
6099 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6100 "}\n",
6103 static const char * const blt_pshaders_masked[tex_type_count] =
6105 /* tex_1d */
6106 NULL,
6107 /* tex_2d */
6108 "#version 120\n"
6109 "uniform sampler2D sampler;\n"
6110 "uniform vec4 mask;\n"
6111 "void main(void)\n"
6112 "{\n"
6113 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6114 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6115 "}\n",
6116 /* tex_3d */
6117 NULL,
6118 /* tex_cube */
6119 "#version 120\n"
6120 "uniform samplerCube sampler;\n"
6121 "uniform vec4 mask;\n"
6122 "void main(void)\n"
6123 "{\n"
6124 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6125 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6126 "}\n",
6127 /* tex_rect */
6128 "#version 120\n"
6129 "#extension GL_ARB_texture_rectangle : enable\n"
6130 "uniform sampler2DRect sampler;\n"
6131 "uniform vec4 mask;\n"
6132 "void main(void)\n"
6133 "{\n"
6134 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6135 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6136 "}\n",
6139 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6140 if (!blt_pshader)
6142 FIXME("tex_type %#x not supported\n", tex_type);
6143 return 0;
6146 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6147 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6149 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6150 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6152 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6153 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6154 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6155 GL_EXTCALL(glLinkProgramARB(program_id));
6157 shader_glsl_validate_link(gl_info, program_id);
6159 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6160 * is destroyed
6162 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6163 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6164 return program_id;
6167 /* Context activation is done by the caller. */
6168 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6169 const struct wined3d_state *state)
6171 struct glsl_context_data *ctx_data = context->shader_backend_data;
6172 const struct wined3d_gl_info *gl_info = context->gl_info;
6173 struct shader_glsl_priv *priv = shader_priv;
6174 GLhandleARB program_id = 0, prev_id = 0;
6175 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6177 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6178 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6180 if (ctx_data->glsl_program)
6182 prev_id = ctx_data->glsl_program->programId;
6183 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6185 else
6187 prev_id = 0;
6188 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6191 set_glsl_shader_program(context, state, priv, ctx_data);
6193 if (ctx_data->glsl_program)
6195 program_id = ctx_data->glsl_program->programId;
6196 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6198 else
6200 program_id = 0;
6201 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6204 if (old_vertex_color_clamp != current_vertex_color_clamp)
6206 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6208 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6209 checkGLcall("glClampColorARB");
6211 else
6213 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6217 TRACE("Using GLSL program %u.\n", program_id);
6219 if (prev_id != program_id)
6221 GL_EXTCALL(glUseProgramObjectARB(program_id));
6222 checkGLcall("glUseProgramObjectARB");
6224 if (program_id)
6225 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6229 /* "context" is not necessarily the currently active context. */
6230 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6232 struct glsl_context_data *ctx_data = context->shader_backend_data;
6234 ctx_data->glsl_program = NULL;
6235 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6236 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6237 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6240 /* Context activation is done by the caller. */
6241 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6243 const struct wined3d_gl_info *gl_info = context->gl_info;
6244 struct shader_glsl_priv *priv = shader_priv;
6246 shader_glsl_invalidate_current_program(context);
6247 GL_EXTCALL(glUseProgramObjectARB(0));
6248 checkGLcall("glUseProgramObjectARB");
6250 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6251 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6253 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6255 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6256 checkGLcall("glClampColorARB");
6260 /* Context activation is done by the caller. */
6261 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6262 enum tex_types tex_type, const SIZE *ds_mask_size)
6264 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6265 struct shader_glsl_priv *priv = shader_priv;
6266 GLhandleARB *blt_program;
6267 GLint loc;
6269 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6270 if (!*blt_program)
6272 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6273 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6274 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6275 GL_EXTCALL(glUniform1iARB(loc, 0));
6277 else
6279 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6282 if (masked)
6284 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6285 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6289 /* Context activation is done by the caller. */
6290 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6292 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6293 GLhandleARB program_id;
6295 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6296 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6298 GL_EXTCALL(glUseProgramObjectARB(program_id));
6299 checkGLcall("glUseProgramObjectARB");
6302 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6303 const struct glsl_shader_prog_link *program)
6305 const struct glsl_context_data *ctx_data;
6306 struct wined3d_context *context;
6307 unsigned int i;
6309 for (i = 0; i < device->context_count; ++i)
6311 context = device->contexts[i];
6312 ctx_data = context->shader_backend_data;
6314 if (ctx_data->glsl_program == program)
6315 shader_glsl_invalidate_current_program(context);
6319 static void shader_glsl_destroy(struct wined3d_shader *shader)
6321 struct glsl_shader_private *shader_data = shader->backend_data;
6322 struct wined3d_device *device = shader->device;
6323 struct shader_glsl_priv *priv = device->shader_priv;
6324 const struct wined3d_gl_info *gl_info;
6325 const struct list *linked_programs;
6326 struct wined3d_context *context;
6328 if (!shader_data || !shader_data->num_gl_shaders)
6330 HeapFree(GetProcessHeap(), 0, shader_data);
6331 shader->backend_data = NULL;
6332 return;
6335 context = context_acquire(device, NULL);
6336 gl_info = context->gl_info;
6338 TRACE("Deleting linked programs.\n");
6339 linked_programs = &shader->linked_programs;
6340 if (linked_programs->next)
6342 struct glsl_shader_prog_link *entry, *entry2;
6343 UINT i;
6345 switch (shader->reg_maps.shader_version.type)
6347 case WINED3D_SHADER_TYPE_PIXEL:
6349 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6351 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6353 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6354 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6355 checkGLcall("glDeleteObjectARB");
6357 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6359 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6360 struct glsl_shader_prog_link, ps.shader_entry)
6362 shader_glsl_invalidate_contexts_program(device, entry);
6363 delete_glsl_program_entry(priv, gl_info, entry);
6366 break;
6369 case WINED3D_SHADER_TYPE_VERTEX:
6371 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6373 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6375 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6376 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6377 checkGLcall("glDeleteObjectARB");
6379 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6381 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6382 struct glsl_shader_prog_link, vs.shader_entry)
6384 shader_glsl_invalidate_contexts_program(device, entry);
6385 delete_glsl_program_entry(priv, gl_info, entry);
6388 break;
6391 case WINED3D_SHADER_TYPE_GEOMETRY:
6393 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6395 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6397 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6398 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6399 checkGLcall("glDeleteObjectARB");
6401 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6403 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6404 struct glsl_shader_prog_link, gs.shader_entry)
6406 shader_glsl_invalidate_contexts_program(device, entry);
6407 delete_glsl_program_entry(priv, gl_info, entry);
6410 break;
6413 default:
6414 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6415 break;
6419 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6420 shader->backend_data = NULL;
6422 context_release(context);
6425 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6427 const struct glsl_program_key *k = key;
6428 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6429 const struct glsl_shader_prog_link, program_lookup_entry);
6431 if (k->vs_id > prog->vs.id) return 1;
6432 else if (k->vs_id < prog->vs.id) return -1;
6434 if (k->gs_id > prog->gs.id) return 1;
6435 else if (k->gs_id < prog->gs.id) return -1;
6437 if (k->ps_id > prog->ps.id) return 1;
6438 else if (k->ps_id < prog->ps.id) return -1;
6440 return 0;
6443 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6445 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6446 + constant_count * sizeof(*heap->contained)
6447 + constant_count * sizeof(*heap->positions);
6448 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6450 if (!mem)
6452 ERR("Failed to allocate memory\n");
6453 return FALSE;
6456 heap->entries = mem;
6457 heap->entries[1].version = 0;
6458 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6459 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6460 heap->positions = (unsigned int *)(heap->contained + constant_count);
6461 heap->size = 1;
6463 return TRUE;
6466 static void constant_heap_free(struct constant_heap *heap)
6468 HeapFree(GetProcessHeap(), 0, heap->entries);
6471 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6473 wined3d_rb_alloc,
6474 wined3d_rb_realloc,
6475 wined3d_rb_free,
6476 glsl_program_key_compare,
6479 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6480 const struct fragment_pipeline *fragment_pipe)
6482 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6483 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6484 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6485 gl_info->limits.glsl_ps_float_constants)) + 1;
6486 struct fragment_caps fragment_caps;
6487 void *vertex_priv, *fragment_priv;
6489 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6491 ERR("Failed to initialize vertex pipe.\n");
6492 HeapFree(GetProcessHeap(), 0, priv);
6493 return E_FAIL;
6496 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6498 ERR("Failed to initialize fragment pipe.\n");
6499 vertex_pipe->vp_free(device);
6500 HeapFree(GetProcessHeap(), 0, priv);
6501 return E_FAIL;
6504 if (!shader_buffer_init(&priv->shader_buffer))
6506 ERR("Failed to initialize shader buffer.\n");
6507 goto fail;
6510 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6511 if (!priv->stack)
6513 ERR("Failed to allocate memory.\n");
6514 goto fail;
6517 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6519 ERR("Failed to initialize vertex shader constant heap\n");
6520 goto fail;
6523 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6525 ERR("Failed to initialize pixel shader constant heap\n");
6526 goto fail;
6529 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6531 ERR("Failed to initialize rbtree.\n");
6532 goto fail;
6535 priv->next_constant_version = 1;
6536 priv->vertex_pipe = vertex_pipe;
6537 priv->fragment_pipe = fragment_pipe;
6538 fragment_pipe->get_caps(gl_info, &fragment_caps);
6539 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6541 device->vertex_priv = vertex_priv;
6542 device->fragment_priv = fragment_priv;
6543 device->shader_priv = priv;
6545 return WINED3D_OK;
6547 fail:
6548 constant_heap_free(&priv->pconst_heap);
6549 constant_heap_free(&priv->vconst_heap);
6550 HeapFree(GetProcessHeap(), 0, priv->stack);
6551 shader_buffer_free(&priv->shader_buffer);
6552 fragment_pipe->free_private(device);
6553 vertex_pipe->vp_free(device);
6554 HeapFree(GetProcessHeap(), 0, priv);
6555 return E_OUTOFMEMORY;
6558 /* Context activation is done by the caller. */
6559 static void shader_glsl_free(struct wined3d_device *device)
6561 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6562 struct shader_glsl_priv *priv = device->shader_priv;
6563 int i;
6565 for (i = 0; i < tex_type_count; ++i)
6567 if (priv->depth_blt_program_full[i])
6569 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6571 if (priv->depth_blt_program_masked[i])
6573 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6577 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6578 constant_heap_free(&priv->pconst_heap);
6579 constant_heap_free(&priv->vconst_heap);
6580 HeapFree(GetProcessHeap(), 0, priv->stack);
6581 shader_buffer_free(&priv->shader_buffer);
6582 priv->fragment_pipe->free_private(device);
6583 priv->vertex_pipe->vp_free(device);
6585 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6586 device->shader_priv = NULL;
6589 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6591 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6592 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6595 static void shader_glsl_free_context_data(struct wined3d_context *context)
6597 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6600 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6602 UINT shader_model;
6604 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6605 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6606 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6607 && gl_info->supported[ARB_TEXTURE_RG])
6608 shader_model = 4;
6609 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6610 * texldd and texldl instructions. */
6611 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6612 shader_model = 3;
6613 else
6614 shader_model = 2;
6615 TRACE("Shader model %u.\n", shader_model);
6617 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6618 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6619 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6621 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6622 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6624 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6625 * Direct3D minimum requirement.
6627 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6628 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6630 * The problem is that the refrast clamps temporary results in the shader to
6631 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6632 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6633 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6634 * offer a way to query this.
6636 if (shader_model >= 4)
6637 caps->ps_1x_max_value = FLT_MAX;
6638 else
6639 caps->ps_1x_max_value = 1024.0f;
6641 /* Ideally we'd only set caps like sRGB writes here if supported by both
6642 * the shader backend and the fragment pipe, but we can get called before
6643 * shader_glsl_alloc(). */
6644 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6645 | WINED3D_SHADER_CAP_SRGB_WRITE;
6648 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6650 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6652 TRACE("Checking support for fixup:\n");
6653 dump_color_fixup_desc(fixup);
6656 /* We support everything except YUV conversions. */
6657 if (!is_complex_fixup(fixup))
6659 TRACE("[OK]\n");
6660 return TRUE;
6663 TRACE("[FAILED]\n");
6664 return FALSE;
6667 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6669 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6670 /* WINED3DSIH_ADD */ shader_glsl_binop,
6671 /* WINED3DSIH_AND */ shader_glsl_binop,
6672 /* WINED3DSIH_BEM */ shader_glsl_bem,
6673 /* WINED3DSIH_BREAK */ shader_glsl_break,
6674 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6675 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6676 /* WINED3DSIH_CALL */ shader_glsl_call,
6677 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6678 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6679 /* WINED3DSIH_CND */ shader_glsl_cnd,
6680 /* WINED3DSIH_CRS */ shader_glsl_cross,
6681 /* WINED3DSIH_CUT */ shader_glsl_cut,
6682 /* WINED3DSIH_DCL */ shader_glsl_nop,
6683 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6684 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6685 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6686 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6687 /* WINED3DSIH_DEF */ shader_glsl_nop,
6688 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6689 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6690 /* WINED3DSIH_DIV */ shader_glsl_binop,
6691 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6692 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6693 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6694 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6695 /* WINED3DSIH_DST */ shader_glsl_dst,
6696 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6697 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6698 /* WINED3DSIH_ELSE */ shader_glsl_else,
6699 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6700 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6701 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6702 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6703 /* WINED3DSIH_EQ */ shader_glsl_relop,
6704 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6705 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6706 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6707 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6708 /* WINED3DSIH_GE */ shader_glsl_relop,
6709 /* WINED3DSIH_IADD */ shader_glsl_binop,
6710 /* WINED3DSIH_IEQ */ NULL,
6711 /* WINED3DSIH_IF */ shader_glsl_if,
6712 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6713 /* WINED3DSIH_IGE */ shader_glsl_relop,
6714 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6715 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6716 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6717 /* WINED3DSIH_LABEL */ shader_glsl_label,
6718 /* WINED3DSIH_LD */ NULL,
6719 /* WINED3DSIH_LIT */ shader_glsl_lit,
6720 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6721 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6722 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6723 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6724 /* WINED3DSIH_LT */ shader_glsl_relop,
6725 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6726 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6727 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6728 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6729 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6730 /* WINED3DSIH_MAD */ shader_glsl_mad,
6731 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6732 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6733 /* WINED3DSIH_MOV */ shader_glsl_mov,
6734 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6735 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6736 /* WINED3DSIH_MUL */ shader_glsl_binop,
6737 /* WINED3DSIH_NOP */ shader_glsl_nop,
6738 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6739 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6740 /* WINED3DSIH_POW */ shader_glsl_pow,
6741 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6742 /* WINED3DSIH_REP */ shader_glsl_rep,
6743 /* WINED3DSIH_RET */ shader_glsl_ret,
6744 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6745 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6746 /* WINED3DSIH_SAMPLE */ NULL,
6747 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6748 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6749 /* WINED3DSIH_SETP */ NULL,
6750 /* WINED3DSIH_SGE */ shader_glsl_compare,
6751 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6752 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6753 /* WINED3DSIH_SLT */ shader_glsl_compare,
6754 /* WINED3DSIH_SQRT */ NULL,
6755 /* WINED3DSIH_SUB */ shader_glsl_binop,
6756 /* WINED3DSIH_TEX */ shader_glsl_tex,
6757 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6758 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6759 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6760 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6761 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6762 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6763 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6764 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6765 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6766 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6767 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6768 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6769 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6770 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6771 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6772 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6773 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6774 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6775 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6776 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6777 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6778 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6779 /* WINED3DSIH_UGE */ shader_glsl_relop,
6780 /* WINED3DSIH_USHR */ shader_glsl_binop,
6781 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6782 /* WINED3DSIH_XOR */ shader_glsl_binop,
6785 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6786 SHADER_HANDLER hw_fct;
6788 /* Select handler */
6789 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6791 /* Unhandled opcode */
6792 if (!hw_fct)
6794 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6795 return;
6797 hw_fct(ins);
6799 shader_glsl_add_instruction_modifiers(ins);
6802 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6804 struct shader_glsl_priv *priv = shader_priv;
6806 return priv->ffp_proj_control;
6809 const struct wined3d_shader_backend_ops glsl_shader_backend =
6811 shader_glsl_handle_instruction,
6812 shader_glsl_select,
6813 shader_glsl_disable,
6814 shader_glsl_select_depth_blt,
6815 shader_glsl_deselect_depth_blt,
6816 shader_glsl_update_float_vertex_constants,
6817 shader_glsl_update_float_pixel_constants,
6818 shader_glsl_load_constants,
6819 shader_glsl_destroy,
6820 shader_glsl_alloc,
6821 shader_glsl_free,
6822 shader_glsl_allocate_context_data,
6823 shader_glsl_free_context_data,
6824 shader_glsl_get_caps,
6825 shader_glsl_color_fixup_supported,
6826 shader_glsl_has_ffp_proj_control,
6829 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6831 if (enable)
6832 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6833 else
6834 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6835 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6838 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6840 caps->xyzrhw = TRUE;
6841 caps->max_active_lights = gl_info->limits.lights;
6842 caps->max_vertex_blend_matrices = 1;
6843 caps->max_vertex_blend_matrix_index = 0;
6844 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6845 | WINED3DVTXPCAPS_MATERIALSOURCE7
6846 | WINED3DVTXPCAPS_VERTEXFOG
6847 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6848 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6849 | WINED3DVTXPCAPS_LOCALVIEWER
6850 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6851 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6852 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6853 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6856 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6858 struct shader_glsl_priv *priv;
6860 if (shader_backend == &glsl_shader_backend)
6862 priv = shader_priv;
6864 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6866 ERR("Failed to initialize rbtree.\n");
6867 return NULL;
6870 return priv;
6873 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6875 return NULL;
6878 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6880 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6881 struct glsl_ffp_vertex_shader, desc.entry);
6882 struct glsl_shader_prog_link *program, *program2;
6883 struct glsl_ffp_destroy_ctx *ctx = context;
6885 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6886 struct glsl_shader_prog_link, vs.shader_entry)
6888 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6890 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6891 HeapFree(GetProcessHeap(), 0, shader);
6894 /* Context activation is done by the caller. */
6895 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6897 struct shader_glsl_priv *priv = device->vertex_priv;
6898 struct glsl_ffp_destroy_ctx ctx;
6900 ctx.priv = priv;
6901 ctx.gl_info = &device->adapter->gl_info;
6902 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6905 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6906 const struct wined3d_state *state, DWORD state_id)
6908 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6911 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6912 const struct wined3d_state *state, DWORD state_id)
6914 /* Table fog behavior depends on the projection matrix. */
6915 if (state->render_states[WINED3D_RS_FOGENABLE]
6916 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6917 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6918 transform_projection(context, state, state_id);
6921 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6923 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6924 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6925 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6926 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6927 /* Clip planes */
6928 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6929 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6930 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6931 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6932 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6933 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6934 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6935 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6936 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6937 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6938 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6939 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6940 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6941 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6942 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6943 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6944 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6945 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6946 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6947 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6948 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6949 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6950 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6951 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6952 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6953 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6954 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6955 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6956 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6957 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6958 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6959 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6960 /* Lights */
6961 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6962 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6963 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6964 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6965 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6966 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6967 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6968 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6969 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6970 /* Viewport */
6971 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6972 /* Transform states */
6973 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6974 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6975 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6976 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6977 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6978 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6979 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6980 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6981 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6982 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6983 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6984 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6985 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6986 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6987 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6988 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6989 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6990 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6991 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6992 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6993 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6994 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6995 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6996 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6997 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6998 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6999 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7000 /* Fog */
7001 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7002 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7003 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7004 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7005 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7006 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7007 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7008 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
7009 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7010 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7011 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7012 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7013 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7014 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7015 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7016 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7017 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7018 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7019 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7020 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7021 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7022 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7023 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7024 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7025 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7026 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7027 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7028 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7029 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7030 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7031 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7032 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7033 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7034 * handler in that case to get the vertex part of sampler() skipped (VTF
7035 * is handled in the misc states). Otherwise, register
7036 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7037 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7038 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7039 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7040 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7041 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7042 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7043 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7044 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7045 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7046 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7047 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7048 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7049 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7050 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7051 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7052 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7053 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7054 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7055 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7056 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7057 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7058 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7059 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7060 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7061 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7062 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7065 /* TODO:
7066 * - This currently depends on GL fixed function functions to set things
7067 * like light parameters. Ideally we'd use regular uniforms for that.
7068 * - In part because of the previous point, much of this is modelled after
7069 * GL fixed function, and has much of the same limitations. For example,
7070 * D3D spot lights are slightly different from GL spot lights.
7071 * - We can now implement drawing transformed vertices using the GLSL pipe,
7072 * instead of using the immediate mode fallback.
7073 * - Similarly, we don't need the fallback for certain combinations of
7074 * material sources anymore.
7075 * - Implement vertex blending and vertex tweening.
7076 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7077 * attribute arrays in load_tex_coords().
7078 * - Per-vertex point sizes. */
7079 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7081 glsl_vertex_pipe_vp_enable,
7082 glsl_vertex_pipe_vp_get_caps,
7083 glsl_vertex_pipe_vp_alloc,
7084 glsl_vertex_pipe_vp_free,
7085 glsl_vertex_pipe_vp_states,
7088 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7090 /* Nothing to do. */
7093 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7095 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7096 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7097 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7098 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7099 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7100 | WINED3DTEXOPCAPS_SELECTARG1
7101 | WINED3DTEXOPCAPS_SELECTARG2
7102 | WINED3DTEXOPCAPS_MODULATE4X
7103 | WINED3DTEXOPCAPS_MODULATE2X
7104 | WINED3DTEXOPCAPS_MODULATE
7105 | WINED3DTEXOPCAPS_ADDSIGNED2X
7106 | WINED3DTEXOPCAPS_ADDSIGNED
7107 | WINED3DTEXOPCAPS_ADD
7108 | WINED3DTEXOPCAPS_SUBTRACT
7109 | WINED3DTEXOPCAPS_ADDSMOOTH
7110 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7111 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7112 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7113 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7114 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7115 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7116 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7117 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7118 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7119 | WINED3DTEXOPCAPS_DOTPRODUCT3
7120 | WINED3DTEXOPCAPS_MULTIPLYADD
7121 | WINED3DTEXOPCAPS_LERP
7122 | WINED3DTEXOPCAPS_BUMPENVMAP
7123 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7124 caps->MaxTextureBlendStages = 8;
7125 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7128 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7130 struct shader_glsl_priv *priv;
7132 if (shader_backend == &glsl_shader_backend)
7134 priv = shader_priv;
7136 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7138 ERR("Failed to initialize rbtree.\n");
7139 return NULL;
7142 return priv;
7145 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7147 return NULL;
7150 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7152 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7153 struct glsl_ffp_fragment_shader, entry.entry);
7154 struct glsl_shader_prog_link *program, *program2;
7155 struct glsl_ffp_destroy_ctx *ctx = context;
7157 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7158 struct glsl_shader_prog_link, ps.shader_entry)
7160 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7162 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7163 HeapFree(GetProcessHeap(), 0, shader);
7166 /* Context activation is done by the caller. */
7167 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7169 struct shader_glsl_priv *priv = device->fragment_priv;
7170 struct glsl_ffp_destroy_ctx ctx;
7172 ctx.priv = priv;
7173 ctx.gl_info = &device->adapter->gl_info;
7174 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7177 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7178 const struct wined3d_state *state, DWORD state_id)
7180 context->last_was_pshader = use_ps(state);
7182 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7185 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7186 const struct wined3d_state *state, DWORD state_id)
7188 BOOL use_vshader = use_vs(state);
7189 enum fogsource new_source;
7190 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7191 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7193 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7195 if (!state->render_states[WINED3D_RS_FOGENABLE])
7196 return;
7198 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7200 if (use_vshader)
7201 new_source = FOGSOURCE_VS;
7202 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7203 new_source = FOGSOURCE_COORD;
7204 else
7205 new_source = FOGSOURCE_FFP;
7207 else
7209 new_source = FOGSOURCE_FFP;
7212 if (new_source != context->fog_source || fogstart == fogend)
7214 context->fog_source = new_source;
7215 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7219 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7220 const struct wined3d_state *state, DWORD state_id)
7222 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7225 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7226 const struct wined3d_state *state, DWORD state_id)
7228 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7231 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7233 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7234 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7250 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7252 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7253 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7254 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7255 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7256 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7257 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7259 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7260 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7261 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7263 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7264 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7265 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7266 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7267 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7268 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7269 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7270 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7271 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7276 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7294 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7295 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7296 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7297 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7298 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7300 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7302 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7303 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7304 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7305 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7306 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7307 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7308 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7309 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7310 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7311 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7312 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7313 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7314 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7315 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7316 {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 },
7317 {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 },
7318 {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 },
7319 {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 },
7320 {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 },
7321 {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 },
7322 {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 },
7323 {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 },
7324 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7325 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7326 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7327 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7328 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7329 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7330 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7331 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7332 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7333 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7336 const struct fragment_pipeline glsl_fragment_pipe =
7338 glsl_fragment_pipe_enable,
7339 glsl_fragment_pipe_get_caps,
7340 glsl_fragment_pipe_alloc,
7341 glsl_fragment_pipe_free,
7342 shader_glsl_color_fixup_supported,
7343 glsl_fragment_pipe_state_template,