Module pipeline

Module pipeline 

Source
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

§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:

  1. Begin Pass: Setup phase for initializing pipeline-specific resources
  2. Draw: Main rendering phase where commands are processed
  3. End Pass: Cleanup phase for finalizing rendering operations

§Implementation Guide

§Creating a Custom Pipeline

To create a custom rendering pipeline:

  1. Define your draw command struct implementing DrawCommand
  2. Create a pipeline struct implementing DrawablePipeline<YourCommand>
  3. 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§

DrawContext
Provides comprehensive context for drawing operations within a render pass.
FrameContext
Provides context for operations that occur once per frame.
PassContext
Provides context for operations within a single render pass.
PipelineRegistry
Registry for managing and dispatching drawable pipelines.

Traits§

DrawablePipeline
Core trait for implementing custom graphics rendering pipelines.
ErasedDrawablePipeline
Internal trait for type erasure of drawable pipelines.