From CPU multithreading to GPU multithreading

I have created a ray tracing engine, loosely following the Ray Tracing in one weekend book series ( https://raytracing.github.io/ ) and its rust implementation ( https://misterdanb.github.io/raytracinginrust/ ). I have, with great difficulty, implemented CPU multithreading using rayon. I'd now like to transfer this task to the GPU to improve my performance. Is there any way to do that without having to re-write my entire code ?

I'm fairly new to rust, so any help is more than welcome.

Short answer: no.

There's a few crates that are aiming at being able to use "normal Rust" running on the GPU, but they're not really aimed at being able to use existing code, but things like being able to reuse cargo as a library system for gpu code, and they're very early.

GitHub - EmbarkStudios/rust-gpu: 🐉 Making Rust a first-class language and ecosystem for GPU shaders 🚧 is the most advanced one I'm aware of.


My current recommendation is to use wgpu to get a compute backend and rewrite your logic in wgsl, they are standard and clean new apis that are well balanced between abstracting the hardware, giving you low level access, and not distracting with historical cruft. As a bonus it means you can nearly trivially port it to browsers when they get WebGPU (next Chrome version!), which is neat.

2 Likes

so from my understanding, at this point i'd almost be better off rewriting it entirely in another language that has better GPU libraries.

Potentially, but with the caveat that I actually don't know of any language that has better GPU libraries: Rust is actually at the bleeding edge there from what I can tell, for a few reasons.

You might argue CUDA is more mature, but it's both Nvidia only and in my opinion really crufty and weird. GLSL is very confusing to work in as it has so many flavors and split documentation, HLSL is better but requires cross compilation for portability which is messy to set up.

WGSL is quite nice now and feels pretty natural to a Rust user, and wgpu is a little verbose at worst in comparison to the best alternatives out there for compute, and not by much.

well thanks for the answer! I'll try seeing if i can implement my logic without switching languages, but at worst that will give me an excuse to finally learn to use game engines.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.