Skip to content

Commit

Permalink
Do a quick round of optimizations
Browse files Browse the repository at this point in the history
Fixes #13.
  • Loading branch information
cespare committed Jun 4, 2017
1 parent f9eee56 commit a95414e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
7 changes: 7 additions & 0 deletions frosty.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"os"
"regexp"
"runtime"

"github.com/pkg/profile"
)

func jsonError(raw []byte, err error) error {
Expand Down Expand Up @@ -47,10 +49,15 @@ func main() {
supersampling = flag.Int("supersampling", 1, "Supersampling (antialiasing) factor")
// Default value of 4 * numcpu is based on some ad hoc testing.
parallelism = flag.Int("parallelism", 4*runtime.NumCPU(), "Number of rays to compute in parallel")
cpuProfile = flag.Bool("cpuprofile", false, "Emit CPU profile")
)
flag.Parse()
_ = *debug

if *cpuProfile {
defer profile.Start(profile.CPUProfile).Stop()
}

if *supersampling < 1 || *supersampling > 8 {
log.Fatalf("Supersampling should be between 1 and 8; got %d", *supersampling)
}
Expand Down
13 changes: 10 additions & 3 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,19 @@ func (i *Image) At(x, y int) Color {
}

func (i *Image) Set(x, y int, c Color) {
if x < 0 || x >= i.Width || y < 0 || y >= i.Height {
panic("Out of bounds for Set() on Image.")
}
//if x < 0 || x >= i.Width || y < 0 || y >= i.Height {
// panic("Out of bounds for Set() on Image.")
//}
i.Pix[y*i.Width+x] = c
}

func (i *Image) SetLine(y int, c []Color) {
if len(c) != i.Width {
panic("line of wrong length")
}
copy(i.Pix[y*i.Width:], c)
}

// ToneMap scales down the range of colors to 32-bit RGBA. Right now it uses a
// simplistic heuristic: it just scales the values linearly such that the most
// intense channel value is 0xFF.
Expand Down
1 change: 1 addition & 0 deletions make.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ set -eu -o pipefail
go build -o frosty
time ./frosty \
-debug \
-cpuprofile \
-hpixels 1200 \
-out out.png
5 changes: 3 additions & 2 deletions render.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@ func (r *Rendering) Render(parallelism int) *Image {
h := int(float64(w) * r.Camera.Aspect)
img := NewImage(w, h)
scanner := NewLineScanner(r.Camera, r.HPixels)

var wg sync.WaitGroup
wg.Add(parallelism)
lines := scanner.Scan()
for i := 0; i < parallelism; i++ {
go func() {
result := make([]Color, scanner.hPixels)
for line := range lines {
for x := line.xMin; x < line.xMax; x++ {
img.Set(x, line.y, r.Trace(line.rays[x]))
result[x] = r.Trace(line.rays[x])
}
img.SetLine(line.y, result)
}
wg.Done()
}()
Expand Down

0 comments on commit a95414e

Please sign in to comment.