about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2020-10-05 18:36:28 -0700
committerKartik Agaram <vc@akkartik.com>2020-10-05 18:36:28 -0700
commit5fa8e972d6a87c200316a84bc51241d86c28fb93 (patch)
tree8e981f6417ebcb4c1c88458b054ee6a9566ff485
parent4f5562c2430f41511c83c28a39cd0ae401bf7679 (diff)
downloadmu-5fa8e972d6a87c200316a84bc51241d86c28fb93.tar.gz
6960
-rw-r--r--apps/raytracing/3.mu32
-rw-r--r--apps/raytracing/color.mu38
-rw-r--r--apps/raytracing/ray.mu2
-rw-r--r--apps/raytracing/vec.mu122
4 files changed, 141 insertions, 53 deletions
diff --git a/apps/raytracing/3.mu b/apps/raytracing/3.mu
index 28640c7b..acc2df33 100644
--- a/apps/raytracing/3.mu
+++ b/apps/raytracing/3.mu
@@ -410,39 +410,39 @@ fn vec3-negate v: (addr vec3) {
   vec3-scale-up v, negative-one-f
 }
 
-fn vec3-scale-up _v1: (addr vec3), f: float {
-  var v1/edi: (addr vec3) <- copy _v1
-  # v1.x *= f
-  var dest/eax: (addr float) <- get v1, x
+fn vec3-scale-up _v: (addr vec3), f: float {
+  var v/edi: (addr vec3) <- copy _v
+  # v.x *= f
+  var dest/eax: (addr float) <- get v, x
   var result/xmm0: float <- copy *dest
   result <- multiply f
   copy-to *dest, result
-  # v1.y *= f
-  dest <- get v1, y
+  # v.y *= f
+  dest <- get v, y
   result <- copy *dest
   result <- multiply f
   copy-to *dest, result
-  # v1.z *= f
-  dest <- get v1, z
+  # v.z *= f
+  dest <- get v, z
   result <- copy *dest
   result <- multiply f
   copy-to *dest, result
 }
 
-fn vec3-scale-down _v1: (addr vec3), f: float {
-  var v1/edi: (addr vec3) <- copy _v1
-  # v1.x /= f
-  var dest/eax: (addr float) <- get v1, x
+fn vec3-scale-down _v: (addr vec3), f: float {
+  var v/edi: (addr vec3) <- copy _v
+  # v.x /= f
+  var dest/eax: (addr float) <- get v, x
   var result/xmm0: float <- copy *dest
   result <- divide f
   copy-to *dest, result
-  # v1.y /= f
-  dest <- get v1, y
+  # v.y /= f
+  dest <- get v, y
   result <- copy *dest
   result <- divide f
   copy-to *dest, result
-  # v1.z /= f
-  dest <- get v1, z
+  # v.z /= f
+  dest <- get v, z
   result <- copy *dest
   result <- divide f
   copy-to *dest, result
diff --git a/apps/raytracing/color.mu b/apps/raytracing/color.mu
index 3b1ff419..86b3d0b6 100644
--- a/apps/raytracing/color.mu
+++ b/apps/raytracing/color.mu
@@ -8,24 +8,28 @@ type rgb {
 # print translating to [0, 256)
 fn print-rgb screen: (addr screen), _c: (addr rgb) {
   var c/esi: (addr rgb) <- copy _c
-  var n/ecx: int <- copy 0xff
-  var xn/xmm1: float <- convert n
-  var tmp/xmm0: float <- copy xn
-  var tmp-a/eax: (addr float) <- get c, r
-  tmp <- multiply *tmp-a
-  var tmp2/edx: int <- convert tmp
-  print-int32-decimal screen, tmp2
+  var xn: float
+  var xn-addr/ecx: (addr float) <- address xn
+  fill-in-rational xn-addr, 0x3e7ff, 0x3e8  # 255999 / 1000
+  # print 255.999 * c->r
+  var result/xmm0: float <- copy xn
+  var src-addr/eax: (addr float) <- get c, r
+  result <- multiply *src-addr
+  var result-int/edx: int <- truncate result
+  print-int32-decimal screen, result-int
   print-string screen, " "
-  tmp-a <- get c, g
-  tmp <- copy xn
-  tmp <- multiply *tmp-a
-  tmp2 <- convert tmp
-  print-int32-decimal screen, tmp2
+  # print 255.999 * c->g
+  src-addr <- get c, g
+  result <- copy xn
+  result <- multiply *src-addr
+  result-int <- truncate result
+  print-int32-decimal screen, result-int
   print-string screen, " "
-  tmp-a <- get c, b
-  tmp <- copy xn
-  tmp <- multiply *tmp-a
-  tmp2 <- convert tmp
-  print-int32-decimal screen, tmp2
+  # print 255.999 * c->b
+  src-addr <- get c, b
+  result <- copy xn
+  result <- multiply *src-addr
+  result-int <- truncate result
+  print-int32-decimal screen, result-int
   print-string screen, "\n"
 }
diff --git a/apps/raytracing/ray.mu b/apps/raytracing/ray.mu
index 33d1a9fe..20cc42e9 100644
--- a/apps/raytracing/ray.mu
+++ b/apps/raytracing/ray.mu
@@ -18,7 +18,7 @@ fn ray-at _self: (addr ray), t: float, out: (addr vec3) {
   var self/esi: (addr ray) <- copy _self
   var src/eax: (addr vec3) <- get self, dir
   copy-object src, out
-  vec3-mul-by out, t
+  vec3-scale-up out, t
   src <- get self, orig
   vec3-add-to out, src
 }
diff --git a/apps/raytracing/vec.mu b/apps/raytracing/vec.mu
index b5fa1c74..ce745a15 100644
--- a/apps/raytracing/vec.mu
+++ b/apps/raytracing/vec.mu
@@ -4,25 +4,126 @@ type vec3 {
   z: float
 }
 
-fn vec3-negate _v: (addr vec3) {
+fn print-vec3 screen: (addr screen), _v: (addr vec3) {
+  var v/esi: (addr vec3) <- copy _v
+  print-string screen, "("
+  var tmp/eax: (addr float) <- get v, x
+  print-float screen, *tmp
+  print-string screen, ", "
+  tmp <- get v, y
+  print-float screen, *tmp
+  print-string screen, ", "
+  tmp <- get v, z
+  print-float screen, *tmp
+  print-string screen, ")"
 }
 
 fn vec3-add-to _v1: (addr vec3), _v2: (addr vec3) {
+  var v1/edi: (addr vec3) <- copy _v1
+  var v2/esi: (addr vec3) <- copy _v2
+  # v1.x += v2.x
+  var arg1/eax: (addr float) <- get v1, x
+  var arg2/ecx: (addr float) <- get v2, x
+  var result/xmm0: float <- copy *arg1
+  result <- add *arg2
+  copy-to *arg1, result
+  # v1.y += v2.y
+  arg1 <- get v1, y
+  arg2 <- get v2, y
+  result <- copy *arg1
+  result <- add *arg2
+  copy-to *arg1, result
+  # v1.z += v2.z
+  arg1 <- get v1, z
+  arg2 <- get v2, z
+  result <- copy *arg1
+  result <- add *arg2
+  copy-to *arg1, result
+}
+
+fn vec3-subtract-from v1: (addr vec3), v2: (addr vec3) {
+  var tmp-storage: vec3
+  var tmp/eax: (addr vec3) <- address tmp-storage
+  copy-object v2, tmp
+  vec3-negate tmp
+  vec3-add-to v1, tmp
 }
 
-fn vec3-mul-by _v1: (addr vec3), _v2: (addr vec3) {
+fn vec3-negate v: (addr vec3) {
+  var negative-one/eax: int <- copy -1
+  var negative-one-f/xmm0: float <- convert negative-one
+  vec3-scale-up v, negative-one-f
 }
 
 fn vec3-scale-up _v: (addr vec3), f: float {
+  var v/edi: (addr vec3) <- copy _v
+  # v.x *= f
+  var dest/eax: (addr float) <- get v, x
+  var result/xmm0: float <- copy *dest
+  result <- multiply f
+  copy-to *dest, result
+  # v.y *= f
+  dest <- get v, y
+  result <- copy *dest
+  result <- multiply f
+  copy-to *dest, result
+  # v.z *= f
+  dest <- get v, z
+  result <- copy *dest
+  result <- multiply f
+  copy-to *dest, result
 }
 
 fn vec3-scale-down _v: (addr vec3), f: float {
+  var v/edi: (addr vec3) <- copy _v
+  # v.x /= f
+  var dest/eax: (addr float) <- get v, x
+  var result/xmm0: float <- copy *dest
+  result <- divide f
+  copy-to *dest, result
+  # v.y /= f
+  dest <- get v, y
+  result <- copy *dest
+  result <- divide f
+  copy-to *dest, result
+  # v.z /= f
+  dest <- get v, z
+  result <- copy *dest
+  result <- divide f
+  copy-to *dest, result
+}
+
+fn vec3-unit in: (addr vec3), out: (addr vec3) {
+  var len/xmm0: float <- vec3-length in
+#?   print-string 0, "len: "
+#?   print-float 0, len
+#?   print-string 0, "\n"
+  copy-object in, out
+  vec3-scale-down out, len
 }
 
 fn vec3-length v: (addr vec3) -> result/xmm0: float {
+  result <- vec3-length-squared v
+  result <- square-root result
 }
 
 fn vec3-length-squared _v: (addr vec3) -> result/xmm0: float {
+  var v/esi: (addr vec3) <- copy _v
+  # result = v.x * v.x
+  var src/eax: (addr float) <- get v, x
+  var tmp/xmm1: float <- copy *src
+  tmp <- multiply tmp
+  result <- copy tmp
+  # result += v.y * v.y
+  src <- get v, y
+  tmp <- copy *src
+  tmp <- multiply tmp
+  result <- add tmp
+  # result += v.z * v.z
+  src <- get v, z
+  tmp <- copy *src
+  tmp <- multiply tmp
+  result <- add tmp
 }
 
 fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> result/xmm0: float {
@@ -30,20 +131,3 @@ fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> result/xmm0: float {
 
 fn vec3-cross _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) {
 }
-
-fn vec3-unit in: (addr vec3), out: (addr vec3) {
-}
-
-fn print-vec3 screen: (addr screen), _v: (addr vec3) {
-  var v/esi: (addr vec3) <- copy _v
-  print-string screen, "("
-  var tmp/eax: (addr float) <- get v, x
-  print-float screen, *tmp
-  print-string screen, ", "
-  tmp <- get v, y
-  print-float screen, *tmp
-  print-string screen, ", "
-  tmp <- get v, z
-  print-float screen, *tmp
-  print-string screen, ")"
-}