2 * GLSL pixel and vertex shader implementation
4 * Copyright 2006 Jason Green
5 * Copyright 2006-2007 Henri Verbeet
6 * Copyright 2007-2008 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.
33 #include "wine/port.h"
38 #include "wined3d_private.h"
40 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader);
41 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants);
42 WINE_DECLARE_DEBUG_CHANNEL(d3d);
43 WINE_DECLARE_DEBUG_CHANNEL(winediag);
45 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
46 #define WINED3D_GLSL_SAMPLE_RECT 0x2
47 #define WINED3D_GLSL_SAMPLE_LOD 0x4
48 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
62 struct glsl_sample_function
70 HEAP_NODE_TRAVERSE_LEFT,
71 HEAP_NODE_TRAVERSE_RIGHT,
83 struct constant_entry *entries;
84 unsigned int *positions;
88 /* GLSL shader private data */
89 struct shader_glsl_priv {
90 struct wined3d_shader_buffer shader_buffer;
91 struct wine_rb_tree program_lookup;
92 struct glsl_shader_prog_link *glsl_program;
93 struct constant_heap vconst_heap;
94 struct constant_heap pconst_heap;
96 GLhandleARB depth_blt_program_full[tex_type_count];
97 GLhandleARB depth_blt_program_masked[tex_type_count];
98 UINT next_constant_version;
100 const struct fragment_pipeline *fragment_pipe;
103 /* Struct to maintain data about a linked GLSL program */
104 struct glsl_shader_prog_link {
105 struct wine_rb_entry program_lookup_entry;
106 struct list vshader_entry;
107 struct list pshader_entry;
108 GLhandleARB programId;
109 GLint *vuniformF_locations;
110 GLint *puniformF_locations;
111 GLint vuniformI_locations[MAX_CONST_I];
112 GLint puniformI_locations[MAX_CONST_I];
113 GLint posFixup_location;
114 GLint np2Fixup_location;
115 GLint bumpenvmat_location[MAX_TEXTURES];
116 GLint luminancescale_location[MAX_TEXTURES];
117 GLint luminanceoffset_location[MAX_TEXTURES];
118 GLint ycorrection_location;
119 GLenum vertex_color_clamp;
120 const struct wined3d_shader *vshader;
121 const struct wined3d_shader *pshader;
122 struct vs_compile_args vs_args;
123 struct ps_compile_args ps_args;
124 UINT constant_version;
125 const struct ps_np2fixup_info *np2Fixup_info;
128 struct glsl_program_key
130 const struct wined3d_shader *vshader;
131 const struct wined3d_shader *pshader;
132 struct ps_compile_args ps_args;
133 struct vs_compile_args vs_args;
136 struct shader_glsl_ctx_priv {
137 const struct vs_compile_args *cur_vs_args;
138 const struct ps_compile_args *cur_ps_args;
139 struct ps_np2fixup_info *cur_np2fixup_info;
142 struct glsl_ps_compiled_shader
144 struct ps_compile_args args;
145 struct ps_np2fixup_info np2fixup;
149 struct glsl_vs_compiled_shader
151 struct vs_compile_args args;
155 struct glsl_shader_private
159 struct glsl_vs_compiled_shader *vs;
160 struct glsl_ps_compiled_shader *ps;
162 UINT num_gl_shaders, shader_array_size;
165 static const char *debug_gl_shader_type(GLenum type)
169 #define WINED3D_TO_STR(u) case u: return #u
170 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB);
171 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB);
172 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB);
173 #undef WINED3D_TO_STR
175 return wine_dbg_sprintf("UNKNOWN(%#x)", type);
179 static const char *shader_glsl_get_prefix(enum wined3d_shader_type type)
183 case WINED3D_SHADER_TYPE_VERTEX:
186 case WINED3D_SHADER_TYPE_GEOMETRY:
189 case WINED3D_SHADER_TYPE_PIXEL:
193 FIXME("Unhandled shader type %#x.\n", type);
198 /* Extract a line from the info log.
199 * Note that this modifies the source string. */
200 static char *get_info_log_line(char **ptr)
205 if (!(q = strstr(p, "\n")))
207 if (!*p) return NULL;
217 /** Prints the GLSL info log which will contain error messages if they exist */
218 /* GL locking is done by the caller */
219 static void print_glsl_info_log(const struct wined3d_gl_info *gl_info, GLhandleARB obj)
221 int infologLength = 0;
224 if (!WARN_ON(d3d_shader) && !FIXME_ON(d3d_shader))
227 GL_EXTCALL(glGetObjectParameterivARB(obj,
228 GL_OBJECT_INFO_LOG_LENGTH_ARB,
231 /* A size of 1 is just a null-terminated string, so the log should be bigger than
232 * that if there are errors. */
233 if (infologLength > 1)
237 infoLog = HeapAlloc(GetProcessHeap(), 0, infologLength);
238 /* The info log is supposed to be zero-terminated, but at least some
239 * versions of fglrx don't terminate the string properly. The reported
240 * length does include the terminator, so explicitly set it to zero
242 infoLog[infologLength - 1] = 0;
243 GL_EXTCALL(glGetInfoLogARB(obj, infologLength, NULL, infoLog));
246 if (gl_info->quirks & WINED3D_QUIRK_INFO_LOG_SPAM)
248 WARN("Info log received from GLSL shader #%u:\n", obj);
249 while ((line = get_info_log_line(&ptr))) WARN(" %s\n", line);
253 FIXME("Info log received from GLSL shader #%u:\n", obj);
254 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
256 HeapFree(GetProcessHeap(), 0, infoLog);
260 /* GL locking is done by the caller. */
261 static void shader_glsl_compile(const struct wined3d_gl_info *gl_info, GLhandleARB shader, const char *src)
263 TRACE("Compiling shader object %u.\n", shader);
264 GL_EXTCALL(glShaderSourceARB(shader, 1, &src, NULL));
265 checkGLcall("glShaderSourceARB");
266 GL_EXTCALL(glCompileShaderARB(shader));
267 checkGLcall("glCompileShaderARB");
268 print_glsl_info_log(gl_info, shader);
271 /* GL locking is done by the caller. */
272 static void shader_glsl_dump_program_source(const struct wined3d_gl_info *gl_info, GLhandleARB program)
274 GLint i, object_count, source_size = -1;
275 GLhandleARB *objects;
278 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_ATTACHED_OBJECTS_ARB, &object_count));
279 objects = HeapAlloc(GetProcessHeap(), 0, object_count * sizeof(*objects));
282 ERR("Failed to allocate object array memory.\n");
286 GL_EXTCALL(glGetAttachedObjectsARB(program, object_count, NULL, objects));
287 for (i = 0; i < object_count; ++i)
292 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB, &tmp));
294 if (source_size < tmp)
296 HeapFree(GetProcessHeap(), 0, source);
298 source = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, tmp);
301 ERR("Failed to allocate %d bytes for shader source.\n", tmp);
302 HeapFree(GetProcessHeap(), 0, objects);
308 FIXME("Object %u:\n", objects[i]);
309 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_SUBTYPE_ARB, &tmp));
310 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp));
311 GL_EXTCALL(glGetObjectParameterivARB(objects[i], GL_OBJECT_COMPILE_STATUS_ARB, &tmp));
312 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp);
316 GL_EXTCALL(glGetShaderSourceARB(objects[i], source_size, NULL, source));
317 while ((line = get_info_log_line(&ptr))) FIXME(" %s\n", line);
321 HeapFree(GetProcessHeap(), 0, source);
322 HeapFree(GetProcessHeap(), 0, objects);
325 /* GL locking is done by the caller. */
326 static void shader_glsl_validate_link(const struct wined3d_gl_info *gl_info, GLhandleARB program)
330 if (!TRACE_ON(d3d_shader) && !FIXME_ON(d3d_shader)) return;
332 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_TYPE_ARB, &tmp));
333 if (tmp == GL_PROGRAM_OBJECT_ARB)
335 GL_EXTCALL(glGetObjectParameterivARB(program, GL_OBJECT_LINK_STATUS_ARB, &tmp));
338 FIXME("Program %u link status invalid.\n", program);
339 shader_glsl_dump_program_source(gl_info, program);
343 print_glsl_info_log(gl_info, program);
347 * Loads (pixel shader) samplers
349 /* GL locking is done by the caller */
350 static void shader_glsl_load_psamplers(const struct wined3d_gl_info *gl_info,
351 const DWORD *tex_unit_map, GLhandleARB programId)
354 char sampler_name[20];
357 for (i = 0; i < MAX_FRAGMENT_SAMPLERS; ++i)
359 snprintf(sampler_name, sizeof(sampler_name), "ps_sampler%u", i);
360 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
361 if (name_loc != -1) {
362 DWORD mapped_unit = tex_unit_map[i];
363 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.fragment_samplers)
365 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
366 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
367 checkGLcall("glUniform1iARB");
369 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
375 /* GL locking is done by the caller */
376 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info *gl_info,
377 const DWORD *tex_unit_map, GLhandleARB programId)
380 char sampler_name[20];
383 for (i = 0; i < MAX_VERTEX_SAMPLERS; ++i)
385 snprintf(sampler_name, sizeof(sampler_name), "vs_sampler%u", i);
386 name_loc = GL_EXTCALL(glGetUniformLocationARB(programId, sampler_name));
387 if (name_loc != -1) {
388 DWORD mapped_unit = tex_unit_map[MAX_FRAGMENT_SAMPLERS + i];
389 if (mapped_unit != WINED3D_UNMAPPED_STAGE && mapped_unit < gl_info->limits.combined_samplers)
391 TRACE("Loading %s for texture %d\n", sampler_name, mapped_unit);
392 GL_EXTCALL(glUniform1iARB(name_loc, mapped_unit));
393 checkGLcall("glUniform1iARB");
395 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name, mapped_unit);
401 /* GL locking is done by the caller */
402 static inline void walk_constant_heap(const struct wined3d_gl_info *gl_info, const float *constants,
403 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
406 unsigned int heap_idx = 1;
409 if (heap->entries[heap_idx].version <= version) return;
411 idx = heap->entries[heap_idx].idx;
412 if (constant_locations[idx] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
413 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
415 while (stack_idx >= 0)
417 /* Note that we fall through to the next case statement. */
418 switch(stack[stack_idx])
420 case HEAP_NODE_TRAVERSE_LEFT:
422 unsigned int left_idx = heap_idx << 1;
423 if (left_idx < heap->size && heap->entries[left_idx].version > version)
426 idx = heap->entries[heap_idx].idx;
427 if (constant_locations[idx] != -1)
428 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
430 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
431 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
436 case HEAP_NODE_TRAVERSE_RIGHT:
438 unsigned int right_idx = (heap_idx << 1) + 1;
439 if (right_idx < heap->size && heap->entries[right_idx].version > version)
441 heap_idx = right_idx;
442 idx = heap->entries[heap_idx].idx;
443 if (constant_locations[idx] != -1)
444 GL_EXTCALL(glUniform4fvARB(constant_locations[idx], 1, &constants[idx * 4]));
446 stack[stack_idx++] = HEAP_NODE_POP;
447 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
458 checkGLcall("walk_constant_heap()");
461 /* GL locking is done by the caller */
462 static inline void apply_clamped_constant(const struct wined3d_gl_info *gl_info, GLint location, const GLfloat *data)
464 GLfloat clamped_constant[4];
466 if (location == -1) return;
468 clamped_constant[0] = data[0] < -1.0f ? -1.0f : data[0] > 1.0f ? 1.0f : data[0];
469 clamped_constant[1] = data[1] < -1.0f ? -1.0f : data[1] > 1.0f ? 1.0f : data[1];
470 clamped_constant[2] = data[2] < -1.0f ? -1.0f : data[2] > 1.0f ? 1.0f : data[2];
471 clamped_constant[3] = data[3] < -1.0f ? -1.0f : data[3] > 1.0f ? 1.0f : data[3];
473 GL_EXTCALL(glUniform4fvARB(location, 1, clamped_constant));
476 /* GL locking is done by the caller */
477 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info *gl_info, const float *constants,
478 const GLint *constant_locations, const struct constant_heap *heap, unsigned char *stack, DWORD version)
481 unsigned int heap_idx = 1;
484 if (heap->entries[heap_idx].version <= version) return;
486 idx = heap->entries[heap_idx].idx;
487 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
488 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
490 while (stack_idx >= 0)
492 /* Note that we fall through to the next case statement. */
493 switch(stack[stack_idx])
495 case HEAP_NODE_TRAVERSE_LEFT:
497 unsigned int left_idx = heap_idx << 1;
498 if (left_idx < heap->size && heap->entries[left_idx].version > version)
501 idx = heap->entries[heap_idx].idx;
502 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
504 stack[stack_idx++] = HEAP_NODE_TRAVERSE_RIGHT;
505 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
510 case HEAP_NODE_TRAVERSE_RIGHT:
512 unsigned int right_idx = (heap_idx << 1) + 1;
513 if (right_idx < heap->size && heap->entries[right_idx].version > version)
515 heap_idx = right_idx;
516 idx = heap->entries[heap_idx].idx;
517 apply_clamped_constant(gl_info, constant_locations[idx], &constants[idx * 4]);
519 stack[stack_idx++] = HEAP_NODE_POP;
520 stack[stack_idx] = HEAP_NODE_TRAVERSE_LEFT;
531 checkGLcall("walk_constant_heap_clamped()");
534 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
535 /* GL locking is done by the caller */
536 static void shader_glsl_load_constantsF(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
537 const float *constants, const GLint *constant_locations, const struct constant_heap *heap,
538 unsigned char *stack, UINT version)
540 const struct wined3d_shader_lconst *lconst;
542 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
543 if (shader->reg_maps.shader_version.major == 1
544 && shader->reg_maps.shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
545 walk_constant_heap_clamped(gl_info, constants, constant_locations, heap, stack, version);
547 walk_constant_heap(gl_info, constants, constant_locations, heap, stack, version);
549 if (!shader->load_local_constsF)
551 TRACE("No need to load local float constants for this shader\n");
555 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
556 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
558 GLint location = constant_locations[lconst->idx];
559 /* We found this uniform name in the program - go ahead and send the data */
560 if (location != -1) GL_EXTCALL(glUniform4fvARB(location, 1, (const GLfloat *)lconst->value));
562 checkGLcall("glUniform4fvARB()");
565 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
566 /* GL locking is done by the caller */
567 static void shader_glsl_load_constantsI(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
568 const GLint locations[MAX_CONST_I], const int *constants, WORD constants_set)
573 for (i = 0; constants_set; constants_set >>= 1, ++i)
575 if (!(constants_set & 1)) continue;
577 TRACE_(d3d_constants)("Loading constants %u: %i, %i, %i, %i\n",
578 i, constants[i*4], constants[i*4+1], constants[i*4+2], constants[i*4+3]);
580 /* We found this uniform name in the program - go ahead and send the data */
581 GL_EXTCALL(glUniform4ivARB(locations[i], 1, &constants[i*4]));
582 checkGLcall("glUniform4ivARB");
585 /* Load immediate constants */
586 ptr = list_head(&shader->constantsI);
589 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
590 unsigned int idx = lconst->idx;
591 const GLint *values = (const GLint *)lconst->value;
593 TRACE_(d3d_constants)("Loading local constants %i: %i, %i, %i, %i\n", idx,
594 values[0], values[1], values[2], values[3]);
596 /* We found this uniform name in the program - go ahead and send the data */
597 GL_EXTCALL(glUniform4ivARB(locations[idx], 1, values));
598 checkGLcall("glUniform4ivARB");
599 ptr = list_next(&shader->constantsI, ptr);
603 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
604 /* GL locking is done by the caller */
605 static void shader_glsl_load_constantsB(const struct wined3d_shader *shader, const struct wined3d_gl_info *gl_info,
606 GLhandleARB programId, const BOOL *constants, WORD constants_set)
614 prefix = shader_glsl_get_prefix(shader->reg_maps.shader_version.type);
616 /* TODO: Benchmark and see if it would be beneficial to store the
617 * locations of the constants to avoid looking up each time */
618 for (i = 0; constants_set; constants_set >>= 1, ++i)
620 if (!(constants_set & 1)) continue;
622 TRACE_(d3d_constants)("Loading constants %i: %i;\n", i, constants[i]);
624 /* TODO: Benchmark and see if it would be beneficial to store the
625 * locations of the constants to avoid looking up each time */
626 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, i);
627 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
630 /* We found this uniform name in the program - go ahead and send the data */
631 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, &constants[i]));
632 checkGLcall("glUniform1ivARB");
636 /* Load immediate constants */
637 ptr = list_head(&shader->constantsB);
640 const struct wined3d_shader_lconst *lconst = LIST_ENTRY(ptr, const struct wined3d_shader_lconst, entry);
641 unsigned int idx = lconst->idx;
642 const GLint *values = (const GLint *)lconst->value;
644 TRACE_(d3d_constants)("Loading local constants %i: %i\n", idx, values[0]);
646 snprintf(tmp_name, sizeof(tmp_name), "%s_b[%i]", prefix, idx);
647 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, tmp_name));
649 /* We found this uniform name in the program - go ahead and send the data */
650 GL_EXTCALL(glUniform1ivARB(tmp_loc, 1, values));
651 checkGLcall("glUniform1ivARB");
653 ptr = list_next(&shader->constantsB, ptr);
657 static void reset_program_constant_version(struct wine_rb_entry *entry, void *context)
659 WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry)->constant_version = 0;
663 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
665 /* GL locking is done by the caller (state handler) */
666 static void shader_glsl_load_np2fixup_constants(void *shader_priv,
667 const struct wined3d_gl_info *gl_info, const struct wined3d_state *state)
669 struct shader_glsl_priv *glsl_priv = shader_priv;
670 const struct glsl_shader_prog_link *prog = glsl_priv->glsl_program;
672 /* No GLSL program set - nothing to do. */
675 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
676 if (!use_ps(state)) return;
678 if (prog->ps_args.np2_fixup && prog->np2Fixup_location != -1)
681 UINT fixup = prog->ps_args.np2_fixup;
682 GLfloat np2fixup_constants[4 * MAX_FRAGMENT_SAMPLERS];
684 for (i = 0; fixup; fixup >>= 1, ++i)
686 const struct wined3d_texture *tex = state->textures[i];
687 const unsigned char idx = prog->np2Fixup_info->idx[i];
688 GLfloat *tex_dim = &np2fixup_constants[(idx >> 1) * 4];
692 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
698 tex_dim[2] = tex->pow2_matrix[0];
699 tex_dim[3] = tex->pow2_matrix[5];
703 tex_dim[0] = tex->pow2_matrix[0];
704 tex_dim[1] = tex->pow2_matrix[5];
708 GL_EXTCALL(glUniform4fvARB(prog->np2Fixup_location, prog->np2Fixup_info->num_consts, np2fixup_constants));
713 * Loads the app-supplied constants into the currently set GLSL program.
715 /* GL locking is done by the caller (state handler) */
716 static void shader_glsl_load_constants(const struct wined3d_context *context,
717 BOOL usePixelShader, BOOL useVertexShader)
719 const struct wined3d_gl_info *gl_info = context->gl_info;
720 struct wined3d_device *device = context->swapchain->device;
721 struct wined3d_stateblock *stateBlock = device->stateBlock;
722 const struct wined3d_state *state = &stateBlock->state;
723 struct shader_glsl_priv *priv = device->shader_priv;
724 float position_fixup[4];
726 GLhandleARB programId;
727 struct glsl_shader_prog_link *prog = priv->glsl_program;
728 UINT constant_version;
732 /* No GLSL program set - nothing to do. */
735 programId = prog->programId;
736 constant_version = prog->constant_version;
740 const struct wined3d_shader *vshader = state->vertex_shader;
742 /* Load DirectX 9 float constants/uniforms for vertex shader */
743 shader_glsl_load_constantsF(vshader, gl_info, state->vs_consts_f,
744 prog->vuniformF_locations, &priv->vconst_heap, priv->stack, constant_version);
746 /* Load DirectX 9 integer constants/uniforms for vertex shader */
747 shader_glsl_load_constantsI(vshader, gl_info, prog->vuniformI_locations, state->vs_consts_i,
748 stateBlock->changed.vertexShaderConstantsI & vshader->reg_maps.integer_constants);
750 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
751 shader_glsl_load_constantsB(vshader, gl_info, programId, state->vs_consts_b,
752 stateBlock->changed.vertexShaderConstantsB & vshader->reg_maps.boolean_constants);
754 /* Upload the position fixup params */
755 shader_get_position_fixup(context, state, position_fixup);
756 GL_EXTCALL(glUniform4fvARB(prog->posFixup_location, 1, position_fixup));
757 checkGLcall("glUniform4fvARB");
762 const struct wined3d_shader *pshader = state->pixel_shader;
764 /* Load DirectX 9 float constants/uniforms for pixel shader */
765 shader_glsl_load_constantsF(pshader, gl_info, state->ps_consts_f,
766 prog->puniformF_locations, &priv->pconst_heap, priv->stack, constant_version);
768 /* Load DirectX 9 integer constants/uniforms for pixel shader */
769 shader_glsl_load_constantsI(pshader, gl_info, prog->puniformI_locations, state->ps_consts_i,
770 stateBlock->changed.pixelShaderConstantsI & pshader->reg_maps.integer_constants);
772 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
773 shader_glsl_load_constantsB(pshader, gl_info, programId, state->ps_consts_b,
774 stateBlock->changed.pixelShaderConstantsB & pshader->reg_maps.boolean_constants);
776 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
777 * It can't be 0 for a valid texbem instruction.
779 for(i = 0; i < MAX_TEXTURES; i++) {
782 if(prog->bumpenvmat_location[i] == -1) continue;
784 data = (const float *)&state->texture_states[i][WINED3D_TSS_BUMPENV_MAT00];
785 GL_EXTCALL(glUniformMatrix2fvARB(prog->bumpenvmat_location[i], 1, 0, data));
786 checkGLcall("glUniformMatrix2fvARB");
788 /* texbeml needs the luminance scale and offset too. If texbeml
789 * is used, needsbumpmat is set too, so we can check that in the
790 * needsbumpmat check. */
791 if (prog->luminancescale_location[i] != -1)
793 const GLfloat *scale = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LSCALE];
794 const GLfloat *offset = (const GLfloat *)&state->texture_states[i][WINED3D_TSS_BUMPENV_LOFFSET];
796 GL_EXTCALL(glUniform1fvARB(prog->luminancescale_location[i], 1, scale));
797 checkGLcall("glUniform1fvARB");
798 GL_EXTCALL(glUniform1fvARB(prog->luminanceoffset_location[i], 1, offset));
799 checkGLcall("glUniform1fvARB");
803 if (prog->ycorrection_location != -1)
805 float correction_params[4];
807 if (context->render_offscreen)
809 correction_params[0] = 0.0f;
810 correction_params[1] = 1.0f;
812 /* position is window relative, not viewport relative */
813 correction_params[0] = (float) context->current_rt->resource.height;
814 correction_params[1] = -1.0f;
816 GL_EXTCALL(glUniform4fvARB(prog->ycorrection_location, 1, correction_params));
820 if (priv->next_constant_version == UINT_MAX)
822 TRACE("Max constant version reached, resetting to 0.\n");
823 wine_rb_for_each_entry(&priv->program_lookup, reset_program_constant_version, NULL);
824 priv->next_constant_version = 1;
828 prog->constant_version = priv->next_constant_version++;
832 static void update_heap_entry(const struct constant_heap *heap, unsigned int idx,
833 unsigned int heap_idx, DWORD new_version)
835 struct constant_entry *entries = heap->entries;
836 unsigned int *positions = heap->positions;
837 unsigned int parent_idx;
841 parent_idx = heap_idx >> 1;
843 if (new_version <= entries[parent_idx].version) break;
845 entries[heap_idx] = entries[parent_idx];
846 positions[entries[parent_idx].idx] = heap_idx;
847 heap_idx = parent_idx;
850 entries[heap_idx].version = new_version;
851 entries[heap_idx].idx = idx;
852 positions[idx] = heap_idx;
855 static void shader_glsl_update_float_vertex_constants(struct wined3d_device *device, UINT start, UINT count)
857 struct shader_glsl_priv *priv = device->shader_priv;
858 struct constant_heap *heap = &priv->vconst_heap;
861 for (i = start; i < count + start; ++i)
863 if (!device->stateBlock->changed.vertexShaderConstantsF[i])
864 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
866 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
870 static void shader_glsl_update_float_pixel_constants(struct wined3d_device *device, UINT start, UINT count)
872 struct shader_glsl_priv *priv = device->shader_priv;
873 struct constant_heap *heap = &priv->pconst_heap;
876 for (i = start; i < count + start; ++i)
878 if (!device->stateBlock->changed.pixelShaderConstantsF[i])
879 update_heap_entry(heap, i, heap->size++, priv->next_constant_version);
881 update_heap_entry(heap, i, heap->positions[i], priv->next_constant_version);
885 static unsigned int vec4_varyings(DWORD shader_major, const struct wined3d_gl_info *gl_info)
887 unsigned int ret = gl_info->limits.glsl_varyings / 4;
888 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
889 if(shader_major > 3) return ret;
891 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
892 if (gl_info->quirks & WINED3D_QUIRK_GLSL_CLIP_VARYING) ret -= 1;
896 /** Generate the variable & register declarations for the GLSL output target */
897 static void shader_generate_glsl_declarations(const struct wined3d_context *context,
898 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
899 const struct wined3d_shader_reg_maps *reg_maps, const struct shader_glsl_ctx_priv *ctx_priv)
901 const struct wined3d_shader_version *version = ®_maps->shader_version;
902 const struct wined3d_state *state = &shader->device->stateBlock->state;
903 const struct ps_compile_args *ps_args = ctx_priv->cur_ps_args;
904 const struct wined3d_gl_info *gl_info = context->gl_info;
905 const struct wined3d_fb_state *fb = &shader->device->fb;
906 unsigned int i, extra_constants_needed = 0;
907 const struct wined3d_shader_lconst *lconst;
911 prefix = shader_glsl_get_prefix(version->type);
913 /* Prototype the subroutines */
914 for (i = 0, map = reg_maps->labels; map; map >>= 1, ++i)
916 if (map & 1) shader_addline(buffer, "void subroutine%u();\n", i);
919 /* Declare the constants (aka uniforms) */
920 if (shader->limits.constant_float > 0)
922 unsigned max_constantsF;
924 /* Unless the shader uses indirect addressing, always declare the
925 * maximum array size and ignore that we need some uniforms privately.
926 * E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup
927 * and immediate values, still declare VC[256]. If the shader needs
928 * more uniforms than we have it won't work in any case. If it uses
929 * less, the compiler will figure out which uniforms are really used
930 * and strip them out. This allows a shader to use c255 on a dx9 card,
931 * as long as it doesn't also use all the other constants.
933 * If the shader uses indirect addressing the compiler must assume
934 * that all declared uniforms are used. In this case, declare only the
935 * amount that we're assured to have.
937 * Thus we run into problems in these two cases:
938 * 1) The shader really uses more uniforms than supported.
939 * 2) The shader uses indirect addressing, less constants than
940 * supported, but uses a constant index > #supported consts. */
941 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
943 /* No indirect addressing here. */
944 max_constantsF = gl_info->limits.glsl_ps_float_constants;
948 if (reg_maps->usesrelconstF)
950 /* Subtract the other potential uniforms from the max
951 * available (bools, ints, and 1 row of projection matrix).
952 * Subtract another uniform for immediate values, which have
953 * to be loaded via uniform by the driver as well. The shader
954 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
955 * shader code, so one vec4 should be enough. (Unfortunately
956 * the Nvidia driver doesn't store 128 and -128 in one float).
958 * Writing gl_ClipVertex requires one uniform for each
959 * clipplane as well. */
960 max_constantsF = gl_info->limits.glsl_vs_float_constants - 3;
961 if(ctx_priv->cur_vs_args->clip_enabled)
963 max_constantsF -= gl_info->limits.clipplanes;
965 max_constantsF -= count_bits(reg_maps->integer_constants);
966 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
967 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
968 * for now take this into account when calculating the number of available constants
970 max_constantsF -= count_bits(reg_maps->boolean_constants);
971 /* Set by driver quirks in directx.c */
972 max_constantsF -= gl_info->reserved_glsl_constants;
974 if (max_constantsF < shader->limits.constant_float)
976 static unsigned int once;
979 ERR_(winediag)("The hardware does not support enough uniform components to run this shader,"
980 " it may not render correctly.\n");
982 WARN("The hardware does not support enough uniform components to run this shader.\n");
987 max_constantsF = gl_info->limits.glsl_vs_float_constants;
990 max_constantsF = min(shader->limits.constant_float, max_constantsF);
991 shader_addline(buffer, "uniform vec4 %s_c[%u];\n", prefix, max_constantsF);
994 /* Always declare the full set of constants, the compiler can remove the
995 * unused ones because d3d doesn't (yet) support indirect int and bool
996 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
997 if (shader->limits.constant_int > 0 && reg_maps->integer_constants)
998 shader_addline(buffer, "uniform ivec4 %s_i[%u];\n", prefix, shader->limits.constant_int);
1000 if (shader->limits.constant_bool > 0 && reg_maps->boolean_constants)
1001 shader_addline(buffer, "uniform bool %s_b[%u];\n", prefix, shader->limits.constant_bool);
1003 for (i = 0; i < WINED3D_MAX_CBS; ++i)
1005 if (reg_maps->cb_sizes[i])
1006 shader_addline(buffer, "uniform vec4 %s_cb%u[%u];\n", prefix, i, reg_maps->cb_sizes[i]);
1009 /* Declare texture samplers */
1010 for (i = 0; i < shader->limits.sampler; ++i)
1012 if (reg_maps->sampler_type[i])
1014 BOOL shadow_sampler = version->type == WINED3D_SHADER_TYPE_PIXEL && (ps_args->shadow & (1 << i));
1015 const struct wined3d_texture *texture;
1017 switch (reg_maps->sampler_type[i])
1021 shader_addline(buffer, "uniform sampler1DShadow %s_sampler%u;\n", prefix, i);
1023 shader_addline(buffer, "uniform sampler1D %s_sampler%u;\n", prefix, i);
1026 texture = state->textures[i];
1029 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1030 shader_addline(buffer, "uniform sampler2DRectShadow %s_sampler%u;\n", prefix, i);
1032 shader_addline(buffer, "uniform sampler2DShadow %s_sampler%u;\n", prefix, i);
1036 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
1037 shader_addline(buffer, "uniform sampler2DRect %s_sampler%u;\n", prefix, i);
1039 shader_addline(buffer, "uniform sampler2D %s_sampler%u;\n", prefix, i);
1042 case WINED3DSTT_CUBE:
1044 FIXME("Unsupported Cube shadow sampler.\n");
1045 shader_addline(buffer, "uniform samplerCube %s_sampler%u;\n", prefix, i);
1047 case WINED3DSTT_VOLUME:
1049 FIXME("Unsupported 3D shadow sampler.\n");
1050 shader_addline(buffer, "uniform sampler3D %s_sampler%u;\n", prefix, i);
1053 shader_addline(buffer, "uniform unsupported_sampler %s_sampler%u;\n", prefix, i);
1054 FIXME("Unrecognized sampler type: %#x\n", reg_maps->sampler_type[i]);
1060 /* Declare uniforms for NP2 texcoord fixup:
1061 * This is NOT done inside the loop that declares the texture samplers
1062 * since the NP2 fixup code is currently only used for the GeforceFX
1063 * series and when forcing the ARB_npot extension off. Modern cards just
1064 * skip the code anyway, so put it inside a separate loop. */
1065 if (version->type == WINED3D_SHADER_TYPE_PIXEL && ps_args->np2_fixup)
1067 struct ps_np2fixup_info *fixup = ctx_priv->cur_np2fixup_info;
1070 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1071 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1072 * samplerNP2Fixup stores texture dimensions and is updated through
1073 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1075 for (i = 0; i < shader->limits.sampler; ++i)
1077 if (reg_maps->sampler_type[i])
1079 if (!(ps_args->np2_fixup & (1 << i))) continue;
1081 if (WINED3DSTT_2D != reg_maps->sampler_type[i]) {
1082 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1086 fixup->idx[i] = cur++;
1090 fixup->num_consts = (cur + 1) >> 1;
1091 shader_addline(buffer, "uniform vec4 %s_samplerNP2Fixup[%u];\n", prefix, fixup->num_consts);
1094 /* Declare address variables */
1095 for (i = 0, map = reg_maps->address; map; map >>= 1, ++i)
1097 if (map & 1) shader_addline(buffer, "ivec4 A%u;\n", i);
1100 /* Declare texture coordinate temporaries and initialize them */
1101 for (i = 0, map = reg_maps->texcoord; map; map >>= 1, ++i)
1103 if (map & 1) shader_addline(buffer, "vec4 T%u = gl_TexCoord[%u];\n", i, i);
1106 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1108 /* Declare attributes. */
1109 for (i = 0, map = reg_maps->input_registers; map; map >>= 1, ++i)
1112 shader_addline(buffer, "attribute vec4 %s_in%u;\n", prefix, i);
1115 shader_addline(buffer, "uniform vec4 posFixup;\n");
1116 shader_addline(buffer, "void order_ps_input(in vec4[%u]);\n", shader->limits.packed_output);
1118 else if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1120 if (version->major >= 3)
1122 UINT in_count = min(vec4_varyings(version->major, gl_info), shader->limits.packed_input);
1125 shader_addline(buffer, "varying vec4 %s_in[%u];\n", prefix, in_count);
1127 /* TODO: Write a replacement shader for the fixed function
1128 * vertex pipeline, so this isn't needed. For fixed function
1129 * vertex processing + 3.0 pixel shader we need a separate
1130 * function in the pixel shader that reads the fixed function
1131 * color into the packed input registers. */
1132 shader_addline(buffer, "vec4 %s_in[%u];\n", prefix, in_count);
1135 for (i = 0, map = reg_maps->bumpmat; map; map >>= 1, ++i)
1140 shader_addline(buffer, "uniform mat2 bumpenvmat%d;\n", i);
1142 if (reg_maps->luminanceparams & (1 << i))
1144 shader_addline(buffer, "uniform float luminancescale%d;\n", i);
1145 shader_addline(buffer, "uniform float luminanceoffset%d;\n", i);
1146 extra_constants_needed++;
1149 extra_constants_needed++;
1152 if (ps_args->srgb_correction)
1154 shader_addline(buffer, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1155 srgb_pow, srgb_mul_high, srgb_sub_high, srgb_mul_low);
1156 shader_addline(buffer, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1159 if (reg_maps->vpos || reg_maps->usesdsy)
1161 if (shader->limits.constant_float + extra_constants_needed
1162 + 1 < gl_info->limits.glsl_ps_float_constants)
1164 shader_addline(buffer, "uniform vec4 ycorrection;\n");
1165 extra_constants_needed++;
1169 /* This happens because we do not have proper tracking of the constant registers that are
1170 * actually used, only the max limit of the shader version
1172 FIXME("Cannot find a free uniform for vpos correction params\n");
1173 shader_addline(buffer, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1174 context->render_offscreen ? 0.0f : fb->render_targets[0]->resource.height,
1175 context->render_offscreen ? 1.0f : -1.0f);
1177 shader_addline(buffer, "vec4 vpos;\n");
1181 /* Declare output register temporaries */
1182 if (shader->limits.packed_output)
1183 shader_addline(buffer, "vec4 %s_out[%u];\n", prefix, shader->limits.packed_output);
1185 /* Declare temporary variables */
1186 for (i = 0, map = reg_maps->temporary; map; map >>= 1, ++i)
1188 if (map & 1) shader_addline(buffer, "vec4 R%u;\n", i);
1191 /* Declare loop registers aLx */
1192 if (version->major < 4)
1194 for (i = 0; i < reg_maps->loop_depth; ++i)
1196 shader_addline(buffer, "int aL%u;\n", i);
1197 shader_addline(buffer, "int tmpInt%u;\n", i);
1201 /* Temporary variables for matrix operations */
1202 shader_addline(buffer, "vec4 tmp0;\n");
1203 shader_addline(buffer, "vec4 tmp1;\n");
1205 /* Local constants use a different name so they can be loaded once at shader link time
1206 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1207 * float -> string conversion can cause precision loss.
1209 if (!shader->load_local_constsF)
1211 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
1213 shader_addline(buffer, "uniform vec4 %s_lc%u;\n", prefix, lconst->idx);
1217 /* Start the main program. */
1218 shader_addline(buffer, "void main()\n{\n");
1220 /* Direct3D applications expect integer vPos values, while OpenGL drivers
1221 * add approximately 0.5. This causes off-by-one problems as spotted by
1222 * the vPos d3d9 visual test. Unfortunately ATI cards do not add exactly
1223 * 0.5, but rather something like 0.49999999 or 0.50000001, which still
1224 * causes precision troubles when we just subtract 0.5.
1226 * To deal with that, just floor() the position. This will eliminate the
1227 * fraction on all cards.
1229 * TODO: Test how this behaves with multisampling.
1231 * An advantage of floor is that it works even if the driver doesn't add
1232 * 0.5. It is somewhat questionable if 1.5, 2.5, ... are the proper values
1233 * to return in gl_FragCoord, even though coordinates specify the pixel
1234 * centers instead of the pixel corners. This code will behave correctly
1235 * on drivers that returns integer values. */
1236 if (version->type == WINED3D_SHADER_TYPE_PIXEL && reg_maps->vpos)
1237 shader_addline(buffer,
1238 "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1241 /*****************************************************************************
1242 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1244 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1245 ****************************************************************************/
1248 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1249 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src);
1251 /** Used for opcode modifiers - They multiply the result by the specified amount */
1252 static const char * const shift_glsl_tab[] = {
1254 "2.0 * ", /* 1 (x2) */
1255 "4.0 * ", /* 2 (x4) */
1256 "8.0 * ", /* 3 (x8) */
1257 "16.0 * ", /* 4 (x16) */
1258 "32.0 * ", /* 5 (x32) */
1265 "0.0625 * ", /* 12 (d16) */
1266 "0.125 * ", /* 13 (d8) */
1267 "0.25 * ", /* 14 (d4) */
1268 "0.5 * " /* 15 (d2) */
1271 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1272 static void shader_glsl_gen_modifier(enum wined3d_shader_src_modifier src_modifier,
1273 const char *in_reg, const char *in_regswizzle, char *out_str)
1277 switch (src_modifier)
1279 case WINED3DSPSM_DZ: /* Need to handle this in the instructions itself (texld & texcrd). */
1280 case WINED3DSPSM_DW:
1281 case WINED3DSPSM_NONE:
1282 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1284 case WINED3DSPSM_NEG:
1285 sprintf(out_str, "-%s%s", in_reg, in_regswizzle);
1287 case WINED3DSPSM_NOT:
1288 sprintf(out_str, "!%s%s", in_reg, in_regswizzle);
1290 case WINED3DSPSM_BIAS:
1291 sprintf(out_str, "(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1293 case WINED3DSPSM_BIASNEG:
1294 sprintf(out_str, "-(%s%s - vec4(0.5)%s)", in_reg, in_regswizzle, in_regswizzle);
1296 case WINED3DSPSM_SIGN:
1297 sprintf(out_str, "(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1299 case WINED3DSPSM_SIGNNEG:
1300 sprintf(out_str, "-(2.0 * (%s%s - 0.5))", in_reg, in_regswizzle);
1302 case WINED3DSPSM_COMP:
1303 sprintf(out_str, "(1.0 - %s%s)", in_reg, in_regswizzle);
1305 case WINED3DSPSM_X2:
1306 sprintf(out_str, "(2.0 * %s%s)", in_reg, in_regswizzle);
1308 case WINED3DSPSM_X2NEG:
1309 sprintf(out_str, "-(2.0 * %s%s)", in_reg, in_regswizzle);
1311 case WINED3DSPSM_ABS:
1312 sprintf(out_str, "abs(%s%s)", in_reg, in_regswizzle);
1314 case WINED3DSPSM_ABSNEG:
1315 sprintf(out_str, "-abs(%s%s)", in_reg, in_regswizzle);
1318 FIXME("Unhandled modifier %u\n", src_modifier);
1319 sprintf(out_str, "%s%s", in_reg, in_regswizzle);
1323 /** Writes the GLSL variable name that corresponds to the register that the
1324 * DX opcode parameter is trying to access */
1325 static void shader_glsl_get_register_name(const struct wined3d_shader_register *reg,
1326 char *register_name, BOOL *is_color, const struct wined3d_shader_instruction *ins)
1328 /* oPos, oFog and oPts in D3D */
1329 static const char * const hwrastout_reg_names[] = {"vs_out[10]", "vs_out[11].x", "vs_out[11].y"};
1331 const struct wined3d_shader *shader = ins->ctx->shader;
1332 const struct wined3d_shader_reg_maps *reg_maps = ins->ctx->reg_maps;
1333 const struct wined3d_shader_version *version = ®_maps->shader_version;
1334 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
1335 const char *prefix = shader_glsl_get_prefix(version->type);
1341 case WINED3DSPR_TEMP:
1342 sprintf(register_name, "R%u", reg->idx[0].offset);
1345 case WINED3DSPR_INPUT:
1346 /* vertex shaders */
1347 if (version->type == WINED3D_SHADER_TYPE_VERTEX)
1349 struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
1350 if (priv->cur_vs_args->swizzle_map & (1 << reg->idx[0].offset))
1352 sprintf(register_name, "%s_in%u", prefix, reg->idx[0].offset);
1356 /* pixel shaders >= 3.0 */
1357 if (version->major >= 3)
1359 DWORD idx = shader->u.ps.input_reg_map[reg->idx[0].offset];
1360 unsigned int in_count = vec4_varyings(version->major, gl_info);
1362 if (reg->idx[0].rel_addr)
1364 struct glsl_src_param rel_param;
1366 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1368 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1369 * operation there */
1372 if (shader->u.ps.declared_in_count > in_count)
1374 sprintf(register_name,
1375 "((%s + %u) > %u ? (%s + %u) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s + %u])",
1376 rel_param.param_str, idx, in_count - 1, rel_param.param_str, idx, in_count,
1377 prefix, rel_param.param_str, idx);
1381 sprintf(register_name, "%s_in[%s + %u]", prefix, rel_param.param_str, idx);
1386 if (shader->u.ps.declared_in_count > in_count)
1388 sprintf(register_name, "((%s) > %u ? (%s) > %u ? gl_SecondaryColor : gl_Color : %s_in[%s])",
1389 rel_param.param_str, in_count - 1, rel_param.param_str, in_count,
1390 prefix, rel_param.param_str);
1394 sprintf(register_name, "%s_in[%s]", prefix, rel_param.param_str);
1400 if (idx == in_count) sprintf(register_name, "gl_Color");
1401 else if (idx == in_count + 1) sprintf(register_name, "gl_SecondaryColor");
1402 else sprintf(register_name, "%s_in[%u]", prefix, idx);
1407 if (!reg->idx[0].offset)
1408 strcpy(register_name, "gl_Color");
1410 strcpy(register_name, "gl_SecondaryColor");
1415 case WINED3DSPR_CONST:
1417 /* Relative addressing */
1418 if (reg->idx[0].rel_addr)
1420 struct glsl_src_param rel_param;
1421 shader_glsl_add_src_param(ins, reg->idx[0].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1422 if (reg->idx[0].offset)
1423 sprintf(register_name, "%s_c[%s + %u]", prefix, rel_param.param_str, reg->idx[0].offset);
1425 sprintf(register_name, "%s_c[%s]", prefix, rel_param.param_str);
1429 if (shader_constant_is_local(shader, reg->idx[0].offset))
1430 sprintf(register_name, "%s_lc%u", prefix, reg->idx[0].offset);
1432 sprintf(register_name, "%s_c[%u]", prefix, reg->idx[0].offset);
1437 case WINED3DSPR_CONSTINT:
1438 sprintf(register_name, "%s_i[%u]", prefix, reg->idx[0].offset);
1441 case WINED3DSPR_CONSTBOOL:
1442 sprintf(register_name, "%s_b[%u]", prefix, reg->idx[0].offset);
1445 case WINED3DSPR_TEXTURE: /* case WINED3DSPR_ADDR: */
1446 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
1447 sprintf(register_name, "T%u", reg->idx[0].offset);
1449 sprintf(register_name, "A%u", reg->idx[0].offset);
1452 case WINED3DSPR_LOOP:
1453 sprintf(register_name, "aL%u", ins->ctx->loop_state->current_reg - 1);
1456 case WINED3DSPR_SAMPLER:
1457 sprintf(register_name, "%s_sampler%u", prefix, reg->idx[0].offset);
1460 case WINED3DSPR_COLOROUT:
1461 if (reg->idx[0].offset >= gl_info->limits.buffers)
1462 WARN("Write to render target %u, only %d supported.\n",
1463 reg->idx[0].offset, gl_info->limits.buffers);
1465 sprintf(register_name, "gl_FragData[%u]", reg->idx[0].offset);
1468 case WINED3DSPR_RASTOUT:
1469 sprintf(register_name, "%s", hwrastout_reg_names[reg->idx[0].offset]);
1472 case WINED3DSPR_DEPTHOUT:
1473 sprintf(register_name, "gl_FragDepth");
1476 case WINED3DSPR_ATTROUT:
1477 if (!reg->idx[0].offset)
1478 sprintf(register_name, "%s_out[8]", prefix);
1480 sprintf(register_name, "%s_out[9]", prefix);
1483 case WINED3DSPR_TEXCRDOUT:
1484 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1485 sprintf(register_name, "%s_out[%u]", prefix, reg->idx[0].offset);
1488 case WINED3DSPR_MISCTYPE:
1489 if (!reg->idx[0].offset)
1492 sprintf(register_name, "vpos");
1494 else if (reg->idx[0].offset == 1)
1496 /* Note that gl_FrontFacing is a bool, while vFace is
1497 * a float for which the sign determines front/back */
1498 sprintf(register_name, "(gl_FrontFacing ? 1.0 : -1.0)");
1502 FIXME("Unhandled misctype register %u.\n", reg->idx[0].offset);
1503 sprintf(register_name, "unrecognized_register");
1507 case WINED3DSPR_IMMCONST:
1508 switch (reg->immconst_type)
1510 case WINED3D_IMMCONST_SCALAR:
1511 switch (reg->data_type)
1513 case WINED3D_DATA_FLOAT:
1514 sprintf(register_name, "%.8e", *(const float *)reg->immconst_data);
1516 case WINED3D_DATA_INT:
1517 sprintf(register_name, "%#x", reg->immconst_data[0]);
1519 case WINED3D_DATA_RESOURCE:
1520 case WINED3D_DATA_SAMPLER:
1521 case WINED3D_DATA_UINT:
1522 sprintf(register_name, "%#xu", reg->immconst_data[0]);
1525 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1530 case WINED3D_IMMCONST_VEC4:
1531 switch (reg->data_type)
1533 case WINED3D_DATA_FLOAT:
1534 sprintf(register_name, "vec4(%.8e, %.8e, %.8e, %.8e)",
1535 *(const float *)®->immconst_data[0], *(const float *)®->immconst_data[1],
1536 *(const float *)®->immconst_data[2], *(const float *)®->immconst_data[3]);
1538 case WINED3D_DATA_INT:
1539 sprintf(register_name, "ivec4(%#x, %#x, %#x, %#x)",
1540 reg->immconst_data[0], reg->immconst_data[1],
1541 reg->immconst_data[2], reg->immconst_data[3]);
1543 case WINED3D_DATA_RESOURCE:
1544 case WINED3D_DATA_SAMPLER:
1545 case WINED3D_DATA_UINT:
1546 sprintf(register_name, "uvec4(%#xu, %#xu, %#xu, %#xu)",
1547 reg->immconst_data[0], reg->immconst_data[1],
1548 reg->immconst_data[2], reg->immconst_data[3]);
1551 sprintf(register_name, "<unhandled data type %#x>", reg->data_type);
1557 FIXME("Unhandled immconst type %#x\n", reg->immconst_type);
1558 sprintf(register_name, "<unhandled_immconst_type %#x>", reg->immconst_type);
1562 case WINED3DSPR_CONSTBUFFER:
1563 if (reg->idx[1].rel_addr)
1565 struct glsl_src_param rel_param;
1566 shader_glsl_add_src_param(ins, reg->idx[1].rel_addr, WINED3DSP_WRITEMASK_0, &rel_param);
1567 sprintf(register_name, "%s_cb%u[%s + %u]",
1568 prefix, reg->idx[0].offset, rel_param.param_str, reg->idx[1].offset);
1572 sprintf(register_name, "%s_cb%u[%u]", prefix, reg->idx[0].offset, reg->idx[1].offset);
1576 case WINED3DSPR_PRIMID:
1577 sprintf(register_name, "uint(gl_PrimitiveIDIn)");
1581 FIXME("Unhandled register type %#x.\n", reg->type);
1582 sprintf(register_name, "unrecognized_register");
1587 static void shader_glsl_write_mask_to_str(DWORD write_mask, char *str)
1590 if (write_mask & WINED3DSP_WRITEMASK_0) *str++ = 'x';
1591 if (write_mask & WINED3DSP_WRITEMASK_1) *str++ = 'y';
1592 if (write_mask & WINED3DSP_WRITEMASK_2) *str++ = 'z';
1593 if (write_mask & WINED3DSP_WRITEMASK_3) *str++ = 'w';
1597 /* Get the GLSL write mask for the destination register */
1598 static DWORD shader_glsl_get_write_mask(const struct wined3d_shader_dst_param *param, char *write_mask)
1600 DWORD mask = param->write_mask;
1602 if (shader_is_scalar(¶m->reg))
1604 mask = WINED3DSP_WRITEMASK_0;
1609 shader_glsl_write_mask_to_str(mask, write_mask);
1615 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask) {
1616 unsigned int size = 0;
1618 if (write_mask & WINED3DSP_WRITEMASK_0) ++size;
1619 if (write_mask & WINED3DSP_WRITEMASK_1) ++size;
1620 if (write_mask & WINED3DSP_WRITEMASK_2) ++size;
1621 if (write_mask & WINED3DSP_WRITEMASK_3) ++size;
1626 static void shader_glsl_swizzle_to_str(const DWORD swizzle, BOOL fixup, DWORD mask, char *str)
1628 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1629 * but addressed as "rgba". To fix this we need to swap the register's x
1630 * and z components. */
1631 const char *swizzle_chars = fixup ? "zyxw" : "xyzw";
1634 /* swizzle bits fields: wwzzyyxx */
1635 if (mask & WINED3DSP_WRITEMASK_0) *str++ = swizzle_chars[swizzle & 0x03];
1636 if (mask & WINED3DSP_WRITEMASK_1) *str++ = swizzle_chars[(swizzle >> 2) & 0x03];
1637 if (mask & WINED3DSP_WRITEMASK_2) *str++ = swizzle_chars[(swizzle >> 4) & 0x03];
1638 if (mask & WINED3DSP_WRITEMASK_3) *str++ = swizzle_chars[(swizzle >> 6) & 0x03];
1642 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param *param,
1643 BOOL fixup, DWORD mask, char *swizzle_str)
1645 if (shader_is_scalar(¶m->reg))
1646 *swizzle_str = '\0';
1648 shader_glsl_swizzle_to_str(param->swizzle, fixup, mask, swizzle_str);
1651 /* From a given parameter token, generate the corresponding GLSL string.
1652 * Also, return the actual register name and swizzle in case the
1653 * caller needs this information as well. */
1654 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction *ins,
1655 const struct wined3d_shader_src_param *wined3d_src, DWORD mask, struct glsl_src_param *glsl_src)
1657 BOOL is_color = FALSE;
1658 char swizzle_str[6];
1660 glsl_src->reg_name[0] = '\0';
1661 glsl_src->param_str[0] = '\0';
1662 swizzle_str[0] = '\0';
1664 shader_glsl_get_register_name(&wined3d_src->reg, glsl_src->reg_name, &is_color, ins);
1665 shader_glsl_get_swizzle(wined3d_src, is_color, mask, swizzle_str);
1667 if (wined3d_src->reg.type == WINED3DSPR_IMMCONST || wined3d_src->reg.type == WINED3DSPR_PRIMID)
1669 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, glsl_src->param_str);
1673 char param_str[200];
1675 shader_glsl_gen_modifier(wined3d_src->modifiers, glsl_src->reg_name, swizzle_str, param_str);
1677 switch (wined3d_src->reg.data_type)
1679 case WINED3D_DATA_FLOAT:
1680 sprintf(glsl_src->param_str, "%s", param_str);
1682 case WINED3D_DATA_INT:
1683 sprintf(glsl_src->param_str, "floatBitsToInt(%s)", param_str);
1685 case WINED3D_DATA_RESOURCE:
1686 case WINED3D_DATA_SAMPLER:
1687 case WINED3D_DATA_UINT:
1688 sprintf(glsl_src->param_str, "floatBitsToUint(%s)", param_str);
1691 FIXME("Unhandled data type %#x.\n", wined3d_src->reg.data_type);
1692 sprintf(glsl_src->param_str, "%s", param_str);
1698 /* From a given parameter token, generate the corresponding GLSL string.
1699 * Also, return the actual register name and swizzle in case the
1700 * caller needs this information as well. */
1701 static DWORD shader_glsl_add_dst_param(const struct wined3d_shader_instruction *ins,
1702 const struct wined3d_shader_dst_param *wined3d_dst, struct glsl_dst_param *glsl_dst)
1704 BOOL is_color = FALSE;
1706 glsl_dst->mask_str[0] = '\0';
1707 glsl_dst->reg_name[0] = '\0';
1709 shader_glsl_get_register_name(&wined3d_dst->reg, glsl_dst->reg_name, &is_color, ins);
1710 return shader_glsl_get_write_mask(wined3d_dst, glsl_dst->mask_str);
1713 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1714 static DWORD shader_glsl_append_dst_ext(struct wined3d_shader_buffer *buffer,
1715 const struct wined3d_shader_instruction *ins, const struct wined3d_shader_dst_param *dst)
1717 struct glsl_dst_param glsl_dst;
1720 if ((mask = shader_glsl_add_dst_param(ins, dst, &glsl_dst)))
1722 switch (dst->reg.data_type)
1724 case WINED3D_DATA_FLOAT:
1725 shader_addline(buffer, "%s%s = %s(",
1726 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1728 case WINED3D_DATA_INT:
1729 shader_addline(buffer, "%s%s = %sintBitsToFloat(",
1730 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1732 case WINED3D_DATA_RESOURCE:
1733 case WINED3D_DATA_SAMPLER:
1734 case WINED3D_DATA_UINT:
1735 shader_addline(buffer, "%s%s = %suintBitsToFloat(",
1736 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1739 FIXME("Unhandled data type %#x.\n", dst->reg.data_type);
1740 shader_addline(buffer, "%s%s = %s(",
1741 glsl_dst.reg_name, glsl_dst.mask_str, shift_glsl_tab[dst->shift]);
1749 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1750 static DWORD shader_glsl_append_dst(struct wined3d_shader_buffer *buffer, const struct wined3d_shader_instruction *ins)
1752 return shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
1755 /** Process GLSL instruction modifiers */
1756 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction *ins)
1758 struct glsl_dst_param dst_param;
1761 if (!ins->dst_count) return;
1763 modifiers = ins->dst[0].modifiers;
1764 if (!modifiers) return;
1766 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
1768 if (modifiers & WINED3DSPDM_SATURATE)
1770 /* _SAT means to clamp the value of the register to between 0 and 1 */
1771 shader_addline(ins->ctx->buffer, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param.reg_name,
1772 dst_param.mask_str, dst_param.reg_name, dst_param.mask_str);
1775 if (modifiers & WINED3DSPDM_MSAMPCENTROID)
1777 FIXME("_centroid modifier not handled\n");
1780 if (modifiers & WINED3DSPDM_PARTIALPRECISION)
1782 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1786 static const char *shader_glsl_get_rel_op(enum wined3d_shader_rel_op op)
1790 case WINED3D_SHADER_REL_OP_GT: return ">";
1791 case WINED3D_SHADER_REL_OP_EQ: return "==";
1792 case WINED3D_SHADER_REL_OP_GE: return ">=";
1793 case WINED3D_SHADER_REL_OP_LT: return "<";
1794 case WINED3D_SHADER_REL_OP_NE: return "!=";
1795 case WINED3D_SHADER_REL_OP_LE: return "<=";
1797 FIXME("Unrecognized operator %#x.\n", op);
1802 static void shader_glsl_get_sample_function(const struct wined3d_shader_context *ctx,
1803 DWORD sampler_idx, DWORD flags, struct glsl_sample_function *sample_function)
1805 enum wined3d_sampler_texture_type sampler_type = ctx->reg_maps->sampler_type[sampler_idx];
1806 const struct wined3d_gl_info *gl_info = ctx->gl_info;
1807 BOOL shadow = ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL
1808 && (((const struct shader_glsl_ctx_priv *)ctx->backend_data)->cur_ps_args->shadow & (1 << sampler_idx));
1809 BOOL projected = flags & WINED3D_GLSL_SAMPLE_PROJECTED;
1810 BOOL texrect = flags & WINED3D_GLSL_SAMPLE_RECT;
1811 BOOL lod = flags & WINED3D_GLSL_SAMPLE_LOD;
1812 BOOL grad = flags & WINED3D_GLSL_SAMPLE_GRAD;
1814 /* Note that there's no such thing as a projected cube texture. */
1815 switch(sampler_type) {
1821 sample_function->name = projected ? "shadow1DProjLod" : "shadow1DLod";
1825 if (gl_info->supported[EXT_GPU_SHADER4])
1826 sample_function->name = projected ? "shadow1DProjGrad" : "shadow1DGrad";
1827 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1828 sample_function->name = projected ? "shadow1DProjGradARB" : "shadow1DGradARB";
1831 FIXME("Unsupported 1D shadow grad function.\n");
1832 sample_function->name = "unsupported1DGrad";
1837 sample_function->name = projected ? "shadow1DProj" : "shadow1D";
1839 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1845 sample_function->name = projected ? "texture1DProjLod" : "texture1DLod";
1849 if (gl_info->supported[EXT_GPU_SHADER4])
1850 sample_function->name = projected ? "texture1DProjGrad" : "texture1DGrad";
1851 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1852 sample_function->name = projected ? "texture1DProjGradARB" : "texture1DGradARB";
1855 FIXME("Unsupported 1D grad function.\n");
1856 sample_function->name = "unsupported1DGrad";
1861 sample_function->name = projected ? "texture1DProj" : "texture1D";
1863 sample_function->coord_mask = WINED3DSP_WRITEMASK_0;
1874 sample_function->name = projected ? "shadow2DRectProjLod" : "shadow2DRectLod";
1878 if (gl_info->supported[EXT_GPU_SHADER4])
1879 sample_function->name = projected ? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1880 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1881 sample_function->name = projected ? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1884 FIXME("Unsupported RECT shadow grad function.\n");
1885 sample_function->name = "unsupported2DRectGrad";
1890 sample_function->name = projected ? "shadow2DRectProj" : "shadow2DRect";
1897 sample_function->name = projected ? "shadow2DProjLod" : "shadow2DLod";
1901 if (gl_info->supported[EXT_GPU_SHADER4])
1902 sample_function->name = projected ? "shadow2DProjGrad" : "shadow2DGrad";
1903 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1904 sample_function->name = projected ? "shadow2DProjGradARB" : "shadow2DGradARB";
1907 FIXME("Unsupported 2D shadow grad function.\n");
1908 sample_function->name = "unsupported2DGrad";
1913 sample_function->name = projected ? "shadow2DProj" : "shadow2D";
1916 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
1924 sample_function->name = projected ? "texture2DRectProjLod" : "texture2DRectLod";
1928 if (gl_info->supported[EXT_GPU_SHADER4])
1929 sample_function->name = projected ? "texture2DRectProjGrad" : "texture2DRectGrad";
1930 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1931 sample_function->name = projected ? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1934 FIXME("Unsupported RECT grad function.\n");
1935 sample_function->name = "unsupported2DRectGrad";
1940 sample_function->name = projected ? "texture2DRectProj" : "texture2DRect";
1947 sample_function->name = projected ? "texture2DProjLod" : "texture2DLod";
1951 if (gl_info->supported[EXT_GPU_SHADER4])
1952 sample_function->name = projected ? "texture2DProjGrad" : "texture2DGrad";
1953 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1954 sample_function->name = projected ? "texture2DProjGradARB" : "texture2DGradARB";
1957 FIXME("Unsupported 2D grad function.\n");
1958 sample_function->name = "unsupported2DGrad";
1963 sample_function->name = projected ? "texture2DProj" : "texture2D";
1966 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1;
1970 case WINED3DSTT_CUBE:
1973 FIXME("Unsupported Cube shadow function.\n");
1974 sample_function->name = "unsupportedCubeShadow";
1975 sample_function->coord_mask = 0;
1981 sample_function->name = "textureCubeLod";
1985 if (gl_info->supported[EXT_GPU_SHADER4])
1986 sample_function->name = "textureCubeGrad";
1987 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
1988 sample_function->name = "textureCubeGradARB";
1991 FIXME("Unsupported Cube grad function.\n");
1992 sample_function->name = "unsupportedCubeGrad";
1997 sample_function->name = "textureCube";
1999 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2003 case WINED3DSTT_VOLUME:
2006 FIXME("Unsupported 3D shadow function.\n");
2007 sample_function->name = "unsupported3DShadow";
2008 sample_function->coord_mask = 0;
2014 sample_function->name = projected ? "texture3DProjLod" : "texture3DLod";
2018 if (gl_info->supported[EXT_GPU_SHADER4])
2019 sample_function->name = projected ? "texture3DProjGrad" : "texture3DGrad";
2020 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
2021 sample_function->name = projected ? "texture3DProjGradARB" : "texture3DGradARB";
2024 FIXME("Unsupported 3D grad function.\n");
2025 sample_function->name = "unsupported3DGrad";
2030 sample_function->name = projected ? "texture3DProj" : "texture3D";
2032 sample_function->coord_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2037 sample_function->name = "";
2038 sample_function->coord_mask = 0;
2039 FIXME("Unrecognized sampler type: %#x;\n", sampler_type);
2044 static void shader_glsl_append_fixup_arg(char *arguments, const char *reg_name,
2045 BOOL sign_fixup, enum fixup_channel_source channel_source)
2047 switch(channel_source)
2049 case CHANNEL_SOURCE_ZERO:
2050 strcat(arguments, "0.0");
2053 case CHANNEL_SOURCE_ONE:
2054 strcat(arguments, "1.0");
2057 case CHANNEL_SOURCE_X:
2058 strcat(arguments, reg_name);
2059 strcat(arguments, ".x");
2062 case CHANNEL_SOURCE_Y:
2063 strcat(arguments, reg_name);
2064 strcat(arguments, ".y");
2067 case CHANNEL_SOURCE_Z:
2068 strcat(arguments, reg_name);
2069 strcat(arguments, ".z");
2072 case CHANNEL_SOURCE_W:
2073 strcat(arguments, reg_name);
2074 strcat(arguments, ".w");
2078 FIXME("Unhandled channel source %#x\n", channel_source);
2079 strcat(arguments, "undefined");
2083 if (sign_fixup) strcat(arguments, " * 2.0 - 1.0");
2086 static void shader_glsl_color_correction(const struct wined3d_shader_instruction *ins, struct color_fixup_desc fixup)
2088 struct wined3d_shader_dst_param dst;
2089 unsigned int mask_size, remaining;
2090 struct glsl_dst_param dst_param;
2091 char arguments[256];
2095 if (fixup.x_sign_fixup || fixup.x_source != CHANNEL_SOURCE_X) mask |= WINED3DSP_WRITEMASK_0;
2096 if (fixup.y_sign_fixup || fixup.y_source != CHANNEL_SOURCE_Y) mask |= WINED3DSP_WRITEMASK_1;
2097 if (fixup.z_sign_fixup || fixup.z_source != CHANNEL_SOURCE_Z) mask |= WINED3DSP_WRITEMASK_2;
2098 if (fixup.w_sign_fixup || fixup.w_source != CHANNEL_SOURCE_W) mask |= WINED3DSP_WRITEMASK_3;
2099 mask &= ins->dst[0].write_mask;
2101 if (!mask) return; /* Nothing to do */
2103 if (is_complex_fixup(fixup))
2105 enum complex_fixup complex_fixup = get_complex_fixup(fixup);
2106 FIXME("Complex fixup (%#x) not supported\n",complex_fixup);
2110 mask_size = shader_glsl_get_write_mask_size(mask);
2113 dst.write_mask = mask;
2114 shader_glsl_add_dst_param(ins, &dst, &dst_param);
2116 arguments[0] = '\0';
2117 remaining = mask_size;
2118 if (mask & WINED3DSP_WRITEMASK_0)
2120 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.x_sign_fixup, fixup.x_source);
2121 if (--remaining) strcat(arguments, ", ");
2123 if (mask & WINED3DSP_WRITEMASK_1)
2125 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.y_sign_fixup, fixup.y_source);
2126 if (--remaining) strcat(arguments, ", ");
2128 if (mask & WINED3DSP_WRITEMASK_2)
2130 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.z_sign_fixup, fixup.z_source);
2131 if (--remaining) strcat(arguments, ", ");
2133 if (mask & WINED3DSP_WRITEMASK_3)
2135 shader_glsl_append_fixup_arg(arguments, dst_param.reg_name, fixup.w_sign_fixup, fixup.w_source);
2136 if (--remaining) strcat(arguments, ", ");
2141 shader_addline(ins->ctx->buffer, "%s%s = vec%u(%s);\n",
2142 dst_param.reg_name, dst_param.mask_str, mask_size, arguments);
2146 shader_addline(ins->ctx->buffer, "%s%s = %s;\n", dst_param.reg_name, dst_param.mask_str, arguments);
2150 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction *ins,
2151 DWORD sampler, const struct glsl_sample_function *sample_function, DWORD swizzle,
2152 const char *dx, const char *dy, const char *bias, const char *coord_reg_fmt, ...)
2154 const struct wined3d_shader_version *version = &ins->ctx->reg_maps->shader_version;
2155 char dst_swizzle[6];
2156 struct color_fixup_desc fixup;
2157 BOOL np2_fixup = FALSE;
2160 shader_glsl_swizzle_to_str(swizzle, FALSE, ins->dst[0].write_mask, dst_swizzle);
2162 if (version->type == WINED3D_SHADER_TYPE_PIXEL)
2164 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2165 fixup = priv->cur_ps_args->color_fixup[sampler];
2167 if(priv->cur_ps_args->np2_fixup & (1 << sampler)) {
2169 FIXME("Biased sampling from NP2 textures is unsupported\n");
2177 fixup = COLOR_FIXUP_IDENTITY; /* FIXME: Vshader color fixup */
2180 shader_glsl_append_dst(ins->ctx->buffer, ins);
2182 shader_addline(ins->ctx->buffer, "%s(%s_sampler%u, ",
2183 sample_function->name, shader_glsl_get_prefix(version->type), sampler);
2185 va_start(args, coord_reg_fmt);
2186 shader_vaddline(ins->ctx->buffer, coord_reg_fmt, args);
2190 shader_addline(ins->ctx->buffer, ", %s)%s);\n", bias, dst_swizzle);
2193 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
2194 const unsigned char idx = priv->cur_np2fixup_info->idx[sampler];
2196 shader_addline(ins->ctx->buffer, " * ps_samplerNP2Fixup[%u].%s)%s);\n", idx >> 1,
2197 (idx % 2) ? "zw" : "xy", dst_swizzle);
2198 } else if(dx && dy) {
2199 shader_addline(ins->ctx->buffer, ", %s, %s)%s);\n", dx, dy, dst_swizzle);
2201 shader_addline(ins->ctx->buffer, ")%s);\n", dst_swizzle);
2205 if(!is_identity_fixup(fixup)) {
2206 shader_glsl_color_correction(ins, fixup);
2210 /*****************************************************************************
2211 * Begin processing individual instruction opcodes
2212 ****************************************************************************/
2214 static void shader_glsl_binop(const struct wined3d_shader_instruction *ins)
2216 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2217 struct glsl_src_param src0_param;
2218 struct glsl_src_param src1_param;
2222 /* Determine the GLSL operator to use based on the opcode */
2223 switch (ins->handler_idx)
2225 case WINED3DSIH_ADD: op = "+"; break;
2226 case WINED3DSIH_AND: op = "&"; break;
2227 case WINED3DSIH_DIV: op = "/"; break;
2228 case WINED3DSIH_IADD: op = "+"; break;
2229 case WINED3DSIH_MUL: op = "*"; break;
2230 case WINED3DSIH_SUB: op = "-"; break;
2231 case WINED3DSIH_USHR: op = ">>"; break;
2232 case WINED3DSIH_XOR: op = "^"; break;
2234 op = "<unhandled operator>";
2235 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2239 write_mask = shader_glsl_append_dst(buffer, ins);
2240 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2241 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2242 shader_addline(buffer, "%s %s %s);\n", src0_param.param_str, op, src1_param.param_str);
2245 static void shader_glsl_relop(const struct wined3d_shader_instruction *ins)
2247 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2248 struct glsl_src_param src0_param;
2249 struct glsl_src_param src1_param;
2250 unsigned int mask_size;
2254 write_mask = shader_glsl_append_dst(buffer, ins);
2255 mask_size = shader_glsl_get_write_mask_size(write_mask);
2256 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2257 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2261 switch (ins->handler_idx)
2263 case WINED3DSIH_EQ: op = "equal"; break;
2264 case WINED3DSIH_GE: op = "greaterThanEqual"; break;
2265 case WINED3DSIH_IGE: op = "greaterThanEqual"; break;
2266 case WINED3DSIH_LT: op = "lessThan"; break;
2268 op = "<unhandled operator>";
2269 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2273 shader_addline(buffer, "uvec%u(%s(%s, %s)) * 0xffffffffu);\n",
2274 mask_size, op, src0_param.param_str, src1_param.param_str);
2278 switch (ins->handler_idx)
2280 case WINED3DSIH_EQ: op = "=="; break;
2281 case WINED3DSIH_GE: op = ">="; break;
2282 case WINED3DSIH_IGE: op = ">="; break;
2283 case WINED3DSIH_LT: op = "<"; break;
2285 op = "<unhandled operator>";
2286 ERR("Unhandled opcode %#x.\n", ins->handler_idx);
2290 shader_addline(buffer, "%s %s %s ? 0xffffffffu : 0u);\n",
2291 src0_param.param_str, op, src1_param.param_str);
2295 static void shader_glsl_imul(const struct wined3d_shader_instruction *ins)
2297 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2298 struct glsl_src_param src0_param;
2299 struct glsl_src_param src1_param;
2302 /* If we have ARB_gpu_shader5 or GLSL 4.0, we can use imulExtended(). If
2303 * not, we can emulate it. */
2304 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2305 FIXME("64-bit integer multiplies not implemented.\n");
2307 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2309 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2310 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2311 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2313 shader_addline(ins->ctx->buffer, "%s * %s);\n",
2314 src0_param.param_str, src1_param.param_str);
2318 static void shader_glsl_udiv(const struct wined3d_shader_instruction *ins)
2320 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2321 struct glsl_src_param src0_param, src1_param;
2324 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
2327 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2331 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2332 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2333 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2334 shader_addline(buffer, "tmp0%s = %s / %s;\n",
2335 dst_mask, src0_param.param_str, src1_param.param_str);
2337 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2338 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2339 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2340 shader_addline(buffer, "%s %% %s));\n", src0_param.param_str, src1_param.param_str);
2342 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2343 shader_addline(buffer, "tmp0%s);\n", dst_mask);
2347 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
2348 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2349 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2350 shader_addline(buffer, "%s / %s);\n", src0_param.param_str, src1_param.param_str);
2353 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
2355 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
2356 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2357 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2358 shader_addline(buffer, "%s %% %s);\n", src0_param.param_str, src1_param.param_str);
2362 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2363 static void shader_glsl_mov(const struct wined3d_shader_instruction *ins)
2365 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
2366 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2367 struct glsl_src_param src0_param;
2370 write_mask = shader_glsl_append_dst(buffer, ins);
2371 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2373 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2374 * shader versions WINED3DSIO_MOVA is used for this. */
2375 if (ins->ctx->reg_maps->shader_version.major == 1
2376 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_VERTEX
2377 && ins->dst[0].reg.type == WINED3DSPR_ADDR)
2379 /* This is a simple floor() */
2380 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2381 if (mask_size > 1) {
2382 shader_addline(buffer, "ivec%d(floor(%s)));\n", mask_size, src0_param.param_str);
2384 shader_addline(buffer, "int(floor(%s)));\n", src0_param.param_str);
2387 else if(ins->handler_idx == WINED3DSIH_MOVA)
2389 /* We need to *round* to the nearest int here. */
2390 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
2392 if (gl_info->supported[EXT_GPU_SHADER4])
2395 shader_addline(buffer, "ivec%d(round(%s)));\n", mask_size, src0_param.param_str);
2397 shader_addline(buffer, "int(round(%s)));\n", src0_param.param_str);
2402 shader_addline(buffer, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2403 mask_size, src0_param.param_str, mask_size, src0_param.param_str);
2405 shader_addline(buffer, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2406 src0_param.param_str, src0_param.param_str);
2411 shader_addline(buffer, "%s);\n", src0_param.param_str);
2415 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2416 static void shader_glsl_dot(const struct wined3d_shader_instruction *ins)
2418 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2419 struct glsl_src_param src0_param;
2420 struct glsl_src_param src1_param;
2421 DWORD dst_write_mask, src_write_mask;
2422 unsigned int dst_size = 0;
2424 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2425 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2427 /* dp3 works on vec3, dp4 on vec4 */
2428 if (ins->handler_idx == WINED3DSIH_DP4)
2430 src_write_mask = WINED3DSP_WRITEMASK_ALL;
2432 src_write_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2435 shader_glsl_add_src_param(ins, &ins->src[0], src_write_mask, &src0_param);
2436 shader_glsl_add_src_param(ins, &ins->src[1], src_write_mask, &src1_param);
2439 shader_addline(buffer, "vec%d(dot(%s, %s)));\n", dst_size, src0_param.param_str, src1_param.param_str);
2441 shader_addline(buffer, "dot(%s, %s));\n", src0_param.param_str, src1_param.param_str);
2445 /* Note that this instruction has some restrictions. The destination write mask
2446 * can't contain the w component, and the source swizzles have to be .xyzw */
2447 static void shader_glsl_cross(const struct wined3d_shader_instruction *ins)
2449 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
2450 struct glsl_src_param src0_param;
2451 struct glsl_src_param src1_param;
2454 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2455 shader_glsl_append_dst(ins->ctx->buffer, ins);
2456 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
2457 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
2458 shader_addline(ins->ctx->buffer, "cross(%s, %s)%s);\n", src0_param.param_str, src1_param.param_str, dst_mask);
2461 static void shader_glsl_cut(const struct wined3d_shader_instruction *ins)
2463 shader_addline(ins->ctx->buffer, "EndPrimitive();\n");
2466 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2467 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2468 * GLSL uses the value as-is. */
2469 static void shader_glsl_pow(const struct wined3d_shader_instruction *ins)
2471 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2472 struct glsl_src_param src0_param;
2473 struct glsl_src_param src1_param;
2474 DWORD dst_write_mask;
2475 unsigned int dst_size;
2477 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2478 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2480 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2481 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
2485 shader_addline(buffer, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2486 dst_size, src1_param.param_str, src0_param.param_str, src1_param.param_str);
2490 shader_addline(buffer, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2491 src1_param.param_str, src0_param.param_str, src1_param.param_str);
2495 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2496 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2497 * GLSL uses the value as-is. */
2498 static void shader_glsl_log(const struct wined3d_shader_instruction *ins)
2500 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2501 struct glsl_src_param src0_param;
2502 DWORD dst_write_mask;
2503 unsigned int dst_size;
2505 dst_write_mask = shader_glsl_append_dst(buffer, ins);
2506 dst_size = shader_glsl_get_write_mask_size(dst_write_mask);
2508 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2512 shader_addline(buffer, "vec%u(log2(abs(%s))));\n",
2513 dst_size, src0_param.param_str);
2517 shader_addline(buffer, "log2(abs(%s)));\n",
2518 src0_param.param_str);
2522 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2523 static void shader_glsl_map2gl(const struct wined3d_shader_instruction *ins)
2525 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2526 struct glsl_src_param src_param;
2527 const char *instruction;
2531 /* Determine the GLSL function to use based on the opcode */
2532 /* TODO: Possibly make this a table for faster lookups */
2533 switch (ins->handler_idx)
2535 case WINED3DSIH_MIN: instruction = "min"; break;
2536 case WINED3DSIH_MAX: instruction = "max"; break;
2537 case WINED3DSIH_ABS: instruction = "abs"; break;
2538 case WINED3DSIH_FRC: instruction = "fract"; break;
2539 case WINED3DSIH_EXP: instruction = "exp2"; break;
2540 case WINED3DSIH_DSX: instruction = "dFdx"; break;
2541 case WINED3DSIH_DSY: instruction = "ycorrection.y * dFdy"; break;
2542 case WINED3DSIH_ROUND_NI: instruction = "floor"; break;
2543 default: instruction = "";
2544 FIXME("Opcode %#x not yet handled in GLSL\n", ins->handler_idx);
2548 write_mask = shader_glsl_append_dst(buffer, ins);
2550 shader_addline(buffer, "%s(", instruction);
2554 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2555 shader_addline(buffer, "%s", src_param.param_str);
2556 for (i = 1; i < ins->src_count; ++i)
2558 shader_glsl_add_src_param(ins, &ins->src[i], write_mask, &src_param);
2559 shader_addline(buffer, ", %s", src_param.param_str);
2563 shader_addline(buffer, "));\n");
2566 static void shader_glsl_nop(const struct wined3d_shader_instruction *ins) {}
2568 static void shader_glsl_nrm(const struct wined3d_shader_instruction *ins)
2570 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2571 struct glsl_src_param src_param;
2572 unsigned int mask_size;
2576 write_mask = shader_glsl_get_write_mask(ins->dst, dst_mask);
2577 mask_size = shader_glsl_get_write_mask_size(write_mask);
2578 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2580 shader_addline(buffer, "tmp0.x = dot(%s, %s);\n",
2581 src_param.param_str, src_param.param_str);
2582 shader_glsl_append_dst(buffer, ins);
2586 shader_addline(buffer, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2587 mask_size, src_param.param_str);
2591 shader_addline(buffer, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2592 src_param.param_str);
2596 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2597 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2598 * dst.x = 2^(floor(src))
2599 * dst.y = src - floor(src)
2600 * dst.z = 2^src (partial precision is allowed, but optional)
2602 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2603 * dst = 2^src; (partial precision is allowed, but optional)
2605 static void shader_glsl_expp(const struct wined3d_shader_instruction *ins)
2607 struct glsl_src_param src_param;
2609 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
2611 if (ins->ctx->reg_maps->shader_version.major < 2)
2615 shader_addline(ins->ctx->buffer, "tmp0.x = exp2(floor(%s));\n", src_param.param_str);
2616 shader_addline(ins->ctx->buffer, "tmp0.y = %s - floor(%s);\n", src_param.param_str, src_param.param_str);
2617 shader_addline(ins->ctx->buffer, "tmp0.z = exp2(%s);\n", src_param.param_str);
2618 shader_addline(ins->ctx->buffer, "tmp0.w = 1.0;\n");
2620 shader_glsl_append_dst(ins->ctx->buffer, ins);
2621 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
2622 shader_addline(ins->ctx->buffer, "tmp0%s);\n", dst_mask);
2625 unsigned int mask_size;
2627 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2628 mask_size = shader_glsl_get_write_mask_size(write_mask);
2630 if (mask_size > 1) {
2631 shader_addline(ins->ctx->buffer, "vec%d(exp2(%s)));\n", mask_size, src_param.param_str);
2633 shader_addline(ins->ctx->buffer, "exp2(%s));\n", src_param.param_str);
2638 static void shader_glsl_to_int(const struct wined3d_shader_instruction *ins)
2640 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2641 struct glsl_src_param src_param;
2642 unsigned int mask_size;
2645 write_mask = shader_glsl_append_dst(buffer, ins);
2646 mask_size = shader_glsl_get_write_mask_size(write_mask);
2647 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2650 shader_addline(buffer, "ivec%u(%s));\n", mask_size, src_param.param_str);
2652 shader_addline(buffer, "int(%s));\n", src_param.param_str);
2655 static void shader_glsl_to_float(const struct wined3d_shader_instruction *ins)
2657 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2658 struct glsl_src_param src_param;
2659 unsigned int mask_size;
2662 write_mask = shader_glsl_append_dst(buffer, ins);
2663 mask_size = shader_glsl_get_write_mask_size(write_mask);
2664 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src_param);
2667 shader_addline(buffer, "vec%u(%s));\n", mask_size, src_param.param_str);
2669 shader_addline(buffer, "float(%s));\n", src_param.param_str);
2672 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2673 static void shader_glsl_rcp(const struct wined3d_shader_instruction *ins)
2675 struct glsl_src_param src_param;
2677 unsigned int mask_size;
2679 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2680 mask_size = shader_glsl_get_write_mask_size(write_mask);
2681 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2685 shader_addline(ins->ctx->buffer, "vec%u(1.0 / %s));\n",
2686 mask_size, src_param.param_str);
2690 shader_addline(ins->ctx->buffer, "1.0 / %s);\n",
2691 src_param.param_str);
2695 static void shader_glsl_rsq(const struct wined3d_shader_instruction *ins)
2697 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
2698 struct glsl_src_param src_param;
2700 unsigned int mask_size;
2702 write_mask = shader_glsl_append_dst(buffer, ins);
2703 mask_size = shader_glsl_get_write_mask_size(write_mask);
2705 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src_param);
2709 shader_addline(buffer, "vec%u(inversesqrt(abs(%s))));\n",
2710 mask_size, src_param.param_str);
2714 shader_addline(buffer, "inversesqrt(abs(%s)));\n",
2715 src_param.param_str);
2719 /** Process signed comparison opcodes in GLSL. */
2720 static void shader_glsl_compare(const struct wined3d_shader_instruction *ins)
2722 struct glsl_src_param src0_param;
2723 struct glsl_src_param src1_param;
2725 unsigned int mask_size;
2727 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2728 mask_size = shader_glsl_get_write_mask_size(write_mask);
2729 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2730 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2732 if (mask_size > 1) {
2733 const char *compare;
2735 switch(ins->handler_idx)
2737 case WINED3DSIH_SLT: compare = "lessThan"; break;
2738 case WINED3DSIH_SGE: compare = "greaterThanEqual"; break;
2739 default: compare = "";
2740 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2743 shader_addline(ins->ctx->buffer, "vec%d(%s(%s, %s)));\n", mask_size, compare,
2744 src0_param.param_str, src1_param.param_str);
2746 switch(ins->handler_idx)
2748 case WINED3DSIH_SLT:
2749 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2750 * to return 0.0 but step returns 1.0 because step is not < x
2751 * An alternative is a bvec compare padded with an unused second component.
2752 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2753 * issue. Playing with not() is not possible either because not() does not accept
2756 shader_addline(ins->ctx->buffer, "(%s < %s) ? 1.0 : 0.0);\n",
2757 src0_param.param_str, src1_param.param_str);
2759 case WINED3DSIH_SGE:
2760 /* Here we can use the step() function and safe a conditional */
2761 shader_addline(ins->ctx->buffer, "step(%s, %s));\n", src1_param.param_str, src0_param.param_str);
2764 FIXME("Can't handle opcode %#x\n", ins->handler_idx);
2770 static void shader_glsl_conditional_move(const struct wined3d_shader_instruction *ins)
2772 const char *condition_prefix, *condition_suffix;
2773 struct wined3d_shader_dst_param dst;
2774 struct glsl_src_param src0_param;
2775 struct glsl_src_param src1_param;
2776 struct glsl_src_param src2_param;
2777 BOOL temp_destination = FALSE;
2778 DWORD cmp_channel = 0;
2783 switch (ins->handler_idx)
2785 case WINED3DSIH_CMP:
2786 condition_prefix = "";
2787 condition_suffix = " >= 0.0";
2790 case WINED3DSIH_CND:
2791 condition_prefix = "";
2792 condition_suffix = " > 0.5";
2795 case WINED3DSIH_MOVC:
2796 condition_prefix = "bool(";
2797 condition_suffix = ")";
2801 FIXME("Unhandled instruction %#x.\n", ins->handler_idx);
2802 condition_prefix = "<unhandled prefix>";
2803 condition_suffix = "<unhandled suffix>";
2807 if (shader_is_scalar(&ins->src[0].reg))
2809 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2810 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2811 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2812 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2814 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2815 condition_prefix, src0_param.param_str, condition_suffix,
2816 src1_param.param_str, src2_param.param_str);
2822 /* Splitting the instruction up in multiple lines imposes a problem:
2823 * The first lines may overwrite source parameters of the following lines.
2824 * Deal with that by using a temporary destination register if needed. */
2825 if ((ins->src[0].reg.idx[0].offset == dst.reg.idx[0].offset
2826 && ins->src[0].reg.type == dst.reg.type)
2827 || (ins->src[1].reg.idx[0].offset == dst.reg.idx[0].offset
2828 && ins->src[1].reg.type == dst.reg.type)
2829 || (ins->src[2].reg.idx[0].offset == dst.reg.idx[0].offset
2830 && ins->src[2].reg.type == dst.reg.type))
2831 temp_destination = TRUE;
2833 /* Cycle through all source0 channels. */
2834 for (i = 0; i < 4; ++i)
2837 /* Find the destination channels which use the current source0 channel. */
2838 for (j = 0; j < 4; ++j)
2840 if (((ins->src[0].swizzle >> (2 * j)) & 0x3) == i)
2842 write_mask |= WINED3DSP_WRITEMASK_0 << j;
2843 cmp_channel = WINED3DSP_WRITEMASK_0 << j;
2846 dst.write_mask = ins->dst[0].write_mask & write_mask;
2848 if (temp_destination)
2850 if (!(write_mask = shader_glsl_get_write_mask(&dst, mask_char)))
2852 shader_addline(ins->ctx->buffer, "tmp0%s = (", mask_char);
2854 else if (!(write_mask = shader_glsl_append_dst_ext(ins->ctx->buffer, ins, &dst)))
2857 shader_glsl_add_src_param(ins, &ins->src[0], cmp_channel, &src0_param);
2858 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2859 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2861 shader_addline(ins->ctx->buffer, "%s%s%s ? %s : %s);\n",
2862 condition_prefix, src0_param.param_str, condition_suffix,
2863 src1_param.param_str, src2_param.param_str);
2866 if (temp_destination)
2868 shader_glsl_get_write_mask(&ins->dst[0], mask_char);
2869 shader_glsl_append_dst(ins->ctx->buffer, ins);
2870 shader_addline(ins->ctx->buffer, "tmp0%s);\n", mask_char);
2874 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2875 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2876 * the compare is done per component of src0. */
2877 static void shader_glsl_cnd(const struct wined3d_shader_instruction *ins)
2879 struct glsl_src_param src0_param;
2880 struct glsl_src_param src1_param;
2881 struct glsl_src_param src2_param;
2883 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
2884 ins->ctx->reg_maps->shader_version.minor);
2886 if (shader_version < WINED3D_SHADER_VERSION(1, 4))
2888 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2889 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
2890 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2891 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2893 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2896 shader_addline(ins->ctx->buffer, "%s /* COISSUE! */);\n", src1_param.param_str);
2898 shader_addline(ins->ctx->buffer, "%s > 0.5 ? %s : %s);\n",
2899 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2904 shader_glsl_conditional_move(ins);
2907 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2908 static void shader_glsl_mad(const struct wined3d_shader_instruction *ins)
2910 struct glsl_src_param src0_param;
2911 struct glsl_src_param src1_param;
2912 struct glsl_src_param src2_param;
2915 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2916 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2917 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2918 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2919 shader_addline(ins->ctx->buffer, "(%s * %s) + %s);\n",
2920 src0_param.param_str, src1_param.param_str, src2_param.param_str);
2923 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2924 Vertex shaders to GLSL codes */
2925 static void shader_glsl_mnxn(const struct wined3d_shader_instruction *ins)
2928 int nComponents = 0;
2929 struct wined3d_shader_dst_param tmp_dst = {{0}};
2930 struct wined3d_shader_src_param tmp_src[2] = {{{0}}};
2931 struct wined3d_shader_instruction tmp_ins;
2933 memset(&tmp_ins, 0, sizeof(tmp_ins));
2935 /* Set constants for the temporary argument */
2936 tmp_ins.ctx = ins->ctx;
2937 tmp_ins.dst_count = 1;
2938 tmp_ins.dst = &tmp_dst;
2939 tmp_ins.src_count = 2;
2940 tmp_ins.src = tmp_src;
2942 switch(ins->handler_idx)
2944 case WINED3DSIH_M4x4:
2946 tmp_ins.handler_idx = WINED3DSIH_DP4;
2948 case WINED3DSIH_M4x3:
2950 tmp_ins.handler_idx = WINED3DSIH_DP4;
2952 case WINED3DSIH_M3x4:
2954 tmp_ins.handler_idx = WINED3DSIH_DP3;
2956 case WINED3DSIH_M3x3:
2958 tmp_ins.handler_idx = WINED3DSIH_DP3;
2960 case WINED3DSIH_M3x2:
2962 tmp_ins.handler_idx = WINED3DSIH_DP3;
2968 tmp_dst = ins->dst[0];
2969 tmp_src[0] = ins->src[0];
2970 tmp_src[1] = ins->src[1];
2971 for (i = 0; i < nComponents; ++i)
2973 tmp_dst.write_mask = WINED3DSP_WRITEMASK_0 << i;
2974 shader_glsl_dot(&tmp_ins);
2975 ++tmp_src[1].reg.idx[0].offset;
2980 The LRP instruction performs a component-wise linear interpolation
2981 between the second and third operands using the first operand as the
2982 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2983 This is equivalent to mix(src2, src1, src0);
2985 static void shader_glsl_lrp(const struct wined3d_shader_instruction *ins)
2987 struct glsl_src_param src0_param;
2988 struct glsl_src_param src1_param;
2989 struct glsl_src_param src2_param;
2992 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
2994 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
2995 shader_glsl_add_src_param(ins, &ins->src[1], write_mask, &src1_param);
2996 shader_glsl_add_src_param(ins, &ins->src[2], write_mask, &src2_param);
2998 shader_addline(ins->ctx->buffer, "mix(%s, %s, %s));\n",
2999 src2_param.param_str, src1_param.param_str, src0_param.param_str);
3002 /** Process the WINED3DSIO_LIT instruction in GLSL:
3003 * dst.x = dst.w = 1.0
3004 * dst.y = (src0.x > 0) ? src0.x
3005 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
3006 * where src.w is clamped at +- 128
3008 static void shader_glsl_lit(const struct wined3d_shader_instruction *ins)
3010 struct glsl_src_param src0_param;
3011 struct glsl_src_param src1_param;
3012 struct glsl_src_param src3_param;
3015 shader_glsl_append_dst(ins->ctx->buffer, ins);
3016 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3018 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3019 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src1_param);
3020 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &src3_param);
3022 /* The sdk specifies the instruction like this
3024 * if(src.x > 0.0) dst.y = src.x
3026 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
3029 * (where power = src.w clamped between -128 and 128)
3031 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
3032 * dst.x = 1.0 ... No further explanation needed
3033 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
3034 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
3035 * dst.w = 1.0. ... Nothing fancy.
3037 * So we still have one conditional in there. So do this:
3038 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
3040 * 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),
3041 * 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.
3042 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
3044 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
3045 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
3046 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
3048 shader_addline(ins->ctx->buffer,
3049 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
3050 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
3051 src0_param.param_str, src3_param.param_str, src1_param.param_str,
3052 src0_param.param_str, src3_param.param_str, dst_mask);
3055 /** Process the WINED3DSIO_DST instruction in GLSL:
3057 * dst.y = src0.x * src0.y
3061 static void shader_glsl_dst(const struct wined3d_shader_instruction *ins)
3063 struct glsl_src_param src0y_param;
3064 struct glsl_src_param src0z_param;
3065 struct glsl_src_param src1y_param;
3066 struct glsl_src_param src1w_param;
3069 shader_glsl_append_dst(ins->ctx->buffer, ins);
3070 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3072 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_1, &src0y_param);
3073 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &src0z_param);
3074 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_1, &src1y_param);
3075 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_3, &src1w_param);
3077 shader_addline(ins->ctx->buffer, "vec4(1.0, %s * %s, %s, %s))%s;\n",
3078 src0y_param.param_str, src1y_param.param_str, src0z_param.param_str, src1w_param.param_str, dst_mask);
3081 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
3082 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
3083 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
3085 * dst.x = cos(src0.?)
3086 * dst.y = sin(src0.?)
3090 static void shader_glsl_sincos(const struct wined3d_shader_instruction *ins)
3092 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3093 struct glsl_src_param src0_param;
3096 if (ins->ctx->reg_maps->shader_version.major < 4)
3098 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3100 write_mask = shader_glsl_append_dst(buffer, ins);
3103 case WINED3DSP_WRITEMASK_0:
3104 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3107 case WINED3DSP_WRITEMASK_1:
3108 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3111 case (WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1):
3112 shader_addline(buffer, "vec2(cos(%s), sin(%s)));\n",
3113 src0_param.param_str, src0_param.param_str);
3117 ERR("Write mask should be .x, .y or .xy\n");
3124 if (ins->dst[0].reg.type != WINED3DSPR_NULL)
3127 if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3131 write_mask = shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3132 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3133 shader_addline(buffer, "tmp0%s = sin(%s);\n", dst_mask, src0_param.param_str);
3135 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3136 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3137 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3139 shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3140 shader_addline(buffer, "tmp0%s);\n", dst_mask);
3144 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[0]);
3145 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3146 shader_addline(buffer, "sin(%s));\n", src0_param.param_str);
3149 else if (ins->dst[1].reg.type != WINED3DSPR_NULL)
3151 write_mask = shader_glsl_append_dst_ext(buffer, ins, &ins->dst[1]);
3152 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3153 shader_addline(buffer, "cos(%s));\n", src0_param.param_str);
3157 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
3158 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
3159 * generate invalid code
3161 static void shader_glsl_sgn(const struct wined3d_shader_instruction *ins)
3163 struct glsl_src_param src0_param;
3166 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3167 shader_glsl_add_src_param(ins, &ins->src[0], write_mask, &src0_param);
3169 shader_addline(ins->ctx->buffer, "sign(%s));\n", src0_param.param_str);
3172 /** Process the WINED3DSIO_LOOP instruction in GLSL:
3173 * Start a for() loop where src1.y is the initial value of aL,
3174 * increment aL by src1.z for a total of src1.x iterations.
3175 * Need to use a temporary variable for this operation.
3177 /* FIXME: I don't think nested loops will work correctly this way. */
3178 static void shader_glsl_loop(const struct wined3d_shader_instruction *ins)
3180 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3181 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3182 const struct wined3d_shader *shader = ins->ctx->shader;
3183 const struct wined3d_shader_lconst *constant;
3184 struct glsl_src_param src1_param;
3185 const DWORD *control_values = NULL;
3187 if (ins->ctx->reg_maps->shader_version.major < 4)
3189 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_ALL, &src1_param);
3191 /* Try to hardcode the loop control parameters if possible. Direct3D 9
3192 * class hardware doesn't support real varying indexing, but Microsoft
3193 * designed this feature for Shader model 2.x+. If the loop control is
3194 * known at compile time, the GLSL compiler can unroll the loop, and
3195 * replace indirect addressing with direct addressing. */
3196 if (ins->src[1].reg.type == WINED3DSPR_CONSTINT)
3198 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3200 if (constant->idx == ins->src[1].reg.idx[0].offset)
3202 control_values = constant->value;
3210 struct wined3d_shader_loop_control loop_control;
3211 loop_control.count = control_values[0];
3212 loop_control.start = control_values[1];
3213 loop_control.step = (int)control_values[2];
3215 if (loop_control.step > 0)
3217 shader_addline(buffer, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d)\n{\n",
3218 loop_state->current_depth, loop_control.start,
3219 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3220 loop_state->current_depth, loop_control.step);
3222 else if (loop_control.step < 0)
3224 shader_addline(buffer, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d)\n{\n",
3225 loop_state->current_depth, loop_control.start,
3226 loop_state->current_depth, loop_control.count, loop_control.step, loop_control.start,
3227 loop_state->current_depth, loop_control.step);
3231 shader_addline(buffer, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++)\n{\n",
3232 loop_state->current_depth, loop_control.start, loop_state->current_depth,
3233 loop_state->current_depth, loop_control.count,
3234 loop_state->current_depth);
3239 shader_addline(buffer, "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z)\n{\n",
3240 loop_state->current_depth, loop_state->current_reg,
3241 src1_param.reg_name, loop_state->current_depth, src1_param.reg_name,
3242 loop_state->current_depth, loop_state->current_reg, src1_param.reg_name);
3245 ++loop_state->current_reg;
3249 shader_addline(buffer, "for (;;)\n{\n");
3252 ++loop_state->current_depth;
3255 static void shader_glsl_end(const struct wined3d_shader_instruction *ins)
3257 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3259 shader_addline(ins->ctx->buffer, "}\n");
3261 if (ins->handler_idx == WINED3DSIH_ENDLOOP)
3263 --loop_state->current_depth;
3264 --loop_state->current_reg;
3267 if (ins->handler_idx == WINED3DSIH_ENDREP)
3269 --loop_state->current_depth;
3273 static void shader_glsl_rep(const struct wined3d_shader_instruction *ins)
3275 const struct wined3d_shader *shader = ins->ctx->shader;
3276 struct wined3d_shader_loop_state *loop_state = ins->ctx->loop_state;
3277 const struct wined3d_shader_lconst *constant;
3278 struct glsl_src_param src0_param;
3279 const DWORD *control_values = NULL;
3281 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
3282 if (ins->src[0].reg.type == WINED3DSPR_CONSTINT)
3284 LIST_FOR_EACH_ENTRY(constant, &shader->constantsI, struct wined3d_shader_lconst, entry)
3286 if (constant->idx == ins->src[0].reg.idx[0].offset)
3288 control_values = constant->value;
3296 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
3297 loop_state->current_depth, loop_state->current_depth,
3298 control_values[0], loop_state->current_depth);
3302 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3303 shader_addline(ins->ctx->buffer, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
3304 loop_state->current_depth, loop_state->current_depth,
3305 src0_param.param_str, loop_state->current_depth);
3308 ++loop_state->current_depth;
3311 static void shader_glsl_if(const struct wined3d_shader_instruction *ins)
3313 struct glsl_src_param src0_param;
3315 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3316 shader_addline(ins->ctx->buffer, "if (%s) {\n", src0_param.param_str);
3319 static void shader_glsl_ifc(const struct wined3d_shader_instruction *ins)
3321 struct glsl_src_param src0_param;
3322 struct glsl_src_param src1_param;
3324 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3325 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3327 shader_addline(ins->ctx->buffer, "if (%s %s %s) {\n",
3328 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3331 static void shader_glsl_else(const struct wined3d_shader_instruction *ins)
3333 shader_addline(ins->ctx->buffer, "} else {\n");
3336 static void shader_glsl_emit(const struct wined3d_shader_instruction *ins)
3338 shader_addline(ins->ctx->buffer, "EmitVertex();\n");
3341 static void shader_glsl_break(const struct wined3d_shader_instruction *ins)
3343 shader_addline(ins->ctx->buffer, "break;\n");
3346 /* FIXME: According to MSDN the compare is done per component. */
3347 static void shader_glsl_breakc(const struct wined3d_shader_instruction *ins)
3349 struct glsl_src_param src0_param;
3350 struct glsl_src_param src1_param;
3352 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src0_param);
3353 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3355 shader_addline(ins->ctx->buffer, "if (%s %s %s) break;\n",
3356 src0_param.param_str, shader_glsl_get_rel_op(ins->flags), src1_param.param_str);
3359 static void shader_glsl_breakp(const struct wined3d_shader_instruction *ins)
3361 struct glsl_src_param src_param;
3363 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0, &src_param);
3364 shader_addline(ins->ctx->buffer, "if (bool(%s)) break;\n", src_param.param_str);
3367 static void shader_glsl_label(const struct wined3d_shader_instruction *ins)
3369 shader_addline(ins->ctx->buffer, "}\n");
3370 shader_addline(ins->ctx->buffer, "void subroutine%u()\n{\n", ins->src[0].reg.idx[0].offset);
3373 static void shader_glsl_call(const struct wined3d_shader_instruction *ins)
3375 shader_addline(ins->ctx->buffer, "subroutine%u();\n", ins->src[0].reg.idx[0].offset);
3378 static void shader_glsl_callnz(const struct wined3d_shader_instruction *ins)
3380 struct glsl_src_param src1_param;
3382 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0, &src1_param);
3383 shader_addline(ins->ctx->buffer, "if (%s) subroutine%u();\n",
3384 src1_param.param_str, ins->src[0].reg.idx[0].offset);
3387 static void shader_glsl_ret(const struct wined3d_shader_instruction *ins)
3389 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3390 * function only suppresses the unhandled instruction warning
3394 /*********************************************
3395 * Pixel Shader Specific Code begins here
3396 ********************************************/
3397 static void shader_glsl_tex(const struct wined3d_shader_instruction *ins)
3399 const struct wined3d_shader *shader = ins->ctx->shader;
3400 struct wined3d_device *device = shader->device;
3401 DWORD shader_version = WINED3D_SHADER_VERSION(ins->ctx->reg_maps->shader_version.major,
3402 ins->ctx->reg_maps->shader_version.minor);
3403 struct glsl_sample_function sample_function;
3404 const struct wined3d_texture *texture;
3405 DWORD sample_flags = 0;
3407 DWORD mask = 0, swizzle;
3409 /* 1.0-1.4: Use destination register as sampler source.
3410 * 2.0+: Use provided sampler source. */
3411 if (shader_version < WINED3D_SHADER_VERSION(2,0))
3412 sampler_idx = ins->dst[0].reg.idx[0].offset;
3414 sampler_idx = ins->src[1].reg.idx[0].offset;
3415 texture = device->stateBlock->state.textures[sampler_idx];
3417 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3419 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3420 DWORD flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3421 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3422 enum wined3d_sampler_texture_type sampler_type = ins->ctx->reg_maps->sampler_type[sampler_idx];
3424 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3425 if (flags & WINED3D_PSARGS_PROJECTED && sampler_type != WINED3DSTT_CUBE)
3427 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3428 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3430 case WINED3D_TTFF_COUNT1:
3431 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3433 case WINED3D_TTFF_COUNT2:
3434 mask = WINED3DSP_WRITEMASK_1;
3436 case WINED3D_TTFF_COUNT3:
3437 mask = WINED3DSP_WRITEMASK_2;
3439 case WINED3D_TTFF_COUNT4:
3440 case WINED3D_TTFF_DISABLE:
3441 mask = WINED3DSP_WRITEMASK_3;
3446 else if (shader_version < WINED3D_SHADER_VERSION(2,0))
3448 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3450 if (src_mod == WINED3DSPSM_DZ) {
3451 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3452 mask = WINED3DSP_WRITEMASK_2;
3453 } else if (src_mod == WINED3DSPSM_DW) {
3454 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3455 mask = WINED3DSP_WRITEMASK_3;
3458 if (ins->flags & WINED3DSI_TEXLD_PROJECT)
3460 /* ps 2.0 texldp instruction always divides by the fourth component. */
3461 sample_flags |= WINED3D_GLSL_SAMPLE_PROJECTED;
3462 mask = WINED3DSP_WRITEMASK_3;
3466 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3467 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3469 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3470 mask |= sample_function.coord_mask;
3472 if (shader_version < WINED3D_SHADER_VERSION(2,0)) swizzle = WINED3DSP_NOSWIZZLE;
3473 else swizzle = ins->src[1].swizzle;
3475 /* 1.0-1.3: Use destination register as coordinate source.
3476 1.4+: Use provided coordinate source register. */
3477 if (shader_version < WINED3D_SHADER_VERSION(1,4))
3480 shader_glsl_write_mask_to_str(mask, coord_mask);
3481 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3482 "T%u%s", sampler_idx, coord_mask);
3486 struct glsl_src_param coord_param;
3487 shader_glsl_add_src_param(ins, &ins->src[0], mask, &coord_param);
3488 if (ins->flags & WINED3DSI_TEXLD_BIAS)
3490 struct glsl_src_param bias;
3491 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &bias);
3492 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, bias.param_str,
3493 "%s", coord_param.param_str);
3495 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, NULL,
3496 "%s", coord_param.param_str);
3501 static void shader_glsl_texldd(const struct wined3d_shader_instruction *ins)
3503 const struct wined3d_shader *shader = ins->ctx->shader;
3504 struct wined3d_device *device = shader->device;
3505 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3506 struct glsl_src_param coord_param, dx_param, dy_param;
3507 DWORD sample_flags = WINED3D_GLSL_SAMPLE_GRAD;
3508 struct glsl_sample_function sample_function;
3510 DWORD swizzle = ins->src[1].swizzle;
3511 const struct wined3d_texture *texture;
3513 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4])
3515 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3516 shader_glsl_tex(ins);
3520 sampler_idx = ins->src[1].reg.idx[0].offset;
3521 texture = device->stateBlock->state.textures[sampler_idx];
3522 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3523 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3525 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3526 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3527 shader_glsl_add_src_param(ins, &ins->src[2], sample_function.coord_mask, &dx_param);
3528 shader_glsl_add_src_param(ins, &ins->src[3], sample_function.coord_mask, &dy_param);
3530 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, dx_param.param_str, dy_param.param_str, NULL,
3531 "%s", coord_param.param_str);
3534 static void shader_glsl_texldl(const struct wined3d_shader_instruction *ins)
3536 const struct wined3d_shader *shader = ins->ctx->shader;
3537 struct wined3d_device *device = shader->device;
3538 const struct wined3d_gl_info *gl_info = ins->ctx->gl_info;
3539 struct glsl_src_param coord_param, lod_param;
3540 DWORD sample_flags = WINED3D_GLSL_SAMPLE_LOD;
3541 struct glsl_sample_function sample_function;
3543 DWORD swizzle = ins->src[1].swizzle;
3544 const struct wined3d_texture *texture;
3546 sampler_idx = ins->src[1].reg.idx[0].offset;
3547 texture = device->stateBlock->state.textures[sampler_idx];
3548 if (texture && texture->target == GL_TEXTURE_RECTANGLE_ARB)
3549 sample_flags |= WINED3D_GLSL_SAMPLE_RECT;
3551 shader_glsl_get_sample_function(ins->ctx, sampler_idx, sample_flags, &sample_function);
3552 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &coord_param);
3554 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &lod_param);
3556 if (!gl_info->supported[ARB_SHADER_TEXTURE_LOD] && !gl_info->supported[EXT_GPU_SHADER4]
3557 && ins->ctx->reg_maps->shader_version.type == WINED3D_SHADER_TYPE_PIXEL)
3559 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3560 * However, the NVIDIA drivers allow them in fragment shaders as well,
3561 * even without the appropriate extension. */
3562 WARN("Using %s in fragment shader.\n", sample_function.name);
3564 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, swizzle, NULL, NULL, lod_param.param_str,
3565 "%s", coord_param.param_str);
3568 static void shader_glsl_texcoord(const struct wined3d_shader_instruction *ins)
3570 /* FIXME: Make this work for more than just 2D textures */
3571 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3572 DWORD write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3574 if (!(ins->ctx->reg_maps->shader_version.major == 1 && ins->ctx->reg_maps->shader_version.minor == 4))
3578 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3579 shader_addline(buffer, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3580 ins->dst[0].reg.idx[0].offset, dst_mask);
3584 enum wined3d_shader_src_modifier src_mod = ins->src[0].modifiers;
3585 DWORD reg = ins->src[0].reg.idx[0].offset;
3586 char dst_swizzle[6];
3588 shader_glsl_get_swizzle(&ins->src[0], FALSE, write_mask, dst_swizzle);
3590 if (src_mod == WINED3DSPSM_DZ)
3592 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3593 struct glsl_src_param div_param;
3595 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &div_param);
3597 if (mask_size > 1) {
3598 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3600 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3603 else if (src_mod == WINED3DSPSM_DW)
3605 unsigned int mask_size = shader_glsl_get_write_mask_size(write_mask);
3606 struct glsl_src_param div_param;
3608 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_3, &div_param);
3610 if (mask_size > 1) {
3611 shader_addline(buffer, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg, dst_swizzle, mask_size, div_param.param_str);
3613 shader_addline(buffer, "gl_TexCoord[%u]%s / %s);\n", reg, dst_swizzle, div_param.param_str);
3616 shader_addline(buffer, "gl_TexCoord[%u]%s);\n", reg, dst_swizzle);
3621 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3622 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3623 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3624 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction *ins)
3626 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3627 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3628 struct glsl_sample_function sample_function;
3629 struct glsl_src_param src0_param;
3632 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3634 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3635 * scalar, and projected sampling would require 4.
3637 * It is a dependent read - not valid with conditional NP2 textures
3639 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3640 mask_size = shader_glsl_get_write_mask_size(sample_function.coord_mask);
3645 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3646 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx, src0_param.param_str);
3650 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3651 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx, src0_param.param_str);
3655 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3656 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx, src0_param.param_str);
3660 FIXME("Unexpected mask size %u\n", mask_size);
3665 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3666 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3667 static void shader_glsl_texdp3(const struct wined3d_shader_instruction *ins)
3669 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3670 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3671 struct glsl_src_param src0_param;
3673 unsigned int mask_size;
3675 dst_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
3676 mask_size = shader_glsl_get_write_mask_size(dst_mask);
3677 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3679 if (mask_size > 1) {
3680 shader_addline(ins->ctx->buffer, "vec%d(dot(T%u.xyz, %s)));\n", mask_size, dstreg, src0_param.param_str);
3682 shader_addline(ins->ctx->buffer, "dot(T%u.xyz, %s));\n", dstreg, src0_param.param_str);
3686 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3687 * Calculate the depth as dst.x / dst.y */
3688 static void shader_glsl_texdepth(const struct wined3d_shader_instruction *ins)
3690 struct glsl_dst_param dst_param;
3692 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3694 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3695 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3696 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3697 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3700 shader_addline(ins->ctx->buffer, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3701 dst_param.reg_name, dst_param.reg_name);
3704 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3705 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3706 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3707 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3709 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction *ins)
3711 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3712 DWORD dstreg = ins->dst[0].reg.idx[0].offset;
3713 struct glsl_src_param src0_param;
3715 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3717 shader_addline(ins->ctx->buffer, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg, src0_param.param_str);
3718 shader_addline(ins->ctx->buffer, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3721 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3722 * Calculate the 1st of a 2-row matrix multiplication. */
3723 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction *ins)
3725 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3726 DWORD reg = ins->dst[0].reg.idx[0].offset;
3727 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3728 struct glsl_src_param src0_param;
3730 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3731 shader_addline(buffer, "tmp0.x = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3734 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3735 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3736 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction *ins)
3738 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3739 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3740 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3741 DWORD reg = ins->dst[0].reg.idx[0].offset;
3742 struct glsl_src_param src0_param;
3744 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3745 shader_addline(buffer, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx->current_row, reg, src0_param.param_str);
3746 tex_mx->texcoord_w[tex_mx->current_row++] = reg;
3749 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction *ins)
3751 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3752 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3753 struct glsl_sample_function sample_function;
3754 DWORD reg = ins->dst[0].reg.idx[0].offset;
3755 struct glsl_src_param src0_param;
3757 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3758 shader_addline(buffer, "tmp0.y = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3760 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3762 /* Sample the texture using the calculated coordinates */
3763 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xy");
3766 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3767 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3768 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction *ins)
3770 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3771 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3772 struct glsl_sample_function sample_function;
3773 DWORD reg = ins->dst[0].reg.idx[0].offset;
3774 struct glsl_src_param src0_param;
3776 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3777 shader_addline(ins->ctx->buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3779 /* Dependent read, not valid with conditional NP2 */
3780 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3782 /* Sample the texture using the calculated coordinates */
3783 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL, "tmp0.xyz");
3785 tex_mx->current_row = 0;
3788 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3789 * Perform the 3rd row of a 3x3 matrix multiply */
3790 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction *ins)
3792 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3793 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3794 DWORD reg = ins->dst[0].reg.idx[0].offset;
3795 struct glsl_src_param src0_param;
3798 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3800 shader_glsl_append_dst(ins->ctx->buffer, ins);
3801 shader_glsl_get_write_mask(&ins->dst[0], dst_mask);
3802 shader_addline(ins->ctx->buffer, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg, src0_param.param_str, dst_mask);
3804 tex_mx->current_row = 0;
3807 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3808 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3809 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction *ins)
3811 struct glsl_src_param src0_param;
3812 struct glsl_src_param src1_param;
3813 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3814 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3815 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3816 struct glsl_sample_function sample_function;
3817 DWORD reg = ins->dst[0].reg.idx[0].offset;
3820 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3821 shader_glsl_add_src_param(ins, &ins->src[1], src_mask, &src1_param);
3823 /* Perform the last matrix multiply operation */
3824 shader_addline(buffer, "tmp0.z = dot(T%u.xyz, %s);\n", reg, src0_param.param_str);
3825 /* Reflection calculation */
3826 shader_addline(buffer, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param.param_str);
3828 /* Dependent read, not valid with conditional NP2 */
3829 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3830 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3832 /* Sample the texture */
3833 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3834 NULL, NULL, NULL, "tmp0%s", coord_mask);
3836 tex_mx->current_row = 0;
3839 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3840 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3841 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction *ins)
3843 struct wined3d_shader_buffer *buffer = ins->ctx->buffer;
3844 struct wined3d_shader_tex_mx *tex_mx = ins->ctx->tex_mx;
3845 DWORD src_mask = WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1 | WINED3DSP_WRITEMASK_2;
3846 struct glsl_sample_function sample_function;
3847 DWORD reg = ins->dst[0].reg.idx[0].offset;
3848 struct glsl_src_param src0_param;
3851 shader_glsl_add_src_param(ins, &ins->src[0], src_mask, &src0_param);
3853 /* Perform the last matrix multiply operation */
3854 shader_addline(buffer, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg, src0_param.param_str);
3856 /* Construct the eye-ray vector from w coordinates */
3857 shader_addline(buffer, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3858 tex_mx->texcoord_w[0], tex_mx->texcoord_w[1], reg);
3859 shader_addline(buffer, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3861 /* Dependent read, not valid with conditional NP2 */
3862 shader_glsl_get_sample_function(ins->ctx, reg, 0, &sample_function);
3863 shader_glsl_write_mask_to_str(sample_function.coord_mask, coord_mask);
3865 /* Sample the texture using the calculated coordinates */
3866 shader_glsl_gen_sample_code(ins, reg, &sample_function, WINED3DSP_NOSWIZZLE,
3867 NULL, NULL, NULL, "tmp0%s", coord_mask);
3869 tex_mx->current_row = 0;
3872 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3873 * Apply a fake bump map transform.
3874 * texbem is pshader <= 1.3 only, this saves a few version checks
3876 static void shader_glsl_texbem(const struct wined3d_shader_instruction *ins)
3878 const struct shader_glsl_ctx_priv *priv = ins->ctx->backend_data;
3879 struct glsl_sample_function sample_function;
3880 struct glsl_src_param coord_param;
3886 sampler_idx = ins->dst[0].reg.idx[0].offset;
3887 flags = (priv->cur_ps_args->tex_transform >> sampler_idx * WINED3D_PSARGS_TEXTRANSFORM_SHIFT)
3888 & WINED3D_PSARGS_TEXTRANSFORM_MASK;
3890 /* Dependent read, not valid with conditional NP2 */
3891 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3892 mask = sample_function.coord_mask;
3894 shader_glsl_write_mask_to_str(mask, coord_mask);
3896 /* With projected textures, texbem only divides the static texture coord,
3897 * not the displacement, so we can't let GL handle this. */
3898 if (flags & WINED3D_PSARGS_PROJECTED)
3901 char coord_div_mask[3];
3902 switch (flags & ~WINED3D_PSARGS_PROJECTED)
3904 case WINED3D_TTFF_COUNT1:
3905 FIXME("WINED3D_TTFF_PROJECTED with WINED3D_TTFF_COUNT1?\n");
3907 case WINED3D_TTFF_COUNT2:
3908 div_mask = WINED3DSP_WRITEMASK_1;
3910 case WINED3D_TTFF_COUNT3:
3911 div_mask = WINED3DSP_WRITEMASK_2;
3913 case WINED3D_TTFF_COUNT4:
3914 case WINED3D_TTFF_DISABLE:
3915 div_mask = WINED3DSP_WRITEMASK_3;
3918 shader_glsl_write_mask_to_str(div_mask, coord_div_mask);
3919 shader_addline(ins->ctx->buffer, "T%u%s /= T%u%s;\n", sampler_idx, coord_mask, sampler_idx, coord_div_mask);
3922 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &coord_param);
3924 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3925 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx, coord_mask, sampler_idx,
3926 coord_param.param_str, coord_mask);
3928 if (ins->handler_idx == WINED3DSIH_TEXBEML)
3930 struct glsl_src_param luminance_param;
3931 struct glsl_dst_param dst_param;
3933 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_2, &luminance_param);
3934 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
3936 shader_addline(ins->ctx->buffer, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3937 dst_param.reg_name, dst_param.mask_str,
3938 luminance_param.param_str, sampler_idx, sampler_idx);
3942 static void shader_glsl_bem(const struct wined3d_shader_instruction *ins)
3944 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3945 struct glsl_src_param src0_param, src1_param;
3947 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
3948 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
3950 shader_glsl_append_dst(ins->ctx->buffer, ins);
3951 shader_addline(ins->ctx->buffer, "%s + bumpenvmat%d * %s);\n",
3952 src0_param.param_str, sampler_idx, src1_param.param_str);
3955 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3956 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3957 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction *ins)
3959 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3960 struct glsl_sample_function sample_function;
3961 struct glsl_src_param src0_param;
3963 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3965 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3966 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3967 "%s.wx", src0_param.reg_name);
3970 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3971 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3972 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction *ins)
3974 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3975 struct glsl_sample_function sample_function;
3976 struct glsl_src_param src0_param;
3978 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_ALL, &src0_param);
3980 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3981 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3982 "%s.yz", src0_param.reg_name);
3985 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3986 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3987 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction *ins)
3989 DWORD sampler_idx = ins->dst[0].reg.idx[0].offset;
3990 struct glsl_sample_function sample_function;
3991 struct glsl_src_param src0_param;
3993 /* Dependent read, not valid with conditional NP2 */
3994 shader_glsl_get_sample_function(ins->ctx, sampler_idx, 0, &sample_function);
3995 shader_glsl_add_src_param(ins, &ins->src[0], sample_function.coord_mask, &src0_param);
3997 shader_glsl_gen_sample_code(ins, sampler_idx, &sample_function, WINED3DSP_NOSWIZZLE, NULL, NULL, NULL,
3998 "%s", src0_param.param_str);
4001 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
4002 * If any of the first 3 components are < 0, discard this pixel */
4003 static void shader_glsl_texkill(const struct wined3d_shader_instruction *ins)
4005 struct glsl_dst_param dst_param;
4007 /* The argument is a destination parameter, and no writemasks are allowed */
4008 shader_glsl_add_dst_param(ins, &ins->dst[0], &dst_param);
4009 if (ins->ctx->reg_maps->shader_version.major >= 2)
4011 /* 2.0 shaders compare all 4 components in texkill */
4012 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param.reg_name);
4014 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
4015 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
4016 * 4 components are defined, only the first 3 are used
4018 shader_addline(ins->ctx->buffer, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param.reg_name);
4022 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
4023 * dst = dot2(src0, src1) + src2 */
4024 static void shader_glsl_dp2add(const struct wined3d_shader_instruction *ins)
4026 struct glsl_src_param src0_param;
4027 struct glsl_src_param src1_param;
4028 struct glsl_src_param src2_param;
4030 unsigned int mask_size;
4032 write_mask = shader_glsl_append_dst(ins->ctx->buffer, ins);
4033 mask_size = shader_glsl_get_write_mask_size(write_mask);
4035 shader_glsl_add_src_param(ins, &ins->src[0], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src0_param);
4036 shader_glsl_add_src_param(ins, &ins->src[1], WINED3DSP_WRITEMASK_0 | WINED3DSP_WRITEMASK_1, &src1_param);
4037 shader_glsl_add_src_param(ins, &ins->src[2], WINED3DSP_WRITEMASK_0, &src2_param);
4039 if (mask_size > 1) {
4040 shader_addline(ins->ctx->buffer, "vec%d(dot(%s, %s) + %s));\n",
4041 mask_size, src0_param.param_str, src1_param.param_str, src2_param.param_str);
4043 shader_addline(ins->ctx->buffer, "dot(%s, %s) + %s);\n",
4044 src0_param.param_str, src1_param.param_str, src2_param.param_str);
4048 static void shader_glsl_input_pack(const struct wined3d_shader *shader, struct wined3d_shader_buffer *buffer,
4049 const struct wined3d_shader_signature_element *input_signature,
4050 const struct wined3d_shader_reg_maps *reg_maps,
4051 enum vertexprocessing_mode vertexprocessing)
4053 WORD map = reg_maps->input_registers;
4056 for (i = 0; map; map >>= 1, ++i)
4058 const char *semantic_name;
4063 if (!(map & 1)) continue;
4065 semantic_name = input_signature[i].semantic_name;
4066 semantic_idx = input_signature[i].semantic_idx;
4067 shader_glsl_write_mask_to_str(input_signature[i].mask, reg_mask);
4069 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4071 if (semantic_idx < 8 && vertexprocessing == pretransformed)
4072 shader_addline(buffer, "ps_in[%u]%s = gl_TexCoord[%u]%s;\n",
4073 shader->u.ps.input_reg_map[i], reg_mask, semantic_idx, reg_mask);
4075 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4076 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4078 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4081 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_Color)%s;\n",
4082 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4083 else if (semantic_idx == 1)
4084 shader_addline(buffer, "ps_in[%u]%s = vec4(gl_SecondaryColor)%s;\n",
4085 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4087 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4088 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4092 shader_addline(buffer, "ps_in[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
4093 shader->u.ps.input_reg_map[i], reg_mask, reg_mask);
4098 /*********************************************
4099 * Vertex Shader Specific Code begins here
4100 ********************************************/
4102 static void add_glsl_program_entry(struct shader_glsl_priv *priv, struct glsl_shader_prog_link *entry)
4104 struct glsl_program_key key;
4106 key.vshader = entry->vshader;
4107 key.pshader = entry->pshader;
4108 key.vs_args = entry->vs_args;
4109 key.ps_args = entry->ps_args;
4111 if (wine_rb_put(&priv->program_lookup, &key, &entry->program_lookup_entry) == -1)
4113 ERR("Failed to insert program entry.\n");
4117 static struct glsl_shader_prog_link *get_glsl_program_entry(const struct shader_glsl_priv *priv,
4118 const struct wined3d_shader *vshader, const struct wined3d_shader *pshader,
4119 const struct vs_compile_args *vs_args, const struct ps_compile_args *ps_args)
4121 struct wine_rb_entry *entry;
4122 struct glsl_program_key key;
4124 key.vshader = vshader;
4125 key.pshader = pshader;
4126 key.vs_args = *vs_args;
4127 key.ps_args = *ps_args;
4129 entry = wine_rb_get(&priv->program_lookup, &key);
4130 return entry ? WINE_RB_ENTRY_VALUE(entry, struct glsl_shader_prog_link, program_lookup_entry) : NULL;
4133 /* GL locking is done by the caller */
4134 static void delete_glsl_program_entry(struct shader_glsl_priv *priv, const struct wined3d_gl_info *gl_info,
4135 struct glsl_shader_prog_link *entry)
4137 struct glsl_program_key key;
4139 key.vshader = entry->vshader;
4140 key.pshader = entry->pshader;
4141 key.vs_args = entry->vs_args;
4142 key.ps_args = entry->ps_args;
4143 wine_rb_remove(&priv->program_lookup, &key);
4145 GL_EXTCALL(glDeleteObjectARB(entry->programId));
4146 if (entry->vshader) list_remove(&entry->vshader_entry);
4147 if (entry->pshader) list_remove(&entry->pshader_entry);
4148 HeapFree(GetProcessHeap(), 0, entry->vuniformF_locations);
4149 HeapFree(GetProcessHeap(), 0, entry->puniformF_locations);
4150 HeapFree(GetProcessHeap(), 0, entry);
4153 static void handle_ps3_input(struct wined3d_shader_buffer *buffer,
4154 const struct wined3d_gl_info *gl_info, const DWORD *map,
4155 const struct wined3d_shader_signature_element *input_signature,
4156 const struct wined3d_shader_reg_maps *reg_maps_in,
4157 const struct wined3d_shader_signature_element *output_signature,
4158 const struct wined3d_shader_reg_maps *reg_maps_out)
4161 const char *semantic_name_in;
4162 UINT semantic_idx_in;
4165 unsigned int in_count = vec4_varyings(3, gl_info);
4167 char destination[50];
4168 WORD input_map, output_map;
4170 set = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*set) * (in_count + 2));
4172 input_map = reg_maps_in->input_registers;
4173 for (i = 0; input_map; input_map >>= 1, ++i)
4175 if (!(input_map & 1)) continue;
4178 /* Declared, but not read register */
4179 if (in_idx == ~0U) continue;
4180 if (in_idx >= (in_count + 2))
4182 FIXME("More input varyings declared than supported, expect issues.\n");
4186 if (in_idx == in_count)
4187 sprintf(destination, "gl_FrontColor");
4188 else if (in_idx == in_count + 1)
4189 sprintf(destination, "gl_FrontSecondaryColor");
4191 sprintf(destination, "ps_in[%u]", in_idx);
4193 semantic_name_in = input_signature[i].semantic_name;
4194 semantic_idx_in = input_signature[i].semantic_idx;
4197 output_map = reg_maps_out->output_registers;
4198 for (j = 0; output_map; output_map >>= 1, ++j)
4202 if (!(output_map & 1)
4203 || semantic_idx_in != output_signature[j].semantic_idx
4204 || strcmp(semantic_name_in, output_signature[j].semantic_name)
4205 || !(mask = input_signature[i].mask & output_signature[j].mask))
4209 shader_glsl_write_mask_to_str(mask, reg_mask);
4211 shader_addline(buffer, "%s%s = vs_out[%u]%s;\n",
4212 destination, reg_mask, j, reg_mask);
4216 for (i = 0; i < in_count + 2; ++i)
4220 if (!set[i] || set[i] == WINED3DSP_WRITEMASK_ALL)
4223 if (set[i] == ~0U) set[i] = 0;
4226 if (!(set[i] & WINED3DSP_WRITEMASK_0)) reg_mask[size++] = 'x';
4227 if (!(set[i] & WINED3DSP_WRITEMASK_1)) reg_mask[size++] = 'y';
4228 if (!(set[i] & WINED3DSP_WRITEMASK_2)) reg_mask[size++] = 'z';
4229 if (!(set[i] & WINED3DSP_WRITEMASK_3)) reg_mask[size++] = 'w';
4230 reg_mask[size] = '\0';
4233 sprintf(destination, "gl_FrontColor");
4234 else if (i == in_count + 1)
4235 sprintf(destination, "gl_FrontSecondaryColor");
4237 sprintf(destination, "ps_in[%u]", i);
4239 if (size == 1) shader_addline(buffer, "%s.%s = 0.0;\n", destination, reg_mask);
4240 else shader_addline(buffer, "%s.%s = vec%u(0.0);\n", destination, reg_mask, size);
4243 HeapFree(GetProcessHeap(), 0, set);
4246 /* GL locking is done by the caller */
4247 static GLhandleARB generate_param_reorder_function(struct wined3d_shader_buffer *buffer,
4248 const struct wined3d_shader *vs, const struct wined3d_shader *ps,
4249 const struct wined3d_gl_info *gl_info)
4251 GLhandleARB ret = 0;
4252 DWORD ps_major = ps ? ps->reg_maps.shader_version.major : 0;
4254 const char *semantic_name;
4257 const struct wined3d_shader_signature_element *output_signature = vs->output_signature;
4258 WORD map = vs->reg_maps.output_registers;
4260 shader_buffer_clear(buffer);
4262 shader_addline(buffer, "#version 120\n");
4266 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4268 for (i = 0; map; map >>= 1, ++i)
4272 if (!(map & 1)) continue;
4274 semantic_name = output_signature[i].semantic_name;
4275 semantic_idx = output_signature[i].semantic_idx;
4276 write_mask = output_signature[i].mask;
4277 shader_glsl_write_mask_to_str(write_mask, reg_mask);
4279 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_COLOR))
4282 shader_addline(buffer, "gl_FrontColor%s = vs_out[%u]%s;\n",
4283 reg_mask, i, reg_mask);
4284 else if (semantic_idx == 1)
4285 shader_addline(buffer, "gl_FrontSecondaryColor%s = vs_out[%u]%s;\n",
4286 reg_mask, i, reg_mask);
4288 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4290 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4291 reg_mask, i, reg_mask);
4293 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_TEXCOORD))
4295 if (semantic_idx < 8)
4297 if (!(gl_info->quirks & WINED3D_QUIRK_SET_TEXCOORD_W) || ps_major > 0)
4298 write_mask |= WINED3DSP_WRITEMASK_3;
4300 shader_addline(buffer, "gl_TexCoord[%u]%s = vs_out[%u]%s;\n",
4301 semantic_idx, reg_mask, i, reg_mask);
4302 if (!(write_mask & WINED3DSP_WRITEMASK_3))
4303 shader_addline(buffer, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx);
4306 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4308 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4310 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_FOG))
4312 shader_addline(buffer, "gl_FogFragCoord = clamp(vs_out[%u].%c, 0.0, 1.0);\n", i, reg_mask[1]);
4315 shader_addline(buffer, "}\n");
4319 UINT in_count = min(vec4_varyings(ps_major, gl_info), ps->limits.packed_input);
4320 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
4321 shader_addline(buffer, "varying vec4 ps_in[%u];\n", in_count);
4322 shader_addline(buffer, "void order_ps_input(in vec4 vs_out[%u])\n{\n", vs->limits.packed_output);
4324 /* First, sort out position and point size. Those are not passed to the pixel shader */
4325 for (i = 0; map; map >>= 1, ++i)
4327 if (!(map & 1)) continue;
4329 semantic_name = output_signature[i].semantic_name;
4330 shader_glsl_write_mask_to_str(output_signature[i].mask, reg_mask);
4332 if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_POSITION))
4334 shader_addline(buffer, "gl_Position%s = vs_out[%u]%s;\n",
4335 reg_mask, i, reg_mask);
4337 else if (shader_match_semantic(semantic_name, WINED3D_DECL_USAGE_PSIZE))
4339 shader_addline(buffer, "gl_PointSize = vs_out[%u].%c;\n", i, reg_mask[1]);
4343 /* Then, fix the pixel shader input */
4344 handle_ps3_input(buffer, gl_info, ps->u.ps.input_reg_map, ps->input_signature,
4345 &ps->reg_maps, output_signature, &vs->reg_maps);
4347 shader_addline(buffer, "}\n");
4350 ret = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4351 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4352 shader_glsl_compile(gl_info, ret, buffer->buffer);
4357 /* GL locking is done by the caller */
4358 static void hardcode_local_constants(const struct wined3d_shader *shader,
4359 const struct wined3d_gl_info *gl_info, GLhandleARB programId, const char *prefix)
4361 const struct wined3d_shader_lconst *lconst;
4366 LIST_FOR_EACH_ENTRY(lconst, &shader->constantsF, struct wined3d_shader_lconst, entry)
4368 value = (const float *)lconst->value;
4369 snprintf(glsl_name, sizeof(glsl_name), "%s_lc%u", prefix, lconst->idx);
4370 tmp_loc = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4371 GL_EXTCALL(glUniform4fvARB(tmp_loc, 1, value));
4373 checkGLcall("Hardcoding local constants");
4376 /* GL locking is done by the caller */
4377 static GLuint shader_glsl_generate_pshader(const struct wined3d_context *context,
4378 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4379 const struct ps_compile_args *args, struct ps_np2fixup_info *np2fixup_info)
4381 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4382 const struct wined3d_gl_info *gl_info = context->gl_info;
4383 const DWORD *function = shader->function;
4384 struct shader_glsl_ctx_priv priv_ctx;
4386 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4387 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4389 memset(&priv_ctx, 0, sizeof(priv_ctx));
4390 priv_ctx.cur_ps_args = args;
4391 priv_ctx.cur_np2fixup_info = np2fixup_info;
4393 shader_addline(buffer, "#version 120\n");
4395 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4396 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4397 if (gl_info->supported[ARB_SHADER_TEXTURE_LOD])
4398 shader_addline(buffer, "#extension GL_ARB_shader_texture_lod : enable\n");
4399 /* The spec says that it doesn't have to be explicitly enabled, but the
4400 * nvidia drivers write a warning if we don't do so. */
4401 if (gl_info->supported[ARB_TEXTURE_RECTANGLE])
4402 shader_addline(buffer, "#extension GL_ARB_texture_rectangle : enable\n");
4403 if (gl_info->supported[EXT_GPU_SHADER4])
4404 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4406 /* Base Declarations */
4407 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4409 /* Pack 3.0 inputs */
4410 if (reg_maps->shader_version.major >= 3 && args->vp_mode != vertexshader)
4411 shader_glsl_input_pack(shader, buffer, shader->input_signature, reg_maps, args->vp_mode);
4413 /* Base Shader Body */
4414 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4416 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4417 if (reg_maps->shader_version.major < 2)
4419 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4420 shader_addline(buffer, "gl_FragData[0] = R0;\n");
4423 if (args->srgb_correction)
4425 shader_addline(buffer, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4426 shader_addline(buffer, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4427 shader_addline(buffer, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4428 shader_addline(buffer, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4429 shader_addline(buffer, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4430 shader_addline(buffer, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4432 /* Pixel shader < 3.0 do not replace the fog stage.
4433 * This implements linear fog computation and blending.
4434 * TODO: non linear fog
4435 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4436 * -1/(e-s) and e/(e-s) respectively.
4438 if (reg_maps->shader_version.major < 3)
4441 case FOG_OFF: break;
4443 shader_addline(buffer, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4444 shader_addline(buffer, "float fogend = gl_Fog.end * -fogstart;\n");
4445 shader_addline(buffer, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4446 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4449 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4450 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4451 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4452 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4455 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4456 shader_addline(buffer, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4457 shader_addline(buffer, "Fog = clamp(Fog, 0.0, 1.0);\n");
4458 shader_addline(buffer, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4463 shader_addline(buffer, "}\n");
4465 TRACE("Compiling shader object %u\n", shader_obj);
4466 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4468 /* Store the shader object */
4472 /* GL locking is done by the caller */
4473 static GLuint shader_glsl_generate_vshader(const struct wined3d_context *context,
4474 struct wined3d_shader_buffer *buffer, const struct wined3d_shader *shader,
4475 const struct vs_compile_args *args)
4477 const struct wined3d_shader_reg_maps *reg_maps = &shader->reg_maps;
4478 const struct wined3d_gl_info *gl_info = context->gl_info;
4479 const DWORD *function = shader->function;
4480 struct shader_glsl_ctx_priv priv_ctx;
4482 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4483 GLhandleARB shader_obj = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4485 shader_addline(buffer, "#version 120\n");
4487 if (gl_info->supported[ARB_SHADER_BIT_ENCODING])
4488 shader_addline(buffer, "#extension GL_ARB_shader_bit_encoding : enable\n");
4489 if (gl_info->supported[EXT_GPU_SHADER4])
4490 shader_addline(buffer, "#extension GL_EXT_gpu_shader4 : enable\n");
4492 memset(&priv_ctx, 0, sizeof(priv_ctx));
4493 priv_ctx.cur_vs_args = args;
4495 /* Base Declarations */
4496 shader_generate_glsl_declarations(context, buffer, shader, reg_maps, &priv_ctx);
4498 /* Base Shader Body */
4499 shader_generate_main(shader, buffer, reg_maps, function, &priv_ctx);
4501 /* Unpack outputs */
4502 shader_addline(buffer, "order_ps_input(vs_out);\n");
4504 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4505 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4506 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4507 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4509 if (args->fog_src == VS_FOG_Z)
4510 shader_addline(buffer, "gl_FogFragCoord = gl_Position.z;\n");
4511 else if (!reg_maps->fog)
4512 shader_addline(buffer, "gl_FogFragCoord = 0.0;\n");
4514 /* We always store the clipplanes without y inversion */
4515 if (args->clip_enabled)
4516 shader_addline(buffer, "gl_ClipVertex = gl_Position;\n");
4518 /* Write the final position.
4520 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4521 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4522 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4523 * contains 1.0 to allow a mad.
4525 shader_addline(buffer, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4526 shader_addline(buffer, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4528 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4530 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4531 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4532 * which is the same as z = z * 2 - w.
4534 shader_addline(buffer, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4536 shader_addline(buffer, "}\n");
4538 TRACE("Compiling shader object %u\n", shader_obj);
4539 shader_glsl_compile(gl_info, shader_obj, buffer->buffer);
4544 static GLhandleARB find_glsl_pshader(const struct wined3d_context *context,
4545 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4546 const struct ps_compile_args *args, const struct ps_np2fixup_info **np2fixup_info)
4548 struct wined3d_state *state = &shader->device->stateBlock->state;
4549 struct glsl_ps_compiled_shader *gl_shaders, *new_array;
4550 struct glsl_shader_private *shader_data;
4551 struct ps_np2fixup_info *np2fixup;
4556 if (!shader->backend_data)
4558 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4559 if (!shader->backend_data)
4561 ERR("Failed to allocate backend data.\n");
4565 shader_data = shader->backend_data;
4566 gl_shaders = shader_data->gl_shaders.ps;
4568 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4569 * so a linear search is more performant than a hashmap or a binary search
4570 * (cache coherency etc)
4572 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4574 if (!memcmp(&gl_shaders[i].args, args, sizeof(*args)))
4576 if (args->np2_fixup)
4577 *np2fixup_info = &gl_shaders[i].np2fixup;
4578 return gl_shaders[i].prgId;
4582 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4583 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4584 if (shader_data->num_gl_shaders)
4586 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4587 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.ps,
4588 new_size * sizeof(*gl_shaders));
4592 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4597 ERR("Out of memory\n");
4600 shader_data->gl_shaders.ps = new_array;
4601 shader_data->shader_array_size = new_size;
4602 gl_shaders = new_array;
4605 gl_shaders[shader_data->num_gl_shaders].args = *args;
4607 np2fixup = &gl_shaders[shader_data->num_gl_shaders].np2fixup;
4608 memset(np2fixup, 0, sizeof(*np2fixup));
4609 *np2fixup_info = args->np2_fixup ? np2fixup : NULL;
4611 pixelshader_update_samplers(&shader->reg_maps, state->textures);
4613 shader_buffer_clear(buffer);
4614 ret = shader_glsl_generate_pshader(context, buffer, shader, args, np2fixup);
4615 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4620 static inline BOOL vs_args_equal(const struct vs_compile_args *stored, const struct vs_compile_args *new,
4621 const DWORD use_map) {
4622 if((stored->swizzle_map & use_map) != new->swizzle_map) return FALSE;
4623 if((stored->clip_enabled) != new->clip_enabled) return FALSE;
4624 return stored->fog_src == new->fog_src;
4627 static GLhandleARB find_glsl_vshader(const struct wined3d_context *context,
4628 struct wined3d_shader_buffer *buffer, struct wined3d_shader *shader,
4629 const struct vs_compile_args *args)
4633 DWORD use_map = shader->device->strided_streams.use_map;
4634 struct glsl_vs_compiled_shader *gl_shaders, *new_array;
4635 struct glsl_shader_private *shader_data;
4638 if (!shader->backend_data)
4640 shader->backend_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*shader_data));
4641 if (!shader->backend_data)
4643 ERR("Failed to allocate backend data.\n");
4647 shader_data = shader->backend_data;
4648 gl_shaders = shader_data->gl_shaders.vs;
4650 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4651 * so a linear search is more performant than a hashmap or a binary search
4652 * (cache coherency etc)
4654 for (i = 0; i < shader_data->num_gl_shaders; ++i)
4656 if (vs_args_equal(&gl_shaders[i].args, args, use_map))
4657 return gl_shaders[i].prgId;
4660 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader);
4662 if(shader_data->shader_array_size == shader_data->num_gl_shaders) {
4663 if (shader_data->num_gl_shaders)
4665 new_size = shader_data->shader_array_size + max(1, shader_data->shader_array_size / 2);
4666 new_array = HeapReAlloc(GetProcessHeap(), 0, shader_data->gl_shaders.vs,
4667 new_size * sizeof(*gl_shaders));
4671 new_array = HeapAlloc(GetProcessHeap(), 0, sizeof(*gl_shaders));
4676 ERR("Out of memory\n");
4679 shader_data->gl_shaders.vs = new_array;
4680 shader_data->shader_array_size = new_size;
4681 gl_shaders = new_array;
4684 gl_shaders[shader_data->num_gl_shaders].args = *args;
4686 shader_buffer_clear(buffer);
4687 ret = shader_glsl_generate_vshader(context, buffer, shader, args);
4688 gl_shaders[shader_data->num_gl_shaders++].prgId = ret;
4693 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4694 * It sets the programId on the current StateBlock (because it should be called
4695 * inside of the DrawPrimitive() part of the render loop).
4697 * If a program for the given combination does not exist, create one, and store
4698 * the program in the hash table. If it creates a program, it will link the
4699 * given objects, too.
4702 /* GL locking is done by the caller */
4703 static void set_glsl_shader_program(const struct wined3d_context *context,
4704 struct wined3d_device *device, BOOL use_ps, BOOL use_vs)
4706 const struct wined3d_state *state = &device->stateBlock->state;
4707 struct wined3d_shader *vshader = use_vs ? state->vertex_shader : NULL;
4708 struct wined3d_shader *pshader = use_ps ? state->pixel_shader : NULL;
4709 const struct wined3d_gl_info *gl_info = context->gl_info;
4710 struct shader_glsl_priv *priv = device->shader_priv;
4711 struct glsl_shader_prog_link *entry = NULL;
4712 GLhandleARB programId = 0;
4713 GLhandleARB reorder_shader_id = 0;
4716 struct ps_compile_args ps_compile_args;
4717 struct vs_compile_args vs_compile_args;
4719 if (vshader) find_vs_compile_args(state, vshader, &vs_compile_args);
4720 if (pshader) find_ps_compile_args(state, pshader, &ps_compile_args);
4722 entry = get_glsl_program_entry(priv, vshader, pshader, &vs_compile_args, &ps_compile_args);
4725 priv->glsl_program = entry;
4729 /* If we get to this point, then no matching program exists, so we create one */
4730 programId = GL_EXTCALL(glCreateProgramObjectARB());
4731 TRACE("Created new GLSL shader program %u\n", programId);
4733 /* Create the entry */
4734 entry = HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link));
4735 entry->programId = programId;
4736 entry->vshader = vshader;
4737 entry->pshader = pshader;
4738 entry->vs_args = vs_compile_args;
4739 entry->ps_args = ps_compile_args;
4740 entry->constant_version = 0;
4741 entry->np2Fixup_info = NULL;
4742 /* Add the hash table entry */
4743 add_glsl_program_entry(priv, entry);
4745 /* Set the current program */
4746 priv->glsl_program = entry;
4748 /* Attach GLSL vshader */
4751 GLhandleARB vshader_id = find_glsl_vshader(context, &priv->shader_buffer, vshader, &vs_compile_args);
4752 WORD map = vshader->reg_maps.input_registers;
4755 reorder_shader_id = generate_param_reorder_function(&priv->shader_buffer, vshader, pshader, gl_info);
4756 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id, programId);
4757 GL_EXTCALL(glAttachObjectARB(programId, reorder_shader_id));
4758 checkGLcall("glAttachObjectARB");
4759 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4762 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id));
4764 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id, programId);
4765 GL_EXTCALL(glAttachObjectARB(programId, vshader_id));
4766 checkGLcall("glAttachObjectARB");
4768 /* Bind vertex attributes to a corresponding index number to match
4769 * the same index numbers as ARB_vertex_programs (makes loading
4770 * vertex attributes simpler). With this method, we can use the
4771 * exact same code to load the attributes later for both ARB and
4774 * We have to do this here because we need to know the Program ID
4775 * in order to make the bindings work, and it has to be done prior
4776 * to linking the GLSL program. */
4777 for (i = 0; map; map >>= 1, ++i)
4779 if (!(map & 1)) continue;
4781 snprintf(tmp_name, sizeof(tmp_name), "vs_in%u", i);
4782 GL_EXTCALL(glBindAttribLocationARB(programId, i, tmp_name));
4784 checkGLcall("glBindAttribLocationARB");
4786 list_add_head(&vshader->linked_programs, &entry->vshader_entry);
4789 /* Attach GLSL pshader */
4792 GLhandleARB pshader_id = find_glsl_pshader(context, &priv->shader_buffer,
4793 pshader, &ps_compile_args, &entry->np2Fixup_info);
4794 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id, programId);
4795 GL_EXTCALL(glAttachObjectARB(programId, pshader_id));
4796 checkGLcall("glAttachObjectARB");
4798 list_add_head(&pshader->linked_programs, &entry->pshader_entry);
4801 /* Link the program */
4802 TRACE("Linking GLSL shader program %u\n", programId);
4803 GL_EXTCALL(glLinkProgramARB(programId));
4804 shader_glsl_validate_link(gl_info, programId);
4806 entry->vuniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4807 sizeof(GLhandleARB) * gl_info->limits.glsl_vs_float_constants);
4808 for (i = 0; i < gl_info->limits.glsl_vs_float_constants; ++i)
4810 snprintf(glsl_name, sizeof(glsl_name), "vs_c[%u]", i);
4811 entry->vuniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4813 for (i = 0; i < MAX_CONST_I; ++i)
4815 snprintf(glsl_name, sizeof(glsl_name), "vs_i[%u]", i);
4816 entry->vuniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4818 entry->puniformF_locations = HeapAlloc(GetProcessHeap(), 0,
4819 sizeof(GLhandleARB) * gl_info->limits.glsl_ps_float_constants);
4820 for (i = 0; i < gl_info->limits.glsl_ps_float_constants; ++i)
4822 snprintf(glsl_name, sizeof(glsl_name), "ps_c[%u]", i);
4823 entry->puniformF_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4825 for (i = 0; i < MAX_CONST_I; ++i)
4827 snprintf(glsl_name, sizeof(glsl_name), "ps_i[%u]", i);
4828 entry->puniformI_locations[i] = GL_EXTCALL(glGetUniformLocationARB(programId, glsl_name));
4834 for(i = 0; i < MAX_TEXTURES; i++) {
4835 sprintf(name, "bumpenvmat%u", i);
4836 entry->bumpenvmat_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4837 sprintf(name, "luminancescale%u", i);
4838 entry->luminancescale_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4839 sprintf(name, "luminanceoffset%u", i);
4840 entry->luminanceoffset_location[i] = GL_EXTCALL(glGetUniformLocationARB(programId, name));
4843 if (ps_compile_args.np2_fixup)
4845 if (entry->np2Fixup_info)
4846 entry->np2Fixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ps_samplerNP2Fixup"));
4848 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4852 entry->posFixup_location = GL_EXTCALL(glGetUniformLocationARB(programId, "posFixup"));
4853 entry->ycorrection_location = GL_EXTCALL(glGetUniformLocationARB(programId, "ycorrection"));
4854 checkGLcall("Find glsl program uniform locations");
4856 if (pshader && pshader->reg_maps.shader_version.major >= 3
4857 && pshader->u.ps.declared_in_count > vec4_varyings(3, gl_info))
4859 TRACE("Shader %d needs vertex color clamping disabled\n", programId);
4860 entry->vertex_color_clamp = GL_FALSE;
4862 entry->vertex_color_clamp = GL_FIXED_ONLY_ARB;
4865 /* Set the shader to allow uniform loading on it */
4866 GL_EXTCALL(glUseProgramObjectARB(programId));
4867 checkGLcall("glUseProgramObjectARB(programId)");
4869 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4870 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4871 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4872 * vertex shader with fixed function pixel processing is used we make sure that the card
4873 * supports enough samplers to allow the max number of vertex samplers with all possible
4874 * fixed function fragment processing setups. So once the program is linked these samplers
4877 if (vshader) shader_glsl_load_vsamplers(gl_info, device->texUnitMap, programId);
4878 if (pshader) shader_glsl_load_psamplers(gl_info, device->texUnitMap, programId);
4880 /* If the local constants do not have to be loaded with the environment constants,
4881 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4884 if (pshader && !pshader->load_local_constsF)
4885 hardcode_local_constants(pshader, gl_info, programId, "ps");
4886 if (vshader && !vshader->load_local_constsF)
4887 hardcode_local_constants(vshader, gl_info, programId, "vs");
4890 /* GL locking is done by the caller */
4891 static GLhandleARB create_glsl_blt_shader(const struct wined3d_gl_info *gl_info, enum tex_types tex_type, BOOL masked)
4893 GLhandleARB program_id;
4894 GLhandleARB vshader_id, pshader_id;
4895 const char *blt_pshader;
4897 static const char *blt_vshader =
4901 " gl_Position = gl_Vertex;\n"
4902 " gl_FrontColor = vec4(1.0);\n"
4903 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4906 static const char * const blt_pshaders_full[tex_type_count] =
4912 "uniform sampler2D sampler;\n"
4915 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4921 "uniform samplerCube sampler;\n"
4924 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4928 "#extension GL_ARB_texture_rectangle : enable\n"
4929 "uniform sampler2DRect sampler;\n"
4932 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4936 static const char * const blt_pshaders_masked[tex_type_count] =
4942 "uniform sampler2D sampler;\n"
4943 "uniform vec4 mask;\n"
4946 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4947 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4953 "uniform samplerCube sampler;\n"
4954 "uniform vec4 mask;\n"
4957 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4958 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4962 "#extension GL_ARB_texture_rectangle : enable\n"
4963 "uniform sampler2DRect sampler;\n"
4964 "uniform vec4 mask;\n"
4967 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4968 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4972 blt_pshader = masked ? blt_pshaders_masked[tex_type] : blt_pshaders_full[tex_type];
4975 FIXME("tex_type %#x not supported\n", tex_type);
4979 vshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB));
4980 shader_glsl_compile(gl_info, vshader_id, blt_vshader);
4982 pshader_id = GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB));
4983 shader_glsl_compile(gl_info, pshader_id, blt_pshader);
4985 program_id = GL_EXTCALL(glCreateProgramObjectARB());
4986 GL_EXTCALL(glAttachObjectARB(program_id, vshader_id));
4987 GL_EXTCALL(glAttachObjectARB(program_id, pshader_id));
4988 GL_EXTCALL(glLinkProgramARB(program_id));
4990 shader_glsl_validate_link(gl_info, program_id);
4992 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4995 GL_EXTCALL(glDeleteObjectARB(vshader_id));
4996 GL_EXTCALL(glDeleteObjectARB(pshader_id));
5000 /* GL locking is done by the caller */
5001 static void shader_glsl_select(const struct wined3d_context *context, BOOL usePS, BOOL useVS)
5003 const struct wined3d_gl_info *gl_info = context->gl_info;
5004 struct wined3d_device *device = context->swapchain->device;
5005 struct shader_glsl_priv *priv = device->shader_priv;
5006 GLhandleARB program_id = 0;
5007 GLenum old_vertex_color_clamp, current_vertex_color_clamp;
5009 old_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
5011 if (useVS || usePS) set_glsl_shader_program(context, device, usePS, useVS);
5012 else priv->glsl_program = NULL;
5014 current_vertex_color_clamp = priv->glsl_program ? priv->glsl_program->vertex_color_clamp : GL_FIXED_ONLY_ARB;
5016 if (old_vertex_color_clamp != current_vertex_color_clamp)
5018 if (gl_info->supported[ARB_COLOR_BUFFER_FLOAT])
5020 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB, current_vertex_color_clamp));
5021 checkGLcall("glClampColorARB");
5025 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
5029 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
5030 if (program_id) TRACE("Using GLSL program %u\n", program_id);
5031 GL_EXTCALL(glUseProgramObjectARB(program_id));
5032 checkGLcall("glUseProgramObjectARB");
5034 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
5035 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
5036 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
5037 if (priv->glsl_program && priv->glsl_program->np2Fixup_info)
5039 shader_glsl_load_np2fixup_constants(priv, gl_info, &device->stateBlock->state);
5043 /* GL locking is done by the caller */
5044 static void shader_glsl_select_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info,
5045 enum tex_types tex_type, const SIZE *ds_mask_size)
5047 BOOL masked = ds_mask_size->cx && ds_mask_size->cy;
5048 struct shader_glsl_priv *priv = shader_priv;
5049 GLhandleARB *blt_program;
5052 blt_program = masked ? &priv->depth_blt_program_masked[tex_type] : &priv->depth_blt_program_full[tex_type];
5055 *blt_program = create_glsl_blt_shader(gl_info, tex_type, masked);
5056 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "sampler"));
5057 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
5058 GL_EXTCALL(glUniform1iARB(loc, 0));
5062 GL_EXTCALL(glUseProgramObjectARB(*blt_program));
5067 loc = GL_EXTCALL(glGetUniformLocationARB(*blt_program, "mask"));
5068 GL_EXTCALL(glUniform4fARB(loc, 0.0f, 0.0f, (float)ds_mask_size->cx, (float)ds_mask_size->cy));
5072 /* GL locking is done by the caller */
5073 static void shader_glsl_deselect_depth_blt(void *shader_priv, const struct wined3d_gl_info *gl_info)
5075 struct shader_glsl_priv *priv = shader_priv;
5076 GLhandleARB program_id;
5078 program_id = priv->glsl_program ? priv->glsl_program->programId : 0;
5079 if (program_id) TRACE("Using GLSL program %u\n", program_id);
5081 GL_EXTCALL(glUseProgramObjectARB(program_id));
5082 checkGLcall("glUseProgramObjectARB");
5085 static void shader_glsl_destroy(struct wined3d_shader *shader)
5087 struct glsl_shader_private *shader_data = shader->backend_data;
5088 struct wined3d_device *device = shader->device;
5089 struct shader_glsl_priv *priv = device->shader_priv;
5090 const struct wined3d_gl_info *gl_info;
5091 const struct list *linked_programs;
5092 struct wined3d_context *context;
5094 if (!shader_data || !shader_data->num_gl_shaders)
5096 HeapFree(GetProcessHeap(), 0, shader_data);
5097 shader->backend_data = NULL;
5101 context = context_acquire(device, NULL);
5102 gl_info = context->gl_info;
5104 if (priv->glsl_program && (priv->glsl_program->vshader == shader
5105 || priv->glsl_program->pshader == shader))
5108 shader_glsl_select(context, FALSE, FALSE);
5112 TRACE("Deleting linked programs.\n");
5113 linked_programs = &shader->linked_programs;
5114 if (linked_programs->next)
5116 struct glsl_shader_prog_link *entry, *entry2;
5121 switch (shader->reg_maps.shader_version.type)
5123 case WINED3D_SHADER_TYPE_PIXEL:
5125 struct glsl_ps_compiled_shader *gl_shaders = shader_data->gl_shaders.ps;
5127 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
5128 struct glsl_shader_prog_link, pshader_entry)
5130 delete_glsl_program_entry(priv, gl_info, entry);
5133 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5135 TRACE("Deleting pixel shader %u.\n", gl_shaders[i].prgId);
5136 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
5137 checkGLcall("glDeleteObjectARB");
5139 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.ps);
5144 case WINED3D_SHADER_TYPE_VERTEX:
5146 struct glsl_vs_compiled_shader *gl_shaders = shader_data->gl_shaders.vs;
5148 LIST_FOR_EACH_ENTRY_SAFE(entry, entry2, linked_programs,
5149 struct glsl_shader_prog_link, vshader_entry)
5151 delete_glsl_program_entry(priv, gl_info, entry);
5154 for (i = 0; i < shader_data->num_gl_shaders; ++i)
5156 TRACE("Deleting vertex shader %u.\n", gl_shaders[i].prgId);
5157 GL_EXTCALL(glDeleteObjectARB(gl_shaders[i].prgId));
5158 checkGLcall("glDeleteObjectARB");
5160 HeapFree(GetProcessHeap(), 0, shader_data->gl_shaders.vs);
5166 ERR("Unhandled shader type %#x.\n", shader->reg_maps.shader_version.type);
5173 HeapFree(GetProcessHeap(), 0, shader->backend_data);
5174 shader->backend_data = NULL;
5176 context_release(context);
5179 static int glsl_program_key_compare(const void *key, const struct wine_rb_entry *entry)
5181 const struct glsl_program_key *k = key;
5182 const struct glsl_shader_prog_link *prog = WINE_RB_ENTRY_VALUE(entry,
5183 const struct glsl_shader_prog_link, program_lookup_entry);
5186 if (k->vshader > prog->vshader) return 1;
5187 else if (k->vshader < prog->vshader) return -1;
5189 if (k->pshader > prog->pshader) return 1;
5190 else if (k->pshader < prog->pshader) return -1;
5192 if (k->vshader && (cmp = memcmp(&k->vs_args, &prog->vs_args, sizeof(prog->vs_args)))) return cmp;
5193 if (k->pshader && (cmp = memcmp(&k->ps_args, &prog->ps_args, sizeof(prog->ps_args)))) return cmp;
5198 static BOOL constant_heap_init(struct constant_heap *heap, unsigned int constant_count)
5200 SIZE_T size = (constant_count + 1) * sizeof(*heap->entries) + constant_count * sizeof(*heap->positions);
5201 void *mem = HeapAlloc(GetProcessHeap(), 0, size);
5205 ERR("Failed to allocate memory\n");
5209 heap->entries = mem;
5210 heap->entries[1].version = 0;
5211 heap->positions = (unsigned int *)(heap->entries + constant_count + 1);
5217 static void constant_heap_free(struct constant_heap *heap)
5219 HeapFree(GetProcessHeap(), 0, heap->entries);
5222 static const struct wine_rb_functions wined3d_glsl_program_rb_functions =
5227 glsl_program_key_compare,
5230 static HRESULT shader_glsl_alloc(struct wined3d_device *device, const struct fragment_pipeline *fragment_pipe)
5232 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5233 struct shader_glsl_priv *priv = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct shader_glsl_priv));
5234 SIZE_T stack_size = wined3d_log2i(max(gl_info->limits.glsl_vs_float_constants,
5235 gl_info->limits.glsl_ps_float_constants)) + 1;
5236 void *fragment_priv;
5238 if (!(fragment_priv = fragment_pipe->alloc_private(&glsl_shader_backend, priv)))
5240 ERR("Failed to initialize fragment pipe.\n");
5241 HeapFree(GetProcessHeap(), 0, priv);
5245 if (!shader_buffer_init(&priv->shader_buffer))
5247 ERR("Failed to initialize shader buffer.\n");
5251 priv->stack = HeapAlloc(GetProcessHeap(), 0, stack_size * sizeof(*priv->stack));
5254 ERR("Failed to allocate memory.\n");
5258 if (!constant_heap_init(&priv->vconst_heap, gl_info->limits.glsl_vs_float_constants))
5260 ERR("Failed to initialize vertex shader constant heap\n");
5264 if (!constant_heap_init(&priv->pconst_heap, gl_info->limits.glsl_ps_float_constants))
5266 ERR("Failed to initialize pixel shader constant heap\n");
5270 if (wine_rb_init(&priv->program_lookup, &wined3d_glsl_program_rb_functions) == -1)
5272 ERR("Failed to initialize rbtree.\n");
5276 priv->next_constant_version = 1;
5277 device->fragment_priv = fragment_priv;
5278 priv->fragment_pipe = fragment_pipe;
5280 device->shader_priv = priv;
5284 constant_heap_free(&priv->pconst_heap);
5285 constant_heap_free(&priv->vconst_heap);
5286 HeapFree(GetProcessHeap(), 0, priv->stack);
5287 shader_buffer_free(&priv->shader_buffer);
5288 fragment_pipe->free_private(device);
5289 HeapFree(GetProcessHeap(), 0, priv);
5290 return E_OUTOFMEMORY;
5293 /* Context activation is done by the caller. */
5294 static void shader_glsl_free(struct wined3d_device *device)
5296 const struct wined3d_gl_info *gl_info = &device->adapter->gl_info;
5297 struct shader_glsl_priv *priv = device->shader_priv;
5301 for (i = 0; i < tex_type_count; ++i)
5303 if (priv->depth_blt_program_full[i])
5305 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_full[i]));
5307 if (priv->depth_blt_program_masked[i])
5309 GL_EXTCALL(glDeleteObjectARB(priv->depth_blt_program_masked[i]));
5314 wine_rb_destroy(&priv->program_lookup, NULL, NULL);
5315 constant_heap_free(&priv->pconst_heap);
5316 constant_heap_free(&priv->vconst_heap);
5317 HeapFree(GetProcessHeap(), 0, priv->stack);
5318 shader_buffer_free(&priv->shader_buffer);
5319 priv->fragment_pipe->free_private(device);
5321 HeapFree(GetProcessHeap(), 0, device->shader_priv);
5322 device->shader_priv = NULL;
5325 static void shader_glsl_context_destroyed(void *shader_priv, const struct wined3d_context *context) {}
5327 static void shader_glsl_get_caps(const struct wined3d_gl_info *gl_info, struct shader_caps *caps)
5331 if (gl_info->supported[EXT_GPU_SHADER4] && gl_info->supported[ARB_SHADER_BIT_ENCODING]
5332 && gl_info->supported[ARB_GEOMETRY_SHADER4] && gl_info->glsl_version >= MAKEDWORD_VERSION(1, 50))
5334 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
5335 * texldd and texldl instructions. */
5336 else if (gl_info->supported[ARB_SHADER_TEXTURE_LOD] || gl_info->supported[EXT_GPU_SHADER4])
5340 TRACE("Shader model %u.\n", shader_model);
5342 caps->vs_version = shader_model;
5343 caps->gs_version = shader_model;
5344 caps->ps_version = shader_model;
5346 caps->vs_uniform_count = gl_info->limits.glsl_vs_float_constants;
5347 caps->ps_uniform_count = gl_info->limits.glsl_ps_float_constants;
5349 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
5350 * Direct3D minimum requirement.
5352 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
5353 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
5355 * The problem is that the refrast clamps temporary results in the shader to
5356 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
5357 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
5358 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
5359 * offer a way to query this.
5361 caps->ps_1x_max_value = 8.0;
5363 caps->vs_clipping = TRUE;
5366 static BOOL shader_glsl_color_fixup_supported(struct color_fixup_desc fixup)
5368 if (TRACE_ON(d3d_shader) && TRACE_ON(d3d))
5370 TRACE("Checking support for fixup:\n");
5371 dump_color_fixup_desc(fixup);
5374 /* We support everything except YUV conversions. */
5375 if (!is_complex_fixup(fixup))
5381 TRACE("[FAILED]\n");
5385 static const SHADER_HANDLER shader_glsl_instruction_handler_table[WINED3DSIH_TABLE_SIZE] =
5387 /* WINED3DSIH_ABS */ shader_glsl_map2gl,
5388 /* WINED3DSIH_ADD */ shader_glsl_binop,
5389 /* WINED3DSIH_AND */ shader_glsl_binop,
5390 /* WINED3DSIH_BEM */ shader_glsl_bem,
5391 /* WINED3DSIH_BREAK */ shader_glsl_break,
5392 /* WINED3DSIH_BREAKC */ shader_glsl_breakc,
5393 /* WINED3DSIH_BREAKP */ shader_glsl_breakp,
5394 /* WINED3DSIH_CALL */ shader_glsl_call,
5395 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz,
5396 /* WINED3DSIH_CMP */ shader_glsl_conditional_move,
5397 /* WINED3DSIH_CND */ shader_glsl_cnd,
5398 /* WINED3DSIH_CRS */ shader_glsl_cross,
5399 /* WINED3DSIH_CUT */ shader_glsl_cut,
5400 /* WINED3DSIH_DCL */ shader_glsl_nop,
5401 /* WINED3DSIH_DCL_CONSTANT_BUFFER */ shader_glsl_nop,
5402 /* WINED3DSIH_DCL_INPUT_PRIMITIVE */ shader_glsl_nop,
5403 /* WINED3DSIH_DCL_OUTPUT_TOPOLOGY */ shader_glsl_nop,
5404 /* WINED3DSIH_DCL_VERTICES_OUT */ shader_glsl_nop,
5405 /* WINED3DSIH_DEF */ shader_glsl_nop,
5406 /* WINED3DSIH_DEFB */ shader_glsl_nop,
5407 /* WINED3DSIH_DEFI */ shader_glsl_nop,
5408 /* WINED3DSIH_DIV */ shader_glsl_binop,
5409 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add,
5410 /* WINED3DSIH_DP3 */ shader_glsl_dot,
5411 /* WINED3DSIH_DP4 */ shader_glsl_dot,
5412 /* WINED3DSIH_DST */ shader_glsl_dst,
5413 /* WINED3DSIH_DSX */ shader_glsl_map2gl,
5414 /* WINED3DSIH_DSY */ shader_glsl_map2gl,
5415 /* WINED3DSIH_ELSE */ shader_glsl_else,
5416 /* WINED3DSIH_EMIT */ shader_glsl_emit,
5417 /* WINED3DSIH_ENDIF */ shader_glsl_end,
5418 /* WINED3DSIH_ENDLOOP */ shader_glsl_end,
5419 /* WINED3DSIH_ENDREP */ shader_glsl_end,
5420 /* WINED3DSIH_EQ */ shader_glsl_relop,
5421 /* WINED3DSIH_EXP */ shader_glsl_map2gl,
5422 /* WINED3DSIH_EXPP */ shader_glsl_expp,
5423 /* WINED3DSIH_FRC */ shader_glsl_map2gl,
5424 /* WINED3DSIH_FTOI */ shader_glsl_to_int,
5425 /* WINED3DSIH_GE */ shader_glsl_relop,
5426 /* WINED3DSIH_IADD */ shader_glsl_binop,
5427 /* WINED3DSIH_IEQ */ NULL,
5428 /* WINED3DSIH_IF */ shader_glsl_if,
5429 /* WINED3DSIH_IFC */ shader_glsl_ifc,
5430 /* WINED3DSIH_IGE */ shader_glsl_relop,
5431 /* WINED3DSIH_IMUL */ shader_glsl_imul,
5432 /* WINED3DSIH_ITOF */ shader_glsl_to_float,
5433 /* WINED3DSIH_LABEL */ shader_glsl_label,
5434 /* WINED3DSIH_LD */ NULL,
5435 /* WINED3DSIH_LIT */ shader_glsl_lit,
5436 /* WINED3DSIH_LOG */ shader_glsl_log,
5437 /* WINED3DSIH_LOGP */ shader_glsl_log,
5438 /* WINED3DSIH_LOOP */ shader_glsl_loop,
5439 /* WINED3DSIH_LRP */ shader_glsl_lrp,
5440 /* WINED3DSIH_LT */ shader_glsl_relop,
5441 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn,
5442 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn,
5443 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn,
5444 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn,
5445 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn,
5446 /* WINED3DSIH_MAD */ shader_glsl_mad,
5447 /* WINED3DSIH_MAX */ shader_glsl_map2gl,
5448 /* WINED3DSIH_MIN */ shader_glsl_map2gl,
5449 /* WINED3DSIH_MOV */ shader_glsl_mov,
5450 /* WINED3DSIH_MOVA */ shader_glsl_mov,
5451 /* WINED3DSIH_MOVC */ shader_glsl_conditional_move,
5452 /* WINED3DSIH_MUL */ shader_glsl_binop,
5453 /* WINED3DSIH_NOP */ shader_glsl_nop,
5454 /* WINED3DSIH_NRM */ shader_glsl_nrm,
5455 /* WINED3DSIH_PHASE */ shader_glsl_nop,
5456 /* WINED3DSIH_POW */ shader_glsl_pow,
5457 /* WINED3DSIH_RCP */ shader_glsl_rcp,
5458 /* WINED3DSIH_REP */ shader_glsl_rep,
5459 /* WINED3DSIH_RET */ shader_glsl_ret,
5460 /* WINED3DSIH_ROUND_NI */ shader_glsl_map2gl,
5461 /* WINED3DSIH_RSQ */ shader_glsl_rsq,
5462 /* WINED3DSIH_SAMPLE */ NULL,
5463 /* WINED3DSIH_SAMPLE_GRAD */ NULL,
5464 /* WINED3DSIH_SAMPLE_LOD */ NULL,
5465 /* WINED3DSIH_SETP */ NULL,
5466 /* WINED3DSIH_SGE */ shader_glsl_compare,
5467 /* WINED3DSIH_SGN */ shader_glsl_sgn,
5468 /* WINED3DSIH_SINCOS */ shader_glsl_sincos,
5469 /* WINED3DSIH_SLT */ shader_glsl_compare,
5470 /* WINED3DSIH_SQRT */ NULL,
5471 /* WINED3DSIH_SUB */ shader_glsl_binop,
5472 /* WINED3DSIH_TEX */ shader_glsl_tex,
5473 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem,
5474 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem,
5475 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord,
5476 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth,
5477 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3,
5478 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex,
5479 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill,
5480 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd,
5481 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl,
5482 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth,
5483 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad,
5484 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex,
5485 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3,
5486 /* WINED3DSIH_TEXM3x3DIFF */ NULL,
5487 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad,
5488 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec,
5489 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex,
5490 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec,
5491 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar,
5492 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb,
5493 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb,
5494 /* WINED3DSIH_UDIV */ shader_glsl_udiv,
5495 /* WINED3DSIH_USHR */ shader_glsl_binop,
5496 /* WINED3DSIH_UTOF */ shader_glsl_to_float,
5497 /* WINED3DSIH_XOR */ shader_glsl_binop,
5500 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction *ins) {
5501 SHADER_HANDLER hw_fct;
5503 /* Select handler */
5504 hw_fct = shader_glsl_instruction_handler_table[ins->handler_idx];
5506 /* Unhandled opcode */
5509 FIXME("Backend can't handle opcode %#x\n", ins->handler_idx);
5514 shader_glsl_add_instruction_modifiers(ins);
5517 static void shader_glsl_enable_fragment_pipe(void *shader_priv,
5518 const struct wined3d_gl_info *gl_info, BOOL enable)
5520 struct shader_glsl_priv *priv = shader_priv;
5522 priv->fragment_pipe->enable_extension(gl_info, enable);
5525 static BOOL shader_glsl_has_ffp_proj_control(void *shader_priv)
5527 struct shader_glsl_priv *priv = shader_priv;
5529 return priv->fragment_pipe->ffp_proj_control;
5532 const struct wined3d_shader_backend_ops glsl_shader_backend =
5534 shader_glsl_handle_instruction,
5536 shader_glsl_select_depth_blt,
5537 shader_glsl_deselect_depth_blt,
5538 shader_glsl_update_float_vertex_constants,
5539 shader_glsl_update_float_pixel_constants,
5540 shader_glsl_load_constants,
5541 shader_glsl_load_np2fixup_constants,
5542 shader_glsl_destroy,
5545 shader_glsl_context_destroyed,
5546 shader_glsl_get_caps,
5547 shader_glsl_color_fixup_supported,
5548 shader_glsl_enable_fragment_pipe,
5549 shader_glsl_has_ffp_proj_control,