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 27, 2025
1 parent 7df45bb commit 39ad9b7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 8 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,15 @@ let allocation_desc = AllocationCreateDesc::buffer(
MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let resource = allocation.make_buffer().unwrap();
let heap = unsafe { allocation.heap() };
let resource = unsafe {
heap.newBufferWithLength_options_offset(
allocation.size() as usize,
heap.resourceOptions(),
allocation.offset() as usize,
)
}
.unwrap();

// Cleanup
drop(resource);
Expand Down
57 changes: 51 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,17 @@ 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();
drop(buffer);
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of GpuOnly memory was successful.");
}
Expand All @@ -49,7 +59,17 @@ 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();
drop(buffer);
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of CpuToGpu memory was successful.");
}
Expand All @@ -63,7 +83,17 @@ 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();
drop(buffer);
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of GpuToCpu memory was successful.");
}
Expand All @@ -78,7 +108,13 @@ 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();
drop(buffer);
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of Texture was successful.");
}
Expand All @@ -96,7 +132,16 @@ 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();
drop(buffer);
allocator.free(&allocation).unwrap();
info!("Allocation and deallocation of Acceleration structure was successful.");
}
Expand Down
11 changes: 10 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,16 @@
//! MemoryLocation::GpuOnly,
//! );
//! let allocation = allocator.allocate(&allocation_desc).unwrap();
//! let resource = allocation.make_buffer().unwrap();
//! # use objc2_metal::MTLHeap;
//! let heap = unsafe { allocation.heap() };
//! let resource = unsafe {
//! heap.newBufferWithLength_options_offset(
//! allocation.size() as usize,
//! heap.resourceOptions(),
//! allocation.offset() as usize,
//! )
//! }
//! .unwrap();
//!
//! // Cleanup
//! drop(resource);
Expand Down

0 comments on commit 39ad9b7

Please sign in to comment.