include: Add constants for CryptProtectMemory/CryptUnprotectMemory.
[wine.git] / dlls / wined3d / glsl_shader.c
blob6f83563554e5e5cbd06d048b09e350051e6b4fbb
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_LOD 0x02
49 #define WINED3D_GLSL_SAMPLE_GRAD 0x04
50 #define WINED3D_GLSL_SAMPLE_LOAD 0x08
51 #define WINED3D_GLSL_SAMPLE_OFFSET 0x10
53 static const struct
55 unsigned int coord_size;
56 unsigned int resinfo_size;
57 const char *type_part;
59 resource_type_info[] =
61 {0, 0, ""}, /* WINED3D_SHADER_RESOURCE_NONE */
62 {1, 1, "Buffer"}, /* WINED3D_SHADER_RESOURCE_BUFFER */
63 {1, 1, "1D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1D */
64 {2, 2, "2D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2D */
65 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMS */
66 {3, 3, "3D"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_3D */
67 {3, 2, "Cube"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBE */
68 {2, 2, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY */
69 {3, 3, "2DArray"}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY */
70 {3, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_2DMSARRAY */
73 struct glsl_dst_param
75 char reg_name[150];
76 char mask_str[6];
79 struct glsl_src_param
81 char reg_name[150];
82 char param_str[200];
85 struct glsl_sample_function
87 struct wined3d_string_buffer *name;
88 unsigned int coord_mask;
89 unsigned int deriv_mask;
90 enum wined3d_data_type data_type;
91 BOOL output_single_component;
92 unsigned int offset_size;
95 enum heap_node_op
97 HEAP_NODE_TRAVERSE_LEFT,
98 HEAP_NODE_TRAVERSE_RIGHT,
99 HEAP_NODE_POP,
102 struct constant_entry
104 unsigned int idx;
105 unsigned int version;
108 struct constant_heap
110 struct constant_entry *entries;
111 BOOL *contained;
112 unsigned int *positions;
113 unsigned int size;
116 /* GLSL shader private data */
117 struct shader_glsl_priv {
118 struct wined3d_string_buffer shader_buffer;
119 struct wined3d_string_buffer_list string_buffers;
120 struct wine_rb_tree program_lookup;
121 struct constant_heap vconst_heap;
122 struct constant_heap pconst_heap;
123 unsigned char *stack;
124 UINT next_constant_version;
126 const struct wined3d_vertex_pipe_ops *vertex_pipe;
127 const struct fragment_pipeline *fragment_pipe;
128 struct wine_rb_tree ffp_vertex_shaders;
129 struct wine_rb_tree ffp_fragment_shaders;
130 BOOL ffp_proj_control;
131 BOOL legacy_lighting;
134 struct glsl_vs_program
136 struct list shader_entry;
137 GLuint id;
138 GLenum vertex_color_clamp;
139 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
140 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
141 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
142 GLint pos_fixup_location;
144 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
145 GLint projection_matrix_location;
146 GLint normal_matrix_location;
147 GLint texture_matrix_location[MAX_TEXTURES];
148 GLint material_ambient_location;
149 GLint material_diffuse_location;
150 GLint material_specular_location;
151 GLint material_emissive_location;
152 GLint material_shininess_location;
153 GLint light_ambient_location;
154 struct
156 GLint diffuse;
157 GLint specular;
158 GLint ambient;
159 GLint position;
160 GLint direction;
161 GLint range;
162 GLint falloff;
163 GLint c_att;
164 GLint l_att;
165 GLint q_att;
166 GLint cos_htheta;
167 GLint cos_hphi;
168 } light_location[MAX_ACTIVE_LIGHTS];
169 GLint pointsize_location;
170 GLint pointsize_min_location;
171 GLint pointsize_max_location;
172 GLint pointsize_c_att_location;
173 GLint pointsize_l_att_location;
174 GLint pointsize_q_att_location;
175 GLint clip_planes_location;
178 struct glsl_gs_program
180 struct list shader_entry;
181 GLuint id;
183 GLint pos_fixup_location;
186 struct glsl_ps_program
188 struct list shader_entry;
189 GLuint id;
190 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
191 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
192 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
193 GLint bumpenv_mat_location[MAX_TEXTURES];
194 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
195 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
196 GLint tss_constant_location[MAX_TEXTURES];
197 GLint tex_factor_location;
198 GLint specular_enable_location;
199 GLint fog_color_location;
200 GLint fog_density_location;
201 GLint fog_end_location;
202 GLint fog_scale_location;
203 GLint alpha_test_ref_location;
204 GLint ycorrection_location;
205 GLint np2_fixup_location;
206 GLint color_key_location;
207 const struct ps_np2fixup_info *np2_fixup_info;
210 /* Struct to maintain data about a linked GLSL program */
211 struct glsl_shader_prog_link
213 struct wine_rb_entry program_lookup_entry;
214 struct glsl_vs_program vs;
215 struct glsl_gs_program gs;
216 struct glsl_ps_program ps;
217 GLuint id;
218 DWORD constant_update_mask;
219 UINT constant_version;
222 struct glsl_program_key
224 GLuint vs_id;
225 GLuint gs_id;
226 GLuint ps_id;
229 struct shader_glsl_ctx_priv {
230 const struct vs_compile_args *cur_vs_args;
231 const struct ps_compile_args *cur_ps_args;
232 struct ps_np2fixup_info *cur_np2fixup_info;
233 struct wined3d_string_buffer_list *string_buffers;
236 struct glsl_context_data
238 struct glsl_shader_prog_link *glsl_program;
241 struct glsl_ps_compiled_shader
243 struct ps_compile_args args;
244 struct ps_np2fixup_info np2fixup;
245 GLuint id;
248 struct glsl_vs_compiled_shader
250 struct vs_compile_args args;
251 GLuint id;
254 struct glsl_gs_compiled_shader
256 struct gs_compile_args args;
257 GLuint id;
260 struct glsl_shader_private
262 union
264 struct glsl_vs_compiled_shader *vs;
265 struct glsl_gs_compiled_shader *gs;
266 struct glsl_ps_compiled_shader *ps;
267 } gl_shaders;
268 UINT num_gl_shaders, shader_array_size;
271 struct glsl_ffp_vertex_shader
273 struct wined3d_ffp_vs_desc desc;
274 GLuint id;
275 struct list linked_programs;
278 struct glsl_ffp_fragment_shader
280 struct ffp_frag_desc entry;
281 GLuint id;
282 struct list linked_programs;
285 struct glsl_ffp_destroy_ctx
287 struct shader_glsl_priv *priv;
288 const struct wined3d_gl_info *gl_info;
291 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
293 static const char *debug_gl_shader_type(GLenum type)
295 switch (type)
297 #define WINED3D_TO_STR(u) case u: return #u
298 WINED3D_TO_STR(GL_VERTEX_SHADER);
299 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
300 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
301 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
302 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
303 WINED3D_TO_STR(GL_COMPUTE_SHADER);
304 #undef WINED3D_TO_STR
305 default:
306 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
310 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
312 switch (type)
314 case WINED3D_SHADER_TYPE_VERTEX:
315 return "vs";
317 case WINED3D_SHADER_TYPE_HULL:
318 return "hs";
320 case WINED3D_SHADER_TYPE_DOMAIN:
321 return "ds";
323 case WINED3D_SHADER_TYPE_GEOMETRY:
324 return "gs";
326 case WINED3D_SHADER_TYPE_PIXEL:
327 return "ps";
329 case WINED3D_SHADER_TYPE_COMPUTE:
330 return "cs";
332 default:
333 FIXME("Unhandled shader type %#x.\n", type);
334 return "unknown";
338 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
339 const struct wined3d_shader_version *version)
341 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
342 return 150;
343 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
344 return 130;
345 else
346 return 120;
349 static const char *shader_glsl_get_version_declaration(const struct wined3d_gl_info *gl_info,
350 const struct wined3d_shader_version *version)
352 unsigned int glsl_version;
354 switch (glsl_version = shader_glsl_get_version(gl_info, version))
356 case 150:
357 return "#version 150";
358 case 130:
359 return "#version 130";
360 case 120:
361 return "#version 120";
362 default:
363 FIXME("Unexpected GLSL version %u requested.\n", glsl_version);
364 return "";
368 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
370 char str[4][17];
372 wined3d_ftoa(values[0], str[0]);
373 wined3d_ftoa(values[1], str[1]);
374 wined3d_ftoa(values[2], str[2]);
375 wined3d_ftoa(values[3], str[3]);
376 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
379 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
380 const int *values, unsigned int size)
382 int i;
384 if (!size || size > 4)
386 ERR("Invalid vector size %u.\n", size);
387 return;
390 if (size > 1)
391 shader_addline(buffer, "ivec%u(", size);
393 for (i = 0; i < size; ++i)
394 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
396 if (size > 1)
397 shader_addline(buffer, ")");
400 static const char *get_info_log_line(const char **ptr)
402 const char *p, *q;
404 p = *ptr;
405 if (!(q = strstr(p, "\n")))
407 if (!*p) return NULL;
408 *ptr += strlen(p);
409 return p;
411 *ptr = q + 1;
413 return p;
416 /* Context activation is done by the caller. */
417 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
419 int length = 0;
420 char *log;
422 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
423 return;
425 if (program)
426 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
427 else
428 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
430 /* A size of 1 is just a null-terminated string, so the log should be bigger than
431 * that if there are errors. */
432 if (length > 1)
434 const char *ptr, *line;
436 log = HeapAlloc(GetProcessHeap(), 0, length);
437 /* The info log is supposed to be zero-terminated, but at least some
438 * versions of fglrx don't terminate the string properly. The reported
439 * length does include the terminator, so explicitly set it to zero
440 * here. */
441 log[length - 1] = 0;
442 if (program)
443 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
444 else
445 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
447 ptr = log;
448 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
450 WARN("Info log received from GLSL shader #%u:\n", id);
451 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
453 else
455 FIXME("Info log received from GLSL shader #%u:\n", id);
456 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
458 HeapFree(GetProcessHeap(), 0, log);
462 /* Context activation is done by the caller. */
463 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
465 const char *ptr, *line;
467 TRACE("Compiling shader object %u.\n", shader);
469 if (TRACE_ON(d3d_shader))
471 ptr = src;
472 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
475 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
476 checkGLcall("glShaderSource");
477 GL_EXTCALL(glCompileShader(shader));
478 checkGLcall("glCompileShader");
479 print_glsl_info_log(gl_info, shader, FALSE);
482 /* Context activation is done by the caller. */
483 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
485 GLint i, shader_count, source_size = -1;
486 GLuint *shaders;
487 char *source = NULL;
489 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
490 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
492 ERR("Failed to allocate shader array memory.\n");
493 return;
496 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
497 for (i = 0; i < shader_count; ++i)
499 const char *ptr, *line;
500 GLint tmp;
502 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
504 if (source_size < tmp)
506 HeapFree(GetProcessHeap(), 0, source);
508 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
509 if (!source)
511 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
512 HeapFree(GetProcessHeap(), 0, shaders);
513 return;
515 source_size = tmp;
518 FIXME("Shader %u:\n", shaders[i]);
519 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
520 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
521 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
522 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
523 FIXME("\n");
525 ptr = source;
526 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
527 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
528 FIXME("\n");
531 HeapFree(GetProcessHeap(), 0, source);
532 HeapFree(GetProcessHeap(), 0, shaders);
535 /* Context activation is done by the caller. */
536 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
538 GLint tmp;
540 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
541 return;
543 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
544 if (!tmp)
546 FIXME("Program %u link status invalid.\n", program);
547 shader_glsl_dump_program_source(gl_info, program);
550 print_glsl_info_log(gl_info, program, TRUE);
553 /* Context activation is done by the caller. */
554 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
555 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
557 unsigned int mapped_unit;
558 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
559 const char *prefix;
560 unsigned int i, j;
561 GLint name_loc;
563 static const struct
565 enum wined3d_shader_type type;
566 unsigned int base_idx;
567 unsigned int count;
569 sampler_info[] =
571 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
572 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
575 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
577 prefix = shader_glsl_get_prefix(sampler_info[i].type);
579 for (j = 0; j < sampler_info[i].count; ++j)
581 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
582 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
583 if (name_loc == -1)
584 continue;
586 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
587 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
589 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
590 continue;
593 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
594 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
597 checkGLcall("Load sampler bindings");
598 string_buffer_release(&priv->string_buffers, sampler_name);
601 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
602 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
604 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
606 if (icb)
608 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
609 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
610 GLint icb_location;
612 string_buffer_sprintf(icb_name, "%s_icb", prefix);
613 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
614 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
615 checkGLcall("Load immediate constant buffer");
617 string_buffer_release(&priv->string_buffers, icb_name);
621 /* Context activation is done by the caller. */
622 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
623 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
625 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
626 GLint location;
627 unsigned int i;
629 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
631 if (!reg_maps->uav_resource_info[i].type)
632 continue;
634 string_buffer_sprintf(name, "ps_image%u", i);
635 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
636 if (location == -1)
637 continue;
639 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
640 GL_EXTCALL(glUniform1i(location, i));
642 checkGLcall("Load image bindings");
643 string_buffer_release(&priv->string_buffers, name);
646 /* Context activation is done by the caller. */
647 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
648 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
650 unsigned int start = ~0U, end = 0;
651 int stack_idx = 0;
652 unsigned int heap_idx = 1;
653 unsigned int idx;
655 if (heap->entries[heap_idx].version <= version) return;
657 idx = heap->entries[heap_idx].idx;
658 if (constant_locations[idx] != -1)
659 start = end = idx;
660 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
662 while (stack_idx >= 0)
664 /* Note that we fall through to the next case statement. */
665 switch(stack[stack_idx])
667 case HEAP_NODE_TRAVERSE_LEFT:
669 unsigned int left_idx = heap_idx << 1;
670 if (left_idx < heap->size && heap->entries[left_idx].version > version)
672 heap_idx = left_idx;
673 idx = heap->entries[heap_idx].idx;
674 if (constant_locations[idx] != -1)
676 if (start > idx)
677 start = idx;
678 if (end < idx)
679 end = idx;
682 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
683 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
684 break;
688 case HEAP_NODE_TRAVERSE_RIGHT:
690 unsigned int right_idx = (heap_idx << 1) + 1;
691 if (right_idx < heap->size && heap->entries[right_idx].version > version)
693 heap_idx = right_idx;
694 idx = heap->entries[heap_idx].idx;
695 if (constant_locations[idx] != -1)
697 if (start > idx)
698 start = idx;
699 if (end < idx)
700 end = idx;
703 stack[stack_idx++] = HEAP_NODE_POP;
704 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
705 break;
709 case HEAP_NODE_POP:
710 heap_idx >>= 1;
711 --stack_idx;
712 break;
715 if (start <= end)
716 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
717 checkGLcall("walk_constant_heap()");
720 /* Context activation is done by the caller. */
721 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
722 GLint location, const struct wined3d_vec4 *data)
724 GLfloat clamped_constant[4];
726 if (location == -1) return;
728 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
729 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
730 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
731 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
733 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
736 /* Context activation is done by the caller. */
737 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
738 const struct wined3d_vec4 *constants, const GLint *constant_locations,
739 const struct constant_heap *heap, unsigned char *stack, DWORD version)
741 int stack_idx = 0;
742 unsigned int heap_idx = 1;
743 unsigned int idx;
745 if (heap->entries[heap_idx].version <= version) return;
747 idx = heap->entries[heap_idx].idx;
748 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
749 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
751 while (stack_idx >= 0)
753 /* Note that we fall through to the next case statement. */
754 switch(stack[stack_idx])
756 case HEAP_NODE_TRAVERSE_LEFT:
758 unsigned int left_idx = heap_idx << 1;
759 if (left_idx < heap->size && heap->entries[left_idx].version > version)
761 heap_idx = left_idx;
762 idx = heap->entries[heap_idx].idx;
763 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
765 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
766 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
767 break;
771 case HEAP_NODE_TRAVERSE_RIGHT:
773 unsigned int right_idx = (heap_idx << 1) + 1;
774 if (right_idx < heap->size && heap->entries[right_idx].version > version)
776 heap_idx = right_idx;
777 idx = heap->entries[heap_idx].idx;
778 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
780 stack[stack_idx++] = HEAP_NODE_POP;
781 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
782 break;
786 case HEAP_NODE_POP:
787 heap_idx >>= 1;
788 --stack_idx;
789 break;
792 checkGLcall("walk_constant_heap_clamped()");
795 /* Context activation is done by the caller. */
796 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
797 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
798 unsigned char *stack, unsigned int version)
800 const struct wined3d_shader_lconst *lconst;
802 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
803 if (shader->reg_maps.shader_version.major == 1
804 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
805 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
806 else
807 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
809 if (!shader->load_local_constsF)
811 TRACE("No need to load local float constants for this shader.\n");
812 return;
815 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
816 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
818 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
820 checkGLcall("glUniform4fv()");
823 /* Context activation is done by the caller. */
824 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
825 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
827 unsigned int i;
828 struct list* ptr;
830 for (i = 0; constants_set; constants_set >>= 1, ++i)
832 if (!(constants_set & 1)) continue;
834 /* We found this uniform name in the program - go ahead and send the data */
835 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
838 /* Load immediate constants */
839 ptr = list_head(&shader->constantsI);
840 while (ptr)
842 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
843 unsigned int idx = lconst->idx;
844 const GLint *values = (const GLint *)lconst->value;
846 /* We found this uniform name in the program - go ahead and send the data */
847 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
848 ptr = list_next(&shader->constantsI, ptr);
850 checkGLcall("glUniform4iv()");
853 /* Context activation is done by the caller. */
854 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
855 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
857 unsigned int i;
858 struct list* ptr;
860 for (i = 0; constants_set; constants_set >>= 1, ++i)
862 if (!(constants_set & 1)) continue;
864 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
867 /* Load immediate constants */
868 ptr = list_head(&shader->constantsB);
869 while (ptr)
871 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
872 unsigned int idx = lconst->idx;
873 const GLint *values = (const GLint *)lconst->value;
875 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
876 ptr = list_next(&shader->constantsB, ptr);
878 checkGLcall("glUniform1iv()");
881 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
883 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
886 /* Context activation is done by the caller (state handler). */
887 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
888 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
890 struct
892 float sx, sy;
894 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
895 UINT fixup = ps->np2_fixup_info->active;
896 UINT i;
898 for (i = 0; fixup; fixup >>= 1, ++i)
900 const struct wined3d_texture *tex = state->textures[i];
901 unsigned char idx = ps->np2_fixup_info->idx[i];
903 if (!tex)
905 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
906 continue;
909 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
910 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
913 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
916 /* Taken and adapted from Mesa. */
917 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
919 float pos, neg, t, det;
920 struct wined3d_matrix temp;
922 /* Calculate the determinant of upper left 3x3 submatrix and
923 * determine if the matrix is singular. */
924 pos = neg = 0.0f;
925 t = in->_11 * in->_22 * in->_33;
926 if (t >= 0.0f)
927 pos += t;
928 else
929 neg += t;
931 t = in->_21 * in->_32 * in->_13;
932 if (t >= 0.0f)
933 pos += t;
934 else
935 neg += t;
936 t = in->_31 * in->_12 * in->_23;
937 if (t >= 0.0f)
938 pos += t;
939 else
940 neg += t;
942 t = -in->_31 * in->_22 * in->_13;
943 if (t >= 0.0f)
944 pos += t;
945 else
946 neg += t;
947 t = -in->_21 * in->_12 * in->_33;
948 if (t >= 0.0f)
949 pos += t;
950 else
951 neg += t;
953 t = -in->_11 * in->_32 * in->_23;
954 if (t >= 0.0f)
955 pos += t;
956 else
957 neg += t;
959 det = pos + neg;
961 if (fabsf(det) < 1e-25f)
962 return FALSE;
964 det = 1.0f / det;
965 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
966 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
967 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
968 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
969 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
970 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
971 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
972 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
973 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
975 *out = temp;
976 return TRUE;
979 static void swap_rows(float **a, float **b)
981 float *tmp = *a;
983 *a = *b;
984 *b = tmp;
987 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
989 float wtmp[4][8];
990 float m0, m1, m2, m3, s;
991 float *r0, *r1, *r2, *r3;
993 r0 = wtmp[0];
994 r1 = wtmp[1];
995 r2 = wtmp[2];
996 r3 = wtmp[3];
998 r0[0] = m->_11;
999 r0[1] = m->_12;
1000 r0[2] = m->_13;
1001 r0[3] = m->_14;
1002 r0[4] = 1.0f;
1003 r0[5] = r0[6] = r0[7] = 0.0f;
1005 r1[0] = m->_21;
1006 r1[1] = m->_22;
1007 r1[2] = m->_23;
1008 r1[3] = m->_24;
1009 r1[5] = 1.0f;
1010 r1[4] = r1[6] = r1[7] = 0.0f;
1012 r2[0] = m->_31;
1013 r2[1] = m->_32;
1014 r2[2] = m->_33;
1015 r2[3] = m->_34;
1016 r2[6] = 1.0f;
1017 r2[4] = r2[5] = r2[7] = 0.0f;
1019 r3[0] = m->_41;
1020 r3[1] = m->_42;
1021 r3[2] = m->_43;
1022 r3[3] = m->_44;
1023 r3[7] = 1.0f;
1024 r3[4] = r3[5] = r3[6] = 0.0f;
1026 /* Choose pivot - or die. */
1027 if (fabsf(r3[0]) > fabsf(r2[0]))
1028 swap_rows(&r3, &r2);
1029 if (fabsf(r2[0]) > fabsf(r1[0]))
1030 swap_rows(&r2, &r1);
1031 if (fabsf(r1[0]) > fabsf(r0[0]))
1032 swap_rows(&r1, &r0);
1033 if (r0[0] == 0.0f)
1034 return FALSE;
1036 /* Eliminate first variable. */
1037 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1038 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1039 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1040 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1041 s = r0[4];
1042 if (s != 0.0f)
1044 r1[4] -= m1 * s;
1045 r2[4] -= m2 * s;
1046 r3[4] -= m3 * s;
1048 s = r0[5];
1049 if (s != 0.0f)
1051 r1[5] -= m1 * s;
1052 r2[5] -= m2 * s;
1053 r3[5] -= m3 * s;
1055 s = r0[6];
1056 if (s != 0.0f)
1058 r1[6] -= m1 * s;
1059 r2[6] -= m2 * s;
1060 r3[6] -= m3 * s;
1062 s = r0[7];
1063 if (s != 0.0f)
1065 r1[7] -= m1 * s;
1066 r2[7] -= m2 * s;
1067 r3[7] -= m3 * s;
1070 /* Choose pivot - or die. */
1071 if (fabsf(r3[1]) > fabsf(r2[1]))
1072 swap_rows(&r3, &r2);
1073 if (fabsf(r2[1]) > fabsf(r1[1]))
1074 swap_rows(&r2, &r1);
1075 if (r1[1] == 0.0f)
1076 return FALSE;
1078 /* Eliminate second variable. */
1079 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1080 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1081 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1082 s = r1[4];
1083 if (s != 0.0f)
1085 r2[4] -= m2 * s;
1086 r3[4] -= m3 * s;
1088 s = r1[5];
1089 if (s != 0.0f)
1091 r2[5] -= m2 * s;
1092 r3[5] -= m3 * s;
1094 s = r1[6];
1095 if (s != 0.0f)
1097 r2[6] -= m2 * s;
1098 r3[6] -= m3 * s;
1100 s = r1[7];
1101 if (s != 0.0f)
1103 r2[7] -= m2 * s;
1104 r3[7] -= m3 * s;
1107 /* Choose pivot - or die. */
1108 if (fabsf(r3[2]) > fabsf(r2[2]))
1109 swap_rows(&r3, &r2);
1110 if (r2[2] == 0.0f)
1111 return FALSE;
1113 /* Eliminate third variable. */
1114 m3 = r3[2] / r2[2];
1115 r3[3] -= m3 * r2[3];
1116 r3[4] -= m3 * r2[4];
1117 r3[5] -= m3 * r2[5];
1118 r3[6] -= m3 * r2[6];
1119 r3[7] -= m3 * r2[7];
1121 /* Last check. */
1122 if (r3[3] == 0.0f)
1123 return FALSE;
1125 /* Back substitute row 3. */
1126 s = 1.0f / r3[3];
1127 r3[4] *= s;
1128 r3[5] *= s;
1129 r3[6] *= s;
1130 r3[7] *= s;
1132 /* Back substitute row 2. */
1133 m2 = r2[3];
1134 s = 1.0f / r2[2];
1135 r2[4] = s * (r2[4] - r3[4] * m2);
1136 r2[5] = s * (r2[5] - r3[5] * m2);
1137 r2[6] = s * (r2[6] - r3[6] * m2);
1138 r2[7] = s * (r2[7] - r3[7] * m2);
1139 m1 = r1[3];
1140 r1[4] -= r3[4] * m1;
1141 r1[5] -= r3[5] * m1;
1142 r1[6] -= r3[6] * m1;
1143 r1[7] -= r3[7] * m1;
1144 m0 = r0[3];
1145 r0[4] -= r3[4] * m0;
1146 r0[5] -= r3[5] * m0;
1147 r0[6] -= r3[6] * m0;
1148 r0[7] -= r3[7] * m0;
1150 /* Back substitute row 1. */
1151 m1 = r1[2];
1152 s = 1.0f / r1[1];
1153 r1[4] = s * (r1[4] - r2[4] * m1);
1154 r1[5] = s * (r1[5] - r2[5] * m1);
1155 r1[6] = s * (r1[6] - r2[6] * m1);
1156 r1[7] = s * (r1[7] - r2[7] * m1);
1157 m0 = r0[2];
1158 r0[4] -= r2[4] * m0;
1159 r0[5] -= r2[5] * m0;
1160 r0[6] -= r2[6] * m0;
1161 r0[7] -= r2[7] * m0;
1163 /* Back substitute row 0. */
1164 m0 = r0[1];
1165 s = 1.0f / r0[0];
1166 r0[4] = s * (r0[4] - r1[4] * m0);
1167 r0[5] = s * (r0[5] - r1[5] * m0);
1168 r0[6] = s * (r0[6] - r1[6] * m0);
1169 r0[7] = s * (r0[7] - r1[7] * m0);
1171 out->_11 = r0[4];
1172 out->_12 = r0[5];
1173 out->_13 = r0[6];
1174 out->_14 = r0[7];
1175 out->_21 = r1[4];
1176 out->_22 = r1[5];
1177 out->_23 = r1[6];
1178 out->_24 = r1[7];
1179 out->_31 = r2[4];
1180 out->_32 = r2[5];
1181 out->_33 = r2[6];
1182 out->_34 = r2[7];
1183 out->_41 = r3[4];
1184 out->_42 = r3[5];
1185 out->_43 = r3[6];
1186 out->_44 = r3[7];
1188 return TRUE;
1191 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1192 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1194 const struct wined3d_gl_info *gl_info = context->gl_info;
1195 float mat[3 * 3];
1196 struct wined3d_matrix mv;
1197 unsigned int i, j;
1199 if (prog->vs.normal_matrix_location == -1)
1200 return;
1202 get_modelview_matrix(context, state, 0, &mv);
1203 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1204 invert_matrix_3d(&mv, &mv);
1205 else
1206 invert_matrix(&mv, &mv);
1207 /* Tests show that singular modelview matrices are used unchanged as normal
1208 * matrices on D3D3 and older. There seems to be no clearly consistent
1209 * behavior on newer D3D versions so always follow older ddraw behavior. */
1210 for (i = 0; i < 3; ++i)
1211 for (j = 0; j < 3; ++j)
1212 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1214 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1215 checkGLcall("glUniformMatrix3fv");
1218 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1219 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1221 const struct wined3d_gl_info *gl_info = context->gl_info;
1222 struct wined3d_matrix mat;
1224 if (tex >= MAX_TEXTURES)
1225 return;
1226 if (prog->vs.texture_matrix_location[tex] == -1)
1227 return;
1229 get_texture_matrix(context, state, tex, &mat);
1230 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1231 checkGLcall("glUniformMatrix4fv");
1234 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1235 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1237 const struct wined3d_gl_info *gl_info = context->gl_info;
1239 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1241 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1242 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1244 else
1246 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1248 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1250 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1251 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1252 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1253 checkGLcall("setting FFP material uniforms");
1256 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1257 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1259 const struct wined3d_gl_info *gl_info = context->gl_info;
1260 struct wined3d_color color;
1262 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1263 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1264 checkGLcall("glUniform3fv");
1267 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1268 const struct wined3d_matrix *src2)
1270 struct wined3d_vec4 temp;
1272 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1273 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1274 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1275 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1277 *dest = temp;
1280 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1281 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1282 struct glsl_shader_prog_link *prog)
1284 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1285 const struct wined3d_gl_info *gl_info = context->gl_info;
1286 struct wined3d_vec4 vec4;
1288 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1289 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1290 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1292 switch (light_info->OriginalParms.type)
1294 case WINED3D_LIGHT_POINT:
1295 multiply_vector_matrix(&vec4, &light_info->position, view);
1296 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1297 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1298 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1299 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1300 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1301 break;
1303 case WINED3D_LIGHT_SPOT:
1304 multiply_vector_matrix(&vec4, &light_info->position, view);
1305 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1307 multiply_vector_matrix(&vec4, &light_info->direction, view);
1308 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1310 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1311 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1312 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1313 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1314 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1315 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1316 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1317 break;
1319 case WINED3D_LIGHT_DIRECTIONAL:
1320 multiply_vector_matrix(&vec4, &light_info->direction, view);
1321 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1322 break;
1324 case WINED3D_LIGHT_PARALLELPOINT:
1325 multiply_vector_matrix(&vec4, &light_info->position, view);
1326 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1327 break;
1329 default:
1330 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1332 checkGLcall("setting FFP lights uniforms");
1335 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1336 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1338 const struct wined3d_gl_info *gl_info = context->gl_info;
1339 float min, max;
1340 float size, att[3];
1342 get_pointsize_minmax(context, state, &min, &max);
1344 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1345 checkGLcall("glUniform1f");
1346 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1347 checkGLcall("glUniform1f");
1349 get_pointsize(context, state, &size, att);
1351 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1352 checkGLcall("glUniform1f");
1353 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1354 checkGLcall("glUniform1f");
1355 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1356 checkGLcall("glUniform1f");
1357 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1358 checkGLcall("glUniform1f");
1361 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1362 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1364 const struct wined3d_gl_info *gl_info = context->gl_info;
1365 struct wined3d_color color;
1366 float start, end, scale;
1367 union
1369 DWORD d;
1370 float f;
1371 } tmpvalue;
1373 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1374 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1375 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1376 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1377 get_fog_start_end(context, state, &start, &end);
1378 scale = 1.0f / (end - start);
1379 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1380 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1381 checkGLcall("fog emulation uniforms");
1384 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1385 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1387 const struct wined3d_gl_info *gl_info = context->gl_info;
1388 struct wined3d_vec4 plane;
1390 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1391 if (!use_vs(state))
1392 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1393 else
1394 plane = state->clip_planes[index];
1396 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1399 /* Context activation is done by the caller (state handler). */
1400 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1401 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1403 struct wined3d_color float_key[2];
1404 const struct wined3d_texture *texture = state->textures[0];
1406 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1407 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1410 /* Context activation is done by the caller (state handler). */
1411 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1412 const struct wined3d_state *state)
1414 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1415 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1416 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1417 const struct wined3d_gl_info *gl_info = context->gl_info;
1418 struct shader_glsl_priv *priv = shader_priv;
1419 float position_fixup[4];
1420 DWORD update_mask;
1422 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1423 UINT constant_version;
1424 int i;
1426 if (!prog) {
1427 /* No GLSL program set - nothing to do. */
1428 return;
1430 constant_version = prog->constant_version;
1431 update_mask = context->constant_update_mask & prog->constant_update_mask;
1433 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1434 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1435 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1437 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1438 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1439 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1441 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1442 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1443 vshader->reg_maps.boolean_constants);
1445 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1447 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1448 shader_glsl_clip_plane_uniform(context, state, i, prog);
1451 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1452 shader_glsl_pointsize_uniform(context, state, prog);
1454 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1456 shader_get_position_fixup(context, state, position_fixup);
1457 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1458 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1459 else
1460 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1461 checkGLcall("glUniform4fv");
1464 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1466 struct wined3d_matrix mat;
1468 get_modelview_matrix(context, state, 0, &mat);
1469 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1470 checkGLcall("glUniformMatrix4fv");
1472 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1475 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1477 struct wined3d_matrix mat;
1479 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1481 if (prog->vs.modelview_matrix_location[i] == -1)
1482 break;
1484 get_modelview_matrix(context, state, i, &mat);
1485 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1486 checkGLcall("glUniformMatrix4fv");
1490 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1492 struct wined3d_matrix projection;
1494 get_projection_matrix(context, state, &projection);
1495 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1496 checkGLcall("glUniformMatrix4fv");
1499 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1501 for (i = 0; i < MAX_TEXTURES; ++i)
1502 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1505 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1506 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1508 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1510 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1511 DWORD point_count = 0;
1512 DWORD spot_count = 0;
1513 DWORD directional_count = 0;
1514 DWORD parallel_point_count = 0;
1516 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1518 if (!state->lights[i])
1519 continue;
1521 switch (state->lights[i]->OriginalParms.type)
1523 case WINED3D_LIGHT_POINT:
1524 ++point_count;
1525 break;
1526 case WINED3D_LIGHT_SPOT:
1527 ++spot_count;
1528 break;
1529 case WINED3D_LIGHT_DIRECTIONAL:
1530 ++directional_count;
1531 break;
1532 case WINED3D_LIGHT_PARALLELPOINT:
1533 ++parallel_point_count;
1534 break;
1535 default:
1536 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1537 break;
1540 point_idx = 0;
1541 spot_idx = point_idx + point_count;
1542 directional_idx = spot_idx + spot_count;
1543 parallel_point_idx = directional_idx + directional_count;
1545 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1546 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1548 const struct wined3d_light_info *light_info = state->lights[i];
1549 unsigned int idx;
1551 if (!light_info)
1552 continue;
1554 switch (light_info->OriginalParms.type)
1556 case WINED3D_LIGHT_POINT:
1557 idx = point_idx++;
1558 break;
1559 case WINED3D_LIGHT_SPOT:
1560 idx = spot_idx++;
1561 break;
1562 case WINED3D_LIGHT_DIRECTIONAL:
1563 idx = directional_idx++;
1564 break;
1565 case WINED3D_LIGHT_PARALLELPOINT:
1566 idx = parallel_point_idx++;
1567 break;
1568 default:
1569 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1570 continue;
1572 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1576 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1577 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1578 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1580 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1581 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1582 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1584 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1585 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1586 pshader->reg_maps.boolean_constants);
1588 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1590 for (i = 0; i < MAX_TEXTURES; ++i)
1592 if (prog->ps.bumpenv_mat_location[i] == -1)
1593 continue;
1595 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1596 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1598 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1600 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1601 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1602 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1603 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1607 checkGLcall("bump env uniforms");
1610 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1612 const struct wined3d_vec4 correction_params =
1614 /* Position is relative to the framebuffer, not the viewport. */
1615 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1616 context->render_offscreen ? 1.0f : -1.0f,
1617 0.0f,
1618 0.0f,
1621 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1624 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1625 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1626 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1627 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1629 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1631 struct wined3d_color color;
1633 if (prog->ps.tex_factor_location != -1)
1635 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1636 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1639 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1640 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1641 else
1642 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1644 for (i = 0; i < MAX_TEXTURES; ++i)
1646 if (prog->ps.tss_constant_location[i] == -1)
1647 continue;
1649 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1650 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1653 checkGLcall("fixed function uniforms");
1656 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1657 shader_glsl_load_fog_uniform(context, state, prog);
1659 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1661 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1663 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1664 checkGLcall("alpha test emulation uniform");
1667 if (priv->next_constant_version == UINT_MAX)
1669 TRACE("Max constant version reached, resetting to 0.\n");
1670 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1671 priv->next_constant_version = 1;
1673 else
1675 prog->constant_version = priv->next_constant_version++;
1679 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1681 struct constant_entry *entries = heap->entries;
1682 unsigned int *positions = heap->positions;
1683 unsigned int heap_idx, parent_idx;
1685 if (!heap->contained[idx])
1687 heap_idx = heap->size++;
1688 heap->contained[idx] = TRUE;
1690 else
1692 heap_idx = positions[idx];
1695 while (heap_idx > 1)
1697 parent_idx = heap_idx >> 1;
1699 if (new_version <= entries[parent_idx].version) break;
1701 entries[heap_idx] = entries[parent_idx];
1702 positions[entries[parent_idx].idx] = heap_idx;
1703 heap_idx = parent_idx;
1706 entries[heap_idx].version = new_version;
1707 entries[heap_idx].idx = idx;
1708 positions[idx] = heap_idx;
1711 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1713 struct shader_glsl_priv *priv = device->shader_priv;
1714 struct constant_heap *heap = &priv->vconst_heap;
1715 UINT i;
1717 for (i = start; i < count + start; ++i)
1719 update_heap_entry(heap, i, priv->next_constant_version);
1723 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1725 struct shader_glsl_priv *priv = device->shader_priv;
1726 struct constant_heap *heap = &priv->pconst_heap;
1727 UINT i;
1729 for (i = start; i < count + start; ++i)
1731 update_heap_entry(heap, i, priv->next_constant_version);
1735 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1737 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1738 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1739 if(shader_major > 3) return ret;
1741 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1742 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1743 return ret;
1746 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1748 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1751 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1753 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1756 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1758 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1761 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1762 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1764 va_list args;
1765 int ret;
1767 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1768 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1769 for (;;)
1771 va_start(args, format);
1772 ret = shader_vaddline(buffer, format, args);
1773 va_end(args);
1774 if (!ret)
1775 return;
1776 if (!string_buffer_resize(buffer, ret))
1777 return;
1781 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1782 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1784 va_list args;
1785 int ret;
1787 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1788 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1789 for (;;)
1791 va_start(args, format);
1792 ret = shader_vaddline(buffer, format, args);
1793 va_end(args);
1794 if (!ret)
1795 return;
1796 if (!string_buffer_resize(buffer, ret))
1797 return;
1801 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1803 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1806 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1808 switch (primitive_type)
1810 case WINED3D_PT_POINTLIST:
1811 return "points";
1813 case WINED3D_PT_LINELIST:
1814 return "lines";
1816 case WINED3D_PT_LINESTRIP:
1817 return "line_strip";
1819 case WINED3D_PT_TRIANGLELIST:
1820 return "triangles";
1822 case WINED3D_PT_TRIANGLESTRIP:
1823 return "triangle_strip";
1825 case WINED3D_PT_LINELIST_ADJ:
1826 return "lines_adjacency";
1828 case WINED3D_PT_TRIANGLELIST_ADJ:
1829 return "triangles_adjacency";
1831 default:
1832 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1833 return "";
1837 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1839 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1840 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1841 const BOOL *input_reg_used = shader->u.ps.input_reg_used;
1842 unsigned int i;
1844 if (reg_maps->shader_version.major < 3)
1845 return input_reg_used[idx];
1847 for (i = 0; i < input_signature->element_count; ++i)
1849 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1851 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1852 continue;
1854 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1855 && input->semantic_idx == idx)
1857 if (input_reg_used[input->register_idx])
1858 return TRUE;
1859 else
1860 return FALSE;
1863 return FALSE;
1866 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1867 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1869 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1871 if (version->major >= 4)
1872 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1873 else
1874 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1877 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1878 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1879 unsigned int index)
1881 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1882 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1883 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1884 index, scalar_type, scalar_type, index);
1887 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1888 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1890 unsigned int index = e->register_idx;
1892 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1894 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1895 index);
1896 return;
1898 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1900 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1901 index);
1902 return;
1904 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1905 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1907 if (shader_glsl_use_explicit_attrib_location(gl_info))
1908 shader_addline(buffer, "layout(location = %u) ", index);
1910 switch (e->component_type)
1912 case WINED3D_TYPE_UINT:
1913 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1914 break;
1915 case WINED3D_TYPE_INT:
1916 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1917 break;
1919 default:
1920 FIXME("Unhandled type %#x.\n", e->component_type);
1921 /* Fall through. */
1922 case WINED3D_TYPE_UNKNOWN:
1923 case WINED3D_TYPE_FLOAT:
1924 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1925 break;
1929 /** Generate the variable & register declarations for the GLSL output target */
1930 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1931 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1932 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1934 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1935 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1936 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1937 const struct wined3d_gl_info *gl_info = context->gl_info;
1938 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1939 unsigned int i, extra_constants_needed = 0;
1940 const struct wined3d_shader_lconst *lconst;
1941 const char *prefix;
1942 DWORD map;
1944 prefix = shader_glsl_get_prefix(version->type);
1946 /* Prototype the subroutines */
1947 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1949 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1952 /* Declare the constants (aka uniforms) */
1953 if (shader->limits->constant_float > 0)
1955 unsigned max_constantsF;
1957 /* Unless the shader uses indirect addressing, always declare the
1958 * maximum array size and ignore that we need some uniforms privately.
1959 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1960 * and immediate values, still declare VC[256]. If the shader needs
1961 * more uniforms than we have it won't work in any case. If it uses
1962 * less, the compiler will figure out which uniforms are really used
1963 * and strip them out. This allows a shader to use c255 on a dx9 card,
1964 * as long as it doesn't also use all the other constants.
1966 * If the shader uses indirect addressing the compiler must assume
1967 * that all declared uniforms are used. In this case, declare only the
1968 * amount that we're assured to have.
1970 * Thus we run into problems in these two cases:
1971 * 1) The shader really uses more uniforms than supported.
1972 * 2) The shader uses indirect addressing, less constants than
1973 * supported, but uses a constant index > #supported consts. */
1974 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1976 /* No indirect addressing here. */
1977 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1979 else
1981 if (reg_maps->usesrelconstF)
1983 /* Subtract the other potential uniforms from the max
1984 * available (bools, ints, and 1 row of projection matrix).
1985 * Subtract another uniform for immediate values, which have
1986 * to be loaded via uniform by the driver as well. The shader
1987 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
1988 * shader code, so one vec4 should be enough. (Unfortunately
1989 * the Nvidia driver doesn't store 128 and -128 in one float).
1991 * Writing gl_ClipVertex requires one uniform for each
1992 * clipplane as well. */
1993 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
1994 if (vs_args->clip_enabled)
1995 max_constantsF -= gl_info->limits.user_clip_distances;
1996 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
1997 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
1998 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
1999 * for now take this into account when calculating the number of available constants
2001 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2002 /* Set by driver quirks in directx.c */
2003 max_constantsF -= gl_info->reserved_glsl_constants;
2005 if (max_constantsF < shader->limits->constant_float)
2007 static unsigned int once;
2009 if (!once++)
2010 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2011 " it may not render correctly.\n");
2012 else
2013 WARN("The hardware does not support enough uniform components to run this shader.\n");
2016 else
2018 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2021 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2022 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2025 /* Always declare the full set of constants, the compiler can remove the
2026 * unused ones because d3d doesn't (yet) support indirect int and bool
2027 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2028 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2029 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2031 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2032 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2034 /* Declare immediate constant buffer */
2035 if (reg_maps->icb)
2036 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2038 /* Declare constant buffers */
2039 for (i = 0; i < WINED3D_MAX_CBS; ++i)
2041 if (reg_maps->cb_sizes[i])
2042 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2043 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2046 /* Declare texture samplers */
2047 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2049 struct wined3d_shader_sampler_map_entry *entry;
2050 const char *sampler_type_prefix, *sampler_type;
2051 BOOL shadow_sampler, tex_rect;
2053 entry = &reg_maps->sampler_map.entries[i];
2055 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2057 ERR("Invalid resource index %u.\n", entry->resource_idx);
2058 continue;
2061 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2063 case WINED3D_DATA_FLOAT:
2064 case WINED3D_DATA_UNORM:
2065 case WINED3D_DATA_SNORM:
2066 sampler_type_prefix = "";
2067 break;
2069 case WINED3D_DATA_INT:
2070 sampler_type_prefix = "i";
2071 break;
2073 case WINED3D_DATA_UINT:
2074 sampler_type_prefix = "u";
2075 break;
2077 default:
2078 sampler_type_prefix = "";
2079 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2080 break;
2083 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2084 switch (reg_maps->resource_info[entry->resource_idx].type)
2086 case WINED3D_SHADER_RESOURCE_BUFFER:
2087 sampler_type = "samplerBuffer";
2088 break;
2090 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2091 if (shadow_sampler)
2092 sampler_type = "sampler1DShadow";
2093 else
2094 sampler_type = "sampler1D";
2095 break;
2097 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2098 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2099 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2100 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2101 if (shadow_sampler)
2103 if (tex_rect)
2104 sampler_type = "sampler2DRectShadow";
2105 else
2106 sampler_type = "sampler2DShadow";
2108 else
2110 if (tex_rect)
2111 sampler_type = "sampler2DRect";
2112 else
2113 sampler_type = "sampler2D";
2115 break;
2117 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2118 if (shadow_sampler)
2119 FIXME("Unsupported 3D shadow sampler.\n");
2120 sampler_type = "sampler3D";
2121 break;
2123 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2124 if (shadow_sampler)
2125 FIXME("Unsupported Cube shadow sampler.\n");
2126 sampler_type = "samplerCube";
2127 break;
2129 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2130 if (shadow_sampler)
2131 sampler_type = "sampler2DArrayShadow";
2132 else
2133 sampler_type = "sampler2DArray";
2134 break;
2136 default:
2137 sampler_type = "unsupported_sampler";
2138 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2139 break;
2141 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2142 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2145 /* Declare images */
2146 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2148 const char *image_type_prefix, *image_type, *read_format;
2150 if (!reg_maps->uav_resource_info[i].type)
2151 continue;
2153 switch (reg_maps->uav_resource_info[i].data_type)
2155 case WINED3D_DATA_FLOAT:
2156 case WINED3D_DATA_UNORM:
2157 case WINED3D_DATA_SNORM:
2158 image_type_prefix = "";
2159 read_format = "r32f";
2160 break;
2162 case WINED3D_DATA_INT:
2163 image_type_prefix = "i";
2164 read_format = "r32i";
2165 break;
2167 case WINED3D_DATA_UINT:
2168 image_type_prefix = "u";
2169 read_format = "r32ui";
2170 break;
2172 default:
2173 image_type_prefix = "";
2174 read_format = "";
2175 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2176 break;
2179 switch (reg_maps->uav_resource_info[i].type)
2181 case WINED3D_SHADER_RESOURCE_BUFFER:
2182 image_type = "imageBuffer";
2183 break;
2185 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2186 image_type = "image2D";
2187 break;
2189 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2190 image_type = "image3D";
2191 break;
2193 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2194 image_type = "image2DArray";
2195 break;
2197 default:
2198 image_type = "unsupported_image";
2199 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2200 break;
2203 if (reg_maps->uav_read_mask & (1u << i))
2204 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2205 read_format, image_type_prefix, image_type, prefix, i);
2206 else
2207 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2208 image_type_prefix, image_type, prefix, i);
2211 /* Declare uniforms for NP2 texcoord fixup:
2212 * This is NOT done inside the loop that declares the texture samplers
2213 * since the NP2 fixup code is currently only used for the GeforceFX
2214 * series and when forcing the ARB_npot extension off. Modern cards just
2215 * skip the code anyway, so put it inside a separate loop. */
2216 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2218 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2219 UINT cur = 0;
2221 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2222 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2223 * samplerNP2Fixup stores texture dimensions and is updated through
2224 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2226 for (i = 0; i < shader->limits->sampler; ++i)
2228 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2229 continue;
2231 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2233 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2234 continue;
2237 fixup->idx[i] = cur++;
2240 fixup->num_consts = (cur + 1) >> 1;
2241 fixup->active = ps_args->np2_fixup;
2242 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2245 /* Declare address variables */
2246 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2248 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2251 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2253 for (i = 0; i < shader->input_signature.element_count; ++i)
2254 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2256 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2258 shader_addline(buffer, "uniform struct\n{\n");
2259 shader_addline(buffer, " float size;\n");
2260 shader_addline(buffer, " float size_min;\n");
2261 shader_addline(buffer, " float size_max;\n");
2262 shader_addline(buffer, "} ffp_point;\n");
2265 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2267 if (vs_args->clip_enabled)
2268 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2270 if (version->major < 3)
2272 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2273 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2274 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2275 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2279 if (version->major < 4)
2280 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2282 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2284 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2286 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2288 else
2290 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2291 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2292 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2293 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2296 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2298 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2300 shader_addline(buffer, "uniform struct\n{\n");
2301 shader_addline(buffer, " vec4 color;\n");
2302 shader_addline(buffer, " float density;\n");
2303 shader_addline(buffer, " float end;\n");
2304 shader_addline(buffer, " float scale;\n");
2305 shader_addline(buffer, "} ffp_fog;\n");
2307 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2309 if (glsl_is_color_reg_read(shader, 0))
2310 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2311 if (glsl_is_color_reg_read(shader, 1))
2312 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2313 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2314 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2316 else
2318 if (glsl_is_color_reg_read(shader, 0))
2319 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2320 if (glsl_is_color_reg_read(shader, 1))
2321 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2322 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2323 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2324 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2328 if (version->major >= 3)
2330 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2332 if (ps_args->vp_mode == vertexshader)
2333 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2334 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2337 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2339 if (!(map & 1))
2340 continue;
2342 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2344 if (reg_maps->luminanceparams & (1u << i))
2346 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2347 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2348 extra_constants_needed++;
2351 extra_constants_needed++;
2354 if (ps_args->srgb_correction)
2356 shader_addline(buffer, "const vec4 srgb_const0 = ");
2357 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2358 shader_addline(buffer, ";\n");
2359 shader_addline(buffer, "const vec4 srgb_const1 = ");
2360 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2361 shader_addline(buffer, ";\n");
2363 if (reg_maps->vpos || reg_maps->usesdsy)
2365 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2367 ++extra_constants_needed;
2368 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2370 if (reg_maps->vpos)
2372 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2374 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2375 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2376 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2377 else if (!ps_args->render_offscreen)
2378 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2380 shader_addline(buffer, "vec4 vpos;\n");
2384 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2385 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2387 if (!needs_legacy_glsl_syntax(gl_info))
2388 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2390 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2391 FIXME("Insufficient uniforms to run this shader.\n");
2394 /* Declare output register temporaries */
2395 if (shader->limits->packed_output)
2396 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2398 /* Declare temporary variables */
2399 if (reg_maps->temporary_count)
2401 for (i = 0; i < reg_maps->temporary_count; ++i)
2402 shader_addline(buffer, "vec4 R%u;\n", i);
2404 else
2406 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2408 if (map & 1)
2409 shader_addline(buffer, "vec4 R%u;\n", i);
2413 /* Declare indexable temporary variables */
2414 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2416 if (idx_temp_reg->component_count != 4)
2417 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2418 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2421 /* Declare loop registers aLx */
2422 if (version->major < 4)
2424 for (i = 0; i < reg_maps->loop_depth; ++i)
2426 shader_addline(buffer, "int aL%u;\n", i);
2427 shader_addline(buffer, "int tmpInt%u;\n", i);
2431 /* Temporary variables for matrix operations */
2432 shader_addline(buffer, "vec4 tmp0;\n");
2433 shader_addline(buffer, "vec4 tmp1;\n");
2435 if (!shader->load_local_constsF)
2437 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2439 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2440 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2441 shader_addline(buffer, ";\n");
2446 /*****************************************************************************
2447 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2449 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2450 ****************************************************************************/
2452 /* Prototypes */
2453 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2454 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2456 /** Used for opcode modifiers - They multiply the result by the specified amount */
2457 static const char * const shift_glsl_tab[] = {
2458 "", /* 0 (none) */
2459 "2.0 * ", /* 1 (x2) */
2460 "4.0 * ", /* 2 (x4) */
2461 "8.0 * ", /* 3 (x8) */
2462 "16.0 * ", /* 4 (x16) */
2463 "32.0 * ", /* 5 (x32) */
2464 "", /* 6 (x64) */
2465 "", /* 7 (x128) */
2466 "", /* 8 (d256) */
2467 "", /* 9 (d128) */
2468 "", /* 10 (d64) */
2469 "", /* 11 (d32) */
2470 "0.0625 * ", /* 12 (d16) */
2471 "0.125 * ", /* 13 (d8) */
2472 "0.25 * ", /* 14 (d4) */
2473 "0.5 * " /* 15 (d2) */
2476 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2477 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2478 const char *in_reg, const char *in_regswizzle, char *out_str)
2480 switch (src_modifier)
2482 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2483 case WINED3DSPSM_DW:
2484 case WINED3DSPSM_NONE:
2485 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2486 break;
2487 case WINED3DSPSM_NEG:
2488 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2489 break;
2490 case WINED3DSPSM_NOT:
2491 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2492 break;
2493 case WINED3DSPSM_BIAS:
2494 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2495 break;
2496 case WINED3DSPSM_BIASNEG:
2497 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2498 break;
2499 case WINED3DSPSM_SIGN:
2500 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2501 break;
2502 case WINED3DSPSM_SIGNNEG:
2503 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2504 break;
2505 case WINED3DSPSM_COMP:
2506 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2507 break;
2508 case WINED3DSPSM_X2:
2509 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2510 break;
2511 case WINED3DSPSM_X2NEG:
2512 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2513 break;
2514 case WINED3DSPSM_ABS:
2515 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2516 break;
2517 case WINED3DSPSM_ABSNEG:
2518 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2519 break;
2520 default:
2521 FIXME("Unhandled modifier %u\n", src_modifier);
2522 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2526 /** Writes the GLSL variable name that corresponds to the register that the
2527 * DX opcode parameter is trying to access */
2528 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2529 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2531 /* oPos, oFog and oPts in D3D */
2532 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2534 const struct wined3d_shader *shader = ins->ctx->shader;
2535 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2536 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2537 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2538 const char *prefix = shader_glsl_get_prefix(version->type);
2539 struct glsl_src_param rel_param0, rel_param1;
2540 char imm_str[4][17];
2542 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2543 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2544 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2545 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2546 *is_color = FALSE;
2548 switch (reg->type)
2550 case WINED3DSPR_TEMP:
2551 sprintf(register_name, "R%u", reg->idx[0].offset);
2552 break;
2554 case WINED3DSPR_INPUT:
2555 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2557 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2559 if (reg->idx[0].rel_addr)
2560 FIXME("VS3+ input registers relative addressing.\n");
2561 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2562 *is_color = TRUE;
2563 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2564 break;
2567 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2569 if (reg->idx[0].rel_addr)
2571 if (reg->idx[1].rel_addr)
2572 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2573 rel_param0.param_str, reg->idx[0].offset,
2574 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2575 rel_param1.param_str, reg->idx[1].offset);
2576 else
2577 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2578 rel_param0.param_str, reg->idx[0].offset,
2579 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2580 reg->idx[1].offset);
2582 else if (reg->idx[1].rel_addr)
2583 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2584 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2585 rel_param1.param_str, reg->idx[1].offset);
2586 else
2587 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2588 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2589 reg->idx[1].offset);
2590 break;
2593 /* pixel shaders >= 3.0 */
2594 if (version->major >= 3)
2596 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2597 unsigned int in_count = vec4_varyings(version->major, gl_info);
2599 if (reg->idx[0].rel_addr)
2601 /* Removing a + 0 would be an obvious optimization, but
2602 * OS X doesn't see the NOP operation there. */
2603 if (idx)
2605 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2606 && shader->u.ps.declared_in_count > in_count)
2608 sprintf(register_name,
2609 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2610 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2611 prefix, rel_param0.param_str, idx);
2613 else
2615 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2618 else
2620 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2621 && shader->u.ps.declared_in_count > in_count)
2623 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2624 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2625 prefix, rel_param0.param_str);
2627 else
2629 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2633 else
2635 if (idx == in_count) sprintf(register_name, "gl_Color");
2636 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2637 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2640 else
2642 if (!reg->idx[0].offset)
2643 strcpy(register_name, "ffp_varying_diffuse");
2644 else
2645 strcpy(register_name, "ffp_varying_specular");
2646 break;
2648 break;
2650 case WINED3DSPR_CONST:
2652 /* Relative addressing */
2653 if (reg->idx[0].rel_addr)
2655 if (wined3d_settings.check_float_constants)
2656 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2657 rel_param0.param_str, reg->idx[0].offset,
2658 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2659 prefix, rel_param0.param_str, reg->idx[0].offset);
2660 else if (reg->idx[0].offset)
2661 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2662 else
2663 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2665 else
2667 if (shader_constant_is_local(shader, reg->idx[0].offset))
2668 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2669 else
2670 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2673 break;
2675 case WINED3DSPR_CONSTINT:
2676 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2677 break;
2679 case WINED3DSPR_CONSTBOOL:
2680 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2681 break;
2683 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2684 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2685 sprintf(register_name, "T%u", reg->idx[0].offset);
2686 else
2687 sprintf(register_name, "A%u", reg->idx[0].offset);
2688 break;
2690 case WINED3DSPR_LOOP:
2691 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2692 break;
2694 case WINED3DSPR_SAMPLER:
2695 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2696 break;
2698 case WINED3DSPR_COLOROUT:
2699 if (reg->idx[0].offset >= gl_info->limits.buffers)
2700 WARN("Write to render target %u, only %d supported.\n",
2701 reg->idx[0].offset, gl_info->limits.buffers);
2703 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2704 break;
2706 case WINED3DSPR_RASTOUT:
2707 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2708 break;
2710 case WINED3DSPR_DEPTHOUT:
2711 sprintf(register_name, "gl_FragDepth");
2712 break;
2714 case WINED3DSPR_ATTROUT:
2715 if (!reg->idx[0].offset)
2716 sprintf(register_name, "%s_out[8]", prefix);
2717 else
2718 sprintf(register_name, "%s_out[9]", prefix);
2719 break;
2721 case WINED3DSPR_TEXCRDOUT:
2722 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2723 if (reg->idx[0].rel_addr)
2724 FIXME("VS3 output registers relative addressing.\n");
2725 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2726 break;
2728 case WINED3DSPR_MISCTYPE:
2729 if (!reg->idx[0].offset)
2731 /* vPos */
2732 sprintf(register_name, "vpos");
2734 else if (reg->idx[0].offset == 1)
2736 /* Note that gl_FrontFacing is a bool, while vFace is
2737 * a float for which the sign determines front/back */
2738 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2740 else
2742 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2743 sprintf(register_name, "unrecognized_register");
2745 break;
2747 case WINED3DSPR_IMMCONST:
2748 switch (reg->immconst_type)
2750 case WINED3D_IMMCONST_SCALAR:
2751 switch (reg->data_type)
2753 case WINED3D_DATA_FLOAT:
2754 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2755 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2756 else
2757 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2758 break;
2759 case WINED3D_DATA_INT:
2760 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2761 break;
2762 case WINED3D_DATA_RESOURCE:
2763 case WINED3D_DATA_SAMPLER:
2764 case WINED3D_DATA_UINT:
2765 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2766 break;
2767 default:
2768 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2769 break;
2771 break;
2773 case WINED3D_IMMCONST_VEC4:
2774 switch (reg->data_type)
2776 case WINED3D_DATA_FLOAT:
2777 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2779 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2780 reg->u.immconst_data[0], reg->u.immconst_data[1],
2781 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2783 else
2785 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2786 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2787 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2788 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2789 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2790 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2792 break;
2793 case WINED3D_DATA_INT:
2794 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2795 reg->u.immconst_data[0], reg->u.immconst_data[1],
2796 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2797 break;
2798 case WINED3D_DATA_RESOURCE:
2799 case WINED3D_DATA_SAMPLER:
2800 case WINED3D_DATA_UINT:
2801 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2802 reg->u.immconst_data[0], reg->u.immconst_data[1],
2803 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2804 break;
2805 default:
2806 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2807 break;
2809 break;
2811 default:
2812 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2813 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2815 break;
2817 case WINED3DSPR_CONSTBUFFER:
2818 if (reg->idx[1].rel_addr)
2819 sprintf(register_name, "%s_cb%u[%s + %u]",
2820 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2821 else
2822 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2823 break;
2825 case WINED3DSPR_IMMCONSTBUFFER:
2826 if (reg->idx[0].rel_addr)
2827 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2828 else
2829 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2830 break;
2832 case WINED3DSPR_PRIMID:
2833 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2834 break;
2836 case WINED3DSPR_IDXTEMP:
2837 if (reg->idx[1].rel_addr)
2838 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2839 else
2840 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2841 break;
2843 default:
2844 FIXME("Unhandled register type %#x.\n", reg->type);
2845 sprintf(register_name, "unrecognized_register");
2846 break;
2850 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2852 *str++ = '.';
2853 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2854 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2855 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2856 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2857 *str = '\0';
2860 /* Get the GLSL write mask for the destination register */
2861 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2863 DWORD mask = param->write_mask;
2865 if (shader_is_scalar(&param->reg))
2867 mask = WINED3DSP_WRITEMASK_0;
2868 *write_mask = '\0';
2870 else
2872 shader_glsl_write_mask_to_str(mask, write_mask);
2875 return mask;
2878 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2879 unsigned int size = 0;
2881 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2882 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2883 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2884 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2886 return size;
2889 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2891 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2892 * but addressed as "rgba". To fix this we need to swap the register's x
2893 * and z components. */
2894 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2896 *str++ = '.';
2897 /* swizzle bits fields: wwzzyyxx */
2898 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2899 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2900 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2901 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2902 *str = '\0';
2905 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2906 BOOL fixup, DWORD mask, char *swizzle_str)
2908 if (shader_is_scalar(&param->reg))
2909 *swizzle_str = '\0';
2910 else
2911 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2914 /* From a given parameter token, generate the corresponding GLSL string.
2915 * Also, return the actual register name and swizzle in case the
2916 * caller needs this information as well. */
2917 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
2918 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2919 enum wined3d_data_type data_type)
2921 BOOL is_color = FALSE;
2922 char swizzle_str[6];
2924 glsl_src->reg_name[0] = '\0';
2925 glsl_src->param_str[0] = '\0';
2926 swizzle_str[0] = '\0';
2928 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2929 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2931 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
2933 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2935 else
2937 char reg_name[200];
2939 switch (data_type)
2941 case WINED3D_DATA_FLOAT:
2942 sprintf(reg_name, "%s", glsl_src->reg_name);
2943 break;
2944 case WINED3D_DATA_INT:
2945 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2946 break;
2947 case WINED3D_DATA_RESOURCE:
2948 case WINED3D_DATA_SAMPLER:
2949 case WINED3D_DATA_UINT:
2950 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2951 break;
2952 default:
2953 FIXME("Unhandled data type %#x.\n", data_type);
2954 sprintf(reg_name, "%s", glsl_src->reg_name);
2955 break;
2958 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
2962 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2963 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
2965 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
2968 /* From a given parameter token, generate the corresponding GLSL string.
2969 * Also, return the actual register name and swizzle in case the
2970 * caller needs this information as well. */
2971 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
2972 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
2974 BOOL is_color = FALSE;
2976 glsl_dst->mask_str[0] = '\0';
2977 glsl_dst->reg_name[0] = '\0';
2979 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
2980 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
2983 /* Append the destination part of the instruction to the buffer, return the effective write mask */
2984 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
2985 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
2986 enum wined3d_data_type data_type)
2988 struct glsl_dst_param glsl_dst;
2989 DWORD mask;
2991 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
2993 switch (data_type)
2995 case WINED3D_DATA_FLOAT:
2996 shader_addline(buffer, "%s%s = %s(",
2997 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
2998 break;
2999 case WINED3D_DATA_INT:
3000 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3001 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3002 break;
3003 case WINED3D_DATA_RESOURCE:
3004 case WINED3D_DATA_SAMPLER:
3005 case WINED3D_DATA_UINT:
3006 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3007 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3008 break;
3009 default:
3010 FIXME("Unhandled data type %#x.\n", data_type);
3011 shader_addline(buffer, "%s%s = %s(",
3012 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3013 break;
3017 return mask;
3020 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3021 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3023 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3026 /** Process GLSL instruction modifiers */
3027 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3029 struct glsl_dst_param dst_param;
3030 DWORD modifiers;
3032 if (!ins->dst_count) return;
3034 modifiers = ins->dst[0].modifiers;
3035 if (!modifiers) return;
3037 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3039 if (modifiers & WINED3DSPDM_SATURATE)
3041 /* _SAT means to clamp the value of the register to between 0 and 1 */
3042 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3043 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3046 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3048 FIXME("_centroid modifier not handled\n");
3051 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3053 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3057 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3059 switch (op)
3061 case WINED3D_SHADER_REL_OP_GT: return ">";
3062 case WINED3D_SHADER_REL_OP_EQ: return "==";
3063 case WINED3D_SHADER_REL_OP_GE: return ">=";
3064 case WINED3D_SHADER_REL_OP_LT: return "<";
3065 case WINED3D_SHADER_REL_OP_NE: return "!=";
3066 case WINED3D_SHADER_REL_OP_LE: return "<=";
3067 default:
3068 FIXME("Unrecognized operator %#x.\n", op);
3069 return "(\?\?)";
3073 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3074 const struct wined3d_shader_version *version)
3076 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3079 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3080 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3082 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3083 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3084 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3085 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3086 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3087 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3088 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3089 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3090 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3091 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3092 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3093 const char *base = "texture", *type_part = "", *suffix = "";
3094 unsigned int coord_size, deriv_size;
3095 BOOL array;
3097 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3099 if (resource_type >= ARRAY_SIZE(resource_type_info))
3101 ERR("Unexpected resource type %#x.\n", resource_type);
3102 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3104 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3105 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3107 /* Note that there's no such thing as a projected cube texture. */
3108 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3109 projected = FALSE;
3111 if (needs_legacy_glsl_syntax(gl_info))
3113 if (shadow)
3114 base = "shadow";
3116 type_part = resource_type_info[resource_type].type_part;
3117 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3118 type_part = "2DRect";
3119 if (!type_part[0])
3120 FIXME("Unhandled resource type %#x.\n", resource_type);
3122 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3124 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3125 suffix = "ARB";
3126 else
3127 FIXME("Unsupported grad function.\n");
3131 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3133 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3134 if (flags & ~texel_fetch_flags)
3135 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3137 base = "texelFetch";
3138 type_part = "";
3141 sample_function->name = string_buffer_get(priv->string_buffers);
3142 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3143 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3145 coord_size = resource_type_info[resource_type].coord_size;
3146 deriv_size = coord_size;
3147 if (shadow)
3148 ++coord_size;
3149 if (array)
3150 --deriv_size;
3151 sample_function->offset_size = offset ? deriv_size : 0;
3152 sample_function->coord_mask = (1u << coord_size) - 1;
3153 sample_function->deriv_mask = (1u << deriv_size) - 1;
3154 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3157 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3158 struct glsl_sample_function *sample_function)
3160 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3162 string_buffer_release(priv->string_buffers, sample_function->name);
3165 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3166 BOOL sign_fixup, enum fixup_channel_source channel_source)
3168 switch(channel_source)
3170 case CHANNEL_SOURCE_ZERO:
3171 strcat(arguments, "0.0");
3172 break;
3174 case CHANNEL_SOURCE_ONE:
3175 strcat(arguments, "1.0");
3176 break;
3178 case CHANNEL_SOURCE_X:
3179 strcat(arguments, reg_name);
3180 strcat(arguments, ".x");
3181 break;
3183 case CHANNEL_SOURCE_Y:
3184 strcat(arguments, reg_name);
3185 strcat(arguments, ".y");
3186 break;
3188 case CHANNEL_SOURCE_Z:
3189 strcat(arguments, reg_name);
3190 strcat(arguments, ".z");
3191 break;
3193 case CHANNEL_SOURCE_W:
3194 strcat(arguments, reg_name);
3195 strcat(arguments, ".w");
3196 break;
3198 default:
3199 FIXME("Unhandled channel source %#x\n", channel_source);
3200 strcat(arguments, "undefined");
3201 break;
3204 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3207 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3208 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3210 unsigned int mask_size, remaining;
3211 DWORD fixup_mask = 0;
3212 char arguments[256];
3213 char mask_str[6];
3215 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3216 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3217 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3218 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3219 if (!(mask &= fixup_mask))
3220 return;
3222 if (is_complex_fixup(fixup))
3224 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3225 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3226 return;
3229 shader_glsl_write_mask_to_str(mask, mask_str);
3230 mask_size = shader_glsl_get_write_mask_size(mask);
3232 arguments[0] = '\0';
3233 remaining = mask_size;
3234 if (mask & WINED3DSP_WRITEMASK_0)
3236 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3237 if (--remaining) strcat(arguments, ", ");
3239 if (mask & WINED3DSP_WRITEMASK_1)
3241 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3242 if (--remaining) strcat(arguments, ", ");
3244 if (mask & WINED3DSP_WRITEMASK_2)
3246 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3247 if (--remaining) strcat(arguments, ", ");
3249 if (mask & WINED3DSP_WRITEMASK_3)
3251 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3252 if (--remaining) strcat(arguments, ", ");
3255 if (mask_size > 1)
3256 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3257 else
3258 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3261 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3263 char reg_name[256];
3264 BOOL is_color;
3266 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3267 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3270 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3271 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3272 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3273 const char *coord_reg_fmt, ...)
3275 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3276 char dst_swizzle[6];
3277 struct color_fixup_desc fixup;
3278 BOOL np2_fixup = FALSE;
3279 va_list args;
3280 int ret;
3282 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3284 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3285 * We actually rely on it for vertex shaders and SM4+. */
3286 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3288 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3289 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3291 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3292 np2_fixup = TRUE;
3294 else
3296 fixup = COLOR_FIXUP_IDENTITY;
3299 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3301 if (sample_function->output_single_component)
3302 shader_addline(ins->ctx->buffer, "vec4(");
3304 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3305 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3307 for (;;)
3309 va_start(args, coord_reg_fmt);
3310 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3311 va_end(args);
3312 if (!ret)
3313 break;
3314 if (!string_buffer_resize(ins->ctx->buffer, ret))
3315 break;
3318 if (np2_fixup)
3320 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3321 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3323 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3325 case 1:
3326 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3327 idx >> 1, (idx % 2) ? "z" : "x");
3328 break;
3329 case 2:
3330 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3331 idx >> 1, (idx % 2) ? "zw" : "xy");
3332 break;
3333 case 3:
3334 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3335 idx >> 1, (idx % 2) ? "zw" : "xy");
3336 break;
3337 case 4:
3338 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3339 idx >> 1, (idx % 2) ? "zw" : "xy");
3340 break;
3343 if (dx && dy)
3344 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3345 else if (bias)
3346 shader_addline(ins->ctx->buffer, ", %s", bias);
3347 if (sample_function->offset_size)
3349 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3350 shader_addline(ins->ctx->buffer, ", ");
3351 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3353 shader_addline(ins->ctx->buffer, ")");
3355 if (sample_function->output_single_component)
3356 shader_addline(ins->ctx->buffer, ")");
3358 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3360 if (!is_identity_fixup(fixup))
3361 shader_glsl_color_correction(ins, fixup);
3364 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3366 /* Write the final position.
3368 * OpenGL coordinates specify the center of the pixel while D3D coords
3369 * specify the corner. The offsets are stored in z and w in
3370 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3371 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3372 * a MAD. */
3373 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3374 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3376 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3377 * in utils.c
3379 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3380 * shaders are run before the homogeneous divide, so we have to take the w
3381 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3382 * z = z * 2 - w. */
3383 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3386 /*****************************************************************************
3387 * Begin processing individual instruction opcodes
3388 ****************************************************************************/
3390 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3392 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3393 struct glsl_src_param src0_param;
3394 struct glsl_src_param src1_param;
3395 DWORD write_mask;
3396 const char *op;
3398 /* Determine the GLSL operator to use based on the opcode */
3399 switch (ins->handler_idx)
3401 case WINED3DSIH_ADD: op = "+"; break;
3402 case WINED3DSIH_AND: op = "&"; break;
3403 case WINED3DSIH_DIV: op = "/"; break;
3404 case WINED3DSIH_IADD: op = "+"; break;
3405 case WINED3DSIH_ISHL: op = "<<"; break;
3406 case WINED3DSIH_ISHR: op = ">>"; break;
3407 case WINED3DSIH_MUL: op = "*"; break;
3408 case WINED3DSIH_OR: op = "|"; break;
3409 case WINED3DSIH_SUB: op = "-"; break;
3410 case WINED3DSIH_USHR: op = ">>"; break;
3411 case WINED3DSIH_XOR: op = "^"; break;
3412 default:
3413 op = "<unhandled operator>";
3414 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3415 break;
3418 write_mask = shader_glsl_append_dst(buffer, ins);
3419 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3420 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3421 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3424 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3426 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3427 struct glsl_src_param src0_param;
3428 struct glsl_src_param src1_param;
3429 unsigned int mask_size;
3430 DWORD write_mask;
3431 const char *op;
3433 write_mask = shader_glsl_append_dst(buffer, ins);
3434 mask_size = shader_glsl_get_write_mask_size(write_mask);
3435 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3436 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3438 if (mask_size > 1)
3440 switch (ins->handler_idx)
3442 case WINED3DSIH_EQ: op = "equal"; break;
3443 case WINED3DSIH_IEQ: op = "equal"; break;
3444 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3445 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3446 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3447 case WINED3DSIH_LT: op = "lessThan"; break;
3448 case WINED3DSIH_ILT: op = "lessThan"; break;
3449 case WINED3DSIH_ULT: op = "lessThan"; break;
3450 case WINED3DSIH_NE: op = "notEqual"; break;
3451 case WINED3DSIH_INE: op = "notEqual"; break;
3452 default:
3453 op = "<unhandled operator>";
3454 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3455 break;
3458 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3459 mask_size, op, src0_param.param_str, src1_param.param_str);
3461 else
3463 switch (ins->handler_idx)
3465 case WINED3DSIH_EQ: op = "=="; break;
3466 case WINED3DSIH_IEQ: op = "=="; break;
3467 case WINED3DSIH_GE: op = ">="; break;
3468 case WINED3DSIH_IGE: op = ">="; break;
3469 case WINED3DSIH_UGE: op = ">="; break;
3470 case WINED3DSIH_LT: op = "<"; break;
3471 case WINED3DSIH_ILT: op = "<"; break;
3472 case WINED3DSIH_ULT: op = "<"; break;
3473 case WINED3DSIH_NE: op = "!="; break;
3474 case WINED3DSIH_INE: op = "!="; break;
3475 default:
3476 op = "<unhandled operator>";
3477 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3478 break;
3481 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3482 src0_param.param_str, op, src1_param.param_str);
3486 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3488 struct glsl_src_param src_param;
3489 DWORD write_mask;
3490 const char *op;
3492 switch (ins->handler_idx)
3494 case WINED3DSIH_INEG: op = "-"; break;
3495 case WINED3DSIH_NOT: op = "~"; break;
3496 default:
3497 op = "<unhandled operator>";
3498 ERR("Unhandled opcode %s.\n",
3499 debug_d3dshaderinstructionhandler(ins->handler_idx));
3500 break;
3503 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3504 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3505 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3508 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3510 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3511 struct glsl_src_param src0_param;
3512 struct glsl_src_param src1_param;
3513 DWORD write_mask;
3515 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3516 * not, we can emulate it. */
3517 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3518 FIXME("64-bit integer multiplies not implemented.\n");
3520 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3522 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3523 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3524 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3526 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3527 src0_param.param_str, src1_param.param_str);
3531 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3533 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3534 struct glsl_src_param src0_param, src1_param;
3535 DWORD write_mask;
3537 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3539 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3541 char dst_mask[6];
3543 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3544 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3545 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3546 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3547 dst_mask, src0_param.param_str, src1_param.param_str);
3549 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3550 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3551 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3552 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3554 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3555 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3557 else
3559 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3560 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3561 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3562 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3565 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3567 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3568 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3569 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3570 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3574 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3575 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3577 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3578 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3579 struct glsl_src_param src0_param;
3580 DWORD write_mask;
3582 write_mask = shader_glsl_append_dst(buffer, ins);
3583 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3585 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3586 * shader versions WINED3DSIO_MOVA is used for this. */
3587 if (ins->ctx->reg_maps->shader_version.major == 1
3588 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3589 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3591 /* This is a simple floor() */
3592 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3593 if (mask_size > 1) {
3594 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3595 } else {
3596 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3599 else if (ins->handler_idx == WINED3DSIH_MOVA)
3601 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3602 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3604 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3606 if (mask_size > 1)
3607 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3608 else
3609 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3611 else
3613 if (mask_size > 1)
3614 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3615 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3616 else
3617 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3618 src0_param.param_str, src0_param.param_str);
3621 else
3623 shader_addline(buffer, "%s);\n", src0_param.param_str);
3627 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3628 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3630 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3631 struct glsl_src_param src0_param;
3632 struct glsl_src_param src1_param;
3633 DWORD dst_write_mask, src_write_mask;
3634 unsigned int dst_size;
3636 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3637 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3639 /* dp4 works on vec4, dp3 on vec3, etc. */
3640 if (ins->handler_idx == WINED3DSIH_DP4)
3641 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3642 else if (ins->handler_idx == WINED3DSIH_DP3)
3643 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3644 else
3645 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3647 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3648 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3650 if (dst_size > 1) {
3651 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3652 } else {
3653 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3657 /* Note that this instruction has some restrictions. The destination write mask
3658 * can't contain the w component, and the source swizzles have to be .xyzw */
3659 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3661 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3662 struct glsl_src_param src0_param;
3663 struct glsl_src_param src1_param;
3664 char dst_mask[6];
3666 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3667 shader_glsl_append_dst(ins->ctx->buffer, ins);
3668 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3669 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3670 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3673 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3675 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3677 if (!stream)
3678 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3679 else
3680 FIXME("Unhandled primitive stream %u.\n", stream);
3683 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3684 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3685 * GLSL uses the value as-is. */
3686 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3688 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3689 struct glsl_src_param src0_param;
3690 struct glsl_src_param src1_param;
3691 DWORD dst_write_mask;
3692 unsigned int dst_size;
3694 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3695 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3697 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3698 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3700 if (dst_size > 1)
3702 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3703 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3705 else
3707 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3708 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3712 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3713 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3715 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3716 struct glsl_src_param src_param;
3717 const char *instruction;
3718 DWORD write_mask;
3719 unsigned i;
3721 /* Determine the GLSL function to use based on the opcode */
3722 /* TODO: Possibly make this a table for faster lookups */
3723 switch (ins->handler_idx)
3725 case WINED3DSIH_ABS: instruction = "abs"; break;
3726 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3727 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3728 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3729 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3730 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3731 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3732 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3733 case WINED3DSIH_FRC: instruction = "fract"; break;
3734 case WINED3DSIH_IMAX: instruction = "max"; break;
3735 case WINED3DSIH_IMIN: instruction = "min"; break;
3736 case WINED3DSIH_MAX: instruction = "max"; break;
3737 case WINED3DSIH_MIN: instruction = "min"; break;
3738 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3739 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3740 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3741 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3742 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3743 case WINED3DSIH_UMAX: instruction = "max"; break;
3744 case WINED3DSIH_UMIN: instruction = "min"; break;
3745 default: instruction = "";
3746 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3747 break;
3750 write_mask = shader_glsl_append_dst(buffer, ins);
3752 shader_addline(buffer, "%s(", instruction);
3754 if (ins->src_count)
3756 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3757 shader_addline(buffer, "%s", src_param.param_str);
3758 for (i = 1; i < ins->src_count; ++i)
3760 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3761 shader_addline(buffer, ", %s", src_param.param_str);
3765 shader_addline(buffer, "));\n");
3768 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3770 struct wined3d_shader_dst_param dst;
3771 struct glsl_src_param src;
3772 DWORD write_mask;
3773 const char *fmt;
3774 unsigned int i;
3776 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3777 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3779 dst = ins->dst[0];
3780 for (i = 0; i < 4; ++i)
3782 write_mask = WINED3DSP_WRITEMASK_0 << i;
3783 dst.write_mask = ins->dst[0].write_mask & write_mask;
3785 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3786 &dst, dst.reg.data_type)))
3787 continue;
3789 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3790 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3794 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3796 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3798 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3799 struct glsl_src_param src_param;
3800 unsigned int mask_size;
3801 DWORD write_mask;
3802 char dst_mask[6];
3804 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3805 mask_size = shader_glsl_get_write_mask_size(write_mask);
3806 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3808 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3809 src_param.param_str, src_param.param_str);
3810 shader_glsl_append_dst(buffer, ins);
3812 if (mask_size > 1)
3814 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3815 mask_size, src_param.param_str);
3817 else
3819 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3820 src_param.param_str);
3824 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3826 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3827 ins->ctx->reg_maps->shader_version.minor);
3828 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3829 struct glsl_src_param src0_param;
3830 const char *prefix, *suffix;
3831 unsigned int dst_size;
3832 DWORD dst_write_mask;
3834 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3835 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3837 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3838 dst_write_mask = WINED3DSP_WRITEMASK_3;
3840 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3842 switch (ins->handler_idx)
3844 case WINED3DSIH_EXP:
3845 case WINED3DSIH_EXPP:
3846 prefix = "exp2(";
3847 suffix = ")";
3848 break;
3850 case WINED3DSIH_LOG:
3851 case WINED3DSIH_LOGP:
3852 prefix = "log2(abs(";
3853 suffix = "))";
3854 break;
3856 case WINED3DSIH_RCP:
3857 prefix = "1.0 / ";
3858 suffix = "";
3859 break;
3861 case WINED3DSIH_RSQ:
3862 prefix = "inversesqrt(abs(";
3863 suffix = "))";
3864 break;
3866 default:
3867 prefix = "";
3868 suffix = "";
3869 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3870 break;
3873 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3874 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3875 else
3876 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3879 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3880 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3881 * dst.x = 2^(floor(src))
3882 * dst.y = src - floor(src)
3883 * dst.z = 2^src (partial precision is allowed, but optional)
3884 * dst.w = 1.0;
3885 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3886 * dst = 2^src; (partial precision is allowed, but optional)
3888 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3890 if (ins->ctx->reg_maps->shader_version.major < 2)
3892 struct glsl_src_param src_param;
3893 char dst_mask[6];
3895 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3897 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3898 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3899 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3900 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3902 shader_glsl_append_dst(ins->ctx->buffer, ins);
3903 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3904 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3905 return;
3908 shader_glsl_scalar_op(ins);
3911 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
3912 const char *vector_constructor, const char *scalar_constructor)
3914 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3915 struct glsl_src_param src_param;
3916 unsigned int mask_size;
3917 DWORD write_mask;
3919 write_mask = shader_glsl_append_dst(buffer, ins);
3920 mask_size = shader_glsl_get_write_mask_size(write_mask);
3921 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3923 if (mask_size > 1)
3924 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
3925 else
3926 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
3929 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
3931 shader_glsl_cast(ins, "ivec", "int");
3934 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
3936 shader_glsl_cast(ins, "uvec", "uint");
3939 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
3941 shader_glsl_cast(ins, "vec", "float");
3944 /** Process signed comparison opcodes in GLSL. */
3945 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
3947 struct glsl_src_param src0_param;
3948 struct glsl_src_param src1_param;
3949 DWORD write_mask;
3950 unsigned int mask_size;
3952 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3953 mask_size = shader_glsl_get_write_mask_size(write_mask);
3954 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3955 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3957 if (mask_size > 1) {
3958 const char *compare;
3960 switch(ins->handler_idx)
3962 case WINED3DSIH_SLT: compare = "lessThan"; break;
3963 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
3964 default: compare = "";
3965 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3968 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
3969 src0_param.param_str, src1_param.param_str);
3970 } else {
3971 switch(ins->handler_idx)
3973 case WINED3DSIH_SLT:
3974 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
3975 * to return 0.0 but step returns 1.0 because step is not < x
3976 * An alternative is a bvec compare padded with an unused second component.
3977 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
3978 * issue. Playing with not() is not possible either because not() does not accept
3979 * a scalar.
3981 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
3982 src0_param.param_str, src1_param.param_str);
3983 break;
3984 case WINED3DSIH_SGE:
3985 /* Here we can use the step() function and safe a conditional */
3986 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
3987 break;
3988 default:
3989 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3995 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
3997 const char *condition_prefix, *condition_suffix;
3998 struct wined3d_shader_dst_param dst;
3999 struct glsl_src_param src0_param;
4000 struct glsl_src_param src1_param;
4001 struct glsl_src_param src2_param;
4002 BOOL temp_destination = FALSE;
4003 DWORD cmp_channel = 0;
4004 unsigned int i, j;
4005 char mask_char[6];
4006 DWORD write_mask;
4008 switch (ins->handler_idx)
4010 case WINED3DSIH_CMP:
4011 condition_prefix = "";
4012 condition_suffix = " >= 0.0";
4013 break;
4015 case WINED3DSIH_CND:
4016 condition_prefix = "";
4017 condition_suffix = " > 0.5";
4018 break;
4020 case WINED3DSIH_MOVC:
4021 condition_prefix = "bool(";
4022 condition_suffix = ")";
4023 break;
4025 default:
4026 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4027 condition_prefix = "<unhandled prefix>";
4028 condition_suffix = "<unhandled suffix>";
4029 break;
4032 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4034 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4035 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4036 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4037 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4039 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4040 condition_prefix, src0_param.param_str, condition_suffix,
4041 src1_param.param_str, src2_param.param_str);
4042 return;
4045 dst = ins->dst[0];
4047 /* Splitting the instruction up in multiple lines imposes a problem:
4048 * The first lines may overwrite source parameters of the following lines.
4049 * Deal with that by using a temporary destination register if needed. */
4050 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4051 && ins->src[0].reg.type == dst.reg.type)
4052 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4053 && ins->src[1].reg.type == dst.reg.type)
4054 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4055 && ins->src[2].reg.type == dst.reg.type))
4056 temp_destination = TRUE;
4058 /* Cycle through all source0 channels. */
4059 for (i = 0; i < 4; ++i)
4061 write_mask = 0;
4062 /* Find the destination channels which use the current source0 channel. */
4063 for (j = 0; j < 4; ++j)
4065 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
4067 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4068 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4071 dst.write_mask = ins->dst[0].write_mask & write_mask;
4073 if (temp_destination)
4075 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4076 continue;
4077 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4079 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4080 continue;
4082 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4083 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4084 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4086 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4087 condition_prefix, src0_param.param_str, condition_suffix,
4088 src1_param.param_str, src2_param.param_str);
4091 if (temp_destination)
4093 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4094 shader_glsl_append_dst(ins->ctx->buffer, ins);
4095 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4099 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4100 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4101 * the compare is done per component of src0. */
4102 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4104 struct glsl_src_param src0_param;
4105 struct glsl_src_param src1_param;
4106 struct glsl_src_param src2_param;
4107 DWORD write_mask;
4108 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4109 ins->ctx->reg_maps->shader_version.minor);
4111 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4113 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4114 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4115 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4116 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4118 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4119 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4120 else
4121 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4122 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4123 return;
4126 shader_glsl_conditional_move(ins);
4129 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4130 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4132 struct glsl_src_param src0_param;
4133 struct glsl_src_param src1_param;
4134 struct glsl_src_param src2_param;
4135 DWORD write_mask;
4137 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4138 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4139 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4140 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4141 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4142 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4145 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4146 Vertex shaders to GLSL codes */
4147 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4149 int i;
4150 int nComponents = 0;
4151 struct wined3d_shader_dst_param tmp_dst = {{0}};
4152 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4153 struct wined3d_shader_instruction tmp_ins;
4155 memset(&tmp_ins, 0, sizeof(tmp_ins));
4157 /* Set constants for the temporary argument */
4158 tmp_ins.ctx = ins->ctx;
4159 tmp_ins.dst_count = 1;
4160 tmp_ins.dst = &tmp_dst;
4161 tmp_ins.src_count = 2;
4162 tmp_ins.src = tmp_src;
4164 switch(ins->handler_idx)
4166 case WINED3DSIH_M4x4:
4167 nComponents = 4;
4168 tmp_ins.handler_idx = WINED3DSIH_DP4;
4169 break;
4170 case WINED3DSIH_M4x3:
4171 nComponents = 3;
4172 tmp_ins.handler_idx = WINED3DSIH_DP4;
4173 break;
4174 case WINED3DSIH_M3x4:
4175 nComponents = 4;
4176 tmp_ins.handler_idx = WINED3DSIH_DP3;
4177 break;
4178 case WINED3DSIH_M3x3:
4179 nComponents = 3;
4180 tmp_ins.handler_idx = WINED3DSIH_DP3;
4181 break;
4182 case WINED3DSIH_M3x2:
4183 nComponents = 2;
4184 tmp_ins.handler_idx = WINED3DSIH_DP3;
4185 break;
4186 default:
4187 break;
4190 tmp_dst = ins->dst[0];
4191 tmp_src[0] = ins->src[0];
4192 tmp_src[1] = ins->src[1];
4193 for (i = 0; i < nComponents; ++i)
4195 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4196 shader_glsl_dot(&tmp_ins);
4197 ++tmp_src[1].reg.idx[0].offset;
4202 The LRP instruction performs a component-wise linear interpolation
4203 between the second and third operands using the first operand as the
4204 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4205 This is equivalent to mix(src2, src1, src0);
4207 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4209 struct glsl_src_param src0_param;
4210 struct glsl_src_param src1_param;
4211 struct glsl_src_param src2_param;
4212 DWORD write_mask;
4214 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4216 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4217 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4218 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4220 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4221 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4224 /** Process the WINED3DSIO_LIT instruction in GLSL:
4225 * dst.x = dst.w = 1.0
4226 * dst.y = (src0.x > 0) ? src0.x
4227 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4228 * where src.w is clamped at +- 128
4230 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4232 struct glsl_src_param src0_param;
4233 struct glsl_src_param src1_param;
4234 struct glsl_src_param src3_param;
4235 char dst_mask[6];
4237 shader_glsl_append_dst(ins->ctx->buffer, ins);
4238 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4240 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4241 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4242 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4244 /* The sdk specifies the instruction like this
4245 * dst.x = 1.0;
4246 * if(src.x > 0.0) dst.y = src.x
4247 * else dst.y = 0.0.
4248 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4249 * else dst.z = 0.0;
4250 * dst.w = 1.0;
4251 * (where power = src.w clamped between -128 and 128)
4253 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4254 * dst.x = 1.0 ... No further explanation needed
4255 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4256 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4257 * dst.w = 1.0. ... Nothing fancy.
4259 * So we still have one conditional in there. So do this:
4260 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4262 * 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),
4263 * 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.
4264 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4266 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4267 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4268 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4270 shader_addline(ins->ctx->buffer,
4271 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4272 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4273 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4274 src0_param.param_str, src3_param.param_str, dst_mask);
4277 /** Process the WINED3DSIO_DST instruction in GLSL:
4278 * dst.x = 1.0
4279 * dst.y = src0.x * src0.y
4280 * dst.z = src0.z
4281 * dst.w = src1.w
4283 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4285 struct glsl_src_param src0y_param;
4286 struct glsl_src_param src0z_param;
4287 struct glsl_src_param src1y_param;
4288 struct glsl_src_param src1w_param;
4289 char dst_mask[6];
4291 shader_glsl_append_dst(ins->ctx->buffer, ins);
4292 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4294 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4295 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4296 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4297 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4299 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4300 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4303 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4304 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4305 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4307 * dst.x = cos(src0.?)
4308 * dst.y = sin(src0.?)
4309 * dst.z = dst.z
4310 * dst.w = dst.w
4312 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4314 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4315 struct glsl_src_param src0_param;
4316 DWORD write_mask;
4318 if (ins->ctx->reg_maps->shader_version.major < 4)
4320 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4322 write_mask = shader_glsl_append_dst(buffer, ins);
4323 switch (write_mask)
4325 case WINED3DSP_WRITEMASK_0:
4326 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4327 break;
4329 case WINED3DSP_WRITEMASK_1:
4330 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4331 break;
4333 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4334 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4335 src0_param.param_str, src0_param.param_str);
4336 break;
4338 default:
4339 ERR("Write mask should be .x, .y or .xy\n");
4340 break;
4343 return;
4346 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4349 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4351 char dst_mask[6];
4353 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4354 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4355 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4357 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4358 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4359 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4361 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4362 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4364 else
4366 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4367 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4368 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4371 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4373 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4374 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4375 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4379 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4380 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4381 * generate invalid code
4383 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4385 struct glsl_src_param src0_param;
4386 DWORD write_mask;
4388 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4389 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4391 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4394 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4395 * Start a for() loop where src1.y is the initial value of aL,
4396 * increment aL by src1.z for a total of src1.x iterations.
4397 * Need to use a temporary variable for this operation.
4399 /* FIXME: I don't think nested loops will work correctly this way. */
4400 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4402 struct wined3d_shader_parser_state *state = ins->ctx->state;
4403 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4404 const struct wined3d_shader *shader = ins->ctx->shader;
4405 const struct wined3d_shader_lconst *constant;
4406 struct glsl_src_param src1_param;
4407 const DWORD *control_values = NULL;
4409 if (ins->ctx->reg_maps->shader_version.major < 4)
4411 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4413 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4414 * class hardware doesn't support real varying indexing, but Microsoft
4415 * designed this feature for Shader model 2.x+. If the loop control is
4416 * known at compile time, the GLSL compiler can unroll the loop, and
4417 * replace indirect addressing with direct addressing. */
4418 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4420 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4422 if (constant->idx == ins->src[1].reg.idx[0].offset)
4424 control_values = constant->value;
4425 break;
4430 if (control_values)
4432 struct wined3d_shader_loop_control loop_control;
4433 loop_control.count = control_values[0];
4434 loop_control.start = control_values[1];
4435 loop_control.step = (int)control_values[2];
4437 if (loop_control.step > 0)
4439 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4440 state->current_loop_depth, loop_control.start,
4441 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4442 state->current_loop_depth, loop_control.step);
4444 else if (loop_control.step < 0)
4446 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4447 state->current_loop_depth, loop_control.start,
4448 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4449 state->current_loop_depth, loop_control.step);
4451 else
4453 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4454 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4455 state->current_loop_depth, loop_control.count,
4456 state->current_loop_depth);
4459 else
4461 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4462 state->current_loop_depth, state->current_loop_reg,
4463 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4464 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4467 ++state->current_loop_reg;
4469 else
4471 shader_addline(buffer, "for (;;)\n{\n");
4474 ++state->current_loop_depth;
4477 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4479 struct wined3d_shader_parser_state *state = ins->ctx->state;
4481 shader_addline(ins->ctx->buffer, "}\n");
4483 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4485 --state->current_loop_depth;
4486 --state->current_loop_reg;
4489 if (ins->handler_idx == WINED3DSIH_ENDREP)
4491 --state->current_loop_depth;
4495 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4497 struct wined3d_shader_parser_state *state = ins->ctx->state;
4498 const struct wined3d_shader *shader = ins->ctx->shader;
4499 const struct wined3d_shader_lconst *constant;
4500 struct glsl_src_param src0_param;
4501 const DWORD *control_values = NULL;
4503 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4504 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4506 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4508 if (constant->idx == ins->src[0].reg.idx[0].offset)
4510 control_values = constant->value;
4511 break;
4516 if (control_values)
4518 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4519 state->current_loop_depth, state->current_loop_depth,
4520 control_values[0], state->current_loop_depth);
4522 else
4524 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4525 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4526 state->current_loop_depth, state->current_loop_depth,
4527 src0_param.param_str, state->current_loop_depth);
4530 ++state->current_loop_depth;
4533 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4535 struct glsl_src_param src0_param;
4537 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4538 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4541 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4543 struct glsl_src_param src0_param;
4545 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4546 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4549 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4551 shader_addline(ins->ctx->buffer, "default:\n");
4554 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4556 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4557 struct glsl_src_param src0_param;
4559 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4560 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4563 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4565 struct glsl_src_param src0_param;
4566 struct glsl_src_param src1_param;
4568 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4569 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4571 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4572 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4575 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4577 shader_addline(ins->ctx->buffer, "} else {\n");
4580 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4582 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4584 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4585 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4586 shader_glsl_fixup_position(ins->ctx->buffer);
4588 if (!stream)
4589 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4590 else
4591 FIXME("Unhandled primitive stream %u.\n", stream);
4594 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4596 shader_addline(ins->ctx->buffer, "break;\n");
4599 /* FIXME: According to MSDN the compare is done per component. */
4600 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4602 struct glsl_src_param src0_param;
4603 struct glsl_src_param src1_param;
4605 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4606 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4608 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4609 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4612 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4614 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4615 struct glsl_src_param src_param;
4617 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4618 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4621 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4623 shader_addline(ins->ctx->buffer, "continue;\n");
4626 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4628 shader_addline(ins->ctx->buffer, "}\n");
4629 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4631 /* Subroutines appear at the end of the shader. */
4632 ins->ctx->state->in_subroutine = TRUE;
4635 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4637 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4640 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4642 struct glsl_src_param src1_param;
4644 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4645 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4646 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4649 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4651 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4653 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4655 shader_glsl_generate_shader_epilogue(ins->ctx);
4656 shader_addline(ins->ctx->buffer, "return;\n");
4660 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4662 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4663 ins->ctx->reg_maps->shader_version.minor);
4664 struct glsl_sample_function sample_function;
4665 DWORD sample_flags = 0;
4666 DWORD resource_idx;
4667 DWORD mask = 0, swizzle;
4668 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4670 /* 1.0-1.4: Use destination register as sampler source.
4671 * 2.0+: Use provided sampler source. */
4672 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4673 resource_idx = ins->dst[0].reg.idx[0].offset;
4674 else
4675 resource_idx = ins->src[1].reg.idx[0].offset;
4677 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4679 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4680 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4681 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4683 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4684 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4686 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4687 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4689 case WINED3D_TTFF_COUNT1:
4690 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4691 break;
4692 case WINED3D_TTFF_COUNT2:
4693 mask = WINED3DSP_WRITEMASK_1;
4694 break;
4695 case WINED3D_TTFF_COUNT3:
4696 mask = WINED3DSP_WRITEMASK_2;
4697 break;
4698 case WINED3D_TTFF_COUNT4:
4699 case WINED3D_TTFF_DISABLE:
4700 mask = WINED3DSP_WRITEMASK_3;
4701 break;
4705 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4707 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4709 if (src_mod == WINED3DSPSM_DZ) {
4710 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4711 mask = WINED3DSP_WRITEMASK_2;
4712 } else if (src_mod == WINED3DSPSM_DW) {
4713 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4714 mask = WINED3DSP_WRITEMASK_3;
4717 else
4719 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4720 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4722 /* ps 2.0 texldp instruction always divides by the fourth component. */
4723 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4724 mask = WINED3DSP_WRITEMASK_3;
4728 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4729 mask |= sample_function.coord_mask;
4730 sample_function.coord_mask = mask;
4732 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4733 else swizzle = ins->src[1].swizzle;
4735 /* 1.0-1.3: Use destination register as coordinate source.
4736 1.4+: Use provided coordinate source register. */
4737 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4739 char coord_mask[6];
4740 shader_glsl_write_mask_to_str(mask, coord_mask);
4741 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4742 "T%u%s", resource_idx, coord_mask);
4744 else
4746 struct glsl_src_param coord_param;
4747 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4748 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4750 struct glsl_src_param bias;
4751 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4752 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4753 NULL, "%s", coord_param.param_str);
4754 } else {
4755 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4756 "%s", coord_param.param_str);
4759 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4762 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4764 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4765 struct glsl_src_param coord_param, dx_param, dy_param;
4766 struct glsl_sample_function sample_function;
4767 DWORD sampler_idx;
4768 DWORD swizzle = ins->src[1].swizzle;
4770 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4771 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4773 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4774 shader_glsl_tex(ins);
4775 return;
4778 sampler_idx = ins->src[1].reg.idx[0].offset;
4780 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4781 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4782 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4783 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4785 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4786 NULL, NULL, "%s", coord_param.param_str);
4787 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4790 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4792 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4793 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4794 struct glsl_src_param coord_param, lod_param;
4795 struct glsl_sample_function sample_function;
4796 DWORD swizzle = ins->src[1].swizzle;
4797 DWORD sampler_idx;
4799 sampler_idx = ins->src[1].reg.idx[0].offset;
4801 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4802 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4804 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4806 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4807 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4809 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4810 * However, the NVIDIA drivers allow them in fragment shaders as well,
4811 * even without the appropriate extension. */
4812 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4814 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4815 "%s", coord_param.param_str);
4816 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4819 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4820 unsigned int resource_idx, unsigned int sampler_idx)
4822 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4823 unsigned int i;
4825 for (i = 0; i < sampler_map->count; ++i)
4827 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4828 return entries[i].bind_idx;
4831 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4833 return ~0u;
4836 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
4838 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4839 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4840 struct glsl_src_param image_coord_param, image_data_param;
4841 enum wined3d_shader_resource_type resource_type;
4842 enum wined3d_data_type data_type;
4843 unsigned int uav_idx;
4844 DWORD coord_mask;
4845 const char *op;
4847 switch (ins->handler_idx)
4849 case WINED3DSIH_ATOMIC_IADD: op = "imageAtomicAdd"; break;
4850 default:
4851 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4852 return;
4855 uav_idx = ins->dst[0].reg.idx[0].offset;
4856 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4857 if (resource_type >= ARRAY_SIZE(resource_type_info))
4859 ERR("Unexpected resource type %#x.\n", resource_type);
4860 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4862 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4863 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4865 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
4866 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
4867 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s, %s);\n",
4868 op, shader_glsl_get_prefix(version->type), uav_idx,
4869 image_coord_param.param_str, image_data_param.param_str);
4872 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
4874 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4875 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4876 enum wined3d_shader_resource_type resource_type;
4877 struct glsl_src_param image_coord_param;
4878 enum wined3d_data_type data_type;
4879 DWORD coord_mask, write_mask;
4880 unsigned int uav_idx;
4881 char dst_swizzle[6];
4883 uav_idx = ins->src[1].reg.idx[0].offset;
4884 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
4886 ERR("Invalid UAV index %u.\n", uav_idx);
4887 return;
4889 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4890 if (resource_type >= ARRAY_SIZE(resource_type_info))
4892 ERR("Unexpected resource type %#x.\n", resource_type);
4893 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4895 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4896 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4898 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
4899 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4901 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
4902 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
4903 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
4906 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
4908 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
4909 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4910 enum wined3d_shader_resource_type resource_type;
4911 enum wined3d_shader_register_type reg_type;
4912 unsigned int resource_idx, bind_idx, i;
4913 enum wined3d_data_type dst_data_type;
4914 struct glsl_src_param lod_param;
4915 char dst_swizzle[6];
4916 DWORD write_mask;
4918 dst_data_type = ins->dst[0].reg.data_type;
4919 if (ins->flags == WINED3DSI_RESINFO_UINT)
4920 dst_data_type = WINED3D_DATA_UINT;
4921 else if (ins->flags)
4922 FIXME("Unhandled flags %#x.\n", ins->flags);
4924 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
4925 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
4927 reg_type = ins->src[1].reg.type;
4928 resource_idx = ins->src[1].reg.idx[0].offset;
4929 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
4930 if (reg_type == WINED3DSPR_RESOURCE)
4932 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4933 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
4934 resource_idx, WINED3D_SAMPLER_DEFAULT);
4936 else
4938 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
4939 bind_idx = resource_idx;
4942 if (resource_type >= ARRAY_SIZE(resource_type_info))
4944 ERR("Unexpected resource type %#x.\n", resource_type);
4945 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
4948 if (dst_data_type == WINED3D_DATA_UINT)
4949 shader_addline(ins->ctx->buffer, "uvec4(");
4950 else
4951 shader_addline(ins->ctx->buffer, "vec4(");
4953 if (reg_type == WINED3DSPR_RESOURCE)
4955 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
4956 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
4958 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
4959 shader_addline(ins->ctx->buffer, "0, ");
4961 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
4963 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
4964 shader_glsl_get_prefix(version->type), bind_idx);
4966 else
4968 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
4969 shader_addline(ins->ctx->buffer, "1");
4972 else
4974 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
4975 shader_glsl_get_prefix(version->type), bind_idx);
4977 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
4978 shader_addline(ins->ctx->buffer, "0, ");
4980 /* For UAVs the returned miplevel count is always 1. */
4981 shader_addline(ins->ctx->buffer, "1");
4984 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
4987 /* FIXME: The current implementation does not handle multisample textures correctly. */
4988 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
4990 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4991 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
4992 struct glsl_src_param coord_param, lod_param;
4993 struct glsl_sample_function sample_function;
4994 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
4995 BOOL has_lod_param;
4997 if (wined3d_shader_instruction_has_texel_offset(ins))
4998 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5000 resource_idx = ins->src[1].reg.idx[0].offset;
5001 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5003 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5005 ERR("Invalid resource index %u.\n", resource_idx);
5006 return;
5008 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5010 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5011 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5012 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5013 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5014 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5015 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5016 "%s", coord_param.param_str);
5017 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5020 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5022 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5023 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5024 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5025 struct glsl_sample_function sample_function;
5026 DWORD flags = 0;
5028 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5029 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5030 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5031 flags |= WINED3D_GLSL_SAMPLE_LOD;
5032 if (wined3d_shader_instruction_has_texel_offset(ins))
5033 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5035 resource_idx = ins->src[1].reg.idx[0].offset;
5036 sampler_idx = ins->src[2].reg.idx[0].offset;
5038 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5039 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5041 switch (ins->handler_idx)
5043 case WINED3DSIH_SAMPLE:
5044 break;
5045 case WINED3DSIH_SAMPLE_B:
5046 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5047 lod_param_str = lod_param.param_str;
5048 break;
5049 case WINED3DSIH_SAMPLE_GRAD:
5050 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5051 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5052 dx_param_str = dx_param.param_str;
5053 dy_param_str = dy_param.param_str;
5054 break;
5055 case WINED3DSIH_SAMPLE_LOD:
5056 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5057 lod_param_str = lod_param.param_str;
5058 break;
5059 default:
5060 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5061 break;
5064 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5065 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5066 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5067 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5070 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5072 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5073 struct glsl_src_param coord_param, compare_param;
5074 struct glsl_sample_function sample_function;
5075 const char *lod_param = NULL;
5076 DWORD flags = 0;
5077 UINT coord_size;
5079 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5081 lod_param = "0";
5082 flags |= WINED3D_GLSL_SAMPLE_LOD;
5085 if (wined3d_shader_instruction_has_texel_offset(ins))
5086 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5088 resource_idx = ins->src[1].reg.idx[0].offset;
5089 sampler_idx = ins->src[2].reg.idx[0].offset;
5091 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5092 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5093 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5094 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5095 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5096 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5097 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5098 coord_size, coord_param.param_str, compare_param.param_str);
5099 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5102 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5104 /* FIXME: Make this work for more than just 2D textures */
5105 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5106 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5108 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5110 char dst_mask[6];
5112 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5113 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5114 ins->dst[0].reg.idx[0].offset, dst_mask);
5116 else
5118 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5119 DWORD reg = ins->src[0].reg.idx[0].offset;
5120 char dst_swizzle[6];
5122 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5124 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5126 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5127 struct glsl_src_param div_param;
5128 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5130 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5132 if (mask_size > 1)
5133 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5134 else
5135 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5137 else
5139 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5144 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5145 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5146 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5147 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5149 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5150 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5151 struct glsl_sample_function sample_function;
5152 struct glsl_src_param src0_param;
5153 UINT mask_size;
5155 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5157 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5158 * scalar, and projected sampling would require 4.
5160 * It is a dependent read - not valid with conditional NP2 textures
5162 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5163 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5165 switch(mask_size)
5167 case 1:
5168 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5169 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5170 break;
5172 case 2:
5173 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5174 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5175 break;
5177 case 3:
5178 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5179 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5180 break;
5182 default:
5183 FIXME("Unexpected mask size %u\n", mask_size);
5184 break;
5186 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5189 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5190 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5191 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5193 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5194 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5195 struct glsl_src_param src0_param;
5196 DWORD dst_mask;
5197 unsigned int mask_size;
5199 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5200 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5201 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5203 if (mask_size > 1) {
5204 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5205 } else {
5206 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5210 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5211 * Calculate the depth as dst.x / dst.y */
5212 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5214 struct glsl_dst_param dst_param;
5216 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5218 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5219 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5220 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5221 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5222 * >= 1.0 or < 0.0
5224 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5225 dst_param.reg_name, dst_param.reg_name);
5228 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5229 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5230 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5231 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5233 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5235 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5236 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5237 struct glsl_src_param src0_param;
5239 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5241 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5242 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5245 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5246 * Calculate the 1st of a 2-row matrix multiplication. */
5247 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5249 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5250 DWORD reg = ins->dst[0].reg.idx[0].offset;
5251 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5252 struct glsl_src_param src0_param;
5254 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5255 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5258 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5259 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5260 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5262 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5263 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5264 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5265 DWORD reg = ins->dst[0].reg.idx[0].offset;
5266 struct glsl_src_param src0_param;
5268 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5269 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5270 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5273 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5275 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5276 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5277 struct glsl_sample_function sample_function;
5278 DWORD reg = ins->dst[0].reg.idx[0].offset;
5279 struct glsl_src_param src0_param;
5281 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5282 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5284 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5286 /* Sample the texture using the calculated coordinates */
5287 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5288 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5291 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5292 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5293 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5295 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5296 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5297 struct glsl_sample_function sample_function;
5298 DWORD reg = ins->dst[0].reg.idx[0].offset;
5299 struct glsl_src_param src0_param;
5301 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5302 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5304 /* Dependent read, not valid with conditional NP2 */
5305 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5307 /* Sample the texture using the calculated coordinates */
5308 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5309 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5311 tex_mx->current_row = 0;
5314 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5315 * Perform the 3rd row of a 3x3 matrix multiply */
5316 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5318 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5319 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5320 DWORD reg = ins->dst[0].reg.idx[0].offset;
5321 struct glsl_src_param src0_param;
5322 char dst_mask[6];
5324 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5326 shader_glsl_append_dst(ins->ctx->buffer, ins);
5327 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5328 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5330 tex_mx->current_row = 0;
5333 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5334 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5335 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5337 struct glsl_src_param src0_param;
5338 struct glsl_src_param src1_param;
5339 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5340 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5341 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5342 struct glsl_sample_function sample_function;
5343 DWORD reg = ins->dst[0].reg.idx[0].offset;
5344 char coord_mask[6];
5346 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5347 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5349 /* Perform the last matrix multiply operation */
5350 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5351 /* Reflection calculation */
5352 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5354 /* Dependent read, not valid with conditional NP2 */
5355 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5356 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5358 /* Sample the texture */
5359 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5360 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5361 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5363 tex_mx->current_row = 0;
5366 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5367 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5368 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5370 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5371 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5372 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5373 struct glsl_sample_function sample_function;
5374 DWORD reg = ins->dst[0].reg.idx[0].offset;
5375 struct glsl_src_param src0_param;
5376 char coord_mask[6];
5378 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5380 /* Perform the last matrix multiply operation */
5381 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5383 /* Construct the eye-ray vector from w coordinates */
5384 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5385 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5386 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5388 /* Dependent read, not valid with conditional NP2 */
5389 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5390 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5392 /* Sample the texture using the calculated coordinates */
5393 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5394 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5395 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5397 tex_mx->current_row = 0;
5400 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5401 * Apply a fake bump map transform.
5402 * texbem is pshader <= 1.3 only, this saves a few version checks
5404 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5406 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5407 struct glsl_sample_function sample_function;
5408 struct glsl_src_param coord_param;
5409 DWORD sampler_idx;
5410 DWORD mask;
5411 DWORD flags;
5412 char coord_mask[6];
5414 sampler_idx = ins->dst[0].reg.idx[0].offset;
5415 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5416 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5418 /* Dependent read, not valid with conditional NP2 */
5419 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5420 mask = sample_function.coord_mask;
5422 shader_glsl_write_mask_to_str(mask, coord_mask);
5424 /* With projected textures, texbem only divides the static texture coord,
5425 * not the displacement, so we can't let GL handle this. */
5426 if (flags & WINED3D_PSARGS_PROJECTED)
5428 DWORD div_mask=0;
5429 char coord_div_mask[3];
5430 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5432 case WINED3D_TTFF_COUNT1:
5433 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5434 break;
5435 case WINED3D_TTFF_COUNT2:
5436 div_mask = WINED3DSP_WRITEMASK_1;
5437 break;
5438 case WINED3D_TTFF_COUNT3:
5439 div_mask = WINED3DSP_WRITEMASK_2;
5440 break;
5441 case WINED3D_TTFF_COUNT4:
5442 case WINED3D_TTFF_DISABLE:
5443 div_mask = WINED3DSP_WRITEMASK_3;
5444 break;
5446 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5447 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5450 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5452 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5453 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5454 coord_param.param_str, coord_mask);
5456 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5458 struct glsl_src_param luminance_param;
5459 struct glsl_dst_param dst_param;
5461 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5462 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5464 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5465 dst_param.reg_name, dst_param.mask_str,
5466 luminance_param.param_str, sampler_idx, sampler_idx);
5468 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5471 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5473 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5474 struct glsl_src_param src0_param, src1_param;
5476 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5477 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5479 shader_glsl_append_dst(ins->ctx->buffer, ins);
5480 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5481 src0_param.param_str, sampler_idx, src1_param.param_str);
5484 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5485 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5486 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5488 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5489 struct glsl_sample_function sample_function;
5490 struct glsl_src_param src0_param;
5492 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5494 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5495 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5496 "%s.wx", src0_param.reg_name);
5497 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5500 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5501 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5502 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5504 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5505 struct glsl_sample_function sample_function;
5506 struct glsl_src_param src0_param;
5508 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5510 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5511 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5512 "%s.yz", src0_param.reg_name);
5513 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5516 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5517 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5518 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5520 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5521 struct glsl_sample_function sample_function;
5522 struct glsl_src_param src0_param;
5524 /* Dependent read, not valid with conditional NP2 */
5525 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5526 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5528 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5529 "%s", src0_param.param_str);
5530 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5533 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5534 * If any of the first 3 components are < 0, discard this pixel */
5535 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5537 if (ins->ctx->reg_maps->shader_version.major >= 4)
5539 struct glsl_src_param src_param;
5541 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5542 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
5544 else
5546 struct glsl_dst_param dst_param;
5548 /* The argument is a destination parameter, and no writemasks are allowed */
5549 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5551 /* 2.0 shaders compare all 4 components in texkill. */
5552 if (ins->ctx->reg_maps->shader_version.major >= 2)
5553 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5554 /* 1.x shaders only compare the first 3 components, probably due to
5555 * the nature of the texkill instruction as a tex* instruction, and
5556 * phase, which kills all .w components. Even if all 4 components are
5557 * defined, only the first 3 are used. */
5558 else
5559 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5563 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5564 * dst = dot2(src0, src1) + src2 */
5565 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5567 struct glsl_src_param src0_param;
5568 struct glsl_src_param src1_param;
5569 struct glsl_src_param src2_param;
5570 DWORD write_mask;
5571 unsigned int mask_size;
5573 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5574 mask_size = shader_glsl_get_write_mask_size(write_mask);
5576 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5577 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5578 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5580 if (mask_size > 1) {
5581 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5582 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5583 } else {
5584 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5585 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5589 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5590 const struct wined3d_shader_signature *input_signature,
5591 const struct wined3d_shader_reg_maps *reg_maps,
5592 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5594 unsigned int i;
5596 for (i = 0; i < input_signature->element_count; ++i)
5598 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5599 const char *semantic_name;
5600 UINT semantic_idx;
5601 char reg_mask[6];
5603 /* Unused */
5604 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5605 continue;
5607 semantic_name = input->semantic_name;
5608 semantic_idx = input->semantic_idx;
5609 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5611 if (args->vp_mode == vertexshader)
5613 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5615 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5616 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5618 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5620 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5622 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5624 shader_addline(buffer, "ps_in[%u] = vec4("
5625 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5626 input->register_idx);
5628 else
5630 if (input->sysval_semantic)
5631 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5632 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5633 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5634 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5637 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5639 if (args->pointsprite)
5640 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5641 shader->u.ps.input_reg_map[input->register_idx]);
5642 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5643 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5644 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5645 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5646 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5647 else
5648 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5649 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5651 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5653 if (!semantic_idx)
5654 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5655 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5656 else if (semantic_idx == 1)
5657 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5658 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5659 else
5660 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5661 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5663 else
5665 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5666 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5671 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5673 struct glsl_program_key key;
5675 key.vs_id = entry->vs.id;
5676 key.gs_id = entry->gs.id;
5677 key.ps_id = entry->ps.id;
5679 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5681 ERR("Failed to insert program entry.\n");
5685 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5686 GLuint vs_id, GLuint gs_id, GLuint ps_id)
5688 struct wine_rb_entry *entry;
5689 struct glsl_program_key key;
5691 key.vs_id = vs_id;
5692 key.gs_id = gs_id;
5693 key.ps_id = ps_id;
5695 entry = wine_rb_get(&priv->program_lookup, &key);
5696 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5699 /* Context activation is done by the caller. */
5700 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5701 struct glsl_shader_prog_link *entry)
5703 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
5705 GL_EXTCALL(glDeleteProgram(entry->id));
5706 if (entry->vs.id)
5707 list_remove(&entry->vs.shader_entry);
5708 if (entry->gs.id)
5709 list_remove(&entry->gs.shader_entry);
5710 if (entry->ps.id)
5711 list_remove(&entry->ps.shader_entry);
5712 HeapFree(GetProcessHeap(), 0, entry);
5715 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5716 const struct wined3d_gl_info *gl_info, const DWORD *map,
5717 const struct wined3d_shader_signature *input_signature,
5718 const struct wined3d_shader_reg_maps *reg_maps_in,
5719 const struct wined3d_shader_signature *output_signature,
5720 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5722 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5723 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5724 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5725 unsigned int in_count = vec4_varyings(3, gl_info);
5726 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5727 DWORD in_idx, *set = NULL;
5728 unsigned int i, j;
5729 char reg_mask[6];
5731 set = wined3d_calloc(max_varyings, sizeof(*set));
5733 for (i = 0; i < input_signature->element_count; ++i)
5735 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5737 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5738 continue;
5740 in_idx = map[input->register_idx];
5741 /* Declared, but not read register */
5742 if (in_idx == ~0u)
5743 continue;
5744 if (in_idx >= max_varyings)
5746 FIXME("More input varyings declared than supported, expect issues.\n");
5747 continue;
5750 if (in_idx == in_count)
5751 string_buffer_sprintf(destination, "gl_FrontColor");
5752 else if (in_idx == in_count + 1)
5753 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5754 else
5755 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5757 if (!set[in_idx])
5758 set[in_idx] = ~0u;
5760 for (j = 0; j < output_signature->element_count; ++j)
5762 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5763 DWORD mask;
5765 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5766 || input->semantic_idx != output->semantic_idx
5767 || strcmp(input->semantic_name, output->semantic_name)
5768 || !(mask = input->mask & output->mask))
5769 continue;
5771 if (set[in_idx] == ~0u)
5772 set[in_idx] = 0;
5773 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5774 shader_glsl_write_mask_to_str(mask, reg_mask);
5776 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5777 destination->buffer, reg_mask, output->register_idx, reg_mask);
5781 for (i = 0; i < max_varyings; ++i)
5783 unsigned int size;
5785 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5786 continue;
5788 if (set[i] == ~0u)
5789 set[i] = 0;
5791 size = 0;
5792 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5793 reg_mask[size++] = 'x';
5794 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5795 reg_mask[size++] = 'y';
5796 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5797 reg_mask[size++] = 'z';
5798 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5799 reg_mask[size++] = 'w';
5800 reg_mask[size] = '\0';
5802 if (i == in_count)
5803 string_buffer_sprintf(destination, "gl_FrontColor");
5804 else if (i == in_count + 1)
5805 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5806 else
5807 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5809 if (size == 1)
5810 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5811 else
5812 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5815 HeapFree(GetProcessHeap(), 0, set);
5816 string_buffer_release(&priv->string_buffers, destination);
5819 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5820 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5821 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5823 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5824 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5825 char reg_mask[6];
5826 unsigned int i;
5828 for (i = 0; i < output_signature->element_count; ++i)
5830 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5832 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5833 continue;
5835 if (output->register_idx >= input_count)
5836 continue;
5838 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5840 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5842 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5843 destination->buffer, reg_mask, output->register_idx, reg_mask);
5846 string_buffer_release(&priv->string_buffers, destination);
5849 /* Context activation is done by the caller. */
5850 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5851 const struct wined3d_shader *vs, unsigned int input_count,
5852 const struct wined3d_gl_info *gl_info)
5854 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5855 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5857 if (legacy_context)
5858 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
5859 else
5860 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
5861 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5863 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
5864 legacy_context ? "gs_in" : "gs_in.gs_in");
5866 shader_addline(buffer, "}\n");
5869 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
5870 const struct wined3d_gl_info *gl_info, const DWORD *map,
5871 const struct wined3d_shader_signature *input_signature,
5872 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
5873 const struct wined3d_shader_signature *output_signature,
5874 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
5876 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5877 const char *semantic_name;
5878 UINT semantic_idx;
5879 char reg_mask[6];
5880 unsigned int i;
5882 /* First, sort out position and point size system values. */
5883 for (i = 0; i < output_signature->element_count; ++i)
5885 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5887 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5888 continue;
5890 semantic_name = output->semantic_name;
5891 semantic_idx = output->semantic_idx;
5892 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5894 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5896 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5897 reg_mask, output->register_idx, reg_mask);
5899 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
5901 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
5902 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
5904 else if (output->sysval_semantic)
5906 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
5910 /* Then, setup the pixel shader input. */
5911 if (reg_maps_out->shader_version.major < 4)
5912 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
5913 output_signature, reg_maps_out, "ps_link");
5914 else
5915 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
5918 /* Context activation is done by the caller. */
5919 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
5920 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
5921 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
5923 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5924 GLuint ret;
5925 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
5926 unsigned int i;
5927 const char *semantic_name;
5928 UINT semantic_idx;
5929 char reg_mask[6];
5930 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5932 string_buffer_clear(buffer);
5934 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &vs->reg_maps.shader_version));
5936 if (per_vertex_point_size)
5938 shader_addline(buffer, "uniform struct\n{\n");
5939 shader_addline(buffer, " float size_min;\n");
5940 shader_addline(buffer, " float size_max;\n");
5941 shader_addline(buffer, "} ffp_point;\n");
5944 if (ps_major < 3)
5946 DWORD colors_written_mask[2] = {0};
5947 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
5949 if (!legacy_context)
5951 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
5952 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
5953 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
5954 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
5957 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
5959 for (i = 0; i < vs->output_signature.element_count; ++i)
5961 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
5962 DWORD write_mask;
5964 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
5965 continue;
5967 semantic_name = output->semantic_name;
5968 semantic_idx = output->semantic_idx;
5969 write_mask = output->mask;
5970 shader_glsl_write_mask_to_str(write_mask, reg_mask);
5972 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
5974 if (legacy_context)
5975 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
5976 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
5977 else
5978 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
5979 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
5981 colors_written_mask[semantic_idx] = write_mask;
5983 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
5985 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
5986 reg_mask, output->register_idx, reg_mask);
5988 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5990 if (semantic_idx < MAX_TEXTURES)
5992 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
5993 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
5994 semantic_idx, reg_mask, output->register_idx, reg_mask);
5995 texcoords_written_mask[semantic_idx] = write_mask;
5998 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6000 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6001 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6003 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6005 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6006 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6007 output->register_idx, reg_mask[1]);
6011 for (i = 0; i < 2; ++i)
6013 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6015 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6016 if (!i)
6017 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6018 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6019 reg_mask, reg_mask);
6020 else
6021 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6022 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6023 reg_mask, reg_mask);
6026 for (i = 0; i < MAX_TEXTURES; ++i)
6028 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6029 continue;
6031 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6033 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6034 && !texcoords_written_mask[i])
6035 continue;
6037 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6038 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6039 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6043 else
6045 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6047 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6048 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6049 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6050 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6053 shader_addline(buffer, "}\n");
6055 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6056 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6057 shader_glsl_compile(gl_info, ret, buffer->buffer);
6059 return ret;
6062 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6063 const struct wined3d_shader *shader, unsigned int input_count,
6064 const struct wined3d_gl_info *gl_info)
6066 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6068 if (input_count)
6069 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6071 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6072 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6074 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6075 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6077 shader_addline(buffer, "}\n");
6080 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6081 const struct wined3d_gl_info *gl_info)
6083 const char *output = get_fragment_output(gl_info);
6085 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6086 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6087 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6088 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6089 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6090 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6093 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6094 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6096 const char *output = get_fragment_output(gl_info);
6098 switch (mode)
6100 case WINED3D_FFP_PS_FOG_OFF:
6101 return;
6103 case WINED3D_FFP_PS_FOG_LINEAR:
6104 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6105 break;
6107 case WINED3D_FFP_PS_FOG_EXP:
6108 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6109 break;
6111 case WINED3D_FFP_PS_FOG_EXP2:
6112 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6113 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6114 break;
6116 default:
6117 ERR("Invalid fog mode %#x.\n", mode);
6118 return;
6121 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6122 output, output);
6125 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6126 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6128 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6129 * flipping all the operators here, just negate the comparison below. */
6130 static const char * const comparison_operator[] =
6132 "", /* WINED3D_CMP_NEVER */
6133 "<", /* WINED3D_CMP_LESS */
6134 "==", /* WINED3D_CMP_EQUAL */
6135 "<=", /* WINED3D_CMP_LESSEQUAL */
6136 ">", /* WINED3D_CMP_GREATER */
6137 "!=", /* WINED3D_CMP_NOTEQUAL */
6138 ">=", /* WINED3D_CMP_GREATEREQUAL */
6139 "" /* WINED3D_CMP_ALWAYS */
6142 if (alpha_func == WINED3D_CMP_ALWAYS)
6143 return;
6145 if (alpha_func != WINED3D_CMP_NEVER)
6146 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6147 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6148 shader_addline(buffer, " discard;\n");
6151 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6152 const struct wined3d_gl_info *gl_info)
6154 if (gl_info->supported[ARB_GPU_SHADER5])
6155 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6156 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6157 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6158 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6159 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6160 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6161 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6162 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6163 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6164 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6165 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6166 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6167 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6168 if (gl_info->supported[EXT_GPU_SHADER4])
6169 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6170 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6171 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6174 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6175 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6176 const struct ps_compile_args *args)
6178 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6180 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6181 if (reg_maps->shader_version.major < 2)
6182 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6184 if (args->srgb_correction)
6185 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6187 /* SM < 3 does not replace the fog stage. */
6188 if (reg_maps->shader_version.major < 3)
6189 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6191 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6194 /* Context activation is done by the caller. */
6195 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6196 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6197 const struct wined3d_shader *shader,
6198 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6200 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6201 const struct wined3d_gl_info *gl_info = context->gl_info;
6202 const DWORD *function = shader->function;
6203 struct shader_glsl_ctx_priv priv_ctx;
6204 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6206 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6207 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6209 memset(&priv_ctx, 0, sizeof(priv_ctx));
6210 priv_ctx.cur_ps_args = args;
6211 priv_ctx.cur_np2fixup_info = np2fixup_info;
6212 priv_ctx.string_buffers = string_buffers;
6214 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6216 shader_glsl_enable_extensions(buffer, gl_info);
6217 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6218 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6219 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6220 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6221 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6222 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6223 /* The spec says that it doesn't have to be explicitly enabled, but the
6224 * nvidia drivers write a warning if we don't do so. */
6225 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6226 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6228 /* Base Declarations */
6229 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6231 shader_addline(buffer, "void main()\n{\n");
6233 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6234 * add approximately 0.5. This causes off-by-one problems as spotted by
6235 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6236 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6237 * causes precision troubles when we just subtract 0.5.
6239 * To deal with that, just floor() the position. This will eliminate the
6240 * fraction on all cards.
6242 * TODO: Test how this behaves with multisampling.
6244 * An advantage of floor is that it works even if the driver doesn't add
6245 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6246 * to return in gl_FragCoord, even though coordinates specify the pixel
6247 * centers instead of the pixel corners. This code will behave correctly
6248 * on drivers that returns integer values. */
6249 if (reg_maps->vpos)
6251 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6252 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6253 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6254 shader_addline(buffer,
6255 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6256 else
6257 shader_addline(buffer,
6258 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6261 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6263 unsigned int i;
6264 WORD map = reg_maps->texcoord;
6266 if (legacy_context)
6268 if (glsl_is_color_reg_read(shader, 0))
6269 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6270 if (glsl_is_color_reg_read(shader, 1))
6271 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6274 for (i = 0; map; map >>= 1, ++i)
6276 if (map & 1)
6278 if (args->pointsprite)
6279 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6280 else if (args->texcoords_initialized & (1u << i))
6281 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6282 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6283 else
6284 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6285 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6289 if (legacy_context)
6290 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6293 /* Pack 3.0 inputs */
6294 if (reg_maps->shader_version.major >= 3)
6295 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6297 /* Base Shader Body */
6298 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6300 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6301 if (reg_maps->shader_version.major < 4)
6302 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6304 shader_addline(buffer, "}\n");
6306 TRACE("Compiling shader object %u.\n", shader_id);
6307 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6309 return shader_id;
6312 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6313 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6314 const struct vs_compile_args *args)
6316 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6317 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6318 unsigned int i;
6320 /* Unpack outputs. */
6321 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6323 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6324 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6325 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6326 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6328 if (reg_maps->shader_version.major < 3)
6330 if (args->fog_src == VS_FOG_Z)
6331 shader_addline(buffer, "%s = gl_Position.z;\n",
6332 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6333 else if (!reg_maps->fog)
6334 shader_addline(buffer, "%s = 0.0;\n",
6335 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6338 /* We always store the clipplanes without y inversion. */
6339 if (args->clip_enabled)
6341 if (legacy_context)
6342 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6343 else
6344 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6345 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6348 if (args->point_size && !args->per_vertex_point_size)
6349 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6351 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6352 shader_glsl_fixup_position(buffer);
6355 /* Context activation is done by the caller. */
6356 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6357 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6359 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6360 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6361 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6362 const struct wined3d_gl_info *gl_info = context->gl_info;
6363 const DWORD *function = shader->function;
6364 struct shader_glsl_ctx_priv priv_ctx;
6366 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6367 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6369 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6371 shader_glsl_enable_extensions(buffer, gl_info);
6372 if (gl_info->supported[ARB_DRAW_INSTANCED])
6373 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6374 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6375 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6377 memset(&priv_ctx, 0, sizeof(priv_ctx));
6378 priv_ctx.cur_vs_args = args;
6379 priv_ctx.string_buffers = string_buffers;
6381 /* Base Declarations */
6382 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6384 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6385 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6387 if (reg_maps->shader_version.major >= 4)
6389 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6390 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6391 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6392 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6395 shader_addline(buffer, "void main()\n{\n");
6397 /* Base Shader Body */
6398 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6400 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6401 if (reg_maps->shader_version.major < 4)
6402 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6404 shader_addline(buffer, "}\n");
6406 TRACE("Compiling shader object %u.\n", shader_id);
6407 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6409 return shader_id;
6412 /* Context activation is done by the caller. */
6413 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6414 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6416 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6417 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6418 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6419 const struct wined3d_gl_info *gl_info = context->gl_info;
6420 const DWORD *function = shader->function;
6421 struct shader_glsl_ctx_priv priv_ctx;
6422 GLuint shader_id;
6424 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6426 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6428 shader_glsl_enable_extensions(buffer, gl_info);
6429 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6430 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6432 memset(&priv_ctx, 0, sizeof(priv_ctx));
6433 priv_ctx.string_buffers = string_buffers;
6434 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6435 if (!gl_info->supported[ARB_CLIP_CONTROL])
6436 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6437 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6438 shader_addline(buffer, "void main()\n{\n");
6439 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6440 shader_addline(buffer, "}\n");
6442 TRACE("Compiling shader object %u.\n", shader_id);
6443 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6445 return shader_id;
6448 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6450 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6451 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6452 const struct wined3d_shader *shader = ctx->shader;
6454 switch (shader->reg_maps.shader_version.type)
6456 case WINED3D_SHADER_TYPE_PIXEL:
6457 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6458 break;
6459 case WINED3D_SHADER_TYPE_VERTEX:
6460 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
6461 break;
6462 case WINED3D_SHADER_TYPE_GEOMETRY:
6463 break;
6464 default:
6465 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6466 break;
6470 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6471 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6472 struct wined3d_shader *shader,
6473 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6475 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6476 struct glsl_shader_private *shader_data;
6477 struct ps_np2fixup_info *np2fixup;
6478 UINT i;
6479 DWORD new_size;
6480 GLuint ret;
6482 if (!shader->backend_data)
6484 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6485 if (!shader->backend_data)
6487 ERR("Failed to allocate backend data.\n");
6488 return 0;
6491 shader_data = shader->backend_data;
6492 gl_shaders = shader_data->gl_shaders.ps;
6494 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6495 * so a linear search is more performant than a hashmap or a binary search
6496 * (cache coherency etc)
6498 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6500 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6502 if (args->np2_fixup)
6503 *np2fixup_info = &gl_shaders[i].np2fixup;
6504 return gl_shaders[i].id;
6508 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6509 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6510 if (shader_data->num_gl_shaders)
6512 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6513 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6514 new_size * sizeof(*gl_shaders));
6516 else
6518 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6519 new_size = 1;
6522 if(!new_array) {
6523 ERR("Out of memory\n");
6524 return 0;
6526 shader_data->gl_shaders.ps = new_array;
6527 shader_data->shader_array_size = new_size;
6528 gl_shaders = new_array;
6531 gl_shaders[shader_data->num_gl_shaders].args = *args;
6533 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6534 memset(np2fixup, 0, sizeof(*np2fixup));
6535 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6537 pixelshader_update_resource_types(shader, args->tex_types);
6539 string_buffer_clear(buffer);
6540 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6541 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6543 return ret;
6546 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6547 const DWORD use_map)
6549 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6550 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6551 if (stored->point_size != new->point_size)
6552 return FALSE;
6553 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6554 return FALSE;
6555 if (stored->flatshading != new->flatshading)
6556 return FALSE;
6557 if (stored->next_shader_type != new->next_shader_type)
6558 return FALSE;
6559 if (stored->next_shader_input_count != new->next_shader_input_count)
6560 return FALSE;
6561 return stored->fog_src == new->fog_src;
6564 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6565 struct wined3d_shader *shader, const struct vs_compile_args *args)
6567 UINT i;
6568 DWORD new_size;
6569 DWORD use_map = context->stream_info.use_map;
6570 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6571 struct glsl_shader_private *shader_data;
6572 GLuint ret;
6574 if (!shader->backend_data)
6576 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6577 if (!shader->backend_data)
6579 ERR("Failed to allocate backend data.\n");
6580 return 0;
6583 shader_data = shader->backend_data;
6584 gl_shaders = shader_data->gl_shaders.vs;
6586 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6587 * so a linear search is more performant than a hashmap or a binary search
6588 * (cache coherency etc)
6590 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6592 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6593 return gl_shaders[i].id;
6596 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6598 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6599 if (shader_data->num_gl_shaders)
6601 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6602 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6603 new_size * sizeof(*gl_shaders));
6605 else
6607 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6608 new_size = 1;
6611 if(!new_array) {
6612 ERR("Out of memory\n");
6613 return 0;
6615 shader_data->gl_shaders.vs = new_array;
6616 shader_data->shader_array_size = new_size;
6617 gl_shaders = new_array;
6620 gl_shaders[shader_data->num_gl_shaders].args = *args;
6622 string_buffer_clear(&priv->shader_buffer);
6623 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6624 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6626 return ret;
6629 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6630 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6632 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6633 struct glsl_shader_private *shader_data;
6634 unsigned int i, new_size;
6635 GLuint ret;
6637 if (!shader->backend_data)
6639 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6641 ERR("Failed to allocate backend data.\n");
6642 return 0;
6645 shader_data = shader->backend_data;
6646 gl_shaders = shader_data->gl_shaders.gs;
6648 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6650 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6651 return gl_shaders[i].id;
6654 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6656 if (shader_data->num_gl_shaders)
6658 new_size = shader_data->shader_array_size + 1;
6659 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6660 new_size * sizeof(*new_array));
6662 else
6664 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6665 new_size = 1;
6668 if (!new_array)
6670 ERR("Failed to allocate GL shaders array.\n");
6671 return 0;
6673 shader_data->gl_shaders.gs = new_array;
6674 shader_data->shader_array_size = new_size;
6675 gl_shaders = new_array;
6677 string_buffer_clear(&priv->shader_buffer);
6678 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6679 gl_shaders[shader_data->num_gl_shaders].args = *args;
6680 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6682 return ret;
6685 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6687 switch (mcs)
6689 case WINED3D_MCS_MATERIAL:
6690 return material;
6691 case WINED3D_MCS_COLOR1:
6692 return "ffp_attrib_diffuse";
6693 case WINED3D_MCS_COLOR2:
6694 return "ffp_attrib_specular";
6695 default:
6696 ERR("Invalid material color source %#x.\n", mcs);
6697 return "<invalid>";
6701 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6702 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6704 const char *diffuse, *specular, *emissive, *ambient;
6705 unsigned int i, idx;
6707 if (!settings->lighting)
6709 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6710 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6711 return;
6714 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6715 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6716 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6717 shader_addline(buffer, "vec3 dir, dst;\n");
6718 shader_addline(buffer, "float att, t;\n");
6720 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6721 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6722 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6723 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6725 idx = 0;
6726 for (i = 0; i < settings->point_light_count; ++i, ++idx)
6728 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
6729 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6730 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6731 shader_addline(buffer, "dst.x = 1.0;\n");
6732 if (legacy_lighting)
6734 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
6735 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6737 else
6739 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
6741 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6742 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
6743 if (!legacy_lighting)
6744 shader_addline(buffer, "att = 1.0 / att;\n");
6745 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
6746 if (!settings->normal)
6748 if (!legacy_lighting)
6749 shader_addline(buffer, "}\n");
6750 continue;
6752 shader_addline(buffer, "dir = normalize(dir);\n");
6753 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6754 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
6755 if (settings->localviewer)
6756 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6757 else
6758 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6759 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6760 " * ffp_light[%u].specular) * att;\n", idx);
6761 if (!legacy_lighting)
6762 shader_addline(buffer, "}\n");
6765 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
6767 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
6768 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6769 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6770 shader_addline(buffer, "dst.x = 1.0;\n");
6771 if (legacy_lighting)
6773 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
6774 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6776 else
6778 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
6780 shader_addline(buffer, "dir = normalize(dir);\n");
6781 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
6782 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
6783 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
6784 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
6785 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
6786 idx, idx, idx, idx);
6787 if (legacy_lighting)
6788 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6789 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6790 idx, idx, idx);
6791 else
6792 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6793 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
6794 idx, idx, idx);
6795 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
6796 if (!settings->normal)
6798 if (!legacy_lighting)
6799 shader_addline(buffer, "}\n");
6800 continue;
6802 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6803 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
6804 if (settings->localviewer)
6805 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6806 else
6807 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6808 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6809 " * ffp_light[%u].specular) * att;\n", idx);
6810 if (!legacy_lighting)
6811 shader_addline(buffer, "}\n");
6814 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
6816 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
6817 if (!settings->normal)
6818 continue;
6819 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
6820 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6821 " * ffp_light[%u].diffuse.xyz;\n", idx);
6822 /* TODO: In the non-local viewer case the halfvector is constant
6823 * and could be precomputed and stored in a uniform. */
6824 if (settings->localviewer)
6825 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6826 else
6827 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6828 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6829 " * ffp_light[%u].specular;\n", idx);
6832 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
6834 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
6835 if (!settings->normal)
6836 continue;
6837 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
6838 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
6839 " * ffp_light[%u].diffuse.xyz;\n", idx);
6840 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6841 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
6842 " * ffp_light[%u].specular;\n", idx);
6845 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
6846 ambient, diffuse, emissive);
6847 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
6848 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
6851 /* Context activation is done by the caller. */
6852 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
6853 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
6855 static const struct attrib_info
6857 const char type[6];
6858 const char name[24];
6860 attrib_info[] =
6862 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
6863 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
6864 /* TODO: Indexed vertex blending */
6865 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
6866 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
6867 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
6868 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
6869 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
6871 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6872 BOOL legacy_lighting = priv->legacy_lighting;
6873 GLuint shader_obj;
6874 unsigned int i;
6875 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6876 BOOL output_legacy_fogcoord = legacy_context;
6878 string_buffer_clear(buffer);
6880 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
6882 if (shader_glsl_use_explicit_attrib_location(gl_info))
6883 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6885 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
6887 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
6889 if (shader_glsl_use_explicit_attrib_location(gl_info))
6890 shader_addline(buffer, "layout(location = %u) ", i);
6891 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
6893 shader_addline(buffer, "\n");
6895 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
6896 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
6897 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
6898 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
6900 shader_addline(buffer, "uniform struct\n{\n");
6901 shader_addline(buffer, " vec4 emissive;\n");
6902 shader_addline(buffer, " vec4 ambient;\n");
6903 shader_addline(buffer, " vec4 diffuse;\n");
6904 shader_addline(buffer, " vec4 specular;\n");
6905 shader_addline(buffer, " float shininess;\n");
6906 shader_addline(buffer, "} ffp_material;\n");
6908 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
6909 shader_addline(buffer, "uniform struct\n{\n");
6910 shader_addline(buffer, " vec4 diffuse;\n");
6911 shader_addline(buffer, " vec4 specular;\n");
6912 shader_addline(buffer, " vec4 ambient;\n");
6913 shader_addline(buffer, " vec4 position;\n");
6914 shader_addline(buffer, " vec3 direction;\n");
6915 shader_addline(buffer, " float range;\n");
6916 shader_addline(buffer, " float falloff;\n");
6917 shader_addline(buffer, " float c_att;\n");
6918 shader_addline(buffer, " float l_att;\n");
6919 shader_addline(buffer, " float q_att;\n");
6920 shader_addline(buffer, " float cos_htheta;\n");
6921 shader_addline(buffer, " float cos_hphi;\n");
6922 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
6924 if (settings->point_size)
6926 shader_addline(buffer, "uniform struct\n{\n");
6927 shader_addline(buffer, " float size;\n");
6928 shader_addline(buffer, " float size_min;\n");
6929 shader_addline(buffer, " float size_max;\n");
6930 shader_addline(buffer, " float c_att;\n");
6931 shader_addline(buffer, " float l_att;\n");
6932 shader_addline(buffer, " float q_att;\n");
6933 shader_addline(buffer, "} ffp_point;\n");
6936 if (legacy_context)
6938 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
6939 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
6940 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6941 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
6943 else
6945 if (settings->clipping)
6946 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
6948 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
6949 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
6950 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6951 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6954 shader_addline(buffer, "\nvoid main()\n{\n");
6955 shader_addline(buffer, "float m;\n");
6956 shader_addline(buffer, "vec3 r;\n");
6958 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
6960 if (attrib_info[i].name[0])
6961 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
6962 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
6964 for (i = 0; i < MAX_TEXTURES; ++i)
6966 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
6967 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
6968 && settings->texcoords & (1u << i))
6969 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
6972 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
6974 if (settings->transformed)
6976 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
6977 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6978 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
6980 else
6982 for (i = 0; i < settings->vertexblends; ++i)
6983 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
6985 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
6986 for (i = 0; i < settings->vertexblends + 1; ++i)
6987 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
6989 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
6990 if (settings->clipping)
6992 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
6993 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
6994 else
6995 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6996 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
6998 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7001 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7002 if (settings->normal)
7004 if (!settings->vertexblends)
7006 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7008 else
7010 for (i = 0; i < settings->vertexblends + 1; ++i)
7011 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7014 if (settings->normalize)
7015 shader_addline(buffer, "normal = normalize(normal);\n");
7018 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7019 if (legacy_context)
7021 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7022 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7024 else
7026 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7027 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7030 for (i = 0; i < MAX_TEXTURES; ++i)
7032 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7034 switch (settings->texgen[i] & 0xffff0000)
7036 case WINED3DTSS_TCI_PASSTHRU:
7037 if (settings->texcoords & (1u << i))
7038 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7039 i, i, i);
7040 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7041 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7042 else
7043 output_legacy_texcoord = FALSE;
7044 break;
7046 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7047 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7048 break;
7050 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7051 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7052 break;
7054 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7055 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7056 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7057 break;
7059 case WINED3DTSS_TCI_SPHEREMAP:
7060 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7061 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7062 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7063 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7064 break;
7066 default:
7067 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7068 break;
7070 if (output_legacy_texcoord)
7071 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7074 switch (settings->fog_mode)
7076 case WINED3D_FFP_VS_FOG_OFF:
7077 output_legacy_fogcoord = FALSE;
7078 break;
7080 case WINED3D_FFP_VS_FOG_FOGCOORD:
7081 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7082 break;
7084 case WINED3D_FFP_VS_FOG_RANGE:
7085 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7086 break;
7088 case WINED3D_FFP_VS_FOG_DEPTH:
7089 if (settings->ortho_fog)
7091 if (gl_info->supported[ARB_CLIP_CONTROL])
7092 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7093 else
7094 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7095 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7097 else if (settings->transformed)
7099 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7101 else
7103 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7105 break;
7107 default:
7108 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7109 break;
7111 if (output_legacy_fogcoord)
7112 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7114 if (settings->point_size)
7116 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7117 " + ffp_point.l_att * length(ec_pos.xyz)"
7118 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7119 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7120 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7123 shader_addline(buffer, "}\n");
7125 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7126 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7128 return shader_obj;
7131 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7132 DWORD argnum, unsigned int stage, DWORD arg)
7134 const char *ret;
7136 if (arg == ARG_UNUSED)
7137 return "<unused arg>";
7139 switch (arg & WINED3DTA_SELECTMASK)
7141 case WINED3DTA_DIFFUSE:
7142 ret = "ffp_varying_diffuse";
7143 break;
7145 case WINED3DTA_CURRENT:
7146 ret = "ret";
7147 break;
7149 case WINED3DTA_TEXTURE:
7150 switch (stage)
7152 case 0: ret = "tex0"; break;
7153 case 1: ret = "tex1"; break;
7154 case 2: ret = "tex2"; break;
7155 case 3: ret = "tex3"; break;
7156 case 4: ret = "tex4"; break;
7157 case 5: ret = "tex5"; break;
7158 case 6: ret = "tex6"; break;
7159 case 7: ret = "tex7"; break;
7160 default:
7161 ret = "<invalid texture>";
7162 break;
7164 break;
7166 case WINED3DTA_TFACTOR:
7167 ret = "tex_factor";
7168 break;
7170 case WINED3DTA_SPECULAR:
7171 ret = "ffp_varying_specular";
7172 break;
7174 case WINED3DTA_TEMP:
7175 ret = "temp_reg";
7176 break;
7178 case WINED3DTA_CONSTANT:
7179 switch (stage)
7181 case 0: ret = "tss_const0"; break;
7182 case 1: ret = "tss_const1"; break;
7183 case 2: ret = "tss_const2"; break;
7184 case 3: ret = "tss_const3"; break;
7185 case 4: ret = "tss_const4"; break;
7186 case 5: ret = "tss_const5"; break;
7187 case 6: ret = "tss_const6"; break;
7188 case 7: ret = "tss_const7"; break;
7189 default:
7190 ret = "<invalid constant>";
7191 break;
7193 break;
7195 default:
7196 return "<unhandled arg>";
7199 if (arg & WINED3DTA_COMPLEMENT)
7201 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7202 if (argnum == 0)
7203 ret = "arg0";
7204 else if (argnum == 1)
7205 ret = "arg1";
7206 else if (argnum == 2)
7207 ret = "arg2";
7210 if (arg & WINED3DTA_ALPHAREPLICATE)
7212 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7213 if (argnum == 0)
7214 ret = "arg0";
7215 else if (argnum == 1)
7216 ret = "arg1";
7217 else if (argnum == 2)
7218 ret = "arg2";
7221 return ret;
7224 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7225 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7227 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7229 if (color && alpha)
7230 dstmask = "";
7231 else if (color)
7232 dstmask = ".xyz";
7233 else
7234 dstmask = ".w";
7236 if (dst == tempreg)
7237 dstreg = "temp_reg";
7238 else
7239 dstreg = "ret";
7241 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7242 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7243 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7245 switch (op)
7247 case WINED3D_TOP_DISABLE:
7248 break;
7250 case WINED3D_TOP_SELECT_ARG1:
7251 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7252 break;
7254 case WINED3D_TOP_SELECT_ARG2:
7255 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7256 break;
7258 case WINED3D_TOP_MODULATE:
7259 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7260 break;
7262 case WINED3D_TOP_MODULATE_4X:
7263 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7264 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7265 break;
7267 case WINED3D_TOP_MODULATE_2X:
7268 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7269 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7270 break;
7272 case WINED3D_TOP_ADD:
7273 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7274 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7275 break;
7277 case WINED3D_TOP_ADD_SIGNED:
7278 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7279 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7280 break;
7282 case WINED3D_TOP_ADD_SIGNED_2X:
7283 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7284 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7285 break;
7287 case WINED3D_TOP_SUBTRACT:
7288 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7289 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7290 break;
7292 case WINED3D_TOP_ADD_SMOOTH:
7293 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7294 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7295 break;
7297 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7298 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7299 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7300 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7301 break;
7303 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7304 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7305 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7306 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7307 break;
7309 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7310 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7311 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7312 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7313 break;
7315 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7316 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7317 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7318 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7319 break;
7321 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7322 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7323 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7324 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7325 break;
7327 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7328 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7329 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7330 break;
7332 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7333 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7334 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7335 break;
7337 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7338 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7339 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7340 break;
7341 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7342 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7343 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7344 break;
7346 case WINED3D_TOP_BUMPENVMAP:
7347 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7348 /* These are handled in the first pass, nothing to do. */
7349 break;
7351 case WINED3D_TOP_DOTPRODUCT3:
7352 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7353 dstreg, dstmask, arg1, arg2, dstmask);
7354 break;
7356 case WINED3D_TOP_MULTIPLY_ADD:
7357 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7358 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7359 break;
7361 case WINED3D_TOP_LERP:
7362 /* MSDN isn't quite right here. */
7363 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7364 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7365 break;
7367 default:
7368 FIXME("Unhandled operation %#x.\n", op);
7369 break;
7373 /* Context activation is done by the caller. */
7374 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7375 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7377 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7378 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7379 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7380 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7381 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7382 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7383 UINT lowest_disabled_stage;
7384 GLuint shader_id;
7385 DWORD arg0, arg1, arg2;
7386 unsigned int stage;
7388 string_buffer_clear(buffer);
7390 /* Find out which textures are read */
7391 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7393 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7394 break;
7396 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7397 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7398 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7400 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7401 || (stage == 0 && settings->color_key_enabled))
7402 tex_map |= 1u << stage;
7403 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7404 tfactor_used = TRUE;
7405 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7406 tempreg_used = TRUE;
7407 if (settings->op[stage].dst == tempreg)
7408 tempreg_used = TRUE;
7409 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7410 tss_const_map |= 1u << stage;
7412 switch (settings->op[stage].cop)
7414 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7415 lum_map |= 1u << stage;
7416 /* fall through */
7417 case WINED3D_TOP_BUMPENVMAP:
7418 bump_map |= 1u << stage;
7419 /* fall through */
7420 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7421 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7422 tex_map |= 1u << stage;
7423 break;
7425 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7426 tfactor_used = TRUE;
7427 break;
7429 default:
7430 break;
7433 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7434 continue;
7436 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7437 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7438 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7440 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7441 tex_map |= 1u << stage;
7442 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7443 tfactor_used = TRUE;
7444 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7445 tempreg_used = TRUE;
7446 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7447 tss_const_map |= 1u << stage;
7449 lowest_disabled_stage = stage;
7451 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7453 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7454 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7456 if (!needs_legacy_glsl_syntax(gl_info))
7457 shader_addline(buffer, "out vec4 ps_out[1];\n");
7459 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7460 shader_addline(buffer, "vec4 ret;\n");
7461 if (tempreg_used || settings->sRGB_write)
7462 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7463 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7465 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7467 if (tss_const_map & (1u << stage))
7468 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7470 if (!(tex_map & (1u << stage)))
7471 continue;
7473 switch (settings->op[stage].tex_type)
7475 case WINED3D_GL_RES_TYPE_TEX_1D:
7476 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7477 break;
7478 case WINED3D_GL_RES_TYPE_TEX_2D:
7479 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7480 break;
7481 case WINED3D_GL_RES_TYPE_TEX_3D:
7482 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7483 break;
7484 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7485 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7486 break;
7487 case WINED3D_GL_RES_TYPE_TEX_RECT:
7488 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7489 break;
7490 default:
7491 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7492 break;
7495 shader_addline(buffer, "vec4 tex%u;\n", stage);
7497 if (!(bump_map & (1u << stage)))
7498 continue;
7499 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7501 if (!(lum_map & (1u << stage)))
7502 continue;
7503 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7504 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7506 if (tfactor_used)
7507 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7508 if (settings->color_key_enabled)
7509 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7510 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7512 if (settings->sRGB_write)
7514 shader_addline(buffer, "const vec4 srgb_const0 = ");
7515 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7516 shader_addline(buffer, ";\n");
7517 shader_addline(buffer, "const vec4 srgb_const1 = ");
7518 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7519 shader_addline(buffer, ";\n");
7522 shader_addline(buffer, "uniform struct\n{\n");
7523 shader_addline(buffer, " vec4 color;\n");
7524 shader_addline(buffer, " float density;\n");
7525 shader_addline(buffer, " float end;\n");
7526 shader_addline(buffer, " float scale;\n");
7527 shader_addline(buffer, "} ffp_fog;\n");
7529 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7530 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7532 if (legacy_context)
7534 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7535 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7536 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7537 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7538 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7540 else
7542 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7543 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7544 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7545 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7546 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7549 shader_addline(buffer, "void main()\n{\n");
7551 if (legacy_context)
7553 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7554 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7557 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7559 if (tex_map & (1u << stage))
7561 if (settings->pointsprite)
7562 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7563 else if (settings->texcoords_initialized & (1u << stage))
7564 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7565 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7566 else
7567 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7571 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7572 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7574 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7575 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7577 /* Generate texture sampling instructions */
7578 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7580 const char *texture_function, *coord_mask;
7581 BOOL proj;
7583 if (!(tex_map & (1u << stage)))
7584 continue;
7586 if (settings->op[stage].projected == proj_none)
7588 proj = FALSE;
7590 else if (settings->op[stage].projected == proj_count4
7591 || settings->op[stage].projected == proj_count3)
7593 proj = TRUE;
7595 else
7597 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7598 proj = TRUE;
7601 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7602 proj = FALSE;
7604 switch (settings->op[stage].tex_type)
7606 case WINED3D_GL_RES_TYPE_TEX_1D:
7607 if (proj)
7609 texture_function = "texture1DProj";
7610 coord_mask = "xw";
7612 else
7614 texture_function = "texture1D";
7615 coord_mask = "x";
7617 break;
7618 case WINED3D_GL_RES_TYPE_TEX_2D:
7619 if (proj)
7621 texture_function = "texture2DProj";
7622 coord_mask = "xyw";
7624 else
7626 texture_function = "texture2D";
7627 coord_mask = "xy";
7629 break;
7630 case WINED3D_GL_RES_TYPE_TEX_3D:
7631 if (proj)
7633 texture_function = "texture3DProj";
7634 coord_mask = "xyzw";
7636 else
7638 texture_function = "texture3D";
7639 coord_mask = "xyz";
7641 break;
7642 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7643 texture_function = "textureCube";
7644 coord_mask = "xyz";
7645 break;
7646 case WINED3D_GL_RES_TYPE_TEX_RECT:
7647 if (proj)
7649 texture_function = "texture2DRectProj";
7650 coord_mask = "xyw";
7652 else
7654 texture_function = "texture2DRect";
7655 coord_mask = "xy";
7657 break;
7658 default:
7659 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7660 texture_function = "";
7661 coord_mask = "xyzw";
7662 break;
7664 if (!needs_legacy_glsl_syntax(gl_info))
7665 texture_function = proj ? "textureProj" : "texture";
7667 if (stage > 0
7668 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7669 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7671 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7673 /* With projective textures, texbem only divides the static
7674 * texture coord, not the displacement, so multiply the
7675 * displacement with the dividing parameter before passing it to
7676 * TXP. */
7677 if (settings->op[stage].projected != proj_none)
7679 if (settings->op[stage].projected == proj_count4)
7681 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7682 stage, stage);
7683 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7685 else
7687 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7688 stage, stage);
7689 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7692 else
7694 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7697 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7698 stage, texture_function, stage, coord_mask);
7700 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7701 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7702 stage, stage - 1, stage - 1, stage - 1);
7704 else if (settings->op[stage].projected == proj_count3)
7706 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7707 stage, texture_function, stage, stage);
7709 else
7711 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7712 stage, texture_function, stage, stage, coord_mask);
7715 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7716 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7717 settings->op[stage].color_fixup);
7720 if (settings->color_key_enabled)
7722 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7723 shader_addline(buffer, " discard;\n");
7726 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7728 /* Generate the main shader */
7729 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7731 BOOL op_equal;
7733 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7734 break;
7736 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7737 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7738 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7739 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7740 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7741 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7742 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7743 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7744 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7745 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7746 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7747 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7748 else
7749 op_equal = settings->op[stage].aop == settings->op[stage].cop
7750 && settings->op[stage].carg0 == settings->op[stage].aarg0
7751 && settings->op[stage].carg1 == settings->op[stage].aarg1
7752 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7754 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7756 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7757 settings->op[stage].cop, settings->op[stage].carg0,
7758 settings->op[stage].carg1, settings->op[stage].carg2);
7760 else if (op_equal)
7762 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7763 settings->op[stage].cop, settings->op[stage].carg0,
7764 settings->op[stage].carg1, settings->op[stage].carg2);
7766 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7767 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7769 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7770 settings->op[stage].cop, settings->op[stage].carg0,
7771 settings->op[stage].carg1, settings->op[stage].carg2);
7772 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7773 settings->op[stage].aop, settings->op[stage].aarg0,
7774 settings->op[stage].aarg1, settings->op[stage].aarg2);
7778 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
7779 get_fragment_output(gl_info));
7781 if (settings->sRGB_write)
7782 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
7784 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
7786 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
7788 shader_addline(buffer, "}\n");
7790 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
7791 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
7793 string_buffer_release(&priv->string_buffers, tex_reg_name);
7794 return shader_id;
7797 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
7798 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
7800 struct glsl_ffp_vertex_shader *shader;
7801 const struct wine_rb_entry *entry;
7803 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
7804 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
7806 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
7807 return NULL;
7809 shader->desc.settings = *settings;
7810 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
7811 list_init(&shader->linked_programs);
7812 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
7813 ERR("Failed to insert ffp vertex shader.\n");
7815 return shader;
7818 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
7819 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
7821 struct glsl_ffp_fragment_shader *glsl_desc;
7822 const struct ffp_frag_desc *desc;
7824 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
7825 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
7827 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
7828 return NULL;
7830 glsl_desc->entry.settings = *args;
7831 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
7832 list_init(&glsl_desc->linked_programs);
7833 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
7835 return glsl_desc;
7839 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
7840 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
7842 unsigned int i;
7843 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7845 for (i = 0; i < vs_c_count; ++i)
7847 string_buffer_sprintf(name, "vs_c[%u]", i);
7848 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7850 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
7852 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7854 string_buffer_sprintf(name, "vs_i[%u]", i);
7855 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7858 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7860 string_buffer_sprintf(name, "vs_b[%u]", i);
7861 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7864 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7866 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
7868 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
7869 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7871 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
7872 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
7873 for (i = 0; i < MAX_TEXTURES; ++i)
7875 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
7876 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7878 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
7879 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
7880 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
7881 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
7882 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
7883 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
7884 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
7886 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
7887 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7888 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
7889 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7890 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
7891 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7892 string_buffer_sprintf(name, "ffp_light[%u].position", i);
7893 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7894 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
7895 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7896 string_buffer_sprintf(name, "ffp_light[%u].range", i);
7897 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7898 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
7899 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7900 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
7901 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7902 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
7903 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7904 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
7905 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7906 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
7907 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7908 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
7909 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7911 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
7912 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
7913 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
7914 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
7915 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
7916 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
7917 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
7919 string_buffer_release(&priv->string_buffers, name);
7922 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
7923 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
7925 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
7928 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
7929 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
7931 unsigned int i;
7932 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7934 for (i = 0; i < ps_c_count; ++i)
7936 string_buffer_sprintf(name, "ps_c[%u]", i);
7937 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7939 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
7941 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
7943 string_buffer_sprintf(name, "ps_i[%u]", i);
7944 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7947 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
7949 string_buffer_sprintf(name, "ps_b[%u]", i);
7950 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7953 for (i = 0; i < MAX_TEXTURES; ++i)
7955 string_buffer_sprintf(name, "bumpenv_mat%u", i);
7956 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7957 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
7958 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7959 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
7960 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7961 string_buffer_sprintf(name, "tss_const%u", i);
7962 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
7965 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
7966 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
7968 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
7969 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
7970 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
7971 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
7973 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
7975 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
7976 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
7977 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
7979 string_buffer_release(&priv->string_buffers, name);
7982 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
7983 struct shader_glsl_priv *priv, GLuint program_id,
7984 const struct wined3d_shader_reg_maps *reg_maps)
7986 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
7987 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
7988 unsigned int i, base, count;
7989 GLuint block_idx;
7991 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
7992 for (i = 0; i < count; ++i)
7994 if (!reg_maps->cb_sizes[i])
7995 continue;
7997 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
7998 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
7999 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8001 checkGLcall("glUniformBlockBinding");
8002 string_buffer_release(&priv->string_buffers, name);
8005 /* Context activation is done by the caller. */
8006 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8007 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8009 const struct wined3d_gl_info *gl_info = context->gl_info;
8010 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8011 const struct ps_np2fixup_info *np2fixup_info = NULL;
8012 struct glsl_shader_prog_link *entry = NULL;
8013 struct wined3d_shader *vshader = NULL;
8014 struct wined3d_shader *gshader = NULL;
8015 struct wined3d_shader *pshader = NULL;
8016 GLuint program_id;
8017 GLuint reorder_shader_id = 0;
8018 unsigned int i;
8019 GLuint vs_id = 0;
8020 GLuint gs_id = 0;
8021 GLuint ps_id = 0;
8022 struct list *ps_list, *vs_list;
8023 WORD attribs_map;
8024 struct wined3d_string_buffer *tmp_name;
8026 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8028 vs_id = ctx_data->glsl_program->vs.id;
8029 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8031 if (use_vs(state))
8033 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8034 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8036 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8037 && ctx_data->glsl_program->gs.id)
8039 gs_id = ctx_data->glsl_program->gs.id;
8041 else if (gshader)
8043 struct gs_compile_args args;
8045 find_gs_compile_args(state, gshader, &args);
8046 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8050 else if (use_vs(state))
8052 struct vs_compile_args vs_compile_args;
8054 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8055 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8057 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8058 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8059 vs_list = &vshader->linked_programs;
8061 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8062 && ctx_data->glsl_program->gs.id)
8064 gs_id = ctx_data->glsl_program->gs.id;
8066 else if (gshader)
8068 struct gs_compile_args gs_compile_args;
8070 find_gs_compile_args(state, gshader, &gs_compile_args);
8071 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8074 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8076 struct glsl_ffp_vertex_shader *ffp_shader;
8077 struct wined3d_ffp_vs_settings settings;
8079 wined3d_ffp_get_vs_settings(context, state, &settings);
8080 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8081 vs_id = ffp_shader->id;
8082 vs_list = &ffp_shader->linked_programs;
8085 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8087 ps_id = ctx_data->glsl_program->ps.id;
8088 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8090 if (use_ps(state))
8091 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8093 else if (use_ps(state))
8095 struct ps_compile_args ps_compile_args;
8096 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8097 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8098 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8099 pshader, &ps_compile_args, &np2fixup_info);
8100 ps_list = &pshader->linked_programs;
8102 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8104 struct glsl_ffp_fragment_shader *ffp_shader;
8105 struct ffp_frag_settings settings;
8107 gen_ffp_frag_op(context, state, &settings, FALSE);
8108 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8109 ps_id = ffp_shader->id;
8110 ps_list = &ffp_shader->linked_programs;
8113 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, vs_id, gs_id, ps_id)))
8115 ctx_data->glsl_program = entry;
8116 return;
8119 /* If we get to this point, then no matching program exists, so we create one */
8120 program_id = GL_EXTCALL(glCreateProgram());
8121 TRACE("Created new GLSL shader program %u.\n", program_id);
8123 /* Create the entry */
8124 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8125 entry->id = program_id;
8126 entry->vs.id = vs_id;
8127 entry->gs.id = gs_id;
8128 entry->ps.id = ps_id;
8129 entry->constant_version = 0;
8130 entry->ps.np2_fixup_info = np2fixup_info;
8131 /* Add the hash table entry */
8132 add_glsl_program_entry(priv, entry);
8134 /* Set the current program */
8135 ctx_data->glsl_program = entry;
8137 /* Attach GLSL vshader */
8138 if (vs_id)
8140 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8141 GL_EXTCALL(glAttachShader(program_id, vs_id));
8142 checkGLcall("glAttachShader");
8144 list_add_head(vs_list, &entry->vs.shader_entry);
8147 if (vshader)
8149 attribs_map = vshader->reg_maps.input_registers;
8150 if (vshader->reg_maps.shader_version.major < 4)
8152 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8153 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8154 d3d_info->emulated_flatshading
8155 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8156 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8157 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8158 checkGLcall("glAttachShader");
8159 /* Flag the reorder function for deletion, it will be freed
8160 * automatically when the program is destroyed. */
8161 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8164 else
8166 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8169 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8171 /* Bind vertex attributes to a corresponding index number to match
8172 * the same index numbers as ARB_vertex_programs (makes loading
8173 * vertex attributes simpler). With this method, we can use the
8174 * exact same code to load the attributes later for both ARB and
8175 * GLSL shaders.
8177 * We have to do this here because we need to know the Program ID
8178 * in order to make the bindings work, and it has to be done prior
8179 * to linking the GLSL program. */
8180 tmp_name = string_buffer_get(&priv->string_buffers);
8181 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8183 if (!(attribs_map & 1))
8184 continue;
8186 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8187 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8188 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8190 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8191 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8192 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8193 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8196 checkGLcall("glBindAttribLocation");
8197 string_buffer_release(&priv->string_buffers, tmp_name);
8200 if (gshader)
8202 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8203 GL_EXTCALL(glAttachShader(program_id, gs_id));
8204 checkGLcall("glAttachShader");
8206 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8208 TRACE("input type %s, output type %s, vertices out %u.\n",
8209 debug_d3dprimitivetype(gshader->u.gs.input_type),
8210 debug_d3dprimitivetype(gshader->u.gs.output_type),
8211 gshader->u.gs.vertices_out);
8212 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8213 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8214 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8215 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8216 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8217 gshader->u.gs.vertices_out));
8218 checkGLcall("glProgramParameteriARB");
8221 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8224 /* Attach GLSL pshader */
8225 if (ps_id)
8227 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8228 GL_EXTCALL(glAttachShader(program_id, ps_id));
8229 checkGLcall("glAttachShader");
8231 list_add_head(ps_list, &entry->ps.shader_entry);
8234 /* Link the program */
8235 TRACE("Linking GLSL shader program %u.\n", program_id);
8236 GL_EXTCALL(glLinkProgram(program_id));
8237 shader_glsl_validate_link(gl_info, program_id);
8239 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8240 vshader ? vshader->limits->constant_float : 0);
8241 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8242 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8243 pshader ? pshader->limits->constant_float : 0);
8244 checkGLcall("Find glsl program uniform locations");
8246 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8248 if (pshader && pshader->reg_maps.shader_version.major >= 3
8249 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8251 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8252 entry->vs.vertex_color_clamp = GL_FALSE;
8254 else
8256 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8259 else
8261 /* With core profile we never change vertex_color_clamp from
8262 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8263 * glClampColorARB(). */
8264 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8267 /* Set the shader to allow uniform loading on it */
8268 GL_EXTCALL(glUseProgram(program_id));
8269 checkGLcall("glUseProgram");
8271 /* Texture unit mapping is set up to be the same each time the shader
8272 * program is used so we can hardcode the sampler uniform values. */
8273 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
8275 entry->constant_update_mask = 0;
8276 if (vshader)
8278 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8279 if (vshader->reg_maps.integer_constants)
8280 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8281 if (vshader->reg_maps.boolean_constants)
8282 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8283 if (entry->vs.pos_fixup_location != -1)
8284 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8286 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8287 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8289 else
8291 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8292 | WINED3D_SHADER_CONST_FFP_PROJ;
8294 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8296 if (entry->vs.modelview_matrix_location[i] != -1)
8298 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8299 break;
8303 for (i = 0; i < MAX_TEXTURES; ++i)
8305 if (entry->vs.texture_matrix_location[i] != -1)
8307 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8308 break;
8311 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
8312 || entry->vs.material_specular_location != -1
8313 || entry->vs.material_emissive_location != -1
8314 || entry->vs.material_shininess_location != -1)
8315 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8316 if (entry->vs.light_ambient_location != -1)
8317 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8319 if (entry->vs.clip_planes_location != -1)
8320 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8321 if (entry->vs.pointsize_min_location != -1)
8322 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8324 if (gshader)
8326 if (entry->gs.pos_fixup_location != -1)
8327 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8328 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8329 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8332 if (ps_id)
8334 if (pshader)
8336 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8337 if (pshader->reg_maps.integer_constants)
8338 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8339 if (pshader->reg_maps.boolean_constants)
8340 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8341 if (entry->ps.ycorrection_location != -1)
8342 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8344 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8345 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8346 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
8348 else
8350 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8353 for (i = 0; i < MAX_TEXTURES; ++i)
8355 if (entry->ps.bumpenv_mat_location[i] != -1)
8357 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8358 break;
8362 if (entry->ps.fog_color_location != -1)
8363 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8364 if (entry->ps.alpha_test_ref_location != -1)
8365 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8366 if (entry->ps.np2_fixup_location != -1)
8367 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8368 if (entry->ps.color_key_location != -1)
8369 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8373 /* Context activation is done by the caller. */
8374 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8375 const struct wined3d_state *state)
8377 struct glsl_context_data *ctx_data = context->shader_backend_data;
8378 const struct wined3d_gl_info *gl_info = context->gl_info;
8379 struct shader_glsl_priv *priv = shader_priv;
8380 GLuint program_id = 0, prev_id = 0;
8381 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
8383 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8384 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8386 if (ctx_data->glsl_program)
8388 prev_id = ctx_data->glsl_program->id;
8389 old_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8391 else
8393 prev_id = 0;
8394 old_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8397 set_glsl_shader_program(context, state, priv, ctx_data);
8399 if (ctx_data->glsl_program)
8401 program_id = ctx_data->glsl_program->id;
8402 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8404 else
8406 program_id = 0;
8407 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8410 if (old_vertex_color_clamp != current_vertex_color_clamp)
8412 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8414 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8415 checkGLcall("glClampColorARB");
8417 else
8419 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
8423 TRACE("Using GLSL program %u.\n", program_id);
8425 if (prev_id != program_id)
8427 GL_EXTCALL(glUseProgram(program_id));
8428 checkGLcall("glUseProgram");
8430 if (program_id)
8431 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8435 /* "context" is not necessarily the currently active context. */
8436 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8438 struct glsl_context_data *ctx_data = context->shader_backend_data;
8440 ctx_data->glsl_program = NULL;
8441 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8442 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8443 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8444 | (1u << WINED3D_SHADER_TYPE_HULL)
8445 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8446 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8449 /* Context activation is done by the caller. */
8450 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8452 const struct wined3d_gl_info *gl_info = context->gl_info;
8453 struct shader_glsl_priv *priv = shader_priv;
8455 shader_glsl_invalidate_current_program(context);
8456 GL_EXTCALL(glUseProgram(0));
8457 checkGLcall("glUseProgram");
8459 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8460 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8462 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8464 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8465 checkGLcall("glClampColorARB");
8469 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8470 const struct glsl_shader_prog_link *program)
8472 const struct glsl_context_data *ctx_data;
8473 struct wined3d_context *context;
8474 unsigned int i;
8476 for (i = 0; i < device->context_count; ++i)
8478 context = device->contexts[i];
8479 ctx_data = context->shader_backend_data;
8481 if (ctx_data->glsl_program == program)
8482 shader_glsl_invalidate_current_program(context);
8486 static void shader_glsl_destroy(struct wined3d_shader *shader)
8488 struct glsl_shader_private *shader_data = shader->backend_data;
8489 struct wined3d_device *device = shader->device;
8490 struct shader_glsl_priv *priv = device->shader_priv;
8491 const struct wined3d_gl_info *gl_info;
8492 const struct list *linked_programs;
8493 struct wined3d_context *context;
8495 if (!shader_data || !shader_data->num_gl_shaders)
8497 HeapFree(GetProcessHeap(), 0, shader_data);
8498 shader->backend_data = NULL;
8499 return;
8502 context = context_acquire(device, NULL);
8503 gl_info = context->gl_info;
8505 TRACE("Deleting linked programs.\n");
8506 linked_programs = &shader->linked_programs;
8507 if (linked_programs->next)
8509 struct glsl_shader_prog_link *entry, *entry2;
8510 UINT i;
8512 switch (shader->reg_maps.shader_version.type)
8514 case WINED3D_SHADER_TYPE_PIXEL:
8516 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8518 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8520 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8521 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8522 checkGLcall("glDeleteShader");
8524 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8526 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8527 struct glsl_shader_prog_link, ps.shader_entry)
8529 shader_glsl_invalidate_contexts_program(device, entry);
8530 delete_glsl_program_entry(priv, gl_info, entry);
8533 break;
8536 case WINED3D_SHADER_TYPE_VERTEX:
8538 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8540 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8542 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8543 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8544 checkGLcall("glDeleteShader");
8546 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8548 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8549 struct glsl_shader_prog_link, vs.shader_entry)
8551 shader_glsl_invalidate_contexts_program(device, entry);
8552 delete_glsl_program_entry(priv, gl_info, entry);
8555 break;
8558 case WINED3D_SHADER_TYPE_GEOMETRY:
8560 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8562 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8564 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8565 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8566 checkGLcall("glDeleteShader");
8568 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8570 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8571 struct glsl_shader_prog_link, gs.shader_entry)
8573 shader_glsl_invalidate_contexts_program(device, entry);
8574 delete_glsl_program_entry(priv, gl_info, entry);
8577 break;
8580 default:
8581 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8582 break;
8586 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8587 shader->backend_data = NULL;
8589 context_release(context);
8592 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8594 const struct glsl_program_key *k = key;
8595 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8596 const struct glsl_shader_prog_link, program_lookup_entry);
8598 if (k->vs_id > prog->vs.id) return 1;
8599 else if (k->vs_id < prog->vs.id) return -1;
8601 if (k->gs_id > prog->gs.id) return 1;
8602 else if (k->gs_id < prog->gs.id) return -1;
8604 if (k->ps_id > prog->ps.id) return 1;
8605 else if (k->ps_id < prog->ps.id) return -1;
8607 return 0;
8610 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8612 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8613 + constant_count * sizeof(*heap->contained)
8614 + constant_count * sizeof(*heap->positions);
8615 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8617 if (!mem)
8619 ERR("Failed to allocate memory\n");
8620 return FALSE;
8623 heap->entries = mem;
8624 heap->entries[1].version = 0;
8625 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8626 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8627 heap->positions = (unsigned int *)(heap->contained + constant_count);
8628 heap->size = 1;
8630 return TRUE;
8633 static void constant_heap_free(struct constant_heap *heap)
8635 HeapFree(GetProcessHeap(), 0, heap->entries);
8638 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8639 const struct fragment_pipeline *fragment_pipe)
8641 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8642 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8643 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8644 struct fragment_caps fragment_caps;
8645 void *vertex_priv, *fragment_priv;
8647 string_buffer_list_init(&priv->string_buffers);
8649 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8651 ERR("Failed to initialize vertex pipe.\n");
8652 HeapFree(GetProcessHeap(), 0, priv);
8653 return E_FAIL;
8656 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
8658 ERR("Failed to initialize fragment pipe.\n");
8659 vertex_pipe->vp_free(device);
8660 HeapFree(GetProcessHeap(), 0, priv);
8661 return E_FAIL;
8664 if (!string_buffer_init(&priv->shader_buffer))
8666 ERR("Failed to initialize shader buffer.\n");
8667 goto fail;
8670 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
8672 ERR("Failed to allocate memory.\n");
8673 goto fail;
8676 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
8678 ERR("Failed to initialize vertex shader constant heap\n");
8679 goto fail;
8682 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
8684 ERR("Failed to initialize pixel shader constant heap\n");
8685 goto fail;
8688 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
8690 priv->next_constant_version = 1;
8691 priv->vertex_pipe = vertex_pipe;
8692 priv->fragment_pipe = fragment_pipe;
8693 fragment_pipe->get_caps(gl_info, &fragment_caps);
8694 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
8695 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
8697 device->vertex_priv = vertex_priv;
8698 device->fragment_priv = fragment_priv;
8699 device->shader_priv = priv;
8701 return WINED3D_OK;
8703 fail:
8704 constant_heap_free(&priv->pconst_heap);
8705 constant_heap_free(&priv->vconst_heap);
8706 HeapFree(GetProcessHeap(), 0, priv->stack);
8707 string_buffer_free(&priv->shader_buffer);
8708 fragment_pipe->free_private(device);
8709 vertex_pipe->vp_free(device);
8710 HeapFree(GetProcessHeap(), 0, priv);
8711 return E_OUTOFMEMORY;
8714 /* Context activation is done by the caller. */
8715 static void shader_glsl_free(struct wined3d_device *device)
8717 struct shader_glsl_priv *priv = device->shader_priv;
8719 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
8720 constant_heap_free(&priv->pconst_heap);
8721 constant_heap_free(&priv->vconst_heap);
8722 HeapFree(GetProcessHeap(), 0, priv->stack);
8723 string_buffer_list_cleanup(&priv->string_buffers);
8724 string_buffer_free(&priv->shader_buffer);
8725 priv->fragment_pipe->free_private(device);
8726 priv->vertex_pipe->vp_free(device);
8728 HeapFree(GetProcessHeap(), 0, device->shader_priv);
8729 device->shader_priv = NULL;
8732 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
8734 return !!(context->shader_backend_data = HeapAlloc(GetProcessHeap(),
8735 HEAP_ZERO_MEMORY, sizeof(struct glsl_context_data)));
8738 static void shader_glsl_free_context_data(struct wined3d_context *context)
8740 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
8743 static void shader_glsl_init_context_state(struct wined3d_context *context)
8745 const struct wined3d_gl_info *gl_info = context->gl_info;
8747 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
8748 checkGLcall("GL_PROGRAM_POINT_SIZE");
8751 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
8753 UINT shader_model;
8755 /* FIXME: Check for the specific extensions required for SM5 support
8756 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
8757 * soon as we introduce them, adjusting the GL / GLSL version checks
8758 * accordingly. */
8759 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
8760 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
8761 && gl_info->supported[ARB_GPU_SHADER5]
8762 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
8763 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
8764 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
8765 shader_model = 5;
8766 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
8767 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
8768 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
8769 shader_model = 4;
8770 /* Support for texldd and texldl instructions in pixel shaders is required
8771 * for SM3. */
8772 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
8773 shader_model = 3;
8774 else
8775 shader_model = 2;
8776 TRACE("Shader model %u.\n", shader_model);
8778 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
8779 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
8780 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
8781 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
8782 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
8783 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
8785 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
8786 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
8787 caps->varying_count = gl_info->limits.glsl_varyings;
8789 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
8790 * Direct3D minimum requirement.
8792 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
8793 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
8795 * The problem is that the refrast clamps temporary results in the shader to
8796 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
8797 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
8798 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
8799 * offer a way to query this.
8801 if (shader_model >= 4)
8802 caps->ps_1x_max_value = FLT_MAX;
8803 else
8804 caps->ps_1x_max_value = 1024.0f;
8806 /* Ideally we'd only set caps like sRGB writes here if supported by both
8807 * the shader backend and the fragment pipe, but we can get called before
8808 * shader_glsl_alloc(). */
8809 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
8810 | WINED3D_SHADER_CAP_SRGB_WRITE;
8813 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
8815 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
8817 TRACE("Checking support for fixup:\n");
8818 dump_color_fixup_desc(fixup);
8821 /* We support everything except YUV conversions. */
8822 if (!is_complex_fixup(fixup))
8824 TRACE("[OK]\n");
8825 return TRUE;
8828 TRACE("[FAILED]\n");
8829 return FALSE;
8832 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
8834 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
8835 /* WINED3DSIH_ADD */ shader_glsl_binop,
8836 /* WINED3DSIH_AND */ shader_glsl_binop,
8837 /* WINED3DSIH_ATOMIC_AND */ NULL,
8838 /* WINED3DSIH_ATOMIC_CMP_STORE */ NULL,
8839 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
8840 /* WINED3DSIH_ATOMIC_IMAX */ NULL,
8841 /* WINED3DSIH_ATOMIC_IMIN */ NULL,
8842 /* WINED3DSIH_ATOMIC_OR */ NULL,
8843 /* WINED3DSIH_ATOMIC_UMAX */ NULL,
8844 /* WINED3DSIH_ATOMIC_UMIN */ NULL,
8845 /* WINED3DSIH_ATOMIC_XOR */ NULL,
8846 /* WINED3DSIH_BEM */ shader_glsl_bem,
8847 /* WINED3DSIH_BFI */ NULL,
8848 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
8849 /* WINED3DSIH_BREAK */ shader_glsl_break,
8850 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
8851 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
8852 /* WINED3DSIH_BUFINFO */ NULL,
8853 /* WINED3DSIH_CALL */ shader_glsl_call,
8854 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
8855 /* WINED3DSIH_CASE */ shader_glsl_case,
8856 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
8857 /* WINED3DSIH_CND */ shader_glsl_cnd,
8858 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
8859 /* WINED3DSIH_CRS */ shader_glsl_cross,
8860 /* WINED3DSIH_CUT */ shader_glsl_cut,
8861 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
8862 /* WINED3DSIH_DCL */ shader_glsl_nop,
8863 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
8864 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
8865 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
8866 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
8867 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
8868 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
8869 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
8870 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
8871 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
8872 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
8873 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
8874 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
8875 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
8876 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
8877 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
8878 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
8879 /* WINED3DSIH_DCL_INTERFACE */ NULL,
8880 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
8881 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
8882 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
8883 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
8884 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
8885 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
8886 /* WINED3DSIH_DCL_STREAM */ NULL,
8887 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
8888 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
8889 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
8890 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
8891 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
8892 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
8893 /* WINED3DSIH_DCL_THREAD_GROUP */ NULL,
8894 /* WINED3DSIH_DCL_UAV_RAW */ NULL,
8895 /* WINED3DSIH_DCL_UAV_STRUCTURED */ NULL,
8896 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
8897 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
8898 /* WINED3DSIH_DEF */ shader_glsl_nop,
8899 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
8900 /* WINED3DSIH_DEFB */ shader_glsl_nop,
8901 /* WINED3DSIH_DEFI */ shader_glsl_nop,
8902 /* WINED3DSIH_DIV */ shader_glsl_binop,
8903 /* WINED3DSIH_DP2 */ shader_glsl_dot,
8904 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
8905 /* WINED3DSIH_DP3 */ shader_glsl_dot,
8906 /* WINED3DSIH_DP4 */ shader_glsl_dot,
8907 /* WINED3DSIH_DST */ shader_glsl_dst,
8908 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
8909 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
8910 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
8911 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
8912 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
8913 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
8914 /* WINED3DSIH_ELSE */ shader_glsl_else,
8915 /* WINED3DSIH_EMIT */ shader_glsl_emit,
8916 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
8917 /* WINED3DSIH_ENDIF */ shader_glsl_end,
8918 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
8919 /* WINED3DSIH_ENDREP */ shader_glsl_end,
8920 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
8921 /* WINED3DSIH_EQ */ shader_glsl_relop,
8922 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
8923 /* WINED3DSIH_EXPP */ shader_glsl_expp,
8924 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
8925 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
8926 /* WINED3DSIH_FCALL */ NULL,
8927 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
8928 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
8929 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
8930 /* WINED3DSIH_GATHER4 */ NULL,
8931 /* WINED3DSIH_GATHER4_C */ NULL,
8932 /* WINED3DSIH_GE */ shader_glsl_relop,
8933 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
8934 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
8935 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
8936 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
8937 /* WINED3DSIH_IADD */ shader_glsl_binop,
8938 /* WINED3DSIH_IEQ */ shader_glsl_relop,
8939 /* WINED3DSIH_IF */ shader_glsl_if,
8940 /* WINED3DSIH_IFC */ shader_glsl_ifc,
8941 /* WINED3DSIH_IGE */ shader_glsl_relop,
8942 /* WINED3DSIH_ILT */ shader_glsl_relop,
8943 /* WINED3DSIH_IMAD */ shader_glsl_mad,
8944 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
8945 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
8946 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
8947 /* WINED3DSIH_IMM_ATOMIC_AND */ NULL,
8948 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ NULL,
8949 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
8950 /* WINED3DSIH_IMM_ATOMIC_EXCH */ NULL,
8951 /* WINED3DSIH_IMM_ATOMIC_OR */ NULL,
8952 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL,
8953 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL,
8954 /* WINED3DSIH_IMM_ATOMIC_XOR */ NULL,
8955 /* WINED3DSIH_IMUL */ shader_glsl_imul,
8956 /* WINED3DSIH_INE */ shader_glsl_relop,
8957 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
8958 /* WINED3DSIH_ISHL */ shader_glsl_binop,
8959 /* WINED3DSIH_ISHR */ shader_glsl_binop,
8960 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
8961 /* WINED3DSIH_LABEL */ shader_glsl_label,
8962 /* WINED3DSIH_LD */ shader_glsl_ld,
8963 /* WINED3DSIH_LD2DMS */ NULL,
8964 /* WINED3DSIH_LD_RAW */ NULL,
8965 /* WINED3DSIH_LD_STRUCTURED */ NULL,
8966 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
8967 /* WINED3DSIH_LIT */ shader_glsl_lit,
8968 /* WINED3DSIH_LOD */ NULL,
8969 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
8970 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
8971 /* WINED3DSIH_LOOP */ shader_glsl_loop,
8972 /* WINED3DSIH_LRP */ shader_glsl_lrp,
8973 /* WINED3DSIH_LT */ shader_glsl_relop,
8974 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
8975 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
8976 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
8977 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
8978 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
8979 /* WINED3DSIH_MAD */ shader_glsl_mad,
8980 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
8981 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
8982 /* WINED3DSIH_MOV */ shader_glsl_mov,
8983 /* WINED3DSIH_MOVA */ shader_glsl_mov,
8984 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
8985 /* WINED3DSIH_MUL */ shader_glsl_binop,
8986 /* WINED3DSIH_NE */ shader_glsl_relop,
8987 /* WINED3DSIH_NOP */ shader_glsl_nop,
8988 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
8989 /* WINED3DSIH_NRM */ shader_glsl_nrm,
8990 /* WINED3DSIH_OR */ shader_glsl_binop,
8991 /* WINED3DSIH_PHASE */ shader_glsl_nop,
8992 /* WINED3DSIH_POW */ shader_glsl_pow,
8993 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
8994 /* WINED3DSIH_REP */ shader_glsl_rep,
8995 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
8996 /* WINED3DSIH_RET */ shader_glsl_ret,
8997 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
8998 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
8999 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9000 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9001 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9002 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9003 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9004 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9005 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9006 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9007 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9008 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9009 /* WINED3DSIH_SAMPLE_POS */ NULL,
9010 /* WINED3DSIH_SETP */ NULL,
9011 /* WINED3DSIH_SGE */ shader_glsl_compare,
9012 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9013 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9014 /* WINED3DSIH_SLT */ shader_glsl_compare,
9015 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9016 /* WINED3DSIH_STORE_RAW */ NULL,
9017 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
9018 /* WINED3DSIH_STORE_UAV_TYPED */ NULL,
9019 /* WINED3DSIH_SUB */ shader_glsl_binop,
9020 /* WINED3DSIH_SWAPC */ NULL,
9021 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9022 /* WINED3DSIH_SYNC */ NULL,
9023 /* WINED3DSIH_TEX */ shader_glsl_tex,
9024 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9025 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9026 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9027 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9028 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9029 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9030 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9031 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9032 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9033 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9034 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9035 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9036 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9037 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9038 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9039 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9040 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9041 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9042 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9043 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9044 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9045 /* WINED3DSIH_UBFE */ NULL,
9046 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9047 /* WINED3DSIH_UGE */ shader_glsl_relop,
9048 /* WINED3DSIH_ULT */ shader_glsl_relop,
9049 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9050 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9051 /* WINED3DSIH_USHR */ shader_glsl_binop,
9052 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9053 /* WINED3DSIH_XOR */ shader_glsl_binop,
9056 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9057 SHADER_HANDLER hw_fct;
9059 /* Select handler */
9060 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9062 /* Unhandled opcode */
9063 if (!hw_fct)
9065 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9066 return;
9068 hw_fct(ins);
9070 shader_glsl_add_instruction_modifiers(ins);
9073 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9075 struct shader_glsl_priv *priv = shader_priv;
9077 return priv->ffp_proj_control;
9080 const struct wined3d_shader_backend_ops glsl_shader_backend =
9082 shader_glsl_handle_instruction,
9083 shader_glsl_select,
9084 shader_glsl_disable,
9085 shader_glsl_update_float_vertex_constants,
9086 shader_glsl_update_float_pixel_constants,
9087 shader_glsl_load_constants,
9088 shader_glsl_destroy,
9089 shader_glsl_alloc,
9090 shader_glsl_free,
9091 shader_glsl_allocate_context_data,
9092 shader_glsl_free_context_data,
9093 shader_glsl_init_context_state,
9094 shader_glsl_get_caps,
9095 shader_glsl_color_fixup_supported,
9096 shader_glsl_has_ffp_proj_control,
9099 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9101 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9103 caps->xyzrhw = TRUE;
9104 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9105 caps->ffp_generic_attributes = TRUE;
9106 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9107 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9108 caps->max_vertex_blend_matrix_index = 0;
9109 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9110 | WINED3DVTXPCAPS_MATERIALSOURCE7
9111 | WINED3DVTXPCAPS_VERTEXFOG
9112 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9113 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9114 | WINED3DVTXPCAPS_LOCALVIEWER
9115 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9116 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9117 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9118 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9121 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9123 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9124 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9125 return 0;
9128 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9130 struct shader_glsl_priv *priv;
9132 if (shader_backend == &glsl_shader_backend)
9134 priv = shader_priv;
9135 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9136 return priv;
9139 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9141 return NULL;
9144 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9146 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9147 struct glsl_ffp_vertex_shader, desc.entry);
9148 struct glsl_shader_prog_link *program, *program2;
9149 struct glsl_ffp_destroy_ctx *ctx = context;
9151 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9152 struct glsl_shader_prog_link, vs.shader_entry)
9154 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9156 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9157 HeapFree(GetProcessHeap(), 0, shader);
9160 /* Context activation is done by the caller. */
9161 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9163 struct shader_glsl_priv *priv = device->vertex_priv;
9164 struct glsl_ffp_destroy_ctx ctx;
9166 ctx.priv = priv;
9167 ctx.gl_info = &device->adapter->gl_info;
9168 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9171 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9172 const struct wined3d_state *state, DWORD state_id) {}
9174 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9175 const struct wined3d_state *state, DWORD state_id)
9177 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9180 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9181 const struct wined3d_state *state, DWORD state_id)
9183 const struct wined3d_gl_info *gl_info = context->gl_info;
9184 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9185 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9186 BOOL transformed = context->stream_info.position_transformed;
9187 BOOL wasrhw = context->last_was_rhw;
9188 unsigned int i;
9190 context->last_was_rhw = transformed;
9192 /* If the vertex declaration contains a transformed position attribute,
9193 * the draw uses the fixed function vertex pipeline regardless of any
9194 * vertex shader set by the application. */
9195 if (transformed != wasrhw
9196 || context->stream_info.swizzle_map != context->last_swizzle_map)
9197 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9199 context->last_swizzle_map = context->stream_info.swizzle_map;
9201 if (!use_vs(state))
9203 if (context->last_was_vshader)
9205 if (legacy_context)
9206 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9207 clipplane(context, state, STATE_CLIPPLANE(i));
9208 else
9209 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9212 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9214 /* Because of settings->texcoords, we have to regenerate the vertex
9215 * shader on a vdecl change if there aren't enough varyings to just
9216 * always output all the texture coordinates. */
9217 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9218 || normal != context->last_was_normal)
9219 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9221 if (use_ps(state)
9222 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9223 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9224 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9226 else
9228 if (!context->last_was_vshader)
9230 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9231 if (legacy_context)
9232 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9233 clipplane(context, state, STATE_CLIPPLANE(i));
9234 else
9235 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9239 context->last_was_vshader = use_vs(state);
9240 context->last_was_normal = normal;
9243 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9244 const struct wined3d_state *state, DWORD state_id)
9246 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9247 /* Different vertex shaders potentially require a different vertex attributes setup. */
9248 if (!isStateDirty(context, STATE_VDECL))
9249 context_apply_state(context, state, STATE_VDECL);
9252 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9253 const struct wined3d_state *state, DWORD state_id)
9255 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9256 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9257 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9260 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9261 const struct wined3d_state *state, DWORD state_id)
9263 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9264 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9265 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9266 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9267 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9270 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9271 const struct wined3d_state *state, DWORD state_id)
9273 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9276 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9277 const struct wined3d_state *state, DWORD state_id)
9279 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9282 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9284 const struct wined3d_gl_info *gl_info = context->gl_info;
9285 unsigned int k;
9287 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9288 | WINED3D_SHADER_CONST_FFP_LIGHTS
9289 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9291 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9293 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9295 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9296 clipplane(context, state, STATE_CLIPPLANE(k));
9299 else
9301 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9305 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9306 const struct wined3d_state *state, DWORD state_id)
9308 /* Table fog behavior depends on the projection matrix. */
9309 if (state->render_states[WINED3D_RS_FOGENABLE]
9310 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9311 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9312 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9315 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9316 const struct wined3d_state *state, DWORD state_id)
9318 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9319 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9320 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9321 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9322 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9323 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9326 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9327 const struct wined3d_state *state, DWORD state_id)
9329 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9332 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9333 const struct wined3d_state *state, DWORD state_id)
9335 DWORD sampler = state_id - STATE_SAMPLER(0);
9336 const struct wined3d_texture *texture = state->textures[sampler];
9337 BOOL np2;
9339 if (!texture)
9340 return;
9342 if (sampler >= MAX_TEXTURES)
9343 return;
9345 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9346 || context->lastWasPow2Texture & (1u << sampler))
9348 if (np2)
9349 context->lastWasPow2Texture |= 1u << sampler;
9350 else
9351 context->lastWasPow2Texture &= ~(1u << sampler);
9353 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9357 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9358 const struct wined3d_state *state, DWORD state_id)
9360 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9363 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9364 const struct wined3d_state *state, DWORD state_id)
9366 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9369 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9370 const struct wined3d_state *state, DWORD state_id)
9372 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9375 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9376 const struct wined3d_state *state, DWORD state_id)
9378 if (!use_vs(state))
9379 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9382 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9383 const struct wined3d_state *state, DWORD state_id)
9385 static unsigned int once;
9387 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9388 FIXME("Non-point sprite points not supported in core profile.\n");
9391 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9392 const struct wined3d_state *state, DWORD state_id)
9394 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9397 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9398 const struct wined3d_state *state, DWORD state_id)
9400 const struct wined3d_gl_info *gl_info = context->gl_info;
9401 UINT index = state_id - STATE_CLIPPLANE(0);
9403 if (index >= gl_info->limits.user_clip_distances)
9404 return;
9406 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9409 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9411 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9412 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9413 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9414 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9415 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9416 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9417 /* Clip planes */
9418 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9419 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9420 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9421 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9422 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9423 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9424 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9425 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9426 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9427 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9428 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9429 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9430 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9431 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9432 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9433 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9434 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9435 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9436 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9437 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9438 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9439 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9440 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9441 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9442 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9443 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9444 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9445 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9446 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9447 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9448 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9449 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9450 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9451 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9452 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9453 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9454 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9455 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9456 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9457 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9458 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9459 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9460 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9461 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9462 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9463 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9464 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9465 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9466 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9467 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9468 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9469 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9470 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9471 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9472 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9473 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9474 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9475 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9476 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9477 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9478 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9479 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9480 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9481 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9482 /* Lights */
9483 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9484 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9485 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9486 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9487 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9488 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9489 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9490 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9491 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9492 /* Viewport */
9493 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9494 /* Transform states */
9495 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9496 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9497 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9498 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9499 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9500 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9501 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9502 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9503 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9504 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9505 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9506 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9507 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9508 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9509 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9510 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9511 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9512 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9513 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9514 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9515 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9516 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9517 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9518 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9519 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9520 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9521 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9522 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9523 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9524 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9525 /* Fog */
9526 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9527 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9528 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9529 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9530 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9531 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9532 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9533 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9534 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9535 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9536 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9537 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9538 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9539 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9540 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9541 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9542 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9543 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9544 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9545 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9546 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9547 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9548 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9549 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9550 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9551 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9552 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9553 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9554 /* NP2 texture matrix fixups. They are not needed if
9555 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9556 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9557 * matrix. */
9558 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9559 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9560 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9561 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9562 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9563 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9564 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9565 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9566 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9567 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9568 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9569 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9570 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9571 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9572 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9573 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9574 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9575 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9576 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9577 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9578 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9579 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9580 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9581 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9582 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9583 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9584 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9585 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9588 /* TODO:
9589 * - Implement vertex tweening. */
9590 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9592 glsl_vertex_pipe_vp_enable,
9593 glsl_vertex_pipe_vp_get_caps,
9594 glsl_vertex_pipe_vp_get_emul_mask,
9595 glsl_vertex_pipe_vp_alloc,
9596 glsl_vertex_pipe_vp_free,
9597 glsl_vertex_pipe_vp_states,
9600 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9602 /* Nothing to do. */
9605 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9607 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9608 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9609 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9610 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9611 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9612 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9613 | WINED3DTEXOPCAPS_SELECTARG1
9614 | WINED3DTEXOPCAPS_SELECTARG2
9615 | WINED3DTEXOPCAPS_MODULATE4X
9616 | WINED3DTEXOPCAPS_MODULATE2X
9617 | WINED3DTEXOPCAPS_MODULATE
9618 | WINED3DTEXOPCAPS_ADDSIGNED2X
9619 | WINED3DTEXOPCAPS_ADDSIGNED
9620 | WINED3DTEXOPCAPS_ADD
9621 | WINED3DTEXOPCAPS_SUBTRACT
9622 | WINED3DTEXOPCAPS_ADDSMOOTH
9623 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9624 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9625 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9626 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9627 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9628 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9629 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9630 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9631 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9632 | WINED3DTEXOPCAPS_DOTPRODUCT3
9633 | WINED3DTEXOPCAPS_MULTIPLYADD
9634 | WINED3DTEXOPCAPS_LERP
9635 | WINED3DTEXOPCAPS_BUMPENVMAP
9636 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9637 caps->MaxTextureBlendStages = MAX_TEXTURES;
9638 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
9641 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
9643 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9644 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9645 return 0;
9648 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9650 struct shader_glsl_priv *priv;
9652 if (shader_backend == &glsl_shader_backend)
9654 priv = shader_priv;
9655 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
9656 return priv;
9659 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
9661 return NULL;
9664 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
9666 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9667 struct glsl_ffp_fragment_shader, entry.entry);
9668 struct glsl_shader_prog_link *program, *program2;
9669 struct glsl_ffp_destroy_ctx *ctx = context;
9671 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9672 struct glsl_shader_prog_link, ps.shader_entry)
9674 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9676 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9677 HeapFree(GetProcessHeap(), 0, shader);
9680 /* Context activation is done by the caller. */
9681 static void glsl_fragment_pipe_free(struct wined3d_device *device)
9683 struct shader_glsl_priv *priv = device->fragment_priv;
9684 struct glsl_ffp_destroy_ctx ctx;
9686 ctx.priv = priv;
9687 ctx.gl_info = &device->adapter->gl_info;
9688 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
9691 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
9692 const struct wined3d_state *state, DWORD state_id)
9694 context->last_was_pshader = use_ps(state);
9696 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9699 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
9700 const struct wined3d_state *state, DWORD state_id)
9702 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9705 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
9706 const struct wined3d_state *state, DWORD state_id)
9708 BOOL use_vshader = use_vs(state);
9709 enum fogsource new_source;
9710 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
9711 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
9713 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9715 if (!state->render_states[WINED3D_RS_FOGENABLE])
9716 return;
9718 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
9720 if (use_vshader)
9721 new_source = FOGSOURCE_VS;
9722 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
9723 new_source = FOGSOURCE_COORD;
9724 else
9725 new_source = FOGSOURCE_FFP;
9727 else
9729 new_source = FOGSOURCE_FFP;
9732 if (new_source != context->fog_source || fogstart == fogend)
9734 context->fog_source = new_source;
9735 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
9739 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
9740 const struct wined3d_state *state, DWORD state_id)
9742 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9743 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9744 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9746 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
9747 glsl_fragment_pipe_fog(context, state, state_id);
9750 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
9751 const struct wined3d_state *state, DWORD state_id)
9753 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
9754 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
9755 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9758 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
9759 const struct wined3d_state *state, DWORD state_id)
9761 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9764 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
9765 const struct wined3d_state *state, DWORD state_id)
9767 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
9770 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
9771 const struct wined3d_state *state, DWORD state_id)
9773 const struct wined3d_gl_info *gl_info = context->gl_info;
9774 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
9775 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
9777 if (func)
9779 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
9780 checkGLcall("glAlphaFunc");
9784 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
9785 const struct wined3d_state *state, DWORD state_id)
9787 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9790 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
9791 const struct wined3d_state *state, DWORD state_id)
9793 const struct wined3d_gl_info *gl_info = context->gl_info;
9795 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
9797 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
9798 checkGLcall("glEnable(GL_ALPHA_TEST)");
9800 else
9802 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
9803 checkGLcall("glDisable(GL_ALPHA_TEST)");
9807 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
9808 const struct wined3d_state *state, DWORD state_id)
9810 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
9813 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
9814 const struct wined3d_state *state, DWORD state_id)
9816 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
9819 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
9820 const struct wined3d_state *state, DWORD state_id)
9822 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9825 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
9827 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9828 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
9829 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9830 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9831 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9832 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9833 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9834 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9835 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9836 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9837 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9838 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9839 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9840 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9841 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9842 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9843 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9844 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9845 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9846 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9847 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9848 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9849 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9850 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9851 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9852 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9853 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9854 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9855 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9856 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9857 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9858 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9859 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9860 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9861 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9862 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9863 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9864 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9865 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9866 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9867 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9868 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9869 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9870 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9871 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9872 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9873 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9874 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9875 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9876 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9877 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9878 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9879 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9880 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9881 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9882 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9883 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9884 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9885 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9886 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9887 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9888 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9889 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9890 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9891 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9892 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9893 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9894 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9895 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9896 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9897 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9898 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9899 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9900 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9901 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9902 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9903 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
9904 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
9905 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
9906 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
9907 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
9908 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
9909 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9910 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
9911 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
9912 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9913 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9914 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9915 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
9916 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
9917 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
9918 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9919 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
9920 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
9921 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
9922 {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 },
9923 {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 },
9924 {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 },
9925 {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 },
9926 {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 },
9927 {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 },
9928 {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 },
9929 {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 },
9930 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9931 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9932 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9933 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9934 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9935 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9936 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9937 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9938 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
9939 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
9940 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
9941 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
9942 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
9945 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
9947 return TRUE;
9950 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
9954 const struct fragment_pipeline glsl_fragment_pipe =
9956 glsl_fragment_pipe_enable,
9957 glsl_fragment_pipe_get_caps,
9958 glsl_fragment_pipe_get_emul_mask,
9959 glsl_fragment_pipe_alloc,
9960 glsl_fragment_pipe_free,
9961 glsl_fragment_pipe_alloc_context_data,
9962 glsl_fragment_pipe_free_context_data,
9963 shader_glsl_color_fixup_supported,
9964 glsl_fragment_pipe_state_template,