|
| 1 | +# path_tracer |
| 2 | + |
| 3 | +A cross-platform **demo** GPU ray tracer built with Rust and WebGPU, demonstrating modern GPU programming techniques. Runs natively on Windows, macOS, and Linux, as well as in web browsers from the same codebase. |
| 4 | + |
| 5 | +## Why WebGPU? |
| 6 | + |
| 7 | +GPU programming offers massive parallelism for graphics and compute workloads, but has historically been fragmented across incompatible APIs: |
| 8 | + |
| 9 | +* **OpenGL**: Aging API with inconsistent driver support |
| 10 | +* **DirectX**: Windows-only |
| 11 | +* **Metal**: Apple platforms only |
| 12 | +* **Vulkan**: Verbose, complex to learn |
| 13 | +* **CUDA & OpenCL**: Compute-focused, vendor-specific or limited adoption |
| 14 | + |
| 15 | +Unlike CPU code that runs anywhere with minimal changes, GPU developers traditionally had to choose between portability and access to modern features. |
| 16 | + |
| 17 | +WebGPU solves this. It's a modern, cross-platform GPU API that provides a single interface across all major platforms and browsers. WebGPU maps efficiently to DirectX 12, Metal, and Vulkan under the hood without sacrificing performance. The W3C WebGPU spec reached a Candidate Recommendation Draft in 2025. |
| 18 | + |
| 19 | +This project demonstrates WebGPU capabilities using **wgpu** (the Rust implementation) and serves as a practical learning resource for modern GPU programming. |
| 20 | + |
| 21 | +## What It Does |
| 22 | + |
| 23 | +This is a real-time GPU ray tracer that: |
| 24 | + |
| 25 | +* Renders 3D scenes loaded from glTF (.glb) files (tested with Blender exported files) |
| 26 | +* Runs the same Rust codebase natively (desktop) and on the web (via WebAssembly) |
| 27 | +* Uses compute shaders for ray-triangle intersection (Möller-Trumbore algorithm) |
| 28 | +* Handles first-person camera controls with keyboard and mouse input |
| 29 | +* Demonstrates core WebGPU (wgpu v27) concepts: bind groups, compute/vertex/fragment pipelines, WGSL compute/vertex/fragment shaders, storage textures, etc. |
| 30 | + |
| 31 | +**Current Status**: This is a basic ray tracer skeleton (~1300 lines of code in total). It traces primary rays only and lacks features like recursive path tracing or acceleration structures (BVH). |
| 32 | + |
| 33 | +## Build & Run |
| 34 | + |
| 35 | +### Prerequisites |
| 36 | + |
| 37 | +1. Install [Rust](https://rustup.rs/) |
| 38 | +2. For web builds, install wasm-pack: |
| 39 | + |
| 40 | + ```bash |
| 41 | + cargo install wasm-pack |
| 42 | + ``` |
| 43 | + |
| 44 | +3. For web hosting on localhost, use Python's built-in server or install simple-http-server: |
| 45 | + |
| 46 | + ```bash |
| 47 | + cargo install simple-http-server |
| 48 | + ``` |
| 49 | + |
| 50 | +### Native (Desktop) |
| 51 | + |
| 52 | +```bash |
| 53 | +git clone https://github.com/0dpe/path_tracer.git |
| 54 | +cd path_tracer |
| 55 | +cargo run --release |
| 56 | +``` |
| 57 | + |
| 58 | +**Controls**: Left click the window, then use <kbd>W</kbd><kbd>A</kbd><kbd>S</kbd><kbd>D</kbd> + <kbd>Left Shift</kbd>/<kbd>Space</kbd> to move, mouse to look around. Press <kbd>Esc</kbd> or click again to release cursor. |
| 59 | + |
| 60 | +### Web (Browser) |
| 61 | + |
| 62 | +```bash |
| 63 | +git clone https://github.com/0dpe/path_tracer.git |
| 64 | +cd path_tracer |
| 65 | +wasm-pack build --target web |
| 66 | + |
| 67 | +# serve the folder |
| 68 | +simple-http-server |
| 69 | +# Or: python -m http.server 8000 |
| 70 | +``` |
| 71 | + |
| 72 | +Open Chrome at `http://localhost:8000` and click on `index.html`. Same controls as native. |
| 73 | + |
| 74 | +> [!NOTE] |
| 75 | +> Requires a browser with WebGPU support (Chrome stable, Safari/Firefox with flags enabled as of 2025). |
| 76 | +
|
| 77 | +## General Crate Structure |
| 78 | + |
| 79 | +```text |
| 80 | +path_tracer/ |
| 81 | +├── assets/ # .glb scene files |
| 82 | +├── src/ |
| 83 | +│ ├── render/ # WebGPU setup, shaders, scene loading |
| 84 | +│ ├── lib.rs # Window management and event loop |
| 85 | +│ └── main.rs # Native entry point |
| 86 | +├── Cargo.toml # Dependencies and wgpu version |
| 87 | +└── index.html # Webpage |
| 88 | +``` |
0 commit comments