Skip to content

Commit

Permalink
metal: Qualified use cleanup
Browse files Browse the repository at this point in the history
Some items are imported in scope with `as _` and then again referenced
using fully-qualified syntax.  Clean that up, and drop the `use
objc2_metal as metal` rename while at it.
  • Loading branch information
MarijnS95 committed Oct 3, 2024
1 parent 031b39d commit ced63e1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 55 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ allocator.free(allocation).unwrap();

```rust
use gpu_allocator::metal::*;
use objc2_metal as metal;
let mut allocator = Allocator::new(&AllocatorCreateDesc {
device: device.clone(),
debug_settings: Default::default(),
Expand All @@ -146,7 +145,6 @@ let mut allocator = Allocator::new(&AllocatorCreateDesc {
```rust
use gpu_allocator::metal::*;
use gpu_allocator::MemoryLocation;
use objc2_metal as metal;
let allocation_desc = AllocationCreateDesc::buffer(
&device,
"Example allocation",
Expand Down
16 changes: 9 additions & 7 deletions examples/metal-buffer.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use gpu_allocator::metal::{AllocationCreateDesc, Allocator, AllocatorCreateDesc};
use log::info;
use metal::MTLDevice as _;
use objc2::rc::Id;
use objc2_foundation::NSArray;
use objc2_metal as metal;
use objc2_metal::{
MTLCreateSystemDefaultDevice, MTLDevice as _, MTLPixelFormat,
MTLPrimitiveAccelerationStructureDescriptor, MTLStorageMode, MTLTextureDescriptor,
};

fn main() {
env_logger::Builder::from_env(env_logger::Env::default().default_filter_or("trace")).init();
Expand All @@ -14,7 +16,7 @@ fn main() {
extern "C" {}

let device =
unsafe { Id::from_raw(metal::MTLCreateSystemDefaultDevice()) }.expect("No MTLDevice found");
unsafe { Id::from_raw(MTLCreateSystemDefaultDevice()) }.expect("No MTLDevice found");

// Setting up the allocator
let mut allocator = Allocator::new(&AllocatorCreateDesc {
Expand Down Expand Up @@ -68,11 +70,11 @@ fn main() {

// Test allocating texture
{
let texture_desc = unsafe { metal::MTLTextureDescriptor::new() };
texture_desc.setPixelFormat(metal::MTLPixelFormat::RGBA8Unorm);
let texture_desc = unsafe { MTLTextureDescriptor::new() };
texture_desc.setPixelFormat(MTLPixelFormat::RGBA8Unorm);
unsafe { texture_desc.setWidth(64) };
unsafe { texture_desc.setHeight(64) };
texture_desc.setStorageMode(metal::MTLStorageMode::Private);
texture_desc.setStorageMode(MTLStorageMode::Private);
let allocation_desc =
AllocationCreateDesc::texture(&device, "Test allocation (Texture)", &texture_desc);
let allocation = allocator.allocate(&allocation_desc).unwrap();
Expand All @@ -84,7 +86,7 @@ fn main() {
// Test allocating acceleration structure
{
let empty_array = NSArray::from_slice(&[]);
let acc_desc = metal::MTLPrimitiveAccelerationStructureDescriptor::descriptor();
let acc_desc = MTLPrimitiveAccelerationStructureDescriptor::descriptor();
acc_desc.setGeometryDescriptors(Some(&empty_array));
let sizes = device.accelerationStructureSizesWithDescriptor(&acc_desc);
let allocation_desc = AllocationCreateDesc::acceleration_structure_with_size(
Expand Down
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,7 @@
//! # fn main() {
//! use gpu_allocator::metal::*;
//! # use objc2::rc::Id;
//! use objc2_metal as metal;
//! # let device = unsafe { metal::MTLCreateSystemDefaultDevice() };
//! # let device = unsafe { objc2_metal::MTLCreateSystemDefaultDevice() };
//! # let device = unsafe { Id::from_raw(device) }.expect("No MTLDevice found");
//! let mut allocator = Allocator::new(&AllocatorCreateDesc {
//! device: device.clone(),
Expand All @@ -183,8 +182,7 @@
//! use gpu_allocator::metal::*;
//! use gpu_allocator::MemoryLocation;
//! # use objc2::rc::Id;
//! use objc2_metal as metal;
//! # let device = unsafe { metal::MTLCreateSystemDefaultDevice() };
//! # let device = unsafe { objc2_metal::MTLCreateSystemDefaultDevice() };
//! # let device = unsafe { Id::from_raw(device) }.expect("No MTLDevice found");
//! # let mut allocator = Allocator::new(&AllocatorCreateDesc {
//! # device: device.clone(),
Expand Down
85 changes: 43 additions & 42 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
use std::{backtrace::Backtrace, sync::Arc};

use log::debug;
use metal::{MTLDevice as _, MTLHeap as _, MTLResource as _};
use objc2::{rc::Retained, runtime::ProtocolObject};
use objc2_foundation::NSString;
use objc2_metal as metal;
use objc2_metal::{
MTLAccelerationStructure, MTLBuffer, MTLCPUCacheMode, MTLDevice, MTLHeap, MTLHeapDescriptor,
MTLHeapType, MTLResource, MTLResourceOptions, MTLStorageMode, MTLTexture, MTLTextureDescriptor,
};

use crate::{
allocator::{self, AllocatorReport, MemoryBlockReport},
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
};

fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions {
fn memory_location_to_metal(location: MemoryLocation) -> MTLResourceOptions {
match location {
MemoryLocation::GpuOnly => metal::MTLResourceOptions::MTLResourceStorageModePrivate,
MemoryLocation::GpuOnly => MTLResourceOptions::MTLResourceStorageModePrivate,
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu | MemoryLocation::Unknown => {
metal::MTLResourceOptions::MTLResourceStorageModeShared
MTLResourceOptions::MTLResourceStorageModeShared
}
}
}
Expand All @@ -27,16 +29,16 @@ pub struct Allocation {
size: u64,
memory_block_index: usize,
memory_type_index: usize,
heap: Retained<ProtocolObject<dyn metal::MTLHeap>>,
heap: Retained<ProtocolObject<dyn MTLHeap>>,
name: Option<Box<str>>,
}

impl Allocation {
pub fn heap(&self) -> &ProtocolObject<dyn metal::MTLHeap> {
pub fn heap(&self) -> &ProtocolObject<dyn MTLHeap> {
&self.heap
}

pub fn make_buffer(&self) -> Option<Retained<ProtocolObject<dyn metal::MTLBuffer>>> {
pub fn make_buffer(&self) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>> {
let resource = unsafe {
self.heap.newBufferWithLength_options_offset(
self.size as usize,
Expand All @@ -54,8 +56,8 @@ impl Allocation {

pub fn make_texture(
&self,
desc: &metal::MTLTextureDescriptor,
) -> Option<Retained<ProtocolObject<dyn metal::MTLTexture>>> {
desc: &MTLTextureDescriptor,
) -> Option<Retained<ProtocolObject<dyn MTLTexture>>> {
let resource = unsafe {
self.heap
.newTextureWithDescriptor_offset(desc, self.offset as usize)
Expand All @@ -70,7 +72,7 @@ impl Allocation {

pub fn make_acceleration_structure(
&self,
) -> Option<Retained<ProtocolObject<dyn metal::MTLAccelerationStructure>>> {
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>> {
let resource = unsafe {
self.heap
.newAccelerationStructureWithSize_offset(self.size as usize, self.offset as usize)
Expand Down Expand Up @@ -100,7 +102,7 @@ pub struct AllocationCreateDesc<'a> {

impl<'a> AllocationCreateDesc<'a> {
pub fn buffer(
device: &ProtocolObject<dyn metal::MTLDevice>,
device: &ProtocolObject<dyn MTLDevice>,
name: &'a str,
length: u64,
location: MemoryLocation,
Expand All @@ -118,27 +120,27 @@ impl<'a> AllocationCreateDesc<'a> {
}

pub fn texture(
device: &ProtocolObject<dyn metal::MTLDevice>,
device: &ProtocolObject<dyn MTLDevice>,
name: &'a str,
desc: &metal::MTLTextureDescriptor,
desc: &MTLTextureDescriptor,
) -> Self {
let size_and_align = device.heapTextureSizeAndAlignWithDescriptor(desc);
Self {
name,
location: match desc.storageMode() {
metal::MTLStorageMode::Shared
| metal::MTLStorageMode::Managed
| metal::MTLStorageMode::Memoryless => MemoryLocation::Unknown,
metal::MTLStorageMode::Private => MemoryLocation::GpuOnly,
metal::MTLStorageMode(mode /* @ 4.. */) => todo!("Unknown storage mode {mode}"),
MTLStorageMode::Shared | MTLStorageMode::Managed | MTLStorageMode::Memoryless => {
MemoryLocation::Unknown
}
MTLStorageMode::Private => MemoryLocation::GpuOnly,
MTLStorageMode(mode /* @ 4.. */) => todo!("Unknown storage mode {mode}"),
},
size: size_and_align.size as u64,
alignment: size_and_align.align as u64,
}
}

pub fn acceleration_structure_with_size(
device: &ProtocolObject<dyn metal::MTLDevice>,
device: &ProtocolObject<dyn MTLDevice>,
name: &'a str,
size: u64, // TODO: usize
location: MemoryLocation,
Expand All @@ -157,15 +159,15 @@ impl<'a> AllocationCreateDesc<'a> {
}

pub struct Allocator {
device: Retained<ProtocolObject<dyn metal::MTLDevice>>,
device: Retained<ProtocolObject<dyn MTLDevice>>,
debug_settings: AllocatorDebugSettings,
memory_types: Vec<MemoryType>,
allocation_sizes: AllocationSizes,
}

#[derive(Debug)]
pub struct AllocatorCreateDesc {
pub device: Retained<ProtocolObject<dyn metal::MTLDevice>>,
pub device: Retained<ProtocolObject<dyn MTLDevice>>,
pub debug_settings: AllocatorDebugSettings,
pub allocation_sizes: AllocationSizes,
}
Expand All @@ -178,16 +180,16 @@ pub struct CommittedAllocationStatistics {

#[derive(Debug)]
struct MemoryBlock {
heap: Retained<ProtocolObject<dyn metal::MTLHeap>>,
heap: Retained<ProtocolObject<dyn MTLHeap>>,
size: u64,
sub_allocator: Box<dyn allocator::SubAllocator>,
}

impl MemoryBlock {
fn new(
device: &ProtocolObject<dyn metal::MTLDevice>,
device: &ProtocolObject<dyn MTLDevice>,
size: u64,
heap_descriptor: &metal::MTLHeapDescriptor,
heap_descriptor: &MTLHeapDescriptor,
dedicated: bool,
memory_location: MemoryLocation,
) -> Result<Self> {
Expand Down Expand Up @@ -220,23 +222,22 @@ struct MemoryType {
memory_blocks: Vec<Option<MemoryBlock>>,
_committed_allocations: CommittedAllocationStatistics,
memory_location: MemoryLocation,
heap_properties: Retained<metal::MTLHeapDescriptor>,
heap_properties: Retained<MTLHeapDescriptor>,
memory_type_index: usize,
active_general_blocks: usize,
}

impl MemoryType {
fn allocate(
&mut self,
device: &ProtocolObject<dyn metal::MTLDevice>,
device: &ProtocolObject<dyn MTLDevice>,
desc: &AllocationCreateDesc<'_>,
backtrace: Arc<Backtrace>,
allocation_sizes: &AllocationSizes,
) -> Result<Allocation> {
let allocation_type = allocator::AllocationType::Linear;

let memblock_size = if self.heap_properties.storageMode() == metal::MTLStorageMode::Private
{
let memblock_size = if self.heap_properties.storageMode() == MTLStorageMode::Private {
allocation_sizes.device_memblock_size
} else {
allocation_sizes.host_memblock_size
Expand Down Expand Up @@ -411,24 +412,24 @@ impl Allocator {
pub fn new(desc: &AllocatorCreateDesc) -> Result<Self> {
let heap_types = [
(MemoryLocation::GpuOnly, {
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::DefaultCache);
heap_desc.setStorageMode(metal::MTLStorageMode::Private);
heap_desc.setType(metal::MTLHeapType::Placement);
let heap_desc = unsafe { MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(MTLCPUCacheMode::DefaultCache);
heap_desc.setStorageMode(MTLStorageMode::Private);
heap_desc.setType(MTLHeapType::Placement);
heap_desc
}),
(MemoryLocation::CpuToGpu, {
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::WriteCombined);
heap_desc.setStorageMode(metal::MTLStorageMode::Shared);
heap_desc.setType(metal::MTLHeapType::Placement);
let heap_desc = unsafe { MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(MTLCPUCacheMode::WriteCombined);
heap_desc.setStorageMode(MTLStorageMode::Shared);
heap_desc.setType(MTLHeapType::Placement);
heap_desc
}),
(MemoryLocation::GpuToCpu, {
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::DefaultCache);
heap_desc.setStorageMode(metal::MTLStorageMode::Shared);
heap_desc.setType(metal::MTLHeapType::Placement);
let heap_desc = unsafe { MTLHeapDescriptor::new() };
heap_desc.setCpuCacheMode(MTLCPUCacheMode::DefaultCache);
heap_desc.setStorageMode(MTLStorageMode::Shared);
heap_desc.setType(MTLHeapType::Placement);
heap_desc
}),
];
Expand Down Expand Up @@ -514,7 +515,7 @@ impl Allocator {
}

/// Returns heaps for all memory blocks
pub fn heaps(&self) -> impl Iterator<Item = &ProtocolObject<dyn metal::MTLHeap>> {
pub fn heaps(&self) -> impl Iterator<Item = &ProtocolObject<dyn MTLHeap>> {
self.memory_types.iter().flat_map(|memory_type| {
memory_type
.memory_blocks
Expand Down

0 comments on commit ced63e1

Please sign in to comment.