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.
35 #include "wined3d_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(d3d_shader
);
38 WINE_DECLARE_DEBUG_CHANNEL(d3d_constants
);
39 WINE_DECLARE_DEBUG_CHANNEL(d3d_caps
);
40 WINE_DECLARE_DEBUG_CHANNEL(d3d
);
42 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
43 #define WINED3D_GLSL_SAMPLE_RECT 0x2
44 #define WINED3D_GLSL_SAMPLE_LOD 0x4
45 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
59 struct glsl_sample_function
67 HEAP_NODE_TRAVERSE_LEFT
,
68 HEAP_NODE_TRAVERSE_RIGHT
,
80 struct constant_entry
*entries
;
81 unsigned int *positions
;
85 /* GLSL shader private data */
86 struct shader_glsl_priv
{
87 struct wined3d_shader_buffer shader_buffer
;
88 struct wine_rb_tree program_lookup
;
89 struct glsl_shader_prog_link
*glsl_program
;
90 struct constant_heap vconst_heap
;
91 struct constant_heap pconst_heap
;
93 GLhandleARB depth_blt_program_full
[tex_type_count
];
94 GLhandleARB depth_blt_program_masked
[tex_type_count
];
95 UINT next_constant_version
;
98 /* Struct to maintain data about a linked GLSL program */
99 struct glsl_shader_prog_link
{
100 struct wine_rb_entry program_lookup_entry
;
101 struct list vshader_entry
;
102 struct list pshader_entry
;
103 GLhandleARB programId
;
104 GLint
*vuniformF_locations
;
105 GLint
*puniformF_locations
;
106 GLint vuniformI_locations
[MAX_CONST_I
];
107 GLint puniformI_locations
[MAX_CONST_I
];
108 GLint posFixup_location
;
109 GLint np2Fixup_location
;
110 GLint bumpenvmat_location
[MAX_TEXTURES
];
111 GLint luminancescale_location
[MAX_TEXTURES
];
112 GLint luminanceoffset_location
[MAX_TEXTURES
];
113 GLint ycorrection_location
;
114 GLenum vertex_color_clamp
;
115 const struct wined3d_shader
*vshader
;
116 const struct wined3d_shader
*pshader
;
117 struct vs_compile_args vs_args
;
118 struct ps_compile_args ps_args
;
119 UINT constant_version
;
120 const struct ps_np2fixup_info
*np2Fixup_info
;
123 struct glsl_program_key
125 const struct wined3d_shader
*vshader
;
126 const struct wined3d_shader
*pshader
;
127 struct ps_compile_args ps_args
;
128 struct vs_compile_args vs_args
;
131 struct shader_glsl_ctx_priv
{
132 const struct vs_compile_args
*cur_vs_args
;
133 const struct ps_compile_args
*cur_ps_args
;
134 struct ps_np2fixup_info
*cur_np2fixup_info
;
137 struct glsl_ps_compiled_shader
139 struct ps_compile_args args
;
140 struct ps_np2fixup_info np2fixup
;
144 struct glsl_pshader_private
146 struct glsl_ps_compiled_shader
*gl_shaders
;
147 UINT num_gl_shaders
, shader_array_size
;
150 struct glsl_vs_compiled_shader
152 struct vs_compile_args args
;
156 struct glsl_vshader_private
158 struct glsl_vs_compiled_shader
*gl_shaders
;
159 UINT num_gl_shaders
, shader_array_size
;
162 static const char *debug_gl_shader_type(GLenum type
)
166 #define WINED3D_TO_STR(u) case u: return #u
167 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB
);
168 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB
);
169 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB
);
170 #undef WINED3D_TO_STR
172 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
176 /* Extract a line from the info log.
177 * Note that this modifies the source string. */
178 static char *get_info_log_line(char **ptr
)
183 if (!(q
= strstr(p
, "\n")))
185 if (!*p
) return NULL
;
195 /** Prints the GLSL info log which will contain error messages if they exist */
196 /* GL locking is done by the caller */
197 static void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLhandleARB obj
)
199 int infologLength
= 0;
202 if (!WARN_ON(d3d_shader
) && !FIXME_ON(d3d_shader
))
205 GL_EXTCALL(glGetObjectParameterivARB(obj
,
206 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
209 /* A size of 1 is just a null-terminated string, so the log should be bigger than
210 * that if there are errors. */
211 if (infologLength
> 1)
215 infoLog
= HeapAlloc(GetProcessHeap(), 0, infologLength
);
216 /* The info log is supposed to be zero-terminated, but at least some
217 * versions of fglrx don't terminate the string properly. The reported
218 * length does include the terminator, so explicitly set it to zero
220 infoLog
[infologLength
- 1] = 0;
221 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
224 if (gl_info
->quirks
& WINED3D_QUIRK_INFO_LOG_SPAM
)
226 WARN("Info log received from GLSL shader #%u:\n", obj
);
227 while ((line
= get_info_log_line(&ptr
))) WARN(" %s\n", line
);
231 FIXME("Info log received from GLSL shader #%u:\n", obj
);
232 while ((line
= get_info_log_line(&ptr
))) FIXME(" %s\n", line
);
234 HeapFree(GetProcessHeap(), 0, infoLog
);
238 /* GL locking is done by the caller. */
239 static void shader_glsl_compile(const struct wined3d_gl_info
*gl_info
, GLhandleARB shader
, const char *src
)
241 TRACE("Compiling shader object %u.\n", shader
);
242 GL_EXTCALL(glShaderSourceARB(shader
, 1, &src
, NULL
));
243 checkGLcall("glShaderSourceARB");
244 GL_EXTCALL(glCompileShaderARB(shader
));
245 checkGLcall("glCompileShaderARB");
246 print_glsl_info_log(gl_info
, shader
);
249 /* GL locking is done by the caller. */
250 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLhandleARB program
)
252 GLint i
, object_count
, source_size
= -1;
253 GLhandleARB
*objects
;
256 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_ATTACHED_OBJECTS_ARB
, &object_count
));
257 objects
= HeapAlloc(GetProcessHeap(), 0, object_count
* sizeof(*objects
));
260 ERR("Failed to allocate object array memory.\n");
264 GL_EXTCALL(glGetAttachedObjectsARB(program
, object_count
, NULL
, objects
));
265 for (i
= 0; i
< object_count
; ++i
)
270 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB
, &tmp
));
272 if (source_size
< tmp
)
274 HeapFree(GetProcessHeap(), 0, source
);
276 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
279 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
280 HeapFree(GetProcessHeap(), 0, objects
);
286 FIXME("Object %u:\n", objects
[i
]);
287 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_SUBTYPE_ARB
, &tmp
));
288 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp
));
289 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_COMPILE_STATUS_ARB
, &tmp
));
290 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp
);
294 GL_EXTCALL(glGetShaderSourceARB(objects
[i
], source_size
, NULL
, source
));
295 while ((line
= get_info_log_line(&ptr
))) FIXME(" %s\n", line
);
299 HeapFree(GetProcessHeap(), 0, source
);
300 HeapFree(GetProcessHeap(), 0, objects
);
303 /* GL locking is done by the caller. */
304 static void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLhandleARB program
)
308 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
)) return;
310 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_TYPE_ARB
, &tmp
));
311 if (tmp
== GL_PROGRAM_OBJECT_ARB
)
313 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_LINK_STATUS_ARB
, &tmp
));
316 FIXME("Program %u link status invalid.\n", program
);
317 shader_glsl_dump_program_source(gl_info
, program
);
321 print_glsl_info_log(gl_info
, program
);
325 * Loads (pixel shader) samplers
327 /* GL locking is done by the caller */
328 static void shader_glsl_load_psamplers(const struct wined3d_gl_info
*gl_info
,
329 const DWORD
*tex_unit_map
, GLhandleARB programId
)
333 char sampler_name
[20];
335 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
336 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
337 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
338 if (name_loc
!= -1) {
339 DWORD mapped_unit
= tex_unit_map
[i
];
340 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< gl_info
->limits
.fragment_samplers
)
342 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
343 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
344 checkGLcall("glUniform1iARB");
346 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
352 /* GL locking is done by the caller */
353 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info
*gl_info
,
354 const DWORD
*tex_unit_map
, GLhandleARB programId
)
357 char sampler_name
[20];
360 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
361 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
362 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
363 if (name_loc
!= -1) {
364 DWORD mapped_unit
= tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
];
365 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< gl_info
->limits
.combined_samplers
)
367 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
368 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
369 checkGLcall("glUniform1iARB");
371 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
377 /* GL locking is done by the caller */
378 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const float *constants
,
379 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
382 unsigned int heap_idx
= 1;
385 if (heap
->entries
[heap_idx
].version
<= version
) return;
387 idx
= heap
->entries
[heap_idx
].idx
;
388 if (constant_locations
[idx
] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
389 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
391 while (stack_idx
>= 0)
393 /* Note that we fall through to the next case statement. */
394 switch(stack
[stack_idx
])
396 case HEAP_NODE_TRAVERSE_LEFT
:
398 unsigned int left_idx
= heap_idx
<< 1;
399 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
402 idx
= heap
->entries
[heap_idx
].idx
;
403 if (constant_locations
[idx
] != -1)
404 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
406 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
407 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
412 case HEAP_NODE_TRAVERSE_RIGHT
:
414 unsigned int right_idx
= (heap_idx
<< 1) + 1;
415 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
417 heap_idx
= right_idx
;
418 idx
= heap
->entries
[heap_idx
].idx
;
419 if (constant_locations
[idx
] != -1)
420 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
422 stack
[stack_idx
++] = HEAP_NODE_POP
;
423 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
436 checkGLcall("walk_constant_heap()");
439 /* GL locking is done by the caller */
440 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
, GLint location
, const GLfloat
*data
)
442 GLfloat clamped_constant
[4];
444 if (location
== -1) return;
446 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0f
? 1.0f
: data
[0];
447 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0f
? 1.0f
: data
[1];
448 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0f
? 1.0f
: data
[2];
449 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0f
? 1.0f
: data
[3];
451 GL_EXTCALL(glUniform4fvARB(location
, 1, clamped_constant
));
454 /* GL locking is done by the caller */
455 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
, const float *constants
,
456 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
459 unsigned int heap_idx
= 1;
462 if (heap
->entries
[heap_idx
].version
<= version
) return;
464 idx
= heap
->entries
[heap_idx
].idx
;
465 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
466 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
468 while (stack_idx
>= 0)
470 /* Note that we fall through to the next case statement. */
471 switch(stack
[stack_idx
])
473 case HEAP_NODE_TRAVERSE_LEFT
:
475 unsigned int left_idx
= heap_idx
<< 1;
476 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
479 idx
= heap
->entries
[heap_idx
].idx
;
480 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
482 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
483 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
488 case HEAP_NODE_TRAVERSE_RIGHT
:
490 unsigned int right_idx
= (heap_idx
<< 1) + 1;
491 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
493 heap_idx
= right_idx
;
494 idx
= heap
->entries
[heap_idx
].idx
;
495 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
497 stack
[stack_idx
++] = HEAP_NODE_POP
;
498 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
511 checkGLcall("walk_constant_heap_clamped()");
514 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
515 /* GL locking is done by the caller */
516 static void shader_glsl_load_constantsF(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
517 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
518 unsigned char *stack
, UINT version
)
520 const local_constant
*lconst
;
522 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
523 if (shader
->reg_maps
.shader_version
.major
== 1
524 && shader_is_pshader_version(shader
->reg_maps
.shader_version
.type
))
525 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
527 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
529 if (!shader
->load_local_constsF
)
531 TRACE("No need to load local float constants for this shader\n");
535 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
536 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, local_constant
, entry
)
538 GLint location
= constant_locations
[lconst
->idx
];
539 /* We found this uniform name in the program - go ahead and send the data */
540 if (location
!= -1) GL_EXTCALL(glUniform4fvARB(location
, 1, (const GLfloat
*)lconst
->value
));
542 checkGLcall("glUniform4fvARB()");
545 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
546 /* GL locking is done by the caller */
547 static void shader_glsl_load_constantsI(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
548 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
553 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
555 if (!(constants_set
& 1)) continue;
557 TRACE_(d3d_constants
)("Loading constants %u: %i, %i, %i, %i\n",
558 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
560 /* We found this uniform name in the program - go ahead and send the data */
561 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
562 checkGLcall("glUniform4ivARB");
565 /* Load immediate constants */
566 ptr
= list_head(&shader
->constantsI
);
569 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
570 unsigned int idx
= lconst
->idx
;
571 const GLint
*values
= (const GLint
*)lconst
->value
;
573 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
574 values
[0], values
[1], values
[2], values
[3]);
576 /* We found this uniform name in the program - go ahead and send the data */
577 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
578 checkGLcall("glUniform4ivARB");
579 ptr
= list_next(&shader
->constantsI
, ptr
);
583 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
584 /* GL locking is done by the caller */
585 static void shader_glsl_load_constantsB(const struct wined3d_shader
*shader
, const struct wined3d_gl_info
*gl_info
,
586 GLhandleARB programId
, const BOOL
*constants
, WORD constants_set
)
594 switch (shader
->reg_maps
.shader_version
.type
)
596 case WINED3D_SHADER_TYPE_VERTEX
:
600 case WINED3D_SHADER_TYPE_GEOMETRY
:
604 case WINED3D_SHADER_TYPE_PIXEL
:
609 FIXME("Unknown shader type %#x.\n",
610 shader
->reg_maps
.shader_version
.type
);
615 /* TODO: Benchmark and see if it would be beneficial to store the
616 * locations of the constants to avoid looking up each time */
617 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
619 if (!(constants_set
& 1)) continue;
621 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
623 /* TODO: Benchmark and see if it would be beneficial to store the
624 * locations of the constants to avoid looking up each time */
625 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
626 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
629 /* We found this uniform name in the program - go ahead and send the data */
630 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
631 checkGLcall("glUniform1ivARB");
635 /* Load immediate constants */
636 ptr
= list_head(&shader
->constantsB
);
639 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
640 unsigned int idx
= lconst
->idx
;
641 const GLint
*values
= (const GLint
*)lconst
->value
;
643 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
645 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
646 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
648 /* We found this uniform name in the program - go ahead and send the data */
649 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
650 checkGLcall("glUniform1ivARB");
652 ptr
= list_next(&shader
->constantsB
, ptr
);
656 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
658 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
662 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
664 /* GL locking is done by the caller (state handler) */
665 static void shader_glsl_load_np2fixup_constants(void *shader_priv
,
666 const struct wined3d_gl_info
*gl_info
, const struct wined3d_state
*state
)
668 struct shader_glsl_priv
*glsl_priv
= shader_priv
;
669 const struct glsl_shader_prog_link
*prog
= glsl_priv
->glsl_program
;
671 /* No GLSL program set - nothing to do. */
674 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
675 if (!use_ps(state
)) return;
677 if (prog
->ps_args
.np2_fixup
&& prog
->np2Fixup_location
!= -1)
680 UINT fixup
= prog
->ps_args
.np2_fixup
;
681 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
683 for (i
= 0; fixup
; fixup
>>= 1, ++i
)
685 const struct wined3d_texture
*tex
= state
->textures
[i
];
686 const unsigned char idx
= prog
->np2Fixup_info
->idx
[i
];
687 GLfloat
*tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
691 ERR("Nonexistent texture is flagged for NP2 texcoord fixup.\n");
697 tex_dim
[2] = tex
->pow2_matrix
[0];
698 tex_dim
[3] = tex
->pow2_matrix
[5];
702 tex_dim
[0] = tex
->pow2_matrix
[0];
703 tex_dim
[1] = tex
->pow2_matrix
[5];
707 GL_EXTCALL(glUniform4fvARB(prog
->np2Fixup_location
, prog
->np2Fixup_info
->num_consts
, np2fixup_constants
));
712 * Loads the app-supplied constants into the currently set GLSL program.
714 /* GL locking is done by the caller (state handler) */
715 static void shader_glsl_load_constants(const struct wined3d_context
*context
,
716 char usePixelShader
, char useVertexShader
)
718 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
719 struct wined3d_device
*device
= context
->swapchain
->device
;
720 struct wined3d_stateblock
*stateBlock
= device
->stateBlock
;
721 struct shader_glsl_priv
*priv
= device
->shader_priv
;
722 float position_fixup
[4];
724 GLhandleARB programId
;
725 struct glsl_shader_prog_link
*prog
= priv
->glsl_program
;
726 UINT constant_version
;
730 /* No GLSL program set - nothing to do. */
733 programId
= prog
->programId
;
734 constant_version
= prog
->constant_version
;
738 const struct wined3d_shader
*vshader
= stateBlock
->state
.vertex_shader
;
740 /* Load DirectX 9 float constants/uniforms for vertex shader */
741 shader_glsl_load_constantsF(vshader
, gl_info
, stateBlock
->state
.vs_consts_f
,
742 prog
->vuniformF_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
744 /* Load DirectX 9 integer constants/uniforms for vertex shader */
745 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vuniformI_locations
, stateBlock
->state
.vs_consts_i
,
746 stateBlock
->changed
.vertexShaderConstantsI
& vshader
->reg_maps
.integer_constants
);
748 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
749 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, stateBlock
->state
.vs_consts_b
,
750 stateBlock
->changed
.vertexShaderConstantsB
& vshader
->reg_maps
.boolean_constants
);
752 /* Upload the position fixup params */
753 shader_get_position_fixup(context
, &stateBlock
->state
, position_fixup
);
754 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, position_fixup
));
755 checkGLcall("glUniform4fvARB");
760 const struct wined3d_shader
*pshader
= stateBlock
->state
.pixel_shader
;
762 /* Load DirectX 9 float constants/uniforms for pixel shader */
763 shader_glsl_load_constantsF(pshader
, gl_info
, stateBlock
->state
.ps_consts_f
,
764 prog
->puniformF_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
766 /* Load DirectX 9 integer constants/uniforms for pixel shader */
767 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->puniformI_locations
, stateBlock
->state
.ps_consts_i
,
768 stateBlock
->changed
.pixelShaderConstantsI
& pshader
->reg_maps
.integer_constants
);
770 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
771 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, stateBlock
->state
.ps_consts_b
,
772 stateBlock
->changed
.pixelShaderConstantsB
& pshader
->reg_maps
.boolean_constants
);
774 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
775 * It can't be 0 for a valid texbem instruction.
777 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
780 if(prog
->bumpenvmat_location
[i
] == -1) continue;
782 data
= (const float *)&stateBlock
->state
.texture_states
[i
][WINED3DTSS_BUMPENVMAT00
];
783 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
[i
], 1, 0, data
));
784 checkGLcall("glUniformMatrix2fvARB");
786 /* texbeml needs the luminance scale and offset too. If texbeml
787 * is used, needsbumpmat is set too, so we can check that in the
788 * needsbumpmat check. */
789 if (prog
->luminancescale_location
[i
] != -1)
791 const GLfloat
*scale
= (const GLfloat
*)&stateBlock
->state
.texture_states
[i
][WINED3DTSS_BUMPENVLSCALE
];
792 const GLfloat
*offset
= (const GLfloat
*)&stateBlock
->state
.texture_states
[i
][WINED3DTSS_BUMPENVLOFFSET
];
794 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
[i
], 1, scale
));
795 checkGLcall("glUniform1fvARB");
796 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
[i
], 1, offset
));
797 checkGLcall("glUniform1fvARB");
801 if (prog
->ycorrection_location
!= -1)
803 float correction_params
[4];
805 if (context
->render_offscreen
)
807 correction_params
[0] = 0.0f
;
808 correction_params
[1] = 1.0f
;
810 /* position is window relative, not viewport relative */
811 correction_params
[0] = (float) context
->current_rt
->resource
.height
;
812 correction_params
[1] = -1.0f
;
814 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
818 if (priv
->next_constant_version
== UINT_MAX
)
820 TRACE("Max constant version reached, resetting to 0.\n");
821 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
822 priv
->next_constant_version
= 1;
826 prog
->constant_version
= priv
->next_constant_version
++;
830 static void update_heap_entry(const struct constant_heap
*heap
, unsigned int idx
,
831 unsigned int heap_idx
, DWORD new_version
)
833 struct constant_entry
*entries
= heap
->entries
;
834 unsigned int *positions
= heap
->positions
;
835 unsigned int parent_idx
;
839 parent_idx
= heap_idx
>> 1;
841 if (new_version
<= entries
[parent_idx
].version
) break;
843 entries
[heap_idx
] = entries
[parent_idx
];
844 positions
[entries
[parent_idx
].idx
] = heap_idx
;
845 heap_idx
= parent_idx
;
848 entries
[heap_idx
].version
= new_version
;
849 entries
[heap_idx
].idx
= idx
;
850 positions
[idx
] = heap_idx
;
853 static void shader_glsl_update_float_vertex_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
855 struct shader_glsl_priv
*priv
= device
->shader_priv
;
856 struct constant_heap
*heap
= &priv
->vconst_heap
;
859 for (i
= start
; i
< count
+ start
; ++i
)
861 if (!device
->stateBlock
->changed
.vertexShaderConstantsF
[i
])
862 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
864 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
868 static void shader_glsl_update_float_pixel_constants(struct wined3d_device
*device
, UINT start
, UINT count
)
870 struct shader_glsl_priv
*priv
= device
->shader_priv
;
871 struct constant_heap
*heap
= &priv
->pconst_heap
;
874 for (i
= start
; i
< count
+ start
; ++i
)
876 if (!device
->stateBlock
->changed
.pixelShaderConstantsF
[i
])
877 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
879 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
883 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
885 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
886 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
887 if(shader_major
> 3) return ret
;
889 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
890 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
894 /** Generate the variable & register declarations for the GLSL output target */
895 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
896 struct wined3d_shader_buffer
*buffer
, const struct wined3d_shader
*shader
,
897 const struct wined3d_shader_reg_maps
*reg_maps
, const struct shader_glsl_ctx_priv
*ctx_priv
)
899 const struct wined3d_state
*state
= &shader
->device
->stateBlock
->state
;
900 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
901 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
902 const struct wined3d_fb_state
*fb
= &shader
->device
->fb
;
903 unsigned int i
, extra_constants_needed
= 0;
904 const local_constant
*lconst
;
907 /* There are some minor differences between pixel and vertex shaders */
908 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
909 char prefix
= pshader
? 'P' : 'V';
911 /* Prototype the subroutines */
912 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
914 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
917 /* Declare the constants (aka uniforms) */
918 if (shader
->limits
.constant_float
> 0)
920 unsigned max_constantsF
;
921 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
922 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
923 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
924 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
925 * a dx9 card, as long as it doesn't also use all the other constants.
927 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
928 * declare only the amount that we're assured to have.
930 * Thus we run into problems in these two cases:
931 * 1) The shader really uses more uniforms than supported
932 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
936 /* No indirect addressing here. */
937 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
941 if (reg_maps
->usesrelconstF
)
943 /* Subtract the other potential uniforms from the max
944 * available (bools, ints, and 1 row of projection matrix).
945 * Subtract another uniform for immediate values, which have
946 * to be loaded via uniform by the driver as well. The shader
947 * code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex
948 * shader code, so one vec4 should be enough. (Unfortunately
949 * the Nvidia driver doesn't store 128 and -128 in one float).
951 * Writing gl_ClipVertex requires one uniform for each
952 * clipplane as well. */
953 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
954 if(ctx_priv
->cur_vs_args
->clip_enabled
)
956 max_constantsF
-= gl_info
->limits
.clipplanes
;
958 max_constantsF
-= count_bits(reg_maps
->integer_constants
);
959 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
960 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
961 * for now take this into account when calculating the number of available constants
963 max_constantsF
-= count_bits(reg_maps
->boolean_constants
);
964 /* Set by driver quirks in directx.c */
965 max_constantsF
-= gl_info
->reserved_glsl_constants
;
969 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
972 max_constantsF
= min(shader
->limits
.constant_float
, max_constantsF
);
973 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
976 /* Always declare the full set of constants, the compiler can remove the
977 * unused ones because d3d doesn't (yet) support indirect int and bool
978 * constant addressing. This avoids problems if the app uses e.g. i0 and i9. */
979 if (shader
->limits
.constant_int
> 0 && reg_maps
->integer_constants
)
980 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, shader
->limits
.constant_int
);
982 if (shader
->limits
.constant_bool
> 0 && reg_maps
->boolean_constants
)
983 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, shader
->limits
.constant_bool
);
987 shader_addline(buffer
, "uniform vec4 posFixup;\n");
988 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
992 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
994 if (!(map
& 1)) continue;
996 shader_addline(buffer
, "uniform mat2 bumpenvmat%d;\n", i
);
998 if (reg_maps
->luminanceparams
& (1 << i
))
1000 shader_addline(buffer
, "uniform float luminancescale%d;\n", i
);
1001 shader_addline(buffer
, "uniform float luminanceoffset%d;\n", i
);
1002 extra_constants_needed
++;
1005 extra_constants_needed
++;
1008 if (ps_args
->srgb_correction
)
1010 shader_addline(buffer
, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1011 srgb_pow
, srgb_mul_high
, srgb_sub_high
, srgb_mul_low
);
1012 shader_addline(buffer
, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1015 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
1017 if (shader
->limits
.constant_float
+ extra_constants_needed
1018 + 1 < gl_info
->limits
.glsl_ps_float_constants
)
1020 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
1021 extra_constants_needed
++;
1025 /* This happens because we do not have proper tracking of the constant registers that are
1026 * actually used, only the max limit of the shader version
1028 FIXME("Cannot find a free uniform for vpos correction params\n");
1029 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1030 context
->render_offscreen
? 0.0f
: fb
->render_targets
[0]->resource
.height
,
1031 context
->render_offscreen
? 1.0f
: -1.0f
);
1033 shader_addline(buffer
, "vec4 vpos;\n");
1037 /* Declare texture samplers */
1038 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
1040 if (reg_maps
->sampler_type
[i
])
1042 const struct wined3d_texture
*texture
;
1044 switch (reg_maps
->sampler_type
[i
])
1047 if (pshader
&& ps_args
->shadow
& (1 << i
))
1048 shader_addline(buffer
, "uniform sampler1DShadow %csampler%u;\n", prefix
, i
);
1050 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
1053 texture
= state
->textures
[i
];
1054 if (pshader
&& ps_args
->shadow
& (1 << i
))
1056 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
1057 shader_addline(buffer
, "uniform sampler2DRectShadow %csampler%u;\n", prefix
, i
);
1059 shader_addline(buffer
, "uniform sampler2DShadow %csampler%u;\n", prefix
, i
);
1063 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
1064 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
1066 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
1069 case WINED3DSTT_CUBE
:
1070 if (pshader
&& ps_args
->shadow
& (1 << i
)) FIXME("Unsupported Cube shadow sampler.\n");
1071 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
1073 case WINED3DSTT_VOLUME
:
1074 if (pshader
&& ps_args
->shadow
& (1 << i
)) FIXME("Unsupported 3D shadow sampler.\n");
1075 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
1078 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
1079 FIXME("Unrecognized sampler type: %#x\n", reg_maps
->sampler_type
[i
]);
1085 /* Declare uniforms for NP2 texcoord fixup:
1086 * This is NOT done inside the loop that declares the texture samplers since the NP2 fixup code
1087 * is currently only used for the GeforceFX series and when forcing the ARB_npot extension off.
1088 * Modern cards just skip the code anyway, so put it inside a separate loop. */
1089 if (pshader
&& ps_args
->np2_fixup
) {
1091 struct ps_np2fixup_info
* const fixup
= ctx_priv
->cur_np2fixup_info
;
1094 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1095 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1096 * samplerNP2Fixup stores texture dimensions and is updated through
1097 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1099 for (i
= 0; i
< shader
->limits
.sampler
; ++i
)
1101 if (reg_maps
->sampler_type
[i
])
1103 if (!(ps_args
->np2_fixup
& (1 << i
))) continue;
1105 if (WINED3DSTT_2D
!= reg_maps
->sampler_type
[i
]) {
1106 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1110 fixup
->idx
[i
] = cur
++;
1114 fixup
->num_consts
= (cur
+ 1) >> 1;
1115 shader_addline(buffer
, "uniform vec4 %csamplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
1118 /* Declare address variables */
1119 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
1121 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
1124 /* Declare texture coordinate temporaries and initialize them */
1125 for (i
= 0, map
= reg_maps
->texcoord
; map
; map
>>= 1, ++i
)
1127 if (map
& 1) shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
1130 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
1131 * helper function shader that is linked in at link time
1133 if (pshader
&& reg_maps
->shader_version
.major
>= 3)
1137 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
1139 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1140 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1141 * pixel shader that reads the fixed function color into the packed input registers.
1143 shader_addline(buffer
, "vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
1147 /* Declare output register temporaries */
1148 if (shader
->limits
.packed_output
)
1149 shader_addline(buffer
, "vec4 OUT[%u];\n", shader
->limits
.packed_output
);
1151 /* Declare temporary variables */
1152 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
1154 if (map
& 1) shader_addline(buffer
, "vec4 R%u;\n", i
);
1157 /* Declare attributes */
1158 if (reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1160 for (i
= 0, map
= reg_maps
->input_registers
; map
; map
>>= 1, ++i
)
1162 if (map
& 1) shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
1166 /* Declare loop registers aLx */
1167 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
1168 shader_addline(buffer
, "int aL%u;\n", i
);
1169 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1172 /* Temporary variables for matrix operations */
1173 shader_addline(buffer
, "vec4 tmp0;\n");
1174 shader_addline(buffer
, "vec4 tmp1;\n");
1176 /* Local constants use a different name so they can be loaded once at shader link time
1177 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1178 * float -> string conversion can cause precision loss.
1180 if (!shader
->load_local_constsF
)
1182 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, local_constant
, entry
)
1184 shader_addline(buffer
, "uniform vec4 %cLC%u;\n", prefix
, lconst
->idx
);
1188 /* Start the main program */
1189 shader_addline(buffer
, "void main() {\n");
1190 if(pshader
&& reg_maps
->vpos
) {
1191 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1192 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1193 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1194 * precision troubles when we just subtract 0.5.
1196 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1198 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1200 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1201 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1202 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1203 * correctly on drivers that returns integer values.
1205 shader_addline(buffer
, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1209 /*****************************************************************************
1210 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1212 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1213 ****************************************************************************/
1216 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1217 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
);
1219 /** Used for opcode modifiers - They multiply the result by the specified amount */
1220 static const char * const shift_glsl_tab
[] = {
1222 "2.0 * ", /* 1 (x2) */
1223 "4.0 * ", /* 2 (x4) */
1224 "8.0 * ", /* 3 (x8) */
1225 "16.0 * ", /* 4 (x16) */
1226 "32.0 * ", /* 5 (x32) */
1233 "0.0625 * ", /* 12 (d16) */
1234 "0.125 * ", /* 13 (d8) */
1235 "0.25 * ", /* 14 (d4) */
1236 "0.5 * " /* 15 (d2) */
1239 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1240 static void shader_glsl_gen_modifier(DWORD src_modifier
, const char *in_reg
, const char *in_regswizzle
, char *out_str
)
1244 switch (src_modifier
)
1246 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
1247 case WINED3DSPSM_DW
:
1248 case WINED3DSPSM_NONE
:
1249 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1251 case WINED3DSPSM_NEG
:
1252 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
1254 case WINED3DSPSM_NOT
:
1255 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
1257 case WINED3DSPSM_BIAS
:
1258 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1260 case WINED3DSPSM_BIASNEG
:
1261 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1263 case WINED3DSPSM_SIGN
:
1264 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1266 case WINED3DSPSM_SIGNNEG
:
1267 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1269 case WINED3DSPSM_COMP
:
1270 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
1272 case WINED3DSPSM_X2
:
1273 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
1275 case WINED3DSPSM_X2NEG
:
1276 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
1278 case WINED3DSPSM_ABS
:
1279 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
1281 case WINED3DSPSM_ABSNEG
:
1282 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
1285 FIXME("Unhandled modifier %u\n", src_modifier
);
1286 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1290 /** Writes the GLSL variable name that corresponds to the register that the
1291 * DX opcode parameter is trying to access */
1292 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
1293 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
1295 /* oPos, oFog and oPts in D3D */
1296 static const char * const hwrastout_reg_names
[] = {"OUT[10]", "OUT[11].x", "OUT[11].y"};
1298 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
1299 const struct wined3d_shader_reg_maps
*reg_maps
= ins
->ctx
->reg_maps
;
1300 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
1301 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
1307 case WINED3DSPR_TEMP
:
1308 sprintf(register_name
, "R%u", reg
->idx
);
1311 case WINED3DSPR_INPUT
:
1312 /* vertex shaders */
1315 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1316 if (priv
->cur_vs_args
->swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1317 sprintf(register_name
, "attrib%u", reg
->idx
);
1321 /* pixel shaders >= 3.0 */
1322 if (reg_maps
->shader_version
.major
>= 3)
1324 DWORD idx
= shader
->u
.ps
.input_reg_map
[reg
->idx
];
1325 unsigned int in_count
= vec4_varyings(reg_maps
->shader_version
.major
, gl_info
);
1329 struct glsl_src_param rel_param
;
1331 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1333 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1334 * operation there */
1337 if (shader
->u
.ps
.declared_in_count
> in_count
)
1339 sprintf(register_name
,
1340 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1341 rel_param
.param_str
, idx
, in_count
- 1, rel_param
.param_str
, idx
, in_count
,
1342 rel_param
.param_str
, idx
);
1346 sprintf(register_name
, "IN[%s + %u]", rel_param
.param_str
, idx
);
1351 if (shader
->u
.ps
.declared_in_count
> in_count
)
1353 sprintf(register_name
, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1354 rel_param
.param_str
, in_count
- 1, rel_param
.param_str
, in_count
,
1355 rel_param
.param_str
);
1359 sprintf(register_name
, "IN[%s]", rel_param
.param_str
);
1365 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
1366 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
1367 else sprintf(register_name
, "IN[%u]", idx
);
1372 if (!reg
->idx
) strcpy(register_name
, "gl_Color");
1373 else strcpy(register_name
, "gl_SecondaryColor");
1378 case WINED3DSPR_CONST
:
1380 const char prefix
= pshader
? 'P' : 'V';
1382 /* Relative addressing */
1385 struct glsl_src_param rel_param
;
1386 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1387 if (reg
->idx
) sprintf(register_name
, "%cC[%s + %u]", prefix
, rel_param
.param_str
, reg
->idx
);
1388 else sprintf(register_name
, "%cC[%s]", prefix
, rel_param
.param_str
);
1392 if (shader_constant_is_local(shader
, reg
->idx
))
1393 sprintf(register_name
, "%cLC%u", prefix
, reg
->idx
);
1395 sprintf(register_name
, "%cC[%u]", prefix
, reg
->idx
);
1400 case WINED3DSPR_CONSTINT
:
1401 if (pshader
) sprintf(register_name
, "PI[%u]", reg
->idx
);
1402 else sprintf(register_name
, "VI[%u]", reg
->idx
);
1405 case WINED3DSPR_CONSTBOOL
:
1406 if (pshader
) sprintf(register_name
, "PB[%u]", reg
->idx
);
1407 else sprintf(register_name
, "VB[%u]", reg
->idx
);
1410 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1411 if (pshader
) sprintf(register_name
, "T%u", reg
->idx
);
1412 else sprintf(register_name
, "A%u", reg
->idx
);
1415 case WINED3DSPR_LOOP
:
1416 sprintf(register_name
, "aL%u", ins
->ctx
->loop_state
->current_reg
- 1);
1419 case WINED3DSPR_SAMPLER
:
1420 if (pshader
) sprintf(register_name
, "Psampler%u", reg
->idx
);
1421 else sprintf(register_name
, "Vsampler%u", reg
->idx
);
1424 case WINED3DSPR_COLOROUT
:
1425 if (reg
->idx
>= gl_info
->limits
.buffers
)
1426 WARN("Write to render target %u, only %d supported.\n", reg
->idx
, gl_info
->limits
.buffers
);
1428 sprintf(register_name
, "gl_FragData[%u]", reg
->idx
);
1431 case WINED3DSPR_RASTOUT
:
1432 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
]);
1435 case WINED3DSPR_DEPTHOUT
:
1436 sprintf(register_name
, "gl_FragDepth");
1439 case WINED3DSPR_ATTROUT
:
1440 if (!reg
->idx
) sprintf(register_name
, "OUT[8]");
1441 else sprintf(register_name
, "OUT[9]");
1444 case WINED3DSPR_TEXCRDOUT
:
1445 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1446 sprintf(register_name
, "OUT[%u]", reg
->idx
);
1449 case WINED3DSPR_MISCTYPE
:
1453 sprintf(register_name
, "vpos");
1455 else if (reg
->idx
== 1)
1457 /* Note that gl_FrontFacing is a bool, while vFace is
1458 * a float for which the sign determines front/back */
1459 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
1463 FIXME("Unhandled misctype register %d\n", reg
->idx
);
1464 sprintf(register_name
, "unrecognized_register");
1468 case WINED3DSPR_IMMCONST
:
1469 switch (reg
->immconst_type
)
1471 case WINED3D_IMMCONST_SCALAR
:
1472 sprintf(register_name
, "%.8e", *(const float *)reg
->immconst_data
);
1475 case WINED3D_IMMCONST_VEC4
:
1476 sprintf(register_name
, "vec4(%.8e, %.8e, %.8e, %.8e)",
1477 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1478 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1482 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
1483 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
1488 FIXME("Unhandled register name Type(%d)\n", reg
->type
);
1489 sprintf(register_name
, "unrecognized_register");
1494 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
1497 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
1498 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
1499 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
1500 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
1504 /* Get the GLSL write mask for the destination register */
1505 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
1507 DWORD mask
= param
->write_mask
;
1509 if (shader_is_scalar(¶m
->reg
))
1511 mask
= WINED3DSP_WRITEMASK_0
;
1516 shader_glsl_write_mask_to_str(mask
, write_mask
);
1522 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
1523 unsigned int size
= 0;
1525 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
1526 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
1527 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
1528 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
1533 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
1535 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1536 * but addressed as "rgba". To fix this we need to swap the register's x
1537 * and z components. */
1538 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1541 /* swizzle bits fields: wwzzyyxx */
1542 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
1543 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1544 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1545 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1549 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
1550 BOOL fixup
, DWORD mask
, char *swizzle_str
)
1552 if (shader_is_scalar(¶m
->reg
))
1553 *swizzle_str
= '\0';
1555 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
1558 /* From a given parameter token, generate the corresponding GLSL string.
1559 * Also, return the actual register name and swizzle in case the
1560 * caller needs this information as well. */
1561 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1562 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, struct glsl_src_param
*glsl_src
)
1564 BOOL is_color
= FALSE
;
1565 char swizzle_str
[6];
1567 glsl_src
->reg_name
[0] = '\0';
1568 glsl_src
->param_str
[0] = '\0';
1569 swizzle_str
[0] = '\0';
1571 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
1572 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
1573 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
1576 /* From a given parameter token, generate the corresponding GLSL string.
1577 * Also, return the actual register name and swizzle in case the
1578 * caller needs this information as well. */
1579 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
1580 const struct wined3d_shader_dst_param
*wined3d_dst
, struct glsl_dst_param
*glsl_dst
)
1582 BOOL is_color
= FALSE
;
1584 glsl_dst
->mask_str
[0] = '\0';
1585 glsl_dst
->reg_name
[0] = '\0';
1587 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
1588 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
1591 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1592 static DWORD
shader_glsl_append_dst_ext(struct wined3d_shader_buffer
*buffer
,
1593 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
)
1595 struct glsl_dst_param glsl_dst
;
1598 mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
);
1599 if (mask
) shader_addline(buffer
, "%s%s = %s(", glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
1604 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1605 static DWORD
shader_glsl_append_dst(struct wined3d_shader_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
1607 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0]);
1610 /** Process GLSL instruction modifiers */
1611 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
1613 struct glsl_dst_param dst_param
;
1616 if (!ins
->dst_count
) return;
1618 modifiers
= ins
->dst
[0].modifiers
;
1619 if (!modifiers
) return;
1621 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
1623 if (modifiers
& WINED3DSPDM_SATURATE
)
1625 /* _SAT means to clamp the value of the register to between 0 and 1 */
1626 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1627 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1630 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
1632 FIXME("_centroid modifier not handled\n");
1635 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
1637 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1641 static inline const char *shader_get_comp_op(DWORD op
)
1644 case COMPARISON_GT
: return ">";
1645 case COMPARISON_EQ
: return "==";
1646 case COMPARISON_GE
: return ">=";
1647 case COMPARISON_LT
: return "<";
1648 case COMPARISON_NE
: return "!=";
1649 case COMPARISON_LE
: return "<=";
1651 FIXME("Unrecognized comparison value: %u\n", op
);
1656 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
1657 DWORD sampler_idx
, DWORD flags
, struct glsl_sample_function
*sample_function
)
1659 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ctx
->reg_maps
->sampler_type
[sampler_idx
];
1660 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
1661 BOOL shadow
= shader_is_pshader_version(ctx
->reg_maps
->shader_version
.type
)
1662 && (((const struct shader_glsl_ctx_priv
*)ctx
->backend_data
)->cur_ps_args
->shadow
& (1 << sampler_idx
));
1663 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
1664 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_RECT
;
1665 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
1666 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
1668 /* Note that there's no such thing as a projected cube texture. */
1669 switch(sampler_type
) {
1675 sample_function
->name
= projected
? "shadow1DProjLod" : "shadow1DLod";
1679 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1680 sample_function
->name
= projected
? "shadow1DProjGrad" : "shadow1DGrad";
1681 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1682 sample_function
->name
= projected
? "shadow1DProjGradARB" : "shadow1DGradARB";
1685 FIXME("Unsupported 1D shadow grad function.\n");
1686 sample_function
->name
= "unsupported1DGrad";
1691 sample_function
->name
= projected
? "shadow1DProj" : "shadow1D";
1693 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1699 sample_function
->name
= projected
? "texture1DProjLod" : "texture1DLod";
1703 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1704 sample_function
->name
= projected
? "texture1DProjGrad" : "texture1DGrad";
1705 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1706 sample_function
->name
= projected
? "texture1DProjGradARB" : "texture1DGradARB";
1709 FIXME("Unsupported 1D grad function.\n");
1710 sample_function
->name
= "unsupported1DGrad";
1715 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1717 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1728 sample_function
->name
= projected
? "shadow2DRectProjLod" : "shadow2DRectLod";
1732 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1733 sample_function
->name
= projected
? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1734 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1735 sample_function
->name
= projected
? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1738 FIXME("Unsupported RECT shadow grad function.\n");
1739 sample_function
->name
= "unsupported2DRectGrad";
1744 sample_function
->name
= projected
? "shadow2DRectProj" : "shadow2DRect";
1751 sample_function
->name
= projected
? "shadow2DProjLod" : "shadow2DLod";
1755 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1756 sample_function
->name
= projected
? "shadow2DProjGrad" : "shadow2DGrad";
1757 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1758 sample_function
->name
= projected
? "shadow2DProjGradARB" : "shadow2DGradARB";
1761 FIXME("Unsupported 2D shadow grad function.\n");
1762 sample_function
->name
= "unsupported2DGrad";
1767 sample_function
->name
= projected
? "shadow2DProj" : "shadow2D";
1770 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1778 sample_function
->name
= projected
? "texture2DRectProjLod" : "texture2DRectLod";
1782 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1783 sample_function
->name
= projected
? "texture2DRectProjGrad" : "texture2DRectGrad";
1784 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1785 sample_function
->name
= projected
? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1788 FIXME("Unsupported RECT grad function.\n");
1789 sample_function
->name
= "unsupported2DRectGrad";
1794 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1801 sample_function
->name
= projected
? "texture2DProjLod" : "texture2DLod";
1805 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1806 sample_function
->name
= projected
? "texture2DProjGrad" : "texture2DGrad";
1807 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1808 sample_function
->name
= projected
? "texture2DProjGradARB" : "texture2DGradARB";
1811 FIXME("Unsupported 2D grad function.\n");
1812 sample_function
->name
= "unsupported2DGrad";
1817 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1820 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1824 case WINED3DSTT_CUBE
:
1827 FIXME("Unsupported Cube shadow function.\n");
1828 sample_function
->name
= "unsupportedCubeShadow";
1829 sample_function
->coord_mask
= 0;
1835 sample_function
->name
= "textureCubeLod";
1839 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1840 sample_function
->name
= "textureCubeGrad";
1841 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1842 sample_function
->name
= "textureCubeGradARB";
1845 FIXME("Unsupported Cube grad function.\n");
1846 sample_function
->name
= "unsupportedCubeGrad";
1851 sample_function
->name
= "textureCube";
1853 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1857 case WINED3DSTT_VOLUME
:
1860 FIXME("Unsupported 3D shadow function.\n");
1861 sample_function
->name
= "unsupported3DShadow";
1862 sample_function
->coord_mask
= 0;
1868 sample_function
->name
= projected
? "texture3DProjLod" : "texture3DLod";
1872 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1873 sample_function
->name
= projected
? "texture3DProjGrad" : "texture3DGrad";
1874 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1875 sample_function
->name
= projected
? "texture3DProjGradARB" : "texture3DGradARB";
1878 FIXME("Unsupported 3D grad function.\n");
1879 sample_function
->name
= "unsupported3DGrad";
1884 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1886 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1891 sample_function
->name
= "";
1892 sample_function
->coord_mask
= 0;
1893 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1898 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
1899 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
1901 switch(channel_source
)
1903 case CHANNEL_SOURCE_ZERO
:
1904 strcat(arguments
, "0.0");
1907 case CHANNEL_SOURCE_ONE
:
1908 strcat(arguments
, "1.0");
1911 case CHANNEL_SOURCE_X
:
1912 strcat(arguments
, reg_name
);
1913 strcat(arguments
, ".x");
1916 case CHANNEL_SOURCE_Y
:
1917 strcat(arguments
, reg_name
);
1918 strcat(arguments
, ".y");
1921 case CHANNEL_SOURCE_Z
:
1922 strcat(arguments
, reg_name
);
1923 strcat(arguments
, ".z");
1926 case CHANNEL_SOURCE_W
:
1927 strcat(arguments
, reg_name
);
1928 strcat(arguments
, ".w");
1932 FIXME("Unhandled channel source %#x\n", channel_source
);
1933 strcat(arguments
, "undefined");
1937 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
1940 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
1942 struct wined3d_shader_dst_param dst
;
1943 unsigned int mask_size
, remaining
;
1944 struct glsl_dst_param dst_param
;
1945 char arguments
[256];
1949 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1950 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1951 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1952 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1953 mask
&= ins
->dst
[0].write_mask
;
1955 if (!mask
) return; /* Nothing to do */
1957 if (is_complex_fixup(fixup
))
1959 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
1960 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
1964 mask_size
= shader_glsl_get_write_mask_size(mask
);
1967 dst
.write_mask
= mask
;
1968 shader_glsl_add_dst_param(ins
, &dst
, &dst_param
);
1970 arguments
[0] = '\0';
1971 remaining
= mask_size
;
1972 if (mask
& WINED3DSP_WRITEMASK_0
)
1974 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
1975 if (--remaining
) strcat(arguments
, ", ");
1977 if (mask
& WINED3DSP_WRITEMASK_1
)
1979 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
1980 if (--remaining
) strcat(arguments
, ", ");
1982 if (mask
& WINED3DSP_WRITEMASK_2
)
1984 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
1985 if (--remaining
) strcat(arguments
, ", ");
1987 if (mask
& WINED3DSP_WRITEMASK_3
)
1989 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
1990 if (--remaining
) strcat(arguments
, ", ");
1995 shader_addline(ins
->ctx
->buffer
, "%s%s = vec%u(%s);\n",
1996 dst_param
.reg_name
, dst_param
.mask_str
, mask_size
, arguments
);
2000 shader_addline(ins
->ctx
->buffer
, "%s%s = %s;\n", dst_param
.reg_name
, dst_param
.mask_str
, arguments
);
2004 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
2005 DWORD sampler
, const struct glsl_sample_function
*sample_function
, DWORD swizzle
,
2006 const char *dx
, const char *dy
, const char *bias
, const char *coord_reg_fmt
, ...)
2008 const char *sampler_base
;
2009 char dst_swizzle
[6];
2010 struct color_fixup_desc fixup
;
2011 BOOL np2_fixup
= FALSE
;
2014 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
2016 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
2018 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2019 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
2020 sampler_base
= "Psampler";
2022 if(priv
->cur_ps_args
->np2_fixup
& (1 << sampler
)) {
2024 FIXME("Biased sampling from NP2 textures is unsupported\n");
2030 sampler_base
= "Vsampler";
2031 fixup
= COLOR_FIXUP_IDENTITY
; /* FIXME: Vshader color fixup */
2034 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2036 shader_addline(ins
->ctx
->buffer
, "%s(%s%u, ", sample_function
->name
, sampler_base
, sampler
);
2038 va_start(args
, coord_reg_fmt
);
2039 shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
2043 shader_addline(ins
->ctx
->buffer
, ", %s)%s);\n", bias
, dst_swizzle
);
2046 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2047 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler
];
2049 shader_addline(ins
->ctx
->buffer
, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx
>> 1,
2050 (idx
% 2) ? "zw" : "xy", dst_swizzle
);
2051 } else if(dx
&& dy
) {
2052 shader_addline(ins
->ctx
->buffer
, ", %s, %s)%s);\n", dx
, dy
, dst_swizzle
);
2054 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
2058 if(!is_identity_fixup(fixup
)) {
2059 shader_glsl_color_correction(ins
, fixup
);
2063 /*****************************************************************************
2064 * Begin processing individual instruction opcodes
2065 ****************************************************************************/
2067 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
2068 static void shader_glsl_arith(const struct wined3d_shader_instruction
*ins
)
2070 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2071 struct glsl_src_param src0_param
;
2072 struct glsl_src_param src1_param
;
2076 /* Determine the GLSL operator to use based on the opcode */
2077 switch (ins
->handler_idx
)
2079 case WINED3DSIH_MUL
: op
= '*'; break;
2080 case WINED3DSIH_ADD
: op
= '+'; break;
2081 case WINED3DSIH_SUB
: op
= '-'; break;
2084 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2088 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2089 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2090 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2091 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
2094 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2095 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
2097 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2098 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2099 struct glsl_src_param src0_param
;
2102 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2103 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2105 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2106 * shader versions WINED3DSIO_MOVA is used for this. */
2107 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
2108 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
2109 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
2111 /* This is a simple floor() */
2112 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2113 if (mask_size
> 1) {
2114 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
2116 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
2119 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
2121 /* We need to *round* to the nearest int here. */
2122 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2124 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2127 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
2129 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
2134 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2135 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
2137 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2138 src0_param
.param_str
, src0_param
.param_str
);
2143 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
2147 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2148 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
2150 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2151 struct glsl_src_param src0_param
;
2152 struct glsl_src_param src1_param
;
2153 DWORD dst_write_mask
, src_write_mask
;
2154 unsigned int dst_size
= 0;
2156 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2157 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2159 /* dp3 works on vec3, dp4 on vec4 */
2160 if (ins
->handler_idx
== WINED3DSIH_DP4
)
2162 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
2164 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2167 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
2168 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
2171 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
2173 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
2177 /* Note that this instruction has some restrictions. The destination write mask
2178 * can't contain the w component, and the source swizzles have to be .xyzw */
2179 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
2181 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2182 struct glsl_src_param src0_param
;
2183 struct glsl_src_param src1_param
;
2186 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2187 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2188 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2189 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
2190 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
2193 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2194 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2195 * GLSL uses the value as-is. */
2196 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
2198 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2199 struct glsl_src_param src0_param
;
2200 struct glsl_src_param src1_param
;
2201 DWORD dst_write_mask
;
2202 unsigned int dst_size
;
2204 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2205 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2207 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2208 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2212 shader_addline(buffer
, "vec%u(%s == 0.0 ? 1.0 : pow(abs(%s), %s)));\n",
2213 dst_size
, src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
2217 shader_addline(buffer
, "%s == 0.0 ? 1.0 : pow(abs(%s), %s));\n",
2218 src1_param
.param_str
, src0_param
.param_str
, src1_param
.param_str
);
2222 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2223 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2224 * GLSL uses the value as-is. */
2225 static void shader_glsl_log(const struct wined3d_shader_instruction
*ins
)
2227 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2228 struct glsl_src_param src0_param
;
2229 DWORD dst_write_mask
;
2230 unsigned int dst_size
;
2232 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2233 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2235 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2239 shader_addline(buffer
, "vec%u(log2(abs(%s))));\n",
2240 dst_size
, src0_param
.param_str
);
2244 shader_addline(buffer
, "log2(abs(%s)));\n",
2245 src0_param
.param_str
);
2249 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2250 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
2252 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2253 struct glsl_src_param src_param
;
2254 const char *instruction
;
2258 /* Determine the GLSL function to use based on the opcode */
2259 /* TODO: Possibly make this a table for faster lookups */
2260 switch (ins
->handler_idx
)
2262 case WINED3DSIH_MIN
: instruction
= "min"; break;
2263 case WINED3DSIH_MAX
: instruction
= "max"; break;
2264 case WINED3DSIH_ABS
: instruction
= "abs"; break;
2265 case WINED3DSIH_FRC
: instruction
= "fract"; break;
2266 case WINED3DSIH_EXP
: instruction
= "exp2"; break;
2267 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
2268 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
2269 default: instruction
= "";
2270 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2274 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2276 shader_addline(buffer
, "%s(", instruction
);
2280 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
2281 shader_addline(buffer
, "%s", src_param
.param_str
);
2282 for (i
= 1; i
< ins
->src_count
; ++i
)
2284 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
2285 shader_addline(buffer
, ", %s", src_param
.param_str
);
2289 shader_addline(buffer
, "));\n");
2292 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
2294 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2295 struct glsl_src_param src_param
;
2296 unsigned int mask_size
;
2300 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
2301 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2302 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
2304 shader_addline(buffer
, "tmp0.x = dot(%s, %s);\n",
2305 src_param
.param_str
, src_param
.param_str
);
2306 shader_glsl_append_dst(buffer
, ins
);
2310 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s * inversesqrt(tmp0.x)));\n",
2311 mask_size
, src_param
.param_str
);
2315 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s * inversesqrt(tmp0.x)));\n",
2316 src_param
.param_str
);
2320 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2321 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2322 * dst.x = 2^(floor(src))
2323 * dst.y = src - floor(src)
2324 * dst.z = 2^src (partial precision is allowed, but optional)
2326 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2327 * dst = 2^src; (partial precision is allowed, but optional)
2329 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
2331 struct glsl_src_param src_param
;
2333 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
2335 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
2339 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
2340 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
2341 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
2342 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
2344 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2345 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2346 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
2349 unsigned int mask_size
;
2351 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2352 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2354 if (mask_size
> 1) {
2355 shader_addline(ins
->ctx
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
2357 shader_addline(ins
->ctx
->buffer
, "exp2(%s));\n", src_param
.param_str
);
2362 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2363 static void shader_glsl_rcp(const struct wined3d_shader_instruction
*ins
)
2365 struct glsl_src_param src_param
;
2367 unsigned int mask_size
;
2369 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2370 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2371 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
2375 shader_addline(ins
->ctx
->buffer
, "vec%u(1.0 / %s));\n",
2376 mask_size
, src_param
.param_str
);
2380 shader_addline(ins
->ctx
->buffer
, "1.0 / %s);\n",
2381 src_param
.param_str
);
2385 static void shader_glsl_rsq(const struct wined3d_shader_instruction
*ins
)
2387 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2388 struct glsl_src_param src_param
;
2390 unsigned int mask_size
;
2392 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2393 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2395 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
2399 shader_addline(buffer
, "vec%u(inversesqrt(abs(%s))));\n",
2400 mask_size
, src_param
.param_str
);
2404 shader_addline(buffer
, "inversesqrt(abs(%s)));\n",
2405 src_param
.param_str
);
2409 /** Process signed comparison opcodes in GLSL. */
2410 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
2412 struct glsl_src_param src0_param
;
2413 struct glsl_src_param src1_param
;
2415 unsigned int mask_size
;
2417 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2418 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2419 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2420 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2422 if (mask_size
> 1) {
2423 const char *compare
;
2425 switch(ins
->handler_idx
)
2427 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
2428 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
2429 default: compare
= "";
2430 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2433 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
2434 src0_param
.param_str
, src1_param
.param_str
);
2436 switch(ins
->handler_idx
)
2438 case WINED3DSIH_SLT
:
2439 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2440 * to return 0.0 but step returns 1.0 because step is not < x
2441 * An alternative is a bvec compare padded with an unused second component.
2442 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2443 * issue. Playing with not() is not possible either because not() does not accept
2446 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
2447 src0_param
.param_str
, src1_param
.param_str
);
2449 case WINED3DSIH_SGE
:
2450 /* Here we can use the step() function and safe a conditional */
2451 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
2454 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2460 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2461 static void shader_glsl_cmp(const struct wined3d_shader_instruction
*ins
)
2463 struct glsl_src_param src0_param
;
2464 struct glsl_src_param src1_param
;
2465 struct glsl_src_param src2_param
;
2466 DWORD write_mask
, cmp_channel
= 0;
2469 BOOL temp_destination
= FALSE
;
2471 if (shader_is_scalar(&ins
->src
[0].reg
))
2473 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2475 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2476 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2477 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2479 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2480 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2482 DWORD dst_mask
= ins
->dst
[0].write_mask
;
2483 struct wined3d_shader_dst_param dst
= ins
->dst
[0];
2485 /* Cycle through all source0 channels */
2486 for (i
=0; i
<4; i
++) {
2488 /* Find the destination channels which use the current source0 channel */
2489 for (j
=0; j
<4; j
++) {
2490 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2492 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2493 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2496 dst
.write_mask
= dst_mask
& write_mask
;
2498 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2499 * The first lines may overwrite source parameters of the following lines.
2500 * Deal with that by using a temporary destination register if needed
2502 if ((ins
->src
[0].reg
.idx
== ins
->dst
[0].reg
.idx
2503 && ins
->src
[0].reg
.type
== ins
->dst
[0].reg
.type
)
2504 || (ins
->src
[1].reg
.idx
== ins
->dst
[0].reg
.idx
2505 && ins
->src
[1].reg
.type
== ins
->dst
[0].reg
.type
)
2506 || (ins
->src
[2].reg
.idx
== ins
->dst
[0].reg
.idx
2507 && ins
->src
[2].reg
.type
== ins
->dst
[0].reg
.type
))
2509 write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
);
2510 if (!write_mask
) continue;
2511 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
2512 temp_destination
= TRUE
;
2514 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2515 if (!write_mask
) continue;
2518 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2519 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2520 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2522 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2523 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2526 if(temp_destination
) {
2527 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
2528 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2529 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
2535 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2536 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2537 * the compare is done per component of src0. */
2538 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
2540 struct wined3d_shader_dst_param dst
;
2541 struct glsl_src_param src0_param
;
2542 struct glsl_src_param src1_param
;
2543 struct glsl_src_param src2_param
;
2544 DWORD write_mask
, cmp_channel
= 0;
2547 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2548 ins
->ctx
->reg_maps
->shader_version
.minor
);
2550 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
2552 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2553 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2554 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2555 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2557 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2560 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
2562 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2563 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2567 /* Cycle through all source0 channels */
2568 dst_mask
= ins
->dst
[0].write_mask
;
2570 for (i
=0; i
<4; i
++) {
2572 /* Find the destination channels which use the current source0 channel */
2573 for (j
=0; j
<4; j
++) {
2574 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2576 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2577 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2581 dst
.write_mask
= dst_mask
& write_mask
;
2582 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2583 if (!write_mask
) continue;
2585 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2586 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2587 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2589 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2590 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2594 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2595 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
2597 struct glsl_src_param src0_param
;
2598 struct glsl_src_param src1_param
;
2599 struct glsl_src_param src2_param
;
2602 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2603 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2604 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2605 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2606 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
2607 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2610 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2611 Vertex shaders to GLSL codes */
2612 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
2615 int nComponents
= 0;
2616 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2617 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2618 struct wined3d_shader_instruction tmp_ins
;
2620 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2622 /* Set constants for the temporary argument */
2623 tmp_ins
.ctx
= ins
->ctx
;
2624 tmp_ins
.dst_count
= 1;
2625 tmp_ins
.dst
= &tmp_dst
;
2626 tmp_ins
.src_count
= 2;
2627 tmp_ins
.src
= tmp_src
;
2629 switch(ins
->handler_idx
)
2631 case WINED3DSIH_M4x4
:
2633 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2635 case WINED3DSIH_M4x3
:
2637 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2639 case WINED3DSIH_M3x4
:
2641 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2643 case WINED3DSIH_M3x3
:
2645 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2647 case WINED3DSIH_M3x2
:
2649 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2655 tmp_dst
= ins
->dst
[0];
2656 tmp_src
[0] = ins
->src
[0];
2657 tmp_src
[1] = ins
->src
[1];
2658 for (i
= 0; i
< nComponents
; ++i
)
2660 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2661 shader_glsl_dot(&tmp_ins
);
2662 ++tmp_src
[1].reg
.idx
;
2667 The LRP instruction performs a component-wise linear interpolation
2668 between the second and third operands using the first operand as the
2669 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2670 This is equivalent to mix(src2, src1, src0);
2672 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
2674 struct glsl_src_param src0_param
;
2675 struct glsl_src_param src1_param
;
2676 struct glsl_src_param src2_param
;
2679 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2681 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2682 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2683 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2685 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
2686 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
2689 /** Process the WINED3DSIO_LIT instruction in GLSL:
2690 * dst.x = dst.w = 1.0
2691 * dst.y = (src0.x > 0) ? src0.x
2692 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2693 * where src.w is clamped at +- 128
2695 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
2697 struct glsl_src_param src0_param
;
2698 struct glsl_src_param src1_param
;
2699 struct glsl_src_param src3_param
;
2702 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2703 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2705 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2706 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
2707 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
2709 /* The sdk specifies the instruction like this
2711 * if(src.x > 0.0) dst.y = src.x
2713 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2716 * (where power = src.w clamped between -128 and 128)
2718 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2719 * dst.x = 1.0 ... No further explanation needed
2720 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2721 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2722 * dst.w = 1.0. ... Nothing fancy.
2724 * So we still have one conditional in there. So do this:
2725 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2727 * 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),
2728 * 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.
2729 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to.
2731 * Unfortunately pow(0.0 ^ 0.0) returns NaN on most GPUs, but lit with src.y = 0 and src.w = 0 returns
2732 * a non-NaN value in dst.z. What we return doesn't matter, as long as it is not NaN. Return 0, which is
2733 * what all Windows HW drivers and GL_ARB_vertex_program's LIT do.
2735 shader_addline(ins
->ctx
->buffer
,
2736 "vec4(1.0, max(%s, 0.0), %s == 0.0 ? 0.0 : "
2737 "pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2738 src0_param
.param_str
, src3_param
.param_str
, src1_param
.param_str
,
2739 src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
2742 /** Process the WINED3DSIO_DST instruction in GLSL:
2744 * dst.y = src0.x * src0.y
2748 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
2750 struct glsl_src_param src0y_param
;
2751 struct glsl_src_param src0z_param
;
2752 struct glsl_src_param src1y_param
;
2753 struct glsl_src_param src1w_param
;
2756 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2757 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2759 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
2760 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
2761 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
2762 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
2764 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2765 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
2768 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2769 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2770 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2772 * dst.x = cos(src0.?)
2773 * dst.y = sin(src0.?)
2777 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
2779 struct glsl_src_param src0_param
;
2782 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2783 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2785 switch (write_mask
) {
2786 case WINED3DSP_WRITEMASK_0
:
2787 shader_addline(ins
->ctx
->buffer
, "cos(%s));\n", src0_param
.param_str
);
2790 case WINED3DSP_WRITEMASK_1
:
2791 shader_addline(ins
->ctx
->buffer
, "sin(%s));\n", src0_param
.param_str
);
2794 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
2795 shader_addline(ins
->ctx
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
2799 ERR("Write mask should be .x, .y or .xy\n");
2804 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2805 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2806 * generate invalid code
2808 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
2810 struct glsl_src_param src0_param
;
2813 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2814 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2816 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
2819 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2820 * Start a for() loop where src1.y is the initial value of aL,
2821 * increment aL by src1.z for a total of src1.x iterations.
2822 * Need to use a temporary variable for this operation.
2824 /* FIXME: I don't think nested loops will work correctly this way. */
2825 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
2827 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
2828 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2829 struct glsl_src_param src1_param
;
2830 const DWORD
*control_values
= NULL
;
2831 const local_constant
*constant
;
2833 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
2835 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2836 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2837 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2840 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
2842 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, local_constant
, entry
)
2844 if (constant
->idx
== ins
->src
[1].reg
.idx
)
2846 control_values
= constant
->value
;
2854 struct wined3d_shader_loop_control loop_control
;
2855 loop_control
.count
= control_values
[0];
2856 loop_control
.start
= control_values
[1];
2857 loop_control
.step
= (int)control_values
[2];
2859 if (loop_control
.step
> 0)
2861 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2862 loop_state
->current_depth
, loop_control
.start
,
2863 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
2864 loop_state
->current_depth
, loop_control
.step
);
2866 else if (loop_control
.step
< 0)
2868 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2869 loop_state
->current_depth
, loop_control
.start
,
2870 loop_state
->current_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
2871 loop_state
->current_depth
, loop_control
.step
);
2875 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2876 loop_state
->current_depth
, loop_control
.start
, loop_state
->current_depth
,
2877 loop_state
->current_depth
, loop_control
.count
,
2878 loop_state
->current_depth
);
2881 shader_addline(ins
->ctx
->buffer
,
2882 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2883 loop_state
->current_depth
, loop_state
->current_reg
,
2884 src1_param
.reg_name
, loop_state
->current_depth
, src1_param
.reg_name
,
2885 loop_state
->current_depth
, loop_state
->current_reg
, src1_param
.reg_name
);
2888 ++loop_state
->current_depth
;
2889 ++loop_state
->current_reg
;
2892 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
2894 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
2896 shader_addline(ins
->ctx
->buffer
, "}\n");
2898 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
2900 --loop_state
->current_depth
;
2901 --loop_state
->current_reg
;
2904 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
2906 --loop_state
->current_depth
;
2910 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
2912 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
2913 struct wined3d_shader_loop_state
*loop_state
= ins
->ctx
->loop_state
;
2914 struct glsl_src_param src0_param
;
2915 const DWORD
*control_values
= NULL
;
2916 const local_constant
*constant
;
2918 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2919 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
2921 LIST_FOR_EACH_ENTRY(constant
, &shader
->constantsI
, local_constant
, entry
)
2923 if (constant
->idx
== ins
->src
[0].reg
.idx
)
2925 control_values
= constant
->value
;
2933 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2934 loop_state
->current_depth
, loop_state
->current_depth
,
2935 control_values
[0], loop_state
->current_depth
);
2939 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2940 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2941 loop_state
->current_depth
, loop_state
->current_depth
,
2942 src0_param
.param_str
, loop_state
->current_depth
);
2945 ++loop_state
->current_depth
;
2948 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
2950 struct glsl_src_param src0_param
;
2952 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2953 shader_addline(ins
->ctx
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2956 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
2958 struct glsl_src_param src0_param
;
2959 struct glsl_src_param src1_param
;
2961 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2962 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2964 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
2965 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2968 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
2970 shader_addline(ins
->ctx
->buffer
, "} else {\n");
2973 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
2975 shader_addline(ins
->ctx
->buffer
, "break;\n");
2978 /* FIXME: According to MSDN the compare is done per component. */
2979 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
2981 struct glsl_src_param src0_param
;
2982 struct glsl_src_param src1_param
;
2984 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2985 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2987 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
2988 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2991 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
2993 shader_addline(ins
->ctx
->buffer
, "}\n");
2994 shader_addline(ins
->ctx
->buffer
, "void subroutine%u () {\n", ins
->src
[0].reg
.idx
);
2997 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
2999 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
);
3002 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
3004 struct glsl_src_param src1_param
;
3006 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
3007 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, ins
->src
[0].reg
.idx
);
3010 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
3012 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3013 * function only suppresses the unhandled instruction warning
3017 /*********************************************
3018 * Pixel Shader Specific Code begins here
3019 ********************************************/
3020 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
3022 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3023 struct wined3d_device
*device
= shader
->device
;
3024 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3025 ins
->ctx
->reg_maps
->shader_version
.minor
);
3026 struct glsl_sample_function sample_function
;
3027 const struct wined3d_texture
*texture
;
3028 DWORD sample_flags
= 0;
3030 DWORD mask
= 0, swizzle
;
3032 /* 1.0-1.4: Use destination register as sampler source.
3033 * 2.0+: Use provided sampler source. */
3034 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) sampler_idx
= ins
->dst
[0].reg
.idx
;
3035 else sampler_idx
= ins
->src
[1].reg
.idx
;
3036 texture
= device
->stateBlock
->state
.textures
[sampler_idx
];
3038 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3040 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3041 DWORD flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
3042 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
3043 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3045 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3046 if (flags
& WINED3D_PSARGS_PROJECTED
&& sampler_type
!= WINED3DSTT_CUBE
) {
3047 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3048 switch (flags
& ~WINED3D_PSARGS_PROJECTED
) {
3049 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3050 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
3051 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
3052 case WINED3DTTFF_COUNT4
:
3053 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
3057 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
3059 DWORD src_mod
= ins
->src
[0].modifiers
;
3061 if (src_mod
== WINED3DSPSM_DZ
) {
3062 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3063 mask
= WINED3DSP_WRITEMASK_2
;
3064 } else if (src_mod
== WINED3DSPSM_DW
) {
3065 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3066 mask
= WINED3DSP_WRITEMASK_3
;
3069 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
3071 /* ps 2.0 texldp instruction always divides by the fourth component. */
3072 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3073 mask
= WINED3DSP_WRITEMASK_3
;
3077 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
3078 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3080 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3081 mask
|= sample_function
.coord_mask
;
3083 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
3084 else swizzle
= ins
->src
[1].swizzle
;
3086 /* 1.0-1.3: Use destination register as coordinate source.
3087 1.4+: Use provided coordinate source register. */
3088 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3091 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3092 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3093 "T%u%s", sampler_idx
, coord_mask
);
3097 struct glsl_src_param coord_param
;
3098 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
3099 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
3101 struct glsl_src_param bias
;
3102 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
3103 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
3104 "%s", coord_param
.param_str
);
3106 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3107 "%s", coord_param
.param_str
);
3112 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
3114 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3115 struct wined3d_device
*device
= shader
->device
;
3116 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3117 struct glsl_src_param coord_param
, dx_param
, dy_param
;
3118 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
3119 struct glsl_sample_function sample_function
;
3121 DWORD swizzle
= ins
->src
[1].swizzle
;
3122 const struct wined3d_texture
*texture
;
3124 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
])
3126 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3127 shader_glsl_tex(ins
);
3131 sampler_idx
= ins
->src
[1].reg
.idx
;
3132 texture
= device
->stateBlock
->state
.textures
[sampler_idx
];
3133 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
3134 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3136 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3137 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3138 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
3139 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
3141 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
3142 "%s", coord_param
.param_str
);
3145 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
3147 const struct wined3d_shader
*shader
= ins
->ctx
->shader
;
3148 struct wined3d_device
*device
= shader
->device
;
3149 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3150 struct glsl_src_param coord_param
, lod_param
;
3151 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
3152 struct glsl_sample_function sample_function
;
3154 DWORD swizzle
= ins
->src
[1].swizzle
;
3155 const struct wined3d_texture
*texture
;
3157 sampler_idx
= ins
->src
[1].reg
.idx
;
3158 texture
= device
->stateBlock
->state
.textures
[sampler_idx
];
3159 if (texture
&& texture
->target
== GL_TEXTURE_RECTANGLE_ARB
)
3160 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3162 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3163 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3165 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
3167 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
]
3168 && shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
3170 /* Plain GLSL only supports Lod sampling functions in vertex shaders.
3171 * However, the NVIDIA drivers allow them in fragment shaders as well,
3172 * even without the appropriate extension. */
3173 WARN("Using %s in fragment shader.\n", sample_function
.name
);
3175 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
3176 "%s", coord_param
.param_str
);
3179 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
3181 /* FIXME: Make this work for more than just 2D textures */
3182 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3183 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3185 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
3189 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3190 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3191 ins
->dst
[0].reg
.idx
, dst_mask
);
3193 DWORD reg
= ins
->src
[0].reg
.idx
;
3194 DWORD src_mod
= ins
->src
[0].modifiers
;
3195 char dst_swizzle
[6];
3197 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
3199 if (src_mod
== WINED3DSPSM_DZ
)
3201 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3202 struct glsl_src_param div_param
;
3204 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
3206 if (mask_size
> 1) {
3207 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
3209 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
3212 else if (src_mod
== WINED3DSPSM_DW
)
3214 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3215 struct glsl_src_param div_param
;
3217 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
3219 if (mask_size
> 1) {
3220 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
3222 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
3225 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
3230 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3231 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3232 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3233 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
3235 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3236 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3237 struct glsl_sample_function sample_function
;
3238 struct glsl_src_param src0_param
;
3241 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3243 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3244 * scalar, and projected sampling would require 4.
3246 * It is a dependent read - not valid with conditional NP2 textures
3248 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3249 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
3254 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3255 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
3259 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3260 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
3264 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3265 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
3269 FIXME("Unexpected mask size %u\n", mask_size
);
3274 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3275 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3276 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
3278 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
3279 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3280 struct glsl_src_param src0_param
;
3282 unsigned int mask_size
;
3284 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3285 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
3286 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3288 if (mask_size
> 1) {
3289 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
3291 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
3295 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3296 * Calculate the depth as dst.x / dst.y */
3297 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
3299 struct glsl_dst_param dst_param
;
3301 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3303 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3304 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3305 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3306 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3309 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3310 dst_param
.reg_name
, dst_param
.reg_name
);
3313 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3314 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3315 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3316 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3318 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
3320 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3321 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
3322 struct glsl_src_param src0_param
;
3324 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3326 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
3327 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3330 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3331 * Calculate the 1st of a 2-row matrix multiplication. */
3332 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
3334 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3335 DWORD reg
= ins
->dst
[0].reg
.idx
;
3336 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3337 struct glsl_src_param src0_param
;
3339 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3340 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3343 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3344 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3345 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
3347 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3348 DWORD reg
= ins
->dst
[0].reg
.idx
;
3349 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3350 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
3351 struct glsl_src_param src0_param
;
3353 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3354 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + tex_mx
->current_row
, reg
, src0_param
.param_str
);
3355 tex_mx
->texcoord_w
[tex_mx
->current_row
++] = reg
;
3358 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
3360 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3361 DWORD reg
= ins
->dst
[0].reg
.idx
;
3362 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3363 struct glsl_sample_function sample_function
;
3364 struct glsl_src_param src0_param
;
3366 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3367 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3369 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3371 /* Sample the texture using the calculated coordinates */
3372 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
3375 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3376 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3377 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
3379 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3380 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
3381 struct glsl_sample_function sample_function
;
3382 struct glsl_src_param src0_param
;
3383 DWORD reg
= ins
->dst
[0].reg
.idx
;
3385 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3386 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3388 /* Dependent read, not valid with conditional NP2 */
3389 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3391 /* Sample the texture using the calculated coordinates */
3392 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3394 tex_mx
->current_row
= 0;
3397 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3398 * Perform the 3rd row of a 3x3 matrix multiply */
3399 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
3401 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3402 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
3403 struct glsl_src_param src0_param
;
3405 DWORD reg
= ins
->dst
[0].reg
.idx
;
3407 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3409 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3410 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3411 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
3413 tex_mx
->current_row
= 0;
3416 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3417 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3418 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
3420 struct glsl_src_param src0_param
;
3421 struct glsl_src_param src1_param
;
3422 DWORD reg
= ins
->dst
[0].reg
.idx
;
3423 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3424 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
3425 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3426 struct glsl_sample_function sample_function
;
3428 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3429 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3431 /* Perform the last matrix multiply operation */
3432 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3433 /* Reflection calculation */
3434 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
3436 /* Dependent read, not valid with conditional NP2 */
3437 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3439 /* Sample the texture */
3440 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3442 tex_mx
->current_row
= 0;
3445 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3446 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3447 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
3449 DWORD reg
= ins
->dst
[0].reg
.idx
;
3450 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3451 struct wined3d_shader_tex_mx
*tex_mx
= ins
->ctx
->tex_mx
;
3452 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3453 struct glsl_sample_function sample_function
;
3454 struct glsl_src_param src0_param
;
3456 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3458 /* Perform the last matrix multiply operation */
3459 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
3461 /* Construct the eye-ray vector from w coordinates */
3462 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3463 tex_mx
->texcoord_w
[0], tex_mx
->texcoord_w
[1], reg
);
3464 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3466 /* Dependent read, not valid with conditional NP2 */
3467 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3469 /* Sample the texture using the calculated coordinates */
3470 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3472 tex_mx
->current_row
= 0;
3475 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3476 * Apply a fake bump map transform.
3477 * texbem is pshader <= 1.3 only, this saves a few version checks
3479 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
3481 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
3482 struct glsl_sample_function sample_function
;
3483 struct glsl_src_param coord_param
;
3489 sampler_idx
= ins
->dst
[0].reg
.idx
;
3490 flags
= (priv
->cur_ps_args
->tex_transform
>> sampler_idx
* WINED3D_PSARGS_TEXTRANSFORM_SHIFT
)
3491 & WINED3D_PSARGS_TEXTRANSFORM_MASK
;
3493 /* Dependent read, not valid with conditional NP2 */
3494 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3495 mask
= sample_function
.coord_mask
;
3497 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3499 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3500 * so we can't let the GL handle this.
3502 if (flags
& WINED3D_PSARGS_PROJECTED
) {
3504 char coord_div_mask
[3];
3505 switch (flags
& ~WINED3D_PSARGS_PROJECTED
) {
3506 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3507 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
3508 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
3509 case WINED3DTTFF_COUNT4
:
3510 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
3512 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
3513 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
3516 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
3518 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3519 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
3520 coord_param
.param_str
, coord_mask
);
3522 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
3524 struct glsl_src_param luminance_param
;
3525 struct glsl_dst_param dst_param
;
3527 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
3528 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3530 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3531 dst_param
.reg_name
, dst_param
.mask_str
,
3532 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
3536 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
3538 struct glsl_src_param src0_param
, src1_param
;
3539 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3541 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3542 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3544 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3545 shader_addline(ins
->ctx
->buffer
, "%s + bumpenvmat%d * %s);\n",
3546 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
3549 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3550 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3551 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
3553 struct glsl_sample_function sample_function
;
3554 struct glsl_src_param src0_param
;
3555 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3557 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3559 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3560 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3561 "%s.wx", src0_param
.reg_name
);
3564 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3565 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3566 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
3568 struct glsl_sample_function sample_function
;
3569 struct glsl_src_param src0_param
;
3570 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3572 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3574 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3575 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3576 "%s.yz", src0_param
.reg_name
);
3579 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3580 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3581 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
3583 struct glsl_sample_function sample_function
;
3584 struct glsl_src_param src0_param
;
3585 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3587 /* Dependent read, not valid with conditional NP2 */
3588 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3589 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
3591 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3592 "%s", src0_param
.param_str
);
3595 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3596 * If any of the first 3 components are < 0, discard this pixel */
3597 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
3599 struct glsl_dst_param dst_param
;
3601 /* The argument is a destination parameter, and no writemasks are allowed */
3602 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3603 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
3605 /* 2.0 shaders compare all 4 components in texkill */
3606 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
3608 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3609 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3610 * 4 components are defined, only the first 3 are used
3612 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
3616 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3617 * dst = dot2(src0, src1) + src2 */
3618 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
3620 struct glsl_src_param src0_param
;
3621 struct glsl_src_param src1_param
;
3622 struct glsl_src_param src2_param
;
3624 unsigned int mask_size
;
3626 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3627 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3629 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3630 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3631 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
3633 if (mask_size
> 1) {
3634 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
3635 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3637 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
3638 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3642 static void shader_glsl_input_pack(const struct wined3d_shader
*shader
, struct wined3d_shader_buffer
*buffer
,
3643 const struct wined3d_shader_signature_element
*input_signature
,
3644 const struct wined3d_shader_reg_maps
*reg_maps
,
3645 enum vertexprocessing_mode vertexprocessing
)
3647 WORD map
= reg_maps
->input_registers
;
3650 for (i
= 0; map
; map
>>= 1, ++i
)
3652 const char *semantic_name
;
3657 if (!(map
& 1)) continue;
3659 semantic_name
= input_signature
[i
].semantic_name
;
3660 semantic_idx
= input_signature
[i
].semantic_idx
;
3661 shader_glsl_write_mask_to_str(input_signature
[i
].mask
, reg_mask
);
3663 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3665 if (semantic_idx
< 8 && vertexprocessing
== pretransformed
)
3666 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3667 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, semantic_idx
, reg_mask
);
3669 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3670 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, reg_mask
);
3672 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3675 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
3676 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, reg_mask
);
3677 else if (semantic_idx
== 1)
3678 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3679 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, reg_mask
);
3681 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3682 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, reg_mask
);
3686 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3687 shader
->u
.ps
.input_reg_map
[i
], reg_mask
, reg_mask
);
3692 /*********************************************
3693 * Vertex Shader Specific Code begins here
3694 ********************************************/
3696 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
)
3698 struct glsl_program_key key
;
3700 key
.vshader
= entry
->vshader
;
3701 key
.pshader
= entry
->pshader
;
3702 key
.vs_args
= entry
->vs_args
;
3703 key
.ps_args
= entry
->ps_args
;
3705 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
3707 ERR("Failed to insert program entry.\n");
3711 static struct glsl_shader_prog_link
*get_glsl_program_entry(const struct shader_glsl_priv
*priv
,
3712 const struct wined3d_shader
*vshader
, const struct wined3d_shader
*pshader
,
3713 const struct vs_compile_args
*vs_args
, const struct ps_compile_args
*ps_args
)
3715 struct wine_rb_entry
*entry
;
3716 struct glsl_program_key key
;
3718 key
.vshader
= vshader
;
3719 key
.pshader
= pshader
;
3720 key
.vs_args
= *vs_args
;
3721 key
.ps_args
= *ps_args
;
3723 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
3724 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
3727 /* GL locking is done by the caller */
3728 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
3729 struct glsl_shader_prog_link
*entry
)
3731 struct glsl_program_key key
;
3733 key
.vshader
= entry
->vshader
;
3734 key
.pshader
= entry
->pshader
;
3735 key
.vs_args
= entry
->vs_args
;
3736 key
.ps_args
= entry
->ps_args
;
3737 wine_rb_remove(&priv
->program_lookup
, &key
);
3739 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
3740 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
3741 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
3742 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
3743 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
3744 HeapFree(GetProcessHeap(), 0, entry
);
3747 static void handle_ps3_input(struct wined3d_shader_buffer
*buffer
,
3748 const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
3749 const struct wined3d_shader_signature_element
*input_signature
,
3750 const struct wined3d_shader_reg_maps
*reg_maps_in
,
3751 const struct wined3d_shader_signature_element
*output_signature
,
3752 const struct wined3d_shader_reg_maps
*reg_maps_out
)
3755 const char *semantic_name_in
;
3756 UINT semantic_idx_in
;
3759 unsigned int in_count
= vec4_varyings(3, gl_info
);
3761 char destination
[50];
3762 WORD input_map
, output_map
;
3764 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
3766 input_map
= reg_maps_in
->input_registers
;
3767 for (i
= 0; input_map
; input_map
>>= 1, ++i
)
3769 if (!(input_map
& 1)) continue;
3772 /* Declared, but not read register */
3773 if (in_idx
== ~0U) continue;
3774 if (in_idx
>= (in_count
+ 2))
3776 FIXME("More input varyings declared than supported, expect issues.\n");
3780 if (in_idx
== in_count
) {
3781 sprintf(destination
, "gl_FrontColor");
3782 } else if (in_idx
== in_count
+ 1) {
3783 sprintf(destination
, "gl_FrontSecondaryColor");
3785 sprintf(destination
, "IN[%u]", in_idx
);
3788 semantic_name_in
= input_signature
[i
].semantic_name
;
3789 semantic_idx_in
= input_signature
[i
].semantic_idx
;
3792 output_map
= reg_maps_out
->output_registers
;
3793 for (j
= 0; output_map
; output_map
>>= 1, ++j
)
3797 if (!(output_map
& 1)
3798 || semantic_idx_in
!= output_signature
[j
].semantic_idx
3799 || strcmp(semantic_name_in
, output_signature
[j
].semantic_name
)
3800 || !(mask
= input_signature
[i
].mask
& output_signature
[j
].mask
))
3804 shader_glsl_write_mask_to_str(mask
, reg_mask
);
3806 shader_addline(buffer
, "%s%s = OUT[%u]%s;\n",
3807 destination
, reg_mask
, j
, reg_mask
);
3811 for (i
= 0; i
< in_count
+ 2; ++i
)
3815 if (!set
[i
] || set
[i
] == WINED3DSP_WRITEMASK_ALL
)
3818 if (set
[i
] == ~0U) set
[i
] = 0;
3821 if (!(set
[i
] & WINED3DSP_WRITEMASK_0
)) reg_mask
[size
++] = 'x';
3822 if (!(set
[i
] & WINED3DSP_WRITEMASK_1
)) reg_mask
[size
++] = 'y';
3823 if (!(set
[i
] & WINED3DSP_WRITEMASK_2
)) reg_mask
[size
++] = 'z';
3824 if (!(set
[i
] & WINED3DSP_WRITEMASK_3
)) reg_mask
[size
++] = 'w';
3825 reg_mask
[size
] = '\0';
3827 if (i
== in_count
) sprintf(destination
, "gl_FrontColor");
3828 else if (i
== in_count
+ 1) sprintf(destination
, "gl_FrontSecondaryColor");
3829 else sprintf(destination
, "IN[%u]", i
);
3831 if (size
== 1) shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
3832 else shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
3835 HeapFree(GetProcessHeap(), 0, set
);
3838 /* GL locking is done by the caller */
3839 static GLhandleARB
generate_param_reorder_function(struct wined3d_shader_buffer
*buffer
,
3840 const struct wined3d_shader
*vs
, const struct wined3d_shader
*ps
,
3841 const struct wined3d_gl_info
*gl_info
)
3843 GLhandleARB ret
= 0;
3844 DWORD ps_major
= ps
? ps
->reg_maps
.shader_version
.major
: 0;
3846 const char *semantic_name
;
3849 const struct wined3d_shader_signature_element
*output_signature
= vs
->output_signature
;
3850 WORD map
= vs
->reg_maps
.output_registers
;
3852 shader_buffer_clear(buffer
);
3854 shader_addline(buffer
, "#version 120\n");
3858 shader_addline(buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3860 for (i
= 0; map
; map
>>= 1, ++i
)
3864 if (!(map
& 1)) continue;
3866 semantic_name
= output_signature
[i
].semantic_name
;
3867 semantic_idx
= output_signature
[i
].semantic_idx
;
3868 write_mask
= output_signature
[i
].mask
;
3869 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
3871 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3874 shader_addline(buffer
, "gl_FrontColor%s = OUT[%u]%s;\n",
3875 reg_mask
, i
, reg_mask
);
3876 else if (semantic_idx
== 1)
3877 shader_addline(buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n",
3878 reg_mask
, i
, reg_mask
);
3880 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3882 shader_addline(buffer
, "gl_Position%s = OUT[%u]%s;\n",
3883 reg_mask
, i
, reg_mask
);
3885 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3887 if (semantic_idx
< 8)
3889 if (!(gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
) || ps_major
> 0)
3890 write_mask
|= WINED3DSP_WRITEMASK_3
;
3892 shader_addline(buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3893 semantic_idx
, reg_mask
, i
, reg_mask
);
3894 if (!(write_mask
& WINED3DSP_WRITEMASK_3
))
3895 shader_addline(buffer
, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx
);
3898 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3900 shader_addline(buffer
, "gl_PointSize = OUT[%u].%c;\n", i
, reg_mask
[1]);
3902 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3904 shader_addline(buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
3907 shader_addline(buffer
, "}\n");
3912 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3913 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info
));
3914 shader_addline(buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3916 /* First, sort out position and point size. Those are not passed to the pixel shader */
3917 for (i
= 0; map
; map
>>= 1, ++i
)
3919 if (!(map
& 1)) continue;
3921 semantic_name
= output_signature
[i
].semantic_name
;
3922 shader_glsl_write_mask_to_str(output_signature
[i
].mask
, reg_mask
);
3924 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3926 shader_addline(buffer
, "gl_Position%s = OUT[%u]%s;\n",
3927 reg_mask
, i
, reg_mask
);
3929 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3931 shader_addline(buffer
, "gl_PointSize = OUT[%u].%c;\n", i
, reg_mask
[1]);
3935 /* Then, fix the pixel shader input */
3936 handle_ps3_input(buffer
, gl_info
, ps
->u
.ps
.input_reg_map
, ps
->input_signature
,
3937 &ps
->reg_maps
, output_signature
, &vs
->reg_maps
);
3939 shader_addline(buffer
, "}\n");
3942 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3943 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3944 shader_glsl_compile(gl_info
, ret
, buffer
->buffer
);
3949 /* GL locking is done by the caller */
3950 static void hardcode_local_constants(const struct wined3d_shader
*shader
,
3951 const struct wined3d_gl_info
*gl_info
, GLhandleARB programId
, char prefix
)
3953 const local_constant
*lconst
;
3958 LIST_FOR_EACH_ENTRY(lconst
, &shader
->constantsF
, local_constant
, entry
)
3960 value
= (const float *)lconst
->value
;
3961 snprintf(glsl_name
, sizeof(glsl_name
), "%cLC%u", prefix
, lconst
->idx
);
3962 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3963 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, value
));
3965 checkGLcall("Hardcoding local constants");
3968 /* GL locking is done by the caller */
3969 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
3970 struct wined3d_shader_buffer
*buffer
, const struct wined3d_shader
*shader
,
3971 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
3973 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
3974 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
3975 const DWORD
*function
= shader
->function
;
3976 struct shader_glsl_ctx_priv priv_ctx
;
3978 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3979 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3981 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3982 priv_ctx
.cur_ps_args
= args
;
3983 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
3985 shader_addline(buffer
, "#version 120\n");
3987 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
3989 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
3991 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
3993 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3994 * drivers write a warning if we don't do so
3996 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
3998 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4000 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4003 /* Base Declarations */
4004 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
4006 /* Pack 3.0 inputs */
4007 if (reg_maps
->shader_version
.major
>= 3 && args
->vp_mode
!= vertexshader
)
4008 shader_glsl_input_pack(shader
, buffer
, shader
->input_signature
, reg_maps
, args
->vp_mode
);
4010 /* Base Shader Body */
4011 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
4013 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4014 if (reg_maps
->shader_version
.major
< 2)
4016 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4017 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
4020 if (args
->srgb_correction
)
4022 shader_addline(buffer
, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4023 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4024 shader_addline(buffer
, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4025 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4026 shader_addline(buffer
, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4027 shader_addline(buffer
, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4029 /* Pixel shader < 3.0 do not replace the fog stage.
4030 * This implements linear fog computation and blending.
4031 * TODO: non linear fog
4032 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4033 * -1/(e-s) and e/(e-s) respectively.
4035 if (reg_maps
->shader_version
.major
< 3)
4038 case FOG_OFF
: break;
4040 shader_addline(buffer
, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4041 shader_addline(buffer
, "float fogend = gl_Fog.end * -fogstart;\n");
4042 shader_addline(buffer
, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4043 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4046 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4047 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4048 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4049 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4052 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4053 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4054 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4055 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4060 shader_addline(buffer
, "}\n");
4062 TRACE("Compiling shader object %u\n", shader_obj
);
4063 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
4065 /* Store the shader object */
4069 /* GL locking is done by the caller */
4070 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
4071 struct wined3d_shader_buffer
*buffer
, const struct wined3d_shader
*shader
,
4072 const struct vs_compile_args
*args
)
4074 const struct wined3d_shader_reg_maps
*reg_maps
= &shader
->reg_maps
;
4075 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4076 const DWORD
*function
= shader
->function
;
4077 struct shader_glsl_ctx_priv priv_ctx
;
4079 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4080 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4082 shader_addline(buffer
, "#version 120\n");
4084 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4085 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4087 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4088 priv_ctx
.cur_vs_args
= args
;
4090 /* Base Declarations */
4091 shader_generate_glsl_declarations(context
, buffer
, shader
, reg_maps
, &priv_ctx
);
4093 /* Base Shader Body */
4094 shader_generate_main(shader
, buffer
, reg_maps
, function
, &priv_ctx
);
4096 /* Unpack outputs */
4097 shader_addline(buffer
, "order_ps_input(OUT);\n");
4099 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4100 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4101 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4102 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4104 if (args
->fog_src
== VS_FOG_Z
)
4105 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
4106 else if (!reg_maps
->fog
)
4107 shader_addline(buffer
, "gl_FogFragCoord = 0.0;\n");
4109 /* We always store the clipplanes without y inversion */
4110 if (args
->clip_enabled
)
4111 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
4113 /* Write the final position.
4115 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4116 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4117 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4118 * contains 1.0 to allow a mad.
4120 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4121 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4123 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4125 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4126 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4127 * which is the same as z = z * 2 - w.
4129 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4131 shader_addline(buffer
, "}\n");
4133 TRACE("Compiling shader object %u\n", shader_obj
);
4134 shader_glsl_compile(gl_info
, shader_obj
, buffer
->buffer
);
4139 static GLhandleARB
find_glsl_pshader(const struct wined3d_context
*context
,
4140 struct wined3d_shader_buffer
*buffer
, struct wined3d_shader
*shader
,
4141 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
4143 struct wined3d_state
*state
= &shader
->device
->stateBlock
->state
;
4146 struct glsl_ps_compiled_shader
*new_array
;
4147 struct glsl_pshader_private
*shader_data
;
4148 struct ps_np2fixup_info
*np2fixup
= NULL
;
4151 if (!shader
->backend_data
)
4153 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4154 if (!shader
->backend_data
)
4156 ERR("Failed to allocate backend data.\n");
4160 shader_data
= shader
->backend_data
;
4162 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4163 * so a linear search is more performant than a hashmap or a binary search
4164 * (cache coherency etc)
4166 for (i
= 0; i
< shader_data
->num_gl_shaders
; ++i
)
4168 if (!memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)))
4170 if (args
->np2_fixup
) *np2fixup_info
= &shader_data
->gl_shaders
[i
].np2fixup
;
4171 return shader_data
->gl_shaders
[i
].prgId
;
4175 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
4176 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4177 if (shader_data
->num_gl_shaders
)
4179 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4180 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
4181 new_size
* sizeof(*shader_data
->gl_shaders
));
4183 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
4188 ERR("Out of memory\n");
4191 shader_data
->gl_shaders
= new_array
;
4192 shader_data
->shader_array_size
= new_size
;
4195 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4197 memset(&shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
, 0, sizeof(struct ps_np2fixup_info
));
4198 if (args
->np2_fixup
) np2fixup
= &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
4200 pixelshader_update_samplers(&shader
->reg_maps
, state
->textures
);
4202 shader_buffer_clear(buffer
);
4203 ret
= shader_glsl_generate_pshader(context
, buffer
, shader
, args
, np2fixup
);
4204 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
4205 *np2fixup_info
= np2fixup
;
4210 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
4211 const DWORD use_map
) {
4212 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
4213 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
4214 return stored
->fog_src
== new->fog_src
;
4217 static GLhandleARB
find_glsl_vshader(const struct wined3d_context
*context
,
4218 struct wined3d_shader_buffer
*buffer
, struct wined3d_shader
*shader
,
4219 const struct vs_compile_args
*args
)
4223 struct glsl_vs_compiled_shader
*new_array
;
4224 DWORD use_map
= shader
->device
->strided_streams
.use_map
;
4225 struct glsl_vshader_private
*shader_data
;
4228 if (!shader
->backend_data
)
4230 shader
->backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4231 if (!shader
->backend_data
)
4233 ERR("Failed to allocate backend data.\n");
4237 shader_data
= shader
->backend_data
;
4239 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4240 * so a linear search is more performant than a hashmap or a binary search
4241 * (cache coherency etc)
4243 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4244 if(vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
, use_map
)) {
4245 return shader_data
->gl_shaders
[i
].prgId
;
4249 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
4251 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4252 if (shader_data
->num_gl_shaders
)
4254 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4255 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
4256 new_size
* sizeof(*shader_data
->gl_shaders
));
4258 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
4263 ERR("Out of memory\n");
4266 shader_data
->gl_shaders
= new_array
;
4267 shader_data
->shader_array_size
= new_size
;
4270 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4272 shader_buffer_clear(buffer
);
4273 ret
= shader_glsl_generate_vshader(context
, buffer
, shader
, args
);
4274 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
4279 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4280 * It sets the programId on the current StateBlock (because it should be called
4281 * inside of the DrawPrimitive() part of the render loop).
4283 * If a program for the given combination does not exist, create one, and store
4284 * the program in the hash table. If it creates a program, it will link the
4285 * given objects, too.
4288 /* GL locking is done by the caller */
4289 static void set_glsl_shader_program(const struct wined3d_context
*context
,
4290 struct wined3d_device
*device
, BOOL use_ps
, BOOL use_vs
)
4292 const struct wined3d_state
*state
= &device
->stateBlock
->state
;
4293 struct wined3d_shader
*vshader
= use_vs
? state
->vertex_shader
: NULL
;
4294 struct wined3d_shader
*pshader
= use_ps
? state
->pixel_shader
: NULL
;
4295 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4296 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4297 struct glsl_shader_prog_link
*entry
= NULL
;
4298 GLhandleARB programId
= 0;
4299 GLhandleARB reorder_shader_id
= 0;
4302 struct ps_compile_args ps_compile_args
;
4303 struct vs_compile_args vs_compile_args
;
4305 if (vshader
) find_vs_compile_args(state
, vshader
, &vs_compile_args
);
4306 if (pshader
) find_ps_compile_args(state
, pshader
, &ps_compile_args
);
4308 entry
= get_glsl_program_entry(priv
, vshader
, pshader
, &vs_compile_args
, &ps_compile_args
);
4311 priv
->glsl_program
= entry
;
4315 /* If we get to this point, then no matching program exists, so we create one */
4316 programId
= GL_EXTCALL(glCreateProgramObjectARB());
4317 TRACE("Created new GLSL shader program %u\n", programId
);
4319 /* Create the entry */
4320 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
4321 entry
->programId
= programId
;
4322 entry
->vshader
= vshader
;
4323 entry
->pshader
= pshader
;
4324 entry
->vs_args
= vs_compile_args
;
4325 entry
->ps_args
= ps_compile_args
;
4326 entry
->constant_version
= 0;
4327 entry
->np2Fixup_info
= NULL
;
4328 /* Add the hash table entry */
4329 add_glsl_program_entry(priv
, entry
);
4331 /* Set the current program */
4332 priv
->glsl_program
= entry
;
4334 /* Attach GLSL vshader */
4337 GLhandleARB vshader_id
= find_glsl_vshader(context
, &priv
->shader_buffer
, vshader
, &vs_compile_args
);
4338 WORD map
= vshader
->reg_maps
.input_registers
;
4341 reorder_shader_id
= generate_param_reorder_function(&priv
->shader_buffer
, vshader
, pshader
, gl_info
);
4342 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
4343 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
4344 checkGLcall("glAttachObjectARB");
4345 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4348 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
4350 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
4351 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
4352 checkGLcall("glAttachObjectARB");
4354 /* Bind vertex attributes to a corresponding index number to match
4355 * the same index numbers as ARB_vertex_programs (makes loading
4356 * vertex attributes simpler). With this method, we can use the
4357 * exact same code to load the attributes later for both ARB and
4360 * We have to do this here because we need to know the Program ID
4361 * in order to make the bindings work, and it has to be done prior
4362 * to linking the GLSL program. */
4363 for (i
= 0; map
; map
>>= 1, ++i
)
4365 if (!(map
& 1)) continue;
4367 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%u", i
);
4368 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
4370 checkGLcall("glBindAttribLocationARB");
4372 list_add_head(&vshader
->linked_programs
, &entry
->vshader_entry
);
4375 /* Attach GLSL pshader */
4378 GLhandleARB pshader_id
= find_glsl_pshader(context
, &priv
->shader_buffer
,
4379 pshader
, &ps_compile_args
, &entry
->np2Fixup_info
);
4380 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
4381 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
4382 checkGLcall("glAttachObjectARB");
4384 list_add_head(&pshader
->linked_programs
, &entry
->pshader_entry
);
4387 /* Link the program */
4388 TRACE("Linking GLSL shader program %u\n", programId
);
4389 GL_EXTCALL(glLinkProgramARB(programId
));
4390 shader_glsl_validate_link(gl_info
, programId
);
4392 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0,
4393 sizeof(GLhandleARB
) * gl_info
->limits
.glsl_vs_float_constants
);
4394 for (i
= 0; i
< gl_info
->limits
.glsl_vs_float_constants
; ++i
)
4396 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
4397 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4399 for (i
= 0; i
< MAX_CONST_I
; ++i
)
4401 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
4402 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4404 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0,
4405 sizeof(GLhandleARB
) * gl_info
->limits
.glsl_ps_float_constants
);
4406 for (i
= 0; i
< gl_info
->limits
.glsl_ps_float_constants
; ++i
)
4408 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
4409 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4411 for (i
= 0; i
< MAX_CONST_I
; ++i
)
4413 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
4414 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4420 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
4421 sprintf(name
, "bumpenvmat%u", i
);
4422 entry
->bumpenvmat_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4423 sprintf(name
, "luminancescale%u", i
);
4424 entry
->luminancescale_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4425 sprintf(name
, "luminanceoffset%u", i
);
4426 entry
->luminanceoffset_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4429 if (ps_compile_args
.np2_fixup
) {
4430 if (entry
->np2Fixup_info
) {
4431 entry
->np2Fixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "PsamplerNP2Fixup"));
4433 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4438 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
4439 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
4440 checkGLcall("Find glsl program uniform locations");
4442 if (pshader
&& pshader
->reg_maps
.shader_version
.major
>= 3
4443 && pshader
->u
.ps
.declared_in_count
> vec4_varyings(3, gl_info
))
4445 TRACE("Shader %d needs vertex color clamping disabled\n", programId
);
4446 entry
->vertex_color_clamp
= GL_FALSE
;
4448 entry
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
4451 /* Set the shader to allow uniform loading on it */
4452 GL_EXTCALL(glUseProgramObjectARB(programId
));
4453 checkGLcall("glUseProgramObjectARB(programId)");
4455 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4456 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4457 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4458 * vertex shader with fixed function pixel processing is used we make sure that the card
4459 * supports enough samplers to allow the max number of vertex samplers with all possible
4460 * fixed function fragment processing setups. So once the program is linked these samplers
4463 if (vshader
) shader_glsl_load_vsamplers(gl_info
, device
->texUnitMap
, programId
);
4464 if (pshader
) shader_glsl_load_psamplers(gl_info
, device
->texUnitMap
, programId
);
4466 /* If the local constants do not have to be loaded with the environment constants,
4467 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4470 if (pshader
&& !pshader
->load_local_constsF
)
4471 hardcode_local_constants(pshader
, gl_info
, programId
, 'P');
4472 if (vshader
&& !vshader
->load_local_constsF
)
4473 hardcode_local_constants(vshader
, gl_info
, programId
, 'V');
4476 /* GL locking is done by the caller */
4477 static GLhandleARB
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum tex_types tex_type
, BOOL masked
)
4479 GLhandleARB program_id
;
4480 GLhandleARB vshader_id
, pshader_id
;
4481 const char *blt_pshader
;
4483 static const char *blt_vshader
=
4487 " gl_Position = gl_Vertex;\n"
4488 " gl_FrontColor = vec4(1.0);\n"
4489 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4492 static const char * const blt_pshaders_full
[tex_type_count
] =
4498 "uniform sampler2D sampler;\n"
4501 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4507 "uniform samplerCube sampler;\n"
4510 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4514 "#extension GL_ARB_texture_rectangle : enable\n"
4515 "uniform sampler2DRect sampler;\n"
4518 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4522 static const char * const blt_pshaders_masked
[tex_type_count
] =
4528 "uniform sampler2D sampler;\n"
4529 "uniform vec4 mask;\n"
4532 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4533 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4539 "uniform samplerCube sampler;\n"
4540 "uniform vec4 mask;\n"
4543 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4544 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4548 "#extension GL_ARB_texture_rectangle : enable\n"
4549 "uniform sampler2DRect sampler;\n"
4550 "uniform vec4 mask;\n"
4553 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4554 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4558 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
4561 FIXME("tex_type %#x not supported\n", tex_type
);
4565 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4566 shader_glsl_compile(gl_info
, vshader_id
, blt_vshader
);
4568 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
4569 shader_glsl_compile(gl_info
, pshader_id
, blt_pshader
);
4571 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
4572 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
4573 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
4574 GL_EXTCALL(glLinkProgramARB(program_id
));
4576 shader_glsl_validate_link(gl_info
, program_id
);
4578 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4581 GL_EXTCALL(glDeleteObjectARB(vshader_id
));
4582 GL_EXTCALL(glDeleteObjectARB(pshader_id
));
4586 /* GL locking is done by the caller */
4587 static void shader_glsl_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
)
4589 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4590 struct wined3d_device
*device
= context
->swapchain
->device
;
4591 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4592 GLhandleARB program_id
= 0;
4593 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
4595 old_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4597 if (useVS
|| usePS
) set_glsl_shader_program(context
, device
, usePS
, useVS
);
4598 else priv
->glsl_program
= NULL
;
4600 current_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4602 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
4604 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
4606 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
4607 checkGLcall("glClampColorARB");
4611 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4615 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4616 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4617 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4618 checkGLcall("glUseProgramObjectARB");
4620 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4621 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4622 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4623 if (priv
->glsl_program
&& priv
->glsl_program
->np2Fixup_info
)
4625 shader_glsl_load_np2fixup_constants(priv
, gl_info
, &device
->stateBlock
->state
);
4629 /* GL locking is done by the caller */
4630 static void shader_glsl_select_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
,
4631 enum tex_types tex_type
, const SIZE
*ds_mask_size
)
4633 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
4634 struct shader_glsl_priv
*priv
= shader_priv
;
4635 GLhandleARB
*blt_program
;
4638 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
4641 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
4642 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "sampler"));
4643 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4644 GL_EXTCALL(glUniform1iARB(loc
, 0));
4648 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4653 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "mask"));
4654 GL_EXTCALL(glUniform4fARB(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
4658 /* GL locking is done by the caller */
4659 static void shader_glsl_deselect_depth_blt(void *shader_priv
, const struct wined3d_gl_info
*gl_info
)
4661 struct shader_glsl_priv
*priv
= shader_priv
;
4662 GLhandleARB program_id
;
4664 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4665 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4667 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4668 checkGLcall("glUseProgramObjectARB");
4671 static void shader_glsl_destroy(struct wined3d_shader
*shader
)
4673 struct wined3d_device
*device
= shader
->device
;
4674 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4675 const struct wined3d_gl_info
*gl_info
;
4676 const struct list
*linked_programs
;
4677 struct wined3d_context
*context
;
4679 char pshader
= shader_is_pshader_version(shader
->reg_maps
.shader_version
.type
);
4683 struct glsl_pshader_private
*shader_data
= shader
->backend_data
;
4685 if (!shader_data
|| !shader_data
->num_gl_shaders
)
4687 HeapFree(GetProcessHeap(), 0, shader_data
);
4688 shader
->backend_data
= NULL
;
4692 context
= context_acquire(device
, NULL
);
4693 gl_info
= context
->gl_info
;
4695 if (priv
->glsl_program
&& priv
->glsl_program
->pshader
== shader
)
4698 shader_glsl_select(context
, FALSE
, FALSE
);
4704 struct glsl_vshader_private
*shader_data
= shader
->backend_data
;
4706 if (!shader_data
|| !shader_data
->num_gl_shaders
)
4708 HeapFree(GetProcessHeap(), 0, shader_data
);
4709 shader
->backend_data
= NULL
;
4713 context
= context_acquire(device
, NULL
);
4714 gl_info
= context
->gl_info
;
4716 if (priv
->glsl_program
&& priv
->glsl_program
->vshader
== shader
)
4719 shader_glsl_select(context
, FALSE
, FALSE
);
4724 linked_programs
= &shader
->linked_programs
;
4726 TRACE("Deleting linked programs\n");
4727 if (linked_programs
->next
) {
4728 struct glsl_shader_prog_link
*entry
, *entry2
;
4732 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
4733 delete_glsl_program_entry(priv
, gl_info
, entry
);
4736 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
4737 delete_glsl_program_entry(priv
, gl_info
, entry
);
4745 struct glsl_pshader_private
*shader_data
= shader
->backend_data
;
4749 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4750 TRACE("deleting pshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4751 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4752 checkGLcall("glDeleteObjectARB");
4755 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4759 struct glsl_vshader_private
*shader_data
= shader
->backend_data
;
4763 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4764 TRACE("deleting vshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4765 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4766 checkGLcall("glDeleteObjectARB");
4769 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4772 HeapFree(GetProcessHeap(), 0, shader
->backend_data
);
4773 shader
->backend_data
= NULL
;
4775 context_release(context
);
4778 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
4780 const struct glsl_program_key
*k
= key
;
4781 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
4782 const struct glsl_shader_prog_link
, program_lookup_entry
);
4785 if (k
->vshader
> prog
->vshader
) return 1;
4786 else if (k
->vshader
< prog
->vshader
) return -1;
4788 if (k
->pshader
> prog
->pshader
) return 1;
4789 else if (k
->pshader
< prog
->pshader
) return -1;
4791 if (k
->vshader
&& (cmp
= memcmp(&k
->vs_args
, &prog
->vs_args
, sizeof(prog
->vs_args
)))) return cmp
;
4792 if (k
->pshader
&& (cmp
= memcmp(&k
->ps_args
, &prog
->ps_args
, sizeof(prog
->ps_args
)))) return cmp
;
4797 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
4799 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
) + constant_count
* sizeof(*heap
->positions
);
4800 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
4804 ERR("Failed to allocate memory\n");
4808 heap
->entries
= mem
;
4809 heap
->entries
[1].version
= 0;
4810 heap
->positions
= (unsigned int *)(heap
->entries
+ constant_count
+ 1);
4816 static void constant_heap_free(struct constant_heap
*heap
)
4818 HeapFree(GetProcessHeap(), 0, heap
->entries
);
4821 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
4826 glsl_program_key_compare
,
4829 static HRESULT
shader_glsl_alloc(struct wined3d_device
*device
)
4831 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4832 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
4833 SIZE_T stack_size
= wined3d_log2i(max(gl_info
->limits
.glsl_vs_float_constants
,
4834 gl_info
->limits
.glsl_ps_float_constants
)) + 1;
4836 if (!shader_buffer_init(&priv
->shader_buffer
))
4838 ERR("Failed to initialize shader buffer.\n");
4842 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
4845 ERR("Failed to allocate memory.\n");
4849 if (!constant_heap_init(&priv
->vconst_heap
, gl_info
->limits
.glsl_vs_float_constants
))
4851 ERR("Failed to initialize vertex shader constant heap\n");
4855 if (!constant_heap_init(&priv
->pconst_heap
, gl_info
->limits
.glsl_ps_float_constants
))
4857 ERR("Failed to initialize pixel shader constant heap\n");
4861 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
4863 ERR("Failed to initialize rbtree.\n");
4867 priv
->next_constant_version
= 1;
4869 device
->shader_priv
= priv
;
4873 constant_heap_free(&priv
->pconst_heap
);
4874 constant_heap_free(&priv
->vconst_heap
);
4875 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4876 shader_buffer_free(&priv
->shader_buffer
);
4877 HeapFree(GetProcessHeap(), 0, priv
);
4878 return E_OUTOFMEMORY
;
4881 /* Context activation is done by the caller. */
4882 static void shader_glsl_free(struct wined3d_device
*device
)
4884 const struct wined3d_gl_info
*gl_info
= &device
->adapter
->gl_info
;
4885 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4889 for (i
= 0; i
< tex_type_count
; ++i
)
4891 if (priv
->depth_blt_program_full
[i
])
4893 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program_full
[i
]));
4895 if (priv
->depth_blt_program_masked
[i
])
4897 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program_masked
[i
]));
4902 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
4903 constant_heap_free(&priv
->pconst_heap
);
4904 constant_heap_free(&priv
->vconst_heap
);
4905 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4906 shader_buffer_free(&priv
->shader_buffer
);
4908 HeapFree(GetProcessHeap(), 0, device
->shader_priv
);
4909 device
->shader_priv
= NULL
;
4912 static void shader_glsl_context_destroyed(void *shader_priv
, const struct wined3d_context
*context
) {}
4914 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*caps
)
4916 /* ARB_shader_texture_lod or EXT_gpu_shader4 is required for the SM3
4917 * texldd and texldl instructions. */
4918 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] || gl_info
->supported
[EXT_GPU_SHADER4
])
4920 caps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
4921 caps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
4925 caps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
4926 caps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
4929 caps
->MaxVertexShaderConst
= gl_info
->limits
.glsl_vs_float_constants
;
4930 caps
->MaxPixelShaderConst
= gl_info
->limits
.glsl_ps_float_constants
;
4932 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4933 * Direct3D minimum requirement.
4935 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4936 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4938 * The problem is that the refrast clamps temporary results in the shader to
4939 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4940 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4941 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4942 * offer a way to query this.
4944 caps
->PixelShader1xMaxValue
= 8.0;
4946 caps
->VSClipping
= TRUE
;
4948 TRACE_(d3d_caps
)("Hardware vertex shader version %u.%u enabled (GLSL).\n",
4949 (caps
->VertexShaderVersion
>> 8) & 0xff, caps
->VertexShaderVersion
& 0xff);
4950 TRACE_(d3d_caps
)("Hardware pixel shader version %u.%u enabled (GLSL).\n",
4951 (caps
->PixelShaderVersion
>> 8) & 0xff, caps
->PixelShaderVersion
& 0xff);
4954 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
4956 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4958 TRACE("Checking support for fixup:\n");
4959 dump_color_fixup_desc(fixup
);
4962 /* We support everything except YUV conversions. */
4963 if (!is_complex_fixup(fixup
))
4969 TRACE("[FAILED]\n");
4973 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4975 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
4976 /* WINED3DSIH_ADD */ shader_glsl_arith
,
4977 /* WINED3DSIH_AND */ NULL
,
4978 /* WINED3DSIH_BEM */ shader_glsl_bem
,
4979 /* WINED3DSIH_BREAK */ shader_glsl_break
,
4980 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
4981 /* WINED3DSIH_BREAKP */ NULL
,
4982 /* WINED3DSIH_CALL */ shader_glsl_call
,
4983 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
4984 /* WINED3DSIH_CMP */ shader_glsl_cmp
,
4985 /* WINED3DSIH_CND */ shader_glsl_cnd
,
4986 /* WINED3DSIH_CRS */ shader_glsl_cross
,
4987 /* WINED3DSIH_CUT */ NULL
,
4988 /* WINED3DSIH_DCL */ NULL
,
4989 /* WINED3DSIH_DEF */ NULL
,
4990 /* WINED3DSIH_DEFB */ NULL
,
4991 /* WINED3DSIH_DEFI */ NULL
,
4992 /* WINED3DSIH_DIV */ NULL
,
4993 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
4994 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
4995 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
4996 /* WINED3DSIH_DST */ shader_glsl_dst
,
4997 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
4998 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
4999 /* WINED3DSIH_ELSE */ shader_glsl_else
,
5000 /* WINED3DSIH_EMIT */ NULL
,
5001 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
5002 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
5003 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
5004 /* WINED3DSIH_EXP */ shader_glsl_map2gl
,
5005 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
5006 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
5007 /* WINED3DSIH_FTOI */ NULL
,
5008 /* WINED3DSIH_IADD */ NULL
,
5009 /* WINED3DSIH_IEQ */ NULL
,
5010 /* WINED3DSIH_IF */ shader_glsl_if
,
5011 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
5012 /* WINED3DSIH_IGE */ NULL
,
5013 /* WINED3DSIH_IMUL */ NULL
,
5014 /* WINED3DSIH_ITOF */ NULL
,
5015 /* WINED3DSIH_LABEL */ shader_glsl_label
,
5016 /* WINED3DSIH_LD */ NULL
,
5017 /* WINED3DSIH_LIT */ shader_glsl_lit
,
5018 /* WINED3DSIH_LOG */ shader_glsl_log
,
5019 /* WINED3DSIH_LOGP */ shader_glsl_log
,
5020 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
5021 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
5022 /* WINED3DSIH_LT */ NULL
,
5023 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
5024 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
5025 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
5026 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
5027 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
5028 /* WINED3DSIH_MAD */ shader_glsl_mad
,
5029 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
5030 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
5031 /* WINED3DSIH_MOV */ shader_glsl_mov
,
5032 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
5033 /* WINED3DSIH_MOVC */ NULL
,
5034 /* WINED3DSIH_MUL */ shader_glsl_arith
,
5035 /* WINED3DSIH_NOP */ NULL
,
5036 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
5037 /* WINED3DSIH_PHASE */ NULL
,
5038 /* WINED3DSIH_POW */ shader_glsl_pow
,
5039 /* WINED3DSIH_RCP */ shader_glsl_rcp
,
5040 /* WINED3DSIH_REP */ shader_glsl_rep
,
5041 /* WINED3DSIH_RET */ shader_glsl_ret
,
5042 /* WINED3DSIH_RSQ */ shader_glsl_rsq
,
5043 /* WINED3DSIH_SAMPLE */ NULL
,
5044 /* WINED3DSIH_SAMPLE_GRAD */ NULL
,
5045 /* WINED3DSIH_SAMPLE_LOD */ NULL
,
5046 /* WINED3DSIH_SETP */ NULL
,
5047 /* WINED3DSIH_SGE */ shader_glsl_compare
,
5048 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
5049 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
5050 /* WINED3DSIH_SLT */ shader_glsl_compare
,
5051 /* WINED3DSIH_SQRT */ NULL
,
5052 /* WINED3DSIH_SUB */ shader_glsl_arith
,
5053 /* WINED3DSIH_TEX */ shader_glsl_tex
,
5054 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
5055 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
5056 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
5057 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
5058 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
5059 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
5060 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
5061 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
5062 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
5063 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
5064 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
5065 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
5066 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
5067 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
5068 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
5069 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
5070 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
5071 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
5072 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
5073 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
5074 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
5075 /* WINED3DSIH_UTOF */ NULL
,
5078 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
5079 SHADER_HANDLER hw_fct
;
5081 /* Select handler */
5082 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
5084 /* Unhandled opcode */
5087 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
5092 shader_glsl_add_instruction_modifiers(ins
);
5095 const struct wined3d_shader_backend_ops glsl_shader_backend
=
5097 shader_glsl_handle_instruction
,
5099 shader_glsl_select_depth_blt
,
5100 shader_glsl_deselect_depth_blt
,
5101 shader_glsl_update_float_vertex_constants
,
5102 shader_glsl_update_float_pixel_constants
,
5103 shader_glsl_load_constants
,
5104 shader_glsl_load_np2fixup_constants
,
5105 shader_glsl_destroy
,
5108 shader_glsl_context_destroyed
,
5109 shader_glsl_get_caps
,
5110 shader_glsl_color_fixup_supported
,