Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
An Introduction to
Shader Programming
in Unity
A MEF Training Presentation - 2016
Graphics Hardware
Central
Processor
CPU
Memory
Graphics
Processor
GPU
Memory
Frame Buffer
Its Programmable Hardware!
OpenGL
API
GLSL
Programs
Cg
Programs (Nvidia)
Khronos Group
EGL
API
Vendor Specific
Fixed Funct...
Well some parts are
programmable & others can be
configured with fixed function calls
OpenGL
API
OpenGL Pipeline
Vertex Bu...
Vertex Buffer Objects
You store Vertex properties in these:
Positions,
Texture Coordinates,
and so on…
Op
en
GL
Pip
elin
e
Vertex Shader
You write a piece of code that
can change those Vertex properties
Primitive Assembly
Converts the Vertices into primitives and
clips shapes that are out of
the camera view
Triangles
Lines
...
Rasterization
Converts the primitives
into Fragments
(Unborn Pixels)
Texture Memory
You upload Textures into the GPU
memory. These are called Samplers.
Fragment Shader
You write a piece of code that
colors the fragments.
Fragment Operations
Recap…
OpenGL
API
OpenGL Pipeline
Vertex Buffer
Objects
Vertex
Shader
Primitive
Assembly
Rasterization
Texture
Memory
Frag...
Before we dive into
code…
We must gain a basic understanding
of the
Model View Projection Matrix!
Models are created in
‘Model Space’ coordinates
But their coordinates transform
when you put them in
‘World Space’
And coordinates transform once
again when viewed through
‘Camera Space’
Finally the coordinates transform
once more based on the ‘Projection’
Therefore EVERY vertex has
to go through…
Model Coordinates
World Coordinates
Camera Coordinates
Projection Coordinates
[M...
For model vertices to become
screen vertices,
we must transform them via
complex matrix multiplications…
The bad news is t...
But Unity makes it very very easy for us
with a built in matrix variable we can use…
UNITY_MATRIX_MVP
UNITY_MATRIX_MVP
X
Model Vertex Position
=
Screen Ready Position!
Unity makes programming
graphics and shaders
VERY easy…
In order to appreciate that, it is
important to look at what it takes
to render a cube if there was no Unity…
Fire up Xcod...
So Unity takes care of all this complexity
plus gives us ShaderLab: Any easy
interface to write shaders with…
Op
en
GL
Pip...
ShaderLab is a meta language that
wraps around a Cg program
Vertex
Shader
Fragment
Shader
ShaderLab
Cg Code
ShaderLab
Code...
A ‘Shader Program’ requires both a
Vertex Shader + Fragment Shader
Vertex
Shader
Fragment
Shader+ =
Shader
Program
Shader Core Inputs & Outputs
Vertex
Shader
Runs for each Vertex
Fragment
Shader
Runs for each Fragment
Vertices Fragments
...
Let’s get going with
writing Shaders…
Fire up Unity!
The rest of this presentation is a hands-on shader writing workshop…
Upcoming SlideShare
Loading in …5
×

Shader Programming With Unity

Shader programming is typically an esoteric technological area for those that have started off programming game clients using Unity. Since Unity abstracts out the graphics pipeline into an easy to use visual editor, we rarely delve into the inner workings of the programmable GPU

  • Be the first to comment

Shader Programming With Unity

  1. 1. An Introduction to Shader Programming in Unity A MEF Training Presentation - 2016
  2. 2. Graphics Hardware Central Processor CPU Memory Graphics Processor GPU Memory Frame Buffer
  3. 3. Its Programmable Hardware! OpenGL API GLSL Programs Cg Programs (Nvidia) Khronos Group EGL API Vendor Specific Fixed Functions *Microsoft equivalent is DirectX and HLSL (Similar to Cg)
  4. 4. Well some parts are programmable & others can be configured with fixed function calls OpenGL API OpenGL Pipeline Vertex Buffer Objects Vertex Shader Primitive Assembly Rasterization Texture Memory Fragment Shader Fragment Operations Frame Buffer EGL API
  5. 5. Vertex Buffer Objects You store Vertex properties in these: Positions, Texture Coordinates, and so on… Op en GL Pip elin e
  6. 6. Vertex Shader You write a piece of code that can change those Vertex properties
  7. 7. Primitive Assembly Converts the Vertices into primitives and clips shapes that are out of the camera view Triangles Lines Points etc…
  8. 8. Rasterization Converts the primitives into Fragments (Unborn Pixels)
  9. 9. Texture Memory You upload Textures into the GPU memory. These are called Samplers.
  10. 10. Fragment Shader You write a piece of code that colors the fragments.
  11. 11. Fragment Operations
  12. 12. Recap… OpenGL API OpenGL Pipeline Vertex Buffer Objects Vertex Shader Primitive Assembly Rasterization Texture Memory Fragment Shader Fragment Operations Frame Buffer EGL API
  13. 13. Before we dive into code…
  14. 14. We must gain a basic understanding of the Model View Projection Matrix!
  15. 15. Models are created in ‘Model Space’ coordinates
  16. 16. But their coordinates transform when you put them in ‘World Space’
  17. 17. And coordinates transform once again when viewed through ‘Camera Space’
  18. 18. Finally the coordinates transform once more based on the ‘Projection’
  19. 19. Therefore EVERY vertex has to go through… Model Coordinates World Coordinates Camera Coordinates Projection Coordinates [Model Matrix] [View Matrix] [Projection Matrix]
  20. 20. For model vertices to become screen vertices, we must transform them via complex matrix multiplications… The bad news is that for OpenGL we have to calculate the MVP matrix manually…
  21. 21. But Unity makes it very very easy for us with a built in matrix variable we can use… UNITY_MATRIX_MVP
  22. 22. UNITY_MATRIX_MVP X Model Vertex Position = Screen Ready Position!
  23. 23. Unity makes programming graphics and shaders VERY easy…
  24. 24. In order to appreciate that, it is important to look at what it takes to render a cube if there was no Unity… Fire up Xcode on your MacBooks!
  25. 25. So Unity takes care of all this complexity plus gives us ShaderLab: Any easy interface to write shaders with… Op en GL Pip elin e Vertex Shader Fragment Shader ShaderLab Surface Shader
  26. 26. ShaderLab is a meta language that wraps around a Cg program Vertex Shader Fragment Shader ShaderLab Cg Code ShaderLab Code Surface Shader
  27. 27. A ‘Shader Program’ requires both a Vertex Shader + Fragment Shader Vertex Shader Fragment Shader+ = Shader Program
  28. 28. Shader Core Inputs & Outputs Vertex Shader Runs for each Vertex Fragment Shader Runs for each Fragment Vertices Fragments Colored FragmentsUniforms
  29. 29. Let’s get going with writing Shaders… Fire up Unity! The rest of this presentation is a hands-on shader writing workshop…

×