環境
- Vulkano 0.35.1
原因
VulkanoのStandardMemoryAllocator::new_default()
を使用してMemoryAllocatorを作成すると、1GiB以上のメモリヒープ1つにつき256MiB、そうでない場合は64MiBが初期化時に確保される。1
対処
StandardMemoryAllocator::new_default()
を使用せず、StandardMemoryAllocator::new()
を使用してメモリを確保する
例
// This codeblock is based on Vulkano Standard memory allocator (https://docs.rs/vulkano/0.35.1/src/vulkano/memory/allocator/mod.rs.html#857).
// Copyright (c) 2016 The Vulkano Developers
// See: https://github.com/vulkano-rs/vulkano/blob/master/LICENSE-MIT
type StandardLiteMemoryAllocator = GenericMemoryAllocator<FreeListAllocator>;
trait StandardLiteMemoryAllocatorExt {
/// Creates a new `StandardLiteMemoryAllocator` with default configuration.
fn new_default_lite(device: Arc<Device>) -> Self;
}
impl StandardLiteMemoryAllocatorExt for StandardLiteMemoryAllocator {
/// Creates a new `StandardMemoryAllocator` with default configuration.
fn new_default_lite(device: Arc<Device>) -> Self {
let MemoryProperties {
memory_types,
memory_heaps,
..
} = device.physical_device().memory_properties();
let mut block_sizes = vec![0; memory_types.len()];
let mut memory_type_bits = u32::MAX;
for (index, memory_type) in memory_types.iter().enumerate() {
const LARGE_HEAP_THRESHOLD: DeviceSize = 1024 * 1024 * 1024;
let heap_size = memory_heaps[memory_type.heap_index as usize].size;
block_sizes[index] = if heap_size >= LARGE_HEAP_THRESHOLD {
// ヒープが大きい場合 初期ブロックサイズ 4MiB
4 * 1024 * 1024
} else {
// ヒープが小さい場合 初期ブロックサイズ 2MiB
2 * 1024 * 1024
};
if memory_type.property_flags.intersects(
MemoryPropertyFlags::LAZILY_ALLOCATED
| MemoryPropertyFlags::PROTECTED
| MemoryPropertyFlags::DEVICE_COHERENT
| MemoryPropertyFlags::RDMA_CAPABLE,
) {
// VUID-VkMemoryAllocateInfo-memoryTypeIndex-01872
// VUID-vkAllocateMemory-deviceCoherentMemory-02790
// Lazily allocated memory would just cause problems for suballocation in general.
memory_type_bits &= !(1 << index);
}
}
let create_info = GenericMemoryAllocatorCreateInfo {
block_sizes: &block_sizes,
memory_type_bits,
..Default::default()
};
Self::new(device, create_info)
}
}
注意点
結局大量のVRAMを確保するようなアプリケーションを書く場合、ブロックサイズが小さすぎるとパフォーマンスが落ちる場合がある