Skip to content

Commit accc7ef

Browse files
committed
Inline descriptor creators into example
1 parent 7df45bb commit accc7ef

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,14 @@ let allocation_desc = AllocationCreateDesc::buffer(
152152
MemoryLocation::GpuOnly,
153153
);
154154
let allocation = allocator.allocate(&allocation_desc).unwrap();
155-
let resource = allocation.make_buffer().unwrap();
155+
let resource = unsafe {
156+
heap.newBufferWithLength_options_offset(
157+
allocation.size() as usize,
158+
heap.resourceOptions(),
159+
allocation.offset() as usize,
160+
)
161+
}
162+
.unwrap();
156163

157164
// Cleanup
158165
drop(resource);

examples/metal-buffer.rs

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use log::info;
33
use objc2::rc::Id;
44
use objc2_foundation::NSArray;
55
use 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
}

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,14 @@
197197
//! MemoryLocation::GpuOnly,
198198
//! );
199199
//! let allocation = allocator.allocate(&allocation_desc).unwrap();
200-
//! let resource = allocation.make_buffer().unwrap();
200+
//! let resource = unsafe {
201+
//! heap.newBufferWithLength_options_offset(
202+
//! allocation.size() as usize,
203+
//! heap.resourceOptions(),
204+
//! allocation.offset() as usize,
205+
//! )
206+
//! }
207+
//! .unwrap();
201208
//!
202209
//! // Cleanup
203210
//! drop(resource);

0 commit comments

Comments
 (0)