View Full Version : alpha-masked materials for shadow mapping -> slow!

05-02-2007, 01:21 AM
i want to support alpha-masked materials for my shadow maps, so i discard all fragments with an alpha value less than 0.5 in the fragment shader:

varying vec2 v_Coordinates;

uniform bool u_UseTexture2;
uniform sampler2D u_Texture2;

void main()
// handle transparency
if( u_UseTexture2 )
if( texture2D( u_Texture2, v_Coordinates ).r < 0.5 )
}this works fine, but is horribly slow! fps drop from 100 to 25 (compared to the same scene without discarding framents). i do the same in my lighting shader and it doesn't have any performance impact at all. any idea what's wrong?

EDIT: if i use GL_ALPHA_TEST instead, there is no performance hit - why?

05-02-2007, 03:59 AM
what gpu/driver?

05-02-2007, 04:12 AM
If could be if( u_UseTexture2 ) because it uses uniform as condition. Shader may get re-optimized every time you switch that uniform.
Perhaps you should try:

varying vec2 v_Coordinates;
uniform float u_Texture2Threshold;
uniform sampler2D u_Texture2;
void main()
if( texture2D( u_Texture2, v_Coordinates ).r < u_Texture2Threshold )
}Set u_Texture2Threshold to 0.5 if you want to discard fragments and to 0.0 if you don't want to.

05-02-2007, 06:58 AM
k_szczech, can be this uniform - depending optimization switched off? It can be sometimes more dangerous than usefull.
Of course if is this is the case :)

05-02-2007, 10:17 AM
EDIT: if i use GL_ALPHA_TEST instead, there is no performance hit - why?Because shaders cost more than an alpha test.

Calling "discard" doesn't mean that the fragment stops processing. In most, if not all, hardware, "discard" is just a flag that tells the post-fragment processor to ignore whatever this fragment comes out to be. So any fragment processing that happens afterwards will still continue.

05-02-2007, 09:36 PM
Yes, but alpha test is done after the fragment shader, so that's not any better. The only reason I could see for this behavior is if the shader compiled to something weird. What hardware is this on btw?