Since there are so many detailed tutorial about compute shaders so I’m not going to explain every line of the code. Here are just some basic concept.
Dynamic batching and Deferred
Vert and Frag + Zwrite On in Deferred are not dynamically batched
(it means transparent shaders will be batched)
unless the shader writes into GBuffer. But then, putting “DisableBatching” = “True” into this shader, it is still batched!
Standard Shader study [WIP]
[Good for shader debug] UAV RandomWriteTarget
Send data to c# script from shader: vert/frag and surface shaders!
Continue reading “[Good for shader debug] UAV RandomWriteTarget”
Shader Compilation
Import time – when you create a .shader file
- compile the shader variants only when needed, etc. platform, variants
Build time – when you build a player
- [level 1] ShaderLab -> glsl or hlsl or …etc
- if platform supports, from [level 2] hlsl -> IL or MetalSL to AIR…etc
- Cache identical shaders under Library/ShaderCache
- compile only not-yet-ever-compiled shaders (variants for this platform only)
Run time – when you open the app
- [level 3] glsl or hlsl or IL or AIR…etc -> driver (GPU bytecode)
- because target device is unknown for PC/mobile at buildtime
- Depends on driver, when to compile these codes e.g. only when it’s used
- use shader pre-warm to “use” shaders to trigger compilation
Sources from lukasc, robs, Unity Manual and https://blogs.unity3d.com/2014/05/06/shader-compilation-in-unity-4-5/
Mobile Graphics Optimization (for Artists)

Unite Europe 2016 – Optimizing Mobile Applications
https://www.youtube.com/watch?v=j4YAY36xjwE&list=PLX2vGYjWbI0RtPoeDoBUPnwY8USGra-pD&index=28
The uses of the 4 UVs
UnityObjectToClipPos(use float4 instead of float3)
In the past, we uses mul(UNITY_MATRIX_MVP, v.vertex) to convert vertex position from local to world space. v.vertex is float4 which has w component.
But in most cases w is = 1. To make vertex shader run faster, Unity replaced it with UnityObjectToClipPos(float3 pos), which ignores w component even you pass a float4 position instead of float3.
For some advanced users who still need the w component in their custom shaders, here is a cheaper UnityObjectToClipPos() function which respects the w component!😄
// More efficient than computing M*VP matrix product
inline float4 UnityObjectToClipPosRespectW(in float4 pos)
{
return mul(UNITY_MATRIX_VP, mul(unity_ObjectToWorld, pos));
}


