Unity3D & Shader Programming

In Game Development Rendering (draw calls) of the game is one of the key value for performance. In Game programing, Right Shaders can make your game fast draw call or wrong shaders can low down your game performance.

Shaders are used to do shading, lighting and color on the objects. Shaders calculate rendering effects on graphics hardware with a high degree of flexibility. Most shaders are coded for a graphics processing unit (GPU), though this is not a strict requirement, Normally shader are written in ARB assembly Language. Now days few Shading languages are also available. and it's easy to write shader then ARB Language.

Now days Mostly this four languages are used for shader programing
  • HLSL (used for DirectX and Direct3D)
  • GLSL
  • OpenGL ES (Mostly  used for mobile devices)
  • CG
All Shading Language has it's benefits and drawback, we will talk about it later..

In Unity3D, Unity Support three kind of Shaders
  1. ShaderLab
  2. GLSL
  3. CG
ShaderLab is designed by Unity3D and Unity3D's compiler convert it's matched ARB  program at on fly (runtime). ShaderLab is very easy to learn and write simple shader. ShaderLab Shader can lots of thing automatically on based of few keywords. Even ShaderLab Shader are pre-optimized for performance, But using ShaderLab we can't write complex shader.

So when we are looking for basic shader where complex calculation not required, Unity's ShaderLab is Best option to use.

Even ShaderLab is good Start also at time of learning How write shaders for Unity3D.

GLSL or GLslang Developed by OpenGL and known as OpenGL Shading Language, and it's High-Level Shading Language. GLSL is based on C Language. OpenGL has a cross platform functional and available for MAC, Windows, Linux and Mobile Device (OpenGL ES).

This feature makes GLSL to popular and widely used Shading language, and even OpenGL will support complex shader calculation without any performance issue. GLSL shader requires two type of programs.
  1. Vertex
  2. Fragment
CG is also known as C for Graphic and Developed by Nvidia with collaboration with Microsoft Corporation. CG also based on C language, and closely same as HLSL (Microsoft DirectX Lang.) and it's High Level Shading Language. But CG have a benefits, it can run on both type of hardware there is no limitation of Graphic Hardware. CG can compile it's code to hardware matched  ARB assembly Language. CG is Little bit easy to write shader programs, Basically it's also need two kind of program to compute proper result on GPU/
  1. Vertex
  2. Fragment
But CG also can understand Surface program, and'ts doesn't require fragment / vertex program. it's compute own vertex and fragment program as surface program requires output. Using Surface program in CG performance will goes down, because it's write vertex and  fragment program on fly, but using surface program only a keyword make lots of code, and surface program can calculate lighting effect automatically when it's write vertex and surface program.

On another side when we use vertex and fragment program, coder need to take care of calculating light effects.