Add mandelbrot example; Update game-of-life example

This commit is contained in:
dcodeIO
2018-04-19 17:32:23 +02:00
parent 350befee9b
commit 485c3fd9b2
30 changed files with 2069 additions and 980 deletions

View File

@ -0,0 +1,21 @@
Mandelbrot Set
==============
An [AssemblyScript](http://assemblyscript.org) example. Renders the Mandelbrot set to a canvas. Compiles to ~350 bytes of optimized WASM.
Instructions
------------
First, install the development dependencies:
```
$> npm install
```
Now, to build [assembly/index.ts](./assembly/index.ts) to an untouched and an optimized `.wasm` including their respective `.wat` representations, run:
```
$> npm run asbuild
```
Afterwards, run `npm run server` to start a <a href="http://127.0.0.1:8080">local server</a>. Should also automatically launch a browser.

View File

@ -0,0 +1,19 @@
export function update(width: u32, height: u32, max: u32): void {
var translateX = width / 1.6;
var translateY = height / 2.0;
var scale = 4.0 / width;
for (let y: u32 = 0; y < height; ++y) {
let imaginary = (y - translateY) * scale;
for (let x: u32 = 0; x < width; ++x) {
let real = (x - translateX) * scale;
let iteration = <u32>0;
for (let ix = 0.0, iy = 0.0; ix * ix + iy * iy <= 4.0; ++iteration) {
let t = ix * ix - iy * iy + real;
iy = 2.0 * ix * iy + imaginary;
ix = t;
if (++iteration >= max) break;
}
store<u8>(y * width + x, iteration * 255 / max);
}
}
}

View File

@ -0,0 +1,6 @@
{
"extends": "../../../std/assembly.d.ts",
"include": [
"./**/*.ts"
]
}

3
examples/mandelbrot/build/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
*.wasm
*.wasm.map
*.asm.js

Binary file not shown.

View File

@ -0,0 +1,261 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(memory $0 1)
(export "update" (func $assembly/index/update))
(export "memory" (memory $0))
(func $assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(local $8 f64)
(local $9 f64)
(local $10 f64)
(local $11 f64)
(local $12 f64)
(local $13 f64)
;;@ assembly/index.ts:2:2
(set_local $9
;;@ assembly/index.ts:2:19
(f64.div
(f64.convert_u/i32
(get_local $0)
)
;;@ assembly/index.ts:2:27
(f64.const 1.6)
)
)
;;@ assembly/index.ts:3:2
(set_local $10
;;@ assembly/index.ts:3:19
(f64.div
(f64.convert_u/i32
(get_local $1)
)
;;@ assembly/index.ts:3:28
(f64.const 2)
)
)
;;@ assembly/index.ts:4:2
(set_local $8
;;@ assembly/index.ts:4:14
(f64.div
(f64.const 4)
(f64.convert_u/i32
;;@ assembly/index.ts:4:20
(get_local $0)
)
)
)
(loop $continue|0
(if
;;@ assembly/index.ts:5:23
(i32.lt_u
(get_local $5)
;;@ assembly/index.ts:5:27
(get_local $1)
)
(block
;;@ assembly/index.ts:6:4
(set_local $11
;;@ assembly/index.ts:6:20
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:6:21
(get_local $5)
)
;;@ assembly/index.ts:6:25
(get_local $10)
)
;;@ assembly/index.ts:6:39
(get_local $8)
)
)
;;@ assembly/index.ts:7:9
(set_local $6
;;@ assembly/index.ts:7:22
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:7:25
(i32.lt_u
(get_local $6)
;;@ assembly/index.ts:7:29
(get_local $0)
)
(block
;;@ assembly/index.ts:8:6
(set_local $12
;;@ assembly/index.ts:8:17
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:8:18
(get_local $6)
)
;;@ assembly/index.ts:8:22
(get_local $9)
)
;;@ assembly/index.ts:8:36
(get_local $8)
)
)
;;@ assembly/index.ts:9:6
(set_local $7
;;@ assembly/index.ts:9:22
(i32.const 0)
)
;;@ assembly/index.ts:10:6
(block $break|2
(set_local $3
;;@ assembly/index.ts:10:20
(f64.const 0)
)
(set_local $4
;;@ assembly/index.ts:10:30
(f64.const 0)
)
(loop $continue|2
(if
;;@ assembly/index.ts:10:35
(f64.le
(f64.add
(f64.mul
(get_local $3)
;;@ assembly/index.ts:10:40
(get_local $3)
)
;;@ assembly/index.ts:10:45
(f64.mul
(get_local $4)
;;@ assembly/index.ts:10:50
(get_local $4)
)
)
;;@ assembly/index.ts:10:56
(f64.const 4)
)
(block
;;@ assembly/index.ts:11:8
(set_local $13
;;@ assembly/index.ts:11:16
(f64.add
(f64.sub
(f64.mul
(get_local $3)
;;@ assembly/index.ts:11:21
(get_local $3)
)
;;@ assembly/index.ts:11:26
(f64.mul
(get_local $4)
;;@ assembly/index.ts:11:31
(get_local $4)
)
)
;;@ assembly/index.ts:11:36
(get_local $12)
)
)
;;@ assembly/index.ts:12:8
(set_local $4
;;@ assembly/index.ts:12:13
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
;;@ assembly/index.ts:12:19
(get_local $3)
)
;;@ assembly/index.ts:12:24
(get_local $4)
)
;;@ assembly/index.ts:12:29
(get_local $11)
)
)
;;@ assembly/index.ts:13:8
(set_local $3
;;@ assembly/index.ts:13:13
(get_local $13)
)
;;@ assembly/index.ts:14:32
(br_if $break|2
;;@ assembly/index.ts:14:12
(i32.ge_u
(tee_local $7
(i32.add
;;@ assembly/index.ts:14:14
(get_local $7)
(i32.const 1)
)
)
;;@ assembly/index.ts:14:27
(get_local $2)
)
)
;;@ assembly/index.ts:10:61
(set_local $7
(i32.add
;;@ assembly/index.ts:10:63
(get_local $7)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
;;@ assembly/index.ts:16:6
(i32.store8
;;@ assembly/index.ts:16:16
(i32.add
(i32.mul
(get_local $5)
;;@ assembly/index.ts:16:20
(get_local $0)
)
;;@ assembly/index.ts:16:28
(get_local $6)
)
;;@ assembly/index.ts:16:31
(i32.div_u
(i32.mul
(get_local $7)
;;@ assembly/index.ts:16:43
(i32.const 255)
)
;;@ assembly/index.ts:16:49
(get_local $2)
)
)
;;@ assembly/index.ts:7:36
(set_local $6
(i32.add
;;@ assembly/index.ts:7:38
(get_local $6)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
;;@ assembly/index.ts:5:35
(set_local $5
(i32.add
;;@ assembly/index.ts:5:37
(get_local $5)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)

View File

@ -0,0 +1,283 @@
(module
(type $iiiv (func (param i32 i32 i32)))
(global $HEAP_BASE i32 (i32.const 4))
(memory $0 1)
(export "update" (func $assembly/index/update))
(export "memory" (memory $0))
(func $assembly/index/update (; 0 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 f64)
(local $4 f64)
(local $5 f64)
(local $6 i32)
(local $7 f64)
(local $8 i32)
(local $9 f64)
(local $10 i32)
(local $11 f64)
(local $12 f64)
(local $13 f64)
;;@ assembly/index.ts:2:2
(set_local $3
;;@ assembly/index.ts:2:19
(f64.div
(f64.convert_u/i32
(get_local $0)
)
;;@ assembly/index.ts:2:27
(f64.const 1.6)
)
)
;;@ assembly/index.ts:3:2
(set_local $4
;;@ assembly/index.ts:3:19
(f64.div
(f64.convert_u/i32
(get_local $1)
)
;;@ assembly/index.ts:3:28
(f64.const 2)
)
)
;;@ assembly/index.ts:4:2
(set_local $5
;;@ assembly/index.ts:4:14
(f64.div
(f64.const 4)
(f64.convert_u/i32
;;@ assembly/index.ts:4:20
(get_local $0)
)
)
)
;;@ assembly/index.ts:5:2
(block $break|0
;;@ assembly/index.ts:5:7
(set_local $6
;;@ assembly/index.ts:5:20
(i32.const 0)
)
(loop $continue|0
(if
;;@ assembly/index.ts:5:23
(i32.lt_u
(get_local $6)
;;@ assembly/index.ts:5:27
(get_local $1)
)
(block
(block
;;@ assembly/index.ts:6:4
(set_local $7
;;@ assembly/index.ts:6:20
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:6:21
(get_local $6)
)
;;@ assembly/index.ts:6:25
(get_local $4)
)
;;@ assembly/index.ts:6:39
(get_local $5)
)
)
;;@ assembly/index.ts:7:4
(block $break|1
;;@ assembly/index.ts:7:9
(set_local $8
;;@ assembly/index.ts:7:22
(i32.const 0)
)
(loop $continue|1
(if
;;@ assembly/index.ts:7:25
(i32.lt_u
(get_local $8)
;;@ assembly/index.ts:7:29
(get_local $0)
)
(block
(block
;;@ assembly/index.ts:8:6
(set_local $9
;;@ assembly/index.ts:8:17
(f64.mul
(f64.sub
(f64.convert_u/i32
;;@ assembly/index.ts:8:18
(get_local $8)
)
;;@ assembly/index.ts:8:22
(get_local $3)
)
;;@ assembly/index.ts:8:36
(get_local $5)
)
)
;;@ assembly/index.ts:9:6
(set_local $10
;;@ assembly/index.ts:9:22
(i32.const 0)
)
;;@ assembly/index.ts:10:6
(block $break|2
(block
(set_local $11
;;@ assembly/index.ts:10:20
(f64.const 0)
)
(set_local $12
;;@ assembly/index.ts:10:30
(f64.const 0)
)
)
(loop $continue|2
(if
;;@ assembly/index.ts:10:35
(f64.le
(f64.add
(f64.mul
(get_local $11)
;;@ assembly/index.ts:10:40
(get_local $11)
)
;;@ assembly/index.ts:10:45
(f64.mul
(get_local $12)
;;@ assembly/index.ts:10:50
(get_local $12)
)
)
;;@ assembly/index.ts:10:56
(f64.const 4)
)
(block
(block
;;@ assembly/index.ts:11:8
(set_local $13
;;@ assembly/index.ts:11:16
(f64.add
(f64.sub
(f64.mul
(get_local $11)
;;@ assembly/index.ts:11:21
(get_local $11)
)
;;@ assembly/index.ts:11:26
(f64.mul
(get_local $12)
;;@ assembly/index.ts:11:31
(get_local $12)
)
)
;;@ assembly/index.ts:11:36
(get_local $9)
)
)
;;@ assembly/index.ts:12:8
(set_local $12
;;@ assembly/index.ts:12:13
(f64.add
(f64.mul
(f64.mul
(f64.const 2)
;;@ assembly/index.ts:12:19
(get_local $11)
)
;;@ assembly/index.ts:12:24
(get_local $12)
)
;;@ assembly/index.ts:12:29
(get_local $7)
)
)
;;@ assembly/index.ts:13:8
(set_local $11
;;@ assembly/index.ts:13:13
(get_local $13)
)
;;@ assembly/index.ts:14:8
(if
;;@ assembly/index.ts:14:12
(i32.ge_u
(tee_local $10
(i32.add
;;@ assembly/index.ts:14:14
(get_local $10)
(i32.const 1)
)
)
;;@ assembly/index.ts:14:27
(get_local $2)
)
;;@ assembly/index.ts:14:32
(br $break|2)
)
)
;;@ assembly/index.ts:10:61
(set_local $10
(i32.add
;;@ assembly/index.ts:10:63
(get_local $10)
(i32.const 1)
)
)
(br $continue|2)
)
)
)
)
;;@ assembly/index.ts:16:6
(i32.store8
;;@ assembly/index.ts:16:16
(i32.add
(i32.mul
(get_local $6)
;;@ assembly/index.ts:16:20
(get_local $0)
)
;;@ assembly/index.ts:16:28
(get_local $8)
)
;;@ assembly/index.ts:16:31
(i32.div_u
(i32.mul
(get_local $10)
;;@ assembly/index.ts:16:43
(i32.const 255)
)
;;@ assembly/index.ts:16:49
(get_local $2)
)
)
)
;;@ assembly/index.ts:7:36
(set_local $8
(i32.add
;;@ assembly/index.ts:7:38
(get_local $8)
(i32.const 1)
)
)
(br $continue|1)
)
)
)
)
)
;;@ assembly/index.ts:5:35
(set_local $6
(i32.add
;;@ assembly/index.ts:5:37
(get_local $6)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
)

View File

@ -0,0 +1,78 @@
<!DOCTYPE html>
<html>
<head>
<title>Mandelbrot Set - AssemblyScript</title>
<style>
html, body { height: 100%; margin: 0; overflow: hidden; color: #111; background: #fff; font-family: sans-serif; }
h1 { padding: 20px; font-size: 12pt; }
a { color: #111; text-decoration: none; }
a:hover { color: #0074C1; text-decoration: underline; }
canvas { position: absolute; top: 60px; left: 20px; width: calc(100% - 40px); height: calc(100% - 80px); background: #eee; }
</style>
</head>
<body>
<h1>
<a href="https://en.wikipedia.org/wiki/Mandelbrot_set">Mandelbrot Set</a> in
<a href="http://assemblyscript.org">AssemblyScript</a>
</h1>
<canvas></canvas>
<script>
// Set up a 2D rendering context
var cnv = document.getElementsByTagName("canvas")[0];
var ctx = cnv.getContext("2d");
var bcr = cnv.getBoundingClientRect();
cnv.width = bcr.width | 0;
cnv.height = bcr.height | 0;
// Fetch and instantiate the module
fetch("build/optimized.wasm")
.then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer))
.then(module => {
var exports = module.instance.exports;
var memory = exports.memory;
// Compute required memory size and grow if necessary
var width = cnv.width | 0;
var height = cnv.height | 0;
var byteSize = width * height;
if (memory.buffer.byteLength < byteSize) {
let pages = ((byteSize - memory.buffer.byteLength + 0xffff) & ~0xffff) >>> 16;
memory.grow(pages);
}
// Update
exports.update(width, height, 100);
// Render
var mem = new Uint8Array(memory.buffer);
var imageData = ctx.createImageData(width, height);
var argb = new Uint32Array(imageData.data.buffer);
for (let y = 0; y < height; ++y) {
let yx = y * width;
for (let x = 0; x < width; ++x) argb[yx + x] = colors[mem[yx + x]];
}
ctx.putImageData(imageData, 0, 0);
});
// Compute a nice set of colors using a gradient
var colors = (() => {
var cnv = document.createElement("canvas");
cnv.width = 256;
cnv.height = 1;
var ctx = cnv.getContext("2d");
var grd = ctx.createLinearGradient(0, 0, 256, 0);
grd.addColorStop(0, "#000764");
grd.addColorStop(0.16, "#2068CB");
grd.addColorStop(0.42, "#EDFFFF");
grd.addColorStop(0.6425, "#FFAA00");
grd.addColorStop(0.8575, "#000200");
grd.addColorStop(1.0, "#000764");
ctx.fillStyle = grd;
ctx.fillRect(0, 0, 256, 1);
return new Uint32Array(ctx.getImageData(0, 0, 256, 1).data.buffer);
})();
</script>
</body>
</html>

View File

@ -0,0 +1,6 @@
const fs = require("fs");
const compiled = new WebAssembly.Module(fs.readFileSync(__dirname + "/build/optimized.wasm"));
const imports = {};
Object.defineProperty(module, "exports", {
get: () => new WebAssembly.Instance(compiled, imports).exports
});

View File

@ -0,0 +1,14 @@
{
"name": "@assemblyscript/mandelbrot-example",
"version": "1.0.0",
"private": true,
"scripts": {
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --measure",
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --sourceMap --validate --noDebug --measure",
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized",
"server": "http-server . -o -c-1"
},
"devDependencies": {
"http-server": "^0.11.1"
}
}