wininet: Properly clean up in InternetGetCookieExW if no cookies were found.
[wine.git] / dlls / wined3d / glsl_shader.c
blob34cc5677ba102d28fbf1a720e0726856804ceff4
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;
70 enum heap_node_op
72 HEAP_NODE_TRAVERSE_LEFT,
73 HEAP_NODE_TRAVERSE_RIGHT,
74 HEAP_NODE_POP,
77 struct constant_entry
79 unsigned int idx;
80 unsigned int version;
83 struct constant_heap
85 struct constant_entry *entries;
86 BOOL *contained;
87 unsigned int *positions;
88 unsigned int size;
91 /* GLSL shader private data */
92 struct shader_glsl_priv {
93 struct wined3d_shader_buffer shader_buffer;
94 struct wine_rb_tree program_lookup;
95 struct constant_heap vconst_heap;
96 struct constant_heap pconst_heap;
97 unsigned char *stack;
98 GLhandleARB depth_blt_program_full[tex_type_count];
99 GLhandleARB depth_blt_program_masked[tex_type_count];
100 UINT next_constant_version;
102 const struct wined3d_vertex_pipe_ops *vertex_pipe;
103 const struct fragment_pipeline *fragment_pipe;
104 struct wine_rb_tree ffp_vertex_shaders;
105 struct wine_rb_tree ffp_fragment_shaders;
106 BOOL ffp_proj_control;
109 struct glsl_vs_program
111 struct list shader_entry;
112 GLhandleARB id;
113 GLenum vertex_color_clamp;
114 GLint *uniform_f_locations;
115 GLint uniform_i_locations[MAX_CONST_I];
116 GLint uniform_b_locations[MAX_CONST_B];
117 GLint pos_fixup_location;
120 struct glsl_gs_program
122 struct list shader_entry;
123 GLhandleARB id;
126 struct glsl_ps_program
128 struct list shader_entry;
129 GLhandleARB id;
130 GLint *uniform_f_locations;
131 GLint uniform_i_locations[MAX_CONST_I];
132 GLint uniform_b_locations[MAX_CONST_B];
133 GLint bumpenv_mat_location[MAX_TEXTURES];
134 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
135 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
136 GLint tss_constant_location[MAX_TEXTURES];
137 GLint tex_factor_location;
138 GLint specular_enable_location;
139 GLint ycorrection_location;
140 GLint np2_fixup_location;
141 const struct ps_np2fixup_info *np2_fixup_info;
144 /* Struct to maintain data about a linked GLSL program */
145 struct glsl_shader_prog_link
147 struct wine_rb_entry program_lookup_entry;
148 struct glsl_vs_program vs;
149 struct glsl_gs_program gs;
150 struct glsl_ps_program ps;
151 GLhandleARB programId;
152 DWORD constant_update_mask;
153 UINT constant_version;
156 struct glsl_program_key
158 GLhandleARB vs_id;
159 GLhandleARB gs_id;
160 GLhandleARB ps_id;
163 struct shader_glsl_ctx_priv {
164 const struct vs_compile_args *cur_vs_args;
165 const struct ps_compile_args *cur_ps_args;
166 struct ps_np2fixup_info *cur_np2fixup_info;
169 struct glsl_context_data
171 struct glsl_shader_prog_link *glsl_program;
174 struct glsl_ps_compiled_shader
176 struct ps_compile_args args;
177 struct ps_np2fixup_info np2fixup;
178 GLhandleARB prgId;
181 struct glsl_vs_compiled_shader
183 struct vs_compile_args args;
184 GLhandleARB prgId;
187 struct glsl_gs_compiled_shader
189 GLhandleARB id;
192 struct glsl_shader_private
194 union
196 struct glsl_vs_compiled_shader *vs;
197 struct glsl_gs_compiled_shader *gs;
198 struct glsl_ps_compiled_shader *ps;
199 } gl_shaders;
200 UINT num_gl_shaders, shader_array_size;
203 struct glsl_ffp_vertex_shader
205 struct wined3d_ffp_vs_desc desc;
206 GLhandleARB id;
207 struct list linked_programs;
210 struct glsl_ffp_fragment_shader
212 struct ffp_frag_desc entry;
213 GLhandleARB id;
214 struct list linked_programs;
217 struct glsl_ffp_destroy_ctx
219 struct shader_glsl_priv *priv;
220 const struct wined3d_gl_info *gl_info;
223 static const char *debug_gl_shader_type(GLenum type)
225 switch (type)
227 #define WINED3D_TO_STR(u) case u: return #u
228 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
229 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
230 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
231 #undef WINED3D_TO_STR
232 default:
233 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
237 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
239 switch (type)
241 case WINED3D_SHADER_TYPE_VERTEX:
242 return "vs";
244 case WINED3D_SHADER_TYPE_GEOMETRY:
245 return "gs";
247 case WINED3D_SHADER_TYPE_PIXEL:
248 return "ps";
250 default:
251 FIXME("Unhandled shader type %#x.\n", type);
252 return "unknown";
256 static void shader_glsl_append_imm_vec4(struct wined3d_shader_buffer *buffer, const float *values)
258 char str[4][17];
260 wined3d_ftoa(values[0], str[0]);
261 wined3d_ftoa(values[1], str[1]);
262 wined3d_ftoa(values[2], str[2]);
263 wined3d_ftoa(values[3], str[3]);
264 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
267 /* Extract a line from the info log.
268 * Note that this modifies the source string. */
269 static char *get_info_log_line(char **ptr)
271 char *p, *q;
273 p = *ptr;
274 if (!(q = strstr(p, "\n")))
276 if (!*p) return NULL;
277 *ptr += strlen(p);
278 return p;
280 *q = '\0';
281 *ptr = q + 1;
283 return p;
286 /* Context activation is done by the caller. */
287 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
289 int infologLength = 0;
290 char *infoLog;
292 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
293 return;
295 GL_EXTCALL(glGetObjectParameterivARB(obj,
296 GL_OBJECT_INFO_LOG_LENGTH_ARB,
297 &infologLength));
299 /* A size of 1 is just a null-terminated string, so the log should be bigger than
300 * that if there are errors. */
301 if (infologLength > 1)
303 char *ptr, *line;
305 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
306 /* The info log is supposed to be zero-terminated, but at least some
307 * versions of fglrx don't terminate the string properly. The reported
308 * length does include the terminator, so explicitly set it to zero
309 * here. */
310 infoLog[infologLength - 1] = 0;
311 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
313 ptr = infoLog;
314 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
316 WARN("Info log received from GLSL shader #%u:\n", obj);
317 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
319 else
321 FIXME("Info log received from GLSL shader #%u:\n", obj);
322 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
324 HeapFree(GetProcessHeap(), 0, infoLog);
328 /* Context activation is done by the caller. */
329 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
331 TRACE("Compiling shader object %u.\n", shader);
332 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
333 checkGLcall("glShaderSourceARB");
334 GL_EXTCALL(glCompileShaderARB(shader));
335 checkGLcall("glCompileShaderARB");
336 print_glsl_info_log(gl_info, shader);
339 /* Context activation is done by the caller. */
340 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
342 GLint i, object_count, source_size = -1;
343 GLhandleARB *objects;
344 char *source = NULL;
346 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
347 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
348 if (!objects)
350 ERR("Failed to allocate object array memory.\n");
351 return;
354 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
355 for (i = 0; i < object_count; ++i)
357 char *ptr, *line;
358 GLint tmp;
360 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
362 if (source_size < tmp)
364 HeapFree(GetProcessHeap(), 0, source);
366 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
367 if (!source)
369 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
370 HeapFree(GetProcessHeap(), 0, objects);
371 return;
373 source_size = tmp;
376 FIXME("Object %u:\n", objects[i]);
377 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
378 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
379 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
380 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
381 FIXME("\n");
383 ptr = source;
384 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
385 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
386 FIXME("\n");
389 HeapFree(GetProcessHeap(), 0, source);
390 HeapFree(GetProcessHeap(), 0, objects);
393 /* Context activation is done by the caller. */
394 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
396 GLint tmp;
398 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
400 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
401 if (tmp == GL_PROGRAM_OBJECT_ARB)
403 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
404 if (!tmp)
406 FIXME("Program %u link status invalid.\n", program);
407 shader_glsl_dump_program_source(gl_info, program);
411 print_glsl_info_log(gl_info, program);
414 /* Context activation is done by the caller. */
415 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
416 const DWORD *tex_unit_map, GLhandleARB programId)
418 GLint name_loc;
419 char sampler_name[20];
420 unsigned int i;
422 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
424 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
425 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
426 if (name_loc != -1) {
427 DWORD mapped_unit = tex_unit_map[i];
428 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
430 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
431 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
432 checkGLcall("glUniform1iARB");
433 } else {
434 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
440 /* Context activation is done by the caller. */
441 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
442 const DWORD *tex_unit_map, GLhandleARB programId)
444 GLint name_loc;
445 char sampler_name[20];
446 unsigned int i;
448 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
450 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
451 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
452 if (name_loc != -1) {
453 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
454 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
456 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
457 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
458 checkGLcall("glUniform1iARB");
459 } else {
460 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
466 /* Context activation is done by the caller. */
467 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
468 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
470 unsigned int start = ~0U, end = 0;
471 int stack_idx = 0;
472 unsigned int heap_idx = 1;
473 unsigned int idx;
475 if (heap->entries[heap_idx].version <= version) return;
477 idx = heap->entries[heap_idx].idx;
478 if (constant_locations[idx] != -1)
479 start = end = idx;
480 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
482 while (stack_idx >= 0)
484 /* Note that we fall through to the next case statement. */
485 switch(stack[stack_idx])
487 case HEAP_NODE_TRAVERSE_LEFT:
489 unsigned int left_idx = heap_idx << 1;
490 if (left_idx < heap->size && heap->entries[left_idx].version > version)
492 heap_idx = left_idx;
493 idx = heap->entries[heap_idx].idx;
494 if (constant_locations[idx] != -1)
496 if (start > idx)
497 start = idx;
498 if (end < idx)
499 end = idx;
502 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
503 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
504 break;
508 case HEAP_NODE_TRAVERSE_RIGHT:
510 unsigned int right_idx = (heap_idx << 1) + 1;
511 if (right_idx < heap->size && heap->entries[right_idx].version > version)
513 heap_idx = right_idx;
514 idx = heap->entries[heap_idx].idx;
515 if (constant_locations[idx] != -1)
517 if (start > idx)
518 start = idx;
519 if (end < idx)
520 end = idx;
523 stack[stack_idx++] = HEAP_NODE_POP;
524 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
525 break;
529 case HEAP_NODE_POP:
530 heap_idx >>= 1;
531 --stack_idx;
532 break;
535 if (start <= end)
536 GL_EXTCALL(glUniform4fvARB(constant_locations[start], end - start + 1, &constants[start * 4]));
537 checkGLcall("walk_constant_heap()");
540 /* Context activation is done by the caller. */
541 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
543 GLfloat clamped_constant[4];
545 if (location == -1) return;
547 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
548 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
549 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
550 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
552 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
555 /* Context activation is done by the caller. */
556 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
557 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
559 int stack_idx = 0;
560 unsigned int heap_idx = 1;
561 unsigned int idx;
563 if (heap->entries[heap_idx].version <= version) return;
565 idx = heap->entries[heap_idx].idx;
566 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
567 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
569 while (stack_idx >= 0)
571 /* Note that we fall through to the next case statement. */
572 switch(stack[stack_idx])
574 case HEAP_NODE_TRAVERSE_LEFT:
576 unsigned int left_idx = heap_idx << 1;
577 if (left_idx < heap->size && heap->entries[left_idx].version > version)
579 heap_idx = left_idx;
580 idx = heap->entries[heap_idx].idx;
581 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
583 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
584 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
585 break;
589 case HEAP_NODE_TRAVERSE_RIGHT:
591 unsigned int right_idx = (heap_idx << 1) + 1;
592 if (right_idx < heap->size && heap->entries[right_idx].version > version)
594 heap_idx = right_idx;
595 idx = heap->entries[heap_idx].idx;
596 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
598 stack[stack_idx++] = HEAP_NODE_POP;
599 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
600 break;
604 case HEAP_NODE_POP:
605 heap_idx >>= 1;
606 --stack_idx;
607 break;
610 checkGLcall("walk_constant_heap_clamped()");
613 /* Context activation is done by the caller. */
614 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
615 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
616 unsigned char *stack, UINT version)
618 const struct wined3d_shader_lconst *lconst;
620 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
621 if (shader->reg_maps.shader_version.major == 1
622 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
623 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
624 else
625 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
627 if (!shader->load_local_constsF)
629 TRACE("No need to load local float constants for this shader\n");
630 return;
633 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
634 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
636 GL_EXTCALL(glUniform4fvARB(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
638 checkGLcall("glUniform4fvARB()");
641 /* Context activation is done by the caller. */
642 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
643 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
645 unsigned int i;
646 struct list* ptr;
648 for (i = 0; constants_set; constants_set >>= 1, ++i)
650 if (!(constants_set & 1)) continue;
652 /* We found this uniform name in the program - go ahead and send the data */
653 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
656 /* Load immediate constants */
657 ptr = list_head(&shader->constantsI);
658 while (ptr)
660 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
661 unsigned int idx = lconst->idx;
662 const GLint *values = (const GLint *)lconst->value;
664 /* We found this uniform name in the program - go ahead and send the data */
665 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
666 ptr = list_next(&shader->constantsI, ptr);
668 checkGLcall("glUniform4ivARB()");
671 /* Context activation is done by the caller. */
672 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
673 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
675 unsigned int i;
676 struct list* ptr;
678 for (i = 0; constants_set; constants_set >>= 1, ++i)
680 if (!(constants_set & 1)) continue;
682 GL_EXTCALL(glUniform1ivARB(locations[i], 1, &constants[i]));
685 /* Load immediate constants */
686 ptr = list_head(&shader->constantsB);
687 while (ptr)
689 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
690 unsigned int idx = lconst->idx;
691 const GLint *values = (const GLint *)lconst->value;
693 GL_EXTCALL(glUniform1ivARB(locations[idx], 1, values));
694 ptr = list_next(&shader->constantsB, ptr);
696 checkGLcall("glUniform1ivARB()");
699 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
701 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
704 /* Context activation is done by the caller (state handler). */
705 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
706 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
708 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
709 UINT fixup = ps->np2_fixup_info->active;
710 UINT i;
712 for (i = 0; fixup; fixup >>= 1, ++i)
714 const struct wined3d_texture *tex = state->textures[i];
715 unsigned char idx = ps->np2_fixup_info->idx[i];
716 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
718 if (!tex)
720 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
721 continue;
724 if (idx % 2)
726 tex_dim[2] = tex->pow2_matrix[0];
727 tex_dim[3] = tex->pow2_matrix[5];
729 else
731 tex_dim[0] = tex->pow2_matrix[0];
732 tex_dim[1] = tex->pow2_matrix[5];
736 GL_EXTCALL(glUniform4fvARB(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
739 /* Context activation is done by the caller (state handler). */
740 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
741 const struct wined3d_state *state)
743 const struct glsl_context_data *ctx_data = context->shader_backend_data;
744 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
745 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
746 const struct wined3d_gl_info *gl_info = context->gl_info;
747 struct shader_glsl_priv *priv = shader_priv;
748 float position_fixup[4];
749 DWORD update_mask = 0;
751 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
752 UINT constant_version;
753 int i;
755 if (!prog) {
756 /* No GLSL program set - nothing to do. */
757 return;
759 constant_version = prog->constant_version;
760 update_mask = context->constant_update_mask & prog->constant_update_mask;
762 if (update_mask & WINED3D_SHADER_CONST_VS_F)
763 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
764 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
766 if (update_mask & WINED3D_SHADER_CONST_VS_I)
767 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
768 vshader->reg_maps.integer_constants);
770 if (update_mask & WINED3D_SHADER_CONST_VS_B)
771 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
772 vshader->reg_maps.boolean_constants);
774 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
776 shader_get_position_fixup(context, state, position_fixup);
777 GL_EXTCALL(glUniform4fvARB(prog->vs.pos_fixup_location, 1, position_fixup));
778 checkGLcall("glUniform4fvARB");
781 if (update_mask & WINED3D_SHADER_CONST_PS_F)
782 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
783 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
785 if (update_mask & WINED3D_SHADER_CONST_PS_I)
786 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
787 pshader->reg_maps.integer_constants);
789 if (update_mask & WINED3D_SHADER_CONST_PS_B)
790 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
791 pshader->reg_maps.boolean_constants);
793 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
795 for (i = 0; i < MAX_TEXTURES; ++i)
797 if (prog->ps.bumpenv_mat_location[i] == -1)
798 continue;
800 GL_EXTCALL(glUniformMatrix2fvARB(prog->ps.bumpenv_mat_location[i], 1, 0,
801 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
803 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
805 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_scale_location[i], 1,
806 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
807 GL_EXTCALL(glUniform1fvARB(prog->ps.bumpenv_lum_offset_location[i], 1,
808 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
812 checkGLcall("bump env uniforms");
815 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
817 float correction_params[4];
819 if (context->render_offscreen)
821 correction_params[0] = 0.0f;
822 correction_params[1] = 1.0f;
823 } else {
824 /* position is window relative, not viewport relative */
825 correction_params[0] = (float) context->current_rt->resource.height;
826 correction_params[1] = -1.0f;
828 GL_EXTCALL(glUniform4fvARB(prog->ps.ycorrection_location, 1, correction_params));
831 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
832 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
834 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
836 float col[4];
838 if (prog->ps.tex_factor_location != -1)
840 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
841 GL_EXTCALL(glUniform4fvARB(prog->ps.tex_factor_location, 1, col));
844 if (state->render_states[WINED3D_RS_SPECULARENABLE])
845 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
846 else
847 GL_EXTCALL(glUniform4fARB(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
849 for (i = 0; i < MAX_TEXTURES; ++i)
851 if (prog->ps.tss_constant_location[i] == -1)
852 continue;
854 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
855 GL_EXTCALL(glUniform4fvARB(prog->ps.tss_constant_location[i], 1, col));
858 checkGLcall("fixed function uniforms");
861 if (priv->next_constant_version == UINT_MAX)
863 TRACE("Max constant version reached, resetting to 0.\n");
864 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
865 priv->next_constant_version = 1;
867 else
869 prog->constant_version = priv->next_constant_version++;
873 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
875 struct constant_entry *entries = heap->entries;
876 unsigned int *positions = heap->positions;
877 unsigned int heap_idx, parent_idx;
879 if (!heap->contained[idx])
881 heap_idx = heap->size++;
882 heap->contained[idx] = TRUE;
884 else
886 heap_idx = positions[idx];
889 while (heap_idx > 1)
891 parent_idx = heap_idx >> 1;
893 if (new_version <= entries[parent_idx].version) break;
895 entries[heap_idx] = entries[parent_idx];
896 positions[entries[parent_idx].idx] = heap_idx;
897 heap_idx = parent_idx;
900 entries[heap_idx].version = new_version;
901 entries[heap_idx].idx = idx;
902 positions[idx] = heap_idx;
905 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
907 struct shader_glsl_priv *priv = device->shader_priv;
908 struct constant_heap *heap = &priv->vconst_heap;
909 UINT i;
911 for (i = start; i < count + start; ++i)
913 update_heap_entry(heap, i, priv->next_constant_version);
916 for (i = 0; i < device->context_count; ++i)
918 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
922 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
924 struct shader_glsl_priv *priv = device->shader_priv;
925 struct constant_heap *heap = &priv->pconst_heap;
926 UINT i;
928 for (i = start; i < count + start; ++i)
930 update_heap_entry(heap, i, priv->next_constant_version);
933 for (i = 0; i < device->context_count; ++i)
935 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
939 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
941 unsigned int ret = gl_info->limits.glsl_varyings / 4;
942 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
943 if(shader_major > 3) return ret;
945 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
946 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
947 return ret;
950 /** Generate the variable & register declarations for the GLSL output target */
951 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
952 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
953 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
955 const struct wined3d_shader_version *version = &reg_maps->shader_version;
956 const struct wined3d_state *state = &shader->device->state;
957 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
958 const struct wined3d_gl_info *gl_info = context->gl_info;
959 const struct wined3d_fb_state *fb = &shader->device->fb;
960 unsigned int i, extra_constants_needed = 0;
961 const struct wined3d_shader_lconst *lconst;
962 const char *prefix;
963 DWORD map;
965 prefix = shader_glsl_get_prefix(version->type);
967 /* Prototype the subroutines */
968 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
970 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
973 /* Declare the constants (aka uniforms) */
974 if (shader->limits.constant_float > 0)
976 unsigned max_constantsF;
978 /* Unless the shader uses indirect addressing, always declare the
979 * maximum array size and ignore that we need some uniforms privately.
980 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
981 * and immediate values, still declare VC[256]. If the shader needs
982 * more uniforms than we have it won't work in any case. If it uses
983 * less, the compiler will figure out which uniforms are really used
984 * and strip them out. This allows a shader to use c255 on a dx9 card,
985 * as long as it doesn't also use all the other constants.
987 * If the shader uses indirect addressing the compiler must assume
988 * that all declared uniforms are used. In this case, declare only the
989 * amount that we're assured to have.
991 * Thus we run into problems in these two cases:
992 * 1) The shader really uses more uniforms than supported.
993 * 2) The shader uses indirect addressing, less constants than
994 * supported, but uses a constant index > #supported consts. */
995 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
997 /* No indirect addressing here. */
998 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1000 else
1002 if (reg_maps->usesrelconstF)
1004 /* Subtract the other potential uniforms from the max
1005 * available (bools, ints, and 1 row of projection matrix).
1006 * Subtract another uniform for immediate values, which have
1007 * to be loaded via uniform by the driver as well. The shader
1008 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1009 * shader code, so one vec4 should be enough. (Unfortunately
1010 * the Nvidia driver doesn't store 128 and -128 in one float).
1012 * Writing gl_ClipVertex requires one uniform for each
1013 * clipplane as well. */
1014 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1015 if(ctx_priv->cur_vs_args->clip_enabled)
1017 max_constantsF -= gl_info->limits.clipplanes;
1019 max_constantsF -= count_bits(reg_maps->integer_constants);
1020 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1021 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1022 * for now take this into account when calculating the number of available constants
1024 max_constantsF -= count_bits(reg_maps->boolean_constants);
1025 /* Set by driver quirks in directx.c */
1026 max_constantsF -= gl_info->reserved_glsl_constants;
1028 if (max_constantsF < shader->limits.constant_float)
1030 static unsigned int once;
1032 if (!once++)
1033 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1034 " it may not render correctly.\n");
1035 else
1036 WARN("The hardware does not support enough uniform components to run this shader.\n");
1039 else
1041 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1044 max_constantsF = min(shader->limits.constant_float, max_constantsF);
1045 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1048 /* Always declare the full set of constants, the compiler can remove the
1049 * unused ones because d3d doesn't (yet) support indirect int and bool
1050 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1051 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
1052 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1054 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1055 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1057 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1059 if (reg_maps->cb_sizes[i])
1060 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1061 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1064 /* Declare texture samplers */
1065 for (i = 0; i < shader->limits.sampler; ++i)
1067 if (reg_maps->sampler_type[i])
1069 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1070 BOOL tex_rect;
1072 switch (reg_maps->sampler_type[i])
1074 case WINED3DSTT_1D:
1075 if (shadow_sampler)
1076 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1077 else
1078 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1079 break;
1080 case WINED3DSTT_2D:
1081 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->np2_fixup & (1 << i));
1082 tex_rect = tex_rect && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1083 if (shadow_sampler)
1085 if (tex_rect)
1086 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1087 else
1088 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1090 else
1092 if (tex_rect)
1093 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1094 else
1095 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1097 break;
1098 case WINED3DSTT_CUBE:
1099 if (shadow_sampler)
1100 FIXME("Unsupported Cube shadow sampler.\n");
1101 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1102 break;
1103 case WINED3DSTT_VOLUME:
1104 if (shadow_sampler)
1105 FIXME("Unsupported 3D shadow sampler.\n");
1106 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1107 break;
1108 default:
1109 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1110 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1111 break;
1116 /* Declare uniforms for NP2 texcoord fixup:
1117 * This is NOT done inside the loop that declares the texture samplers
1118 * since the NP2 fixup code is currently only used for the GeforceFX
1119 * series and when forcing the ARB_npot extension off. Modern cards just
1120 * skip the code anyway, so put it inside a separate loop. */
1121 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1123 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1124 UINT cur = 0;
1126 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1127 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1128 * samplerNP2Fixup stores texture dimensions and is updated through
1129 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1131 for (i = 0; i < shader->limits.sampler; ++i)
1133 if (reg_maps->sampler_type[i])
1135 if (!(ps_args->np2_fixup & (1 << i))) continue;
1137 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1138 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1139 continue;
1142 fixup->idx[i] = cur++;
1146 fixup->num_consts = (cur + 1) >> 1;
1147 fixup->active = ps_args->np2_fixup;
1148 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1151 /* Declare address variables */
1152 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1154 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1157 /* Declare texture coordinate temporaries and initialize them */
1158 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1160 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1163 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1165 /* Declare attributes. */
1166 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1168 if (map & 1)
1169 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1172 shader_addline(buffer, "uniform vec4 posFixup;\n");
1173 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1175 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1177 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits.packed_input);
1179 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1181 if (version->major >= 3)
1183 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1185 if (use_vs(state))
1186 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1187 else
1188 /* TODO: Write a replacement shader for the fixed function
1189 * vertex pipeline, so this isn't needed. For fixed function
1190 * vertex processing + 3.0 pixel shader we need a separate
1191 * function in the pixel shader that reads the fixed function
1192 * color into the packed input registers. */
1193 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1196 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1198 if (!(map & 1))
1199 continue;
1201 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1203 if (reg_maps->luminanceparams & (1 << i))
1205 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1206 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1207 extra_constants_needed++;
1210 extra_constants_needed++;
1213 if (ps_args->srgb_correction)
1215 shader_addline(buffer, "const vec4 srgb_const0 = ");
1216 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1217 shader_addline(buffer, ";\n");
1218 shader_addline(buffer, "const vec4 srgb_const1 = ");
1219 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1220 shader_addline(buffer, ";\n");
1222 if (reg_maps->vpos || reg_maps->usesdsy)
1224 if (shader->limits.constant_float + extra_constants_needed
1225 + 1 < gl_info->limits.glsl_ps_float_constants)
1227 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1228 extra_constants_needed++;
1230 else
1232 float ycorrection[] =
1234 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1235 context->render_offscreen ? 1.0f : -1.0f,
1236 0.0f,
1237 0.0f,
1240 /* This happens because we do not have proper tracking of the
1241 * constant registers that are actually used, only the max
1242 * limit of the shader version. */
1243 FIXME("Cannot find a free uniform for vpos correction params\n");
1244 shader_addline(buffer, "const vec4 ycorrection = ");
1245 shader_glsl_append_imm_vec4(buffer, ycorrection);
1246 shader_addline(buffer, ";\n");
1248 shader_addline(buffer, "vec4 vpos;\n");
1252 /* Declare output register temporaries */
1253 if (shader->limits.packed_output)
1254 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1256 /* Declare temporary variables */
1257 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1259 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1262 /* Declare loop registers aLx */
1263 if (version->major < 4)
1265 for (i = 0; i < reg_maps->loop_depth; ++i)
1267 shader_addline(buffer, "int aL%u;\n", i);
1268 shader_addline(buffer, "int tmpInt%u;\n", i);
1272 /* Temporary variables for matrix operations */
1273 shader_addline(buffer, "vec4 tmp0;\n");
1274 shader_addline(buffer, "vec4 tmp1;\n");
1276 if (!shader->load_local_constsF)
1278 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1280 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1281 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1282 shader_addline(buffer, ";\n");
1286 /* Start the main program. */
1287 shader_addline(buffer, "void main()\n{\n");
1289 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1290 * add approximately 0.5. This causes off-by-one problems as spotted by
1291 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1292 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1293 * causes precision troubles when we just subtract 0.5.
1295 * To deal with that, just floor() the position. This will eliminate the
1296 * fraction on all cards.
1298 * TODO: Test how this behaves with multisampling.
1300 * An advantage of floor is that it works even if the driver doesn't add
1301 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1302 * to return in gl_FragCoord, even though coordinates specify the pixel
1303 * centers instead of the pixel corners. This code will behave correctly
1304 * on drivers that returns integer values. */
1305 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1306 shader_addline(buffer,
1307 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1310 /*****************************************************************************
1311 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1313 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1314 ****************************************************************************/
1316 /* Prototypes */
1317 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1318 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1320 /** Used for opcode modifiers - They multiply the result by the specified amount */
1321 static const char * const shift_glsl_tab[] = {
1322 "", /* 0 (none) */
1323 "2.0 * ", /* 1 (x2) */
1324 "4.0 * ", /* 2 (x4) */
1325 "8.0 * ", /* 3 (x8) */
1326 "16.0 * ", /* 4 (x16) */
1327 "32.0 * ", /* 5 (x32) */
1328 "", /* 6 (x64) */
1329 "", /* 7 (x128) */
1330 "", /* 8 (d256) */
1331 "", /* 9 (d128) */
1332 "", /* 10 (d64) */
1333 "", /* 11 (d32) */
1334 "0.0625 * ", /* 12 (d16) */
1335 "0.125 * ", /* 13 (d8) */
1336 "0.25 * ", /* 14 (d4) */
1337 "0.5 * " /* 15 (d2) */
1340 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1341 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1342 const char *in_reg, const char *in_regswizzle, char *out_str)
1344 out_str[0] = 0;
1346 switch (src_modifier)
1348 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1349 case WINED3DSPSM_DW:
1350 case WINED3DSPSM_NONE:
1351 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1352 break;
1353 case WINED3DSPSM_NEG:
1354 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1355 break;
1356 case WINED3DSPSM_NOT:
1357 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1358 break;
1359 case WINED3DSPSM_BIAS:
1360 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1361 break;
1362 case WINED3DSPSM_BIASNEG:
1363 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1364 break;
1365 case WINED3DSPSM_SIGN:
1366 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1367 break;
1368 case WINED3DSPSM_SIGNNEG:
1369 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1370 break;
1371 case WINED3DSPSM_COMP:
1372 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1373 break;
1374 case WINED3DSPSM_X2:
1375 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1376 break;
1377 case WINED3DSPSM_X2NEG:
1378 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1379 break;
1380 case WINED3DSPSM_ABS:
1381 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1382 break;
1383 case WINED3DSPSM_ABSNEG:
1384 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1385 break;
1386 default:
1387 FIXME("Unhandled modifier %u\n", src_modifier);
1388 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1392 /** Writes the GLSL variable name that corresponds to the register that the
1393 * DX opcode parameter is trying to access */
1394 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1395 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1397 /* oPos, oFog and oPts in D3D */
1398 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1400 const struct wined3d_shader *shader = ins->ctx->shader;
1401 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1402 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1403 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1404 const char *prefix = shader_glsl_get_prefix(version->type);
1405 struct glsl_src_param rel_param0, rel_param1;
1406 char imm_str[4][17];
1408 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1409 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1410 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1411 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1412 *is_color = FALSE;
1414 switch (reg->type)
1416 case WINED3DSPR_TEMP:
1417 sprintf(register_name, "R%u", reg->idx[0].offset);
1418 break;
1420 case WINED3DSPR_INPUT:
1421 /* vertex shaders */
1422 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1424 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1425 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1426 *is_color = TRUE;
1427 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1428 break;
1431 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1433 if (reg->idx[0].rel_addr)
1435 if (reg->idx[1].rel_addr)
1436 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1437 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1438 else
1439 sprintf(register_name, "gs_in[%s + %u][%u]",
1440 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1442 else if (reg->idx[1].rel_addr)
1443 sprintf(register_name, "gs_in[%u][%s + %u]",
1444 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1445 else
1446 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1447 break;
1450 /* pixel shaders >= 3.0 */
1451 if (version->major >= 3)
1453 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1454 unsigned int in_count = vec4_varyings(version->major, gl_info);
1456 if (reg->idx[0].rel_addr)
1458 /* Removing a + 0 would be an obvious optimization, but
1459 * OS X doesn't see the NOP operation there. */
1460 if (idx)
1462 if (shader->u.ps.declared_in_count > in_count)
1464 sprintf(register_name,
1465 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1466 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1467 prefix, rel_param0.param_str, idx);
1469 else
1471 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1474 else
1476 if (shader->u.ps.declared_in_count > in_count)
1478 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1479 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1480 prefix, rel_param0.param_str);
1482 else
1484 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
1488 else
1490 if (idx == in_count) sprintf(register_name, "gl_Color");
1491 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1492 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1495 else
1497 if (!reg->idx[0].offset)
1498 strcpy(register_name, "gl_Color");
1499 else
1500 strcpy(register_name, "gl_SecondaryColor");
1501 break;
1503 break;
1505 case WINED3DSPR_CONST:
1507 /* Relative addressing */
1508 if (reg->idx[0].rel_addr)
1510 if (reg->idx[0].offset)
1511 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
1512 else
1513 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
1515 else
1517 if (shader_constant_is_local(shader, reg->idx[0].offset))
1518 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1519 else
1520 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1523 break;
1525 case WINED3DSPR_CONSTINT:
1526 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1527 break;
1529 case WINED3DSPR_CONSTBOOL:
1530 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1531 break;
1533 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1534 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1535 sprintf(register_name, "T%u", reg->idx[0].offset);
1536 else
1537 sprintf(register_name, "A%u", reg->idx[0].offset);
1538 break;
1540 case WINED3DSPR_LOOP:
1541 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1542 break;
1544 case WINED3DSPR_SAMPLER:
1545 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1546 break;
1548 case WINED3DSPR_COLOROUT:
1549 if (reg->idx[0].offset >= gl_info->limits.buffers)
1550 WARN("Write to render target %u, only %d supported.\n",
1551 reg->idx[0].offset, gl_info->limits.buffers);
1553 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1554 break;
1556 case WINED3DSPR_RASTOUT:
1557 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1558 break;
1560 case WINED3DSPR_DEPTHOUT:
1561 sprintf(register_name, "gl_FragDepth");
1562 break;
1564 case WINED3DSPR_ATTROUT:
1565 if (!reg->idx[0].offset)
1566 sprintf(register_name, "%s_out[8]", prefix);
1567 else
1568 sprintf(register_name, "%s_out[9]", prefix);
1569 break;
1571 case WINED3DSPR_TEXCRDOUT:
1572 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1573 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1574 break;
1576 case WINED3DSPR_MISCTYPE:
1577 if (!reg->idx[0].offset)
1579 /* vPos */
1580 sprintf(register_name, "vpos");
1582 else if (reg->idx[0].offset == 1)
1584 /* Note that gl_FrontFacing is a bool, while vFace is
1585 * a float for which the sign determines front/back */
1586 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1588 else
1590 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1591 sprintf(register_name, "unrecognized_register");
1593 break;
1595 case WINED3DSPR_IMMCONST:
1596 switch (reg->immconst_type)
1598 case WINED3D_IMMCONST_SCALAR:
1599 switch (reg->data_type)
1601 case WINED3D_DATA_FLOAT:
1602 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
1603 break;
1604 case WINED3D_DATA_INT:
1605 sprintf(register_name, "%#x", reg->immconst_data[0]);
1606 break;
1607 case WINED3D_DATA_RESOURCE:
1608 case WINED3D_DATA_SAMPLER:
1609 case WINED3D_DATA_UINT:
1610 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1611 break;
1612 default:
1613 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1614 break;
1616 break;
1618 case WINED3D_IMMCONST_VEC4:
1619 switch (reg->data_type)
1621 case WINED3D_DATA_FLOAT:
1622 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
1623 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
1624 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
1625 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
1626 sprintf(register_name, "vec4(%s, %s, %s, %s)",
1627 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
1628 break;
1629 case WINED3D_DATA_INT:
1630 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1631 reg->immconst_data[0], reg->immconst_data[1],
1632 reg->immconst_data[2], reg->immconst_data[3]);
1633 break;
1634 case WINED3D_DATA_RESOURCE:
1635 case WINED3D_DATA_SAMPLER:
1636 case WINED3D_DATA_UINT:
1637 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1638 reg->immconst_data[0], reg->immconst_data[1],
1639 reg->immconst_data[2], reg->immconst_data[3]);
1640 break;
1641 default:
1642 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1643 break;
1645 break;
1647 default:
1648 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1649 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1651 break;
1653 case WINED3DSPR_CONSTBUFFER:
1654 if (reg->idx[1].rel_addr)
1655 sprintf(register_name, "%s_cb%u[%s + %u]",
1656 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1657 else
1658 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1659 break;
1661 case WINED3DSPR_PRIMID:
1662 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1663 break;
1665 default:
1666 FIXME("Unhandled register type %#x.\n", reg->type);
1667 sprintf(register_name, "unrecognized_register");
1668 break;
1672 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1674 *str++ = '.';
1675 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1676 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1677 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1678 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1679 *str = '\0';
1682 /* Get the GLSL write mask for the destination register */
1683 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1685 DWORD mask = param->write_mask;
1687 if (shader_is_scalar(&param->reg))
1689 mask = WINED3DSP_WRITEMASK_0;
1690 *write_mask = '\0';
1692 else
1694 shader_glsl_write_mask_to_str(mask, write_mask);
1697 return mask;
1700 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1701 unsigned int size = 0;
1703 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1704 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1705 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1706 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1708 return size;
1711 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1713 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1714 * but addressed as "rgba". To fix this we need to swap the register's x
1715 * and z components. */
1716 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1718 *str++ = '.';
1719 /* swizzle bits fields: wwzzyyxx */
1720 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1721 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1722 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1723 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1724 *str = '\0';
1727 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1728 BOOL fixup, DWORD mask, char *swizzle_str)
1730 if (shader_is_scalar(&param->reg))
1731 *swizzle_str = '\0';
1732 else
1733 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1736 /* From a given parameter token, generate the corresponding GLSL string.
1737 * Also, return the actual register name and swizzle in case the
1738 * caller needs this information as well. */
1739 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1740 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1742 BOOL is_color = FALSE;
1743 char swizzle_str[6];
1745 glsl_src->reg_name[0] = '\0';
1746 glsl_src->param_str[0] = '\0';
1747 swizzle_str[0] = '\0';
1749 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1750 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1752 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1754 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1756 else
1758 char param_str[200];
1760 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1762 switch (wined3d_src->reg.data_type)
1764 case WINED3D_DATA_FLOAT:
1765 sprintf(glsl_src->param_str, "%s", param_str);
1766 break;
1767 case WINED3D_DATA_INT:
1768 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1769 break;
1770 case WINED3D_DATA_RESOURCE:
1771 case WINED3D_DATA_SAMPLER:
1772 case WINED3D_DATA_UINT:
1773 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1774 break;
1775 default:
1776 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1777 sprintf(glsl_src->param_str, "%s", param_str);
1778 break;
1783 /* From a given parameter token, generate the corresponding GLSL string.
1784 * Also, return the actual register name and swizzle in case the
1785 * caller needs this information as well. */
1786 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1787 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1789 BOOL is_color = FALSE;
1791 glsl_dst->mask_str[0] = '\0';
1792 glsl_dst->reg_name[0] = '\0';
1794 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1795 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1798 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1799 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1800 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1802 struct glsl_dst_param glsl_dst;
1803 DWORD mask;
1805 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1807 switch (dst->reg.data_type)
1809 case WINED3D_DATA_FLOAT:
1810 shader_addline(buffer, "%s%s = %s(",
1811 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1812 break;
1813 case WINED3D_DATA_INT:
1814 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1815 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1816 break;
1817 case WINED3D_DATA_RESOURCE:
1818 case WINED3D_DATA_SAMPLER:
1819 case WINED3D_DATA_UINT:
1820 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1821 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1822 break;
1823 default:
1824 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1825 shader_addline(buffer, "%s%s = %s(",
1826 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1827 break;
1831 return mask;
1834 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1835 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1837 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1840 /** Process GLSL instruction modifiers */
1841 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1843 struct glsl_dst_param dst_param;
1844 DWORD modifiers;
1846 if (!ins->dst_count) return;
1848 modifiers = ins->dst[0].modifiers;
1849 if (!modifiers) return;
1851 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1853 if (modifiers & WINED3DSPDM_SATURATE)
1855 /* _SAT means to clamp the value of the register to between 0 and 1 */
1856 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1857 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1860 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1862 FIXME("_centroid modifier not handled\n");
1865 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1867 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1871 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1873 switch (op)
1875 case WINED3D_SHADER_REL_OP_GT: return ">";
1876 case WINED3D_SHADER_REL_OP_EQ: return "==";
1877 case WINED3D_SHADER_REL_OP_GE: return ">=";
1878 case WINED3D_SHADER_REL_OP_LT: return "<";
1879 case WINED3D_SHADER_REL_OP_NE: return "!=";
1880 case WINED3D_SHADER_REL_OP_LE: return "<=";
1881 default:
1882 FIXME("Unrecognized operator %#x.\n", op);
1883 return "(\?\?)";
1887 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1888 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1890 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1891 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1892 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1893 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1894 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1895 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1896 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1897 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1899 /* Note that there's no such thing as a projected cube texture. */
1900 switch(sampler_type) {
1901 case WINED3DSTT_1D:
1902 if (shadow)
1904 if (lod)
1906 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1908 else if (grad)
1910 if (gl_info->supported[EXT_GPU_SHADER4])
1911 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1912 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1913 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1914 else
1916 FIXME("Unsupported 1D shadow grad function.\n");
1917 sample_function->name = "unsupported1DGrad";
1920 else
1922 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1924 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1926 else
1928 if (lod)
1930 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1932 else if (grad)
1934 if (gl_info->supported[EXT_GPU_SHADER4])
1935 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1936 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1937 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1938 else
1940 FIXME("Unsupported 1D grad function.\n");
1941 sample_function->name = "unsupported1DGrad";
1944 else
1946 sample_function->name = projected ? "texture1DProj" : "texture1D";
1948 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1950 break;
1952 case WINED3DSTT_2D:
1953 if (shadow)
1955 if (texrect)
1957 if (lod)
1959 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1961 else if (grad)
1963 if (gl_info->supported[EXT_GPU_SHADER4])
1964 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1965 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1966 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1967 else
1969 FIXME("Unsupported RECT shadow grad function.\n");
1970 sample_function->name = "unsupported2DRectGrad";
1973 else
1975 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1978 else
1980 if (lod)
1982 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1984 else if (grad)
1986 if (gl_info->supported[EXT_GPU_SHADER4])
1987 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1988 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1989 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1990 else
1992 FIXME("Unsupported 2D shadow grad function.\n");
1993 sample_function->name = "unsupported2DGrad";
1996 else
1998 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2001 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2003 else
2005 if (texrect)
2007 if (lod)
2009 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2011 else if (grad)
2013 if (gl_info->supported[EXT_GPU_SHADER4])
2014 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2015 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2016 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2017 else
2019 FIXME("Unsupported RECT grad function.\n");
2020 sample_function->name = "unsupported2DRectGrad";
2023 else
2025 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2028 else
2030 if (lod)
2032 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2034 else if (grad)
2036 if (gl_info->supported[EXT_GPU_SHADER4])
2037 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2038 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2039 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2040 else
2042 FIXME("Unsupported 2D grad function.\n");
2043 sample_function->name = "unsupported2DGrad";
2046 else
2048 sample_function->name = projected ? "texture2DProj" : "texture2D";
2051 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2053 break;
2055 case WINED3DSTT_CUBE:
2056 if (shadow)
2058 FIXME("Unsupported Cube shadow function.\n");
2059 sample_function->name = "unsupportedCubeShadow";
2060 sample_function->coord_mask = 0;
2062 else
2064 if (lod)
2066 sample_function->name = "textureCubeLod";
2068 else if (grad)
2070 if (gl_info->supported[EXT_GPU_SHADER4])
2071 sample_function->name = "textureCubeGrad";
2072 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2073 sample_function->name = "textureCubeGradARB";
2074 else
2076 FIXME("Unsupported Cube grad function.\n");
2077 sample_function->name = "unsupportedCubeGrad";
2080 else
2082 sample_function->name = "textureCube";
2084 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2086 break;
2088 case WINED3DSTT_VOLUME:
2089 if (shadow)
2091 FIXME("Unsupported 3D shadow function.\n");
2092 sample_function->name = "unsupported3DShadow";
2093 sample_function->coord_mask = 0;
2095 else
2097 if (lod)
2099 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2101 else if (grad)
2103 if (gl_info->supported[EXT_GPU_SHADER4])
2104 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2105 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2106 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2107 else
2109 FIXME("Unsupported 3D grad function.\n");
2110 sample_function->name = "unsupported3DGrad";
2113 else
2115 sample_function->name = projected ? "texture3DProj" : "texture3D";
2117 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2119 break;
2121 default:
2122 sample_function->name = "";
2123 sample_function->coord_mask = 0;
2124 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2125 break;
2129 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2130 BOOL sign_fixup, enum fixup_channel_source channel_source)
2132 switch(channel_source)
2134 case CHANNEL_SOURCE_ZERO:
2135 strcat(arguments, "0.0");
2136 break;
2138 case CHANNEL_SOURCE_ONE:
2139 strcat(arguments, "1.0");
2140 break;
2142 case CHANNEL_SOURCE_X:
2143 strcat(arguments, reg_name);
2144 strcat(arguments, ".x");
2145 break;
2147 case CHANNEL_SOURCE_Y:
2148 strcat(arguments, reg_name);
2149 strcat(arguments, ".y");
2150 break;
2152 case CHANNEL_SOURCE_Z:
2153 strcat(arguments, reg_name);
2154 strcat(arguments, ".z");
2155 break;
2157 case CHANNEL_SOURCE_W:
2158 strcat(arguments, reg_name);
2159 strcat(arguments, ".w");
2160 break;
2162 default:
2163 FIXME("Unhandled channel source %#x\n", channel_source);
2164 strcat(arguments, "undefined");
2165 break;
2168 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2171 static void shader_glsl_color_correction_ext(struct wined3d_shader_buffer *buffer,
2172 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2174 unsigned int mask_size, remaining;
2175 DWORD fixup_mask = 0;
2176 char arguments[256];
2177 char mask_str[6];
2179 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2180 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2181 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2182 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2183 if (!(mask &= fixup_mask))
2184 return;
2186 if (is_complex_fixup(fixup))
2188 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2189 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2190 return;
2193 shader_glsl_write_mask_to_str(mask, mask_str);
2194 mask_size = shader_glsl_get_write_mask_size(mask);
2196 arguments[0] = '\0';
2197 remaining = mask_size;
2198 if (mask & WINED3DSP_WRITEMASK_0)
2200 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2201 if (--remaining) strcat(arguments, ", ");
2203 if (mask & WINED3DSP_WRITEMASK_1)
2205 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2206 if (--remaining) strcat(arguments, ", ");
2208 if (mask & WINED3DSP_WRITEMASK_2)
2210 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2211 if (--remaining) strcat(arguments, ", ");
2213 if (mask & WINED3DSP_WRITEMASK_3)
2215 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2216 if (--remaining) strcat(arguments, ", ");
2219 if (mask_size > 1)
2220 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2221 else
2222 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2225 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2227 char reg_name[256];
2228 BOOL is_color;
2230 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2231 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2234 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2235 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2236 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2238 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2239 char dst_swizzle[6];
2240 struct color_fixup_desc fixup;
2241 BOOL np2_fixup = FALSE;
2242 va_list args;
2244 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2246 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2248 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2249 fixup = priv->cur_ps_args->color_fixup[sampler];
2251 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2252 if(bias) {
2253 FIXME("Biased sampling from NP2 textures is unsupported\n");
2254 } else {
2255 np2_fixup = TRUE;
2259 else
2261 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2264 shader_glsl_append_dst(ins->ctx->buffer, ins);
2266 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2267 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2269 va_start(args, coord_reg_fmt);
2270 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2271 va_end(args);
2273 if(bias) {
2274 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2275 } else {
2276 if (np2_fixup) {
2277 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2278 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2280 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2281 (idx % 2) ? "zw" : "xy", dst_swizzle);
2282 } else if(dx && dy) {
2283 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2284 } else {
2285 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2289 if(!is_identity_fixup(fixup)) {
2290 shader_glsl_color_correction(ins, fixup);
2294 /*****************************************************************************
2295 * Begin processing individual instruction opcodes
2296 ****************************************************************************/
2298 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2300 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2301 struct glsl_src_param src0_param;
2302 struct glsl_src_param src1_param;
2303 DWORD write_mask;
2304 const char *op;
2306 /* Determine the GLSL operator to use based on the opcode */
2307 switch (ins->handler_idx)
2309 case WINED3DSIH_ADD: op = "+"; break;
2310 case WINED3DSIH_AND: op = "&"; break;
2311 case WINED3DSIH_DIV: op = "/"; break;
2312 case WINED3DSIH_IADD: op = "+"; break;
2313 case WINED3DSIH_ISHL: op = "<<"; break;
2314 case WINED3DSIH_MUL: op = "*"; break;
2315 case WINED3DSIH_SUB: op = "-"; break;
2316 case WINED3DSIH_USHR: op = ">>"; break;
2317 case WINED3DSIH_XOR: op = "^"; break;
2318 default:
2319 op = "<unhandled operator>";
2320 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2321 break;
2324 write_mask = shader_glsl_append_dst(buffer, ins);
2325 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2326 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2327 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2330 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2332 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2333 struct glsl_src_param src0_param;
2334 struct glsl_src_param src1_param;
2335 unsigned int mask_size;
2336 DWORD write_mask;
2337 const char *op;
2339 write_mask = shader_glsl_append_dst(buffer, ins);
2340 mask_size = shader_glsl_get_write_mask_size(write_mask);
2341 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2342 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2344 if (mask_size > 1)
2346 switch (ins->handler_idx)
2348 case WINED3DSIH_EQ: op = "equal"; break;
2349 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2350 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2351 case WINED3DSIH_LT: op = "lessThan"; break;
2352 default:
2353 op = "<unhandled operator>";
2354 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2355 break;
2358 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2359 mask_size, op, src0_param.param_str, src1_param.param_str);
2361 else
2363 switch (ins->handler_idx)
2365 case WINED3DSIH_EQ: op = "=="; break;
2366 case WINED3DSIH_GE: op = ">="; break;
2367 case WINED3DSIH_IGE: op = ">="; break;
2368 case WINED3DSIH_LT: op = "<"; break;
2369 default:
2370 op = "<unhandled operator>";
2371 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2372 break;
2375 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2376 src0_param.param_str, op, src1_param.param_str);
2380 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2382 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2383 struct glsl_src_param src0_param;
2384 struct glsl_src_param src1_param;
2385 DWORD write_mask;
2387 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2388 * not, we can emulate it. */
2389 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2390 FIXME("64-bit integer multiplies not implemented.\n");
2392 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2394 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2395 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2396 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2398 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2399 src0_param.param_str, src1_param.param_str);
2403 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2405 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2406 struct glsl_src_param src0_param, src1_param;
2407 DWORD write_mask;
2409 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2412 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2414 char dst_mask[6];
2416 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2417 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2418 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2419 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2420 dst_mask, src0_param.param_str, src1_param.param_str);
2422 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2423 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2424 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2425 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2427 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2428 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2430 else
2432 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
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);
2438 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2440 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2441 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2442 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2443 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2447 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2448 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2450 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2451 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2452 struct glsl_src_param src0_param;
2453 DWORD write_mask;
2455 write_mask = shader_glsl_append_dst(buffer, ins);
2456 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2458 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2459 * shader versions WINED3DSIO_MOVA is used for this. */
2460 if (ins->ctx->reg_maps->shader_version.major == 1
2461 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2462 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2464 /* This is a simple floor() */
2465 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2466 if (mask_size > 1) {
2467 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2468 } else {
2469 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2472 else if(ins->handler_idx == WINED3DSIH_MOVA)
2474 /* We need to *round* to the nearest int here. */
2475 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2477 if (gl_info->supported[EXT_GPU_SHADER4])
2479 if (mask_size > 1)
2480 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2481 else
2482 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2484 else
2486 if (mask_size > 1)
2487 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2488 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2489 else
2490 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2491 src0_param.param_str, src0_param.param_str);
2494 else
2496 shader_addline(buffer, "%s);\n", src0_param.param_str);
2500 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2501 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2503 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2504 struct glsl_src_param src0_param;
2505 struct glsl_src_param src1_param;
2506 DWORD dst_write_mask, src_write_mask;
2507 unsigned int dst_size = 0;
2509 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2510 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2512 /* dp3 works on vec3, dp4 on vec4 */
2513 if (ins->handler_idx == WINED3DSIH_DP4)
2515 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2516 } else {
2517 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2520 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2521 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2523 if (dst_size > 1) {
2524 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2525 } else {
2526 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2530 /* Note that this instruction has some restrictions. The destination write mask
2531 * can't contain the w component, and the source swizzles have to be .xyzw */
2532 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2534 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2535 struct glsl_src_param src0_param;
2536 struct glsl_src_param src1_param;
2537 char dst_mask[6];
2539 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2540 shader_glsl_append_dst(ins->ctx->buffer, ins);
2541 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2542 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2543 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2546 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2548 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2551 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2552 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2553 * GLSL uses the value as-is. */
2554 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2556 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2557 struct glsl_src_param src0_param;
2558 struct glsl_src_param src1_param;
2559 DWORD dst_write_mask;
2560 unsigned int dst_size;
2562 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2563 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2565 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2566 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2568 if (dst_size > 1)
2570 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2571 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2573 else
2575 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2576 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2580 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2581 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2583 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2584 struct glsl_src_param src_param;
2585 const char *instruction;
2586 DWORD write_mask;
2587 unsigned i;
2589 /* Determine the GLSL function to use based on the opcode */
2590 /* TODO: Possibly make this a table for faster lookups */
2591 switch (ins->handler_idx)
2593 case WINED3DSIH_MIN: instruction = "min"; break;
2594 case WINED3DSIH_MAX: instruction = "max"; break;
2595 case WINED3DSIH_ABS: instruction = "abs"; break;
2596 case WINED3DSIH_FRC: instruction = "fract"; break;
2597 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2598 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2599 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2600 default: instruction = "";
2601 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2602 break;
2605 write_mask = shader_glsl_append_dst(buffer, ins);
2607 shader_addline(buffer, "%s(", instruction);
2609 if (ins->src_count)
2611 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2612 shader_addline(buffer, "%s", src_param.param_str);
2613 for (i = 1; i < ins->src_count; ++i)
2615 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2616 shader_addline(buffer, ", %s", src_param.param_str);
2620 shader_addline(buffer, "));\n");
2623 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2625 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2627 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2628 struct glsl_src_param src_param;
2629 unsigned int mask_size;
2630 DWORD write_mask;
2631 char dst_mask[6];
2633 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2634 mask_size = shader_glsl_get_write_mask_size(write_mask);
2635 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2637 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2638 src_param.param_str, src_param.param_str);
2639 shader_glsl_append_dst(buffer, ins);
2641 if (mask_size > 1)
2643 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2644 mask_size, src_param.param_str);
2646 else
2648 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2649 src_param.param_str);
2653 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
2655 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2656 struct glsl_src_param src0_param;
2657 const char *prefix, *suffix;
2658 unsigned int dst_size;
2659 DWORD dst_write_mask;
2661 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2662 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2664 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
2666 switch (ins->handler_idx)
2668 case WINED3DSIH_EXP:
2669 case WINED3DSIH_EXPP:
2670 prefix = "exp2(";
2671 suffix = ")";
2672 break;
2674 case WINED3DSIH_LOG:
2675 case WINED3DSIH_LOGP:
2676 prefix = "log2(abs(";
2677 suffix = "))";
2678 break;
2680 case WINED3DSIH_RCP:
2681 prefix = "1.0 / ";
2682 suffix = "";
2683 break;
2685 case WINED3DSIH_RSQ:
2686 prefix = "inversesqrt(abs(";
2687 suffix = "))";
2688 break;
2690 default:
2691 prefix = "";
2692 suffix = "";
2693 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2694 break;
2697 if (dst_size > 1)
2698 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
2699 else
2700 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
2703 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2704 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2705 * dst.x = 2^(floor(src))
2706 * dst.y = src - floor(src)
2707 * dst.z = 2^src (partial precision is allowed, but optional)
2708 * dst.w = 1.0;
2709 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2710 * dst = 2^src; (partial precision is allowed, but optional)
2712 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2714 if (ins->ctx->reg_maps->shader_version.major < 2)
2716 struct glsl_src_param src_param;
2717 char dst_mask[6];
2719 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2721 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2722 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2723 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2724 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2726 shader_glsl_append_dst(ins->ctx->buffer, ins);
2727 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2728 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2729 return;
2732 shader_glsl_scalar_op(ins);
2735 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2737 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2738 struct glsl_src_param src_param;
2739 unsigned int mask_size;
2740 DWORD write_mask;
2742 write_mask = shader_glsl_append_dst(buffer, ins);
2743 mask_size = shader_glsl_get_write_mask_size(write_mask);
2744 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2746 if (mask_size > 1)
2747 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2748 else
2749 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2752 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2754 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2755 struct glsl_src_param src_param;
2756 unsigned int mask_size;
2757 DWORD write_mask;
2759 write_mask = shader_glsl_append_dst(buffer, ins);
2760 mask_size = shader_glsl_get_write_mask_size(write_mask);
2761 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2763 if (mask_size > 1)
2764 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2765 else
2766 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2769 /** Process signed comparison opcodes in GLSL. */
2770 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2772 struct glsl_src_param src0_param;
2773 struct glsl_src_param src1_param;
2774 DWORD write_mask;
2775 unsigned int mask_size;
2777 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2778 mask_size = shader_glsl_get_write_mask_size(write_mask);
2779 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2780 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2782 if (mask_size > 1) {
2783 const char *compare;
2785 switch(ins->handler_idx)
2787 case WINED3DSIH_SLT: compare = "lessThan"; break;
2788 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2789 default: compare = "";
2790 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2793 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2794 src0_param.param_str, src1_param.param_str);
2795 } else {
2796 switch(ins->handler_idx)
2798 case WINED3DSIH_SLT:
2799 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2800 * to return 0.0 but step returns 1.0 because step is not < x
2801 * An alternative is a bvec compare padded with an unused second component.
2802 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2803 * issue. Playing with not() is not possible either because not() does not accept
2804 * a scalar.
2806 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2807 src0_param.param_str, src1_param.param_str);
2808 break;
2809 case WINED3DSIH_SGE:
2810 /* Here we can use the step() function and safe a conditional */
2811 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2812 break;
2813 default:
2814 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2820 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2822 const char *condition_prefix, *condition_suffix;
2823 struct wined3d_shader_dst_param dst;
2824 struct glsl_src_param src0_param;
2825 struct glsl_src_param src1_param;
2826 struct glsl_src_param src2_param;
2827 BOOL temp_destination = FALSE;
2828 DWORD cmp_channel = 0;
2829 unsigned int i, j;
2830 char mask_char[6];
2831 DWORD write_mask;
2833 switch (ins->handler_idx)
2835 case WINED3DSIH_CMP:
2836 condition_prefix = "";
2837 condition_suffix = " >= 0.0";
2838 break;
2840 case WINED3DSIH_CND:
2841 condition_prefix = "";
2842 condition_suffix = " > 0.5";
2843 break;
2845 case WINED3DSIH_MOVC:
2846 condition_prefix = "bool(";
2847 condition_suffix = ")";
2848 break;
2850 default:
2851 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2852 condition_prefix = "<unhandled prefix>";
2853 condition_suffix = "<unhandled suffix>";
2854 break;
2857 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
2859 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2860 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2861 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2862 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2864 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2865 condition_prefix, src0_param.param_str, condition_suffix,
2866 src1_param.param_str, src2_param.param_str);
2867 return;
2870 dst = ins->dst[0];
2872 /* Splitting the instruction up in multiple lines imposes a problem:
2873 * The first lines may overwrite source parameters of the following lines.
2874 * Deal with that by using a temporary destination register if needed. */
2875 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2876 && ins->src[0].reg.type == dst.reg.type)
2877 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2878 && ins->src[1].reg.type == dst.reg.type)
2879 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2880 && ins->src[2].reg.type == dst.reg.type))
2881 temp_destination = TRUE;
2883 /* Cycle through all source0 channels. */
2884 for (i = 0; i < 4; ++i)
2886 write_mask = 0;
2887 /* Find the destination channels which use the current source0 channel. */
2888 for (j = 0; j < 4; ++j)
2890 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2892 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2893 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2896 dst.write_mask = ins->dst[0].write_mask & write_mask;
2898 if (temp_destination)
2900 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2901 continue;
2902 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2904 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2905 continue;
2907 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2908 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2909 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2911 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2912 condition_prefix, src0_param.param_str, condition_suffix,
2913 src1_param.param_str, src2_param.param_str);
2916 if (temp_destination)
2918 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2919 shader_glsl_append_dst(ins->ctx->buffer, ins);
2920 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2924 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2925 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2926 * the compare is done per component of src0. */
2927 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2929 struct glsl_src_param src0_param;
2930 struct glsl_src_param src1_param;
2931 struct glsl_src_param src2_param;
2932 DWORD write_mask;
2933 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2934 ins->ctx->reg_maps->shader_version.minor);
2936 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2938 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2939 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2940 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2941 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2943 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
2944 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2945 else
2946 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2947 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2948 return;
2951 shader_glsl_conditional_move(ins);
2954 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2955 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2957 struct glsl_src_param src0_param;
2958 struct glsl_src_param src1_param;
2959 struct glsl_src_param src2_param;
2960 DWORD write_mask;
2962 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2963 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2964 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2965 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2966 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2967 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2970 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2971 Vertex shaders to GLSL codes */
2972 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2974 int i;
2975 int nComponents = 0;
2976 struct wined3d_shader_dst_param tmp_dst = {{0}};
2977 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2978 struct wined3d_shader_instruction tmp_ins;
2980 memset(&tmp_ins, 0, sizeof(tmp_ins));
2982 /* Set constants for the temporary argument */
2983 tmp_ins.ctx = ins->ctx;
2984 tmp_ins.dst_count = 1;
2985 tmp_ins.dst = &tmp_dst;
2986 tmp_ins.src_count = 2;
2987 tmp_ins.src = tmp_src;
2989 switch(ins->handler_idx)
2991 case WINED3DSIH_M4x4:
2992 nComponents = 4;
2993 tmp_ins.handler_idx = WINED3DSIH_DP4;
2994 break;
2995 case WINED3DSIH_M4x3:
2996 nComponents = 3;
2997 tmp_ins.handler_idx = WINED3DSIH_DP4;
2998 break;
2999 case WINED3DSIH_M3x4:
3000 nComponents = 4;
3001 tmp_ins.handler_idx = WINED3DSIH_DP3;
3002 break;
3003 case WINED3DSIH_M3x3:
3004 nComponents = 3;
3005 tmp_ins.handler_idx = WINED3DSIH_DP3;
3006 break;
3007 case WINED3DSIH_M3x2:
3008 nComponents = 2;
3009 tmp_ins.handler_idx = WINED3DSIH_DP3;
3010 break;
3011 default:
3012 break;
3015 tmp_dst = ins->dst[0];
3016 tmp_src[0] = ins->src[0];
3017 tmp_src[1] = ins->src[1];
3018 for (i = 0; i < nComponents; ++i)
3020 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3021 shader_glsl_dot(&tmp_ins);
3022 ++tmp_src[1].reg.idx[0].offset;
3027 The LRP instruction performs a component-wise linear interpolation
3028 between the second and third operands using the first operand as the
3029 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3030 This is equivalent to mix(src2, src1, src0);
3032 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3034 struct glsl_src_param src0_param;
3035 struct glsl_src_param src1_param;
3036 struct glsl_src_param src2_param;
3037 DWORD write_mask;
3039 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3041 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3042 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3043 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3045 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3046 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3049 /** Process the WINED3DSIO_LIT instruction in GLSL:
3050 * dst.x = dst.w = 1.0
3051 * dst.y = (src0.x > 0) ? src0.x
3052 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3053 * where src.w is clamped at +- 128
3055 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3057 struct glsl_src_param src0_param;
3058 struct glsl_src_param src1_param;
3059 struct glsl_src_param src3_param;
3060 char dst_mask[6];
3062 shader_glsl_append_dst(ins->ctx->buffer, ins);
3063 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3065 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3066 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3067 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3069 /* The sdk specifies the instruction like this
3070 * dst.x = 1.0;
3071 * if(src.x > 0.0) dst.y = src.x
3072 * else dst.y = 0.0.
3073 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3074 * else dst.z = 0.0;
3075 * dst.w = 1.0;
3076 * (where power = src.w clamped between -128 and 128)
3078 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3079 * dst.x = 1.0 ... No further explanation needed
3080 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3081 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3082 * dst.w = 1.0. ... Nothing fancy.
3084 * So we still have one conditional in there. So do this:
3085 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3087 * 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),
3088 * 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.
3089 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3091 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3092 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3093 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3095 shader_addline(ins->ctx->buffer,
3096 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3097 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3098 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3099 src0_param.param_str, src3_param.param_str, dst_mask);
3102 /** Process the WINED3DSIO_DST instruction in GLSL:
3103 * dst.x = 1.0
3104 * dst.y = src0.x * src0.y
3105 * dst.z = src0.z
3106 * dst.w = src1.w
3108 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3110 struct glsl_src_param src0y_param;
3111 struct glsl_src_param src0z_param;
3112 struct glsl_src_param src1y_param;
3113 struct glsl_src_param src1w_param;
3114 char dst_mask[6];
3116 shader_glsl_append_dst(ins->ctx->buffer, ins);
3117 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3119 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3120 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3121 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3122 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3124 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3125 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3128 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3129 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3130 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3132 * dst.x = cos(src0.?)
3133 * dst.y = sin(src0.?)
3134 * dst.z = dst.z
3135 * dst.w = dst.w
3137 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3139 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3140 struct glsl_src_param src0_param;
3141 DWORD write_mask;
3143 if (ins->ctx->reg_maps->shader_version.major < 4)
3145 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3147 write_mask = shader_glsl_append_dst(buffer, ins);
3148 switch (write_mask)
3150 case WINED3DSP_WRITEMASK_0:
3151 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3152 break;
3154 case WINED3DSP_WRITEMASK_1:
3155 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3156 break;
3158 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3159 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3160 src0_param.param_str, src0_param.param_str);
3161 break;
3163 default:
3164 ERR("Write mask should be .x, .y or .xy\n");
3165 break;
3168 return;
3171 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3174 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3176 char dst_mask[6];
3178 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3179 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3180 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3182 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3183 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3184 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3186 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3187 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3189 else
3191 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3192 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3193 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3196 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3198 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3199 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3200 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3204 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3205 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3206 * generate invalid code
3208 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3210 struct glsl_src_param src0_param;
3211 DWORD write_mask;
3213 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3214 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3216 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3219 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3220 * Start a for() loop where src1.y is the initial value of aL,
3221 * increment aL by src1.z for a total of src1.x iterations.
3222 * Need to use a temporary variable for this operation.
3224 /* FIXME: I don't think nested loops will work correctly this way. */
3225 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3227 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3228 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3229 const struct wined3d_shader *shader = ins->ctx->shader;
3230 const struct wined3d_shader_lconst *constant;
3231 struct glsl_src_param src1_param;
3232 const DWORD *control_values = NULL;
3234 if (ins->ctx->reg_maps->shader_version.major < 4)
3236 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3238 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3239 * class hardware doesn't support real varying indexing, but Microsoft
3240 * designed this feature for Shader model 2.x+. If the loop control is
3241 * known at compile time, the GLSL compiler can unroll the loop, and
3242 * replace indirect addressing with direct addressing. */
3243 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3245 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3247 if (constant->idx == ins->src[1].reg.idx[0].offset)
3249 control_values = constant->value;
3250 break;
3255 if (control_values)
3257 struct wined3d_shader_loop_control loop_control;
3258 loop_control.count = control_values[0];
3259 loop_control.start = control_values[1];
3260 loop_control.step = (int)control_values[2];
3262 if (loop_control.step > 0)
3264 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3265 loop_state->current_depth, loop_control.start,
3266 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3267 loop_state->current_depth, loop_control.step);
3269 else if (loop_control.step < 0)
3271 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3272 loop_state->current_depth, loop_control.start,
3273 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3274 loop_state->current_depth, loop_control.step);
3276 else
3278 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3279 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3280 loop_state->current_depth, loop_control.count,
3281 loop_state->current_depth);
3284 else
3286 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3287 loop_state->current_depth, loop_state->current_reg,
3288 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3289 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3292 ++loop_state->current_reg;
3294 else
3296 shader_addline(buffer, "for (;;)\n{\n");
3299 ++loop_state->current_depth;
3302 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3304 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3306 shader_addline(ins->ctx->buffer, "}\n");
3308 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3310 --loop_state->current_depth;
3311 --loop_state->current_reg;
3314 if (ins->handler_idx == WINED3DSIH_ENDREP)
3316 --loop_state->current_depth;
3320 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3322 const struct wined3d_shader *shader = ins->ctx->shader;
3323 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3324 const struct wined3d_shader_lconst *constant;
3325 struct glsl_src_param src0_param;
3326 const DWORD *control_values = NULL;
3328 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3329 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3331 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3333 if (constant->idx == ins->src[0].reg.idx[0].offset)
3335 control_values = constant->value;
3336 break;
3341 if (control_values)
3343 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3344 loop_state->current_depth, loop_state->current_depth,
3345 control_values[0], loop_state->current_depth);
3347 else
3349 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3350 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3351 loop_state->current_depth, loop_state->current_depth,
3352 src0_param.param_str, loop_state->current_depth);
3355 ++loop_state->current_depth;
3358 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3360 struct glsl_src_param src0_param;
3362 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3363 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3366 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3368 struct glsl_src_param src0_param;
3369 struct glsl_src_param src1_param;
3371 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3372 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3374 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3375 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3378 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3380 shader_addline(ins->ctx->buffer, "} else {\n");
3383 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3385 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3388 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3390 shader_addline(ins->ctx->buffer, "break;\n");
3393 /* FIXME: According to MSDN the compare is done per component. */
3394 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3396 struct glsl_src_param src0_param;
3397 struct glsl_src_param src1_param;
3399 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3400 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3402 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3403 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3406 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3408 struct glsl_src_param src_param;
3410 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3411 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3414 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3416 shader_addline(ins->ctx->buffer, "}\n");
3417 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3420 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3422 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3425 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3427 struct glsl_src_param src1_param;
3429 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3430 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3431 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3434 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3436 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3437 * function only suppresses the unhandled instruction warning
3441 /*********************************************
3442 * Pixel Shader Specific Code begins here
3443 ********************************************/
3444 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3446 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3447 ins->ctx->reg_maps->shader_version.minor);
3448 struct glsl_sample_function sample_function;
3449 DWORD sample_flags = 0;
3450 DWORD sampler_idx;
3451 DWORD mask = 0, swizzle;
3452 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3454 /* 1.0-1.4: Use destination register as sampler source.
3455 * 2.0+: Use provided sampler source. */
3456 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3457 sampler_idx = ins->dst[0].reg.idx[0].offset;
3458 else
3459 sampler_idx = ins->src[1].reg.idx[0].offset;
3461 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3463 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3464 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3465 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3467 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3468 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3470 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3471 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3473 case WINED3D_TTFF_COUNT1:
3474 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3475 break;
3476 case WINED3D_TTFF_COUNT2:
3477 mask = WINED3DSP_WRITEMASK_1;
3478 break;
3479 case WINED3D_TTFF_COUNT3:
3480 mask = WINED3DSP_WRITEMASK_2;
3481 break;
3482 case WINED3D_TTFF_COUNT4:
3483 case WINED3D_TTFF_DISABLE:
3484 mask = WINED3DSP_WRITEMASK_3;
3485 break;
3489 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3491 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3493 if (src_mod == WINED3DSPSM_DZ) {
3494 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3495 mask = WINED3DSP_WRITEMASK_2;
3496 } else if (src_mod == WINED3DSPSM_DW) {
3497 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3498 mask = WINED3DSP_WRITEMASK_3;
3501 else
3503 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
3504 && ins->ctx->reg_maps->sampler_type[sampler_idx] != WINED3DSTT_CUBE)
3506 /* ps 2.0 texldp instruction always divides by the fourth component. */
3507 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3508 mask = WINED3DSP_WRITEMASK_3;
3512 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3513 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3515 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3516 mask |= sample_function.coord_mask;
3518 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3519 else swizzle = ins->src[1].swizzle;
3521 /* 1.0-1.3: Use destination register as coordinate source.
3522 1.4+: Use provided coordinate source register. */
3523 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3525 char coord_mask[6];
3526 shader_glsl_write_mask_to_str(mask, coord_mask);
3527 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3528 "T%u%s", sampler_idx, coord_mask);
3530 else
3532 struct glsl_src_param coord_param;
3533 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3534 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3536 struct glsl_src_param bias;
3537 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3538 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3539 "%s", coord_param.param_str);
3540 } else {
3541 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3542 "%s", coord_param.param_str);
3547 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3549 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3550 struct glsl_src_param coord_param, dx_param, dy_param;
3551 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3552 struct glsl_sample_function sample_function;
3553 DWORD sampler_idx;
3554 DWORD swizzle = ins->src[1].swizzle;
3555 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3557 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3559 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3560 shader_glsl_tex(ins);
3561 return;
3564 sampler_idx = ins->src[1].reg.idx[0].offset;
3565 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3566 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3568 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3569 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3570 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3571 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3573 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3574 "%s", coord_param.param_str);
3577 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3579 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3580 struct glsl_src_param coord_param, lod_param;
3581 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3582 struct glsl_sample_function sample_function;
3583 DWORD sampler_idx;
3584 DWORD swizzle = ins->src[1].swizzle;
3585 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3587 sampler_idx = ins->src[1].reg.idx[0].offset;
3588 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3589 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
3590 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
3592 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3593 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3595 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3597 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3598 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3600 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3601 * However, the NVIDIA drivers allow them in fragment shaders as well,
3602 * even without the appropriate extension. */
3603 WARN("Using %s in fragment shader.\n", sample_function.name);
3605 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3606 "%s", coord_param.param_str);
3609 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3611 /* FIXME: Make this work for more than just 2D textures */
3612 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3613 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3615 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3617 char dst_mask[6];
3619 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3620 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3621 ins->dst[0].reg.idx[0].offset, dst_mask);
3623 else
3625 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3626 DWORD reg = ins->src[0].reg.idx[0].offset;
3627 char dst_swizzle[6];
3629 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3631 if (src_mod == WINED3DSPSM_DZ)
3633 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3634 struct glsl_src_param div_param;
3636 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3638 if (mask_size > 1) {
3639 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3640 } else {
3641 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3644 else if (src_mod == WINED3DSPSM_DW)
3646 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3647 struct glsl_src_param div_param;
3649 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3651 if (mask_size > 1) {
3652 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3653 } else {
3654 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3656 } else {
3657 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3662 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3663 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3664 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3665 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3667 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3668 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3669 struct glsl_sample_function sample_function;
3670 struct glsl_src_param src0_param;
3671 UINT mask_size;
3673 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3675 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3676 * scalar, and projected sampling would require 4.
3678 * It is a dependent read - not valid with conditional NP2 textures
3680 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3681 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3683 switch(mask_size)
3685 case 1:
3686 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3687 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3688 break;
3690 case 2:
3691 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3692 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3693 break;
3695 case 3:
3696 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3697 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3698 break;
3700 default:
3701 FIXME("Unexpected mask size %u\n", mask_size);
3702 break;
3706 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3707 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3708 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3710 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3711 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3712 struct glsl_src_param src0_param;
3713 DWORD dst_mask;
3714 unsigned int mask_size;
3716 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3717 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3718 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3720 if (mask_size > 1) {
3721 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3722 } else {
3723 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3727 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3728 * Calculate the depth as dst.x / dst.y */
3729 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3731 struct glsl_dst_param dst_param;
3733 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3735 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3736 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3737 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3738 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3739 * >= 1.0 or < 0.0
3741 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3742 dst_param.reg_name, dst_param.reg_name);
3745 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3746 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3747 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3748 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3750 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3752 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3753 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3754 struct glsl_src_param src0_param;
3756 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3758 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3759 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3762 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3763 * Calculate the 1st of a 2-row matrix multiplication. */
3764 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3766 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3767 DWORD reg = ins->dst[0].reg.idx[0].offset;
3768 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3769 struct glsl_src_param src0_param;
3771 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3772 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3775 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3776 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3777 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3779 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3780 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3781 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3782 DWORD reg = ins->dst[0].reg.idx[0].offset;
3783 struct glsl_src_param src0_param;
3785 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3786 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3787 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3790 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3792 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3793 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3794 struct glsl_sample_function sample_function;
3795 DWORD reg = ins->dst[0].reg.idx[0].offset;
3796 struct glsl_src_param src0_param;
3798 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3799 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3801 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3803 /* Sample the texture using the calculated coordinates */
3804 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3807 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3808 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3809 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3811 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3812 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3813 struct glsl_sample_function sample_function;
3814 DWORD reg = ins->dst[0].reg.idx[0].offset;
3815 struct glsl_src_param src0_param;
3817 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3818 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3820 /* Dependent read, not valid with conditional NP2 */
3821 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3823 /* Sample the texture using the calculated coordinates */
3824 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3826 tex_mx->current_row = 0;
3829 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3830 * Perform the 3rd row of a 3x3 matrix multiply */
3831 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3833 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3834 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3835 DWORD reg = ins->dst[0].reg.idx[0].offset;
3836 struct glsl_src_param src0_param;
3837 char dst_mask[6];
3839 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3841 shader_glsl_append_dst(ins->ctx->buffer, ins);
3842 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3843 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3845 tex_mx->current_row = 0;
3848 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3849 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3850 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3852 struct glsl_src_param src0_param;
3853 struct glsl_src_param src1_param;
3854 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3855 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3856 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3857 struct glsl_sample_function sample_function;
3858 DWORD reg = ins->dst[0].reg.idx[0].offset;
3859 char coord_mask[6];
3861 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3862 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3864 /* Perform the last matrix multiply operation */
3865 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3866 /* Reflection calculation */
3867 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3869 /* Dependent read, not valid with conditional NP2 */
3870 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3871 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3873 /* Sample the texture */
3874 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3875 NULL, NULL, NULL, "tmp0%s", coord_mask);
3877 tex_mx->current_row = 0;
3880 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3881 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3882 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3884 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3885 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3886 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3887 struct glsl_sample_function sample_function;
3888 DWORD reg = ins->dst[0].reg.idx[0].offset;
3889 struct glsl_src_param src0_param;
3890 char coord_mask[6];
3892 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3894 /* Perform the last matrix multiply operation */
3895 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3897 /* Construct the eye-ray vector from w coordinates */
3898 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3899 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3900 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3902 /* Dependent read, not valid with conditional NP2 */
3903 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3904 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3906 /* Sample the texture using the calculated coordinates */
3907 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3908 NULL, NULL, NULL, "tmp0%s", coord_mask);
3910 tex_mx->current_row = 0;
3913 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3914 * Apply a fake bump map transform.
3915 * texbem is pshader <= 1.3 only, this saves a few version checks
3917 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3919 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3920 struct glsl_sample_function sample_function;
3921 struct glsl_src_param coord_param;
3922 DWORD sampler_idx;
3923 DWORD mask;
3924 DWORD flags;
3925 char coord_mask[6];
3927 sampler_idx = ins->dst[0].reg.idx[0].offset;
3928 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3929 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3931 /* Dependent read, not valid with conditional NP2 */
3932 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3933 mask = sample_function.coord_mask;
3935 shader_glsl_write_mask_to_str(mask, coord_mask);
3937 /* With projected textures, texbem only divides the static texture coord,
3938 * not the displacement, so we can't let GL handle this. */
3939 if (flags & WINED3D_PSARGS_PROJECTED)
3941 DWORD div_mask=0;
3942 char coord_div_mask[3];
3943 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3945 case WINED3D_TTFF_COUNT1:
3946 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3947 break;
3948 case WINED3D_TTFF_COUNT2:
3949 div_mask = WINED3DSP_WRITEMASK_1;
3950 break;
3951 case WINED3D_TTFF_COUNT3:
3952 div_mask = WINED3DSP_WRITEMASK_2;
3953 break;
3954 case WINED3D_TTFF_COUNT4:
3955 case WINED3D_TTFF_DISABLE:
3956 div_mask = WINED3DSP_WRITEMASK_3;
3957 break;
3959 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3960 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3963 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3965 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3966 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3967 coord_param.param_str, coord_mask);
3969 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3971 struct glsl_src_param luminance_param;
3972 struct glsl_dst_param dst_param;
3974 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3975 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3977 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
3978 dst_param.reg_name, dst_param.mask_str,
3979 luminance_param.param_str, sampler_idx, sampler_idx);
3983 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3985 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3986 struct glsl_src_param src0_param, src1_param;
3988 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3989 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3991 shader_glsl_append_dst(ins->ctx->buffer, ins);
3992 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
3993 src0_param.param_str, sampler_idx, src1_param.param_str);
3996 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3997 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3998 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4000 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4001 struct glsl_sample_function sample_function;
4002 struct glsl_src_param src0_param;
4004 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4006 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4007 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4008 "%s.wx", src0_param.reg_name);
4011 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4012 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4013 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4015 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4016 struct glsl_sample_function sample_function;
4017 struct glsl_src_param src0_param;
4019 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4021 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4022 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4023 "%s.yz", src0_param.reg_name);
4026 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4027 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4028 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4030 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4031 struct glsl_sample_function sample_function;
4032 struct glsl_src_param src0_param;
4034 /* Dependent read, not valid with conditional NP2 */
4035 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4036 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4038 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4039 "%s", src0_param.param_str);
4042 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4043 * If any of the first 3 components are < 0, discard this pixel */
4044 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4046 struct glsl_dst_param dst_param;
4048 /* The argument is a destination parameter, and no writemasks are allowed */
4049 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4050 if (ins->ctx->reg_maps->shader_version.major >= 2)
4052 if (ins->ctx->reg_maps->shader_version.major >= 4)
4053 FIXME("SM4 discard not implemented.\n");
4054 /* 2.0 shaders compare all 4 components in texkill */
4055 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4056 } else {
4057 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4058 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4059 * 4 components are defined, only the first 3 are used
4061 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4065 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4066 * dst = dot2(src0, src1) + src2 */
4067 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4069 struct glsl_src_param src0_param;
4070 struct glsl_src_param src1_param;
4071 struct glsl_src_param src2_param;
4072 DWORD write_mask;
4073 unsigned int mask_size;
4075 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4076 mask_size = shader_glsl_get_write_mask_size(write_mask);
4078 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4079 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4080 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4082 if (mask_size > 1) {
4083 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4084 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4085 } else {
4086 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4087 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4091 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4092 const struct wined3d_shader_signature_element *input_signature,
4093 const struct wined3d_shader_reg_maps *reg_maps,
4094 enum vertexprocessing_mode vertexprocessing)
4096 WORD map = reg_maps->input_registers;
4097 unsigned int i;
4099 for (i = 0; map; map >>= 1, ++i)
4101 const char *semantic_name;
4102 UINT semantic_idx;
4103 char reg_mask[6];
4105 /* Unused */
4106 if (!(map & 1)) continue;
4108 semantic_name = input_signature[i].semantic_name;
4109 semantic_idx = input_signature[i].semantic_idx;
4110 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4112 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4114 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4115 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4116 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4117 else
4118 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4119 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4121 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4123 if (!semantic_idx)
4124 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4125 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4126 else if (semantic_idx == 1)
4127 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4128 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4129 else
4130 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4131 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4133 else
4135 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4136 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4141 /*********************************************
4142 * Vertex Shader Specific Code begins here
4143 ********************************************/
4145 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4147 struct glsl_program_key key;
4149 key.vs_id = entry->vs.id;
4150 key.gs_id = entry->gs.id;
4151 key.ps_id = entry->ps.id;
4153 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4155 ERR("Failed to insert program entry.\n");
4159 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4160 GLhandleARB vs_id, GLhandleARB gs_id, GLhandleARB ps_id)
4162 struct wine_rb_entry *entry;
4163 struct glsl_program_key key;
4165 key.vs_id = vs_id;
4166 key.gs_id = gs_id;
4167 key.ps_id = ps_id;
4169 entry = wine_rb_get(&priv->program_lookup, &key);
4170 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4173 /* Context activation is done by the caller. */
4174 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4175 struct glsl_shader_prog_link *entry)
4177 struct glsl_program_key key;
4179 key.vs_id = entry->vs.id;
4180 key.gs_id = entry->gs.id;
4181 key.ps_id = entry->ps.id;
4182 wine_rb_remove(&priv->program_lookup, &key);
4184 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4185 if (entry->vs.id)
4186 list_remove(&entry->vs.shader_entry);
4187 if (entry->gs.id)
4188 list_remove(&entry->gs.shader_entry);
4189 if (entry->ps.id)
4190 list_remove(&entry->ps.shader_entry);
4191 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4192 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4193 HeapFree(GetProcessHeap(), 0, entry);
4196 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4197 const struct wined3d_gl_info *gl_info, const DWORD *map,
4198 const struct wined3d_shader_signature_element *input_signature,
4199 const struct wined3d_shader_reg_maps *reg_maps_in,
4200 const struct wined3d_shader_signature_element *output_signature,
4201 const struct wined3d_shader_reg_maps *reg_maps_out)
4203 unsigned int i, j;
4204 const char *semantic_name_in;
4205 UINT semantic_idx_in;
4206 DWORD *set;
4207 DWORD in_idx;
4208 unsigned int in_count = vec4_varyings(3, gl_info);
4209 char reg_mask[6];
4210 char destination[50];
4211 WORD input_map, output_map;
4213 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4215 input_map = reg_maps_in->input_registers;
4216 for (i = 0; input_map; input_map >>= 1, ++i)
4218 if (!(input_map & 1)) continue;
4220 in_idx = map[i];
4221 /* Declared, but not read register */
4222 if (in_idx == ~0U) continue;
4223 if (in_idx >= (in_count + 2))
4225 FIXME("More input varyings declared than supported, expect issues.\n");
4226 continue;
4229 if (in_idx == in_count)
4230 sprintf(destination, "gl_FrontColor");
4231 else if (in_idx == in_count + 1)
4232 sprintf(destination, "gl_FrontSecondaryColor");
4233 else
4234 sprintf(destination, "ps_in[%u]", in_idx);
4236 semantic_name_in = input_signature[i].semantic_name;
4237 semantic_idx_in = input_signature[i].semantic_idx;
4238 set[in_idx] = ~0U;
4240 output_map = reg_maps_out->output_registers;
4241 for (j = 0; output_map; output_map >>= 1, ++j)
4243 DWORD mask;
4245 if (!(output_map & 1)
4246 || semantic_idx_in != output_signature[j].semantic_idx
4247 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4248 || !(mask = input_signature[i].mask & output_signature[j].mask))
4249 continue;
4251 set[in_idx] = mask;
4252 shader_glsl_write_mask_to_str(mask, reg_mask);
4254 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4255 destination, reg_mask, j, reg_mask);
4259 for (i = 0; i < in_count + 2; ++i)
4261 unsigned int size;
4263 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4264 continue;
4266 if (set[i] == ~0U) set[i] = 0;
4268 size = 0;
4269 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4270 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4271 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4272 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4273 reg_mask[size] = '\0';
4275 if (i == in_count)
4276 sprintf(destination, "gl_FrontColor");
4277 else if (i == in_count + 1)
4278 sprintf(destination, "gl_FrontSecondaryColor");
4279 else
4280 sprintf(destination, "ps_in[%u]", i);
4282 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4283 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4286 HeapFree(GetProcessHeap(), 0, set);
4289 /* Context activation is done by the caller. */
4290 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4291 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4292 const struct wined3d_gl_info *gl_info)
4294 GLhandleARB ret = 0;
4295 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4296 unsigned int i;
4297 const char *semantic_name;
4298 UINT semantic_idx;
4299 char reg_mask[6];
4300 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4301 WORD map = vs->reg_maps.output_registers;
4303 shader_buffer_clear(buffer);
4305 shader_addline(buffer, "#version 120\n");
4307 if (ps_major < 3)
4309 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4311 for (i = 0; map; map >>= 1, ++i)
4313 DWORD write_mask;
4315 if (!(map & 1)) continue;
4317 semantic_name = output_signature[i].semantic_name;
4318 semantic_idx = output_signature[i].semantic_idx;
4319 write_mask = output_signature[i].mask;
4320 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4322 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4324 if (!semantic_idx)
4325 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4326 reg_mask, i, reg_mask);
4327 else if (semantic_idx == 1)
4328 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4329 reg_mask, i, reg_mask);
4331 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4333 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4334 reg_mask, i, reg_mask);
4336 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4338 if (semantic_idx < 8)
4340 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4341 write_mask |= WINED3DSP_WRITEMASK_3;
4343 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4344 semantic_idx, reg_mask, i, reg_mask);
4345 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4346 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4349 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4351 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4353 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4355 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4358 shader_addline(buffer, "}\n");
4360 else
4362 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4363 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4364 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4365 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4367 /* First, sort out position and point size. Those are not passed to the pixel shader */
4368 for (i = 0; map; map >>= 1, ++i)
4370 if (!(map & 1)) continue;
4372 semantic_name = output_signature[i].semantic_name;
4373 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4375 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4377 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4378 reg_mask, i, reg_mask);
4380 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4382 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4386 /* Then, fix the pixel shader input */
4387 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4388 &ps->reg_maps, output_signature, &vs->reg_maps);
4390 shader_addline(buffer, "}\n");
4393 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4394 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4395 shader_glsl_compile(gl_info, ret, buffer->buffer);
4397 return ret;
4400 static void shader_glsl_generate_srgb_write_correction(struct wined3d_shader_buffer *buffer)
4402 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4403 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4404 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4405 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4406 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4407 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4410 static void shader_glsl_generate_fog_code(struct wined3d_shader_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
4412 switch (mode)
4414 case WINED3D_FFP_PS_FOG_OFF:
4415 return;
4417 case WINED3D_FFP_PS_FOG_LINEAR:
4418 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
4419 break;
4421 case WINED3D_FFP_PS_FOG_EXP:
4422 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
4423 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4424 break;
4426 case WINED3D_FFP_PS_FOG_EXP2:
4427 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
4428 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4429 break;
4431 default:
4432 ERR("Invalid fog mode %#x.\n", mode);
4433 return;
4436 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
4439 /* Context activation is done by the caller. */
4440 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4441 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4442 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4444 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4445 const struct wined3d_gl_info *gl_info = context->gl_info;
4446 const DWORD *function = shader->function;
4447 struct shader_glsl_ctx_priv priv_ctx;
4449 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4450 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4452 memset(&priv_ctx, 0, sizeof(priv_ctx));
4453 priv_ctx.cur_ps_args = args;
4454 priv_ctx.cur_np2fixup_info = np2fixup_info;
4456 shader_addline(buffer, "#version 120\n");
4458 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4459 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4460 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4461 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4462 /* The spec says that it doesn't have to be explicitly enabled, but the
4463 * nvidia drivers write a warning if we don't do so. */
4464 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4465 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4466 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4467 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4468 if (gl_info->supported[EXT_GPU_SHADER4])
4469 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4471 /* Base Declarations */
4472 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4474 /* Pack 3.0 inputs */
4475 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4476 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4478 /* Base Shader Body */
4479 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4481 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4482 if (reg_maps->shader_version.major < 2)
4484 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4485 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4488 if (args->srgb_correction)
4489 shader_glsl_generate_srgb_write_correction(buffer);
4491 /* SM < 3 does not replace the fog stage. */
4492 if (reg_maps->shader_version.major < 3)
4493 shader_glsl_generate_fog_code(buffer, args->fog);
4495 shader_addline(buffer, "}\n");
4497 TRACE("Compiling shader object %u\n", shader_obj);
4498 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4500 /* Store the shader object */
4501 return shader_obj;
4504 /* Context activation is done by the caller. */
4505 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4506 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4507 const struct vs_compile_args *args)
4509 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4510 const struct wined3d_gl_info *gl_info = context->gl_info;
4511 const DWORD *function = shader->function;
4512 struct shader_glsl_ctx_priv priv_ctx;
4514 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4515 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4517 shader_addline(buffer, "#version 120\n");
4519 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4520 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4521 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4522 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4523 if (gl_info->supported[EXT_GPU_SHADER4])
4524 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4526 memset(&priv_ctx, 0, sizeof(priv_ctx));
4527 priv_ctx.cur_vs_args = args;
4529 /* Base Declarations */
4530 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4532 /* Base Shader Body */
4533 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4535 /* Unpack outputs */
4536 shader_addline(buffer, "order_ps_input(vs_out);\n");
4538 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4539 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4540 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4541 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4543 if (args->fog_src == VS_FOG_Z)
4544 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4545 else if (!reg_maps->fog)
4546 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4548 /* We always store the clipplanes without y inversion */
4549 if (args->clip_enabled)
4550 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4552 /* Write the final position.
4554 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4555 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4556 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4557 * contains 1.0 to allow a mad.
4559 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4560 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4562 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4564 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4565 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4566 * which is the same as z = z * 2 - w.
4568 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4570 shader_addline(buffer, "}\n");
4572 TRACE("Compiling shader object %u\n", shader_obj);
4573 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4575 return shader_obj;
4578 /* Context activation is done by the caller. */
4579 static GLhandleARB shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
4580 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader)
4582 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4583 const struct wined3d_gl_info *gl_info = context->gl_info;
4584 const DWORD *function = shader->function;
4585 struct shader_glsl_ctx_priv priv_ctx;
4586 GLhandleARB shader_id;
4588 shader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_GEOMETRY_SHADER_ARB));
4590 shader_addline(buffer, "#version 120\n");
4592 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
4593 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
4594 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4595 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4596 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
4597 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
4598 if (gl_info->supported[EXT_GPU_SHADER4])
4599 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4601 memset(&priv_ctx, 0, sizeof(priv_ctx));
4602 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4603 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4604 shader_addline(buffer, "}\n");
4606 TRACE("Compiling shader object %u.\n", shader_id);
4607 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
4609 return shader_id;
4612 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4613 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4614 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4616 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4617 struct glsl_shader_private *shader_data;
4618 struct ps_np2fixup_info *np2fixup;
4619 UINT i;
4620 DWORD new_size;
4621 GLhandleARB ret;
4623 if (!shader->backend_data)
4625 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4626 if (!shader->backend_data)
4628 ERR("Failed to allocate backend data.\n");
4629 return 0;
4632 shader_data = shader->backend_data;
4633 gl_shaders = shader_data->gl_shaders.ps;
4635 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4636 * so a linear search is more performant than a hashmap or a binary search
4637 * (cache coherency etc)
4639 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4641 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4643 if (args->np2_fixup)
4644 *np2fixup_info = &gl_shaders[i].np2fixup;
4645 return gl_shaders[i].prgId;
4649 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4650 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4651 if (shader_data->num_gl_shaders)
4653 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4654 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4655 new_size * sizeof(*gl_shaders));
4657 else
4659 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4660 new_size = 1;
4663 if(!new_array) {
4664 ERR("Out of memory\n");
4665 return 0;
4667 shader_data->gl_shaders.ps = new_array;
4668 shader_data->shader_array_size = new_size;
4669 gl_shaders = new_array;
4672 gl_shaders[shader_data->num_gl_shaders].args = *args;
4674 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4675 memset(np2fixup, 0, sizeof(*np2fixup));
4676 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4678 pixelshader_update_samplers(shader, args->tex_types);
4680 shader_buffer_clear(buffer);
4681 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4682 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4684 return ret;
4687 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4688 const DWORD use_map) {
4689 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4690 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4691 return stored->fog_src == new->fog_src;
4694 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4695 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4696 const struct vs_compile_args *args)
4698 UINT i;
4699 DWORD new_size;
4700 DWORD use_map = context->stream_info.use_map;
4701 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4702 struct glsl_shader_private *shader_data;
4703 GLhandleARB ret;
4705 if (!shader->backend_data)
4707 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4708 if (!shader->backend_data)
4710 ERR("Failed to allocate backend data.\n");
4711 return 0;
4714 shader_data = shader->backend_data;
4715 gl_shaders = shader_data->gl_shaders.vs;
4717 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4718 * so a linear search is more performant than a hashmap or a binary search
4719 * (cache coherency etc)
4721 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4723 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4724 return gl_shaders[i].prgId;
4727 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4729 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4730 if (shader_data->num_gl_shaders)
4732 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4733 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4734 new_size * sizeof(*gl_shaders));
4736 else
4738 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4739 new_size = 1;
4742 if(!new_array) {
4743 ERR("Out of memory\n");
4744 return 0;
4746 shader_data->gl_shaders.vs = new_array;
4747 shader_data->shader_array_size = new_size;
4748 gl_shaders = new_array;
4751 gl_shaders[shader_data->num_gl_shaders].args = *args;
4753 shader_buffer_clear(buffer);
4754 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4755 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4757 return ret;
4760 static GLhandleARB find_glsl_geometry_shader(const struct wined3d_context *context,
4761 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader)
4763 struct glsl_gs_compiled_shader *gl_shaders;
4764 struct glsl_shader_private *shader_data;
4765 GLhandleARB ret;
4767 if (!shader->backend_data)
4769 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
4771 ERR("Failed to allocate backend data.\n");
4772 return 0;
4775 shader_data = shader->backend_data;
4776 gl_shaders = shader_data->gl_shaders.gs;
4778 if (shader_data->num_gl_shaders)
4779 return gl_shaders[0].id;
4781 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4783 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
4785 ERR("Failed to allocate GL shader array.\n");
4786 return 0;
4788 shader_data->shader_array_size = 1;
4789 gl_shaders = shader_data->gl_shaders.gs;
4791 shader_buffer_clear(buffer);
4792 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
4793 gl_shaders[shader_data->num_gl_shaders++].id = ret;
4795 return ret;
4798 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
4800 switch (mcs)
4802 case WINED3D_MCS_MATERIAL:
4803 return material;
4804 case WINED3D_MCS_COLOR1:
4805 return "gl_Color";
4806 case WINED3D_MCS_COLOR2:
4807 return "gl_SecondaryColor";
4808 default:
4809 ERR("Invalid material color source %#x.\n", mcs);
4810 return "<invalid>";
4814 static void shader_glsl_ffp_vertex_lighting(struct wined3d_shader_buffer *buffer,
4815 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4817 const char *diffuse, *specular, *emission, *ambient;
4818 enum wined3d_light_type light_type;
4819 unsigned int i;
4821 if (!settings->lighting)
4823 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
4824 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
4825 return;
4828 shader_addline(buffer, "vec3 ambient = gl_LightModel.ambient.xyz;\n");
4829 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
4830 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
4831 shader_addline(buffer, "vec3 dir, dst;\n");
4832 shader_addline(buffer, "float att, t;\n");
4834 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "gl_FrontMaterial.ambient");
4835 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "gl_FrontMaterial.diffuse");
4836 specular = shader_glsl_ffp_mcs(settings->specular_source, "gl_FrontMaterial.specular");
4837 emission = shader_glsl_ffp_mcs(settings->emission_source, "gl_FrontMaterial.emission");
4839 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
4841 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
4842 switch (light_type)
4844 case WINED3D_LIGHT_POINT:
4845 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4846 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4847 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4848 shader_addline(buffer, "dst.x = 1.0;\n");
4849 shader_addline(buffer, "att = dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4850 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n", i, i, i);
4851 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz / att;\n", i);
4852 if (!settings->normal)
4853 break;
4854 shader_addline(buffer, "dir = normalize(dir);\n");
4855 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4856 " * gl_LightSource[%u].diffuse.xyz) / att;\n", i);
4857 if (settings->localviewer)
4858 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4859 else
4860 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4861 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4862 " * gl_LightSource[%u].specular) / att;\n", i);
4863 break;
4865 case WINED3D_LIGHT_SPOT:
4866 shader_addline(buffer, "dir = gl_LightSource[%u].position.xyz - ec_pos.xyz;\n", i);
4867 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
4868 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
4869 shader_addline(buffer, "dst.x = 1.0;\n");
4870 shader_addline(buffer, "dir = normalize(dir);\n");
4871 shader_addline(buffer, "t = dot(-dir, normalize(gl_LightSource[%u].spotDirection));\n", i);
4872 shader_addline(buffer, "if (t < gl_LightSource[%u].spotCosCutoff) att = 0.0;\n", i);
4873 shader_addline(buffer, "else att = pow(t, gl_LightSource[%u].spotExponent)"
4874 " / dot(dst.xyz, vec3(gl_LightSource[%u].constantAttenuation,"
4875 " gl_LightSource[%u].linearAttenuation, gl_LightSource[%u].quadraticAttenuation));\n",
4876 i, i, i, i);
4877 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz * att;\n", i);
4878 if (!settings->normal)
4879 break;
4880 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
4881 " * gl_LightSource[%u].diffuse.xyz) * att;\n", i);
4882 if (settings->localviewer)
4883 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
4884 else
4885 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, 1.0)));\n");
4886 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, gl_FrontMaterial.shininess)"
4887 " * gl_LightSource[%u].specular) * att;\n", i);
4888 break;
4890 case WINED3D_LIGHT_DIRECTIONAL:
4891 shader_addline(buffer, "ambient += gl_LightSource[%u].ambient.xyz;\n", i);
4892 if (!settings->normal)
4893 break;
4894 shader_addline(buffer, "dir = normalize(gl_LightSource[%u].position.xyz);\n", i);
4895 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
4896 " * gl_LightSource[%u].diffuse.xyz;\n", i);
4897 shader_addline(buffer, "t = dot(normal, gl_LightSource[%u].halfVector.xyz);\n", i);
4898 shader_addline(buffer, "if (t > 0.0) specular += pow(t, gl_FrontMaterial.shininess)"
4899 " * gl_LightSource[%u].specular;\n", i);
4900 break;
4902 default:
4903 if (light_type)
4904 FIXME("Unhandled light type %#x.\n", light_type);
4905 continue;
4909 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
4910 ambient, diffuse, emission);
4911 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
4912 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
4915 /* Context activation is done by the caller. */
4916 static GLhandleARB shader_glsl_generate_ffp_vertex_shader(struct wined3d_shader_buffer *buffer,
4917 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
4919 GLhandleARB shader_obj;
4920 unsigned int i;
4922 shader_buffer_clear(buffer);
4924 shader_addline(buffer, "#version 120\n");
4925 shader_addline(buffer, "\n");
4926 shader_addline(buffer, "void main()\n{\n");
4927 shader_addline(buffer, "float m;\n");
4928 shader_addline(buffer, "vec3 r;\n");
4930 if (settings->transformed)
4932 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
4933 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4934 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
4936 else
4938 shader_addline(buffer, "vec4 ec_pos = gl_ModelViewMatrix * gl_Vertex;\n");
4939 shader_addline(buffer, "gl_Position = gl_ProjectionMatrix * ec_pos;\n");
4940 if (settings->clipping)
4941 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
4942 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
4945 if (!settings->normal)
4946 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
4947 else if (settings->normalize)
4948 shader_addline(buffer, "vec3 normal = normalize(gl_NormalMatrix * gl_Normal);\n");
4949 else
4950 shader_addline(buffer, "vec3 normal = gl_NormalMatrix * gl_Normal;\n");
4952 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
4954 for (i = 0; i < MAX_TEXTURES; ++i)
4956 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
4958 case WINED3DTSS_TCI_PASSTHRU:
4959 if (settings->texcoords & (1 << i))
4960 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * gl_MultiTexCoord%d;\n",
4961 i, i, i);
4962 break;
4964 case WINED3DTSS_TCI_CAMERASPACENORMAL:
4965 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * vec4(normal, 1.0);\n", i, i);
4966 break;
4968 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
4969 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u] * ec_pos;\n", i, i);
4970 break;
4972 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
4973 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4974 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
4975 break;
4977 case WINED3DTSS_TCI_SPHEREMAP:
4978 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
4979 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
4980 shader_addline(buffer, "gl_TexCoord[%u] = gl_TextureMatrix[%u]"
4981 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
4982 break;
4984 default:
4985 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
4986 break;
4990 switch (settings->fog_mode)
4992 case WINED3D_FFP_VS_FOG_OFF:
4993 break;
4995 case WINED3D_FFP_VS_FOG_FOGCOORD:
4996 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
4997 break;
4999 case WINED3D_FFP_VS_FOG_RANGE:
5000 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5001 break;
5003 case WINED3D_FFP_VS_FOG_DEPTH:
5004 if (settings->ortho_fog)
5005 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5006 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5007 else
5008 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5009 break;
5011 default:
5012 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5013 break;
5016 if (settings->point_size)
5018 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5019 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5020 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5021 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5024 shader_addline(buffer, "}\n");
5026 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
5027 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5029 return shader_obj;
5032 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_shader_buffer *buffer,
5033 DWORD argnum, unsigned int stage, DWORD arg)
5035 const char *ret;
5037 if (arg == ARG_UNUSED)
5038 return "<unused arg>";
5040 switch (arg & WINED3DTA_SELECTMASK)
5042 case WINED3DTA_DIFFUSE:
5043 ret = "gl_Color";
5044 break;
5046 case WINED3DTA_CURRENT:
5047 if (!stage)
5048 ret = "gl_Color";
5049 else
5050 ret = "ret";
5051 break;
5053 case WINED3DTA_TEXTURE:
5054 switch (stage)
5056 case 0: ret = "tex0"; break;
5057 case 1: ret = "tex1"; break;
5058 case 2: ret = "tex2"; break;
5059 case 3: ret = "tex3"; break;
5060 case 4: ret = "tex4"; break;
5061 case 5: ret = "tex5"; break;
5062 case 6: ret = "tex6"; break;
5063 case 7: ret = "tex7"; break;
5064 default:
5065 ret = "<invalid texture>";
5066 break;
5068 break;
5070 case WINED3DTA_TFACTOR:
5071 ret = "tex_factor";
5072 break;
5074 case WINED3DTA_SPECULAR:
5075 ret = "gl_SecondaryColor";
5076 break;
5078 case WINED3DTA_TEMP:
5079 ret = "temp_reg";
5080 break;
5082 case WINED3DTA_CONSTANT:
5083 switch (stage)
5085 case 0: ret = "tss_const0"; break;
5086 case 1: ret = "tss_const1"; break;
5087 case 2: ret = "tss_const2"; break;
5088 case 3: ret = "tss_const3"; break;
5089 case 4: ret = "tss_const4"; break;
5090 case 5: ret = "tss_const5"; break;
5091 case 6: ret = "tss_const6"; break;
5092 case 7: ret = "tss_const7"; break;
5093 default:
5094 ret = "<invalid constant>";
5095 break;
5097 break;
5099 default:
5100 return "<unhandled arg>";
5103 if (arg & WINED3DTA_COMPLEMENT)
5105 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5106 if (argnum == 0)
5107 ret = "arg0";
5108 else if (argnum == 1)
5109 ret = "arg1";
5110 else if (argnum == 2)
5111 ret = "arg2";
5114 if (arg & WINED3DTA_ALPHAREPLICATE)
5116 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5117 if (argnum == 0)
5118 ret = "arg0";
5119 else if (argnum == 1)
5120 ret = "arg1";
5121 else if (argnum == 2)
5122 ret = "arg2";
5125 return ret;
5128 static void shader_glsl_ffp_fragment_op(struct wined3d_shader_buffer *buffer, unsigned int stage, BOOL color,
5129 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5131 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5133 if (color && alpha)
5134 dstmask = "";
5135 else if (color)
5136 dstmask = ".xyz";
5137 else
5138 dstmask = ".w";
5140 if (dst == tempreg)
5141 dstreg = "temp_reg";
5142 else
5143 dstreg = "ret";
5145 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5146 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5147 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5149 switch (op)
5151 case WINED3D_TOP_DISABLE:
5152 if (!stage)
5153 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5154 break;
5156 case WINED3D_TOP_SELECT_ARG1:
5157 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5158 break;
5160 case WINED3D_TOP_SELECT_ARG2:
5161 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5162 break;
5164 case WINED3D_TOP_MODULATE:
5165 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5166 break;
5168 case WINED3D_TOP_MODULATE_4X:
5169 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5170 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5171 break;
5173 case WINED3D_TOP_MODULATE_2X:
5174 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5175 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5176 break;
5178 case WINED3D_TOP_ADD:
5179 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5180 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5181 break;
5183 case WINED3D_TOP_ADD_SIGNED:
5184 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5185 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5186 break;
5188 case WINED3D_TOP_ADD_SIGNED_2X:
5189 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5190 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5191 break;
5193 case WINED3D_TOP_SUBTRACT:
5194 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5195 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5196 break;
5198 case WINED3D_TOP_ADD_SMOOTH:
5199 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5200 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5201 break;
5203 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5204 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5205 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5206 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5207 break;
5209 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5210 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5211 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5212 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5213 break;
5215 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5216 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
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_PM:
5222 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5223 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5224 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5225 break;
5227 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5228 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
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_MODULATE_ALPHA_ADD_COLOR:
5234 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5235 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5236 break;
5238 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5239 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5240 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5241 break;
5243 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5244 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5245 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5246 break;
5247 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5248 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5249 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5250 break;
5252 case WINED3D_TOP_BUMPENVMAP:
5253 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5254 /* These are handled in the first pass, nothing to do. */
5255 break;
5257 case WINED3D_TOP_DOTPRODUCT3:
5258 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5259 dstreg, dstmask, arg1, arg2, dstmask);
5260 break;
5262 case WINED3D_TOP_MULTIPLY_ADD:
5263 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5264 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5265 break;
5267 case WINED3D_TOP_LERP:
5268 /* MSDN isn't quite right here. */
5269 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5270 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5271 break;
5273 default:
5274 FIXME("Unhandled operation %#x.\n", op);
5275 break;
5279 /* Context activation is done by the caller. */
5280 static GLuint shader_glsl_generate_ffp_fragment_shader(struct wined3d_shader_buffer *buffer,
5281 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5283 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5284 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5285 const char *final_combiner_src = "ret";
5286 UINT lowest_disabled_stage;
5287 GLhandleARB shader_obj;
5288 DWORD arg0, arg1, arg2;
5289 unsigned int stage;
5291 shader_buffer_clear(buffer);
5293 /* Find out which textures are read */
5294 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5296 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5297 break;
5299 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5300 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5301 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5303 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5304 tex_map |= 1 << stage;
5305 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5306 tfactor_used = TRUE;
5307 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5308 tempreg_used = TRUE;
5309 if (settings->op[stage].dst == tempreg)
5310 tempreg_used = TRUE;
5311 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5312 tss_const_map |= 1 << stage;
5314 switch (settings->op[stage].cop)
5316 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5317 lum_map |= 1 << stage;
5318 /* fall through */
5319 case WINED3D_TOP_BUMPENVMAP:
5320 bump_map |= 1 << stage;
5321 /* fall through */
5322 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5323 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5324 tex_map |= 1 << stage;
5325 break;
5327 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5328 tfactor_used = TRUE;
5329 break;
5331 default:
5332 break;
5335 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5336 continue;
5338 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5339 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5340 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5342 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5343 tex_map |= 1 << stage;
5344 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5345 tfactor_used = TRUE;
5346 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5347 tempreg_used = TRUE;
5348 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5349 tss_const_map |= 1 << stage;
5351 lowest_disabled_stage = stage;
5353 shader_addline(buffer, "#version 120\n");
5355 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5356 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5358 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
5359 shader_addline(buffer, "vec4 ret;\n");
5360 if (tempreg_used || settings->sRGB_write)
5361 shader_addline(buffer, "vec4 temp_reg;\n");
5362 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
5364 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5366 if (tss_const_map & (1 << stage))
5367 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
5369 if (!(tex_map & (1 << stage)))
5370 continue;
5372 switch (settings->op[stage].tex_type)
5374 case tex_1d:
5375 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
5376 break;
5377 case tex_2d:
5378 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
5379 break;
5380 case tex_3d:
5381 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
5382 break;
5383 case tex_cube:
5384 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
5385 break;
5386 case tex_rect:
5387 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
5388 break;
5389 default:
5390 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
5391 break;
5394 shader_addline(buffer, "vec4 tex%u;\n", stage);
5396 if (!(bump_map & (1 << stage)))
5397 continue;
5398 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
5400 if (!(lum_map & (1 << stage)))
5401 continue;
5402 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
5403 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
5405 if (tfactor_used)
5406 shader_addline(buffer, "uniform vec4 tex_factor;\n");
5407 shader_addline(buffer, "uniform vec4 specular_enable;\n");
5409 if (settings->sRGB_write)
5411 shader_addline(buffer, "const vec4 srgb_const0 = ");
5412 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
5413 shader_addline(buffer, ";\n");
5414 shader_addline(buffer, "const vec4 srgb_const1 = ");
5415 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
5416 shader_addline(buffer, ";\n");
5419 shader_addline(buffer, "void main()\n{\n");
5421 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
5422 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
5424 /* Generate texture sampling instructions) */
5425 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
5427 const char *texture_function, *coord_mask;
5428 char tex_reg_name[8];
5429 BOOL proj;
5431 if (!(tex_map & (1 << stage)))
5432 continue;
5434 if (settings->op[stage].projected == proj_none)
5436 proj = FALSE;
5438 else if (settings->op[stage].projected == proj_count4
5439 || settings->op[stage].projected == proj_count3)
5441 proj = TRUE;
5443 else
5445 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
5446 proj = TRUE;
5449 switch (settings->op[stage].tex_type)
5451 case tex_1d:
5452 if (proj)
5454 texture_function = "texture1DProj";
5455 coord_mask = "xw";
5457 else
5459 texture_function = "texture1D";
5460 coord_mask = "x";
5462 break;
5463 case tex_2d:
5464 if (proj)
5466 texture_function = "texture2DProj";
5467 coord_mask = "xyw";
5469 else
5471 texture_function = "texture2D";
5472 coord_mask = "xy";
5474 break;
5475 case tex_3d:
5476 if (proj)
5478 texture_function = "texture3DProj";
5479 coord_mask = "xyzw";
5481 else
5483 texture_function = "texture3D";
5484 coord_mask = "xyz";
5486 break;
5487 case tex_cube:
5488 texture_function = "textureCube";
5489 coord_mask = "xyz";
5490 break;
5491 case tex_rect:
5492 if (proj)
5494 texture_function = "texture2DRectProj";
5495 coord_mask = "xyw";
5497 else
5499 texture_function = "texture2DRect";
5500 coord_mask = "xy";
5502 break;
5503 default:
5504 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
5505 texture_function = "";
5506 coord_mask = "xyzw";
5507 break;
5510 if (stage > 0
5511 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
5512 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
5514 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
5516 /* With projective textures, texbem only divides the static
5517 * texture coord, not the displacement, so multiply the
5518 * displacement with the dividing parameter before passing it to
5519 * TXP. */
5520 if (settings->op[stage].projected != proj_none)
5522 if (settings->op[stage].projected == proj_count4)
5524 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
5525 stage, stage);
5526 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
5528 else
5530 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
5531 stage, stage);
5532 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
5535 else
5537 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
5540 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
5541 stage, texture_function, stage, coord_mask);
5543 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
5544 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
5545 stage, stage - 1, stage - 1, stage - 1);
5547 else if (settings->op[stage].projected == proj_count3)
5549 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
5550 stage, texture_function, stage, stage);
5552 else
5554 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
5555 stage, texture_function, stage, stage, coord_mask);
5558 sprintf(tex_reg_name, "tex%u", stage);
5559 shader_glsl_color_correction_ext(buffer, tex_reg_name, WINED3DSP_WRITEMASK_ALL,
5560 settings->op[stage].color_fixup);
5563 /* Generate the main shader */
5564 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5566 BOOL op_equal;
5568 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5570 if (!stage)
5571 final_combiner_src = "gl_Color";
5572 break;
5575 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5576 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5577 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
5578 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
5579 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5580 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
5581 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5582 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
5583 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
5584 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
5585 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
5586 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
5587 else
5588 op_equal = settings->op[stage].aop == settings->op[stage].cop
5589 && settings->op[stage].carg0 == settings->op[stage].aarg0
5590 && settings->op[stage].carg1 == settings->op[stage].aarg1
5591 && settings->op[stage].carg2 == settings->op[stage].aarg2;
5593 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5595 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5596 settings->op[stage].cop, settings->op[stage].carg0,
5597 settings->op[stage].carg1, settings->op[stage].carg2);
5598 if (!stage)
5599 shader_addline(buffer, "ret.w = gl_Color.w;\n");
5601 else if (op_equal)
5603 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
5604 settings->op[stage].cop, settings->op[stage].carg0,
5605 settings->op[stage].carg1, settings->op[stage].carg2);
5607 else
5609 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
5610 settings->op[stage].cop, settings->op[stage].carg0,
5611 settings->op[stage].carg1, settings->op[stage].carg2);
5612 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
5613 settings->op[stage].aop, settings->op[stage].aarg0,
5614 settings->op[stage].aarg1, settings->op[stage].aarg2);
5618 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
5620 if (settings->sRGB_write)
5621 shader_glsl_generate_srgb_write_correction(buffer);
5623 shader_glsl_generate_fog_code(buffer, settings->fog);
5625 shader_addline(buffer, "}\n");
5627 shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
5628 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5629 return shader_obj;
5632 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
5633 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
5635 struct glsl_ffp_vertex_shader *shader;
5636 const struct wine_rb_entry *entry;
5638 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
5639 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
5641 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
5642 return NULL;
5644 shader->desc.settings = *settings;
5645 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
5646 list_init(&shader->linked_programs);
5647 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
5648 ERR("Failed to insert ffp vertex shader.\n");
5650 return shader;
5653 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
5654 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
5656 struct glsl_ffp_fragment_shader *glsl_desc;
5657 const struct ffp_frag_desc *desc;
5659 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
5660 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
5662 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
5663 return NULL;
5665 glsl_desc->entry.settings = *args;
5666 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(&priv->shader_buffer, args, gl_info);
5667 list_init(&glsl_desc->linked_programs);
5668 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
5670 return glsl_desc;
5674 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
5675 GLhandleARB program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
5677 unsigned int i;
5678 char name[32];
5680 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5681 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
5682 for (i = 0; i < vs_c_count; ++i)
5684 snprintf(name, sizeof(name), "vs_c[%u]", i);
5685 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5687 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
5688 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLhandleARB));
5690 for (i = 0; i < MAX_CONST_I; ++i)
5692 snprintf(name, sizeof(name), "vs_i[%u]", i);
5693 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5696 for (i = 0; i < MAX_CONST_B; ++i)
5698 snprintf(name, sizeof(name), "vs_b[%u]", i);
5699 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5702 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "posFixup"));
5705 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
5706 GLhandleARB program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
5708 unsigned int i;
5709 char name[32];
5711 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
5712 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
5713 for (i = 0; i < ps_c_count; ++i)
5715 snprintf(name, sizeof(name), "ps_c[%u]", i);
5716 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5718 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
5719 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLhandleARB));
5721 for (i = 0; i < MAX_CONST_I; ++i)
5723 snprintf(name, sizeof(name), "ps_i[%u]", i);
5724 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5727 for (i = 0; i < MAX_CONST_B; ++i)
5729 snprintf(name, sizeof(name), "ps_b[%u]", i);
5730 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5733 for (i = 0; i < MAX_TEXTURES; ++i)
5735 snprintf(name, sizeof(name), "bumpenv_mat%u", i);
5736 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5737 snprintf(name, sizeof(name), "bumpenv_lum_scale%u", i);
5738 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5739 snprintf(name, sizeof(name), "bumpenv_lum_offset%u", i);
5740 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5741 snprintf(name, sizeof(name), "tss_const%u", i);
5742 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocationARB(program_id, name));
5745 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "tex_factor"));
5746 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "specular_enable"));
5747 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ps_samplerNP2Fixup"));
5748 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(program_id, "ycorrection"));
5751 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info, GLhandleARB program_id,
5752 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
5754 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
5755 GLuint block_idx;
5756 unsigned int i;
5757 char name[16];
5759 for (i = 0; i < count; ++i)
5761 if (!reg_maps->cb_sizes[i])
5762 continue;
5764 snprintf(name, sizeof(name), "block_%s_cb%u", prefix, i);
5765 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name));
5766 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
5768 checkGLcall("glUniformBlockBinding");
5771 /* Context activation is done by the caller. */
5772 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
5773 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
5775 const struct wined3d_gl_info *gl_info = context->gl_info;
5776 const struct ps_np2fixup_info *np2fixup_info = NULL;
5777 struct glsl_shader_prog_link *entry = NULL;
5778 struct wined3d_shader *vshader = NULL;
5779 struct wined3d_shader *gshader = NULL;
5780 struct wined3d_shader *pshader = NULL;
5781 GLhandleARB programId = 0;
5782 GLhandleARB reorder_shader_id = 0;
5783 unsigned int i;
5784 GLhandleARB vs_id = 0;
5785 GLhandleARB gs_id = 0;
5786 GLhandleARB ps_id = 0;
5787 struct list *ps_list, *vs_list;
5789 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
5791 vs_id = ctx_data->glsl_program->vs.id;
5792 vs_list = &ctx_data->glsl_program->vs.shader_entry;
5794 if (use_vs(state))
5796 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5797 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
5799 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
5800 && ctx_data->glsl_program->gs.id)
5801 gs_id = ctx_data->glsl_program->gs.id;
5802 else if (gshader)
5803 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5806 else if (use_vs(state))
5808 struct vs_compile_args vs_compile_args;
5809 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
5811 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
5812 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
5813 vs_list = &vshader->linked_programs;
5815 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
5816 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
5818 else if (priv->vertex_pipe == &glsl_vertex_pipe)
5820 struct glsl_ffp_vertex_shader *ffp_shader;
5821 struct wined3d_ffp_vs_settings settings;
5823 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
5824 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
5825 vs_id = ffp_shader->id;
5826 vs_list = &ffp_shader->linked_programs;
5829 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
5831 ps_id = ctx_data->glsl_program->ps.id;
5832 ps_list = &ctx_data->glsl_program->ps.shader_entry;
5834 if (use_ps(state))
5835 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5837 else if (use_ps(state))
5839 struct ps_compile_args ps_compile_args;
5840 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
5841 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
5842 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
5843 pshader, &ps_compile_args, &np2fixup_info);
5844 ps_list = &pshader->linked_programs;
5846 else if (priv->fragment_pipe == &glsl_fragment_pipe)
5848 struct glsl_ffp_fragment_shader *ffp_shader;
5849 struct ffp_frag_settings settings;
5851 gen_ffp_frag_op(context, state, &settings, FALSE);
5852 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
5853 ps_id = ffp_shader->id;
5854 ps_list = &ffp_shader->linked_programs;
5857 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
5859 ctx_data->glsl_program = entry;
5860 return;
5863 /* If we get to this point, then no matching program exists, so we create one */
5864 programId = GL_EXTCALL(glCreateProgramObjectARB());
5865 TRACE("Created new GLSL shader program %u\n", programId);
5867 /* Create the entry */
5868 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
5869 entry->programId = programId;
5870 entry->vs.id = vs_id;
5871 entry->gs.id = gs_id;
5872 entry->ps.id = ps_id;
5873 entry->constant_version = 0;
5874 entry->ps.np2_fixup_info = np2fixup_info;
5875 /* Add the hash table entry */
5876 add_glsl_program_entry(priv, entry);
5878 /* Set the current program */
5879 ctx_data->glsl_program = entry;
5881 /* Attach GLSL vshader */
5882 if (vs_id)
5884 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, programId);
5885 GL_EXTCALL(glAttachObjectARB(programId, vs_id));
5886 checkGLcall("glAttachObjectARB");
5888 list_add_head(vs_list, &entry->vs.shader_entry);
5891 if (vshader)
5893 WORD map = vshader->reg_maps.input_registers;
5894 char tmp_name[10];
5896 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
5897 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
5898 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
5899 checkGLcall("glAttachObjectARB");
5900 /* Flag the reorder function for deletion, then it will be freed automatically when the program
5901 * is destroyed
5903 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
5905 /* Bind vertex attributes to a corresponding index number to match
5906 * the same index numbers as ARB_vertex_programs (makes loading
5907 * vertex attributes simpler). With this method, we can use the
5908 * exact same code to load the attributes later for both ARB and
5909 * GLSL shaders.
5911 * We have to do this here because we need to know the Program ID
5912 * in order to make the bindings work, and it has to be done prior
5913 * to linking the GLSL program. */
5914 for (i = 0; map; map >>= 1, ++i)
5916 if (!(map & 1)) continue;
5918 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
5919 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
5921 checkGLcall("glBindAttribLocationARB");
5924 if (gshader)
5926 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, programId);
5927 GL_EXTCALL(glAttachObjectARB(programId, gs_id));
5928 checkGLcall("glAttachObjectARB");
5930 TRACE("input type %s, output type %s, vertices out %u.\n",
5931 debug_d3dprimitivetype(gshader->u.gs.input_type),
5932 debug_d3dprimitivetype(gshader->u.gs.output_type),
5933 gshader->u.gs.vertices_out);
5934 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_INPUT_TYPE_ARB,
5935 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
5936 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_OUTPUT_TYPE_ARB,
5937 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
5938 GL_EXTCALL(glProgramParameteriARB(programId, GL_GEOMETRY_VERTICES_OUT_ARB,
5939 gshader->u.gs.vertices_out));
5940 checkGLcall("glProgramParameteriARB");
5942 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
5945 /* Attach GLSL pshader */
5946 if (ps_id)
5948 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, programId);
5949 GL_EXTCALL(glAttachObjectARB(programId, ps_id));
5950 checkGLcall("glAttachObjectARB");
5952 list_add_head(ps_list, &entry->ps.shader_entry);
5955 /* Link the program */
5956 TRACE("Linking GLSL shader program %u\n", programId);
5957 GL_EXTCALL(glLinkProgramARB(programId));
5958 shader_glsl_validate_link(gl_info, programId);
5960 shader_glsl_init_vs_uniform_locations(gl_info, programId, &entry->vs,
5961 vshader ? vshader->limits.constant_float : 0);
5962 shader_glsl_init_ps_uniform_locations(gl_info, programId, &entry->ps,
5963 pshader ? pshader->limits.constant_float : 0);
5964 checkGLcall("Find glsl program uniform locations");
5966 if (pshader && pshader->reg_maps.shader_version.major >= 3
5967 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
5969 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
5970 entry->vs.vertex_color_clamp = GL_FALSE;
5972 else
5974 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
5977 /* Set the shader to allow uniform loading on it */
5978 GL_EXTCALL(glUseProgramObjectARB(programId));
5979 checkGLcall("glUseProgramObjectARB(programId)");
5981 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
5982 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
5983 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
5984 * vertex shader with fixed function pixel processing is used we make sure that the card
5985 * supports enough samplers to allow the max number of vertex samplers with all possible
5986 * fixed function fragment processing setups. So once the program is linked these samplers
5987 * won't change.
5989 shader_glsl_load_vsamplers(gl_info, context->tex_unit_map, programId);
5990 shader_glsl_load_psamplers(gl_info, context->tex_unit_map, programId);
5992 entry->constant_update_mask = 0;
5993 if (vshader)
5995 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
5996 if (vshader->reg_maps.integer_constants)
5997 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
5998 if (vshader->reg_maps.boolean_constants)
5999 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6000 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6002 shader_glsl_init_uniform_block_bindings(gl_info, programId, &vshader->reg_maps,
6003 0, gl_info->limits.vertex_uniform_blocks);
6006 if (gshader)
6007 shader_glsl_init_uniform_block_bindings(gl_info, programId, &gshader->reg_maps,
6008 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6010 if (ps_id)
6012 if (pshader)
6014 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6015 if (pshader->reg_maps.integer_constants)
6016 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6017 if (pshader->reg_maps.boolean_constants)
6018 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6019 if (entry->ps.ycorrection_location != -1)
6020 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6022 shader_glsl_init_uniform_block_bindings(gl_info, programId, &pshader->reg_maps,
6023 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6024 gl_info->limits.fragment_uniform_blocks);
6026 else
6028 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6031 for (i = 0; i < MAX_TEXTURES; ++i)
6033 if (entry->ps.bumpenv_mat_location[i] != -1)
6035 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6036 break;
6040 if (entry->ps.np2_fixup_location != -1)
6041 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6045 /* Context activation is done by the caller. */
6046 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
6048 GLhandleARB program_id;
6049 GLhandleARB vshader_id, pshader_id;
6050 const char *blt_pshader;
6052 static const char blt_vshader[] =
6053 "#version 120\n"
6054 "void main(void)\n"
6055 "{\n"
6056 " gl_Position = gl_Vertex;\n"
6057 " gl_FrontColor = vec4(1.0);\n"
6058 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6059 "}\n";
6061 static const char * const blt_pshaders_full[tex_type_count] =
6063 /* tex_1d */
6064 NULL,
6065 /* tex_2d */
6066 "#version 120\n"
6067 "uniform sampler2D sampler;\n"
6068 "void main(void)\n"
6069 "{\n"
6070 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6071 "}\n",
6072 /* tex_3d */
6073 NULL,
6074 /* tex_cube */
6075 "#version 120\n"
6076 "uniform samplerCube sampler;\n"
6077 "void main(void)\n"
6078 "{\n"
6079 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6080 "}\n",
6081 /* tex_rect */
6082 "#version 120\n"
6083 "#extension GL_ARB_texture_rectangle : enable\n"
6084 "uniform sampler2DRect sampler;\n"
6085 "void main(void)\n"
6086 "{\n"
6087 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6088 "}\n",
6091 static const char * const blt_pshaders_masked[tex_type_count] =
6093 /* tex_1d */
6094 NULL,
6095 /* tex_2d */
6096 "#version 120\n"
6097 "uniform sampler2D sampler;\n"
6098 "uniform vec4 mask;\n"
6099 "void main(void)\n"
6100 "{\n"
6101 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6102 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6103 "}\n",
6104 /* tex_3d */
6105 NULL,
6106 /* tex_cube */
6107 "#version 120\n"
6108 "uniform samplerCube sampler;\n"
6109 "uniform vec4 mask;\n"
6110 "void main(void)\n"
6111 "{\n"
6112 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6113 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6114 "}\n",
6115 /* tex_rect */
6116 "#version 120\n"
6117 "#extension GL_ARB_texture_rectangle : enable\n"
6118 "uniform sampler2DRect sampler;\n"
6119 "uniform vec4 mask;\n"
6120 "void main(void)\n"
6121 "{\n"
6122 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6123 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6124 "}\n",
6127 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6128 if (!blt_pshader)
6130 FIXME("tex_type %#x not supported\n", tex_type);
6131 return 0;
6134 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
6135 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6137 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
6138 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6140 program_id = GL_EXTCALL(glCreateProgramObjectARB());
6141 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
6142 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
6143 GL_EXTCALL(glLinkProgramARB(program_id));
6145 shader_glsl_validate_link(gl_info, program_id);
6147 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6148 * is destroyed
6150 GL_EXTCALL(glDeleteObjectARB(vshader_id));
6151 GL_EXTCALL(glDeleteObjectARB(pshader_id));
6152 return program_id;
6155 /* Context activation is done by the caller. */
6156 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6157 const struct wined3d_state *state)
6159 struct glsl_context_data *ctx_data = context->shader_backend_data;
6160 const struct wined3d_gl_info *gl_info = context->gl_info;
6161 struct shader_glsl_priv *priv = shader_priv;
6162 GLhandleARB program_id = 0, prev_id = 0;
6163 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6165 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6166 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6168 if (ctx_data->glsl_program)
6170 prev_id = ctx_data->glsl_program->programId;
6171 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6173 else
6175 prev_id = 0;
6176 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6179 set_glsl_shader_program(context, state, priv, ctx_data);
6181 if (ctx_data->glsl_program)
6183 program_id = ctx_data->glsl_program->programId;
6184 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6186 else
6188 program_id = 0;
6189 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6192 if (old_vertex_color_clamp != current_vertex_color_clamp)
6194 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6196 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6197 checkGLcall("glClampColorARB");
6199 else
6201 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6205 TRACE("Using GLSL program %u.\n", program_id);
6207 if (prev_id != program_id)
6209 GL_EXTCALL(glUseProgramObjectARB(program_id));
6210 checkGLcall("glUseProgramObjectARB");
6212 if (program_id)
6213 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6217 /* "context" is not necessarily the currently active context. */
6218 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6220 struct glsl_context_data *ctx_data = context->shader_backend_data;
6222 ctx_data->glsl_program = NULL;
6223 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6224 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6225 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6228 /* Context activation is done by the caller. */
6229 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6231 const struct wined3d_gl_info *gl_info = context->gl_info;
6232 struct shader_glsl_priv *priv = shader_priv;
6234 shader_glsl_invalidate_current_program(context);
6235 GL_EXTCALL(glUseProgramObjectARB(0));
6236 checkGLcall("glUseProgramObjectARB");
6238 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6239 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6241 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6243 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6244 checkGLcall("glClampColorARB");
6248 /* Context activation is done by the caller. */
6249 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6250 enum tex_types tex_type, const SIZE *ds_mask_size)
6252 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6253 struct shader_glsl_priv *priv = shader_priv;
6254 GLhandleARB *blt_program;
6255 GLint loc;
6257 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6258 if (!*blt_program)
6260 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6261 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
6262 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6263 GL_EXTCALL(glUniform1iARB(loc, 0));
6265 else
6267 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
6270 if (masked)
6272 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
6273 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6277 /* Context activation is done by the caller. */
6278 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6280 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
6281 GLhandleARB program_id;
6283 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->programId : 0;
6284 if (program_id) TRACE("Using GLSL program %u\n", program_id);
6286 GL_EXTCALL(glUseProgramObjectARB(program_id));
6287 checkGLcall("glUseProgramObjectARB");
6290 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
6291 const struct glsl_shader_prog_link *program)
6293 const struct glsl_context_data *ctx_data;
6294 struct wined3d_context *context;
6295 unsigned int i;
6297 for (i = 0; i < device->context_count; ++i)
6299 context = device->contexts[i];
6300 ctx_data = context->shader_backend_data;
6302 if (ctx_data->glsl_program == program)
6303 shader_glsl_invalidate_current_program(context);
6307 static void shader_glsl_destroy(struct wined3d_shader *shader)
6309 struct glsl_shader_private *shader_data = shader->backend_data;
6310 struct wined3d_device *device = shader->device;
6311 struct shader_glsl_priv *priv = device->shader_priv;
6312 const struct wined3d_gl_info *gl_info;
6313 const struct list *linked_programs;
6314 struct wined3d_context *context;
6316 if (!shader_data || !shader_data->num_gl_shaders)
6318 HeapFree(GetProcessHeap(), 0, shader_data);
6319 shader->backend_data = NULL;
6320 return;
6323 context = context_acquire(device, NULL);
6324 gl_info = context->gl_info;
6326 TRACE("Deleting linked programs.\n");
6327 linked_programs = &shader->linked_programs;
6328 if (linked_programs->next)
6330 struct glsl_shader_prog_link *entry, *entry2;
6331 UINT i;
6333 switch (shader->reg_maps.shader_version.type)
6335 case WINED3D_SHADER_TYPE_PIXEL:
6337 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
6339 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6341 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
6342 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6343 checkGLcall("glDeleteObjectARB");
6345 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
6347 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6348 struct glsl_shader_prog_link, ps.shader_entry)
6350 shader_glsl_invalidate_contexts_program(device, entry);
6351 delete_glsl_program_entry(priv, gl_info, entry);
6354 break;
6357 case WINED3D_SHADER_TYPE_VERTEX:
6359 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
6361 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6363 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
6364 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
6365 checkGLcall("glDeleteObjectARB");
6367 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
6369 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6370 struct glsl_shader_prog_link, vs.shader_entry)
6372 shader_glsl_invalidate_contexts_program(device, entry);
6373 delete_glsl_program_entry(priv, gl_info, entry);
6376 break;
6379 case WINED3D_SHADER_TYPE_GEOMETRY:
6381 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
6383 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6385 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
6386 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].id));
6387 checkGLcall("glDeleteObjectARB");
6389 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
6391 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
6392 struct glsl_shader_prog_link, gs.shader_entry)
6394 shader_glsl_invalidate_contexts_program(device, entry);
6395 delete_glsl_program_entry(priv, gl_info, entry);
6398 break;
6401 default:
6402 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6403 break;
6407 HeapFree(GetProcessHeap(), 0, shader->backend_data);
6408 shader->backend_data = NULL;
6410 context_release(context);
6413 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
6415 const struct glsl_program_key *k = key;
6416 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
6417 const struct glsl_shader_prog_link, program_lookup_entry);
6419 if (k->vs_id > prog->vs.id) return 1;
6420 else if (k->vs_id < prog->vs.id) return -1;
6422 if (k->gs_id > prog->gs.id) return 1;
6423 else if (k->gs_id < prog->gs.id) return -1;
6425 if (k->ps_id > prog->ps.id) return 1;
6426 else if (k->ps_id < prog->ps.id) return -1;
6428 return 0;
6431 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
6433 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
6434 + constant_count * sizeof(*heap->contained)
6435 + constant_count * sizeof(*heap->positions);
6436 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
6438 if (!mem)
6440 ERR("Failed to allocate memory\n");
6441 return FALSE;
6444 heap->entries = mem;
6445 heap->entries[1].version = 0;
6446 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
6447 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
6448 heap->positions = (unsigned int *)(heap->contained + constant_count);
6449 heap->size = 1;
6451 return TRUE;
6454 static void constant_heap_free(struct constant_heap *heap)
6456 HeapFree(GetProcessHeap(), 0, heap->entries);
6459 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
6461 wined3d_rb_alloc,
6462 wined3d_rb_realloc,
6463 wined3d_rb_free,
6464 glsl_program_key_compare,
6467 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
6468 const struct fragment_pipeline *fragment_pipe)
6470 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6471 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
6472 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
6473 gl_info->limits.glsl_ps_float_constants)) + 1;
6474 struct fragment_caps fragment_caps;
6475 void *vertex_priv, *fragment_priv;
6477 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
6479 ERR("Failed to initialize vertex pipe.\n");
6480 HeapFree(GetProcessHeap(), 0, priv);
6481 return E_FAIL;
6484 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
6486 ERR("Failed to initialize fragment pipe.\n");
6487 vertex_pipe->vp_free(device);
6488 HeapFree(GetProcessHeap(), 0, priv);
6489 return E_FAIL;
6492 if (!shader_buffer_init(&priv->shader_buffer))
6494 ERR("Failed to initialize shader buffer.\n");
6495 goto fail;
6498 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
6499 if (!priv->stack)
6501 ERR("Failed to allocate memory.\n");
6502 goto fail;
6505 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
6507 ERR("Failed to initialize vertex shader constant heap\n");
6508 goto fail;
6511 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
6513 ERR("Failed to initialize pixel shader constant heap\n");
6514 goto fail;
6517 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
6519 ERR("Failed to initialize rbtree.\n");
6520 goto fail;
6523 priv->next_constant_version = 1;
6524 priv->vertex_pipe = vertex_pipe;
6525 priv->fragment_pipe = fragment_pipe;
6526 fragment_pipe->get_caps(gl_info, &fragment_caps);
6527 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
6529 device->vertex_priv = vertex_priv;
6530 device->fragment_priv = fragment_priv;
6531 device->shader_priv = priv;
6533 return WINED3D_OK;
6535 fail:
6536 constant_heap_free(&priv->pconst_heap);
6537 constant_heap_free(&priv->vconst_heap);
6538 HeapFree(GetProcessHeap(), 0, priv->stack);
6539 shader_buffer_free(&priv->shader_buffer);
6540 fragment_pipe->free_private(device);
6541 vertex_pipe->vp_free(device);
6542 HeapFree(GetProcessHeap(), 0, priv);
6543 return E_OUTOFMEMORY;
6546 /* Context activation is done by the caller. */
6547 static void shader_glsl_free(struct wined3d_device *device)
6549 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
6550 struct shader_glsl_priv *priv = device->shader_priv;
6551 int i;
6553 for (i = 0; i < tex_type_count; ++i)
6555 if (priv->depth_blt_program_full[i])
6557 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
6559 if (priv->depth_blt_program_masked[i])
6561 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
6565 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
6566 constant_heap_free(&priv->pconst_heap);
6567 constant_heap_free(&priv->vconst_heap);
6568 HeapFree(GetProcessHeap(), 0, priv->stack);
6569 shader_buffer_free(&priv->shader_buffer);
6570 priv->fragment_pipe->free_private(device);
6571 priv->vertex_pipe->vp_free(device);
6573 HeapFree(GetProcessHeap(), 0, device->shader_priv);
6574 device->shader_priv = NULL;
6577 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
6579 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
6580 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
6583 static void shader_glsl_free_context_data(struct wined3d_context *context)
6585 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
6588 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
6590 UINT shader_model;
6592 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
6593 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
6594 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
6595 && gl_info->supported[ARB_TEXTURE_RG])
6596 shader_model = 4;
6597 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
6598 * texldd and texldl instructions. */
6599 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
6600 shader_model = 3;
6601 else
6602 shader_model = 2;
6603 TRACE("Shader model %u.\n", shader_model);
6605 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
6606 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
6607 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
6609 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
6610 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
6612 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
6613 * Direct3D minimum requirement.
6615 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
6616 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
6618 * The problem is that the refrast clamps temporary results in the shader to
6619 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
6620 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
6621 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
6622 * offer a way to query this.
6624 if (shader_model >= 4)
6625 caps->ps_1x_max_value = FLT_MAX;
6626 else
6627 caps->ps_1x_max_value = 1024.0f;
6629 /* Ideally we'd only set caps like sRGB writes here if supported by both
6630 * the shader backend and the fragment pipe, but we can get called before
6631 * shader_glsl_alloc(). */
6632 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
6633 | WINED3D_SHADER_CAP_SRGB_WRITE;
6636 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
6638 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
6640 TRACE("Checking support for fixup:\n");
6641 dump_color_fixup_desc(fixup);
6644 /* We support everything except YUV conversions. */
6645 if (!is_complex_fixup(fixup))
6647 TRACE("[OK]\n");
6648 return TRUE;
6651 TRACE("[FAILED]\n");
6652 return FALSE;
6655 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
6657 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
6658 /* WINED3DSIH_ADD */ shader_glsl_binop,
6659 /* WINED3DSIH_AND */ shader_glsl_binop,
6660 /* WINED3DSIH_BEM */ shader_glsl_bem,
6661 /* WINED3DSIH_BREAK */ shader_glsl_break,
6662 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
6663 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
6664 /* WINED3DSIH_CALL */ shader_glsl_call,
6665 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
6666 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
6667 /* WINED3DSIH_CND */ shader_glsl_cnd,
6668 /* WINED3DSIH_CRS */ shader_glsl_cross,
6669 /* WINED3DSIH_CUT */ shader_glsl_cut,
6670 /* WINED3DSIH_DCL */ shader_glsl_nop,
6671 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
6672 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
6673 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
6674 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
6675 /* WINED3DSIH_DEF */ shader_glsl_nop,
6676 /* WINED3DSIH_DEFB */ shader_glsl_nop,
6677 /* WINED3DSIH_DEFI */ shader_glsl_nop,
6678 /* WINED3DSIH_DIV */ shader_glsl_binop,
6679 /* WINED3DSIH_DP2 */ shader_glsl_dot,
6680 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
6681 /* WINED3DSIH_DP3 */ shader_glsl_dot,
6682 /* WINED3DSIH_DP4 */ shader_glsl_dot,
6683 /* WINED3DSIH_DST */ shader_glsl_dst,
6684 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
6685 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
6686 /* WINED3DSIH_ELSE */ shader_glsl_else,
6687 /* WINED3DSIH_EMIT */ shader_glsl_emit,
6688 /* WINED3DSIH_ENDIF */ shader_glsl_end,
6689 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
6690 /* WINED3DSIH_ENDREP */ shader_glsl_end,
6691 /* WINED3DSIH_EQ */ shader_glsl_relop,
6692 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
6693 /* WINED3DSIH_EXPP */ shader_glsl_expp,
6694 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
6695 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
6696 /* WINED3DSIH_GE */ shader_glsl_relop,
6697 /* WINED3DSIH_IADD */ shader_glsl_binop,
6698 /* WINED3DSIH_IEQ */ NULL,
6699 /* WINED3DSIH_IF */ shader_glsl_if,
6700 /* WINED3DSIH_IFC */ shader_glsl_ifc,
6701 /* WINED3DSIH_IGE */ shader_glsl_relop,
6702 /* WINED3DSIH_IMUL */ shader_glsl_imul,
6703 /* WINED3DSIH_ISHL */ shader_glsl_binop,
6704 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
6705 /* WINED3DSIH_LABEL */ shader_glsl_label,
6706 /* WINED3DSIH_LD */ NULL,
6707 /* WINED3DSIH_LIT */ shader_glsl_lit,
6708 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
6709 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
6710 /* WINED3DSIH_LOOP */ shader_glsl_loop,
6711 /* WINED3DSIH_LRP */ shader_glsl_lrp,
6712 /* WINED3DSIH_LT */ shader_glsl_relop,
6713 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
6714 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
6715 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
6716 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
6717 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
6718 /* WINED3DSIH_MAD */ shader_glsl_mad,
6719 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
6720 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
6721 /* WINED3DSIH_MOV */ shader_glsl_mov,
6722 /* WINED3DSIH_MOVA */ shader_glsl_mov,
6723 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
6724 /* WINED3DSIH_MUL */ shader_glsl_binop,
6725 /* WINED3DSIH_NOP */ shader_glsl_nop,
6726 /* WINED3DSIH_NRM */ shader_glsl_nrm,
6727 /* WINED3DSIH_PHASE */ shader_glsl_nop,
6728 /* WINED3DSIH_POW */ shader_glsl_pow,
6729 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
6730 /* WINED3DSIH_REP */ shader_glsl_rep,
6731 /* WINED3DSIH_RET */ shader_glsl_ret,
6732 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
6733 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
6734 /* WINED3DSIH_SAMPLE */ NULL,
6735 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
6736 /* WINED3DSIH_SAMPLE_LOD */ NULL,
6737 /* WINED3DSIH_SETP */ NULL,
6738 /* WINED3DSIH_SGE */ shader_glsl_compare,
6739 /* WINED3DSIH_SGN */ shader_glsl_sgn,
6740 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
6741 /* WINED3DSIH_SLT */ shader_glsl_compare,
6742 /* WINED3DSIH_SQRT */ NULL,
6743 /* WINED3DSIH_SUB */ shader_glsl_binop,
6744 /* WINED3DSIH_TEX */ shader_glsl_tex,
6745 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
6746 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
6747 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
6748 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
6749 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
6750 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
6751 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
6752 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
6753 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
6754 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
6755 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
6756 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
6757 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
6758 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
6759 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
6760 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
6761 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
6762 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
6763 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
6764 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
6765 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
6766 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
6767 /* WINED3DSIH_USHR */ shader_glsl_binop,
6768 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
6769 /* WINED3DSIH_XOR */ shader_glsl_binop,
6772 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
6773 SHADER_HANDLER hw_fct;
6775 /* Select handler */
6776 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
6778 /* Unhandled opcode */
6779 if (!hw_fct)
6781 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
6782 return;
6784 hw_fct(ins);
6786 shader_glsl_add_instruction_modifiers(ins);
6789 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
6791 struct shader_glsl_priv *priv = shader_priv;
6793 return priv->ffp_proj_control;
6796 const struct wined3d_shader_backend_ops glsl_shader_backend =
6798 shader_glsl_handle_instruction,
6799 shader_glsl_select,
6800 shader_glsl_disable,
6801 shader_glsl_select_depth_blt,
6802 shader_glsl_deselect_depth_blt,
6803 shader_glsl_update_float_vertex_constants,
6804 shader_glsl_update_float_pixel_constants,
6805 shader_glsl_load_constants,
6806 shader_glsl_destroy,
6807 shader_glsl_alloc,
6808 shader_glsl_free,
6809 shader_glsl_allocate_context_data,
6810 shader_glsl_free_context_data,
6811 shader_glsl_get_caps,
6812 shader_glsl_color_fixup_supported,
6813 shader_glsl_has_ffp_proj_control,
6816 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
6818 if (enable)
6819 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6820 else
6821 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
6822 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
6825 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
6827 caps->xyzrhw = TRUE;
6828 caps->max_active_lights = gl_info->limits.lights;
6829 caps->max_vertex_blend_matrices = 1;
6830 caps->max_vertex_blend_matrix_index = 0;
6831 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
6832 | WINED3DVTXPCAPS_MATERIALSOURCE7
6833 | WINED3DVTXPCAPS_VERTEXFOG
6834 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
6835 | WINED3DVTXPCAPS_POSITIONALLIGHTS
6836 | WINED3DVTXPCAPS_LOCALVIEWER
6837 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
6838 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
6839 caps->max_user_clip_planes = gl_info->limits.clipplanes;
6840 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
6843 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
6845 struct shader_glsl_priv *priv;
6847 if (shader_backend == &glsl_shader_backend)
6849 priv = shader_priv;
6851 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
6853 ERR("Failed to initialize rbtree.\n");
6854 return NULL;
6857 return priv;
6860 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
6862 return NULL;
6865 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
6867 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
6868 struct glsl_ffp_vertex_shader, desc.entry);
6869 struct glsl_shader_prog_link *program, *program2;
6870 struct glsl_ffp_destroy_ctx *ctx = context;
6872 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
6873 struct glsl_shader_prog_link, vs.shader_entry)
6875 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
6877 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
6878 HeapFree(GetProcessHeap(), 0, shader);
6881 /* Context activation is done by the caller. */
6882 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
6884 struct shader_glsl_priv *priv = device->vertex_priv;
6885 struct glsl_ffp_destroy_ctx ctx;
6887 ctx.priv = priv;
6888 ctx.gl_info = &device->adapter->gl_info;
6889 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
6892 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
6893 const struct wined3d_state *state, DWORD state_id)
6895 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6898 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
6899 const struct wined3d_state *state, DWORD state_id)
6901 /* Table fog behavior depends on the projection matrix. */
6902 if (state->render_states[WINED3D_RS_FOGENABLE]
6903 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
6904 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
6905 transform_projection(context, state, state_id);
6908 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
6910 {STATE_VDECL, {STATE_VDECL, vertexdeclaration }, WINED3D_GL_EXT_NONE },
6911 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6912 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
6913 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), state_specularenable }, WINED3D_GL_EXT_NONE },
6914 /* Clip planes */
6915 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
6916 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
6917 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
6918 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
6919 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
6920 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
6921 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
6922 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
6923 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
6924 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
6925 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
6926 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
6927 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
6928 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
6929 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
6930 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
6931 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
6932 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
6933 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
6934 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
6935 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
6936 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
6937 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
6938 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
6939 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
6940 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
6941 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
6942 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
6943 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
6944 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
6945 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
6946 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
6947 /* Lights */
6948 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6949 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), light }, WINED3D_GL_EXT_NONE },
6950 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), light }, WINED3D_GL_EXT_NONE },
6951 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), light }, WINED3D_GL_EXT_NONE },
6952 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), light }, WINED3D_GL_EXT_NONE },
6953 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), light }, WINED3D_GL_EXT_NONE },
6954 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), light }, WINED3D_GL_EXT_NONE },
6955 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), light }, WINED3D_GL_EXT_NONE },
6956 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), light }, WINED3D_GL_EXT_NONE },
6957 /* Viewport */
6958 {STATE_VIEWPORT, {STATE_VIEWPORT, viewport_vertexpart }, WINED3D_GL_EXT_NONE },
6959 /* Transform states */
6960 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), transform_view }, WINED3D_GL_EXT_NONE },
6961 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
6962 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6963 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6964 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6965 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6966 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6967 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6968 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6969 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
6970 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), transform_world }, WINED3D_GL_EXT_NONE },
6971 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6972 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6973 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6974 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6975 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6976 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6977 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6978 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), transform_texture }, WINED3D_GL_EXT_NONE },
6979 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6980 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6981 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6982 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6983 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6984 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6985 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6986 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6987 /* Fog */
6988 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6989 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6990 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6991 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
6992 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
6993 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
6994 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6995 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), state_ambient }, WINED3D_GL_EXT_NONE },
6996 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
6997 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6998 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
6999 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7000 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7001 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7002 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7003 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7004 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7005 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7006 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7007 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7008 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7009 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7010 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7011 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7012 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7013 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7014 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7015 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7016 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7017 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7018 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_VDECL, NULL }, WINED3D_GL_EXT_NONE },
7019 /* Samplers for NP2 texture matrix adjustions. They are not needed if
7020 * GL_ARB_texture_non_power_of_two is supported, so register a NULL state
7021 * handler in that case to get the vertex part of sampler() skipped (VTF
7022 * is handled in the misc states). Otherwise, register
7023 * sampler_texmatrix(), which takes care of updating the texture matrix. */
7024 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7025 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7026 {STATE_SAMPLER(0), {STATE_SAMPLER(0), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7027 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7028 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7029 {STATE_SAMPLER(1), {STATE_SAMPLER(1), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7030 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7031 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7032 {STATE_SAMPLER(2), {STATE_SAMPLER(2), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7033 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7034 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7035 {STATE_SAMPLER(3), {STATE_SAMPLER(3), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7036 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7037 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7038 {STATE_SAMPLER(4), {STATE_SAMPLER(4), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7039 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7040 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7041 {STATE_SAMPLER(5), {STATE_SAMPLER(5), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7042 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7043 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7044 {STATE_SAMPLER(6), {STATE_SAMPLER(6), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7045 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7046 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7047 {STATE_SAMPLER(7), {STATE_SAMPLER(7), sampler_texmatrix }, WINED3D_GL_EXT_NONE },
7048 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7049 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7052 /* TODO:
7053 * - This currently depends on GL fixed function functions to set things
7054 * like light parameters. Ideally we'd use regular uniforms for that.
7055 * - In part because of the previous point, much of this is modelled after
7056 * GL fixed function, and has much of the same limitations. For example,
7057 * D3D spot lights are slightly different from GL spot lights.
7058 * - We can now implement drawing transformed vertices using the GLSL pipe,
7059 * instead of using the immediate mode fallback.
7060 * - Similarly, we don't need the fallback for certain combinations of
7061 * material sources anymore.
7062 * - Implement vertex blending and vertex tweening.
7063 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7064 * attribute arrays in load_tex_coords().
7065 * - Per-vertex point sizes. */
7066 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7068 glsl_vertex_pipe_vp_enable,
7069 glsl_vertex_pipe_vp_get_caps,
7070 glsl_vertex_pipe_vp_alloc,
7071 glsl_vertex_pipe_vp_free,
7072 glsl_vertex_pipe_vp_states,
7075 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7077 /* Nothing to do. */
7080 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7082 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7083 | WINED3D_FRAGMENT_CAP_SRGB_WRITE;
7084 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7085 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7086 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7087 | WINED3DTEXOPCAPS_SELECTARG1
7088 | WINED3DTEXOPCAPS_SELECTARG2
7089 | WINED3DTEXOPCAPS_MODULATE4X
7090 | WINED3DTEXOPCAPS_MODULATE2X
7091 | WINED3DTEXOPCAPS_MODULATE
7092 | WINED3DTEXOPCAPS_ADDSIGNED2X
7093 | WINED3DTEXOPCAPS_ADDSIGNED
7094 | WINED3DTEXOPCAPS_ADD
7095 | WINED3DTEXOPCAPS_SUBTRACT
7096 | WINED3DTEXOPCAPS_ADDSMOOTH
7097 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7098 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7099 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7100 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7101 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7102 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7103 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7104 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7105 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7106 | WINED3DTEXOPCAPS_DOTPRODUCT3
7107 | WINED3DTEXOPCAPS_MULTIPLYADD
7108 | WINED3DTEXOPCAPS_LERP
7109 | WINED3DTEXOPCAPS_BUMPENVMAP
7110 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7111 caps->MaxTextureBlendStages = 8;
7112 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7115 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7117 struct shader_glsl_priv *priv;
7119 if (shader_backend == &glsl_shader_backend)
7121 priv = shader_priv;
7123 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7125 ERR("Failed to initialize rbtree.\n");
7126 return NULL;
7129 return priv;
7132 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7134 return NULL;
7137 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7139 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7140 struct glsl_ffp_fragment_shader, entry.entry);
7141 struct glsl_shader_prog_link *program, *program2;
7142 struct glsl_ffp_destroy_ctx *ctx = context;
7144 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7145 struct glsl_shader_prog_link, ps.shader_entry)
7147 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7149 ctx->gl_info->gl_ops.ext.p_glDeleteObjectARB(shader->id);
7150 HeapFree(GetProcessHeap(), 0, shader);
7153 /* Context activation is done by the caller. */
7154 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7156 struct shader_glsl_priv *priv = device->fragment_priv;
7157 struct glsl_ffp_destroy_ctx ctx;
7159 ctx.priv = priv;
7160 ctx.gl_info = &device->adapter->gl_info;
7161 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
7164 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
7165 const struct wined3d_state *state, DWORD state_id)
7167 context->last_was_pshader = use_ps(state);
7169 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7172 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
7173 const struct wined3d_state *state, DWORD state_id)
7175 BOOL use_vshader = use_vs(state);
7176 enum fogsource new_source;
7177 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
7178 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
7180 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7182 if (!state->render_states[WINED3D_RS_FOGENABLE])
7183 return;
7185 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
7187 if (use_vshader)
7188 new_source = FOGSOURCE_VS;
7189 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->last_was_rhw)
7190 new_source = FOGSOURCE_COORD;
7191 else
7192 new_source = FOGSOURCE_FFP;
7194 else
7196 new_source = FOGSOURCE_FFP;
7199 if (new_source != context->fog_source || fogstart == fogend)
7201 context->fog_source = new_source;
7202 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
7206 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
7207 const struct wined3d_state *state, DWORD state_id)
7209 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7212 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
7213 const struct wined3d_state *state, DWORD state_id)
7215 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7218 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
7220 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7221 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7222 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7223 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7224 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7225 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7226 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7227 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7228 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7229 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7230 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7231 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7232 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7233 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7234 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7235 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7236 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7237 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7238 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7239 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7240 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7241 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7242 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7243 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7244 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7245 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7246 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7247 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7248 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7249 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7250 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7251 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7252 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7253 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7254 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7255 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7256 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7257 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7258 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7259 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7260 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7261 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7262 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7263 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7264 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7265 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7266 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7267 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7268 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7269 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7270 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7271 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7272 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7273 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7274 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7275 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7276 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7277 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7278 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7279 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7280 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7281 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7282 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7283 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7284 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7285 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7286 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7287 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7288 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7289 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7290 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7291 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7292 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7293 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
7294 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
7295 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7296 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7297 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
7298 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
7299 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
7300 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
7301 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
7302 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
7303 {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 },
7304 {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 },
7305 {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 },
7306 {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 },
7307 {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 },
7308 {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 },
7309 {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 },
7310 {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 },
7311 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7312 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7313 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7314 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7315 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7316 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7317 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7318 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7319 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
7320 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
7323 const struct fragment_pipeline glsl_fragment_pipe =
7325 glsl_fragment_pipe_enable,
7326 glsl_fragment_pipe_get_caps,
7327 glsl_fragment_pipe_alloc,
7328 glsl_fragment_pipe_free,
7329 shader_glsl_color_fixup_supported,
7330 glsl_fragment_pipe_state_template,