tessera_ui/renderer/drawer.rs
1//! Graphics rendering pipeline management.
2//!
3//! This module provides the drawing infrastructure for the unified command system,
4//! handling graphics pipeline registration and command dispatch.
5
6pub mod command;
7mod pipeline;
8
9use crate::{
10 PxPosition,
11 px::{PxRect, PxSize},
12};
13
14pub use command::DrawCommand;
15pub use pipeline::{DrawablePipeline, PipelineRegistry};
16
17/// Drawer manages graphics pipelines and processes draw commands.
18///
19/// The Drawer acts as the central coordinator for all graphics rendering operations,
20/// maintaining a registry of pipelines and dispatching draw commands to the appropriate
21/// pipeline implementations.
22pub struct Drawer {
23 /// Registry containing all registered graphics pipelines
24 pub pipeline_registry: PipelineRegistry,
25}
26
27impl Default for Drawer {
28 fn default() -> Self {
29 Self::new()
30 }
31}
32
33impl Drawer {
34 /// Create a new drawer
35 pub fn new() -> Self {
36 Self {
37 pipeline_registry: PipelineRegistry::new(),
38 }
39 }
40
41 /// Initialize all pipelines at the beginning of each render pass.
42 ///
43 /// This method calls the `begin_pass` method on all registered pipelines,
44 /// allowing them to set up per-pass state such as uniform buffers.
45 pub fn begin_pass(
46 &mut self,
47 gpu: &wgpu::Device,
48 queue: &wgpu::Queue,
49 config: &wgpu::SurfaceConfiguration,
50 render_pass: &mut wgpu::RenderPass<'_>,
51 scene_texture_view: &wgpu::TextureView,
52 ) {
53 self.pipeline_registry.begin_all_passes(
54 gpu,
55 queue,
56 config,
57 render_pass,
58 scene_texture_view,
59 );
60 }
61
62 /// Finalize all pipelines at the end of each render pass.
63 ///
64 /// This method calls the `end_pass` method on all registered pipelines,
65 /// allowing them to perform cleanup or final operations.
66 pub fn end_pass(
67 &mut self,
68 gpu: &wgpu::Device,
69 queue: &wgpu::Queue,
70 config: &wgpu::SurfaceConfiguration,
71 render_pass: &mut wgpu::RenderPass<'_>,
72 scene_texture_view: &wgpu::TextureView,
73 ) {
74 self.pipeline_registry
75 .end_all_passes(gpu, queue, config, render_pass, scene_texture_view);
76 }
77
78 /// Submit a draw command to the appropriate pipeline for rendering.
79 ///
80 /// This method dispatches the command to the correct pipeline based on its type,
81 /// providing all necessary context including GPU resources, render pass, and
82 /// positioning information.
83 ///
84 /// # Arguments
85 /// * `cmd` - The draw command to execute
86 /// * `size` - Size of the component being drawn
87 /// * `start_pos` - Position where drawing should begin
88 /// * `scene_texture_view` - Optional background texture for sampling
89 /// * `compute_texture_view` - Compute pipeline output texture
90 pub fn submit(
91 &mut self,
92 gpu: &wgpu::Device,
93 queue: &wgpu::Queue,
94 config: &wgpu::SurfaceConfiguration,
95 render_pass: &mut wgpu::RenderPass<'_>,
96 commands: &[(&dyn DrawCommand, PxSize, PxPosition)],
97 scene_texture_view: &wgpu::TextureView,
98 clip_rect: Option<PxRect>,
99 ) {
100 self.pipeline_registry.dispatch(
101 gpu,
102 queue,
103 config,
104 render_pass,
105 commands,
106 scene_texture_view,
107 clip_rect,
108 );
109 }
110}