wined3d: Recognize SM5 imm_atomic_imax opcode.
[wine.git] / dlls / wined3d / glsl_shader.c
blobff24a928103e888d886e3516e3cb36488c0db3f1
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 BOOL is_imm_instruction = WINED3DSIH_IMM_ATOMIC_AND <= ins->handler_idx
4928 && ins->handler_idx <= WINED3DSIH_IMM_ATOMIC_XOR;
4929 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
4930 const struct wined3d_shader_version *version = &reg_maps->shader_version;
4931 struct glsl_src_param coord_param, data_param, data_param2;
4932 enum wined3d_shader_resource_type resource_type;
4933 enum wined3d_data_type data_type;
4934 unsigned int uav_idx;
4935 DWORD coord_mask;
4936 const char *op;
4938 uav_idx = ins->dst[is_imm_instruction].reg.idx[0].offset;
4939 resource_type = reg_maps->uav_resource_info[uav_idx].type;
4940 if (resource_type >= ARRAY_SIZE(resource_type_info))
4942 ERR("Unexpected resource type %#x.\n", resource_type);
4943 return;
4945 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
4946 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
4948 switch (ins->handler_idx)
4950 case WINED3DSIH_ATOMIC_AND:
4951 case WINED3DSIH_IMM_ATOMIC_AND:
4952 op = "imageAtomicAnd";
4953 break;
4954 case WINED3DSIH_ATOMIC_CMP_STORE:
4955 case WINED3DSIH_IMM_ATOMIC_CMP_EXCH:
4956 op = "imageAtomicCompSwap";
4957 break;
4958 case WINED3DSIH_ATOMIC_IADD:
4959 case WINED3DSIH_IMM_ATOMIC_IADD:
4960 op = "imageAtomicAdd";
4961 break;
4962 case WINED3DSIH_ATOMIC_OR:
4963 case WINED3DSIH_IMM_ATOMIC_OR:
4964 op = "imageAtomicOr";
4965 break;
4966 case WINED3DSIH_ATOMIC_XOR:
4967 case WINED3DSIH_IMM_ATOMIC_XOR:
4968 op = "imageAtomicXor";
4969 break;
4970 case WINED3DSIH_IMM_ATOMIC_EXCH:
4971 op = "imageAtomicExchange";
4972 break;
4973 default:
4974 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
4975 return;
4978 if (is_imm_instruction)
4979 shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
4981 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &coord_param);
4983 if (reg_maps->uav_resource_info[uav_idx].flags & WINED3D_VIEW_BUFFER_RAW)
4984 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s / 4, ",
4985 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
4986 else
4987 shader_addline(ins->ctx->buffer, "%s(%s_image%u, %s, ",
4988 op, shader_glsl_get_prefix(version->type), uav_idx, coord_param.param_str);
4990 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &data_param, data_type);
4991 shader_addline(ins->ctx->buffer, "%s", data_param.param_str);
4992 if (ins->src_count >= 3)
4994 shader_glsl_add_src_param_ext(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &data_param2, data_type);
4995 shader_addline(ins->ctx->buffer, ", %s", data_param2.param_str);
4998 if (is_imm_instruction)
4999 shader_addline(ins->ctx->buffer, ")");
5000 shader_addline(ins->ctx->buffer, ");\n");
5003 static void shader_glsl_ld_uav(const struct wined3d_shader_instruction *ins)
5005 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5006 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5007 enum wined3d_shader_resource_type resource_type;
5008 struct glsl_src_param image_coord_param;
5009 enum wined3d_data_type data_type;
5010 DWORD coord_mask, write_mask;
5011 unsigned int uav_idx;
5012 char dst_swizzle[6];
5014 uav_idx = ins->src[1].reg.idx[0].offset;
5015 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5017 ERR("Invalid UAV index %u.\n", uav_idx);
5018 return;
5020 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5021 if (resource_type >= ARRAY_SIZE(resource_type_info))
5023 ERR("Unexpected resource type %#x.\n", resource_type);
5024 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5026 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5027 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5029 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], data_type);
5030 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5032 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5033 shader_addline(ins->ctx->buffer, "imageLoad(%s_image%u, %s)%s);\n",
5034 shader_glsl_get_prefix(version->type), uav_idx, image_coord_param.param_str, dst_swizzle);
5037 static void shader_glsl_store_uav(const struct wined3d_shader_instruction *ins)
5039 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5040 const struct wined3d_shader_version *version = &reg_maps->shader_version;
5041 struct glsl_src_param image_coord_param, image_data_param;
5042 enum wined3d_shader_resource_type resource_type;
5043 enum wined3d_data_type data_type;
5044 unsigned int uav_idx;
5045 DWORD coord_mask;
5047 uav_idx = ins->dst[0].reg.idx[0].offset;
5048 if (uav_idx >= ARRAY_SIZE(reg_maps->uav_resource_info))
5050 ERR("Invalid UAV index %u.\n", uav_idx);
5051 return;
5053 resource_type = reg_maps->uav_resource_info[uav_idx].type;
5054 if (resource_type >= ARRAY_SIZE(resource_type_info))
5056 ERR("Unexpected resource type %#x.\n", resource_type);
5057 return;
5059 data_type = reg_maps->uav_resource_info[uav_idx].data_type;
5060 coord_mask = (1u << resource_type_info[resource_type].coord_size) - 1;
5062 shader_glsl_add_src_param(ins, &ins->src[0], coord_mask, &image_coord_param);
5063 shader_glsl_add_src_param_ext(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &image_data_param, data_type);
5064 shader_addline(ins->ctx->buffer, "imageStore(%s_image%u, %s, %s);\n",
5065 shader_glsl_get_prefix(version->type), uav_idx,
5066 image_coord_param.param_str, image_data_param.param_str);
5069 static void shader_glsl_store_raw(const struct wined3d_shader_instruction *ins)
5071 const char *prefix = shader_glsl_get_prefix(ins->ctx->reg_maps->shader_version.type);
5072 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5073 struct glsl_src_param offset, data;
5074 DWORD write_mask;
5075 unsigned int i;
5077 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &offset);
5079 for (i = 0; i < 4; ++i)
5081 if (!(write_mask = ins->dst[0].write_mask & (WINED3DSP_WRITEMASK_0 << i)))
5082 continue;
5084 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &data);
5086 shader_addline(buffer, "imageStore(%s_image%u, %s / 4 + %u, uvec4(%s, 0, 0, 0));\n",
5087 prefix, ins->dst[0].reg.idx[0].offset, offset.param_str, i, data.param_str);
5091 static void shader_glsl_resinfo(const struct wined3d_shader_instruction *ins)
5093 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
5094 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
5095 enum wined3d_shader_resource_type resource_type;
5096 enum wined3d_shader_register_type reg_type;
5097 unsigned int resource_idx, bind_idx, i;
5098 enum wined3d_data_type dst_data_type;
5099 struct glsl_src_param lod_param;
5100 char dst_swizzle[6];
5101 DWORD write_mask;
5103 dst_data_type = ins->dst[0].reg.data_type;
5104 if (ins->flags == WINED3DSI_RESINFO_UINT)
5105 dst_data_type = WINED3D_DATA_UINT;
5106 else if (ins->flags)
5107 FIXME("Unhandled flags %#x.\n", ins->flags);
5109 write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &ins->dst[0], dst_data_type);
5110 shader_glsl_get_swizzle(&ins->src[1], FALSE, write_mask, dst_swizzle);
5112 reg_type = ins->src[1].reg.type;
5113 resource_idx = ins->src[1].reg.idx[0].offset;
5114 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &lod_param);
5115 if (reg_type == WINED3DSPR_RESOURCE)
5117 resource_type = ins->ctx->reg_maps->resource_info[resource_idx].type;
5118 bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map,
5119 resource_idx, WINED3D_SAMPLER_DEFAULT);
5121 else
5123 resource_type = ins->ctx->reg_maps->uav_resource_info[resource_idx].type;
5124 bind_idx = resource_idx;
5127 if (resource_type >= ARRAY_SIZE(resource_type_info))
5129 ERR("Unexpected resource type %#x.\n", resource_type);
5130 resource_type = WINED3D_SHADER_RESOURCE_TEXTURE_2D;
5133 if (dst_data_type == WINED3D_DATA_UINT)
5134 shader_addline(ins->ctx->buffer, "uvec4(");
5135 else
5136 shader_addline(ins->ctx->buffer, "vec4(");
5138 if (reg_type == WINED3DSPR_RESOURCE)
5140 shader_addline(ins->ctx->buffer, "textureSize(%s_sampler%u, %s), ",
5141 shader_glsl_get_prefix(version->type), bind_idx, lod_param.param_str);
5143 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5144 shader_addline(ins->ctx->buffer, "0, ");
5146 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
5148 shader_addline(ins->ctx->buffer, "textureQueryLevels(%s_sampler%u)",
5149 shader_glsl_get_prefix(version->type), bind_idx);
5151 else
5153 FIXME("textureQueryLevels is not supported, returning 1 mipmap level.\n");
5154 shader_addline(ins->ctx->buffer, "1");
5157 else
5159 shader_addline(ins->ctx->buffer, "imageSize(%s_image%u), ",
5160 shader_glsl_get_prefix(version->type), bind_idx);
5162 for (i = 0; i < 3 - resource_type_info[resource_type].resinfo_size; ++i)
5163 shader_addline(ins->ctx->buffer, "0, ");
5165 /* For UAVs the returned miplevel count is always 1. */
5166 shader_addline(ins->ctx->buffer, "1");
5169 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
5172 /* FIXME: The current implementation does not handle multisample textures correctly. */
5173 static void shader_glsl_ld(const struct wined3d_shader_instruction *ins)
5175 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
5176 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5177 struct glsl_src_param coord_param, lod_param;
5178 struct glsl_sample_function sample_function;
5179 DWORD flags = WINED3D_GLSL_SAMPLE_LOAD;
5180 BOOL has_lod_param;
5182 if (wined3d_shader_instruction_has_texel_offset(ins))
5183 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5185 resource_idx = ins->src[1].reg.idx[0].offset;
5186 sampler_idx = WINED3D_SAMPLER_DEFAULT;
5188 if (resource_idx >= ARRAY_SIZE(reg_maps->resource_info))
5190 ERR("Invalid resource index %u.\n", resource_idx);
5191 return;
5193 has_lod_param = reg_maps->resource_info[resource_idx].type != WINED3D_SHADER_RESOURCE_BUFFER;
5195 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5196 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5197 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
5198 sampler_bind_idx = shader_glsl_find_sampler(&reg_maps->sampler_map, resource_idx, sampler_idx);
5199 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5200 NULL, NULL, has_lod_param ? lod_param.param_str : NULL, &ins->texel_offset,
5201 "%s", coord_param.param_str);
5202 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5205 static void shader_glsl_sample(const struct wined3d_shader_instruction *ins)
5207 const char *lod_param_str = NULL, *dx_param_str = NULL, *dy_param_str = NULL;
5208 struct glsl_src_param coord_param, lod_param, dx_param, dy_param;
5209 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5210 struct glsl_sample_function sample_function;
5211 DWORD flags = 0;
5213 if (ins->handler_idx == WINED3DSIH_SAMPLE_GRAD)
5214 flags |= WINED3D_GLSL_SAMPLE_GRAD;
5215 if (ins->handler_idx == WINED3DSIH_SAMPLE_LOD)
5216 flags |= WINED3D_GLSL_SAMPLE_LOD;
5217 if (wined3d_shader_instruction_has_texel_offset(ins))
5218 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5220 resource_idx = ins->src[1].reg.idx[0].offset;
5221 sampler_idx = ins->src[2].reg.idx[0].offset;
5223 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5224 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
5226 switch (ins->handler_idx)
5228 case WINED3DSIH_SAMPLE:
5229 break;
5230 case WINED3DSIH_SAMPLE_B:
5231 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5232 lod_param_str = lod_param.param_str;
5233 break;
5234 case WINED3DSIH_SAMPLE_GRAD:
5235 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.deriv_mask, &dx_param);
5236 shader_glsl_add_src_param(ins, &ins->src[4], sample_function.deriv_mask, &dy_param);
5237 dx_param_str = dx_param.param_str;
5238 dy_param_str = dy_param.param_str;
5239 break;
5240 case WINED3DSIH_SAMPLE_LOD:
5241 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &lod_param);
5242 lod_param_str = lod_param.param_str;
5243 break;
5244 default:
5245 ERR("Unhandled opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
5246 break;
5249 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5250 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, ins->src[1].swizzle,
5251 dx_param_str, dy_param_str, lod_param_str, &ins->texel_offset, "%s", coord_param.param_str);
5252 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5255 static void shader_glsl_sample_c(const struct wined3d_shader_instruction *ins)
5257 unsigned int resource_idx, sampler_idx, sampler_bind_idx;
5258 struct glsl_src_param coord_param, compare_param;
5259 struct glsl_sample_function sample_function;
5260 const char *lod_param = NULL;
5261 DWORD flags = 0;
5262 UINT coord_size;
5264 if (ins->handler_idx == WINED3DSIH_SAMPLE_C_LZ)
5266 lod_param = "0";
5267 flags |= WINED3D_GLSL_SAMPLE_LOD;
5270 if (wined3d_shader_instruction_has_texel_offset(ins))
5271 flags |= WINED3D_GLSL_SAMPLE_OFFSET;
5273 resource_idx = ins->src[1].reg.idx[0].offset;
5274 sampler_idx = ins->src[2].reg.idx[0].offset;
5276 shader_glsl_get_sample_function(ins->ctx, resource_idx, sampler_idx, flags, &sample_function);
5277 coord_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5278 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask >> 1, &coord_param);
5279 shader_glsl_add_src_param(ins, &ins->src[3], WINED3DSP_WRITEMASK_0, &compare_param);
5280 sampler_bind_idx = shader_glsl_find_sampler(&ins->ctx->reg_maps->sampler_map, resource_idx, sampler_idx);
5281 shader_glsl_gen_sample_code(ins, sampler_bind_idx, &sample_function, WINED3DSP_NOSWIZZLE,
5282 NULL, NULL, lod_param, &ins->texel_offset, "vec%u(%s, %s)",
5283 coord_size, coord_param.param_str, compare_param.param_str);
5284 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5287 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
5289 /* FIXME: Make this work for more than just 2D textures */
5290 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5291 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5293 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
5295 char dst_mask[6];
5297 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5298 shader_addline(buffer, "clamp(ffp_texcoord[%u], 0.0, 1.0)%s);\n",
5299 ins->dst[0].reg.idx[0].offset, dst_mask);
5301 else
5303 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
5304 DWORD reg = ins->src[0].reg.idx[0].offset;
5305 char dst_swizzle[6];
5307 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
5309 if (src_mod == WINED3DSPSM_DZ || src_mod == WINED3DSPSM_DW)
5311 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
5312 struct glsl_src_param div_param;
5313 DWORD src_writemask = src_mod == WINED3DSPSM_DZ ? WINED3DSP_WRITEMASK_2 : WINED3DSP_WRITEMASK_3;
5315 shader_glsl_add_src_param(ins, &ins->src[0], src_writemask, &div_param);
5317 if (mask_size > 1)
5318 shader_addline(buffer, "ffp_texcoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
5319 else
5320 shader_addline(buffer, "ffp_texcoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
5322 else
5324 shader_addline(buffer, "ffp_texcoord[%u]%s);\n", reg, dst_swizzle);
5329 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
5330 * Take a 3-component dot product of the TexCoord[dstreg] and src,
5331 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
5332 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
5334 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5335 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5336 struct glsl_sample_function sample_function;
5337 struct glsl_src_param src0_param;
5338 UINT mask_size;
5340 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5342 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
5343 * scalar, and projected sampling would require 4.
5345 * It is a dependent read - not valid with conditional NP2 textures
5347 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5348 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
5350 switch(mask_size)
5352 case 1:
5353 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5354 NULL, "dot(ffp_texcoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
5355 break;
5357 case 2:
5358 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5359 NULL, "vec2(dot(ffp_texcoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
5360 break;
5362 case 3:
5363 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
5364 NULL, "vec3(dot(ffp_texcoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
5365 break;
5367 default:
5368 FIXME("Unexpected mask size %u\n", mask_size);
5369 break;
5371 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5374 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
5375 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
5376 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
5378 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5379 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5380 struct glsl_src_param src0_param;
5381 DWORD dst_mask;
5382 unsigned int mask_size;
5384 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5385 mask_size = shader_glsl_get_write_mask_size(dst_mask);
5386 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5388 if (mask_size > 1) {
5389 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
5390 } else {
5391 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
5395 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
5396 * Calculate the depth as dst.x / dst.y */
5397 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
5399 struct glsl_dst_param dst_param;
5401 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5403 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
5404 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
5405 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
5406 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
5407 * >= 1.0 or < 0.0
5409 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
5410 dst_param.reg_name, dst_param.reg_name);
5413 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
5414 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
5415 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
5416 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
5418 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
5420 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5421 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
5422 struct glsl_src_param src0_param;
5424 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5426 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
5427 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
5430 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
5431 * Calculate the 1st of a 2-row matrix multiplication. */
5432 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
5434 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5435 DWORD reg = ins->dst[0].reg.idx[0].offset;
5436 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5437 struct glsl_src_param src0_param;
5439 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5440 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5443 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
5444 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
5445 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
5447 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5448 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5449 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5450 DWORD reg = ins->dst[0].reg.idx[0].offset;
5451 struct glsl_src_param src0_param;
5453 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5454 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
5455 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
5458 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
5460 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5461 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5462 struct glsl_sample_function sample_function;
5463 DWORD reg = ins->dst[0].reg.idx[0].offset;
5464 struct glsl_src_param src0_param;
5466 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5467 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5469 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5471 /* Sample the texture using the calculated coordinates */
5472 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xy");
5473 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5476 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
5477 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
5478 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
5480 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5481 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5482 struct glsl_sample_function sample_function;
5483 DWORD reg = ins->dst[0].reg.idx[0].offset;
5484 struct glsl_src_param src0_param;
5486 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5487 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5489 /* Dependent read, not valid with conditional NP2 */
5490 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5492 /* Sample the texture using the calculated coordinates */
5493 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL, "tmp0.xyz");
5494 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5496 tex_mx->current_row = 0;
5499 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
5500 * Perform the 3rd row of a 3x3 matrix multiply */
5501 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
5503 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5504 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5505 DWORD reg = ins->dst[0].reg.idx[0].offset;
5506 struct glsl_src_param src0_param;
5507 char dst_mask[6];
5509 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5511 shader_glsl_append_dst(ins->ctx->buffer, ins);
5512 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
5513 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
5515 tex_mx->current_row = 0;
5518 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
5519 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5520 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
5522 struct glsl_src_param src0_param;
5523 struct glsl_src_param src1_param;
5524 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5525 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5526 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5527 struct glsl_sample_function sample_function;
5528 DWORD reg = ins->dst[0].reg.idx[0].offset;
5529 char coord_mask[6];
5531 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5532 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
5534 /* Perform the last matrix multiply operation */
5535 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
5536 /* Reflection calculation */
5537 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
5539 /* Dependent read, not valid with conditional NP2 */
5540 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5541 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5543 /* Sample the texture */
5544 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5545 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5546 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5548 tex_mx->current_row = 0;
5551 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
5552 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
5553 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
5555 struct wined3d_string_buffer *buffer = ins->ctx->buffer;
5556 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
5557 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
5558 struct glsl_sample_function sample_function;
5559 DWORD reg = ins->dst[0].reg.idx[0].offset;
5560 struct glsl_src_param src0_param;
5561 char coord_mask[6];
5563 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
5565 /* Perform the last matrix multiply operation */
5566 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
5568 /* Construct the eye-ray vector from w coordinates */
5569 shader_addline(buffer, "tmp1.xyz = normalize(vec3(ffp_texcoord[%u].w, ffp_texcoord[%u].w, ffp_texcoord[%u].w));\n",
5570 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
5571 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
5573 /* Dependent read, not valid with conditional NP2 */
5574 shader_glsl_get_sample_function(ins->ctx, reg, reg, 0, &sample_function);
5575 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
5577 /* Sample the texture using the calculated coordinates */
5578 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
5579 NULL, NULL, NULL, NULL, "tmp0%s", coord_mask);
5580 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5582 tex_mx->current_row = 0;
5585 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
5586 * Apply a fake bump map transform.
5587 * texbem is pshader <= 1.3 only, this saves a few version checks
5589 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
5591 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
5592 struct glsl_sample_function sample_function;
5593 struct glsl_src_param coord_param;
5594 DWORD sampler_idx;
5595 DWORD mask;
5596 DWORD flags;
5597 char coord_mask[6];
5599 sampler_idx = ins->dst[0].reg.idx[0].offset;
5600 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
5601 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
5603 /* Dependent read, not valid with conditional NP2 */
5604 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5605 mask = sample_function.coord_mask;
5607 shader_glsl_write_mask_to_str(mask, coord_mask);
5609 /* With projected textures, texbem only divides the static texture coord,
5610 * not the displacement, so we can't let GL handle this. */
5611 if (flags & WINED3D_PSARGS_PROJECTED)
5613 DWORD div_mask=0;
5614 char coord_div_mask[3];
5615 switch (flags & ~WINED3D_PSARGS_PROJECTED)
5617 case WINED3D_TTFF_COUNT1:
5618 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
5619 break;
5620 case WINED3D_TTFF_COUNT2:
5621 div_mask = WINED3DSP_WRITEMASK_1;
5622 break;
5623 case WINED3D_TTFF_COUNT3:
5624 div_mask = WINED3DSP_WRITEMASK_2;
5625 break;
5626 case WINED3D_TTFF_COUNT4:
5627 case WINED3D_TTFF_DISABLE:
5628 div_mask = WINED3DSP_WRITEMASK_3;
5629 break;
5631 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
5632 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
5635 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
5637 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5638 "T%u%s + vec4(bumpenv_mat%u * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
5639 coord_param.param_str, coord_mask);
5641 if (ins->handler_idx == WINED3DSIH_TEXBEML)
5643 struct glsl_src_param luminance_param;
5644 struct glsl_dst_param dst_param;
5646 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
5647 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5649 shader_addline(ins->ctx->buffer, "%s%s *= (%s * bumpenv_lum_scale%u + bumpenv_lum_offset%u);\n",
5650 dst_param.reg_name, dst_param.mask_str,
5651 luminance_param.param_str, sampler_idx, sampler_idx);
5653 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5656 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
5658 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5659 struct glsl_src_param src0_param, src1_param;
5661 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5662 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5664 shader_glsl_append_dst(ins->ctx->buffer, ins);
5665 shader_addline(ins->ctx->buffer, "%s + bumpenv_mat%u * %s);\n",
5666 src0_param.param_str, sampler_idx, src1_param.param_str);
5669 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
5670 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
5671 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
5673 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5674 struct glsl_sample_function sample_function;
5675 struct glsl_src_param src0_param;
5677 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5679 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5680 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5681 "%s.wx", src0_param.reg_name);
5682 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5685 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
5686 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
5687 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
5689 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5690 struct glsl_sample_function sample_function;
5691 struct glsl_src_param src0_param;
5693 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
5695 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5696 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5697 "%s.yz", src0_param.reg_name);
5698 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5701 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
5702 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
5703 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
5705 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
5706 struct glsl_sample_function sample_function;
5707 struct glsl_src_param src0_param;
5709 /* Dependent read, not valid with conditional NP2 */
5710 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sampler_idx, 0, &sample_function);
5711 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
5713 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, NULL,
5714 "%s", src0_param.param_str);
5715 shader_glsl_release_sample_function(ins->ctx, &sample_function);
5718 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
5719 * If any of the first 3 components are < 0, discard this pixel */
5720 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
5722 if (ins->ctx->reg_maps->shader_version.major >= 4)
5724 struct glsl_src_param src_param;
5726 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
5727 shader_addline(ins->ctx->buffer, "if (bool(%s)) discard;\n", src_param.param_str);
5729 else
5731 struct glsl_dst_param dst_param;
5733 /* The argument is a destination parameter, and no writemasks are allowed */
5734 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
5736 /* 2.0 shaders compare all 4 components in texkill. */
5737 if (ins->ctx->reg_maps->shader_version.major >= 2)
5738 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
5739 /* 1.x shaders only compare the first 3 components, probably due to
5740 * the nature of the texkill instruction as a tex* instruction, and
5741 * phase, which kills all .w components. Even if all 4 components are
5742 * defined, only the first 3 are used. */
5743 else
5744 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
5748 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
5749 * dst = dot2(src0, src1) + src2 */
5750 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
5752 struct glsl_src_param src0_param;
5753 struct glsl_src_param src1_param;
5754 struct glsl_src_param src2_param;
5755 DWORD write_mask;
5756 unsigned int mask_size;
5758 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
5759 mask_size = shader_glsl_get_write_mask_size(write_mask);
5761 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
5762 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
5763 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
5765 if (mask_size > 1) {
5766 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
5767 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
5768 } else {
5769 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
5770 src0_param.param_str, src1_param.param_str, src2_param.param_str);
5774 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_string_buffer *buffer,
5775 const struct wined3d_shader_signature *input_signature,
5776 const struct wined3d_shader_reg_maps *reg_maps,
5777 const struct ps_compile_args *args, const struct wined3d_gl_info *gl_info)
5779 unsigned int i;
5781 for (i = 0; i < input_signature->element_count; ++i)
5783 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5784 const char *semantic_name;
5785 UINT semantic_idx;
5786 char reg_mask[6];
5788 /* Unused */
5789 if (!(reg_maps->input_registers & (1u << input->register_idx)))
5790 continue;
5792 semantic_name = input->semantic_name;
5793 semantic_idx = input->semantic_idx;
5794 shader_glsl_write_mask_to_str(input->mask, reg_mask);
5796 if (args->vp_mode == vertexshader)
5798 if (input->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
5800 shader_addline(buffer, "ps_in[%u]%s = vpos%s;\n",
5801 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5803 else if (args->pointsprite && shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5805 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", input->register_idx);
5807 else if (input->sysval_semantic == WINED3D_SV_IS_FRONT_FACE)
5809 shader_addline(buffer, "ps_in[%u] = vec4("
5810 "uintBitsToFloat(gl_FrontFacing ? 0xffffffffu : 0u), 0.0, 0.0, 0.0);\n",
5811 input->register_idx);
5813 else
5815 if (input->sysval_semantic)
5816 FIXME("Unhandled sysval semantic %#x.\n", input->sysval_semantic);
5817 shader_addline(buffer, "ps_in[%u]%s = ps_link[%u]%s;\n",
5818 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5819 shader->u.ps.input_reg_map[input->register_idx], reg_mask);
5822 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
5824 if (args->pointsprite)
5825 shader_addline(buffer, "ps_in[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n",
5826 shader->u.ps.input_reg_map[input->register_idx]);
5827 else if (args->vp_mode == pretransformed && args->texcoords_initialized & (1u << semantic_idx))
5828 shader_addline(buffer, "ps_in[%u]%s = %s[%u]%s;\n",
5829 shader->u.ps.input_reg_map[input->register_idx], reg_mask,
5830 gl_info->supported[WINED3D_GL_LEGACY_CONTEXT]
5831 ? "gl_TexCoord" : "ffp_varying_texcoord", semantic_idx, reg_mask);
5832 else
5833 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5834 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5836 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
5838 if (!semantic_idx)
5839 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_diffuse)%s;\n",
5840 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5841 else if (semantic_idx == 1)
5842 shader_addline(buffer, "ps_in[%u]%s = vec4(ffp_varying_specular)%s;\n",
5843 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5844 else
5845 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5846 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5848 else
5850 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
5851 shader->u.ps.input_reg_map[input->register_idx], reg_mask, reg_mask);
5856 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
5858 struct glsl_program_key key;
5860 key.vs_id = entry->vs.id;
5861 key.gs_id = entry->gs.id;
5862 key.ps_id = entry->ps.id;
5863 key.cs_id = entry->cs.id;
5865 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
5867 ERR("Failed to insert program entry.\n");
5871 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
5872 const struct glsl_program_key *key)
5874 struct wine_rb_entry *entry;
5876 entry = wine_rb_get(&priv->program_lookup, key);
5877 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
5880 /* Context activation is done by the caller. */
5881 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
5882 struct glsl_shader_prog_link *entry)
5884 wine_rb_remove(&priv->program_lookup, &entry->program_lookup_entry);
5886 GL_EXTCALL(glDeleteProgram(entry->id));
5887 if (entry->vs.id)
5888 list_remove(&entry->vs.shader_entry);
5889 if (entry->gs.id)
5890 list_remove(&entry->gs.shader_entry);
5891 if (entry->ps.id)
5892 list_remove(&entry->ps.shader_entry);
5893 if (entry->cs.id)
5894 list_remove(&entry->cs.shader_entry);
5895 HeapFree(GetProcessHeap(), 0, entry);
5898 static void shader_glsl_setup_vs3_output(struct shader_glsl_priv *priv,
5899 const struct wined3d_gl_info *gl_info, const DWORD *map,
5900 const struct wined3d_shader_signature *input_signature,
5901 const struct wined3d_shader_reg_maps *reg_maps_in,
5902 const struct wined3d_shader_signature *output_signature,
5903 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
5905 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
5906 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
5907 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
5908 unsigned int in_count = vec4_varyings(3, gl_info);
5909 unsigned int max_varyings = legacy_context ? in_count + 2 : in_count;
5910 DWORD in_idx, *set = NULL;
5911 unsigned int i, j;
5912 char reg_mask[6];
5914 set = wined3d_calloc(max_varyings, sizeof(*set));
5916 for (i = 0; i < input_signature->element_count; ++i)
5918 const struct wined3d_shader_signature_element *input = &input_signature->elements[i];
5920 if (!(reg_maps_in->input_registers & (1u << input->register_idx)))
5921 continue;
5923 in_idx = map[input->register_idx];
5924 /* Declared, but not read register */
5925 if (in_idx == ~0u)
5926 continue;
5927 if (in_idx >= max_varyings)
5929 FIXME("More input varyings declared than supported, expect issues.\n");
5930 continue;
5933 if (in_idx == in_count)
5934 string_buffer_sprintf(destination, "gl_FrontColor");
5935 else if (in_idx == in_count + 1)
5936 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5937 else
5938 string_buffer_sprintf(destination, "%s[%u]", out_array_name, in_idx);
5940 if (!set[in_idx])
5941 set[in_idx] = ~0u;
5943 for (j = 0; j < output_signature->element_count; ++j)
5945 const struct wined3d_shader_signature_element *output = &output_signature->elements[j];
5946 DWORD mask;
5948 if (!(reg_maps_out->output_registers & (1u << output->register_idx))
5949 || input->semantic_idx != output->semantic_idx
5950 || strcmp(input->semantic_name, output->semantic_name)
5951 || !(mask = input->mask & output->mask))
5952 continue;
5954 if (set[in_idx] == ~0u)
5955 set[in_idx] = 0;
5956 set[in_idx] |= mask & reg_maps_out->u.output_registers_mask[output->register_idx];
5957 shader_glsl_write_mask_to_str(mask, reg_mask);
5959 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
5960 destination->buffer, reg_mask, output->register_idx, reg_mask);
5964 for (i = 0; i < max_varyings; ++i)
5966 unsigned int size;
5968 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
5969 continue;
5971 if (set[i] == ~0u)
5972 set[i] = 0;
5974 size = 0;
5975 if (!(set[i] & WINED3DSP_WRITEMASK_0))
5976 reg_mask[size++] = 'x';
5977 if (!(set[i] & WINED3DSP_WRITEMASK_1))
5978 reg_mask[size++] = 'y';
5979 if (!(set[i] & WINED3DSP_WRITEMASK_2))
5980 reg_mask[size++] = 'z';
5981 if (!(set[i] & WINED3DSP_WRITEMASK_3))
5982 reg_mask[size++] = 'w';
5983 reg_mask[size] = '\0';
5985 if (i == in_count)
5986 string_buffer_sprintf(destination, "gl_FrontColor");
5987 else if (i == in_count + 1)
5988 string_buffer_sprintf(destination, "gl_FrontSecondaryColor");
5989 else
5990 string_buffer_sprintf(destination, "%s[%u]", out_array_name, i);
5992 if (size == 1)
5993 shader_addline(buffer, "%s.%s = 0.0;\n", destination->buffer, reg_mask);
5994 else
5995 shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination->buffer, reg_mask, size);
5998 HeapFree(GetProcessHeap(), 0, set);
5999 string_buffer_release(&priv->string_buffers, destination);
6002 static void shader_glsl_setup_sm4_shader_output(struct shader_glsl_priv *priv,
6003 unsigned int input_count, const struct wined3d_shader_signature *output_signature,
6004 const struct wined3d_shader_reg_maps *reg_maps_out, const char *out_array_name)
6006 struct wined3d_string_buffer *destination = string_buffer_get(&priv->string_buffers);
6007 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6008 char reg_mask[6];
6009 unsigned int i;
6011 for (i = 0; i < output_signature->element_count; ++i)
6013 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6015 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6016 continue;
6018 if (output->register_idx >= input_count)
6019 continue;
6021 string_buffer_sprintf(destination, "%s[%u]", out_array_name, output->register_idx);
6023 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6025 shader_addline(buffer, "%s%s = shader_out[%u]%s;\n",
6026 destination->buffer, reg_mask, output->register_idx, reg_mask);
6029 string_buffer_release(&priv->string_buffers, destination);
6032 /* Context activation is done by the caller. */
6033 static void shader_glsl_generate_vs_gs_setup(struct shader_glsl_priv *priv,
6034 const struct wined3d_shader *vs, unsigned int input_count,
6035 const struct wined3d_gl_info *gl_info)
6037 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6038 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6040 if (legacy_context)
6041 shader_addline(buffer, "varying out vec4 gs_in[%u];\n", input_count);
6042 else
6043 shader_addline(buffer, "out vs_gs_iface { vec4 gs_in[%u]; } gs_in;\n", input_count);
6044 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6046 shader_glsl_setup_sm4_shader_output(priv, input_count, &vs->output_signature, &vs->reg_maps,
6047 legacy_context ? "gs_in" : "gs_in.gs_in");
6049 shader_addline(buffer, "}\n");
6052 static void shader_glsl_setup_sm3_rasterizer_input(struct shader_glsl_priv *priv,
6053 const struct wined3d_gl_info *gl_info, const DWORD *map,
6054 const struct wined3d_shader_signature *input_signature,
6055 const struct wined3d_shader_reg_maps *reg_maps_in, unsigned int input_count,
6056 const struct wined3d_shader_signature *output_signature,
6057 const struct wined3d_shader_reg_maps *reg_maps_out, BOOL per_vertex_point_size)
6059 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6060 const char *semantic_name;
6061 UINT semantic_idx;
6062 char reg_mask[6];
6063 unsigned int i;
6065 /* First, sort out position and point size system values. */
6066 for (i = 0; i < output_signature->element_count; ++i)
6068 const struct wined3d_shader_signature_element *output = &output_signature->elements[i];
6070 if (!(reg_maps_out->output_registers & (1u << output->register_idx)))
6071 continue;
6073 semantic_name = output->semantic_name;
6074 semantic_idx = output->semantic_idx;
6075 shader_glsl_write_mask_to_str(output->mask, reg_mask);
6077 if (output->sysval_semantic == WINED3D_SV_POSITION && !semantic_idx)
6079 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6080 reg_mask, output->register_idx, reg_mask);
6082 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6084 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6085 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6087 else if (output->sysval_semantic)
6089 FIXME("Unhandled sysval semantic %#x.\n", output->sysval_semantic);
6093 /* Then, setup the pixel shader input. */
6094 if (reg_maps_out->shader_version.major < 4)
6095 shader_glsl_setup_vs3_output(priv, gl_info, map, input_signature, reg_maps_in,
6096 output_signature, reg_maps_out, "ps_link");
6097 else
6098 shader_glsl_setup_sm4_shader_output(priv, input_count, output_signature, reg_maps_out, "ps_link");
6101 /* Context activation is done by the caller. */
6102 static GLuint shader_glsl_generate_vs3_rasterizer_input_setup(struct shader_glsl_priv *priv,
6103 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
6104 BOOL per_vertex_point_size, BOOL flatshading, const struct wined3d_gl_info *gl_info)
6106 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6107 GLuint ret;
6108 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
6109 unsigned int i;
6110 const char *semantic_name;
6111 UINT semantic_idx;
6112 char reg_mask[6];
6113 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6115 string_buffer_clear(buffer);
6117 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &vs->reg_maps.shader_version));
6119 if (per_vertex_point_size)
6121 shader_addline(buffer, "uniform struct\n{\n");
6122 shader_addline(buffer, " float size_min;\n");
6123 shader_addline(buffer, " float size_max;\n");
6124 shader_addline(buffer, "} ffp_point;\n");
6127 if (ps_major < 3)
6129 DWORD colors_written_mask[2] = {0};
6130 DWORD texcoords_written_mask[MAX_TEXTURES] = {0};
6132 if (!legacy_context)
6134 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_diffuse;\n");
6135 declare_out_varying(gl_info, buffer, flatshading, "vec4 ffp_varying_specular;\n");
6136 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
6137 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
6140 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6142 for (i = 0; i < vs->output_signature.element_count; ++i)
6144 const struct wined3d_shader_signature_element *output = &vs->output_signature.elements[i];
6145 DWORD write_mask;
6147 if (!(vs->reg_maps.output_registers & (1u << output->register_idx)))
6148 continue;
6150 semantic_name = output->semantic_name;
6151 semantic_idx = output->semantic_idx;
6152 write_mask = output->mask;
6153 shader_glsl_write_mask_to_str(write_mask, reg_mask);
6155 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR) && semantic_idx < 2)
6157 if (legacy_context)
6158 shader_addline(buffer, "gl_Front%sColor%s = shader_out[%u]%s;\n",
6159 semantic_idx ? "Secondary" : "", reg_mask, output->register_idx, reg_mask);
6160 else
6161 shader_addline(buffer, "ffp_varying_%s%s = clamp(shader_out[%u]%s, 0.0, 1.0);\n",
6162 semantic_idx ? "specular" : "diffuse", reg_mask, output->register_idx, reg_mask);
6164 colors_written_mask[semantic_idx] = write_mask;
6166 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION) && !semantic_idx)
6168 shader_addline(buffer, "gl_Position%s = shader_out[%u]%s;\n",
6169 reg_mask, output->register_idx, reg_mask);
6171 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
6173 if (semantic_idx < MAX_TEXTURES)
6175 shader_addline(buffer, "%s[%u]%s = shader_out[%u]%s;\n",
6176 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord",
6177 semantic_idx, reg_mask, output->register_idx, reg_mask);
6178 texcoords_written_mask[semantic_idx] = write_mask;
6181 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE) && per_vertex_point_size)
6183 shader_addline(buffer, "gl_PointSize = clamp(shader_out[%u].%c, "
6184 "ffp_point.size_min, ffp_point.size_max);\n", output->register_idx, reg_mask[1]);
6186 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
6188 shader_addline(buffer, "%s = clamp(shader_out[%u].%c, 0.0, 1.0);\n",
6189 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord",
6190 output->register_idx, reg_mask[1]);
6194 for (i = 0; i < 2; ++i)
6196 if (colors_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6198 shader_glsl_write_mask_to_str(~colors_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6199 if (!i)
6200 shader_addline(buffer, "%s%s = vec4(1.0)%s;\n",
6201 legacy_context ? "gl_FrontColor" : "ffp_varying_diffuse",
6202 reg_mask, reg_mask);
6203 else
6204 shader_addline(buffer, "%s%s = vec4(0.0)%s;\n",
6205 legacy_context ? "gl_FrontSecondaryColor" : "ffp_varying_specular",
6206 reg_mask, reg_mask);
6209 for (i = 0; i < MAX_TEXTURES; ++i)
6211 if (ps && !(ps->reg_maps.texcoord & (1u << i)))
6212 continue;
6214 if (texcoords_written_mask[i] != WINED3DSP_WRITEMASK_ALL)
6216 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
6217 && !texcoords_written_mask[i])
6218 continue;
6220 shader_glsl_write_mask_to_str(~texcoords_written_mask[i] & WINED3DSP_WRITEMASK_ALL, reg_mask);
6221 shader_addline(buffer, "%s[%u]%s = vec4(0.0)%s;\n",
6222 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i, reg_mask, reg_mask);
6226 else
6228 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits->packed_input);
6230 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", in_count);
6231 shader_addline(buffer, "void setup_vs_output(in vec4 shader_out[%u])\n{\n", vs->limits->packed_output);
6232 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, ps->u.ps.input_reg_map, &ps->input_signature,
6233 &ps->reg_maps, 0, &vs->output_signature, &vs->reg_maps, per_vertex_point_size);
6236 shader_addline(buffer, "}\n");
6238 ret = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6239 checkGLcall("glCreateShader(GL_VERTEX_SHADER)");
6240 shader_glsl_compile(gl_info, ret, buffer->buffer);
6242 return ret;
6245 static void shader_glsl_generate_sm4_rasterizer_input_setup(struct shader_glsl_priv *priv,
6246 const struct wined3d_shader *shader, unsigned int input_count,
6247 const struct wined3d_gl_info *gl_info)
6249 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6251 if (input_count)
6252 declare_out_varying(gl_info, buffer, FALSE, "vec4 ps_link[%u];\n", min(vec4_varyings(4, gl_info), input_count));
6254 shader_addline(buffer, "void setup_%s_output(in vec4 shader_out[%u])\n{\n",
6255 shader_glsl_get_prefix(shader->reg_maps.shader_version.type), shader->limits->packed_output);
6257 shader_glsl_setup_sm3_rasterizer_input(priv, gl_info, NULL, NULL,
6258 NULL, input_count, &shader->output_signature, &shader->reg_maps, FALSE);
6260 shader_addline(buffer, "}\n");
6263 static void shader_glsl_generate_srgb_write_correction(struct wined3d_string_buffer *buffer,
6264 const struct wined3d_gl_info *gl_info)
6266 const char *output = get_fragment_output(gl_info);
6268 shader_addline(buffer, "tmp0.xyz = pow(%s[0].xyz, vec3(srgb_const0.x));\n", output);
6269 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
6270 shader_addline(buffer, "tmp1.xyz = %s[0].xyz * vec3(srgb_const0.w);\n", output);
6271 shader_addline(buffer, "bvec3 srgb_compare = lessThan(%s[0].xyz, vec3(srgb_const1.x));\n", output);
6272 shader_addline(buffer, "%s[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n", output);
6273 shader_addline(buffer, "%s[0] = clamp(%s[0], 0.0, 1.0);\n", output, output);
6276 static void shader_glsl_generate_fog_code(struct wined3d_string_buffer *buffer,
6277 const struct wined3d_gl_info *gl_info, enum wined3d_ffp_ps_fog_mode mode)
6279 const char *output = get_fragment_output(gl_info);
6281 switch (mode)
6283 case WINED3D_FFP_PS_FOG_OFF:
6284 return;
6286 case WINED3D_FFP_PS_FOG_LINEAR:
6287 shader_addline(buffer, "float fog = (ffp_fog.end - ffp_varying_fogcoord) * ffp_fog.scale;\n");
6288 break;
6290 case WINED3D_FFP_PS_FOG_EXP:
6291 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_varying_fogcoord);\n");
6292 break;
6294 case WINED3D_FFP_PS_FOG_EXP2:
6295 shader_addline(buffer, "float fog = exp(-ffp_fog.density * ffp_fog.density"
6296 " * ffp_varying_fogcoord * ffp_varying_fogcoord);\n");
6297 break;
6299 default:
6300 ERR("Invalid fog mode %#x.\n", mode);
6301 return;
6304 shader_addline(buffer, "%s[0].xyz = mix(ffp_fog.color.xyz, %s[0].xyz, clamp(fog, 0.0, 1.0));\n",
6305 output, output);
6308 static void shader_glsl_generate_alpha_test(struct wined3d_string_buffer *buffer,
6309 const struct wined3d_gl_info *gl_info, enum wined3d_cmp_func alpha_func)
6311 /* alpha_func is the PASS condition, not the DISCARD condition. Instead of
6312 * flipping all the operators here, just negate the comparison below. */
6313 static const char * const comparison_operator[] =
6315 "", /* WINED3D_CMP_NEVER */
6316 "<", /* WINED3D_CMP_LESS */
6317 "==", /* WINED3D_CMP_EQUAL */
6318 "<=", /* WINED3D_CMP_LESSEQUAL */
6319 ">", /* WINED3D_CMP_GREATER */
6320 "!=", /* WINED3D_CMP_NOTEQUAL */
6321 ">=", /* WINED3D_CMP_GREATEREQUAL */
6322 "" /* WINED3D_CMP_ALWAYS */
6325 if (alpha_func == WINED3D_CMP_ALWAYS)
6326 return;
6328 if (alpha_func != WINED3D_CMP_NEVER)
6329 shader_addline(buffer, "if (!(%s[0].a %s alpha_test_ref))\n",
6330 get_fragment_output(gl_info), comparison_operator[alpha_func - WINED3D_CMP_NEVER]);
6331 shader_addline(buffer, " discard;\n");
6334 static void shader_glsl_enable_extensions(struct wined3d_string_buffer *buffer,
6335 const struct wined3d_gl_info *gl_info)
6337 if (gl_info->supported[ARB_GPU_SHADER5])
6338 shader_addline(buffer, "#extension GL_ARB_gpu_shader5 : enable\n");
6339 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
6340 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
6341 if (gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE])
6342 shader_addline(buffer, "#extension GL_ARB_shader_image_load_store : enable\n");
6343 if (gl_info->supported[ARB_SHADER_IMAGE_SIZE])
6344 shader_addline(buffer, "#extension GL_ARB_shader_image_size : enable\n");
6345 if (gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
6346 shader_addline(buffer, "#extension GL_ARB_shading_language_packing : enable\n");
6347 if (gl_info->supported[ARB_TEXTURE_CUBE_MAP_ARRAY])
6348 shader_addline(buffer, "#extension GL_ARB_texture_cube_map_array : enable\n");
6349 if (gl_info->supported[ARB_TEXTURE_QUERY_LEVELS])
6350 shader_addline(buffer, "#extension GL_ARB_texture_query_levels : enable\n");
6351 if (gl_info->supported[ARB_UNIFORM_BUFFER_OBJECT])
6352 shader_addline(buffer, "#extension GL_ARB_uniform_buffer_object : enable\n");
6353 if (gl_info->supported[EXT_GPU_SHADER4])
6354 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
6355 if (gl_info->supported[EXT_TEXTURE_ARRAY])
6356 shader_addline(buffer, "#extension GL_EXT_texture_array : enable\n");
6359 static void shader_glsl_generate_ps_epilogue(const struct wined3d_gl_info *gl_info,
6360 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6361 const struct ps_compile_args *args)
6363 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6365 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly. */
6366 if (reg_maps->shader_version.major < 2)
6367 shader_addline(buffer, "%s[0] = R0;\n", get_fragment_output(gl_info));
6369 if (args->srgb_correction)
6370 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
6372 /* SM < 3 does not replace the fog stage. */
6373 if (reg_maps->shader_version.major < 3)
6374 shader_glsl_generate_fog_code(buffer, gl_info, args->fog);
6376 shader_glsl_generate_alpha_test(buffer, gl_info, args->alpha_test_func + 1);
6379 /* Context activation is done by the caller. */
6380 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
6381 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6382 const struct wined3d_shader *shader,
6383 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
6385 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6386 const struct wined3d_gl_info *gl_info = context->gl_info;
6387 const DWORD *function = shader->function;
6388 struct shader_glsl_ctx_priv priv_ctx;
6389 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6391 /* Create the hw GLSL shader object and assign it as the shader->prgId */
6392 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
6394 memset(&priv_ctx, 0, sizeof(priv_ctx));
6395 priv_ctx.cur_ps_args = args;
6396 priv_ctx.cur_np2fixup_info = np2fixup_info;
6397 priv_ctx.string_buffers = string_buffers;
6399 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6401 shader_glsl_enable_extensions(buffer, gl_info);
6402 if (gl_info->supported[ARB_DERIVATIVE_CONTROL])
6403 shader_addline(buffer, "#extension GL_ARB_derivative_control : enable\n");
6404 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6405 shader_addline(buffer, "#extension GL_ARB_fragment_coord_conventions : enable\n");
6406 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
6407 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
6408 /* The spec says that it doesn't have to be explicitly enabled, but the
6409 * nvidia drivers write a warning if we don't do so. */
6410 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
6411 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
6413 /* Base Declarations */
6414 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6416 shader_addline(buffer, "void main()\n{\n");
6418 /* Direct3D applications expect integer vPos values, while OpenGL drivers
6419 * add approximately 0.5. This causes off-by-one problems as spotted by
6420 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
6421 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
6422 * causes precision troubles when we just subtract 0.5.
6424 * To deal with that, just floor() the position. This will eliminate the
6425 * fraction on all cards.
6427 * TODO: Test how this behaves with multisampling.
6429 * An advantage of floor is that it works even if the driver doesn't add
6430 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
6431 * to return in gl_FragCoord, even though coordinates specify the pixel
6432 * centers instead of the pixel corners. This code will behave correctly
6433 * on drivers that returns integer values. */
6434 if (reg_maps->vpos)
6436 if (gl_info->supported[ARB_FRAGMENT_COORD_CONVENTIONS])
6437 shader_addline(buffer, "vpos = gl_FragCoord;\n");
6438 else if (context->d3d_info->wined3d_creation_flags & WINED3D_PIXEL_CENTER_INTEGER)
6439 shader_addline(buffer,
6440 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
6441 else
6442 shader_addline(buffer,
6443 "vpos = vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1);\n");
6446 if (reg_maps->shader_version.major < 3 || args->vp_mode != vertexshader)
6448 unsigned int i;
6449 WORD map = reg_maps->texcoord;
6451 if (legacy_context)
6453 if (glsl_is_color_reg_read(shader, 0))
6454 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
6455 if (glsl_is_color_reg_read(shader, 1))
6456 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
6459 for (i = 0; map; map >>= 1, ++i)
6461 if (map & 1)
6463 if (args->pointsprite)
6464 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", i);
6465 else if (args->texcoords_initialized & (1u << i))
6466 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n", i,
6467 legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", i);
6468 else
6469 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", i);
6470 shader_addline(buffer, "vec4 T%u = ffp_texcoord[%u];\n", i, i);
6474 if (legacy_context)
6475 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
6478 /* Pack 3.0 inputs */
6479 if (reg_maps->shader_version.major >= 3)
6480 shader_glsl_input_pack(shader, buffer, &shader->input_signature, reg_maps, args, gl_info);
6482 /* Base Shader Body */
6483 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6485 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6486 if (reg_maps->shader_version.major < 4)
6487 shader_glsl_generate_ps_epilogue(gl_info, buffer, shader, args);
6489 shader_addline(buffer, "}\n");
6491 TRACE("Compiling shader object %u.\n", shader_id);
6492 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6494 return shader_id;
6497 static void shader_glsl_generate_vs_epilogue(const struct wined3d_gl_info *gl_info,
6498 struct wined3d_string_buffer *buffer, const struct wined3d_shader *shader,
6499 const struct vs_compile_args *args)
6501 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6502 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
6503 unsigned int i;
6505 /* Unpack outputs. */
6506 shader_addline(buffer, "setup_vs_output(vs_out);\n");
6508 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
6509 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
6510 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
6511 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0).
6513 if (reg_maps->shader_version.major < 3)
6515 if (args->fog_src == VS_FOG_Z)
6516 shader_addline(buffer, "%s = gl_Position.z;\n",
6517 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6518 else if (!reg_maps->fog)
6519 shader_addline(buffer, "%s = 0.0;\n",
6520 legacy_context ? "gl_FogFragCoord" : "ffp_varying_fogcoord");
6523 /* We always store the clipplanes without y inversion. */
6524 if (args->clip_enabled)
6526 if (legacy_context)
6527 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
6528 else
6529 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
6530 shader_addline(buffer, "gl_ClipDistance[%u] = dot(gl_Position, clip_planes[%u]);\n", i, i);
6533 if (args->point_size && !args->per_vertex_point_size)
6534 shader_addline(buffer, "gl_PointSize = clamp(ffp_point.size, ffp_point.size_min, ffp_point.size_max);\n");
6536 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6537 shader_glsl_fixup_position(buffer);
6540 /* Context activation is done by the caller. */
6541 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
6542 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct vs_compile_args *args)
6544 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6545 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6546 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6547 const struct wined3d_gl_info *gl_info = context->gl_info;
6548 const DWORD *function = shader->function;
6549 struct shader_glsl_ctx_priv priv_ctx;
6551 /* Create the hw GLSL shader program and assign it as the shader->prgId */
6552 GLuint shader_id = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
6554 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6556 shader_glsl_enable_extensions(buffer, gl_info);
6557 if (gl_info->supported[ARB_DRAW_INSTANCED])
6558 shader_addline(buffer, "#extension GL_ARB_draw_instanced : enable\n");
6559 if (gl_info->supported[ARB_EXPLICIT_ATTRIB_LOCATION])
6560 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
6562 memset(&priv_ctx, 0, sizeof(priv_ctx));
6563 priv_ctx.cur_vs_args = args;
6564 priv_ctx.string_buffers = string_buffers;
6566 /* Base Declarations */
6567 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6569 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL && !gl_info->supported[ARB_CLIP_CONTROL])
6570 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6572 if (reg_maps->shader_version.major >= 4)
6574 if (args->next_shader_type == WINED3D_SHADER_TYPE_PIXEL)
6575 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->next_shader_input_count, gl_info);
6576 else if (args->next_shader_type == WINED3D_SHADER_TYPE_GEOMETRY)
6577 shader_glsl_generate_vs_gs_setup(priv, shader, args->next_shader_input_count, gl_info);
6580 shader_addline(buffer, "void main()\n{\n");
6582 /* Base Shader Body */
6583 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6585 /* In SM4+ the shader epilogue is generated by the "ret" instruction. */
6586 if (reg_maps->shader_version.major < 4)
6587 shader_glsl_generate_vs_epilogue(gl_info, buffer, shader, args);
6589 shader_addline(buffer, "}\n");
6591 TRACE("Compiling shader object %u.\n", shader_id);
6592 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6594 return shader_id;
6597 /* Context activation is done by the caller. */
6598 static GLuint shader_glsl_generate_geometry_shader(const struct wined3d_context *context,
6599 struct shader_glsl_priv *priv, const struct wined3d_shader *shader, const struct gs_compile_args *args)
6601 struct wined3d_string_buffer_list *string_buffers = &priv->string_buffers;
6602 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6603 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
6604 const struct wined3d_gl_info *gl_info = context->gl_info;
6605 const DWORD *function = shader->function;
6606 struct shader_glsl_ctx_priv priv_ctx;
6607 GLuint shader_id;
6609 shader_id = GL_EXTCALL(glCreateShader(GL_GEOMETRY_SHADER));
6611 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6613 shader_glsl_enable_extensions(buffer, gl_info);
6614 if (gl_info->supported[ARB_GEOMETRY_SHADER4])
6615 shader_addline(buffer, "#extension GL_ARB_geometry_shader4 : enable\n");
6617 memset(&priv_ctx, 0, sizeof(priv_ctx));
6618 priv_ctx.string_buffers = string_buffers;
6619 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6620 if (!gl_info->supported[ARB_CLIP_CONTROL])
6621 shader_addline(buffer, "uniform vec4 pos_fixup;\n");
6622 shader_glsl_generate_sm4_rasterizer_input_setup(priv, shader, args->ps_input_count, gl_info);
6623 shader_addline(buffer, "void main()\n{\n");
6624 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6625 shader_addline(buffer, "}\n");
6627 TRACE("Compiling shader object %u.\n", shader_id);
6628 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6630 return shader_id;
6633 static void shader_glsl_generate_shader_epilogue(const struct wined3d_shader_context *ctx)
6635 const struct shader_glsl_ctx_priv *priv = ctx->backend_data;
6636 const struct wined3d_gl_info *gl_info = ctx->gl_info;
6637 const struct wined3d_shader *shader = ctx->shader;
6639 switch (shader->reg_maps.shader_version.type)
6641 case WINED3D_SHADER_TYPE_PIXEL:
6642 shader_glsl_generate_ps_epilogue(gl_info, ctx->buffer, shader, priv->cur_ps_args);
6643 break;
6644 case WINED3D_SHADER_TYPE_VERTEX:
6645 shader_glsl_generate_vs_epilogue(gl_info, ctx->buffer, shader, priv->cur_vs_args);
6646 break;
6647 case WINED3D_SHADER_TYPE_GEOMETRY:
6648 case WINED3D_SHADER_TYPE_COMPUTE:
6649 break;
6650 default:
6651 FIXME("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
6652 break;
6656 /* Context activation is done by the caller. */
6657 static GLuint shader_glsl_generate_compute_shader(const struct wined3d_context *context,
6658 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6659 const struct wined3d_shader *shader)
6661 const struct wined3d_shader_thread_group_size *thread_group_size = &shader->u.cs.thread_group_size;
6662 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
6663 const struct wined3d_gl_info *gl_info = context->gl_info;
6664 const DWORD *function = shader->function;
6665 struct shader_glsl_ctx_priv priv_ctx;
6666 GLuint shader_id;
6668 shader_id = GL_EXTCALL(glCreateShader(GL_COMPUTE_SHADER));
6670 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, &reg_maps->shader_version));
6672 shader_glsl_enable_extensions(buffer, gl_info);
6673 if (gl_info->supported[ARB_COMPUTE_SHADER])
6674 shader_addline(buffer, "#extension GL_ARB_compute_shader : enable\n");
6676 memset(&priv_ctx, 0, sizeof(priv_ctx));
6677 priv_ctx.string_buffers = string_buffers;
6678 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
6680 shader_addline(buffer, "layout(local_size_x = %u, local_size_y = %u, local_size_z = %u) in;\n",
6681 thread_group_size->x, thread_group_size->y, thread_group_size->z);
6683 shader_addline(buffer, "void main()\n{\n");
6684 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
6685 shader_addline(buffer, "}\n");
6687 TRACE("Compiling shader object %u.\n", shader_id);
6688 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
6690 return shader_id;
6693 static GLuint find_glsl_pshader(const struct wined3d_context *context,
6694 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6695 struct wined3d_shader *shader,
6696 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
6698 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
6699 struct glsl_shader_private *shader_data;
6700 struct ps_np2fixup_info *np2fixup;
6701 UINT i;
6702 DWORD new_size;
6703 GLuint ret;
6705 if (!shader->backend_data)
6707 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6708 if (!shader->backend_data)
6710 ERR("Failed to allocate backend data.\n");
6711 return 0;
6714 shader_data = shader->backend_data;
6715 gl_shaders = shader_data->gl_shaders.ps;
6717 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6718 * so a linear search is more performant than a hashmap or a binary search
6719 * (cache coherency etc)
6721 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6723 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6725 if (args->np2_fixup)
6726 *np2fixup_info = &gl_shaders[i].np2fixup;
6727 return gl_shaders[i].id;
6731 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6732 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6733 if (shader_data->num_gl_shaders)
6735 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6736 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
6737 new_size * sizeof(*gl_shaders));
6739 else
6741 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6742 new_size = 1;
6745 if(!new_array) {
6746 ERR("Out of memory\n");
6747 return 0;
6749 shader_data->gl_shaders.ps = new_array;
6750 shader_data->shader_array_size = new_size;
6751 gl_shaders = new_array;
6754 gl_shaders[shader_data->num_gl_shaders].args = *args;
6756 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
6757 memset(np2fixup, 0, sizeof(*np2fixup));
6758 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
6760 pixelshader_update_resource_types(shader, args->tex_types);
6762 string_buffer_clear(buffer);
6763 ret = shader_glsl_generate_pshader(context, buffer, string_buffers, shader, args, np2fixup);
6764 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6766 return ret;
6769 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
6770 const DWORD use_map)
6772 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
6773 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
6774 if (stored->point_size != new->point_size)
6775 return FALSE;
6776 if (stored->per_vertex_point_size != new->per_vertex_point_size)
6777 return FALSE;
6778 if (stored->flatshading != new->flatshading)
6779 return FALSE;
6780 if (stored->next_shader_type != new->next_shader_type)
6781 return FALSE;
6782 if (stored->next_shader_input_count != new->next_shader_input_count)
6783 return FALSE;
6784 return stored->fog_src == new->fog_src;
6787 static GLuint find_glsl_vshader(const struct wined3d_context *context, struct shader_glsl_priv *priv,
6788 struct wined3d_shader *shader, const struct vs_compile_args *args)
6790 UINT i;
6791 DWORD new_size;
6792 DWORD use_map = context->stream_info.use_map;
6793 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
6794 struct glsl_shader_private *shader_data;
6795 GLuint ret;
6797 if (!shader->backend_data)
6799 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
6800 if (!shader->backend_data)
6802 ERR("Failed to allocate backend data.\n");
6803 return 0;
6806 shader_data = shader->backend_data;
6807 gl_shaders = shader_data->gl_shaders.vs;
6809 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
6810 * so a linear search is more performant than a hashmap or a binary search
6811 * (cache coherency etc)
6813 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6815 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
6816 return gl_shaders[i].id;
6819 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6821 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
6822 if (shader_data->num_gl_shaders)
6824 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
6825 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
6826 new_size * sizeof(*gl_shaders));
6828 else
6830 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
6831 new_size = 1;
6834 if(!new_array) {
6835 ERR("Out of memory\n");
6836 return 0;
6838 shader_data->gl_shaders.vs = new_array;
6839 shader_data->shader_array_size = new_size;
6840 gl_shaders = new_array;
6843 gl_shaders[shader_data->num_gl_shaders].args = *args;
6845 string_buffer_clear(&priv->shader_buffer);
6846 ret = shader_glsl_generate_vshader(context, priv, shader, args);
6847 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6849 return ret;
6852 static GLuint find_glsl_geometry_shader(const struct wined3d_context *context,
6853 struct shader_glsl_priv *priv, struct wined3d_shader *shader, const struct gs_compile_args *args)
6855 struct glsl_gs_compiled_shader *gl_shaders, *new_array;
6856 struct glsl_shader_private *shader_data;
6857 unsigned int i, new_size;
6858 GLuint ret;
6860 if (!shader->backend_data)
6862 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6864 ERR("Failed to allocate backend data.\n");
6865 return 0;
6868 shader_data = shader->backend_data;
6869 gl_shaders = shader_data->gl_shaders.gs;
6871 for (i = 0; i < shader_data->num_gl_shaders; ++i)
6873 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
6874 return gl_shaders[i].id;
6877 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6879 if (shader_data->num_gl_shaders)
6881 new_size = shader_data->shader_array_size + 1;
6882 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.gs,
6883 new_size * sizeof(*new_array));
6885 else
6887 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*new_array));
6888 new_size = 1;
6891 if (!new_array)
6893 ERR("Failed to allocate GL shaders array.\n");
6894 return 0;
6896 shader_data->gl_shaders.gs = new_array;
6897 shader_data->shader_array_size = new_size;
6898 gl_shaders = new_array;
6900 string_buffer_clear(&priv->shader_buffer);
6901 ret = shader_glsl_generate_geometry_shader(context, priv, shader, args);
6902 gl_shaders[shader_data->num_gl_shaders].args = *args;
6903 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6905 return ret;
6908 static GLuint find_glsl_compute_shader(const struct wined3d_context *context,
6909 struct wined3d_string_buffer *buffer, struct wined3d_string_buffer_list *string_buffers,
6910 struct wined3d_shader *shader)
6912 struct glsl_cs_compiled_shader *gl_shaders;
6913 struct glsl_shader_private *shader_data;
6914 GLuint ret;
6916 if (!shader->backend_data)
6918 if (!(shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data))))
6920 ERR("Failed to allocate backend data.\n");
6921 return 0;
6924 shader_data = shader->backend_data;
6925 gl_shaders = shader_data->gl_shaders.cs;
6927 /* No shader variants are used for compute shaders. */
6928 if (shader_data->num_gl_shaders)
6929 return gl_shaders[0].id;
6931 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
6933 if (!(shader_data->gl_shaders.cs = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders))))
6935 ERR("Failed to allocate GL shader array.\n");
6936 return 0;
6938 shader_data->shader_array_size = 1;
6939 gl_shaders = shader_data->gl_shaders.cs;
6941 string_buffer_clear(buffer);
6942 ret = shader_glsl_generate_compute_shader(context, buffer, string_buffers, shader);
6943 gl_shaders[shader_data->num_gl_shaders++].id = ret;
6945 return ret;
6948 static const char *shader_glsl_ffp_mcs(enum wined3d_material_color_source mcs, const char *material)
6950 switch (mcs)
6952 case WINED3D_MCS_MATERIAL:
6953 return material;
6954 case WINED3D_MCS_COLOR1:
6955 return "ffp_attrib_diffuse";
6956 case WINED3D_MCS_COLOR2:
6957 return "ffp_attrib_specular";
6958 default:
6959 ERR("Invalid material color source %#x.\n", mcs);
6960 return "<invalid>";
6964 static void shader_glsl_ffp_vertex_lighting(struct wined3d_string_buffer *buffer,
6965 const struct wined3d_ffp_vs_settings *settings, BOOL legacy_lighting)
6967 const char *diffuse, *specular, *emissive, *ambient;
6968 unsigned int i, idx;
6970 if (!settings->lighting)
6972 shader_addline(buffer, "ffp_varying_diffuse = ffp_attrib_diffuse;\n");
6973 shader_addline(buffer, "ffp_varying_specular = ffp_attrib_specular;\n");
6974 return;
6977 shader_addline(buffer, "vec3 ambient = ffp_light_ambient;\n");
6978 shader_addline(buffer, "vec3 diffuse = vec3(0.0);\n");
6979 shader_addline(buffer, "vec4 specular = vec4(0.0);\n");
6980 shader_addline(buffer, "vec3 dir, dst;\n");
6981 shader_addline(buffer, "float att, t;\n");
6983 ambient = shader_glsl_ffp_mcs(settings->ambient_source, "ffp_material.ambient");
6984 diffuse = shader_glsl_ffp_mcs(settings->diffuse_source, "ffp_material.diffuse");
6985 specular = shader_glsl_ffp_mcs(settings->specular_source, "ffp_material.specular");
6986 emissive = shader_glsl_ffp_mcs(settings->emissive_source, "ffp_material.emissive");
6988 idx = 0;
6989 for (i = 0; i < settings->point_light_count; ++i, ++idx)
6991 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
6992 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
6993 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
6994 shader_addline(buffer, "dst.x = 1.0;\n");
6995 if (legacy_lighting)
6997 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
6998 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7000 else
7002 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7004 shader_addline(buffer, "att = dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7005 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n", idx, idx, idx);
7006 if (!legacy_lighting)
7007 shader_addline(buffer, "att = 1.0 / att;\n");
7008 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7009 if (!settings->normal)
7011 if (!legacy_lighting)
7012 shader_addline(buffer, "}\n");
7013 continue;
7015 shader_addline(buffer, "dir = normalize(dir);\n");
7016 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7017 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7018 if (settings->localviewer)
7019 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7020 else
7021 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7022 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7023 " * ffp_light[%u].specular) * att;\n", idx);
7024 if (!legacy_lighting)
7025 shader_addline(buffer, "}\n");
7028 for (i = 0; i < settings->spot_light_count; ++i, ++idx)
7030 shader_addline(buffer, "dir = ffp_light[%u].position.xyz - ec_pos.xyz;\n", idx);
7031 shader_addline(buffer, "dst.z = dot(dir, dir);\n");
7032 shader_addline(buffer, "dst.y = sqrt(dst.z);\n");
7033 shader_addline(buffer, "dst.x = 1.0;\n");
7034 if (legacy_lighting)
7036 shader_addline(buffer, "dst.y = (ffp_light[%u].range - dst.y) / ffp_light[%u].range;\n", idx, idx);
7037 shader_addline(buffer, "dst.z = dst.y * dst.y;\n");
7039 else
7041 shader_addline(buffer, "if (dst.y <= ffp_light[%u].range)\n{\n", idx);
7043 shader_addline(buffer, "dir = normalize(dir);\n");
7044 shader_addline(buffer, "t = dot(-dir, normalize(ffp_light[%u].direction));\n", idx);
7045 shader_addline(buffer, "if (t > ffp_light[%u].cos_htheta) att = 1.0;\n", idx);
7046 shader_addline(buffer, "else if (t <= ffp_light[%u].cos_hphi) att = 0.0;\n", idx);
7047 shader_addline(buffer, "else att = pow((t - ffp_light[%u].cos_hphi)"
7048 " / (ffp_light[%u].cos_htheta - ffp_light[%u].cos_hphi), ffp_light[%u].falloff);\n",
7049 idx, idx, idx, idx);
7050 if (legacy_lighting)
7051 shader_addline(buffer, "att *= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7052 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7053 idx, idx, idx);
7054 else
7055 shader_addline(buffer, "att /= dot(dst.xyz, vec3(ffp_light[%u].c_att,"
7056 " ffp_light[%u].l_att, ffp_light[%u].q_att));\n",
7057 idx, idx, idx);
7058 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz * att;\n", idx);
7059 if (!settings->normal)
7061 if (!legacy_lighting)
7062 shader_addline(buffer, "}\n");
7063 continue;
7065 shader_addline(buffer, "diffuse += (clamp(dot(dir, normal), 0.0, 1.0)"
7066 " * ffp_light[%u].diffuse.xyz) * att;\n", idx);
7067 if (settings->localviewer)
7068 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7069 else
7070 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7071 shader_addline(buffer, "if (t > 0.0) specular += (pow(t, ffp_material.shininess)"
7072 " * ffp_light[%u].specular) * att;\n", idx);
7073 if (!legacy_lighting)
7074 shader_addline(buffer, "}\n");
7077 for (i = 0; i < settings->directional_light_count; ++i, ++idx)
7079 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7080 if (!settings->normal)
7081 continue;
7082 shader_addline(buffer, "dir = normalize(ffp_light[%u].direction.xyz);\n", idx);
7083 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7084 " * ffp_light[%u].diffuse.xyz;\n", idx);
7085 /* TODO: In the non-local viewer case the halfvector is constant
7086 * and could be precomputed and stored in a uniform. */
7087 if (settings->localviewer)
7088 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7089 else
7090 shader_addline(buffer, "t = dot(normal, normalize(dir + vec3(0.0, 0.0, -1.0)));\n");
7091 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7092 " * ffp_light[%u].specular;\n", idx);
7095 for (i = 0; i < settings->parallel_point_light_count; ++i, ++idx)
7097 shader_addline(buffer, "ambient += ffp_light[%u].ambient.xyz;\n", idx);
7098 if (!settings->normal)
7099 continue;
7100 shader_addline(buffer, "dir = normalize(ffp_light[%u].position.xyz);\n", idx);
7101 shader_addline(buffer, "diffuse += clamp(dot(dir, normal), 0.0, 1.0)"
7102 " * ffp_light[%u].diffuse.xyz;\n", idx);
7103 shader_addline(buffer, "t = dot(normal, normalize(dir - normalize(ec_pos.xyz)));\n");
7104 shader_addline(buffer, "if (t > 0.0) specular += pow(t, ffp_material.shininess)"
7105 " * ffp_light[%u].specular;\n", idx);
7108 shader_addline(buffer, "ffp_varying_diffuse.xyz = %s.xyz * ambient + %s.xyz * diffuse + %s.xyz;\n",
7109 ambient, diffuse, emissive);
7110 shader_addline(buffer, "ffp_varying_diffuse.w = %s.w;\n", diffuse);
7111 shader_addline(buffer, "ffp_varying_specular = %s * specular;\n", specular);
7114 /* Context activation is done by the caller. */
7115 static GLuint shader_glsl_generate_ffp_vertex_shader(struct shader_glsl_priv *priv,
7116 const struct wined3d_ffp_vs_settings *settings, const struct wined3d_gl_info *gl_info)
7118 static const struct attrib_info
7120 const char type[6];
7121 const char name[24];
7123 attrib_info[] =
7125 {"vec4", "ffp_attrib_position"}, /* WINED3D_FFP_POSITION */
7126 {"vec4", "ffp_attrib_blendweight"}, /* WINED3D_FFP_BLENDWEIGHT */
7127 /* TODO: Indexed vertex blending */
7128 {"float", ""}, /* WINED3D_FFP_BLENDINDICES */
7129 {"vec3", "ffp_attrib_normal"}, /* WINED3D_FFP_NORMAL */
7130 {"float", "ffp_attrib_psize"}, /* WINED3D_FFP_PSIZE */
7131 {"vec4", "ffp_attrib_diffuse"}, /* WINED3D_FFP_DIFFUSE */
7132 {"vec4", "ffp_attrib_specular"}, /* WINED3D_FFP_SPECULAR */
7134 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7135 BOOL legacy_lighting = priv->legacy_lighting;
7136 GLuint shader_obj;
7137 unsigned int i;
7138 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7139 BOOL output_legacy_fogcoord = legacy_context;
7141 string_buffer_clear(buffer);
7143 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7145 if (shader_glsl_use_explicit_attrib_location(gl_info))
7146 shader_addline(buffer, "#extension GL_ARB_explicit_attrib_location : enable\n");
7148 for (i = 0; i < WINED3D_FFP_ATTRIBS_COUNT; ++i)
7150 const char *type = i < ARRAY_SIZE(attrib_info) ? attrib_info[i].type : "vec4";
7152 if (shader_glsl_use_explicit_attrib_location(gl_info))
7153 shader_addline(buffer, "layout(location = %u) ", i);
7154 shader_addline(buffer, "%s %s vs_in%u;\n", get_attribute_keyword(gl_info), type, i);
7156 shader_addline(buffer, "\n");
7158 shader_addline(buffer, "uniform mat4 ffp_modelview_matrix[%u];\n", MAX_VERTEX_BLENDS);
7159 shader_addline(buffer, "uniform mat4 ffp_projection_matrix;\n");
7160 shader_addline(buffer, "uniform mat3 ffp_normal_matrix;\n");
7161 shader_addline(buffer, "uniform mat4 ffp_texture_matrix[%u];\n", MAX_TEXTURES);
7163 shader_addline(buffer, "uniform struct\n{\n");
7164 shader_addline(buffer, " vec4 emissive;\n");
7165 shader_addline(buffer, " vec4 ambient;\n");
7166 shader_addline(buffer, " vec4 diffuse;\n");
7167 shader_addline(buffer, " vec4 specular;\n");
7168 shader_addline(buffer, " float shininess;\n");
7169 shader_addline(buffer, "} ffp_material;\n");
7171 shader_addline(buffer, "uniform vec3 ffp_light_ambient;\n");
7172 shader_addline(buffer, "uniform struct\n{\n");
7173 shader_addline(buffer, " vec4 diffuse;\n");
7174 shader_addline(buffer, " vec4 specular;\n");
7175 shader_addline(buffer, " vec4 ambient;\n");
7176 shader_addline(buffer, " vec4 position;\n");
7177 shader_addline(buffer, " vec3 direction;\n");
7178 shader_addline(buffer, " float range;\n");
7179 shader_addline(buffer, " float falloff;\n");
7180 shader_addline(buffer, " float c_att;\n");
7181 shader_addline(buffer, " float l_att;\n");
7182 shader_addline(buffer, " float q_att;\n");
7183 shader_addline(buffer, " float cos_htheta;\n");
7184 shader_addline(buffer, " float cos_hphi;\n");
7185 shader_addline(buffer, "} ffp_light[%u];\n", MAX_ACTIVE_LIGHTS);
7187 if (settings->point_size)
7189 shader_addline(buffer, "uniform struct\n{\n");
7190 shader_addline(buffer, " float size;\n");
7191 shader_addline(buffer, " float size_min;\n");
7192 shader_addline(buffer, " float size_max;\n");
7193 shader_addline(buffer, " float c_att;\n");
7194 shader_addline(buffer, " float l_att;\n");
7195 shader_addline(buffer, " float q_att;\n");
7196 shader_addline(buffer, "} ffp_point;\n");
7199 if (legacy_context)
7201 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7202 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7203 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7204 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7206 else
7208 if (settings->clipping)
7209 shader_addline(buffer, "uniform vec4 clip_planes[%u];\n", gl_info->limits.user_clip_distances);
7211 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7212 declare_out_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7213 declare_out_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7214 declare_out_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7217 shader_addline(buffer, "\nvoid main()\n{\n");
7218 shader_addline(buffer, "float m;\n");
7219 shader_addline(buffer, "vec3 r;\n");
7221 for (i = 0; i < ARRAY_SIZE(attrib_info); ++i)
7223 if (attrib_info[i].name[0])
7224 shader_addline(buffer, "%s %s = vs_in%u%s;\n", attrib_info[i].type, attrib_info[i].name,
7225 i, settings->swizzle_map & (1u << i) ? ".zyxw" : "");
7227 for (i = 0; i < MAX_TEXTURES; ++i)
7229 unsigned int coord_idx = settings->texgen[i] & 0x0000ffff;
7230 if ((settings->texgen[i] & 0xffff0000) == WINED3DTSS_TCI_PASSTHRU
7231 && settings->texcoords & (1u << i))
7232 shader_addline(buffer, "vec4 ffp_attrib_texcoord%u = vs_in%u;\n", i, coord_idx + WINED3D_FFP_TEXCOORD0);
7235 shader_addline(buffer, "ffp_attrib_blendweight[%u] = 1.0;\n", settings->vertexblends);
7237 if (settings->transformed)
7239 shader_addline(buffer, "vec4 ec_pos = vec4(ffp_attrib_position.xyz, 1.0);\n");
7240 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7241 shader_addline(buffer, "if (ffp_attrib_position.w != 0.0) gl_Position /= ffp_attrib_position.w;\n");
7243 else
7245 for (i = 0; i < settings->vertexblends; ++i)
7246 shader_addline(buffer, "ffp_attrib_blendweight[%u] -= ffp_attrib_blendweight[%u];\n", settings->vertexblends, i);
7248 shader_addline(buffer, "vec4 ec_pos = vec4(0.0);\n");
7249 for (i = 0; i < settings->vertexblends + 1; ++i)
7250 shader_addline(buffer, "ec_pos += ffp_attrib_blendweight[%u] * (ffp_modelview_matrix[%u] * ffp_attrib_position);\n", i, i);
7252 shader_addline(buffer, "gl_Position = ffp_projection_matrix * ec_pos;\n");
7253 if (settings->clipping)
7255 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
7256 shader_addline(buffer, "gl_ClipVertex = ec_pos;\n");
7257 else
7258 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
7259 shader_addline(buffer, "gl_ClipDistance[%u] = dot(ec_pos, clip_planes[%u]);\n", i, i);
7261 shader_addline(buffer, "ec_pos /= ec_pos.w;\n");
7264 shader_addline(buffer, "vec3 normal = vec3(0.0);\n");
7265 if (settings->normal)
7267 if (!settings->vertexblends)
7269 shader_addline(buffer, "normal = ffp_normal_matrix * ffp_attrib_normal;\n");
7271 else
7273 for (i = 0; i < settings->vertexblends + 1; ++i)
7274 shader_addline(buffer, "normal += ffp_attrib_blendweight[%u] * (mat3(ffp_modelview_matrix[%u]) * ffp_attrib_normal);\n", i, i);
7277 if (settings->normalize)
7278 shader_addline(buffer, "normal = normalize(normal);\n");
7281 shader_glsl_ffp_vertex_lighting(buffer, settings, legacy_lighting);
7282 if (legacy_context)
7284 shader_addline(buffer, "gl_FrontColor = ffp_varying_diffuse;\n");
7285 shader_addline(buffer, "gl_FrontSecondaryColor = ffp_varying_specular;\n");
7287 else
7289 shader_addline(buffer, "ffp_varying_diffuse = clamp(ffp_varying_diffuse, 0.0, 1.0);\n");
7290 shader_addline(buffer, "ffp_varying_specular = clamp(ffp_varying_specular, 0.0, 1.0);\n");
7293 for (i = 0; i < MAX_TEXTURES; ++i)
7295 BOOL output_legacy_texcoord = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7297 switch (settings->texgen[i] & 0xffff0000)
7299 case WINED3DTSS_TCI_PASSTHRU:
7300 if (settings->texcoords & (1u << i))
7301 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ffp_attrib_texcoord%u;\n",
7302 i, i, i);
7303 else if (gl_info->limits.glsl_varyings >= wined3d_max_compat_varyings(gl_info))
7304 shader_addline(buffer, "ffp_varying_texcoord[%u] = vec4(0.0);\n", i);
7305 else
7306 output_legacy_texcoord = FALSE;
7307 break;
7309 case WINED3DTSS_TCI_CAMERASPACENORMAL:
7310 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * vec4(normal, 1.0);\n", i, i);
7311 break;
7313 case WINED3DTSS_TCI_CAMERASPACEPOSITION:
7314 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u] * ec_pos;\n", i, i);
7315 break;
7317 case WINED3DTSS_TCI_CAMERASPACEREFLECTIONVECTOR:
7318 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7319 " * vec4(reflect(normalize(ec_pos.xyz), normal), 1.0);\n", i, i);
7320 break;
7322 case WINED3DTSS_TCI_SPHEREMAP:
7323 shader_addline(buffer, "r = reflect(normalize(ec_pos.xyz), normal);\n");
7324 shader_addline(buffer, "m = 2.0 * length(vec3(r.x, r.y, r.z + 1.0));\n");
7325 shader_addline(buffer, "ffp_varying_texcoord[%u] = ffp_texture_matrix[%u]"
7326 " * vec4(r.x / m + 0.5, r.y / m + 0.5, 0.0, 1.0);\n", i, i);
7327 break;
7329 default:
7330 ERR("Unhandled texgen %#x.\n", settings->texgen[i]);
7331 break;
7333 if (output_legacy_texcoord)
7334 shader_addline(buffer, "gl_TexCoord[%u] = ffp_varying_texcoord[%u];\n", i, i);
7337 switch (settings->fog_mode)
7339 case WINED3D_FFP_VS_FOG_OFF:
7340 output_legacy_fogcoord = FALSE;
7341 break;
7343 case WINED3D_FFP_VS_FOG_FOGCOORD:
7344 shader_addline(buffer, "ffp_varying_fogcoord = ffp_attrib_specular.w * 255.0;\n");
7345 break;
7347 case WINED3D_FFP_VS_FOG_RANGE:
7348 shader_addline(buffer, "ffp_varying_fogcoord = length(ec_pos.xyz);\n");
7349 break;
7351 case WINED3D_FFP_VS_FOG_DEPTH:
7352 if (settings->ortho_fog)
7354 if (gl_info->supported[ARB_CLIP_CONTROL])
7355 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z;\n");
7356 else
7357 /* Need to undo the [0.0 - 1.0] -> [-1.0 - 1.0] transformation from D3D to GL coordinates. */
7358 shader_addline(buffer, "ffp_varying_fogcoord = gl_Position.z * 0.5 + 0.5;\n");
7360 else if (settings->transformed)
7362 shader_addline(buffer, "ffp_varying_fogcoord = ec_pos.z;\n");
7364 else
7366 shader_addline(buffer, "ffp_varying_fogcoord = abs(ec_pos.z);\n");
7368 break;
7370 default:
7371 ERR("Unhandled fog mode %#x.\n", settings->fog_mode);
7372 break;
7374 if (output_legacy_fogcoord)
7375 shader_addline(buffer, "gl_FogFragCoord = ffp_varying_fogcoord;\n");
7377 if (settings->point_size)
7379 shader_addline(buffer, "gl_PointSize = %s / sqrt(ffp_point.c_att"
7380 " + ffp_point.l_att * length(ec_pos.xyz)"
7381 " + ffp_point.q_att * dot(ec_pos.xyz, ec_pos.xyz));\n",
7382 settings->per_vertex_point_size ? "ffp_attrib_psize" : "ffp_point.size");
7383 shader_addline(buffer, "gl_PointSize = clamp(gl_PointSize, ffp_point.size_min, ffp_point.size_max);\n");
7386 shader_addline(buffer, "}\n");
7388 shader_obj = GL_EXTCALL(glCreateShader(GL_VERTEX_SHADER));
7389 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
7391 return shader_obj;
7394 static const char *shader_glsl_get_ffp_fragment_op_arg(struct wined3d_string_buffer *buffer,
7395 DWORD argnum, unsigned int stage, DWORD arg)
7397 const char *ret;
7399 if (arg == ARG_UNUSED)
7400 return "<unused arg>";
7402 switch (arg & WINED3DTA_SELECTMASK)
7404 case WINED3DTA_DIFFUSE:
7405 ret = "ffp_varying_diffuse";
7406 break;
7408 case WINED3DTA_CURRENT:
7409 ret = "ret";
7410 break;
7412 case WINED3DTA_TEXTURE:
7413 switch (stage)
7415 case 0: ret = "tex0"; break;
7416 case 1: ret = "tex1"; break;
7417 case 2: ret = "tex2"; break;
7418 case 3: ret = "tex3"; break;
7419 case 4: ret = "tex4"; break;
7420 case 5: ret = "tex5"; break;
7421 case 6: ret = "tex6"; break;
7422 case 7: ret = "tex7"; break;
7423 default:
7424 ret = "<invalid texture>";
7425 break;
7427 break;
7429 case WINED3DTA_TFACTOR:
7430 ret = "tex_factor";
7431 break;
7433 case WINED3DTA_SPECULAR:
7434 ret = "ffp_varying_specular";
7435 break;
7437 case WINED3DTA_TEMP:
7438 ret = "temp_reg";
7439 break;
7441 case WINED3DTA_CONSTANT:
7442 switch (stage)
7444 case 0: ret = "tss_const0"; break;
7445 case 1: ret = "tss_const1"; break;
7446 case 2: ret = "tss_const2"; break;
7447 case 3: ret = "tss_const3"; break;
7448 case 4: ret = "tss_const4"; break;
7449 case 5: ret = "tss_const5"; break;
7450 case 6: ret = "tss_const6"; break;
7451 case 7: ret = "tss_const7"; break;
7452 default:
7453 ret = "<invalid constant>";
7454 break;
7456 break;
7458 default:
7459 return "<unhandled arg>";
7462 if (arg & WINED3DTA_COMPLEMENT)
7464 shader_addline(buffer, "arg%u = vec4(1.0) - %s;\n", argnum, ret);
7465 if (argnum == 0)
7466 ret = "arg0";
7467 else if (argnum == 1)
7468 ret = "arg1";
7469 else if (argnum == 2)
7470 ret = "arg2";
7473 if (arg & WINED3DTA_ALPHAREPLICATE)
7475 shader_addline(buffer, "arg%u = vec4(%s.w);\n", argnum, ret);
7476 if (argnum == 0)
7477 ret = "arg0";
7478 else if (argnum == 1)
7479 ret = "arg1";
7480 else if (argnum == 2)
7481 ret = "arg2";
7484 return ret;
7487 static void shader_glsl_ffp_fragment_op(struct wined3d_string_buffer *buffer, unsigned int stage, BOOL color,
7488 BOOL alpha, DWORD dst, DWORD op, DWORD dw_arg0, DWORD dw_arg1, DWORD dw_arg2)
7490 const char *dstmask, *dstreg, *arg0, *arg1, *arg2;
7492 if (color && alpha)
7493 dstmask = "";
7494 else if (color)
7495 dstmask = ".xyz";
7496 else
7497 dstmask = ".w";
7499 if (dst == tempreg)
7500 dstreg = "temp_reg";
7501 else
7502 dstreg = "ret";
7504 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, dw_arg0);
7505 arg1 = shader_glsl_get_ffp_fragment_op_arg(buffer, 1, stage, dw_arg1);
7506 arg2 = shader_glsl_get_ffp_fragment_op_arg(buffer, 2, stage, dw_arg2);
7508 switch (op)
7510 case WINED3D_TOP_DISABLE:
7511 break;
7513 case WINED3D_TOP_SELECT_ARG1:
7514 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg1, dstmask);
7515 break;
7517 case WINED3D_TOP_SELECT_ARG2:
7518 shader_addline(buffer, "%s%s = %s%s;\n", dstreg, dstmask, arg2, dstmask);
7519 break;
7521 case WINED3D_TOP_MODULATE:
7522 shader_addline(buffer, "%s%s = %s%s * %s%s;\n", dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7523 break;
7525 case WINED3D_TOP_MODULATE_4X:
7526 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 4.0, 0.0, 1.0);\n",
7527 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7528 break;
7530 case WINED3D_TOP_MODULATE_2X:
7531 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s * 2.0, 0.0, 1.0);\n",
7532 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7533 break;
7535 case WINED3D_TOP_ADD:
7536 shader_addline(buffer, "%s%s = clamp(%s%s + %s%s, 0.0, 1.0);\n",
7537 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7538 break;
7540 case WINED3D_TOP_ADD_SIGNED:
7541 shader_addline(buffer, "%s%s = clamp(%s%s + (%s - vec4(0.5))%s, 0.0, 1.0);\n",
7542 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7543 break;
7545 case WINED3D_TOP_ADD_SIGNED_2X:
7546 shader_addline(buffer, "%s%s = clamp((%s%s + (%s - vec4(0.5))%s) * 2.0, 0.0, 1.0);\n",
7547 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7548 break;
7550 case WINED3D_TOP_SUBTRACT:
7551 shader_addline(buffer, "%s%s = clamp(%s%s - %s%s, 0.0, 1.0);\n",
7552 dstreg, dstmask, arg1, dstmask, arg2, dstmask);
7553 break;
7555 case WINED3D_TOP_ADD_SMOOTH:
7556 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s%s, 0.0, 1.0);\n",
7557 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1, dstmask);
7558 break;
7560 case WINED3D_TOP_BLEND_DIFFUSE_ALPHA:
7561 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_DIFFUSE);
7562 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7563 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7564 break;
7566 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7567 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7568 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7569 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7570 break;
7572 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7573 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TFACTOR);
7574 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7575 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7576 break;
7578 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7579 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_TEXTURE);
7580 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7581 dstreg, dstmask, arg2, dstmask, arg0, arg1, dstmask);
7582 break;
7584 case WINED3D_TOP_BLEND_CURRENT_ALPHA:
7585 arg0 = shader_glsl_get_ffp_fragment_op_arg(buffer, 0, stage, WINED3DTA_CURRENT);
7586 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s.w);\n",
7587 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0);
7588 break;
7590 case WINED3D_TOP_MODULATE_ALPHA_ADD_COLOR:
7591 shader_addline(buffer, "%s%s = clamp(%s%s * %s.w + %s%s, 0.0, 1.0);\n",
7592 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7593 break;
7595 case WINED3D_TOP_MODULATE_COLOR_ADD_ALPHA:
7596 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s.w, 0.0, 1.0);\n",
7597 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7598 break;
7600 case WINED3D_TOP_MODULATE_INVALPHA_ADD_COLOR:
7601 shader_addline(buffer, "%s%s = clamp(%s%s * (1.0 - %s.w) + %s%s, 0.0, 1.0);\n",
7602 dstreg, dstmask, arg2, dstmask, arg1, arg1, dstmask);
7603 break;
7604 case WINED3D_TOP_MODULATE_INVCOLOR_ADD_ALPHA:
7605 shader_addline(buffer, "%s%s = clamp((vec4(1.0) - %s)%s * %s%s + %s.w, 0.0, 1.0);\n",
7606 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg1);
7607 break;
7609 case WINED3D_TOP_BUMPENVMAP:
7610 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7611 /* These are handled in the first pass, nothing to do. */
7612 break;
7614 case WINED3D_TOP_DOTPRODUCT3:
7615 shader_addline(buffer, "%s%s = vec4(clamp(dot(%s.xyz - 0.5, %s.xyz - 0.5) * 4.0, 0.0, 1.0))%s;\n",
7616 dstreg, dstmask, arg1, arg2, dstmask);
7617 break;
7619 case WINED3D_TOP_MULTIPLY_ADD:
7620 shader_addline(buffer, "%s%s = clamp(%s%s * %s%s + %s%s, 0.0, 1.0);\n",
7621 dstreg, dstmask, arg1, dstmask, arg2, dstmask, arg0, dstmask);
7622 break;
7624 case WINED3D_TOP_LERP:
7625 /* MSDN isn't quite right here. */
7626 shader_addline(buffer, "%s%s = mix(%s%s, %s%s, %s%s);\n",
7627 dstreg, dstmask, arg2, dstmask, arg1, dstmask, arg0, dstmask);
7628 break;
7630 default:
7631 FIXME("Unhandled operation %#x.\n", op);
7632 break;
7636 /* Context activation is done by the caller. */
7637 static GLuint shader_glsl_generate_ffp_fragment_shader(struct shader_glsl_priv *priv,
7638 const struct ffp_frag_settings *settings, const struct wined3d_gl_info *gl_info)
7640 struct wined3d_string_buffer *tex_reg_name = string_buffer_get(&priv->string_buffers);
7641 enum wined3d_cmp_func alpha_test_func = settings->alpha_test_func + 1;
7642 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
7643 struct wined3d_string_buffer *buffer = &priv->shader_buffer;
7644 BYTE lum_map = 0, bump_map = 0, tex_map = 0, tss_const_map = 0;
7645 BOOL tempreg_used = FALSE, tfactor_used = FALSE;
7646 UINT lowest_disabled_stage;
7647 GLuint shader_id;
7648 DWORD arg0, arg1, arg2;
7649 unsigned int stage;
7651 string_buffer_clear(buffer);
7653 /* Find out which textures are read */
7654 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7656 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7657 break;
7659 arg0 = settings->op[stage].carg0 & WINED3DTA_SELECTMASK;
7660 arg1 = settings->op[stage].carg1 & WINED3DTA_SELECTMASK;
7661 arg2 = settings->op[stage].carg2 & WINED3DTA_SELECTMASK;
7663 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE
7664 || (stage == 0 && settings->color_key_enabled))
7665 tex_map |= 1u << stage;
7666 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7667 tfactor_used = TRUE;
7668 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7669 tempreg_used = TRUE;
7670 if (settings->op[stage].dst == tempreg)
7671 tempreg_used = TRUE;
7672 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7673 tss_const_map |= 1u << stage;
7675 switch (settings->op[stage].cop)
7677 case WINED3D_TOP_BUMPENVMAP_LUMINANCE:
7678 lum_map |= 1u << stage;
7679 /* fall through */
7680 case WINED3D_TOP_BUMPENVMAP:
7681 bump_map |= 1u << stage;
7682 /* fall through */
7683 case WINED3D_TOP_BLEND_TEXTURE_ALPHA:
7684 case WINED3D_TOP_BLEND_TEXTURE_ALPHA_PM:
7685 tex_map |= 1u << stage;
7686 break;
7688 case WINED3D_TOP_BLEND_FACTOR_ALPHA:
7689 tfactor_used = TRUE;
7690 break;
7692 default:
7693 break;
7696 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
7697 continue;
7699 arg0 = settings->op[stage].aarg0 & WINED3DTA_SELECTMASK;
7700 arg1 = settings->op[stage].aarg1 & WINED3DTA_SELECTMASK;
7701 arg2 = settings->op[stage].aarg2 & WINED3DTA_SELECTMASK;
7703 if (arg0 == WINED3DTA_TEXTURE || arg1 == WINED3DTA_TEXTURE || arg2 == WINED3DTA_TEXTURE)
7704 tex_map |= 1u << stage;
7705 if (arg0 == WINED3DTA_TFACTOR || arg1 == WINED3DTA_TFACTOR || arg2 == WINED3DTA_TFACTOR)
7706 tfactor_used = TRUE;
7707 if (arg0 == WINED3DTA_TEMP || arg1 == WINED3DTA_TEMP || arg2 == WINED3DTA_TEMP)
7708 tempreg_used = TRUE;
7709 if (arg0 == WINED3DTA_CONSTANT || arg1 == WINED3DTA_CONSTANT || arg2 == WINED3DTA_CONSTANT)
7710 tss_const_map |= 1u << stage;
7712 lowest_disabled_stage = stage;
7714 shader_addline(buffer, "%s\n", shader_glsl_get_version_declaration(gl_info, NULL));
7716 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
7717 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
7719 if (!needs_legacy_glsl_syntax(gl_info))
7720 shader_addline(buffer, "out vec4 ps_out[1];\n");
7722 shader_addline(buffer, "vec4 tmp0, tmp1;\n");
7723 shader_addline(buffer, "vec4 ret;\n");
7724 if (tempreg_used || settings->sRGB_write)
7725 shader_addline(buffer, "vec4 temp_reg = vec4(0.0);\n");
7726 shader_addline(buffer, "vec4 arg0, arg1, arg2;\n");
7728 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7730 if (tss_const_map & (1u << stage))
7731 shader_addline(buffer, "uniform vec4 tss_const%u;\n", stage);
7733 if (!(tex_map & (1u << stage)))
7734 continue;
7736 switch (settings->op[stage].tex_type)
7738 case WINED3D_GL_RES_TYPE_TEX_1D:
7739 shader_addline(buffer, "uniform sampler1D ps_sampler%u;\n", stage);
7740 break;
7741 case WINED3D_GL_RES_TYPE_TEX_2D:
7742 shader_addline(buffer, "uniform sampler2D ps_sampler%u;\n", stage);
7743 break;
7744 case WINED3D_GL_RES_TYPE_TEX_3D:
7745 shader_addline(buffer, "uniform sampler3D ps_sampler%u;\n", stage);
7746 break;
7747 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7748 shader_addline(buffer, "uniform samplerCube ps_sampler%u;\n", stage);
7749 break;
7750 case WINED3D_GL_RES_TYPE_TEX_RECT:
7751 shader_addline(buffer, "uniform sampler2DRect ps_sampler%u;\n", stage);
7752 break;
7753 default:
7754 FIXME("Unhandled sampler type %#x.\n", settings->op[stage].tex_type);
7755 break;
7758 shader_addline(buffer, "vec4 tex%u;\n", stage);
7760 if (!(bump_map & (1u << stage)))
7761 continue;
7762 shader_addline(buffer, "uniform mat2 bumpenv_mat%u;\n", stage);
7764 if (!(lum_map & (1u << stage)))
7765 continue;
7766 shader_addline(buffer, "uniform float bumpenv_lum_scale%u;\n", stage);
7767 shader_addline(buffer, "uniform float bumpenv_lum_offset%u;\n", stage);
7769 if (tfactor_used)
7770 shader_addline(buffer, "uniform vec4 tex_factor;\n");
7771 if (settings->color_key_enabled)
7772 shader_addline(buffer, "uniform vec4 color_key[2];\n");
7773 shader_addline(buffer, "uniform vec4 specular_enable;\n");
7775 if (settings->sRGB_write)
7777 shader_addline(buffer, "const vec4 srgb_const0 = ");
7778 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const0);
7779 shader_addline(buffer, ";\n");
7780 shader_addline(buffer, "const vec4 srgb_const1 = ");
7781 shader_glsl_append_imm_vec4(buffer, wined3d_srgb_const1);
7782 shader_addline(buffer, ";\n");
7785 shader_addline(buffer, "uniform struct\n{\n");
7786 shader_addline(buffer, " vec4 color;\n");
7787 shader_addline(buffer, " float density;\n");
7788 shader_addline(buffer, " float end;\n");
7789 shader_addline(buffer, " float scale;\n");
7790 shader_addline(buffer, "} ffp_fog;\n");
7792 if (alpha_test_func != WINED3D_CMP_ALWAYS)
7793 shader_addline(buffer, "uniform float alpha_test_ref;\n");
7795 if (legacy_context)
7797 shader_addline(buffer, "vec4 ffp_varying_diffuse;\n");
7798 shader_addline(buffer, "vec4 ffp_varying_specular;\n");
7799 shader_addline(buffer, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7800 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7801 shader_addline(buffer, "float ffp_varying_fogcoord;\n");
7803 else
7805 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_diffuse;\n");
7806 declare_in_varying(gl_info, buffer, settings->flatshading, "vec4 ffp_varying_specular;\n");
7807 declare_in_varying(gl_info, buffer, FALSE, "vec4 ffp_varying_texcoord[%u];\n", MAX_TEXTURES);
7808 shader_addline(buffer, "vec4 ffp_texcoord[%u];\n", MAX_TEXTURES);
7809 declare_in_varying(gl_info, buffer, FALSE, "float ffp_varying_fogcoord;\n");
7812 shader_addline(buffer, "void main()\n{\n");
7814 if (legacy_context)
7816 shader_addline(buffer, "ffp_varying_diffuse = gl_Color;\n");
7817 shader_addline(buffer, "ffp_varying_specular = gl_SecondaryColor;\n");
7820 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7822 if (tex_map & (1u << stage))
7824 if (settings->pointsprite)
7825 shader_addline(buffer, "ffp_texcoord[%u] = vec4(gl_PointCoord.xy, 0.0, 0.0);\n", stage);
7826 else if (settings->texcoords_initialized & (1u << stage))
7827 shader_addline(buffer, "ffp_texcoord[%u] = %s[%u];\n",
7828 stage, legacy_context ? "gl_TexCoord" : "ffp_varying_texcoord", stage);
7829 else
7830 shader_addline(buffer, "ffp_texcoord[%u] = vec4(0.0);\n", stage);
7834 if (legacy_context && settings->fog != WINED3D_FFP_PS_FOG_OFF)
7835 shader_addline(buffer, "ffp_varying_fogcoord = gl_FogFragCoord;\n");
7837 if (lowest_disabled_stage < 7 && settings->emul_clipplanes)
7838 shader_addline(buffer, "if (any(lessThan(ffp_texcoord[7], vec4(0.0)))) discard;\n");
7840 /* Generate texture sampling instructions */
7841 for (stage = 0; stage < MAX_TEXTURES && settings->op[stage].cop != WINED3D_TOP_DISABLE; ++stage)
7843 const char *texture_function, *coord_mask;
7844 BOOL proj;
7846 if (!(tex_map & (1u << stage)))
7847 continue;
7849 if (settings->op[stage].projected == proj_none)
7851 proj = FALSE;
7853 else if (settings->op[stage].projected == proj_count4
7854 || settings->op[stage].projected == proj_count3)
7856 proj = TRUE;
7858 else
7860 FIXME("Unexpected projection mode %d\n", settings->op[stage].projected);
7861 proj = TRUE;
7864 if (settings->op[stage].tex_type == WINED3D_GL_RES_TYPE_TEX_CUBE)
7865 proj = FALSE;
7867 switch (settings->op[stage].tex_type)
7869 case WINED3D_GL_RES_TYPE_TEX_1D:
7870 if (proj)
7872 texture_function = "texture1DProj";
7873 coord_mask = "xw";
7875 else
7877 texture_function = "texture1D";
7878 coord_mask = "x";
7880 break;
7881 case WINED3D_GL_RES_TYPE_TEX_2D:
7882 if (proj)
7884 texture_function = "texture2DProj";
7885 coord_mask = "xyw";
7887 else
7889 texture_function = "texture2D";
7890 coord_mask = "xy";
7892 break;
7893 case WINED3D_GL_RES_TYPE_TEX_3D:
7894 if (proj)
7896 texture_function = "texture3DProj";
7897 coord_mask = "xyzw";
7899 else
7901 texture_function = "texture3D";
7902 coord_mask = "xyz";
7904 break;
7905 case WINED3D_GL_RES_TYPE_TEX_CUBE:
7906 texture_function = "textureCube";
7907 coord_mask = "xyz";
7908 break;
7909 case WINED3D_GL_RES_TYPE_TEX_RECT:
7910 if (proj)
7912 texture_function = "texture2DRectProj";
7913 coord_mask = "xyw";
7915 else
7917 texture_function = "texture2DRect";
7918 coord_mask = "xy";
7920 break;
7921 default:
7922 FIXME("Unhandled texture type %#x.\n", settings->op[stage].tex_type);
7923 texture_function = "";
7924 coord_mask = "xyzw";
7925 break;
7927 if (!needs_legacy_glsl_syntax(gl_info))
7928 texture_function = proj ? "textureProj" : "texture";
7930 if (stage > 0
7931 && (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP
7932 || settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE))
7934 shader_addline(buffer, "ret.xy = bumpenv_mat%u * tex%u.xy;\n", stage - 1, stage - 1);
7936 /* With projective textures, texbem only divides the static
7937 * texture coord, not the displacement, so multiply the
7938 * displacement with the dividing parameter before passing it to
7939 * TXP. */
7940 if (settings->op[stage].projected != proj_none)
7942 if (settings->op[stage].projected == proj_count4)
7944 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].w) + ffp_texcoord[%u].xy;\n",
7945 stage, stage);
7946 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].ww;\n", stage);
7948 else
7950 shader_addline(buffer, "ret.xy = (ret.xy * ffp_texcoord[%u].z) + ffp_texcoord[%u].xy;\n",
7951 stage, stage);
7952 shader_addline(buffer, "ret.zw = ffp_texcoord[%u].zz;\n", stage);
7955 else
7957 shader_addline(buffer, "ret = ffp_texcoord[%u] + ret.xyxy;\n", stage);
7960 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ret.%s);\n",
7961 stage, texture_function, stage, coord_mask);
7963 if (settings->op[stage - 1].cop == WINED3D_TOP_BUMPENVMAP_LUMINANCE)
7964 shader_addline(buffer, "tex%u *= clamp(tex%u.z * bumpenv_lum_scale%u + bumpenv_lum_offset%u, 0.0, 1.0);\n",
7965 stage, stage - 1, stage - 1, stage - 1);
7967 else if (settings->op[stage].projected == proj_count3)
7969 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].xyz);\n",
7970 stage, texture_function, stage, stage);
7972 else
7974 shader_addline(buffer, "tex%u = %s(ps_sampler%u, ffp_texcoord[%u].%s);\n",
7975 stage, texture_function, stage, stage, coord_mask);
7978 string_buffer_sprintf(tex_reg_name, "tex%u", stage);
7979 shader_glsl_color_correction_ext(buffer, tex_reg_name->buffer, WINED3DSP_WRITEMASK_ALL,
7980 settings->op[stage].color_fixup);
7983 if (settings->color_key_enabled)
7985 shader_addline(buffer, "if (all(greaterThanEqual(tex0, color_key[0])) && all(lessThan(tex0, color_key[1])))\n");
7986 shader_addline(buffer, " discard;\n");
7989 shader_addline(buffer, "ret = ffp_varying_diffuse;\n");
7991 /* Generate the main shader */
7992 for (stage = 0; stage < MAX_TEXTURES; ++stage)
7994 BOOL op_equal;
7996 if (settings->op[stage].cop == WINED3D_TOP_DISABLE)
7997 break;
7999 if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8000 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8001 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg1;
8002 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG1
8003 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8004 op_equal = settings->op[stage].carg1 == settings->op[stage].aarg2;
8005 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8006 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG1)
8007 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg1;
8008 else if (settings->op[stage].cop == WINED3D_TOP_SELECT_ARG2
8009 && settings->op[stage].aop == WINED3D_TOP_SELECT_ARG2)
8010 op_equal = settings->op[stage].carg2 == settings->op[stage].aarg2;
8011 else
8012 op_equal = settings->op[stage].aop == settings->op[stage].cop
8013 && settings->op[stage].carg0 == settings->op[stage].aarg0
8014 && settings->op[stage].carg1 == settings->op[stage].aarg1
8015 && settings->op[stage].carg2 == settings->op[stage].aarg2;
8017 if (settings->op[stage].aop == WINED3D_TOP_DISABLE)
8019 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8020 settings->op[stage].cop, settings->op[stage].carg0,
8021 settings->op[stage].carg1, settings->op[stage].carg2);
8023 else if (op_equal)
8025 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, TRUE, settings->op[stage].dst,
8026 settings->op[stage].cop, settings->op[stage].carg0,
8027 settings->op[stage].carg1, settings->op[stage].carg2);
8029 else if (settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP
8030 && settings->op[stage].cop != WINED3D_TOP_BUMPENVMAP_LUMINANCE)
8032 shader_glsl_ffp_fragment_op(buffer, stage, TRUE, FALSE, settings->op[stage].dst,
8033 settings->op[stage].cop, settings->op[stage].carg0,
8034 settings->op[stage].carg1, settings->op[stage].carg2);
8035 shader_glsl_ffp_fragment_op(buffer, stage, FALSE, TRUE, settings->op[stage].dst,
8036 settings->op[stage].aop, settings->op[stage].aarg0,
8037 settings->op[stage].aarg1, settings->op[stage].aarg2);
8041 shader_addline(buffer, "%s[0] = ffp_varying_specular * specular_enable + ret;\n",
8042 get_fragment_output(gl_info));
8044 if (settings->sRGB_write)
8045 shader_glsl_generate_srgb_write_correction(buffer, gl_info);
8047 shader_glsl_generate_fog_code(buffer, gl_info, settings->fog);
8049 shader_glsl_generate_alpha_test(buffer, gl_info, alpha_test_func);
8051 shader_addline(buffer, "}\n");
8053 shader_id = GL_EXTCALL(glCreateShader(GL_FRAGMENT_SHADER));
8054 shader_glsl_compile(gl_info, shader_id, buffer->buffer);
8056 string_buffer_release(&priv->string_buffers, tex_reg_name);
8057 return shader_id;
8060 static struct glsl_ffp_vertex_shader *shader_glsl_find_ffp_vertex_shader(struct shader_glsl_priv *priv,
8061 const struct wined3d_gl_info *gl_info, const struct wined3d_ffp_vs_settings *settings)
8063 struct glsl_ffp_vertex_shader *shader;
8064 const struct wine_rb_entry *entry;
8066 if ((entry = wine_rb_get(&priv->ffp_vertex_shaders, settings)))
8067 return WINE_RB_ENTRY_VALUE(entry, struct glsl_ffp_vertex_shader, desc.entry);
8069 if (!(shader = HeapAlloc(GetProcessHeap(), 0, sizeof(*shader))))
8070 return NULL;
8072 shader->desc.settings = *settings;
8073 shader->id = shader_glsl_generate_ffp_vertex_shader(priv, settings, gl_info);
8074 list_init(&shader->linked_programs);
8075 if (wine_rb_put(&priv->ffp_vertex_shaders, &shader->desc.settings, &shader->desc.entry) == -1)
8076 ERR("Failed to insert ffp vertex shader.\n");
8078 return shader;
8081 static struct glsl_ffp_fragment_shader *shader_glsl_find_ffp_fragment_shader(struct shader_glsl_priv *priv,
8082 const struct wined3d_gl_info *gl_info, const struct ffp_frag_settings *args)
8084 struct glsl_ffp_fragment_shader *glsl_desc;
8085 const struct ffp_frag_desc *desc;
8087 if ((desc = find_ffp_frag_shader(&priv->ffp_fragment_shaders, args)))
8088 return CONTAINING_RECORD(desc, struct glsl_ffp_fragment_shader, entry);
8090 if (!(glsl_desc = HeapAlloc(GetProcessHeap(), 0, sizeof(*glsl_desc))))
8091 return NULL;
8093 glsl_desc->entry.settings = *args;
8094 glsl_desc->id = shader_glsl_generate_ffp_fragment_shader(priv, args, gl_info);
8095 list_init(&glsl_desc->linked_programs);
8096 add_ffp_frag_shader(&priv->ffp_fragment_shaders, &glsl_desc->entry);
8098 return glsl_desc;
8102 static void shader_glsl_init_vs_uniform_locations(const struct wined3d_gl_info *gl_info,
8103 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_vs_program *vs, unsigned int vs_c_count)
8105 unsigned int i;
8106 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8108 for (i = 0; i < vs_c_count; ++i)
8110 string_buffer_sprintf(name, "vs_c[%u]", i);
8111 vs->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8113 memset(&vs->uniform_f_locations[vs_c_count], 0xff, (WINED3D_MAX_VS_CONSTS_F - vs_c_count) * sizeof(GLuint));
8115 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8117 string_buffer_sprintf(name, "vs_i[%u]", i);
8118 vs->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8121 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8123 string_buffer_sprintf(name, "vs_b[%u]", i);
8124 vs->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8127 vs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8129 for (i = 0; i < MAX_VERTEX_BLENDS; ++i)
8131 string_buffer_sprintf(name, "ffp_modelview_matrix[%u]", i);
8132 vs->modelview_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8134 vs->projection_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_projection_matrix"));
8135 vs->normal_matrix_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_normal_matrix"));
8136 for (i = 0; i < MAX_TEXTURES; ++i)
8138 string_buffer_sprintf(name, "ffp_texture_matrix[%u]", i);
8139 vs->texture_matrix_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8141 vs->material_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.ambient"));
8142 vs->material_diffuse_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.diffuse"));
8143 vs->material_specular_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.specular"));
8144 vs->material_emissive_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.emissive"));
8145 vs->material_shininess_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_material.shininess"));
8146 vs->light_ambient_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_light_ambient"));
8147 for (i = 0; i < MAX_ACTIVE_LIGHTS; ++i)
8149 string_buffer_sprintf(name, "ffp_light[%u].diffuse", i);
8150 vs->light_location[i].diffuse = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8151 string_buffer_sprintf(name, "ffp_light[%u].specular", i);
8152 vs->light_location[i].specular = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8153 string_buffer_sprintf(name, "ffp_light[%u].ambient", i);
8154 vs->light_location[i].ambient = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8155 string_buffer_sprintf(name, "ffp_light[%u].position", i);
8156 vs->light_location[i].position = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8157 string_buffer_sprintf(name, "ffp_light[%u].direction", i);
8158 vs->light_location[i].direction = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8159 string_buffer_sprintf(name, "ffp_light[%u].range", i);
8160 vs->light_location[i].range = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8161 string_buffer_sprintf(name, "ffp_light[%u].falloff", i);
8162 vs->light_location[i].falloff = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8163 string_buffer_sprintf(name, "ffp_light[%u].c_att", i);
8164 vs->light_location[i].c_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8165 string_buffer_sprintf(name, "ffp_light[%u].l_att", i);
8166 vs->light_location[i].l_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8167 string_buffer_sprintf(name, "ffp_light[%u].q_att", i);
8168 vs->light_location[i].q_att = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8169 string_buffer_sprintf(name, "ffp_light[%u].cos_htheta", i);
8170 vs->light_location[i].cos_htheta = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8171 string_buffer_sprintf(name, "ffp_light[%u].cos_hphi", i);
8172 vs->light_location[i].cos_hphi = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8174 vs->pointsize_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size"));
8175 vs->pointsize_min_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_min"));
8176 vs->pointsize_max_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.size_max"));
8177 vs->pointsize_c_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.c_att"));
8178 vs->pointsize_l_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.l_att"));
8179 vs->pointsize_q_att_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_point.q_att"));
8180 vs->clip_planes_location = GL_EXTCALL(glGetUniformLocation(program_id, "clip_planes"));
8182 string_buffer_release(&priv->string_buffers, name);
8185 static void shader_glsl_init_gs_uniform_locations(const struct wined3d_gl_info *gl_info,
8186 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_gs_program *gs)
8188 gs->pos_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "pos_fixup"));
8191 static void shader_glsl_init_ps_uniform_locations(const struct wined3d_gl_info *gl_info,
8192 struct shader_glsl_priv *priv, GLuint program_id, struct glsl_ps_program *ps, unsigned int ps_c_count)
8194 unsigned int i;
8195 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8197 for (i = 0; i < ps_c_count; ++i)
8199 string_buffer_sprintf(name, "ps_c[%u]", i);
8200 ps->uniform_f_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8202 memset(&ps->uniform_f_locations[ps_c_count], 0xff, (WINED3D_MAX_PS_CONSTS_F - ps_c_count) * sizeof(GLuint));
8204 for (i = 0; i < WINED3D_MAX_CONSTS_I; ++i)
8206 string_buffer_sprintf(name, "ps_i[%u]", i);
8207 ps->uniform_i_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8210 for (i = 0; i < WINED3D_MAX_CONSTS_B; ++i)
8212 string_buffer_sprintf(name, "ps_b[%u]", i);
8213 ps->uniform_b_locations[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8216 for (i = 0; i < MAX_TEXTURES; ++i)
8218 string_buffer_sprintf(name, "bumpenv_mat%u", i);
8219 ps->bumpenv_mat_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8220 string_buffer_sprintf(name, "bumpenv_lum_scale%u", i);
8221 ps->bumpenv_lum_scale_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8222 string_buffer_sprintf(name, "bumpenv_lum_offset%u", i);
8223 ps->bumpenv_lum_offset_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8224 string_buffer_sprintf(name, "tss_const%u", i);
8225 ps->tss_constant_location[i] = GL_EXTCALL(glGetUniformLocation(program_id, name->buffer));
8228 ps->tex_factor_location = GL_EXTCALL(glGetUniformLocation(program_id, "tex_factor"));
8229 ps->specular_enable_location = GL_EXTCALL(glGetUniformLocation(program_id, "specular_enable"));
8231 ps->fog_color_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.color"));
8232 ps->fog_density_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.density"));
8233 ps->fog_end_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.end"));
8234 ps->fog_scale_location = GL_EXTCALL(glGetUniformLocation(program_id, "ffp_fog.scale"));
8236 ps->alpha_test_ref_location = GL_EXTCALL(glGetUniformLocation(program_id, "alpha_test_ref"));
8238 ps->np2_fixup_location = GL_EXTCALL(glGetUniformLocation(program_id, "ps_samplerNP2Fixup"));
8239 ps->ycorrection_location = GL_EXTCALL(glGetUniformLocation(program_id, "ycorrection"));
8240 ps->color_key_location = GL_EXTCALL(glGetUniformLocation(program_id, "color_key"));
8242 string_buffer_release(&priv->string_buffers, name);
8245 static void shader_glsl_init_uniform_block_bindings(const struct wined3d_gl_info *gl_info,
8246 struct shader_glsl_priv *priv, GLuint program_id,
8247 const struct wined3d_shader_reg_maps *reg_maps)
8249 struct wined3d_string_buffer *name = string_buffer_get(&priv->string_buffers);
8250 const char *prefix = shader_glsl_get_prefix(reg_maps->shader_version.type);
8251 unsigned int i, base, count;
8252 GLuint block_idx;
8254 wined3d_gl_limits_get_uniform_block_range(&gl_info->limits, reg_maps->shader_version.type, &base, &count);
8255 for (i = 0; i < count; ++i)
8257 if (!reg_maps->cb_sizes[i])
8258 continue;
8260 string_buffer_sprintf(name, "block_%s_cb%u", prefix, i);
8261 block_idx = GL_EXTCALL(glGetUniformBlockIndex(program_id, name->buffer));
8262 GL_EXTCALL(glUniformBlockBinding(program_id, block_idx, base + i));
8264 checkGLcall("glUniformBlockBinding");
8265 string_buffer_release(&priv->string_buffers, name);
8268 /* Context activation is done by the caller. */
8269 static void set_glsl_compute_shader_program(const struct wined3d_context *context,
8270 const struct wined3d_state *state, struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8272 const struct wined3d_gl_info *gl_info = context->gl_info;
8273 struct glsl_shader_prog_link *entry = NULL;
8274 struct wined3d_shader *shader;
8275 struct glsl_program_key key;
8276 GLuint program_id, cs_id;
8278 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_COMPUTE)))
8279 return;
8281 if (!(shader = state->shader[WINED3D_SHADER_TYPE_COMPUTE]))
8283 WARN("Compute shader is NULL.\n");
8284 ctx_data->glsl_program = NULL;
8285 return;
8288 cs_id = find_glsl_compute_shader(context, &priv->shader_buffer, &priv->string_buffers, shader);
8289 memset(&key, 0, sizeof(key));
8290 key.cs_id = cs_id;
8291 if ((entry = get_glsl_program_entry(priv, &key)))
8293 ctx_data->glsl_program = entry;
8294 return;
8297 program_id = GL_EXTCALL(glCreateProgram());
8298 TRACE("Created new GLSL shader program %u.\n", program_id);
8300 if (!(entry = HeapAlloc(GetProcessHeap(), 0, sizeof(*entry))))
8302 ERR("Out of memory.\n");
8303 return;
8305 entry->id = program_id;
8306 entry->vs.id = 0;
8307 entry->gs.id = 0;
8308 entry->ps.id = 0;
8309 entry->cs.id = cs_id;
8310 entry->constant_version = 0;
8311 entry->ps.np2_fixup_info = NULL;
8312 add_glsl_program_entry(priv, entry);
8314 ctx_data->glsl_program = entry;
8316 TRACE("Attaching GLSL shader object %u to program %u.\n", cs_id, program_id);
8317 GL_EXTCALL(glAttachShader(program_id, cs_id));
8318 checkGLcall("glAttachShader");
8320 list_add_head(&shader->linked_programs, &entry->cs.shader_entry);
8322 TRACE("Linking GLSL shader program %u.\n", program_id);
8323 GL_EXTCALL(glLinkProgram(program_id));
8324 shader_glsl_validate_link(gl_info, program_id);
8326 GL_EXTCALL(glUseProgram(program_id));
8327 checkGLcall("glUseProgram");
8328 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &shader->reg_maps);
8329 shader_glsl_load_icb(gl_info, priv, program_id, &shader->reg_maps);
8330 shader_glsl_load_images(gl_info, priv, program_id, &shader->reg_maps);
8332 entry->constant_update_mask = 0;
8335 /* Context activation is done by the caller. */
8336 static void set_glsl_shader_program(const struct wined3d_context *context, const struct wined3d_state *state,
8337 struct shader_glsl_priv *priv, struct glsl_context_data *ctx_data)
8339 const struct wined3d_gl_info *gl_info = context->gl_info;
8340 const struct wined3d_d3d_info *d3d_info = context->d3d_info;
8341 const struct ps_np2fixup_info *np2fixup_info = NULL;
8342 struct glsl_shader_prog_link *entry = NULL;
8343 struct wined3d_shader *vshader = NULL;
8344 struct wined3d_shader *gshader = NULL;
8345 struct wined3d_shader *pshader = NULL;
8346 GLuint reorder_shader_id = 0;
8347 struct glsl_program_key key;
8348 GLuint program_id;
8349 unsigned int i;
8350 GLuint vs_id = 0;
8351 GLuint gs_id = 0;
8352 GLuint ps_id = 0;
8353 struct list *ps_list, *vs_list;
8354 WORD attribs_map;
8355 struct wined3d_string_buffer *tmp_name;
8357 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_VERTEX)) && ctx_data->glsl_program)
8359 vs_id = ctx_data->glsl_program->vs.id;
8360 vs_list = &ctx_data->glsl_program->vs.shader_entry;
8362 if (use_vs(state))
8364 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8365 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8367 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8368 && ctx_data->glsl_program->gs.id)
8370 gs_id = ctx_data->glsl_program->gs.id;
8372 else if (gshader)
8374 struct gs_compile_args args;
8376 find_gs_compile_args(state, gshader, &args);
8377 gs_id = find_glsl_geometry_shader(context, priv, gshader, &args);
8381 else if (use_vs(state))
8383 struct vs_compile_args vs_compile_args;
8385 vshader = state->shader[WINED3D_SHADER_TYPE_VERTEX];
8386 gshader = state->shader[WINED3D_SHADER_TYPE_GEOMETRY];
8388 find_vs_compile_args(state, vshader, context->stream_info.swizzle_map, &vs_compile_args, d3d_info);
8389 vs_id = find_glsl_vshader(context, priv, vshader, &vs_compile_args);
8390 vs_list = &vshader->linked_programs;
8392 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_GEOMETRY))
8393 && ctx_data->glsl_program->gs.id)
8395 gs_id = ctx_data->glsl_program->gs.id;
8397 else if (gshader)
8399 struct gs_compile_args gs_compile_args;
8401 find_gs_compile_args(state, gshader, &gs_compile_args);
8402 gs_id = find_glsl_geometry_shader(context, priv, gshader, &gs_compile_args);
8405 else if (priv->vertex_pipe == &glsl_vertex_pipe)
8407 struct glsl_ffp_vertex_shader *ffp_shader;
8408 struct wined3d_ffp_vs_settings settings;
8410 wined3d_ffp_get_vs_settings(context, state, &settings);
8411 ffp_shader = shader_glsl_find_ffp_vertex_shader(priv, gl_info, &settings);
8412 vs_id = ffp_shader->id;
8413 vs_list = &ffp_shader->linked_programs;
8416 if (!(context->shader_update_mask & (1u << WINED3D_SHADER_TYPE_PIXEL)) && ctx_data->glsl_program)
8418 ps_id = ctx_data->glsl_program->ps.id;
8419 ps_list = &ctx_data->glsl_program->ps.shader_entry;
8421 if (use_ps(state))
8422 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8424 else if (use_ps(state))
8426 struct ps_compile_args ps_compile_args;
8427 pshader = state->shader[WINED3D_SHADER_TYPE_PIXEL];
8428 find_ps_compile_args(state, pshader, context->stream_info.position_transformed, &ps_compile_args, context);
8429 ps_id = find_glsl_pshader(context, &priv->shader_buffer, &priv->string_buffers,
8430 pshader, &ps_compile_args, &np2fixup_info);
8431 ps_list = &pshader->linked_programs;
8433 else if (priv->fragment_pipe == &glsl_fragment_pipe)
8435 struct glsl_ffp_fragment_shader *ffp_shader;
8436 struct ffp_frag_settings settings;
8438 gen_ffp_frag_op(context, state, &settings, FALSE);
8439 ffp_shader = shader_glsl_find_ffp_fragment_shader(priv, gl_info, &settings);
8440 ps_id = ffp_shader->id;
8441 ps_list = &ffp_shader->linked_programs;
8444 key.vs_id = vs_id;
8445 key.gs_id = gs_id;
8446 key.ps_id = ps_id;
8447 key.cs_id = 0;
8448 if ((!vs_id && !gs_id && !ps_id) || (entry = get_glsl_program_entry(priv, &key)))
8450 ctx_data->glsl_program = entry;
8451 return;
8454 /* If we get to this point, then no matching program exists, so we create one */
8455 program_id = GL_EXTCALL(glCreateProgram());
8456 TRACE("Created new GLSL shader program %u.\n", program_id);
8458 /* Create the entry */
8459 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
8460 entry->id = program_id;
8461 entry->vs.id = vs_id;
8462 entry->gs.id = gs_id;
8463 entry->ps.id = ps_id;
8464 entry->cs.id = 0;
8465 entry->constant_version = 0;
8466 entry->ps.np2_fixup_info = np2fixup_info;
8467 /* Add the hash table entry */
8468 add_glsl_program_entry(priv, entry);
8470 /* Set the current program */
8471 ctx_data->glsl_program = entry;
8473 /* Attach GLSL vshader */
8474 if (vs_id)
8476 TRACE("Attaching GLSL shader object %u to program %u.\n", vs_id, program_id);
8477 GL_EXTCALL(glAttachShader(program_id, vs_id));
8478 checkGLcall("glAttachShader");
8480 list_add_head(vs_list, &entry->vs.shader_entry);
8483 if (vshader)
8485 attribs_map = vshader->reg_maps.input_registers;
8486 if (vshader->reg_maps.shader_version.major < 4)
8488 reorder_shader_id = shader_glsl_generate_vs3_rasterizer_input_setup(priv, vshader, pshader,
8489 state->gl_primitive_type == GL_POINTS && vshader->reg_maps.point_size,
8490 d3d_info->emulated_flatshading
8491 && state->render_states[WINED3D_RS_SHADEMODE] == WINED3D_SHADE_FLAT, gl_info);
8492 TRACE("Attaching GLSL shader object %u to program %u.\n", reorder_shader_id, program_id);
8493 GL_EXTCALL(glAttachShader(program_id, reorder_shader_id));
8494 checkGLcall("glAttachShader");
8495 /* Flag the reorder function for deletion, it will be freed
8496 * automatically when the program is destroyed. */
8497 GL_EXTCALL(glDeleteShader(reorder_shader_id));
8500 else
8502 attribs_map = (1u << WINED3D_FFP_ATTRIBS_COUNT) - 1;
8505 if (!shader_glsl_use_explicit_attrib_location(gl_info))
8507 /* Bind vertex attributes to a corresponding index number to match
8508 * the same index numbers as ARB_vertex_programs (makes loading
8509 * vertex attributes simpler). With this method, we can use the
8510 * exact same code to load the attributes later for both ARB and
8511 * GLSL shaders.
8513 * We have to do this here because we need to know the Program ID
8514 * in order to make the bindings work, and it has to be done prior
8515 * to linking the GLSL program. */
8516 tmp_name = string_buffer_get(&priv->string_buffers);
8517 for (i = 0; attribs_map; attribs_map >>= 1, ++i)
8519 if (!(attribs_map & 1))
8520 continue;
8522 string_buffer_sprintf(tmp_name, "vs_in%u", i);
8523 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8524 if (vshader && vshader->reg_maps.shader_version.major >= 4)
8526 string_buffer_sprintf(tmp_name, "vs_in_uint%u", i);
8527 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8528 string_buffer_sprintf(tmp_name, "vs_in_int%u", i);
8529 GL_EXTCALL(glBindAttribLocation(program_id, i, tmp_name->buffer));
8532 checkGLcall("glBindAttribLocation");
8533 string_buffer_release(&priv->string_buffers, tmp_name);
8536 if (gshader)
8538 TRACE("Attaching GLSL geometry shader object %u to program %u.\n", gs_id, program_id);
8539 GL_EXTCALL(glAttachShader(program_id, gs_id));
8540 checkGLcall("glAttachShader");
8542 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8544 TRACE("input type %s, output type %s, vertices out %u.\n",
8545 debug_d3dprimitivetype(gshader->u.gs.input_type),
8546 debug_d3dprimitivetype(gshader->u.gs.output_type),
8547 gshader->u.gs.vertices_out);
8548 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_INPUT_TYPE_ARB,
8549 gl_primitive_type_from_d3d(gshader->u.gs.input_type)));
8550 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_OUTPUT_TYPE_ARB,
8551 gl_primitive_type_from_d3d(gshader->u.gs.output_type)));
8552 GL_EXTCALL(glProgramParameteriARB(program_id, GL_GEOMETRY_VERTICES_OUT_ARB,
8553 gshader->u.gs.vertices_out));
8554 checkGLcall("glProgramParameteriARB");
8557 list_add_head(&gshader->linked_programs, &entry->gs.shader_entry);
8560 /* Attach GLSL pshader */
8561 if (ps_id)
8563 TRACE("Attaching GLSL shader object %u to program %u.\n", ps_id, program_id);
8564 GL_EXTCALL(glAttachShader(program_id, ps_id));
8565 checkGLcall("glAttachShader");
8567 list_add_head(ps_list, &entry->ps.shader_entry);
8570 /* Link the program */
8571 TRACE("Linking GLSL shader program %u.\n", program_id);
8572 GL_EXTCALL(glLinkProgram(program_id));
8573 shader_glsl_validate_link(gl_info, program_id);
8575 shader_glsl_init_vs_uniform_locations(gl_info, priv, program_id, &entry->vs,
8576 vshader ? vshader->limits->constant_float : 0);
8577 shader_glsl_init_gs_uniform_locations(gl_info, priv, program_id, &entry->gs);
8578 shader_glsl_init_ps_uniform_locations(gl_info, priv, program_id, &entry->ps,
8579 pshader ? pshader->limits->constant_float : 0);
8580 checkGLcall("Find glsl program uniform locations");
8582 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
8584 if (pshader && pshader->reg_maps.shader_version.major >= 3
8585 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
8587 TRACE("Shader %d needs vertex color clamping disabled.\n", program_id);
8588 entry->vs.vertex_color_clamp = GL_FALSE;
8590 else
8592 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8595 else
8597 /* With core profile we never change vertex_color_clamp from
8598 * GL_FIXED_ONLY_MODE (which is also the initial value) so we never call
8599 * glClampColorARB(). */
8600 entry->vs.vertex_color_clamp = GL_FIXED_ONLY_ARB;
8603 /* Set the shader to allow uniform loading on it */
8604 GL_EXTCALL(glUseProgram(program_id));
8605 checkGLcall("glUseProgram");
8607 /* Texture unit mapping is set up to be the same each time the shader
8608 * program is used so we can hardcode the sampler uniform values. */
8609 shader_glsl_load_samplers(gl_info, priv, context->tex_unit_map, program_id);
8611 entry->constant_update_mask = 0;
8612 if (vshader)
8614 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_F;
8615 if (vshader->reg_maps.integer_constants)
8616 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_I;
8617 if (vshader->reg_maps.boolean_constants)
8618 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_B;
8619 if (entry->vs.pos_fixup_location != -1)
8620 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8622 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &vshader->reg_maps);
8623 shader_glsl_load_icb(gl_info, priv, program_id, &vshader->reg_maps);
8625 else
8627 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
8628 | WINED3D_SHADER_CONST_FFP_PROJ;
8630 for (i = 1; i < MAX_VERTEX_BLENDS; ++i)
8632 if (entry->vs.modelview_matrix_location[i] != -1)
8634 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
8635 break;
8639 for (i = 0; i < MAX_TEXTURES; ++i)
8641 if (entry->vs.texture_matrix_location[i] != -1)
8643 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
8644 break;
8647 if (entry->vs.material_ambient_location != -1 || entry->vs.material_diffuse_location != -1
8648 || entry->vs.material_specular_location != -1
8649 || entry->vs.material_emissive_location != -1
8650 || entry->vs.material_shininess_location != -1)
8651 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
8652 if (entry->vs.light_ambient_location != -1)
8653 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
8655 if (entry->vs.clip_planes_location != -1)
8656 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
8657 if (entry->vs.pointsize_min_location != -1)
8658 entry->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
8660 if (gshader)
8662 if (entry->gs.pos_fixup_location != -1)
8663 entry->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
8664 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &gshader->reg_maps);
8665 shader_glsl_load_icb(gl_info, priv, program_id, &gshader->reg_maps);
8668 if (ps_id)
8670 if (pshader)
8672 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_F;
8673 if (pshader->reg_maps.integer_constants)
8674 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_I;
8675 if (pshader->reg_maps.boolean_constants)
8676 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_B;
8677 if (entry->ps.ycorrection_location != -1)
8678 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_Y_CORR;
8680 shader_glsl_init_uniform_block_bindings(gl_info, priv, program_id, &pshader->reg_maps);
8681 shader_glsl_load_icb(gl_info, priv, program_id, &pshader->reg_maps);
8682 shader_glsl_load_images(gl_info, priv, program_id, &pshader->reg_maps);
8684 else
8686 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
8689 for (i = 0; i < MAX_TEXTURES; ++i)
8691 if (entry->ps.bumpenv_mat_location[i] != -1)
8693 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_BUMP_ENV;
8694 break;
8698 if (entry->ps.fog_color_location != -1)
8699 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
8700 if (entry->ps.alpha_test_ref_location != -1)
8701 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
8702 if (entry->ps.np2_fixup_location != -1)
8703 entry->constant_update_mask |= WINED3D_SHADER_CONST_PS_NP2_FIXUP;
8704 if (entry->ps.color_key_location != -1)
8705 entry->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
8709 /* Context activation is done by the caller. */
8710 static void shader_glsl_select(void *shader_priv, struct wined3d_context *context,
8711 const struct wined3d_state *state)
8713 struct glsl_context_data *ctx_data = context->shader_backend_data;
8714 const struct wined3d_gl_info *gl_info = context->gl_info;
8715 struct shader_glsl_priv *priv = shader_priv;
8716 GLenum current_vertex_color_clamp;
8717 GLuint program_id, prev_id;
8719 priv->vertex_pipe->vp_enable(gl_info, !use_vs(state));
8720 priv->fragment_pipe->enable_extension(gl_info, !use_ps(state));
8722 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8724 set_glsl_shader_program(context, state, priv, ctx_data);
8726 if (ctx_data->glsl_program)
8728 program_id = ctx_data->glsl_program->id;
8729 current_vertex_color_clamp = ctx_data->glsl_program->vs.vertex_color_clamp;
8731 else
8733 program_id = 0;
8734 current_vertex_color_clamp = GL_FIXED_ONLY_ARB;
8737 if (ctx_data->vertex_color_clamp != current_vertex_color_clamp)
8739 ctx_data->vertex_color_clamp = current_vertex_color_clamp;
8740 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8742 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
8743 checkGLcall("glClampColorARB");
8745 else
8747 FIXME("Vertex color clamp needs to be changed, but extension not supported.\n");
8751 TRACE("Using GLSL program %u.\n", program_id);
8753 if (prev_id != program_id)
8755 GL_EXTCALL(glUseProgram(program_id));
8756 checkGLcall("glUseProgram");
8758 if (program_id)
8759 context->constant_update_mask |= ctx_data->glsl_program->constant_update_mask;
8762 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_COMPUTE);
8765 /* Context activation is done by the caller. */
8766 static void shader_glsl_select_compute(void *shader_priv, struct wined3d_context *context,
8767 const struct wined3d_state *state)
8769 struct glsl_context_data *ctx_data = context->shader_backend_data;
8770 const struct wined3d_gl_info *gl_info = context->gl_info;
8771 struct shader_glsl_priv *priv = shader_priv;
8772 GLuint program_id, prev_id;
8774 prev_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8775 set_glsl_compute_shader_program(context, state, priv, ctx_data);
8776 program_id = ctx_data->glsl_program ? ctx_data->glsl_program->id : 0;
8778 TRACE("Using GLSL program %u.\n", program_id);
8780 if (prev_id != program_id)
8782 GL_EXTCALL(glUseProgram(program_id));
8783 checkGLcall("glUseProgram");
8786 context->shader_update_mask |= (1u << WINED3D_SHADER_TYPE_PIXEL)
8787 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8788 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8789 | (1u << WINED3D_SHADER_TYPE_HULL)
8790 | (1u << WINED3D_SHADER_TYPE_DOMAIN);
8793 /* "context" is not necessarily the currently active context. */
8794 static void shader_glsl_invalidate_current_program(struct wined3d_context *context)
8796 struct glsl_context_data *ctx_data = context->shader_backend_data;
8798 ctx_data->glsl_program = NULL;
8799 context->shader_update_mask = (1u << WINED3D_SHADER_TYPE_PIXEL)
8800 | (1u << WINED3D_SHADER_TYPE_VERTEX)
8801 | (1u << WINED3D_SHADER_TYPE_GEOMETRY)
8802 | (1u << WINED3D_SHADER_TYPE_HULL)
8803 | (1u << WINED3D_SHADER_TYPE_DOMAIN)
8804 | (1u << WINED3D_SHADER_TYPE_COMPUTE);
8807 /* Context activation is done by the caller. */
8808 static void shader_glsl_disable(void *shader_priv, struct wined3d_context *context)
8810 struct glsl_context_data *ctx_data = context->shader_backend_data;
8811 const struct wined3d_gl_info *gl_info = context->gl_info;
8812 struct shader_glsl_priv *priv = shader_priv;
8814 shader_glsl_invalidate_current_program(context);
8815 GL_EXTCALL(glUseProgram(0));
8816 checkGLcall("glUseProgram");
8818 priv->vertex_pipe->vp_enable(gl_info, FALSE);
8819 priv->fragment_pipe->enable_extension(gl_info, FALSE);
8821 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT] && gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
8823 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
8824 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, GL_FIXED_ONLY_ARB));
8825 checkGLcall("glClampColorARB");
8829 static void shader_glsl_invalidate_contexts_program(struct wined3d_device *device,
8830 const struct glsl_shader_prog_link *program)
8832 const struct glsl_context_data *ctx_data;
8833 struct wined3d_context *context;
8834 unsigned int i;
8836 for (i = 0; i < device->context_count; ++i)
8838 context = device->contexts[i];
8839 ctx_data = context->shader_backend_data;
8841 if (ctx_data->glsl_program == program)
8842 shader_glsl_invalidate_current_program(context);
8846 static void shader_glsl_destroy(struct wined3d_shader *shader)
8848 struct glsl_shader_private *shader_data = shader->backend_data;
8849 struct wined3d_device *device = shader->device;
8850 struct shader_glsl_priv *priv = device->shader_priv;
8851 const struct wined3d_gl_info *gl_info;
8852 const struct list *linked_programs;
8853 struct wined3d_context *context;
8855 if (!shader_data || !shader_data->num_gl_shaders)
8857 HeapFree(GetProcessHeap(), 0, shader_data);
8858 shader->backend_data = NULL;
8859 return;
8862 context = context_acquire(device, NULL, 0);
8863 gl_info = context->gl_info;
8865 TRACE("Deleting linked programs.\n");
8866 linked_programs = &shader->linked_programs;
8867 if (linked_programs->next)
8869 struct glsl_shader_prog_link *entry, *entry2;
8870 UINT i;
8872 switch (shader->reg_maps.shader_version.type)
8874 case WINED3D_SHADER_TYPE_PIXEL:
8876 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
8878 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8880 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].id);
8881 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8882 checkGLcall("glDeleteShader");
8884 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
8886 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8887 struct glsl_shader_prog_link, ps.shader_entry)
8889 shader_glsl_invalidate_contexts_program(device, entry);
8890 delete_glsl_program_entry(priv, gl_info, entry);
8893 break;
8896 case WINED3D_SHADER_TYPE_VERTEX:
8898 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
8900 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8902 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].id);
8903 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8904 checkGLcall("glDeleteShader");
8906 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
8908 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8909 struct glsl_shader_prog_link, vs.shader_entry)
8911 shader_glsl_invalidate_contexts_program(device, entry);
8912 delete_glsl_program_entry(priv, gl_info, entry);
8915 break;
8918 case WINED3D_SHADER_TYPE_GEOMETRY:
8920 struct glsl_gs_compiled_shader *gl_shaders = shader_data->gl_shaders.gs;
8922 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8924 TRACE("Deleting geometry shader %u.\n", gl_shaders[i].id);
8925 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8926 checkGLcall("glDeleteShader");
8928 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.gs);
8930 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8931 struct glsl_shader_prog_link, gs.shader_entry)
8933 shader_glsl_invalidate_contexts_program(device, entry);
8934 delete_glsl_program_entry(priv, gl_info, entry);
8937 break;
8940 case WINED3D_SHADER_TYPE_COMPUTE:
8942 struct glsl_cs_compiled_shader *gl_shaders = shader_data->gl_shaders.cs;
8944 for (i = 0; i < shader_data->num_gl_shaders; ++i)
8946 TRACE("Deleting compute shader %u.\n", gl_shaders[i].id);
8947 GL_EXTCALL(glDeleteShader(gl_shaders[i].id));
8948 checkGLcall("glDeleteShader");
8950 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.cs);
8952 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
8953 struct glsl_shader_prog_link, cs.shader_entry)
8955 shader_glsl_invalidate_contexts_program(device, entry);
8956 delete_glsl_program_entry(priv, gl_info, entry);
8959 break;
8962 default:
8963 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
8964 break;
8968 HeapFree(GetProcessHeap(), 0, shader->backend_data);
8969 shader->backend_data = NULL;
8971 context_release(context);
8974 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
8976 const struct glsl_program_key *k = key;
8977 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
8978 const struct glsl_shader_prog_link, program_lookup_entry);
8980 if (k->vs_id > prog->vs.id) return 1;
8981 else if (k->vs_id < prog->vs.id) return -1;
8983 if (k->gs_id > prog->gs.id) return 1;
8984 else if (k->gs_id < prog->gs.id) return -1;
8986 if (k->ps_id > prog->ps.id) return 1;
8987 else if (k->ps_id < prog->ps.id) return -1;
8989 if (k->cs_id > prog->cs.id) return 1;
8990 else if (k->cs_id < prog->cs.id) return -1;
8992 return 0;
8995 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
8997 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries)
8998 + constant_count * sizeof(*heap->contained)
8999 + constant_count * sizeof(*heap->positions);
9000 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
9002 if (!mem)
9004 ERR("Failed to allocate memory\n");
9005 return FALSE;
9008 heap->entries = mem;
9009 heap->entries[1].version = 0;
9010 heap->contained = (BOOL *)(heap->entries + constant_count + 1);
9011 memset(heap->contained, 0, constant_count * sizeof(*heap->contained));
9012 heap->positions = (unsigned int *)(heap->contained + constant_count);
9013 heap->size = 1;
9015 return TRUE;
9018 static void constant_heap_free(struct constant_heap *heap)
9020 HeapFree(GetProcessHeap(), 0, heap->entries);
9023 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct wined3d_vertex_pipe_ops *vertex_pipe,
9024 const struct fragment_pipeline *fragment_pipe)
9026 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
9027 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
9028 SIZE_T stack_size = wined3d_log2i(max(WINED3D_MAX_VS_CONSTS_F, WINED3D_MAX_PS_CONSTS_F)) + 1;
9029 struct fragment_caps fragment_caps;
9030 void *vertex_priv, *fragment_priv;
9032 string_buffer_list_init(&priv->string_buffers);
9034 if (!(vertex_priv = vertex_pipe->vp_alloc(&glsl_shader_backend, priv)))
9036 ERR("Failed to initialize vertex pipe.\n");
9037 HeapFree(GetProcessHeap(), 0, priv);
9038 return E_FAIL;
9041 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
9043 ERR("Failed to initialize fragment pipe.\n");
9044 vertex_pipe->vp_free(device);
9045 HeapFree(GetProcessHeap(), 0, priv);
9046 return E_FAIL;
9049 if (!string_buffer_init(&priv->shader_buffer))
9051 ERR("Failed to initialize shader buffer.\n");
9052 goto fail;
9055 if (!(priv->stack = wined3d_calloc(stack_size, sizeof(*priv->stack))))
9057 ERR("Failed to allocate memory.\n");
9058 goto fail;
9061 if (!constant_heap_init(&priv->vconst_heap, WINED3D_MAX_VS_CONSTS_F))
9063 ERR("Failed to initialize vertex shader constant heap\n");
9064 goto fail;
9067 if (!constant_heap_init(&priv->pconst_heap, WINED3D_MAX_PS_CONSTS_F))
9069 ERR("Failed to initialize pixel shader constant heap\n");
9070 goto fail;
9073 wine_rb_init(&priv->program_lookup, glsl_program_key_compare);
9075 priv->next_constant_version = 1;
9076 priv->vertex_pipe = vertex_pipe;
9077 priv->fragment_pipe = fragment_pipe;
9078 fragment_pipe->get_caps(gl_info, &fragment_caps);
9079 priv->ffp_proj_control = fragment_caps.wined3d_caps & WINED3D_FRAGMENT_CAP_PROJ_CONTROL;
9080 priv->legacy_lighting = device->wined3d->flags & WINED3D_LEGACY_FFP_LIGHTING;
9082 device->vertex_priv = vertex_priv;
9083 device->fragment_priv = fragment_priv;
9084 device->shader_priv = priv;
9086 return WINED3D_OK;
9088 fail:
9089 constant_heap_free(&priv->pconst_heap);
9090 constant_heap_free(&priv->vconst_heap);
9091 HeapFree(GetProcessHeap(), 0, priv->stack);
9092 string_buffer_free(&priv->shader_buffer);
9093 fragment_pipe->free_private(device);
9094 vertex_pipe->vp_free(device);
9095 HeapFree(GetProcessHeap(), 0, priv);
9096 return E_OUTOFMEMORY;
9099 /* Context activation is done by the caller. */
9100 static void shader_glsl_free(struct wined3d_device *device)
9102 struct shader_glsl_priv *priv = device->shader_priv;
9104 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
9105 constant_heap_free(&priv->pconst_heap);
9106 constant_heap_free(&priv->vconst_heap);
9107 HeapFree(GetProcessHeap(), 0, priv->stack);
9108 string_buffer_list_cleanup(&priv->string_buffers);
9109 string_buffer_free(&priv->shader_buffer);
9110 priv->fragment_pipe->free_private(device);
9111 priv->vertex_pipe->vp_free(device);
9113 HeapFree(GetProcessHeap(), 0, device->shader_priv);
9114 device->shader_priv = NULL;
9117 static BOOL shader_glsl_allocate_context_data(struct wined3d_context *context)
9119 struct glsl_context_data *ctx_data;
9120 if (!(ctx_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ctx_data))))
9121 return FALSE;
9122 ctx_data->vertex_color_clamp = GL_FIXED_ONLY_ARB;
9123 context->shader_backend_data = ctx_data;
9124 return TRUE;
9127 static void shader_glsl_free_context_data(struct wined3d_context *context)
9129 HeapFree(GetProcessHeap(), 0, context->shader_backend_data);
9132 static void shader_glsl_init_context_state(struct wined3d_context *context)
9134 const struct wined3d_gl_info *gl_info = context->gl_info;
9136 gl_info->gl_ops.gl.p_glEnable(GL_PROGRAM_POINT_SIZE);
9137 checkGLcall("GL_PROGRAM_POINT_SIZE");
9140 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
9142 UINT shader_model;
9144 /* FIXME: Check for the specific extensions required for SM5 support
9145 * (ARB_compute_shader, ARB_tessellation_shader, ARB_gpu_shader5, ...) as
9146 * soon as we introduce them, adjusting the GL / GLSL version checks
9147 * accordingly. */
9148 if (gl_info->glsl_version >= MAKEDWORD_VERSION(4, 30) && gl_info->supported[WINED3D_GL_VERSION_4_3]
9149 && gl_info->supported[ARB_COMPUTE_SHADER]
9150 && gl_info->supported[ARB_DERIVATIVE_CONTROL]
9151 && gl_info->supported[ARB_GPU_SHADER5]
9152 && gl_info->supported[ARB_SHADER_IMAGE_LOAD_STORE]
9153 && gl_info->supported[ARB_SHADER_IMAGE_SIZE]
9154 && gl_info->supported[ARB_SHADING_LANGUAGE_PACKING])
9155 shader_model = 5;
9156 else if (gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50) && gl_info->supported[WINED3D_GL_VERSION_3_2]
9157 && gl_info->supported[ARB_SHADER_BIT_ENCODING] && gl_info->supported[ARB_SAMPLER_OBJECTS]
9158 && gl_info->supported[ARB_TEXTURE_SWIZZLE])
9159 shader_model = 4;
9160 /* Support for texldd and texldl instructions in pixel shaders is required
9161 * for SM3. */
9162 else if (shader_glsl_has_core_grad(gl_info, NULL) || gl_info->supported[ARB_SHADER_TEXTURE_LOD])
9163 shader_model = 3;
9164 else
9165 shader_model = 2;
9166 TRACE("Shader model %u.\n", shader_model);
9168 caps->vs_version = min(wined3d_settings.max_sm_vs, shader_model);
9169 caps->hs_version = min(wined3d_settings.max_sm_hs, shader_model);
9170 caps->ds_version = min(wined3d_settings.max_sm_ds, shader_model);
9171 caps->gs_version = min(wined3d_settings.max_sm_gs, shader_model);
9172 caps->ps_version = min(wined3d_settings.max_sm_ps, shader_model);
9173 caps->cs_version = min(wined3d_settings.max_sm_cs, shader_model);
9175 caps->vs_uniform_count = min(WINED3D_MAX_VS_CONSTS_F, gl_info->limits.glsl_vs_float_constants);
9176 caps->ps_uniform_count = min(WINED3D_MAX_PS_CONSTS_F, gl_info->limits.glsl_ps_float_constants);
9177 caps->varying_count = gl_info->limits.glsl_varyings;
9179 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
9180 * Direct3D minimum requirement.
9182 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
9183 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
9185 * The problem is that the refrast clamps temporary results in the shader to
9186 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
9187 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
9188 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
9189 * offer a way to query this.
9191 if (shader_model >= 4)
9192 caps->ps_1x_max_value = FLT_MAX;
9193 else
9194 caps->ps_1x_max_value = 1024.0f;
9196 /* Ideally we'd only set caps like sRGB writes here if supported by both
9197 * the shader backend and the fragment pipe, but we can get called before
9198 * shader_glsl_alloc(). */
9199 caps->wined3d_caps = WINED3D_SHADER_CAP_VS_CLIPPING
9200 | WINED3D_SHADER_CAP_SRGB_WRITE;
9203 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
9205 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
9207 TRACE("Checking support for fixup:\n");
9208 dump_color_fixup_desc(fixup);
9211 /* We support everything except YUV conversions. */
9212 if (!is_complex_fixup(fixup))
9214 TRACE("[OK]\n");
9215 return TRUE;
9218 TRACE("[FAILED]\n");
9219 return FALSE;
9222 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
9224 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
9225 /* WINED3DSIH_ADD */ shader_glsl_binop,
9226 /* WINED3DSIH_AND */ shader_glsl_binop,
9227 /* WINED3DSIH_ATOMIC_AND */ shader_glsl_atomic,
9228 /* WINED3DSIH_ATOMIC_CMP_STORE */ shader_glsl_atomic,
9229 /* WINED3DSIH_ATOMIC_IADD */ shader_glsl_atomic,
9230 /* WINED3DSIH_ATOMIC_IMAX */ NULL,
9231 /* WINED3DSIH_ATOMIC_IMIN */ NULL,
9232 /* WINED3DSIH_ATOMIC_OR */ shader_glsl_atomic,
9233 /* WINED3DSIH_ATOMIC_UMAX */ NULL,
9234 /* WINED3DSIH_ATOMIC_UMIN */ NULL,
9235 /* WINED3DSIH_ATOMIC_XOR */ shader_glsl_atomic,
9236 /* WINED3DSIH_BEM */ shader_glsl_bem,
9237 /* WINED3DSIH_BFI */ shader_glsl_bitwise_op,
9238 /* WINED3DSIH_BFREV */ shader_glsl_map2gl,
9239 /* WINED3DSIH_BREAK */ shader_glsl_break,
9240 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
9241 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
9242 /* WINED3DSIH_BUFINFO */ NULL,
9243 /* WINED3DSIH_CALL */ shader_glsl_call,
9244 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
9245 /* WINED3DSIH_CASE */ shader_glsl_case,
9246 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
9247 /* WINED3DSIH_CND */ shader_glsl_cnd,
9248 /* WINED3DSIH_CONTINUE */ shader_glsl_continue,
9249 /* WINED3DSIH_COUNTBITS */ shader_glsl_map2gl,
9250 /* WINED3DSIH_CRS */ shader_glsl_cross,
9251 /* WINED3DSIH_CUT */ shader_glsl_cut,
9252 /* WINED3DSIH_CUT_STREAM */ shader_glsl_cut,
9253 /* WINED3DSIH_DCL */ shader_glsl_nop,
9254 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
9255 /* WINED3DSIH_DCL_FUNCTION_BODY */ NULL,
9256 /* WINED3DSIH_DCL_FUNCTION_TABLE */ NULL,
9257 /* WINED3DSIH_DCL_GLOBAL_FLAGS */ shader_glsl_nop,
9258 /* WINED3DSIH_DCL_HS_FORK_PHASE_INSTANCE_COUNT */ NULL,
9259 /* WINED3DSIH_DCL_HS_MAX_TESSFACTOR */ NULL,
9260 /* WINED3DSIH_DCL_IMMEDIATE_CONSTANT_BUFFER */ shader_glsl_nop,
9261 /* WINED3DSIH_DCL_INDEXABLE_TEMP */ shader_glsl_nop,
9262 /* WINED3DSIH_DCL_INPUT */ shader_glsl_nop,
9263 /* WINED3DSIH_DCL_INPUT_CONTROL_POINT_COUNT */ NULL,
9264 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
9265 /* WINED3DSIH_DCL_INPUT_PS */ NULL,
9266 /* WINED3DSIH_DCL_INPUT_PS_SGV */ NULL,
9267 /* WINED3DSIH_DCL_INPUT_PS_SIV */ NULL,
9268 /* WINED3DSIH_DCL_INPUT_SGV */ shader_glsl_nop,
9269 /* WINED3DSIH_DCL_INPUT_SIV */ shader_glsl_nop,
9270 /* WINED3DSIH_DCL_INTERFACE */ NULL,
9271 /* WINED3DSIH_DCL_OUTPUT */ shader_glsl_nop,
9272 /* WINED3DSIH_DCL_OUTPUT_CONTROL_POINT_COUNT */ NULL,
9273 /* WINED3DSIH_DCL_OUTPUT_SIV */ shader_glsl_nop,
9274 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
9275 /* WINED3DSIH_DCL_RESOURCE_RAW */ shader_glsl_nop,
9276 /* WINED3DSIH_DCL_RESOURCE_STRUCTURED */ NULL,
9277 /* WINED3DSIH_DCL_SAMPLER */ shader_glsl_nop,
9278 /* WINED3DSIH_DCL_STREAM */ NULL,
9279 /* WINED3DSIH_DCL_TEMPS */ shader_glsl_nop,
9280 /* WINED3DSIH_DCL_TESSELLATOR_DOMAIN */ NULL,
9281 /* WINED3DSIH_DCL_TESSELLATOR_OUTPUT_PRIMITIVE */ NULL,
9282 /* WINED3DSIH_DCL_TESSELLATOR_PARTITIONING */ NULL,
9283 /* WINED3DSIH_DCL_TGSM_RAW */ NULL,
9284 /* WINED3DSIH_DCL_TGSM_STRUCTURED */ NULL,
9285 /* WINED3DSIH_DCL_THREAD_GROUP */ shader_glsl_nop,
9286 /* WINED3DSIH_DCL_UAV_RAW */ shader_glsl_nop,
9287 /* WINED3DSIH_DCL_UAV_STRUCTURED */ NULL,
9288 /* WINED3DSIH_DCL_UAV_TYPED */ shader_glsl_nop,
9289 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
9290 /* WINED3DSIH_DEF */ shader_glsl_nop,
9291 /* WINED3DSIH_DEFAULT */ shader_glsl_default,
9292 /* WINED3DSIH_DEFB */ shader_glsl_nop,
9293 /* WINED3DSIH_DEFI */ shader_glsl_nop,
9294 /* WINED3DSIH_DIV */ shader_glsl_binop,
9295 /* WINED3DSIH_DP2 */ shader_glsl_dot,
9296 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
9297 /* WINED3DSIH_DP3 */ shader_glsl_dot,
9298 /* WINED3DSIH_DP4 */ shader_glsl_dot,
9299 /* WINED3DSIH_DST */ shader_glsl_dst,
9300 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
9301 /* WINED3DSIH_DSX_COARSE */ shader_glsl_map2gl,
9302 /* WINED3DSIH_DSX_FINE */ shader_glsl_map2gl,
9303 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
9304 /* WINED3DSIH_DSY_COARSE */ shader_glsl_map2gl,
9305 /* WINED3DSIH_DSY_FINE */ shader_glsl_map2gl,
9306 /* WINED3DSIH_ELSE */ shader_glsl_else,
9307 /* WINED3DSIH_EMIT */ shader_glsl_emit,
9308 /* WINED3DSIH_EMIT_STREAM */ shader_glsl_emit,
9309 /* WINED3DSIH_ENDIF */ shader_glsl_end,
9310 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
9311 /* WINED3DSIH_ENDREP */ shader_glsl_end,
9312 /* WINED3DSIH_ENDSWITCH */ shader_glsl_end,
9313 /* WINED3DSIH_EQ */ shader_glsl_relop,
9314 /* WINED3DSIH_EXP */ shader_glsl_scalar_op,
9315 /* WINED3DSIH_EXPP */ shader_glsl_expp,
9316 /* WINED3DSIH_F16TOF32 */ shader_glsl_float16,
9317 /* WINED3DSIH_F32TOF16 */ shader_glsl_float16,
9318 /* WINED3DSIH_FCALL */ NULL,
9319 /* WINED3DSIH_FIRSTBIT_HI */ shader_glsl_map2gl,
9320 /* WINED3DSIH_FIRSTBIT_LO */ shader_glsl_map2gl,
9321 /* WINED3DSIH_FIRSTBIT_SHI */ shader_glsl_map2gl,
9322 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
9323 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
9324 /* WINED3DSIH_FTOU */ shader_glsl_to_uint,
9325 /* WINED3DSIH_GATHER4 */ NULL,
9326 /* WINED3DSIH_GATHER4_C */ NULL,
9327 /* WINED3DSIH_GE */ shader_glsl_relop,
9328 /* WINED3DSIH_HS_CONTROL_POINT_PHASE */ NULL,
9329 /* WINED3DSIH_HS_DECLS */ shader_glsl_nop,
9330 /* WINED3DSIH_HS_FORK_PHASE */ NULL,
9331 /* WINED3DSIH_HS_JOIN_PHASE */ NULL,
9332 /* WINED3DSIH_IADD */ shader_glsl_binop,
9333 /* WINED3DSIH_IEQ */ shader_glsl_relop,
9334 /* WINED3DSIH_IF */ shader_glsl_if,
9335 /* WINED3DSIH_IFC */ shader_glsl_ifc,
9336 /* WINED3DSIH_IGE */ shader_glsl_relop,
9337 /* WINED3DSIH_ILT */ shader_glsl_relop,
9338 /* WINED3DSIH_IMAD */ shader_glsl_mad,
9339 /* WINED3DSIH_IMAX */ shader_glsl_map2gl,
9340 /* WINED3DSIH_IMIN */ shader_glsl_map2gl,
9341 /* WINED3DSIH_IMM_ATOMIC_ALLOC */ NULL,
9342 /* WINED3DSIH_IMM_ATOMIC_AND */ shader_glsl_atomic,
9343 /* WINED3DSIH_IMM_ATOMIC_CMP_EXCH */ shader_glsl_atomic,
9344 /* WINED3DSIH_IMM_ATOMIC_CONSUME */ NULL,
9345 /* WINED3DSIH_IMM_ATOMIC_EXCH */ shader_glsl_atomic,
9346 /* WINED3DSIH_IMM_ATOMIC_IADD */ shader_glsl_atomic,
9347 /* WINED3DSIH_IMM_ATOMIC_IMAX */ NULL,
9348 /* WINED3DSIH_IMM_ATOMIC_OR */ shader_glsl_atomic,
9349 /* WINED3DSIH_IMM_ATOMIC_UMAX */ NULL,
9350 /* WINED3DSIH_IMM_ATOMIC_UMIN */ NULL,
9351 /* WINED3DSIH_IMM_ATOMIC_XOR */ shader_glsl_atomic,
9352 /* WINED3DSIH_IMUL */ shader_glsl_imul,
9353 /* WINED3DSIH_INE */ shader_glsl_relop,
9354 /* WINED3DSIH_INEG */ shader_glsl_unary_op,
9355 /* WINED3DSIH_ISHL */ shader_glsl_binop,
9356 /* WINED3DSIH_ISHR */ shader_glsl_binop,
9357 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
9358 /* WINED3DSIH_LABEL */ shader_glsl_label,
9359 /* WINED3DSIH_LD */ shader_glsl_ld,
9360 /* WINED3DSIH_LD2DMS */ NULL,
9361 /* WINED3DSIH_LD_RAW */ NULL,
9362 /* WINED3DSIH_LD_STRUCTURED */ NULL,
9363 /* WINED3DSIH_LD_UAV_TYPED */ shader_glsl_ld_uav,
9364 /* WINED3DSIH_LIT */ shader_glsl_lit,
9365 /* WINED3DSIH_LOD */ NULL,
9366 /* WINED3DSIH_LOG */ shader_glsl_scalar_op,
9367 /* WINED3DSIH_LOGP */ shader_glsl_scalar_op,
9368 /* WINED3DSIH_LOOP */ shader_glsl_loop,
9369 /* WINED3DSIH_LRP */ shader_glsl_lrp,
9370 /* WINED3DSIH_LT */ shader_glsl_relop,
9371 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
9372 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
9373 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
9374 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
9375 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
9376 /* WINED3DSIH_MAD */ shader_glsl_mad,
9377 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
9378 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
9379 /* WINED3DSIH_MOV */ shader_glsl_mov,
9380 /* WINED3DSIH_MOVA */ shader_glsl_mov,
9381 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
9382 /* WINED3DSIH_MUL */ shader_glsl_binop,
9383 /* WINED3DSIH_NE */ shader_glsl_relop,
9384 /* WINED3DSIH_NOP */ shader_glsl_nop,
9385 /* WINED3DSIH_NOT */ shader_glsl_unary_op,
9386 /* WINED3DSIH_NRM */ shader_glsl_nrm,
9387 /* WINED3DSIH_OR */ shader_glsl_binop,
9388 /* WINED3DSIH_PHASE */ shader_glsl_nop,
9389 /* WINED3DSIH_POW */ shader_glsl_pow,
9390 /* WINED3DSIH_RCP */ shader_glsl_scalar_op,
9391 /* WINED3DSIH_REP */ shader_glsl_rep,
9392 /* WINED3DSIH_RESINFO */ shader_glsl_resinfo,
9393 /* WINED3DSIH_RET */ shader_glsl_ret,
9394 /* WINED3DSIH_ROUND_NE */ shader_glsl_map2gl,
9395 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
9396 /* WINED3DSIH_ROUND_PI */ shader_glsl_map2gl,
9397 /* WINED3DSIH_ROUND_Z */ shader_glsl_map2gl,
9398 /* WINED3DSIH_RSQ */ shader_glsl_scalar_op,
9399 /* WINED3DSIH_SAMPLE */ shader_glsl_sample,
9400 /* WINED3DSIH_SAMPLE_B */ shader_glsl_sample,
9401 /* WINED3DSIH_SAMPLE_C */ shader_glsl_sample_c,
9402 /* WINED3DSIH_SAMPLE_C_LZ */ shader_glsl_sample_c,
9403 /* WINED3DSIH_SAMPLE_GRAD */ shader_glsl_sample,
9404 /* WINED3DSIH_SAMPLE_INFO */ NULL,
9405 /* WINED3DSIH_SAMPLE_LOD */ shader_glsl_sample,
9406 /* WINED3DSIH_SAMPLE_POS */ NULL,
9407 /* WINED3DSIH_SETP */ NULL,
9408 /* WINED3DSIH_SGE */ shader_glsl_compare,
9409 /* WINED3DSIH_SGN */ shader_glsl_sgn,
9410 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
9411 /* WINED3DSIH_SLT */ shader_glsl_compare,
9412 /* WINED3DSIH_SQRT */ shader_glsl_map2gl,
9413 /* WINED3DSIH_STORE_RAW */ shader_glsl_store_raw,
9414 /* WINED3DSIH_STORE_STRUCTURED */ NULL,
9415 /* WINED3DSIH_STORE_UAV_TYPED */ shader_glsl_store_uav,
9416 /* WINED3DSIH_SUB */ shader_glsl_binop,
9417 /* WINED3DSIH_SWAPC */ NULL,
9418 /* WINED3DSIH_SWITCH */ shader_glsl_switch,
9419 /* WINED3DSIH_SYNC */ NULL,
9420 /* WINED3DSIH_TEX */ shader_glsl_tex,
9421 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
9422 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
9423 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
9424 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
9425 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
9426 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
9427 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
9428 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
9429 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
9430 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
9431 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
9432 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
9433 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
9434 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
9435 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
9436 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
9437 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
9438 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
9439 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
9440 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
9441 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
9442 /* WINED3DSIH_UBFE */ shader_glsl_bitwise_op,
9443 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
9444 /* WINED3DSIH_UGE */ shader_glsl_relop,
9445 /* WINED3DSIH_ULT */ shader_glsl_relop,
9446 /* WINED3DSIH_UMAX */ shader_glsl_map2gl,
9447 /* WINED3DSIH_UMIN */ shader_glsl_map2gl,
9448 /* WINED3DSIH_USHR */ shader_glsl_binop,
9449 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
9450 /* WINED3DSIH_XOR */ shader_glsl_binop,
9453 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
9454 SHADER_HANDLER hw_fct;
9456 /* Select handler */
9457 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
9459 /* Unhandled opcode */
9460 if (!hw_fct)
9462 FIXME("Backend can't handle opcode %s.\n", debug_d3dshaderinstructionhandler(ins->handler_idx));
9463 return;
9465 hw_fct(ins);
9467 shader_glsl_add_instruction_modifiers(ins);
9470 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
9472 struct shader_glsl_priv *priv = shader_priv;
9474 return priv->ffp_proj_control;
9477 const struct wined3d_shader_backend_ops glsl_shader_backend =
9479 shader_glsl_handle_instruction,
9480 shader_glsl_select,
9481 shader_glsl_select_compute,
9482 shader_glsl_disable,
9483 shader_glsl_update_float_vertex_constants,
9484 shader_glsl_update_float_pixel_constants,
9485 shader_glsl_load_constants,
9486 shader_glsl_destroy,
9487 shader_glsl_alloc,
9488 shader_glsl_free,
9489 shader_glsl_allocate_context_data,
9490 shader_glsl_free_context_data,
9491 shader_glsl_init_context_state,
9492 shader_glsl_get_caps,
9493 shader_glsl_color_fixup_supported,
9494 shader_glsl_has_ffp_proj_control,
9497 static void glsl_vertex_pipe_vp_enable(const struct wined3d_gl_info *gl_info, BOOL enable) {}
9499 static void glsl_vertex_pipe_vp_get_caps(const struct wined3d_gl_info *gl_info, struct wined3d_vertex_caps *caps)
9501 caps->xyzrhw = TRUE;
9502 caps->emulated_flatshading = !gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9503 caps->ffp_generic_attributes = TRUE;
9504 caps->max_active_lights = MAX_ACTIVE_LIGHTS;
9505 caps->max_vertex_blend_matrices = MAX_VERTEX_BLENDS;
9506 caps->max_vertex_blend_matrix_index = 0;
9507 caps->vertex_processing_caps = WINED3DVTXPCAPS_TEXGEN
9508 | WINED3DVTXPCAPS_MATERIALSOURCE7
9509 | WINED3DVTXPCAPS_VERTEXFOG
9510 | WINED3DVTXPCAPS_DIRECTIONALLIGHTS
9511 | WINED3DVTXPCAPS_POSITIONALLIGHTS
9512 | WINED3DVTXPCAPS_LOCALVIEWER
9513 | WINED3DVTXPCAPS_TEXGEN_SPHEREMAP;
9514 caps->fvf_caps = WINED3DFVFCAPS_PSIZE | 8; /* 8 texture coordinates. */
9515 caps->max_user_clip_planes = gl_info->limits.user_clip_distances;
9516 caps->raster_caps = WINED3DPRASTERCAPS_FOGRANGE;
9519 static DWORD glsl_vertex_pipe_vp_get_emul_mask(const struct wined3d_gl_info *gl_info)
9521 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9522 return GL_EXT_EMUL_ARB_MULTITEXTURE;
9523 return 0;
9526 static void *glsl_vertex_pipe_vp_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
9528 struct shader_glsl_priv *priv;
9530 if (shader_backend == &glsl_shader_backend)
9532 priv = shader_priv;
9533 wine_rb_init(&priv->ffp_vertex_shaders, wined3d_ffp_vertex_program_key_compare);
9534 return priv;
9537 FIXME("GLSL vertex pipe without GLSL shader backend not implemented.\n");
9539 return NULL;
9542 static void shader_glsl_free_ffp_vertex_shader(struct wine_rb_entry *entry, void *context)
9544 struct glsl_ffp_vertex_shader *shader = WINE_RB_ENTRY_VALUE(entry,
9545 struct glsl_ffp_vertex_shader, desc.entry);
9546 struct glsl_shader_prog_link *program, *program2;
9547 struct glsl_ffp_destroy_ctx *ctx = context;
9549 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
9550 struct glsl_shader_prog_link, vs.shader_entry)
9552 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
9554 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
9555 HeapFree(GetProcessHeap(), 0, shader);
9558 /* Context activation is done by the caller. */
9559 static void glsl_vertex_pipe_vp_free(struct wined3d_device *device)
9561 struct shader_glsl_priv *priv = device->vertex_priv;
9562 struct glsl_ffp_destroy_ctx ctx;
9564 ctx.priv = priv;
9565 ctx.gl_info = &device->adapter->gl_info;
9566 wine_rb_destroy(&priv->ffp_vertex_shaders, shader_glsl_free_ffp_vertex_shader, &ctx);
9569 static void glsl_vertex_pipe_nop(struct wined3d_context *context,
9570 const struct wined3d_state *state, DWORD state_id) {}
9572 static void glsl_vertex_pipe_shader(struct wined3d_context *context,
9573 const struct wined3d_state *state, DWORD state_id)
9575 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9578 static void glsl_vertex_pipe_vdecl(struct wined3d_context *context,
9579 const struct wined3d_state *state, DWORD state_id)
9581 const struct wined3d_gl_info *gl_info = context->gl_info;
9582 BOOL normal = !!(context->stream_info.use_map & (1u << WINED3D_FFP_NORMAL));
9583 BOOL legacy_context = gl_info->supported[WINED3D_GL_LEGACY_CONTEXT];
9584 BOOL transformed = context->stream_info.position_transformed;
9585 BOOL wasrhw = context->last_was_rhw;
9586 unsigned int i;
9588 context->last_was_rhw = transformed;
9590 /* If the vertex declaration contains a transformed position attribute,
9591 * the draw uses the fixed function vertex pipeline regardless of any
9592 * vertex shader set by the application. */
9593 if (transformed != wasrhw
9594 || context->stream_info.swizzle_map != context->last_swizzle_map)
9595 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9597 context->last_swizzle_map = context->stream_info.swizzle_map;
9599 if (!use_vs(state))
9601 if (context->last_was_vshader)
9603 if (legacy_context)
9604 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9605 clipplane(context, state, STATE_CLIPPLANE(i));
9606 else
9607 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9610 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9612 /* Because of settings->texcoords, we have to regenerate the vertex
9613 * shader on a vdecl change if there aren't enough varyings to just
9614 * always output all the texture coordinates. */
9615 if (gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(gl_info)
9616 || normal != context->last_was_normal)
9617 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9619 if (use_ps(state)
9620 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.major == 1
9621 && state->shader[WINED3D_SHADER_TYPE_PIXEL]->reg_maps.shader_version.minor <= 3)
9622 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
9624 else
9626 if (!context->last_was_vshader)
9628 /* Vertex shader clipping ignores the view matrix. Update all clip planes. */
9629 if (legacy_context)
9630 for (i = 0; i < gl_info->limits.user_clip_distances; ++i)
9631 clipplane(context, state, STATE_CLIPPLANE(i));
9632 else
9633 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9637 context->last_was_vshader = use_vs(state);
9638 context->last_was_normal = normal;
9641 static void glsl_vertex_pipe_vs(struct wined3d_context *context,
9642 const struct wined3d_state *state, DWORD state_id)
9644 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9645 /* Different vertex shaders potentially require a different vertex attributes setup. */
9646 if (!isStateDirty(context, STATE_VDECL))
9647 context_apply_state(context, state, STATE_VDECL);
9650 static void glsl_vertex_pipe_geometry_shader(struct wined3d_context *context,
9651 const struct wined3d_state *state, DWORD state_id)
9653 if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9654 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9655 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9658 static void glsl_vertex_pipe_pixel_shader(struct wined3d_context *context,
9659 const struct wined3d_state *state, DWORD state_id)
9661 if (state->shader[WINED3D_SHADER_TYPE_GEOMETRY])
9662 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_GEOMETRY;
9663 else if (state->shader[WINED3D_SHADER_TYPE_VERTEX]
9664 && state->shader[WINED3D_SHADER_TYPE_VERTEX]->reg_maps.shader_version.major >= 4)
9665 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9668 static void glsl_vertex_pipe_world(struct wined3d_context *context,
9669 const struct wined3d_state *state, DWORD state_id)
9671 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW;
9674 static void glsl_vertex_pipe_vertexblend(struct wined3d_context *context,
9675 const struct wined3d_state *state, DWORD state_id)
9677 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9680 static void glsl_vertex_pipe_view(struct wined3d_context *context, const struct wined3d_state *state, DWORD state_id)
9682 const struct wined3d_gl_info *gl_info = context->gl_info;
9683 unsigned int k;
9685 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MODELVIEW
9686 | WINED3D_SHADER_CONST_FFP_LIGHTS
9687 | WINED3D_SHADER_CONST_FFP_VERTEXBLEND;
9689 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
9691 for (k = 0; k < gl_info->limits.user_clip_distances; ++k)
9693 if (!isStateDirty(context, STATE_CLIPPLANE(k)))
9694 clipplane(context, state, STATE_CLIPPLANE(k));
9697 else
9699 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9703 static void glsl_vertex_pipe_projection(struct wined3d_context *context,
9704 const struct wined3d_state *state, DWORD state_id)
9706 /* Table fog behavior depends on the projection matrix. */
9707 if (state->render_states[WINED3D_RS_FOGENABLE]
9708 && state->render_states[WINED3D_RS_FOGTABLEMODE] != WINED3D_FOG_NONE)
9709 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9710 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PROJ;
9713 static void glsl_vertex_pipe_viewport(struct wined3d_context *context,
9714 const struct wined3d_state *state, DWORD state_id)
9716 if (!isStateDirty(context, STATE_TRANSFORM(WINED3D_TS_PROJECTION)))
9717 glsl_vertex_pipe_projection(context, state, STATE_TRANSFORM(WINED3D_TS_PROJECTION));
9718 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_POINTSCALEENABLE))
9719 && state->render_states[WINED3D_RS_POINTSCALEENABLE])
9720 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9721 context->constant_update_mask |= WINED3D_SHADER_CONST_POS_FIXUP;
9724 static void glsl_vertex_pipe_texmatrix(struct wined3d_context *context,
9725 const struct wined3d_state *state, DWORD state_id)
9727 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9730 static void glsl_vertex_pipe_texmatrix_np2(struct wined3d_context *context,
9731 const struct wined3d_state *state, DWORD state_id)
9733 DWORD sampler = state_id - STATE_SAMPLER(0);
9734 const struct wined3d_texture *texture = state->textures[sampler];
9735 BOOL np2;
9737 if (!texture)
9738 return;
9740 if (sampler >= MAX_TEXTURES)
9741 return;
9743 if ((np2 = !(texture->flags & WINED3D_TEXTURE_POW2_MAT_IDENT))
9744 || context->lastWasPow2Texture & (1u << sampler))
9746 if (np2)
9747 context->lastWasPow2Texture |= 1u << sampler;
9748 else
9749 context->lastWasPow2Texture &= ~(1u << sampler);
9751 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_TEXMATRIX;
9755 static void glsl_vertex_pipe_material(struct wined3d_context *context,
9756 const struct wined3d_state *state, DWORD state_id)
9758 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_MATERIAL;
9761 static void glsl_vertex_pipe_light(struct wined3d_context *context,
9762 const struct wined3d_state *state, DWORD state_id)
9764 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_LIGHTS;
9767 static void glsl_vertex_pipe_pointsize(struct wined3d_context *context,
9768 const struct wined3d_state *state, DWORD state_id)
9770 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9773 static void glsl_vertex_pipe_pointscale(struct wined3d_context *context,
9774 const struct wined3d_state *state, DWORD state_id)
9776 if (!use_vs(state))
9777 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_POINTSIZE;
9780 static void glsl_vertex_pointsprite_core(struct wined3d_context *context,
9781 const struct wined3d_state *state, DWORD state_id)
9783 static unsigned int once;
9785 if (state->gl_primitive_type == GL_POINTS && !state->render_states[WINED3D_RS_POINTSPRITEENABLE] && !once++)
9786 FIXME("Non-point sprite points not supported in core profile.\n");
9789 static void glsl_vertex_pipe_shademode(struct wined3d_context *context,
9790 const struct wined3d_state *state, DWORD state_id)
9792 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_VERTEX;
9795 static void glsl_vertex_pipe_clip_plane(struct wined3d_context *context,
9796 const struct wined3d_state *state, DWORD state_id)
9798 const struct wined3d_gl_info *gl_info = context->gl_info;
9799 UINT index = state_id - STATE_CLIPPLANE(0);
9801 if (index >= gl_info->limits.user_clip_distances)
9802 return;
9804 context->constant_update_mask |= WINED3D_SHADER_CONST_VS_CLIP_PLANES;
9807 static const struct StateEntryTemplate glsl_vertex_pipe_vp_states[] =
9809 {STATE_VDECL, {STATE_VDECL, glsl_vertex_pipe_vdecl }, WINED3D_GL_EXT_NONE },
9810 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_vertex_pipe_vs }, WINED3D_GL_EXT_NONE },
9811 {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), {STATE_SHADER(WINED3D_SHADER_TYPE_GEOMETRY), glsl_vertex_pipe_geometry_shader}, WINED3D_GL_EXT_NONE },
9812 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_vertex_pipe_pixel_shader}, WINED3D_GL_EXT_NONE },
9813 {STATE_MATERIAL, {STATE_RENDER(WINED3D_RS_SPECULARENABLE), NULL }, WINED3D_GL_EXT_NONE },
9814 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_vertex_pipe_material}, WINED3D_GL_EXT_NONE },
9815 /* Clip planes */
9816 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9817 {STATE_CLIPPLANE(0), {STATE_CLIPPLANE(0), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9818 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9819 {STATE_CLIPPLANE(1), {STATE_CLIPPLANE(1), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9820 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9821 {STATE_CLIPPLANE(2), {STATE_CLIPPLANE(2), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9822 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9823 {STATE_CLIPPLANE(3), {STATE_CLIPPLANE(3), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9824 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9825 {STATE_CLIPPLANE(4), {STATE_CLIPPLANE(4), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9826 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9827 {STATE_CLIPPLANE(5), {STATE_CLIPPLANE(5), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9828 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9829 {STATE_CLIPPLANE(6), {STATE_CLIPPLANE(6), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9830 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9831 {STATE_CLIPPLANE(7), {STATE_CLIPPLANE(7), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9832 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9833 {STATE_CLIPPLANE(8), {STATE_CLIPPLANE(8), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9834 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9835 {STATE_CLIPPLANE(9), {STATE_CLIPPLANE(9), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9836 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9837 {STATE_CLIPPLANE(10), {STATE_CLIPPLANE(10), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9838 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9839 {STATE_CLIPPLANE(11), {STATE_CLIPPLANE(11), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9840 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9841 {STATE_CLIPPLANE(12), {STATE_CLIPPLANE(12), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9842 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9843 {STATE_CLIPPLANE(13), {STATE_CLIPPLANE(13), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9844 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9845 {STATE_CLIPPLANE(14), {STATE_CLIPPLANE(14), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9846 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9847 {STATE_CLIPPLANE(15), {STATE_CLIPPLANE(15), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9848 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9849 {STATE_CLIPPLANE(16), {STATE_CLIPPLANE(16), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9850 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9851 {STATE_CLIPPLANE(17), {STATE_CLIPPLANE(17), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9852 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9853 {STATE_CLIPPLANE(18), {STATE_CLIPPLANE(18), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9854 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9855 {STATE_CLIPPLANE(19), {STATE_CLIPPLANE(19), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9856 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9857 {STATE_CLIPPLANE(20), {STATE_CLIPPLANE(20), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9858 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9859 {STATE_CLIPPLANE(21), {STATE_CLIPPLANE(21), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9860 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9861 {STATE_CLIPPLANE(22), {STATE_CLIPPLANE(22), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9862 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9863 {STATE_CLIPPLANE(23), {STATE_CLIPPLANE(23), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9864 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9865 {STATE_CLIPPLANE(24), {STATE_CLIPPLANE(24), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9866 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9867 {STATE_CLIPPLANE(25), {STATE_CLIPPLANE(25), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9868 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9869 {STATE_CLIPPLANE(26), {STATE_CLIPPLANE(26), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9870 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9871 {STATE_CLIPPLANE(27), {STATE_CLIPPLANE(27), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9872 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9873 {STATE_CLIPPLANE(28), {STATE_CLIPPLANE(28), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9874 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9875 {STATE_CLIPPLANE(29), {STATE_CLIPPLANE(29), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9876 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9877 {STATE_CLIPPLANE(30), {STATE_CLIPPLANE(30), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9878 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), clipplane }, WINED3D_GL_LEGACY_CONTEXT },
9879 {STATE_CLIPPLANE(31), {STATE_CLIPPLANE(31), glsl_vertex_pipe_clip_plane}, WINED3D_GL_EXT_NONE },
9880 /* Lights */
9881 {STATE_LIGHT_TYPE, {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9882 {STATE_ACTIVELIGHT(0), {STATE_ACTIVELIGHT(0), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9883 {STATE_ACTIVELIGHT(1), {STATE_ACTIVELIGHT(1), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9884 {STATE_ACTIVELIGHT(2), {STATE_ACTIVELIGHT(2), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9885 {STATE_ACTIVELIGHT(3), {STATE_ACTIVELIGHT(3), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9886 {STATE_ACTIVELIGHT(4), {STATE_ACTIVELIGHT(4), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9887 {STATE_ACTIVELIGHT(5), {STATE_ACTIVELIGHT(5), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9888 {STATE_ACTIVELIGHT(6), {STATE_ACTIVELIGHT(6), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9889 {STATE_ACTIVELIGHT(7), {STATE_ACTIVELIGHT(7), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9890 /* Viewport */
9891 {STATE_VIEWPORT, {STATE_VIEWPORT, glsl_vertex_pipe_viewport}, WINED3D_GL_EXT_NONE },
9892 /* Transform states */
9893 {STATE_TRANSFORM(WINED3D_TS_VIEW), {STATE_TRANSFORM(WINED3D_TS_VIEW), glsl_vertex_pipe_view }, WINED3D_GL_EXT_NONE },
9894 {STATE_TRANSFORM(WINED3D_TS_PROJECTION), {STATE_TRANSFORM(WINED3D_TS_PROJECTION), glsl_vertex_pipe_projection}, WINED3D_GL_EXT_NONE },
9895 {STATE_TRANSFORM(WINED3D_TS_TEXTURE0), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9896 {STATE_TRANSFORM(WINED3D_TS_TEXTURE1), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9897 {STATE_TRANSFORM(WINED3D_TS_TEXTURE2), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9898 {STATE_TRANSFORM(WINED3D_TS_TEXTURE3), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9899 {STATE_TRANSFORM(WINED3D_TS_TEXTURE4), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9900 {STATE_TRANSFORM(WINED3D_TS_TEXTURE5), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9901 {STATE_TRANSFORM(WINED3D_TS_TEXTURE6), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9902 {STATE_TRANSFORM(WINED3D_TS_TEXTURE7), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), NULL }, WINED3D_GL_EXT_NONE },
9903 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(0)), glsl_vertex_pipe_world }, WINED3D_GL_EXT_NONE },
9904 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(1)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9905 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(2)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9906 {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), {STATE_TRANSFORM(WINED3D_TS_WORLD_MATRIX(3)), glsl_vertex_pipe_vertexblend }, WINED3D_GL_EXT_NONE },
9907 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9908 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9909 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9910 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9911 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9912 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9913 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9914 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXTURE_TRANSFORM_FLAGS), glsl_vertex_pipe_texmatrix}, WINED3D_GL_EXT_NONE },
9915 {STATE_TEXTURESTAGE(0, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9916 {STATE_TEXTURESTAGE(1, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9917 {STATE_TEXTURESTAGE(2, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9918 {STATE_TEXTURESTAGE(3, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9919 {STATE_TEXTURESTAGE(4, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9920 {STATE_TEXTURESTAGE(5, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9921 {STATE_TEXTURESTAGE(6, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9922 {STATE_TEXTURESTAGE(7, WINED3D_TSS_TEXCOORD_INDEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9923 /* Fog */
9924 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9925 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9926 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9927 {STATE_RENDER(WINED3D_RS_RANGEFOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
9928 {STATE_RENDER(WINED3D_RS_CLIPPING), {STATE_RENDER(WINED3D_RS_CLIPPING), state_clipping }, WINED3D_GL_EXT_NONE },
9929 {STATE_RENDER(WINED3D_RS_CLIPPLANEENABLE), {STATE_RENDER(WINED3D_RS_CLIPPING), NULL }, WINED3D_GL_EXT_NONE },
9930 {STATE_RENDER(WINED3D_RS_LIGHTING), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9931 {STATE_RENDER(WINED3D_RS_AMBIENT), {STATE_RENDER(WINED3D_RS_AMBIENT), glsl_vertex_pipe_light }, WINED3D_GL_EXT_NONE },
9932 {STATE_RENDER(WINED3D_RS_COLORVERTEX), {STATE_RENDER(WINED3D_RS_COLORVERTEX), glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9933 {STATE_RENDER(WINED3D_RS_LOCALVIEWER), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9934 {STATE_RENDER(WINED3D_RS_NORMALIZENORMALS), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9935 {STATE_RENDER(WINED3D_RS_DIFFUSEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9936 {STATE_RENDER(WINED3D_RS_SPECULARMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9937 {STATE_RENDER(WINED3D_RS_AMBIENTMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9938 {STATE_RENDER(WINED3D_RS_EMISSIVEMATERIALSOURCE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9939 {STATE_RENDER(WINED3D_RS_VERTEXBLEND), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9940 {STATE_RENDER(WINED3D_RS_POINTSIZE), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9941 {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), glsl_vertex_pipe_pointsize}, WINED3D_GL_EXT_NONE },
9942 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite }, ARB_POINT_SPRITE },
9943 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), state_pointsprite_w }, WINED3D_GL_LEGACY_CONTEXT },
9944 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_vertex_pointsprite_core}, WINED3D_GL_EXT_NONE },
9945 {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), glsl_vertex_pipe_pointscale}, WINED3D_GL_EXT_NONE },
9946 {STATE_RENDER(WINED3D_RS_POINTSCALE_A), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9947 {STATE_RENDER(WINED3D_RS_POINTSCALE_B), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9948 {STATE_RENDER(WINED3D_RS_POINTSCALE_C), {STATE_RENDER(WINED3D_RS_POINTSCALEENABLE), NULL }, WINED3D_GL_EXT_NONE },
9949 {STATE_RENDER(WINED3D_RS_POINTSIZE_MAX), {STATE_RENDER(WINED3D_RS_POINTSIZE_MIN), NULL }, WINED3D_GL_EXT_NONE },
9950 {STATE_RENDER(WINED3D_RS_TWEENFACTOR), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9951 {STATE_RENDER(WINED3D_RS_INDEXEDVERTEXBLENDENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), NULL }, WINED3D_GL_EXT_NONE },
9952 /* NP2 texture matrix fixups. They are not needed if
9953 * GL_ARB_texture_non_power_of_two is supported. Otherwise, register
9954 * glsl_vertex_pipe_texmatrix(), which takes care of updating the texture
9955 * matrix. */
9956 {STATE_SAMPLER(0), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9957 {STATE_SAMPLER(0), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9958 {STATE_SAMPLER(0), {STATE_SAMPLER(0), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9959 {STATE_SAMPLER(1), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9960 {STATE_SAMPLER(1), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9961 {STATE_SAMPLER(1), {STATE_SAMPLER(1), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9962 {STATE_SAMPLER(2), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9963 {STATE_SAMPLER(2), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9964 {STATE_SAMPLER(2), {STATE_SAMPLER(2), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9965 {STATE_SAMPLER(3), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9966 {STATE_SAMPLER(3), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9967 {STATE_SAMPLER(3), {STATE_SAMPLER(3), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9968 {STATE_SAMPLER(4), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9969 {STATE_SAMPLER(4), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9970 {STATE_SAMPLER(4), {STATE_SAMPLER(4), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9971 {STATE_SAMPLER(5), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9972 {STATE_SAMPLER(5), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9973 {STATE_SAMPLER(5), {STATE_SAMPLER(5), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9974 {STATE_SAMPLER(6), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9975 {STATE_SAMPLER(6), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9976 {STATE_SAMPLER(6), {STATE_SAMPLER(6), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9977 {STATE_SAMPLER(7), {0, NULL }, ARB_TEXTURE_NON_POWER_OF_TWO },
9978 {STATE_SAMPLER(7), {0, NULL }, WINED3D_GL_NORMALIZED_TEXRECT},
9979 {STATE_SAMPLER(7), {STATE_SAMPLER(7), glsl_vertex_pipe_texmatrix_np2}, WINED3D_GL_EXT_NONE },
9980 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_vertex_pipe_shader}, WINED3D_GL_EXT_NONE },
9981 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_nop }, WINED3D_GL_LEGACY_CONTEXT },
9982 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_vertex_pipe_shademode}, WINED3D_GL_EXT_NONE },
9983 {0 /* Terminate */, {0, NULL }, WINED3D_GL_EXT_NONE },
9986 /* TODO:
9987 * - Implement vertex tweening. */
9988 const struct wined3d_vertex_pipe_ops glsl_vertex_pipe =
9990 glsl_vertex_pipe_vp_enable,
9991 glsl_vertex_pipe_vp_get_caps,
9992 glsl_vertex_pipe_vp_get_emul_mask,
9993 glsl_vertex_pipe_vp_alloc,
9994 glsl_vertex_pipe_vp_free,
9995 glsl_vertex_pipe_vp_states,
9998 static void glsl_fragment_pipe_enable(const struct wined3d_gl_info *gl_info, BOOL enable)
10000 /* Nothing to do. */
10003 static void glsl_fragment_pipe_get_caps(const struct wined3d_gl_info *gl_info, struct fragment_caps *caps)
10005 caps->wined3d_caps = WINED3D_FRAGMENT_CAP_PROJ_CONTROL
10006 | WINED3D_FRAGMENT_CAP_SRGB_WRITE
10007 | WINED3D_FRAGMENT_CAP_COLOR_KEY;
10008 caps->PrimitiveMiscCaps = WINED3DPMISCCAPS_TSSARGTEMP
10009 | WINED3DPMISCCAPS_PERSTAGECONSTANT;
10010 caps->TextureOpCaps = WINED3DTEXOPCAPS_DISABLE
10011 | WINED3DTEXOPCAPS_SELECTARG1
10012 | WINED3DTEXOPCAPS_SELECTARG2
10013 | WINED3DTEXOPCAPS_MODULATE4X
10014 | WINED3DTEXOPCAPS_MODULATE2X
10015 | WINED3DTEXOPCAPS_MODULATE
10016 | WINED3DTEXOPCAPS_ADDSIGNED2X
10017 | WINED3DTEXOPCAPS_ADDSIGNED
10018 | WINED3DTEXOPCAPS_ADD
10019 | WINED3DTEXOPCAPS_SUBTRACT
10020 | WINED3DTEXOPCAPS_ADDSMOOTH
10021 | WINED3DTEXOPCAPS_BLENDCURRENTALPHA
10022 | WINED3DTEXOPCAPS_BLENDFACTORALPHA
10023 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHA
10024 | WINED3DTEXOPCAPS_BLENDDIFFUSEALPHA
10025 | WINED3DTEXOPCAPS_BLENDTEXTUREALPHAPM
10026 | WINED3DTEXOPCAPS_MODULATEALPHA_ADDCOLOR
10027 | WINED3DTEXOPCAPS_MODULATECOLOR_ADDALPHA
10028 | WINED3DTEXOPCAPS_MODULATEINVCOLOR_ADDALPHA
10029 | WINED3DTEXOPCAPS_MODULATEINVALPHA_ADDCOLOR
10030 | WINED3DTEXOPCAPS_DOTPRODUCT3
10031 | WINED3DTEXOPCAPS_MULTIPLYADD
10032 | WINED3DTEXOPCAPS_LERP
10033 | WINED3DTEXOPCAPS_BUMPENVMAP
10034 | WINED3DTEXOPCAPS_BUMPENVMAPLUMINANCE;
10035 caps->MaxTextureBlendStages = MAX_TEXTURES;
10036 caps->MaxSimultaneousTextures = min(gl_info->limits.fragment_samplers, MAX_TEXTURES);
10039 static DWORD glsl_fragment_pipe_get_emul_mask(const struct wined3d_gl_info *gl_info)
10041 if (gl_info->supported[WINED3D_GL_LEGACY_CONTEXT])
10042 return GL_EXT_EMUL_ARB_MULTITEXTURE;
10043 return 0;
10046 static void *glsl_fragment_pipe_alloc(const struct wined3d_shader_backend_ops *shader_backend, void *shader_priv)
10048 struct shader_glsl_priv *priv;
10050 if (shader_backend == &glsl_shader_backend)
10052 priv = shader_priv;
10053 wine_rb_init(&priv->ffp_fragment_shaders, wined3d_ffp_frag_program_key_compare);
10054 return priv;
10057 FIXME("GLSL fragment pipe without GLSL shader backend not implemented.\n");
10059 return NULL;
10062 static void shader_glsl_free_ffp_fragment_shader(struct wine_rb_entry *entry, void *context)
10064 struct glsl_ffp_fragment_shader *shader = WINE_RB_ENTRY_VALUE(entry,
10065 struct glsl_ffp_fragment_shader, entry.entry);
10066 struct glsl_shader_prog_link *program, *program2;
10067 struct glsl_ffp_destroy_ctx *ctx = context;
10069 LIST_FOR_EACH_ENTRY_SAFE(program, program2, &shader->linked_programs,
10070 struct glsl_shader_prog_link, ps.shader_entry)
10072 delete_glsl_program_entry(ctx->priv, ctx->gl_info, program);
10074 ctx->gl_info->gl_ops.ext.p_glDeleteShader(shader->id);
10075 HeapFree(GetProcessHeap(), 0, shader);
10078 /* Context activation is done by the caller. */
10079 static void glsl_fragment_pipe_free(struct wined3d_device *device)
10081 struct shader_glsl_priv *priv = device->fragment_priv;
10082 struct glsl_ffp_destroy_ctx ctx;
10084 ctx.priv = priv;
10085 ctx.gl_info = &device->adapter->gl_info;
10086 wine_rb_destroy(&priv->ffp_fragment_shaders, shader_glsl_free_ffp_fragment_shader, &ctx);
10089 static void glsl_fragment_pipe_shader(struct wined3d_context *context,
10090 const struct wined3d_state *state, DWORD state_id)
10092 context->last_was_pshader = use_ps(state);
10094 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10097 static void glsl_fragment_pipe_fogparams(struct wined3d_context *context,
10098 const struct wined3d_state *state, DWORD state_id)
10100 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10103 static void glsl_fragment_pipe_fog(struct wined3d_context *context,
10104 const struct wined3d_state *state, DWORD state_id)
10106 BOOL use_vshader = use_vs(state);
10107 enum fogsource new_source;
10108 DWORD fogstart = state->render_states[WINED3D_RS_FOGSTART];
10109 DWORD fogend = state->render_states[WINED3D_RS_FOGEND];
10111 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10113 if (!state->render_states[WINED3D_RS_FOGENABLE])
10114 return;
10116 if (state->render_states[WINED3D_RS_FOGTABLEMODE] == WINED3D_FOG_NONE)
10118 if (use_vshader)
10119 new_source = FOGSOURCE_VS;
10120 else if (state->render_states[WINED3D_RS_FOGVERTEXMODE] == WINED3D_FOG_NONE || context->stream_info.position_transformed)
10121 new_source = FOGSOURCE_COORD;
10122 else
10123 new_source = FOGSOURCE_FFP;
10125 else
10127 new_source = FOGSOURCE_FFP;
10130 if (new_source != context->fog_source || fogstart == fogend)
10132 context->fog_source = new_source;
10133 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_FOG;
10137 static void glsl_fragment_pipe_vdecl(struct wined3d_context *context,
10138 const struct wined3d_state *state, DWORD state_id)
10140 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10141 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10142 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10144 if (!isStateDirty(context, STATE_RENDER(WINED3D_RS_FOGENABLE)))
10145 glsl_fragment_pipe_fog(context, state, state_id);
10148 static void glsl_fragment_pipe_vs(struct wined3d_context *context,
10149 const struct wined3d_state *state, DWORD state_id)
10151 /* Because of settings->texcoords_initialized and args->texcoords_initialized. */
10152 if (context->gl_info->limits.glsl_varyings < wined3d_max_compat_varyings(context->gl_info))
10153 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10156 static void glsl_fragment_pipe_tex_transform(struct wined3d_context *context,
10157 const struct wined3d_state *state, DWORD state_id)
10159 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10162 static void glsl_fragment_pipe_invalidate_constants(struct wined3d_context *context,
10163 const struct wined3d_state *state, DWORD state_id)
10165 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_PS;
10168 static void glsl_fragment_pipe_alpha_test_func(struct wined3d_context *context,
10169 const struct wined3d_state *state, DWORD state_id)
10171 const struct wined3d_gl_info *gl_info = context->gl_info;
10172 GLint func = wined3d_gl_compare_func(state->render_states[WINED3D_RS_ALPHAFUNC]);
10173 float ref = state->render_states[WINED3D_RS_ALPHAREF] / 255.0f;
10175 if (func)
10177 gl_info->gl_ops.gl.p_glAlphaFunc(func, ref);
10178 checkGLcall("glAlphaFunc");
10182 static void glsl_fragment_pipe_core_alpha_test(struct wined3d_context *context,
10183 const struct wined3d_state *state, DWORD state_id)
10185 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10188 static void glsl_fragment_pipe_alpha_test(struct wined3d_context *context,
10189 const struct wined3d_state *state, DWORD state_id)
10191 const struct wined3d_gl_info *gl_info = context->gl_info;
10193 if (state->render_states[WINED3D_RS_ALPHATESTENABLE])
10195 gl_info->gl_ops.gl.p_glEnable(GL_ALPHA_TEST);
10196 checkGLcall("glEnable(GL_ALPHA_TEST)");
10198 else
10200 gl_info->gl_ops.gl.p_glDisable(GL_ALPHA_TEST);
10201 checkGLcall("glDisable(GL_ALPHA_TEST)");
10205 static void glsl_fragment_pipe_core_alpha_test_ref(struct wined3d_context *context,
10206 const struct wined3d_state *state, DWORD state_id)
10208 context->constant_update_mask |= WINED3D_SHADER_CONST_PS_ALPHA_TEST;
10211 static void glsl_fragment_pipe_color_key(struct wined3d_context *context,
10212 const struct wined3d_state *state, DWORD state_id)
10214 context->constant_update_mask |= WINED3D_SHADER_CONST_FFP_COLOR_KEY;
10217 static void glsl_fragment_pipe_shademode(struct wined3d_context *context,
10218 const struct wined3d_state *state, DWORD state_id)
10220 context->shader_update_mask |= 1u << WINED3D_SHADER_TYPE_PIXEL;
10223 static const struct StateEntryTemplate glsl_fragment_pipe_state_template[] =
10225 {STATE_VDECL, {STATE_VDECL, glsl_fragment_pipe_vdecl }, WINED3D_GL_EXT_NONE },
10226 {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), {STATE_SHADER(WINED3D_SHADER_TYPE_VERTEX), glsl_fragment_pipe_vs }, WINED3D_GL_EXT_NONE },
10227 {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), {STATE_RENDER(WINED3D_RS_TEXTUREFACTOR), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10228 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10229 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10230 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10231 {STATE_TEXTURESTAGE(0, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10232 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10233 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10234 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10235 {STATE_TEXTURESTAGE(0, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10236 {STATE_TEXTURESTAGE(0, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10237 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10238 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10239 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10240 {STATE_TEXTURESTAGE(1, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10241 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10242 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10243 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10244 {STATE_TEXTURESTAGE(1, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10245 {STATE_TEXTURESTAGE(1, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10246 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10247 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10248 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10249 {STATE_TEXTURESTAGE(2, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10250 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10251 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10252 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10253 {STATE_TEXTURESTAGE(2, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10254 {STATE_TEXTURESTAGE(2, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10255 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10256 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10257 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10258 {STATE_TEXTURESTAGE(3, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10259 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10260 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10261 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10262 {STATE_TEXTURESTAGE(3, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10263 {STATE_TEXTURESTAGE(3, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10264 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10265 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10266 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10267 {STATE_TEXTURESTAGE(4, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10268 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10269 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10270 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10271 {STATE_TEXTURESTAGE(4, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10272 {STATE_TEXTURESTAGE(4, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10273 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10274 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10275 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10276 {STATE_TEXTURESTAGE(5, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10277 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10278 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10279 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10280 {STATE_TEXTURESTAGE(5, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10281 {STATE_TEXTURESTAGE(5, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10282 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10283 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10284 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10285 {STATE_TEXTURESTAGE(6, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10286 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10287 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10288 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10289 {STATE_TEXTURESTAGE(6, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10290 {STATE_TEXTURESTAGE(6, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10291 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10292 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10293 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10294 {STATE_TEXTURESTAGE(7, WINED3D_TSS_COLOR_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10295 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_OP), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10296 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG1), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10297 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG2), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10298 {STATE_TEXTURESTAGE(7, WINED3D_TSS_ALPHA_ARG0), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10299 {STATE_TEXTURESTAGE(7, WINED3D_TSS_RESULT_ARG), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10300 {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10301 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), glsl_fragment_pipe_alpha_test_func }, WINED3D_GL_LEGACY_CONTEXT},
10302 {STATE_RENDER(WINED3D_RS_ALPHAFUNC), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), NULL }, WINED3D_GL_EXT_NONE },
10303 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAFUNC), NULL }, WINED3D_GL_LEGACY_CONTEXT},
10304 {STATE_RENDER(WINED3D_RS_ALPHAREF), {STATE_RENDER(WINED3D_RS_ALPHAREF), glsl_fragment_pipe_core_alpha_test_ref }, WINED3D_GL_EXT_NONE },
10305 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_alpha_test }, WINED3D_GL_LEGACY_CONTEXT},
10306 {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), {STATE_RENDER(WINED3D_RS_ALPHATESTENABLE), glsl_fragment_pipe_core_alpha_test }, WINED3D_GL_EXT_NONE },
10307 {STATE_RENDER(WINED3D_RS_COLORKEYENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10308 {STATE_COLOR_KEY, { STATE_COLOR_KEY, glsl_fragment_pipe_color_key }, WINED3D_GL_EXT_NONE },
10309 {STATE_RENDER(WINED3D_RS_FOGENABLE), {STATE_RENDER(WINED3D_RS_FOGENABLE), glsl_fragment_pipe_fog }, WINED3D_GL_EXT_NONE },
10310 {STATE_RENDER(WINED3D_RS_FOGTABLEMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10311 {STATE_RENDER(WINED3D_RS_FOGVERTEXMODE), {STATE_RENDER(WINED3D_RS_FOGENABLE), NULL }, WINED3D_GL_EXT_NONE },
10312 {STATE_RENDER(WINED3D_RS_FOGSTART), {STATE_RENDER(WINED3D_RS_FOGSTART), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10313 {STATE_RENDER(WINED3D_RS_FOGEND), {STATE_RENDER(WINED3D_RS_FOGSTART), NULL }, WINED3D_GL_EXT_NONE },
10314 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), state_srgbwrite }, ARB_FRAMEBUFFER_SRGB},
10315 {STATE_RENDER(WINED3D_RS_SRGBWRITEENABLE), {STATE_SHADER(WINED3D_SHADER_TYPE_PIXEL), NULL }, WINED3D_GL_EXT_NONE },
10316 {STATE_RENDER(WINED3D_RS_FOGCOLOR), {STATE_RENDER(WINED3D_RS_FOGCOLOR), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10317 {STATE_RENDER(WINED3D_RS_FOGDENSITY), {STATE_RENDER(WINED3D_RS_FOGDENSITY), glsl_fragment_pipe_fogparams }, WINED3D_GL_EXT_NONE },
10318 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, ARB_POINT_SPRITE },
10319 {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), {STATE_RENDER(WINED3D_RS_POINTSPRITEENABLE), glsl_fragment_pipe_shader }, WINED3D_GL_VERSION_2_0},
10320 {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 },
10321 {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 },
10322 {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 },
10323 {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 },
10324 {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 },
10325 {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 },
10326 {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 },
10327 {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 },
10328 {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(0, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10329 {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(1, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10330 {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(2, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10331 {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(3, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10332 {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(4, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10333 {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(5, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10334 {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(6, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10335 {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), {STATE_TEXTURESTAGE(7, WINED3D_TSS_CONSTANT), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10336 {STATE_RENDER(WINED3D_RS_SPECULARENABLE), {STATE_RENDER(WINED3D_RS_SPECULARENABLE), glsl_fragment_pipe_invalidate_constants}, WINED3D_GL_EXT_NONE },
10337 {STATE_POINT_ENABLE, {STATE_POINT_ENABLE, glsl_fragment_pipe_shader }, WINED3D_GL_EXT_NONE },
10338 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), state_shademode }, WINED3D_GL_LEGACY_CONTEXT},
10339 {STATE_RENDER(WINED3D_RS_SHADEMODE), {STATE_RENDER(WINED3D_RS_SHADEMODE), glsl_fragment_pipe_shademode }, WINED3D_GL_EXT_NONE },
10340 {0 /* Terminate */, {0, 0 }, WINED3D_GL_EXT_NONE },
10343 static BOOL glsl_fragment_pipe_alloc_context_data(struct wined3d_context *context)
10345 return TRUE;
10348 static void glsl_fragment_pipe_free_context_data(struct wined3d_context *context)
10352 const struct fragment_pipeline glsl_fragment_pipe =
10354 glsl_fragment_pipe_enable,
10355 glsl_fragment_pipe_get_caps,
10356 glsl_fragment_pipe_get_emul_mask,
10357 glsl_fragment_pipe_alloc,
10358 glsl_fragment_pipe_free,
10359 glsl_fragment_pipe_alloc_context_data,
10360 glsl_fragment_pipe_free_context_data,
10361 shader_glsl_color_fixup_supported,
10362 glsl_fragment_pipe_state_template,