Jump to content
  • Advertisement
Sign in to follow this  
tmason

GLSL; return statement ...

This topic is 1271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello,

Simple question about GLSL and fragment shaders. May I return the "final color" early if I don't need to do any further processing in the void main function?

Consider the following example:

#version 330 core

out vec4 finalColor;

uniform float drawWireframe;

uniform vec4 materialColor;

vec4 fancyLightingFunction(vec4 colorToProcess) {

// Fancy lighting function here...

}

void main() {

if (drawWireframe == 1.0) finalColor = materialColor;

// May I call "return" here?

finalColor = fancyLightingFunction(materialColor);

}

Thank you for your time.

Share this post


Link to post
Share on other sites
Advertisement

 

Simple question about GLSL and fragment shaders. May I return the "final color" early if I don't need to do any further processing in the void main function?

Yes you can but it will not save you from any precessing in fact. Strange but true. Once gpu arrives at fragment processing, it is just too pointless to omit few local computations at the data. Surprising?

Share this post


Link to post
Share on other sites

I will add up, I think I was just too short. If you want to use a branch not for computational requirement, but for optimizing one, then it maybe might be possible that some optimization would result up if you branched by an uniform value. That might get rid of the branched routine call for entire draw call. But if you branch byt vertex atrribute value/texture fetch value/or any other draw data dependant volatile value, do not even do it unless it is for required computational issue .

Share this post


Link to post
Share on other sites

Dynamic branching with modern gpu's aren't that slow anymore. In fact its common optimization to skip large blocks of code. 

Share this post


Link to post
Share on other sites
Thank you for all of the feedback.

Since I am a beginner maybe my thinking isn't correct on this but I will share it in hopes of hearing what experts have to say on the matter:

I wanted to use one (1) shader for drawing such that I don't have to constantly switch shaders CPU-side. The shader would use float-based uniforms to chose whether to draw:
  • Wireframe
  • Skybox (using a texture2D sampler)
  • Standard shading (with different texture channels for ambient, diffuse, specular, emissive, etc.)
    • Within my standard shading model I plan to have the capability to calculate multiple lights and be able to position them, etc.
    • I would also have the capability to turn lighting "on" or "off" such that I can just show the ambient/diffuse textures without lighting calculations.
The method I was going about doing this was as shown in my OP as an example; if a "drawWireframe" uniform was equal to 1.0, use the wireframe section of code in my shader but if that uniform was 0.0 then do something else.

In my mind this was simpler than using multiple shaders as I am just altering the uniforms in memory but not changing shaders.

From what I read you should avoid changing shaders if you can.

Let me know if this makes sense and if my thinking is correct here.

Thank you. Edited by tmason

Share this post


Link to post
Share on other sites

There is no point to bundle all those shaders that do totally different things to single shader. Swapping shader for thing like sky box is no brainer. You draw sky box one time per frame and sky box can cover millions of pixels.

Share this post


Link to post
Share on other sites

From what I read you should avoid changing shaders if you can.

As a rule of thumb you should avoid unnecessarily changing render states or bound resources, yes... But you should also avoid unnecessary 'if' statements within shaders!

You're making every pixel much more expensive in order to save a little bit of CPU time.

Share this post


Link to post
Share on other sites

From what I read you should avoid changing shaders if you can.

As a rule of thumb you should avoid unnecessarily changing render states or bound resources, yes... But you should also avoid unnecessary 'if' statements within shaders!

You're making every pixel much more expensive in order to save a little bit of CPU time.


I see; is there a happy medium inbetween where I can use one shader to do simple stuff like draw SkyBoxes/wireframes?

I understand what folks are saying here; but having multiple shaders just to do simple tasks like drawing skyboxes/wireframes/other simple stuff seems like a waste, no?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!