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 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 GLINFO_LOCATION (*gl_info)
44 #define WINED3D_GLSL_SAMPLE_PROJECTED 0x1
45 #define WINED3D_GLSL_SAMPLE_RECT 0x2
46 #define WINED3D_GLSL_SAMPLE_LOD 0x4
47 #define WINED3D_GLSL_SAMPLE_GRAD 0x8
62 } glsl_sample_function_t
;
66 HEAP_NODE_TRAVERSE_LEFT
,
67 HEAP_NODE_TRAVERSE_RIGHT
,
79 struct constant_entry
*entries
;
80 unsigned int *positions
;
84 /* GLSL shader private data */
85 struct shader_glsl_priv
{
86 struct wine_rb_tree program_lookup
;
87 struct glsl_shader_prog_link
*glsl_program
;
88 struct constant_heap vconst_heap
;
89 struct constant_heap pconst_heap
;
91 GLhandleARB depth_blt_program
[tex_type_count
];
92 UINT next_constant_version
;
95 /* Struct to maintain data about a linked GLSL program */
96 struct glsl_shader_prog_link
{
97 struct wine_rb_entry program_lookup_entry
;
98 struct list vshader_entry
;
99 struct list pshader_entry
;
100 GLhandleARB programId
;
101 GLint
*vuniformF_locations
;
102 GLint
*puniformF_locations
;
103 GLint vuniformI_locations
[MAX_CONST_I
];
104 GLint puniformI_locations
[MAX_CONST_I
];
105 GLint posFixup_location
;
106 GLint np2Fixup_location
[MAX_FRAGMENT_SAMPLERS
];
107 GLint bumpenvmat_location
[MAX_TEXTURES
];
108 GLint luminancescale_location
[MAX_TEXTURES
];
109 GLint luminanceoffset_location
[MAX_TEXTURES
];
110 GLint ycorrection_location
;
111 GLenum vertex_color_clamp
;
112 IWineD3DVertexShader
*vshader
;
113 IWineD3DPixelShader
*pshader
;
114 struct vs_compile_args vs_args
;
115 struct ps_compile_args ps_args
;
116 UINT constant_version
;
120 IWineD3DVertexShader
*vshader
;
121 IWineD3DPixelShader
*pshader
;
122 struct ps_compile_args ps_args
;
123 struct vs_compile_args vs_args
;
124 } glsl_program_key_t
;
126 struct shader_glsl_ctx_priv
{
127 const struct vs_compile_args
*cur_vs_args
;
128 const struct ps_compile_args
*cur_ps_args
;
131 struct glsl_ps_compiled_shader
133 struct ps_compile_args args
;
137 struct glsl_pshader_private
139 struct glsl_ps_compiled_shader
*gl_shaders
;
140 UINT num_gl_shaders
, shader_array_size
;
143 struct glsl_vs_compiled_shader
145 struct vs_compile_args args
;
149 struct glsl_vshader_private
151 struct glsl_vs_compiled_shader
*gl_shaders
;
152 UINT num_gl_shaders
, shader_array_size
;
155 /* Extract a line from the info log.
156 * Note that this modifies the source string. */
157 static char *get_info_log_line(char **ptr
)
162 if (!(q
= strstr(p
, "\n")))
164 if (!*p
) return NULL
;
174 /** Prints the GLSL info log which will contain error messages if they exist */
175 /* GL locking is done by the caller */
176 static void print_glsl_info_log(const WineD3D_GL_Info
*gl_info
, GLhandleARB obj
)
178 int infologLength
= 0;
183 static const char * const spam
[] =
185 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
186 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
187 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
188 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
189 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
190 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
191 "Fragment shader was successfully compiled to run on hardware.\n"
192 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported",
193 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
194 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
195 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
198 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
)) return;
200 GL_EXTCALL(glGetObjectParameterivARB(obj
,
201 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
204 /* A size of 1 is just a null-terminated string, so the log should be bigger than
205 * that if there are errors. */
206 if (infologLength
> 1)
210 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
211 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
213 infoLog
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, infologLength
);
214 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
217 for(i
= 0; i
< sizeof(spam
) / sizeof(spam
[0]); i
++) {
218 if(strcmp(infoLog
, spam
[i
]) == 0) {
227 TRACE("Spam received from GLSL shader #%u:\n", obj
);
228 while ((line
= get_info_log_line(&ptr
))) TRACE(" %s\n", line
);
232 FIXME("Error received from GLSL shader #%u:\n", obj
);
233 while ((line
= get_info_log_line(&ptr
))) FIXME(" %s\n", line
);
235 HeapFree(GetProcessHeap(), 0, infoLog
);
240 * Loads (pixel shader) samplers
242 /* GL locking is done by the caller */
243 static void shader_glsl_load_psamplers(const WineD3D_GL_Info
*gl_info
, DWORD
*tex_unit_map
, GLhandleARB programId
)
247 char sampler_name
[20];
249 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
250 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
251 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
252 if (name_loc
!= -1) {
253 DWORD mapped_unit
= tex_unit_map
[i
];
254 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< GL_LIMITS(fragment_samplers
))
256 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
257 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
258 checkGLcall("glUniform1iARB");
260 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
266 /* GL locking is done by the caller */
267 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info
*gl_info
, DWORD
*tex_unit_map
, GLhandleARB programId
)
270 char sampler_name
[20];
273 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
274 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
275 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
276 if (name_loc
!= -1) {
277 DWORD mapped_unit
= tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
];
278 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< GL_LIMITS(combined_samplers
))
280 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
281 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
282 checkGLcall("glUniform1iARB");
284 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
290 /* GL locking is done by the caller */
291 static inline void walk_constant_heap(const WineD3D_GL_Info
*gl_info
, const float *constants
,
292 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
295 unsigned int heap_idx
= 1;
298 if (heap
->entries
[heap_idx
].version
<= version
) return;
300 idx
= heap
->entries
[heap_idx
].idx
;
301 if (constant_locations
[idx
] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
302 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
304 while (stack_idx
>= 0)
306 /* Note that we fall through to the next case statement. */
307 switch(stack
[stack_idx
])
309 case HEAP_NODE_TRAVERSE_LEFT
:
311 unsigned int left_idx
= heap_idx
<< 1;
312 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
315 idx
= heap
->entries
[heap_idx
].idx
;
316 if (constant_locations
[idx
] != -1)
317 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
319 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
320 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
325 case HEAP_NODE_TRAVERSE_RIGHT
:
327 unsigned int right_idx
= (heap_idx
<< 1) + 1;
328 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
330 heap_idx
= right_idx
;
331 idx
= heap
->entries
[heap_idx
].idx
;
332 if (constant_locations
[idx
] != -1)
333 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
335 stack
[stack_idx
++] = HEAP_NODE_POP
;
336 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
349 checkGLcall("walk_constant_heap()");
352 /* GL locking is done by the caller */
353 static inline void apply_clamped_constant(const WineD3D_GL_Info
*gl_info
, GLint location
, const GLfloat
*data
)
355 GLfloat clamped_constant
[4];
357 if (location
== -1) return;
359 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0 ? 1.0 : data
[0];
360 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0 ? 1.0 : data
[1];
361 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0 ? 1.0 : data
[2];
362 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0 ? 1.0 : data
[3];
364 GL_EXTCALL(glUniform4fvARB(location
, 1, clamped_constant
));
367 /* GL locking is done by the caller */
368 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info
*gl_info
, const float *constants
,
369 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
372 unsigned int heap_idx
= 1;
375 if (heap
->entries
[heap_idx
].version
<= version
) return;
377 idx
= heap
->entries
[heap_idx
].idx
;
378 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
379 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
381 while (stack_idx
>= 0)
383 /* Note that we fall through to the next case statement. */
384 switch(stack
[stack_idx
])
386 case HEAP_NODE_TRAVERSE_LEFT
:
388 unsigned int left_idx
= heap_idx
<< 1;
389 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
392 idx
= heap
->entries
[heap_idx
].idx
;
393 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
395 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
396 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
401 case HEAP_NODE_TRAVERSE_RIGHT
:
403 unsigned int right_idx
= (heap_idx
<< 1) + 1;
404 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
406 heap_idx
= right_idx
;
407 idx
= heap
->entries
[heap_idx
].idx
;
408 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
410 stack
[stack_idx
++] = HEAP_NODE_POP
;
411 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
424 checkGLcall("walk_constant_heap_clamped()");
427 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
428 /* GL locking is done by the caller */
429 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
430 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
431 unsigned char *stack
, UINT version
)
433 const local_constant
*lconst
;
435 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
436 if (This
->baseShader
.reg_maps
.shader_version
.major
== 1
437 && shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
))
438 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
440 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
442 if (!This
->baseShader
.load_local_constsF
)
444 TRACE("No need to load local float constants for this shader\n");
448 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
449 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
)
451 GLint location
= constant_locations
[lconst
->idx
];
452 /* We found this uniform name in the program - go ahead and send the data */
453 if (location
!= -1) GL_EXTCALL(glUniform4fvARB(location
, 1, (const GLfloat
*)lconst
->value
));
455 checkGLcall("glUniform4fvARB()");
458 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
459 /* GL locking is done by the caller */
460 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
461 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
466 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
468 if (!(constants_set
& 1)) continue;
470 TRACE_(d3d_constants
)("Loading constants %u: %i, %i, %i, %i\n",
471 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
473 /* We found this uniform name in the program - go ahead and send the data */
474 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
475 checkGLcall("glUniform4ivARB");
478 /* Load immediate constants */
479 ptr
= list_head(&This
->baseShader
.constantsI
);
481 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
482 unsigned int idx
= lconst
->idx
;
483 const GLint
*values
= (const GLint
*)lconst
->value
;
485 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
486 values
[0], values
[1], values
[2], values
[3]);
488 /* We found this uniform name in the program - go ahead and send the data */
489 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
490 checkGLcall("glUniform4ivARB");
491 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
495 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
496 /* GL locking is done by the caller */
497 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
498 GLhandleARB programId
, const BOOL
*constants
, WORD constants_set
)
503 char is_pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
504 const char* prefix
= is_pshader
? "PB":"VB";
507 /* TODO: Benchmark and see if it would be beneficial to store the
508 * locations of the constants to avoid looking up each time */
509 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
511 if (!(constants_set
& 1)) continue;
513 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
515 /* TODO: Benchmark and see if it would be beneficial to store the
516 * locations of the constants to avoid looking up each time */
517 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
518 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
521 /* We found this uniform name in the program - go ahead and send the data */
522 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
523 checkGLcall("glUniform1ivARB");
527 /* Load immediate constants */
528 ptr
= list_head(&This
->baseShader
.constantsB
);
530 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
531 unsigned int idx
= lconst
->idx
;
532 const GLint
*values
= (const GLint
*)lconst
->value
;
534 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
536 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
537 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
539 /* We found this uniform name in the program - go ahead and send the data */
540 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
541 checkGLcall("glUniform1ivARB");
543 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
547 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
549 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
553 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
555 /* GL locking is done by the caller (state handler) */
556 static void shader_glsl_load_np2fixup_constants(
557 IWineD3DDevice
* device
,
559 char useVertexShader
) {
561 const IWineD3DDeviceImpl
* deviceImpl
= (const IWineD3DDeviceImpl
*) device
;
562 const struct glsl_shader_prog_link
* prog
= ((struct shader_glsl_priv
*)(deviceImpl
->shader_priv
))->glsl_program
;
565 /* No GLSL program set - nothing to do. */
569 if (!usePixelShader
) {
570 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
574 if (prog
->ps_args
.np2_fixup
) {
576 UINT fixup
= prog
->ps_args
.np2_fixup
;
577 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
578 const IWineD3DStateBlockImpl
* stateBlock
= (const IWineD3DStateBlockImpl
*) deviceImpl
->stateBlock
;
580 for (i
= 0; fixup
; fixup
>>= 1, ++i
) {
581 if (-1 != prog
->np2Fixup_location
[i
]) {
582 const IWineD3DBaseTextureImpl
* const tex
= (const IWineD3DBaseTextureImpl
*) stateBlock
->textures
[i
];
584 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
587 const float tex_dim
[2] = {tex
->baseTexture
.pow2Matrix
[0], tex
->baseTexture
.pow2Matrix
[5]};
588 GL_EXTCALL(glUniform2fvARB(prog
->np2Fixup_location
[i
], 1, tex_dim
));
596 * Loads the app-supplied constants into the currently set GLSL program.
598 /* GL locking is done by the caller (state handler) */
599 static void shader_glsl_load_constants(
600 IWineD3DDevice
* device
,
602 char useVertexShader
) {
604 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
605 struct shader_glsl_priv
*priv
= deviceImpl
->shader_priv
;
606 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
607 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
609 GLhandleARB programId
;
610 struct glsl_shader_prog_link
*prog
= priv
->glsl_program
;
611 UINT constant_version
;
615 /* No GLSL program set - nothing to do. */
618 programId
= prog
->programId
;
619 constant_version
= prog
->constant_version
;
621 if (useVertexShader
) {
622 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
624 /* Load DirectX 9 float constants/uniforms for vertex shader */
625 shader_glsl_load_constantsF(vshader
, gl_info
, stateBlock
->vertexShaderConstantF
,
626 prog
->vuniformF_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
628 /* Load DirectX 9 integer constants/uniforms for vertex shader */
629 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vuniformI_locations
, stateBlock
->vertexShaderConstantI
,
630 stateBlock
->changed
.vertexShaderConstantsI
& vshader
->baseShader
.reg_maps
.integer_constants
);
632 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
633 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, stateBlock
->vertexShaderConstantB
,
634 stateBlock
->changed
.vertexShaderConstantsB
& vshader
->baseShader
.reg_maps
.boolean_constants
);
636 /* Upload the position fixup params */
637 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &deviceImpl
->posFixup
[0]));
638 checkGLcall("glUniform4fvARB");
641 if (usePixelShader
) {
643 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
645 /* Load DirectX 9 float constants/uniforms for pixel shader */
646 shader_glsl_load_constantsF(pshader
, gl_info
, stateBlock
->pixelShaderConstantF
,
647 prog
->puniformF_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
649 /* Load DirectX 9 integer constants/uniforms for pixel shader */
650 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->puniformI_locations
, stateBlock
->pixelShaderConstantI
,
651 stateBlock
->changed
.pixelShaderConstantsI
& pshader
->baseShader
.reg_maps
.integer_constants
);
653 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
654 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, stateBlock
->pixelShaderConstantB
,
655 stateBlock
->changed
.pixelShaderConstantsB
& pshader
->baseShader
.reg_maps
.boolean_constants
);
657 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
658 * It can't be 0 for a valid texbem instruction.
660 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
663 if(prog
->bumpenvmat_location
[i
] == -1) continue;
665 data
= (const float *)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVMAT00
];
666 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
[i
], 1, 0, data
));
667 checkGLcall("glUniformMatrix2fvARB");
669 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
670 * is set too, so we can check that in the needsbumpmat check
672 if(prog
->luminancescale_location
[i
] != -1) {
673 const GLfloat
*scale
= (const GLfloat
*)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVLSCALE
];
674 const GLfloat
*offset
= (const GLfloat
*)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVLOFFSET
];
676 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
[i
], 1, scale
));
677 checkGLcall("glUniform1fvARB");
678 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
[i
], 1, offset
));
679 checkGLcall("glUniform1fvARB");
683 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
684 float correction_params
[4];
685 if(deviceImpl
->render_offscreen
) {
686 correction_params
[0] = 0.0;
687 correction_params
[1] = 1.0;
689 /* position is window relative, not viewport relative */
690 correction_params
[0] = ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
691 correction_params
[1] = -1.0;
693 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
697 if (priv
->next_constant_version
== UINT_MAX
)
699 TRACE("Max constant version reached, resetting to 0.\n");
700 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
701 priv
->next_constant_version
= 1;
705 prog
->constant_version
= priv
->next_constant_version
++;
709 static inline void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
,
710 unsigned int heap_idx
, DWORD new_version
)
712 struct constant_entry
*entries
= heap
->entries
;
713 unsigned int *positions
= heap
->positions
;
714 unsigned int parent_idx
;
718 parent_idx
= heap_idx
>> 1;
720 if (new_version
<= entries
[parent_idx
].version
) break;
722 entries
[heap_idx
] = entries
[parent_idx
];
723 positions
[entries
[parent_idx
].idx
] = heap_idx
;
724 heap_idx
= parent_idx
;
727 entries
[heap_idx
].version
= new_version
;
728 entries
[heap_idx
].idx
= idx
;
729 positions
[idx
] = heap_idx
;
732 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
734 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
735 struct shader_glsl_priv
*priv
= This
->shader_priv
;
736 struct constant_heap
*heap
= &priv
->vconst_heap
;
739 for (i
= start
; i
< count
+ start
; ++i
)
741 if (!This
->stateBlock
->changed
.vertexShaderConstantsF
[i
])
742 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
744 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
748 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
750 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
751 struct shader_glsl_priv
*priv
= This
->shader_priv
;
752 struct constant_heap
*heap
= &priv
->pconst_heap
;
755 for (i
= start
; i
< count
+ start
; ++i
)
757 if (!This
->stateBlock
->changed
.pixelShaderConstantsF
[i
])
758 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
760 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
764 static int vec4_varyings(DWORD shader_major
, const WineD3D_GL_Info
*gl_info
)
766 int ret
= GL_LIMITS(glsl_varyings
) / 4;
767 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
768 if(shader_major
> 3) return ret
;
770 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
771 if(gl_info
->glsl_clip_varying
) ret
-= 1;
775 /** Generate the variable & register declarations for the GLSL output target */
776 static void shader_generate_glsl_declarations(IWineD3DBaseShader
*iface
, const shader_reg_maps
*reg_maps
,
777 SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
,
778 const struct ps_compile_args
*ps_args
)
780 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
781 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
782 unsigned int i
, extra_constants_needed
= 0;
783 const local_constant
*lconst
;
785 /* There are some minor differences between pixel and vertex shaders */
786 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
787 char prefix
= pshader
? 'P' : 'V';
789 /* Prototype the subroutines */
790 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
791 if (reg_maps
->labels
[i
])
792 shader_addline(buffer
, "void subroutine%u();\n", i
);
795 /* Declare the constants (aka uniforms) */
796 if (This
->baseShader
.limits
.constant_float
> 0) {
797 unsigned max_constantsF
;
798 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
799 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
800 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
801 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
802 * a dx9 card, as long as it doesn't also use all the other constants.
804 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
805 * declare only the amount that we're assured to have.
807 * Thus we run into problems in these two cases:
808 * 1) The shader really uses more uniforms than supported
809 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
812 /* No indirect addressing here */
813 max_constantsF
= GL_LIMITS(pshader_constantsF
);
815 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
816 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix).
817 * Subtract another uniform for immediate values, which have to be loaded via uniform by the driver as well.
818 * The shader code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex shader code, so one vec4 should be enough
819 * (Unfortunately the Nvidia driver doesn't store 128 and -128 in one float).
821 * Writing gl_ClipPos requires one uniform for each clipplane as well.
823 max_constantsF
= GL_LIMITS(vshader_constantsF
) - 3 - GL_LIMITS(clipplanes
);
824 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.integer_constants
);
825 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
826 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
827 * for now take this into account when calculating the number of available constants
829 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.boolean_constants
);
830 /* Set by driver quirks in directx.c */
831 max_constantsF
-= GLINFO_LOCATION
.reserved_glsl_constants
;
833 max_constantsF
= GL_LIMITS(vshader_constantsF
);
836 max_constantsF
= min(This
->baseShader
.limits
.constant_float
, max_constantsF
);
837 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
840 /* Always declare the full set of constants, the compiler can remove the unused ones because d3d doesn't(yet)
841 * support indirect int and bool constant addressing. This avoids problems if the app uses e.g. i0 and i9.
843 if (This
->baseShader
.limits
.constant_int
> 0 && This
->baseShader
.reg_maps
.integer_constants
)
844 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
846 if (This
->baseShader
.limits
.constant_bool
> 0 && This
->baseShader
.reg_maps
.boolean_constants
)
847 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
850 shader_addline(buffer
, "uniform vec4 posFixup;\n");
851 /* Predeclaration; This function is added at link time based on the pixel shader.
852 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
853 * that. We know the input to the reorder function at vertex shader compile time, so
854 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
855 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
856 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
857 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
858 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
861 if (reg_maps
->shader_version
.major
>= 3)
863 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
865 shader_addline(buffer
, "void order_ps_input();\n");
868 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
869 if(!reg_maps
->bumpmat
[i
]) {
873 shader_addline(buffer
, "uniform mat2 bumpenvmat%d;\n", i
);
875 if(reg_maps
->luminanceparams
) {
876 shader_addline(buffer
, "uniform float luminancescale%d;\n", i
);
877 shader_addline(buffer
, "uniform float luminanceoffset%d;\n", i
);
878 extra_constants_needed
++;
881 extra_constants_needed
++;
884 if(ps_args
->srgb_correction
) {
885 shader_addline(buffer
, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
886 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
887 shader_addline(buffer
, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
888 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
890 if(reg_maps
->vpos
|| reg_maps
->usesdsy
) {
891 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
892 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
893 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
894 extra_constants_needed
++;
896 /* This happens because we do not have proper tracking of the constant registers that are
897 * actually used, only the max limit of the shader version
899 FIXME("Cannot find a free uniform for vpos correction params\n");
900 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
901 device
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) device
->render_targets
[0])->currentDesc
.Height
,
902 device
->render_offscreen
? 1.0 : -1.0);
904 shader_addline(buffer
, "vec4 vpos;\n");
908 /* Declare texture samplers */
909 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
910 if (reg_maps
->sampler_type
[i
])
912 switch (reg_maps
->sampler_type
[i
])
915 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
918 if(device
->stateBlock
->textures
[i
] &&
919 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
]) == GL_TEXTURE_RECTANGLE_ARB
) {
920 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
922 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
925 if (pshader
&& ps_args
->np2_fixup
& (1 << i
))
927 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
928 * while D3D has them in the (normalized) [0,1]x[0,1] range.
929 * samplerNP2Fixup stores texture dimensions and is updated through
930 * shader_glsl_load_np2fixup_constants when the sampler changes. */
931 shader_addline(buffer
, "uniform vec2 %csamplerNP2Fixup%u;\n", prefix
, i
);
934 case WINED3DSTT_CUBE
:
935 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
937 case WINED3DSTT_VOLUME
:
938 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
941 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
942 FIXME("Unrecognized sampler type: %#x\n", reg_maps
->sampler_type
[i
]);
948 /* Declare address variables */
949 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
950 if (reg_maps
->address
[i
])
951 shader_addline(buffer
, "ivec4 A%d;\n", i
);
954 /* Declare texture coordinate temporaries and initialize them */
955 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
956 if (reg_maps
->texcoord
[i
])
957 shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
960 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
961 * helper function shader that is linked in at link time
963 if (pshader
&& reg_maps
->shader_version
.major
>= 3)
965 if (use_vs(device
->stateBlock
))
967 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
969 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
970 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
971 * pixel shader that reads the fixed function color into the packed input registers.
973 shader_addline(buffer
, "vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
977 /* Declare output register temporaries */
978 if(This
->baseShader
.limits
.packed_output
) {
979 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
982 /* Declare temporary variables */
983 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
984 if (reg_maps
->temporary
[i
])
985 shader_addline(buffer
, "vec4 R%u;\n", i
);
988 /* Declare attributes */
989 if (reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
991 WORD map
= reg_maps
->input_registers
;
993 for (i
= 0; map
; map
>>= 1, ++i
)
995 if (!(map
& 1)) continue;
997 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
1001 /* Declare loop registers aLx */
1002 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
1003 shader_addline(buffer
, "int aL%u;\n", i
);
1004 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1007 /* Temporary variables for matrix operations */
1008 shader_addline(buffer
, "vec4 tmp0;\n");
1009 shader_addline(buffer
, "vec4 tmp1;\n");
1011 /* Local constants use a different name so they can be loaded once at shader link time
1012 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1013 * float -> string conversion can cause precision loss.
1015 if(!This
->baseShader
.load_local_constsF
) {
1016 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
1017 shader_addline(buffer
, "uniform vec4 %cLC%u;\n", prefix
, lconst
->idx
);
1021 /* Start the main program */
1022 shader_addline(buffer
, "void main() {\n");
1023 if(pshader
&& reg_maps
->vpos
) {
1024 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1025 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1026 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1027 * precision troubles when we just substract 0.5.
1029 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1031 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1033 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1034 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1035 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1036 * correctly on drivers that returns integer values.
1038 shader_addline(buffer
, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1042 /*****************************************************************************
1043 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1045 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1046 ****************************************************************************/
1049 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1050 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
);
1052 /** Used for opcode modifiers - They multiply the result by the specified amount */
1053 static const char * const shift_glsl_tab
[] = {
1055 "2.0 * ", /* 1 (x2) */
1056 "4.0 * ", /* 2 (x4) */
1057 "8.0 * ", /* 3 (x8) */
1058 "16.0 * ", /* 4 (x16) */
1059 "32.0 * ", /* 5 (x32) */
1066 "0.0625 * ", /* 12 (d16) */
1067 "0.125 * ", /* 13 (d8) */
1068 "0.25 * ", /* 14 (d4) */
1069 "0.5 * " /* 15 (d2) */
1072 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1073 static void shader_glsl_gen_modifier(DWORD src_modifier
, const char *in_reg
, const char *in_regswizzle
, char *out_str
)
1077 switch (src_modifier
)
1079 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
1080 case WINED3DSPSM_DW
:
1081 case WINED3DSPSM_NONE
:
1082 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1084 case WINED3DSPSM_NEG
:
1085 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
1087 case WINED3DSPSM_NOT
:
1088 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
1090 case WINED3DSPSM_BIAS
:
1091 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1093 case WINED3DSPSM_BIASNEG
:
1094 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1096 case WINED3DSPSM_SIGN
:
1097 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1099 case WINED3DSPSM_SIGNNEG
:
1100 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1102 case WINED3DSPSM_COMP
:
1103 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
1105 case WINED3DSPSM_X2
:
1106 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
1108 case WINED3DSPSM_X2NEG
:
1109 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
1111 case WINED3DSPSM_ABS
:
1112 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
1114 case WINED3DSPSM_ABSNEG
:
1115 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
1118 FIXME("Unhandled modifier %u\n", src_modifier
);
1119 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1123 /** Writes the GLSL variable name that corresponds to the register that the
1124 * DX opcode parameter is trying to access */
1125 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
1126 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
1128 /* oPos, oFog and oPts in D3D */
1129 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1131 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1132 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1133 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
1134 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
1140 case WINED3DSPR_TEMP
:
1141 sprintf(register_name
, "R%u", reg
->idx
);
1144 case WINED3DSPR_INPUT
:
1145 /* vertex shaders */
1148 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1149 if (priv
->cur_vs_args
->swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1150 sprintf(register_name
, "attrib%u", reg
->idx
);
1154 /* pixel shaders >= 3.0 */
1155 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3)
1157 DWORD idx
= ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
->idx
];
1158 DWORD in_count
= vec4_varyings(This
->baseShader
.reg_maps
.shader_version
.major
, gl_info
);
1162 glsl_src_param_t rel_param
;
1164 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1166 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1167 * operation there */
1170 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1172 sprintf(register_name
,
1173 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1174 rel_param
.param_str
, idx
, in_count
- 1, rel_param
.param_str
, idx
, in_count
,
1175 rel_param
.param_str
, idx
);
1179 sprintf(register_name
, "IN[%s + %u]", rel_param
.param_str
, idx
);
1184 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1186 sprintf(register_name
, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1187 rel_param
.param_str
, in_count
- 1, rel_param
.param_str
, in_count
,
1188 rel_param
.param_str
);
1192 sprintf(register_name
, "IN[%s]", rel_param
.param_str
);
1198 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
1199 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
1200 else sprintf(register_name
, "IN[%u]", idx
);
1205 if (reg
->idx
== 0) strcpy(register_name
, "gl_Color");
1206 else strcpy(register_name
, "gl_SecondaryColor");
1211 case WINED3DSPR_CONST
:
1213 const char prefix
= pshader
? 'P' : 'V';
1215 /* Relative addressing */
1218 glsl_src_param_t rel_param
;
1219 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1220 if (reg
->idx
) sprintf(register_name
, "%cC[%s + %u]", prefix
, rel_param
.param_str
, reg
->idx
);
1221 else sprintf(register_name
, "%cC[%s]", prefix
, rel_param
.param_str
);
1225 if (shader_constant_is_local(This
, reg
->idx
))
1226 sprintf(register_name
, "%cLC%u", prefix
, reg
->idx
);
1228 sprintf(register_name
, "%cC[%u]", prefix
, reg
->idx
);
1233 case WINED3DSPR_CONSTINT
:
1234 if (pshader
) sprintf(register_name
, "PI[%u]", reg
->idx
);
1235 else sprintf(register_name
, "VI[%u]", reg
->idx
);
1238 case WINED3DSPR_CONSTBOOL
:
1239 if (pshader
) sprintf(register_name
, "PB[%u]", reg
->idx
);
1240 else sprintf(register_name
, "VB[%u]", reg
->idx
);
1243 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1244 if (pshader
) sprintf(register_name
, "T%u", reg
->idx
);
1245 else sprintf(register_name
, "A%u", reg
->idx
);
1248 case WINED3DSPR_LOOP
:
1249 sprintf(register_name
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
1252 case WINED3DSPR_SAMPLER
:
1253 if (pshader
) sprintf(register_name
, "Psampler%u", reg
->idx
);
1254 else sprintf(register_name
, "Vsampler%u", reg
->idx
);
1257 case WINED3DSPR_COLOROUT
:
1258 if (reg
->idx
>= GL_LIMITS(buffers
))
1259 WARN("Write to render target %u, only %d supported\n", reg
->idx
, GL_LIMITS(buffers
));
1261 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) sprintf(register_name
, "gl_FragData[%u]", reg
->idx
);
1262 /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1263 else sprintf(register_name
, "gl_FragColor");
1266 case WINED3DSPR_RASTOUT
:
1267 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
]);
1270 case WINED3DSPR_DEPTHOUT
:
1271 sprintf(register_name
, "gl_FragDepth");
1274 case WINED3DSPR_ATTROUT
:
1275 if (reg
->idx
== 0) sprintf(register_name
, "gl_FrontColor");
1276 else sprintf(register_name
, "gl_FrontSecondaryColor");
1279 case WINED3DSPR_TEXCRDOUT
:
1280 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1281 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3) sprintf(register_name
, "OUT[%u]", reg
->idx
);
1282 else sprintf(register_name
, "gl_TexCoord[%u]", reg
->idx
);
1285 case WINED3DSPR_MISCTYPE
:
1289 sprintf(register_name
, "vpos");
1291 else if (reg
->idx
== 1)
1293 /* Note that gl_FrontFacing is a bool, while vFace is
1294 * a float for which the sign determines front/back */
1295 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
1299 FIXME("Unhandled misctype register %d\n", reg
->idx
);
1300 sprintf(register_name
, "unrecognized_register");
1304 case WINED3DSPR_IMMCONST
:
1305 switch (reg
->immconst_type
)
1307 case WINED3D_IMMCONST_FLOAT
:
1308 sprintf(register_name
, "%.8e", *(float *)reg
->immconst_data
);
1311 case WINED3D_IMMCONST_FLOAT4
:
1312 sprintf(register_name
, "vec4(%.8e, %.8e, %.8e, %.8e)",
1313 *(float *)®
->immconst_data
[0], *(float *)®
->immconst_data
[1],
1314 *(float *)®
->immconst_data
[2], *(float *)®
->immconst_data
[3]);
1318 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
1319 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
1324 FIXME("Unhandled register name Type(%d)\n", reg
->type
);
1325 sprintf(register_name
, "unrecognized_register");
1330 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
1333 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
1334 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
1335 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
1336 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
1340 /* Get the GLSL write mask for the destination register */
1341 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
1343 DWORD mask
= param
->write_mask
;
1345 if (shader_is_scalar(¶m
->reg
))
1347 mask
= WINED3DSP_WRITEMASK_0
;
1352 shader_glsl_write_mask_to_str(mask
, write_mask
);
1358 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
1359 unsigned int size
= 0;
1361 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
1362 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
1363 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
1364 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
1369 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
1371 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1372 * but addressed as "rgba". To fix this we need to swap the register's x
1373 * and z components. */
1374 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1377 /* swizzle bits fields: wwzzyyxx */
1378 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
1379 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1380 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1381 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1385 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
1386 BOOL fixup
, DWORD mask
, char *swizzle_str
)
1388 if (shader_is_scalar(¶m
->reg
))
1389 *swizzle_str
= '\0';
1391 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
1394 /* From a given parameter token, generate the corresponding GLSL string.
1395 * Also, return the actual register name and swizzle in case the
1396 * caller needs this information as well. */
1397 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1398 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
)
1400 BOOL is_color
= FALSE
;
1401 char swizzle_str
[6];
1403 glsl_src
->reg_name
[0] = '\0';
1404 glsl_src
->param_str
[0] = '\0';
1405 swizzle_str
[0] = '\0';
1407 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
1408 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
1409 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
1412 /* From a given parameter token, generate the corresponding GLSL string.
1413 * Also, return the actual register name and swizzle in case the
1414 * caller needs this information as well. */
1415 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
1416 const struct wined3d_shader_dst_param
*wined3d_dst
, glsl_dst_param_t
*glsl_dst
)
1418 BOOL is_color
= FALSE
;
1420 glsl_dst
->mask_str
[0] = '\0';
1421 glsl_dst
->reg_name
[0] = '\0';
1423 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
1424 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
1427 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1428 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
,
1429 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
)
1431 glsl_dst_param_t glsl_dst
;
1434 mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
);
1435 if (mask
) shader_addline(buffer
, "%s%s = %s(", glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
1440 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1441 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, const struct wined3d_shader_instruction
*ins
)
1443 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0]);
1446 /** Process GLSL instruction modifiers */
1447 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
1449 glsl_dst_param_t dst_param
;
1452 if (!ins
->dst_count
) return;
1454 modifiers
= ins
->dst
[0].modifiers
;
1455 if (!modifiers
) return;
1457 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
1459 if (modifiers
& WINED3DSPDM_SATURATE
)
1461 /* _SAT means to clamp the value of the register to between 0 and 1 */
1462 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1463 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1466 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
1468 FIXME("_centroid modifier not handled\n");
1471 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
1473 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1477 static inline const char *shader_get_comp_op(DWORD op
)
1480 case COMPARISON_GT
: return ">";
1481 case COMPARISON_EQ
: return "==";
1482 case COMPARISON_GE
: return ">=";
1483 case COMPARISON_LT
: return "<";
1484 case COMPARISON_NE
: return "!=";
1485 case COMPARISON_LE
: return "<=";
1487 FIXME("Unrecognized comparison value: %u\n", op
);
1492 static void shader_glsl_get_sample_function(DWORD sampler_type
, DWORD flags
, glsl_sample_function_t
*sample_function
)
1494 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
1495 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_RECT
;
1496 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
1497 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
1499 /* Note that there's no such thing as a projected cube texture. */
1500 switch(sampler_type
) {
1503 sample_function
->name
= projected
? "texture1DProjLod" : "texture1DLod";
1505 sample_function
->name
= projected
? "texture1DProjGradARB" : "texture1DGradARB";
1507 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1509 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1514 sample_function
->name
= projected
? "texture2DRectProjLod" : "texture2DRectLod";
1516 /* What good are texrect grad functions? I don't know, but GL_EXT_gpu_shader4 defines them.
1517 * There is no GL_ARB_shader_texture_lod spec yet, so I don't know if they're defined there
1519 sample_function
->name
= projected
? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1521 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1525 sample_function
->name
= projected
? "texture2DProjLod" : "texture2DLod";
1527 sample_function
->name
= projected
? "texture2DProjGradARB" : "texture2DGradARB";
1529 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1532 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1534 case WINED3DSTT_CUBE
:
1536 sample_function
->name
= "textureCubeLod";
1538 sample_function
->name
= "textureCubeGradARB";
1540 sample_function
->name
= "textureCube";
1542 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1544 case WINED3DSTT_VOLUME
:
1546 sample_function
->name
= projected
? "texture3DProjLod" : "texture3DLod";
1548 sample_function
->name
= projected
? "texture3DProjGradARB" : "texture3DGradARB";
1550 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1552 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1555 sample_function
->name
= "";
1556 sample_function
->coord_mask
= 0;
1557 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1562 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
1563 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
1565 switch(channel_source
)
1567 case CHANNEL_SOURCE_ZERO
:
1568 strcat(arguments
, "0.0");
1571 case CHANNEL_SOURCE_ONE
:
1572 strcat(arguments
, "1.0");
1575 case CHANNEL_SOURCE_X
:
1576 strcat(arguments
, reg_name
);
1577 strcat(arguments
, ".x");
1580 case CHANNEL_SOURCE_Y
:
1581 strcat(arguments
, reg_name
);
1582 strcat(arguments
, ".y");
1585 case CHANNEL_SOURCE_Z
:
1586 strcat(arguments
, reg_name
);
1587 strcat(arguments
, ".z");
1590 case CHANNEL_SOURCE_W
:
1591 strcat(arguments
, reg_name
);
1592 strcat(arguments
, ".w");
1596 FIXME("Unhandled channel source %#x\n", channel_source
);
1597 strcat(arguments
, "undefined");
1601 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
1604 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
1606 struct wined3d_shader_dst_param dst
;
1607 unsigned int mask_size
, remaining
;
1608 glsl_dst_param_t dst_param
;
1609 char arguments
[256];
1613 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1614 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1615 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1616 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1617 mask
&= ins
->dst
[0].write_mask
;
1619 if (!mask
) return; /* Nothing to do */
1621 if (is_yuv_fixup(fixup
))
1623 enum yuv_fixup yuv_fixup
= get_yuv_fixup(fixup
);
1624 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup
);
1628 mask_size
= shader_glsl_get_write_mask_size(mask
);
1631 dst
.write_mask
= mask
;
1632 shader_glsl_add_dst_param(ins
, &dst
, &dst_param
);
1634 arguments
[0] = '\0';
1635 remaining
= mask_size
;
1636 if (mask
& WINED3DSP_WRITEMASK_0
)
1638 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
1639 if (--remaining
) strcat(arguments
, ", ");
1641 if (mask
& WINED3DSP_WRITEMASK_1
)
1643 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
1644 if (--remaining
) strcat(arguments
, ", ");
1646 if (mask
& WINED3DSP_WRITEMASK_2
)
1648 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
1649 if (--remaining
) strcat(arguments
, ", ");
1651 if (mask
& WINED3DSP_WRITEMASK_3
)
1653 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
1654 if (--remaining
) strcat(arguments
, ", ");
1659 shader_addline(ins
->ctx
->buffer
, "%s%s = vec%u(%s);\n",
1660 dst_param
.reg_name
, dst_param
.mask_str
, mask_size
, arguments
);
1664 shader_addline(ins
->ctx
->buffer
, "%s%s = %s;\n", dst_param
.reg_name
, dst_param
.mask_str
, arguments
);
1668 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
1669 DWORD sampler
, const glsl_sample_function_t
*sample_function
, DWORD swizzle
,
1670 const char *dx
, const char *dy
,
1671 const char *bias
, const char *coord_reg_fmt
, ...)
1673 const char *sampler_base
;
1674 char dst_swizzle
[6];
1675 struct color_fixup_desc fixup
;
1676 BOOL np2_fixup
= FALSE
;
1679 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
1681 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
1683 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1684 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
1685 sampler_base
= "Psampler";
1687 if(priv
->cur_ps_args
->np2_fixup
& (1 << sampler
)) {
1689 FIXME("Biased sampling from NP2 textures is unsupported\n");
1695 sampler_base
= "Vsampler";
1696 fixup
= COLOR_FIXUP_IDENTITY
; /* FIXME: Vshader color fixup */
1699 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1701 shader_addline(ins
->ctx
->buffer
, "%s(%s%u, ", sample_function
->name
, sampler_base
, sampler
);
1703 va_start(args
, coord_reg_fmt
);
1704 shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
1708 shader_addline(ins
->ctx
->buffer
, ", %s)%s);\n", bias
, dst_swizzle
);
1711 shader_addline(ins
->ctx
->buffer
, " * PsamplerNP2Fixup%u)%s);\n", sampler
, dst_swizzle
);
1712 } else if(dx
&& dy
) {
1713 shader_addline(ins
->ctx
->buffer
, ", %s, %s)%s);\n", dx
, dy
, dst_swizzle
);
1715 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
1719 if(!is_identity_fixup(fixup
)) {
1720 shader_glsl_color_correction(ins
, fixup
);
1724 /*****************************************************************************
1726 * Begin processing individual instruction opcodes
1728 ****************************************************************************/
1730 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1731 static void shader_glsl_arith(const struct wined3d_shader_instruction
*ins
)
1733 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1734 glsl_src_param_t src0_param
;
1735 glsl_src_param_t src1_param
;
1739 /* Determine the GLSL operator to use based on the opcode */
1740 switch (ins
->handler_idx
)
1742 case WINED3DSIH_MUL
: op
= '*'; break;
1743 case WINED3DSIH_ADD
: op
= '+'; break;
1744 case WINED3DSIH_SUB
: op
= '-'; break;
1747 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
1751 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1752 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
1753 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
1754 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1757 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1758 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
1760 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1761 glsl_src_param_t src0_param
;
1764 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1765 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
1767 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1768 * shader versions WINED3DSIO_MOVA is used for this. */
1769 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
1770 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
1771 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
1773 /* This is a simple floor() */
1774 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1775 if (mask_size
> 1) {
1776 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
1778 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
1781 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
1783 /* We need to *round* to the nearest int here. */
1784 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1785 if (mask_size
> 1) {
1786 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
1788 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1791 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1795 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1796 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
1798 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1799 glsl_src_param_t src0_param
;
1800 glsl_src_param_t src1_param
;
1801 DWORD dst_write_mask
, src_write_mask
;
1802 unsigned int dst_size
= 0;
1804 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1805 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1807 /* dp3 works on vec3, dp4 on vec4 */
1808 if (ins
->handler_idx
== WINED3DSIH_DP4
)
1810 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1812 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1815 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
1816 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
1819 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1821 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1825 /* Note that this instruction has some restrictions. The destination write mask
1826 * can't contain the w component, and the source swizzles have to be .xyzw */
1827 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
1829 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1830 glsl_src_param_t src0_param
;
1831 glsl_src_param_t src1_param
;
1834 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
1835 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1836 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
1837 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
1838 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1841 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1842 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1843 * GLSL uses the value as-is. */
1844 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
1846 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1847 glsl_src_param_t src0_param
;
1848 glsl_src_param_t src1_param
;
1849 DWORD dst_write_mask
;
1850 unsigned int dst_size
;
1852 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1853 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1855 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1856 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1859 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1861 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1865 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1866 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1867 * GLSL uses the value as-is. */
1868 static void shader_glsl_log(const struct wined3d_shader_instruction
*ins
)
1870 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1871 glsl_src_param_t src0_param
;
1872 DWORD dst_write_mask
;
1873 unsigned int dst_size
;
1875 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1876 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1878 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1881 shader_addline(buffer
, "vec%d(log2(abs(%s))));\n", dst_size
, src0_param
.param_str
);
1883 shader_addline(buffer
, "log2(abs(%s)));\n", src0_param
.param_str
);
1887 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1888 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
1890 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1891 glsl_src_param_t src_param
;
1892 const char *instruction
;
1896 /* Determine the GLSL function to use based on the opcode */
1897 /* TODO: Possibly make this a table for faster lookups */
1898 switch (ins
->handler_idx
)
1900 case WINED3DSIH_MIN
: instruction
= "min"; break;
1901 case WINED3DSIH_MAX
: instruction
= "max"; break;
1902 case WINED3DSIH_ABS
: instruction
= "abs"; break;
1903 case WINED3DSIH_FRC
: instruction
= "fract"; break;
1904 case WINED3DSIH_NRM
: instruction
= "normalize"; break;
1905 case WINED3DSIH_EXP
: instruction
= "exp2"; break;
1906 case WINED3DSIH_SGN
: instruction
= "sign"; break;
1907 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
1908 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
1909 default: instruction
= "";
1910 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
1914 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1916 shader_addline(buffer
, "%s(", instruction
);
1920 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
1921 shader_addline(buffer
, "%s", src_param
.param_str
);
1922 for (i
= 1; i
< ins
->src_count
; ++i
)
1924 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
1925 shader_addline(buffer
, ", %s", src_param
.param_str
);
1929 shader_addline(buffer
, "));\n");
1932 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1933 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1934 * dst.x = 2^(floor(src))
1935 * dst.y = src - floor(src)
1936 * dst.z = 2^src (partial precision is allowed, but optional)
1938 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1939 * dst = 2^src; (partial precision is allowed, but optional)
1941 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
1943 glsl_src_param_t src_param
;
1945 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1947 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
1951 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1952 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1953 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1954 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
1956 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1957 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
1958 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
1961 unsigned int mask_size
;
1963 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1964 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1966 if (mask_size
> 1) {
1967 shader_addline(ins
->ctx
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1969 shader_addline(ins
->ctx
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1974 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1975 static void shader_glsl_rcp(const struct wined3d_shader_instruction
*ins
)
1977 glsl_src_param_t src_param
;
1979 unsigned int mask_size
;
1981 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1982 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1983 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1985 if (mask_size
> 1) {
1986 shader_addline(ins
->ctx
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1988 shader_addline(ins
->ctx
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1992 static void shader_glsl_rsq(const struct wined3d_shader_instruction
*ins
)
1994 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1995 glsl_src_param_t src_param
;
1997 unsigned int mask_size
;
1999 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2000 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2002 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
2004 if (mask_size
> 1) {
2005 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
2007 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
2011 /** Process signed comparison opcodes in GLSL. */
2012 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
2014 glsl_src_param_t src0_param
;
2015 glsl_src_param_t src1_param
;
2017 unsigned int mask_size
;
2019 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2020 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2021 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2022 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2024 if (mask_size
> 1) {
2025 const char *compare
;
2027 switch(ins
->handler_idx
)
2029 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
2030 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
2031 default: compare
= "";
2032 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2035 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
2036 src0_param
.param_str
, src1_param
.param_str
);
2038 switch(ins
->handler_idx
)
2040 case WINED3DSIH_SLT
:
2041 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2042 * to return 0.0 but step returns 1.0 because step is not < x
2043 * An alternative is a bvec compare padded with an unused second component.
2044 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2045 * issue. Playing with not() is not possible either because not() does not accept
2048 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
2049 src0_param
.param_str
, src1_param
.param_str
);
2051 case WINED3DSIH_SGE
:
2052 /* Here we can use the step() function and safe a conditional */
2053 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
2056 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2062 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2063 static void shader_glsl_cmp(const struct wined3d_shader_instruction
*ins
)
2065 glsl_src_param_t src0_param
;
2066 glsl_src_param_t src1_param
;
2067 glsl_src_param_t src2_param
;
2068 DWORD write_mask
, cmp_channel
= 0;
2071 BOOL temp_destination
= FALSE
;
2073 if (shader_is_scalar(&ins
->src
[0].reg
))
2075 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2077 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2078 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2079 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2081 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2082 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2084 DWORD dst_mask
= ins
->dst
[0].write_mask
;
2085 struct wined3d_shader_dst_param dst
= ins
->dst
[0];
2087 /* Cycle through all source0 channels */
2088 for (i
=0; i
<4; i
++) {
2090 /* Find the destination channels which use the current source0 channel */
2091 for (j
=0; j
<4; j
++) {
2092 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2094 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2095 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2098 dst
.write_mask
= dst_mask
& write_mask
;
2100 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2101 * The first lines may overwrite source parameters of the following lines.
2102 * Deal with that by using a temporary destination register if needed
2104 if ((ins
->src
[0].reg
.idx
== ins
->dst
[0].reg
.idx
2105 && ins
->src
[0].reg
.type
== ins
->dst
[0].reg
.type
)
2106 || (ins
->src
[1].reg
.idx
== ins
->dst
[0].reg
.idx
2107 && ins
->src
[1].reg
.type
== ins
->dst
[0].reg
.type
)
2108 || (ins
->src
[2].reg
.idx
== ins
->dst
[0].reg
.idx
2109 && ins
->src
[2].reg
.type
== ins
->dst
[0].reg
.type
))
2111 write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
);
2112 if (!write_mask
) continue;
2113 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
2114 temp_destination
= TRUE
;
2116 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2117 if (!write_mask
) continue;
2120 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2121 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2122 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2124 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2125 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2128 if(temp_destination
) {
2129 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
2130 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2131 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
2137 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2138 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2139 * the compare is done per component of src0. */
2140 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
2142 struct wined3d_shader_dst_param dst
;
2143 glsl_src_param_t src0_param
;
2144 glsl_src_param_t src1_param
;
2145 glsl_src_param_t src2_param
;
2146 DWORD write_mask
, cmp_channel
= 0;
2149 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2150 ins
->ctx
->reg_maps
->shader_version
.minor
);
2152 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
2154 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2155 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2156 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2157 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2159 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2162 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
2164 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2165 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2169 /* Cycle through all source0 channels */
2170 dst_mask
= ins
->dst
[0].write_mask
;
2172 for (i
=0; i
<4; i
++) {
2174 /* Find the destination channels which use the current source0 channel */
2175 for (j
=0; j
<4; j
++) {
2176 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2178 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2179 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2183 dst
.write_mask
= dst_mask
& write_mask
;
2184 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2185 if (!write_mask
) continue;
2187 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2188 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2189 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2191 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2192 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2196 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2197 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
2199 glsl_src_param_t src0_param
;
2200 glsl_src_param_t src1_param
;
2201 glsl_src_param_t src2_param
;
2204 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2205 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2206 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2207 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2208 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
2209 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2212 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2213 Vertex shaders to GLSL codes */
2214 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
2217 int nComponents
= 0;
2218 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2219 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2220 struct wined3d_shader_instruction tmp_ins
;
2222 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2224 /* Set constants for the temporary argument */
2225 tmp_ins
.ctx
= ins
->ctx
;
2226 tmp_ins
.dst_count
= 1;
2227 tmp_ins
.dst
= &tmp_dst
;
2228 tmp_ins
.src_count
= 2;
2229 tmp_ins
.src
= tmp_src
;
2231 switch(ins
->handler_idx
)
2233 case WINED3DSIH_M4x4
:
2235 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2237 case WINED3DSIH_M4x3
:
2239 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2241 case WINED3DSIH_M3x4
:
2243 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2245 case WINED3DSIH_M3x3
:
2247 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2249 case WINED3DSIH_M3x2
:
2251 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2257 tmp_dst
= ins
->dst
[0];
2258 tmp_src
[0] = ins
->src
[0];
2259 tmp_src
[1] = ins
->src
[1];
2260 for (i
= 0; i
< nComponents
; ++i
)
2262 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2263 shader_glsl_dot(&tmp_ins
);
2264 ++tmp_src
[1].reg
.idx
;
2269 The LRP instruction performs a component-wise linear interpolation
2270 between the second and third operands using the first operand as the
2271 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2272 This is equivalent to mix(src2, src1, src0);
2274 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
2276 glsl_src_param_t src0_param
;
2277 glsl_src_param_t src1_param
;
2278 glsl_src_param_t src2_param
;
2281 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2283 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2284 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2285 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2287 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
2288 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
2291 /** Process the WINED3DSIO_LIT instruction in GLSL:
2292 * dst.x = dst.w = 1.0
2293 * dst.y = (src0.x > 0) ? src0.x
2294 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2295 * where src.w is clamped at +- 128
2297 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
2299 glsl_src_param_t src0_param
;
2300 glsl_src_param_t src1_param
;
2301 glsl_src_param_t src3_param
;
2304 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2305 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2307 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2308 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
2309 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
2311 /* The sdk specifies the instruction like this
2313 * if(src.x > 0.0) dst.y = src.x
2315 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2319 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2320 * dst.x = 1.0 ... No further explanation needed
2321 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2322 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2323 * dst.w = 1.0. ... Nothing fancy.
2325 * So we still have one conditional in there. So do this:
2326 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2328 * 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),
2329 * 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.
2330 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2332 shader_addline(ins
->ctx
->buffer
,
2333 "vec4(1.0, max(%s, 0.0), pow(max(0.0, %s) * step(0.0, %s), clamp(%s, -128.0, 128.0)), 1.0)%s);\n",
2334 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
2337 /** Process the WINED3DSIO_DST instruction in GLSL:
2339 * dst.y = src0.x * src0.y
2343 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
2345 glsl_src_param_t src0y_param
;
2346 glsl_src_param_t src0z_param
;
2347 glsl_src_param_t src1y_param
;
2348 glsl_src_param_t src1w_param
;
2351 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2352 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2354 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
2355 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
2356 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
2357 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
2359 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2360 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
2363 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2364 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2365 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2367 * dst.x = cos(src0.?)
2368 * dst.y = sin(src0.?)
2372 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
2374 glsl_src_param_t src0_param
;
2377 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2378 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2380 switch (write_mask
) {
2381 case WINED3DSP_WRITEMASK_0
:
2382 shader_addline(ins
->ctx
->buffer
, "cos(%s));\n", src0_param
.param_str
);
2385 case WINED3DSP_WRITEMASK_1
:
2386 shader_addline(ins
->ctx
->buffer
, "sin(%s));\n", src0_param
.param_str
);
2389 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
2390 shader_addline(ins
->ctx
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
2394 ERR("Write mask should be .x, .y or .xy\n");
2399 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2400 * Start a for() loop where src1.y is the initial value of aL,
2401 * increment aL by src1.z for a total of src1.x iterations.
2402 * Need to use a temporary variable for this operation.
2404 /* FIXME: I don't think nested loops will work correctly this way. */
2405 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
2407 glsl_src_param_t src1_param
;
2408 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2409 const DWORD
*control_values
= NULL
;
2410 const local_constant
*constant
;
2412 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
2414 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2415 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2416 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2419 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
2421 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
2422 if (constant
->idx
== ins
->src
[1].reg
.idx
)
2424 control_values
= constant
->value
;
2430 if(control_values
) {
2431 if(control_values
[2] > 0) {
2432 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2433 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2434 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2435 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2436 } else if(control_values
[2] == 0) {
2437 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2438 shader
->baseShader
.cur_loop_depth
, control_values
[1], shader
->baseShader
.cur_loop_depth
,
2439 shader
->baseShader
.cur_loop_depth
, control_values
[0],
2440 shader
->baseShader
.cur_loop_depth
);
2442 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2443 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2444 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2445 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2448 shader_addline(ins
->ctx
->buffer
,
2449 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2450 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
2451 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
2452 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
2455 shader
->baseShader
.cur_loop_depth
++;
2456 shader
->baseShader
.cur_loop_regno
++;
2459 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
2461 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2463 shader_addline(ins
->ctx
->buffer
, "}\n");
2465 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
2467 shader
->baseShader
.cur_loop_depth
--;
2468 shader
->baseShader
.cur_loop_regno
--;
2471 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
2473 shader
->baseShader
.cur_loop_depth
--;
2477 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
2479 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2480 glsl_src_param_t src0_param
;
2481 const DWORD
*control_values
= NULL
;
2482 const local_constant
*constant
;
2484 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2485 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
2487 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
)
2489 if (constant
->idx
== ins
->src
[0].reg
.idx
)
2491 control_values
= constant
->value
;
2497 if(control_values
) {
2498 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2499 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2500 control_values
[0], shader
->baseShader
.cur_loop_depth
);
2502 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2503 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2504 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2505 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
2507 shader
->baseShader
.cur_loop_depth
++;
2510 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
2512 glsl_src_param_t src0_param
;
2514 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2515 shader_addline(ins
->ctx
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2518 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
2520 glsl_src_param_t src0_param
;
2521 glsl_src_param_t src1_param
;
2523 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2524 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2526 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
2527 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2530 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
2532 shader_addline(ins
->ctx
->buffer
, "} else {\n");
2535 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
2537 shader_addline(ins
->ctx
->buffer
, "break;\n");
2540 /* FIXME: According to MSDN the compare is done per component. */
2541 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
2543 glsl_src_param_t src0_param
;
2544 glsl_src_param_t src1_param
;
2546 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2547 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2549 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
2550 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2553 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
2555 shader_addline(ins
->ctx
->buffer
, "}\n");
2556 shader_addline(ins
->ctx
->buffer
, "void subroutine%u () {\n", ins
->src
[0].reg
.idx
);
2559 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
2561 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
);
2564 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
2566 glsl_src_param_t src1_param
;
2568 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2569 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, ins
->src
[0].reg
.idx
);
2572 /*********************************************
2573 * Pixel Shader Specific Code begins here
2574 ********************************************/
2575 static void pshader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
2577 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2578 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2579 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2580 ins
->ctx
->reg_maps
->shader_version
.minor
);
2581 glsl_sample_function_t sample_function
;
2582 DWORD sample_flags
= 0;
2583 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
;
2585 DWORD mask
= 0, swizzle
;
2587 /* 1.0-1.4: Use destination register as sampler source.
2588 * 2.0+: Use provided sampler source. */
2589 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) sampler_idx
= ins
->dst
[0].reg
.idx
;
2590 else sampler_idx
= ins
->src
[1].reg
.idx
;
2591 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2593 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
2595 DWORD flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2597 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2598 if (flags
& WINED3DTTFF_PROJECTED
&& sampler_type
!= WINED3DSTT_CUBE
) {
2599 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2600 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2601 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2602 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
2603 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
2604 case WINED3DTTFF_COUNT4
:
2605 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
2609 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
2611 DWORD src_mod
= ins
->src
[0].modifiers
;
2613 if (src_mod
== WINED3DSPSM_DZ
) {
2614 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2615 mask
= WINED3DSP_WRITEMASK_2
;
2616 } else if (src_mod
== WINED3DSPSM_DW
) {
2617 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2618 mask
= WINED3DSP_WRITEMASK_3
;
2621 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
2623 /* ps 2.0 texldp instruction always divides by the fourth component. */
2624 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2625 mask
= WINED3DSP_WRITEMASK_3
;
2629 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2630 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2631 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2634 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2635 mask
|= sample_function
.coord_mask
;
2637 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
2638 else swizzle
= ins
->src
[1].swizzle
;
2640 /* 1.0-1.3: Use destination register as coordinate source.
2641 1.4+: Use provided coordinate source register. */
2642 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
2645 shader_glsl_write_mask_to_str(mask
, coord_mask
);
2646 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
2647 "T%u%s", sampler_idx
, coord_mask
);
2649 glsl_src_param_t coord_param
;
2650 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
2651 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
2653 glsl_src_param_t bias
;
2654 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
2655 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
2656 "%s", coord_param
.param_str
);
2658 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
2659 "%s", coord_param
.param_str
);
2664 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
2666 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2667 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2668 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
2669 glsl_sample_function_t sample_function
;
2670 glsl_src_param_t coord_param
, dx_param
, dy_param
;
2671 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
2674 DWORD swizzle
= ins
->src
[1].swizzle
;
2676 if(!GL_SUPPORT(ARB_SHADER_TEXTURE_LOD
)) {
2677 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
2678 return pshader_glsl_tex(ins
);
2681 sampler_idx
= ins
->src
[1].reg
.idx
;
2682 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2683 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2684 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2685 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2688 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2689 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
2690 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
2691 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
2693 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
2694 "%s", coord_param
.param_str
);
2697 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
2699 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2700 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2701 glsl_sample_function_t sample_function
;
2702 glsl_src_param_t coord_param
, lod_param
;
2703 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
2706 DWORD swizzle
= ins
->src
[1].swizzle
;
2708 sampler_idx
= ins
->src
[1].reg
.idx
;
2709 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2710 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2711 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2712 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2714 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2715 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
2717 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
2719 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
2721 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2722 * However, they seem to work just fine in fragment shaders as well. */
2723 WARN("Using %s in fragment shader.\n", sample_function
.name
);
2725 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
2726 "%s", coord_param
.param_str
);
2729 static void pshader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
2731 /* FIXME: Make this work for more than just 2D textures */
2732 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2733 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2735 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
2739 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2740 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
2741 ins
->dst
[0].reg
.idx
, dst_mask
);
2743 DWORD reg
= ins
->src
[0].reg
.idx
;
2744 DWORD src_mod
= ins
->src
[0].modifiers
;
2745 char dst_swizzle
[6];
2747 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
2749 if (src_mod
== WINED3DSPSM_DZ
) {
2750 glsl_src_param_t div_param
;
2751 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2752 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
2754 if (mask_size
> 1) {
2755 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2757 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2759 } else if (src_mod
== WINED3DSPSM_DW
) {
2760 glsl_src_param_t div_param
;
2761 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2762 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
2764 if (mask_size
> 1) {
2765 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2767 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2770 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
2775 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2776 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2777 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2778 static void pshader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
2780 glsl_src_param_t src0_param
;
2781 glsl_sample_function_t sample_function
;
2782 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
2783 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2784 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2787 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2789 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2790 * scalar, and projected sampling would require 4.
2792 * It is a dependent read - not valid with conditional NP2 textures
2794 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2795 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
2800 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2801 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
2805 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2806 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
2810 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2811 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
2815 FIXME("Unexpected mask size %u\n", mask_size
);
2820 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2821 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2822 static void pshader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
2824 glsl_src_param_t src0_param
;
2825 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
2826 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2828 unsigned int mask_size
;
2830 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2831 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2832 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2834 if (mask_size
> 1) {
2835 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2837 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2841 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2842 * Calculate the depth as dst.x / dst.y */
2843 static void pshader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
2845 glsl_dst_param_t dst_param
;
2847 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2849 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2850 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2851 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2852 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2855 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
2856 dst_param
.reg_name
, dst_param
.reg_name
);
2859 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2860 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2861 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2862 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2864 static void pshader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
2866 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2867 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
2868 glsl_src_param_t src0_param
;
2870 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2872 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2873 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2876 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2877 * Calculate the 1st of a 2-row matrix multiplication. */
2878 static void pshader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
2880 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2881 DWORD reg
= ins
->dst
[0].reg
.idx
;
2882 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2883 glsl_src_param_t src0_param
;
2885 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2886 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2889 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2890 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2891 static void pshader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
2893 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2894 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2895 DWORD reg
= ins
->dst
[0].reg
.idx
;
2896 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2897 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2898 glsl_src_param_t src0_param
;
2900 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2901 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2902 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2905 static void pshader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
2907 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2908 DWORD reg
= ins
->dst
[0].reg
.idx
;
2909 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2910 glsl_src_param_t src0_param
;
2911 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2912 glsl_sample_function_t sample_function
;
2914 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2915 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2917 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2919 /* Sample the texture using the calculated coordinates */
2920 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
2923 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2924 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2925 static void pshader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
2927 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2928 glsl_src_param_t src0_param
;
2929 DWORD reg
= ins
->dst
[0].reg
.idx
;
2930 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2931 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2932 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2933 glsl_sample_function_t sample_function
;
2935 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2936 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2938 /* Dependent read, not valid with conditional NP2 */
2939 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2941 /* Sample the texture using the calculated coordinates */
2942 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
2944 current_state
->current_row
= 0;
2947 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2948 * Perform the 3rd row of a 3x3 matrix multiply */
2949 static void pshader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
2951 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2952 glsl_src_param_t src0_param
;
2954 DWORD reg
= ins
->dst
[0].reg
.idx
;
2955 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2956 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2958 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2960 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2961 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2962 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2964 current_state
->current_row
= 0;
2967 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2968 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2969 static void pshader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
2971 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2972 DWORD reg
= ins
->dst
[0].reg
.idx
;
2973 glsl_src_param_t src0_param
;
2974 glsl_src_param_t src1_param
;
2975 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2976 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2977 WINED3DSAMPLER_TEXTURE_TYPE stype
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2978 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2979 glsl_sample_function_t sample_function
;
2981 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2982 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
2984 /* Perform the last matrix multiply operation */
2985 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2986 /* Reflection calculation */
2987 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2989 /* Dependent read, not valid with conditional NP2 */
2990 shader_glsl_get_sample_function(stype
, 0, &sample_function
);
2992 /* Sample the texture */
2993 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
2995 current_state
->current_row
= 0;
2998 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2999 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3000 static void pshader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
3002 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
3003 DWORD reg
= ins
->dst
[0].reg
.idx
;
3004 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
3005 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
3006 glsl_src_param_t src0_param
;
3007 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3008 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
3009 glsl_sample_function_t sample_function
;
3011 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3013 /* Perform the last matrix multiply operation */
3014 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
3016 /* Construct the eye-ray vector from w coordinates */
3017 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3018 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
3019 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3021 /* Dependent read, not valid with conditional NP2 */
3022 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3024 /* Sample the texture using the calculated coordinates */
3025 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3027 current_state
->current_row
= 0;
3030 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3031 * Apply a fake bump map transform.
3032 * texbem is pshader <= 1.3 only, this saves a few version checks
3034 static void pshader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
3036 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
3037 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
3038 glsl_sample_function_t sample_function
;
3039 glsl_src_param_t coord_param
;
3040 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
;
3046 sampler_idx
= ins
->dst
[0].reg
.idx
;
3047 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
3049 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3050 /* Dependent read, not valid with conditional NP2 */
3051 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3052 mask
= sample_function
.coord_mask
;
3054 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3056 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3057 * so we can't let the GL handle this.
3059 if (flags
& WINED3DTTFF_PROJECTED
) {
3061 char coord_div_mask
[3];
3062 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
3063 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3064 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
3065 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
3066 case WINED3DTTFF_COUNT4
:
3067 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
3069 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
3070 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
3073 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
3075 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3076 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
3077 coord_param
.param_str
, coord_mask
);
3079 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
3081 glsl_src_param_t luminance_param
;
3082 glsl_dst_param_t dst_param
;
3084 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
3085 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3087 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3088 dst_param
.reg_name
, dst_param
.mask_str
,
3089 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
3093 static void pshader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
3095 glsl_src_param_t src0_param
, src1_param
;
3096 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3098 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3099 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3101 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3102 shader_addline(ins
->ctx
->buffer
, "%s + bumpenvmat%d * %s);\n",
3103 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
3106 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3107 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3108 static void pshader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
3110 glsl_src_param_t src0_param
;
3111 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3112 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3113 glsl_sample_function_t sample_function
;
3115 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3117 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3118 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3119 "%s.wx", src0_param
.reg_name
);
3122 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3123 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3124 static void pshader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
3126 glsl_src_param_t src0_param
;
3127 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3128 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3129 glsl_sample_function_t sample_function
;
3131 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3133 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3134 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3135 "%s.yz", src0_param
.reg_name
);
3138 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3139 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3140 static void pshader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
3142 glsl_src_param_t src0_param
;
3143 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3144 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3145 glsl_sample_function_t sample_function
;
3147 /* Dependent read, not valid with conditional NP2 */
3148 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3149 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
3151 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3152 "%s", src0_param
.param_str
);
3155 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3156 * If any of the first 3 components are < 0, discard this pixel */
3157 static void pshader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
3159 glsl_dst_param_t dst_param
;
3161 /* The argument is a destination parameter, and no writemasks are allowed */
3162 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3163 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
3165 /* 2.0 shaders compare all 4 components in texkill */
3166 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
3168 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3169 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3170 * 4 components are defined, only the first 3 are used
3172 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
3176 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3177 * dst = dot2(src0, src1) + src2 */
3178 static void pshader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
3180 glsl_src_param_t src0_param
;
3181 glsl_src_param_t src1_param
;
3182 glsl_src_param_t src2_param
;
3184 unsigned int mask_size
;
3186 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3187 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3189 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3190 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3191 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
3193 if (mask_size
> 1) {
3194 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
3195 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3197 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
3198 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3202 static void pshader_glsl_input_pack(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
,
3203 const struct wined3d_shader_signature_element
*input_signature
, const struct shader_reg_maps
*reg_maps
,
3204 enum vertexprocessing_mode vertexprocessing
)
3207 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
3208 WORD map
= reg_maps
->input_registers
;
3210 for (i
= 0; map
; map
>>= 1, ++i
)
3212 const char *semantic_name
;
3217 if (!(map
& 1)) continue;
3219 semantic_name
= input_signature
[i
].semantic_name
;
3220 semantic_idx
= input_signature
[i
].semantic_idx
;
3221 shader_glsl_write_mask_to_str(input_signature
[i
].mask
, reg_mask
);
3223 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3225 if (semantic_idx
< 8 && vertexprocessing
== pretransformed
)
3226 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3227 This
->input_reg_map
[i
], reg_mask
, semantic_idx
, reg_mask
);
3229 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3230 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3232 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3234 if (semantic_idx
== 0)
3235 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
3236 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3237 else if (semantic_idx
== 1)
3238 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3239 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3241 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3242 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3246 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3247 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3252 /*********************************************
3253 * Vertex Shader Specific Code begins here
3254 ********************************************/
3256 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
) {
3257 glsl_program_key_t key
;
3259 key
.vshader
= entry
->vshader
;
3260 key
.pshader
= entry
->pshader
;
3261 key
.vs_args
= entry
->vs_args
;
3262 key
.ps_args
= entry
->ps_args
;
3264 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
3266 ERR("Failed to insert program entry.\n");
3270 static struct glsl_shader_prog_link
*get_glsl_program_entry(struct shader_glsl_priv
*priv
,
3271 IWineD3DVertexShader
*vshader
, IWineD3DPixelShader
*pshader
, struct vs_compile_args
*vs_args
,
3272 struct ps_compile_args
*ps_args
) {
3273 struct wine_rb_entry
*entry
;
3274 glsl_program_key_t key
;
3276 key
.vshader
= vshader
;
3277 key
.pshader
= pshader
;
3278 key
.vs_args
= *vs_args
;
3279 key
.ps_args
= *ps_args
;
3281 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
3282 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
3285 /* GL locking is done by the caller */
3286 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const WineD3D_GL_Info
*gl_info
,
3287 struct glsl_shader_prog_link
*entry
)
3289 glsl_program_key_t key
;
3291 key
.vshader
= entry
->vshader
;
3292 key
.pshader
= entry
->pshader
;
3293 key
.vs_args
= entry
->vs_args
;
3294 key
.ps_args
= entry
->ps_args
;
3295 wine_rb_remove(&priv
->program_lookup
, &key
);
3297 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
3298 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
3299 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
3300 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
3301 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
3302 HeapFree(GetProcessHeap(), 0, entry
);
3305 static void handle_ps3_input(SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
, const DWORD
*map
,
3306 const struct wined3d_shader_signature_element
*input_signature
, const struct shader_reg_maps
*reg_maps_in
,
3307 const struct wined3d_shader_signature_element
*output_signature
, const struct shader_reg_maps
*reg_maps_out
)
3310 const char *semantic_name_in
, *semantic_name_out
;
3311 UINT semantic_idx_in
, semantic_idx_out
;
3314 DWORD in_count
= vec4_varyings(3, gl_info
);
3315 char reg_mask
[6], reg_mask_out
[6];
3316 char destination
[50];
3317 WORD input_map
, output_map
;
3319 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
3321 if (!output_signature
)
3323 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3324 shader_addline(buffer
, "vec4 front_color = gl_FrontColor;\n");
3325 shader_addline(buffer
, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3328 input_map
= reg_maps_in
->input_registers
;
3329 for (i
= 0; input_map
; input_map
>>= 1, ++i
)
3331 if (!(input_map
& 1)) continue;
3334 if (in_idx
>= (in_count
+ 2)) {
3335 FIXME("More input varyings declared than supported, expect issues\n");
3338 else if (map
[i
] == ~0U)
3340 /* Declared, but not read register */
3344 if (in_idx
== in_count
) {
3345 sprintf(destination
, "gl_FrontColor");
3346 } else if (in_idx
== in_count
+ 1) {
3347 sprintf(destination
, "gl_FrontSecondaryColor");
3349 sprintf(destination
, "IN[%u]", in_idx
);
3352 semantic_name_in
= input_signature
[i
].semantic_name
;
3353 semantic_idx_in
= input_signature
[i
].semantic_idx
;
3354 set
[map
[i
]] = input_signature
[i
].mask
;
3355 shader_glsl_write_mask_to_str(input_signature
[i
].mask
, reg_mask
);
3357 if (!output_signature
)
3359 if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_COLOR
))
3361 if (semantic_idx_in
== 0)
3362 shader_addline(buffer
, "%s%s = front_color%s;\n",
3363 destination
, reg_mask
, reg_mask
);
3364 else if (semantic_idx_in
== 1)
3365 shader_addline(buffer
, "%s%s = front_secondary_color%s;\n",
3366 destination
, reg_mask
, reg_mask
);
3368 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3369 destination
, reg_mask
, reg_mask
);
3371 else if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_TEXCOORD
))
3373 if (semantic_idx_in
< 8)
3375 shader_addline(buffer
, "%s%s = gl_TexCoord[%u]%s;\n",
3376 destination
, reg_mask
, semantic_idx_in
, reg_mask
);
3380 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3381 destination
, reg_mask
, reg_mask
);
3384 else if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_FOG
))
3386 shader_addline(buffer
, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3387 destination
, reg_mask
, reg_mask
);
3391 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3392 destination
, reg_mask
, reg_mask
);
3397 output_map
= reg_maps_out
->output_registers
;
3398 for (j
= 0; output_map
; output_map
>>= 1, ++j
)
3400 if (!(output_map
& 1)) continue;
3402 semantic_name_out
= output_signature
[j
].semantic_name
;
3403 semantic_idx_out
= output_signature
[j
].semantic_idx
;
3404 shader_glsl_write_mask_to_str(output_signature
[j
].mask
, reg_mask_out
);
3406 if (semantic_idx_in
== semantic_idx_out
3407 && !strcmp(semantic_name_in
, semantic_name_out
))
3409 shader_addline(buffer
, "%s%s = OUT[%u]%s;\n",
3410 destination
, reg_mask
, j
, reg_mask
);
3415 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3416 destination
, reg_mask
, reg_mask
);
3421 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3422 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3423 * input varyings are assigned above, if the optimizer works properly.
3425 for(i
= 0; i
< in_count
+ 2; i
++) {
3426 if (set
[i
] && set
[i
] != WINED3DSP_WRITEMASK_ALL
)
3428 unsigned int size
= 0;
3429 memset(reg_mask
, 0, sizeof(reg_mask
));
3430 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
3431 reg_mask
[size
] = 'x';
3434 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
3435 reg_mask
[size
] = 'y';
3438 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
3439 reg_mask
[size
] = 'z';
3442 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
3443 reg_mask
[size
] = 'w';
3447 if (i
== in_count
) {
3448 sprintf(destination
, "gl_FrontColor");
3449 } else if (i
== in_count
+ 1) {
3450 sprintf(destination
, "gl_FrontSecondaryColor");
3452 sprintf(destination
, "IN[%u]", i
);
3456 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
3458 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
3463 HeapFree(GetProcessHeap(), 0, set
);
3466 /* GL locking is done by the caller */
3467 static GLhandleARB
generate_param_reorder_function(IWineD3DVertexShader
*vertexshader
,
3468 IWineD3DPixelShader
*pixelshader
, const WineD3D_GL_Info
*gl_info
)
3470 GLhandleARB ret
= 0;
3471 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
3472 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
3473 IWineD3DDeviceImpl
*device
;
3474 DWORD vs_major
= vs
->baseShader
.reg_maps
.shader_version
.major
;
3475 DWORD ps_major
= ps
? ps
->baseShader
.reg_maps
.shader_version
.major
: 0;
3477 SHADER_BUFFER buffer
;
3478 const char *semantic_name
;
3481 const struct wined3d_shader_signature_element
*output_signature
;
3483 shader_buffer_init(&buffer
);
3485 shader_addline(&buffer
, "#version 120\n");
3487 if(vs_major
< 3 && ps_major
< 3) {
3488 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3489 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3491 device
= (IWineD3DDeviceImpl
*) vs
->baseShader
.device
;
3492 if((GLINFO_LOCATION
).set_texcoord_w
&& ps_major
== 0 && vs_major
> 0 &&
3493 !device
->frag_pipe
->ffp_proj_control
) {
3494 shader_addline(&buffer
, "void order_ps_input() {\n");
3495 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
3496 if(vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
3497 vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3498 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", i
);
3501 shader_addline(&buffer
, "}\n");
3503 shader_addline(&buffer
, "void order_ps_input() { /* do nothing */ }\n");
3505 } else if(ps_major
< 3 && vs_major
>= 3) {
3506 WORD map
= vs
->baseShader
.reg_maps
.output_registers
;
3508 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3509 output_signature
= vs
->output_signature
;
3511 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3512 for (i
= 0; map
; map
>>= 1, ++i
)
3516 if (!(map
& 1)) continue;
3518 semantic_name
= output_signature
[i
].semantic_name
;
3519 semantic_idx
= output_signature
[i
].semantic_idx
;
3520 write_mask
= output_signature
[i
].mask
;
3521 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
3523 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3525 if (semantic_idx
== 0)
3526 shader_addline(&buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3527 else if (semantic_idx
== 1)
3528 shader_addline(&buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3530 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3532 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3534 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3536 if (semantic_idx
< 8)
3538 if (!(GLINFO_LOCATION
).set_texcoord_w
|| ps_major
> 0) write_mask
|= WINED3DSP_WRITEMASK_3
;
3540 shader_addline(&buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3541 semantic_idx
, reg_mask
, i
, reg_mask
);
3542 if (!(write_mask
& WINED3DSP_WRITEMASK_3
))
3543 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx
);
3546 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3548 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3550 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3552 shader_addline(&buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
3555 shader_addline(&buffer
, "}\n");
3557 } else if(ps_major
>= 3 && vs_major
>= 3) {
3558 WORD map
= vs
->baseShader
.reg_maps
.output_registers
;
3560 output_signature
= vs
->output_signature
;
3562 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3563 shader_addline(&buffer
, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info
));
3564 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3566 /* First, sort out position and point size. Those are not passed to the pixel shader */
3567 for (i
= 0; map
; map
>>= 1, ++i
)
3569 if (!(map
& 1)) continue;
3571 semantic_name
= output_signature
[i
].semantic_name
;
3572 shader_glsl_write_mask_to_str(output_signature
[i
].mask
, reg_mask
);
3574 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3576 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3578 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3580 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3584 /* Then, fix the pixel shader input */
3585 handle_ps3_input(&buffer
, gl_info
, ps
->input_reg_map
, ps
->input_signature
,
3586 &ps
->baseShader
.reg_maps
, output_signature
, &vs
->baseShader
.reg_maps
);
3588 shader_addline(&buffer
, "}\n");
3589 } else if(ps_major
>= 3 && vs_major
< 3) {
3590 shader_addline(&buffer
, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info
));
3591 shader_addline(&buffer
, "void order_ps_input() {\n");
3592 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3593 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3594 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3596 handle_ps3_input(&buffer
, gl_info
, ps
->input_reg_map
, ps
->input_signature
,
3597 &ps
->baseShader
.reg_maps
, NULL
, NULL
);
3598 shader_addline(&buffer
, "}\n");
3600 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
3603 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3604 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3605 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
.buffer
, NULL
));
3606 checkGLcall("glShaderSourceARB(ret, 1, &buffer.buffer, NULL)");
3607 GL_EXTCALL(glCompileShaderARB(ret
));
3608 checkGLcall("glCompileShaderARB(ret)");
3610 shader_buffer_free(&buffer
);
3614 /* GL locking is done by the caller */
3615 static void hardcode_local_constants(IWineD3DBaseShaderImpl
*shader
, const WineD3D_GL_Info
*gl_info
,
3616 GLhandleARB programId
, char prefix
)
3618 const local_constant
*lconst
;
3623 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
) {
3624 value
= (const float *)lconst
->value
;
3625 snprintf(glsl_name
, sizeof(glsl_name
), "%cLC%u", prefix
, lconst
->idx
);
3626 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3627 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, value
));
3629 checkGLcall("Hardcoding local constants\n");
3632 /* GL locking is done by the caller */
3633 static GLuint
shader_glsl_generate_pshader(IWineD3DPixelShaderImpl
*This
,
3634 SHADER_BUFFER
*buffer
, const struct ps_compile_args
*args
)
3636 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
3637 CONST DWORD
*function
= This
->baseShader
.function
;
3638 const char *fragcolor
;
3639 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
3640 struct shader_glsl_ctx_priv priv_ctx
;
3642 /* Create the hw GLSL shader object and assign it as the shader->prgId */
3643 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3645 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3646 priv_ctx
.cur_ps_args
= args
;
3648 shader_addline(buffer
, "#version 120\n");
3650 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
3651 shader_addline(buffer
, "#extension GL_ARB_draw_buffers : enable\n");
3653 if(GL_SUPPORT(ARB_SHADER_TEXTURE_LOD
) && reg_maps
->usestexldd
) {
3654 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
3656 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
3657 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
3658 * drivers write a warning if we don't do so
3660 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
3663 /* Base Declarations */
3664 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
, args
);
3666 /* Pack 3.0 inputs */
3667 if (reg_maps
->shader_version
.major
>= 3 && args
->vp_mode
!= vertexshader
)
3669 pshader_glsl_input_pack((IWineD3DPixelShader
*) This
, buffer
, This
->input_signature
, reg_maps
, args
->vp_mode
);
3672 /* Base Shader Body */
3673 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3675 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
3676 if (reg_maps
->shader_version
.major
< 2)
3678 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
3679 if(GL_SUPPORT(ARB_DRAW_BUFFERS
))
3680 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
3682 shader_addline(buffer
, "gl_FragColor = R0;\n");
3685 if(GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
3686 fragcolor
= "gl_FragData[0]";
3688 fragcolor
= "gl_FragColor";
3690 if(args
->srgb_correction
) {
3691 shader_addline(buffer
, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
3692 fragcolor
, srgb_pow
, srgb_pow
, srgb_pow
, srgb_mul_high
, srgb_mul_high
, srgb_mul_high
,
3693 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
3694 shader_addline(buffer
, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor
);
3695 shader_addline(buffer
, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor
, fragcolor
);
3696 shader_addline(buffer
, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor
, fragcolor
);
3697 shader_addline(buffer
, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor
, fragcolor
);
3698 shader_addline(buffer
, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor
, fragcolor
);
3700 /* Pixel shader < 3.0 do not replace the fog stage.
3701 * This implements linear fog computation and blending.
3702 * TODO: non linear fog
3703 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
3704 * -1/(e-s) and e/(e-s) respectively.
3706 if (reg_maps
->shader_version
.major
< 3)
3709 case FOG_OFF
: break;
3711 shader_addline(buffer
, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
3712 shader_addline(buffer
, "float fogend = gl_Fog.end * -fogstart;\n");
3713 shader_addline(buffer
, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
3714 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
3717 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
3718 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
3719 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
3720 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
3723 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
3724 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
3725 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
3726 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
3731 shader_addline(buffer
, "}\n");
3733 TRACE("Compiling shader object %u\n", shader_obj
);
3734 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
3735 GL_EXTCALL(glCompileShaderARB(shader_obj
));
3736 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
3738 /* Store the shader object */
3742 /* GL locking is done by the caller */
3743 static GLuint
shader_glsl_generate_vshader(IWineD3DVertexShaderImpl
*This
,
3744 SHADER_BUFFER
*buffer
, const struct vs_compile_args
*args
)
3746 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
3747 CONST DWORD
*function
= This
->baseShader
.function
;
3748 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
3749 struct shader_glsl_ctx_priv priv_ctx
;
3751 /* Create the hw GLSL shader program and assign it as the shader->prgId */
3752 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3754 shader_addline(buffer
, "#version 120\n");
3756 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
3757 priv_ctx
.cur_vs_args
= args
;
3759 /* Base Declarations */
3760 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
, NULL
);
3762 /* Base Shader Body */
3763 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
3765 /* Unpack 3.0 outputs */
3766 if (reg_maps
->shader_version
.major
>= 3) shader_addline(buffer
, "order_ps_input(OUT);\n");
3767 else shader_addline(buffer
, "order_ps_input();\n");
3769 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
3770 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
3771 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
3772 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
3774 if(args
->fog_src
== VS_FOG_Z
) {
3775 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
3776 } else if (!reg_maps
->fog
) {
3777 shader_addline(buffer
, "gl_FogFragCoord = 0.0;\n");
3780 /* Write the final position.
3782 * OpenGL coordinates specify the center of the pixel while d3d coords specify
3783 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
3784 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
3785 * contains 1.0 to allow a mad.
3787 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
3788 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
3789 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
3791 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
3793 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
3794 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
3795 * which is the same as z = z * 2 - w.
3797 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
3799 shader_addline(buffer
, "}\n");
3801 TRACE("Compiling shader object %u\n", shader_obj
);
3802 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
3803 GL_EXTCALL(glCompileShaderARB(shader_obj
));
3804 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
3809 static GLhandleARB
find_glsl_pshader(IWineD3DPixelShaderImpl
*shader
, const struct ps_compile_args
*args
)
3813 struct glsl_ps_compiled_shader
*new_array
;
3814 SHADER_BUFFER buffer
;
3815 struct glsl_pshader_private
*shader_data
;
3818 if(!shader
->backend_priv
) {
3819 shader
->backend_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
3821 shader_data
= shader
->backend_priv
;
3823 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3824 * so a linear search is more performant than a hashmap or a binary search
3825 * (cache coherency etc)
3827 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
3828 if(memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)) == 0) {
3829 return shader_data
->gl_shaders
[i
].prgId
;
3833 TRACE("No matching GL shader found, compiling a new shader\n");
3834 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
3835 if (shader_data
->num_gl_shaders
)
3837 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
3838 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
3839 new_size
* sizeof(*shader_data
->gl_shaders
));
3841 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
3846 ERR("Out of memory\n");
3849 shader_data
->gl_shaders
= new_array
;
3850 shader_data
->shader_array_size
= new_size
;
3853 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
3855 pixelshader_update_samplers(&shader
->baseShader
.reg_maps
,
3856 ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->stateBlock
->textures
);
3858 shader_buffer_init(&buffer
);
3859 ret
= shader_glsl_generate_pshader(shader
, &buffer
, args
);
3860 shader_buffer_free(&buffer
);
3861 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
3866 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
3867 const DWORD use_map
) {
3868 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
3869 return stored
->fog_src
== new->fog_src
;
3872 static GLhandleARB
find_glsl_vshader(IWineD3DVertexShaderImpl
*shader
, const struct vs_compile_args
*args
)
3876 struct glsl_vs_compiled_shader
*new_array
;
3877 DWORD use_map
= ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.use_map
;
3878 SHADER_BUFFER buffer
;
3879 struct glsl_vshader_private
*shader_data
;
3882 if(!shader
->backend_priv
) {
3883 shader
->backend_priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
3885 shader_data
= shader
->backend_priv
;
3887 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
3888 * so a linear search is more performant than a hashmap or a binary search
3889 * (cache coherency etc)
3891 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
3892 if(vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
, use_map
)) {
3893 return shader_data
->gl_shaders
[i
].prgId
;
3897 TRACE("No matching GL shader found, compiling a new shader\n");
3899 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
3900 if (shader_data
->num_gl_shaders
)
3902 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
3903 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
3904 new_size
* sizeof(*shader_data
->gl_shaders
));
3906 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
3911 ERR("Out of memory\n");
3914 shader_data
->gl_shaders
= new_array
;
3915 shader_data
->shader_array_size
= new_size
;
3918 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
3920 shader_buffer_init(&buffer
);
3921 ret
= shader_glsl_generate_vshader(shader
, &buffer
, args
);
3922 shader_buffer_free(&buffer
);
3923 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
3928 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3929 * It sets the programId on the current StateBlock (because it should be called
3930 * inside of the DrawPrimitive() part of the render loop).
3932 * If a program for the given combination does not exist, create one, and store
3933 * the program in the hash table. If it creates a program, it will link the
3934 * given objects, too.
3937 /* GL locking is done by the caller */
3938 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
3939 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3940 struct shader_glsl_priv
*priv
= This
->shader_priv
;
3941 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3942 IWineD3DPixelShader
*pshader
= use_ps
? This
->stateBlock
->pixelShader
: NULL
;
3943 IWineD3DVertexShader
*vshader
= use_vs
? This
->stateBlock
->vertexShader
: NULL
;
3944 struct glsl_shader_prog_link
*entry
= NULL
;
3945 GLhandleARB programId
= 0;
3946 GLhandleARB reorder_shader_id
= 0;
3949 struct ps_compile_args ps_compile_args
;
3950 struct vs_compile_args vs_compile_args
;
3952 if (vshader
) find_vs_compile_args((IWineD3DVertexShaderImpl
*)vshader
, This
->stateBlock
, &vs_compile_args
);
3953 if (pshader
) find_ps_compile_args((IWineD3DPixelShaderImpl
*)pshader
, This
->stateBlock
, &ps_compile_args
);
3955 entry
= get_glsl_program_entry(priv
, vshader
, pshader
, &vs_compile_args
, &ps_compile_args
);
3957 priv
->glsl_program
= entry
;
3961 /* If we get to this point, then no matching program exists, so we create one */
3962 programId
= GL_EXTCALL(glCreateProgramObjectARB());
3963 TRACE("Created new GLSL shader program %u\n", programId
);
3965 /* Create the entry */
3966 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
3967 entry
->programId
= programId
;
3968 entry
->vshader
= vshader
;
3969 entry
->pshader
= pshader
;
3970 entry
->vs_args
= vs_compile_args
;
3971 entry
->ps_args
= ps_compile_args
;
3972 entry
->constant_version
= 0;
3973 /* Add the hash table entry */
3974 add_glsl_program_entry(priv
, entry
);
3976 /* Set the current program */
3977 priv
->glsl_program
= entry
;
3979 /* Attach GLSL vshader */
3982 GLhandleARB vshader_id
= find_glsl_vshader((IWineD3DVertexShaderImpl
*)vshader
, &vs_compile_args
);
3983 WORD map
= ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.input_registers
;
3986 reorder_shader_id
= generate_param_reorder_function(vshader
, pshader
, gl_info
);
3987 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
3988 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
3989 checkGLcall("glAttachObjectARB");
3990 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3993 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
3995 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
3996 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
3997 checkGLcall("glAttachObjectARB");
3999 /* Bind vertex attributes to a corresponding index number to match
4000 * the same index numbers as ARB_vertex_programs (makes loading
4001 * vertex attributes simpler). With this method, we can use the
4002 * exact same code to load the attributes later for both ARB and
4005 * We have to do this here because we need to know the Program ID
4006 * in order to make the bindings work, and it has to be done prior
4007 * to linking the GLSL program. */
4008 for (i
= 0; map
; map
>>= 1, ++i
)
4010 if (!(map
& 1)) continue;
4012 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%u", i
);
4013 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
4015 checkGLcall("glBindAttribLocationARB");
4017 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
4020 /* Attach GLSL pshader */
4023 GLhandleARB pshader_id
= find_glsl_pshader((IWineD3DPixelShaderImpl
*)pshader
, &ps_compile_args
);
4024 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
4025 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
4026 checkGLcall("glAttachObjectARB");
4028 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
4031 /* Link the program */
4032 TRACE("Linking GLSL shader program %u\n", programId
);
4033 GL_EXTCALL(glLinkProgramARB(programId
));
4034 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
4036 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
4037 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
4038 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
4039 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4041 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
4042 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
4043 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4045 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
4046 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
4047 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
4048 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4050 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
4051 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
4052 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4059 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
4060 sprintf(name
, "bumpenvmat%u", i
);
4061 entry
->bumpenvmat_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4062 sprintf(name
, "luminancescale%u", i
);
4063 entry
->luminancescale_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4064 sprintf(name
, "luminanceoffset%u", i
);
4065 entry
->luminanceoffset_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4068 map
= ps_compile_args
.np2_fixup
;
4069 for (i
= 0; map
; map
>>= 1, ++i
)
4071 if (!(map
& 1)) continue;
4073 sprintf(name
, "PsamplerNP2Fixup%u", i
);
4074 entry
->np2Fixup_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4078 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
4079 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
4080 checkGLcall("Find glsl program uniform locations");
4083 && ((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.reg_maps
.shader_version
.major
>= 3
4084 && ((IWineD3DPixelShaderImpl
*)pshader
)->declared_in_count
> vec4_varyings(3, gl_info
))
4086 TRACE("Shader %d needs vertex color clamping disabled\n", programId
);
4087 entry
->vertex_color_clamp
= GL_FALSE
;
4089 entry
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
4092 /* Set the shader to allow uniform loading on it */
4093 GL_EXTCALL(glUseProgramObjectARB(programId
));
4094 checkGLcall("glUseProgramObjectARB(programId)");
4096 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4097 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4098 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4099 * vertex shader with fixed function pixel processing is used we make sure that the card
4100 * supports enough samplers to allow the max number of vertex samplers with all possible
4101 * fixed function fragment processing setups. So once the program is linked these samplers
4104 if (vshader
) shader_glsl_load_vsamplers(gl_info
, This
->texUnitMap
, programId
);
4105 if (pshader
) shader_glsl_load_psamplers(gl_info
, This
->texUnitMap
, programId
);
4107 /* If the local constants do not have to be loaded with the environment constants,
4108 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4111 if(pshader
&& !((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.load_local_constsF
) {
4112 hardcode_local_constants((IWineD3DBaseShaderImpl
*) pshader
, gl_info
, programId
, 'P');
4114 if(vshader
&& !((IWineD3DVertexShaderImpl
*)vshader
)->baseShader
.load_local_constsF
) {
4115 hardcode_local_constants((IWineD3DBaseShaderImpl
*) vshader
, gl_info
, programId
, 'V');
4119 /* GL locking is done by the caller */
4120 static GLhandleARB
create_glsl_blt_shader(const WineD3D_GL_Info
*gl_info
, enum tex_types tex_type
)
4122 GLhandleARB program_id
;
4123 GLhandleARB vshader_id
, pshader_id
;
4124 static const char *blt_vshader
[] =
4129 " gl_Position = gl_Vertex;\n"
4130 " gl_FrontColor = vec4(1.0);\n"
4131 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4135 static const char *blt_pshaders
[tex_type_count
] =
4141 "uniform sampler2D sampler;\n"
4144 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4150 "uniform samplerCube sampler;\n"
4153 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4157 "#extension GL_ARB_texture_rectangle : enable\n"
4158 "uniform sampler2DRect sampler;\n"
4161 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4165 if (!blt_pshaders
[tex_type
])
4167 FIXME("tex_type %#x not supported\n", tex_type
);
4171 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4172 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
4173 GL_EXTCALL(glCompileShaderARB(vshader_id
));
4175 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
4176 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, &blt_pshaders
[tex_type
], NULL
));
4177 GL_EXTCALL(glCompileShaderARB(pshader_id
));
4179 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
4180 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
4181 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
4182 GL_EXTCALL(glLinkProgramARB(program_id
));
4184 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
4186 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4189 GL_EXTCALL(glDeleteObjectARB(vshader_id
));
4190 GL_EXTCALL(glDeleteObjectARB(pshader_id
));
4194 /* GL locking is done by the caller */
4195 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
4196 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4197 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4198 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4199 GLhandleARB program_id
= 0;
4200 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
4202 old_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4204 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
4205 else priv
->glsl_program
= NULL
;
4207 current_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4209 if (old_vertex_color_clamp
!= current_vertex_color_clamp
) {
4210 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT
)) {
4211 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
4212 checkGLcall("glClampColorARB");
4214 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4218 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4219 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4220 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4221 checkGLcall("glUseProgramObjectARB");
4224 /* GL locking is done by the caller */
4225 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {
4226 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4227 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4228 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4229 GLhandleARB
*blt_program
= &priv
->depth_blt_program
[tex_type
];
4231 if (!*blt_program
) {
4233 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
);
4234 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "sampler"));
4235 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4236 GL_EXTCALL(glUniform1iARB(loc
, 0));
4238 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4242 /* GL locking is done by the caller */
4243 static void shader_glsl_deselect_depth_blt(IWineD3DDevice
*iface
) {
4244 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4245 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4246 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4247 GLhandleARB program_id
;
4249 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4250 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4252 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4253 checkGLcall("glUseProgramObjectARB");
4256 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
4257 const struct list
*linked_programs
;
4258 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
4259 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
4260 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4261 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
4262 IWineD3DPixelShaderImpl
*ps
= NULL
;
4263 IWineD3DVertexShaderImpl
*vs
= NULL
;
4265 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
4266 * can be called from IWineD3DBaseShader::Release
4268 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
4270 ActivateContext(device
, device
->lastActiveRenderTarget
, CTXUSAGE_RESOURCELOAD
);
4273 struct glsl_pshader_private
*shader_data
;
4274 ps
= (IWineD3DPixelShaderImpl
*) This
;
4275 shader_data
= ps
->backend_priv
;
4276 if(!shader_data
|| shader_data
->num_gl_shaders
== 0)
4278 HeapFree(GetProcessHeap(), 0, shader_data
);
4279 ps
->backend_priv
= NULL
;
4283 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->pshader
== iface
)
4286 shader_glsl_select(This
->baseShader
.device
, FALSE
, FALSE
);
4290 struct glsl_vshader_private
*shader_data
;
4291 vs
= (IWineD3DVertexShaderImpl
*) This
;
4292 shader_data
= vs
->backend_priv
;
4293 if(!shader_data
|| shader_data
->num_gl_shaders
== 0)
4295 HeapFree(GetProcessHeap(), 0, shader_data
);
4296 vs
->backend_priv
= NULL
;
4300 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->vshader
== iface
)
4303 shader_glsl_select(This
->baseShader
.device
, FALSE
, FALSE
);
4308 linked_programs
= &This
->baseShader
.linked_programs
;
4310 TRACE("Deleting linked programs\n");
4311 if (linked_programs
->next
) {
4312 struct glsl_shader_prog_link
*entry
, *entry2
;
4316 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
4317 delete_glsl_program_entry(priv
, gl_info
, entry
);
4320 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
4321 delete_glsl_program_entry(priv
, gl_info
, entry
);
4329 struct glsl_pshader_private
*shader_data
= ps
->backend_priv
;
4332 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4333 TRACE("deleting pshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4334 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4335 checkGLcall("glDeleteObjectARB");
4338 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4339 HeapFree(GetProcessHeap(), 0, shader_data
);
4340 ps
->backend_priv
= NULL
;
4343 struct glsl_vshader_private
*shader_data
= vs
->backend_priv
;
4346 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4347 TRACE("deleting vshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4348 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4349 checkGLcall("glDeleteObjectARB");
4352 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4353 HeapFree(GetProcessHeap(), 0, shader_data
);
4354 vs
->backend_priv
= NULL
;
4358 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
4360 const glsl_program_key_t
*k
= key
;
4361 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
4362 const struct glsl_shader_prog_link
, program_lookup_entry
);
4365 if (k
->vshader
> prog
->vshader
) return 1;
4366 else if (k
->vshader
< prog
->vshader
) return -1;
4368 if (k
->pshader
> prog
->pshader
) return 1;
4369 else if (k
->pshader
< prog
->pshader
) return -1;
4371 if (k
->vshader
&& (cmp
= memcmp(&k
->vs_args
, &prog
->vs_args
, sizeof(prog
->vs_args
)))) return cmp
;
4372 if (k
->pshader
&& (cmp
= memcmp(&k
->ps_args
, &prog
->ps_args
, sizeof(prog
->ps_args
)))) return cmp
;
4377 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
4379 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
) + constant_count
* sizeof(*heap
->positions
);
4380 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
4384 ERR("Failed to allocate memory\n");
4388 heap
->entries
= mem
;
4389 heap
->entries
[1].version
= 0;
4390 heap
->positions
= (unsigned int *)(heap
->entries
+ constant_count
+ 1);
4396 static void constant_heap_free(struct constant_heap
*heap
)
4398 HeapFree(GetProcessHeap(), 0, heap
->entries
);
4401 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
4406 glsl_program_key_compare
,
4409 static HRESULT
shader_glsl_alloc(IWineD3DDevice
*iface
) {
4410 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4411 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4412 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
4413 SIZE_T stack_size
= wined3d_log2i(max(GL_LIMITS(vshader_constantsF
), GL_LIMITS(pshader_constantsF
))) + 1;
4415 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
4418 ERR("Failed to allocate memory.\n");
4419 HeapFree(GetProcessHeap(), 0, priv
);
4420 return E_OUTOFMEMORY
;
4423 if (!constant_heap_init(&priv
->vconst_heap
, GL_LIMITS(vshader_constantsF
)))
4425 ERR("Failed to initialize vertex shader constant heap\n");
4426 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4427 HeapFree(GetProcessHeap(), 0, priv
);
4428 return E_OUTOFMEMORY
;
4431 if (!constant_heap_init(&priv
->pconst_heap
, GL_LIMITS(pshader_constantsF
)))
4433 ERR("Failed to initialize pixel shader constant heap\n");
4434 constant_heap_free(&priv
->vconst_heap
);
4435 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4436 HeapFree(GetProcessHeap(), 0, priv
);
4437 return E_OUTOFMEMORY
;
4440 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
4442 ERR("Failed to initialize rbtree.\n");
4443 constant_heap_free(&priv
->pconst_heap
);
4444 constant_heap_free(&priv
->vconst_heap
);
4445 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4446 HeapFree(GetProcessHeap(), 0, priv
);
4447 return E_OUTOFMEMORY
;
4450 priv
->next_constant_version
= 1;
4452 This
->shader_priv
= priv
;
4456 static void shader_glsl_free(IWineD3DDevice
*iface
) {
4457 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4458 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4459 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4463 for (i
= 0; i
< tex_type_count
; ++i
)
4465 if (priv
->depth_blt_program
[i
])
4467 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program
[i
]));
4472 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
4473 constant_heap_free(&priv
->pconst_heap
);
4474 constant_heap_free(&priv
->vconst_heap
);
4475 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4477 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
4478 This
->shader_priv
= NULL
;
4481 static BOOL
shader_glsl_dirty_const(IWineD3DDevice
*iface
) {
4482 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
4486 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
4488 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4489 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
4490 * vs_nv_version which is based on NV_vertex_program.
4491 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4492 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4493 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4494 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4496 if((GLINFO_LOCATION
.vs_nv_version
== VS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
4497 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
4499 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
4500 TRACE_(d3d_caps
)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps
->VertexShaderVersion
>> 8) & 0xff, pCaps
->VertexShaderVersion
& 0xff);
4501 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
);
4503 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4504 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4505 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4506 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4507 * in max native instructions. Intel and others also offer the info in this extension but they
4508 * don't support GLSL (at least on Windows).
4510 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4511 * of instructions is 512 or less we have to do with ps2.0 hardware.
4512 * NOTE: ps3.0 hardware requires 512 or more instructions but ati and nvidia offer 'enough' (1024 vs 4096) on their most basic ps3.0 hardware.
4514 if((GLINFO_LOCATION
.ps_nv_version
== PS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
4515 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
4517 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
4519 pCaps
->MaxPixelShaderConst
= GL_LIMITS(pshader_constantsF
);
4521 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4522 * Direct3D minimum requirement.
4524 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4525 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4527 * The problem is that the refrast clamps temporary results in the shader to
4528 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4529 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4530 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4531 * offer a way to query this.
4533 pCaps
->PixelShader1xMaxValue
= 8.0;
4534 TRACE_(d3d_caps
)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps
->PixelShaderVersion
>> 8) & 0xff, pCaps
->PixelShaderVersion
& 0xff);
4536 pCaps
->VSClipping
= TRUE
;
4539 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
4541 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4543 TRACE("Checking support for fixup:\n");
4544 dump_color_fixup_desc(fixup
);
4547 /* We support everything except YUV conversions. */
4548 if (!is_yuv_fixup(fixup
))
4554 TRACE("[FAILED]\n");
4558 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4560 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
4561 /* WINED3DSIH_ADD */ shader_glsl_arith
,
4562 /* WINED3DSIH_BEM */ pshader_glsl_bem
,
4563 /* WINED3DSIH_BREAK */ shader_glsl_break
,
4564 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
4565 /* WINED3DSIH_BREAKP */ NULL
,
4566 /* WINED3DSIH_CALL */ shader_glsl_call
,
4567 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
4568 /* WINED3DSIH_CMP */ shader_glsl_cmp
,
4569 /* WINED3DSIH_CND */ shader_glsl_cnd
,
4570 /* WINED3DSIH_CRS */ shader_glsl_cross
,
4571 /* WINED3DSIH_DCL */ NULL
,
4572 /* WINED3DSIH_DEF */ NULL
,
4573 /* WINED3DSIH_DEFB */ NULL
,
4574 /* WINED3DSIH_DEFI */ NULL
,
4575 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add
,
4576 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
4577 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
4578 /* WINED3DSIH_DST */ shader_glsl_dst
,
4579 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
4580 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
4581 /* WINED3DSIH_ELSE */ shader_glsl_else
,
4582 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
4583 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
4584 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
4585 /* WINED3DSIH_EXP */ shader_glsl_map2gl
,
4586 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
4587 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
4588 /* WINED3DSIH_IF */ shader_glsl_if
,
4589 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
4590 /* WINED3DSIH_LABEL */ shader_glsl_label
,
4591 /* WINED3DSIH_LIT */ shader_glsl_lit
,
4592 /* WINED3DSIH_LOG */ shader_glsl_log
,
4593 /* WINED3DSIH_LOGP */ shader_glsl_log
,
4594 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
4595 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
4596 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
4597 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
4598 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
4599 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
4600 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
4601 /* WINED3DSIH_MAD */ shader_glsl_mad
,
4602 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
4603 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
4604 /* WINED3DSIH_MOV */ shader_glsl_mov
,
4605 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
4606 /* WINED3DSIH_MUL */ shader_glsl_arith
,
4607 /* WINED3DSIH_NOP */ NULL
,
4608 /* WINED3DSIH_NRM */ shader_glsl_map2gl
,
4609 /* WINED3DSIH_PHASE */ NULL
,
4610 /* WINED3DSIH_POW */ shader_glsl_pow
,
4611 /* WINED3DSIH_RCP */ shader_glsl_rcp
,
4612 /* WINED3DSIH_REP */ shader_glsl_rep
,
4613 /* WINED3DSIH_RET */ NULL
,
4614 /* WINED3DSIH_RSQ */ shader_glsl_rsq
,
4615 /* WINED3DSIH_SETP */ NULL
,
4616 /* WINED3DSIH_SGE */ shader_glsl_compare
,
4617 /* WINED3DSIH_SGN */ shader_glsl_map2gl
,
4618 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
4619 /* WINED3DSIH_SLT */ shader_glsl_compare
,
4620 /* WINED3DSIH_SUB */ shader_glsl_arith
,
4621 /* WINED3DSIH_TEX */ pshader_glsl_tex
,
4622 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem
,
4623 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem
,
4624 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord
,
4625 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth
,
4626 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3
,
4627 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex
,
4628 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill
,
4629 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
4630 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
4631 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth
,
4632 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad
,
4633 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex
,
4634 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3
,
4635 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
4636 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad
,
4637 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec
,
4638 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex
,
4639 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec
,
4640 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar
,
4641 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb
,
4642 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb
,
4645 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
4646 SHADER_HANDLER hw_fct
;
4648 /* Select handler */
4649 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
4651 /* Unhandled opcode */
4654 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
4659 shader_glsl_add_instruction_modifiers(ins
);
4662 const shader_backend_t glsl_shader_backend
= {
4663 shader_glsl_handle_instruction
,
4665 shader_glsl_select_depth_blt
,
4666 shader_glsl_deselect_depth_blt
,
4667 shader_glsl_update_float_vertex_constants
,
4668 shader_glsl_update_float_pixel_constants
,
4669 shader_glsl_load_constants
,
4670 shader_glsl_load_np2fixup_constants
,
4671 shader_glsl_destroy
,
4674 shader_glsl_dirty_const
,
4675 shader_glsl_get_caps
,
4676 shader_glsl_color_fixup_supported
,