wined3d: Don't use the same va_list multiple times in shader_vaddline().
[wine/multimedia.git] / dlls / wined3d / glsl_shader.c
blob19c3d317294c678f29c9099418f184ef8a1be6bb
1 /*
2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2009, 2013 Stefan Dösinger for CodeWeavers
7 * Copyright 2009-2011 Henri Verbeet for CodeWeavers
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * D3D shader asm has swizzles on source parameters, and write masks for
26 * destination parameters. GLSL uses swizzles for both. The result of this is
27 * that for example "mov dst.xw, src.zyxw" becomes "dst.xw = src.zw" in GLSL.
28 * Ie, to generate a proper GLSL source swizzle, we need to take the D3D write
29 * mask for the destination parameter into account.
32 #include "config.h"
33 #include "wine/port.h"
35 #include <limits.h>
36 #include <stdio.h>
37 #ifdef HAVE_FLOAT_H
38 # include <float.h>
39 #endif
41 #include "wined3d_private.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
44 WINE_DECLARE_DEBUG_CHANNEL(d3d);
45 WINE_DECLARE_DEBUG_CHANNEL(winediag);
47 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x2
49 #define WINED3D_GLSL_SAMPLE_LOD 0x4
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
52 struct glsl_dst_param
54 char reg_name[150];
55 char mask_str[6];
58 struct glsl_src_param
60 char reg_name[150];
61 char param_str[200];
64 struct glsl_sample_function
66 const char *name;
67 DWORD coord_mask;
68 enum wined3d_data_type data_type;
71 enum heap_node_op
73 HEAP_NODE_TRAVERSE_LEFT,
74 HEAP_NODE_TRAVERSE_RIGHT,
75 HEAP_NODE_POP,
78 struct constant_entry
80 unsigned int idx;
81 unsigned int version;
84 struct constant_heap
86 struct constant_entry *entries;
87 BOOL *contained;
88 unsigned int *positions;
89 unsigned int size;
92 /* GLSL shader private data */
93 struct shader_glsl_priv {
94 struct wined3d_string_buffer shader_buffer;
95 struct wined3d_string_buffer_list string_buffers;
96 struct wine_rb_tree program_lookup;
97 struct constant_heap vconst_heap;
98 struct constant_heap pconst_heap;
99 unsigned char *stack;
100 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
101 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
102 UINT next_constant_version;
104 const struct wined3d_vertex_pipe_ops *vertex_pipe;
105 const struct fragment_pipeline *fragment_pipe;
106 struct wine_rb_tree ffp_vertex_shaders;
107 struct wine_rb_tree ffp_fragment_shaders;
108 BOOL ffp_proj_control;
111 struct glsl_vs_program
113 struct list shader_entry;
114 GLuint id;
115 GLenum vertex_color_clamp;
116 GLint *uniform_f_locations;
117 GLint uniform_i_locations[MAX_CONST_I];
118 GLint uniform_b_locations[MAX_CONST_B];
119 GLint pos_fixup_location;
121 GLint modelview_matrix_location;
122 GLint projection_matrix_location;
123 GLint normal_matrix_location;
124 GLint texture_matrix_location[MAX_TEXTURES];
125 GLint material_ambient_location;
126 GLint material_diffuse_location;
127 GLint material_specular_location;
128 GLint material_emission_location;
129 GLint material_shininess_location;
130 GLint light_ambient_location;
131 struct
133 GLint diffuse;
134 GLint specular;
135 GLint ambient;
136 GLint position;
137 GLint direction;
138 GLint range;
139 GLint falloff;
140 GLint c_att;
141 GLint l_att;
142 GLint q_att;
143 GLint cos_htheta;
144 GLint cos_hphi;
145 } light_location[MAX_ACTIVE_LIGHTS];
148 struct glsl_gs_program
150 struct list shader_entry;
151 GLuint id;
154 struct glsl_ps_program
156 struct list shader_entry;
157 GLuint id;
158 GLint *uniform_f_locations;
159 GLint uniform_i_locations[MAX_CONST_I];
160 GLint uniform_b_locations[MAX_CONST_B];
161 GLint bumpenv_mat_location[MAX_TEXTURES];
162 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
163 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
164 GLint tss_constant_location[MAX_TEXTURES];
165 GLint tex_factor_location;
166 GLint specular_enable_location;
167 GLint ycorrection_location;
168 GLint np2_fixup_location;
169 GLint color_key_location;
170 const struct ps_np2fixup_info *np2_fixup_info;
173 /* Struct to maintain data about a linked GLSL program */
174 struct glsl_shader_prog_link
176 struct wine_rb_entry program_lookup_entry;
177 struct glsl_vs_program vs;
178 struct glsl_gs_program gs;
179 struct glsl_ps_program ps;
180 GLuint id;
181 DWORD constant_update_mask;
182 UINT constant_version;
185 struct glsl_program_key
187 GLuint vs_id;
188 GLuint gs_id;
189 GLuint ps_id;
192 struct shader_glsl_ctx_priv {
193 const struct vs_compile_args *cur_vs_args;
194 const struct ps_compile_args *cur_ps_args;
195 struct ps_np2fixup_info *cur_np2fixup_info;
198 struct glsl_context_data
200 struct glsl_shader_prog_link *glsl_program;
203 struct glsl_ps_compiled_shader
205 struct ps_compile_args args;
206 struct ps_np2fixup_info np2fixup;
207 GLuint id;
210 struct glsl_vs_compiled_shader
212 struct vs_compile_args args;
213 GLuint id;
216 struct glsl_gs_compiled_shader
218 GLuint id;
221 struct glsl_shader_private
223 union
225 struct glsl_vs_compiled_shader *vs;
226 struct glsl_gs_compiled_shader *gs;
227 struct glsl_ps_compiled_shader *ps;
228 } gl_shaders;
229 UINT num_gl_shaders, shader_array_size;
232 struct glsl_ffp_vertex_shader
234 struct wined3d_ffp_vs_desc desc;
235 GLuint id;
236 struct list linked_programs;
239 struct glsl_ffp_fragment_shader
241 struct ffp_frag_desc entry;
242 GLuint id;
243 struct list linked_programs;
246 struct glsl_ffp_destroy_ctx
248 struct shader_glsl_priv *priv;
249 const struct wined3d_gl_info *gl_info;
252 static const char *debug_gl_shader_type(GLenum type)
254 switch (type)
256 #define WINED3D_TO_STR(u) case u: return #u
257 WINED3D_TO_STR(GL_VERTEX_SHADER);
258 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
259 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
260 #undef WINED3D_TO_STR
261 default:
262 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
266 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
268 switch (type)
270 case WINED3D_SHADER_TYPE_VERTEX:
271 return "vs";
273 case WINED3D_SHADER_TYPE_GEOMETRY:
274 return "gs";
276 case WINED3D_SHADER_TYPE_PIXEL:
277 return "ps";
279 default:
280 FIXME("Unhandled shader type %#x.\n", type);
281 return "unknown";
285 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
286 const struct wined3d_shader_version *version)
288 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version->major >= 4)
289 return "#version 130";
290 else
291 return "#version 120";
294 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
296 char str[4][17];
298 wined3d_ftoa(values[0], str[0]);
299 wined3d_ftoa(values[1], str[1]);
300 wined3d_ftoa(values[2], str[2]);
301 wined3d_ftoa(values[3], str[3]);
302 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
305 static const char *get_info_log_line(const char **ptr)
307 const char *p, *q;
309 p = *ptr;
310 if (!(q = strstr(p, "\n")))
312 if (!*p) return NULL;
313 *ptr += strlen(p);
314 return p;
316 *ptr = q + 1;
318 return p;
321 /* Context activation is done by the caller. */
322 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
324 int length = 0;
325 char *log;
327 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
328 return;
330 if (program)
331 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
332 else
333 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
335 /* A size of 1 is just a null-terminated string, so the log should be bigger than
336 * that if there are errors. */
337 if (length > 1)
339 const char *ptr, *line;
341 log = HeapAlloc(GetProcessHeap(), 0, length);
342 /* The info log is supposed to be zero-terminated, but at least some
343 * versions of fglrx don't terminate the string properly. The reported
344 * length does include the terminator, so explicitly set it to zero
345 * here. */
346 log[length - 1] = 0;
347 if (program)
348 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
349 else
350 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
352 ptr = log;
353 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
355 WARN("Info log received from GLSL shader #%u:\n", id);
356 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
358 else
360 FIXME("Info log received from GLSL shader #%u:\n", id);
361 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
363 HeapFree(GetProcessHeap(), 0, log);
367 /* Context activation is done by the caller. */
368 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
370 const char *ptr, *line;
372 TRACE("Compiling shader object %u.\n", shader);
374 if (TRACE_ON(d3d_shader))
376 ptr = src;
377 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
380 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
381 checkGLcall("glShaderSource");
382 GL_EXTCALL(glCompileShader(shader));
383 checkGLcall("glCompileShader");
384 print_glsl_info_log(gl_info, shader, FALSE);
387 /* Context activation is done by the caller. */
388 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
390 GLint i, shader_count, source_size = -1;
391 GLuint *shaders;
392 char *source = NULL;
394 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
395 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
396 if (!shaders)
398 ERR("Failed to allocate shader array memory.\n");
399 return;
402 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
403 for (i = 0; i < shader_count; ++i)
405 const char *ptr, *line;
406 GLint tmp;
408 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
410 if (source_size < tmp)
412 HeapFree(GetProcessHeap(), 0, source);
414 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
415 if (!source)
417 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
418 HeapFree(GetProcessHeap(), 0, shaders);
419 return;
421 source_size = tmp;
424 FIXME("Shader %u:\n", shaders[i]);
425 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
426 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
427 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
428 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
429 FIXME("\n");
431 ptr = source;
432 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
433 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
434 FIXME("\n");
437 HeapFree(GetProcessHeap(), 0, source);
438 HeapFree(GetProcessHeap(), 0, shaders);
441 /* Context activation is done by the caller. */
442 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
444 GLint tmp;
446 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
447 return;
449 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
450 if (!tmp)
452 FIXME("Program %u link status invalid.\n", program);
453 shader_glsl_dump_program_source(gl_info, program);
456 print_glsl_info_log(gl_info, program, TRUE);
459 /* Context activation is done by the caller. */
460 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
461 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
463 unsigned int mapped_unit;
464 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
465 const char *prefix;
466 unsigned int i, j;
467 GLint name_loc;
469 static const struct
471 enum wined3d_shader_type type;
472 unsigned int base_idx;
473 unsigned int count;
475 sampler_info[] =
477 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
478 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
481 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
483 prefix = shader_glsl_get_prefix(sampler_info[i].type);
485 for (j = 0; j < sampler_info[i].count; ++j)
487 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
488 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
489 if (name_loc == -1)
490 continue;
492 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
493 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
495 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
496 continue;
499 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
500 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
503 checkGLcall("glUniform1i");
504 string_buffer_release(&priv->string_buffers, sampler_name);
507 /* Context activation is done by the caller. */
508 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
509 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
511 unsigned int start = ~0U, end = 0;
512 int stack_idx = 0;
513 unsigned int heap_idx = 1;
514 unsigned int idx;
516 if (heap->entries[heap_idx].version <= version) return;
518 idx = heap->entries[heap_idx].idx;
519 if (constant_locations[idx] != -1)
520 start = end = idx;
521 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
523 while (stack_idx >= 0)
525 /* Note that we fall through to the next case statement. */
526 switch(stack[stack_idx])
528 case HEAP_NODE_TRAVERSE_LEFT:
530 unsigned int left_idx = heap_idx << 1;
531 if (left_idx < heap->size && heap->entries[left_idx].version > version)
533 heap_idx = left_idx;
534 idx = heap->entries[heap_idx].idx;
535 if (constant_locations[idx] != -1)
537 if (start > idx)
538 start = idx;
539 if (end < idx)
540 end = idx;
543 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
544 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
545 break;
549 case HEAP_NODE_TRAVERSE_RIGHT:
551 unsigned int right_idx = (heap_idx << 1) + 1;
552 if (right_idx < heap->size && heap->entries[right_idx].version > version)
554 heap_idx = right_idx;
555 idx = heap->entries[heap_idx].idx;
556 if (constant_locations[idx] != -1)
558 if (start > idx)
559 start = idx;
560 if (end < idx)
561 end = idx;
564 stack[stack_idx++] = HEAP_NODE_POP;
565 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
566 break;
570 case HEAP_NODE_POP:
571 heap_idx >>= 1;
572 --stack_idx;
573 break;
576 if (start <= end)
577 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
578 checkGLcall("walk_constant_heap()");
581 /* Context activation is done by the caller. */
582 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
584 GLfloat clamped_constant[4];
586 if (location == -1) return;
588 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
589 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
590 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
591 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
593 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
596 /* Context activation is done by the caller. */
597 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
598 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
600 int stack_idx = 0;
601 unsigned int heap_idx = 1;
602 unsigned int idx;
604 if (heap->entries[heap_idx].version <= version) return;
606 idx = heap->entries[heap_idx].idx;
607 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
608 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
610 while (stack_idx >= 0)
612 /* Note that we fall through to the next case statement. */
613 switch(stack[stack_idx])
615 case HEAP_NODE_TRAVERSE_LEFT:
617 unsigned int left_idx = heap_idx << 1;
618 if (left_idx < heap->size && heap->entries[left_idx].version > version)
620 heap_idx = left_idx;
621 idx = heap->entries[heap_idx].idx;
622 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
624 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
625 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
626 break;
630 case HEAP_NODE_TRAVERSE_RIGHT:
632 unsigned int right_idx = (heap_idx << 1) + 1;
633 if (right_idx < heap->size && heap->entries[right_idx].version > version)
635 heap_idx = right_idx;
636 idx = heap->entries[heap_idx].idx;
637 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
639 stack[stack_idx++] = HEAP_NODE_POP;
640 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
641 break;
645 case HEAP_NODE_POP:
646 heap_idx >>= 1;
647 --stack_idx;
648 break;
651 checkGLcall("walk_constant_heap_clamped()");
654 /* Context activation is done by the caller. */
655 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
656 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
657 unsigned char *stack, UINT version)
659 const struct wined3d_shader_lconst *lconst;
661 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
662 if (shader->reg_maps.shader_version.major == 1
663 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
664 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
665 else
666 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
668 if (!shader->load_local_constsF)
670 TRACE("No need to load local float constants for this shader\n");
671 return;
674 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
675 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
677 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
679 checkGLcall("glUniform4fv()");
682 /* Context activation is done by the caller. */
683 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
684 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
686 unsigned int i;
687 struct list* ptr;
689 for (i = 0; constants_set; constants_set >>= 1, ++i)
691 if (!(constants_set & 1)) continue;
693 /* We found this uniform name in the program - go ahead and send the data */
694 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
697 /* Load immediate constants */
698 ptr = list_head(&shader->constantsI);
699 while (ptr)
701 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
702 unsigned int idx = lconst->idx;
703 const GLint *values = (const GLint *)lconst->value;
705 /* We found this uniform name in the program - go ahead and send the data */
706 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
707 ptr = list_next(&shader->constantsI, ptr);
709 checkGLcall("glUniform4iv()");
712 /* Context activation is done by the caller. */
713 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
714 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
716 unsigned int i;
717 struct list* ptr;
719 for (i = 0; constants_set; constants_set >>= 1, ++i)
721 if (!(constants_set & 1)) continue;
723 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
726 /* Load immediate constants */
727 ptr = list_head(&shader->constantsB);
728 while (ptr)
730 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
731 unsigned int idx = lconst->idx;
732 const GLint *values = (const GLint *)lconst->value;
734 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
735 ptr = list_next(&shader->constantsB, ptr);
737 checkGLcall("glUniform1iv()");
740 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
742 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
745 /* Context activation is done by the caller (state handler). */
746 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
747 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
749 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
750 UINT fixup = ps->np2_fixup_info->active;
751 UINT i;
753 for (i = 0; fixup; fixup >>= 1, ++i)
755 const struct wined3d_texture *tex = state->textures[i];
756 unsigned char idx = ps->np2_fixup_info->idx[i];
757 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
759 if (!tex)
761 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
762 continue;
765 if (idx % 2)
767 tex_dim[2] = tex->pow2_matrix[0];
768 tex_dim[3] = tex->pow2_matrix[5];
770 else
772 tex_dim[0] = tex->pow2_matrix[0];
773 tex_dim[1] = tex->pow2_matrix[5];
777 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
780 /* Taken and adapted from Mesa. */
781 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
783 float pos, neg, t, det;
784 struct wined3d_matrix temp;
786 /* Calculate the determinant of upper left 3x3 submatrix and
787 * determine if the matrix is singular. */
788 pos = neg = 0.0f;
789 t = in->_11 * in->_22 * in->_33;
790 if (t >= 0.0f)
791 pos += t;
792 else
793 neg += t;
795 t = in->_21 * in->_32 * in->_13;
796 if (t >= 0.0f)
797 pos += t;
798 else
799 neg += t;
800 t = in->_31 * in->_12 * in->_23;
801 if (t >= 0.0f)
802 pos += t;
803 else
804 neg += t;
806 t = -in->_31 * in->_22 * in->_13;
807 if (t >= 0.0f)
808 pos += t;
809 else
810 neg += t;
811 t = -in->_21 * in->_12 * in->_33;
812 if (t >= 0.0f)
813 pos += t;
814 else
815 neg += t;
817 t = -in->_11 * in->_32 * in->_23;
818 if (t >= 0.0f)
819 pos += t;
820 else
821 neg += t;
823 det = pos + neg;
825 if (fabsf(det) < 1e-25f)
826 return FALSE;
828 det = 1.0f / det;
829 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
830 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
831 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
832 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
833 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
834 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
835 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
836 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
837 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
839 *out = temp;
840 return TRUE;
843 static void swap_rows(float **a, float **b)
845 float *tmp = *a;
847 *a = *b;
848 *b = tmp;
851 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
853 float wtmp[4][8];
854 float m0, m1, m2, m3, s;
855 float *r0, *r1, *r2, *r3;
857 r0 = wtmp[0];
858 r1 = wtmp[1];
859 r2 = wtmp[2];
860 r3 = wtmp[3];
862 r0[0] = m->_11;
863 r0[1] = m->_12;
864 r0[2] = m->_13;
865 r0[3] = m->_14;
866 r0[4] = 1.0f;
867 r0[5] = r0[6] = r0[7] = 0.0f;
869 r1[0] = m->_21;
870 r1[1] = m->_22;
871 r1[2] = m->_23;
872 r1[3] = m->_24;
873 r1[5] = 1.0f;
874 r1[4] = r1[6] = r1[7] = 0.0f;
876 r2[0] = m->_31;
877 r2[1] = m->_32;
878 r2[2] = m->_33;
879 r2[3] = m->_34;
880 r2[6] = 1.0f;
881 r2[4] = r2[5] = r2[7] = 0.0f;
883 r3[0] = m->_41;
884 r3[1] = m->_42;
885 r3[2] = m->_43;
886 r3[3] = m->_44;
887 r3[7] = 1.0f;
888 r3[4] = r3[5] = r3[6] = 0.0f;
890 /* Choose pivot - or die. */
891 if (fabsf(r3[0]) > fabsf(r2[0]))
892 swap_rows(&r3, &r2);
893 if (fabsf(r2[0]) > fabsf(r1[0]))
894 swap_rows(&r2, &r1);
895 if (fabsf(r1[0]) > fabsf(r0[0]))
896 swap_rows(&r1, &r0);
897 if (r0[0] == 0.0f)
898 return FALSE;
900 /* Eliminate first variable. */
901 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
902 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
903 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
904 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
905 s = r0[4];
906 if (s != 0.0f)
908 r1[4] -= m1 * s;
909 r2[4] -= m2 * s;
910 r3[4] -= m3 * s;
912 s = r0[5];
913 if (s != 0.0f)
915 r1[5] -= m1 * s;
916 r2[5] -= m2 * s;
917 r3[5] -= m3 * s;
919 s = r0[6];
920 if (s != 0.0f)
922 r1[6] -= m1 * s;
923 r2[6] -= m2 * s;
924 r3[6] -= m3 * s;
926 s = r0[7];
927 if (s != 0.0f)
929 r1[7] -= m1 * s;
930 r2[7] -= m2 * s;
931 r3[7] -= m3 * s;
934 /* Choose pivot - or die. */
935 if (fabsf(r3[1]) > fabsf(r2[1]))
936 swap_rows(&r3, &r2);
937 if (fabsf(r2[1]) > fabsf(r1[1]))
938 swap_rows(&r2, &r1);
939 if (r1[1] == 0.0f)
940 return FALSE;
942 /* Eliminate second variable. */
943 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
944 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
945 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
946 s = r1[4];
947 if (s != 0.0f)
949 r2[4] -= m2 * s;
950 r3[4] -= m3 * s;
952 s = r1[5];
953 if (s != 0.0f)
955 r2[5] -= m2 * s;
956 r3[5] -= m3 * s;
958 s = r1[6];
959 if (s != 0.0f)
961 r2[6] -= m2 * s;
962 r3[6] -= m3 * s;
964 s = r1[7];
965 if (s != 0.0f)
967 r2[7] -= m2 * s;
968 r3[7] -= m3 * s;
971 /* Choose pivot - or die. */
972 if (fabsf(r3[2]) > fabsf(r2[2]))
973 swap_rows(&r3, &r2);
974 if (r2[2] == 0.0f)
975 return FALSE;
977 /* Eliminate third variable. */
978 m3 = r3[2] / r2[2];
979 r3[3] -= m3 * r2[3];
980 r3[4] -= m3 * r2[4];
981 r3[5] -= m3 * r2[5];
982 r3[6] -= m3 * r2[6];
983 r3[7] -= m3 * r2[7];
985 /* Last check. */
986 if (r3[3] == 0.0f)
987 return FALSE;
989 /* Back substitute row 3. */
990 s = 1.0f / r3[3];
991 r3[4] *= s;
992 r3[5] *= s;
993 r3[6] *= s;
994 r3[7] *= s;
996 /* Back substitute row 2. */
997 m2 = r2[3];
998 s = 1.0f / r2[2];
999 r2[4] = s * (r2[4] - r3[4] * m2);
1000 r2[5] = s * (r2[5] - r3[5] * m2);
1001 r2[6] = s * (r2[6] - r3[6] * m2);
1002 r2[7] = s * (r2[7] - r3[7] * m2);
1003 m1 = r1[3];
1004 r1[4] -= r3[4] * m1;
1005 r1[5] -= r3[5] * m1;
1006 r1[6] -= r3[6] * m1;
1007 r1[7] -= r3[7] * m1;
1008 m0 = r0[3];
1009 r0[4] -= r3[4] * m0;
1010 r0[5] -= r3[5] * m0;
1011 r0[6] -= r3[6] * m0;
1012 r0[7] -= r3[7] * m0;
1014 /* Back substitute row 1. */
1015 m1 = r1[2];
1016 s = 1.0f / r1[1];
1017 r1[4] = s * (r1[4] - r2[4] * m1);
1018 r1[5] = s * (r1[5] - r2[5] * m1);
1019 r1[6] = s * (r1[6] - r2[6] * m1);
1020 r1[7] = s * (r1[7] - r2[7] * m1);
1021 m0 = r0[2];
1022 r0[4] -= r2[4] * m0;
1023 r0[5] -= r2[5] * m0;
1024 r0[6] -= r2[6] * m0;
1025 r0[7] -= r2[7] * m0;
1027 /* Back substitute row 0. */
1028 m0 = r0[1];
1029 s = 1.0f / r0[0];
1030 r0[4] = s * (r0[4] - r1[4] * m0);
1031 r0[5] = s * (r0[5] - r1[5] * m0);
1032 r0[6] = s * (r0[6] - r1[6] * m0);
1033 r0[7] = s * (r0[7] - r1[7] * m0);
1035 out->_11 = r0[4];
1036 out->_12 = r0[5];
1037 out->_13 = r0[6];
1038 out->_14 = r0[7];
1039 out->_21 = r1[4];
1040 out->_22 = r1[5];
1041 out->_23 = r1[6];
1042 out->_24 = r1[7];
1043 out->_31 = r2[4];
1044 out->_32 = r2[5];
1045 out->_33 = r2[6];
1046 out->_34 = r2[7];
1047 out->_41 = r3[4];
1048 out->_42 = r3[5];
1049 out->_43 = r3[6];
1050 out->_44 = r3[7];
1052 return TRUE;
1055 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1056 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1058 const struct wined3d_gl_info *gl_info = context->gl_info;
1059 float mat[3 * 3];
1060 struct wined3d_matrix mv;
1061 unsigned int i, j;
1063 get_modelview_matrix(context, state, &mv);
1064 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1065 invert_matrix_3d(&mv, &mv);
1066 else
1067 invert_matrix(&mv, &mv);
1068 /* Tests show that singular modelview matrices are used unchanged as normal
1069 * matrices on D3D3 and older. There seems to be no clearly consistent
1070 * behavior on newer D3D versions so always follow older ddraw behavior. */
1071 for (i = 0; i < 3; ++i)
1072 for (j = 0; j < 3; ++j)
1073 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1075 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1076 checkGLcall("glUniformMatrix3fv");
1079 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1080 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1082 const struct wined3d_gl_info *gl_info = context->gl_info;
1083 struct wined3d_matrix mat;
1085 if (tex >= MAX_TEXTURES)
1086 return;
1087 if (prog->vs.texture_matrix_location[tex] == -1)
1088 return;
1090 get_texture_matrix(context, state, tex, &mat);
1091 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1092 checkGLcall("glUniformMatrix4fv");
1095 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1096 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1098 const struct wined3d_gl_info *gl_info = context->gl_info;
1100 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1102 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1103 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1105 else
1107 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1109 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1111 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1112 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1113 GL_EXTCALL(glUniform4fv(prog->vs.material_emission_location, 1, &state->material.emissive.r));
1114 checkGLcall("setting FFP material uniforms");
1117 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1118 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1120 const struct wined3d_gl_info *gl_info = context->gl_info;
1121 float col[4];
1123 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1124 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1125 checkGLcall("glUniform3fv");
1128 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1129 const struct wined3d_matrix *src2)
1131 struct wined3d_vec4 temp;
1133 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1134 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1135 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1136 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1138 *dest = temp;
1141 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1142 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1144 const struct wined3d_gl_info *gl_info = context->gl_info;
1145 const struct wined3d_light_info *light_info = state->lights[light];
1146 struct wined3d_vec4 vec4;
1147 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1149 if (!light_info)
1150 return;
1152 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1153 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1154 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1156 switch (light_info->OriginalParms.type)
1158 case WINED3D_LIGHT_POINT:
1159 multiply_vector_matrix(&vec4, &light_info->position, view);
1160 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1161 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1162 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1163 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1164 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1165 break;
1167 case WINED3D_LIGHT_SPOT:
1168 multiply_vector_matrix(&vec4, &light_info->position, view);
1169 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1171 multiply_vector_matrix(&vec4, &light_info->direction, view);
1172 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1174 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1175 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1176 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1177 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1178 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1179 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1180 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1181 break;
1183 case WINED3D_LIGHT_DIRECTIONAL:
1184 multiply_vector_matrix(&vec4, &light_info->direction, view);
1185 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1186 break;
1188 default:
1189 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1191 checkGLcall("setting FFP lights uniforms");
1194 /* Context activation is done by the caller (state handler). */
1195 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1196 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1198 struct wined3d_color float_key;
1199 const struct wined3d_texture *texture = state->textures[0];
1201 wined3d_format_convert_color_to_float(texture->resource.format, NULL,
1202 texture->async.src_blt_color_key.color_space_high_value, &float_key);
1203 GL_EXTCALL(glUniform4fv(ps->color_key_location, 1, &float_key.r));
1206 /* Context activation is done by the caller (state handler). */
1207 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1208 const struct wined3d_state *state)
1210 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1211 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1212 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1213 const struct wined3d_gl_info *gl_info = context->gl_info;
1214 struct shader_glsl_priv *priv = shader_priv;
1215 float position_fixup[4];
1216 DWORD update_mask = 0;
1218 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1219 UINT constant_version;
1220 int i;
1222 if (!prog) {
1223 /* No GLSL program set - nothing to do. */
1224 return;
1226 constant_version = prog->constant_version;
1227 update_mask = context->constant_update_mask & prog->constant_update_mask;
1229 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1230 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1231 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1233 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1234 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1235 vshader->reg_maps.integer_constants);
1237 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1238 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1239 vshader->reg_maps.boolean_constants);
1241 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1243 shader_get_position_fixup(context, state, position_fixup);
1244 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1245 checkGLcall("glUniform4fv");
1248 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1250 struct wined3d_matrix mat;
1252 get_modelview_matrix(context, state, &mat);
1253 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location, 1, FALSE, &mat._11));
1254 checkGLcall("glUniformMatrix4fv");
1256 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1259 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1261 struct wined3d_matrix projection;
1263 get_projection_matrix(context, state, &projection);
1264 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1265 checkGLcall("glUniformMatrix4fv");
1268 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1270 for (i = 0; i < MAX_TEXTURES; ++i)
1271 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1274 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1275 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1277 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1279 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1280 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1281 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1284 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1285 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1286 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1288 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1289 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1290 pshader->reg_maps.integer_constants);
1292 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1293 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1294 pshader->reg_maps.boolean_constants);
1296 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1298 for (i = 0; i < MAX_TEXTURES; ++i)
1300 if (prog->ps.bumpenv_mat_location[i] == -1)
1301 continue;
1303 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1304 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1306 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1308 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1309 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1310 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1311 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1315 checkGLcall("bump env uniforms");
1318 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1320 float correction_params[] =
1322 /* position is window relative, not viewport relative */
1323 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1324 context->render_offscreen ? 1.0f : -1.0f,
1325 0.0f,
1326 0.0f,
1329 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1332 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1333 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1334 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1335 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1337 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1339 float col[4];
1341 if (prog->ps.tex_factor_location != -1)
1343 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1344 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1347 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1348 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1349 else
1350 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1352 for (i = 0; i < MAX_TEXTURES; ++i)
1354 if (prog->ps.tss_constant_location[i] == -1)
1355 continue;
1357 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1358 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1361 checkGLcall("fixed function uniforms");
1364 if (priv->next_constant_version == UINT_MAX)
1366 TRACE("Max constant version reached, resetting to 0.\n");
1367 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1368 priv->next_constant_version = 1;
1370 else
1372 prog->constant_version = priv->next_constant_version++;
1376 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1378 struct constant_entry *entries = heap->entries;
1379 unsigned int *positions = heap->positions;
1380 unsigned int heap_idx, parent_idx;
1382 if (!heap->contained[idx])
1384 heap_idx = heap->size++;
1385 heap->contained[idx] = TRUE;
1387 else
1389 heap_idx = positions[idx];
1392 while (heap_idx > 1)
1394 parent_idx = heap_idx >> 1;
1396 if (new_version <= entries[parent_idx].version) break;
1398 entries[heap_idx] = entries[parent_idx];
1399 positions[entries[parent_idx].idx] = heap_idx;
1400 heap_idx = parent_idx;
1403 entries[heap_idx].version = new_version;
1404 entries[heap_idx].idx = idx;
1405 positions[idx] = heap_idx;
1408 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1410 struct shader_glsl_priv *priv = device->shader_priv;
1411 struct constant_heap *heap = &priv->vconst_heap;
1412 UINT i;
1414 for (i = start; i < count + start; ++i)
1416 update_heap_entry(heap, i, priv->next_constant_version);
1419 for (i = 0; i < device->context_count; ++i)
1421 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1425 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1427 struct shader_glsl_priv *priv = device->shader_priv;
1428 struct constant_heap *heap = &priv->pconst_heap;
1429 UINT i;
1431 for (i = start; i < count + start; ++i)
1433 update_heap_entry(heap, i, priv->next_constant_version);
1436 for (i = 0; i < device->context_count; ++i)
1438 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1442 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1444 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1445 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1446 if(shader_major > 3) return ret;
1448 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1449 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1450 return ret;
1453 /** Generate the variable & register declarations for the GLSL output target */
1454 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1455 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1456 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1458 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1459 const struct wined3d_state *state = &shader->device->state;
1460 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1461 const struct wined3d_gl_info *gl_info = context->gl_info;
1462 const struct wined3d_fb_state *fb = &shader->device->fb;
1463 unsigned int i, extra_constants_needed = 0;
1464 const struct wined3d_shader_lconst *lconst;
1465 const char *prefix;
1466 DWORD map;
1468 prefix = shader_glsl_get_prefix(version->type);
1470 /* Prototype the subroutines */
1471 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1473 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1476 /* Declare the constants (aka uniforms) */
1477 if (shader->limits->constant_float > 0)
1479 unsigned max_constantsF;
1481 /* Unless the shader uses indirect addressing, always declare the
1482 * maximum array size and ignore that we need some uniforms privately.
1483 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1484 * and immediate values, still declare VC[256]. If the shader needs
1485 * more uniforms than we have it won't work in any case. If it uses
1486 * less, the compiler will figure out which uniforms are really used
1487 * and strip them out. This allows a shader to use c255 on a dx9 card,
1488 * as long as it doesn't also use all the other constants.
1490 * If the shader uses indirect addressing the compiler must assume
1491 * that all declared uniforms are used. In this case, declare only the
1492 * amount that we're assured to have.
1494 * Thus we run into problems in these two cases:
1495 * 1) The shader really uses more uniforms than supported.
1496 * 2) The shader uses indirect addressing, less constants than
1497 * supported, but uses a constant index > #supported consts. */
1498 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1500 /* No indirect addressing here. */
1501 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1503 else
1505 if (reg_maps->usesrelconstF)
1507 /* Subtract the other potential uniforms from the max
1508 * available (bools, ints, and 1 row of projection matrix).
1509 * Subtract another uniform for immediate values, which have
1510 * to be loaded via uniform by the driver as well. The shader
1511 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1512 * shader code, so one vec4 should be enough. (Unfortunately
1513 * the Nvidia driver doesn't store 128 and -128 in one float).
1515 * Writing gl_ClipVertex requires one uniform for each
1516 * clipplane as well. */
1517 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1518 if(ctx_priv->cur_vs_args->clip_enabled)
1520 max_constantsF -= gl_info->limits.clipplanes;
1522 max_constantsF -= count_bits(reg_maps->integer_constants);
1523 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1524 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1525 * for now take this into account when calculating the number of available constants
1527 max_constantsF -= count_bits(reg_maps->boolean_constants);
1528 /* Set by driver quirks in directx.c */
1529 max_constantsF -= gl_info->reserved_glsl_constants;
1531 if (max_constantsF < shader->limits->constant_float)
1533 static unsigned int once;
1535 if (!once++)
1536 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1537 " it may not render correctly.\n");
1538 else
1539 WARN("The hardware does not support enough uniform components to run this shader.\n");
1542 else
1544 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1547 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1548 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1551 /* Always declare the full set of constants, the compiler can remove the
1552 * unused ones because d3d doesn't (yet) support indirect int and bool
1553 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1554 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1555 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1557 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1558 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1560 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1562 if (reg_maps->cb_sizes[i])
1563 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1564 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1567 /* Declare texture samplers */
1568 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1570 struct wined3d_shader_sampler_map_entry *entry;
1571 BOOL shadow_sampler, tex_rect;
1572 const char *sampler_type;
1574 entry = &reg_maps->sampler_map.entries[i];
1576 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1578 ERR("Invalid resource index %u.\n", entry->resource_idx);
1579 continue;
1582 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << entry->sampler_idx));
1583 switch (reg_maps->resource_info[entry->resource_idx].type)
1585 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1586 if (shadow_sampler)
1587 sampler_type = "sampler1DShadow";
1588 else
1589 sampler_type = "sampler1D";
1590 break;
1592 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1593 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1594 && (ps_args->np2_fixup & (1 << entry->resource_idx))
1595 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1596 if (shadow_sampler)
1598 if (tex_rect)
1599 sampler_type = "sampler2DRectShadow";
1600 else
1601 sampler_type = "sampler2DShadow";
1603 else
1605 if (tex_rect)
1606 sampler_type = "sampler2DRect";
1607 else
1608 sampler_type = "sampler2D";
1610 break;
1612 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1613 if (shadow_sampler)
1614 FIXME("Unsupported 3D shadow sampler.\n");
1615 sampler_type = "sampler3D";
1616 break;
1618 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1619 if (shadow_sampler)
1620 FIXME("Unsupported Cube shadow sampler.\n");
1621 sampler_type = "samplerCube";
1622 break;
1624 default:
1625 sampler_type = "unsupported_sampler";
1626 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1627 break;
1629 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1632 /* Declare uniforms for NP2 texcoord fixup:
1633 * This is NOT done inside the loop that declares the texture samplers
1634 * since the NP2 fixup code is currently only used for the GeforceFX
1635 * series and when forcing the ARB_npot extension off. Modern cards just
1636 * skip the code anyway, so put it inside a separate loop. */
1637 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1639 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1640 UINT cur = 0;
1642 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1643 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1644 * samplerNP2Fixup stores texture dimensions and is updated through
1645 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1647 for (i = 0; i < shader->limits->sampler; ++i)
1649 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1 << i)))
1650 continue;
1652 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1654 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1655 continue;
1658 fixup->idx[i] = cur++;
1661 fixup->num_consts = (cur + 1) >> 1;
1662 fixup->active = ps_args->np2_fixup;
1663 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1666 /* Declare address variables */
1667 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1669 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1672 /* Declare texture coordinate temporaries and initialize them */
1673 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1675 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1678 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1680 for (i = 0; i < shader->input_signature.element_count; ++i)
1682 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1683 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1684 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1685 prefix, e->register_idx);
1686 else
1687 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1690 shader_addline(buffer, "uniform vec4 posFixup;\n");
1691 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1693 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1695 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1697 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1699 if (version->major >= 3)
1701 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1703 if (use_vs(state))
1704 shader_addline(buffer, "varying vec4 %s_link[%u];\n", prefix, in_count);
1705 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1708 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1710 if (!(map & 1))
1711 continue;
1713 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1715 if (reg_maps->luminanceparams & (1 << i))
1717 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1718 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1719 extra_constants_needed++;
1722 extra_constants_needed++;
1725 if (ps_args->srgb_correction)
1727 shader_addline(buffer, "const vec4 srgb_const0 = ");
1728 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1729 shader_addline(buffer, ";\n");
1730 shader_addline(buffer, "const vec4 srgb_const1 = ");
1731 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1732 shader_addline(buffer, ";\n");
1734 if (reg_maps->vpos || reg_maps->usesdsy)
1736 if (shader->limits->constant_float + extra_constants_needed
1737 + 1 < gl_info->limits.glsl_ps_float_constants)
1739 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1740 extra_constants_needed++;
1742 else
1744 float ycorrection[] =
1746 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1747 context->render_offscreen ? 1.0f : -1.0f,
1748 0.0f,
1749 0.0f,
1752 /* This happens because we do not have proper tracking of the
1753 * constant registers that are actually used, only the max
1754 * limit of the shader version. */
1755 FIXME("Cannot find a free uniform for vpos correction params\n");
1756 shader_addline(buffer, "const vec4 ycorrection = ");
1757 shader_glsl_append_imm_vec4(buffer, ycorrection);
1758 shader_addline(buffer, ";\n");
1760 shader_addline(buffer, "vec4 vpos;\n");
1764 /* Declare output register temporaries */
1765 if (shader->limits->packed_output)
1766 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1768 /* Declare temporary variables */
1769 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1771 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1774 /* Declare loop registers aLx */
1775 if (version->major < 4)
1777 for (i = 0; i < reg_maps->loop_depth; ++i)
1779 shader_addline(buffer, "int aL%u;\n", i);
1780 shader_addline(buffer, "int tmpInt%u;\n", i);
1784 /* Temporary variables for matrix operations */
1785 shader_addline(buffer, "vec4 tmp0;\n");
1786 shader_addline(buffer, "vec4 tmp1;\n");
1788 if (!shader->load_local_constsF)
1790 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1792 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
1793 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
1794 shader_addline(buffer, ";\n");
1798 /* Start the main program. */
1799 shader_addline(buffer, "void main()\n{\n");
1801 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1802 * add approximately 0.5. This causes off-by-one problems as spotted by
1803 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1804 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1805 * causes precision troubles when we just subtract 0.5.
1807 * To deal with that, just floor() the position. This will eliminate the
1808 * fraction on all cards.
1810 * TODO: Test how this behaves with multisampling.
1812 * An advantage of floor is that it works even if the driver doesn't add
1813 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1814 * to return in gl_FragCoord, even though coordinates specify the pixel
1815 * centers instead of the pixel corners. This code will behave correctly
1816 * on drivers that returns integer values. */
1817 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1819 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
1820 shader_addline(buffer,
1821 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1822 else
1823 shader_addline(buffer,
1824 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
1828 /*****************************************************************************
1829 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1831 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1832 ****************************************************************************/
1834 /* Prototypes */
1835 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1836 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1838 /** Used for opcode modifiers - They multiply the result by the specified amount */
1839 static const char * const shift_glsl_tab[] = {
1840 "", /* 0 (none) */
1841 "2.0 * ", /* 1 (x2) */
1842 "4.0 * ", /* 2 (x4) */
1843 "8.0 * ", /* 3 (x8) */
1844 "16.0 * ", /* 4 (x16) */
1845 "32.0 * ", /* 5 (x32) */
1846 "", /* 6 (x64) */
1847 "", /* 7 (x128) */
1848 "", /* 8 (d256) */
1849 "", /* 9 (d128) */
1850 "", /* 10 (d64) */
1851 "", /* 11 (d32) */
1852 "0.0625 * ", /* 12 (d16) */
1853 "0.125 * ", /* 13 (d8) */
1854 "0.25 * ", /* 14 (d4) */
1855 "0.5 * " /* 15 (d2) */
1858 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1859 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1860 const char *in_reg, const char *in_regswizzle, char *out_str)
1862 out_str[0] = 0;
1864 switch (src_modifier)
1866 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1867 case WINED3DSPSM_DW:
1868 case WINED3DSPSM_NONE:
1869 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1870 break;
1871 case WINED3DSPSM_NEG:
1872 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1873 break;
1874 case WINED3DSPSM_NOT:
1875 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1876 break;
1877 case WINED3DSPSM_BIAS:
1878 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1879 break;
1880 case WINED3DSPSM_BIASNEG:
1881 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1882 break;
1883 case WINED3DSPSM_SIGN:
1884 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1885 break;
1886 case WINED3DSPSM_SIGNNEG:
1887 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1888 break;
1889 case WINED3DSPSM_COMP:
1890 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1891 break;
1892 case WINED3DSPSM_X2:
1893 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1894 break;
1895 case WINED3DSPSM_X2NEG:
1896 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1897 break;
1898 case WINED3DSPSM_ABS:
1899 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1900 break;
1901 case WINED3DSPSM_ABSNEG:
1902 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1903 break;
1904 default:
1905 FIXME("Unhandled modifier %u\n", src_modifier);
1906 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1910 /** Writes the GLSL variable name that corresponds to the register that the
1911 * DX opcode parameter is trying to access */
1912 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1913 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1915 /* oPos, oFog and oPts in D3D */
1916 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1918 const struct wined3d_shader *shader = ins->ctx->shader;
1919 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1920 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1921 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1922 const char *prefix = shader_glsl_get_prefix(version->type);
1923 struct glsl_src_param rel_param0, rel_param1;
1924 char imm_str[4][17];
1926 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
1927 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
1928 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
1929 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
1930 *is_color = FALSE;
1932 switch (reg->type)
1934 case WINED3DSPR_TEMP:
1935 sprintf(register_name, "R%u", reg->idx[0].offset);
1936 break;
1938 case WINED3DSPR_INPUT:
1939 /* vertex shaders */
1940 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1942 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1943 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1944 *is_color = TRUE;
1945 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1946 break;
1949 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1951 if (reg->idx[0].rel_addr)
1953 if (reg->idx[1].rel_addr)
1954 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
1955 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1956 else
1957 sprintf(register_name, "gs_in[%s + %u][%u]",
1958 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
1960 else if (reg->idx[1].rel_addr)
1961 sprintf(register_name, "gs_in[%u][%s + %u]",
1962 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
1963 else
1964 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
1965 break;
1968 /* pixel shaders >= 3.0 */
1969 if (version->major >= 3)
1971 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1972 unsigned int in_count = vec4_varyings(version->major, gl_info);
1974 if (reg->idx[0].rel_addr)
1976 /* Removing a + 0 would be an obvious optimization, but
1977 * OS X doesn't see the NOP operation there. */
1978 if (idx)
1980 if (shader->u.ps.declared_in_count > in_count)
1982 sprintf(register_name,
1983 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1984 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
1985 prefix, rel_param0.param_str, idx);
1987 else
1989 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
1992 else
1994 if (shader->u.ps.declared_in_count > in_count)
1996 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1997 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
1998 prefix, rel_param0.param_str);
2000 else
2002 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2006 else
2008 if (idx == in_count) sprintf(register_name, "gl_Color");
2009 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2010 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2013 else
2015 if (!reg->idx[0].offset)
2016 strcpy(register_name, "gl_Color");
2017 else
2018 strcpy(register_name, "gl_SecondaryColor");
2019 break;
2021 break;
2023 case WINED3DSPR_CONST:
2025 /* Relative addressing */
2026 if (reg->idx[0].rel_addr)
2028 if (reg->idx[0].offset)
2029 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2030 else
2031 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2033 else
2035 if (shader_constant_is_local(shader, reg->idx[0].offset))
2036 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2037 else
2038 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2041 break;
2043 case WINED3DSPR_CONSTINT:
2044 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2045 break;
2047 case WINED3DSPR_CONSTBOOL:
2048 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2049 break;
2051 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2052 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2053 sprintf(register_name, "T%u", reg->idx[0].offset);
2054 else
2055 sprintf(register_name, "A%u", reg->idx[0].offset);
2056 break;
2058 case WINED3DSPR_LOOP:
2059 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2060 break;
2062 case WINED3DSPR_SAMPLER:
2063 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2064 break;
2066 case WINED3DSPR_COLOROUT:
2067 if (reg->idx[0].offset >= gl_info->limits.buffers)
2068 WARN("Write to render target %u, only %d supported.\n",
2069 reg->idx[0].offset, gl_info->limits.buffers);
2071 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2072 break;
2074 case WINED3DSPR_RASTOUT:
2075 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2076 break;
2078 case WINED3DSPR_DEPTHOUT:
2079 sprintf(register_name, "gl_FragDepth");
2080 break;
2082 case WINED3DSPR_ATTROUT:
2083 if (!reg->idx[0].offset)
2084 sprintf(register_name, "%s_out[8]", prefix);
2085 else
2086 sprintf(register_name, "%s_out[9]", prefix);
2087 break;
2089 case WINED3DSPR_TEXCRDOUT:
2090 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2091 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2092 break;
2094 case WINED3DSPR_MISCTYPE:
2095 if (!reg->idx[0].offset)
2097 /* vPos */
2098 sprintf(register_name, "vpos");
2100 else if (reg->idx[0].offset == 1)
2102 /* Note that gl_FrontFacing is a bool, while vFace is
2103 * a float for which the sign determines front/back */
2104 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2106 else
2108 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2109 sprintf(register_name, "unrecognized_register");
2111 break;
2113 case WINED3DSPR_IMMCONST:
2114 switch (reg->immconst_type)
2116 case WINED3D_IMMCONST_SCALAR:
2117 switch (reg->data_type)
2119 case WINED3D_DATA_FLOAT:
2120 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2121 break;
2122 case WINED3D_DATA_INT:
2123 sprintf(register_name, "%#x", reg->immconst_data[0]);
2124 break;
2125 case WINED3D_DATA_RESOURCE:
2126 case WINED3D_DATA_SAMPLER:
2127 case WINED3D_DATA_UINT:
2128 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2129 break;
2130 default:
2131 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2132 break;
2134 break;
2136 case WINED3D_IMMCONST_VEC4:
2137 switch (reg->data_type)
2139 case WINED3D_DATA_FLOAT:
2140 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2141 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2142 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2143 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2144 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2145 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2146 break;
2147 case WINED3D_DATA_INT:
2148 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2149 reg->immconst_data[0], reg->immconst_data[1],
2150 reg->immconst_data[2], reg->immconst_data[3]);
2151 break;
2152 case WINED3D_DATA_RESOURCE:
2153 case WINED3D_DATA_SAMPLER:
2154 case WINED3D_DATA_UINT:
2155 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2156 reg->immconst_data[0], reg->immconst_data[1],
2157 reg->immconst_data[2], reg->immconst_data[3]);
2158 break;
2159 default:
2160 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2161 break;
2163 break;
2165 default:
2166 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2167 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2169 break;
2171 case WINED3DSPR_CONSTBUFFER:
2172 if (reg->idx[1].rel_addr)
2173 sprintf(register_name, "%s_cb%u[%s + %u]",
2174 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2175 else
2176 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2177 break;
2179 case WINED3DSPR_PRIMID:
2180 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2181 break;
2183 default:
2184 FIXME("Unhandled register type %#x.\n", reg->type);
2185 sprintf(register_name, "unrecognized_register");
2186 break;
2190 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2192 *str++ = '.';
2193 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2194 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2195 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2196 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2197 *str = '\0';
2200 /* Get the GLSL write mask for the destination register */
2201 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2203 DWORD mask = param->write_mask;
2205 if (shader_is_scalar(&param->reg))
2207 mask = WINED3DSP_WRITEMASK_0;
2208 *write_mask = '\0';
2210 else
2212 shader_glsl_write_mask_to_str(mask, write_mask);
2215 return mask;
2218 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2219 unsigned int size = 0;
2221 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2222 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2223 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2224 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2226 return size;
2229 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2231 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2232 * but addressed as "rgba". To fix this we need to swap the register's x
2233 * and z components. */
2234 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2236 *str++ = '.';
2237 /* swizzle bits fields: wwzzyyxx */
2238 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2239 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2240 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2241 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2242 *str = '\0';
2245 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2246 BOOL fixup, DWORD mask, char *swizzle_str)
2248 if (shader_is_scalar(&param->reg))
2249 *swizzle_str = '\0';
2250 else
2251 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2254 /* From a given parameter token, generate the corresponding GLSL string.
2255 * Also, return the actual register name and swizzle in case the
2256 * caller needs this information as well. */
2257 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2258 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2260 BOOL is_color = FALSE;
2261 char swizzle_str[6];
2263 glsl_src->reg_name[0] = '\0';
2264 glsl_src->param_str[0] = '\0';
2265 swizzle_str[0] = '\0';
2267 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2268 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2270 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2272 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2274 else
2276 char reg_name[200];
2278 switch (wined3d_src->reg.data_type)
2280 case WINED3D_DATA_FLOAT:
2281 sprintf(reg_name, "%s", glsl_src->reg_name);
2282 break;
2283 case WINED3D_DATA_INT:
2284 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2285 break;
2286 case WINED3D_DATA_RESOURCE:
2287 case WINED3D_DATA_SAMPLER:
2288 case WINED3D_DATA_UINT:
2289 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2290 break;
2291 default:
2292 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2293 sprintf(reg_name, "%s", glsl_src->reg_name);
2294 break;
2297 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2301 /* From a given parameter token, generate the corresponding GLSL string.
2302 * Also, return the actual register name and swizzle in case the
2303 * caller needs this information as well. */
2304 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2305 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2307 BOOL is_color = FALSE;
2309 glsl_dst->mask_str[0] = '\0';
2310 glsl_dst->reg_name[0] = '\0';
2312 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2313 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2316 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2317 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2318 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2319 enum wined3d_data_type data_type)
2321 struct glsl_dst_param glsl_dst;
2322 DWORD mask;
2324 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2326 switch (data_type)
2328 case WINED3D_DATA_FLOAT:
2329 shader_addline(buffer, "%s%s = %s(",
2330 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2331 break;
2332 case WINED3D_DATA_INT:
2333 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2334 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2335 break;
2336 case WINED3D_DATA_RESOURCE:
2337 case WINED3D_DATA_SAMPLER:
2338 case WINED3D_DATA_UINT:
2339 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2340 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2341 break;
2342 default:
2343 FIXME("Unhandled data type %#x.\n", data_type);
2344 shader_addline(buffer, "%s%s = %s(",
2345 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2346 break;
2350 return mask;
2353 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2354 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2356 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2359 /** Process GLSL instruction modifiers */
2360 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2362 struct glsl_dst_param dst_param;
2363 DWORD modifiers;
2365 if (!ins->dst_count) return;
2367 modifiers = ins->dst[0].modifiers;
2368 if (!modifiers) return;
2370 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2372 if (modifiers & WINED3DSPDM_SATURATE)
2374 /* _SAT means to clamp the value of the register to between 0 and 1 */
2375 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2376 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2379 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2381 FIXME("_centroid modifier not handled\n");
2384 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2386 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2390 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2392 switch (op)
2394 case WINED3D_SHADER_REL_OP_GT: return ">";
2395 case WINED3D_SHADER_REL_OP_EQ: return "==";
2396 case WINED3D_SHADER_REL_OP_GE: return ">=";
2397 case WINED3D_SHADER_REL_OP_LT: return "<";
2398 case WINED3D_SHADER_REL_OP_NE: return "!=";
2399 case WINED3D_SHADER_REL_OP_LE: return "<=";
2400 default:
2401 FIXME("Unrecognized operator %#x.\n", op);
2402 return "(\?\?)";
2406 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2407 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2409 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2410 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2411 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2412 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << resource_idx));
2413 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2414 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2415 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2416 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2418 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2420 /* Note that there's no such thing as a projected cube texture. */
2421 switch (resource_type)
2423 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2424 if (shadow)
2426 if (lod)
2428 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
2430 else if (grad)
2432 if (gl_info->supported[EXT_GPU_SHADER4])
2433 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
2434 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2435 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
2436 else
2438 FIXME("Unsupported 1D shadow grad function.\n");
2439 sample_function->name = "unsupported1DGrad";
2442 else
2444 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
2446 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2448 else
2450 if (lod)
2452 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
2454 else if (grad)
2456 if (gl_info->supported[EXT_GPU_SHADER4])
2457 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
2458 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2459 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
2460 else
2462 FIXME("Unsupported 1D grad function.\n");
2463 sample_function->name = "unsupported1DGrad";
2466 else
2468 sample_function->name = projected ? "texture1DProj" : "texture1D";
2470 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
2472 break;
2474 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2475 if (shadow)
2477 if (texrect)
2479 if (lod)
2481 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
2483 else if (grad)
2485 if (gl_info->supported[EXT_GPU_SHADER4])
2486 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
2487 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2488 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
2489 else
2491 FIXME("Unsupported RECT shadow grad function.\n");
2492 sample_function->name = "unsupported2DRectGrad";
2495 else
2497 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
2500 else
2502 if (lod)
2504 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
2506 else if (grad)
2508 if (gl_info->supported[EXT_GPU_SHADER4])
2509 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
2510 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2511 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
2512 else
2514 FIXME("Unsupported 2D shadow grad function.\n");
2515 sample_function->name = "unsupported2DGrad";
2518 else
2520 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
2523 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2525 else
2527 if (texrect)
2529 if (lod)
2531 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
2533 else if (grad)
2535 if (gl_info->supported[EXT_GPU_SHADER4])
2536 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
2537 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2538 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
2539 else
2541 FIXME("Unsupported RECT grad function.\n");
2542 sample_function->name = "unsupported2DRectGrad";
2545 else
2547 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
2550 else
2552 if (lod)
2554 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
2556 else if (grad)
2558 if (gl_info->supported[EXT_GPU_SHADER4])
2559 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
2560 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2561 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
2562 else
2564 FIXME("Unsupported 2D grad function.\n");
2565 sample_function->name = "unsupported2DGrad";
2568 else
2570 sample_function->name = projected ? "texture2DProj" : "texture2D";
2573 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
2575 break;
2577 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2578 if (shadow)
2580 FIXME("Unsupported 3D shadow function.\n");
2581 sample_function->name = "unsupported3DShadow";
2582 sample_function->coord_mask = 0;
2584 else
2586 if (lod)
2588 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2590 else if (grad)
2592 if (gl_info->supported[EXT_GPU_SHADER4])
2593 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2594 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2595 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2596 else
2598 FIXME("Unsupported 3D grad function.\n");
2599 sample_function->name = "unsupported3DGrad";
2602 else
2604 sample_function->name = projected ? "texture3DProj" : "texture3D";
2606 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2608 break;
2610 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2611 if (shadow)
2613 FIXME("Unsupported Cube shadow function.\n");
2614 sample_function->name = "unsupportedCubeShadow";
2615 sample_function->coord_mask = 0;
2617 else
2619 if (lod)
2621 sample_function->name = "textureCubeLod";
2623 else if (grad)
2625 if (gl_info->supported[EXT_GPU_SHADER4])
2626 sample_function->name = "textureCubeGrad";
2627 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2628 sample_function->name = "textureCubeGradARB";
2629 else
2631 FIXME("Unsupported Cube grad function.\n");
2632 sample_function->name = "unsupportedCubeGrad";
2635 else
2637 sample_function->name = "textureCube";
2639 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2641 break;
2643 default:
2644 sample_function->name = "";
2645 sample_function->coord_mask = 0;
2646 FIXME("Unhandled resource type %#x.\n", resource_type);
2647 break;
2651 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2652 BOOL sign_fixup, enum fixup_channel_source channel_source)
2654 switch(channel_source)
2656 case CHANNEL_SOURCE_ZERO:
2657 strcat(arguments, "0.0");
2658 break;
2660 case CHANNEL_SOURCE_ONE:
2661 strcat(arguments, "1.0");
2662 break;
2664 case CHANNEL_SOURCE_X:
2665 strcat(arguments, reg_name);
2666 strcat(arguments, ".x");
2667 break;
2669 case CHANNEL_SOURCE_Y:
2670 strcat(arguments, reg_name);
2671 strcat(arguments, ".y");
2672 break;
2674 case CHANNEL_SOURCE_Z:
2675 strcat(arguments, reg_name);
2676 strcat(arguments, ".z");
2677 break;
2679 case CHANNEL_SOURCE_W:
2680 strcat(arguments, reg_name);
2681 strcat(arguments, ".w");
2682 break;
2684 default:
2685 FIXME("Unhandled channel source %#x\n", channel_source);
2686 strcat(arguments, "undefined");
2687 break;
2690 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2693 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2694 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2696 unsigned int mask_size, remaining;
2697 DWORD fixup_mask = 0;
2698 char arguments[256];
2699 char mask_str[6];
2701 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2702 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2703 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2704 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2705 if (!(mask &= fixup_mask))
2706 return;
2708 if (is_complex_fixup(fixup))
2710 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2711 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2712 return;
2715 shader_glsl_write_mask_to_str(mask, mask_str);
2716 mask_size = shader_glsl_get_write_mask_size(mask);
2718 arguments[0] = '\0';
2719 remaining = mask_size;
2720 if (mask & WINED3DSP_WRITEMASK_0)
2722 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2723 if (--remaining) strcat(arguments, ", ");
2725 if (mask & WINED3DSP_WRITEMASK_1)
2727 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2728 if (--remaining) strcat(arguments, ", ");
2730 if (mask & WINED3DSP_WRITEMASK_2)
2732 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2733 if (--remaining) strcat(arguments, ", ");
2735 if (mask & WINED3DSP_WRITEMASK_3)
2737 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2738 if (--remaining) strcat(arguments, ", ");
2741 if (mask_size > 1)
2742 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2743 else
2744 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2747 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2749 char reg_name[256];
2750 BOOL is_color;
2752 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2753 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2756 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2757 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2758 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2760 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2761 char dst_swizzle[6];
2762 struct color_fixup_desc fixup;
2763 BOOL np2_fixup = FALSE;
2764 va_list args;
2765 int ret;
2767 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2769 /* FIXME: We currently don't support fixups for vertex shaders or anything
2770 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2771 * the resource index. */
2772 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2774 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2775 fixup = priv->cur_ps_args->color_fixup[sampler];
2777 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2778 if(bias) {
2779 FIXME("Biased sampling from NP2 textures is unsupported\n");
2780 } else {
2781 np2_fixup = TRUE;
2785 else
2787 fixup = COLOR_FIXUP_IDENTITY;
2790 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2792 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2793 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2795 for (;;)
2797 va_start(args, coord_reg_fmt);
2798 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2799 va_end(args);
2800 if (!ret)
2801 break;
2802 if (!string_buffer_resize(ins->ctx->buffer, ret))
2803 break;
2806 if(bias) {
2807 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2808 } else {
2809 if (np2_fixup) {
2810 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2811 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2813 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2814 (idx % 2) ? "zw" : "xy", dst_swizzle);
2815 } else if(dx && dy) {
2816 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2817 } else {
2818 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2822 if(!is_identity_fixup(fixup)) {
2823 shader_glsl_color_correction(ins, fixup);
2827 /*****************************************************************************
2828 * Begin processing individual instruction opcodes
2829 ****************************************************************************/
2831 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2833 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2834 struct glsl_src_param src0_param;
2835 struct glsl_src_param src1_param;
2836 DWORD write_mask;
2837 const char *op;
2839 /* Determine the GLSL operator to use based on the opcode */
2840 switch (ins->handler_idx)
2842 case WINED3DSIH_ADD: op = "+"; break;
2843 case WINED3DSIH_AND: op = "&"; break;
2844 case WINED3DSIH_DIV: op = "/"; break;
2845 case WINED3DSIH_IADD: op = "+"; break;
2846 case WINED3DSIH_ISHL: op = "<<"; break;
2847 case WINED3DSIH_MUL: op = "*"; break;
2848 case WINED3DSIH_OR: op = "|"; break;
2849 case WINED3DSIH_SUB: op = "-"; break;
2850 case WINED3DSIH_USHR: op = ">>"; break;
2851 case WINED3DSIH_XOR: op = "^"; break;
2852 default:
2853 op = "<unhandled operator>";
2854 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2855 break;
2858 write_mask = shader_glsl_append_dst(buffer, ins);
2859 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2860 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2861 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2864 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2866 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2867 struct glsl_src_param src0_param;
2868 struct glsl_src_param src1_param;
2869 unsigned int mask_size;
2870 DWORD write_mask;
2871 const char *op;
2873 write_mask = shader_glsl_append_dst(buffer, ins);
2874 mask_size = shader_glsl_get_write_mask_size(write_mask);
2875 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2876 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2878 if (mask_size > 1)
2880 switch (ins->handler_idx)
2882 case WINED3DSIH_EQ: op = "equal"; break;
2883 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2884 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2885 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2886 case WINED3DSIH_LT: op = "lessThan"; break;
2887 case WINED3DSIH_NE: op = "notEqual"; break;
2888 default:
2889 op = "<unhandled operator>";
2890 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2891 break;
2894 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2895 mask_size, op, src0_param.param_str, src1_param.param_str);
2897 else
2899 switch (ins->handler_idx)
2901 case WINED3DSIH_EQ: op = "=="; break;
2902 case WINED3DSIH_GE: op = ">="; break;
2903 case WINED3DSIH_IGE: op = ">="; break;
2904 case WINED3DSIH_UGE: op = ">="; break;
2905 case WINED3DSIH_LT: op = "<"; break;
2906 case WINED3DSIH_NE: op = "!="; break;
2907 default:
2908 op = "<unhandled operator>";
2909 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2910 break;
2913 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2914 src0_param.param_str, op, src1_param.param_str);
2918 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2920 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2921 struct glsl_src_param src0_param;
2922 struct glsl_src_param src1_param;
2923 DWORD write_mask;
2925 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2926 * not, we can emulate it. */
2927 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2928 FIXME("64-bit integer multiplies not implemented.\n");
2930 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2932 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2933 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2934 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2936 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2937 src0_param.param_str, src1_param.param_str);
2941 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2943 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2944 struct glsl_src_param src0_param, src1_param;
2945 DWORD write_mask;
2947 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2950 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2952 char dst_mask[6];
2954 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2955 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2956 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2957 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2958 dst_mask, src0_param.param_str, src1_param.param_str);
2960 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2961 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2962 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2963 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2965 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2966 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2968 else
2970 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2971 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2972 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2973 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2976 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2978 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
2979 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2980 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2981 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2985 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2986 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2988 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2989 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2990 struct glsl_src_param src0_param;
2991 DWORD write_mask;
2993 write_mask = shader_glsl_append_dst(buffer, ins);
2994 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2996 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2997 * shader versions WINED3DSIO_MOVA is used for this. */
2998 if (ins->ctx->reg_maps->shader_version.major == 1
2999 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3000 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3002 /* This is a simple floor() */
3003 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3004 if (mask_size > 1) {
3005 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3006 } else {
3007 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3010 else if(ins->handler_idx == WINED3DSIH_MOVA)
3012 /* We need to *round* to the nearest int here. */
3013 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3015 if (gl_info->supported[EXT_GPU_SHADER4])
3017 if (mask_size > 1)
3018 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3019 else
3020 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3022 else
3024 if (mask_size > 1)
3025 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3026 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3027 else
3028 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3029 src0_param.param_str, src0_param.param_str);
3032 else
3034 shader_addline(buffer, "%s);\n", src0_param.param_str);
3038 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3039 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3041 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3042 struct glsl_src_param src0_param;
3043 struct glsl_src_param src1_param;
3044 DWORD dst_write_mask, src_write_mask;
3045 unsigned int dst_size = 0;
3047 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3048 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3050 /* dp4 works on vec4, dp3 on vec3, etc. */
3051 if (ins->handler_idx == WINED3DSIH_DP4)
3052 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3053 else if (ins->handler_idx == WINED3DSIH_DP3)
3054 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3055 else
3056 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3058 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3059 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3061 if (dst_size > 1) {
3062 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3063 } else {
3064 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3068 /* Note that this instruction has some restrictions. The destination write mask
3069 * can't contain the w component, and the source swizzles have to be .xyzw */
3070 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3072 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3073 struct glsl_src_param src0_param;
3074 struct glsl_src_param src1_param;
3075 char dst_mask[6];
3077 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3078 shader_glsl_append_dst(ins->ctx->buffer, ins);
3079 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3080 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3081 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3084 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3086 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3089 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3090 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3091 * GLSL uses the value as-is. */
3092 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3094 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3095 struct glsl_src_param src0_param;
3096 struct glsl_src_param src1_param;
3097 DWORD dst_write_mask;
3098 unsigned int dst_size;
3100 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3101 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3103 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3104 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3106 if (dst_size > 1)
3108 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3109 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3111 else
3113 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3114 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3118 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3119 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3121 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3122 struct glsl_src_param src_param;
3123 const char *instruction;
3124 DWORD write_mask;
3125 unsigned i;
3127 /* Determine the GLSL function to use based on the opcode */
3128 /* TODO: Possibly make this a table for faster lookups */
3129 switch (ins->handler_idx)
3131 case WINED3DSIH_MIN: instruction = "min"; break;
3132 case WINED3DSIH_MAX: instruction = "max"; break;
3133 case WINED3DSIH_ABS: instruction = "abs"; break;
3134 case WINED3DSIH_FRC: instruction = "fract"; break;
3135 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3136 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3137 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3138 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3139 default: instruction = "";
3140 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
3141 break;
3144 write_mask = shader_glsl_append_dst(buffer, ins);
3146 shader_addline(buffer, "%s(", instruction);
3148 if (ins->src_count)
3150 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3151 shader_addline(buffer, "%s", src_param.param_str);
3152 for (i = 1; i < ins->src_count; ++i)
3154 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3155 shader_addline(buffer, ", %s", src_param.param_str);
3159 shader_addline(buffer, "));\n");
3162 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3164 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3166 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3167 struct glsl_src_param src_param;
3168 unsigned int mask_size;
3169 DWORD write_mask;
3170 char dst_mask[6];
3172 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3173 mask_size = shader_glsl_get_write_mask_size(write_mask);
3174 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3176 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3177 src_param.param_str, src_param.param_str);
3178 shader_glsl_append_dst(buffer, ins);
3180 if (mask_size > 1)
3182 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3183 mask_size, src_param.param_str);
3185 else
3187 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3188 src_param.param_str);
3192 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3194 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3195 struct glsl_src_param src0_param;
3196 const char *prefix, *suffix;
3197 unsigned int dst_size;
3198 DWORD dst_write_mask;
3200 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3201 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3203 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3205 switch (ins->handler_idx)
3207 case WINED3DSIH_EXP:
3208 case WINED3DSIH_EXPP:
3209 prefix = "exp2(";
3210 suffix = ")";
3211 break;
3213 case WINED3DSIH_LOG:
3214 case WINED3DSIH_LOGP:
3215 prefix = "log2(abs(";
3216 suffix = "))";
3217 break;
3219 case WINED3DSIH_RCP:
3220 prefix = "1.0 / ";
3221 suffix = "";
3222 break;
3224 case WINED3DSIH_RSQ:
3225 prefix = "inversesqrt(abs(";
3226 suffix = "))";
3227 break;
3229 default:
3230 prefix = "";
3231 suffix = "";
3232 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3233 break;
3236 if (dst_size > 1)
3237 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3238 else
3239 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3242 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3243 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3244 * dst.x = 2^(floor(src))
3245 * dst.y = src - floor(src)
3246 * dst.z = 2^src (partial precision is allowed, but optional)
3247 * dst.w = 1.0;
3248 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3249 * dst = 2^src; (partial precision is allowed, but optional)
3251 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3253 if (ins->ctx->reg_maps->shader_version.major < 2)
3255 struct glsl_src_param src_param;
3256 char dst_mask[6];
3258 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3260 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3261 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3262 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3263 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3265 shader_glsl_append_dst(ins->ctx->buffer, ins);
3266 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3267 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3268 return;
3271 shader_glsl_scalar_op(ins);
3274 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3276 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3277 struct glsl_src_param src_param;
3278 unsigned int mask_size;
3279 DWORD write_mask;
3281 write_mask = shader_glsl_append_dst(buffer, ins);
3282 mask_size = shader_glsl_get_write_mask_size(write_mask);
3283 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3285 if (mask_size > 1)
3286 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
3287 else
3288 shader_addline(buffer, "int(%s));\n", src_param.param_str);
3291 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3293 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3294 struct glsl_src_param src_param;
3295 unsigned int mask_size;
3296 DWORD write_mask;
3298 write_mask = shader_glsl_append_dst(buffer, ins);
3299 mask_size = shader_glsl_get_write_mask_size(write_mask);
3300 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3302 if (mask_size > 1)
3303 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
3304 else
3305 shader_addline(buffer, "float(%s));\n", src_param.param_str);
3308 /** Process signed comparison opcodes in GLSL. */
3309 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3311 struct glsl_src_param src0_param;
3312 struct glsl_src_param src1_param;
3313 DWORD write_mask;
3314 unsigned int mask_size;
3316 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3317 mask_size = shader_glsl_get_write_mask_size(write_mask);
3318 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3319 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3321 if (mask_size > 1) {
3322 const char *compare;
3324 switch(ins->handler_idx)
3326 case WINED3DSIH_SLT: compare = "lessThan"; break;
3327 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3328 default: compare = "";
3329 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3332 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3333 src0_param.param_str, src1_param.param_str);
3334 } else {
3335 switch(ins->handler_idx)
3337 case WINED3DSIH_SLT:
3338 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3339 * to return 0.0 but step returns 1.0 because step is not < x
3340 * An alternative is a bvec compare padded with an unused second component.
3341 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3342 * issue. Playing with not() is not possible either because not() does not accept
3343 * a scalar.
3345 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3346 src0_param.param_str, src1_param.param_str);
3347 break;
3348 case WINED3DSIH_SGE:
3349 /* Here we can use the step() function and safe a conditional */
3350 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3351 break;
3352 default:
3353 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
3359 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3361 const char *condition_prefix, *condition_suffix;
3362 struct wined3d_shader_dst_param dst;
3363 struct glsl_src_param src0_param;
3364 struct glsl_src_param src1_param;
3365 struct glsl_src_param src2_param;
3366 BOOL temp_destination = FALSE;
3367 DWORD cmp_channel = 0;
3368 unsigned int i, j;
3369 char mask_char[6];
3370 DWORD write_mask;
3372 switch (ins->handler_idx)
3374 case WINED3DSIH_CMP:
3375 condition_prefix = "";
3376 condition_suffix = " >= 0.0";
3377 break;
3379 case WINED3DSIH_CND:
3380 condition_prefix = "";
3381 condition_suffix = " > 0.5";
3382 break;
3384 case WINED3DSIH_MOVC:
3385 condition_prefix = "bool(";
3386 condition_suffix = ")";
3387 break;
3389 default:
3390 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3391 condition_prefix = "<unhandled prefix>";
3392 condition_suffix = "<unhandled suffix>";
3393 break;
3396 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3398 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3399 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3400 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3401 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3403 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3404 condition_prefix, src0_param.param_str, condition_suffix,
3405 src1_param.param_str, src2_param.param_str);
3406 return;
3409 dst = ins->dst[0];
3411 /* Splitting the instruction up in multiple lines imposes a problem:
3412 * The first lines may overwrite source parameters of the following lines.
3413 * Deal with that by using a temporary destination register if needed. */
3414 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3415 && ins->src[0].reg.type == dst.reg.type)
3416 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3417 && ins->src[1].reg.type == dst.reg.type)
3418 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3419 && ins->src[2].reg.type == dst.reg.type))
3420 temp_destination = TRUE;
3422 /* Cycle through all source0 channels. */
3423 for (i = 0; i < 4; ++i)
3425 write_mask = 0;
3426 /* Find the destination channels which use the current source0 channel. */
3427 for (j = 0; j < 4; ++j)
3429 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3431 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3432 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3435 dst.write_mask = ins->dst[0].write_mask & write_mask;
3437 if (temp_destination)
3439 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3440 continue;
3441 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3443 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3444 continue;
3446 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3447 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3448 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3450 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3451 condition_prefix, src0_param.param_str, condition_suffix,
3452 src1_param.param_str, src2_param.param_str);
3455 if (temp_destination)
3457 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3458 shader_glsl_append_dst(ins->ctx->buffer, ins);
3459 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3463 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3464 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3465 * the compare is done per component of src0. */
3466 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3468 struct glsl_src_param src0_param;
3469 struct glsl_src_param src1_param;
3470 struct glsl_src_param src2_param;
3471 DWORD write_mask;
3472 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3473 ins->ctx->reg_maps->shader_version.minor);
3475 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3477 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3478 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3479 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3480 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3482 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3483 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3484 else
3485 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3486 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3487 return;
3490 shader_glsl_conditional_move(ins);
3493 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3494 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3496 struct glsl_src_param src0_param;
3497 struct glsl_src_param src1_param;
3498 struct glsl_src_param src2_param;
3499 DWORD write_mask;
3501 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3502 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3503 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3504 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3505 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3506 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3509 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3510 Vertex shaders to GLSL codes */
3511 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3513 int i;
3514 int nComponents = 0;
3515 struct wined3d_shader_dst_param tmp_dst = {{0}};
3516 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3517 struct wined3d_shader_instruction tmp_ins;
3519 memset(&tmp_ins, 0, sizeof(tmp_ins));
3521 /* Set constants for the temporary argument */
3522 tmp_ins.ctx = ins->ctx;
3523 tmp_ins.dst_count = 1;
3524 tmp_ins.dst = &tmp_dst;
3525 tmp_ins.src_count = 2;
3526 tmp_ins.src = tmp_src;
3528 switch(ins->handler_idx)
3530 case WINED3DSIH_M4x4:
3531 nComponents = 4;
3532 tmp_ins.handler_idx = WINED3DSIH_DP4;
3533 break;
3534 case WINED3DSIH_M4x3:
3535 nComponents = 3;
3536 tmp_ins.handler_idx = WINED3DSIH_DP4;
3537 break;
3538 case WINED3DSIH_M3x4:
3539 nComponents = 4;
3540 tmp_ins.handler_idx = WINED3DSIH_DP3;
3541 break;
3542 case WINED3DSIH_M3x3:
3543 nComponents = 3;
3544 tmp_ins.handler_idx = WINED3DSIH_DP3;
3545 break;
3546 case WINED3DSIH_M3x2:
3547 nComponents = 2;
3548 tmp_ins.handler_idx = WINED3DSIH_DP3;
3549 break;
3550 default:
3551 break;
3554 tmp_dst = ins->dst[0];
3555 tmp_src[0] = ins->src[0];
3556 tmp_src[1] = ins->src[1];
3557 for (i = 0; i < nComponents; ++i)
3559 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3560 shader_glsl_dot(&tmp_ins);
3561 ++tmp_src[1].reg.idx[0].offset;
3566 The LRP instruction performs a component-wise linear interpolation
3567 between the second and third operands using the first operand as the
3568 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3569 This is equivalent to mix(src2, src1, src0);
3571 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3573 struct glsl_src_param src0_param;
3574 struct glsl_src_param src1_param;
3575 struct glsl_src_param src2_param;
3576 DWORD write_mask;
3578 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3580 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3581 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3582 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3584 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3585 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3588 /** Process the WINED3DSIO_LIT instruction in GLSL:
3589 * dst.x = dst.w = 1.0
3590 * dst.y = (src0.x > 0) ? src0.x
3591 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3592 * where src.w is clamped at +- 128
3594 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3596 struct glsl_src_param src0_param;
3597 struct glsl_src_param src1_param;
3598 struct glsl_src_param src3_param;
3599 char dst_mask[6];
3601 shader_glsl_append_dst(ins->ctx->buffer, ins);
3602 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3604 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3605 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3606 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3608 /* The sdk specifies the instruction like this
3609 * dst.x = 1.0;
3610 * if(src.x > 0.0) dst.y = src.x
3611 * else dst.y = 0.0.
3612 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3613 * else dst.z = 0.0;
3614 * dst.w = 1.0;
3615 * (where power = src.w clamped between -128 and 128)
3617 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3618 * dst.x = 1.0 ... No further explanation needed
3619 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3620 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3621 * dst.w = 1.0. ... Nothing fancy.
3623 * So we still have one conditional in there. So do this:
3624 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3626 * 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),
3627 * 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.
3628 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3630 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3631 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3632 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3634 shader_addline(ins->ctx->buffer,
3635 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3636 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3637 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3638 src0_param.param_str, src3_param.param_str, dst_mask);
3641 /** Process the WINED3DSIO_DST instruction in GLSL:
3642 * dst.x = 1.0
3643 * dst.y = src0.x * src0.y
3644 * dst.z = src0.z
3645 * dst.w = src1.w
3647 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3649 struct glsl_src_param src0y_param;
3650 struct glsl_src_param src0z_param;
3651 struct glsl_src_param src1y_param;
3652 struct glsl_src_param src1w_param;
3653 char dst_mask[6];
3655 shader_glsl_append_dst(ins->ctx->buffer, ins);
3656 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3658 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3659 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3660 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3661 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3663 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3664 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3667 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3668 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3669 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3671 * dst.x = cos(src0.?)
3672 * dst.y = sin(src0.?)
3673 * dst.z = dst.z
3674 * dst.w = dst.w
3676 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3678 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3679 struct glsl_src_param src0_param;
3680 DWORD write_mask;
3682 if (ins->ctx->reg_maps->shader_version.major < 4)
3684 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3686 write_mask = shader_glsl_append_dst(buffer, ins);
3687 switch (write_mask)
3689 case WINED3DSP_WRITEMASK_0:
3690 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3691 break;
3693 case WINED3DSP_WRITEMASK_1:
3694 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3695 break;
3697 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3698 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3699 src0_param.param_str, src0_param.param_str);
3700 break;
3702 default:
3703 ERR("Write mask should be .x, .y or .xy\n");
3704 break;
3707 return;
3710 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3713 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3715 char dst_mask[6];
3717 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3718 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3719 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3721 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3722 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3723 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3725 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3726 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3728 else
3730 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3731 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3732 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3735 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3737 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3738 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3739 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3743 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3744 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3745 * generate invalid code
3747 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3749 struct glsl_src_param src0_param;
3750 DWORD write_mask;
3752 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3753 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3755 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3758 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3759 * Start a for() loop where src1.y is the initial value of aL,
3760 * increment aL by src1.z for a total of src1.x iterations.
3761 * Need to use a temporary variable for this operation.
3763 /* FIXME: I don't think nested loops will work correctly this way. */
3764 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3766 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3767 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3768 const struct wined3d_shader *shader = ins->ctx->shader;
3769 const struct wined3d_shader_lconst *constant;
3770 struct glsl_src_param src1_param;
3771 const DWORD *control_values = NULL;
3773 if (ins->ctx->reg_maps->shader_version.major < 4)
3775 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3777 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3778 * class hardware doesn't support real varying indexing, but Microsoft
3779 * designed this feature for Shader model 2.x+. If the loop control is
3780 * known at compile time, the GLSL compiler can unroll the loop, and
3781 * replace indirect addressing with direct addressing. */
3782 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3784 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3786 if (constant->idx == ins->src[1].reg.idx[0].offset)
3788 control_values = constant->value;
3789 break;
3794 if (control_values)
3796 struct wined3d_shader_loop_control loop_control;
3797 loop_control.count = control_values[0];
3798 loop_control.start = control_values[1];
3799 loop_control.step = (int)control_values[2];
3801 if (loop_control.step > 0)
3803 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3804 loop_state->current_depth, loop_control.start,
3805 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3806 loop_state->current_depth, loop_control.step);
3808 else if (loop_control.step < 0)
3810 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3811 loop_state->current_depth, loop_control.start,
3812 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3813 loop_state->current_depth, loop_control.step);
3815 else
3817 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3818 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3819 loop_state->current_depth, loop_control.count,
3820 loop_state->current_depth);
3823 else
3825 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3826 loop_state->current_depth, loop_state->current_reg,
3827 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3828 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3831 ++loop_state->current_reg;
3833 else
3835 shader_addline(buffer, "for (;;)\n{\n");
3838 ++loop_state->current_depth;
3841 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3843 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3845 shader_addline(ins->ctx->buffer, "}\n");
3847 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3849 --loop_state->current_depth;
3850 --loop_state->current_reg;
3853 if (ins->handler_idx == WINED3DSIH_ENDREP)
3855 --loop_state->current_depth;
3859 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3861 const struct wined3d_shader *shader = ins->ctx->shader;
3862 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3863 const struct wined3d_shader_lconst *constant;
3864 struct glsl_src_param src0_param;
3865 const DWORD *control_values = NULL;
3867 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3868 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3870 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3872 if (constant->idx == ins->src[0].reg.idx[0].offset)
3874 control_values = constant->value;
3875 break;
3880 if (control_values)
3882 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3883 loop_state->current_depth, loop_state->current_depth,
3884 control_values[0], loop_state->current_depth);
3886 else
3888 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3889 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3890 loop_state->current_depth, loop_state->current_depth,
3891 src0_param.param_str, loop_state->current_depth);
3894 ++loop_state->current_depth;
3897 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3899 struct glsl_src_param src0_param;
3901 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3902 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3905 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3907 struct glsl_src_param src0_param;
3908 struct glsl_src_param src1_param;
3910 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3911 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3913 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3914 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3917 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3919 shader_addline(ins->ctx->buffer, "} else {\n");
3922 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3924 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3927 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3929 shader_addline(ins->ctx->buffer, "break;\n");
3932 /* FIXME: According to MSDN the compare is done per component. */
3933 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3935 struct glsl_src_param src0_param;
3936 struct glsl_src_param src1_param;
3938 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3939 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3941 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3942 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3945 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3947 struct glsl_src_param src_param;
3949 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3950 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3953 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3955 shader_addline(ins->ctx->buffer, "}\n");
3956 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3959 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3961 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3964 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3966 struct glsl_src_param src1_param;
3968 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3969 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3970 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3973 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3975 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3976 * function only suppresses the unhandled instruction warning
3980 /*********************************************
3981 * Pixel Shader Specific Code begins here
3982 ********************************************/
3983 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3985 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3986 ins->ctx->reg_maps->shader_version.minor);
3987 struct glsl_sample_function sample_function;
3988 DWORD sample_flags = 0;
3989 DWORD resource_idx;
3990 DWORD mask = 0, swizzle;
3991 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3993 /* 1.0-1.4: Use destination register as sampler source.
3994 * 2.0+: Use provided sampler source. */
3995 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3996 resource_idx = ins->dst[0].reg.idx[0].offset;
3997 else
3998 resource_idx = ins->src[1].reg.idx[0].offset;
4000 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4002 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4003 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4004 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4006 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4007 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4009 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4010 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4012 case WINED3D_TTFF_COUNT1:
4013 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4014 break;
4015 case WINED3D_TTFF_COUNT2:
4016 mask = WINED3DSP_WRITEMASK_1;
4017 break;
4018 case WINED3D_TTFF_COUNT3:
4019 mask = WINED3DSP_WRITEMASK_2;
4020 break;
4021 case WINED3D_TTFF_COUNT4:
4022 case WINED3D_TTFF_DISABLE:
4023 mask = WINED3DSP_WRITEMASK_3;
4024 break;
4028 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4030 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4032 if (src_mod == WINED3DSPSM_DZ) {
4033 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4034 mask = WINED3DSP_WRITEMASK_2;
4035 } else if (src_mod == WINED3DSPSM_DW) {
4036 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4037 mask = WINED3DSP_WRITEMASK_3;
4040 else
4042 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4043 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4045 /* ps 2.0 texldp instruction always divides by the fourth component. */
4046 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4047 mask = WINED3DSP_WRITEMASK_3;
4051 if (priv->cur_ps_args->np2_fixup & (1 << resource_idx))
4052 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4054 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4055 mask |= sample_function.coord_mask;
4057 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4058 else swizzle = ins->src[1].swizzle;
4060 /* 1.0-1.3: Use destination register as coordinate source.
4061 1.4+: Use provided coordinate source register. */
4062 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4064 char coord_mask[6];
4065 shader_glsl_write_mask_to_str(mask, coord_mask);
4066 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4067 "T%u%s", resource_idx, coord_mask);
4069 else
4071 struct glsl_src_param coord_param;
4072 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4073 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4075 struct glsl_src_param bias;
4076 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4077 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4078 "%s", coord_param.param_str);
4079 } else {
4080 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4081 "%s", coord_param.param_str);
4086 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4088 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4089 struct glsl_src_param coord_param, dx_param, dy_param;
4090 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4091 struct glsl_sample_function sample_function;
4092 DWORD sampler_idx;
4093 DWORD swizzle = ins->src[1].swizzle;
4094 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4096 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4098 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4099 shader_glsl_tex(ins);
4100 return;
4103 sampler_idx = ins->src[1].reg.idx[0].offset;
4104 if (priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4105 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4107 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4108 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4109 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4110 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4112 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4113 "%s", coord_param.param_str);
4116 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4118 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4119 struct glsl_src_param coord_param, lod_param;
4120 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4121 struct glsl_sample_function sample_function;
4122 DWORD sampler_idx;
4123 DWORD swizzle = ins->src[1].swizzle;
4124 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4126 sampler_idx = ins->src[1].reg.idx[0].offset;
4127 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4128 && priv->cur_ps_args->np2_fixup & (1 << sampler_idx))
4129 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4131 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4132 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4134 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4136 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4137 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4139 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4140 * However, the NVIDIA drivers allow them in fragment shaders as well,
4141 * even without the appropriate extension. */
4142 WARN("Using %s in fragment shader.\n", sample_function.name);
4144 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4145 "%s", coord_param.param_str);
4148 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4149 unsigned int resource_idx, unsigned int sampler_idx)
4151 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4152 unsigned int i;
4154 for (i = 0; i < sampler_map->count; ++i)
4156 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4157 return entries[i].bind_idx;
4160 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4162 return ~0u;
4165 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4167 struct glsl_sample_function sample_function;
4168 struct glsl_src_param coord_param;
4169 unsigned int sampler_idx;
4171 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4172 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4173 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4174 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4175 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE,
4176 NULL, NULL, NULL, "%s", coord_param.param_str);
4179 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4181 /* FIXME: Make this work for more than just 2D textures */
4182 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4183 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4185 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4187 char dst_mask[6];
4189 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4190 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
4191 ins->dst[0].reg.idx[0].offset, dst_mask);
4193 else
4195 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4196 DWORD reg = ins->src[0].reg.idx[0].offset;
4197 char dst_swizzle[6];
4199 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4201 if (src_mod == WINED3DSPSM_DZ)
4203 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4204 struct glsl_src_param div_param;
4206 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
4208 if (mask_size > 1) {
4209 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4210 } else {
4211 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4214 else if (src_mod == WINED3DSPSM_DW)
4216 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4217 struct glsl_src_param div_param;
4219 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
4221 if (mask_size > 1) {
4222 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4223 } else {
4224 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4226 } else {
4227 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
4232 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4233 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4234 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4235 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4237 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4238 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4239 struct glsl_sample_function sample_function;
4240 struct glsl_src_param src0_param;
4241 UINT mask_size;
4243 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4245 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4246 * scalar, and projected sampling would require 4.
4248 * It is a dependent read - not valid with conditional NP2 textures
4250 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4251 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4253 switch(mask_size)
4255 case 1:
4256 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4257 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4258 break;
4260 case 2:
4261 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4262 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4263 break;
4265 case 3:
4266 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4267 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4268 break;
4270 default:
4271 FIXME("Unexpected mask size %u\n", mask_size);
4272 break;
4276 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4277 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4278 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4280 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4281 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4282 struct glsl_src_param src0_param;
4283 DWORD dst_mask;
4284 unsigned int mask_size;
4286 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4287 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4288 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4290 if (mask_size > 1) {
4291 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4292 } else {
4293 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4297 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4298 * Calculate the depth as dst.x / dst.y */
4299 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4301 struct glsl_dst_param dst_param;
4303 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4305 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4306 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4307 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4308 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4309 * >= 1.0 or < 0.0
4311 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4312 dst_param.reg_name, dst_param.reg_name);
4315 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4316 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4317 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4318 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4320 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4322 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4323 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4324 struct glsl_src_param src0_param;
4326 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4328 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4329 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4332 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4333 * Calculate the 1st of a 2-row matrix multiplication. */
4334 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4336 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4337 DWORD reg = ins->dst[0].reg.idx[0].offset;
4338 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4339 struct glsl_src_param src0_param;
4341 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4342 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4345 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4346 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4347 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4349 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4350 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4351 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4352 DWORD reg = ins->dst[0].reg.idx[0].offset;
4353 struct glsl_src_param src0_param;
4355 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4356 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4357 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4360 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4362 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4363 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4364 struct glsl_sample_function sample_function;
4365 DWORD reg = ins->dst[0].reg.idx[0].offset;
4366 struct glsl_src_param src0_param;
4368 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4369 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4371 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4373 /* Sample the texture using the calculated coordinates */
4374 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4377 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4378 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4379 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4381 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4382 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4383 struct glsl_sample_function sample_function;
4384 DWORD reg = ins->dst[0].reg.idx[0].offset;
4385 struct glsl_src_param src0_param;
4387 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4388 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4390 /* Dependent read, not valid with conditional NP2 */
4391 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4393 /* Sample the texture using the calculated coordinates */
4394 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4396 tex_mx->current_row = 0;
4399 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4400 * Perform the 3rd row of a 3x3 matrix multiply */
4401 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4403 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4404 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4405 DWORD reg = ins->dst[0].reg.idx[0].offset;
4406 struct glsl_src_param src0_param;
4407 char dst_mask[6];
4409 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4411 shader_glsl_append_dst(ins->ctx->buffer, ins);
4412 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4413 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4415 tex_mx->current_row = 0;
4418 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4419 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4420 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4422 struct glsl_src_param src0_param;
4423 struct glsl_src_param src1_param;
4424 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4425 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4426 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4427 struct glsl_sample_function sample_function;
4428 DWORD reg = ins->dst[0].reg.idx[0].offset;
4429 char coord_mask[6];
4431 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4432 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4434 /* Perform the last matrix multiply operation */
4435 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4436 /* Reflection calculation */
4437 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4439 /* Dependent read, not valid with conditional NP2 */
4440 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4441 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4443 /* Sample the texture */
4444 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4445 NULL, NULL, NULL, "tmp0%s", coord_mask);
4447 tex_mx->current_row = 0;
4450 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4451 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4452 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4454 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4455 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4456 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4457 struct glsl_sample_function sample_function;
4458 DWORD reg = ins->dst[0].reg.idx[0].offset;
4459 struct glsl_src_param src0_param;
4460 char coord_mask[6];
4462 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4464 /* Perform the last matrix multiply operation */
4465 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4467 /* Construct the eye-ray vector from w coordinates */
4468 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
4469 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4470 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4472 /* Dependent read, not valid with conditional NP2 */
4473 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4474 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4476 /* Sample the texture using the calculated coordinates */
4477 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4478 NULL, NULL, NULL, "tmp0%s", coord_mask);
4480 tex_mx->current_row = 0;
4483 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4484 * Apply a fake bump map transform.
4485 * texbem is pshader <= 1.3 only, this saves a few version checks
4487 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4489 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4490 struct glsl_sample_function sample_function;
4491 struct glsl_src_param coord_param;
4492 DWORD sampler_idx;
4493 DWORD mask;
4494 DWORD flags;
4495 char coord_mask[6];
4497 sampler_idx = ins->dst[0].reg.idx[0].offset;
4498 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4499 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4501 /* Dependent read, not valid with conditional NP2 */
4502 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4503 mask = sample_function.coord_mask;
4505 shader_glsl_write_mask_to_str(mask, coord_mask);
4507 /* With projected textures, texbem only divides the static texture coord,
4508 * not the displacement, so we can't let GL handle this. */
4509 if (flags & WINED3D_PSARGS_PROJECTED)
4511 DWORD div_mask=0;
4512 char coord_div_mask[3];
4513 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4515 case WINED3D_TTFF_COUNT1:
4516 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4517 break;
4518 case WINED3D_TTFF_COUNT2:
4519 div_mask = WINED3DSP_WRITEMASK_1;
4520 break;
4521 case WINED3D_TTFF_COUNT3:
4522 div_mask = WINED3DSP_WRITEMASK_2;
4523 break;
4524 case WINED3D_TTFF_COUNT4:
4525 case WINED3D_TTFF_DISABLE:
4526 div_mask = WINED3DSP_WRITEMASK_3;
4527 break;
4529 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4530 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4533 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4535 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4536 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4537 coord_param.param_str, coord_mask);
4539 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4541 struct glsl_src_param luminance_param;
4542 struct glsl_dst_param dst_param;
4544 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4545 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4547 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4548 dst_param.reg_name, dst_param.mask_str,
4549 luminance_param.param_str, sampler_idx, sampler_idx);
4553 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4555 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4556 struct glsl_src_param src0_param, src1_param;
4558 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4559 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4561 shader_glsl_append_dst(ins->ctx->buffer, ins);
4562 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4563 src0_param.param_str, sampler_idx, src1_param.param_str);
4566 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4567 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4568 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4570 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4571 struct glsl_sample_function sample_function;
4572 struct glsl_src_param src0_param;
4574 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4576 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4577 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4578 "%s.wx", src0_param.reg_name);
4581 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4582 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4583 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4585 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4586 struct glsl_sample_function sample_function;
4587 struct glsl_src_param src0_param;
4589 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4591 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4592 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4593 "%s.yz", src0_param.reg_name);
4596 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4597 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4598 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4600 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4601 struct glsl_sample_function sample_function;
4602 struct glsl_src_param src0_param;
4604 /* Dependent read, not valid with conditional NP2 */
4605 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4606 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4608 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4609 "%s", src0_param.param_str);
4612 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4613 * If any of the first 3 components are < 0, discard this pixel */
4614 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4616 struct glsl_dst_param dst_param;
4618 /* The argument is a destination parameter, and no writemasks are allowed */
4619 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4620 if (ins->ctx->reg_maps->shader_version.major >= 2)
4622 if (ins->ctx->reg_maps->shader_version.major >= 4)
4623 FIXME("SM4 discard not implemented.\n");
4624 /* 2.0 shaders compare all 4 components in texkill */
4625 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4626 } else {
4627 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4628 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4629 * 4 components are defined, only the first 3 are used
4631 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4635 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4636 * dst = dot2(src0, src1) + src2 */
4637 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4639 struct glsl_src_param src0_param;
4640 struct glsl_src_param src1_param;
4641 struct glsl_src_param src2_param;
4642 DWORD write_mask;
4643 unsigned int mask_size;
4645 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4646 mask_size = shader_glsl_get_write_mask_size(write_mask);
4648 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4649 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4650 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4652 if (mask_size > 1) {
4653 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4654 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4655 } else {
4656 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4657 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4661 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4662 const struct wined3d_shader_signature *input_signature,
4663 const struct wined3d_shader_reg_maps *reg_maps,
4664 enum vertexprocessing_mode vertexprocessing)
4666 unsigned int i;
4668 for (i = 0; i < input_signature->element_count; ++i)
4670 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4671 const char *semantic_name;
4672 UINT semantic_idx;
4673 char reg_mask[6];
4675 /* Unused */
4676 if (!(reg_maps->input_registers & (1 << input->register_idx)))
4677 continue;
4679 semantic_name = input->semantic_name;
4680 semantic_idx = input->semantic_idx;
4681 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4683 if (vertexprocessing == vertexshader)
4685 if (input->sysval_semantic == WINED3D_SV_POSITION)
4686 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4687 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4688 else
4689 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4690 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4691 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4693 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4695 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4696 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4697 shader->u.ps.input_reg_map[input->register_idx], reg_mask, semantic_idx, reg_mask);
4698 else
4699 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4700 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4702 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4704 if (!semantic_idx)
4705 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4706 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4707 else if (semantic_idx == 1)
4708 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4709 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4710 else
4711 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4712 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4714 else
4716 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4717 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4722 /*********************************************
4723 * Vertex Shader Specific Code begins here
4724 ********************************************/
4726 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4728 struct glsl_program_key key;
4730 key.vs_id = entry->vs.id;
4731 key.gs_id = entry->gs.id;
4732 key.ps_id = entry->ps.id;
4734 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4736 ERR("Failed to insert program entry.\n");
4740 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4741 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4743 struct wine_rb_entry *entry;
4744 struct glsl_program_key key;
4746 key.vs_id = vs_id;
4747 key.gs_id = gs_id;
4748 key.ps_id = ps_id;
4750 entry = wine_rb_get(&priv->program_lookup, &key);
4751 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4754 /* Context activation is done by the caller. */
4755 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4756 struct glsl_shader_prog_link *entry)
4758 struct glsl_program_key key;
4760 key.vs_id = entry->vs.id;
4761 key.gs_id = entry->gs.id;
4762 key.ps_id = entry->ps.id;
4763 wine_rb_remove(&priv->program_lookup, &key);
4765 GL_EXTCALL(glDeleteProgram(entry->id));
4766 if (entry->vs.id)
4767 list_remove(&entry->vs.shader_entry);
4768 if (entry->gs.id)
4769 list_remove(&entry->gs.shader_entry);
4770 if (entry->ps.id)
4771 list_remove(&entry->ps.shader_entry);
4772 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4773 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4774 HeapFree(GetProcessHeap(), 0, entry);
4777 static void handle_ps3_input(struct shader_glsl_priv *priv,
4778 const struct wined3d_gl_info *gl_info, const DWORD *map,
4779 const struct wined3d_shader_signature *input_signature,
4780 const struct wined3d_shader_reg_maps *reg_maps_in,
4781 const struct wined3d_shader_signature *output_signature,
4782 const struct wined3d_shader_reg_maps *reg_maps_out)
4784 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4785 unsigned int i, j;
4786 DWORD *set;
4787 DWORD in_idx;
4788 unsigned int in_count = vec4_varyings(3, gl_info);
4789 char reg_mask[6];
4790 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4792 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4794 for (i = 0; i < input_signature->element_count; ++i)
4796 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4798 if (!(reg_maps_in->input_registers & (1 << input->register_idx)))
4799 continue;
4801 in_idx = map[input->register_idx];
4802 /* Declared, but not read register */
4803 if (in_idx == ~0u)
4804 continue;
4805 if (in_idx >= (in_count + 2))
4807 FIXME("More input varyings declared than supported, expect issues.\n");
4808 continue;
4811 if (in_idx == in_count)
4812 string_buffer_sprintf(destination, "gl_FrontColor");
4813 else if (in_idx == in_count + 1)
4814 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4815 else
4816 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
4818 if (!set[in_idx])
4819 set[in_idx] = ~0u;
4821 for (j = 0; j < output_signature->element_count; ++j)
4823 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
4824 DWORD mask;
4826 if (!(reg_maps_out->output_registers & (1 << output->register_idx))
4827 || input->semantic_idx != output->semantic_idx
4828 || strcmp(input->semantic_name, output->semantic_name)
4829 || !(mask = input->mask & output->mask))
4830 continue;
4832 if (set[in_idx] == ~0u)
4833 set[in_idx] = mask;
4834 else
4835 set[in_idx] |= mask;
4836 shader_glsl_write_mask_to_str(mask, reg_mask);
4838 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4839 destination->buffer, reg_mask, output->register_idx, reg_mask);
4843 for (i = 0; i < in_count + 2; ++i)
4845 unsigned int size;
4847 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4848 continue;
4850 if (set[i] == ~0U) set[i] = 0;
4852 size = 0;
4853 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4854 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4855 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4856 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4857 reg_mask[size] = '\0';
4859 if (i == in_count)
4860 string_buffer_sprintf(destination, "gl_FrontColor");
4861 else if (i == in_count + 1)
4862 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
4863 else
4864 string_buffer_sprintf(destination, "ps_link[%u]", i);
4866 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
4867 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
4870 HeapFree(GetProcessHeap(), 0, set);
4871 string_buffer_release(&priv->string_buffers, destination);
4874 /* Context activation is done by the caller. */
4875 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
4876 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4877 const struct wined3d_gl_info *gl_info)
4879 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4880 GLuint ret = 0;
4881 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4882 unsigned int i;
4883 const char *semantic_name;
4884 UINT semantic_idx;
4885 char reg_mask[6];
4887 string_buffer_clear(buffer);
4889 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
4891 if (ps_major < 3)
4893 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4895 for (i = 0; i < vs->output_signature.element_count; ++i)
4897 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4898 DWORD write_mask;
4900 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4901 continue;
4903 semantic_name = output->semantic_name;
4904 semantic_idx = output->semantic_idx;
4905 write_mask = output->mask;
4906 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4908 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4910 if (!semantic_idx)
4911 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4912 reg_mask, output->register_idx, reg_mask);
4913 else if (semantic_idx == 1)
4914 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4915 reg_mask, output->register_idx, reg_mask);
4917 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4919 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4920 reg_mask, output->register_idx, reg_mask);
4922 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4924 if (semantic_idx < 8)
4926 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4927 write_mask |= WINED3DSP_WRITEMASK_3;
4929 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4930 semantic_idx, reg_mask, output->register_idx, reg_mask);
4931 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4932 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4935 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4937 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4939 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4941 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
4942 output->register_idx, reg_mask[1]);
4945 shader_addline(buffer, "}\n");
4947 else
4949 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
4950 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4951 shader_addline(buffer, "varying vec4 ps_link[%u];\n", in_count);
4952 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
4954 /* First, sort out position and point size. Those are not passed to the pixel shader */
4955 for (i = 0; i < vs->output_signature.element_count; ++i)
4957 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
4959 if (!(vs->reg_maps.output_registers & (1 << output->register_idx)))
4960 continue;
4962 semantic_name = output->semantic_name;
4963 semantic_idx = output->semantic_idx;
4964 shader_glsl_write_mask_to_str(output->mask, reg_mask);
4966 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
4968 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4969 reg_mask, output->register_idx, reg_mask);
4971 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4973 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", output->register_idx, reg_mask[1]);
4977 /* Then, fix the pixel shader input */
4978 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
4979 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
4981 shader_addline(buffer, "}\n");
4984 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
4985 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
4986 shader_glsl_compile(gl_info, ret, buffer->buffer);
4988 return ret;
4991 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
4993 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4994 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4995 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4996 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4997 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4998 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5001 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5003 switch (mode)
5005 case WINED3D_FFP_PS_FOG_OFF:
5006 return;
5008 case WINED3D_FFP_PS_FOG_LINEAR:
5009 shader_addline(buffer, "float Fog = (gl_Fog.end - gl_FogFragCoord) * gl_Fog.scale;\n");
5010 break;
5012 case WINED3D_FFP_PS_FOG_EXP:
5013 /* Fog = e^-(gl_Fog.density * gl_FogFragCoord) */
5014 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
5015 break;
5017 case WINED3D_FFP_PS_FOG_EXP2:
5018 /* Fog = e^-((gl_Fog.density * gl_FogFragCoord)^2) */
5019 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
5020 break;
5022 default:
5023 ERR("Invalid fog mode %#x.\n", mode);
5024 return;
5027 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, clamp(Fog, 0.0, 1.0));\n");
5030 /* Context activation is done by the caller. */
5031 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5032 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
5033 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5035 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5036 const struct wined3d_gl_info *gl_info = context->gl_info;
5037 const DWORD *function = shader->function;
5038 struct shader_glsl_ctx_priv priv_ctx;
5040 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5041 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5043 memset(&priv_ctx, 0, sizeof(priv_ctx));
5044 priv_ctx.cur_ps_args = args;
5045 priv_ctx.cur_np2fixup_info = np2fixup_info;
5047 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5049 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5050 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5051 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5052 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5053 /* The spec says that it doesn't have to be explicitly enabled, but the
5054 * nvidia drivers write a warning if we don't do so. */
5055 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5056 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5057 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5058 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5059 if (gl_info->supported[EXT_GPU_SHADER4])
5060 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5062 /* Base Declarations */
5063 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5065 /* Pack 3.0 inputs */
5066 if (reg_maps->shader_version.major >= 3)
5067 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args->vp_mode);
5069 /* Base Shader Body */
5070 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5072 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5073 if (reg_maps->shader_version.major < 2)
5075 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5076 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5079 if (args->srgb_correction)
5080 shader_glsl_generate_srgb_write_correction(buffer);
5082 /* SM < 3 does not replace the fog stage. */
5083 if (reg_maps->shader_version.major < 3)
5084 shader_glsl_generate_fog_code(buffer, args->fog);
5086 shader_addline(buffer, "}\n");
5088 TRACE("Compiling shader object %u.\n", shader_id);
5089 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5091 return shader_id;
5094 /* Context activation is done by the caller. */
5095 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5096 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
5097 const struct vs_compile_args *args)
5099 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5100 const struct wined3d_gl_info *gl_info = context->gl_info;
5101 const DWORD *function = shader->function;
5102 struct shader_glsl_ctx_priv priv_ctx;
5104 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5105 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5107 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5109 if (gl_info->supported[ARB_DRAW_INSTANCED])
5110 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5111 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5112 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5113 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5114 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5115 if (gl_info->supported[EXT_GPU_SHADER4])
5116 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5118 memset(&priv_ctx, 0, sizeof(priv_ctx));
5119 priv_ctx.cur_vs_args = args;
5121 /* Base Declarations */
5122 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5124 /* Base Shader Body */
5125 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5127 /* Unpack outputs */
5128 shader_addline(buffer, "order_ps_input(vs_out);\n");
5130 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5131 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5132 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5133 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5135 if (args->fog_src == VS_FOG_Z)
5136 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
5137 else if (!reg_maps->fog)
5138 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
5140 /* We always store the clipplanes without y inversion */
5141 if (args->clip_enabled)
5142 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5144 /* Write the final position.
5146 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5147 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5148 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5149 * contains 1.0 to allow a mad.
5151 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5152 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5154 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5156 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5157 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5158 * which is the same as z = z * 2 - w.
5160 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5162 shader_addline(buffer, "}\n");
5164 TRACE("Compiling shader object %u.\n", shader_id);
5165 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5167 return shader_id;
5170 /* Context activation is done by the caller. */
5171 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5172 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader)
5174 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5175 const struct wined3d_gl_info *gl_info = context->gl_info;
5176 const DWORD *function = shader->function;
5177 struct shader_glsl_ctx_priv priv_ctx;
5178 GLuint shader_id;
5180 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5182 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5184 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5185 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5186 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5187 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5188 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5189 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5190 if (gl_info->supported[EXT_GPU_SHADER4])
5191 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5193 memset(&priv_ctx, 0, sizeof(priv_ctx));
5194 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5195 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5196 shader_addline(buffer, "}\n");
5198 TRACE("Compiling shader object %u.\n", shader_id);
5199 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5201 return shader_id;
5204 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5205 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5206 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5208 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5209 struct glsl_shader_private *shader_data;
5210 struct ps_np2fixup_info *np2fixup;
5211 UINT i;
5212 DWORD new_size;
5213 GLuint ret;
5215 if (!shader->backend_data)
5217 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5218 if (!shader->backend_data)
5220 ERR("Failed to allocate backend data.\n");
5221 return 0;
5224 shader_data = shader->backend_data;
5225 gl_shaders = shader_data->gl_shaders.ps;
5227 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5228 * so a linear search is more performant than a hashmap or a binary search
5229 * (cache coherency etc)
5231 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5233 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5235 if (args->np2_fixup)
5236 *np2fixup_info = &gl_shaders[i].np2fixup;
5237 return gl_shaders[i].id;
5241 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5242 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5243 if (shader_data->num_gl_shaders)
5245 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5246 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5247 new_size * sizeof(*gl_shaders));
5249 else
5251 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5252 new_size = 1;
5255 if(!new_array) {
5256 ERR("Out of memory\n");
5257 return 0;
5259 shader_data->gl_shaders.ps = new_array;
5260 shader_data->shader_array_size = new_size;
5261 gl_shaders = new_array;
5264 gl_shaders[shader_data->num_gl_shaders].args = *args;
5266 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5267 memset(np2fixup, 0, sizeof(*np2fixup));
5268 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5270 pixelshader_update_resource_types(shader, args->tex_types);
5272 string_buffer_clear(buffer);
5273 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
5274 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5276 return ret;
5279 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5280 const DWORD use_map) {
5281 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5282 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5283 return stored->fog_src == new->fog_src;
5286 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5287 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader,
5288 const struct vs_compile_args *args)
5290 UINT i;
5291 DWORD new_size;
5292 DWORD use_map = context->stream_info.use_map;
5293 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5294 struct glsl_shader_private *shader_data;
5295 GLuint ret;
5297 if (!shader->backend_data)
5299 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5300 if (!shader->backend_data)
5302 ERR("Failed to allocate backend data.\n");
5303 return 0;
5306 shader_data = shader->backend_data;
5307 gl_shaders = shader_data->gl_shaders.vs;
5309 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5310 * so a linear search is more performant than a hashmap or a binary search
5311 * (cache coherency etc)
5313 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5315 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5316 return gl_shaders[i].id;
5319 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5321 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5322 if (shader_data->num_gl_shaders)
5324 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5325 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5326 new_size * sizeof(*gl_shaders));
5328 else
5330 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5331 new_size = 1;
5334 if(!new_array) {
5335 ERR("Out of memory\n");
5336 return 0;
5338 shader_data->gl_shaders.vs = new_array;
5339 shader_data->shader_array_size = new_size;
5340 gl_shaders = new_array;
5343 gl_shaders[shader_data->num_gl_shaders].args = *args;
5345 string_buffer_clear(buffer);
5346 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
5347 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5349 return ret;
5352 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5353 struct wined3d_string_buffer *buffer, struct wined3d_shader *shader)
5355 struct glsl_gs_compiled_shader *gl_shaders;
5356 struct glsl_shader_private *shader_data;
5357 GLuint ret;
5359 if (!shader->backend_data)
5361 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5363 ERR("Failed to allocate backend data.\n");
5364 return 0;
5367 shader_data = shader->backend_data;
5368 gl_shaders = shader_data->gl_shaders.gs;
5370 if (shader_data->num_gl_shaders)
5371 return gl_shaders[0].id;
5373 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5375 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5377 ERR("Failed to allocate GL shader array.\n");
5378 return 0;
5380 shader_data->shader_array_size = 1;
5381 gl_shaders = shader_data->gl_shaders.gs;
5383 string_buffer_clear(buffer);
5384 ret = shader_glsl_generate_geometry_shader(context, buffer, shader);
5385 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5387 return ret;
5390 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5392 switch (mcs)
5394 case WINED3D_MCS_MATERIAL:
5395 return material;
5396 case WINED3D_MCS_COLOR1:
5397 return "gl_Color";
5398 case WINED3D_MCS_COLOR2:
5399 return "gl_SecondaryColor";
5400 default:
5401 ERR("Invalid material color source %#x.\n", mcs);
5402 return "<invalid>";
5406 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5407 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5409 const char *diffuse, *specular, *emission, *ambient;
5410 enum wined3d_light_type light_type;
5411 unsigned int i;
5413 if (!settings->lighting)
5415 shader_addline(buffer, "gl_FrontColor = gl_Color;\n");
5416 shader_addline(buffer, "gl_FrontSecondaryColor = gl_SecondaryColor;\n");
5417 return;
5420 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5421 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5422 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5423 shader_addline(buffer, "vec3 dir, dst;\n");
5424 shader_addline(buffer, "float att, t;\n");
5426 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5427 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5428 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5429 emission = shader_glsl_ffp_mcs(settings->emission_source, "ffp_material.emission");
5431 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5433 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5434 switch (light_type)
5436 case WINED3D_LIGHT_POINT:
5437 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5438 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5439 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5440 shader_addline(buffer, "dst.x = 1.0;\n");
5441 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5442 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5443 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5444 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz / att;\n", i);
5445 if (!settings->normal)
5447 shader_addline(buffer, "}\n");
5448 break;
5450 shader_addline(buffer, "dir = normalize(dir);\n");
5451 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5452 " * ffp_light[%u].diffuse.xyz) / att;\n", i);
5453 if (settings->localviewer)
5454 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5455 else
5456 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5457 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5458 " * ffp_light[%u].specular) / att;\n", i);
5459 shader_addline(buffer, "}\n");
5460 break;
5462 case WINED3D_LIGHT_SPOT:
5463 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5464 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5465 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5466 shader_addline(buffer, "dst.x = 1.0;\n");
5467 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5468 shader_addline(buffer, "dir = normalize(dir);\n");
5469 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5470 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5471 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5472 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5473 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff)"
5474 " / dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5475 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5476 i, i, i, i, i, i, i);
5477 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5478 if (!settings->normal)
5480 shader_addline(buffer, "}\n");
5481 break;
5483 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5484 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5485 if (settings->localviewer)
5486 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5487 else
5488 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5489 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5490 " * ffp_light[%u].specular) * att;\n", i);
5491 shader_addline(buffer, "}\n");
5492 break;
5494 case WINED3D_LIGHT_DIRECTIONAL:
5495 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5496 if (!settings->normal)
5497 break;
5498 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5499 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5500 " * ffp_light[%u].diffuse.xyz;\n", i);
5501 /* TODO: In the non-local viewer case the halfvector is constant
5502 * and could be precomputed and stored in a uniform. */
5503 if (settings->localviewer)
5504 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5505 else
5506 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5507 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5508 " * ffp_light[%u].specular;\n", i);
5509 break;
5511 default:
5512 if (light_type)
5513 FIXME("Unhandled light type %#x.\n", light_type);
5514 continue;
5518 shader_addline(buffer, "gl_FrontColor.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5519 ambient, diffuse, emission);
5520 shader_addline(buffer, "gl_FrontColor.w = %s.w;\n", diffuse);
5521 shader_addline(buffer, "gl_FrontSecondaryColor = %s * specular;\n", specular);
5524 /* Context activation is done by the caller. */
5525 static GLuint shader_glsl_generate_ffp_vertex_shader(struct wined3d_string_buffer *buffer,
5526 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5528 GLuint shader_obj;
5529 unsigned int i;
5531 string_buffer_clear(buffer);
5533 shader_addline(buffer, "#version 120\n");
5534 shader_addline(buffer, "\n");
5536 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix;\n");
5537 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5538 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5539 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5541 shader_addline(buffer, "uniform struct\n{\n");
5542 shader_addline(buffer, " vec4 emission;\n");
5543 shader_addline(buffer, " vec4 ambient;\n");
5544 shader_addline(buffer, " vec4 diffuse;\n");
5545 shader_addline(buffer, " vec4 specular;\n");
5546 shader_addline(buffer, " float shininess;\n");
5547 shader_addline(buffer, "} ffp_material;\n");
5549 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5550 shader_addline(buffer, "uniform struct\n{\n");
5551 shader_addline(buffer, " vec4 diffuse;\n");
5552 shader_addline(buffer, " vec4 specular;\n");
5553 shader_addline(buffer, " vec4 ambient;\n");
5554 shader_addline(buffer, " vec4 position;\n");
5555 shader_addline(buffer, " vec3 direction;\n");
5556 shader_addline(buffer, " float range;\n");
5557 shader_addline(buffer, " float falloff;\n");
5558 shader_addline(buffer, " float c_att;\n");
5559 shader_addline(buffer, " float l_att;\n");
5560 shader_addline(buffer, " float q_att;\n");
5561 shader_addline(buffer, " float cos_htheta;\n");
5562 shader_addline(buffer, " float cos_hphi;\n");
5563 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5565 shader_addline(buffer, "\nvoid main()\n{\n");
5566 shader_addline(buffer, "float m;\n");
5567 shader_addline(buffer, "vec3 r;\n");
5569 if (settings->transformed)
5571 shader_addline(buffer, "vec4 ec_pos = vec4(gl_Vertex.xyz, 1.0);\n");
5572 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5573 shader_addline(buffer, "if (gl_Vertex.w != 0.0) gl_Position /= gl_Vertex.w;\n");
5575 else
5577 shader_addline(buffer, "vec4 ec_pos = ffp_modelview_matrix * gl_Vertex;\n");
5578 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5579 if (settings->clipping)
5580 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
5581 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
5584 if (!settings->normal)
5585 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
5586 else if (settings->normalize)
5587 shader_addline(buffer, "vec3 normal = normalize(ffp_normal_matrix * gl_Normal);\n");
5588 else
5589 shader_addline(buffer, "vec3 normal = ffp_normal_matrix * gl_Normal;\n");
5591 shader_glsl_ffp_vertex_lighting(buffer, settings, gl_info);
5593 for (i = 0; i < MAX_TEXTURES; ++i)
5595 switch (settings->texgen[i] << WINED3D_FFP_TCI_SHIFT)
5597 case WINED3DTSS_TCI_PASSTHRU:
5598 if (settings->texcoords & (1 << i))
5599 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * gl_MultiTexCoord%d;\n",
5600 i, i, i);
5601 break;
5603 case WINED3DTSS_TCI_CAMERASPACENORMAL:
5604 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
5605 break;
5607 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
5608 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
5609 break;
5611 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
5612 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5613 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
5614 break;
5616 case WINED3DTSS_TCI_SPHEREMAP:
5617 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
5618 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
5619 shader_addline(buffer, "gl_TexCoord[%u] = ffp_texture_matrix[%u]"
5620 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
5621 break;
5623 default:
5624 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
5625 break;
5629 switch (settings->fog_mode)
5631 case WINED3D_FFP_VS_FOG_OFF:
5632 break;
5634 case WINED3D_FFP_VS_FOG_FOGCOORD:
5635 shader_addline(buffer, "gl_FogFragCoord = gl_SecondaryColor.w * 255.0;\n");
5636 break;
5638 case WINED3D_FFP_VS_FOG_RANGE:
5639 shader_addline(buffer, "gl_FogFragCoord = length(ec_pos.xyz);\n");
5640 break;
5642 case WINED3D_FFP_VS_FOG_DEPTH:
5643 if (settings->ortho_fog)
5644 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
5645 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z * 0.5 + 0.5;\n");
5646 else if (settings->transformed)
5647 shader_addline(buffer, "gl_FogFragCoord = ec_pos.z;\n");
5648 else
5649 shader_addline(buffer, "gl_FogFragCoord = abs(ec_pos.z);\n");
5650 break;
5652 default:
5653 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
5654 break;
5657 if (settings->point_size)
5659 shader_addline(buffer, "gl_PointSize = gl_Point.size / sqrt(gl_Point.distanceConstantAttenuation"
5660 " + gl_Point.distanceLinearAttenuation * length(ec_pos.xyz)"
5661 " + gl_Point.distanceQuadraticAttenuation * dot(ec_pos.xyz, ec_pos.xyz));\n");
5662 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, gl_Point.sizeMin, gl_Point.sizeMax);\n");
5665 shader_addline(buffer, "}\n");
5667 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5668 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
5670 return shader_obj;
5673 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
5674 DWORD argnum, unsigned int stage, DWORD arg)
5676 const char *ret;
5678 if (arg == ARG_UNUSED)
5679 return "<unused arg>";
5681 switch (arg & WINED3DTA_SELECTMASK)
5683 case WINED3DTA_DIFFUSE:
5684 ret = "gl_Color";
5685 break;
5687 case WINED3DTA_CURRENT:
5688 if (!stage)
5689 ret = "gl_Color";
5690 else
5691 ret = "ret";
5692 break;
5694 case WINED3DTA_TEXTURE:
5695 switch (stage)
5697 case 0: ret = "tex0"; break;
5698 case 1: ret = "tex1"; break;
5699 case 2: ret = "tex2"; break;
5700 case 3: ret = "tex3"; break;
5701 case 4: ret = "tex4"; break;
5702 case 5: ret = "tex5"; break;
5703 case 6: ret = "tex6"; break;
5704 case 7: ret = "tex7"; break;
5705 default:
5706 ret = "<invalid texture>";
5707 break;
5709 break;
5711 case WINED3DTA_TFACTOR:
5712 ret = "tex_factor";
5713 break;
5715 case WINED3DTA_SPECULAR:
5716 ret = "gl_SecondaryColor";
5717 break;
5719 case WINED3DTA_TEMP:
5720 ret = "temp_reg";
5721 break;
5723 case WINED3DTA_CONSTANT:
5724 switch (stage)
5726 case 0: ret = "tss_const0"; break;
5727 case 1: ret = "tss_const1"; break;
5728 case 2: ret = "tss_const2"; break;
5729 case 3: ret = "tss_const3"; break;
5730 case 4: ret = "tss_const4"; break;
5731 case 5: ret = "tss_const5"; break;
5732 case 6: ret = "tss_const6"; break;
5733 case 7: ret = "tss_const7"; break;
5734 default:
5735 ret = "<invalid constant>";
5736 break;
5738 break;
5740 default:
5741 return "<unhandled arg>";
5744 if (arg & WINED3DTA_COMPLEMENT)
5746 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
5747 if (argnum == 0)
5748 ret = "arg0";
5749 else if (argnum == 1)
5750 ret = "arg1";
5751 else if (argnum == 2)
5752 ret = "arg2";
5755 if (arg & WINED3DTA_ALPHAREPLICATE)
5757 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
5758 if (argnum == 0)
5759 ret = "arg0";
5760 else if (argnum == 1)
5761 ret = "arg1";
5762 else if (argnum == 2)
5763 ret = "arg2";
5766 return ret;
5769 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
5770 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
5772 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
5774 if (color && alpha)
5775 dstmask = "";
5776 else if (color)
5777 dstmask = ".xyz";
5778 else
5779 dstmask = ".w";
5781 if (dst == tempreg)
5782 dstreg = "temp_reg";
5783 else
5784 dstreg = "ret";
5786 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
5787 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
5788 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
5790 switch (op)
5792 case WINED3D_TOP_DISABLE:
5793 if (!stage)
5794 shader_addline(buffer, "%s%s = gl_Color%s;\n", dstreg, dstmask, dstmask);
5795 break;
5797 case WINED3D_TOP_SELECT_ARG1:
5798 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
5799 break;
5801 case WINED3D_TOP_SELECT_ARG2:
5802 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
5803 break;
5805 case WINED3D_TOP_MODULATE:
5806 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5807 break;
5809 case WINED3D_TOP_MODULATE_4X:
5810 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
5811 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5812 break;
5814 case WINED3D_TOP_MODULATE_2X:
5815 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
5816 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5817 break;
5819 case WINED3D_TOP_ADD:
5820 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
5821 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5822 break;
5824 case WINED3D_TOP_ADD_SIGNED:
5825 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
5826 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5827 break;
5829 case WINED3D_TOP_ADD_SIGNED_2X:
5830 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
5831 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5832 break;
5834 case WINED3D_TOP_SUBTRACT:
5835 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
5836 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
5837 break;
5839 case WINED3D_TOP_ADD_SMOOTH:
5840 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
5841 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
5842 break;
5844 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
5845 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
5846 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5847 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5848 break;
5850 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5851 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5852 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5853 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5854 break;
5856 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5857 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
5858 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5859 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5860 break;
5862 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5863 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
5864 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5865 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
5866 break;
5868 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
5869 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
5870 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
5871 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
5872 break;
5874 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
5875 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
5876 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5877 break;
5879 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
5880 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
5881 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5882 break;
5884 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
5885 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
5886 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
5887 break;
5888 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
5889 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
5890 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
5891 break;
5893 case WINED3D_TOP_BUMPENVMAP:
5894 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5895 /* These are handled in the first pass, nothing to do. */
5896 break;
5898 case WINED3D_TOP_DOTPRODUCT3:
5899 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
5900 dstreg, dstmask, arg1, arg2, dstmask);
5901 break;
5903 case WINED3D_TOP_MULTIPLY_ADD:
5904 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
5905 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
5906 break;
5908 case WINED3D_TOP_LERP:
5909 /* MSDN isn't quite right here. */
5910 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
5911 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
5912 break;
5914 default:
5915 FIXME("Unhandled operation %#x.\n", op);
5916 break;
5920 /* Context activation is done by the caller. */
5921 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
5922 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
5924 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5925 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
5926 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
5927 const char *final_combiner_src = "ret";
5928 UINT lowest_disabled_stage;
5929 GLuint shader_id;
5930 DWORD arg0, arg1, arg2;
5931 unsigned int stage;
5932 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
5934 string_buffer_clear(buffer);
5936 /* Find out which textures are read */
5937 for (stage = 0; stage < MAX_TEXTURES; ++stage)
5939 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
5940 break;
5942 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
5943 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
5944 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
5946 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
5947 || (stage == 0 && settings->color_key_enabled))
5948 tex_map |= 1 << stage;
5949 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5950 tfactor_used = TRUE;
5951 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5952 tempreg_used = TRUE;
5953 if (settings->op[stage].dst == tempreg)
5954 tempreg_used = TRUE;
5955 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5956 tss_const_map |= 1 << stage;
5958 switch (settings->op[stage].cop)
5960 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
5961 lum_map |= 1 << stage;
5962 /* fall through */
5963 case WINED3D_TOP_BUMPENVMAP:
5964 bump_map |= 1 << stage;
5965 /* fall through */
5966 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
5967 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
5968 tex_map |= 1 << stage;
5969 break;
5971 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
5972 tfactor_used = TRUE;
5973 break;
5975 default:
5976 break;
5979 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
5980 continue;
5982 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
5983 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
5984 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
5986 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
5987 tex_map |= 1 << stage;
5988 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
5989 tfactor_used = TRUE;
5990 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
5991 tempreg_used = TRUE;
5992 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
5993 tss_const_map |= 1 << stage;
5995 lowest_disabled_stage = stage;
5997 shader_addline(buffer, "#version 120\n");
5999 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6000 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6002 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6003 shader_addline(buffer, "vec4 ret;\n");
6004 if (tempreg_used || settings->sRGB_write)
6005 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6006 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6008 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6010 if (tss_const_map & (1 << stage))
6011 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6013 if (!(tex_map & (1 << stage)))
6014 continue;
6016 switch (settings->op[stage].tex_type)
6018 case WINED3D_GL_RES_TYPE_TEX_1D:
6019 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6020 break;
6021 case WINED3D_GL_RES_TYPE_TEX_2D:
6022 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6023 break;
6024 case WINED3D_GL_RES_TYPE_TEX_3D:
6025 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6026 break;
6027 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6028 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6029 break;
6030 case WINED3D_GL_RES_TYPE_TEX_RECT:
6031 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6032 break;
6033 default:
6034 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6035 break;
6038 shader_addline(buffer, "vec4 tex%u;\n", stage);
6040 if (!(bump_map & (1 << stage)))
6041 continue;
6042 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6044 if (!(lum_map & (1 << stage)))
6045 continue;
6046 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6047 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6049 if (tfactor_used)
6050 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6051 if (settings->color_key_enabled)
6052 shader_addline(buffer, "uniform vec4 color_key;\n");
6053 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6055 if (settings->sRGB_write)
6057 shader_addline(buffer, "const vec4 srgb_const0 = ");
6058 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6059 shader_addline(buffer, ";\n");
6060 shader_addline(buffer, "const vec4 srgb_const1 = ");
6061 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6062 shader_addline(buffer, ";\n");
6065 shader_addline(buffer, "void main()\n{\n");
6067 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6068 shader_addline(buffer, "if (any(lessThan(gl_TexCoord[7], vec4(0.0)))) discard;\n");
6070 /* Generate texture sampling instructions) */
6071 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6073 const char *texture_function, *coord_mask;
6074 BOOL proj;
6076 if (!(tex_map & (1 << stage)))
6077 continue;
6079 if (settings->op[stage].projected == proj_none)
6081 proj = FALSE;
6083 else if (settings->op[stage].projected == proj_count4
6084 || settings->op[stage].projected == proj_count3)
6086 proj = TRUE;
6088 else
6090 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6091 proj = TRUE;
6094 switch (settings->op[stage].tex_type)
6096 case WINED3D_GL_RES_TYPE_TEX_1D:
6097 if (proj)
6099 texture_function = "texture1DProj";
6100 coord_mask = "xw";
6102 else
6104 texture_function = "texture1D";
6105 coord_mask = "x";
6107 break;
6108 case WINED3D_GL_RES_TYPE_TEX_2D:
6109 if (proj)
6111 texture_function = "texture2DProj";
6112 coord_mask = "xyw";
6114 else
6116 texture_function = "texture2D";
6117 coord_mask = "xy";
6119 break;
6120 case WINED3D_GL_RES_TYPE_TEX_3D:
6121 if (proj)
6123 texture_function = "texture3DProj";
6124 coord_mask = "xyzw";
6126 else
6128 texture_function = "texture3D";
6129 coord_mask = "xyz";
6131 break;
6132 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6133 texture_function = "textureCube";
6134 coord_mask = "xyz";
6135 break;
6136 case WINED3D_GL_RES_TYPE_TEX_RECT:
6137 if (proj)
6139 texture_function = "texture2DRectProj";
6140 coord_mask = "xyw";
6142 else
6144 texture_function = "texture2DRect";
6145 coord_mask = "xy";
6147 break;
6148 default:
6149 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6150 texture_function = "";
6151 coord_mask = "xyzw";
6152 break;
6155 if (stage > 0
6156 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6157 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6159 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6161 /* With projective textures, texbem only divides the static
6162 * texture coord, not the displacement, so multiply the
6163 * displacement with the dividing parameter before passing it to
6164 * TXP. */
6165 if (settings->op[stage].projected != proj_none)
6167 if (settings->op[stage].projected == proj_count4)
6169 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].w) + gl_TexCoord[%u].xy;\n",
6170 stage, stage);
6171 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].ww;\n", stage);
6173 else
6175 shader_addline(buffer, "ret.xy = (ret.xy * gl_TexCoord[%u].z) + gl_TexCoord[%u].xy;\n",
6176 stage, stage);
6177 shader_addline(buffer, "ret.zw = gl_TexCoord[%u].zz;\n", stage);
6180 else
6182 shader_addline(buffer, "ret = gl_TexCoord[%u] + ret.xyxy;\n", stage);
6185 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6186 stage, texture_function, stage, coord_mask);
6188 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6189 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6190 stage, stage - 1, stage - 1, stage - 1);
6192 else if (settings->op[stage].projected == proj_count3)
6194 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].xyz);\n",
6195 stage, texture_function, stage, stage);
6197 else
6199 shader_addline(buffer, "tex%u = %s(ps_sampler%u, gl_TexCoord[%u].%s);\n",
6200 stage, texture_function, stage, stage, coord_mask);
6203 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6204 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6205 settings->op[stage].color_fixup);
6208 if (settings->color_key_enabled)
6209 shader_addline(buffer, "if (all(equal(tex0, color_key))) discard;\n");
6211 /* Generate the main shader */
6212 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6214 BOOL op_equal;
6216 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6218 if (!stage)
6219 final_combiner_src = "gl_Color";
6220 break;
6223 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6224 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6225 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6226 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6227 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6228 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6229 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6230 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6231 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6232 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6233 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6234 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6235 else
6236 op_equal = settings->op[stage].aop == settings->op[stage].cop
6237 && settings->op[stage].carg0 == settings->op[stage].aarg0
6238 && settings->op[stage].carg1 == settings->op[stage].aarg1
6239 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6241 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6243 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6244 settings->op[stage].cop, settings->op[stage].carg0,
6245 settings->op[stage].carg1, settings->op[stage].carg2);
6246 if (!stage)
6247 shader_addline(buffer, "ret.w = gl_Color.w;\n");
6249 else if (op_equal)
6251 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6252 settings->op[stage].cop, settings->op[stage].carg0,
6253 settings->op[stage].carg1, settings->op[stage].carg2);
6255 else
6257 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6258 settings->op[stage].cop, settings->op[stage].carg0,
6259 settings->op[stage].carg1, settings->op[stage].carg2);
6260 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6261 settings->op[stage].aop, settings->op[stage].aarg0,
6262 settings->op[stage].aarg1, settings->op[stage].aarg2);
6266 shader_addline(buffer, "gl_FragData[0] = gl_SecondaryColor * specular_enable + %s;\n", final_combiner_src);
6268 if (settings->sRGB_write)
6269 shader_glsl_generate_srgb_write_correction(buffer);
6271 shader_glsl_generate_fog_code(buffer, settings->fog);
6273 shader_addline(buffer, "}\n");
6275 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6276 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6278 string_buffer_release(&priv->string_buffers, tex_reg_name);
6279 return shader_id;
6282 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6283 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6285 struct glsl_ffp_vertex_shader *shader;
6286 const struct wine_rb_entry *entry;
6288 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6289 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6291 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6292 return NULL;
6294 shader->desc.settings = *settings;
6295 shader->id = shader_glsl_generate_ffp_vertex_shader(&priv->shader_buffer, settings, gl_info);
6296 list_init(&shader->linked_programs);
6297 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6298 ERR("Failed to insert ffp vertex shader.\n");
6300 return shader;
6303 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6304 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6306 struct glsl_ffp_fragment_shader *glsl_desc;
6307 const struct ffp_frag_desc *desc;
6309 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6310 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6312 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6313 return NULL;
6315 glsl_desc->entry.settings = *args;
6316 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6317 list_init(&glsl_desc->linked_programs);
6318 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6320 return glsl_desc;
6324 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6325 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6327 unsigned int i;
6328 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6330 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6331 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6332 for (i = 0; i < vs_c_count; ++i)
6334 string_buffer_sprintf(name, "vs_c[%u]", i);
6335 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6337 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6338 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6340 for (i = 0; i < MAX_CONST_I; ++i)
6342 string_buffer_sprintf(name, "vs_i[%u]", i);
6343 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6346 for (i = 0; i < MAX_CONST_B; ++i)
6348 string_buffer_sprintf(name, "vs_b[%u]", i);
6349 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6352 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6354 vs->modelview_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_modelview_matrix"));
6355 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6356 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6357 for (i = 0; i < MAX_TEXTURES; ++i)
6359 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6360 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6362 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6363 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6364 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6365 vs->material_emission_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emission"));
6366 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6367 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6368 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6370 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6371 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6372 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6373 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6374 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6375 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6376 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6377 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6378 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6379 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6380 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6381 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6382 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6383 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6384 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6385 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6386 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6387 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6388 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6389 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6390 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6391 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6392 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6393 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6396 string_buffer_release(&priv->string_buffers, name);
6399 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6400 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6402 unsigned int i;
6403 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6405 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6406 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6407 for (i = 0; i < ps_c_count; ++i)
6409 string_buffer_sprintf(name, "ps_c[%u]", i);
6410 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6412 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6413 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6415 for (i = 0; i < MAX_CONST_I; ++i)
6417 string_buffer_sprintf(name, "ps_i[%u]", i);
6418 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6421 for (i = 0; i < MAX_CONST_B; ++i)
6423 string_buffer_sprintf(name, "ps_b[%u]", i);
6424 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6427 for (i = 0; i < MAX_TEXTURES; ++i)
6429 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6430 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6431 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6432 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6433 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6434 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6435 string_buffer_sprintf(name, "tss_const%u", i);
6436 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6439 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6440 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6441 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6442 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6443 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6445 string_buffer_release(&priv->string_buffers, name);
6448 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6449 struct shader_glsl_priv *priv, GLuint program_id,
6450 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6452 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6453 GLuint block_idx;
6454 unsigned int i;
6455 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6457 for (i = 0; i < count; ++i)
6459 if (!reg_maps->cb_sizes[i])
6460 continue;
6462 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6463 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6464 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6466 checkGLcall("glUniformBlockBinding");
6467 string_buffer_release(&priv->string_buffers, name);
6470 /* Context activation is done by the caller. */
6471 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6472 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6474 const struct wined3d_gl_info *gl_info = context->gl_info;
6475 const struct ps_np2fixup_info *np2fixup_info = NULL;
6476 struct glsl_shader_prog_link *entry = NULL;
6477 struct wined3d_shader *vshader = NULL;
6478 struct wined3d_shader *gshader = NULL;
6479 struct wined3d_shader *pshader = NULL;
6480 GLuint program_id = 0;
6481 GLuint reorder_shader_id = 0;
6482 unsigned int i;
6483 GLuint vs_id = 0;
6484 GLuint gs_id = 0;
6485 GLuint ps_id = 0;
6486 struct list *ps_list, *vs_list;
6488 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_VERTEX)))
6490 vs_id = ctx_data->glsl_program->vs.id;
6491 vs_list = &ctx_data->glsl_program->vs.shader_entry;
6493 if (use_vs(state))
6495 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6496 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
6498 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_GEOMETRY))
6499 && ctx_data->glsl_program->gs.id)
6500 gs_id = ctx_data->glsl_program->gs.id;
6501 else if (gshader)
6502 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6505 else if (use_vs(state))
6507 struct vs_compile_args vs_compile_args;
6508 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
6510 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args);
6511 vs_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
6512 vs_list = &vshader->linked_programs;
6514 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
6515 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, gshader);
6517 else if (priv->vertex_pipe == &glsl_vertex_pipe)
6519 struct glsl_ffp_vertex_shader *ffp_shader;
6520 struct wined3d_ffp_vs_settings settings;
6522 wined3d_ffp_get_vs_settings(state, &context->stream_info, &settings);
6523 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
6524 vs_id = ffp_shader->id;
6525 vs_list = &ffp_shader->linked_programs;
6528 if (!(context->shader_update_mask & (1 << WINED3D_SHADER_TYPE_PIXEL)))
6530 ps_id = ctx_data->glsl_program->ps.id;
6531 ps_list = &ctx_data->glsl_program->ps.shader_entry;
6533 if (use_ps(state))
6534 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6536 else if (use_ps(state))
6538 struct ps_compile_args ps_compile_args;
6539 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
6540 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, gl_info);
6541 ps_id = find_glsl_pshader(context, &priv->shader_buffer,
6542 pshader, &ps_compile_args, &np2fixup_info);
6543 ps_list = &pshader->linked_programs;
6545 else if (priv->fragment_pipe == &glsl_fragment_pipe)
6547 struct glsl_ffp_fragment_shader *ffp_shader;
6548 struct ffp_frag_settings settings;
6550 gen_ffp_frag_op(context, state, &settings, FALSE);
6551 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
6552 ps_id = ffp_shader->id;
6553 ps_list = &ffp_shader->linked_programs;
6556 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
6558 ctx_data->glsl_program = entry;
6559 return;
6562 /* If we get to this point, then no matching program exists, so we create one */
6563 program_id = GL_EXTCALL(glCreateProgram());
6564 TRACE("Created new GLSL shader program %u.\n", program_id);
6566 /* Create the entry */
6567 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
6568 entry->id = program_id;
6569 entry->vs.id = vs_id;
6570 entry->gs.id = gs_id;
6571 entry->ps.id = ps_id;
6572 entry->constant_version = 0;
6573 entry->ps.np2_fixup_info = np2fixup_info;
6574 /* Add the hash table entry */
6575 add_glsl_program_entry(priv, entry);
6577 /* Set the current program */
6578 ctx_data->glsl_program = entry;
6580 /* Attach GLSL vshader */
6581 if (vs_id)
6583 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
6584 GL_EXTCALL(glAttachShader(program_id, vs_id));
6585 checkGLcall("glAttachShader");
6587 list_add_head(vs_list, &entry->vs.shader_entry);
6590 if (vshader)
6592 WORD map = vshader->reg_maps.input_registers;
6593 struct wined3d_string_buffer *tmp_name = string_buffer_get(&priv->string_buffers);
6595 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader, gl_info);
6596 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
6597 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
6598 checkGLcall("glAttachShader");
6599 /* Flag the reorder function for deletion, then it will be freed automatically when the program
6600 * is destroyed
6602 GL_EXTCALL(glDeleteShader(reorder_shader_id));
6604 /* Bind vertex attributes to a corresponding index number to match
6605 * the same index numbers as ARB_vertex_programs (makes loading
6606 * vertex attributes simpler). With this method, we can use the
6607 * exact same code to load the attributes later for both ARB and
6608 * GLSL shaders.
6610 * We have to do this here because we need to know the Program ID
6611 * in order to make the bindings work, and it has to be done prior
6612 * to linking the GLSL program. */
6613 for (i = 0; map; map >>= 1, ++i)
6615 if (!(map & 1)) continue;
6617 string_buffer_sprintf(tmp_name, "vs_in%u", i);
6618 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
6620 checkGLcall("glBindAttribLocation");
6621 string_buffer_release(&priv->string_buffers, tmp_name);
6624 if (gshader)
6626 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
6627 GL_EXTCALL(glAttachShader(program_id, gs_id));
6628 checkGLcall("glAttachShader");
6630 TRACE("input type %s, output type %s, vertices out %u.\n",
6631 debug_d3dprimitivetype(gshader->u.gs.input_type),
6632 debug_d3dprimitivetype(gshader->u.gs.output_type),
6633 gshader->u.gs.vertices_out);
6634 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
6635 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
6636 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
6637 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
6638 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
6639 gshader->u.gs.vertices_out));
6640 checkGLcall("glProgramParameteriARB");
6642 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
6645 /* Attach GLSL pshader */
6646 if (ps_id)
6648 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
6649 GL_EXTCALL(glAttachShader(program_id, ps_id));
6650 checkGLcall("glAttachShader");
6652 list_add_head(ps_list, &entry->ps.shader_entry);
6655 /* Link the program */
6656 TRACE("Linking GLSL shader program %u.\n", program_id);
6657 GL_EXTCALL(glLinkProgram(program_id));
6658 shader_glsl_validate_link(gl_info, program_id);
6660 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
6661 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
6662 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
6663 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
6664 checkGLcall("Find glsl program uniform locations");
6666 if (pshader && pshader->reg_maps.shader_version.major >= 3
6667 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
6669 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
6670 entry->vs.vertex_color_clamp = GL_FALSE;
6672 else
6674 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
6677 /* Set the shader to allow uniform loading on it */
6678 GL_EXTCALL(glUseProgram(program_id));
6679 checkGLcall("glUseProgram");
6681 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
6682 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
6683 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
6684 * vertex shader with fixed function pixel processing is used we make sure that the card
6685 * supports enough samplers to allow the max number of vertex samplers with all possible
6686 * fixed function fragment processing setups. So once the program is linked these samplers
6687 * won't change. */
6688 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
6690 entry->constant_update_mask = 0;
6691 if (vshader)
6693 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
6694 if (vshader->reg_maps.integer_constants)
6695 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
6696 if (vshader->reg_maps.boolean_constants)
6697 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
6698 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
6700 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
6701 0, gl_info->limits.vertex_uniform_blocks);
6703 else
6705 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
6706 | WINED3D_SHADER_CONST_FFP_PROJ;
6708 for (i = 0; i < MAX_TEXTURES; ++i)
6710 if (entry->vs.texture_matrix_location[i] != -1)
6712 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
6713 break;
6716 if (entry->vs.material_ambient_location != -1)
6717 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
6718 if (entry->vs.light_ambient_location != -1)
6719 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
6722 if (gshader)
6723 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
6724 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
6726 if (ps_id)
6728 if (pshader)
6730 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
6731 if (pshader->reg_maps.integer_constants)
6732 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
6733 if (pshader->reg_maps.boolean_constants)
6734 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
6735 if (entry->ps.ycorrection_location != -1)
6736 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
6738 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
6739 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
6740 gl_info->limits.fragment_uniform_blocks);
6742 else
6744 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
6747 for (i = 0; i < MAX_TEXTURES; ++i)
6749 if (entry->ps.bumpenv_mat_location[i] != -1)
6751 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
6752 break;
6756 if (entry->ps.np2_fixup_location != -1)
6757 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
6758 if (entry->ps.color_key_location != -1)
6759 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
6763 /* Context activation is done by the caller. */
6764 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
6765 BOOL masked)
6767 GLuint program_id;
6768 GLuint vshader_id, pshader_id;
6769 const char *blt_pshader;
6771 static const char blt_vshader[] =
6772 "#version 120\n"
6773 "void main(void)\n"
6774 "{\n"
6775 " gl_Position = gl_Vertex;\n"
6776 " gl_FrontColor = vec4(1.0);\n"
6777 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
6778 "}\n";
6780 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
6782 /* WINED3D_GL_RES_TYPE_TEX_1D */
6783 NULL,
6784 /* WINED3D_GL_RES_TYPE_TEX_2D */
6785 "#version 120\n"
6786 "uniform sampler2D sampler;\n"
6787 "void main(void)\n"
6788 "{\n"
6789 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6790 "}\n",
6791 /* WINED3D_GL_RES_TYPE_TEX_3D */
6792 NULL,
6793 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6794 "#version 120\n"
6795 "uniform samplerCube sampler;\n"
6796 "void main(void)\n"
6797 "{\n"
6798 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6799 "}\n",
6800 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6801 "#version 120\n"
6802 "#extension GL_ARB_texture_rectangle : enable\n"
6803 "uniform sampler2DRect sampler;\n"
6804 "void main(void)\n"
6805 "{\n"
6806 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6807 "}\n",
6810 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
6812 /* WINED3D_GL_RES_TYPE_TEX_1D */
6813 NULL,
6814 /* WINED3D_GL_RES_TYPE_TEX_2D */
6815 "#version 120\n"
6816 "uniform sampler2D sampler;\n"
6817 "uniform vec4 mask;\n"
6818 "void main(void)\n"
6819 "{\n"
6820 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6821 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
6822 "}\n",
6823 /* WINED3D_GL_RES_TYPE_TEX_3D */
6824 NULL,
6825 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
6826 "#version 120\n"
6827 "uniform samplerCube sampler;\n"
6828 "uniform vec4 mask;\n"
6829 "void main(void)\n"
6830 "{\n"
6831 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6832 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
6833 "}\n",
6834 /* WINED3D_GL_RES_TYPE_TEX_RECT */
6835 "#version 120\n"
6836 "#extension GL_ARB_texture_rectangle : enable\n"
6837 "uniform sampler2DRect sampler;\n"
6838 "uniform vec4 mask;\n"
6839 "void main(void)\n"
6840 "{\n"
6841 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
6842 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
6843 "}\n",
6846 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
6847 if (!blt_pshader)
6849 FIXME("tex_type %#x not supported\n", tex_type);
6850 return 0;
6853 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6854 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
6856 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6857 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
6859 program_id = GL_EXTCALL(glCreateProgram());
6860 GL_EXTCALL(glAttachShader(program_id, vshader_id));
6861 GL_EXTCALL(glAttachShader(program_id, pshader_id));
6862 GL_EXTCALL(glLinkProgram(program_id));
6864 shader_glsl_validate_link(gl_info, program_id);
6866 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
6867 * is destroyed
6869 GL_EXTCALL(glDeleteShader(vshader_id));
6870 GL_EXTCALL(glDeleteShader(pshader_id));
6871 return program_id;
6874 /* Context activation is done by the caller. */
6875 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
6876 const struct wined3d_state *state)
6878 struct glsl_context_data *ctx_data = context->shader_backend_data;
6879 const struct wined3d_gl_info *gl_info = context->gl_info;
6880 struct shader_glsl_priv *priv = shader_priv;
6881 GLuint program_id = 0, prev_id = 0;
6882 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
6884 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
6885 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
6887 if (ctx_data->glsl_program)
6889 prev_id = ctx_data->glsl_program->id;
6890 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6892 else
6894 prev_id = 0;
6895 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6898 set_glsl_shader_program(context, state, priv, ctx_data);
6900 if (ctx_data->glsl_program)
6902 program_id = ctx_data->glsl_program->id;
6903 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
6905 else
6907 program_id = 0;
6908 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
6911 if (old_vertex_color_clamp != current_vertex_color_clamp)
6913 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6915 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
6916 checkGLcall("glClampColorARB");
6918 else
6920 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
6924 TRACE("Using GLSL program %u.\n", program_id);
6926 if (prev_id != program_id)
6928 GL_EXTCALL(glUseProgram(program_id));
6929 checkGLcall("glUseProgram");
6931 if (program_id)
6932 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
6936 /* "context" is not necessarily the currently active context. */
6937 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
6939 struct glsl_context_data *ctx_data = context->shader_backend_data;
6941 ctx_data->glsl_program = NULL;
6942 context->shader_update_mask = (1 << WINED3D_SHADER_TYPE_PIXEL)
6943 | (1 << WINED3D_SHADER_TYPE_VERTEX)
6944 | (1 << WINED3D_SHADER_TYPE_GEOMETRY);
6947 /* Context activation is done by the caller. */
6948 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
6950 const struct wined3d_gl_info *gl_info = context->gl_info;
6951 struct shader_glsl_priv *priv = shader_priv;
6953 shader_glsl_invalidate_current_program(context);
6954 GL_EXTCALL(glUseProgram(0));
6955 checkGLcall("glUseProgram");
6957 priv->vertex_pipe->vp_enable(gl_info, FALSE);
6958 priv->fragment_pipe->enable_extension(gl_info, FALSE);
6960 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
6962 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
6963 checkGLcall("glClampColorARB");
6967 /* Context activation is done by the caller. */
6968 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
6969 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
6971 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
6972 struct shader_glsl_priv *priv = shader_priv;
6973 GLuint *blt_program;
6974 GLint loc;
6976 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
6977 if (!*blt_program)
6979 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
6980 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
6981 GL_EXTCALL(glUseProgram(*blt_program));
6982 GL_EXTCALL(glUniform1i(loc, 0));
6984 else
6986 GL_EXTCALL(glUseProgram(*blt_program));
6989 if (masked)
6991 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
6992 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
6996 /* Context activation is done by the caller. */
6997 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
6999 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7000 GLuint program_id;
7002 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7003 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7005 GL_EXTCALL(glUseProgram(program_id));
7006 checkGLcall("glUseProgram");
7009 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7010 const struct glsl_shader_prog_link *program)
7012 const struct glsl_context_data *ctx_data;
7013 struct wined3d_context *context;
7014 unsigned int i;
7016 for (i = 0; i < device->context_count; ++i)
7018 context = device->contexts[i];
7019 ctx_data = context->shader_backend_data;
7021 if (ctx_data->glsl_program == program)
7022 shader_glsl_invalidate_current_program(context);
7026 static void shader_glsl_destroy(struct wined3d_shader *shader)
7028 struct glsl_shader_private *shader_data = shader->backend_data;
7029 struct wined3d_device *device = shader->device;
7030 struct shader_glsl_priv *priv = device->shader_priv;
7031 const struct wined3d_gl_info *gl_info;
7032 const struct list *linked_programs;
7033 struct wined3d_context *context;
7035 if (!shader_data || !shader_data->num_gl_shaders)
7037 HeapFree(GetProcessHeap(), 0, shader_data);
7038 shader->backend_data = NULL;
7039 return;
7042 context = context_acquire(device, NULL);
7043 gl_info = context->gl_info;
7045 TRACE("Deleting linked programs.\n");
7046 linked_programs = &shader->linked_programs;
7047 if (linked_programs->next)
7049 struct glsl_shader_prog_link *entry, *entry2;
7050 UINT i;
7052 switch (shader->reg_maps.shader_version.type)
7054 case WINED3D_SHADER_TYPE_PIXEL:
7056 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7058 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7060 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7061 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7062 checkGLcall("glDeleteShader");
7064 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7066 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7067 struct glsl_shader_prog_link, ps.shader_entry)
7069 shader_glsl_invalidate_contexts_program(device, entry);
7070 delete_glsl_program_entry(priv, gl_info, entry);
7073 break;
7076 case WINED3D_SHADER_TYPE_VERTEX:
7078 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7080 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7082 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7083 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7084 checkGLcall("glDeleteShader");
7086 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7088 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7089 struct glsl_shader_prog_link, vs.shader_entry)
7091 shader_glsl_invalidate_contexts_program(device, entry);
7092 delete_glsl_program_entry(priv, gl_info, entry);
7095 break;
7098 case WINED3D_SHADER_TYPE_GEOMETRY:
7100 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7102 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7104 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7105 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7106 checkGLcall("glDeleteShader");
7108 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7110 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7111 struct glsl_shader_prog_link, gs.shader_entry)
7113 shader_glsl_invalidate_contexts_program(device, entry);
7114 delete_glsl_program_entry(priv, gl_info, entry);
7117 break;
7120 default:
7121 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7122 break;
7126 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7127 shader->backend_data = NULL;
7129 context_release(context);
7132 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7134 const struct glsl_program_key *k = key;
7135 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7136 const struct glsl_shader_prog_link, program_lookup_entry);
7138 if (k->vs_id > prog->vs.id) return 1;
7139 else if (k->vs_id < prog->vs.id) return -1;
7141 if (k->gs_id > prog->gs.id) return 1;
7142 else if (k->gs_id < prog->gs.id) return -1;
7144 if (k->ps_id > prog->ps.id) return 1;
7145 else if (k->ps_id < prog->ps.id) return -1;
7147 return 0;
7150 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7152 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7153 + constant_count * sizeof(*heap->contained)
7154 + constant_count * sizeof(*heap->positions);
7155 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7157 if (!mem)
7159 ERR("Failed to allocate memory\n");
7160 return FALSE;
7163 heap->entries = mem;
7164 heap->entries[1].version = 0;
7165 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7166 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7167 heap->positions = (unsigned int *)(heap->contained + constant_count);
7168 heap->size = 1;
7170 return TRUE;
7173 static void constant_heap_free(struct constant_heap *heap)
7175 HeapFree(GetProcessHeap(), 0, heap->entries);
7178 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7180 wined3d_rb_alloc,
7181 wined3d_rb_realloc,
7182 wined3d_rb_free,
7183 glsl_program_key_compare,
7186 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7187 const struct fragment_pipeline *fragment_pipe)
7189 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7190 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7191 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7192 gl_info->limits.glsl_ps_float_constants)) + 1;
7193 struct fragment_caps fragment_caps;
7194 void *vertex_priv, *fragment_priv;
7196 string_buffer_list_init(&priv->string_buffers);
7198 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7200 ERR("Failed to initialize vertex pipe.\n");
7201 HeapFree(GetProcessHeap(), 0, priv);
7202 return E_FAIL;
7205 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7207 ERR("Failed to initialize fragment pipe.\n");
7208 vertex_pipe->vp_free(device);
7209 HeapFree(GetProcessHeap(), 0, priv);
7210 return E_FAIL;
7213 if (!string_buffer_init(&priv->shader_buffer))
7215 ERR("Failed to initialize shader buffer.\n");
7216 goto fail;
7219 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7220 if (!priv->stack)
7222 ERR("Failed to allocate memory.\n");
7223 goto fail;
7226 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7228 ERR("Failed to initialize vertex shader constant heap\n");
7229 goto fail;
7232 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7234 ERR("Failed to initialize pixel shader constant heap\n");
7235 goto fail;
7238 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7240 ERR("Failed to initialize rbtree.\n");
7241 goto fail;
7244 priv->next_constant_version = 1;
7245 priv->vertex_pipe = vertex_pipe;
7246 priv->fragment_pipe = fragment_pipe;
7247 fragment_pipe->get_caps(gl_info, &fragment_caps);
7248 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7250 device->vertex_priv = vertex_priv;
7251 device->fragment_priv = fragment_priv;
7252 device->shader_priv = priv;
7254 return WINED3D_OK;
7256 fail:
7257 constant_heap_free(&priv->pconst_heap);
7258 constant_heap_free(&priv->vconst_heap);
7259 HeapFree(GetProcessHeap(), 0, priv->stack);
7260 string_buffer_free(&priv->shader_buffer);
7261 fragment_pipe->free_private(device);
7262 vertex_pipe->vp_free(device);
7263 HeapFree(GetProcessHeap(), 0, priv);
7264 return E_OUTOFMEMORY;
7267 /* Context activation is done by the caller. */
7268 static void shader_glsl_free(struct wined3d_device *device)
7270 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7271 struct shader_glsl_priv *priv = device->shader_priv;
7272 int i;
7274 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7276 if (priv->depth_blt_program_full[i])
7278 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7280 if (priv->depth_blt_program_masked[i])
7282 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7286 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7287 constant_heap_free(&priv->pconst_heap);
7288 constant_heap_free(&priv->vconst_heap);
7289 HeapFree(GetProcessHeap(), 0, priv->stack);
7290 string_buffer_list_cleanup(&priv->string_buffers);
7291 string_buffer_free(&priv->shader_buffer);
7292 priv->fragment_pipe->free_private(device);
7293 priv->vertex_pipe->vp_free(device);
7295 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7296 device->shader_priv = NULL;
7299 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7301 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7302 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7305 static void shader_glsl_free_context_data(struct wined3d_context *context)
7307 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7310 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7312 UINT shader_model;
7314 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
7315 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50)
7316 && gl_info->supported[ARB_DRAW_ELEMENTS_BASE_VERTEX] && gl_info->supported[ARB_DRAW_INSTANCED]
7317 && gl_info->supported[ARB_TEXTURE_RG] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7318 shader_model = 4;
7319 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7320 * texldd and texldl instructions. */
7321 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7322 shader_model = 3;
7323 else
7324 shader_model = 2;
7325 TRACE("Shader model %u.\n", shader_model);
7327 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7328 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7329 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7331 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7332 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7334 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7335 * Direct3D minimum requirement.
7337 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7338 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7340 * The problem is that the refrast clamps temporary results in the shader to
7341 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7342 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7343 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7344 * offer a way to query this.
7346 if (shader_model >= 4)
7347 caps->ps_1x_max_value = FLT_MAX;
7348 else
7349 caps->ps_1x_max_value = 1024.0f;
7351 /* Ideally we'd only set caps like sRGB writes here if supported by both
7352 * the shader backend and the fragment pipe, but we can get called before
7353 * shader_glsl_alloc(). */
7354 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7355 | WINED3D_SHADER_CAP_SRGB_WRITE;
7358 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7360 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7362 TRACE("Checking support for fixup:\n");
7363 dump_color_fixup_desc(fixup);
7366 /* We support everything except YUV conversions. */
7367 if (!is_complex_fixup(fixup))
7369 TRACE("[OK]\n");
7370 return TRUE;
7373 TRACE("[FAILED]\n");
7374 return FALSE;
7377 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7379 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7380 /* WINED3DSIH_ADD */ shader_glsl_binop,
7381 /* WINED3DSIH_AND */ shader_glsl_binop,
7382 /* WINED3DSIH_BEM */ shader_glsl_bem,
7383 /* WINED3DSIH_BREAK */ shader_glsl_break,
7384 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7385 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7386 /* WINED3DSIH_CALL */ shader_glsl_call,
7387 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7388 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7389 /* WINED3DSIH_CND */ shader_glsl_cnd,
7390 /* WINED3DSIH_CRS */ shader_glsl_cross,
7391 /* WINED3DSIH_CUT */ shader_glsl_cut,
7392 /* WINED3DSIH_DCL */ shader_glsl_nop,
7393 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7394 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7395 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7396 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7397 /* WINED3DSIH_DEF */ shader_glsl_nop,
7398 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7399 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7400 /* WINED3DSIH_DIV */ shader_glsl_binop,
7401 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7402 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7403 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7404 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7405 /* WINED3DSIH_DST */ shader_glsl_dst,
7406 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7407 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7408 /* WINED3DSIH_ELSE */ shader_glsl_else,
7409 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7410 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7411 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7412 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7413 /* WINED3DSIH_EQ */ shader_glsl_relop,
7414 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7415 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7416 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7417 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7418 /* WINED3DSIH_GE */ shader_glsl_relop,
7419 /* WINED3DSIH_IADD */ shader_glsl_binop,
7420 /* WINED3DSIH_IEQ */ NULL,
7421 /* WINED3DSIH_IF */ shader_glsl_if,
7422 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7423 /* WINED3DSIH_IGE */ shader_glsl_relop,
7424 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7425 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7426 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7427 /* WINED3DSIH_LABEL */ shader_glsl_label,
7428 /* WINED3DSIH_LD */ NULL,
7429 /* WINED3DSIH_LIT */ shader_glsl_lit,
7430 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7431 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7432 /* WINED3DSIH_LOOP */ shader_glsl_loop,
7433 /* WINED3DSIH_LRP */ shader_glsl_lrp,
7434 /* WINED3DSIH_LT */ shader_glsl_relop,
7435 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
7436 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
7437 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
7438 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
7439 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
7440 /* WINED3DSIH_MAD */ shader_glsl_mad,
7441 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
7442 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
7443 /* WINED3DSIH_MOV */ shader_glsl_mov,
7444 /* WINED3DSIH_MOVA */ shader_glsl_mov,
7445 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
7446 /* WINED3DSIH_MUL */ shader_glsl_binop,
7447 /* WINED3DSIH_NE */ shader_glsl_relop,
7448 /* WINED3DSIH_NOP */ shader_glsl_nop,
7449 /* WINED3DSIH_NRM */ shader_glsl_nrm,
7450 /* WINED3DSIH_OR */ shader_glsl_binop,
7451 /* WINED3DSIH_PHASE */ shader_glsl_nop,
7452 /* WINED3DSIH_POW */ shader_glsl_pow,
7453 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
7454 /* WINED3DSIH_REP */ shader_glsl_rep,
7455 /* WINED3DSIH_RET */ shader_glsl_ret,
7456 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
7457 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
7458 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
7459 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
7460 /* WINED3DSIH_SAMPLE_LOD */ NULL,
7461 /* WINED3DSIH_SETP */ NULL,
7462 /* WINED3DSIH_SGE */ shader_glsl_compare,
7463 /* WINED3DSIH_SGN */ shader_glsl_sgn,
7464 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
7465 /* WINED3DSIH_SLT */ shader_glsl_compare,
7466 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
7467 /* WINED3DSIH_SUB */ shader_glsl_binop,
7468 /* WINED3DSIH_TEX */ shader_glsl_tex,
7469 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
7470 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
7471 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
7472 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
7473 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
7474 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
7475 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
7476 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
7477 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
7478 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
7479 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
7480 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
7481 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
7482 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
7483 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
7484 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
7485 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
7486 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
7487 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
7488 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
7489 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
7490 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
7491 /* WINED3DSIH_UGE */ shader_glsl_relop,
7492 /* WINED3DSIH_USHR */ shader_glsl_binop,
7493 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
7494 /* WINED3DSIH_XOR */ shader_glsl_binop,
7497 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
7498 SHADER_HANDLER hw_fct;
7500 /* Select handler */
7501 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
7503 /* Unhandled opcode */
7504 if (!hw_fct)
7506 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
7507 return;
7509 hw_fct(ins);
7511 shader_glsl_add_instruction_modifiers(ins);
7514 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
7516 struct shader_glsl_priv *priv = shader_priv;
7518 return priv->ffp_proj_control;
7521 const struct wined3d_shader_backend_ops glsl_shader_backend =
7523 shader_glsl_handle_instruction,
7524 shader_glsl_select,
7525 shader_glsl_disable,
7526 shader_glsl_select_depth_blt,
7527 shader_glsl_deselect_depth_blt,
7528 shader_glsl_update_float_vertex_constants,
7529 shader_glsl_update_float_pixel_constants,
7530 shader_glsl_load_constants,
7531 shader_glsl_destroy,
7532 shader_glsl_alloc,
7533 shader_glsl_free,
7534 shader_glsl_allocate_context_data,
7535 shader_glsl_free_context_data,
7536 shader_glsl_get_caps,
7537 shader_glsl_color_fixup_supported,
7538 shader_glsl_has_ffp_proj_control,
7541 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7543 if (enable)
7544 gl_info->gl_ops.gl.p_glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7545 else
7546 gl_info->gl_ops.gl.p_glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB);
7547 checkGLcall("GL_VERTEX_PROGRAM_POINT_SIZE_ARB");
7550 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
7552 caps->xyzrhw = TRUE;
7553 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
7554 caps->max_vertex_blend_matrices = 1;
7555 caps->max_vertex_blend_matrix_index = 0;
7556 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
7557 | WINED3DVTXPCAPS_MATERIALSOURCE7
7558 | WINED3DVTXPCAPS_VERTEXFOG
7559 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
7560 | WINED3DVTXPCAPS_POSITIONALLIGHTS
7561 | WINED3DVTXPCAPS_LOCALVIEWER
7562 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
7563 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
7564 caps->max_user_clip_planes = gl_info->limits.clipplanes;
7565 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
7568 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7570 struct shader_glsl_priv *priv;
7572 if (shader_backend == &glsl_shader_backend)
7574 priv = shader_priv;
7576 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
7578 ERR("Failed to initialize rbtree.\n");
7579 return NULL;
7582 return priv;
7585 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
7587 return NULL;
7590 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
7592 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7593 struct glsl_ffp_vertex_shader, desc.entry);
7594 struct glsl_shader_prog_link *program, *program2;
7595 struct glsl_ffp_destroy_ctx *ctx = context;
7597 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7598 struct glsl_shader_prog_link, vs.shader_entry)
7600 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7602 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7603 HeapFree(GetProcessHeap(), 0, shader);
7606 /* Context activation is done by the caller. */
7607 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
7609 struct shader_glsl_priv *priv = device->vertex_priv;
7610 struct glsl_ffp_destroy_ctx ctx;
7612 ctx.priv = priv;
7613 ctx.gl_info = &device->adapter->gl_info;
7614 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
7617 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
7618 const struct wined3d_state *state, DWORD state_id)
7620 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7623 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
7624 const struct wined3d_state *state, DWORD state_id)
7626 const struct wined3d_gl_info *gl_info = context->gl_info;
7627 BOOL transformed = context->stream_info.position_transformed;
7628 BOOL wasrhw = context->last_was_rhw;
7629 unsigned int i;
7631 context->last_was_rhw = transformed;
7633 if (!use_vs(state))
7635 if (context->last_was_vshader)
7637 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7638 clipplane(context, state, STATE_CLIPPLANE(i));
7641 if (transformed != wasrhw)
7642 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7644 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7646 /* Because of settings->texcoords, we have to always regenerate the
7647 * vertex shader on a vdecl change.
7648 * TODO: Just always output all the texcoords when there are enough
7649 * varyings available to drop the dependency. */
7650 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7652 if (use_ps(state)
7653 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
7654 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
7655 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
7657 else
7659 if (!context->last_was_vshader)
7661 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
7662 for (i = 0; i < gl_info->limits.clipplanes; ++i)
7663 clipplane(context, state, STATE_CLIPPLANE(i));
7667 context->last_was_vshader = use_vs(state);
7670 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
7671 const struct wined3d_state *state, DWORD state_id)
7673 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7674 /* Different vertex shaders potentially require a different vertex attributes setup. */
7675 if (!isStateDirty(context, STATE_VDECL))
7676 context_apply_state(context, state, STATE_VDECL);
7679 static void glsl_vertex_pipe_world(struct wined3d_context *context,
7680 const struct wined3d_state *state, DWORD state_id)
7682 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
7685 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
7687 const struct wined3d_gl_info *gl_info = context->gl_info;
7688 unsigned int k;
7690 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7691 | WINED3D_SHADER_CONST_FFP_LIGHTS;
7693 for (k = 0; k < gl_info->limits.clipplanes; ++k)
7695 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
7696 clipplane(context, state, STATE_CLIPPLANE(k));
7699 if (context->swapchain->device->vertexBlendUsed)
7701 static int warned;
7703 if (!warned++)
7704 FIXME("Vertex blending emulation.\n");
7708 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
7709 const struct wined3d_state *state, DWORD state_id)
7711 /* Table fog behavior depends on the projection matrix. */
7712 if (state->render_states[WINED3D_RS_FOGENABLE]
7713 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
7714 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
7715 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
7718 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
7719 const struct wined3d_state *state, DWORD state_id)
7721 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
7722 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
7723 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
7724 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
7725 state_pscale(context, state, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE));
7726 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7729 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
7730 const struct wined3d_state *state, DWORD state_id)
7732 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7735 static void glsl_vertex_pipe_material(struct wined3d_context *context,
7736 const struct wined3d_state *state, DWORD state_id)
7738 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7741 static void glsl_vertex_pipe_light(struct wined3d_context *context,
7742 const struct wined3d_state *state, DWORD state_id)
7744 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7747 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
7749 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
7750 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
7751 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
7752 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
7753 /* Clip planes */
7754 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
7755 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
7756 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
7757 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
7758 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
7759 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
7760 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
7761 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
7762 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
7763 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
7764 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
7765 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
7766 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
7767 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
7768 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
7769 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
7770 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
7771 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
7772 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
7773 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
7774 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
7775 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
7776 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
7777 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
7778 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
7779 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
7780 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
7781 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
7782 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
7783 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
7784 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
7785 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
7786 /* Lights */
7787 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7788 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7789 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7790 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7791 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7792 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7793 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7794 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7795 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7796 /* Viewport */
7797 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
7798 /* Transform states */
7799 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
7800 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
7801 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7802 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7803 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7804 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7805 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7806 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7807 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7808 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
7809 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
7810 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7811 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7812 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7813 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7814 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7815 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7816 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7817 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7818 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7819 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7820 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7821 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7822 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7823 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7824 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7825 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7826 /* Fog */
7827 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7828 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7829 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7830 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7831 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
7832 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
7833 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7834 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
7835 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
7836 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7837 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7838 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7839 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7840 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7841 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7842 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7843 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7844 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_arb }, ARB_POINT_PARAMETERS },
7845 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_ext }, EXT_POINT_PARAMETERS },
7846 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), state_psizemin_w }, WINED3D_GL_EXT_NONE },
7847 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
7848 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
7849 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), state_pscale }, WINED3D_GL_EXT_NONE },
7850 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7851 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7852 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
7853 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, ARB_POINT_PARAMETERS },
7854 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, EXT_POINT_PARAMETERS },
7855 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
7856 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7857 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
7858 /* NP2 texture matrix fixups. They are not needed if
7859 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
7860 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
7861 * matrix. */
7862 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7863 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7864 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7865 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7866 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7867 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7868 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7869 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7870 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7871 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7872 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7873 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7874 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7875 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7876 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7877 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7878 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7879 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7880 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7881 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7882 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7883 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
7884 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
7885 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
7886 {STATE_POINT_SIZE_ENABLE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
7887 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
7890 /* TODO:
7891 * - This currently depends on GL fixed function functions to set things
7892 * like light parameters. Ideally we'd use regular uniforms for that.
7893 * - In part because of the previous point, much of this is modelled after
7894 * GL fixed function, and has much of the same limitations. For example,
7895 * D3D spot lights are slightly different from GL spot lights.
7896 * - We can now implement drawing transformed vertices using the GLSL pipe,
7897 * instead of using the immediate mode fallback.
7898 * - Similarly, we don't need the fallback for certain combinations of
7899 * material sources anymore.
7900 * - Implement vertex blending and vertex tweening.
7901 * - Handle WINED3D_TSS_TEXCOORD_INDEX in the shader, instead of duplicating
7902 * attribute arrays in load_tex_coords().
7903 * - Per-vertex point sizes. */
7904 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
7906 glsl_vertex_pipe_vp_enable,
7907 glsl_vertex_pipe_vp_get_caps,
7908 glsl_vertex_pipe_vp_alloc,
7909 glsl_vertex_pipe_vp_free,
7910 glsl_vertex_pipe_vp_states,
7913 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
7915 /* Nothing to do. */
7918 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
7920 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
7921 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
7922 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
7923 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
7924 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
7925 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
7926 | WINED3DTEXOPCAPS_SELECTARG1
7927 | WINED3DTEXOPCAPS_SELECTARG2
7928 | WINED3DTEXOPCAPS_MODULATE4X
7929 | WINED3DTEXOPCAPS_MODULATE2X
7930 | WINED3DTEXOPCAPS_MODULATE
7931 | WINED3DTEXOPCAPS_ADDSIGNED2X
7932 | WINED3DTEXOPCAPS_ADDSIGNED
7933 | WINED3DTEXOPCAPS_ADD
7934 | WINED3DTEXOPCAPS_SUBTRACT
7935 | WINED3DTEXOPCAPS_ADDSMOOTH
7936 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
7937 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
7938 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
7939 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
7940 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
7941 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
7942 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
7943 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
7944 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
7945 | WINED3DTEXOPCAPS_DOTPRODUCT3
7946 | WINED3DTEXOPCAPS_MULTIPLYADD
7947 | WINED3DTEXOPCAPS_LERP
7948 | WINED3DTEXOPCAPS_BUMPENVMAP
7949 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
7950 caps->MaxTextureBlendStages = 8;
7951 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
7954 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
7956 struct shader_glsl_priv *priv;
7958 if (shader_backend == &glsl_shader_backend)
7960 priv = shader_priv;
7962 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
7964 ERR("Failed to initialize rbtree.\n");
7965 return NULL;
7968 return priv;
7971 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
7973 return NULL;
7976 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
7978 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
7979 struct glsl_ffp_fragment_shader, entry.entry);
7980 struct glsl_shader_prog_link *program, *program2;
7981 struct glsl_ffp_destroy_ctx *ctx = context;
7983 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
7984 struct glsl_shader_prog_link, ps.shader_entry)
7986 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
7988 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
7989 HeapFree(GetProcessHeap(), 0, shader);
7992 /* Context activation is done by the caller. */
7993 static void glsl_fragment_pipe_free(struct wined3d_device *device)
7995 struct shader_glsl_priv *priv = device->fragment_priv;
7996 struct glsl_ffp_destroy_ctx ctx;
7998 ctx.priv = priv;
7999 ctx.gl_info = &device->adapter->gl_info;
8000 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8003 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8004 const struct wined3d_state *state, DWORD state_id)
8006 context->last_was_pshader = use_ps(state);
8008 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8011 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8012 const struct wined3d_state *state, DWORD state_id)
8014 BOOL use_vshader = use_vs(state);
8015 enum fogsource new_source;
8016 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8017 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8019 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8021 if (!state->render_states[WINED3D_RS_FOGENABLE])
8022 return;
8024 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8026 if (use_vshader)
8027 new_source = FOGSOURCE_VS;
8028 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8029 new_source = FOGSOURCE_COORD;
8030 else
8031 new_source = FOGSOURCE_FFP;
8033 else
8035 new_source = FOGSOURCE_FFP;
8038 if (new_source != context->fog_source || fogstart == fogend)
8040 context->fog_source = new_source;
8041 state_fogstartend(context, state, STATE_RENDER(WINED3D_RS_FOGSTART));
8045 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8046 const struct wined3d_state *state, DWORD state_id)
8048 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8049 glsl_fragment_pipe_fog(context, state, state_id);
8052 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8053 const struct wined3d_state *state, DWORD state_id)
8055 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8058 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8059 const struct wined3d_state *state, DWORD state_id)
8061 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8064 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8065 const struct wined3d_state *state, DWORD state_id)
8067 const struct wined3d_gl_info *gl_info = context->gl_info;
8068 int glParm;
8069 float ref;
8071 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8073 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8075 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8076 checkGLcall("glEnable GL_ALPHA_TEST");
8078 else
8080 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8081 checkGLcall("glDisable GL_ALPHA_TEST");
8082 return;
8085 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8086 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8088 if (glParm)
8090 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8091 checkGLcall("glAlphaFunc");
8095 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8096 const struct wined3d_state *state, DWORD state_id)
8098 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8101 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8103 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8104 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8105 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8106 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8107 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8108 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8109 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8110 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8111 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8112 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8113 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8114 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8115 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8116 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8117 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8118 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8119 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8120 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8121 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8122 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8123 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8124 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8125 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8126 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8127 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8128 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8129 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8130 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8131 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8132 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8133 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8134 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8135 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8136 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8137 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8138 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8139 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8140 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8141 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8142 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8143 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8144 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8145 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8146 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8147 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8148 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8149 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8150 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8151 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8152 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8153 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8154 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8155 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8156 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8157 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8158 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8159 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8160 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8161 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8162 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8163 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8164 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8165 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8166 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8167 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8168 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8169 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8170 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8171 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8172 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8173 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8174 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8175 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8176 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8177 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8178 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8179 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8180 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8181 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8182 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8183 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8184 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8185 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8186 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), state_fogstartend }, WINED3D_GL_EXT_NONE },
8187 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8188 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8189 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8190 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), state_fogcolor }, WINED3D_GL_EXT_NONE },
8191 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), state_fogdensity }, WINED3D_GL_EXT_NONE },
8192 {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 },
8193 {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 },
8194 {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 },
8195 {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 },
8196 {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 },
8197 {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 },
8198 {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 },
8199 {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 },
8200 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8201 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8202 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8203 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8204 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8205 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8206 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8207 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8208 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8209 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8212 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8214 return TRUE;
8217 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8221 const struct fragment_pipeline glsl_fragment_pipe =
8223 glsl_fragment_pipe_enable,
8224 glsl_fragment_pipe_get_caps,
8225 glsl_fragment_pipe_alloc,
8226 glsl_fragment_pipe_free,
8227 glsl_fragment_pipe_alloc_context_data,
8228 glsl_fragment_pipe_free_context_data,
8229 shader_glsl_color_fixup_supported,
8230 glsl_fragment_pipe_state_template,