Skip to content

Commit 222b3d8

Browse files
authored
✂️ metal: Qualified use cleanup (#250)
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.
1 parent 5b3341b commit 222b3d8

File tree

4 files changed

+54
-55
lines changed

4 files changed

+54
-55
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ allocator.free(allocation).unwrap();
134134

135135
```rust
136136
use gpu_allocator::metal::*;
137-
use objc2_metal as metal;
138137
let mut allocator = Allocator::new(&AllocatorCreateDesc {
139138
device: device.clone(),
140139
debug_settings: Default::default(),
@@ -146,7 +145,6 @@ let mut allocator = Allocator::new(&AllocatorCreateDesc {
146145
```rust
147146
use gpu_allocator::metal::*;
148147
use gpu_allocator::MemoryLocation;
149-
use objc2_metal as metal;
150148
let allocation_desc = AllocationCreateDesc::buffer(
151149
&device,
152150
"Example allocation",

examples/metal-buffer.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
use gpu_allocator::metal::{AllocationCreateDesc, Allocator, AllocatorCreateDesc};
22
use log::info;
3-
use metal::MTLDevice as _;
43
use objc2::rc::Id;
54
use objc2_foundation::NSArray;
6-
use objc2_metal as metal;
5+
use objc2_metal::{
6+
MTLCreateSystemDefaultDevice, MTLDevice as _, MTLPixelFormat,
7+
MTLPrimitiveAccelerationStructureDescriptor, MTLStorageMode, MTLTextureDescriptor,
8+
};
79

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

1618
let device =
17-
unsafe { Id::from_raw(metal::MTLCreateSystemDefaultDevice()) }.expect("No MTLDevice found");
19+
unsafe { Id::from_raw(MTLCreateSystemDefaultDevice()) }.expect("No MTLDevice found");
1820

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

6971
// Test allocating texture
7072
{
71-
let texture_desc = unsafe { metal::MTLTextureDescriptor::new() };
72-
texture_desc.setPixelFormat(metal::MTLPixelFormat::RGBA8Unorm);
73+
let texture_desc = unsafe { MTLTextureDescriptor::new() };
74+
texture_desc.setPixelFormat(MTLPixelFormat::RGBA8Unorm);
7375
unsafe { texture_desc.setWidth(64) };
7476
unsafe { texture_desc.setHeight(64) };
75-
texture_desc.setStorageMode(metal::MTLStorageMode::Private);
77+
texture_desc.setStorageMode(MTLStorageMode::Private);
7678
let allocation_desc =
7779
AllocationCreateDesc::texture(&device, "Test allocation (Texture)", &texture_desc);
7880
let allocation = allocator.allocate(&allocation_desc).unwrap();
@@ -84,7 +86,7 @@ fn main() {
8486
// Test allocating acceleration structure
8587
{
8688
let empty_array = NSArray::from_slice(&[]);
87-
let acc_desc = metal::MTLPrimitiveAccelerationStructureDescriptor::descriptor();
89+
let acc_desc = MTLPrimitiveAccelerationStructureDescriptor::descriptor();
8890
acc_desc.setGeometryDescriptors(Some(&empty_array));
8991
let sizes = device.accelerationStructureSizesWithDescriptor(&acc_desc);
9092
let allocation_desc = AllocationCreateDesc::acceleration_structure_with_size(

src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,7 @@
163163
//! # fn main() {
164164
//! use gpu_allocator::metal::*;
165165
//! # use objc2::rc::Id;
166-
//! use objc2_metal as metal;
167-
//! # let device = unsafe { metal::MTLCreateSystemDefaultDevice() };
166+
//! # let device = unsafe { objc2_metal::MTLCreateSystemDefaultDevice() };
168167
//! # let device = unsafe { Id::from_raw(device) }.expect("No MTLDevice found");
169168
//! let mut allocator = Allocator::new(&AllocatorCreateDesc {
170169
//! device: device.clone(),
@@ -183,8 +182,7 @@
183182
//! use gpu_allocator::metal::*;
184183
//! use gpu_allocator::MemoryLocation;
185184
//! # use objc2::rc::Id;
186-
//! use objc2_metal as metal;
187-
//! # let device = unsafe { metal::MTLCreateSystemDefaultDevice() };
185+
//! # let device = unsafe { objc2_metal::MTLCreateSystemDefaultDevice() };
188186
//! # let device = unsafe { Id::from_raw(device) }.expect("No MTLDevice found");
189187
//! # let mut allocator = Allocator::new(&AllocatorCreateDesc {
190188
//! # device: device.clone(),

src/metal/mod.rs

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,23 @@ mod visualizer;
66
pub use visualizer::AllocatorVisualizer;
77

88
use log::debug;
9-
use metal::{MTLDevice as _, MTLHeap as _, MTLResource as _};
109
use objc2::{rc::Retained, runtime::ProtocolObject};
1110
use objc2_foundation::NSString;
12-
use objc2_metal as metal;
11+
use objc2_metal::{
12+
MTLAccelerationStructure, MTLBuffer, MTLCPUCacheMode, MTLDevice, MTLHeap, MTLHeapDescriptor,
13+
MTLHeapType, MTLResource, MTLResourceOptions, MTLStorageMode, MTLTexture, MTLTextureDescriptor,
14+
};
1315

1416
use crate::{
1517
allocator::{self, AllocatorReport, MemoryBlockReport},
1618
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
1719
};
1820

19-
fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions {
21+
fn memory_location_to_metal(location: MemoryLocation) -> MTLResourceOptions {
2022
match location {
21-
MemoryLocation::GpuOnly => metal::MTLResourceOptions::MTLResourceStorageModePrivate,
23+
MemoryLocation::GpuOnly => MTLResourceOptions::MTLResourceStorageModePrivate,
2224
MemoryLocation::CpuToGpu | MemoryLocation::GpuToCpu | MemoryLocation::Unknown => {
23-
metal::MTLResourceOptions::MTLResourceStorageModeShared
25+
MTLResourceOptions::MTLResourceStorageModeShared
2426
}
2527
}
2628
}
@@ -32,16 +34,16 @@ pub struct Allocation {
3234
size: u64,
3335
memory_block_index: usize,
3436
memory_type_index: usize,
35-
heap: Retained<ProtocolObject<dyn metal::MTLHeap>>,
37+
heap: Retained<ProtocolObject<dyn MTLHeap>>,
3638
name: Option<Box<str>>,
3739
}
3840

3941
impl Allocation {
40-
pub fn heap(&self) -> &ProtocolObject<dyn metal::MTLHeap> {
42+
pub fn heap(&self) -> &ProtocolObject<dyn MTLHeap> {
4143
&self.heap
4244
}
4345

44-
pub fn make_buffer(&self) -> Option<Retained<ProtocolObject<dyn metal::MTLBuffer>>> {
46+
pub fn make_buffer(&self) -> Option<Retained<ProtocolObject<dyn MTLBuffer>>> {
4547
let resource = unsafe {
4648
self.heap.newBufferWithLength_options_offset(
4749
self.size as usize,
@@ -59,8 +61,8 @@ impl Allocation {
5961

6062
pub fn make_texture(
6163
&self,
62-
desc: &metal::MTLTextureDescriptor,
63-
) -> Option<Retained<ProtocolObject<dyn metal::MTLTexture>>> {
64+
desc: &MTLTextureDescriptor,
65+
) -> Option<Retained<ProtocolObject<dyn MTLTexture>>> {
6466
let resource = unsafe {
6567
self.heap
6668
.newTextureWithDescriptor_offset(desc, self.offset as usize)
@@ -75,7 +77,7 @@ impl Allocation {
7577

7678
pub fn make_acceleration_structure(
7779
&self,
78-
) -> Option<Retained<ProtocolObject<dyn metal::MTLAccelerationStructure>>> {
80+
) -> Option<Retained<ProtocolObject<dyn MTLAccelerationStructure>>> {
7981
let resource = unsafe {
8082
self.heap
8183
.newAccelerationStructureWithSize_offset(self.size as usize, self.offset as usize)
@@ -105,7 +107,7 @@ pub struct AllocationCreateDesc<'a> {
105107

106108
impl<'a> AllocationCreateDesc<'a> {
107109
pub fn buffer(
108-
device: &ProtocolObject<dyn metal::MTLDevice>,
110+
device: &ProtocolObject<dyn MTLDevice>,
109111
name: &'a str,
110112
length: u64,
111113
location: MemoryLocation,
@@ -123,27 +125,27 @@ impl<'a> AllocationCreateDesc<'a> {
123125
}
124126

125127
pub fn texture(
126-
device: &ProtocolObject<dyn metal::MTLDevice>,
128+
device: &ProtocolObject<dyn MTLDevice>,
127129
name: &'a str,
128-
desc: &metal::MTLTextureDescriptor,
130+
desc: &MTLTextureDescriptor,
129131
) -> Self {
130132
let size_and_align = device.heapTextureSizeAndAlignWithDescriptor(desc);
131133
Self {
132134
name,
133135
location: match desc.storageMode() {
134-
metal::MTLStorageMode::Shared
135-
| metal::MTLStorageMode::Managed
136-
| metal::MTLStorageMode::Memoryless => MemoryLocation::Unknown,
137-
metal::MTLStorageMode::Private => MemoryLocation::GpuOnly,
138-
metal::MTLStorageMode(mode /* @ 4.. */) => todo!("Unknown storage mode {mode}"),
136+
MTLStorageMode::Shared | MTLStorageMode::Managed | MTLStorageMode::Memoryless => {
137+
MemoryLocation::Unknown
138+
}
139+
MTLStorageMode::Private => MemoryLocation::GpuOnly,
140+
MTLStorageMode(mode /* @ 4.. */) => todo!("Unknown storage mode {mode}"),
139141
},
140142
size: size_and_align.size as u64,
141143
alignment: size_and_align.align as u64,
142144
}
143145
}
144146

145147
pub fn acceleration_structure_with_size(
146-
device: &ProtocolObject<dyn metal::MTLDevice>,
148+
device: &ProtocolObject<dyn MTLDevice>,
147149
name: &'a str,
148150
size: u64, // TODO: usize
149151
location: MemoryLocation,
@@ -162,7 +164,7 @@ impl<'a> AllocationCreateDesc<'a> {
162164
}
163165

164166
pub struct Allocator {
165-
device: Retained<ProtocolObject<dyn metal::MTLDevice>>,
167+
device: Retained<ProtocolObject<dyn MTLDevice>>,
166168
debug_settings: AllocatorDebugSettings,
167169
memory_types: Vec<MemoryType>,
168170
allocation_sizes: AllocationSizes,
@@ -176,7 +178,7 @@ impl std::fmt::Debug for Allocator {
176178

177179
#[derive(Debug)]
178180
pub struct AllocatorCreateDesc {
179-
pub device: Retained<ProtocolObject<dyn metal::MTLDevice>>,
181+
pub device: Retained<ProtocolObject<dyn MTLDevice>>,
180182
pub debug_settings: AllocatorDebugSettings,
181183
pub allocation_sizes: AllocationSizes,
182184
}
@@ -189,16 +191,16 @@ pub struct CommittedAllocationStatistics {
189191

190192
#[derive(Debug)]
191193
struct MemoryBlock {
192-
heap: Retained<ProtocolObject<dyn metal::MTLHeap>>,
194+
heap: Retained<ProtocolObject<dyn MTLHeap>>,
193195
size: u64,
194196
sub_allocator: Box<dyn allocator::SubAllocator>,
195197
}
196198

197199
impl MemoryBlock {
198200
fn new(
199-
device: &ProtocolObject<dyn metal::MTLDevice>,
201+
device: &ProtocolObject<dyn MTLDevice>,
200202
size: u64,
201-
heap_descriptor: &metal::MTLHeapDescriptor,
203+
heap_descriptor: &MTLHeapDescriptor,
202204
dedicated: bool,
203205
memory_location: MemoryLocation,
204206
) -> Result<Self> {
@@ -231,23 +233,22 @@ struct MemoryType {
231233
memory_blocks: Vec<Option<MemoryBlock>>,
232234
_committed_allocations: CommittedAllocationStatistics,
233235
memory_location: MemoryLocation,
234-
heap_properties: Retained<metal::MTLHeapDescriptor>,
236+
heap_properties: Retained<MTLHeapDescriptor>,
235237
memory_type_index: usize,
236238
active_general_blocks: usize,
237239
}
238240

239241
impl MemoryType {
240242
fn allocate(
241243
&mut self,
242-
device: &ProtocolObject<dyn metal::MTLDevice>,
244+
device: &ProtocolObject<dyn MTLDevice>,
243245
desc: &AllocationCreateDesc<'_>,
244246
backtrace: Arc<Backtrace>,
245247
allocation_sizes: &AllocationSizes,
246248
) -> Result<Allocation> {
247249
let allocation_type = allocator::AllocationType::Linear;
248250

249-
let memblock_size = if self.heap_properties.storageMode() == metal::MTLStorageMode::Private
250-
{
251+
let memblock_size = if self.heap_properties.storageMode() == MTLStorageMode::Private {
251252
allocation_sizes.device_memblock_size
252253
} else {
253254
allocation_sizes.host_memblock_size
@@ -422,24 +423,24 @@ impl Allocator {
422423
pub fn new(desc: &AllocatorCreateDesc) -> Result<Self> {
423424
let heap_types = [
424425
(MemoryLocation::GpuOnly, {
425-
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
426-
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::DefaultCache);
427-
heap_desc.setStorageMode(metal::MTLStorageMode::Private);
428-
heap_desc.setType(metal::MTLHeapType::Placement);
426+
let heap_desc = unsafe { MTLHeapDescriptor::new() };
427+
heap_desc.setCpuCacheMode(MTLCPUCacheMode::DefaultCache);
428+
heap_desc.setStorageMode(MTLStorageMode::Private);
429+
heap_desc.setType(MTLHeapType::Placement);
429430
heap_desc
430431
}),
431432
(MemoryLocation::CpuToGpu, {
432-
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
433-
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::WriteCombined);
434-
heap_desc.setStorageMode(metal::MTLStorageMode::Shared);
435-
heap_desc.setType(metal::MTLHeapType::Placement);
433+
let heap_desc = unsafe { MTLHeapDescriptor::new() };
434+
heap_desc.setCpuCacheMode(MTLCPUCacheMode::WriteCombined);
435+
heap_desc.setStorageMode(MTLStorageMode::Shared);
436+
heap_desc.setType(MTLHeapType::Placement);
436437
heap_desc
437438
}),
438439
(MemoryLocation::GpuToCpu, {
439-
let heap_desc = unsafe { metal::MTLHeapDescriptor::new() };
440-
heap_desc.setCpuCacheMode(metal::MTLCPUCacheMode::DefaultCache);
441-
heap_desc.setStorageMode(metal::MTLStorageMode::Shared);
442-
heap_desc.setType(metal::MTLHeapType::Placement);
440+
let heap_desc = unsafe { MTLHeapDescriptor::new() };
441+
heap_desc.setCpuCacheMode(MTLCPUCacheMode::DefaultCache);
442+
heap_desc.setStorageMode(MTLStorageMode::Shared);
443+
heap_desc.setType(MTLHeapType::Placement);
443444
heap_desc
444445
}),
445446
];
@@ -525,7 +526,7 @@ impl Allocator {
525526
}
526527

527528
/// Returns heaps for all memory blocks
528-
pub fn heaps(&self) -> impl Iterator<Item = &ProtocolObject<dyn metal::MTLHeap>> {
529+
pub fn heaps(&self) -> impl Iterator<Item = &ProtocolObject<dyn MTLHeap>> {
529530
self.memory_types.iter().flat_map(|memory_type| {
530531
memory_type
531532
.memory_blocks

0 commit comments

Comments
 (0)