@@ -3,7 +3,7 @@ use log::info;
3
3
use objc2:: rc:: Id ;
4
4
use objc2_foundation:: NSArray ;
5
5
use objc2_metal:: {
6
- MTLCreateSystemDefaultDevice , MTLDevice as _, MTLPixelFormat ,
6
+ MTLCreateSystemDefaultDevice , MTLDevice as _, MTLHeap , MTLPixelFormat ,
7
7
MTLPrimitiveAccelerationStructureDescriptor , MTLStorageMode , MTLTextureDescriptor ,
8
8
} ;
9
9
@@ -35,7 +35,17 @@ fn main() {
35
35
gpu_allocator:: MemoryLocation :: GpuOnly ,
36
36
) ;
37
37
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) ;
39
49
allocator. free ( & allocation) . unwrap ( ) ;
40
50
info ! ( "Allocation and deallocation of GpuOnly memory was successful." ) ;
41
51
}
@@ -49,7 +59,17 @@ fn main() {
49
59
gpu_allocator:: MemoryLocation :: CpuToGpu ,
50
60
) ;
51
61
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) ;
53
73
allocator. free ( & allocation) . unwrap ( ) ;
54
74
info ! ( "Allocation and deallocation of CpuToGpu memory was successful." ) ;
55
75
}
@@ -63,7 +83,17 @@ fn main() {
63
83
gpu_allocator:: MemoryLocation :: GpuToCpu ,
64
84
) ;
65
85
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) ;
67
97
allocator. free ( & allocation) . unwrap ( ) ;
68
98
info ! ( "Allocation and deallocation of GpuToCpu memory was successful." ) ;
69
99
}
@@ -78,7 +108,13 @@ fn main() {
78
108
let allocation_desc =
79
109
AllocationCreateDesc :: texture ( & device, "Test allocation (Texture)" , & texture_desc) ;
80
110
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) ;
82
118
allocator. free ( & allocation) . unwrap ( ) ;
83
119
info ! ( "Allocation and deallocation of Texture was successful." ) ;
84
120
}
@@ -96,7 +132,16 @@ fn main() {
96
132
gpu_allocator:: MemoryLocation :: GpuOnly ,
97
133
) ;
98
134
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) ;
100
145
allocator. free ( & allocation) . unwrap ( ) ;
101
146
info ! ( "Allocation and deallocation of Acceleration structure was successful." ) ;
102
147
}
0 commit comments