about summary refs log tree commit diff stats
path: root/linux/raytracing/3.mu
blob: c64ea0352cde1e1d759df991d6aa0068807433d4 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# Listing 9 of https://raytracing.github.io/books/RayTracingInOneWeekend.html
#
# To run (on Linux):
#   $ git clone https://github.com/akkartik/mu
#   $ cd mu
#   $ ./translate raytracing/3.mu
#   $ ./a.elf > 3.ppm

fn ray-color _in: (addr ray), _out: (addr rgb) {
  var in/esi: (addr ray) <- copy _in
  var out/edi: (addr rgb) <- copy _out
  var dir/eax: (addr vec3) <- get in, dir
#?   print-string 0, "r.dir: "
#?   print-vec3 0, dir
#?   print-string 0, "\n"
  var unit-storage: vec3
  var unit/ecx: (addr vec3) <- address unit-storage
  vec3-unit dir, unit
#?   print-string 0, "r.dir normalized: "
#?   print-vec3 0, unit
#?   print-string 0, "\n"
  var y-addr/eax: (addr float) <- get unit, y
  # t = (dir.y + 1.0) / 2.0
  var t/xmm0: float <- copy *y-addr
  var one/eax: int <- copy 1
  var one-f/xmm1: float <- convert one
  t <- add one-f
  var two/eax: int <- copy 2
  var two-f/xmm2: float <- convert two
  t <- divide two-f
#?   print-string 0, "t: "
#?   print-float-hex 0, t
#?   print-string 0, "\n"
  # whitening = (1.0 - t) * white
  var whitening-storage: rgb
  var whitening/ecx: (addr rgb) <- address whitening-storage
  rgb-white whitening
  var one-minus-t/xmm3: float <- copy one-f
  one-minus-t <- subtract t
  rgb-scale-up whitening, one-minus-t
#?   print-string 0, "whitening: "
#?   print-rgb-raw 0, whitening
#?   print-string 0, "\n"
  # out = t * (0.5, 0.7, 1.0)
  var dest/eax: (addr float) <- get out, r
  fill-in-rational dest, 5, 0xa
  dest <- get out, g
  fill-in-rational dest, 7, 0xa
  dest <- get out, b
  copy-to *dest, one-f
  rgb-scale-up out, t
#?   print-string 0, "base: "
#?   print-rgb-raw 0, out
#?   print-string 0, "\n"
  # blend with whitening
  rgb-add-to out, whitening
#?   print-string 0, "result: "
#?   print-rgb-raw 0, out
#?   print-string 0, "\n"
}

fn main -> _/ebx: int {

  # image
  #   width = 400
  #   height = 400 * 9/16 = 225
  var aspect: float
  var aspect-addr/eax: (addr float) <- address aspect
  fill-in-rational aspect-addr, 0x10, 9  # 16/9
#?   print-string 0, "aspect ratio: "
#?   print-float-hex 0, aspect
#?   print-string 0, " "
#?   {
#?     var foo2/ebx: int <- reinterpret aspect
#?     print-int32-hex 0, foo2
#?   }
#?   print-string 0, "\n"

  # camera

  # viewport-height = 2.0
  var tmp/eax: int <- copy 2
  var two-f/xmm4: float <- convert tmp
  var viewport-height/xmm7: float <- copy two-f
#?   print-string 0, "viewport height: "
#?   print-float-hex 0, viewport-height
#?   print-string 0, " "
#?   {
#?     var foo: float
#?     copy-to foo, viewport-height
#?     var foo2/ebx: int <- reinterpret foo
#?     print-int32-hex 0, foo2
#?   }
#?   print-string 0, "\n"
  # viewport-width = aspect * viewport-height
  var viewport-width/xmm6: float <- convert tmp
  viewport-width <- multiply aspect
#?   print-string 0, "viewport width: "
#?   print-float-hex 0, viewport-width
#?   print-string 0, " "
#?   {
#?     var foo: float
#?     copy-to foo, viewport-width
#?     var foo2/ebx: int <- reinterpret foo
#?     print-int32-hex 0, foo2
#?   }
#?   print-string 0, "\n"
  # focal-length = 1.0
  tmp <- copy 1
  var focal-length/xmm5: float <- convert tmp

  # origin = point3(0, 0, 0)
  var origin-storage: vec3
  var origin/edi: (addr vec3) <- address origin-storage
  # horizontal = vec3(viewport-width, 0, 0)
  var horizontal-storage: vec3
  var dest/eax: (addr float) <- get horizontal-storage, x
  copy-to *dest, viewport-width
  var horizontal/ebx: (addr vec3) <- address horizontal-storage
  # vertical = vec3(0, viewport-height, 0)
  var vertical-storage: vec3
  dest <- get vertical-storage, y
  copy-to *dest, viewport-height
  var vertical/edx: (addr vec3) <- address vertical-storage
  # lower-left-corner = origin - horizontal/2 - vertical/2 - vec3(0, 0, focal-length)
  # . lower-left-corner = origin
  var lower-left-corner-storage: vec3
  var lower-left-corner/esi: (addr vec3) <- address lower-left-corner-storage
  copy-object origin, lower-left-corner
  # . lower-left-corner -= horizontal/2
  var tmp2: vec3
  var tmp2-addr/eax: (addr vec3) <- address tmp2
  copy-object horizontal, tmp2-addr
  vec3-scale-down tmp2-addr, two-f
  vec3-subtract-from lower-left-corner, tmp2-addr
  # . lower-left-corner -= vertical/2
  copy-object vertical, tmp2-addr
  vec3-scale-down tmp2-addr, two-f
  vec3-subtract-from lower-left-corner, tmp2-addr
  # . lower-left-corner -= vec3(0, 0, focal-length)
  var dest2/ecx: (addr float) <- get lower-left-corner, z
  var tmp3/xmm0: float <- copy *dest2
  tmp3 <- subtract focal-length
  copy-to *dest2, tmp3
  # phew!

  # render

  # live variables at this point:
  #   origin (edi)
  #   lower-left-corner (esi)
  #   horizontal (ebx)
  #   vertical (edx)
  # floating-point registers are all free
  print-string 0, "P3\n400 225\n255\n"  # 225 = image height
  var tmp/eax: int <- copy 0x18f # image width - 1
  var image-width-1/xmm7: float <- convert tmp
  tmp <- copy 0xe0  # image height - 1
  var image-height-1/xmm6: float <- convert tmp
  #
  var j/ecx: int <- copy 0xe0  # 224
  {
    compare j, 0
    break-if-<
    var i/eax: int <- copy 0
    {
      compare i, 0x190  # 400 = image width
      break-if->=
      # u = i / (image-width - 1)
      var u/xmm0: float <- convert i
      u <- divide image-width-1
#?       print-string 0, "u: "
#?       print-float-hex 0, u
#?       print-string 0, "\n"
      # v = j / (image-height - 1)
      var v/xmm1: float <- convert j
      v <- divide image-height-1
      # r = ray(origin, lower-left-corner + u*horizontal + v*vertical - origin)
      var r-storage: ray
      # . . we're running out of int registers now,
      # . . but luckily we don't need i and j in the rest of this loop iteration,
      # . . so we'll just spill them in a block
      {
        # . r.orig = origin
        var r/eax: (addr ray) <- address r-storage
        var dest/ecx: (addr vec3) <- get r, orig
        copy-object origin, dest
        # . r.dir = lower-left-corner
        dest <- get r, dir
        copy-object lower-left-corner, dest
        # . r.dir += horizontal*u
        var tmp-vec3: vec3
        var tmp/eax: (addr vec3) <- address tmp-vec3
        copy-object horizontal, tmp
        vec3-scale-up tmp, u
        vec3-add-to dest, tmp
        # . r.dir += vertical*v
        copy-object vertical, tmp
        vec3-scale-up tmp, v
        vec3-add-to dest, tmp
        # . r.dir -= origin
        vec3-subtract-from dest, origin
#?         print-string 0, "ray direction: "
#?         print-vec3 0, dest
#?         print-string 0, "\n"
      }
      # pixel-color = ray-color(r)
      var c-storage: rgb
      var c/ecx: (addr rgb) <- address c-storage
      {
        var r/eax: (addr ray) <- address r-storage
        ray-color r, c
        # write color
        print-rgb 0, c
#?         print-rgb-raw 0, c
#?         print-string 0, "\n"
      }
      i <- increment
      loop
    }
    j <- decrement
    loop
  }
  return 0
}

type ray {
  orig: vec3  # point
  dir: vec3
}

# A little different from the constructor at https://raytracing.github.io/books/RayTracingInOneWeekend.html
# We immediately normalize the direction vector so we don't have to keep doing
# so.
fn initialize-ray _self: (addr ray), o: (addr vec3), d: (addr vec3) {
  var self/esi: (addr ray) <- copy _self
  var dest/eax: (addr vec3) <- get self, orig
  copy-object o, dest
  dest <- get self, dir
  vec3-unit d, dest
}

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-scale-up out, t
  src <- get self, orig
  vec3-add-to out, src
}

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 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, " "
  # 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, " "
  # 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"
}

fn print-rgb-raw screen: (addr screen), _v: (addr rgb) {
  var v/esi: (addr rgb) <- copy _v
  print-string screen, "("
  var tmp/eax: (addr float) <- get v, r
  print-float-hex screen, *tmp
  print-string screen, ", "
  tmp <- get v, g
  print-float-hex screen, *tmp
  print-string screen, ", "
  tmp <- get v, b
  print-float-hex screen, *tmp
  print-string screen, ")"
}

fn rgb-white _c: (addr rgb) {
  var c/esi: (addr rgb) <- copy _c
  var one/eax: int <- copy 1
  var one-f/xmm0: float <- convert one
  var dest/edi: (addr float) <- get c, r
  copy-to *dest, one-f
  dest <- get c, g
  copy-to *dest, one-f
  dest <- get c, b
  copy-to *dest, one-f
}

fn rgb-add-to _c1: (addr rgb), _c2: (addr rgb) {
  var c1/edi: (addr rgb) <- copy _c1
  var c2/esi: (addr rgb) <- copy _c2
  # c1.r += c2.r
  var arg1/eax: (addr float) <- get c1, r
  var arg2/ecx: (addr float) <- get c2, r
  var result/xmm0: float <- copy *arg1
  result <- add *arg2
  copy-to *arg1, result
  # c1.g += c2.g
  arg1 <- get c1, g
  arg2 <- get c2, g
  result <- copy *arg1
  result <- add *arg2
  copy-to *arg1, result
  # c1.b += c2.b
  arg1 <- get c1, b
  arg2 <- get c2, b
  result <- copy *arg1
  result <- add *arg2
  copy-to *arg1, result
}

fn rgb-scale-up _c1: (addr rgb), f: float {
  var c1/edi: (addr rgb) <- copy _c1
  # c1.r *= f
  var dest/eax: (addr float) <- get c1, r
  var result/xmm0: float <- copy *dest
  result <- multiply f
  copy-to *dest, result
  # c1.g *= f
  dest <- get c1, g
  result <- copy *dest
  result <- multiply f
  copy-to *dest, result
  # c1.b *= f
  dest <- get c1, b
  result <- copy *dest
  result <- multiply f
  copy-to *dest, result
}

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
}