Real-Time Rendering (Echtzeitgraphik) - TU Wien · [email protected] . Walking down the...

40
Real-Time Rendering (Echtzeitgraphik) Michael Wimmer [email protected]

Transcript of Real-Time Rendering (Echtzeitgraphik) - TU Wien · [email protected] . Walking down the...

Page 1: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Real-Time Rendering (Echtzeitgraphik)

Michael Wimmer [email protected]

Page 2: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Walking down the graphics pipeline

Application Geometry Rasterizer

Page 3: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

What for?

Understanding the rendering pipeline is the key to real-time rendering!

Insights into how things work

Understanding algorithms

Insights into how fast things work

Performance

Vienna University of Technology 3

Page 4: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Simple Graphics Pipeline

Often found in text books

Will take a more detailed look into OpenGL

Vienna University of Technology 4

Application Geometry Rasterizer

Display

Page 5: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Nowadays, everything part of the pipeline is hardware accelerated

Fragment: “pixel”, but with additional info (alpha, depth, stencil, …)

Graphics Pipeline (pre DX10, OpenGL 2 )

Vienna University of Technology 5

Geom

etr

y

Raste

rizer

Driver

Geometry

Rasterization

Texture

Fragment

Display

Command

Application

CP

U

Page 6: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Fixed Function Pipeline – Dataflow View

Vienna University of Technology 6

on-chip cache memory

video memory

system

memory

rasterization

CPU

vertex

shading

(T&L)

triangle setup

fragment

shading

and

raster

operations

textures

frame buffer

geometry

commands

pre-TnL

cache

post-TnL cache

texture

cache

Page 7: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX10 /OpenGL 3.2 Evolution

Vienna University of Technology 7

Vertex

Shader

Geometry

Shader

Pixel

Shader

Input

Assembler

Setup/

Rasterization

Output

Merger

Stream

Out

Memory

Vertex

Buffer

Texture

Depth

Texture

Texture

Color

Index Buffer

Buffer Geom

etr

y

Raste

rizer

Driver

Geometry

Rasterization

Texture

Fragment

Display

Command

Application

CP

U

Page 8: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

OpenGL 3.0

OpenGL 2.x is not as capable as DirectX 10

But: New features are vendor specific extensions (geometry shaders, streams…)

GLSL a little more restrictive than HLSL (SM 3.0)

OpenGL 3.0 did not clean up this mess! OpenGL 2.1 + extensions

Geometry shaders are only an extension

New: depreciation mechanism

OpenGL 4.x New extensions

OpenGL ES compatibility!

Vienna University of Technology 8

Page 9: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX 11/OpenGL 4.0 Evolution

Vienna University of Technology 9

Vertex

Shader

Setup

Rasterizer

Output

Merger

Pixel

Shader Geometry

Shader

Texture Texture Render

Target

Depth

Stencil Texture

Stream

Buffer

Stream out

Memory

memory

programmable

fixed

Sampler Sampler Sampler

Constant Constant Constant

Vertex

Buffer

Input

Assembler

Index

Buffer

Tessellator Control Point

Shader

Texture

Sampler

Constant

Not the final place in the pipeline!!!

Page 10: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX 11

Tesselation

At unexpected position!

Compute Shaders

Multithreading

To reduce state change overhead

Dynamic shader linking

HDR texture compression

Many other features...

Vienna University of Technology 10

Page 11: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX 11 Pipeline

Vienna University of Technology 11

Page 12: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX 12/Vulkan/AMD Mantle/Apple Metal

Reduce driver overhad

Indirect drawing

Pipeline state objects

Command lists/bundles

Partly possible already in OpenGL 4.3+

Other features

Conservative rasterization (for culling)

New blend modes

Order-independent transparency

Vienna University of Technology 12

Page 13: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Application

Generate database (Scene description)

Usually only once

Load from disk

Build acceleration structures (hierarchy, …)

Simulation (Animation, AI, Physics)

Input event handlers

Modify data structures

Database traversal

Shaders (vertex,geometry,fragment)

Vienna University of Technology 13

Page 14: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Driver

Maintain graphics API state

Command interpretation/translation

Host commands GPU commands

Handle data transfer

Memory management

Emulation of missing hardware features

Usually huge overhead!

Significantly reduced in DX10

Vienna University of Technology 14

Page 15: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Geometry Stage

Vienna University of Technology 15

Command

Vertex Processing

Clipping

Perspective Division

Primitive Assembly

Culling

Tesselation

Geometry Shading

Page 16: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Command

Command buffering (!)

Command interpretation

Unpack and perform format conversion (“Input Assembler”)

Vienna University of Technology 16

glLoadIdentity( );

glMultMatrix( T );

glBegin( GL_TRIANGLE_STRIP );

glColor3f ( 0.0, 0.5, 0.0 );

glVertex3f( 0.0, 0.0, 0.0 );

glColor3f ( 0.5, 0.0, 0.0 );

glVertex3f( 1.0, 0.0, 0.0 );

glColor3f ( 0.0, 0.5, 0.0 );

glVertex3f( 0.0, 1.0, 0.0 );

glColor3f ( 0.5, 0.0, 0.0 );

glVertex3f( 1.0, 1.0, 0.0 );

glEnd( );

Color

Transformation matrixT

Page 17: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Vertex Processing

Transformation

Vienna University of Technology 17

Vertex Processing

v e r t e x

Modelview

Matrix

Projection

Matrix

Perspective

Division Viewport

Transform

Modelview

Modelview

Projection

l l l

object eye clip normalized

device window

Page 18: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Vertex Processing

Fixed function pipeline:

User has to provide matrices, the rest happens automatically

Programmable pipeline:

User has to provide matrices/other data to shader

Shader Code transforms vertex explicitly

We can do whatever we want with the vertex!

Usually a gl_ModelViewProjectionMatrix is provided

In GLSL-Shader : gl_Position = ftransform();

Vienna University of Technology 18

Page 19: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Vertex Processing

Lighting

Texture coordinate generation and/or transformation

Vertex shading for special effects

Vienna University of Technology 19

T

Object-space triangles Screen-space lit triangles

Page 20: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Tesselation

If just triangles, nothing needs to be done, otherwise:

Evaluation of polynomials for curved surfaces

Create vertices (tesselation)

DirectX11 specifies this in hardware!

3 new shader stages!!!

Still not trivial (special algorithms required)

Vienna University of Technology 20

Page 21: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

DirectX11 Tesselation

Vienna University of Technology 21

control shader evaluation shader

Page 22: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Tesselation Example

Vienna University of Technology 22

Optimally tesslated!

Page 23: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Geometry Shader

Calculations on a primitive (triangle)

Access to neighbor triangles

Limited output (1024 32-bit values)

No general tesselation!

Applications: Render to cubemap

Shadow volume generation

Triangle extension for ray tracing

Extrusion operations (fur rendering)

Vienna University of Technology 23

Page 24: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rest of Geometry Stage

Primitive assembly

Geometry shader

Clipping (in homogeneous coordinates)

Perspective division, viewport transform

Culling

Vienna University of Technology 24

Page 25: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rasterization Stage

Vienna University of Technology 25

Rasterization

Fragment

Processing

Raster Operations

Texture

Processing

Triangle Setup

Page 26: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rasterization

Setup (per-triangle)

Sampling (triangle = {fragments})

Interpolation (interpolate colors and coordinates)

Vienna University of Technology 26

Screen-space triangles Fragments

Page 27: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rasterization

Sampling inclusion determination

In tile order improves cache coherency

Tile sizes vendor/generation specific

Old graphics cards: 16x64

New: 4x4

Smaller tile size favors conditionals in shaders

All tile fragments calculated in parallel on modern hardware

Vienna University of Technology 27

Page 28: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rasterization – Coordinates

Fragments represent “future” pixels

Vienna University of Technology 28

0.0 1.0 2.0 3.0

0.0

1.0

2.0

3.0

x window coordinate

y windowcoordinate

Pixel(2,1)

Lower left cornerof the window

Pixel center at

(2.5, 1.5)!

Page 29: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Rasterization – Rules

Separate rule for each primitive

Non-ambiguous!

Polygons:

Pixel center contained in polygon

On-edge pixels: only one is rasterized

Vienna University of Technology 29

Page 30: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Texture

Texture “transformation” and projection

E.g., projective textures

Texture address calculation (programmable in shader)

Texture filtering

Vienna University of Technology 30

Fragments Texture Fragments

Page 31: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Fragment

Texture operations (combinations, modulations, animations etc.)

Vienna University of Technology 31

Fragments

Textured Fragments

Texture Fragments

Page 32: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Raster Tests

Ownership

Is pixel obscured by other window?

Scissor test

Only render to scissor rectangle

Depth test

Test according to z-buffer

Alpha test

Test according to alpha-value

Stencil test

Test according to stencil buffer

Vienna University of Technology 32

Textured Fragments Framebuffer Pixels

Page 33: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Raster Operations

Blending or compositing

Dithering

Logical operations

Vienna University of Technology 33

Textured Fragments Framebuffer Pixels

Page 34: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Raster Operations

ScissorTest

AlphaTest

StencilTest

DepthTest

Blending(RGBA only)

Dithering Logicop FrameBuffer

Stencil BufferDepth Buffer

Fragmentand

associateddata

PixelOwnership

Test

Vienna University of Technology 34

After fragment color calculation (“Output Merger”)

Page 35: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Display

Gamma correction

Digital to analog conversion if necessary

Vienna University of Technology 35

Framebuffer Pixels Light

Page 36: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Display

Frame buffer pixel format: RGBA vs. index (obsolete)

Bits: 16, 32, 128 bit floating point, …

Double buffered vs. single buffered

Quad-buffered for stereo

Overlays (extra bit planes) for GUI

Auxiliary buffers: alpha, stencil

Vienna University of Technology 36

Page 37: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Functionality vs. Frequency

Geometry processing = per-vertex

Transformation and Lighting (T&L)

Historically floating point, complex operations

Today: fully programmable flow control, texture lookup

20-1500 million vertices per second

Fragment processing = per-fragment

Blending and texture combination

Historically fixed point and limited operations

Up to 50 billion fragments (“Gigatexel”/sec)

Floating point, programmable complex operations

Vienna University of Technology 37

Page 38: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Application

Geometry

Rasterization

Texture

Fragment

Display

Command

Assume typical non-trivial fixed-function rendering task

1 light, texture coordinates, projective texture mapping

7 interpolants (z,r,g,b,s,t,q)

Trilinear filtering, texture-, color blending, depth buffering

Rough estimate:

Computational Requirements

Vienna University of Technology 38

ADD CMP MUL DIV

Vertex 102 30 108 5

Fragment 66 9 70 1

Page 39: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Communication Requirements

Vertex size:

Position x,y,z

Normal x,y,z

Texture coordinate s,t

8 4 = 32 bytes

Texture:

Color r,g,b,a, 4 bytes

Display:

Color r,g,b, 3 bytes

Fragment size (in frame buffer):

Color r,g,b,a

Depth z (assume 32 bit)

8 bytes, but goes both ways (because of blending!)

Vienna University of Technology 39

Page 40: Real-Time Rendering (Echtzeitgraphik) - TU Wien · wimmer@cg.tuwien.ac.at . Walking down the graphics pipeline ... Understanding the rendering pipeline is the key to real-time rendering!

Communication Requirements

Vienna University of Technology 40

Vertex 5 Gops

Fragment 150 Gops

Framebuffer

0.36 GB/s

1000 Mpix/s

20 Mvert/s

120 Mpix/s16 GB/s

4 GB/s

0.640 GB/s

Texture Memory

Application

Geometry

Rasterization

Texture

Fragment

Display

Command