10: The OpenGL Pipeline
07: Phong Shading
in Vulkan
COMP5822M: High Performance Graphics
Uniform Buffers
Vertex buffers are for attributes
Values that change for every vertex
Expensive to change (20K vertices => 2MB)
Should be loaded at startup/level load
Uniform buffers are for constants
These are the control variables
Often small in size (< 1KB)
Can be modified every frame
COMP5822M: High Performance Graphics
Types of Uniform Data
For now, there are really only three types:
Transformation matrices
Small, change every frame
Flags & constants to control rendering
Small, may change every frame
Textures
Large (MB+), often constant
Unless they are generated in multipass
COMP5822M: High Performance Graphics
Transformation Matrices
We may want:
Model matrix
View matrix
Projection matrix
Texture matrix
Shadow matrix
And many others . . .
COMP5822M: High Performance Graphics
Multiple Matrices
OpenGL had matrix stacks
Useful for animation
But this broke down with skinning
Vertices affected by multiple transformations
E.g. armpit – skin affected by chest & arm
Need to compute an average position
So need access to more than one matrix
COMP5822M: High Performance Graphics
Vulkan Solution
Uniform buffer is untyped (like all buffers)
Store anything you want
You have to specify alignment / offsets
So shader can find the data it needs
But you can pass anything you want
Including 50 different matrices
COMP5822M: High Performance Graphics
Uniform Buffer Stages
Control code specifies a descriptor layout
As part of pipeline creation
Then allocates a descriptor set from a pool
And binds the descriptor set for rendering
Layout (again) tells shader how to access it
Very similar to vertex buffer setup
But don’t use a staging buffer (why not?)
COMP5822M: High Performance Graphics
Image Buffers (Textures)
Textures are basically constants
Shared between many vertices
So they are passed as uniform buffers
Ideally, no texture swapping during the pass
But they need an extra layer
A sampler
Which takes care of interpolation / filtering
COMP5822M: High Performance Graphics
Fragment Processing
Stages:
Scissor test – use a rectangle to clip rendering
Depth test – use the z-buffer to discard
Stencil test – use a bitmap to clip rendering
These can be performed early or late (default)
Relative to the fragment shader
COMP5822M: High Performance Graphics
Fixed Function Pipeline
Vertex stage transforms vertices
Performs lighting calculations
Passes colour to rasteriser
Rasteriser interpolates colour
This is called Gouraud shading
Fragment stage combines with texture
And stores in frame buffer
COMP5822M: High Performance Graphics
Gouraud vs. Phong Shading
Phong shading is the ideal
Every pixel computes its own lighting
But this is (was) expensive)
Gouraud shading is a hack
Interpolate colour over each triangle
Much cheaper
But many fragments => many computations
COMP5822M: High Performance Graphics
Goal
Phong shading in fragment shader
Needs as input:
Fragment (position implicit)
Normal (in world coordinates)
Light position (ditto)
Material properties (usually white)
So vertex shader has to pass them through
COMP5822M: High Performance Graphics
Vertex Shader Inputs
Vertex Buffer:
Vertex position, normal
Vertex material properties
Vertex lighting
Uniform Buffer:
Light position & properties
Transformation Matrices
COMP5822M: High Performance Graphics
Vertex Shader Outputs
Vertex positions in NDCS
Normals in VCS
Light vectors in VCS
Lighting colour
Material properties
Texture coordinates
COMP5822M: High Performance Graphics
Raster Stage
Rasterises from vertex positions (NDCS)
Interpolates
Normals (VCS)
Light vectors (VCS)
Lighting colour (RGB)
Material properties (if desired)
Texture coordinates (UV)
COMP5822M: High Performance Graphics
Vertex Shader I/O
COMP5822M: High Performance Graphics
Vertex Shader Uniforms
COMP5822M: High Performance Graphics
Vertex Shader Main(), I
COMP5822M: High Performance Graphics
Vertex Shader Main(), II
COMP5822M: High Performance Graphics
Fragment Shader I/O
COMP5822M: High Performance Graphics
Fragment Shader Main(), I
COMP5822M: High Performance Graphics
Fragment Shader Main(), II
COMP5822M: High Performance Graphics