Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃Ъ Clean up Metal implementation #227

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 30 additions & 21 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,6 +16,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptio
}
}

#[derive(Debug)]
pub struct Allocation {
chunk_id: Option<std::num::NonZeroU64>,
offset: u64,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -130,21 +132,28 @@ impl<'a> AllocationCreateDesc<'a> {
}
}
}

pub struct Allocator {
device: Arc<metal::Device>,
debug_settings: AllocatorDebugSettings,
memory_types: Vec<MemoryType>,
allocation_sizes: AllocationSizes,
}

#[derive(Debug)]
pub struct AllocatorCreateDesc {
MarijnS95 marked this conversation as resolved.
Show resolved Hide resolved
pub device: Arc<metal::Device>,
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<metal::Heap>,
size: u64,
Expand All @@ -157,10 +166,12 @@ impl MemoryBlock {
size: u64,
heap_descriptor: &metal::HeapDescriptor,
dedicated: bool,
memory_location: MemoryLocation,
) -> Result<Self> {
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<dyn allocator::SubAllocator> = if dedicated {
Box::new(allocator::DedicatedBlockAllocator::new(size))
Expand All @@ -176,6 +187,7 @@ impl MemoryBlock {
}
}

#[derive(Debug)]
struct MemoryType {
memory_blocks: Vec<Option<MemoryBlock>>,
_committed_allocations: CommittedAllocationStatistics,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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<Self> {
let heap_types = [
(MemoryLocation::GpuOnly, {
Expand All @@ -390,16 +406,16 @@ impl Allocator {
];

let memory_types = heap_types
.iter()
.into_iter()
.enumerate()
.map(|(i, (memory_location, heap_descriptor))| MemoryType {
memory_blocks: vec![],
_committed_allocations: CommittedAllocationStatistics {
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,
})
Expand Down Expand Up @@ -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![];
Expand Down
Loading