• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by electricsauce · Apr 21, 2013 at 10:41 PM · shaderlerpfragment

shader if else performance

How expensive are if statements inside a fragment shader? How do they compare performance wise against a lerp?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

Answer by Bunny83 · Apr 21, 2013 at 11:05 PM

I think it depends on the hardware but in general they are really bad. That's because the GPU is a pipeline processor. It works best with a fix set of instructions on a large amount of data which is called SIMD(single instruction multiple data). See the Advantages / Disadvantages sections. Any kind of branching is inefficient because the pipeline has to be rebuild. Modern GPUs have a great branch prediction and can hold multiple pipeline versions but in most cases a lerp instruction is better ;)

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image electricsauce · Apr 21, 2013 at 11:19 PM 0
Share

Ok This is going to be tricky then. I have: if(distance > _Value){ SomethingAwesomeHappens; }

avatar image Bunny83 · Apr 21, 2013 at 11:51 PM 0
Share

What is "SomethingAwesomeHappens"? You can construct a value that is 1 if your condition is "true" and 0 if it's false.

Something like that:

 // hlsl
 v = max(0,sign(distance - _Value))

However it depends on what SomethingAwesomeHappens does and if you can control it's result with a float value of 0 / 1. The compiler also might optimise a simple if else block into something like the above mentioned. It can also "unroll" for-loops if they are short and predictable.

avatar image electricsauce · Apr 22, 2013 at 12:00 AM 0
Share

I'm assigning one of two textures depending on their distance from a point. If it's within a specified range of the median point, it lerps between the two, otherwise it picks one depending on distance.

if((distance < _ColorDecay + _FadeDistance) && (distance > _ColorDecay - _FadeDistance) ) { fixed mixFactor = saturate(1 - (((distance - _ColorDecay) + _FadeDistance) / (_FadeDistance * 2)));

     cfinal =  lerp(c, c2, mixFactor);
     }
         
             

else if(distance > _ColorDecay) { cfinal = c; }

else { cfinal = c2;
}

avatar image Bunny83 · Apr 22, 2013 at 12:40 AM 0
Share

Uhm, what's actually the point of your if else statement? Your lerp already does everything at once since saturate clamps your mixFactor between 0 and 1.

So just use this

 fixed mixFactor = saturate(1 - ((distance - _ColorDecay) / _FadeDistance + 1) * 0.5);
 cfinal = lerp(c, c2, mixFactor);
avatar image electricsauce · Apr 22, 2013 at 12:47 AM 0
Share

That works perfectly. I have no idea why I was trying to do this the hard way. Thanks!

avatar image
-1

Answer by Lumen Digital · Jan 25, 2014 at 09:17 PM

Hi - In your opinion how evil is a fragment program like this that has 2 returns based on a conditional? Will the GPU have to process both outputs?

 float4 frag (vertex_output IN): COLOR {
 if ( sunColor.z < 0.15){
         float4 stars = tex2D( starTexture,IN.uv.xy );
         stars *= 1 - saturate(sunColor.z * 4);
         return stars + IN.color * float4(Saturate(sunColor,0,1.5,1,0,0),1);
     }
 return IN.color * float4(Saturate(sunColor,0,1.5,1,0,0),1);
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
-2

Answer by schmidtrobin · Dec 18, 2013 at 08:36 PM

Hey I have a question to this Topic.

I have something in my head like a pixelbased multimaterial. I want to seperate phongshader and labertshader by an pixelbased if calculation to save math operations. for example one part of the model use Nomalmaps and specular and an otherpart dont use it. i think to use vertex/fracment with an lerp Funktion. in the end it isnt specular normal some more expensive per Pixel math.

is it better to lerp between this or make some per Pixel if calculation to save not visible math or just use a different lighting model?

thanks.. my english isnt the best but i hope u get the Point.

Thanks Robin

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Statement · Dec 18, 2013 at 10:26 PM 4
Share

Ask a new question. Don't post a question as an answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

15 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vertex/Fragment shaders - transparency ignoring Render Queue 3 Answers

Help getting shader to fade textures then stop as opposed to Lerp back and forth 0 Answers

Cutaway shader for sprites 0 Answers

Shader - Directional Lights Depth 0 Answers

_CameraGBufferTexture3 output in shaderlab 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges