summary refs log tree commit diff
diff options
context:
space:
mode:
authorIrene Knapp <ireneista@irenes.space>2026-08-09 18:05:15 -0700
committerIrene Knapp <ireneista@irenes.space>2026-08-09 18:05:15 -0700
commit1216c5f3dd1f902fb545db875e2b88f50d24e580 (patch)
treef367588e57d43b391c0c21642886e4aa34fe1b4a
parentd672e147db0df6186c528262367770dcde9d75fc (diff)
move PNG-specific code to a new module files::png
this mirrors files::obj

Force-Push: yes
Change-Id: I17ccae0b54b49453100873ba707ee36406b96057
-rw-r--r--src/files/mod.rs2
-rw-r--r--src/files/png.rs23
-rw-r--r--src/graphics/texture.rs31
-rw-r--r--src/main.rs3
4 files changed, 38 insertions, 21 deletions
diff --git a/src/files/mod.rs b/src/files/mod.rs
index 8bc7539..c977be4 100644
--- a/src/files/mod.rs
+++ b/src/files/mod.rs
@@ -1,3 +1,5 @@
 #![deny(unsafe_code)]
 
 pub mod obj;
+pub mod png;
+
diff --git a/src/files/png.rs b/src/files/png.rs
new file mode 100644
index 0000000..c164613
--- /dev/null
+++ b/src/files/png.rs
@@ -0,0 +1,23 @@
+#![deny(unsafe_code)]
+use crate::error::*;
+use crate::graphics::{ Permanent, Texture };
+
+use std::io::Cursor;
+
+use png::Decoder;
+
+
+pub fn load_png(permanent: &Permanent) -> Result<(Texture, u32)> {
+  let png = include_bytes!("../../textures/forest_leaves_04_diff.png");
+
+  let decoder = Decoder::new(Cursor::new(png));
+  let mut reader = decoder.read_info()?;
+
+  let (width, height) = reader.info().size();
+
+  let mut pixels = vec![0; reader.info().raw_bytes()];
+  reader.next_frame(&mut pixels)?;
+
+  Texture::new(width, height, pixels, permanent)
+}
+
diff --git a/src/graphics/texture.rs b/src/graphics/texture.rs
index a6085e3..b28d540 100644
--- a/src/graphics/texture.rs
+++ b/src/graphics/texture.rs
@@ -6,9 +6,6 @@ use crate::graphics::util::{
   begin_transient_commands, end_transient_commands
 };
 
-use std::io::Cursor;
-
-use png::Decoder;
 use vulkanalia::{ Device, Instance };
 use vulkanalia::vk::{ self, HasBuilder, InstanceV1_0, DeviceV1_0 };
 
@@ -22,15 +19,18 @@ pub struct Texture {
 
 
 impl Texture {
-  pub fn new(permanent: &Permanent) -> Result<(Self, u32)> {
+  pub fn new(width: u32, height: u32, pixels: Vec<u8>,
+             permanent: &Permanent)
+      -> Result<(Self, u32)>
+  {
     let graphics_queue = &permanent.graphics_queue;
     let instance = &permanent.instance;
     let device = &permanent.device;
     let transient_command_pool = &permanent.transient_command_pool;
 
     let (image, image_memory, image_view, mip_count)
-            = init_texture(instance, device, graphics_queue,
-                           &transient_command_pool)?;
+            = init_texture(width, height, pixels, instance, device,
+                           graphics_queue, &transient_command_pool)?;
 
     Ok((Texture {
       image,
@@ -51,19 +51,13 @@ impl Texture {
 
 
 #[allow(unsafe_code)]
-fn init_texture(instance: &Instance, device: &Device, queue: &vk::Queue,
-                command_pool: &vk::CommandPool)
+fn init_texture(width: u32, height: u32, pixels: Vec<u8>,
+                instance: &Instance, device: &Device,
+                queue: &vk::Queue, command_pool: &vk::CommandPool)
     -> Result<(vk::Image, vk::DeviceMemory, vk::ImageView, u32)>
 {
   let physical_device = device.physical_device();
 
-  let png = include_bytes!("../../textures/forest_leaves_04_diff.png");
-
-  let decoder = Decoder::new(Cursor::new(png));
-  let mut reader = decoder.read_info()?;
-
-  let (width, height) = reader.info().size();
-
   let format_properties = unsafe {
     instance.get_physical_device_format_properties(physical_device,
                                                    vk::Format::R8G8B8A8_SRGB)
@@ -79,15 +73,12 @@ fn init_texture(instance: &Instance, device: &Device, queue: &vk::Queue,
     1
   };
 
-  let mut pixels = vec![0; reader.info().raw_bytes()];
-  reader.next_frame(&mut pixels)?;
-
   let (staging_buffer, staging_memory, _byte_size)
           = stage_in_buffer(instance, device, &pixels)?;
 
   let (image, image_memory)
-          = allocate_image(instance, device,
-                           width, height, mip_count, vk::SampleCountFlags::_1,
+          = allocate_image(instance, device, width, height, mip_count,
+                           vk::SampleCountFlags::_1,
                            vk::Format::R8G8B8A8_SRGB,
                            vk::ImageTiling::OPTIMAL,
                            vk::ImageUsageFlags::SAMPLED
diff --git a/src/main.rs b/src/main.rs
index 3e8fb0b..819ce40 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use crate::graphics::window_dressing::{
 };
 use crate::linear_algebra::{ Vec3, Mat4, Transformation };
 use crate::files::obj::load_obj;
+use crate::files::png::load_png;
 use crate::shader_data::UniformBlock;
 
 use std::cell::RefCell;
@@ -67,7 +68,7 @@ impl Surreality {
     let (permanent, for_reinit, enable_anisotropy, enable_swapchain)
         = Permanent::new(event_loop)?;
 
-    let (texture, mip_count) = Texture::new(&permanent)?;
+    let (texture, mip_count) = load_png(&permanent)?;
 
     if enable_swapchain.0 {
       let window_dressing = WindowDressing::new(&permanent, &for_reinit,