Giter Site home page Giter Site logo

rotozoom-wasm's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.