From 3d1c4216ede8c628558c9fe700fb2be1aa08b473 Mon Sep 17 00:00:00 2001 From: Kartik Agaram Date: Mon, 2 Nov 2020 21:24:53 -0800 Subject: 7162 --- html/apps/raytracing/1.mu.html | 10 +- html/apps/raytracing/2.mu.html | 12 +- html/apps/raytracing/3.mu.html | 890 ++++++++++++++++++++----------------- html/apps/raytracing/color.mu.html | 46 +- html/apps/raytracing/ray.mu.html | 2 +- html/apps/raytracing/vec.mu.html | 186 ++++++-- 6 files changed, 649 insertions(+), 497 deletions(-) (limited to 'html/apps/raytracing') diff --git a/html/apps/raytracing/1.mu.html b/html/apps/raytracing/1.mu.html index 8361aa42..de84e398 100644 --- a/html/apps/raytracing/1.mu.html +++ b/html/apps/raytracing/1.mu.html @@ -65,8 +65,8 @@ if ('onhashchange' in window) { 7 # $ ./translate_mu apps/raytracing/1.mu 8 # $ ./a.elf > 1.ppm 9 -10 fn main -> exit-status/ebx: int { -11 print-string 0, "P3\n256 256\n255\n" +10 fn main -> _/ebx: int { +11 print-string 0, "P3\n256 256\n255\n" 12 var j/ecx: int <- copy 0xff 13 { 14 compare j, 0 @@ -76,16 +76,16 @@ if ('onhashchange' in window) { 18 compare i, 0xff 19 break-if-> 20 print-int32-decimal 0, i -21 print-string 0, " " +21 print-string 0, " " 22 print-int32-decimal 0, j -23 print-string 0, " 64\n" +23 print-string 0, " 64\n" 24 i <- increment 25 loop 26 } 27 j <- decrement 28 loop 29 } -30 exit-status <- copy 1 +30 return 0 31 } diff --git a/html/apps/raytracing/2.mu.html b/html/apps/raytracing/2.mu.html index 66e60f05..5bc16803 100644 --- a/html/apps/raytracing/2.mu.html +++ b/html/apps/raytracing/2.mu.html @@ -65,8 +65,8 @@ if ('onhashchange' in window) { 6 # $ ./translate_mu apps/raytracing/2.mu 7 # $ ./a.elf > 2.ppm 8 - 9 fn main -> exit-status/ebx: int { -10 print-string 0, "P3\n256 256\n255\n" + 9 fn main -> _/ebx: int { +10 print-string 0, "P3\n256 256\n255\n" 11 var _four/edx: int <- copy 4 12 var four/xmm1: float <- convert _four 13 var one-fourth/xmm1: float <- reciprocal four @@ -106,7 +106,7 @@ if ('onhashchange' in window) { 47 j <- decrement 48 loop 49 } -50 exit-status <- copy 1 +50 return 0 51 } 52 53 type rgb { @@ -127,21 +127,21 @@ if ('onhashchange' in window) { 68 result <- multiply *src-addr 69 var result-int/edx: int <- convert result 70 print-int32-decimal screen, result-int -71 print-string screen, " " +71 print-string screen, " " 72 # print 255 * c->g 73 src-addr <- get c, g 74 result <- copy xn 75 result <- multiply *src-addr 76 result-int <- convert result 77 print-int32-decimal screen, result-int -78 print-string screen, " " +78 print-string screen, " " 79 # print 255 * c->b 80 src-addr <- get c, b 81 result <- copy xn 82 result <- multiply *src-addr 83 result-int <- convert result 84 print-int32-decimal screen, result-int -85 print-string screen, "\n" +85 print-string screen, "\n" 86 } diff --git a/html/apps/raytracing/3.mu.html b/html/apps/raytracing/3.mu.html index 9288e46f..f090649a 100644 --- a/html/apps/raytracing/3.mu.html +++ b/html/apps/raytracing/3.mu.html @@ -14,6 +14,7 @@ pre { white-space: pre-wrap; font-family: monospace; color: #000000; background- body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; } a { color:inherit; } * { font-size:12pt; font-size: 1em; } +.CommentedCode { color: #8a8a8a; } .muComment { color: #005faf; } .muS1Comment { color: #0000af; } .LineNr { } @@ -66,423 +67,482 @@ if ('onhashchange' in window) { 6 # $ ./translate_mu apps/raytracing/3.mu 7 # $ ./a.elf > 3.ppm 8 - 9 fn ray-color _in: (addr ray), _out: (addr rgb) { - 10 var in/esi: (addr ray) <- copy _in - 11 var out/edi: (addr rgb) <- copy _out - 12 var dir/eax: (addr vec3) <- get in, dir - 13 var unit-storage: vec3 - 14 var unit/ecx: (addr vec3) <- address unit-storage - 15 vec3-unit dir, unit - 16 var y-addr/eax: (addr float) <- get unit, y - 17 # t = (dir.y + 1.0) / 2.0 - 18 var t/xmm0: float <- copy *y-addr - 19 var one/eax: int <- copy 1 - 20 var one-f/xmm1: float <- convert one - 21 t <- add one-f - 22 var two/eax: int <- copy 2 - 23 var two-f/xmm2: float <- convert two - 24 t <- divide two-f - 25 # whitening = (1.0 - t) * white - 26 var whitening-storage: rgb - 27 var whitening/ecx: (addr rgb) <- address whitening-storage - 28 rgb-white whitening - 29 var one-minus-t/xmm3: float <- copy one-f - 30 one-minus-t <- subtract t - 31 rgb-scale-up whitening, one-minus-t - 32 # out = t * (0.5, 0.7, 1.0) - 33 var dest/eax: (addr float) <- get out, r - 34 fill-in-rational dest, 5, 0xa - 35 dest <- get out, g - 36 fill-in-rational dest, 7, 0xa - 37 dest <- get out, b - 38 copy-to *dest, one-f - 39 rgb-scale-up out, t - 40 # blend with whitening - 41 rgb-add-to out, whitening - 42 } - 43 - 44 fn main -> exit-status/ebx: int { - 45 - 46 # image - 47 # width = 400 - 48 # height = 400 * 9/16 = 225 - 49 var aspect: float - 50 var aspect-addr/eax: (addr float) <- address aspect - 51 fill-in-rational aspect-addr, 0x10, 9 # 16/9 - 52 - 53 # camera - 54 - 55 # viewport-height = 2.0 - 56 var tmp/eax: int <- copy 2 - 57 var two-f/xmm4: float <- convert tmp - 58 var viewport-height/xmm7: float <- copy two-f - 59 # viewport-width = aspect * viewport-height - 60 var viewport-width/xmm6: float <- convert tmp - 61 viewport-width <- multiply aspect - 62 # focal-length = 1.0 - 63 tmp <- copy 1 - 64 var focal-length/xmm5: float <- convert tmp - 65 - 66 # origin = point3(0, 0, 0) - 67 var origin-storage: vec3 - 68 var origin/edi: (addr vec3) <- address origin-storage - 69 # horizontal = vec3(viewport-width, 0, 0) - 70 var horizontal-storage: vec3 - 71 var dest/eax: (addr float) <- get horizontal-storage, x - 72 copy-to *dest, viewport-width - 73 var horizontal/ebx: (addr vec3) <- address horizontal-storage - 74 # vertical = vec3(0, viewport-height, 0) - 75 var vertical-storage: vec3 - 76 dest <- get vertical-storage, y - 77 copy-to *dest, viewport-height - 78 var vertical/edx: (addr vec3) <- address vertical-storage - 79 # lower-left-corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal-length) - 80 # . lower-left-corner = origin - 81 var lower-left-corner-storage: vec3 - 82 var lower-left-corner/esi: (addr vec3) <- address lower-left-corner-storage - 83 copy-object origin, lower-left-corner - 84 # . lower-left-corner -= horizontal/2 - 85 var tmp2: vec3 - 86 var tmp2-addr/eax: (addr vec3) <- address tmp2 - 87 copy-object horizontal, tmp2-addr - 88 vec3-scale-down tmp2-addr, two-f - 89 vec3-subtract-from lower-left-corner, tmp2-addr - 90 # . lower-left-corner -= vertical/2 - 91 copy-object vertical, tmp2-addr - 92 vec3-scale-down tmp2-addr, two-f - 93 vec3-subtract-from lower-left-corner, tmp2-addr - 94 # . lower-left-corner -= vec3(0, 0, focal-length) - 95 var dest2/ecx: (addr float) <- get lower-left-corner, z - 96 var tmp3/xmm0: float <- copy *dest2 - 97 tmp3 <- subtract focal-length - 98 copy-to *dest2, tmp3 - 99 # phew! -100 -101 # render -102 -103 # live variables at this point: -104 # origin (edi) -105 # lower-left-corner (esi) -106 # horizontal (ebx) -107 # vertical (edx) -108 # floating-point registers are all free -109 print-string 0, "P3\n400 225\n255\n" # 225 = image height -110 var tmp/eax: int <- copy 0x18f # image width - 1 -111 var image-width-1/xmm7: float <- convert tmp -112 tmp <- copy 0xe0 # image height - 1 -113 var image-height-1/xmm6: float <- convert tmp -114 # -115 var j/ecx: int <- copy 0xe0 # 224 -116 { -117 compare j, 0 -118 break-if-< -119 var i/eax: int <- copy 0 -120 { -121 compare i, 0x190 # 400 = image width -122 break-if->= -123 # u = i / (image-width - 1) -124 var u/xmm0: float <- convert i -125 u <- divide image-width-1 -126 # v = j / (image-height - 1) -127 var v/xmm1: float <- convert j -128 v <- divide image-height-1 -129 # r = ray(origin, lower-left-corner + u*horizontal + v*vertical - origin) -130 var r-storage: ray -131 # . . we're running out of int registers now, -132 # . . but luckily we don't need i and j in the rest of this loop iteration, -133 # . . so we'll just spill them in a block -134 { -135 # . r.orig = origin -136 var r/eax: (addr ray) <- address r-storage -137 var dest/ecx: (addr vec3) <- get r, orig -138 copy-object origin, dest -139 # . r.dir = lower-left-corner -140 dest <- get r, dir -141 copy-object lower-left-corner, dest -142 # . r.dir += horizontal*u -143 var tmp-vec3: vec3 -144 var tmp/eax: (addr vec3) <- address tmp-vec3 -145 copy-object horizontal, tmp -146 vec3-scale-up tmp, u -147 vec3-add-to dest, tmp -148 # . r.dir += vertical*v -149 copy-object vertical, tmp -150 vec3-scale-up tmp, v -151 vec3-add-to dest, tmp -152 # . r.dir -= origin -153 vec3-subtract-from dest, origin -154 } -155 # pixel-color = ray-color(r) -156 var c-storage: rgb -157 var c/ecx: (addr rgb) <- address c-storage -158 { -159 var r/eax: (addr ray) <- address r-storage -160 ray-color r, c -161 # write color -162 print-rgb 0, c -163 } -164 i <- increment -165 loop -166 } -167 j <- decrement -168 loop -169 } -170 exit-status <- copy 1 -171 } -172 -173 type ray { -174 orig: vec3 # point -175 dir: vec3 -176 } -177 -178 # A little different from the constructor at https://raytracing.github.io/books/RayTracingInOneWeekend.html -179 # We immediately normalize the direction vector so we don't have to keep doing -180 # so. -181 fn initialize-ray _self: (addr ray), o: (addr vec3), d: (addr vec3) { -182 var self/esi: (addr ray) <- copy _self -183 var dest/eax: (addr vec3) <- get self, orig -184 copy-object o, dest -185 dest <- get self, dir -186 vec3-unit d, dest -187 } -188 -189 fn ray-at _self: (addr ray), t: float, out: (addr vec3) { -190 var self/esi: (addr ray) <- copy _self -191 var src/eax: (addr vec3) <- get self, dir -192 copy-object src, out -193 vec3-scale-up out, t -194 src <- get self, orig -195 vec3-add-to out, src -196 } -197 -198 type rgb { -199 # components normalized to within [0.0, 1.0] -200 r: float -201 g: float -202 b: float -203 } -204 -205 # print translating to [0, 256) -206 fn print-rgb screen: (addr screen), _c: (addr rgb) { -207 var c/esi: (addr rgb) <- copy _c -208 var xn: float -209 var xn-addr/ecx: (addr float) <- address xn -210 fill-in-rational xn-addr, 0x3e7ff, 0x3e8 # 255999 / 1000 -211 # print 255.999 * c->r -212 var result/xmm0: float <- copy xn -213 var src-addr/eax: (addr float) <- get c, r -214 result <- multiply *src-addr -215 var result-int/edx: int <- truncate result -216 print-int32-decimal screen, result-int -217 print-string screen, " " -218 # print 255.999 * c->g -219 src-addr <- get c, g -220 result <- copy xn -221 result <- multiply *src-addr -222 result-int <- truncate result -223 print-int32-decimal screen, result-int -224 print-string screen, " " -225 # print 255.999 * c->b -226 src-addr <- get c, b -227 result <- copy xn -228 result <- multiply *src-addr -229 result-int <- truncate result -230 print-int32-decimal screen, result-int -231 print-string screen, "\n" -232 } -233 -234 fn print-rgb-raw screen: (addr screen), _v: (addr rgb) { -235 var v/esi: (addr rgb) <- copy _v -236 print-string screen, "(" -237 var tmp/eax: (addr float) <- get v, r -238 print-float screen, *tmp -239 print-string screen, ", " -240 tmp <- get v, g -241 print-float screen, *tmp -242 print-string screen, ", " -243 tmp <- get v, b -244 print-float screen, *tmp -245 print-string screen, ")" -246 } -247 -248 fn rgb-white _c: (addr rgb) { -249 var c/esi: (addr rgb) <- copy _c -250 var one/eax: int <- copy 1 -251 var one-f/xmm0: float <- convert one -252 var dest/edi: (addr float) <- get c, r -253 copy-to *dest, one-f -254 dest <- get c, g -255 copy-to *dest, one-f -256 dest <- get c, b -257 copy-to *dest, one-f -258 } -259 -260 fn rgb-add-to _c1: (addr rgb), _c2: (addr rgb) { -261 var c1/edi: (addr rgb) <- copy _c1 -262 var c2/esi: (addr rgb) <- copy _c2 -263 # c1.r += c2.r -264 var arg1/eax: (addr float) <- get c1, r -265 var arg2/ecx: (addr float) <- get c2, r -266 var result/xmm0: float <- copy *arg1 -267 result <- add *arg2 -268 copy-to *arg1, result -269 # c1.g += c2.g -270 arg1 <- get c1, g -271 arg2 <- get c2, g -272 result <- copy *arg1 -273 result <- add *arg2 -274 copy-to *arg1, result -275 # c1.b += c2.b -276 arg1 <- get c1, b -277 arg2 <- get c2, b -278 result <- copy *arg1 -279 result <- add *arg2 -280 copy-to *arg1, result -281 } -282 -283 fn rgb-scale-up _c1: (addr rgb), f: float { -284 var c1/edi: (addr rgb) <- copy _c1 -285 # c1.r *= f -286 var dest/eax: (addr float) <- get c1, r -287 var result/xmm0: float <- copy *dest -288 result <- multiply f -289 copy-to *dest, result -290 # c1.g *= f -291 dest <- get c1, g -292 result <- copy *dest -293 result <- multiply f -294 copy-to *dest, result -295 # c1.b *= f -296 dest <- get c1, b -297 result <- copy *dest -298 result <- multiply f -299 copy-to *dest, result + 9 fn ray-color _in: (addr ray), _out: (addr rgb) { + 10 var in/esi: (addr ray) <- copy _in + 11 var out/edi: (addr rgb) <- copy _out + 12 var dir/eax: (addr vec3) <- get in, dir + 13 #? print-string 0, "r.dir: " + 14 #? print-vec3 0, dir + 15 #? print-string 0, "\n" + 16 var unit-storage: vec3 + 17 var unit/ecx: (addr vec3) <- address unit-storage + 18 vec3-unit dir, unit + 19 #? print-string 0, "r.dir normalized: " + 20 #? print-vec3 0, unit + 21 #? print-string 0, "\n" + 22 var y-addr/eax: (addr float) <- get unit, y + 23 # t = (dir.y + 1.0) / 2.0 + 24 var t/xmm0: float <- copy *y-addr + 25 var one/eax: int <- copy 1 + 26 var one-f/xmm1: float <- convert one + 27 t <- add one-f + 28 var two/eax: int <- copy 2 + 29 var two-f/xmm2: float <- convert two + 30 t <- divide two-f + 31 #? print-string 0, "t: " + 32 #? print-float 0, t + 33 #? print-string 0, "\n" + 34 # whitening = (1.0 - t) * white + 35 var whitening-storage: rgb + 36 var whitening/ecx: (addr rgb) <- address whitening-storage + 37 rgb-white whitening + 38 var one-minus-t/xmm3: float <- copy one-f + 39 one-minus-t <- subtract t + 40 rgb-scale-up whitening, one-minus-t + 41 #? print-string 0, "whitening: " + 42 #? print-rgb-raw 0, whitening + 43 #? print-string 0, "\n" + 44 # out = t * (0.5, 0.7, 1.0) + 45 var dest/eax: (addr float) <- get out, r + 46 fill-in-rational dest, 5, 0xa + 47 dest <- get out, g + 48 fill-in-rational dest, 7, 0xa + 49 dest <- get out, b + 50 copy-to *dest, one-f + 51 rgb-scale-up out, t + 52 #? print-string 0, "base: " + 53 #? print-rgb-raw 0, out + 54 #? print-string 0, "\n" + 55 # blend with whitening + 56 rgb-add-to out, whitening + 57 #? print-string 0, "result: " + 58 #? print-rgb-raw 0, out + 59 #? print-string 0, "\n" + 60 } + 61 + 62 fn main -> _/ebx: int { + 63 + 64 # image + 65 # width = 400 + 66 # height = 400 * 9/16 = 225 + 67 var aspect: float + 68 var aspect-addr/eax: (addr float) <- address aspect + 69 fill-in-rational aspect-addr, 0x10, 9 # 16/9 + 70 #? print-string 0, "aspect ratio: " + 71 #? print-float 0, aspect + 72 #? print-string 0, " " + 73 #? { + 74 #? var foo2/ebx: int <- reinterpret aspect + 75 #? print-int32-hex 0, foo2 + 76 #? } + 77 #? print-string 0, "\n" + 78 + 79 # camera + 80 + 81 # viewport-height = 2.0 + 82 var tmp/eax: int <- copy 2 + 83 var two-f/xmm4: float <- convert tmp + 84 var viewport-height/xmm7: float <- copy two-f + 85 #? print-string 0, "viewport height: " + 86 #? print-float 0, viewport-height + 87 #? print-string 0, " " + 88 #? { + 89 #? var foo: float + 90 #? copy-to foo, viewport-height + 91 #? var foo2/ebx: int <- reinterpret foo + 92 #? print-int32-hex 0, foo2 + 93 #? } + 94 #? print-string 0, "\n" + 95 # viewport-width = aspect * viewport-height + 96 var viewport-width/xmm6: float <- convert tmp + 97 viewport-width <- multiply aspect + 98 #? print-string 0, "viewport width: " + 99 #? print-float 0, viewport-width +100 #? print-string 0, " " +101 #? { +102 #? var foo: float +103 #? copy-to foo, viewport-width +104 #? var foo2/ebx: int <- reinterpret foo +105 #? print-int32-hex 0, foo2 +106 #? } +107 #? print-string 0, "\n" +108 # focal-length = 1.0 +109 tmp <- copy 1 +110 var focal-length/xmm5: float <- convert tmp +111 +112 # origin = point3(0, 0, 0) +113 var origin-storage: vec3 +114 var origin/edi: (addr vec3) <- address origin-storage +115 # horizontal = vec3(viewport-width, 0, 0) +116 var horizontal-storage: vec3 +117 var dest/eax: (addr float) <- get horizontal-storage, x +118 copy-to *dest, viewport-width +119 var horizontal/ebx: (addr vec3) <- address horizontal-storage +120 # vertical = vec3(0, viewport-height, 0) +121 var vertical-storage: vec3 +122 dest <- get vertical-storage, y +123 copy-to *dest, viewport-height +124 var vertical/edx: (addr vec3) <- address vertical-storage +125 # lower-left-corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal-length) +126 # . lower-left-corner = origin +127 var lower-left-corner-storage: vec3 +128 var lower-left-corner/esi: (addr vec3) <- address lower-left-corner-storage +129 copy-object origin, lower-left-corner +130 # . lower-left-corner -= horizontal/2 +131 var tmp2: vec3 +132 var tmp2-addr/eax: (addr vec3) <- address tmp2 +133 copy-object horizontal, tmp2-addr +134 vec3-scale-down tmp2-addr, two-f +135 vec3-subtract-from lower-left-corner, tmp2-addr +136 # . lower-left-corner -= vertical/2 +137 copy-object vertical, tmp2-addr +138 vec3-scale-down tmp2-addr, two-f +139 vec3-subtract-from lower-left-corner, tmp2-addr +140 # . lower-left-corner -= vec3(0, 0, focal-length) +141 var dest2/ecx: (addr float) <- get lower-left-corner, z +142 var tmp3/xmm0: float <- copy *dest2 +143 tmp3 <- subtract focal-length +144 copy-to *dest2, tmp3 +145 # phew! +146 +147 # render +148 +149 # live variables at this point: +150 # origin (edi) +151 # lower-left-corner (esi) +152 # horizontal (ebx) +153 # vertical (edx) +154 # floating-point registers are all free +155 print-string 0, "P3\n400 225\n255\n" # 225 = image height +156 var tmp/eax: int <- copy 0x18f # image width - 1 +157 var image-width-1/xmm7: float <- convert tmp +158 tmp <- copy 0xe0 # image height - 1 +159 var image-height-1/xmm6: float <- convert tmp +160 # +161 var j/ecx: int <- copy 0xe0 # 224 +162 { +163 compare j, 0 +164 break-if-< +165 var i/eax: int <- copy 0 +166 { +167 compare i, 0x190 # 400 = image width +168 break-if->= +169 # u = i / (image-width - 1) +170 var u/xmm0: float <- convert i +171 u <- divide image-width-1 +172 #? print-string 0, "u: " +173 #? print-float 0, u +174 #? print-string 0, "\n" +175 # v = j / (image-height - 1) +176 var v/xmm1: float <- convert j +177 v <- divide image-height-1 +178 # r = ray(origin, lower-left-corner + u*horizontal + v*vertical - origin) +179 var r-storage: ray +180 # . . we're running out of int registers now, +181 # . . but luckily we don't need i and j in the rest of this loop iteration, +182 # . . so we'll just spill them in a block +183 { +184 # . r.orig = origin +185 var r/eax: (addr ray) <- address r-storage +186 var dest/ecx: (addr vec3) <- get r, orig +187 copy-object origin, dest +188 # . r.dir = lower-left-corner +189 dest <- get r, dir +190 copy-object lower-left-corner, dest +191 # . r.dir += horizontal*u +192 var tmp-vec3: vec3 +193 var tmp/eax: (addr vec3) <- address tmp-vec3 +194 copy-object horizontal, tmp +195 vec3-scale-up tmp, u +196 vec3-add-to dest, tmp +197 # . r.dir += vertical*v +198 copy-object vertical, tmp +199 vec3-scale-up tmp, v +200 vec3-add-to dest, tmp +201 # . r.dir -= origin +202 vec3-subtract-from dest, origin +203 #? print-string 0, "ray direction: " +204 #? print-vec3 0, dest +205 #? print-string 0, "\n" +206 } +207 # pixel-color = ray-color(r) +208 var c-storage: rgb +209 var c/ecx: (addr rgb) <- address c-storage +210 { +211 var r/eax: (addr ray) <- address r-storage +212 ray-color r, c +213 # write color +214 print-rgb 0, c +215 #? print-rgb-raw 0, c +216 #? print-string 0, "\n" +217 } +218 i <- increment +219 loop +220 } +221 j <- decrement +222 loop +223 } +224 return 0 +225 } +226 +227 type ray { +228 orig: vec3 # point +229 dir: vec3 +230 } +231 +232 # A little different from the constructor at https://raytracing.github.io/books/RayTracingInOneWeekend.html +233 # We immediately normalize the direction vector so we don't have to keep doing +234 # so. +235 fn initialize-ray _self: (addr ray), o: (addr vec3), d: (addr vec3) { +236 var self/esi: (addr ray) <- copy _self +237 var dest/eax: (addr vec3) <- get self, orig +238 copy-object o, dest +239 dest <- get self, dir +240 vec3-unit d, dest +241 } +242 +243 fn ray-at _self: (addr ray), t: float, out: (addr vec3) { +244 var self/esi: (addr ray) <- copy _self +245 var src/eax: (addr vec3) <- get self, dir +246 copy-object src, out +247 vec3-scale-up out, t +248 src <- get self, orig +249 vec3-add-to out, src +250 } +251 +252 type rgb { +253 # components normalized to within [0.0, 1.0] +254 r: float +255 g: float +256 b: float +257 } +258 +259 # print translating to [0, 256) +260 fn print-rgb screen: (addr screen), _c: (addr rgb) { +261 var c/esi: (addr rgb) <- copy _c +262 var xn: float +263 var xn-addr/ecx: (addr float) <- address xn +264 fill-in-rational xn-addr, 0x3e7ff, 0x3e8 # 255999 / 1000 +265 # print 255.999 * c->r +266 var result/xmm0: float <- copy xn +267 var src-addr/eax: (addr float) <- get c, r +268 result <- multiply *src-addr +269 var result-int/edx: int <- truncate result +270 print-int32-decimal screen, result-int +271 print-string screen, " " +272 # print 255.999 * c->g +273 src-addr <- get c, g +274 result <- copy xn +275 result <- multiply *src-addr +276 result-int <- truncate result +277 print-int32-decimal screen, result-int +278 print-string screen, " " +279 # print 255.999 * c->b +280 src-addr <- get c, b +281 result <- copy xn +282 result <- multiply *src-addr +283 result-int <- truncate result +284 print-int32-decimal screen, result-int +285 print-string screen, "\n" +286 } +287 +288 fn print-rgb-raw screen: (addr screen), _v: (addr rgb) { +289 var v/esi: (addr rgb) <- copy _v +290 print-string screen, "(" +291 var tmp/eax: (addr float) <- get v, r +292 print-float screen, *tmp +293 print-string screen, ", " +294 tmp <- get v, g +295 print-float screen, *tmp +296 print-string screen, ", " +297 tmp <- get v, b +298 print-float screen, *tmp +299 print-string screen, ")" 300 } 301 -302 type vec3 { -303 x: float -304 y: float -305 z: float -306 } -307 -308 fn print-vec3 screen: (addr screen), _v: (addr vec3) { -309 var v/esi: (addr vec3) <- copy _v -310 print-string screen, "(" -311 var tmp/eax: (addr float) <- get v, x -312 print-float screen, *tmp -313 print-string screen, ", " -314 tmp <- get v, y -315 print-float screen, *tmp -316 print-string screen, ", " -317 tmp <- get v, z -318 print-float screen, *tmp -319 print-string screen, ")" -320 } -321 -322 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) { -323 var v1/edi: (addr vec3) <- copy _v1 -324 var v2/esi: (addr vec3) <- copy _v2 -325 # v1.x += v2.x -326 var arg1/eax: (addr float) <- get v1, x -327 var arg2/ecx: (addr float) <- get v2, x -328 var result/xmm0: float <- copy *arg1 -329 result <- add *arg2 -330 copy-to *arg1, result -331 # v1.y += v2.y -332 arg1 <- get v1, y -333 arg2 <- get v2, y -334 result <- copy *arg1 -335 result <- add *arg2 -336 copy-to *arg1, result -337 # v1.z += v2.z -338 arg1 <- get v1, z -339 arg2 <- get v2, z -340 result <- copy *arg1 -341 result <- add *arg2 -342 copy-to *arg1, result -343 } -344 -345 fn vec3-subtract-from v1: (addr vec3), v2: (addr vec3) { -346 var tmp-storage: vec3 -347 var tmp/eax: (addr vec3) <- address tmp-storage -348 copy-object v2, tmp -349 vec3-negate tmp -350 vec3-add-to v1, tmp -351 } -352 -353 fn vec3-negate v: (addr vec3) { -354 var negative-one/eax: int <- copy -1 -355 var negative-one-f/xmm0: float <- convert negative-one -356 vec3-scale-up v, negative-one-f -357 } -358 -359 fn vec3-scale-up _v1: (addr vec3), f: float { -360 var v1/edi: (addr vec3) <- copy _v1 -361 # v1.x *= f -362 var dest/eax: (addr float) <- get v1, x -363 var result/xmm0: float <- copy *dest -364 result <- multiply f -365 copy-to *dest, result -366 # v1.y *= f -367 dest <- get v1, y -368 result <- copy *dest -369 result <- multiply f -370 copy-to *dest, result -371 # v1.z *= f -372 dest <- get v1, z -373 result <- copy *dest -374 result <- multiply f -375 copy-to *dest, result -376 } -377 -378 fn vec3-scale-down _v1: (addr vec3), f: float { -379 var v1/edi: (addr vec3) <- copy _v1 -380 # v1.x /= f -381 var dest/eax: (addr float) <- get v1, x -382 var result/xmm0: float <- copy *dest -383 result <- divide f -384 copy-to *dest, result -385 # v1.y /= f -386 dest <- get v1, y -387 result <- copy *dest -388 result <- divide f -389 copy-to *dest, result -390 # v1.z /= f -391 dest <- get v1, z -392 result <- copy *dest -393 result <- divide f -394 copy-to *dest, result -395 } -396 -397 fn vec3-unit in: (addr vec3), out: (addr vec3) { -398 var len/xmm0: float <- vec3-length in -399 copy-object in, out -400 vec3-scale-down out, len -401 } -402 -403 fn vec3-length v: (addr vec3) -> result/xmm0: float { -404 result <- vec3-length-squared v -405 result <- square-root result -406 } -407 -408 fn vec3-length-squared _v: (addr vec3) -> result/xmm0: float { -409 var v/esi: (addr vec3) <- copy _v -410 # result = v.x * v.x -411 var src/eax: (addr float) <- get v, x -412 var tmp/xmm1: float <- copy *src -413 tmp <- multiply tmp -414 result <- copy tmp -415 # result += v.y * v.y -416 src <- get v, y -417 tmp <- copy *src -418 tmp <- multiply tmp -419 result <- add tmp -420 # result += v.z * v.z -421 src <- get v, z -422 tmp <- copy *src -423 tmp <- multiply tmp -424 result <- add tmp -425 } +302 fn rgb-white _c: (addr rgb) { +303 var c/esi: (addr rgb) <- copy _c +304 var one/eax: int <- copy 1 +305 var one-f/xmm0: float <- convert one +306 var dest/edi: (addr float) <- get c, r +307 copy-to *dest, one-f +308 dest <- get c, g +309 copy-to *dest, one-f +310 dest <- get c, b +311 copy-to *dest, one-f +312 } +313 +314 fn rgb-add-to _c1: (addr rgb), _c2: (addr rgb) { +315 var c1/edi: (addr rgb) <- copy _c1 +316 var c2/esi: (addr rgb) <- copy _c2 +317 # c1.r += c2.r +318 var arg1/eax: (addr float) <- get c1, r +319 var arg2/ecx: (addr float) <- get c2, r +320 var result/xmm0: float <- copy *arg1 +321 result <- add *arg2 +322 copy-to *arg1, result +323 # c1.g += c2.g +324 arg1 <- get c1, g +325 arg2 <- get c2, g +326 result <- copy *arg1 +327 result <- add *arg2 +328 copy-to *arg1, result +329 # c1.b += c2.b +330 arg1 <- get c1, b +331 arg2 <- get c2, b +332 result <- copy *arg1 +333 result <- add *arg2 +334 copy-to *arg1, result +335 } +336 +337 fn rgb-scale-up _c1: (addr rgb), f: float { +338 var c1/edi: (addr rgb) <- copy _c1 +339 # c1.r *= f +340 var dest/eax: (addr float) <- get c1, r +341 var result/xmm0: float <- copy *dest +342 result <- multiply f +343 copy-to *dest, result +344 # c1.g *= f +345 dest <- get c1, g +346 result <- copy *dest +347 result <- multiply f +348 copy-to *dest, result +349 # c1.b *= f +350 dest <- get c1, b +351 result <- copy *dest +352 result <- multiply f +353 copy-to *dest, result +354 } +355 +356 type vec3 { +357 x: float +358 y: float +359 z: float +360 } +361 +362 fn print-vec3 screen: (addr screen), _v: (addr vec3) { +363 var v/esi: (addr vec3) <- copy _v +364 print-string screen, "(" +365 var tmp/eax: (addr float) <- get v, x +366 print-float screen, *tmp +367 print-string screen, ", " +368 tmp <- get v, y +369 print-float screen, *tmp +370 print-string screen, ", " +371 tmp <- get v, z +372 print-float screen, *tmp +373 print-string screen, ")" +374 } +375 +376 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) { +377 var v1/edi: (addr vec3) <- copy _v1 +378 var v2/esi: (addr vec3) <- copy _v2 +379 # v1.x += v2.x +380 var arg1/eax: (addr float) <- get v1, x +381 var arg2/ecx: (addr float) <- get v2, x +382 var result/xmm0: float <- copy *arg1 +383 result <- add *arg2 +384 copy-to *arg1, result +385 # v1.y += v2.y +386 arg1 <- get v1, y +387 arg2 <- get v2, y +388 result <- copy *arg1 +389 result <- add *arg2 +390 copy-to *arg1, result +391 # v1.z += v2.z +392 arg1 <- get v1, z +393 arg2 <- get v2, z +394 result <- copy *arg1 +395 result <- add *arg2 +396 copy-to *arg1, result +397 } +398 +399 fn vec3-subtract-from v1: (addr vec3), v2: (addr vec3) { +400 var tmp-storage: vec3 +401 var tmp/eax: (addr vec3) <- address tmp-storage +402 copy-object v2, tmp +403 vec3-negate tmp +404 vec3-add-to v1, tmp +405 } +406 +407 fn vec3-negate v: (addr vec3) { +408 var negative-one/eax: int <- copy -1 +409 var negative-one-f/xmm0: float <- convert negative-one +410 vec3-scale-up v, negative-one-f +411 } +412 +413 fn vec3-scale-up _v: (addr vec3), f: float { +414 var v/edi: (addr vec3) <- copy _v +415 # v.x *= f +416 var dest/eax: (addr float) <- get v, x +417 var result/xmm0: float <- copy *dest +418 result <- multiply f +419 copy-to *dest, result +420 # v.y *= f +421 dest <- get v, y +422 result <- copy *dest +423 result <- multiply f +424 copy-to *dest, result +425 # v.z *= f +426 dest <- get v, z +427 result <- copy *dest +428 result <- multiply f +429 copy-to *dest, result +430 } +431 +432 fn vec3-scale-down _v: (addr vec3), f: float { +433 var v/edi: (addr vec3) <- copy _v +434 # v.x /= f +435 var dest/eax: (addr float) <- get v, x +436 var result/xmm0: float <- copy *dest +437 result <- divide f +438 copy-to *dest, result +439 # v.y /= f +440 dest <- get v, y +441 result <- copy *dest +442 result <- divide f +443 copy-to *dest, result +444 # v.z /= f +445 dest <- get v, z +446 result <- copy *dest +447 result <- divide f +448 copy-to *dest, result +449 } +450 +451 fn vec3-unit in: (addr vec3), out: (addr vec3) { +452 var len/xmm0: float <- vec3-length in +453 #? print-string 0, "len: " +454 #? print-float 0, len +455 #? print-string 0, "\n" +456 copy-object in, out +457 vec3-scale-down out, len +458 } +459 +460 fn vec3-length v: (addr vec3) -> _/xmm0: float { +461 var result/xmm0: float <- vec3-length-squared v +462 result <- square-root result +463 return result +464 } +465 +466 fn vec3-length-squared _v: (addr vec3) -> _/xmm0: float { +467 var v/esi: (addr vec3) <- copy _v +468 # result = v.x * v.x +469 var src/eax: (addr float) <- get v, x +470 var tmp/xmm1: float <- copy *src +471 tmp <- multiply tmp +472 var result/xmm0: float <- copy tmp +473 # result += v.y * v.y +474 src <- get v, y +475 tmp <- copy *src +476 tmp <- multiply tmp +477 result <- add tmp +478 # result += v.z * v.z +479 src <- get v, z +480 tmp <- copy *src +481 tmp <- multiply tmp +482 result <- add tmp +483 return result +484 } diff --git a/html/apps/raytracing/color.mu.html b/html/apps/raytracing/color.mu.html index a434c204..b1d90af3 100644 --- a/html/apps/raytracing/color.mu.html +++ b/html/apps/raytracing/color.mu.html @@ -66,27 +66,31 @@ if ('onhashchange' in window) { 8 # print translating to [0, 256) 9 fn print-rgb screen: (addr screen), _c: (addr rgb) { 10 var c/esi: (addr rgb) <- copy _c -11 var n/ecx: int <- copy 0xff -12 var xn/xmm1: float <- convert n -13 var tmp/xmm0: float <- copy xn -14 var tmp-a/eax: (addr float) <- get c, r -15 tmp <- multiply *tmp-a -16 var tmp2/edx: int <- convert tmp -17 print-int32-decimal screen, tmp2 -18 print-string screen, " " -19 tmp-a <- get c, g -20 tmp <- copy xn -21 tmp <- multiply *tmp-a -22 tmp2 <- convert tmp -23 print-int32-decimal screen, tmp2 -24 print-string screen, " " -25 tmp-a <- get c, b -26 tmp <- copy xn -27 tmp <- multiply *tmp-a -28 tmp2 <- convert tmp -29 print-int32-decimal screen, tmp2 -30 print-string screen, "\n" -31 } +11 var xn: float +12 var xn-addr/ecx: (addr float) <- address xn +13 fill-in-rational xn-addr, 0x3e7ff, 0x3e8 # 255999 / 1000 +14 # print 255.999 * c->r +15 var result/xmm0: float <- copy xn +16 var src-addr/eax: (addr float) <- get c, r +17 result <- multiply *src-addr +18 var result-int/edx: int <- truncate result +19 print-int32-decimal screen, result-int +20 print-string screen, " " +21 # print 255.999 * c->g +22 src-addr <- get c, g +23 result <- copy xn +24 result <- multiply *src-addr +25 result-int <- truncate result +26 print-int32-decimal screen, result-int +27 print-string screen, " " +28 # print 255.999 * c->b +29 src-addr <- get c, b +30 result <- copy xn +31 result <- multiply *src-addr +32 result-int <- truncate result +33 print-int32-decimal screen, result-int +34 print-string screen, "\n" +35 } diff --git a/html/apps/raytracing/ray.mu.html b/html/apps/raytracing/ray.mu.html index fc4ef2f2..b3fe9789 100644 --- a/html/apps/raytracing/ray.mu.html +++ b/html/apps/raytracing/ray.mu.html @@ -76,7 +76,7 @@ if ('onhashchange' in window) { 18 var self/esi: (addr ray) <- copy _self 19 var src/eax: (addr vec3) <- get self, dir 20 copy-object src, out -21 vec3-mul-by out, t +21 vec3-scale-up out, t 22 src <- get self, orig 23 vec3-add-to out, src 24 } diff --git a/html/apps/raytracing/vec.mu.html b/html/apps/raytracing/vec.mu.html index 04795182..f871a1ea 100644 --- a/html/apps/raytracing/vec.mu.html +++ b/html/apps/raytracing/vec.mu.html @@ -14,6 +14,8 @@ pre { white-space: pre-wrap; font-family: monospace; color: #000000; background- body { font-size:12pt; font-family: monospace; color: #000000; background-color: #c6c6c6; } a { color:inherit; } * { font-size:12pt; font-size: 1em; } +.CommentedCode { color: #8a8a8a; } +.muComment { color: #005faf; } .LineNr { } .SpecialChar { color: #d70000; } .Delimiter { color: #c000c0; } @@ -55,55 +57,141 @@ if ('onhashchange' in window) { https://github.com/akkartik/mu/blob/master/apps/raytracing/vec.mu
- 1 type vec3 {
- 2   x: float
- 3   y: float
- 4   z: float
- 5 }
- 6 
- 7 fn vec3-negate _v: (addr vec3) {
- 8 }
- 9 
-10 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) {
-11 }
-12 
-13 fn vec3-mul-by _v1: (addr vec3), _v2: (addr vec3) {
-14 }
-15 
-16 fn vec3-scale-up _v: (addr vec3), f: float {
-17 }
-18 
-19 fn vec3-scale-down _v: (addr vec3), f: float {
-20 }
-21 
-22 fn vec3-length v: (addr vec3) -> result/xmm0: float {
-23 }
-24 
-25 fn vec3-length-squared _v: (addr vec3) -> result/xmm0: float {
-26 }
-27 
-28 fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> result/xmm0: float {
-29 }
-30 
-31 fn vec3-cross _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) {
-32 }
-33 
-34 fn vec3-unit in: (addr vec3), out: (addr vec3) {
-35 }
-36 
-37 fn print-vec3 screen: (addr screen), _v: (addr vec3) {
-38   var v/esi: (addr vec3) <- copy _v
-39   print-string screen, "("
-40   var tmp/eax: (addr float) <- get v, x
-41   print-float screen, *tmp
-42   print-string screen, ", "
-43   tmp <- get v, y
-44   print-float screen, *tmp
-45   print-string screen, ", "
-46   tmp <- get v, z
-47   print-float screen, *tmp
-48   print-string screen, ")"
-49 }
+  1 type vec3 {
+  2   x: float
+  3   y: float
+  4   z: float
+  5 }
+  6 
+  7 fn print-vec3 screen: (addr screen), _v: (addr vec3) {
+  8   var v/esi: (addr vec3) <- copy _v
+  9   print-string screen, "("
+ 10   var tmp/eax: (addr float) <- get v, x
+ 11   print-float screen, *tmp
+ 12   print-string screen, ", "
+ 13   tmp <- get v, y
+ 14   print-float screen, *tmp
+ 15   print-string screen, ", "
+ 16   tmp <- get v, z
+ 17   print-float screen, *tmp
+ 18   print-string screen, ")"
+ 19 }
+ 20 
+ 21 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) {
+ 22   var v1/edi: (addr vec3) <- copy _v1
+ 23   var v2/esi: (addr vec3) <- copy _v2
+ 24   # v1.x += v2.x
+ 25   var arg1/eax: (addr float) <- get v1, x
+ 26   var arg2/ecx: (addr float) <- get v2, x
+ 27   var result/xmm0: float <- copy *arg1
+ 28   result <- add *arg2
+ 29   copy-to *arg1, result
+ 30   # v1.y += v2.y
+ 31   arg1 <- get v1, y
+ 32   arg2 <- get v2, y
+ 33   result <- copy *arg1
+ 34   result <- add *arg2
+ 35   copy-to *arg1, result
+ 36   # v1.z += v2.z
+ 37   arg1 <- get v1, z
+ 38   arg2 <- get v2, z
+ 39   result <- copy *arg1
+ 40   result <- add *arg2
+ 41   copy-to *arg1, result
+ 42 }
+ 43 
+ 44 fn vec3-subtract-from v1: (addr vec3), v2: (addr vec3) {
+ 45   var tmp-storage: vec3
+ 46   var tmp/eax: (addr vec3) <- address tmp-storage
+ 47   copy-object v2, tmp
+ 48   vec3-negate tmp
+ 49   vec3-add-to v1, tmp
+ 50 }
+ 51 
+ 52 fn vec3-negate v: (addr vec3) {
+ 53   var negative-one/eax: int <- copy -1
+ 54   var negative-one-f/xmm0: float <- convert negative-one
+ 55   vec3-scale-up v, negative-one-f
+ 56 }
+ 57 
+ 58 fn vec3-scale-up _v: (addr vec3), f: float {
+ 59   var v/edi: (addr vec3) <- copy _v
+ 60   # v.x *= f
+ 61   var dest/eax: (addr float) <- get v, x
+ 62   var result/xmm0: float <- copy *dest
+ 63   result <- multiply f
+ 64   copy-to *dest, result
+ 65   # v.y *= f
+ 66   dest <- get v, y
+ 67   result <- copy *dest
+ 68   result <- multiply f
+ 69   copy-to *dest, result
+ 70   # v.z *= f
+ 71   dest <- get v, z
+ 72   result <- copy *dest
+ 73   result <- multiply f
+ 74   copy-to *dest, result
+ 75 }
+ 76 
+ 77 fn vec3-scale-down _v: (addr vec3), f: float {
+ 78   var v/edi: (addr vec3) <- copy _v
+ 79   # v.x /= f
+ 80   var dest/eax: (addr float) <- get v, x
+ 81   var result/xmm0: float <- copy *dest
+ 82   result <- divide f
+ 83   copy-to *dest, result
+ 84   # v.y /= f
+ 85   dest <- get v, y
+ 86   result <- copy *dest
+ 87   result <- divide f
+ 88   copy-to *dest, result
+ 89   # v.z /= f
+ 90   dest <- get v, z
+ 91   result <- copy *dest
+ 92   result <- divide f
+ 93   copy-to *dest, result
+ 94 }
+ 95 
+ 96 fn vec3-unit in: (addr vec3), out: (addr vec3) {
+ 97   var len/xmm0: float <- vec3-length in
+ 98 #?   print-string 0, "len: "
+ 99 #?   print-float 0, len
+100 #?   print-string 0, "\n"
+101   copy-object in, out
+102   vec3-scale-down out, len
+103 }
+104 
+105 fn vec3-length v: (addr vec3) -> _/xmm0: float {
+106   var result/xmm0: float <- vec3-length-squared v
+107   result <- square-root result
+108   return result
+109 }
+110 
+111 fn vec3-length-squared _v: (addr vec3) -> _/xmm0: float {
+112   var v/esi: (addr vec3) <- copy _v
+113   # result = v.x * v.x
+114   var src/eax: (addr float) <- get v, x
+115   var tmp/xmm1: float <- copy *src
+116   tmp <- multiply tmp
+117   var result/xmm0: float <- copy tmp
+118   # result += v.y * v.y
+119   src <- get v, y
+120   tmp <- copy *src
+121   tmp <- multiply tmp
+122   result <- add tmp
+123   # result += v.z * v.z
+124   src <- get v, z
+125   tmp <- copy *src
+126   tmp <- multiply tmp
+127   result <- add tmp
+128   return result
+129 }
+130 
+131 fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> result/xmm0: float {
+132 }
+133 
+134 fn vec3-cross _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) {
+135 }
 
-- cgit 1.4.1-2-gfad0