Skip to content

Commit

Permalink
Inline descriptor creators into example
Browse files Browse the repository at this point in the history
  • Loading branch information
MarijnS95 committed Jan 8, 2025
1 parent aef77ab commit dc4a717
Showing 1 changed file with 46 additions and 6 deletions.
52 changes: 46 additions & 6 deletions examples/metal-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use log::info;
use objc2::rc::Id;
use objc2_foundation::NSArray;
use objc2_metal::{
MTLCreateSystemDefaultDevice, MTLDevice as _, MTLPixelFormat,
MTLCreateSystemDefaultDevice, MTLDevice as _, MTLHeap, MTLPixelFormat,
MTLPrimitiveAccelerationStructureDescriptor, MTLStorageMode, MTLTextureDescriptor,
};

Expand Down Expand Up @@ -35,7 +35,16 @@ fn main() {
gpu_allocator::MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _buffer = allocation.make_buffer().unwrap();
// SAFETY: We will only allocate objects on this heap within the returned offset and size
let heap = unsafe { allocation.heap() };
let _buffer = unsafe {
heap.newBufferWithLength_options_offset(
allocation.size() as usize,
heap.resourceOptions(),
allocation.offset() as usize,
)
}
.unwrap();
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of GpuOnly memory was successful.");
}
Expand All @@ -49,7 +58,16 @@ fn main() {
gpu_allocator::MemoryLocation::CpuToGpu,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _buffer = allocation.make_buffer().unwrap();
// SAFETY: We will only allocate objects on this heap within the returned offset and size
let heap = unsafe { allocation.heap() };
let _buffer = unsafe {
heap.newBufferWithLength_options_offset(
allocation.size() as usize,
heap.resourceOptions(),
allocation.offset() as usize,
)
}
.unwrap();
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of CpuToGpu memory was successful.");
}
Expand All @@ -63,7 +81,16 @@ fn main() {
gpu_allocator::MemoryLocation::GpuToCpu,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _buffer = allocation.make_buffer().unwrap();
// SAFETY: We will only allocate objects on this heap within the returned offset and size
let heap = unsafe { allocation.heap() };
let _buffer = unsafe {
heap.newBufferWithLength_options_offset(
allocation.size() as usize,
heap.resourceOptions(),
allocation.offset() as usize,
)
}
.unwrap();
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of GpuToCpu memory was successful.");
}
Expand All @@ -78,7 +105,12 @@ fn main() {
let allocation_desc =
AllocationCreateDesc::texture(&device, "Test allocation (Texture)", &texture_desc);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _texture = allocation.make_texture(&texture_desc).unwrap();
// SAFETY: We will only allocate objects on this heap within the returned offset and size
let heap = unsafe { allocation.heap() };
let _buffer = unsafe {
heap.newTextureWithDescriptor_offset(&texture_desc, allocation.offset() as usize)
}
.unwrap();
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of Texture was successful.");
}
Expand All @@ -96,7 +128,15 @@ fn main() {
gpu_allocator::MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _acc_structure = allocation.make_acceleration_structure();
// SAFETY: We will only allocate objects on this heap within the returned offset and size
let heap = unsafe { allocation.heap() };
let _buffer = unsafe {
heap.newAccelerationStructureWithSize_offset(
allocation.size() as usize,
allocation.offset() as usize,
)
}
.unwrap();
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of Acceleration structure was successful.");
}
Expand Down

0 comments on commit dc4a717

Please sign in to comment.