From 655491bd278b6d4f3b474260877cc3e5041f9b7f Mon Sep 17 00:00:00 2001 From: Marijn Suijten Date: Fri, 21 Jun 2024 10:58:42 +0200 Subject: [PATCH] Squashed commit of the following: commit b1407d528dccbfd27bda1c0020c22ca6a4717794 Author: Marijn Suijten Date: Wed Jun 12 14:34:30 2024 +0200 metal: Use our own `MemoryLocation` for `MTLHeap` (`MemoryBlock`) labels commit 91a839dd7b56f6c183cf108a333844513f734e16 Author: Marijn Suijten Date: Wed Jun 12 14:32:38 2024 +0200 metal: Delete strange copy-paste functions that always panic via `todo!()` Since we're not implementing `trait`s here (every backend just "looks" the same but is different because of heavy API coupling), we shouldn't have a bunch of dead `todo!()` unimplemented functions publicly exposed. commit 8182a398901edc4e40cfcecc5bd012f4c72d116e Merge: c812c08 95ba6d0 Author: Marijn Suijten Date: Wed Jun 12 12:47:19 2024 +0200 Merge remote-tracking branch 'origin/main' into metal-clean commit c812c082177bd264b8368bdd7d102a52015e9c34 Author: Marijn Suijten Date: Wed Jun 12 10:15:56 2024 +0200 Remove clone commit 0a943ea58845c828b085062781c835d316bdaec5 Author: Lily Haverlag Date: Wed May 22 12:19:04 2024 +0200 Add white lines commit 465713f52a2778070c0771f750f15629a4a85b5f Author: Lily Haverlag Date: Wed May 22 12:18:36 2024 +0200 Remove unused empty structs commit d8092089551342e1cb7bb06484084fe56fb58ebb Author: Lily Haverlag Date: Wed May 22 12:18:26 2024 +0200 Improve debug label for memory blocks commit 33e8ae0d9a6ca4cd11350db225ed24bacbd45373 Author: Lily Haverlag Date: Wed May 22 12:18:13 2024 +0200 Add debug derives --- src/metal/mod.rs | 51 ++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 21 deletions(-) diff --git a/src/metal/mod.rs b/src/metal/mod.rs index 89cf1c5..5ccc5b2 100644 --- a/src/metal/mod.rs +++ b/src/metal/mod.rs @@ -5,7 +5,7 @@ use crate::{ allocator::{self, AllocatorReport, MemoryBlockReport}, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result, }; -use log::{debug, Level}; +use log::debug; fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions { match location { @@ -16,6 +16,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptio } } +#[derive(Debug)] pub struct Allocation { chunk_id: Option, offset: u64, @@ -70,6 +71,7 @@ impl Allocation { } } +#[derive(Clone, Debug)] pub struct AllocationCreateDesc<'a> { /// Name of the allocation, for tracking and debugging purposes pub name: &'a str, @@ -130,21 +132,28 @@ impl<'a> AllocationCreateDesc<'a> { } } } + pub struct Allocator { device: Arc, debug_settings: AllocatorDebugSettings, memory_types: Vec, allocation_sizes: AllocationSizes, } + +#[derive(Debug)] pub struct AllocatorCreateDesc { pub device: Arc, pub debug_settings: AllocatorDebugSettings, pub allocation_sizes: AllocationSizes, } + +#[derive(Debug)] pub struct CommittedAllocationStatistics { pub num_allocations: usize, pub total_size: u64, } + +#[derive(Debug)] struct MemoryBlock { heap: Arc, size: u64, @@ -157,10 +166,12 @@ impl MemoryBlock { size: u64, heap_descriptor: &metal::HeapDescriptor, dedicated: bool, + memory_location: MemoryLocation, ) -> Result { heap_descriptor.set_size(size); let heap = Arc::new(device.new_heap(heap_descriptor)); + heap.set_label(&format!("MemoryBlock {memory_location:?}")); let sub_allocator: Box = if dedicated { Box::new(allocator::DedicatedBlockAllocator::new(size)) @@ -176,6 +187,7 @@ impl MemoryBlock { } } +#[derive(Debug)] struct MemoryType { memory_blocks: Vec>, _committed_allocations: CommittedAllocationStatistics, @@ -207,7 +219,13 @@ impl MemoryType { // Create a dedicated block for large memory allocations if size > memblock_size { - let mem_block = MemoryBlock::new(device, size, &self.heap_properties, true)?; + let mem_block = MemoryBlock::new( + device, + size, + &self.heap_properties, + true, + self.memory_location, + )?; let block_index = self.memory_blocks.iter().position(|block| block.is_none()); let block_index = match block_index { @@ -277,8 +295,13 @@ impl MemoryType { } } - let new_memory_block = - MemoryBlock::new(device, memblock_size, &self.heap_properties, false)?; + let new_memory_block = MemoryBlock::new( + device, + memblock_size, + &self.heap_properties, + false, + self.memory_location, + )?; let new_block_index = if let Some(block_index) = empty_block_index { self.memory_blocks[block_index] = Some(new_memory_block); @@ -356,14 +379,7 @@ impl MemoryType { } } -pub struct ResourceCreateDesc {} -pub struct Resource {} - impl Allocator { - pub fn device(&self) -> &metal::Device { - todo!() - } - pub fn new(desc: &AllocatorCreateDesc) -> Result { let heap_types = [ (MemoryLocation::GpuOnly, { @@ -390,7 +406,7 @@ impl Allocator { ]; let memory_types = heap_types - .iter() + .into_iter() .enumerate() .map(|(i, (memory_location, heap_descriptor))| MemoryType { memory_blocks: vec![], @@ -398,8 +414,8 @@ impl Allocator { num_allocations: 0, total_size: 0, }, - memory_location: *memory_location, - heap_properties: heap_descriptor.clone(), + memory_location, + heap_properties: heap_descriptor, memory_type_index: i, active_general_blocks: 0, }) @@ -480,13 +496,6 @@ impl Allocator { heaps } - pub fn rename_allocation(&mut self, _allocation: &mut Allocation, _name: &str) -> Result<()> { - todo!() - } - pub fn report_memory_leaks(&self, _log_level: Level) { - todo!() - } - pub fn generate_report(&self) -> AllocatorReport { let mut allocations = vec![]; let mut blocks = vec![];