strata-render 0.1.1

High-performance Display List, Stacking Context Compositor, and GPU rasterization pipeline
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
//! 2D overlay pass drawn over depth-tested 3D geometry.
//!
//! The Earth reference draws graticules, contours, trajectories, waypoints,
//! reticles, and labels as 2D primitives after the 3D pass. They share the 3D
//! camera projection, so a projected anchor lands on the same pixel as the mesh
//! vertex it annotates, and everything is clipped to the viewport.
//!
//! The pass is split in two on purpose:
//!
//! 1. Projection: scene-space anchors are projected with the f64 frame and
//!    reduced to screen-space commands. This step is deterministic and pure, so
//!    tests can assert anchor accuracy without reading pixels.
//! 2. Rasterization: the commands are drawn into a [`FrameBuffer`].

use crate::camera64::{DVec3, RenderFrame64};
use crate::font::get_glyph_or_fallback;
use crate::rasterizer::FrameBuffer;
use strata_layout::Rect;
use strata_style::types::Color;

/// A marker glyph drawn at a projected anchor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MarkerKind {
    /// Filled square, the default waypoint glyph.
    Square,
    /// Filled circle.
    Circle,
    /// Diamond.
    Diamond,
    /// Hollow square: selected or focused entity.
    Ring,
}

/// Placement of a label relative to its projected anchor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LabelAnchor {
    /// Box centred on the anchor.
    Centre,
    /// Box starts at the anchor.
    Left,
    /// Box ends at the anchor.
    Right,
    /// Box sits above the anchor with a short gap.
    Above,
}

/// Text styling for an overlay label.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OverlayTextStyle {
    pub color: Color,
    /// Integer scale of the 8x16 glyph cell.
    pub scale: u32,
}

impl Default for OverlayTextStyle {
    fn default() -> Self {
        Self {
            color: Color::rgba(241, 245, 249, 255),
            scale: 1,
        }
    }
}

/// A scene-space overlay primitive.
#[derive(Debug, Clone, PartialEq)]
pub enum OverlayPrimitive {
    /// Connected line segments through scene points.
    Polyline {
        points: Vec<DVec3>,
        color: Color,
        width: f32,
    },
    /// Circle of constant world radius around a scene-space centre.
    Circle {
        centre: DVec3,
        radius: f64,
        color: Color,
        width: f32,
    },
    /// Filled marker at a scene anchor.
    Marker {
        anchor: DVec3,
        kind: MarkerKind,
        color: Color,
        size: f32,
    },
    /// Text label at a scene anchor, optionally with a leader line back to the
    /// anchor.
    Label {
        anchor: DVec3,
        text: String,
        style: OverlayTextStyle,
        placement: LabelAnchor,
        leader: bool,
    },
    /// Screen-space line, for reticles and crosshairs.
    Line {
        a: [f32; 2],
        b: [f32; 2],
        color: Color,
        width: f32,
    },
}

/// A projected, screen-space overlay command.
#[derive(Debug, Clone, PartialEq)]
pub enum OverlayCommand {
    Polyline {
        points: Vec<[f32; 2]>,
        color: Color,
        width: f32,
    },
    Circle {
        centre: [f32; 2],
        radius: f32,
        color: Color,
        width: f32,
    },
    Marker {
        centre: [f32; 2],
        kind: MarkerKind,
        color: Color,
        size: f32,
    },
    Label {
        text: String,
        /// Top-left of the text box in screen space.
        origin: [f32; 2],
        style: OverlayTextStyle,
        /// Leader from the label box edge to the anchor, when requested.
        leader: Option<([f32; 2], [f32; 2])>,
    },
    Line {
        a: [f32; 2],
        b: [f32; 2],
        color: Color,
        width: f32,
    },
}

/// Overlay primitives plus an optional screen-space clip rectangle.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct Overlay3D {
    pub primitives: Vec<OverlayPrimitive>,
    /// Clip in screen space. `None` clips to the draw viewport.
    pub clip: Option<Rect>,
}

impl Overlay3D {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn push(&mut self, primitive: OverlayPrimitive) -> &mut Self {
        self.primitives.push(primitive);
        self
    }

    pub fn is_empty(&self) -> bool {
        self.primitives.is_empty()
    }

    /// Projects every primitive into screen space with the shared 3D frame.
    ///
    /// Pieces behind the near plane are dropped, matching the geometry pass.
    pub fn project(&self, frame: &RenderFrame64) -> Vec<OverlayCommand> {
        let mut out = Vec::with_capacity(self.primitives.len());
        for primitive in &self.primitives {
            match primitive {
                OverlayPrimitive::Polyline {
                    points,
                    color,
                    width,
                } => {
                    let mut projected = Vec::with_capacity(points.len());
                    let mut visible = true;
                    for point in points {
                        let p = frame.project(*point);
                        if p.clipped {
                            visible = false;
                            break;
                        }
                        projected.push([p.x as f32, p.y as f32]);
                    }
                    if visible && projected.len() >= 2 {
                        out.push(OverlayCommand::Polyline {
                            points: projected,
                            color: *color,
                            width: *width,
                        });
                    }
                }
                OverlayPrimitive::Circle {
                    centre,
                    radius,
                    color,
                    width,
                } => {
                    let p = frame.project(*centre);
                    if p.clipped {
                        continue;
                    }
                    // Radius is projected through a second point one radius
                    // away along a screen-parallel axis so the circle shrinks
                    // with distance exactly like the geometry does.
                    let edge = frame.project(centre.add(DVec3::new(*radius, 0.0, 0.0)));
                    let screen_radius = if edge.clipped {
                        0.0
                    } else {
                        ((edge.x - p.x).hypot(edge.y - p.y)) as f32
                    };
                    out.push(OverlayCommand::Circle {
                        centre: [p.x as f32, p.y as f32],
                        radius: screen_radius,
                        color: *color,
                        width: *width,
                    });
                }
                OverlayPrimitive::Marker {
                    anchor,
                    kind,
                    color,
                    size,
                } => {
                    let p = frame.project(*anchor);
                    if p.clipped {
                        continue;
                    }
                    out.push(OverlayCommand::Marker {
                        centre: [p.x as f32, p.y as f32],
                        kind: *kind,
                        color: *color,
                        size: *size,
                    });
                }
                OverlayPrimitive::Label {
                    anchor,
                    text,
                    style,
                    placement,
                    leader,
                } => {
                    let p = frame.project(*anchor);
                    if p.clipped {
                        continue;
                    }
                    let (w, h) = text_size(text, style.scale);
                    let ax = p.x as f32;
                    let ay = p.y as f32;
                    let (ox, oy) = match placement {
                        LabelAnchor::Centre => (ax - w * 0.5, ay - h * 0.5),
                        LabelAnchor::Left => (ax, ay - h * 0.5),
                        LabelAnchor::Right => (ax - w, ay - h * 0.5),
                        LabelAnchor::Above => (ax - w * 0.5, ay - h - 6.0),
                    };
                    let leader_info = if *leader {
                        Some(([ax, ay], [ax, oy + h]))
                    } else {
                        None
                    };
                    out.push(OverlayCommand::Label {
                        text: text.clone(),
                        origin: [ox, oy],
                        style: *style,
                        leader: leader_info,
                    });
                }
                OverlayPrimitive::Line { a, b, color, width } => out.push(OverlayCommand::Line {
                    a: *a,
                    b: *b,
                    color: *color,
                    width: *width,
                }),
            }
        }
        out
    }

    /// Projects and rasterizes this overlay over already-rendered geometry,
    /// including text labels.
    pub fn render(&self, frame_buffer: &mut FrameBuffer, viewport: Rect, frame: &RenderFrame64) {
        self.render_with(frame_buffer, viewport, frame, draw_text);
    }

    /// Projects and rasterizes with an explicit text rasterizer.
    pub fn render_with(
        &self,
        frame_buffer: &mut FrameBuffer,
        viewport: Rect,
        frame: &RenderFrame64,
        text: TextRasterizer,
    ) {
        let commands = self.project(frame);
        rasterize_overlay_with_text(frame_buffer, viewport, self.clip, &commands, text);
    }

    /// Projects and rasterizes everything except text, for callers that have no
    /// text backend installed.
    pub fn render_geometry_only(
        &self,
        frame_buffer: &mut FrameBuffer,
        viewport: Rect,
        frame: &RenderFrame64,
    ) {
        let commands = self.project(frame);
        rasterize_overlay(frame_buffer, viewport, self.clip, &commands);
    }
}

/// Screen size of a text run in the 8x16 glyph cell.
pub fn text_size(text: &str, scale: u32) -> (f32, f32) {
    let scale = scale.max(1) as f32;
    let chars = text.chars().count() as f32;
    (chars * 8.0 * scale, 16.0 * scale)
}

/// Signature of a text rasterizer installed by the platform layer.
pub type TextRasterizer = fn(&mut FrameBuffer, Rect, [f32; 2], &str, OverlayTextStyle);

/// Rasterizes projected overlay commands into `frame_buffer`, clipped to
/// `clip` when present and to `viewport` always. Labels are drawn with the
/// built-in glyph rasterizer.
pub fn rasterize_overlay(
    frame_buffer: &mut FrameBuffer,
    viewport: Rect,
    clip: Option<Rect>,
    commands: &[OverlayCommand],
) {
    rasterize_overlay_with_text(frame_buffer, viewport, clip, commands, draw_text);
}

/// Rasterizes projected overlay commands with an explicit text rasterizer.
pub fn rasterize_overlay_with_text(
    frame_buffer: &mut FrameBuffer,
    viewport: Rect,
    clip: Option<Rect>,
    commands: &[OverlayCommand],
    text: TextRasterizer,
) {
    let Some(region) = viewport.intersect(clip.unwrap_or(viewport)) else {
        return;
    };
    if region.width <= 0.0 || region.height <= 0.0 {
        return;
    }

    for command in commands {
        match command {
            OverlayCommand::Polyline {
                points,
                color,
                width,
            } => {
                for pair in points.windows(2) {
                    draw_line(frame_buffer, region, pair[0], pair[1], *color, *width);
                }
            }
            OverlayCommand::Circle {
                centre,
                radius,
                color,
                width,
            } => draw_circle(frame_buffer, region, *centre, *radius, *color, *width),
            OverlayCommand::Marker {
                centre,
                kind,
                color,
                size,
            } => draw_marker(frame_buffer, region, *centre, *kind, *color, *size),
            OverlayCommand::Label {
                text: text_run,
                origin,
                style,
                leader,
            } => {
                if let Some((from, to)) = leader {
                    draw_line(frame_buffer, region, *from, *to, style.color, 1.0);
                }
                text(frame_buffer, region, *origin, text_run, *style);
            }
            OverlayCommand::Line { a, b, color, width } => {
                draw_line(frame_buffer, region, *a, *b, *color, *width)
            }
        }
    }
}

#[inline]
fn plot(frame_buffer: &mut FrameBuffer, region: Rect, x: i32, y: i32, color: Color) {
    let fx = x as f32 + 0.5;
    let fy = y as f32 + 0.5;
    if fx < region.x || fy < region.y || fx >= region.max_x() || fy >= region.max_y() {
        return;
    }
    if x < 0 || y < 0 {
        return;
    }
    frame_buffer.set_pixel(x as usize, y as usize, color);
}

/// Clips a segment to a rectangle with Liang-Barsky.
///
/// Projected globe-scale polylines can have endpoints millions of pixels
/// outside the frame. Clipping first keeps the Bresenham loop proportional to
/// the visible part instead of the full off-screen span.
fn clip_segment_to_rect(a: [f32; 2], b: [f32; 2], region: Rect) -> Option<([f32; 2], [f32; 2])> {
    let dx = b[0] - a[0];
    let dy = b[1] - a[1];
    let p = [-dx, dx, -dy, dy];
    let q = [
        a[0] - region.x,
        region.max_x() - a[0],
        a[1] - region.y,
        region.max_y() - a[1],
    ];
    let mut t0 = 0.0f32;
    let mut t1 = 1.0f32;
    for index in 0..4 {
        if p[index].abs() < 1e-9 {
            if q[index] < 0.0 {
                return None;
            }
            continue;
        }
        let t = q[index] / p[index];
        if p[index] < 0.0 {
            t0 = t0.max(t);
        } else {
            t1 = t1.min(t);
        }
        if t0 > t1 {
            return None;
        }
    }
    Some((
        [a[0] + dx * t0, a[1] + dy * t0],
        [a[0] + dx * t1, a[1] + dy * t1],
    ))
}

/// Bresenham line with a square brush so widths above one pixel are visible.
fn draw_line(
    frame_buffer: &mut FrameBuffer,
    region: Rect,
    a: [f32; 2],
    b: [f32; 2],
    color: Color,
    width: f32,
) {
    let Some((a, b)) = clip_segment_to_rect(a, b, region) else {
        return;
    };
    let half = ((width.max(1.0) / 2.0).round() as i32).max(0);
    let (mut x0, mut y0) = (a[0].round() as i32, a[1].round() as i32);
    let (x1, y1) = (b[0].round() as i32, b[1].round() as i32);
    let dx = (x1 - x0).abs();
    let sx = if x0 < x1 { 1 } else { -1 };
    let dy = -(y1 - y0).abs();
    let sy = if y0 < y1 { 1 } else { -1 };
    let mut err = dx + dy;

    loop {
        for oy in -half..=half {
            for ox in -half..=half {
                plot(frame_buffer, region, x0 + ox, y0 + oy, color);
            }
        }
        if x0 == x1 && y0 == y1 {
            break;
        }
        let e2 = 2 * err;
        if e2 >= dy {
            err += dy;
            x0 += sx;
        }
        if e2 <= dx {
            err += dx;
            y0 += sy;
        }
    }
}

fn draw_circle(
    frame_buffer: &mut FrameBuffer,
    region: Rect,
    centre: [f32; 2],
    radius: f32,
    color: Color,
    width: f32,
) {
    if radius <= 0.5 {
        return;
    }
    let steps = ((radius * 6.0).ceil() as i32).max(16);
    let mut prev = [centre[0] + radius, centre[1]];
    for step in 1..=steps {
        let angle = step as f32 / steps as f32 * std::f32::consts::TAU;
        let next = [
            centre[0] + radius * angle.cos(),
            centre[1] + radius * angle.sin(),
        ];
        draw_line(frame_buffer, region, prev, next, color, width);
        prev = next;
    }
}

fn draw_marker(
    frame_buffer: &mut FrameBuffer,
    region: Rect,
    centre: [f32; 2],
    kind: MarkerKind,
    color: Color,
    size: f32,
) {
    let half = (size * 0.5).max(1.0);
    let cx = centre[0].round() as i32;
    let cy = centre[1].round() as i32;
    let h = half.round() as i32;
    match kind {
        MarkerKind::Square => {
            for y in -h..=h {
                for x in -h..=h {
                    plot(frame_buffer, region, cx + x, cy + y, color);
                }
            }
        }
        MarkerKind::Ring => {
            for y in -h..=h {
                for x in -h..=h {
                    let edge = x.abs() == h || y.abs() == h;
                    if edge {
                        plot(frame_buffer, region, cx + x, cy + y, color);
                    }
                }
            }
        }
        MarkerKind::Circle => {
            for y in -h..=h {
                for x in -h..=h {
                    if x * x + y * y <= h * h {
                        plot(frame_buffer, region, cx + x, cy + y, color);
                    }
                }
            }
        }
        MarkerKind::Diamond => {
            for y in -h..=h {
                for x in -h..=h {
                    if x.abs() + y.abs() <= h {
                        plot(frame_buffer, region, cx + x, cy + y, color);
                    }
                }
            }
        }
    }
}

/// Draws text with the built-in 8x16 cell font, scaled by integer steps.
pub fn draw_text(
    frame_buffer: &mut FrameBuffer,
    region: Rect,
    origin: [f32; 2],
    text: &str,
    style: OverlayTextStyle,
) {
    let scale = style.scale.max(1) as i32;
    let mut cursor_x = origin[0].round() as i32;
    let cursor_y = origin[1].round() as i32;
    for ch in text.chars() {
        let glyph = get_glyph_or_fallback(ch);
        for (row, bits) in glyph.iter().enumerate() {
            for col in 0..8 {
                if bits & (0x80 >> col) == 0 {
                    continue;
                }
                for sy in 0..scale {
                    for sx in 0..scale {
                        plot(
                            frame_buffer,
                            region,
                            cursor_x + col * scale + sx,
                            cursor_y + row as i32 * scale + sy,
                            style.color,
                        );
                    }
                }
            }
        }
        cursor_x += 8 * scale;
    }
}