cuneus 0.2.2

A WGPU-based shader development tool
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424

# How To Use Cuneus

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 “RenderKit.rs” (an important backend file in cuneus where most things are init) and in the final rust file we created for our shader. The only purpose of this is related to hot reload. :-)

## Quick Start

1. Copy one of the template files from `src/bin/` that best matches your needs:
    - compute.rs is simply debug screen, you can start with here.


2. Rename and modify the copied file to create your shader
3. Focus on writing your WGSL shader code :-)

## GStreamer Requirement for Video/Camera Textures

Cuneus supports using Videos as textures, which requires GStreamer bins to be installed on your system.

### Installation

Install from the [GStreamer website](https://gstreamer.freedesktop.org/download/#macos), both the runtime and development libraries.

[build.rs](https://github.com/altunenes/cuneus/blob/main/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:

[release](https://github.com/altunenes/cuneus/blob/main/.github/workflows/release.yaml)

## Template Structure

### Basic Single Pass Shader (No GUI)

This template includes:

1. Core imports and setup
2. Minimal shader parameters
3. Basic render pipeline
4. WGSL shader code


```rust
// 1. Required imports
use cuneus::{Core, ShaderManager, RenderKit /* ... */};

// 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: RenderKit,
    // 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 { /* ... */ }
}
```

### Adding GUI Controls (optimal)

To add parameter controls through egui:

1. Define your parameters struct
2. Add UI controls in the render function

```rust
#[repr(C)]
#[derive(Copy, Clone, Debug, bytemuck::Pod, bytemuck::Zeroable)]
struct ShaderParams {
    rotation_speed: f32,
    intensity: f32,
    // Add more parameters as needed, note please be careful about the GPU memory aligment! (I usally use paddings)
}

// 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
        });
    })
};
```

## WGSL Shader Patterns

### Standard Vertex Shader
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.

```wgsl
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));
}
```

### Single Pass Fragment Shader
Basic structure for a fragment shader:
```wgsl
// 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);
}
```

### Multi-Pass Shader
For effects requiring multiple passes:
```wgsl
@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
}
```


### Hot Reloading
cuneus supports hot reloading of shaders. Simply modify your WGSL files and they will automatically reload.

### Export Support
Built-in support for exporting frames as images. Access through the UI when enabled. "Start time" is not working correctly currently.

### Texture Support
Load and use textures in your shaders:
```rust
if let Some(ref texture_manager) = self.base.texture_manager {
    render_pass.set_bind_group(0, &texture_manager.bind_group, &[]);
}
```

## Resolution Handling

cuneus handles both logical and physical resolution:

1. Initial window size is set in logical pixels:

```rust
   let (app, event_loop) = ShaderApp::new("My Shader", 800, 600);
 ```
2.  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:
```wgsl
let dimensions = vec2<f32>(textureDimensions(my_texture));
```

## Using Compute Shaders

Cuneus provides robust support for GPU compute shaders, enabling you to harness parallel processing power for various tasks such as image processing, simulations, and data manipulation.

### Quick Start

There are two main ways to use compute shaders in Cuneus:

1. **Simple integration with RenderKit** - Best for basic compute shaders please see: [compute.rs]https://github.com/altunenes/cuneus/blob/main/src/ bin/compute.rs
2. **Custom implementation** - For more complex scenarios like ping-pong buffers or multi-pass compute shaders, see: [computecolors.rs]https://github.com/altunenes/cuneus/blob/main/src/bin/computecolors.rs

#### Option 1: Using RenderKit (Simpler)

```rust
// In your ShaderManager implementation
fn init(core: &Core) -> Self {
    let mut base = RenderKit::new(
        core,
        include_str!("../../shaders/vertex.wgsl"),
        include_str!("../../shaders/blit.wgsl"),
        &[&texture_bind_group_layout],
        None,
    );

    // Create a compute shader with configuration
    let compute_config = cuneus::compute::ComputeShaderConfig {
        workgroup_size: [16, 16, 1],
        workgroup_count: None,  // Auto-determine from texture size
        dispatch_once: false,   // Run every frame
        storage_texture_format: cuneus::compute::COMPUTE_TEXTURE_FORMAT_RGBA16,
        enable_atomic_buffer: false,
        entry_points: vec!["main".to_string()],
        sampler_address_mode: wgpu::AddressMode::ClampToEdge,
        sampler_filter_mode: wgpu::FilterMode::Linear,
        label: "My Compute Shader".to_string(),
    };
    
    base.compute_shader = Some(cuneus::compute::ComputeShader::new_with_config(
        core,
        include_str!("../../shaders/my_compute.wgsl"),
        compute_config,
    ));
    
    Self { base }
}

// In your render function
fn render(&mut self, core: &Core) -> Result<(), wgpu::SurfaceError> {
    // ...
    let mut encoder = core.device.create_command_encoder(&wgpu::CommandEncoderDescriptor {
        label: Some("Render Encoder"),
    });
    
    // Run the compute shader
    self.base.dispatch_compute_shader(&mut encoder, core);
    
    // Render the compute shader output
    if let Some(compute_texture) = self.base.get_compute_output_texture() {
        render_pass.set_bind_group(0, &compute_texture.bind_group, &[]);
        // ...
    }
    // ...
}
```

#### WGSL Compute Shader Structure

```wgsl
// Time uniform
struct TimeUniform { 
    time: f32, 
    delta: f32,
    frame: u32,
    _padding: u32 
}
@group(0) @binding(0) var<uniform> time_data: TimeUniform;

// Optional parameters
struct Params {
    intensity: f32,
    scale: f32,
    // ...
}
@group(1) @binding(0) var<uniform> params: Params;

// Input/output textures
@group(2) @binding(0) var input_texture: texture_2d<f32>;
@group(2) @binding(1) var tex_sampler: sampler;
@group(2) @binding(2) var output: texture_storage_2d<rgba16float, write>;

@compute @workgroup_size(16, 16, 1)
fn main(@builtin(global_invocation_id) id: vec3<u32>) {
    // Get dimensions
    let dims = textureDimensions(output);
    
    // Bounds check
    if (id.x >= dims.x || id.y >= dims.y) {
        return;
    }
    
    // Process and write result
    textureStore(output, vec2<i32>(id.xy), vec4<f32>(1.0, 0.0, 0.0, 1.0));
}
```

### Advanced: Multi-Pass Compute Shaders
For more complex effects, use multiple entry points:

```rust
// Configure with multiple entry points
let config = ComputeShaderConfig {
    entry_points: vec![
        "preprocess".to_string(),
        "process".to_string(),
        "postprocess".to_string()
    ],
    enable_atomic_buffer: true,
    // ...
};
```

With corresponding WGSL:
```wgsl
@compute @workgroup_size(16, 16, 1)
fn preprocess(@builtin(global_invocation_id) id: vec3<u32>) {
    // First pass
}

@compute @workgroup_size(16, 16, 1)
fn process(@builtin(global_invocation_id) id: vec3<u32>) {
    // Second pass
}

@compute @workgroup_size(16, 16, 1)
fn postprocess(@builtin(global_invocation_id) id: vec3<u32>) {
    // Third pass
}
```

### Ping-Pong Buffers
For effects that need to read their previous output:

```rust
// Create two textures and swap between them
let source_bind_group = if self.source_is_a {
    &self.texture_pair.bind_group_a
} else {
    &self.texture_pair.bind_group_b
};

// After rendering, swap buffers
self.source_is_a = !self.source_is_a;
```


Utility Functions

```rust
// Create two textures and swap between them
let source_bind_group = if self.source_is_a {
    &self.texture_pair.bind_group_a
} else {
    &self.texture_pair.bind_group_b
};

// After rendering, swap buffers
self.source_is_a = !self.source_is_a;
```

For complete examples, see `src/bin/clifford_compute.rs` and `src/bin/computecolors.rs`.


### How to use Audio Data 

 On the Rust side, unlike other backends, make sure you only add this single line. 

https://github.com/altunenes/cuneus/blob/776434e2f5ac8797dd5a07ffe86659745a8e88a6/src/bin/audiovis.rs#L370 

I passed the data about spectrum and BPM to the resolution uniform. You can use them from here:

https://github.com/altunenes/cuneus/blob/776434e2f5ac8797dd5a07ffe86659745a8e88a6/shaders/audiovis.wgsl#L10-L15

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

## Font Rendering

Cuneus provides built-in font rendering for text overlays, scoring systems, and creative text effects in shaders.

### Basic Font Usage

Enable fonts in your compute shader configuration:

```rust
let compute_config = ComputeShaderConfig {
    enable_fonts: true,  // Enable font system
    // ... other config
};
```

### WGSL Font Functions

Use these functions in your compute shaders:

```wgsl
// Render single character at any size
let alpha = render_char_sized(pixel_pos, position, 'A', 64.0);

// Render numbers (integers)
let alpha = render_number(pixel_pos, position, 123u, 32.0);

// Render text strings (use word arrays)
let word: array<u32, 8> = array<u32, 8>(72u, 101u, 108u, 108u, 111u, 0u, 0u, 0u); // "Hello"
let alpha = render_word(pixel_pos, position, word, 5u, 48.0);

// Render floating point numbers
let alpha = render_float(pixel_pos, position, 3.14, 24.0);
```

### Font System Details

- **Font:** Courier Prime Bold
- **Atlas:** 1024×1024 texture with 64×64 character cells
- **Characters:** Full ASCII printable set (32-126)
- **Performance:** GPU-optimized, real-time rendering
- **Quality:** High-resolution with gamma correction

The font system is fully scalable and can be positioned anywhere in your shader effects.

### Adding Interactive Controls
1. Start with a template that includes GUI (e.g., `xmas.rs`)
2. Define your parameters in the ShaderParams struct
3. Add UI controls in the render function
4. Connect parameters to your shader