Skip to content

Commit

Permalink
Parallelize the process.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsy committed Nov 9, 2017
1 parent 6c290ef commit 0b1b428
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ test:
go test ./... --cover

clean:
rm -rf main *.jpg *.png
rm -rf main *.jpg *.png *.prof
11 changes: 6 additions & 5 deletions src/accelerator/bvh.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ type Bvh struct {
func NewBvh(primitives []Primitive) (bvh *Bvh) {
bvh = &Bvh{}
bvh.primitives = primitives
bvh.root = NewBvhSub(bvh, primitives, 0)
bvh.root = NewBvhSub(bvh, primitives)
return
}

func NewBvhSub(bvh *Bvh, primitives []Primitive, axis int) *BvhNode {
func NewBvhSub(bvh *Bvh, primitives []Primitive) *BvhNode {
if len(primitives) == 1 {
node := NewLeafNode(primitives[0].Shape)
bvh.nodes = append(bvh.nodes, *node)
Expand All @@ -84,8 +84,9 @@ func NewBvhSub(bvh *Bvh, primitives []Primitive, axis int) *BvhNode {
bbox.Merge(b)
items[i] = SortItem{b.Center(), i}
}
axis := bbox.MaxExtent()

axisSorter := &AxisSorter{items, axis % 3}
axisSorter := &AxisSorter{items, axis}
sort.Sort(axisSorter)

newPrimitives := make([]Primitive, len(primitives))
Expand All @@ -94,8 +95,8 @@ func NewBvhSub(bvh *Bvh, primitives []Primitive, axis int) *BvhNode {
}

iHalf := len(newPrimitives) / 2
leftNode := NewBvhSub(bvh, newPrimitives[:iHalf], (axis + 1) % 3)
rightNode := NewBvhSub(bvh, newPrimitives[iHalf:], (axis + 1) % 3)
leftNode := NewBvhSub(bvh, newPrimitives[:iHalf])
rightNode := NewBvhSub(bvh, newPrimitives[iHalf:])

node := NewForkNode(leftNode, rightNode, bbox)
bvh.nodes = append(bvh.nodes, *node)
Expand Down
13 changes: 13 additions & 0 deletions src/core/bounds3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ func (b *Bounds3d) Center() (c Vector3d) {
return
}

func (b *Bounds3d) MaxExtent() int {
v := b.MaxPos.Subtract(b.MinPos)
v = v.Abs()
switch {
case v.X > v.Y && v.X > v.Z:
return 0
case v.Y > v.Z:
return 1
default:
return 2
}
}

func (b *Bounds3d) Intersect(ray *Ray, tNear *Float, tFar *Float) bool {
t0 := 0.0
t1 := ray.MaxDist
Expand Down
2 changes: 2 additions & 0 deletions src/core/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const (
Infinity = Float(1.0e20)
)

type Semaphore struct {}

func Sign(x Float) Float {
if x < 0.0 {
return -1.0
Expand Down
7 changes: 7 additions & 0 deletions src/core/vector3d.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ func (v *Vector3d) Divide(s Float) (ret Vector3d) {
return
}

func (v *Vector3d) Abs() (ret Vector3d) {
ret.X = math.Abs(v.X)
ret.Y = math.Abs(v.Y)
ret.Z = math.Abs(v.Z)
return
}

func (v1 *Vector3d) Dot(v2 Vector3d) (ret Float) {
ret = v1.X * v2.X + v1.Y * v2.Y + v1.Z * v2.Z
return
Expand Down
15 changes: 12 additions & 3 deletions src/integrator/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,21 @@ func (integrator *PathIntegrator) Render(bvh *Bvh, sensor Sensor, sampler Sample
width := sensor.Film().Width
height := sensor.Film().Height
film := sensor.Film()
sem := make(chan Semaphore, width)
for y := 0; y < height; y++ {
for x := 0; x < width; x++ {
ray := sensor.SpawnRay(x, y)
L := integrator.Li(bvh, ray, sampler)
film.Update(x, y, L)
go func(x, y int) {
ray := sensor.SpawnRay(x, y)
L := integrator.Li(bvh, ray, sampler)
film.Update(x, y, L)
sem <- Semaphore{}
} (x, y)
}

for x := 0; x < width; x++ {
<-sem
}

ProgressBar(y + 1, height)
}
fmt.Println()
Expand Down
2 changes: 1 addition & 1 deletion src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
)

func main() {
cpuprofile := "mycpu.prof"
cpuprofile := "profile.prof"
f, err := os.Create(cpuprofile)
if err != nil {
log.Fatal(err)
Expand Down

0 comments on commit 0b1b428

Please sign in to comment.