karljohan lundin palmerius - weber.itn.liu.se

87
Real-time Rendering Karljohan Lundin Palmerius

Upload: others

Post on 26-Nov-2021

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Karljohan Lundin Palmerius - weber.itn.liu.se

Real­time Rendering

Karljohan Lundin Palmerius

Page 2: Karljohan Lundin Palmerius - weber.itn.liu.se

About Me● Karljohan Lundin Palmerius● Ph.D. in Scientific Visualization● Research on multi­modal visualization

Page 3: Karljohan Lundin Palmerius - weber.itn.liu.se

Course Contents● Real­time Rendering

– goals, conditions, concepts, algorithms● Parallel Processing

– architectures and programming● Graphics Hardware

– mobile graphics, shading languages● Self study course

Page 4: Karljohan Lundin Palmerius - weber.itn.liu.se

Course ContentsReal­time Rendering KJ

RtR cont. KJ

Parallel Architectures MC

Parallel Programming MC

GPU Shaders JJ

Mobile Graphics AH

GPU Shaders cont. JJ

Summary or Buffert KJ

Page 5: Karljohan Lundin Palmerius - weber.itn.liu.se

Course Contents● Laborations – deadline 07­10­19

– Parallel processing graphics– GLSL shader programming– Hand in report, code and executable– Individual or in pair

● Essay or Project – deadline 07­10­19– Project/essay suggestion/topic by 07­09­21– Individual or in pair

Page 6: Karljohan Lundin Palmerius - weber.itn.liu.se

Lecture Contents● What is Real­time rendering● Achieve speed and flexibility

– concepts, theories and algorithms– shadows, culling– basic cheating, advanced cheating

Page 7: Karljohan Lundin Palmerius - weber.itn.liu.se

Real­time Rendering● Computer graphics with a time­constraint

– “Real­time” is relative– Computer games, interactive visualization– Limited resources, time limits and other demands

● Concepts:– do as much as you can!– don't do more than you can!

Page 8: Karljohan Lundin Palmerius - weber.itn.liu.se

Real­time Rendering

Data

VisualizationProcesses

Interaction

Page 9: Karljohan Lundin Palmerius - weber.itn.liu.se

Issue 1● Do as much as you can!

Page 10: Karljohan Lundin Palmerius - weber.itn.liu.se

Do as much as you can!● Acceleration

– software acceleration● code optimization● parallelization

– hardware acceleration● dedicated hardware● hardware optimized graphics approach

Page 11: Karljohan Lundin Palmerius - weber.itn.liu.se

Software Acceleration● Code optimization

– algorithm complexity– inner loop optimization– caching / cache optimization

Page 12: Karljohan Lundin Palmerius - weber.itn.liu.se

Code Optimization● Algorithm complexity

– Simple operation many times● Keep down the number of times – ordo notation● c.f. radiosity vs gouraud shading

– Algorithm optimization

O N2O N

Page 13: Karljohan Lundin Palmerius - weber.itn.liu.se

Code Optimization● Inner loop optimization

– Simple operation many times● Keep the cost low

– Low­level optimization● LUT or replace cost intensive functions

– pow, arctan, sincos – Taylor Polynomials● CPU specific routines● Stall minimization● Manual assembly coding

Page 14: Karljohan Lundin Palmerius - weber.itn.liu.se

Code Optimization● caching / cache optimization

– Fit as much as possibleas close as possible to CPU

● memory coherency / block data● L1 ~ 16 kB L2 ~ 1 MB

RAM ~ 1 GB HD ~ 1 TB– advanced HPC

CPU

L1 Cache

L2 Cache

RAM

L3 Cache

Hard Drive

Page 15: Karljohan Lundin Palmerius - weber.itn.liu.se

Parallelization● Independent operation in parallel

– trivial● unsychronized read● screen space multiplex

– non­trivial● synchronized write● object space multiplex

● Work­load balancing

Page 16: Karljohan Lundin Palmerius - weber.itn.liu.se

Hardware Acceleration● Hardware optimized algorithms

– simple operations in parallel pipelines– polygon rasterization approach

● Specialized hardware– GPU, PPU– Hardware interface

● Driver● API: OpenGL, Direct3D, PhysX

Page 17: Karljohan Lundin Palmerius - weber.itn.liu.se

Graphics HardwareVertex Positions Vertex Connections

Vertex Operations

Rasterization

Textures

Primitives

Fragment Operations

Buffer

Primitives

Blending

Image Data

Page 18: Karljohan Lundin Palmerius - weber.itn.liu.se

Hardware Acceleration● General Purpose Hardware

– Multi­core x86● 2 x Quad core = 8 virtual CPUs● TILE64 = 64 virtual CPUs

– Field­Programmable Gate Array(FPGA)

– Cell Broadband Engine● 1 st Power Processing Element● 8 st Synergistic Processing Elements

Page 19: Karljohan Lundin Palmerius - weber.itn.liu.se

Hardware Acceleration● General Purpose Hardware

– Hardware Interface● Compiler / compiler extensions● Optimization tools

Page 20: Karljohan Lundin Palmerius - weber.itn.liu.se

Hardware Acceleration● Caveats

– Software distribution● unknown hardware support● resulting in disabled functions● driver may simulate features

– Hard to realize● design algorithms● implement in program

Page 21: Karljohan Lundin Palmerius - weber.itn.liu.se

Fast Algorithms● Fast lighting and shadows

Page 22: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadowing Algorithms● Global illumination (radiosity)

– really, really slow● Raytracing

– very slow● Hardware accelerated

– Shadow Volume, Shadow MappingShadow Texture, Etc...

Page 23: Karljohan Lundin Palmerius - weber.itn.liu.se

Global Illumination

Scene

Connectivity Radiosity Light Maps

O N2 O N2 logN

Lighting

Rendering

ON

Page 24: Karljohan Lundin Palmerius - weber.itn.liu.se

Global Illumination● Hierarchical radiosity

– group patches far away– group patches in early iterations

● (Quasi) Monte­Carlo radiosity● Trans­illumination planes

– intermediate transspatial radiosity map– can be hardware accelerated

Page 25: Karljohan Lundin Palmerius - weber.itn.liu.se

Raytracing● for each buffer row

for each row pixelfor each object

check for intersection

.=O W⋅H⋅N

Page 26: Karljohan Lundin Palmerius - weber.itn.liu.se

Raytracing● Speed­up

– image­space (trivial) parallelization– collision search speed­up– frame­to­frame coherency

● Close to interactive with current hardware

Page 27: Karljohan Lundin Palmerius - weber.itn.liu.se

Projected Planar Shadows● Only planar surfaces

● Project object and colour black

x⋅n=d

p'=xL−dn⋅xL

n⋅p−xLp−xL

Page 28: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Volumes● For each light source

–  polygon shadow volume● Multi­pass render

1) render all in shadow2) render shadow polygons

– to mask buffer3) render lighting masked

.=O N!

Page 29: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Volumesgenerate shadow polygons

render scene with ambient and emission lighting

for each front face shadow polygon

if Z­buffer test passesthen increment stencil buffer value

for each shadow polygon back face

if Z­buffer test passesthen decrement stencil buffer value

render lit scene with stencil buffer mask

Page 30: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Volumes● Shadow volume estimation

– computationally expensive– dependent on scene complexity

Page 31: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Mapping● Completely image­space algorithm!

1) Render scene from light source ­> depth map2) Render scene from viewpoint (use map)– check each fragment against depth buffer from light

.=ON

Page 32: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Mappingfor each light

render scene to light's depth buffer

for each fragment

transform fragment into each light's buffer space

if fragment depth < light buffer depththen render fragment lit by lightelse render fragment shadowed

Page 33: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Mapping● Precision limit artifacts

– depth buffer precision is limited=> self shadowing

– solution● substract bias from Z­value● glPolygonOffset

Page 34: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Mapping● Resolution mismatch artefacts

– depth fragment size in image space

– solutions● percentage closer filtering● perspective shadow maps

d=d s

r sr i

coscos

r s

r i

Page 35: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Mapping

Page 36: Karljohan Lundin Palmerius - weber.itn.liu.se

Shadow Texture● Hack!

– texture shadow onto object

glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

Page 37: Karljohan Lundin Palmerius - weber.itn.liu.se

Issue 2● Don't do more than you can!

Page 38: Karljohan Lundin Palmerius - weber.itn.liu.se

Don't do more than you can!● Frame­rate maintenance

– Remove unnecessary work– Flexibility

● adjust rendering to fit conditions● graphics hardware, memory, CPU

– Degrading● use graceful degradation● adaptive or pre­determined degradation

Page 39: Karljohan Lundin Palmerius - weber.itn.liu.se

Prepare Your Data● Pre processing

– prepare and organize● hierarchical scenes● data structures

– when?● compile­time● initialization­time● run­time

Design

Compilation

Initialization

Run­time

Page 40: Karljohan Lundin Palmerius - weber.itn.liu.se

Culling● Remove objects that will not affect the scene

– behind the viewer (near/far plane)– outside view frustum– behind another object

Page 41: Karljohan Lundin Palmerius - weber.itn.liu.se

Culling● Bounding volumes

– cull groups with bounding spaces outside the view– bounding

● sphere● axis­aligned bounding box (AABB)● oriented bounding box (OBB)

– should be easy to work with– should have small void space

Page 42: Karljohan Lundin Palmerius - weber.itn.liu.se

Bounding Volumes

Page 43: Karljohan Lundin Palmerius - weber.itn.liu.se

Bounding Volume Culling

Page 44: Karljohan Lundin Palmerius - weber.itn.liu.se

Scene­graph● Hierarchical rendering● Groups● Local transformation

– wheel relative car– car relative earth– earth relative sun

Earth transform

Car transform

Wheel transforms

Wheel

Page 45: Karljohan Lundin Palmerius - weber.itn.liu.se

Hierarchical Bounding Volumes

Page 46: Karljohan Lundin Palmerius - weber.itn.liu.se

Scene Partitioning● Rooms are very nice

– limited contents– occluding walls– static relation to other rooms

● Intelligent door placement– automatic closing– cells­and­portals

Page 47: Karljohan Lundin Palmerius - weber.itn.liu.se

Scene Partitioning

Page 48: Karljohan Lundin Palmerius - weber.itn.liu.se

Scene Partitioning● Scene splitting benefits

– intelligent culling● visibility lists● real­time visibility tests

– scene pre­loading● objects and textures

– detail adjustments● physics simulation● artificial intelligence

Page 49: Karljohan Lundin Palmerius - weber.itn.liu.se

Automatic Partitioning● Binary Space Partitioning (BSP)

– recursive space division– results in hierarchy with convex leaves– results in “BSP tree”

● Breaking the Walls (BW)– two pass partitioning– supports outdoor and dynamic scenes

Page 50: Karljohan Lundin Palmerius - weber.itn.liu.se

BSP Algorithm

Page 51: Karljohan Lundin Palmerius - weber.itn.liu.se

BW Algorithm● First pass

– traverse edges (walls)● e.g. counter clockwise

– local greedy algorithm● choose shortest path● adjacent wall or portal

Page 52: Karljohan Lundin Palmerius - weber.itn.liu.se

BW Algorithm● Second pass

– merge cells with long portals– split over estimated cells

● cells that can be split● split at concave corners

Page 53: Karljohan Lundin Palmerius - weber.itn.liu.se

Partitioning Algorithms

Page 54: Karljohan Lundin Palmerius - weber.itn.liu.se

Scene Partitioning● Alternative partitioning

– merge BSP cells by visibility lists– regular grid blocks– quad­tree

Page 55: Karljohan Lundin Palmerius - weber.itn.liu.se

Cheating

Page 56: Karljohan Lundin Palmerius - weber.itn.liu.se

Cheating● Don't use more effort than necessary

– psychophysical aspects (perception)● speed, contrast, (eccentricity)● fog, (depth­of­field)

– projected size

Page 57: Karljohan Lundin Palmerius - weber.itn.liu.se

Cheating

Page 58: Karljohan Lundin Palmerius - weber.itn.liu.se

Cheating● Object impact

– speed, contrast, projected size– use bounding volume projected size– analyze pre­rendered image of object

● Simplifications– polygonal resolution– texturing– shaders

Page 59: Karljohan Lundin Palmerius - weber.itn.liu.se

Object Resolution● Level­of­detail (LoD)

– use as low resolution as possible– select resolution at render­time

Page 60: Karljohan Lundin Palmerius - weber.itn.liu.se

Textures● Replace geometrical details with texture

– clothes, faces and ground

Page 61: Karljohan Lundin Palmerius - weber.itn.liu.se

Textures● Image­based rendering

– Use photographs of things● Lighting● Material and colours● Textures (microstructure)

Page 62: Karljohan Lundin Palmerius - weber.itn.liu.se

Fragment Shaders● Hardware accelerated details

– each fragment individually estimated– real­time updated texturing– bumpiness, details, etc.– relief texturing

Page 63: Karljohan Lundin Palmerius - weber.itn.liu.se

Sprites and Impostors● Sprite

– often billboard – polygon facing the camera– image/photograph/video instead of object– commonly plants, grass, people

● Impostors– replace geometry with image

● pre­rendered / dynamically generated

Page 64: Karljohan Lundin Palmerius - weber.itn.liu.se

Adaptive Degradation● Adjust degradation at run­time

– Pre­determine degradation● time buffer● priority queue

– depending on distance, size, velocity● buy quality for each object

– Dynamic degradation● adjust quality if frame­rate is high or low● hysteresis

Page 65: Karljohan Lundin Palmerius - weber.itn.liu.se

Level­of­Details

Page 66: Karljohan Lundin Palmerius - weber.itn.liu.se

Level­of­Details● Adaptive cheating

– select model resolution at run­time– usually distance specific

● Issues– generating detail levels– error estimation / level selection– pop / pop­up artifacts– resolution mis­match

Page 67: Karljohan Lundin Palmerius - weber.itn.liu.se

Pop Artifacts● Too large of a change

– object morphing – change in shape– creeping texture– popping attracts eye

● Remidy– gradual change is invisible– change at sub visible size (resolution)

Page 68: Karljohan Lundin Palmerius - weber.itn.liu.se

Resolution Mis­match● Part of different resolution don't match

– Texture resolution change

– Polygon patches

Page 69: Karljohan Lundin Palmerius - weber.itn.liu.se

MIP­map● Texture resolution issues

– Aliasing at distance – moiré patterns– Unnecessarily high resolution– Too low resolution

● Solution– Multum In Parvo (MIP map)– Multiple texture resolutions– Hardware support in OpenGL

Page 70: Karljohan Lundin Palmerius - weber.itn.liu.se

MIP­map

Page 71: Karljohan Lundin Palmerius - weber.itn.liu.se

MIP­map

Page 72: Karljohan Lundin Palmerius - weber.itn.liu.se

Clip­map● Clipped MIP­map (SGI)

– Full texture at low resolution– Only partial texture at high resolution

Page 73: Karljohan Lundin Palmerius - weber.itn.liu.se

LoD Polygonal Objects● Multiple resolution versions

– Generate multiple resolutions– Select right resolution

Decimation

Decimation

Decimation

Switch

Page 74: Karljohan Lundin Palmerius - weber.itn.liu.se

Decimation

Page 75: Karljohan Lundin Palmerius - weber.itn.liu.se

Decimation● Vertex clustering

– Cluster vertices using regular grid● Replace cluster with single vertex● Update faces accordingly

– Poor result● low quality● no triangle count specification● no error measure

Page 76: Karljohan Lundin Palmerius - weber.itn.liu.se

75 ~28%

Decimation● Remove vertices to simplify model

– Vertex removal, edge contraction

– Remove vertex/edge until● exceeding error threshold● reaching triangle count

108 ~20%

Page 77: Karljohan Lundin Palmerius - weber.itn.liu.se

Quadric Error Metric● “Quadric Decimation”

– Edge / non­edge contraction– Associate vertex with error quadric Q

– Use heap with vertices and costs

v =vTQv

Q1,2=Q1Q2

cost=v 'T Q1Q2 v '

v '=Q1,2−1 0,0,0,1

T

Page 78: Karljohan Lundin Palmerius - weber.itn.liu.se

Quadric Error Metric● Initial Quadric Error

– approximate plane distance square error

v = ∑p∈planes v

pT v 2

= ∑p∈planes v

vT p pT v

= ∑p∈planes v

vT p pT v

=vT

∑p∈planes v

p pT

v

⇒Q= ∑p∈planes v

p pT

Page 79: Karljohan Lundin Palmerius - weber.itn.liu.se

Adaptive Decimation● What if different parts are at different distance?

– Parts of same object requires different resolution– MIP­mapping for textures– Adaptive decimation for polygonal data

● e.g. ground meshes

Page 80: Karljohan Lundin Palmerius - weber.itn.liu.se

Adaptive Decimation● Alternatives

– Patches– Adaptive decimation

● ROAM● SOAR

● Criteria– distance, frustum culling,

decimation error, line­of­sight

Page 81: Karljohan Lundin Palmerius - weber.itn.liu.se

ROAM● Real­time Optimally Adapting Meshes

– Triangle bin­tree– Time­dependent priority– Priority queues

● Incremental changes● Split● Merge

Page 82: Karljohan Lundin Palmerius - weber.itn.liu.se

ROAM● Forced split

Page 83: Karljohan Lundin Palmerius - weber.itn.liu.se

ROAM

Page 84: Karljohan Lundin Palmerius - weber.itn.liu.se

ROAM● Error Metrics

– Nested wedges – hierarchical error bound– Screen distortions – project wedge on screen– Check line­of­sight

● intersection with wedge – modify triangle priority– View frustum, backface detail reduction,

specular highlights, silhouette edges,atmospheric obscurance, object positioning

Page 85: Karljohan Lundin Palmerius - weber.itn.liu.se

SOAR● Stateless, One­pass Adaptive Refinement

– Longest edge intersection, 4­k mesh, ...– Multiresolution mesh forms DAG of vertices

● children belong to higher resolution level● nested error terms ensure active parent of active vertex

Page 86: Karljohan Lundin Palmerius - weber.itn.liu.se

SOAR● Pros

– Simple to implement– No forced split– High memory coherency

● Cons– No frame­to­frame coherency

Page 87: Karljohan Lundin Palmerius - weber.itn.liu.se

The end