wined3d: Implement atomic instructions on structured buffer memory.
[wine.git] / dlls / wined3d / glsl_shader.c
blob0fd463dbf20151efe7664c422dd6a1bedfc0d2d7
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 */
71 {4, 3, ""}, /* WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY */
74 struct glsl_dst_param
76 char reg_name[150];
77 char mask_str[6];
80 struct glsl_src_param
82 char reg_name[150];
83 char param_str[200];
86 struct glsl_sample_function
88 struct wined3d_string_buffer *name;
89 unsigned int coord_mask;
90 unsigned int deriv_mask;
91 enum wined3d_data_type data_type;
92 BOOL output_single_component;
93 unsigned int offset_size;
96 enum heap_node_op
98 HEAP_NODE_TRAVERSE_LEFT,
99 HEAP_NODE_TRAVERSE_RIGHT,
100 HEAP_NODE_POP,
103 struct constant_entry
105 unsigned int idx;
106 unsigned int version;
109 struct constant_heap
111 struct constant_entry *entries;
112 BOOL *contained;
113 unsigned int *positions;
114 unsigned int size;
117 /* GLSL shader private data */
118 struct shader_glsl_priv {
119 struct wined3d_string_buffer shader_buffer;
120 struct wined3d_string_buffer_list string_buffers;
121 struct wine_rb_tree program_lookup;
122 struct constant_heap vconst_heap;
123 struct constant_heap pconst_heap;
124 unsigned char *stack;
125 UINT next_constant_version;
127 const struct wined3d_vertex_pipe_ops *vertex_pipe;
128 const struct fragment_pipeline *fragment_pipe;
129 struct wine_rb_tree ffp_vertex_shaders;
130 struct wine_rb_tree ffp_fragment_shaders;
131 BOOL ffp_proj_control;
132 BOOL legacy_lighting;
135 struct glsl_vs_program
137 struct list shader_entry;
138 GLuint id;
139 GLenum vertex_color_clamp;
140 GLint uniform_f_locations[WINED3D_MAX_VS_CONSTS_F];
141 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
142 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
143 GLint pos_fixup_location;
145 GLint modelview_matrix_location[MAX_VERTEX_BLENDS];
146 GLint projection_matrix_location;
147 GLint normal_matrix_location;
148 GLint texture_matrix_location[MAX_TEXTURES];
149 GLint material_ambient_location;
150 GLint material_diffuse_location;
151 GLint material_specular_location;
152 GLint material_emissive_location;
153 GLint material_shininess_location;
154 GLint light_ambient_location;
155 struct
157 GLint diffuse;
158 GLint specular;
159 GLint ambient;
160 GLint position;
161 GLint direction;
162 GLint range;
163 GLint falloff;
164 GLint c_att;
165 GLint l_att;
166 GLint q_att;
167 GLint cos_htheta;
168 GLint cos_hphi;
169 } light_location[MAX_ACTIVE_LIGHTS];
170 GLint pointsize_location;
171 GLint pointsize_min_location;
172 GLint pointsize_max_location;
173 GLint pointsize_c_att_location;
174 GLint pointsize_l_att_location;
175 GLint pointsize_q_att_location;
176 GLint clip_planes_location;
179 struct glsl_gs_program
181 struct list shader_entry;
182 GLuint id;
184 GLint pos_fixup_location;
187 struct glsl_ps_program
189 struct list shader_entry;
190 GLuint id;
191 GLint uniform_f_locations[WINED3D_MAX_PS_CONSTS_F];
192 GLint uniform_i_locations[WINED3D_MAX_CONSTS_I];
193 GLint uniform_b_locations[WINED3D_MAX_CONSTS_B];
194 GLint bumpenv_mat_location[MAX_TEXTURES];
195 GLint bumpenv_lum_scale_location[MAX_TEXTURES];
196 GLint bumpenv_lum_offset_location[MAX_TEXTURES];
197 GLint tss_constant_location[MAX_TEXTURES];
198 GLint tex_factor_location;
199 GLint specular_enable_location;
200 GLint fog_color_location;
201 GLint fog_density_location;
202 GLint fog_end_location;
203 GLint fog_scale_location;
204 GLint alpha_test_ref_location;
205 GLint ycorrection_location;
206 GLint np2_fixup_location;
207 GLint color_key_location;
208 const struct ps_np2fixup_info *np2_fixup_info;
211 struct glsl_cs_program
213 struct list shader_entry;
214 GLuint id;
217 /* Struct to maintain data about a linked GLSL program */
218 struct glsl_shader_prog_link
220 struct wine_rb_entry program_lookup_entry;
221 struct glsl_vs_program vs;
222 struct glsl_gs_program gs;
223 struct glsl_ps_program ps;
224 struct glsl_cs_program cs;
225 GLuint id;
226 DWORD constant_update_mask;
227 UINT constant_version;
230 struct glsl_program_key
232 GLuint vs_id;
233 GLuint gs_id;
234 GLuint ps_id;
235 GLuint cs_id;
238 struct shader_glsl_ctx_priv {
239 const struct vs_compile_args *cur_vs_args;
240 const struct ps_compile_args *cur_ps_args;
241 struct ps_np2fixup_info *cur_np2fixup_info;
242 struct wined3d_string_buffer_list *string_buffers;
245 struct glsl_context_data
247 struct glsl_shader_prog_link *glsl_program;
248 GLenum vertex_color_clamp;
251 struct glsl_ps_compiled_shader
253 struct ps_compile_args args;
254 struct ps_np2fixup_info np2fixup;
255 GLuint id;
258 struct glsl_vs_compiled_shader
260 struct vs_compile_args args;
261 GLuint id;
264 struct glsl_gs_compiled_shader
266 struct gs_compile_args args;
267 GLuint id;
270 struct glsl_cs_compiled_shader
272 GLuint id;
275 struct glsl_shader_private
277 union
279 struct glsl_vs_compiled_shader *vs;
280 struct glsl_gs_compiled_shader *gs;
281 struct glsl_ps_compiled_shader *ps;
282 struct glsl_cs_compiled_shader *cs;
283 } gl_shaders;
284 UINT num_gl_shaders, shader_array_size;
287 struct glsl_ffp_vertex_shader
289 struct wined3d_ffp_vs_desc desc;
290 GLuint id;
291 struct list linked_programs;
294 struct glsl_ffp_fragment_shader
296 struct ffp_frag_desc entry;
297 GLuint id;
298 struct list linked_programs;
301 struct glsl_ffp_destroy_ctx
303 struct shader_glsl_priv *priv;
304 const struct wined3d_gl_info *gl_info;
307 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx);
309 static const char *debug_gl_shader_type(GLenum type)
311 switch (type)
313 #define WINED3D_TO_STR(u) case u: return #u
314 WINED3D_TO_STR(GL_VERTEX_SHADER);
315 WINED3D_TO_STR(GL_TESS_CONTROL_SHADER);
316 WINED3D_TO_STR(GL_TESS_EVALUATION_SHADER);
317 WINED3D_TO_STR(GL_GEOMETRY_SHADER);
318 WINED3D_TO_STR(GL_FRAGMENT_SHADER);
319 WINED3D_TO_STR(GL_COMPUTE_SHADER);
320 #undef WINED3D_TO_STR
321 default:
322 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
326 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
328 switch (type)
330 case WINED3D_SHADER_TYPE_VERTEX:
331 return "vs";
333 case WINED3D_SHADER_TYPE_HULL:
334 return "hs";
336 case WINED3D_SHADER_TYPE_DOMAIN:
337 return "ds";
339 case WINED3D_SHADER_TYPE_GEOMETRY:
340 return "gs";
342 case WINED3D_SHADER_TYPE_PIXEL:
343 return "ps";
345 case WINED3D_SHADER_TYPE_COMPUTE:
346 return "cs";
348 default:
349 FIXME("Unhandled shader type %#x.\n", type);
350 return "unknown";
354 static unsigned int shader_glsl_get_version(const struct wined3d_gl_info *gl_info,
355 const struct wined3d_shader_version *version)
357 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
358 || (version && version->type == WINED3D_SHADER_TYPE_COMPUTE))
359 return 150;
360 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 30) && version && version->major >= 4)
361 return 130;
362 else
363 return 120;
366 static void shader_glsl_add_version_declaration(struct wined3d_string_buffer *buffer,
367 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_version *version)
369 unsigned int glsl_version = shader_glsl_get_version(gl_info, version);
370 if (glsl_version >= 150 && gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
371 shader_addline(buffer, "#version %u compatibility\n", glsl_version);
372 else
373 shader_addline(buffer, "#version %u\n", glsl_version);
376 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
378 char str[4][17];
380 wined3d_ftoa(values[0], str[0]);
381 wined3d_ftoa(values[1], str[1]);
382 wined3d_ftoa(values[2], str[2]);
383 wined3d_ftoa(values[3], str[3]);
384 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
387 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
388 const int *values, unsigned int size)
390 int i;
392 if (!size || size > 4)
394 ERR("Invalid vector size %u.\n", size);
395 return;
398 if (size > 1)
399 shader_addline(buffer, "ivec%u(", size);
401 for (i = 0; i < size; ++i)
402 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
404 if (size > 1)
405 shader_addline(buffer, ")");
408 static const char *get_info_log_line(const char **ptr)
410 const char *p, *q;
412 p = *ptr;
413 if (!(q = strstr(p, "\n")))
415 if (!*p) return NULL;
416 *ptr += strlen(p);
417 return p;
419 *ptr = q + 1;
421 return p;
424 /* Context activation is done by the caller. */
425 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
427 int length = 0;
428 char *log;
430 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
431 return;
433 if (program)
434 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
435 else
436 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
438 /* A size of 1 is just a null-terminated string, so the log should be bigger than
439 * that if there are errors. */
440 if (length > 1)
442 const char *ptr, *line;
444 log = HeapAlloc(GetProcessHeap(), 0, length);
445 /* The info log is supposed to be zero-terminated, but at least some
446 * versions of fglrx don't terminate the string properly. The reported
447 * length does include the terminator, so explicitly set it to zero
448 * here. */
449 log[length - 1] = 0;
450 if (program)
451 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
452 else
453 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
455 ptr = log;
456 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
458 WARN("Info log received from GLSL shader #%u:\n", id);
459 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
461 else
463 FIXME("Info log received from GLSL shader #%u:\n", id);
464 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
466 HeapFree(GetProcessHeap(), 0, log);
470 /* Context activation is done by the caller. */
471 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
473 const char *ptr, *line;
475 TRACE("Compiling shader object %u.\n", shader);
477 if (TRACE_ON(d3d_shader))
479 ptr = src;
480 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
483 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
484 checkGLcall("glShaderSource");
485 GL_EXTCALL(glCompileShader(shader));
486 checkGLcall("glCompileShader");
487 print_glsl_info_log(gl_info, shader, FALSE);
490 /* Context activation is done by the caller. */
491 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
493 GLint i, shader_count, source_size = -1;
494 GLuint *shaders;
495 char *source = NULL;
497 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
498 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
500 ERR("Failed to allocate shader array memory.\n");
501 return;
504 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
505 for (i = 0; i < shader_count; ++i)
507 const char *ptr, *line;
508 GLint tmp;
510 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
512 if (source_size < tmp)
514 HeapFree(GetProcessHeap(), 0, source);
516 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
517 if (!source)
519 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
520 HeapFree(GetProcessHeap(), 0, shaders);
521 return;
523 source_size = tmp;
526 FIXME("Shader %u:\n", shaders[i]);
527 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
528 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
529 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
530 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
531 FIXME("\n");
533 ptr = source;
534 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
535 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
536 FIXME("\n");
539 HeapFree(GetProcessHeap(), 0, source);
540 HeapFree(GetProcessHeap(), 0, shaders);
543 /* Context activation is done by the caller. */
544 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
546 GLint tmp;
548 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
549 return;
551 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
552 if (!tmp)
554 FIXME("Program %u link status invalid.\n", program);
555 shader_glsl_dump_program_source(gl_info, program);
558 print_glsl_info_log(gl_info, program, TRUE);
561 /* Context activation is done by the caller. */
562 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
563 struct shader_glsl_priv *priv, const char *prefix, unsigned int base_idx,
564 unsigned int count, const DWORD *tex_unit_map, GLuint program_id)
566 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
567 unsigned int mapped_unit, i;
568 GLint name_loc;
570 for (i = 0; i < count; ++i)
572 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, i);
573 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
574 if (name_loc == -1)
575 continue;
577 mapped_unit = tex_unit_map ? tex_unit_map[base_idx + i] : base_idx + i;
578 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
580 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
581 continue;
584 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
585 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
587 checkGLcall("Load sampler bindings");
588 string_buffer_release(&priv->string_buffers, sampler_name);
591 /* Context activation is done by the caller. */
592 static void shader_glsl_load_graphics_samplers(const struct wined3d_gl_info *gl_info,
593 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
595 const char *prefix;
596 unsigned int i;
598 static const struct
600 enum wined3d_shader_type type;
601 unsigned int base_idx;
602 unsigned int count;
604 sampler_info[] =
606 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
607 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
610 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
612 prefix = shader_glsl_get_prefix(sampler_info[i].type);
613 shader_glsl_load_samplers(gl_info, priv, prefix,
614 sampler_info[i].base_idx, sampler_info[i].count, tex_unit_map, program_id);
618 static void shader_glsl_load_icb(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
619 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
621 const struct wined3d_shader_immediate_constant_buffer *icb = reg_maps->icb;
623 if (icb)
625 struct wined3d_string_buffer *icb_name = string_buffer_get(&priv->string_buffers);
626 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
627 GLint icb_location;
629 string_buffer_sprintf(icb_name, "%s_icb", prefix);
630 icb_location = GL_EXTCALL(glGetUniformLocation(program_id, icb_name->buffer));
631 GL_EXTCALL(glUniform4fv(icb_location, icb->vec4_count, (const GLfloat *)icb->data));
632 checkGLcall("Load immediate constant buffer");
634 string_buffer_release(&priv->string_buffers, icb_name);
638 /* Context activation is done by the caller. */
639 static void shader_glsl_load_images(const struct wined3d_gl_info *gl_info, struct shader_glsl_priv *priv,
640 GLuint program_id, const struct wined3d_shader_reg_maps *reg_maps)
642 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
643 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
644 GLint location;
645 unsigned int i;
647 for (i = 0; i < MAX_UNORDERED_ACCESS_VIEWS; ++i)
649 if (!reg_maps->uav_resource_info[i].type)
650 continue;
652 string_buffer_sprintf(name, "%s_image%u", prefix, i);
653 location = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
654 if (location == -1)
655 continue;
657 TRACE("Loading image %s on unit %u.\n", name->buffer, i);
658 GL_EXTCALL(glUniform1i(location, i));
660 checkGLcall("Load image bindings");
661 string_buffer_release(&priv->string_buffers, name);
664 /* Context activation is done by the caller. */
665 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const struct wined3d_vec4 *constants,
666 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
668 unsigned int start = ~0U, end = 0;
669 int stack_idx = 0;
670 unsigned int heap_idx = 1;
671 unsigned int idx;
673 if (heap->entries[heap_idx].version <= version) return;
675 idx = heap->entries[heap_idx].idx;
676 if (constant_locations[idx] != -1)
677 start = end = idx;
678 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
680 while (stack_idx >= 0)
682 /* Note that we fall through to the next case statement. */
683 switch(stack[stack_idx])
685 case HEAP_NODE_TRAVERSE_LEFT:
687 unsigned int left_idx = heap_idx << 1;
688 if (left_idx < heap->size && heap->entries[left_idx].version > version)
690 heap_idx = left_idx;
691 idx = heap->entries[heap_idx].idx;
692 if (constant_locations[idx] != -1)
694 if (start > idx)
695 start = idx;
696 if (end < idx)
697 end = idx;
700 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
701 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
702 break;
706 case HEAP_NODE_TRAVERSE_RIGHT:
708 unsigned int right_idx = (heap_idx << 1) + 1;
709 if (right_idx < heap->size && heap->entries[right_idx].version > version)
711 heap_idx = right_idx;
712 idx = heap->entries[heap_idx].idx;
713 if (constant_locations[idx] != -1)
715 if (start > idx)
716 start = idx;
717 if (end < idx)
718 end = idx;
721 stack[stack_idx++] = HEAP_NODE_POP;
722 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
723 break;
727 case HEAP_NODE_POP:
728 heap_idx >>= 1;
729 --stack_idx;
730 break;
733 if (start <= end)
734 GL_EXTCALL(glUniform4fv(constant_locations[start], end - start + 1, &constants[start].x));
735 checkGLcall("walk_constant_heap()");
738 /* Context activation is done by the caller. */
739 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info,
740 GLint location, const struct wined3d_vec4 *data)
742 GLfloat clamped_constant[4];
744 if (location == -1) return;
746 clamped_constant[0] = data->x < -1.0f ? -1.0f : data->x > 1.0f ? 1.0f : data->x;
747 clamped_constant[1] = data->y < -1.0f ? -1.0f : data->y > 1.0f ? 1.0f : data->y;
748 clamped_constant[2] = data->z < -1.0f ? -1.0f : data->z > 1.0f ? 1.0f : data->z;
749 clamped_constant[3] = data->w < -1.0f ? -1.0f : data->w > 1.0f ? 1.0f : data->w;
751 GL_EXTCALL(glUniform4fv(location, 1, clamped_constant));
754 /* Context activation is done by the caller. */
755 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info,
756 const struct wined3d_vec4 *constants, const GLint *constant_locations,
757 const struct constant_heap *heap, unsigned char *stack, DWORD version)
759 int stack_idx = 0;
760 unsigned int heap_idx = 1;
761 unsigned int idx;
763 if (heap->entries[heap_idx].version <= version) return;
765 idx = heap->entries[heap_idx].idx;
766 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
767 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
769 while (stack_idx >= 0)
771 /* Note that we fall through to the next case statement. */
772 switch(stack[stack_idx])
774 case HEAP_NODE_TRAVERSE_LEFT:
776 unsigned int left_idx = heap_idx << 1;
777 if (left_idx < heap->size && heap->entries[left_idx].version > version)
779 heap_idx = left_idx;
780 idx = heap->entries[heap_idx].idx;
781 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
783 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
784 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
785 break;
789 case HEAP_NODE_TRAVERSE_RIGHT:
791 unsigned int right_idx = (heap_idx << 1) + 1;
792 if (right_idx < heap->size && heap->entries[right_idx].version > version)
794 heap_idx = right_idx;
795 idx = heap->entries[heap_idx].idx;
796 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx]);
798 stack[stack_idx++] = HEAP_NODE_POP;
799 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
800 break;
804 case HEAP_NODE_POP:
805 heap_idx >>= 1;
806 --stack_idx;
807 break;
810 checkGLcall("walk_constant_heap_clamped()");
813 /* Context activation is done by the caller. */
814 static void shader_glsl_load_constants_f(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
815 const struct wined3d_vec4 *constants, const GLint *constant_locations, const struct constant_heap *heap,
816 unsigned char *stack, unsigned int version)
818 const struct wined3d_shader_lconst *lconst;
820 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
821 if (shader->reg_maps.shader_version.major == 1
822 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
823 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
824 else
825 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
827 if (!shader->load_local_constsF)
829 TRACE("No need to load local float constants for this shader.\n");
830 return;
833 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
834 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
836 GL_EXTCALL(glUniform4fv(constant_locations[lconst->idx], 1, (const GLfloat *)lconst->value));
838 checkGLcall("glUniform4fv()");
841 /* Context activation is done by the caller. */
842 static void shader_glsl_load_constants_i(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
843 const struct wined3d_ivec4 *constants, const GLint locations[WINED3D_MAX_CONSTS_I], WORD constants_set)
845 unsigned int i;
846 struct list* ptr;
848 for (i = 0; constants_set; constants_set >>= 1, ++i)
850 if (!(constants_set & 1)) continue;
852 /* We found this uniform name in the program - go ahead and send the data */
853 GL_EXTCALL(glUniform4iv(locations[i], 1, &constants[i].x));
856 /* Load immediate constants */
857 ptr = list_head(&shader->constantsI);
858 while (ptr)
860 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
861 unsigned int idx = lconst->idx;
862 const GLint *values = (const GLint *)lconst->value;
864 /* We found this uniform name in the program - go ahead and send the data */
865 GL_EXTCALL(glUniform4iv(locations[idx], 1, values));
866 ptr = list_next(&shader->constantsI, ptr);
868 checkGLcall("glUniform4iv()");
871 /* Context activation is done by the caller. */
872 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
873 const GLint locations[WINED3D_MAX_CONSTS_B], const BOOL *constants, WORD constants_set)
875 unsigned int i;
876 struct list* ptr;
878 for (i = 0; constants_set; constants_set >>= 1, ++i)
880 if (!(constants_set & 1)) continue;
882 GL_EXTCALL(glUniform1iv(locations[i], 1, &constants[i]));
885 /* Load immediate constants */
886 ptr = list_head(&shader->constantsB);
887 while (ptr)
889 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
890 unsigned int idx = lconst->idx;
891 const GLint *values = (const GLint *)lconst->value;
893 GL_EXTCALL(glUniform1iv(locations[idx], 1, values));
894 ptr = list_next(&shader->constantsB, ptr);
896 checkGLcall("glUniform1iv()");
899 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
901 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
904 /* Context activation is done by the caller (state handler). */
905 static void shader_glsl_load_np2fixup_constants(const struct glsl_ps_program *ps,
906 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
908 struct
910 float sx, sy;
912 np2fixup_constants[MAX_FRAGMENT_SAMPLERS];
913 UINT fixup = ps->np2_fixup_info->active;
914 UINT i;
916 for (i = 0; fixup; fixup >>= 1, ++i)
918 const struct wined3d_texture *tex = state->textures[i];
919 unsigned char idx = ps->np2_fixup_info->idx[i];
921 if (!tex)
923 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
924 continue;
927 np2fixup_constants[idx].sx = tex->pow2_matrix[0];
928 np2fixup_constants[idx].sy = tex->pow2_matrix[5];
931 GL_EXTCALL(glUniform4fv(ps->np2_fixup_location, ps->np2_fixup_info->num_consts, &np2fixup_constants[0].sx));
934 /* Taken and adapted from Mesa. */
935 static BOOL invert_matrix_3d(struct wined3d_matrix *out, const struct wined3d_matrix *in)
937 float pos, neg, t, det;
938 struct wined3d_matrix temp;
940 /* Calculate the determinant of upper left 3x3 submatrix and
941 * determine if the matrix is singular. */
942 pos = neg = 0.0f;
943 t = in->_11 * in->_22 * in->_33;
944 if (t >= 0.0f)
945 pos += t;
946 else
947 neg += t;
949 t = in->_21 * in->_32 * in->_13;
950 if (t >= 0.0f)
951 pos += t;
952 else
953 neg += t;
954 t = in->_31 * in->_12 * in->_23;
955 if (t >= 0.0f)
956 pos += t;
957 else
958 neg += t;
960 t = -in->_31 * in->_22 * in->_13;
961 if (t >= 0.0f)
962 pos += t;
963 else
964 neg += t;
965 t = -in->_21 * in->_12 * in->_33;
966 if (t >= 0.0f)
967 pos += t;
968 else
969 neg += t;
971 t = -in->_11 * in->_32 * in->_23;
972 if (t >= 0.0f)
973 pos += t;
974 else
975 neg += t;
977 det = pos + neg;
979 if (fabsf(det) < 1e-25f)
980 return FALSE;
982 det = 1.0f / det;
983 temp._11 = (in->_22 * in->_33 - in->_32 * in->_23) * det;
984 temp._12 = -(in->_12 * in->_33 - in->_32 * in->_13) * det;
985 temp._13 = (in->_12 * in->_23 - in->_22 * in->_13) * det;
986 temp._21 = -(in->_21 * in->_33 - in->_31 * in->_23) * det;
987 temp._22 = (in->_11 * in->_33 - in->_31 * in->_13) * det;
988 temp._23 = -(in->_11 * in->_23 - in->_21 * in->_13) * det;
989 temp._31 = (in->_21 * in->_32 - in->_31 * in->_22) * det;
990 temp._32 = -(in->_11 * in->_32 - in->_31 * in->_12) * det;
991 temp._33 = (in->_11 * in->_22 - in->_21 * in->_12) * det;
993 *out = temp;
994 return TRUE;
997 static void swap_rows(float **a, float **b)
999 float *tmp = *a;
1001 *a = *b;
1002 *b = tmp;
1005 static BOOL invert_matrix(struct wined3d_matrix *out, struct wined3d_matrix *m)
1007 float wtmp[4][8];
1008 float m0, m1, m2, m3, s;
1009 float *r0, *r1, *r2, *r3;
1011 r0 = wtmp[0];
1012 r1 = wtmp[1];
1013 r2 = wtmp[2];
1014 r3 = wtmp[3];
1016 r0[0] = m->_11;
1017 r0[1] = m->_12;
1018 r0[2] = m->_13;
1019 r0[3] = m->_14;
1020 r0[4] = 1.0f;
1021 r0[5] = r0[6] = r0[7] = 0.0f;
1023 r1[0] = m->_21;
1024 r1[1] = m->_22;
1025 r1[2] = m->_23;
1026 r1[3] = m->_24;
1027 r1[5] = 1.0f;
1028 r1[4] = r1[6] = r1[7] = 0.0f;
1030 r2[0] = m->_31;
1031 r2[1] = m->_32;
1032 r2[2] = m->_33;
1033 r2[3] = m->_34;
1034 r2[6] = 1.0f;
1035 r2[4] = r2[5] = r2[7] = 0.0f;
1037 r3[0] = m->_41;
1038 r3[1] = m->_42;
1039 r3[2] = m->_43;
1040 r3[3] = m->_44;
1041 r3[7] = 1.0f;
1042 r3[4] = r3[5] = r3[6] = 0.0f;
1044 /* Choose pivot - or die. */
1045 if (fabsf(r3[0]) > fabsf(r2[0]))
1046 swap_rows(&r3, &r2);
1047 if (fabsf(r2[0]) > fabsf(r1[0]))
1048 swap_rows(&r2, &r1);
1049 if (fabsf(r1[0]) > fabsf(r0[0]))
1050 swap_rows(&r1, &r0);
1051 if (r0[0] == 0.0f)
1052 return FALSE;
1054 /* Eliminate first variable. */
1055 m1 = r1[0] / r0[0]; m2 = r2[0] / r0[0]; m3 = r3[0] / r0[0];
1056 s = r0[1]; r1[1] -= m1 * s; r2[1] -= m2 * s; r3[1] -= m3 * s;
1057 s = r0[2]; r1[2] -= m1 * s; r2[2] -= m2 * s; r3[2] -= m3 * s;
1058 s = r0[3]; r1[3] -= m1 * s; r2[3] -= m2 * s; r3[3] -= m3 * s;
1059 s = r0[4];
1060 if (s != 0.0f)
1062 r1[4] -= m1 * s;
1063 r2[4] -= m2 * s;
1064 r3[4] -= m3 * s;
1066 s = r0[5];
1067 if (s != 0.0f)
1069 r1[5] -= m1 * s;
1070 r2[5] -= m2 * s;
1071 r3[5] -= m3 * s;
1073 s = r0[6];
1074 if (s != 0.0f)
1076 r1[6] -= m1 * s;
1077 r2[6] -= m2 * s;
1078 r3[6] -= m3 * s;
1080 s = r0[7];
1081 if (s != 0.0f)
1083 r1[7] -= m1 * s;
1084 r2[7] -= m2 * s;
1085 r3[7] -= m3 * s;
1088 /* Choose pivot - or die. */
1089 if (fabsf(r3[1]) > fabsf(r2[1]))
1090 swap_rows(&r3, &r2);
1091 if (fabsf(r2[1]) > fabsf(r1[1]))
1092 swap_rows(&r2, &r1);
1093 if (r1[1] == 0.0f)
1094 return FALSE;
1096 /* Eliminate second variable. */
1097 m2 = r2[1] / r1[1]; m3 = r3[1] / r1[1];
1098 r2[2] -= m2 * r1[2]; r3[2] -= m3 * r1[2];
1099 r2[3] -= m2 * r1[3]; r3[3] -= m3 * r1[3];
1100 s = r1[4];
1101 if (s != 0.0f)
1103 r2[4] -= m2 * s;
1104 r3[4] -= m3 * s;
1106 s = r1[5];
1107 if (s != 0.0f)
1109 r2[5] -= m2 * s;
1110 r3[5] -= m3 * s;
1112 s = r1[6];
1113 if (s != 0.0f)
1115 r2[6] -= m2 * s;
1116 r3[6] -= m3 * s;
1118 s = r1[7];
1119 if (s != 0.0f)
1121 r2[7] -= m2 * s;
1122 r3[7] -= m3 * s;
1125 /* Choose pivot - or die. */
1126 if (fabsf(r3[2]) > fabsf(r2[2]))
1127 swap_rows(&r3, &r2);
1128 if (r2[2] == 0.0f)
1129 return FALSE;
1131 /* Eliminate third variable. */
1132 m3 = r3[2] / r2[2];
1133 r3[3] -= m3 * r2[3];
1134 r3[4] -= m3 * r2[4];
1135 r3[5] -= m3 * r2[5];
1136 r3[6] -= m3 * r2[6];
1137 r3[7] -= m3 * r2[7];
1139 /* Last check. */
1140 if (r3[3] == 0.0f)
1141 return FALSE;
1143 /* Back substitute row 3. */
1144 s = 1.0f / r3[3];
1145 r3[4] *= s;
1146 r3[5] *= s;
1147 r3[6] *= s;
1148 r3[7] *= s;
1150 /* Back substitute row 2. */
1151 m2 = r2[3];
1152 s = 1.0f / r2[2];
1153 r2[4] = s * (r2[4] - r3[4] * m2);
1154 r2[5] = s * (r2[5] - r3[5] * m2);
1155 r2[6] = s * (r2[6] - r3[6] * m2);
1156 r2[7] = s * (r2[7] - r3[7] * m2);
1157 m1 = r1[3];
1158 r1[4] -= r3[4] * m1;
1159 r1[5] -= r3[5] * m1;
1160 r1[6] -= r3[6] * m1;
1161 r1[7] -= r3[7] * m1;
1162 m0 = r0[3];
1163 r0[4] -= r3[4] * m0;
1164 r0[5] -= r3[5] * m0;
1165 r0[6] -= r3[6] * m0;
1166 r0[7] -= r3[7] * m0;
1168 /* Back substitute row 1. */
1169 m1 = r1[2];
1170 s = 1.0f / r1[1];
1171 r1[4] = s * (r1[4] - r2[4] * m1);
1172 r1[5] = s * (r1[5] - r2[5] * m1);
1173 r1[6] = s * (r1[6] - r2[6] * m1);
1174 r1[7] = s * (r1[7] - r2[7] * m1);
1175 m0 = r0[2];
1176 r0[4] -= r2[4] * m0;
1177 r0[5] -= r2[5] * m0;
1178 r0[6] -= r2[6] * m0;
1179 r0[7] -= r2[7] * m0;
1181 /* Back substitute row 0. */
1182 m0 = r0[1];
1183 s = 1.0f / r0[0];
1184 r0[4] = s * (r0[4] - r1[4] * m0);
1185 r0[5] = s * (r0[5] - r1[5] * m0);
1186 r0[6] = s * (r0[6] - r1[6] * m0);
1187 r0[7] = s * (r0[7] - r1[7] * m0);
1189 out->_11 = r0[4];
1190 out->_12 = r0[5];
1191 out->_13 = r0[6];
1192 out->_14 = r0[7];
1193 out->_21 = r1[4];
1194 out->_22 = r1[5];
1195 out->_23 = r1[6];
1196 out->_24 = r1[7];
1197 out->_31 = r2[4];
1198 out->_32 = r2[5];
1199 out->_33 = r2[6];
1200 out->_34 = r2[7];
1201 out->_41 = r3[4];
1202 out->_42 = r3[5];
1203 out->_43 = r3[6];
1204 out->_44 = r3[7];
1206 return TRUE;
1209 static void shader_glsl_ffp_vertex_normalmatrix_uniform(const struct wined3d_context *context,
1210 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1212 const struct wined3d_gl_info *gl_info = context->gl_info;
1213 float mat[3 * 3];
1214 struct wined3d_matrix mv;
1215 unsigned int i, j;
1217 if (prog->vs.normal_matrix_location == -1)
1218 return;
1220 get_modelview_matrix(context, state, 0, &mv);
1221 if (context->d3d_info->wined3d_creation_flags & WINED3D_LEGACY_FFP_LIGHTING)
1222 invert_matrix_3d(&mv, &mv);
1223 else
1224 invert_matrix(&mv, &mv);
1225 /* Tests show that singular modelview matrices are used unchanged as normal
1226 * matrices on D3D3 and older. There seems to be no clearly consistent
1227 * behavior on newer D3D versions so always follow older ddraw behavior. */
1228 for (i = 0; i < 3; ++i)
1229 for (j = 0; j < 3; ++j)
1230 mat[i * 3 + j] = (&mv._11)[j * 4 + i];
1232 GL_EXTCALL(glUniformMatrix3fv(prog->vs.normal_matrix_location, 1, FALSE, mat));
1233 checkGLcall("glUniformMatrix3fv");
1236 static void shader_glsl_ffp_vertex_texmatrix_uniform(const struct wined3d_context *context,
1237 const struct wined3d_state *state, unsigned int tex, struct glsl_shader_prog_link *prog)
1239 const struct wined3d_gl_info *gl_info = context->gl_info;
1240 struct wined3d_matrix mat;
1242 if (tex >= MAX_TEXTURES)
1243 return;
1244 if (prog->vs.texture_matrix_location[tex] == -1)
1245 return;
1247 get_texture_matrix(context, state, tex, &mat);
1248 GL_EXTCALL(glUniformMatrix4fv(prog->vs.texture_matrix_location[tex], 1, FALSE, &mat._11));
1249 checkGLcall("glUniformMatrix4fv");
1252 static void shader_glsl_ffp_vertex_material_uniform(const struct wined3d_context *context,
1253 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1255 const struct wined3d_gl_info *gl_info = context->gl_info;
1257 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1259 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, &state->material.specular.r));
1260 GL_EXTCALL(glUniform1f(prog->vs.material_shininess_location, state->material.power));
1262 else
1264 static const float black[] = {0.0f, 0.0f, 0.0f, 0.0f};
1266 GL_EXTCALL(glUniform4fv(prog->vs.material_specular_location, 1, black));
1268 GL_EXTCALL(glUniform4fv(prog->vs.material_ambient_location, 1, &state->material.ambient.r));
1269 GL_EXTCALL(glUniform4fv(prog->vs.material_diffuse_location, 1, &state->material.diffuse.r));
1270 GL_EXTCALL(glUniform4fv(prog->vs.material_emissive_location, 1, &state->material.emissive.r));
1271 checkGLcall("setting FFP material uniforms");
1274 static void shader_glsl_ffp_vertex_lightambient_uniform(const struct wined3d_context *context,
1275 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1277 const struct wined3d_gl_info *gl_info = context->gl_info;
1278 struct wined3d_color color;
1280 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_AMBIENT]);
1281 GL_EXTCALL(glUniform3fv(prog->vs.light_ambient_location, 1, &color.r));
1282 checkGLcall("glUniform3fv");
1285 static void multiply_vector_matrix(struct wined3d_vec4 *dest, const struct wined3d_vec4 *src1,
1286 const struct wined3d_matrix *src2)
1288 struct wined3d_vec4 temp;
1290 temp.x = (src1->x * src2->_11) + (src1->y * src2->_21) + (src1->z * src2->_31) + (src1->w * src2->_41);
1291 temp.y = (src1->x * src2->_12) + (src1->y * src2->_22) + (src1->z * src2->_32) + (src1->w * src2->_42);
1292 temp.z = (src1->x * src2->_13) + (src1->y * src2->_23) + (src1->z * src2->_33) + (src1->w * src2->_43);
1293 temp.w = (src1->x * src2->_14) + (src1->y * src2->_24) + (src1->z * src2->_34) + (src1->w * src2->_44);
1295 *dest = temp;
1298 static void shader_glsl_ffp_vertex_light_uniform(const struct wined3d_context *context,
1299 const struct wined3d_state *state, unsigned int light, const struct wined3d_light_info *light_info,
1300 struct glsl_shader_prog_link *prog)
1302 const struct wined3d_matrix *view = &state->transforms[WINED3D_TS_VIEW];
1303 const struct wined3d_gl_info *gl_info = context->gl_info;
1304 struct wined3d_vec4 vec4;
1306 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].diffuse, 1, &light_info->OriginalParms.diffuse.r));
1307 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].specular, 1, &light_info->OriginalParms.specular.r));
1308 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].ambient, 1, &light_info->OriginalParms.ambient.r));
1310 switch (light_info->OriginalParms.type)
1312 case WINED3D_LIGHT_POINT:
1313 multiply_vector_matrix(&vec4, &light_info->position, view);
1314 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1315 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1316 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1317 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1318 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1319 break;
1321 case WINED3D_LIGHT_SPOT:
1322 multiply_vector_matrix(&vec4, &light_info->position, view);
1323 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1325 multiply_vector_matrix(&vec4, &light_info->direction, view);
1326 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1328 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].range, light_info->OriginalParms.range));
1329 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].falloff, light_info->OriginalParms.falloff));
1330 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].c_att, light_info->OriginalParms.attenuation0));
1331 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].l_att, light_info->OriginalParms.attenuation1));
1332 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].q_att, light_info->OriginalParms.attenuation2));
1333 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_htheta, cosf(light_info->OriginalParms.theta / 2.0f)));
1334 GL_EXTCALL(glUniform1f(prog->vs.light_location[light].cos_hphi, cosf(light_info->OriginalParms.phi / 2.0f)));
1335 break;
1337 case WINED3D_LIGHT_DIRECTIONAL:
1338 multiply_vector_matrix(&vec4, &light_info->direction, view);
1339 GL_EXTCALL(glUniform3fv(prog->vs.light_location[light].direction, 1, &vec4.x));
1340 break;
1342 case WINED3D_LIGHT_PARALLELPOINT:
1343 multiply_vector_matrix(&vec4, &light_info->position, view);
1344 GL_EXTCALL(glUniform4fv(prog->vs.light_location[light].position, 1, &vec4.x));
1345 break;
1347 default:
1348 FIXME("Unrecognized light type %#x.\n", light_info->OriginalParms.type);
1350 checkGLcall("setting FFP lights uniforms");
1353 static void shader_glsl_pointsize_uniform(const struct wined3d_context *context,
1354 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1356 const struct wined3d_gl_info *gl_info = context->gl_info;
1357 float min, max;
1358 float size, att[3];
1360 get_pointsize_minmax(context, state, &min, &max);
1362 GL_EXTCALL(glUniform1f(prog->vs.pointsize_min_location, min));
1363 checkGLcall("glUniform1f");
1364 GL_EXTCALL(glUniform1f(prog->vs.pointsize_max_location, max));
1365 checkGLcall("glUniform1f");
1367 get_pointsize(context, state, &size, att);
1369 GL_EXTCALL(glUniform1f(prog->vs.pointsize_location, size));
1370 checkGLcall("glUniform1f");
1371 GL_EXTCALL(glUniform1f(prog->vs.pointsize_c_att_location, att[0]));
1372 checkGLcall("glUniform1f");
1373 GL_EXTCALL(glUniform1f(prog->vs.pointsize_l_att_location, att[1]));
1374 checkGLcall("glUniform1f");
1375 GL_EXTCALL(glUniform1f(prog->vs.pointsize_q_att_location, att[2]));
1376 checkGLcall("glUniform1f");
1379 static void shader_glsl_load_fog_uniform(const struct wined3d_context *context,
1380 const struct wined3d_state *state, struct glsl_shader_prog_link *prog)
1382 const struct wined3d_gl_info *gl_info = context->gl_info;
1383 struct wined3d_color color;
1384 float start, end, scale;
1385 union
1387 DWORD d;
1388 float f;
1389 } tmpvalue;
1391 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_FOGCOLOR]);
1392 GL_EXTCALL(glUniform4fv(prog->ps.fog_color_location, 1, &color.r));
1393 tmpvalue.d = state->render_states[WINED3D_RS_FOGDENSITY];
1394 GL_EXTCALL(glUniform1f(prog->ps.fog_density_location, tmpvalue.f));
1395 get_fog_start_end(context, state, &start, &end);
1396 scale = 1.0f / (end - start);
1397 GL_EXTCALL(glUniform1f(prog->ps.fog_end_location, end));
1398 GL_EXTCALL(glUniform1f(prog->ps.fog_scale_location, scale));
1399 checkGLcall("fog emulation uniforms");
1402 static void shader_glsl_clip_plane_uniform(const struct wined3d_context *context,
1403 const struct wined3d_state *state, unsigned int index, struct glsl_shader_prog_link *prog)
1405 const struct wined3d_gl_info *gl_info = context->gl_info;
1406 struct wined3d_vec4 plane;
1408 /* Clip planes are affected by the view transform in d3d for FFP draws. */
1409 if (!use_vs(state))
1410 multiply_vector_matrix(&plane, &state->clip_planes[index], &state->transforms[WINED3D_TS_VIEW]);
1411 else
1412 plane = state->clip_planes[index];
1414 GL_EXTCALL(glUniform4fv(prog->vs.clip_planes_location + index, 1, &plane.x));
1417 /* Context activation is done by the caller (state handler). */
1418 static void shader_glsl_load_color_key_constant(const struct glsl_ps_program *ps,
1419 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
1421 struct wined3d_color float_key[2];
1422 const struct wined3d_texture *texture = state->textures[0];
1424 wined3d_format_get_float_color_key(texture->resource.format, &texture->async.src_blt_color_key, float_key);
1425 GL_EXTCALL(glUniform4fv(ps->color_key_location, 2, &float_key[0].r));
1428 /* Context activation is done by the caller (state handler). */
1429 static void shader_glsl_load_constants(void *shader_priv, struct wined3d_context *context,
1430 const struct wined3d_state *state)
1432 const struct glsl_context_data *ctx_data = context->shader_backend_data;
1433 const struct wined3d_shader *vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
1434 const struct wined3d_shader *pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
1435 const struct wined3d_gl_info *gl_info = context->gl_info;
1436 struct shader_glsl_priv *priv = shader_priv;
1437 float position_fixup[4];
1438 DWORD update_mask;
1440 struct glsl_shader_prog_link *prog = ctx_data->glsl_program;
1441 UINT constant_version;
1442 int i;
1444 if (!prog) {
1445 /* No GLSL program set - nothing to do. */
1446 return;
1448 constant_version = prog->constant_version;
1449 update_mask = context->constant_update_mask & prog->constant_update_mask;
1451 if (update_mask & WINED3D_SHADER_CONST_VS_F)
1452 shader_glsl_load_constants_f(vshader, gl_info, state->vs_consts_f,
1453 prog->vs.uniform_f_locations, &priv->vconst_heap, priv->stack, constant_version);
1455 if (update_mask & WINED3D_SHADER_CONST_VS_I)
1456 shader_glsl_load_constants_i(vshader, gl_info, state->vs_consts_i,
1457 prog->vs.uniform_i_locations, vshader->reg_maps.integer_constants);
1459 if (update_mask & WINED3D_SHADER_CONST_VS_B)
1460 shader_glsl_load_constantsB(vshader, gl_info, prog->vs.uniform_b_locations, state->vs_consts_b,
1461 vshader->reg_maps.boolean_constants);
1463 if (update_mask & WINED3D_SHADER_CONST_VS_CLIP_PLANES)
1465 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
1466 shader_glsl_clip_plane_uniform(context, state, i, prog);
1469 if (update_mask & WINED3D_SHADER_CONST_VS_POINTSIZE)
1470 shader_glsl_pointsize_uniform(context, state, prog);
1472 if (update_mask & WINED3D_SHADER_CONST_POS_FIXUP)
1474 shader_get_position_fixup(context, state, position_fixup);
1475 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
1476 GL_EXTCALL(glUniform4fv(prog->gs.pos_fixup_location, 1, position_fixup));
1477 else
1478 GL_EXTCALL(glUniform4fv(prog->vs.pos_fixup_location, 1, position_fixup));
1479 checkGLcall("glUniform4fv");
1482 if (update_mask & WINED3D_SHADER_CONST_FFP_MODELVIEW)
1484 struct wined3d_matrix mat;
1486 get_modelview_matrix(context, state, 0, &mat);
1487 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[0], 1, FALSE, &mat._11));
1488 checkGLcall("glUniformMatrix4fv");
1490 shader_glsl_ffp_vertex_normalmatrix_uniform(context, state, prog);
1493 if (update_mask & WINED3D_SHADER_CONST_FFP_VERTEXBLEND)
1495 struct wined3d_matrix mat;
1497 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
1499 if (prog->vs.modelview_matrix_location[i] == -1)
1500 break;
1502 get_modelview_matrix(context, state, i, &mat);
1503 GL_EXTCALL(glUniformMatrix4fv(prog->vs.modelview_matrix_location[i], 1, FALSE, &mat._11));
1504 checkGLcall("glUniformMatrix4fv");
1508 if (update_mask & WINED3D_SHADER_CONST_FFP_PROJ)
1510 struct wined3d_matrix projection;
1512 get_projection_matrix(context, state, &projection);
1513 GL_EXTCALL(glUniformMatrix4fv(prog->vs.projection_matrix_location, 1, FALSE, &projection._11));
1514 checkGLcall("glUniformMatrix4fv");
1517 if (update_mask & WINED3D_SHADER_CONST_FFP_TEXMATRIX)
1519 for (i = 0; i < MAX_TEXTURES; ++i)
1520 shader_glsl_ffp_vertex_texmatrix_uniform(context, state, i, prog);
1523 if (update_mask & WINED3D_SHADER_CONST_FFP_MATERIAL)
1524 shader_glsl_ffp_vertex_material_uniform(context, state, prog);
1526 if (update_mask & WINED3D_SHADER_CONST_FFP_LIGHTS)
1528 unsigned int point_idx, spot_idx, directional_idx, parallel_point_idx;
1529 DWORD point_count = 0;
1530 DWORD spot_count = 0;
1531 DWORD directional_count = 0;
1532 DWORD parallel_point_count = 0;
1534 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1536 if (!state->lights[i])
1537 continue;
1539 switch (state->lights[i]->OriginalParms.type)
1541 case WINED3D_LIGHT_POINT:
1542 ++point_count;
1543 break;
1544 case WINED3D_LIGHT_SPOT:
1545 ++spot_count;
1546 break;
1547 case WINED3D_LIGHT_DIRECTIONAL:
1548 ++directional_count;
1549 break;
1550 case WINED3D_LIGHT_PARALLELPOINT:
1551 ++parallel_point_count;
1552 break;
1553 default:
1554 FIXME("Unhandled light type %#x.\n", state->lights[i]->OriginalParms.type);
1555 break;
1558 point_idx = 0;
1559 spot_idx = point_idx + point_count;
1560 directional_idx = spot_idx + spot_count;
1561 parallel_point_idx = directional_idx + directional_count;
1563 shader_glsl_ffp_vertex_lightambient_uniform(context, state, prog);
1564 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
1566 const struct wined3d_light_info *light_info = state->lights[i];
1567 unsigned int idx;
1569 if (!light_info)
1570 continue;
1572 switch (light_info->OriginalParms.type)
1574 case WINED3D_LIGHT_POINT:
1575 idx = point_idx++;
1576 break;
1577 case WINED3D_LIGHT_SPOT:
1578 idx = spot_idx++;
1579 break;
1580 case WINED3D_LIGHT_DIRECTIONAL:
1581 idx = directional_idx++;
1582 break;
1583 case WINED3D_LIGHT_PARALLELPOINT:
1584 idx = parallel_point_idx++;
1585 break;
1586 default:
1587 FIXME("Unhandled light type %#x.\n", light_info->OriginalParms.type);
1588 continue;
1590 shader_glsl_ffp_vertex_light_uniform(context, state, idx, light_info, prog);
1594 if (update_mask & WINED3D_SHADER_CONST_PS_F)
1595 shader_glsl_load_constants_f(pshader, gl_info, state->ps_consts_f,
1596 prog->ps.uniform_f_locations, &priv->pconst_heap, priv->stack, constant_version);
1598 if (update_mask & WINED3D_SHADER_CONST_PS_I)
1599 shader_glsl_load_constants_i(pshader, gl_info, state->ps_consts_i,
1600 prog->ps.uniform_i_locations, pshader->reg_maps.integer_constants);
1602 if (update_mask & WINED3D_SHADER_CONST_PS_B)
1603 shader_glsl_load_constantsB(pshader, gl_info, prog->ps.uniform_b_locations, state->ps_consts_b,
1604 pshader->reg_maps.boolean_constants);
1606 if (update_mask & WINED3D_SHADER_CONST_PS_BUMP_ENV)
1608 for (i = 0; i < MAX_TEXTURES; ++i)
1610 if (prog->ps.bumpenv_mat_location[i] == -1)
1611 continue;
1613 GL_EXTCALL(glUniformMatrix2fv(prog->ps.bumpenv_mat_location[i], 1, 0,
1614 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00]));
1616 if (prog->ps.bumpenv_lum_scale_location[i] != -1)
1618 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_scale_location[i], 1,
1619 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE]));
1620 GL_EXTCALL(glUniform1fv(prog->ps.bumpenv_lum_offset_location[i], 1,
1621 (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET]));
1625 checkGLcall("bump env uniforms");
1628 if (update_mask & WINED3D_SHADER_CONST_PS_Y_CORR)
1630 const struct wined3d_vec4 correction_params =
1632 /* Position is relative to the framebuffer, not the viewport. */
1633 context->render_offscreen ? 0.0f : (float)state->fb->render_targets[0]->height,
1634 context->render_offscreen ? 1.0f : -1.0f,
1635 0.0f,
1636 0.0f,
1639 GL_EXTCALL(glUniform4fv(prog->ps.ycorrection_location, 1, &correction_params.x));
1642 if (update_mask & WINED3D_SHADER_CONST_PS_NP2_FIXUP)
1643 shader_glsl_load_np2fixup_constants(&prog->ps, gl_info, state);
1644 if (update_mask & WINED3D_SHADER_CONST_FFP_COLOR_KEY)
1645 shader_glsl_load_color_key_constant(&prog->ps, gl_info, state);
1647 if (update_mask & WINED3D_SHADER_CONST_FFP_PS)
1649 struct wined3d_color color;
1651 if (prog->ps.tex_factor_location != -1)
1653 wined3d_color_from_d3dcolor(&color, state->render_states[WINED3D_RS_TEXTUREFACTOR]);
1654 GL_EXTCALL(glUniform4fv(prog->ps.tex_factor_location, 1, &color.r));
1657 if (state->render_states[WINED3D_RS_SPECULARENABLE])
1658 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 1.0f, 1.0f, 1.0f, 0.0f));
1659 else
1660 GL_EXTCALL(glUniform4f(prog->ps.specular_enable_location, 0.0f, 0.0f, 0.0f, 0.0f));
1662 for (i = 0; i < MAX_TEXTURES; ++i)
1664 if (prog->ps.tss_constant_location[i] == -1)
1665 continue;
1667 wined3d_color_from_d3dcolor(&color, state->texture_states[i][WINED3D_TSS_CONSTANT]);
1668 GL_EXTCALL(glUniform4fv(prog->ps.tss_constant_location[i], 1, &color.r));
1671 checkGLcall("fixed function uniforms");
1674 if (update_mask & WINED3D_SHADER_CONST_PS_FOG)
1675 shader_glsl_load_fog_uniform(context, state, prog);
1677 if (update_mask & WINED3D_SHADER_CONST_PS_ALPHA_TEST)
1679 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
1681 GL_EXTCALL(glUniform1f(prog->ps.alpha_test_ref_location, ref));
1682 checkGLcall("alpha test emulation uniform");
1685 if (priv->next_constant_version == UINT_MAX)
1687 TRACE("Max constant version reached, resetting to 0.\n");
1688 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
1689 priv->next_constant_version = 1;
1691 else
1693 prog->constant_version = priv->next_constant_version++;
1697 static void update_heap_entry(struct constant_heap *heap, unsigned int idx, DWORD new_version)
1699 struct constant_entry *entries = heap->entries;
1700 unsigned int *positions = heap->positions;
1701 unsigned int heap_idx, parent_idx;
1703 if (!heap->contained[idx])
1705 heap_idx = heap->size++;
1706 heap->contained[idx] = TRUE;
1708 else
1710 heap_idx = positions[idx];
1713 while (heap_idx > 1)
1715 parent_idx = heap_idx >> 1;
1717 if (new_version <= entries[parent_idx].version) break;
1719 entries[heap_idx] = entries[parent_idx];
1720 positions[entries[parent_idx].idx] = heap_idx;
1721 heap_idx = parent_idx;
1724 entries[heap_idx].version = new_version;
1725 entries[heap_idx].idx = idx;
1726 positions[idx] = heap_idx;
1729 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
1731 struct shader_glsl_priv *priv = device->shader_priv;
1732 struct constant_heap *heap = &priv->vconst_heap;
1733 UINT i;
1735 for (i = start; i < count + start; ++i)
1737 update_heap_entry(heap, i, priv->next_constant_version);
1741 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
1743 struct shader_glsl_priv *priv = device->shader_priv;
1744 struct constant_heap *heap = &priv->pconst_heap;
1745 UINT i;
1747 for (i = start; i < count + start; ++i)
1749 update_heap_entry(heap, i, priv->next_constant_version);
1753 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
1755 unsigned int ret = gl_info->limits.glsl_varyings / 4;
1756 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
1757 if(shader_major > 3) return ret;
1759 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
1760 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
1761 return ret;
1764 static BOOL needs_legacy_glsl_syntax(const struct wined3d_gl_info *gl_info)
1766 return gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
1769 static BOOL shader_glsl_use_explicit_attrib_location(const struct wined3d_gl_info *gl_info)
1771 return !needs_legacy_glsl_syntax(gl_info) && gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION];
1774 static const char *get_attribute_keyword(const struct wined3d_gl_info *gl_info)
1776 return needs_legacy_glsl_syntax(gl_info) ? "attribute" : "in";
1779 static void PRINTF_ATTR(4, 5) declare_in_varying(const struct wined3d_gl_info *gl_info,
1780 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1782 va_list args;
1783 int ret;
1785 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1786 needs_legacy_glsl_syntax(gl_info) ? "varying" : "in");
1787 for (;;)
1789 va_start(args, format);
1790 ret = shader_vaddline(buffer, format, args);
1791 va_end(args);
1792 if (!ret)
1793 return;
1794 if (!string_buffer_resize(buffer, ret))
1795 return;
1799 static void PRINTF_ATTR(4, 5) declare_out_varying(const struct wined3d_gl_info *gl_info,
1800 struct wined3d_string_buffer *buffer, BOOL flat, const char *format, ...)
1802 va_list args;
1803 int ret;
1805 shader_addline(buffer, "%s%s ", flat ? "flat " : "",
1806 needs_legacy_glsl_syntax(gl_info) ? "varying" : "out");
1807 for (;;)
1809 va_start(args, format);
1810 ret = shader_vaddline(buffer, format, args);
1811 va_end(args);
1812 if (!ret)
1813 return;
1814 if (!string_buffer_resize(buffer, ret))
1815 return;
1819 static const char *get_fragment_output(const struct wined3d_gl_info *gl_info)
1821 return needs_legacy_glsl_syntax(gl_info) ? "gl_FragData" : "ps_out";
1824 static const char *glsl_primitive_type_from_d3d(enum wined3d_primitive_type primitive_type)
1826 switch (primitive_type)
1828 case WINED3D_PT_POINTLIST:
1829 return "points";
1831 case WINED3D_PT_LINELIST:
1832 return "lines";
1834 case WINED3D_PT_LINESTRIP:
1835 return "line_strip";
1837 case WINED3D_PT_TRIANGLELIST:
1838 return "triangles";
1840 case WINED3D_PT_TRIANGLESTRIP:
1841 return "triangle_strip";
1843 case WINED3D_PT_LINELIST_ADJ:
1844 return "lines_adjacency";
1846 case WINED3D_PT_TRIANGLELIST_ADJ:
1847 return "triangles_adjacency";
1849 default:
1850 FIXME("Unhandled primitive type %s.\n", debug_d3dprimitivetype(primitive_type));
1851 return "";
1855 static BOOL glsl_is_color_reg_read(const struct wined3d_shader *shader, unsigned int idx)
1857 const struct wined3d_shader_signature *input_signature = &shader->input_signature;
1858 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
1859 DWORD input_reg_used = shader->u.ps.input_reg_used;
1860 unsigned int i;
1862 if (reg_maps->shader_version.major < 3)
1863 return input_reg_used & (1u << idx);
1865 for (i = 0; i < input_signature->element_count; ++i)
1867 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
1869 if (!(reg_maps->input_registers & (1u << input->register_idx)))
1870 continue;
1872 if (shader_match_semantic(input->semantic_name, WINED3D_DECL_USAGE_COLOR)
1873 && input->semantic_idx == idx)
1874 return input_reg_used & (1u << input->register_idx);
1876 return FALSE;
1879 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1880 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1882 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1884 if (version->major >= 4)
1885 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1886 else
1887 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1890 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1891 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1892 unsigned int index)
1894 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1895 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1896 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1897 index, scalar_type, scalar_type, index);
1900 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1901 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1903 unsigned int index = e->register_idx;
1905 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1907 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1908 index);
1909 return;
1911 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1913 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1914 index);
1915 return;
1917 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1918 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1920 if (shader_glsl_use_explicit_attrib_location(gl_info))
1921 shader_addline(buffer, "layout(location = %u) ", index);
1923 switch (e->component_type)
1925 case WINED3D_TYPE_UINT:
1926 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1927 break;
1928 case WINED3D_TYPE_INT:
1929 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1930 break;
1932 default:
1933 FIXME("Unhandled type %#x.\n", e->component_type);
1934 /* Fall through. */
1935 case WINED3D_TYPE_UNKNOWN:
1936 case WINED3D_TYPE_FLOAT:
1937 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1938 break;
1942 /** Generate the variable & register declarations for the GLSL output target */
1943 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1944 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1945 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1947 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1948 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1949 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1950 const struct wined3d_gl_info *gl_info = context->gl_info;
1951 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1952 unsigned int i, extra_constants_needed = 0;
1953 const struct wined3d_shader_lconst *lconst;
1954 const char *prefix;
1955 DWORD map;
1957 prefix = shader_glsl_get_prefix(version->type);
1959 /* Prototype the subroutines */
1960 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1962 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1965 /* Declare the constants (aka uniforms) */
1966 if (shader->limits->constant_float > 0)
1968 unsigned max_constantsF;
1970 /* Unless the shader uses indirect addressing, always declare the
1971 * maximum array size and ignore that we need some uniforms privately.
1972 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1973 * and immediate values, still declare VC[256]. If the shader needs
1974 * more uniforms than we have it won't work in any case. If it uses
1975 * less, the compiler will figure out which uniforms are really used
1976 * and strip them out. This allows a shader to use c255 on a dx9 card,
1977 * as long as it doesn't also use all the other constants.
1979 * If the shader uses indirect addressing the compiler must assume
1980 * that all declared uniforms are used. In this case, declare only the
1981 * amount that we're assured to have.
1983 * Thus we run into problems in these two cases:
1984 * 1) The shader really uses more uniforms than supported.
1985 * 2) The shader uses indirect addressing, less constants than
1986 * supported, but uses a constant index > #supported consts. */
1987 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1989 /* No indirect addressing here. */
1990 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1992 else
1994 if (reg_maps->usesrelconstF)
1996 /* Subtract the other potential uniforms from the max
1997 * available (bools, ints, and 1 row of projection matrix).
1998 * Subtract another uniform for immediate values, which have
1999 * to be loaded via uniform by the driver as well. The shader
2000 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2001 * shader code, so one vec4 should be enough. (Unfortunately
2002 * the Nvidia driver doesn't store 128 and -128 in one float).
2004 * Writing gl_ClipVertex requires one uniform for each
2005 * clipplane as well. */
2006 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2007 if (vs_args->clip_enabled)
2008 max_constantsF -= gl_info->limits.user_clip_distances;
2009 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2010 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2011 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2012 * for now take this into account when calculating the number of available constants
2014 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2015 /* Set by driver quirks in directx.c */
2016 max_constantsF -= gl_info->reserved_glsl_constants;
2018 if (max_constantsF < shader->limits->constant_float)
2020 static unsigned int once;
2022 if (!once++)
2023 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2024 " it may not render correctly.\n");
2025 else
2026 WARN("The hardware does not support enough uniform components to run this shader.\n");
2029 else
2031 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2034 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2035 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2038 /* Always declare the full set of constants, the compiler can remove the
2039 * unused ones because d3d doesn't (yet) support indirect int and bool
2040 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2041 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2042 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2044 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2045 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2047 /* Declare immediate constant buffer */
2048 if (reg_maps->icb)
2049 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2051 /* Declare constant buffers */
2052 for (i = 0; i < WINED3D_MAX_CBS; ++i)
2054 if (reg_maps->cb_sizes[i])
2055 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2056 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2059 /* Declare texture samplers */
2060 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2062 struct wined3d_shader_sampler_map_entry *entry;
2063 const char *sampler_type_prefix, *sampler_type;
2064 BOOL shadow_sampler, tex_rect;
2066 entry = &reg_maps->sampler_map.entries[i];
2068 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2070 ERR("Invalid resource index %u.\n", entry->resource_idx);
2071 continue;
2074 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2076 case WINED3D_DATA_FLOAT:
2077 case WINED3D_DATA_UNORM:
2078 case WINED3D_DATA_SNORM:
2079 sampler_type_prefix = "";
2080 break;
2082 case WINED3D_DATA_INT:
2083 sampler_type_prefix = "i";
2084 break;
2086 case WINED3D_DATA_UINT:
2087 sampler_type_prefix = "u";
2088 break;
2090 default:
2091 sampler_type_prefix = "";
2092 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2093 break;
2096 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2097 switch (reg_maps->resource_info[entry->resource_idx].type)
2099 case WINED3D_SHADER_RESOURCE_BUFFER:
2100 sampler_type = "samplerBuffer";
2101 break;
2103 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2104 if (shadow_sampler)
2105 sampler_type = "sampler1DShadow";
2106 else
2107 sampler_type = "sampler1D";
2108 break;
2110 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2111 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2112 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2113 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2114 if (shadow_sampler)
2116 if (tex_rect)
2117 sampler_type = "sampler2DRectShadow";
2118 else
2119 sampler_type = "sampler2DShadow";
2121 else
2123 if (tex_rect)
2124 sampler_type = "sampler2DRect";
2125 else
2126 sampler_type = "sampler2D";
2128 break;
2130 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2131 if (shadow_sampler)
2132 FIXME("Unsupported 3D shadow sampler.\n");
2133 sampler_type = "sampler3D";
2134 break;
2136 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2137 if (shadow_sampler)
2138 FIXME("Unsupported Cube shadow sampler.\n");
2139 sampler_type = "samplerCube";
2140 break;
2142 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2143 if (shadow_sampler)
2144 sampler_type = "sampler2DArrayShadow";
2145 else
2146 sampler_type = "sampler2DArray";
2147 break;
2149 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2150 if (shadow_sampler)
2151 FIXME("Unsupported Cube array shadow sampler.\n");
2152 sampler_type = "samplerCubeArray";
2153 break;
2155 default:
2156 sampler_type = "unsupported_sampler";
2157 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2158 break;
2160 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2161 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2164 /* Declare images */
2165 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2167 const char *image_type_prefix, *image_type, *read_format;
2169 if (!reg_maps->uav_resource_info[i].type)
2170 continue;
2172 switch (reg_maps->uav_resource_info[i].data_type)
2174 case WINED3D_DATA_FLOAT:
2175 case WINED3D_DATA_UNORM:
2176 case WINED3D_DATA_SNORM:
2177 image_type_prefix = "";
2178 read_format = "r32f";
2179 break;
2181 case WINED3D_DATA_INT:
2182 image_type_prefix = "i";
2183 read_format = "r32i";
2184 break;
2186 case WINED3D_DATA_UINT:
2187 image_type_prefix = "u";
2188 read_format = "r32ui";
2189 break;
2191 default:
2192 image_type_prefix = "";
2193 read_format = "";
2194 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2195 break;
2198 switch (reg_maps->uav_resource_info[i].type)
2200 case WINED3D_SHADER_RESOURCE_BUFFER:
2201 image_type = "imageBuffer";
2202 break;
2204 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2205 image_type = "image2D";
2206 break;
2208 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2209 image_type = "image3D";
2210 break;
2212 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2213 image_type = "image2DArray";
2214 break;
2216 default:
2217 image_type = "unsupported_image";
2218 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2219 break;
2222 if (reg_maps->uav_read_mask & (1u << i))
2223 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2224 read_format, image_type_prefix, image_type, prefix, i);
2225 else
2226 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2227 image_type_prefix, image_type, prefix, i);
2230 /* Declare uniforms for NP2 texcoord fixup:
2231 * This is NOT done inside the loop that declares the texture samplers
2232 * since the NP2 fixup code is currently only used for the GeforceFX
2233 * series and when forcing the ARB_npot extension off. Modern cards just
2234 * skip the code anyway, so put it inside a separate loop. */
2235 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2237 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2238 UINT cur = 0;
2240 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2241 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2242 * samplerNP2Fixup stores texture dimensions and is updated through
2243 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2245 for (i = 0; i < shader->limits->sampler; ++i)
2247 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2248 continue;
2250 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2252 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2253 continue;
2256 fixup->idx[i] = cur++;
2259 fixup->num_consts = (cur + 1) >> 1;
2260 fixup->active = ps_args->np2_fixup;
2261 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2264 /* Declare address variables */
2265 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2267 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2270 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2272 for (i = 0; i < shader->input_signature.element_count; ++i)
2273 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2275 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2277 shader_addline(buffer, "uniform struct\n{\n");
2278 shader_addline(buffer, " float size;\n");
2279 shader_addline(buffer, " float size_min;\n");
2280 shader_addline(buffer, " float size_max;\n");
2281 shader_addline(buffer, "} ffp_point;\n");
2284 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2286 if (vs_args->clip_enabled)
2287 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2289 if (version->major < 3)
2291 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2292 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2293 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2294 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2298 if (version->major < 4)
2299 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2301 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2303 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2305 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2307 else
2309 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2310 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2311 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2312 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2315 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2317 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2319 shader_addline(buffer, "uniform struct\n{\n");
2320 shader_addline(buffer, " vec4 color;\n");
2321 shader_addline(buffer, " float density;\n");
2322 shader_addline(buffer, " float end;\n");
2323 shader_addline(buffer, " float scale;\n");
2324 shader_addline(buffer, "} ffp_fog;\n");
2326 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2328 if (glsl_is_color_reg_read(shader, 0))
2329 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2330 if (glsl_is_color_reg_read(shader, 1))
2331 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2332 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2333 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2335 else
2337 if (glsl_is_color_reg_read(shader, 0))
2338 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2339 if (glsl_is_color_reg_read(shader, 1))
2340 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2341 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2342 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2343 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2347 if (version->major >= 3)
2349 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2351 if (ps_args->vp_mode == vertexshader)
2352 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2353 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2356 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2358 if (!(map & 1))
2359 continue;
2361 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2363 if (reg_maps->luminanceparams & (1u << i))
2365 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2366 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2367 extra_constants_needed++;
2370 extra_constants_needed++;
2373 if (ps_args->srgb_correction)
2375 shader_addline(buffer, "const vec4 srgb_const0 = ");
2376 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2377 shader_addline(buffer, ";\n");
2378 shader_addline(buffer, "const vec4 srgb_const1 = ");
2379 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2380 shader_addline(buffer, ";\n");
2382 if (reg_maps->vpos || reg_maps->usesdsy)
2384 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2386 ++extra_constants_needed;
2387 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2389 if (reg_maps->vpos)
2391 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2393 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2394 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2395 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2396 else if (!ps_args->render_offscreen)
2397 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2399 shader_addline(buffer, "vec4 vpos;\n");
2403 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2404 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2406 if (!needs_legacy_glsl_syntax(gl_info))
2407 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2409 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2410 FIXME("Insufficient uniforms to run this shader.\n");
2413 /* Declare output register temporaries */
2414 if (shader->limits->packed_output)
2415 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2417 /* Declare temporary variables */
2418 if (reg_maps->temporary_count)
2420 for (i = 0; i < reg_maps->temporary_count; ++i)
2421 shader_addline(buffer, "vec4 R%u;\n", i);
2423 else
2425 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2427 if (map & 1)
2428 shader_addline(buffer, "vec4 R%u;\n", i);
2432 /* Declare indexable temporary variables */
2433 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2435 if (idx_temp_reg->component_count != 4)
2436 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2437 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2440 /* Declare loop registers aLx */
2441 if (version->major < 4)
2443 for (i = 0; i < reg_maps->loop_depth; ++i)
2445 shader_addline(buffer, "int aL%u;\n", i);
2446 shader_addline(buffer, "int tmpInt%u;\n", i);
2450 /* Temporary variables for matrix operations */
2451 shader_addline(buffer, "vec4 tmp0;\n");
2452 shader_addline(buffer, "vec4 tmp1;\n");
2454 if (!shader->load_local_constsF)
2456 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2458 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2459 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2460 shader_addline(buffer, ";\n");
2465 /*****************************************************************************
2466 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2468 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2469 ****************************************************************************/
2471 /* Prototypes */
2472 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2473 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2475 /** Used for opcode modifiers - They multiply the result by the specified amount */
2476 static const char * const shift_glsl_tab[] = {
2477 "", /* 0 (none) */
2478 "2.0 * ", /* 1 (x2) */
2479 "4.0 * ", /* 2 (x4) */
2480 "8.0 * ", /* 3 (x8) */
2481 "16.0 * ", /* 4 (x16) */
2482 "32.0 * ", /* 5 (x32) */
2483 "", /* 6 (x64) */
2484 "", /* 7 (x128) */
2485 "", /* 8 (d256) */
2486 "", /* 9 (d128) */
2487 "", /* 10 (d64) */
2488 "", /* 11 (d32) */
2489 "0.0625 * ", /* 12 (d16) */
2490 "0.125 * ", /* 13 (d8) */
2491 "0.25 * ", /* 14 (d4) */
2492 "0.5 * " /* 15 (d2) */
2495 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2496 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2497 const char *in_reg, const char *in_regswizzle, char *out_str)
2499 switch (src_modifier)
2501 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2502 case WINED3DSPSM_DW:
2503 case WINED3DSPSM_NONE:
2504 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2505 break;
2506 case WINED3DSPSM_NEG:
2507 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2508 break;
2509 case WINED3DSPSM_NOT:
2510 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2511 break;
2512 case WINED3DSPSM_BIAS:
2513 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2514 break;
2515 case WINED3DSPSM_BIASNEG:
2516 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2517 break;
2518 case WINED3DSPSM_SIGN:
2519 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2520 break;
2521 case WINED3DSPSM_SIGNNEG:
2522 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2523 break;
2524 case WINED3DSPSM_COMP:
2525 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2526 break;
2527 case WINED3DSPSM_X2:
2528 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2529 break;
2530 case WINED3DSPSM_X2NEG:
2531 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2532 break;
2533 case WINED3DSPSM_ABS:
2534 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2535 break;
2536 case WINED3DSPSM_ABSNEG:
2537 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2538 break;
2539 default:
2540 FIXME("Unhandled modifier %u\n", src_modifier);
2541 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2545 /** Writes the GLSL variable name that corresponds to the register that the
2546 * DX opcode parameter is trying to access */
2547 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2548 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2550 /* oPos, oFog and oPts in D3D */
2551 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2553 const struct wined3d_shader *shader = ins->ctx->shader;
2554 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2555 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2556 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2557 const char *prefix = shader_glsl_get_prefix(version->type);
2558 struct glsl_src_param rel_param0, rel_param1;
2559 char imm_str[4][17];
2561 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2562 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2563 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2564 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2565 *is_color = FALSE;
2567 switch (reg->type)
2569 case WINED3DSPR_TEMP:
2570 sprintf(register_name, "R%u", reg->idx[0].offset);
2571 break;
2573 case WINED3DSPR_INPUT:
2574 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2576 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2578 if (reg->idx[0].rel_addr)
2579 FIXME("VS3+ input registers relative addressing.\n");
2580 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2581 *is_color = TRUE;
2582 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2583 break;
2586 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2588 if (reg->idx[0].rel_addr)
2590 if (reg->idx[1].rel_addr)
2591 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2592 rel_param0.param_str, reg->idx[0].offset,
2593 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2594 rel_param1.param_str, reg->idx[1].offset);
2595 else
2596 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2597 rel_param0.param_str, reg->idx[0].offset,
2598 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2599 reg->idx[1].offset);
2601 else if (reg->idx[1].rel_addr)
2602 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2603 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2604 rel_param1.param_str, reg->idx[1].offset);
2605 else
2606 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2607 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2608 reg->idx[1].offset);
2609 break;
2612 /* pixel shaders >= 3.0 */
2613 if (version->major >= 3)
2615 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2616 unsigned int in_count = vec4_varyings(version->major, gl_info);
2618 if (reg->idx[0].rel_addr)
2620 /* Removing a + 0 would be an obvious optimization, but
2621 * OS X doesn't see the NOP operation there. */
2622 if (idx)
2624 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2625 && shader->u.ps.declared_in_count > in_count)
2627 sprintf(register_name,
2628 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2629 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2630 prefix, rel_param0.param_str, idx);
2632 else
2634 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2637 else
2639 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2640 && shader->u.ps.declared_in_count > in_count)
2642 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2643 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2644 prefix, rel_param0.param_str);
2646 else
2648 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2652 else
2654 if (idx == in_count) sprintf(register_name, "gl_Color");
2655 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2656 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2659 else
2661 if (!reg->idx[0].offset)
2662 strcpy(register_name, "ffp_varying_diffuse");
2663 else
2664 strcpy(register_name, "ffp_varying_specular");
2665 break;
2667 break;
2669 case WINED3DSPR_CONST:
2671 /* Relative addressing */
2672 if (reg->idx[0].rel_addr)
2674 if (wined3d_settings.check_float_constants)
2675 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2676 rel_param0.param_str, reg->idx[0].offset,
2677 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2678 prefix, rel_param0.param_str, reg->idx[0].offset);
2679 else if (reg->idx[0].offset)
2680 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2681 else
2682 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2684 else
2686 if (shader_constant_is_local(shader, reg->idx[0].offset))
2687 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2688 else
2689 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2692 break;
2694 case WINED3DSPR_CONSTINT:
2695 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2696 break;
2698 case WINED3DSPR_CONSTBOOL:
2699 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2700 break;
2702 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2703 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2704 sprintf(register_name, "T%u", reg->idx[0].offset);
2705 else
2706 sprintf(register_name, "A%u", reg->idx[0].offset);
2707 break;
2709 case WINED3DSPR_LOOP:
2710 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2711 break;
2713 case WINED3DSPR_SAMPLER:
2714 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2715 break;
2717 case WINED3DSPR_COLOROUT:
2718 if (reg->idx[0].offset >= gl_info->limits.buffers)
2719 WARN("Write to render target %u, only %d supported.\n",
2720 reg->idx[0].offset, gl_info->limits.buffers);
2722 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2723 break;
2725 case WINED3DSPR_RASTOUT:
2726 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2727 break;
2729 case WINED3DSPR_DEPTHOUT:
2730 sprintf(register_name, "gl_FragDepth");
2731 break;
2733 case WINED3DSPR_ATTROUT:
2734 if (!reg->idx[0].offset)
2735 sprintf(register_name, "%s_out[8]", prefix);
2736 else
2737 sprintf(register_name, "%s_out[9]", prefix);
2738 break;
2740 case WINED3DSPR_TEXCRDOUT:
2741 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2742 if (reg->idx[0].rel_addr)
2743 FIXME("VS3 output registers relative addressing.\n");
2744 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2745 break;
2747 case WINED3DSPR_MISCTYPE:
2748 if (!reg->idx[0].offset)
2750 /* vPos */
2751 sprintf(register_name, "vpos");
2753 else if (reg->idx[0].offset == 1)
2755 /* Note that gl_FrontFacing is a bool, while vFace is
2756 * a float for which the sign determines front/back */
2757 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2759 else
2761 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2762 sprintf(register_name, "unrecognized_register");
2764 break;
2766 case WINED3DSPR_IMMCONST:
2767 switch (reg->immconst_type)
2769 case WINED3D_IMMCONST_SCALAR:
2770 switch (reg->data_type)
2772 case WINED3D_DATA_FLOAT:
2773 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2774 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2775 else
2776 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2777 break;
2778 case WINED3D_DATA_INT:
2779 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2780 break;
2781 case WINED3D_DATA_RESOURCE:
2782 case WINED3D_DATA_SAMPLER:
2783 case WINED3D_DATA_UINT:
2784 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2785 break;
2786 default:
2787 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2788 break;
2790 break;
2792 case WINED3D_IMMCONST_VEC4:
2793 switch (reg->data_type)
2795 case WINED3D_DATA_FLOAT:
2796 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2798 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2799 reg->u.immconst_data[0], reg->u.immconst_data[1],
2800 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2802 else
2804 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2805 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2806 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2807 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2808 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2809 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2811 break;
2812 case WINED3D_DATA_INT:
2813 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2814 reg->u.immconst_data[0], reg->u.immconst_data[1],
2815 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2816 break;
2817 case WINED3D_DATA_RESOURCE:
2818 case WINED3D_DATA_SAMPLER:
2819 case WINED3D_DATA_UINT:
2820 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2821 reg->u.immconst_data[0], reg->u.immconst_data[1],
2822 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2823 break;
2824 default:
2825 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2826 break;
2828 break;
2830 default:
2831 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2832 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2834 break;
2836 case WINED3DSPR_CONSTBUFFER:
2837 if (reg->idx[1].rel_addr)
2838 sprintf(register_name, "%s_cb%u[%s + %u]",
2839 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2840 else
2841 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2842 break;
2844 case WINED3DSPR_IMMCONSTBUFFER:
2845 if (reg->idx[0].rel_addr)
2846 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2847 else
2848 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2849 break;
2851 case WINED3DSPR_PRIMID:
2852 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2853 break;
2855 case WINED3DSPR_IDXTEMP:
2856 if (reg->idx[1].rel_addr)
2857 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2858 else
2859 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2860 break;
2862 case WINED3DSPR_LOCALTHREADINDEX:
2863 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
2864 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2865 else
2866 sprintf(register_name, "ivec2(gl_LocalInvocationIndex, 0)");
2867 break;
2869 case WINED3DSPR_THREADID:
2870 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2871 break;
2873 case WINED3DSPR_THREADGROUPID:
2874 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2875 break;
2877 case WINED3DSPR_LOCALTHREADID:
2878 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2879 break;
2881 default:
2882 FIXME("Unhandled register type %#x.\n", reg->type);
2883 sprintf(register_name, "unrecognized_register");
2884 break;
2888 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2890 *str++ = '.';
2891 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2892 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2893 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2894 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2895 *str = '\0';
2898 /* Get the GLSL write mask for the destination register */
2899 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2901 DWORD mask = param->write_mask;
2903 if (shader_is_scalar(&param->reg))
2905 mask = WINED3DSP_WRITEMASK_0;
2906 *write_mask = '\0';
2908 else
2910 shader_glsl_write_mask_to_str(mask, write_mask);
2913 return mask;
2916 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask)
2918 unsigned int size = 0;
2920 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2921 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2922 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2923 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2925 return size;
2928 static unsigned int shader_glsl_swizzle_get_component(DWORD swizzle,
2929 unsigned int component_idx)
2931 /* swizzle bits fields: wwzzyyxx */
2932 return (swizzle >> (2 * component_idx)) & 0x3;
2935 static void shader_glsl_swizzle_to_str(DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2937 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2938 * but addressed as "rgba". To fix this we need to swap the register's x
2939 * and z components. */
2940 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2941 unsigned int i;
2943 *str++ = '.';
2944 for (i = 0; i < 4; ++i)
2946 if (mask & (WINED3DSP_WRITEMASK_0 << i))
2947 *str++ = swizzle_chars[shader_glsl_swizzle_get_component(swizzle, i)];
2949 *str = '\0';
2952 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2953 BOOL fixup, DWORD mask, char *swizzle_str)
2955 if (shader_is_scalar(&param->reg))
2956 *swizzle_str = '\0';
2957 else
2958 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2961 static void shader_glsl_sprintf_cast(struct wined3d_string_buffer *dst_param, const char *src_param,
2962 enum wined3d_data_type dst_data_type, enum wined3d_data_type src_data_type)
2964 if (dst_data_type == src_data_type)
2966 string_buffer_sprintf(dst_param, "%s", src_param);
2967 return;
2970 if (src_data_type == WINED3D_DATA_FLOAT)
2972 switch (dst_data_type)
2974 case WINED3D_DATA_INT:
2975 string_buffer_sprintf(dst_param, "floatBitsToInt(%s)", src_param);
2976 return;
2977 case WINED3D_DATA_RESOURCE:
2978 case WINED3D_DATA_SAMPLER:
2979 case WINED3D_DATA_UINT:
2980 string_buffer_sprintf(dst_param, "floatBitsToUint(%s)", src_param);
2981 return;
2982 default:
2983 break;
2987 if (src_data_type == WINED3D_DATA_UINT && dst_data_type == WINED3D_DATA_FLOAT)
2989 string_buffer_sprintf(dst_param, "uintBitsToFloat(%s)", src_param);
2990 return;
2993 if (src_data_type == WINED3D_DATA_INT && dst_data_type == WINED3D_DATA_FLOAT)
2995 string_buffer_sprintf(dst_param, "intBitsToFloat(%s)", src_param);
2996 return;
2999 FIXME("Unhandled cast from %#x to %#x.\n", src_data_type, dst_data_type);
3000 string_buffer_sprintf(dst_param, "%s", src_param);
3003 /* From a given parameter token, generate the corresponding GLSL string.
3004 * Also, return the actual register name and swizzle in case the
3005 * caller needs this information as well. */
3006 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
3007 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
3008 enum wined3d_data_type data_type)
3010 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3011 struct wined3d_string_buffer *reg_name = string_buffer_get(priv->string_buffers);
3012 enum wined3d_data_type param_data_type;
3013 BOOL is_color = FALSE;
3014 char swizzle_str[6];
3016 glsl_src->reg_name[0] = '\0';
3017 glsl_src->param_str[0] = '\0';
3018 swizzle_str[0] = '\0';
3020 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
3021 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
3023 switch (wined3d_src->reg.type)
3025 case WINED3DSPR_IMMCONST:
3026 param_data_type = data_type;
3027 break;
3028 case WINED3DSPR_PRIMID:
3029 param_data_type = WINED3D_DATA_UINT;
3030 break;
3031 case WINED3DSPR_LOCALTHREADINDEX:
3032 case WINED3DSPR_THREADID:
3033 case WINED3DSPR_THREADGROUPID:
3034 case WINED3DSPR_LOCALTHREADID:
3035 param_data_type = WINED3D_DATA_INT;
3036 break;
3037 default:
3038 param_data_type = WINED3D_DATA_FLOAT;
3039 break;
3042 shader_glsl_sprintf_cast(reg_name, glsl_src->reg_name, data_type, param_data_type);
3043 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name->buffer, swizzle_str, glsl_src->param_str);
3045 string_buffer_release(priv->string_buffers, reg_name);
3048 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3049 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3051 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3054 /* From a given parameter token, generate the corresponding GLSL string.
3055 * Also, return the actual register name and swizzle in case the
3056 * caller needs this information as well. */
3057 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3058 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3060 BOOL is_color = FALSE;
3062 glsl_dst->mask_str[0] = '\0';
3063 glsl_dst->reg_name[0] = '\0';
3065 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3066 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3069 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3070 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3071 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3072 enum wined3d_data_type data_type)
3074 struct glsl_dst_param glsl_dst;
3075 DWORD mask;
3077 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3079 switch (data_type)
3081 case WINED3D_DATA_FLOAT:
3082 shader_addline(buffer, "%s%s = %s(",
3083 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3084 break;
3085 case WINED3D_DATA_INT:
3086 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3087 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3088 break;
3089 case WINED3D_DATA_RESOURCE:
3090 case WINED3D_DATA_SAMPLER:
3091 case WINED3D_DATA_UINT:
3092 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3093 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3094 break;
3095 default:
3096 FIXME("Unhandled data type %#x.\n", data_type);
3097 shader_addline(buffer, "%s%s = %s(",
3098 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3099 break;
3103 return mask;
3106 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3107 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3109 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3112 /** Process GLSL instruction modifiers */
3113 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3115 struct glsl_dst_param dst_param;
3116 DWORD modifiers;
3118 if (!ins->dst_count) return;
3120 modifiers = ins->dst[0].modifiers;
3121 if (!modifiers) return;
3123 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3125 if (modifiers & WINED3DSPDM_SATURATE)
3127 /* _SAT means to clamp the value of the register to between 0 and 1 */
3128 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3129 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3132 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3134 FIXME("_centroid modifier not handled\n");
3137 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3139 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3143 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3145 switch (op)
3147 case WINED3D_SHADER_REL_OP_GT: return ">";
3148 case WINED3D_SHADER_REL_OP_EQ: return "==";
3149 case WINED3D_SHADER_REL_OP_GE: return ">=";
3150 case WINED3D_SHADER_REL_OP_LT: return "<";
3151 case WINED3D_SHADER_REL_OP_NE: return "!=";
3152 case WINED3D_SHADER_REL_OP_LE: return "<=";
3153 default:
3154 FIXME("Unrecognized operator %#x.\n", op);
3155 return "(\?\?)";
3159 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3160 const struct wined3d_shader_version *version)
3162 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3165 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3166 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3168 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3169 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3170 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3171 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3172 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3173 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3174 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3175 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3176 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3177 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3178 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3179 const char *base = "texture", *type_part = "", *suffix = "";
3180 unsigned int coord_size, deriv_size;
3181 BOOL array;
3183 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3185 if (resource_type >= ARRAY_SIZE(resource_type_info))
3187 ERR("Unexpected resource type %#x.\n", resource_type);
3188 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3190 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3191 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3193 /* Note that there's no such thing as a projected cube texture. */
3194 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3195 projected = FALSE;
3197 if (needs_legacy_glsl_syntax(gl_info))
3199 if (shadow)
3200 base = "shadow";
3202 type_part = resource_type_info[resource_type].type_part;
3203 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3204 type_part = "2DRect";
3205 if (!type_part[0])
3206 FIXME("Unhandled resource type %#x.\n", resource_type);
3208 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3210 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3211 suffix = "ARB";
3212 else
3213 FIXME("Unsupported grad function.\n");
3217 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3219 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3220 if (flags & ~texel_fetch_flags)
3221 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3223 base = "texelFetch";
3224 type_part = "";
3227 sample_function->name = string_buffer_get(priv->string_buffers);
3228 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3229 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3231 coord_size = resource_type_info[resource_type].coord_size;
3232 deriv_size = coord_size;
3233 if (shadow)
3234 ++coord_size;
3235 if (array)
3236 --deriv_size;
3237 sample_function->offset_size = offset ? deriv_size : 0;
3238 sample_function->coord_mask = (1u << coord_size) - 1;
3239 sample_function->deriv_mask = (1u << deriv_size) - 1;
3240 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3243 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3244 struct glsl_sample_function *sample_function)
3246 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3248 string_buffer_release(priv->string_buffers, sample_function->name);
3251 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3252 BOOL sign_fixup, enum fixup_channel_source channel_source)
3254 switch(channel_source)
3256 case CHANNEL_SOURCE_ZERO:
3257 strcat(arguments, "0.0");
3258 break;
3260 case CHANNEL_SOURCE_ONE:
3261 strcat(arguments, "1.0");
3262 break;
3264 case CHANNEL_SOURCE_X:
3265 strcat(arguments, reg_name);
3266 strcat(arguments, ".x");
3267 break;
3269 case CHANNEL_SOURCE_Y:
3270 strcat(arguments, reg_name);
3271 strcat(arguments, ".y");
3272 break;
3274 case CHANNEL_SOURCE_Z:
3275 strcat(arguments, reg_name);
3276 strcat(arguments, ".z");
3277 break;
3279 case CHANNEL_SOURCE_W:
3280 strcat(arguments, reg_name);
3281 strcat(arguments, ".w");
3282 break;
3284 default:
3285 FIXME("Unhandled channel source %#x\n", channel_source);
3286 strcat(arguments, "undefined");
3287 break;
3290 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3293 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3294 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3296 unsigned int mask_size, remaining;
3297 DWORD fixup_mask = 0;
3298 char arguments[256];
3299 char mask_str[6];
3301 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3302 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3303 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3304 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3305 if (!(mask &= fixup_mask))
3306 return;
3308 if (is_complex_fixup(fixup))
3310 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3311 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3312 return;
3315 shader_glsl_write_mask_to_str(mask, mask_str);
3316 mask_size = shader_glsl_get_write_mask_size(mask);
3318 arguments[0] = '\0';
3319 remaining = mask_size;
3320 if (mask & WINED3DSP_WRITEMASK_0)
3322 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3323 if (--remaining) strcat(arguments, ", ");
3325 if (mask & WINED3DSP_WRITEMASK_1)
3327 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3328 if (--remaining) strcat(arguments, ", ");
3330 if (mask & WINED3DSP_WRITEMASK_2)
3332 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3333 if (--remaining) strcat(arguments, ", ");
3335 if (mask & WINED3DSP_WRITEMASK_3)
3337 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3338 if (--remaining) strcat(arguments, ", ");
3341 if (mask_size > 1)
3342 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3343 else
3344 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3347 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3349 char reg_name[256];
3350 BOOL is_color;
3352 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3353 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3356 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3357 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3358 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3359 const char *coord_reg_fmt, ...)
3361 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3362 char dst_swizzle[6];
3363 struct color_fixup_desc fixup;
3364 BOOL np2_fixup = FALSE;
3365 va_list args;
3366 int ret;
3368 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3370 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3371 * We actually rely on it for vertex shaders and SM4+. */
3372 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3374 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3375 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3377 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3378 np2_fixup = TRUE;
3380 else
3382 fixup = COLOR_FIXUP_IDENTITY;
3385 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3387 if (sample_function->output_single_component)
3388 shader_addline(ins->ctx->buffer, "vec4(");
3390 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3391 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3393 for (;;)
3395 va_start(args, coord_reg_fmt);
3396 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3397 va_end(args);
3398 if (!ret)
3399 break;
3400 if (!string_buffer_resize(ins->ctx->buffer, ret))
3401 break;
3404 if (np2_fixup)
3406 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3407 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3409 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3411 case 1:
3412 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3413 idx >> 1, (idx % 2) ? "z" : "x");
3414 break;
3415 case 2:
3416 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3417 idx >> 1, (idx % 2) ? "zw" : "xy");
3418 break;
3419 case 3:
3420 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3421 idx >> 1, (idx % 2) ? "zw" : "xy");
3422 break;
3423 case 4:
3424 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3425 idx >> 1, (idx % 2) ? "zw" : "xy");
3426 break;
3429 if (dx && dy)
3430 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3431 else if (bias)
3432 shader_addline(ins->ctx->buffer, ", %s", bias);
3433 if (sample_function->offset_size)
3435 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3436 shader_addline(ins->ctx->buffer, ", ");
3437 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3439 shader_addline(ins->ctx->buffer, ")");
3441 if (sample_function->output_single_component)
3442 shader_addline(ins->ctx->buffer, ")");
3444 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3446 if (!is_identity_fixup(fixup))
3447 shader_glsl_color_correction(ins, fixup);
3450 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3452 /* Write the final position.
3454 * OpenGL coordinates specify the center of the pixel while D3D coords
3455 * specify the corner. The offsets are stored in z and w in
3456 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3457 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3458 * a MAD. */
3459 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3460 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3462 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3463 * in utils.c
3465 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3466 * shaders are run before the homogeneous divide, so we have to take the w
3467 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3468 * z = z * 2 - w. */
3469 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3472 /*****************************************************************************
3473 * Begin processing individual instruction opcodes
3474 ****************************************************************************/
3476 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3478 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3479 struct glsl_src_param src0_param;
3480 struct glsl_src_param src1_param;
3481 DWORD write_mask;
3482 const char *op;
3484 /* Determine the GLSL operator to use based on the opcode */
3485 switch (ins->handler_idx)
3487 case WINED3DSIH_ADD: op = "+"; break;
3488 case WINED3DSIH_AND: op = "&"; break;
3489 case WINED3DSIH_DIV: op = "/"; break;
3490 case WINED3DSIH_IADD: op = "+"; break;
3491 case WINED3DSIH_ISHL: op = "<<"; break;
3492 case WINED3DSIH_ISHR: op = ">>"; break;
3493 case WINED3DSIH_MUL: op = "*"; break;
3494 case WINED3DSIH_OR: op = "|"; break;
3495 case WINED3DSIH_SUB: op = "-"; break;
3496 case WINED3DSIH_USHR: op = ">>"; break;
3497 case WINED3DSIH_XOR: op = "^"; break;
3498 default:
3499 op = "<unhandled operator>";
3500 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3501 break;
3504 write_mask = shader_glsl_append_dst(buffer, ins);
3505 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3506 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3507 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3510 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3512 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3513 struct glsl_src_param src0_param;
3514 struct glsl_src_param src1_param;
3515 unsigned int mask_size;
3516 DWORD write_mask;
3517 const char *op;
3519 write_mask = shader_glsl_append_dst(buffer, ins);
3520 mask_size = shader_glsl_get_write_mask_size(write_mask);
3521 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3522 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3524 if (mask_size > 1)
3526 switch (ins->handler_idx)
3528 case WINED3DSIH_EQ: op = "equal"; break;
3529 case WINED3DSIH_IEQ: op = "equal"; break;
3530 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3531 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3532 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3533 case WINED3DSIH_LT: op = "lessThan"; break;
3534 case WINED3DSIH_ILT: op = "lessThan"; break;
3535 case WINED3DSIH_ULT: op = "lessThan"; break;
3536 case WINED3DSIH_NE: op = "notEqual"; break;
3537 case WINED3DSIH_INE: op = "notEqual"; break;
3538 default:
3539 op = "<unhandled operator>";
3540 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3541 break;
3544 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3545 mask_size, op, src0_param.param_str, src1_param.param_str);
3547 else
3549 switch (ins->handler_idx)
3551 case WINED3DSIH_EQ: op = "=="; break;
3552 case WINED3DSIH_IEQ: op = "=="; break;
3553 case WINED3DSIH_GE: op = ">="; break;
3554 case WINED3DSIH_IGE: op = ">="; break;
3555 case WINED3DSIH_UGE: op = ">="; break;
3556 case WINED3DSIH_LT: op = "<"; break;
3557 case WINED3DSIH_ILT: op = "<"; break;
3558 case WINED3DSIH_ULT: op = "<"; break;
3559 case WINED3DSIH_NE: op = "!="; break;
3560 case WINED3DSIH_INE: op = "!="; break;
3561 default:
3562 op = "<unhandled operator>";
3563 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3564 break;
3567 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3568 src0_param.param_str, op, src1_param.param_str);
3572 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3574 struct glsl_src_param src_param;
3575 DWORD write_mask;
3576 const char *op;
3578 switch (ins->handler_idx)
3580 case WINED3DSIH_INEG: op = "-"; break;
3581 case WINED3DSIH_NOT: op = "~"; break;
3582 default:
3583 op = "<unhandled operator>";
3584 ERR("Unhandled opcode %s.\n",
3585 debug_d3dshaderinstructionhandler(ins->handler_idx));
3586 break;
3589 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3590 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3591 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3594 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3596 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3597 struct glsl_src_param src0_param;
3598 struct glsl_src_param src1_param;
3599 DWORD write_mask;
3601 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3602 * not, we can emulate it. */
3603 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3604 FIXME("64-bit integer multiplies not implemented.\n");
3606 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3608 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3609 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3610 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3612 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3613 src0_param.param_str, src1_param.param_str);
3617 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3619 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3620 struct glsl_src_param src0_param, src1_param;
3621 DWORD write_mask;
3623 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3625 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3627 char dst_mask[6];
3629 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3630 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3631 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3632 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3633 dst_mask, src0_param.param_str, src1_param.param_str);
3635 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3636 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3637 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3638 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3640 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3641 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3643 else
3645 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3646 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3647 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3648 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3651 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3653 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3654 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3655 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3656 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3660 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3661 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3663 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3664 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3665 struct glsl_src_param src0_param;
3666 DWORD write_mask;
3668 write_mask = shader_glsl_append_dst(buffer, ins);
3669 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3671 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3672 * shader versions WINED3DSIO_MOVA is used for this. */
3673 if (ins->ctx->reg_maps->shader_version.major == 1
3674 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3675 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3677 /* This is a simple floor() */
3678 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3679 if (mask_size > 1) {
3680 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3681 } else {
3682 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3685 else if (ins->handler_idx == WINED3DSIH_MOVA)
3687 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3688 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3690 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3692 if (mask_size > 1)
3693 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3694 else
3695 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3697 else
3699 if (mask_size > 1)
3700 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3701 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3702 else
3703 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3704 src0_param.param_str, src0_param.param_str);
3707 else
3709 shader_addline(buffer, "%s);\n", src0_param.param_str);
3713 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3714 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3716 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3717 struct glsl_src_param src0_param;
3718 struct glsl_src_param src1_param;
3719 DWORD dst_write_mask, src_write_mask;
3720 unsigned int dst_size;
3722 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3723 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3725 /* dp4 works on vec4, dp3 on vec3, etc. */
3726 if (ins->handler_idx == WINED3DSIH_DP4)
3727 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3728 else if (ins->handler_idx == WINED3DSIH_DP3)
3729 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3730 else
3731 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3733 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3734 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3736 if (dst_size > 1) {
3737 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3738 } else {
3739 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3743 /* Note that this instruction has some restrictions. The destination write mask
3744 * can't contain the w component, and the source swizzles have to be .xyzw */
3745 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3747 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3748 struct glsl_src_param src0_param;
3749 struct glsl_src_param src1_param;
3750 char dst_mask[6];
3752 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3753 shader_glsl_append_dst(ins->ctx->buffer, ins);
3754 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3755 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3756 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3759 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3761 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3763 if (!stream)
3764 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3765 else
3766 FIXME("Unhandled primitive stream %u.\n", stream);
3769 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3770 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3771 * GLSL uses the value as-is. */
3772 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3774 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3775 struct glsl_src_param src0_param;
3776 struct glsl_src_param src1_param;
3777 DWORD dst_write_mask;
3778 unsigned int dst_size;
3780 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3781 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3783 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3784 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3786 if (dst_size > 1)
3788 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3789 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3791 else
3793 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3794 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3798 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3799 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3801 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3802 struct glsl_src_param src_param;
3803 const char *instruction;
3804 DWORD write_mask;
3805 unsigned i;
3807 /* Determine the GLSL function to use based on the opcode */
3808 /* TODO: Possibly make this a table for faster lookups */
3809 switch (ins->handler_idx)
3811 case WINED3DSIH_ABS: instruction = "abs"; break;
3812 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3813 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3814 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3815 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3816 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3817 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3818 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3819 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3820 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3821 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3822 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3823 case WINED3DSIH_FRC: instruction = "fract"; break;
3824 case WINED3DSIH_IMAX: instruction = "max"; break;
3825 case WINED3DSIH_IMIN: instruction = "min"; break;
3826 case WINED3DSIH_MAX: instruction = "max"; break;
3827 case WINED3DSIH_MIN: instruction = "min"; break;
3828 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3829 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3830 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3831 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3832 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3833 case WINED3DSIH_UMAX: instruction = "max"; break;
3834 case WINED3DSIH_UMIN: instruction = "min"; break;
3835 default: instruction = "";
3836 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3837 break;
3840 write_mask = shader_glsl_append_dst(buffer, ins);
3842 /* In D3D bits are numbered from the most significant bit. */
3843 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3844 shader_addline(buffer, "31 - ");
3845 shader_addline(buffer, "%s(", instruction);
3847 if (ins->src_count)
3849 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3850 shader_addline(buffer, "%s", src_param.param_str);
3851 for (i = 1; i < ins->src_count; ++i)
3853 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3854 shader_addline(buffer, ", %s", src_param.param_str);
3858 shader_addline(buffer, "));\n");
3861 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3863 struct wined3d_shader_dst_param dst;
3864 struct glsl_src_param src;
3865 DWORD write_mask;
3866 const char *fmt;
3867 unsigned int i;
3869 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3870 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3872 dst = ins->dst[0];
3873 for (i = 0; i < 4; ++i)
3875 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3876 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3877 &dst, dst.reg.data_type)))
3878 continue;
3880 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3881 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3885 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3887 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3888 struct wined3d_shader_dst_param dst;
3889 struct glsl_src_param src[4];
3890 const char *instruction;
3891 unsigned int i, j;
3892 DWORD write_mask;
3894 switch (ins->handler_idx)
3896 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3897 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3898 default:
3899 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3900 return;
3903 dst = ins->dst[0];
3904 for (i = 0; i < 4; ++i)
3906 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
3907 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3908 &dst, dst.reg.data_type)))
3909 continue;
3911 for (j = 0; j < ins->src_count; ++j)
3912 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3913 shader_addline(buffer, "%s(", instruction);
3914 for (j = 0; j < ins->src_count - 2; ++j)
3915 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3916 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3920 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3922 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3924 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3925 struct glsl_src_param src_param;
3926 unsigned int mask_size;
3927 DWORD write_mask;
3928 char dst_mask[6];
3930 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3931 mask_size = shader_glsl_get_write_mask_size(write_mask);
3932 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3934 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3935 src_param.param_str, src_param.param_str);
3936 shader_glsl_append_dst(buffer, ins);
3938 if (mask_size > 1)
3940 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3941 mask_size, src_param.param_str);
3943 else
3945 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3946 src_param.param_str);
3950 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3952 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3953 ins->ctx->reg_maps->shader_version.minor);
3954 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3955 struct glsl_src_param src0_param;
3956 const char *prefix, *suffix;
3957 unsigned int dst_size;
3958 DWORD dst_write_mask;
3960 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3961 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3963 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3964 dst_write_mask = WINED3DSP_WRITEMASK_3;
3966 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3968 switch (ins->handler_idx)
3970 case WINED3DSIH_EXP:
3971 case WINED3DSIH_EXPP:
3972 prefix = "exp2(";
3973 suffix = ")";
3974 break;
3976 case WINED3DSIH_LOG:
3977 case WINED3DSIH_LOGP:
3978 prefix = "log2(abs(";
3979 suffix = "))";
3980 break;
3982 case WINED3DSIH_RCP:
3983 prefix = "1.0 / ";
3984 suffix = "";
3985 break;
3987 case WINED3DSIH_RSQ:
3988 prefix = "inversesqrt(abs(";
3989 suffix = "))";
3990 break;
3992 default:
3993 prefix = "";
3994 suffix = "";
3995 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3996 break;
3999 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
4000 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
4001 else
4002 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
4005 /** Process the WINED3DSIO_EXPP instruction in GLSL:
4006 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
4007 * dst.x = 2^(floor(src))
4008 * dst.y = src - floor(src)
4009 * dst.z = 2^src (partial precision is allowed, but optional)
4010 * dst.w = 1.0;
4011 * For 2.0 shaders, just do this (honoring writemask and swizzle):
4012 * dst = 2^src; (partial precision is allowed, but optional)
4014 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
4016 if (ins->ctx->reg_maps->shader_version.major < 2)
4018 struct glsl_src_param src_param;
4019 char dst_mask[6];
4021 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
4023 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
4024 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
4025 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
4026 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
4028 shader_glsl_append_dst(ins->ctx->buffer, ins);
4029 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4030 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
4031 return;
4034 shader_glsl_scalar_op(ins);
4037 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4038 const char *vector_constructor, const char *scalar_constructor)
4040 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4041 struct glsl_src_param src_param;
4042 unsigned int mask_size;
4043 DWORD write_mask;
4045 write_mask = shader_glsl_append_dst(buffer, ins);
4046 mask_size = shader_glsl_get_write_mask_size(write_mask);
4047 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4049 if (mask_size > 1)
4050 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4051 else
4052 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4055 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4057 shader_glsl_cast(ins, "ivec", "int");
4060 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4062 shader_glsl_cast(ins, "uvec", "uint");
4065 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4067 shader_glsl_cast(ins, "vec", "float");
4070 /** Process signed comparison opcodes in GLSL. */
4071 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4073 struct glsl_src_param src0_param;
4074 struct glsl_src_param src1_param;
4075 DWORD write_mask;
4076 unsigned int mask_size;
4078 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4079 mask_size = shader_glsl_get_write_mask_size(write_mask);
4080 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4081 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4083 if (mask_size > 1) {
4084 const char *compare;
4086 switch(ins->handler_idx)
4088 case WINED3DSIH_SLT: compare = "lessThan"; break;
4089 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4090 default: compare = "";
4091 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4094 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4095 src0_param.param_str, src1_param.param_str);
4096 } else {
4097 switch(ins->handler_idx)
4099 case WINED3DSIH_SLT:
4100 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4101 * to return 0.0 but step returns 1.0 because step is not < x
4102 * An alternative is a bvec compare padded with an unused second component.
4103 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4104 * issue. Playing with not() is not possible either because not() does not accept
4105 * a scalar.
4107 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4108 src0_param.param_str, src1_param.param_str);
4109 break;
4110 case WINED3DSIH_SGE:
4111 /* Here we can use the step() function and safe a conditional */
4112 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4113 break;
4114 default:
4115 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4121 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4123 const char *condition_prefix, *condition_suffix;
4124 struct wined3d_shader_dst_param dst;
4125 struct glsl_src_param src0_param;
4126 struct glsl_src_param src1_param;
4127 struct glsl_src_param src2_param;
4128 BOOL temp_destination = FALSE;
4129 DWORD cmp_channel = 0;
4130 unsigned int i, j;
4131 char mask_char[6];
4132 DWORD write_mask;
4134 switch (ins->handler_idx)
4136 case WINED3DSIH_CMP:
4137 condition_prefix = "";
4138 condition_suffix = " >= 0.0";
4139 break;
4141 case WINED3DSIH_CND:
4142 condition_prefix = "";
4143 condition_suffix = " > 0.5";
4144 break;
4146 case WINED3DSIH_MOVC:
4147 condition_prefix = "bool(";
4148 condition_suffix = ")";
4149 break;
4151 default:
4152 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4153 condition_prefix = "<unhandled prefix>";
4154 condition_suffix = "<unhandled suffix>";
4155 break;
4158 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4160 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4161 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4162 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4163 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4165 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4166 condition_prefix, src0_param.param_str, condition_suffix,
4167 src1_param.param_str, src2_param.param_str);
4168 return;
4171 dst = ins->dst[0];
4173 /* Splitting the instruction up in multiple lines imposes a problem:
4174 * The first lines may overwrite source parameters of the following lines.
4175 * Deal with that by using a temporary destination register if needed. */
4176 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4177 && ins->src[0].reg.type == dst.reg.type)
4178 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4179 && ins->src[1].reg.type == dst.reg.type)
4180 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4181 && ins->src[2].reg.type == dst.reg.type))
4182 temp_destination = TRUE;
4184 /* Cycle through all source0 channels. */
4185 for (i = 0; i < 4; ++i)
4187 write_mask = 0;
4188 /* Find the destination channels which use the current source0 channel. */
4189 for (j = 0; j < 4; ++j)
4191 if (shader_glsl_swizzle_get_component(ins->src[0].swizzle, j) == i)
4193 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4194 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4197 dst.write_mask = ins->dst[0].write_mask & write_mask;
4199 if (temp_destination)
4201 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4202 continue;
4203 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4205 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4206 continue;
4208 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4209 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4210 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4212 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4213 condition_prefix, src0_param.param_str, condition_suffix,
4214 src1_param.param_str, src2_param.param_str);
4217 if (temp_destination)
4219 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4220 shader_glsl_append_dst(ins->ctx->buffer, ins);
4221 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4225 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4226 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4227 * the compare is done per component of src0. */
4228 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4230 struct glsl_src_param src0_param;
4231 struct glsl_src_param src1_param;
4232 struct glsl_src_param src2_param;
4233 DWORD write_mask;
4234 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4235 ins->ctx->reg_maps->shader_version.minor);
4237 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4239 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4240 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4241 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4242 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4244 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4245 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4246 else
4247 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4248 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4249 return;
4252 shader_glsl_conditional_move(ins);
4255 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4256 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4258 struct glsl_src_param src0_param;
4259 struct glsl_src_param src1_param;
4260 struct glsl_src_param src2_param;
4261 DWORD write_mask;
4263 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4264 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4265 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4266 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4267 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4268 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4271 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4272 Vertex shaders to GLSL codes */
4273 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4275 int i;
4276 int nComponents = 0;
4277 struct wined3d_shader_dst_param tmp_dst = {{0}};
4278 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4279 struct wined3d_shader_instruction tmp_ins;
4281 memset(&tmp_ins, 0, sizeof(tmp_ins));
4283 /* Set constants for the temporary argument */
4284 tmp_ins.ctx = ins->ctx;
4285 tmp_ins.dst_count = 1;
4286 tmp_ins.dst = &tmp_dst;
4287 tmp_ins.src_count = 2;
4288 tmp_ins.src = tmp_src;
4290 switch(ins->handler_idx)
4292 case WINED3DSIH_M4x4:
4293 nComponents = 4;
4294 tmp_ins.handler_idx = WINED3DSIH_DP4;
4295 break;
4296 case WINED3DSIH_M4x3:
4297 nComponents = 3;
4298 tmp_ins.handler_idx = WINED3DSIH_DP4;
4299 break;
4300 case WINED3DSIH_M3x4:
4301 nComponents = 4;
4302 tmp_ins.handler_idx = WINED3DSIH_DP3;
4303 break;
4304 case WINED3DSIH_M3x3:
4305 nComponents = 3;
4306 tmp_ins.handler_idx = WINED3DSIH_DP3;
4307 break;
4308 case WINED3DSIH_M3x2:
4309 nComponents = 2;
4310 tmp_ins.handler_idx = WINED3DSIH_DP3;
4311 break;
4312 default:
4313 break;
4316 tmp_dst = ins->dst[0];
4317 tmp_src[0] = ins->src[0];
4318 tmp_src[1] = ins->src[1];
4319 for (i = 0; i < nComponents; ++i)
4321 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4322 shader_glsl_dot(&tmp_ins);
4323 ++tmp_src[1].reg.idx[0].offset;
4328 The LRP instruction performs a component-wise linear interpolation
4329 between the second and third operands using the first operand as the
4330 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4331 This is equivalent to mix(src2, src1, src0);
4333 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4335 struct glsl_src_param src0_param;
4336 struct glsl_src_param src1_param;
4337 struct glsl_src_param src2_param;
4338 DWORD write_mask;
4340 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4342 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4343 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4344 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4346 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4347 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4350 /** Process the WINED3DSIO_LIT instruction in GLSL:
4351 * dst.x = dst.w = 1.0
4352 * dst.y = (src0.x > 0) ? src0.x
4353 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4354 * where src.w is clamped at +- 128
4356 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4358 struct glsl_src_param src0_param;
4359 struct glsl_src_param src1_param;
4360 struct glsl_src_param src3_param;
4361 char dst_mask[6];
4363 shader_glsl_append_dst(ins->ctx->buffer, ins);
4364 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4366 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4367 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4368 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4370 /* The sdk specifies the instruction like this
4371 * dst.x = 1.0;
4372 * if(src.x > 0.0) dst.y = src.x
4373 * else dst.y = 0.0.
4374 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4375 * else dst.z = 0.0;
4376 * dst.w = 1.0;
4377 * (where power = src.w clamped between -128 and 128)
4379 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4380 * dst.x = 1.0 ... No further explanation needed
4381 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4382 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4383 * dst.w = 1.0. ... Nothing fancy.
4385 * So we still have one conditional in there. So do this:
4386 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4388 * 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),
4389 * 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.
4390 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4392 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4393 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4394 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4396 shader_addline(ins->ctx->buffer,
4397 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4398 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4399 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4400 src0_param.param_str, src3_param.param_str, dst_mask);
4403 /** Process the WINED3DSIO_DST instruction in GLSL:
4404 * dst.x = 1.0
4405 * dst.y = src0.x * src0.y
4406 * dst.z = src0.z
4407 * dst.w = src1.w
4409 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4411 struct glsl_src_param src0y_param;
4412 struct glsl_src_param src0z_param;
4413 struct glsl_src_param src1y_param;
4414 struct glsl_src_param src1w_param;
4415 char dst_mask[6];
4417 shader_glsl_append_dst(ins->ctx->buffer, ins);
4418 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4420 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4421 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4422 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4423 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4425 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4426 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4429 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4430 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4431 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4433 * dst.x = cos(src0.?)
4434 * dst.y = sin(src0.?)
4435 * dst.z = dst.z
4436 * dst.w = dst.w
4438 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4440 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4441 struct glsl_src_param src0_param;
4442 DWORD write_mask;
4444 if (ins->ctx->reg_maps->shader_version.major < 4)
4446 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4448 write_mask = shader_glsl_append_dst(buffer, ins);
4449 switch (write_mask)
4451 case WINED3DSP_WRITEMASK_0:
4452 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4453 break;
4455 case WINED3DSP_WRITEMASK_1:
4456 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4457 break;
4459 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4460 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4461 src0_param.param_str, src0_param.param_str);
4462 break;
4464 default:
4465 ERR("Write mask should be .x, .y or .xy\n");
4466 break;
4469 return;
4472 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4475 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4477 char dst_mask[6];
4479 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4480 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4481 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4483 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4484 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4485 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4487 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4488 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4490 else
4492 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4493 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4494 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4497 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4499 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4500 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4501 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4505 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4506 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4507 * generate invalid code
4509 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4511 struct glsl_src_param src0_param;
4512 DWORD write_mask;
4514 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4515 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4517 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4520 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4521 * Start a for() loop where src1.y is the initial value of aL,
4522 * increment aL by src1.z for a total of src1.x iterations.
4523 * Need to use a temporary variable for this operation.
4525 /* FIXME: I don't think nested loops will work correctly this way. */
4526 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4528 struct wined3d_shader_parser_state *state = ins->ctx->state;
4529 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4530 const struct wined3d_shader *shader = ins->ctx->shader;
4531 const struct wined3d_shader_lconst *constant;
4532 struct glsl_src_param src1_param;
4533 const DWORD *control_values = NULL;
4535 if (ins->ctx->reg_maps->shader_version.major < 4)
4537 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4539 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4540 * class hardware doesn't support real varying indexing, but Microsoft
4541 * designed this feature for Shader model 2.x+. If the loop control is
4542 * known at compile time, the GLSL compiler can unroll the loop, and
4543 * replace indirect addressing with direct addressing. */
4544 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4546 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4548 if (constant->idx == ins->src[1].reg.idx[0].offset)
4550 control_values = constant->value;
4551 break;
4556 if (control_values)
4558 struct wined3d_shader_loop_control loop_control;
4559 loop_control.count = control_values[0];
4560 loop_control.start = control_values[1];
4561 loop_control.step = (int)control_values[2];
4563 if (loop_control.step > 0)
4565 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4566 state->current_loop_depth, loop_control.start,
4567 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4568 state->current_loop_depth, loop_control.step);
4570 else if (loop_control.step < 0)
4572 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4573 state->current_loop_depth, loop_control.start,
4574 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4575 state->current_loop_depth, loop_control.step);
4577 else
4579 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4580 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4581 state->current_loop_depth, loop_control.count,
4582 state->current_loop_depth);
4585 else
4587 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4588 state->current_loop_depth, state->current_loop_reg,
4589 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4590 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4593 ++state->current_loop_reg;
4595 else
4597 shader_addline(buffer, "for (;;)\n{\n");
4600 ++state->current_loop_depth;
4603 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4605 struct wined3d_shader_parser_state *state = ins->ctx->state;
4607 shader_addline(ins->ctx->buffer, "}\n");
4609 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4611 --state->current_loop_depth;
4612 --state->current_loop_reg;
4615 if (ins->handler_idx == WINED3DSIH_ENDREP)
4617 --state->current_loop_depth;
4621 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4623 struct wined3d_shader_parser_state *state = ins->ctx->state;
4624 const struct wined3d_shader *shader = ins->ctx->shader;
4625 const struct wined3d_shader_lconst *constant;
4626 struct glsl_src_param src0_param;
4627 const DWORD *control_values = NULL;
4629 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4630 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4632 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4634 if (constant->idx == ins->src[0].reg.idx[0].offset)
4636 control_values = constant->value;
4637 break;
4642 if (control_values)
4644 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4645 state->current_loop_depth, state->current_loop_depth,
4646 control_values[0], state->current_loop_depth);
4648 else
4650 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4651 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4652 state->current_loop_depth, state->current_loop_depth,
4653 src0_param.param_str, state->current_loop_depth);
4656 ++state->current_loop_depth;
4659 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4661 struct glsl_src_param src0_param;
4663 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4664 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4667 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4669 struct glsl_src_param src0_param;
4671 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4672 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4675 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4677 shader_addline(ins->ctx->buffer, "default:\n");
4680 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4682 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4683 struct glsl_src_param src0_param;
4685 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4686 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4689 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4691 struct glsl_src_param src0_param;
4692 struct glsl_src_param src1_param;
4694 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4695 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4697 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4698 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4701 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4703 shader_addline(ins->ctx->buffer, "} else {\n");
4706 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4708 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4710 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4711 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4712 shader_glsl_fixup_position(ins->ctx->buffer);
4714 if (!stream)
4715 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4716 else
4717 FIXME("Unhandled primitive stream %u.\n", stream);
4720 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4722 shader_addline(ins->ctx->buffer, "break;\n");
4725 /* FIXME: According to MSDN the compare is done per component. */
4726 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
4728 struct glsl_src_param src0_param;
4729 struct glsl_src_param src1_param;
4731 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4732 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4734 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
4735 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4738 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4740 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4741 struct glsl_src_param src_param;
4743 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4744 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4747 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4749 shader_addline(ins->ctx->buffer, "continue;\n");
4752 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4754 shader_addline(ins->ctx->buffer, "}\n");
4755 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4757 /* Subroutines appear at the end of the shader. */
4758 ins->ctx->state->in_subroutine = TRUE;
4761 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4763 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4766 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4768 struct glsl_src_param src1_param;
4770 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4771 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4772 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4775 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4777 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4779 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4781 shader_glsl_generate_shader_epilogue(ins->ctx);
4782 shader_addline(ins->ctx->buffer, "return;\n");
4786 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4788 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4789 ins->ctx->reg_maps->shader_version.minor);
4790 struct glsl_sample_function sample_function;
4791 DWORD sample_flags = 0;
4792 DWORD resource_idx;
4793 DWORD mask = 0, swizzle;
4794 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4796 /* 1.0-1.4: Use destination register as sampler source.
4797 * 2.0+: Use provided sampler source. */
4798 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4799 resource_idx = ins->dst[0].reg.idx[0].offset;
4800 else
4801 resource_idx = ins->src[1].reg.idx[0].offset;
4803 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4805 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4806 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4807 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4809 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4810 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4812 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4813 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4815 case WINED3D_TTFF_COUNT1:
4816 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4817 break;
4818 case WINED3D_TTFF_COUNT2:
4819 mask = WINED3DSP_WRITEMASK_1;
4820 break;
4821 case WINED3D_TTFF_COUNT3:
4822 mask = WINED3DSP_WRITEMASK_2;
4823 break;
4824 case WINED3D_TTFF_COUNT4:
4825 case WINED3D_TTFF_DISABLE:
4826 mask = WINED3DSP_WRITEMASK_3;
4827 break;
4831 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4833 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4835 if (src_mod == WINED3DSPSM_DZ) {
4836 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4837 mask = WINED3DSP_WRITEMASK_2;
4838 } else if (src_mod == WINED3DSPSM_DW) {
4839 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4840 mask = WINED3DSP_WRITEMASK_3;
4843 else
4845 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4846 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4848 /* ps 2.0 texldp instruction always divides by the fourth component. */
4849 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4850 mask = WINED3DSP_WRITEMASK_3;
4854 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4855 mask |= sample_function.coord_mask;
4856 sample_function.coord_mask = mask;
4858 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4859 else swizzle = ins->src[1].swizzle;
4861 /* 1.0-1.3: Use destination register as coordinate source.
4862 1.4+: Use provided coordinate source register. */
4863 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4865 char coord_mask[6];
4866 shader_glsl_write_mask_to_str(mask, coord_mask);
4867 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4868 "T%u%s", resource_idx, coord_mask);
4870 else
4872 struct glsl_src_param coord_param;
4873 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4874 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4876 struct glsl_src_param bias;
4877 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4878 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4879 NULL, "%s", coord_param.param_str);
4880 } else {
4881 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4882 "%s", coord_param.param_str);
4885 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4888 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4890 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4891 struct glsl_src_param coord_param, dx_param, dy_param;
4892 struct glsl_sample_function sample_function;
4893 DWORD sampler_idx;
4894 DWORD swizzle = ins->src[1].swizzle;
4896 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4897 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4899 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4900 shader_glsl_tex(ins);
4901 return;
4904 sampler_idx = ins->src[1].reg.idx[0].offset;
4906 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4907 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4908 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4909 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4911 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4912 NULL, NULL, "%s", coord_param.param_str);
4913 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4916 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4918 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4919 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4920 struct glsl_src_param coord_param, lod_param;
4921 struct glsl_sample_function sample_function;
4922 DWORD swizzle = ins->src[1].swizzle;
4923 DWORD sampler_idx;
4925 sampler_idx = ins->src[1].reg.idx[0].offset;
4927 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4928 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4930 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4932 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4933 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4935 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4936 * However, the NVIDIA drivers allow them in fragment shaders as well,
4937 * even without the appropriate extension. */
4938 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4940 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4941 "%s", coord_param.param_str);
4942 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4945 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4946 unsigned int resource_idx, unsigned int sampler_idx)
4948 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4949 unsigned int i;
4951 for (i = 0; i < sampler_map->count; ++i)
4953 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4954 return entries[i].bind_idx;
4957 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4959 return ~0u;
4962 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
4964 const BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
4965 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
4966 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4967 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4968 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4969 struct glsl_src_param structure_idx, offset, data, data2;
4970 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4971 enum wined3d_shader_resource_type resource_type;
4972 struct wined3d_string_buffer *address;
4973 enum wined3d_data_type data_type;
4974 unsigned int uav_idx, stride;
4975 DWORD coord_mask;
4976 const char *op;
4978 uav_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
4979 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4980 if (resource_type >= ARRAY_SIZE(resource_type_info))
4982 ERR("Unexpected resource type %#x.\n", resource_type);
4983 return;
4985 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4986 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4987 stride = reg_maps->uav_resource_info[uav_idx].stride;
4989 switch (ins->handler_idx)
4991 case WINED3DSIH_ATOMIC_AND:
4992 case WINED3DSIH_IMM_ATOMIC_AND:
4993 op = "imageAtomicAnd";
4994 break;
4995 case WINED3DSIH_ATOMIC_CMP_STORE:
4996 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
4997 op = "imageAtomicCompSwap";
4998 break;
4999 case WINED3DSIH_ATOMIC_IADD:
5000 case WINED3DSIH_IMM_ATOMIC_IADD:
5001 op = "imageAtomicAdd";
5002 break;
5003 case WINED3DSIH_ATOMIC_IMAX:
5004 case WINED3DSIH_IMM_ATOMIC_IMAX:
5005 op = "imageAtomicMax";
5006 if (data_type != WINED3D_DATA_INT)
5008 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5009 return;
5011 break;
5012 case WINED3DSIH_ATOMIC_IMIN:
5013 case WINED3DSIH_IMM_ATOMIC_IMIN:
5014 op = "imageAtomicMin";
5015 if (data_type != WINED3D_DATA_INT)
5017 FIXME("Unhandled opcode %#x for unsigned integers.\n", ins->handler_idx);
5018 return;
5020 break;
5021 case WINED3DSIH_ATOMIC_OR:
5022 case WINED3DSIH_IMM_ATOMIC_OR:
5023 op = "imageAtomicOr";
5024 break;
5025 case WINED3DSIH_ATOMIC_UMAX:
5026 case WINED3DSIH_IMM_ATOMIC_UMAX:
5027 op = "imageAtomicMax";
5028 if (data_type != WINED3D_DATA_UINT)
5030 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5031 return;
5033 break;
5034 case WINED3DSIH_ATOMIC_UMIN:
5035 case WINED3DSIH_IMM_ATOMIC_UMIN:
5036 op = "imageAtomicMin";
5037 if (data_type != WINED3D_DATA_UINT)
5039 FIXME("Unhandled opcode %#x for signed integers.\n", ins->handler_idx);
5040 return;
5042 break;
5043 case WINED3DSIH_ATOMIC_XOR:
5044 case WINED3DSIH_IMM_ATOMIC_XOR:
5045 op = "imageAtomicXor";
5046 break;
5047 case WINED3DSIH_IMM_ATOMIC_EXCH:
5048 op = "imageAtomicExchange";
5049 break;
5050 default:
5051 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
5052 return;
5055 address = string_buffer_get(priv->string_buffers);
5056 if (stride)
5058 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &structure_idx);
5059 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &offset);
5060 string_buffer_sprintf(address, "%s * %u + %s / 4", structure_idx.param_str, stride, offset.param_str);
5062 else
5064 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &offset);
5065 string_buffer_sprintf(address, "%s", offset.param_str);
5066 if (reg_maps->uav_resource_info[uav_idx].flags & WINED3D_VIEW_BUFFER_RAW)
5067 shader_addline(address, "/ 4");
5070 if (is_imm_instruction)
5071 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5073 shader_addline(buffer, "%s(%s_image%u, %s, ",
5074 op, shader_glsl_get_prefix(version->type), uav_idx, address->buffer);
5076 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data, data_type);
5077 shader_addline(buffer, "%s", data.param_str);
5078 if (ins->src_count >= 3)
5080 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data2, data_type);
5081 shader_addline(buffer, ", %s", data2.param_str);
5084 if (is_imm_instruction)
5085 shader_addline(buffer, ")");
5086 shader_addline(buffer, ");\n");
5088 string_buffer_release(priv->string_buffers, address);
5091 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5093 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5094 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5095 enum wined3d_shader_resource_type resource_type;
5096 struct glsl_src_param image_coord_param;
5097 enum wined3d_data_type data_type;
5098 DWORD coord_mask, write_mask;
5099 unsigned int uav_idx;
5100 char dst_swizzle[6];
5102 uav_idx = ins->src[1].reg.idx[0].offset;
5103 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5105 ERR("Invalid UAV index %u.\n", uav_idx);
5106 return;
5108 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5109 if (resource_type >= ARRAY_SIZE(resource_type_info))
5111 ERR("Unexpected resource type %#x.\n", resource_type);
5112 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5114 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5115 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5117 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5118 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5120 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5121 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5122 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5125 static void shader_glsl_ld_buffer(const struct wined3d_shader_instruction *ins)
5127 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5128 const struct wined3d_shader_src_param *src = &ins->src[ins->src_count - 1];
5129 unsigned int i, swizzle, resource_idx, bind_idx, stride, src_idx = 0;
5130 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5131 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5132 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5133 struct glsl_src_param structure_idx, offset;
5134 struct wined3d_string_buffer *address;
5135 struct wined3d_shader_dst_param dst;
5136 const char *function, *resource;
5138 resource_idx = src->reg.idx[0].offset;
5139 if (src->reg.type == WINED3DSPR_RESOURCE)
5141 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5143 ERR("Invalid resource index %u.\n", resource_idx);
5144 return;
5146 stride = reg_maps->resource_info[resource_idx].stride;
5147 bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, WINED3D_SAMPLER_DEFAULT);
5148 function = "texelFetch";
5149 resource = "sampler";
5151 else
5153 if (resource_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5155 ERR("Invalid UAV index %u.\n", resource_idx);
5156 return;
5158 stride = reg_maps->uav_resource_info[resource_idx].stride;
5159 bind_idx = resource_idx;
5160 function = "imageLoad";
5161 resource = "image";
5164 address = string_buffer_get(priv->string_buffers);
5165 if (ins->handler_idx == WINED3DSIH_LD_STRUCTURED)
5167 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5168 shader_addline(address, "%s * %u + ", structure_idx.param_str, stride);
5170 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5171 shader_addline(address, "%s / 4", offset.param_str);
5173 dst = ins->dst[0];
5174 for (i = 0; i < 4; ++i)
5176 dst.write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i);
5177 if (!shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type))
5178 continue;
5180 swizzle = shader_glsl_swizzle_get_component(src->swizzle, i);
5181 shader_addline(buffer, "%s(%s_%s%u, %s + %u).x);\n",
5182 function, prefix, resource, bind_idx, address->buffer, swizzle);
5185 string_buffer_release(priv->string_buffers, address);
5188 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5190 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5191 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5192 struct glsl_src_param image_coord_param, image_data_param;
5193 enum wined3d_shader_resource_type resource_type;
5194 enum wined3d_data_type data_type;
5195 unsigned int uav_idx;
5196 DWORD coord_mask;
5198 uav_idx = ins->dst[0].reg.idx[0].offset;
5199 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5201 ERR("Invalid UAV index %u.\n", uav_idx);
5202 return;
5204 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5205 if (resource_type >= ARRAY_SIZE(resource_type_info))
5207 ERR("Unexpected resource type %#x.\n", resource_type);
5208 return;
5210 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5211 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5213 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5214 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5215 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5216 shader_glsl_get_prefix(version->type), uav_idx,
5217 image_coord_param.param_str, image_data_param.param_str);
5220 static void shader_glsl_store_buffer(const struct wined3d_shader_instruction *ins)
5222 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5223 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5224 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5225 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5226 struct glsl_src_param structure_idx, offset, data;
5227 struct wined3d_string_buffer *address;
5228 unsigned int i, uav_idx, src_idx = 0;
5229 DWORD write_mask;
5231 uav_idx = ins->dst[0].reg.idx[0].offset;
5232 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5234 ERR("Invalid UAV index %u.\n", uav_idx);
5235 return;
5238 address = string_buffer_get(priv->string_buffers);
5239 if (ins->handler_idx == WINED3DSIH_STORE_STRUCTURED)
5241 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &structure_idx);
5242 shader_addline(address, "%s * %u + ", structure_idx.param_str,
5243 reg_maps->uav_resource_info[uav_idx].stride);
5245 shader_glsl_add_src_param(ins, &ins->src[src_idx++], WINED3DSP_WRITEMASK_0, &offset);
5246 shader_addline(address, "%s / 4", offset.param_str);
5248 for (i = 0; i < 4; ++i)
5250 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5251 continue;
5253 shader_glsl_add_src_param(ins, &ins->src[src_idx], write_mask, &data);
5255 shader_addline(buffer, "imageStore(%s_image%u, %s + %u, uvec4(%s, 0, 0, 0));\n",
5256 prefix, uav_idx, address->buffer, i, data.param_str);
5259 string_buffer_release(priv->string_buffers, address);
5262 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5264 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5265 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5266 enum wined3d_shader_resource_type resource_type;
5267 enum wined3d_shader_register_type reg_type;
5268 unsigned int resource_idx, bind_idx, i;
5269 enum wined3d_data_type dst_data_type;
5270 struct glsl_src_param lod_param;
5271 char dst_swizzle[6];
5272 DWORD write_mask;
5274 dst_data_type = ins->dst[0].reg.data_type;
5275 if (ins->flags == WINED3DSI_RESINFO_UINT)
5276 dst_data_type = WINED3D_DATA_UINT;
5277 else if (ins->flags)
5278 FIXME("Unhandled flags %#x.\n", ins->flags);
5280 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5281 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5283 reg_type = ins->src[1].reg.type;
5284 resource_idx = ins->src[1].reg.idx[0].offset;
5285 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5286 if (reg_type == WINED3DSPR_RESOURCE)
5288 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5289 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5290 resource_idx, WINED3D_SAMPLER_DEFAULT);
5292 else
5294 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5295 bind_idx = resource_idx;
5298 if (resource_type >= ARRAY_SIZE(resource_type_info))
5300 ERR("Unexpected resource type %#x.\n", resource_type);
5301 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5304 if (dst_data_type == WINED3D_DATA_UINT)
5305 shader_addline(ins->ctx->buffer, "uvec4(");
5306 else
5307 shader_addline(ins->ctx->buffer, "vec4(");
5309 if (reg_type == WINED3DSPR_RESOURCE)
5311 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5312 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5314 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5315 shader_addline(ins->ctx->buffer, "0, ");
5317 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5319 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5320 shader_glsl_get_prefix(version->type), bind_idx);
5322 else
5324 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5325 shader_addline(ins->ctx->buffer, "1");
5328 else
5330 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5331 shader_glsl_get_prefix(version->type), bind_idx);
5333 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5334 shader_addline(ins->ctx->buffer, "0, ");
5336 /* For UAVs the returned miplevel count is always 1. */
5337 shader_addline(ins->ctx->buffer, "1");
5340 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5343 /* FIXME: The current implementation does not handle multisample textures correctly. */
5344 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5346 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5347 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5348 struct glsl_src_param coord_param, lod_param;
5349 struct glsl_sample_function sample_function;
5350 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5351 BOOL has_lod_param;
5353 if (wined3d_shader_instruction_has_texel_offset(ins))
5354 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5356 resource_idx = ins->src[1].reg.idx[0].offset;
5357 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5359 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5361 ERR("Invalid resource index %u.\n", resource_idx);
5362 return;
5364 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5366 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5367 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5368 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5369 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5370 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5371 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5372 "%s", coord_param.param_str);
5373 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5376 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5378 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5379 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5380 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5381 struct glsl_sample_function sample_function;
5382 DWORD flags = 0;
5384 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5385 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5386 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5387 flags |= WINED3D_GLSL_SAMPLE_LOD;
5388 if (wined3d_shader_instruction_has_texel_offset(ins))
5389 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5391 resource_idx = ins->src[1].reg.idx[0].offset;
5392 sampler_idx = ins->src[2].reg.idx[0].offset;
5394 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5395 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5397 switch (ins->handler_idx)
5399 case WINED3DSIH_SAMPLE:
5400 break;
5401 case WINED3DSIH_SAMPLE_B:
5402 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5403 lod_param_str = lod_param.param_str;
5404 break;
5405 case WINED3DSIH_SAMPLE_GRAD:
5406 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5407 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5408 dx_param_str = dx_param.param_str;
5409 dy_param_str = dy_param.param_str;
5410 break;
5411 case WINED3DSIH_SAMPLE_LOD:
5412 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5413 lod_param_str = lod_param.param_str;
5414 break;
5415 default:
5416 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5417 break;
5420 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5421 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5422 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5423 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5426 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5428 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5429 struct glsl_src_param coord_param, compare_param;
5430 struct glsl_sample_function sample_function;
5431 const char *lod_param = NULL;
5432 DWORD flags = 0;
5433 UINT coord_size;
5435 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5437 lod_param = "0";
5438 flags |= WINED3D_GLSL_SAMPLE_LOD;
5441 if (wined3d_shader_instruction_has_texel_offset(ins))
5442 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5444 resource_idx = ins->src[1].reg.idx[0].offset;
5445 sampler_idx = ins->src[2].reg.idx[0].offset;
5447 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5448 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5449 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5450 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5451 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5452 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5453 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5454 coord_size, coord_param.param_str, compare_param.param_str);
5455 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5458 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5460 /* FIXME: Make this work for more than just 2D textures */
5461 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5462 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5464 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5466 char dst_mask[6];
5468 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5469 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5470 ins->dst[0].reg.idx[0].offset, dst_mask);
5472 else
5474 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5475 DWORD reg = ins->src[0].reg.idx[0].offset;
5476 char dst_swizzle[6];
5478 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5480 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5482 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5483 struct glsl_src_param div_param;
5484 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5486 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5488 if (mask_size > 1)
5489 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5490 else
5491 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5493 else
5495 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5500 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5501 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5502 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5503 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5505 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5506 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5507 struct glsl_sample_function sample_function;
5508 struct glsl_src_param src0_param;
5509 UINT mask_size;
5511 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5513 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5514 * scalar, and projected sampling would require 4.
5516 * It is a dependent read - not valid with conditional NP2 textures
5518 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5519 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5521 switch(mask_size)
5523 case 1:
5524 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5525 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5526 break;
5528 case 2:
5529 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5530 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5531 break;
5533 case 3:
5534 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5535 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5536 break;
5538 default:
5539 FIXME("Unexpected mask size %u\n", mask_size);
5540 break;
5542 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5545 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5546 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5547 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5549 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5550 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5551 struct glsl_src_param src0_param;
5552 DWORD dst_mask;
5553 unsigned int mask_size;
5555 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5556 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5557 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5559 if (mask_size > 1) {
5560 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5561 } else {
5562 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5566 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5567 * Calculate the depth as dst.x / dst.y */
5568 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5570 struct glsl_dst_param dst_param;
5572 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5574 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5575 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5576 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5577 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5578 * >= 1.0 or < 0.0
5580 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5581 dst_param.reg_name, dst_param.reg_name);
5584 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5585 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5586 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5587 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5589 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5591 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5592 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5593 struct glsl_src_param src0_param;
5595 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5597 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5598 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5601 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5602 * Calculate the 1st of a 2-row matrix multiplication. */
5603 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5605 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5606 DWORD reg = ins->dst[0].reg.idx[0].offset;
5607 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5608 struct glsl_src_param src0_param;
5610 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5611 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5614 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5615 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5616 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5618 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5619 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5620 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5621 DWORD reg = ins->dst[0].reg.idx[0].offset;
5622 struct glsl_src_param src0_param;
5624 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5625 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5626 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5629 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5631 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5632 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5633 struct glsl_sample_function sample_function;
5634 DWORD reg = ins->dst[0].reg.idx[0].offset;
5635 struct glsl_src_param src0_param;
5637 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5638 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5640 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5642 /* Sample the texture using the calculated coordinates */
5643 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5644 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5647 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5648 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5649 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5651 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5652 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5653 struct glsl_sample_function sample_function;
5654 DWORD reg = ins->dst[0].reg.idx[0].offset;
5655 struct glsl_src_param src0_param;
5657 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5658 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5660 /* Dependent read, not valid with conditional NP2 */
5661 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5663 /* Sample the texture using the calculated coordinates */
5664 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5665 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5667 tex_mx->current_row = 0;
5670 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5671 * Perform the 3rd row of a 3x3 matrix multiply */
5672 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5674 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5675 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5676 DWORD reg = ins->dst[0].reg.idx[0].offset;
5677 struct glsl_src_param src0_param;
5678 char dst_mask[6];
5680 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5682 shader_glsl_append_dst(ins->ctx->buffer, ins);
5683 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5684 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5686 tex_mx->current_row = 0;
5689 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5690 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5691 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5693 struct glsl_src_param src0_param;
5694 struct glsl_src_param src1_param;
5695 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5696 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5697 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5698 struct glsl_sample_function sample_function;
5699 DWORD reg = ins->dst[0].reg.idx[0].offset;
5700 char coord_mask[6];
5702 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5703 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5705 /* Perform the last matrix multiply operation */
5706 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5707 /* Reflection calculation */
5708 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5710 /* Dependent read, not valid with conditional NP2 */
5711 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5712 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5714 /* Sample the texture */
5715 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5716 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5717 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5719 tex_mx->current_row = 0;
5722 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5723 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5724 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5726 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5727 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5728 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5729 struct glsl_sample_function sample_function;
5730 DWORD reg = ins->dst[0].reg.idx[0].offset;
5731 struct glsl_src_param src0_param;
5732 char coord_mask[6];
5734 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5736 /* Perform the last matrix multiply operation */
5737 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5739 /* Construct the eye-ray vector from w coordinates */
5740 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5741 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5742 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5744 /* Dependent read, not valid with conditional NP2 */
5745 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5746 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5748 /* Sample the texture using the calculated coordinates */
5749 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5750 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5751 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5753 tex_mx->current_row = 0;
5756 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5757 * Apply a fake bump map transform.
5758 * texbem is pshader <= 1.3 only, this saves a few version checks
5760 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5762 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5763 struct glsl_sample_function sample_function;
5764 struct glsl_src_param coord_param;
5765 DWORD sampler_idx;
5766 DWORD mask;
5767 DWORD flags;
5768 char coord_mask[6];
5770 sampler_idx = ins->dst[0].reg.idx[0].offset;
5771 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5772 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5774 /* Dependent read, not valid with conditional NP2 */
5775 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5776 mask = sample_function.coord_mask;
5778 shader_glsl_write_mask_to_str(mask, coord_mask);
5780 /* With projected textures, texbem only divides the static texture coord,
5781 * not the displacement, so we can't let GL handle this. */
5782 if (flags & WINED3D_PSARGS_PROJECTED)
5784 DWORD div_mask=0;
5785 char coord_div_mask[3];
5786 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5788 case WINED3D_TTFF_COUNT1:
5789 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5790 break;
5791 case WINED3D_TTFF_COUNT2:
5792 div_mask = WINED3DSP_WRITEMASK_1;
5793 break;
5794 case WINED3D_TTFF_COUNT3:
5795 div_mask = WINED3DSP_WRITEMASK_2;
5796 break;
5797 case WINED3D_TTFF_COUNT4:
5798 case WINED3D_TTFF_DISABLE:
5799 div_mask = WINED3DSP_WRITEMASK_3;
5800 break;
5802 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5803 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5806 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5808 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5809 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5810 coord_param.param_str, coord_mask);
5812 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5814 struct glsl_src_param luminance_param;
5815 struct glsl_dst_param dst_param;
5817 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5818 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5820 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5821 dst_param.reg_name, dst_param.mask_str,
5822 luminance_param.param_str, sampler_idx, sampler_idx);
5824 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5827 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5829 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5830 struct glsl_src_param src0_param, src1_param;
5832 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5833 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5835 shader_glsl_append_dst(ins->ctx->buffer, ins);
5836 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5837 src0_param.param_str, sampler_idx, src1_param.param_str);
5840 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5841 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5842 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5844 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5845 struct glsl_sample_function sample_function;
5846 struct glsl_src_param src0_param;
5848 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5850 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5851 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5852 "%s.wx", src0_param.reg_name);
5853 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5856 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5857 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5858 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5860 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5861 struct glsl_sample_function sample_function;
5862 struct glsl_src_param src0_param;
5864 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5866 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5867 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5868 "%s.yz", src0_param.reg_name);
5869 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5872 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5873 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5874 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5876 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5877 struct glsl_sample_function sample_function;
5878 struct glsl_src_param src0_param;
5880 /* Dependent read, not valid with conditional NP2 */
5881 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5882 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5884 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5885 "%s", src0_param.param_str);
5886 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5889 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5890 * If any of the first 3 components are < 0, discard this pixel */
5891 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5893 if (ins->ctx->reg_maps->shader_version.major >= 4)
5895 struct glsl_src_param src_param;
5897 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5898 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
5900 else
5902 struct glsl_dst_param dst_param;
5904 /* The argument is a destination parameter, and no writemasks are allowed */
5905 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5907 /* 2.0 shaders compare all 4 components in texkill. */
5908 if (ins->ctx->reg_maps->shader_version.major >= 2)
5909 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5910 /* 1.x shaders only compare the first 3 components, probably due to
5911 * the nature of the texkill instruction as a tex* instruction, and
5912 * phase, which kills all .w components. Even if all 4 components are
5913 * defined, only the first 3 are used. */
5914 else
5915 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5919 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5920 * dst = dot2(src0, src1) + src2 */
5921 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5923 struct glsl_src_param src0_param;
5924 struct glsl_src_param src1_param;
5925 struct glsl_src_param src2_param;
5926 DWORD write_mask;
5927 unsigned int mask_size;
5929 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5930 mask_size = shader_glsl_get_write_mask_size(write_mask);
5932 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5933 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5934 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5936 if (mask_size > 1) {
5937 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5938 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5939 } else {
5940 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5941 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5945 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5946 const struct wined3d_shader_signature *input_signature,
5947 const struct wined3d_shader_reg_maps *reg_maps,
5948 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5950 unsigned int i;
5952 for (i = 0; i < input_signature->element_count; ++i)
5954 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5955 const char *semantic_name;
5956 UINT semantic_idx;
5957 char reg_mask[6];
5959 /* Unused */
5960 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5961 continue;
5963 semantic_name = input->semantic_name;
5964 semantic_idx = input->semantic_idx;
5965 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5967 if (args->vp_mode == vertexshader)
5969 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5971 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5972 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5974 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5976 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5978 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5980 shader_addline(buffer, "ps_in[%u] = vec4("
5981 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5982 input->register_idx);
5984 else
5986 if (input->sysval_semantic)
5987 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5988 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5989 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5990 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5993 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5995 if (args->pointsprite)
5996 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5997 shader->u.ps.input_reg_map[input->register_idx]);
5998 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5999 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
6000 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
6001 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
6002 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
6003 else
6004 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6005 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6007 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
6009 if (!semantic_idx)
6010 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
6011 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6012 else if (semantic_idx == 1)
6013 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
6014 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6015 else
6016 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6017 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6019 else
6021 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
6022 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
6027 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
6029 struct glsl_program_key key;
6031 key.vs_id = entry->vs.id;
6032 key.gs_id = entry->gs.id;
6033 key.ps_id = entry->ps.id;
6034 key.cs_id = entry->cs.id;
6036 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
6038 ERR("Failed to insert program entry.\n");
6042 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
6043 const struct glsl_program_key *key)
6045 struct wine_rb_entry *entry;
6047 entry = wine_rb_get(&priv->program_lookup, key);
6048 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
6051 /* Context activation is done by the caller. */
6052 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
6053 struct glsl_shader_prog_link *entry)
6055 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
6057 GL_EXTCALL(glDeleteProgram(entry->id));
6058 if (entry->vs.id)
6059 list_remove(&entry->vs.shader_entry);
6060 if (entry->gs.id)
6061 list_remove(&entry->gs.shader_entry);
6062 if (entry->ps.id)
6063 list_remove(&entry->ps.shader_entry);
6064 if (entry->cs.id)
6065 list_remove(&entry->cs.shader_entry);
6066 HeapFree(GetProcessHeap(), 0, entry);
6069 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
6070 const struct wined3d_gl_info *gl_info, const DWORD *map,
6071 const struct wined3d_shader_signature *input_signature,
6072 const struct wined3d_shader_reg_maps *reg_maps_in,
6073 const struct wined3d_shader_signature *output_signature,
6074 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6076 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6077 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6078 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6079 unsigned int in_count = vec4_varyings(3, gl_info);
6080 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
6081 DWORD in_idx, *set = NULL;
6082 unsigned int i, j;
6083 char reg_mask[6];
6085 set = wined3d_calloc(max_varyings, sizeof(*set));
6087 for (i = 0; i < input_signature->element_count; ++i)
6089 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
6091 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
6092 continue;
6094 in_idx = map[input->register_idx];
6095 /* Declared, but not read register */
6096 if (in_idx == ~0u)
6097 continue;
6098 if (in_idx >= max_varyings)
6100 FIXME("More input varyings declared than supported, expect issues.\n");
6101 continue;
6104 if (in_idx == in_count)
6105 string_buffer_sprintf(destination, "gl_FrontColor");
6106 else if (in_idx == in_count + 1)
6107 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6108 else
6109 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
6111 if (!set[in_idx])
6112 set[in_idx] = ~0u;
6114 for (j = 0; j < output_signature->element_count; ++j)
6116 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
6117 DWORD mask;
6119 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
6120 || input->semantic_idx != output->semantic_idx
6121 || strcmp(input->semantic_name, output->semantic_name)
6122 || !(mask = input->mask & output->mask))
6123 continue;
6125 if (set[in_idx] == ~0u)
6126 set[in_idx] = 0;
6127 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
6128 shader_glsl_write_mask_to_str(mask, reg_mask);
6130 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6131 destination->buffer, reg_mask, output->register_idx, reg_mask);
6135 for (i = 0; i < max_varyings; ++i)
6137 unsigned int size;
6139 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
6140 continue;
6142 if (set[i] == ~0u)
6143 set[i] = 0;
6145 size = 0;
6146 if (!(set[i] & WINED3DSP_WRITEMASK_0))
6147 reg_mask[size++] = 'x';
6148 if (!(set[i] & WINED3DSP_WRITEMASK_1))
6149 reg_mask[size++] = 'y';
6150 if (!(set[i] & WINED3DSP_WRITEMASK_2))
6151 reg_mask[size++] = 'z';
6152 if (!(set[i] & WINED3DSP_WRITEMASK_3))
6153 reg_mask[size++] = 'w';
6154 reg_mask[size] = '\0';
6156 if (i == in_count)
6157 string_buffer_sprintf(destination, "gl_FrontColor");
6158 else if (i == in_count + 1)
6159 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
6160 else
6161 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
6163 if (size == 1)
6164 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
6165 else
6166 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
6169 HeapFree(GetProcessHeap(), 0, set);
6170 string_buffer_release(&priv->string_buffers, destination);
6173 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6174 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6175 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6177 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6178 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6179 char reg_mask[6];
6180 unsigned int i;
6182 for (i = 0; i < output_signature->element_count; ++i)
6184 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6186 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6187 continue;
6189 if (output->register_idx >= input_count)
6190 continue;
6192 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6194 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6196 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6197 destination->buffer, reg_mask, output->register_idx, reg_mask);
6200 string_buffer_release(&priv->string_buffers, destination);
6203 /* Context activation is done by the caller. */
6204 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6205 const struct wined3d_shader *vs, unsigned int input_count,
6206 const struct wined3d_gl_info *gl_info)
6208 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6209 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6211 if (legacy_context)
6212 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6213 else
6214 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6215 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6217 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6218 legacy_context ? "gs_in" : "gs_in.gs_in");
6220 shader_addline(buffer, "}\n");
6223 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6224 const struct wined3d_gl_info *gl_info, const DWORD *map,
6225 const struct wined3d_shader_signature *input_signature,
6226 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6227 const struct wined3d_shader_signature *output_signature,
6228 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6230 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6231 const char *semantic_name;
6232 UINT semantic_idx;
6233 char reg_mask[6];
6234 unsigned int i;
6236 /* First, sort out position and point size system values. */
6237 for (i = 0; i < output_signature->element_count; ++i)
6239 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6241 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6242 continue;
6244 semantic_name = output->semantic_name;
6245 semantic_idx = output->semantic_idx;
6246 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6248 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6250 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6251 reg_mask, output->register_idx, reg_mask);
6253 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6255 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6256 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6258 else if (output->sysval_semantic)
6260 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6264 /* Then, setup the pixel shader input. */
6265 if (reg_maps_out->shader_version.major < 4)
6266 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6267 output_signature, reg_maps_out, "ps_link");
6268 else
6269 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6272 /* Context activation is done by the caller. */
6273 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6274 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6275 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6277 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6278 GLuint ret;
6279 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6280 unsigned int i;
6281 const char *semantic_name;
6282 UINT semantic_idx;
6283 char reg_mask[6];
6284 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6286 string_buffer_clear(buffer);
6288 shader_glsl_add_version_declaration(buffer, gl_info, &vs->reg_maps.shader_version);
6290 if (per_vertex_point_size)
6292 shader_addline(buffer, "uniform struct\n{\n");
6293 shader_addline(buffer, " float size_min;\n");
6294 shader_addline(buffer, " float size_max;\n");
6295 shader_addline(buffer, "} ffp_point;\n");
6298 if (ps_major < 3)
6300 DWORD colors_written_mask[2] = {0};
6301 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6303 if (!legacy_context)
6305 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6306 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6307 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6308 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6311 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6313 for (i = 0; i < vs->output_signature.element_count; ++i)
6315 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6316 DWORD write_mask;
6318 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6319 continue;
6321 semantic_name = output->semantic_name;
6322 semantic_idx = output->semantic_idx;
6323 write_mask = output->mask;
6324 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6326 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6328 if (legacy_context)
6329 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6330 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6331 else
6332 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6333 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6335 colors_written_mask[semantic_idx] = write_mask;
6337 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6339 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6340 reg_mask, output->register_idx, reg_mask);
6342 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6344 if (semantic_idx < MAX_TEXTURES)
6346 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6347 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6348 semantic_idx, reg_mask, output->register_idx, reg_mask);
6349 texcoords_written_mask[semantic_idx] = write_mask;
6352 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6354 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6355 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6357 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6359 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6360 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6361 output->register_idx, reg_mask[1]);
6365 for (i = 0; i < 2; ++i)
6367 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6369 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6370 if (!i)
6371 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6372 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6373 reg_mask, reg_mask);
6374 else
6375 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6376 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6377 reg_mask, reg_mask);
6380 for (i = 0; i < MAX_TEXTURES; ++i)
6382 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6383 continue;
6385 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6387 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6388 && !texcoords_written_mask[i])
6389 continue;
6391 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6392 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6393 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6397 else
6399 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6401 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6402 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6403 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6404 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6407 shader_addline(buffer, "}\n");
6409 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6410 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6411 shader_glsl_compile(gl_info, ret, buffer->buffer);
6413 return ret;
6416 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6417 const struct wined3d_shader *shader, unsigned int input_count,
6418 const struct wined3d_gl_info *gl_info)
6420 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6422 if (input_count)
6423 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6425 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6426 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6428 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6429 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6431 shader_addline(buffer, "}\n");
6434 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6435 const struct wined3d_gl_info *gl_info)
6437 const char *output = get_fragment_output(gl_info);
6439 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6440 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6441 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6442 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6443 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6444 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6447 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6448 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6450 const char *output = get_fragment_output(gl_info);
6452 switch (mode)
6454 case WINED3D_FFP_PS_FOG_OFF:
6455 return;
6457 case WINED3D_FFP_PS_FOG_LINEAR:
6458 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6459 break;
6461 case WINED3D_FFP_PS_FOG_EXP:
6462 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6463 break;
6465 case WINED3D_FFP_PS_FOG_EXP2:
6466 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6467 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6468 break;
6470 default:
6471 ERR("Invalid fog mode %#x.\n", mode);
6472 return;
6475 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6476 output, output);
6479 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6480 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6482 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6483 * flipping all the operators here, just negate the comparison below. */
6484 static const char * const comparison_operator[] =
6486 "", /* WINED3D_CMP_NEVER */
6487 "<", /* WINED3D_CMP_LESS */
6488 "==", /* WINED3D_CMP_EQUAL */
6489 "<=", /* WINED3D_CMP_LESSEQUAL */
6490 ">", /* WINED3D_CMP_GREATER */
6491 "!=", /* WINED3D_CMP_NOTEQUAL */
6492 ">=", /* WINED3D_CMP_GREATEREQUAL */
6493 "" /* WINED3D_CMP_ALWAYS */
6496 if (alpha_func == WINED3D_CMP_ALWAYS)
6497 return;
6499 if (alpha_func != WINED3D_CMP_NEVER)
6500 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6501 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6502 shader_addline(buffer, " discard;\n");
6505 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6506 const struct wined3d_gl_info *gl_info)
6508 if (gl_info->supported[ARB_GPU_SHADER5])
6509 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6510 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6511 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6512 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6513 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6514 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6515 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6516 if (gl_info->supported[ARB_SHADING_LANGUAGE_420PACK])
6517 shader_addline(buffer, "#extension GL_ARB_shading_language_420pack : enable\n");
6518 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6519 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6520 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6521 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6522 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6523 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6524 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6525 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6526 if (gl_info->supported[EXT_GPU_SHADER4])
6527 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6528 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6529 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6532 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6533 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6534 const struct ps_compile_args *args)
6536 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6538 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6539 if (reg_maps->shader_version.major < 2)
6540 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6542 if (args->srgb_correction)
6543 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6545 /* SM < 3 does not replace the fog stage. */
6546 if (reg_maps->shader_version.major < 3)
6547 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6549 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6552 /* Context activation is done by the caller. */
6553 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6554 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6555 const struct wined3d_shader *shader,
6556 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6558 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6559 const struct wined3d_gl_info *gl_info = context->gl_info;
6560 struct shader_glsl_ctx_priv priv_ctx;
6561 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6563 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6564 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6566 memset(&priv_ctx, 0, sizeof(priv_ctx));
6567 priv_ctx.cur_ps_args = args;
6568 priv_ctx.cur_np2fixup_info = np2fixup_info;
6569 priv_ctx.string_buffers = string_buffers;
6571 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6573 shader_glsl_enable_extensions(buffer, gl_info);
6574 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6575 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6576 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6577 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6578 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6579 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6580 /* The spec says that it doesn't have to be explicitly enabled, but the
6581 * nvidia drivers write a warning if we don't do so. */
6582 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6583 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6585 /* Base Declarations */
6586 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6588 shader_addline(buffer, "void main()\n{\n");
6590 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6591 * add approximately 0.5. This causes off-by-one problems as spotted by
6592 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6593 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6594 * causes precision troubles when we just subtract 0.5.
6596 * To deal with that, just floor() the position. This will eliminate the
6597 * fraction on all cards.
6599 * TODO: Test how this behaves with multisampling.
6601 * An advantage of floor is that it works even if the driver doesn't add
6602 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6603 * to return in gl_FragCoord, even though coordinates specify the pixel
6604 * centers instead of the pixel corners. This code will behave correctly
6605 * on drivers that returns integer values. */
6606 if (reg_maps->vpos)
6608 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6609 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6610 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6611 shader_addline(buffer,
6612 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6613 else
6614 shader_addline(buffer,
6615 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6618 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6620 unsigned int i;
6621 WORD map = reg_maps->texcoord;
6623 if (legacy_context)
6625 if (glsl_is_color_reg_read(shader, 0))
6626 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6627 if (glsl_is_color_reg_read(shader, 1))
6628 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6631 for (i = 0; map; map >>= 1, ++i)
6633 if (map & 1)
6635 if (args->pointsprite)
6636 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6637 else if (args->texcoords_initialized & (1u << i))
6638 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6639 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6640 else
6641 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6642 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6646 if (legacy_context)
6647 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6650 /* Pack 3.0 inputs */
6651 if (reg_maps->shader_version.major >= 3)
6652 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6654 /* Base Shader Body */
6655 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6657 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6658 if (reg_maps->shader_version.major < 4)
6659 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6661 shader_addline(buffer, "}\n");
6663 TRACE("Compiling shader object %u.\n", shader_id);
6664 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6666 return shader_id;
6669 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6670 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6671 const struct vs_compile_args *args)
6673 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6674 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6675 unsigned int i;
6677 /* Unpack outputs. */
6678 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6680 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6681 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6682 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6683 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6685 if (reg_maps->shader_version.major < 3)
6687 if (args->fog_src == VS_FOG_Z)
6688 shader_addline(buffer, "%s = gl_Position.z;\n",
6689 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6690 else if (!reg_maps->fog)
6691 shader_addline(buffer, "%s = 0.0;\n",
6692 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6695 /* We always store the clipplanes without y inversion. */
6696 if (args->clip_enabled)
6698 if (legacy_context)
6699 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6700 else
6701 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6702 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6705 if (args->point_size && !args->per_vertex_point_size)
6706 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6708 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6709 shader_glsl_fixup_position(buffer);
6712 /* Context activation is done by the caller. */
6713 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6714 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6716 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6717 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6718 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6719 const struct wined3d_gl_info *gl_info = context->gl_info;
6720 struct shader_glsl_ctx_priv priv_ctx;
6722 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6723 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6725 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6727 shader_glsl_enable_extensions(buffer, gl_info);
6728 if (gl_info->supported[ARB_DRAW_INSTANCED])
6729 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6730 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6731 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6733 memset(&priv_ctx, 0, sizeof(priv_ctx));
6734 priv_ctx.cur_vs_args = args;
6735 priv_ctx.string_buffers = string_buffers;
6737 /* Base Declarations */
6738 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6740 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6741 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6743 if (reg_maps->shader_version.major >= 4)
6745 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6746 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6747 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6748 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6751 shader_addline(buffer, "void main()\n{\n");
6753 /* Base Shader Body */
6754 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6756 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6757 if (reg_maps->shader_version.major < 4)
6758 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6760 shader_addline(buffer, "}\n");
6762 TRACE("Compiling shader object %u.\n", shader_id);
6763 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6765 return shader_id;
6768 /* Context activation is done by the caller. */
6769 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6770 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6772 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6773 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6774 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6775 const struct wined3d_gl_info *gl_info = context->gl_info;
6776 struct shader_glsl_ctx_priv priv_ctx;
6777 GLuint shader_id;
6779 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6781 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6783 shader_glsl_enable_extensions(buffer, gl_info);
6784 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6785 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6787 memset(&priv_ctx, 0, sizeof(priv_ctx));
6788 priv_ctx.string_buffers = string_buffers;
6789 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6790 if (!gl_info->supported[ARB_CLIP_CONTROL])
6791 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6792 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6793 shader_addline(buffer, "void main()\n{\n");
6794 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6795 shader_addline(buffer, "}\n");
6797 TRACE("Compiling shader object %u.\n", shader_id);
6798 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6800 return shader_id;
6803 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6805 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6806 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6807 const struct wined3d_shader *shader = ctx->shader;
6809 switch (shader->reg_maps.shader_version.type)
6811 case WINED3D_SHADER_TYPE_PIXEL:
6812 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6813 break;
6814 case WINED3D_SHADER_TYPE_VERTEX:
6815 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
6816 break;
6817 case WINED3D_SHADER_TYPE_GEOMETRY:
6818 case WINED3D_SHADER_TYPE_COMPUTE:
6819 break;
6820 default:
6821 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6822 break;
6826 /* Context activation is done by the caller. */
6827 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
6828 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6829 const struct wined3d_shader *shader)
6831 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
6832 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6833 const struct wined3d_gl_info *gl_info = context->gl_info;
6834 struct shader_glsl_ctx_priv priv_ctx;
6835 GLuint shader_id;
6837 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
6839 shader_glsl_add_version_declaration(buffer, gl_info, &reg_maps->shader_version);
6841 shader_glsl_enable_extensions(buffer, gl_info);
6842 if (gl_info->supported[ARB_COMPUTE_SHADER])
6843 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
6845 memset(&priv_ctx, 0, sizeof(priv_ctx));
6846 priv_ctx.string_buffers = string_buffers;
6847 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6849 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
6850 thread_group_size->x, thread_group_size->y, thread_group_size->z);
6852 shader_addline(buffer, "void main()\n{\n");
6853 shader_generate_main(shader, buffer, reg_maps, &priv_ctx);
6854 shader_addline(buffer, "}\n");
6856 TRACE("Compiling shader object %u.\n", shader_id);
6857 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6859 return shader_id;
6862 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6863 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6864 struct wined3d_shader *shader,
6865 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6867 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6868 struct glsl_shader_private *shader_data;
6869 struct ps_np2fixup_info *np2fixup;
6870 UINT i;
6871 DWORD new_size;
6872 GLuint ret;
6874 if (!shader->backend_data)
6876 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6877 if (!shader->backend_data)
6879 ERR("Failed to allocate backend data.\n");
6880 return 0;
6883 shader_data = shader->backend_data;
6884 gl_shaders = shader_data->gl_shaders.ps;
6886 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6887 * so a linear search is more performant than a hashmap or a binary search
6888 * (cache coherency etc)
6890 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6892 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6894 if (args->np2_fixup)
6895 *np2fixup_info = &gl_shaders[i].np2fixup;
6896 return gl_shaders[i].id;
6900 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6901 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6902 if (shader_data->num_gl_shaders)
6904 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6905 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6906 new_size * sizeof(*gl_shaders));
6908 else
6910 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6911 new_size = 1;
6914 if(!new_array) {
6915 ERR("Out of memory\n");
6916 return 0;
6918 shader_data->gl_shaders.ps = new_array;
6919 shader_data->shader_array_size = new_size;
6920 gl_shaders = new_array;
6923 gl_shaders[shader_data->num_gl_shaders].args = *args;
6925 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6926 memset(np2fixup, 0, sizeof(*np2fixup));
6927 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6929 pixelshader_update_resource_types(shader, args->tex_types);
6931 string_buffer_clear(buffer);
6932 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6933 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6935 return ret;
6938 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6939 const DWORD use_map)
6941 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6942 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6943 if (stored->point_size != new->point_size)
6944 return FALSE;
6945 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6946 return FALSE;
6947 if (stored->flatshading != new->flatshading)
6948 return FALSE;
6949 if (stored->next_shader_type != new->next_shader_type)
6950 return FALSE;
6951 if (stored->next_shader_input_count != new->next_shader_input_count)
6952 return FALSE;
6953 return stored->fog_src == new->fog_src;
6956 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6957 struct wined3d_shader *shader, const struct vs_compile_args *args)
6959 UINT i;
6960 DWORD new_size;
6961 DWORD use_map = context->stream_info.use_map;
6962 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6963 struct glsl_shader_private *shader_data;
6964 GLuint ret;
6966 if (!shader->backend_data)
6968 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6969 if (!shader->backend_data)
6971 ERR("Failed to allocate backend data.\n");
6972 return 0;
6975 shader_data = shader->backend_data;
6976 gl_shaders = shader_data->gl_shaders.vs;
6978 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6979 * so a linear search is more performant than a hashmap or a binary search
6980 * (cache coherency etc)
6982 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6984 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6985 return gl_shaders[i].id;
6988 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6990 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6991 if (shader_data->num_gl_shaders)
6993 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6994 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6995 new_size * sizeof(*gl_shaders));
6997 else
6999 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
7000 new_size = 1;
7003 if(!new_array) {
7004 ERR("Out of memory\n");
7005 return 0;
7007 shader_data->gl_shaders.vs = new_array;
7008 shader_data->shader_array_size = new_size;
7009 gl_shaders = new_array;
7012 gl_shaders[shader_data->num_gl_shaders].args = *args;
7014 string_buffer_clear(&priv->shader_buffer);
7015 ret = shader_glsl_generate_vshader(context, priv, shader, args);
7016 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7018 return ret;
7021 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
7022 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
7024 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
7025 struct glsl_shader_private *shader_data;
7026 unsigned int i, new_size;
7027 GLuint ret;
7029 if (!shader->backend_data)
7031 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7033 ERR("Failed to allocate backend data.\n");
7034 return 0;
7037 shader_data = shader->backend_data;
7038 gl_shaders = shader_data->gl_shaders.gs;
7040 for (i = 0; i < shader_data->num_gl_shaders; ++i)
7042 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
7043 return gl_shaders[i].id;
7046 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7048 if (shader_data->num_gl_shaders)
7050 new_size = shader_data->shader_array_size + 1;
7051 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
7052 new_size * sizeof(*new_array));
7054 else
7056 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
7057 new_size = 1;
7060 if (!new_array)
7062 ERR("Failed to allocate GL shaders array.\n");
7063 return 0;
7065 shader_data->gl_shaders.gs = new_array;
7066 shader_data->shader_array_size = new_size;
7067 gl_shaders = new_array;
7069 string_buffer_clear(&priv->shader_buffer);
7070 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
7071 gl_shaders[shader_data->num_gl_shaders].args = *args;
7072 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7074 return ret;
7077 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
7078 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
7079 struct wined3d_shader *shader)
7081 struct glsl_cs_compiled_shader *gl_shaders;
7082 struct glsl_shader_private *shader_data;
7083 GLuint ret;
7085 if (!shader->backend_data)
7087 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
7089 ERR("Failed to allocate backend data.\n");
7090 return 0;
7093 shader_data = shader->backend_data;
7094 gl_shaders = shader_data->gl_shaders.cs;
7096 /* No shader variants are used for compute shaders. */
7097 if (shader_data->num_gl_shaders)
7098 return gl_shaders[0].id;
7100 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
7102 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
7104 ERR("Failed to allocate GL shader array.\n");
7105 return 0;
7107 shader_data->shader_array_size = 1;
7108 gl_shaders = shader_data->gl_shaders.cs;
7110 string_buffer_clear(buffer);
7111 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
7112 gl_shaders[shader_data->num_gl_shaders++].id = ret;
7114 return ret;
7117 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
7119 switch (mcs)
7121 case WINED3D_MCS_MATERIAL:
7122 return material;
7123 case WINED3D_MCS_COLOR1:
7124 return "ffp_attrib_diffuse";
7125 case WINED3D_MCS_COLOR2:
7126 return "ffp_attrib_specular";
7127 default:
7128 ERR("Invalid material color source %#x.\n", mcs);
7129 return "<invalid>";
7133 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
7134 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
7136 const char *diffuse, *specular, *emissive, *ambient;
7137 unsigned int i, idx;
7139 if (!settings->lighting)
7141 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
7142 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
7143 return;
7146 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
7147 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
7148 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
7149 shader_addline(buffer, "vec3 dir, dst;\n");
7150 shader_addline(buffer, "float att, t;\n");
7152 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
7153 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
7154 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
7155 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
7157 idx = 0;
7158 for (i = 0; i < settings->point_light_count; ++i, ++idx)
7160 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7161 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7162 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7163 shader_addline(buffer, "dst.x = 1.0;\n");
7164 if (legacy_lighting)
7166 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7167 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7169 else
7171 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7173 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7174 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7175 if (!legacy_lighting)
7176 shader_addline(buffer, "att = 1.0 / att;\n");
7177 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7178 if (!settings->normal)
7180 if (!legacy_lighting)
7181 shader_addline(buffer, "}\n");
7182 continue;
7184 shader_addline(buffer, "dir = normalize(dir);\n");
7185 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7186 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7187 if (settings->localviewer)
7188 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7189 else
7190 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7191 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7192 " * ffp_light[%u].specular) * att;\n", idx);
7193 if (!legacy_lighting)
7194 shader_addline(buffer, "}\n");
7197 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7199 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7200 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7201 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7202 shader_addline(buffer, "dst.x = 1.0;\n");
7203 if (legacy_lighting)
7205 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7206 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7208 else
7210 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7212 shader_addline(buffer, "dir = normalize(dir);\n");
7213 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7214 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7215 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7216 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7217 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7218 idx, idx, idx, idx);
7219 if (legacy_lighting)
7220 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7221 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7222 idx, idx, idx);
7223 else
7224 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7225 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7226 idx, idx, idx);
7227 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7228 if (!settings->normal)
7230 if (!legacy_lighting)
7231 shader_addline(buffer, "}\n");
7232 continue;
7234 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7235 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7236 if (settings->localviewer)
7237 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7238 else
7239 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7240 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7241 " * ffp_light[%u].specular) * att;\n", idx);
7242 if (!legacy_lighting)
7243 shader_addline(buffer, "}\n");
7246 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7248 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7249 if (!settings->normal)
7250 continue;
7251 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7252 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7253 " * ffp_light[%u].diffuse.xyz;\n", idx);
7254 /* TODO: In the non-local viewer case the halfvector is constant
7255 * and could be precomputed and stored in a uniform. */
7256 if (settings->localviewer)
7257 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7258 else
7259 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7260 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7261 " * ffp_light[%u].specular;\n", idx);
7264 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7266 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7267 if (!settings->normal)
7268 continue;
7269 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7270 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7271 " * ffp_light[%u].diffuse.xyz;\n", idx);
7272 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7273 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7274 " * ffp_light[%u].specular;\n", idx);
7277 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7278 ambient, diffuse, emissive);
7279 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7280 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7283 /* Context activation is done by the caller. */
7284 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7285 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7287 static const struct attrib_info
7289 const char type[6];
7290 const char name[24];
7292 attrib_info[] =
7294 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7295 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7296 /* TODO: Indexed vertex blending */
7297 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7298 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7299 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7300 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7301 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7303 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7304 BOOL legacy_lighting = priv->legacy_lighting;
7305 GLuint shader_obj;
7306 unsigned int i;
7307 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7308 BOOL output_legacy_fogcoord = legacy_context;
7310 string_buffer_clear(buffer);
7312 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7314 if (shader_glsl_use_explicit_attrib_location(gl_info))
7315 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7317 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7319 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7321 if (shader_glsl_use_explicit_attrib_location(gl_info))
7322 shader_addline(buffer, "layout(location = %u) ", i);
7323 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7325 shader_addline(buffer, "\n");
7327 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7328 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7329 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7330 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7332 shader_addline(buffer, "uniform struct\n{\n");
7333 shader_addline(buffer, " vec4 emissive;\n");
7334 shader_addline(buffer, " vec4 ambient;\n");
7335 shader_addline(buffer, " vec4 diffuse;\n");
7336 shader_addline(buffer, " vec4 specular;\n");
7337 shader_addline(buffer, " float shininess;\n");
7338 shader_addline(buffer, "} ffp_material;\n");
7340 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7341 shader_addline(buffer, "uniform struct\n{\n");
7342 shader_addline(buffer, " vec4 diffuse;\n");
7343 shader_addline(buffer, " vec4 specular;\n");
7344 shader_addline(buffer, " vec4 ambient;\n");
7345 shader_addline(buffer, " vec4 position;\n");
7346 shader_addline(buffer, " vec3 direction;\n");
7347 shader_addline(buffer, " float range;\n");
7348 shader_addline(buffer, " float falloff;\n");
7349 shader_addline(buffer, " float c_att;\n");
7350 shader_addline(buffer, " float l_att;\n");
7351 shader_addline(buffer, " float q_att;\n");
7352 shader_addline(buffer, " float cos_htheta;\n");
7353 shader_addline(buffer, " float cos_hphi;\n");
7354 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7356 if (settings->point_size)
7358 shader_addline(buffer, "uniform struct\n{\n");
7359 shader_addline(buffer, " float size;\n");
7360 shader_addline(buffer, " float size_min;\n");
7361 shader_addline(buffer, " float size_max;\n");
7362 shader_addline(buffer, " float c_att;\n");
7363 shader_addline(buffer, " float l_att;\n");
7364 shader_addline(buffer, " float q_att;\n");
7365 shader_addline(buffer, "} ffp_point;\n");
7368 if (legacy_context)
7370 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7371 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7372 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7373 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7375 else
7377 if (settings->clipping)
7378 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7380 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7381 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7382 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7383 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7386 shader_addline(buffer, "\nvoid main()\n{\n");
7387 shader_addline(buffer, "float m;\n");
7388 shader_addline(buffer, "vec3 r;\n");
7390 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7392 if (attrib_info[i].name[0])
7393 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7394 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7396 for (i = 0; i < MAX_TEXTURES; ++i)
7398 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7399 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7400 && settings->texcoords & (1u << i))
7401 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7404 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7406 if (settings->transformed)
7408 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7409 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7410 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7412 else
7414 for (i = 0; i < settings->vertexblends; ++i)
7415 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7417 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7418 for (i = 0; i < settings->vertexblends + 1; ++i)
7419 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7421 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7422 if (settings->clipping)
7424 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7425 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7426 else
7427 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7428 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7430 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7433 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7434 if (settings->normal)
7436 if (!settings->vertexblends)
7438 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7440 else
7442 for (i = 0; i < settings->vertexblends + 1; ++i)
7443 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7446 if (settings->normalize)
7447 shader_addline(buffer, "normal = normalize(normal);\n");
7450 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7451 if (legacy_context)
7453 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7454 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7456 else
7458 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7459 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7462 for (i = 0; i < MAX_TEXTURES; ++i)
7464 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7466 switch (settings->texgen[i] & 0xffff0000)
7468 case WINED3DTSS_TCI_PASSTHRU:
7469 if (settings->texcoords & (1u << i))
7470 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7471 i, i, i);
7472 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7473 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7474 else
7475 output_legacy_texcoord = FALSE;
7476 break;
7478 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7479 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7480 break;
7482 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7483 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7484 break;
7486 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7487 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7488 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7489 break;
7491 case WINED3DTSS_TCI_SPHEREMAP:
7492 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7493 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7494 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7495 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7496 break;
7498 default:
7499 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7500 break;
7502 if (output_legacy_texcoord)
7503 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7506 switch (settings->fog_mode)
7508 case WINED3D_FFP_VS_FOG_OFF:
7509 output_legacy_fogcoord = FALSE;
7510 break;
7512 case WINED3D_FFP_VS_FOG_FOGCOORD:
7513 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7514 break;
7516 case WINED3D_FFP_VS_FOG_RANGE:
7517 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7518 break;
7520 case WINED3D_FFP_VS_FOG_DEPTH:
7521 if (settings->ortho_fog)
7523 if (gl_info->supported[ARB_CLIP_CONTROL])
7524 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7525 else
7526 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7527 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7529 else if (settings->transformed)
7531 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7533 else
7535 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7537 break;
7539 default:
7540 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7541 break;
7543 if (output_legacy_fogcoord)
7544 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7546 if (settings->point_size)
7548 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7549 " + ffp_point.l_att * length(ec_pos.xyz)"
7550 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7551 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7552 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7555 shader_addline(buffer, "}\n");
7557 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7558 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7560 return shader_obj;
7563 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7564 DWORD argnum, unsigned int stage, DWORD arg)
7566 const char *ret;
7568 if (arg == ARG_UNUSED)
7569 return "<unused arg>";
7571 switch (arg & WINED3DTA_SELECTMASK)
7573 case WINED3DTA_DIFFUSE:
7574 ret = "ffp_varying_diffuse";
7575 break;
7577 case WINED3DTA_CURRENT:
7578 ret = "ret";
7579 break;
7581 case WINED3DTA_TEXTURE:
7582 switch (stage)
7584 case 0: ret = "tex0"; break;
7585 case 1: ret = "tex1"; break;
7586 case 2: ret = "tex2"; break;
7587 case 3: ret = "tex3"; break;
7588 case 4: ret = "tex4"; break;
7589 case 5: ret = "tex5"; break;
7590 case 6: ret = "tex6"; break;
7591 case 7: ret = "tex7"; break;
7592 default:
7593 ret = "<invalid texture>";
7594 break;
7596 break;
7598 case WINED3DTA_TFACTOR:
7599 ret = "tex_factor";
7600 break;
7602 case WINED3DTA_SPECULAR:
7603 ret = "ffp_varying_specular";
7604 break;
7606 case WINED3DTA_TEMP:
7607 ret = "temp_reg";
7608 break;
7610 case WINED3DTA_CONSTANT:
7611 switch (stage)
7613 case 0: ret = "tss_const0"; break;
7614 case 1: ret = "tss_const1"; break;
7615 case 2: ret = "tss_const2"; break;
7616 case 3: ret = "tss_const3"; break;
7617 case 4: ret = "tss_const4"; break;
7618 case 5: ret = "tss_const5"; break;
7619 case 6: ret = "tss_const6"; break;
7620 case 7: ret = "tss_const7"; break;
7621 default:
7622 ret = "<invalid constant>";
7623 break;
7625 break;
7627 default:
7628 return "<unhandled arg>";
7631 if (arg & WINED3DTA_COMPLEMENT)
7633 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7634 if (argnum == 0)
7635 ret = "arg0";
7636 else if (argnum == 1)
7637 ret = "arg1";
7638 else if (argnum == 2)
7639 ret = "arg2";
7642 if (arg & WINED3DTA_ALPHAREPLICATE)
7644 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7645 if (argnum == 0)
7646 ret = "arg0";
7647 else if (argnum == 1)
7648 ret = "arg1";
7649 else if (argnum == 2)
7650 ret = "arg2";
7653 return ret;
7656 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7657 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7659 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7661 if (color && alpha)
7662 dstmask = "";
7663 else if (color)
7664 dstmask = ".xyz";
7665 else
7666 dstmask = ".w";
7668 if (dst == tempreg)
7669 dstreg = "temp_reg";
7670 else
7671 dstreg = "ret";
7673 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7674 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7675 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7677 switch (op)
7679 case WINED3D_TOP_DISABLE:
7680 break;
7682 case WINED3D_TOP_SELECT_ARG1:
7683 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7684 break;
7686 case WINED3D_TOP_SELECT_ARG2:
7687 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7688 break;
7690 case WINED3D_TOP_MODULATE:
7691 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7692 break;
7694 case WINED3D_TOP_MODULATE_4X:
7695 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7696 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7697 break;
7699 case WINED3D_TOP_MODULATE_2X:
7700 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7701 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7702 break;
7704 case WINED3D_TOP_ADD:
7705 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7706 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7707 break;
7709 case WINED3D_TOP_ADD_SIGNED:
7710 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7711 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7712 break;
7714 case WINED3D_TOP_ADD_SIGNED_2X:
7715 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7716 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7717 break;
7719 case WINED3D_TOP_SUBTRACT:
7720 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7721 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7722 break;
7724 case WINED3D_TOP_ADD_SMOOTH:
7725 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7726 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7727 break;
7729 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7730 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7731 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7732 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7733 break;
7735 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7736 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7737 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7738 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7739 break;
7741 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7742 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7743 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7744 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7745 break;
7747 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7748 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7749 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7750 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7751 break;
7753 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7754 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7755 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7756 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7757 break;
7759 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7760 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7761 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7762 break;
7764 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7765 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7766 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7767 break;
7769 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7770 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7771 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7772 break;
7773 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7774 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7775 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7776 break;
7778 case WINED3D_TOP_BUMPENVMAP:
7779 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7780 /* These are handled in the first pass, nothing to do. */
7781 break;
7783 case WINED3D_TOP_DOTPRODUCT3:
7784 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7785 dstreg, dstmask, arg1, arg2, dstmask);
7786 break;
7788 case WINED3D_TOP_MULTIPLY_ADD:
7789 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7790 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7791 break;
7793 case WINED3D_TOP_LERP:
7794 /* MSDN isn't quite right here. */
7795 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7796 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7797 break;
7799 default:
7800 FIXME("Unhandled operation %#x.\n", op);
7801 break;
7805 /* Context activation is done by the caller. */
7806 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7807 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7809 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7810 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7811 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7812 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7813 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7814 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7815 UINT lowest_disabled_stage;
7816 GLuint shader_id;
7817 DWORD arg0, arg1, arg2;
7818 unsigned int stage;
7820 string_buffer_clear(buffer);
7822 /* Find out which textures are read */
7823 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7825 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7826 break;
7828 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7829 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7830 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7832 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7833 || (stage == 0 && settings->color_key_enabled))
7834 tex_map |= 1u << stage;
7835 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7836 tfactor_used = TRUE;
7837 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7838 tempreg_used = TRUE;
7839 if (settings->op[stage].dst == tempreg)
7840 tempreg_used = TRUE;
7841 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7842 tss_const_map |= 1u << stage;
7844 switch (settings->op[stage].cop)
7846 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7847 lum_map |= 1u << stage;
7848 /* fall through */
7849 case WINED3D_TOP_BUMPENVMAP:
7850 bump_map |= 1u << stage;
7851 /* fall through */
7852 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7853 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7854 tex_map |= 1u << stage;
7855 break;
7857 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7858 tfactor_used = TRUE;
7859 break;
7861 default:
7862 break;
7865 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7866 continue;
7868 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7869 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7870 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7872 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7873 tex_map |= 1u << stage;
7874 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7875 tfactor_used = TRUE;
7876 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7877 tempreg_used = TRUE;
7878 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7879 tss_const_map |= 1u << stage;
7881 lowest_disabled_stage = stage;
7883 shader_glsl_add_version_declaration(buffer, gl_info, NULL);
7885 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7886 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7888 if (!needs_legacy_glsl_syntax(gl_info))
7889 shader_addline(buffer, "out vec4 ps_out[1];\n");
7891 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7892 shader_addline(buffer, "vec4 ret;\n");
7893 if (tempreg_used || settings->sRGB_write)
7894 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7895 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7897 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7899 if (tss_const_map & (1u << stage))
7900 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7902 if (!(tex_map & (1u << stage)))
7903 continue;
7905 switch (settings->op[stage].tex_type)
7907 case WINED3D_GL_RES_TYPE_TEX_1D:
7908 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7909 break;
7910 case WINED3D_GL_RES_TYPE_TEX_2D:
7911 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7912 break;
7913 case WINED3D_GL_RES_TYPE_TEX_3D:
7914 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7915 break;
7916 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7917 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7918 break;
7919 case WINED3D_GL_RES_TYPE_TEX_RECT:
7920 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7921 break;
7922 default:
7923 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7924 break;
7927 shader_addline(buffer, "vec4 tex%u;\n", stage);
7929 if (!(bump_map & (1u << stage)))
7930 continue;
7931 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7933 if (!(lum_map & (1u << stage)))
7934 continue;
7935 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7936 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7938 if (tfactor_used)
7939 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7940 if (settings->color_key_enabled)
7941 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7942 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7944 if (settings->sRGB_write)
7946 shader_addline(buffer, "const vec4 srgb_const0 = ");
7947 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7948 shader_addline(buffer, ";\n");
7949 shader_addline(buffer, "const vec4 srgb_const1 = ");
7950 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7951 shader_addline(buffer, ";\n");
7954 shader_addline(buffer, "uniform struct\n{\n");
7955 shader_addline(buffer, " vec4 color;\n");
7956 shader_addline(buffer, " float density;\n");
7957 shader_addline(buffer, " float end;\n");
7958 shader_addline(buffer, " float scale;\n");
7959 shader_addline(buffer, "} ffp_fog;\n");
7961 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7962 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7964 if (legacy_context)
7966 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7967 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7968 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7969 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7970 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7972 else
7974 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7975 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7976 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7977 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7978 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7981 shader_addline(buffer, "void main()\n{\n");
7983 if (legacy_context)
7985 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7986 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7989 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7991 if (tex_map & (1u << stage))
7993 if (settings->pointsprite)
7994 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7995 else if (settings->texcoords_initialized & (1u << stage))
7996 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7997 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7998 else
7999 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
8003 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
8004 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
8006 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
8007 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
8009 /* Generate texture sampling instructions */
8010 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
8012 const char *texture_function, *coord_mask;
8013 BOOL proj;
8015 if (!(tex_map & (1u << stage)))
8016 continue;
8018 if (settings->op[stage].projected == proj_none)
8020 proj = FALSE;
8022 else if (settings->op[stage].projected == proj_count4
8023 || settings->op[stage].projected == proj_count3)
8025 proj = TRUE;
8027 else
8029 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
8030 proj = TRUE;
8033 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
8034 proj = FALSE;
8036 switch (settings->op[stage].tex_type)
8038 case WINED3D_GL_RES_TYPE_TEX_1D:
8039 if (proj)
8041 texture_function = "texture1DProj";
8042 coord_mask = "xw";
8044 else
8046 texture_function = "texture1D";
8047 coord_mask = "x";
8049 break;
8050 case WINED3D_GL_RES_TYPE_TEX_2D:
8051 if (proj)
8053 texture_function = "texture2DProj";
8054 coord_mask = "xyw";
8056 else
8058 texture_function = "texture2D";
8059 coord_mask = "xy";
8061 break;
8062 case WINED3D_GL_RES_TYPE_TEX_3D:
8063 if (proj)
8065 texture_function = "texture3DProj";
8066 coord_mask = "xyzw";
8068 else
8070 texture_function = "texture3D";
8071 coord_mask = "xyz";
8073 break;
8074 case WINED3D_GL_RES_TYPE_TEX_CUBE:
8075 texture_function = "textureCube";
8076 coord_mask = "xyz";
8077 break;
8078 case WINED3D_GL_RES_TYPE_TEX_RECT:
8079 if (proj)
8081 texture_function = "texture2DRectProj";
8082 coord_mask = "xyw";
8084 else
8086 texture_function = "texture2DRect";
8087 coord_mask = "xy";
8089 break;
8090 default:
8091 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
8092 texture_function = "";
8093 coord_mask = "xyzw";
8094 break;
8096 if (!needs_legacy_glsl_syntax(gl_info))
8097 texture_function = proj ? "textureProj" : "texture";
8099 if (stage > 0
8100 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
8101 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
8103 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
8105 /* With projective textures, texbem only divides the static
8106 * texture coord, not the displacement, so multiply the
8107 * displacement with the dividing parameter before passing it to
8108 * TXP. */
8109 if (settings->op[stage].projected != proj_none)
8111 if (settings->op[stage].projected == proj_count4)
8113 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
8114 stage, stage);
8115 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
8117 else
8119 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
8120 stage, stage);
8121 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
8124 else
8126 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
8129 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
8130 stage, texture_function, stage, coord_mask);
8132 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8133 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
8134 stage, stage - 1, stage - 1, stage - 1);
8136 else if (settings->op[stage].projected == proj_count3)
8138 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
8139 stage, texture_function, stage, stage);
8141 else
8143 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
8144 stage, texture_function, stage, stage, coord_mask);
8147 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
8148 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
8149 settings->op[stage].color_fixup);
8152 if (settings->color_key_enabled)
8154 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
8155 shader_addline(buffer, " discard;\n");
8158 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
8160 /* Generate the main shader */
8161 for (stage = 0; stage < MAX_TEXTURES; ++stage)
8163 BOOL op_equal;
8165 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
8166 break;
8168 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8169 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8170 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8171 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8172 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8173 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8174 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8175 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8176 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8177 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8178 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8179 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8180 else
8181 op_equal = settings->op[stage].aop == settings->op[stage].cop
8182 && settings->op[stage].carg0 == settings->op[stage].aarg0
8183 && settings->op[stage].carg1 == settings->op[stage].aarg1
8184 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8186 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8188 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8189 settings->op[stage].cop, settings->op[stage].carg0,
8190 settings->op[stage].carg1, settings->op[stage].carg2);
8192 else if (op_equal)
8194 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8195 settings->op[stage].cop, settings->op[stage].carg0,
8196 settings->op[stage].carg1, settings->op[stage].carg2);
8198 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8199 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8201 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8202 settings->op[stage].cop, settings->op[stage].carg0,
8203 settings->op[stage].carg1, settings->op[stage].carg2);
8204 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8205 settings->op[stage].aop, settings->op[stage].aarg0,
8206 settings->op[stage].aarg1, settings->op[stage].aarg2);
8210 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8211 get_fragment_output(gl_info));
8213 if (settings->sRGB_write)
8214 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8216 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8218 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8220 shader_addline(buffer, "}\n");
8222 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8223 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8225 string_buffer_release(&priv->string_buffers, tex_reg_name);
8226 return shader_id;
8229 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8230 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8232 struct glsl_ffp_vertex_shader *shader;
8233 const struct wine_rb_entry *entry;
8235 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8236 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8238 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8239 return NULL;
8241 shader->desc.settings = *settings;
8242 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8243 list_init(&shader->linked_programs);
8244 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8245 ERR("Failed to insert ffp vertex shader.\n");
8247 return shader;
8250 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8251 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8253 struct glsl_ffp_fragment_shader *glsl_desc;
8254 const struct ffp_frag_desc *desc;
8256 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8257 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8259 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8260 return NULL;
8262 glsl_desc->entry.settings = *args;
8263 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8264 list_init(&glsl_desc->linked_programs);
8265 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8267 return glsl_desc;
8271 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8272 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8274 unsigned int i;
8275 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8277 for (i = 0; i < vs_c_count; ++i)
8279 string_buffer_sprintf(name, "vs_c[%u]", i);
8280 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8282 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8284 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8286 string_buffer_sprintf(name, "vs_i[%u]", i);
8287 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8290 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8292 string_buffer_sprintf(name, "vs_b[%u]", i);
8293 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8296 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8298 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8300 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8301 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8303 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8304 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8305 for (i = 0; i < MAX_TEXTURES; ++i)
8307 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8308 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8310 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8311 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8312 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8313 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8314 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8315 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8316 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8318 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8319 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8320 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8321 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8322 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8323 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8324 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8325 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8326 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8327 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8328 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8329 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8330 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8331 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8332 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8333 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8334 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8335 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8336 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8337 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8338 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8339 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8340 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8341 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8343 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8344 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8345 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8346 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8347 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8348 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8349 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8351 string_buffer_release(&priv->string_buffers, name);
8354 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8355 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8357 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8360 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8361 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8363 unsigned int i;
8364 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8366 for (i = 0; i < ps_c_count; ++i)
8368 string_buffer_sprintf(name, "ps_c[%u]", i);
8369 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8371 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8373 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8375 string_buffer_sprintf(name, "ps_i[%u]", i);
8376 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8379 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8381 string_buffer_sprintf(name, "ps_b[%u]", i);
8382 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8385 for (i = 0; i < MAX_TEXTURES; ++i)
8387 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8388 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8389 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8390 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8391 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8392 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8393 string_buffer_sprintf(name, "tss_const%u", i);
8394 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8397 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8398 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8400 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8401 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8402 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8403 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8405 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8407 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8408 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8409 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8411 string_buffer_release(&priv->string_buffers, name);
8414 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
8415 struct shader_glsl_priv *priv, GLuint program_id,
8416 const struct wined3d_shader_reg_maps *reg_maps)
8418 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8419 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
8420 unsigned int i, base, count;
8421 GLuint block_idx;
8423 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
8424 for (i = 0; i < count; ++i)
8426 if (!reg_maps->cb_sizes[i])
8427 continue;
8429 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
8430 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
8431 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8433 checkGLcall("glUniformBlockBinding");
8434 string_buffer_release(&priv->string_buffers, name);
8437 /* Context activation is done by the caller. */
8438 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8439 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8441 const struct wined3d_gl_info *gl_info = context->gl_info;
8442 unsigned int base_sampler_idx, sampler_count;
8443 struct glsl_shader_prog_link *entry = NULL;
8444 struct wined3d_shader *shader;
8445 struct glsl_program_key key;
8446 GLuint program_id, cs_id;
8448 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8449 return;
8451 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8453 WARN("Compute shader is NULL.\n");
8454 ctx_data->glsl_program = NULL;
8455 return;
8458 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8459 memset(&key, 0, sizeof(key));
8460 key.cs_id = cs_id;
8461 if ((entry = get_glsl_program_entry(priv, &key)))
8463 ctx_data->glsl_program = entry;
8464 return;
8467 program_id = GL_EXTCALL(glCreateProgram());
8468 TRACE("Created new GLSL shader program %u.\n", program_id);
8470 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8472 ERR("Out of memory.\n");
8473 return;
8475 entry->id = program_id;
8476 entry->vs.id = 0;
8477 entry->gs.id = 0;
8478 entry->ps.id = 0;
8479 entry->cs.id = cs_id;
8480 entry->constant_version = 0;
8481 entry->ps.np2_fixup_info = NULL;
8482 add_glsl_program_entry(priv, entry);
8484 ctx_data->glsl_program = entry;
8486 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8487 GL_EXTCALL(glAttachShader(program_id, cs_id));
8488 checkGLcall("glAttachShader");
8490 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8492 TRACE("Linking GLSL shader program %u.\n", program_id);
8493 GL_EXTCALL(glLinkProgram(program_id));
8494 shader_glsl_validate_link(gl_info, program_id);
8496 GL_EXTCALL(glUseProgram(program_id));
8497 checkGLcall("glUseProgram");
8498 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &shader->reg_maps);
8499 shader_glsl_load_icb(gl_info, priv, program_id, &shader->reg_maps);
8500 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8501 wined3d_gl_limits_get_texture_unit_range(&gl_info->limits, WINED3D_SHADER_TYPE_COMPUTE,
8502 &base_sampler_idx, &sampler_count);
8503 shader_glsl_load_samplers(gl_info, priv, shader_glsl_get_prefix(WINED3D_SHADER_TYPE_COMPUTE),
8504 base_sampler_idx, sampler_count, NULL, program_id);
8506 entry->constant_update_mask = 0;
8509 /* Context activation is done by the caller. */
8510 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8511 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8513 const struct wined3d_gl_info *gl_info = context->gl_info;
8514 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8515 const struct ps_np2fixup_info *np2fixup_info = NULL;
8516 struct glsl_shader_prog_link *entry = NULL;
8517 struct wined3d_shader *vshader = NULL;
8518 struct wined3d_shader *gshader = NULL;
8519 struct wined3d_shader *pshader = NULL;
8520 GLuint reorder_shader_id = 0;
8521 struct glsl_program_key key;
8522 GLuint program_id;
8523 unsigned int i;
8524 GLuint vs_id = 0;
8525 GLuint gs_id = 0;
8526 GLuint ps_id = 0;
8527 struct list *ps_list, *vs_list;
8528 WORD attribs_map;
8529 struct wined3d_string_buffer *tmp_name;
8531 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8533 vs_id = ctx_data->glsl_program->vs.id;
8534 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8536 if (use_vs(state))
8538 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8539 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8541 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8542 && ctx_data->glsl_program->gs.id)
8544 gs_id = ctx_data->glsl_program->gs.id;
8546 else if (gshader)
8548 struct gs_compile_args args;
8550 find_gs_compile_args(state, gshader, &args);
8551 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8555 else if (use_vs(state))
8557 struct vs_compile_args vs_compile_args;
8559 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8560 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8562 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8563 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8564 vs_list = &vshader->linked_programs;
8566 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8567 && ctx_data->glsl_program->gs.id)
8569 gs_id = ctx_data->glsl_program->gs.id;
8571 else if (gshader)
8573 struct gs_compile_args gs_compile_args;
8575 find_gs_compile_args(state, gshader, &gs_compile_args);
8576 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8579 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8581 struct glsl_ffp_vertex_shader *ffp_shader;
8582 struct wined3d_ffp_vs_settings settings;
8584 wined3d_ffp_get_vs_settings(context, state, &settings);
8585 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8586 vs_id = ffp_shader->id;
8587 vs_list = &ffp_shader->linked_programs;
8590 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8592 ps_id = ctx_data->glsl_program->ps.id;
8593 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8595 if (use_ps(state))
8596 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8598 else if (use_ps(state))
8600 struct ps_compile_args ps_compile_args;
8601 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8602 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8603 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8604 pshader, &ps_compile_args, &np2fixup_info);
8605 ps_list = &pshader->linked_programs;
8607 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8609 struct glsl_ffp_fragment_shader *ffp_shader;
8610 struct ffp_frag_settings settings;
8612 gen_ffp_frag_op(context, state, &settings, FALSE);
8613 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8614 ps_id = ffp_shader->id;
8615 ps_list = &ffp_shader->linked_programs;
8618 key.vs_id = vs_id;
8619 key.gs_id = gs_id;
8620 key.ps_id = ps_id;
8621 key.cs_id = 0;
8622 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8624 ctx_data->glsl_program = entry;
8625 return;
8628 /* If we get to this point, then no matching program exists, so we create one */
8629 program_id = GL_EXTCALL(glCreateProgram());
8630 TRACE("Created new GLSL shader program %u.\n", program_id);
8632 /* Create the entry */
8633 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8634 entry->id = program_id;
8635 entry->vs.id = vs_id;
8636 entry->gs.id = gs_id;
8637 entry->ps.id = ps_id;
8638 entry->cs.id = 0;
8639 entry->constant_version = 0;
8640 entry->ps.np2_fixup_info = np2fixup_info;
8641 /* Add the hash table entry */
8642 add_glsl_program_entry(priv, entry);
8644 /* Set the current program */
8645 ctx_data->glsl_program = entry;
8647 /* Attach GLSL vshader */
8648 if (vs_id)
8650 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8651 GL_EXTCALL(glAttachShader(program_id, vs_id));
8652 checkGLcall("glAttachShader");
8654 list_add_head(vs_list, &entry->vs.shader_entry);
8657 if (vshader)
8659 attribs_map = vshader->reg_maps.input_registers;
8660 if (vshader->reg_maps.shader_version.major < 4)
8662 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8663 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8664 d3d_info->emulated_flatshading
8665 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8666 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8667 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8668 checkGLcall("glAttachShader");
8669 /* Flag the reorder function for deletion, it will be freed
8670 * automatically when the program is destroyed. */
8671 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8674 else
8676 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8679 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8681 /* Bind vertex attributes to a corresponding index number to match
8682 * the same index numbers as ARB_vertex_programs (makes loading
8683 * vertex attributes simpler). With this method, we can use the
8684 * exact same code to load the attributes later for both ARB and
8685 * GLSL shaders.
8687 * We have to do this here because we need to know the Program ID
8688 * in order to make the bindings work, and it has to be done prior
8689 * to linking the GLSL program. */
8690 tmp_name = string_buffer_get(&priv->string_buffers);
8691 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8693 if (!(attribs_map & 1))
8694 continue;
8696 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8697 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8698 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8700 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8701 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8702 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8703 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8706 checkGLcall("glBindAttribLocation");
8707 string_buffer_release(&priv->string_buffers, tmp_name);
8710 if (gshader)
8712 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8713 GL_EXTCALL(glAttachShader(program_id, gs_id));
8714 checkGLcall("glAttachShader");
8716 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8718 TRACE("input type %s, output type %s, vertices out %u.\n",
8719 debug_d3dprimitivetype(gshader->u.gs.input_type),
8720 debug_d3dprimitivetype(gshader->u.gs.output_type),
8721 gshader->u.gs.vertices_out);
8722 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8723 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8724 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8725 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8726 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8727 gshader->u.gs.vertices_out));
8728 checkGLcall("glProgramParameteriARB");
8731 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8734 /* Attach GLSL pshader */
8735 if (ps_id)
8737 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8738 GL_EXTCALL(glAttachShader(program_id, ps_id));
8739 checkGLcall("glAttachShader");
8741 list_add_head(ps_list, &entry->ps.shader_entry);
8744 /* Link the program */
8745 TRACE("Linking GLSL shader program %u.\n", program_id);
8746 GL_EXTCALL(glLinkProgram(program_id));
8747 shader_glsl_validate_link(gl_info, program_id);
8749 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8750 vshader ? vshader->limits->constant_float : 0);
8751 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8752 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8753 pshader ? pshader->limits->constant_float : 0);
8754 checkGLcall("Find glsl program uniform locations");
8756 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8758 if (pshader && pshader->reg_maps.shader_version.major >= 3
8759 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8761 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8762 entry->vs.vertex_color_clamp = GL_FALSE;
8764 else
8766 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8769 else
8771 /* With core profile we never change vertex_color_clamp from
8772 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8773 * glClampColorARB(). */
8774 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8777 /* Set the shader to allow uniform loading on it */
8778 GL_EXTCALL(glUseProgram(program_id));
8779 checkGLcall("glUseProgram");
8781 /* Texture unit mapping is set up to be the same each time the shader
8782 * program is used so we can hardcode the sampler uniform values. */
8783 shader_glsl_load_graphics_samplers(gl_info, priv, context->tex_unit_map, program_id);
8785 entry->constant_update_mask = 0;
8786 if (vshader)
8788 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8789 if (vshader->reg_maps.integer_constants)
8790 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8791 if (vshader->reg_maps.boolean_constants)
8792 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8793 if (entry->vs.pos_fixup_location != -1)
8794 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8796 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8797 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8799 else
8801 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8802 | WINED3D_SHADER_CONST_FFP_PROJ;
8804 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8806 if (entry->vs.modelview_matrix_location[i] != -1)
8808 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8809 break;
8813 for (i = 0; i < MAX_TEXTURES; ++i)
8815 if (entry->vs.texture_matrix_location[i] != -1)
8817 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8818 break;
8821 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
8822 || entry->vs.material_specular_location != -1
8823 || entry->vs.material_emissive_location != -1
8824 || entry->vs.material_shininess_location != -1)
8825 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8826 if (entry->vs.light_ambient_location != -1)
8827 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8829 if (entry->vs.clip_planes_location != -1)
8830 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8831 if (entry->vs.pointsize_min_location != -1)
8832 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8834 if (gshader)
8836 if (entry->gs.pos_fixup_location != -1)
8837 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8838 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8839 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8842 if (ps_id)
8844 if (pshader)
8846 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8847 if (pshader->reg_maps.integer_constants)
8848 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8849 if (pshader->reg_maps.boolean_constants)
8850 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8851 if (entry->ps.ycorrection_location != -1)
8852 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8854 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8855 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8856 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
8858 else
8860 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8863 for (i = 0; i < MAX_TEXTURES; ++i)
8865 if (entry->ps.bumpenv_mat_location[i] != -1)
8867 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8868 break;
8872 if (entry->ps.fog_color_location != -1)
8873 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8874 if (entry->ps.alpha_test_ref_location != -1)
8875 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8876 if (entry->ps.np2_fixup_location != -1)
8877 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8878 if (entry->ps.color_key_location != -1)
8879 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8883 /* Context activation is done by the caller. */
8884 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8885 const struct wined3d_state *state)
8887 struct glsl_context_data *ctx_data = context->shader_backend_data;
8888 const struct wined3d_gl_info *gl_info = context->gl_info;
8889 struct shader_glsl_priv *priv = shader_priv;
8890 GLenum current_vertex_color_clamp;
8891 GLuint program_id, prev_id;
8893 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8894 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8896 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8898 set_glsl_shader_program(context, state, priv, ctx_data);
8900 if (ctx_data->glsl_program)
8902 program_id = ctx_data->glsl_program->id;
8903 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8905 else
8907 program_id = 0;
8908 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8911 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
8913 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
8914 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8916 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8917 checkGLcall("glClampColorARB");
8919 else
8921 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
8925 TRACE("Using GLSL program %u.\n", program_id);
8927 if (prev_id != program_id)
8929 GL_EXTCALL(glUseProgram(program_id));
8930 checkGLcall("glUseProgram");
8932 if (program_id)
8933 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8936 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
8939 /* Context activation is done by the caller. */
8940 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
8941 const struct wined3d_state *state)
8943 struct glsl_context_data *ctx_data = context->shader_backend_data;
8944 const struct wined3d_gl_info *gl_info = context->gl_info;
8945 struct shader_glsl_priv *priv = shader_priv;
8946 GLuint program_id, prev_id;
8948 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8949 set_glsl_compute_shader_program(context, state, priv, ctx_data);
8950 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8952 TRACE("Using GLSL program %u.\n", program_id);
8954 if (prev_id != program_id)
8956 GL_EXTCALL(glUseProgram(program_id));
8957 checkGLcall("glUseProgram");
8960 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
8961 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8962 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8963 | (1u << WINED3D_SHADER_TYPE_HULL)
8964 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
8967 /* "context" is not necessarily the currently active context. */
8968 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8970 struct glsl_context_data *ctx_data = context->shader_backend_data;
8972 ctx_data->glsl_program = NULL;
8973 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8974 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8975 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8976 | (1u << WINED3D_SHADER_TYPE_HULL)
8977 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8978 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8981 /* Context activation is done by the caller. */
8982 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8984 struct glsl_context_data *ctx_data = context->shader_backend_data;
8985 const struct wined3d_gl_info *gl_info = context->gl_info;
8986 struct shader_glsl_priv *priv = shader_priv;
8988 shader_glsl_invalidate_current_program(context);
8989 GL_EXTCALL(glUseProgram(0));
8990 checkGLcall("glUseProgram");
8992 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8993 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8995 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8997 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
8998 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8999 checkGLcall("glClampColorARB");
9003 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
9004 const struct glsl_shader_prog_link *program)
9006 const struct glsl_context_data *ctx_data;
9007 struct wined3d_context *context;
9008 unsigned int i;
9010 for (i = 0; i < device->context_count; ++i)
9012 context = device->contexts[i];
9013 ctx_data = context->shader_backend_data;
9015 if (ctx_data->glsl_program == program)
9016 shader_glsl_invalidate_current_program(context);
9020 static void shader_glsl_destroy(struct wined3d_shader *shader)
9022 struct glsl_shader_private *shader_data = shader->backend_data;
9023 struct wined3d_device *device = shader->device;
9024 struct shader_glsl_priv *priv = device->shader_priv;
9025 const struct wined3d_gl_info *gl_info;
9026 const struct list *linked_programs;
9027 struct wined3d_context *context;
9029 if (!shader_data || !shader_data->num_gl_shaders)
9031 HeapFree(GetProcessHeap(), 0, shader_data);
9032 shader->backend_data = NULL;
9033 return;
9036 context = context_acquire(device, NULL, 0);
9037 gl_info = context->gl_info;
9039 TRACE("Deleting linked programs.\n");
9040 linked_programs = &shader->linked_programs;
9041 if (linked_programs->next)
9043 struct glsl_shader_prog_link *entry, *entry2;
9044 UINT i;
9046 switch (shader->reg_maps.shader_version.type)
9048 case WINED3D_SHADER_TYPE_PIXEL:
9050 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
9052 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9054 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
9055 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9056 checkGLcall("glDeleteShader");
9058 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
9060 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9061 struct glsl_shader_prog_link, ps.shader_entry)
9063 shader_glsl_invalidate_contexts_program(device, entry);
9064 delete_glsl_program_entry(priv, gl_info, entry);
9067 break;
9070 case WINED3D_SHADER_TYPE_VERTEX:
9072 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
9074 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9076 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
9077 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9078 checkGLcall("glDeleteShader");
9080 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
9082 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9083 struct glsl_shader_prog_link, vs.shader_entry)
9085 shader_glsl_invalidate_contexts_program(device, entry);
9086 delete_glsl_program_entry(priv, gl_info, entry);
9089 break;
9092 case WINED3D_SHADER_TYPE_GEOMETRY:
9094 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
9096 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9098 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
9099 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9100 checkGLcall("glDeleteShader");
9102 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
9104 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9105 struct glsl_shader_prog_link, gs.shader_entry)
9107 shader_glsl_invalidate_contexts_program(device, entry);
9108 delete_glsl_program_entry(priv, gl_info, entry);
9111 break;
9114 case WINED3D_SHADER_TYPE_COMPUTE:
9116 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
9118 for (i = 0; i < shader_data->num_gl_shaders; ++i)
9120 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
9121 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
9122 checkGLcall("glDeleteShader");
9124 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
9126 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
9127 struct glsl_shader_prog_link, cs.shader_entry)
9129 shader_glsl_invalidate_contexts_program(device, entry);
9130 delete_glsl_program_entry(priv, gl_info, entry);
9133 break;
9136 default:
9137 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
9138 break;
9142 HeapFree(GetProcessHeap(), 0, shader->backend_data);
9143 shader->backend_data = NULL;
9145 context_release(context);
9148 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
9150 const struct glsl_program_key *k = key;
9151 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
9152 const struct glsl_shader_prog_link, program_lookup_entry);
9154 if (k->vs_id > prog->vs.id) return 1;
9155 else if (k->vs_id < prog->vs.id) return -1;
9157 if (k->gs_id > prog->gs.id) return 1;
9158 else if (k->gs_id < prog->gs.id) return -1;
9160 if (k->ps_id > prog->ps.id) return 1;
9161 else if (k->ps_id < prog->ps.id) return -1;
9163 if (k->cs_id > prog->cs.id) return 1;
9164 else if (k->cs_id < prog->cs.id) return -1;
9166 return 0;
9169 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
9171 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
9172 + constant_count * sizeof(*heap->contained)
9173 + constant_count * sizeof(*heap->positions);
9174 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9176 if (!mem)
9178 ERR("Failed to allocate memory\n");
9179 return FALSE;
9182 heap->entries = mem;
9183 heap->entries[1].version = 0;
9184 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9185 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9186 heap->positions = (unsigned int *)(heap->contained + constant_count);
9187 heap->size = 1;
9189 return TRUE;
9192 static void constant_heap_free(struct constant_heap *heap)
9194 HeapFree(GetProcessHeap(), 0, heap->entries);
9197 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9198 const struct fragment_pipeline *fragment_pipe)
9200 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9201 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9202 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9203 struct fragment_caps fragment_caps;
9204 void *vertex_priv, *fragment_priv;
9206 string_buffer_list_init(&priv->string_buffers);
9208 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9210 ERR("Failed to initialize vertex pipe.\n");
9211 HeapFree(GetProcessHeap(), 0, priv);
9212 return E_FAIL;
9215 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9217 ERR("Failed to initialize fragment pipe.\n");
9218 vertex_pipe->vp_free(device);
9219 HeapFree(GetProcessHeap(), 0, priv);
9220 return E_FAIL;
9223 if (!string_buffer_init(&priv->shader_buffer))
9225 ERR("Failed to initialize shader buffer.\n");
9226 goto fail;
9229 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9231 ERR("Failed to allocate memory.\n");
9232 goto fail;
9235 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9237 ERR("Failed to initialize vertex shader constant heap\n");
9238 goto fail;
9241 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9243 ERR("Failed to initialize pixel shader constant heap\n");
9244 goto fail;
9247 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9249 priv->next_constant_version = 1;
9250 priv->vertex_pipe = vertex_pipe;
9251 priv->fragment_pipe = fragment_pipe;
9252 fragment_pipe->get_caps(gl_info, &fragment_caps);
9253 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9254 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9256 device->vertex_priv = vertex_priv;
9257 device->fragment_priv = fragment_priv;
9258 device->shader_priv = priv;
9260 return WINED3D_OK;
9262 fail:
9263 constant_heap_free(&priv->pconst_heap);
9264 constant_heap_free(&priv->vconst_heap);
9265 HeapFree(GetProcessHeap(), 0, priv->stack);
9266 string_buffer_free(&priv->shader_buffer);
9267 fragment_pipe->free_private(device);
9268 vertex_pipe->vp_free(device);
9269 HeapFree(GetProcessHeap(), 0, priv);
9270 return E_OUTOFMEMORY;
9273 /* Context activation is done by the caller. */
9274 static void shader_glsl_free(struct wined3d_device *device)
9276 struct shader_glsl_priv *priv = device->shader_priv;
9278 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9279 constant_heap_free(&priv->pconst_heap);
9280 constant_heap_free(&priv->vconst_heap);
9281 HeapFree(GetProcessHeap(), 0, priv->stack);
9282 string_buffer_list_cleanup(&priv->string_buffers);
9283 string_buffer_free(&priv->shader_buffer);
9284 priv->fragment_pipe->free_private(device);
9285 priv->vertex_pipe->vp_free(device);
9287 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9288 device->shader_priv = NULL;
9291 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9293 struct glsl_context_data *ctx_data;
9294 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9295 return FALSE;
9296 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9297 context->shader_backend_data = ctx_data;
9298 return TRUE;
9301 static void shader_glsl_free_context_data(struct wined3d_context *context)
9303 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9306 static void shader_glsl_init_context_state(struct wined3d_context *context)
9308 const struct wined3d_gl_info *gl_info = context->gl_info;
9310 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9311 checkGLcall("GL_PROGRAM_POINT_SIZE");
9314 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9316 UINT shader_model;
9318 /* FIXME: Check for the specific extensions required for SM5 support
9319 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9320 * soon as we introduce them, adjusting the GL / GLSL version checks
9321 * accordingly. */
9322 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9323 && gl_info->supported[ARB_COMPUTE_SHADER]
9324 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9325 && gl_info->supported[ARB_GPU_SHADER5]
9326 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9327 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9328 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9329 shader_model = 5;
9330 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9331 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9332 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9333 shader_model = 4;
9334 /* Support for texldd and texldl instructions in pixel shaders is required
9335 * for SM3. */
9336 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9337 shader_model = 3;
9338 else
9339 shader_model = 2;
9340 TRACE("Shader model %u.\n", shader_model);
9342 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9343 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9344 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9345 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9346 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9347 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9349 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9350 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9351 caps->varying_count = gl_info->limits.glsl_varyings;
9353 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9354 * Direct3D minimum requirement.
9356 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9357 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9359 * The problem is that the refrast clamps temporary results in the shader to
9360 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9361 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9362 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9363 * offer a way to query this.
9365 if (shader_model >= 4)
9366 caps->ps_1x_max_value = FLT_MAX;
9367 else
9368 caps->ps_1x_max_value = 1024.0f;
9370 /* Ideally we'd only set caps like sRGB writes here if supported by both
9371 * the shader backend and the fragment pipe, but we can get called before
9372 * shader_glsl_alloc(). */
9373 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9374 | WINED3D_SHADER_CAP_SRGB_WRITE;
9377 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9379 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9381 TRACE("Checking support for fixup:\n");
9382 dump_color_fixup_desc(fixup);
9385 /* We support everything except YUV conversions. */
9386 if (!is_complex_fixup(fixup))
9388 TRACE("[OK]\n");
9389 return TRUE;
9392 TRACE("[FAILED]\n");
9393 return FALSE;
9396 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9398 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9399 /* WINED3DSIH_ADD */ shader_glsl_binop,
9400 /* WINED3DSIH_AND */ shader_glsl_binop,
9401 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9402 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9403 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9404 /* WINED3DSIH_ATOMIC_IMAX */ shader_glsl_atomic,
9405 /* WINED3DSIH_ATOMIC_IMIN */ shader_glsl_atomic,
9406 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9407 /* WINED3DSIH_ATOMIC_UMAX */ shader_glsl_atomic,
9408 /* WINED3DSIH_ATOMIC_UMIN */ shader_glsl_atomic,
9409 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9410 /* WINED3DSIH_BEM */ shader_glsl_bem,
9411 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9412 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9413 /* WINED3DSIH_BREAK */ shader_glsl_break,
9414 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9415 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9416 /* WINED3DSIH_BUFINFO */ NULL,
9417 /* WINED3DSIH_CALL */ shader_glsl_call,
9418 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9419 /* WINED3DSIH_CASE */ shader_glsl_case,
9420 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9421 /* WINED3DSIH_CND */ shader_glsl_cnd,
9422 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9423 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9424 /* WINED3DSIH_CRS */ shader_glsl_cross,
9425 /* WINED3DSIH_CUT */ shader_glsl_cut,
9426 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9427 /* WINED3DSIH_DCL */ shader_glsl_nop,
9428 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9429 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9430 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9431 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9432 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9433 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9434 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9435 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9436 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9437 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9438 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9439 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9440 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9441 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9442 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9443 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9444 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9445 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9446 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9447 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9448 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9449 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9450 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ shader_glsl_nop,
9451 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9452 /* WINED3DSIH_DCL_STREAM */ NULL,
9453 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9454 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9455 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9456 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9457 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
9458 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
9459 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9460 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9461 /* WINED3DSIH_DCL_UAV_STRUCTURED */ shader_glsl_nop,
9462 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9463 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9464 /* WINED3DSIH_DEF */ shader_glsl_nop,
9465 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9466 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9467 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9468 /* WINED3DSIH_DIV */ shader_glsl_binop,
9469 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9470 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9471 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9472 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9473 /* WINED3DSIH_DST */ shader_glsl_dst,
9474 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9475 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9476 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9477 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9478 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9479 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9480 /* WINED3DSIH_ELSE */ shader_glsl_else,
9481 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9482 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9483 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9484 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9485 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9486 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9487 /* WINED3DSIH_EQ */ shader_glsl_relop,
9488 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9489 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9490 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9491 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9492 /* WINED3DSIH_FCALL */ NULL,
9493 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9494 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9495 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9496 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9497 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9498 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9499 /* WINED3DSIH_GATHER4 */ NULL,
9500 /* WINED3DSIH_GATHER4_C */ NULL,
9501 /* WINED3DSIH_GE */ shader_glsl_relop,
9502 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9503 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9504 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9505 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9506 /* WINED3DSIH_IADD */ shader_glsl_binop,
9507 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9508 /* WINED3DSIH_IF */ shader_glsl_if,
9509 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9510 /* WINED3DSIH_IGE */ shader_glsl_relop,
9511 /* WINED3DSIH_ILT */ shader_glsl_relop,
9512 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9513 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9514 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9515 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
9516 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9517 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9518 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
9519 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9520 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9521 /* WINED3DSIH_IMM_ATOMIC_IMAX */ shader_glsl_atomic,
9522 /* WINED3DSIH_IMM_ATOMIC_IMIN */ shader_glsl_atomic,
9523 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9524 /* WINED3DSIH_IMM_ATOMIC_UMAX */ shader_glsl_atomic,
9525 /* WINED3DSIH_IMM_ATOMIC_UMIN */ shader_glsl_atomic,
9526 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9527 /* WINED3DSIH_IMUL */ shader_glsl_imul,
9528 /* WINED3DSIH_INE */ shader_glsl_relop,
9529 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9530 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9531 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9532 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9533 /* WINED3DSIH_LABEL */ shader_glsl_label,
9534 /* WINED3DSIH_LD */ shader_glsl_ld,
9535 /* WINED3DSIH_LD2DMS */ NULL,
9536 /* WINED3DSIH_LD_RAW */ shader_glsl_ld_buffer,
9537 /* WINED3DSIH_LD_STRUCTURED */ shader_glsl_ld_buffer,
9538 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9539 /* WINED3DSIH_LIT */ shader_glsl_lit,
9540 /* WINED3DSIH_LOD */ NULL,
9541 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9542 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9543 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9544 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9545 /* WINED3DSIH_LT */ shader_glsl_relop,
9546 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9547 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9548 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9549 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9550 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9551 /* WINED3DSIH_MAD */ shader_glsl_mad,
9552 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9553 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9554 /* WINED3DSIH_MOV */ shader_glsl_mov,
9555 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9556 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9557 /* WINED3DSIH_MUL */ shader_glsl_binop,
9558 /* WINED3DSIH_NE */ shader_glsl_relop,
9559 /* WINED3DSIH_NOP */ shader_glsl_nop,
9560 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9561 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9562 /* WINED3DSIH_OR */ shader_glsl_binop,
9563 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9564 /* WINED3DSIH_POW */ shader_glsl_pow,
9565 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9566 /* WINED3DSIH_REP */ shader_glsl_rep,
9567 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9568 /* WINED3DSIH_RET */ shader_glsl_ret,
9569 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9570 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9571 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9572 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9573 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9574 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9575 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9576 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9577 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9578 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9579 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9580 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9581 /* WINED3DSIH_SAMPLE_POS */ NULL,
9582 /* WINED3DSIH_SETP */ NULL,
9583 /* WINED3DSIH_SGE */ shader_glsl_compare,
9584 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9585 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9586 /* WINED3DSIH_SLT */ shader_glsl_compare,
9587 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9588 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_buffer,
9589 /* WINED3DSIH_STORE_STRUCTURED */ shader_glsl_store_buffer,
9590 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9591 /* WINED3DSIH_SUB */ shader_glsl_binop,
9592 /* WINED3DSIH_SWAPC */ NULL,
9593 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9594 /* WINED3DSIH_SYNC */ NULL,
9595 /* WINED3DSIH_TEX */ shader_glsl_tex,
9596 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9597 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9598 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9599 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9600 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9601 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9602 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9603 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9604 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9605 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9606 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9607 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9608 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9609 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9610 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9611 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9612 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9613 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9614 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9615 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9616 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9617 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9618 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9619 /* WINED3DSIH_UGE */ shader_glsl_relop,
9620 /* WINED3DSIH_ULT */ shader_glsl_relop,
9621 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9622 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9623 /* WINED3DSIH_USHR */ shader_glsl_binop,
9624 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9625 /* WINED3DSIH_XOR */ shader_glsl_binop,
9628 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9629 SHADER_HANDLER hw_fct;
9631 /* Select handler */
9632 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9634 /* Unhandled opcode */
9635 if (!hw_fct)
9637 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9638 return;
9640 hw_fct(ins);
9642 shader_glsl_add_instruction_modifiers(ins);
9645 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9647 struct shader_glsl_priv *priv = shader_priv;
9649 return priv->ffp_proj_control;
9652 const struct wined3d_shader_backend_ops glsl_shader_backend =
9654 shader_glsl_handle_instruction,
9655 shader_glsl_select,
9656 shader_glsl_select_compute,
9657 shader_glsl_disable,
9658 shader_glsl_update_float_vertex_constants,
9659 shader_glsl_update_float_pixel_constants,
9660 shader_glsl_load_constants,
9661 shader_glsl_destroy,
9662 shader_glsl_alloc,
9663 shader_glsl_free,
9664 shader_glsl_allocate_context_data,
9665 shader_glsl_free_context_data,
9666 shader_glsl_init_context_state,
9667 shader_glsl_get_caps,
9668 shader_glsl_color_fixup_supported,
9669 shader_glsl_has_ffp_proj_control,
9672 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9674 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9676 caps->xyzrhw = TRUE;
9677 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9678 caps->ffp_generic_attributes = TRUE;
9679 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9680 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9681 caps->max_vertex_blend_matrix_index = 0;
9682 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9683 | WINED3DVTXPCAPS_MATERIALSOURCE7
9684 | WINED3DVTXPCAPS_VERTEXFOG
9685 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9686 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9687 | WINED3DVTXPCAPS_LOCALVIEWER
9688 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9689 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9690 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9691 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9694 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9696 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9697 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9698 return 0;
9701 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9703 struct shader_glsl_priv *priv;
9705 if (shader_backend == &glsl_shader_backend)
9707 priv = shader_priv;
9708 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9709 return priv;
9712 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9714 return NULL;
9717 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9719 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9720 struct glsl_ffp_vertex_shader, desc.entry);
9721 struct glsl_shader_prog_link *program, *program2;
9722 struct glsl_ffp_destroy_ctx *ctx = context;
9724 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9725 struct glsl_shader_prog_link, vs.shader_entry)
9727 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9729 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9730 HeapFree(GetProcessHeap(), 0, shader);
9733 /* Context activation is done by the caller. */
9734 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9736 struct shader_glsl_priv *priv = device->vertex_priv;
9737 struct glsl_ffp_destroy_ctx ctx;
9739 ctx.priv = priv;
9740 ctx.gl_info = &device->adapter->gl_info;
9741 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9744 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9745 const struct wined3d_state *state, DWORD state_id) {}
9747 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9748 const struct wined3d_state *state, DWORD state_id)
9750 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9753 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9754 const struct wined3d_state *state, DWORD state_id)
9756 const struct wined3d_gl_info *gl_info = context->gl_info;
9757 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9758 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9759 BOOL transformed = context->stream_info.position_transformed;
9760 BOOL wasrhw = context->last_was_rhw;
9761 unsigned int i;
9763 context->last_was_rhw = transformed;
9765 /* If the vertex declaration contains a transformed position attribute,
9766 * the draw uses the fixed function vertex pipeline regardless of any
9767 * vertex shader set by the application. */
9768 if (transformed != wasrhw
9769 || context->stream_info.swizzle_map != context->last_swizzle_map)
9770 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9772 context->last_swizzle_map = context->stream_info.swizzle_map;
9774 if (!use_vs(state))
9776 if (context->last_was_vshader)
9778 if (legacy_context)
9779 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9780 clipplane(context, state, STATE_CLIPPLANE(i));
9781 else
9782 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9785 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9787 /* Because of settings->texcoords, we have to regenerate the vertex
9788 * shader on a vdecl change if there aren't enough varyings to just
9789 * always output all the texture coordinates. */
9790 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9791 || normal != context->last_was_normal)
9792 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9794 if (use_ps(state)
9795 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9796 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9797 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9799 else
9801 if (!context->last_was_vshader)
9803 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9804 if (legacy_context)
9805 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9806 clipplane(context, state, STATE_CLIPPLANE(i));
9807 else
9808 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9812 context->last_was_vshader = use_vs(state);
9813 context->last_was_normal = normal;
9816 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9817 const struct wined3d_state *state, DWORD state_id)
9819 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9820 /* Different vertex shaders potentially require a different vertex attributes setup. */
9821 if (!isStateDirty(context, STATE_VDECL))
9822 context_apply_state(context, state, STATE_VDECL);
9825 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9826 const struct wined3d_state *state, DWORD state_id)
9828 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9829 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9830 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9833 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9834 const struct wined3d_state *state, DWORD state_id)
9836 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9837 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9838 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9839 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9840 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9843 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9844 const struct wined3d_state *state, DWORD state_id)
9846 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9849 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9850 const struct wined3d_state *state, DWORD state_id)
9852 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9855 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9857 const struct wined3d_gl_info *gl_info = context->gl_info;
9858 unsigned int k;
9860 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9861 | WINED3D_SHADER_CONST_FFP_LIGHTS
9862 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9864 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9866 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9868 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9869 clipplane(context, state, STATE_CLIPPLANE(k));
9872 else
9874 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9878 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9879 const struct wined3d_state *state, DWORD state_id)
9881 /* Table fog behavior depends on the projection matrix. */
9882 if (state->render_states[WINED3D_RS_FOGENABLE]
9883 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9884 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9885 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9888 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9889 const struct wined3d_state *state, DWORD state_id)
9891 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9892 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9893 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9894 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9895 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9896 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9899 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9900 const struct wined3d_state *state, DWORD state_id)
9902 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9905 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9906 const struct wined3d_state *state, DWORD state_id)
9908 DWORD sampler = state_id - STATE_SAMPLER(0);
9909 const struct wined3d_texture *texture = state->textures[sampler];
9910 BOOL np2;
9912 if (!texture)
9913 return;
9915 if (sampler >= MAX_TEXTURES)
9916 return;
9918 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9919 || context->lastWasPow2Texture & (1u << sampler))
9921 if (np2)
9922 context->lastWasPow2Texture |= 1u << sampler;
9923 else
9924 context->lastWasPow2Texture &= ~(1u << sampler);
9926 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9930 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9931 const struct wined3d_state *state, DWORD state_id)
9933 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9936 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9937 const struct wined3d_state *state, DWORD state_id)
9939 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9942 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9943 const struct wined3d_state *state, DWORD state_id)
9945 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9948 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9949 const struct wined3d_state *state, DWORD state_id)
9951 if (!use_vs(state))
9952 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9955 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9956 const struct wined3d_state *state, DWORD state_id)
9958 static unsigned int once;
9960 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9961 FIXME("Non-point sprite points not supported in core profile.\n");
9964 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9965 const struct wined3d_state *state, DWORD state_id)
9967 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9970 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9971 const struct wined3d_state *state, DWORD state_id)
9973 const struct wined3d_gl_info *gl_info = context->gl_info;
9974 UINT index = state_id - STATE_CLIPPLANE(0);
9976 if (index >= gl_info->limits.user_clip_distances)
9977 return;
9979 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9982 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9984 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9985 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9986 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9987 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9988 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9989 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9990 /* Clip planes */
9991 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9992 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9993 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9994 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9995 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9996 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9997 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9998 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9999 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10000 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10001 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10002 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10003 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10004 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10005 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10006 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10007 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10008 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10009 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10010 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10011 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10012 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10013 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10014 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10015 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10016 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10017 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10018 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10019 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10020 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10021 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10022 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10023 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10024 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10025 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10026 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10027 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10028 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10029 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10030 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10031 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10032 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10033 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10034 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10035 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10036 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10037 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10038 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10039 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10040 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10041 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10042 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10043 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10044 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10045 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10046 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10047 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10048 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10049 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10050 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10051 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10052 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10053 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
10054 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
10055 /* Lights */
10056 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10057 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10058 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10059 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10060 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10061 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10062 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10063 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10064 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10065 /* Viewport */
10066 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
10067 /* Transform states */
10068 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
10069 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
10070 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10071 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10072 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10073 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10074 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10075 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10076 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10077 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
10078 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
10079 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10080 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10081 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
10082 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10083 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10084 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10085 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10086 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10087 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10088 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10089 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
10090 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10091 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10092 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10093 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10094 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10095 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10096 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10097 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10098 /* Fog */
10099 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10100 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10101 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10102 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10103 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
10104 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
10105 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10106 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
10107 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10108 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10109 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10110 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10111 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10112 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10113 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10114 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10115 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10116 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
10117 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
10118 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
10119 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
10120 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
10121 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10122 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10123 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
10124 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
10125 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10126 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
10127 /* NP2 texture matrix fixups. They are not needed if
10128 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
10129 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
10130 * matrix. */
10131 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10132 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10133 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10134 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10135 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10136 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10137 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10138 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10139 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10140 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10141 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10142 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10143 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10144 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10145 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10146 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10147 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10148 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10149 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10150 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10151 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10152 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
10153 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
10154 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
10155 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
10156 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
10157 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
10158 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
10161 /* TODO:
10162 * - Implement vertex tweening. */
10163 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
10165 glsl_vertex_pipe_vp_enable,
10166 glsl_vertex_pipe_vp_get_caps,
10167 glsl_vertex_pipe_vp_get_emul_mask,
10168 glsl_vertex_pipe_vp_alloc,
10169 glsl_vertex_pipe_vp_free,
10170 glsl_vertex_pipe_vp_states,
10173 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10175 /* Nothing to do. */
10178 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10180 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10181 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10182 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10183 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10184 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10185 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10186 | WINED3DTEXOPCAPS_SELECTARG1
10187 | WINED3DTEXOPCAPS_SELECTARG2
10188 | WINED3DTEXOPCAPS_MODULATE4X
10189 | WINED3DTEXOPCAPS_MODULATE2X
10190 | WINED3DTEXOPCAPS_MODULATE
10191 | WINED3DTEXOPCAPS_ADDSIGNED2X
10192 | WINED3DTEXOPCAPS_ADDSIGNED
10193 | WINED3DTEXOPCAPS_ADD
10194 | WINED3DTEXOPCAPS_SUBTRACT
10195 | WINED3DTEXOPCAPS_ADDSMOOTH
10196 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10197 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10198 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10199 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10200 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10201 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10202 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10203 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10204 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10205 | WINED3DTEXOPCAPS_DOTPRODUCT3
10206 | WINED3DTEXOPCAPS_MULTIPLYADD
10207 | WINED3DTEXOPCAPS_LERP
10208 | WINED3DTEXOPCAPS_BUMPENVMAP
10209 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10210 caps->MaxTextureBlendStages = MAX_TEXTURES;
10211 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10214 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10216 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10217 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10218 return 0;
10221 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10223 struct shader_glsl_priv *priv;
10225 if (shader_backend == &glsl_shader_backend)
10227 priv = shader_priv;
10228 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10229 return priv;
10232 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10234 return NULL;
10237 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10239 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10240 struct glsl_ffp_fragment_shader, entry.entry);
10241 struct glsl_shader_prog_link *program, *program2;
10242 struct glsl_ffp_destroy_ctx *ctx = context;
10244 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10245 struct glsl_shader_prog_link, ps.shader_entry)
10247 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10249 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10250 HeapFree(GetProcessHeap(), 0, shader);
10253 /* Context activation is done by the caller. */
10254 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10256 struct shader_glsl_priv *priv = device->fragment_priv;
10257 struct glsl_ffp_destroy_ctx ctx;
10259 ctx.priv = priv;
10260 ctx.gl_info = &device->adapter->gl_info;
10261 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10264 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10265 const struct wined3d_state *state, DWORD state_id)
10267 context->last_was_pshader = use_ps(state);
10269 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10272 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10273 const struct wined3d_state *state, DWORD state_id)
10275 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10278 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10279 const struct wined3d_state *state, DWORD state_id)
10281 BOOL use_vshader = use_vs(state);
10282 enum fogsource new_source;
10283 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10284 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10286 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10288 if (!state->render_states[WINED3D_RS_FOGENABLE])
10289 return;
10291 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10293 if (use_vshader)
10294 new_source = FOGSOURCE_VS;
10295 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10296 new_source = FOGSOURCE_COORD;
10297 else
10298 new_source = FOGSOURCE_FFP;
10300 else
10302 new_source = FOGSOURCE_FFP;
10305 if (new_source != context->fog_source || fogstart == fogend)
10307 context->fog_source = new_source;
10308 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10312 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10313 const struct wined3d_state *state, DWORD state_id)
10315 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10316 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10317 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10319 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10320 glsl_fragment_pipe_fog(context, state, state_id);
10323 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10324 const struct wined3d_state *state, DWORD state_id)
10326 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10327 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10328 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10331 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10332 const struct wined3d_state *state, DWORD state_id)
10334 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10337 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10338 const struct wined3d_state *state, DWORD state_id)
10340 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10343 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10344 const struct wined3d_state *state, DWORD state_id)
10346 const struct wined3d_gl_info *gl_info = context->gl_info;
10347 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10348 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10350 if (func)
10352 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10353 checkGLcall("glAlphaFunc");
10357 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10358 const struct wined3d_state *state, DWORD state_id)
10360 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10363 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10364 const struct wined3d_state *state, DWORD state_id)
10366 const struct wined3d_gl_info *gl_info = context->gl_info;
10368 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10370 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10371 checkGLcall("glEnable(GL_ALPHA_TEST)");
10373 else
10375 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10376 checkGLcall("glDisable(GL_ALPHA_TEST)");
10380 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10381 const struct wined3d_state *state, DWORD state_id)
10383 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10386 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10387 const struct wined3d_state *state, DWORD state_id)
10389 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10392 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10393 const struct wined3d_state *state, DWORD state_id)
10395 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10398 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10400 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10401 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10402 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10403 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10404 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10405 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10406 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10407 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10408 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10409 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10410 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10411 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10412 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10413 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10414 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10415 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10416 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10417 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10418 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10419 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10420 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10421 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10422 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10423 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10424 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10425 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10426 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10427 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10428 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10429 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10430 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10431 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10432 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10433 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10434 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10435 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10436 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10437 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10438 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10439 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10440 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10441 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10442 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10443 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10444 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10445 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10446 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10447 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10448 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10449 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10450 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10451 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10452 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10453 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10454 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10455 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10456 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10457 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10458 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10459 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10460 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10461 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10462 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10463 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10464 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10465 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10466 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10467 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10468 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10469 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10470 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10471 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10472 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10473 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10474 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10475 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10476 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10477 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10478 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10479 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10480 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10481 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10482 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10483 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10484 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10485 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10486 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10487 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10488 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10489 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10490 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10491 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10492 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10493 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10494 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10495 {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 },
10496 {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 },
10497 {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 },
10498 {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 },
10499 {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 },
10500 {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 },
10501 {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 },
10502 {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 },
10503 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10504 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10505 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10506 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10507 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10508 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10509 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10510 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10511 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10512 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10513 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10514 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10515 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10518 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10520 return TRUE;
10523 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10527 const struct fragment_pipeline glsl_fragment_pipe =
10529 glsl_fragment_pipe_enable,
10530 glsl_fragment_pipe_get_caps,
10531 glsl_fragment_pipe_get_emul_mask,
10532 glsl_fragment_pipe_alloc,
10533 glsl_fragment_pipe_free,
10534 glsl_fragment_pipe_alloc_context_data,
10535 glsl_fragment_pipe_free_context_data,
10536 shader_glsl_color_fixup_supported,
10537 glsl_fragment_pipe_state_template,