| 1 |
;;; |
| 2 |
;;; aobench - Ambient Occlusion Benchmark |
| 3 |
;;; Based on the code by Syoyo Fujita |
| 4 |
;;; https://github.com/syoyo/aobench |
| 5 |
;;; |
| 6 |
|
| 7 |
;; More discussion about this code is here: |
| 8 |
;; http://practical-scheme.net/wiliki/wiliki.cgi?Gauche%3AAOBench |
| 9 |
|
| 10 |
;; Copyright 2009-2014, Syoyo Fujita |
| 11 |
;; All rights reserved. |
| 12 |
;; |
| 13 |
;; Redistribution and use in source and binary forms, with or without |
| 14 |
;; modification, are permitted provided that the following conditions are |
| 15 |
;; met: |
| 16 |
;; |
| 17 |
;; 1. Redistributions of source code must retain the above copyright |
| 18 |
;; notice, this list of conditions and the following disclaimer. |
| 19 |
;; |
| 20 |
;; 2. Redistributions in binary form must reproduce the above copyright |
| 21 |
;; notice, this list of conditions and the following disclaimer in the |
| 22 |
;; documentation and/or other materials provided with the distribution. |
| 23 |
;; |
| 24 |
;; THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 25 |
;; CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, |
| 26 |
;; BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 27 |
;; FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 28 |
;; COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 29 |
;; INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 30 |
;; BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
| 31 |
;; OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 32 |
;; ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR |
| 33 |
;; TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE |
| 34 |
;; USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH |
| 35 |
;; DAMAGE. |
| 36 |
|
| 37 |
;; Run this script as |
| 38 |
;; $ gosh ./aobench.scm |
| 39 |
;; The output is written to ao.ppm. |
| 40 |
|
| 41 |
;; This script uses multiple threads if multiple cores are available. |
| 42 |
;; To force single thread execution, set GAUCHE_AVAILABLE_PROCESSORS |
| 43 |
;; environment variable to 1. |
| 44 |
|
| 45 |
(use gauche.uvector) |
| 46 |
(use gauche.record) |
| 47 |
(use gauche.threads) |
| 48 |
(use control.thread-pool) |
| 49 |
(use math.const) |
| 50 |
(use srfi.27) |
| 51 |
|
| 52 |
(define-constant WIDTH 256) |
| 53 |
(define-constant HEIGHT 256) |
| 54 |
(define-constant NSUBSAMPLES 2) |
| 55 |
(define-constant NAO_SAMPLES 8) |
| 56 |
|
| 57 |
;; Eventually these should be written by define-record-type with pseudo-rtd. |
| 58 |
;; Currently, the accessors generated by define-record-type are not inlined, |
| 59 |
;; so we define these manually for speed. |
| 60 |
(define-inline (vec x y z) (f64vector x y z)) |
| 61 |
(define-inline (vec-x v) (f64vector-ref v 0)) |
| 62 |
(define-inline (vec-y v) (f64vector-ref v 1)) |
| 63 |
(define-inline (vec-z v) (f64vector-ref v 2)) |
| 64 |
(define-inline (vec-x-set! v x) (f64vector-set! v 0 x)) |
| 65 |
(define-inline (vec-y-set! v y) (f64vector-set! v 1 y)) |
| 66 |
(define-inline (vec-z-set! v z) (f64vector-set! v 2 z)) |
| 67 |
(define-inline (make-vec) (make-f64vector 3)) |
| 68 |
|
| 69 |
(define-inline (vdot v0 v1) (f64vector-dot v0 v1)) |
| 70 |
(define-inline (vcross! v v0 v1) |
| 71 |
(f64vector-set! v 0 (- (* (vec-y v0) (vec-z v1)) (* (vec-z v0) (vec-y v1)))) |
| 72 |
(f64vector-set! v 1 (- (* (vec-z v0) (vec-x v1)) (* (vec-x v0) (vec-z v1)))) |
| 73 |
(f64vector-set! v 2 (- (* (vec-x v0) (vec-y v1)) (* (vec-y v0) (vec-x v1))))) |
| 74 |
(define-inline (vnormalize! v) |
| 75 |
(let1 size (real-sqrt (vdot v v)) |
| 76 |
(when (> (abs size) 1.0e-17) |
| 77 |
(f64vector-div! v size)))) |
| 78 |
(define-inline (vec-diff v0 v1) (f64vector-sub v0 v1)) |
| 79 |
(define-inline (vec-mul! v0 x) (f64vector-mul! v0 x)) |
| 80 |
(define-inline (vec-add! v0 v1) (f64vector-add! v0 v1)) |
| 81 |
(define-inline (vec-sub! v0 v1) (f64vector-sub! v0 v1)) |
| 82 |
(define-inline (vec-set! v0 v1) (f64vector-copy! v0 0 v1)) |
| 83 |
|
| 84 |
;(define-record-type isect #t #t (t) (p) (n) (hit)) |
| 85 |
(define (make-isect) (vector 1.0e+17 (make-vec) (make-vec) #f)) |
| 86 |
(define-inline (isect-t isect) (vector-ref isect 0)) |
| 87 |
(define-inline (isect-p isect) (vector-ref isect 1)) |
| 88 |
(define-inline (isect-n isect) (vector-ref isect 2)) |
| 89 |
(define-inline (isect-hit isect) (vector-ref isect 3)) |
| 90 |
(define-inline (isect-t-set! isect t) (vector-set! isect 0 t)) |
| 91 |
(define-inline (isect-hit-set! isect hit) (vector-set! isect 3 hit)) |
| 92 |
(define-inline (isect-init! isect) |
| 93 |
(isect-t-set! isect 1.0e+17) |
| 94 |
(isect-hit-set! isect #f)) |
| 95 |
|
| 96 |
(define (make-sphere x y z r) (cons (vec x y z) r)) |
| 97 |
(define-inline sphere-center car) |
| 98 |
(define-inline sphere-radius cdr) |
| 99 |
|
| 100 |
(define make-plane cons) |
| 101 |
(define-inline plane-p car) |
| 102 |
(define-inline plane-n cdr) |
| 103 |
|
| 104 |
(define make-ray cons) |
| 105 |
(define-inline ray-org car) |
| 106 |
(define-inline ray-dir cdr) |
| 107 |
|
| 108 |
(define *spheres* |
| 109 |
`(,(make-sphere -2.0 0.0 -3.5 0.5) |
| 110 |
,(make-sphere -0.5 0.0 -3.0 0.5) |
| 111 |
,(make-sphere 1.0 0.0 -2.2 0.5))) |
| 112 |
|
| 113 |
(define *plane* |
| 114 |
(make-plane (vec 0.0 -0.5 0.0) (vec 0.0 1.0 0.0))) |
| 115 |
|
| 116 |
(define (ray-sphere-intersect! isect ray sphere) ; modify isect |
| 117 |
(let* ([rs (vec-diff (ray-org ray) (sphere-center sphere))] |
| 118 |
[B (vdot rs (ray-dir ray))] |
| 119 |
[C (- (vdot rs rs) (square (sphere-radius sphere)))] |
| 120 |
[D (- (* B B) C)]) |
| 121 |
(when (> D 0.0) |
| 122 |
(let1 t (- (+ B (real-sqrt D))) |
| 123 |
(when (< 0.0 t (isect-t isect)) |
| 124 |
(isect-t-set! isect t) |
| 125 |
(isect-hit-set! isect #t) |
| 126 |
(let ([p (isect-p isect)] |
| 127 |
[n (isect-n isect)]) |
| 128 |
(vec-set! p (ray-dir ray)) |
| 129 |
(vec-mul! p t) |
| 130 |
(vec-add! p (ray-org ray)) |
| 131 |
(vec-set! n p) |
| 132 |
(vec-sub! n (sphere-center sphere)) |
| 133 |
(vnormalize! n))))))) |
| 134 |
|
| 135 |
(define (ray-plane-intersect! isect ray plane) ; modify isect |
| 136 |
(let* ([d (- (vdot (plane-p plane) (plane-n plane)))] |
| 137 |
[v (vdot (ray-dir ray) (plane-n plane))]) |
| 138 |
(unless (< (abs v) 1.0e-17) |
| 139 |
(let1 t (/ (- (+ (vdot (ray-org ray) (plane-n plane)) d)) v) |
| 140 |
(when (< 0.0 t (isect-t isect)) |
| 141 |
(isect-t-set! isect t) |
| 142 |
(isect-hit-set! isect #t) |
| 143 |
(let ([p (isect-p isect)] |
| 144 |
[n (isect-n isect)]) |
| 145 |
(vec-set! p (ray-dir ray)) |
| 146 |
(vec-mul! p t) |
| 147 |
(vec-add! p (ray-org ray)) |
| 148 |
(vec-set! n (plane-n plane)))))))) |
| 149 |
|
| 150 |
;; initialize three vectors b0, b1 and b2 |
| 151 |
(define (ortho-basis! b0 b1 b2 n) |
| 152 |
(vec-set! b2 n) |
| 153 |
(cond [(< -0.6 (vec-x n) 0.6) (vec-set! b1 '#f64(1.0 0.0 0.0))] |
| 154 |
[(< -0.6 (vec-y n) 0.6) (vec-set! b1 '#f64(0.0 1.0 0.0))] |
| 155 |
[(< -0.6 (vec-z n) 0.6) (vec-set! b1 '#f64(0.0 0.0 1.0))] |
| 156 |
[else (vec-set! b1 '#f64(1.0 0.0 0.0))]) |
| 157 |
(vcross! b0 b1 b2) |
| 158 |
(vnormalize! b0) |
| 159 |
(vcross! b1 b2 b0) |
| 160 |
(vnormalize! b1)) |
| 161 |
|
| 162 |
;; `Out' is a vector of 5 vecs; the first one is used to store |
| 163 |
;; the result color; the rest is used to hold intermediate values. |
| 164 |
(define (ambient-occlusion! out isect occ-isect) |
| 165 |
(let ([ntheta NAO_SAMPLES] |
| 166 |
[nphi NAO_SAMPLES] |
| 167 |
[eps 0.0001] |
| 168 |
[col (vector-ref out 0)] |
| 169 |
[p (vector-ref out 1)] |
| 170 |
[b0 (vector-ref out 2)] |
| 171 |
[b1 (vector-ref out 3)] |
| 172 |
[b2 (vector-ref out 4)] |
| 173 |
[occlusion 0.0]) |
| 174 |
(vec-set! p (isect-n isect)) |
| 175 |
(vec-mul! p eps) |
| 176 |
(vec-add! p (isect-p isect)) |
| 177 |
(ortho-basis! b0 b1 b2 (isect-n isect)) |
| 178 |
(dotimes [j ntheta] |
| 179 |
(dotimes [i nphi] |
| 180 |
(let ([theta (real-sqrt (random-real))] |
| 181 |
[phi (* 2.0 pi (random-real))]) |
| 182 |
(let ([x (* (real-cos phi) theta)] |
| 183 |
[y (* (real-sin phi) theta)] |
| 184 |
[z (real-sqrt (- 1.0 (* theta theta)))]) |
| 185 |
(let ([rx (+ (* x (vec-x b0)) (* y (vec-x b1)) (* z (vec-x b2)))] |
| 186 |
[ry (+ (* x (vec-y b0)) (* y (vec-y b1)) (* z (vec-y b2)))] |
| 187 |
[rz (+ (* x (vec-z b0)) (* y (vec-z b1)) (* z (vec-z b2)))]) |
| 188 |
(let1 ray (make-ray p (vec rx ry rz)) |
| 189 |
(isect-init! occ-isect) |
| 190 |
(dolist [sphere *spheres*] |
| 191 |
(ray-sphere-intersect! occ-isect ray sphere)) |
| 192 |
(ray-plane-intersect! occ-isect ray *plane*) |
| 193 |
(when (isect-hit occ-isect) (inc! occlusion)))))))) |
| 194 |
(let1 occ (/ (- (* ntheta nphi) occlusion) |
| 195 |
(* ntheta nphi)) |
| 196 |
(vec-x-set! col occ) |
| 197 |
(vec-y-set! col occ) |
| 198 |
(vec-z-set! col occ) |
| 199 |
))) |
| 200 |
|
| 201 |
(define (render-1 fimg y w h nsubsamples) |
| 202 |
(let ([buf (vector-tabulate 5 (^_ (make-vec)))] |
| 203 |
[isect (make-isect)] |
| 204 |
[o-isect (make-isect)]) |
| 205 |
(dotimes [x w] |
| 206 |
(define off (* 3 (+ (* y w) x))) |
| 207 |
(dotimes [v nsubsamples] |
| 208 |
(dotimes [u nsubsamples] |
| 209 |
(let* ([px (/ (+ x (- (/. u nsubsamples) (/ w 2.0))) (/ w 2.0))] |
| 210 |
[py (/ (- (+ y (- (/. v nsubsamples) (/ h 2.0)))) (/ h 2.0))] |
| 211 |
[ray (make-ray '#f64(0 0 0) (vec px py -1.0))]) |
| 212 |
(vnormalize! (ray-dir ray)) |
| 213 |
(isect-init! isect) |
| 214 |
(dolist [sphere *spheres*] |
| 215 |
(ray-sphere-intersect! isect ray sphere)) |
| 216 |
(ray-plane-intersect! isect ray *plane*) |
| 217 |
(when (isect-hit isect) |
| 218 |
(ambient-occlusion! buf isect o-isect) |
| 219 |
(let1 col (vector-ref buf 0) |
| 220 |
($ f64vector-set! fimg (+ off 0) |
| 221 |
$ + (f64vector-ref fimg (+ off 0)) (vec-x col)) |
| 222 |
($ f64vector-set! fimg (+ off 1) |
| 223 |
$ + (f64vector-ref fimg (+ off 1)) (vec-y col)) |
| 224 |
($ f64vector-set! fimg (+ off 2) |
| 225 |
$ + (f64vector-ref fimg (+ off 2)) (vec-z col)))))))))) |
| 226 |
|
| 227 |
(define (render img w h nsubsamples) |
| 228 |
(let ([fimg (make-f64vector (* w h 3))] |
| 229 |
[nprocs (sys-available-processors)]) |
| 230 |
(if (= nprocs 1) |
| 231 |
(dotimes [y h] (render-1 fimg y w h nsubsamples)) |
| 232 |
(let1 pool (make-thread-pool nprocs) |
| 233 |
(dotimes [y h] (add-job! pool (^[] (render-1 fimg y w h nsubsamples)))) |
| 234 |
(wait-all pool #f #e1e8) |
| 235 |
(terminate-all! pool))) |
| 236 |
(f64vector-div! fimg (/ (* nsubsamples nsubsamples) 255.5)) |
| 237 |
(dotimes [i (* w h 3)] |
| 238 |
(u8vector-set! img i (clamp (f64vector-ref fimg i) 0 255))))) |
| 239 |
|
| 240 |
(define (save-ppm name w h img) |
| 241 |
(with-output-to-file name |
| 242 |
(^[] |
| 243 |
(format #t "P6\n~d ~d\n255\n" w h) |
| 244 |
(write-uvector img)))) |
| 245 |
|
| 246 |
(define (main args) |
| 247 |
(let1 img (make-u8vector (* 3 WIDTH HEIGHT)) |
| 248 |
(render img WIDTH HEIGHT NSUBSAMPLES) |
| 249 |
(save-ppm "ao.ppm" WIDTH HEIGHT img) |
| 250 |
0)) |