@@ -6,21 +6,23 @@ mod visualizer;
6
6
pub use visualizer:: AllocatorVisualizer ;
7
7
8
8
use log:: debug;
9
- use metal:: { MTLDevice as _, MTLHeap as _, MTLResource as _} ;
10
9
use objc2:: { rc:: Retained , runtime:: ProtocolObject } ;
11
10
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
+ } ;
13
15
14
16
use crate :: {
15
17
allocator:: { self , AllocatorReport , MemoryBlockReport } ,
16
18
AllocationError , AllocationSizes , AllocatorDebugSettings , MemoryLocation , Result ,
17
19
} ;
18
20
19
- fn memory_location_to_metal ( location : MemoryLocation ) -> metal :: MTLResourceOptions {
21
+ fn memory_location_to_metal ( location : MemoryLocation ) -> MTLResourceOptions {
20
22
match location {
21
- MemoryLocation :: GpuOnly => metal :: MTLResourceOptions :: MTLResourceStorageModePrivate ,
23
+ MemoryLocation :: GpuOnly => MTLResourceOptions :: MTLResourceStorageModePrivate ,
22
24
MemoryLocation :: CpuToGpu | MemoryLocation :: GpuToCpu | MemoryLocation :: Unknown => {
23
- metal :: MTLResourceOptions :: MTLResourceStorageModeShared
25
+ MTLResourceOptions :: MTLResourceStorageModeShared
24
26
}
25
27
}
26
28
}
@@ -32,16 +34,16 @@ pub struct Allocation {
32
34
size : u64 ,
33
35
memory_block_index : usize ,
34
36
memory_type_index : usize ,
35
- heap : Retained < ProtocolObject < dyn metal :: MTLHeap > > ,
37
+ heap : Retained < ProtocolObject < dyn MTLHeap > > ,
36
38
name : Option < Box < str > > ,
37
39
}
38
40
39
41
impl Allocation {
40
- pub fn heap ( & self ) -> & ProtocolObject < dyn metal :: MTLHeap > {
42
+ pub fn heap ( & self ) -> & ProtocolObject < dyn MTLHeap > {
41
43
& self . heap
42
44
}
43
45
44
- pub fn make_buffer ( & self ) -> Option < Retained < ProtocolObject < dyn metal :: MTLBuffer > > > {
46
+ pub fn make_buffer ( & self ) -> Option < Retained < ProtocolObject < dyn MTLBuffer > > > {
45
47
let resource = unsafe {
46
48
self . heap . newBufferWithLength_options_offset (
47
49
self . size as usize ,
@@ -59,8 +61,8 @@ impl Allocation {
59
61
60
62
pub fn make_texture (
61
63
& self ,
62
- desc : & metal :: MTLTextureDescriptor ,
63
- ) -> Option < Retained < ProtocolObject < dyn metal :: MTLTexture > > > {
64
+ desc : & MTLTextureDescriptor ,
65
+ ) -> Option < Retained < ProtocolObject < dyn MTLTexture > > > {
64
66
let resource = unsafe {
65
67
self . heap
66
68
. newTextureWithDescriptor_offset ( desc, self . offset as usize )
@@ -75,7 +77,7 @@ impl Allocation {
75
77
76
78
pub fn make_acceleration_structure (
77
79
& self ,
78
- ) -> Option < Retained < ProtocolObject < dyn metal :: MTLAccelerationStructure > > > {
80
+ ) -> Option < Retained < ProtocolObject < dyn MTLAccelerationStructure > > > {
79
81
let resource = unsafe {
80
82
self . heap
81
83
. newAccelerationStructureWithSize_offset ( self . size as usize , self . offset as usize )
@@ -105,7 +107,7 @@ pub struct AllocationCreateDesc<'a> {
105
107
106
108
impl < ' a > AllocationCreateDesc < ' a > {
107
109
pub fn buffer (
108
- device : & ProtocolObject < dyn metal :: MTLDevice > ,
110
+ device : & ProtocolObject < dyn MTLDevice > ,
109
111
name : & ' a str ,
110
112
length : u64 ,
111
113
location : MemoryLocation ,
@@ -123,27 +125,27 @@ impl<'a> AllocationCreateDesc<'a> {
123
125
}
124
126
125
127
pub fn texture (
126
- device : & ProtocolObject < dyn metal :: MTLDevice > ,
128
+ device : & ProtocolObject < dyn MTLDevice > ,
127
129
name : & ' a str ,
128
- desc : & metal :: MTLTextureDescriptor ,
130
+ desc : & MTLTextureDescriptor ,
129
131
) -> Self {
130
132
let size_and_align = device. heapTextureSizeAndAlignWithDescriptor ( desc) ;
131
133
Self {
132
134
name,
133
135
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}" ) ,
139
141
} ,
140
142
size : size_and_align. size as u64 ,
141
143
alignment : size_and_align. align as u64 ,
142
144
}
143
145
}
144
146
145
147
pub fn acceleration_structure_with_size (
146
- device : & ProtocolObject < dyn metal :: MTLDevice > ,
148
+ device : & ProtocolObject < dyn MTLDevice > ,
147
149
name : & ' a str ,
148
150
size : u64 , // TODO: usize
149
151
location : MemoryLocation ,
@@ -162,7 +164,7 @@ impl<'a> AllocationCreateDesc<'a> {
162
164
}
163
165
164
166
pub struct Allocator {
165
- device : Retained < ProtocolObject < dyn metal :: MTLDevice > > ,
167
+ device : Retained < ProtocolObject < dyn MTLDevice > > ,
166
168
debug_settings : AllocatorDebugSettings ,
167
169
memory_types : Vec < MemoryType > ,
168
170
allocation_sizes : AllocationSizes ,
@@ -176,7 +178,7 @@ impl std::fmt::Debug for Allocator {
176
178
177
179
#[ derive( Debug ) ]
178
180
pub struct AllocatorCreateDesc {
179
- pub device : Retained < ProtocolObject < dyn metal :: MTLDevice > > ,
181
+ pub device : Retained < ProtocolObject < dyn MTLDevice > > ,
180
182
pub debug_settings : AllocatorDebugSettings ,
181
183
pub allocation_sizes : AllocationSizes ,
182
184
}
@@ -189,16 +191,16 @@ pub struct CommittedAllocationStatistics {
189
191
190
192
#[ derive( Debug ) ]
191
193
struct MemoryBlock {
192
- heap : Retained < ProtocolObject < dyn metal :: MTLHeap > > ,
194
+ heap : Retained < ProtocolObject < dyn MTLHeap > > ,
193
195
size : u64 ,
194
196
sub_allocator : Box < dyn allocator:: SubAllocator > ,
195
197
}
196
198
197
199
impl MemoryBlock {
198
200
fn new (
199
- device : & ProtocolObject < dyn metal :: MTLDevice > ,
201
+ device : & ProtocolObject < dyn MTLDevice > ,
200
202
size : u64 ,
201
- heap_descriptor : & metal :: MTLHeapDescriptor ,
203
+ heap_descriptor : & MTLHeapDescriptor ,
202
204
dedicated : bool ,
203
205
memory_location : MemoryLocation ,
204
206
) -> Result < Self > {
@@ -231,23 +233,22 @@ struct MemoryType {
231
233
memory_blocks : Vec < Option < MemoryBlock > > ,
232
234
_committed_allocations : CommittedAllocationStatistics ,
233
235
memory_location : MemoryLocation ,
234
- heap_properties : Retained < metal :: MTLHeapDescriptor > ,
236
+ heap_properties : Retained < MTLHeapDescriptor > ,
235
237
memory_type_index : usize ,
236
238
active_general_blocks : usize ,
237
239
}
238
240
239
241
impl MemoryType {
240
242
fn allocate (
241
243
& mut self ,
242
- device : & ProtocolObject < dyn metal :: MTLDevice > ,
244
+ device : & ProtocolObject < dyn MTLDevice > ,
243
245
desc : & AllocationCreateDesc < ' _ > ,
244
246
backtrace : Arc < Backtrace > ,
245
247
allocation_sizes : & AllocationSizes ,
246
248
) -> Result < Allocation > {
247
249
let allocation_type = allocator:: AllocationType :: Linear ;
248
250
249
- let memblock_size = if self . heap_properties . storageMode ( ) == metal:: MTLStorageMode :: Private
250
- {
251
+ let memblock_size = if self . heap_properties . storageMode ( ) == MTLStorageMode :: Private {
251
252
allocation_sizes. device_memblock_size
252
253
} else {
253
254
allocation_sizes. host_memblock_size
@@ -422,24 +423,24 @@ impl Allocator {
422
423
pub fn new ( desc : & AllocatorCreateDesc ) -> Result < Self > {
423
424
let heap_types = [
424
425
( 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 ) ;
429
430
heap_desc
430
431
} ) ,
431
432
( 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 ) ;
436
437
heap_desc
437
438
} ) ,
438
439
( 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 ) ;
443
444
heap_desc
444
445
} ) ,
445
446
] ;
@@ -525,7 +526,7 @@ impl Allocator {
525
526
}
526
527
527
528
/// 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 > > {
529
530
self . memory_types . iter ( ) . flat_map ( |memory_type| {
530
531
memory_type
531
532
. memory_blocks
0 commit comments