Skip to main content

damascene_core/paint/
ir.rs

1//! Backend-neutral draw-op IR.
2//!
3//! Every visual fact in the laid-out tree resolves to a [`DrawOp`] bound
4//! to a [`ShaderHandle`] and a uniform block. The wgpu renderer dispatches
5//! by shader handle; the SVG fallback (`crate::bundle::svg`) interprets stock
6//! shaders best-effort and emits placeholder rects for custom ones.
7//!
8//! `BackdropSnapshot` is emitted by [`crate::runtime::RunnerCore`] when
9//! the resolved paint stream first needs a backdrop-sampling shader. See
10//! `docs/SHADER_VISION.md` for the backend contract.
11//!
12//! # Why DrawOp over RenderCmd
13//!
14//! Damascene keeps visual material decisions in shader handles and uniform blocks
15//! instead of baking CSS-shaped fields into the IR. Rect colors, gradients,
16//! shadows, focus rings, and glass effects resolve to stock shader uniforms or
17//! custom shader bindings before a backend records GPU commands.
18
19use std::sync::Arc;
20
21use crate::icons::svg::IconSource;
22use crate::image::{Image, ImageFit};
23use crate::scene::Scene3DData;
24use crate::shader::{ShaderHandle, UniformBlock};
25use crate::text::atlas::RunStyle;
26use crate::text::metrics::TextLayout;
27use crate::tree::{Color, Corners, FontFamily, FontWeight, Rect, TextWrap};
28use crate::vector::VectorRenderMode;
29
30/// One paint operation in the laid-out frame.
31#[derive(Clone, Debug)]
32pub enum DrawOp {
33    /// A rectangular region painted by a shader (typically
34    /// `stock::rounded_rect`, but custom shaders also emit `Quad`).
35    Quad {
36        id: String,
37        rect: Rect,
38        scissor: Option<Rect>,
39        shader: ShaderHandle,
40        uniforms: UniformBlock,
41    },
42    /// A run of text. The draw op carries the author text and measured layout;
43    /// backends shape/rasterize through the shared glyph atlas path.
44    GlyphRun {
45        id: String,
46        rect: Rect,
47        scissor: Option<Rect>,
48        shader: ShaderHandle,
49        /// Carried explicitly on the op for SVG fallback and backend text
50        /// shaping.
51        color: Color,
52        text: String,
53        size: f32,
54        line_height: f32,
55        family: FontFamily,
56        /// Monospace face used when `mono` is set. Stamped from the
57        /// source El's `mono_font_family` (themed via
58        /// `Theme::mono_font_family`).
59        mono_family: FontFamily,
60        weight: FontWeight,
61        mono: bool,
62        wrap: TextWrap,
63        anchor: TextAnchor,
64        layout: TextLayout,
65        /// Underline / strikethrough state lifted from the source El's
66        /// `text_underline` / `text_strikethrough`. Backends fold them
67        /// into the synthesized [`RunStyle`] before shaping so the
68        /// decoration pass in [`crate::text::atlas`] runs uniformly
69        /// for standalone leaves and attributed paragraphs.
70        underline: bool,
71        strikethrough: bool,
72        /// Optional link URL from the El's `text_link`. Carried for
73        /// future hit-test work; today it just pins color + underline
74        /// via [`RunStyle::with_link`].
75        link: Option<String>,
76    },
77    /// An attributed paragraph: a sequence of styled runs that flow
78    /// together inside one `rect`. The runtime hands `runs` straight to
79    /// [`crate::text::atlas::GlyphAtlas::shape_and_rasterize_runs`] so
80    /// wrapping decisions cross run boundaries (real prose, not glued
81    /// segments). `layout` is an approximate pre-shaping measurement
82    /// from `text::metrics` — backends shape for accurate placement;
83    /// SVG uses it to lay tspan baselines.
84    AttributedText {
85        id: String,
86        rect: Rect,
87        scissor: Option<Rect>,
88        shader: ShaderHandle,
89        /// Source-order styled spans. Each `String` may contain
90        /// embedded `\n` to express in-paragraph hard breaks.
91        runs: Vec<(String, RunStyle)>,
92        size: f32,
93        line_height: f32,
94        wrap: TextWrap,
95        anchor: TextAnchor,
96        layout: TextLayout,
97    },
98    /// A vector icon scaled into `rect`. The `source` is either a
99    /// built-in [`crate::tree::IconName`] (24x24 lucide-style) or an
100    /// app-supplied [`crate::SvgIcon`]. SVG bundle output renders the
101    /// vector paths directly; wgpu/vulkano backends bake an MTSDF (or
102    /// tessellate for non-flat materials); backends without a native
103    /// vector painter fall back to a glyph for built-ins.
104    Icon {
105        id: String,
106        rect: Rect,
107        scissor: Option<Rect>,
108        source: IconSource,
109        color: Color,
110        size: f32,
111        stroke_width: f32,
112    },
113    /// A raster image painted into `rect`. The `image` carries the
114    /// pixel data (Arc-shared with the source El) and is keyed by
115    /// `image.content_hash()` in backend texture caches. `rect` is the
116    /// post-`fit` destination; for `Cover` it can extend past the El's
117    /// content area and is clipped via `scissor`. SVG bundle output
118    /// emits a placeholder rect labelled with the image's hash.
119    Image {
120        id: String,
121        rect: Rect,
122        scissor: Option<Rect>,
123        image: Image,
124        tint: Option<Color>,
125        radius: Corners,
126        fit: ImageFit,
127    },
128    /// An app-owned GPU texture composited into the paint stream.
129    /// Unlike `DrawOp::Image`, the backend does not upload pixels —
130    /// it samples the existing texture identified by `texture` during
131    /// paint, keying its bind-group cache on
132    /// [`crate::surface::AppTextureId`]. `rect` is the post-`fit`
133    /// destination rect; for `Cover` it can extend past the El's
134    /// content area and is clipped via `scissor`. `transform` is an
135    /// affine applied to the textured quad in destination space,
136    /// around the centre of `rect`. `alpha` selects the blend path.
137    /// SVG bundle output emits a placeholder rect labelled with the
138    /// texture's id.
139    AppTexture {
140        id: String,
141        rect: Rect,
142        scissor: Option<Rect>,
143        texture: crate::surface::AppTexture,
144        alpha: crate::surface::SurfaceAlpha,
145        fit: ImageFit,
146        transform: crate::affine::Affine2,
147    },
148    /// An app-supplied vector asset. `render_mode` decides whether the
149    /// backend preserves authored paint or treats the asset as a
150    /// one-colour mask. SVG bundle output emits a placeholder rect
151    /// labelled with the asset's hash.
152    Vector {
153        id: String,
154        rect: Rect,
155        scissor: Option<Rect>,
156        asset: std::sync::Arc<crate::vector::VectorAsset>,
157        render_mode: VectorRenderMode,
158    },
159    /// A backend-neutral 3D scene (closed-scope graph/model: point
160    /// scatter, small lit meshes, lines) composited into `rect`. Unlike
161    /// the other content ops, the backend does not tessellate or sample a
162    /// texture from core — it renders [`Scene3DData`] with its own scene
163    /// pipelines (points/mesh/lines), resolves MSAA, and composites the
164    /// result into `rect`. `scene` is `Arc`-shared and carries
165    /// revision-keyed geometry handles so backends cache GPU buffers
166    /// across frames. SVG bundle output emits a labelled placeholder rect
167    /// (3D cannot be represented in the SVG fallback). See
168    /// `docs/SCENE3D_PLAN.md`.
169    Scene3D {
170        id: String,
171        rect: Rect,
172        scissor: Option<Rect>,
173        scene: Arc<Scene3DData>,
174    },
175    /// Mid-frame snapshot of the current target into a sampled texture,
176    /// scheduled before any backdrop-sampling pass.
177    BackdropSnapshot,
178}
179
180#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
181pub enum TextAnchor {
182    Start,
183    Middle,
184    End,
185}
186
187impl DrawOp {
188    pub fn id(&self) -> &str {
189        match self {
190            DrawOp::Quad { id, .. }
191            | DrawOp::GlyphRun { id, .. }
192            | DrawOp::AttributedText { id, .. }
193            | DrawOp::Icon { id, .. }
194            | DrawOp::Image { id, .. }
195            | DrawOp::AppTexture { id, .. }
196            | DrawOp::Vector { id, .. }
197            | DrawOp::Scene3D { id, .. } => id,
198            DrawOp::BackdropSnapshot => "<backdrop-snapshot>",
199        }
200    }
201    pub fn shader(&self) -> Option<&ShaderHandle> {
202        match self {
203            DrawOp::Quad { shader, .. }
204            | DrawOp::GlyphRun { shader, .. }
205            | DrawOp::AttributedText { shader, .. } => Some(shader),
206            DrawOp::Icon { .. }
207            | DrawOp::Image { .. }
208            | DrawOp::AppTexture { .. }
209            | DrawOp::Vector { .. }
210            | DrawOp::Scene3D { .. } => None,
211            DrawOp::BackdropSnapshot => None,
212        }
213    }
214    pub fn scissor(&self) -> Option<Rect> {
215        match self {
216            DrawOp::Quad { scissor, .. }
217            | DrawOp::GlyphRun { scissor, .. }
218            | DrawOp::AttributedText { scissor, .. }
219            | DrawOp::Icon { scissor, .. }
220            | DrawOp::Image { scissor, .. }
221            | DrawOp::AppTexture { scissor, .. }
222            | DrawOp::Vector { scissor, .. }
223            | DrawOp::Scene3D { scissor, .. } => *scissor,
224            DrawOp::BackdropSnapshot => None,
225        }
226    }
227}