-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixels.wat
391 lines (324 loc) · 14.2 KB
/
pixels.wat
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
(module
(import "deps" "atan2" (func $deps.atan2 (param f64) (param f64) (result f64)))
(import "deps" "updatePixels" (func $deps.updatePixels (param i32) (param i32)))
(import "deps" "getRngSeed" (func $deps.getRngSeed (result i32)))
(memory $memory (export "memory") 0)
(global $width (mut i32) (i32.const 0))
(global $height (mut i32) (i32.const 0))
(global $iterationCount (mut i64) (i64.const 0))
(global $currentBuffer (mut i32) (i32.const 0))
(global $bufferA (mut i32) (i32.const 0))
(global $bufferB (mut i32) (i32.const 0))
(global $factionCount (mut i32) (i32.const 0))
(global $factionColors (mut i32) (i32.const 0))
(global $ownerCounts (mut i32) (i32.const 0))
(global $queryBuffer (mut i32) (i32.const 0))
(global $neighborBuffer (mut i32) (i32.const 0))
(global $currentX (mut i32) (i32.const 0))
(global $currentY (mut i32) (i32.const 0))
(global $rngSeed (mut i32) (i32.const 0))
(func $init
(export "init")
(param $width i32)
(param $height i32)
(param $factionCount i32)
(local $memPtr i32)
;; Initialize globals
(global.set $width (local.get $width))
(global.set $height (local.get $height))
(global.set $factionCount (local.get $factionCount))
(local.set $memPtr (i32.const 0))
;; Allocate space for buffer A
(global.set $bufferA (local.get $memPtr))
(i32.shl (i32.mul (local.get $width) (local.get $height)) (i32.const 2))
(local.set $memPtr (i32.add (local.get $memPtr)))
;; Allocate space for buffer B
(global.set $bufferB (local.get $memPtr))
(i32.shl (i32.mul (local.get $width) (local.get $height)) (i32.const 2))
(local.set $memPtr (i32.add (local.get $memPtr)))
;; Allocate space for faction colors
(global.set $factionColors (local.get $memPtr))
(i32.shl (local.get $factionCount) (i32.const 2))
(local.set $memPtr (i32.add (local.get $memPtr)))
;; Allocate space for owner counts
(global.set $ownerCounts (local.get $memPtr))
(i32.shl (local.get $factionCount) (i32.const 3))
(local.set $memPtr (i32.add (local.get $memPtr)))
;; Allocate space for neighbor buffer
(global.set $neighborBuffer (local.get $memPtr))
(i32.add (local.get $memPtr) (i32.const 32))
(local.set $memPtr)
;; Actually allocate memory
(i32.div_u (local.get $memPtr) (i32.const 0x10000))
(i32.add (i32.const 1))
(drop (memory.grow))
)
(func $factionColor
(export "factionColor")
(param $faction_id i32)
(param $color i32)
(i32.shl (local.get $faction_id) (i32.const 2))
(i32.add (global.get $factionColors))
;; Reverse RGBA to BGRA
(i32.shr_u (i32.and (local.get $color) (i32.const 0xFF0000)) (i32.const 16))
(i32.and (local.get $color) (i32.const 0xFF00))
(i32.shl (i32.and (local.get $color) (i32.const 0xFF)) (i32.const 16))
;; Mix together and store
(i32.or)
(i32.or)
(i32.store)
)
(func $reset
(export "reset")
(local $i i32)
(local $j i32)
(local $x i32)
(local $angle f64)
(local $offset i32)
(local $owner i32)
;; Reset current buffer and iteration count
(global.set $iterationCount (i64.const 0))
(global.set $currentBuffer (i32.const 0))
;; Reset faction counts
(global.get $factionCount)
(i32.const 0)
(i32.shl (global.get $factionCount) (i32.const 2))
(memory.fill)
(local.set $i (i32.const 0))
(loop $outer_loop
(if (i32.lt_u (local.get $i) (global.get $height)) (then
(local.set $x (i32.mul (global.get $width) (local.get $i)))
(local.set $j (i32.const 0))
(loop $inner_loop
(if (i32.lt_u (local.get $j) (global.get $width)) (then
;; Get in radians
(f64.div (f64.convert_i32_u (global.get $width)) (f64.const 2))
(f64.sub (f64.convert_i32_u (local.get $i)))
(f64.sub (f64.const 0.5))
(f64.div (f64.convert_i32_u (global.get $height)) (f64.const 2))
(f64.sub (f64.convert_i32_u (local.get $j)))
(f64.sub (f64.const 0.5))
(call $deps.atan2)
;; Convert to degrees
(f64.mul (f64.const 57.2957795131))
(local.set $angle (f64.sub (f64.const 90)))
;; Correct the angle
(loop $angleCorrection
(if (f64.lt (local.get $angle) (f64.const 0)) (then
(f64.add (local.get $angle) (f64.const 360))
(local.set $angle)
(br $angleCorrection)
))
)
;; Convert to faction ID
(local.get $angle)
(f64.div (f64.const 360) (f64.convert_i32_u (global.get $factionCount)))
(f64.div)
(local.set $owner (i32.trunc_f64_u))
;; Write to buffers
(local.set $offset (i32.shl (i32.add (local.get $x) (local.get $j)) (i32.const 2)))
(i32.add (global.get $bufferB) (local.get $offset))
(i32.add (global.get $bufferA) (local.get $offset))
(i32.store (local.get $owner))
(i32.store (local.get $owner))
;; Increase owner count
(local.set $offset (i32.shl (local.get $owner) (i32.const 3)))
(local.set $offset (i32.add (global.get $ownerCounts) (local.get $offset)))
(local.get $offset)
(i64.load (local.get $offset))
(i64.store (i64.add (i64.const 1)))
;; j++
(local.set $j (i32.add (local.get $j) (i32.const 1)))
(br $inner_loop)
))
)
;; i++
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $outer_loop)
))
)
;; Now redraw
(call $draw)
)
(func $step
(export "step")
(local $newBuffer i32)
(local $oldBuffer i32)
(local $i i32)
(local $j i32)
(local $neighborBufferStart i32)
(local $ptr i32)
(global.set $rngSeed (call $deps.getRngSeed))
(global.set $iterationCount (i64.add (global.get $iterationCount) (i64.const 1)))
;; Swap buffers
(i32.sub (i32.const 1) (global.get $currentBuffer))
(local.tee $oldBuffer)
(global.set $currentBuffer)
(select (global.get $bufferB) (global.get $bufferA) (local.get $oldBuffer))
(local.tee $oldBuffer)
(global.set $queryBuffer)
(select (global.get $bufferA) (global.get $bufferB) (local.get $oldBuffer))
(local.set $newBuffer)
(local.set $neighborBufferStart (global.get $neighborBuffer))
(local.set $i (i32.const 0))
(loop $L0
(if (i32.lt_u (local.get $i) (global.get $width)) (then
(local.set $j (i32.const 0))
(global.set $currentX (local.get $i))
(loop $L2
(if (i32.lt_u (local.get $j) (global.get $height)) (then
(global.set $currentY (local.get $j))
;; Go through all neighbors
(call $getNeighbor (i32.const -1) (i32.const 0))
(call $getNeighbor (i32.const 1) (i32.const 0))
(call $getNeighbor (i32.const 0) (i32.const 1))
(call $getNeighbor (i32.const -1) (i32.const 1))
(call $getNeighbor (i32.const 1) (i32.const 1))
(call $getNeighbor (i32.const 0) (i32.const -1))
(call $getNeighbor (i32.const -1) (i32.const -1))
(call $getNeighbor (i32.const 1) (i32.const -1))
;; Convert coordinates to pointer
(i32.mul (local.get $j) (global.get $width))
(i32.add (local.get $i))
(i32.shl (i32.const 2))
;; Pointers to new and old buffers
(local.tee $ptr)
(i32.add (local.get $newBuffer))
(i32.add (local.get $ptr) (local.get $oldBuffer))
;; Get pointer to owner buffer
(i32.load)
(i32.shl (i32.const 3))
(i32.add (global.get $ownerCounts))
;; Decrement old owner
(local.tee $ptr)
(i64.sub (i64.load (local.get $ptr)) (i64.const 1))
(i64.store)
;; Select random neighbor index
(call $rng (local.get $i) (local.get $j))
(i32.sub (global.get $neighborBuffer) (local.get $neighborBufferStart))
(i32.rem_u)
(i32.and (i32.const 0xFC))
;; Reset neighbor buffer pointer
(global.set $neighborBuffer (local.get $neighborBufferStart))
;; Get previously selected random neighbor
(i32.add (global.get $neighborBuffer))
(i32.load)
(local.tee $ptr)
(i32.store)
;; Increment new owner
(i32.shl (local.get $ptr) (i32.const 3))
(i32.add (global.get $ownerCounts))
(local.tee $ptr)
(i64.add (i64.load (local.get $ptr)) (i64.const 1))
(i64.store)
;; j++
(local.set $j (i32.add (local.get $j) (i32.const 1)))
(br $L2)
))
)
;; i++
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $L0)
))
)
;; Redraw
(call $draw)
)
(func $getNeighbor
(param $offsetX i32)
(param $offsetY i32)
(local $neighbor i32)
;; Get X position
(i32.add (local.get $offsetX) (global.get $currentX))
(local.tee $offsetX)
(if (i32.ge_u (global.get $width)) (then
(return (i32.const -1))
))
;; Get Y position
(i32.add (local.get $offsetY) (global.get $currentY))
(local.tee $offsetY)
(if (i32.ge_u (global.get $height)) (then
(return (i32.const -1))
))
;; Get pixel offset
(i32.mul (local.get $offsetY) (global.get $width))
(i32.add (local.get $offsetX))
(i32.shl (i32.const 2))
(i32.add (global.get $queryBuffer))
(i32.load)
;; Does neighbor exist? (bit 32 set)
(local.tee $neighbor)
(if (i32.eq (i32.const -1)) (then
(return)
))
;; Store in- and advance neighbor buffer
(i32.store (global.get $neighborBuffer) (local.get $neighbor))
(global.set $neighborBuffer (i32.add (global.get $neighborBuffer) (i32.const 4)))
)
(func $rng
(param $x i32)
(param $y i32)
(result i32)
(local $temp i32)
;; Get a good mix in here
(i32.add (local.get $x) (global.get $rngSeed))
(i32.mul (local.get $y) (global.get $width))
(i32.add)
;; Do a bunch of bit-fiddling
(i32.xor (i32.const 0xA3C59AC3))
(i32.mul (i32.const 0x9E3779B9))
(local.tee $temp)
(i32.shr_u (i32.const 16))
(i32.xor (local.get $temp))
(i32.mul (i32.const 0x9E3779B9))
(local.tee $temp)
(i32.shr_u (i32.const 16))
(i32.xor (local.get $temp))
(i32.mul (i32.const 0x9E3779B9))
)
(func $draw
(export "draw")
(local $dataBuffer i32)
(local $paintBuffer i32)
(local $i i32)
(local $bufferBounds i32)
(select (global.get $bufferA) (global.get $bufferB) (global.get $currentBuffer))
(local.set $dataBuffer)
(select (global.get $bufferB) (global.get $bufferA) (global.get $currentBuffer))
(local.set $paintBuffer)
(local.set $bufferBounds (i32.mul (global.get $width) (global.get $height)))
(local.set $i (i32.const 0))
(loop $L0
(if (i32.lt_u (local.get $i) (local.get $bufferBounds)) (then
;; Get paint and data pointers
(i32.add (local.get $paintBuffer) (i32.shl (local.get $i) (i32.const 2)))
(i32.add (local.get $dataBuffer) (i32.shl (local.get $i) (i32.const 2)))
;; Get color of owner
(i32.shl (i32.load) (i32.const 2))
(i32.add (global.get $factionColors))
(i32.load)
;; Full alpha, always
(i32.store (i32.or (i32.const 0xFF000000)))
;; i++
(local.set $i (i32.add (local.get $i) (i32.const 1)))
(br $L0)
))
)
(local.get $paintBuffer)
(i32.add (local.get $paintBuffer) (i32.shl (local.get $bufferBounds) (i32.const 2)))
(call $deps.updatePixels)
)
(func $getIterations
(export "getIterations")
(result i64)
(global.get $iterationCount)
)
(func $getFactionCount
(export "getFactionCount")
(param $p0 i32)
(result i64)
(i32.shl (local.get $p0) (i32.const 3))
(i32.add (global.get $ownerCounts))
(i64.load)
)
)