tessera_ui/renderer/command.rs
1//! Unified command system for rendering and computation.
2//!
3//! This module defines the `Command` enum that unifies draw and compute operations
4//! into a single type, enabling seamless integration of graphics and compute pipelines
5//! in the rendering workflow.
6
7use crate::{BarrierRequirement, ComputeCommand, DrawCommand};
8
9/// Unified command enum that can represent either a draw or compute operation.
10///
11/// This enum enables the rendering system to process both graphics and compute
12/// commands in a unified pipeline, with proper barrier handling for multi-pass
13/// rendering scenarios.
14///
15/// # Examples
16///
17/// ```rust,ignore
18/// // Creating a draw command
19/// let draw_cmd = Command::Draw(Box::new(ShapeCommand::Rect { /* ... */ }));
20///
21/// // Creating a compute command
22/// let compute_cmd = Command::Compute(Box::new(BlurCommand { /* ... */ }));
23/// ```
24pub enum Command {
25 /// A graphics rendering command processed by draw pipelines
26 Draw(Box<dyn DrawCommand>),
27 /// A GPU computation command processed by compute pipelines
28 Compute(Box<dyn ComputeCommand>),
29}
30
31impl Command {
32 /// Returns the barrier requirement for this command.
33 ///
34 /// Commands that need to sample from previously rendered content
35 /// should return a barrier requirement to ensure proper synchronization.
36 pub fn barrier(&self) -> Option<BarrierRequirement> {
37 match self {
38 Command::Draw(command) => command.barrier(),
39 // Currently, compute can only be used for after effects,
40 // so we assume it must require a barrier to sample background.
41 Command::Compute(_) => Some(BarrierRequirement::SampleBackground),
42 }
43 }
44}
45
46/// Automatic conversion from boxed draw commands to unified commands
47impl From<Box<dyn DrawCommand>> for Command {
48 fn from(val: Box<dyn DrawCommand>) -> Self {
49 Command::Draw(val)
50 }
51}
52
53/// Automatic conversion from boxed compute commands to unified commands
54impl From<Box<dyn ComputeCommand>> for Command {
55 fn from(val: Box<dyn ComputeCommand>) -> Self {
56 Command::Compute(val)
57 }
58}