Expand description
Graphics rendering pipeline system for Tessera UI framework.
This module provides the core infrastructure for pluggable graphics rendering pipelines in Tessera. The design philosophy emphasizes flexibility and extensibility, allowing developers to create custom rendering effects without being constrained by built-in drawing primitives.
§Architecture Overview
The pipeline system uses a trait-based approach with type erasure to support dynamic dispatch of rendering commands. Each pipeline is responsible for rendering a specific type of draw command, such as shapes, text, images, or custom visual effects.
§Key Components
DrawablePipeline<T>: The main trait for implementing custom rendering pipelinesPipelineRegistry: Manages and dispatches commands to registered pipelinesErasedDrawablePipeline: Internal trait for type erasure and dynamic dispatch
§Design Philosophy
Unlike traditional UI frameworks that provide built-in “brush” or drawing primitives, Tessera treats shaders as first-class citizens. This approach offers several advantages:
- Modern GPU Utilization: Leverages WGPU and WGSL for efficient, cross-platform rendering
- Advanced Visual Effects: Enables complex effects like neumorphic design, lighting, shadows, reflections, and bloom that are difficult to achieve with traditional approaches
- Flexibility: Custom shaders allow for unlimited creative possibilities
- Performance: Direct GPU programming eliminates abstraction overhead
§Pipeline Lifecycle
Each pipeline follows a three-phase lifecycle during rendering:
- Begin Pass: Setup phase for initializing pipeline-specific resources
- Draw: Main rendering phase where commands are processed
- End Pass: Cleanup phase for finalizing rendering operations
§Implementation Guide
§Creating a Custom Pipeline
To create a custom rendering pipeline:
- Define your draw command struct implementing
DrawCommand - Create a pipeline struct implementing
DrawablePipeline<YourCommand> - Register the pipeline with
PipelineRegistry::register
§Example: Simple Rectangle Pipeline
use tessera_ui::{DrawCommand, DrawablePipeline, PxPosition, PxSize};
use wgpu;
// 1. Define the draw command
#[derive(Debug)]
struct RectangleCommand {
color: [f32; 4],
corner_radius: f32,
}
impl DrawCommand for RectangleCommand {
// Most commands don't need barriers
fn barrier(&self) -> Option<tessera_ui::BarrierRequirement> {
None
}
}
// 2. Implement the pipeline
struct RectanglePipeline {
render_pipeline: wgpu::RenderPipeline,
uniform_buffer: wgpu::Buffer,
bind_group: wgpu::BindGroup,
}
impl RectanglePipeline {
fn new(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration, sample_count: u32) -> Self {
// Create shader, pipeline, buffers, etc.
// ... implementation details ...
}
}
impl DrawablePipeline<RectangleCommand> for RectanglePipeline {
fn draw(
&mut self,
context: &mut DrawContext<RectangleCommand>,
) {
// Update uniforms with command data
// Set pipeline and draw
context.render_pass.set_pipeline(&self.render_pipeline);
context.render_pass.set_bind_group(0, &self.bind_group, &[]);
context.render_pass.draw(0..6, 0..1); // Draw quad
}
}
// 3. Register the pipeline
let mut registry = PipelineRegistry::new();
let rectangle_pipeline = RectanglePipeline::new(&device, &config, sample_count);
registry.register(rectangle_pipeline);§Integration with Basic Components
The tessera_basic_components crate demonstrates real-world pipeline implementations:
- ShapePipeline: Renders rounded rectangles, circles, and complex shapes with shadows and ripple effects
- TextPipeline: Handles text rendering with font management and glyph caching
- ImagePipeline: Displays images with various scaling and filtering options
- FluidGlassPipeline: Creates advanced glass effects with distortion and transparency
These pipelines are registered in tessera_ui_basic_components::pipelines::register_pipelines().
§Performance Considerations
- Batch Similar Commands: Group similar draw commands to minimize pipeline switches
- Resource Management: Reuse buffers and textures when possible
- Shader Optimization: Write efficient shaders optimized for your target platforms
- State Changes: Minimize render state changes within the draw method
§Advanced Features
§Barrier Requirements
Some rendering effects need to sample from previously rendered content (e.g., blur effects).
Implement DrawCommand::barrier() to return [BarrierRequirement::SampleBackground]
for such commands.
§Multi-Pass Rendering
Use begin_pass() and end_pass() for pipelines that require multiple rendering passes
or complex setup/teardown operations.
§Scene Texture Access
The scene_texture_view parameter provides access to the current scene texture,
enabling effects that sample from the background or perform post-processing.
Structs§
- Draw
Context - Provides comprehensive context for drawing operations within a render pass.
- Frame
Context - Provides context for operations that occur once per frame.
- Pass
Context - Provides context for operations within a single render pass.
- Pipeline
Registry - Registry for managing and dispatching drawable pipelines.
Traits§
- Drawable
Pipeline - Core trait for implementing custom graphics rendering pipelines.
- Erased
Drawable Pipeline - Internal trait for type erasure of drawable pipelines.