A real-time implementation of "Ray Tracing in One Weekend" using nannou and rust-gpu.

Overview

Real-time Ray Tracing with nannou & rust-gpu

An attempt at a real-time implementation of "Ray Tracing in One Weekend" by Peter Shirley. This was a personal experiment to learn more about rust-gpu, ray tracing, and what limitations there are around ray tracing in real-time.

screenshot

Performance

A small GUI was developed to tweak the most performance intensive parameters to better understand what affects performance. E.g. on my XPS 13 with integrated GPU the render above was taken with 20 rays per pixel and runs at around 3 FPS, however running the same scene with 2 rays per pixel yields ~25 FPS but with a much noisier result.

Build Requirements

  • Use rustup to install nightly Rust and include the llvm-tools-preview, rustc-dev and rust-src components. These are necessary for the rust-gpu spir-v builder to function.
  • nannou is used to provide the event loop, wgpu graphics pipeline and a small GUI for tweaking performance. Take a look at the platform-specific requirements for nannou projects here.

Running

To run the project, use:

cargo run --release -p nannou-raytracer-app

Code Structure

There are 3 crates in this repo:

  • app is the main application that provides the GUI, builds the Rust shader via SpirvBuilder and sets up the WGPU pipeline.
  • shader is the crate containing both the fragment shader and vertex shader entrypoints (main_fs and main_vs).
  • shared contains code shared between both app and shader. It declares and implements most of the ray-tracing abstractions and logic. By implementing most stuff in a shared crate, I could more easily debug certain functions on the CPU in the app if necessary.

rust-gpu

rust-gpu is still very rough around the edges but is already approaching a dream-come-true.

Being able to share code between the CPU and GPU was especially helpful for debugging. I had a few bugs (poor RNG, tracing an infinite loop, etc) that would normally be a nightmare to debug in a shader, however in this case I could just call the same function on the CPU, add some dbg!s and work out what the issue was in no time.

I especially look forward to support for ADTs (enums with data) as this would make looping over Materials and Hittable objects much easier (the current workaround uses IDs and indices in a rather hacky manner). Support for trait objects would be equally nice, though it looks like this would require changes to the Vulkan specification itself.

You might also like...
A gpu accelerated (optional) neural network Rust crate.

Intricate A GPU accelerated library that creates/trains/runs neural networks in pure safe Rust code. Architechture overview Intricate has a layout ver

LLaMa 7b with CUDA acceleration implemented in rust. Minimal GPU memory needed!

LLaMa 7b in rust This repo contains the popular LLaMa 7b language model, fully implemented in the rust programming language! Uses dfdx tensors and CUD

A fun, hackable, GPU-accelerated, neural network library in Rust, written by an idiot

Tensorken: A Fun, Hackable, GPU-Accelerated, Neural Network library in Rust, Written by an Idiot (work in progress) Understanding deep learning from t

Signed distance functions + Rust (CPU & GPU) = ❤️❤️
Signed distance functions + Rust (CPU & GPU) = ❤️❤️

sdf-playground Signed distance functions + Rust (CPU & GPU) = ❤️❤️ Platforms: Windows, Mac & Linux. About sdf-playground is a demo showcasing how you

rust-gpu CLI driver

rust-gpu-driver Experiment to make rust-gpu more accessible as a GPU shading language in various projects. DISCLAIMER: This is an unstable experiment

Open Machine Intelligence Framework for Hackers. (GPU/CPU)

Leaf • Introduction Leaf is a open Machine Learning Framework for hackers to build classical, deep or hybrid machine learning applications. It was ins

Damavand is a quantum circuit simulator. It can  run on laptops or High Performance Computing architectures, such CPU distributed architectures or multi GPU distributed architectures.
Damavand is a quantum circuit simulator. It can run on laptops or High Performance Computing architectures, such CPU distributed architectures or multi GPU distributed architectures.

Damavand is a code that simulates quantum circuits. In order to learn more about damavand, refer to the documentation. Development status Core feature

A library implementing GPU-accelerated cryptographic functionality for the zkSync prover.
A library implementing GPU-accelerated cryptographic functionality for the zkSync prover.

zkSync Era: A ZK Rollup For Scaling Ethereum zkSync Era is a layer 2 rollup that uses zero-knowledge proofs to scale Ethereum without compromising on

An NVIDIA SMI'esk GPU Monitoring tool for your terminal.
An NVIDIA SMI'esk GPU Monitoring tool for your terminal.

NVTOP An NVIDIA SMI'esk GPU Monitoring tool for your terminal. art by stable-diffusion + Maz Contents: usage prerequisites installation why troublesho

Comments
  • Add rust-toolchain file, ie. pins Rust version

    Add rust-toolchain file, ie. pins Rust version

    This is really such a wonderful project. First because I never realised how "simple" ray tracing was, and secondly it's such a relief to be playing with shaders in sane language ❤

    Commit message: The rust-toolchain is just copied from the rustgpu repo, as it seems the most fussy about which Rust it uses.

    I also changed the references from mitchmindtree's old fork of rustgpu to the native Embark repo, and pinned to the latest commit. Might have been better to pin to the latest tag, but I'd already had a day full of compiling (I seem to have to compile for rust-analyzer as well??). Anyway it all works now, and everything that can be pinned is pinned.

    opened by tombh 2
  • Crash while compiling shader on latest nightly

    Crash while compiling shader on latest nightly

    Screenshots and concept looks awesome and I can't wait to try it. On the latest nightly on my machine, it crashes with the following error though, trying to compile the shader I'm guessing:

         Running `target/release/nannou-raytracer-app`
       Compiling nannou-raytracer-shader v0.1.0 (/home/erik/git/nannou-rustgpu-raytracer/shader)
    warning: unused import: `Vec3`
     --> shader/src/lib.rs:9:47
      |
    9 | use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec4};
      |                                               ^^^^
      |
      = note: `#[warn(unused_imports)]` on by default
    
    error: OpCopyMemorySized without OpCapability Addresses
        --> /home/erik/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/intrinsics.rs:1861:14
         |
    1861 |     unsafe { copy_nonoverlapping(src, dst, count) }
         |              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    
    error: aborting due to previous error; 1 warning emitted
    
    error: could not compile `nannou-raytracer-shader`
    
    To learn more, run the command again with --verbose.
    thread 'main' panicked at 'Crate had no .spv artifacts', /home/erik/.cargo/git/checkouts/rust-gpu-8a6af324770bf590/a1e349b/crates/spirv-builder/src/lib.rs:465:37
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    [1]    26638 segmentation fault (core dumped)  cargo run --release -p nannou-raytracer-app
    

    What nightly version are you using? I tried a couple of different ones from May, but those wouldn't compile at all. I also needed to install llvm-tools-preview for compilation to work and I installed rust-src and rustc-dev as components, guessing those were what you meant in the readme.

    opened by ErikNatanael 6
Owner
null
Wonnx - a GPU-accelerated ONNX inference run-time written 100% in Rust, ready for the web

Wonnx is a GPU-accelerated ONNX inference run-time written 100% in Rust, ready for the web. Supported Platforms (enabled by wgpu) API Windows Linux &

WebONNX 354 Jan 6, 2023
A repo for learning how to parallelize computations in the GPU using Apple's Metal, in Rust.

Metal playground in rust Made for learning how to parallelize computations in the GPU using Apple's Metal, in Rust, via the metal crate. Overview The

Lambdaclass 5 Feb 20, 2023
Rust implementation of real-coded GA for solving optimization problems and training of neural networks

revonet Rust implementation of real-coded genetic algorithm for solving optimization problems and training of neural networks. The latter is also know

Yury Tsoy 19 Aug 11, 2022
Ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust.

Ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust.

Riccardo D'Ambrosio 2.1k Jan 5, 2023
Ecosystem of libraries and tools for writing and executing fast GPU code fully in Rust.

The Rust CUDA Project An ecosystem of libraries and tools for writing and executing extremely fast GPU code fully in Rust Guide | Getting Started | Fe

Rust GPU 2.1k Dec 30, 2022
🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧

?? rust-gpu Rust as a first-class language and ecosystem for GPU graphics & compute shaders Current Status ?? Note: This project is still heavily in d

Embark 5.5k Jan 9, 2023
Open deep learning compiler stack for cpu, gpu and specialized accelerators

Open Deep Learning Compiler Stack Documentation | Contributors | Community | Release Notes Apache TVM is a compiler stack for deep learning systems. I

The Apache Software Foundation 8.9k Jan 4, 2023
Rust based Cross-GPU Machine Learning

HAL : Hyper Adaptive Learning Rust based Cross-GPU Machine Learning. Why Rust? This project is for those that miss strongly typed compiled languages.

Jason Ramapuram 83 Dec 20, 2022
How to: Run Rust code on your NVIDIA GPU

Status This documentation about an unstable feature is UNMAINTAINED and was written over a year ago. Things may have drastically changed since then; r

null 343 Dec 22, 2022
A Demo server serving Bert through ONNX with GPU written in Rust with <3

Demo BERT ONNX server written in rust This demo showcase the use of onnxruntime-rs on BERT with a GPU on CUDA 11 served by actix-web and tokenized wit

Xavier Tao 28 Jan 1, 2023