Skip to main content

strata/
low_level.rs

1//! Low-level direct engine driver and hardware surface APIs.
2//!
3//! Provides direct access to raw swapchains, offscreen framebuffers,
4//! and unabstracted command encoders without RSX virtual DOM tree overhead.
5
6/// Raw windowing surface representation.
7#[derive(Debug, Clone, Copy, PartialEq)]
8pub struct RawSurfaceHandle {
9    pub surface_ptr: usize,
10    pub width: u32,
11    pub height: u32,
12    pub scale_factor: f64,
13}
14
15impl RawSurfaceHandle {
16    pub fn new(surface_ptr: usize, width: u32, height: u32, scale_factor: f64) -> Self {
17        Self {
18            surface_ptr,
19            width,
20            height,
21            scale_factor,
22        }
23    }
24
25    pub fn physical_width(&self) -> u32 {
26        (self.width as f64 * self.scale_factor).round() as u32
27    }
28
29    pub fn physical_height(&self) -> u32 {
30        (self.height as f64 * self.scale_factor).round() as u32
31    }
32}
33
34/// Headless offscreen pixel buffer.
35#[derive(Debug, Clone)]
36pub struct OffscreenBuffer {
37    pub width: u32,
38    pub height: u32,
39    pixels: Vec<u8>, // RGBA8
40}
41
42impl OffscreenBuffer {
43    pub fn new(width: u32, height: u32) -> Self {
44        let size = (width as usize) * (height as usize) * 4;
45        Self {
46            width,
47            height,
48            pixels: vec![0; size],
49        }
50    }
51
52    pub fn clear(&mut self, color: [u8; 4]) {
53        for chunk in self.pixels.as_chunks_mut::<4>().0 {
54            chunk.copy_from_slice(&color);
55        }
56    }
57
58    pub fn set_pixel(&mut self, x: u32, y: u32, color: [u8; 4]) {
59        if x < self.width && y < self.height {
60            let offset = ((y * self.width + x) * 4) as usize;
61            self.pixels[offset..offset + 4].copy_from_slice(&color);
62        }
63    }
64
65    pub fn get_pixel(&self, x: u32, y: u32) -> Option<[u8; 4]> {
66        if x < self.width && y < self.height {
67            let offset = ((y * self.width + x) * 4) as usize;
68            Some([
69                self.pixels[offset],
70                self.pixels[offset + 1],
71                self.pixels[offset + 2],
72                self.pixels[offset + 3],
73            ])
74        } else {
75            None
76        }
77    }
78
79    pub fn to_raw_rgba(&self) -> &[u8] {
80        &self.pixels
81    }
82
83    pub fn byte_len(&self) -> usize {
84        self.pixels.len()
85    }
86}
87
88/// Direct low-level render pass encoder for submitting raw primitives directly to hardware.
89#[derive(Debug, Clone)]
90pub struct LowLevelRenderer {
91    pub width: u32,
92    pub height: u32,
93    quad_count: usize,
94    damaged_rects: Vec<(u32, u32, u32, u32)>,
95    buffer: OffscreenBuffer,
96}
97
98impl LowLevelRenderer {
99    pub fn new(width: u32, height: u32) -> Self {
100        Self {
101            width,
102            height,
103            quad_count: 0,
104            damaged_rects: Vec::new(),
105            buffer: OffscreenBuffer::new(width, height),
106        }
107    }
108
109    /// Submits a colored quad without going through declarative RSX layout or DOM tree.
110    pub fn submit_quad(&mut self, x: u32, y: u32, w: u32, h: u32, color: [u8; 4]) {
111        self.quad_count += 1;
112        let x_end = (x + w).min(self.width);
113        let y_end = (y + h).min(self.height);
114
115        for py in y..y_end {
116            for px in x..x_end {
117                self.buffer.set_pixel(px, py, color);
118            }
119        }
120    }
121
122    /// Records a damaged dirty rectangle for partial swapchain blitting.
123    pub fn damaged_blit(&mut self, x: u32, y: u32, w: u32, h: u32) {
124        self.damaged_rects.push((x, y, w, h));
125    }
126
127    pub fn rendered_quad_count(&self) -> usize {
128        self.quad_count
129    }
130
131    pub fn damaged_rect_count(&self) -> usize {
132        self.damaged_rects.len()
133    }
134
135    pub fn buffer(&self) -> &OffscreenBuffer {
136        &self.buffer
137    }
138}