@@ -3,7 +3,7 @@ use log::info;
33use objc2:: rc:: Id ;
44use objc2_foundation:: NSArray ;
55use objc2_metal:: {
6- MTLCreateSystemDefaultDevice , MTLDevice as _, MTLPixelFormat ,
6+ MTLCreateSystemDefaultDevice , MTLDevice as _, MTLHeap , MTLPixelFormat ,
77 MTLPrimitiveAccelerationStructureDescriptor , MTLStorageMode , MTLTextureDescriptor ,
88} ;
99
@@ -35,7 +35,17 @@ fn main() {
3535 gpu_allocator:: MemoryLocation :: GpuOnly ,
3636 ) ;
3737 let allocation = allocator. allocate ( & allocation_desc) . unwrap ( ) ;
38- let _buffer = allocation. make_buffer ( ) . unwrap ( ) ;
38+ // SAFETY: We will only allocate objects on this heap within the returned offset and size
39+ let heap = unsafe { allocation. heap ( ) } ;
40+ let buffer = unsafe {
41+ heap. newBufferWithLength_options_offset (
42+ allocation. size ( ) as usize ,
43+ heap. resourceOptions ( ) ,
44+ allocation. offset ( ) as usize ,
45+ )
46+ }
47+ . unwrap ( ) ;
48+ drop ( buffer) ;
3949 allocator. free ( & allocation) . unwrap ( ) ;
4050 info ! ( "Allocation and deallocation of GpuOnly memory was successful." ) ;
4151 }
@@ -49,7 +59,17 @@ fn main() {
4959 gpu_allocator:: MemoryLocation :: CpuToGpu ,
5060 ) ;
5161 let allocation = allocator. allocate ( & allocation_desc) . unwrap ( ) ;
52- let _buffer = allocation. make_buffer ( ) . unwrap ( ) ;
62+ // SAFETY: We will only allocate objects on this heap within the returned offset and size
63+ let heap = unsafe { allocation. heap ( ) } ;
64+ let buffer = unsafe {
65+ heap. newBufferWithLength_options_offset (
66+ allocation. size ( ) as usize ,
67+ heap. resourceOptions ( ) ,
68+ allocation. offset ( ) as usize ,
69+ )
70+ }
71+ . unwrap ( ) ;
72+ drop ( buffer) ;
5373 allocator. free ( & allocation) . unwrap ( ) ;
5474 info ! ( "Allocation and deallocation of CpuToGpu memory was successful." ) ;
5575 }
@@ -63,7 +83,17 @@ fn main() {
6383 gpu_allocator:: MemoryLocation :: GpuToCpu ,
6484 ) ;
6585 let allocation = allocator. allocate ( & allocation_desc) . unwrap ( ) ;
66- let _buffer = allocation. make_buffer ( ) . unwrap ( ) ;
86+ // SAFETY: We will only allocate objects on this heap within the returned offset and size
87+ let heap = unsafe { allocation. heap ( ) } ;
88+ let buffer = unsafe {
89+ heap. newBufferWithLength_options_offset (
90+ allocation. size ( ) as usize ,
91+ heap. resourceOptions ( ) ,
92+ allocation. offset ( ) as usize ,
93+ )
94+ }
95+ . unwrap ( ) ;
96+ drop ( buffer) ;
6797 allocator. free ( & allocation) . unwrap ( ) ;
6898 info ! ( "Allocation and deallocation of GpuToCpu memory was successful." ) ;
6999 }
@@ -78,7 +108,13 @@ fn main() {
78108 let allocation_desc =
79109 AllocationCreateDesc :: texture ( & device, "Test allocation (Texture)" , & texture_desc) ;
80110 let allocation = allocator. allocate ( & allocation_desc) . unwrap ( ) ;
81- let _texture = allocation. make_texture ( & texture_desc) . unwrap ( ) ;
111+ // SAFETY: We will only allocate objects on this heap within the returned offset and size
112+ let heap = unsafe { allocation. heap ( ) } ;
113+ let buffer = unsafe {
114+ heap. newTextureWithDescriptor_offset ( & texture_desc, allocation. offset ( ) as usize )
115+ }
116+ . unwrap ( ) ;
117+ drop ( buffer) ;
82118 allocator. free ( & allocation) . unwrap ( ) ;
83119 info ! ( "Allocation and deallocation of Texture was successful." ) ;
84120 }
@@ -96,7 +132,16 @@ fn main() {
96132 gpu_allocator:: MemoryLocation :: GpuOnly ,
97133 ) ;
98134 let allocation = allocator. allocate ( & allocation_desc) . unwrap ( ) ;
99- let _acc_structure = allocation. make_acceleration_structure ( ) ;
135+ // SAFETY: We will only allocate objects on this heap within the returned offset and size
136+ let heap = unsafe { allocation. heap ( ) } ;
137+ let buffer = unsafe {
138+ heap. newAccelerationStructureWithSize_offset (
139+ allocation. size ( ) as usize ,
140+ allocation. offset ( ) as usize ,
141+ )
142+ }
143+ . unwrap ( ) ;
144+ drop ( buffer) ;
100145 allocator. free ( & allocation) . unwrap ( ) ;
101146 info ! ( "Allocation and deallocation of Acceleration structure was successful." ) ;
102147 }
0 commit comments