Skip to content

Commit

Permalink
Specify linear or non-linear allocation
Browse files Browse the repository at this point in the history
Similar to how the Vulkan implementation does it
  • Loading branch information
FlannyH committed Feb 22, 2024
1 parent e7f0611 commit fb16010
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
12 changes: 10 additions & 2 deletions examples/metal-buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
&device,
"Test allocation (Gpu Only)",
512,
true,
gpu_allocator::MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
Expand All @@ -36,6 +37,7 @@ fn main() {
&device,
"Test allocation (Cpu to Gpu)",
512,
true,
gpu_allocator::MemoryLocation::CpuToGpu,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
Expand All @@ -50,6 +52,7 @@ fn main() {
&device,
"Test allocation (Gpu to Cpu)",
512,
true,
gpu_allocator::MemoryLocation::GpuToCpu,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
Expand All @@ -65,8 +68,12 @@ fn main() {
texture_desc.set_width(64);
texture_desc.set_height(64);
texture_desc.set_storage_mode(metal::MTLStorageMode::Private);
let allocation_desc =
AllocationCreateDesc::texture(&device, "Test allocation (Texture)", &texture_desc);
let allocation_desc = AllocationCreateDesc::texture(
&device,
"Test allocation (Texture)",
true,
&texture_desc,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
let _texture = allocation.make_texture(&texture_desc).unwrap();
allocator.free(&allocation).unwrap();
Expand All @@ -83,6 +90,7 @@ fn main() {
&device,
"Test allocation (Acceleration structure)",
sizes.acceleration_structure_size,
true,
gpu_allocator::MemoryLocation::GpuOnly,
);
let allocation = allocator.allocate(&allocation_desc).unwrap();
Expand Down
14 changes: 13 additions & 1 deletion src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub struct AllocationCreateDesc<'a> {
pub name: &'a str,
/// Location where the memory allocation should be stored
pub location: MemoryLocation,
/// If the resource is linear (buffer / linear texture) or a regular (tiled) texture.
pub linear: bool,
pub size: u64,
pub alignment: u64,
}
Expand All @@ -94,6 +96,7 @@ impl<'a> AllocationCreateDesc<'a> {
device: &metal::Device,
name: &'a str,
length: u64,
linear: bool,
location: MemoryLocation,
) -> AllocationCreateDesc<'a> {
let size_and_align =
Expand All @@ -103,12 +106,14 @@ impl<'a> AllocationCreateDesc<'a> {
location,
size: size_and_align.size,
alignment: size_and_align.align,
linear,
}
}

pub fn texture(
device: &metal::Device,
name: &'a str,
linear: bool,
desc: &metal::TextureDescriptor,
) -> AllocationCreateDesc<'a> {
let size_and_align = device.heap_texture_size_and_align(desc);
Expand All @@ -122,13 +127,15 @@ impl<'a> AllocationCreateDesc<'a> {
},
size: size_and_align.size,
alignment: size_and_align.align,
linear,
}
}

pub fn acceleration_structure_with_size(
device: &metal::Device,
name: &'a str,
size: u64,
linear: bool,
location: MemoryLocation,
) -> AllocationCreateDesc<'a> {
let size_and_align = device.heap_acceleration_structure_size_and_align_with_size(size);
Expand All @@ -137,6 +144,7 @@ impl<'a> AllocationCreateDesc<'a> {
location,
size: size_and_align.size,
alignment: size_and_align.align,
linear,
}
}
}
Expand Down Expand Up @@ -203,7 +211,11 @@ impl MemoryType {
backtrace: Arc<Backtrace>,
allocation_sizes: &AllocationSizes,
) -> Result<Allocation> {
let allocation_type = AllocationType::Linear;
let allocation_type = if desc.linear {
AllocationType::Linear
} else {
AllocationType::NonLinear
};

let memblock_size = if self.heap_properties.storage_mode() == MTLStorageMode::Private {
allocation_sizes.device_memblock_size
Expand Down

0 comments on commit fb16010

Please sign in to comment.