include rust

This commit is contained in:
Magnus Ulimoen 2019-04-11 23:57:29 +02:00
parent 86b8c7f899
commit c62e9fa15c
6 changed files with 102 additions and 4 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
/target
**/*.rs.bk

20
Cargo.toml Normal file
View File

@ -0,0 +1,20 @@
[package]
name = "webgl"
version = "0.1.0"
authors = ["Magnus Ulimoen <flymagnus@gmail.com>"]
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"

View File

@ -11,5 +11,5 @@
<canvas id="glCanvas"></canvas> <canvas id="glCanvas"></canvas>
</body> </body>
<link rel="stylesheet" type="text/css" href="style.css"> <link rel="stylesheet" type="text/css" href="style.css">
<script src="main.js"></script> <script async type="module" src="main.js"></script>
</html> </html>

16
main.js
View File

@ -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 canvas = document.getElementById("glCanvas");
const gl = canvas.getContext("webgl"); const gl = canvas.getContext("webgl");
@ -82,6 +82,8 @@ function main() {
const width = 4; const width = 4;
const height = 5; const height = 5;
let universe = Universe.new(width, height);
/*
const field = new Uint8Array(width*height); const field = new Uint8Array(width*height);
for (let i = 0; i < height; i += 1) { for (let i = 0; i < height; i += 1) {
for (let j = 0; j < width; j += 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)); 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(); const texture = gl.createTexture();
{ {
@ -156,3 +164,5 @@ function main() {
window.addEventListener('resize', resizeCanvas, false); window.addEventListener('resize', resizeCanvas, false);
resizeCanvas(); resizeCanvas();
} }
run();

23
make_wasm.py Executable file
View File

@ -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"])

43
src/lib.rs Normal file
View File

@ -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<u8>,
}
#[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()
}
}