CS 6958 LECTURE 5 TM ANALYSIS SHADING January 20, 2014 - - PowerPoint PPT Presentation

cs 6958 lecture 5 tm analysis shading
SMART_READER_LITE
LIVE PREVIEW

CS 6958 LECTURE 5 TM ANALYSIS SHADING January 20, 2014 - - PowerPoint PPT Presentation

CS 6958 LECTURE 5 TM ANALYSIS SHADING January 20, 2014 Clarification Avoid global variables class/struct types will cause compiler to fail What I meant was global instances of objects You can definitely define and use


slide-1
SLIDE 1

CS 6958 LECTURE 5 TM ANALYSIS SHADING

January 20, 2014

slide-2
SLIDE 2

Clarification

¨ Avoid global variables ¨ class/struct types will cause compiler to fail

¤ What I meant was global instances of objects ¤ You can definitely define and use classes/structs

slide-3
SLIDE 3

Pass By Reference

¨ Do this whenever possible ¨ Copying arguments is slow

¤ Especially for large data types

¨ Passing by const reference keeps the original

data safe

slide-4
SLIDE 4

Issue Statistics

¨ Issue Rate:

¤ Want this as high as possible

¨ iCache conflicts

¤ Tracked separately from “resource” conflicts

¨ thread*cycles of resource conflicts

¤ FU conflicts, L1, L2, DRAM

¨ thread*cycles of data dependence

¤ Nothing you can do about this (for now) ¤ Includes RF conflicts

slide-5
SLIDE 5

Default Areas (square mm)

¨ FPADD

.003

¨ FPMIN

.00072

¨ FPCMP

.00072

¨ INTADD

.00066

¨ FPMUL

.0165

¨ INTMUL

.0117

¨ FPINV

.112

¨ CONV

.001814

¨ BLT

.00066

¨ BITWISE

.00066

slide-6
SLIDE 6

Instruction Caches

¨ Instruction caches are actually “double

pumped”

¤ Each bank can service 2 requests every cycle

¨ Is N banks as good as N caches?

¤ Is N caches reasonable?

¨ Is N banks > T threads useful?

slide-7
SLIDE 7

trax.hpp

¨ Some of the useful functions (more as we

need them)

¤ invsqrt(float f) ¤ sqrt(float f) ¤ min(float a, float b) ¤ max(float a, float b) ¤ GetXRes() ¤ GetYRes() ¤ GetFrameBuffer()

slide-8
SLIDE 8

Ray tracer design

The major components in a ray tracer are:

¨ Camera (Pixels to Rays) ¨ Objects (Rays to intersection info) ¨ Materials (Intersection info and light to color) ¨ Lights ¨ Background (Rays to Color) ¨ All together: a Scene

slide-9
SLIDE 9

Ray tracing algorithm

foreach frame foreach pixel foreach sample generate ray intersect ray with objects shade intersection point

slide-10
SLIDE 10

Starting simple

foreach frame Ignore for now foreach pixel Atomic Increment foreach sample Ignore for now generate ray Camera intersect ray with objects Spheres, more soon shade intersection point Material

slide-11
SLIDE 11

Foreach pixel

Row-major order Frameless rendering Tiled Parallel Progressive

slide-12
SLIDE 12

Atomic Increment

¨ atomicinc(0)

¤ Atomically increments global register 0 ¤ All threads have access to this register

for(int pix = atomicinc(0); pix < xres*yres; pix = atomicinc(0)) int i = pix / xres; int j = pix % xres;

¨ Compare to

for(int i=0; i < xres; i++) for(int j=0; j < yres; j++)

slide-13
SLIDE 13

Atomic Increment

for(int pix = atomicinc(0); pix < xres*yres; pix = atomicinc(0)) int i = pix / xres; int j = pix % xres;

Thread 1 Thread 2 1 2 3 6 4 7 5 Potential pixel assignments

slide-14
SLIDE 14

Cameras – Coming Soon

foreach frame foreach pixel foreach sample generate ray Camera

slide-15
SLIDE 15

Find Closest Object

foreach frame foreach pixel foreach sample generate ray intersect ray with objects Spheres, more soon

tnear

slide-16
SLIDE 16

Shading (can get very complex)

foreach frame foreach pixel foreach sample generate ray intersect ray with objects shade intersection point Pixel gets a color

slide-17
SLIDE 17

Shading

¨ Path tracing (and other global techniques)

¤ Consider light from all sources

¨ Starting simple

¤ Consider light from direct source(s) Light source

slide-18
SLIDE 18

Color Multiplication

¨ C1 * C2 =

<C1.R * C2.R, C1.G * C2.G, C1.B * C2.B>

¨ If white=<1,1,1>, red=<1,0,0>, green=<0,1,0>

¨ white * c = c ¨ red * green = black ¨ Remember, colors in range [0 .. 1]

¤ Can only get darker by reflecting off surfaces

slide-19
SLIDE 19

Direct Light (and thus shadows)

¨ If there is line of sight from hit point to light

source, add light’s contribution

¤ Pixel color += object color * light color

¨ Else it is in shadow

¤ Do nothing

L ! " − P ! " L ! " − P ! " P ! "

Bright Occluded

slide-20
SLIDE 20

Computing Direct Light

¨ First we need a vector from hit point to light

¤ P = hit point ¤ LPos = location of light ¤ L = LPos – P

Ray shadowRay(P , normalize(L)); intersect shadowRay with scene objects determine if anything blocking light

P ! " L ! "

P LPos

slide-21
SLIDE 21

Computing Direct Light

¨ We don’t care about intersections behind the light!

max_t = |L| ß before normalizing! If (! (hit && 0 < hit_t < max_t)) Pixel color += object color * light color

P ! " L ! "

L

slide-22
SLIDE 22

Computing Direct Light

¨ Sometimes we don’t need to cast a ray

¤ Hit surface is on opposite side of light ¤ Angle between normal and L > 90

n cosθ = N ·√ normalize(L) n N = surface normal direction

if(cosθ < 0) skip shadow ray

L ! "

P LPos N

slide-23
SLIDE 23

Flipped Normals

¨ Normals don’t always point the right way

¤ Depends on how N calculated

¤ θ = wrong angle!

¨ V = camera ray direction

if(V ·√ N > 0) N = -N

Always flip normals to be on same side as incoming ray

N !"

θ

V ! "

N V

  • N
slide-24
SLIDE 24

Shadows

slide-25
SLIDE 25

What’s Wrong Here?

slide-26
SLIDE 26

Numerical Precision

¨ Zoomed in: ideal

slide-27
SLIDE 27

Numerical Precision

¨ Zoomed in: reality

(numerical roundoff)

¨ Object casts shadow

  • n itself

False intersection point!

slide-28
SLIDE 28

Solution

¨ Offset shadow ray origin in normal direction

¤ P += N*epsilon ¤ epsilon = some small number

¨ Epsilon depends on scene (1e-3 .. 1e-6)

slide-29
SLIDE 29

Lambert’s Cosine Law

¨ Light reaching surface is proportional to projected

visible area: cos θ

¨ θ = angle between light and normal

slide-30
SLIDE 30

Lambertian shading

¨ Comes from a “rough” surface

(at microscopic level)

¨ Light that reaches the surface

is reflected equally in all directions

slide-31
SLIDE 31

Lambertian shading

¨ Color at surface: cosθ * lightColor

¤ cosθ = (N ·√ L)

¨ (where N and L are unit vectors)

N !" L ! "

θ L N

slide-32
SLIDE 32

Direct Light

slide-33
SLIDE 33

Ambient light

¨ With this mechanism, the light in a shadowed region

is 0 (black)

¨ To avoid this, use “ambient” lighting ¨ For each scene, define Kd, Ka

¤ Kd + Ka = 1

¨ Kd light comes from direct sources ¨ Ka light comes from “ambient” sources

slide-34
SLIDE 34

Ambient light

¨ Pixel color =

¤ objectColor * [(N ·√ L)*lightColor*Kd + ambientColor*Ka]

¨ Define some ambientColor for the scene

¤ Based on how bright scene is, background, etc… ¤ Artistic choice (since it is a hack)

slide-35
SLIDE 35

Direct + Ambient Light

¨ Ambient = <.6, .6, .6> ¨ Kd = .7 ¨ Ka = .3