about summary refs log tree commit diff stats
path: root/linux/raytracing/2.mu
blob: 7de89518369efff4f9efb05f195d40ea66e35e74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# Listing 7 of https://raytracing.github.io/books/RayTracingInOneWeekend.html
#
# To run (on Linux):
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu
#   $ ./translate raytracing/2.mu
#   $ ./a.elf > 2.ppm

fn main -> _/ebx: int {
  print-string 0, "P3\n256 256\n255\n"
  var _four/edx: int <- copy 4
  var four/xmm1: float <- convert _four
  var one-fourth/xmm1: float <- reciprocal four
  var max/edx: int <- copy 0xff
  var image-size/xmm2: float <- convert max
  var j/ecx: int <- copy 0xff
  {
    compare j, 0
    break-if-<
    var i/eax: int <- copy 0
    {
      compare i, 0xff
      break-if->
      var c: rgb
      # compute r
      var tmp/xmm0: float <- convert i
      tmp <- divide image-size
      var r-addr/edx: (addr float) <- get c, r
      copy-to *r-addr, tmp
#?       var tmp2/ebx: int <- reinterpret *r-addr
#?       print-int32-hex 0, tmp2
#?       print-string 0, "\n"
      # compute g
      tmp <- convert j
      tmp <- divide image-size
      var g-addr/edx: (addr float) <- get c, g
      copy-to *g-addr, tmp
      # compute b
      var b-addr/edx: (addr float) <- get c, b
      copy-to *b-addr, one-fourth
      # emit
      var c-addr/edx: (addr rgb) <- address c
      print-rgb 0, c-addr
      i <- increment
      loop
    }
    j <- decrement
    loop
  }
  return 0
}

type rgb {
  # components normalized to within [0.0, 1.0]
  r: float
  g: float
  b: float
}

# 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  # turns out 255 works just as well as 255.999, which is lucky because we don't have floating-point literals
  var xn/xmm1: float <- convert n
  # print 255 * 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 <- convert result
  print-int32-decimal screen, result-int
  print-string screen, " "
  # print 255 * c->g
  src-addr <- get c, g
  result <- copy xn
  result <- multiply *src-addr
  result-int <- convert result
  print-int32-decimal screen, result-int
  print-string screen, " "
  # print 255 * c->b
  src-addr <- get c, b
  result <- copy xn
  result <- multiply *src-addr
  result-int <- convert result
  print-int32-decimal screen, result-int
  print-string screen, "\n"
}