0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Vulkanoで書いたアプリケーションのVRAM消費量が多すぎる場合の対応

Last updated at Posted at 2025-06-13

環境

  • 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を確保するようなアプリケーションを書く場合、ブロックサイズが小さすぎるとパフォーマンスが落ちる場合がある

  1. https://docs.rs/vulkano/0.35.1/src/vulkano/memory/allocator/mod.rs.html#873-877

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?