Skip to main content

rcus_render/
quad.rs

1//! Vertex assembly for the two pipelines: solid quads and textured glyphs.
2//! Everything is built per frame into one buffer each, so the renderer issues a
3//! single draw per pipeline no matter how large the frame is.
4
5use bytemuck::{Pod, Zeroable};
6use rcus_layout::{LayoutNode, LayoutTree, Rect, Size};
7
8#[repr(C)]
9#[derive(Clone, Copy, Debug, Pod, Zeroable)]
10pub struct Vertex {
11    pub position: [f32; 2],
12    pub color: [f32; 4],
13}
14
15#[repr(C)]
16#[derive(Clone, Copy, Debug, Pod, Zeroable)]
17pub struct TextVertex {
18    /// Device-space position in pixels, y down from the top of the viewport.
19    pub position: [f32; 2],
20    /// Atlas coordinates normalized to atlas size.
21    pub uv: [f32; 2],
22    pub color: [f32; 4],
23    /// Atlas array layer holding this glyph's coverage.
24    pub layer: f32,
25}
26
27/// A run of geometry that must be clipped, in device pixels.
28#[derive(Clone, Copy, Debug)]
29pub struct Scissor {
30    pub x: f32,
31    pub y: f32,
32    pub width: f32,
33    pub height: f32,
34}
35
36/// One assembled frame: solid quads, glyph quads, and their clip rectangles.
37pub struct Frame {
38    /// Solid quads, parents before children so children paint on top.
39    pub quads: Vec<Vertex>,
40    /// Glyph quads with their atlas coordinates.
41    pub glyphs: Vec<TextVertex>,
42    /// Clip rectangles referenced by the per-node scissor indices.
43    pub scissors: Vec<Scissor>,
44    /// Scissor index per glyph, or `None` when the glyph is unclipped.
45    pub glyph_scissors: Vec<Option<u16>>,
46    /// Scissor index per quad vertex, or `None` when unclipped.
47    pub quad_scissors: Vec<Option<u16>>,
48}
49
50impl Frame {
51    pub fn new() -> Self {
52        Self {
53            quads: Vec::with_capacity(256),
54            glyphs: Vec::with_capacity(4096),
55            scissors: Vec::new(),
56            glyph_scissors: Vec::new(),
57            quad_scissors: Vec::new(),
58        }
59    }
60
61    pub fn is_empty(&self) -> bool {
62        self.quads.is_empty() && self.glyphs.is_empty()
63    }
64}
65
66impl Default for Frame {
67    fn default() -> Self {
68        Self::new()
69    }
70}
71
72/// Appends a solid rectangle in normalized device coordinates.
73pub fn push_rect(out: &mut Vec<Vertex>, viewport: Size, rect: Rect, color: [f32; 4]) {
74    if rect.width <= 0.0 || rect.height <= 0.0 || viewport.width <= 0.0 || viewport.height <= 0.0 {
75        return;
76    }
77    let x0 = rect.x / viewport.width * 2.0 - 1.0;
78    let x1 = rect.right() / viewport.width * 2.0 - 1.0;
79    let y0 = 1.0 - rect.y / viewport.height * 2.0;
80    let y1 = 1.0 - rect.bottom() / viewport.height * 2.0;
81    for (x, y) in [(x0, y0), (x1, y0), (x1, y1), (x0, y0), (x1, y1), (x0, y1)] {
82        out.push(Vertex {
83            position: [x, y],
84            color,
85        });
86    }
87}
88
89/// One glyph quad in device pixels, ready to be expanded into triangles.
90#[derive(Clone, Copy, Debug)]
91pub struct GlyphQuad {
92    pub left: f32,
93    pub top: f32,
94    pub width: f32,
95    pub height: f32,
96    /// Atlas rectangle: u, v, width, height normalized to atlas size.
97    pub uv: [f32; 4],
98    pub color: [f32; 4],
99    pub layer: f32,
100}
101
102/// Appends one glyph as two triangles, positions in device pixels.
103pub fn push_glyph(out: &mut Vec<TextVertex>, quad: GlyphQuad) {
104    let GlyphQuad {
105        left,
106        top,
107        width,
108        height,
109        uv,
110        color,
111        layer,
112    } = quad;
113    let (u0, v0) = (uv[0], uv[1]);
114    let (u1, v1) = (uv[0] + uv[2], uv[1] + uv[3]);
115    for (x, y, u, v) in [
116        (left, top, u0, v0),
117        (left + width, top, u1, v0),
118        (left + width, top + height, u1, v1),
119        (left, top, u0, v0),
120        (left + width, top + height, u1, v1),
121        (left, top + height, u0, v1),
122    ] {
123        out.push(TextVertex {
124            position: [x, y],
125            uv: [u, v],
126            color,
127            layer,
128        });
129    }
130}
131
132/// Builds solid quads for a laid-out tree, honoring nested clip rectangles.
133pub fn build_quads(tree: &LayoutTree) -> Frame {
134    let mut frame = Frame::new();
135    let viewport = tree.viewport;
136    push_rect(
137        &mut frame.quads,
138        viewport,
139        Rect::new(0.0, 0.0, viewport.width, viewport.height),
140        [0.02, 0.02, 0.03, 1.0],
141    );
142    // One scissor index per vertex, so a run can be sliced at any quad.
143    frame.quad_scissors.extend(std::iter::repeat_n(None, 6));
144    collect_quads(&tree.root, viewport, None, &mut frame);
145    frame
146}
147
148fn collect_quads(node: &LayoutNode, viewport: Size, inherited: Option<Rect>, frame: &mut Frame) {
149    let mut clip = inherited;
150    if node.clip {
151        let rect = match clip {
152            Some(parent) => intersect(parent, node.rect),
153            None => node.rect,
154        };
155        if rect.width <= 0.0 || rect.height <= 0.0 {
156            return;
157        }
158        clip = Some(rect);
159    }
160    let scissor = clip.filter(|rect| {
161        !(rect.x <= node.rect.x
162            && rect.y <= node.rect.y
163            && rect.right() >= node.rect.right()
164            && rect.bottom() >= node.rect.bottom())
165    });
166    let scissor_index = scissor.map(|rect| {
167        frame.scissors.push(Scissor {
168            x: rect.x,
169            y: rect.y,
170            width: rect.width,
171            height: rect.height,
172        });
173        (frame.scissors.len() - 1) as u16
174    });
175    if let Some(color) = node.background {
176        push_rect(
177            &mut frame.quads,
178            viewport,
179            node.rect,
180            [color.r, color.g, color.b, color.a],
181        );
182        frame
183            .quad_scissors
184            .extend(std::iter::repeat_n(scissor_index, 6));
185    }
186    for child in &node.children {
187        collect_quads(child, viewport, clip, frame);
188    }
189}
190
191pub fn intersect(a: Rect, b: Rect) -> Rect {
192    let x = a.x.max(b.x);
193    let y = a.y.max(b.y);
194    let right = a.right().min(b.right());
195    let bottom = a.bottom().min(b.bottom());
196    Rect::new(x, y, (right - x).max(0.0), (bottom - y).max(0.0))
197}