Skip to content

Commit 7cd68af

Browse files
committed
raytracer example tweaks
* Lowered the grain size to 256 pixels for better performance on large core counts. * Added a `--ppm6` flag option to output smaller files
1 parent ac9d952 commit 7cd68af

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

examples/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,12 @@ $ ./dmm @mpl procs 4 -- -N 1024
5555
## Ray Tracing
5656

5757
A simple ray tracer that generates MxN images in PPM format. Use
58-
`-m`/`-n` for output size, `-s` to select a scene, and `-f` to specific
59-
the output file. For example:
58+
`-m`/`-n` for output size, `-s` to select a scene, and `-f` to specify
59+
the output file. By default, this outputs in P3 format (human readable);
60+
use `--ppm6` for P6 instead (smaller file size but not easily readable).
61+
For example:
6062
```
6163
$ make ray
6264
$ ./ray -f out.ppm -m 400 -n 400 -s irreg
65+
$ ./ray @mpl procs 4 -- -f out.ppm -m 1000 -n 1000 -s rgbbox --ppm6
6366
```

examples/src/ray.sml

+16-2
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,17 @@ fun image2ppm out ({pixels, height, width}: image) =
288288
before Array.app onPixel pixels
289289
end
290290

291+
fun image2ppm6 out ({pixels, height, width}: image) =
292+
let
293+
fun onPixel (r,g,b) =
294+
TextIO.output(out, String.implode (List.map Char.chr [r,g,b]))
295+
in TextIO.output(out,
296+
"P6\n" ^
297+
Int.toString width ^ " " ^ Int.toString height ^ "\n" ^
298+
"255\n")
299+
before Array.app onPixel pixels
300+
end
301+
291302
fun render objs width height cam : image =
292303
let val pixels = ForkJoin.alloc (height*width)
293304
fun pixel l =
@@ -296,7 +307,7 @@ fun render objs width height cam : image =
296307
in Array.update (pixels,
297308
l,
298309
colour_to_pixel (trace_ray objs width height cam j i)) end
299-
val _ = ForkJoin.parfor 10000 (0,height*width) pixel
310+
val _ = ForkJoin.parfor 256 (0,height*width) pixel
300311
in {width = width,
301312
height = height,
302313
pixels = pixels
@@ -385,6 +396,7 @@ val irreg : scene =
385396
val height = CommandLineArgs.parseInt "m" 200
386397
val width = CommandLineArgs.parseInt "n" 200
387398
val f = CommandLineArgs.parseString "f" ""
399+
val dop6 = CommandLineArgs.parseFlag "ppm6"
388400
val scene_name = CommandLineArgs.parseString "s" "rgbbox"
389401
val scene = case scene_name of
390402
"rgbbox" => rgbbox
@@ -404,10 +416,12 @@ val t1 = Time.now ()
404416

405417
val _ = print ("Rendering in " ^ Time.fmt 4 (Time.- (t1, t0)) ^ "s.\n")
406418

419+
val writeImage = if dop6 then image2ppm6 else image2ppm
420+
407421
val _ = if f <> "" then
408422
let val out = TextIO.openOut f
409423
in print ("Writing image to " ^ f ^ ".\n")
410-
before image2ppm out (render objs width height cam)
424+
before writeImage out (render objs width height cam)
411425
before TextIO.closeOut out
412426
end
413427
else print ("-f not passed, so not writing image to file.\n")

0 commit comments

Comments
 (0)