10: The OpenGL Pipeline
05: Introduction to
GLSL & SPIR-V
COMP5822M: High Performance Graphics
Shaders
Essentially, the innards of a loop
Applied in parallel to many data
For each fragment
Invoke fragment shader
Need to discuss access to resources, output
Start with a diagram
COMP5822M: High Performance Graphics
The Vulkan Pipeline
COMP5822M: High Performance Graphics
Early Stages
Draw:
Commands enter the pipeline
Input Assembly:
Reads index/vertex buffers
Vertex Shader:
Transforms & processes the vertices
COMP5822M: High Performance Graphics
Tessellation Stages
Tessellation Control Shader:
Generates patch tessellation commands
Tessellation Primitive Generation:
Breaks patches into smaller patches
Tessellation Evaluation Shader:
Sets attributes for new vertices
Similar to vertex shader
COMP5822M: High Performance Graphics
Geometric Stages
Geometry Shader
Operates on full primitives
Can change primitive type
Primitive Assembly
Preps vertices for rasterisation
Clip & Cull
Early discard for offscreen primitives
COMP5822M: High Performance Graphics
Rasterisation
Many options, but basically fixed function
Rasterises & generates fragments
Computes barycentric coordinates
Uses them to interpolate attributes
COMP5822M: High Performance Graphics
Fragment Stages
Prefragment Operations
Early discard before shading (depth, stencil)
Fragment Assembly
Groups data for fragment shader
Fragment shader
Code for doing shading / rendering
COMP5822M: High Performance Graphics
Final Stages
Postfragment Operations
Deferred prefragment operations
If Fragment shader changes data
Color Blending
Updates the Framebuffer
Performs image processing
COMP5822M: High Performance Graphics
Writing Shaders
A shader is therefore just a small program
With a main() routine
Known as an entry point
We need to discuss:
Compilation
Language
I/O
COMP5822M: High Performance Graphics
Shader Compilation
Can be done:
Runtime (GLSL)
Precomputed (CUDA)
Intermediate Code (SPIR-V)
All of which have advantages / disadvantages
COMP5822M: High Performance Graphics
SPIR-V Compilation
Shaders precompiled to modules (libraries)
Collections of functions with entry points
Each has a name
And a type (which pipeline stage it is for)
Stored as a stream of 4B words
Essentially, an opcode / bytecode like Java
Can be inspected with spirv-dis
COMP5822M: High Performance Graphics
GLSL
Essentially a dialect of C
With some C++ conveniences
We (like Vulkan) will use GLSL
Others are similar
Most of the standard library routines built-in
Except memory allocation & I/O
COMP5822M: High Performance Graphics
GLSL Types
bool: boolean type, as C++
int/ uint: basic integer type (usually 4B)
float / double: IEEE floating point
float is often a lot faster than double
vec2, vec3, vec4: floating point vectors
integers / doubles also available
mat2, mat3, mat4: matrices
COMP5822M: High Performance Graphics
Matrix Types
Float & double are available, no int or bool
Represented in column-major format
Can use m[3] to refer to column 3
All usual arithmetic operations defined
But you’ll still need a library on CPU
COMP5822M: High Performance Graphics
I/O
Shaders have no print routines
Instead, they have shared buffers
Which change from time to time
So they need to be declared explicitly
Based on resources and descriptors
Resource: a variable outside the shader
Descriptor: a bundle of resources
COMP5822M: High Performance Graphics
Descriptor Set
Set of resources bound as a group
Typically textures, samplers or buffers
Set up as part of the pipeline
Then bound to the inputs of the shader
Push constants are a special case
Variables set directly from command buffer
All others are set by storage in a buffer
COMP5822M: High Performance Graphics
Shader Memory Access
Shaders can have local variables
These are essentially register variables
Anything else is a resource (three kinds):
Uniform Blocks
Shader Storage Blocks
Texel Buffers
COMP5822M: High Performance Graphics
Uniform Blocks
Read-only memory
Shared between all invocations of the shader
Limited size
Usually the fastest memory (ie. cache)
Declared with the uniform keyword
COMP5822M: High Performance Graphics
Shader Storage Blocks
Read-write access
Support atomic operations
Therefore often slower
But much larger in size
Declared with buffer keyword
COMP5822M: High Performance Graphics
Texel Buffers
Read-only
Can convert formats for you
Can do interpolation
Best choice for large arrays of data
Accessed with texelFetch() function
Declared as uniform samplerBuffer
COMP5822M: High Performance Graphics
A Simple Vertex Shader
COMP5822M: High Performance Graphics
And a Fragment Shader
COMP5822M: High Performance Graphics
Vulkan Pipeline.graffle
Draw
Input Assembly
Vertex Shader
Tessellation
Control Shader
Tessellation
Evaluation Shader
Geometry Shader
Primitive
Assembly
Clip and Cull
Rasteriser
Prefragment
Operations
Fragment Shader
Postfragment
Operations
Color Blending
Ind
irect B
u
ffer
Ind
ex B
u
ffer
V
ertex B
u
ffers
Input Buffers
P
u
sh C
onstants
Im
ages
U
niform
B
u
ffers
Storage B
u
ffers
Texel B
u
ffers
Descriptors
D
ep
th-Stencil
A
ttachm
ents
Inp
u
t
A
ttachm
ents
C
olor
A
ttachm
ents
Framebuffer
Draw
Input Assembly
Vertex Shader
Tessellation
Control Shader
Tessellation
Evaluation Shader
Geometry Shader
Primitive
Assembly
Clip and Cull
Rasteriser
Prefragment
Operations
Fragment Shader
Postfragment
Operations
Color Blending
I
n
d
i
r
e
c
t
B
u
f
f
e
r
I
n
d
e
x
B
u
f
f
e
r
V
e
r
t
e
x
B
u
f
f
e
r
s
Input Buffers
P
u
s
h
C
o
n
s
t
a
n
t
s
I
m
a
g
e
s
U
n
i
f
o
r
m
B
u
f
f
e
r
s
S
t
o
r
a
g
e
B
u
f
f
e
r
s
T
e
x
e
l
B
u
f
f
e
r
s
Descriptors
D
e
p
t
h
–
S
t
e
n
c
i
l
A
t
t
a
c
h
m
e
n
t
s
I
n
p
u
t
A
t
t
a
c
h
m
e
n
t
s
C
o
l
o
r
A
t
t
a
c
h
m
e
n
t
s
Framebuffer