rendering - CNR

20
Marco Tarini - Video Game Dev - Univ Insubria 2013 13/01/2014 1 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)

Transcript of rendering - CNR

Page 1: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

1

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)

Page 2: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

2

Rendering

Scena 3D Immaginerendering

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

Page 3: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

3

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)

Page 4: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

4

Hardware specializzato per ilrendering

� "GPU": � Graphics Processing Unit

� La CPU della scheda video

� Instruction Set specializzato!

� Architettura a pipeline� a "catena di montaggio"

� Modello di computazione SIMD� sfrutta l'alto grado di parallelismo insito nel problema

� Possiede la propria memoria 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

Page 5: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

5

Schema (semplificato)

9

BUS

CPUALU

(central)RAM

Disk

Scheda video

…bus internobus interno(scheda video)

RAM(sch. video)

GPU

Il triangolo

x

y z

v0 =( x0, y0, z0 )

v1 =( x1, y1, z1 )

v2 =( x2, y2, z2 )

Page 6: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

6

Rasterization-Based Rendering

vertici3D

fragmentfragmentfragmentfragmentprocessprocessprocessprocess

pixelspixelspixelspixelsfinalifinalifinalifinali

"frammenti"(fragments)

transformtransformtransformtransform

z x

v0v1

v2

rasterizerrasterizerrasterizerrasterizer

y

triangolo 2Da schermo(2D screen

triangle)

v0v1

v2

11

Rasterization-Based Rendering(“transform and lighting”)

vertici3D

fragmentfragmentfragmentfragmentprocessprocessprocessprocess

pixelspixelspixelspixelsfinalifinalifinalifinali

"frammenti"(fragments)

transformtransformtransformtransform

z x

v0v1

v2

rasterizerrasterizerrasterizerrasterizer

y

triangolo 2Da schermo(2D screen

triangle)

v0v1

v2

carico di lavoro

per vertice

(sottosistema geometrico)

carico di lavoro

per frammento (~per pixel)

(sottosistema raster)12

Page 7: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

7

... Rasterization-Based Rendering

� Dove è il collo di bottiglia?� nel sistema geometrico?

� (l'applicaizone è transofrm-limitedsinonimo: 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

fram

menti

(candid

ati p

ixels

)

Vert

ici

(punti in R

3)

pixel

finali(nello

screen-buffer)

Vert

ici

pro

iettati

(punti in R

2)

Z co

mp

uta

zio

ni

pe

r ve

rtic

e

rasterizer

co

mp

uta

zio

ni

pe

r fr

am

me

nto

set-

up

componenti fisiche dell'HW!Pipeline → Parallelismo → Efficienza

inoltre, molte componenti sono replicate(negli stages collo di bottiglia)

Fragment proces.Vertex porcessor

14

Page 8: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

8

fram

menti

(candid

ati p

ixels

)

Vert

ici

(punti in R

3)

pixel

finali(nello

screen-buffer)V

ert

ici

pro

iettati

(punti in R

2)

Z co

mp

uta

zio

ni

pe

r ve

rtic

e

rasterizer

co

mp

uta

zio

ni

pe

r fr

am

me

nto

z

y

x

v0v1

v2

set-

up

v0v1

v2

Rasterizzare triangoli

fram

menti

(candid

ati p

ixels

)

3 V

ert

ici

(punti in R

3)

pixel

finali(nello

screen-buffer)3 V

ert

ici

pro

iettati

(punti in R

2)

Z co

mp

uta

zio

ni

pe

r ve

rtic

e

rasterizer

triangoli

co

mp

uta

zio

ni

pe

r fr

am

me

nto

z

y

x

v0v1

v2

set-

up

v0v1

v2

Page 9: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

9

Rasterizzare Segmenti

fram

menti

(candid

ati p

ixels

)

2 V

ert

ici

(punti in R

3)

pixel

finali(nello

screen-buffer)2 V

ert

ici

pro

iettati

(punti in R

2)

Z co

mp

uta

zio

ni

pe

r ve

rtic

e

rasterizer

triangoli

co

mp

uta

zio

ni

pe

r fr

am

me

nto

z

y

x

v0

v1

set-

up

v0

v1

rasterizer

segmenti

set-

up

O anche punti

fram

menti

(candid

ati p

ixels

)

Vert

ice

(punto

in R

3)

pixel

finali(nello

screen-buffer)Vert

ice

pro

iettato

(punto

in R

2)

Z co

mp

uta

zio

ni

pe

r ve

rtic

e

rasterizer

triangoli

co

mp

uta

zio

ni

pe

r fr

am

me

nto

z

y

x

v1

set-

up

v1

rasterizer

segmenti

set-

up

rasterizer

punti

set-

up

esempio di point "splat"(point splatting)

Page 10: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

10

1. Ogni vertice viene trasformato

� proiettato da spazio 3D (spazio “oggetto”) a spazio 2D (spazio “schermo”)

� indipendentemente dagli altri vertici

� (deve poter avvenire in parallelo!)

� indipendentemente da quale primitiva fa parte

2. Ogni primitiva viene rasterizzata in 2D

� primitiva = triangolo, segmento, o punto

� rasterizzatore distinti per ogni tipo di primitiva

� indipendentemente dalle altre primitive

� rasterizzare = produrre i frammenti corrispondenti

3. Ogni frammento in pos [X,Y] viene processato

� indipendentemente dagli altri frammenti

� indipendentemente da 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 p

ara

llelo

(in

ca

sca

ta)

(in

pip

elin

e)

(a c

ate

na

dim

on

tag

gio

)

Programmable HW

Fram

men

ti&

attri

buti

& at

tribu

ti &

attri

buti

& at

tribu

ti in

terp

olat

iin

terp

olat

iin

terp

olat

iin

terp

olat

i

Verti

ci&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i

Screen Screen Screen Screen bufferbufferbufferbuffer

Verti

ci p

orie

ttati

& at

tribu

ti &

attri

buti

& at

tribu

ti &

attri

buti

com

puta

tico

mpu

tati

com

puta

tico

mpu

tati

rasterizer triangoli

set-up

rasterizersegmenti

set-up

rasterizerpunti

set-up

PROGRAMMABILITA' ! ! !

com

puta

zion

ipe

r ver

tice

com

puta

zion

ipe

r fra

mm

ento

Page 11: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

11

Programmable HW

Fram

men

ti&

attri

buti

& at

tribu

ti &

attri

buti

& at

tribu

ti in

terp

olat

iin

terp

olat

iin

terp

olat

iin

terp

olat

i

Verti

ci&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i

Screen Screen Screen Screen bufferbufferbufferbuffer

Verti

ci p

orie

ttati

& at

tribu

ti &

attri

buti

& at

tribu

ti &

attri

buti

com

puta

tico

mpu

tati

com

puta

tico

mpu

tati

rasterizer triangoli

set-up

rasterizersegmenti

set-up

rasterizerpunti

set-up

com

puta

zion

ipe

r ver

tice

com

puta

zion

ipe

r fra

mm

ento

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

Page 12: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

12

Linguaggi di shading

� Alto livello:

� HLSL (High Level Shader Language, Direct3D, Microsoft)

� GLSL (OpenGL Shading Language)

� CG (C for Graphics, NVidia)

� Basso livello

� ARB Shader 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

Fram

men

ti&

attri

buti

inte

rpol

ati

Verti

ci&

loro

attr

ibut

i Screen buffer

Verti

ci&

attri

buti

com

puta

ti

rasterizer triangoli

set-up

rasterizersegmenti

set-up

rasterizerpunti

set-up

com

puta

zion

ipe

r ver

tice

Depth Depth Depth Depth bufferbufferbufferbufferco

mpu

tazi

oni

per f

ram

men

to

Transform.Metti la z

finale comeattributo

aggiuntivo

Interpola la z

(come tutti 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

Page 13: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

13

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 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 6363 63 63 63 63 63 63 63

5 5

5

5 5 5 5 5 5 5 635 5 5 5 5 5 63 635 5 5 5 5 63 63 635 5 5 5 63 63 63 635 5 5 63 63 63 63 635 5 63 63 63 63 63 635 63 63 63 63 63 63 6363 63 63 63 63 63 63 63

+ =

5 5 5 5 5 5 5 635 5 5 5 5 5 63 635 5 5 5 5 63 63 635 5 5 5 63 63 63 635 5 5 63 63 63 63 635 5 63 63 63 63 63 635 63 63 63 63 63 63 6363 63 63 63 63 63 63 63

7

2 7

5 5 5 5 5 5 5 635 5 5 5 5 5 63 635 5 5 5 5 63 63 635 5 5 5 63 63 63 634 5 5 7 63 63 63 633 4 5 6 7 63 63 632 3 4 5 6 7 63 6363 63 63 63 63 63 63 63

+ =

5 55 55 55 55 55 55

5 5 5 5 5 5 55 5 5 5 5 55 5 5 5 55 5 5 55 5 55 55

76 75 74 73 72 7

76 75 6 74 5 6 73 4 5 6 72 3 4 5 6 7

Page 14: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

14

Alpha Blending

� Semitrasparenze

Fram

men

ti&

attri

buti

& at

tribu

ti &

attri

buti

& at

tribu

ti in

terp

olat

iin

terp

olat

iin

terp

olat

iin

terp

olat

i

Verti

ci&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i&

loro

attr

ibut

i

Screen buffer

Verti

ci p

orie

ttati

& at

tribu

ti &

attri

buti

& at

tribu

ti &

attri

buti

com

puta

tico

mpu

tati

com

puta

tico

mpu

tati

rasterizer triangoli

set-up

rasterizersegmenti

set-up

rasterizerpunti

set-up

com

puta

zion

ipe

r ver

tice

com

puta

zion

ipe

r fra

mm

ento

Alpha Blending

� I colori hanno 4 componenti:

� R,G,B, αααα

� Dato un frammento

� (che sopravviva al depth test)

invece di sovrascriverlo,

uso la formula

Fram

men

ti&

attri

buti

& at

tribu

ti &

attri

buti

& at

tribu

ti in

terp

olat

iin

terp

olat

iin

terp

olat

iin

terp

olat

i

Screen buffer

com

puta

zion

ipe

r fra

mm

ento

)(),,()1(),,(),,( αα ⋅+−⋅= nuovovecchiofinale bgrbgrbgr

"alpha blending"

Page 15: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

15

Alpha Blending

Fram

men

ti&

attri

buti

& at

tribu

ti &

attri

buti

& at

tribu

ti in

terp

olat

iin

terp

olat

iin

terp

olat

iin

terp

olat

i

Screen buffer

com

puta

zion

ipe

r fra

mm

ento

� 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

v0v1

v2

world Coordinates

1

1) transformazione di vista

2) transformazione di proiezione

3) transformazione di viewport

2y

-zv0

v1

v2

view Coordinates

(a.k.a. eye Coordinates)

y-x

-zv0

v1

v2

v0

v2

v1

v0v1

v2

screen Space

3

normalized projected

coordinates

1

-11

-1

x

z

y

x

v0

v1

v2

object Coordinates

0

0) transformazione di modellazione

Page 16: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

16

Trasformazione di proiezione: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

focaledistanza

focale

Page 17: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

17

Pin-hole camera

distanza

focale

-x

y

-z

image

plane

View Frustum

Page 18: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

18

Pin-hole camera: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)

Page 19: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

19

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

Page 20: rendering - CNR

Marco Tarini - Video Game Dev - Univ

Insubria 2013

13/01/2014

20

Shadowing: 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

OCCHIOLUCE

SHADOW

MAP

final

SCREEN

BUFFER