diff --git a/engine/src/main.rs b/engine/src/main.rs index 544604d..5e0711f 100644 --- a/engine/src/main.rs +++ b/engine/src/main.rs @@ -10,7 +10,7 @@ mod util; mod physics; use render::vulkan::data; -use render::renderable::{Object, Renderable, Resource, ResourceManager, Material, Mesh, ImageRefOrColor}; +use render::renderable::{Object, Renderable, ResourceManager, Material, Mesh, ImageRefOrColor}; use physics::color; fn main() { @@ -63,13 +63,13 @@ fn main() { 20, 22, 21, 22, 20, 23, ]); - rm.add(Resource::Mesh(Mesh::new(vertices, indices))) + rm.add_mesh(Mesh::new(vertices, indices)) }; - let material_cube = rm.add(Resource::Material(Material::new( + let material_cube = rm.add_material(Material::new( ImageRefOrColor::image(String::from("assets/test-128px.png")), ImageRefOrColor::color(color::LinearF32::new(1.0)), - ))); + )); let mut renderer = render::Renderer::initialize(); diff --git a/engine/src/render/renderable.rs b/engine/src/render/renderable.rs index f72c172..3eaa02d 100644 --- a/engine/src/render/renderable.rs +++ b/engine/src/render/renderable.rs @@ -66,19 +66,16 @@ impl<'a> ResourceManager { } } - pub fn add(&mut self, r: Resource) -> ResourceID { - match r { - Resource::Material(t) => { - let id = t.id; - self.materials.insert(id, t); - ResourceID::Material(id) - } - Resource::Mesh(t) => { - let id = t.id; - self.meshes.insert(id, t); - ResourceID::Mesh(id) - } - } + pub fn add_material(&mut self, t: Material) -> ResourceID { + let id = t.id; + self.materials.insert(id, t); + ResourceID::Material(id) + } + + pub fn add_mesh(&mut self, t: Mesh) -> ResourceID { + let id = t.id; + self.meshes.insert(id, t); + ResourceID::Mesh(id) } pub fn material(&'a self, id: &ResourceID) -> Option<&'a Material> { @@ -96,130 +93,6 @@ impl<'a> ResourceManager { } } -pub trait ChannelLayout { - fn vulkan_from_image( - image: Arc, - graphics_queue: Arc, - ) -> Arc>; - - fn vulkan_from_value( - &self, - graphics_queue: Arc, - ) -> Arc>; -} - -impl ChannelLayout for color::XYZ { - fn vulkan_from_image( - image: Arc, - graphics_queue: Arc, - ) -> Arc> { - let (width, height) = (image.width(), image.height()); - let rgba = image.to_rgba(); - // TODO(q3k): RGB -> CIE XYZ - let (image_view, future) = vm::ImmutableImage::from_iter( - rgba.into_raw().iter().cloned(), - vm::Dimensions::Dim2d{ width, height }, - vf::Format::R8G8B8A8Unorm, - graphics_queue.clone(), - ).unwrap(); - - future.flush().unwrap(); - image_view - } - - fn vulkan_from_value( - &self, - graphics_queue: Arc, - ) -> Arc> { - let mut image = image::ImageBuffer::, Vec>::new(1, 1); - image.put_pixel(0, 0, image::Rgba([self.x, self.y, self.z, 0.0])); - - let (image_view, future) = vm::ImmutableImage::from_iter( - image.into_raw().iter().cloned(), - vm::Dimensions::Dim2d{ width: 1, height: 1 }, - vf::Format::R32G32B32A32Sfloat, - graphics_queue.clone(), - ).unwrap(); - future.flush().unwrap(); - image_view - } -} - -impl ChannelLayout for color::LinearF32 { - fn vulkan_from_image( - image: Arc, - graphics_queue: Arc, - ) -> Arc> { - let (width, height) = (image.width(), image.height()); - assert!(match image.color() { - image::ColorType::L8 => true, - image::ColorType::L16 => true, - _ => false, - }, "linearf32 texture must be 8-bit grayscale"); - let gray = image.to_luma(); - let (image_view, future) = vm::ImmutableImage::from_iter( - gray.into_raw().iter().cloned(), - vm::Dimensions::Dim2d{ width, height }, - vf::Format::R8G8B8A8Unorm, - graphics_queue.clone(), - ).unwrap(); - - future.flush().unwrap(); - image_view - } - - fn vulkan_from_value( - &self, - graphics_queue: Arc, - ) -> Arc> { - let mut image = image::ImageBuffer::, Vec>::new(1, 1); - image.put_pixel(0, 0, image::Luma([self.d])); - - let (image_view, future) = vm::ImmutableImage::from_iter( - image.into_raw().iter().cloned(), - vm::Dimensions::Dim2d{ width: 1, height: 1 }, - vf::Format::R32Sfloat, - graphics_queue.clone(), - ).unwrap(); - - future.flush().unwrap(); - image_view - } -} - -pub enum ImageRefOrColor { - Color(T), - ImageRef(ImageRef), -} - -impl ImageRefOrColor { - fn vulkan_image(&self, graphics_queue: Arc) -> Arc> { - match self { - ImageRefOrColor::::Color(c) => c.vulkan_from_value(graphics_queue), - ImageRefOrColor::::ImageRef(r) => T::vulkan_from_image(r.load(), graphics_queue), - } - } - - pub fn color(color: T) -> Self { - ImageRefOrColor::::Color(color) - } - - pub fn image(name: String) -> Self { - ImageRefOrColor::::ImageRef(ImageRef{ name }) - } -} - -pub struct ImageRef { - name: String, -} - -impl ImageRef { - fn load (&self) -> Arc { - let path = &file::resource_path(self.name.clone()); - Arc::new(image::open(path).unwrap()) - } -} - pub struct Material { diffuse: ImageRefOrColor, roughness: ImageRefOrColor, @@ -359,3 +232,128 @@ impl Renderable for Object { Some((self.mesh, self.material, &self.transform)) } } + +pub trait ChannelLayout { + fn vulkan_from_image( + image: Arc, + graphics_queue: Arc, + ) -> Arc>; + + fn vulkan_from_value( + &self, + graphics_queue: Arc, + ) -> Arc>; +} + +impl ChannelLayout for color::XYZ { + fn vulkan_from_image( + image: Arc, + graphics_queue: Arc, + ) -> Arc> { + let (width, height) = (image.width(), image.height()); + let rgba = image.to_rgba(); + // TODO(q3k): RGB -> CIE XYZ + let (image_view, future) = vm::ImmutableImage::from_iter( + rgba.into_raw().iter().cloned(), + vm::Dimensions::Dim2d{ width, height }, + vf::Format::R8G8B8A8Unorm, + graphics_queue.clone(), + ).unwrap(); + + future.flush().unwrap(); + image_view + } + + fn vulkan_from_value( + &self, + graphics_queue: Arc, + ) -> Arc> { + let mut image = image::ImageBuffer::, Vec>::new(1, 1); + image.put_pixel(0, 0, image::Rgba([self.x, self.y, self.z, 0.0])); + + let (image_view, future) = vm::ImmutableImage::from_iter( + image.into_raw().iter().cloned(), + vm::Dimensions::Dim2d{ width: 1, height: 1 }, + vf::Format::R32G32B32A32Sfloat, + graphics_queue.clone(), + ).unwrap(); + future.flush().unwrap(); + image_view + } +} + +impl ChannelLayout for color::LinearF32 { + fn vulkan_from_image( + image: Arc, + graphics_queue: Arc, + ) -> Arc> { + let (width, height) = (image.width(), image.height()); + assert!(match image.color() { + image::ColorType::L8 => true, + image::ColorType::L16 => true, + _ => false, + }, "linearf32 texture must be 8-bit grayscale"); + let gray = image.to_luma(); + let (image_view, future) = vm::ImmutableImage::from_iter( + gray.into_raw().iter().cloned(), + vm::Dimensions::Dim2d{ width, height }, + vf::Format::R8G8B8A8Unorm, + graphics_queue.clone(), + ).unwrap(); + + future.flush().unwrap(); + image_view + } + + fn vulkan_from_value( + &self, + graphics_queue: Arc, + ) -> Arc> { + let mut image = image::ImageBuffer::, Vec>::new(1, 1); + image.put_pixel(0, 0, image::Luma([self.d])); + + let (image_view, future) = vm::ImmutableImage::from_iter( + image.into_raw().iter().cloned(), + vm::Dimensions::Dim2d{ width: 1, height: 1 }, + vf::Format::R32Sfloat, + graphics_queue.clone(), + ).unwrap(); + + future.flush().unwrap(); + image_view + } +} + +pub enum ImageRefOrColor { + Color(T), + ImageRef(ImageRef), +} + +impl ImageRefOrColor { + fn vulkan_image(&self, graphics_queue: Arc) -> Arc> { + match self { + ImageRefOrColor::::Color(c) => c.vulkan_from_value(graphics_queue), + ImageRefOrColor::::ImageRef(r) => T::vulkan_from_image(r.load(), graphics_queue), + } + } + + pub fn color(color: T) -> Self { + ImageRefOrColor::::Color(color) + } + + pub fn image(name: String) -> Self { + ImageRefOrColor::::ImageRef(ImageRef{ name }) + } +} + +pub struct ImageRef { + name: String, +} + +impl ImageRef { + fn load (&self) -> Arc { + let path = &file::resource_path(self.name.clone()); + Arc::new(image::open(path).unwrap()) + } +} +