pub trait DrawablePipeline<T: DrawCommand> {
// Required method
fn draw(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
commands: &[(&T, PxSize, PxPosition)],
scene_texture_view: &TextureView,
clip_rect: Option<PxRect>,
);
// Provided methods
fn begin_frame(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
) { ... }
fn begin_pass(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
scene_texture_view: &TextureView,
) { ... }
fn end_pass(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
scene_texture_view: &TextureView,
) { ... }
fn end_frame(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
) { ... }
}
Expand description
Core trait for implementing custom graphics rendering pipelines.
This trait defines the interface for rendering pipelines that process specific types of draw commands. Each pipeline is responsible for setting up GPU resources, managing render state, and executing the actual drawing operations.
§Type Parameters
T
- The specificDrawCommand
type this pipeline can handle
§Lifecycle Methods
The pipeline system provides five lifecycle hooks, executed in the following order:
begin_frame()
: Called once at the start of a new frame, before any render passes.begin_pass()
: Called at the start of each render pass that involves this pipeline.draw()
: Called for each command of typeT
within a render pass.end_pass()
: Called at the end of each render pass that involved this pipeline.end_frame()
: Called once at the end of the frame, after all render passes are complete.
Typically, begin_pass
, draw
, and end_pass
are used for the core rendering logic within a pass,
while begin_frame
and end_frame
are used for setup and teardown that spans the entire frame.
§Implementation Notes
- Only the
draw()
method is required; others have default empty implementations. - Pipelines should be stateless between frames when possible
- Resource management should prefer reuse over recreation
- Consider batching multiple commands for better performance
§Example
See the module-level documentation for a complete implementation example.
Required Methods§
Sourcefn draw(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
commands: &[(&T, PxSize, PxPosition)],
scene_texture_view: &TextureView,
clip_rect: Option<PxRect>,
)
fn draw( &mut self, gpu: &Device, gpu_queue: &Queue, config: &SurfaceConfiguration, render_pass: &mut RenderPass<'_>, commands: &[(&T, PxSize, PxPosition)], scene_texture_view: &TextureView, clip_rect: Option<PxRect>, )
Renders a batch of draw commands.
This is the core method where the actual rendering happens. It’s called
once for a batch of draw commands of type T
that need to be rendered.
§Parameters
gpu
- The WGPU device for creating resources.gpu_queue
- The WGPU queue for submitting commands and updating buffers.config
- Current surface configuration containing format and size information.render_pass
- The active render pass to record draw commands into.commands
- A slice of tuples, each containing the command, its size, and its position.scene_texture_view
- View of the current scene texture for background sampling.clip_rect
- An optional rectangle to clip the drawing area.
§Implementation Guidelines
- Iterate over the
commands
slice to process each command. - Update buffers (e.g., instance buffers, storage buffers) with data from the command batch.
- Set the appropriate render pipeline.
- Bind necessary resources (textures, buffers, bind groups).
- Issue one or more draw calls (e.g., an instanced draw call) to render the entire batch.
- If
clip_rect
isSome
, userender_pass.set_scissor_rect()
to clip rendering. - Avoid expensive operations like buffer creation; prefer reusing and updating existing resources.
§Scene Texture Usage
The scene_texture_view
provides access to the current rendered scene,
enabling effects that sample from the background.
Provided Methods§
Sourcefn begin_frame(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
)
fn begin_frame( &mut self, gpu: &Device, gpu_queue: &Queue, config: &SurfaceConfiguration, )
Called once at the beginning of the frame, before any render passes.
This method is the first hook in the pipeline’s frame lifecycle. It’s invoked
after a new CommandEncoder
has been created but before any rendering occurs.
It’s ideal for per-frame setup that is not tied to a specific wgpu::RenderPass
.
Since this method is called outside a render pass, it cannot be used for drawing commands. However, it can be used for operations like:
- Updating frame-global uniform buffers (e.g., with time or resolution data)
using [
wgpu::Queue::write_buffer
]. - Preparing or resizing buffers that will be used throughout the frame.
- Performing CPU-side calculations needed for the frame.
§Parameters
gpu
- The WGPU device, for resource creation.gpu_queue
- The WGPU queue, for submitting buffer writes.config
- The current surface configuration.
§Default Implementation
The default implementation does nothing.
Sourcefn begin_pass(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
scene_texture_view: &TextureView,
)
fn begin_pass( &mut self, gpu: &Device, gpu_queue: &Queue, config: &SurfaceConfiguration, render_pass: &mut RenderPass<'_>, scene_texture_view: &TextureView, )
Called once at the beginning of the render pass.
Use this method to perform one-time setup operations that apply to all draw commands of this type in the current frame. This is ideal for:
- Setting up shared uniform buffers
- Binding global resources
- Configuring render state that persists across multiple draw calls
§Parameters
gpu
- The WGPU device for creating resourcesgpu_queue
- The WGPU queue for submitting commandsconfig
- Current surface configurationrender_pass
- The active render passscene_texture_view
- View of the current scene texture for background sampling
§Default Implementation
The default implementation does nothing, which is suitable for most pipelines.
Sourcefn end_pass(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
render_pass: &mut RenderPass<'_>,
scene_texture_view: &TextureView,
)
fn end_pass( &mut self, gpu: &Device, gpu_queue: &Queue, config: &SurfaceConfiguration, render_pass: &mut RenderPass<'_>, scene_texture_view: &TextureView, )
Called once at the end of the render pass.
Use this method to perform cleanup operations or finalize rendering for all draw commands of this type in the current frame. This is useful for:
- Cleaning up temporary resources
- Finalizing multi-pass rendering operations
- Submitting batched draw calls
§Parameters
gpu
- The WGPU device for creating resourcesgpu_queue
- The WGPU queue for submitting commandsconfig
- Current surface configurationrender_pass
- The active render passscene_texture_view
- View of the current scene texture for background sampling
§Default Implementation
The default implementation does nothing, which is suitable for most pipelines.
Sourcefn end_frame(
&mut self,
gpu: &Device,
gpu_queue: &Queue,
config: &SurfaceConfiguration,
)
fn end_frame( &mut self, gpu: &Device, gpu_queue: &Queue, config: &SurfaceConfiguration, )
Called once at the end of the frame, after all render passes are complete.
This method is the final hook in the pipeline’s frame lifecycle. It’s invoked
after all begin_pass
, draw
, and end_pass
calls for the frame have
completed, but before the frame’s command buffer is submitted to the GPU.
It’s suitable for frame-level cleanup or finalization tasks, such as:
- Reading data back from the GPU (though this can be slow and should be used sparingly).
- Cleaning up temporary resources created in
begin_frame
. - Preparing data for the next frame.
§Parameters
gpu
- The WGPU device.gpu_queue
- The WGPU queue.config
- The current surface configuration.
§Default Implementation
The default implementation does nothing.