diff options
author | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
---|---|---|
committer | tcmal <me@aria.rip> | 2024-08-25 17:44:23 +0100 |
commit | 6ab13f2d0cb345795f761181a06777ade61ff09c (patch) | |
tree | 42007acef9846d5e79f1bf418a96647f34b530d1 /stockton-render/src/texture/image.rs | |
parent | ccf0074b08ce835cf22e7d46153d1cb3f3d06d32 (diff) |
refactor(all): separate rendering from framework
stockton-passes is mostly just a stand-in until this is
properly separated
Diffstat (limited to 'stockton-render/src/texture/image.rs')
-rw-r--r-- | stockton-render/src/texture/image.rs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/stockton-render/src/texture/image.rs b/stockton-render/src/texture/image.rs new file mode 100644 index 0000000..0e272e9 --- /dev/null +++ b/stockton-render/src/texture/image.rs @@ -0,0 +1,41 @@ +use super::PIXEL_SIZE; + +use core::ptr::copy_nonoverlapping; +use std::convert::TryInto; + +use image::RgbaImage; + +/// An object that can be loaded as an image into GPU memory +pub trait LoadableImage { + fn width(&self) -> u32; + fn height(&self) -> u32; + fn copy_row(&self, y: u32, ptr: *mut u8); + unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize); +} + +impl LoadableImage for RgbaImage { + fn width(&self) -> u32 { + self.width() + } + + fn height(&self) -> u32 { + self.height() + } + + fn copy_row(&self, y: u32, ptr: *mut u8) { + let row_size_bytes = self.width() as usize * PIXEL_SIZE; + let raw: &Vec<u8> = self.as_raw(); + let row = &raw[y as usize * row_size_bytes..(y as usize + 1) * row_size_bytes]; + + unsafe { + copy_nonoverlapping(row.as_ptr(), ptr, row.len()); + } + } + + unsafe fn copy_into(&self, ptr: *mut u8, row_size: usize) { + for y in 0..self.height() as usize { + let dest_base: isize = (y * row_size).try_into().unwrap(); + self.copy_row(y as u32, ptr.offset(dest_base)); + } + } +} |