Skip to content

Latest commit

 

History

History
76 lines (56 loc) · 4.61 KB

README.md

File metadata and controls

76 lines (56 loc) · 4.61 KB

Rust RotoZoom to Wasm


Preamble

rotozom-wasm is a cross-platform reimplementation of the SDL rotozoom example in RUST language. While this program have been originally implemented in C language for old framebuffer video cards, nowadays inside current browsers, we can achieve the same effect within a Canvas 2D object using:

  • A bitmap pointer as shared memory (i.e. without copying data)
  • An exposed native function in WASM (without using Wasm-Bindgen dependency library)
  • Pure array of bytes as a framebuffer (i.e. without using the extra Rust std library)
  • Avoid using Rust Wasm wee_alloc as the global allocator (to save extra KB in Wasm)
  • Isolated bytecode (inside browsers) being cross-platform (i.e. ARM ready even with SVE)

Build

Building this project using Cargo is easy because the process is the same as for every other Rust program:

cargo build --release --target wasm32-unknown-unknown

PS: If above default target is not already installed, you can add the given wasm32 target in your toolchain:

rustup target add wasm32-unknown-unknown

Roadmap

  • Raw pointer for zero-copy feature (optimal transfert between JS ⇔ RUST)
  • Faster Mathematical functions using lookup tables (with fixed-width integers?)
  • Split parallel pixels computational rotation calculus using Wasm SIMD extension
  • Separate core loop algorithm into pieces being handled by Wasm Thread extension
  • Implement a defined texture interpolation to improve the rendering (when zoomed)

Dev and testing

You need to serve the generated files with an HTTP server using such command:

python -m http.server 1234

Reverse engineering

A lot of emphasis has been put on code simplicity (and for size) when writing this rotozoom effect. Therefore if you want to see what has been generated inside the WASM bytecode, just run:

cargo install wasm-tools
wasm-tools print target/wasm32-unknown-unknown/release/rotozoom_wasm_rs.wasm

Algorithm

The whole effect is based on 2D transformation used in Computer Graphics and specifically on this rotation matrix calculus in cartesian coordinate system using this formula:

For the algorithm code, previous equation can be written and simplified into discrete math such as:

Reference

This project is a wink and a tribute to the best demoscene group that I've seen, called Future Crew which did release 2nd Reality in 1993 at the Assembly demo party. It contains this original rotozoom effect and texture (thank you Pixel). By the way, their original source code has been released in C + ASM and Fabien Sanglard did a great analysis here.

2nd Reality (1993) by Future Crew
YouTube video emulated on a 486DX PC running under MS/DOS™

License

rotozom-wasm is licensed under the MIT License - see the LICENSE file for more details.