Alpha-masked materials for shadow mapping -> slow!

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 )
			discard;
	}
}

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?

what gpu/driver?

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 )
    discard;
}

Set u_Texture2Threshold to 0.5 if you want to discard fragments and to 0.0 if you don’t want to.

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 :slight_smile:

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.

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?

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.