From: Joshua Simmons Date: Mon, 17 Jul 2023 18:57:01 +0000 (+0200) Subject: narcissus-gpu: Explicit flag for host mapped memory X-Git-Url: https://git.nega.tv//gitweb.cgi?a=commitdiff_plain;h=d8aa8748e0ea18f21d785ab649cf0902c717a474;p=josh%2Fnarcissus narcissus-gpu: Explicit flag for host mapped memory --- diff --git a/bins/narcissus/src/helpers.rs b/bins/narcissus/src/helpers.rs index 3456b70..dfb4560 100644 --- a/bins/narcissus/src/helpers.rs +++ b/bins/narcissus/src/helpers.rs @@ -103,7 +103,8 @@ where let initial_data = std::slice::from_raw_parts(data.as_ptr() as *const u8, len); device.create_buffer_with_data( &BufferDesc { - location: MemoryLocation::HostMapped, + memory_location: MemoryLocation::Host, + host_mapped: true, usage, size: len, }, diff --git a/bins/narcissus/src/main.rs b/bins/narcissus/src/main.rs index d8b1d1a..640279f 100644 --- a/bins/narcissus/src/main.rs +++ b/bins/narcissus/src/main.rs @@ -83,7 +83,7 @@ pub fn main() { ); let blåhaj_image = device.create_image(&ImageDesc { - location: MemoryLocation::Device, + memory_location: MemoryLocation::Device, usage: ImageUsageFlags::SAMPLED | ImageUsageFlags::TRANSFER, dimension: ImageDimension::Type2d, format: ImageFormat::RGBA8_SRGB, @@ -114,7 +114,7 @@ pub fn main() { ); let glyph_atlas = device.create_image(&ImageDesc { - location: MemoryLocation::Device, + memory_location: MemoryLocation::Device, usage: ImageUsageFlags::SAMPLED | ImageUsageFlags::TRANSFER, dimension: ImageDimension::Type2d, format: ImageFormat::R8_UNORM, @@ -130,7 +130,8 @@ pub fn main() { let mut buffers = (0..4096) .map(|_| { device.create_buffer(&BufferDesc { - location: MemoryLocation::HostMapped, + memory_location: MemoryLocation::Host, + host_mapped: true, usage: BufferUsageFlags::STORAGE, size: 16 + rng.next_bound_usize(1024 - 16), }) @@ -139,7 +140,8 @@ pub fn main() { buffers.extend((0..512).map(|_| { device.create_buffer(&BufferDesc { - location: MemoryLocation::HostMapped, + memory_location: MemoryLocation::Host, + host_mapped: true, usage: BufferUsageFlags::STORAGE, size: 16 + rng.next_bound_usize(10 * 1024 * 1024 - 16), }) @@ -291,7 +293,7 @@ pub fn main() { if width != depth_width || height != depth_height { device.destroy_image(&frame, depth_image); depth_image = device.create_image(&ImageDesc { - location: MemoryLocation::Device, + memory_location: MemoryLocation::Device, usage: ImageUsageFlags::DEPTH_STENCIL_ATTACHMENT, dimension: ImageDimension::Type2d, format: ImageFormat::DEPTH_F32, diff --git a/bins/narcissus/src/mapped_buffer.rs b/bins/narcissus/src/mapped_buffer.rs index 77acf18..f25b1b5 100644 --- a/bins/narcissus/src/mapped_buffer.rs +++ b/bins/narcissus/src/mapped_buffer.rs @@ -11,7 +11,8 @@ pub struct MappedBuffer<'a> { impl<'a> MappedBuffer<'a> { pub fn new(device: &'a dyn Device, usage: BufferUsageFlags, len: usize) -> Self { let buffer = device.create_buffer(&BufferDesc { - location: MemoryLocation::HostMapped, + memory_location: MemoryLocation::Host, + host_mapped: true, usage, size: len, }); diff --git a/libs/narcissus-gpu/src/backend/vulkan/allocator.rs b/libs/narcissus-gpu/src/backend/vulkan/allocator.rs index 79dbfe1..db79801 100644 --- a/libs/narcissus-gpu/src/backend/vulkan/allocator.rs +++ b/libs/narcissus-gpu/src/backend/vulkan/allocator.rs @@ -28,12 +28,6 @@ pub enum VulkanMemoryDedicatedDesc { Buffer(vk::Buffer), } -pub struct VulkanMemoryDesc { - pub requirements: vk::MemoryRequirements, - pub memory_location: MemoryLocation, - pub _linear: bool, -} - #[derive(Clone)] pub struct VulkanMemoryDedicated { memory: vk::DeviceMemory, @@ -116,23 +110,26 @@ impl VulkanDevice { pub fn allocate_memory_dedicated( &self, - desc: &VulkanMemoryDesc, + memory_location: MemoryLocation, + requirements: &vk::MemoryRequirements, dedicated_desc: &VulkanMemoryDedicatedDesc, ) -> VulkanMemory { - let memory_property_flags = match desc.memory_location { - MemoryLocation::HostMapped => vk::MemoryPropertyFlags::HOST_VISIBLE, + let memory_property_flags = match memory_location { + MemoryLocation::Host => vk::MemoryPropertyFlags::HOST_VISIBLE, MemoryLocation::Device => vk::MemoryPropertyFlags::DEVICE_LOCAL, }; + let size = requirements.size; + let memory_type_index = - self.find_memory_type_index(desc.requirements.memory_type_bits, memory_property_flags); + self.find_memory_type_index(requirements.memory_type_bits, memory_property_flags); let allocator = self.allocators[memory_type_index.widen()] .as_ref() .expect("returned a memory type index that has no associated allocator"); let mut allocate_info = vk::MemoryAllocateInfo { - allocation_size: desc.requirements.size, + allocation_size: size, memory_type_index, ..default() }; @@ -177,19 +174,26 @@ impl VulkanDevice { VulkanMemory::Dedicated(VulkanMemoryDedicated { memory, mapped_ptr, - size: desc.requirements.size, + size, memory_type_index, }) } - pub fn allocate_memory(&self, desc: &VulkanMemoryDesc) -> VulkanMemory { - let memory_property_flags = match desc.memory_location { - MemoryLocation::HostMapped => vk::MemoryPropertyFlags::HOST_VISIBLE, + pub fn allocate_memory( + &self, + memory_location: MemoryLocation, + requirements: &vk::MemoryRequirements, + ) -> VulkanMemory { + let memory_property_flags = match memory_location { + MemoryLocation::Host => vk::MemoryPropertyFlags::HOST_VISIBLE, MemoryLocation::Device => vk::MemoryPropertyFlags::DEVICE_LOCAL, }; + let size = requirements.size; + let align = requirements.alignment; + let memory_type_index = - self.find_memory_type_index(desc.requirements.memory_type_bits, memory_property_flags); + self.find_memory_type_index(requirements.memory_type_bits, memory_property_flags); let allocator = self.allocators[memory_type_index.widen()] .as_ref() @@ -198,9 +202,7 @@ impl VulkanDevice { let mut tlsf = allocator.tlsf.lock(); let allocation = { - if let Some(allocation) = - tlsf.alloc(desc.requirements.size, desc.requirements.alignment) - { + if let Some(allocation) = tlsf.alloc(size, align) { allocation } else { let allocate_info = vk::MemoryAllocateInfo { @@ -241,14 +243,13 @@ impl VulkanDevice { VulkanAllocationInfo { memory, mapped_ptr }, ); - tlsf.alloc(desc.requirements.size, desc.requirements.alignment) - .expect("failed to allocate") + tlsf.alloc(size, align).expect("failed to allocate") } }; VulkanMemory::SubAlloc(VulkanMemorySubAlloc { allocation, - size: desc.requirements.size, + size: requirements.size, memory_type_index, }) } diff --git a/libs/narcissus-gpu/src/backend/vulkan/mod.rs b/libs/narcissus-gpu/src/backend/vulkan/mod.rs index 81d612e..30cf005 100644 --- a/libs/narcissus-gpu/src/backend/vulkan/mod.rs +++ b/libs/narcissus-gpu/src/backend/vulkan/mod.rs @@ -31,7 +31,7 @@ mod libc; mod wsi; use self::{ - allocator::{VulkanAllocator, VulkanMemory, VulkanMemoryDedicatedDesc, VulkanMemoryDesc}, + allocator::{VulkanAllocator, VulkanMemory, VulkanMemoryDedicatedDesc}, barrier::{vulkan_image_memory_barrier, vulkan_memory_barrier}, convert::*, wsi::{VulkanWsi, VulkanWsiFrame}, @@ -826,19 +826,15 @@ impl VulkanDevice { || memory_dedicated_requirements.requires_dedicated_allocation == vk::Bool32::True { self.allocate_memory_dedicated( - &VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: desc.location, - _linear: true, - }, + desc.memory_location, + &memory_requirements.memory_requirements, &VulkanMemoryDedicatedDesc::Buffer(buffer), ) } else { - self.allocate_memory(&VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: desc.location, - _linear: true, - }) + self.allocate_memory( + desc.memory_location, + &memory_requirements.memory_requirements, + ) }; if let Some(initial_data) = initial_data { @@ -1010,19 +1006,15 @@ impl Device for VulkanDevice { || memory_dedicated_requirements.requires_dedicated_allocation == vk::Bool32::True { self.allocate_memory_dedicated( - &VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: desc.location, - _linear: true, - }, + desc.memory_location, + &memory_requirements.memory_requirements, &VulkanMemoryDedicatedDesc::Image(image), ) } else { - self.allocate_memory(&VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: desc.location, - _linear: true, - }) + self.allocate_memory( + desc.memory_location, + &memory_requirements.memory_requirements, + ) }; unsafe { @@ -2384,11 +2376,10 @@ impl VulkanDevice { &mut memory_requirements, ); - let memory = self.allocate_memory(&VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: MemoryLocation::HostMapped, - _linear: true, - }); + let memory = self.allocate_memory( + MemoryLocation::Host, + &memory_requirements.memory_requirements, + ); unsafe { self.device_fn.bind_buffer_memory2( @@ -2519,11 +2510,10 @@ impl VulkanDevice { &mut memory_requirements, ); - let memory = self.allocate_memory(&VulkanMemoryDesc { - requirements: memory_requirements.memory_requirements, - memory_location: MemoryLocation::HostMapped, - _linear: true, - }); + let memory = self.allocate_memory( + MemoryLocation::Host, + &memory_requirements.memory_requirements, + ); assert!(!memory.mapped_ptr().is_null()); // SAFETY: The memory has just been allocated, so as long as the pointer is diff --git a/libs/narcissus-gpu/src/lib.rs b/libs/narcissus-gpu/src/lib.rs index 9ab97bf..9a5526e 100644 --- a/libs/narcissus-gpu/src/lib.rs +++ b/libs/narcissus-gpu/src/lib.rs @@ -78,7 +78,7 @@ impl<'a> TransientBuffer<'a> { #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum MemoryLocation { - HostMapped, + Host, Device, } @@ -204,13 +204,14 @@ impl BufferUsageFlags { } pub struct BufferDesc { - pub location: MemoryLocation, + pub memory_location: MemoryLocation, + pub host_mapped: bool, pub usage: BufferUsageFlags, pub size: usize, } pub struct ImageDesc { - pub location: MemoryLocation, + pub memory_location: MemoryLocation, pub usage: ImageUsageFlags, pub dimension: ImageDimension, pub format: ImageFormat,