Jump to content
Search In
  • More options...
Find results that contain...
Find results in...
Kamjam66xx

Shaders - Calculating Shadows

Recommended Posts

Posted · Original PosterOP

i'm trying to optimize my shaders while im adding to them. Is there a way to avoid "if" statements in calculating shadow maps? I know "if" statements could be taxing in the past, and dont have a clear answer to if they should be avoided now days. I just know they were a big problem for older cards. 

 

For example: my code below..

vec4 CalcLightByDirection(Light light, vec3 direction, float shadowFactor)
{
	vec4 ambientColour = vec4(light.colour, 1.0f) * light.ambientIntensity;
	
	float diffuseFactor = max(dot(normalize(Normal), normalize(direction)), 0.0f);
	vec4 diffuseColour = vec4(light.colour * light.diffuseIntensity * diffuseFactor, 1.0f);
	
	vec4 specularColour = vec4(0, 0, 0, 0);
	
	if(diffuseFactor > 0.0f) 			// <--    <--    <--    <--    <--  //
	{
		vec3 fragToEye = normalize(eyePosition - FragPos);
		vec3 reflectedVertex = normalize(reflect(direction, normalize(Normal)));
		
		float specularFactor = dot(fragToEye, reflectedVertex);
		if(specularFactor > 0.0f)
		{
			specularFactor = pow(specularFactor, material.specularPower);
			specularColour = vec4(light.colour * material.specularIntensity * specularFactor, 1.0f);
		}
	}

	return (ambientColour + (1.0 - shadowFactor) * (diffuseColour + specularColour));
}

 

Link to post
Share on other sites

I don't understand, that if is necessary that's the dot between the normal and direction. if it's 0 it mean your angle is 90 DEG or more which mean you don't show the specular as it's not toward you view. Unless i misread your variables.

Link to post
Share on other sites
14 hours ago, Kamjam66xx said:

i'm trying to optimize my shaders while im adding to them. Is there a way to avoid "if" statements in calculating shadow maps? I know "if" statements could be taxing in the past, and dont have a clear answer to if they should be avoided now days. I just know they were a big problem for older cards.  

Well, that depends. There are three types of branching, atleast in terms of what the compiler can see:
 

  1. Static branching: Everything is known at compile time and is optimized away. There is no actual branching occuring when your shader runs.
  2. Statically Uniform Branching: The compiler can't easily know for sure what branch is taken, but it does notice that it is based only off of constants/uniforms. Therefore, the compiler can know that the decision taken will be the same for every instance of the shader, and therefore each instance of the shader will be in the same place at the same time. Coherency is maintained (really, really good for GPUs.).
  3. Dynamic branching: Nothing can be determined at compile time. The shader may or may not be in different places for different instances.

The one that you should probably still care about is dynamic branching, however, newer hardware doesn't have as much a problem with it as older hardware does.

 

The first question one should ask: What does the image look like if you don't do any branching? If the image is the same, is performance better, or worse?

 

If the answer to that is "unnaceptable", then one might consider: Is there some non piecewise function that approximates the piecewise function I have? This is a very hard question to answer, and these types of questions are a big part of the reason that people always say that computer graphics is a very hard field.


Teacher: Does anyone have a thin ruler?

Students: No.

Teacher: Ok. Does anyone have a nail file.

Students: Why do you need a nail file?
Teacher: I have a staple I need to remove.

Student: Oh. I have a staple remover...

 

People don't want power drills, they want holes.

Link to post
Share on other sites
Posted · Original PosterOP
20 minutes ago, straight_stewie said:

The first question one should ask: What does the image look like if you don't do any branching? If the image is the same, is performance better, or worse?

This qoute is something i needed^

 

It certainly feels like a hard field. My cousins husband does it professionally, and he said they have had people with masters that dont do so great. 

 

Thanks for the insight. Ill have to study more to really understand, but i know a good place to start now.

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×