From c62e9fa15c1d909d50e61b21bfcc68bdaaf31e97 Mon Sep 17 00:00:00 2001 From: Magnus Ulimoen Date: Thu, 11 Apr 2019 23:57:29 +0200 Subject: [PATCH] include rust --- .gitignore | 2 ++ Cargo.toml | 20 ++++++++++++++++++++ index.html | 2 +- main.js | 16 +++++++++++++--- make_wasm.py | 23 +++++++++++++++++++++++ src/lib.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 102 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 Cargo.toml create mode 100755 make_wasm.py create mode 100644 src/lib.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f0e3bca --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/target +**/*.rs.bk \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..e116c09 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "webgl" +version = "0.1.0" +authors = ["Magnus Ulimoen "] +edition = "2018" + +[lib] +crate-type = ["cdylib", "rlib"] + +[features] +default = ["console_error_panic_hook", "wee_alloc"] + +[dependencies] +wasm-bindgen = "*" +console_error_panic_hook = { version = "*", optional = true } +wee_alloc = { version = "*", optional = true } + +[profile.release] +opt-level = "s" +lto = "full" diff --git a/index.html b/index.html index fd07c24..4b4f532 100644 --- a/index.html +++ b/index.html @@ -11,5 +11,5 @@ - + diff --git a/main.js b/main.js index c5ac8ba..48ec5dd 100644 --- a/main.js +++ b/main.js @@ -1,8 +1,8 @@ -"use strict"; +import { Universe, default as init } from "./webgl.js"; -main(); +async function run() { + let wasm = await init("./webgl_bg.wasm"); -function main() { const canvas = document.getElementById("glCanvas"); const gl = canvas.getContext("webgl"); @@ -82,6 +82,8 @@ function main() { const width = 4; const height = 5; + let universe = Universe.new(width, height); + /* const field = new Uint8Array(width*height); for (let i = 0; i < height; i += 1) { for (let j = 0; j < width; j += 1) { @@ -91,6 +93,12 @@ function main() { field[i*width + j] = Math.floor(Math.random() * (255 + 1)); } } + */ + universe.set_something(); + const field = new Uint8Array(wasm.memory.buffer, universe.get_field(), width*height); + console.log(field); + console.log(wasm.memory.buffer); + console.log(universe.get_field()); const texture = gl.createTexture(); { @@ -156,3 +164,5 @@ function main() { window.addEventListener('resize', resizeCanvas, false); resizeCanvas(); } + +run(); diff --git a/make_wasm.py b/make_wasm.py new file mode 100755 index 0000000..deac9c7 --- /dev/null +++ b/make_wasm.py @@ -0,0 +1,23 @@ +#! /usr/bin/env python3 + +from argparse import ArgumentParser +from subprocess import call + +if __name__ == "__main__": + parser = ArgumentParser(description="Build js and wasm") + parser.add_argument("-r", help="Build release type", + dest="release", action="store_true") + + args = parser.parse_args() + + if args.release: + call(["cargo", "build", "--release", + "--target", "wasm32-unknown-unknown"]) + target = "target/wasm32-unknown-unknown/release/webgl.wasm" + else: + call(["cargo", "build", + "--target", "wasm32-unknown-unknown"]) + target = "target/wasm32-unknown-unknown/debug/webgl.wasm" + + call(["wasm-bindgen", target, "--out-dir", ".", + "--no-typescript", "--target", "web"]) diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..6e19c4e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,43 @@ +use wasm_bindgen::prelude::*; + +#[cfg(feature = "wee_alloc")] +#[global_allocator] +static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT; + +pub fn set_panic_hook() { + #[cfg(feature = "console_error_panic_hook")] + console_error_panic_hook::set_once(); +} + +#[wasm_bindgen] +pub struct Universe { + width: u32, + height: u32, + field: Vec, +} + +#[wasm_bindgen] +impl Universe { + pub fn new(width: u32, height: u32) -> Self { + set_panic_hook(); + + Universe { + width, + height, + field: vec![0u8; width as usize * height as usize], + } + } + + pub fn set_something(&mut self) { + for j in 0..self.height { + for i in 0..self.width { + self.field[(self.width * j + i) as usize] = + ((10 * i + 100 * j) % (u8::max_value() as u32)) as u8; + } + } + } + + pub fn get_field(&mut self) -> *mut u8 { + self.field.as_mut_ptr() + } +}