Skip to content

Commit bc95d25

Browse files
Add draft of general documentation chapter about memory budget
Work in progress...
1 parent 5f573f5 commit bc95d25

1 file changed

Lines changed: 78 additions & 9 deletions

File tree

src/vk_mem_alloc.h

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ extern "C" {
2929

3030
/** \mainpage Vulkan Memory Allocator
3131

32-
<b>Version 2.3.0-development</b> (2019-07-02)
32+
<b>Version 2.3.0-development</b> (2019-10-31)
3333

34-
Copyright (c) 2017-2018 Advanced Micro Devices, Inc. All rights reserved. \n
34+
Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. \n
3535
License: MIT
3636

3737
Documentation of all members: vk_mem_alloc.h
@@ -54,6 +54,9 @@ Documentation of all members: vk_mem_alloc.h
5454
- [Persistently mapped memory](@ref memory_mapping_persistently_mapped_memory)
5555
- [Cache control](@ref memory_mapping_cache_control)
5656
- [Finding out if memory is mappable](@ref memory_mapping_finding_if_memory_mappable)
57+
- \subpage staying_within_budget
58+
- [Querying for budget](@ref staying_within_budget_querying_for_budget)
59+
- [Controlling memory usage](@ref staying_within_budget_controlling_memory_usage)
5760
- \subpage custom_memory_pools
5861
- [Choosing memory type index](@ref custom_memory_pools_MemTypeIndex)
5962
- [Linear allocation algorithm](@ref linear_algorithm)
@@ -523,6 +526,78 @@ else
523526
\endcode
524527

525528

529+
\page staying_within_budget Staying within budget
530+
531+
When developing a graphics-intensive game or program, it is important to avoid allocating
532+
more GPU memory than it's physically available. When the memory is over-committed,
533+
various bad things can happen, depending on the specific GPU, graphics driver, and
534+
operating system:
535+
536+
- It may just work without any problems.
537+
- The application may slow down because some memory blocks are moved to system RAM
538+
and the GPU has to access them through PCI Express bus.
539+
- A new allocation may take very long time to complete, even few seconds, and possibly
540+
freeze entire system.
541+
- The new allocation may fail with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
542+
- It may even result in GPU crash (TDR), observed as `VK_ERROR_DEVICE_LOST`
543+
returned somewhere later.
544+
545+
\section staying_within_budget_querying_for_budget Querying for budget
546+
547+
To query for current memory usage and available budget, use function vmaGetBudget().
548+
Returned structure #VmaBudget contains quantities expressed in bytes, per Vulkan memory heap.
549+
550+
Please note that this function returns different information and works faster than
551+
vmaCalculateStats(). vmaGetBudget() can be called every frame or even before every
552+
allocation, while vmaCalculateStats() is intended to be used rarely,
553+
only to obtain statistical information, e.g. for debugging purposes.
554+
555+
It is recommended to use <b>VK_EXT_memory_budget</b> device extension to obtain information
556+
about the budget from Vulkan device. VMA is able to use this extension automatically.
557+
When not enabled, the allocator behaves same way, but then it estimates current usage
558+
and available budget based on its internal information and Vulkan memory heap sizes,
559+
which may be less precise. In order to use this extension:
560+
561+
1. Make sure extensions VK_EXT_memory_budget and VK_KHR_get_physical_device_properties2
562+
required by it are available and enable them. Please note that the first is a device
563+
extension and the second is instance extension!
564+
2. Use flag #VMA_ALLOCATOR_CREATE_ ?? when creating #VmaAllocator object.
565+
3. Make sure to call vmaSetCurrentFrameIndex() every frame. Budget is queried from
566+
Vulkan inside of it to avoid overhead of querying it with every allocation.
567+
568+
\section staying_within_budget_controlling_memory_usage Controlling memory usage
569+
570+
There are many ways in which you can try to stay within the budget.
571+
572+
First, when making new allocation requires allocating a new memory block, the library
573+
tries not to exceed the budget automatically. If a block with default recommended size
574+
(e.g. 256 MB) would go over budget, a smaller block is allocated, possibly even
575+
dedicated memory for just this resource.
576+
577+
If the size of the requested resource plus current memory usage is more than the
578+
budget, by default the library still tries to create it, leaving it to the Vulkan
579+
implementation whether the allocation succeeds or fails. You can change this behavior
580+
by using #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag. With it, the allocation is
581+
not made if it would exceed the budget or if the budget is already exceeded.
582+
Some other allocations become lost instead to make room for it, if the mechanism of
583+
[lost allocations](@ref lost_allocations) is used.
584+
If that is not possible, the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
585+
Example usage pattern may be to pass the #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag
586+
when creating resources that are not essential for the application (e.g. the texture
587+
of a specific object) and not to pass it when creating critically important resources
588+
(e.g. render targets).
589+
590+
Finally, you can also use #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT flag to make sure
591+
a new allocation is created only when it fits inside one of the existing memory blocks.
592+
If it would require to allocate a new block, if fails instead with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
593+
This also ensures that the function call is very fast because it never goes to Vulkan
594+
to obtain a new block.
595+
596+
Please note that creating \ref custom_memory_pools with VmaPoolCreateInfo::minBlockCount
597+
set to more than 0 will try to allocate memory blocks without checking whether they
598+
fit within budget.
599+
600+
526601
\page custom_memory_pools Custom memory pools
527602

528603
A memory pool contains a number of `VkDeviceMemory` blocks.
@@ -2070,13 +2145,7 @@ typedef struct VmaBudget
20702145

20712146
It might be different (most probably smaller) than `VkMemoryHeap::size[i]` due to factors
20722147
external to the program, like other programs also consuming system resources.
2073-
2074-
Exceeding the budget may result, depending on operating system and graphics driver:
2075-
2076-
- Allocation failing with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
2077-
- Allocation taking very long time, even few seconds.
2078-
- Overall system slowdown.
2079-
- Even GPU crash (TDR), observed as `VK_ERROR_DEVICE_LOST` returned somewhere later.
2148+
Exceeding the budget may result in various problems.
20802149
*/
20812150
VkDeviceSize budget[VK_MAX_MEMORY_HEAPS];
20822151
} VmaBudget;

0 commit comments

Comments
 (0)