From d29b121aca33a937756d721881781312bc056c49 Mon Sep 17 00:00:00 2001 From: Janis Date: Sat, 21 Dec 2024 22:18:18 +0100 Subject: [PATCH] idk man... --- crates/game/src/main.rs | 10 +++++- crates/renderer/src/images.rs | 56 ++++++++++++++++++++--------- crates/renderer/src/lib.rs | 31 ++++++++-------- crates/renderer/src/render_graph.rs | 8 ++--- 4 files changed, 68 insertions(+), 37 deletions(-) diff --git a/crates/game/src/main.rs b/crates/game/src/main.rs index 9a367e2..9c14519 100644 --- a/crates/game/src/main.rs +++ b/crates/game/src/main.rs @@ -16,6 +16,10 @@ struct WindowState { egui_platform: egui_winit_platform::Platform, } +struct EguiRenderState { + textures: BTreeMap, +} + struct WinitState { last_resize_events: BTreeMap>, window_attrs: WindowAttributes, @@ -77,7 +81,11 @@ impl WinitState { // rendering self.renderer - .debug_draw(&window_id, || window.window.pre_present_notify()) + .debug_draw( + &window_id, + || { // window.window.pre_present_notify() + }, + ) .expect("drawing"); window.window.request_redraw(); } diff --git a/crates/renderer/src/images.rs b/crates/renderer/src/images.rs index 561bcc5..1182ae1 100644 --- a/crates/renderer/src/images.rs +++ b/crates/renderer/src/images.rs @@ -1,19 +1,31 @@ -use super::{Device, Queue, VkAllocator}; +use std::sync::Arc; + +use super::{Device, Queue}; use ash::{prelude::*, vk}; use vk_mem::Alloc; pub struct Image2D { - alloc: VkAllocator, - image: vk::Image, + device: Device, size: vk::Extent2D, mip_levels: u32, format: vk::Format, + image: vk::Image, allocation: vk_mem::Allocation, } +impl Drop for Image2D { + fn drop(&mut self) { + unsafe { + self.device + .alloc() + .destroy_image(self.image, &mut self.allocation); + } + } +} + impl Image2D { pub fn new_exclusive( - alloc: VkAllocator, + device: &Device, extent: vk::Extent2D, mip_levels: u32, array_layers: u32, @@ -22,7 +34,7 @@ impl Image2D { usage: vk::ImageUsageFlags, memory_usage: vk_mem::MemoryUsage, alloc_flags: vk_mem::AllocationCreateFlags, - ) -> VkResult { + ) -> VkResult> { let create_info = vk::ImageCreateInfo::default() .array_layers(array_layers) .mip_levels(mip_levels) @@ -46,25 +58,25 @@ impl Image2D { }; let (image, allocation) = - unsafe { alloc.create_image(&create_info, &alloc_info)? }; + unsafe { device.alloc().create_image(&create_info, &alloc_info)? }; - Ok(Self { - alloc, - image, + Ok(Arc::new(Self { size: extent, mip_levels, format, + device: device.clone(), + image, allocation, - }) + })) } pub fn view( - &self, + self: &Arc, device: &Device, aspect: vk::ImageAspectFlags, - ) -> VkResult { + ) -> VkResult> { let create_info = vk::ImageViewCreateInfo::default() - .image(self.image) + .image(self.image()) .view_type(vk::ImageViewType::TYPE_2D) .format(self.format) .components(vk::ComponentMapping::default()) @@ -80,7 +92,11 @@ impl Image2D { let view = unsafe { device.dev().create_image_view(&create_info, None)? }; - Ok(view) + Ok(Arc::new(ImageView2D { + view, + image: self.clone(), + aspect, + })) } pub fn image(&self) -> vk::Image { @@ -88,14 +104,22 @@ impl Image2D { } } -impl Drop for Image2D { +pub struct ImageView2D { + view: vk::ImageView, + aspect: vk::ImageAspectFlags, + image: Arc, +} + +impl Drop for ImageView2D { fn drop(&mut self) { unsafe { - self.alloc.destroy_image(self.image, &mut self.allocation); + self.image.device.dev().destroy_image_view(self.view, None); } } } +impl ImageView2D {} + pub struct QueueOwnership { src: u32, dst: u32, diff --git a/crates/renderer/src/lib.rs b/crates/renderer/src/lib.rs index c96f65d..1cf1afc 100644 --- a/crates/renderer/src/lib.rs +++ b/crates/renderer/src/lib.rs @@ -36,9 +36,8 @@ use tracing::info; mod commands; mod images; mod render_graph; -mod sync; -type VkAllocator = Arc; +mod sync; #[derive(Debug, thiserror::Error)] pub enum Error { @@ -470,6 +469,7 @@ impl DeviceQueueFamilies { struct DeviceInner { instance: Arc, physical: PhysicalDevice, + alloc: vk_mem::Allocator, device: ash::Device, swapchain: khr::swapchain::Device, debug_utils: ash::ext::debug_utils::Device, @@ -505,6 +505,9 @@ impl Device { fn weak(&self) -> WeakDevice { Arc::downgrade(&self.0) } + fn alloc(&self) -> &vk_mem::Allocator { + &self.0.alloc + } fn dev(&self) -> &ash::Device { &self.0.device } @@ -1194,7 +1197,6 @@ impl Drop for Surface { pub struct Vulkan { instance: Arc, device: Device, - alloc: VkAllocator, } impl Drop for Vulkan { @@ -1393,19 +1395,7 @@ impl Vulkan { let device = Self::create_device(instance.clone(), pdev, &mut features)?; - let alloc_info = vk_mem::AllocatorCreateInfo::new( - &instance.instance, - device.dev(), - device.phy(), - ); - - let alloc = Arc::new(unsafe { vk_mem::Allocator::new(alloc_info)? }); - - Ok(Self { - instance, - device, - alloc, - }) + Ok(Self { instance, device }) } fn queue_family_supports_presentation( @@ -1693,6 +1683,14 @@ impl Vulkan { .map(get_queue) .unwrap_or(compute_queue.clone()); + let alloc_info = vk_mem::AllocatorCreateInfo::new( + &instance.instance, + &device, + pdev.pdev, + ); + + let alloc = unsafe { vk_mem::Allocator::new(alloc_info)? }; + Device::new(DeviceInner { device: device.clone(), physical: pdev, @@ -1705,6 +1703,7 @@ impl Vulkan { &device, ), instance, + alloc, allocated_queues, main_queue, present_queue, diff --git a/crates/renderer/src/render_graph.rs b/crates/renderer/src/render_graph.rs index 1c6cc05..f48d7c8 100644 --- a/crates/renderer/src/render_graph.rs +++ b/crates/renderer/src/render_graph.rs @@ -1,6 +1,6 @@ use ash::vk; -struct Rgba([f32;4]); +struct Rgba([f32; 4]); enum LoadOp { Clear(Rgba), @@ -9,7 +9,8 @@ enum LoadOp { } enum StoreOp { - DontCare,Store, + DontCare, + Store, } struct AttachmentInfo { @@ -23,5 +24,4 @@ struct Texture { texture: vk::Image, } -pub struct RenderGraph { -} +pub struct RenderGraph {}