Trait ComputablePipeline

Source
pub trait ComputablePipeline<C: ComputeCommand>:
    Send
    + Sync
    + 'static {
    // Required method
    fn dispatch(
        &mut self,
        device: &Device,
        queue: &Queue,
        config: &SurfaceConfiguration,
        compute_pass: &mut ComputePass<'_>,
        command: &C,
        resource_manager: &mut ComputeResourceManager,
        input_view: &TextureView,
        output_view: &TextureView,
    );
}
Expand description

Core trait for implementing GPU compute pipelines.

This trait defines the interface for compute pipelines that process specific types of compute commands using GPU compute shaders. Each pipeline is responsible for setting up compute resources, managing shader dispatch, and processing texture data.

§Type Parameters

§Design Principles

  • Single Responsibility: Each pipeline handles one specific type of compute operation
  • Stateless Operation: Pipelines should not maintain state between dispatch calls
  • Resource Efficiency: Reuse GPU resources when possible through the resource manager
  • Thread Safety: All implementations must be Send + Sync for parallel execution

§Integration with Rendering

Compute pipelines operate within the broader rendering pipeline, typically:

  1. After Rendering: Process the rendered scene for post-effects
  2. Between Passes: Transform data between different rendering stages
  3. Before Rendering: Prepare data or textures for subsequent render operations

§Example Implementation Pattern

impl ComputablePipeline<MyCommand> for MyPipeline {
    fn dispatch(&mut self, device, queue, config, compute_pass, command,
                resource_manager, input_view, output_view) {
        // 1. Create or retrieve uniform buffer
        let uniforms = create_uniforms_from_command(command);
        let uniform_buffer = device.create_buffer_init(...);
         
        // 2. Create bind group with textures and uniforms
        let bind_group = device.create_bind_group(...);
         
        // 3. Set pipeline and dispatch
        compute_pass.set_pipeline(&self.compute_pipeline);
        compute_pass.set_bind_group(0, &bind_group, &[]);
        compute_pass.dispatch_workgroups(workgroup_x, workgroup_y, 1);
    }
}

Required Methods§

Source

fn dispatch( &mut self, device: &Device, queue: &Queue, config: &SurfaceConfiguration, compute_pass: &mut ComputePass<'_>, command: &C, resource_manager: &mut ComputeResourceManager, input_view: &TextureView, output_view: &TextureView, )

Dispatches the compute command within an active compute pass.

This method is called once for each compute command that needs to be processed. It should set up the necessary GPU resources, bind them to the compute pipeline, and dispatch the appropriate number of workgroups to process the input texture.

§Parameters
  • device - The WGPU device for creating GPU resources
  • queue - The WGPU queue for submitting commands and updating buffers
  • config - Current surface configuration containing dimensions and format info
  • compute_pass - The active compute pass to record commands into
  • command - The specific compute command containing operation parameters
  • resource_manager - Manager for reusing GPU buffers across operations
  • input_view - View of the input texture (result from previous pass)
  • output_view - View of the output texture (target for this operation)
§Texture Format Requirements

Due to WGPU limitations, storage textures have specific format requirements:

  • Input Texture: Can be any readable format, typically from render passes
  • Output Texture: Must use wgpu::TextureFormat::Rgba8Unorm format
  • sRGB Limitation: sRGB formats cannot be used as storage textures

The framework ensures that output_view always uses a compatible format for storage binding operations.

§Workgroup Dispatch Guidelines

When dispatching workgroups, consider:

  • Workgroup Size: Match your shader’s @workgroup_size declaration
  • Coverage: Ensure all pixels are processed by calculating appropriate dispatch dimensions
  • Alignment: Round up dispatch dimensions to cover the entire texture

Common dispatch pattern:

let workgroup_size = 8; // Match shader @workgroup_size(8, 8)
let dispatch_x = (config.width + workgroup_size - 1) / workgroup_size;
let dispatch_y = (config.height + workgroup_size - 1) / workgroup_size;
compute_pass.dispatch_workgroups(dispatch_x, dispatch_y, 1);
§Resource Management

Use the resource_manager to:

  • Store persistent buffers that can be reused across frames
  • Avoid recreating expensive GPU resources
  • Manage buffer lifetimes efficiently
§Error Handling

This method should handle errors gracefully:

  • Validate command parameters before use
  • Ensure texture dimensions are compatible
  • Handle resource creation failures appropriately

Implementors§