### Clone and build Cuneus Source: https://github.com/altunenes/cuneus/blob/main/BUILD.md Commands to clone the repository and execute the audiovis example. ```bash # Clone and build git clone https://github.com/altunenes/cuneus cd cuneus # Run a specific shader cargo run --release --example audiovis ``` -------------------------------- ### Run Cuneus on Windows Source: https://github.com/altunenes/cuneus/blob/main/INSTALL.md Execute the batch file to start the application on Windows systems. ```batch run_shader-name.bat ``` -------------------------------- ### Run Cuneus on macOS or Linux Source: https://github.com/altunenes/cuneus/blob/main/INSTALL.md Execute the shell script to start the application on Unix-based systems. ```bash ./run_shader-name.sh ``` -------------------------------- ### Load Media Assets from Code Source: https://github.com/altunenes/cuneus/blob/main/usage.md Demonstrates embedding assets via load_media and manually updating channel textures. ```rust // Single embedded texture — works with .with_input_texture() or .with_channels(1) let mut base = RenderKit::new(core); base.load_media(core, "assets/sky.hdr").ok(); // HDRI, image, or video // Multiple independent channels — each bound separately let mut cs = cuneus::compute_shader!(core, "shaders/my.wgsl", config); // config has .with_channels(2) let img = image::open("assets/albedo.png").unwrap().into_rgba8(); let tex = TextureManager::new(&core.device, &core.queue, &img, &base.texture_bind_group_layout); cs.update_channel_texture(0, &tex.view, &tex.sampler, &core.device, &core.queue); // channel0 // channel1 left empty (1x1 magenta fallback) or loaded the same way ``` -------------------------------- ### Configure and build a compute shader Source: https://github.com/altunenes/cuneus/blob/main/usage.md Use the ComputeShader builder to define resources and the compute_shader! macro to embed and enable hot-reloading for the shader source. ```rust let config = ComputeShader::builder() .with_label("My Awesome Shader") .with_custom_uniforms::() // Custom parameters .with_mouse() // Enable mouse input .with_channels(1) // Enable one external texture (e.g., video) .build(); // The compute_shader! macro embeds the shader source AND enables hot reload automatically. let compute_shader = cuneus::compute_shader!(core, "shaders/my_shader.wgsl", config); ``` -------------------------------- ### Implement Per-Sample GPU Audio Synthesis in WGSL Source: https://github.com/altunenes/cuneus/blob/main/usage.md Define a mainSound function to generate stereo PCM samples and use a compute shader thread to fill the audio buffer. ```wgsl fn mainSound(t: f32) -> vec2 { // Your synthesis here — harmonics, filters, delay lines, drums, anything let melody = sin(6.283 * 440.0 * t) * 0.3; return vec2(melody, melody); // stereo } // Thread (0,0) generates all samples for this frame if (g.x == 0u && g.y == 0u) { for (var i = 0u; i < params.samples_to_generate; i++) { let t = f32(params.sample_offset + i) / params.sample_rate; let stereo = mainSound(t); audio_buffer[i * 2u] = stereo.x; audio_buffer[i * 2u + 1u] = stereo.y; } } ``` -------------------------------- ### Implement Engine Traits for Input and Resizing Source: https://github.com/altunenes/cuneus/blob/main/usage.md Use these methods within your engine implementation to handle window events and resolution updates. ```rust fn resize(&mut self, core: &Core) { // default_resize updates resolution uniform + resizes compute shader self.base.default_resize(core, &mut self.compute_shader); } fn handle_input(&mut self, core: &Core, event: &WindowEvent) -> bool { // default_handle_input handles egui events + keyboard shortcuts (H to toggle UI, etc.) // For DroppedFile support, check the event after this call. // For mouse input, add: self.base.handle_mouse_input(core, event, false) self.base.default_handle_input(core, event) } } ``` -------------------------------- ### Configure Compute Pipeline with Builder Pattern Source: https://github.com/altunenes/cuneus/blob/main/README.md Define a multi-pass pipeline using a dependency graph and configure the compute shader with custom uniforms and input handling. ```rust // Define your multi-pass pipeline as a dependency graph: let passes = vec![ PassDescription::new("compute_field", &[]), // no inputs PassDescription::new("render", &["compute_field"]), // reads compute_field PassDescription::new("feedback", &["render", "feedback"]),// reads render + own previous frame PassDescription::new("main_image", &["feedback"]), ]; let config = ComputeShader::builder() .with_multi_pass(&passes) // the render graph above .with_custom_uniforms::() // UI-controllable parameters .with_mouse() // mouse input .build(); ``` -------------------------------- ### Manage Audio PCM Stream in Rust Source: https://github.com/altunenes/cuneus/blob/main/usage.md Read back generated PCM samples from the GPU buffer and push them to the audio output stream. ```rust // In Rust: read back PCM and push to audio output let mut pcm = PcmStreamManager::new(Some(44100))?; pcm.start()?; // In update() each frame: let prev = self.last_samples_generated; if prev > 0 { if let Ok(data) = pollster::block_on(compute.read_audio_buffer(&device, &queue)) { pcm.push_samples(&data[..(prev * 2) as usize])?; } } // Time-sync: generate exactly the samples real-time demands let needed = ((elapsed * 44100.0) as u64 - pcm.samples_written()).min(1024) as u32; params.sample_offset = pcm.samples_written() as u32; params.samples_to_generate = needed; ``` -------------------------------- ### Configure Per-Buffer Resolution Source: https://github.com/altunenes/cuneus/blob/main/usage.md Sets custom resolutions for individual buffers, allowing for techniques like half-res blurs or fixed-size lookup tables. ```rust let passes = vec![ PassDescription::new("scene", &[]), // Full screen resolution PassDescription::new("blur", &["scene"]) .with_resolution_scale(0.5), // Half-res (recomputed on resize) PassDescription::new("lut", &["scene"]) .with_resolution(256, 1), // Fixed 256x1 lookup table PassDescription::new("main_image", &["scene", "blur", "lut"]), // Reads from all three ]; ``` -------------------------------- ### Standard WGSL Bind Group Layout Source: https://github.com/altunenes/cuneus/blob/main/usage.md Follow this binding structure to ensure predictable resource access. Note that Group 2 bindings are dynamic based on builder configuration. ```wgsl // Group 0: Per-Frame Data (Engine-Managed) struct TimeUniform { time: f32, delta: f32, frame: u32, _padding: u32 }; @group(0) @binding(0) var time_data: TimeUniform; // Group 1: Primary Pass I/O & Custom Parameters @group(1) @binding(0) var output: texture_storage_2d; // Optional: Your custom uniform struct @group(1) @binding(1) var params: MyParams; // Optional: Input texture for image processing @group(1) @binding(2) var input_texture: texture_2d; @group(1) @binding(3) var input_sampler: sampler; // Group 2: Global Engine Resources // IMPORTANT: Binding numbers are DYNAMIC based on what you enable in the builder. // Resources are added in this order: mouse → fonts → audio → audio_spectrum → atomics → channels // Example 1: Only .with_audio_spectrum() → audio_spectrum is @binding(0) // Example 2: .with_audio_spectrum() + .with_atomic_buffer() → audio_spectrum @binding(0), atomic_buffer @binding(1) // Example 3: .with_mouse() + .with_fonts() + .with_audio() → mouse @binding(0), fonts @binding(1-2), audio @binding(3) // Mouse (if .with_mouse() is used) - takes 1 binding @group(2) @binding(N) var mouse: MouseUniform; // Fonts (if .with_fonts() is used) - takes 2 bindings (uses textureLoad, no sampler needed) @group(2) @binding(N) var font_uniform: FontUniforms; @group(2) @binding(N+1) var font_texture: texture_2d; // Audio buffer (if .with_audio() is used) - takes 1 binding @group(2) @binding(N) var audio_buffer: array; // Audio spectrum (if .with_audio_spectrum() is used) - takes 1 binding @group(2) @binding(N) var audio_spectrum: array; // Atomic buffer (if .with_atomic_buffer() is used) - takes 1 binding @group(2) @binding(N) var atomic_buffer: array>; // Media channels (if .with_channels(2) is used) - takes 2 bindings per channel @group(2) @binding(N) var channel0: texture_2d; @group(2) @binding(N+1) var channel0_sampler: sampler; // Group 3: User Data & Multi-Pass I/O // User-defined storage buffers (if .with_storage_buffer() is used, this takes priority) @group(3) @binding(0) var my_data: array; // OR: Multi-pass input textures (if .with_multi_pass() is used without storage buffers) @group(3) @binding(0) var input_texture0: texture_2d; @group(3) @binding(1) var input_sampler0: sampler; ``` -------------------------------- ### Implement Iterative Solvers Source: https://github.com/altunenes/cuneus/blob/main/usage.md Repeats pass names to perform iterative algorithms like Jacobi pressure, leveraging automatic ping-pong buffering. ```rust let passes = vec![ PassDescription::new("compute_field", &["compute_field"]), PassDescription::new("pressure", &["compute_field", "pressure"]), // iteration 1 PassDescription::new("pressure", &["compute_field", "pressure"]), // iteration 2 PassDescription::new("pressure", &["compute_field", "pressure"]), // ...repeat N times PassDescription::new("project", &["compute_field", "pressure"]), PassDescription::new("main_image", &["project"]), ]; ``` -------------------------------- ### Define Input Texture in WGSL and Rust Source: https://github.com/altunenes/cuneus/blob/main/usage.md Configures a single input texture and sampler in Group 1 for shader access. ```wgsl @group(1) @binding(2) var input_texture: texture_2d; @group(1) @binding(3) var input_sampler: sampler; ``` ```rust compute_shader.update_input_texture(&tm.view, &tm.sampler, &core.device); ``` -------------------------------- ### Define Channel Textures in WGSL and Rust Source: https://github.com/altunenes/cuneus/blob/main/usage.md Configures multiple texture/sampler pairs in Group 2, accessible across all shader passes. ```wgsl @group(2) @binding(0) var channel0: texture_2d; @group(2) @binding(1) var channel0_sampler: sampler; @group(2) @binding(2) var channel1: texture_2d; @group(2) @binding(3) var channel1_sampler: sampler; ``` ```rust compute_shader.update_channel_texture(0, &tm.view, &tm.sampler, &core.device, &core.queue); compute_shader.update_channel_texture(1, &tm2.view, &tm2.sampler, &core.device, &core.queue); ``` -------------------------------- ### Configure Workgroup Sizes Source: https://github.com/altunenes/cuneus/blob/main/usage.md Define custom workgroup sizes per pass using PassDescription. This overrides the default or WGSL-defined size. ```rust // See cnn.rs for a practical example let passes = vec![ PassDescription::new("conv_layer1", &["canvas_update"]) .with_workgroup_size([12, 12, 8]), // Custom size for this pass PassDescription::new("main_image", &["fully_connected"]), // Uses default or WGSL size ]; ``` -------------------------------- ### Define Multi-Pass Texture Dependencies Source: https://github.com/altunenes/cuneus/blob/main/usage.md Maps pass outputs to input textures using the dependency array in PassDescription. ```rust // Each pass reads from any previous pass — order doesn't matter PassDescription::new("structure_tensor", &[]), PassDescription::new("tensor_field", &["structure_tensor"]), // input_texture0 = structure_tensor PassDescription::new("kuwahara", &["tensor_field"]), // input_texture0 = tensor_field // Reading from non-adjacent passes is fine: PassDescription::new("lic_edges", &["tensor_field", "kuwahara"]), // input_texture0 = tensor_field, 1 = kuwahara PassDescription::new("main_image", &["lic_edges"]), ``` -------------------------------- ### Execute Mid-Frame Buffer Updates Source: https://github.com/altunenes/cuneus/blob/main/usage.md Force buffer updates to take effect before the next dispatch by using core.flush_encoder(). ```rust // Update params, submit, get new encoder self.params.ping = 1 - self.params.ping; self.compute_shader.set_custom_params(self.params, &core.queue); frame.encoder = core.flush_encoder(frame.encoder); // Now the next dispatch sees the updated ping value self.compute_shader.dispatch_stage(&mut frame.encoder, core, NEXT_PASS); ``` -------------------------------- ### Implement ShaderManager for Custom Shaders Source: https://github.com/altunenes/cuneus/blob/main/usage.md Defines the structure for a custom shader application, including uniform parameters and the ShaderManager trait implementation. ```rust use cuneus::prelude::*; use cuneus::compute::*; // 1. Define custom parameters for the UI using the uniform_params! macro // This adds #[repr(C)], derives, UniformProvider impl, and a compile-time // assert that the struct size is a multiple of 16 bytes (catches padding errors). cuneus::uniform_params! { struct MyParams { strength: f32, color: [f32; 3], } } // 2. Define the main application struct struct MyShader { base: RenderKit, compute_shader: ComputeShader, current_params: MyParams, } // 3. Implement the ShaderManager trait impl ShaderManager for MyShader { fn init(core: &Core) -> Self { // RenderKit handles the final blit to screen and UI (vertex/blit shaders built-in) let base = RenderKit::new(core); let initial_params = MyParams { /* ... */ }; // --- To convert this to a Multi-Pass shader, make the following changes: --- // 1. (Multi-Pass) Define your passes and their dependencies. // The string in `new()` is the WGSL entry point name. // The slice `&[]` lists buffers to bind as input_texture0, input_texture1, etc. // Self-reference (e.g., "feedback" in its own inputs) enables cross-frame feedback. /* let passes = vec![ PassDescription::new("compute_field", &[]), // No inputs PassDescription::new("feedback", &["compute_field"]), // input_texture0 = compute_field PassDescription::new("main_image", &["feedback"]), ]; // For cross-frame feedback (temporal effects), add self to inputs: // PassDescription::new("feedback", &["compute_field", "feedback"]) // Then input_texture1 = feedback's PREVIOUS frame output (automatic) */ // Configure the compute shader using the builder let config = ComputeShader::builder() // For Single-Pass, use .with_entry_point(): .with_entry_point("main") // 2. (Multi-Pass) Comment out .with_entry_point() and use .with_multi_pass() instead: (we define the passes above) // .with_multi_pass(&passes) .with_custom_uniforms::() .with_mouse() .with_label("My Shader") .build(); // Create the compute shader with automatic hot reload. let compute_shader = cuneus::compute_shader!(core, "shaders/my_shader.wgsl", config); // Set initial parameters compute_shader.set_custom_params(initial_params, &core.queue); Self { base, compute_shader, current_params: initial_params } } fn update(&mut self, _core: &Core) { // Hot-reload is checked automatically by dispatch()/dispatch_stage(). // FPS tracking is updated automatically by end_frame(). } fn render(&mut self, core: &Core) -> Result<(), wgpu::SurfaceError> { // begin_frame() bundles surface texture + view + encoder into a FrameContext let mut frame = self.base.begin_frame(core)?; // get_ui_request() returns a ControlsRequest with time, window size, and FPS auto-populated let mut controls_request = self.base.controls .get_ui_request(&self.base.start_time, &core.size, self.base.fps_tracker.fps()); // Build the UI (apply_default_style sets the standard theme) let full_output = if self.base.key_handler.show_ui { self.base.render_ui(core, |ctx| { RenderKit::apply_default_style(ctx); // ... egui windows here ... }) } else { self.base.render_ui(core, |_ctx| {}) }; // Apply UI control requests after the UI closure: // - Non-media examples: use apply_control_request (handles time reset + param updates) // self.base.apply_control_request(controls_request.clone()); // - Media examples (video/webcam/hdri): use apply_media_requests (bundles // apply_control_request + handle_video/webcam/hdri_requests in one call) // self.base.apply_media_requests(core, &controls_request); // Execute the entire compute pipeline. // This works for both single-pass and multi-pass shaders automatically. self.compute_shader.dispatch(&mut frame.encoder, core); // Blit the compute shader's output texture to the screen self.base.renderer.render_to_view(&mut frame.encoder, &frame.view, &self.compute_shader); // end_frame() handles UI overlay + submit + present in one call self.base.end_frame(core, frame, full_output); // Cross-frame feedback (self-referencing passes) works automatically ``` -------------------------------- ### Perform Manual Dispatching Source: https://github.com/altunenes/cuneus/blob/main/usage.md Use dispatch_stage to control frame accumulation manually, preventing automatic frame counter advancement. ```rust // See mandelbulb.rs for a practical example fn render(&mut self, core: &Core) -> Result<(), wgpu::SurfaceError> { // ... // Set frame uniform manually for accumulation self.compute_shader.time_uniform.data.frame = self.frame_count; self.compute_shader.time_uniform.update(&core.queue); // Dispatch the single stage of the path tracer self.compute_shader.dispatch_stage(&mut encoder, core, 0); // Only increment the logical frame count when accumulation is active if self.current_params.accumulate > 0 { self.frame_count += 1; } // ... } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.