Posted February 8 · 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
Posted February 8 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
Posted February 8 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: Static branching: Everything is known at compile time and is optimized away. There is no actual branching occuring when your shader runs. 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.). 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. Check out these threads --> Explanation of PCIe How to Pick a Programming Language Aerospace Link to post Share on other sites
Posted February 8 · 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