wined3d: Implement SM4 imad instruction.
[wine.git] / dlls / wined3d / glsl_shader.c
blob07a8fc4d3a69d2330dad106ee6b000db1964a5ed
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 0x01
48 #define WINED3D_GLSL_SAMPLE_NPOT 0x02
49 #define WINED3D_GLSL_SAMPLE_LOD 0x04
50 #define WINED3D_GLSL_SAMPLE_GRAD 0x08
51 #define WINED3D_GLSL_SAMPLE_LOAD 0x10
53 struct glsl_dst_param
55 char reg_name[150];
56 char mask_str[6];
59 struct glsl_src_param
61 char reg_name[150];
62 char param_str[200];
65 struct glsl_sample_function
67 struct wined3d_string_buffer *name;
68 DWORD coord_mask;
69 enum wined3d_data_type data_type;
70 BOOL output_single_component;
73 enum heap_node_op
75 HEAP_NODE_TRAVERSE_LEFT,
76 HEAP_NODE_TRAVERSE_RIGHT,
77 HEAP_NODE_POP,
80 struct constant_entry
82 unsigned int idx;
83 unsigned int version;
86 struct constant_heap
88 struct constant_entry *entries;
89 BOOL *contained;
90 unsigned int *positions;
91 unsigned int size;
94 /* GLSL shader private data */
95 struct shader_glsl_priv {
96 struct wined3d_string_buffer shader_buffer;
97 struct wined3d_string_buffer_list string_buffers;
98 struct wine_rb_tree program_lookup;
99 struct constant_heap vconst_heap;
100 struct constant_heap pconst_heap;
101 unsigned char *stack;
102 GLuint depth_blt_program_full[WINED3D_GL_RES_TYPE_COUNT];
103 GLuint depth_blt_program_masked[WINED3D_GL_RES_TYPE_COUNT];
104 UINT next_constant_version;
106 const struct wined3d_vertex_pipe_ops *vertex_pipe;
107 const struct fragment_pipeline *fragment_pipe;
108 struct wine_rb_tree ffp_vertex_shaders;
109 struct wine_rb_tree ffp_fragment_shaders;
110 BOOL ffp_proj_control;
111 BOOL legacy_lighting;
114 struct glsl_vs_program
116 struct list shader_entry;
117 GLuint id;
118 GLenum vertex_color_clamp;
119 GLint *uniform_f_locations;
120 GLint uniform_i_locations[MAX_CONST_I];
121 GLint uniform_b_locations[MAX_CONST_B];
122 GLint pos_fixup_location;
124 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
125 GLint projection_matrix_location;
126 GLint normal_matrix_location;
127 GLint texture_matrix_location[MAX_TEXTURES];
128 GLint material_ambient_location;
129 GLint material_diffuse_location;
130 GLint material_specular_location;
131 GLint material_emissive_location;
132 GLint material_shininess_location;
133 GLint light_ambient_location;
134 struct
136 GLint diffuse;
137 GLint specular;
138 GLint ambient;
139 GLint position;
140 GLint direction;
141 GLint range;
142 GLint falloff;
143 GLint c_att;
144 GLint l_att;
145 GLint q_att;
146 GLint cos_htheta;
147 GLint cos_hphi;
148 } light_location[MAX_ACTIVE_LIGHTS];
149 GLint pointsize_location;
150 GLint pointsize_min_location;
151 GLint pointsize_max_location;
152 GLint pointsize_c_att_location;
153 GLint pointsize_l_att_location;
154 GLint pointsize_q_att_location;
157 struct glsl_gs_program
159 struct list shader_entry;
160 GLuint id;
163 struct glsl_ps_program
165 struct list shader_entry;
166 GLuint id;
167 GLint *uniform_f_locations;
168 GLint uniform_i_locations[MAX_CONST_I];
169 GLint uniform_b_locations[MAX_CONST_B];
170 GLint bumpenv_mat_location[MAX_TEXTURES];
171 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
172 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
173 GLint tss_constant_location[MAX_TEXTURES];
174 GLint tex_factor_location;
175 GLint specular_enable_location;
176 GLint fog_color_location;
177 GLint fog_density_location;
178 GLint fog_end_location;
179 GLint fog_scale_location;
180 GLint ycorrection_location;
181 GLint np2_fixup_location;
182 GLint color_key_location;
183 const struct ps_np2fixup_info *np2_fixup_info;
186 /* Struct to maintain data about a linked GLSL program */
187 struct glsl_shader_prog_link
189 struct wine_rb_entry program_lookup_entry;
190 struct glsl_vs_program vs;
191 struct glsl_gs_program gs;
192 struct glsl_ps_program ps;
193 GLuint id;
194 DWORD constant_update_mask;
195 UINT constant_version;
198 struct glsl_program_key
200 GLuint vs_id;
201 GLuint gs_id;
202 GLuint ps_id;
205 struct shader_glsl_ctx_priv {
206 const struct vs_compile_args *cur_vs_args;
207 const struct ps_compile_args *cur_ps_args;
208 struct ps_np2fixup_info *cur_np2fixup_info;
209 struct wined3d_string_buffer_list *string_buffers;
212 struct glsl_context_data
214 struct glsl_shader_prog_link *glsl_program;
217 struct glsl_ps_compiled_shader
219 struct ps_compile_args args;
220 struct ps_np2fixup_info np2fixup;
221 GLuint id;
224 struct glsl_vs_compiled_shader
226 struct vs_compile_args args;
227 GLuint id;
230 struct glsl_gs_compiled_shader
232 GLuint id;
235 struct glsl_shader_private
237 union
239 struct glsl_vs_compiled_shader *vs;
240 struct glsl_gs_compiled_shader *gs;
241 struct glsl_ps_compiled_shader *ps;
242 } gl_shaders;
243 UINT num_gl_shaders, shader_array_size;
246 struct glsl_ffp_vertex_shader
248 struct wined3d_ffp_vs_desc desc;
249 GLuint id;
250 struct list linked_programs;
253 struct glsl_ffp_fragment_shader
255 struct ffp_frag_desc entry;
256 GLuint id;
257 struct list linked_programs;
260 struct glsl_ffp_destroy_ctx
262 struct shader_glsl_priv *priv;
263 const struct wined3d_gl_info *gl_info;
266 static const char *debug_gl_shader_type(GLenum type)
268 switch (type)
270 #define WINED3D_TO_STR(u) case u: return #u
271 WINED3D_TO_STR(GL_VERTEX_SHADER);
272 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
273 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
274 #undef WINED3D_TO_STR
275 default:
276 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
280 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
282 switch (type)
284 case WINED3D_SHADER_TYPE_VERTEX:
285 return "vs";
287 case WINED3D_SHADER_TYPE_GEOMETRY:
288 return "gs";
290 case WINED3D_SHADER_TYPE_PIXEL:
291 return "ps";
293 default:
294 FIXME("Unhandled shader type %#x.\n", type);
295 return "unknown";
299 static const char *shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
300 const struct wined3d_shader_version *version)
302 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
303 return "#version 150";
304 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
305 return "#version 130";
306 else
307 return "#version 120";
310 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
312 char str[4][17];
314 wined3d_ftoa(values[0], str[0]);
315 wined3d_ftoa(values[1], str[1]);
316 wined3d_ftoa(values[2], str[2]);
317 wined3d_ftoa(values[3], str[3]);
318 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
321 static const char *get_info_log_line(const char **ptr)
323 const char *p, *q;
325 p = *ptr;
326 if (!(q = strstr(p, "\n")))
328 if (!*p) return NULL;
329 *ptr += strlen(p);
330 return p;
332 *ptr = q + 1;
334 return p;
337 /* Context activation is done by the caller. */
338 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
340 int length = 0;
341 char *log;
343 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
344 return;
346 if (program)
347 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
348 else
349 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
351 /* A size of 1 is just a null-terminated string, so the log should be bigger than
352 * that if there are errors. */
353 if (length > 1)
355 const char *ptr, *line;
357 log = HeapAlloc(GetProcessHeap(), 0, length);
358 /* The info log is supposed to be zero-terminated, but at least some
359 * versions of fglrx don't terminate the string properly. The reported
360 * length does include the terminator, so explicitly set it to zero
361 * here. */
362 log[length - 1] = 0;
363 if (program)
364 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
365 else
366 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
368 ptr = log;
369 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
371 WARN("Info log received from GLSL shader #%u:\n", id);
372 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
374 else
376 FIXME("Info log received from GLSL shader #%u:\n", id);
377 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
379 HeapFree(GetProcessHeap(), 0, log);
383 /* Context activation is done by the caller. */
384 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
386 const char *ptr, *line;
388 TRACE("Compiling shader object %u.\n", shader);
390 if (TRACE_ON(d3d_shader))
392 ptr = src;
393 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
396 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
397 checkGLcall("glShaderSource");
398 GL_EXTCALL(glCompileShader(shader));
399 checkGLcall("glCompileShader");
400 print_glsl_info_log(gl_info, shader, FALSE);
403 /* Context activation is done by the caller. */
404 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
406 GLint i, shader_count, source_size = -1;
407 GLuint *shaders;
408 char *source = NULL;
410 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
411 shaders = HeapAlloc(GetProcessHeap(), 0, shader_count * sizeof(*shaders));
412 if (!shaders)
414 ERR("Failed to allocate shader array memory.\n");
415 return;
418 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
419 for (i = 0; i < shader_count; ++i)
421 const char *ptr, *line;
422 GLint tmp;
424 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
426 if (source_size < tmp)
428 HeapFree(GetProcessHeap(), 0, source);
430 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
431 if (!source)
433 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
434 HeapFree(GetProcessHeap(), 0, shaders);
435 return;
437 source_size = tmp;
440 FIXME("Shader %u:\n", shaders[i]);
441 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
442 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
443 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
444 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
445 FIXME("\n");
447 ptr = source;
448 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
449 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
450 FIXME("\n");
453 HeapFree(GetProcessHeap(), 0, source);
454 HeapFree(GetProcessHeap(), 0, shaders);
457 /* Context activation is done by the caller. */
458 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
460 GLint tmp;
462 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
463 return;
465 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
466 if (!tmp)
468 FIXME("Program %u link status invalid.\n", program);
469 shader_glsl_dump_program_source(gl_info, program);
472 print_glsl_info_log(gl_info, program, TRUE);
475 /* Context activation is done by the caller. */
476 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
477 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
479 unsigned int mapped_unit;
480 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
481 const char *prefix;
482 unsigned int i, j;
483 GLint name_loc;
485 static const struct
487 enum wined3d_shader_type type;
488 unsigned int base_idx;
489 unsigned int count;
491 sampler_info[] =
493 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
494 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
497 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
499 prefix = shader_glsl_get_prefix(sampler_info[i].type);
501 for (j = 0; j < sampler_info[i].count; ++j)
503 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
504 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
505 if (name_loc == -1)
506 continue;
508 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
509 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
511 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
512 continue;
515 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
516 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
519 checkGLcall("glUniform1i");
520 string_buffer_release(&priv->string_buffers, sampler_name);
523 /* Context activation is done by the caller. */
524 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
525 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
527 unsigned int start = ~0U, end = 0;
528 int stack_idx = 0;
529 unsigned int heap_idx = 1;
530 unsigned int idx;
532 if (heap->entries[heap_idx].version <= version) return;
534 idx = heap->entries[heap_idx].idx;
535 if (constant_locations[idx] != -1)
536 start = end = idx;
537 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
539 while (stack_idx >= 0)
541 /* Note that we fall through to the next case statement. */
542 switch(stack[stack_idx])
544 case HEAP_NODE_TRAVERSE_LEFT:
546 unsigned int left_idx = heap_idx << 1;
547 if (left_idx < heap->size && heap->entries[left_idx].version > version)
549 heap_idx = left_idx;
550 idx = heap->entries[heap_idx].idx;
551 if (constant_locations[idx] != -1)
553 if (start > idx)
554 start = idx;
555 if (end < idx)
556 end = idx;
559 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
560 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
561 break;
565 case HEAP_NODE_TRAVERSE_RIGHT:
567 unsigned int right_idx = (heap_idx << 1) + 1;
568 if (right_idx < heap->size && heap->entries[right_idx].version > version)
570 heap_idx = right_idx;
571 idx = heap->entries[heap_idx].idx;
572 if (constant_locations[idx] != -1)
574 if (start > idx)
575 start = idx;
576 if (end < idx)
577 end = idx;
580 stack[stack_idx++] = HEAP_NODE_POP;
581 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
582 break;
586 case HEAP_NODE_POP:
587 heap_idx >>= 1;
588 --stack_idx;
589 break;
592 if (start <= end)
593 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start * 4]));
594 checkGLcall("walk_constant_heap()");
597 /* Context activation is done by the caller. */
598 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
600 GLfloat clamped_constant[4];
602 if (location == -1) return;
604 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
605 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
606 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
607 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
609 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
612 /* Context activation is done by the caller. */
613 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
614 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
616 int stack_idx = 0;
617 unsigned int heap_idx = 1;
618 unsigned int idx;
620 if (heap->entries[heap_idx].version <= version) return;
622 idx = heap->entries[heap_idx].idx;
623 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
624 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
626 while (stack_idx >= 0)
628 /* Note that we fall through to the next case statement. */
629 switch(stack[stack_idx])
631 case HEAP_NODE_TRAVERSE_LEFT:
633 unsigned int left_idx = heap_idx << 1;
634 if (left_idx < heap->size && heap->entries[left_idx].version > version)
636 heap_idx = left_idx;
637 idx = heap->entries[heap_idx].idx;
638 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
640 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
641 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
642 break;
646 case HEAP_NODE_TRAVERSE_RIGHT:
648 unsigned int right_idx = (heap_idx << 1) + 1;
649 if (right_idx < heap->size && heap->entries[right_idx].version > version)
651 heap_idx = right_idx;
652 idx = heap->entries[heap_idx].idx;
653 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
655 stack[stack_idx++] = HEAP_NODE_POP;
656 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
657 break;
661 case HEAP_NODE_POP:
662 heap_idx >>= 1;
663 --stack_idx;
664 break;
667 checkGLcall("walk_constant_heap_clamped()");
670 /* Context activation is done by the caller. */
671 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
672 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
673 unsigned char *stack, UINT version)
675 const struct wined3d_shader_lconst *lconst;
677 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
678 if (shader->reg_maps.shader_version.major == 1
679 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
680 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
681 else
682 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
684 if (!shader->load_local_constsF)
686 TRACE("No need to load local float constants for this shader\n");
687 return;
690 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
691 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
693 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
695 checkGLcall("glUniform4fv()");
698 /* Context activation is done by the caller. */
699 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
700 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
702 unsigned int i;
703 struct list* ptr;
705 for (i = 0; constants_set; constants_set >>= 1, ++i)
707 if (!(constants_set & 1)) continue;
709 /* We found this uniform name in the program - go ahead and send the data */
710 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i * 4]));
713 /* Load immediate constants */
714 ptr = list_head(&shader->constantsI);
715 while (ptr)
717 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
718 unsigned int idx = lconst->idx;
719 const GLint *values = (const GLint *)lconst->value;
721 /* We found this uniform name in the program - go ahead and send the data */
722 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
723 ptr = list_next(&shader->constantsI, ptr);
725 checkGLcall("glUniform4iv()");
728 /* Context activation is done by the caller. */
729 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
730 const GLint locations[MAX_CONST_B], const BOOL *constants, WORD constants_set)
732 unsigned int i;
733 struct list* ptr;
735 for (i = 0; constants_set; constants_set >>= 1, ++i)
737 if (!(constants_set & 1)) continue;
739 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
742 /* Load immediate constants */
743 ptr = list_head(&shader->constantsB);
744 while (ptr)
746 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
747 unsigned int idx = lconst->idx;
748 const GLint *values = (const GLint *)lconst->value;
750 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
751 ptr = list_next(&shader->constantsB, ptr);
753 checkGLcall("glUniform1iv()");
756 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
758 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
761 /* Context activation is done by the caller (state handler). */
762 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
763 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
765 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
766 UINT fixup = ps->np2_fixup_info->active;
767 UINT i;
769 for (i = 0; fixup; fixup >>= 1, ++i)
771 const struct wined3d_texture *tex = state->textures[i];
772 unsigned char idx = ps->np2_fixup_info->idx[i];
773 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
775 if (!tex)
777 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
778 continue;
781 if (idx % 2)
783 tex_dim[2] = tex->pow2_matrix[0];
784 tex_dim[3] = tex->pow2_matrix[5];
786 else
788 tex_dim[0] = tex->pow2_matrix[0];
789 tex_dim[1] = tex->pow2_matrix[5];
793 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, np2fixup_constants));
796 /* Taken and adapted from Mesa. */
797 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
799 float pos, neg, t, det;
800 struct wined3d_matrix temp;
802 /* Calculate the determinant of upper left 3x3 submatrix and
803 * determine if the matrix is singular. */
804 pos = neg = 0.0f;
805 t = in->_11 * in->_22 * in->_33;
806 if (t >= 0.0f)
807 pos += t;
808 else
809 neg += t;
811 t = in->_21 * in->_32 * in->_13;
812 if (t >= 0.0f)
813 pos += t;
814 else
815 neg += t;
816 t = in->_31 * in->_12 * in->_23;
817 if (t >= 0.0f)
818 pos += t;
819 else
820 neg += t;
822 t = -in->_31 * in->_22 * in->_13;
823 if (t >= 0.0f)
824 pos += t;
825 else
826 neg += t;
827 t = -in->_21 * in->_12 * in->_33;
828 if (t >= 0.0f)
829 pos += t;
830 else
831 neg += t;
833 t = -in->_11 * in->_32 * in->_23;
834 if (t >= 0.0f)
835 pos += t;
836 else
837 neg += t;
839 det = pos + neg;
841 if (fabsf(det) < 1e-25f)
842 return FALSE;
844 det = 1.0f / det;
845 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
846 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
847 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
848 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
849 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
850 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
851 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
852 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
853 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
855 *out = temp;
856 return TRUE;
859 static void swap_rows(float **a, float **b)
861 float *tmp = *a;
863 *a = *b;
864 *b = tmp;
867 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
869 float wtmp[4][8];
870 float m0, m1, m2, m3, s;
871 float *r0, *r1, *r2, *r3;
873 r0 = wtmp[0];
874 r1 = wtmp[1];
875 r2 = wtmp[2];
876 r3 = wtmp[3];
878 r0[0] = m->_11;
879 r0[1] = m->_12;
880 r0[2] = m->_13;
881 r0[3] = m->_14;
882 r0[4] = 1.0f;
883 r0[5] = r0[6] = r0[7] = 0.0f;
885 r1[0] = m->_21;
886 r1[1] = m->_22;
887 r1[2] = m->_23;
888 r1[3] = m->_24;
889 r1[5] = 1.0f;
890 r1[4] = r1[6] = r1[7] = 0.0f;
892 r2[0] = m->_31;
893 r2[1] = m->_32;
894 r2[2] = m->_33;
895 r2[3] = m->_34;
896 r2[6] = 1.0f;
897 r2[4] = r2[5] = r2[7] = 0.0f;
899 r3[0] = m->_41;
900 r3[1] = m->_42;
901 r3[2] = m->_43;
902 r3[3] = m->_44;
903 r3[7] = 1.0f;
904 r3[4] = r3[5] = r3[6] = 0.0f;
906 /* Choose pivot - or die. */
907 if (fabsf(r3[0]) > fabsf(r2[0]))
908 swap_rows(&r3, &r2);
909 if (fabsf(r2[0]) > fabsf(r1[0]))
910 swap_rows(&r2, &r1);
911 if (fabsf(r1[0]) > fabsf(r0[0]))
912 swap_rows(&r1, &r0);
913 if (r0[0] == 0.0f)
914 return FALSE;
916 /* Eliminate first variable. */
917 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
918 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
919 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
920 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
921 s = r0[4];
922 if (s != 0.0f)
924 r1[4] -= m1 * s;
925 r2[4] -= m2 * s;
926 r3[4] -= m3 * s;
928 s = r0[5];
929 if (s != 0.0f)
931 r1[5] -= m1 * s;
932 r2[5] -= m2 * s;
933 r3[5] -= m3 * s;
935 s = r0[6];
936 if (s != 0.0f)
938 r1[6] -= m1 * s;
939 r2[6] -= m2 * s;
940 r3[6] -= m3 * s;
942 s = r0[7];
943 if (s != 0.0f)
945 r1[7] -= m1 * s;
946 r2[7] -= m2 * s;
947 r3[7] -= m3 * s;
950 /* Choose pivot - or die. */
951 if (fabsf(r3[1]) > fabsf(r2[1]))
952 swap_rows(&r3, &r2);
953 if (fabsf(r2[1]) > fabsf(r1[1]))
954 swap_rows(&r2, &r1);
955 if (r1[1] == 0.0f)
956 return FALSE;
958 /* Eliminate second variable. */
959 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
960 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
961 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
962 s = r1[4];
963 if (s != 0.0f)
965 r2[4] -= m2 * s;
966 r3[4] -= m3 * s;
968 s = r1[5];
969 if (s != 0.0f)
971 r2[5] -= m2 * s;
972 r3[5] -= m3 * s;
974 s = r1[6];
975 if (s != 0.0f)
977 r2[6] -= m2 * s;
978 r3[6] -= m3 * s;
980 s = r1[7];
981 if (s != 0.0f)
983 r2[7] -= m2 * s;
984 r3[7] -= m3 * s;
987 /* Choose pivot - or die. */
988 if (fabsf(r3[2]) > fabsf(r2[2]))
989 swap_rows(&r3, &r2);
990 if (r2[2] == 0.0f)
991 return FALSE;
993 /* Eliminate third variable. */
994 m3 = r3[2] / r2[2];
995 r3[3] -= m3 * r2[3];
996 r3[4] -= m3 * r2[4];
997 r3[5] -= m3 * r2[5];
998 r3[6] -= m3 * r2[6];
999 r3[7] -= m3 * r2[7];
1001 /* Last check. */
1002 if (r3[3] == 0.0f)
1003 return FALSE;
1005 /* Back substitute row 3. */
1006 s = 1.0f / r3[3];
1007 r3[4] *= s;
1008 r3[5] *= s;
1009 r3[6] *= s;
1010 r3[7] *= s;
1012 /* Back substitute row 2. */
1013 m2 = r2[3];
1014 s = 1.0f / r2[2];
1015 r2[4] = s * (r2[4] - r3[4] * m2);
1016 r2[5] = s * (r2[5] - r3[5] * m2);
1017 r2[6] = s * (r2[6] - r3[6] * m2);
1018 r2[7] = s * (r2[7] - r3[7] * m2);
1019 m1 = r1[3];
1020 r1[4] -= r3[4] * m1;
1021 r1[5] -= r3[5] * m1;
1022 r1[6] -= r3[6] * m1;
1023 r1[7] -= r3[7] * m1;
1024 m0 = r0[3];
1025 r0[4] -= r3[4] * m0;
1026 r0[5] -= r3[5] * m0;
1027 r0[6] -= r3[6] * m0;
1028 r0[7] -= r3[7] * m0;
1030 /* Back substitute row 1. */
1031 m1 = r1[2];
1032 s = 1.0f / r1[1];
1033 r1[4] = s * (r1[4] - r2[4] * m1);
1034 r1[5] = s * (r1[5] - r2[5] * m1);
1035 r1[6] = s * (r1[6] - r2[6] * m1);
1036 r1[7] = s * (r1[7] - r2[7] * m1);
1037 m0 = r0[2];
1038 r0[4] -= r2[4] * m0;
1039 r0[5] -= r2[5] * m0;
1040 r0[6] -= r2[6] * m0;
1041 r0[7] -= r2[7] * m0;
1043 /* Back substitute row 0. */
1044 m0 = r0[1];
1045 s = 1.0f / r0[0];
1046 r0[4] = s * (r0[4] - r1[4] * m0);
1047 r0[5] = s * (r0[5] - r1[5] * m0);
1048 r0[6] = s * (r0[6] - r1[6] * m0);
1049 r0[7] = s * (r0[7] - r1[7] * m0);
1051 out->_11 = r0[4];
1052 out->_12 = r0[5];
1053 out->_13 = r0[6];
1054 out->_14 = r0[7];
1055 out->_21 = r1[4];
1056 out->_22 = r1[5];
1057 out->_23 = r1[6];
1058 out->_24 = r1[7];
1059 out->_31 = r2[4];
1060 out->_32 = r2[5];
1061 out->_33 = r2[6];
1062 out->_34 = r2[7];
1063 out->_41 = r3[4];
1064 out->_42 = r3[5];
1065 out->_43 = r3[6];
1066 out->_44 = r3[7];
1068 return TRUE;
1071 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1072 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1074 const struct wined3d_gl_info *gl_info = context->gl_info;
1075 float mat[3 * 3];
1076 struct wined3d_matrix mv;
1077 unsigned int i, j;
1079 if (prog->vs.normal_matrix_location == -1)
1080 return;
1082 get_modelview_matrix(context, state, 0, &mv);
1083 if (context->swapchain->device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING)
1084 invert_matrix_3d(&mv, &mv);
1085 else
1086 invert_matrix(&mv, &mv);
1087 /* Tests show that singular modelview matrices are used unchanged as normal
1088 * matrices on D3D3 and older. There seems to be no clearly consistent
1089 * behavior on newer D3D versions so always follow older ddraw behavior. */
1090 for (i = 0; i < 3; ++i)
1091 for (j = 0; j < 3; ++j)
1092 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1094 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1095 checkGLcall("glUniformMatrix3fv");
1098 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1099 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1101 const struct wined3d_gl_info *gl_info = context->gl_info;
1102 struct wined3d_matrix mat;
1104 if (tex >= MAX_TEXTURES)
1105 return;
1106 if (prog->vs.texture_matrix_location[tex] == -1)
1107 return;
1109 get_texture_matrix(context, state, tex, &mat);
1110 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1111 checkGLcall("glUniformMatrix4fv");
1114 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1115 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1117 const struct wined3d_gl_info *gl_info = context->gl_info;
1119 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1121 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1122 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1124 else
1126 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1128 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1130 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1131 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1132 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1133 checkGLcall("setting FFP material uniforms");
1136 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1137 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1139 const struct wined3d_gl_info *gl_info = context->gl_info;
1140 float col[4];
1142 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_AMBIENT], col);
1143 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, col));
1144 checkGLcall("glUniform3fv");
1147 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1148 const struct wined3d_matrix *src2)
1150 struct wined3d_vec4 temp;
1152 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1153 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1154 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1155 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1157 *dest = temp;
1160 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1161 const struct wined3d_state *state, unsigned int light, struct glsl_shader_prog_link *prog)
1163 const struct wined3d_gl_info *gl_info = context->gl_info;
1164 const struct wined3d_light_info *light_info = state->lights[light];
1165 struct wined3d_vec4 vec4;
1166 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1168 if (!light_info)
1169 return;
1171 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1172 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1173 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1175 switch (light_info->OriginalParms.type)
1177 case WINED3D_LIGHT_POINT:
1178 multiply_vector_matrix(&vec4, &light_info->position, view);
1179 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1180 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1181 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1182 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1183 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1184 break;
1186 case WINED3D_LIGHT_SPOT:
1187 multiply_vector_matrix(&vec4, &light_info->position, view);
1188 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1190 multiply_vector_matrix(&vec4, &light_info->direction, view);
1191 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1193 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1194 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1195 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1196 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1197 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1198 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1199 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1200 break;
1202 case WINED3D_LIGHT_DIRECTIONAL:
1203 multiply_vector_matrix(&vec4, &light_info->direction, view);
1204 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1205 break;
1207 case WINED3D_LIGHT_PARALLELPOINT:
1208 multiply_vector_matrix(&vec4, &light_info->position, view);
1209 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1210 break;
1212 default:
1213 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1215 checkGLcall("setting FFP lights uniforms");
1218 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1219 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1221 const struct wined3d_gl_info *gl_info = context->gl_info;
1222 float min, max;
1223 float size, att[3];
1225 get_pointsize_minmax(context, state, &min, &max);
1227 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1228 checkGLcall("glUniform1f");
1229 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1230 checkGLcall("glUniform1f");
1232 get_pointsize(context, state, &size, att);
1234 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1235 checkGLcall("glUniform1f");
1236 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1237 checkGLcall("glUniform1f");
1238 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1239 checkGLcall("glUniform1f");
1240 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1241 checkGLcall("glUniform1f");
1244 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1245 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1247 const struct wined3d_gl_info *gl_info = context->gl_info;
1248 float start, end, scale;
1249 union
1251 DWORD d;
1252 float f;
1253 } tmpvalue;
1254 float col[4];
1256 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_FOGCOLOR], col);
1257 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, col));
1258 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1259 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1260 get_fog_start_end(context, state, &start, &end);
1261 scale = 1.0f / (end - start);
1262 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1263 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1264 checkGLcall("fog emulation uniforms");
1267 /* Context activation is done by the caller (state handler). */
1268 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1269 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1271 struct wined3d_color float_key[2];
1272 const struct wined3d_texture *texture = state->textures[0];
1274 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1275 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1278 /* Context activation is done by the caller (state handler). */
1279 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1280 const struct wined3d_state *state)
1282 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1283 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1284 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1285 const struct wined3d_gl_info *gl_info = context->gl_info;
1286 struct shader_glsl_priv *priv = shader_priv;
1287 float position_fixup[4];
1288 DWORD update_mask;
1290 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1291 UINT constant_version;
1292 int i;
1294 if (!prog) {
1295 /* No GLSL program set - nothing to do. */
1296 return;
1298 constant_version = prog->constant_version;
1299 update_mask = context->constant_update_mask & prog->constant_update_mask;
1301 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1302 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
1303 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1305 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1306 shader_glsl_load_constantsI(vshader, gl_info, prog->vs.uniform_i_locations, state->vs_consts_i,
1307 vshader->reg_maps.integer_constants);
1309 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1310 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1311 vshader->reg_maps.boolean_constants);
1313 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1314 shader_glsl_pointsize_uniform(context, state, prog);
1316 if (update_mask & WINED3D_SHADER_CONST_VS_POS_FIXUP)
1318 shader_get_position_fixup(context, state, position_fixup);
1319 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1320 checkGLcall("glUniform4fv");
1323 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1325 struct wined3d_matrix mat;
1327 get_modelview_matrix(context, state, 0, &mat);
1328 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1329 checkGLcall("glUniformMatrix4fv");
1331 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1334 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1336 struct wined3d_matrix mat;
1338 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1340 if (prog->vs.modelview_matrix_location[i] == -1)
1341 break;
1343 get_modelview_matrix(context, state, i, &mat);
1344 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1345 checkGLcall("glUniformMatrix4fv");
1349 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1351 struct wined3d_matrix projection;
1353 get_projection_matrix(context, state, &projection);
1354 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1355 checkGLcall("glUniformMatrix4fv");
1358 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1360 for (i = 0; i < MAX_TEXTURES; ++i)
1361 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1364 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1365 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1367 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1369 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1370 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1371 shader_glsl_ffp_vertex_light_uniform(context, state, i, prog);
1374 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1375 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
1376 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1378 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1379 shader_glsl_load_constantsI(pshader, gl_info, prog->ps.uniform_i_locations, state->ps_consts_i,
1380 pshader->reg_maps.integer_constants);
1382 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1383 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1384 pshader->reg_maps.boolean_constants);
1386 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1388 for (i = 0; i < MAX_TEXTURES; ++i)
1390 if (prog->ps.bumpenv_mat_location[i] == -1)
1391 continue;
1393 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1394 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1396 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1398 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1399 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1400 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1401 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1405 checkGLcall("bump env uniforms");
1408 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1410 float correction_params[] =
1412 /* position is window relative, not viewport relative */
1413 context->render_offscreen ? 0.0f : (float)context->current_rt->resource.height,
1414 context->render_offscreen ? 1.0f : -1.0f,
1415 0.0f,
1416 0.0f,
1419 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, correction_params));
1422 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1423 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1424 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1425 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1427 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1429 float col[4];
1431 if (prog->ps.tex_factor_location != -1)
1433 D3DCOLORTOGLFLOAT4(state->render_states[WINED3D_RS_TEXTUREFACTOR], col);
1434 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, col));
1437 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1438 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1439 else
1440 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1442 for (i = 0; i < MAX_TEXTURES; ++i)
1444 if (prog->ps.tss_constant_location[i] == -1)
1445 continue;
1447 D3DCOLORTOGLFLOAT4(state->texture_states[i][WINED3D_TSS_CONSTANT], col);
1448 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, col));
1451 checkGLcall("fixed function uniforms");
1454 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1455 shader_glsl_load_fog_uniform(context, state, prog);
1457 if (priv->next_constant_version == UINT_MAX)
1459 TRACE("Max constant version reached, resetting to 0.\n");
1460 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1461 priv->next_constant_version = 1;
1463 else
1465 prog->constant_version = priv->next_constant_version++;
1469 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1471 struct constant_entry *entries = heap->entries;
1472 unsigned int *positions = heap->positions;
1473 unsigned int heap_idx, parent_idx;
1475 if (!heap->contained[idx])
1477 heap_idx = heap->size++;
1478 heap->contained[idx] = TRUE;
1480 else
1482 heap_idx = positions[idx];
1485 while (heap_idx > 1)
1487 parent_idx = heap_idx >> 1;
1489 if (new_version <= entries[parent_idx].version) break;
1491 entries[heap_idx] = entries[parent_idx];
1492 positions[entries[parent_idx].idx] = heap_idx;
1493 heap_idx = parent_idx;
1496 entries[heap_idx].version = new_version;
1497 entries[heap_idx].idx = idx;
1498 positions[idx] = heap_idx;
1501 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1503 struct shader_glsl_priv *priv = device->shader_priv;
1504 struct constant_heap *heap = &priv->vconst_heap;
1505 UINT i;
1507 for (i = start; i < count + start; ++i)
1509 update_heap_entry(heap, i, priv->next_constant_version);
1512 for (i = 0; i < device->context_count; ++i)
1514 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
1518 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1520 struct shader_glsl_priv *priv = device->shader_priv;
1521 struct constant_heap *heap = &priv->pconst_heap;
1522 UINT i;
1524 for (i = start; i < count + start; ++i)
1526 update_heap_entry(heap, i, priv->next_constant_version);
1529 for (i = 0; i < device->context_count; ++i)
1531 device->contexts[i]->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
1535 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1537 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1538 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1539 if(shader_major > 3) return ret;
1541 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1542 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1543 return ret;
1546 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1548 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1551 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1552 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1554 va_list args;
1555 int ret;
1557 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1558 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1559 for (;;)
1561 va_start(args, format);
1562 ret = shader_vaddline(buffer, format, args);
1563 va_end(args);
1564 if (!ret)
1565 return;
1566 if (!string_buffer_resize(buffer, ret))
1567 return;
1571 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1572 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1574 va_list args;
1575 int ret;
1577 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1578 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1579 for (;;)
1581 va_start(args, format);
1582 ret = shader_vaddline(buffer, format, args);
1583 va_end(args);
1584 if (!ret)
1585 return;
1586 if (!string_buffer_resize(buffer, ret))
1587 return;
1591 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1593 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1594 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1595 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1596 unsigned int i;
1598 if (reg_maps->shader_version.major < 3)
1599 return input_reg_used[idx];
1601 for (i = 0; i < input_signature->element_count; ++i)
1603 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1605 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1606 continue;
1608 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1609 && input->semantic_idx == idx)
1611 if (input_reg_used[input->register_idx])
1612 return TRUE;
1613 else
1614 return FALSE;
1617 return FALSE;
1620 /** Generate the variable & register declarations for the GLSL output target */
1621 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1622 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1623 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1625 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1626 const struct wined3d_state *state = &shader->device->state;
1627 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1628 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1629 const struct wined3d_gl_info *gl_info = context->gl_info;
1630 const struct wined3d_fb_state *fb = &shader->device->fb;
1631 unsigned int i, extra_constants_needed = 0;
1632 const struct wined3d_shader_lconst *lconst;
1633 const char *prefix;
1634 DWORD map;
1636 prefix = shader_glsl_get_prefix(version->type);
1638 /* Prototype the subroutines */
1639 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1641 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1644 /* Declare the constants (aka uniforms) */
1645 if (shader->limits->constant_float > 0)
1647 unsigned max_constantsF;
1649 /* Unless the shader uses indirect addressing, always declare the
1650 * maximum array size and ignore that we need some uniforms privately.
1651 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1652 * and immediate values, still declare VC[256]. If the shader needs
1653 * more uniforms than we have it won't work in any case. If it uses
1654 * less, the compiler will figure out which uniforms are really used
1655 * and strip them out. This allows a shader to use c255 on a dx9 card,
1656 * as long as it doesn't also use all the other constants.
1658 * If the shader uses indirect addressing the compiler must assume
1659 * that all declared uniforms are used. In this case, declare only the
1660 * amount that we're assured to have.
1662 * Thus we run into problems in these two cases:
1663 * 1) The shader really uses more uniforms than supported.
1664 * 2) The shader uses indirect addressing, less constants than
1665 * supported, but uses a constant index > #supported consts. */
1666 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1668 /* No indirect addressing here. */
1669 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1671 else
1673 if (reg_maps->usesrelconstF)
1675 /* Subtract the other potential uniforms from the max
1676 * available (bools, ints, and 1 row of projection matrix).
1677 * Subtract another uniform for immediate values, which have
1678 * to be loaded via uniform by the driver as well. The shader
1679 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1680 * shader code, so one vec4 should be enough. (Unfortunately
1681 * the Nvidia driver doesn't store 128 and -128 in one float).
1683 * Writing gl_ClipVertex requires one uniform for each
1684 * clipplane as well. */
1685 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1686 if (vs_args->clip_enabled)
1687 max_constantsF -= gl_info->limits.clipplanes;
1688 max_constantsF -= count_bits(reg_maps->integer_constants);
1689 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1690 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1691 * for now take this into account when calculating the number of available constants
1693 max_constantsF -= count_bits(reg_maps->boolean_constants);
1694 /* Set by driver quirks in directx.c */
1695 max_constantsF -= gl_info->reserved_glsl_constants;
1697 if (max_constantsF < shader->limits->constant_float)
1699 static unsigned int once;
1701 if (!once++)
1702 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
1703 " it may not render correctly.\n");
1704 else
1705 WARN("The hardware does not support enough uniform components to run this shader.\n");
1708 else
1710 max_constantsF = gl_info->limits.glsl_vs_float_constants;
1713 max_constantsF = min(shader->limits->constant_float, max_constantsF);
1714 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
1717 /* Always declare the full set of constants, the compiler can remove the
1718 * unused ones because d3d doesn't (yet) support indirect int and bool
1719 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
1720 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
1721 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
1723 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
1724 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
1726 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1728 if (reg_maps->cb_sizes[i])
1729 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
1730 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
1733 /* Declare texture samplers */
1734 for (i = 0; i < reg_maps->sampler_map.count; ++i)
1736 struct wined3d_shader_sampler_map_entry *entry;
1737 BOOL shadow_sampler, tex_rect;
1738 const char *sampler_type;
1740 entry = &reg_maps->sampler_map.entries[i];
1742 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
1744 ERR("Invalid resource index %u.\n", entry->resource_idx);
1745 continue;
1748 shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << entry->sampler_idx));
1749 switch (reg_maps->resource_info[entry->resource_idx].type)
1751 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
1752 if (shadow_sampler)
1753 sampler_type = "sampler1DShadow";
1754 else
1755 sampler_type = "sampler1D";
1756 break;
1758 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
1759 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
1760 && (ps_args->np2_fixup & (1u << entry->resource_idx))
1761 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
1762 if (shadow_sampler)
1764 if (tex_rect)
1765 sampler_type = "sampler2DRectShadow";
1766 else
1767 sampler_type = "sampler2DShadow";
1769 else
1771 if (tex_rect)
1772 sampler_type = "sampler2DRect";
1773 else
1774 sampler_type = "sampler2D";
1776 break;
1778 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
1779 if (shadow_sampler)
1780 FIXME("Unsupported 3D shadow sampler.\n");
1781 sampler_type = "sampler3D";
1782 break;
1784 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
1785 if (shadow_sampler)
1786 FIXME("Unsupported Cube shadow sampler.\n");
1787 sampler_type = "samplerCube";
1788 break;
1790 default:
1791 sampler_type = "unsupported_sampler";
1792 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[i].type);
1793 break;
1795 shader_addline(buffer, "uniform %s %s_sampler%u;\n", sampler_type, prefix, entry->bind_idx);
1798 /* Declare uniforms for NP2 texcoord fixup:
1799 * This is NOT done inside the loop that declares the texture samplers
1800 * since the NP2 fixup code is currently only used for the GeforceFX
1801 * series and when forcing the ARB_npot extension off. Modern cards just
1802 * skip the code anyway, so put it inside a separate loop. */
1803 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1805 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1806 UINT cur = 0;
1808 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1809 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1810 * samplerNP2Fixup stores texture dimensions and is updated through
1811 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1813 for (i = 0; i < shader->limits->sampler; ++i)
1815 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
1816 continue;
1818 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
1820 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1821 continue;
1824 fixup->idx[i] = cur++;
1827 fixup->num_consts = (cur + 1) >> 1;
1828 fixup->active = ps_args->np2_fixup;
1829 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1832 /* Declare address variables */
1833 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1835 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1838 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1840 for (i = 0; i < shader->input_signature.element_count; ++i)
1842 const struct wined3d_shader_signature_element *e = &shader->input_signature.elements[i];
1843 if (e->sysval_semantic == WINED3D_SV_INSTANCEID)
1844 shader_addline(buffer, "vec4 %s_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1845 prefix, e->register_idx);
1846 else
1847 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, e->register_idx);
1850 if (vs_args->point_size && !vs_args->per_vertex_point_size)
1852 shader_addline(buffer, "uniform struct\n{\n");
1853 shader_addline(buffer, " float size;\n");
1854 shader_addline(buffer, " float size_min;\n");
1855 shader_addline(buffer, " float size_max;\n");
1856 shader_addline(buffer, "} ffp_point;\n");
1859 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && version->major < 3)
1861 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1862 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
1863 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1864 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1867 shader_addline(buffer, "uniform vec4 posFixup;\n");
1868 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits->packed_output);
1870 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
1872 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
1874 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1876 if (version->major < 3 || ps_args->vp_mode != vertexshader)
1878 shader_addline(buffer, "uniform struct\n{\n");
1879 shader_addline(buffer, " vec4 color;\n");
1880 shader_addline(buffer, " float density;\n");
1881 shader_addline(buffer, " float end;\n");
1882 shader_addline(buffer, " float scale;\n");
1883 shader_addline(buffer, "} ffp_fog;\n");
1885 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
1887 if (glsl_is_color_reg_read(shader, 0))
1888 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
1889 if (glsl_is_color_reg_read(shader, 1))
1890 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
1891 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1892 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
1894 else
1896 if (glsl_is_color_reg_read(shader, 0))
1897 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
1898 if (glsl_is_color_reg_read(shader, 1))
1899 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
1900 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
1901 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
1902 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
1906 if (version->major >= 3)
1908 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
1910 if (use_vs(state))
1911 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
1912 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1915 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1917 if (!(map & 1))
1918 continue;
1920 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
1922 if (reg_maps->luminanceparams & (1u << i))
1924 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
1925 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
1926 extra_constants_needed++;
1929 extra_constants_needed++;
1932 if (ps_args->srgb_correction)
1934 shader_addline(buffer, "const vec4 srgb_const0 = ");
1935 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
1936 shader_addline(buffer, ";\n");
1937 shader_addline(buffer, "const vec4 srgb_const1 = ");
1938 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
1939 shader_addline(buffer, ";\n");
1941 if (reg_maps->vpos || reg_maps->usesdsy)
1943 if (shader->limits->constant_float + extra_constants_needed
1944 + 1 < gl_info->limits.glsl_ps_float_constants)
1946 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1947 extra_constants_needed++;
1949 else
1951 float ycorrection[] =
1953 context->render_offscreen ? 0.0f : fb->render_targets[0]->height,
1954 context->render_offscreen ? 1.0f : -1.0f,
1955 0.0f,
1956 0.0f,
1959 /* This happens because we do not have proper tracking of the
1960 * constant registers that are actually used, only the max
1961 * limit of the shader version. */
1962 FIXME("Cannot find a free uniform for vpos correction params\n");
1963 shader_addline(buffer, "const vec4 ycorrection = ");
1964 shader_glsl_append_imm_vec4(buffer, ycorrection);
1965 shader_addline(buffer, ";\n");
1967 shader_addline(buffer, "vec4 vpos;\n");
1971 /* Declare output register temporaries */
1972 if (shader->limits->packed_output)
1973 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
1975 /* Declare temporary variables */
1976 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1978 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1981 /* Declare loop registers aLx */
1982 if (version->major < 4)
1984 for (i = 0; i < reg_maps->loop_depth; ++i)
1986 shader_addline(buffer, "int aL%u;\n", i);
1987 shader_addline(buffer, "int tmpInt%u;\n", i);
1991 /* Temporary variables for matrix operations */
1992 shader_addline(buffer, "vec4 tmp0;\n");
1993 shader_addline(buffer, "vec4 tmp1;\n");
1995 if (!shader->load_local_constsF)
1997 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1999 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2000 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2001 shader_addline(buffer, ";\n");
2005 /* Start the main program. */
2006 shader_addline(buffer, "void main()\n{\n");
2008 /* Direct3D applications expect integer vPos values, while OpenGL drivers
2009 * add approximately 0.5. This causes off-by-one problems as spotted by
2010 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
2011 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
2012 * causes precision troubles when we just subtract 0.5.
2014 * To deal with that, just floor() the position. This will eliminate the
2015 * fraction on all cards.
2017 * TODO: Test how this behaves with multisampling.
2019 * An advantage of floor is that it works even if the driver doesn't add
2020 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
2021 * to return in gl_FragCoord, even though coordinates specify the pixel
2022 * centers instead of the pixel corners. This code will behave correctly
2023 * on drivers that returns integer values. */
2024 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
2026 if (shader->device->wined3d->flags & WINED3D_PIXEL_CENTER_INTEGER)
2027 shader_addline(buffer,
2028 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
2029 else
2030 shader_addline(buffer,
2031 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
2035 /*****************************************************************************
2036 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2038 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2039 ****************************************************************************/
2041 /* Prototypes */
2042 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2043 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2045 /** Used for opcode modifiers - They multiply the result by the specified amount */
2046 static const char * const shift_glsl_tab[] = {
2047 "", /* 0 (none) */
2048 "2.0 * ", /* 1 (x2) */
2049 "4.0 * ", /* 2 (x4) */
2050 "8.0 * ", /* 3 (x8) */
2051 "16.0 * ", /* 4 (x16) */
2052 "32.0 * ", /* 5 (x32) */
2053 "", /* 6 (x64) */
2054 "", /* 7 (x128) */
2055 "", /* 8 (d256) */
2056 "", /* 9 (d128) */
2057 "", /* 10 (d64) */
2058 "", /* 11 (d32) */
2059 "0.0625 * ", /* 12 (d16) */
2060 "0.125 * ", /* 13 (d8) */
2061 "0.25 * ", /* 14 (d4) */
2062 "0.5 * " /* 15 (d2) */
2065 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2066 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2067 const char *in_reg, const char *in_regswizzle, char *out_str)
2069 switch (src_modifier)
2071 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2072 case WINED3DSPSM_DW:
2073 case WINED3DSPSM_NONE:
2074 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2075 break;
2076 case WINED3DSPSM_NEG:
2077 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2078 break;
2079 case WINED3DSPSM_NOT:
2080 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2081 break;
2082 case WINED3DSPSM_BIAS:
2083 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2084 break;
2085 case WINED3DSPSM_BIASNEG:
2086 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2087 break;
2088 case WINED3DSPSM_SIGN:
2089 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2090 break;
2091 case WINED3DSPSM_SIGNNEG:
2092 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2093 break;
2094 case WINED3DSPSM_COMP:
2095 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2096 break;
2097 case WINED3DSPSM_X2:
2098 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2099 break;
2100 case WINED3DSPSM_X2NEG:
2101 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2102 break;
2103 case WINED3DSPSM_ABS:
2104 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2105 break;
2106 case WINED3DSPSM_ABSNEG:
2107 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2108 break;
2109 default:
2110 FIXME("Unhandled modifier %u\n", src_modifier);
2111 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2115 /** Writes the GLSL variable name that corresponds to the register that the
2116 * DX opcode parameter is trying to access */
2117 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2118 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2120 /* oPos, oFog and oPts in D3D */
2121 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2123 const struct wined3d_shader *shader = ins->ctx->shader;
2124 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2125 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2126 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2127 const char *prefix = shader_glsl_get_prefix(version->type);
2128 struct glsl_src_param rel_param0, rel_param1;
2129 char imm_str[4][17];
2131 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2132 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2133 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2134 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2135 *is_color = FALSE;
2137 switch (reg->type)
2139 case WINED3DSPR_TEMP:
2140 sprintf(register_name, "R%u", reg->idx[0].offset);
2141 break;
2143 case WINED3DSPR_INPUT:
2144 /* vertex shaders */
2145 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2147 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2148 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2149 *is_color = TRUE;
2150 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2151 break;
2154 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2156 if (reg->idx[0].rel_addr)
2158 if (reg->idx[1].rel_addr)
2159 sprintf(register_name, "gs_in[%s + %u][%s + %u]",
2160 rel_param0.param_str, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2161 else
2162 sprintf(register_name, "gs_in[%s + %u][%u]",
2163 rel_param0.param_str, reg->idx[0].offset, reg->idx[1].offset);
2165 else if (reg->idx[1].rel_addr)
2166 sprintf(register_name, "gs_in[%u][%s + %u]",
2167 reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2168 else
2169 sprintf(register_name, "gs_in[%u][%u]", reg->idx[0].offset, reg->idx[1].offset);
2170 break;
2173 /* pixel shaders >= 3.0 */
2174 if (version->major >= 3)
2176 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2177 unsigned int in_count = vec4_varyings(version->major, gl_info);
2179 if (reg->idx[0].rel_addr)
2181 /* Removing a + 0 would be an obvious optimization, but
2182 * OS X doesn't see the NOP operation there. */
2183 if (idx)
2185 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2186 && shader->u.ps.declared_in_count > in_count)
2188 sprintf(register_name,
2189 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2190 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2191 prefix, rel_param0.param_str, idx);
2193 else
2195 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2198 else
2200 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2201 && shader->u.ps.declared_in_count > in_count)
2203 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2204 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2205 prefix, rel_param0.param_str);
2207 else
2209 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2213 else
2215 if (idx == in_count) sprintf(register_name, "gl_Color");
2216 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2217 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2220 else
2222 if (!reg->idx[0].offset)
2223 strcpy(register_name, "ffp_varying_diffuse");
2224 else
2225 strcpy(register_name, "ffp_varying_specular");
2226 break;
2228 break;
2230 case WINED3DSPR_CONST:
2232 /* Relative addressing */
2233 if (reg->idx[0].rel_addr)
2235 if (reg->idx[0].offset)
2236 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2237 else
2238 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2240 else
2242 if (shader_constant_is_local(shader, reg->idx[0].offset))
2243 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2244 else
2245 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2248 break;
2250 case WINED3DSPR_CONSTINT:
2251 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2252 break;
2254 case WINED3DSPR_CONSTBOOL:
2255 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2256 break;
2258 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2259 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2260 sprintf(register_name, "T%u", reg->idx[0].offset);
2261 else
2262 sprintf(register_name, "A%u", reg->idx[0].offset);
2263 break;
2265 case WINED3DSPR_LOOP:
2266 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
2267 break;
2269 case WINED3DSPR_SAMPLER:
2270 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2271 break;
2273 case WINED3DSPR_COLOROUT:
2274 if (reg->idx[0].offset >= gl_info->limits.buffers)
2275 WARN("Write to render target %u, only %d supported.\n",
2276 reg->idx[0].offset, gl_info->limits.buffers);
2278 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
2279 break;
2281 case WINED3DSPR_RASTOUT:
2282 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2283 break;
2285 case WINED3DSPR_DEPTHOUT:
2286 sprintf(register_name, "gl_FragDepth");
2287 break;
2289 case WINED3DSPR_ATTROUT:
2290 if (!reg->idx[0].offset)
2291 sprintf(register_name, "%s_out[8]", prefix);
2292 else
2293 sprintf(register_name, "%s_out[9]", prefix);
2294 break;
2296 case WINED3DSPR_TEXCRDOUT:
2297 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2298 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2299 break;
2301 case WINED3DSPR_MISCTYPE:
2302 if (!reg->idx[0].offset)
2304 /* vPos */
2305 sprintf(register_name, "vpos");
2307 else if (reg->idx[0].offset == 1)
2309 /* Note that gl_FrontFacing is a bool, while vFace is
2310 * a float for which the sign determines front/back */
2311 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2313 else
2315 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2316 sprintf(register_name, "unrecognized_register");
2318 break;
2320 case WINED3DSPR_IMMCONST:
2321 switch (reg->immconst_type)
2323 case WINED3D_IMMCONST_SCALAR:
2324 switch (reg->data_type)
2326 case WINED3D_DATA_FLOAT:
2327 wined3d_ftoa(*(const float *)reg->immconst_data, register_name);
2328 break;
2329 case WINED3D_DATA_INT:
2330 sprintf(register_name, "%#x", reg->immconst_data[0]);
2331 break;
2332 case WINED3D_DATA_RESOURCE:
2333 case WINED3D_DATA_SAMPLER:
2334 case WINED3D_DATA_UINT:
2335 sprintf(register_name, "%#xu", reg->immconst_data[0]);
2336 break;
2337 default:
2338 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2339 break;
2341 break;
2343 case WINED3D_IMMCONST_VEC4:
2344 switch (reg->data_type)
2346 case WINED3D_DATA_FLOAT:
2347 wined3d_ftoa(*(const float *)&reg->immconst_data[0], imm_str[0]);
2348 wined3d_ftoa(*(const float *)&reg->immconst_data[1], imm_str[1]);
2349 wined3d_ftoa(*(const float *)&reg->immconst_data[2], imm_str[2]);
2350 wined3d_ftoa(*(const float *)&reg->immconst_data[3], imm_str[3]);
2351 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2352 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2353 break;
2354 case WINED3D_DATA_INT:
2355 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2356 reg->immconst_data[0], reg->immconst_data[1],
2357 reg->immconst_data[2], reg->immconst_data[3]);
2358 break;
2359 case WINED3D_DATA_RESOURCE:
2360 case WINED3D_DATA_SAMPLER:
2361 case WINED3D_DATA_UINT:
2362 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2363 reg->immconst_data[0], reg->immconst_data[1],
2364 reg->immconst_data[2], reg->immconst_data[3]);
2365 break;
2366 default:
2367 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2368 break;
2370 break;
2372 default:
2373 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2374 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2376 break;
2378 case WINED3DSPR_CONSTBUFFER:
2379 if (reg->idx[1].rel_addr)
2380 sprintf(register_name, "%s_cb%u[%s + %u]",
2381 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2382 else
2383 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2384 break;
2386 case WINED3DSPR_PRIMID:
2387 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2388 break;
2390 default:
2391 FIXME("Unhandled register type %#x.\n", reg->type);
2392 sprintf(register_name, "unrecognized_register");
2393 break;
2397 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2399 *str++ = '.';
2400 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2401 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2402 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2403 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2404 *str = '\0';
2407 /* Get the GLSL write mask for the destination register */
2408 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2410 DWORD mask = param->write_mask;
2412 if (shader_is_scalar(&param->reg))
2414 mask = WINED3DSP_WRITEMASK_0;
2415 *write_mask = '\0';
2417 else
2419 shader_glsl_write_mask_to_str(mask, write_mask);
2422 return mask;
2425 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2426 unsigned int size = 0;
2428 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2429 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2430 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2431 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2433 return size;
2436 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2438 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2439 * but addressed as "rgba". To fix this we need to swap the register's x
2440 * and z components. */
2441 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2443 *str++ = '.';
2444 /* swizzle bits fields: wwzzyyxx */
2445 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2446 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2447 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2448 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2449 *str = '\0';
2452 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2453 BOOL fixup, DWORD mask, char *swizzle_str)
2455 if (shader_is_scalar(&param->reg))
2456 *swizzle_str = '\0';
2457 else
2458 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2461 /* From a given parameter token, generate the corresponding GLSL string.
2462 * Also, return the actual register name and swizzle in case the
2463 * caller needs this information as well. */
2464 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2465 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2467 BOOL is_color = FALSE;
2468 char swizzle_str[6];
2470 glsl_src->reg_name[0] = '\0';
2471 glsl_src->param_str[0] = '\0';
2472 swizzle_str[0] = '\0';
2474 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2475 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2477 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2479 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2481 else
2483 char reg_name[200];
2485 switch (wined3d_src->reg.data_type)
2487 case WINED3D_DATA_FLOAT:
2488 sprintf(reg_name, "%s", glsl_src->reg_name);
2489 break;
2490 case WINED3D_DATA_INT:
2491 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2492 break;
2493 case WINED3D_DATA_RESOURCE:
2494 case WINED3D_DATA_SAMPLER:
2495 case WINED3D_DATA_UINT:
2496 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2497 break;
2498 default:
2499 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
2500 sprintf(reg_name, "%s", glsl_src->reg_name);
2501 break;
2504 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2508 /* From a given parameter token, generate the corresponding GLSL string.
2509 * Also, return the actual register name and swizzle in case the
2510 * caller needs this information as well. */
2511 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2512 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2514 BOOL is_color = FALSE;
2516 glsl_dst->mask_str[0] = '\0';
2517 glsl_dst->reg_name[0] = '\0';
2519 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2520 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2523 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2524 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2525 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2526 enum wined3d_data_type data_type)
2528 struct glsl_dst_param glsl_dst;
2529 DWORD mask;
2531 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2533 switch (data_type)
2535 case WINED3D_DATA_FLOAT:
2536 shader_addline(buffer, "%s%s = %s(",
2537 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2538 break;
2539 case WINED3D_DATA_INT:
2540 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
2541 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2542 break;
2543 case WINED3D_DATA_RESOURCE:
2544 case WINED3D_DATA_SAMPLER:
2545 case WINED3D_DATA_UINT:
2546 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
2547 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2548 break;
2549 default:
2550 FIXME("Unhandled data type %#x.\n", data_type);
2551 shader_addline(buffer, "%s%s = %s(",
2552 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2553 break;
2557 return mask;
2560 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2561 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
2563 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
2566 /** Process GLSL instruction modifiers */
2567 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
2569 struct glsl_dst_param dst_param;
2570 DWORD modifiers;
2572 if (!ins->dst_count) return;
2574 modifiers = ins->dst[0].modifiers;
2575 if (!modifiers) return;
2577 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
2579 if (modifiers & WINED3DSPDM_SATURATE)
2581 /* _SAT means to clamp the value of the register to between 0 and 1 */
2582 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
2583 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
2586 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
2588 FIXME("_centroid modifier not handled\n");
2591 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
2593 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
2597 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
2599 switch (op)
2601 case WINED3D_SHADER_REL_OP_GT: return ">";
2602 case WINED3D_SHADER_REL_OP_EQ: return "==";
2603 case WINED3D_SHADER_REL_OP_GE: return ">=";
2604 case WINED3D_SHADER_REL_OP_LT: return "<";
2605 case WINED3D_SHADER_REL_OP_NE: return "!=";
2606 case WINED3D_SHADER_REL_OP_LE: return "<=";
2607 default:
2608 FIXME("Unrecognized operator %#x.\n", op);
2609 return "(\?\?)";
2613 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
2614 DWORD resource_idx, DWORD flags, struct glsl_sample_function *sample_function)
2616 static const struct
2618 unsigned int coord_size;
2619 const char *type_part;
2621 resource_types[] =
2623 {0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
2624 {1, ""}, /* WINED3D_SHADER_RESOURCE_BUFFER */
2625 {1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
2626 {2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
2627 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
2628 {3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
2629 {3, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
2630 {2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
2631 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
2632 {3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
2634 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2635 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
2636 const struct wined3d_gl_info *gl_info = ctx->gl_info;
2637 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
2638 && (priv->cur_ps_args->shadow & (1u << resource_idx));
2639 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
2640 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_NPOT && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2641 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
2642 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
2643 const char *base = "texture", *type_part = "", *suffix = "";
2644 unsigned int coord_size;
2646 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
2648 if (resource_type >= ARRAY_SIZE(resource_types))
2650 ERR("Unexpected resource type %#x.\n", resource_type);
2651 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
2654 /* Note that there's no such thing as a projected cube texture. */
2655 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
2656 projected = FALSE;
2658 if (needs_legacy_glsl_syntax(gl_info))
2660 if (shadow)
2661 base = "shadow";
2663 type_part = resource_types[resource_type].type_part;
2664 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
2665 type_part = "2DRect";
2666 if (!type_part[0])
2667 FIXME("Unhandled resource type %#x.\n", resource_type);
2669 if (!lod && grad && !gl_info->supported[EXT_GPU_SHADER4])
2671 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2672 suffix = "ARB";
2673 else
2674 FIXME("Unsupported grad function.\n");
2678 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
2680 if (flags != WINED3D_GLSL_SAMPLE_LOAD)
2681 ERR("Unexpected flags for texelFetch %#x.\n", flags & ~WINED3D_GLSL_SAMPLE_LOAD);
2683 base = "texelFetch";
2684 type_part = "";
2687 sample_function->name = string_buffer_get(priv->string_buffers);
2688 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
2689 lod ? "Lod" : grad ? "Grad" : "", suffix);
2691 coord_size = resource_types[resource_type].coord_size;
2692 if (shadow)
2693 ++coord_size;
2694 sample_function->coord_mask = (1u << coord_size) - 1;
2695 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
2698 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
2699 struct glsl_sample_function *sample_function)
2701 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
2703 string_buffer_release(priv->string_buffers, sample_function->name);
2706 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2707 BOOL sign_fixup, enum fixup_channel_source channel_source)
2709 switch(channel_source)
2711 case CHANNEL_SOURCE_ZERO:
2712 strcat(arguments, "0.0");
2713 break;
2715 case CHANNEL_SOURCE_ONE:
2716 strcat(arguments, "1.0");
2717 break;
2719 case CHANNEL_SOURCE_X:
2720 strcat(arguments, reg_name);
2721 strcat(arguments, ".x");
2722 break;
2724 case CHANNEL_SOURCE_Y:
2725 strcat(arguments, reg_name);
2726 strcat(arguments, ".y");
2727 break;
2729 case CHANNEL_SOURCE_Z:
2730 strcat(arguments, reg_name);
2731 strcat(arguments, ".z");
2732 break;
2734 case CHANNEL_SOURCE_W:
2735 strcat(arguments, reg_name);
2736 strcat(arguments, ".w");
2737 break;
2739 default:
2740 FIXME("Unhandled channel source %#x\n", channel_source);
2741 strcat(arguments, "undefined");
2742 break;
2745 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2748 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
2749 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
2751 unsigned int mask_size, remaining;
2752 DWORD fixup_mask = 0;
2753 char arguments[256];
2754 char mask_str[6];
2756 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
2757 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
2758 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
2759 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
2760 if (!(mask &= fixup_mask))
2761 return;
2763 if (is_complex_fixup(fixup))
2765 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2766 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2767 return;
2770 shader_glsl_write_mask_to_str(mask, mask_str);
2771 mask_size = shader_glsl_get_write_mask_size(mask);
2773 arguments[0] = '\0';
2774 remaining = mask_size;
2775 if (mask & WINED3DSP_WRITEMASK_0)
2777 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
2778 if (--remaining) strcat(arguments, ", ");
2780 if (mask & WINED3DSP_WRITEMASK_1)
2782 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
2783 if (--remaining) strcat(arguments, ", ");
2785 if (mask & WINED3DSP_WRITEMASK_2)
2787 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
2788 if (--remaining) strcat(arguments, ", ");
2790 if (mask & WINED3DSP_WRITEMASK_3)
2792 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
2793 if (--remaining) strcat(arguments, ", ");
2796 if (mask_size > 1)
2797 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
2798 else
2799 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
2802 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2804 char reg_name[256];
2805 BOOL is_color;
2807 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
2808 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
2811 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2812 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2813 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2815 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2816 char dst_swizzle[6];
2817 struct color_fixup_desc fixup;
2818 BOOL np2_fixup = FALSE;
2819 va_list args;
2820 int ret;
2822 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2824 /* FIXME: We currently don't support fixups for vertex shaders or anything
2825 * above SM3. Note that for SM4+ the sampler index doesn't have to match
2826 * the resource index. */
2827 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
2829 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2830 fixup = priv->cur_ps_args->color_fixup[sampler];
2832 if (priv->cur_ps_args->np2_fixup & (1u << sampler))
2833 np2_fixup = TRUE;
2835 else
2837 fixup = COLOR_FIXUP_IDENTITY;
2840 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
2842 if (sample_function->output_single_component)
2843 shader_addline(ins->ctx->buffer, "vec4(");
2845 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2846 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler);
2848 for (;;)
2850 va_start(args, coord_reg_fmt);
2851 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2852 va_end(args);
2853 if (!ret)
2854 break;
2855 if (!string_buffer_resize(ins->ctx->buffer, ret))
2856 break;
2859 if (np2_fixup)
2861 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2862 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2864 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
2866 case 1:
2867 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2868 idx >> 1, (idx % 2) ? "z" : "x");
2869 break;
2870 case 2:
2871 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
2872 idx >> 1, (idx % 2) ? "zw" : "xy");
2873 break;
2874 case 3:
2875 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
2876 idx >> 1, (idx % 2) ? "zw" : "xy");
2877 break;
2878 case 4:
2879 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
2880 idx >> 1, (idx % 2) ? "zw" : "xy");
2881 break;
2884 if (dx && dy)
2885 shader_addline(ins->ctx->buffer, ", %s, %s)", dx, dy);
2886 else if (bias)
2887 shader_addline(ins->ctx->buffer, ", %s)", bias);
2888 else
2889 shader_addline(ins->ctx->buffer, ")");
2891 if (sample_function->output_single_component)
2892 shader_addline(ins->ctx->buffer, ")");
2894 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
2896 if (!is_identity_fixup(fixup))
2897 shader_glsl_color_correction(ins, fixup);
2900 /*****************************************************************************
2901 * Begin processing individual instruction opcodes
2902 ****************************************************************************/
2904 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2906 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2907 struct glsl_src_param src0_param;
2908 struct glsl_src_param src1_param;
2909 DWORD write_mask;
2910 const char *op;
2912 /* Determine the GLSL operator to use based on the opcode */
2913 switch (ins->handler_idx)
2915 case WINED3DSIH_ADD: op = "+"; break;
2916 case WINED3DSIH_AND: op = "&"; break;
2917 case WINED3DSIH_DIV: op = "/"; break;
2918 case WINED3DSIH_IADD: op = "+"; break;
2919 case WINED3DSIH_ISHL: op = "<<"; break;
2920 case WINED3DSIH_MUL: op = "*"; break;
2921 case WINED3DSIH_OR: op = "|"; break;
2922 case WINED3DSIH_SUB: op = "-"; break;
2923 case WINED3DSIH_USHR: op = ">>"; break;
2924 case WINED3DSIH_XOR: op = "^"; break;
2925 default:
2926 op = "<unhandled operator>";
2927 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
2928 break;
2931 write_mask = shader_glsl_append_dst(buffer, ins);
2932 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2933 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2934 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2937 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2939 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2940 struct glsl_src_param src0_param;
2941 struct glsl_src_param src1_param;
2942 unsigned int mask_size;
2943 DWORD write_mask;
2944 const char *op;
2946 write_mask = shader_glsl_append_dst(buffer, ins);
2947 mask_size = shader_glsl_get_write_mask_size(write_mask);
2948 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2949 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2951 if (mask_size > 1)
2953 switch (ins->handler_idx)
2955 case WINED3DSIH_EQ: op = "equal"; break;
2956 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2957 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2958 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
2959 case WINED3DSIH_LT: op = "lessThan"; break;
2960 case WINED3DSIH_NE: op = "notEqual"; break;
2961 default:
2962 op = "<unhandled operator>";
2963 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2964 break;
2967 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2968 mask_size, op, src0_param.param_str, src1_param.param_str);
2970 else
2972 switch (ins->handler_idx)
2974 case WINED3DSIH_EQ: op = "=="; break;
2975 case WINED3DSIH_GE: op = ">="; break;
2976 case WINED3DSIH_IGE: op = ">="; break;
2977 case WINED3DSIH_UGE: op = ">="; break;
2978 case WINED3DSIH_LT: op = "<"; break;
2979 case WINED3DSIH_NE: op = "!="; break;
2980 default:
2981 op = "<unhandled operator>";
2982 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2983 break;
2986 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2987 src0_param.param_str, op, src1_param.param_str);
2991 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2993 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
2994 struct glsl_src_param src0_param;
2995 struct glsl_src_param src1_param;
2996 DWORD write_mask;
2998 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2999 * not, we can emulate it. */
3000 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3001 FIXME("64-bit integer multiplies not implemented.\n");
3003 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3005 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3006 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3007 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3009 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3010 src0_param.param_str, src1_param.param_str);
3014 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3016 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3017 struct glsl_src_param src0_param, src1_param;
3018 DWORD write_mask;
3020 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3023 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3025 char dst_mask[6];
3027 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3028 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3029 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3030 shader_addline(buffer, "tmp0%s = %s / %s;\n",
3031 dst_mask, src0_param.param_str, src1_param.param_str);
3033 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3034 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3035 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3036 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
3038 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3039 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3041 else
3043 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3044 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3045 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3046 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3049 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3051 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3052 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3053 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3054 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3058 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3059 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3061 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3062 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3063 struct glsl_src_param src0_param;
3064 DWORD write_mask;
3066 write_mask = shader_glsl_append_dst(buffer, ins);
3067 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3069 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3070 * shader versions WINED3DSIO_MOVA is used for this. */
3071 if (ins->ctx->reg_maps->shader_version.major == 1
3072 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3073 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3075 /* This is a simple floor() */
3076 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3077 if (mask_size > 1) {
3078 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3079 } else {
3080 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3083 else if(ins->handler_idx == WINED3DSIH_MOVA)
3085 /* We need to *round* to the nearest int here. */
3086 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3088 if (gl_info->supported[EXT_GPU_SHADER4])
3090 if (mask_size > 1)
3091 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3092 else
3093 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3095 else
3097 if (mask_size > 1)
3098 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3099 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3100 else
3101 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3102 src0_param.param_str, src0_param.param_str);
3105 else
3107 shader_addline(buffer, "%s);\n", src0_param.param_str);
3111 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3112 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3114 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3115 struct glsl_src_param src0_param;
3116 struct glsl_src_param src1_param;
3117 DWORD dst_write_mask, src_write_mask;
3118 unsigned int dst_size;
3120 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3121 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3123 /* dp4 works on vec4, dp3 on vec3, etc. */
3124 if (ins->handler_idx == WINED3DSIH_DP4)
3125 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3126 else if (ins->handler_idx == WINED3DSIH_DP3)
3127 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3128 else
3129 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3131 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3132 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3134 if (dst_size > 1) {
3135 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3136 } else {
3137 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3141 /* Note that this instruction has some restrictions. The destination write mask
3142 * can't contain the w component, and the source swizzles have to be .xyzw */
3143 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3145 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3146 struct glsl_src_param src0_param;
3147 struct glsl_src_param src1_param;
3148 char dst_mask[6];
3150 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3151 shader_glsl_append_dst(ins->ctx->buffer, ins);
3152 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3153 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3154 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3157 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3159 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3162 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3163 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3164 * GLSL uses the value as-is. */
3165 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3167 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3168 struct glsl_src_param src0_param;
3169 struct glsl_src_param src1_param;
3170 DWORD dst_write_mask;
3171 unsigned int dst_size;
3173 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3174 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3176 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3177 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3179 if (dst_size > 1)
3181 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3182 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3184 else
3186 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3187 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3191 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3192 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3194 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3195 struct glsl_src_param src_param;
3196 const char *instruction;
3197 DWORD write_mask;
3198 unsigned i;
3200 /* Determine the GLSL function to use based on the opcode */
3201 /* TODO: Possibly make this a table for faster lookups */
3202 switch (ins->handler_idx)
3204 case WINED3DSIH_ABS: instruction = "abs"; break;
3205 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3206 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3207 case WINED3DSIH_FRC: instruction = "fract"; break;
3208 case WINED3DSIH_IMAX: instruction = "max"; break;
3209 case WINED3DSIH_IMIN: instruction = "min"; break;
3210 case WINED3DSIH_MAX: instruction = "max"; break;
3211 case WINED3DSIH_MIN: instruction = "min"; break;
3212 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3213 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3214 default: instruction = "";
3215 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3216 break;
3219 write_mask = shader_glsl_append_dst(buffer, ins);
3221 shader_addline(buffer, "%s(", instruction);
3223 if (ins->src_count)
3225 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3226 shader_addline(buffer, "%s", src_param.param_str);
3227 for (i = 1; i < ins->src_count; ++i)
3229 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3230 shader_addline(buffer, ", %s", src_param.param_str);
3234 shader_addline(buffer, "));\n");
3237 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3239 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3241 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3242 struct glsl_src_param src_param;
3243 unsigned int mask_size;
3244 DWORD write_mask;
3245 char dst_mask[6];
3247 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3248 mask_size = shader_glsl_get_write_mask_size(write_mask);
3249 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3251 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3252 src_param.param_str, src_param.param_str);
3253 shader_glsl_append_dst(buffer, ins);
3255 if (mask_size > 1)
3257 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3258 mask_size, src_param.param_str);
3260 else
3262 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3263 src_param.param_str);
3267 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3269 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3270 struct glsl_src_param src0_param;
3271 const char *prefix, *suffix;
3272 unsigned int dst_size;
3273 DWORD dst_write_mask;
3275 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3276 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3278 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src0_param);
3280 switch (ins->handler_idx)
3282 case WINED3DSIH_EXP:
3283 case WINED3DSIH_EXPP:
3284 prefix = "exp2(";
3285 suffix = ")";
3286 break;
3288 case WINED3DSIH_LOG:
3289 case WINED3DSIH_LOGP:
3290 prefix = "log2(abs(";
3291 suffix = "))";
3292 break;
3294 case WINED3DSIH_RCP:
3295 prefix = "1.0 / ";
3296 suffix = "";
3297 break;
3299 case WINED3DSIH_RSQ:
3300 prefix = "inversesqrt(abs(";
3301 suffix = "))";
3302 break;
3304 default:
3305 prefix = "";
3306 suffix = "";
3307 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3308 break;
3311 if (dst_size > 1)
3312 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3313 else
3314 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3317 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3318 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3319 * dst.x = 2^(floor(src))
3320 * dst.y = src - floor(src)
3321 * dst.z = 2^src (partial precision is allowed, but optional)
3322 * dst.w = 1.0;
3323 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3324 * dst = 2^src; (partial precision is allowed, but optional)
3326 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3328 if (ins->ctx->reg_maps->shader_version.major < 2)
3330 struct glsl_src_param src_param;
3331 char dst_mask[6];
3333 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3335 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3336 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3337 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3338 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3340 shader_glsl_append_dst(ins->ctx->buffer, ins);
3341 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3342 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3343 return;
3346 shader_glsl_scalar_op(ins);
3349 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3350 const char *vector_constructor, const char *scalar_constructor)
3352 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3353 struct glsl_src_param src_param;
3354 unsigned int mask_size;
3355 DWORD write_mask;
3357 write_mask = shader_glsl_append_dst(buffer, ins);
3358 mask_size = shader_glsl_get_write_mask_size(write_mask);
3359 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3361 if (mask_size > 1)
3362 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3363 else
3364 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3367 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3369 shader_glsl_cast(ins, "ivec", "int");
3372 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3374 shader_glsl_cast(ins, "uvec", "uint");
3377 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3379 shader_glsl_cast(ins, "vec", "float");
3382 /** Process signed comparison opcodes in GLSL. */
3383 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3385 struct glsl_src_param src0_param;
3386 struct glsl_src_param src1_param;
3387 DWORD write_mask;
3388 unsigned int mask_size;
3390 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3391 mask_size = shader_glsl_get_write_mask_size(write_mask);
3392 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3393 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3395 if (mask_size > 1) {
3396 const char *compare;
3398 switch(ins->handler_idx)
3400 case WINED3DSIH_SLT: compare = "lessThan"; break;
3401 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3402 default: compare = "";
3403 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3406 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3407 src0_param.param_str, src1_param.param_str);
3408 } else {
3409 switch(ins->handler_idx)
3411 case WINED3DSIH_SLT:
3412 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3413 * to return 0.0 but step returns 1.0 because step is not < x
3414 * An alternative is a bvec compare padded with an unused second component.
3415 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3416 * issue. Playing with not() is not possible either because not() does not accept
3417 * a scalar.
3419 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3420 src0_param.param_str, src1_param.param_str);
3421 break;
3422 case WINED3DSIH_SGE:
3423 /* Here we can use the step() function and safe a conditional */
3424 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3425 break;
3426 default:
3427 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3433 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3435 const char *condition_prefix, *condition_suffix;
3436 struct wined3d_shader_dst_param dst;
3437 struct glsl_src_param src0_param;
3438 struct glsl_src_param src1_param;
3439 struct glsl_src_param src2_param;
3440 BOOL temp_destination = FALSE;
3441 DWORD cmp_channel = 0;
3442 unsigned int i, j;
3443 char mask_char[6];
3444 DWORD write_mask;
3446 switch (ins->handler_idx)
3448 case WINED3DSIH_CMP:
3449 condition_prefix = "";
3450 condition_suffix = " >= 0.0";
3451 break;
3453 case WINED3DSIH_CND:
3454 condition_prefix = "";
3455 condition_suffix = " > 0.5";
3456 break;
3458 case WINED3DSIH_MOVC:
3459 condition_prefix = "bool(";
3460 condition_suffix = ")";
3461 break;
3463 default:
3464 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3465 condition_prefix = "<unhandled prefix>";
3466 condition_suffix = "<unhandled suffix>";
3467 break;
3470 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
3472 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3473 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3474 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3475 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3477 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3478 condition_prefix, src0_param.param_str, condition_suffix,
3479 src1_param.param_str, src2_param.param_str);
3480 return;
3483 dst = ins->dst[0];
3485 /* Splitting the instruction up in multiple lines imposes a problem:
3486 * The first lines may overwrite source parameters of the following lines.
3487 * Deal with that by using a temporary destination register if needed. */
3488 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
3489 && ins->src[0].reg.type == dst.reg.type)
3490 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
3491 && ins->src[1].reg.type == dst.reg.type)
3492 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
3493 && ins->src[2].reg.type == dst.reg.type))
3494 temp_destination = TRUE;
3496 /* Cycle through all source0 channels. */
3497 for (i = 0; i < 4; ++i)
3499 write_mask = 0;
3500 /* Find the destination channels which use the current source0 channel. */
3501 for (j = 0; j < 4; ++j)
3503 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
3505 write_mask |= WINED3DSP_WRITEMASK_0 << j;
3506 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
3509 dst.write_mask = ins->dst[0].write_mask & write_mask;
3511 if (temp_destination)
3513 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
3514 continue;
3515 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
3517 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
3518 continue;
3520 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
3521 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3522 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3524 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
3525 condition_prefix, src0_param.param_str, condition_suffix,
3526 src1_param.param_str, src2_param.param_str);
3529 if (temp_destination)
3531 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
3532 shader_glsl_append_dst(ins->ctx->buffer, ins);
3533 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
3537 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
3538 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
3539 * the compare is done per component of src0. */
3540 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
3542 struct glsl_src_param src0_param;
3543 struct glsl_src_param src1_param;
3544 struct glsl_src_param src2_param;
3545 DWORD write_mask;
3546 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3547 ins->ctx->reg_maps->shader_version.minor);
3549 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
3551 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3552 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3553 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3554 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3556 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
3557 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
3558 else
3559 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
3560 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3561 return;
3564 shader_glsl_conditional_move(ins);
3567 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
3568 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
3570 struct glsl_src_param src0_param;
3571 struct glsl_src_param src1_param;
3572 struct glsl_src_param src2_param;
3573 DWORD write_mask;
3575 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3576 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3577 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3578 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3579 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
3580 src0_param.param_str, src1_param.param_str, src2_param.param_str);
3583 /* Handles transforming all WINED3DSIO_M?x? opcodes for
3584 Vertex shaders to GLSL codes */
3585 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
3587 int i;
3588 int nComponents = 0;
3589 struct wined3d_shader_dst_param tmp_dst = {{0}};
3590 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
3591 struct wined3d_shader_instruction tmp_ins;
3593 memset(&tmp_ins, 0, sizeof(tmp_ins));
3595 /* Set constants for the temporary argument */
3596 tmp_ins.ctx = ins->ctx;
3597 tmp_ins.dst_count = 1;
3598 tmp_ins.dst = &tmp_dst;
3599 tmp_ins.src_count = 2;
3600 tmp_ins.src = tmp_src;
3602 switch(ins->handler_idx)
3604 case WINED3DSIH_M4x4:
3605 nComponents = 4;
3606 tmp_ins.handler_idx = WINED3DSIH_DP4;
3607 break;
3608 case WINED3DSIH_M4x3:
3609 nComponents = 3;
3610 tmp_ins.handler_idx = WINED3DSIH_DP4;
3611 break;
3612 case WINED3DSIH_M3x4:
3613 nComponents = 4;
3614 tmp_ins.handler_idx = WINED3DSIH_DP3;
3615 break;
3616 case WINED3DSIH_M3x3:
3617 nComponents = 3;
3618 tmp_ins.handler_idx = WINED3DSIH_DP3;
3619 break;
3620 case WINED3DSIH_M3x2:
3621 nComponents = 2;
3622 tmp_ins.handler_idx = WINED3DSIH_DP3;
3623 break;
3624 default:
3625 break;
3628 tmp_dst = ins->dst[0];
3629 tmp_src[0] = ins->src[0];
3630 tmp_src[1] = ins->src[1];
3631 for (i = 0; i < nComponents; ++i)
3633 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
3634 shader_glsl_dot(&tmp_ins);
3635 ++tmp_src[1].reg.idx[0].offset;
3640 The LRP instruction performs a component-wise linear interpolation
3641 between the second and third operands using the first operand as the
3642 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
3643 This is equivalent to mix(src2, src1, src0);
3645 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
3647 struct glsl_src_param src0_param;
3648 struct glsl_src_param src1_param;
3649 struct glsl_src_param src2_param;
3650 DWORD write_mask;
3652 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3654 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3655 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3656 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
3658 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
3659 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3662 /** Process the WINED3DSIO_LIT instruction in GLSL:
3663 * dst.x = dst.w = 1.0
3664 * dst.y = (src0.x > 0) ? src0.x
3665 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3666 * where src.w is clamped at +- 128
3668 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3670 struct glsl_src_param src0_param;
3671 struct glsl_src_param src1_param;
3672 struct glsl_src_param src3_param;
3673 char dst_mask[6];
3675 shader_glsl_append_dst(ins->ctx->buffer, ins);
3676 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3678 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3679 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3680 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3682 /* The sdk specifies the instruction like this
3683 * dst.x = 1.0;
3684 * if(src.x > 0.0) dst.y = src.x
3685 * else dst.y = 0.0.
3686 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3687 * else dst.z = 0.0;
3688 * dst.w = 1.0;
3689 * (where power = src.w clamped between -128 and 128)
3691 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3692 * dst.x = 1.0 ... No further explanation needed
3693 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3694 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3695 * dst.w = 1.0. ... Nothing fancy.
3697 * So we still have one conditional in there. So do this:
3698 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3700 * 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),
3701 * 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.
3702 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3704 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3705 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3706 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3708 shader_addline(ins->ctx->buffer,
3709 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3710 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3711 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3712 src0_param.param_str, src3_param.param_str, dst_mask);
3715 /** Process the WINED3DSIO_DST instruction in GLSL:
3716 * dst.x = 1.0
3717 * dst.y = src0.x * src0.y
3718 * dst.z = src0.z
3719 * dst.w = src1.w
3721 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3723 struct glsl_src_param src0y_param;
3724 struct glsl_src_param src0z_param;
3725 struct glsl_src_param src1y_param;
3726 struct glsl_src_param src1w_param;
3727 char dst_mask[6];
3729 shader_glsl_append_dst(ins->ctx->buffer, ins);
3730 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3732 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3733 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3734 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3735 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3737 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3738 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3741 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3742 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3743 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3745 * dst.x = cos(src0.?)
3746 * dst.y = sin(src0.?)
3747 * dst.z = dst.z
3748 * dst.w = dst.w
3750 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3752 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3753 struct glsl_src_param src0_param;
3754 DWORD write_mask;
3756 if (ins->ctx->reg_maps->shader_version.major < 4)
3758 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3760 write_mask = shader_glsl_append_dst(buffer, ins);
3761 switch (write_mask)
3763 case WINED3DSP_WRITEMASK_0:
3764 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3765 break;
3767 case WINED3DSP_WRITEMASK_1:
3768 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3769 break;
3771 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3772 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3773 src0_param.param_str, src0_param.param_str);
3774 break;
3776 default:
3777 ERR("Write mask should be .x, .y or .xy\n");
3778 break;
3781 return;
3784 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3787 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3789 char dst_mask[6];
3791 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3792 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3793 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3795 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3796 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3797 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3799 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3800 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3802 else
3804 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3805 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3806 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3809 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3811 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3812 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3813 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3817 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3818 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3819 * generate invalid code
3821 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3823 struct glsl_src_param src0_param;
3824 DWORD write_mask;
3826 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3827 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3829 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3832 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3833 * Start a for() loop where src1.y is the initial value of aL,
3834 * increment aL by src1.z for a total of src1.x iterations.
3835 * Need to use a temporary variable for this operation.
3837 /* FIXME: I don't think nested loops will work correctly this way. */
3838 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3840 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3841 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3842 const struct wined3d_shader *shader = ins->ctx->shader;
3843 const struct wined3d_shader_lconst *constant;
3844 struct glsl_src_param src1_param;
3845 const DWORD *control_values = NULL;
3847 if (ins->ctx->reg_maps->shader_version.major < 4)
3849 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3851 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3852 * class hardware doesn't support real varying indexing, but Microsoft
3853 * designed this feature for Shader model 2.x+. If the loop control is
3854 * known at compile time, the GLSL compiler can unroll the loop, and
3855 * replace indirect addressing with direct addressing. */
3856 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3858 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3860 if (constant->idx == ins->src[1].reg.idx[0].offset)
3862 control_values = constant->value;
3863 break;
3868 if (control_values)
3870 struct wined3d_shader_loop_control loop_control;
3871 loop_control.count = control_values[0];
3872 loop_control.start = control_values[1];
3873 loop_control.step = (int)control_values[2];
3875 if (loop_control.step > 0)
3877 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3878 loop_state->current_depth, loop_control.start,
3879 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3880 loop_state->current_depth, loop_control.step);
3882 else if (loop_control.step < 0)
3884 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3885 loop_state->current_depth, loop_control.start,
3886 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3887 loop_state->current_depth, loop_control.step);
3889 else
3891 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3892 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3893 loop_state->current_depth, loop_control.count,
3894 loop_state->current_depth);
3897 else
3899 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3900 loop_state->current_depth, loop_state->current_reg,
3901 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3902 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3905 ++loop_state->current_reg;
3907 else
3909 shader_addline(buffer, "for (;;)\n{\n");
3912 ++loop_state->current_depth;
3915 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3917 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3919 shader_addline(ins->ctx->buffer, "}\n");
3921 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3923 --loop_state->current_depth;
3924 --loop_state->current_reg;
3927 if (ins->handler_idx == WINED3DSIH_ENDREP)
3929 --loop_state->current_depth;
3933 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3935 const struct wined3d_shader *shader = ins->ctx->shader;
3936 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3937 const struct wined3d_shader_lconst *constant;
3938 struct glsl_src_param src0_param;
3939 const DWORD *control_values = NULL;
3941 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3942 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3944 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3946 if (constant->idx == ins->src[0].reg.idx[0].offset)
3948 control_values = constant->value;
3949 break;
3954 if (control_values)
3956 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3957 loop_state->current_depth, loop_state->current_depth,
3958 control_values[0], loop_state->current_depth);
3960 else
3962 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3963 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3964 loop_state->current_depth, loop_state->current_depth,
3965 src0_param.param_str, loop_state->current_depth);
3968 ++loop_state->current_depth;
3971 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3973 struct glsl_src_param src0_param;
3975 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3976 shader_addline(ins->ctx->buffer, "if (bool(%s)) {\n", src0_param.param_str);
3979 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3981 struct glsl_src_param src0_param;
3982 struct glsl_src_param src1_param;
3984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3985 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3987 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3988 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3991 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3993 shader_addline(ins->ctx->buffer, "} else {\n");
3996 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3998 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4001 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4003 shader_addline(ins->ctx->buffer, "break;\n");
4006 /* FIXME: According to MSDN the compare is done per component. */
4007 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4009 struct glsl_src_param src0_param;
4010 struct glsl_src_param src1_param;
4012 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4013 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4015 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4016 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4019 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4021 struct glsl_src_param src_param;
4023 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4024 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
4027 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4029 shader_addline(ins->ctx->buffer, "}\n");
4030 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4033 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4035 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4038 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4040 struct glsl_src_param src1_param;
4042 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4043 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4044 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4047 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4049 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
4050 * function only suppresses the unhandled instruction warning
4054 /*********************************************
4055 * Pixel Shader Specific Code begins here
4056 ********************************************/
4057 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4059 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4060 ins->ctx->reg_maps->shader_version.minor);
4061 struct glsl_sample_function sample_function;
4062 DWORD sample_flags = 0;
4063 DWORD resource_idx;
4064 DWORD mask = 0, swizzle;
4065 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4067 /* 1.0-1.4: Use destination register as sampler source.
4068 * 2.0+: Use provided sampler source. */
4069 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4070 resource_idx = ins->dst[0].reg.idx[0].offset;
4071 else
4072 resource_idx = ins->src[1].reg.idx[0].offset;
4074 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4076 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4077 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4078 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4080 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4081 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4083 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4084 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4086 case WINED3D_TTFF_COUNT1:
4087 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4088 break;
4089 case WINED3D_TTFF_COUNT2:
4090 mask = WINED3DSP_WRITEMASK_1;
4091 break;
4092 case WINED3D_TTFF_COUNT3:
4093 mask = WINED3DSP_WRITEMASK_2;
4094 break;
4095 case WINED3D_TTFF_COUNT4:
4096 case WINED3D_TTFF_DISABLE:
4097 mask = WINED3DSP_WRITEMASK_3;
4098 break;
4102 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4104 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4106 if (src_mod == WINED3DSPSM_DZ) {
4107 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4108 mask = WINED3DSP_WRITEMASK_2;
4109 } else if (src_mod == WINED3DSPSM_DW) {
4110 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4111 mask = WINED3DSP_WRITEMASK_3;
4114 else
4116 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4117 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4119 /* ps 2.0 texldp instruction always divides by the fourth component. */
4120 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4121 mask = WINED3DSP_WRITEMASK_3;
4125 if (priv->cur_ps_args->np2_fixup & (1u << resource_idx))
4126 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4128 shader_glsl_get_sample_function(ins->ctx, resource_idx, sample_flags, &sample_function);
4129 mask |= sample_function.coord_mask;
4130 sample_function.coord_mask = mask;
4132 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4133 else swizzle = ins->src[1].swizzle;
4135 /* 1.0-1.3: Use destination register as coordinate source.
4136 1.4+: Use provided coordinate source register. */
4137 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4139 char coord_mask[6];
4140 shader_glsl_write_mask_to_str(mask, coord_mask);
4141 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4142 "T%u%s", resource_idx, coord_mask);
4144 else
4146 struct glsl_src_param coord_param;
4147 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4148 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4150 struct glsl_src_param bias;
4151 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4152 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4153 "%s", coord_param.param_str);
4154 } else {
4155 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL,
4156 "%s", coord_param.param_str);
4159 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4162 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4164 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4165 struct glsl_src_param coord_param, dx_param, dy_param;
4166 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
4167 struct glsl_sample_function sample_function;
4168 DWORD sampler_idx;
4169 DWORD swizzle = ins->src[1].swizzle;
4170 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4172 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
4174 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4175 shader_glsl_tex(ins);
4176 return;
4179 sampler_idx = ins->src[1].reg.idx[0].offset;
4180 if (priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4181 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4183 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4184 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4185 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
4186 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
4188 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
4189 "%s", coord_param.param_str);
4190 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4193 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4195 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4196 struct glsl_src_param coord_param, lod_param;
4197 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
4198 struct glsl_sample_function sample_function;
4199 DWORD sampler_idx;
4200 DWORD swizzle = ins->src[1].swizzle;
4201 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4203 sampler_idx = ins->src[1].reg.idx[0].offset;
4204 if (ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
4205 && priv->cur_ps_args->np2_fixup & (1u << sampler_idx))
4206 sample_flags |= WINED3D_GLSL_SAMPLE_NPOT;
4208 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
4209 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4211 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4213 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
4214 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
4216 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4217 * However, the NVIDIA drivers allow them in fragment shaders as well,
4218 * even without the appropriate extension. */
4219 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4221 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
4222 "%s", coord_param.param_str);
4223 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4226 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4227 unsigned int resource_idx, unsigned int sampler_idx)
4229 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4230 unsigned int i;
4232 for (i = 0; i < sampler_map->count; ++i)
4234 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4235 return entries[i].bind_idx;
4238 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4240 return ~0u;
4243 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4245 static const unsigned int texture_size_component_count[] =
4247 0, /* WINED3D_SHADER_RESOURCE_NONE */
4248 1, /* WINED3D_SHADER_RESOURCE_BUFFER */
4249 1, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
4250 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
4251 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
4252 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
4253 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
4254 2, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
4255 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
4256 3, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
4259 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4260 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4261 enum wined3d_shader_resource_type resource_type;
4262 unsigned int resource_idx, sampler_idx, i;
4263 enum wined3d_data_type dst_data_type;
4264 struct glsl_src_param lod_param;
4265 char dst_swizzle[6];
4266 DWORD write_mask;
4268 dst_data_type = ins->dst[0].reg.data_type;
4269 if (ins->flags == WINED3DSI_RESINFO_UINT)
4270 dst_data_type = WINED3D_DATA_UINT;
4271 else if (ins->flags)
4272 FIXME("Unhandled flags %#x.\n", ins->flags);
4274 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4275 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4277 resource_idx = ins->src[1].reg.idx[0].offset;
4278 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4279 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4280 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4281 resource_idx, WINED3D_SAMPLER_DEFAULT);
4283 if (resource_type >= ARRAY_SIZE(texture_size_component_count))
4285 ERR("Unexpected resource type %#x.\n", resource_type);
4286 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4289 if (dst_data_type == WINED3D_DATA_UINT)
4290 shader_addline(ins->ctx->buffer, "uvec4(");
4291 else
4292 shader_addline(ins->ctx->buffer, "vec4(");
4294 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4295 shader_glsl_get_prefix(version->type), sampler_idx, lod_param.param_str);
4297 for (i = 0; i < 3 - texture_size_component_count[resource_type]; ++i)
4298 shader_addline(ins->ctx->buffer, "0, ");
4300 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4302 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4303 shader_glsl_get_prefix(version->type), sampler_idx);
4305 else
4307 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4308 shader_addline(ins->ctx->buffer, "1");
4311 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4314 /* FIXME: The current implementation does not handle multisample textures correctly. */
4315 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4317 struct glsl_src_param coord_param, lod_param;
4318 struct glsl_sample_function sample_function;
4319 unsigned int sampler_idx;
4321 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, WINED3D_GLSL_SAMPLE_LOAD,
4322 &sample_function);
4323 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4324 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4325 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4326 ins->src[1].reg.idx[0].offset, WINED3D_SAMPLER_DEFAULT);
4327 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, ins->src[1].swizzle,
4328 NULL, NULL, lod_param.param_str, "%s", coord_param.param_str);
4329 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4332 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
4334 struct glsl_sample_function sample_function;
4335 struct glsl_src_param coord_param;
4336 unsigned int sampler_idx;
4338 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset, 0, &sample_function);
4339 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4340 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4341 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4342 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, ins->src[1].swizzle,
4343 NULL, NULL, NULL, "%s", coord_param.param_str);
4344 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4347 static void shader_glsl_sample_lod(const struct wined3d_shader_instruction *ins)
4349 struct glsl_src_param coord_param, lod_param;
4350 struct glsl_sample_function sample_function;
4351 unsigned int sampler_idx;
4353 shader_glsl_get_sample_function(ins->ctx, ins->src[1].reg.idx[0].offset,
4354 WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4355 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4356 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
4357 sampler_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4358 ins->src[1].reg.idx[0].offset, ins->src[2].reg.idx[0].offset);
4359 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, ins->src[1].swizzle,
4360 NULL, NULL, lod_param.param_str, "%s", coord_param.param_str);
4361 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4364 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
4366 /* FIXME: Make this work for more than just 2D textures */
4367 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4368 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4370 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
4372 char dst_mask[6];
4374 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4375 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
4376 ins->dst[0].reg.idx[0].offset, dst_mask);
4378 else
4380 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4381 DWORD reg = ins->src[0].reg.idx[0].offset;
4382 char dst_swizzle[6];
4384 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
4386 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
4388 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
4389 struct glsl_src_param div_param;
4390 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
4392 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
4394 if (mask_size > 1)
4395 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
4396 else
4397 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
4399 else
4401 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
4406 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
4407 * Take a 3-component dot product of the TexCoord[dstreg] and src,
4408 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
4409 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
4411 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4412 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4413 struct glsl_sample_function sample_function;
4414 struct glsl_src_param src0_param;
4415 UINT mask_size;
4417 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4419 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
4420 * scalar, and projected sampling would require 4.
4422 * It is a dependent read - not valid with conditional NP2 textures
4424 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4425 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
4427 switch(mask_size)
4429 case 1:
4430 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4431 "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
4432 break;
4434 case 2:
4435 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4436 "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
4437 break;
4439 case 3:
4440 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4441 "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
4442 break;
4444 default:
4445 FIXME("Unexpected mask size %u\n", mask_size);
4446 break;
4448 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4451 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
4452 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
4453 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
4455 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4456 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4457 struct glsl_src_param src0_param;
4458 DWORD dst_mask;
4459 unsigned int mask_size;
4461 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4462 mask_size = shader_glsl_get_write_mask_size(dst_mask);
4463 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4465 if (mask_size > 1) {
4466 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
4467 } else {
4468 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
4472 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
4473 * Calculate the depth as dst.x / dst.y */
4474 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
4476 struct glsl_dst_param dst_param;
4478 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4480 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
4481 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
4482 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
4483 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
4484 * >= 1.0 or < 0.0
4486 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
4487 dst_param.reg_name, dst_param.reg_name);
4490 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
4491 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
4492 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
4493 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
4495 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
4497 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4498 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
4499 struct glsl_src_param src0_param;
4501 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4503 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
4504 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
4507 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
4508 * Calculate the 1st of a 2-row matrix multiplication. */
4509 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
4511 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4512 DWORD reg = ins->dst[0].reg.idx[0].offset;
4513 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4514 struct glsl_src_param src0_param;
4516 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4517 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4520 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
4521 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
4522 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
4524 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4525 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4526 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4527 DWORD reg = ins->dst[0].reg.idx[0].offset;
4528 struct glsl_src_param src0_param;
4530 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4531 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
4532 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
4535 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
4537 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4538 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4539 struct glsl_sample_function sample_function;
4540 DWORD reg = ins->dst[0].reg.idx[0].offset;
4541 struct glsl_src_param src0_param;
4543 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4544 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4546 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4548 /* Sample the texture using the calculated coordinates */
4549 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
4550 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4553 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
4554 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
4555 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
4557 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4558 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4559 struct glsl_sample_function sample_function;
4560 DWORD reg = ins->dst[0].reg.idx[0].offset;
4561 struct glsl_src_param src0_param;
4563 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4564 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4566 /* Dependent read, not valid with conditional NP2 */
4567 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4569 /* Sample the texture using the calculated coordinates */
4570 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
4571 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4573 tex_mx->current_row = 0;
4576 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
4577 * Perform the 3rd row of a 3x3 matrix multiply */
4578 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
4580 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4581 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4582 DWORD reg = ins->dst[0].reg.idx[0].offset;
4583 struct glsl_src_param src0_param;
4584 char dst_mask[6];
4586 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4588 shader_glsl_append_dst(ins->ctx->buffer, ins);
4589 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4590 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
4592 tex_mx->current_row = 0;
4595 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
4596 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4597 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
4599 struct glsl_src_param src0_param;
4600 struct glsl_src_param src1_param;
4601 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4602 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4603 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4604 struct glsl_sample_function sample_function;
4605 DWORD reg = ins->dst[0].reg.idx[0].offset;
4606 char coord_mask[6];
4608 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4609 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
4611 /* Perform the last matrix multiply operation */
4612 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
4613 /* Reflection calculation */
4614 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
4616 /* Dependent read, not valid with conditional NP2 */
4617 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4618 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4620 /* Sample the texture */
4621 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4622 NULL, NULL, NULL, "tmp0%s", coord_mask);
4623 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4625 tex_mx->current_row = 0;
4628 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
4629 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
4630 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
4632 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4633 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
4634 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
4635 struct glsl_sample_function sample_function;
4636 DWORD reg = ins->dst[0].reg.idx[0].offset;
4637 struct glsl_src_param src0_param;
4638 char coord_mask[6];
4640 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
4642 /* Perform the last matrix multiply operation */
4643 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
4645 /* Construct the eye-ray vector from w coordinates */
4646 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
4647 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
4648 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
4650 /* Dependent read, not valid with conditional NP2 */
4651 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
4652 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
4654 /* Sample the texture using the calculated coordinates */
4655 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
4656 NULL, NULL, NULL, "tmp0%s", coord_mask);
4657 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4659 tex_mx->current_row = 0;
4662 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
4663 * Apply a fake bump map transform.
4664 * texbem is pshader <= 1.3 only, this saves a few version checks
4666 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
4668 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4669 struct glsl_sample_function sample_function;
4670 struct glsl_src_param coord_param;
4671 DWORD sampler_idx;
4672 DWORD mask;
4673 DWORD flags;
4674 char coord_mask[6];
4676 sampler_idx = ins->dst[0].reg.idx[0].offset;
4677 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4678 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4680 /* Dependent read, not valid with conditional NP2 */
4681 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4682 mask = sample_function.coord_mask;
4684 shader_glsl_write_mask_to_str(mask, coord_mask);
4686 /* With projected textures, texbem only divides the static texture coord,
4687 * not the displacement, so we can't let GL handle this. */
4688 if (flags & WINED3D_PSARGS_PROJECTED)
4690 DWORD div_mask=0;
4691 char coord_div_mask[3];
4692 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4694 case WINED3D_TTFF_COUNT1:
4695 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4696 break;
4697 case WINED3D_TTFF_COUNT2:
4698 div_mask = WINED3DSP_WRITEMASK_1;
4699 break;
4700 case WINED3D_TTFF_COUNT3:
4701 div_mask = WINED3DSP_WRITEMASK_2;
4702 break;
4703 case WINED3D_TTFF_COUNT4:
4704 case WINED3D_TTFF_DISABLE:
4705 div_mask = WINED3DSP_WRITEMASK_3;
4706 break;
4708 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
4709 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
4712 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
4714 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4715 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
4716 coord_param.param_str, coord_mask);
4718 if (ins->handler_idx == WINED3DSIH_TEXBEML)
4720 struct glsl_src_param luminance_param;
4721 struct glsl_dst_param dst_param;
4723 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
4724 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4726 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
4727 dst_param.reg_name, dst_param.mask_str,
4728 luminance_param.param_str, sampler_idx, sampler_idx);
4730 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4733 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
4735 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4736 struct glsl_src_param src0_param, src1_param;
4738 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4739 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4741 shader_glsl_append_dst(ins->ctx->buffer, ins);
4742 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
4743 src0_param.param_str, sampler_idx, src1_param.param_str);
4746 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
4747 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
4748 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
4750 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4751 struct glsl_sample_function sample_function;
4752 struct glsl_src_param src0_param;
4754 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4756 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4757 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4758 "%s.wx", src0_param.reg_name);
4759 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4762 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
4763 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
4764 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
4766 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4767 struct glsl_sample_function sample_function;
4768 struct glsl_src_param src0_param;
4770 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
4772 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4773 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4774 "%s.yz", src0_param.reg_name);
4775 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4778 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
4779 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
4780 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
4782 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
4783 struct glsl_sample_function sample_function;
4784 struct glsl_src_param src0_param;
4786 /* Dependent read, not valid with conditional NP2 */
4787 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
4788 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
4790 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
4791 "%s", src0_param.param_str);
4792 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4795 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4796 * If any of the first 3 components are < 0, discard this pixel */
4797 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4799 if (ins->ctx->reg_maps->shader_version.major >= 4)
4801 struct glsl_src_param src_param;
4803 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4804 shader_addline(ins->ctx->buffer, "if (bool(floatBitsToUint(%s))) discard;\n", src_param.param_str);
4806 else
4808 struct glsl_dst_param dst_param;
4810 /* The argument is a destination parameter, and no writemasks are allowed */
4811 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4813 /* 2.0 shaders compare all 4 components in texkill. */
4814 if (ins->ctx->reg_maps->shader_version.major >= 2)
4815 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4816 /* 1.x shaders only compare the first 3 components, probably due to
4817 * the nature of the texkill instruction as a tex* instruction, and
4818 * phase, which kills all .w components. Even if all 4 components are
4819 * defined, only the first 3 are used. */
4820 else
4821 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4825 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4826 * dst = dot2(src0, src1) + src2 */
4827 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4829 struct glsl_src_param src0_param;
4830 struct glsl_src_param src1_param;
4831 struct glsl_src_param src2_param;
4832 DWORD write_mask;
4833 unsigned int mask_size;
4835 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4836 mask_size = shader_glsl_get_write_mask_size(write_mask);
4838 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4839 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4840 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4842 if (mask_size > 1) {
4843 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4844 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4845 } else {
4846 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4847 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4851 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
4852 const struct wined3d_shader_signature *input_signature,
4853 const struct wined3d_shader_reg_maps *reg_maps,
4854 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
4856 unsigned int i;
4858 for (i = 0; i < input_signature->element_count; ++i)
4860 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4861 const char *semantic_name;
4862 UINT semantic_idx;
4863 char reg_mask[6];
4865 /* Unused */
4866 if (!(reg_maps->input_registers & (1u << input->register_idx)))
4867 continue;
4869 semantic_name = input->semantic_name;
4870 semantic_idx = input->semantic_idx;
4871 shader_glsl_write_mask_to_str(input->mask, reg_mask);
4873 if (args->vp_mode == vertexshader)
4875 if (input->sysval_semantic == WINED3D_SV_POSITION)
4876 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
4877 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4878 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4879 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
4880 else
4881 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
4882 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4883 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
4885 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4887 if (args->pointsprite)
4888 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
4889 shader->u.ps.input_reg_map[input->register_idx]);
4890 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
4891 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
4892 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
4893 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
4894 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
4895 else
4896 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4897 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4899 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4901 if (!semantic_idx)
4902 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
4903 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4904 else if (semantic_idx == 1)
4905 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
4906 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4907 else
4908 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4909 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4911 else
4913 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4914 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
4919 /*********************************************
4920 * Vertex Shader Specific Code begins here
4921 ********************************************/
4923 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4925 struct glsl_program_key key;
4927 key.vs_id = entry->vs.id;
4928 key.gs_id = entry->gs.id;
4929 key.ps_id = entry->ps.id;
4931 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4933 ERR("Failed to insert program entry.\n");
4937 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4938 GLuint vs_id, GLuint gs_id, GLuint ps_id)
4940 struct wine_rb_entry *entry;
4941 struct glsl_program_key key;
4943 key.vs_id = vs_id;
4944 key.gs_id = gs_id;
4945 key.ps_id = ps_id;
4947 entry = wine_rb_get(&priv->program_lookup, &key);
4948 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4951 /* Context activation is done by the caller. */
4952 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4953 struct glsl_shader_prog_link *entry)
4955 struct glsl_program_key key;
4957 key.vs_id = entry->vs.id;
4958 key.gs_id = entry->gs.id;
4959 key.ps_id = entry->ps.id;
4960 wine_rb_remove(&priv->program_lookup, &key);
4962 GL_EXTCALL(glDeleteProgram(entry->id));
4963 if (entry->vs.id)
4964 list_remove(&entry->vs.shader_entry);
4965 if (entry->gs.id)
4966 list_remove(&entry->gs.shader_entry);
4967 if (entry->ps.id)
4968 list_remove(&entry->ps.shader_entry);
4969 HeapFree(GetProcessHeap(), 0, entry->vs.uniform_f_locations);
4970 HeapFree(GetProcessHeap(), 0, entry->ps.uniform_f_locations);
4971 HeapFree(GetProcessHeap(), 0, entry);
4974 static void handle_ps3_input(struct shader_glsl_priv *priv,
4975 const struct wined3d_gl_info *gl_info, const DWORD *map,
4976 const struct wined3d_shader_signature *input_signature,
4977 const struct wined3d_shader_reg_maps *reg_maps_in,
4978 const struct wined3d_shader_signature *output_signature,
4979 const struct wined3d_shader_reg_maps *reg_maps_out)
4981 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
4982 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
4983 unsigned int i, j;
4984 DWORD *set;
4985 DWORD in_idx;
4986 unsigned int in_count = vec4_varyings(3, gl_info);
4987 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
4988 char reg_mask[6];
4989 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
4991 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * max_varyings);
4993 for (i = 0; i < input_signature->element_count; ++i)
4995 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
4997 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
4998 continue;
5000 in_idx = map[input->register_idx];
5001 /* Declared, but not read register */
5002 if (in_idx == ~0u)
5003 continue;
5004 if (in_idx >= max_varyings)
5006 FIXME("More input varyings declared than supported, expect issues.\n");
5007 continue;
5010 if (in_idx == in_count)
5011 string_buffer_sprintf(destination, "gl_FrontColor");
5012 else if (in_idx == in_count + 1)
5013 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5014 else
5015 string_buffer_sprintf(destination, "ps_link[%u]", in_idx);
5017 if (!set[in_idx])
5018 set[in_idx] = ~0u;
5020 for (j = 0; j < output_signature->element_count; ++j)
5022 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5023 DWORD mask;
5025 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5026 || input->semantic_idx != output->semantic_idx
5027 || strcmp(input->semantic_name, output->semantic_name)
5028 || !(mask = input->mask & output->mask))
5029 continue;
5031 if (set[in_idx] == ~0u)
5032 set[in_idx] = 0;
5033 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5034 shader_glsl_write_mask_to_str(mask, reg_mask);
5036 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
5037 destination->buffer, reg_mask, output->register_idx, reg_mask);
5041 for (i = 0; i < max_varyings; ++i)
5043 unsigned int size;
5045 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5046 continue;
5048 if (set[i] == ~0U) set[i] = 0;
5050 size = 0;
5051 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
5052 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
5053 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
5054 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
5055 reg_mask[size] = '\0';
5057 if (i == in_count)
5058 string_buffer_sprintf(destination, "gl_FrontColor");
5059 else if (i == in_count + 1)
5060 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5061 else
5062 string_buffer_sprintf(destination, "ps_link[%u]", i);
5064 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5065 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5068 HeapFree(GetProcessHeap(), 0, set);
5069 string_buffer_release(&priv->string_buffers, destination);
5072 /* Context activation is done by the caller. */
5073 static GLuint generate_param_reorder_function(struct shader_glsl_priv *priv,
5074 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5075 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5077 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5078 GLuint ret;
5079 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5080 unsigned int i;
5081 const char *semantic_name;
5082 UINT semantic_idx;
5083 char reg_mask[6];
5084 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5086 string_buffer_clear(buffer);
5088 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &vs->reg_maps.shader_version));
5090 if (per_vertex_point_size)
5092 shader_addline(buffer, "uniform struct\n{\n");
5093 shader_addline(buffer, " float size_min;\n");
5094 shader_addline(buffer, " float size_max;\n");
5095 shader_addline(buffer, "} ffp_point;\n");
5098 if (ps_major < 3)
5100 DWORD colors_written_mask[2] = {0};
5101 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5103 if (!legacy_context)
5105 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5106 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5107 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5108 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5111 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5113 for (i = 0; i < vs->output_signature.element_count; ++i)
5115 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5116 DWORD write_mask;
5118 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5119 continue;
5121 semantic_name = output->semantic_name;
5122 semantic_idx = output->semantic_idx;
5123 write_mask = output->mask;
5124 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5126 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5128 if (legacy_context)
5129 shader_addline(buffer, "gl_Front%sColor%s = vs_out[%u]%s;\n",
5130 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5131 else
5132 shader_addline(buffer, "ffp_varying_%s%s = clamp(vs_out[%u]%s, 0.0, 1.0);\n",
5133 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5135 colors_written_mask[semantic_idx] = write_mask;
5137 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5139 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5140 reg_mask, output->register_idx, reg_mask);
5142 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5144 if (semantic_idx < MAX_TEXTURES)
5146 shader_addline(buffer, "%s[%u]%s = vs_out[%u]%s;\n",
5147 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5148 semantic_idx, reg_mask, output->register_idx, reg_mask);
5149 texcoords_written_mask[semantic_idx] = write_mask;
5152 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5154 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5155 output->register_idx, reg_mask[1]);
5157 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
5159 shader_addline(buffer, "%s = clamp(vs_out[%u].%c, 0.0, 1.0);\n",
5160 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
5161 output->register_idx, reg_mask[1]);
5165 for (i = 0; i < 2; ++i)
5167 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5169 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5170 if (!i)
5171 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
5172 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
5173 reg_mask, reg_mask);
5174 else
5175 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
5176 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
5177 reg_mask, reg_mask);
5180 for (i = 0; i < MAX_TEXTURES; ++i)
5182 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
5183 continue;
5185 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
5187 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
5188 && !texcoords_written_mask[i])
5189 continue;
5191 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
5192 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
5193 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
5197 else
5199 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
5201 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
5202 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits->packed_output);
5204 /* First, sort out position and point size. Those are not passed to the pixel shader */
5205 for (i = 0; i < vs->output_signature.element_count; ++i)
5207 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5209 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5210 continue;
5212 semantic_name = output->semantic_name;
5213 semantic_idx = output->semantic_idx;
5214 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5216 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5218 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
5219 reg_mask, output->register_idx, reg_mask);
5221 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5223 shader_addline(buffer, "gl_PointSize = clamp(vs_out[%u].%c, ffp_point.size_min, ffp_point.size_max);\n",
5224 output->register_idx, reg_mask[1]);
5228 /* Then, fix the pixel shader input */
5229 handle_ps3_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
5230 &ps->reg_maps, &vs->output_signature, &vs->reg_maps);
5233 shader_addline(buffer, "}\n");
5235 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5236 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
5237 shader_glsl_compile(gl_info, ret, buffer->buffer);
5239 return ret;
5242 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer)
5244 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
5245 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
5246 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
5247 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
5248 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
5249 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
5252 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer, enum wined3d_ffp_ps_fog_mode mode)
5254 switch (mode)
5256 case WINED3D_FFP_PS_FOG_OFF:
5257 return;
5259 case WINED3D_FFP_PS_FOG_LINEAR:
5260 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
5261 break;
5263 case WINED3D_FFP_PS_FOG_EXP:
5264 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
5265 break;
5267 case WINED3D_FFP_PS_FOG_EXP2:
5268 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
5269 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
5270 break;
5272 default:
5273 ERR("Invalid fog mode %#x.\n", mode);
5274 return;
5277 shader_addline(buffer, "gl_FragData[0].xyz = mix(ffp_fog.color.xyz, gl_FragData[0].xyz,"
5278 " clamp(fog, 0.0, 1.0));\n");
5281 /* Context activation is done by the caller. */
5282 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
5283 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5284 const struct wined3d_shader *shader,
5285 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
5287 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5288 const struct wined3d_gl_info *gl_info = context->gl_info;
5289 const DWORD *function = shader->function;
5290 struct shader_glsl_ctx_priv priv_ctx;
5291 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5293 /* Create the hw GLSL shader object and assign it as the shader->prgId */
5294 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
5296 memset(&priv_ctx, 0, sizeof(priv_ctx));
5297 priv_ctx.cur_ps_args = args;
5298 priv_ctx.cur_np2fixup_info = np2fixup_info;
5299 priv_ctx.string_buffers = string_buffers;
5301 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5303 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5304 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5305 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
5306 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
5307 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5308 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5309 /* The spec says that it doesn't have to be explicitly enabled, but the
5310 * nvidia drivers write a warning if we don't do so. */
5311 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
5312 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
5313 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5314 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5315 if (gl_info->supported[EXT_GPU_SHADER4])
5316 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5318 /* Base Declarations */
5319 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5321 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
5323 unsigned int i;
5324 WORD map = reg_maps->texcoord;
5326 if (legacy_context)
5328 if (glsl_is_color_reg_read(shader, 0))
5329 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
5330 if (glsl_is_color_reg_read(shader, 1))
5331 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
5334 for (i = 0; map; map >>= 1, ++i)
5336 if (map & 1)
5338 if (args->pointsprite)
5339 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
5340 else if (args->texcoords_initialized & (1u << i))
5341 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
5342 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
5343 else
5344 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
5345 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
5349 if (legacy_context)
5350 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
5353 /* Pack 3.0 inputs */
5354 if (reg_maps->shader_version.major >= 3)
5355 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
5357 /* Base Shader Body */
5358 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5360 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
5361 if (reg_maps->shader_version.major < 2)
5363 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
5364 shader_addline(buffer, "gl_FragData[0] = R0;\n");
5367 if (args->srgb_correction)
5368 shader_glsl_generate_srgb_write_correction(buffer);
5370 /* SM < 3 does not replace the fog stage. */
5371 if (reg_maps->shader_version.major < 3)
5372 shader_glsl_generate_fog_code(buffer, args->fog);
5374 shader_addline(buffer, "}\n");
5376 TRACE("Compiling shader object %u.\n", shader_id);
5377 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5379 return shader_id;
5382 /* Context activation is done by the caller. */
5383 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
5384 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5385 const struct wined3d_shader *shader,
5386 const struct vs_compile_args *args)
5388 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5389 const struct wined3d_gl_info *gl_info = context->gl_info;
5390 const DWORD *function = shader->function;
5391 struct shader_glsl_ctx_priv priv_ctx;
5392 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5394 /* Create the hw GLSL shader program and assign it as the shader->prgId */
5395 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
5397 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5399 if (gl_info->supported[ARB_DRAW_INSTANCED])
5400 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
5401 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5402 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5403 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5404 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5405 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5406 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5407 if (gl_info->supported[EXT_GPU_SHADER4])
5408 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5410 memset(&priv_ctx, 0, sizeof(priv_ctx));
5411 priv_ctx.cur_vs_args = args;
5412 priv_ctx.string_buffers = string_buffers;
5414 /* Base Declarations */
5415 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5417 /* Base Shader Body */
5418 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5420 /* Unpack outputs */
5421 shader_addline(buffer, "order_ps_input(vs_out);\n");
5423 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
5424 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
5425 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
5426 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
5428 if (reg_maps->shader_version.major < 3)
5430 if (args->fog_src == VS_FOG_Z)
5431 shader_addline(buffer, "%s = gl_Position.z;\n",
5432 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5433 else if (!reg_maps->fog)
5434 shader_addline(buffer, "%s = 0.0;\n",
5435 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
5438 /* We always store the clipplanes without y inversion */
5439 if (args->clip_enabled)
5440 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
5442 if (args->point_size && !args->per_vertex_point_size)
5443 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
5445 /* Write the final position.
5447 * OpenGL coordinates specify the center of the pixel while d3d coords specify
5448 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
5449 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
5450 * contains 1.0 to allow a mad.
5452 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
5453 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
5455 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
5457 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
5458 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
5459 * which is the same as z = z * 2 - w.
5461 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
5463 shader_addline(buffer, "}\n");
5465 TRACE("Compiling shader object %u.\n", shader_id);
5466 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5468 return shader_id;
5471 /* Context activation is done by the caller. */
5472 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
5473 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5474 const struct wined3d_shader *shader)
5476 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
5477 const struct wined3d_gl_info *gl_info = context->gl_info;
5478 const DWORD *function = shader->function;
5479 struct shader_glsl_ctx_priv priv_ctx;
5480 GLuint shader_id;
5482 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
5484 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, &reg_maps->shader_version));
5486 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
5487 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
5488 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
5489 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
5490 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5491 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
5492 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
5493 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
5494 if (gl_info->supported[EXT_GPU_SHADER4])
5495 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
5497 memset(&priv_ctx, 0, sizeof(priv_ctx));
5498 priv_ctx.string_buffers = string_buffers;
5499 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
5500 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
5501 shader_addline(buffer, "}\n");
5503 TRACE("Compiling shader object %u.\n", shader_id);
5504 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
5506 return shader_id;
5509 static GLuint find_glsl_pshader(const struct wined3d_context *context,
5510 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5511 struct wined3d_shader *shader,
5512 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
5514 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
5515 struct glsl_shader_private *shader_data;
5516 struct ps_np2fixup_info *np2fixup;
5517 UINT i;
5518 DWORD new_size;
5519 GLuint ret;
5521 if (!shader->backend_data)
5523 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5524 if (!shader->backend_data)
5526 ERR("Failed to allocate backend data.\n");
5527 return 0;
5530 shader_data = shader->backend_data;
5531 gl_shaders = shader_data->gl_shaders.ps;
5533 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5534 * so a linear search is more performant than a hashmap or a binary search
5535 * (cache coherency etc)
5537 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5539 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
5541 if (args->np2_fixup)
5542 *np2fixup_info = &gl_shaders[i].np2fixup;
5543 return gl_shaders[i].id;
5547 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5548 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5549 if (shader_data->num_gl_shaders)
5551 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5552 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
5553 new_size * sizeof(*gl_shaders));
5555 else
5557 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5558 new_size = 1;
5561 if(!new_array) {
5562 ERR("Out of memory\n");
5563 return 0;
5565 shader_data->gl_shaders.ps = new_array;
5566 shader_data->shader_array_size = new_size;
5567 gl_shaders = new_array;
5570 gl_shaders[shader_data->num_gl_shaders].args = *args;
5572 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
5573 memset(np2fixup, 0, sizeof(*np2fixup));
5574 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
5576 pixelshader_update_resource_types(shader, args->tex_types);
5578 string_buffer_clear(buffer);
5579 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
5580 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5582 return ret;
5585 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
5586 const DWORD use_map)
5588 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
5589 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
5590 if (stored->point_size != new->point_size)
5591 return FALSE;
5592 if (stored->per_vertex_point_size != new->per_vertex_point_size)
5593 return FALSE;
5594 if (stored->flatshading != new->flatshading)
5595 return FALSE;
5596 return stored->fog_src == new->fog_src;
5599 static GLuint find_glsl_vshader(const struct wined3d_context *context,
5600 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5601 struct wined3d_shader *shader,
5602 const struct vs_compile_args *args)
5604 UINT i;
5605 DWORD new_size;
5606 DWORD use_map = context->stream_info.use_map;
5607 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
5608 struct glsl_shader_private *shader_data;
5609 GLuint ret;
5611 if (!shader->backend_data)
5613 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
5614 if (!shader->backend_data)
5616 ERR("Failed to allocate backend data.\n");
5617 return 0;
5620 shader_data = shader->backend_data;
5621 gl_shaders = shader_data->gl_shaders.vs;
5623 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
5624 * so a linear search is more performant than a hashmap or a binary search
5625 * (cache coherency etc)
5627 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5629 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
5630 return gl_shaders[i].id;
5633 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5635 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
5636 if (shader_data->num_gl_shaders)
5638 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
5639 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
5640 new_size * sizeof(*gl_shaders));
5642 else
5644 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
5645 new_size = 1;
5648 if(!new_array) {
5649 ERR("Out of memory\n");
5650 return 0;
5652 shader_data->gl_shaders.vs = new_array;
5653 shader_data->shader_array_size = new_size;
5654 gl_shaders = new_array;
5657 gl_shaders[shader_data->num_gl_shaders].args = *args;
5659 string_buffer_clear(buffer);
5660 ret = shader_glsl_generate_vshader(context, buffer, string_buffers, shader, args);
5661 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5663 return ret;
5666 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
5667 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
5668 struct wined3d_shader *shader)
5670 struct glsl_gs_compiled_shader *gl_shaders;
5671 struct glsl_shader_private *shader_data;
5672 GLuint ret;
5674 if (!shader->backend_data)
5676 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
5678 ERR("Failed to allocate backend data.\n");
5679 return 0;
5682 shader_data = shader->backend_data;
5683 gl_shaders = shader_data->gl_shaders.gs;
5685 if (shader_data->num_gl_shaders)
5686 return gl_shaders[0].id;
5688 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
5690 if (!(shader_data->gl_shaders.gs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
5692 ERR("Failed to allocate GL shader array.\n");
5693 return 0;
5695 shader_data->shader_array_size = 1;
5696 gl_shaders = shader_data->gl_shaders.gs;
5698 string_buffer_clear(buffer);
5699 ret = shader_glsl_generate_geometry_shader(context, buffer, string_buffers, shader);
5700 gl_shaders[shader_data->num_gl_shaders++].id = ret;
5702 return ret;
5705 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
5707 switch (mcs)
5709 case WINED3D_MCS_MATERIAL:
5710 return material;
5711 case WINED3D_MCS_COLOR1:
5712 return "ffp_attrib_diffuse";
5713 case WINED3D_MCS_COLOR2:
5714 return "ffp_attrib_specular";
5715 default:
5716 ERR("Invalid material color source %#x.\n", mcs);
5717 return "<invalid>";
5721 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
5722 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
5724 const char *diffuse, *specular, *emissive, *ambient;
5725 enum wined3d_light_type light_type;
5726 unsigned int i;
5728 if (!settings->lighting)
5730 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
5731 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
5732 return;
5735 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
5736 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
5737 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
5738 shader_addline(buffer, "vec3 dir, dst;\n");
5739 shader_addline(buffer, "float att, t;\n");
5741 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
5742 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
5743 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
5744 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
5746 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
5748 light_type = (settings->light_type >> WINED3D_FFP_LIGHT_TYPE_SHIFT(i)) & WINED3D_FFP_LIGHT_TYPE_MASK;
5749 switch (light_type)
5751 case WINED3D_LIGHT_POINT:
5752 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5753 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5754 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5755 shader_addline(buffer, "dst.x = 1.0;\n");
5756 if (legacy_lighting)
5758 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5759 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5761 else
5763 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5765 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5766 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", i, i, i);
5767 if (!legacy_lighting)
5768 shader_addline(buffer, "att = 1.0 / att;\n");
5769 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5770 if (!settings->normal)
5772 if (!legacy_lighting)
5773 shader_addline(buffer, "}\n");
5774 break;
5776 shader_addline(buffer, "dir = normalize(dir);\n");
5777 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5778 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5779 if (settings->localviewer)
5780 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5781 else
5782 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5783 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5784 " * ffp_light[%u].specular) * att;\n", i);
5785 if (!legacy_lighting)
5786 shader_addline(buffer, "}\n");
5787 break;
5789 case WINED3D_LIGHT_SPOT:
5790 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", i);
5791 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
5792 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
5793 shader_addline(buffer, "dst.x = 1.0;\n");
5794 if (legacy_lighting)
5796 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", i, i);
5797 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
5799 else
5801 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", i);
5803 shader_addline(buffer, "dir = normalize(dir);\n");
5804 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", i);
5805 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", i);
5806 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", i);
5807 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
5808 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
5809 i, i, i, i);
5810 if (legacy_lighting)
5811 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5812 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5813 i, i, i);
5814 else
5815 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
5816 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
5817 i, i, i);
5818 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", i);
5819 if (!settings->normal)
5821 if (!legacy_lighting)
5822 shader_addline(buffer, "}\n");
5823 break;
5825 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
5826 " * ffp_light[%u].diffuse.xyz) * att;\n", i);
5827 if (settings->localviewer)
5828 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5829 else
5830 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5831 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
5832 " * ffp_light[%u].specular) * att;\n", i);
5833 if (!legacy_lighting)
5834 shader_addline(buffer, "}\n");
5835 break;
5837 case WINED3D_LIGHT_DIRECTIONAL:
5838 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5839 if (!settings->normal)
5840 break;
5841 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", i);
5842 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5843 " * ffp_light[%u].diffuse.xyz;\n", i);
5844 /* TODO: In the non-local viewer case the halfvector is constant
5845 * and could be precomputed and stored in a uniform. */
5846 if (settings->localviewer)
5847 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5848 else
5849 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
5850 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5851 " * ffp_light[%u].specular;\n", i);
5852 break;
5854 case WINED3D_LIGHT_PARALLELPOINT:
5855 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", i);
5856 if (!settings->normal)
5857 break;
5858 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", i);
5859 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
5860 " * ffp_light[%u].diffuse.xyz;\n", i);
5861 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
5862 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
5863 " * ffp_light[%u].specular;\n", i);
5864 break;
5866 default:
5867 if (light_type)
5868 FIXME("Unhandled light type %#x.\n", light_type);
5869 continue;
5873 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
5874 ambient, diffuse, emissive);
5875 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
5876 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
5879 /* Context activation is done by the caller. */
5880 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
5881 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
5883 static const struct attrib_info
5885 const char type[6];
5886 const char name[24];
5888 attrib_info[] =
5890 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
5891 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
5892 /* TODO: Indexed vertex blending */
5893 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
5894 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
5895 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
5896 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
5897 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
5899 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5900 BOOL legacy_lighting = priv->legacy_lighting;
5901 GLuint shader_obj;
5902 unsigned int i;
5903 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5904 BOOL output_legacy_fogcoord = legacy_context;
5906 string_buffer_clear(buffer);
5908 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
5910 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
5912 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
5914 shader_addline(buffer, "attribute %s vs_in%u;\n", type, i);
5916 shader_addline(buffer, "\n");
5918 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
5919 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
5920 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
5921 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
5923 shader_addline(buffer, "uniform struct\n{\n");
5924 shader_addline(buffer, " vec4 emissive;\n");
5925 shader_addline(buffer, " vec4 ambient;\n");
5926 shader_addline(buffer, " vec4 diffuse;\n");
5927 shader_addline(buffer, " vec4 specular;\n");
5928 shader_addline(buffer, " float shininess;\n");
5929 shader_addline(buffer, "} ffp_material;\n");
5931 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
5932 shader_addline(buffer, "uniform struct\n{\n");
5933 shader_addline(buffer, " vec4 diffuse;\n");
5934 shader_addline(buffer, " vec4 specular;\n");
5935 shader_addline(buffer, " vec4 ambient;\n");
5936 shader_addline(buffer, " vec4 position;\n");
5937 shader_addline(buffer, " vec3 direction;\n");
5938 shader_addline(buffer, " float range;\n");
5939 shader_addline(buffer, " float falloff;\n");
5940 shader_addline(buffer, " float c_att;\n");
5941 shader_addline(buffer, " float l_att;\n");
5942 shader_addline(buffer, " float q_att;\n");
5943 shader_addline(buffer, " float cos_htheta;\n");
5944 shader_addline(buffer, " float cos_hphi;\n");
5945 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
5947 if (settings->point_size)
5949 shader_addline(buffer, "uniform struct\n{\n");
5950 shader_addline(buffer, " float size;\n");
5951 shader_addline(buffer, " float size_min;\n");
5952 shader_addline(buffer, " float size_max;\n");
5953 shader_addline(buffer, " float c_att;\n");
5954 shader_addline(buffer, " float l_att;\n");
5955 shader_addline(buffer, " float q_att;\n");
5956 shader_addline(buffer, "} ffp_point;\n");
5959 if (legacy_context)
5961 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
5962 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
5963 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5964 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
5966 else
5968 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
5969 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
5970 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5971 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5974 shader_addline(buffer, "\nvoid main()\n{\n");
5975 shader_addline(buffer, "float m;\n");
5976 shader_addline(buffer, "vec3 r;\n");
5978 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
5980 if (attrib_info[i].name[0])
5981 shader_addline(buffer, "%s %s = vs_in%u;\n",
5982 attrib_info[i].type, attrib_info[i].name, i);
5984 for (i = 0; i < MAX_TEXTURES; ++i)
5986 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
5987 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU)
5989 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
5993 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
5995 if (settings->transformed)
5997 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
5998 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
5999 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6001 else
6003 for (i = 0; i < settings->vertexblends; ++i)
6004 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6006 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6007 for (i = 0; i < settings->vertexblends + 1; ++i)
6008 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6010 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6011 if (settings->clipping)
6012 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6013 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
6016 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
6017 if (settings->normal)
6019 if (!settings->vertexblends)
6021 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
6023 else
6025 for (i = 0; i < settings->vertexblends + 1; ++i)
6026 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
6029 if (settings->normalize)
6030 shader_addline(buffer, "normal = normalize(normal);\n");
6033 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
6034 if (legacy_context)
6036 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
6037 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
6040 for (i = 0; i < MAX_TEXTURES; ++i)
6042 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6044 switch (settings->texgen[i] & 0xffff0000)
6046 case WINED3DTSS_TCI_PASSTHRU:
6047 if (settings->texcoords & (1u << i))
6048 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
6049 i, i, i);
6050 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
6051 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
6052 else
6053 output_legacy_texcoord = FALSE;
6054 break;
6056 case WINED3DTSS_TCI_CAMERASPACENORMAL:
6057 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
6058 break;
6060 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
6061 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
6062 break;
6064 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
6065 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6066 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
6067 break;
6069 case WINED3DTSS_TCI_SPHEREMAP:
6070 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
6071 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
6072 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
6073 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
6074 break;
6076 default:
6077 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
6078 break;
6080 if (output_legacy_texcoord)
6081 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
6084 switch (settings->fog_mode)
6086 case WINED3D_FFP_VS_FOG_OFF:
6087 output_legacy_fogcoord = FALSE;
6088 break;
6090 case WINED3D_FFP_VS_FOG_FOGCOORD:
6091 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
6092 break;
6094 case WINED3D_FFP_VS_FOG_RANGE:
6095 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
6096 break;
6098 case WINED3D_FFP_VS_FOG_DEPTH:
6099 if (settings->ortho_fog)
6100 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
6101 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
6102 else if (settings->transformed)
6103 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
6104 else
6105 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
6106 break;
6108 default:
6109 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
6110 break;
6112 if (output_legacy_fogcoord)
6113 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
6115 if (settings->point_size)
6117 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
6118 " + ffp_point.l_att * length(ec_pos.xyz)"
6119 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
6120 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
6121 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
6124 shader_addline(buffer, "}\n");
6126 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6127 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
6129 return shader_obj;
6132 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
6133 DWORD argnum, unsigned int stage, DWORD arg)
6135 const char *ret;
6137 if (arg == ARG_UNUSED)
6138 return "<unused arg>";
6140 switch (arg & WINED3DTA_SELECTMASK)
6142 case WINED3DTA_DIFFUSE:
6143 ret = "ffp_varying_diffuse";
6144 break;
6146 case WINED3DTA_CURRENT:
6147 ret = "ret";
6148 break;
6150 case WINED3DTA_TEXTURE:
6151 switch (stage)
6153 case 0: ret = "tex0"; break;
6154 case 1: ret = "tex1"; break;
6155 case 2: ret = "tex2"; break;
6156 case 3: ret = "tex3"; break;
6157 case 4: ret = "tex4"; break;
6158 case 5: ret = "tex5"; break;
6159 case 6: ret = "tex6"; break;
6160 case 7: ret = "tex7"; break;
6161 default:
6162 ret = "<invalid texture>";
6163 break;
6165 break;
6167 case WINED3DTA_TFACTOR:
6168 ret = "tex_factor";
6169 break;
6171 case WINED3DTA_SPECULAR:
6172 ret = "ffp_varying_specular";
6173 break;
6175 case WINED3DTA_TEMP:
6176 ret = "temp_reg";
6177 break;
6179 case WINED3DTA_CONSTANT:
6180 switch (stage)
6182 case 0: ret = "tss_const0"; break;
6183 case 1: ret = "tss_const1"; break;
6184 case 2: ret = "tss_const2"; break;
6185 case 3: ret = "tss_const3"; break;
6186 case 4: ret = "tss_const4"; break;
6187 case 5: ret = "tss_const5"; break;
6188 case 6: ret = "tss_const6"; break;
6189 case 7: ret = "tss_const7"; break;
6190 default:
6191 ret = "<invalid constant>";
6192 break;
6194 break;
6196 default:
6197 return "<unhandled arg>";
6200 if (arg & WINED3DTA_COMPLEMENT)
6202 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
6203 if (argnum == 0)
6204 ret = "arg0";
6205 else if (argnum == 1)
6206 ret = "arg1";
6207 else if (argnum == 2)
6208 ret = "arg2";
6211 if (arg & WINED3DTA_ALPHAREPLICATE)
6213 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
6214 if (argnum == 0)
6215 ret = "arg0";
6216 else if (argnum == 1)
6217 ret = "arg1";
6218 else if (argnum == 2)
6219 ret = "arg2";
6222 return ret;
6225 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
6226 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
6228 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
6230 if (color && alpha)
6231 dstmask = "";
6232 else if (color)
6233 dstmask = ".xyz";
6234 else
6235 dstmask = ".w";
6237 if (dst == tempreg)
6238 dstreg = "temp_reg";
6239 else
6240 dstreg = "ret";
6242 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
6243 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
6244 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
6246 switch (op)
6248 case WINED3D_TOP_DISABLE:
6249 break;
6251 case WINED3D_TOP_SELECT_ARG1:
6252 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
6253 break;
6255 case WINED3D_TOP_SELECT_ARG2:
6256 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
6257 break;
6259 case WINED3D_TOP_MODULATE:
6260 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6261 break;
6263 case WINED3D_TOP_MODULATE_4X:
6264 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
6265 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6266 break;
6268 case WINED3D_TOP_MODULATE_2X:
6269 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
6270 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6271 break;
6273 case WINED3D_TOP_ADD:
6274 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
6275 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6276 break;
6278 case WINED3D_TOP_ADD_SIGNED:
6279 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
6280 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6281 break;
6283 case WINED3D_TOP_ADD_SIGNED_2X:
6284 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
6285 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6286 break;
6288 case WINED3D_TOP_SUBTRACT:
6289 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
6290 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
6291 break;
6293 case WINED3D_TOP_ADD_SMOOTH:
6294 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
6295 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
6296 break;
6298 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
6299 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
6300 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6301 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6302 break;
6304 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6305 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6306 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6307 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6308 break;
6310 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6311 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
6312 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6313 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6314 break;
6316 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6317 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
6318 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6319 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
6320 break;
6322 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
6323 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
6324 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
6325 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
6326 break;
6328 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
6329 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
6330 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6331 break;
6333 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
6334 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
6335 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6336 break;
6338 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
6339 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
6340 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
6341 break;
6342 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
6343 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
6344 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
6345 break;
6347 case WINED3D_TOP_BUMPENVMAP:
6348 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6349 /* These are handled in the first pass, nothing to do. */
6350 break;
6352 case WINED3D_TOP_DOTPRODUCT3:
6353 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
6354 dstreg, dstmask, arg1, arg2, dstmask);
6355 break;
6357 case WINED3D_TOP_MULTIPLY_ADD:
6358 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
6359 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
6360 break;
6362 case WINED3D_TOP_LERP:
6363 /* MSDN isn't quite right here. */
6364 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
6365 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
6366 break;
6368 default:
6369 FIXME("Unhandled operation %#x.\n", op);
6370 break;
6374 /* Context activation is done by the caller. */
6375 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
6376 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
6378 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6379 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
6380 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
6381 UINT lowest_disabled_stage;
6382 GLuint shader_id;
6383 DWORD arg0, arg1, arg2;
6384 unsigned int stage;
6385 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
6386 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6388 string_buffer_clear(buffer);
6390 /* Find out which textures are read */
6391 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6393 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6394 break;
6396 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
6397 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
6398 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
6400 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
6401 || (stage == 0 && settings->color_key_enabled))
6402 tex_map |= 1u << stage;
6403 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6404 tfactor_used = TRUE;
6405 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6406 tempreg_used = TRUE;
6407 if (settings->op[stage].dst == tempreg)
6408 tempreg_used = TRUE;
6409 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6410 tss_const_map |= 1u << stage;
6412 switch (settings->op[stage].cop)
6414 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
6415 lum_map |= 1u << stage;
6416 /* fall through */
6417 case WINED3D_TOP_BUMPENVMAP:
6418 bump_map |= 1u << stage;
6419 /* fall through */
6420 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
6421 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
6422 tex_map |= 1u << stage;
6423 break;
6425 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
6426 tfactor_used = TRUE;
6427 break;
6429 default:
6430 break;
6433 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6434 continue;
6436 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
6437 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
6438 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
6440 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
6441 tex_map |= 1u << stage;
6442 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
6443 tfactor_used = TRUE;
6444 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
6445 tempreg_used = TRUE;
6446 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
6447 tss_const_map |= 1u << stage;
6449 lowest_disabled_stage = stage;
6451 shader_addline(buffer, "%s\n", shader_glsl_get_version(gl_info, NULL));
6453 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6454 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6456 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
6457 shader_addline(buffer, "vec4 ret;\n");
6458 if (tempreg_used || settings->sRGB_write)
6459 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
6460 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
6462 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6464 if (tss_const_map & (1u << stage))
6465 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
6467 if (!(tex_map & (1u << stage)))
6468 continue;
6470 switch (settings->op[stage].tex_type)
6472 case WINED3D_GL_RES_TYPE_TEX_1D:
6473 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
6474 break;
6475 case WINED3D_GL_RES_TYPE_TEX_2D:
6476 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
6477 break;
6478 case WINED3D_GL_RES_TYPE_TEX_3D:
6479 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
6480 break;
6481 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6482 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
6483 break;
6484 case WINED3D_GL_RES_TYPE_TEX_RECT:
6485 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
6486 break;
6487 default:
6488 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
6489 break;
6492 shader_addline(buffer, "vec4 tex%u;\n", stage);
6494 if (!(bump_map & (1u << stage)))
6495 continue;
6496 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
6498 if (!(lum_map & (1u << stage)))
6499 continue;
6500 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
6501 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
6503 if (tfactor_used)
6504 shader_addline(buffer, "uniform vec4 tex_factor;\n");
6505 if (settings->color_key_enabled)
6506 shader_addline(buffer, "uniform vec4 color_key[2];\n");
6507 shader_addline(buffer, "uniform vec4 specular_enable;\n");
6509 if (settings->sRGB_write)
6511 shader_addline(buffer, "const vec4 srgb_const0 = ");
6512 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
6513 shader_addline(buffer, ";\n");
6514 shader_addline(buffer, "const vec4 srgb_const1 = ");
6515 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
6516 shader_addline(buffer, ";\n");
6519 shader_addline(buffer, "uniform struct\n{\n");
6520 shader_addline(buffer, " vec4 color;\n");
6521 shader_addline(buffer, " float density;\n");
6522 shader_addline(buffer, " float end;\n");
6523 shader_addline(buffer, " float scale;\n");
6524 shader_addline(buffer, "} ffp_fog;\n");
6526 if (legacy_context)
6528 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6529 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6530 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6531 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6532 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6534 else
6536 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6537 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6538 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6539 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
6540 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6543 shader_addline(buffer, "void main()\n{\n");
6545 if (legacy_context)
6547 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6548 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6551 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6553 if (tex_map & (1u << stage))
6555 if (settings->pointsprite)
6556 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
6557 else if (settings->texcoords_initialized & (1u << stage))
6558 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
6559 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
6560 else
6561 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
6565 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
6566 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6568 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
6569 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
6571 /* Generate texture sampling instructions */
6572 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
6574 const char *texture_function, *coord_mask;
6575 BOOL proj;
6577 if (!(tex_map & (1u << stage)))
6578 continue;
6580 if (settings->op[stage].projected == proj_none)
6582 proj = FALSE;
6584 else if (settings->op[stage].projected == proj_count4
6585 || settings->op[stage].projected == proj_count3)
6587 proj = TRUE;
6589 else
6591 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
6592 proj = TRUE;
6595 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
6596 proj = FALSE;
6598 switch (settings->op[stage].tex_type)
6600 case WINED3D_GL_RES_TYPE_TEX_1D:
6601 if (proj)
6603 texture_function = "texture1DProj";
6604 coord_mask = "xw";
6606 else
6608 texture_function = "texture1D";
6609 coord_mask = "x";
6611 break;
6612 case WINED3D_GL_RES_TYPE_TEX_2D:
6613 if (proj)
6615 texture_function = "texture2DProj";
6616 coord_mask = "xyw";
6618 else
6620 texture_function = "texture2D";
6621 coord_mask = "xy";
6623 break;
6624 case WINED3D_GL_RES_TYPE_TEX_3D:
6625 if (proj)
6627 texture_function = "texture3DProj";
6628 coord_mask = "xyzw";
6630 else
6632 texture_function = "texture3D";
6633 coord_mask = "xyz";
6635 break;
6636 case WINED3D_GL_RES_TYPE_TEX_CUBE:
6637 texture_function = "textureCube";
6638 coord_mask = "xyz";
6639 break;
6640 case WINED3D_GL_RES_TYPE_TEX_RECT:
6641 if (proj)
6643 texture_function = "texture2DRectProj";
6644 coord_mask = "xyw";
6646 else
6648 texture_function = "texture2DRect";
6649 coord_mask = "xy";
6651 break;
6652 default:
6653 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
6654 texture_function = "";
6655 coord_mask = "xyzw";
6656 break;
6658 if (!needs_legacy_glsl_syntax(gl_info))
6659 texture_function = proj ? "textureProj" : "texture";
6661 if (stage > 0
6662 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
6663 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
6665 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
6667 /* With projective textures, texbem only divides the static
6668 * texture coord, not the displacement, so multiply the
6669 * displacement with the dividing parameter before passing it to
6670 * TXP. */
6671 if (settings->op[stage].projected != proj_none)
6673 if (settings->op[stage].projected == proj_count4)
6675 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
6676 stage, stage);
6677 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
6679 else
6681 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
6682 stage, stage);
6683 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
6686 else
6688 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
6691 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
6692 stage, texture_function, stage, coord_mask);
6694 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6695 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
6696 stage, stage - 1, stage - 1, stage - 1);
6698 else if (settings->op[stage].projected == proj_count3)
6700 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
6701 stage, texture_function, stage, stage);
6703 else
6705 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
6706 stage, texture_function, stage, stage, coord_mask);
6709 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
6710 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
6711 settings->op[stage].color_fixup);
6714 if (settings->color_key_enabled)
6716 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
6717 shader_addline(buffer, " discard;\n");
6720 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
6722 /* Generate the main shader */
6723 for (stage = 0; stage < MAX_TEXTURES; ++stage)
6725 BOOL op_equal;
6727 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
6728 break;
6730 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6731 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6732 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
6733 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
6734 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6735 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
6736 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6737 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
6738 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
6739 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
6740 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
6741 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
6742 else
6743 op_equal = settings->op[stage].aop == settings->op[stage].cop
6744 && settings->op[stage].carg0 == settings->op[stage].aarg0
6745 && settings->op[stage].carg1 == settings->op[stage].aarg1
6746 && settings->op[stage].carg2 == settings->op[stage].aarg2;
6748 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
6750 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6751 settings->op[stage].cop, settings->op[stage].carg0,
6752 settings->op[stage].carg1, settings->op[stage].carg2);
6754 else if (op_equal)
6756 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
6757 settings->op[stage].cop, settings->op[stage].carg0,
6758 settings->op[stage].carg1, settings->op[stage].carg2);
6760 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
6761 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
6763 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
6764 settings->op[stage].cop, settings->op[stage].carg0,
6765 settings->op[stage].carg1, settings->op[stage].carg2);
6766 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
6767 settings->op[stage].aop, settings->op[stage].aarg0,
6768 settings->op[stage].aarg1, settings->op[stage].aarg2);
6772 shader_addline(buffer, "gl_FragData[0] = ffp_varying_specular * specular_enable + ret;\n");
6774 if (settings->sRGB_write)
6775 shader_glsl_generate_srgb_write_correction(buffer);
6777 shader_glsl_generate_fog_code(buffer, settings->fog);
6779 shader_addline(buffer, "}\n");
6781 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6782 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6784 string_buffer_release(&priv->string_buffers, tex_reg_name);
6785 return shader_id;
6788 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
6789 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
6791 struct glsl_ffp_vertex_shader *shader;
6792 const struct wine_rb_entry *entry;
6794 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
6795 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
6797 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
6798 return NULL;
6800 shader->desc.settings = *settings;
6801 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
6802 list_init(&shader->linked_programs);
6803 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
6804 ERR("Failed to insert ffp vertex shader.\n");
6806 return shader;
6809 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
6810 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
6812 struct glsl_ffp_fragment_shader *glsl_desc;
6813 const struct ffp_frag_desc *desc;
6815 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
6816 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
6818 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
6819 return NULL;
6821 glsl_desc->entry.settings = *args;
6822 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
6823 list_init(&glsl_desc->linked_programs);
6824 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
6826 return glsl_desc;
6830 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
6831 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
6833 unsigned int i;
6834 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6836 vs->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6837 sizeof(GLuint) * gl_info->limits.glsl_vs_float_constants);
6838 for (i = 0; i < vs_c_count; ++i)
6840 string_buffer_sprintf(name, "vs_c[%u]", i);
6841 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6843 memset(&vs->uniform_f_locations[vs_c_count], 0xff,
6844 (gl_info->limits.glsl_vs_float_constants - vs_c_count) * sizeof(GLuint));
6846 for (i = 0; i < MAX_CONST_I; ++i)
6848 string_buffer_sprintf(name, "vs_i[%u]", i);
6849 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6852 for (i = 0; i < MAX_CONST_B; ++i)
6854 string_buffer_sprintf(name, "vs_b[%u]", i);
6855 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6858 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "posFixup"));
6860 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
6862 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
6863 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6865 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
6866 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
6867 for (i = 0; i < MAX_TEXTURES; ++i)
6869 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
6870 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6872 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
6873 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
6874 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
6875 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
6876 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
6877 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
6878 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
6880 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
6881 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6882 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
6883 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6884 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
6885 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6886 string_buffer_sprintf(name, "ffp_light[%u].position", i);
6887 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6888 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
6889 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6890 string_buffer_sprintf(name, "ffp_light[%u].range", i);
6891 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6892 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
6893 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6894 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
6895 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6896 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
6897 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6898 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
6899 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6900 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
6901 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6902 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
6903 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6905 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
6906 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
6907 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
6908 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
6909 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
6910 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
6912 string_buffer_release(&priv->string_buffers, name);
6915 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
6916 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
6918 unsigned int i;
6919 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6921 ps->uniform_f_locations = HeapAlloc(GetProcessHeap(), 0,
6922 sizeof(GLuint) * gl_info->limits.glsl_ps_float_constants);
6923 for (i = 0; i < ps_c_count; ++i)
6925 string_buffer_sprintf(name, "ps_c[%u]", i);
6926 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6928 memset(&ps->uniform_f_locations[ps_c_count], 0xff,
6929 (gl_info->limits.glsl_ps_float_constants - ps_c_count) * sizeof(GLuint));
6931 for (i = 0; i < MAX_CONST_I; ++i)
6933 string_buffer_sprintf(name, "ps_i[%u]", i);
6934 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6937 for (i = 0; i < MAX_CONST_B; ++i)
6939 string_buffer_sprintf(name, "ps_b[%u]", i);
6940 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6943 for (i = 0; i < MAX_TEXTURES; ++i)
6945 string_buffer_sprintf(name, "bumpenv_mat%u", i);
6946 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6947 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
6948 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6949 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
6950 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6951 string_buffer_sprintf(name, "tss_const%u", i);
6952 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
6955 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
6956 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
6958 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
6959 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
6960 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
6961 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
6963 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
6964 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
6965 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
6967 string_buffer_release(&priv->string_buffers, name);
6970 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
6971 struct shader_glsl_priv *priv, GLuint program_id,
6972 const struct wined3d_shader_reg_maps *reg_maps, unsigned int base, unsigned int count)
6974 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
6975 GLuint block_idx;
6976 unsigned int i;
6977 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
6979 for (i = 0; i < count; ++i)
6981 if (!reg_maps->cb_sizes[i])
6982 continue;
6984 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
6985 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
6986 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
6988 checkGLcall("glUniformBlockBinding");
6989 string_buffer_release(&priv->string_buffers, name);
6992 /* Context activation is done by the caller. */
6993 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
6994 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
6996 const struct wined3d_gl_info *gl_info = context->gl_info;
6997 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
6998 const struct ps_np2fixup_info *np2fixup_info = NULL;
6999 struct glsl_shader_prog_link *entry = NULL;
7000 struct wined3d_shader *vshader = NULL;
7001 struct wined3d_shader *gshader = NULL;
7002 struct wined3d_shader *pshader = NULL;
7003 GLuint program_id;
7004 GLuint reorder_shader_id = 0;
7005 unsigned int i;
7006 GLuint vs_id = 0;
7007 GLuint gs_id = 0;
7008 GLuint ps_id = 0;
7009 struct list *ps_list, *vs_list;
7010 WORD attribs_map;
7011 struct wined3d_string_buffer *tmp_name;
7013 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
7015 vs_id = ctx_data->glsl_program->vs.id;
7016 vs_list = &ctx_data->glsl_program->vs.shader_entry;
7018 if (use_vs(state))
7020 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7021 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
7023 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
7024 && ctx_data->glsl_program->gs.id)
7025 gs_id = ctx_data->glsl_program->gs.id;
7026 else if (gshader)
7027 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7030 else if (use_vs(state))
7032 struct vs_compile_args vs_compile_args;
7034 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
7036 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
7037 vs_id = find_glsl_vshader(context, &priv->shader_buffer, &priv->string_buffers, vshader, &vs_compile_args);
7038 vs_list = &vshader->linked_programs;
7040 if ((gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY]))
7041 gs_id = find_glsl_geometry_shader(context, &priv->shader_buffer, &priv->string_buffers, gshader);
7043 else if (priv->vertex_pipe == &glsl_vertex_pipe)
7045 struct glsl_ffp_vertex_shader *ffp_shader;
7046 struct wined3d_ffp_vs_settings settings;
7048 wined3d_ffp_get_vs_settings(context, state, &settings);
7049 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
7050 vs_id = ffp_shader->id;
7051 vs_list = &ffp_shader->linked_programs;
7054 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
7056 ps_id = ctx_data->glsl_program->ps.id;
7057 ps_list = &ctx_data->glsl_program->ps.shader_entry;
7059 if (use_ps(state))
7060 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7062 else if (use_ps(state))
7064 struct ps_compile_args ps_compile_args;
7065 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
7066 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
7067 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
7068 pshader, &ps_compile_args, &np2fixup_info);
7069 ps_list = &pshader->linked_programs;
7071 else if (priv->fragment_pipe == &glsl_fragment_pipe)
7073 struct glsl_ffp_fragment_shader *ffp_shader;
7074 struct ffp_frag_settings settings;
7076 gen_ffp_frag_op(context, state, &settings, FALSE);
7077 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
7078 ps_id = ffp_shader->id;
7079 ps_list = &ffp_shader->linked_programs;
7082 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
7084 ctx_data->glsl_program = entry;
7085 return;
7088 /* If we get to this point, then no matching program exists, so we create one */
7089 program_id = GL_EXTCALL(glCreateProgram());
7090 TRACE("Created new GLSL shader program %u.\n", program_id);
7092 /* Create the entry */
7093 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
7094 entry->id = program_id;
7095 entry->vs.id = vs_id;
7096 entry->gs.id = gs_id;
7097 entry->ps.id = ps_id;
7098 entry->constant_version = 0;
7099 entry->ps.np2_fixup_info = np2fixup_info;
7100 /* Add the hash table entry */
7101 add_glsl_program_entry(priv, entry);
7103 /* Set the current program */
7104 ctx_data->glsl_program = entry;
7106 /* Attach GLSL vshader */
7107 if (vs_id)
7109 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
7110 GL_EXTCALL(glAttachShader(program_id, vs_id));
7111 checkGLcall("glAttachShader");
7113 list_add_head(vs_list, &entry->vs.shader_entry);
7116 if (vshader)
7118 attribs_map = vshader->reg_maps.input_registers;
7119 reorder_shader_id = generate_param_reorder_function(priv, vshader, pshader,
7120 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
7121 d3d_info->emulated_flatshading
7122 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
7123 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
7124 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
7125 checkGLcall("glAttachShader");
7126 /* Flag the reorder function for deletion, then it will be freed automatically when the program
7127 * is destroyed
7129 GL_EXTCALL(glDeleteShader(reorder_shader_id));
7131 else
7133 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
7136 /* Bind vertex attributes to a corresponding index number to match
7137 * the same index numbers as ARB_vertex_programs (makes loading
7138 * vertex attributes simpler). With this method, we can use the
7139 * exact same code to load the attributes later for both ARB and
7140 * GLSL shaders.
7142 * We have to do this here because we need to know the Program ID
7143 * in order to make the bindings work, and it has to be done prior
7144 * to linking the GLSL program. */
7145 tmp_name = string_buffer_get(&priv->string_buffers);
7146 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
7148 if (!(attribs_map & 1))
7149 continue;
7151 string_buffer_sprintf(tmp_name, "vs_in%u", i);
7152 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
7154 checkGLcall("glBindAttribLocation");
7155 string_buffer_release(&priv->string_buffers, tmp_name);
7157 if (gshader)
7159 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
7160 GL_EXTCALL(glAttachShader(program_id, gs_id));
7161 checkGLcall("glAttachShader");
7163 TRACE("input type %s, output type %s, vertices out %u.\n",
7164 debug_d3dprimitivetype(gshader->u.gs.input_type),
7165 debug_d3dprimitivetype(gshader->u.gs.output_type),
7166 gshader->u.gs.vertices_out);
7167 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
7168 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
7169 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
7170 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
7171 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
7172 gshader->u.gs.vertices_out));
7173 checkGLcall("glProgramParameteriARB");
7175 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
7178 /* Attach GLSL pshader */
7179 if (ps_id)
7181 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
7182 GL_EXTCALL(glAttachShader(program_id, ps_id));
7183 checkGLcall("glAttachShader");
7185 list_add_head(ps_list, &entry->ps.shader_entry);
7188 /* Link the program */
7189 TRACE("Linking GLSL shader program %u.\n", program_id);
7190 GL_EXTCALL(glLinkProgram(program_id));
7191 shader_glsl_validate_link(gl_info, program_id);
7193 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
7194 vshader ? min(vshader->limits->constant_float, gl_info->limits.glsl_vs_float_constants) : 0);
7195 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
7196 pshader ? min(pshader->limits->constant_float, gl_info->limits.glsl_ps_float_constants) : 0);
7197 checkGLcall("Find glsl program uniform locations");
7199 if (pshader && pshader->reg_maps.shader_version.major >= 3
7200 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
7202 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
7203 entry->vs.vertex_color_clamp = GL_FALSE;
7205 else
7207 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
7210 /* Set the shader to allow uniform loading on it */
7211 GL_EXTCALL(glUseProgram(program_id));
7212 checkGLcall("glUseProgram");
7214 /* Texture unit mapping is set up to be the same each time the shader
7215 * program is used so we can hardcode the sampler uniform values. */
7216 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
7218 entry->constant_update_mask = 0;
7219 if (vshader)
7221 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
7222 if (vshader->reg_maps.integer_constants)
7223 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
7224 if (vshader->reg_maps.boolean_constants)
7225 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
7226 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
7228 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps,
7229 0, gl_info->limits.vertex_uniform_blocks);
7231 else
7233 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
7234 | WINED3D_SHADER_CONST_FFP_PROJ;
7236 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
7238 if (entry->vs.modelview_matrix_location[i] != -1)
7240 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
7241 break;
7245 for (i = 0; i < MAX_TEXTURES; ++i)
7247 if (entry->vs.texture_matrix_location[i] != -1)
7249 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
7250 break;
7253 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
7254 || entry->vs.material_specular_location != -1
7255 || entry->vs.material_emissive_location != -1
7256 || entry->vs.material_shininess_location != -1)
7257 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
7258 if (entry->vs.light_ambient_location != -1)
7259 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
7261 if (entry->vs.pointsize_min_location != -1)
7262 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
7264 if (gshader)
7265 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps,
7266 gl_info->limits.vertex_uniform_blocks, gl_info->limits.geometry_uniform_blocks);
7268 if (ps_id)
7270 if (pshader)
7272 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
7273 if (pshader->reg_maps.integer_constants)
7274 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
7275 if (pshader->reg_maps.boolean_constants)
7276 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
7277 if (entry->ps.ycorrection_location != -1)
7278 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
7280 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps,
7281 gl_info->limits.vertex_uniform_blocks + gl_info->limits.geometry_uniform_blocks,
7282 gl_info->limits.fragment_uniform_blocks);
7284 else
7286 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
7289 for (i = 0; i < MAX_TEXTURES; ++i)
7291 if (entry->ps.bumpenv_mat_location[i] != -1)
7293 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
7294 break;
7298 if (entry->ps.fog_color_location != -1)
7299 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
7300 if (entry->ps.np2_fixup_location != -1)
7301 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
7302 if (entry->ps.color_key_location != -1)
7303 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
7307 /* Context activation is done by the caller. */
7308 static GLuint create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum wined3d_gl_resource_type tex_type,
7309 BOOL masked)
7311 GLuint program_id;
7312 GLuint vshader_id, pshader_id;
7313 const char *blt_pshader;
7315 static const char blt_vshader[] =
7316 "#version 120\n"
7317 "void main(void)\n"
7318 "{\n"
7319 " gl_Position = gl_Vertex;\n"
7320 " gl_FrontColor = vec4(1.0);\n"
7321 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
7322 "}\n";
7324 static const char * const blt_pshaders_full[WINED3D_GL_RES_TYPE_COUNT] =
7326 /* WINED3D_GL_RES_TYPE_TEX_1D */
7327 NULL,
7328 /* WINED3D_GL_RES_TYPE_TEX_2D */
7329 "#version 120\n"
7330 "uniform sampler2D sampler;\n"
7331 "void main(void)\n"
7332 "{\n"
7333 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7334 "}\n",
7335 /* WINED3D_GL_RES_TYPE_TEX_3D */
7336 NULL,
7337 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7338 "#version 120\n"
7339 "uniform samplerCube sampler;\n"
7340 "void main(void)\n"
7341 "{\n"
7342 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7343 "}\n",
7344 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7345 "#version 120\n"
7346 "#extension GL_ARB_texture_rectangle : enable\n"
7347 "uniform sampler2DRect sampler;\n"
7348 "void main(void)\n"
7349 "{\n"
7350 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7351 "}\n",
7352 /* WINED3D_GL_RES_TYPE_BUFFER */
7353 NULL,
7354 /* WINED3D_GL_RES_TYPE_RB */
7355 NULL,
7358 static const char * const blt_pshaders_masked[WINED3D_GL_RES_TYPE_COUNT] =
7360 /* WINED3D_GL_RES_TYPE_TEX_1D */
7361 NULL,
7362 /* WINED3D_GL_RES_TYPE_TEX_2D */
7363 "#version 120\n"
7364 "uniform sampler2D sampler;\n"
7365 "uniform vec4 mask;\n"
7366 "void main(void)\n"
7367 "{\n"
7368 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7369 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
7370 "}\n",
7371 /* WINED3D_GL_RES_TYPE_TEX_3D */
7372 NULL,
7373 /* WINED3D_GL_RES_TYPE_TEX_CUBE */
7374 "#version 120\n"
7375 "uniform samplerCube sampler;\n"
7376 "uniform vec4 mask;\n"
7377 "void main(void)\n"
7378 "{\n"
7379 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7380 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
7381 "}\n",
7382 /* WINED3D_GL_RES_TYPE_TEX_RECT */
7383 "#version 120\n"
7384 "#extension GL_ARB_texture_rectangle : enable\n"
7385 "uniform sampler2DRect sampler;\n"
7386 "uniform vec4 mask;\n"
7387 "void main(void)\n"
7388 "{\n"
7389 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
7390 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
7391 "}\n",
7392 /* WINED3D_GL_RES_TYPE_BUFFER */
7393 NULL,
7394 /* WINED3D_GL_RES_TYPE_RB */
7395 NULL,
7398 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
7399 if (!blt_pshader)
7401 FIXME("tex_type %#x not supported\n", tex_type);
7402 return 0;
7405 vshader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7406 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
7408 pshader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7409 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
7411 program_id = GL_EXTCALL(glCreateProgram());
7412 GL_EXTCALL(glAttachShader(program_id, vshader_id));
7413 GL_EXTCALL(glAttachShader(program_id, pshader_id));
7414 GL_EXTCALL(glLinkProgram(program_id));
7416 shader_glsl_validate_link(gl_info, program_id);
7418 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
7419 * is destroyed
7421 GL_EXTCALL(glDeleteShader(vshader_id));
7422 GL_EXTCALL(glDeleteShader(pshader_id));
7423 return program_id;
7426 /* Context activation is done by the caller. */
7427 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
7428 const struct wined3d_state *state)
7430 struct glsl_context_data *ctx_data = context->shader_backend_data;
7431 const struct wined3d_gl_info *gl_info = context->gl_info;
7432 struct shader_glsl_priv *priv = shader_priv;
7433 GLuint program_id = 0, prev_id = 0;
7434 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
7436 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
7437 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
7439 if (ctx_data->glsl_program)
7441 prev_id = ctx_data->glsl_program->id;
7442 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7444 else
7446 prev_id = 0;
7447 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7450 set_glsl_shader_program(context, state, priv, ctx_data);
7452 if (ctx_data->glsl_program)
7454 program_id = ctx_data->glsl_program->id;
7455 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
7457 else
7459 program_id = 0;
7460 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
7463 if (old_vertex_color_clamp != current_vertex_color_clamp)
7465 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7467 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
7468 checkGLcall("glClampColorARB");
7470 else
7472 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
7476 TRACE("Using GLSL program %u.\n", program_id);
7478 if (prev_id != program_id)
7480 GL_EXTCALL(glUseProgram(program_id));
7481 checkGLcall("glUseProgram");
7483 if (program_id)
7484 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
7488 /* "context" is not necessarily the currently active context. */
7489 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
7491 struct glsl_context_data *ctx_data = context->shader_backend_data;
7493 ctx_data->glsl_program = NULL;
7494 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
7495 | (1u << WINED3D_SHADER_TYPE_VERTEX)
7496 | (1u << WINED3D_SHADER_TYPE_GEOMETRY);
7499 /* Context activation is done by the caller. */
7500 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
7502 const struct wined3d_gl_info *gl_info = context->gl_info;
7503 struct shader_glsl_priv *priv = shader_priv;
7505 shader_glsl_invalidate_current_program(context);
7506 GL_EXTCALL(glUseProgram(0));
7507 checkGLcall("glUseProgram");
7509 priv->vertex_pipe->vp_enable(gl_info, FALSE);
7510 priv->fragment_pipe->enable_extension(gl_info, FALSE);
7512 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
7514 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
7515 checkGLcall("glClampColorARB");
7519 /* Context activation is done by the caller. */
7520 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
7521 enum wined3d_gl_resource_type tex_type, const SIZE *ds_mask_size)
7523 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
7524 struct shader_glsl_priv *priv = shader_priv;
7525 GLuint *blt_program;
7526 GLint loc;
7528 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
7529 if (!*blt_program)
7531 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
7532 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "sampler"));
7533 GL_EXTCALL(glUseProgram(*blt_program));
7534 GL_EXTCALL(glUniform1i(loc, 0));
7536 else
7538 GL_EXTCALL(glUseProgram(*blt_program));
7541 if (masked)
7543 loc = GL_EXTCALL(glGetUniformLocation(*blt_program, "mask"));
7544 GL_EXTCALL(glUniform4f(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
7548 /* Context activation is done by the caller. */
7549 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
7551 const struct glsl_context_data *ctx_data = context_get_current()->shader_backend_data;
7552 GLuint program_id;
7554 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
7555 if (program_id) TRACE("Using GLSL program %u\n", program_id);
7557 GL_EXTCALL(glUseProgram(program_id));
7558 checkGLcall("glUseProgram");
7561 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
7562 const struct glsl_shader_prog_link *program)
7564 const struct glsl_context_data *ctx_data;
7565 struct wined3d_context *context;
7566 unsigned int i;
7568 for (i = 0; i < device->context_count; ++i)
7570 context = device->contexts[i];
7571 ctx_data = context->shader_backend_data;
7573 if (ctx_data->glsl_program == program)
7574 shader_glsl_invalidate_current_program(context);
7578 static void shader_glsl_destroy(struct wined3d_shader *shader)
7580 struct glsl_shader_private *shader_data = shader->backend_data;
7581 struct wined3d_device *device = shader->device;
7582 struct shader_glsl_priv *priv = device->shader_priv;
7583 const struct wined3d_gl_info *gl_info;
7584 const struct list *linked_programs;
7585 struct wined3d_context *context;
7587 if (!shader_data || !shader_data->num_gl_shaders)
7589 HeapFree(GetProcessHeap(), 0, shader_data);
7590 shader->backend_data = NULL;
7591 return;
7594 context = context_acquire(device, NULL);
7595 gl_info = context->gl_info;
7597 TRACE("Deleting linked programs.\n");
7598 linked_programs = &shader->linked_programs;
7599 if (linked_programs->next)
7601 struct glsl_shader_prog_link *entry, *entry2;
7602 UINT i;
7604 switch (shader->reg_maps.shader_version.type)
7606 case WINED3D_SHADER_TYPE_PIXEL:
7608 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
7610 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7612 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
7613 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7614 checkGLcall("glDeleteShader");
7616 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
7618 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7619 struct glsl_shader_prog_link, ps.shader_entry)
7621 shader_glsl_invalidate_contexts_program(device, entry);
7622 delete_glsl_program_entry(priv, gl_info, entry);
7625 break;
7628 case WINED3D_SHADER_TYPE_VERTEX:
7630 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
7632 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7634 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
7635 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7636 checkGLcall("glDeleteShader");
7638 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
7640 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7641 struct glsl_shader_prog_link, vs.shader_entry)
7643 shader_glsl_invalidate_contexts_program(device, entry);
7644 delete_glsl_program_entry(priv, gl_info, entry);
7647 break;
7650 case WINED3D_SHADER_TYPE_GEOMETRY:
7652 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
7654 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7656 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
7657 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
7658 checkGLcall("glDeleteShader");
7660 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
7662 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
7663 struct glsl_shader_prog_link, gs.shader_entry)
7665 shader_glsl_invalidate_contexts_program(device, entry);
7666 delete_glsl_program_entry(priv, gl_info, entry);
7669 break;
7672 default:
7673 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
7674 break;
7678 HeapFree(GetProcessHeap(), 0, shader->backend_data);
7679 shader->backend_data = NULL;
7681 context_release(context);
7684 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
7686 const struct glsl_program_key *k = key;
7687 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
7688 const struct glsl_shader_prog_link, program_lookup_entry);
7690 if (k->vs_id > prog->vs.id) return 1;
7691 else if (k->vs_id < prog->vs.id) return -1;
7693 if (k->gs_id > prog->gs.id) return 1;
7694 else if (k->gs_id < prog->gs.id) return -1;
7696 if (k->ps_id > prog->ps.id) return 1;
7697 else if (k->ps_id < prog->ps.id) return -1;
7699 return 0;
7702 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
7704 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
7705 + constant_count * sizeof(*heap->contained)
7706 + constant_count * sizeof(*heap->positions);
7707 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
7709 if (!mem)
7711 ERR("Failed to allocate memory\n");
7712 return FALSE;
7715 heap->entries = mem;
7716 heap->entries[1].version = 0;
7717 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
7718 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
7719 heap->positions = (unsigned int *)(heap->contained + constant_count);
7720 heap->size = 1;
7722 return TRUE;
7725 static void constant_heap_free(struct constant_heap *heap)
7727 HeapFree(GetProcessHeap(), 0, heap->entries);
7730 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
7732 wined3d_rb_alloc,
7733 wined3d_rb_realloc,
7734 wined3d_rb_free,
7735 glsl_program_key_compare,
7738 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
7739 const struct fragment_pipeline *fragment_pipe)
7741 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7742 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
7743 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
7744 gl_info->limits.glsl_ps_float_constants)) + 1;
7745 struct fragment_caps fragment_caps;
7746 void *vertex_priv, *fragment_priv;
7748 string_buffer_list_init(&priv->string_buffers);
7750 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
7752 ERR("Failed to initialize vertex pipe.\n");
7753 HeapFree(GetProcessHeap(), 0, priv);
7754 return E_FAIL;
7757 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
7759 ERR("Failed to initialize fragment pipe.\n");
7760 vertex_pipe->vp_free(device);
7761 HeapFree(GetProcessHeap(), 0, priv);
7762 return E_FAIL;
7765 if (!string_buffer_init(&priv->shader_buffer))
7767 ERR("Failed to initialize shader buffer.\n");
7768 goto fail;
7771 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
7772 if (!priv->stack)
7774 ERR("Failed to allocate memory.\n");
7775 goto fail;
7778 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
7780 ERR("Failed to initialize vertex shader constant heap\n");
7781 goto fail;
7784 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
7786 ERR("Failed to initialize pixel shader constant heap\n");
7787 goto fail;
7790 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
7792 ERR("Failed to initialize rbtree.\n");
7793 goto fail;
7796 priv->next_constant_version = 1;
7797 priv->vertex_pipe = vertex_pipe;
7798 priv->fragment_pipe = fragment_pipe;
7799 fragment_pipe->get_caps(gl_info, &fragment_caps);
7800 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
7801 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
7803 device->vertex_priv = vertex_priv;
7804 device->fragment_priv = fragment_priv;
7805 device->shader_priv = priv;
7807 return WINED3D_OK;
7809 fail:
7810 constant_heap_free(&priv->pconst_heap);
7811 constant_heap_free(&priv->vconst_heap);
7812 HeapFree(GetProcessHeap(), 0, priv->stack);
7813 string_buffer_free(&priv->shader_buffer);
7814 fragment_pipe->free_private(device);
7815 vertex_pipe->vp_free(device);
7816 HeapFree(GetProcessHeap(), 0, priv);
7817 return E_OUTOFMEMORY;
7820 /* Context activation is done by the caller. */
7821 static void shader_glsl_free(struct wined3d_device *device)
7823 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
7824 struct shader_glsl_priv *priv = device->shader_priv;
7825 int i;
7827 for (i = 0; i < WINED3D_GL_RES_TYPE_COUNT; ++i)
7829 if (priv->depth_blt_program_full[i])
7831 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_full[i]));
7833 if (priv->depth_blt_program_masked[i])
7835 GL_EXTCALL(glDeleteProgram(priv->depth_blt_program_masked[i]));
7839 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
7840 constant_heap_free(&priv->pconst_heap);
7841 constant_heap_free(&priv->vconst_heap);
7842 HeapFree(GetProcessHeap(), 0, priv->stack);
7843 string_buffer_list_cleanup(&priv->string_buffers);
7844 string_buffer_free(&priv->shader_buffer);
7845 priv->fragment_pipe->free_private(device);
7846 priv->vertex_pipe->vp_free(device);
7848 HeapFree(GetProcessHeap(), 0, device->shader_priv);
7849 device->shader_priv = NULL;
7852 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
7854 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
7855 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
7858 static void shader_glsl_free_context_data(struct wined3d_context *context)
7860 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
7863 static void shader_glsl_init_context_state(struct wined3d_context *context)
7865 const struct wined3d_gl_info *gl_info = context->gl_info;
7867 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
7868 checkGLcall("GL_PROGRAM_POINT_SIZE");
7871 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
7873 UINT shader_model;
7875 if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
7876 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS])
7877 shader_model = 4;
7878 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
7879 * texldd and texldl instructions. */
7880 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
7881 shader_model = 3;
7882 else
7883 shader_model = 2;
7884 TRACE("Shader model %u.\n", shader_model);
7886 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
7887 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
7888 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
7890 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
7891 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
7892 caps->varying_count = gl_info->limits.glsl_varyings;
7894 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
7895 * Direct3D minimum requirement.
7897 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
7898 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
7900 * The problem is that the refrast clamps temporary results in the shader to
7901 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
7902 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
7903 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
7904 * offer a way to query this.
7906 if (shader_model >= 4)
7907 caps->ps_1x_max_value = FLT_MAX;
7908 else
7909 caps->ps_1x_max_value = 1024.0f;
7911 /* Ideally we'd only set caps like sRGB writes here if supported by both
7912 * the shader backend and the fragment pipe, but we can get called before
7913 * shader_glsl_alloc(). */
7914 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
7915 | WINED3D_SHADER_CAP_SRGB_WRITE;
7918 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
7920 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
7922 TRACE("Checking support for fixup:\n");
7923 dump_color_fixup_desc(fixup);
7926 /* We support everything except YUV conversions. */
7927 if (!is_complex_fixup(fixup))
7929 TRACE("[OK]\n");
7930 return TRUE;
7933 TRACE("[FAILED]\n");
7934 return FALSE;
7937 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
7939 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
7940 /* WINED3DSIH_ADD */ shader_glsl_binop,
7941 /* WINED3DSIH_AND */ shader_glsl_binop,
7942 /* WINED3DSIH_BEM */ shader_glsl_bem,
7943 /* WINED3DSIH_BREAK */ shader_glsl_break,
7944 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
7945 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
7946 /* WINED3DSIH_CALL */ shader_glsl_call,
7947 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
7948 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
7949 /* WINED3DSIH_CND */ shader_glsl_cnd,
7950 /* WINED3DSIH_CRS */ shader_glsl_cross,
7951 /* WINED3DSIH_CUT */ shader_glsl_cut,
7952 /* WINED3DSIH_DCL */ shader_glsl_nop,
7953 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
7954 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
7955 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
7956 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
7957 /* WINED3DSIH_DEF */ shader_glsl_nop,
7958 /* WINED3DSIH_DEFB */ shader_glsl_nop,
7959 /* WINED3DSIH_DEFI */ shader_glsl_nop,
7960 /* WINED3DSIH_DIV */ shader_glsl_binop,
7961 /* WINED3DSIH_DP2 */ shader_glsl_dot,
7962 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
7963 /* WINED3DSIH_DP3 */ shader_glsl_dot,
7964 /* WINED3DSIH_DP4 */ shader_glsl_dot,
7965 /* WINED3DSIH_DST */ shader_glsl_dst,
7966 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
7967 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
7968 /* WINED3DSIH_ELSE */ shader_glsl_else,
7969 /* WINED3DSIH_EMIT */ shader_glsl_emit,
7970 /* WINED3DSIH_ENDIF */ shader_glsl_end,
7971 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
7972 /* WINED3DSIH_ENDREP */ shader_glsl_end,
7973 /* WINED3DSIH_EQ */ shader_glsl_relop,
7974 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
7975 /* WINED3DSIH_EXPP */ shader_glsl_expp,
7976 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
7977 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
7978 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
7979 /* WINED3DSIH_GE */ shader_glsl_relop,
7980 /* WINED3DSIH_IADD */ shader_glsl_binop,
7981 /* WINED3DSIH_IEQ */ NULL,
7982 /* WINED3DSIH_IF */ shader_glsl_if,
7983 /* WINED3DSIH_IFC */ shader_glsl_ifc,
7984 /* WINED3DSIH_IGE */ shader_glsl_relop,
7985 /* WINED3DSIH_ILT */ NULL,
7986 /* WINED3DSIH_IMAD */ shader_glsl_mad,
7987 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
7988 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
7989 /* WINED3DSIH_IMUL */ shader_glsl_imul,
7990 /* WINED3DSIH_INE */ NULL,
7991 /* WINED3DSIH_INEG */ NULL,
7992 /* WINED3DSIH_ISHL */ shader_glsl_binop,
7993 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
7994 /* WINED3DSIH_LABEL */ shader_glsl_label,
7995 /* WINED3DSIH_LD */ shader_glsl_ld,
7996 /* WINED3DSIH_LIT */ shader_glsl_lit,
7997 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
7998 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
7999 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8000 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8001 /* WINED3DSIH_LT */ shader_glsl_relop,
8002 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8003 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8004 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8005 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8006 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8007 /* WINED3DSIH_MAD */ shader_glsl_mad,
8008 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8009 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8010 /* WINED3DSIH_MOV */ shader_glsl_mov,
8011 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8012 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8013 /* WINED3DSIH_MUL */ shader_glsl_binop,
8014 /* WINED3DSIH_NE */ shader_glsl_relop,
8015 /* WINED3DSIH_NOP */ shader_glsl_nop,
8016 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8017 /* WINED3DSIH_OR */ shader_glsl_binop,
8018 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8019 /* WINED3DSIH_POW */ shader_glsl_pow,
8020 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8021 /* WINED3DSIH_REP */ shader_glsl_rep,
8022 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8023 /* WINED3DSIH_RET */ shader_glsl_ret,
8024 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8025 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
8026 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
8027 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
8028 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample_lod,
8029 /* WINED3DSIH_SETP */ NULL,
8030 /* WINED3DSIH_SGE */ shader_glsl_compare,
8031 /* WINED3DSIH_SGN */ shader_glsl_sgn,
8032 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
8033 /* WINED3DSIH_SLT */ shader_glsl_compare,
8034 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
8035 /* WINED3DSIH_SUB */ shader_glsl_binop,
8036 /* WINED3DSIH_TEX */ shader_glsl_tex,
8037 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
8038 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
8039 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
8040 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
8041 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
8042 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
8043 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
8044 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
8045 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
8046 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
8047 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
8048 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
8049 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
8050 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
8051 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
8052 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
8053 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
8054 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
8055 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
8056 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
8057 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
8058 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
8059 /* WINED3DSIH_UGE */ shader_glsl_relop,
8060 /* WINED3DSIH_USHR */ shader_glsl_binop,
8061 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
8062 /* WINED3DSIH_XOR */ shader_glsl_binop,
8065 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
8066 SHADER_HANDLER hw_fct;
8068 /* Select handler */
8069 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
8071 /* Unhandled opcode */
8072 if (!hw_fct)
8074 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
8075 return;
8077 hw_fct(ins);
8079 shader_glsl_add_instruction_modifiers(ins);
8082 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
8084 struct shader_glsl_priv *priv = shader_priv;
8086 return priv->ffp_proj_control;
8089 const struct wined3d_shader_backend_ops glsl_shader_backend =
8091 shader_glsl_handle_instruction,
8092 shader_glsl_select,
8093 shader_glsl_disable,
8094 shader_glsl_select_depth_blt,
8095 shader_glsl_deselect_depth_blt,
8096 shader_glsl_update_float_vertex_constants,
8097 shader_glsl_update_float_pixel_constants,
8098 shader_glsl_load_constants,
8099 shader_glsl_destroy,
8100 shader_glsl_alloc,
8101 shader_glsl_free,
8102 shader_glsl_allocate_context_data,
8103 shader_glsl_free_context_data,
8104 shader_glsl_init_context_state,
8105 shader_glsl_get_caps,
8106 shader_glsl_color_fixup_supported,
8107 shader_glsl_has_ffp_proj_control,
8110 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
8112 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
8114 caps->xyzrhw = TRUE;
8115 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
8116 caps->ffp_generic_attributes = TRUE;
8117 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
8118 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
8119 caps->max_vertex_blend_matrix_index = 0;
8120 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
8121 | WINED3DVTXPCAPS_MATERIALSOURCE7
8122 | WINED3DVTXPCAPS_VERTEXFOG
8123 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
8124 | WINED3DVTXPCAPS_POSITIONALLIGHTS
8125 | WINED3DVTXPCAPS_LOCALVIEWER
8126 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
8127 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
8128 caps->max_user_clip_planes = gl_info->limits.clipplanes;
8129 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
8132 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
8134 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8135 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8136 return 0;
8139 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8141 struct shader_glsl_priv *priv;
8143 if (shader_backend == &glsl_shader_backend)
8145 priv = shader_priv;
8147 if (wine_rb_init(&priv->ffp_vertex_shaders, &wined3d_ffp_vertex_program_rb_functions) == -1)
8149 ERR("Failed to initialize rbtree.\n");
8150 return NULL;
8153 return priv;
8156 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
8158 return NULL;
8161 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
8163 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8164 struct glsl_ffp_vertex_shader, desc.entry);
8165 struct glsl_shader_prog_link *program, *program2;
8166 struct glsl_ffp_destroy_ctx *ctx = context;
8168 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8169 struct glsl_shader_prog_link, vs.shader_entry)
8171 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8173 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8174 HeapFree(GetProcessHeap(), 0, shader);
8177 /* Context activation is done by the caller. */
8178 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
8180 struct shader_glsl_priv *priv = device->vertex_priv;
8181 struct glsl_ffp_destroy_ctx ctx;
8183 ctx.priv = priv;
8184 ctx.gl_info = &device->adapter->gl_info;
8185 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
8188 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
8189 const struct wined3d_state *state, DWORD state_id) {}
8191 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
8192 const struct wined3d_state *state, DWORD state_id)
8194 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8197 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
8198 const struct wined3d_state *state, DWORD state_id)
8200 const struct wined3d_gl_info *gl_info = context->gl_info;
8201 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
8202 BOOL transformed = context->stream_info.position_transformed;
8203 BOOL wasrhw = context->last_was_rhw;
8204 unsigned int i;
8206 context->last_was_rhw = transformed;
8208 /* If the vertex declaration contains a transformed position attribute,
8209 * the draw uses the fixed function vertex pipeline regardless of any
8210 * vertex shader set by the application. */
8211 if (transformed != wasrhw)
8212 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8214 if (!use_vs(state))
8216 if (context->last_was_vshader)
8218 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8219 clipplane(context, state, STATE_CLIPPLANE(i));
8222 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8224 /* Because of settings->texcoords, we have to regenerate the vertex
8225 * shader on a vdecl change if there aren't enough varyings to just
8226 * always output all the texture coordinates. */
8227 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
8228 || normal != context->last_was_normal)
8229 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8231 if (use_ps(state)
8232 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
8233 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
8234 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8236 else
8238 if (!context->last_was_vshader)
8240 /* Vertex shader clipping ignores the view matrix. Update all clipplanes. */
8241 for (i = 0; i < gl_info->limits.clipplanes; ++i)
8242 clipplane(context, state, STATE_CLIPPLANE(i));
8246 context->last_was_vshader = use_vs(state);
8247 context->last_was_normal = normal;
8250 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
8251 const struct wined3d_state *state, DWORD state_id)
8253 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8254 /* Different vertex shaders potentially require a different vertex attributes setup. */
8255 if (!isStateDirty(context, STATE_VDECL))
8256 context_apply_state(context, state, STATE_VDECL);
8259 static void glsl_vertex_pipe_world(struct wined3d_context *context,
8260 const struct wined3d_state *state, DWORD state_id)
8262 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
8265 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
8266 const struct wined3d_state *state, DWORD state_id)
8268 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8271 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
8273 const struct wined3d_gl_info *gl_info = context->gl_info;
8274 unsigned int k;
8276 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8277 | WINED3D_SHADER_CONST_FFP_LIGHTS
8278 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8280 for (k = 0; k < gl_info->limits.clipplanes; ++k)
8282 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
8283 clipplane(context, state, STATE_CLIPPLANE(k));
8287 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
8288 const struct wined3d_state *state, DWORD state_id)
8290 /* Table fog behavior depends on the projection matrix. */
8291 if (state->render_states[WINED3D_RS_FOGENABLE]
8292 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
8293 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
8294 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
8297 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
8298 const struct wined3d_state *state, DWORD state_id)
8300 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
8301 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
8302 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
8303 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
8304 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8305 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POS_FIXUP;
8308 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
8309 const struct wined3d_state *state, DWORD state_id)
8311 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8314 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
8315 const struct wined3d_state *state, DWORD state_id)
8317 DWORD sampler = state_id - STATE_SAMPLER(0);
8318 const struct wined3d_texture *texture = state->textures[sampler];
8319 BOOL np2;
8321 if (!texture)
8322 return;
8324 if (sampler >= MAX_TEXTURES)
8325 return;
8327 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
8328 || context->lastWasPow2Texture & (1u << sampler))
8330 if (np2)
8331 context->lastWasPow2Texture |= 1u << sampler;
8332 else
8333 context->lastWasPow2Texture &= ~(1u << sampler);
8335 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8339 static void glsl_vertex_pipe_material(struct wined3d_context *context,
8340 const struct wined3d_state *state, DWORD state_id)
8342 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8345 static void glsl_vertex_pipe_light(struct wined3d_context *context,
8346 const struct wined3d_state *state, DWORD state_id)
8348 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8351 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
8352 const struct wined3d_state *state, DWORD state_id)
8354 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8357 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
8358 const struct wined3d_state *state, DWORD state_id)
8360 if (!use_vs(state))
8361 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8364 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
8365 const struct wined3d_state *state, DWORD state_id)
8367 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_VERTEX;
8370 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
8372 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8373 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
8374 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
8375 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
8376 /* Clip planes */
8377 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_EXT_NONE },
8378 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_EXT_NONE },
8379 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_EXT_NONE },
8380 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_EXT_NONE },
8381 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_EXT_NONE },
8382 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_EXT_NONE },
8383 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_EXT_NONE },
8384 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_EXT_NONE },
8385 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_EXT_NONE },
8386 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_EXT_NONE },
8387 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_EXT_NONE },
8388 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_EXT_NONE },
8389 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_EXT_NONE },
8390 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_EXT_NONE },
8391 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_EXT_NONE },
8392 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_EXT_NONE },
8393 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_EXT_NONE },
8394 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_EXT_NONE },
8395 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_EXT_NONE },
8396 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_EXT_NONE },
8397 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_EXT_NONE },
8398 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_EXT_NONE },
8399 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_EXT_NONE },
8400 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_EXT_NONE },
8401 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_EXT_NONE },
8402 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_EXT_NONE },
8403 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_EXT_NONE },
8404 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_EXT_NONE },
8405 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_EXT_NONE },
8406 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_EXT_NONE },
8407 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_EXT_NONE },
8408 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_EXT_NONE },
8409 /* Lights */
8410 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8411 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8412 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8413 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8414 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8415 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8416 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8417 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8418 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8419 /* Viewport */
8420 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
8421 /* Transform states */
8422 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
8423 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
8424 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8425 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8426 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8427 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8428 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8429 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8430 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8431 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
8432 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
8433 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8434 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8435 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
8436 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8437 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8438 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8439 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8440 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8441 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8442 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8443 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
8444 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8445 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8446 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8447 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8448 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8449 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8450 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8451 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8452 /* Fog */
8453 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8454 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8455 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8456 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8457 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
8458 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
8459 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8460 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
8461 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8462 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8463 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8464 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8465 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8466 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8467 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8468 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8469 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8470 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
8471 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
8472 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_EXT_NONE },
8473 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
8474 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8475 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8476 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
8477 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
8478 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8479 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
8480 /* NP2 texture matrix fixups. They are not needed if
8481 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
8482 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
8483 * matrix. */
8484 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8485 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8486 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8487 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8488 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8489 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8490 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8491 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8492 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8493 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8494 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8495 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8496 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8497 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8498 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8499 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8500 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8501 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8502 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8503 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8504 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8505 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
8506 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
8507 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
8508 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
8509 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
8510 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
8511 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
8514 /* TODO:
8515 * - Implement vertex tweening. */
8516 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
8518 glsl_vertex_pipe_vp_enable,
8519 glsl_vertex_pipe_vp_get_caps,
8520 glsl_vertex_pipe_vp_get_emul_mask,
8521 glsl_vertex_pipe_vp_alloc,
8522 glsl_vertex_pipe_vp_free,
8523 glsl_vertex_pipe_vp_states,
8526 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
8528 /* Nothing to do. */
8531 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
8533 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
8534 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
8535 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
8536 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
8537 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
8538 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
8539 | WINED3DTEXOPCAPS_SELECTARG1
8540 | WINED3DTEXOPCAPS_SELECTARG2
8541 | WINED3DTEXOPCAPS_MODULATE4X
8542 | WINED3DTEXOPCAPS_MODULATE2X
8543 | WINED3DTEXOPCAPS_MODULATE
8544 | WINED3DTEXOPCAPS_ADDSIGNED2X
8545 | WINED3DTEXOPCAPS_ADDSIGNED
8546 | WINED3DTEXOPCAPS_ADD
8547 | WINED3DTEXOPCAPS_SUBTRACT
8548 | WINED3DTEXOPCAPS_ADDSMOOTH
8549 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
8550 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
8551 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
8552 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
8553 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
8554 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
8555 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
8556 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
8557 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
8558 | WINED3DTEXOPCAPS_DOTPRODUCT3
8559 | WINED3DTEXOPCAPS_MULTIPLYADD
8560 | WINED3DTEXOPCAPS_LERP
8561 | WINED3DTEXOPCAPS_BUMPENVMAP
8562 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
8563 caps->MaxTextureBlendStages = 8;
8564 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, 8);
8567 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
8569 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8570 return GL_EXT_EMUL_ARB_MULTITEXTURE;
8571 return 0;
8574 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
8576 struct shader_glsl_priv *priv;
8578 if (shader_backend == &glsl_shader_backend)
8580 priv = shader_priv;
8582 if (wine_rb_init(&priv->ffp_fragment_shaders, &wined3d_ffp_frag_program_rb_functions) == -1)
8584 ERR("Failed to initialize rbtree.\n");
8585 return NULL;
8588 return priv;
8591 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
8593 return NULL;
8596 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
8598 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
8599 struct glsl_ffp_fragment_shader, entry.entry);
8600 struct glsl_shader_prog_link *program, *program2;
8601 struct glsl_ffp_destroy_ctx *ctx = context;
8603 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
8604 struct glsl_shader_prog_link, ps.shader_entry)
8606 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
8608 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
8609 HeapFree(GetProcessHeap(), 0, shader);
8612 /* Context activation is done by the caller. */
8613 static void glsl_fragment_pipe_free(struct wined3d_device *device)
8615 struct shader_glsl_priv *priv = device->fragment_priv;
8616 struct glsl_ffp_destroy_ctx ctx;
8618 ctx.priv = priv;
8619 ctx.gl_info = &device->adapter->gl_info;
8620 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
8623 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
8624 const struct wined3d_state *state, DWORD state_id)
8626 context->last_was_pshader = use_ps(state);
8628 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8631 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
8632 const struct wined3d_state *state, DWORD state_id)
8634 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8637 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
8638 const struct wined3d_state *state, DWORD state_id)
8640 BOOL use_vshader = use_vs(state);
8641 enum fogsource new_source;
8642 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
8643 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
8645 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8647 if (!state->render_states[WINED3D_RS_FOGENABLE])
8648 return;
8650 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
8652 if (use_vshader)
8653 new_source = FOGSOURCE_VS;
8654 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
8655 new_source = FOGSOURCE_COORD;
8656 else
8657 new_source = FOGSOURCE_FFP;
8659 else
8661 new_source = FOGSOURCE_FFP;
8664 if (new_source != context->fog_source || fogstart == fogend)
8666 context->fog_source = new_source;
8667 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8671 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
8672 const struct wined3d_state *state, DWORD state_id)
8674 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8675 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8676 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8678 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
8679 glsl_fragment_pipe_fog(context, state, state_id);
8682 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
8683 const struct wined3d_state *state, DWORD state_id)
8685 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
8686 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
8687 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8690 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
8691 const struct wined3d_state *state, DWORD state_id)
8693 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
8696 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
8697 const struct wined3d_state *state, DWORD state_id)
8699 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8702 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
8703 const struct wined3d_state *state, DWORD state_id)
8705 const struct wined3d_gl_info *gl_info = context->gl_info;
8706 int glParm;
8707 float ref;
8709 TRACE("context %p, state %p, state_id %#x.\n", context, state, state_id);
8711 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
8713 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
8714 checkGLcall("glEnable GL_ALPHA_TEST");
8716 else
8718 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
8719 checkGLcall("glDisable GL_ALPHA_TEST");
8720 return;
8723 ref = ((float)state->render_states[WINED3D_RS_ALPHAREF]) / 255.0f;
8724 glParm = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
8726 if (glParm)
8728 gl_info->gl_ops.gl.p_glAlphaFunc(glParm, ref);
8729 checkGLcall("glAlphaFunc");
8733 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
8734 const struct wined3d_state *state, DWORD state_id)
8736 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8739 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
8740 const struct wined3d_state *state, DWORD state_id)
8742 context->shader_update_mask |= 1 << WINED3D_SHADER_TYPE_PIXEL;
8745 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
8747 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
8748 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
8749 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8750 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8751 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8752 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8753 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8754 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8755 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8756 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8757 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8758 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8759 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8760 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8761 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8762 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8763 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8764 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8765 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8766 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8767 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8768 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8769 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8770 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8771 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8772 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8773 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8774 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8775 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8776 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8777 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8778 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8779 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8780 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8781 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8782 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8783 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8784 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8785 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8786 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8787 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8788 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8789 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8790 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8791 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8792 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8793 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8794 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8795 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8796 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8797 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8798 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8799 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8800 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8801 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8802 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8803 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8804 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8805 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8806 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8807 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8808 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8809 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8810 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8811 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8812 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8813 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8814 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8815 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8816 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8817 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8818 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8819 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8820 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8821 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8822 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8823 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8824 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
8825 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_EXT_NONE },
8826 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8827 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
8828 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
8829 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8830 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
8831 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8832 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
8833 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
8834 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
8835 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8836 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
8837 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
8838 {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 },
8839 {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 },
8840 {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 },
8841 {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 },
8842 {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 },
8843 {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 },
8844 {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 },
8845 {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 },
8846 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8847 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8848 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8849 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8850 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8851 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8852 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8853 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8854 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
8855 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
8856 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
8857 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
8858 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
8861 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
8863 return TRUE;
8866 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
8870 const struct fragment_pipeline glsl_fragment_pipe =
8872 glsl_fragment_pipe_enable,
8873 glsl_fragment_pipe_get_caps,
8874 glsl_fragment_pipe_get_emul_mask,
8875 glsl_fragment_pipe_alloc,
8876 glsl_fragment_pipe_free,
8877 glsl_fragment_pipe_alloc_context_data,
8878 glsl_fragment_pipe_free_context_data,
8879 shader_glsl_color_fixup_supported,
8880 glsl_fragment_pipe_state_template,