Rendering
(recall?)
Game Engine
Parte del game che si occupa di alcuni dei task “comuni”
Scena / livello Renderer
Real time transofrm + lighting Models, materials … Physics engine
(soft real-time) newtonian physical simulations Collision detection + response
Networking
(LAN – es tramite UTP)
Sound mixer e “sound-renderer”
Gestore unificato HCI devices
Main event loop, timers, windows manager…
Memory management Artificial intelligence module
Soluz dei sotto task comuni AI Supporto alla localizzazione Scripting
GUI (HUD)
Rendering
Scena 3D
renderingImmagine
descritto da un insieme di primitive
screen buffer ( array 2D di pixel )
Real Time 3D Rendering
Task molto oneroso
ma, "embarrassingly parallel"
Ingrediente base della soluzione:
hardware specializzato
Rendering nei games
Real time
20 o 30 o 60 FPS
Hardware based
Pipelined, stream processing
Complessità:
Lineare col numero di primitive
Real Time 3D Rendering:
API
OpenGL
(gruppo Khronos)
DirectX
(Microsoft)
rendering
"GPU":
Graphics Processing Unit La CPU della scheda video Instruction Set specializzato!
Architettura a pipeline
a "catena di montaggio"
Modello di computazioneSIMD
sfrutta l'alto grado di parallelismo insito nel problema
Possiede la propriamemoria RAM a bordo
"RAM CPU" vs "RAM GPU"
grandi copie di memoria da una all'altra dispendiose7
Hardware specializzato per il rendering
potenza di calcolo
migliaia di GFlops!
bus molto performante
e.g. PCI-express:~16 GB/s
8
Schema (semplificato)
BUS
9CPU
ALU
(central)
RAM
Disk Scheda video
…
bus interno bus interno
(scheda video) (sch. video)RAM GPU
Il triangolo
x
y z
v
0=( x
0, y
0, z
0)
v
1=( x
1, y
1, z
1)
v
2=( x
2, y
2, z
2)
Rasterization-Based Rendering
vertici 3D
fragment fragment fragment fragment process process process
process pixelspixelspixelspixels finali finali finali finali
"frammenti"
(fragments) transform
transform transform transform
z x
v0 v1
v2
rasterizer rasterizerrasterizer rasterizer y
triangolo 2D a schermo
(2D screen triangle) v0 v1
v2
11
Rasterization-Based Rendering (“transform and lighting”)
vertici 3D
fragment fragment fragment fragment process process process
process pixelspixelspixelspixels finali finali finali finali
"frammenti"
(fragments) transform
transform transform transform
z x
v0 v1
v2
rasterizer rasterizerrasterizer rasterizer y
triangolo 2D a schermo
(2D screen triangle) v0 v1
v2
carico di lavoro per vertice
(sottosistema geometrico)
carico di lavoro per frammento (~per pixel)
(sottosistema raster)
12
... Rasterization-Based Rendering
Dove è il collo di bottiglia?
nel sistema geometrico?
(l'applicaizone è transofrm-limited sinonimo: geometry-limited) nel sistema raster?
(applicaizone è fill-limited) nel bus?
(applicaizone è bus-limited sin: bandwidth-limited) nella CPU?
(applicaizone è CPU-limited)
perché è importante scoprirlo?
come si può predirre (in teoria)?
come si può verificare in pratica?
HW support
frammenti (candidati pixels) Vertici (punti in R3)
pixel finali
(nello screen-buffer)
Vertici proiettati (punti in R2)
Zziciontaupmo per vertice
rasterizer
computazioni per frammento set-
up
componenti fisiche dell'HW!
Pipeline → Parallelismo → Efficienza
inoltre, molte componenti sono replicate (negli stages collo di bottiglia)
Fragment proces.
Vertex porcessor
14
frammenti (candidati pixels) Vertici (punti in R3)
pixel finali
(nello screen-buffer)
Vertici proiettati (punti in R2)
Zziciontaupmo per vertice
rasterizer
computazioni per frammento
z
y
x v0 v1
v2
set- up
v0 v1
v2
Rasterizzare triangoli
frammenti (candidati pixels) 3 Vertici (punti in R3)
pixel finali
(nello screen-buffer)
3 Vertici proiettati (punti in R2)
Zziciontaupmo per vertice
rasterizer triangoli
computazioni per frammento
z
y
x v0 v1
v2
set- up
v0 v1
v2
Rasterizzare Segmenti
frammenti (candidati pixels) 2 Vertici (punti in R3)
pixel finali
(nello screen-buffer)
2 Vertici proiettati (punti in R2)
Zziciontaupmo per vertice
rasterizer triangoli
computazioni per frammento
z
y
x v0
v1
set- up
v0
v1
rasterizer segmenti set-
up
O anche punti
frammenti (candidati pixels) Vertice (punto in R3)
pixel finali
(nello screen-buffer)
Vertice proiettato (punto in R2)
Zziciontaupmo per vertice
rasterizer triangoli
computazioni per frammento
z
y
x v1
set- up
v1
rasterizer segmenti set-
up
rasterizer punti set-
up
esempio di point "splat"
(point splatting)
1. Ogni vertice vienetrasformato
proiettato da spazio 3D (spazio “oggetto”) a spazio 2D (spazio “schermo”)
indipendentementedagli altri vertici (deve poter avvenire in parallelo!)
indipendentementeda quale primitiva fa parte
2. Ogni primitiva vienerasterizzata in 2D primitiva = triangolo, segmento, o punto
rasterizzatore distinti per ogni tipo di primitiva indipendentementedalle altre primitive
rasterizzare = produrre i frammenti corrispondenti
3. Ogni frammento in pos [X,Y] viene processato indipendentementedagli altri frammenti
indipendentementeda quale primitiva lo ha generato output della computazione: un pixel nello screen buffer (RGB)
quello a pos [X,Y] (prefissata, la computazione decide solo RGB, non X,Y)
in parallelo (in cascata) (in pipeline) (a catena dimontaggio)
Programmable HW
Frammenti & attributi & attributi & attributi & attributi interpolatiinterpolatiinterpolatiinterpolati Vertici & loro attributi& loro attributi& loro attributi& loro attributi
Screen Screen Screen Screen buffer bufferbuffer buffer Vertici poriettati & attributi & attributi & attributi & attributi computaticomputaticomputaticomputati
rasterizer triangoli set-up
rasterizer segmenti set-up
rasterizer punti set-up
PROGRAMMABILITA' ! ! !
computazioni per vertice computazioni per frammento
Programmable HW
Frammenti & attributi & attributi & attributi & attributi interpolatiinterpolatiinterpolatiinterpolati Vertici & loro attributi& loro attributi& loro attributi& loro attributi
Screen Screen Screen Screen buffer bufferbuffer buffer Vertici poriettati & attributi & attributi & attributi & attributi computaticomputaticomputaticomputati
rasterizer triangoli set-up
rasterizer segmenti set-up
rasterizer punti set-up
computazioni per vertice computazioni per frammento
Qui agisce il nostro
"Vertex Program"
(anche detto vertex shader) arbitrario
Qui agisce il nostro
"Fragment Program"
(anche detto fragment shader) arbitrario
Rasterization based rendering:
schema base
Per vertice: (vertex shader)
transform(da spazio oggetto a spazio schermo)
Per primitiva: (rasterizer)
rasterizzazione
interpolazione dati prodotti per vertice
Per frammento: (fragment shader)
lighting(da normale + luci + materiale a RGB)
texturing alpha kill
Per frammento: (dopo il fragment shader)
depth test alpha blend
Linguaggi di shading
Alto livello:
HLSL(High Level Shader Language, Direct3D, Microsoft) GLSL(OpenGL Shading Language)
CG(C for Graphics, NVidia)
Basso livello
ARBShader Program (come un assembler)
HLSL e GLSL molto simili
CG più ad alto livello e pensato per utilizzare sia HLSL che GLSL
Algoritmo dello z-buffer
Frammenti & attributi interpolati
Vertici & loro attributi Screen
buffer
Vertici & attributi computati
rasterizer triangoli set-up
rasterizer segmenti set-up
rasterizer punti set-up
computazioni per vertice
Depth Depth Depth Depth buffer buffer buffer buffer
computazioni per frammento
Transform.
Metti la z finale come
attributo aggiuntivo
Interpola (come tutti la z gli attributi)
per un frammento con
screen coordinates (x,y), colore (r,g,b) e profondità z:
if ( z <= DepthBuffer[x,y] ) {
ScreenBuffer[x,y] = (r , g , b) ; DepthBuffer[x,y] = z ;
} else scarta (“discard”, “kill”) frammento depth test
Algoritmo dello z-buffer:
proprietà
“order independent” ☺!!!
Molto robusto
funziona anche su:
Eseguire un rendering = costruire un depth test (come effetto collaterale)
5 5 5 5 5 5 5 63 5 5 5 5 5 5 63 63 5 5 5 5 5 63 63 63 5 5 5 5 63 63 63 63 4 5 5 7 63 63 63 63 3 4 5 6 7 63 63 63 2 3 4 5 6 7 63 63 63 63 63 63 63 63 63 63
M a r c o T a r i n i ‧ C o m p u t e r G r a p h i c s ‧ 2 0 1 1 / 1 2
‧ U n i v e r s i t à d e l l ’ I n s u b r i a
Algoritmo dello z-buffer:
esempio
a 63)63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
5 5
5
5 5 5 5 5 5 5 63 5 5 5 5 5 5 63 63 5 5 5 5 5 63 63 63 5 5 5 5 63 63 63 63 5 5 5 63 63 63 63 63 5 5 63 63 63 63 63 63 5 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
+ =
5 5 5 5 5 5 5 63 5 5 5 5 5 5 63 63 5 5 5 5 5 63 63 63 5 5 5 5 63 63 63 63 5 5 5 63 63 63 63 63 5 5 63 63 63 63 63 63 5 63 63 63 63 63 63 63 63 63 63 63 63 63 63 63
7
2 7
5 5 5 5 5 5 5 63 5 5 5 5 5 5 63 63 5 5 5 5 5 63 63 63 5 5 5 5 63 63 63 63 4 5 5 7 63 63 63 63 3 4 5 6 7 63 63 63 2 3 4 5 6 7 63 63 63 63 63 63 63 63 63 63
+ =
5 5
5 5
5 5
5 5
5 5
5 5 5
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
7 6 7
5 7
4 7
3 7
2 7
7 6 7 5 6 7 4 5 6 7 3 4 5 6 7 2 3 4 5 6 7
Alpha Blending
Semitrasparenze
Frammenti & attributi & attributi & attributi & attributi interpolatiinterpolatiinterpolatiinterpolati Vertici & loro attributi& loro attributi& loro attributi& loro attributi
Screen buffer
Vertici poriettati & attributi & attributi & attributi & attributi computaticomputaticomputaticomputati
rasterizer triangoli set-up
rasterizer segmenti set-up
rasterizer punti set-up
computazioni per vertice computazioni per frammento
Alpha Blending
I colori hanno 4 componenti:
R,G,B, αααα
Dato un frammento
(che sopravviva al depth test)
invece di sovrascriverlo, uso la formula
Frammenti & attributi & attributi & attributi & attributi interpolatiinterpolatiinterpolatiinterpolati
Screen buffer
computazioni per frammento
) ( ) , , ( ) 1 ( )
, , ( )
, ,
( = ⋅ −
α
+ ⋅α
nuovo vecchio
finale r g b r g b
b g r
"alpha blending"
Alpha Blending
Frammenti & attributi & attributi & attributi & attributi interpolatiinterpolatiinterpolatiinterpolati
Screen buffer
computazioni per frammento
Il fragment shader dovrà dare in output:
…un colore RGB e…
…una profondità e…
un parametro alpha
la trasparenza di quel pixel
e’ la quarta componente del colore RGB α
Parte I: Transform
z
y
x v0 v1
v2
world Coordinates
1
1) transformazione di vista 2) transformazione di proiezione 3) transformazione di viewport
2
y
-z v0 v1
v2
view Coordinates (a.k.a. eye Coordinates)
y -x
-z v0
v1
v2
v0
v2 v1
v0 v1
v2 screen Space
3
normalized projected coordinates 1
1 -1
-1
x z
y
x v0
v1
v2
object Coordinates
0
0) transformazione di modellazione
deformazione prospettica
Come si svolge fisicamente il processo:
Occhio o macchina fotografica (stesso concetto):
lenti
CCD o pellicola
(2D screen buffer)
lenti
retina
(2D screen buffer)
distanza
focale distanza
focale
Pin-hole camera
distanza focale
-x y
-z image
plane
View Frustum
parametri
Estrinseci
posizione, orientamento
Intrinseci
Lunghezza focale,
(equivalentemente, angolo Field of View, FOV)
Distanze di near plane, far plane
Rendering parte II:
lighting base
Lighting locale
(vediamo dettagli prossima lezione)
Rendering parte III:
tecniche avanzate diffuse nei games
Shadowing
shadow volumes shadow mapping
Screen Space Ambient Occlusion
Camera lens effects
Flares
limited Depth Of Field
Motion blur
High Dynamic Range
Non Photorealistic Rendering
contours toon BRDF
Texture based techniques
Bumpmapping Parallax mapping
SSAO
DoF
HDR
NPR con PCF
shadow maps
Due renderings:
1: dal punto di vista della luce
tieni solo il depth buffer
2: della camera
usa il depth buffer precedente
per determinare luce/ombra
Shadow mapping
OCCHIO LUCE
SHADOW MAP
final SCREEN BUFFER