From 4cc439922a286c8b9534e5b85c07e04c5155a754 Mon Sep 17 00:00:00 2001
From: bruvzg <7645683+bruvzg@users.noreply.github.com>
Date: Tue, 11 Feb 2020 18:19:02 +0200
Subject: [PATCH] Update VulkanMemoryAllocator to 2.3.0 (Fixes build for 32-bit
Windows and Linux).
---
thirdparty/vulkan/vk_mem_alloc.h | 13704 +++++++++++++++++------------
1 file changed, 8259 insertions(+), 5445 deletions(-)
diff --git a/thirdparty/vulkan/vk_mem_alloc.h b/thirdparty/vulkan/vk_mem_alloc.h
index 862ea312a60..465864b3634 100644
--- a/thirdparty/vulkan/vk_mem_alloc.h
+++ b/thirdparty/vulkan/vk_mem_alloc.h
@@ -1,5 +1,5 @@
//
-// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
+// Copyright (c) 2017-2020 Advanced Micro Devices, Inc. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@@ -29,9 +29,9 @@ extern "C" {
/** \mainpage Vulkan Memory Allocator
-Version 2.3.0-development (2019-03-05)
+Version 2.3.0 (2019-12-04)
-Copyright (c) 2017-2018 Advanced Micro Devices, Inc. All rights reserved. \n
+Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. \n
License: MIT
Documentation of all members: vk_mem_alloc.h
@@ -40,46 +40,50 @@ Documentation of all members: vk_mem_alloc.h
- User guide
- \subpage quick_start
- - [Project setup](@ref quick_start_project_setup)
- - [Initialization](@ref quick_start_initialization)
- - [Resource allocation](@ref quick_start_resource_allocation)
+ - [Project setup](@ref quick_start_project_setup)
+ - [Initialization](@ref quick_start_initialization)
+ - [Resource allocation](@ref quick_start_resource_allocation)
- \subpage choosing_memory_type
- - [Usage](@ref choosing_memory_type_usage)
- - [Required and preferred flags](@ref choosing_memory_type_required_preferred_flags)
- - [Explicit memory types](@ref choosing_memory_type_explicit_memory_types)
- - [Custom memory pools](@ref choosing_memory_type_custom_memory_pools)
- - [Dedicated allocations](@ref choosing_memory_type_dedicated_allocations)
+ - [Usage](@ref choosing_memory_type_usage)
+ - [Required and preferred flags](@ref choosing_memory_type_required_preferred_flags)
+ - [Explicit memory types](@ref choosing_memory_type_explicit_memory_types)
+ - [Custom memory pools](@ref choosing_memory_type_custom_memory_pools)
+ - [Dedicated allocations](@ref choosing_memory_type_dedicated_allocations)
- \subpage memory_mapping
- - [Mapping functions](@ref memory_mapping_mapping_functions)
- - [Persistently mapped memory](@ref memory_mapping_persistently_mapped_memory)
- - [Cache control](@ref memory_mapping_cache_control)
- - [Finding out if memory is mappable](@ref memory_mapping_finding_if_memory_mappable)
+ - [Mapping functions](@ref memory_mapping_mapping_functions)
+ - [Persistently mapped memory](@ref memory_mapping_persistently_mapped_memory)
+ - [Cache flush and invalidate](@ref memory_mapping_cache_control)
+ - [Finding out if memory is mappable](@ref memory_mapping_finding_if_memory_mappable)
+ - \subpage staying_within_budget
+ - [Querying for budget](@ref staying_within_budget_querying_for_budget)
+ - [Controlling memory usage](@ref staying_within_budget_controlling_memory_usage)
- \subpage custom_memory_pools
- - [Choosing memory type index](@ref custom_memory_pools_MemTypeIndex)
- - [Linear allocation algorithm](@ref linear_algorithm)
- - [Free-at-once](@ref linear_algorithm_free_at_once)
- - [Stack](@ref linear_algorithm_stack)
- - [Double stack](@ref linear_algorithm_double_stack)
- - [Ring buffer](@ref linear_algorithm_ring_buffer)
- - [Buddy allocation algorithm](@ref buddy_algorithm)
+ - [Choosing memory type index](@ref custom_memory_pools_MemTypeIndex)
+ - [Linear allocation algorithm](@ref linear_algorithm)
+ - [Free-at-once](@ref linear_algorithm_free_at_once)
+ - [Stack](@ref linear_algorithm_stack)
+ - [Double stack](@ref linear_algorithm_double_stack)
+ - [Ring buffer](@ref linear_algorithm_ring_buffer)
+ - [Buddy allocation algorithm](@ref buddy_algorithm)
- \subpage defragmentation
- - [Defragmenting CPU memory](@ref defragmentation_cpu)
- - [Defragmenting GPU memory](@ref defragmentation_gpu)
- - [Additional notes](@ref defragmentation_additional_notes)
- - [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm)
+ - [Defragmenting CPU memory](@ref defragmentation_cpu)
+ - [Defragmenting GPU memory](@ref defragmentation_gpu)
+ - [Additional notes](@ref defragmentation_additional_notes)
+ - [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm)
- \subpage lost_allocations
- \subpage statistics
- - [Numeric statistics](@ref statistics_numeric_statistics)
- - [JSON dump](@ref statistics_json_dump)
+ - [Numeric statistics](@ref statistics_numeric_statistics)
+ - [JSON dump](@ref statistics_json_dump)
- \subpage allocation_annotation
- - [Allocation user data](@ref allocation_user_data)
- - [Allocation names](@ref allocation_names)
+ - [Allocation user data](@ref allocation_user_data)
+ - [Allocation names](@ref allocation_names)
- \subpage debugging_memory_usage
- - [Memory initialization](@ref debugging_memory_usage_initialization)
- - [Margins](@ref debugging_memory_usage_margins)
- - [Corruption detection](@ref debugging_memory_usage_corruption_detection)
+ - [Memory initialization](@ref debugging_memory_usage_initialization)
+ - [Margins](@ref debugging_memory_usage_margins)
+ - [Corruption detection](@ref debugging_memory_usage_corruption_detection)
- \subpage record_and_replay
- \subpage usage_patterns
+ - [Common mistakes](@ref usage_patterns_common_mistakes)
- [Simple patterns](@ref usage_patterns_simple)
- [Advanced patterns](@ref usage_patterns_advanced)
- \subpage configuration
@@ -88,6 +92,7 @@ Documentation of all members: vk_mem_alloc.h
- [Device memory allocation callbacks](@ref allocation_callbacks)
- [Device heap memory limit](@ref heap_memory_limit)
- \subpage vk_khr_dedicated_allocation
+ - \subpage vk_amd_device_coherent_memory
- \subpage general_considerations
- [Thread safety](@ref general_considerations_thread_safety)
- [Validation layer warnings](@ref general_considerations_validation_layer_warnings)
@@ -206,7 +211,8 @@ You can also combine multiple methods.
-# If you already have a buffer or an image created, you want to allocate memory
for it and then you will bind it yourself, you can use function
vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage().
- For binding you should use functions: vmaBindBufferMemory(), vmaBindImageMemory().
+ For binding you should use functions: vmaBindBufferMemory(), vmaBindImageMemory()
+ or their extended versions: vmaBindBufferMemory2(), vmaBindImageMemory2().
-# If you want to create a buffer or an image, allocate memory for it and bind
them together, all in one call, you can use function vmaCreateBuffer(),
vmaCreateImage(). This is the easiest and recommended way to use this library.
@@ -351,7 +357,7 @@ Example:
struct ConstantBuffer
{
- ...
+ ...
};
ConstantBuffer constantBufferData;
@@ -421,12 +427,13 @@ There are some exceptions though, when you should consider mapping memory only f
which requires unmapping before GPU can see updated texture.
- Keeping many large memory blocks mapped may impact performance or stability of some debugging tools.
-\section memory_mapping_cache_control Cache control
-
+\section memory_mapping_cache_control Cache flush and invalidate
+
Memory in Vulkan doesn't need to be unmapped before using it on GPU,
but unless a memory types has `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` flag set,
-you need to manually invalidate cache before reading of mapped pointer
-and flush cache after writing to mapped pointer.
+you need to manually **invalidate** cache before reading of mapped pointer
+and **flush** cache after writing to mapped pointer.
+Map/unmap operations don't do that automatically.
Vulkan provides following functions for this purpose `vkFlushMappedMemoryRanges()`,
`vkInvalidateMappedMemoryRanges()`, but this library provides more convenient
functions that refer to given allocation object: vmaFlushAllocation(),
@@ -440,7 +447,7 @@ within blocks are aligned to this value, so their offsets are always multiply of
Please note that memory allocated with #VMA_MEMORY_USAGE_CPU_ONLY is guaranteed to be `HOST_COHERENT`.
-Also, Windows drivers from all 3 PC GPU vendors (AMD, Intel, NVIDIA)
+Also, Windows drivers from all 3 **PC** GPU vendors (AMD, Intel, NVIDIA)
currently provide `HOST_COHERENT` flag on all memory types that are
`HOST_VISIBLE`, so on this platform you may not need to bother.
@@ -474,16 +481,16 @@ VkMemoryPropertyFlags memFlags;
vmaGetMemoryTypeProperties(allocator, allocInfo.memoryType, &memFlags);
if((memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)
{
- // Allocation ended up in mappable memory. You can map it and access it directly.
- void* mappedData;
- vmaMapMemory(allocator, alloc, &mappedData);
- memcpy(mappedData, &constantBufferData, sizeof(constantBufferData));
- vmaUnmapMemory(allocator, alloc);
+ // Allocation ended up in mappable memory. You can map it and access it directly.
+ void* mappedData;
+ vmaMapMemory(allocator, alloc, &mappedData);
+ memcpy(mappedData, &constantBufferData, sizeof(constantBufferData));
+ vmaUnmapMemory(allocator, alloc);
}
else
{
- // Allocation ended up in non-mappable memory.
- // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.
+ // Allocation ended up in non-mappable memory.
+ // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.
}
\endcode
@@ -509,18 +516,90 @@ vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allo
if(allocInfo.pUserData != nullptr)
{
- // Allocation ended up in mappable memory.
- // It's persistently mapped. You can access it directly.
- memcpy(allocInfo.pMappedData, &constantBufferData, sizeof(constantBufferData));
+ // Allocation ended up in mappable memory.
+ // It's persistently mapped. You can access it directly.
+ memcpy(allocInfo.pMappedData, &constantBufferData, sizeof(constantBufferData));
}
else
{
- // Allocation ended up in non-mappable memory.
- // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.
+ // Allocation ended up in non-mappable memory.
+ // You need to create CPU-side buffer in VMA_MEMORY_USAGE_CPU_ONLY and make a transfer.
}
\endcode
+\page staying_within_budget Staying within budget
+
+When developing a graphics-intensive game or program, it is important to avoid allocating
+more GPU memory than it's physically available. When the memory is over-committed,
+various bad things can happen, depending on the specific GPU, graphics driver, and
+operating system:
+
+- It may just work without any problems.
+- The application may slow down because some memory blocks are moved to system RAM
+ and the GPU has to access them through PCI Express bus.
+- A new allocation may take very long time to complete, even few seconds, and possibly
+ freeze entire system.
+- The new allocation may fail with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
+- It may even result in GPU crash (TDR), observed as `VK_ERROR_DEVICE_LOST`
+ returned somewhere later.
+
+\section staying_within_budget_querying_for_budget Querying for budget
+
+To query for current memory usage and available budget, use function vmaGetBudget().
+Returned structure #VmaBudget contains quantities expressed in bytes, per Vulkan memory heap.
+
+Please note that this function returns different information and works faster than
+vmaCalculateStats(). vmaGetBudget() can be called every frame or even before every
+allocation, while vmaCalculateStats() is intended to be used rarely,
+only to obtain statistical information, e.g. for debugging purposes.
+
+It is recommended to use VK_EXT_memory_budget device extension to obtain information
+about the budget from Vulkan device. VMA is able to use this extension automatically.
+When not enabled, the allocator behaves same way, but then it estimates current usage
+and available budget based on its internal information and Vulkan memory heap sizes,
+which may be less precise. In order to use this extension:
+
+1. Make sure extensions VK_EXT_memory_budget and VK_KHR_get_physical_device_properties2
+ required by it are available and enable them. Please note that the first is a device
+ extension and the second is instance extension!
+2. Use flag #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT when creating #VmaAllocator object.
+3. Make sure to call vmaSetCurrentFrameIndex() every frame. Budget is queried from
+ Vulkan inside of it to avoid overhead of querying it with every allocation.
+
+\section staying_within_budget_controlling_memory_usage Controlling memory usage
+
+There are many ways in which you can try to stay within the budget.
+
+First, when making new allocation requires allocating a new memory block, the library
+tries not to exceed the budget automatically. If a block with default recommended size
+(e.g. 256 MB) would go over budget, a smaller block is allocated, possibly even
+dedicated memory for just this resource.
+
+If the size of the requested resource plus current memory usage is more than the
+budget, by default the library still tries to create it, leaving it to the Vulkan
+implementation whether the allocation succeeds or fails. You can change this behavior
+by using #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag. With it, the allocation is
+not made if it would exceed the budget or if the budget is already exceeded.
+Some other allocations become lost instead to make room for it, if the mechanism of
+[lost allocations](@ref lost_allocations) is used.
+If that is not possible, the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
+Example usage pattern may be to pass the #VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT flag
+when creating resources that are not essential for the application (e.g. the texture
+of a specific object) and not to pass it when creating critically important resources
+(e.g. render targets).
+
+Finally, you can also use #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT flag to make sure
+a new allocation is created only when it fits inside one of the existing memory blocks.
+If it would require to allocate a new block, if fails instead with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
+This also ensures that the function call is very fast because it never goes to Vulkan
+to obtain a new block.
+
+Please note that creating \ref custom_memory_pools with VmaPoolCreateInfo::minBlockCount
+set to more than 0 will try to allocate memory blocks without checking whether they
+fit within budget.
+
+
\page custom_memory_pools Custom memory pools
A memory pool contains a number of `VkDeviceMemory` blocks.
@@ -744,7 +823,7 @@ allocations.
To mitigate this problem, you can use defragmentation feature:
structure #VmaDefragmentationInfo2, function vmaDefragmentationBegin(), vmaDefragmentationEnd().
-Given set of allocations,
+Given set of allocations,
this function can move them to compact used memory, ensure more continuous free
space and possibly also free some `VkDeviceMemory` blocks.
@@ -761,7 +840,8 @@ What it doesn't do, so you need to do it yourself:
- Recreate buffers and images that were bound to allocations that were defragmented and
bind them with their new places in memory.
You must use `vkDestroyBuffer()`, `vkDestroyImage()`,
- `vkCreateBuffer()`, `vkCreateImage()` for that purpose and NOT vmaDestroyBuffer(),
+ `vkCreateBuffer()`, `vkCreateImage()`, vmaBindBufferMemory(), vmaBindImageMemory()
+ for that purpose and NOT vmaDestroyBuffer(),
vmaDestroyImage(), vmaCreateBuffer(), vmaCreateImage(), because you don't need to
destroy or create allocation objects!
- Recreate views and update descriptors that point to these buffers and images.
@@ -801,22 +881,22 @@ vmaDefragmentationEnd(allocator, defragCtx);
for(uint32_t i = 0; i < allocCount; ++i)
{
- if(allocationsChanged[i])
- {
- // Destroy buffer that is immutably bound to memory region which is no longer valid.
- vkDestroyBuffer(device, buffers[i], nullptr);
+ if(allocationsChanged[i])
+ {
+ // Destroy buffer that is immutably bound to memory region which is no longer valid.
+ vkDestroyBuffer(device, buffers[i], nullptr);
- // Create new buffer with same parameters.
- VkBufferCreateInfo bufferInfo = ...;
- vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);
-
- // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.
-
- // Bind new buffer to new memory region. Data contained in it is already moved.
- VmaAllocationInfo allocInfo;
- vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);
- vkBindBufferMemory(device, buffers[i], allocInfo.deviceMemory, allocInfo.offset);
- }
+ // Create new buffer with same parameters.
+ VkBufferCreateInfo bufferInfo = ...;
+ vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);
+
+ // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.
+
+ // Bind new buffer to new memory region. Data contained in it is already moved.
+ VmaAllocationInfo allocInfo;
+ vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);
+ vmaBindBufferMemory(allocator, allocations[i], buffers[i]);
+ }
}
\endcode
@@ -879,22 +959,22 @@ vmaDefragmentationEnd(allocator, defragCtx);
for(uint32_t i = 0; i < allocCount; ++i)
{
- if(allocationsChanged[i])
- {
- // Destroy buffer that is immutably bound to memory region which is no longer valid.
- vkDestroyBuffer(device, buffers[i], nullptr);
+ if(allocationsChanged[i])
+ {
+ // Destroy buffer that is immutably bound to memory region which is no longer valid.
+ vkDestroyBuffer(device, buffers[i], nullptr);
- // Create new buffer with same parameters.
- VkBufferCreateInfo bufferInfo = ...;
- vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);
-
- // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.
-
- // Bind new buffer to new memory region. Data contained in it is already moved.
- VmaAllocationInfo allocInfo;
- vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);
- vkBindBufferMemory(device, buffers[i], allocInfo.deviceMemory, allocInfo.offset);
- }
+ // Create new buffer with same parameters.
+ VkBufferCreateInfo bufferInfo = ...;
+ vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]);
+
+ // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning.
+
+ // Bind new buffer to new memory region. Data contained in it is already moved.
+ VmaAllocationInfo allocInfo;
+ vmaGetAllocationInfo(allocator, allocations[i], &allocInfo);
+ vmaBindBufferMemory(allocator, allocations[i], buffers[i]);
+ }
}
\endcode
@@ -1008,40 +1088,40 @@ Example code:
\code
struct MyBuffer
{
- VkBuffer m_Buf = nullptr;
- VmaAllocation m_Alloc = nullptr;
+ VkBuffer m_Buf = nullptr;
+ VmaAllocation m_Alloc = nullptr;
- // Called when the buffer is really needed in the current frame.
- void EnsureBuffer();
+ // Called when the buffer is really needed in the current frame.
+ void EnsureBuffer();
};
void MyBuffer::EnsureBuffer()
{
- // Buffer has been created.
- if(m_Buf != VK_NULL_HANDLE)
- {
- // Check if its allocation is not lost + mark it as used in current frame.
- if(vmaTouchAllocation(allocator, m_Alloc))
+ // Buffer has been created.
+ if(m_Buf != VK_NULL_HANDLE)
{
- // It's all OK - safe to use m_Buf.
- return;
+ // Check if its allocation is not lost + mark it as used in current frame.
+ if(vmaTouchAllocation(allocator, m_Alloc))
+ {
+ // It's all OK - safe to use m_Buf.
+ return;
+ }
}
- }
- // Buffer not yet exists or lost - destroy and recreate it.
+ // Buffer not yet exists or lost - destroy and recreate it.
- vmaDestroyBuffer(allocator, m_Buf, m_Alloc);
+ vmaDestroyBuffer(allocator, m_Buf, m_Alloc);
- VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
- bufCreateInfo.size = 1024;
- bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
+ VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
+ bufCreateInfo.size = 1024;
+ bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
- VmaAllocationCreateInfo allocCreateInfo = {};
- allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
- allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT |
- VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
+ VmaAllocationCreateInfo allocCreateInfo = {};
+ allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
+ allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT |
+ VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
- vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &m_Buf, &m_Alloc, nullptr);
+ vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &m_Buf, &m_Alloc, nullptr);
}
\endcode
@@ -1189,6 +1269,9 @@ printf("Image name: %s\n", imageName);
That string is also printed in JSON report created by vmaBuildStatsString().
+\note Passing string name to VMA allocation doesn't automatically set it to the Vulkan buffer or image created with it.
+You must do it manually using an extension like VK_EXT_debug_utils, which is independent of this library.
+
\page debugging_memory_usage Debugging incorrect memory usage
@@ -1280,7 +1363,7 @@ which indicates a serious bug.
You can also explicitly request checking margins of all allocations in all memory blocks
that belong to specified memory types by using function vmaCheckCorruption(),
-or in memory blocks that belong to specified custom pool, by using function
+or in memory blocks that belong to specified custom pool, by using function
vmaCheckPoolCorruption().
Margin validation (corruption detection) works only for memory types that are
@@ -1304,6 +1387,13 @@ application. It can be useful to:
\section record_and_replay_usage Usage
+Recording functionality is disabled by default.
+To enable it, define following macro before every include of this library:
+
+\code
+#define VMA_RECORDING_ENABLED 1
+\endcode
+
To record sequence of calls to a file: Fill in
VmaAllocatorCreateInfo::pRecordSettings member while creating #VmaAllocator
object. File is opened and written during whole lifetime of the allocator.
@@ -1315,7 +1405,7 @@ Its project is generated by Premake.
Command line syntax is printed when the program is launched without parameters.
Basic usage:
- VmaReplay.exe MyRecording.csv
+ VmaReplay.exe MyRecording.csv
Documentation of file format can be found in file: "docs/Recording file format.md".
It's a human-readable, text file in CSV format (Comma Separated Values).
@@ -1330,7 +1420,6 @@ It's a human-readable, text file in CSV format (Comma Separated Values).
coded and tested only on Windows. Inclusion of recording code is driven by
`VMA_RECORDING_ENABLED` macro. Support for other platforms should be easy to
add. Contributions are welcomed.
-- Currently calls to vmaDefragment() function are not recorded.
\page usage_patterns Recommended usage patterns
@@ -1339,6 +1428,27 @@ See also slides from talk:
[Sawicki, Adam. Advanced Graphics Techniques Tutorial: Memory management in Vulkan and DX12. Game Developers Conference, 2018](https://www.gdcvault.com/play/1025458/Advanced-Graphics-Techniques-Tutorial-New)
+\section usage_patterns_common_mistakes Common mistakes
+
+Use of CPU_TO_GPU instead of CPU_ONLY memory
+
+#VMA_MEMORY_USAGE_CPU_TO_GPU is recommended only for resources that will be
+mapped and written by the CPU, as well as read directly by the GPU - like some
+buffers or textures updated every frame (dynamic). If you create a staging copy
+of a resource to be written by CPU and then used as a source of transfer to
+another resource placed in the GPU memory, that staging resource should be
+created with #VMA_MEMORY_USAGE_CPU_ONLY. Please read the descriptions of these
+enums carefully for details.
+
+Unnecessary use of custom pools
+
+\ref custom_memory_pools may be useful for special purposes - when you want to
+keep certain type of resources separate e.g. to reserve minimum amount of memory
+for them, limit maximum amount of memory they can occupy, or make some of them
+push out the other through the mechanism of \ref lost_allocations. For most
+resources this is not needed and so it is not recommended to create #VmaPool
+objects and allocations out of them. Allocating from the default pool is sufficient.
+
\section usage_patterns_simple Simple patterns
\subsection usage_patterns_simple_render_targets Render targets
@@ -1472,6 +1582,11 @@ mutex, atomic etc.
The library uses its own implementation of containers by default, but you can switch to using
STL containers instead.
+For example, define `VMA_ASSERT(expr)` before including the library to provide
+custom implementation of the assertion, compatible with your project.
+By default it is defined to standard C `assert(expr)` in `_DEBUG` configuration
+and empty otherwise.
+
\section config_Vulkan_functions Pointers to Vulkan functions
The library uses Vulkan functions straight from the `vulkan.h` header by default.
@@ -1505,7 +1620,7 @@ behavior is implementation-dependant - it depends on GPU vendor and graphics
driver.
On AMD cards it can be controlled while creating Vulkan device object by using
-VK_AMD_memory_allocation_behavior extension, if available.
+VK_AMD_memory_overallocation_behavior extension, if available.
Alternatively, if you want to test how your program behaves with limited amount of Vulkan device
memory available without switching your graphics card to one that really has
@@ -1550,7 +1665,7 @@ buffer using vmaCreateBuffer() or image using vmaCreateImage().
When using the extension together with Vulkan Validation Layer, you will receive
warnings like this:
- vkBindBufferMemory(): Binding memory to buffer 0x33 but vkGetBufferMemoryRequirements() has not been called on that buffer.
+ vkBindBufferMemory(): Binding memory to buffer 0x33 but vkGetBufferMemoryRequirements() has not been called on that buffer.
It is OK, you should just ignore it. It happens because you use function
`vkGetBufferMemoryRequirements2KHR()` instead of standard
@@ -1559,11 +1674,68 @@ unaware of it.
To learn more about this extension, see:
-- [VK_KHR_dedicated_allocation in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#VK_KHR_dedicated_allocation)
+- [VK_KHR_dedicated_allocation in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap44.html#VK_KHR_dedicated_allocation)
- [VK_KHR_dedicated_allocation unofficial manual](http://asawicki.info/articles/VK_KHR_dedicated_allocation.php5)
+\page vk_amd_device_coherent_memory VK_AMD_device_coherent_memory
+
+VK_AMD_device_coherent_memory is a device extension that enables access to
+additional memory types with `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and
+`VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flag. It is useful mostly for
+allocation of buffers intended for writing "breadcrumb markers" in between passes
+or draw calls, which in turn are useful for debugging GPU crash/hang/TDR cases.
+
+When the extension is available but has not been enabled, Vulkan physical device
+still exposes those memory types, but their usage is forbidden. VMA automatically
+takes care of that - it returns `VK_ERROR_FEATURE_NOT_PRESENT` when an attempt
+to allocate memory of such type is made.
+
+If you want to use this extension in connection with VMA, follow these steps:
+
+\section vk_amd_device_coherent_memory_initialization Initialization
+
+1) Call `vkEnumerateDeviceExtensionProperties` for the physical device.
+Check if the extension is supported - if returned array of `VkExtensionProperties` contains "VK_AMD_device_coherent_memory".
+
+2) Call `vkGetPhysicalDeviceFeatures2` for the physical device instead of old `vkGetPhysicalDeviceFeatures`.
+Attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to `VkPhysicalDeviceFeatures2::pNext` to be returned.
+Check if the device feature is really supported - check if `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true.
+
+3) While creating device with `vkCreateDevice`, enable this extension - add "VK_AMD_device_coherent_memory"
+to the list passed as `VkDeviceCreateInfo::ppEnabledExtensionNames`.
+
+4) While creating the device, also don't set `VkDeviceCreateInfo::pEnabledFeatures`.
+Fill in `VkPhysicalDeviceFeatures2` structure instead and pass it as `VkDeviceCreateInfo::pNext`.
+Enable this device feature - attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to
+`VkPhysicalDeviceFeatures2::pNext` and set its member `deviceCoherentMemory` to `VK_TRUE`.
+
+5) While creating #VmaAllocator with vmaCreateAllocator() inform VMA that you
+have enabled this extension and feature - add #VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT
+to VmaAllocatorCreateInfo::flags.
+
+\section vk_amd_device_coherent_memory_usage Usage
+
+After following steps described above, you can create VMA allocations and custom pools
+out of the special `DEVICE_COHERENT` and `DEVICE_UNCACHED` memory types on eligible
+devices. There are multiple ways to do it, for example:
+
+- You can request or prefer to allocate out of such memory types by adding
+ `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` to VmaAllocationCreateInfo::requiredFlags
+ or VmaAllocationCreateInfo::preferredFlags. Those flags can be freely mixed with
+ other ways of \ref choosing_memory_type, like setting VmaAllocationCreateInfo::usage.
+- If you manually found memory type index to use for this purpose, force allocation
+ from this specific index by setting VmaAllocationCreateInfo::memoryTypeBits `= 1u << index`.
+
+\section vk_amd_device_coherent_memory_more_information More information
+
+To learn more about this extension, see [VK_AMD_device_coherent_memory in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap44.html#VK_AMD_device_coherent_memory)
+
+Example use of this extension can be found in the code of the sample and test suite
+accompanying this library.
+
+
\page general_considerations General considerations
\section general_considerations_thread_safety Thread safety
@@ -1590,14 +1762,14 @@ to just ignore them.
- *vkBindBufferMemory(): Binding memory to buffer 0xeb8e4 but vkGetBufferMemoryRequirements() has not been called on that buffer.*
- It happens when VK_KHR_dedicated_allocation extension is enabled.
- `vkGetBufferMemoryRequirements2KHR` function is used instead, while validation layer seems to be unaware of it.
+ `vkGetBufferMemoryRequirements2KHR` function is used instead, while validation layer seems to be unaware of it.
- *Mapping an image with layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL can result in undefined behavior if this memory is used by the device. Only GENERAL or PREINITIALIZED should be used.*
- It happens when you map a buffer or image, because the library maps entire
- `VkDeviceMemory` block, where different types of images and buffers may end
- up together, especially on GPUs with unified memory like Intel.
+ `VkDeviceMemory` block, where different types of images and buffers may end
+ up together, especially on GPUs with unified memory like Intel.
- *Non-linear image 0xebc91 is aliased with linear buffer 0xeb8e4 which may indicate a bug.*
- It happens when you use lost allocations, and a new image or buffer is
- created in place of an existing object that bacame lost.
+ created in place of an existing object that bacame lost.
- It may happen also when you use [defragmentation](@ref defragmentation).
\section general_considerations_allocation_algorithm Allocation algorithm
@@ -1639,7 +1811,7 @@ Features deliberately excluded from the scope of this library:
and handled gracefully, because that would complicate code significantly and
is usually not needed in desktop PC applications anyway.
- Code free of any compiler warnings. Maintaining the library to compile and
- work correctly on so many different platforms is hard enough. Being free of
+ work correctly on so many different platforms is hard enough. Being free of
any warnings, on any version of any compiler, is simply not feasible.
- This is a C++ library with C interface.
Bindings or ports to any other programming languages are welcomed as external projects and
@@ -1652,31 +1824,66 @@ Define this macro to 0/1 to disable/enable support for recording functionality,
available through VmaAllocatorCreateInfo::pRecordSettings.
*/
#ifndef VMA_RECORDING_ENABLED
-#ifdef _WIN32
-#define VMA_RECORDING_ENABLED 1
-#else
-#define VMA_RECORDING_ENABLED 0
-#endif
+ #define VMA_RECORDING_ENABLED 0
#endif
#ifndef NOMINMAX
-#define NOMINMAX // For windows.h
+ #define NOMINMAX // For windows.h
#endif
#ifndef VULKAN_H_
-#include
+ #include
#endif
#if VMA_RECORDING_ENABLED
-#include
+ #include
+#endif
+
+// Define this macro to declare maximum supported Vulkan version in format AAABBBCCC,
+// where AAA = major, BBB = minor, CCC = patch.
+// If you want to use version > 1.0, it still needs to be enabled via VmaAllocatorCreateInfo::vulkanApiVersion.
+#if !defined(VMA_VULKAN_VERSION)
+ #if defined(VK_VERSION_1_1)
+ #define VMA_VULKAN_VERSION 1001000
+ #else
+ #define VMA_VULKAN_VERSION 1000000
+ #endif
#endif
#if !defined(VMA_DEDICATED_ALLOCATION)
-#if VK_KHR_get_memory_requirements2 && VK_KHR_dedicated_allocation
-#define VMA_DEDICATED_ALLOCATION 1
-#else
-#define VMA_DEDICATED_ALLOCATION 0
+ #if VK_KHR_get_memory_requirements2 && VK_KHR_dedicated_allocation
+ #define VMA_DEDICATED_ALLOCATION 1
+ #else
+ #define VMA_DEDICATED_ALLOCATION 0
+ #endif
#endif
+
+#if !defined(VMA_BIND_MEMORY2)
+ #if VK_KHR_bind_memory2
+ #define VMA_BIND_MEMORY2 1
+ #else
+ #define VMA_BIND_MEMORY2 0
+ #endif
+#endif
+
+#if !defined(VMA_MEMORY_BUDGET)
+ #if VK_EXT_memory_budget && (VK_KHR_get_physical_device_properties2 || VMA_VULKAN_VERSION >= 1001000)
+ #define VMA_MEMORY_BUDGET 1
+ #else
+ #define VMA_MEMORY_BUDGET 0
+ #endif
+#endif
+
+// Define these macros to decorate all public functions with additional code,
+// before and after returned type, appropriately. This may be useful for
+// exporing the functions when compiling VMA as a separate library. Example:
+// #define VMA_CALL_PRE __declspec(dllexport)
+// #define VMA_CALL_POST __cdecl
+#ifndef VMA_CALL_PRE
+ #define VMA_CALL_PRE
+#endif
+#ifndef VMA_CALL_POST
+ #define VMA_CALL_POST
#endif
/** \struct VmaAllocator
@@ -1691,17 +1898,17 @@ right after Vulkan is initialized and keep it alive until before Vulkan device i
VK_DEFINE_HANDLE(VmaAllocator)
/// Callback function called after successful vkAllocateMemory.
-typedef void(VKAPI_PTR *PFN_vmaAllocateDeviceMemoryFunction)(
- VmaAllocator allocator,
- uint32_t memoryType,
- VkDeviceMemory memory,
- VkDeviceSize size);
+typedef void (VKAPI_PTR *PFN_vmaAllocateDeviceMemoryFunction)(
+ VmaAllocator allocator,
+ uint32_t memoryType,
+ VkDeviceMemory memory,
+ VkDeviceSize size);
/// Callback function called before vkFreeMemory.
-typedef void(VKAPI_PTR *PFN_vmaFreeDeviceMemoryFunction)(
- VmaAllocator allocator,
- uint32_t memoryType,
- VkDeviceMemory memory,
- VkDeviceSize size);
+typedef void (VKAPI_PTR *PFN_vmaFreeDeviceMemoryFunction)(
+ VmaAllocator allocator,
+ uint32_t memoryType,
+ VkDeviceMemory memory,
+ VkDeviceSize size);
/** \brief Set of callbacks that the library will call for `vkAllocateMemory` and `vkFreeMemory`.
@@ -1721,31 +1928,79 @@ typedef struct VmaDeviceMemoryCallbacks {
typedef enum VmaAllocatorCreateFlagBits {
/** \brief Allocator and all objects created from it will not be synchronized internally, so you must guarantee they are used from only one thread at a time or synchronized externally by you.
- Using this flag may increase performance because internal mutexes are not used.
- */
+ Using this flag may increase performance because internal mutexes are not used.
+ */
VMA_ALLOCATOR_CREATE_EXTERNALLY_SYNCHRONIZED_BIT = 0x00000001,
/** \brief Enables usage of VK_KHR_dedicated_allocation extension.
- Using this extenion will automatically allocate dedicated blocks of memory for
- some buffers and images instead of suballocating place for them out of bigger
- memory blocks (as if you explicitly used #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT
- flag) when it is recommended by the driver. It may improve performance on some
- GPUs.
+ The flag works only if VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_0`.
+ When it's `VK_API_VERSION_1_1`, the flag is ignored because the extension has been promoted to Vulkan 1.1.
- You may set this flag only if you found out that following device extensions are
- supported, you enabled them while creating Vulkan device passed as
- VmaAllocatorCreateInfo::device, and you want them to be used internally by this
- library:
+ Using this extenion will automatically allocate dedicated blocks of memory for
+ some buffers and images instead of suballocating place for them out of bigger
+ memory blocks (as if you explicitly used #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT
+ flag) when it is recommended by the driver. It may improve performance on some
+ GPUs.
- - VK_KHR_get_memory_requirements2
- - VK_KHR_dedicated_allocation
+ You may set this flag only if you found out that following device extensions are
+ supported, you enabled them while creating Vulkan device passed as
+ VmaAllocatorCreateInfo::device, and you want them to be used internally by this
+ library:
-When this flag is set, you can experience following warnings reported by Vulkan
-validation layer. You can ignore them.
+ - VK_KHR_get_memory_requirements2 (device extension)
+ - VK_KHR_dedicated_allocation (device extension)
-> vkBindBufferMemory(): Binding memory to buffer 0x2d but vkGetBufferMemoryRequirements() has not been called on that buffer.
- */
+ When this flag is set, you can experience following warnings reported by Vulkan
+ validation layer. You can ignore them.
+
+ > vkBindBufferMemory(): Binding memory to buffer 0x2d but vkGetBufferMemoryRequirements() has not been called on that buffer.
+ */
VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT = 0x00000002,
+ /**
+ Enables usage of VK_KHR_bind_memory2 extension.
+
+ The flag works only if VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_0`.
+ When it's `VK_API_VERSION_1_1`, the flag is ignored because the extension has been promoted to Vulkan 1.1.
+
+ You may set this flag only if you found out that this device extension is supported,
+ you enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,
+ and you want it to be used internally by this library.
+
+ The extension provides functions `vkBindBufferMemory2KHR` and `vkBindImageMemory2KHR`,
+ which allow to pass a chain of `pNext` structures while binding.
+ This flag is required if you use `pNext` parameter in vmaBindBufferMemory2() or vmaBindImageMemory2().
+ */
+ VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT = 0x00000004,
+ /**
+ Enables usage of VK_EXT_memory_budget extension.
+
+ You may set this flag only if you found out that this device extension is supported,
+ you enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,
+ and you want it to be used internally by this library, along with another instance extension
+ VK_KHR_get_physical_device_properties2, which is required by it (or Vulkan 1.1, where this extension is promoted).
+
+ The extension provides query for current memory usage and budget, which will probably
+ be more accurate than an estimation used by the library otherwise.
+ */
+ VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT = 0x00000008,
+ /**
+ Enabled usage of VK_AMD_device_coherent_memory extension.
+
+ You may set this flag only if you:
+
+ - found out that this device extension is supported and enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device,
+ - checked that `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true and set it while creating the Vulkan device,
+ - want it to be used internally by this library.
+
+ The extension and accompanying device feature provide access to memory types with
+ `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and `VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flags.
+ They are useful mostly for writing breadcrumb markers - a common method for debugging GPU crash/hang/TDR.
+
+ When the extension is not enabled, such memory types are still enumerated, but their usage is illegal.
+ To protect from this error, if you don't create the allocator with this flag, it will refuse to allocate any memory or create a custom pool in such memory type,
+ returning `VK_ERROR_FEATURE_NOT_PRESENT`.
+ */
+ VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT = 0x00000010,
VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaAllocatorCreateFlagBits;
@@ -1773,41 +2028,50 @@ typedef struct VmaVulkanFunctions {
PFN_vkCreateImage vkCreateImage;
PFN_vkDestroyImage vkDestroyImage;
PFN_vkCmdCopyBuffer vkCmdCopyBuffer;
-#if VMA_DEDICATED_ALLOCATION
+#if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000
PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR;
PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR;
#endif
+#if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000
+ PFN_vkBindBufferMemory2KHR vkBindBufferMemory2KHR;
+ PFN_vkBindImageMemory2KHR vkBindImageMemory2KHR;
+#endif
+#if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000
+ PFN_vkGetPhysicalDeviceMemoryProperties2KHR vkGetPhysicalDeviceMemoryProperties2KHR;
+#endif
} VmaVulkanFunctions;
/// Flags to be used in VmaRecordSettings::flags.
typedef enum VmaRecordFlagBits {
/** \brief Enables flush after recording every function call.
- Enable it if you expect your application to crash, which may leave recording file truncated.
- It may degrade performance though.
- */
+ Enable it if you expect your application to crash, which may leave recording file truncated.
+ It may degrade performance though.
+ */
VMA_RECORD_FLUSH_AFTER_CALL_BIT = 0x00000001,
-
+
VMA_RECORD_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaRecordFlagBits;
typedef VkFlags VmaRecordFlags;
/// Parameters for recording calls to VMA functions. To be used in VmaAllocatorCreateInfo::pRecordSettings.
-typedef struct VmaRecordSettings {
+typedef struct VmaRecordSettings
+{
/// Flags for recording. Use #VmaRecordFlagBits enum.
VmaRecordFlags flags;
/** \brief Path to the file that should be written by the recording.
- Suggested extension: "csv".
- If the file already exists, it will be overwritten.
- It will be opened for the whole time #VmaAllocator object is alive.
- If opening this file fails, creation of the whole allocator object fails.
- */
- const char *pFilePath;
+ Suggested extension: "csv".
+ If the file already exists, it will be overwritten.
+ It will be opened for the whole time #VmaAllocator object is alive.
+ If opening this file fails, creation of the whole allocator object fails.
+ */
+ const char* pFilePath;
} VmaRecordSettings;
/// Description of a Allocator to be created.
-typedef struct VmaAllocatorCreateInfo {
+typedef struct VmaAllocatorCreateInfo
+{
/// Flags for created allocator. Use #VmaAllocatorCreateFlagBits enum.
VmaAllocatorCreateFlags flags;
/// Vulkan physical device.
@@ -1821,94 +2085,109 @@ typedef struct VmaAllocatorCreateInfo {
VkDeviceSize preferredLargeHeapBlockSize;
/// Custom CPU memory allocation callbacks. Optional.
/** Optional, can be null. When specified, will also be used for all CPU-side memory allocations. */
- const VkAllocationCallbacks *pAllocationCallbacks;
+ const VkAllocationCallbacks* pAllocationCallbacks;
/// Informative callbacks for `vkAllocateMemory`, `vkFreeMemory`. Optional.
/** Optional, can be null. */
- const VmaDeviceMemoryCallbacks *pDeviceMemoryCallbacks;
+ const VmaDeviceMemoryCallbacks* pDeviceMemoryCallbacks;
/** \brief Maximum number of additional frames that are in use at the same time as current frame.
- This value is used only when you make allocations with
- VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become
- lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.
+ This value is used only when you make allocations with
+ VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become
+ lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.
- For example, if you double-buffer your command buffers, so resources used for
- rendering in previous frame may still be in use by the GPU at the moment you
- allocate resources needed for the current frame, set this value to 1.
+ For example, if you double-buffer your command buffers, so resources used for
+ rendering in previous frame may still be in use by the GPU at the moment you
+ allocate resources needed for the current frame, set this value to 1.
- If you want to allow any allocations other than used in the current frame to
- become lost, set this value to 0.
- */
+ If you want to allow any allocations other than used in the current frame to
+ become lost, set this value to 0.
+ */
uint32_t frameInUseCount;
/** \brief Either null or a pointer to an array of limits on maximum number of bytes that can be allocated out of particular Vulkan memory heap.
- If not NULL, it must be a pointer to an array of
- `VkPhysicalDeviceMemoryProperties::memoryHeapCount` elements, defining limit on
- maximum number of bytes that can be allocated out of particular Vulkan memory
- heap.
+ If not NULL, it must be a pointer to an array of
+ `VkPhysicalDeviceMemoryProperties::memoryHeapCount` elements, defining limit on
+ maximum number of bytes that can be allocated out of particular Vulkan memory
+ heap.
- Any of the elements may be equal to `VK_WHOLE_SIZE`, which means no limit on that
- heap. This is also the default in case of `pHeapSizeLimit` = NULL.
+ Any of the elements may be equal to `VK_WHOLE_SIZE`, which means no limit on that
+ heap. This is also the default in case of `pHeapSizeLimit` = NULL.
- If there is a limit defined for a heap:
+ If there is a limit defined for a heap:
- - If user tries to allocate more memory from that heap using this allocator,
- the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
- - If the limit is smaller than heap size reported in `VkMemoryHeap::size`, the
- value of this limit will be reported instead when using vmaGetMemoryProperties().
+ - If user tries to allocate more memory from that heap using this allocator,
+ the allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
+ - If the limit is smaller than heap size reported in `VkMemoryHeap::size`, the
+ value of this limit will be reported instead when using vmaGetMemoryProperties().
- Warning! Using this feature may not be equivalent to installing a GPU with
- smaller amount of memory, because graphics driver doesn't necessary fail new
- allocations with `VK_ERROR_OUT_OF_DEVICE_MEMORY` result when memory capacity is
- exceeded. It may return success and just silently migrate some device memory
- blocks to system RAM. This driver behavior can also be controlled using
- VK_AMD_memory_overallocation_behavior extension.
- */
- const VkDeviceSize *pHeapSizeLimit;
+ Warning! Using this feature may not be equivalent to installing a GPU with
+ smaller amount of memory, because graphics driver doesn't necessary fail new
+ allocations with `VK_ERROR_OUT_OF_DEVICE_MEMORY` result when memory capacity is
+ exceeded. It may return success and just silently migrate some device memory
+ blocks to system RAM. This driver behavior can also be controlled using
+ VK_AMD_memory_overallocation_behavior extension.
+ */
+ const VkDeviceSize* pHeapSizeLimit;
/** \brief Pointers to Vulkan functions. Can be null if you leave define `VMA_STATIC_VULKAN_FUNCTIONS 1`.
- If you leave define `VMA_STATIC_VULKAN_FUNCTIONS 1` in configuration section,
- you can pass null as this member, because the library will fetch pointers to
- Vulkan functions internally in a static way, like:
+ If you leave define `VMA_STATIC_VULKAN_FUNCTIONS 1` in configuration section,
+ you can pass null as this member, because the library will fetch pointers to
+ Vulkan functions internally in a static way, like:
- vulkanFunctions.vkAllocateMemory = &vkAllocateMemory;
+ vulkanFunctions.vkAllocateMemory = &vkAllocateMemory;
- Fill this member if you want to provide your own pointers to Vulkan functions,
- e.g. fetched using `vkGetInstanceProcAddr()` and `vkGetDeviceProcAddr()`.
- */
- const VmaVulkanFunctions *pVulkanFunctions;
+ Fill this member if you want to provide your own pointers to Vulkan functions,
+ e.g. fetched using `vkGetInstanceProcAddr()` and `vkGetDeviceProcAddr()`.
+ */
+ const VmaVulkanFunctions* pVulkanFunctions;
/** \brief Parameters for recording of VMA calls. Can be null.
- If not null, it enables recording of calls to VMA functions to a file.
- If support for recording is not enabled using `VMA_RECORDING_ENABLED` macro,
- creation of the allocator object fails with `VK_ERROR_FEATURE_NOT_PRESENT`.
- */
- const VmaRecordSettings *pRecordSettings;
+ If not null, it enables recording of calls to VMA functions to a file.
+ If support for recording is not enabled using `VMA_RECORDING_ENABLED` macro,
+ creation of the allocator object fails with `VK_ERROR_FEATURE_NOT_PRESENT`.
+ */
+ const VmaRecordSettings* pRecordSettings;
+ /** \brief Optional handle to Vulkan instance object.
+
+ Optional, can be null. Must be set if #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT flas is used
+ or if `vulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)`.
+ */
+ VkInstance instance;
+ /** \brief Optional. The highest version of Vulkan that the application is designed to use.
+
+ It must be a value in the format as created by macro `VK_MAKE_VERSION` or a constant like: `VK_API_VERSION_1_1`, `VK_API_VERSION_1_0`.
+ The patch version number specified is ignored. Only the major and minor versions are considered.
+ It must be less or euqal (preferably equal) to value as passed to `vkCreateInstance` as `VkApplicationInfo::apiVersion`.
+ Only versions 1.0 and 1.1 are supported by the current implementation.
+ Leaving it initialized to zero is equivalent to `VK_API_VERSION_1_0`.
+ */
+ uint32_t vulkanApiVersion;
} VmaAllocatorCreateInfo;
/// Creates Allocator object.
-VkResult vmaCreateAllocator(
- const VmaAllocatorCreateInfo *pCreateInfo,
- VmaAllocator *pAllocator);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateAllocator(
+ const VmaAllocatorCreateInfo* pCreateInfo,
+ VmaAllocator* pAllocator);
/// Destroys allocator object.
-void vmaDestroyAllocator(
- VmaAllocator allocator);
+VMA_CALL_PRE void VMA_CALL_POST vmaDestroyAllocator(
+ VmaAllocator allocator);
/**
PhysicalDeviceProperties are fetched from physicalDevice by the allocator.
You can access it here, without fetching it again on your own.
*/
-void vmaGetPhysicalDeviceProperties(
- VmaAllocator allocator,
- const VkPhysicalDeviceProperties **ppPhysicalDeviceProperties);
+VMA_CALL_PRE void VMA_CALL_POST vmaGetPhysicalDeviceProperties(
+ VmaAllocator allocator,
+ const VkPhysicalDeviceProperties** ppPhysicalDeviceProperties);
/**
PhysicalDeviceMemoryProperties are fetched from physicalDevice by the allocator.
You can access it here, without fetching it again on your own.
*/
-void vmaGetMemoryProperties(
- VmaAllocator allocator,
- const VkPhysicalDeviceMemoryProperties **ppPhysicalDeviceMemoryProperties);
+VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryProperties(
+ VmaAllocator allocator,
+ const VkPhysicalDeviceMemoryProperties** ppPhysicalDeviceMemoryProperties);
/**
\brief Given Memory Type Index, returns Property Flags of this memory type.
@@ -1916,10 +2195,10 @@ void vmaGetMemoryProperties(
This is just a convenience function. Same information can be obtained using
vmaGetMemoryProperties().
*/
-void vmaGetMemoryTypeProperties(
- VmaAllocator allocator,
- uint32_t memoryTypeIndex,
- VkMemoryPropertyFlags *pFlags);
+VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryTypeProperties(
+ VmaAllocator allocator,
+ uint32_t memoryTypeIndex,
+ VkMemoryPropertyFlags* pFlags);
/** \brief Sets index of the current frame.
@@ -1929,13 +2208,14 @@ This function must be used if you make allocations with
when a new frame begins. Allocations queried using vmaGetAllocationInfo() cannot
become lost in the current frame.
*/
-void vmaSetCurrentFrameIndex(
- VmaAllocator allocator,
- uint32_t frameIndex);
+VMA_CALL_PRE void VMA_CALL_POST vmaSetCurrentFrameIndex(
+ VmaAllocator allocator,
+ uint32_t frameIndex);
/** \brief Calculated statistics of memory usage in entire allocator.
*/
-typedef struct VmaStatInfo {
+typedef struct VmaStatInfo
+{
/// Number of `VkDeviceMemory` Vulkan memory blocks allocated.
uint32_t blockCount;
/// Number of #VmaAllocation allocation objects allocated.
@@ -1951,16 +2231,80 @@ typedef struct VmaStatInfo {
} VmaStatInfo;
/// General statistics from current state of Allocator.
-typedef struct VmaStats {
+typedef struct VmaStats
+{
VmaStatInfo memoryType[VK_MAX_MEMORY_TYPES];
VmaStatInfo memoryHeap[VK_MAX_MEMORY_HEAPS];
VmaStatInfo total;
} VmaStats;
-/// Retrieves statistics from current state of the Allocator.
-void vmaCalculateStats(
- VmaAllocator allocator,
- VmaStats *pStats);
+/** \brief Retrieves statistics from current state of the Allocator.
+
+This function is called "calculate" not "get" because it has to traverse all
+internal data structures, so it may be quite slow. For faster but more brief statistics
+suitable to be called every frame or every allocation, use vmaGetBudget().
+
+Note that when using allocator from multiple threads, returned information may immediately
+become outdated.
+*/
+VMA_CALL_PRE void VMA_CALL_POST vmaCalculateStats(
+ VmaAllocator allocator,
+ VmaStats* pStats);
+
+/** \brief Statistics of current memory usage and available budget, in bytes, for specific memory heap.
+*/
+typedef struct VmaBudget
+{
+ /** \brief Sum size of all `VkDeviceMemory` blocks allocated from particular heap, in bytes.
+ */
+ VkDeviceSize blockBytes;
+
+ /** \brief Sum size of all allocations created in particular heap, in bytes.
+
+ Usually less or equal than `blockBytes`.
+ Difference `blockBytes - allocationBytes` is the amount of memory allocated but unused -
+ available for new allocations or wasted due to fragmentation.
+
+ It might be greater than `blockBytes` if there are some allocations in lost state, as they account
+ to this value as well.
+ */
+ VkDeviceSize allocationBytes;
+
+ /** \brief Estimated current memory usage of the program, in bytes.
+
+ Fetched from system using `VK_EXT_memory_budget` extension if enabled.
+
+ It might be different than `blockBytes` (usually higher) due to additional implicit objects
+ also occupying the memory, like swapchain, pipelines, descriptor heaps, command buffers, or
+ `VkDeviceMemory` blocks allocated outside of this library, if any.
+ */
+ VkDeviceSize usage;
+
+ /** \brief Estimated amount of memory available to the program, in bytes.
+
+ Fetched from system using `VK_EXT_memory_budget` extension if enabled.
+
+ It might be different (most probably smaller) than `VkMemoryHeap::size[heapIndex]` due to factors
+ external to the program, like other programs also consuming system resources.
+ Difference `budget - usage` is the amount of additional memory that can probably
+ be allocated without problems. Exceeding the budget may result in various problems.
+ */
+ VkDeviceSize budget;
+} VmaBudget;
+
+/** \brief Retrieves information about current memory budget for all memory heaps.
+
+\param[out] pBudget Must point to array with number of elements at least equal to number of memory heaps in physical device used.
+
+This function is called "get" not "calculate" because it is very fast, suitable to be called
+every frame or every allocation. For more detailed statistics use vmaCalculateStats().
+
+Note that when using allocator from multiple threads, returned information may immediately
+become outdated.
+*/
+VMA_CALL_PRE void VMA_CALL_POST vmaGetBudget(
+ VmaAllocator allocator,
+ VmaBudget* pBudget);
#ifndef VMA_STATS_STRING_ENABLED
#define VMA_STATS_STRING_ENABLED 1
@@ -1971,14 +2315,14 @@ void vmaCalculateStats(
/// Builds and returns statistics as string in JSON format.
/** @param[out] ppStatsString Must be freed using vmaFreeStatsString() function.
*/
-void vmaBuildStatsString(
- VmaAllocator allocator,
- char **ppStatsString,
- VkBool32 detailedMap);
+VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString(
+ VmaAllocator allocator,
+ char** ppStatsString,
+ VkBool32 detailedMap);
-void vmaFreeStatsString(
- VmaAllocator allocator,
- char *pStatsString);
+VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString(
+ VmaAllocator allocator,
+ char* pStatsString);
#endif // #if VMA_STATS_STRING_ENABLED
@@ -1992,205 +2336,226 @@ For more information see [Custom memory pools](@ref choosing_memory_type_custom_
*/
VK_DEFINE_HANDLE(VmaPool)
-typedef enum VmaMemoryUsage {
+typedef enum VmaMemoryUsage
+{
/** No intended memory usage specified.
- Use other members of VmaAllocationCreateInfo to specify your requirements.
- */
+ Use other members of VmaAllocationCreateInfo to specify your requirements.
+ */
VMA_MEMORY_USAGE_UNKNOWN = 0,
/** Memory will be used on device only, so fast access from the device is preferred.
- It usually means device-local GPU (video) memory.
- No need to be mappable on host.
- It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`.
+ It usually means device-local GPU (video) memory.
+ No need to be mappable on host.
+ It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`.
- Usage:
+ Usage:
+
+ - Resources written and read by device, e.g. images used as attachments.
+ - Resources transferred from host once (immutable) or infrequently and read by
+ device multiple times, e.g. textures to be sampled, vertex buffers, uniform
+ (constant) buffers, and majority of other types of resources used on GPU.
- - Resources written and read by device, e.g. images used as attachments.
- - Resources transferred from host once (immutable) or infrequently and read by
- device multiple times, e.g. textures to be sampled, vertex buffers, uniform
- (constant) buffers, and majority of other types of resources used on GPU.
-
- Allocation may still end up in `HOST_VISIBLE` memory on some implementations.
- In such case, you are free to map it.
- You can use #VMA_ALLOCATION_CREATE_MAPPED_BIT with this usage type.
- */
+ Allocation may still end up in `HOST_VISIBLE` memory on some implementations.
+ In such case, you are free to map it.
+ You can use #VMA_ALLOCATION_CREATE_MAPPED_BIT with this usage type.
+ */
VMA_MEMORY_USAGE_GPU_ONLY = 1,
/** Memory will be mappable on host.
- It usually means CPU (system) memory.
- Guarantees to be `HOST_VISIBLE` and `HOST_COHERENT`.
- CPU access is typically uncached. Writes may be write-combined.
- Resources created in this pool may still be accessible to the device, but access to them can be slow.
- It is roughly equivalent of `D3D12_HEAP_TYPE_UPLOAD`.
+ It usually means CPU (system) memory.
+ Guarantees to be `HOST_VISIBLE` and `HOST_COHERENT`.
+ CPU access is typically uncached. Writes may be write-combined.
+ Resources created in this pool may still be accessible to the device, but access to them can be slow.
+ It is roughly equivalent of `D3D12_HEAP_TYPE_UPLOAD`.
- Usage: Staging copy of resources used as transfer source.
- */
+ Usage: Staging copy of resources used as transfer source.
+ */
VMA_MEMORY_USAGE_CPU_ONLY = 2,
/**
- Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU.
- CPU access is typically uncached. Writes may be write-combined.
+ Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU.
+ CPU access is typically uncached. Writes may be write-combined.
- Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call.
- */
+ Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call.
+ */
VMA_MEMORY_USAGE_CPU_TO_GPU = 3,
/** Memory mappable on host (guarantees to be `HOST_VISIBLE`) and cached.
- It is roughly equivalent of `D3D12_HEAP_TYPE_READBACK`.
+ It is roughly equivalent of `D3D12_HEAP_TYPE_READBACK`.
- Usage:
+ Usage:
- - Resources written by device, read by host - results of some computations, e.g. screen capture, average scene luminance for HDR tone mapping.
- - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection.
- */
+ - Resources written by device, read by host - results of some computations, e.g. screen capture, average scene luminance for HDR tone mapping.
+ - Any resources read or accessed randomly on host, e.g. CPU-side copy of vertex buffer used as source of transfer, but also used for collision detection.
+ */
VMA_MEMORY_USAGE_GPU_TO_CPU = 4,
+ /** CPU memory - memory that is preferably not `DEVICE_LOCAL`, but also not guaranteed to be `HOST_VISIBLE`.
+
+ Usage: Staging copy of resources moved from GPU memory to CPU memory as part
+ of custom paging/residency mechanism, to be moved back to GPU memory when needed.
+ */
+ VMA_MEMORY_USAGE_CPU_COPY = 5,
+ /** Lazily allocated GPU memory having `VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT`.
+ Exists mostly on mobile platforms. Using it on desktop PC or other GPUs with no such memory type present will fail the allocation.
+
+ Usage: Memory for transient attachment images (color attachments, depth attachments etc.), created with `VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT`.
+
+ Allocations with this usage are always created as dedicated - it implies #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.
+ */
+ VMA_MEMORY_USAGE_GPU_LAZILY_ALLOCATED = 6,
+
VMA_MEMORY_USAGE_MAX_ENUM = 0x7FFFFFFF
} VmaMemoryUsage;
/// Flags to be passed as VmaAllocationCreateInfo::flags.
typedef enum VmaAllocationCreateFlagBits {
/** \brief Set this flag if the allocation should have its own memory block.
-
- Use it for special, big resources, like fullscreen images used as attachments.
-
- You should not use this flag if VmaAllocationCreateInfo::pool is not null.
- */
+
+ Use it for special, big resources, like fullscreen images used as attachments.
+
+ You should not use this flag if VmaAllocationCreateInfo::pool is not null.
+ */
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT = 0x00000001,
/** \brief Set this flag to only try to allocate from existing `VkDeviceMemory` blocks and never create new such block.
-
- If new allocation cannot be placed in any of the existing blocks, allocation
- fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY` error.
-
- You should not use #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT and
- #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT at the same time. It makes no sense.
-
- If VmaAllocationCreateInfo::pool is not null, this flag is implied and ignored. */
+
+ If new allocation cannot be placed in any of the existing blocks, allocation
+ fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY` error.
+
+ You should not use #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT and
+ #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT at the same time. It makes no sense.
+
+ If VmaAllocationCreateInfo::pool is not null, this flag is implied and ignored. */
VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT = 0x00000002,
/** \brief Set this flag to use a memory that will be persistently mapped and retrieve pointer to it.
+
+ Pointer to mapped memory will be returned through VmaAllocationInfo::pMappedData.
- Pointer to mapped memory will be returned through VmaAllocationInfo::pMappedData.
+ Is it valid to use this flag for allocation made from memory type that is not
+ `HOST_VISIBLE`. This flag is then ignored and memory is not mapped. This is
+ useful if you need an allocation that is efficient to use on GPU
+ (`DEVICE_LOCAL`) and still want to map it directly if possible on platforms that
+ support it (e.g. Intel GPU).
- Is it valid to use this flag for allocation made from memory type that is not
- `HOST_VISIBLE`. This flag is then ignored and memory is not mapped. This is
- useful if you need an allocation that is efficient to use on GPU
- (`DEVICE_LOCAL`) and still want to map it directly if possible on platforms that
- support it (e.g. Intel GPU).
-
- You should not use this flag together with #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT.
- */
+ You should not use this flag together with #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT.
+ */
VMA_ALLOCATION_CREATE_MAPPED_BIT = 0x00000004,
/** Allocation created with this flag can become lost as a result of another
- allocation with #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag, so you
- must check it before use.
+ allocation with #VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT flag, so you
+ must check it before use.
- To check if allocation is not lost, call vmaGetAllocationInfo() and check if
- VmaAllocationInfo::deviceMemory is not `VK_NULL_HANDLE`.
+ To check if allocation is not lost, call vmaGetAllocationInfo() and check if
+ VmaAllocationInfo::deviceMemory is not `VK_NULL_HANDLE`.
- For details about supporting lost allocations, see Lost Allocations
- chapter of User Guide on Main Page.
+ For details about supporting lost allocations, see Lost Allocations
+ chapter of User Guide on Main Page.
- You should not use this flag together with #VMA_ALLOCATION_CREATE_MAPPED_BIT.
- */
+ You should not use this flag together with #VMA_ALLOCATION_CREATE_MAPPED_BIT.
+ */
VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT = 0x00000008,
/** While creating allocation using this flag, other allocations that were
- created with flag #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT can become lost.
+ created with flag #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT can become lost.
- For details about supporting lost allocations, see Lost Allocations
- chapter of User Guide on Main Page.
- */
+ For details about supporting lost allocations, see Lost Allocations
+ chapter of User Guide on Main Page.
+ */
VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT = 0x00000010,
/** Set this flag to treat VmaAllocationCreateInfo::pUserData as pointer to a
- null-terminated string. Instead of copying pointer value, a local copy of the
- string is made and stored in allocation's `pUserData`. The string is automatically
- freed together with the allocation. It is also used in vmaBuildStatsString().
- */
+ null-terminated string. Instead of copying pointer value, a local copy of the
+ string is made and stored in allocation's `pUserData`. The string is automatically
+ freed together with the allocation. It is also used in vmaBuildStatsString().
+ */
VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT = 0x00000020,
/** Allocation will be created from upper stack in a double stack pool.
- This flag is only allowed for custom pools created with #VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT flag.
- */
+ This flag is only allowed for custom pools created with #VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT flag.
+ */
VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT = 0x00000040,
/** Create both buffer/image and allocation, but don't bind them together.
- It is useful when you want to bind yourself to do some more advanced binding, e.g. using some extensions.
- The flag is meaningful only with functions that bind by default: vmaCreateBuffer(), vmaCreateImage().
- Otherwise it is ignored.
- */
+ It is useful when you want to bind yourself to do some more advanced binding, e.g. using some extensions.
+ The flag is meaningful only with functions that bind by default: vmaCreateBuffer(), vmaCreateImage().
+ Otherwise it is ignored.
+ */
VMA_ALLOCATION_CREATE_DONT_BIND_BIT = 0x00000080,
+ /** Create allocation only if additional device memory required for it, if any, won't exceed
+ memory budget. Otherwise return `VK_ERROR_OUT_OF_DEVICE_MEMORY`.
+ */
+ VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT = 0x00000100,
/** Allocation strategy that chooses smallest possible free range for the
- allocation.
- */
- VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT = 0x00010000,
+ allocation.
+ */
+ VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT = 0x00010000,
/** Allocation strategy that chooses biggest possible free range for the
- allocation.
- */
+ allocation.
+ */
VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT = 0x00020000,
/** Allocation strategy that chooses first suitable free range for the
- allocation.
+ allocation.
- "First" doesn't necessarily means the one with smallest offset in memory,
- but rather the one that is easiest and fastest to find.
- */
+ "First" doesn't necessarily means the one with smallest offset in memory,
+ but rather the one that is easiest and fastest to find.
+ */
VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT = 0x00040000,
/** Allocation strategy that tries to minimize memory usage.
- */
+ */
VMA_ALLOCATION_CREATE_STRATEGY_MIN_MEMORY_BIT = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT,
/** Allocation strategy that tries to minimize allocation time.
- */
+ */
VMA_ALLOCATION_CREATE_STRATEGY_MIN_TIME_BIT = VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,
/** Allocation strategy that tries to minimize memory fragmentation.
- */
+ */
VMA_ALLOCATION_CREATE_STRATEGY_MIN_FRAGMENTATION_BIT = VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT,
/** A bit mask to extract only `STRATEGY` bits from entire set of flags.
- */
+ */
VMA_ALLOCATION_CREATE_STRATEGY_MASK =
- VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT |
- VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT |
- VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,
+ VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT |
+ VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT |
+ VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT,
VMA_ALLOCATION_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaAllocationCreateFlagBits;
typedef VkFlags VmaAllocationCreateFlags;
-typedef struct VmaAllocationCreateInfo {
+typedef struct VmaAllocationCreateInfo
+{
/// Use #VmaAllocationCreateFlagBits enum.
VmaAllocationCreateFlags flags;
/** \brief Intended usage of memory.
-
- You can leave #VMA_MEMORY_USAGE_UNKNOWN if you specify memory requirements in other way. \n
- If `pool` is not null, this member is ignored.
- */
+
+ You can leave #VMA_MEMORY_USAGE_UNKNOWN if you specify memory requirements in other way. \n
+ If `pool` is not null, this member is ignored.
+ */
VmaMemoryUsage usage;
/** \brief Flags that must be set in a Memory Type chosen for an allocation.
-
- Leave 0 if you specify memory requirements in other way. \n
- If `pool` is not null, this member is ignored.*/
+
+ Leave 0 if you specify memory requirements in other way. \n
+ If `pool` is not null, this member is ignored.*/
VkMemoryPropertyFlags requiredFlags;
/** \brief Flags that preferably should be set in a memory type chosen for an allocation.
-
- Set to 0 if no additional flags are prefered. \n
- If `pool` is not null, this member is ignored. */
+
+ Set to 0 if no additional flags are prefered. \n
+ If `pool` is not null, this member is ignored. */
VkMemoryPropertyFlags preferredFlags;
/** \brief Bitmask containing one bit set for every memory type acceptable for this allocation.
- Value 0 is equivalent to `UINT32_MAX` - it means any memory type is accepted if
- it meets other requirements specified by this structure, with no further
- restrictions on memory type index. \n
- If `pool` is not null, this member is ignored.
- */
+ Value 0 is equivalent to `UINT32_MAX` - it means any memory type is accepted if
+ it meets other requirements specified by this structure, with no further
+ restrictions on memory type index. \n
+ If `pool` is not null, this member is ignored.
+ */
uint32_t memoryTypeBits;
/** \brief Pool that this allocation should be created in.
- Leave `VK_NULL_HANDLE` to allocate from default pool. If not null, members:
- `usage`, `requiredFlags`, `preferredFlags`, `memoryTypeBits` are ignored.
- */
+ Leave `VK_NULL_HANDLE` to allocate from default pool. If not null, members:
+ `usage`, `requiredFlags`, `preferredFlags`, `memoryTypeBits` are ignored.
+ */
VmaPool pool;
/** \brief Custom general-purpose pointer that will be stored in #VmaAllocation, can be read as VmaAllocationInfo::pUserData and changed using vmaSetAllocationUserData().
-
- If #VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is used, it must be either
- null or pointer to a null-terminated string. The string will be then copied to
- internal buffer, so it doesn't need to be valid after allocation call.
- */
- void *pUserData;
+
+ If #VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is used, it must be either
+ null or pointer to a null-terminated string. The string will be then copied to
+ internal buffer, so it doesn't need to be valid after allocation call.
+ */
+ void* pUserData;
} VmaAllocationCreateInfo;
/**
@@ -2209,11 +2574,11 @@ device doesn't support any memory type with requested features for the specific
type of resource you want to use it for. Please check parameters of your
resource, like image layout (OPTIMAL versus LINEAR) or mip level count.
*/
-VkResult vmaFindMemoryTypeIndex(
- VmaAllocator allocator,
- uint32_t memoryTypeBits,
- const VmaAllocationCreateInfo *pAllocationCreateInfo,
- uint32_t *pMemoryTypeIndex);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex(
+ VmaAllocator allocator,
+ uint32_t memoryTypeBits,
+ const VmaAllocationCreateInfo* pAllocationCreateInfo,
+ uint32_t* pMemoryTypeIndex);
/**
\brief Helps to find memoryTypeIndex, given VkBufferCreateInfo and VmaAllocationCreateInfo.
@@ -2227,11 +2592,11 @@ It is just a convenience function, equivalent to calling:
- `vmaFindMemoryTypeIndex`
- `vkDestroyBuffer`
*/
-VkResult vmaFindMemoryTypeIndexForBufferInfo(
- VmaAllocator allocator,
- const VkBufferCreateInfo *pBufferCreateInfo,
- const VmaAllocationCreateInfo *pAllocationCreateInfo,
- uint32_t *pMemoryTypeIndex);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo(
+ VmaAllocator allocator,
+ const VkBufferCreateInfo* pBufferCreateInfo,
+ const VmaAllocationCreateInfo* pAllocationCreateInfo,
+ uint32_t* pMemoryTypeIndex);
/**
\brief Helps to find memoryTypeIndex, given VkImageCreateInfo and VmaAllocationCreateInfo.
@@ -2245,66 +2610,66 @@ It is just a convenience function, equivalent to calling:
- `vmaFindMemoryTypeIndex`
- `vkDestroyImage`
*/
-VkResult vmaFindMemoryTypeIndexForImageInfo(
- VmaAllocator allocator,
- const VkImageCreateInfo *pImageCreateInfo,
- const VmaAllocationCreateInfo *pAllocationCreateInfo,
- uint32_t *pMemoryTypeIndex);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo(
+ VmaAllocator allocator,
+ const VkImageCreateInfo* pImageCreateInfo,
+ const VmaAllocationCreateInfo* pAllocationCreateInfo,
+ uint32_t* pMemoryTypeIndex);
/// Flags to be passed as VmaPoolCreateInfo::flags.
typedef enum VmaPoolCreateFlagBits {
/** \brief Use this flag if you always allocate only buffers and linear images or only optimal images out of this pool and so Buffer-Image Granularity can be ignored.
- This is an optional optimization flag.
+ This is an optional optimization flag.
- If you always allocate using vmaCreateBuffer(), vmaCreateImage(),
- vmaAllocateMemoryForBuffer(), then you don't need to use it because allocator
- knows exact type of your allocations so it can handle Buffer-Image Granularity
- in the optimal way.
+ If you always allocate using vmaCreateBuffer(), vmaCreateImage(),
+ vmaAllocateMemoryForBuffer(), then you don't need to use it because allocator
+ knows exact type of your allocations so it can handle Buffer-Image Granularity
+ in the optimal way.
- If you also allocate using vmaAllocateMemoryForImage() or vmaAllocateMemory(),
- exact type of such allocations is not known, so allocator must be conservative
- in handling Buffer-Image Granularity, which can lead to suboptimal allocation
- (wasted memory). In that case, if you can make sure you always allocate only
- buffers and linear images or only optimal images out of this pool, use this flag
- to make allocator disregard Buffer-Image Granularity and so make allocations
- faster and more optimal.
- */
+ If you also allocate using vmaAllocateMemoryForImage() or vmaAllocateMemory(),
+ exact type of such allocations is not known, so allocator must be conservative
+ in handling Buffer-Image Granularity, which can lead to suboptimal allocation
+ (wasted memory). In that case, if you can make sure you always allocate only
+ buffers and linear images or only optimal images out of this pool, use this flag
+ to make allocator disregard Buffer-Image Granularity and so make allocations
+ faster and more optimal.
+ */
VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT = 0x00000002,
/** \brief Enables alternative, linear allocation algorithm in this pool.
- Specify this flag to enable linear allocation algorithm, which always creates
- new allocations after last one and doesn't reuse space from allocations freed in
- between. It trades memory consumption for simplified algorithm and data
- structure, which has better performance and uses less memory for metadata.
+ Specify this flag to enable linear allocation algorithm, which always creates
+ new allocations after last one and doesn't reuse space from allocations freed in
+ between. It trades memory consumption for simplified algorithm and data
+ structure, which has better performance and uses less memory for metadata.
- By using this flag, you can achieve behavior of free-at-once, stack,
- ring buffer, and double stack. For details, see documentation chapter
- \ref linear_algorithm.
+ By using this flag, you can achieve behavior of free-at-once, stack,
+ ring buffer, and double stack. For details, see documentation chapter
+ \ref linear_algorithm.
- When using this flag, you must specify VmaPoolCreateInfo::maxBlockCount == 1 (or 0 for default).
+ When using this flag, you must specify VmaPoolCreateInfo::maxBlockCount == 1 (or 0 for default).
- For more details, see [Linear allocation algorithm](@ref linear_algorithm).
- */
+ For more details, see [Linear allocation algorithm](@ref linear_algorithm).
+ */
VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT = 0x00000004,
/** \brief Enables alternative, buddy allocation algorithm in this pool.
- It operates on a tree of blocks, each having size that is a power of two and
- a half of its parent's size. Comparing to default algorithm, this one provides
- faster allocation and deallocation and decreased external fragmentation,
- at the expense of more memory wasted (internal fragmentation).
+ It operates on a tree of blocks, each having size that is a power of two and
+ a half of its parent's size. Comparing to default algorithm, this one provides
+ faster allocation and deallocation and decreased external fragmentation,
+ at the expense of more memory wasted (internal fragmentation).
- For more details, see [Buddy allocation algorithm](@ref buddy_algorithm).
- */
+ For more details, see [Buddy allocation algorithm](@ref buddy_algorithm).
+ */
VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT = 0x00000008,
/** Bit mask to extract only `ALGORITHM` bits from entire set of flags.
- */
+ */
VMA_POOL_CREATE_ALGORITHM_MASK =
- VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT |
- VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT,
+ VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT |
+ VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT,
VMA_POOL_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaPoolCreateFlagBits;
@@ -2314,46 +2679,46 @@ typedef VkFlags VmaPoolCreateFlags;
*/
typedef struct VmaPoolCreateInfo {
/** \brief Vulkan memory type index to allocate this pool from.
- */
+ */
uint32_t memoryTypeIndex;
/** \brief Use combination of #VmaPoolCreateFlagBits.
- */
+ */
VmaPoolCreateFlags flags;
/** \brief Size of a single `VkDeviceMemory` block to be allocated as part of this pool, in bytes. Optional.
- Specify nonzero to set explicit, constant size of memory blocks used by this
- pool.
+ Specify nonzero to set explicit, constant size of memory blocks used by this
+ pool.
- Leave 0 to use default and let the library manage block sizes automatically.
- Sizes of particular blocks may vary.
- */
+ Leave 0 to use default and let the library manage block sizes automatically.
+ Sizes of particular blocks may vary.
+ */
VkDeviceSize blockSize;
/** \brief Minimum number of blocks to be always allocated in this pool, even if they stay empty.
- Set to 0 to have no preallocated blocks and allow the pool be completely empty.
- */
+ Set to 0 to have no preallocated blocks and allow the pool be completely empty.
+ */
size_t minBlockCount;
/** \brief Maximum number of blocks that can be allocated in this pool. Optional.
- Set to 0 to use default, which is `SIZE_MAX`, which means no limit.
-
- Set to same value as VmaPoolCreateInfo::minBlockCount to have fixed amount of memory allocated
- throughout whole lifetime of this pool.
- */
+ Set to 0 to use default, which is `SIZE_MAX`, which means no limit.
+
+ Set to same value as VmaPoolCreateInfo::minBlockCount to have fixed amount of memory allocated
+ throughout whole lifetime of this pool.
+ */
size_t maxBlockCount;
/** \brief Maximum number of additional frames that are in use at the same time as current frame.
- This value is used only when you make allocations with
- #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become
- lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.
+ This value is used only when you make allocations with
+ #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocation cannot become
+ lost if allocation.lastUseFrameIndex >= allocator.currentFrameIndex - frameInUseCount.
- For example, if you double-buffer your command buffers, so resources used for
- rendering in previous frame may still be in use by the GPU at the moment you
- allocate resources needed for the current frame, set this value to 1.
+ For example, if you double-buffer your command buffers, so resources used for
+ rendering in previous frame may still be in use by the GPU at the moment you
+ allocate resources needed for the current frame, set this value to 1.
- If you want to allow any allocations other than used in the current frame to
- become lost, set this value to 0.
- */
+ If you want to allow any allocations other than used in the current frame to
+ become lost, set this value to 0.
+ */
uint32_t frameInUseCount;
} VmaPoolCreateInfo;
@@ -2361,26 +2726,26 @@ typedef struct VmaPoolCreateInfo {
*/
typedef struct VmaPoolStats {
/** \brief Total amount of `VkDeviceMemory` allocated from Vulkan for this pool, in bytes.
- */
+ */
VkDeviceSize size;
/** \brief Total number of bytes in the pool not used by any #VmaAllocation.
- */
+ */
VkDeviceSize unusedSize;
/** \brief Number of #VmaAllocation objects created from this pool that were not destroyed or lost.
- */
+ */
size_t allocationCount;
/** \brief Number of continuous memory ranges in the pool not used by any #VmaAllocation.
- */
+ */
size_t unusedRangeCount;
/** \brief Size of the largest continuous free memory region available for new allocation.
- Making a new allocation of that size is not guaranteed to succeed because of
- possible additional margin required to respect alignment and buffer/image
- granularity.
- */
+ Making a new allocation of that size is not guaranteed to succeed because of
+ possible additional margin required to respect alignment and buffer/image
+ granularity.
+ */
VkDeviceSize unusedRangeSizeMax;
/** \brief Number of `VkDeviceMemory` blocks allocated for this pool.
- */
+ */
size_t blockCount;
} VmaPoolStats;
@@ -2390,16 +2755,16 @@ typedef struct VmaPoolStats {
@param pCreateInfo Parameters of pool to create.
@param[out] pPool Handle to created pool.
*/
-VkResult vmaCreatePool(
- VmaAllocator allocator,
- const VmaPoolCreateInfo *pCreateInfo,
- VmaPool *pPool);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreatePool(
+ VmaAllocator allocator,
+ const VmaPoolCreateInfo* pCreateInfo,
+ VmaPool* pPool);
/** \brief Destroys #VmaPool object and frees Vulkan device memory.
*/
-void vmaDestroyPool(
- VmaAllocator allocator,
- VmaPool pool);
+VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool(
+ VmaAllocator allocator,
+ VmaPool pool);
/** \brief Retrieves statistics of existing #VmaPool object.
@@ -2407,10 +2772,10 @@ void vmaDestroyPool(
@param pool Pool object.
@param[out] pPoolStats Statistics of specified pool.
*/
-void vmaGetPoolStats(
- VmaAllocator allocator,
- VmaPool pool,
- VmaPoolStats *pPoolStats);
+VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolStats(
+ VmaAllocator allocator,
+ VmaPool pool,
+ VmaPoolStats* pPoolStats);
/** \brief Marks all allocations in given pool as lost if they are not used in current frame or VmaPoolCreateInfo::frameInUseCount back from now.
@@ -2418,10 +2783,10 @@ void vmaGetPoolStats(
@param pool Pool.
@param[out] pLostAllocationCount Number of allocations marked as lost. Optional - pass null if you don't need this information.
*/
-void vmaMakePoolAllocationsLost(
- VmaAllocator allocator,
- VmaPool pool,
- size_t *pLostAllocationCount);
+VMA_CALL_PRE void VMA_CALL_POST vmaMakePoolAllocationsLost(
+ VmaAllocator allocator,
+ VmaPool pool,
+ size_t* pLostAllocationCount);
/** \brief Checks magic number in margins around all allocations in given memory pool in search for corruptions.
@@ -2437,7 +2802,28 @@ Possible return values:
`VMA_ASSERT` is also fired in that case.
- Other value: Error returned by Vulkan, e.g. memory mapping failure.
*/
-VkResult vmaCheckPoolCorruption(VmaAllocator allocator, VmaPool pool);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator allocator, VmaPool pool);
+
+/** \brief Retrieves name of a custom pool.
+
+After the call `ppName` is either null or points to an internally-owned null-terminated string
+containing name of the pool that was previously set. The pointer becomes invalid when the pool is
+destroyed or its name is changed using vmaSetPoolName().
+*/
+VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName(
+ VmaAllocator allocator,
+ VmaPool pool,
+ const char** ppName);
+
+/** \brief Sets name of a custom pool.
+
+`pName` can be either null or pointer to a null-terminated string with new name for the pool.
+Function makes internal copy of the string, so it can be changed or freed immediately after this call.
+*/
+VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName(
+ VmaAllocator allocator,
+ VmaPool pool,
+ const char* pName);
/** \struct VmaAllocation
\brief Represents single memory allocation.
@@ -2469,43 +2855,43 @@ VK_DEFINE_HANDLE(VmaAllocation)
*/
typedef struct VmaAllocationInfo {
/** \brief Memory type index that this allocation was allocated from.
-
- It never changes.
- */
+
+ It never changes.
+ */
uint32_t memoryType;
/** \brief Handle to Vulkan memory object.
- Same memory object can be shared by multiple allocations.
+ Same memory object can be shared by multiple allocations.
+
+ It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.
- It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.
-
- If the allocation is lost, it is equal to `VK_NULL_HANDLE`.
- */
+ If the allocation is lost, it is equal to `VK_NULL_HANDLE`.
+ */
VkDeviceMemory deviceMemory;
/** \brief Offset into deviceMemory object to the beginning of this allocation, in bytes. (deviceMemory, offset) pair is unique to this allocation.
- It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.
- */
+ It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost.
+ */
VkDeviceSize offset;
/** \brief Size of this allocation, in bytes.
- It never changes, unless allocation is lost.
- */
+ It never changes, unless allocation is lost.
+ */
VkDeviceSize size;
/** \brief Pointer to the beginning of this allocation as mapped data.
- If the allocation hasn't been mapped using vmaMapMemory() and hasn't been
- created with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag, this value null.
+ If the allocation hasn't been mapped using vmaMapMemory() and hasn't been
+ created with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag, this value null.
- It can change after call to vmaMapMemory(), vmaUnmapMemory().
- It can also change after call to vmaDefragment() if this allocation is passed to the function.
- */
- void *pMappedData;
+ It can change after call to vmaMapMemory(), vmaUnmapMemory().
+ It can also change after call to vmaDefragment() if this allocation is passed to the function.
+ */
+ void* pMappedData;
/** \brief Custom general-purpose pointer that was passed as VmaAllocationCreateInfo::pUserData or set using vmaSetAllocationUserData().
- It can change after call to vmaSetAllocationUserData() for this allocation.
- */
- void *pUserData;
+ It can change after call to vmaSetAllocationUserData() for this allocation.
+ */
+ void* pUserData;
} VmaAllocationInfo;
/** \brief General purpose memory allocation.
@@ -2518,12 +2904,12 @@ You should free the memory using vmaFreeMemory() or vmaFreeMemoryPages().
It is recommended to use vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage(),
vmaCreateBuffer(), vmaCreateImage() instead whenever possible.
*/
-VkResult vmaAllocateMemory(
- VmaAllocator allocator,
- const VkMemoryRequirements *pVkMemoryRequirements,
- const VmaAllocationCreateInfo *pCreateInfo,
- VmaAllocation *pAllocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory(
+ VmaAllocator allocator,
+ const VkMemoryRequirements* pVkMemoryRequirements,
+ const VmaAllocationCreateInfo* pCreateInfo,
+ VmaAllocation* pAllocation,
+ VmaAllocationInfo* pAllocationInfo);
/** \brief General purpose memory allocation for multiple allocation objects at once.
@@ -2544,13 +2930,13 @@ All allocations are made using same parameters. All of them are created out of t
If any allocation fails, all allocations already made within this function call are also freed, so that when
returned result is not `VK_SUCCESS`, `pAllocation` array is always entirely filled with `VK_NULL_HANDLE`.
*/
-VkResult vmaAllocateMemoryPages(
- VmaAllocator allocator,
- const VkMemoryRequirements *pVkMemoryRequirements,
- const VmaAllocationCreateInfo *pCreateInfo,
- size_t allocationCount,
- VmaAllocation *pAllocations,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages(
+ VmaAllocator allocator,
+ const VkMemoryRequirements* pVkMemoryRequirements,
+ const VmaAllocationCreateInfo* pCreateInfo,
+ size_t allocationCount,
+ VmaAllocation* pAllocations,
+ VmaAllocationInfo* pAllocationInfo);
/**
@param[out] pAllocation Handle to allocated memory.
@@ -2558,28 +2944,28 @@ VkResult vmaAllocateMemoryPages(
You should free the memory using vmaFreeMemory().
*/
-VkResult vmaAllocateMemoryForBuffer(
- VmaAllocator allocator,
- VkBuffer buffer,
- const VmaAllocationCreateInfo *pCreateInfo,
- VmaAllocation *pAllocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer(
+ VmaAllocator allocator,
+ VkBuffer buffer,
+ const VmaAllocationCreateInfo* pCreateInfo,
+ VmaAllocation* pAllocation,
+ VmaAllocationInfo* pAllocationInfo);
/// Function similar to vmaAllocateMemoryForBuffer().
-VkResult vmaAllocateMemoryForImage(
- VmaAllocator allocator,
- VkImage image,
- const VmaAllocationCreateInfo *pCreateInfo,
- VmaAllocation *pAllocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage(
+ VmaAllocator allocator,
+ VkImage image,
+ const VmaAllocationCreateInfo* pCreateInfo,
+ VmaAllocation* pAllocation,
+ VmaAllocationInfo* pAllocationInfo);
/** \brief Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage().
Passing `VK_NULL_HANDLE` as `allocation` is valid. Such function call is just skipped.
*/
-void vmaFreeMemory(
- VmaAllocator allocator,
- VmaAllocation allocation);
+VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory(
+ VmaAllocator allocator,
+ VmaAllocation allocation);
/** \brief Frees memory and destroys multiple allocations.
@@ -2591,35 +2977,22 @@ It may be internally optimized to be more efficient than calling vmaFreeMemory()
Allocations in `pAllocations` array can come from any memory pools and types.
Passing `VK_NULL_HANDLE` as elements of `pAllocations` array is valid. Such entries are just skipped.
*/
-void vmaFreeMemoryPages(
- VmaAllocator allocator,
- size_t allocationCount,
- VmaAllocation *pAllocations);
+VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages(
+ VmaAllocator allocator,
+ size_t allocationCount,
+ VmaAllocation* pAllocations);
-/** \brief Tries to resize an allocation in place, if there is enough free memory after it.
+/** \brief Deprecated.
-Tries to change allocation's size without moving or reallocating it.
-You can both shrink and grow allocation size.
-When growing, it succeeds only when the allocation belongs to a memory block with enough
-free space after it.
-
-Returns `VK_SUCCESS` if allocation's size has been successfully changed.
-Returns `VK_ERROR_OUT_OF_POOL_MEMORY` if allocation's size could not be changed.
-
-After successful call to this function, VmaAllocationInfo::size of this allocation changes.
-All other parameters stay the same: memory pool and type, alignment, offset, mapped pointer.
-
-- Calling this function on allocation that is in lost state fails with result `VK_ERROR_VALIDATION_FAILED_EXT`.
-- Calling this function with `newSize` same as current allocation size does nothing and returns `VK_SUCCESS`.
-- Resizing dedicated allocations, as well as allocations created in pools that use linear
- or buddy algorithm, is not supported.
- The function returns `VK_ERROR_FEATURE_NOT_PRESENT` in such cases.
- Support may be added in the future.
+\deprecated
+In version 2.2.0 it used to try to change allocation's size without moving or reallocating it.
+In current version it returns `VK_SUCCESS` only if `newSize` equals current allocation's size.
+Otherwise returns `VK_ERROR_OUT_OF_POOL_MEMORY`, indicating that allocation's size could not be changed.
*/
-VkResult vmaResizeAllocation(
- VmaAllocator allocator,
- VmaAllocation allocation,
- VkDeviceSize newSize);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaResizeAllocation(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VkDeviceSize newSize);
/** \brief Returns current information about specified allocation and atomically marks it as used in current frame.
@@ -2637,10 +3010,10 @@ you can avoid calling it too often.
(e.g. due to defragmentation or allocation becoming lost).
- If you just want to check if allocation is not lost, vmaTouchAllocation() will work faster.
*/
-void vmaGetAllocationInfo(
- VmaAllocator allocator,
- VmaAllocation allocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VmaAllocationInfo* pAllocationInfo);
/** \brief Returns `VK_TRUE` if allocation is not lost and atomically marks it as used in current frame.
@@ -2656,9 +3029,9 @@ Lost allocation and the buffer/image still need to be destroyed.
If the allocation has been created without #VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag,
this function always returns `VK_TRUE`.
*/
-VkBool32 vmaTouchAllocation(
- VmaAllocator allocator,
- VmaAllocation allocation);
+VMA_CALL_PRE VkBool32 VMA_CALL_POST vmaTouchAllocation(
+ VmaAllocator allocator,
+ VmaAllocation allocation);
/** \brief Sets pUserData in given allocation to new value.
@@ -2673,10 +3046,10 @@ If the flag was not used, the value of pointer `pUserData` is just copied to
allocation's `pUserData`. It is opaque, so you can use it however you want - e.g.
as a pointer, ordinal number or some handle to you own data.
*/
-void vmaSetAllocationUserData(
- VmaAllocator allocator,
- VmaAllocation allocation,
- void *pUserData);
+VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ void* pUserData);
/** \brief Creates new allocation that is in lost state from the beginning.
@@ -2688,9 +3061,9 @@ Returned allocation is not tied to any specific memory pool or memory type and
not bound to any image or buffer. It has size = 0. It cannot be turned into
a real, non-empty allocation.
*/
-void vmaCreateLostAllocation(
- VmaAllocator allocator,
- VmaAllocation *pAllocation);
+VMA_CALL_PRE void VMA_CALL_POST vmaCreateLostAllocation(
+ VmaAllocator allocator,
+ VmaAllocation* pAllocation);
/** \brief Maps memory represented by given allocation and returns pointer to it.
@@ -2725,23 +3098,33 @@ This function fails when used on allocation made in memory type that is not
This function always fails when called for allocation that was created with
#VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT flag. Such allocations cannot be
mapped.
+
+This function doesn't automatically flush or invalidate caches.
+If the allocation is made from a memory types that is not `HOST_COHERENT`,
+you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification.
*/
-VkResult vmaMapMemory(
- VmaAllocator allocator,
- VmaAllocation allocation,
- void **ppData);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaMapMemory(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ void** ppData);
/** \brief Unmaps memory represented by given allocation, mapped previously using vmaMapMemory().
For details, see description of vmaMapMemory().
+
+This function doesn't automatically flush or invalidate caches.
+If the allocation is made from a memory types that is not `HOST_COHERENT`,
+you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification.
*/
-void vmaUnmapMemory(
- VmaAllocator allocator,
- VmaAllocation allocation);
+VMA_CALL_PRE void VMA_CALL_POST vmaUnmapMemory(
+ VmaAllocator allocator,
+ VmaAllocation allocation);
/** \brief Flushes memory of given allocation.
Calls `vkFlushMappedMemoryRanges()` for memory associated with given range of given allocation.
+It needs to be called after writing to a mapped memory for memory types that are not `HOST_COHERENT`.
+Unmap operation doesn't do that automatically.
- `offset` must be relative to the beginning of allocation.
- `size` can be `VK_WHOLE_SIZE`. It means all memory from `offset` the the end of given allocation.
@@ -2755,11 +3138,13 @@ Warning! `offset` and `size` are relative to the contents of given `allocation`.
If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively.
Do not pass allocation's offset as `offset`!!!
*/
-void vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);
+VMA_CALL_PRE void VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);
/** \brief Invalidates memory of given allocation.
Calls `vkInvalidateMappedMemoryRanges()` for memory associated with given range of given allocation.
+It needs to be called before reading from a mapped memory for memory types that are not `HOST_COHERENT`.
+Map operation doesn't do that automatically.
- `offset` must be relative to the beginning of allocation.
- `size` can be `VK_WHOLE_SIZE`. It means all memory from `offset` the the end of given allocation.
@@ -2773,7 +3158,7 @@ Warning! `offset` and `size` are relative to the contents of given `allocation`.
If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively.
Do not pass allocation's offset as `offset`!!!
*/
-void vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);
+VMA_CALL_PRE void VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size);
/** \brief Checks magic number in margins around all allocations in given memory types (in both default and custom pools) in search for corruptions.
@@ -2791,7 +3176,7 @@ Possible return values:
`VMA_ASSERT` is also fired in that case.
- Other value: Error returned by Vulkan, e.g. memory mapping failure.
*/
-VkResult vmaCheckCorruption(VmaAllocator allocator, uint32_t memoryTypeBits);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator allocator, uint32_t memoryTypeBits);
/** \struct VmaDefragmentationContext
\brief Represents Opaque object that represents started defragmentation process.
@@ -2803,6 +3188,7 @@ VK_DEFINE_HANDLE(VmaDefragmentationContext)
/// Flags to be used in vmaDefragmentationBegin(). None at the moment. Reserved for future use.
typedef enum VmaDefragmentationFlagBits {
+ VMA_DEFRAGMENTATION_FLAG_INCREMENTAL = 0x1,
VMA_DEFRAGMENTATION_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF
} VmaDefragmentationFlagBits;
typedef VkFlags VmaDefragmentationFlags;
@@ -2813,90 +3199,105 @@ To be used with function vmaDefragmentationBegin().
*/
typedef struct VmaDefragmentationInfo2 {
/** \brief Reserved for future use. Should be 0.
- */
+ */
VmaDefragmentationFlags flags;
/** \brief Number of allocations in `pAllocations` array.
- */
+ */
uint32_t allocationCount;
/** \brief Pointer to array of allocations that can be defragmented.
- The array should have `allocationCount` elements.
- The array should not contain nulls.
- Elements in the array should be unique - same allocation cannot occur twice.
- It is safe to pass allocations that are in the lost state - they are ignored.
- All allocations not present in this array are considered non-moveable during this defragmentation.
- */
- VmaAllocation *pAllocations;
+ The array should have `allocationCount` elements.
+ The array should not contain nulls.
+ Elements in the array should be unique - same allocation cannot occur twice.
+ It is safe to pass allocations that are in the lost state - they are ignored.
+ All allocations not present in this array are considered non-moveable during this defragmentation.
+ */
+ VmaAllocation* pAllocations;
/** \brief Optional, output. Pointer to array that will be filled with information whether the allocation at certain index has been changed during defragmentation.
- The array should have `allocationCount` elements.
- You can pass null if you are not interested in this information.
- */
- VkBool32 *pAllocationsChanged;
+ The array should have `allocationCount` elements.
+ You can pass null if you are not interested in this information.
+ */
+ VkBool32* pAllocationsChanged;
/** \brief Numer of pools in `pPools` array.
- */
+ */
uint32_t poolCount;
/** \brief Either null or pointer to array of pools to be defragmented.
- All the allocations in the specified pools can be moved during defragmentation
- and there is no way to check if they were really moved as in `pAllocationsChanged`,
- so you must query all the allocations in all these pools for new `VkDeviceMemory`
- and offset using vmaGetAllocationInfo() if you might need to recreate buffers
- and images bound to them.
+ All the allocations in the specified pools can be moved during defragmentation
+ and there is no way to check if they were really moved as in `pAllocationsChanged`,
+ so you must query all the allocations in all these pools for new `VkDeviceMemory`
+ and offset using vmaGetAllocationInfo() if you might need to recreate buffers
+ and images bound to them.
- The array should have `poolCount` elements.
- The array should not contain nulls.
- Elements in the array should be unique - same pool cannot occur twice.
+ The array should have `poolCount` elements.
+ The array should not contain nulls.
+ Elements in the array should be unique - same pool cannot occur twice.
- Using this array is equivalent to specifying all allocations from the pools in `pAllocations`.
- It might be more efficient.
- */
- VmaPool *pPools;
+ Using this array is equivalent to specifying all allocations from the pools in `pAllocations`.
+ It might be more efficient.
+ */
+ VmaPool* pPools;
/** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on CPU side, like `memcpy()`, `memmove()`.
-
- `VK_WHOLE_SIZE` means no limit.
- */
+
+ `VK_WHOLE_SIZE` means no limit.
+ */
VkDeviceSize maxCpuBytesToMove;
/** \brief Maximum number of allocations that can be moved to a different place using transfers on CPU side, like `memcpy()`, `memmove()`.
- `UINT32_MAX` means no limit.
- */
+ `UINT32_MAX` means no limit.
+ */
uint32_t maxCpuAllocationsToMove;
/** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on GPU side, posted to `commandBuffer`.
-
- `VK_WHOLE_SIZE` means no limit.
- */
+
+ `VK_WHOLE_SIZE` means no limit.
+ */
VkDeviceSize maxGpuBytesToMove;
/** \brief Maximum number of allocations that can be moved to a different place using transfers on GPU side, posted to `commandBuffer`.
- `UINT32_MAX` means no limit.
- */
+ `UINT32_MAX` means no limit.
+ */
uint32_t maxGpuAllocationsToMove;
/** \brief Optional. Command buffer where GPU copy commands will be posted.
- If not null, it must be a valid command buffer handle that supports Transfer queue type.
- It must be in the recording state and outside of a render pass instance.
- You need to submit it and make sure it finished execution before calling vmaDefragmentationEnd().
+ If not null, it must be a valid command buffer handle that supports Transfer queue type.
+ It must be in the recording state and outside of a render pass instance.
+ You need to submit it and make sure it finished execution before calling vmaDefragmentationEnd().
- Passing null means that only CPU defragmentation will be performed.
- */
+ Passing null means that only CPU defragmentation will be performed.
+ */
VkCommandBuffer commandBuffer;
} VmaDefragmentationInfo2;
+typedef struct VmaDefragmentationPassMoveInfo {
+ VmaAllocation allocation;
+ VkDeviceMemory memory;
+ VkDeviceSize offset;
+} VmaDefragmentationPassMoveInfo;
+
+/** \brief Parameters for incremental defragmentation steps.
+
+To be used with function vmaBeginDefragmentationPass().
+*/
+typedef struct VmaDefragmentationPassInfo {
+ uint32_t moveCount;
+ VmaDefragmentationPassMoveInfo* pMoves;
+} VmaDefragmentationPassInfo;
+
/** \brief Deprecated. Optional configuration parameters to be passed to function vmaDefragment().
\deprecated This is a part of the old interface. It is recommended to use structure #VmaDefragmentationInfo2 and function vmaDefragmentationBegin() instead.
*/
typedef struct VmaDefragmentationInfo {
/** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places.
-
- Default is `VK_WHOLE_SIZE`, which means no limit.
- */
+
+ Default is `VK_WHOLE_SIZE`, which means no limit.
+ */
VkDeviceSize maxBytesToMove;
/** \brief Maximum number of allocations that can be moved to different place.
- Default is `UINT32_MAX`, which means no limit.
- */
+ Default is `UINT32_MAX`, which means no limit.
+ */
uint32_t maxAllocationsToMove;
} VmaDefragmentationInfo;
@@ -2941,20 +3342,30 @@ Warning! Between the call to vmaDefragmentationBegin() and vmaDefragmentationEnd
For more information and important limitations regarding defragmentation, see documentation chapter:
[Defragmentation](@ref defragmentation).
*/
-VkResult vmaDefragmentationBegin(
- VmaAllocator allocator,
- const VmaDefragmentationInfo2 *pInfo,
- VmaDefragmentationStats *pStats,
- VmaDefragmentationContext *pContext);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationBegin(
+ VmaAllocator allocator,
+ const VmaDefragmentationInfo2* pInfo,
+ VmaDefragmentationStats* pStats,
+ VmaDefragmentationContext *pContext);
/** \brief Ends defragmentation process.
Use this function to finish defragmentation started by vmaDefragmentationBegin().
It is safe to pass `context == null`. The function then does nothing.
*/
-VkResult vmaDefragmentationEnd(
- VmaAllocator allocator,
- VmaDefragmentationContext context);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationEnd(
+ VmaAllocator allocator,
+ VmaDefragmentationContext context);
+
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaBeginDefragmentationPass(
+ VmaAllocator allocator,
+ VmaDefragmentationContext context,
+ VmaDefragmentationPassInfo* pInfo
+);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaEndDefragmentationPass(
+ VmaAllocator allocator,
+ VmaDefragmentationContext context
+);
/** \brief Deprecated. Compacts memory by moving allocations.
@@ -2996,13 +3407,13 @@ you should measure that on your platform.
For more information, see [Defragmentation](@ref defragmentation) chapter.
*/
-VkResult vmaDefragment(
- VmaAllocator allocator,
- VmaAllocation *pAllocations,
- size_t allocationCount,
- VkBool32 *pAllocationsChanged,
- const VmaDefragmentationInfo *pDefragmentationInfo,
- VmaDefragmentationStats *pDefragmentationStats);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragment(
+ VmaAllocator allocator,
+ VmaAllocation* pAllocations,
+ size_t allocationCount,
+ VkBool32* pAllocationsChanged,
+ const VmaDefragmentationInfo *pDefragmentationInfo,
+ VmaDefragmentationStats* pDefragmentationStats);
/** \brief Binds buffer to allocation.
@@ -3016,10 +3427,27 @@ allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from mul
It is recommended to use function vmaCreateBuffer() instead of this one.
*/
-VkResult vmaBindBufferMemory(
- VmaAllocator allocator,
- VmaAllocation allocation,
- VkBuffer buffer);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VkBuffer buffer);
+
+/** \brief Binds buffer to allocation with additional parameters.
+
+@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0.
+@param pNext A chain of structures to be attached to `VkBindBufferMemoryInfoKHR` structure used internally. Normally it should be null.
+
+This function is similar to vmaBindBufferMemory(), but it provides additional parameters.
+
+If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag
+or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails.
+*/
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory2(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VkDeviceSize allocationLocalOffset,
+ VkBuffer buffer,
+ const void* pNext);
/** \brief Binds image to allocation.
@@ -3033,10 +3461,27 @@ allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from mul
It is recommended to use function vmaCreateImage() instead of this one.
*/
-VkResult vmaBindImageMemory(
- VmaAllocator allocator,
- VmaAllocation allocation,
- VkImage image);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VkImage image);
+
+/** \brief Binds image to allocation with additional parameters.
+
+@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0.
+@param pNext A chain of structures to be attached to `VkBindImageMemoryInfoKHR` structure used internally. Normally it should be null.
+
+This function is similar to vmaBindImageMemory(), but it provides additional parameters.
+
+If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag
+or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails.
+*/
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory2(
+ VmaAllocator allocator,
+ VmaAllocation allocation,
+ VkDeviceSize allocationLocalOffset,
+ VkImage image,
+ const void* pNext);
/**
@param[out] pBuffer Buffer that was created.
@@ -3064,13 +3509,13 @@ and VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT is not used), it creates dedicated
allocation for this buffer, just like when using
VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT.
*/
-VkResult vmaCreateBuffer(
- VmaAllocator allocator,
- const VkBufferCreateInfo *pBufferCreateInfo,
- const VmaAllocationCreateInfo *pAllocationCreateInfo,
- VkBuffer *pBuffer,
- VmaAllocation *pAllocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer(
+ VmaAllocator allocator,
+ const VkBufferCreateInfo* pBufferCreateInfo,
+ const VmaAllocationCreateInfo* pAllocationCreateInfo,
+ VkBuffer* pBuffer,
+ VmaAllocation* pAllocation,
+ VmaAllocationInfo* pAllocationInfo);
/** \brief Destroys Vulkan buffer and frees allocated memory.
@@ -3083,19 +3528,19 @@ vmaFreeMemory(allocator, allocation);
It it safe to pass null as buffer and/or allocation.
*/
-void vmaDestroyBuffer(
- VmaAllocator allocator,
- VkBuffer buffer,
- VmaAllocation allocation);
+VMA_CALL_PRE void VMA_CALL_POST vmaDestroyBuffer(
+ VmaAllocator allocator,
+ VkBuffer buffer,
+ VmaAllocation allocation);
/// Function similar to vmaCreateBuffer().
-VkResult vmaCreateImage(
- VmaAllocator allocator,
- const VkImageCreateInfo *pImageCreateInfo,
- const VmaAllocationCreateInfo *pAllocationCreateInfo,
- VkImage *pImage,
- VmaAllocation *pAllocation,
- VmaAllocationInfo *pAllocationInfo);
+VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage(
+ VmaAllocator allocator,
+ const VkImageCreateInfo* pImageCreateInfo,
+ const VmaAllocationCreateInfo* pAllocationCreateInfo,
+ VkImage* pImage,
+ VmaAllocation* pAllocation,
+ VmaAllocationInfo* pAllocationInfo);
/** \brief Destroys Vulkan image and frees allocated memory.
@@ -3108,10 +3553,10 @@ vmaFreeMemory(allocator, allocation);
It it safe to pass null as image and/or allocation.
*/
-void vmaDestroyImage(
- VmaAllocator allocator,
- VkImage image,
- VmaAllocation allocation);
+VMA_CALL_PRE void VMA_CALL_POST vmaDestroyImage(
+ VmaAllocator allocator,
+ VkImage image,
+ VmaAllocation allocation);
#ifdef __cplusplus
}
@@ -3130,6 +3575,7 @@ void vmaDestroyImage(
#include
#include
#include
+#include
/*******************************************************************************
CONFIGURATION SECTION
@@ -3142,7 +3588,7 @@ here if you need other then default behavior depending on your environment.
Define this macro to 1 to make the library fetch pointers to Vulkan functions
internally, like:
- vulkanFunctions.vkAllocateMemory = &vkAllocateMemory;
+ vulkanFunctions.vkAllocateMemory = &vkAllocateMemory;
Define to 0 if you are going to provide you own pointers to Vulkan functions via
VmaAllocatorCreateInfo::pVulkanFunctions.
@@ -3161,23 +3607,23 @@ Set it to 0 or undefined to make the library using its own implementation of
the containers.
*/
#if VMA_USE_STL_CONTAINERS
-#define VMA_USE_STL_VECTOR 1
-#define VMA_USE_STL_UNORDERED_MAP 1
-#define VMA_USE_STL_LIST 1
+ #define VMA_USE_STL_VECTOR 1
+ #define VMA_USE_STL_UNORDERED_MAP 1
+ #define VMA_USE_STL_LIST 1
#endif
#ifndef VMA_USE_STL_SHARED_MUTEX
-// Compiler conforms to C++17.
-#if __cplusplus >= 201703L
-#define VMA_USE_STL_SHARED_MUTEX 1
-// Visual studio defines __cplusplus properly only when passed additional parameter: /Zc:__cplusplus
-// Otherwise it's always 199711L, despite shared_mutex works since Visual Studio 2015 Update 2.
-// See: https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/
-#elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && __cplusplus == 199711L && _MSVC_LANG >= 201703L
-#define VMA_USE_STL_SHARED_MUTEX 1
-#else
-#define VMA_USE_STL_SHARED_MUTEX 0
-#endif
+ // Compiler conforms to C++17.
+ #if __cplusplus >= 201703L
+ #define VMA_USE_STL_SHARED_MUTEX 1
+ // Visual studio defines __cplusplus properly only when passed additional parameter: /Zc:__cplusplus
+ // Otherwise it's always 199711L, despite shared_mutex works since Visual Studio 2015 Update 2.
+ // See: https://blogs.msdn.microsoft.com/vcblog/2018/04/09/msvc-now-correctly-reports-__cplusplus/
+ #elif defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 190023918 && __cplusplus == 199711L && _MSVC_LANG >= 201703L
+ #define VMA_USE_STL_SHARED_MUTEX 1
+ #else
+ #define VMA_USE_STL_SHARED_MUTEX 0
+ #endif
#endif
/*
@@ -3185,50 +3631,54 @@ THESE INCLUDES ARE NOT ENABLED BY DEFAULT.
Library has its own container implementation.
*/
#if VMA_USE_STL_VECTOR
-#include
+ #include
#endif
#if VMA_USE_STL_UNORDERED_MAP
-#include
+ #include
#endif
#if VMA_USE_STL_LIST
-#include
+ #include
#endif
/*
Following headers are used in this CONFIGURATION section only, so feel free to
remove them if not needed.
*/
-#include // for min, max
#include // for assert
+#include // for min, max
#include
#ifndef VMA_NULL
-// Value used as null pointer. Define it to e.g.: nullptr, NULL, 0, (void*)0.
-#define VMA_NULL nullptr
+ // Value used as null pointer. Define it to e.g.: nullptr, NULL, 0, (void*)0.
+ #define VMA_NULL nullptr
#endif
#if defined(__ANDROID_API__) && (__ANDROID_API__ < 16)
#include
-void *aligned_alloc(size_t alignment, size_t size) {
+void *aligned_alloc(size_t alignment, size_t size)
+{
// alignment must be >= sizeof(void*)
- if (alignment < sizeof(void *)) {
- alignment = sizeof(void *);
+ if(alignment < sizeof(void*))
+ {
+ alignment = sizeof(void*);
}
return memalign(alignment, size);
}
-#elif defined(__APPLE__) || defined(__ANDROID__)
+#elif defined(__APPLE__) || defined(__ANDROID__) || (defined(__linux__) && defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC))
#include
-void *aligned_alloc(size_t alignment, size_t size) {
+void *aligned_alloc(size_t alignment, size_t size)
+{
// alignment must be >= sizeof(void*)
- if (alignment < sizeof(void *)) {
- alignment = sizeof(void *);
+ if(alignment < sizeof(void*))
+ {
+ alignment = sizeof(void*);
}
void *pointer;
- if (posix_memalign(&pointer, alignment, size) == 0)
+ if(posix_memalign(&pointer, alignment, size) == 0)
return pointer;
return VMA_NULL;
}
@@ -3241,226 +3691,236 @@ void *aligned_alloc(size_t alignment, size_t size) {
// Normal assert to check for programmer's errors, especially in Debug configuration.
#ifndef VMA_ASSERT
-#ifdef _DEBUG
-#define VMA_ASSERT(expr) assert(expr)
-#else
-#define VMA_ASSERT(expr)
-#endif
+ #ifdef NDEBUG
+ #define VMA_ASSERT(expr)
+ #else
+ #define VMA_ASSERT(expr) assert(expr)
+ #endif
#endif
// Assert that will be called very often, like inside data structures e.g. operator[].
// Making it non-empty can make program slow.
#ifndef VMA_HEAVY_ASSERT
-#ifdef _DEBUG
-#define VMA_HEAVY_ASSERT(expr) //VMA_ASSERT(expr)
-#else
-#define VMA_HEAVY_ASSERT(expr)
-#endif
+ #ifdef NDEBUG
+ #define VMA_HEAVY_ASSERT(expr)
+ #else
+ #define VMA_HEAVY_ASSERT(expr) //VMA_ASSERT(expr)
+ #endif
#endif
#ifndef VMA_ALIGN_OF
-#define VMA_ALIGN_OF(type) (__alignof(type))
+ #define VMA_ALIGN_OF(type) (__alignof(type))
#endif
#ifndef VMA_SYSTEM_ALIGNED_MALLOC
-#if defined(_WIN32)
-#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (_aligned_malloc((size), (alignment)))
-#else
-#define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (aligned_alloc((alignment), (size)))
-#endif
+ #if defined(_WIN32)
+ #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (_aligned_malloc((size), (alignment)))
+ #else
+ #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (aligned_alloc((alignment), (size) ))
+ #endif
#endif
#ifndef VMA_SYSTEM_FREE
-#if defined(_WIN32)
-#define VMA_SYSTEM_FREE(ptr) _aligned_free(ptr)
-#else
-#define VMA_SYSTEM_FREE(ptr) free(ptr)
-#endif
+ #if defined(_WIN32)
+ #define VMA_SYSTEM_FREE(ptr) _aligned_free(ptr)
+ #else
+ #define VMA_SYSTEM_FREE(ptr) free(ptr)
+ #endif
#endif
#ifndef VMA_MIN
-#define VMA_MIN(v1, v2) (std::min((v1), (v2)))
+ #define VMA_MIN(v1, v2) (std::min((v1), (v2)))
#endif
#ifndef VMA_MAX
-#define VMA_MAX(v1, v2) (std::max((v1), (v2)))
+ #define VMA_MAX(v1, v2) (std::max((v1), (v2)))
#endif
#ifndef VMA_SWAP
-#define VMA_SWAP(v1, v2) std::swap((v1), (v2))
+ #define VMA_SWAP(v1, v2) std::swap((v1), (v2))
#endif
#ifndef VMA_SORT
-#define VMA_SORT(beg, end, cmp) std::sort(beg, end, cmp)
+ #define VMA_SORT(beg, end, cmp) std::sort(beg, end, cmp)
#endif
#ifndef VMA_DEBUG_LOG
-#define VMA_DEBUG_LOG(format, ...)
-/*
+ #define VMA_DEBUG_LOG(format, ...)
+ /*
#define VMA_DEBUG_LOG(format, ...) do { \
- printf(format, __VA_ARGS__); \
- printf("\n"); \
+ printf(format, __VA_ARGS__); \
+ printf("\n"); \
} while(false)
*/
#endif
// Define this macro to 1 to enable functions: vmaBuildStatsString, vmaFreeStatsString.
#if VMA_STATS_STRING_ENABLED
-static inline void VmaUint32ToStr(char *outStr, size_t strLen, uint32_t num) {
- snprintf(outStr, strLen, "%u", static_cast(num));
-}
-static inline void VmaUint64ToStr(char *outStr, size_t strLen, uint64_t num) {
- snprintf(outStr, strLen, "%llu", static_cast(num));
-}
-static inline void VmaPtrToStr(char *outStr, size_t strLen, const void *ptr) {
- snprintf(outStr, strLen, "%p", ptr);
-}
+ static inline void VmaUint32ToStr(char* outStr, size_t strLen, uint32_t num)
+ {
+ snprintf(outStr, strLen, "%u", static_cast(num));
+ }
+ static inline void VmaUint64ToStr(char* outStr, size_t strLen, uint64_t num)
+ {
+ snprintf(outStr, strLen, "%llu", static_cast(num));
+ }
+ static inline void VmaPtrToStr(char* outStr, size_t strLen, const void* ptr)
+ {
+ snprintf(outStr, strLen, "%p", ptr);
+ }
#endif
#ifndef VMA_MUTEX
-class VmaMutex {
-public:
- void Lock() { m_Mutex.lock(); }
- void Unlock() { m_Mutex.unlock(); }
-
-private:
- std::mutex m_Mutex;
-};
-#define VMA_MUTEX VmaMutex
+ class VmaMutex
+ {
+ public:
+ void Lock() { m_Mutex.lock(); }
+ void Unlock() { m_Mutex.unlock(); }
+ bool TryLock() { return m_Mutex.try_lock(); }
+ private:
+ std::mutex m_Mutex;
+ };
+ #define VMA_MUTEX VmaMutex
#endif
// Read-write mutex, where "read" is shared access, "write" is exclusive access.
#ifndef VMA_RW_MUTEX
-#if VMA_USE_STL_SHARED_MUTEX
-// Use std::shared_mutex from C++17.
-#include
-class VmaRWMutex {
-public:
- void LockRead() { m_Mutex.lock_shared(); }
- void UnlockRead() { m_Mutex.unlock_shared(); }
- void LockWrite() { m_Mutex.lock(); }
- void UnlockWrite() { m_Mutex.unlock(); }
-
-private:
- std::shared_mutex m_Mutex;
-};
-#define VMA_RW_MUTEX VmaRWMutex
-#elif defined(_WIN32) && defined(WINVER) && WINVER >= 0x0600
-// Use SRWLOCK from WinAPI.
-// Minimum supported client = Windows Vista, server = Windows Server 2008.
-class VmaRWMutex {
-public:
- VmaRWMutex() { InitializeSRWLock(&m_Lock); }
- void LockRead() { AcquireSRWLockShared(&m_Lock); }
- void UnlockRead() { ReleaseSRWLockShared(&m_Lock); }
- void LockWrite() { AcquireSRWLockExclusive(&m_Lock); }
- void UnlockWrite() { ReleaseSRWLockExclusive(&m_Lock); }
-
-private:
- SRWLOCK m_Lock;
-};
-#define VMA_RW_MUTEX VmaRWMutex
-#else
-// Less efficient fallback: Use normal mutex.
-class VmaRWMutex {
-public:
- void LockRead() { m_Mutex.Lock(); }
- void UnlockRead() { m_Mutex.Unlock(); }
- void LockWrite() { m_Mutex.Lock(); }
- void UnlockWrite() { m_Mutex.Unlock(); }
-
-private:
- VMA_MUTEX m_Mutex;
-};
-#define VMA_RW_MUTEX VmaRWMutex
-#endif // #if VMA_USE_STL_SHARED_MUTEX
+ #if VMA_USE_STL_SHARED_MUTEX
+ // Use std::shared_mutex from C++17.
+ #include
+ class VmaRWMutex
+ {
+ public:
+ void LockRead() { m_Mutex.lock_shared(); }
+ void UnlockRead() { m_Mutex.unlock_shared(); }
+ bool TryLockRead() { return m_Mutex.try_lock_shared(); }
+ void LockWrite() { m_Mutex.lock(); }
+ void UnlockWrite() { m_Mutex.unlock(); }
+ bool TryLockWrite() { return m_Mutex.try_lock(); }
+ private:
+ std::shared_mutex m_Mutex;
+ };
+ #define VMA_RW_MUTEX VmaRWMutex
+ #elif defined(_WIN32) && defined(WINVER) && WINVER >= 0x0600
+ // Use SRWLOCK from WinAPI.
+ // Minimum supported client = Windows Vista, server = Windows Server 2008.
+ class VmaRWMutex
+ {
+ public:
+ VmaRWMutex() { InitializeSRWLock(&m_Lock); }
+ void LockRead() { AcquireSRWLockShared(&m_Lock); }
+ void UnlockRead() { ReleaseSRWLockShared(&m_Lock); }
+ bool TryLockRead() { return TryAcquireSRWLockShared(&m_Lock) != FALSE; }
+ void LockWrite() { AcquireSRWLockExclusive(&m_Lock); }
+ void UnlockWrite() { ReleaseSRWLockExclusive(&m_Lock); }
+ bool TryLockWrite() { return TryAcquireSRWLockExclusive(&m_Lock) != FALSE; }
+ private:
+ SRWLOCK m_Lock;
+ };
+ #define VMA_RW_MUTEX VmaRWMutex
+ #else
+ // Less efficient fallback: Use normal mutex.
+ class VmaRWMutex
+ {
+ public:
+ void LockRead() { m_Mutex.Lock(); }
+ void UnlockRead() { m_Mutex.Unlock(); }
+ bool TryLockRead() { return m_Mutex.TryLock(); }
+ void LockWrite() { m_Mutex.Lock(); }
+ void UnlockWrite() { m_Mutex.Unlock(); }
+ bool TryLockWrite() { return m_Mutex.TryLock(); }
+ private:
+ VMA_MUTEX m_Mutex;
+ };
+ #define VMA_RW_MUTEX VmaRWMutex
+ #endif // #if VMA_USE_STL_SHARED_MUTEX
#endif // #ifndef VMA_RW_MUTEX
/*
-If providing your own implementation, you need to implement a subset of std::atomic:
-
-- Constructor(uint32_t desired)
-- uint32_t load() const
-- void store(uint32_t desired)
-- bool compare_exchange_weak(uint32_t& expected, uint32_t desired)
+If providing your own implementation, you need to implement a subset of std::atomic.
*/
#ifndef VMA_ATOMIC_UINT32
-#include
-#define VMA_ATOMIC_UINT32 std::atomic
+ #include
+ #define VMA_ATOMIC_UINT32 std::atomic
+#endif
+
+#ifndef VMA_ATOMIC_UINT64
+ #include
+ #define VMA_ATOMIC_UINT64 std::atomic
#endif
#ifndef VMA_DEBUG_ALWAYS_DEDICATED_MEMORY
-/**
- Every allocation will have its own memory block.
- Define to 1 for debugging purposes only.
- */
-#define VMA_DEBUG_ALWAYS_DEDICATED_MEMORY (0)
+ /**
+ Every allocation will have its own memory block.
+ Define to 1 for debugging purposes only.
+ */
+ #define VMA_DEBUG_ALWAYS_DEDICATED_MEMORY (0)
#endif
#ifndef VMA_DEBUG_ALIGNMENT
-/**
- Minimum alignment of all allocations, in bytes.
- Set to more than 1 for debugging purposes only. Must be power of two.
- */
-#define VMA_DEBUG_ALIGNMENT (1)
+ /**
+ Minimum alignment of all allocations, in bytes.
+ Set to more than 1 for debugging purposes only. Must be power of two.
+ */
+ #define VMA_DEBUG_ALIGNMENT (1)
#endif
#ifndef VMA_DEBUG_MARGIN
-/**
- Minimum margin before and after every allocation, in bytes.
- Set nonzero for debugging purposes only.
- */
-#define VMA_DEBUG_MARGIN (0)
+ /**
+ Minimum margin before and after every allocation, in bytes.
+ Set nonzero for debugging purposes only.
+ */
+ #define VMA_DEBUG_MARGIN (0)
#endif
#ifndef VMA_DEBUG_INITIALIZE_ALLOCATIONS
-/**
- Define this macro to 1 to automatically fill new allocations and destroyed
- allocations with some bit pattern.
- */
-#define VMA_DEBUG_INITIALIZE_ALLOCATIONS (0)
+ /**
+ Define this macro to 1 to automatically fill new allocations and destroyed
+ allocations with some bit pattern.
+ */
+ #define VMA_DEBUG_INITIALIZE_ALLOCATIONS (0)
#endif
#ifndef VMA_DEBUG_DETECT_CORRUPTION
-/**
- Define this macro to 1 together with non-zero value of VMA_DEBUG_MARGIN to
- enable writing magic value to the margin before and after every allocation and
- validating it, so that memory corruptions (out-of-bounds writes) are detected.
- */
-#define VMA_DEBUG_DETECT_CORRUPTION (0)
+ /**
+ Define this macro to 1 together with non-zero value of VMA_DEBUG_MARGIN to
+ enable writing magic value to the margin before and after every allocation and
+ validating it, so that memory corruptions (out-of-bounds writes) are detected.
+ */
+ #define VMA_DEBUG_DETECT_CORRUPTION (0)
#endif
#ifndef VMA_DEBUG_GLOBAL_MUTEX
-/**
- Set this to 1 for debugging purposes only, to enable single mutex protecting all
- entry calls to the library. Can be useful for debugging multithreading issues.
- */
-#define VMA_DEBUG_GLOBAL_MUTEX (0)
+ /**
+ Set this to 1 for debugging purposes only, to enable single mutex protecting all
+ entry calls to the library. Can be useful for debugging multithreading issues.
+ */
+ #define VMA_DEBUG_GLOBAL_MUTEX (0)
#endif
#ifndef VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY
-/**
- Minimum value for VkPhysicalDeviceLimits::bufferImageGranularity.
- Set to more than 1 for debugging purposes only. Must be power of two.
- */
-#define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY (1)
+ /**
+ Minimum value for VkPhysicalDeviceLimits::bufferImageGranularity.
+ Set to more than 1 for debugging purposes only. Must be power of two.
+ */
+ #define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY (1)
#endif
#ifndef VMA_SMALL_HEAP_MAX_SIZE
-/// Maximum size of a memory heap in Vulkan to consider it "small".
-#define VMA_SMALL_HEAP_MAX_SIZE (1024ull * 1024 * 1024)
+ /// Maximum size of a memory heap in Vulkan to consider it "small".
+ #define VMA_SMALL_HEAP_MAX_SIZE (1024ull * 1024 * 1024)
#endif
#ifndef VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE
-/// Default size of a block allocated as single VkDeviceMemory from a "large" heap.
-#define VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE (256ull * 1024 * 1024)
+ /// Default size of a block allocated as single VkDeviceMemory from a "large" heap.
+ #define VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE (256ull * 1024 * 1024)
#endif
#ifndef VMA_CLASS_NO_COPY
-#define VMA_CLASS_NO_COPY(className) \
-private: \
- className(const className &) = delete; \
- className &operator=(const className &) = delete;
+ #define VMA_CLASS_NO_COPY(className) \
+ private: \
+ className(const className&) = delete; \
+ className& operator=(const className&) = delete;
#endif
static const uint32_t VMA_FRAME_INDEX_LOST = UINT32_MAX;
@@ -3468,25 +3928,31 @@ static const uint32_t VMA_FRAME_INDEX_LOST = UINT32_MAX;
// Decimal 2139416166, float NaN, little-endian binary 66 E6 84 7F.
static const uint32_t VMA_CORRUPTION_DETECTION_MAGIC_VALUE = 0x7F84E666;
-static const uint8_t VMA_ALLOCATION_FILL_PATTERN_CREATED = 0xDC;
+static const uint8_t VMA_ALLOCATION_FILL_PATTERN_CREATED = 0xDC;
static const uint8_t VMA_ALLOCATION_FILL_PATTERN_DESTROYED = 0xEF;
/*******************************************************************************
END OF CONFIGURATION
*/
+// # Copy of some Vulkan definitions so we don't need to check their existence just to handle few constants.
+
+static const uint32_t VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY = 0x00000040;
+static const uint32_t VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY = 0x00000080;
+
+
static const uint32_t VMA_ALLOCATION_INTERNAL_STRATEGY_MIN_OFFSET = 0x10000000u;
static VkAllocationCallbacks VmaEmptyAllocationCallbacks = {
- VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL
-};
+ VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL, VMA_NULL };
// Returns number of bits set to 1 in (v).
-static inline uint32_t VmaCountBitsSet(uint32_t v) {
+static inline uint32_t VmaCountBitsSet(uint32_t v)
+{
uint32_t c = v - ((v >> 1) & 0x55555555);
- c = ((c >> 2) & 0x33333333) + (c & 0x33333333);
- c = ((c >> 4) + c) & 0x0F0F0F0F;
- c = ((c >> 8) + c) & 0x00FF00FF;
+ c = ((c >> 2) & 0x33333333) + (c & 0x33333333);
+ c = ((c >> 4) + c) & 0x0F0F0F0F;
+ c = ((c >> 8) + c) & 0x00FF00FF;
c = ((c >> 16) + c) & 0x0000FFFF;
return c;
}
@@ -3494,19 +3960,22 @@ static inline uint32_t VmaCountBitsSet(uint32_t v) {
// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16.
// Use types like uint32_t, uint64_t as T.
template
-static inline T VmaAlignUp(T val, T align) {
+static inline T VmaAlignUp(T val, T align)
+{
return (val + align - 1) / align * align;
}
// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8.
// Use types like uint32_t, uint64_t as T.
template
-static inline T VmaAlignDown(T val, T align) {
+static inline T VmaAlignDown(T val, T align)
+{
return val / align * align;
}
// Division with mathematical rounding to nearest number.
template
-static inline T VmaRoundDiv(T x, T y) {
+static inline T VmaRoundDiv(T x, T y)
+{
return (x + (y / (T)2)) / y;
}
@@ -3516,12 +3985,14 @@ T must be unsigned integer number or signed integer but always nonnegative.
For 0 returns true.
*/
template
-inline bool VmaIsPow2(T x) {
- return (x & (x - 1)) == 0;
+inline bool VmaIsPow2(T x)
+{
+ return (x & (x-1)) == 0;
}
// Returns smallest power of 2 greater or equal to v.
-static inline uint32_t VmaNextPow2(uint32_t v) {
+static inline uint32_t VmaNextPow2(uint32_t v)
+{
v--;
v |= v >> 1;
v |= v >> 2;
@@ -3531,7 +4002,8 @@ static inline uint32_t VmaNextPow2(uint32_t v) {
v++;
return v;
}
-static inline uint64_t VmaNextPow2(uint64_t v) {
+static inline uint64_t VmaNextPow2(uint64_t v)
+{
v--;
v |= v >> 1;
v |= v >> 2;
@@ -3544,7 +4016,8 @@ static inline uint64_t VmaNextPow2(uint64_t v) {
}
// Returns largest power of 2 less or equal to v.
-static inline uint32_t VmaPrevPow2(uint32_t v) {
+static inline uint32_t VmaPrevPow2(uint32_t v)
+{
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
@@ -3553,7 +4026,8 @@ static inline uint32_t VmaPrevPow2(uint32_t v) {
v = v ^ (v >> 1);
return v;
}
-static inline uint64_t VmaPrevPow2(uint64_t v) {
+static inline uint64_t VmaPrevPow2(uint64_t v)
+{
v |= v >> 1;
v |= v >> 2;
v |= v >> 4;
@@ -3564,23 +4038,26 @@ static inline uint64_t VmaPrevPow2(uint64_t v) {
return v;
}
-static inline bool VmaStrIsEmpty(const char *pStr) {
+static inline bool VmaStrIsEmpty(const char* pStr)
+{
return pStr == VMA_NULL || *pStr == '\0';
}
#if VMA_STATS_STRING_ENABLED
-static const char *VmaAlgorithmToStr(uint32_t algorithm) {
- switch (algorithm) {
- case VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT:
- return "Linear";
- case VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT:
- return "Buddy";
- case 0:
- return "Default";
- default:
- VMA_ASSERT(0);
- return "";
+static const char* VmaAlgorithmToStr(uint32_t algorithm)
+{
+ switch(algorithm)
+ {
+ case VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT:
+ return "Linear";
+ case VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT:
+ return "Buddy";
+ case 0:
+ return "Default";
+ default:
+ VMA_ASSERT(0);
+ return "";
}
}
@@ -3588,28 +4065,34 @@ static const char *VmaAlgorithmToStr(uint32_t algorithm) {
#ifndef VMA_SORT
-template
-Iterator VmaQuickSortPartition(Iterator beg, Iterator end, Compare cmp) {
- Iterator centerValue = end;
- --centerValue;
+template
+Iterator VmaQuickSortPartition(Iterator beg, Iterator end, Compare cmp)
+{
+ Iterator centerValue = end; --centerValue;
Iterator insertIndex = beg;
- for (Iterator memTypeIndex = beg; memTypeIndex < centerValue; ++memTypeIndex) {
- if (cmp(*memTypeIndex, *centerValue)) {
- if (insertIndex != memTypeIndex) {
+ for(Iterator memTypeIndex = beg; memTypeIndex < centerValue; ++memTypeIndex)
+ {
+ if(cmp(*memTypeIndex, *centerValue))
+ {
+ if(insertIndex != memTypeIndex)
+ {
VMA_SWAP(*memTypeIndex, *insertIndex);
}
++insertIndex;
}
}
- if (insertIndex != centerValue) {
+ if(insertIndex != centerValue)
+ {
VMA_SWAP(*insertIndex, *centerValue);
}
return insertIndex;
}
-template
-void VmaQuickSort(Iterator beg, Iterator end, Compare cmp) {
- if (beg < end) {
+template
+void VmaQuickSort(Iterator beg, Iterator end, Compare cmp)
+{
+ if(beg < end)
+ {
Iterator it = VmaQuickSortPartition(beg, end, cmp);
VmaQuickSort(beg, it, cmp);
VmaQuickSort(it + 1, end, cmp);
@@ -3628,10 +4111,11 @@ Algorithm is based on "Vulkan 1.0.39 - A Specification (with all registered Vulk
chapter 11.6 "Resource Memory Association", paragraph "Buffer-Image Granularity".
*/
static inline bool VmaBlocksOnSamePage(
- VkDeviceSize resourceAOffset,
- VkDeviceSize resourceASize,
- VkDeviceSize resourceBOffset,
- VkDeviceSize pageSize) {
+ VkDeviceSize resourceAOffset,
+ VkDeviceSize resourceASize,
+ VkDeviceSize resourceBOffset,
+ VkDeviceSize pageSize)
+{
VMA_ASSERT(resourceAOffset + resourceASize <= resourceBOffset && resourceASize > 0 && pageSize > 0);
VkDeviceSize resourceAEnd = resourceAOffset + resourceASize - 1;
VkDeviceSize resourceAEndPage = resourceAEnd & ~(pageSize - 1);
@@ -3640,7 +4124,8 @@ static inline bool VmaBlocksOnSamePage(
return resourceAEndPage == resourceBStartPage;
}
-enum VmaSuballocationType {
+enum VmaSuballocationType
+{
VMA_SUBALLOCATION_TYPE_FREE = 0,
VMA_SUBALLOCATION_TYPE_UNKNOWN = 1,
VMA_SUBALLOCATION_TYPE_BUFFER = 2,
@@ -3657,50 +4142,67 @@ or linear image and another one is optimal image. If type is unknown, behave
conservatively.
*/
static inline bool VmaIsBufferImageGranularityConflict(
- VmaSuballocationType suballocType1,
- VmaSuballocationType suballocType2) {
- if (suballocType1 > suballocType2) {
+ VmaSuballocationType suballocType1,
+ VmaSuballocationType suballocType2)
+{
+ if(suballocType1 > suballocType2)
+ {
VMA_SWAP(suballocType1, suballocType2);
}
-
- switch (suballocType1) {
- case VMA_SUBALLOCATION_TYPE_FREE:
- return false;
- case VMA_SUBALLOCATION_TYPE_UNKNOWN:
- return true;
- case VMA_SUBALLOCATION_TYPE_BUFFER:
- return suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
- case VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN:
- return suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR ||
- suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
- case VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR:
- return suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
- case VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL:
- return false;
- default:
- VMA_ASSERT(0);
- return true;
+
+ switch(suballocType1)
+ {
+ case VMA_SUBALLOCATION_TYPE_FREE:
+ return false;
+ case VMA_SUBALLOCATION_TYPE_UNKNOWN:
+ return true;
+ case VMA_SUBALLOCATION_TYPE_BUFFER:
+ return
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
+ case VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN:
+ return
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN ||
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR ||
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
+ case VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR:
+ return
+ suballocType2 == VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL;
+ case VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL:
+ return false;
+ default:
+ VMA_ASSERT(0);
+ return true;
}
}
-static void VmaWriteMagicValue(void *pData, VkDeviceSize offset) {
- uint32_t *pDst = (uint32_t *)((char *)pData + offset);
+static void VmaWriteMagicValue(void* pData, VkDeviceSize offset)
+{
+#if VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_DETECT_CORRUPTION
+ uint32_t* pDst = (uint32_t*)((char*)pData + offset);
const size_t numberCount = VMA_DEBUG_MARGIN / sizeof(uint32_t);
- for (size_t i = 0; i < numberCount; ++i, ++pDst) {
+ for(size_t i = 0; i < numberCount; ++i, ++pDst)
+ {
*pDst = VMA_CORRUPTION_DETECTION_MAGIC_VALUE;
}
+#else
+ // no-op
+#endif
}
-static bool VmaValidateMagicValue(const void *pData, VkDeviceSize offset) {
- const uint32_t *pSrc = (const uint32_t *)((const char *)pData + offset);
+static bool VmaValidateMagicValue(const void* pData, VkDeviceSize offset)
+{
+#if VMA_DEBUG_MARGIN > 0 && VMA_DEBUG_DETECT_CORRUPTION
+ const uint32_t* pSrc = (const uint32_t*)((const char*)pData + offset);
const size_t numberCount = VMA_DEBUG_MARGIN / sizeof(uint32_t);
- for (size_t i = 0; i < numberCount; ++i, ++pSrc) {
- if (*pSrc != VMA_CORRUPTION_DETECTION_MAGIC_VALUE) {
+ for(size_t i = 0; i < numberCount; ++i, ++pSrc)
+ {
+ if(*pSrc != VMA_CORRUPTION_DETECTION_MAGIC_VALUE)
+ {
return false;
}
}
+#endif
return true;
}
@@ -3708,7 +4210,8 @@ static bool VmaValidateMagicValue(const void *pData, VkDeviceSize offset) {
Fills structure with parameters of an example buffer to be used for transfers
during GPU memory defragmentation.
*/
-static void VmaFillGpuDefragmentationBufferCreateInfo(VkBufferCreateInfo &outBufCreateInfo) {
+static void VmaFillGpuDefragmentationBufferCreateInfo(VkBufferCreateInfo& outBufCreateInfo)
+{
memset(&outBufCreateInfo, 0, sizeof(outBufCreateInfo));
outBufCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
outBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
@@ -3716,70 +4219,50 @@ static void VmaFillGpuDefragmentationBufferCreateInfo(VkBufferCreateInfo &outBuf
}
// Helper RAII class to lock a mutex in constructor and unlock it in destructor (at the end of scope).
-struct VmaMutexLock {
+struct VmaMutexLock
+{
VMA_CLASS_NO_COPY(VmaMutexLock)
public:
- VmaMutexLock(VMA_MUTEX &mutex, bool useMutex = true) :
- m_pMutex(useMutex ? &mutex : VMA_NULL) {
- if (m_pMutex) {
- m_pMutex->Lock();
- }
- }
- ~VmaMutexLock() {
- if (m_pMutex) {
- m_pMutex->Unlock();
- }
- }
-
+ VmaMutexLock(VMA_MUTEX& mutex, bool useMutex = true) :
+ m_pMutex(useMutex ? &mutex : VMA_NULL)
+ { if(m_pMutex) { m_pMutex->Lock(); } }
+ ~VmaMutexLock()
+ { if(m_pMutex) { m_pMutex->Unlock(); } }
private:
- VMA_MUTEX *m_pMutex;
+ VMA_MUTEX* m_pMutex;
};
// Helper RAII class to lock a RW mutex in constructor and unlock it in destructor (at the end of scope), for reading.
-struct VmaMutexLockRead {
+struct VmaMutexLockRead
+{
VMA_CLASS_NO_COPY(VmaMutexLockRead)
public:
- VmaMutexLockRead(VMA_RW_MUTEX &mutex, bool useMutex) :
- m_pMutex(useMutex ? &mutex : VMA_NULL) {
- if (m_pMutex) {
- m_pMutex->LockRead();
- }
- }
- ~VmaMutexLockRead() {
- if (m_pMutex) {
- m_pMutex->UnlockRead();
- }
- }
-
+ VmaMutexLockRead(VMA_RW_MUTEX& mutex, bool useMutex) :
+ m_pMutex(useMutex ? &mutex : VMA_NULL)
+ { if(m_pMutex) { m_pMutex->LockRead(); } }
+ ~VmaMutexLockRead() { if(m_pMutex) { m_pMutex->UnlockRead(); } }
private:
- VMA_RW_MUTEX *m_pMutex;
+ VMA_RW_MUTEX* m_pMutex;
};
// Helper RAII class to lock a RW mutex in constructor and unlock it in destructor (at the end of scope), for writing.
-struct VmaMutexLockWrite {
+struct VmaMutexLockWrite
+{
VMA_CLASS_NO_COPY(VmaMutexLockWrite)
public:
- VmaMutexLockWrite(VMA_RW_MUTEX &mutex, bool useMutex) :
- m_pMutex(useMutex ? &mutex : VMA_NULL) {
- if (m_pMutex) {
- m_pMutex->LockWrite();
- }
- }
- ~VmaMutexLockWrite() {
- if (m_pMutex) {
- m_pMutex->UnlockWrite();
- }
- }
-
+ VmaMutexLockWrite(VMA_RW_MUTEX& mutex, bool useMutex) :
+ m_pMutex(useMutex ? &mutex : VMA_NULL)
+ { if(m_pMutex) { m_pMutex->LockWrite(); } }
+ ~VmaMutexLockWrite() { if(m_pMutex) { m_pMutex->UnlockWrite(); } }
private:
- VMA_RW_MUTEX *m_pMutex;
+ VMA_RW_MUTEX* m_pMutex;
};
#if VMA_DEBUG_GLOBAL_MUTEX
-static VMA_MUTEX gDebugGlobalMutex;
-#define VMA_DEBUG_GLOBAL_MUTEX_LOCK VmaMutexLock debugGlobalMutexLock(gDebugGlobalMutex, true);
+ static VMA_MUTEX gDebugGlobalMutex;
+ #define VMA_DEBUG_GLOBAL_MUTEX_LOCK VmaMutexLock debugGlobalMutexLock(gDebugGlobalMutex, true);
#else
-#define VMA_DEBUG_GLOBAL_MUTEX_LOCK
+ #define VMA_DEBUG_GLOBAL_MUTEX_LOCK
#endif
// Minimum size of a free suballocation to register it in the free suballocation collection.
@@ -3795,33 +4278,56 @@ Returned value is the found element, if present in the collection or place where
new element with value (key) should be inserted.
*/
template
-static IterT VmaBinaryFindFirstNotLess(IterT beg, IterT end, const KeyT &key, CmpLess cmp) {
+static IterT VmaBinaryFindFirstNotLess(IterT beg, IterT end, const KeyT &key, const CmpLess& cmp)
+{
size_t down = 0, up = (end - beg);
- while (down < up) {
+ while(down < up)
+ {
const size_t mid = (down + up) / 2;
- if (cmp(*(beg + mid), key)) {
+ if(cmp(*(beg+mid), key))
+ {
down = mid + 1;
- } else {
+ }
+ else
+ {
up = mid;
}
}
return beg + down;
}
+template
+IterT VmaBinaryFindSorted(const IterT& beg, const IterT& end, const KeyT& value, const CmpLess& cmp)
+{
+ IterT it = VmaBinaryFindFirstNotLess(
+ beg, end, value, cmp);
+ if(it == end ||
+ (!cmp(*it, value) && !cmp(value, *it)))
+ {
+ return it;
+ }
+ return end;
+}
+
/*
Returns true if all pointers in the array are not-null and unique.
Warning! O(n^2) complexity. Use only inside VMA_HEAVY_ASSERT.
T must be pointer type, e.g. VmaAllocation, VmaPool.
*/
-template
-static bool VmaValidatePointerArray(uint32_t count, const T *arr) {
- for (uint32_t i = 0; i < count; ++i) {
+template
+static bool VmaValidatePointerArray(uint32_t count, const T* arr)
+{
+ for(uint32_t i = 0; i < count; ++i)
+ {
const T iPtr = arr[i];
- if (iPtr == VMA_NULL) {
+ if(iPtr == VMA_NULL)
+ {
return false;
}
- for (uint32_t j = i + 1; j < count; ++j) {
- if (iPtr == arr[j]) {
+ for(uint32_t j = i + 1; j < count; ++j)
+ {
+ if(iPtr == arr[j])
+ {
return false;
}
}
@@ -3832,97 +4338,137 @@ static bool VmaValidatePointerArray(uint32_t count, const T *arr) {
////////////////////////////////////////////////////////////////////////////////
// Memory allocation
-static void *VmaMalloc(const VkAllocationCallbacks *pAllocationCallbacks, size_t size, size_t alignment) {
- if ((pAllocationCallbacks != VMA_NULL) &&
- (pAllocationCallbacks->pfnAllocation != VMA_NULL)) {
+static void* VmaMalloc(const VkAllocationCallbacks* pAllocationCallbacks, size_t size, size_t alignment)
+{
+ if((pAllocationCallbacks != VMA_NULL) &&
+ (pAllocationCallbacks->pfnAllocation != VMA_NULL))
+ {
return (*pAllocationCallbacks->pfnAllocation)(
- pAllocationCallbacks->pUserData,
- size,
- alignment,
- VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
- } else {
+ pAllocationCallbacks->pUserData,
+ size,
+ alignment,
+ VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
+ }
+ else
+ {
return VMA_SYSTEM_ALIGNED_MALLOC(size, alignment);
}
}
-static void VmaFree(const VkAllocationCallbacks *pAllocationCallbacks, void *ptr) {
- if ((pAllocationCallbacks != VMA_NULL) &&
- (pAllocationCallbacks->pfnFree != VMA_NULL)) {
+static void VmaFree(const VkAllocationCallbacks* pAllocationCallbacks, void* ptr)
+{
+ if((pAllocationCallbacks != VMA_NULL) &&
+ (pAllocationCallbacks->pfnFree != VMA_NULL))
+ {
(*pAllocationCallbacks->pfnFree)(pAllocationCallbacks->pUserData, ptr);
- } else {
+ }
+ else
+ {
VMA_SYSTEM_FREE(ptr);
}
}
-template
-static T *VmaAllocate(const VkAllocationCallbacks *pAllocationCallbacks) {
- return (T *)VmaMalloc(pAllocationCallbacks, sizeof(T), VMA_ALIGN_OF(T));
+template
+static T* VmaAllocate(const VkAllocationCallbacks* pAllocationCallbacks)
+{
+ return (T*)VmaMalloc(pAllocationCallbacks, sizeof(T), VMA_ALIGN_OF(T));
}
-template
-static T *VmaAllocateArray(const VkAllocationCallbacks *pAllocationCallbacks, size_t count) {
- return (T *)VmaMalloc(pAllocationCallbacks, sizeof(T) * count, VMA_ALIGN_OF(T));
+template
+static T* VmaAllocateArray(const VkAllocationCallbacks* pAllocationCallbacks, size_t count)
+{
+ return (T*)VmaMalloc(pAllocationCallbacks, sizeof(T) * count, VMA_ALIGN_OF(T));
}
-#define vma_new(allocator, type) new (VmaAllocate(allocator))(type)
+#define vma_new(allocator, type) new(VmaAllocate(allocator))(type)
-#define vma_new_array(allocator, type, count) new (VmaAllocateArray((allocator), (count)))(type)
+#define vma_new_array(allocator, type, count) new(VmaAllocateArray((allocator), (count)))(type)
-template
-static void vma_delete(const VkAllocationCallbacks *pAllocationCallbacks, T *ptr) {
+template
+static void vma_delete(const VkAllocationCallbacks* pAllocationCallbacks, T* ptr)
+{
ptr->~T();
VmaFree(pAllocationCallbacks, ptr);
}
-template
-static void vma_delete_array(const VkAllocationCallbacks *pAllocationCallbacks, T *ptr, size_t count) {
- if (ptr != VMA_NULL) {
- for (size_t i = count; i--;) {
+template
+static void vma_delete_array(const VkAllocationCallbacks* pAllocationCallbacks, T* ptr, size_t count)
+{
+ if(ptr != VMA_NULL)
+ {
+ for(size_t i = count; i--; )
+ {
ptr[i].~T();
}
VmaFree(pAllocationCallbacks, ptr);
}
}
+static char* VmaCreateStringCopy(const VkAllocationCallbacks* allocs, const char* srcStr)
+{
+ if(srcStr != VMA_NULL)
+ {
+ const size_t len = strlen(srcStr);
+ char* const result = vma_new_array(allocs, char, len + 1);
+ memcpy(result, srcStr, len + 1);
+ return result;
+ }
+ else
+ {
+ return VMA_NULL;
+ }
+}
+
+static void VmaFreeString(const VkAllocationCallbacks* allocs, char* str)
+{
+ if(str != VMA_NULL)
+ {
+ const size_t len = strlen(str);
+ vma_delete_array(allocs, str, len + 1);
+ }
+}
+
// STL-compatible allocator.
-template
-class VmaStlAllocator {
+template
+class VmaStlAllocator
+{
public:
- const VkAllocationCallbacks *const m_pCallbacks;
+ const VkAllocationCallbacks* const m_pCallbacks;
typedef T value_type;
+
+ VmaStlAllocator(const VkAllocationCallbacks* pCallbacks) : m_pCallbacks(pCallbacks) { }
+ template VmaStlAllocator(const VmaStlAllocator& src) : m_pCallbacks(src.m_pCallbacks) { }
- VmaStlAllocator(const VkAllocationCallbacks *pCallbacks) :
- m_pCallbacks(pCallbacks) {}
- template
- VmaStlAllocator(const VmaStlAllocator &src) :
- m_pCallbacks(src.m_pCallbacks) {}
+ T* allocate(size_t n) { return VmaAllocateArray(m_pCallbacks, n); }
+ void deallocate(T* p, size_t n) { VmaFree(m_pCallbacks, p); }
- T *allocate(size_t n) { return VmaAllocateArray(m_pCallbacks, n); }
- void deallocate(T *p, size_t n) { VmaFree(m_pCallbacks, p); }
-
- template
- bool operator==(const VmaStlAllocator &rhs) const {
+ template
+ bool operator==(const VmaStlAllocator& rhs) const
+ {
return m_pCallbacks == rhs.m_pCallbacks;
}
- template
- bool operator!=(const VmaStlAllocator &rhs) const {
+ template
+ bool operator!=(const VmaStlAllocator& rhs) const
+ {
return m_pCallbacks != rhs.m_pCallbacks;
}
- VmaStlAllocator &operator=(const VmaStlAllocator &x) = delete;
+ VmaStlAllocator& operator=(const VmaStlAllocator& x) = delete;
};
#if VMA_USE_STL_VECTOR
#define VmaVector std::vector
-template
-static void VmaVectorInsert(std::vector &vec, size_t index, const T &item) {
+template
+static void VmaVectorInsert(std::vector& vec, size_t index, const T& item)
+{
vec.insert(vec.begin() + index, item);
}
-template
-static void VmaVectorRemove(std::vector &vec, size_t index) {
+template
+static void VmaVectorRemove(std::vector& vec, size_t index)
+{
vec.erase(vec.begin() + index);
}
@@ -3931,90 +4477,114 @@ static void VmaVectorRemove(std::vector &vec, size_t index) {
/* Class with interface compatible with subset of std::vector.
T must be POD because constructors and destructors are not called and memcpy is
used for these objects. */
-template
-class VmaVector {
+template
+class VmaVector
+{
public:
typedef T value_type;
- VmaVector(const AllocatorT &allocator) :
- m_Allocator(allocator),
- m_pArray(VMA_NULL),
- m_Count(0),
- m_Capacity(0) {
+ VmaVector(const AllocatorT& allocator) :
+ m_Allocator(allocator),
+ m_pArray(VMA_NULL),
+ m_Count(0),
+ m_Capacity(0)
+ {
}
- VmaVector(size_t count, const AllocatorT &allocator) :
- m_Allocator(allocator),
- m_pArray(count ? (T *)VmaAllocateArray(allocator.m_pCallbacks, count) : VMA_NULL),
- m_Count(count),
- m_Capacity(count) {
+ VmaVector(size_t count, const AllocatorT& allocator) :
+ m_Allocator(allocator),
+ m_pArray(count ? (T*)VmaAllocateArray(allocator.m_pCallbacks, count) : VMA_NULL),
+ m_Count(count),
+ m_Capacity(count)
+ {
}
-
- VmaVector(const VmaVector &src) :
- m_Allocator(src.m_Allocator),
- m_pArray(src.m_Count ? (T *)VmaAllocateArray(src.m_Allocator.m_pCallbacks, src.m_Count) : VMA_NULL),
- m_Count(src.m_Count),
- m_Capacity(src.m_Count) {
- if (m_Count != 0) {
+
+ // This version of the constructor is here for compatibility with pre-C++14 std::vector.
+ // value is unused.
+ VmaVector(size_t count, const T& value, const AllocatorT& allocator)
+ : VmaVector(count, allocator) {}
+
+ VmaVector(const VmaVector& src) :
+ m_Allocator(src.m_Allocator),
+ m_pArray(src.m_Count ? (T*)VmaAllocateArray(src.m_Allocator.m_pCallbacks, src.m_Count) : VMA_NULL),
+ m_Count(src.m_Count),
+ m_Capacity(src.m_Count)
+ {
+ if(m_Count != 0)
+ {
memcpy(m_pArray, src.m_pArray, m_Count * sizeof(T));
}
}
-
- ~VmaVector() {
+
+ ~VmaVector()
+ {
VmaFree(m_Allocator.m_pCallbacks, m_pArray);
}
- VmaVector &operator=(const VmaVector &rhs) {
- if (&rhs != this) {
+ VmaVector& operator=(const VmaVector& rhs)
+ {
+ if(&rhs != this)
+ {
resize(rhs.m_Count);
- if (m_Count != 0) {
+ if(m_Count != 0)
+ {
memcpy(m_pArray, rhs.m_pArray, m_Count * sizeof(T));
}
}
return *this;
}
-
+
bool empty() const { return m_Count == 0; }
size_t size() const { return m_Count; }
- T *data() { return m_pArray; }
- const T *data() const { return m_pArray; }
-
- T &operator[](size_t index) {
+ T* data() { return m_pArray; }
+ const T* data() const { return m_pArray; }
+
+ T& operator[](size_t index)
+ {
VMA_HEAVY_ASSERT(index < m_Count);
return m_pArray[index];
}
- const T &operator[](size_t index) const {
+ const T& operator[](size_t index) const
+ {
VMA_HEAVY_ASSERT(index < m_Count);
return m_pArray[index];
}
- T &front() {
+ T& front()
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
return m_pArray[0];
}
- const T &front() const {
+ const T& front() const
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
return m_pArray[0];
}
- T &back() {
+ T& back()
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
return m_pArray[m_Count - 1];
}
- const T &back() const {
+ const T& back() const
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
return m_pArray[m_Count - 1];
}
- void reserve(size_t newCapacity, bool freeMemory = false) {
+ void reserve(size_t newCapacity, bool freeMemory = false)
+ {
newCapacity = VMA_MAX(newCapacity, m_Count);
-
- if ((newCapacity < m_Capacity) && !freeMemory) {
+
+ if((newCapacity < m_Capacity) && !freeMemory)
+ {
newCapacity = m_Capacity;
}
-
- if (newCapacity != m_Capacity) {
- T *const newArray = newCapacity ? VmaAllocateArray(m_Allocator, newCapacity) : VMA_NULL;
- if (m_Count != 0) {
+
+ if(newCapacity != m_Capacity)
+ {
+ T* const newArray = newCapacity ? VmaAllocateArray(m_Allocator, newCapacity) : VMA_NULL;
+ if(m_Count != 0)
+ {
memcpy(newArray, m_pArray, m_Count * sizeof(T));
}
VmaFree(m_Allocator.m_pCallbacks, m_pArray);
@@ -4023,18 +4593,24 @@ public:
}
}
- void resize(size_t newCount, bool freeMemory = false) {
+ void resize(size_t newCount, bool freeMemory = false)
+ {
size_t newCapacity = m_Capacity;
- if (newCount > m_Capacity) {
+ if(newCount > m_Capacity)
+ {
newCapacity = VMA_MAX(newCount, VMA_MAX(m_Capacity * 3 / 2, (size_t)8));
- } else if (freeMemory) {
+ }
+ else if(freeMemory)
+ {
newCapacity = newCount;
}
- if (newCapacity != m_Capacity) {
- T *const newArray = newCapacity ? VmaAllocateArray(m_Allocator.m_pCallbacks, newCapacity) : VMA_NULL;
+ if(newCapacity != m_Capacity)
+ {
+ T* const newArray = newCapacity ? VmaAllocateArray(m_Allocator.m_pCallbacks, newCapacity) : VMA_NULL;
const size_t elementsToCopy = VMA_MIN(m_Count, newCount);
- if (elementsToCopy != 0) {
+ if(elementsToCopy != 0)
+ {
memcpy(newArray, m_pArray, elementsToCopy * sizeof(T));
}
VmaFree(m_Allocator.m_pCallbacks, m_pArray);
@@ -4045,94 +4621,107 @@ public:
m_Count = newCount;
}
- void clear(bool freeMemory = false) {
+ void clear(bool freeMemory = false)
+ {
resize(0, freeMemory);
}
- void insert(size_t index, const T &src) {
+ void insert(size_t index, const T& src)
+ {
VMA_HEAVY_ASSERT(index <= m_Count);
const size_t oldCount = size();
resize(oldCount + 1);
- if (index < oldCount) {
+ if(index < oldCount)
+ {
memmove(m_pArray + (index + 1), m_pArray + index, (oldCount - index) * sizeof(T));
}
m_pArray[index] = src;
}
- void remove(size_t index) {
+ void remove(size_t index)
+ {
VMA_HEAVY_ASSERT(index < m_Count);
const size_t oldCount = size();
- if (index < oldCount - 1) {
+ if(index < oldCount - 1)
+ {
memmove(m_pArray + index, m_pArray + (index + 1), (oldCount - index - 1) * sizeof(T));
}
resize(oldCount - 1);
}
- void push_back(const T &src) {
+ void push_back(const T& src)
+ {
const size_t newIndex = size();
resize(newIndex + 1);
m_pArray[newIndex] = src;
}
- void pop_back() {
+ void pop_back()
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
resize(size() - 1);
}
- void push_front(const T &src) {
+ void push_front(const T& src)
+ {
insert(0, src);
}
- void pop_front() {
+ void pop_front()
+ {
VMA_HEAVY_ASSERT(m_Count > 0);
remove(0);
}
- typedef T *iterator;
+ typedef T* iterator;
iterator begin() { return m_pArray; }
iterator end() { return m_pArray + m_Count; }
private:
AllocatorT m_Allocator;
- T *m_pArray;
+ T* m_pArray;
size_t m_Count;
size_t m_Capacity;
};
-template
-static void VmaVectorInsert(VmaVector &vec, size_t index, const T &item) {
+template
+static void VmaVectorInsert(VmaVector& vec, size_t index, const T& item)
+{
vec.insert(index, item);
}
-template
-static void VmaVectorRemove(VmaVector &vec, size_t index) {
+template
+static void VmaVectorRemove(VmaVector& vec, size_t index)
+{
vec.remove(index);
}
#endif // #if VMA_USE_STL_VECTOR
-template
-size_t VmaVectorInsertSorted(VectorT &vector, const typename VectorT::value_type &value) {
+template
+size_t VmaVectorInsertSorted(VectorT& vector, const typename VectorT::value_type& value)
+{
const size_t indexToInsert = VmaBinaryFindFirstNotLess(
- vector.data(),
- vector.data() + vector.size(),
- value,
- CmpLess()) -
- vector.data();
+ vector.data(),
+ vector.data() + vector.size(),
+ value,
+ CmpLess()) - vector.data();
VmaVectorInsert(vector, indexToInsert, value);
return indexToInsert;
}
-template
-bool VmaVectorRemoveSorted(VectorT &vector, const typename VectorT::value_type &value) {
+template
+bool VmaVectorRemoveSorted(VectorT& vector, const typename VectorT::value_type& value)
+{
CmpLess comparator;
typename VectorT::iterator it = VmaBinaryFindFirstNotLess(
- vector.begin(),
- vector.end(),
- value,
- comparator);
- if ((it != vector.end()) && !comparator(*it, value) && !comparator(value, *it)) {
+ vector.begin(),
+ vector.end(),
+ value,
+ comparator);
+ if((it != vector.end()) && !comparator(*it, value) && !comparator(value, *it))
+ {
size_t indexToRemove = it - vector.begin();
VmaVectorRemove(vector, indexToRemove);
return true;
@@ -4140,18 +4729,6 @@ bool VmaVectorRemoveSorted(VectorT &vector, const typename VectorT::value_type &
return false;
}
-template
-IterT VmaVectorFindSorted(const IterT &beg, const IterT &end, const KeyT &value) {
- CmpLess comparator;
- IterT it = VmaBinaryFindFirstNotLess(
- beg, end, value, comparator);
- if (it == end ||
- (!comparator(*it, value) && !comparator(value, *it))) {
- return it;
- }
- return end;
-}
-
////////////////////////////////////////////////////////////////////////////////
// class VmaPoolAllocator
@@ -4160,86 +4737,96 @@ Allocator for objects of type T using a list of arrays (pools) to speed up
allocation. Number of elements that can be allocated is not bounded because
allocator can create multiple blocks.
*/
-template
-class VmaPoolAllocator {
+template
+class VmaPoolAllocator
+{
VMA_CLASS_NO_COPY(VmaPoolAllocator)
public:
- VmaPoolAllocator(const VkAllocationCallbacks *pAllocationCallbacks, uint32_t firstBlockCapacity);
+ VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity);
~VmaPoolAllocator();
- void Clear();
- T *Alloc();
- void Free(T *ptr);
+ template T* Alloc(Types... args);
+ void Free(T* ptr);
private:
- union Item {
+ union Item
+ {
uint32_t NextFreeIndex;
- T Value;
+ alignas(T) char Value[sizeof(T)];
};
- struct ItemBlock {
- Item *pItems;
+ struct ItemBlock
+ {
+ Item* pItems;
uint32_t Capacity;
uint32_t FirstFreeIndex;
};
-
- const VkAllocationCallbacks *m_pAllocationCallbacks;
+
+ const VkAllocationCallbacks* m_pAllocationCallbacks;
const uint32_t m_FirstBlockCapacity;
- VmaVector > m_ItemBlocks;
+ VmaVector< ItemBlock, VmaStlAllocator > m_ItemBlocks;
- ItemBlock &CreateNewBlock();
+ ItemBlock& CreateNewBlock();
};
-template
-VmaPoolAllocator::VmaPoolAllocator(const VkAllocationCallbacks *pAllocationCallbacks, uint32_t firstBlockCapacity) :
- m_pAllocationCallbacks(pAllocationCallbacks),
- m_FirstBlockCapacity(firstBlockCapacity),
- m_ItemBlocks(VmaStlAllocator(pAllocationCallbacks)) {
+template
+VmaPoolAllocator::VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity) :
+ m_pAllocationCallbacks(pAllocationCallbacks),
+ m_FirstBlockCapacity(firstBlockCapacity),
+ m_ItemBlocks(VmaStlAllocator(pAllocationCallbacks))
+{
VMA_ASSERT(m_FirstBlockCapacity > 1);
}
-template
-VmaPoolAllocator::~VmaPoolAllocator() {
- Clear();
-}
-
-template
-void VmaPoolAllocator::Clear() {
- for (size_t i = m_ItemBlocks.size(); i--;)
+template
+VmaPoolAllocator::~VmaPoolAllocator()
+{
+ for(size_t i = m_ItemBlocks.size(); i--; )
vma_delete_array(m_pAllocationCallbacks, m_ItemBlocks[i].pItems, m_ItemBlocks[i].Capacity);
m_ItemBlocks.clear();
}
-template
-T *VmaPoolAllocator::Alloc() {
- for (size_t i = m_ItemBlocks.size(); i--;) {
- ItemBlock &block = m_ItemBlocks[i];
+template
+template T* VmaPoolAllocator::Alloc(Types... args)
+{
+ for(size_t i = m_ItemBlocks.size(); i--; )
+ {
+ ItemBlock& block = m_ItemBlocks[i];
// This block has some free items: Use first one.
- if (block.FirstFreeIndex != UINT32_MAX) {
- Item *const pItem = &block.pItems[block.FirstFreeIndex];
+ if(block.FirstFreeIndex != UINT32_MAX)
+ {
+ Item* const pItem = &block.pItems[block.FirstFreeIndex];
block.FirstFreeIndex = pItem->NextFreeIndex;
- return &pItem->Value;
+ T* result = (T*)&pItem->Value;
+ new(result)T(std::forward(args)...); // Explicit constructor call.
+ return result;
}
}
// No block has free item: Create new one and use it.
- ItemBlock &newBlock = CreateNewBlock();
- Item *const pItem = &newBlock.pItems[0];
+ ItemBlock& newBlock = CreateNewBlock();
+ Item* const pItem = &newBlock.pItems[0];
newBlock.FirstFreeIndex = pItem->NextFreeIndex;
- return &pItem->Value;
+ T* result = (T*)&pItem->Value;
+ new(result)T(std::forward(args)...); // Explicit constructor call.
+ return result;
}
-template
-void VmaPoolAllocator::Free(T *ptr) {
+template
+void VmaPoolAllocator::Free(T* ptr)
+{
// Search all memory blocks to find ptr.
- for (size_t i = m_ItemBlocks.size(); i--;) {
- ItemBlock &block = m_ItemBlocks[i];
-
+ for(size_t i = m_ItemBlocks.size(); i--; )
+ {
+ ItemBlock& block = m_ItemBlocks[i];
+
// Casting to union.
- Item *pItemPtr;
+ Item* pItemPtr;
memcpy(&pItemPtr, &ptr, sizeof(pItemPtr));
-
+
// Check if pItemPtr is in address range of this block.
- if ((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity)) {
+ if((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity))
+ {
+ ptr->~T(); // Explicit destructor call.
const uint32_t index = static_cast(pItemPtr - block.pItems);
pItemPtr->NextFreeIndex = block.FirstFreeIndex;
block.FirstFreeIndex = index;
@@ -4249,22 +4836,21 @@ void VmaPoolAllocator::Free(T *ptr) {
VMA_ASSERT(0 && "Pointer doesn't belong to this memory pool.");
}
-template
-typename VmaPoolAllocator::ItemBlock &VmaPoolAllocator::CreateNewBlock() {
+template
+typename VmaPoolAllocator::ItemBlock& VmaPoolAllocator::CreateNewBlock()
+{
const uint32_t newBlockCapacity = m_ItemBlocks.empty() ?
- m_FirstBlockCapacity :
- m_ItemBlocks.back().Capacity * 3 / 2;
+ m_FirstBlockCapacity : m_ItemBlocks.back().Capacity * 3 / 2;
const ItemBlock newBlock = {
vma_new_array(m_pAllocationCallbacks, Item, newBlockCapacity),
newBlockCapacity,
- 0
- };
+ 0 };
m_ItemBlocks.push_back(newBlock);
// Setup singly-linked list of all free items in this block.
- for (uint32_t i = 0; i < newBlockCapacity - 1; ++i)
+ for(uint32_t i = 0; i < newBlockCapacity - 1; ++i)
newBlock.pItems[i].NextFreeIndex = i + 1;
newBlock.pItems[newBlockCapacity - 1].NextFreeIndex = UINT32_MAX;
return m_ItemBlocks.back();
@@ -4279,78 +4865,85 @@ typename VmaPoolAllocator::ItemBlock &VmaPoolAllocator::CreateNewBlock() {
#else // #if VMA_USE_STL_LIST
-template
-struct VmaListItem {
- VmaListItem *pPrev;
- VmaListItem *pNext;
+template
+struct VmaListItem
+{
+ VmaListItem* pPrev;
+ VmaListItem* pNext;
T Value;
};
// Doubly linked list.
-template
-class VmaRawList {
+template
+class VmaRawList
+{
VMA_CLASS_NO_COPY(VmaRawList)
public:
typedef VmaListItem ItemType;
- VmaRawList(const VkAllocationCallbacks *pAllocationCallbacks);
+ VmaRawList(const VkAllocationCallbacks* pAllocationCallbacks);
~VmaRawList();
void Clear();
size_t GetCount() const { return m_Count; }
bool IsEmpty() const { return m_Count == 0; }
- ItemType *Front() { return m_pFront; }
- const ItemType *Front() const { return m_pFront; }
- ItemType *Back() { return m_pBack; }
- const ItemType *Back() const { return m_pBack; }
+ ItemType* Front() { return m_pFront; }
+ const ItemType* Front() const { return m_pFront; }
+ ItemType* Back() { return m_pBack; }
+ const ItemType* Back() const { return m_pBack; }
- ItemType *PushBack();
- ItemType *PushFront();
- ItemType *PushBack(const T &value);
- ItemType *PushFront(const T &value);
+ ItemType* PushBack();
+ ItemType* PushFront();
+ ItemType* PushBack(const T& value);
+ ItemType* PushFront(const T& value);
void PopBack();
void PopFront();
-
+
// Item can be null - it means PushBack.
- ItemType *InsertBefore(ItemType *pItem);
+ ItemType* InsertBefore(ItemType* pItem);
// Item can be null - it means PushFront.
- ItemType *InsertAfter(ItemType *pItem);
+ ItemType* InsertAfter(ItemType* pItem);
- ItemType *InsertBefore(ItemType *pItem, const T &value);
- ItemType *InsertAfter(ItemType *pItem, const T &value);
+ ItemType* InsertBefore(ItemType* pItem, const T& value);
+ ItemType* InsertAfter(ItemType* pItem, const T& value);
- void Remove(ItemType *pItem);
+ void Remove(ItemType* pItem);
private:
- const VkAllocationCallbacks *const m_pAllocationCallbacks;
+ const VkAllocationCallbacks* const m_pAllocationCallbacks;
VmaPoolAllocator m_ItemAllocator;
- ItemType *m_pFront;
- ItemType *m_pBack;
+ ItemType* m_pFront;
+ ItemType* m_pBack;
size_t m_Count;
};
-template
-VmaRawList::VmaRawList(const VkAllocationCallbacks *pAllocationCallbacks) :
- m_pAllocationCallbacks(pAllocationCallbacks),
- m_ItemAllocator(pAllocationCallbacks, 128),
- m_pFront(VMA_NULL),
- m_pBack(VMA_NULL),
- m_Count(0) {
+template
+VmaRawList::VmaRawList(const VkAllocationCallbacks* pAllocationCallbacks) :
+ m_pAllocationCallbacks(pAllocationCallbacks),
+ m_ItemAllocator(pAllocationCallbacks, 128),
+ m_pFront(VMA_NULL),
+ m_pBack(VMA_NULL),
+ m_Count(0)
+{
}
-template
-VmaRawList::~VmaRawList() {
+template
+VmaRawList::~VmaRawList()
+{
// Intentionally not calling Clear, because that would be unnecessary
// computations to return all items to m_ItemAllocator as free.
}
-template
-void VmaRawList::Clear() {
- if (IsEmpty() == false) {
- ItemType *pItem = m_pBack;
- while (pItem != VMA_NULL) {
- ItemType *const pPrevItem = pItem->pPrev;
+template
+void VmaRawList::Clear()
+{
+ if(IsEmpty() == false)
+ {
+ ItemType* pItem = m_pBack;
+ while(pItem != VMA_NULL)
+ {
+ ItemType* const pPrevItem = pItem->pPrev;
m_ItemAllocator.Free(pItem);
pItem = pPrevItem;
}
@@ -4360,16 +4953,20 @@ void VmaRawList::Clear() {
}
}
-template
-VmaListItem *VmaRawList::PushBack() {
- ItemType *const pNewItem = m_ItemAllocator.Alloc();
+template
+VmaListItem* VmaRawList::PushBack()
+{
+ ItemType* const pNewItem = m_ItemAllocator.Alloc();
pNewItem->pNext = VMA_NULL;
- if (IsEmpty()) {
+ if(IsEmpty())
+ {
pNewItem->pPrev = VMA_NULL;
m_pFront = pNewItem;
m_pBack = pNewItem;
m_Count = 1;
- } else {
+ }
+ else
+ {
pNewItem->pPrev = m_pBack;
m_pBack->pNext = pNewItem;
m_pBack = pNewItem;
@@ -4378,16 +4975,20 @@ VmaListItem *VmaRawList::PushBack() {
return pNewItem;
}
-template
-VmaListItem *VmaRawList::PushFront() {
- ItemType *const pNewItem = m_ItemAllocator.Alloc();
+template
+VmaListItem* VmaRawList::PushFront()
+{
+ ItemType* const pNewItem = m_ItemAllocator.Alloc();
pNewItem->pPrev = VMA_NULL;
- if (IsEmpty()) {
+ if(IsEmpty())
+ {
pNewItem->pNext = VMA_NULL;
m_pFront = pNewItem;
m_pBack = pNewItem;
m_Count = 1;
- } else {
+ }
+ else
+ {
pNewItem->pNext = m_pFront;
m_pFront->pPrev = pNewItem;
m_pFront = pNewItem;
@@ -4396,26 +4997,30 @@ VmaListItem *VmaRawList::PushFront() {
return pNewItem;
}
-template
-VmaListItem *VmaRawList::PushBack(const T &value) {
- ItemType *const pNewItem = PushBack();
+template
+VmaListItem* VmaRawList::PushBack(const T& value)
+{
+ ItemType* const pNewItem = PushBack();
pNewItem->Value = value;
return pNewItem;
}
-template
-VmaListItem *VmaRawList::PushFront(const T &value) {
- ItemType *const pNewItem = PushFront();
+template
+VmaListItem* VmaRawList::PushFront(const T& value)
+{
+ ItemType* const pNewItem = PushFront();
pNewItem->Value = value;
return pNewItem;
}
-template
-void VmaRawList::PopBack() {
+template
+void VmaRawList::PopBack()
+{
VMA_HEAVY_ASSERT(m_Count > 0);
- ItemType *const pBackItem = m_pBack;
- ItemType *const pPrevItem = pBackItem->pPrev;
- if (pPrevItem != VMA_NULL) {
+ ItemType* const pBackItem = m_pBack;
+ ItemType* const pPrevItem = pBackItem->pPrev;
+ if(pPrevItem != VMA_NULL)
+ {
pPrevItem->pNext = VMA_NULL;
}
m_pBack = pPrevItem;
@@ -4423,12 +5028,14 @@ void VmaRawList::PopBack() {
--m_Count;
}
-template
-void VmaRawList::PopFront() {
+template
+void VmaRawList::PopFront()
+{
VMA_HEAVY_ASSERT(m_Count > 0);
- ItemType *const pFrontItem = m_pFront;
- ItemType *const pNextItem = pFrontItem->pNext;
- if (pNextItem != VMA_NULL) {
+ ItemType* const pFrontItem = m_pFront;
+ ItemType* const pNextItem = pFrontItem->pNext;
+ if(pNextItem != VMA_NULL)
+ {
pNextItem->pPrev = VMA_NULL;
}
m_pFront = pNextItem;
@@ -4436,21 +5043,28 @@ void VmaRawList::PopFront() {
--m_Count;
}
-template
-void VmaRawList::Remove(ItemType *pItem) {
+template
+void VmaRawList::Remove(ItemType* pItem)
+{
VMA_HEAVY_ASSERT(pItem != VMA_NULL);
VMA_HEAVY_ASSERT(m_Count > 0);
- if (pItem->pPrev != VMA_NULL) {
+ if(pItem->pPrev != VMA_NULL)
+ {
pItem->pPrev->pNext = pItem->pNext;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(m_pFront == pItem);
m_pFront = pItem->pNext;
}
- if (pItem->pNext != VMA_NULL) {
+ if(pItem->pNext != VMA_NULL)
+ {
pItem->pNext->pPrev = pItem->pPrev;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(m_pBack == pItem);
m_pBack = pItem->pPrev;
}
@@ -4459,197 +5073,240 @@ void VmaRawList::Remove(ItemType *pItem) {
--m_Count;
}
-template
-VmaListItem *VmaRawList::InsertBefore(ItemType *pItem) {
- if (pItem != VMA_NULL) {
- ItemType *const prevItem = pItem->pPrev;
- ItemType *const newItem = m_ItemAllocator.Alloc();
+template
+VmaListItem* VmaRawList::InsertBefore(ItemType* pItem)
+{
+ if(pItem != VMA_NULL)
+ {
+ ItemType* const prevItem = pItem->pPrev;
+ ItemType* const newItem = m_ItemAllocator.Alloc();
newItem->pPrev = prevItem;
newItem->pNext = pItem;
pItem->pPrev = newItem;
- if (prevItem != VMA_NULL) {
+ if(prevItem != VMA_NULL)
+ {
prevItem->pNext = newItem;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(m_pFront == pItem);
m_pFront = newItem;
}
++m_Count;
return newItem;
- } else
+ }
+ else
return PushBack();
}
-template
-VmaListItem *VmaRawList::InsertAfter(ItemType *pItem) {
- if (pItem != VMA_NULL) {
- ItemType *const nextItem = pItem->pNext;
- ItemType *const newItem = m_ItemAllocator.Alloc();
+template
+VmaListItem* VmaRawList::InsertAfter(ItemType* pItem)
+{
+ if(pItem != VMA_NULL)
+ {
+ ItemType* const nextItem = pItem->pNext;
+ ItemType* const newItem = m_ItemAllocator.Alloc();
newItem->pNext = nextItem;
newItem->pPrev = pItem;
pItem->pNext = newItem;
- if (nextItem != VMA_NULL) {
+ if(nextItem != VMA_NULL)
+ {
nextItem->pPrev = newItem;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(m_pBack == pItem);
m_pBack = newItem;
}
++m_Count;
return newItem;
- } else
+ }
+ else
return PushFront();
}
-template
-VmaListItem *VmaRawList::InsertBefore(ItemType *pItem, const T &value) {
- ItemType *const newItem = InsertBefore(pItem);
+template
+VmaListItem* VmaRawList::InsertBefore(ItemType* pItem, const T& value)
+{
+ ItemType* const newItem = InsertBefore(pItem);
newItem->Value = value;
return newItem;
}
-template
-VmaListItem *VmaRawList::InsertAfter(ItemType *pItem, const T &value) {
- ItemType *const newItem = InsertAfter(pItem);
+template
+VmaListItem* VmaRawList::InsertAfter(ItemType* pItem, const T& value)
+{
+ ItemType* const newItem = InsertAfter(pItem);
newItem->Value = value;
return newItem;
}
-template
-class VmaList {
+template
+class VmaList
+{
VMA_CLASS_NO_COPY(VmaList)
public:
- class iterator {
+ class iterator
+ {
public:
iterator() :
- m_pList(VMA_NULL),
- m_pItem(VMA_NULL) {
+ m_pList(VMA_NULL),
+ m_pItem(VMA_NULL)
+ {
}
- T &operator*() const {
+ T& operator*() const
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
return m_pItem->Value;
}
- T *operator->() const {
+ T* operator->() const
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
return &m_pItem->Value;
}
- iterator &operator++() {
+ iterator& operator++()
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
m_pItem = m_pItem->pNext;
return *this;
}
- iterator &operator--() {
- if (m_pItem != VMA_NULL) {
+ iterator& operator--()
+ {
+ if(m_pItem != VMA_NULL)
+ {
m_pItem = m_pItem->pPrev;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(!m_pList->IsEmpty());
m_pItem = m_pList->Back();
}
return *this;
}
- iterator operator++(int) {
+ iterator operator++(int)
+ {
iterator result = *this;
++*this;
return result;
}
- iterator operator--(int) {
+ iterator operator--(int)
+ {
iterator result = *this;
--*this;
return result;
}
- bool operator==(const iterator &rhs) const {
+ bool operator==(const iterator& rhs) const
+ {
VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);
return m_pItem == rhs.m_pItem;
}
- bool operator!=(const iterator &rhs) const {
+ bool operator!=(const iterator& rhs) const
+ {
VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);
return m_pItem != rhs.m_pItem;
}
-
+
private:
- VmaRawList *m_pList;
- VmaListItem *m_pItem;
+ VmaRawList* m_pList;
+ VmaListItem* m_pItem;
- iterator(VmaRawList *pList, VmaListItem *pItem) :
- m_pList(pList),
- m_pItem(pItem) {
+ iterator(VmaRawList* pList, VmaListItem* pItem) :
+ m_pList(pList),
+ m_pItem(pItem)
+ {
}
friend class VmaList;
};
- class const_iterator {
+ class const_iterator
+ {
public:
const_iterator() :
- m_pList(VMA_NULL),
- m_pItem(VMA_NULL) {
+ m_pList(VMA_NULL),
+ m_pItem(VMA_NULL)
+ {
}
- const_iterator(const iterator &src) :
- m_pList(src.m_pList),
- m_pItem(src.m_pItem) {
+ const_iterator(const iterator& src) :
+ m_pList(src.m_pList),
+ m_pItem(src.m_pItem)
+ {
}
-
- const T &operator*() const {
+
+ const T& operator*() const
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
return m_pItem->Value;
}
- const T *operator->() const {
+ const T* operator->() const
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
return &m_pItem->Value;
}
- const_iterator &operator++() {
+ const_iterator& operator++()
+ {
VMA_HEAVY_ASSERT(m_pItem != VMA_NULL);
m_pItem = m_pItem->pNext;
return *this;
}
- const_iterator &operator--() {
- if (m_pItem != VMA_NULL) {
+ const_iterator& operator--()
+ {
+ if(m_pItem != VMA_NULL)
+ {
m_pItem = m_pItem->pPrev;
- } else {
+ }
+ else
+ {
VMA_HEAVY_ASSERT(!m_pList->IsEmpty());
m_pItem = m_pList->Back();
}
return *this;
}
- const_iterator operator++(int) {
+ const_iterator operator++(int)
+ {
const_iterator result = *this;
++*this;
return result;
}
- const_iterator operator--(int) {
+ const_iterator operator--(int)
+ {
const_iterator result = *this;
--*this;
return result;
}
- bool operator==(const const_iterator &rhs) const {
+ bool operator==(const const_iterator& rhs) const
+ {
VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);
return m_pItem == rhs.m_pItem;
}
- bool operator!=(const const_iterator &rhs) const {
+ bool operator!=(const const_iterator& rhs) const
+ {
VMA_HEAVY_ASSERT(m_pList == rhs.m_pList);
return m_pItem != rhs.m_pItem;
}
-
+
private:
- const_iterator(const VmaRawList *pList, const VmaListItem *pItem) :
- m_pList(pList),
- m_pItem(pItem) {
+ const_iterator(const VmaRawList* pList, const VmaListItem* pItem) :
+ m_pList(pList),
+ m_pItem(pItem)
+ {
}
- const VmaRawList *m_pList;
- const VmaListItem *m_pItem;
+ const VmaRawList