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 hash_table_t
*glsl_program_lookup
;
87 struct glsl_shader_prog_link
*glsl_program
;
88 struct constant_heap vconst_heap
;
89 struct constant_heap pconst_heap
;
91 GLhandleARB depth_blt_program
[tex_type_count
];
92 UINT next_constant_version
;
95 /* Struct to maintain data about a linked GLSL program */
96 struct glsl_shader_prog_link
{
97 struct list vshader_entry
;
98 struct list pshader_entry
;
99 GLhandleARB programId
;
100 GLint
*vuniformF_locations
;
101 GLint
*puniformF_locations
;
102 GLint vuniformI_locations
[MAX_CONST_I
];
103 GLint puniformI_locations
[MAX_CONST_I
];
104 GLint posFixup_location
;
105 GLint np2Fixup_location
[MAX_FRAGMENT_SAMPLERS
];
106 GLint bumpenvmat_location
[MAX_TEXTURES
];
107 GLint luminancescale_location
[MAX_TEXTURES
];
108 GLint luminanceoffset_location
[MAX_TEXTURES
];
109 GLint ycorrection_location
;
110 GLenum vertex_color_clamp
;
111 IWineD3DVertexShader
*vshader
;
112 IWineD3DPixelShader
*pshader
;
113 struct vs_compile_args vs_args
;
114 struct ps_compile_args ps_args
;
115 UINT constant_version
;
119 IWineD3DVertexShader
*vshader
;
120 IWineD3DPixelShader
*pshader
;
121 struct ps_compile_args ps_args
;
122 struct vs_compile_args vs_args
;
123 } glsl_program_key_t
;
126 /** Prints the GLSL info log which will contain error messages if they exist */
127 /* GL locking is done by the caller */
128 static void print_glsl_info_log(const WineD3D_GL_Info
*gl_info
, GLhandleARB obj
)
130 int infologLength
= 0;
135 static const char * const spam
[] =
137 "Vertex shader was successfully compiled to run on hardware.\n", /* fglrx */
138 "Fragment shader was successfully compiled to run on hardware.\n", /* fglrx */
139 "Fragment shader(s) linked, vertex shader(s) linked. \n ", /* fglrx, with \n */
140 "Fragment shader(s) linked, vertex shader(s) linked.", /* fglrx, no \n */
141 "Vertex shader(s) linked, no fragment shader(s) defined. \n ", /* fglrx, with \n */
142 "Vertex shader(s) linked, no fragment shader(s) defined.", /* fglrx, no \n */
143 "Fragment shader was successfully compiled to run on hardware.\n"
144 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported",
145 "Fragment shader(s) linked, no vertex shader(s) defined.", /* fglrx, no \n */
146 "Fragment shader(s) linked, no vertex shader(s) defined. \n ", /* fglrx, with \n */
147 "WARNING: 0:2: extension 'GL_ARB_draw_buffers' is not supported\n" /* MacOS ati */
150 if (!TRACE_ON(d3d_shader
) && !FIXME_ON(d3d_shader
)) return;
152 GL_EXTCALL(glGetObjectParameterivARB(obj
,
153 GL_OBJECT_INFO_LOG_LENGTH_ARB
,
156 /* A size of 1 is just a null-terminated string, so the log should be bigger than
157 * that if there are errors. */
158 if (infologLength
> 1)
160 /* Fglrx doesn't terminate the string properly, but it tells us the proper length.
161 * So use HEAP_ZERO_MEMORY to avoid uninitialized bytes
163 infoLog
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, infologLength
);
164 GL_EXTCALL(glGetInfoLogARB(obj
, infologLength
, NULL
, infoLog
));
167 for(i
= 0; i
< sizeof(spam
) / sizeof(spam
[0]); i
++) {
168 if(strcmp(infoLog
, spam
[i
]) == 0) {
174 TRACE("Spam received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
176 FIXME("Error received from GLSL shader #%u: %s\n", obj
, debugstr_a(infoLog
));
178 HeapFree(GetProcessHeap(), 0, infoLog
);
183 * Loads (pixel shader) samplers
185 /* GL locking is done by the caller */
186 static void shader_glsl_load_psamplers(const WineD3D_GL_Info
*gl_info
, DWORD
*tex_unit_map
, GLhandleARB programId
)
190 char sampler_name
[20];
192 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
193 snprintf(sampler_name
, sizeof(sampler_name
), "Psampler%d", i
);
194 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
195 if (name_loc
!= -1) {
196 DWORD mapped_unit
= tex_unit_map
[i
];
197 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< GL_LIMITS(fragment_samplers
))
199 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
200 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
201 checkGLcall("glUniform1iARB");
203 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
209 /* GL locking is done by the caller */
210 static void shader_glsl_load_vsamplers(const WineD3D_GL_Info
*gl_info
, DWORD
*tex_unit_map
, GLhandleARB programId
)
213 char sampler_name
[20];
216 for (i
= 0; i
< MAX_VERTEX_SAMPLERS
; ++i
) {
217 snprintf(sampler_name
, sizeof(sampler_name
), "Vsampler%d", i
);
218 name_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, sampler_name
));
219 if (name_loc
!= -1) {
220 DWORD mapped_unit
= tex_unit_map
[MAX_FRAGMENT_SAMPLERS
+ i
];
221 if (mapped_unit
!= WINED3D_UNMAPPED_STAGE
&& mapped_unit
< GL_LIMITS(combined_samplers
))
223 TRACE("Loading %s for texture %d\n", sampler_name
, mapped_unit
);
224 GL_EXTCALL(glUniform1iARB(name_loc
, mapped_unit
));
225 checkGLcall("glUniform1iARB");
227 ERR("Trying to load sampler %s on unsupported unit %d\n", sampler_name
, mapped_unit
);
233 /* GL locking is done by the caller */
234 static inline void walk_constant_heap(const WineD3D_GL_Info
*gl_info
, const float *constants
,
235 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
238 unsigned int heap_idx
= 1;
241 if (heap
->entries
[heap_idx
].version
<= version
) return;
243 idx
= heap
->entries
[heap_idx
].idx
;
244 if (constant_locations
[idx
] != -1) GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
245 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
247 while (stack_idx
>= 0)
249 /* Note that we fall through to the next case statement. */
250 switch(stack
[stack_idx
])
252 case HEAP_NODE_TRAVERSE_LEFT
:
254 unsigned int left_idx
= heap_idx
<< 1;
255 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
258 idx
= heap
->entries
[heap_idx
].idx
;
259 if (constant_locations
[idx
] != -1)
260 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
262 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
263 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
268 case HEAP_NODE_TRAVERSE_RIGHT
:
270 unsigned int right_idx
= (heap_idx
<< 1) + 1;
271 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
273 heap_idx
= right_idx
;
274 idx
= heap
->entries
[heap_idx
].idx
;
275 if (constant_locations
[idx
] != -1)
276 GL_EXTCALL(glUniform4fvARB(constant_locations
[idx
], 1, &constants
[idx
* 4]));
278 stack
[stack_idx
++] = HEAP_NODE_POP
;
279 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
292 checkGLcall("walk_constant_heap()");
295 /* GL locking is done by the caller */
296 static inline void apply_clamped_constant(const WineD3D_GL_Info
*gl_info
, GLint location
, const GLfloat
*data
)
298 GLfloat clamped_constant
[4];
300 if (location
== -1) return;
302 clamped_constant
[0] = data
[0] < -1.0f
? -1.0f
: data
[0] > 1.0 ? 1.0 : data
[0];
303 clamped_constant
[1] = data
[1] < -1.0f
? -1.0f
: data
[1] > 1.0 ? 1.0 : data
[1];
304 clamped_constant
[2] = data
[2] < -1.0f
? -1.0f
: data
[2] > 1.0 ? 1.0 : data
[2];
305 clamped_constant
[3] = data
[3] < -1.0f
? -1.0f
: data
[3] > 1.0 ? 1.0 : data
[3];
307 GL_EXTCALL(glUniform4fvARB(location
, 1, clamped_constant
));
310 /* GL locking is done by the caller */
311 static inline void walk_constant_heap_clamped(const WineD3D_GL_Info
*gl_info
, const float *constants
,
312 const GLint
*constant_locations
, const struct constant_heap
*heap
, unsigned char *stack
, DWORD version
)
315 unsigned int heap_idx
= 1;
318 if (heap
->entries
[heap_idx
].version
<= version
) return;
320 idx
= heap
->entries
[heap_idx
].idx
;
321 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
322 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
324 while (stack_idx
>= 0)
326 /* Note that we fall through to the next case statement. */
327 switch(stack
[stack_idx
])
329 case HEAP_NODE_TRAVERSE_LEFT
:
331 unsigned int left_idx
= heap_idx
<< 1;
332 if (left_idx
< heap
->size
&& heap
->entries
[left_idx
].version
> version
)
335 idx
= heap
->entries
[heap_idx
].idx
;
336 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
338 stack
[stack_idx
++] = HEAP_NODE_TRAVERSE_RIGHT
;
339 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
344 case HEAP_NODE_TRAVERSE_RIGHT
:
346 unsigned int right_idx
= (heap_idx
<< 1) + 1;
347 if (right_idx
< heap
->size
&& heap
->entries
[right_idx
].version
> version
)
349 heap_idx
= right_idx
;
350 idx
= heap
->entries
[heap_idx
].idx
;
351 apply_clamped_constant(gl_info
, constant_locations
[idx
], &constants
[idx
* 4]);
353 stack
[stack_idx
++] = HEAP_NODE_POP
;
354 stack
[stack_idx
] = HEAP_NODE_TRAVERSE_LEFT
;
367 checkGLcall("walk_constant_heap_clamped()");
370 /* Loads floating point constants (aka uniforms) into the currently set GLSL program. */
371 /* GL locking is done by the caller */
372 static void shader_glsl_load_constantsF(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
373 const float *constants
, const GLint
*constant_locations
, const struct constant_heap
*heap
,
374 unsigned char *stack
, UINT version
)
376 const local_constant
*lconst
;
378 /* 1.X pshaders have the constants clamped to [-1;1] implicitly. */
379 if (This
->baseShader
.reg_maps
.shader_version
.major
== 1
380 && shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
))
381 walk_constant_heap_clamped(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
383 walk_constant_heap(gl_info
, constants
, constant_locations
, heap
, stack
, version
);
385 if (!This
->baseShader
.load_local_constsF
)
387 TRACE("No need to load local float constants for this shader\n");
391 /* Immediate constants are clamped to [-1;1] at shader creation time if needed */
392 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
)
394 GLint location
= constant_locations
[lconst
->idx
];
395 /* We found this uniform name in the program - go ahead and send the data */
396 if (location
!= -1) GL_EXTCALL(glUniform4fvARB(location
, 1, (const GLfloat
*)lconst
->value
));
398 checkGLcall("glUniform4fvARB()");
401 /* Loads integer constants (aka uniforms) into the currently set GLSL program. */
402 /* GL locking is done by the caller */
403 static void shader_glsl_load_constantsI(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
404 const GLint locations
[MAX_CONST_I
], const int *constants
, WORD constants_set
)
409 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
411 if (!(constants_set
& 1)) continue;
413 TRACE_(d3d_constants
)("Loading constants %u: %i, %i, %i, %i\n",
414 i
, constants
[i
*4], constants
[i
*4+1], constants
[i
*4+2], constants
[i
*4+3]);
416 /* We found this uniform name in the program - go ahead and send the data */
417 GL_EXTCALL(glUniform4ivARB(locations
[i
], 1, &constants
[i
*4]));
418 checkGLcall("glUniform4ivARB");
421 /* Load immediate constants */
422 ptr
= list_head(&This
->baseShader
.constantsI
);
424 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
425 unsigned int idx
= lconst
->idx
;
426 const GLint
*values
= (const GLint
*)lconst
->value
;
428 TRACE_(d3d_constants
)("Loading local constants %i: %i, %i, %i, %i\n", idx
,
429 values
[0], values
[1], values
[2], values
[3]);
431 /* We found this uniform name in the program - go ahead and send the data */
432 GL_EXTCALL(glUniform4ivARB(locations
[idx
], 1, values
));
433 checkGLcall("glUniform4ivARB");
434 ptr
= list_next(&This
->baseShader
.constantsI
, ptr
);
438 /* Loads boolean constants (aka uniforms) into the currently set GLSL program. */
439 /* GL locking is done by the caller */
440 static void shader_glsl_load_constantsB(IWineD3DBaseShaderImpl
*This
, const WineD3D_GL_Info
*gl_info
,
441 GLhandleARB programId
, const BOOL
*constants
, WORD constants_set
)
446 char is_pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
447 const char* prefix
= is_pshader
? "PB":"VB";
450 /* TODO: Benchmark and see if it would be beneficial to store the
451 * locations of the constants to avoid looking up each time */
452 for (i
= 0; constants_set
; constants_set
>>= 1, ++i
)
454 if (!(constants_set
& 1)) continue;
456 TRACE_(d3d_constants
)("Loading constants %i: %i;\n", i
, constants
[i
]);
458 /* TODO: Benchmark and see if it would be beneficial to store the
459 * locations of the constants to avoid looking up each time */
460 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, i
);
461 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
464 /* We found this uniform name in the program - go ahead and send the data */
465 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, &constants
[i
]));
466 checkGLcall("glUniform1ivARB");
470 /* Load immediate constants */
471 ptr
= list_head(&This
->baseShader
.constantsB
);
473 const struct local_constant
*lconst
= LIST_ENTRY(ptr
, const struct local_constant
, entry
);
474 unsigned int idx
= lconst
->idx
;
475 const GLint
*values
= (const GLint
*)lconst
->value
;
477 TRACE_(d3d_constants
)("Loading local constants %i: %i\n", idx
, values
[0]);
479 snprintf(tmp_name
, sizeof(tmp_name
), "%s[%i]", prefix
, idx
);
480 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, tmp_name
));
482 /* We found this uniform name in the program - go ahead and send the data */
483 GL_EXTCALL(glUniform1ivARB(tmp_loc
, 1, values
));
484 checkGLcall("glUniform1ivARB");
486 ptr
= list_next(&This
->baseShader
.constantsB
, ptr
);
490 static void reset_program_constant_version(void *value
, void *context
)
492 struct glsl_shader_prog_link
*entry
= value
;
493 entry
->constant_version
= 0;
497 * Loads the texture dimensions for NP2 fixup into the currently set GLSL program.
499 /* GL locking is done by the caller (state handler) */
500 static void shader_glsl_load_np2fixup_constants(
501 IWineD3DDevice
* device
,
503 char useVertexShader
) {
505 const IWineD3DDeviceImpl
* deviceImpl
= (const IWineD3DDeviceImpl
*) device
;
506 const struct glsl_shader_prog_link
* prog
= ((struct shader_glsl_priv
*)(deviceImpl
->shader_priv
))->glsl_program
;
509 /* No GLSL program set - nothing to do. */
513 if (!usePixelShader
) {
514 /* NP2 texcoord fixup is (currently) only done for pixelshaders. */
518 if (prog
->ps_args
.np2_fixup
) {
520 UINT fixup
= prog
->ps_args
.np2_fixup
;
521 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
522 const IWineD3DStateBlockImpl
* stateBlock
= (const IWineD3DStateBlockImpl
*) deviceImpl
->stateBlock
;
524 for (i
= 0; fixup
; fixup
>>= 1, ++i
) {
525 if (-1 != prog
->np2Fixup_location
[i
]) {
526 const IWineD3DBaseTextureImpl
* const tex
= (const IWineD3DBaseTextureImpl
*) stateBlock
->textures
[i
];
528 FIXME("Nonexistent texture is flagged for NP2 texcoord fixup\n");
531 const float tex_dim
[2] = {tex
->baseTexture
.pow2Matrix
[0], tex
->baseTexture
.pow2Matrix
[5]};
532 GL_EXTCALL(glUniform2fvARB(prog
->np2Fixup_location
[i
], 1, tex_dim
));
540 * Loads the app-supplied constants into the currently set GLSL program.
542 /* GL locking is done by the caller (state handler) */
543 static void shader_glsl_load_constants(
544 IWineD3DDevice
* device
,
546 char useVertexShader
) {
548 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) device
;
549 struct shader_glsl_priv
*priv
= deviceImpl
->shader_priv
;
550 IWineD3DStateBlockImpl
* stateBlock
= deviceImpl
->stateBlock
;
551 const WineD3D_GL_Info
*gl_info
= &deviceImpl
->adapter
->gl_info
;
553 GLhandleARB programId
;
554 struct glsl_shader_prog_link
*prog
= priv
->glsl_program
;
555 UINT constant_version
;
559 /* No GLSL program set - nothing to do. */
562 programId
= prog
->programId
;
563 constant_version
= prog
->constant_version
;
565 if (useVertexShader
) {
566 IWineD3DBaseShaderImpl
* vshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->vertexShader
;
568 /* Load DirectX 9 float constants/uniforms for vertex shader */
569 shader_glsl_load_constantsF(vshader
, gl_info
, stateBlock
->vertexShaderConstantF
,
570 prog
->vuniformF_locations
, &priv
->vconst_heap
, priv
->stack
, constant_version
);
572 /* Load DirectX 9 integer constants/uniforms for vertex shader */
573 shader_glsl_load_constantsI(vshader
, gl_info
, prog
->vuniformI_locations
, stateBlock
->vertexShaderConstantI
,
574 stateBlock
->changed
.vertexShaderConstantsI
& vshader
->baseShader
.reg_maps
.integer_constants
);
576 /* Load DirectX 9 boolean constants/uniforms for vertex shader */
577 shader_glsl_load_constantsB(vshader
, gl_info
, programId
, stateBlock
->vertexShaderConstantB
,
578 stateBlock
->changed
.vertexShaderConstantsB
& vshader
->baseShader
.reg_maps
.boolean_constants
);
580 /* Upload the position fixup params */
581 GL_EXTCALL(glUniform4fvARB(prog
->posFixup_location
, 1, &deviceImpl
->posFixup
[0]));
582 checkGLcall("glUniform4fvARB");
585 if (usePixelShader
) {
587 IWineD3DBaseShaderImpl
* pshader
= (IWineD3DBaseShaderImpl
*) stateBlock
->pixelShader
;
589 /* Load DirectX 9 float constants/uniforms for pixel shader */
590 shader_glsl_load_constantsF(pshader
, gl_info
, stateBlock
->pixelShaderConstantF
,
591 prog
->puniformF_locations
, &priv
->pconst_heap
, priv
->stack
, constant_version
);
593 /* Load DirectX 9 integer constants/uniforms for pixel shader */
594 shader_glsl_load_constantsI(pshader
, gl_info
, prog
->puniformI_locations
, stateBlock
->pixelShaderConstantI
,
595 stateBlock
->changed
.pixelShaderConstantsI
& pshader
->baseShader
.reg_maps
.integer_constants
);
597 /* Load DirectX 9 boolean constants/uniforms for pixel shader */
598 shader_glsl_load_constantsB(pshader
, gl_info
, programId
, stateBlock
->pixelShaderConstantB
,
599 stateBlock
->changed
.pixelShaderConstantsB
& pshader
->baseShader
.reg_maps
.boolean_constants
);
601 /* Upload the environment bump map matrix if needed. The needsbumpmat member specifies the texture stage to load the matrix from.
602 * It can't be 0 for a valid texbem instruction.
604 for(i
= 0; i
< ((IWineD3DPixelShaderImpl
*) pshader
)->numbumpenvmatconsts
; i
++) {
605 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pshader
;
606 int stage
= ps
->luminanceconst
[i
].texunit
;
608 const float *data
= (const float *)&stateBlock
->textureState
[(int)ps
->bumpenvmatconst
[i
].texunit
][WINED3DTSS_BUMPENVMAT00
];
609 GL_EXTCALL(glUniformMatrix2fvARB(prog
->bumpenvmat_location
[i
], 1, 0, data
));
610 checkGLcall("glUniformMatrix2fvARB");
612 /* texbeml needs the luminance scale and offset too. If texbeml is used, needsbumpmat
613 * is set too, so we can check that in the needsbumpmat check
615 if(ps
->baseShader
.reg_maps
.luminanceparams
[stage
]) {
616 const GLfloat
*scale
= (const GLfloat
*)&stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLSCALE
];
617 const GLfloat
*offset
= (const GLfloat
*)&stateBlock
->textureState
[stage
][WINED3DTSS_BUMPENVLOFFSET
];
619 GL_EXTCALL(glUniform1fvARB(prog
->luminancescale_location
[i
], 1, scale
));
620 checkGLcall("glUniform1fvARB");
621 GL_EXTCALL(glUniform1fvARB(prog
->luminanceoffset_location
[i
], 1, offset
));
622 checkGLcall("glUniform1fvARB");
626 if(((IWineD3DPixelShaderImpl
*) pshader
)->vpos_uniform
) {
627 float correction_params
[4];
628 if(deviceImpl
->render_offscreen
) {
629 correction_params
[0] = 0.0;
630 correction_params
[1] = 1.0;
632 /* position is window relative, not viewport relative */
633 correction_params
[0] = ((IWineD3DSurfaceImpl
*) deviceImpl
->render_targets
[0])->currentDesc
.Height
;
634 correction_params
[1] = -1.0;
636 GL_EXTCALL(glUniform4fvARB(prog
->ycorrection_location
, 1, correction_params
));
640 if (priv
->next_constant_version
== UINT_MAX
)
642 TRACE("Max constant version reached, resetting to 0.\n");
643 hash_table_for_each_entry(priv
->glsl_program_lookup
, reset_program_constant_version
, NULL
);
644 priv
->next_constant_version
= 1;
648 prog
->constant_version
= priv
->next_constant_version
++;
652 static inline void update_heap_entry(struct constant_heap
*heap
, unsigned int idx
,
653 unsigned int heap_idx
, DWORD new_version
)
655 struct constant_entry
*entries
= heap
->entries
;
656 unsigned int *positions
= heap
->positions
;
657 unsigned int parent_idx
;
661 parent_idx
= heap_idx
>> 1;
663 if (new_version
<= entries
[parent_idx
].version
) break;
665 entries
[heap_idx
] = entries
[parent_idx
];
666 positions
[entries
[parent_idx
].idx
] = heap_idx
;
667 heap_idx
= parent_idx
;
670 entries
[heap_idx
].version
= new_version
;
671 entries
[heap_idx
].idx
= idx
;
672 positions
[idx
] = heap_idx
;
675 static void shader_glsl_update_float_vertex_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
677 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
678 struct shader_glsl_priv
*priv
= This
->shader_priv
;
679 struct constant_heap
*heap
= &priv
->vconst_heap
;
682 for (i
= start
; i
< count
+ start
; ++i
)
684 if (!This
->stateBlock
->changed
.vertexShaderConstantsF
[i
])
685 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
687 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
691 static void shader_glsl_update_float_pixel_constants(IWineD3DDevice
*iface
, UINT start
, UINT count
)
693 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
694 struct shader_glsl_priv
*priv
= This
->shader_priv
;
695 struct constant_heap
*heap
= &priv
->pconst_heap
;
698 for (i
= start
; i
< count
+ start
; ++i
)
700 if (!This
->stateBlock
->changed
.pixelShaderConstantsF
[i
])
701 update_heap_entry(heap
, i
, heap
->size
++, priv
->next_constant_version
);
703 update_heap_entry(heap
, i
, heap
->positions
[i
], priv
->next_constant_version
);
707 /** Generate the variable & register declarations for the GLSL output target */
708 static void shader_generate_glsl_declarations(IWineD3DBaseShader
*iface
, const shader_reg_maps
*reg_maps
,
709 SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
,
710 const struct ps_compile_args
*ps_args
)
712 IWineD3DBaseShaderImpl
* This
= (IWineD3DBaseShaderImpl
*) iface
;
713 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
714 unsigned int i
, extra_constants_needed
= 0;
715 const local_constant
*lconst
;
717 /* There are some minor differences between pixel and vertex shaders */
718 char pshader
= shader_is_pshader_version(reg_maps
->shader_version
.type
);
719 char prefix
= pshader
? 'P' : 'V';
721 /* Prototype the subroutines */
722 for (i
= 0; i
< This
->baseShader
.limits
.label
; i
++) {
723 if (reg_maps
->labels
[i
])
724 shader_addline(buffer
, "void subroutine%u();\n", i
);
727 /* Declare the constants (aka uniforms) */
728 if (This
->baseShader
.limits
.constant_float
> 0) {
729 unsigned max_constantsF
;
730 /* Unless the shader uses indirect addressing, always declare the maximum array size and ignore that we need some
731 * uniforms privately. E.g. if GL supports 256 uniforms, and we need 2 for the pos fixup and immediate values, still
732 * declare VC[256]. If the shader needs more uniforms than we have it won't work in any case. If it uses less, the
733 * compiler will figure out which uniforms are really used and strip them out. This allows a shader to use c255 on
734 * a dx9 card, as long as it doesn't also use all the other constants.
736 * If the shader uses indirect addressing the compiler must assume that all declared uniforms are used. In this case,
737 * declare only the amount that we're assured to have.
739 * Thus we run into problems in these two cases:
740 * 1) The shader really uses more uniforms than supported
741 * 2) The shader uses indirect addressing, less constants than supported, but uses a constant index > #supported consts
744 /* No indirect addressing here */
745 max_constantsF
= GL_LIMITS(pshader_constantsF
);
747 if(This
->baseShader
.reg_maps
.usesrelconstF
) {
748 /* Subtract the other potential uniforms from the max available (bools, ints, and 1 row of projection matrix).
749 * Subtract another uniform for immediate values, which have to be loaded via uniform by the driver as well.
750 * The shader code only uses 0.5, 2.0, 1.0, 128 and -128 in vertex shader code, so one vec4 should be enough
751 * (Unfortunately the Nvidia driver doesn't store 128 and -128 in one float
753 max_constantsF
= GL_LIMITS(vshader_constantsF
) - 3;
754 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.integer_constants
);
755 /* Strictly speaking a bool only uses one scalar, but the nvidia(Linux) compiler doesn't pack them properly,
756 * so each scalar requires a full vec4. We could work around this by packing the booleans ourselves, but
757 * for now take this into account when calculating the number of available constants
759 max_constantsF
-= count_bits(This
->baseShader
.reg_maps
.boolean_constants
);
760 /* Set by driver quirks in directx.c */
761 max_constantsF
-= GLINFO_LOCATION
.reserved_glsl_constants
;
763 max_constantsF
= GL_LIMITS(vshader_constantsF
);
766 max_constantsF
= min(This
->baseShader
.limits
.constant_float
, max_constantsF
);
767 shader_addline(buffer
, "uniform vec4 %cC[%u];\n", prefix
, max_constantsF
);
770 /* Always declare the full set of constants, the compiler can remove the unused ones because d3d doesn't(yet)
771 * support indirect int and bool constant addressing. This avoids problems if the app uses e.g. i0 and i9.
773 if (This
->baseShader
.limits
.constant_int
> 0 && This
->baseShader
.reg_maps
.integer_constants
)
774 shader_addline(buffer
, "uniform ivec4 %cI[%u];\n", prefix
, This
->baseShader
.limits
.constant_int
);
776 if (This
->baseShader
.limits
.constant_bool
> 0 && This
->baseShader
.reg_maps
.boolean_constants
)
777 shader_addline(buffer
, "uniform bool %cB[%u];\n", prefix
, This
->baseShader
.limits
.constant_bool
);
780 shader_addline(buffer
, "uniform vec4 posFixup;\n");
781 /* Predeclaration; This function is added at link time based on the pixel shader.
782 * VS 3.0 shaders have an array OUT[] the shader writes to, earlier versions don't have
783 * that. We know the input to the reorder function at vertex shader compile time, so
784 * we can deal with that. The reorder function for a 1.x and 2.x vertex shader can just
785 * read gl_FrontColor. The output depends on the pixel shader. The reorder function for a
786 * 1.x and 2.x pshader or for fixed function will write gl_FrontColor, and for a 3.0 shader
787 * it will write to the varying array. Here we depend on the shader optimizer on sorting that
788 * out. The nvidia driver only does that if the parameter is inout instead of out, hence the
791 if (reg_maps
->shader_version
.major
>= 3)
793 shader_addline(buffer
, "void order_ps_input(in vec4[%u]);\n", MAX_REG_OUTPUT
);
795 shader_addline(buffer
, "void order_ps_input();\n");
798 IWineD3DPixelShaderImpl
*ps_impl
= (IWineD3DPixelShaderImpl
*) This
;
800 ps_impl
->numbumpenvmatconsts
= 0;
801 for(i
= 0; i
< (sizeof(reg_maps
->bumpmat
) / sizeof(reg_maps
->bumpmat
[0])); i
++) {
802 if(!reg_maps
->bumpmat
[i
]) {
806 ps_impl
->bumpenvmatconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= i
;
807 shader_addline(buffer
, "uniform mat2 bumpenvmat%d;\n", i
);
809 if(reg_maps
->luminanceparams
) {
810 ps_impl
->luminanceconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= i
;
811 shader_addline(buffer
, "uniform float luminancescale%d;\n", i
);
812 shader_addline(buffer
, "uniform float luminanceoffset%d;\n", i
);
813 extra_constants_needed
++;
815 ps_impl
->luminanceconst
[(int) ps_impl
->numbumpenvmatconsts
].texunit
= -1;
818 extra_constants_needed
++;
819 ps_impl
->numbumpenvmatconsts
++;
822 if(ps_args
->srgb_correction
) {
823 shader_addline(buffer
, "const vec4 srgb_mul_low = vec4(%f, %f, %f, %f);\n",
824 srgb_mul_low
, srgb_mul_low
, srgb_mul_low
, srgb_mul_low
);
825 shader_addline(buffer
, "const vec4 srgb_comparison = vec4(%f, %f, %f, %f);\n",
826 srgb_cmp
, srgb_cmp
, srgb_cmp
, srgb_cmp
);
828 if(reg_maps
->vpos
|| reg_maps
->usesdsy
) {
829 if(This
->baseShader
.limits
.constant_float
+ extra_constants_needed
+ 1 < GL_LIMITS(pshader_constantsF
)) {
830 shader_addline(buffer
, "uniform vec4 ycorrection;\n");
831 ((IWineD3DPixelShaderImpl
*) This
)->vpos_uniform
= 1;
832 extra_constants_needed
++;
834 /* This happens because we do not have proper tracking of the constant registers that are
835 * actually used, only the max limit of the shader version
837 FIXME("Cannot find a free uniform for vpos correction params\n");
838 shader_addline(buffer
, "const vec4 ycorrection = vec4(%f, %f, 0.0, 0.0);\n",
839 device
->render_offscreen
? 0.0 : ((IWineD3DSurfaceImpl
*) device
->render_targets
[0])->currentDesc
.Height
,
840 device
->render_offscreen
? 1.0 : -1.0);
842 shader_addline(buffer
, "vec4 vpos;\n");
846 /* Declare texture samplers */
847 for (i
= 0; i
< This
->baseShader
.limits
.sampler
; i
++) {
848 if (reg_maps
->sampler_type
[i
])
850 switch (reg_maps
->sampler_type
[i
])
853 shader_addline(buffer
, "uniform sampler1D %csampler%u;\n", prefix
, i
);
856 if(device
->stateBlock
->textures
[i
] &&
857 IWineD3DBaseTexture_GetTextureDimensions(device
->stateBlock
->textures
[i
]) == GL_TEXTURE_RECTANGLE_ARB
) {
858 shader_addline(buffer
, "uniform sampler2DRect %csampler%u;\n", prefix
, i
);
860 shader_addline(buffer
, "uniform sampler2D %csampler%u;\n", prefix
, i
);
863 if (pshader
&& ps_args
->np2_fixup
& (1 << i
))
865 /* NP2/RECT textures in OpenGL use texcoords in the range [0,width]x[0,height]
866 * while D3D has them in the (normalized) [0,1]x[0,1] range.
867 * samplerNP2Fixup stores texture dimensions and is updated through
868 * shader_glsl_load_np2fixup_constants when the sampler changes. */
869 shader_addline(buffer
, "uniform vec2 %csamplerNP2Fixup%u;\n", prefix
, i
);
872 case WINED3DSTT_CUBE
:
873 shader_addline(buffer
, "uniform samplerCube %csampler%u;\n", prefix
, i
);
875 case WINED3DSTT_VOLUME
:
876 shader_addline(buffer
, "uniform sampler3D %csampler%u;\n", prefix
, i
);
879 shader_addline(buffer
, "uniform unsupported_sampler %csampler%u;\n", prefix
, i
);
880 FIXME("Unrecognized sampler type: %#x\n", reg_maps
->sampler_type
[i
]);
886 /* Declare address variables */
887 for (i
= 0; i
< This
->baseShader
.limits
.address
; i
++) {
888 if (reg_maps
->address
[i
])
889 shader_addline(buffer
, "ivec4 A%d;\n", i
);
892 /* Declare texture coordinate temporaries and initialize them */
893 for (i
= 0; i
< This
->baseShader
.limits
.texcoord
; i
++) {
894 if (reg_maps
->texcoord
[i
])
895 shader_addline(buffer
, "vec4 T%u = gl_TexCoord[%u];\n", i
, i
);
898 /* Declare input register varyings. Only pixel shader, vertex shaders have that declared in the
899 * helper function shader that is linked in at link time
901 if (pshader
&& reg_maps
->shader_version
.major
>= 3)
903 if (use_vs(device
->stateBlock
))
905 shader_addline(buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
907 /* TODO: Write a replacement shader for the fixed function vertex pipeline, so this isn't needed.
908 * For fixed function vertex processing + 3.0 pixel shader we need a separate function in the
909 * pixel shader that reads the fixed function color into the packed input registers.
911 shader_addline(buffer
, "vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
915 /* Declare output register temporaries */
916 if(This
->baseShader
.limits
.packed_output
) {
917 shader_addline(buffer
, "vec4 OUT[%u];\n", This
->baseShader
.limits
.packed_output
);
920 /* Declare temporary variables */
921 for(i
= 0; i
< This
->baseShader
.limits
.temporary
; i
++) {
922 if (reg_maps
->temporary
[i
])
923 shader_addline(buffer
, "vec4 R%u;\n", i
);
926 /* Declare attributes */
927 for (i
= 0; i
< This
->baseShader
.limits
.attributes
; i
++) {
928 if (reg_maps
->attributes
[i
])
929 shader_addline(buffer
, "attribute vec4 attrib%i;\n", i
);
932 /* Declare loop registers aLx */
933 for (i
= 0; i
< reg_maps
->loop_depth
; i
++) {
934 shader_addline(buffer
, "int aL%u;\n", i
);
935 shader_addline(buffer
, "int tmpInt%u;\n", i
);
938 /* Temporary variables for matrix operations */
939 shader_addline(buffer
, "vec4 tmp0;\n");
940 shader_addline(buffer
, "vec4 tmp1;\n");
942 /* Local constants use a different name so they can be loaded once at shader link time
943 * They can't be hardcoded into the shader text via LC = {x, y, z, w}; because the
944 * float -> string conversion can cause precision loss.
946 if(!This
->baseShader
.load_local_constsF
) {
947 LIST_FOR_EACH_ENTRY(lconst
, &This
->baseShader
.constantsF
, local_constant
, entry
) {
948 shader_addline(buffer
, "uniform vec4 %cLC%u;\n", prefix
, lconst
->idx
);
952 /* Start the main program */
953 shader_addline(buffer
, "void main() {\n");
954 if(pshader
&& reg_maps
->vpos
) {
955 /* DirectX apps expect integer values, while OpenGL drivers add approximately 0.5. This causes
956 * off-by-one problems as spotted by the vPos d3d9 visual test. Unfortunately the ATI cards do
957 * not add exactly 0.5, but rather something like 0.49999999 or 0.50000001, which still causes
958 * precision troubles when we just substract 0.5.
960 * To deal with that just floor() the position. This will eliminate the fraction on all cards.
962 * TODO: Test how that behaves with multisampling once we can enable multisampling in winex11.
964 * An advantage of floor is that it works even if the driver doesn't add 1/2. It is somewhat
965 * questionable if 1.5, 2.5, ... are the proper values to return in gl_FragCoord, even though
966 * coordinates specify the pixel centers instead of the pixel corners. This code will behave
967 * correctly on drivers that returns integer values.
969 shader_addline(buffer
, "vpos = floor(vec4(0, ycorrection[0], 0, 0) + gl_FragCoord * vec4(1, ycorrection[1], 1, 1));\n");
973 /*****************************************************************************
974 * Functions to generate GLSL strings from DirectX Shader bytecode begin here.
976 * For more information, see http://wiki.winehq.org/DirectX-Shaders
977 ****************************************************************************/
980 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
981 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
);
983 /** Used for opcode modifiers - They multiply the result by the specified amount */
984 static const char * const shift_glsl_tab
[] = {
986 "2.0 * ", /* 1 (x2) */
987 "4.0 * ", /* 2 (x4) */
988 "8.0 * ", /* 3 (x8) */
989 "16.0 * ", /* 4 (x16) */
990 "32.0 * ", /* 5 (x32) */
997 "0.0625 * ", /* 12 (d16) */
998 "0.125 * ", /* 13 (d8) */
999 "0.25 * ", /* 14 (d4) */
1000 "0.5 * " /* 15 (d2) */
1003 /* Generate a GLSL parameter that does the input modifier computation and return the input register/mask to use */
1004 static void shader_glsl_gen_modifier(DWORD src_modifier
, const char *in_reg
, const char *in_regswizzle
, char *out_str
)
1008 switch (src_modifier
)
1010 case WINED3DSPSM_DZ
: /* Need to handle this in the instructions itself (texld & texcrd). */
1011 case WINED3DSPSM_DW
:
1012 case WINED3DSPSM_NONE
:
1013 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1015 case WINED3DSPSM_NEG
:
1016 sprintf(out_str
, "-%s%s", in_reg
, in_regswizzle
);
1018 case WINED3DSPSM_NOT
:
1019 sprintf(out_str
, "!%s%s", in_reg
, in_regswizzle
);
1021 case WINED3DSPSM_BIAS
:
1022 sprintf(out_str
, "(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1024 case WINED3DSPSM_BIASNEG
:
1025 sprintf(out_str
, "-(%s%s - vec4(0.5)%s)", in_reg
, in_regswizzle
, in_regswizzle
);
1027 case WINED3DSPSM_SIGN
:
1028 sprintf(out_str
, "(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1030 case WINED3DSPSM_SIGNNEG
:
1031 sprintf(out_str
, "-(2.0 * (%s%s - 0.5))", in_reg
, in_regswizzle
);
1033 case WINED3DSPSM_COMP
:
1034 sprintf(out_str
, "(1.0 - %s%s)", in_reg
, in_regswizzle
);
1036 case WINED3DSPSM_X2
:
1037 sprintf(out_str
, "(2.0 * %s%s)", in_reg
, in_regswizzle
);
1039 case WINED3DSPSM_X2NEG
:
1040 sprintf(out_str
, "-(2.0 * %s%s)", in_reg
, in_regswizzle
);
1042 case WINED3DSPSM_ABS
:
1043 sprintf(out_str
, "abs(%s%s)", in_reg
, in_regswizzle
);
1045 case WINED3DSPSM_ABSNEG
:
1046 sprintf(out_str
, "-abs(%s%s)", in_reg
, in_regswizzle
);
1049 FIXME("Unhandled modifier %u\n", src_modifier
);
1050 sprintf(out_str
, "%s%s", in_reg
, in_regswizzle
);
1054 /** Writes the GLSL variable name that corresponds to the register that the
1055 * DX opcode parameter is trying to access */
1056 static void shader_glsl_get_register_name(const struct wined3d_shader_register
*reg
,
1057 char *register_name
, BOOL
*is_color
, const struct wined3d_shader_instruction
*ins
)
1059 /* oPos, oFog and oPts in D3D */
1060 static const char * const hwrastout_reg_names
[] = { "gl_Position", "gl_FogFragCoord", "gl_PointSize" };
1062 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
1063 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
1064 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
1065 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
1071 case WINED3DSPR_TEMP
:
1072 sprintf(register_name
, "R%u", reg
->idx
);
1075 case WINED3DSPR_INPUT
:
1076 /* vertex shaders */
1079 if (((IWineD3DVertexShaderImpl
*)This
)->cur_args
->swizzle_map
& (1 << reg
->idx
)) *is_color
= TRUE
;
1080 sprintf(register_name
, "attrib%u", reg
->idx
);
1084 /* pixel shaders >= 3.0 */
1085 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3)
1087 DWORD idx
= ((IWineD3DPixelShaderImpl
*)This
)->input_reg_map
[reg
->idx
];
1088 DWORD in_count
= GL_LIMITS(glsl_varyings
) / 4;
1092 glsl_src_param_t rel_param
;
1094 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1096 /* Removing a + 0 would be an obvious optimization, but macos doesn't see the NOP
1097 * operation there */
1100 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1102 sprintf(register_name
,
1103 "((%s + %u) > %d ? (%s + %u) > %d ? gl_SecondaryColor : gl_Color : IN[%s + %u])",
1104 rel_param
.param_str
, idx
, in_count
- 1, rel_param
.param_str
, idx
, in_count
,
1105 rel_param
.param_str
, idx
);
1109 sprintf(register_name
, "IN[%s + %u]", rel_param
.param_str
, idx
);
1114 if (((IWineD3DPixelShaderImpl
*)This
)->declared_in_count
> in_count
)
1116 sprintf(register_name
, "((%s) > %d ? (%s) > %d ? gl_SecondaryColor : gl_Color : IN[%s])",
1117 rel_param
.param_str
, in_count
- 1, rel_param
.param_str
, in_count
,
1118 rel_param
.param_str
);
1122 sprintf(register_name
, "IN[%s]", rel_param
.param_str
);
1128 if (idx
== in_count
) sprintf(register_name
, "gl_Color");
1129 else if (idx
== in_count
+ 1) sprintf(register_name
, "gl_SecondaryColor");
1130 else sprintf(register_name
, "IN[%u]", idx
);
1135 if (reg
->idx
== 0) strcpy(register_name
, "gl_Color");
1136 else strcpy(register_name
, "gl_SecondaryColor");
1141 case WINED3DSPR_CONST
:
1143 const char prefix
= pshader
? 'P' : 'V';
1145 /* Relative addressing */
1148 glsl_src_param_t rel_param
;
1149 shader_glsl_add_src_param(ins
, reg
->rel_addr
, WINED3DSP_WRITEMASK_0
, &rel_param
);
1150 if (reg
->idx
) sprintf(register_name
, "%cC[%s + %u]", prefix
, rel_param
.param_str
, reg
->idx
);
1151 else sprintf(register_name
, "%cC[%s]", prefix
, rel_param
.param_str
);
1155 if (shader_constant_is_local(This
, reg
->idx
))
1156 sprintf(register_name
, "%cLC%u", prefix
, reg
->idx
);
1158 sprintf(register_name
, "%cC[%u]", prefix
, reg
->idx
);
1163 case WINED3DSPR_CONSTINT
:
1164 if (pshader
) sprintf(register_name
, "PI[%u]", reg
->idx
);
1165 else sprintf(register_name
, "VI[%u]", reg
->idx
);
1168 case WINED3DSPR_CONSTBOOL
:
1169 if (pshader
) sprintf(register_name
, "PB[%u]", reg
->idx
);
1170 else sprintf(register_name
, "VB[%u]", reg
->idx
);
1173 case WINED3DSPR_TEXTURE
: /* case WINED3DSPR_ADDR: */
1174 if (pshader
) sprintf(register_name
, "T%u", reg
->idx
);
1175 else sprintf(register_name
, "A%u", reg
->idx
);
1178 case WINED3DSPR_LOOP
:
1179 sprintf(register_name
, "aL%u", This
->baseShader
.cur_loop_regno
- 1);
1182 case WINED3DSPR_SAMPLER
:
1183 if (pshader
) sprintf(register_name
, "Psampler%u", reg
->idx
);
1184 else sprintf(register_name
, "Vsampler%u", reg
->idx
);
1187 case WINED3DSPR_COLOROUT
:
1188 if (reg
->idx
>= GL_LIMITS(buffers
))
1189 WARN("Write to render target %u, only %d supported\n", reg
->idx
, GL_LIMITS(buffers
));
1191 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) sprintf(register_name
, "gl_FragData[%u]", reg
->idx
);
1192 /* On older cards with GLSL support like the GeforceFX there's only one buffer. */
1193 else sprintf(register_name
, "gl_FragColor");
1196 case WINED3DSPR_RASTOUT
:
1197 sprintf(register_name
, "%s", hwrastout_reg_names
[reg
->idx
]);
1200 case WINED3DSPR_DEPTHOUT
:
1201 sprintf(register_name
, "gl_FragDepth");
1204 case WINED3DSPR_ATTROUT
:
1205 if (reg
->idx
== 0) sprintf(register_name
, "gl_FrontColor");
1206 else sprintf(register_name
, "gl_FrontSecondaryColor");
1209 case WINED3DSPR_TEXCRDOUT
:
1210 /* Vertex shaders >= 3.0: WINED3DSPR_OUTPUT */
1211 if (This
->baseShader
.reg_maps
.shader_version
.major
>= 3) sprintf(register_name
, "OUT[%u]", reg
->idx
);
1212 else sprintf(register_name
, "gl_TexCoord[%u]", reg
->idx
);
1215 case WINED3DSPR_MISCTYPE
:
1219 sprintf(register_name
, "vpos");
1221 else if (reg
->idx
== 1)
1223 /* Note that gl_FrontFacing is a bool, while vFace is
1224 * a float for which the sign determines front/back */
1225 sprintf(register_name
, "(gl_FrontFacing ? 1.0 : -1.0)");
1229 FIXME("Unhandled misctype register %d\n", reg
->idx
);
1230 sprintf(register_name
, "unrecognized_register");
1234 case WINED3DSPR_IMMCONST
:
1235 switch (reg
->immconst_type
)
1237 case WINED3D_IMMCONST_FLOAT
:
1238 sprintf(register_name
, "%.8e", *(float *)reg
->immconst_data
);
1241 case WINED3D_IMMCONST_FLOAT4
:
1242 sprintf(register_name
, "vec4(%.8e, %.8e, %.8e, %.8e)",
1243 *(float *)®
->immconst_data
[0], *(float *)®
->immconst_data
[1],
1244 *(float *)®
->immconst_data
[2], *(float *)®
->immconst_data
[3]);
1248 FIXME("Unhandled immconst type %#x\n", reg
->immconst_type
);
1249 sprintf(register_name
, "<unhandled_immconst_type %#x>", reg
->immconst_type
);
1254 FIXME("Unhandled register name Type(%d)\n", reg
->type
);
1255 sprintf(register_name
, "unrecognized_register");
1260 static void shader_glsl_write_mask_to_str(DWORD write_mask
, char *str
)
1263 if (write_mask
& WINED3DSP_WRITEMASK_0
) *str
++ = 'x';
1264 if (write_mask
& WINED3DSP_WRITEMASK_1
) *str
++ = 'y';
1265 if (write_mask
& WINED3DSP_WRITEMASK_2
) *str
++ = 'z';
1266 if (write_mask
& WINED3DSP_WRITEMASK_3
) *str
++ = 'w';
1270 /* Get the GLSL write mask for the destination register */
1271 static DWORD
shader_glsl_get_write_mask(const struct wined3d_shader_dst_param
*param
, char *write_mask
)
1273 DWORD mask
= param
->write_mask
;
1275 if (shader_is_scalar(¶m
->reg
))
1277 mask
= WINED3DSP_WRITEMASK_0
;
1282 shader_glsl_write_mask_to_str(mask
, write_mask
);
1288 static unsigned int shader_glsl_get_write_mask_size(DWORD write_mask
) {
1289 unsigned int size
= 0;
1291 if (write_mask
& WINED3DSP_WRITEMASK_0
) ++size
;
1292 if (write_mask
& WINED3DSP_WRITEMASK_1
) ++size
;
1293 if (write_mask
& WINED3DSP_WRITEMASK_2
) ++size
;
1294 if (write_mask
& WINED3DSP_WRITEMASK_3
) ++size
;
1299 static void shader_glsl_swizzle_to_str(const DWORD swizzle
, BOOL fixup
, DWORD mask
, char *str
)
1301 /* For registers of type WINED3DDECLTYPE_D3DCOLOR, data is stored as "bgra",
1302 * but addressed as "rgba". To fix this we need to swap the register's x
1303 * and z components. */
1304 const char *swizzle_chars
= fixup
? "zyxw" : "xyzw";
1307 /* swizzle bits fields: wwzzyyxx */
1308 if (mask
& WINED3DSP_WRITEMASK_0
) *str
++ = swizzle_chars
[swizzle
& 0x03];
1309 if (mask
& WINED3DSP_WRITEMASK_1
) *str
++ = swizzle_chars
[(swizzle
>> 2) & 0x03];
1310 if (mask
& WINED3DSP_WRITEMASK_2
) *str
++ = swizzle_chars
[(swizzle
>> 4) & 0x03];
1311 if (mask
& WINED3DSP_WRITEMASK_3
) *str
++ = swizzle_chars
[(swizzle
>> 6) & 0x03];
1315 static void shader_glsl_get_swizzle(const struct wined3d_shader_src_param
*param
,
1316 BOOL fixup
, DWORD mask
, char *swizzle_str
)
1318 if (shader_is_scalar(¶m
->reg
))
1319 *swizzle_str
= '\0';
1321 shader_glsl_swizzle_to_str(param
->swizzle
, fixup
, mask
, swizzle_str
);
1324 /* From a given parameter token, generate the corresponding GLSL string.
1325 * Also, return the actual register name and swizzle in case the
1326 * caller needs this information as well. */
1327 static void shader_glsl_add_src_param(const struct wined3d_shader_instruction
*ins
,
1328 const struct wined3d_shader_src_param
*wined3d_src
, DWORD mask
, glsl_src_param_t
*glsl_src
)
1330 BOOL is_color
= FALSE
;
1331 char swizzle_str
[6];
1333 glsl_src
->reg_name
[0] = '\0';
1334 glsl_src
->param_str
[0] = '\0';
1335 swizzle_str
[0] = '\0';
1337 shader_glsl_get_register_name(&wined3d_src
->reg
, glsl_src
->reg_name
, &is_color
, ins
);
1338 shader_glsl_get_swizzle(wined3d_src
, is_color
, mask
, swizzle_str
);
1339 shader_glsl_gen_modifier(wined3d_src
->modifiers
, glsl_src
->reg_name
, swizzle_str
, glsl_src
->param_str
);
1342 /* From a given parameter token, generate the corresponding GLSL string.
1343 * Also, return the actual register name and swizzle in case the
1344 * caller needs this information as well. */
1345 static DWORD
shader_glsl_add_dst_param(const struct wined3d_shader_instruction
*ins
,
1346 const struct wined3d_shader_dst_param
*wined3d_dst
, glsl_dst_param_t
*glsl_dst
)
1348 BOOL is_color
= FALSE
;
1350 glsl_dst
->mask_str
[0] = '\0';
1351 glsl_dst
->reg_name
[0] = '\0';
1353 shader_glsl_get_register_name(&wined3d_dst
->reg
, glsl_dst
->reg_name
, &is_color
, ins
);
1354 return shader_glsl_get_write_mask(wined3d_dst
, glsl_dst
->mask_str
);
1357 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1358 static DWORD
shader_glsl_append_dst_ext(SHADER_BUFFER
*buffer
,
1359 const struct wined3d_shader_instruction
*ins
, const struct wined3d_shader_dst_param
*dst
)
1361 glsl_dst_param_t glsl_dst
;
1364 mask
= shader_glsl_add_dst_param(ins
, dst
, &glsl_dst
);
1365 if (mask
) shader_addline(buffer
, "%s%s = %s(", glsl_dst
.reg_name
, glsl_dst
.mask_str
, shift_glsl_tab
[dst
->shift
]);
1370 /* Append the destination part of the instruction to the buffer, return the effective write mask */
1371 static DWORD
shader_glsl_append_dst(SHADER_BUFFER
*buffer
, const struct wined3d_shader_instruction
*ins
)
1373 return shader_glsl_append_dst_ext(buffer
, ins
, &ins
->dst
[0]);
1376 /** Process GLSL instruction modifiers */
1377 static void shader_glsl_add_instruction_modifiers(const struct wined3d_shader_instruction
*ins
)
1379 glsl_dst_param_t dst_param
;
1382 if (!ins
->dst_count
) return;
1384 modifiers
= ins
->dst
[0].modifiers
;
1385 if (!modifiers
) return;
1387 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
1389 if (modifiers
& WINED3DSPDM_SATURATE
)
1391 /* _SAT means to clamp the value of the register to between 0 and 1 */
1392 shader_addline(ins
->ctx
->buffer
, "%s%s = clamp(%s%s, 0.0, 1.0);\n", dst_param
.reg_name
,
1393 dst_param
.mask_str
, dst_param
.reg_name
, dst_param
.mask_str
);
1396 if (modifiers
& WINED3DSPDM_MSAMPCENTROID
)
1398 FIXME("_centroid modifier not handled\n");
1401 if (modifiers
& WINED3DSPDM_PARTIALPRECISION
)
1403 /* MSDN says this modifier can be safely ignored, so that's what we'll do. */
1407 static inline const char *shader_get_comp_op(DWORD op
)
1410 case COMPARISON_GT
: return ">";
1411 case COMPARISON_EQ
: return "==";
1412 case COMPARISON_GE
: return ">=";
1413 case COMPARISON_LT
: return "<";
1414 case COMPARISON_NE
: return "!=";
1415 case COMPARISON_LE
: return "<=";
1417 FIXME("Unrecognized comparison value: %u\n", op
);
1422 static void shader_glsl_get_sample_function(DWORD sampler_type
, DWORD flags
, glsl_sample_function_t
*sample_function
)
1424 BOOL projected
= flags
& WINED3D_GLSL_SAMPLE_PROJECTED
;
1425 BOOL texrect
= flags
& WINED3D_GLSL_SAMPLE_RECT
;
1426 BOOL lod
= flags
& WINED3D_GLSL_SAMPLE_LOD
;
1427 BOOL grad
= flags
& WINED3D_GLSL_SAMPLE_GRAD
;
1429 /* Note that there's no such thing as a projected cube texture. */
1430 switch(sampler_type
) {
1433 sample_function
->name
= projected
? "texture1DProjLod" : "texture1DLod";
1435 sample_function
->name
= projected
? "texture1DProjGradARB" : "texture1DGradARB";
1437 sample_function
->name
= projected
? "texture1DProj" : "texture1D";
1439 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
;
1444 sample_function
->name
= projected
? "texture2DRectProjLod" : "texture2DRectLod";
1446 /* What good are texrect grad functions? I don't know, but GL_EXT_gpu_shader4 defines them.
1447 * There is no GL_ARB_shader_texture_lod spec yet, so I don't know if they're defined there
1449 sample_function
->name
= projected
? "shadow2DRectProjGradARB" : "shadow2DRectGradARB";
1451 sample_function
->name
= projected
? "texture2DRectProj" : "texture2DRect";
1455 sample_function
->name
= projected
? "texture2DProjLod" : "texture2DLod";
1457 sample_function
->name
= projected
? "texture2DProjGradARB" : "texture2DGradARB";
1459 sample_function
->name
= projected
? "texture2DProj" : "texture2D";
1462 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
;
1464 case WINED3DSTT_CUBE
:
1466 sample_function
->name
= "textureCubeLod";
1468 sample_function
->name
= "textureCubeGradARB";
1470 sample_function
->name
= "textureCube";
1472 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1474 case WINED3DSTT_VOLUME
:
1476 sample_function
->name
= projected
? "texture3DProjLod" : "texture3DLod";
1478 sample_function
->name
= projected
? "texture3DProjGradARB" : "texture3DGradARB";
1480 sample_function
->name
= projected
? "texture3DProj" : "texture3D";
1482 sample_function
->coord_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1485 sample_function
->name
= "";
1486 sample_function
->coord_mask
= 0;
1487 FIXME("Unrecognized sampler type: %#x;\n", sampler_type
);
1492 static void shader_glsl_append_fixup_arg(char *arguments
, const char *reg_name
,
1493 BOOL sign_fixup
, enum fixup_channel_source channel_source
)
1495 switch(channel_source
)
1497 case CHANNEL_SOURCE_ZERO
:
1498 strcat(arguments
, "0.0");
1501 case CHANNEL_SOURCE_ONE
:
1502 strcat(arguments
, "1.0");
1505 case CHANNEL_SOURCE_X
:
1506 strcat(arguments
, reg_name
);
1507 strcat(arguments
, ".x");
1510 case CHANNEL_SOURCE_Y
:
1511 strcat(arguments
, reg_name
);
1512 strcat(arguments
, ".y");
1515 case CHANNEL_SOURCE_Z
:
1516 strcat(arguments
, reg_name
);
1517 strcat(arguments
, ".z");
1520 case CHANNEL_SOURCE_W
:
1521 strcat(arguments
, reg_name
);
1522 strcat(arguments
, ".w");
1526 FIXME("Unhandled channel source %#x\n", channel_source
);
1527 strcat(arguments
, "undefined");
1531 if (sign_fixup
) strcat(arguments
, " * 2.0 - 1.0");
1534 static void shader_glsl_color_correction(const struct wined3d_shader_instruction
*ins
, struct color_fixup_desc fixup
)
1536 struct wined3d_shader_dst_param dst
;
1537 unsigned int mask_size
, remaining
;
1538 glsl_dst_param_t dst_param
;
1539 char arguments
[256];
1543 if (fixup
.x_sign_fixup
|| fixup
.x_source
!= CHANNEL_SOURCE_X
) mask
|= WINED3DSP_WRITEMASK_0
;
1544 if (fixup
.y_sign_fixup
|| fixup
.y_source
!= CHANNEL_SOURCE_Y
) mask
|= WINED3DSP_WRITEMASK_1
;
1545 if (fixup
.z_sign_fixup
|| fixup
.z_source
!= CHANNEL_SOURCE_Z
) mask
|= WINED3DSP_WRITEMASK_2
;
1546 if (fixup
.w_sign_fixup
|| fixup
.w_source
!= CHANNEL_SOURCE_W
) mask
|= WINED3DSP_WRITEMASK_3
;
1547 mask
&= ins
->dst
[0].write_mask
;
1549 if (!mask
) return; /* Nothing to do */
1551 if (is_yuv_fixup(fixup
))
1553 enum yuv_fixup yuv_fixup
= get_yuv_fixup(fixup
);
1554 FIXME("YUV fixup (%#x) not supported\n", yuv_fixup
);
1558 mask_size
= shader_glsl_get_write_mask_size(mask
);
1561 dst
.write_mask
= mask
;
1562 shader_glsl_add_dst_param(ins
, &dst
, &dst_param
);
1564 arguments
[0] = '\0';
1565 remaining
= mask_size
;
1566 if (mask
& WINED3DSP_WRITEMASK_0
)
1568 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.x_sign_fixup
, fixup
.x_source
);
1569 if (--remaining
) strcat(arguments
, ", ");
1571 if (mask
& WINED3DSP_WRITEMASK_1
)
1573 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.y_sign_fixup
, fixup
.y_source
);
1574 if (--remaining
) strcat(arguments
, ", ");
1576 if (mask
& WINED3DSP_WRITEMASK_2
)
1578 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.z_sign_fixup
, fixup
.z_source
);
1579 if (--remaining
) strcat(arguments
, ", ");
1581 if (mask
& WINED3DSP_WRITEMASK_3
)
1583 shader_glsl_append_fixup_arg(arguments
, dst_param
.reg_name
, fixup
.w_sign_fixup
, fixup
.w_source
);
1584 if (--remaining
) strcat(arguments
, ", ");
1589 shader_addline(ins
->ctx
->buffer
, "%s%s = vec%u(%s);\n",
1590 dst_param
.reg_name
, dst_param
.mask_str
, mask_size
, arguments
);
1594 shader_addline(ins
->ctx
->buffer
, "%s%s = %s;\n", dst_param
.reg_name
, dst_param
.mask_str
, arguments
);
1598 static void PRINTF_ATTR(8, 9) shader_glsl_gen_sample_code(const struct wined3d_shader_instruction
*ins
,
1599 DWORD sampler
, const glsl_sample_function_t
*sample_function
, DWORD swizzle
,
1600 const char *dx
, const char *dy
,
1601 const char *bias
, const char *coord_reg_fmt
, ...)
1603 const char *sampler_base
;
1604 char dst_swizzle
[6];
1605 struct color_fixup_desc fixup
;
1606 BOOL np2_fixup
= FALSE
;
1609 shader_glsl_swizzle_to_str(swizzle
, FALSE
, ins
->dst
[0].write_mask
, dst_swizzle
);
1611 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
1613 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
1614 fixup
= This
->cur_args
->color_fixup
[sampler
];
1615 sampler_base
= "Psampler";
1617 if(This
->cur_args
->np2_fixup
& (1 << sampler
)) {
1619 FIXME("Biased sampling from NP2 textures is unsupported\n");
1625 sampler_base
= "Vsampler";
1626 fixup
= COLOR_FIXUP_IDENTITY
; /* FIXME: Vshader color fixup */
1629 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1631 shader_addline(ins
->ctx
->buffer
, "%s(%s%u, ", sample_function
->name
, sampler_base
, sampler
);
1633 va_start(args
, coord_reg_fmt
);
1634 shader_vaddline(ins
->ctx
->buffer
, coord_reg_fmt
, args
);
1638 shader_addline(ins
->ctx
->buffer
, ", %s)%s);\n", bias
, dst_swizzle
);
1641 shader_addline(ins
->ctx
->buffer
, " * PsamplerNP2Fixup%u)%s);\n", sampler
, dst_swizzle
);
1642 } else if(dx
&& dy
) {
1643 shader_addline(ins
->ctx
->buffer
, ", %s, %s)%s);\n", dx
, dy
, dst_swizzle
);
1645 shader_addline(ins
->ctx
->buffer
, ")%s);\n", dst_swizzle
);
1649 if(!is_identity_fixup(fixup
)) {
1650 shader_glsl_color_correction(ins
, fixup
);
1654 /*****************************************************************************
1656 * Begin processing individual instruction opcodes
1658 ****************************************************************************/
1660 /* Generate GLSL arithmetic functions (dst = src1 + src2) */
1661 static void shader_glsl_arith(const struct wined3d_shader_instruction
*ins
)
1663 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1664 glsl_src_param_t src0_param
;
1665 glsl_src_param_t src1_param
;
1669 /* Determine the GLSL operator to use based on the opcode */
1670 switch (ins
->handler_idx
)
1672 case WINED3DSIH_MUL
: op
= '*'; break;
1673 case WINED3DSIH_ADD
: op
= '+'; break;
1674 case WINED3DSIH_SUB
: op
= '-'; break;
1677 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
1681 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1682 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
1683 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
1684 shader_addline(buffer
, "%s %c %s);\n", src0_param
.param_str
, op
, src1_param
.param_str
);
1687 /* Process the WINED3DSIO_MOV opcode using GLSL (dst = src) */
1688 static void shader_glsl_mov(const struct wined3d_shader_instruction
*ins
)
1690 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1691 glsl_src_param_t src0_param
;
1694 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1695 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
1697 /* In vs_1_1 WINED3DSIO_MOV can write to the address register. In later
1698 * shader versions WINED3DSIO_MOVA is used for this. */
1699 if (ins
->ctx
->reg_maps
->shader_version
.major
== 1
1700 && !shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
)
1701 && ins
->dst
[0].reg
.type
== WINED3DSPR_ADDR
)
1703 /* This is a simple floor() */
1704 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1705 if (mask_size
> 1) {
1706 shader_addline(buffer
, "ivec%d(floor(%s)));\n", mask_size
, src0_param
.param_str
);
1708 shader_addline(buffer
, "int(floor(%s)));\n", src0_param
.param_str
);
1711 else if(ins
->handler_idx
== WINED3DSIH_MOVA
)
1713 /* We need to *round* to the nearest int here. */
1714 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1715 if (mask_size
> 1) {
1716 shader_addline(buffer
, "ivec%d(floor(abs(%s) + vec%d(0.5)) * sign(%s)));\n", mask_size
, src0_param
.param_str
, mask_size
, src0_param
.param_str
);
1718 shader_addline(buffer
, "int(floor(abs(%s) + 0.5) * sign(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
1721 shader_addline(buffer
, "%s);\n", src0_param
.param_str
);
1725 /* Process the dot product operators DP3 and DP4 in GLSL (dst = dot(src0, src1)) */
1726 static void shader_glsl_dot(const struct wined3d_shader_instruction
*ins
)
1728 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1729 glsl_src_param_t src0_param
;
1730 glsl_src_param_t src1_param
;
1731 DWORD dst_write_mask
, src_write_mask
;
1732 unsigned int dst_size
= 0;
1734 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1735 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1737 /* dp3 works on vec3, dp4 on vec4 */
1738 if (ins
->handler_idx
== WINED3DSIH_DP4
)
1740 src_write_mask
= WINED3DSP_WRITEMASK_ALL
;
1742 src_write_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1745 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_write_mask
, &src0_param
);
1746 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_write_mask
, &src1_param
);
1749 shader_addline(buffer
, "vec%d(dot(%s, %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1751 shader_addline(buffer
, "dot(%s, %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1755 /* Note that this instruction has some restrictions. The destination write mask
1756 * can't contain the w component, and the source swizzles have to be .xyzw */
1757 static void shader_glsl_cross(const struct wined3d_shader_instruction
*ins
)
1759 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
1760 glsl_src_param_t src0_param
;
1761 glsl_src_param_t src1_param
;
1764 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
1765 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1766 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
1767 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
1768 shader_addline(ins
->ctx
->buffer
, "cross(%s, %s)%s);\n", src0_param
.param_str
, src1_param
.param_str
, dst_mask
);
1771 /* Process the WINED3DSIO_POW instruction in GLSL (dst = |src0|^src1)
1772 * Src0 and src1 are scalars. Note that D3D uses the absolute of src0, while
1773 * GLSL uses the value as-is. */
1774 static void shader_glsl_pow(const struct wined3d_shader_instruction
*ins
)
1776 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1777 glsl_src_param_t src0_param
;
1778 glsl_src_param_t src1_param
;
1779 DWORD dst_write_mask
;
1780 unsigned int dst_size
;
1782 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1783 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1785 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1786 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
1789 shader_addline(buffer
, "vec%d(pow(abs(%s), %s)));\n", dst_size
, src0_param
.param_str
, src1_param
.param_str
);
1791 shader_addline(buffer
, "pow(abs(%s), %s));\n", src0_param
.param_str
, src1_param
.param_str
);
1795 /* Process the WINED3DSIO_LOG instruction in GLSL (dst = log2(|src0|))
1796 * Src0 is a scalar. Note that D3D uses the absolute of src0, while
1797 * GLSL uses the value as-is. */
1798 static void shader_glsl_log(const struct wined3d_shader_instruction
*ins
)
1800 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1801 glsl_src_param_t src0_param
;
1802 DWORD dst_write_mask
;
1803 unsigned int dst_size
;
1805 dst_write_mask
= shader_glsl_append_dst(buffer
, ins
);
1806 dst_size
= shader_glsl_get_write_mask_size(dst_write_mask
);
1808 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
1811 shader_addline(buffer
, "vec%d(log2(abs(%s))));\n", dst_size
, src0_param
.param_str
);
1813 shader_addline(buffer
, "log2(abs(%s)));\n", src0_param
.param_str
);
1817 /* Map the opcode 1-to-1 to the GL code (arg->dst = instruction(src0, src1, ...) */
1818 static void shader_glsl_map2gl(const struct wined3d_shader_instruction
*ins
)
1820 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1821 glsl_src_param_t src_param
;
1822 const char *instruction
;
1826 /* Determine the GLSL function to use based on the opcode */
1827 /* TODO: Possibly make this a table for faster lookups */
1828 switch (ins
->handler_idx
)
1830 case WINED3DSIH_MIN
: instruction
= "min"; break;
1831 case WINED3DSIH_MAX
: instruction
= "max"; break;
1832 case WINED3DSIH_ABS
: instruction
= "abs"; break;
1833 case WINED3DSIH_FRC
: instruction
= "fract"; break;
1834 case WINED3DSIH_NRM
: instruction
= "normalize"; break;
1835 case WINED3DSIH_EXP
: instruction
= "exp2"; break;
1836 case WINED3DSIH_SGN
: instruction
= "sign"; break;
1837 case WINED3DSIH_DSX
: instruction
= "dFdx"; break;
1838 case WINED3DSIH_DSY
: instruction
= "ycorrection.y * dFdy"; break;
1839 default: instruction
= "";
1840 FIXME("Opcode %#x not yet handled in GLSL\n", ins
->handler_idx
);
1844 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1846 shader_addline(buffer
, "%s(", instruction
);
1850 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src_param
);
1851 shader_addline(buffer
, "%s", src_param
.param_str
);
1852 for (i
= 1; i
< ins
->src_count
; ++i
)
1854 shader_glsl_add_src_param(ins
, &ins
->src
[i
], write_mask
, &src_param
);
1855 shader_addline(buffer
, ", %s", src_param
.param_str
);
1859 shader_addline(buffer
, "));\n");
1862 /** Process the WINED3DSIO_EXPP instruction in GLSL:
1863 * For shader model 1.x, do the following (and honor the writemask, so use a temporary variable):
1864 * dst.x = 2^(floor(src))
1865 * dst.y = src - floor(src)
1866 * dst.z = 2^src (partial precision is allowed, but optional)
1868 * For 2.0 shaders, just do this (honoring writemask and swizzle):
1869 * dst = 2^src; (partial precision is allowed, but optional)
1871 static void shader_glsl_expp(const struct wined3d_shader_instruction
*ins
)
1873 glsl_src_param_t src_param
;
1875 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src_param
);
1877 if (ins
->ctx
->reg_maps
->shader_version
.major
< 2)
1881 shader_addline(ins
->ctx
->buffer
, "tmp0.x = exp2(floor(%s));\n", src_param
.param_str
);
1882 shader_addline(ins
->ctx
->buffer
, "tmp0.y = %s - floor(%s);\n", src_param
.param_str
, src_param
.param_str
);
1883 shader_addline(ins
->ctx
->buffer
, "tmp0.z = exp2(%s);\n", src_param
.param_str
);
1884 shader_addline(ins
->ctx
->buffer
, "tmp0.w = 1.0;\n");
1886 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1887 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
1888 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", dst_mask
);
1891 unsigned int mask_size
;
1893 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1894 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1896 if (mask_size
> 1) {
1897 shader_addline(ins
->ctx
->buffer
, "vec%d(exp2(%s)));\n", mask_size
, src_param
.param_str
);
1899 shader_addline(ins
->ctx
->buffer
, "exp2(%s));\n", src_param
.param_str
);
1904 /** Process the RCP (reciprocal or inverse) opcode in GLSL (dst = 1 / src) */
1905 static void shader_glsl_rcp(const struct wined3d_shader_instruction
*ins
)
1907 glsl_src_param_t src_param
;
1909 unsigned int mask_size
;
1911 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1912 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1913 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1915 if (mask_size
> 1) {
1916 shader_addline(ins
->ctx
->buffer
, "vec%d(1.0 / %s));\n", mask_size
, src_param
.param_str
);
1918 shader_addline(ins
->ctx
->buffer
, "1.0 / %s);\n", src_param
.param_str
);
1922 static void shader_glsl_rsq(const struct wined3d_shader_instruction
*ins
)
1924 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
1925 glsl_src_param_t src_param
;
1927 unsigned int mask_size
;
1929 write_mask
= shader_glsl_append_dst(buffer
, ins
);
1930 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1932 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src_param
);
1934 if (mask_size
> 1) {
1935 shader_addline(buffer
, "vec%d(inversesqrt(%s)));\n", mask_size
, src_param
.param_str
);
1937 shader_addline(buffer
, "inversesqrt(%s));\n", src_param
.param_str
);
1941 /** Process signed comparison opcodes in GLSL. */
1942 static void shader_glsl_compare(const struct wined3d_shader_instruction
*ins
)
1944 glsl_src_param_t src0_param
;
1945 glsl_src_param_t src1_param
;
1947 unsigned int mask_size
;
1949 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
1950 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
1951 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
1952 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
1954 if (mask_size
> 1) {
1955 const char *compare
;
1957 switch(ins
->handler_idx
)
1959 case WINED3DSIH_SLT
: compare
= "lessThan"; break;
1960 case WINED3DSIH_SGE
: compare
= "greaterThanEqual"; break;
1961 default: compare
= "";
1962 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
1965 shader_addline(ins
->ctx
->buffer
, "vec%d(%s(%s, %s)));\n", mask_size
, compare
,
1966 src0_param
.param_str
, src1_param
.param_str
);
1968 switch(ins
->handler_idx
)
1970 case WINED3DSIH_SLT
:
1971 /* Step(src0, src1) is not suitable here because if src0 == src1 SLT is supposed,
1972 * to return 0.0 but step returns 1.0 because step is not < x
1973 * An alternative is a bvec compare padded with an unused second component.
1974 * step(src1 * -1.0, src0 * -1.0) is not an option because it suffers from the same
1975 * issue. Playing with not() is not possible either because not() does not accept
1978 shader_addline(ins
->ctx
->buffer
, "(%s < %s) ? 1.0 : 0.0);\n",
1979 src0_param
.param_str
, src1_param
.param_str
);
1981 case WINED3DSIH_SGE
:
1982 /* Here we can use the step() function and safe a conditional */
1983 shader_addline(ins
->ctx
->buffer
, "step(%s, %s));\n", src1_param
.param_str
, src0_param
.param_str
);
1986 FIXME("Can't handle opcode %#x\n", ins
->handler_idx
);
1992 /** Process CMP instruction in GLSL (dst = src0 >= 0.0 ? src1 : src2), per channel */
1993 static void shader_glsl_cmp(const struct wined3d_shader_instruction
*ins
)
1995 glsl_src_param_t src0_param
;
1996 glsl_src_param_t src1_param
;
1997 glsl_src_param_t src2_param
;
1998 DWORD write_mask
, cmp_channel
= 0;
2001 BOOL temp_destination
= FALSE
;
2003 if (shader_is_scalar(&ins
->src
[0].reg
))
2005 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2007 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
2008 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2009 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2011 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2012 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2014 DWORD dst_mask
= ins
->dst
[0].write_mask
;
2015 struct wined3d_shader_dst_param dst
= ins
->dst
[0];
2017 /* Cycle through all source0 channels */
2018 for (i
=0; i
<4; i
++) {
2020 /* Find the destination channels which use the current source0 channel */
2021 for (j
=0; j
<4; j
++) {
2022 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2024 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2025 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2028 dst
.write_mask
= dst_mask
& write_mask
;
2030 /* Splitting the cmp instruction up in multiple lines imposes a problem:
2031 * The first lines may overwrite source parameters of the following lines.
2032 * Deal with that by using a temporary destination register if needed
2034 if ((ins
->src
[0].reg
.idx
== ins
->dst
[0].reg
.idx
2035 && ins
->src
[0].reg
.type
== ins
->dst
[0].reg
.type
)
2036 || (ins
->src
[1].reg
.idx
== ins
->dst
[0].reg
.idx
2037 && ins
->src
[1].reg
.type
== ins
->dst
[0].reg
.type
)
2038 || (ins
->src
[2].reg
.idx
== ins
->dst
[0].reg
.idx
2039 && ins
->src
[2].reg
.type
== ins
->dst
[0].reg
.type
))
2041 write_mask
= shader_glsl_get_write_mask(&dst
, mask_char
);
2042 if (!write_mask
) continue;
2043 shader_addline(ins
->ctx
->buffer
, "tmp0%s = (", mask_char
);
2044 temp_destination
= TRUE
;
2046 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2047 if (!write_mask
) continue;
2050 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2051 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2052 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2054 shader_addline(ins
->ctx
->buffer
, "%s >= 0.0 ? %s : %s);\n",
2055 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2058 if(temp_destination
) {
2059 shader_glsl_get_write_mask(&ins
->dst
[0], mask_char
);
2060 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2061 shader_addline(ins
->ctx
->buffer
, "tmp0%s);\n", mask_char
);
2067 /** Process the CND opcode in GLSL (dst = (src0 > 0.5) ? src1 : src2) */
2068 /* For ps 1.1-1.3, only a single component of src0 is used. For ps 1.4
2069 * the compare is done per component of src0. */
2070 static void shader_glsl_cnd(const struct wined3d_shader_instruction
*ins
)
2072 struct wined3d_shader_dst_param dst
;
2073 glsl_src_param_t src0_param
;
2074 glsl_src_param_t src1_param
;
2075 glsl_src_param_t src2_param
;
2076 DWORD write_mask
, cmp_channel
= 0;
2079 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2080 ins
->ctx
->reg_maps
->shader_version
.minor
);
2082 if (shader_version
< WINED3D_SHADER_VERSION(1, 4))
2084 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2085 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2086 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2087 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2089 /* Fun: The D3DSI_COISSUE flag changes the semantic of the cnd instruction for < 1.4 shaders */
2092 shader_addline(ins
->ctx
->buffer
, "%s /* COISSUE! */);\n", src1_param
.param_str
);
2094 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2095 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2099 /* Cycle through all source0 channels */
2100 dst_mask
= ins
->dst
[0].write_mask
;
2102 for (i
=0; i
<4; i
++) {
2104 /* Find the destination channels which use the current source0 channel */
2105 for (j
=0; j
<4; j
++) {
2106 if (((ins
->src
[0].swizzle
>> (2 * j
)) & 0x3) == i
)
2108 write_mask
|= WINED3DSP_WRITEMASK_0
<< j
;
2109 cmp_channel
= WINED3DSP_WRITEMASK_0
<< j
;
2113 dst
.write_mask
= dst_mask
& write_mask
;
2114 write_mask
= shader_glsl_append_dst_ext(ins
->ctx
->buffer
, ins
, &dst
);
2115 if (!write_mask
) continue;
2117 shader_glsl_add_src_param(ins
, &ins
->src
[0], cmp_channel
, &src0_param
);
2118 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2119 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2121 shader_addline(ins
->ctx
->buffer
, "%s > 0.5 ? %s : %s);\n",
2122 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2126 /** GLSL code generation for WINED3DSIO_MAD: Multiply the first 2 opcodes, then add the last */
2127 static void shader_glsl_mad(const struct wined3d_shader_instruction
*ins
)
2129 glsl_src_param_t src0_param
;
2130 glsl_src_param_t src1_param
;
2131 glsl_src_param_t src2_param
;
2134 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2135 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2136 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2137 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2138 shader_addline(ins
->ctx
->buffer
, "(%s * %s) + %s);\n",
2139 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
2142 /** Handles transforming all WINED3DSIO_M?x? opcodes for
2143 Vertex shaders to GLSL codes */
2144 static void shader_glsl_mnxn(const struct wined3d_shader_instruction
*ins
)
2147 int nComponents
= 0;
2148 struct wined3d_shader_dst_param tmp_dst
= {{0}};
2149 struct wined3d_shader_src_param tmp_src
[2] = {{{0}}};
2150 struct wined3d_shader_instruction tmp_ins
;
2152 memset(&tmp_ins
, 0, sizeof(tmp_ins
));
2154 /* Set constants for the temporary argument */
2155 tmp_ins
.ctx
= ins
->ctx
;
2156 tmp_ins
.dst_count
= 1;
2157 tmp_ins
.dst
= &tmp_dst
;
2158 tmp_ins
.src_count
= 2;
2159 tmp_ins
.src
= tmp_src
;
2161 switch(ins
->handler_idx
)
2163 case WINED3DSIH_M4x4
:
2165 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2167 case WINED3DSIH_M4x3
:
2169 tmp_ins
.handler_idx
= WINED3DSIH_DP4
;
2171 case WINED3DSIH_M3x4
:
2173 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2175 case WINED3DSIH_M3x3
:
2177 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2179 case WINED3DSIH_M3x2
:
2181 tmp_ins
.handler_idx
= WINED3DSIH_DP3
;
2187 tmp_dst
= ins
->dst
[0];
2188 tmp_src
[0] = ins
->src
[0];
2189 tmp_src
[1] = ins
->src
[1];
2190 for (i
= 0; i
< nComponents
; ++i
)
2192 tmp_dst
.write_mask
= WINED3DSP_WRITEMASK_0
<< i
;
2193 shader_glsl_dot(&tmp_ins
);
2194 ++tmp_src
[1].reg
.idx
;
2199 The LRP instruction performs a component-wise linear interpolation
2200 between the second and third operands using the first operand as the
2201 blend factor. Equation: (dst = src2 + src0 * (src1 - src2))
2202 This is equivalent to mix(src2, src1, src0);
2204 static void shader_glsl_lrp(const struct wined3d_shader_instruction
*ins
)
2206 glsl_src_param_t src0_param
;
2207 glsl_src_param_t src1_param
;
2208 glsl_src_param_t src2_param
;
2211 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2213 shader_glsl_add_src_param(ins
, &ins
->src
[0], write_mask
, &src0_param
);
2214 shader_glsl_add_src_param(ins
, &ins
->src
[1], write_mask
, &src1_param
);
2215 shader_glsl_add_src_param(ins
, &ins
->src
[2], write_mask
, &src2_param
);
2217 shader_addline(ins
->ctx
->buffer
, "mix(%s, %s, %s));\n",
2218 src2_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
);
2221 /** Process the WINED3DSIO_LIT instruction in GLSL:
2222 * dst.x = dst.w = 1.0
2223 * dst.y = (src0.x > 0) ? src0.x
2224 * dst.z = (src0.x > 0) ? ((src0.y > 0) ? pow(src0.y, src.w) : 0) : 0
2225 * where src.w is clamped at +- 128
2227 static void shader_glsl_lit(const struct wined3d_shader_instruction
*ins
)
2229 glsl_src_param_t src0_param
;
2230 glsl_src_param_t src1_param
;
2231 glsl_src_param_t src3_param
;
2234 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2235 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2237 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2238 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src1_param
);
2239 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &src3_param
);
2241 /* The sdk specifies the instruction like this
2243 * if(src.x > 0.0) dst.y = src.x
2245 * if(src.x > 0.0 && src.y > 0.0) dst.z = pow(src.y, power);
2249 * Obviously that has quite a few conditionals in it which we don't like. So the first step is this:
2250 * dst.x = 1.0 ... No further explanation needed
2251 * dst.y = max(src.y, 0.0); ... If x < 0.0, use 0.0, otherwise x. Same as the conditional
2252 * dst.z = x > 0.0 ? pow(max(y, 0.0), p) : 0; ... 0 ^ power is 0, and otherwise we use y anyway
2253 * dst.w = 1.0. ... Nothing fancy.
2255 * So we still have one conditional in there. So do this:
2256 * dst.z = pow(max(0.0, src.y) * step(0.0, src.x), power);
2258 * 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),
2259 * 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.
2260 * if both x and y are > 0, we get pow(y * 1.0, power), as it is supposed to
2262 shader_addline(ins
->ctx
->buffer
,
2263 "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",
2264 src0_param
.param_str
, src1_param
.param_str
, src0_param
.param_str
, src3_param
.param_str
, dst_mask
);
2267 /** Process the WINED3DSIO_DST instruction in GLSL:
2269 * dst.y = src0.x * src0.y
2273 static void shader_glsl_dst(const struct wined3d_shader_instruction
*ins
)
2275 glsl_src_param_t src0y_param
;
2276 glsl_src_param_t src0z_param
;
2277 glsl_src_param_t src1y_param
;
2278 glsl_src_param_t src1w_param
;
2281 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2282 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2284 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_1
, &src0y_param
);
2285 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &src0z_param
);
2286 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_1
, &src1y_param
);
2287 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_3
, &src1w_param
);
2289 shader_addline(ins
->ctx
->buffer
, "vec4(1.0, %s * %s, %s, %s))%s;\n",
2290 src0y_param
.param_str
, src1y_param
.param_str
, src0z_param
.param_str
, src1w_param
.param_str
, dst_mask
);
2293 /** Process the WINED3DSIO_SINCOS instruction in GLSL:
2294 * VS 2.0 requires that specific cosine and sine constants be passed to this instruction so the hardware
2295 * can handle it. But, these functions are built-in for GLSL, so we can just ignore the last 2 params.
2297 * dst.x = cos(src0.?)
2298 * dst.y = sin(src0.?)
2302 static void shader_glsl_sincos(const struct wined3d_shader_instruction
*ins
)
2304 glsl_src_param_t src0_param
;
2307 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2308 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2310 switch (write_mask
) {
2311 case WINED3DSP_WRITEMASK_0
:
2312 shader_addline(ins
->ctx
->buffer
, "cos(%s));\n", src0_param
.param_str
);
2315 case WINED3DSP_WRITEMASK_1
:
2316 shader_addline(ins
->ctx
->buffer
, "sin(%s));\n", src0_param
.param_str
);
2319 case (WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
):
2320 shader_addline(ins
->ctx
->buffer
, "vec2(cos(%s), sin(%s)));\n", src0_param
.param_str
, src0_param
.param_str
);
2324 ERR("Write mask should be .x, .y or .xy\n");
2329 /** Process the WINED3DSIO_LOOP instruction in GLSL:
2330 * Start a for() loop where src1.y is the initial value of aL,
2331 * increment aL by src1.z for a total of src1.x iterations.
2332 * Need to use a temporary variable for this operation.
2334 /* FIXME: I don't think nested loops will work correctly this way. */
2335 static void shader_glsl_loop(const struct wined3d_shader_instruction
*ins
)
2337 glsl_src_param_t src1_param
;
2338 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2339 const DWORD
*control_values
= NULL
;
2340 const local_constant
*constant
;
2342 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_ALL
, &src1_param
);
2344 /* Try to hardcode the loop control parameters if possible. Direct3D 9 class hardware doesn't support real
2345 * varying indexing, but Microsoft designed this feature for Shader model 2.x+. If the loop control is
2346 * known at compile time, the GLSL compiler can unroll the loop, and replace indirect addressing with direct
2349 if (ins
->src
[1].reg
.type
== WINED3DSPR_CONSTINT
)
2351 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
) {
2352 if (constant
->idx
== ins
->src
[1].reg
.idx
)
2354 control_values
= constant
->value
;
2360 if(control_values
) {
2361 if(control_values
[2] > 0) {
2362 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d; aL%u < (%d * %d + %d); aL%u += %d) {\n",
2363 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2364 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2365 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2366 } else if(control_values
[2] == 0) {
2367 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d, tmpInt%u = 0; tmpInt%u < %d; tmpInt%u++) {\n",
2368 shader
->baseShader
.cur_loop_depth
, control_values
[1], shader
->baseShader
.cur_loop_depth
,
2369 shader
->baseShader
.cur_loop_depth
, control_values
[0],
2370 shader
->baseShader
.cur_loop_depth
);
2372 shader_addline(ins
->ctx
->buffer
, "for (aL%u = %d; aL%u > (%d * %d + %d); aL%u += %d) {\n",
2373 shader
->baseShader
.cur_loop_depth
, control_values
[1],
2374 shader
->baseShader
.cur_loop_depth
, control_values
[0], control_values
[2], control_values
[1],
2375 shader
->baseShader
.cur_loop_depth
, control_values
[2]);
2378 shader_addline(ins
->ctx
->buffer
,
2379 "for (tmpInt%u = 0, aL%u = %s.y; tmpInt%u < %s.x; tmpInt%u++, aL%u += %s.z) {\n",
2380 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
,
2381 src1_param
.reg_name
, shader
->baseShader
.cur_loop_depth
, src1_param
.reg_name
,
2382 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_regno
, src1_param
.reg_name
);
2385 shader
->baseShader
.cur_loop_depth
++;
2386 shader
->baseShader
.cur_loop_regno
++;
2389 static void shader_glsl_end(const struct wined3d_shader_instruction
*ins
)
2391 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2393 shader_addline(ins
->ctx
->buffer
, "}\n");
2395 if (ins
->handler_idx
== WINED3DSIH_ENDLOOP
)
2397 shader
->baseShader
.cur_loop_depth
--;
2398 shader
->baseShader
.cur_loop_regno
--;
2401 if (ins
->handler_idx
== WINED3DSIH_ENDREP
)
2403 shader
->baseShader
.cur_loop_depth
--;
2407 static void shader_glsl_rep(const struct wined3d_shader_instruction
*ins
)
2409 IWineD3DBaseShaderImpl
*shader
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2410 glsl_src_param_t src0_param
;
2411 const DWORD
*control_values
= NULL
;
2412 const local_constant
*constant
;
2414 /* Try to hardcode local values to help the GLSL compiler to unroll and optimize the loop */
2415 if (ins
->src
[0].reg
.type
== WINED3DSPR_CONSTINT
)
2417 LIST_FOR_EACH_ENTRY(constant
, &shader
->baseShader
.constantsI
, local_constant
, entry
)
2419 if (constant
->idx
== ins
->src
[0].reg
.idx
)
2421 control_values
= constant
->value
;
2427 if(control_values
) {
2428 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %d; tmpInt%d++) {\n",
2429 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2430 control_values
[0], shader
->baseShader
.cur_loop_depth
);
2432 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2433 shader_addline(ins
->ctx
->buffer
, "for (tmpInt%d = 0; tmpInt%d < %s; tmpInt%d++) {\n",
2434 shader
->baseShader
.cur_loop_depth
, shader
->baseShader
.cur_loop_depth
,
2435 src0_param
.param_str
, shader
->baseShader
.cur_loop_depth
);
2437 shader
->baseShader
.cur_loop_depth
++;
2440 static void shader_glsl_if(const struct wined3d_shader_instruction
*ins
)
2442 glsl_src_param_t src0_param
;
2444 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2445 shader_addline(ins
->ctx
->buffer
, "if (%s) {\n", src0_param
.param_str
);
2448 static void shader_glsl_ifc(const struct wined3d_shader_instruction
*ins
)
2450 glsl_src_param_t src0_param
;
2451 glsl_src_param_t src1_param
;
2453 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2454 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2456 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) {\n",
2457 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2460 static void shader_glsl_else(const struct wined3d_shader_instruction
*ins
)
2462 shader_addline(ins
->ctx
->buffer
, "} else {\n");
2465 static void shader_glsl_break(const struct wined3d_shader_instruction
*ins
)
2467 shader_addline(ins
->ctx
->buffer
, "break;\n");
2470 /* FIXME: According to MSDN the compare is done per component. */
2471 static void shader_glsl_breakc(const struct wined3d_shader_instruction
*ins
)
2473 glsl_src_param_t src0_param
;
2474 glsl_src_param_t src1_param
;
2476 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
, &src0_param
);
2477 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2479 shader_addline(ins
->ctx
->buffer
, "if (%s %s %s) break;\n",
2480 src0_param
.param_str
, shader_get_comp_op(ins
->flags
), src1_param
.param_str
);
2483 static void shader_glsl_label(const struct wined3d_shader_instruction
*ins
)
2485 shader_addline(ins
->ctx
->buffer
, "}\n");
2486 shader_addline(ins
->ctx
->buffer
, "void subroutine%u () {\n", ins
->src
[0].reg
.idx
);
2489 static void shader_glsl_call(const struct wined3d_shader_instruction
*ins
)
2491 shader_addline(ins
->ctx
->buffer
, "subroutine%u();\n", ins
->src
[0].reg
.idx
);
2494 static void shader_glsl_callnz(const struct wined3d_shader_instruction
*ins
)
2496 glsl_src_param_t src1_param
;
2498 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
, &src1_param
);
2499 shader_addline(ins
->ctx
->buffer
, "if (%s) subroutine%u();\n", src1_param
.param_str
, ins
->src
[0].reg
.idx
);
2502 /*********************************************
2503 * Pixel Shader Specific Code begins here
2504 ********************************************/
2505 static void pshader_glsl_tex(const struct wined3d_shader_instruction
*ins
)
2507 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2508 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2509 DWORD shader_version
= WINED3D_SHADER_VERSION(ins
->ctx
->reg_maps
->shader_version
.major
,
2510 ins
->ctx
->reg_maps
->shader_version
.minor
);
2511 glsl_sample_function_t sample_function
;
2512 DWORD sample_flags
= 0;
2513 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
;
2515 DWORD mask
= 0, swizzle
;
2517 /* 1.0-1.4: Use destination register as sampler source.
2518 * 2.0+: Use provided sampler source. */
2519 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) sampler_idx
= ins
->dst
[0].reg
.idx
;
2520 else sampler_idx
= ins
->src
[1].reg
.idx
;
2521 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2523 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
2525 DWORD flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2527 /* Projected cube textures don't make a lot of sense, the resulting coordinates stay the same. */
2528 if (flags
& WINED3DTTFF_PROJECTED
&& sampler_type
!= WINED3DSTT_CUBE
) {
2529 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2530 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2531 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2532 case WINED3DTTFF_COUNT2
: mask
= WINED3DSP_WRITEMASK_1
; break;
2533 case WINED3DTTFF_COUNT3
: mask
= WINED3DSP_WRITEMASK_2
; break;
2534 case WINED3DTTFF_COUNT4
:
2535 case WINED3DTTFF_DISABLE
: mask
= WINED3DSP_WRITEMASK_3
; break;
2539 else if (shader_version
< WINED3D_SHADER_VERSION(2,0))
2541 DWORD src_mod
= ins
->src
[0].modifiers
;
2543 if (src_mod
== WINED3DSPSM_DZ
) {
2544 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2545 mask
= WINED3DSP_WRITEMASK_2
;
2546 } else if (src_mod
== WINED3DSPSM_DW
) {
2547 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2548 mask
= WINED3DSP_WRITEMASK_3
;
2551 if (ins
->flags
& WINED3DSI_TEXLD_PROJECT
)
2553 /* ps 2.0 texldp instruction always divides by the fourth component. */
2554 sample_flags
|= WINED3D_GLSL_SAMPLE_PROJECTED
;
2555 mask
= WINED3DSP_WRITEMASK_3
;
2559 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2560 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2561 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2564 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2565 mask
|= sample_function
.coord_mask
;
2567 if (shader_version
< WINED3D_SHADER_VERSION(2,0)) swizzle
= WINED3DSP_NOSWIZZLE
;
2568 else swizzle
= ins
->src
[1].swizzle
;
2570 /* 1.0-1.3: Use destination register as coordinate source.
2571 1.4+: Use provided coordinate source register. */
2572 if (shader_version
< WINED3D_SHADER_VERSION(1,4))
2575 shader_glsl_write_mask_to_str(mask
, coord_mask
);
2576 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
2577 "T%u%s", sampler_idx
, coord_mask
);
2579 glsl_src_param_t coord_param
;
2580 shader_glsl_add_src_param(ins
, &ins
->src
[0], mask
, &coord_param
);
2581 if (ins
->flags
& WINED3DSI_TEXLD_BIAS
)
2583 glsl_src_param_t bias
;
2584 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &bias
);
2585 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, bias
.param_str
,
2586 "%s", coord_param
.param_str
);
2588 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, NULL
,
2589 "%s", coord_param
.param_str
);
2594 static void shader_glsl_texldd(const struct wined3d_shader_instruction
*ins
)
2596 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2597 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2598 const WineD3D_GL_Info
* gl_info
= &deviceImpl
->adapter
->gl_info
;
2599 glsl_sample_function_t sample_function
;
2600 glsl_src_param_t coord_param
, dx_param
, dy_param
;
2601 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_GRAD
;
2604 DWORD swizzle
= ins
->src
[1].swizzle
;
2606 if(!GL_SUPPORT(ARB_SHADER_TEXTURE_LOD
)) {
2607 FIXME("texldd used, but not supported by hardware. Falling back to regular tex\n");
2608 return pshader_glsl_tex(ins
);
2611 sampler_idx
= ins
->src
[1].reg
.idx
;
2612 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2613 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2614 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2615 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2618 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2619 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
2620 shader_glsl_add_src_param(ins
, &ins
->src
[2], sample_function
.coord_mask
, &dx_param
);
2621 shader_glsl_add_src_param(ins
, &ins
->src
[3], sample_function
.coord_mask
, &dy_param
);
2623 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, dx_param
.param_str
, dy_param
.param_str
, NULL
,
2624 "%s", coord_param
.param_str
);
2627 static void shader_glsl_texldl(const struct wined3d_shader_instruction
*ins
)
2629 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*)ins
->ctx
->shader
;
2630 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2631 glsl_sample_function_t sample_function
;
2632 glsl_src_param_t coord_param
, lod_param
;
2633 DWORD sample_flags
= WINED3D_GLSL_SAMPLE_LOD
;
2636 DWORD swizzle
= ins
->src
[1].swizzle
;
2638 sampler_idx
= ins
->src
[1].reg
.idx
;
2639 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2640 if(deviceImpl
->stateBlock
->textures
[sampler_idx
] &&
2641 IWineD3DBaseTexture_GetTextureDimensions(deviceImpl
->stateBlock
->textures
[sampler_idx
]) == GL_TEXTURE_RECTANGLE_ARB
) {
2642 sample_flags
|= WINED3D_GLSL_SAMPLE_RECT
;
2644 shader_glsl_get_sample_function(sampler_type
, sample_flags
, &sample_function
);
2645 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &coord_param
);
2647 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &lod_param
);
2649 if (shader_is_pshader_version(ins
->ctx
->reg_maps
->shader_version
.type
))
2651 /* The GLSL spec claims the Lod sampling functions are only supported in vertex shaders.
2652 * However, they seem to work just fine in fragment shaders as well. */
2653 WARN("Using %s in fragment shader.\n", sample_function
.name
);
2655 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, swizzle
, NULL
, NULL
, lod_param
.param_str
,
2656 "%s", coord_param
.param_str
);
2659 static void pshader_glsl_texcoord(const struct wined3d_shader_instruction
*ins
)
2661 /* FIXME: Make this work for more than just 2D textures */
2662 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2663 DWORD write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2665 if (!(ins
->ctx
->reg_maps
->shader_version
.major
== 1 && ins
->ctx
->reg_maps
->shader_version
.minor
== 4))
2669 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2670 shader_addline(buffer
, "clamp(gl_TexCoord[%u], 0.0, 1.0)%s);\n",
2671 ins
->dst
[0].reg
.idx
, dst_mask
);
2673 DWORD reg
= ins
->src
[0].reg
.idx
;
2674 DWORD src_mod
= ins
->src
[0].modifiers
;
2675 char dst_swizzle
[6];
2677 shader_glsl_get_swizzle(&ins
->src
[0], FALSE
, write_mask
, dst_swizzle
);
2679 if (src_mod
== WINED3DSPSM_DZ
) {
2680 glsl_src_param_t div_param
;
2681 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2682 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &div_param
);
2684 if (mask_size
> 1) {
2685 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2687 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2689 } else if (src_mod
== WINED3DSPSM_DW
) {
2690 glsl_src_param_t div_param
;
2691 unsigned int mask_size
= shader_glsl_get_write_mask_size(write_mask
);
2692 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_3
, &div_param
);
2694 if (mask_size
> 1) {
2695 shader_addline(buffer
, "gl_TexCoord[%u]%s / vec%d(%s));\n", reg
, dst_swizzle
, mask_size
, div_param
.param_str
);
2697 shader_addline(buffer
, "gl_TexCoord[%u]%s / %s);\n", reg
, dst_swizzle
, div_param
.param_str
);
2700 shader_addline(buffer
, "gl_TexCoord[%u]%s);\n", reg
, dst_swizzle
);
2705 /** Process the WINED3DSIO_TEXDP3TEX instruction in GLSL:
2706 * Take a 3-component dot product of the TexCoord[dstreg] and src,
2707 * then perform a 1D texture lookup from stage dstregnum, place into dst. */
2708 static void pshader_glsl_texdp3tex(const struct wined3d_shader_instruction
*ins
)
2710 glsl_src_param_t src0_param
;
2711 glsl_sample_function_t sample_function
;
2712 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
2713 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2714 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2717 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2719 /* Do I have to take care about the projected bit? I don't think so, since the dp3 returns only one
2720 * scalar, and projected sampling would require 4.
2722 * It is a dependent read - not valid with conditional NP2 textures
2724 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2725 mask_size
= shader_glsl_get_write_mask_size(sample_function
.coord_mask
);
2730 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2731 "dot(gl_TexCoord[%u].xyz, %s)", sampler_idx
, src0_param
.param_str
);
2735 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2736 "vec2(dot(gl_TexCoord[%u].xyz, %s), 0.0)", sampler_idx
, src0_param
.param_str
);
2740 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
2741 "vec3(dot(gl_TexCoord[%u].xyz, %s), 0.0, 0.0)", sampler_idx
, src0_param
.param_str
);
2745 FIXME("Unexpected mask size %u\n", mask_size
);
2750 /** Process the WINED3DSIO_TEXDP3 instruction in GLSL:
2751 * Take a 3-component dot product of the TexCoord[dstreg] and src. */
2752 static void pshader_glsl_texdp3(const struct wined3d_shader_instruction
*ins
)
2754 glsl_src_param_t src0_param
;
2755 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
2756 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2758 unsigned int mask_size
;
2760 dst_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2761 mask_size
= shader_glsl_get_write_mask_size(dst_mask
);
2762 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2764 if (mask_size
> 1) {
2765 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(T%u.xyz, %s)));\n", mask_size
, dstreg
, src0_param
.param_str
);
2767 shader_addline(ins
->ctx
->buffer
, "dot(T%u.xyz, %s));\n", dstreg
, src0_param
.param_str
);
2771 /** Process the WINED3DSIO_TEXDEPTH instruction in GLSL:
2772 * Calculate the depth as dst.x / dst.y */
2773 static void pshader_glsl_texdepth(const struct wined3d_shader_instruction
*ins
)
2775 glsl_dst_param_t dst_param
;
2777 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
2779 /* Tests show that texdepth never returns anything below 0.0, and that r5.y is clamped to 1.0.
2780 * Negative input is accepted, -0.25 / -0.5 returns 0.5. GL should clamp gl_FragDepth to [0;1], but
2781 * this doesn't always work, so clamp the results manually. Whether or not the x value is clamped at 1
2782 * too is irrelevant, since if x = 0, any y value < 1.0 (and > 1.0 is not allowed) results in a result
2785 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = clamp((%s.x / min(%s.y, 1.0)), 0.0, 1.0);\n",
2786 dst_param
.reg_name
, dst_param
.reg_name
);
2789 /** Process the WINED3DSIO_TEXM3X2DEPTH instruction in GLSL:
2790 * Last row of a 3x2 matrix multiply, use the result to calculate the depth:
2791 * Calculate tmp0.y = TexCoord[dstreg] . src.xyz; (tmp0.x has already been calculated)
2792 * depth = (tmp0.y == 0.0) ? 1.0 : tmp0.x / tmp0.y
2794 static void pshader_glsl_texm3x2depth(const struct wined3d_shader_instruction
*ins
)
2796 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2797 DWORD dstreg
= ins
->dst
[0].reg
.idx
;
2798 glsl_src_param_t src0_param
;
2800 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2802 shader_addline(ins
->ctx
->buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", dstreg
, src0_param
.param_str
);
2803 shader_addline(ins
->ctx
->buffer
, "gl_FragDepth = (tmp0.y == 0.0) ? 1.0 : clamp(tmp0.x / tmp0.y, 0.0, 1.0);\n");
2806 /** Process the WINED3DSIO_TEXM3X2PAD instruction in GLSL
2807 * Calculate the 1st of a 2-row matrix multiplication. */
2808 static void pshader_glsl_texm3x2pad(const struct wined3d_shader_instruction
*ins
)
2810 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2811 DWORD reg
= ins
->dst
[0].reg
.idx
;
2812 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2813 glsl_src_param_t src0_param
;
2815 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2816 shader_addline(buffer
, "tmp0.x = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2819 /** Process the WINED3DSIO_TEXM3X3PAD instruction in GLSL
2820 * Calculate the 1st or 2nd row of a 3-row matrix multiplication. */
2821 static void pshader_glsl_texm3x3pad(const struct wined3d_shader_instruction
*ins
)
2823 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2824 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2825 DWORD reg
= ins
->dst
[0].reg
.idx
;
2826 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2827 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2828 glsl_src_param_t src0_param
;
2830 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2831 shader_addline(buffer
, "tmp0.%c = dot(T%u.xyz, %s);\n", 'x' + current_state
->current_row
, reg
, src0_param
.param_str
);
2832 current_state
->texcoord_w
[current_state
->current_row
++] = reg
;
2835 static void pshader_glsl_texm3x2tex(const struct wined3d_shader_instruction
*ins
)
2837 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2838 DWORD reg
= ins
->dst
[0].reg
.idx
;
2839 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2840 glsl_src_param_t src0_param
;
2841 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2842 glsl_sample_function_t sample_function
;
2844 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2845 shader_addline(buffer
, "tmp0.y = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2847 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2849 /* Sample the texture using the calculated coordinates */
2850 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xy");
2853 /** Process the WINED3DSIO_TEXM3X3TEX instruction in GLSL
2854 * Perform the 3rd row of a 3x3 matrix multiply, then sample the texture using the calculated coordinates */
2855 static void pshader_glsl_texm3x3tex(const struct wined3d_shader_instruction
*ins
)
2857 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2858 glsl_src_param_t src0_param
;
2859 DWORD reg
= ins
->dst
[0].reg
.idx
;
2860 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2861 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2862 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2863 glsl_sample_function_t sample_function
;
2865 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2866 shader_addline(ins
->ctx
->buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2868 /* Dependent read, not valid with conditional NP2 */
2869 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2871 /* Sample the texture using the calculated coordinates */
2872 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
2874 current_state
->current_row
= 0;
2877 /** Process the WINED3DSIO_TEXM3X3 instruction in GLSL
2878 * Perform the 3rd row of a 3x3 matrix multiply */
2879 static void pshader_glsl_texm3x3(const struct wined3d_shader_instruction
*ins
)
2881 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2882 glsl_src_param_t src0_param
;
2884 DWORD reg
= ins
->dst
[0].reg
.idx
;
2885 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2886 SHADER_PARSE_STATE
* current_state
= &This
->baseShader
.parse_state
;
2888 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2890 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
2891 shader_glsl_get_write_mask(&ins
->dst
[0], dst_mask
);
2892 shader_addline(ins
->ctx
->buffer
, "vec4(tmp0.xy, dot(T%u.xyz, %s), 1.0)%s);\n", reg
, src0_param
.param_str
, dst_mask
);
2894 current_state
->current_row
= 0;
2897 /** Process the WINED3DSIO_TEXM3X3SPEC instruction in GLSL
2898 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2899 static void pshader_glsl_texm3x3spec(const struct wined3d_shader_instruction
*ins
)
2901 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2902 DWORD reg
= ins
->dst
[0].reg
.idx
;
2903 glsl_src_param_t src0_param
;
2904 glsl_src_param_t src1_param
;
2905 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2906 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2907 WINED3DSAMPLER_TEXTURE_TYPE stype
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2908 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2909 glsl_sample_function_t sample_function
;
2911 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2912 shader_glsl_add_src_param(ins
, &ins
->src
[1], src_mask
, &src1_param
);
2914 /* Perform the last matrix multiply operation */
2915 shader_addline(buffer
, "tmp0.z = dot(T%u.xyz, %s);\n", reg
, src0_param
.param_str
);
2916 /* Reflection calculation */
2917 shader_addline(buffer
, "tmp0.xyz = -reflect((%s), normalize(tmp0.xyz));\n", src1_param
.param_str
);
2919 /* Dependent read, not valid with conditional NP2 */
2920 shader_glsl_get_sample_function(stype
, 0, &sample_function
);
2922 /* Sample the texture */
2923 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
2925 current_state
->current_row
= 0;
2928 /** Process the WINED3DSIO_TEXM3X3VSPEC instruction in GLSL
2929 * Perform the final texture lookup based on the previous 2 3x3 matrix multiplies */
2930 static void pshader_glsl_texm3x3vspec(const struct wined3d_shader_instruction
*ins
)
2932 IWineD3DPixelShaderImpl
*shader
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2933 DWORD reg
= ins
->dst
[0].reg
.idx
;
2934 SHADER_BUFFER
*buffer
= ins
->ctx
->buffer
;
2935 SHADER_PARSE_STATE
* current_state
= &shader
->baseShader
.parse_state
;
2936 glsl_src_param_t src0_param
;
2937 DWORD src_mask
= WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
| WINED3DSP_WRITEMASK_2
;
2938 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[reg
];
2939 glsl_sample_function_t sample_function
;
2941 shader_glsl_add_src_param(ins
, &ins
->src
[0], src_mask
, &src0_param
);
2943 /* Perform the last matrix multiply operation */
2944 shader_addline(buffer
, "tmp0.z = dot(vec3(T%u), vec3(%s));\n", reg
, src0_param
.param_str
);
2946 /* Construct the eye-ray vector from w coordinates */
2947 shader_addline(buffer
, "tmp1.xyz = normalize(vec3(gl_TexCoord[%u].w, gl_TexCoord[%u].w, gl_TexCoord[%u].w));\n",
2948 current_state
->texcoord_w
[0], current_state
->texcoord_w
[1], reg
);
2949 shader_addline(buffer
, "tmp0.xyz = -reflect(tmp1.xyz, normalize(tmp0.xyz));\n");
2951 /* Dependent read, not valid with conditional NP2 */
2952 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2954 /* Sample the texture using the calculated coordinates */
2955 shader_glsl_gen_sample_code(ins
, reg
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
, "tmp0.xyz");
2957 current_state
->current_row
= 0;
2960 /** Process the WINED3DSIO_TEXBEM instruction in GLSL.
2961 * Apply a fake bump map transform.
2962 * texbem is pshader <= 1.3 only, this saves a few version checks
2964 static void pshader_glsl_texbem(const struct wined3d_shader_instruction
*ins
)
2966 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)ins
->ctx
->shader
;
2967 IWineD3DDeviceImpl
* deviceImpl
= (IWineD3DDeviceImpl
*) This
->baseShader
.device
;
2968 glsl_sample_function_t sample_function
;
2969 glsl_src_param_t coord_param
;
2970 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
;
2976 sampler_idx
= ins
->dst
[0].reg
.idx
;
2977 flags
= deviceImpl
->stateBlock
->textureState
[sampler_idx
][WINED3DTSS_TEXTURETRANSFORMFLAGS
];
2979 sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
2980 /* Dependent read, not valid with conditional NP2 */
2981 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
2982 mask
= sample_function
.coord_mask
;
2984 shader_glsl_write_mask_to_str(mask
, coord_mask
);
2986 /* with projective textures, texbem only divides the static texture coord, not the displacement,
2987 * so we can't let the GL handle this.
2989 if (flags
& WINED3DTTFF_PROJECTED
) {
2991 char coord_div_mask
[3];
2992 switch (flags
& ~WINED3DTTFF_PROJECTED
) {
2993 case WINED3DTTFF_COUNT1
: FIXME("WINED3DTTFF_PROJECTED with WINED3DTTFF_COUNT1?\n"); break;
2994 case WINED3DTTFF_COUNT2
: div_mask
= WINED3DSP_WRITEMASK_1
; break;
2995 case WINED3DTTFF_COUNT3
: div_mask
= WINED3DSP_WRITEMASK_2
; break;
2996 case WINED3DTTFF_COUNT4
:
2997 case WINED3DTTFF_DISABLE
: div_mask
= WINED3DSP_WRITEMASK_3
; break;
2999 shader_glsl_write_mask_to_str(div_mask
, coord_div_mask
);
3000 shader_addline(ins
->ctx
->buffer
, "T%u%s /= T%u%s;\n", sampler_idx
, coord_mask
, sampler_idx
, coord_div_mask
);
3003 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &coord_param
);
3005 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3006 "T%u%s + vec4(bumpenvmat%d * %s, 0.0, 0.0)%s", sampler_idx
, coord_mask
, sampler_idx
,
3007 coord_param
.param_str
, coord_mask
);
3009 if (ins
->handler_idx
== WINED3DSIH_TEXBEML
)
3011 glsl_src_param_t luminance_param
;
3012 glsl_dst_param_t dst_param
;
3014 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_2
, &luminance_param
);
3015 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3017 shader_addline(ins
->ctx
->buffer
, "%s%s *= (%s * luminancescale%d + luminanceoffset%d);\n",
3018 dst_param
.reg_name
, dst_param
.mask_str
,
3019 luminance_param
.param_str
, sampler_idx
, sampler_idx
);
3023 static void pshader_glsl_bem(const struct wined3d_shader_instruction
*ins
)
3025 glsl_src_param_t src0_param
, src1_param
;
3026 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3028 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3029 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3031 shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3032 shader_addline(ins
->ctx
->buffer
, "%s + bumpenvmat%d * %s);\n",
3033 src0_param
.param_str
, sampler_idx
, src1_param
.param_str
);
3036 /** Process the WINED3DSIO_TEXREG2AR instruction in GLSL
3037 * Sample 2D texture at dst using the alpha & red (wx) components of src as texture coordinates */
3038 static void pshader_glsl_texreg2ar(const struct wined3d_shader_instruction
*ins
)
3040 glsl_src_param_t src0_param
;
3041 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3042 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3043 glsl_sample_function_t sample_function
;
3045 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3047 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3048 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3049 "%s.wx", src0_param
.reg_name
);
3052 /** Process the WINED3DSIO_TEXREG2GB instruction in GLSL
3053 * Sample 2D texture at dst using the green & blue (yz) components of src as texture coordinates */
3054 static void pshader_glsl_texreg2gb(const struct wined3d_shader_instruction
*ins
)
3056 glsl_src_param_t src0_param
;
3057 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3058 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3059 glsl_sample_function_t sample_function
;
3061 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_ALL
, &src0_param
);
3063 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3064 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3065 "%s.yz", src0_param
.reg_name
);
3068 /** Process the WINED3DSIO_TEXREG2RGB instruction in GLSL
3069 * Sample texture at dst using the rgb (xyz) components of src as texture coordinates */
3070 static void pshader_glsl_texreg2rgb(const struct wined3d_shader_instruction
*ins
)
3072 glsl_src_param_t src0_param
;
3073 DWORD sampler_idx
= ins
->dst
[0].reg
.idx
;
3074 WINED3DSAMPLER_TEXTURE_TYPE sampler_type
= ins
->ctx
->reg_maps
->sampler_type
[sampler_idx
];
3075 glsl_sample_function_t sample_function
;
3077 /* Dependent read, not valid with conditional NP2 */
3078 shader_glsl_get_sample_function(sampler_type
, 0, &sample_function
);
3079 shader_glsl_add_src_param(ins
, &ins
->src
[0], sample_function
.coord_mask
, &src0_param
);
3081 shader_glsl_gen_sample_code(ins
, sampler_idx
, &sample_function
, WINED3DSP_NOSWIZZLE
, NULL
, NULL
, NULL
,
3082 "%s", src0_param
.param_str
);
3085 /** Process the WINED3DSIO_TEXKILL instruction in GLSL.
3086 * If any of the first 3 components are < 0, discard this pixel */
3087 static void pshader_glsl_texkill(const struct wined3d_shader_instruction
*ins
)
3089 glsl_dst_param_t dst_param
;
3091 /* The argument is a destination parameter, and no writemasks are allowed */
3092 shader_glsl_add_dst_param(ins
, &ins
->dst
[0], &dst_param
);
3093 if (ins
->ctx
->reg_maps
->shader_version
.major
>= 2)
3095 /* 2.0 shaders compare all 4 components in texkill */
3096 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyzw, vec4(0.0)))) discard;\n", dst_param
.reg_name
);
3098 /* 1.X shaders only compare the first 3 components, probably due to the nature of the texkill
3099 * instruction as a tex* instruction, and phase, which kills all a / w components. Even if all
3100 * 4 components are defined, only the first 3 are used
3102 shader_addline(ins
->ctx
->buffer
, "if (any(lessThan(%s.xyz, vec3(0.0)))) discard;\n", dst_param
.reg_name
);
3106 /** Process the WINED3DSIO_DP2ADD instruction in GLSL.
3107 * dst = dot2(src0, src1) + src2 */
3108 static void pshader_glsl_dp2add(const struct wined3d_shader_instruction
*ins
)
3110 glsl_src_param_t src0_param
;
3111 glsl_src_param_t src1_param
;
3112 glsl_src_param_t src2_param
;
3114 unsigned int mask_size
;
3116 write_mask
= shader_glsl_append_dst(ins
->ctx
->buffer
, ins
);
3117 mask_size
= shader_glsl_get_write_mask_size(write_mask
);
3119 shader_glsl_add_src_param(ins
, &ins
->src
[0], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src0_param
);
3120 shader_glsl_add_src_param(ins
, &ins
->src
[1], WINED3DSP_WRITEMASK_0
| WINED3DSP_WRITEMASK_1
, &src1_param
);
3121 shader_glsl_add_src_param(ins
, &ins
->src
[2], WINED3DSP_WRITEMASK_0
, &src2_param
);
3123 if (mask_size
> 1) {
3124 shader_addline(ins
->ctx
->buffer
, "vec%d(dot(%s, %s) + %s));\n",
3125 mask_size
, src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3127 shader_addline(ins
->ctx
->buffer
, "dot(%s, %s) + %s);\n",
3128 src0_param
.param_str
, src1_param
.param_str
, src2_param
.param_str
);
3132 static void pshader_glsl_input_pack(IWineD3DPixelShader
*iface
, SHADER_BUFFER
*buffer
,
3133 const struct wined3d_shader_semantic
*semantics_in
, const struct shader_reg_maps
*reg_maps
,
3134 enum vertexprocessing_mode vertexprocessing
)
3137 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
3139 for (i
= 0; i
< MAX_REG_INPUT
; ++i
)
3141 DWORD usage
, usage_idx
;
3145 if (!reg_maps
->packed_input
[i
]) continue;
3147 usage
= semantics_in
[i
].usage
;
3148 usage_idx
= semantics_in
[i
].usage_idx
;
3149 shader_glsl_get_write_mask(&semantics_in
[i
].reg
, reg_mask
);
3153 case WINED3DDECLUSAGE_TEXCOORD
:
3154 if (usage_idx
< 8 && vertexprocessing
== pretransformed
)
3155 shader_addline(buffer
, "IN[%u]%s = gl_TexCoord[%u]%s;\n",
3156 This
->input_reg_map
[i
], reg_mask
, usage_idx
, reg_mask
);
3158 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3159 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3162 case WINED3DDECLUSAGE_COLOR
:
3164 shader_addline(buffer
, "IN[%u]%s = vec4(gl_Color)%s;\n",
3165 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3166 else if (usage_idx
== 1)
3167 shader_addline(buffer
, "IN[%u]%s = vec4(gl_SecondaryColor)%s;\n",
3168 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3170 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3171 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3175 shader_addline(buffer
, "IN[%u]%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3176 This
->input_reg_map
[i
], reg_mask
, reg_mask
);
3182 /*********************************************
3183 * Vertex Shader Specific Code begins here
3184 ********************************************/
3186 static void add_glsl_program_entry(struct shader_glsl_priv
*priv
, struct glsl_shader_prog_link
*entry
) {
3187 glsl_program_key_t
*key
;
3189 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
3190 key
->vshader
= entry
->vshader
;
3191 key
->pshader
= entry
->pshader
;
3192 key
->vs_args
= entry
->vs_args
;
3193 key
->ps_args
= entry
->ps_args
;
3195 hash_table_put(priv
->glsl_program_lookup
, key
, entry
);
3198 static struct glsl_shader_prog_link
*get_glsl_program_entry(struct shader_glsl_priv
*priv
,
3199 IWineD3DVertexShader
*vshader
, IWineD3DPixelShader
*pshader
, struct vs_compile_args
*vs_args
,
3200 struct ps_compile_args
*ps_args
) {
3201 glsl_program_key_t key
;
3203 key
.vshader
= vshader
;
3204 key
.pshader
= pshader
;
3205 key
.vs_args
= *vs_args
;
3206 key
.ps_args
= *ps_args
;
3208 return hash_table_get(priv
->glsl_program_lookup
, &key
);
3211 /* GL locking is done by the caller */
3212 static void delete_glsl_program_entry(struct shader_glsl_priv
*priv
, const WineD3D_GL_Info
*gl_info
,
3213 struct glsl_shader_prog_link
*entry
)
3215 glsl_program_key_t
*key
;
3217 key
= HeapAlloc(GetProcessHeap(), 0, sizeof(glsl_program_key_t
));
3218 key
->vshader
= entry
->vshader
;
3219 key
->pshader
= entry
->pshader
;
3220 key
->vs_args
= entry
->vs_args
;
3221 key
->ps_args
= entry
->ps_args
;
3222 hash_table_remove(priv
->glsl_program_lookup
, key
);
3224 GL_EXTCALL(glDeleteObjectARB(entry
->programId
));
3225 if (entry
->vshader
) list_remove(&entry
->vshader_entry
);
3226 if (entry
->pshader
) list_remove(&entry
->pshader_entry
);
3227 HeapFree(GetProcessHeap(), 0, entry
->vuniformF_locations
);
3228 HeapFree(GetProcessHeap(), 0, entry
->puniformF_locations
);
3229 HeapFree(GetProcessHeap(), 0, entry
);
3232 static void handle_ps3_input(SHADER_BUFFER
*buffer
, const WineD3D_GL_Info
*gl_info
, const DWORD
*map
,
3233 const struct wined3d_shader_semantic
*semantics_in
, const struct shader_reg_maps
*reg_maps_in
,
3234 const struct wined3d_shader_semantic
*semantics_out
, const struct shader_reg_maps
*reg_maps_out
)
3237 DWORD usage
, usage_idx
, usage_out
, usage_idx_out
;
3240 DWORD in_count
= GL_LIMITS(glsl_varyings
) / 4;
3241 char reg_mask
[6], reg_mask_out
[6];
3242 char destination
[50];
3244 set
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(*set
) * (in_count
+ 2));
3246 if (!semantics_out
) {
3247 /* Save gl_FrontColor & gl_FrontSecondaryColor before overwriting them. */
3248 shader_addline(buffer
, "vec4 front_color = gl_FrontColor;\n");
3249 shader_addline(buffer
, "vec4 front_secondary_color = gl_FrontSecondaryColor;\n");
3252 for(i
= 0; i
< MAX_REG_INPUT
; i
++) {
3253 if (!reg_maps_in
->packed_input
[i
]) continue;
3256 if (in_idx
>= (in_count
+ 2)) {
3257 FIXME("More input varyings declared than supported, expect issues\n");
3260 else if (map
[i
] == ~0U)
3262 /* Declared, but not read register */
3266 if (in_idx
== in_count
) {
3267 sprintf(destination
, "gl_FrontColor");
3268 } else if (in_idx
== in_count
+ 1) {
3269 sprintf(destination
, "gl_FrontSecondaryColor");
3271 sprintf(destination
, "IN[%u]", in_idx
);
3274 usage
= semantics_in
[i
].usage
;
3275 usage_idx
= semantics_in
[i
].usage_idx
;
3276 set
[map
[i
]] = shader_glsl_get_write_mask(&semantics_in
[i
].reg
, reg_mask
);
3278 if(!semantics_out
) {
3280 case WINED3DDECLUSAGE_COLOR
:
3282 shader_addline(buffer
, "%s%s = front_color%s;\n",
3283 destination
, reg_mask
, reg_mask
);
3284 else if (usage_idx
== 1)
3285 shader_addline(buffer
, "%s%s = front_secondary_color%s;\n",
3286 destination
, reg_mask
, reg_mask
);
3288 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3289 destination
, reg_mask
, reg_mask
);
3292 case WINED3DDECLUSAGE_TEXCOORD
:
3293 if (usage_idx
< 8) {
3294 shader_addline(buffer
, "%s%s = gl_TexCoord[%u]%s;\n",
3295 destination
, reg_mask
, usage_idx
, reg_mask
);
3297 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3298 destination
, reg_mask
, reg_mask
);
3302 case WINED3DDECLUSAGE_FOG
:
3303 shader_addline(buffer
, "%s%s = vec4(gl_FogFragCoord, 0.0, 0.0, 0.0)%s;\n",
3304 destination
, reg_mask
, reg_mask
);
3308 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3309 destination
, reg_mask
, reg_mask
);
3313 for(j
= 0; j
< MAX_REG_OUTPUT
; j
++) {
3314 if (!reg_maps_out
->packed_output
[j
]) continue;
3316 usage_out
= semantics_out
[j
].usage
;
3317 usage_idx_out
= semantics_out
[j
].usage_idx
;
3318 shader_glsl_get_write_mask(&semantics_out
[j
].reg
, reg_mask_out
);
3320 if(usage
== usage_out
&&
3321 usage_idx
== usage_idx_out
) {
3322 shader_addline(buffer
, "%s%s = OUT[%u]%s;\n",
3323 destination
, reg_mask
, j
, reg_mask
);
3328 shader_addline(buffer
, "%s%s = vec4(0.0, 0.0, 0.0, 0.0)%s;\n",
3329 destination
, reg_mask
, reg_mask
);
3334 /* This is solely to make the compiler / linker happy and avoid warning about undefined
3335 * varyings. It shouldn't result in any real code executed on the GPU, since all read
3336 * input varyings are assigned above, if the optimizer works properly.
3338 for(i
= 0; i
< in_count
+ 2; i
++) {
3339 if(set
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3340 unsigned int size
= 0;
3341 memset(reg_mask
, 0, sizeof(reg_mask
));
3342 if(!(set
[i
] & WINED3DSP_WRITEMASK_0
)) {
3343 reg_mask
[size
] = 'x';
3346 if(!(set
[i
] & WINED3DSP_WRITEMASK_1
)) {
3347 reg_mask
[size
] = 'y';
3350 if(!(set
[i
] & WINED3DSP_WRITEMASK_2
)) {
3351 reg_mask
[size
] = 'z';
3354 if(!(set
[i
] & WINED3DSP_WRITEMASK_3
)) {
3355 reg_mask
[size
] = 'w';
3359 if (i
== in_count
) {
3360 sprintf(destination
, "gl_FrontColor");
3361 } else if (i
== in_count
+ 1) {
3362 sprintf(destination
, "gl_FrontSecondaryColor");
3364 sprintf(destination
, "IN[%u]", i
);
3368 shader_addline(buffer
, "%s.%s = 0.0;\n", destination
, reg_mask
);
3370 shader_addline(buffer
, "%s.%s = vec%u(0.0);\n", destination
, reg_mask
, size
);
3375 HeapFree(GetProcessHeap(), 0, set
);
3378 /* GL locking is done by the caller */
3379 static GLhandleARB
generate_param_reorder_function(IWineD3DVertexShader
*vertexshader
,
3380 IWineD3DPixelShader
*pixelshader
, const WineD3D_GL_Info
*gl_info
)
3382 GLhandleARB ret
= 0;
3383 IWineD3DVertexShaderImpl
*vs
= (IWineD3DVertexShaderImpl
*) vertexshader
;
3384 IWineD3DPixelShaderImpl
*ps
= (IWineD3DPixelShaderImpl
*) pixelshader
;
3385 IWineD3DDeviceImpl
*device
;
3386 DWORD vs_major
= vs
->baseShader
.reg_maps
.shader_version
.major
;
3387 DWORD ps_major
= ps
? ps
->baseShader
.reg_maps
.shader_version
.major
: 0;
3389 SHADER_BUFFER buffer
;
3390 DWORD usage
, usage_idx
, writemask
;
3392 const struct wined3d_shader_semantic
*semantics_out
;
3394 shader_buffer_init(&buffer
);
3396 shader_addline(&buffer
, "#version 120\n");
3398 if(vs_major
< 3 && ps_major
< 3) {
3399 /* That one is easy: The vertex shader writes to the builtin varyings, the pixel shader reads from them.
3400 * Take care about the texcoord .w fixup though if we're using the fixed function fragment pipeline
3402 device
= (IWineD3DDeviceImpl
*) vs
->baseShader
.device
;
3403 if((GLINFO_LOCATION
).set_texcoord_w
&& ps_major
== 0 && vs_major
> 0 &&
3404 !device
->frag_pipe
->ffp_proj_control
) {
3405 shader_addline(&buffer
, "void order_ps_input() {\n");
3406 for(i
= 0; i
< min(8, MAX_REG_TEXCRD
); i
++) {
3407 if(vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != 0 &&
3408 vs
->baseShader
.reg_maps
.texcoord_mask
[i
] != WINED3DSP_WRITEMASK_ALL
) {
3409 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", i
);
3412 shader_addline(&buffer
, "}\n");
3414 shader_addline(&buffer
, "void order_ps_input() { /* do nothing */ }\n");
3416 } else if(ps_major
< 3 && vs_major
>= 3) {
3417 /* The vertex shader writes to its own varyings, the pixel shader needs them in the builtin ones */
3418 semantics_out
= vs
->semantics_out
;
3420 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3421 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
3422 if (!vs
->baseShader
.reg_maps
.packed_output
[i
]) continue;
3424 usage
= semantics_out
[i
].usage
;
3425 usage_idx
= semantics_out
[i
].usage_idx
;
3426 writemask
= shader_glsl_get_write_mask(&semantics_out
[i
].reg
, reg_mask
);
3429 case WINED3DDECLUSAGE_COLOR
:
3431 shader_addline(&buffer
, "gl_FrontColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3432 else if (usage_idx
== 1)
3433 shader_addline(&buffer
, "gl_FrontSecondaryColor%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3436 case WINED3DDECLUSAGE_POSITION
:
3437 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3440 case WINED3DDECLUSAGE_TEXCOORD
:
3441 if (usage_idx
< 8) {
3442 if(!(GLINFO_LOCATION
).set_texcoord_w
|| ps_major
> 0) writemask
|= WINED3DSP_WRITEMASK_3
;
3444 shader_addline(&buffer
, "gl_TexCoord[%u]%s = OUT[%u]%s;\n",
3445 usage_idx
, reg_mask
, i
, reg_mask
);
3446 if(!(writemask
& WINED3DSP_WRITEMASK_3
)) {
3447 shader_addline(&buffer
, "gl_TexCoord[%u].w = 1.0;\n", usage_idx
);
3452 case WINED3DDECLUSAGE_PSIZE
:
3453 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3456 case WINED3DDECLUSAGE_FOG
:
3457 shader_addline(&buffer
, "gl_FogFragCoord = OUT[%u].%c;\n", i
, reg_mask
[1]);
3464 shader_addline(&buffer
, "}\n");
3466 } else if(ps_major
>= 3 && vs_major
>= 3) {
3467 semantics_out
= vs
->semantics_out
;
3469 /* This one is tricky: a 3.0 pixel shader reads from a 3.0 vertex shader */
3470 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
3471 shader_addline(&buffer
, "void order_ps_input(in vec4 OUT[%u]) {\n", MAX_REG_OUTPUT
);
3473 /* First, sort out position and point size. Those are not passed to the pixel shader */
3474 for(i
= 0; i
< MAX_REG_OUTPUT
; i
++) {
3475 if (!vs
->baseShader
.reg_maps
.packed_output
[i
]) continue;
3477 usage
= semantics_out
[i
].usage
;
3478 usage_idx
= semantics_out
[i
].usage_idx
;
3479 shader_glsl_get_write_mask(&semantics_out
[i
].reg
, reg_mask
);
3482 case WINED3DDECLUSAGE_POSITION
:
3483 shader_addline(&buffer
, "gl_Position%s = OUT[%u]%s;\n", reg_mask
, i
, reg_mask
);
3486 case WINED3DDECLUSAGE_PSIZE
:
3487 shader_addline(&buffer
, "gl_PointSize = OUT[%u].x;\n", i
);
3495 /* Then, fix the pixel shader input */
3496 handle_ps3_input(&buffer
, gl_info
, ps
->input_reg_map
,
3497 ps
->semantics_in
, &ps
->baseShader
.reg_maps
, semantics_out
, &vs
->baseShader
.reg_maps
);
3499 shader_addline(&buffer
, "}\n");
3500 } else if(ps_major
>= 3 && vs_major
< 3) {
3501 shader_addline(&buffer
, "varying vec4 IN[%u];\n", GL_LIMITS(glsl_varyings
) / 4);
3502 shader_addline(&buffer
, "void order_ps_input() {\n");
3503 /* The vertex shader wrote to the builtin varyings. There is no need to figure out position and
3504 * point size, but we depend on the optimizers kindness to find out that the pixel shader doesn't
3505 * read gl_TexCoord and gl_ColorX, otherwise we'll run out of varyings
3507 handle_ps3_input(&buffer
, gl_info
, ps
->input_reg_map
, ps
->semantics_in
, &ps
->baseShader
.reg_maps
, NULL
, NULL
);
3508 shader_addline(&buffer
, "}\n");
3510 ERR("Unexpected vertex and pixel shader version condition: vs: %d, ps: %d\n", vs_major
, ps_major
);
3513 ret
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3514 checkGLcall("glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB)");
3515 GL_EXTCALL(glShaderSourceARB(ret
, 1, (const char**)&buffer
.buffer
, NULL
));
3516 checkGLcall("glShaderSourceARB(ret, 1, &buffer.buffer, NULL)");
3517 GL_EXTCALL(glCompileShaderARB(ret
));
3518 checkGLcall("glCompileShaderARB(ret)");
3520 shader_buffer_free(&buffer
);
3524 /* GL locking is done by the caller */
3525 static void hardcode_local_constants(IWineD3DBaseShaderImpl
*shader
, const WineD3D_GL_Info
*gl_info
,
3526 GLhandleARB programId
, char prefix
)
3528 const local_constant
*lconst
;
3533 LIST_FOR_EACH_ENTRY(lconst
, &shader
->baseShader
.constantsF
, local_constant
, entry
) {
3534 value
= (const float *)lconst
->value
;
3535 snprintf(glsl_name
, sizeof(glsl_name
), "%cLC%u", prefix
, lconst
->idx
);
3536 tmp_loc
= GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3537 GL_EXTCALL(glUniform4fvARB(tmp_loc
, 1, value
));
3539 checkGLcall("Hardcoding local constants\n");
3542 /** Sets the GLSL program ID for the given pixel and vertex shader combination.
3543 * It sets the programId on the current StateBlock (because it should be called
3544 * inside of the DrawPrimitive() part of the render loop).
3546 * If a program for the given combination does not exist, create one, and store
3547 * the program in the hash table. If it creates a program, it will link the
3548 * given objects, too.
3551 /* GL locking is done by the caller */
3552 static void set_glsl_shader_program(IWineD3DDevice
*iface
, BOOL use_ps
, BOOL use_vs
) {
3553 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3554 struct shader_glsl_priv
*priv
= This
->shader_priv
;
3555 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3556 IWineD3DPixelShader
*pshader
= This
->stateBlock
->pixelShader
;
3557 IWineD3DVertexShader
*vshader
= This
->stateBlock
->vertexShader
;
3558 struct glsl_shader_prog_link
*entry
= NULL
;
3559 GLhandleARB programId
= 0;
3560 GLhandleARB reorder_shader_id
= 0;
3563 GLhandleARB vshader_id
, pshader_id
;
3564 struct ps_compile_args ps_compile_args
;
3565 struct vs_compile_args vs_compile_args
;
3568 find_vs_compile_args((IWineD3DVertexShaderImpl
*)This
->stateBlock
->vertexShader
, This
->stateBlock
, &vs_compile_args
);
3570 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3571 memset(&vs_compile_args
, 0, sizeof(vs_compile_args
));
3574 find_ps_compile_args((IWineD3DPixelShaderImpl
*)This
->stateBlock
->pixelShader
, This
->stateBlock
, &ps_compile_args
);
3576 /* FIXME: Do we really have to spend CPU cycles to generate a few zeroed bytes? */
3577 memset(&ps_compile_args
, 0, sizeof(ps_compile_args
));
3579 entry
= get_glsl_program_entry(priv
, vshader
, pshader
, &vs_compile_args
, &ps_compile_args
);
3581 priv
->glsl_program
= entry
;
3585 /* If we get to this point, then no matching program exists, so we create one */
3586 programId
= GL_EXTCALL(glCreateProgramObjectARB());
3587 TRACE("Created new GLSL shader program %u\n", programId
);
3589 /* Create the entry */
3590 entry
= HeapAlloc(GetProcessHeap(), 0, sizeof(struct glsl_shader_prog_link
));
3591 entry
->programId
= programId
;
3592 entry
->vshader
= vshader
;
3593 entry
->pshader
= pshader
;
3594 entry
->vs_args
= vs_compile_args
;
3595 entry
->ps_args
= ps_compile_args
;
3596 entry
->constant_version
= 0;
3597 /* Add the hash table entry */
3598 add_glsl_program_entry(priv
, entry
);
3600 /* Set the current program */
3601 priv
->glsl_program
= entry
;
3604 vshader_id
= find_gl_vshader((IWineD3DVertexShaderImpl
*) vshader
, &vs_compile_args
);
3609 /* Attach GLSL vshader */
3611 const unsigned int max_attribs
= 16; /* TODO: Will this always be the case? It is at the moment... */
3614 reorder_shader_id
= generate_param_reorder_function(vshader
, pshader
, gl_info
);
3615 TRACE("Attaching GLSL shader object %u to program %u\n", reorder_shader_id
, programId
);
3616 GL_EXTCALL(glAttachObjectARB(programId
, reorder_shader_id
));
3617 checkGLcall("glAttachObjectARB");
3618 /* Flag the reorder function for deletion, then it will be freed automatically when the program
3621 GL_EXTCALL(glDeleteObjectARB(reorder_shader_id
));
3623 TRACE("Attaching GLSL shader object %u to program %u\n", vshader_id
, programId
);
3624 GL_EXTCALL(glAttachObjectARB(programId
, vshader_id
));
3625 checkGLcall("glAttachObjectARB");
3627 /* Bind vertex attributes to a corresponding index number to match
3628 * the same index numbers as ARB_vertex_programs (makes loading
3629 * vertex attributes simpler). With this method, we can use the
3630 * exact same code to load the attributes later for both ARB and
3633 * We have to do this here because we need to know the Program ID
3634 * in order to make the bindings work, and it has to be done prior
3635 * to linking the GLSL program. */
3636 for (i
= 0; i
< max_attribs
; ++i
) {
3637 if (((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.reg_maps
.attributes
[i
]) {
3638 snprintf(tmp_name
, sizeof(tmp_name
), "attrib%i", i
);
3639 GL_EXTCALL(glBindAttribLocationARB(programId
, i
, tmp_name
));
3642 checkGLcall("glBindAttribLocationARB");
3644 list_add_head(&((IWineD3DBaseShaderImpl
*)vshader
)->baseShader
.linked_programs
, &entry
->vshader_entry
);
3648 pshader_id
= find_gl_pshader((IWineD3DPixelShaderImpl
*) pshader
, &ps_compile_args
);
3653 /* Attach GLSL pshader */
3655 TRACE("Attaching GLSL shader object %u to program %u\n", pshader_id
, programId
);
3656 GL_EXTCALL(glAttachObjectARB(programId
, pshader_id
));
3657 checkGLcall("glAttachObjectARB");
3659 list_add_head(&((IWineD3DBaseShaderImpl
*)pshader
)->baseShader
.linked_programs
, &entry
->pshader_entry
);
3662 /* Link the program */
3663 TRACE("Linking GLSL shader program %u\n", programId
);
3664 GL_EXTCALL(glLinkProgramARB(programId
));
3665 print_glsl_info_log(&GLINFO_LOCATION
, programId
);
3667 entry
->vuniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(vshader_constantsF
));
3668 for (i
= 0; i
< GL_LIMITS(vshader_constantsF
); ++i
) {
3669 snprintf(glsl_name
, sizeof(glsl_name
), "VC[%i]", i
);
3670 entry
->vuniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3672 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3673 snprintf(glsl_name
, sizeof(glsl_name
), "VI[%i]", i
);
3674 entry
->vuniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3676 entry
->puniformF_locations
= HeapAlloc(GetProcessHeap(), 0, sizeof(GLhandleARB
) * GL_LIMITS(pshader_constantsF
));
3677 for (i
= 0; i
< GL_LIMITS(pshader_constantsF
); ++i
) {
3678 snprintf(glsl_name
, sizeof(glsl_name
), "PC[%i]", i
);
3679 entry
->puniformF_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3681 for (i
= 0; i
< MAX_CONST_I
; ++i
) {
3682 snprintf(glsl_name
, sizeof(glsl_name
), "PI[%i]", i
);
3683 entry
->puniformI_locations
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, glsl_name
));
3687 for(i
= 0; i
< ((IWineD3DPixelShaderImpl
*)pshader
)->numbumpenvmatconsts
; i
++) {
3689 sprintf(name
, "bumpenvmat%d", ((IWineD3DPixelShaderImpl
*)pshader
)->bumpenvmatconst
[i
].texunit
);
3690 entry
->bumpenvmat_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3691 sprintf(name
, "luminancescale%d", ((IWineD3DPixelShaderImpl
*)pshader
)->luminanceconst
[i
].texunit
);
3692 entry
->luminancescale_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3693 sprintf(name
, "luminanceoffset%d", ((IWineD3DPixelShaderImpl
*)pshader
)->luminanceconst
[i
].texunit
);
3694 entry
->luminanceoffset_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3698 if (use_ps
&& ps_compile_args
.np2_fixup
) {
3700 for (i
= 0; i
< MAX_FRAGMENT_SAMPLERS
; ++i
) {
3701 if (ps_compile_args
.np2_fixup
& (1 << i
)) {
3702 sprintf(name
, "PsamplerNP2Fixup%u", i
);
3703 entry
->np2Fixup_location
[i
] = GL_EXTCALL(glGetUniformLocationARB(programId
, name
));
3705 entry
->np2Fixup_location
[i
] = -1;
3710 entry
->posFixup_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "posFixup"));
3711 entry
->ycorrection_location
= GL_EXTCALL(glGetUniformLocationARB(programId
, "ycorrection"));
3712 checkGLcall("Find glsl program uniform locations");
3715 && ((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.reg_maps
.shader_version
.major
>= 3
3716 && ((IWineD3DPixelShaderImpl
*)pshader
)->declared_in_count
> GL_LIMITS(glsl_varyings
) / 4)
3718 TRACE("Shader %d needs vertex color clamping disabled\n", programId
);
3719 entry
->vertex_color_clamp
= GL_FALSE
;
3721 entry
->vertex_color_clamp
= GL_FIXED_ONLY_ARB
;
3724 /* Set the shader to allow uniform loading on it */
3725 GL_EXTCALL(glUseProgramObjectARB(programId
));
3726 checkGLcall("glUseProgramObjectARB(programId)");
3728 /* Load the vertex and pixel samplers now. The function that finds the mappings makes sure
3729 * that it stays the same for each vertexshader-pixelshader pair(=linked glsl program). If
3730 * a pshader with fixed function pipeline is used there are no vertex samplers, and if a
3731 * vertex shader with fixed function pixel processing is used we make sure that the card
3732 * supports enough samplers to allow the max number of vertex samplers with all possible
3733 * fixed function fragment processing setups. So once the program is linked these samplers
3737 /* Load vertex shader samplers */
3738 shader_glsl_load_vsamplers(gl_info
, This
->texUnitMap
, programId
);
3741 /* Load pixel shader samplers */
3742 shader_glsl_load_psamplers(gl_info
, This
->texUnitMap
, programId
);
3745 /* If the local constants do not have to be loaded with the environment constants,
3746 * load them now to have them hardcoded in the GLSL program. This saves some CPU cycles
3749 if(pshader
&& !((IWineD3DPixelShaderImpl
*)pshader
)->baseShader
.load_local_constsF
) {
3750 hardcode_local_constants((IWineD3DBaseShaderImpl
*) pshader
, gl_info
, programId
, 'P');
3752 if(vshader
&& !((IWineD3DVertexShaderImpl
*)vshader
)->baseShader
.load_local_constsF
) {
3753 hardcode_local_constants((IWineD3DBaseShaderImpl
*) vshader
, gl_info
, programId
, 'V');
3757 /* GL locking is done by the caller */
3758 static GLhandleARB
create_glsl_blt_shader(const WineD3D_GL_Info
*gl_info
, enum tex_types tex_type
)
3760 GLhandleARB program_id
;
3761 GLhandleARB vshader_id
, pshader_id
;
3762 static const char *blt_vshader
[] =
3767 " gl_Position = gl_Vertex;\n"
3768 " gl_FrontColor = vec4(1.0);\n"
3769 " gl_TexCoord[0] = gl_MultiTexCoord0;\n"
3773 static const char *blt_pshaders
[tex_type_count
] =
3779 "uniform sampler2D sampler;\n"
3782 " gl_FragDepth = texture2D(sampler, gl_TexCoord[0].xy).x;\n"
3788 "uniform samplerCube sampler;\n"
3791 " gl_FragDepth = textureCube(sampler, gl_TexCoord[0].xyz).x;\n"
3795 "#extension GL_ARB_texture_rectangle : enable\n"
3796 "uniform sampler2DRect sampler;\n"
3799 " gl_FragDepth = texture2DRect(sampler, gl_TexCoord[0].xy).x;\n"
3803 if (!blt_pshaders
[tex_type
])
3805 FIXME("tex_type %#x not supported\n", tex_type
);
3809 vshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
3810 GL_EXTCALL(glShaderSourceARB(vshader_id
, 1, blt_vshader
, NULL
));
3811 GL_EXTCALL(glCompileShaderARB(vshader_id
));
3813 pshader_id
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
3814 GL_EXTCALL(glShaderSourceARB(pshader_id
, 1, &blt_pshaders
[tex_type
], NULL
));
3815 GL_EXTCALL(glCompileShaderARB(pshader_id
));
3817 program_id
= GL_EXTCALL(glCreateProgramObjectARB());
3818 GL_EXTCALL(glAttachObjectARB(program_id
, vshader_id
));
3819 GL_EXTCALL(glAttachObjectARB(program_id
, pshader_id
));
3820 GL_EXTCALL(glLinkProgramARB(program_id
));
3822 print_glsl_info_log(&GLINFO_LOCATION
, program_id
);
3824 /* Once linked we can mark the shaders for deletion. They will be deleted once the program
3827 GL_EXTCALL(glDeleteObjectARB(vshader_id
));
3828 GL_EXTCALL(glDeleteObjectARB(pshader_id
));
3832 /* GL locking is done by the caller */
3833 static void shader_glsl_select(IWineD3DDevice
*iface
, BOOL usePS
, BOOL useVS
) {
3834 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3835 struct shader_glsl_priv
*priv
= This
->shader_priv
;
3836 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3837 GLhandleARB program_id
= 0;
3838 GLenum old_vertex_color_clamp
, current_vertex_color_clamp
;
3840 old_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
3842 if (useVS
|| usePS
) set_glsl_shader_program(iface
, usePS
, useVS
);
3843 else priv
->glsl_program
= NULL
;
3845 current_vertex_color_clamp
= priv
->glsl_program
? priv
->glsl_program
->vertex_color_clamp
: GL_FIXED_ONLY_ARB
;
3847 if (old_vertex_color_clamp
!= current_vertex_color_clamp
) {
3848 if (GL_SUPPORT(ARB_COLOR_BUFFER_FLOAT
)) {
3849 GL_EXTCALL(glClampColorARB(GL_CLAMP_VERTEX_COLOR_ARB
, current_vertex_color_clamp
));
3850 checkGLcall("glClampColorARB");
3852 FIXME("vertex color clamp needs to be changed, but extension not supported.\n");
3856 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
3857 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3858 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3859 checkGLcall("glUseProgramObjectARB");
3862 /* GL locking is done by the caller */
3863 static void shader_glsl_select_depth_blt(IWineD3DDevice
*iface
, enum tex_types tex_type
) {
3864 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3865 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3866 struct shader_glsl_priv
*priv
= This
->shader_priv
;
3867 GLhandleARB
*blt_program
= &priv
->depth_blt_program
[tex_type
];
3869 if (!*blt_program
) {
3871 *blt_program
= create_glsl_blt_shader(gl_info
, tex_type
);
3872 loc
= GL_EXTCALL(glGetUniformLocationARB(*blt_program
, "sampler"));
3873 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
3874 GL_EXTCALL(glUniform1iARB(loc
, 0));
3876 GL_EXTCALL(glUseProgramObjectARB(*blt_program
));
3880 /* GL locking is done by the caller */
3881 static void shader_glsl_deselect_depth_blt(IWineD3DDevice
*iface
) {
3882 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
3883 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
3884 struct shader_glsl_priv
*priv
= This
->shader_priv
;
3885 GLhandleARB program_id
;
3887 program_id
= priv
->glsl_program
? priv
->glsl_program
->programId
: 0;
3888 if (program_id
) TRACE("Using GLSL program %u\n", program_id
);
3890 GL_EXTCALL(glUseProgramObjectARB(program_id
));
3891 checkGLcall("glUseProgramObjectARB");
3894 static void shader_glsl_destroy(IWineD3DBaseShader
*iface
) {
3895 const struct list
*linked_programs
;
3896 IWineD3DBaseShaderImpl
*This
= (IWineD3DBaseShaderImpl
*) iface
;
3897 IWineD3DDeviceImpl
*device
= (IWineD3DDeviceImpl
*)This
->baseShader
.device
;
3898 struct shader_glsl_priv
*priv
= device
->shader_priv
;
3899 const WineD3D_GL_Info
*gl_info
= &device
->adapter
->gl_info
;
3900 IWineD3DPixelShaderImpl
*ps
= NULL
;
3901 IWineD3DVertexShaderImpl
*vs
= NULL
;
3903 /* Note: Do not use QueryInterface here to find out which shader type this is because this code
3904 * can be called from IWineD3DBaseShader::Release
3906 char pshader
= shader_is_pshader_version(This
->baseShader
.reg_maps
.shader_version
.type
);
3909 ps
= (IWineD3DPixelShaderImpl
*) This
;
3910 if(ps
->num_gl_shaders
== 0) return;
3911 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->pshader
== iface
)
3914 shader_glsl_select(This
->baseShader
.device
, FALSE
, FALSE
);
3918 vs
= (IWineD3DVertexShaderImpl
*) This
;
3919 if(vs
->num_gl_shaders
== 0) return;
3920 if (priv
->glsl_program
&& (IWineD3DBaseShader
*)priv
->glsl_program
->vshader
== iface
)
3923 shader_glsl_select(This
->baseShader
.device
, FALSE
, FALSE
);
3928 linked_programs
= &This
->baseShader
.linked_programs
;
3930 TRACE("Deleting linked programs\n");
3931 if (linked_programs
->next
) {
3932 struct glsl_shader_prog_link
*entry
, *entry2
;
3936 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, pshader_entry
) {
3937 delete_glsl_program_entry(priv
, gl_info
, entry
);
3940 LIST_FOR_EACH_ENTRY_SAFE(entry
, entry2
, linked_programs
, struct glsl_shader_prog_link
, vshader_entry
) {
3941 delete_glsl_program_entry(priv
, gl_info
, entry
);
3951 for(i
= 0; i
< ps
->num_gl_shaders
; i
++) {
3952 TRACE("deleting pshader %u\n", ps
->gl_shaders
[i
].prgId
);
3953 GL_EXTCALL(glDeleteObjectARB(ps
->gl_shaders
[i
].prgId
));
3954 checkGLcall("glDeleteObjectARB");
3957 HeapFree(GetProcessHeap(), 0, ps
->gl_shaders
);
3958 ps
->gl_shaders
= NULL
;
3959 ps
->num_gl_shaders
= 0;
3960 ps
->shader_array_size
= 0;
3965 for(i
= 0; i
< vs
->num_gl_shaders
; i
++) {
3966 TRACE("deleting vshader %u\n", vs
->gl_shaders
[i
].prgId
);
3967 GL_EXTCALL(glDeleteObjectARB(vs
->gl_shaders
[i
].prgId
));
3968 checkGLcall("glDeleteObjectARB");
3971 HeapFree(GetProcessHeap(), 0, vs
->gl_shaders
);
3972 vs
->gl_shaders
= NULL
;
3973 vs
->num_gl_shaders
= 0;
3974 vs
->shader_array_size
= 0;
3978 static unsigned int glsl_program_key_hash(const void *key
)
3980 const glsl_program_key_t
*k
= key
;
3982 unsigned int hash
= ((DWORD_PTR
) k
->vshader
) | ((DWORD_PTR
) k
->pshader
) << 16;
3983 hash
+= ~(hash
<< 15);
3984 hash
^= (hash
>> 10);
3985 hash
+= (hash
<< 3);
3986 hash
^= (hash
>> 6);
3987 hash
+= ~(hash
<< 11);
3988 hash
^= (hash
>> 16);
3993 static BOOL
glsl_program_key_compare(const void *keya
, const void *keyb
)
3995 const glsl_program_key_t
*ka
= keya
;
3996 const glsl_program_key_t
*kb
= keyb
;
3998 return ka
->vshader
== kb
->vshader
&& ka
->pshader
== kb
->pshader
&&
3999 (memcmp(&ka
->ps_args
, &kb
->ps_args
, sizeof(kb
->ps_args
)) == 0) &&
4000 (memcmp(&ka
->vs_args
, &kb
->vs_args
, sizeof(kb
->vs_args
)) == 0);
4003 static BOOL
constant_heap_init(struct constant_heap
*heap
, unsigned int constant_count
)
4005 SIZE_T size
= (constant_count
+ 1) * sizeof(*heap
->entries
) + constant_count
* sizeof(*heap
->positions
);
4006 void *mem
= HeapAlloc(GetProcessHeap(), 0, size
);
4010 ERR("Failed to allocate memory\n");
4014 heap
->entries
= mem
;
4015 heap
->entries
[1].version
= 0;
4016 heap
->positions
= (unsigned int *)(heap
->entries
+ constant_count
+ 1);
4022 static void constant_heap_free(struct constant_heap
*heap
)
4024 HeapFree(GetProcessHeap(), 0, heap
->entries
);
4027 static HRESULT
shader_glsl_alloc(IWineD3DDevice
*iface
) {
4028 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4029 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4030 struct shader_glsl_priv
*priv
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(struct shader_glsl_priv
));
4031 SIZE_T stack_size
= wined3d_log2i(max(GL_LIMITS(vshader_constantsF
), GL_LIMITS(pshader_constantsF
))) + 1;
4033 priv
->stack
= HeapAlloc(GetProcessHeap(), 0, stack_size
* sizeof(*priv
->stack
));
4036 ERR("Failed to allocate memory.\n");
4037 HeapFree(GetProcessHeap(), 0, priv
);
4038 return E_OUTOFMEMORY
;
4041 if (!constant_heap_init(&priv
->vconst_heap
, GL_LIMITS(vshader_constantsF
)))
4043 ERR("Failed to initialize vertex shader constant heap\n");
4044 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4045 HeapFree(GetProcessHeap(), 0, priv
);
4046 return E_OUTOFMEMORY
;
4049 if (!constant_heap_init(&priv
->pconst_heap
, GL_LIMITS(pshader_constantsF
)))
4051 ERR("Failed to initialize pixel shader constant heap\n");
4052 constant_heap_free(&priv
->vconst_heap
);
4053 HeapFree(GetProcessHeap(), 0, priv
->stack
);
4054 HeapFree(GetProcessHeap(), 0, priv
);
4055 return E_OUTOFMEMORY
;
4058 priv
->glsl_program_lookup
= hash_table_create(glsl_program_key_hash
, glsl_program_key_compare
);
4059 priv
->next_constant_version
= 1;
4061 This
->shader_priv
= priv
;
4065 static void shader_glsl_free(IWineD3DDevice
*iface
) {
4066 IWineD3DDeviceImpl
*This
= (IWineD3DDeviceImpl
*)iface
;
4067 const WineD3D_GL_Info
*gl_info
= &This
->adapter
->gl_info
;
4068 struct shader_glsl_priv
*priv
= This
->shader_priv
;
4072 for (i
= 0; i
< tex_type_count
; ++i
)
4074 if (priv
->depth_blt_program
[i
])
4076 GL_EXTCALL(glDeleteObjectARB(priv
->depth_blt_program
[i
]));
4081 hash_table_destroy(priv
->glsl_program_lookup
, NULL
, NULL
);
4082 constant_heap_free(&priv
->pconst_heap
);
4083 constant_heap_free(&priv
->vconst_heap
);
4085 HeapFree(GetProcessHeap(), 0, This
->shader_priv
);
4086 This
->shader_priv
= NULL
;
4089 static BOOL
shader_glsl_dirty_const(IWineD3DDevice
*iface
) {
4090 /* TODO: GL_EXT_bindable_uniform can be used to share constants across shaders */
4094 /* GL locking is done by the caller */
4095 static GLuint
shader_glsl_generate_pshader(IWineD3DPixelShader
*iface
,
4096 SHADER_BUFFER
*buffer
, const struct ps_compile_args
*args
)
4098 IWineD3DPixelShaderImpl
*This
= (IWineD3DPixelShaderImpl
*)iface
;
4099 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
4100 CONST DWORD
*function
= This
->baseShader
.function
;
4101 const char *fragcolor
;
4102 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
4104 /* Create the hw GLSL shader object and assign it as the shader->prgId */
4105 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB
));
4107 shader_addline(buffer
, "#version 120\n");
4109 if (GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
4110 shader_addline(buffer
, "#extension GL_ARB_draw_buffers : enable\n");
4112 if(GL_SUPPORT(ARB_SHADER_TEXTURE_LOD
) && reg_maps
->usestexldd
) {
4113 shader_addline(buffer
, "#extension GL_ARB_shader_texture_lod : enable\n");
4115 if (GL_SUPPORT(ARB_TEXTURE_RECTANGLE
)) {
4116 /* The spec says that it doesn't have to be explicitly enabled, but the nvidia
4117 * drivers write a warning if we don't do so
4119 shader_addline(buffer
, "#extension GL_ARB_texture_rectangle : enable\n");
4122 /* Base Declarations */
4123 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
, args
);
4125 /* Pack 3.0 inputs */
4126 if (reg_maps
->shader_version
.major
>= 3 && args
->vp_mode
!= vertexshader
)
4128 pshader_glsl_input_pack(iface
, buffer
, This
->semantics_in
, reg_maps
, args
->vp_mode
);
4131 /* Base Shader Body */
4132 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
);
4134 /* Pixel shaders < 2.0 place the resulting color in R0 implicitly */
4135 if (reg_maps
->shader_version
.major
< 2)
4137 /* Some older cards like GeforceFX ones don't support multiple buffers, so also not gl_FragData */
4138 if(GL_SUPPORT(ARB_DRAW_BUFFERS
))
4139 shader_addline(buffer
, "gl_FragData[0] = R0;\n");
4141 shader_addline(buffer
, "gl_FragColor = R0;\n");
4144 if(GL_SUPPORT(ARB_DRAW_BUFFERS
)) {
4145 fragcolor
= "gl_FragData[0]";
4147 fragcolor
= "gl_FragColor";
4149 if(args
->srgb_correction
) {
4150 shader_addline(buffer
, "tmp0.xyz = pow(%s.xyz, vec3(%f, %f, %f)) * vec3(%f, %f, %f) - vec3(%f, %f, %f);\n",
4151 fragcolor
, srgb_pow
, srgb_pow
, srgb_pow
, srgb_mul_high
, srgb_mul_high
, srgb_mul_high
,
4152 srgb_sub_high
, srgb_sub_high
, srgb_sub_high
);
4153 shader_addline(buffer
, "tmp1.xyz = %s.xyz * srgb_mul_low.xyz;\n", fragcolor
);
4154 shader_addline(buffer
, "%s.x = %s.x < srgb_comparison.x ? tmp1.x : tmp0.x;\n", fragcolor
, fragcolor
);
4155 shader_addline(buffer
, "%s.y = %s.y < srgb_comparison.y ? tmp1.y : tmp0.y;\n", fragcolor
, fragcolor
);
4156 shader_addline(buffer
, "%s.z = %s.z < srgb_comparison.z ? tmp1.z : tmp0.z;\n", fragcolor
, fragcolor
);
4157 shader_addline(buffer
, "%s = clamp(%s, 0.0, 1.0);\n", fragcolor
, fragcolor
);
4159 /* Pixel shader < 3.0 do not replace the fog stage.
4160 * This implements linear fog computation and blending.
4161 * TODO: non linear fog
4162 * NOTE: gl_Fog.start and gl_Fog.end don't hold fog start s and end e but
4163 * -1/(e-s) and e/(e-s) respectively.
4165 if (reg_maps
->shader_version
.major
< 3)
4168 case FOG_OFF
: break;
4170 shader_addline(buffer
, "float fogstart = -1.0 / (gl_Fog.end - gl_Fog.start);\n");
4171 shader_addline(buffer
, "float fogend = gl_Fog.end * -fogstart;\n");
4172 shader_addline(buffer
, "float Fog = clamp(gl_FogFragCoord * fogstart + fogend, 0.0, 1.0);\n");
4173 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
4176 /* Fog = e^(-gl_Fog.density * gl_FogFragCoord) */
4177 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_FogFragCoord);\n");
4178 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4179 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
4182 /* Fog = e^(-(gl_Fog.density * gl_FogFragCoord)^2) */
4183 shader_addline(buffer
, "float Fog = exp(-gl_Fog.density * gl_Fog.density * gl_FogFragCoord * gl_FogFragCoord);\n");
4184 shader_addline(buffer
, "Fog = clamp(Fog, 0.0, 1.0);\n");
4185 shader_addline(buffer
, "%s.xyz = mix(gl_Fog.color.xyz, %s.xyz, Fog);\n", fragcolor
, fragcolor
);
4190 shader_addline(buffer
, "}\n");
4192 TRACE("Compiling shader object %u\n", shader_obj
);
4193 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
4194 GL_EXTCALL(glCompileShaderARB(shader_obj
));
4195 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
4197 /* Store the shader object */
4201 /* GL locking is done by the caller */
4202 static GLuint
shader_glsl_generate_vshader(IWineD3DVertexShader
*iface
,
4203 SHADER_BUFFER
*buffer
, const struct vs_compile_args
*args
)
4205 IWineD3DVertexShaderImpl
*This
= (IWineD3DVertexShaderImpl
*)iface
;
4206 const struct shader_reg_maps
*reg_maps
= &This
->baseShader
.reg_maps
;
4207 CONST DWORD
*function
= This
->baseShader
.function
;
4208 const WineD3D_GL_Info
*gl_info
= &((IWineD3DDeviceImpl
*)This
->baseShader
.device
)->adapter
->gl_info
;
4210 /* Create the hw GLSL shader program and assign it as the shader->prgId */
4211 GLhandleARB shader_obj
= GL_EXTCALL(glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB
));
4213 shader_addline(buffer
, "#version 120\n");
4215 /* Base Declarations */
4216 shader_generate_glsl_declarations( (IWineD3DBaseShader
*) This
, reg_maps
, buffer
, &GLINFO_LOCATION
, NULL
);
4218 /* Base Shader Body */
4219 shader_generate_main((IWineD3DBaseShader
*)This
, buffer
, reg_maps
, function
);
4221 /* Unpack 3.0 outputs */
4222 if (reg_maps
->shader_version
.major
>= 3) shader_addline(buffer
, "order_ps_input(OUT);\n");
4223 else shader_addline(buffer
, "order_ps_input();\n");
4225 /* The D3DRS_FOGTABLEMODE render state defines if the shader-generated fog coord is used
4226 * or if the fragment depth is used. If the fragment depth is used(FOGTABLEMODE != NONE),
4227 * the fog frag coord is thrown away. If the fog frag coord is used, but not written by
4228 * the shader, it is set to 0.0(fully fogged, since start = 1.0, end = 0.0)
4230 if(args
->fog_src
== VS_FOG_Z
) {
4231 shader_addline(buffer
, "gl_FogFragCoord = gl_Position.z;\n");
4232 } else if (!reg_maps
->fog
) {
4233 shader_addline(buffer
, "gl_FogFragCoord = 0.0;\n");
4236 /* Write the final position.
4238 * OpenGL coordinates specify the center of the pixel while d3d coords specify
4239 * the corner. The offsets are stored in z and w in posFixup. posFixup.y contains
4240 * 1.0 or -1.0 to turn the rendering upside down for offscreen rendering. PosFixup.x
4241 * contains 1.0 to allow a mad.
4243 shader_addline(buffer
, "gl_Position.y = gl_Position.y * posFixup.y;\n");
4244 shader_addline(buffer
, "gl_Position.xy += posFixup.zw * gl_Position.ww;\n");
4246 /* Z coord [0;1]->[-1;1] mapping, see comment in transform_projection in state.c
4248 * Basically we want (in homogeneous coordinates) z = z * 2 - 1. However, shaders are run
4249 * before the homogeneous divide, so we have to take the w into account: z = ((z / w) * 2 - 1) * w,
4250 * which is the same as z = z * 2 - w.
4252 shader_addline(buffer
, "gl_Position.z = gl_Position.z * 2.0 - gl_Position.w;\n");
4254 shader_addline(buffer
, "}\n");
4256 TRACE("Compiling shader object %u\n", shader_obj
);
4257 GL_EXTCALL(glShaderSourceARB(shader_obj
, 1, (const char**)&buffer
->buffer
, NULL
));
4258 GL_EXTCALL(glCompileShaderARB(shader_obj
));
4259 print_glsl_info_log(&GLINFO_LOCATION
, shader_obj
);
4264 static void shader_glsl_get_caps(WINED3DDEVTYPE devtype
, const WineD3D_GL_Info
*gl_info
, struct shader_caps
*pCaps
)
4266 /* Nvidia Geforce6/7 or Ati R4xx/R5xx cards with GLSL support, support VS 3.0 but older Nvidia/Ati
4267 * models with GLSL support only support 2.0. In case of nvidia we can detect VS 2.0 support using
4268 * vs_nv_version which is based on NV_vertex_program.
4269 * For Ati cards there's no way using glsl (it abstracts the lowlevel info away) and also not
4270 * using ARB_vertex_program. It is safe to assume that when a card supports pixel shader 2.0 it
4271 * supports vertex shader 2.0 too and the way around. We can detect ps2.0 using the maximum number
4272 * of native instructions, so use that here. For more info see the pixel shader versioning code below.
4274 if((GLINFO_LOCATION
.vs_nv_version
== VS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
4275 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(2,0);
4277 pCaps
->VertexShaderVersion
= WINED3DVS_VERSION(3,0);
4278 TRACE_(d3d_caps
)("Hardware vertex shader version %d.%d enabled (GLSL)\n", (pCaps
->VertexShaderVersion
>> 8) & 0xff, pCaps
->VertexShaderVersion
& 0xff);
4279 pCaps
->MaxVertexShaderConst
= GL_LIMITS(vshader_constantsF
);
4281 /* Older DX9-class videocards (GeforceFX / Radeon >9500/X*00) only support pixel shader 2.0/2.0a/2.0b.
4282 * In OpenGL the extensions related to GLSL abstract lowlevel GL info away which is needed
4283 * to distinguish between 2.0 and 3.0 (and 2.0a/2.0b). In case of Nvidia we use their fragment
4284 * program extensions. On other hardware including ATI GL_ARB_fragment_program offers the info
4285 * in max native instructions. Intel and others also offer the info in this extension but they
4286 * don't support GLSL (at least on Windows).
4288 * PS2.0 requires at least 96 instructions, 2.0a/2.0b go up to 512. Assume that if the number
4289 * of instructions is 512 or less we have to do with ps2.0 hardware.
4290 * 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.
4292 if((GLINFO_LOCATION
.ps_nv_version
== PS_VERSION_20
) || (GLINFO_LOCATION
.ps_arb_max_instructions
<= 512))
4293 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(2,0);
4295 pCaps
->PixelShaderVersion
= WINED3DPS_VERSION(3,0);
4297 pCaps
->MaxPixelShaderConst
= GL_LIMITS(pshader_constantsF
);
4299 /* FIXME: The following line is card dependent. -8.0 to 8.0 is the
4300 * Direct3D minimum requirement.
4302 * Both GL_ARB_fragment_program and GLSL require a "maximum representable magnitude"
4303 * of colors to be 2^10, and 2^32 for other floats. Should we use 1024 here?
4305 * The problem is that the refrast clamps temporary results in the shader to
4306 * [-MaxValue;+MaxValue]. If the card's max value is bigger than the one we advertize here,
4307 * then applications may miss the clamping behavior. On the other hand, if it is smaller,
4308 * the shader will generate incorrect results too. Unfortunately, GL deliberately doesn't
4309 * offer a way to query this.
4311 pCaps
->PixelShader1xMaxValue
= 8.0;
4312 TRACE_(d3d_caps
)("Hardware pixel shader version %d.%d enabled (GLSL)\n", (pCaps
->PixelShaderVersion
>> 8) & 0xff, pCaps
->PixelShaderVersion
& 0xff);
4315 static BOOL
shader_glsl_color_fixup_supported(struct color_fixup_desc fixup
)
4317 if (TRACE_ON(d3d_shader
) && TRACE_ON(d3d
))
4319 TRACE("Checking support for fixup:\n");
4320 dump_color_fixup_desc(fixup
);
4323 /* We support everything except YUV conversions. */
4324 if (!is_yuv_fixup(fixup
))
4330 TRACE("[FAILED]\n");
4334 static const SHADER_HANDLER shader_glsl_instruction_handler_table
[WINED3DSIH_TABLE_SIZE
] =
4336 /* WINED3DSIH_ABS */ shader_glsl_map2gl
,
4337 /* WINED3DSIH_ADD */ shader_glsl_arith
,
4338 /* WINED3DSIH_BEM */ pshader_glsl_bem
,
4339 /* WINED3DSIH_BREAK */ shader_glsl_break
,
4340 /* WINED3DSIH_BREAKC */ shader_glsl_breakc
,
4341 /* WINED3DSIH_BREAKP */ NULL
,
4342 /* WINED3DSIH_CALL */ shader_glsl_call
,
4343 /* WINED3DSIH_CALLNZ */ shader_glsl_callnz
,
4344 /* WINED3DSIH_CMP */ shader_glsl_cmp
,
4345 /* WINED3DSIH_CND */ shader_glsl_cnd
,
4346 /* WINED3DSIH_CRS */ shader_glsl_cross
,
4347 /* WINED3DSIH_DCL */ NULL
,
4348 /* WINED3DSIH_DEF */ NULL
,
4349 /* WINED3DSIH_DEFB */ NULL
,
4350 /* WINED3DSIH_DEFI */ NULL
,
4351 /* WINED3DSIH_DP2ADD */ pshader_glsl_dp2add
,
4352 /* WINED3DSIH_DP3 */ shader_glsl_dot
,
4353 /* WINED3DSIH_DP4 */ shader_glsl_dot
,
4354 /* WINED3DSIH_DST */ shader_glsl_dst
,
4355 /* WINED3DSIH_DSX */ shader_glsl_map2gl
,
4356 /* WINED3DSIH_DSY */ shader_glsl_map2gl
,
4357 /* WINED3DSIH_ELSE */ shader_glsl_else
,
4358 /* WINED3DSIH_ENDIF */ shader_glsl_end
,
4359 /* WINED3DSIH_ENDLOOP */ shader_glsl_end
,
4360 /* WINED3DSIH_ENDREP */ shader_glsl_end
,
4361 /* WINED3DSIH_EXP */ shader_glsl_map2gl
,
4362 /* WINED3DSIH_EXPP */ shader_glsl_expp
,
4363 /* WINED3DSIH_FRC */ shader_glsl_map2gl
,
4364 /* WINED3DSIH_IF */ shader_glsl_if
,
4365 /* WINED3DSIH_IFC */ shader_glsl_ifc
,
4366 /* WINED3DSIH_LABEL */ shader_glsl_label
,
4367 /* WINED3DSIH_LIT */ shader_glsl_lit
,
4368 /* WINED3DSIH_LOG */ shader_glsl_log
,
4369 /* WINED3DSIH_LOGP */ shader_glsl_log
,
4370 /* WINED3DSIH_LOOP */ shader_glsl_loop
,
4371 /* WINED3DSIH_LRP */ shader_glsl_lrp
,
4372 /* WINED3DSIH_M3x2 */ shader_glsl_mnxn
,
4373 /* WINED3DSIH_M3x3 */ shader_glsl_mnxn
,
4374 /* WINED3DSIH_M3x4 */ shader_glsl_mnxn
,
4375 /* WINED3DSIH_M4x3 */ shader_glsl_mnxn
,
4376 /* WINED3DSIH_M4x4 */ shader_glsl_mnxn
,
4377 /* WINED3DSIH_MAD */ shader_glsl_mad
,
4378 /* WINED3DSIH_MAX */ shader_glsl_map2gl
,
4379 /* WINED3DSIH_MIN */ shader_glsl_map2gl
,
4380 /* WINED3DSIH_MOV */ shader_glsl_mov
,
4381 /* WINED3DSIH_MOVA */ shader_glsl_mov
,
4382 /* WINED3DSIH_MUL */ shader_glsl_arith
,
4383 /* WINED3DSIH_NOP */ NULL
,
4384 /* WINED3DSIH_NRM */ shader_glsl_map2gl
,
4385 /* WINED3DSIH_PHASE */ NULL
,
4386 /* WINED3DSIH_POW */ shader_glsl_pow
,
4387 /* WINED3DSIH_RCP */ shader_glsl_rcp
,
4388 /* WINED3DSIH_REP */ shader_glsl_rep
,
4389 /* WINED3DSIH_RET */ NULL
,
4390 /* WINED3DSIH_RSQ */ shader_glsl_rsq
,
4391 /* WINED3DSIH_SETP */ NULL
,
4392 /* WINED3DSIH_SGE */ shader_glsl_compare
,
4393 /* WINED3DSIH_SGN */ shader_glsl_map2gl
,
4394 /* WINED3DSIH_SINCOS */ shader_glsl_sincos
,
4395 /* WINED3DSIH_SLT */ shader_glsl_compare
,
4396 /* WINED3DSIH_SUB */ shader_glsl_arith
,
4397 /* WINED3DSIH_TEX */ pshader_glsl_tex
,
4398 /* WINED3DSIH_TEXBEM */ pshader_glsl_texbem
,
4399 /* WINED3DSIH_TEXBEML */ pshader_glsl_texbem
,
4400 /* WINED3DSIH_TEXCOORD */ pshader_glsl_texcoord
,
4401 /* WINED3DSIH_TEXDEPTH */ pshader_glsl_texdepth
,
4402 /* WINED3DSIH_TEXDP3 */ pshader_glsl_texdp3
,
4403 /* WINED3DSIH_TEXDP3TEX */ pshader_glsl_texdp3tex
,
4404 /* WINED3DSIH_TEXKILL */ pshader_glsl_texkill
,
4405 /* WINED3DSIH_TEXLDD */ shader_glsl_texldd
,
4406 /* WINED3DSIH_TEXLDL */ shader_glsl_texldl
,
4407 /* WINED3DSIH_TEXM3x2DEPTH */ pshader_glsl_texm3x2depth
,
4408 /* WINED3DSIH_TEXM3x2PAD */ pshader_glsl_texm3x2pad
,
4409 /* WINED3DSIH_TEXM3x2TEX */ pshader_glsl_texm3x2tex
,
4410 /* WINED3DSIH_TEXM3x3 */ pshader_glsl_texm3x3
,
4411 /* WINED3DSIH_TEXM3x3DIFF */ NULL
,
4412 /* WINED3DSIH_TEXM3x3PAD */ pshader_glsl_texm3x3pad
,
4413 /* WINED3DSIH_TEXM3x3SPEC */ pshader_glsl_texm3x3spec
,
4414 /* WINED3DSIH_TEXM3x3TEX */ pshader_glsl_texm3x3tex
,
4415 /* WINED3DSIH_TEXM3x3VSPEC */ pshader_glsl_texm3x3vspec
,
4416 /* WINED3DSIH_TEXREG2AR */ pshader_glsl_texreg2ar
,
4417 /* WINED3DSIH_TEXREG2GB */ pshader_glsl_texreg2gb
,
4418 /* WINED3DSIH_TEXREG2RGB */ pshader_glsl_texreg2rgb
,
4421 const shader_backend_t glsl_shader_backend
= {
4422 shader_glsl_instruction_handler_table
,
4424 shader_glsl_select_depth_blt
,
4425 shader_glsl_deselect_depth_blt
,
4426 shader_glsl_update_float_vertex_constants
,
4427 shader_glsl_update_float_pixel_constants
,
4428 shader_glsl_load_constants
,
4429 shader_glsl_load_np2fixup_constants
,
4430 shader_glsl_destroy
,
4433 shader_glsl_dirty_const
,
4434 shader_glsl_generate_pshader
,
4435 shader_glsl_generate_vshader
,
4436 shader_glsl_get_caps
,
4437 shader_glsl_color_fixup_supported
,
4438 shader_glsl_add_instruction_modifiers
,