Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
updated AssemblyScript to latest version
- Loading branch information
1 parent
6089569
commit a9e6666
Showing
8 changed files
with
104 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,40 @@ | ||
module.exports = async() => { | ||
module.exports = async () => { | ||
|
||
const WIDTH = 1200, HEIGHT = 800; | ||
const WIDTH = 1200; | ||
const HEIGHT = 800; | ||
const PAGE_SIZE = 65536; | ||
|
||
function allocateMemory() { | ||
const initial = Math.floor(WIDTH * HEIGHT * 4 / PAGE_SIZE) + 1; | ||
return new WebAssembly.Memory({ initial }); | ||
} | ||
|
||
const imports = { | ||
env: { | ||
memoryBase: 0, | ||
memory: allocateMemory() | ||
} | ||
}; | ||
|
||
const res = await fetch('assemblyscript/mandelbrot.wasm'); | ||
const buffer = await res.arrayBuffer(); | ||
const instance = await WebAssembly.instantiate(buffer, {}); | ||
|
||
|
||
const module = await WebAssembly.instantiate(buffer, imports); | ||
const { mandelbrot, getDataBuffer, memory } = module.instance.exports; | ||
let imgData = null; | ||
|
||
return { | ||
render: (ctx, config) => { | ||
console.log(instance.instance.exports.structured()); | ||
instance.instance.exports.mandelbrot(config.iterations, config.x, config.y, config.d); | ||
render: (ctx, { iterations, x, y, d }) => { | ||
mandelbrot(iterations, x, y, d); | ||
|
||
const imgData = ctx.createImageData(WIDTH, HEIGHT); | ||
const offset = instance.instance.exports.getData(); | ||
const linearMemory = new Uint8Array(instance.instance.exports.memory.buffer, offset, WIDTH * HEIGHT * 4); | ||
for (let i = 0; i < linearMemory.length; i++) { | ||
if (!imgData) imgData = ctx.createImageData(WIDTH, HEIGHT); | ||
const offset = getDataBuffer(); | ||
const linearMemory = new Uint8Array(memory.buffer, offset, WIDTH * HEIGHT * 4); | ||
for (let i = 0, len = linearMemory.length; i < len; i++) { | ||
imgData.data[i] = linearMemory[i]; | ||
} | ||
ctx.putImageData(imgData, 0, 0); | ||
}, | ||
name: 'AssemblyScript', | ||
description: 'This implementation uses the AssemblyScript compiler to compile a TypeScript mandelbrot to WebAssembly.' | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,24 @@ | ||
const typeScriptMandelbrot = require('./mandelbrot.js'); | ||
|
||
const WIDTH = 1200; | ||
const HEIGHT = 800; | ||
|
||
var imgData = null; | ||
|
||
const { getData, mandelbrot } = typeScriptMandelbrot; | ||
|
||
module.exports = () => Promise.resolve({ | ||
render: (ctx, config) => { | ||
const WIDTH = 1200, HEIGHT = 800; | ||
|
||
typeScriptMandelbrot.mandelbrot(config.iterations, config.x, config.y, config.d); | ||
|
||
const imgData = ctx.createImageData(WIDTH, HEIGHT); | ||
const linearMemory = typeScriptMandelbrot.getData(); | ||
for (let i = 0; i < linearMemory.length; i++) { | ||
render: (ctx, { iterations, x, y, d }) => { | ||
mandelbrot(iterations, x, y, d); | ||
|
||
if (!imgData) imgData = ctx.createImageData(WIDTH, HEIGHT); | ||
const linearMemory = getData(); | ||
|
||
for (let i = 0, len = linearMemory.length; i < len; i++) { | ||
imgData.data[i] = linearMemory[i]; | ||
} | ||
ctx.putImageData(imgData, 0, 0); | ||
}, | ||
name: 'TypeScript', | ||
description: 'This implementation takes the TypeScript that is used by AssemblyScript to generate WebAssembly, but instead passes it through the TypeScript compiler to create JavaScript.' | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{ | ||
"extends": "./node_modules/assemblyscript/tsconfig.assembly.json", | ||
"extends": "./node_modules/assemblyscript/std/assembly.json", | ||
"include": [ | ||
"./*.ts" | ||
"./**/*.ts" | ||
], | ||
"compilerOptions": { | ||
"target": "es6", | ||
"module": "amd" | ||
} | ||
} |