Introduction
Welcome to the Unreal Majid tutorial series. In this guide, we will build a high-performance GPU particle simulation from the ground up using Rust and wgpu.
What We Are Building
We are building a particle system that simulates physics entirely on the GPU. Instead of updating particle positions on the CPU (which is slow for large numbers), we will use Compute Shaders to update millions of particles in parallel every frame.
CPUs have a few powerful cores (great for logic), while GPUs have thousands of smaller cores (great for parallel tasks). A CPU might struggle with 10,000 particles, but a modern GPU can easily handle 1,000,000+ at 60 FPS.
Key Features
- Cross-Platform: Runs natively on Windows, macOS, Linux, and in the browser via WebAssembly.
- Compute Shaders: Physics calculations (velocity, acceleration, gravity) happen on the GPU.
- Interactive Camera: A fly-cam implementation to navigate the particle cloud.
- Dynamic Controls: Toggle gravity, reset simulation, and attract particles with the mouse.
Technologies Used
Rust
We use Rust for its memory safety, performance, and excellent WebAssembly support. It prevents entire classes of bugs (like null pointer dereferences) at compile time.
wgpu
wgpu is a cross-platform graphics API for Rust. It is the core of our rendering engine. It abstracts over Vulkan, DirectX 12, Metal, and WebGPU, allowing us to write code once and run it anywhere.
WebAssembly (WASM)
By compiling our Rust code to WASM, we can run this high-performance simulation directly in a web browser with near-native speed.
Project Structure Overview
Here is a high-level look at the files we will create:
Unreal_Majid/
├── src/
│ ├── main.rs (Native entry point)
│ ├── lib.rs (WASM entry point)
│ └── core/
│ ├── renderer.rs (The heavy lifter: GPU setup & render loop)
│ └── window.rs (Window creation & input handling)
├── compute_shader.wgsl (GPU Physics code)
├── draw_shader.wgsl (GPU Rendering code)
├── index.html (Web page for WASM)
├── Cargo.toml (Dependencies)
└── Makefile (Build scripts)
Don't worry if this looks like a lot. We will go through each file line-by-line.