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 wined3d_shader_buffer shader_buffer
;
87 struct wine_rb_tree program_lookup
;
88 struct glsl_shader_prog_link
*glsl_program
;
89 struct constant_heap vconst_heap
;
90 struct constant_heap pconst_heap
;
92 GLhandleARB depth_blt_program_full
[tex_type_count
];
93 GLhandleARB depth_blt_program_masked
[tex_type_count
];
94 UINT next_constant_version
;
97 /* Struct to maintain data about a linked GLSL program */
98 struct glsl_shader_prog_link
{
99 struct wine_rb_entry program_lookup_entry
;
100 struct list vshader_entry
;
101 struct list pshader_entry
;
102 GLhandleARB programId
;
103 GLint
*vuniformF_locations
;
104 GLint
*puniformF_locations
;
105 GLint vuniformI_locations
[MAX_CONST_I
];
106 GLint puniformI_locations
[MAX_CONST_I
];
107 GLint posFixup_location
;
108 GLint np2Fixup_location
;
109 GLint bumpenvmat_location
[MAX_TEXTURES
];
110 GLint luminancescale_location
[MAX_TEXTURES
];
111 GLint luminanceoffset_location
[MAX_TEXTURES
];
112 GLint ycorrection_location
;
113 GLenum vertex_color_clamp
;
114 IWineD3DVertexShader
*vshader
;
115 IWineD3DPixelShader
*pshader
;
116 struct vs_compile_args vs_args
;
117 struct ps_compile_args ps_args
;
118 UINT constant_version
;
119 const struct ps_np2fixup_info
*np2Fixup_info
;
123 IWineD3DVertexShader
*vshader
;
124 IWineD3DPixelShader
*pshader
;
125 struct ps_compile_args ps_args
;
126 struct vs_compile_args vs_args
;
127 } glsl_program_key_t
;
129 struct shader_glsl_ctx_priv
{
130 const struct vs_compile_args
*cur_vs_args
;
131 const struct ps_compile_args
*cur_ps_args
;
132 struct ps_np2fixup_info
*cur_np2fixup_info
;
135 struct glsl_ps_compiled_shader
137 struct ps_compile_args args
;
138 struct ps_np2fixup_info np2fixup
;
142 struct glsl_pshader_private
144 struct glsl_ps_compiled_shader
*gl_shaders
;
145 UINT num_gl_shaders
, shader_array_size
;
148 struct glsl_vs_compiled_shader
150 struct vs_compile_args args
;
154 struct glsl_vshader_private
156 struct glsl_vs_compiled_shader
*gl_shaders
;
157 UINT num_gl_shaders
, shader_array_size
;
160 static const char *debug_gl_shader_type(GLenum type
)
164 #define WINED3D_TO_STR(u) case u: return #u
165 WINED3D_TO_STR(GL_VERTEX_SHADER_ARB
);
166 WINED3D_TO_STR(GL_GEOMETRY_SHADER_ARB
);
167 WINED3D_TO_STR(GL_FRAGMENT_SHADER_ARB
);
168 #undef WINED3D_TO_STR
170 return wine_dbg_sprintf("UNKNOWN(%#x)", type
);
174 /* Extract a line from the info log.
175 * Note that this modifies the source string. */
176 static char *get_info_log_line(char **ptr
)
181 if (!(q
= strstr(p
, "\n")))
183 if (!*p
) return NULL
;
193 /** Prints the GLSL info log which will contain error messages if they exist */
194 /* GL locking is done by the caller */
195 static void print_glsl_info_log(const struct wined3d_gl_info
*gl_info
, GLhandleARB obj
)
197 int infologLength
= 0;
202 static const char * const spam
[] =
204 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
205 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx, with \n */
206 "Fragment shader was successfully compiled to run on hardware.", /* fglrx, no \n */
207 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
208 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
209 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
210 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
211 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
212 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
215 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
)) return;
217 GL_EXTCALL(glGetObjectParameterivARB(obj
,
218 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
221 /* A size of 1 is just a null-terminated string, so the log should be bigger than
222 * that if there are errors. */
223 if (infologLength
> 1)
227 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
228 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
230 infoLog
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, infologLength
);
231 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
234 for(i
= 0; i
< sizeof(spam
) / sizeof(spam
[0]); i
++) {
235 if(strcmp(infoLog
, spam
[i
]) == 0) {
244 TRACE("Spam received from GLSL shader #%u:\n", obj
);
245 while ((line
= get_info_log_line(&ptr
))) TRACE(" %s\n", line
);
249 FIXME("Error received from GLSL shader #%u:\n", obj
);
250 while ((line
= get_info_log_line(&ptr
))) FIXME(" %s\n", line
);
252 HeapFree(GetProcessHeap(), 0, infoLog
);
256 /* GL locking is done by the caller. */
257 static void shader_glsl_dump_program_source(const struct wined3d_gl_info
*gl_info
, GLhandleARB program
)
259 GLint i
, object_count
, source_size
= -1;
260 GLhandleARB
*objects
;
263 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_ATTACHED_OBJECTS_ARB
, &object_count
));
264 objects
= HeapAlloc(GetProcessHeap(), 0, object_count
* sizeof(*objects
));
267 ERR("Failed to allocate object array memory.\n");
271 GL_EXTCALL(glGetAttachedObjectsARB(program
, object_count
, NULL
, objects
));
272 for (i
= 0; i
< object_count
; ++i
)
277 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_SHADER_SOURCE_LENGTH_ARB
, &tmp
));
279 if (source_size
< tmp
)
281 HeapFree(GetProcessHeap(), 0, source
);
283 source
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, tmp
);
286 ERR("Failed to allocate %d bytes for shader source.\n", tmp
);
287 HeapFree(GetProcessHeap(), 0, objects
);
293 FIXME("Object %u:\n", objects
[i
]);
294 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_SUBTYPE_ARB
, &tmp
));
295 FIXME(" GL_OBJECT_SUBTYPE_ARB: %s.\n", debug_gl_shader_type(tmp
));
296 GL_EXTCALL(glGetObjectParameterivARB(objects
[i
], GL_OBJECT_COMPILE_STATUS_ARB
, &tmp
));
297 FIXME(" GL_OBJECT_COMPILE_STATUS_ARB: %d.\n", tmp
);
301 GL_EXTCALL(glGetShaderSourceARB(objects
[i
], source_size
, NULL
, source
));
302 while ((line
= get_info_log_line(&ptr
))) FIXME(" %s\n", line
);
306 HeapFree(GetProcessHeap(), 0, source
);
307 HeapFree(GetProcessHeap(), 0, objects
);
310 /* GL locking is done by the caller. */
311 static void shader_glsl_validate_link(const struct wined3d_gl_info
*gl_info
, GLhandleARB program
)
315 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
)) return;
317 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_TYPE_ARB
, &tmp
));
318 if (tmp
== GL_PROGRAM_OBJECT_ARB
)
320 GL_EXTCALL(glGetObjectParameterivARB(program
, GL_OBJECT_LINK_STATUS_ARB
, &tmp
));
323 FIXME("Program %u link status invalid.\n", program
);
324 shader_glsl_dump_program_source(gl_info
, program
);
328 print_glsl_info_log(gl_info
, program
);
332 * Loads (pixel shader) samplers
334 /* GL locking is done by the caller */
335 static void shader_glsl_load_psamplers(const struct wined3d_gl_info
*gl_info
,
336 DWORD
*tex_unit_map
, GLhandleARB programId
)
340 char sampler_name
[20];
342 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
343 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
344 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
345 if (name_loc
!= -1) {
346 DWORD mapped_unit
= tex_unit_map
[i
];
347 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< gl_info
->limits
.fragment_samplers
)
349 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
350 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
351 checkGLcall("glUniform1iARB");
353 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
359 /* GL locking is done by the caller */
360 static void shader_glsl_load_vsamplers(const struct wined3d_gl_info
*gl_info
,
361 DWORD
*tex_unit_map
, GLhandleARB programId
)
364 char sampler_name
[20];
367 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
368 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
369 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
370 if (name_loc
!= -1) {
371 DWORD mapped_unit
= tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
];
372 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< gl_info
->limits
.combined_samplers
)
374 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
375 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
376 checkGLcall("glUniform1iARB");
378 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
384 /* GL locking is done by the caller */
385 static inline void walk_constant_heap(const struct wined3d_gl_info
*gl_info
, const float *constants
,
386 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
389 unsigned int heap_idx
= 1;
392 if (heap
->entries
[heap_idx
].version
<= version
) return;
394 idx
= heap
->entries
[heap_idx
].idx
;
395 if (constant_locations
[idx
] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
396 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
398 while (stack_idx
>= 0)
400 /* Note that we fall through to the next case statement. */
401 switch(stack
[stack_idx
])
403 case HEAP_NODE_TRAVERSE_LEFT
:
405 unsigned int left_idx
= heap_idx
<< 1;
406 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
409 idx
= heap
->entries
[heap_idx
].idx
;
410 if (constant_locations
[idx
] != -1)
411 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
413 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
414 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
419 case HEAP_NODE_TRAVERSE_RIGHT
:
421 unsigned int right_idx
= (heap_idx
<< 1) + 1;
422 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
424 heap_idx
= right_idx
;
425 idx
= heap
->entries
[heap_idx
].idx
;
426 if (constant_locations
[idx
] != -1)
427 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
429 stack
[stack_idx
++] = HEAP_NODE_POP
;
430 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
443 checkGLcall("walk_constant_heap()");
446 /* GL locking is done by the caller */
447 static inline void apply_clamped_constant(const struct wined3d_gl_info
*gl_info
, GLint location
, const GLfloat
*data
)
449 GLfloat clamped_constant
[4];
451 if (location
== -1) return;
453 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0f
? 1.0f
: data
[0];
454 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0f
? 1.0f
: data
[1];
455 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0f
? 1.0f
: data
[2];
456 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0f
? 1.0f
: data
[3];
458 GL_EXTCALL(glUniform4fvARB(location
, 1, clamped_constant
));
461 /* GL locking is done by the caller */
462 static inline void walk_constant_heap_clamped(const struct wined3d_gl_info
*gl_info
, const float *constants
,
463 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
466 unsigned int heap_idx
= 1;
469 if (heap
->entries
[heap_idx
].version
<= version
) return;
471 idx
= heap
->entries
[heap_idx
].idx
;
472 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
473 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
475 while (stack_idx
>= 0)
477 /* Note that we fall through to the next case statement. */
478 switch(stack
[stack_idx
])
480 case HEAP_NODE_TRAVERSE_LEFT
:
482 unsigned int left_idx
= heap_idx
<< 1;
483 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
486 idx
= heap
->entries
[heap_idx
].idx
;
487 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
489 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
490 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
495 case HEAP_NODE_TRAVERSE_RIGHT
:
497 unsigned int right_idx
= (heap_idx
<< 1) + 1;
498 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
500 heap_idx
= right_idx
;
501 idx
= heap
->entries
[heap_idx
].idx
;
502 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
504 stack
[stack_idx
++] = HEAP_NODE_POP
;
505 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
518 checkGLcall("walk_constant_heap_clamped()");
521 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
522 /* GL locking is done by the caller */
523 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
*This
, const struct wined3d_gl_info
*gl_info
,
524 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
525 unsigned char *stack
, UINT version
)
527 const local_constant
*lconst
;
529 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
530 if (This
->baseShader
.reg_maps
.shader_version
.major
== 1
531 && shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
))
532 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
534 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
536 if (!This
->baseShader
.load_local_constsF
)
538 TRACE("No need to load local float constants for this shader\n");
542 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
543 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
)
545 GLint location
= constant_locations
[lconst
->idx
];
546 /* We found this uniform name in the program - go ahead and send the data */
547 if (location
!= -1) GL_EXTCALL(glUniform4fvARB(location
, 1, (const GLfloat
*)lconst
->value
));
549 checkGLcall("glUniform4fvARB()");
552 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
553 /* GL locking is done by the caller */
554 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl
*This
, const struct wined3d_gl_info
*gl_info
,
555 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
560 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
562 if (!(constants_set
& 1)) continue;
564 TRACE_(d3d_constants
)("Loading constants %u: %i, %i, %i, %i\n",
565 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
567 /* We found this uniform name in the program - go ahead and send the data */
568 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
569 checkGLcall("glUniform4ivARB");
572 /* Load immediate constants */
573 ptr
= list_head(&This
->baseShader
.constantsI
);
575 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
576 unsigned int idx
= lconst
->idx
;
577 const GLint
*values
= (const GLint
*)lconst
->value
;
579 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
580 values
[0], values
[1], values
[2], values
[3]);
582 /* We found this uniform name in the program - go ahead and send the data */
583 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
584 checkGLcall("glUniform4ivARB");
585 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
589 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
590 /* GL locking is done by the caller */
591 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl
*This
, const struct wined3d_gl_info
*gl_info
,
592 GLhandleARB programId
, const BOOL
*constants
, WORD constants_set
)
600 switch (This
->baseShader
.reg_maps
.shader_version
.type
)
602 case WINED3D_SHADER_TYPE_VERTEX
:
606 case WINED3D_SHADER_TYPE_GEOMETRY
:
610 case WINED3D_SHADER_TYPE_PIXEL
:
615 FIXME("Unknown shader type %#x.\n",
616 This
->baseShader
.reg_maps
.shader_version
.type
);
621 /* TODO: Benchmark and see if it would be beneficial to store the
622 * locations of the constants to avoid looking up each time */
623 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
625 if (!(constants_set
& 1)) continue;
627 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
629 /* TODO: Benchmark and see if it would be beneficial to store the
630 * locations of the constants to avoid looking up each time */
631 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
632 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
635 /* We found this uniform name in the program - go ahead and send the data */
636 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
637 checkGLcall("glUniform1ivARB");
641 /* Load immediate constants */
642 ptr
= list_head(&This
->baseShader
.constantsB
);
644 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
645 unsigned int idx
= lconst
->idx
;
646 const GLint
*values
= (const GLint
*)lconst
->value
;
648 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
650 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
651 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
653 /* We found this uniform name in the program - go ahead and send the data */
654 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
655 checkGLcall("glUniform1ivARB");
657 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
661 static void reset_program_constant_version(struct wine_rb_entry
*entry
, void *context
)
663 WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
)->constant_version
= 0;
667 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
669 /* GL locking is done by the caller (state handler) */
670 static void shader_glsl_load_np2fixup_constants(
671 IWineD3DDevice
* device
,
673 char useVertexShader
) {
675 const IWineD3DDeviceImpl
* deviceImpl
= (const IWineD3DDeviceImpl
*) device
;
676 const struct glsl_shader_prog_link
* prog
= ((struct shader_glsl_priv
*)(deviceImpl
->shader_priv
))->glsl_program
;
679 /* No GLSL program set - nothing to do. */
683 if (!usePixelShader
) {
684 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
688 if (prog
->ps_args
.np2_fixup
&& -1 != prog
->np2Fixup_location
) {
689 const struct wined3d_gl_info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
690 const IWineD3DStateBlockImpl
* stateBlock
= (const IWineD3DStateBlockImpl
*) deviceImpl
->stateBlock
;
692 UINT fixup
= prog
->ps_args
.np2_fixup
;
693 GLfloat np2fixup_constants
[4 * MAX_FRAGMENT_SAMPLERS
];
695 for (i
= 0; fixup
; fixup
>>= 1, ++i
) {
696 const unsigned char idx
= prog
->np2Fixup_info
->idx
[i
];
697 const IWineD3DBaseTextureImpl
* const tex
= (const IWineD3DBaseTextureImpl
*) stateBlock
->textures
[i
];
698 GLfloat
* tex_dim
= &np2fixup_constants
[(idx
>> 1) * 4];
701 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
706 tex_dim
[2] = tex
->baseTexture
.pow2Matrix
[0]; tex_dim
[3] = tex
->baseTexture
.pow2Matrix
[5];
708 tex_dim
[0] = tex
->baseTexture
.pow2Matrix
[0]; tex_dim
[1] = tex
->baseTexture
.pow2Matrix
[5];
712 GL_EXTCALL(glUniform4fvARB(prog
->np2Fixup_location
, prog
->np2Fixup_info
->num_consts
, np2fixup_constants
));
717 * Loads the app-supplied constants into the currently set GLSL program.
719 /* GL locking is done by the caller (state handler) */
720 static void shader_glsl_load_constants(const struct wined3d_context
*context
,
721 char usePixelShader
, char useVertexShader
)
723 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
724 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
725 IWineD3DStateBlockImpl
* stateBlock
= device
->stateBlock
;
726 struct shader_glsl_priv
*priv
= device
->shader_priv
;
728 GLhandleARB programId
;
729 struct glsl_shader_prog_link
*prog
= priv
->glsl_program
;
730 UINT constant_version
;
734 /* No GLSL program set - nothing to do. */
737 programId
= prog
->programId
;
738 constant_version
= prog
->constant_version
;
740 if (useVertexShader
) {
741 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
743 /* Load DirectX 9 float constants/uniforms for vertex shader */
744 shader_glsl_load_constantsF(vshader
, gl_info
, stateBlock
->vertexShaderConstantF
,
745 prog
->vuniformF_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
747 /* Load DirectX 9 integer constants/uniforms for vertex shader */
748 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vuniformI_locations
, stateBlock
->vertexShaderConstantI
,
749 stateBlock
->changed
.vertexShaderConstantsI
& vshader
->baseShader
.reg_maps
.integer_constants
);
751 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
752 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, stateBlock
->vertexShaderConstantB
,
753 stateBlock
->changed
.vertexShaderConstantsB
& vshader
->baseShader
.reg_maps
.boolean_constants
);
755 /* Upload the position fixup params */
756 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &device
->posFixup
[0]));
757 checkGLcall("glUniform4fvARB");
760 if (usePixelShader
) {
762 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
764 /* Load DirectX 9 float constants/uniforms for pixel shader */
765 shader_glsl_load_constantsF(pshader
, gl_info
, stateBlock
->pixelShaderConstantF
,
766 prog
->puniformF_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
768 /* Load DirectX 9 integer constants/uniforms for pixel shader */
769 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->puniformI_locations
, stateBlock
->pixelShaderConstantI
,
770 stateBlock
->changed
.pixelShaderConstantsI
& pshader
->baseShader
.reg_maps
.integer_constants
);
772 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
773 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, stateBlock
->pixelShaderConstantB
,
774 stateBlock
->changed
.pixelShaderConstantsB
& pshader
->baseShader
.reg_maps
.boolean_constants
);
776 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
777 * It can't be 0 for a valid texbem instruction.
779 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
782 if(prog
->bumpenvmat_location
[i
] == -1) continue;
784 data
= (const float *)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVMAT00
];
785 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
[i
], 1, 0, data
));
786 checkGLcall("glUniformMatrix2fvARB");
788 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
789 * is set too, so we can check that in the needsbumpmat check
791 if(prog
->luminancescale_location
[i
] != -1) {
792 const GLfloat
*scale
= (const GLfloat
*)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVLSCALE
];
793 const GLfloat
*offset
= (const GLfloat
*)&stateBlock
->textureState
[i
][WINED3DTSS_BUMPENVLOFFSET
];
795 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
[i
], 1, scale
));
796 checkGLcall("glUniform1fvARB");
797 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
[i
], 1, offset
));
798 checkGLcall("glUniform1fvARB");
802 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
803 float correction_params
[4];
805 if (context
->render_offscreen
)
807 correction_params
[0] = 0.0f
;
808 correction_params
[1] = 1.0f
;
810 /* position is window relative, not viewport relative */
811 correction_params
[0] = context
->current_rt
->currentDesc
.Height
;
812 correction_params
[1] = -1.0f
;
814 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
818 if (priv
->next_constant_version
== UINT_MAX
)
820 TRACE("Max constant version reached, resetting to 0.\n");
821 wine_rb_for_each_entry(&priv
->program_lookup
, reset_program_constant_version
, NULL
);
822 priv
->next_constant_version
= 1;
826 prog
->constant_version
= priv
->next_constant_version
++;
830 static inline void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
,
831 unsigned int heap_idx
, DWORD new_version
)
833 struct constant_entry
*entries
= heap
->entries
;
834 unsigned int *positions
= heap
->positions
;
835 unsigned int parent_idx
;
839 parent_idx
= heap_idx
>> 1;
841 if (new_version
<= entries
[parent_idx
].version
) break;
843 entries
[heap_idx
] = entries
[parent_idx
];
844 positions
[entries
[parent_idx
].idx
] = heap_idx
;
845 heap_idx
= parent_idx
;
848 entries
[heap_idx
].version
= new_version
;
849 entries
[heap_idx
].idx
= idx
;
850 positions
[idx
] = heap_idx
;
853 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
855 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
856 struct shader_glsl_priv
*priv
= This
->shader_priv
;
857 struct constant_heap
*heap
= &priv
->vconst_heap
;
860 for (i
= start
; i
< count
+ start
; ++i
)
862 if (!This
->stateBlock
->changed
.vertexShaderConstantsF
[i
])
863 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
865 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
869 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
871 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
872 struct shader_glsl_priv
*priv
= This
->shader_priv
;
873 struct constant_heap
*heap
= &priv
->pconst_heap
;
876 for (i
= start
; i
< count
+ start
; ++i
)
878 if (!This
->stateBlock
->changed
.pixelShaderConstantsF
[i
])
879 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
881 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
885 static unsigned int vec4_varyings(DWORD shader_major
, const struct wined3d_gl_info
*gl_info
)
887 unsigned int ret
= gl_info
->limits
.glsl_varyings
/ 4;
888 /* 4.0 shaders do not write clip coords because d3d10 does not support user clipplanes */
889 if(shader_major
> 3) return ret
;
891 /* 3.0 shaders may need an extra varying for the clip coord on some cards(mostly dx10 ones) */
892 if (gl_info
->quirks
& WINED3D_QUIRK_GLSL_CLIP_VARYING
) ret
-= 1;
896 /** Generate the variable & register declarations for the GLSL output target */
897 static void shader_generate_glsl_declarations(const struct wined3d_context
*context
,
898 struct wined3d_shader_buffer
*buffer
, IWineD3DBaseShader
*iface
,
899 const shader_reg_maps
*reg_maps
, struct shader_glsl_ctx_priv
*ctx_priv
)
901 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
902 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
903 const struct ps_compile_args
*ps_args
= ctx_priv
->cur_ps_args
;
904 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
905 unsigned int i
, extra_constants_needed
= 0;
906 const local_constant
*lconst
;
909 /* There are some minor differences between pixel and vertex shaders */
910 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
911 char prefix
= pshader
? 'P' : 'V';
913 /* Prototype the subroutines */
914 for (i
= 0, map
= reg_maps
->labels
; map
; map
>>= 1, ++i
)
916 if (map
& 1) shader_addline(buffer
, "void subroutine%u();\n", i
);
919 /* Declare the constants (aka uniforms) */
920 if (This
->baseShader
.limits
.constant_float
> 0) {
921 unsigned max_constantsF
;
922 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
923 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
924 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
925 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
926 * a dx9 card, as long as it doesn't also use all the other constants.
928 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
929 * declare only the amount that we're assured to have.
931 * Thus we run into problems in these two cases:
932 * 1) The shader really uses more uniforms than supported
933 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
937 /* No indirect addressing here. */
938 max_constantsF
= gl_info
->limits
.glsl_ps_float_constants
;
942 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
943 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix).
944 * Subtract another uniform for immediate values, which have to be loaded via uniform by the driver as well.
945 * The shader code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex shader code, so one vec4 should be enough
946 * (Unfortunately the Nvidia driver doesn't store 128 and -128 in one float).
948 * Writing gl_ClipVertex requires one uniform for each clipplane as well.
950 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
- 3;
951 if(ctx_priv
->cur_vs_args
->clip_enabled
)
953 max_constantsF
-= gl_info
->limits
.clipplanes
;
955 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.integer_constants
);
956 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
957 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
958 * for now take this into account when calculating the number of available constants
960 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.boolean_constants
);
961 /* Set by driver quirks in directx.c */
962 max_constantsF
-= gl_info
->reserved_glsl_constants
;
966 max_constantsF
= gl_info
->limits
.glsl_vs_float_constants
;
969 max_constantsF
= min(This
->baseShader
.limits
.constant_float
, max_constantsF
);
970 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
973 /* Always declare the full set of constants, the compiler can remove the unused ones because d3d doesn't(yet)
974 * support indirect int and bool constant addressing. This avoids problems if the app uses e.g. i0 and i9.
976 if (This
->baseShader
.limits
.constant_int
> 0 && This
->baseShader
.reg_maps
.integer_constants
)
977 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
979 if (This
->baseShader
.limits
.constant_bool
> 0 && This
->baseShader
.reg_maps
.boolean_constants
)
980 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
983 shader_addline(buffer
, "uniform vec4 posFixup;\n");
984 /* Predeclaration; This function is added at link time based on the pixel shader.
985 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
986 * that. We know the input to the reorder function at vertex shader compile time, so
987 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
988 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
989 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
990 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
991 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
994 if (reg_maps
->shader_version
.major
>= 3)
996 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
998 shader_addline(buffer
, "void order_ps_input();\n");
1001 for (i
= 0, map
= reg_maps
->bumpmat
; map
; map
>>= 1, ++i
)
1003 if (!(map
& 1)) continue;
1005 shader_addline(buffer
, "uniform mat2 bumpenvmat%d;\n", i
);
1007 if (reg_maps
->luminanceparams
& (1 << i
))
1009 shader_addline(buffer
, "uniform float luminancescale%d;\n", i
);
1010 shader_addline(buffer
, "uniform float luminanceoffset%d;\n", i
);
1011 extra_constants_needed
++;
1014 extra_constants_needed
++;
1017 if (ps_args
->srgb_correction
)
1019 shader_addline(buffer
, "const vec4 srgb_const0 = vec4(%.8e, %.8e, %.8e, %.8e);\n",
1020 srgb_pow
, srgb_mul_high
, srgb_sub_high
, srgb_mul_low
);
1021 shader_addline(buffer
, "const vec4 srgb_const1 = vec4(%.8e, 0.0, 0.0, 0.0);\n",
1024 if (reg_maps
->vpos
|| reg_maps
->usesdsy
)
1026 if (This
->baseShader
.limits
.constant_float
+ extra_constants_needed
1027 + 1 < gl_info
->limits
.glsl_ps_float_constants
)
1029 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
1030 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
1031 extra_constants_needed
++;
1033 /* This happens because we do not have proper tracking of the constant registers that are
1034 * actually used, only the max limit of the shader version
1036 FIXME("Cannot find a free uniform for vpos correction params\n");
1037 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
1038 context
->render_offscreen
? 0.0f
: device
->render_targets
[0]->currentDesc
.Height
,
1039 context
->render_offscreen
? 1.0f
: -1.0f
);
1041 shader_addline(buffer
, "vec4 vpos;\n");
1045 /* Declare texture samplers */
1046 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
1047 if (reg_maps
->sampler_type
[i
])
1049 switch (reg_maps
->sampler_type
[i
])
1052 if (pshader
&& ps_args
->shadow
& (1 << i
))
1053 shader_addline(buffer
, "uniform sampler1DShadow %csampler%u;\n", prefix
, i
);
1055 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
1058 if (pshader
&& ps_args
->shadow
& (1 << i
))
1060 if (device
->stateBlock
->textures
[i
]
1061 && IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
])
1062 == GL_TEXTURE_RECTANGLE_ARB
)
1063 shader_addline(buffer
, "uniform sampler2DRectShadow %csampler%u;\n", prefix
, i
);
1065 shader_addline(buffer
, "uniform sampler2DShadow %csampler%u;\n", prefix
, i
);
1069 if (device
->stateBlock
->textures
[i
]
1070 && IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
])
1071 == GL_TEXTURE_RECTANGLE_ARB
)
1072 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
1074 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
1077 case WINED3DSTT_CUBE
:
1078 if (pshader
&& ps_args
->shadow
& (1 << i
)) FIXME("Unsupported Cube shadow sampler.\n");
1079 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
1081 case WINED3DSTT_VOLUME
:
1082 if (pshader
&& ps_args
->shadow
& (1 << i
)) FIXME("Unsupported 3D shadow sampler.\n");
1083 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
1086 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
1087 FIXME("Unrecognized sampler type: %#x\n", reg_maps
->sampler_type
[i
]);
1093 /* Declare uniforms for NP2 texcoord fixup:
1094 * This is NOT done inside the loop that declares the texture samplers since the NP2 fixup code
1095 * is currently only used for the GeforceFX series and when forcing the ARB_npot extension off.
1096 * Modern cards just skip the code anyway, so put it inside a separate loop. */
1097 if (pshader
&& ps_args
->np2_fixup
) {
1099 struct ps_np2fixup_info
* const fixup
= ctx_priv
->cur_np2fixup_info
;
1102 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
1103 * while D3D has them in the (normalized) [0,1]x[0,1] range.
1104 * samplerNP2Fixup stores texture dimensions and is updated through
1105 * shader_glsl_load_np2fixup_constants when the sampler changes. */
1107 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; ++i
) {
1108 if (reg_maps
->sampler_type
[i
]) {
1109 if (!(ps_args
->np2_fixup
& (1 << i
))) continue;
1111 if (WINED3DSTT_2D
!= reg_maps
->sampler_type
[i
]) {
1112 FIXME("Non-2D texture is flagged for NP2 texcoord fixup.\n");
1116 fixup
->idx
[i
] = cur
++;
1120 fixup
->num_consts
= (cur
+ 1) >> 1;
1121 shader_addline(buffer
, "uniform vec4 %csamplerNP2Fixup[%u];\n", prefix
, fixup
->num_consts
);
1124 /* Declare address variables */
1125 for (i
= 0, map
= reg_maps
->address
; map
; map
>>= 1, ++i
)
1127 if (map
& 1) shader_addline(buffer
, "ivec4 A%u;\n", i
);
1130 /* Declare texture coordinate temporaries and initialize them */
1131 for (i
= 0, map
= reg_maps
->texcoord
; map
; map
>>= 1, ++i
)
1133 if (map
& 1) shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
1136 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
1137 * helper function shader that is linked in at link time
1139 if (pshader
&& reg_maps
->shader_version
.major
>= 3)
1141 if (use_vs(device
->stateBlock
))
1143 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
1145 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
1146 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
1147 * pixel shader that reads the fixed function color into the packed input registers.
1149 shader_addline(buffer
, "vec4 IN[%u];\n", vec4_varyings(reg_maps
->shader_version
.major
, gl_info
));
1153 /* Declare output register temporaries */
1154 if(This
->baseShader
.limits
.packed_output
) {
1155 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
1158 /* Declare temporary variables */
1159 for (i
= 0, map
= reg_maps
->temporary
; map
; map
>>= 1, ++i
)
1161 if (map
& 1) shader_addline(buffer
, "vec4 R%u;\n", i
);
1164 /* Declare attributes */
1165 if (reg_maps
->shader_version
.type
== WINED3D_SHADER_TYPE_VERTEX
)
1167 for (i
= 0, map
= reg_maps
->input_registers
; map
; map
>>= 1, ++i
)
1169 if (map
& 1) shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
1173 /* Declare loop registers aLx */
1174 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
1175 shader_addline(buffer
, "int aL%u;\n", i
);
1176 shader_addline(buffer
, "int tmpInt%u;\n", i
);
1179 /* Temporary variables for matrix operations */
1180 shader_addline(buffer
, "vec4 tmp0;\n");
1181 shader_addline(buffer
, "vec4 tmp1;\n");
1183 /* Local constants use a different name so they can be loaded once at shader link time
1184 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
1185 * float -> string conversion can cause precision loss.
1187 if(!This
->baseShader
.load_local_constsF
) {
1188 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
1189 shader_addline(buffer
, "uniform vec4 %cLC%u;\n", prefix
, lconst
->idx
);
1193 shader_addline(buffer
, "const float FLT_MAX = 1e38;\n");
1195 /* Start the main program */
1196 shader_addline(buffer
, "void main() {\n");
1197 if(pshader
&& reg_maps
->vpos
) {
1198 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
1199 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
1200 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
1201 * precision troubles when we just substract 0.5.
1203 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
1205 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
1207 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
1208 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
1209 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
1210 * correctly on drivers that returns integer values.
1212 shader_addline(buffer
, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
1216 /*****************************************************************************
1217 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
1219 * For more information, see http://wiki.winehq.org/DirectX-Shaders
1220 ****************************************************************************/
1223 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1224 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
);
1226 /** Used for opcode modifiers - They multiply the result by the specified amount */
1227 static const char * const shift_glsl_tab
[] = {
1229 "2.0 * ", /* 1 (x2) */
1230 "4.0 * ", /* 2 (x4) */
1231 "8.0 * ", /* 3 (x8) */
1232 "16.0 * ", /* 4 (x16) */
1233 "32.0 * ", /* 5 (x32) */
1240 "0.0625 * ", /* 12 (d16) */
1241 "0.125 * ", /* 13 (d8) */
1242 "0.25 * ", /* 14 (d4) */
1243 "0.5 * " /* 15 (d2) */
1246 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1247 static void shader_glsl_gen_modifier(DWORD src_modifier
, const char *in_reg
, const char *in_regswizzle
, char *out_str
)
1251 switch (src_modifier
)
1253 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
1254 case WINED3DSPSM_DW
:
1255 case WINED3DSPSM_NONE
:
1256 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1258 case WINED3DSPSM_NEG
:
1259 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
1261 case WINED3DSPSM_NOT
:
1262 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
1264 case WINED3DSPSM_BIAS
:
1265 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1267 case WINED3DSPSM_BIASNEG
:
1268 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1270 case WINED3DSPSM_SIGN
:
1271 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1273 case WINED3DSPSM_SIGNNEG
:
1274 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1276 case WINED3DSPSM_COMP
:
1277 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
1279 case WINED3DSPSM_X2
:
1280 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
1282 case WINED3DSPSM_X2NEG
:
1283 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
1285 case WINED3DSPSM_ABS
:
1286 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
1288 case WINED3DSPSM_ABSNEG
:
1289 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
1292 FIXME("Unhandled modifier %u\n", src_modifier
);
1293 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1297 /** Writes the GLSL variable name that corresponds to the register that the
1298 * DX opcode parameter is trying to access */
1299 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
1300 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
1302 /* oPos, oFog and oPts in D3D */
1303 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1305 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1306 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
1307 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
1313 case WINED3DSPR_TEMP
:
1314 sprintf(register_name
, "R%u", reg
->idx
);
1317 case WINED3DSPR_INPUT
:
1318 /* vertex shaders */
1321 struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
1322 if (priv
->cur_vs_args
->swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1323 sprintf(register_name
, "attrib%u", reg
->idx
);
1327 /* pixel shaders >= 3.0 */
1328 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3)
1330 DWORD idx
= ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
->idx
];
1331 unsigned int in_count
= vec4_varyings(This
->baseShader
.reg_maps
.shader_version
.major
, gl_info
);
1335 glsl_src_param_t rel_param
;
1337 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1339 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1340 * operation there */
1343 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1345 sprintf(register_name
,
1346 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1347 rel_param
.param_str
, idx
, in_count
- 1, rel_param
.param_str
, idx
, in_count
,
1348 rel_param
.param_str
, idx
);
1352 sprintf(register_name
, "IN[%s + %u]", rel_param
.param_str
, idx
);
1357 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1359 sprintf(register_name
, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1360 rel_param
.param_str
, in_count
- 1, rel_param
.param_str
, in_count
,
1361 rel_param
.param_str
);
1365 sprintf(register_name
, "IN[%s]", rel_param
.param_str
);
1371 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
1372 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
1373 else sprintf(register_name
, "IN[%u]", idx
);
1378 if (reg
->idx
== 0) strcpy(register_name
, "gl_Color");
1379 else strcpy(register_name
, "gl_SecondaryColor");
1384 case WINED3DSPR_CONST
:
1386 const char prefix
= pshader
? 'P' : 'V';
1388 /* Relative addressing */
1391 glsl_src_param_t rel_param
;
1392 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1393 if (reg
->idx
) sprintf(register_name
, "%cC[%s + %u]", prefix
, rel_param
.param_str
, reg
->idx
);
1394 else sprintf(register_name
, "%cC[%s]", prefix
, rel_param
.param_str
);
1398 if (shader_constant_is_local(This
, reg
->idx
))
1399 sprintf(register_name
, "%cLC%u", prefix
, reg
->idx
);
1401 sprintf(register_name
, "%cC[%u]", prefix
, reg
->idx
);
1406 case WINED3DSPR_CONSTINT
:
1407 if (pshader
) sprintf(register_name
, "PI[%u]", reg
->idx
);
1408 else sprintf(register_name
, "VI[%u]", reg
->idx
);
1411 case WINED3DSPR_CONSTBOOL
:
1412 if (pshader
) sprintf(register_name
, "PB[%u]", reg
->idx
);
1413 else sprintf(register_name
, "VB[%u]", reg
->idx
);
1416 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1417 if (pshader
) sprintf(register_name
, "T%u", reg
->idx
);
1418 else sprintf(register_name
, "A%u", reg
->idx
);
1421 case WINED3DSPR_LOOP
:
1422 sprintf(register_name
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
1425 case WINED3DSPR_SAMPLER
:
1426 if (pshader
) sprintf(register_name
, "Psampler%u", reg
->idx
);
1427 else sprintf(register_name
, "Vsampler%u", reg
->idx
);
1430 case WINED3DSPR_COLOROUT
:
1431 if (reg
->idx
>= gl_info
->limits
.buffers
)
1432 WARN("Write to render target %u, only %d supported.\n", reg
->idx
, gl_info
->limits
.buffers
);
1434 sprintf(register_name
, "gl_FragData[%u]", reg
->idx
);
1437 case WINED3DSPR_RASTOUT
:
1438 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
]);
1441 case WINED3DSPR_DEPTHOUT
:
1442 sprintf(register_name
, "gl_FragDepth");
1445 case WINED3DSPR_ATTROUT
:
1446 if (reg
->idx
== 0) sprintf(register_name
, "gl_FrontColor");
1447 else sprintf(register_name
, "gl_FrontSecondaryColor");
1450 case WINED3DSPR_TEXCRDOUT
:
1451 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1452 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3) sprintf(register_name
, "OUT[%u]", reg
->idx
);
1453 else sprintf(register_name
, "gl_TexCoord[%u]", reg
->idx
);
1456 case WINED3DSPR_MISCTYPE
:
1460 sprintf(register_name
, "vpos");
1462 else if (reg
->idx
== 1)
1464 /* Note that gl_FrontFacing is a bool, while vFace is
1465 * a float for which the sign determines front/back */
1466 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
1470 FIXME("Unhandled misctype register %d\n", reg
->idx
);
1471 sprintf(register_name
, "unrecognized_register");
1475 case WINED3DSPR_IMMCONST
:
1476 switch (reg
->immconst_type
)
1478 case WINED3D_IMMCONST_FLOAT
:
1479 sprintf(register_name
, "%.8e", *(const float *)reg
->immconst_data
);
1482 case WINED3D_IMMCONST_FLOAT4
:
1483 sprintf(register_name
, "vec4(%.8e, %.8e, %.8e, %.8e)",
1484 *(const float *)®
->immconst_data
[0], *(const float *)®
->immconst_data
[1],
1485 *(const float *)®
->immconst_data
[2], *(const float *)®
->immconst_data
[3]);
1489 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
1490 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
1495 FIXME("Unhandled register name Type(%d)\n", reg
->type
);
1496 sprintf(register_name
, "unrecognized_register");
1501 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
1504 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
1505 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
1506 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
1507 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
1511 /* Get the GLSL write mask for the destination register */
1512 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
1514 DWORD mask
= param
->write_mask
;
1516 if (shader_is_scalar(¶m
->reg
))
1518 mask
= WINED3DSP_WRITEMASK_0
;
1523 shader_glsl_write_mask_to_str(mask
, write_mask
);
1529 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
1530 unsigned int size
= 0;
1532 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
1533 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
1534 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
1535 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
1540 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
1542 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1543 * but addressed as "rgba". To fix this we need to swap the register's x
1544 * and z components. */
1545 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1548 /* swizzle bits fields: wwzzyyxx */
1549 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
1550 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1551 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1552 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1556 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
1557 BOOL fixup
, DWORD mask
, char *swizzle_str
)
1559 if (shader_is_scalar(¶m
->reg
))
1560 *swizzle_str
= '\0';
1562 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
1565 /* From a given parameter token, generate the corresponding GLSL string.
1566 * Also, return the actual register name and swizzle in case the
1567 * caller needs this information as well. */
1568 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1569 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
)
1571 BOOL is_color
= FALSE
;
1572 char swizzle_str
[6];
1574 glsl_src
->reg_name
[0] = '\0';
1575 glsl_src
->param_str
[0] = '\0';
1576 swizzle_str
[0] = '\0';
1578 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
1579 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
1580 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
1583 /* From a given parameter token, generate the corresponding GLSL string.
1584 * Also, return the actual register name and swizzle in case the
1585 * caller needs this information as well. */
1586 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
1587 const struct wined3d_shader_dst_param
*wined3d_dst
, glsl_dst_param_t
*glsl_dst
)
1589 BOOL is_color
= FALSE
;
1591 glsl_dst
->mask_str
[0] = '\0';
1592 glsl_dst
->reg_name
[0] = '\0';
1594 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
1595 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
1598 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1599 static DWORD
shader_glsl_append_dst_ext(struct wined3d_shader_buffer
*buffer
,
1600 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
)
1602 glsl_dst_param_t glsl_dst
;
1605 mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
);
1606 if (mask
) shader_addline(buffer
, "%s%s = %s(", glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
1611 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1612 static DWORD
shader_glsl_append_dst(struct wined3d_shader_buffer
*buffer
, const struct wined3d_shader_instruction
*ins
)
1614 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0]);
1617 /** Process GLSL instruction modifiers */
1618 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
1620 glsl_dst_param_t dst_param
;
1623 if (!ins
->dst_count
) return;
1625 modifiers
= ins
->dst
[0].modifiers
;
1626 if (!modifiers
) return;
1628 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
1630 if (modifiers
& WINED3DSPDM_SATURATE
)
1632 /* _SAT means to clamp the value of the register to between 0 and 1 */
1633 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1634 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1637 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
1639 FIXME("_centroid modifier not handled\n");
1642 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
1644 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1648 static inline const char *shader_get_comp_op(DWORD op
)
1651 case COMPARISON_GT
: return ">";
1652 case COMPARISON_EQ
: return "==";
1653 case COMPARISON_GE
: return ">=";
1654 case COMPARISON_LT
: return "<";
1655 case COMPARISON_NE
: return "!=";
1656 case COMPARISON_LE
: return "<=";
1658 FIXME("Unrecognized comparison value: %u\n", op
);
1663 static void shader_glsl_get_sample_function(const struct wined3d_shader_context
*ctx
,
1664 DWORD sampler_idx
, DWORD flags
, glsl_sample_function_t
*sample_function
)
1666 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ctx
->reg_maps
->sampler_type
[sampler_idx
];
1667 const struct wined3d_gl_info
*gl_info
= ctx
->gl_info
;
1668 BOOL shadow
= shader_is_pshader_version(ctx
->reg_maps
->shader_version
.type
)
1669 && (((const struct shader_glsl_ctx_priv
*)ctx
->backend_data
)->cur_ps_args
->shadow
& (1 << sampler_idx
));
1670 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
1671 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_RECT
;
1672 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
1673 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
1675 /* Note that there's no such thing as a projected cube texture. */
1676 switch(sampler_type
) {
1682 sample_function
->name
= projected
? "shadow1DProjLod" : "shadow1DLod";
1686 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1687 sample_function
->name
= projected
? "shadow1DProjGrad" : "shadow1DGrad";
1688 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1689 sample_function
->name
= projected
? "shadow1DProjGradARB" : "shadow1DGradARB";
1692 FIXME("Unsupported 1D shadow grad function.\n");
1693 sample_function
->name
= "unsupported1DGrad";
1698 sample_function
->name
= projected
? "shadow1DProj" : "shadow1D";
1700 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1706 sample_function
->name
= projected
? "texture1DProjLod" : "texture1DLod";
1710 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1711 sample_function
->name
= projected
? "texture1DProjGrad" : "texture1DGrad";
1712 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1713 sample_function
->name
= projected
? "texture1DProjGradARB" : "texture1DGradARB";
1716 FIXME("Unsupported 1D grad function.\n");
1717 sample_function
->name
= "unsupported1DGrad";
1722 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1724 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1735 sample_function
->name
= projected
? "shadow2DRectProjLod" : "shadow2DRectLod";
1739 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1740 sample_function
->name
= projected
? "shadow2DRectProjGrad" : "shadow2DRectGrad";
1741 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1742 sample_function
->name
= projected
? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1745 FIXME("Unsupported RECT shadow grad function.\n");
1746 sample_function
->name
= "unsupported2DRectGrad";
1751 sample_function
->name
= projected
? "shadow2DRectProj" : "shadow2DRect";
1758 sample_function
->name
= projected
? "shadow2DProjLod" : "shadow2DLod";
1762 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1763 sample_function
->name
= projected
? "shadow2DProjGrad" : "shadow2DGrad";
1764 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1765 sample_function
->name
= projected
? "shadow2DProjGradARB" : "shadow2DGradARB";
1768 FIXME("Unsupported 2D shadow grad function.\n");
1769 sample_function
->name
= "unsupported2DGrad";
1774 sample_function
->name
= projected
? "shadow2DProj" : "shadow2D";
1777 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1785 sample_function
->name
= projected
? "texture2DRectProjLod" : "texture2DRectLod";
1789 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1790 sample_function
->name
= projected
? "texture2DRectProjGrad" : "texture2DRectGrad";
1791 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1792 sample_function
->name
= projected
? "texture2DRectProjGradARB" : "texture2DRectGradARB";
1795 FIXME("Unsupported RECT grad function.\n");
1796 sample_function
->name
= "unsupported2DRectGrad";
1801 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1808 sample_function
->name
= projected
? "texture2DProjLod" : "texture2DLod";
1812 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1813 sample_function
->name
= projected
? "texture2DProjGrad" : "texture2DGrad";
1814 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1815 sample_function
->name
= projected
? "texture2DProjGradARB" : "texture2DGradARB";
1818 FIXME("Unsupported 2D grad function.\n");
1819 sample_function
->name
= "unsupported2DGrad";
1824 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1827 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1831 case WINED3DSTT_CUBE
:
1834 FIXME("Unsupported Cube shadow function.\n ");
1835 sample_function
->name
= "unsupportedCubeShadow";
1836 sample_function
->coord_mask
= 0;
1842 sample_function
->name
= "textureCubeLod";
1846 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1847 sample_function
->name
= "textureCubeGrad";
1848 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1849 sample_function
->name
= "textureCubeGradARB";
1852 FIXME("Unsupported Cube grad function.\n");
1853 sample_function
->name
= "unsupportedCubeGrad";
1858 sample_function
->name
= "textureCube";
1860 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1864 case WINED3DSTT_VOLUME
:
1867 FIXME("Unsupported 3D shadow function.\n ");
1868 sample_function
->name
= "unsupported3DShadow";
1869 sample_function
->coord_mask
= 0;
1875 sample_function
->name
= projected
? "texture3DProjLod" : "texture3DLod";
1879 if (gl_info
->supported
[EXT_GPU_SHADER4
])
1880 sample_function
->name
= projected
? "texture3DProjGrad" : "texture3DGrad";
1881 else if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
])
1882 sample_function
->name
= projected
? "texture3DProjGradARB" : "texture3DGradARB";
1885 FIXME("Unsupported 3D grad function.\n");
1886 sample_function
->name
= "unsupported3DGrad";
1891 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1893 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1898 sample_function
->name
= "";
1899 sample_function
->coord_mask
= 0;
1900 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1905 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
1906 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
1908 switch(channel_source
)
1910 case CHANNEL_SOURCE_ZERO
:
1911 strcat(arguments
, "0.0");
1914 case CHANNEL_SOURCE_ONE
:
1915 strcat(arguments
, "1.0");
1918 case CHANNEL_SOURCE_X
:
1919 strcat(arguments
, reg_name
);
1920 strcat(arguments
, ".x");
1923 case CHANNEL_SOURCE_Y
:
1924 strcat(arguments
, reg_name
);
1925 strcat(arguments
, ".y");
1928 case CHANNEL_SOURCE_Z
:
1929 strcat(arguments
, reg_name
);
1930 strcat(arguments
, ".z");
1933 case CHANNEL_SOURCE_W
:
1934 strcat(arguments
, reg_name
);
1935 strcat(arguments
, ".w");
1939 FIXME("Unhandled channel source %#x\n", channel_source
);
1940 strcat(arguments
, "undefined");
1944 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
1947 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
1949 struct wined3d_shader_dst_param dst
;
1950 unsigned int mask_size
, remaining
;
1951 glsl_dst_param_t dst_param
;
1952 char arguments
[256];
1956 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1957 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1958 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1959 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1960 mask
&= ins
->dst
[0].write_mask
;
1962 if (!mask
) return; /* Nothing to do */
1964 if (is_complex_fixup(fixup
))
1966 enum complex_fixup complex_fixup
= get_complex_fixup(fixup
);
1967 FIXME("Complex fixup (%#x) not supported\n",complex_fixup
);
1971 mask_size
= shader_glsl_get_write_mask_size(mask
);
1974 dst
.write_mask
= mask
;
1975 shader_glsl_add_dst_param(ins
, &dst
, &dst_param
);
1977 arguments
[0] = '\0';
1978 remaining
= mask_size
;
1979 if (mask
& WINED3DSP_WRITEMASK_0
)
1981 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
1982 if (--remaining
) strcat(arguments
, ", ");
1984 if (mask
& WINED3DSP_WRITEMASK_1
)
1986 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
1987 if (--remaining
) strcat(arguments
, ", ");
1989 if (mask
& WINED3DSP_WRITEMASK_2
)
1991 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
1992 if (--remaining
) strcat(arguments
, ", ");
1994 if (mask
& WINED3DSP_WRITEMASK_3
)
1996 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
1997 if (--remaining
) strcat(arguments
, ", ");
2002 shader_addline(ins
->ctx
->buffer
, "%s%s = vec%u(%s);\n",
2003 dst_param
.reg_name
, dst_param
.mask_str
, mask_size
, arguments
);
2007 shader_addline(ins
->ctx
->buffer
, "%s%s = %s;\n", dst_param
.reg_name
, dst_param
.mask_str
, arguments
);
2011 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
2012 DWORD sampler
, const glsl_sample_function_t
*sample_function
, DWORD swizzle
,
2013 const char *dx
, const char *dy
,
2014 const char *bias
, const char *coord_reg_fmt
, ...)
2016 const char *sampler_base
;
2017 char dst_swizzle
[6];
2018 struct color_fixup_desc fixup
;
2019 BOOL np2_fixup
= FALSE
;
2022 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
2024 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
2026 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2027 fixup
= priv
->cur_ps_args
->color_fixup
[sampler
];
2028 sampler_base
= "Psampler";
2030 if(priv
->cur_ps_args
->np2_fixup
& (1 << sampler
)) {
2032 FIXME("Biased sampling from NP2 textures is unsupported\n");
2038 sampler_base
= "Vsampler";
2039 fixup
= COLOR_FIXUP_IDENTITY
; /* FIXME: Vshader color fixup */
2042 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2044 shader_addline(ins
->ctx
->buffer
, "%s(%s%u, ", sample_function
->name
, sampler_base
, sampler
);
2046 va_start(args
, coord_reg_fmt
);
2047 shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
2051 shader_addline(ins
->ctx
->buffer
, ", %s)%s);\n", bias
, dst_swizzle
);
2054 const struct shader_glsl_ctx_priv
*priv
= ins
->ctx
->backend_data
;
2055 const unsigned char idx
= priv
->cur_np2fixup_info
->idx
[sampler
];
2057 shader_addline(ins
->ctx
->buffer
, " * PsamplerNP2Fixup[%u].%s)%s);\n", idx
>> 1,
2058 (idx
% 2) ? "zw" : "xy", dst_swizzle
);
2059 } else if(dx
&& dy
) {
2060 shader_addline(ins
->ctx
->buffer
, ", %s, %s)%s);\n", dx
, dy
, dst_swizzle
);
2062 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
2066 if(!is_identity_fixup(fixup
)) {
2067 shader_glsl_color_correction(ins
, fixup
);
2071 /*****************************************************************************
2072 * Begin processing individual instruction opcodes
2073 ****************************************************************************/
2075 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
2076 static void shader_glsl_arith(const struct wined3d_shader_instruction
*ins
)
2078 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2079 glsl_src_param_t src0_param
;
2080 glsl_src_param_t src1_param
;
2084 /* Determine the GLSL operator to use based on the opcode */
2085 switch (ins
->handler_idx
)
2087 case WINED3DSIH_MUL
: op
= '*'; break;
2088 case WINED3DSIH_ADD
: op
= '+'; break;
2089 case WINED3DSIH_SUB
: op
= '-'; break;
2092 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2096 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2097 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2098 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2099 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
2102 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
2103 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
2105 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
2106 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2107 glsl_src_param_t src0_param
;
2110 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2111 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2113 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
2114 * shader versions WINED3DSIO_MOVA is used for this. */
2115 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
2116 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
2117 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
2119 /* This is a simple floor() */
2120 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2121 if (mask_size
> 1) {
2122 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
2124 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
2127 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
2129 /* We need to *round* to the nearest int here. */
2130 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2132 if (gl_info
->supported
[EXT_GPU_SHADER4
])
2135 shader_addline(buffer
, "ivec%d(round(%s)));\n", mask_size
, src0_param
.param_str
);
2137 shader_addline(buffer
, "int(round(%s)));\n", src0_param
.param_str
);
2142 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n",
2143 mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
2145 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n",
2146 src0_param
.param_str
, src0_param
.param_str
);
2151 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
2155 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
2156 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
2158 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2159 glsl_src_param_t src0_param
;
2160 glsl_src_param_t src1_param
;
2161 DWORD dst_write_mask
, src_write_mask
;
2162 unsigned int dst_size
= 0;
2164 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2165 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2167 /* dp3 works on vec3, dp4 on vec4 */
2168 if (ins
->handler_idx
== WINED3DSIH_DP4
)
2170 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
2172 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2175 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
2176 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
2179 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
2181 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
2185 /* Note that this instruction has some restrictions. The destination write mask
2186 * can't contain the w component, and the source swizzles have to be .xyzw */
2187 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
2189 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2190 glsl_src_param_t src0_param
;
2191 glsl_src_param_t src1_param
;
2194 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2195 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2196 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2197 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
2198 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
2201 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
2202 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
2203 * GLSL uses the value as-is. */
2204 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
2206 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2207 glsl_src_param_t src0_param
;
2208 glsl_src_param_t src1_param
;
2209 DWORD dst_write_mask
;
2210 unsigned int dst_size
;
2212 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2213 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2215 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2216 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2219 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
2221 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
2225 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
2226 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
2227 * GLSL uses the value as-is. */
2228 static void shader_glsl_log(const struct wined3d_shader_instruction
*ins
)
2230 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2231 glsl_src_param_t src0_param
;
2232 DWORD dst_write_mask
;
2233 unsigned int dst_size
;
2235 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
2236 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
2238 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2242 shader_addline(buffer
, "vec%d(%s == 0.0 ? -FLT_MAX : log2(abs(%s))));\n",
2243 dst_size
, src0_param
.param_str
, src0_param
.param_str
);
2247 shader_addline(buffer
, "%s == 0.0 ? -FLT_MAX : log2(abs(%s)));\n",
2248 src0_param
.param_str
, src0_param
.param_str
);
2252 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
2253 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
2255 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2256 glsl_src_param_t src_param
;
2257 const char *instruction
;
2261 /* Determine the GLSL function to use based on the opcode */
2262 /* TODO: Possibly make this a table for faster lookups */
2263 switch (ins
->handler_idx
)
2265 case WINED3DSIH_MIN
: instruction
= "min"; break;
2266 case WINED3DSIH_MAX
: instruction
= "max"; break;
2267 case WINED3DSIH_ABS
: instruction
= "abs"; break;
2268 case WINED3DSIH_FRC
: instruction
= "fract"; break;
2269 case WINED3DSIH_EXP
: instruction
= "exp2"; break;
2270 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
2271 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
2272 default: instruction
= "";
2273 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
2277 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2279 shader_addline(buffer
, "%s(", instruction
);
2283 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
2284 shader_addline(buffer
, "%s", src_param
.param_str
);
2285 for (i
= 1; i
< ins
->src_count
; ++i
)
2287 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
2288 shader_addline(buffer
, ", %s", src_param
.param_str
);
2292 shader_addline(buffer
, "));\n");
2295 static void shader_glsl_nrm(const struct wined3d_shader_instruction
*ins
)
2297 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2298 glsl_src_param_t src_param
;
2299 unsigned int mask_size
;
2303 write_mask
= shader_glsl_get_write_mask(ins
->dst
, dst_mask
);
2304 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2305 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
2307 shader_addline(buffer
, "tmp0.x = length(%s);\n", src_param
.param_str
);
2308 shader_glsl_append_dst(buffer
, ins
);
2311 shader_addline(buffer
, "tmp0.x == 0.0 ? vec%u(0.0) : (%s / tmp0.x));\n",
2312 mask_size
, src_param
.param_str
);
2316 shader_addline(buffer
, "tmp0.x == 0.0 ? 0.0 : (%s / tmp0.x));\n",
2317 src_param
.param_str
);
2321 /** Process the WINED3DSIO_EXPP instruction in GLSL:
2322 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
2323 * dst.x = 2^(floor(src))
2324 * dst.y = src - floor(src)
2325 * dst.z = 2^src (partial precision is allowed, but optional)
2327 * For 2.0 shaders, just do this (honoring writemask and swizzle):
2328 * dst = 2^src; (partial precision is allowed, but optional)
2330 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
2332 glsl_src_param_t src_param
;
2334 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
2336 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
2340 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
2341 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
2342 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
2343 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
2345 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2346 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2347 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
2350 unsigned int mask_size
;
2352 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2353 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2355 if (mask_size
> 1) {
2356 shader_addline(ins
->ctx
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
2358 shader_addline(ins
->ctx
->buffer
, "exp2(%s));\n", src_param
.param_str
);
2363 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
2364 static void shader_glsl_rcp(const struct wined3d_shader_instruction
*ins
)
2366 glsl_src_param_t src_param
;
2368 unsigned int mask_size
;
2370 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2371 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2372 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
2376 shader_addline(ins
->ctx
->buffer
, "vec%d(%s == 0.0 ? FLT_MAX : 1.0 / %s));\n",
2377 mask_size
, src_param
.param_str
, src_param
.param_str
);
2381 shader_addline(ins
->ctx
->buffer
, "%s == 0.0 ? FLT_MAX : 1.0 / %s);\n",
2382 src_param
.param_str
, src_param
.param_str
);
2386 static void shader_glsl_rsq(const struct wined3d_shader_instruction
*ins
)
2388 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
2389 glsl_src_param_t src_param
;
2391 unsigned int mask_size
;
2393 write_mask
= shader_glsl_append_dst(buffer
, ins
);
2394 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2396 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
2400 shader_addline(buffer
, "vec%d(%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s))));\n",
2401 mask_size
, src_param
.param_str
, src_param
.param_str
);
2405 shader_addline(buffer
, "%s == 0.0 ? FLT_MAX : inversesqrt(abs(%s)));\n",
2406 src_param
.param_str
, src_param
.param_str
);
2410 /** Process signed comparison opcodes in GLSL. */
2411 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
2413 glsl_src_param_t src0_param
;
2414 glsl_src_param_t src1_param
;
2416 unsigned int mask_size
;
2418 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2419 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2420 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2421 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2423 if (mask_size
> 1) {
2424 const char *compare
;
2426 switch(ins
->handler_idx
)
2428 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
2429 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
2430 default: compare
= "";
2431 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2434 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
2435 src0_param
.param_str
, src1_param
.param_str
);
2437 switch(ins
->handler_idx
)
2439 case WINED3DSIH_SLT
:
2440 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
2441 * to return 0.0 but step returns 1.0 because step is not < x
2442 * An alternative is a bvec compare padded with an unused second component.
2443 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
2444 * issue. Playing with not() is not possible either because not() does not accept
2447 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
2448 src0_param
.param_str
, src1_param
.param_str
);
2450 case WINED3DSIH_SGE
:
2451 /* Here we can use the step() function and safe a conditional */
2452 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
2455 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
2461 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
2462 static void shader_glsl_cmp(const struct wined3d_shader_instruction
*ins
)
2464 glsl_src_param_t src0_param
;
2465 glsl_src_param_t src1_param
;
2466 glsl_src_param_t src2_param
;
2467 DWORD write_mask
, cmp_channel
= 0;
2470 BOOL temp_destination
= FALSE
;
2472 if (shader_is_scalar(&ins
->src
[0].reg
))
2474 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2476 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2477 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2478 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2480 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2481 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2483 DWORD dst_mask
= ins
->dst
[0].write_mask
;
2484 struct wined3d_shader_dst_param dst
= ins
->dst
[0];
2486 /* Cycle through all source0 channels */
2487 for (i
=0; i
<4; i
++) {
2489 /* Find the destination channels which use the current source0 channel */
2490 for (j
=0; j
<4; j
++) {
2491 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2493 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2494 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2497 dst
.write_mask
= dst_mask
& write_mask
;
2499 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2500 * The first lines may overwrite source parameters of the following lines.
2501 * Deal with that by using a temporary destination register if needed
2503 if ((ins
->src
[0].reg
.idx
== ins
->dst
[0].reg
.idx
2504 && ins
->src
[0].reg
.type
== ins
->dst
[0].reg
.type
)
2505 || (ins
->src
[1].reg
.idx
== ins
->dst
[0].reg
.idx
2506 && ins
->src
[1].reg
.type
== ins
->dst
[0].reg
.type
)
2507 || (ins
->src
[2].reg
.idx
== ins
->dst
[0].reg
.idx
2508 && ins
->src
[2].reg
.type
== ins
->dst
[0].reg
.type
))
2510 write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
);
2511 if (!write_mask
) continue;
2512 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
2513 temp_destination
= TRUE
;
2515 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2516 if (!write_mask
) continue;
2519 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2520 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2521 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2523 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2524 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2527 if(temp_destination
) {
2528 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
2529 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2530 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
2536 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2537 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2538 * the compare is done per component of src0. */
2539 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
2541 struct wined3d_shader_dst_param dst
;
2542 glsl_src_param_t src0_param
;
2543 glsl_src_param_t src1_param
;
2544 glsl_src_param_t src2_param
;
2545 DWORD write_mask
, cmp_channel
= 0;
2548 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2549 ins
->ctx
->reg_maps
->shader_version
.minor
);
2551 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
2553 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2554 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2555 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2556 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2558 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2561 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
2563 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2564 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2568 /* Cycle through all source0 channels */
2569 dst_mask
= ins
->dst
[0].write_mask
;
2571 for (i
=0; i
<4; i
++) {
2573 /* Find the destination channels which use the current source0 channel */
2574 for (j
=0; j
<4; j
++) {
2575 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2577 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2578 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2582 dst
.write_mask
= dst_mask
& write_mask
;
2583 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2584 if (!write_mask
) continue;
2586 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2587 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2588 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2590 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2591 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2595 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2596 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
2598 glsl_src_param_t src0_param
;
2599 glsl_src_param_t src1_param
;
2600 glsl_src_param_t src2_param
;
2603 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2604 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2605 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2606 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2607 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
2608 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2611 /* Handles transforming all WINED3DSIO_M?x? opcodes for
2612 Vertex shaders to GLSL codes */
2613 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
2616 int nComponents
= 0;
2617 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2618 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2619 struct wined3d_shader_instruction tmp_ins
;
2621 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2623 /* Set constants for the temporary argument */
2624 tmp_ins
.ctx
= ins
->ctx
;
2625 tmp_ins
.dst_count
= 1;
2626 tmp_ins
.dst
= &tmp_dst
;
2627 tmp_ins
.src_count
= 2;
2628 tmp_ins
.src
= tmp_src
;
2630 switch(ins
->handler_idx
)
2632 case WINED3DSIH_M4x4
:
2634 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2636 case WINED3DSIH_M4x3
:
2638 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2640 case WINED3DSIH_M3x4
:
2642 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2644 case WINED3DSIH_M3x3
:
2646 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2648 case WINED3DSIH_M3x2
:
2650 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2656 tmp_dst
= ins
->dst
[0];
2657 tmp_src
[0] = ins
->src
[0];
2658 tmp_src
[1] = ins
->src
[1];
2659 for (i
= 0; i
< nComponents
; ++i
)
2661 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2662 shader_glsl_dot(&tmp_ins
);
2663 ++tmp_src
[1].reg
.idx
;
2668 The LRP instruction performs a component-wise linear interpolation
2669 between the second and third operands using the first operand as the
2670 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2671 This is equivalent to mix(src2, src1, src0);
2673 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
2675 glsl_src_param_t src0_param
;
2676 glsl_src_param_t src1_param
;
2677 glsl_src_param_t src2_param
;
2680 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2682 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2683 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2684 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2686 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
2687 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
2690 /** Process the WINED3DSIO_LIT instruction in GLSL:
2691 * dst.x = dst.w = 1.0
2692 * dst.y = (src0.x > 0) ? src0.x
2693 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2694 * where src.w is clamped at +- 128
2696 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
2698 glsl_src_param_t src0_param
;
2699 glsl_src_param_t src1_param
;
2700 glsl_src_param_t src3_param
;
2703 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2704 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2706 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2707 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
2708 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
2710 /* The sdk specifies the instruction like this
2712 * if(src.x > 0.0) dst.y = src.x
2714 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2718 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2719 * dst.x = 1.0 ... No further explanation needed
2720 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2721 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2722 * dst.w = 1.0. ... Nothing fancy.
2724 * So we still have one conditional in there. So do this:
2725 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2727 * step(0.0, x) will return 1 if src.x > 0.0, and 0 otherwise. So if y is 0 we get pow(0.0 * 1.0, power),
2728 * which sets dst.z to 0. If y > 0, but x = 0.0, we get pow(y * 0.0, power), which results in 0 too.
2729 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2731 shader_addline(ins
->ctx
->buffer
,
2732 "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",
2733 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
2736 /** Process the WINED3DSIO_DST instruction in GLSL:
2738 * dst.y = src0.x * src0.y
2742 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
2744 glsl_src_param_t src0y_param
;
2745 glsl_src_param_t src0z_param
;
2746 glsl_src_param_t src1y_param
;
2747 glsl_src_param_t src1w_param
;
2750 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2751 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2753 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
2754 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
2755 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
2756 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
2758 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2759 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
2762 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2763 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2764 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2766 * dst.x = cos(src0.?)
2767 * dst.y = sin(src0.?)
2771 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
2773 glsl_src_param_t src0_param
;
2776 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2777 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2779 switch (write_mask
) {
2780 case WINED3DSP_WRITEMASK_0
:
2781 shader_addline(ins
->ctx
->buffer
, "cos(%s));\n", src0_param
.param_str
);
2784 case WINED3DSP_WRITEMASK_1
:
2785 shader_addline(ins
->ctx
->buffer
, "sin(%s));\n", src0_param
.param_str
);
2788 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
2789 shader_addline(ins
->ctx
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
2793 ERR("Write mask should be .x, .y or .xy\n");
2798 /* sgn in vs_2_0 has 2 extra parameters(registers for temporary storage) which we don't use
2799 * here. But those extra parameters require a dedicated function for sgn, since map2gl would
2800 * generate invalid code
2802 static void shader_glsl_sgn(const struct wined3d_shader_instruction
*ins
)
2804 glsl_src_param_t src0_param
;
2807 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2808 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2810 shader_addline(ins
->ctx
->buffer
, "sign(%s));\n", src0_param
.param_str
);
2813 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2814 * Start a for() loop where src1.y is the initial value of aL,
2815 * increment aL by src1.z for a total of src1.x iterations.
2816 * Need to use a temporary variable for this operation.
2818 /* FIXME: I don't think nested loops will work correctly this way. */
2819 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
2821 glsl_src_param_t src1_param
;
2822 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2823 const DWORD
*control_values
= NULL
;
2824 const local_constant
*constant
;
2826 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
2828 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2829 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2830 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2833 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
2835 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
2836 if (constant
->idx
== ins
->src
[1].reg
.idx
)
2838 control_values
= constant
->value
;
2846 struct wined3d_shader_loop_control loop_control
;
2847 loop_control
.count
= control_values
[0];
2848 loop_control
.start
= control_values
[1];
2849 loop_control
.step
= (int)control_values
[2];
2851 if (loop_control
.step
> 0)
2853 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u; aL%u < (%u * %d + %u); aL%u += %d) {\n",
2854 shader
->baseShader
.cur_loop_depth
, loop_control
.start
,
2855 shader
->baseShader
.cur_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
2856 shader
->baseShader
.cur_loop_depth
, loop_control
.step
);
2858 else if (loop_control
.step
< 0)
2860 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u; aL%u > (%u * %d + %u); aL%u += %d) {\n",
2861 shader
->baseShader
.cur_loop_depth
, loop_control
.start
,
2862 shader
->baseShader
.cur_loop_depth
, loop_control
.count
, loop_control
.step
, loop_control
.start
,
2863 shader
->baseShader
.cur_loop_depth
, loop_control
.step
);
2867 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %u, tmpInt%u = 0; tmpInt%u < %u; tmpInt%u++) {\n",
2868 shader
->baseShader
.cur_loop_depth
, loop_control
.start
, shader
->baseShader
.cur_loop_depth
,
2869 shader
->baseShader
.cur_loop_depth
, loop_control
.count
,
2870 shader
->baseShader
.cur_loop_depth
);
2873 shader_addline(ins
->ctx
->buffer
,
2874 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2875 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
2876 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
2877 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
2880 shader
->baseShader
.cur_loop_depth
++;
2881 shader
->baseShader
.cur_loop_regno
++;
2884 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
2886 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2888 shader_addline(ins
->ctx
->buffer
, "}\n");
2890 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
2892 shader
->baseShader
.cur_loop_depth
--;
2893 shader
->baseShader
.cur_loop_regno
--;
2896 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
2898 shader
->baseShader
.cur_loop_depth
--;
2902 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
2904 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2905 glsl_src_param_t src0_param
;
2906 const DWORD
*control_values
= NULL
;
2907 const local_constant
*constant
;
2909 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2910 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
2912 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
)
2914 if (constant
->idx
== ins
->src
[0].reg
.idx
)
2916 control_values
= constant
->value
;
2922 if(control_values
) {
2923 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2924 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2925 control_values
[0], shader
->baseShader
.cur_loop_depth
);
2927 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2928 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2929 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2930 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
2932 shader
->baseShader
.cur_loop_depth
++;
2935 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
2937 glsl_src_param_t src0_param
;
2939 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2940 shader_addline(ins
->ctx
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2943 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
2945 glsl_src_param_t src0_param
;
2946 glsl_src_param_t src1_param
;
2948 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2949 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2951 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
2952 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2955 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
2957 shader_addline(ins
->ctx
->buffer
, "} else {\n");
2960 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
2962 shader_addline(ins
->ctx
->buffer
, "break;\n");
2965 /* FIXME: According to MSDN the compare is done per component. */
2966 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
2968 glsl_src_param_t src0_param
;
2969 glsl_src_param_t src1_param
;
2971 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2972 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2974 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
2975 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2978 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
2980 shader_addline(ins
->ctx
->buffer
, "}\n");
2981 shader_addline(ins
->ctx
->buffer
, "void subroutine%u () {\n", ins
->src
[0].reg
.idx
);
2984 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
2986 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
);
2989 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
2991 glsl_src_param_t src1_param
;
2993 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2994 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, ins
->src
[0].reg
.idx
);
2997 static void shader_glsl_ret(const struct wined3d_shader_instruction
*ins
)
2999 /* No-op. The closing } is written when a new function is started, and at the end of the shader. This
3000 * function only suppresses the unhandled instruction warning
3004 /*********************************************
3005 * Pixel Shader Specific Code begins here
3006 ********************************************/
3007 static void shader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
3009 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3010 IWineD3DDeviceImpl
*deviceImpl
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
3011 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
3012 ins
->ctx
->reg_maps
->shader_version
.minor
);
3013 glsl_sample_function_t sample_function
;
3014 DWORD sample_flags
= 0;
3016 DWORD mask
= 0, swizzle
;
3018 /* 1.0-1.4: Use destination register as sampler source.
3019 * 2.0+: Use provided sampler source. */
3020 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) sampler_idx
= ins
->dst
[0].reg
.idx
;
3021 else sampler_idx
= ins
->src
[1].reg
.idx
;
3023 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3025 DWORD flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
3026 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3028 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
3029 if (flags
& WINED3DTTFF_PROJECTED
&& sampler_type
!= WINED3DSTT_CUBE
) {
3030 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3031 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
3032 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3033 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
3034 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
3035 case WINED3DTTFF_COUNT4
:
3036 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
3040 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
3042 DWORD src_mod
= ins
->src
[0].modifiers
;
3044 if (src_mod
== WINED3DSPSM_DZ
) {
3045 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3046 mask
= WINED3DSP_WRITEMASK_2
;
3047 } else if (src_mod
== WINED3DSPSM_DW
) {
3048 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3049 mask
= WINED3DSP_WRITEMASK_3
;
3052 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
3054 /* ps 2.0 texldp instruction always divides by the fourth component. */
3055 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
3056 mask
= WINED3DSP_WRITEMASK_3
;
3060 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
3061 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
3062 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3065 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3066 mask
|= sample_function
.coord_mask
;
3068 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
3069 else swizzle
= ins
->src
[1].swizzle
;
3071 /* 1.0-1.3: Use destination register as coordinate source.
3072 1.4+: Use provided coordinate source register. */
3073 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
3076 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3077 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3078 "T%u%s", sampler_idx
, coord_mask
);
3080 glsl_src_param_t coord_param
;
3081 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
3082 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
3084 glsl_src_param_t bias
;
3085 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
3086 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
3087 "%s", coord_param
.param_str
);
3089 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
3090 "%s", coord_param
.param_str
);
3095 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
3097 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3098 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
3099 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3100 glsl_sample_function_t sample_function
;
3101 glsl_src_param_t coord_param
, dx_param
, dy_param
;
3102 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
3104 DWORD swizzle
= ins
->src
[1].swizzle
;
3106 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
])
3108 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
3109 return shader_glsl_tex(ins
);
3112 sampler_idx
= ins
->src
[1].reg
.idx
;
3113 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
3114 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
3115 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3118 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3119 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3120 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
3121 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
3123 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
3124 "%s", coord_param
.param_str
);
3127 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
3129 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3130 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
3131 const struct wined3d_gl_info
*gl_info
= ins
->ctx
->gl_info
;
3132 glsl_sample_function_t sample_function
;
3133 glsl_src_param_t coord_param
, lod_param
;
3134 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
3136 DWORD swizzle
= ins
->src
[1].swizzle
;
3138 sampler_idx
= ins
->src
[1].reg
.idx
;
3139 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
3140 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
3141 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
3143 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, sample_flags
, &sample_function
);
3144 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
3146 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
3148 if (!gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && !gl_info
->supported
[EXT_GPU_SHADER4
]
3149 && shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
3151 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
3152 * However, they seem to work just fine in fragment shaders as well. */
3153 WARN("Using %s in fragment shader.\n", sample_function
.name
);
3155 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
3156 "%s", coord_param
.param_str
);
3159 static void shader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
3161 /* FIXME: Make this work for more than just 2D textures */
3162 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3163 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3165 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
3169 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3170 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
3171 ins
->dst
[0].reg
.idx
, dst_mask
);
3173 DWORD reg
= ins
->src
[0].reg
.idx
;
3174 DWORD src_mod
= ins
->src
[0].modifiers
;
3175 char dst_swizzle
[6];
3177 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
3179 if (src_mod
== WINED3DSPSM_DZ
) {
3180 glsl_src_param_t div_param
;
3181 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3182 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
3184 if (mask_size
> 1) {
3185 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
3187 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
3189 } else if (src_mod
== WINED3DSPSM_DW
) {
3190 glsl_src_param_t div_param
;
3191 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3192 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
3194 if (mask_size
> 1) {
3195 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
3197 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
3200 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
3205 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
3206 * Take a 3-component dot product of the TexCoord[dstreg] and src,
3207 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
3208 static void shader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
3210 glsl_src_param_t src0_param
;
3211 glsl_sample_function_t sample_function
;
3212 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3213 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3216 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3218 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
3219 * scalar, and projected sampling would require 4.
3221 * It is a dependent read - not valid with conditional NP2 textures
3223 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3224 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
3229 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3230 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
3234 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3235 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
3239 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3240 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
3244 FIXME("Unexpected mask size %u\n", mask_size
);
3249 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
3250 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
3251 static void shader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
3253 glsl_src_param_t src0_param
;
3254 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
3255 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3257 unsigned int mask_size
;
3259 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3260 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
3261 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3263 if (mask_size
> 1) {
3264 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
3266 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
3270 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
3271 * Calculate the depth as dst.x / dst.y */
3272 static void shader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
3274 glsl_dst_param_t dst_param
;
3276 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3278 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
3279 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
3280 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
3281 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
3284 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
3285 dst_param
.reg_name
, dst_param
.reg_name
);
3288 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
3289 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
3290 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
3291 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
3293 static void shader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
3295 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3296 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
3297 glsl_src_param_t src0_param
;
3299 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3301 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
3302 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
3305 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
3306 * Calculate the 1st of a 2-row matrix multiplication. */
3307 static void shader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
3309 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3310 DWORD reg
= ins
->dst
[0].reg
.idx
;
3311 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3312 glsl_src_param_t src0_param
;
3314 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3315 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3318 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
3319 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
3320 static void shader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
3322 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3323 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3324 DWORD reg
= ins
->dst
[0].reg
.idx
;
3325 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3326 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
3327 glsl_src_param_t src0_param
;
3329 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3330 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
3331 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
3334 static void shader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
3336 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3337 DWORD reg
= ins
->dst
[0].reg
.idx
;
3338 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3339 glsl_src_param_t src0_param
;
3340 glsl_sample_function_t sample_function
;
3342 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3343 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3345 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3347 /* Sample the texture using the calculated coordinates */
3348 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
3351 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
3352 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
3353 static void shader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
3355 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3356 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3357 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
3358 glsl_src_param_t src0_param
;
3359 DWORD reg
= ins
->dst
[0].reg
.idx
;
3360 glsl_sample_function_t sample_function
;
3362 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3363 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3365 /* Dependent read, not valid with conditional NP2 */
3366 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3368 /* Sample the texture using the calculated coordinates */
3369 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3371 current_state
->current_row
= 0;
3374 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
3375 * Perform the 3rd row of a 3x3 matrix multiply */
3376 static void shader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
3378 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3379 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3380 SHADER_PARSE_STATE
*current_state
= &shader
->baseShader
.parse_state
;
3381 glsl_src_param_t src0_param
;
3383 DWORD reg
= ins
->dst
[0].reg
.idx
;
3385 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3387 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3388 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
3389 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
3391 current_state
->current_row
= 0;
3394 /* Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
3395 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3396 static void shader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
3398 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3399 DWORD reg
= ins
->dst
[0].reg
.idx
;
3400 glsl_src_param_t src0_param
;
3401 glsl_src_param_t src1_param
;
3402 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3403 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
3404 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3405 glsl_sample_function_t sample_function
;
3407 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3408 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
3410 /* Perform the last matrix multiply operation */
3411 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
3412 /* Reflection calculation */
3413 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
3415 /* Dependent read, not valid with conditional NP2 */
3416 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3418 /* Sample the texture */
3419 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3421 current_state
->current_row
= 0;
3424 /* Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
3425 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
3426 static void shader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
3428 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3429 DWORD reg
= ins
->dst
[0].reg
.idx
;
3430 struct wined3d_shader_buffer
*buffer
= ins
->ctx
->buffer
;
3431 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
3432 glsl_src_param_t src0_param
;
3433 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
3434 glsl_sample_function_t sample_function
;
3436 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
3438 /* Perform the last matrix multiply operation */
3439 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
3441 /* Construct the eye-ray vector from w coordinates */
3442 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
3443 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
3444 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
3446 /* Dependent read, not valid with conditional NP2 */
3447 shader_glsl_get_sample_function(ins
->ctx
, reg
, 0, &sample_function
);
3449 /* Sample the texture using the calculated coordinates */
3450 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
3452 current_state
->current_row
= 0;
3455 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
3456 * Apply a fake bump map transform.
3457 * texbem is pshader <= 1.3 only, this saves a few version checks
3459 static void shader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
3461 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
3462 IWineD3DDeviceImpl
*deviceImpl
= (IWineD3DDeviceImpl
*)shader
->baseShader
.device
;
3463 glsl_sample_function_t sample_function
;
3464 glsl_src_param_t coord_param
;
3470 sampler_idx
= ins
->dst
[0].reg
.idx
;
3471 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
3473 /* Dependent read, not valid with conditional NP2 */
3474 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3475 mask
= sample_function
.coord_mask
;
3477 shader_glsl_write_mask_to_str(mask
, coord_mask
);
3479 /* with projective textures, texbem only divides the static texture coord, not the displacement,
3480 * so we can't let the GL handle this.
3482 if (flags
& WINED3DTTFF_PROJECTED
) {
3484 char coord_div_mask
[3];
3485 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
3486 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
3487 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
3488 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
3489 case WINED3DTTFF_COUNT4
:
3490 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
3492 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
3493 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
3496 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
3498 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3499 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
3500 coord_param
.param_str
, coord_mask
);
3502 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
3504 glsl_src_param_t luminance_param
;
3505 glsl_dst_param_t dst_param
;
3507 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
3508 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3510 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3511 dst_param
.reg_name
, dst_param
.mask_str
,
3512 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
3516 static void shader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
3518 glsl_src_param_t src0_param
, src1_param
;
3519 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3521 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3522 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3524 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3525 shader_addline(ins
->ctx
->buffer
, "%s + bumpenvmat%d * %s);\n",
3526 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
3529 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3530 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3531 static void shader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
3533 glsl_src_param_t src0_param
;
3534 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3535 glsl_sample_function_t sample_function
;
3537 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3539 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3540 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3541 "%s.wx", src0_param
.reg_name
);
3544 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3545 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3546 static void shader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
3548 glsl_src_param_t src0_param
;
3549 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3550 glsl_sample_function_t sample_function
;
3552 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3554 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3555 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3556 "%s.yz", src0_param
.reg_name
);
3559 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3560 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3561 static void shader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
3563 glsl_src_param_t src0_param
;
3564 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3565 glsl_sample_function_t sample_function
;
3567 /* Dependent read, not valid with conditional NP2 */
3568 shader_glsl_get_sample_function(ins
->ctx
, sampler_idx
, 0, &sample_function
);
3569 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
3571 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3572 "%s", src0_param
.param_str
);
3575 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3576 * If any of the first 3 components are < 0, discard this pixel */
3577 static void shader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
3579 glsl_dst_param_t dst_param
;
3581 /* The argument is a destination parameter, and no writemasks are allowed */
3582 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3583 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
3585 /* 2.0 shaders compare all 4 components in texkill */
3586 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
3588 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3589 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3590 * 4 components are defined, only the first 3 are used
3592 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
3596 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3597 * dst = dot2(src0, src1) + src2 */
3598 static void shader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
3600 glsl_src_param_t src0_param
;
3601 glsl_src_param_t src1_param
;
3602 glsl_src_param_t src2_param
;
3604 unsigned int mask_size
;
3606 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3607 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3609 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3610 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3611 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
3613 if (mask_size
> 1) {
3614 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
3615 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3617 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
3618 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3622 static void shader_glsl_input_pack(IWineD3DPixelShader
*iface
, struct wined3d_shader_buffer
*buffer
,
3623 const struct wined3d_shader_signature_element
*input_signature
, const struct shader_reg_maps
*reg_maps
,
3624 enum vertexprocessing_mode vertexprocessing
)
3627 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
3628 WORD map
= reg_maps
->input_registers
;
3630 for (i
= 0; map
; map
>>= 1, ++i
)
3632 const char *semantic_name
;
3637 if (!(map
& 1)) continue;
3639 semantic_name
= input_signature
[i
].semantic_name
;
3640 semantic_idx
= input_signature
[i
].semantic_idx
;
3641 shader_glsl_write_mask_to_str(input_signature
[i
].mask
, reg_mask
);
3643 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3645 if (semantic_idx
< 8 && vertexprocessing
== pretransformed
)
3646 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3647 This
->input_reg_map
[i
], reg_mask
, semantic_idx
, reg_mask
);
3649 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3650 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3652 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3654 if (semantic_idx
== 0)
3655 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
3656 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3657 else if (semantic_idx
== 1)
3658 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3659 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3661 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3662 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3666 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3667 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3672 /*********************************************
3673 * Vertex Shader Specific Code begins here
3674 ********************************************/
3676 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
) {
3677 glsl_program_key_t key
;
3679 key
.vshader
= entry
->vshader
;
3680 key
.pshader
= entry
->pshader
;
3681 key
.vs_args
= entry
->vs_args
;
3682 key
.ps_args
= entry
->ps_args
;
3684 if (wine_rb_put(&priv
->program_lookup
, &key
, &entry
->program_lookup_entry
) == -1)
3686 ERR("Failed to insert program entry.\n");
3690 static struct glsl_shader_prog_link
*get_glsl_program_entry(struct shader_glsl_priv
*priv
,
3691 IWineD3DVertexShader
*vshader
, IWineD3DPixelShader
*pshader
, struct vs_compile_args
*vs_args
,
3692 struct ps_compile_args
*ps_args
) {
3693 struct wine_rb_entry
*entry
;
3694 glsl_program_key_t key
;
3696 key
.vshader
= vshader
;
3697 key
.pshader
= pshader
;
3698 key
.vs_args
= *vs_args
;
3699 key
.ps_args
= *ps_args
;
3701 entry
= wine_rb_get(&priv
->program_lookup
, &key
);
3702 return entry
? WINE_RB_ENTRY_VALUE(entry
, struct glsl_shader_prog_link
, program_lookup_entry
) : NULL
;
3705 /* GL locking is done by the caller */
3706 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const struct wined3d_gl_info
*gl_info
,
3707 struct glsl_shader_prog_link
*entry
)
3709 glsl_program_key_t key
;
3711 key
.vshader
= entry
->vshader
;
3712 key
.pshader
= entry
->pshader
;
3713 key
.vs_args
= entry
->vs_args
;
3714 key
.ps_args
= entry
->ps_args
;
3715 wine_rb_remove(&priv
->program_lookup
, &key
);
3717 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
3718 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
3719 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
3720 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
3721 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
3722 HeapFree(GetProcessHeap(), 0, entry
);
3725 static void handle_ps3_input(struct wined3d_shader_buffer
*buffer
, const struct wined3d_gl_info
*gl_info
, const DWORD
*map
,
3726 const struct wined3d_shader_signature_element
*input_signature
, const struct shader_reg_maps
*reg_maps_in
,
3727 const struct wined3d_shader_signature_element
*output_signature
, const struct shader_reg_maps
*reg_maps_out
)
3730 const char *semantic_name_in
, *semantic_name_out
;
3731 UINT semantic_idx_in
, semantic_idx_out
;
3734 unsigned int in_count
= vec4_varyings(3, gl_info
);
3735 char reg_mask
[6], reg_mask_out
[6];
3736 char destination
[50];
3737 WORD input_map
, output_map
;
3739 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
3741 if (!output_signature
)
3743 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3744 shader_addline(buffer
, "vec4 front_color = gl_FrontColor;\n");
3745 shader_addline(buffer
, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3748 input_map
= reg_maps_in
->input_registers
;
3749 for (i
= 0; input_map
; input_map
>>= 1, ++i
)
3751 if (!(input_map
& 1)) continue;
3754 if (in_idx
>= (in_count
+ 2)) {
3755 FIXME("More input varyings declared than supported, expect issues\n");
3758 else if (map
[i
] == ~0U)
3760 /* Declared, but not read register */
3764 if (in_idx
== in_count
) {
3765 sprintf(destination
, "gl_FrontColor");
3766 } else if (in_idx
== in_count
+ 1) {
3767 sprintf(destination
, "gl_FrontSecondaryColor");
3769 sprintf(destination
, "IN[%u]", in_idx
);
3772 semantic_name_in
= input_signature
[i
].semantic_name
;
3773 semantic_idx_in
= input_signature
[i
].semantic_idx
;
3774 set
[map
[i
]] = input_signature
[i
].mask
;
3775 shader_glsl_write_mask_to_str(input_signature
[i
].mask
, reg_mask
);
3777 if (!output_signature
)
3779 if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_COLOR
))
3781 if (semantic_idx_in
== 0)
3782 shader_addline(buffer
, "%s%s = front_color%s;\n",
3783 destination
, reg_mask
, reg_mask
);
3784 else if (semantic_idx_in
== 1)
3785 shader_addline(buffer
, "%s%s = front_secondary_color%s;\n",
3786 destination
, reg_mask
, reg_mask
);
3788 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3789 destination
, reg_mask
, reg_mask
);
3791 else if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_TEXCOORD
))
3793 if (semantic_idx_in
< 8)
3795 shader_addline(buffer
, "%s%s = gl_TexCoord[%u]%s;\n",
3796 destination
, reg_mask
, semantic_idx_in
, reg_mask
);
3800 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3801 destination
, reg_mask
, reg_mask
);
3804 else if (shader_match_semantic(semantic_name_in
, WINED3DDECLUSAGE_FOG
))
3806 shader_addline(buffer
, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3807 destination
, reg_mask
, reg_mask
);
3811 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3812 destination
, reg_mask
, reg_mask
);
3817 output_map
= reg_maps_out
->output_registers
;
3818 for (j
= 0; output_map
; output_map
>>= 1, ++j
)
3820 if (!(output_map
& 1)) continue;
3822 semantic_name_out
= output_signature
[j
].semantic_name
;
3823 semantic_idx_out
= output_signature
[j
].semantic_idx
;
3824 shader_glsl_write_mask_to_str(output_signature
[j
].mask
, reg_mask_out
);
3826 if (semantic_idx_in
== semantic_idx_out
3827 && !strcmp(semantic_name_in
, semantic_name_out
))
3829 shader_addline(buffer
, "%s%s = OUT[%u]%s;\n",
3830 destination
, reg_mask
, j
, reg_mask
);
3835 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3836 destination
, reg_mask
, reg_mask
);
3841 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3842 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3843 * input varyings are assigned above, if the optimizer works properly.
3845 for(i
= 0; i
< in_count
+ 2; i
++) {
3846 if (set
[i
] && set
[i
] != WINED3DSP_WRITEMASK_ALL
)
3848 unsigned int size
= 0;
3849 memset(reg_mask
, 0, sizeof(reg_mask
));
3850 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
3851 reg_mask
[size
] = 'x';
3854 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
3855 reg_mask
[size
] = 'y';
3858 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
3859 reg_mask
[size
] = 'z';
3862 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
3863 reg_mask
[size
] = 'w';
3867 if (i
== in_count
) {
3868 sprintf(destination
, "gl_FrontColor");
3869 } else if (i
== in_count
+ 1) {
3870 sprintf(destination
, "gl_FrontSecondaryColor");
3872 sprintf(destination
, "IN[%u]", i
);
3876 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
3878 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
3883 HeapFree(GetProcessHeap(), 0, set
);
3886 /* GL locking is done by the caller */
3887 static GLhandleARB
generate_param_reorder_function(struct wined3d_shader_buffer
*buffer
,
3888 IWineD3DVertexShader
*vertexshader
, IWineD3DPixelShader
*pixelshader
, const struct wined3d_gl_info
*gl_info
)
3890 GLhandleARB ret
= 0;
3891 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
3892 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
3893 IWineD3DDeviceImpl
*device
;
3894 DWORD vs_major
= vs
->baseShader
.reg_maps
.shader_version
.major
;
3895 DWORD ps_major
= ps
? ps
->baseShader
.reg_maps
.shader_version
.major
: 0;
3897 const char *semantic_name
;
3900 const struct wined3d_shader_signature_element
*output_signature
;
3902 shader_buffer_clear(buffer
);
3904 shader_addline(buffer
, "#version 120\n");
3906 if(vs_major
< 3 && ps_major
< 3) {
3907 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3908 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3910 device
= (IWineD3DDeviceImpl
*) vs
->baseShader
.device
;
3911 if ((gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
)
3912 && ps_major
== 0 && vs_major
> 0 && !device
->frag_pipe
->ffp_proj_control
)
3914 shader_addline(buffer
, "void order_ps_input() {\n");
3915 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
3916 if(vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
3917 vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3918 shader_addline(buffer
, "gl_TexCoord[%u].w = 1.0;\n", i
);
3921 shader_addline(buffer
, "}\n");
3923 shader_addline(buffer
, "void order_ps_input() { /* do nothing */ }\n");
3925 } else if(ps_major
< 3 && vs_major
>= 3) {
3926 WORD map
= vs
->baseShader
.reg_maps
.output_registers
;
3928 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3929 output_signature
= vs
->baseShader
.output_signature
;
3931 shader_addline(buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3932 for (i
= 0; map
; map
>>= 1, ++i
)
3936 if (!(map
& 1)) continue;
3938 semantic_name
= output_signature
[i
].semantic_name
;
3939 semantic_idx
= output_signature
[i
].semantic_idx
;
3940 write_mask
= output_signature
[i
].mask
;
3941 shader_glsl_write_mask_to_str(write_mask
, reg_mask
);
3943 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_COLOR
))
3945 if (semantic_idx
== 0)
3946 shader_addline(buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3947 else if (semantic_idx
== 1)
3948 shader_addline(buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3950 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3952 shader_addline(buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3954 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_TEXCOORD
))
3956 if (semantic_idx
< 8)
3958 if (!(gl_info
->quirks
& WINED3D_QUIRK_SET_TEXCOORD_W
) || ps_major
> 0)
3959 write_mask
|= WINED3DSP_WRITEMASK_3
;
3961 shader_addline(buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3962 semantic_idx
, reg_mask
, i
, reg_mask
);
3963 if (!(write_mask
& WINED3DSP_WRITEMASK_3
))
3964 shader_addline(buffer
, "gl_TexCoord[%u].w = 1.0;\n", semantic_idx
);
3967 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
3969 shader_addline(buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3971 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_FOG
))
3973 shader_addline(buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
3976 shader_addline(buffer
, "}\n");
3978 } else if(ps_major
>= 3 && vs_major
>= 3) {
3979 WORD map
= vs
->baseShader
.reg_maps
.output_registers
;
3981 output_signature
= vs
->baseShader
.output_signature
;
3983 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3984 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info
));
3985 shader_addline(buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3987 /* First, sort out position and point size. Those are not passed to the pixel shader */
3988 for (i
= 0; map
; map
>>= 1, ++i
)
3990 if (!(map
& 1)) continue;
3992 semantic_name
= output_signature
[i
].semantic_name
;
3993 shader_glsl_write_mask_to_str(output_signature
[i
].mask
, reg_mask
);
3995 if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_POSITION
))
3997 shader_addline(buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3999 else if (shader_match_semantic(semantic_name
, WINED3DDECLUSAGE_PSIZE
))
4001 shader_addline(buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
4005 /* Then, fix the pixel shader input */
4006 handle_ps3_input(buffer
, gl_info
, ps
->input_reg_map
, ps
->baseShader
.input_signature
,
4007 &ps
->baseShader
.reg_maps
, output_signature
, &vs
->baseShader
.reg_maps
);
4009 shader_addline(buffer
, "}\n");
4010 } else if(ps_major
>= 3 && vs_major
< 3) {
4011 shader_addline(buffer
, "varying vec4 IN[%u];\n", vec4_varyings(3, gl_info
));
4012 shader_addline(buffer
, "void order_ps_input() {\n");
4013 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
4014 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
4015 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
4017 handle_ps3_input(buffer
, gl_info
, ps
->input_reg_map
, ps
->baseShader
.input_signature
,
4018 &ps
->baseShader
.reg_maps
, NULL
, NULL
);
4019 shader_addline(buffer
, "}\n");
4021 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
4024 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4025 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
4026 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
->buffer
, NULL
));
4027 checkGLcall("glShaderSourceARB(ret, 1, &buffer->buffer, NULL)");
4028 GL_EXTCALL(glCompileShaderARB(ret
));
4029 checkGLcall("glCompileShaderARB(ret)");
4034 /* GL locking is done by the caller */
4035 static void hardcode_local_constants(IWineD3DBaseShaderImpl
*shader
, const struct wined3d_gl_info
*gl_info
,
4036 GLhandleARB programId
, char prefix
)
4038 const local_constant
*lconst
;
4043 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
) {
4044 value
= (const float *)lconst
->value
;
4045 snprintf(glsl_name
, sizeof(glsl_name
), "%cLC%u", prefix
, lconst
->idx
);
4046 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4047 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, value
));
4049 checkGLcall("Hardcoding local constants");
4052 /* GL locking is done by the caller */
4053 static GLuint
shader_glsl_generate_pshader(const struct wined3d_context
*context
,
4054 struct wined3d_shader_buffer
*buffer
, IWineD3DPixelShaderImpl
*This
,
4055 const struct ps_compile_args
*args
, struct ps_np2fixup_info
*np2fixup_info
)
4057 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
4058 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4059 CONST DWORD
*function
= This
->baseShader
.function
;
4060 struct shader_glsl_ctx_priv priv_ctx
;
4062 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4063 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
4065 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4066 priv_ctx
.cur_ps_args
= args
;
4067 priv_ctx
.cur_np2fixup_info
= np2fixup_info
;
4069 shader_addline(buffer
, "#version 120\n");
4071 if (gl_info
->supported
[ARB_SHADER_TEXTURE_LOD
] && reg_maps
->usestexldd
)
4073 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
4075 if (gl_info
->supported
[ARB_TEXTURE_RECTANGLE
])
4077 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4078 * drivers write a warning if we don't do so
4080 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
4082 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4084 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4087 /* Base Declarations */
4088 shader_generate_glsl_declarations(context
, buffer
, (IWineD3DBaseShader
*)This
, reg_maps
, &priv_ctx
);
4090 /* Pack 3.0 inputs */
4091 if (reg_maps
->shader_version
.major
>= 3 && args
->vp_mode
!= vertexshader
)
4093 shader_glsl_input_pack((IWineD3DPixelShader
*) This
, buffer
,
4094 This
->baseShader
.input_signature
, reg_maps
, args
->vp_mode
);
4097 /* Base Shader Body */
4098 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
4100 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4101 if (reg_maps
->shader_version
.major
< 2)
4103 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4104 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
4107 if (args
->srgb_correction
)
4109 shader_addline(buffer
, "tmp0.xyz = pow(gl_FragData[0].xyz, vec3(srgb_const0.x));\n");
4110 shader_addline(buffer
, "tmp0.xyz = tmp0.xyz * vec3(srgb_const0.y) - vec3(srgb_const0.z);\n");
4111 shader_addline(buffer
, "tmp1.xyz = gl_FragData[0].xyz * vec3(srgb_const0.w);\n");
4112 shader_addline(buffer
, "bvec3 srgb_compare = lessThan(gl_FragData[0].xyz, vec3(srgb_const1.x));\n");
4113 shader_addline(buffer
, "gl_FragData[0].xyz = mix(tmp0.xyz, tmp1.xyz, vec3(srgb_compare));\n");
4114 shader_addline(buffer
, "gl_FragData[0] = clamp(gl_FragData[0], 0.0, 1.0);\n");
4116 /* Pixel shader < 3.0 do not replace the fog stage.
4117 * This implements linear fog computation and blending.
4118 * TODO: non linear fog
4119 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4120 * -1/(e-s) and e/(e-s) respectively.
4122 if (reg_maps
->shader_version
.major
< 3)
4125 case FOG_OFF
: break;
4127 shader_addline(buffer
, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4128 shader_addline(buffer
, "float fogend = gl_Fog.end * -fogstart;\n");
4129 shader_addline(buffer
, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4130 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4133 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4134 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4135 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4136 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4139 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4140 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4141 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4142 shader_addline(buffer
, "gl_FragData[0].xyz = mix(gl_Fog.color.xyz, gl_FragData[0].xyz, Fog);\n");
4147 shader_addline(buffer
, "}\n");
4149 TRACE("Compiling shader object %u\n", shader_obj
);
4150 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
4151 GL_EXTCALL(glCompileShaderARB(shader_obj
));
4152 print_glsl_info_log(gl_info
, shader_obj
);
4154 /* Store the shader object */
4158 /* GL locking is done by the caller */
4159 static GLuint
shader_glsl_generate_vshader(const struct wined3d_context
*context
,
4160 struct wined3d_shader_buffer
*buffer
, IWineD3DVertexShaderImpl
*This
,
4161 const struct vs_compile_args
*args
)
4163 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
4164 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4165 CONST DWORD
*function
= This
->baseShader
.function
;
4166 struct shader_glsl_ctx_priv priv_ctx
;
4168 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4169 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4171 shader_addline(buffer
, "#version 120\n");
4173 if (gl_info
->supported
[EXT_GPU_SHADER4
])
4175 shader_addline(buffer
, "#extension GL_EXT_gpu_shader4 : enable\n");
4178 memset(&priv_ctx
, 0, sizeof(priv_ctx
));
4179 priv_ctx
.cur_vs_args
= args
;
4181 /* Base Declarations */
4182 shader_generate_glsl_declarations(context
, buffer
, (IWineD3DBaseShader
*)This
, reg_maps
, &priv_ctx
);
4184 /* Base Shader Body */
4185 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
, &priv_ctx
);
4187 /* Unpack 3.0 outputs */
4188 if (reg_maps
->shader_version
.major
>= 3) shader_addline(buffer
, "order_ps_input(OUT);\n");
4189 else shader_addline(buffer
, "order_ps_input();\n");
4191 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4192 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4193 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4194 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4196 if(args
->fog_src
== VS_FOG_Z
) {
4197 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
4198 } else if (!reg_maps
->fog
) {
4199 shader_addline(buffer
, "gl_FogFragCoord = 0.0;\n");
4202 /* Write the final position.
4204 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4205 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4206 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4207 * contains 1.0 to allow a mad.
4209 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4210 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4211 if(args
->clip_enabled
) {
4212 shader_addline(buffer
, "gl_ClipVertex = gl_Position;\n");
4215 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4217 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4218 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4219 * which is the same as z = z * 2 - w.
4221 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4223 shader_addline(buffer
, "}\n");
4225 TRACE("Compiling shader object %u\n", shader_obj
);
4226 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
4227 GL_EXTCALL(glCompileShaderARB(shader_obj
));
4228 print_glsl_info_log(gl_info
, shader_obj
);
4233 static GLhandleARB
find_glsl_pshader(const struct wined3d_context
*context
,
4234 struct wined3d_shader_buffer
*buffer
, IWineD3DPixelShaderImpl
*shader
,
4235 const struct ps_compile_args
*args
, const struct ps_np2fixup_info
**np2fixup_info
)
4239 struct glsl_ps_compiled_shader
*new_array
;
4240 struct glsl_pshader_private
*shader_data
;
4241 struct ps_np2fixup_info
*np2fixup
= NULL
;
4244 if (!shader
->baseShader
.backend_data
)
4246 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4247 if (!shader
->baseShader
.backend_data
)
4249 ERR("Failed to allocate backend data.\n");
4253 shader_data
= shader
->baseShader
.backend_data
;
4255 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4256 * so a linear search is more performant than a hashmap or a binary search
4257 * (cache coherency etc)
4259 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4260 if(memcmp(&shader_data
->gl_shaders
[i
].args
, args
, sizeof(*args
)) == 0) {
4261 if(args
->np2_fixup
) *np2fixup_info
= &shader_data
->gl_shaders
[i
].np2fixup
;
4262 return shader_data
->gl_shaders
[i
].prgId
;
4266 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
4267 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4268 if (shader_data
->num_gl_shaders
)
4270 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4271 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
4272 new_size
* sizeof(*shader_data
->gl_shaders
));
4274 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
4279 ERR("Out of memory\n");
4282 shader_data
->gl_shaders
= new_array
;
4283 shader_data
->shader_array_size
= new_size
;
4286 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4288 memset(&shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
, 0, sizeof(struct ps_np2fixup_info
));
4289 if (args
->np2_fixup
) np2fixup
= &shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].np2fixup
;
4291 pixelshader_update_samplers(&shader
->baseShader
.reg_maps
,
4292 ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->stateBlock
->textures
);
4294 shader_buffer_clear(buffer
);
4295 ret
= shader_glsl_generate_pshader(context
, buffer
, shader
, args
, np2fixup
);
4296 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
4297 *np2fixup_info
= np2fixup
;
4302 static inline BOOL
vs_args_equal(const struct vs_compile_args
*stored
, const struct vs_compile_args
*new,
4303 const DWORD use_map
) {
4304 if((stored
->swizzle_map
& use_map
) != new->swizzle_map
) return FALSE
;
4305 if((stored
->clip_enabled
) != new->clip_enabled
) return FALSE
;
4306 return stored
->fog_src
== new->fog_src
;
4309 static GLhandleARB
find_glsl_vshader(const struct wined3d_context
*context
,
4310 struct wined3d_shader_buffer
*buffer
, IWineD3DVertexShaderImpl
*shader
,
4311 const struct vs_compile_args
*args
)
4315 struct glsl_vs_compiled_shader
*new_array
;
4316 DWORD use_map
= ((IWineD3DDeviceImpl
*)shader
->baseShader
.device
)->strided_streams
.use_map
;
4317 struct glsl_vshader_private
*shader_data
;
4320 if (!shader
->baseShader
.backend_data
)
4322 shader
->baseShader
.backend_data
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*shader_data
));
4323 if (!shader
->baseShader
.backend_data
)
4325 ERR("Failed to allocate backend data.\n");
4329 shader_data
= shader
->baseShader
.backend_data
;
4331 /* Usually we have very few GL shaders for each d3d shader(just 1 or maybe 2),
4332 * so a linear search is more performant than a hashmap or a binary search
4333 * (cache coherency etc)
4335 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4336 if(vs_args_equal(&shader_data
->gl_shaders
[i
].args
, args
, use_map
)) {
4337 return shader_data
->gl_shaders
[i
].prgId
;
4341 TRACE("No matching GL shader found for shader %p, compiling a new shader.\n", shader
);
4343 if(shader_data
->shader_array_size
== shader_data
->num_gl_shaders
) {
4344 if (shader_data
->num_gl_shaders
)
4346 new_size
= shader_data
->shader_array_size
+ max(1, shader_data
->shader_array_size
/ 2);
4347 new_array
= HeapReAlloc(GetProcessHeap(), 0, shader_data
->gl_shaders
,
4348 new_size
* sizeof(*shader_data
->gl_shaders
));
4350 new_array
= HeapAlloc(GetProcessHeap(), 0, sizeof(*shader_data
->gl_shaders
));
4355 ERR("Out of memory\n");
4358 shader_data
->gl_shaders
= new_array
;
4359 shader_data
->shader_array_size
= new_size
;
4362 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
].args
= *args
;
4364 shader_buffer_clear(buffer
);
4365 ret
= shader_glsl_generate_vshader(context
, buffer
, shader
, args
);
4366 shader_data
->gl_shaders
[shader_data
->num_gl_shaders
++].prgId
= ret
;
4371 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
4372 * It sets the programId on the current StateBlock (because it should be called
4373 * inside of the DrawPrimitive() part of the render loop).
4375 * If a program for the given combination does not exist, create one, and store
4376 * the program in the hash table. If it creates a program, it will link the
4377 * given objects, too.
4380 /* GL locking is done by the caller */
4381 static void set_glsl_shader_program(const struct wined3d_context
*context
,
4382 IWineD3DDeviceImpl
*device
, BOOL use_ps
, BOOL use_vs
)
4384 IWineD3DVertexShader
*vshader
= use_vs
? device
->stateBlock
->vertexShader
: NULL
;
4385 IWineD3DPixelShader
*pshader
= use_ps
? device
->stateBlock
->pixelShader
: NULL
;
4386 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4387 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4388 struct glsl_shader_prog_link
*entry
= NULL
;
4389 GLhandleARB programId
= 0;
4390 GLhandleARB reorder_shader_id
= 0;
4393 struct ps_compile_args ps_compile_args
;
4394 struct vs_compile_args vs_compile_args
;
4396 if (vshader
) find_vs_compile_args((IWineD3DVertexShaderImpl
*)vshader
, device
->stateBlock
, &vs_compile_args
);
4397 if (pshader
) find_ps_compile_args((IWineD3DPixelShaderImpl
*)pshader
, device
->stateBlock
, &ps_compile_args
);
4399 entry
= get_glsl_program_entry(priv
, vshader
, pshader
, &vs_compile_args
, &ps_compile_args
);
4401 priv
->glsl_program
= entry
;
4405 /* If we get to this point, then no matching program exists, so we create one */
4406 programId
= GL_EXTCALL(glCreateProgramObjectARB());
4407 TRACE("Created new GLSL shader program %u\n", programId
);
4409 /* Create the entry */
4410 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
4411 entry
->programId
= programId
;
4412 entry
->vshader
= vshader
;
4413 entry
->pshader
= pshader
;
4414 entry
->vs_args
= vs_compile_args
;
4415 entry
->ps_args
= ps_compile_args
;
4416 entry
->constant_version
= 0;
4417 entry
->np2Fixup_info
= NULL
;
4418 /* Add the hash table entry */
4419 add_glsl_program_entry(priv
, entry
);
4421 /* Set the current program */
4422 priv
->glsl_program
= entry
;
4424 /* Attach GLSL vshader */
4427 GLhandleARB vshader_id
= find_glsl_vshader(context
, &priv
->shader_buffer
,
4428 (IWineD3DVertexShaderImpl
*)vshader
, &vs_compile_args
);
4429 WORD map
= ((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.input_registers
;
4432 reorder_shader_id
= generate_param_reorder_function(&priv
->shader_buffer
, vshader
, pshader
, gl_info
);
4433 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
4434 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
4435 checkGLcall("glAttachObjectARB");
4436 /* Flag the reorder function for deletion, then it will be freed automatically when the program
4439 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
4441 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
4442 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
4443 checkGLcall("glAttachObjectARB");
4445 /* Bind vertex attributes to a corresponding index number to match
4446 * the same index numbers as ARB_vertex_programs (makes loading
4447 * vertex attributes simpler). With this method, we can use the
4448 * exact same code to load the attributes later for both ARB and
4451 * We have to do this here because we need to know the Program ID
4452 * in order to make the bindings work, and it has to be done prior
4453 * to linking the GLSL program. */
4454 for (i
= 0; map
; map
>>= 1, ++i
)
4456 if (!(map
& 1)) continue;
4458 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%u", i
);
4459 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
4461 checkGLcall("glBindAttribLocationARB");
4463 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
4466 /* Attach GLSL pshader */
4469 GLhandleARB pshader_id
= find_glsl_pshader(context
, &priv
->shader_buffer
,
4470 (IWineD3DPixelShaderImpl
*)pshader
, &ps_compile_args
, &entry
->np2Fixup_info
);
4471 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
4472 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
4473 checkGLcall("glAttachObjectARB");
4475 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
4478 /* Link the program */
4479 TRACE("Linking GLSL shader program %u\n", programId
);
4480 GL_EXTCALL(glLinkProgramARB(programId
));
4481 shader_glsl_validate_link(gl_info
, programId
);
4483 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0,
4484 sizeof(GLhandleARB
) * gl_info
->limits
.glsl_vs_float_constants
);
4485 for (i
= 0; i
< gl_info
->limits
.glsl_vs_float_constants
; ++i
)
4487 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
4488 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4490 for (i
= 0; i
< MAX_CONST_I
; ++i
)
4492 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
4493 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4495 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0,
4496 sizeof(GLhandleARB
) * gl_info
->limits
.glsl_ps_float_constants
);
4497 for (i
= 0; i
< gl_info
->limits
.glsl_ps_float_constants
; ++i
)
4499 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
4500 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4502 for (i
= 0; i
< MAX_CONST_I
; ++i
)
4504 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
4505 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
4511 for(i
= 0; i
< MAX_TEXTURES
; i
++) {
4512 sprintf(name
, "bumpenvmat%u", i
);
4513 entry
->bumpenvmat_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4514 sprintf(name
, "luminancescale%u", i
);
4515 entry
->luminancescale_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4516 sprintf(name
, "luminanceoffset%u", i
);
4517 entry
->luminanceoffset_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
4520 if (ps_compile_args
.np2_fixup
) {
4521 if (entry
->np2Fixup_info
) {
4522 entry
->np2Fixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "PsamplerNP2Fixup"));
4524 FIXME("NP2 texcoord fixup needed for this pixelshader, but no fixup uniform found.\n");
4529 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
4530 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
4531 checkGLcall("Find glsl program uniform locations");
4534 && ((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.reg_maps
.shader_version
.major
>= 3
4535 && ((IWineD3DPixelShaderImpl
*)pshader
)->declared_in_count
> vec4_varyings(3, gl_info
))
4537 TRACE("Shader %d needs vertex color clamping disabled\n", programId
);
4538 entry
->vertex_color_clamp
= GL_FALSE
;
4540 entry
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
4543 /* Set the shader to allow uniform loading on it */
4544 GL_EXTCALL(glUseProgramObjectARB(programId
));
4545 checkGLcall("glUseProgramObjectARB(programId)");
4547 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
4548 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
4549 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
4550 * vertex shader with fixed function pixel processing is used we make sure that the card
4551 * supports enough samplers to allow the max number of vertex samplers with all possible
4552 * fixed function fragment processing setups. So once the program is linked these samplers
4555 if (vshader
) shader_glsl_load_vsamplers(gl_info
, device
->texUnitMap
, programId
);
4556 if (pshader
) shader_glsl_load_psamplers(gl_info
, device
->texUnitMap
, programId
);
4558 /* If the local constants do not have to be loaded with the environment constants,
4559 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
4562 if (pshader
&& !((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.load_local_constsF
)
4564 hardcode_local_constants((IWineD3DBaseShaderImpl
*) pshader
, gl_info
, programId
, 'P');
4566 if (vshader
&& !((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.load_local_constsF
)
4568 hardcode_local_constants((IWineD3DBaseShaderImpl
*) vshader
, gl_info
, programId
, 'V');
4572 /* GL locking is done by the caller */
4573 static GLhandleARB
create_glsl_blt_shader(const struct wined3d_gl_info
*gl_info
, enum tex_types tex_type
, BOOL masked
)
4575 GLhandleARB program_id
;
4576 GLhandleARB vshader_id
, pshader_id
;
4577 const char *blt_pshader
;
4579 static const char *blt_vshader
[] =
4584 " gl_Position = gl_Vertex;\n"
4585 " gl_FrontColor = vec4(1.0);\n"
4586 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
4590 static const char *blt_pshaders_full
[tex_type_count
] =
4596 "uniform sampler2D sampler;\n"
4599 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4605 "uniform samplerCube sampler;\n"
4608 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4612 "#extension GL_ARB_texture_rectangle : enable\n"
4613 "uniform sampler2DRect sampler;\n"
4616 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4620 static const char *blt_pshaders_masked
[tex_type_count
] =
4626 "uniform sampler2D sampler;\n"
4627 "uniform vec4 mask;\n"
4630 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4631 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
4637 "uniform samplerCube sampler;\n"
4638 "uniform vec4 mask;\n"
4641 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4642 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
4646 "#extension GL_ARB_texture_rectangle : enable\n"
4647 "uniform sampler2DRect sampler;\n"
4648 "uniform vec4 mask;\n"
4651 " if (all(lessThan(gl_FragCoord.xy, mask.zw))) discard;\n"
4652 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
4656 blt_pshader
= masked
? blt_pshaders_masked
[tex_type
] : blt_pshaders_full
[tex_type
];
4659 FIXME("tex_type %#x not supported\n", tex_type
);
4663 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4664 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
4665 GL_EXTCALL(glCompileShaderARB(vshader_id
));
4667 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
4668 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, &blt_pshader
, NULL
));
4669 GL_EXTCALL(glCompileShaderARB(pshader_id
));
4671 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
4672 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
4673 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
4674 GL_EXTCALL(glLinkProgramARB(program_id
));
4676 shader_glsl_validate_link(gl_info
, program_id
);
4678 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
4681 GL_EXTCALL(glDeleteObjectARB(vshader_id
));
4682 GL_EXTCALL(glDeleteObjectARB(pshader_id
));
4686 /* GL locking is done by the caller */
4687 static void shader_glsl_select(const struct wined3d_context
*context
, BOOL usePS
, BOOL useVS
)
4689 const struct wined3d_gl_info
*gl_info
= context
->gl_info
;
4690 IWineD3DDeviceImpl
*device
= context
->swapchain
->device
;
4691 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4692 GLhandleARB program_id
= 0;
4693 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
4695 old_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4697 if (useVS
|| usePS
) set_glsl_shader_program(context
, device
, usePS
, useVS
);
4698 else priv
->glsl_program
= NULL
;
4700 current_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
4702 if (old_vertex_color_clamp
!= current_vertex_color_clamp
)
4704 if (gl_info
->supported
[ARB_COLOR_BUFFER_FLOAT
])
4706 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
4707 checkGLcall("glClampColorARB");
4711 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
4715 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4716 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4717 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4718 checkGLcall("glUseProgramObjectARB");
4720 /* In case that NP2 texcoord fixup data is found for the selected program, trigger a reload of the
4721 * constants. This has to be done because it can't be guaranteed that sampler() (from state.c) is
4722 * called between selecting the shader and using it, which results in wrong fixup for some frames. */
4723 if (priv
->glsl_program
&& priv
->glsl_program
->np2Fixup_info
)
4725 shader_glsl_load_np2fixup_constants((IWineD3DDevice
*)device
, usePS
, useVS
);
4729 /* GL locking is done by the caller */
4730 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
,
4731 enum tex_types tex_type
, const SIZE
*ds_mask_size
)
4733 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4734 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4735 BOOL masked
= ds_mask_size
->cx
&& ds_mask_size
->cy
;
4736 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4737 GLhandleARB
*blt_program
;
4740 blt_program
= masked
? &priv
->depth_blt_program_masked
[tex_type
] : &priv
->depth_blt_program_full
[tex_type
];
4743 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
, masked
);
4744 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "sampler"));
4745 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4746 GL_EXTCALL(glUniform1iARB(loc
, 0));
4750 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
4755 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "mask"));
4756 GL_EXTCALL(glUniform4fARB(loc
, 0.0f
, 0.0f
, (float)ds_mask_size
->cx
, (float)ds_mask_size
->cy
));
4760 /* GL locking is done by the caller */
4761 static void shader_glsl_deselect_depth_blt(IWineD3DDevice
*iface
) {
4762 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4763 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4764 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4765 GLhandleARB program_id
;
4767 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
4768 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
4770 GL_EXTCALL(glUseProgramObjectARB(program_id
));
4771 checkGLcall("glUseProgramObjectARB");
4774 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
4775 const struct list
*linked_programs
;
4776 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
4777 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
4778 struct shader_glsl_priv
*priv
= device
->shader_priv
;
4779 const struct wined3d_gl_info
*gl_info
;
4780 struct wined3d_context
*context
;
4782 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
4783 * can be called from IWineD3DBaseShader::Release
4785 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
4788 struct glsl_pshader_private
*shader_data
;
4789 shader_data
= This
->baseShader
.backend_data
;
4790 if(!shader_data
|| shader_data
->num_gl_shaders
== 0)
4792 HeapFree(GetProcessHeap(), 0, shader_data
);
4793 This
->baseShader
.backend_data
= NULL
;
4797 context
= context_acquire(device
, NULL
);
4798 gl_info
= context
->gl_info
;
4800 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->pshader
== iface
)
4803 shader_glsl_select(context
, FALSE
, FALSE
);
4807 struct glsl_vshader_private
*shader_data
;
4808 shader_data
= This
->baseShader
.backend_data
;
4809 if(!shader_data
|| shader_data
->num_gl_shaders
== 0)
4811 HeapFree(GetProcessHeap(), 0, shader_data
);
4812 This
->baseShader
.backend_data
= NULL
;
4816 context
= context_acquire(device
, NULL
);
4817 gl_info
= context
->gl_info
;
4819 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->vshader
== iface
)
4822 shader_glsl_select(context
, FALSE
, FALSE
);
4827 linked_programs
= &This
->baseShader
.linked_programs
;
4829 TRACE("Deleting linked programs\n");
4830 if (linked_programs
->next
) {
4831 struct glsl_shader_prog_link
*entry
, *entry2
;
4835 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
4836 delete_glsl_program_entry(priv
, gl_info
, entry
);
4839 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
4840 delete_glsl_program_entry(priv
, gl_info
, entry
);
4848 struct glsl_pshader_private
*shader_data
= This
->baseShader
.backend_data
;
4851 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4852 TRACE("deleting pshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4853 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4854 checkGLcall("glDeleteObjectARB");
4857 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4862 struct glsl_vshader_private
*shader_data
= This
->baseShader
.backend_data
;
4865 for(i
= 0; i
< shader_data
->num_gl_shaders
; i
++) {
4866 TRACE("deleting vshader %u\n", shader_data
->gl_shaders
[i
].prgId
);
4867 GL_EXTCALL(glDeleteObjectARB(shader_data
->gl_shaders
[i
].prgId
));
4868 checkGLcall("glDeleteObjectARB");
4871 HeapFree(GetProcessHeap(), 0, shader_data
->gl_shaders
);
4874 HeapFree(GetProcessHeap(), 0, This
->baseShader
.backend_data
);
4875 This
->baseShader
.backend_data
= NULL
;
4877 context_release(context
);
4880 static int glsl_program_key_compare(const void *key
, const struct wine_rb_entry
*entry
)
4882 const glsl_program_key_t
*k
= key
;
4883 const struct glsl_shader_prog_link
*prog
= WINE_RB_ENTRY_VALUE(entry
,
4884 const struct glsl_shader_prog_link
, program_lookup_entry
);
4887 if (k
->vshader
> prog
->vshader
) return 1;
4888 else if (k
->vshader
< prog
->vshader
) return -1;
4890 if (k
->pshader
> prog
->pshader
) return 1;
4891 else if (k
->pshader
< prog
->pshader
) return -1;
4893 if (k
->vshader
&& (cmp
= memcmp(&k
->vs_args
, &prog
->vs_args
, sizeof(prog
->vs_args
)))) return cmp
;
4894 if (k
->pshader
&& (cmp
= memcmp(&k
->ps_args
, &prog
->ps_args
, sizeof(prog
->ps_args
)))) return cmp
;
4899 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
4901 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
) + constant_count
* sizeof(*heap
->positions
);
4902 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
4906 ERR("Failed to allocate memory\n");
4910 heap
->entries
= mem
;
4911 heap
->entries
[1].version
= 0;
4912 heap
->positions
= (unsigned int *)(heap
->entries
+ constant_count
+ 1);
4918 static void constant_heap_free(struct constant_heap
*heap
)
4920 HeapFree(GetProcessHeap(), 0, heap
->entries
);
4923 static const struct wine_rb_functions wined3d_glsl_program_rb_functions
=
4928 glsl_program_key_compare
,
4931 static HRESULT
shader_glsl_alloc(IWineD3DDevice
*iface
) {
4932 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4933 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4934 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
4935 SIZE_T stack_size
= wined3d_log2i(max(gl_info
->limits
.glsl_vs_float_constants
,
4936 gl_info
->limits
.glsl_ps_float_constants
)) + 1;
4938 if (!shader_buffer_init(&priv
->shader_buffer
))
4940 ERR("Failed to initialize shader buffer.\n");
4944 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
4947 ERR("Failed to allocate memory.\n");
4951 if (!constant_heap_init(&priv
->vconst_heap
, gl_info
->limits
.glsl_vs_float_constants
))
4953 ERR("Failed to initialize vertex shader constant heap\n");
4957 if (!constant_heap_init(&priv
->pconst_heap
, gl_info
->limits
.glsl_ps_float_constants
))
4959 ERR("Failed to initialize pixel shader constant heap\n");
4963 if (wine_rb_init(&priv
->program_lookup
, &wined3d_glsl_program_rb_functions
) == -1)
4965 ERR("Failed to initialize rbtree.\n");
4969 priv
->next_constant_version
= 1;
4971 This
->shader_priv
= priv
;
4975 constant_heap_free(&priv
->pconst_heap
);
4976 constant_heap_free(&priv
->vconst_heap
);
4977 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4978 shader_buffer_free(&priv
->shader_buffer
);
4979 HeapFree(GetProcessHeap(), 0, priv
);
4980 return E_OUTOFMEMORY
;
4983 /* Context activation is done by the caller. */
4984 static void shader_glsl_free(IWineD3DDevice
*iface
) {
4985 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4986 const struct wined3d_gl_info
*gl_info
= &This
->adapter
->gl_info
;
4987 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4991 for (i
= 0; i
< tex_type_count
; ++i
)
4993 if (priv
->depth_blt_program_full
[i
])
4995 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program_full
[i
]));
4997 if (priv
->depth_blt_program_masked
[i
])
4999 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program_masked
[i
]));
5004 wine_rb_destroy(&priv
->program_lookup
, NULL
, NULL
);
5005 constant_heap_free(&priv
->pconst_heap
);
5006 constant_heap_free(&priv
->vconst_heap
);
5007 HeapFree(GetProcessHeap(), 0, priv
->stack
);
5008 shader_buffer_free(&priv
->shader_buffer
);
5010 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
5011 This
->shader_priv
= NULL
;
5014 static BOOL
shader_glsl_dirty_const(IWineD3DDevice
*iface
) {
5015 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
5019 static void shader_glsl_get_caps(const struct wined3d_gl_info
*gl_info
, struct shader_caps
*pCaps
)
5021 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
5022 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support based
5023 * on the version of NV_vertex_program.
5024 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
5025 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
5026 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
5027 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
5029 if ((gl_info
->supported
[NV_VERTEX_PROGRAM2
] && !gl_info
->supported
[NV_VERTEX_PROGRAM3
])
5030 || gl_info
->limits
.arb_ps_instructions
<= 512)
5031 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
5033 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
5034 TRACE_(d3d_caps
)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps
->VertexShaderVersion
>> 8) & 0xff, pCaps
->VertexShaderVersion
& 0xff);
5035 pCaps
->MaxVertexShaderConst
= gl_info
->limits
.glsl_vs_float_constants
;
5037 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
5038 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
5039 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
5040 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
5041 * in max native instructions. Intel and others also offer the info in this extension but they
5042 * don't support GLSL (at least on Windows).
5044 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
5045 * of instructions is 512 or less we have to do with ps2.0 hardware.
5046 * 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.
5048 if ((gl_info
->supported
[NV_FRAGMENT_PROGRAM
] && !gl_info
->supported
[NV_FRAGMENT_PROGRAM2
])
5049 || gl_info
->limits
.arb_ps_instructions
<= 512)
5050 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
5052 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
5054 pCaps
->MaxPixelShaderConst
= gl_info
->limits
.glsl_ps_float_constants
;
5056 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
5057 * Direct3D minimum requirement.
5059 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
5060 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
5062 * The problem is that the refrast clamps temporary results in the shader to
5063 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
5064 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
5065 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
5066 * offer a way to query this.
5068 pCaps
->PixelShader1xMaxValue
= 8.0;
5069 TRACE_(d3d_caps
)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps
->PixelShaderVersion
>> 8) & 0xff, pCaps
->PixelShaderVersion
& 0xff);
5071 pCaps
->VSClipping
= TRUE
;
5074 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
5076 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
5078 TRACE("Checking support for fixup:\n");
5079 dump_color_fixup_desc(fixup
);
5082 /* We support everything except YUV conversions. */
5083 if (!is_complex_fixup(fixup
))
5089 TRACE("[FAILED]\n");
5093 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
5095 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
5096 /* WINED3DSIH_ADD */ shader_glsl_arith
,
5097 /* WINED3DSIH_BEM */ shader_glsl_bem
,
5098 /* WINED3DSIH_BREAK */ shader_glsl_break
,
5099 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
5100 /* WINED3DSIH_BREAKP */ NULL
,
5101 /* WINED3DSIH_CALL */ shader_glsl_call
,
5102 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
5103 /* WINED3DSIH_CMP */ shader_glsl_cmp
,
5104 /* WINED3DSIH_CND */ shader_glsl_cnd
,
5105 /* WINED3DSIH_CRS */ shader_glsl_cross
,
5106 /* WINED3DSIH_CUT */ NULL
,
5107 /* WINED3DSIH_DCL */ NULL
,
5108 /* WINED3DSIH_DEF */ NULL
,
5109 /* WINED3DSIH_DEFB */ NULL
,
5110 /* WINED3DSIH_DEFI */ NULL
,
5111 /* WINED3DSIH_DP2ADD */ shader_glsl_dp2add
,
5112 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
5113 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
5114 /* WINED3DSIH_DST */ shader_glsl_dst
,
5115 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
5116 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
5117 /* WINED3DSIH_ELSE */ shader_glsl_else
,
5118 /* WINED3DSIH_EMIT */ NULL
,
5119 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
5120 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
5121 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
5122 /* WINED3DSIH_EXP */ shader_glsl_map2gl
,
5123 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
5124 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
5125 /* WINED3DSIH_IADD */ NULL
,
5126 /* WINED3DSIH_IF */ shader_glsl_if
,
5127 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
5128 /* WINED3DSIH_IGE */ NULL
,
5129 /* WINED3DSIH_LABEL */ shader_glsl_label
,
5130 /* WINED3DSIH_LIT */ shader_glsl_lit
,
5131 /* WINED3DSIH_LOG */ shader_glsl_log
,
5132 /* WINED3DSIH_LOGP */ shader_glsl_log
,
5133 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
5134 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
5135 /* WINED3DSIH_LT */ NULL
,
5136 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
5137 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
5138 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
5139 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
5140 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
5141 /* WINED3DSIH_MAD */ shader_glsl_mad
,
5142 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
5143 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
5144 /* WINED3DSIH_MOV */ shader_glsl_mov
,
5145 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
5146 /* WINED3DSIH_MUL */ shader_glsl_arith
,
5147 /* WINED3DSIH_NOP */ NULL
,
5148 /* WINED3DSIH_NRM */ shader_glsl_nrm
,
5149 /* WINED3DSIH_PHASE */ NULL
,
5150 /* WINED3DSIH_POW */ shader_glsl_pow
,
5151 /* WINED3DSIH_RCP */ shader_glsl_rcp
,
5152 /* WINED3DSIH_REP */ shader_glsl_rep
,
5153 /* WINED3DSIH_RET */ shader_glsl_ret
,
5154 /* WINED3DSIH_RSQ */ shader_glsl_rsq
,
5155 /* WINED3DSIH_SETP */ NULL
,
5156 /* WINED3DSIH_SGE */ shader_glsl_compare
,
5157 /* WINED3DSIH_SGN */ shader_glsl_sgn
,
5158 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
5159 /* WINED3DSIH_SLT */ shader_glsl_compare
,
5160 /* WINED3DSIH_SUB */ shader_glsl_arith
,
5161 /* WINED3DSIH_TEX */ shader_glsl_tex
,
5162 /* WINED3DSIH_TEXBEM */ shader_glsl_texbem
,
5163 /* WINED3DSIH_TEXBEML */ shader_glsl_texbem
,
5164 /* WINED3DSIH_TEXCOORD */ shader_glsl_texcoord
,
5165 /* WINED3DSIH_TEXDEPTH */ shader_glsl_texdepth
,
5166 /* WINED3DSIH_TEXDP3 */ shader_glsl_texdp3
,
5167 /* WINED3DSIH_TEXDP3TEX */ shader_glsl_texdp3tex
,
5168 /* WINED3DSIH_TEXKILL */ shader_glsl_texkill
,
5169 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
5170 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
5171 /* WINED3DSIH_TEXM3x2DEPTH */ shader_glsl_texm3x2depth
,
5172 /* WINED3DSIH_TEXM3x2PAD */ shader_glsl_texm3x2pad
,
5173 /* WINED3DSIH_TEXM3x2TEX */ shader_glsl_texm3x2tex
,
5174 /* WINED3DSIH_TEXM3x3 */ shader_glsl_texm3x3
,
5175 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
5176 /* WINED3DSIH_TEXM3x3PAD */ shader_glsl_texm3x3pad
,
5177 /* WINED3DSIH_TEXM3x3SPEC */ shader_glsl_texm3x3spec
,
5178 /* WINED3DSIH_TEXM3x3TEX */ shader_glsl_texm3x3tex
,
5179 /* WINED3DSIH_TEXM3x3VSPEC */ shader_glsl_texm3x3vspec
,
5180 /* WINED3DSIH_TEXREG2AR */ shader_glsl_texreg2ar
,
5181 /* WINED3DSIH_TEXREG2GB */ shader_glsl_texreg2gb
,
5182 /* WINED3DSIH_TEXREG2RGB */ shader_glsl_texreg2rgb
,
5185 static void shader_glsl_handle_instruction(const struct wined3d_shader_instruction
*ins
) {
5186 SHADER_HANDLER hw_fct
;
5188 /* Select handler */
5189 hw_fct
= shader_glsl_instruction_handler_table
[ins
->handler_idx
];
5191 /* Unhandled opcode */
5194 FIXME("Backend can't handle opcode %#x\n", ins
->handler_idx
);
5199 shader_glsl_add_instruction_modifiers(ins
);
5202 const shader_backend_t glsl_shader_backend
= {
5203 shader_glsl_handle_instruction
,
5205 shader_glsl_select_depth_blt
,
5206 shader_glsl_deselect_depth_blt
,
5207 shader_glsl_update_float_vertex_constants
,
5208 shader_glsl_update_float_pixel_constants
,
5209 shader_glsl_load_constants
,
5210 shader_glsl_load_np2fixup_constants
,
5211 shader_glsl_destroy
,
5214 shader_glsl_dirty_const
,
5215 shader_glsl_get_caps
,
5216 shader_glsl_color_fixup_supported
,