In fact you can simply copy a rust file in the “bin” folder and just go to the wgsl stage. But to set the parameters in egui you only need to change the parameters.
Note: Please don't be surprised to see that some Uniforms are common both in “baseshader.rs” (an important backend file in cuneus where most things are init) and in the final rust file we created for our shader (like mandelbrot.rs). The only purpose of this is related to hot reload. :-)
- Copy one of the template files from
src/bin/
that best matches your needs:mandelbrot.rs
: Minimal single-pass shader without GUI controlsspiral.rs
: Simple single-pass shader with texture supportfeedback.rs
: Basic two-pass shaderfluid.rs
: Multi-pass shader with texture supportattractor.rs
: Three-pass rendering examplexmas.rs
: Single pass with extensive parameter controlsaudiovis.rs
Audio visualizer example to show how you can use spectrum/bpm data from rust.
if you want 4 passes or more the logic is exactly the same.
- Rename and modify the copied file to create your shader
- Focus on writing your WGSL shader code :-)
Cuneus supports using Videos as textures, which requires GStreamer bins to be installed on your system.
Install from the GStreamer website, both the runtime and development libraries.
build.rs contains configuration for macOS frameworks and library paths. Adjust as needed for your system for development :-)
Note: GStreamer is only required if you need video texture functionality. Image-based textures should work without it.
Note2 Development libraries are required only for building the project.
Please see how I build the project in github actions, you can use it as a reference:
The simplest way to start is with a basic shader like mandelbrot.rs
. This template includes:
- Core imports and setup
- Minimal shader parameters
- Basic render pipeline
- WGSL shader code
I created this by completely copying and pasting xmas.rs, and I could only focus on my shader.
// 1. Required imports
use cuneus::{Core, ShaderManager, BaseShader /* ... */};
// 2. Optional parameters if needed
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ShaderParams {
// Your parameters here
}
// 3. Main shader structure
struct MyShader {
base: BaseShader,
// Add any additional fields needed
}
// 4. Implement required traits
impl ShaderManager for MyShader {
fn init(core: &Core) -> Self { /* ... */ }
fn update(&mut self, core: &Core) { /* ... */ }
fn render(&mut self, core: &Core) -> Result<(), wgpu::SurfaceError> { /* ... */ }
fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool { /* ... */ }
}
To add parameter controls through egui:
- Define your parameters struct
- Add UI controls in the render function
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ShaderParams {
rotation_speed: f32,
intensity: f32,
// Add more parameters as needed
}
// In render function:
let full_output = if self.base.key_handler.show_ui {
self.base.render_ui(core, |ctx| {
egui::Window::new("Settings").show(ctx, |ui| {
changed |= ui.add(egui::Slider::new(&mut params.rotation_speed, 0.0..=5.0)
.text("Rotation Speed")).changed();
// Add more controls
});
})
};
All shaders use this common vertex shader (vertex.wgsl): If you are not doing anything special, this file can always remain fixed. If you look at my shader examples, they all use this same file.
struct VertexOutput {
@location(0) tex_coords: vec2<f32>,
@builtin(position) out_pos: vec4<f32>,
};
@vertex
fn vs_main(@location(0) pos: vec2<f32>) -> VertexOutput {
let tex_coords = vec2<f32>(pos.x * 0.5 + 0.5, 1.0 - (pos.y * 0.5 + 0.5));
return VertexOutput(tex_coords, vec4<f32>(pos, 0.0, 1.0));
}
Basic structure for a fragment shader:
// Time uniform
@group(0) @binding(0)
var<uniform> u_time: TimeUniform;
// Optional EGUI parameters
@group(1) @binding(0)
var<uniform> params: Params;
@fragment
fn fs_main(@builtin(position) FragCoord: vec4<f32>,
@location(0) tex_coords: vec2<f32>) -> @location(0) vec4<f32> {
// Your shader code here
return vec4<f32>(1.0, 0.0, 0.0, 1.0);
}
For effects requiring multiple passes:
@group(0) @binding(0) var prev_frame: texture_2d<f32>;
@group(0) @binding(1) var tex_sampler: sampler;
@fragment
fn fs_pass1(...) -> @location(0) vec4<f32> {
// First pass processing
}
@fragment
fn fs_pass2(...) -> @location(0) vec4<f32> {
// Second pass processing
}
cuneus supports hot reloading of shaders. Simply modify your WGSL files and they will automatically reload.
Built-in support for exporting frames as images. Access through the UI when enabled. "Start time" is not working correctly currently.
Load and use textures in your shaders:
if let Some(ref texture_manager) = self.base.texture_manager {
render_pass.set_bind_group(0, &texture_manager.bind_group, &[]);
}
cuneus handles both logical and physical resolution:
- Initial window size is set in logical pixels:
let (app, event_loop) = ShaderApp::new("My Shader", 800, 600);
- On high-DPI displays (like Retina), the physical resolution is automatically scaled: e.g., 800x600 logical becomes 1600x1200 physical on a 2x scaling display Your shader's UV coordinates (0.0 to 1.0) automatically adapt to any resolution Export resolution can be set independently through the UI
Your WGSL shaders can access actual dimensions when needed:
let dimensions = vec2<f32>(textureDimensions(my_texture));
On the Rust side, unlike other backends, make sure you only add this single line.
Line 370 in 776434e
I passed the data about spectrum and BPM to the resolution uniform. You can use them from here:
Lines 10 to 15 in 776434e
then use it as you desired.
note that, spectrum data is not raw. I process it on the rust side. If this is not suitable for you, you can fix it. Audio is not my specialty. If you have a better idea please open a PR. https://github.com/altunenes/cuneus/blob/main/src/spectrum.rs#L47
- Start with a template that includes GUI (e.g.,
xmas.rs
) - Define your parameters in the ShaderParams struct
- Add UI controls in the render function
- Connect parameters to your shader