wined3d: Handle declarations for raw buffer shader resources.
[wine.git] / dlls / wined3d / glsl_shader.c
blob7f467e8e21d910875420a0324f0db55485f2a697
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 const char *shader_glsl_get_version_declaration(const struct wined3d_gl_info *gl_info,
367 const struct wined3d_shader_version *version)
369 unsigned int glsl_version;
371 switch (glsl_version = shader_glsl_get_version(gl_info, version))
373 case 150:
374 return "#version 150";
375 case 130:
376 return "#version 130";
377 case 120:
378 return "#version 120";
379 default:
380 FIXME("Unexpected GLSL version %u requested.\n", glsl_version);
381 return "";
385 static void shader_glsl_append_imm_vec4(struct wined3d_string_buffer *buffer, const float *values)
387 char str[4][17];
389 wined3d_ftoa(values[0], str[0]);
390 wined3d_ftoa(values[1], str[1]);
391 wined3d_ftoa(values[2], str[2]);
392 wined3d_ftoa(values[3], str[3]);
393 shader_addline(buffer, "vec4(%s, %s, %s, %s)", str[0], str[1], str[2], str[3]);
396 static void shader_glsl_append_imm_ivec(struct wined3d_string_buffer *buffer,
397 const int *values, unsigned int size)
399 int i;
401 if (!size || size > 4)
403 ERR("Invalid vector size %u.\n", size);
404 return;
407 if (size > 1)
408 shader_addline(buffer, "ivec%u(", size);
410 for (i = 0; i < size; ++i)
411 shader_addline(buffer, i ? ", %#x" : "%#x", values[i]);
413 if (size > 1)
414 shader_addline(buffer, ")");
417 static const char *get_info_log_line(const char **ptr)
419 const char *p, *q;
421 p = *ptr;
422 if (!(q = strstr(p, "\n")))
424 if (!*p) return NULL;
425 *ptr += strlen(p);
426 return p;
428 *ptr = q + 1;
430 return p;
433 /* Context activation is done by the caller. */
434 void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLuint id, BOOL program)
436 int length = 0;
437 char *log;
439 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
440 return;
442 if (program)
443 GL_EXTCALL(glGetProgramiv(id, GL_INFO_LOG_LENGTH, &length));
444 else
445 GL_EXTCALL(glGetShaderiv(id, GL_INFO_LOG_LENGTH, &length));
447 /* A size of 1 is just a null-terminated string, so the log should be bigger than
448 * that if there are errors. */
449 if (length > 1)
451 const char *ptr, *line;
453 log = HeapAlloc(GetProcessHeap(), 0, length);
454 /* The info log is supposed to be zero-terminated, but at least some
455 * versions of fglrx don't terminate the string properly. The reported
456 * length does include the terminator, so explicitly set it to zero
457 * here. */
458 log[length - 1] = 0;
459 if (program)
460 GL_EXTCALL(glGetProgramInfoLog(id, length, NULL, log));
461 else
462 GL_EXTCALL(glGetShaderInfoLog(id, length, NULL, log));
464 ptr = log;
465 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
467 WARN("Info log received from GLSL shader #%u:\n", id);
468 while ((line = get_info_log_line(&ptr))) WARN(" %.*s", (int)(ptr - line), line);
470 else
472 FIXME("Info log received from GLSL shader #%u:\n", id);
473 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
475 HeapFree(GetProcessHeap(), 0, log);
479 /* Context activation is done by the caller. */
480 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLuint shader, const char *src)
482 const char *ptr, *line;
484 TRACE("Compiling shader object %u.\n", shader);
486 if (TRACE_ON(d3d_shader))
488 ptr = src;
489 while ((line = get_info_log_line(&ptr))) TRACE_(d3d_shader)(" %.*s", (int)(ptr - line), line);
492 GL_EXTCALL(glShaderSource(shader, 1, &src, NULL));
493 checkGLcall("glShaderSource");
494 GL_EXTCALL(glCompileShader(shader));
495 checkGLcall("glCompileShader");
496 print_glsl_info_log(gl_info, shader, FALSE);
499 /* Context activation is done by the caller. */
500 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLuint program)
502 GLint i, shader_count, source_size = -1;
503 GLuint *shaders;
504 char *source = NULL;
506 GL_EXTCALL(glGetProgramiv(program, GL_ATTACHED_SHADERS, &shader_count));
507 if (!(shaders = wined3d_calloc(shader_count, sizeof(*shaders))))
509 ERR("Failed to allocate shader array memory.\n");
510 return;
513 GL_EXTCALL(glGetAttachedShaders(program, shader_count, NULL, shaders));
514 for (i = 0; i < shader_count; ++i)
516 const char *ptr, *line;
517 GLint tmp;
519 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_SOURCE_LENGTH, &tmp));
521 if (source_size < tmp)
523 HeapFree(GetProcessHeap(), 0, source);
525 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
526 if (!source)
528 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
529 HeapFree(GetProcessHeap(), 0, shaders);
530 return;
532 source_size = tmp;
535 FIXME("Shader %u:\n", shaders[i]);
536 GL_EXTCALL(glGetShaderiv(shaders[i], GL_SHADER_TYPE, &tmp));
537 FIXME(" GL_SHADER_TYPE: %s.\n", debug_gl_shader_type(tmp));
538 GL_EXTCALL(glGetShaderiv(shaders[i], GL_COMPILE_STATUS, &tmp));
539 FIXME(" GL_COMPILE_STATUS: %d.\n", tmp);
540 FIXME("\n");
542 ptr = source;
543 GL_EXTCALL(glGetShaderSource(shaders[i], source_size, NULL, source));
544 while ((line = get_info_log_line(&ptr))) FIXME(" %.*s", (int)(ptr - line), line);
545 FIXME("\n");
548 HeapFree(GetProcessHeap(), 0, source);
549 HeapFree(GetProcessHeap(), 0, shaders);
552 /* Context activation is done by the caller. */
553 void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLuint program)
555 GLint tmp;
557 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader))
558 return;
560 GL_EXTCALL(glGetProgramiv(program, GL_LINK_STATUS, &tmp));
561 if (!tmp)
563 FIXME("Program %u link status invalid.\n", program);
564 shader_glsl_dump_program_source(gl_info, program);
567 print_glsl_info_log(gl_info, program, TRUE);
570 /* Context activation is done by the caller. */
571 static void shader_glsl_load_samplers(const struct wined3d_gl_info *gl_info,
572 struct shader_glsl_priv *priv, const DWORD *tex_unit_map, GLuint program_id)
574 unsigned int mapped_unit;
575 struct wined3d_string_buffer *sampler_name = string_buffer_get(&priv->string_buffers);
576 const char *prefix;
577 unsigned int i, j;
578 GLint name_loc;
580 static const struct
582 enum wined3d_shader_type type;
583 unsigned int base_idx;
584 unsigned int count;
586 sampler_info[] =
588 {WINED3D_SHADER_TYPE_PIXEL, 0, MAX_FRAGMENT_SAMPLERS},
589 {WINED3D_SHADER_TYPE_VERTEX, MAX_FRAGMENT_SAMPLERS, MAX_VERTEX_SAMPLERS},
592 for (i = 0; i < ARRAY_SIZE(sampler_info); ++i)
594 prefix = shader_glsl_get_prefix(sampler_info[i].type);
596 for (j = 0; j < sampler_info[i].count; ++j)
598 string_buffer_sprintf(sampler_name, "%s_sampler%u", prefix, j);
599 name_loc = GL_EXTCALL(glGetUniformLocation(program_id, sampler_name->buffer));
600 if (name_loc == -1)
601 continue;
603 mapped_unit = tex_unit_map[sampler_info[i].base_idx + j];
604 if (mapped_unit == WINED3D_UNMAPPED_STAGE || mapped_unit >= gl_info->limits.combined_samplers)
606 ERR("Trying to load sampler %s on unsupported unit %u.\n", sampler_name->buffer, mapped_unit);
607 continue;
610 TRACE("Loading sampler %s on unit %u.\n", sampler_name->buffer, mapped_unit);
611 GL_EXTCALL(glUniform1i(name_loc, mapped_unit));
614 checkGLcall("Load sampler bindings");
615 string_buffer_release(&priv->string_buffers, sampler_name);
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 const BOOL *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[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)
1875 if (input_reg_used[input->register_idx])
1876 return TRUE;
1877 else
1878 return FALSE;
1881 return FALSE;
1884 static BOOL glsl_is_shadow_sampler(const struct wined3d_shader *shader,
1885 const struct ps_compile_args *ps_args, unsigned int resource_idx, unsigned int sampler_idx)
1887 const struct wined3d_shader_version *version = &shader->reg_maps.shader_version;
1889 if (version->major >= 4)
1890 return shader->reg_maps.sampler_comparison_mode & (1u << sampler_idx);
1891 else
1892 return version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1u << resource_idx));
1895 static void shader_glsl_declare_typed_vertex_attribute(struct wined3d_string_buffer *buffer,
1896 const struct wined3d_gl_info *gl_info, const char *vector_type, const char *scalar_type,
1897 unsigned int index)
1899 shader_addline(buffer, "%s %s4 vs_in_%s%u;\n",
1900 get_attribute_keyword(gl_info), vector_type, scalar_type, index);
1901 shader_addline(buffer, "vec4 vs_in%u = %sBitsToFloat(vs_in_%s%u);\n",
1902 index, scalar_type, scalar_type, index);
1905 static void shader_glsl_declare_generic_vertex_attribute(struct wined3d_string_buffer *buffer,
1906 const struct wined3d_gl_info *gl_info, const struct wined3d_shader_signature_element *e)
1908 unsigned int index = e->register_idx;
1910 if (e->sysval_semantic == WINED3D_SV_VERTEX_ID)
1912 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_VertexID), 0.0, 0.0, 0.0);\n",
1913 index);
1914 return;
1916 if (e->sysval_semantic == WINED3D_SV_INSTANCE_ID)
1918 shader_addline(buffer, "vec4 vs_in%u = vec4(intBitsToFloat(gl_InstanceID), 0.0, 0.0, 0.0);\n",
1919 index);
1920 return;
1922 if (e->sysval_semantic && e->sysval_semantic != WINED3D_SV_POSITION)
1923 FIXME("Unhandled sysval semantic %#x.\n", e->sysval_semantic);
1925 if (shader_glsl_use_explicit_attrib_location(gl_info))
1926 shader_addline(buffer, "layout(location = %u) ", index);
1928 switch (e->component_type)
1930 case WINED3D_TYPE_UINT:
1931 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "uvec", "uint", index);
1932 break;
1933 case WINED3D_TYPE_INT:
1934 shader_glsl_declare_typed_vertex_attribute(buffer, gl_info, "ivec", "int", index);
1935 break;
1937 default:
1938 FIXME("Unhandled type %#x.\n", e->component_type);
1939 /* Fall through. */
1940 case WINED3D_TYPE_UNKNOWN:
1941 case WINED3D_TYPE_FLOAT:
1942 shader_addline(buffer, "%s vec4 vs_in%u;\n", get_attribute_keyword(gl_info), index);
1943 break;
1947 /** Generate the variable & register declarations for the GLSL output target */
1948 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
1949 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
1950 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
1952 const struct wined3d_shader_version *version = &reg_maps->shader_version;
1953 const struct vs_compile_args *vs_args = ctx_priv->cur_vs_args;
1954 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
1955 const struct wined3d_gl_info *gl_info = context->gl_info;
1956 const struct wined3d_shader_indexable_temp *idx_temp_reg;
1957 unsigned int i, extra_constants_needed = 0;
1958 const struct wined3d_shader_lconst *lconst;
1959 const char *prefix;
1960 DWORD map;
1962 prefix = shader_glsl_get_prefix(version->type);
1964 /* Prototype the subroutines */
1965 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
1967 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
1970 /* Declare the constants (aka uniforms) */
1971 if (shader->limits->constant_float > 0)
1973 unsigned max_constantsF;
1975 /* Unless the shader uses indirect addressing, always declare the
1976 * maximum array size and ignore that we need some uniforms privately.
1977 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
1978 * and immediate values, still declare VC[256]. If the shader needs
1979 * more uniforms than we have it won't work in any case. If it uses
1980 * less, the compiler will figure out which uniforms are really used
1981 * and strip them out. This allows a shader to use c255 on a dx9 card,
1982 * as long as it doesn't also use all the other constants.
1984 * If the shader uses indirect addressing the compiler must assume
1985 * that all declared uniforms are used. In this case, declare only the
1986 * amount that we're assured to have.
1988 * Thus we run into problems in these two cases:
1989 * 1) The shader really uses more uniforms than supported.
1990 * 2) The shader uses indirect addressing, less constants than
1991 * supported, but uses a constant index > #supported consts. */
1992 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1994 /* No indirect addressing here. */
1995 max_constantsF = gl_info->limits.glsl_ps_float_constants;
1997 else
1999 if (reg_maps->usesrelconstF)
2001 /* Subtract the other potential uniforms from the max
2002 * available (bools, ints, and 1 row of projection matrix).
2003 * Subtract another uniform for immediate values, which have
2004 * to be loaded via uniform by the driver as well. The shader
2005 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
2006 * shader code, so one vec4 should be enough. (Unfortunately
2007 * the Nvidia driver doesn't store 128 and -128 in one float).
2009 * Writing gl_ClipVertex requires one uniform for each
2010 * clipplane as well. */
2011 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
2012 if (vs_args->clip_enabled)
2013 max_constantsF -= gl_info->limits.user_clip_distances;
2014 max_constantsF -= wined3d_popcount(reg_maps->integer_constants);
2015 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
2016 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
2017 * for now take this into account when calculating the number of available constants
2019 max_constantsF -= wined3d_popcount(reg_maps->boolean_constants);
2020 /* Set by driver quirks in directx.c */
2021 max_constantsF -= gl_info->reserved_glsl_constants;
2023 if (max_constantsF < shader->limits->constant_float)
2025 static unsigned int once;
2027 if (!once++)
2028 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
2029 " it may not render correctly.\n");
2030 else
2031 WARN("The hardware does not support enough uniform components to run this shader.\n");
2034 else
2036 max_constantsF = gl_info->limits.glsl_vs_float_constants;
2039 max_constantsF = min(shader->limits->constant_float, max_constantsF);
2040 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
2043 /* Always declare the full set of constants, the compiler can remove the
2044 * unused ones because d3d doesn't (yet) support indirect int and bool
2045 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
2046 if (shader->limits->constant_int > 0 && reg_maps->integer_constants)
2047 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits->constant_int);
2049 if (shader->limits->constant_bool > 0 && reg_maps->boolean_constants)
2050 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits->constant_bool);
2052 /* Declare immediate constant buffer */
2053 if (reg_maps->icb)
2054 shader_addline(buffer, "uniform vec4 %s_icb[%u];\n", prefix, reg_maps->icb->vec4_count);
2056 /* Declare constant buffers */
2057 for (i = 0; i < WINED3D_MAX_CBS; ++i)
2059 if (reg_maps->cb_sizes[i])
2060 shader_addline(buffer, "layout(std140) uniform block_%s_cb%u { vec4 %s_cb%u[%u]; };\n",
2061 prefix, i, prefix, i, reg_maps->cb_sizes[i]);
2064 /* Declare texture samplers */
2065 for (i = 0; i < reg_maps->sampler_map.count; ++i)
2067 struct wined3d_shader_sampler_map_entry *entry;
2068 const char *sampler_type_prefix, *sampler_type;
2069 BOOL shadow_sampler, tex_rect;
2071 entry = &reg_maps->sampler_map.entries[i];
2073 if (entry->resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
2075 ERR("Invalid resource index %u.\n", entry->resource_idx);
2076 continue;
2079 switch (reg_maps->resource_info[entry->resource_idx].data_type)
2081 case WINED3D_DATA_FLOAT:
2082 case WINED3D_DATA_UNORM:
2083 case WINED3D_DATA_SNORM:
2084 sampler_type_prefix = "";
2085 break;
2087 case WINED3D_DATA_INT:
2088 sampler_type_prefix = "i";
2089 break;
2091 case WINED3D_DATA_UINT:
2092 sampler_type_prefix = "u";
2093 break;
2095 default:
2096 sampler_type_prefix = "";
2097 ERR("Unhandled resource data type %#x.\n", reg_maps->resource_info[i].data_type);
2098 break;
2101 shadow_sampler = glsl_is_shadow_sampler(shader, ps_args, entry->resource_idx, entry->sampler_idx);
2102 switch (reg_maps->resource_info[entry->resource_idx].type)
2104 case WINED3D_SHADER_RESOURCE_BUFFER:
2105 sampler_type = "samplerBuffer";
2106 break;
2108 case WINED3D_SHADER_RESOURCE_TEXTURE_1D:
2109 if (shadow_sampler)
2110 sampler_type = "sampler1DShadow";
2111 else
2112 sampler_type = "sampler1D";
2113 break;
2115 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2116 tex_rect = version->type == WINED3D_SHADER_TYPE_PIXEL
2117 && (ps_args->np2_fixup & (1u << entry->resource_idx))
2118 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
2119 if (shadow_sampler)
2121 if (tex_rect)
2122 sampler_type = "sampler2DRectShadow";
2123 else
2124 sampler_type = "sampler2DShadow";
2126 else
2128 if (tex_rect)
2129 sampler_type = "sampler2DRect";
2130 else
2131 sampler_type = "sampler2D";
2133 break;
2135 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2136 if (shadow_sampler)
2137 FIXME("Unsupported 3D shadow sampler.\n");
2138 sampler_type = "sampler3D";
2139 break;
2141 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBE:
2142 if (shadow_sampler)
2143 FIXME("Unsupported Cube shadow sampler.\n");
2144 sampler_type = "samplerCube";
2145 break;
2147 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2148 if (shadow_sampler)
2149 sampler_type = "sampler2DArrayShadow";
2150 else
2151 sampler_type = "sampler2DArray";
2152 break;
2154 case WINED3D_SHADER_RESOURCE_TEXTURE_CUBEARRAY:
2155 if (shadow_sampler)
2156 FIXME("Unsupported Cube array shadow sampler.\n");
2157 sampler_type = "samplerCubeArray";
2158 break;
2160 default:
2161 sampler_type = "unsupported_sampler";
2162 FIXME("Unhandled resource type %#x.\n", reg_maps->resource_info[entry->resource_idx].type);
2163 break;
2165 shader_addline(buffer, "uniform %s%s %s_sampler%u;\n",
2166 sampler_type_prefix, sampler_type, prefix, entry->bind_idx);
2169 /* Declare images */
2170 for (i = 0; i < ARRAY_SIZE(reg_maps->uav_resource_info); ++i)
2172 const char *image_type_prefix, *image_type, *read_format;
2174 if (!reg_maps->uav_resource_info[i].type)
2175 continue;
2177 switch (reg_maps->uav_resource_info[i].data_type)
2179 case WINED3D_DATA_FLOAT:
2180 case WINED3D_DATA_UNORM:
2181 case WINED3D_DATA_SNORM:
2182 image_type_prefix = "";
2183 read_format = "r32f";
2184 break;
2186 case WINED3D_DATA_INT:
2187 image_type_prefix = "i";
2188 read_format = "r32i";
2189 break;
2191 case WINED3D_DATA_UINT:
2192 image_type_prefix = "u";
2193 read_format = "r32ui";
2194 break;
2196 default:
2197 image_type_prefix = "";
2198 read_format = "";
2199 ERR("Unhandled resource data type %#x.\n", reg_maps->uav_resource_info[i].data_type);
2200 break;
2203 switch (reg_maps->uav_resource_info[i].type)
2205 case WINED3D_SHADER_RESOURCE_BUFFER:
2206 image_type = "imageBuffer";
2207 break;
2209 case WINED3D_SHADER_RESOURCE_TEXTURE_2D:
2210 image_type = "image2D";
2211 break;
2213 case WINED3D_SHADER_RESOURCE_TEXTURE_3D:
2214 image_type = "image3D";
2215 break;
2217 case WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY:
2218 image_type = "image2DArray";
2219 break;
2221 default:
2222 image_type = "unsupported_image";
2223 FIXME("Unhandled resource type %#x.\n", reg_maps->uav_resource_info[i].type);
2224 break;
2227 if (reg_maps->uav_read_mask & (1u << i))
2228 shader_addline(buffer, "layout(%s) uniform %s%s %s_image%u;\n",
2229 read_format, image_type_prefix, image_type, prefix, i);
2230 else
2231 shader_addline(buffer, "writeonly uniform %s%s %s_image%u;\n",
2232 image_type_prefix, image_type, prefix, i);
2235 /* Declare uniforms for NP2 texcoord fixup:
2236 * This is NOT done inside the loop that declares the texture samplers
2237 * since the NP2 fixup code is currently only used for the GeforceFX
2238 * series and when forcing the ARB_npot extension off. Modern cards just
2239 * skip the code anyway, so put it inside a separate loop. */
2240 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
2242 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
2243 UINT cur = 0;
2245 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
2246 * while D3D has them in the (normalized) [0,1]x[0,1] range.
2247 * samplerNP2Fixup stores texture dimensions and is updated through
2248 * shader_glsl_load_np2fixup_constants when the sampler changes. */
2250 for (i = 0; i < shader->limits->sampler; ++i)
2252 if (!reg_maps->resource_info[i].type || !(ps_args->np2_fixup & (1u << i)))
2253 continue;
2255 if (reg_maps->resource_info[i].type != WINED3D_SHADER_RESOURCE_TEXTURE_2D)
2257 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
2258 continue;
2261 fixup->idx[i] = cur++;
2264 fixup->num_consts = (cur + 1) >> 1;
2265 fixup->active = ps_args->np2_fixup;
2266 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
2269 /* Declare address variables */
2270 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
2272 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
2275 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2277 for (i = 0; i < shader->input_signature.element_count; ++i)
2278 shader_glsl_declare_generic_vertex_attribute(buffer, gl_info, &shader->input_signature.elements[i]);
2280 if (vs_args->point_size && !vs_args->per_vertex_point_size)
2282 shader_addline(buffer, "uniform struct\n{\n");
2283 shader_addline(buffer, " float size;\n");
2284 shader_addline(buffer, " float size_min;\n");
2285 shader_addline(buffer, " float size_max;\n");
2286 shader_addline(buffer, "} ffp_point;\n");
2289 if (!gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2291 if (vs_args->clip_enabled)
2292 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
2294 if (version->major < 3)
2296 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2297 declare_out_varying(gl_info, buffer, vs_args->flatshading, "vec4 ffp_varying_specular;\n");
2298 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2299 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2303 if (version->major < 4)
2304 shader_addline(buffer, "void setup_vs_output(in vec4[%u]);\n", shader->limits->packed_output);
2306 else if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2308 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2310 shader_addline(buffer, "varying in vec4 gs_in[][%u];\n", shader->limits->packed_input);
2312 else
2314 shader_addline(buffer, "layout(%s) in;\n", glsl_primitive_type_from_d3d(shader->u.gs.input_type));
2315 shader_addline(buffer, "layout(%s, max_vertices = %u) out;\n",
2316 glsl_primitive_type_from_d3d(shader->u.gs.output_type), shader->u.gs.vertices_out);
2317 shader_addline(buffer, "in vs_gs_iface { vec4 gs_in[%u]; } gs_in[];\n", shader->limits->packed_input);
2320 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2322 if (version->major < 3 || ps_args->vp_mode != vertexshader)
2324 shader_addline(buffer, "uniform struct\n{\n");
2325 shader_addline(buffer, " vec4 color;\n");
2326 shader_addline(buffer, " float density;\n");
2327 shader_addline(buffer, " float end;\n");
2328 shader_addline(buffer, " float scale;\n");
2329 shader_addline(buffer, "} ffp_fog;\n");
2331 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
2333 if (glsl_is_color_reg_read(shader, 0))
2334 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
2335 if (glsl_is_color_reg_read(shader, 1))
2336 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
2337 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2338 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
2340 else
2342 if (glsl_is_color_reg_read(shader, 0))
2343 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_diffuse;\n");
2344 if (glsl_is_color_reg_read(shader, 1))
2345 declare_in_varying(gl_info, buffer, ps_args->flatshading, "vec4 ffp_varying_specular;\n");
2346 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
2347 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
2348 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
2352 if (version->major >= 3)
2354 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits->packed_input);
2356 if (ps_args->vp_mode == vertexshader)
2357 declare_in_varying(gl_info, buffer, FALSE, "vec4 %s_link[%u];\n", prefix, in_count);
2358 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
2361 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
2363 if (!(map & 1))
2364 continue;
2366 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", i);
2368 if (reg_maps->luminanceparams & (1u << i))
2370 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", i);
2371 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", i);
2372 extra_constants_needed++;
2375 extra_constants_needed++;
2378 if (ps_args->srgb_correction)
2380 shader_addline(buffer, "const vec4 srgb_const0 = ");
2381 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
2382 shader_addline(buffer, ";\n");
2383 shader_addline(buffer, "const vec4 srgb_const1 = ");
2384 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
2385 shader_addline(buffer, ";\n");
2387 if (reg_maps->vpos || reg_maps->usesdsy)
2389 if (reg_maps->usesdsy || !gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2391 ++extra_constants_needed;
2392 shader_addline(buffer, "uniform vec4 ycorrection;\n");
2394 if (reg_maps->vpos)
2396 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
2398 if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
2399 shader_addline(buffer, "layout(%spixel_center_integer) in vec4 gl_FragCoord;\n",
2400 ps_args->render_offscreen ? "" : "origin_upper_left, ");
2401 else if (!ps_args->render_offscreen)
2402 shader_addline(buffer, "layout(origin_upper_left) in vec4 gl_FragCoord;\n");
2404 shader_addline(buffer, "vec4 vpos;\n");
2408 if (ps_args->alpha_test_func + 1 != WINED3D_CMP_ALWAYS)
2409 shader_addline(buffer, "uniform float alpha_test_ref;\n");
2411 if (!needs_legacy_glsl_syntax(gl_info))
2412 shader_addline(buffer, "out vec4 ps_out[%u];\n", gl_info->limits.buffers);
2414 if (shader->limits->constant_float + extra_constants_needed >= gl_info->limits.glsl_ps_float_constants)
2415 FIXME("Insufficient uniforms to run this shader.\n");
2418 /* Declare output register temporaries */
2419 if (shader->limits->packed_output)
2420 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits->packed_output);
2422 /* Declare temporary variables */
2423 if (reg_maps->temporary_count)
2425 for (i = 0; i < reg_maps->temporary_count; ++i)
2426 shader_addline(buffer, "vec4 R%u;\n", i);
2428 else
2430 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
2432 if (map & 1)
2433 shader_addline(buffer, "vec4 R%u;\n", i);
2437 /* Declare indexable temporary variables */
2438 LIST_FOR_EACH_ENTRY(idx_temp_reg, &reg_maps->indexable_temps, struct wined3d_shader_indexable_temp, entry)
2440 if (idx_temp_reg->component_count != 4)
2441 FIXME("Ignoring component count %u.\n", idx_temp_reg->component_count);
2442 shader_addline(buffer, "vec4 X%u[%u];\n", idx_temp_reg->register_idx, idx_temp_reg->register_size);
2445 /* Declare loop registers aLx */
2446 if (version->major < 4)
2448 for (i = 0; i < reg_maps->loop_depth; ++i)
2450 shader_addline(buffer, "int aL%u;\n", i);
2451 shader_addline(buffer, "int tmpInt%u;\n", i);
2455 /* Temporary variables for matrix operations */
2456 shader_addline(buffer, "vec4 tmp0;\n");
2457 shader_addline(buffer, "vec4 tmp1;\n");
2459 if (!shader->load_local_constsF)
2461 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
2463 shader_addline(buffer, "const vec4 %s_lc%u = ", prefix, lconst->idx);
2464 shader_glsl_append_imm_vec4(buffer, (const float *)lconst->value);
2465 shader_addline(buffer, ";\n");
2470 /*****************************************************************************
2471 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
2473 * For more information, see http://wiki.winehq.org/DirectX-Shaders
2474 ****************************************************************************/
2476 /* Prototypes */
2477 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
2478 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
2480 /** Used for opcode modifiers - They multiply the result by the specified amount */
2481 static const char * const shift_glsl_tab[] = {
2482 "", /* 0 (none) */
2483 "2.0 * ", /* 1 (x2) */
2484 "4.0 * ", /* 2 (x4) */
2485 "8.0 * ", /* 3 (x8) */
2486 "16.0 * ", /* 4 (x16) */
2487 "32.0 * ", /* 5 (x32) */
2488 "", /* 6 (x64) */
2489 "", /* 7 (x128) */
2490 "", /* 8 (d256) */
2491 "", /* 9 (d128) */
2492 "", /* 10 (d64) */
2493 "", /* 11 (d32) */
2494 "0.0625 * ", /* 12 (d16) */
2495 "0.125 * ", /* 13 (d8) */
2496 "0.25 * ", /* 14 (d4) */
2497 "0.5 * " /* 15 (d2) */
2500 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
2501 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
2502 const char *in_reg, const char *in_regswizzle, char *out_str)
2504 switch (src_modifier)
2506 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
2507 case WINED3DSPSM_DW:
2508 case WINED3DSPSM_NONE:
2509 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2510 break;
2511 case WINED3DSPSM_NEG:
2512 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
2513 break;
2514 case WINED3DSPSM_NOT:
2515 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
2516 break;
2517 case WINED3DSPSM_BIAS:
2518 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2519 break;
2520 case WINED3DSPSM_BIASNEG:
2521 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
2522 break;
2523 case WINED3DSPSM_SIGN:
2524 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2525 break;
2526 case WINED3DSPSM_SIGNNEG:
2527 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
2528 break;
2529 case WINED3DSPSM_COMP:
2530 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
2531 break;
2532 case WINED3DSPSM_X2:
2533 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
2534 break;
2535 case WINED3DSPSM_X2NEG:
2536 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
2537 break;
2538 case WINED3DSPSM_ABS:
2539 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
2540 break;
2541 case WINED3DSPSM_ABSNEG:
2542 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
2543 break;
2544 default:
2545 FIXME("Unhandled modifier %u\n", src_modifier);
2546 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
2550 /** Writes the GLSL variable name that corresponds to the register that the
2551 * DX opcode parameter is trying to access */
2552 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
2553 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
2555 /* oPos, oFog and oPts in D3D */
2556 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
2558 const struct wined3d_shader *shader = ins->ctx->shader;
2559 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
2560 const struct wined3d_shader_version *version = &reg_maps->shader_version;
2561 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2562 const char *prefix = shader_glsl_get_prefix(version->type);
2563 struct glsl_src_param rel_param0, rel_param1;
2564 char imm_str[4][17];
2566 if (reg->idx[0].offset != ~0U && reg->idx[0].rel_addr)
2567 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param0);
2568 if (reg->idx[1].offset != ~0U && reg->idx[1].rel_addr)
2569 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param1);
2570 *is_color = FALSE;
2572 switch (reg->type)
2574 case WINED3DSPR_TEMP:
2575 sprintf(register_name, "R%u", reg->idx[0].offset);
2576 break;
2578 case WINED3DSPR_INPUT:
2579 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
2581 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2583 if (reg->idx[0].rel_addr)
2584 FIXME("VS3+ input registers relative addressing.\n");
2585 if (priv->cur_vs_args->swizzle_map & (1u << reg->idx[0].offset))
2586 *is_color = TRUE;
2587 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
2588 break;
2591 if (version->type == WINED3D_SHADER_TYPE_GEOMETRY)
2593 if (reg->idx[0].rel_addr)
2595 if (reg->idx[1].rel_addr)
2596 sprintf(register_name, "gs_in[%s + %u]%s[%s + %u]",
2597 rel_param0.param_str, reg->idx[0].offset,
2598 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2599 rel_param1.param_str, reg->idx[1].offset);
2600 else
2601 sprintf(register_name, "gs_in[%s + %u]%s[%u]",
2602 rel_param0.param_str, reg->idx[0].offset,
2603 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2604 reg->idx[1].offset);
2606 else if (reg->idx[1].rel_addr)
2607 sprintf(register_name, "gs_in[%u]%s[%s + %u]", reg->idx[0].offset,
2608 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2609 rel_param1.param_str, reg->idx[1].offset);
2610 else
2611 sprintf(register_name, "gs_in[%u]%s[%u]", reg->idx[0].offset,
2612 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] ? "" : ".gs_in",
2613 reg->idx[1].offset);
2614 break;
2617 /* pixel shaders >= 3.0 */
2618 if (version->major >= 3)
2620 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
2621 unsigned int in_count = vec4_varyings(version->major, gl_info);
2623 if (reg->idx[0].rel_addr)
2625 /* Removing a + 0 would be an obvious optimization, but
2626 * OS X doesn't see the NOP operation there. */
2627 if (idx)
2629 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2630 && shader->u.ps.declared_in_count > in_count)
2632 sprintf(register_name,
2633 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
2634 rel_param0.param_str, idx, in_count - 1, rel_param0.param_str, idx, in_count,
2635 prefix, rel_param0.param_str, idx);
2637 else
2639 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param0.param_str, idx);
2642 else
2644 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
2645 && shader->u.ps.declared_in_count > in_count)
2647 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
2648 rel_param0.param_str, in_count - 1, rel_param0.param_str, in_count,
2649 prefix, rel_param0.param_str);
2651 else
2653 sprintf(register_name, "%s_in[%s]", prefix, rel_param0.param_str);
2657 else
2659 if (idx == in_count) sprintf(register_name, "gl_Color");
2660 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
2661 else sprintf(register_name, "%s_in[%u]", prefix, idx);
2664 else
2666 if (!reg->idx[0].offset)
2667 strcpy(register_name, "ffp_varying_diffuse");
2668 else
2669 strcpy(register_name, "ffp_varying_specular");
2670 break;
2672 break;
2674 case WINED3DSPR_CONST:
2676 /* Relative addressing */
2677 if (reg->idx[0].rel_addr)
2679 if (wined3d_settings.check_float_constants)
2680 sprintf(register_name, "(%s + %u >= 0 && %s + %u < %u ? %s_c[%s + %u] : vec4(0.0))",
2681 rel_param0.param_str, reg->idx[0].offset,
2682 rel_param0.param_str, reg->idx[0].offset, shader->limits->constant_float,
2683 prefix, rel_param0.param_str, reg->idx[0].offset);
2684 else if (reg->idx[0].offset)
2685 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2686 else
2687 sprintf(register_name, "%s_c[%s]", prefix, rel_param0.param_str);
2689 else
2691 if (shader_constant_is_local(shader, reg->idx[0].offset))
2692 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
2693 else
2694 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
2697 break;
2699 case WINED3DSPR_CONSTINT:
2700 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
2701 break;
2703 case WINED3DSPR_CONSTBOOL:
2704 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
2705 break;
2707 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
2708 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2709 sprintf(register_name, "T%u", reg->idx[0].offset);
2710 else
2711 sprintf(register_name, "A%u", reg->idx[0].offset);
2712 break;
2714 case WINED3DSPR_LOOP:
2715 sprintf(register_name, "aL%u", ins->ctx->state->current_loop_reg - 1);
2716 break;
2718 case WINED3DSPR_SAMPLER:
2719 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
2720 break;
2722 case WINED3DSPR_COLOROUT:
2723 if (reg->idx[0].offset >= gl_info->limits.buffers)
2724 WARN("Write to render target %u, only %d supported.\n",
2725 reg->idx[0].offset, gl_info->limits.buffers);
2727 sprintf(register_name, "%s[%u]", get_fragment_output(gl_info), reg->idx[0].offset);
2728 break;
2730 case WINED3DSPR_RASTOUT:
2731 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
2732 break;
2734 case WINED3DSPR_DEPTHOUT:
2735 sprintf(register_name, "gl_FragDepth");
2736 break;
2738 case WINED3DSPR_ATTROUT:
2739 if (!reg->idx[0].offset)
2740 sprintf(register_name, "%s_out[8]", prefix);
2741 else
2742 sprintf(register_name, "%s_out[9]", prefix);
2743 break;
2745 case WINED3DSPR_TEXCRDOUT:
2746 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
2747 if (reg->idx[0].rel_addr)
2748 FIXME("VS3 output registers relative addressing.\n");
2749 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
2750 break;
2752 case WINED3DSPR_MISCTYPE:
2753 if (!reg->idx[0].offset)
2755 /* vPos */
2756 sprintf(register_name, "vpos");
2758 else if (reg->idx[0].offset == 1)
2760 /* Note that gl_FrontFacing is a bool, while vFace is
2761 * a float for which the sign determines front/back */
2762 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
2764 else
2766 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
2767 sprintf(register_name, "unrecognized_register");
2769 break;
2771 case WINED3DSPR_IMMCONST:
2772 switch (reg->immconst_type)
2774 case WINED3D_IMMCONST_SCALAR:
2775 switch (reg->data_type)
2777 case WINED3D_DATA_FLOAT:
2778 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2779 sprintf(register_name, "uintBitsToFloat(%#xu)", reg->u.immconst_data[0]);
2780 else
2781 wined3d_ftoa(*(const float *)reg->u.immconst_data, register_name);
2782 break;
2783 case WINED3D_DATA_INT:
2784 sprintf(register_name, "%#x", reg->u.immconst_data[0]);
2785 break;
2786 case WINED3D_DATA_RESOURCE:
2787 case WINED3D_DATA_SAMPLER:
2788 case WINED3D_DATA_UINT:
2789 sprintf(register_name, "%#xu", reg->u.immconst_data[0]);
2790 break;
2791 default:
2792 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2793 break;
2795 break;
2797 case WINED3D_IMMCONST_VEC4:
2798 switch (reg->data_type)
2800 case WINED3D_DATA_FLOAT:
2801 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
2803 sprintf(register_name, "uintBitsToFloat(uvec4(%#xu, %#xu, %#xu, %#xu))",
2804 reg->u.immconst_data[0], reg->u.immconst_data[1],
2805 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2807 else
2809 wined3d_ftoa(*(const float *)&reg->u.immconst_data[0], imm_str[0]);
2810 wined3d_ftoa(*(const float *)&reg->u.immconst_data[1], imm_str[1]);
2811 wined3d_ftoa(*(const float *)&reg->u.immconst_data[2], imm_str[2]);
2812 wined3d_ftoa(*(const float *)&reg->u.immconst_data[3], imm_str[3]);
2813 sprintf(register_name, "vec4(%s, %s, %s, %s)",
2814 imm_str[0], imm_str[1], imm_str[2], imm_str[3]);
2816 break;
2817 case WINED3D_DATA_INT:
2818 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
2819 reg->u.immconst_data[0], reg->u.immconst_data[1],
2820 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2821 break;
2822 case WINED3D_DATA_RESOURCE:
2823 case WINED3D_DATA_SAMPLER:
2824 case WINED3D_DATA_UINT:
2825 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
2826 reg->u.immconst_data[0], reg->u.immconst_data[1],
2827 reg->u.immconst_data[2], reg->u.immconst_data[3]);
2828 break;
2829 default:
2830 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
2831 break;
2833 break;
2835 default:
2836 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
2837 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
2839 break;
2841 case WINED3DSPR_CONSTBUFFER:
2842 if (reg->idx[1].rel_addr)
2843 sprintf(register_name, "%s_cb%u[%s + %u]",
2844 prefix, reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2845 else
2846 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
2847 break;
2849 case WINED3DSPR_IMMCONSTBUFFER:
2850 if (reg->idx[0].rel_addr)
2851 sprintf(register_name, "%s_icb[%s + %u]", prefix, rel_param0.param_str, reg->idx[0].offset);
2852 else
2853 sprintf(register_name, "%s_icb[%u]", prefix, reg->idx[0].offset);
2854 break;
2856 case WINED3DSPR_PRIMID:
2857 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
2858 break;
2860 case WINED3DSPR_IDXTEMP:
2861 if (reg->idx[1].rel_addr)
2862 sprintf(register_name, "X%u[%s + %u]", reg->idx[0].offset, rel_param1.param_str, reg->idx[1].offset);
2863 else
2864 sprintf(register_name, "X%u[%u]", reg->idx[0].offset, reg->idx[1].offset);
2865 break;
2867 case WINED3DSPR_LOCALTHREADINDEX:
2868 sprintf(register_name, "int(gl_LocalInvocationIndex)");
2869 break;
2871 case WINED3DSPR_THREADID:
2872 sprintf(register_name, "ivec3(gl_GlobalInvocationID)");
2873 break;
2875 case WINED3DSPR_THREADGROUPID:
2876 sprintf(register_name, "ivec3(gl_WorkGroupID)");
2877 break;
2879 case WINED3DSPR_LOCALTHREADID:
2880 sprintf(register_name, "ivec3(gl_LocalInvocationID)");
2881 break;
2883 default:
2884 FIXME("Unhandled register type %#x.\n", reg->type);
2885 sprintf(register_name, "unrecognized_register");
2886 break;
2890 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
2892 *str++ = '.';
2893 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
2894 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
2895 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
2896 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
2897 *str = '\0';
2900 /* Get the GLSL write mask for the destination register */
2901 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
2903 DWORD mask = param->write_mask;
2905 if (shader_is_scalar(&param->reg))
2907 mask = WINED3DSP_WRITEMASK_0;
2908 *write_mask = '\0';
2910 else
2912 shader_glsl_write_mask_to_str(mask, write_mask);
2915 return mask;
2918 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
2919 unsigned int size = 0;
2921 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
2922 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
2923 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
2924 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
2926 return size;
2929 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
2931 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
2932 * but addressed as "rgba". To fix this we need to swap the register's x
2933 * and z components. */
2934 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
2936 *str++ = '.';
2937 /* swizzle bits fields: wwzzyyxx */
2938 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
2939 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
2940 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
2941 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
2942 *str = '\0';
2945 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
2946 BOOL fixup, DWORD mask, char *swizzle_str)
2948 if (shader_is_scalar(&param->reg))
2949 *swizzle_str = '\0';
2950 else
2951 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
2954 /* From a given parameter token, generate the corresponding GLSL string.
2955 * Also, return the actual register name and swizzle in case the
2956 * caller needs this information as well. */
2957 static void shader_glsl_add_src_param_ext(const struct wined3d_shader_instruction *ins,
2958 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src,
2959 enum wined3d_data_type data_type)
2961 BOOL is_color = FALSE;
2962 char swizzle_str[6];
2964 glsl_src->reg_name[0] = '\0';
2965 glsl_src->param_str[0] = '\0';
2966 swizzle_str[0] = '\0';
2968 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
2969 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
2971 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST
2972 || wined3d_src->reg.type == WINED3DSPR_PRIMID
2973 || wined3d_src->reg.type == WINED3DSPR_LOCALTHREADINDEX
2974 || wined3d_src->reg.type == WINED3DSPR_THREADID
2975 || wined3d_src->reg.type == WINED3DSPR_THREADGROUPID
2976 || wined3d_src->reg.type == WINED3DSPR_LOCALTHREADID)
2978 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
2980 else
2982 char reg_name[200];
2984 switch (data_type)
2986 case WINED3D_DATA_FLOAT:
2987 sprintf(reg_name, "%s", glsl_src->reg_name);
2988 break;
2989 case WINED3D_DATA_INT:
2990 sprintf(reg_name, "floatBitsToInt(%s)", glsl_src->reg_name);
2991 break;
2992 case WINED3D_DATA_RESOURCE:
2993 case WINED3D_DATA_SAMPLER:
2994 case WINED3D_DATA_UINT:
2995 sprintf(reg_name, "floatBitsToUint(%s)", glsl_src->reg_name);
2996 break;
2997 default:
2998 FIXME("Unhandled data type %#x.\n", data_type);
2999 sprintf(reg_name, "%s", glsl_src->reg_name);
3000 break;
3003 shader_glsl_gen_modifier(wined3d_src->modifiers, reg_name, swizzle_str, glsl_src->param_str);
3007 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
3008 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
3010 shader_glsl_add_src_param_ext(ins, wined3d_src, mask, glsl_src, wined3d_src->reg.data_type);
3013 /* From a given parameter token, generate the corresponding GLSL string.
3014 * Also, return the actual register name and swizzle in case the
3015 * caller needs this information as well. */
3016 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
3017 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
3019 BOOL is_color = FALSE;
3021 glsl_dst->mask_str[0] = '\0';
3022 glsl_dst->reg_name[0] = '\0';
3024 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
3025 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
3028 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3029 static DWORD shader_glsl_append_dst_ext(struct wined3d_string_buffer *buffer,
3030 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst,
3031 enum wined3d_data_type data_type)
3033 struct glsl_dst_param glsl_dst;
3034 DWORD mask;
3036 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
3038 switch (data_type)
3040 case WINED3D_DATA_FLOAT:
3041 shader_addline(buffer, "%s%s = %s(",
3042 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3043 break;
3044 case WINED3D_DATA_INT:
3045 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
3046 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3047 break;
3048 case WINED3D_DATA_RESOURCE:
3049 case WINED3D_DATA_SAMPLER:
3050 case WINED3D_DATA_UINT:
3051 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
3052 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3053 break;
3054 default:
3055 FIXME("Unhandled data type %#x.\n", data_type);
3056 shader_addline(buffer, "%s%s = %s(",
3057 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
3058 break;
3062 return mask;
3065 /* Append the destination part of the instruction to the buffer, return the effective write mask */
3066 static DWORD shader_glsl_append_dst(struct wined3d_string_buffer *buffer, const struct wined3d_shader_instruction *ins)
3068 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3071 /** Process GLSL instruction modifiers */
3072 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
3074 struct glsl_dst_param dst_param;
3075 DWORD modifiers;
3077 if (!ins->dst_count) return;
3079 modifiers = ins->dst[0].modifiers;
3080 if (!modifiers) return;
3082 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3084 if (modifiers & WINED3DSPDM_SATURATE)
3086 /* _SAT means to clamp the value of the register to between 0 and 1 */
3087 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
3088 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
3091 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
3093 FIXME("_centroid modifier not handled\n");
3096 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
3098 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
3102 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
3104 switch (op)
3106 case WINED3D_SHADER_REL_OP_GT: return ">";
3107 case WINED3D_SHADER_REL_OP_EQ: return "==";
3108 case WINED3D_SHADER_REL_OP_GE: return ">=";
3109 case WINED3D_SHADER_REL_OP_LT: return "<";
3110 case WINED3D_SHADER_REL_OP_NE: return "!=";
3111 case WINED3D_SHADER_REL_OP_LE: return "<=";
3112 default:
3113 FIXME("Unrecognized operator %#x.\n", op);
3114 return "(\?\?)";
3118 static BOOL shader_glsl_has_core_grad(const struct wined3d_gl_info *gl_info,
3119 const struct wined3d_shader_version *version)
3121 return shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4];
3124 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
3125 DWORD resource_idx, DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
3127 enum wined3d_shader_resource_type resource_type = ctx->reg_maps->resource_info[resource_idx].type;
3128 struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3129 const struct wined3d_gl_info *gl_info = ctx->gl_info;
3130 BOOL shadow = glsl_is_shadow_sampler(ctx->shader, priv->cur_ps_args, resource_idx, sampler_idx);
3131 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
3132 BOOL texrect = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
3133 && priv->cur_ps_args->np2_fixup & (1u << resource_idx)
3134 && gl_info->supported[ARB_TEXTURE_RECTANGLE];
3135 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
3136 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
3137 BOOL offset = flags & WINED3D_GLSL_SAMPLE_OFFSET;
3138 const char *base = "texture", *type_part = "", *suffix = "";
3139 unsigned int coord_size, deriv_size;
3140 BOOL array;
3142 sample_function->data_type = ctx->reg_maps->resource_info[resource_idx].data_type;
3144 if (resource_type >= ARRAY_SIZE(resource_type_info))
3146 ERR("Unexpected resource type %#x.\n", resource_type);
3147 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
3149 array = resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_1DARRAY
3150 || resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2DARRAY;
3152 /* Note that there's no such thing as a projected cube texture. */
3153 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
3154 projected = FALSE;
3156 if (needs_legacy_glsl_syntax(gl_info))
3158 if (shadow)
3159 base = "shadow";
3161 type_part = resource_type_info[resource_type].type_part;
3162 if (resource_type == WINED3D_SHADER_RESOURCE_TEXTURE_2D && texrect)
3163 type_part = "2DRect";
3164 if (!type_part[0])
3165 FIXME("Unhandled resource type %#x.\n", resource_type);
3167 if (!lod && grad && !shader_glsl_has_core_grad(gl_info, &ctx->shader->reg_maps.shader_version))
3169 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
3170 suffix = "ARB";
3171 else
3172 FIXME("Unsupported grad function.\n");
3176 if (flags & WINED3D_GLSL_SAMPLE_LOAD)
3178 static const DWORD texel_fetch_flags = WINED3D_GLSL_SAMPLE_LOAD | WINED3D_GLSL_SAMPLE_OFFSET;
3179 if (flags & ~texel_fetch_flags)
3180 ERR("Unexpected flags %#x for texelFetch.\n", flags & ~texel_fetch_flags);
3182 base = "texelFetch";
3183 type_part = "";
3186 sample_function->name = string_buffer_get(priv->string_buffers);
3187 string_buffer_sprintf(sample_function->name, "%s%s%s%s%s%s", base, type_part, projected ? "Proj" : "",
3188 lod ? "Lod" : grad ? "Grad" : "", offset ? "Offset" : "", suffix);
3190 coord_size = resource_type_info[resource_type].coord_size;
3191 deriv_size = coord_size;
3192 if (shadow)
3193 ++coord_size;
3194 if (array)
3195 --deriv_size;
3196 sample_function->offset_size = offset ? deriv_size : 0;
3197 sample_function->coord_mask = (1u << coord_size) - 1;
3198 sample_function->deriv_mask = (1u << deriv_size) - 1;
3199 sample_function->output_single_component = shadow && !needs_legacy_glsl_syntax(gl_info);
3202 static void shader_glsl_release_sample_function(const struct wined3d_shader_context *ctx,
3203 struct glsl_sample_function *sample_function)
3205 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
3207 string_buffer_release(priv->string_buffers, sample_function->name);
3210 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
3211 BOOL sign_fixup, enum fixup_channel_source channel_source)
3213 switch(channel_source)
3215 case CHANNEL_SOURCE_ZERO:
3216 strcat(arguments, "0.0");
3217 break;
3219 case CHANNEL_SOURCE_ONE:
3220 strcat(arguments, "1.0");
3221 break;
3223 case CHANNEL_SOURCE_X:
3224 strcat(arguments, reg_name);
3225 strcat(arguments, ".x");
3226 break;
3228 case CHANNEL_SOURCE_Y:
3229 strcat(arguments, reg_name);
3230 strcat(arguments, ".y");
3231 break;
3233 case CHANNEL_SOURCE_Z:
3234 strcat(arguments, reg_name);
3235 strcat(arguments, ".z");
3236 break;
3238 case CHANNEL_SOURCE_W:
3239 strcat(arguments, reg_name);
3240 strcat(arguments, ".w");
3241 break;
3243 default:
3244 FIXME("Unhandled channel source %#x\n", channel_source);
3245 strcat(arguments, "undefined");
3246 break;
3249 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
3252 static void shader_glsl_color_correction_ext(struct wined3d_string_buffer *buffer,
3253 const char *reg_name, DWORD mask, struct color_fixup_desc fixup)
3255 unsigned int mask_size, remaining;
3256 DWORD fixup_mask = 0;
3257 char arguments[256];
3258 char mask_str[6];
3260 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) fixup_mask |= WINED3DSP_WRITEMASK_0;
3261 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) fixup_mask |= WINED3DSP_WRITEMASK_1;
3262 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) fixup_mask |= WINED3DSP_WRITEMASK_2;
3263 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) fixup_mask |= WINED3DSP_WRITEMASK_3;
3264 if (!(mask &= fixup_mask))
3265 return;
3267 if (is_complex_fixup(fixup))
3269 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
3270 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
3271 return;
3274 shader_glsl_write_mask_to_str(mask, mask_str);
3275 mask_size = shader_glsl_get_write_mask_size(mask);
3277 arguments[0] = '\0';
3278 remaining = mask_size;
3279 if (mask & WINED3DSP_WRITEMASK_0)
3281 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.x_sign_fixup, fixup.x_source);
3282 if (--remaining) strcat(arguments, ", ");
3284 if (mask & WINED3DSP_WRITEMASK_1)
3286 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.y_sign_fixup, fixup.y_source);
3287 if (--remaining) strcat(arguments, ", ");
3289 if (mask & WINED3DSP_WRITEMASK_2)
3291 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.z_sign_fixup, fixup.z_source);
3292 if (--remaining) strcat(arguments, ", ");
3294 if (mask & WINED3DSP_WRITEMASK_3)
3296 shader_glsl_append_fixup_arg(arguments, reg_name, fixup.w_sign_fixup, fixup.w_source);
3297 if (--remaining) strcat(arguments, ", ");
3300 if (mask_size > 1)
3301 shader_addline(buffer, "%s%s = vec%u(%s);\n", reg_name, mask_str, mask_size, arguments);
3302 else
3303 shader_addline(buffer, "%s%s = %s;\n", reg_name, mask_str, arguments);
3306 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
3308 char reg_name[256];
3309 BOOL is_color;
3311 shader_glsl_get_register_name(&ins->dst[0].reg, reg_name, &is_color, ins);
3312 shader_glsl_color_correction_ext(ins->ctx->buffer, reg_name, ins->dst[0].write_mask, fixup);
3315 static void PRINTF_ATTR(9, 10) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
3316 unsigned int sampler_bind_idx, const struct glsl_sample_function *sample_function, DWORD swizzle,
3317 const char *dx, const char *dy, const char *bias, const struct wined3d_shader_texel_offset *offset,
3318 const char *coord_reg_fmt, ...)
3320 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
3321 char dst_swizzle[6];
3322 struct color_fixup_desc fixup;
3323 BOOL np2_fixup = FALSE;
3324 va_list args;
3325 int ret;
3327 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
3329 /* If ARB_texture_swizzle is supported we don't need to do anything here.
3330 * We actually rely on it for vertex shaders and SM4+. */
3331 if (version->type == WINED3D_SHADER_TYPE_PIXEL && version->major < 4)
3333 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3334 fixup = priv->cur_ps_args->color_fixup[sampler_bind_idx];
3336 if (priv->cur_ps_args->np2_fixup & (1u << sampler_bind_idx))
3337 np2_fixup = TRUE;
3339 else
3341 fixup = COLOR_FIXUP_IDENTITY;
3344 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], sample_function->data_type);
3346 if (sample_function->output_single_component)
3347 shader_addline(ins->ctx->buffer, "vec4(");
3349 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
3350 sample_function->name->buffer, shader_glsl_get_prefix(version->type), sampler_bind_idx);
3352 for (;;)
3354 va_start(args, coord_reg_fmt);
3355 ret = shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
3356 va_end(args);
3357 if (!ret)
3358 break;
3359 if (!string_buffer_resize(ins->ctx->buffer, ret))
3360 break;
3363 if (np2_fixup)
3365 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3366 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler_bind_idx];
3368 switch (shader_glsl_get_write_mask_size(sample_function->coord_mask))
3370 case 1:
3371 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3372 idx >> 1, (idx % 2) ? "z" : "x");
3373 break;
3374 case 2:
3375 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s",
3376 idx >> 1, (idx % 2) ? "zw" : "xy");
3377 break;
3378 case 3:
3379 shader_addline(ins->ctx->buffer, " * vec3(ps_samplerNP2Fixup[%u].%s, 1.0)",
3380 idx >> 1, (idx % 2) ? "zw" : "xy");
3381 break;
3382 case 4:
3383 shader_addline(ins->ctx->buffer, " * vec4(ps_samplerNP2Fixup[%u].%s, 1.0, 1.0)",
3384 idx >> 1, (idx % 2) ? "zw" : "xy");
3385 break;
3388 if (dx && dy)
3389 shader_addline(ins->ctx->buffer, ", %s, %s", dx, dy);
3390 else if (bias)
3391 shader_addline(ins->ctx->buffer, ", %s", bias);
3392 if (sample_function->offset_size)
3394 int offset_immdata[4] = {offset->u, offset->v, offset->w};
3395 shader_addline(ins->ctx->buffer, ", ");
3396 shader_glsl_append_imm_ivec(ins->ctx->buffer, offset_immdata, sample_function->offset_size);
3398 shader_addline(ins->ctx->buffer, ")");
3400 if (sample_function->output_single_component)
3401 shader_addline(ins->ctx->buffer, ")");
3403 shader_addline(ins->ctx->buffer, "%s);\n", dst_swizzle);
3405 if (!is_identity_fixup(fixup))
3406 shader_glsl_color_correction(ins, fixup);
3409 static void shader_glsl_fixup_position(struct wined3d_string_buffer *buffer)
3411 /* Write the final position.
3413 * OpenGL coordinates specify the center of the pixel while D3D coords
3414 * specify the corner. The offsets are stored in z and w in
3415 * pos_fixup. pos_fixup.y contains 1.0 or -1.0 to turn the rendering
3416 * upside down for offscreen rendering. pos_fixup.x contains 1.0 to allow
3417 * a MAD. */
3418 shader_addline(buffer, "gl_Position.y = gl_Position.y * pos_fixup.y;\n");
3419 shader_addline(buffer, "gl_Position.xy += pos_fixup.zw * gl_Position.ww;\n");
3421 /* Z coord [0;1]->[-1;1] mapping, see comment in get_projection_matrix()
3422 * in utils.c
3424 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However,
3425 * shaders are run before the homogeneous divide, so we have to take the w
3426 * into account: z = ((z / w) * 2 - 1) * w, which is the same as
3427 * z = z * 2 - w. */
3428 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3431 /*****************************************************************************
3432 * Begin processing individual instruction opcodes
3433 ****************************************************************************/
3435 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
3437 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3438 struct glsl_src_param src0_param;
3439 struct glsl_src_param src1_param;
3440 DWORD write_mask;
3441 const char *op;
3443 /* Determine the GLSL operator to use based on the opcode */
3444 switch (ins->handler_idx)
3446 case WINED3DSIH_ADD: op = "+"; break;
3447 case WINED3DSIH_AND: op = "&"; break;
3448 case WINED3DSIH_DIV: op = "/"; break;
3449 case WINED3DSIH_IADD: op = "+"; break;
3450 case WINED3DSIH_ISHL: op = "<<"; break;
3451 case WINED3DSIH_ISHR: op = ">>"; break;
3452 case WINED3DSIH_MUL: op = "*"; break;
3453 case WINED3DSIH_OR: op = "|"; break;
3454 case WINED3DSIH_SUB: op = "-"; break;
3455 case WINED3DSIH_USHR: op = ">>"; break;
3456 case WINED3DSIH_XOR: op = "^"; break;
3457 default:
3458 op = "<unhandled operator>";
3459 FIXME("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3460 break;
3463 write_mask = shader_glsl_append_dst(buffer, ins);
3464 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3465 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3466 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
3469 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
3471 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3472 struct glsl_src_param src0_param;
3473 struct glsl_src_param src1_param;
3474 unsigned int mask_size;
3475 DWORD write_mask;
3476 const char *op;
3478 write_mask = shader_glsl_append_dst(buffer, ins);
3479 mask_size = shader_glsl_get_write_mask_size(write_mask);
3480 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3481 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3483 if (mask_size > 1)
3485 switch (ins->handler_idx)
3487 case WINED3DSIH_EQ: op = "equal"; break;
3488 case WINED3DSIH_IEQ: op = "equal"; break;
3489 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
3490 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
3491 case WINED3DSIH_UGE: op = "greaterThanEqual"; break;
3492 case WINED3DSIH_LT: op = "lessThan"; break;
3493 case WINED3DSIH_ILT: op = "lessThan"; break;
3494 case WINED3DSIH_ULT: op = "lessThan"; break;
3495 case WINED3DSIH_NE: op = "notEqual"; break;
3496 case WINED3DSIH_INE: op = "notEqual"; break;
3497 default:
3498 op = "<unhandled operator>";
3499 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3500 break;
3503 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
3504 mask_size, op, src0_param.param_str, src1_param.param_str);
3506 else
3508 switch (ins->handler_idx)
3510 case WINED3DSIH_EQ: op = "=="; break;
3511 case WINED3DSIH_IEQ: op = "=="; break;
3512 case WINED3DSIH_GE: op = ">="; break;
3513 case WINED3DSIH_IGE: op = ">="; break;
3514 case WINED3DSIH_UGE: op = ">="; break;
3515 case WINED3DSIH_LT: op = "<"; break;
3516 case WINED3DSIH_ILT: op = "<"; break;
3517 case WINED3DSIH_ULT: op = "<"; break;
3518 case WINED3DSIH_NE: op = "!="; break;
3519 case WINED3DSIH_INE: op = "!="; break;
3520 default:
3521 op = "<unhandled operator>";
3522 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3523 break;
3526 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
3527 src0_param.param_str, op, src1_param.param_str);
3531 static void shader_glsl_unary_op(const struct wined3d_shader_instruction *ins)
3533 struct glsl_src_param src_param;
3534 DWORD write_mask;
3535 const char *op;
3537 switch (ins->handler_idx)
3539 case WINED3DSIH_INEG: op = "-"; break;
3540 case WINED3DSIH_NOT: op = "~"; break;
3541 default:
3542 op = "<unhandled operator>";
3543 ERR("Unhandled opcode %s.\n",
3544 debug_d3dshaderinstructionhandler(ins->handler_idx));
3545 break;
3548 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3549 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3550 shader_addline(ins->ctx->buffer, "%s%s);\n", op, src_param.param_str);
3553 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
3555 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3556 struct glsl_src_param src0_param;
3557 struct glsl_src_param src1_param;
3558 DWORD write_mask;
3560 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
3561 * not, we can emulate it. */
3562 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3563 FIXME("64-bit integer multiplies not implemented.\n");
3565 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3567 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3568 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3569 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3571 shader_addline(ins->ctx->buffer, "%s * %s);\n",
3572 src0_param.param_str, src1_param.param_str);
3576 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
3578 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3579 struct glsl_src_param src0_param, src1_param;
3580 DWORD write_mask;
3582 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3584 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3586 char dst_mask[6];
3588 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3589 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3590 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3591 shader_addline(buffer, "tmp0%s = uintBitsToFloat(%s / %s);\n",
3592 dst_mask, src0_param.param_str, src1_param.param_str);
3594 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3595 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3596 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3597 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3599 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], WINED3D_DATA_FLOAT);
3600 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3602 else
3604 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
3605 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3606 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3607 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
3610 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3612 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
3613 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3614 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
3615 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
3619 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
3620 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
3622 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3623 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3624 struct glsl_src_param src0_param;
3625 DWORD write_mask;
3627 write_mask = shader_glsl_append_dst(buffer, ins);
3628 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3630 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
3631 * shader versions WINED3DSIO_MOVA is used for this. */
3632 if (ins->ctx->reg_maps->shader_version.major == 1
3633 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
3634 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
3636 /* This is a simple floor() */
3637 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3638 if (mask_size > 1) {
3639 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
3640 } else {
3641 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
3644 else if (ins->handler_idx == WINED3DSIH_MOVA)
3646 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
3647 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3649 if (shader_glsl_get_version(gl_info, version) >= 130 || gl_info->supported[EXT_GPU_SHADER4])
3651 if (mask_size > 1)
3652 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
3653 else
3654 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
3656 else
3658 if (mask_size > 1)
3659 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
3660 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
3661 else
3662 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
3663 src0_param.param_str, src0_param.param_str);
3666 else
3668 shader_addline(buffer, "%s);\n", src0_param.param_str);
3672 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
3673 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
3675 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3676 struct glsl_src_param src0_param;
3677 struct glsl_src_param src1_param;
3678 DWORD dst_write_mask, src_write_mask;
3679 unsigned int dst_size;
3681 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3682 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3684 /* dp4 works on vec4, dp3 on vec3, etc. */
3685 if (ins->handler_idx == WINED3DSIH_DP4)
3686 src_write_mask = WINED3DSP_WRITEMASK_ALL;
3687 else if (ins->handler_idx == WINED3DSIH_DP3)
3688 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3689 else
3690 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
3692 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
3693 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
3695 if (dst_size > 1) {
3696 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
3697 } else {
3698 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
3702 /* Note that this instruction has some restrictions. The destination write mask
3703 * can't contain the w component, and the source swizzles have to be .xyzw */
3704 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
3706 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3707 struct glsl_src_param src0_param;
3708 struct glsl_src_param src1_param;
3709 char dst_mask[6];
3711 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3712 shader_glsl_append_dst(ins->ctx->buffer, ins);
3713 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3714 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3715 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
3718 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
3720 unsigned int stream = ins->handler_idx == WINED3DSIH_CUT ? 0 : ins->src[0].reg.idx[0].offset;
3722 if (!stream)
3723 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
3724 else
3725 FIXME("Unhandled primitive stream %u.\n", stream);
3728 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
3729 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
3730 * GLSL uses the value as-is. */
3731 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
3733 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3734 struct glsl_src_param src0_param;
3735 struct glsl_src_param src1_param;
3736 DWORD dst_write_mask;
3737 unsigned int dst_size;
3739 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3740 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3742 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3743 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3745 if (dst_size > 1)
3747 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
3748 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
3750 else
3752 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
3753 src1_param.param_str, src0_param.param_str, src1_param.param_str);
3757 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
3758 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
3760 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3761 struct glsl_src_param src_param;
3762 const char *instruction;
3763 DWORD write_mask;
3764 unsigned i;
3766 /* Determine the GLSL function to use based on the opcode */
3767 /* TODO: Possibly make this a table for faster lookups */
3768 switch (ins->handler_idx)
3770 case WINED3DSIH_ABS: instruction = "abs"; break;
3771 case WINED3DSIH_BFREV: instruction = "bitfieldReverse"; break;
3772 case WINED3DSIH_COUNTBITS: instruction = "bitCount"; break;
3773 case WINED3DSIH_DSX: instruction = "dFdx"; break;
3774 case WINED3DSIH_DSX_COARSE: instruction = "dFdxCoarse"; break;
3775 case WINED3DSIH_DSX_FINE: instruction = "dFdxFine"; break;
3776 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
3777 case WINED3DSIH_DSY_COARSE: instruction = "ycorrection.y * dFdyCoarse"; break;
3778 case WINED3DSIH_DSY_FINE: instruction = "ycorrection.y * dFdyFine"; break;
3779 case WINED3DSIH_FIRSTBIT_HI: instruction = "findMSB"; break;
3780 case WINED3DSIH_FIRSTBIT_LO: instruction = "findLSB"; break;
3781 case WINED3DSIH_FIRSTBIT_SHI: instruction = "findMSB"; break;
3782 case WINED3DSIH_FRC: instruction = "fract"; break;
3783 case WINED3DSIH_IMAX: instruction = "max"; break;
3784 case WINED3DSIH_IMIN: instruction = "min"; break;
3785 case WINED3DSIH_MAX: instruction = "max"; break;
3786 case WINED3DSIH_MIN: instruction = "min"; break;
3787 case WINED3DSIH_ROUND_NE: instruction = "roundEven"; break;
3788 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
3789 case WINED3DSIH_ROUND_PI: instruction = "ceil"; break;
3790 case WINED3DSIH_ROUND_Z: instruction = "trunc"; break;
3791 case WINED3DSIH_SQRT: instruction = "sqrt"; break;
3792 case WINED3DSIH_UMAX: instruction = "max"; break;
3793 case WINED3DSIH_UMIN: instruction = "min"; break;
3794 default: instruction = "";
3795 ERR("Opcode %s not yet handled in GLSL.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
3796 break;
3799 write_mask = shader_glsl_append_dst(buffer, ins);
3801 /* In D3D bits are numbered from the most significant bit. */
3802 if (ins->handler_idx == WINED3DSIH_FIRSTBIT_HI || ins->handler_idx == WINED3DSIH_FIRSTBIT_SHI)
3803 shader_addline(buffer, "31 - ");
3804 shader_addline(buffer, "%s(", instruction);
3806 if (ins->src_count)
3808 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3809 shader_addline(buffer, "%s", src_param.param_str);
3810 for (i = 1; i < ins->src_count; ++i)
3812 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
3813 shader_addline(buffer, ", %s", src_param.param_str);
3817 shader_addline(buffer, "));\n");
3820 static void shader_glsl_float16(const struct wined3d_shader_instruction *ins)
3822 struct wined3d_shader_dst_param dst;
3823 struct glsl_src_param src;
3824 DWORD write_mask;
3825 const char *fmt;
3826 unsigned int i;
3828 fmt = ins->handler_idx == WINED3DSIH_F16TOF32
3829 ? "unpackHalf2x16(%s).x);\n" : "packHalf2x16(vec2(%s, 0.0)));\n";
3831 dst = ins->dst[0];
3832 for (i = 0; i < 4; ++i)
3834 write_mask = WINED3DSP_WRITEMASK_0 << i;
3835 dst.write_mask = ins->dst[0].write_mask & write_mask;
3837 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3838 &dst, dst.reg.data_type)))
3839 continue;
3841 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src);
3842 shader_addline(ins->ctx->buffer, fmt, src.param_str);
3846 static void shader_glsl_bitwise_op(const struct wined3d_shader_instruction *ins)
3848 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3849 struct wined3d_shader_dst_param dst;
3850 struct glsl_src_param src[4];
3851 const char *instruction;
3852 unsigned int i, j;
3853 DWORD write_mask;
3855 switch (ins->handler_idx)
3857 case WINED3DSIH_BFI: instruction = "bitfieldInsert"; break;
3858 case WINED3DSIH_UBFE: instruction = "bitfieldExtract"; break;
3859 default:
3860 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
3861 return;
3864 dst = ins->dst[0];
3865 for (i = 0; i < 4; ++i)
3867 write_mask = WINED3DSP_WRITEMASK_0 << i;
3868 dst.write_mask = ins->dst[0].write_mask & write_mask;
3870 if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins,
3871 &dst, dst.reg.data_type)))
3872 continue;
3874 for (j = 0; j < ins->src_count; ++j)
3875 shader_glsl_add_src_param(ins, &ins->src[j], write_mask, &src[j]);
3876 shader_addline(buffer, "%s(", instruction);
3877 for (j = 0; j < ins->src_count - 2; ++j)
3878 shader_addline(buffer, "%s, ", src[ins->src_count - j - 1].param_str);
3879 shader_addline(buffer, "%s & 0x1f, %s & 0x1f));\n", src[1].param_str, src[0].param_str);
3883 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
3885 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
3887 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3888 struct glsl_src_param src_param;
3889 unsigned int mask_size;
3890 DWORD write_mask;
3891 char dst_mask[6];
3893 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
3894 mask_size = shader_glsl_get_write_mask_size(write_mask);
3895 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
3897 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
3898 src_param.param_str, src_param.param_str);
3899 shader_glsl_append_dst(buffer, ins);
3901 if (mask_size > 1)
3903 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
3904 mask_size, src_param.param_str);
3906 else
3908 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
3909 src_param.param_str);
3913 static void shader_glsl_scalar_op(const struct wined3d_shader_instruction *ins)
3915 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3916 ins->ctx->reg_maps->shader_version.minor);
3917 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
3918 struct glsl_src_param src0_param;
3919 const char *prefix, *suffix;
3920 unsigned int dst_size;
3921 DWORD dst_write_mask;
3923 dst_write_mask = shader_glsl_append_dst(buffer, ins);
3924 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
3926 if (shader_version < WINED3D_SHADER_VERSION(4, 0))
3927 dst_write_mask = WINED3DSP_WRITEMASK_3;
3929 shader_glsl_add_src_param(ins, &ins->src[0], dst_write_mask, &src0_param);
3931 switch (ins->handler_idx)
3933 case WINED3DSIH_EXP:
3934 case WINED3DSIH_EXPP:
3935 prefix = "exp2(";
3936 suffix = ")";
3937 break;
3939 case WINED3DSIH_LOG:
3940 case WINED3DSIH_LOGP:
3941 prefix = "log2(abs(";
3942 suffix = "))";
3943 break;
3945 case WINED3DSIH_RCP:
3946 prefix = "1.0 / ";
3947 suffix = "";
3948 break;
3950 case WINED3DSIH_RSQ:
3951 prefix = "inversesqrt(abs(";
3952 suffix = "))";
3953 break;
3955 default:
3956 prefix = "";
3957 suffix = "";
3958 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
3959 break;
3962 if (dst_size > 1 && shader_version < WINED3D_SHADER_VERSION(4, 0))
3963 shader_addline(buffer, "vec%u(%s%s%s));\n", dst_size, prefix, src0_param.param_str, suffix);
3964 else
3965 shader_addline(buffer, "%s%s%s);\n", prefix, src0_param.param_str, suffix);
3968 /** Process the WINED3DSIO_EXPP instruction in GLSL:
3969 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
3970 * dst.x = 2^(floor(src))
3971 * dst.y = src - floor(src)
3972 * dst.z = 2^src (partial precision is allowed, but optional)
3973 * dst.w = 1.0;
3974 * For 2.0 shaders, just do this (honoring writemask and swizzle):
3975 * dst = 2^src; (partial precision is allowed, but optional)
3977 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
3979 if (ins->ctx->reg_maps->shader_version.major < 2)
3981 struct glsl_src_param src_param;
3982 char dst_mask[6];
3984 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
3986 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
3987 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
3988 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
3989 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
3991 shader_glsl_append_dst(ins->ctx->buffer, ins);
3992 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3993 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
3994 return;
3997 shader_glsl_scalar_op(ins);
4000 static void shader_glsl_cast(const struct wined3d_shader_instruction *ins,
4001 const char *vector_constructor, const char *scalar_constructor)
4003 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4004 struct glsl_src_param src_param;
4005 unsigned int mask_size;
4006 DWORD write_mask;
4008 write_mask = shader_glsl_append_dst(buffer, ins);
4009 mask_size = shader_glsl_get_write_mask_size(write_mask);
4010 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
4012 if (mask_size > 1)
4013 shader_addline(buffer, "%s%u(%s));\n", vector_constructor, mask_size, src_param.param_str);
4014 else
4015 shader_addline(buffer, "%s(%s));\n", scalar_constructor, src_param.param_str);
4018 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
4020 shader_glsl_cast(ins, "ivec", "int");
4023 static void shader_glsl_to_uint(const struct wined3d_shader_instruction *ins)
4025 shader_glsl_cast(ins, "uvec", "uint");
4028 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
4030 shader_glsl_cast(ins, "vec", "float");
4033 /** Process signed comparison opcodes in GLSL. */
4034 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
4036 struct glsl_src_param src0_param;
4037 struct glsl_src_param src1_param;
4038 DWORD write_mask;
4039 unsigned int mask_size;
4041 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4042 mask_size = shader_glsl_get_write_mask_size(write_mask);
4043 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4044 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4046 if (mask_size > 1) {
4047 const char *compare;
4049 switch(ins->handler_idx)
4051 case WINED3DSIH_SLT: compare = "lessThan"; break;
4052 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
4053 default: compare = "";
4054 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4057 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
4058 src0_param.param_str, src1_param.param_str);
4059 } else {
4060 switch(ins->handler_idx)
4062 case WINED3DSIH_SLT:
4063 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
4064 * to return 0.0 but step returns 1.0 because step is not < x
4065 * An alternative is a bvec compare padded with an unused second component.
4066 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
4067 * issue. Playing with not() is not possible either because not() does not accept
4068 * a scalar.
4070 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
4071 src0_param.param_str, src1_param.param_str);
4072 break;
4073 case WINED3DSIH_SGE:
4074 /* Here we can use the step() function and safe a conditional */
4075 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
4076 break;
4077 default:
4078 FIXME("Can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
4084 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
4086 const char *condition_prefix, *condition_suffix;
4087 struct wined3d_shader_dst_param dst;
4088 struct glsl_src_param src0_param;
4089 struct glsl_src_param src1_param;
4090 struct glsl_src_param src2_param;
4091 BOOL temp_destination = FALSE;
4092 DWORD cmp_channel = 0;
4093 unsigned int i, j;
4094 char mask_char[6];
4095 DWORD write_mask;
4097 switch (ins->handler_idx)
4099 case WINED3DSIH_CMP:
4100 condition_prefix = "";
4101 condition_suffix = " >= 0.0";
4102 break;
4104 case WINED3DSIH_CND:
4105 condition_prefix = "";
4106 condition_suffix = " > 0.5";
4107 break;
4109 case WINED3DSIH_MOVC:
4110 condition_prefix = "bool(";
4111 condition_suffix = ")";
4112 break;
4114 default:
4115 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
4116 condition_prefix = "<unhandled prefix>";
4117 condition_suffix = "<unhandled suffix>";
4118 break;
4121 if (shader_is_scalar(&ins->dst[0].reg) || shader_is_scalar(&ins->src[0].reg))
4123 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4124 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4125 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4126 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4128 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4129 condition_prefix, src0_param.param_str, condition_suffix,
4130 src1_param.param_str, src2_param.param_str);
4131 return;
4134 dst = ins->dst[0];
4136 /* Splitting the instruction up in multiple lines imposes a problem:
4137 * The first lines may overwrite source parameters of the following lines.
4138 * Deal with that by using a temporary destination register if needed. */
4139 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
4140 && ins->src[0].reg.type == dst.reg.type)
4141 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
4142 && ins->src[1].reg.type == dst.reg.type)
4143 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
4144 && ins->src[2].reg.type == dst.reg.type))
4145 temp_destination = TRUE;
4147 /* Cycle through all source0 channels. */
4148 for (i = 0; i < 4; ++i)
4150 write_mask = 0;
4151 /* Find the destination channels which use the current source0 channel. */
4152 for (j = 0; j < 4; ++j)
4154 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
4156 write_mask |= WINED3DSP_WRITEMASK_0 << j;
4157 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
4160 dst.write_mask = ins->dst[0].write_mask & write_mask;
4162 if (temp_destination)
4164 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
4165 continue;
4166 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
4168 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst, dst.reg.data_type)))
4169 continue;
4171 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
4172 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4173 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4175 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
4176 condition_prefix, src0_param.param_str, condition_suffix,
4177 src1_param.param_str, src2_param.param_str);
4180 if (temp_destination)
4182 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
4183 shader_glsl_append_dst(ins->ctx->buffer, ins);
4184 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
4188 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
4189 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
4190 * the compare is done per component of src0. */
4191 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
4193 struct glsl_src_param src0_param;
4194 struct glsl_src_param src1_param;
4195 struct glsl_src_param src2_param;
4196 DWORD write_mask;
4197 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4198 ins->ctx->reg_maps->shader_version.minor);
4200 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
4202 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4203 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4204 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4205 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4207 if (ins->coissue && ins->dst->write_mask != WINED3DSP_WRITEMASK_3)
4208 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
4209 else
4210 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
4211 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4212 return;
4215 shader_glsl_conditional_move(ins);
4218 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
4219 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
4221 struct glsl_src_param src0_param;
4222 struct glsl_src_param src1_param;
4223 struct glsl_src_param src2_param;
4224 DWORD write_mask;
4226 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4227 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4228 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4229 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4230 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
4231 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4234 /* Handles transforming all WINED3DSIO_M?x? opcodes for
4235 Vertex shaders to GLSL codes */
4236 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
4238 int i;
4239 int nComponents = 0;
4240 struct wined3d_shader_dst_param tmp_dst = {{0}};
4241 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
4242 struct wined3d_shader_instruction tmp_ins;
4244 memset(&tmp_ins, 0, sizeof(tmp_ins));
4246 /* Set constants for the temporary argument */
4247 tmp_ins.ctx = ins->ctx;
4248 tmp_ins.dst_count = 1;
4249 tmp_ins.dst = &tmp_dst;
4250 tmp_ins.src_count = 2;
4251 tmp_ins.src = tmp_src;
4253 switch(ins->handler_idx)
4255 case WINED3DSIH_M4x4:
4256 nComponents = 4;
4257 tmp_ins.handler_idx = WINED3DSIH_DP4;
4258 break;
4259 case WINED3DSIH_M4x3:
4260 nComponents = 3;
4261 tmp_ins.handler_idx = WINED3DSIH_DP4;
4262 break;
4263 case WINED3DSIH_M3x4:
4264 nComponents = 4;
4265 tmp_ins.handler_idx = WINED3DSIH_DP3;
4266 break;
4267 case WINED3DSIH_M3x3:
4268 nComponents = 3;
4269 tmp_ins.handler_idx = WINED3DSIH_DP3;
4270 break;
4271 case WINED3DSIH_M3x2:
4272 nComponents = 2;
4273 tmp_ins.handler_idx = WINED3DSIH_DP3;
4274 break;
4275 default:
4276 break;
4279 tmp_dst = ins->dst[0];
4280 tmp_src[0] = ins->src[0];
4281 tmp_src[1] = ins->src[1];
4282 for (i = 0; i < nComponents; ++i)
4284 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
4285 shader_glsl_dot(&tmp_ins);
4286 ++tmp_src[1].reg.idx[0].offset;
4291 The LRP instruction performs a component-wise linear interpolation
4292 between the second and third operands using the first operand as the
4293 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
4294 This is equivalent to mix(src2, src1, src0);
4296 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
4298 struct glsl_src_param src0_param;
4299 struct glsl_src_param src1_param;
4300 struct glsl_src_param src2_param;
4301 DWORD write_mask;
4303 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4305 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4306 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
4307 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
4309 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
4310 src2_param.param_str, src1_param.param_str, src0_param.param_str);
4313 /** Process the WINED3DSIO_LIT instruction in GLSL:
4314 * dst.x = dst.w = 1.0
4315 * dst.y = (src0.x > 0) ? src0.x
4316 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
4317 * where src.w is clamped at +- 128
4319 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
4321 struct glsl_src_param src0_param;
4322 struct glsl_src_param src1_param;
4323 struct glsl_src_param src3_param;
4324 char dst_mask[6];
4326 shader_glsl_append_dst(ins->ctx->buffer, ins);
4327 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4329 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4330 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
4331 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
4333 /* The sdk specifies the instruction like this
4334 * dst.x = 1.0;
4335 * if(src.x > 0.0) dst.y = src.x
4336 * else dst.y = 0.0.
4337 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
4338 * else dst.z = 0.0;
4339 * dst.w = 1.0;
4340 * (where power = src.w clamped between -128 and 128)
4342 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
4343 * dst.x = 1.0 ... No further explanation needed
4344 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
4345 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
4346 * dst.w = 1.0. ... Nothing fancy.
4348 * So we still have one conditional in there. So do this:
4349 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
4351 * 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),
4352 * 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.
4353 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
4355 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
4356 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
4357 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
4359 shader_addline(ins->ctx->buffer,
4360 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
4361 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
4362 src0_param.param_str, src3_param.param_str, src1_param.param_str,
4363 src0_param.param_str, src3_param.param_str, dst_mask);
4366 /** Process the WINED3DSIO_DST instruction in GLSL:
4367 * dst.x = 1.0
4368 * dst.y = src0.x * src0.y
4369 * dst.z = src0.z
4370 * dst.w = src1.w
4372 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
4374 struct glsl_src_param src0y_param;
4375 struct glsl_src_param src0z_param;
4376 struct glsl_src_param src1y_param;
4377 struct glsl_src_param src1w_param;
4378 char dst_mask[6];
4380 shader_glsl_append_dst(ins->ctx->buffer, ins);
4381 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4383 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
4384 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
4385 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
4386 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
4388 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
4389 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
4392 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
4393 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
4394 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
4396 * dst.x = cos(src0.?)
4397 * dst.y = sin(src0.?)
4398 * dst.z = dst.z
4399 * dst.w = dst.w
4401 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
4403 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4404 struct glsl_src_param src0_param;
4405 DWORD write_mask;
4407 if (ins->ctx->reg_maps->shader_version.major < 4)
4409 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4411 write_mask = shader_glsl_append_dst(buffer, ins);
4412 switch (write_mask)
4414 case WINED3DSP_WRITEMASK_0:
4415 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4416 break;
4418 case WINED3DSP_WRITEMASK_1:
4419 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4420 break;
4422 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
4423 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
4424 src0_param.param_str, src0_param.param_str);
4425 break;
4427 default:
4428 ERR("Write mask should be .x, .y or .xy\n");
4429 break;
4432 return;
4435 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
4438 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4440 char dst_mask[6];
4442 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
4443 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4444 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
4446 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4447 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4448 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4450 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4451 shader_addline(buffer, "tmp0%s);\n", dst_mask);
4453 else
4455 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0], ins->dst[0].reg.data_type);
4456 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4457 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
4460 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
4462 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1], ins->dst[1].reg.data_type);
4463 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4464 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
4468 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
4469 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
4470 * generate invalid code
4472 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
4474 struct glsl_src_param src0_param;
4475 DWORD write_mask;
4477 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4478 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
4480 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
4483 /** Process the WINED3DSIO_LOOP instruction in GLSL:
4484 * Start a for() loop where src1.y is the initial value of aL,
4485 * increment aL by src1.z for a total of src1.x iterations.
4486 * Need to use a temporary variable for this operation.
4488 /* FIXME: I don't think nested loops will work correctly this way. */
4489 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
4491 struct wined3d_shader_parser_state *state = ins->ctx->state;
4492 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
4493 const struct wined3d_shader *shader = ins->ctx->shader;
4494 const struct wined3d_shader_lconst *constant;
4495 struct glsl_src_param src1_param;
4496 const DWORD *control_values = NULL;
4498 if (ins->ctx->reg_maps->shader_version.major < 4)
4500 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
4502 /* Try to hardcode the loop control parameters if possible. Direct3D 9
4503 * class hardware doesn't support real varying indexing, but Microsoft
4504 * designed this feature for Shader model 2.x+. If the loop control is
4505 * known at compile time, the GLSL compiler can unroll the loop, and
4506 * replace indirect addressing with direct addressing. */
4507 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
4509 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4511 if (constant->idx == ins->src[1].reg.idx[0].offset)
4513 control_values = constant->value;
4514 break;
4519 if (control_values)
4521 struct wined3d_shader_loop_control loop_control;
4522 loop_control.count = control_values[0];
4523 loop_control.start = control_values[1];
4524 loop_control.step = (int)control_values[2];
4526 if (loop_control.step > 0)
4528 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
4529 state->current_loop_depth, loop_control.start,
4530 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4531 state->current_loop_depth, loop_control.step);
4533 else if (loop_control.step < 0)
4535 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
4536 state->current_loop_depth, loop_control.start,
4537 state->current_loop_depth, loop_control.count, loop_control.step, loop_control.start,
4538 state->current_loop_depth, loop_control.step);
4540 else
4542 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
4543 state->current_loop_depth, loop_control.start, state->current_loop_depth,
4544 state->current_loop_depth, loop_control.count,
4545 state->current_loop_depth);
4548 else
4550 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
4551 state->current_loop_depth, state->current_loop_reg,
4552 src1_param.reg_name, state->current_loop_depth, src1_param.reg_name,
4553 state->current_loop_depth, state->current_loop_reg, src1_param.reg_name);
4556 ++state->current_loop_reg;
4558 else
4560 shader_addline(buffer, "for (;;)\n{\n");
4563 ++state->current_loop_depth;
4566 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
4568 struct wined3d_shader_parser_state *state = ins->ctx->state;
4570 shader_addline(ins->ctx->buffer, "}\n");
4572 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
4574 --state->current_loop_depth;
4575 --state->current_loop_reg;
4578 if (ins->handler_idx == WINED3DSIH_ENDREP)
4580 --state->current_loop_depth;
4584 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
4586 struct wined3d_shader_parser_state *state = ins->ctx->state;
4587 const struct wined3d_shader *shader = ins->ctx->shader;
4588 const struct wined3d_shader_lconst *constant;
4589 struct glsl_src_param src0_param;
4590 const DWORD *control_values = NULL;
4592 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
4593 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
4595 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
4597 if (constant->idx == ins->src[0].reg.idx[0].offset)
4599 control_values = constant->value;
4600 break;
4605 if (control_values)
4607 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
4608 state->current_loop_depth, state->current_loop_depth,
4609 control_values[0], state->current_loop_depth);
4611 else
4613 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4614 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
4615 state->current_loop_depth, state->current_loop_depth,
4616 src0_param.param_str, state->current_loop_depth);
4619 ++state->current_loop_depth;
4622 static void shader_glsl_switch(const struct wined3d_shader_instruction *ins)
4624 struct glsl_src_param src0_param;
4626 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4627 shader_addline(ins->ctx->buffer, "switch (%s)\n{\n", src0_param.param_str);
4630 static void shader_glsl_case(const struct wined3d_shader_instruction *ins)
4632 struct glsl_src_param src0_param;
4634 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4635 shader_addline(ins->ctx->buffer, "case %s:\n", src0_param.param_str);
4638 static void shader_glsl_default(const struct wined3d_shader_instruction *ins)
4640 shader_addline(ins->ctx->buffer, "default:\n");
4643 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
4645 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4646 struct glsl_src_param src0_param;
4648 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4649 shader_addline(ins->ctx->buffer, "if (%s(%s)) {\n", condition, src0_param.param_str);
4652 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
4654 struct glsl_src_param src0_param;
4655 struct glsl_src_param src1_param;
4657 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
4658 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4660 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
4661 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4664 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
4666 shader_addline(ins->ctx->buffer, "} else {\n");
4669 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
4671 unsigned int stream = ins->handler_idx == WINED3DSIH_EMIT ? 0 : ins->src[0].reg.idx[0].offset;
4673 shader_addline(ins->ctx->buffer, "setup_gs_output(gs_out);\n");
4674 if (!ins->ctx->gl_info->supported[ARB_CLIP_CONTROL])
4675 shader_glsl_fixup_position(ins->ctx->buffer);
4677 if (!stream)
4678 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
4679 else
4680 FIXME("Unhandled primitive stream %u.\n", stream);
4683 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
4685 shader_addline(ins->ctx->buffer, "break;\n");
4688 /* FIXME: According to MSDN the compare is done per component. */
4689 static void shader_glsl_breakc(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) break;\n",
4698 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
4701 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
4703 const char *condition = (ins->flags == WINED3D_SHADER_CONDITIONAL_OP_NZ) ? "bool" : "!bool";
4704 struct glsl_src_param src_param;
4706 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
4707 shader_addline(ins->ctx->buffer, "if (%s(%s)) break;\n", condition, src_param.param_str);
4710 static void shader_glsl_continue(const struct wined3d_shader_instruction *ins)
4712 shader_addline(ins->ctx->buffer, "continue;\n");
4715 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
4717 shader_addline(ins->ctx->buffer, "}\n");
4718 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
4720 /* Subroutines appear at the end of the shader. */
4721 ins->ctx->state->in_subroutine = TRUE;
4724 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
4726 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
4729 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
4731 struct glsl_src_param src1_param;
4733 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
4734 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
4735 src1_param.param_str, ins->src[0].reg.idx[0].offset);
4738 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
4740 const struct wined3d_shader_version *version = &ins->ctx->shader->reg_maps.shader_version;
4742 if (version->major >= 4 && !ins->ctx->state->in_subroutine)
4744 shader_glsl_generate_shader_epilogue(ins->ctx);
4745 shader_addline(ins->ctx->buffer, "return;\n");
4749 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
4751 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
4752 ins->ctx->reg_maps->shader_version.minor);
4753 struct glsl_sample_function sample_function;
4754 DWORD sample_flags = 0;
4755 DWORD resource_idx;
4756 DWORD mask = 0, swizzle;
4757 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
4759 /* 1.0-1.4: Use destination register as sampler source.
4760 * 2.0+: Use provided sampler source. */
4761 if (shader_version < WINED3D_SHADER_VERSION(2,0))
4762 resource_idx = ins->dst[0].reg.idx[0].offset;
4763 else
4764 resource_idx = ins->src[1].reg.idx[0].offset;
4766 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4768 DWORD flags = (priv->cur_ps_args->tex_transform >> resource_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
4769 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
4770 enum wined3d_shader_resource_type resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
4772 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
4773 if (flags & WINED3D_PSARGS_PROJECTED && resource_type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4775 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4776 switch (flags & ~WINED3D_PSARGS_PROJECTED)
4778 case WINED3D_TTFF_COUNT1:
4779 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
4780 break;
4781 case WINED3D_TTFF_COUNT2:
4782 mask = WINED3DSP_WRITEMASK_1;
4783 break;
4784 case WINED3D_TTFF_COUNT3:
4785 mask = WINED3DSP_WRITEMASK_2;
4786 break;
4787 case WINED3D_TTFF_COUNT4:
4788 case WINED3D_TTFF_DISABLE:
4789 mask = WINED3DSP_WRITEMASK_3;
4790 break;
4794 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
4796 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
4798 if (src_mod == WINED3DSPSM_DZ) {
4799 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4800 mask = WINED3DSP_WRITEMASK_2;
4801 } else if (src_mod == WINED3DSPSM_DW) {
4802 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4803 mask = WINED3DSP_WRITEMASK_3;
4806 else
4808 if ((ins->flags & WINED3DSI_TEXLD_PROJECT)
4809 && ins->ctx->reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_TEXTURE_CUBE)
4811 /* ps 2.0 texldp instruction always divides by the fourth component. */
4812 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
4813 mask = WINED3DSP_WRITEMASK_3;
4817 shader_glsl_get_sample_function(ins->ctx, resource_idx, resource_idx, sample_flags, &sample_function);
4818 mask |= sample_function.coord_mask;
4819 sample_function.coord_mask = mask;
4821 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
4822 else swizzle = ins->src[1].swizzle;
4824 /* 1.0-1.3: Use destination register as coordinate source.
4825 1.4+: Use provided coordinate source register. */
4826 if (shader_version < WINED3D_SHADER_VERSION(1,4))
4828 char coord_mask[6];
4829 shader_glsl_write_mask_to_str(mask, coord_mask);
4830 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4831 "T%u%s", resource_idx, coord_mask);
4833 else
4835 struct glsl_src_param coord_param;
4836 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
4837 if (ins->flags & WINED3DSI_TEXLD_BIAS)
4839 struct glsl_src_param bias;
4840 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
4841 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
4842 NULL, "%s", coord_param.param_str);
4843 } else {
4844 shader_glsl_gen_sample_code(ins, resource_idx, &sample_function, swizzle, NULL, NULL, NULL, NULL,
4845 "%s", coord_param.param_str);
4848 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4851 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
4853 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4854 struct glsl_src_param coord_param, dx_param, dy_param;
4855 struct glsl_sample_function sample_function;
4856 DWORD sampler_idx;
4857 DWORD swizzle = ins->src[1].swizzle;
4859 if (!shader_glsl_has_core_grad(gl_info, &ins->ctx->shader->reg_maps.shader_version)
4860 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4862 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
4863 shader_glsl_tex(ins);
4864 return;
4867 sampler_idx = ins->src[1].reg.idx[0].offset;
4869 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_GRAD, &sample_function);
4870 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4871 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.deriv_mask, &dx_param);
4872 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dy_param);
4874 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str,
4875 NULL, NULL, "%s", coord_param.param_str);
4876 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4879 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
4881 const struct wined3d_shader_version *shader_version = &ins->ctx->reg_maps->shader_version;
4882 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
4883 struct glsl_src_param coord_param, lod_param;
4884 struct glsl_sample_function sample_function;
4885 DWORD swizzle = ins->src[1].swizzle;
4886 DWORD sampler_idx;
4888 sampler_idx = ins->src[1].reg.idx[0].offset;
4890 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, WINED3D_GLSL_SAMPLE_LOD, &sample_function);
4891 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
4893 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
4895 if (shader_version->type == WINED3D_SHADER_TYPE_PIXEL && !shader_glsl_has_core_grad(gl_info, shader_version)
4896 && !gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4898 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
4899 * However, the NVIDIA drivers allow them in fragment shaders as well,
4900 * even without the appropriate extension. */
4901 WARN("Using %s in fragment shader.\n", sample_function.name->buffer);
4903 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str, NULL,
4904 "%s", coord_param.param_str);
4905 shader_glsl_release_sample_function(ins->ctx, &sample_function);
4908 static unsigned int shader_glsl_find_sampler(const struct wined3d_shader_sampler_map *sampler_map,
4909 unsigned int resource_idx, unsigned int sampler_idx)
4911 struct wined3d_shader_sampler_map_entry *entries = sampler_map->entries;
4912 unsigned int i;
4914 for (i = 0; i < sampler_map->count; ++i)
4916 if (entries[i].resource_idx == resource_idx && entries[i].sampler_idx == sampler_idx)
4917 return entries[i].bind_idx;
4920 ERR("No GLSL sampler found for resource %u / sampler %u.\n", resource_idx, sampler_idx);
4922 return ~0u;
4925 static void shader_glsl_atomic(const struct wined3d_shader_instruction *ins)
4927 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4928 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4929 struct glsl_src_param coord_param, data_param, data_param2;
4930 enum wined3d_shader_resource_type resource_type;
4931 enum wined3d_data_type data_type;
4932 unsigned int uav_idx;
4933 DWORD coord_mask;
4934 const char *op;
4936 uav_idx = ins->dst[0].reg.idx[0].offset;
4937 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4938 if (resource_type >= ARRAY_SIZE(resource_type_info))
4940 ERR("Unexpected resource type %#x.\n", resource_type);
4941 return;
4943 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4944 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4946 switch (ins->handler_idx)
4948 case WINED3DSIH_ATOMIC_AND:
4949 op = "imageAtomicAnd";
4950 break;
4951 case WINED3DSIH_ATOMIC_CMP_STORE:
4952 op = "imageAtomicCompSwap";
4953 break;
4954 case WINED3DSIH_ATOMIC_IADD:
4955 op = "imageAtomicAdd";
4956 break;
4957 case WINED3DSIH_ATOMIC_OR:
4958 op = "imageAtomicOr";
4959 break;
4960 case WINED3DSIH_ATOMIC_XOR:
4961 op = "imageAtomicXor";
4962 break;
4963 default:
4964 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4965 return;
4968 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &coord_param);
4970 if (reg_maps->uav_resource_info[uav_idx].flags & WINED3D_VIEW_BUFFER_RAW)
4971 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s / 4, ",
4972 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
4973 else
4974 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s, ",
4975 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
4977 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data_param, data_type);
4978 shader_addline(ins->ctx->buffer, "%s", data_param.param_str);
4979 if (ins->src_count >= 3)
4981 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data_param2, data_type);
4982 shader_addline(ins->ctx->buffer, ", %s", data_param2.param_str);
4985 shader_addline(ins->ctx->buffer, ");\n");
4988 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
4990 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4991 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4992 enum wined3d_shader_resource_type resource_type;
4993 struct glsl_src_param image_coord_param;
4994 enum wined3d_data_type data_type;
4995 DWORD coord_mask, write_mask;
4996 unsigned int uav_idx;
4997 char dst_swizzle[6];
4999 uav_idx = ins->src[1].reg.idx[0].offset;
5000 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5002 ERR("Invalid UAV index %u.\n", uav_idx);
5003 return;
5005 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5006 if (resource_type >= ARRAY_SIZE(resource_type_info))
5008 ERR("Unexpected resource type %#x.\n", resource_type);
5009 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5011 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5012 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5014 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5015 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5017 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5018 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5019 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5022 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5024 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5025 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5026 struct glsl_src_param image_coord_param, image_data_param;
5027 enum wined3d_shader_resource_type resource_type;
5028 enum wined3d_data_type data_type;
5029 unsigned int uav_idx;
5030 DWORD coord_mask;
5032 uav_idx = ins->dst[0].reg.idx[0].offset;
5033 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5035 ERR("Invalid UAV index %u.\n", uav_idx);
5036 return;
5038 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5039 if (resource_type >= ARRAY_SIZE(resource_type_info))
5041 ERR("Unexpected resource type %#x.\n", resource_type);
5042 return;
5044 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5045 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5047 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5048 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5049 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5050 shader_glsl_get_prefix(version->type), uav_idx,
5051 image_coord_param.param_str, image_data_param.param_str);
5054 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5056 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5057 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5058 enum wined3d_shader_resource_type resource_type;
5059 enum wined3d_shader_register_type reg_type;
5060 unsigned int resource_idx, bind_idx, i;
5061 enum wined3d_data_type dst_data_type;
5062 struct glsl_src_param lod_param;
5063 char dst_swizzle[6];
5064 DWORD write_mask;
5066 dst_data_type = ins->dst[0].reg.data_type;
5067 if (ins->flags == WINED3DSI_RESINFO_UINT)
5068 dst_data_type = WINED3D_DATA_UINT;
5069 else if (ins->flags)
5070 FIXME("Unhandled flags %#x.\n", ins->flags);
5072 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5073 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5075 reg_type = ins->src[1].reg.type;
5076 resource_idx = ins->src[1].reg.idx[0].offset;
5077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5078 if (reg_type == WINED3DSPR_RESOURCE)
5080 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5081 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5082 resource_idx, WINED3D_SAMPLER_DEFAULT);
5084 else
5086 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5087 bind_idx = resource_idx;
5090 if (resource_type >= ARRAY_SIZE(resource_type_info))
5092 ERR("Unexpected resource type %#x.\n", resource_type);
5093 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5096 if (dst_data_type == WINED3D_DATA_UINT)
5097 shader_addline(ins->ctx->buffer, "uvec4(");
5098 else
5099 shader_addline(ins->ctx->buffer, "vec4(");
5101 if (reg_type == WINED3DSPR_RESOURCE)
5103 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5104 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5106 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5107 shader_addline(ins->ctx->buffer, "0, ");
5109 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5111 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5112 shader_glsl_get_prefix(version->type), bind_idx);
5114 else
5116 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5117 shader_addline(ins->ctx->buffer, "1");
5120 else
5122 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5123 shader_glsl_get_prefix(version->type), bind_idx);
5125 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5126 shader_addline(ins->ctx->buffer, "0, ");
5128 /* For UAVs the returned miplevel count is always 1. */
5129 shader_addline(ins->ctx->buffer, "1");
5132 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5135 /* FIXME: The current implementation does not handle multisample textures correctly. */
5136 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5138 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5139 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5140 struct glsl_src_param coord_param, lod_param;
5141 struct glsl_sample_function sample_function;
5142 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5143 BOOL has_lod_param;
5145 if (wined3d_shader_instruction_has_texel_offset(ins))
5146 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5148 resource_idx = ins->src[1].reg.idx[0].offset;
5149 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5151 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5153 ERR("Invalid resource index %u.\n", resource_idx);
5154 return;
5156 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5158 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5159 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5160 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5161 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5162 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5163 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5164 "%s", coord_param.param_str);
5165 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5168 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5170 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5171 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5172 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5173 struct glsl_sample_function sample_function;
5174 DWORD flags = 0;
5176 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5177 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5178 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5179 flags |= WINED3D_GLSL_SAMPLE_LOD;
5180 if (wined3d_shader_instruction_has_texel_offset(ins))
5181 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5183 resource_idx = ins->src[1].reg.idx[0].offset;
5184 sampler_idx = ins->src[2].reg.idx[0].offset;
5186 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5187 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5189 switch (ins->handler_idx)
5191 case WINED3DSIH_SAMPLE:
5192 break;
5193 case WINED3DSIH_SAMPLE_B:
5194 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5195 lod_param_str = lod_param.param_str;
5196 break;
5197 case WINED3DSIH_SAMPLE_GRAD:
5198 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5199 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5200 dx_param_str = dx_param.param_str;
5201 dy_param_str = dy_param.param_str;
5202 break;
5203 case WINED3DSIH_SAMPLE_LOD:
5204 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5205 lod_param_str = lod_param.param_str;
5206 break;
5207 default:
5208 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5209 break;
5212 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5213 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5214 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5215 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5218 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5220 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5221 struct glsl_src_param coord_param, compare_param;
5222 struct glsl_sample_function sample_function;
5223 const char *lod_param = NULL;
5224 DWORD flags = 0;
5225 UINT coord_size;
5227 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5229 lod_param = "0";
5230 flags |= WINED3D_GLSL_SAMPLE_LOD;
5233 if (wined3d_shader_instruction_has_texel_offset(ins))
5234 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5236 resource_idx = ins->src[1].reg.idx[0].offset;
5237 sampler_idx = ins->src[2].reg.idx[0].offset;
5239 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5240 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5241 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5242 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5243 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5244 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5245 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5246 coord_size, coord_param.param_str, compare_param.param_str);
5247 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5250 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5252 /* FIXME: Make this work for more than just 2D textures */
5253 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5254 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5256 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5258 char dst_mask[6];
5260 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5261 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5262 ins->dst[0].reg.idx[0].offset, dst_mask);
5264 else
5266 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5267 DWORD reg = ins->src[0].reg.idx[0].offset;
5268 char dst_swizzle[6];
5270 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5272 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5274 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5275 struct glsl_src_param div_param;
5276 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5278 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5280 if (mask_size > 1)
5281 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5282 else
5283 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5285 else
5287 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5292 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5293 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5294 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5295 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5297 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5298 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5299 struct glsl_sample_function sample_function;
5300 struct glsl_src_param src0_param;
5301 UINT mask_size;
5303 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5305 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5306 * scalar, and projected sampling would require 4.
5308 * It is a dependent read - not valid with conditional NP2 textures
5310 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5311 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5313 switch(mask_size)
5315 case 1:
5316 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5317 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5318 break;
5320 case 2:
5321 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5322 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5323 break;
5325 case 3:
5326 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5327 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5328 break;
5330 default:
5331 FIXME("Unexpected mask size %u\n", mask_size);
5332 break;
5334 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5337 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5338 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5339 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5341 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5342 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5343 struct glsl_src_param src0_param;
5344 DWORD dst_mask;
5345 unsigned int mask_size;
5347 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5348 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5349 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5351 if (mask_size > 1) {
5352 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5353 } else {
5354 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5358 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5359 * Calculate the depth as dst.x / dst.y */
5360 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5362 struct glsl_dst_param dst_param;
5364 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5366 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5367 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5368 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5369 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5370 * >= 1.0 or < 0.0
5372 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5373 dst_param.reg_name, dst_param.reg_name);
5376 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5377 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5378 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5379 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5381 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5383 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5384 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5385 struct glsl_src_param src0_param;
5387 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5389 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5390 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5393 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5394 * Calculate the 1st of a 2-row matrix multiplication. */
5395 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5397 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5398 DWORD reg = ins->dst[0].reg.idx[0].offset;
5399 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5400 struct glsl_src_param src0_param;
5402 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5403 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5406 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5407 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5408 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5410 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5411 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5412 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5413 DWORD reg = ins->dst[0].reg.idx[0].offset;
5414 struct glsl_src_param src0_param;
5416 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5417 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5418 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5421 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5423 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5424 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5425 struct glsl_sample_function sample_function;
5426 DWORD reg = ins->dst[0].reg.idx[0].offset;
5427 struct glsl_src_param src0_param;
5429 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5430 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5432 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5434 /* Sample the texture using the calculated coordinates */
5435 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5436 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5439 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5440 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5441 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5443 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5444 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5445 struct glsl_sample_function sample_function;
5446 DWORD reg = ins->dst[0].reg.idx[0].offset;
5447 struct glsl_src_param src0_param;
5449 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5450 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5452 /* Dependent read, not valid with conditional NP2 */
5453 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5455 /* Sample the texture using the calculated coordinates */
5456 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5457 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5459 tex_mx->current_row = 0;
5462 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5463 * Perform the 3rd row of a 3x3 matrix multiply */
5464 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5466 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5467 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5468 DWORD reg = ins->dst[0].reg.idx[0].offset;
5469 struct glsl_src_param src0_param;
5470 char dst_mask[6];
5472 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5474 shader_glsl_append_dst(ins->ctx->buffer, ins);
5475 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5476 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5478 tex_mx->current_row = 0;
5481 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5482 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5483 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5485 struct glsl_src_param src0_param;
5486 struct glsl_src_param src1_param;
5487 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5488 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5489 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5490 struct glsl_sample_function sample_function;
5491 DWORD reg = ins->dst[0].reg.idx[0].offset;
5492 char coord_mask[6];
5494 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5495 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5497 /* Perform the last matrix multiply operation */
5498 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5499 /* Reflection calculation */
5500 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5502 /* Dependent read, not valid with conditional NP2 */
5503 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5504 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5506 /* Sample the texture */
5507 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5508 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5509 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5511 tex_mx->current_row = 0;
5514 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5515 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5516 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5518 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5519 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5520 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5521 struct glsl_sample_function sample_function;
5522 DWORD reg = ins->dst[0].reg.idx[0].offset;
5523 struct glsl_src_param src0_param;
5524 char coord_mask[6];
5526 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5528 /* Perform the last matrix multiply operation */
5529 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5531 /* Construct the eye-ray vector from w coordinates */
5532 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5533 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5534 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5536 /* Dependent read, not valid with conditional NP2 */
5537 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5538 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5540 /* Sample the texture using the calculated coordinates */
5541 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5542 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5543 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5545 tex_mx->current_row = 0;
5548 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5549 * Apply a fake bump map transform.
5550 * texbem is pshader <= 1.3 only, this saves a few version checks
5552 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5554 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5555 struct glsl_sample_function sample_function;
5556 struct glsl_src_param coord_param;
5557 DWORD sampler_idx;
5558 DWORD mask;
5559 DWORD flags;
5560 char coord_mask[6];
5562 sampler_idx = ins->dst[0].reg.idx[0].offset;
5563 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5564 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5566 /* Dependent read, not valid with conditional NP2 */
5567 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5568 mask = sample_function.coord_mask;
5570 shader_glsl_write_mask_to_str(mask, coord_mask);
5572 /* With projected textures, texbem only divides the static texture coord,
5573 * not the displacement, so we can't let GL handle this. */
5574 if (flags & WINED3D_PSARGS_PROJECTED)
5576 DWORD div_mask=0;
5577 char coord_div_mask[3];
5578 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5580 case WINED3D_TTFF_COUNT1:
5581 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5582 break;
5583 case WINED3D_TTFF_COUNT2:
5584 div_mask = WINED3DSP_WRITEMASK_1;
5585 break;
5586 case WINED3D_TTFF_COUNT3:
5587 div_mask = WINED3DSP_WRITEMASK_2;
5588 break;
5589 case WINED3D_TTFF_COUNT4:
5590 case WINED3D_TTFF_DISABLE:
5591 div_mask = WINED3DSP_WRITEMASK_3;
5592 break;
5594 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5595 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5598 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5600 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5601 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5602 coord_param.param_str, coord_mask);
5604 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5606 struct glsl_src_param luminance_param;
5607 struct glsl_dst_param dst_param;
5609 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5610 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5612 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5613 dst_param.reg_name, dst_param.mask_str,
5614 luminance_param.param_str, sampler_idx, sampler_idx);
5616 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5619 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5621 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5622 struct glsl_src_param src0_param, src1_param;
5624 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5625 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5627 shader_glsl_append_dst(ins->ctx->buffer, ins);
5628 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5629 src0_param.param_str, sampler_idx, src1_param.param_str);
5632 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5633 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5634 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5636 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5637 struct glsl_sample_function sample_function;
5638 struct glsl_src_param src0_param;
5640 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5642 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5643 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5644 "%s.wx", src0_param.reg_name);
5645 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5648 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5649 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5650 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5652 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5653 struct glsl_sample_function sample_function;
5654 struct glsl_src_param src0_param;
5656 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5658 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5659 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5660 "%s.yz", src0_param.reg_name);
5661 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5664 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5665 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5666 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5668 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5669 struct glsl_sample_function sample_function;
5670 struct glsl_src_param src0_param;
5672 /* Dependent read, not valid with conditional NP2 */
5673 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5674 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5676 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5677 "%s", src0_param.param_str);
5678 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5681 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5682 * If any of the first 3 components are < 0, discard this pixel */
5683 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5685 if (ins->ctx->reg_maps->shader_version.major >= 4)
5687 struct glsl_src_param src_param;
5689 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5690 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
5692 else
5694 struct glsl_dst_param dst_param;
5696 /* The argument is a destination parameter, and no writemasks are allowed */
5697 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5699 /* 2.0 shaders compare all 4 components in texkill. */
5700 if (ins->ctx->reg_maps->shader_version.major >= 2)
5701 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5702 /* 1.x shaders only compare the first 3 components, probably due to
5703 * the nature of the texkill instruction as a tex* instruction, and
5704 * phase, which kills all .w components. Even if all 4 components are
5705 * defined, only the first 3 are used. */
5706 else
5707 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5711 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5712 * dst = dot2(src0, src1) + src2 */
5713 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5715 struct glsl_src_param src0_param;
5716 struct glsl_src_param src1_param;
5717 struct glsl_src_param src2_param;
5718 DWORD write_mask;
5719 unsigned int mask_size;
5721 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5722 mask_size = shader_glsl_get_write_mask_size(write_mask);
5724 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5725 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5726 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5728 if (mask_size > 1) {
5729 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5730 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5731 } else {
5732 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5733 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5737 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5738 const struct wined3d_shader_signature *input_signature,
5739 const struct wined3d_shader_reg_maps *reg_maps,
5740 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5742 unsigned int i;
5744 for (i = 0; i < input_signature->element_count; ++i)
5746 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5747 const char *semantic_name;
5748 UINT semantic_idx;
5749 char reg_mask[6];
5751 /* Unused */
5752 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5753 continue;
5755 semantic_name = input->semantic_name;
5756 semantic_idx = input->semantic_idx;
5757 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5759 if (args->vp_mode == vertexshader)
5761 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5763 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5764 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5766 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5768 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5770 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5772 shader_addline(buffer, "ps_in[%u] = vec4("
5773 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5774 input->register_idx);
5776 else
5778 if (input->sysval_semantic)
5779 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5780 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5781 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5782 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5785 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5787 if (args->pointsprite)
5788 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5789 shader->u.ps.input_reg_map[input->register_idx]);
5790 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5791 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5792 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5793 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5794 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5795 else
5796 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5797 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5799 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5801 if (!semantic_idx)
5802 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5803 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5804 else if (semantic_idx == 1)
5805 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5806 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5807 else
5808 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5809 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5811 else
5813 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5814 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5819 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5821 struct glsl_program_key key;
5823 key.vs_id = entry->vs.id;
5824 key.gs_id = entry->gs.id;
5825 key.ps_id = entry->ps.id;
5826 key.cs_id = entry->cs.id;
5828 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5830 ERR("Failed to insert program entry.\n");
5834 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5835 const struct glsl_program_key *key)
5837 struct wine_rb_entry *entry;
5839 entry = wine_rb_get(&priv->program_lookup, key);
5840 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5843 /* Context activation is done by the caller. */
5844 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5845 struct glsl_shader_prog_link *entry)
5847 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
5849 GL_EXTCALL(glDeleteProgram(entry->id));
5850 if (entry->vs.id)
5851 list_remove(&entry->vs.shader_entry);
5852 if (entry->gs.id)
5853 list_remove(&entry->gs.shader_entry);
5854 if (entry->ps.id)
5855 list_remove(&entry->ps.shader_entry);
5856 if (entry->cs.id)
5857 list_remove(&entry->cs.shader_entry);
5858 HeapFree(GetProcessHeap(), 0, entry);
5861 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5862 const struct wined3d_gl_info *gl_info, const DWORD *map,
5863 const struct wined3d_shader_signature *input_signature,
5864 const struct wined3d_shader_reg_maps *reg_maps_in,
5865 const struct wined3d_shader_signature *output_signature,
5866 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5868 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5869 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5870 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5871 unsigned int in_count = vec4_varyings(3, gl_info);
5872 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5873 DWORD in_idx, *set = NULL;
5874 unsigned int i, j;
5875 char reg_mask[6];
5877 set = wined3d_calloc(max_varyings, sizeof(*set));
5879 for (i = 0; i < input_signature->element_count; ++i)
5881 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5883 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5884 continue;
5886 in_idx = map[input->register_idx];
5887 /* Declared, but not read register */
5888 if (in_idx == ~0u)
5889 continue;
5890 if (in_idx >= max_varyings)
5892 FIXME("More input varyings declared than supported, expect issues.\n");
5893 continue;
5896 if (in_idx == in_count)
5897 string_buffer_sprintf(destination, "gl_FrontColor");
5898 else if (in_idx == in_count + 1)
5899 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5900 else
5901 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5903 if (!set[in_idx])
5904 set[in_idx] = ~0u;
5906 for (j = 0; j < output_signature->element_count; ++j)
5908 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5909 DWORD mask;
5911 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5912 || input->semantic_idx != output->semantic_idx
5913 || strcmp(input->semantic_name, output->semantic_name)
5914 || !(mask = input->mask & output->mask))
5915 continue;
5917 if (set[in_idx] == ~0u)
5918 set[in_idx] = 0;
5919 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5920 shader_glsl_write_mask_to_str(mask, reg_mask);
5922 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5923 destination->buffer, reg_mask, output->register_idx, reg_mask);
5927 for (i = 0; i < max_varyings; ++i)
5929 unsigned int size;
5931 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5932 continue;
5934 if (set[i] == ~0u)
5935 set[i] = 0;
5937 size = 0;
5938 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5939 reg_mask[size++] = 'x';
5940 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5941 reg_mask[size++] = 'y';
5942 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5943 reg_mask[size++] = 'z';
5944 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5945 reg_mask[size++] = 'w';
5946 reg_mask[size] = '\0';
5948 if (i == in_count)
5949 string_buffer_sprintf(destination, "gl_FrontColor");
5950 else if (i == in_count + 1)
5951 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5952 else
5953 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5955 if (size == 1)
5956 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5957 else
5958 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5961 HeapFree(GetProcessHeap(), 0, set);
5962 string_buffer_release(&priv->string_buffers, destination);
5965 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
5966 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
5967 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5969 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5970 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5971 char reg_mask[6];
5972 unsigned int i;
5974 for (i = 0; i < output_signature->element_count; ++i)
5976 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
5978 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
5979 continue;
5981 if (output->register_idx >= input_count)
5982 continue;
5984 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
5986 shader_glsl_write_mask_to_str(output->mask, reg_mask);
5988 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5989 destination->buffer, reg_mask, output->register_idx, reg_mask);
5992 string_buffer_release(&priv->string_buffers, destination);
5995 /* Context activation is done by the caller. */
5996 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
5997 const struct wined3d_shader *vs, unsigned int input_count,
5998 const struct wined3d_gl_info *gl_info)
6000 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6001 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6003 if (legacy_context)
6004 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6005 else
6006 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6007 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6009 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6010 legacy_context ? "gs_in" : "gs_in.gs_in");
6012 shader_addline(buffer, "}\n");
6015 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6016 const struct wined3d_gl_info *gl_info, const DWORD *map,
6017 const struct wined3d_shader_signature *input_signature,
6018 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6019 const struct wined3d_shader_signature *output_signature,
6020 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6022 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6023 const char *semantic_name;
6024 UINT semantic_idx;
6025 char reg_mask[6];
6026 unsigned int i;
6028 /* First, sort out position and point size system values. */
6029 for (i = 0; i < output_signature->element_count; ++i)
6031 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6033 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6034 continue;
6036 semantic_name = output->semantic_name;
6037 semantic_idx = output->semantic_idx;
6038 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6040 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6042 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6043 reg_mask, output->register_idx, reg_mask);
6045 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6047 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6048 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6050 else if (output->sysval_semantic)
6052 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6056 /* Then, setup the pixel shader input. */
6057 if (reg_maps_out->shader_version.major < 4)
6058 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6059 output_signature, reg_maps_out, "ps_link");
6060 else
6061 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6064 /* Context activation is done by the caller. */
6065 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6066 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6067 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6069 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6070 GLuint ret;
6071 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6072 unsigned int i;
6073 const char *semantic_name;
6074 UINT semantic_idx;
6075 char reg_mask[6];
6076 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6078 string_buffer_clear(buffer);
6080 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &vs->reg_maps.shader_version));
6082 if (per_vertex_point_size)
6084 shader_addline(buffer, "uniform struct\n{\n");
6085 shader_addline(buffer, " float size_min;\n");
6086 shader_addline(buffer, " float size_max;\n");
6087 shader_addline(buffer, "} ffp_point;\n");
6090 if (ps_major < 3)
6092 DWORD colors_written_mask[2] = {0};
6093 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6095 if (!legacy_context)
6097 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6098 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6099 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6100 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6103 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6105 for (i = 0; i < vs->output_signature.element_count; ++i)
6107 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6108 DWORD write_mask;
6110 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6111 continue;
6113 semantic_name = output->semantic_name;
6114 semantic_idx = output->semantic_idx;
6115 write_mask = output->mask;
6116 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6118 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6120 if (legacy_context)
6121 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6122 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6123 else
6124 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6125 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6127 colors_written_mask[semantic_idx] = write_mask;
6129 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6131 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6132 reg_mask, output->register_idx, reg_mask);
6134 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6136 if (semantic_idx < MAX_TEXTURES)
6138 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6139 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6140 semantic_idx, reg_mask, output->register_idx, reg_mask);
6141 texcoords_written_mask[semantic_idx] = write_mask;
6144 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6146 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6147 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6149 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6151 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6152 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6153 output->register_idx, reg_mask[1]);
6157 for (i = 0; i < 2; ++i)
6159 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6161 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6162 if (!i)
6163 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6164 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6165 reg_mask, reg_mask);
6166 else
6167 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6168 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6169 reg_mask, reg_mask);
6172 for (i = 0; i < MAX_TEXTURES; ++i)
6174 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6175 continue;
6177 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6179 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6180 && !texcoords_written_mask[i])
6181 continue;
6183 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6184 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6185 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6189 else
6191 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6193 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6194 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6195 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6196 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6199 shader_addline(buffer, "}\n");
6201 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6202 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6203 shader_glsl_compile(gl_info, ret, buffer->buffer);
6205 return ret;
6208 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6209 const struct wined3d_shader *shader, unsigned int input_count,
6210 const struct wined3d_gl_info *gl_info)
6212 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6214 if (input_count)
6215 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6217 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6218 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6220 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6221 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6223 shader_addline(buffer, "}\n");
6226 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6227 const struct wined3d_gl_info *gl_info)
6229 const char *output = get_fragment_output(gl_info);
6231 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6232 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6233 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6234 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6235 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6236 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6239 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6240 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6242 const char *output = get_fragment_output(gl_info);
6244 switch (mode)
6246 case WINED3D_FFP_PS_FOG_OFF:
6247 return;
6249 case WINED3D_FFP_PS_FOG_LINEAR:
6250 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6251 break;
6253 case WINED3D_FFP_PS_FOG_EXP:
6254 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6255 break;
6257 case WINED3D_FFP_PS_FOG_EXP2:
6258 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6259 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6260 break;
6262 default:
6263 ERR("Invalid fog mode %#x.\n", mode);
6264 return;
6267 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6268 output, output);
6271 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6272 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6274 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6275 * flipping all the operators here, just negate the comparison below. */
6276 static const char * const comparison_operator[] =
6278 "", /* WINED3D_CMP_NEVER */
6279 "<", /* WINED3D_CMP_LESS */
6280 "==", /* WINED3D_CMP_EQUAL */
6281 "<=", /* WINED3D_CMP_LESSEQUAL */
6282 ">", /* WINED3D_CMP_GREATER */
6283 "!=", /* WINED3D_CMP_NOTEQUAL */
6284 ">=", /* WINED3D_CMP_GREATEREQUAL */
6285 "" /* WINED3D_CMP_ALWAYS */
6288 if (alpha_func == WINED3D_CMP_ALWAYS)
6289 return;
6291 if (alpha_func != WINED3D_CMP_NEVER)
6292 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6293 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6294 shader_addline(buffer, " discard;\n");
6297 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6298 const struct wined3d_gl_info *gl_info)
6300 if (gl_info->supported[ARB_GPU_SHADER5])
6301 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6302 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6303 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6304 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6305 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6306 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6307 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6308 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6309 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6310 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6311 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6312 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6313 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6314 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6315 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6316 if (gl_info->supported[EXT_GPU_SHADER4])
6317 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6318 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6319 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6322 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6323 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6324 const struct ps_compile_args *args)
6326 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6328 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6329 if (reg_maps->shader_version.major < 2)
6330 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6332 if (args->srgb_correction)
6333 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6335 /* SM < 3 does not replace the fog stage. */
6336 if (reg_maps->shader_version.major < 3)
6337 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6339 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6342 /* Context activation is done by the caller. */
6343 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6344 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6345 const struct wined3d_shader *shader,
6346 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6348 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6349 const struct wined3d_gl_info *gl_info = context->gl_info;
6350 const DWORD *function = shader->function;
6351 struct shader_glsl_ctx_priv priv_ctx;
6352 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6354 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6355 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6357 memset(&priv_ctx, 0, sizeof(priv_ctx));
6358 priv_ctx.cur_ps_args = args;
6359 priv_ctx.cur_np2fixup_info = np2fixup_info;
6360 priv_ctx.string_buffers = string_buffers;
6362 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6364 shader_glsl_enable_extensions(buffer, gl_info);
6365 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6366 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6367 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6368 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6369 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6370 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6371 /* The spec says that it doesn't have to be explicitly enabled, but the
6372 * nvidia drivers write a warning if we don't do so. */
6373 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6374 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6376 /* Base Declarations */
6377 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6379 shader_addline(buffer, "void main()\n{\n");
6381 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6382 * add approximately 0.5. This causes off-by-one problems as spotted by
6383 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6384 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6385 * causes precision troubles when we just subtract 0.5.
6387 * To deal with that, just floor() the position. This will eliminate the
6388 * fraction on all cards.
6390 * TODO: Test how this behaves with multisampling.
6392 * An advantage of floor is that it works even if the driver doesn't add
6393 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6394 * to return in gl_FragCoord, even though coordinates specify the pixel
6395 * centers instead of the pixel corners. This code will behave correctly
6396 * on drivers that returns integer values. */
6397 if (reg_maps->vpos)
6399 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6400 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6401 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6402 shader_addline(buffer,
6403 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6404 else
6405 shader_addline(buffer,
6406 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6409 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6411 unsigned int i;
6412 WORD map = reg_maps->texcoord;
6414 if (legacy_context)
6416 if (glsl_is_color_reg_read(shader, 0))
6417 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6418 if (glsl_is_color_reg_read(shader, 1))
6419 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6422 for (i = 0; map; map >>= 1, ++i)
6424 if (map & 1)
6426 if (args->pointsprite)
6427 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6428 else if (args->texcoords_initialized & (1u << i))
6429 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6430 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6431 else
6432 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6433 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6437 if (legacy_context)
6438 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6441 /* Pack 3.0 inputs */
6442 if (reg_maps->shader_version.major >= 3)
6443 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6445 /* Base Shader Body */
6446 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6448 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6449 if (reg_maps->shader_version.major < 4)
6450 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6452 shader_addline(buffer, "}\n");
6454 TRACE("Compiling shader object %u.\n", shader_id);
6455 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6457 return shader_id;
6460 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6461 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6462 const struct vs_compile_args *args)
6464 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6465 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6466 unsigned int i;
6468 /* Unpack outputs. */
6469 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6471 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6472 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6473 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6474 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6476 if (reg_maps->shader_version.major < 3)
6478 if (args->fog_src == VS_FOG_Z)
6479 shader_addline(buffer, "%s = gl_Position.z;\n",
6480 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6481 else if (!reg_maps->fog)
6482 shader_addline(buffer, "%s = 0.0;\n",
6483 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6486 /* We always store the clipplanes without y inversion. */
6487 if (args->clip_enabled)
6489 if (legacy_context)
6490 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6491 else
6492 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6493 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6496 if (args->point_size && !args->per_vertex_point_size)
6497 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6499 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6500 shader_glsl_fixup_position(buffer);
6503 /* Context activation is done by the caller. */
6504 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6505 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6507 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6508 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6509 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6510 const struct wined3d_gl_info *gl_info = context->gl_info;
6511 const DWORD *function = shader->function;
6512 struct shader_glsl_ctx_priv priv_ctx;
6514 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6515 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6517 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6519 shader_glsl_enable_extensions(buffer, gl_info);
6520 if (gl_info->supported[ARB_DRAW_INSTANCED])
6521 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6522 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6523 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6525 memset(&priv_ctx, 0, sizeof(priv_ctx));
6526 priv_ctx.cur_vs_args = args;
6527 priv_ctx.string_buffers = string_buffers;
6529 /* Base Declarations */
6530 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6532 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6533 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6535 if (reg_maps->shader_version.major >= 4)
6537 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6538 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6539 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6540 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6543 shader_addline(buffer, "void main()\n{\n");
6545 /* Base Shader Body */
6546 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6548 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6549 if (reg_maps->shader_version.major < 4)
6550 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6552 shader_addline(buffer, "}\n");
6554 TRACE("Compiling shader object %u.\n", shader_id);
6555 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6557 return shader_id;
6560 /* Context activation is done by the caller. */
6561 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6562 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6564 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6565 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6566 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6567 const struct wined3d_gl_info *gl_info = context->gl_info;
6568 const DWORD *function = shader->function;
6569 struct shader_glsl_ctx_priv priv_ctx;
6570 GLuint shader_id;
6572 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6574 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6576 shader_glsl_enable_extensions(buffer, gl_info);
6577 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6578 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6580 memset(&priv_ctx, 0, sizeof(priv_ctx));
6581 priv_ctx.string_buffers = string_buffers;
6582 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6583 if (!gl_info->supported[ARB_CLIP_CONTROL])
6584 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6585 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6586 shader_addline(buffer, "void main()\n{\n");
6587 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6588 shader_addline(buffer, "}\n");
6590 TRACE("Compiling shader object %u.\n", shader_id);
6591 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6593 return shader_id;
6596 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6598 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6599 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6600 const struct wined3d_shader *shader = ctx->shader;
6602 switch (shader->reg_maps.shader_version.type)
6604 case WINED3D_SHADER_TYPE_PIXEL:
6605 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6606 break;
6607 case WINED3D_SHADER_TYPE_VERTEX:
6608 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
6609 break;
6610 case WINED3D_SHADER_TYPE_GEOMETRY:
6611 case WINED3D_SHADER_TYPE_COMPUTE:
6612 break;
6613 default:
6614 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6615 break;
6619 /* Context activation is done by the caller. */
6620 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
6621 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6622 const struct wined3d_shader *shader)
6624 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
6625 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6626 const struct wined3d_gl_info *gl_info = context->gl_info;
6627 const DWORD *function = shader->function;
6628 struct shader_glsl_ctx_priv priv_ctx;
6629 GLuint shader_id;
6631 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
6633 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6635 shader_glsl_enable_extensions(buffer, gl_info);
6636 if (gl_info->supported[ARB_COMPUTE_SHADER])
6637 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
6639 memset(&priv_ctx, 0, sizeof(priv_ctx));
6640 priv_ctx.string_buffers = string_buffers;
6641 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6643 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
6644 thread_group_size->x, thread_group_size->y, thread_group_size->z);
6646 shader_addline(buffer, "void main()\n{\n");
6647 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6648 shader_addline(buffer, "}\n");
6650 TRACE("Compiling shader object %u.\n", shader_id);
6651 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6653 return shader_id;
6656 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6657 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6658 struct wined3d_shader *shader,
6659 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6661 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6662 struct glsl_shader_private *shader_data;
6663 struct ps_np2fixup_info *np2fixup;
6664 UINT i;
6665 DWORD new_size;
6666 GLuint ret;
6668 if (!shader->backend_data)
6670 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6671 if (!shader->backend_data)
6673 ERR("Failed to allocate backend data.\n");
6674 return 0;
6677 shader_data = shader->backend_data;
6678 gl_shaders = shader_data->gl_shaders.ps;
6680 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6681 * so a linear search is more performant than a hashmap or a binary search
6682 * (cache coherency etc)
6684 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6686 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6688 if (args->np2_fixup)
6689 *np2fixup_info = &gl_shaders[i].np2fixup;
6690 return gl_shaders[i].id;
6694 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6695 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6696 if (shader_data->num_gl_shaders)
6698 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6699 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6700 new_size * sizeof(*gl_shaders));
6702 else
6704 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6705 new_size = 1;
6708 if(!new_array) {
6709 ERR("Out of memory\n");
6710 return 0;
6712 shader_data->gl_shaders.ps = new_array;
6713 shader_data->shader_array_size = new_size;
6714 gl_shaders = new_array;
6717 gl_shaders[shader_data->num_gl_shaders].args = *args;
6719 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6720 memset(np2fixup, 0, sizeof(*np2fixup));
6721 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6723 pixelshader_update_resource_types(shader, args->tex_types);
6725 string_buffer_clear(buffer);
6726 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6727 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6729 return ret;
6732 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6733 const DWORD use_map)
6735 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6736 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6737 if (stored->point_size != new->point_size)
6738 return FALSE;
6739 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6740 return FALSE;
6741 if (stored->flatshading != new->flatshading)
6742 return FALSE;
6743 if (stored->next_shader_type != new->next_shader_type)
6744 return FALSE;
6745 if (stored->next_shader_input_count != new->next_shader_input_count)
6746 return FALSE;
6747 return stored->fog_src == new->fog_src;
6750 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6751 struct wined3d_shader *shader, const struct vs_compile_args *args)
6753 UINT i;
6754 DWORD new_size;
6755 DWORD use_map = context->stream_info.use_map;
6756 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6757 struct glsl_shader_private *shader_data;
6758 GLuint ret;
6760 if (!shader->backend_data)
6762 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6763 if (!shader->backend_data)
6765 ERR("Failed to allocate backend data.\n");
6766 return 0;
6769 shader_data = shader->backend_data;
6770 gl_shaders = shader_data->gl_shaders.vs;
6772 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6773 * so a linear search is more performant than a hashmap or a binary search
6774 * (cache coherency etc)
6776 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6778 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6779 return gl_shaders[i].id;
6782 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6784 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6785 if (shader_data->num_gl_shaders)
6787 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6788 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6789 new_size * sizeof(*gl_shaders));
6791 else
6793 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6794 new_size = 1;
6797 if(!new_array) {
6798 ERR("Out of memory\n");
6799 return 0;
6801 shader_data->gl_shaders.vs = new_array;
6802 shader_data->shader_array_size = new_size;
6803 gl_shaders = new_array;
6806 gl_shaders[shader_data->num_gl_shaders].args = *args;
6808 string_buffer_clear(&priv->shader_buffer);
6809 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6810 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6812 return ret;
6815 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6816 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6818 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6819 struct glsl_shader_private *shader_data;
6820 unsigned int i, new_size;
6821 GLuint ret;
6823 if (!shader->backend_data)
6825 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6827 ERR("Failed to allocate backend data.\n");
6828 return 0;
6831 shader_data = shader->backend_data;
6832 gl_shaders = shader_data->gl_shaders.gs;
6834 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6836 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6837 return gl_shaders[i].id;
6840 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6842 if (shader_data->num_gl_shaders)
6844 new_size = shader_data->shader_array_size + 1;
6845 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6846 new_size * sizeof(*new_array));
6848 else
6850 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6851 new_size = 1;
6854 if (!new_array)
6856 ERR("Failed to allocate GL shaders array.\n");
6857 return 0;
6859 shader_data->gl_shaders.gs = new_array;
6860 shader_data->shader_array_size = new_size;
6861 gl_shaders = new_array;
6863 string_buffer_clear(&priv->shader_buffer);
6864 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6865 gl_shaders[shader_data->num_gl_shaders].args = *args;
6866 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6868 return ret;
6871 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
6872 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6873 struct wined3d_shader *shader)
6875 struct glsl_cs_compiled_shader *gl_shaders;
6876 struct glsl_shader_private *shader_data;
6877 GLuint ret;
6879 if (!shader->backend_data)
6881 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6883 ERR("Failed to allocate backend data.\n");
6884 return 0;
6887 shader_data = shader->backend_data;
6888 gl_shaders = shader_data->gl_shaders.cs;
6890 /* No shader variants are used for compute shaders. */
6891 if (shader_data->num_gl_shaders)
6892 return gl_shaders[0].id;
6894 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6896 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
6898 ERR("Failed to allocate GL shader array.\n");
6899 return 0;
6901 shader_data->shader_array_size = 1;
6902 gl_shaders = shader_data->gl_shaders.cs;
6904 string_buffer_clear(buffer);
6905 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
6906 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6908 return ret;
6911 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6913 switch (mcs)
6915 case WINED3D_MCS_MATERIAL:
6916 return material;
6917 case WINED3D_MCS_COLOR1:
6918 return "ffp_attrib_diffuse";
6919 case WINED3D_MCS_COLOR2:
6920 return "ffp_attrib_specular";
6921 default:
6922 ERR("Invalid material color source %#x.\n", mcs);
6923 return "<invalid>";
6927 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6928 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6930 const char *diffuse, *specular, *emissive, *ambient;
6931 unsigned int i, idx;
6933 if (!settings->lighting)
6935 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6936 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6937 return;
6940 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6941 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6942 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6943 shader_addline(buffer, "vec3 dir, dst;\n");
6944 shader_addline(buffer, "float att, t;\n");
6946 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6947 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6948 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6949 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6951 idx = 0;
6952 for (i = 0; i < settings->point_light_count; ++i, ++idx)
6954 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
6955 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6956 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6957 shader_addline(buffer, "dst.x = 1.0;\n");
6958 if (legacy_lighting)
6960 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
6961 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
6963 else
6965 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
6967 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
6968 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
6969 if (!legacy_lighting)
6970 shader_addline(buffer, "att = 1.0 / att;\n");
6971 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
6972 if (!settings->normal)
6974 if (!legacy_lighting)
6975 shader_addline(buffer, "}\n");
6976 continue;
6978 shader_addline(buffer, "dir = normalize(dir);\n");
6979 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
6980 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
6981 if (settings->localviewer)
6982 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
6983 else
6984 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
6985 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
6986 " * ffp_light[%u].specular) * att;\n", idx);
6987 if (!legacy_lighting)
6988 shader_addline(buffer, "}\n");
6991 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
6993 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
6994 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6995 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6996 shader_addline(buffer, "dst.x = 1.0;\n");
6997 if (legacy_lighting)
6999 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7000 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7002 else
7004 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7006 shader_addline(buffer, "dir = normalize(dir);\n");
7007 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7008 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7009 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7010 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7011 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7012 idx, idx, idx, idx);
7013 if (legacy_lighting)
7014 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7015 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7016 idx, idx, idx);
7017 else
7018 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7019 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7020 idx, idx, idx);
7021 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7022 if (!settings->normal)
7024 if (!legacy_lighting)
7025 shader_addline(buffer, "}\n");
7026 continue;
7028 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7029 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7030 if (settings->localviewer)
7031 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7032 else
7033 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7034 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7035 " * ffp_light[%u].specular) * att;\n", idx);
7036 if (!legacy_lighting)
7037 shader_addline(buffer, "}\n");
7040 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7042 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7043 if (!settings->normal)
7044 continue;
7045 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7046 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7047 " * ffp_light[%u].diffuse.xyz;\n", idx);
7048 /* TODO: In the non-local viewer case the halfvector is constant
7049 * and could be precomputed and stored in a uniform. */
7050 if (settings->localviewer)
7051 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7052 else
7053 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7054 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7055 " * ffp_light[%u].specular;\n", idx);
7058 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7060 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7061 if (!settings->normal)
7062 continue;
7063 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7064 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7065 " * ffp_light[%u].diffuse.xyz;\n", idx);
7066 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7067 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7068 " * ffp_light[%u].specular;\n", idx);
7071 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7072 ambient, diffuse, emissive);
7073 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7074 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7077 /* Context activation is done by the caller. */
7078 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7079 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7081 static const struct attrib_info
7083 const char type[6];
7084 const char name[24];
7086 attrib_info[] =
7088 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7089 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7090 /* TODO: Indexed vertex blending */
7091 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7092 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7093 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7094 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7095 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7097 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7098 BOOL legacy_lighting = priv->legacy_lighting;
7099 GLuint shader_obj;
7100 unsigned int i;
7101 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7102 BOOL output_legacy_fogcoord = legacy_context;
7104 string_buffer_clear(buffer);
7106 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7108 if (shader_glsl_use_explicit_attrib_location(gl_info))
7109 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7111 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7113 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7115 if (shader_glsl_use_explicit_attrib_location(gl_info))
7116 shader_addline(buffer, "layout(location = %u) ", i);
7117 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7119 shader_addline(buffer, "\n");
7121 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7122 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7123 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7124 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7126 shader_addline(buffer, "uniform struct\n{\n");
7127 shader_addline(buffer, " vec4 emissive;\n");
7128 shader_addline(buffer, " vec4 ambient;\n");
7129 shader_addline(buffer, " vec4 diffuse;\n");
7130 shader_addline(buffer, " vec4 specular;\n");
7131 shader_addline(buffer, " float shininess;\n");
7132 shader_addline(buffer, "} ffp_material;\n");
7134 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7135 shader_addline(buffer, "uniform struct\n{\n");
7136 shader_addline(buffer, " vec4 diffuse;\n");
7137 shader_addline(buffer, " vec4 specular;\n");
7138 shader_addline(buffer, " vec4 ambient;\n");
7139 shader_addline(buffer, " vec4 position;\n");
7140 shader_addline(buffer, " vec3 direction;\n");
7141 shader_addline(buffer, " float range;\n");
7142 shader_addline(buffer, " float falloff;\n");
7143 shader_addline(buffer, " float c_att;\n");
7144 shader_addline(buffer, " float l_att;\n");
7145 shader_addline(buffer, " float q_att;\n");
7146 shader_addline(buffer, " float cos_htheta;\n");
7147 shader_addline(buffer, " float cos_hphi;\n");
7148 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7150 if (settings->point_size)
7152 shader_addline(buffer, "uniform struct\n{\n");
7153 shader_addline(buffer, " float size;\n");
7154 shader_addline(buffer, " float size_min;\n");
7155 shader_addline(buffer, " float size_max;\n");
7156 shader_addline(buffer, " float c_att;\n");
7157 shader_addline(buffer, " float l_att;\n");
7158 shader_addline(buffer, " float q_att;\n");
7159 shader_addline(buffer, "} ffp_point;\n");
7162 if (legacy_context)
7164 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7165 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7166 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7167 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7169 else
7171 if (settings->clipping)
7172 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7174 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7175 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7176 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7177 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7180 shader_addline(buffer, "\nvoid main()\n{\n");
7181 shader_addline(buffer, "float m;\n");
7182 shader_addline(buffer, "vec3 r;\n");
7184 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7186 if (attrib_info[i].name[0])
7187 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7188 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7190 for (i = 0; i < MAX_TEXTURES; ++i)
7192 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7193 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7194 && settings->texcoords & (1u << i))
7195 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7198 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7200 if (settings->transformed)
7202 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7203 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7204 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7206 else
7208 for (i = 0; i < settings->vertexblends; ++i)
7209 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7211 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7212 for (i = 0; i < settings->vertexblends + 1; ++i)
7213 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7215 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7216 if (settings->clipping)
7218 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7219 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7220 else
7221 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7222 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7224 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7227 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7228 if (settings->normal)
7230 if (!settings->vertexblends)
7232 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7234 else
7236 for (i = 0; i < settings->vertexblends + 1; ++i)
7237 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7240 if (settings->normalize)
7241 shader_addline(buffer, "normal = normalize(normal);\n");
7244 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7245 if (legacy_context)
7247 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7248 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7250 else
7252 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7253 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7256 for (i = 0; i < MAX_TEXTURES; ++i)
7258 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7260 switch (settings->texgen[i] & 0xffff0000)
7262 case WINED3DTSS_TCI_PASSTHRU:
7263 if (settings->texcoords & (1u << i))
7264 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7265 i, i, i);
7266 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7267 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7268 else
7269 output_legacy_texcoord = FALSE;
7270 break;
7272 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7273 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7274 break;
7276 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7277 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7278 break;
7280 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7281 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7282 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7283 break;
7285 case WINED3DTSS_TCI_SPHEREMAP:
7286 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7287 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7288 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7289 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7290 break;
7292 default:
7293 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7294 break;
7296 if (output_legacy_texcoord)
7297 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7300 switch (settings->fog_mode)
7302 case WINED3D_FFP_VS_FOG_OFF:
7303 output_legacy_fogcoord = FALSE;
7304 break;
7306 case WINED3D_FFP_VS_FOG_FOGCOORD:
7307 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7308 break;
7310 case WINED3D_FFP_VS_FOG_RANGE:
7311 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7312 break;
7314 case WINED3D_FFP_VS_FOG_DEPTH:
7315 if (settings->ortho_fog)
7317 if (gl_info->supported[ARB_CLIP_CONTROL])
7318 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7319 else
7320 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7321 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7323 else if (settings->transformed)
7325 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7327 else
7329 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7331 break;
7333 default:
7334 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7335 break;
7337 if (output_legacy_fogcoord)
7338 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7340 if (settings->point_size)
7342 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7343 " + ffp_point.l_att * length(ec_pos.xyz)"
7344 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7345 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7346 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7349 shader_addline(buffer, "}\n");
7351 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7352 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7354 return shader_obj;
7357 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7358 DWORD argnum, unsigned int stage, DWORD arg)
7360 const char *ret;
7362 if (arg == ARG_UNUSED)
7363 return "<unused arg>";
7365 switch (arg & WINED3DTA_SELECTMASK)
7367 case WINED3DTA_DIFFUSE:
7368 ret = "ffp_varying_diffuse";
7369 break;
7371 case WINED3DTA_CURRENT:
7372 ret = "ret";
7373 break;
7375 case WINED3DTA_TEXTURE:
7376 switch (stage)
7378 case 0: ret = "tex0"; break;
7379 case 1: ret = "tex1"; break;
7380 case 2: ret = "tex2"; break;
7381 case 3: ret = "tex3"; break;
7382 case 4: ret = "tex4"; break;
7383 case 5: ret = "tex5"; break;
7384 case 6: ret = "tex6"; break;
7385 case 7: ret = "tex7"; break;
7386 default:
7387 ret = "<invalid texture>";
7388 break;
7390 break;
7392 case WINED3DTA_TFACTOR:
7393 ret = "tex_factor";
7394 break;
7396 case WINED3DTA_SPECULAR:
7397 ret = "ffp_varying_specular";
7398 break;
7400 case WINED3DTA_TEMP:
7401 ret = "temp_reg";
7402 break;
7404 case WINED3DTA_CONSTANT:
7405 switch (stage)
7407 case 0: ret = "tss_const0"; break;
7408 case 1: ret = "tss_const1"; break;
7409 case 2: ret = "tss_const2"; break;
7410 case 3: ret = "tss_const3"; break;
7411 case 4: ret = "tss_const4"; break;
7412 case 5: ret = "tss_const5"; break;
7413 case 6: ret = "tss_const6"; break;
7414 case 7: ret = "tss_const7"; break;
7415 default:
7416 ret = "<invalid constant>";
7417 break;
7419 break;
7421 default:
7422 return "<unhandled arg>";
7425 if (arg & WINED3DTA_COMPLEMENT)
7427 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7428 if (argnum == 0)
7429 ret = "arg0";
7430 else if (argnum == 1)
7431 ret = "arg1";
7432 else if (argnum == 2)
7433 ret = "arg2";
7436 if (arg & WINED3DTA_ALPHAREPLICATE)
7438 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7439 if (argnum == 0)
7440 ret = "arg0";
7441 else if (argnum == 1)
7442 ret = "arg1";
7443 else if (argnum == 2)
7444 ret = "arg2";
7447 return ret;
7450 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7451 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7453 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7455 if (color && alpha)
7456 dstmask = "";
7457 else if (color)
7458 dstmask = ".xyz";
7459 else
7460 dstmask = ".w";
7462 if (dst == tempreg)
7463 dstreg = "temp_reg";
7464 else
7465 dstreg = "ret";
7467 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7468 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7469 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7471 switch (op)
7473 case WINED3D_TOP_DISABLE:
7474 break;
7476 case WINED3D_TOP_SELECT_ARG1:
7477 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7478 break;
7480 case WINED3D_TOP_SELECT_ARG2:
7481 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7482 break;
7484 case WINED3D_TOP_MODULATE:
7485 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7486 break;
7488 case WINED3D_TOP_MODULATE_4X:
7489 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7490 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7491 break;
7493 case WINED3D_TOP_MODULATE_2X:
7494 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7495 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7496 break;
7498 case WINED3D_TOP_ADD:
7499 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7500 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7501 break;
7503 case WINED3D_TOP_ADD_SIGNED:
7504 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7505 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7506 break;
7508 case WINED3D_TOP_ADD_SIGNED_2X:
7509 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7510 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7511 break;
7513 case WINED3D_TOP_SUBTRACT:
7514 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7515 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7516 break;
7518 case WINED3D_TOP_ADD_SMOOTH:
7519 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7520 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7521 break;
7523 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7524 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7525 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7526 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7527 break;
7529 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7530 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7531 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7532 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7533 break;
7535 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7536 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7537 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7538 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7539 break;
7541 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7542 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7543 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7544 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7545 break;
7547 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7548 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7549 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7550 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7551 break;
7553 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7554 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7555 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7556 break;
7558 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7559 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7560 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7561 break;
7563 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7564 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7565 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7566 break;
7567 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7568 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7569 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7570 break;
7572 case WINED3D_TOP_BUMPENVMAP:
7573 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7574 /* These are handled in the first pass, nothing to do. */
7575 break;
7577 case WINED3D_TOP_DOTPRODUCT3:
7578 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7579 dstreg, dstmask, arg1, arg2, dstmask);
7580 break;
7582 case WINED3D_TOP_MULTIPLY_ADD:
7583 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7584 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7585 break;
7587 case WINED3D_TOP_LERP:
7588 /* MSDN isn't quite right here. */
7589 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7590 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7591 break;
7593 default:
7594 FIXME("Unhandled operation %#x.\n", op);
7595 break;
7599 /* Context activation is done by the caller. */
7600 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7601 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7603 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7604 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7605 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7606 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7607 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7608 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7609 UINT lowest_disabled_stage;
7610 GLuint shader_id;
7611 DWORD arg0, arg1, arg2;
7612 unsigned int stage;
7614 string_buffer_clear(buffer);
7616 /* Find out which textures are read */
7617 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7619 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7620 break;
7622 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7623 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7624 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7626 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7627 || (stage == 0 && settings->color_key_enabled))
7628 tex_map |= 1u << stage;
7629 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7630 tfactor_used = TRUE;
7631 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7632 tempreg_used = TRUE;
7633 if (settings->op[stage].dst == tempreg)
7634 tempreg_used = TRUE;
7635 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7636 tss_const_map |= 1u << stage;
7638 switch (settings->op[stage].cop)
7640 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7641 lum_map |= 1u << stage;
7642 /* fall through */
7643 case WINED3D_TOP_BUMPENVMAP:
7644 bump_map |= 1u << stage;
7645 /* fall through */
7646 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7647 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7648 tex_map |= 1u << stage;
7649 break;
7651 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7652 tfactor_used = TRUE;
7653 break;
7655 default:
7656 break;
7659 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7660 continue;
7662 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7663 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7664 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7666 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7667 tex_map |= 1u << stage;
7668 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7669 tfactor_used = TRUE;
7670 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7671 tempreg_used = TRUE;
7672 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7673 tss_const_map |= 1u << stage;
7675 lowest_disabled_stage = stage;
7677 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7679 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7680 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7682 if (!needs_legacy_glsl_syntax(gl_info))
7683 shader_addline(buffer, "out vec4 ps_out[1];\n");
7685 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7686 shader_addline(buffer, "vec4 ret;\n");
7687 if (tempreg_used || settings->sRGB_write)
7688 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7689 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7691 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7693 if (tss_const_map & (1u << stage))
7694 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7696 if (!(tex_map & (1u << stage)))
7697 continue;
7699 switch (settings->op[stage].tex_type)
7701 case WINED3D_GL_RES_TYPE_TEX_1D:
7702 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7703 break;
7704 case WINED3D_GL_RES_TYPE_TEX_2D:
7705 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7706 break;
7707 case WINED3D_GL_RES_TYPE_TEX_3D:
7708 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7709 break;
7710 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7711 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7712 break;
7713 case WINED3D_GL_RES_TYPE_TEX_RECT:
7714 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7715 break;
7716 default:
7717 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7718 break;
7721 shader_addline(buffer, "vec4 tex%u;\n", stage);
7723 if (!(bump_map & (1u << stage)))
7724 continue;
7725 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7727 if (!(lum_map & (1u << stage)))
7728 continue;
7729 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7730 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7732 if (tfactor_used)
7733 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7734 if (settings->color_key_enabled)
7735 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7736 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7738 if (settings->sRGB_write)
7740 shader_addline(buffer, "const vec4 srgb_const0 = ");
7741 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7742 shader_addline(buffer, ";\n");
7743 shader_addline(buffer, "const vec4 srgb_const1 = ");
7744 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7745 shader_addline(buffer, ";\n");
7748 shader_addline(buffer, "uniform struct\n{\n");
7749 shader_addline(buffer, " vec4 color;\n");
7750 shader_addline(buffer, " float density;\n");
7751 shader_addline(buffer, " float end;\n");
7752 shader_addline(buffer, " float scale;\n");
7753 shader_addline(buffer, "} ffp_fog;\n");
7755 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7756 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7758 if (legacy_context)
7760 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7761 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7762 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7763 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7764 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7766 else
7768 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7769 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7770 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7771 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7772 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7775 shader_addline(buffer, "void main()\n{\n");
7777 if (legacy_context)
7779 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7780 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7783 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7785 if (tex_map & (1u << stage))
7787 if (settings->pointsprite)
7788 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7789 else if (settings->texcoords_initialized & (1u << stage))
7790 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7791 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7792 else
7793 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7797 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7798 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7800 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7801 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7803 /* Generate texture sampling instructions */
7804 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7806 const char *texture_function, *coord_mask;
7807 BOOL proj;
7809 if (!(tex_map & (1u << stage)))
7810 continue;
7812 if (settings->op[stage].projected == proj_none)
7814 proj = FALSE;
7816 else if (settings->op[stage].projected == proj_count4
7817 || settings->op[stage].projected == proj_count3)
7819 proj = TRUE;
7821 else
7823 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7824 proj = TRUE;
7827 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7828 proj = FALSE;
7830 switch (settings->op[stage].tex_type)
7832 case WINED3D_GL_RES_TYPE_TEX_1D:
7833 if (proj)
7835 texture_function = "texture1DProj";
7836 coord_mask = "xw";
7838 else
7840 texture_function = "texture1D";
7841 coord_mask = "x";
7843 break;
7844 case WINED3D_GL_RES_TYPE_TEX_2D:
7845 if (proj)
7847 texture_function = "texture2DProj";
7848 coord_mask = "xyw";
7850 else
7852 texture_function = "texture2D";
7853 coord_mask = "xy";
7855 break;
7856 case WINED3D_GL_RES_TYPE_TEX_3D:
7857 if (proj)
7859 texture_function = "texture3DProj";
7860 coord_mask = "xyzw";
7862 else
7864 texture_function = "texture3D";
7865 coord_mask = "xyz";
7867 break;
7868 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7869 texture_function = "textureCube";
7870 coord_mask = "xyz";
7871 break;
7872 case WINED3D_GL_RES_TYPE_TEX_RECT:
7873 if (proj)
7875 texture_function = "texture2DRectProj";
7876 coord_mask = "xyw";
7878 else
7880 texture_function = "texture2DRect";
7881 coord_mask = "xy";
7883 break;
7884 default:
7885 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7886 texture_function = "";
7887 coord_mask = "xyzw";
7888 break;
7890 if (!needs_legacy_glsl_syntax(gl_info))
7891 texture_function = proj ? "textureProj" : "texture";
7893 if (stage > 0
7894 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7895 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7897 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7899 /* With projective textures, texbem only divides the static
7900 * texture coord, not the displacement, so multiply the
7901 * displacement with the dividing parameter before passing it to
7902 * TXP. */
7903 if (settings->op[stage].projected != proj_none)
7905 if (settings->op[stage].projected == proj_count4)
7907 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7908 stage, stage);
7909 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7911 else
7913 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7914 stage, stage);
7915 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7918 else
7920 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7923 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7924 stage, texture_function, stage, coord_mask);
7926 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7927 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7928 stage, stage - 1, stage - 1, stage - 1);
7930 else if (settings->op[stage].projected == proj_count3)
7932 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7933 stage, texture_function, stage, stage);
7935 else
7937 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7938 stage, texture_function, stage, stage, coord_mask);
7941 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7942 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7943 settings->op[stage].color_fixup);
7946 if (settings->color_key_enabled)
7948 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7949 shader_addline(buffer, " discard;\n");
7952 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7954 /* Generate the main shader */
7955 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7957 BOOL op_equal;
7959 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7960 break;
7962 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7963 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7964 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
7965 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
7966 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7967 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
7968 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7969 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
7970 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
7971 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
7972 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
7973 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
7974 else
7975 op_equal = settings->op[stage].aop == settings->op[stage].cop
7976 && settings->op[stage].carg0 == settings->op[stage].aarg0
7977 && settings->op[stage].carg1 == settings->op[stage].aarg1
7978 && settings->op[stage].carg2 == settings->op[stage].aarg2;
7980 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7982 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7983 settings->op[stage].cop, settings->op[stage].carg0,
7984 settings->op[stage].carg1, settings->op[stage].carg2);
7986 else if (op_equal)
7988 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
7989 settings->op[stage].cop, settings->op[stage].carg0,
7990 settings->op[stage].carg1, settings->op[stage].carg2);
7992 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
7993 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7995 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
7996 settings->op[stage].cop, settings->op[stage].carg0,
7997 settings->op[stage].carg1, settings->op[stage].carg2);
7998 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
7999 settings->op[stage].aop, settings->op[stage].aarg0,
8000 settings->op[stage].aarg1, settings->op[stage].aarg2);
8004 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8005 get_fragment_output(gl_info));
8007 if (settings->sRGB_write)
8008 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8010 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8012 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8014 shader_addline(buffer, "}\n");
8016 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8017 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8019 string_buffer_release(&priv->string_buffers, tex_reg_name);
8020 return shader_id;
8023 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8024 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8026 struct glsl_ffp_vertex_shader *shader;
8027 const struct wine_rb_entry *entry;
8029 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8030 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8032 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8033 return NULL;
8035 shader->desc.settings = *settings;
8036 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8037 list_init(&shader->linked_programs);
8038 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8039 ERR("Failed to insert ffp vertex shader.\n");
8041 return shader;
8044 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8045 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8047 struct glsl_ffp_fragment_shader *glsl_desc;
8048 const struct ffp_frag_desc *desc;
8050 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8051 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8053 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8054 return NULL;
8056 glsl_desc->entry.settings = *args;
8057 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8058 list_init(&glsl_desc->linked_programs);
8059 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8061 return glsl_desc;
8065 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8066 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8068 unsigned int i;
8069 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8071 for (i = 0; i < vs_c_count; ++i)
8073 string_buffer_sprintf(name, "vs_c[%u]", i);
8074 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8076 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8078 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8080 string_buffer_sprintf(name, "vs_i[%u]", i);
8081 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8084 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8086 string_buffer_sprintf(name, "vs_b[%u]", i);
8087 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8090 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8092 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8094 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8095 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8097 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8098 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8099 for (i = 0; i < MAX_TEXTURES; ++i)
8101 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8102 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8104 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8105 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8106 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8107 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8108 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8109 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8110 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8112 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8113 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8114 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8115 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8116 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8117 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8118 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8119 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8120 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8121 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8122 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8123 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8124 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8125 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8126 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8127 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8128 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8129 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8130 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8131 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8132 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8133 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8134 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8135 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8137 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8138 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8139 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8140 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8141 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8142 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8143 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8145 string_buffer_release(&priv->string_buffers, name);
8148 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8149 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8151 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8154 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8155 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8157 unsigned int i;
8158 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8160 for (i = 0; i < ps_c_count; ++i)
8162 string_buffer_sprintf(name, "ps_c[%u]", i);
8163 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8165 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8167 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8169 string_buffer_sprintf(name, "ps_i[%u]", i);
8170 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8173 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8175 string_buffer_sprintf(name, "ps_b[%u]", i);
8176 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8179 for (i = 0; i < MAX_TEXTURES; ++i)
8181 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8182 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8183 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8184 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8185 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8186 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8187 string_buffer_sprintf(name, "tss_const%u", i);
8188 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8191 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8192 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8194 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8195 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8196 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8197 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8199 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8201 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8202 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8203 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8205 string_buffer_release(&priv->string_buffers, name);
8208 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
8209 struct shader_glsl_priv *priv, GLuint program_id,
8210 const struct wined3d_shader_reg_maps *reg_maps)
8212 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8213 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
8214 unsigned int i, base, count;
8215 GLuint block_idx;
8217 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
8218 for (i = 0; i < count; ++i)
8220 if (!reg_maps->cb_sizes[i])
8221 continue;
8223 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
8224 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
8225 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8227 checkGLcall("glUniformBlockBinding");
8228 string_buffer_release(&priv->string_buffers, name);
8231 /* Context activation is done by the caller. */
8232 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8233 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8235 const struct wined3d_gl_info *gl_info = context->gl_info;
8236 struct glsl_shader_prog_link *entry = NULL;
8237 struct wined3d_shader *shader;
8238 struct glsl_program_key key;
8239 GLuint program_id, cs_id;
8241 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8242 return;
8244 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8246 WARN("Compute shader is NULL.\n");
8247 ctx_data->glsl_program = NULL;
8248 return;
8251 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8252 memset(&key, 0, sizeof(key));
8253 key.cs_id = cs_id;
8254 if ((entry = get_glsl_program_entry(priv, &key)))
8256 ctx_data->glsl_program = entry;
8257 return;
8260 program_id = GL_EXTCALL(glCreateProgram());
8261 TRACE("Created new GLSL shader program %u.\n", program_id);
8263 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8265 ERR("Out of memory.\n");
8266 return;
8268 entry->id = program_id;
8269 entry->vs.id = 0;
8270 entry->gs.id = 0;
8271 entry->ps.id = 0;
8272 entry->cs.id = cs_id;
8273 entry->constant_version = 0;
8274 entry->ps.np2_fixup_info = NULL;
8275 add_glsl_program_entry(priv, entry);
8277 ctx_data->glsl_program = entry;
8279 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8280 GL_EXTCALL(glAttachShader(program_id, cs_id));
8281 checkGLcall("glAttachShader");
8283 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8285 TRACE("Linking GLSL shader program %u.\n", program_id);
8286 GL_EXTCALL(glLinkProgram(program_id));
8287 shader_glsl_validate_link(gl_info, program_id);
8289 GL_EXTCALL(glUseProgram(program_id));
8290 checkGLcall("glUseProgram");
8291 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &shader->reg_maps);
8292 shader_glsl_load_icb(gl_info, priv, program_id, &shader->reg_maps);
8293 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8295 entry->constant_update_mask = 0;
8298 /* Context activation is done by the caller. */
8299 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8300 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8302 const struct wined3d_gl_info *gl_info = context->gl_info;
8303 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8304 const struct ps_np2fixup_info *np2fixup_info = NULL;
8305 struct glsl_shader_prog_link *entry = NULL;
8306 struct wined3d_shader *vshader = NULL;
8307 struct wined3d_shader *gshader = NULL;
8308 struct wined3d_shader *pshader = NULL;
8309 GLuint reorder_shader_id = 0;
8310 struct glsl_program_key key;
8311 GLuint program_id;
8312 unsigned int i;
8313 GLuint vs_id = 0;
8314 GLuint gs_id = 0;
8315 GLuint ps_id = 0;
8316 struct list *ps_list, *vs_list;
8317 WORD attribs_map;
8318 struct wined3d_string_buffer *tmp_name;
8320 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8322 vs_id = ctx_data->glsl_program->vs.id;
8323 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8325 if (use_vs(state))
8327 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8328 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8330 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8331 && ctx_data->glsl_program->gs.id)
8333 gs_id = ctx_data->glsl_program->gs.id;
8335 else if (gshader)
8337 struct gs_compile_args args;
8339 find_gs_compile_args(state, gshader, &args);
8340 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8344 else if (use_vs(state))
8346 struct vs_compile_args vs_compile_args;
8348 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8349 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8351 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8352 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8353 vs_list = &vshader->linked_programs;
8355 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8356 && ctx_data->glsl_program->gs.id)
8358 gs_id = ctx_data->glsl_program->gs.id;
8360 else if (gshader)
8362 struct gs_compile_args gs_compile_args;
8364 find_gs_compile_args(state, gshader, &gs_compile_args);
8365 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8368 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8370 struct glsl_ffp_vertex_shader *ffp_shader;
8371 struct wined3d_ffp_vs_settings settings;
8373 wined3d_ffp_get_vs_settings(context, state, &settings);
8374 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8375 vs_id = ffp_shader->id;
8376 vs_list = &ffp_shader->linked_programs;
8379 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8381 ps_id = ctx_data->glsl_program->ps.id;
8382 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8384 if (use_ps(state))
8385 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8387 else if (use_ps(state))
8389 struct ps_compile_args ps_compile_args;
8390 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8391 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8392 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8393 pshader, &ps_compile_args, &np2fixup_info);
8394 ps_list = &pshader->linked_programs;
8396 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8398 struct glsl_ffp_fragment_shader *ffp_shader;
8399 struct ffp_frag_settings settings;
8401 gen_ffp_frag_op(context, state, &settings, FALSE);
8402 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8403 ps_id = ffp_shader->id;
8404 ps_list = &ffp_shader->linked_programs;
8407 key.vs_id = vs_id;
8408 key.gs_id = gs_id;
8409 key.ps_id = ps_id;
8410 key.cs_id = 0;
8411 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8413 ctx_data->glsl_program = entry;
8414 return;
8417 /* If we get to this point, then no matching program exists, so we create one */
8418 program_id = GL_EXTCALL(glCreateProgram());
8419 TRACE("Created new GLSL shader program %u.\n", program_id);
8421 /* Create the entry */
8422 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8423 entry->id = program_id;
8424 entry->vs.id = vs_id;
8425 entry->gs.id = gs_id;
8426 entry->ps.id = ps_id;
8427 entry->cs.id = 0;
8428 entry->constant_version = 0;
8429 entry->ps.np2_fixup_info = np2fixup_info;
8430 /* Add the hash table entry */
8431 add_glsl_program_entry(priv, entry);
8433 /* Set the current program */
8434 ctx_data->glsl_program = entry;
8436 /* Attach GLSL vshader */
8437 if (vs_id)
8439 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8440 GL_EXTCALL(glAttachShader(program_id, vs_id));
8441 checkGLcall("glAttachShader");
8443 list_add_head(vs_list, &entry->vs.shader_entry);
8446 if (vshader)
8448 attribs_map = vshader->reg_maps.input_registers;
8449 if (vshader->reg_maps.shader_version.major < 4)
8451 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8452 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8453 d3d_info->emulated_flatshading
8454 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8455 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8456 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8457 checkGLcall("glAttachShader");
8458 /* Flag the reorder function for deletion, it will be freed
8459 * automatically when the program is destroyed. */
8460 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8463 else
8465 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8468 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8470 /* Bind vertex attributes to a corresponding index number to match
8471 * the same index numbers as ARB_vertex_programs (makes loading
8472 * vertex attributes simpler). With this method, we can use the
8473 * exact same code to load the attributes later for both ARB and
8474 * GLSL shaders.
8476 * We have to do this here because we need to know the Program ID
8477 * in order to make the bindings work, and it has to be done prior
8478 * to linking the GLSL program. */
8479 tmp_name = string_buffer_get(&priv->string_buffers);
8480 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8482 if (!(attribs_map & 1))
8483 continue;
8485 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8486 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8487 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8489 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8490 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8491 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8492 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8495 checkGLcall("glBindAttribLocation");
8496 string_buffer_release(&priv->string_buffers, tmp_name);
8499 if (gshader)
8501 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8502 GL_EXTCALL(glAttachShader(program_id, gs_id));
8503 checkGLcall("glAttachShader");
8505 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8507 TRACE("input type %s, output type %s, vertices out %u.\n",
8508 debug_d3dprimitivetype(gshader->u.gs.input_type),
8509 debug_d3dprimitivetype(gshader->u.gs.output_type),
8510 gshader->u.gs.vertices_out);
8511 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8512 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8513 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8514 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8515 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8516 gshader->u.gs.vertices_out));
8517 checkGLcall("glProgramParameteriARB");
8520 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8523 /* Attach GLSL pshader */
8524 if (ps_id)
8526 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8527 GL_EXTCALL(glAttachShader(program_id, ps_id));
8528 checkGLcall("glAttachShader");
8530 list_add_head(ps_list, &entry->ps.shader_entry);
8533 /* Link the program */
8534 TRACE("Linking GLSL shader program %u.\n", program_id);
8535 GL_EXTCALL(glLinkProgram(program_id));
8536 shader_glsl_validate_link(gl_info, program_id);
8538 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8539 vshader ? vshader->limits->constant_float : 0);
8540 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8541 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8542 pshader ? pshader->limits->constant_float : 0);
8543 checkGLcall("Find glsl program uniform locations");
8545 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8547 if (pshader && pshader->reg_maps.shader_version.major >= 3
8548 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8550 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8551 entry->vs.vertex_color_clamp = GL_FALSE;
8553 else
8555 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8558 else
8560 /* With core profile we never change vertex_color_clamp from
8561 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8562 * glClampColorARB(). */
8563 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8566 /* Set the shader to allow uniform loading on it */
8567 GL_EXTCALL(glUseProgram(program_id));
8568 checkGLcall("glUseProgram");
8570 /* Texture unit mapping is set up to be the same each time the shader
8571 * program is used so we can hardcode the sampler uniform values. */
8572 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
8574 entry->constant_update_mask = 0;
8575 if (vshader)
8577 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8578 if (vshader->reg_maps.integer_constants)
8579 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8580 if (vshader->reg_maps.boolean_constants)
8581 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8582 if (entry->vs.pos_fixup_location != -1)
8583 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8585 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8586 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8588 else
8590 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8591 | WINED3D_SHADER_CONST_FFP_PROJ;
8593 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8595 if (entry->vs.modelview_matrix_location[i] != -1)
8597 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8598 break;
8602 for (i = 0; i < MAX_TEXTURES; ++i)
8604 if (entry->vs.texture_matrix_location[i] != -1)
8606 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8607 break;
8610 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
8611 || entry->vs.material_specular_location != -1
8612 || entry->vs.material_emissive_location != -1
8613 || entry->vs.material_shininess_location != -1)
8614 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8615 if (entry->vs.light_ambient_location != -1)
8616 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8618 if (entry->vs.clip_planes_location != -1)
8619 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8620 if (entry->vs.pointsize_min_location != -1)
8621 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8623 if (gshader)
8625 if (entry->gs.pos_fixup_location != -1)
8626 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8627 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8628 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8631 if (ps_id)
8633 if (pshader)
8635 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8636 if (pshader->reg_maps.integer_constants)
8637 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8638 if (pshader->reg_maps.boolean_constants)
8639 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8640 if (entry->ps.ycorrection_location != -1)
8641 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8643 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8644 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8645 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
8647 else
8649 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8652 for (i = 0; i < MAX_TEXTURES; ++i)
8654 if (entry->ps.bumpenv_mat_location[i] != -1)
8656 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8657 break;
8661 if (entry->ps.fog_color_location != -1)
8662 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8663 if (entry->ps.alpha_test_ref_location != -1)
8664 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8665 if (entry->ps.np2_fixup_location != -1)
8666 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8667 if (entry->ps.color_key_location != -1)
8668 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8672 /* Context activation is done by the caller. */
8673 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8674 const struct wined3d_state *state)
8676 struct glsl_context_data *ctx_data = context->shader_backend_data;
8677 const struct wined3d_gl_info *gl_info = context->gl_info;
8678 struct shader_glsl_priv *priv = shader_priv;
8679 GLenum current_vertex_color_clamp;
8680 GLuint program_id, prev_id;
8682 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8683 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8685 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8687 set_glsl_shader_program(context, state, priv, ctx_data);
8689 if (ctx_data->glsl_program)
8691 program_id = ctx_data->glsl_program->id;
8692 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8694 else
8696 program_id = 0;
8697 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8700 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
8702 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
8703 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8705 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8706 checkGLcall("glClampColorARB");
8708 else
8710 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
8714 TRACE("Using GLSL program %u.\n", program_id);
8716 if (prev_id != program_id)
8718 GL_EXTCALL(glUseProgram(program_id));
8719 checkGLcall("glUseProgram");
8721 if (program_id)
8722 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8725 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
8728 /* Context activation is done by the caller. */
8729 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
8730 const struct wined3d_state *state)
8732 struct glsl_context_data *ctx_data = context->shader_backend_data;
8733 const struct wined3d_gl_info *gl_info = context->gl_info;
8734 struct shader_glsl_priv *priv = shader_priv;
8735 GLuint program_id, prev_id;
8737 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8738 set_glsl_compute_shader_program(context, state, priv, ctx_data);
8739 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8741 TRACE("Using GLSL program %u.\n", program_id);
8743 if (prev_id != program_id)
8745 GL_EXTCALL(glUseProgram(program_id));
8746 checkGLcall("glUseProgram");
8749 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
8750 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8751 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8752 | (1u << WINED3D_SHADER_TYPE_HULL)
8753 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
8756 /* "context" is not necessarily the currently active context. */
8757 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8759 struct glsl_context_data *ctx_data = context->shader_backend_data;
8761 ctx_data->glsl_program = NULL;
8762 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8763 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8764 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8765 | (1u << WINED3D_SHADER_TYPE_HULL)
8766 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8767 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8770 /* Context activation is done by the caller. */
8771 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8773 struct glsl_context_data *ctx_data = context->shader_backend_data;
8774 const struct wined3d_gl_info *gl_info = context->gl_info;
8775 struct shader_glsl_priv *priv = shader_priv;
8777 shader_glsl_invalidate_current_program(context);
8778 GL_EXTCALL(glUseProgram(0));
8779 checkGLcall("glUseProgram");
8781 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8782 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8784 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8786 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
8787 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8788 checkGLcall("glClampColorARB");
8792 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8793 const struct glsl_shader_prog_link *program)
8795 const struct glsl_context_data *ctx_data;
8796 struct wined3d_context *context;
8797 unsigned int i;
8799 for (i = 0; i < device->context_count; ++i)
8801 context = device->contexts[i];
8802 ctx_data = context->shader_backend_data;
8804 if (ctx_data->glsl_program == program)
8805 shader_glsl_invalidate_current_program(context);
8809 static void shader_glsl_destroy(struct wined3d_shader *shader)
8811 struct glsl_shader_private *shader_data = shader->backend_data;
8812 struct wined3d_device *device = shader->device;
8813 struct shader_glsl_priv *priv = device->shader_priv;
8814 const struct wined3d_gl_info *gl_info;
8815 const struct list *linked_programs;
8816 struct wined3d_context *context;
8818 if (!shader_data || !shader_data->num_gl_shaders)
8820 HeapFree(GetProcessHeap(), 0, shader_data);
8821 shader->backend_data = NULL;
8822 return;
8825 context = context_acquire(device, NULL, 0);
8826 gl_info = context->gl_info;
8828 TRACE("Deleting linked programs.\n");
8829 linked_programs = &shader->linked_programs;
8830 if (linked_programs->next)
8832 struct glsl_shader_prog_link *entry, *entry2;
8833 UINT i;
8835 switch (shader->reg_maps.shader_version.type)
8837 case WINED3D_SHADER_TYPE_PIXEL:
8839 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8841 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8843 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8844 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8845 checkGLcall("glDeleteShader");
8847 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8849 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8850 struct glsl_shader_prog_link, ps.shader_entry)
8852 shader_glsl_invalidate_contexts_program(device, entry);
8853 delete_glsl_program_entry(priv, gl_info, entry);
8856 break;
8859 case WINED3D_SHADER_TYPE_VERTEX:
8861 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8863 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8865 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8866 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8867 checkGLcall("glDeleteShader");
8869 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8871 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8872 struct glsl_shader_prog_link, vs.shader_entry)
8874 shader_glsl_invalidate_contexts_program(device, entry);
8875 delete_glsl_program_entry(priv, gl_info, entry);
8878 break;
8881 case WINED3D_SHADER_TYPE_GEOMETRY:
8883 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8885 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8887 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8888 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8889 checkGLcall("glDeleteShader");
8891 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8893 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8894 struct glsl_shader_prog_link, gs.shader_entry)
8896 shader_glsl_invalidate_contexts_program(device, entry);
8897 delete_glsl_program_entry(priv, gl_info, entry);
8900 break;
8903 case WINED3D_SHADER_TYPE_COMPUTE:
8905 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
8907 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8909 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
8910 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8911 checkGLcall("glDeleteShader");
8913 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
8915 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8916 struct glsl_shader_prog_link, cs.shader_entry)
8918 shader_glsl_invalidate_contexts_program(device, entry);
8919 delete_glsl_program_entry(priv, gl_info, entry);
8922 break;
8925 default:
8926 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8927 break;
8931 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8932 shader->backend_data = NULL;
8934 context_release(context);
8937 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8939 const struct glsl_program_key *k = key;
8940 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8941 const struct glsl_shader_prog_link, program_lookup_entry);
8943 if (k->vs_id > prog->vs.id) return 1;
8944 else if (k->vs_id < prog->vs.id) return -1;
8946 if (k->gs_id > prog->gs.id) return 1;
8947 else if (k->gs_id < prog->gs.id) return -1;
8949 if (k->ps_id > prog->ps.id) return 1;
8950 else if (k->ps_id < prog->ps.id) return -1;
8952 if (k->cs_id > prog->cs.id) return 1;
8953 else if (k->cs_id < prog->cs.id) return -1;
8955 return 0;
8958 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8960 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8961 + constant_count * sizeof(*heap->contained)
8962 + constant_count * sizeof(*heap->positions);
8963 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
8965 if (!mem)
8967 ERR("Failed to allocate memory\n");
8968 return FALSE;
8971 heap->entries = mem;
8972 heap->entries[1].version = 0;
8973 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
8974 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
8975 heap->positions = (unsigned int *)(heap->contained + constant_count);
8976 heap->size = 1;
8978 return TRUE;
8981 static void constant_heap_free(struct constant_heap *heap)
8983 HeapFree(GetProcessHeap(), 0, heap->entries);
8986 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
8987 const struct fragment_pipeline *fragment_pipe)
8989 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
8990 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
8991 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
8992 struct fragment_caps fragment_caps;
8993 void *vertex_priv, *fragment_priv;
8995 string_buffer_list_init(&priv->string_buffers);
8997 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
8999 ERR("Failed to initialize vertex pipe.\n");
9000 HeapFree(GetProcessHeap(), 0, priv);
9001 return E_FAIL;
9004 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9006 ERR("Failed to initialize fragment pipe.\n");
9007 vertex_pipe->vp_free(device);
9008 HeapFree(GetProcessHeap(), 0, priv);
9009 return E_FAIL;
9012 if (!string_buffer_init(&priv->shader_buffer))
9014 ERR("Failed to initialize shader buffer.\n");
9015 goto fail;
9018 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9020 ERR("Failed to allocate memory.\n");
9021 goto fail;
9024 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9026 ERR("Failed to initialize vertex shader constant heap\n");
9027 goto fail;
9030 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9032 ERR("Failed to initialize pixel shader constant heap\n");
9033 goto fail;
9036 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9038 priv->next_constant_version = 1;
9039 priv->vertex_pipe = vertex_pipe;
9040 priv->fragment_pipe = fragment_pipe;
9041 fragment_pipe->get_caps(gl_info, &fragment_caps);
9042 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9043 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9045 device->vertex_priv = vertex_priv;
9046 device->fragment_priv = fragment_priv;
9047 device->shader_priv = priv;
9049 return WINED3D_OK;
9051 fail:
9052 constant_heap_free(&priv->pconst_heap);
9053 constant_heap_free(&priv->vconst_heap);
9054 HeapFree(GetProcessHeap(), 0, priv->stack);
9055 string_buffer_free(&priv->shader_buffer);
9056 fragment_pipe->free_private(device);
9057 vertex_pipe->vp_free(device);
9058 HeapFree(GetProcessHeap(), 0, priv);
9059 return E_OUTOFMEMORY;
9062 /* Context activation is done by the caller. */
9063 static void shader_glsl_free(struct wined3d_device *device)
9065 struct shader_glsl_priv *priv = device->shader_priv;
9067 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9068 constant_heap_free(&priv->pconst_heap);
9069 constant_heap_free(&priv->vconst_heap);
9070 HeapFree(GetProcessHeap(), 0, priv->stack);
9071 string_buffer_list_cleanup(&priv->string_buffers);
9072 string_buffer_free(&priv->shader_buffer);
9073 priv->fragment_pipe->free_private(device);
9074 priv->vertex_pipe->vp_free(device);
9076 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9077 device->shader_priv = NULL;
9080 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9082 struct glsl_context_data *ctx_data;
9083 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9084 return FALSE;
9085 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9086 context->shader_backend_data = ctx_data;
9087 return TRUE;
9090 static void shader_glsl_free_context_data(struct wined3d_context *context)
9092 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9095 static void shader_glsl_init_context_state(struct wined3d_context *context)
9097 const struct wined3d_gl_info *gl_info = context->gl_info;
9099 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9100 checkGLcall("GL_PROGRAM_POINT_SIZE");
9103 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9105 UINT shader_model;
9107 /* FIXME: Check for the specific extensions required for SM5 support
9108 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9109 * soon as we introduce them, adjusting the GL / GLSL version checks
9110 * accordingly. */
9111 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9112 && gl_info->supported[ARB_COMPUTE_SHADER]
9113 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9114 && gl_info->supported[ARB_GPU_SHADER5]
9115 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9116 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9117 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9118 shader_model = 5;
9119 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9120 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9121 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9122 shader_model = 4;
9123 /* Support for texldd and texldl instructions in pixel shaders is required
9124 * for SM3. */
9125 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9126 shader_model = 3;
9127 else
9128 shader_model = 2;
9129 TRACE("Shader model %u.\n", shader_model);
9131 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9132 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9133 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9134 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9135 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9136 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9138 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9139 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9140 caps->varying_count = gl_info->limits.glsl_varyings;
9142 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9143 * Direct3D minimum requirement.
9145 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9146 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9148 * The problem is that the refrast clamps temporary results in the shader to
9149 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9150 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9151 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9152 * offer a way to query this.
9154 if (shader_model >= 4)
9155 caps->ps_1x_max_value = FLT_MAX;
9156 else
9157 caps->ps_1x_max_value = 1024.0f;
9159 /* Ideally we'd only set caps like sRGB writes here if supported by both
9160 * the shader backend and the fragment pipe, but we can get called before
9161 * shader_glsl_alloc(). */
9162 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9163 | WINED3D_SHADER_CAP_SRGB_WRITE;
9166 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9168 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9170 TRACE("Checking support for fixup:\n");
9171 dump_color_fixup_desc(fixup);
9174 /* We support everything except YUV conversions. */
9175 if (!is_complex_fixup(fixup))
9177 TRACE("[OK]\n");
9178 return TRUE;
9181 TRACE("[FAILED]\n");
9182 return FALSE;
9185 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9187 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9188 /* WINED3DSIH_ADD */ shader_glsl_binop,
9189 /* WINED3DSIH_AND */ shader_glsl_binop,
9190 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9191 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9192 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9193 /* WINED3DSIH_ATOMIC_IMAX */ NULL,
9194 /* WINED3DSIH_ATOMIC_IMIN */ NULL,
9195 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9196 /* WINED3DSIH_ATOMIC_UMAX */ NULL,
9197 /* WINED3DSIH_ATOMIC_UMIN */ NULL,
9198 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9199 /* WINED3DSIH_BEM */ shader_glsl_bem,
9200 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9201 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9202 /* WINED3DSIH_BREAK */ shader_glsl_break,
9203 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9204 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9205 /* WINED3DSIH_BUFINFO */ NULL,
9206 /* WINED3DSIH_CALL */ shader_glsl_call,
9207 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9208 /* WINED3DSIH_CASE */ shader_glsl_case,
9209 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9210 /* WINED3DSIH_CND */ shader_glsl_cnd,
9211 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9212 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9213 /* WINED3DSIH_CRS */ shader_glsl_cross,
9214 /* WINED3DSIH_CUT */ shader_glsl_cut,
9215 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9216 /* WINED3DSIH_DCL */ shader_glsl_nop,
9217 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9218 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9219 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9220 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9221 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9222 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9223 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9224 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9225 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9226 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9227 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9228 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9229 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9230 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9231 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9232 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9233 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9234 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9235 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9236 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9237 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9238 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9239 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
9240 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9241 /* WINED3DSIH_DCL_STREAM */ NULL,
9242 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9243 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9244 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9245 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9246 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
9247 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
9248 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9249 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9250 /* WINED3DSIH_DCL_UAV_STRUCTURED */ NULL,
9251 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9252 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9253 /* WINED3DSIH_DEF */ shader_glsl_nop,
9254 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9255 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9256 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9257 /* WINED3DSIH_DIV */ shader_glsl_binop,
9258 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9259 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9260 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9261 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9262 /* WINED3DSIH_DST */ shader_glsl_dst,
9263 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9264 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9265 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9266 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9267 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9268 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9269 /* WINED3DSIH_ELSE */ shader_glsl_else,
9270 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9271 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9272 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9273 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9274 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9275 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9276 /* WINED3DSIH_EQ */ shader_glsl_relop,
9277 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9278 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9279 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9280 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9281 /* WINED3DSIH_FCALL */ NULL,
9282 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9283 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9284 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9285 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9286 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9287 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9288 /* WINED3DSIH_GATHER4 */ NULL,
9289 /* WINED3DSIH_GATHER4_C */ NULL,
9290 /* WINED3DSIH_GE */ shader_glsl_relop,
9291 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9292 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9293 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9294 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9295 /* WINED3DSIH_IADD */ shader_glsl_binop,
9296 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9297 /* WINED3DSIH_IF */ shader_glsl_if,
9298 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9299 /* WINED3DSIH_IGE */ shader_glsl_relop,
9300 /* WINED3DSIH_ILT */ shader_glsl_relop,
9301 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9302 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9303 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9304 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
9305 /* WINED3DSIH_IMM_ATOMIC_AND */ NULL,
9306 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ NULL,
9307 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
9308 /* WINED3DSIH_IMM_ATOMIC_EXCH */ NULL,
9309 /* WINED3DSIH_IMM_ATOMIC_IADD */ NULL,
9310 /* WINED3DSIH_IMM_ATOMIC_OR */ NULL,
9311 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL,
9312 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL,
9313 /* WINED3DSIH_IMM_ATOMIC_XOR */ NULL,
9314 /* WINED3DSIH_IMUL */ shader_glsl_imul,
9315 /* WINED3DSIH_INE */ shader_glsl_relop,
9316 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9317 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9318 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9319 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9320 /* WINED3DSIH_LABEL */ shader_glsl_label,
9321 /* WINED3DSIH_LD */ shader_glsl_ld,
9322 /* WINED3DSIH_LD2DMS */ NULL,
9323 /* WINED3DSIH_LD_RAW */ NULL,
9324 /* WINED3DSIH_LD_STRUCTURED */ NULL,
9325 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9326 /* WINED3DSIH_LIT */ shader_glsl_lit,
9327 /* WINED3DSIH_LOD */ NULL,
9328 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9329 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9330 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9331 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9332 /* WINED3DSIH_LT */ shader_glsl_relop,
9333 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9334 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9335 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9336 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9337 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9338 /* WINED3DSIH_MAD */ shader_glsl_mad,
9339 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9340 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9341 /* WINED3DSIH_MOV */ shader_glsl_mov,
9342 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9343 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9344 /* WINED3DSIH_MUL */ shader_glsl_binop,
9345 /* WINED3DSIH_NE */ shader_glsl_relop,
9346 /* WINED3DSIH_NOP */ shader_glsl_nop,
9347 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9348 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9349 /* WINED3DSIH_OR */ shader_glsl_binop,
9350 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9351 /* WINED3DSIH_POW */ shader_glsl_pow,
9352 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9353 /* WINED3DSIH_REP */ shader_glsl_rep,
9354 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9355 /* WINED3DSIH_RET */ shader_glsl_ret,
9356 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9357 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9358 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9359 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9360 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9361 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9362 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9363 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9364 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9365 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9366 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9367 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9368 /* WINED3DSIH_SAMPLE_POS */ NULL,
9369 /* WINED3DSIH_SETP */ NULL,
9370 /* WINED3DSIH_SGE */ shader_glsl_compare,
9371 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9372 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9373 /* WINED3DSIH_SLT */ shader_glsl_compare,
9374 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9375 /* WINED3DSIH_STORE_RAW */ NULL,
9376 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
9377 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9378 /* WINED3DSIH_SUB */ shader_glsl_binop,
9379 /* WINED3DSIH_SWAPC */ NULL,
9380 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9381 /* WINED3DSIH_SYNC */ NULL,
9382 /* WINED3DSIH_TEX */ shader_glsl_tex,
9383 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9384 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9385 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9386 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9387 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9388 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9389 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9390 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9391 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9392 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9393 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9394 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9395 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9396 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9397 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9398 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9399 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9400 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9401 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9402 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9403 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9404 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9405 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9406 /* WINED3DSIH_UGE */ shader_glsl_relop,
9407 /* WINED3DSIH_ULT */ shader_glsl_relop,
9408 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9409 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9410 /* WINED3DSIH_USHR */ shader_glsl_binop,
9411 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9412 /* WINED3DSIH_XOR */ shader_glsl_binop,
9415 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9416 SHADER_HANDLER hw_fct;
9418 /* Select handler */
9419 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9421 /* Unhandled opcode */
9422 if (!hw_fct)
9424 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9425 return;
9427 hw_fct(ins);
9429 shader_glsl_add_instruction_modifiers(ins);
9432 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9434 struct shader_glsl_priv *priv = shader_priv;
9436 return priv->ffp_proj_control;
9439 const struct wined3d_shader_backend_ops glsl_shader_backend =
9441 shader_glsl_handle_instruction,
9442 shader_glsl_select,
9443 shader_glsl_select_compute,
9444 shader_glsl_disable,
9445 shader_glsl_update_float_vertex_constants,
9446 shader_glsl_update_float_pixel_constants,
9447 shader_glsl_load_constants,
9448 shader_glsl_destroy,
9449 shader_glsl_alloc,
9450 shader_glsl_free,
9451 shader_glsl_allocate_context_data,
9452 shader_glsl_free_context_data,
9453 shader_glsl_init_context_state,
9454 shader_glsl_get_caps,
9455 shader_glsl_color_fixup_supported,
9456 shader_glsl_has_ffp_proj_control,
9459 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9461 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9463 caps->xyzrhw = TRUE;
9464 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9465 caps->ffp_generic_attributes = TRUE;
9466 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9467 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9468 caps->max_vertex_blend_matrix_index = 0;
9469 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9470 | WINED3DVTXPCAPS_MATERIALSOURCE7
9471 | WINED3DVTXPCAPS_VERTEXFOG
9472 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9473 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9474 | WINED3DVTXPCAPS_LOCALVIEWER
9475 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9476 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9477 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9478 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9481 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9483 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9484 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9485 return 0;
9488 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9490 struct shader_glsl_priv *priv;
9492 if (shader_backend == &glsl_shader_backend)
9494 priv = shader_priv;
9495 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9496 return priv;
9499 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9501 return NULL;
9504 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9506 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9507 struct glsl_ffp_vertex_shader, desc.entry);
9508 struct glsl_shader_prog_link *program, *program2;
9509 struct glsl_ffp_destroy_ctx *ctx = context;
9511 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9512 struct glsl_shader_prog_link, vs.shader_entry)
9514 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9516 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9517 HeapFree(GetProcessHeap(), 0, shader);
9520 /* Context activation is done by the caller. */
9521 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9523 struct shader_glsl_priv *priv = device->vertex_priv;
9524 struct glsl_ffp_destroy_ctx ctx;
9526 ctx.priv = priv;
9527 ctx.gl_info = &device->adapter->gl_info;
9528 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9531 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9532 const struct wined3d_state *state, DWORD state_id) {}
9534 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9535 const struct wined3d_state *state, DWORD state_id)
9537 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9540 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9541 const struct wined3d_state *state, DWORD state_id)
9543 const struct wined3d_gl_info *gl_info = context->gl_info;
9544 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9545 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9546 BOOL transformed = context->stream_info.position_transformed;
9547 BOOL wasrhw = context->last_was_rhw;
9548 unsigned int i;
9550 context->last_was_rhw = transformed;
9552 /* If the vertex declaration contains a transformed position attribute,
9553 * the draw uses the fixed function vertex pipeline regardless of any
9554 * vertex shader set by the application. */
9555 if (transformed != wasrhw
9556 || context->stream_info.swizzle_map != context->last_swizzle_map)
9557 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9559 context->last_swizzle_map = context->stream_info.swizzle_map;
9561 if (!use_vs(state))
9563 if (context->last_was_vshader)
9565 if (legacy_context)
9566 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9567 clipplane(context, state, STATE_CLIPPLANE(i));
9568 else
9569 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9572 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9574 /* Because of settings->texcoords, we have to regenerate the vertex
9575 * shader on a vdecl change if there aren't enough varyings to just
9576 * always output all the texture coordinates. */
9577 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9578 || normal != context->last_was_normal)
9579 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9581 if (use_ps(state)
9582 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9583 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9584 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9586 else
9588 if (!context->last_was_vshader)
9590 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9591 if (legacy_context)
9592 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9593 clipplane(context, state, STATE_CLIPPLANE(i));
9594 else
9595 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9599 context->last_was_vshader = use_vs(state);
9600 context->last_was_normal = normal;
9603 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9604 const struct wined3d_state *state, DWORD state_id)
9606 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9607 /* Different vertex shaders potentially require a different vertex attributes setup. */
9608 if (!isStateDirty(context, STATE_VDECL))
9609 context_apply_state(context, state, STATE_VDECL);
9612 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9613 const struct wined3d_state *state, DWORD state_id)
9615 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9616 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9617 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9620 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9621 const struct wined3d_state *state, DWORD state_id)
9623 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9624 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9625 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9626 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9627 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9630 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9631 const struct wined3d_state *state, DWORD state_id)
9633 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9636 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9637 const struct wined3d_state *state, DWORD state_id)
9639 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9642 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9644 const struct wined3d_gl_info *gl_info = context->gl_info;
9645 unsigned int k;
9647 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9648 | WINED3D_SHADER_CONST_FFP_LIGHTS
9649 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9651 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9653 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9655 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9656 clipplane(context, state, STATE_CLIPPLANE(k));
9659 else
9661 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9665 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9666 const struct wined3d_state *state, DWORD state_id)
9668 /* Table fog behavior depends on the projection matrix. */
9669 if (state->render_states[WINED3D_RS_FOGENABLE]
9670 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9671 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9672 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9675 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9676 const struct wined3d_state *state, DWORD state_id)
9678 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9679 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9680 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9681 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9682 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9683 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9686 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9687 const struct wined3d_state *state, DWORD state_id)
9689 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9692 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9693 const struct wined3d_state *state, DWORD state_id)
9695 DWORD sampler = state_id - STATE_SAMPLER(0);
9696 const struct wined3d_texture *texture = state->textures[sampler];
9697 BOOL np2;
9699 if (!texture)
9700 return;
9702 if (sampler >= MAX_TEXTURES)
9703 return;
9705 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9706 || context->lastWasPow2Texture & (1u << sampler))
9708 if (np2)
9709 context->lastWasPow2Texture |= 1u << sampler;
9710 else
9711 context->lastWasPow2Texture &= ~(1u << sampler);
9713 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9717 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9718 const struct wined3d_state *state, DWORD state_id)
9720 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9723 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9724 const struct wined3d_state *state, DWORD state_id)
9726 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9729 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9730 const struct wined3d_state *state, DWORD state_id)
9732 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9735 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9736 const struct wined3d_state *state, DWORD state_id)
9738 if (!use_vs(state))
9739 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9742 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9743 const struct wined3d_state *state, DWORD state_id)
9745 static unsigned int once;
9747 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9748 FIXME("Non-point sprite points not supported in core profile.\n");
9751 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9752 const struct wined3d_state *state, DWORD state_id)
9754 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9757 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9758 const struct wined3d_state *state, DWORD state_id)
9760 const struct wined3d_gl_info *gl_info = context->gl_info;
9761 UINT index = state_id - STATE_CLIPPLANE(0);
9763 if (index >= gl_info->limits.user_clip_distances)
9764 return;
9766 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9769 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9771 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9772 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9773 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9774 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9775 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9776 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9777 /* Clip planes */
9778 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9779 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9780 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9781 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9782 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9783 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9784 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9785 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9786 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9787 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9788 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9789 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9790 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9791 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9792 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9793 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9794 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9795 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9796 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9797 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9798 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9799 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9800 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9801 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9802 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9803 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9804 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9805 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9806 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9807 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9808 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9809 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9810 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9811 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9812 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9813 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9814 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9815 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9816 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9817 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9818 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9819 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9820 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9821 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9822 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9823 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9824 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9825 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9826 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9827 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9828 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9829 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9830 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9831 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9832 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9833 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9834 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9835 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9836 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9837 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9838 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9839 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9840 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9841 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9842 /* Lights */
9843 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9844 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9845 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9846 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9847 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9848 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9849 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9850 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9851 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9852 /* Viewport */
9853 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9854 /* Transform states */
9855 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9856 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9857 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9858 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9859 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9860 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9861 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9862 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9863 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9864 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9865 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9866 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9867 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9868 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9869 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9870 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9871 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9872 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9873 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9874 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9875 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9876 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9877 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9878 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9879 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9880 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9881 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9882 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9883 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9884 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9885 /* Fog */
9886 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9887 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9888 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9889 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9890 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9891 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9892 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9893 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9894 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9895 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9896 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9897 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9898 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9899 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9900 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9901 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9902 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9903 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9904 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9905 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9906 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9907 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9908 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9909 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9910 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9911 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9912 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9913 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9914 /* NP2 texture matrix fixups. They are not needed if
9915 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9916 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9917 * matrix. */
9918 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9919 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9920 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9921 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9922 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9923 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9924 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9925 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9926 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9927 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9928 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9929 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9930 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9931 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9932 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9933 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9934 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9935 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9936 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9937 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9938 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9939 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9940 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9941 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9942 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9943 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9944 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9945 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9948 /* TODO:
9949 * - Implement vertex tweening. */
9950 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9952 glsl_vertex_pipe_vp_enable,
9953 glsl_vertex_pipe_vp_get_caps,
9954 glsl_vertex_pipe_vp_get_emul_mask,
9955 glsl_vertex_pipe_vp_alloc,
9956 glsl_vertex_pipe_vp_free,
9957 glsl_vertex_pipe_vp_states,
9960 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
9962 /* Nothing to do. */
9965 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
9967 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
9968 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
9969 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
9970 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
9971 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
9972 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
9973 | WINED3DTEXOPCAPS_SELECTARG1
9974 | WINED3DTEXOPCAPS_SELECTARG2
9975 | WINED3DTEXOPCAPS_MODULATE4X
9976 | WINED3DTEXOPCAPS_MODULATE2X
9977 | WINED3DTEXOPCAPS_MODULATE
9978 | WINED3DTEXOPCAPS_ADDSIGNED2X
9979 | WINED3DTEXOPCAPS_ADDSIGNED
9980 | WINED3DTEXOPCAPS_ADD
9981 | WINED3DTEXOPCAPS_SUBTRACT
9982 | WINED3DTEXOPCAPS_ADDSMOOTH
9983 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
9984 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
9985 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
9986 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
9987 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
9988 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
9989 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
9990 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
9991 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
9992 | WINED3DTEXOPCAPS_DOTPRODUCT3
9993 | WINED3DTEXOPCAPS_MULTIPLYADD
9994 | WINED3DTEXOPCAPS_LERP
9995 | WINED3DTEXOPCAPS_BUMPENVMAP
9996 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
9997 caps->MaxTextureBlendStages = MAX_TEXTURES;
9998 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10001 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10003 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10004 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10005 return 0;
10008 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10010 struct shader_glsl_priv *priv;
10012 if (shader_backend == &glsl_shader_backend)
10014 priv = shader_priv;
10015 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10016 return priv;
10019 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10021 return NULL;
10024 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10026 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10027 struct glsl_ffp_fragment_shader, entry.entry);
10028 struct glsl_shader_prog_link *program, *program2;
10029 struct glsl_ffp_destroy_ctx *ctx = context;
10031 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10032 struct glsl_shader_prog_link, ps.shader_entry)
10034 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10036 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10037 HeapFree(GetProcessHeap(), 0, shader);
10040 /* Context activation is done by the caller. */
10041 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10043 struct shader_glsl_priv *priv = device->fragment_priv;
10044 struct glsl_ffp_destroy_ctx ctx;
10046 ctx.priv = priv;
10047 ctx.gl_info = &device->adapter->gl_info;
10048 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10051 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10052 const struct wined3d_state *state, DWORD state_id)
10054 context->last_was_pshader = use_ps(state);
10056 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10059 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10060 const struct wined3d_state *state, DWORD state_id)
10062 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10065 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10066 const struct wined3d_state *state, DWORD state_id)
10068 BOOL use_vshader = use_vs(state);
10069 enum fogsource new_source;
10070 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10071 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10073 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10075 if (!state->render_states[WINED3D_RS_FOGENABLE])
10076 return;
10078 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10080 if (use_vshader)
10081 new_source = FOGSOURCE_VS;
10082 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10083 new_source = FOGSOURCE_COORD;
10084 else
10085 new_source = FOGSOURCE_FFP;
10087 else
10089 new_source = FOGSOURCE_FFP;
10092 if (new_source != context->fog_source || fogstart == fogend)
10094 context->fog_source = new_source;
10095 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10099 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10100 const struct wined3d_state *state, DWORD state_id)
10102 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10103 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10104 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10106 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10107 glsl_fragment_pipe_fog(context, state, state_id);
10110 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10111 const struct wined3d_state *state, DWORD state_id)
10113 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10114 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10115 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10118 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10119 const struct wined3d_state *state, DWORD state_id)
10121 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10124 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10125 const struct wined3d_state *state, DWORD state_id)
10127 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10130 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10131 const struct wined3d_state *state, DWORD state_id)
10133 const struct wined3d_gl_info *gl_info = context->gl_info;
10134 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10135 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10137 if (func)
10139 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10140 checkGLcall("glAlphaFunc");
10144 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10145 const struct wined3d_state *state, DWORD state_id)
10147 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10150 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10151 const struct wined3d_state *state, DWORD state_id)
10153 const struct wined3d_gl_info *gl_info = context->gl_info;
10155 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10157 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10158 checkGLcall("glEnable(GL_ALPHA_TEST)");
10160 else
10162 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10163 checkGLcall("glDisable(GL_ALPHA_TEST)");
10167 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10168 const struct wined3d_state *state, DWORD state_id)
10170 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10173 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10174 const struct wined3d_state *state, DWORD state_id)
10176 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10179 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10180 const struct wined3d_state *state, DWORD state_id)
10182 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10185 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10187 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10188 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10189 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10190 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10191 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10192 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10193 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10194 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10195 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10196 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10197 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10198 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10199 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10200 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10201 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10202 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10203 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10204 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10205 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10206 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10207 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10208 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10209 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10210 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10211 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10212 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10213 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10214 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10215 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10216 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10217 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10218 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10219 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10220 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10221 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10222 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10223 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10224 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10225 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10226 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10227 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10228 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10229 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10230 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10231 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10232 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10233 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10234 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10235 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10236 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10237 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10238 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10239 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10240 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10241 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10242 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10243 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10244 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10245 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10246 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10247 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10248 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10249 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10250 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10251 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10252 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10253 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10254 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10255 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10256 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10257 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10258 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10259 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10260 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10261 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10262 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10263 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10264 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10265 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10266 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10267 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10268 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10269 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10270 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10271 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10272 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10273 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10274 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10275 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10276 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10277 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10278 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10279 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10280 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10281 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10282 {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 },
10283 {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 },
10284 {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 },
10285 {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 },
10286 {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 },
10287 {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 },
10288 {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 },
10289 {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 },
10290 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10291 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10292 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10293 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10294 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10295 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10296 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10297 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10298 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10299 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10300 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10301 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10302 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10305 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10307 return TRUE;
10310 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10314 const struct fragment_pipeline glsl_fragment_pipe =
10316 glsl_fragment_pipe_enable,
10317 glsl_fragment_pipe_get_caps,
10318 glsl_fragment_pipe_get_emul_mask,
10319 glsl_fragment_pipe_alloc,
10320 glsl_fragment_pipe_free,
10321 glsl_fragment_pipe_alloc_context_data,
10322 glsl_fragment_pipe_free_context_data,
10323 shader_glsl_color_fixup_supported,
10324 glsl_fragment_pipe_state_template,