10+ +Rasterization+Pipelines
10+ +Rasterization+Pipelines
Christoph Garth
Scientific Visualization Lab
Motivation
Contents of this chapter:
Even though the specification is written in C, there are bindings for many programming
languages.
WebGL renders hardware accelerated graphics in browser based on JavaScript and the
<canvas>-element. It is based on the OpenGL ES standard.
Overall the different OpenGL variants have a lot in common – knowledge of one variant
carry over to others.
This ia also true for Microsoft Direct3D, the graphics sub-API of DirectX, as well as Vulkan
(cross-platform) and Metal (Apple). Since graphics APIs are thin abstractions of the
graphics hardware, many concepts are similar.
Modern approach: complete specification of geometry, after that the draw call (e.g.
glDrawArrays() –> retained mode
Rasterization
Vertex Stream
• Assembly of primitives from vertices
and identification of covered pixels; Primitive Assembly
for each pixel and primitive a Scan Conversion
fragment is generated
Fragment Stream
normalized window
object space view space clip space device coordinates coordinates
Fragment
Stream
Stream
Vertex
Vertex Stream
Primitive Assembly
Scan Conversion
Fragment Stream
Fragment Stream
Output / Blending
uniforms
Transformation Matrix
...
constant over all vertices of a draw call
attributes varyings
Position Position
(object space) Vertex (clip space)
Normals Shader Normals
... ...
per vertex per vertex
1 // input (attributes)
2 in vec3 vertexPosition;
3 in vec3 vertexNormal;
4
5 // output (varyings)
6 out vec3 normal;
7
8 // constant parameters (uniforms)
9 uniform mat4 mvpMatrix;
10 uniform mat3 normalMatrix;
11
12 void main(){
13 // compute transformed vertex in homogeneous clip coordinates
14 gl_Position = mvpMatrix * vec4( vertexPosition, 1.0 );
15
16 // compute normal in world coordinates
17 normal = normalMatrix * vertexNormal;
18 }
float rmm[16] = {
row-major
a b c d a, b, c, d,
e f g h
e, f, g, h,
i j k l
i, j, k, l,
m n o p m, n, o, p
};
float cmm[16] = {
column-major
a b c d a, e, i, m,
e f g h
b, f, j, n,
i j k l
c, g, k, o,
m n o p d, h, l, p
};
OpenGL docs: “The translation components [d, h, l] occupy the 13th, 14th, and 15th
elements of the 16-element matrix.” column-major layout expected.
Computer Graphics – Rasterization Pipelines– Vertex Stage 10–15
Specification of Input Mesh
attribute position V0,x V0,y V0,z V1,x V1,y V1,z V2,x V2,y V2,z
attribute normal N0,x N0,y N0,z N1,x N1,y N1,z N2,x N2,y N2,z
index buffer 0 1 2
Primitive types:
modern graphics hardware focuses near exclusively on triangles (and potentially lines).
All other primitives are emulated and are no longer supported in OpenGL from version 3.3
onward.
Fragment Stream
Output / Blending
glFrontFace( GL_CCW );
• Caution: Orientation is set by order of traversal
glCullFace( GL_BACK );
in the image plane and not by the (in principal glEnable( GL_CULLING );
optional) normal vector attribute.
(−1, 1, 1)
(−1, 1, −1) +y (1, 1, 1)
+z
(r, t, f )
+x
(l, t, n)
(r, t, n) (−1, −1, −1) (1, −1, 1)
+y
(l, b, n)
+x (r, b, n) (1, −1, −1)
+z
The rasterizer generates fragments (parts of a primitive that fall into a pixel) from the
primitives (e. g. as described in Chapter 3).
Furthermore:
• If necessary, fragments that are not inside the viewport are discarded.
• Interpolation of varyings (e.g. barycentric)
• Interpolation of z-coordinate and depth test ( later)
• Stencil test ( later)
• pixel coordinates
• interpolated z-value
• interpolated attributes (color, normals,. . . )
These are emitted as a fragment stream to the fragment stage and are processed there
(typically in parallel).
per-fragment data
x y z CR CG CB Nx Ny Nz ...
Vertex Stream
Vertex Stream
Primitive Assembly
Scan Conversion
Fragment Stream
Fragment Processing
Fragment Stage / Shader
• Shading and fragment color (programmable)
// output
out vec4 fragColor;
// constant parameters
uniform vec3 lightDirection;
uniform vec3 objectColor;
void main()
{
vec3 nnormal = normalize( normal );
float kd = max( 0.0, dot( nnormal, lightDirection ) );
3 1 4 2
It gives the distance to the near plane and is used far plane
for the so-called depth test (→ output stage) 1
view
frustum
Depth test: If fragments of multiple surfaces fall
into the same pixel, only the fragment that is 0
• The depth buffer is initialized with the largest possible depth (1.0)
• If a fragment produces an output in the color buffer, its z-value is written to the
depth buffer.
• A fragment only produces output if its z-value is smaller than the one stored in the
depth buffer.
Illustration:
If the z-value is not changed by the fragment shader, the depth test can be performed
before the fragment stage. This is done often in practice (→ early-z).
glEnable( GL_DEPTH_TEST );
glDepthFunc( GL_LESS );
For some specific effects it might be useful to do the depth test without overwriting the
z-buffer:
glDepthMask( GL_FALSE );
During fragment rendering, a bitmask can be set in the stencil or manipulated by logical
operators (AND, OR, XOR).
Later fragments are tested with logical bit operation against the existing values and are
shaded only if the test is not returning 0.
The stencil test occurs after the depth test in the pipeline.
Yes
24 bit depth Compare src and dst depth values
+8 bit stencil Depth Test result
= 32 bit
No
Is Depth Write enabled?
This minimizes memory access
cost if both are in use. Yes
End Depth Test
No
Is Stencil Test Result TRUE?
Historically, depth and stencil
Yes
test are often integrated in the
Apply Depth Write Mask and
End Depth Test
same hardware unit. save depth component
There are different blend functions that allow for various effects.
Intuition: α ∈ [0, 1] describes which fraction of the fragment color is carried over into the
pixel.
Illustration:
α-blending
C = α1 C1 + (1 − α1 )α2 C2
α = α1 + (1 − α1 )α2
Correct transparency via blending requires that fragments further in front are drawn after
fragments further in back (correct z-order). The depth buffer cannot ensure this.
In order to correctly represent transparent objects, the following approach can be used:
Start blending
If blending is deactivated
No
(default), the pixel color is Is blending enabled?
hardware, see
glBlendEquation(). Blend operation Use pixel shader output
End blending
Many effects have to be recreated (with large efforts) and only represent a bad copy of
the real physics.
Examples:
• Shadows
• Reflection
• Refraction
• Caustics
• ...
1. Render the scene with the light source as the view point and create a depth image
(shadow map) that encodes the distance to the light source.
scene without shadows depth image with the light source as view point
In video games etc. a multitude of different techniques are used that achieves the
desired compromise between the different aspects (empirical realism, performance, type
of light source).
The acquired result look very compelling, the methods, however, are equally complex.
The computed ambient occlusion is multiplied with the direct illumination in the
fragment shader. More details in Chapter 11.
The overdraw factor measures how many fragments fall into one pixel averaged over the
whole image.
A large overdraw factor indicates high costs for to shader evaluation and memory
bandwidth (color/z-buffer)
Computer Graphics – Rasterization Pipelines– Rendering Techniques 10–57
For deferred rendering (also deferred shading) different buffers holding the relevant
information for the shading are created.
Since the amount of information inside the g-buffer exceeds the capacity of a
RGBA-buffer, multiple images have to be created simultaneously. For this, multiple render
targets (MRT) are used.
final image
The volume hit by the light (represented by the wire frame) is painted into the scene. The
resulting fragments are shaded with the light source.
Common engines:
• Unreal Engine
• Source Engine
• C4 Engine
• Unity
• ...
Besides the actual rendering, these engines support tasks like the loading of so-called
assets (meshes, textures,. . . )
• the general operation of rasterization pipelines and their individual stages (vertex
stage, rasterization, fragment stage, output stage),
• fundamental programming of such pipelines using the OpenGL API,
• concepts of shader-based rendering and shader programming, such as variable
types (uniforms, attributes, varyings),
• depth testing and stencil testing for correct visibility and advanced rendering,
• blending for transparency rendering,
• some techniques to achieve high-quality rendering including global illumination
aspects in rasterization pipelines.
In the next chapter, we will discuss mapping techniques, which allows us to describe
complex surface appearance easily.