Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by Feelnside · Aug 14, 2018 at 10:16 AM · shadersperformanceoptimization

What is more optimized: use manually the shader variables or declare it and then use

I'm trying to find out the best way to use the shader variables.

We have some kind of parameters which can be used in the shaders. For example some light parameters: _WorldSpaceLightPos0 - Directional lights: (world space direction, 0). Other lights: (world space position, 1).

So I have two ways: 1)

 half3 lightPos = _WorldSpaceLightPos0.xyz;
 o.diff = v.color * max(0, dot(o.worldNormal, lightPos));
 o.lightDir = dot(o.worldNormal, normalize(lightPos + o.worldViewDir));

2)

 o.diff = v.color * max(0, dot(o.worldNormal, _WorldSpaceLightPos0.xyz));
 o.lightDir = dot(o.worldNormal, normalize(_WorldSpaceLightPos0.xyz + o.worldViewDir));

Could you please clarify which one will be more optimized?

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

1 Reply

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

Answer by RShields · Aug 15, 2018 at 05:06 AM

They should both compile to the exact same thing. You can check this with Show Generated Code in the Inspector.

This is because the compiler is pretty clever. While some are even better than others, all compilers are expected to handle cases like this by using two memory reads, rather than a read-write, read, read.

Comment
Add comment · Show 4 · 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 RShields · Aug 15, 2018 at 05:10 AM 0
Share

Probably the biggest optimizations available in shaders are to avoid branching and to use $$anonymous$$AD operations rather than ADD, $$anonymous$$UL. Some compilers are smart enough to figure out the latter, but the former is often just not feasible to optimize on the compiler side.

avatar image Feelnside RShields · Aug 15, 2018 at 07:55 AM 0
Share

Thank you so much! Could you please clarify what is the $$anonymous$$AD operations? Just tried to google it but looks Like I'm doing something wrong.

avatar image RShields Feelnside · Aug 16, 2018 at 08:14 PM 1
Share

$$anonymous$$ost graphics cards have the ability to perform a*x + b in one step rather than doing a*x then +b. However, compilers have a hard time optimizing code. Here's an example:

  • float y = (x + 2.0) / 2.0; will likely be converted to an ADD then a DIV

  • float y = (x + 2.0) * 0.5; will likely be converted to an ADD then a $$anonymous$$UL

  • float y = (x / 2.0) + 1.0; will likely be converted to a DIV then an ADD

  • float y = (x * 0.5) + 1.0; will likely be converted to a $$anonymous$$AD

Therefore, the bottom code runs twice as fast as the top ones.

Show more comments

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.

Follow this Question

Answers Answers and Comments

120 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 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 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 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 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 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 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 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

What's a good method for measuring shader performance? 0 Answers

Performance Question: Baked textures vs Procedural shaders 0 Answers

Batch texture sampling in shader? 1 Answer

How Can I Reduce Build Time for Script-Heavy Projects? 2 Answers

How to optimize with a lot of objects 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