AtlatestRepositorysigil-geometry
sigil-geometry / tree / harness / src / geometry-harnessalloc.sgl
1
;;; (geometry-harness alloc) - allocation check for the ! forms2
;;;3
;;; Each check calls one operation CALLS times on preallocated values4
;;; and reads the GC's allocation counter before and after. The same5
;;; loop around an empty thunk is the baseline, subtracted from every6
;;; row, so the loop, the closure call and `gc-stats` itself cancel out.7
;;;8
;;; A positive control (the allocating `vec3-add`) must show at least9
;;; one allocation per call; if it does not, the counter is not10
;;; measuring anything and the check reports SETUP-FAILED rather than11
;;; a pass.13
(define-library (geometry-harness alloc)14
(import (sigil core) (sigil io) (sigil gc) (sigil geometry))15
(export run-alloc-check)16
(begin17
(define calls 1000)19
;; An op "allocates" if it shows more than this many allocations over20
;; CALLS calls (1%): room for a stray GC bookkeeping object, far21
;; below one per call.22
(define tolerance 10)24
(define (allocations) (cdr (assq 'allocations (gc-stats))))26
(define (repeat thunk)27
(let loop ((i 0))28
(when (< i calls)29
(thunk)30
(loop (+ i 1)))))32
(define (measure thunk)33
(thunk)34
(let* ((before (allocations))35
(_ (repeat thunk))36
(after (allocations)))37
(- after before)))39
;; Inputs and destinations, made once.40
(define a3 (vec3 1 2 3))41
(define b3 (vec3 -4 5 0.5))42
(define d3 (make-vec3))43
(define a2 (vec2 1 2))44
(define b2 (vec2 3 -1))45
(define d2 (make-vec2))46
(define a4 (vec4 1 2 3 4))47
(define b4 (vec4 4 3 2 1))48
(define d4 (make-vec4))49
(define qa (quat-from-axis-angle (vec3 1 2 3) 0.7))50
(define qb (quat-from-axis-angle (vec3 0 1 0) -1.2))51
(define qd (make-quat))52
(define ma (mat4-from-trs (vec3 1 2 3) qa (vec3 1 2 0.5)))53
(define mb (mat4-look-at (vec3 3 4 5) (vec3 0 0 0) (vec3 0 1 0)))54
(define md (make-mat4))55
(define na (mat3-from-mat4 ma))56
(define nd (make-mat3))57
(define std (make-bytevector 48 0))58
(define plane (vec4 0 1 0 0))59
(define origin (vec3 0.2 5 0.3))60
(define down (vec3 0 -1 0))61
(define lo (vec3 -1 -1 -1))62
(define hi (vec3 1 1 1))63
(define t0 (vec3 0 0 0))64
(define t1 (vec3 1 0 0))65
(define t2 (vec3 0 0 1))66
(define eye (vec3 2 1.7 6))67
(define target (vec3 0 1 0))68
(define up (vec3 0 1 0))69
(define axis (vec3 1 1 0))70
(define scale (vec3 2 2 2))72
;; Inputs that force the other branches.73
(define proj (mat4-perspective 1.2 1.777 0.1 100.0)) ; w != 1: the divide74
(define far-point (vec3 0.3 -0.2 -7.0))75
(define singular4 (mat4-scaling (vec3 1 0 1)))76
(define singular3 (mat3 1 2 3 2 4 6 0 0 1))77
(define zero3 (make-vec3))78
(define zero2 (make-vec2))79
(define zero4 (make-vec4))80
(define zero-q (quat 0 0 0 0))81
(define straight-up (vec3 0 10 0)) ; look-at along up82
(define rx-pi (mat4-rotation-x 3.0)) ; Shoemake x branch83
(define ry-pi (mat4-rotation-y 3.0)) ; y branch84
(define rz-pi (mat4-rotation-z 3.0)) ; z branch85
(define n3 (mat3-from-mat4 rx-pi))86
(define qa-near (quat-from-axis-angle (vec3 1 2 3) 0.7001)) ; slerp's nlerp branch87
(define qa-neg (quat (- (quat-x qa)) (- (quat-y qa)) (- (quat-z qa)) (- (quat-w qa))))88
(define away (vec3 0 1 0)) ; rays pointing away89
(define sideways (vec3 1 0 0)) ; parallel to the plane90
(define p4 (vec4 5 2 -7 0))92
(define checks93
(list94
(cons "vec2-add!" (lambda () (vec2-add! d2 a2 b2)))95
(cons "vec2-normalize!" (lambda () (vec2-normalize! d2 a2)))96
(cons "vec3-add!" (lambda () (vec3-add! d3 a3 b3)))97
(cons "vec3-sub!" (lambda () (vec3-sub! d3 a3 b3)))98
(cons "vec3-scale!" (lambda () (vec3-scale! d3 a3 2.5)))99
(cons "vec3-add-scaled!" (lambda () (vec3-add-scaled! d3 a3 b3 0.5)))100
(cons "vec3-cross!" (lambda () (vec3-cross! d3 a3 b3)))101
(cons "vec3-normalize!" (lambda () (vec3-normalize! d3 b3)))102
(cons "vec3-lerp!" (lambda () (vec3-lerp! d3 a3 b3 0.3)))103
(cons "vec3-dot" (lambda () (vec3-dot a3 b3)))104
(cons "vec3-length" (lambda () (vec3-length b3)))105
(cons "vec3-distance" (lambda () (vec3-distance a3 b3)))106
(cons "vec3-min!" (lambda () (vec3-min! d3 a3 b3)))107
(cons "vec4-lerp!" (lambda () (vec4-lerp! d4 a4 b4 0.5)))108
(cons "vec4-normalize!" (lambda () (vec4-normalize! d4 a4)))109
(cons "mat4-mul!" (lambda () (mat4-mul! md ma mb)))110
(cons "mat4-mul! (aliased)" (lambda () (mat4-mul! md md mb)))111
(cons "mat4-transpose!" (lambda () (mat4-transpose! md ma)))112
(cons "mat4-inverse!" (lambda () (mat4-inverse! md ma)))113
(cons "mat4-determinant" (lambda () (mat4-determinant ma)))114
(cons "mat4-identity!" (lambda () (mat4-identity! md)))115
(cons "mat4-copy!" (lambda () (mat4-copy! md ma)))116
(cons "mat4-translation!" (lambda () (mat4-translation! md a3)))117
(cons "mat4-scaling!" (lambda () (mat4-scaling! md scale)))118
(cons "mat4-rotation-y!" (lambda () (mat4-rotation-y! md 0.4)))119
(cons "mat4-rotation!" (lambda () (mat4-rotation! md axis 0.4)))120
(cons "mat4-from-quat!" (lambda () (mat4-from-quat! md qa)))121
(cons "mat4-from-trs!" (lambda () (mat4-from-trs! md a3 qa scale)))122
(cons "mat4-transform-point!" (lambda () (mat4-transform-point! d3 ma a3)))123
(cons "mat4-transform-direction!" (lambda () (mat4-transform-direction! d3 ma a3)))124
(cons "mat4-transform-vec4!" (lambda () (mat4-transform-vec4! d4 ma a4)))125
(cons "mat4-perspective!" (lambda () (mat4-perspective! md 1.2 1.777 0.1 100.0)))126
(cons "mat4-orthographic!" (lambda () (mat4-orthographic! md 0.0 480.0 0.0 270.0 -1.0 1.0)))127
(cons "mat4-look-at!" (lambda () (mat4-look-at! md eye target up)))128
(cons "mat3-mul!" (lambda () (mat3-mul! nd na na)))129
(cons "mat3-inverse!" (lambda () (mat3-inverse! nd na)))130
(cons "mat3-normal-matrix!" (lambda () (mat3-normal-matrix! nd ma)))131
(cons "mat3-from-mat4!" (lambda () (mat3-from-mat4! nd ma)))132
(cons "mat3-from-quat!" (lambda () (mat3-from-quat! nd qa)))133
(cons "mat3-transform-vec3!" (lambda () (mat3-transform-vec3! d3 na a3)))134
(cons "mat3->std140!" (lambda () (mat3->std140! std na)))135
(cons "quat-mul!" (lambda () (quat-mul! qd qa qb)))136
(cons "quat-slerp!" (lambda () (quat-slerp! qd qa qb 0.3)))137
(cons "quat-nlerp!" (lambda () (quat-nlerp! qd qa qb 0.3)))138
(cons "quat-normalize!" (lambda () (quat-normalize! qd qa)))139
(cons "quat-conjugate!" (lambda () (quat-conjugate! qd qa)))140
(cons "quat-from-axis-angle!" (lambda () (quat-from-axis-angle! qd axis 0.9)))141
(cons "quat-from-mat4!" (lambda () (quat-from-mat4! qd mb)))142
(cons "quat-rotate-vec3!" (lambda () (quat-rotate-vec3! d3 qa a3)))143
(cons "ray-point!" (lambda () (ray-point! d3 origin down 2.0)))144
(cons "ray-plane" (lambda () (ray-plane origin down plane)))145
(cons "ray-aabb" (lambda () (ray-aabb origin down lo hi)))146
(cons "ray-triangle" (lambda () (ray-triangle origin down t0 t1 t2)))147
(cons "ray-triangle-hit!" (lambda () (ray-triangle-hit! d3 origin down t0 t1 t2)))148
(cons "plane-signed-distance" (lambda () (plane-signed-distance plane a3)))149
;; the rest of the exported ! forms150
(cons "mat3-copy!" (lambda () (mat3-copy! nd na)))151
(cons "mat3-identity!" (lambda () (mat3-identity! nd)))152
(cons "mat3-set!" (lambda () (mat3-set! nd 1 2 0.5)))153
(cons "mat3-transpose!" (lambda () (mat3-transpose! nd na)))154
(cons "mat4-get-translation!" (lambda () (mat4-get-translation! d3 ma)))155
(cons "mat4-rotation-x!" (lambda () (mat4-rotation-x! md 0.4)))156
(cons "mat4-rotation-z!" (lambda () (mat4-rotation-z! md 0.4)))157
(cons "mat4-set!" (lambda () (mat4-set! md 3 1 2.5)))158
(cons "plane-from-point-normal!" (lambda () (plane-from-point-normal! d4 a3 up)))159
(cons "plane-from-points!" (lambda () (plane-from-points! d4 t0 t1 t2)))160
(cons "quat-copy!" (lambda () (quat-copy! qd qa)))161
(cons "quat-from-mat3!" (lambda () (quat-from-mat3! qd na)))162
(cons "quat-identity!" (lambda () (quat-identity! qd)))163
(cons "quat-inverse!" (lambda () (quat-inverse! qd qa)))164
(cons "quat-set!" (lambda () (quat-set! qd 0.0 0.0 0.0 1.0)))165
(cons "vec2-copy!" (lambda () (vec2-copy! d2 a2)))166
(cons "vec2-lerp!" (lambda () (vec2-lerp! d2 a2 b2 0.3)))167
(cons "vec2-mul!" (lambda () (vec2-mul! d2 a2 b2)))168
(cons "vec2-negate!" (lambda () (vec2-negate! d2 a2)))169
(cons "vec2-scale!" (lambda () (vec2-scale! d2 a2 1.5)))170
(cons "vec2-set!" (lambda () (vec2-set! d2 1.0 2.0)))171
(cons "vec2-sub!" (lambda () (vec2-sub! d2 a2 b2)))172
(cons "vec3-copy!" (lambda () (vec3-copy! d3 a3)))173
(cons "vec3-max!" (lambda () (vec3-max! d3 a3 b3)))174
(cons "vec3-mul!" (lambda () (vec3-mul! d3 a3 b3)))175
(cons "vec3-negate!" (lambda () (vec3-negate! d3 a3)))176
(cons "vec3-set!" (lambda () (vec3-set! d3 1.0 2.0 3.0)))177
(cons "vec4-add!" (lambda () (vec4-add! d4 a4 b4)))178
(cons "vec4-copy!" (lambda () (vec4-copy! d4 a4)))179
(cons "vec4-mul!" (lambda () (vec4-mul! d4 a4 b4)))180
(cons "vec4-negate!" (lambda () (vec4-negate! d4 a4)))181
(cons "vec4-scale!" (lambda () (vec4-scale! d4 a4 2.0)))182
(cons "vec4-set!" (lambda () (vec4-set! d4 1.0 2.0 3.0 4.0)))183
(cons "vec4-sub!" (lambda () (vec4-sub! d4 a4 b4)))184
;; other branches of ops above185
(cons "mat4-transform-point! (projection, divides by w)"186
(lambda () (mat4-transform-point! d3 proj far-point)))187
(cons "mat4-inverse! (singular)" (lambda () (mat4-inverse! md singular4)))188
(cons "mat3-inverse! (singular)" (lambda () (mat3-inverse! nd singular3)))189
(cons "mat3-normal-matrix! (singular)" (lambda () (mat3-normal-matrix! nd singular4)))190
(cons "mat4-look-at! (up parallel to view)" (lambda () (mat4-look-at! md straight-up t0 up)))191
(cons "mat4-look-at! (eye = target)" (lambda () (mat4-look-at! md t0 t0 up)))192
(cons "quat-from-mat4! (x branch)" (lambda () (quat-from-mat4! qd rx-pi)))193
(cons "quat-from-mat4! (y branch)" (lambda () (quat-from-mat4! qd ry-pi)))194
(cons "quat-from-mat4! (z branch)" (lambda () (quat-from-mat4! qd rz-pi)))195
(cons "quat-from-mat3! (x branch)" (lambda () (quat-from-mat3! qd n3)))196
(cons "quat-slerp! (nearly equal, nlerp branch)" (lambda () (quat-slerp! qd qa qa-near 0.3)))197
(cons "quat-slerp! (opposite signs)" (lambda () (quat-slerp! qd qa qa-neg 0.3)))198
(cons "quat-normalize! (zero)" (lambda () (quat-normalize! qd zero-q)))199
(cons "quat-inverse! (zero)" (lambda () (quat-inverse! qd zero-q)))200
(cons "vec2-normalize! (zero)" (lambda () (vec2-normalize! d2 zero2)))201
(cons "vec3-normalize! (zero)" (lambda () (vec3-normalize! d3 zero3)))202
(cons "vec4-normalize! (zero)" (lambda () (vec4-normalize! d4 zero4)))203
(cons "mat4-rotation! (zero axis)" (lambda () (mat4-rotation! md zero3 0.4)))204
(cons "plane-from-point-normal! (aliased)" (lambda () (plane-from-point-normal! p4 p4 up)))205
(cons "ray-plane (parallel)" (lambda () (ray-plane origin sideways plane)))206
(cons "ray-plane (pointing away)" (lambda () (ray-plane origin away plane)))207
(cons "ray-aabb (miss)" (lambda () (ray-aabb origin away lo hi)))208
(cons "ray-aabb (inside)" (lambda () (ray-aabb t0 away lo hi)))209
(cons "ray-triangle (miss)" (lambda () (ray-triangle origin away t0 t1 t2)))210
(cons "ray-triangle-hit! (miss)" (lambda () (ray-triangle-hit! d3 origin away t0 t1 t2)))211
(cons "aabb-contains-point?" (lambda () (aabb-contains-point? lo hi a3)))212
(cons "aabb-overlap?" (lambda () (aabb-overlap? lo hi t0 hi)))213
(cons "vec3-approx=?" (lambda () (vec3-approx=? a3 b3)))214
(cons "mat4-approx=?" (lambda () (mat4-approx=? ma mb)))215
(cons "mat3-approx=?" (lambda () (mat3-approx=? na na)))216
(cons "quat-approx=?" (lambda () (quat-approx=? qa qa)))217
(cons "quat-same-rotation?" (lambda () (quat-same-rotation? qa qa-neg)))218
(cons "float-approx=?" (lambda () (float-approx=? 1.0 1.0)))219
(cons "vec4-approx=?" (lambda () (vec4-approx=? a4 a4)))220
))222
;;; Run every check. Prints one line per op and returns a list:223
;;; (checked allocating-count control-ok?).224
(define (run-alloc-check)225
(let* ((baseline (measure (lambda () #f)))226
(control (- (measure (lambda () (vec3-add a3 b3))) baseline))227
;; one bytevector per call, less the same noise allowance228
(control-ok? (>= control (- calls tolerance))))229
(println "alloc: baseline ~a allocations per ~a empty calls" baseline calls)230
(println "alloc: control vec3-add (allocating form) ~a over ~a calls ~a"231
control calls (if control-ok? "(counter works)" "(COUNTER NOT MEASURING)"))232
(let loop ((cs checks) (checked 0) (bad 0))233
(if (null? cs)234
(list checked bad control-ok?)235
(let* ((name (car (car cs)))236
(delta (- (measure (cdr (car cs))) baseline))237
(allocates? (> delta tolerance)))238
(println "alloc: ~a ~a ~a" (if allocates? "ALLOCATES" "zero") name delta)239
(loop (cdr cs) (+ checked 1) (if allocates? (+ bad 1) bad)))))))))