Shadertoys Ported to Rust GPU

https://news.ycombinator.com/rss Hits: 19
Summary

We ported a few popular Shadertoy shaders over to Rust using Rust GPU. The process was straightforward and we want to share some highlights. The code is available on GitHub. What is Rust GPU?​ Rust GPU is a project that allows you to write code for GPUs using the Rust programming language. GPUs are typically programmed using specialized languages like WGSL, GLSL, MSL, or HLSL. Rust GPU changes this by letting you use Rust to write GPU programs (often called "shaders" or "kernels"). These Rust GPU programs are then compiled into SPIR-V, a low-level format that most GPUs understand. Since SPIR-V is the format Vulkan uses, Rust GPU makes it possible to integrate Rust-based GPU programs into any Vulkan-compatible workflow. For more details, check out the Rust GPU website or the GitHub repository. Shared code between CPU and GPU​ Sharing data between the CPU and GPU is common in shader programming. This often requires special tooling or manual effort. Using Rust on both sides made this seamless: #[repr(C)]#[derive(Copy, Clone, Pod, Zeroable)]pub struct ShaderConstants { pub width: u32, pub height: u32, pub time: f32, pub cursor_x: f32, pub cursor_y: f32, pub drag_start_x: f32, pub drag_start_y: f32, pub drag_end_x: f32, pub drag_end_y: f32, pub mouse_left_pressed: u32, pub mouse_left_clicked: u32,} Note that on both the CPU and the GPU we are using the bytemuck crate for the Pod and Zeroable derives. This crate is unmodified and integrated directly from crates.io. Many no_std + no alloc Rust crates work on the GPU! Traits, generics, and macros​ Rust GPU supports traits. We used traits to encapsulate shader-specific operations in reusable ergonomic abstractions: pub trait FloatExt { fn gl_fract(self) -> Self; fn rem_euclid(self, rhs: Self) -> Self; fn gl_sign(self) -> Self; fn deg_to_radians(self) -> Self; fn step(self, x: Self) -> Self;} While there are still some rough edges, generics mostly work as expected. We used them to support multiple channel types without duplic...

First seen: 2025-04-12 22:56

Last seen: 2025-04-14 10:03