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 /// HDR headroom policy (CSS `dynamic-range-limit`): how the
128 /// backend remasters content brighter than the output can show.
129 range_limit: crate::image::DynamicRangeLimit,
130 },
131 /// An app-owned GPU texture composited into the paint stream.
132 /// Unlike `DrawOp::Image`, the backend does not upload pixels —
133 /// it samples the existing texture identified by `texture` during
134 /// paint, keying its bind-group cache on
135 /// [`crate::surface::AppTextureId`]. `rect` is the post-`fit`
136 /// destination rect; for `Cover` it can extend past the El's
137 /// content area and is clipped via `scissor`. `transform` is an
138 /// affine applied to the textured quad in destination space,
139 /// around the centre of `rect`. `alpha` selects the blend path.
140 /// SVG bundle output emits a placeholder rect labelled with the
141 /// texture's id.
142 AppTexture {
143 id: String,
144 rect: Rect,
145 scissor: Option<Rect>,
146 texture: crate::surface::AppTexture,
147 alpha: crate::surface::SurfaceAlpha,
148 fit: ImageFit,
149 transform: crate::affine::Affine2,
150 },
151 /// An app-supplied vector asset. `render_mode` decides whether the
152 /// backend preserves authored paint or treats the asset as a
153 /// one-colour mask. SVG bundle output emits a placeholder rect
154 /// labelled with the asset's hash.
155 Vector {
156 id: String,
157 rect: Rect,
158 scissor: Option<Rect>,
159 asset: std::sync::Arc<crate::vector::VectorAsset>,
160 render_mode: VectorRenderMode,
161 },
162 /// A backend-neutral 3D scene (closed-scope graph/model: point
163 /// scatter, small lit meshes, lines) composited into `rect`. Unlike
164 /// the other content ops, the backend does not tessellate or sample a
165 /// texture from core — it renders [`Scene3DData`] with its own scene
166 /// pipelines (points/mesh/lines), resolves MSAA, and composites the
167 /// result into `rect`. `scene` is `Arc`-shared and carries
168 /// revision-keyed geometry handles so backends cache GPU buffers
169 /// across frames. SVG bundle output emits a labelled placeholder rect
170 /// (3D cannot be represented in the SVG fallback). See
171 /// `docs/SCENE3D_PLAN.md`.
172 Scene3D {
173 id: String,
174 rect: Rect,
175 scissor: Option<Rect>,
176 scene: Arc<Scene3DData>,
177 },
178 /// Mid-frame snapshot of the current target into a sampled texture,
179 /// scheduled before any backdrop-sampling pass.
180 BackdropSnapshot,
181}
182
183#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
184pub enum TextAnchor {
185 Start,
186 Middle,
187 End,
188}
189
190impl DrawOp {
191 pub fn id(&self) -> &str {
192 match self {
193 DrawOp::Quad { id, .. }
194 | DrawOp::GlyphRun { id, .. }
195 | DrawOp::AttributedText { id, .. }
196 | DrawOp::Icon { id, .. }
197 | DrawOp::Image { id, .. }
198 | DrawOp::AppTexture { id, .. }
199 | DrawOp::Vector { id, .. }
200 | DrawOp::Scene3D { id, .. } => id,
201 DrawOp::BackdropSnapshot => "<backdrop-snapshot>",
202 }
203 }
204 pub fn shader(&self) -> Option<&ShaderHandle> {
205 match self {
206 DrawOp::Quad { shader, .. }
207 | DrawOp::GlyphRun { shader, .. }
208 | DrawOp::AttributedText { shader, .. } => Some(shader),
209 DrawOp::Icon { .. }
210 | DrawOp::Image { .. }
211 | DrawOp::AppTexture { .. }
212 | DrawOp::Vector { .. }
213 | DrawOp::Scene3D { .. } => None,
214 DrawOp::BackdropSnapshot => None,
215 }
216 }
217 pub fn scissor(&self) -> Option<Rect> {
218 match self {
219 DrawOp::Quad { scissor, .. }
220 | DrawOp::GlyphRun { scissor, .. }
221 | DrawOp::AttributedText { scissor, .. }
222 | DrawOp::Icon { scissor, .. }
223 | DrawOp::Image { scissor, .. }
224 | DrawOp::AppTexture { scissor, .. }
225 | DrawOp::Vector { scissor, .. }
226 | DrawOp::Scene3D { scissor, .. } => *scissor,
227 DrawOp::BackdropSnapshot => None,
228 }
229 }
230}