Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
3
Question by dyankov · Nov 26, 2013 at 05:56 PM · shader

Surface vs Vertex/Fragment shaders

Hi,

I'm looking to get into shader programming and I'm doing a small research. I watched a couple videos at cgcookie.com about shader writing in Unity (great website btw) and the guy claimed that surface shaders are more expensive to render than vertex/fragment shaders. However, I couldn't find such information in the Unity docs.

Can anyone confirm or disregard this?

If that's really the case:

  • is it a big performance hit?

  • should I use fragment shaders instead for my mobile game?

Comment
Add comment · Show 3
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 Professor Snake · Nov 26, 2013 at 05:59 PM 0
Share

Surface shaders are compiled to a bunch of vertex/fragment shaders anyway. They are more expensive because they automatically handle lighting calculations even if you don't want to.

avatar image dyankov · Nov 26, 2013 at 06:08 PM 0
Share

In my case I want them to calculate lighting, so that's fine. I'm more interested in point 1 of my question - how big is the performance hit and should I be worried about it

avatar image Dante_CyberdeckGames · Sep 12, 2016 at 12:55 PM 0
Share

It looks like you can override lighting as described here: https://docs.unity3d.com/$$anonymous$$anual/SL-SurfaceShaderLightingExamples.html

4 Replies

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

Answer by tanoshimi · Nov 26, 2013 at 10:24 PM

As already pointed out, "surface shaders" are not really shaders - think of them as (Unity-specific) macros/templates that can be used to automate a lot of standard shader code - which are then compiled into regular Cg vertex/fragment shaders. Everything that can be done in a surface shader could be written by hand in a vertex/fragment shader (although note that the reverse is not always true).

As with any process, there is necessarily a bit of a trade-off between convenience of using an automated system, compared to the loss of control over fine optimisation that could be achieved manually. However, particularly if you're new to shader writing, there is also the potential for you to make your own mistakes that will cost far more than those apparent inefficiencies caused by the surface shader template.

I'd like to point out that by adding #pragma debug to your surface shader you can view the complete vertex/fragment shader code that it translates to, and then, if you feel any optimisation is necessary, you can edit that shader by hand, which gives the best of both worlds.

As a final point, you might want to notice that most of the built-in Unity shaders are created as surface shaders, including: Diffuse, Bumped, Reflective/Specular, Transparent/Parallax... if there was a noticeable performance hit involved, I don't think Unity would be doing so ;)

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 Owen-Reynolds · Nov 26, 2013 at 11:15 PM 1
Share

ShaderLab is mostly Unity specific. The code in surface shaders is almost identical to "real" CG code (except for all the missing lighting math, and any Unity-specific function calls.)

avatar image
3

Answer by MaT227 · Nov 26, 2013 at 06:10 PM

If you want to get into shader programming, I suggest you to start with surface shader because a lot of lighting calculation are done under the hood and the results are ready to use. With this you can discover shader programming.

But of course there is a performance hit. But I think this is not significant. Unless you want super optimized mobile shader. But if you want this, I think you are quite comfortable with shaders.

When you are comfortable with surface shader you can look at fragment shader and start developping them. A very good point is that they handle lighting which is a hard piece in fragment I think.

With fragment shader you have total control of what is calculated and with this you can optimize your shader. But I think it's a bit hard for a beginner.

This depends of what shader you want to develop. Simple rim, unlit etc... shaders can be easily done with fragment shader.

As you said CGCookie have a very good tutorial selection about fragment shader which can help you jump into fragment shader programming.

Hope it helped.

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
3

Answer by Owen-Reynolds · Nov 26, 2013 at 09:36 PM

Cars with air-conditioning are more expensive than cars without. But buying a non-air-conditioned car, then installing AC, that will probably cost more and not work as well.

Surface shaders "cost more" since they perform lighting. But, same as cars, if you're going to have lighting anyway, you may as well use a surface shader. Also, same as cars, if you really know what you're doing, in some cases you may save by custom-adding your own lighting to a vertex shader (but there are probably a dozen better non-shader speedups, depending on the game.)

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
0

Answer by Khalanar_Dev · Apr 24, 2014 at 07:08 AM

As far as I'm concerned there is one more important difference, surface shaders get all messed up when lightmapping them with beast and Vertex/Frag seem to work correctly. Is this correct or I'm not seeing the whole of it?

Comment
Add comment · Show 2 · 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 tanoshimi · Apr 24, 2014 at 07:43 AM 0
Share

It's you ;) Did your follow "decoding lightmaps" at https://docs.unity3d.com/Documentation/Components/SL-SurfaceShaderLighting.html?

avatar image Khalanar_Dev · Apr 24, 2014 at 04:10 PM 0
Share

Wow, totally ran over that, thanks!! Anyway, I'm trying to get lightdir into this, and I am not able to do it. I would know how to do so in Vert/Frag but not in surface. Any advice tanoshimi?

 half4 LightingRamp_SingleLightmap (SurfaceOutput s, fixed4 color){
      half3 lm = DecodeLightmap (color);
      return half4 (lm, 0);
 }

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

22 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

Related Questions

How does Unity apply a lightmap? 0 Answers

How to make a submesh from part of my character 1 Answer

A node in a childnode? 1 Answer

Alpha Transparency Shader Issue 1 Answer

How to implement shader 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