cotis-wgpu 0.1.0-alpha

Desktop wgpu renderer backend for Cotis
Documentation
//! Custom render command extension point for the wgpu backend.
//!
//! Implement [`WgpuDrawable`] on types not covered by [`crate::default_drawables`].
//! Custom drawables receive a [`WgpuDrawContext`] with access to the frame's geometry batch,
//! text state, scissor region, depth ordering, and DPI scale.
//!
//! # Examples
//!
//! ```rust,ignore
//! use cotis_wgpu::drawable::{WgpuDrawable, WgpuDrawContext};
//! use cotis_wgpu::pipeline::{UIColor, UIPosition, UICornerRadii};
//!
//! struct RedSquare;
//!
//! impl WgpuDrawable for RedSquare {
//!     fn draw(self: Box<Self>, ctx: &mut WgpuDrawContext<'_>) {
//!         ctx.geometry.filled_rectangle(
//!             UIPosition { x: 10.0, y: 10.0, z: *ctx.depth },
//!             UIPosition { x: 100.0, y: 100.0, z: *ctx.depth },
//!             UIColor { r: 1.0, g: 0.0, b: 0.0 },
//!             UICornerRadii {
//!                 top_left: 0.0,
//!                 top_right: 0.0,
//!                 bottom_left: 0.0,
//!                 bottom_right: 0.0,
//!             },
//!         );
//!         *ctx.depth -= 0.000_1;
//!     }
//! }
//! ```

use crate::pipeline::GeometryBatch;
use crate::pipeline::UIPosition;
use crate::text::TextState;

/// Per-frame drawing context passed to each [`WgpuDrawable`] command.
pub struct WgpuDrawContext<'a> {
    /// CPU-side geometry batch flushed to the UI render pipeline at end of frame.
    pub geometry: &'a mut GeometryBatch,
    /// Glyphon text state for queuing text during command execution.
    pub text: &'a mut TextState,
    /// Whether a clip region from [`cotis_defaults::render_commands::ClipStart`] is active.
    pub scissor_active: bool,
    /// Top-left corner of the active clip region in physical pixels.
    pub scissor_position: UIPosition,
    /// Width and height of the active clip region in physical pixels.
    pub scissor_bounds: UIPosition,
    /// Current draw depth; decremented by each command for painter's-order z-sorting.
    pub depth: &'a mut f32,
    /// Window scale factor from winit; multiply text font sizes by this value.
    pub dpi_scale: f32,
}

/// A render command that can be executed against the wgpu backend.
///
/// Commands are consumed as `Box<Self>` and executed during frame presentation.
/// See [`crate::default_drawables`] for built-in implementations of standard
/// `cotis-defaults` command types.
pub trait WgpuDrawable {
    /// Executes this command against the given draw context.
    ///
    /// Decrement `ctx.depth` after drawing if z-ordering relative to other commands matters.
    fn draw(self: Box<Self>, ctx: &mut WgpuDrawContext<'_>);
}