embedded-3dgfx 0.6.2

3D graphics rendering for embedded systems (fork of embedded-gfx by Kezii)
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
//! Interoperability bridges between embedded-3dgfx and embedded-graphics.
//!
//! # What's here
//!
//! - [`draw_to`] — render a [`DrawPrimitive`] to any `DrawTarget<Color = C>`
//!   (not just `Rgb565`) via on-the-fly color conversion.
//! - [`AsEgPoint`] / [`AsNalgebraPoint`] — zero-cost conversions between
//!   `embedded_graphics_core::geometry::Point` and `nalgebra::Point2<i32>`.
//! - [`nalgebra_to_eg`] / [`eg_to_nalgebra`] — free-function equivalents.
//! - [`render_drawable_to_buffer`] — rasterize any `Drawable<Color = Rgb565>`
//!   into a `&mut [Rgb565]` slice so it can be used as a 3D texture.
//!
//! Readback for `FrameBuf` is no longer implemented here: `embedded-draw-target`
//! provides `PixelRead` for it, which satisfies `ReadPixel` through this
//! crate's blanket impl, so anti-aliased rasterization still works on a plain
//! `FrameBuf` without boilerplate.

use core::fmt::Debug;
use core::marker::PhantomData;

use embedded_graphics_core::{
    Drawable, Pixel,
    draw_target::DrawTarget,
    geometry::{Dimensions, Point},
    pixelcolor::{PixelColor, Rgb565},
    primitives::Rectangle,
};
use embedded_graphics_framebuf::{FrameBuf, backends::FrameBufferBackend};

use crate::draw::draw;
use crate::primitive::DrawPrimitive;

// ── 1. Color adapter ─────────────────────────────────────────────────────────

/// Adapts any `DrawTarget<Color = C>` to accept `Rgb565` pixels by converting
/// each pixel's color on the fly. Constructed internally by [`draw_to`].
pub struct ColorAdapter<'a, C, D> {
    inner: &'a mut D,
    _phantom: PhantomData<C>,
}

impl<C, D> Dimensions for ColorAdapter<'_, C, D>
where
    C: PixelColor,
    D: DrawTarget<Color = C>,
{
    fn bounding_box(&self) -> Rectangle {
        self.inner.bounding_box()
    }
}

impl<C, D> DrawTarget for ColorAdapter<'_, C, D>
where
    C: PixelColor + From<Rgb565>,
    D: DrawTarget<Color = C>,
{
    type Color = Rgb565;
    type Error = D::Error;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Rgb565>>,
    {
        self.inner.draw_iter(
            pixels
                .into_iter()
                .map(|Pixel(pos, color)| Pixel(pos, C::from(color))),
        )
    }
}

/// Draw a [`DrawPrimitive`] to any [`DrawTarget`], converting pixel colors from
/// `Rgb565` (the 3D engine's internal format) to the target's native color type.
///
/// Use the plain [`draw`] function directly when your
/// framebuffer already uses `Rgb565` — it has zero conversion overhead.
///
/// # Example
///
/// ```ignore
/// use embedded_3dgfx::bridge::draw_to;
/// use embedded_graphics_core::pixelcolor::Rgb888;
///
/// // display: impl DrawTarget<Color = Rgb888>
/// draw_to(primitive, &mut display);
/// ```
pub fn draw_to<C, D>(primitive: DrawPrimitive, fb: &mut D)
where
    C: PixelColor + From<Rgb565>,
    D: DrawTarget<Color = C>,
    D::Error: Debug,
{
    let mut adapter = ColorAdapter {
        inner: fb,
        _phantom: PhantomData,
    };
    draw(primitive, &mut adapter);
}

// ── 2. Point conversion helpers ───────────────────────────────────────────────

/// Convert a `nalgebra::Point2<i32>` to an embedded-graphics `Point`.
#[inline]
pub fn nalgebra_to_eg(p: nalgebra::Point2<i32>) -> Point {
    Point::new(p.x, p.y)
}

/// Convert an embedded-graphics `Point` to a `nalgebra::Point2<i32>`.
#[inline]
pub fn eg_to_nalgebra(p: Point) -> nalgebra::Point2<i32> {
    nalgebra::Point2::new(p.x, p.y)
}

/// Extension trait: convert a `nalgebra::Point2<i32>` to an embedded-graphics `Point`.
pub trait AsEgPoint {
    /// Returns the equivalent embedded-graphics `Point`.
    fn as_eg_point(&self) -> Point;
}

impl AsEgPoint for nalgebra::Point2<i32> {
    #[inline]
    fn as_eg_point(&self) -> Point {
        Point::new(self.x, self.y)
    }
}

/// Extension trait: convert an embedded-graphics `Point` to `nalgebra::Point2<i32>`.
pub trait AsNalgebraPoint {
    /// Returns the equivalent `nalgebra::Point2<i32>`.
    fn as_nalgebra(&self) -> nalgebra::Point2<i32>;
}

impl AsNalgebraPoint for Point {
    #[inline]
    fn as_nalgebra(&self) -> nalgebra::Point2<i32> {
        nalgebra::Point2::new(self.x, self.y)
    }
}

// ── 3. Drawable → texture buffer helper ──────────────────────────────────────

struct SliceBackend<'a>(pub &'a mut [Rgb565]);

impl FrameBufferBackend for SliceBackend<'_> {
    type Color = Rgb565;
    fn set(&mut self, index: usize, color: Rgb565) {
        self.0[index] = color;
    }
    fn get(&self, index: usize) -> Rgb565 {
        self.0[index]
    }
    fn nr_elements(&self) -> usize {
        self.0.len()
    }
}

/// Rasterize a [`Drawable<Color = Rgb565>`](Drawable) into a caller-supplied
/// pixel buffer, ready to be used as a 3D texture.
///
/// `width` and `height` must be powers of two (required by
/// [`Texture::new`](crate::texture::Texture::new)), and `buffer.len()` must
/// equal `width * height`.
///
/// # Usage
///
/// ```ignore
/// use embedded_3dgfx::{bridge::render_drawable_to_buffer, texture::Texture};
/// use embedded_graphics_core::pixelcolor::{Rgb565, RgbColor};
///
/// static mut BUF: [Rgb565; 32 * 32] = [Rgb565::BLACK; 32 * 32];
///
/// // SAFETY: single-threaded, called once before the render loop starts.
/// render_drawable_to_buffer(&my_icon, unsafe { &mut BUF }, 32, 32).unwrap();
/// let texture = Texture::new(unsafe { &BUF }, 32, 32);
/// ```
///
/// # Errors
///
/// Returns `Err(())` when `buffer.len() != width * height`.
pub fn render_drawable_to_buffer<D>(
    drawable: &D,
    buffer: &mut [Rgb565],
    width: usize,
    height: usize,
) -> Result<(), ()>
where
    D: Drawable<Color = Rgb565>,
{
    if buffer.len() != width * height {
        return Err(());
    }
    let mut fb = FrameBuf::new(SliceBackend(buffer), width, height);
    drawable.draw(&mut fb).map(|_| ()).map_err(|_| ())
}

// ── 3. Half-Width / Anamorphic Framebuffer Adapter ───────────────────────────

/// An adapter wrapping a `DrawTarget<Color = Rgb565>` that horizontally doubles every pixel.
///
/// Writing a pixel at `(x, y)` emits two adjacent pixels at `(2*x, y)` and `(2*x + 1, y)`.
/// This enables rendering 3D scenes into a half-width coordinate space (e.g. 160x240)
/// while scanout outputs directly to a full-width target (e.g. 320x240), saving 50% RAM.
pub struct HalfWidthDrawTargetAdapter<'a, D> {
    inner: &'a mut D,
}

impl<'a, D> HalfWidthDrawTargetAdapter<'a, D> {
    pub fn new(inner: &'a mut D) -> Self {
        Self { inner }
    }
}

impl<D> Dimensions for HalfWidthDrawTargetAdapter<'_, D>
where
    D: Dimensions,
{
    fn bounding_box(&self) -> Rectangle {
        let orig = self.inner.bounding_box();
        Rectangle::new(
            Point::new(orig.top_left.x / 2, orig.top_left.y),
            embedded_graphics_core::geometry::Size::new(orig.size.width / 2, orig.size.height),
        )
    }
}

impl<D> DrawTarget for HalfWidthDrawTargetAdapter<'_, D>
where
    D: DrawTarget<Color = Rgb565>,
{
    type Color = Rgb565;
    type Error = D::Error;

    fn draw_iter<I>(&mut self, pixels: I) -> Result<(), Self::Error>
    where
        I: IntoIterator<Item = Pixel<Rgb565>>,
    {
        self.inner
            .draw_iter(pixels.into_iter().flat_map(|Pixel(pos, color)| {
                [
                    Pixel(Point::new(pos.x * 2, pos.y), color),
                    Pixel(Point::new(pos.x * 2 + 1, pos.y), color),
                ]
            }))
    }
}

/// Expands a half-width row or buffer `&[Rgb565]` (width `half_w`) into a full-width slice `dst` (width `half_w * 2`)
/// with 2x horizontal pixel replication.
pub fn scanout_half_width_row(src: &[Rgb565], dst: &mut [Rgb565]) {
    let count = src.len().min(dst.len() / 2);
    for i in 0..count {
        dst[i * 2] = src[i];
        dst[i * 2 + 1] = src[i];
    }
}

/// Blits an entire half-width framebuffer `src` (dimensions `half_w x height`) to a full-width `DrawTarget`
/// with 2x horizontal pixel doubling.
pub fn scanout_half_width_buffer<D>(
    src: &[Rgb565],
    half_w: usize,
    height: usize,
    target: &mut D,
) -> Result<(), D::Error>
where
    D: DrawTarget<Color = Rgb565>,
{
    for y in 0..height {
        let row_start = y * half_w;
        let row_end = row_start + half_w;
        if row_end > src.len() {
            break;
        }
        let row = &src[row_start..row_end];
        target.draw_iter(row.iter().enumerate().flat_map(|(x, &color)| {
            [
                Pixel(Point::new((x * 2) as i32, y as i32), color),
                Pixel(Point::new((x * 2 + 1) as i32, y as i32), color),
            ]
        }))?;
    }
    Ok(())
}

/// Reconstructs missing checkerboard pixels in a row slice.
///
/// For pixels where `(x ^ y) & 1 != field_parity`, interpolates between the horizontal neighbors.
pub fn reconstruct_checkerboard_row(
    row: &mut [Rgb565],
    y: usize,
    field: crate::draw::effects::CheckerboardField,
) {
    if field == crate::draw::effects::CheckerboardField::Disabled || row.len() < 2 {
        return;
    }
    let len = row.len();
    for x in 0..len {
        if !field.includes_pixel(x as i32, y as i32) {
            let left = if x > 0 {
                row[x - 1]
            } else {
                row[(x + 1).min(len - 1)]
            };
            let right = if x + 1 < len {
                row[x + 1]
            } else {
                row[x.saturating_sub(1)]
            };
            row[x] = crate::shader::blend::fast_blend_rgb565(left, right, 128);
        }
    }
}

/// Reconstructs missing checkerboard pixels for an entire framebuffer in-place.
pub fn reconstruct_checkerboard_buffer(
    fb: &mut [Rgb565],
    width: usize,
    height: usize,
    field: crate::draw::effects::CheckerboardField,
) {
    if field == crate::draw::effects::CheckerboardField::Disabled || width == 0 {
        return;
    }
    for y in 0..height {
        let start = y * width;
        let end = start + width;
        if end <= fb.len() {
            reconstruct_checkerboard_row(&mut fb[start..end], y, field);
        }
    }
}

#[cfg(test)]
mod tests {
    extern crate std;

    use super::*;
    use embedded_graphics_core::pixelcolor::RgbColor;
    use embedded_graphics_framebuf::backends::{EndianCorrectedBuffer, EndianCorrection};
    use nalgebra::Point2;

    struct SingleRedPixel;

    impl Drawable for SingleRedPixel {
        type Color = Rgb565;
        type Output = ();

        fn draw<D>(&self, target: &mut D) -> Result<(), D::Error>
        where
            D: DrawTarget<Color = Self::Color>,
        {
            target.draw_iter([Pixel(Point::new(0, 0), Rgb565::RED)])
        }
    }

    #[test]
    fn point_conversion_helpers_roundtrip() {
        let n = Point2::new(12, -9);
        let eg = nalgebra_to_eg(n);
        assert_eq!(eg, Point::new(12, -9));
        let n2 = eg_to_nalgebra(eg);
        assert_eq!(n2, n);
        assert_eq!(n.as_eg_point(), eg);
        assert_eq!(eg.as_nalgebra(), n);
    }

    #[test]
    fn render_drawable_to_buffer_validates_buffer_size() {
        let mut too_short = [Rgb565::BLACK; 3];
        let err = render_drawable_to_buffer(&SingleRedPixel, &mut too_short, 2, 2);
        assert!(err.is_err());
    }

    #[test]
    fn render_drawable_to_buffer_draws_into_slice() {
        let mut data = [Rgb565::BLACK; 4];
        render_drawable_to_buffer(&SingleRedPixel, &mut data, 2, 2).unwrap();
        assert_eq!(data[0], Rgb565::RED);
    }

    #[test]
    fn draw_to_writes_primitive_to_target() {
        let backing = std::vec![Rgb565::BLACK; 4].leak();
        let mut fb = FrameBuf::new(
            EndianCorrectedBuffer::new(backing, EndianCorrection::ToLittleEndian),
            2,
            2,
        );
        draw_to::<Rgb565, _>(
            DrawPrimitive::ColoredPoint(Point2::new(1, 1), Rgb565::new(31, 0, 0)),
            &mut fb,
        );
        assert_eq!(fb.get_color_at(Point::new(1, 1)), Rgb565::new(31, 0, 0));
    }

    #[test]
    fn test_half_width_draw_target_adapter() {
        let backing = std::vec![Rgb565::BLACK; 8].leak();
        let mut fb = FrameBuf::new(
            EndianCorrectedBuffer::new(backing, EndianCorrection::ToLittleEndian),
            4,
            2,
        );
        {
            let mut adapter = HalfWidthDrawTargetAdapter::new(&mut fb);
            adapter
                .draw_iter([Pixel(Point::new(0, 0), Rgb565::RED)])
                .unwrap();
        }
        assert_eq!(fb.get_color_at(Point::new(0, 0)), Rgb565::RED);
        assert_eq!(fb.get_color_at(Point::new(1, 0)), Rgb565::RED);
        assert_eq!(fb.get_color_at(Point::new(2, 0)), Rgb565::BLACK);
    }

    #[test]
    fn test_scanout_half_width() {
        let src = [Rgb565::RED, Rgb565::GREEN];
        let mut dst = [Rgb565::BLACK; 4];
        scanout_half_width_row(&src, &mut dst);
        assert_eq!(dst[0], Rgb565::RED);
        assert_eq!(dst[1], Rgb565::RED);
        assert_eq!(dst[2], Rgb565::GREEN);
        assert_eq!(dst[3], Rgb565::GREEN);
    }

    #[test]
    fn test_reconstruct_checkerboard() {
        use crate::draw::effects::CheckerboardField;
        // Even field: (0,0) valid, (1,0) missing, (2,0) valid
        let mut row = [Rgb565::RED, Rgb565::BLACK, Rgb565::RED];
        reconstruct_checkerboard_row(&mut row, 0, CheckerboardField::Even);
        assert_eq!(row[0], Rgb565::RED);
        assert_eq!(row[2], Rgb565::RED);
        // Missing pixel (1,0) should be interpolated to RED
        assert_eq!(row[1], Rgb565::RED);
    }
}