• 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
Question by daniel-eherbert · Apr 02, 2015 at 01:52 PM · shaderrenderingperformancepass

Is a shader with selective passes faster than a single-pass which uses lerp to enable/disable shader effects?

Been messing around with shaders a lot recently, both via code and via Shader Forge.

I noticed that in the standard and image effects shaders that Unity uses multiple shader pass declarations to contain functions of a shader (eg, different methods of blurring for bloom). The pass is then defined in the script which handles the processing.

From Unity's bloom script:

 Graphics.Blit (rtDown4, quarterRezColor, screenBlend, 6);

And part of the relevant shader:

  // 4: nicer & softer "screen" blend mode(cheapest)
 Pass {    
         // ..... snip .....
 }  
  // 5: "add" blend mode (cheapest)
 Pass {    
         // ..... snip .....
  }     
 // 6: used for "stable" downsampling (blur)
 Pass {    
      CGPROGRAM
      #pragma vertex vertMultiTap
      #pragma fragment fragMultiTapBlur
      ENDCG
  }   


Currently I've been achieving similar selective functionality by using Switches in Shader Forge (which essentially boil down to a lerp between two values based on a 0f/1f float you define in the shader parameters).

To (very, very simply) clarify how a switch works by adding a color to a texture:

 //_TheTexture = texture property, value set by material
 //_DoColoring = float, value of 0f or 1f set by material
 float4 frag (VertexOutput i) : COLOR {
     float4 outputColor = tex2D(_TheTexture, i.uv0);
     float4 color = float4(1, 0, 0, 1); //red

     outputColor = lerp(outputColor, outputColor * color, _DoColoring);

     return outputColor;
 }

Obviously that is a very crude example, but the A and B values of the lerp could have any amount of processing behind them.

Are the A and B values of the lerp both calculated if the lerp amount is 0 or 1, or is the alternate value (and processing) completely ignored? Obviously with a value between 0 and 1 both would need to be done...

I suppose there are other benefits to using selective passes (such as handling hardware support, being able to use cheaper versions of processing methods on slower systems, etc), but generally is that approach faster than leaving it all up to the GPU?

Comment

People who like this

0 Show 1
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 Tudor · Aug 31, 2015 at 11:40 AM 0
Share

I don't know much about how unity manages shader code at runtime, but I think it's a stretch to think that whenever it sees your_FLAG is 0, it traces back through the code to ignore every path that only writes to that lerp function.

But even if this happened, it would make no sense to think it would be faster than using if statements in the shader. If statements aren't super fast in shaders, but if you can use them to hide more than trivial computations, you should.

I don't know how selective use of shader Passes works (and how fast that is), and am also interested in hearing about it.

0 Replies

  • Sort: 

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

uGUI Image without transparency/ opaque render queue 0 Answers

Low Performance when drawing many objects 1 Answer

Forcing Multi-Pass shader to use Deferred passes instead of Forward passes? 1 Answer

Multipass Shader Question - How to handle alpha blending between passes 0 Answers

Is the template containing Shader Graph heavy on performance? 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