]> git.nega.tv - josh/narcissus/commitdiff
narcissus-gpu: Explicit flag for host mapped memory
authorJoshua Simmons <josh@nega.tv>
Mon, 17 Jul 2023 18:57:01 +0000 (20:57 +0200)
committerJoshua Simmons <josh@nega.tv>
Mon, 17 Jul 2023 18:57:01 +0000 (20:57 +0200)
bins/narcissus/src/helpers.rs
bins/narcissus/src/main.rs
bins/narcissus/src/mapped_buffer.rs
libs/narcissus-gpu/src/backend/vulkan/allocator.rs
libs/narcissus-gpu/src/backend/vulkan/mod.rs
libs/narcissus-gpu/src/lib.rs

index 3456b7004fc3416cae43a63290bbdf35e686d8a8..dfb45605a124877e19f8b3157d4be1c893bf3783 100644 (file)
@@ -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,
             },
index d8b1d1a668b0f1caf243450d77f17390daf9a21a..640279fb1cda6e565e3c0909ec0e2ef452dc79a9 100644 (file)
@@ -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,
index 77acf18f5f4e600d1920b00198a620ab29187bd4..f25b1b56abd667d59499b87c9557f5a3aafb142e 100644 (file)
@@ -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,
         });
index 79dbfe15103fe5f61773199ec637fc947f120298..db79801c5212113ebc04f0eb9569e2a5fff59c3a 100644 (file)
@@ -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,
         })
     }
index 81d612efa709bc436af344b679bc69656362e98c..30cf005f14f261580cef7286a73d6dab88be1740 100644 (file)
@@ -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
index 9ab97bff7247a529392ad6f435d3b10a588c9080..9a5526e7ddfa0ccd812d68d53504c80d9604b445 100644 (file)
@@ -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,