Acceleration Digital Image Synthesis YungYu Chuang with slides

  • Slides: 131
Download presentation
Acceleration Digital Image Synthesis Yung-Yu Chuang with slides by Mario Costa Sousa, Gordon Stoll

Acceleration Digital Image Synthesis Yung-Yu Chuang with slides by Mario Costa Sousa, Gordon Stoll and Pat Hanrahan

Acceleration techniques

Acceleration techniques

Bounding volume hierarchy

Bounding volume hierarchy

Bounding volume hierarchy 1) Find bounding box of objects

Bounding volume hierarchy 1) Find bounding box of objects

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two groups

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two groups 3) Recurse

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two groups 3) Recurse

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two groups 3) Recurse

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two

Bounding volume hierarchy 1) Find bounding box of objects 2) Split objects into two groups 3) Recurse

Where to split? • At midpoint • Sort, and put half of the objects

Where to split? • At midpoint • Sort, and put half of the objects on each side • Use modeling hierarchy

BVH traversal • If hit parent, then check all children

BVH traversal • If hit parent, then check all children

BVH traversal • Don't return intersection immediately because the other subvolumes may have a

BVH traversal • Don't return intersection immediately because the other subvolumes may have a closer intersection

Bounding volume hierarchy

Bounding volume hierarchy

Bounding volume hierarchy

Bounding volume hierarchy

Space subdivision approaches Unifrom grid Quadtree (2 D) Octree (3 D)

Space subdivision approaches Unifrom grid Quadtree (2 D) Octree (3 D)

Space subdivision approaches KD tree BSP tree

Space subdivision approaches KD tree BSP tree

Uniform grid

Uniform grid

Uniform grid Preprocess scene 1. Find bounding box

Uniform grid Preprocess scene 1. Find bounding box

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place object in cell if its bounding box overlaps the cell

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place

Uniform grid Preprocess scene 1. Find bounding box 2. Determine grid resolution 3. Place object in cell if its bounding box overlaps the cell 4. Check that object overlaps cell (expensive!)

Uniform grid traversal Preprocess scene Traverse grid 3 D line = 3 D-DDA (Digital

Uniform grid traversal Preprocess scene Traverse grid 3 D line = 3 D-DDA (Digital Differential Analyzer) naive DDA

octree Octree

octree Octree

K-d tree A A Leaf nodes correspond to unique regions in space

K-d tree A A Leaf nodes correspond to unique regions in space

K-d tree A B A Leaf nodes correspond to unique regions in space

K-d tree A B A Leaf nodes correspond to unique regions in space

K-d tree A B B A Leaf nodes correspond to unique regions in space

K-d tree A B B A Leaf nodes correspond to unique regions in space

K-d tree A C B B A

K-d tree A C B B A

K-d tree A C B A

K-d tree A C B A

K-d tree A C D B C B A

K-d tree A C D B C B A

K-d tree A C D B C B D A

K-d tree A C D B C B D A

K-d tree A D B B C C D A Leaf nodes correspond to

K-d tree A D B B C C D A Leaf nodes correspond to unique regions in space

K-d tree traversal A D B B C C D A Leaf nodes correspond

K-d tree traversal A D B B C C D A Leaf nodes correspond to unique regions in space

BSP tree 6 5 9 7 10 8 1 11 2 4 3

BSP tree 6 5 9 7 10 8 1 11 2 4 3

BSP tree 6 5 1 inside ones 9 7 10 8 1 11 2

BSP tree 6 5 1 inside ones 9 7 10 8 1 11 2 4 3 outside ones

BSP tree 6 5 1 9 7 10 8 1 11 2 4 3

BSP tree 6 5 1 9 7 10 8 1 11 2 4 3 2 3 4 5 6 7 8 9 10 11

BSP tree 6 1 5 9 b 9 7 10 1 11 b 9

BSP tree 6 1 5 9 b 9 7 10 1 11 b 9 a 11 a 2 4 3 5 11 8 6 7 9 a 10 11 a 8 9 b 11 b

BSP tree 6 5 1 9 b 9 7 10 1 11 b 9

BSP tree 6 5 1 9 b 9 7 10 1 11 b 9 a 11 a 2 5 2 8 11 3 4 4 10 11 a 9 b 7 9 a 3 8 6 11 b

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a 9 a 11 a 2 9 b 8 11 3 4 4 3 5 2 10 11 a 9 b 7 9 a point 8 6 11 b

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a 9 a 11 a 2 9 b 8 11 3 4 4 3 5 2 10 11 a 9 b 7 9 a point 8 6 11 b

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a

BSP tree traversal 6 5 1 9 7 10 1 11 b 9 a 9 a 11 a 2 9 b 8 11 3 4 4 3 5 2 10 11 a 9 b 7 9 a point 8 6 11 b

Classes • Primitive (in core/primitive. *) – Geometric. Primitive – Instance. Primitive – Aggregate

Classes • Primitive (in core/primitive. *) – Geometric. Primitive – Instance. Primitive – Aggregate • Three types of accelerators are provided (in accelerators/*. cpp) – Grid. Accel – BVHAccel – Kd. Tree. Accel

Hierarchy Primitive Geometric Primitive Transformed Primitive Aggregate T Material Shape Support both Instancing and

Hierarchy Primitive Geometric Primitive Transformed Primitive Aggregate T Material Shape Support both Instancing and animation Store intersectable primitives, call Refine If necessary

Primitive class Primitive : public Reference. Counted { <Primitive interface> const int primitive. Id;

Primitive class Primitive : public Reference. Counted { <Primitive interface> const int primitive. Id; static int nextprimitive. Id; } class Transformed. Primitive: public Primitive { … Reference<Primitive> instance; }

Interface BBox World. Bound(); geometry bool Can. Intersect(); // update maxt bool Intersect(const Ray

Interface BBox World. Bound(); geometry bool Can. Intersect(); // update maxt bool Intersect(const Ray &r, Intersection *in); bool Intersect. P(const Ray &r); void Refine(vector<Reference<Primitive>> &refined); void Fully. Refine(vector<Reference<Primitive>> &refined); Area. Light *Get. Area. Light(); material BSDF *Get. BSDF(const Differential. Geometry &dg, Transform &World. To. Object); BSSRDF *Get. BSSRDF(Differential. Geometry &dg, Transform &World. To. Object);

Intersection struct Intersection { <Intersection interface> Differential. Geometry dg; const Primitive *primitive; Transform World.

Intersection struct Intersection { <Intersection interface> Differential. Geometry dg; const Primitive *primitive; Transform World. To. Object, Object. To. World; int shape. Id, primitive. Id; float ray. Epsilon; }; adaptively estimated primitive stores the actual intersecting primitive, hence Primitive->Get. Area. Light and Get. BSDF can only be called for Geometric. Primitive

Geometric. Primitive • represents a single shape • holds a reference to a Shape

Geometric. Primitive • represents a single shape • holds a reference to a Shape and its Material, and a pointer to an Area. Light Reference<Shape> shape; Reference<Material> material; // BRDF Area. Light *area. Light; // emittance • Most operations are forwarded to shape

Geometric. Primitive bool Intersect(Ray &r, Intersection *isect) { float thit, ray. Epsilon; if (!shape->Intersect(r,

Geometric. Primitive bool Intersect(Ray &r, Intersection *isect) { float thit, ray. Epsilon; if (!shape->Intersect(r, &thit, &ray. Epsilon, &isect->dg)) return false; isect->primitive = this; isect->World. To. Object = *shape->World. To. Object; isect->Object. To. World = *shape->Object. To. World; isect->shape. Id = shape->shape. Id; isect->primitive. Id = primitive. Id; isect->ray. Epsilon = ray. Epsilon; r. maxt = thit; return true; }

Object instancing 61 unique plants, 4000 individual plants, 19. 5 M triangles With instancing,

Object instancing 61 unique plants, 4000 individual plants, 19. 5 M triangles With instancing, store only 1. 1 M triangles, 11 GB->600 MB

Transformed. Primitive Reference<Primitive> primitive; Animated. Transform World. To. Primitive; for instancing and animation Ray

Transformed. Primitive Reference<Primitive> primitive; Animated. Transform World. To. Primitive; for instancing and animation Ray ray = World. To. Primitive(r); if (!instance->Intersect(ray, isect)) return false; r. maxt = ray. maxt; isect->World. To. Object = isect->World. To. Object *World. To. Instance;

Transformed. Primitive bool Intersect(Ray &r, Intersection *isect){ Transform w 2 p; World. To. Primitive.

Transformed. Primitive bool Intersect(Ray &r, Intersection *isect){ Transform w 2 p; World. To. Primitive. Interpolate(r. time, &w 2 p); Ray ray = w 2 p(r); if (!primitive->Intersect(ray, isect)) return false; r. maxt = ray. maxt; isect->primitive. Id = primitive. Id; if (!w 2 p. Is. Identity()) { // Compute world-to-object transformation for instance isect->World. To. Object=isect->World. To. Object*w 2 p; isect->Object. To. World=Inverse( isect->World. To. Object);

Transformed. Primitive // Transform instance's differential geometry to world space Transform Primitive. To. World

Transformed. Primitive // Transform instance's differential geometry to world space Transform Primitive. To. World = Inverse(w 2 p); isect->dg. p = Primitive. To. World(isect->dg. p); isect->dg. nn = Normalize( Primitive. To. World(isect- >dg. nn)); isect->dg. dpdu=Primitive. To. World(isect->dg. dpdu); isect->dg. dpdv=Primitive. To. World(isect->dg. dpdv); isect->dg. dndu=Primitive. To. World(isect->dg. dndu); isect->dg. dndv=Primitive. To. World(isect->dg. dndv); } return true; }

Aggregates • Acceleration is a heart component of a ray tracer because ray/scene intersection

Aggregates • Acceleration is a heart component of a ray tracer because ray/scene intersection accounts for the majority of execution time • Goal: reduce the number of ray/primitive intersections by quick simultaneous rejection of groups of primitives and the fact that nearby intersections are likely to be found first • Two main approaches: spatial subdivision, object subdivision • No clear winner

Ray-Box intersections • Almost all acclerators require it • Quick rejection, use enter and

Ray-Box intersections • Almost all acclerators require it • Quick rejection, use enter and exit point to traverse the hierarchy • AABB is the intersection of three slabs

Ray-Box intersections 1 Dx x=x 0 x=x 1

Ray-Box intersections 1 Dx x=x 0 x=x 1

Ray-Box intersections bool BBox: : Intersect. P(const Ray &ray, float *hitt 0, float *hitt

Ray-Box intersections bool BBox: : Intersect. P(const Ray &ray, float *hitt 0, float *hitt 1) { float t 0 = ray. mint, t 1 = ray. maxt; for (int i = 0; i < 3; ++i) { float inv. Ray. Dir = 1. f / ray. d[i]; float t. Near = (p. Min[i] - ray. o[i]) * inv. Ray. Dir; float t. Far = (p. Max[i] - ray. o[i]) * inv. Ray. Dir; if t 0 t 1 if (t. Near > t. Far) swap(t. Near, t. Far); = t. Near > t 0 ? t. Near : t 0; segment intersection = t. Far < t 1 ? t. Far : t 1; (t 0 > t 1) return false; intersection is empty } if (hitt 0) *hitt 0 = t 0; if (hitt 1) *hitt 1 = t 1; return true; }

Grid accelerator • Uniform grid

Grid accelerator • Uniform grid

Teapot in a stadium problem • Not adaptive to distribution of primitives. • Have

Teapot in a stadium problem • Not adaptive to distribution of primitives. • Have to determine the number of voxels. (problem with too many or too few)

Grid. Accel Class Grid. Accel: public Aggregate { <Grid. Accel methods> u_int n. Mailboxes;

Grid. Accel Class Grid. Accel: public Aggregate { <Grid. Accel methods> u_int n. Mailboxes; Mailbox. Prim *mailboxes; vector<Reference<Primitive>> primitives; int n. Voxels[3]; BBox bounds; Vector Width, Inv. Width; Voxel **voxels; Memory. Arena voxel. Arena; RWMutex *rw. Mutex; }

mailbox struct Mailbox. Prim { Reference<Primitive> primitive; Int last. Mailbox. Id; }

mailbox struct Mailbox. Prim { Reference<Primitive> primitive; Int last. Mailbox. Id; }

Grid. Accel(vector<Reference<Primitive> > &p, bool for. Refined, bool refine. Immediately) : grid. For. Refined(for.

Grid. Accel(vector<Reference<Primitive> > &p, bool for. Refined, bool refine. Immediately) : grid. For. Refined(for. Refined) { // Initialize with primitives for grid if (refine. Immediately) for (int i = 0; i < p. size(); ++i) p[i]->Fully. Refine(primitives); else primitives = p; for (int i = 0; i < primitives. size(); ++i) bounds = Union(bounds,

Determine number of voxels • Too many voxels → slow traverse, large memory consumption

Determine number of voxels • Too many voxels → slow traverse, large memory consumption (bad cache performance) • Too few voxels → too many primitives in a voxel • Let the axis with the largest extent have partitions (N: number of primitives) Vector delta = bounds. p. Max - bounds. p. Min; int max. Axis=bounds. Maximum. Extent(); float inv. Max. Width=1. f/delta[max. Axis]; float cube. Root=3. f*powf(float(prims. size()), 1. f/3. f); float voxels. Per. Unit. Dist=cube. Root * inv. Max. Width;

Calculate voxel size and allocate voxels for (int axis=0; axis<3; ++axis) { n. Voxels[axis]=Round

Calculate voxel size and allocate voxels for (int axis=0; axis<3; ++axis) { n. Voxels[axis]=Round 2 Int(delta[axis]*voxels. Per. Unit. Dist); n. Voxels[axis]=Clamp(n. Voxels[axis], 1, 64); } for (int axis=0; axis<3; ++axis) { width[axis]=delta[axis]/n. Voxels[axis]; inv. Width[axis]= (width[axis]==0. f)? 0. f: 1. f/width[axis]; } int nv = n. Voxels[0] * n. Voxels[1] * n. Voxels[2]; voxels=Alloc. Aligned<Voxel *>(nv); memset(voxels, 0, nv * sizeof(Voxel *));

Conversion between voxel and position int pos. To. Voxel(const Point &P, int axis) {

Conversion between voxel and position int pos. To. Voxel(const Point &P, int axis) { int v=Float 2 Int( (P[axis]-bounds. p. Min[axis])*Inv. Width[axis]); return Clamp(v, 0, NVoxels[axis]-1); } float voxel. To. Pos(int p, int axis) const { return bounds. p. Min[axis]+p*Width[axis]; } Point voxel. To. Pos(int x, int y, int z) const { return bounds. p. Min+ Vector(x*Width[0], y*Width[1], z*Width[2]); } inline int offset(int x, int y, int z) { return z*NVoxels[0]*NVoxels[1] + y*NVoxels[0] + x; }

Add primitives into voxels for (u_int i=0; i<prims. size(); ++i) { <Find voxel extent

Add primitives into voxels for (u_int i=0; i<prims. size(); ++i) { <Find voxel extent of primitive> <Add primitive to overlapping voxels> }

<Find voxel extent of primitive> BBox pb = prims[i]->World. Bound(); int vmin[3], vmax[3]; for

<Find voxel extent of primitive> BBox pb = prims[i]->World. Bound(); int vmin[3], vmax[3]; for (int axis = 0; axis < 3; ++axis) { vmin[axis] = pos. To. Voxel(pb. p. Min, axis); vmax[axis] = pos. To. Voxel(pb. p. Max, axis); }

<Add primitive to overlapping voxels> for (int z = vmin[2]; z <= vmax[2]; ++z)

<Add primitive to overlapping voxels> for (int z = vmin[2]; z <= vmax[2]; ++z) for (int y = vmin[1]; y <= vmax[1]; ++y) for (int x = vmin[0]; x <= vmax[0]; ++x) { int o = offset(x, y, z); if (!voxels[o]) { voxels[o] = voxel. Arena. Alloc<Voxel>(); *voxels[o] = Voxel(primitives[i]); } else { // Add primitive to already-allocated voxels[o]->Add. Primitive(primitives[i]); } }

Voxel structure struct Voxel { <Voxel methods> vector<Reference<Primitive>> primitives; bool all. Can. Intersect; }

Voxel structure struct Voxel { <Voxel methods> vector<Reference<Primitive>> primitives; bool all. Can. Intersect; } Voxel(Reference<Primitive> op) { all. Can. Intersect = false; primitives. push_back(op); } void Add. Primitive(Reference<Primitive> p) { primitives. push_back(p); }

Grid. Accel traversal bool Grid. Accel: : Intersect( Ray &ray, Intersection *isect) { <Check

Grid. Accel traversal bool Grid. Accel: : Intersect( Ray &ray, Intersection *isect) { <Check ray against overall grid bounds> <Set up 3 D DDA for ray> <Walk ray through voxel grid> }

Check against overall bound float ray. T; if (bounds. Inside(ray. mint))) ray. T =

Check against overall bound float ray. T; if (bounds. Inside(ray. mint))) ray. T = ray. mint; else if (!bounds. Intersect. P(ray, &ray. T)) return false; Point grid. Intersect = ray(ray. T);

Set up 3 D DDA (Digital Differential Analyzer) • Similar to Bresenhm’s line drawing

Set up 3 D DDA (Digital Differential Analyzer) • Similar to Bresenhm’s line drawing algorithm

Set up 3 D DDA (Digital Differential Analyzer) blue values changes along the traversal

Set up 3 D DDA (Digital Differential Analyzer) blue values changes along the traversal Out Next. Crossing. T[1] voxel index Delta. T[0] Step[0]=1 ray. T Pos Next. Crossing. T[0] Delta. T: the distance change when voxel changes 1 in that direction

Set up 3 D DDA for (int axis=0; axis<3; ++axis) { Pos[axis]=pos. To. Voxel(grid.

Set up 3 D DDA for (int axis=0; axis<3; ++axis) { Pos[axis]=pos. To. Voxel(grid. Intersect, axis); if (ray. d[axis]>=0) { Next. Crossing. T[axis] = ray. T+ (voxel. To. Pos(Pos[axis]+1, axis)-grid. Intersect[axis]) /ray. d[axis]; Delta. T[axis] = width[axis] / ray. d[axis]; Step[axis] = 1; 1 Out[axis] = n. Voxels[axis]; } else { Dx. . . Step[axis] = -1; Out[axis] = -1; } } width[0]

Walk through grid for (; ; ) { *voxel=voxels[offset(Pos[0], Pos[1], Pos[2])]; if (voxel !=

Walk through grid for (; ; ) { *voxel=voxels[offset(Pos[0], Pos[1], Pos[2])]; if (voxel != NULL) hit. Something |= voxel->Intersect(ray, isect, ray. Id); <Advance to next voxel> } return hit. Something; Do not return; cut tmax instead Return when entering a voxel that is beyond the closest found intersection.

Advance to next voxel int bits=((Next. Crossing. T[0]<Next. Crossing. T[1])<<2) + ((Next. Crossing. T[0]<Next.

Advance to next voxel int bits=((Next. Crossing. T[0]<Next. Crossing. T[1])<<2) + ((Next. Crossing. T[0]<Next. Crossing. T[2])<<1) + ((Next. Crossing. T[1]<Next. Crossing. T[2])); const int cmp. To. Axis[8] = { 2, 1, 2, 2, 0, 0 }; int step. Axis=cmp. To. Axis[bits]; if (ray. maxt < Next. Crossing. T[step. Axis]) break; Pos[step. Axis]+=Step[step. Axis]; if (Pos[step. Axis] == Out[step. Axis]) break; Next. Crossing. T[step. Axis] += Delta. T[step. Axis];

conditions x<y x<z y<z 0 0 0 x≥y≥z 2 0 0 1 x≥z>y 1

conditions x<y x<z y<z 0 0 0 x≥y≥z 2 0 0 1 x≥z>y 1 0 0 1 1 z>x≥y 1 1 0 0 y>x≥z 2 1 0 1 1 1 0 y≥z>x 0 1 1 1 z>y>x 0 - -

Bounding volume hierarchies • Object subdivision. Each primitive appears in the hierarchy exactly once.

Bounding volume hierarchies • Object subdivision. Each primitive appears in the hierarchy exactly once. Additionally, the required space for the hierarchy is bounded. • BVH v. s. Grid: both are efficient to build, but BVH provides much faster intersection. • BVH v. s. Kd-tree: Kd-tree could be slightly faster for intersection, but takes much longer to build. In addition, BVH is generally more numerically robust and less prone to subtle round-off bugs. • accelerators/bvh. *

BVHAccel class BVHAccel : public Aggregate { <member functions> uint 32_t max. Prims. In.

BVHAccel class BVHAccel : public Aggregate { <member functions> uint 32_t max. Prims. In. Node; enum Split. Method { SPLIT_MIDDLE, SPLIT_EQUAL_COUNTS, SPLIT_SAH }; Split. Method split. Method; vector<Reference<Primitive> > primitives; Linear. BVHNode *nodes; }

BVHAccel construction BVHAccel: : BVHAccel(vector<Reference<Primitive> > &p, uint 32_t mp, const string &sm) {

BVHAccel construction BVHAccel: : BVHAccel(vector<Reference<Primitive> > &p, uint 32_t mp, const string &sm) { max. Prims. In. Node = min(255 u, mp); for (uint 32_t i = 0; i < p. size(); ++i) p[i]->Fully. Refine(primitives); if (sm=="sah") split. Method =SPLIT_SAH; else if (sm=="middle") split. Method =SPLIT_MIDDLE; else if (sm=="equal") split. Method=SPLIT_EQUAL_COUNTS; else { Warning("BVH split method "%s" unknown. Using "sah". ", sm. c_str()); split. Method = SPLIT_SAH; }

BVHAccel construction <Initialize build. Data array for primitives> <Recursively build BVH tree for primitives>

BVHAccel construction <Initialize build. Data array for primitives> <Recursively build BVH tree for primitives> <compute representation of depth-first traversal of BVH tree> } It is possible to construct a pointer-less BVH tree directly, but it is less straightforward.

Initialize build. Data array vector<BVHPrimitive. Info> build. Data; build. Data. reserve(primitives. size()); for (int

Initialize build. Data array vector<BVHPrimitive. Info> build. Data; build. Data. reserve(primitives. size()); for (int i = 0; i < primitives. size(); ++i) { BBox bbox = primitives[i]->World. Bound(); build. Data. push_back( BVHPrimitive. Info(i, bbox)); } struct BVHPrimitive. Info { BVHPrimitive. Info() { } BVHPrimitive. Info(int pn, const BBox &b) : primitive. Number(pn), bounds(b) { centroid =. 5 f * b. p. Min +. 5 f * b. p. Max; } int primitive. Number; Point centroid; BBox bounds; };

Recursively build BVH tree Memory. Arena build. Arena; uint 32_t total. Nodes = 0;

Recursively build BVH tree Memory. Arena build. Arena; uint 32_t total. Nodes = 0; vector<Reference<Primitive> > ordered. Prims; ordered. Prims. reserve(primitives. size()); BVHBuild. Node *root = recursive. Build(build. Arena, build. Data, 0, primitives. size(), &total. Nodes, ordered. Prims); [start end) primitives. swap(ordered. Prims);

BVHBuild. Node struct BVHBuild. Node { void Init. Leaf(int first, int n, BBox &b)

BVHBuild. Node struct BVHBuild. Node { void Init. Leaf(int first, int n, BBox &b) { first. Prim. Offset = first; n. Primitives = n; bounds = b; } void Init. Interior(int axis, BVHBuild. Node *c 0, BVHBuild. Node *c 1) { children[0] = c 0; children[1] = c 1; bounds = Union(c 0 ->bounds, c 1 ->bounds); split. Axis = axis; n. Primitives = 0; The leaf contains primitives from } BVHAccel: : primitives[first. Prim. Offset] BBox bounds; to [first. Prim. Offset+n. Primitives-1] BVHBuild. Node *children[2]; int split. Axis, first. Prim. Offset, n. Primitives; };

recursive. Build • Given n primitives, there are in general 2 n-2 possible ways

recursive. Build • Given n primitives, there are in general 2 n-2 possible ways to partition them into two nonempty groups. In practice, one considers partitions along a coordinate axis, resulting in 6 n candidate partitions. 1. Choose axis 2. Choose split 3. Interior(dim, recursive. Build(. . , start, mid, . . ), recursive. Build(. . , mid, end, . . ) )

Choose axis BBox c. Bounds; for (int i = start; i < end; ++i)

Choose axis BBox c. Bounds; for (int i = start; i < end; ++i) c. Bounds=Union(c. Bounds, build. Data[i]. centroid); int dim = centroid. Bounds. Maximum. Extent(); If c. Bounds has zreo volume, create a leaf

Choose split (split_middle) float pmid =. 5 f * (centroid. Bounds. p. Min[dim] +

Choose split (split_middle) float pmid =. 5 f * (centroid. Bounds. p. Min[dim] + centroid. Bounds. p. Max[dim]); BVHPrimitive. Info *mid. Ptr = std: : partition( &build. Data[start], &build. Data[end-1]+1, Compare. To. Mid(dim, pmid)); mid = mid. Ptr - &build. Data[0]; Return true if the given primitive’s bound’s centroid is below the given midpoint

Choose split (split_equal_count) mid = (start + end) / 2; std: : nth_element(&build. Data[start],

Choose split (split_equal_count) mid = (start + end) / 2; std: : nth_element(&build. Data[start], &build. Data[mid], &build. Data[end-1]+1, Compare. Points(dim)); It orders the array so that the middle pointer has median, the first half is smaller and the second half is larger in O(n).

Choose split both heuristics work well both are sub-optimal better solution

Choose split both heuristics work well both are sub-optimal better solution

Choose split (split_SAH) Do not split B A C

Choose split (split_SAH) Do not split B A C

Choose split (split_SAH) • If there are no more than 4 primitives, use equal

Choose split (split_SAH) • If there are no more than 4 primitives, use equal size heuristics instead. • Instead of testing 2 n candidates, the extend is divided into a small number (12) of buckets of equal extent. Only buck boundaries are considered.

Choose split (split_SAH) const int n. Buckets = 12; struct Bucket. Info { int

Choose split (split_SAH) const int n. Buckets = 12; struct Bucket. Info { int count; BBox bounds; }; Bucket. Info buckets[n. Buckets]; for (int i=start; i<end; ++i) { int b = n. Buckets * ((build. Data[i]. centroid[dim]-centroid. Bounds. p. Min[dim])/ (centroid. Bounds. p. Max[dim]-centroid. Bounds. p. Min[dim])); if (b == n. Buckets) b = n. Buckets-1; buckets[b]. count++; buckets[b]. bounds = Union(buckets[b]. bounds, build. Data[i]. bounds); }

Choose split (split_SAH) float cost[n. Buckets-1]; for (int i = 0; i < n.

Choose split (split_SAH) float cost[n. Buckets-1]; for (int i = 0; i < n. Buckets-1; ++i) { BBox b 0, b 1; int count 0 = 0, count 1 = 0; for (int j = 0; j <= i; ++j) { b 0 = Union(b 0, buckets[j]. bounds); count 0 += buckets[j]. count; } for (int j = i+1; j < n. Buckets; ++j) { b 1 = Union(b 1, buckets[j]. bounds); count 1 += buckets[j]. count; } cost[i] =. 125 f + (count 0*b 0. Surface. Area() + count 1*b 1. Surface. Area())/bbox. Surface. Area(); Traverse cost : Intersection cost = 1 : 8 }

Choose split (split_SAH) float min. Cost = cost[0]; uint 32_t min. Cost. Split =

Choose split (split_SAH) float min. Cost = cost[0]; uint 32_t min. Cost. Split = 0; for (int i = 1; i < n. Buckets-1; ++i) { if (cost[i] < min. Cost) { min. Cost = cost[i]; min. Cost. Split = i; } } if (n. Primitives > max. Prims. In. Node || min. Cost < n. Primitives) { BVHPrimitive. Info *pmid = std: : partition(&build. Data[start], &build. Data[end 1]+1, Compare. To. Bucket(min. Cost. Split, n. Buckets, dim, centroid. Bounds)); mid = pmid - &build. Data[0]; } else <create a leaf>

Compact BVH • The last step is to convert the BVH tree into a

Compact BVH • The last step is to convert the BVH tree into a compact representation which improves cache, memory and thus overall performance.

BVHAccel traversal bool BVHAccel: : Intersect(const Ray &ray, Intersection *isect) const { if (!nodes)

BVHAccel traversal bool BVHAccel: : Intersect(const Ray &ray, Intersection *isect) const { if (!nodes) return false; bool hit = false; Point origin = ray(ray. mint); Vector inv. Dir(1. f / ray. d. x, 1. f / ray. d. y, 1. f / ray. d. z); uint 32_t dir. Is. Neg[3]={ inv. Dir. x < 0, inv. Dir. y < 0, inv. Dir. z < 0 }; uint 32_t node. Num = 0; offset into the nodes array to be visited uint 32_t todo[64]; nodes to be visited; acts like a stack uint 32_t todo. Offset = 0; next free element in the stack

BVHAccel traversal while (true) { const Linear. BVHNode *node = &nodes[node. Num]; if (:

BVHAccel traversal while (true) { const Linear. BVHNode *node = &nodes[node. Num]; if (: : Intersect. P(node->bounds, ray, inv. Dir, dir. Is. Neg)){ if (node->n. Primitives > 0) { leaf node // Intersect ray with primitives in leaf BVH node for (uint 32_t i = 0; i < node->n. Primitives; ++i){ if (primitives[node->primitives. Offset+i] ->Intersect(ray, isect)) hit = true; } if (todo. Offset == 0) break; node. Num = todo[--todo. Offset]; }

BVHAccel traversal else { interior node if (dir. Is. Neg[node->axis]) { todo[todo. Offset++] =

BVHAccel traversal else { interior node if (dir. Is. Neg[node->axis]) { todo[todo. Offset++] = node. Num + 1; node. Num = node->second. Child. Offset; } else { todo[todo. Offset++] = node->second. Child. Offset; node. Num = node. Num + 1; } } } else { Do not hit the bounding box; retrieve the next one if any if (todo. Offset == 0) break; node. Num = todo[--todo. Offset]; }

KD-Tree accelerator • Non-uniform space subdivision (for example, kd -tree and octree) is better

KD-Tree accelerator • Non-uniform space subdivision (for example, kd -tree and octree) is better than uniform grid if the scene is irregularly distributed.

Spatial hierarchies A A Letters correspond to planes (A) Point Location by recursive search

Spatial hierarchies A A Letters correspond to planes (A) Point Location by recursive search

Spatial hierarchies A B B A Letters correspond to planes (A, B) Point Location

Spatial hierarchies A B B A Letters correspond to planes (A, B) Point Location by recursive search

Spatial hierarchies A D B B C C D A Letters correspond to planes

Spatial hierarchies A D B B C C D A Letters correspond to planes (A, B, C, D) Point Location by recursive search

Variations kd-tree octree bsp-tree

Variations kd-tree octree bsp-tree

“Hack” kd-tree building • Split axis – Round-robin; largest extent • Split location –

“Hack” kd-tree building • Split axis – Round-robin; largest extent • Split location – Middle of extent; median of geometry (balanced tree) • Termination – Target # of primitives, limited tree depth • All of these techniques stink.

Building good kd-trees • What split do we really want? – Clever Idea: the

Building good kd-trees • What split do we really want? – Clever Idea: the one that makes ray tracing cheap – Write down an expression of cost and minimize it – Greedy cost optimization • What is the cost of tracing a ray through a cell? Cost(cell) = C_trav + Prob(hit L) * Cost(L) + Prob(hit R) * Cost(R)

Splitting with cost in mind

Splitting with cost in mind

Split in the middle To get through this part of empty space, you need

Split in the middle To get through this part of empty space, you need to test all triangles on the right. • Makes the L & R probabilities equal • Pays no attention to the L & R costs

Split at the median • Makes the L & R costs equal • Pays

Split at the median • Makes the L & R costs equal • Pays no attention to the L & R probabilities

Cost-optimized split Since Cost(R) is much higher, make it as small as possible •

Cost-optimized split Since Cost(R) is much higher, make it as small as possible • Automatically and rapidly isolates complexity • Produces large chunks of empty space

Building good kd-trees • Need the probabilities – Turns out to be proportional to

Building good kd-trees • Need the probabilities – Turns out to be proportional to surface area • Need the child cell costs – Simple triangle count works great (very rough approx. ) – Empty cell “boost” Cost(cell) = C_trav + Prob(hit L) * Cost(L) + Prob(hit R) * Cost(R) = C_trav + SA(L) * Tri. Count(L) + SA(R) * Tri. Count(R) C_trav is the ratio of the cost to traverse to the cost to intersect C_trav = 1: 80 in pbrt (found by experiments)

Surface area heuristic 2 n splits; must coincides with object boundary. Why? a b

Surface area heuristic 2 n splits; must coincides with object boundary. Why? a b

Termination criteria • When should we stop splitting? – Bad: depth limit, number of

Termination criteria • When should we stop splitting? – Bad: depth limit, number of triangles – Good: when split does not help any more. • Threshold of cost improvement – Stretch over multiple levels – For example, if cost does not go down after three splits in a row, terminate • Threshold of cell size – Absolute probability SA(node)/SA(scene) small

Basic building algorithm 1. Pick an axis, or optimize across all three 2. Build

Basic building algorithm 1. Pick an axis, or optimize across all three 2. Build a set of candidate split locations (cost extrema must be at bbox vertices) 3. Sort or bin the triangles 4. Sweep to incrementally track L/R counts, cost 5. Output position of minimum cost split Running time: • Characteristics of highly optimized tree – very deep, very small leaves, big empty cells

Ray traversal algorithm • Recursive inorder traversal Intersect(L, tmin, tmax) Intersect(L, tmin, t*) Intersect(R,

Ray traversal algorithm • Recursive inorder traversal Intersect(L, tmin, tmax) Intersect(L, tmin, t*) Intersect(R, tmin, tmax) Intersect(R, t*, tmax) a video for kdtree

Tree representation 8 -byte (reduced from 16 -byte, 20% gain) struct Kd. Accel. Node

Tree representation 8 -byte (reduced from 16 -byte, 20% gain) struct Kd. Accel. Node {. . . union { float split; // Interior u_int one. Primitive; // Leaf u_int *primitives; // Leaf }; union { u_int flags; // Both u_int n. Prims; // Leaf u_int above. Child; // Interior }; } interior leaf n

Tree representation 1 8 float is irrelevant in pbrt 2 23 S E M

Tree representation 1 8 float is irrelevant in pbrt 2 23 S E M n flags 2 Flag: 0, 1, 2 (interior x, y, z) 3 (leaf)

Kd. Tree. Accel construction • Recursive top-down algorithm • max depth = If (n.

Kd. Tree. Accel construction • Recursive top-down algorithm • max depth = If (n. Prims <= max. Prims || depth==0) { <create leaf> }

Interior node • Choose split axis position – Medpoint – Medium cut – Area

Interior node • Choose split axis position – Medpoint – Medium cut – Area heuristic • Create leaf if no good splits were found • Classify primitives with respect to split

Choose split axis position cost of no split: cost of split: assumptions: 1. ti

Choose split axis position cost of no split: cost of split: assumptions: 1. ti is the same for all primitives 2. ti : tt = 80 : 1 (determined by experiments, main factor for the performance) cost of no split: cost of split: A B C

Choose split axis position Start from the axis with maximum extent, sort all edge

Choose split axis position Start from the axis with maximum extent, sort all edge events and process them in order A C B a 0 b 0 a 1 b 1 c 0 c 1

Choose split axis position If there is no split along this axis, try other

Choose split axis position If there is no split along this axis, try other axes. When all fail, create a leaf.

Kd. Tree. Accel traversal

Kd. Tree. Accel traversal

Kd. Tree. Accel traversal tmax To. Do stack tplane tmin far near

Kd. Tree. Accel traversal tmax To. Do stack tplane tmin far near

Kd. Tree. Accel traversal To. Do stack tmax tmin far near

Kd. Tree. Accel traversal To. Do stack tmax tmin far near

Kd. Tree. Accel traversal far tmax tplane tmin near To. Do stack

Kd. Tree. Accel traversal far tmax tplane tmin near To. Do stack

Kd. Tree. Accel traversal tmax tmin To. Do stack

Kd. Tree. Accel traversal tmax tmin To. Do stack

Kd. Tree. Accel traversal tmax tmin To. Do stack

Kd. Tree. Accel traversal tmax tmin To. Do stack

Kd. Tree. Accel traversal bool Kd. Tree. Accel: : Intersect (const Ray &ray, Intersection

Kd. Tree. Accel traversal bool Kd. Tree. Accel: : Intersect (const Ray &ray, Intersection *isect) { if (!bounds. Intersect. P(ray, &tmin, &tmax)) return false; Kd. Accel. Node *node=&nodes[0]; while (node!=NULL) { if (ray. maxt<tmin) break; if (!node->Is. Leaf()) <Interior> else <Leaf> } } To. Do stack (max depth)

Leaf node 1. Check whether ray intersects primitive(s) inside the node; update ray’s maxt

Leaf node 1. Check whether ray intersects primitive(s) inside the node; update ray’s maxt 2. Grab next node from To. Do queue

Interior node 1. Determine near and far (by testing which side O is) below

Interior node 1. Determine near and far (by testing which side O is) below node+1 above &(nodes[node->above. Child]) 2. Determine whether we can skip a node tmin tmax tplane tmin tmax near far tplane near far

Acceleration techniques

Acceleration techniques

Best efficiency scheme

Best efficiency scheme

References • J. Goldsmith and J. Salmon, Automatic Creation of Object Hierarchies for Ray

References • J. Goldsmith and J. Salmon, Automatic Creation of Object Hierarchies for Ray Tracing, IEEE CG&A, 1987. • Brian Smits, Efficiency Issues for Ray Tracing, Journal of Graphics Tools, 1998. • K. Klimaszewski and T. Sederberg, Faster Ray Tracing Using Adaptive Grids, IEEE CG&A Jan/Feb 1999. • Whang et. al. , Octree-R: An Adaptive Octree for efficient ray tracing, IEEE TVCG 1(4), 1995. • A. Glassner, Space subdivision for fast ray tracing. IEEE CG&A, 4(10), 1984