diff --git a/.gitignore b/.gitignore index 8089110..77c55de 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ pirates/target +build diff --git a/front/index.html b/front/index.html new file mode 100644 index 0000000..7a9803c --- /dev/null +++ b/front/index.html @@ -0,0 +1,11 @@ + + + + + + + + + + diff --git a/front/index.js b/front/index.js new file mode 100644 index 0000000..7d27a7e --- /dev/null +++ b/front/index.js @@ -0,0 +1,35 @@ +async function init() { + const { instance } = await WebAssembly.instantiateStreaming( + fetch("./pirates.wasm") + ); + + const width = 600; + const height = 600; + + const canvas = document.getElementById("window"); + canvas.width = width; + canvas.height = height; + + const buffer_address = instance.exports.BUFFER.value; + const image = new ImageData( + new Uint8ClampedArray( + instance.exports.memory.buffer, + buffer_address, + 4 * width * height, + ), + width, + ); + + const ctx = canvas.getContext("2d"); + + const render = () => { + instance.exports.frame_entry(); + ctx.putImageData(image, 0, 0); + + requestAnimationFrame(render); + } + + render(); +} + +init(); diff --git a/justfile b/justfile index 192985d..14defcd 100644 --- a/justfile +++ b/justfile @@ -1,7 +1,42 @@ +alias b := build +alias r := run +alias c := check-zip + +build: minify-js minify-rust minify-html + build-rust: cargo build --manifest-path pirates/Cargo.toml --target wasm32-unknown-unknown --release -minify-rust: build-rust +minify-rust: build-rust build-dir wasm-strip pirates/target/wasm32-unknown-unknown/release/pirates.wasm - wasm-opt -o pirates/target/wasm32-unknown-unknown/release/pirates-opt.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm - + wasm-opt -o build/pirates.wasm -Oz pirates/target/wasm32-unknown-unknown/release/pirates.wasm + +minify-js: build-dir + #minify-js -m module --output build/index.js front/index.js + minify front/index.js > build/index.js + +minify-html: build-dir + minify front/index.html > build/index.html + +[private] +build-dir: + mkdir -p build + +check-size: build + dust -s build + +check-zip: zip + unzip -v build/release.zip | awk '{printf ("%5s\t%s\n", $3, $8)}' + + @cat build/release.zip | wc -c | xargs -I {} python3 -c "print(str(round({}/(13*1024),2))+'%')" + +zip: build + zip -r build/release.zip build -x release.zip + +run: build + python3 -m http.server & + firefox http://0.0.0.0:8080/build/index.html + +clean: + cargo clean --manifest-path pirates/Cargo.toml + rm -r build diff --git a/pirates/src/lib.rs b/pirates/src/lib.rs index 7d12d9a..24d86e8 100644 --- a/pirates/src/lib.rs +++ b/pirates/src/lib.rs @@ -1,5 +1,33 @@ -pub fn add(left: usize, right: usize) -> usize { - left + right +#![no_std] + +use core::sync::atomic::{AtomicU32, Ordering}; + +#[panic_handler] +fn handle_panic(_: &core::panic::PanicInfo) -> ! { + loop {} +} + +const WIDTH: usize = 600; +const HEIGHT: usize = 600; + +#[no_mangle] +static mut BUFFER: [u32; WIDTH * HEIGHT] = [0; WIDTH * HEIGHT]; + +static FRAME: AtomicU32 = AtomicU32::new(0); + +#[no_mangle] +pub unsafe extern fn frame_entry() { + // calling from multiple threads is ub + render_frame(&mut BUFFER) +} + +fn render_frame(buffer: &mut [u32; WIDTH*HEIGHT]) { + let frame = FRAME.fetch_add(1, Ordering::Relaxed); + for y in 0..HEIGHT { + for x in 0..WIDTH { + buffer[y*WIDTH + x] = frame.wrapping_add((x^y) as u32) | 0xFF000000; + } + } } #[cfg(test)] @@ -8,7 +36,7 @@ mod tests { #[test] fn it_works() { - let result = add(2, 2); - assert_eq!(result, 4); + let result = number(); + assert_eq!(result, 64); } }