From 71e4f3812982dba2efb471283d310224e8db363e Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Wed, 3 Mar 2021 22:09:50 -0800 Subject: 7842 - new directory organization Baremetal is now the default build target and therefore has its sources at the top-level. Baremetal programs build using the phase-2 Mu toolchain that requires a Linux kernel. This phase-2 codebase which used to be at the top-level is now under the linux/ directory. Finally, the phase-2 toolchain, while self-hosting, has a way to bootstrap from a C implementation, which is now stored in linux/bootstrap. The bootstrap C implementation uses some literate programming tools that are now in linux/bootstrap/tools. So the whole thing has gotten inverted. Each directory should build one artifact and include the main sources (along with standard library). Tools used for building it are relegated to sub-directories, even though those tools are often useful in their own right, and have had lots of interesting programs written using them. A couple of things have gotten dropped in this process: - I had old ways to run on just a Linux kernel, or with a Soso kernel. No more. - I had some old tooling for running a single test at the cursor. I haven't used that lately. Maybe I'll bring it back one day. The reorg isn't done yet. Still to do: - redo documentation everywhere. All the README files, all other markdown, particularly vocabulary.md. - clean up how-to-run comments at the start of programs everywhere - rethink what to do with the html/ directory. Do we even want to keep supporting it? In spite of these shortcomings, all the scripts at the top-level, linux/ and linux/bootstrap are working. The names of the scripts also feel reasonable. This is a good milestone to take stock at. --- linux/raytracing/vec.mu | 135 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 135 insertions(+) create mode 100644 linux/raytracing/vec.mu (limited to 'linux/raytracing/vec.mu') diff --git a/linux/raytracing/vec.mu b/linux/raytracing/vec.mu new file mode 100644 index 00000000..7caf7e88 --- /dev/null +++ b/linux/raytracing/vec.mu @@ -0,0 +1,135 @@ +type vec3 { + x: float + y: float + z: float +} + +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-hex screen, *tmp + print-string screen, ", " + tmp <- get v, y + print-float-hex screen, *tmp + print-string screen, ", " + tmp <- get v, z + print-float-hex 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-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-hex 0, len +#? print-string 0, "\n" + copy-object in, out + vec3-scale-down out, len +} + +fn vec3-length v: (addr vec3) -> _/xmm0: float { + var result/xmm0: float <- vec3-length-squared v + result <- square-root result + return result +} + +fn vec3-length-squared _v: (addr vec3) -> _/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 + var result/xmm0: float <- 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 + return result +} + +fn vec3-dot _v1: (addr vec3), _v2: (addr vec3) -> _/xmm0: float { +} + +fn vec3-cross _v1: (addr vec3), _v2: (addr vec3), out: (addr vec3) { +} -- cgit 1.4.1-2-gfad0