Adam Sawicki | ae5c466 | 2019-01-02 10:23:35 +0100 | [diff] [blame] | 1 | //
|
| 2 | // Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved.
|
| 3 | //
|
| 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 5 | // of this software and associated documentation files (the "Software"), to deal
|
| 6 | // in the Software without restriction, including without limitation the rights
|
| 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 8 | // copies of the Software, and to permit persons to whom the Software is
|
| 9 | // furnished to do so, subject to the following conditions:
|
| 10 | //
|
| 11 | // The above copyright notice and this permission notice shall be included in
|
| 12 | // all copies or substantial portions of the Software.
|
| 13 | //
|
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 20 | // THE SOFTWARE.
|
| 21 | //
|
| 22 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 23 | #include "Tests.h"
|
| 24 | #include "VmaUsage.h"
|
| 25 | #include "Common.h"
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 26 | #include <atomic>
|
| 27 | #include <thread>
|
| 28 | #include <mutex>
|
Adam Sawicki | 94ce3d7 | 2019-04-17 14:59:25 +0200 | [diff] [blame] | 29 | #include <functional>
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 30 |
|
| 31 | #ifdef _WIN32
|
| 32 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 33 | static const char* CODE_DESCRIPTION = "Foo";
|
| 34 |
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 35 | extern VkCommandBuffer g_hTemporaryCommandBuffer;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 36 | extern const VkAllocationCallbacks* g_Allocs;
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 37 | void BeginSingleTimeCommands();
|
| 38 | void EndSingleTimeCommands();
|
| 39 |
|
Adam Sawicki | bdb89a9 | 2018-12-13 11:56:30 +0100 | [diff] [blame] | 40 | #ifndef VMA_DEBUG_MARGIN
|
| 41 | #define VMA_DEBUG_MARGIN 0
|
| 42 | #endif
|
| 43 |
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 44 | enum CONFIG_TYPE {
|
| 45 | CONFIG_TYPE_MINIMUM,
|
| 46 | CONFIG_TYPE_SMALL,
|
| 47 | CONFIG_TYPE_AVERAGE,
|
| 48 | CONFIG_TYPE_LARGE,
|
| 49 | CONFIG_TYPE_MAXIMUM,
|
| 50 | CONFIG_TYPE_COUNT
|
| 51 | };
|
| 52 |
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 53 | static constexpr CONFIG_TYPE ConfigType = CONFIG_TYPE_SMALL;
|
| 54 | //static constexpr CONFIG_TYPE ConfigType = CONFIG_TYPE_LARGE;
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 55 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 56 | enum class FREE_ORDER { FORWARD, BACKWARD, RANDOM, COUNT };
|
| 57 |
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 58 | static const char* FREE_ORDER_NAMES[] = {
|
| 59 | "FORWARD",
|
| 60 | "BACKWARD",
|
| 61 | "RANDOM",
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 62 | };
|
| 63 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 64 | // Copy of internal VmaAlgorithmToStr.
|
| 65 | static const char* AlgorithmToStr(uint32_t algorithm)
|
| 66 | {
|
| 67 | switch(algorithm)
|
| 68 | {
|
| 69 | case VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT:
|
| 70 | return "Linear";
|
| 71 | case VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT:
|
| 72 | return "Buddy";
|
| 73 | case 0:
|
| 74 | return "Default";
|
| 75 | default:
|
| 76 | assert(0);
|
| 77 | return "";
|
| 78 | }
|
| 79 | }
|
| 80 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 81 | struct AllocationSize
|
| 82 | {
|
| 83 | uint32_t Probability;
|
| 84 | VkDeviceSize BufferSizeMin, BufferSizeMax;
|
| 85 | uint32_t ImageSizeMin, ImageSizeMax;
|
| 86 | };
|
| 87 |
|
| 88 | struct Config
|
| 89 | {
|
| 90 | uint32_t RandSeed;
|
| 91 | VkDeviceSize BeginBytesToAllocate;
|
| 92 | uint32_t AdditionalOperationCount;
|
| 93 | VkDeviceSize MaxBytesToAllocate;
|
| 94 | uint32_t MemUsageProbability[4]; // For VMA_MEMORY_USAGE_*
|
| 95 | std::vector<AllocationSize> AllocationSizes;
|
| 96 | uint32_t ThreadCount;
|
| 97 | uint32_t ThreadsUsingCommonAllocationsProbabilityPercent;
|
| 98 | FREE_ORDER FreeOrder;
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 99 | VmaAllocationCreateFlags AllocationStrategy; // For VMA_ALLOCATION_CREATE_STRATEGY_*
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 100 | };
|
| 101 |
|
| 102 | struct Result
|
| 103 | {
|
| 104 | duration TotalTime;
|
| 105 | duration AllocationTimeMin, AllocationTimeAvg, AllocationTimeMax;
|
| 106 | duration DeallocationTimeMin, DeallocationTimeAvg, DeallocationTimeMax;
|
| 107 | VkDeviceSize TotalMemoryAllocated;
|
| 108 | VkDeviceSize FreeRangeSizeAvg, FreeRangeSizeMax;
|
| 109 | };
|
| 110 |
|
| 111 | void TestDefragmentationSimple();
|
| 112 | void TestDefragmentationFull();
|
| 113 |
|
| 114 | struct PoolTestConfig
|
| 115 | {
|
| 116 | uint32_t RandSeed;
|
| 117 | uint32_t ThreadCount;
|
| 118 | VkDeviceSize PoolSize;
|
| 119 | uint32_t FrameCount;
|
| 120 | uint32_t TotalItemCount;
|
| 121 | // Range for number of items used in each frame.
|
| 122 | uint32_t UsedItemCountMin, UsedItemCountMax;
|
| 123 | // Percent of items to make unused, and possibly make some others used in each frame.
|
| 124 | uint32_t ItemsToMakeUnusedPercent;
|
| 125 | std::vector<AllocationSize> AllocationSizes;
|
| 126 |
|
| 127 | VkDeviceSize CalcAvgResourceSize() const
|
| 128 | {
|
| 129 | uint32_t probabilitySum = 0;
|
| 130 | VkDeviceSize sizeSum = 0;
|
| 131 | for(size_t i = 0; i < AllocationSizes.size(); ++i)
|
| 132 | {
|
| 133 | const AllocationSize& allocSize = AllocationSizes[i];
|
| 134 | if(allocSize.BufferSizeMax > 0)
|
| 135 | sizeSum += (allocSize.BufferSizeMin + allocSize.BufferSizeMax) / 2 * allocSize.Probability;
|
| 136 | else
|
| 137 | {
|
| 138 | const VkDeviceSize avgDimension = (allocSize.ImageSizeMin + allocSize.ImageSizeMax) / 2;
|
| 139 | sizeSum += avgDimension * avgDimension * 4 * allocSize.Probability;
|
| 140 | }
|
| 141 | probabilitySum += allocSize.Probability;
|
| 142 | }
|
| 143 | return sizeSum / probabilitySum;
|
| 144 | }
|
| 145 |
|
| 146 | bool UsesBuffers() const
|
| 147 | {
|
| 148 | for(size_t i = 0; i < AllocationSizes.size(); ++i)
|
| 149 | if(AllocationSizes[i].BufferSizeMax > 0)
|
| 150 | return true;
|
| 151 | return false;
|
| 152 | }
|
| 153 |
|
| 154 | bool UsesImages() const
|
| 155 | {
|
| 156 | for(size_t i = 0; i < AllocationSizes.size(); ++i)
|
| 157 | if(AllocationSizes[i].ImageSizeMax > 0)
|
| 158 | return true;
|
| 159 | return false;
|
| 160 | }
|
| 161 | };
|
| 162 |
|
| 163 | struct PoolTestResult
|
| 164 | {
|
| 165 | duration TotalTime;
|
| 166 | duration AllocationTimeMin, AllocationTimeAvg, AllocationTimeMax;
|
| 167 | duration DeallocationTimeMin, DeallocationTimeAvg, DeallocationTimeMax;
|
| 168 | size_t LostAllocationCount, LostAllocationTotalSize;
|
| 169 | size_t FailedAllocationCount, FailedAllocationTotalSize;
|
| 170 | };
|
| 171 |
|
| 172 | static const uint32_t IMAGE_BYTES_PER_PIXEL = 1;
|
| 173 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 174 | uint32_t g_FrameIndex = 0;
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 175 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 176 | struct BufferInfo
|
| 177 | {
|
| 178 | VkBuffer Buffer = VK_NULL_HANDLE;
|
| 179 | VmaAllocation Allocation = VK_NULL_HANDLE;
|
| 180 | };
|
| 181 |
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 182 | static uint32_t MemoryTypeToHeap(uint32_t memoryTypeIndex)
|
| 183 | {
|
| 184 | const VkPhysicalDeviceMemoryProperties* props;
|
| 185 | vmaGetMemoryProperties(g_hAllocator, &props);
|
| 186 | return props->memoryTypes[memoryTypeIndex].heapIndex;
|
| 187 | }
|
| 188 |
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 189 | static uint32_t GetAllocationStrategyCount()
|
| 190 | {
|
| 191 | uint32_t strategyCount = 0;
|
| 192 | switch(ConfigType)
|
| 193 | {
|
| 194 | case CONFIG_TYPE_MINIMUM: strategyCount = 1; break;
|
| 195 | case CONFIG_TYPE_SMALL: strategyCount = 1; break;
|
| 196 | case CONFIG_TYPE_AVERAGE: strategyCount = 2; break;
|
| 197 | case CONFIG_TYPE_LARGE: strategyCount = 2; break;
|
| 198 | case CONFIG_TYPE_MAXIMUM: strategyCount = 3; break;
|
| 199 | default: assert(0);
|
| 200 | }
|
| 201 | return strategyCount;
|
| 202 | }
|
| 203 |
|
| 204 | static const char* GetAllocationStrategyName(VmaAllocationCreateFlags allocStrategy)
|
| 205 | {
|
| 206 | switch(allocStrategy)
|
| 207 | {
|
| 208 | case VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT: return "BEST_FIT"; break;
|
| 209 | case VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT: return "WORST_FIT"; break;
|
| 210 | case VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT: return "FIRST_FIT"; break;
|
| 211 | case 0: return "Default"; break;
|
| 212 | default: assert(0); return "";
|
| 213 | }
|
| 214 | }
|
| 215 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 216 | static void InitResult(Result& outResult)
|
| 217 | {
|
| 218 | outResult.TotalTime = duration::zero();
|
| 219 | outResult.AllocationTimeMin = duration::max();
|
| 220 | outResult.AllocationTimeAvg = duration::zero();
|
| 221 | outResult.AllocationTimeMax = duration::min();
|
| 222 | outResult.DeallocationTimeMin = duration::max();
|
| 223 | outResult.DeallocationTimeAvg = duration::zero();
|
| 224 | outResult.DeallocationTimeMax = duration::min();
|
| 225 | outResult.TotalMemoryAllocated = 0;
|
| 226 | outResult.FreeRangeSizeAvg = 0;
|
| 227 | outResult.FreeRangeSizeMax = 0;
|
| 228 | }
|
| 229 |
|
| 230 | class TimeRegisterObj
|
| 231 | {
|
| 232 | public:
|
| 233 | TimeRegisterObj(duration& min, duration& sum, duration& max) :
|
| 234 | m_Min(min),
|
| 235 | m_Sum(sum),
|
| 236 | m_Max(max),
|
| 237 | m_TimeBeg(std::chrono::high_resolution_clock::now())
|
| 238 | {
|
| 239 | }
|
| 240 |
|
| 241 | ~TimeRegisterObj()
|
| 242 | {
|
| 243 | duration d = std::chrono::high_resolution_clock::now() - m_TimeBeg;
|
| 244 | m_Sum += d;
|
| 245 | if(d < m_Min) m_Min = d;
|
| 246 | if(d > m_Max) m_Max = d;
|
| 247 | }
|
| 248 |
|
| 249 | private:
|
| 250 | duration& m_Min;
|
| 251 | duration& m_Sum;
|
| 252 | duration& m_Max;
|
| 253 | time_point m_TimeBeg;
|
| 254 | };
|
| 255 |
|
| 256 | struct PoolTestThreadResult
|
| 257 | {
|
| 258 | duration AllocationTimeMin, AllocationTimeSum, AllocationTimeMax;
|
| 259 | duration DeallocationTimeMin, DeallocationTimeSum, DeallocationTimeMax;
|
| 260 | size_t AllocationCount, DeallocationCount;
|
| 261 | size_t LostAllocationCount, LostAllocationTotalSize;
|
| 262 | size_t FailedAllocationCount, FailedAllocationTotalSize;
|
| 263 | };
|
| 264 |
|
| 265 | class AllocationTimeRegisterObj : public TimeRegisterObj
|
| 266 | {
|
| 267 | public:
|
| 268 | AllocationTimeRegisterObj(Result& result) :
|
| 269 | TimeRegisterObj(result.AllocationTimeMin, result.AllocationTimeAvg, result.AllocationTimeMax)
|
| 270 | {
|
| 271 | }
|
| 272 | };
|
| 273 |
|
| 274 | class DeallocationTimeRegisterObj : public TimeRegisterObj
|
| 275 | {
|
| 276 | public:
|
| 277 | DeallocationTimeRegisterObj(Result& result) :
|
| 278 | TimeRegisterObj(result.DeallocationTimeMin, result.DeallocationTimeAvg, result.DeallocationTimeMax)
|
| 279 | {
|
| 280 | }
|
| 281 | };
|
| 282 |
|
| 283 | class PoolAllocationTimeRegisterObj : public TimeRegisterObj
|
| 284 | {
|
| 285 | public:
|
| 286 | PoolAllocationTimeRegisterObj(PoolTestThreadResult& result) :
|
| 287 | TimeRegisterObj(result.AllocationTimeMin, result.AllocationTimeSum, result.AllocationTimeMax)
|
| 288 | {
|
| 289 | }
|
| 290 | };
|
| 291 |
|
| 292 | class PoolDeallocationTimeRegisterObj : public TimeRegisterObj
|
| 293 | {
|
| 294 | public:
|
| 295 | PoolDeallocationTimeRegisterObj(PoolTestThreadResult& result) :
|
| 296 | TimeRegisterObj(result.DeallocationTimeMin, result.DeallocationTimeSum, result.DeallocationTimeMax)
|
| 297 | {
|
| 298 | }
|
| 299 | };
|
| 300 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 301 | static void CurrentTimeToStr(std::string& out)
|
| 302 | {
|
| 303 | time_t rawTime; time(&rawTime);
|
| 304 | struct tm timeInfo; localtime_s(&timeInfo, &rawTime);
|
| 305 | char timeStr[128];
|
| 306 | strftime(timeStr, _countof(timeStr), "%c", &timeInfo);
|
| 307 | out = timeStr;
|
| 308 | }
|
| 309 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 310 | VkResult MainTest(Result& outResult, const Config& config)
|
| 311 | {
|
| 312 | assert(config.ThreadCount > 0);
|
| 313 |
|
| 314 | InitResult(outResult);
|
| 315 |
|
| 316 | RandomNumberGenerator mainRand{config.RandSeed};
|
| 317 |
|
| 318 | time_point timeBeg = std::chrono::high_resolution_clock::now();
|
| 319 |
|
| 320 | std::atomic<size_t> allocationCount = 0;
|
| 321 | VkResult res = VK_SUCCESS;
|
| 322 |
|
| 323 | uint32_t memUsageProbabilitySum =
|
| 324 | config.MemUsageProbability[0] + config.MemUsageProbability[1] +
|
| 325 | config.MemUsageProbability[2] + config.MemUsageProbability[3];
|
| 326 | assert(memUsageProbabilitySum > 0);
|
| 327 |
|
| 328 | uint32_t allocationSizeProbabilitySum = std::accumulate(
|
| 329 | config.AllocationSizes.begin(),
|
| 330 | config.AllocationSizes.end(),
|
| 331 | 0u,
|
| 332 | [](uint32_t sum, const AllocationSize& allocSize) {
|
| 333 | return sum + allocSize.Probability;
|
| 334 | });
|
| 335 |
|
| 336 | struct Allocation
|
| 337 | {
|
| 338 | VkBuffer Buffer;
|
| 339 | VkImage Image;
|
| 340 | VmaAllocation Alloc;
|
| 341 | };
|
| 342 |
|
| 343 | std::vector<Allocation> commonAllocations;
|
| 344 | std::mutex commonAllocationsMutex;
|
| 345 |
|
| 346 | auto Allocate = [&](
|
| 347 | VkDeviceSize bufferSize,
|
| 348 | const VkExtent2D imageExtent,
|
| 349 | RandomNumberGenerator& localRand,
|
| 350 | VkDeviceSize& totalAllocatedBytes,
|
| 351 | std::vector<Allocation>& allocations) -> VkResult
|
| 352 | {
|
| 353 | assert((bufferSize == 0) != (imageExtent.width == 0 && imageExtent.height == 0));
|
| 354 |
|
| 355 | uint32_t memUsageIndex = 0;
|
| 356 | uint32_t memUsageRand = localRand.Generate() % memUsageProbabilitySum;
|
| 357 | while(memUsageRand >= config.MemUsageProbability[memUsageIndex])
|
| 358 | memUsageRand -= config.MemUsageProbability[memUsageIndex++];
|
| 359 |
|
| 360 | VmaAllocationCreateInfo memReq = {};
|
| 361 | memReq.usage = (VmaMemoryUsage)(VMA_MEMORY_USAGE_GPU_ONLY + memUsageIndex);
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 362 | memReq.flags |= config.AllocationStrategy;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 363 |
|
| 364 | Allocation allocation = {};
|
| 365 | VmaAllocationInfo allocationInfo;
|
| 366 |
|
| 367 | // Buffer
|
| 368 | if(bufferSize > 0)
|
| 369 | {
|
| 370 | assert(imageExtent.width == 0);
|
| 371 | VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 372 | bufferInfo.size = bufferSize;
|
| 373 | bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 374 |
|
| 375 | {
|
| 376 | AllocationTimeRegisterObj timeRegisterObj{outResult};
|
| 377 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &memReq, &allocation.Buffer, &allocation.Alloc, &allocationInfo);
|
| 378 | }
|
| 379 | }
|
| 380 | // Image
|
| 381 | else
|
| 382 | {
|
| 383 | VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 384 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 385 | imageInfo.extent.width = imageExtent.width;
|
| 386 | imageInfo.extent.height = imageExtent.height;
|
| 387 | imageInfo.extent.depth = 1;
|
| 388 | imageInfo.mipLevels = 1;
|
| 389 | imageInfo.arrayLayers = 1;
|
| 390 | imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 391 | imageInfo.tiling = memReq.usage == VMA_MEMORY_USAGE_GPU_ONLY ?
|
| 392 | VK_IMAGE_TILING_OPTIMAL :
|
| 393 | VK_IMAGE_TILING_LINEAR;
|
| 394 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 395 | switch(memReq.usage)
|
| 396 | {
|
| 397 | case VMA_MEMORY_USAGE_GPU_ONLY:
|
| 398 | switch(localRand.Generate() % 3)
|
| 399 | {
|
| 400 | case 0:
|
| 401 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
| 402 | break;
|
| 403 | case 1:
|
| 404 | imageInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
| 405 | break;
|
| 406 | case 2:
|
| 407 | imageInfo.usage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
| 408 | break;
|
| 409 | }
|
| 410 | break;
|
| 411 | case VMA_MEMORY_USAGE_CPU_ONLY:
|
| 412 | case VMA_MEMORY_USAGE_CPU_TO_GPU:
|
| 413 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
| 414 | break;
|
| 415 | case VMA_MEMORY_USAGE_GPU_TO_CPU:
|
| 416 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
| 417 | break;
|
| 418 | }
|
| 419 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 420 | imageInfo.flags = 0;
|
| 421 |
|
| 422 | {
|
| 423 | AllocationTimeRegisterObj timeRegisterObj{outResult};
|
| 424 | res = vmaCreateImage(g_hAllocator, &imageInfo, &memReq, &allocation.Image, &allocation.Alloc, &allocationInfo);
|
| 425 | }
|
| 426 | }
|
| 427 |
|
| 428 | if(res == VK_SUCCESS)
|
| 429 | {
|
| 430 | ++allocationCount;
|
| 431 | totalAllocatedBytes += allocationInfo.size;
|
| 432 | bool useCommonAllocations = localRand.Generate() % 100 < config.ThreadsUsingCommonAllocationsProbabilityPercent;
|
| 433 | if(useCommonAllocations)
|
| 434 | {
|
| 435 | std::unique_lock<std::mutex> lock(commonAllocationsMutex);
|
| 436 | commonAllocations.push_back(allocation);
|
| 437 | }
|
| 438 | else
|
| 439 | allocations.push_back(allocation);
|
| 440 | }
|
| 441 | else
|
| 442 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 443 | TEST(0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 444 | }
|
| 445 | return res;
|
| 446 | };
|
| 447 |
|
| 448 | auto GetNextAllocationSize = [&](
|
| 449 | VkDeviceSize& outBufSize,
|
| 450 | VkExtent2D& outImageSize,
|
| 451 | RandomNumberGenerator& localRand)
|
| 452 | {
|
| 453 | outBufSize = 0;
|
| 454 | outImageSize = {0, 0};
|
| 455 |
|
| 456 | uint32_t allocSizeIndex = 0;
|
| 457 | uint32_t r = localRand.Generate() % allocationSizeProbabilitySum;
|
| 458 | while(r >= config.AllocationSizes[allocSizeIndex].Probability)
|
| 459 | r -= config.AllocationSizes[allocSizeIndex++].Probability;
|
| 460 |
|
| 461 | const AllocationSize& allocSize = config.AllocationSizes[allocSizeIndex];
|
| 462 | if(allocSize.BufferSizeMax > 0)
|
| 463 | {
|
| 464 | assert(allocSize.ImageSizeMax == 0);
|
| 465 | if(allocSize.BufferSizeMax == allocSize.BufferSizeMin)
|
| 466 | outBufSize = allocSize.BufferSizeMin;
|
| 467 | else
|
| 468 | {
|
| 469 | outBufSize = allocSize.BufferSizeMin + localRand.Generate() % (allocSize.BufferSizeMax - allocSize.BufferSizeMin);
|
| 470 | outBufSize = outBufSize / 16 * 16;
|
| 471 | }
|
| 472 | }
|
| 473 | else
|
| 474 | {
|
| 475 | if(allocSize.ImageSizeMax == allocSize.ImageSizeMin)
|
| 476 | outImageSize.width = outImageSize.height = allocSize.ImageSizeMax;
|
| 477 | else
|
| 478 | {
|
| 479 | outImageSize.width = allocSize.ImageSizeMin + localRand.Generate() % (allocSize.ImageSizeMax - allocSize.ImageSizeMin);
|
| 480 | outImageSize.height = allocSize.ImageSizeMin + localRand.Generate() % (allocSize.ImageSizeMax - allocSize.ImageSizeMin);
|
| 481 | }
|
| 482 | }
|
| 483 | };
|
| 484 |
|
| 485 | std::atomic<uint32_t> numThreadsReachedMaxAllocations = 0;
|
| 486 | HANDLE threadsFinishEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
|
| 487 |
|
| 488 | auto ThreadProc = [&](uint32_t randSeed) -> void
|
| 489 | {
|
| 490 | RandomNumberGenerator threadRand(randSeed);
|
| 491 | VkDeviceSize threadTotalAllocatedBytes = 0;
|
| 492 | std::vector<Allocation> threadAllocations;
|
| 493 | VkDeviceSize threadBeginBytesToAllocate = config.BeginBytesToAllocate / config.ThreadCount;
|
| 494 | VkDeviceSize threadMaxBytesToAllocate = config.MaxBytesToAllocate / config.ThreadCount;
|
| 495 | uint32_t threadAdditionalOperationCount = config.AdditionalOperationCount / config.ThreadCount;
|
| 496 |
|
| 497 | // BEGIN ALLOCATIONS
|
| 498 | for(;;)
|
| 499 | {
|
| 500 | VkDeviceSize bufferSize = 0;
|
| 501 | VkExtent2D imageExtent = {};
|
| 502 | GetNextAllocationSize(bufferSize, imageExtent, threadRand);
|
| 503 | if(threadTotalAllocatedBytes + bufferSize + imageExtent.width * imageExtent.height * IMAGE_BYTES_PER_PIXEL <
|
| 504 | threadBeginBytesToAllocate)
|
| 505 | {
|
| 506 | if(Allocate(bufferSize, imageExtent, threadRand, threadTotalAllocatedBytes, threadAllocations) != VK_SUCCESS)
|
| 507 | break;
|
| 508 | }
|
| 509 | else
|
| 510 | break;
|
| 511 | }
|
| 512 |
|
| 513 | // ADDITIONAL ALLOCATIONS AND FREES
|
| 514 | for(size_t i = 0; i < threadAdditionalOperationCount; ++i)
|
| 515 | {
|
| 516 | VkDeviceSize bufferSize = 0;
|
| 517 | VkExtent2D imageExtent = {};
|
| 518 | GetNextAllocationSize(bufferSize, imageExtent, threadRand);
|
| 519 |
|
| 520 | // true = allocate, false = free
|
| 521 | bool allocate = threadRand.Generate() % 2 != 0;
|
| 522 |
|
| 523 | if(allocate)
|
| 524 | {
|
| 525 | if(threadTotalAllocatedBytes +
|
| 526 | bufferSize +
|
| 527 | imageExtent.width * imageExtent.height * IMAGE_BYTES_PER_PIXEL <
|
| 528 | threadMaxBytesToAllocate)
|
| 529 | {
|
| 530 | if(Allocate(bufferSize, imageExtent, threadRand, threadTotalAllocatedBytes, threadAllocations) != VK_SUCCESS)
|
| 531 | break;
|
| 532 | }
|
| 533 | }
|
| 534 | else
|
| 535 | {
|
| 536 | bool useCommonAllocations = threadRand.Generate() % 100 < config.ThreadsUsingCommonAllocationsProbabilityPercent;
|
| 537 | if(useCommonAllocations)
|
| 538 | {
|
| 539 | std::unique_lock<std::mutex> lock(commonAllocationsMutex);
|
| 540 | if(!commonAllocations.empty())
|
| 541 | {
|
| 542 | size_t indexToFree = threadRand.Generate() % commonAllocations.size();
|
| 543 | VmaAllocationInfo allocationInfo;
|
| 544 | vmaGetAllocationInfo(g_hAllocator, commonAllocations[indexToFree].Alloc, &allocationInfo);
|
| 545 | if(threadTotalAllocatedBytes >= allocationInfo.size)
|
| 546 | {
|
| 547 | DeallocationTimeRegisterObj timeRegisterObj{outResult};
|
| 548 | if(commonAllocations[indexToFree].Buffer != VK_NULL_HANDLE)
|
| 549 | vmaDestroyBuffer(g_hAllocator, commonAllocations[indexToFree].Buffer, commonAllocations[indexToFree].Alloc);
|
| 550 | else
|
| 551 | vmaDestroyImage(g_hAllocator, commonAllocations[indexToFree].Image, commonAllocations[indexToFree].Alloc);
|
| 552 | threadTotalAllocatedBytes -= allocationInfo.size;
|
| 553 | commonAllocations.erase(commonAllocations.begin() + indexToFree);
|
| 554 | }
|
| 555 | }
|
| 556 | }
|
| 557 | else
|
| 558 | {
|
| 559 | if(!threadAllocations.empty())
|
| 560 | {
|
| 561 | size_t indexToFree = threadRand.Generate() % threadAllocations.size();
|
| 562 | VmaAllocationInfo allocationInfo;
|
| 563 | vmaGetAllocationInfo(g_hAllocator, threadAllocations[indexToFree].Alloc, &allocationInfo);
|
| 564 | if(threadTotalAllocatedBytes >= allocationInfo.size)
|
| 565 | {
|
| 566 | DeallocationTimeRegisterObj timeRegisterObj{outResult};
|
| 567 | if(threadAllocations[indexToFree].Buffer != VK_NULL_HANDLE)
|
| 568 | vmaDestroyBuffer(g_hAllocator, threadAllocations[indexToFree].Buffer, threadAllocations[indexToFree].Alloc);
|
| 569 | else
|
| 570 | vmaDestroyImage(g_hAllocator, threadAllocations[indexToFree].Image, threadAllocations[indexToFree].Alloc);
|
| 571 | threadTotalAllocatedBytes -= allocationInfo.size;
|
| 572 | threadAllocations.erase(threadAllocations.begin() + indexToFree);
|
| 573 | }
|
| 574 | }
|
| 575 | }
|
| 576 | }
|
| 577 | }
|
| 578 |
|
| 579 | ++numThreadsReachedMaxAllocations;
|
| 580 |
|
| 581 | WaitForSingleObject(threadsFinishEvent, INFINITE);
|
| 582 |
|
| 583 | // DEALLOCATION
|
| 584 | while(!threadAllocations.empty())
|
| 585 | {
|
| 586 | size_t indexToFree = 0;
|
| 587 | switch(config.FreeOrder)
|
| 588 | {
|
| 589 | case FREE_ORDER::FORWARD:
|
| 590 | indexToFree = 0;
|
| 591 | break;
|
| 592 | case FREE_ORDER::BACKWARD:
|
| 593 | indexToFree = threadAllocations.size() - 1;
|
| 594 | break;
|
| 595 | case FREE_ORDER::RANDOM:
|
| 596 | indexToFree = mainRand.Generate() % threadAllocations.size();
|
| 597 | break;
|
| 598 | }
|
| 599 |
|
| 600 | {
|
| 601 | DeallocationTimeRegisterObj timeRegisterObj{outResult};
|
| 602 | if(threadAllocations[indexToFree].Buffer != VK_NULL_HANDLE)
|
| 603 | vmaDestroyBuffer(g_hAllocator, threadAllocations[indexToFree].Buffer, threadAllocations[indexToFree].Alloc);
|
| 604 | else
|
| 605 | vmaDestroyImage(g_hAllocator, threadAllocations[indexToFree].Image, threadAllocations[indexToFree].Alloc);
|
| 606 | }
|
| 607 | threadAllocations.erase(threadAllocations.begin() + indexToFree);
|
| 608 | }
|
| 609 | };
|
| 610 |
|
| 611 | uint32_t threadRandSeed = mainRand.Generate();
|
| 612 | std::vector<std::thread> bkgThreads;
|
| 613 | for(size_t i = 0; i < config.ThreadCount; ++i)
|
| 614 | {
|
| 615 | bkgThreads.emplace_back(std::bind(ThreadProc, threadRandSeed + (uint32_t)i));
|
| 616 | }
|
| 617 |
|
| 618 | // Wait for threads reached max allocations
|
| 619 | while(numThreadsReachedMaxAllocations < config.ThreadCount)
|
| 620 | Sleep(0);
|
| 621 |
|
| 622 | // CALCULATE MEMORY STATISTICS ON FINAL USAGE
|
| 623 | VmaStats vmaStats = {};
|
| 624 | vmaCalculateStats(g_hAllocator, &vmaStats);
|
| 625 | outResult.TotalMemoryAllocated = vmaStats.total.usedBytes + vmaStats.total.unusedBytes;
|
| 626 | outResult.FreeRangeSizeMax = vmaStats.total.unusedRangeSizeMax;
|
| 627 | outResult.FreeRangeSizeAvg = vmaStats.total.unusedRangeSizeAvg;
|
| 628 |
|
| 629 | // Signal threads to deallocate
|
| 630 | SetEvent(threadsFinishEvent);
|
| 631 |
|
| 632 | // Wait for threads finished
|
| 633 | for(size_t i = 0; i < bkgThreads.size(); ++i)
|
| 634 | bkgThreads[i].join();
|
| 635 | bkgThreads.clear();
|
| 636 |
|
| 637 | CloseHandle(threadsFinishEvent);
|
| 638 |
|
| 639 | // Deallocate remaining common resources
|
| 640 | while(!commonAllocations.empty())
|
| 641 | {
|
| 642 | size_t indexToFree = 0;
|
| 643 | switch(config.FreeOrder)
|
| 644 | {
|
| 645 | case FREE_ORDER::FORWARD:
|
| 646 | indexToFree = 0;
|
| 647 | break;
|
| 648 | case FREE_ORDER::BACKWARD:
|
| 649 | indexToFree = commonAllocations.size() - 1;
|
| 650 | break;
|
| 651 | case FREE_ORDER::RANDOM:
|
| 652 | indexToFree = mainRand.Generate() % commonAllocations.size();
|
| 653 | break;
|
| 654 | }
|
| 655 |
|
| 656 | {
|
| 657 | DeallocationTimeRegisterObj timeRegisterObj{outResult};
|
| 658 | if(commonAllocations[indexToFree].Buffer != VK_NULL_HANDLE)
|
| 659 | vmaDestroyBuffer(g_hAllocator, commonAllocations[indexToFree].Buffer, commonAllocations[indexToFree].Alloc);
|
| 660 | else
|
| 661 | vmaDestroyImage(g_hAllocator, commonAllocations[indexToFree].Image, commonAllocations[indexToFree].Alloc);
|
| 662 | }
|
| 663 | commonAllocations.erase(commonAllocations.begin() + indexToFree);
|
| 664 | }
|
| 665 |
|
| 666 | if(allocationCount)
|
| 667 | {
|
| 668 | outResult.AllocationTimeAvg /= allocationCount;
|
| 669 | outResult.DeallocationTimeAvg /= allocationCount;
|
| 670 | }
|
| 671 |
|
| 672 | outResult.TotalTime = std::chrono::high_resolution_clock::now() - timeBeg;
|
| 673 |
|
| 674 | return res;
|
| 675 | }
|
| 676 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 677 | void SaveAllocatorStatsToFile(const wchar_t* filePath)
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 678 | {
|
Adam Sawicki | 4d844e2 | 2019-01-24 16:21:05 +0100 | [diff] [blame] | 679 | wprintf(L"Saving JSON dump to file \"%s\"\n", filePath);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 680 | char* stats;
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 681 | vmaBuildStatsString(g_hAllocator, &stats, VK_TRUE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 682 | SaveFile(filePath, stats, strlen(stats));
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 683 | vmaFreeStatsString(g_hAllocator, stats);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 684 | }
|
| 685 |
|
| 686 | struct AllocInfo
|
| 687 | {
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 688 | VmaAllocation m_Allocation = VK_NULL_HANDLE;
|
| 689 | VkBuffer m_Buffer = VK_NULL_HANDLE;
|
| 690 | VkImage m_Image = VK_NULL_HANDLE;
|
| 691 | uint32_t m_StartValue = 0;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 692 | union
|
| 693 | {
|
| 694 | VkBufferCreateInfo m_BufferInfo;
|
| 695 | VkImageCreateInfo m_ImageInfo;
|
| 696 | };
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 697 |
|
| 698 | void CreateBuffer(
|
| 699 | const VkBufferCreateInfo& bufCreateInfo,
|
| 700 | const VmaAllocationCreateInfo& allocCreateInfo);
|
| 701 | void Destroy();
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 702 | };
|
| 703 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 704 | void AllocInfo::CreateBuffer(
|
| 705 | const VkBufferCreateInfo& bufCreateInfo,
|
| 706 | const VmaAllocationCreateInfo& allocCreateInfo)
|
| 707 | {
|
| 708 | m_BufferInfo = bufCreateInfo;
|
| 709 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &m_Buffer, &m_Allocation, nullptr);
|
| 710 | TEST(res == VK_SUCCESS);
|
| 711 | }
|
| 712 |
|
| 713 | void AllocInfo::Destroy()
|
| 714 | {
|
| 715 | if(m_Image)
|
| 716 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 717 | vkDestroyImage(g_hDevice, m_Image, g_Allocs);
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 718 | }
|
| 719 | if(m_Buffer)
|
| 720 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 721 | vkDestroyBuffer(g_hDevice, m_Buffer, g_Allocs);
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 722 | }
|
| 723 | if(m_Allocation)
|
| 724 | {
|
| 725 | vmaFreeMemory(g_hAllocator, m_Allocation);
|
| 726 | }
|
| 727 | }
|
| 728 |
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 729 | class StagingBufferCollection
|
| 730 | {
|
| 731 | public:
|
| 732 | StagingBufferCollection() { }
|
| 733 | ~StagingBufferCollection();
|
| 734 | // Returns false if maximum total size of buffers would be exceeded.
|
| 735 | bool AcquireBuffer(VkDeviceSize size, VkBuffer& outBuffer, void*& outMappedPtr);
|
| 736 | void ReleaseAllBuffers();
|
| 737 |
|
| 738 | private:
|
| 739 | static const VkDeviceSize MAX_TOTAL_SIZE = 256ull * 1024 * 1024;
|
| 740 | struct BufInfo
|
| 741 | {
|
| 742 | VmaAllocation Allocation = VK_NULL_HANDLE;
|
| 743 | VkBuffer Buffer = VK_NULL_HANDLE;
|
| 744 | VkDeviceSize Size = VK_WHOLE_SIZE;
|
| 745 | void* MappedPtr = nullptr;
|
| 746 | bool Used = false;
|
| 747 | };
|
| 748 | std::vector<BufInfo> m_Bufs;
|
| 749 | // Including both used and unused.
|
| 750 | VkDeviceSize m_TotalSize = 0;
|
| 751 | };
|
| 752 |
|
| 753 | StagingBufferCollection::~StagingBufferCollection()
|
| 754 | {
|
| 755 | for(size_t i = m_Bufs.size(); i--; )
|
| 756 | {
|
| 757 | vmaDestroyBuffer(g_hAllocator, m_Bufs[i].Buffer, m_Bufs[i].Allocation);
|
| 758 | }
|
| 759 | }
|
| 760 |
|
| 761 | bool StagingBufferCollection::AcquireBuffer(VkDeviceSize size, VkBuffer& outBuffer, void*& outMappedPtr)
|
| 762 | {
|
| 763 | assert(size <= MAX_TOTAL_SIZE);
|
| 764 |
|
| 765 | // Try to find existing unused buffer with best size.
|
| 766 | size_t bestIndex = SIZE_MAX;
|
| 767 | for(size_t i = 0, count = m_Bufs.size(); i < count; ++i)
|
| 768 | {
|
| 769 | BufInfo& currBufInfo = m_Bufs[i];
|
| 770 | if(!currBufInfo.Used && currBufInfo.Size >= size &&
|
| 771 | (bestIndex == SIZE_MAX || currBufInfo.Size < m_Bufs[bestIndex].Size))
|
| 772 | {
|
| 773 | bestIndex = i;
|
| 774 | }
|
| 775 | }
|
| 776 |
|
| 777 | if(bestIndex != SIZE_MAX)
|
| 778 | {
|
| 779 | m_Bufs[bestIndex].Used = true;
|
| 780 | outBuffer = m_Bufs[bestIndex].Buffer;
|
| 781 | outMappedPtr = m_Bufs[bestIndex].MappedPtr;
|
| 782 | return true;
|
| 783 | }
|
| 784 |
|
| 785 | // Allocate new buffer with requested size.
|
| 786 | if(m_TotalSize + size <= MAX_TOTAL_SIZE)
|
| 787 | {
|
| 788 | BufInfo bufInfo;
|
| 789 | bufInfo.Size = size;
|
| 790 | bufInfo.Used = true;
|
| 791 |
|
| 792 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 793 | bufCreateInfo.size = size;
|
| 794 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 795 |
|
| 796 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 797 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 798 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 799 |
|
| 800 | VmaAllocationInfo allocInfo;
|
| 801 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &bufInfo.Buffer, &bufInfo.Allocation, &allocInfo);
|
| 802 | bufInfo.MappedPtr = allocInfo.pMappedData;
|
| 803 | TEST(res == VK_SUCCESS && bufInfo.MappedPtr);
|
| 804 |
|
| 805 | outBuffer = bufInfo.Buffer;
|
| 806 | outMappedPtr = bufInfo.MappedPtr;
|
| 807 |
|
| 808 | m_Bufs.push_back(std::move(bufInfo));
|
| 809 |
|
| 810 | m_TotalSize += size;
|
| 811 |
|
| 812 | return true;
|
| 813 | }
|
| 814 |
|
| 815 | // There are some unused but smaller buffers: Free them and try again.
|
| 816 | bool hasUnused = false;
|
| 817 | for(size_t i = 0, count = m_Bufs.size(); i < count; ++i)
|
| 818 | {
|
| 819 | if(!m_Bufs[i].Used)
|
| 820 | {
|
| 821 | hasUnused = true;
|
| 822 | break;
|
| 823 | }
|
| 824 | }
|
| 825 | if(hasUnused)
|
| 826 | {
|
| 827 | for(size_t i = m_Bufs.size(); i--; )
|
| 828 | {
|
| 829 | if(!m_Bufs[i].Used)
|
| 830 | {
|
| 831 | m_TotalSize -= m_Bufs[i].Size;
|
| 832 | vmaDestroyBuffer(g_hAllocator, m_Bufs[i].Buffer, m_Bufs[i].Allocation);
|
| 833 | m_Bufs.erase(m_Bufs.begin() + i);
|
| 834 | }
|
| 835 | }
|
| 836 |
|
| 837 | return AcquireBuffer(size, outBuffer, outMappedPtr);
|
| 838 | }
|
| 839 |
|
| 840 | return false;
|
| 841 | }
|
| 842 |
|
| 843 | void StagingBufferCollection::ReleaseAllBuffers()
|
| 844 | {
|
| 845 | for(size_t i = 0, count = m_Bufs.size(); i < count; ++i)
|
| 846 | {
|
| 847 | m_Bufs[i].Used = false;
|
| 848 | }
|
| 849 | }
|
| 850 |
|
| 851 | static void UploadGpuData(const AllocInfo* allocInfo, size_t allocInfoCount)
|
| 852 | {
|
| 853 | StagingBufferCollection stagingBufs;
|
| 854 |
|
| 855 | bool cmdBufferStarted = false;
|
| 856 | for(size_t allocInfoIndex = 0; allocInfoIndex < allocInfoCount; ++allocInfoIndex)
|
| 857 | {
|
| 858 | const AllocInfo& currAllocInfo = allocInfo[allocInfoIndex];
|
| 859 | if(currAllocInfo.m_Buffer)
|
| 860 | {
|
| 861 | const VkDeviceSize size = currAllocInfo.m_BufferInfo.size;
|
| 862 |
|
| 863 | VkBuffer stagingBuf = VK_NULL_HANDLE;
|
| 864 | void* stagingBufMappedPtr = nullptr;
|
| 865 | if(!stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr))
|
| 866 | {
|
| 867 | TEST(cmdBufferStarted);
|
| 868 | EndSingleTimeCommands();
|
| 869 | stagingBufs.ReleaseAllBuffers();
|
| 870 | cmdBufferStarted = false;
|
| 871 |
|
| 872 | bool ok = stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr);
|
| 873 | TEST(ok);
|
| 874 | }
|
| 875 |
|
| 876 | // Fill staging buffer.
|
| 877 | {
|
| 878 | assert(size % sizeof(uint32_t) == 0);
|
| 879 | uint32_t* stagingValPtr = (uint32_t*)stagingBufMappedPtr;
|
| 880 | uint32_t val = currAllocInfo.m_StartValue;
|
| 881 | for(size_t i = 0; i < size / sizeof(uint32_t); ++i)
|
| 882 | {
|
| 883 | *stagingValPtr = val;
|
| 884 | ++stagingValPtr;
|
| 885 | ++val;
|
| 886 | }
|
| 887 | }
|
| 888 |
|
| 889 | // Issue copy command from staging buffer to destination buffer.
|
| 890 | if(!cmdBufferStarted)
|
| 891 | {
|
| 892 | cmdBufferStarted = true;
|
| 893 | BeginSingleTimeCommands();
|
| 894 | }
|
| 895 |
|
| 896 | VkBufferCopy copy = {};
|
| 897 | copy.srcOffset = 0;
|
| 898 | copy.dstOffset = 0;
|
| 899 | copy.size = size;
|
| 900 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, stagingBuf, currAllocInfo.m_Buffer, 1, ©);
|
| 901 | }
|
| 902 | else
|
| 903 | {
|
| 904 | TEST(0 && "Images not currently supported.");
|
| 905 | }
|
| 906 | }
|
| 907 |
|
| 908 | if(cmdBufferStarted)
|
| 909 | {
|
| 910 | EndSingleTimeCommands();
|
| 911 | stagingBufs.ReleaseAllBuffers();
|
| 912 | }
|
| 913 | }
|
| 914 |
|
| 915 | static void ValidateGpuData(const AllocInfo* allocInfo, size_t allocInfoCount)
|
| 916 | {
|
| 917 | StagingBufferCollection stagingBufs;
|
| 918 |
|
| 919 | bool cmdBufferStarted = false;
|
| 920 | size_t validateAllocIndexOffset = 0;
|
| 921 | std::vector<void*> validateStagingBuffers;
|
| 922 | for(size_t allocInfoIndex = 0; allocInfoIndex < allocInfoCount; ++allocInfoIndex)
|
| 923 | {
|
| 924 | const AllocInfo& currAllocInfo = allocInfo[allocInfoIndex];
|
| 925 | if(currAllocInfo.m_Buffer)
|
| 926 | {
|
| 927 | const VkDeviceSize size = currAllocInfo.m_BufferInfo.size;
|
| 928 |
|
| 929 | VkBuffer stagingBuf = VK_NULL_HANDLE;
|
| 930 | void* stagingBufMappedPtr = nullptr;
|
| 931 | if(!stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr))
|
| 932 | {
|
| 933 | TEST(cmdBufferStarted);
|
| 934 | EndSingleTimeCommands();
|
| 935 | cmdBufferStarted = false;
|
| 936 |
|
| 937 | for(size_t validateIndex = 0;
|
| 938 | validateIndex < validateStagingBuffers.size();
|
| 939 | ++validateIndex)
|
| 940 | {
|
| 941 | const size_t validateAllocIndex = validateIndex + validateAllocIndexOffset;
|
| 942 | const VkDeviceSize validateSize = allocInfo[validateAllocIndex].m_BufferInfo.size;
|
| 943 | TEST(validateSize % sizeof(uint32_t) == 0);
|
| 944 | const uint32_t* stagingValPtr = (const uint32_t*)validateStagingBuffers[validateIndex];
|
| 945 | uint32_t val = allocInfo[validateAllocIndex].m_StartValue;
|
| 946 | bool valid = true;
|
| 947 | for(size_t i = 0; i < validateSize / sizeof(uint32_t); ++i)
|
| 948 | {
|
| 949 | if(*stagingValPtr != val)
|
| 950 | {
|
| 951 | valid = false;
|
| 952 | break;
|
| 953 | }
|
| 954 | ++stagingValPtr;
|
| 955 | ++val;
|
| 956 | }
|
| 957 | TEST(valid);
|
| 958 | }
|
| 959 |
|
| 960 | stagingBufs.ReleaseAllBuffers();
|
| 961 |
|
| 962 | validateAllocIndexOffset = allocInfoIndex;
|
| 963 | validateStagingBuffers.clear();
|
| 964 |
|
| 965 | bool ok = stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr);
|
| 966 | TEST(ok);
|
| 967 | }
|
| 968 |
|
| 969 | // Issue copy command from staging buffer to destination buffer.
|
| 970 | if(!cmdBufferStarted)
|
| 971 | {
|
| 972 | cmdBufferStarted = true;
|
| 973 | BeginSingleTimeCommands();
|
| 974 | }
|
| 975 |
|
| 976 | VkBufferCopy copy = {};
|
| 977 | copy.srcOffset = 0;
|
| 978 | copy.dstOffset = 0;
|
| 979 | copy.size = size;
|
| 980 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, currAllocInfo.m_Buffer, stagingBuf, 1, ©);
|
| 981 |
|
| 982 | // Sava mapped pointer for later validation.
|
| 983 | validateStagingBuffers.push_back(stagingBufMappedPtr);
|
| 984 | }
|
| 985 | else
|
| 986 | {
|
| 987 | TEST(0 && "Images not currently supported.");
|
| 988 | }
|
| 989 | }
|
| 990 |
|
| 991 | if(cmdBufferStarted)
|
| 992 | {
|
| 993 | EndSingleTimeCommands();
|
| 994 |
|
| 995 | for(size_t validateIndex = 0;
|
| 996 | validateIndex < validateStagingBuffers.size();
|
| 997 | ++validateIndex)
|
| 998 | {
|
| 999 | const size_t validateAllocIndex = validateIndex + validateAllocIndexOffset;
|
| 1000 | const VkDeviceSize validateSize = allocInfo[validateAllocIndex].m_BufferInfo.size;
|
| 1001 | TEST(validateSize % sizeof(uint32_t) == 0);
|
| 1002 | const uint32_t* stagingValPtr = (const uint32_t*)validateStagingBuffers[validateIndex];
|
| 1003 | uint32_t val = allocInfo[validateAllocIndex].m_StartValue;
|
| 1004 | bool valid = true;
|
| 1005 | for(size_t i = 0; i < validateSize / sizeof(uint32_t); ++i)
|
| 1006 | {
|
| 1007 | if(*stagingValPtr != val)
|
| 1008 | {
|
| 1009 | valid = false;
|
| 1010 | break;
|
| 1011 | }
|
| 1012 | ++stagingValPtr;
|
| 1013 | ++val;
|
| 1014 | }
|
| 1015 | TEST(valid);
|
| 1016 | }
|
| 1017 |
|
| 1018 | stagingBufs.ReleaseAllBuffers();
|
| 1019 | }
|
| 1020 | }
|
| 1021 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1022 | static void GetMemReq(VmaAllocationCreateInfo& outMemReq)
|
| 1023 | {
|
| 1024 | outMemReq = {};
|
| 1025 | outMemReq.usage = VMA_MEMORY_USAGE_CPU_TO_GPU;
|
| 1026 | //outMemReq.flags = VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT;
|
| 1027 | }
|
| 1028 |
|
| 1029 | static void CreateBuffer(
|
| 1030 | VmaPool pool,
|
| 1031 | const VkBufferCreateInfo& bufCreateInfo,
|
| 1032 | bool persistentlyMapped,
|
| 1033 | AllocInfo& outAllocInfo)
|
| 1034 | {
|
| 1035 | outAllocInfo = {};
|
| 1036 | outAllocInfo.m_BufferInfo = bufCreateInfo;
|
| 1037 |
|
| 1038 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1039 | allocCreateInfo.pool = pool;
|
| 1040 | if(persistentlyMapped)
|
| 1041 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 1042 |
|
| 1043 | VmaAllocationInfo vmaAllocInfo = {};
|
| 1044 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &outAllocInfo.m_Buffer, &outAllocInfo.m_Allocation, &vmaAllocInfo) );
|
| 1045 |
|
| 1046 | // Setup StartValue and fill.
|
| 1047 | {
|
| 1048 | outAllocInfo.m_StartValue = (uint32_t)rand();
|
| 1049 | uint32_t* data = (uint32_t*)vmaAllocInfo.pMappedData;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1050 | TEST((data != nullptr) == persistentlyMapped);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1051 | if(!persistentlyMapped)
|
| 1052 | {
|
| 1053 | ERR_GUARD_VULKAN( vmaMapMemory(g_hAllocator, outAllocInfo.m_Allocation, (void**)&data) );
|
| 1054 | }
|
| 1055 |
|
| 1056 | uint32_t value = outAllocInfo.m_StartValue;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1057 | TEST(bufCreateInfo.size % 4 == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1058 | for(size_t i = 0; i < bufCreateInfo.size / sizeof(uint32_t); ++i)
|
| 1059 | data[i] = value++;
|
| 1060 |
|
| 1061 | if(!persistentlyMapped)
|
| 1062 | vmaUnmapMemory(g_hAllocator, outAllocInfo.m_Allocation);
|
| 1063 | }
|
| 1064 | }
|
| 1065 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1066 | static void CreateAllocation(AllocInfo& outAllocation)
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1067 | {
|
| 1068 | outAllocation.m_Allocation = nullptr;
|
| 1069 | outAllocation.m_Buffer = nullptr;
|
| 1070 | outAllocation.m_Image = nullptr;
|
| 1071 | outAllocation.m_StartValue = (uint32_t)rand();
|
| 1072 |
|
| 1073 | VmaAllocationCreateInfo vmaMemReq;
|
| 1074 | GetMemReq(vmaMemReq);
|
| 1075 |
|
| 1076 | VmaAllocationInfo allocInfo;
|
| 1077 |
|
| 1078 | const bool isBuffer = true;//(rand() & 0x1) != 0;
|
| 1079 | const bool isLarge = (rand() % 16) == 0;
|
| 1080 | if(isBuffer)
|
| 1081 | {
|
| 1082 | const uint32_t bufferSize = isLarge ?
|
| 1083 | (rand() % 10 + 1) * (1024 * 1024) : // 1 MB ... 10 MB
|
| 1084 | (rand() % 1024 + 1) * 1024; // 1 KB ... 1 MB
|
| 1085 |
|
| 1086 | VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1087 | bufferInfo.size = bufferSize;
|
| 1088 | bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1089 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1090 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &vmaMemReq, &outAllocation.m_Buffer, &outAllocation.m_Allocation, &allocInfo);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1091 | outAllocation.m_BufferInfo = bufferInfo;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1092 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1093 | }
|
| 1094 | else
|
| 1095 | {
|
| 1096 | const uint32_t imageSizeX = isLarge ?
|
| 1097 | 1024 + rand() % (4096 - 1024) : // 1024 ... 4096
|
| 1098 | rand() % 1024 + 1; // 1 ... 1024
|
| 1099 | const uint32_t imageSizeY = isLarge ?
|
| 1100 | 1024 + rand() % (4096 - 1024) : // 1024 ... 4096
|
| 1101 | rand() % 1024 + 1; // 1 ... 1024
|
| 1102 |
|
| 1103 | VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 1104 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 1105 | imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 1106 | imageInfo.extent.width = imageSizeX;
|
| 1107 | imageInfo.extent.height = imageSizeY;
|
| 1108 | imageInfo.extent.depth = 1;
|
| 1109 | imageInfo.mipLevels = 1;
|
| 1110 | imageInfo.arrayLayers = 1;
|
| 1111 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 1112 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 1113 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 1114 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
| 1115 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1116 | VkResult res = vmaCreateImage(g_hAllocator, &imageInfo, &vmaMemReq, &outAllocation.m_Image, &outAllocation.m_Allocation, &allocInfo);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1117 | outAllocation.m_ImageInfo = imageInfo;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1118 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1119 | }
|
| 1120 |
|
| 1121 | uint32_t* data = (uint32_t*)allocInfo.pMappedData;
|
| 1122 | if(allocInfo.pMappedData == nullptr)
|
| 1123 | {
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1124 | VkResult res = vmaMapMemory(g_hAllocator, outAllocation.m_Allocation, (void**)&data);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1125 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1126 | }
|
| 1127 |
|
| 1128 | uint32_t value = outAllocation.m_StartValue;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1129 | TEST(allocInfo.size % 4 == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1130 | for(size_t i = 0; i < allocInfo.size / sizeof(uint32_t); ++i)
|
| 1131 | data[i] = value++;
|
| 1132 |
|
| 1133 | if(allocInfo.pMappedData == nullptr)
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1134 | vmaUnmapMemory(g_hAllocator, outAllocation.m_Allocation);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1135 | }
|
| 1136 |
|
| 1137 | static void DestroyAllocation(const AllocInfo& allocation)
|
| 1138 | {
|
| 1139 | if(allocation.m_Buffer)
|
| 1140 | vmaDestroyBuffer(g_hAllocator, allocation.m_Buffer, allocation.m_Allocation);
|
| 1141 | else
|
| 1142 | vmaDestroyImage(g_hAllocator, allocation.m_Image, allocation.m_Allocation);
|
| 1143 | }
|
| 1144 |
|
| 1145 | static void DestroyAllAllocations(std::vector<AllocInfo>& allocations)
|
| 1146 | {
|
| 1147 | for(size_t i = allocations.size(); i--; )
|
| 1148 | DestroyAllocation(allocations[i]);
|
| 1149 | allocations.clear();
|
| 1150 | }
|
| 1151 |
|
| 1152 | static void ValidateAllocationData(const AllocInfo& allocation)
|
| 1153 | {
|
| 1154 | VmaAllocationInfo allocInfo;
|
| 1155 | vmaGetAllocationInfo(g_hAllocator, allocation.m_Allocation, &allocInfo);
|
| 1156 |
|
| 1157 | uint32_t* data = (uint32_t*)allocInfo.pMappedData;
|
| 1158 | if(allocInfo.pMappedData == nullptr)
|
| 1159 | {
|
| 1160 | VkResult res = vmaMapMemory(g_hAllocator, allocation.m_Allocation, (void**)&data);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1161 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1162 | }
|
| 1163 |
|
| 1164 | uint32_t value = allocation.m_StartValue;
|
| 1165 | bool ok = true;
|
| 1166 | size_t i;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1167 | TEST(allocInfo.size % 4 == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1168 | for(i = 0; i < allocInfo.size / sizeof(uint32_t); ++i)
|
| 1169 | {
|
| 1170 | if(data[i] != value++)
|
| 1171 | {
|
| 1172 | ok = false;
|
| 1173 | break;
|
| 1174 | }
|
| 1175 | }
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1176 | TEST(ok);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1177 |
|
| 1178 | if(allocInfo.pMappedData == nullptr)
|
| 1179 | vmaUnmapMemory(g_hAllocator, allocation.m_Allocation);
|
| 1180 | }
|
| 1181 |
|
| 1182 | static void RecreateAllocationResource(AllocInfo& allocation)
|
| 1183 | {
|
| 1184 | VmaAllocationInfo allocInfo;
|
| 1185 | vmaGetAllocationInfo(g_hAllocator, allocation.m_Allocation, &allocInfo);
|
| 1186 |
|
| 1187 | if(allocation.m_Buffer)
|
| 1188 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1189 | vkDestroyBuffer(g_hDevice, allocation.m_Buffer, g_Allocs);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1190 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1191 | VkResult res = vkCreateBuffer(g_hDevice, &allocation.m_BufferInfo, g_Allocs, &allocation.m_Buffer);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1192 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1193 |
|
| 1194 | // Just to silence validation layer warnings.
|
| 1195 | VkMemoryRequirements vkMemReq;
|
| 1196 | vkGetBufferMemoryRequirements(g_hDevice, allocation.m_Buffer, &vkMemReq);
|
Adam Sawicki | 2af57d7 | 2018-12-06 15:35:05 +0100 | [diff] [blame] | 1197 | TEST(vkMemReq.size >= allocation.m_BufferInfo.size);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1198 |
|
Adam Sawicki | af88c1b | 2019-07-02 12:34:26 +0200 | [diff] [blame] | 1199 | res = vmaBindBufferMemory(g_hAllocator, allocation.m_Allocation, allocation.m_Buffer);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1200 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1201 | }
|
| 1202 | else
|
| 1203 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1204 | vkDestroyImage(g_hDevice, allocation.m_Image, g_Allocs);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1205 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1206 | VkResult res = vkCreateImage(g_hDevice, &allocation.m_ImageInfo, g_Allocs, &allocation.m_Image);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1207 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1208 |
|
| 1209 | // Just to silence validation layer warnings.
|
| 1210 | VkMemoryRequirements vkMemReq;
|
| 1211 | vkGetImageMemoryRequirements(g_hDevice, allocation.m_Image, &vkMemReq);
|
| 1212 |
|
Adam Sawicki | af88c1b | 2019-07-02 12:34:26 +0200 | [diff] [blame] | 1213 | res = vmaBindImageMemory(g_hAllocator, allocation.m_Allocation, allocation.m_Image);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1214 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1215 | }
|
| 1216 | }
|
| 1217 |
|
| 1218 | static void Defragment(AllocInfo* allocs, size_t allocCount,
|
| 1219 | const VmaDefragmentationInfo* defragmentationInfo = nullptr,
|
| 1220 | VmaDefragmentationStats* defragmentationStats = nullptr)
|
| 1221 | {
|
| 1222 | std::vector<VmaAllocation> vmaAllocs(allocCount);
|
| 1223 | for(size_t i = 0; i < allocCount; ++i)
|
| 1224 | vmaAllocs[i] = allocs[i].m_Allocation;
|
| 1225 |
|
| 1226 | std::vector<VkBool32> allocChanged(allocCount);
|
| 1227 |
|
| 1228 | ERR_GUARD_VULKAN( vmaDefragment(g_hAllocator, vmaAllocs.data(), allocCount, allocChanged.data(),
|
| 1229 | defragmentationInfo, defragmentationStats) );
|
| 1230 |
|
| 1231 | for(size_t i = 0; i < allocCount; ++i)
|
| 1232 | {
|
| 1233 | if(allocChanged[i])
|
| 1234 | {
|
| 1235 | RecreateAllocationResource(allocs[i]);
|
| 1236 | }
|
| 1237 | }
|
| 1238 | }
|
| 1239 |
|
| 1240 | static void ValidateAllocationsData(const AllocInfo* allocs, size_t allocCount)
|
| 1241 | {
|
| 1242 | std::for_each(allocs, allocs + allocCount, [](const AllocInfo& allocInfo) {
|
| 1243 | ValidateAllocationData(allocInfo);
|
| 1244 | });
|
| 1245 | }
|
| 1246 |
|
| 1247 | void TestDefragmentationSimple()
|
| 1248 | {
|
| 1249 | wprintf(L"Test defragmentation simple\n");
|
| 1250 |
|
| 1251 | RandomNumberGenerator rand(667);
|
| 1252 |
|
| 1253 | const VkDeviceSize BUF_SIZE = 0x10000;
|
| 1254 | const VkDeviceSize BLOCK_SIZE = BUF_SIZE * 8;
|
| 1255 |
|
| 1256 | const VkDeviceSize MIN_BUF_SIZE = 32;
|
| 1257 | const VkDeviceSize MAX_BUF_SIZE = BUF_SIZE * 4;
|
| 1258 | auto RandomBufSize = [&]() -> VkDeviceSize {
|
| 1259 | return align_up<VkDeviceSize>(rand.Generate() % (MAX_BUF_SIZE - MIN_BUF_SIZE + 1) + MIN_BUF_SIZE, 32);
|
| 1260 | };
|
| 1261 |
|
| 1262 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1263 | bufCreateInfo.size = BUF_SIZE;
|
| 1264 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1265 |
|
| 1266 | VmaAllocationCreateInfo exampleAllocCreateInfo = {};
|
| 1267 | exampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1268 |
|
| 1269 | uint32_t memTypeIndex = UINT32_MAX;
|
| 1270 | vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &bufCreateInfo, &exampleAllocCreateInfo, &memTypeIndex);
|
| 1271 |
|
| 1272 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 1273 | poolCreateInfo.blockSize = BLOCK_SIZE;
|
| 1274 | poolCreateInfo.memoryTypeIndex = memTypeIndex;
|
| 1275 |
|
| 1276 | VmaPool pool;
|
| 1277 | ERR_GUARD_VULKAN( vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool) );
|
| 1278 |
|
Adam Sawicki | e168191 | 2018-11-23 17:50:12 +0100 | [diff] [blame] | 1279 | // Defragmentation of empty pool.
|
| 1280 | {
|
| 1281 | VmaDefragmentationInfo2 defragInfo = {};
|
| 1282 | defragInfo.maxCpuBytesToMove = VK_WHOLE_SIZE;
|
| 1283 | defragInfo.maxCpuAllocationsToMove = UINT32_MAX;
|
| 1284 | defragInfo.poolCount = 1;
|
| 1285 | defragInfo.pPools = &pool;
|
| 1286 |
|
| 1287 | VmaDefragmentationStats defragStats = {};
|
| 1288 | VmaDefragmentationContext defragCtx = nullptr;
|
| 1289 | VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &defragStats, &defragCtx);
|
| 1290 | TEST(res >= VK_SUCCESS);
|
| 1291 | vmaDefragmentationEnd(g_hAllocator, defragCtx);
|
| 1292 | TEST(defragStats.allocationsMoved == 0 && defragStats.bytesFreed == 0 &&
|
| 1293 | defragStats.bytesMoved == 0 && defragStats.deviceMemoryBlocksFreed == 0);
|
| 1294 | }
|
| 1295 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1296 | std::vector<AllocInfo> allocations;
|
| 1297 |
|
| 1298 | // persistentlyMappedOption = 0 - not persistently mapped.
|
| 1299 | // persistentlyMappedOption = 1 - persistently mapped.
|
| 1300 | for(uint32_t persistentlyMappedOption = 0; persistentlyMappedOption < 2; ++persistentlyMappedOption)
|
| 1301 | {
|
| 1302 | wprintf(L" Persistently mapped option = %u\n", persistentlyMappedOption);
|
| 1303 | const bool persistentlyMapped = persistentlyMappedOption != 0;
|
| 1304 |
|
| 1305 | // # Test 1
|
| 1306 | // Buffers of fixed size.
|
| 1307 | // Fill 2 blocks. Remove odd buffers. Defragment everything.
|
| 1308 | // Expected result: at least 1 block freed.
|
| 1309 | {
|
| 1310 | for(size_t i = 0; i < BLOCK_SIZE / BUF_SIZE * 2; ++i)
|
| 1311 | {
|
| 1312 | AllocInfo allocInfo;
|
| 1313 | CreateBuffer(pool, bufCreateInfo, persistentlyMapped, allocInfo);
|
| 1314 | allocations.push_back(allocInfo);
|
| 1315 | }
|
| 1316 |
|
| 1317 | for(size_t i = 1; i < allocations.size(); ++i)
|
| 1318 | {
|
| 1319 | DestroyAllocation(allocations[i]);
|
| 1320 | allocations.erase(allocations.begin() + i);
|
| 1321 | }
|
| 1322 |
|
| 1323 | VmaDefragmentationStats defragStats;
|
| 1324 | Defragment(allocations.data(), allocations.size(), nullptr, &defragStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1325 | TEST(defragStats.allocationsMoved > 0 && defragStats.bytesMoved > 0);
|
| 1326 | TEST(defragStats.deviceMemoryBlocksFreed >= 1);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1327 |
|
| 1328 | ValidateAllocationsData(allocations.data(), allocations.size());
|
| 1329 |
|
| 1330 | DestroyAllAllocations(allocations);
|
| 1331 | }
|
| 1332 |
|
| 1333 | // # Test 2
|
| 1334 | // Buffers of fixed size.
|
| 1335 | // Fill 2 blocks. Remove odd buffers. Defragment one buffer at time.
|
| 1336 | // Expected result: Each of 4 interations makes some progress.
|
| 1337 | {
|
| 1338 | for(size_t i = 0; i < BLOCK_SIZE / BUF_SIZE * 2; ++i)
|
| 1339 | {
|
| 1340 | AllocInfo allocInfo;
|
| 1341 | CreateBuffer(pool, bufCreateInfo, persistentlyMapped, allocInfo);
|
| 1342 | allocations.push_back(allocInfo);
|
| 1343 | }
|
| 1344 |
|
| 1345 | for(size_t i = 1; i < allocations.size(); ++i)
|
| 1346 | {
|
| 1347 | DestroyAllocation(allocations[i]);
|
| 1348 | allocations.erase(allocations.begin() + i);
|
| 1349 | }
|
| 1350 |
|
| 1351 | VmaDefragmentationInfo defragInfo = {};
|
| 1352 | defragInfo.maxAllocationsToMove = 1;
|
| 1353 | defragInfo.maxBytesToMove = BUF_SIZE;
|
| 1354 |
|
| 1355 | for(size_t i = 0; i < BLOCK_SIZE / BUF_SIZE / 2; ++i)
|
| 1356 | {
|
| 1357 | VmaDefragmentationStats defragStats;
|
| 1358 | Defragment(allocations.data(), allocations.size(), &defragInfo, &defragStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1359 | TEST(defragStats.allocationsMoved > 0 && defragStats.bytesMoved > 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1360 | }
|
| 1361 |
|
| 1362 | ValidateAllocationsData(allocations.data(), allocations.size());
|
| 1363 |
|
| 1364 | DestroyAllAllocations(allocations);
|
| 1365 | }
|
| 1366 |
|
| 1367 | // # Test 3
|
| 1368 | // Buffers of variable size.
|
| 1369 | // Create a number of buffers. Remove some percent of them.
|
| 1370 | // Defragment while having some percent of them unmovable.
|
| 1371 | // Expected result: Just simple validation.
|
| 1372 | {
|
| 1373 | for(size_t i = 0; i < 100; ++i)
|
| 1374 | {
|
| 1375 | VkBufferCreateInfo localBufCreateInfo = bufCreateInfo;
|
| 1376 | localBufCreateInfo.size = RandomBufSize();
|
| 1377 |
|
| 1378 | AllocInfo allocInfo;
|
| 1379 | CreateBuffer(pool, bufCreateInfo, persistentlyMapped, allocInfo);
|
| 1380 | allocations.push_back(allocInfo);
|
| 1381 | }
|
| 1382 |
|
| 1383 | const uint32_t percentToDelete = 60;
|
| 1384 | const size_t numberToDelete = allocations.size() * percentToDelete / 100;
|
| 1385 | for(size_t i = 0; i < numberToDelete; ++i)
|
| 1386 | {
|
| 1387 | size_t indexToDelete = rand.Generate() % (uint32_t)allocations.size();
|
| 1388 | DestroyAllocation(allocations[indexToDelete]);
|
| 1389 | allocations.erase(allocations.begin() + indexToDelete);
|
| 1390 | }
|
| 1391 |
|
| 1392 | // Non-movable allocations will be at the beginning of allocations array.
|
| 1393 | const uint32_t percentNonMovable = 20;
|
| 1394 | const size_t numberNonMovable = allocations.size() * percentNonMovable / 100;
|
| 1395 | for(size_t i = 0; i < numberNonMovable; ++i)
|
| 1396 | {
|
| 1397 | size_t indexNonMovable = i + rand.Generate() % (uint32_t)(allocations.size() - i);
|
| 1398 | if(indexNonMovable != i)
|
| 1399 | std::swap(allocations[i], allocations[indexNonMovable]);
|
| 1400 | }
|
| 1401 |
|
| 1402 | VmaDefragmentationStats defragStats;
|
| 1403 | Defragment(
|
| 1404 | allocations.data() + numberNonMovable,
|
| 1405 | allocations.size() - numberNonMovable,
|
| 1406 | nullptr, &defragStats);
|
| 1407 |
|
| 1408 | ValidateAllocationsData(allocations.data(), allocations.size());
|
| 1409 |
|
| 1410 | DestroyAllAllocations(allocations);
|
| 1411 | }
|
| 1412 | }
|
| 1413 |
|
Adam Sawicki | 647cf24 | 2018-11-23 17:58:00 +0100 | [diff] [blame] | 1414 | /*
|
| 1415 | Allocation that must be move to an overlapping place using memmove().
|
| 1416 | Create 2 buffers, second slightly bigger than the first. Delete first. Then defragment.
|
| 1417 | */
|
Adam Sawicki | bdb89a9 | 2018-12-13 11:56:30 +0100 | [diff] [blame] | 1418 | if(VMA_DEBUG_MARGIN == 0) // FAST algorithm works only when DEBUG_MARGIN disabled.
|
Adam Sawicki | 647cf24 | 2018-11-23 17:58:00 +0100 | [diff] [blame] | 1419 | {
|
| 1420 | AllocInfo allocInfo[2];
|
| 1421 |
|
| 1422 | bufCreateInfo.size = BUF_SIZE;
|
| 1423 | CreateBuffer(pool, bufCreateInfo, false, allocInfo[0]);
|
| 1424 | const VkDeviceSize biggerBufSize = BUF_SIZE + BUF_SIZE / 256;
|
| 1425 | bufCreateInfo.size = biggerBufSize;
|
| 1426 | CreateBuffer(pool, bufCreateInfo, false, allocInfo[1]);
|
| 1427 |
|
| 1428 | DestroyAllocation(allocInfo[0]);
|
| 1429 |
|
| 1430 | VmaDefragmentationStats defragStats;
|
| 1431 | Defragment(&allocInfo[1], 1, nullptr, &defragStats);
|
| 1432 | // If this fails, it means we couldn't do memmove with overlapping regions.
|
| 1433 | TEST(defragStats.allocationsMoved == 1 && defragStats.bytesMoved > 0);
|
| 1434 |
|
| 1435 | ValidateAllocationsData(&allocInfo[1], 1);
|
| 1436 | DestroyAllocation(allocInfo[1]);
|
| 1437 | }
|
| 1438 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1439 | vmaDestroyPool(g_hAllocator, pool);
|
| 1440 | }
|
| 1441 |
|
Adam Sawicki | 52076eb | 2018-11-22 16:14:50 +0100 | [diff] [blame] | 1442 | void TestDefragmentationWholePool()
|
| 1443 | {
|
| 1444 | wprintf(L"Test defragmentation whole pool\n");
|
| 1445 |
|
| 1446 | RandomNumberGenerator rand(668);
|
| 1447 |
|
| 1448 | const VkDeviceSize BUF_SIZE = 0x10000;
|
| 1449 | const VkDeviceSize BLOCK_SIZE = BUF_SIZE * 8;
|
| 1450 |
|
| 1451 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1452 | bufCreateInfo.size = BUF_SIZE;
|
| 1453 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1454 |
|
| 1455 | VmaAllocationCreateInfo exampleAllocCreateInfo = {};
|
| 1456 | exampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1457 |
|
| 1458 | uint32_t memTypeIndex = UINT32_MAX;
|
| 1459 | vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &bufCreateInfo, &exampleAllocCreateInfo, &memTypeIndex);
|
| 1460 |
|
| 1461 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 1462 | poolCreateInfo.blockSize = BLOCK_SIZE;
|
| 1463 | poolCreateInfo.memoryTypeIndex = memTypeIndex;
|
| 1464 |
|
| 1465 | VmaDefragmentationStats defragStats[2];
|
| 1466 | for(size_t caseIndex = 0; caseIndex < 2; ++caseIndex)
|
| 1467 | {
|
| 1468 | VmaPool pool;
|
| 1469 | ERR_GUARD_VULKAN( vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool) );
|
| 1470 |
|
| 1471 | std::vector<AllocInfo> allocations;
|
| 1472 |
|
| 1473 | // Buffers of fixed size.
|
| 1474 | // Fill 2 blocks. Remove odd buffers. Defragment all of them.
|
| 1475 | for(size_t i = 0; i < BLOCK_SIZE / BUF_SIZE * 2; ++i)
|
| 1476 | {
|
| 1477 | AllocInfo allocInfo;
|
| 1478 | CreateBuffer(pool, bufCreateInfo, false, allocInfo);
|
| 1479 | allocations.push_back(allocInfo);
|
| 1480 | }
|
| 1481 |
|
| 1482 | for(size_t i = 1; i < allocations.size(); ++i)
|
| 1483 | {
|
| 1484 | DestroyAllocation(allocations[i]);
|
| 1485 | allocations.erase(allocations.begin() + i);
|
| 1486 | }
|
| 1487 |
|
| 1488 | VmaDefragmentationInfo2 defragInfo = {};
|
| 1489 | defragInfo.maxCpuAllocationsToMove = UINT32_MAX;
|
| 1490 | defragInfo.maxCpuBytesToMove = VK_WHOLE_SIZE;
|
| 1491 | std::vector<VmaAllocation> allocationsToDefrag;
|
| 1492 | if(caseIndex == 0)
|
| 1493 | {
|
| 1494 | defragInfo.poolCount = 1;
|
| 1495 | defragInfo.pPools = &pool;
|
| 1496 | }
|
| 1497 | else
|
| 1498 | {
|
| 1499 | const size_t allocCount = allocations.size();
|
| 1500 | allocationsToDefrag.resize(allocCount);
|
| 1501 | std::transform(
|
| 1502 | allocations.begin(), allocations.end(),
|
| 1503 | allocationsToDefrag.begin(),
|
| 1504 | [](const AllocInfo& allocInfo) { return allocInfo.m_Allocation; });
|
| 1505 | defragInfo.allocationCount = (uint32_t)allocCount;
|
| 1506 | defragInfo.pAllocations = allocationsToDefrag.data();
|
| 1507 | }
|
| 1508 |
|
| 1509 | VmaDefragmentationContext defragCtx = VK_NULL_HANDLE;
|
| 1510 | VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &defragStats[caseIndex], &defragCtx);
|
| 1511 | TEST(res >= VK_SUCCESS);
|
| 1512 | vmaDefragmentationEnd(g_hAllocator, defragCtx);
|
| 1513 |
|
| 1514 | TEST(defragStats[caseIndex].allocationsMoved > 0 && defragStats[caseIndex].bytesMoved > 0);
|
| 1515 |
|
| 1516 | ValidateAllocationsData(allocations.data(), allocations.size());
|
| 1517 |
|
| 1518 | DestroyAllAllocations(allocations);
|
| 1519 |
|
| 1520 | vmaDestroyPool(g_hAllocator, pool);
|
| 1521 | }
|
| 1522 |
|
| 1523 | TEST(defragStats[0].bytesMoved == defragStats[1].bytesMoved);
|
| 1524 | TEST(defragStats[0].allocationsMoved == defragStats[1].allocationsMoved);
|
| 1525 | TEST(defragStats[0].bytesFreed == defragStats[1].bytesFreed);
|
| 1526 | TEST(defragStats[0].deviceMemoryBlocksFreed == defragStats[1].deviceMemoryBlocksFreed);
|
| 1527 | }
|
| 1528 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1529 | void TestDefragmentationFull()
|
| 1530 | {
|
| 1531 | std::vector<AllocInfo> allocations;
|
| 1532 |
|
| 1533 | // Create initial allocations.
|
| 1534 | for(size_t i = 0; i < 400; ++i)
|
| 1535 | {
|
| 1536 | AllocInfo allocation;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1537 | CreateAllocation(allocation);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1538 | allocations.push_back(allocation);
|
| 1539 | }
|
| 1540 |
|
| 1541 | // Delete random allocations
|
| 1542 | const size_t allocationsToDeletePercent = 80;
|
| 1543 | size_t allocationsToDelete = allocations.size() * allocationsToDeletePercent / 100;
|
| 1544 | for(size_t i = 0; i < allocationsToDelete; ++i)
|
| 1545 | {
|
| 1546 | size_t index = (size_t)rand() % allocations.size();
|
| 1547 | DestroyAllocation(allocations[index]);
|
| 1548 | allocations.erase(allocations.begin() + index);
|
| 1549 | }
|
| 1550 |
|
| 1551 | for(size_t i = 0; i < allocations.size(); ++i)
|
| 1552 | ValidateAllocationData(allocations[i]);
|
| 1553 |
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 1554 | //SaveAllocatorStatsToFile(L"Before.csv");
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1555 |
|
| 1556 | {
|
| 1557 | std::vector<VmaAllocation> vmaAllocations(allocations.size());
|
| 1558 | for(size_t i = 0; i < allocations.size(); ++i)
|
| 1559 | vmaAllocations[i] = allocations[i].m_Allocation;
|
| 1560 |
|
| 1561 | const size_t nonMovablePercent = 0;
|
| 1562 | size_t nonMovableCount = vmaAllocations.size() * nonMovablePercent / 100;
|
| 1563 | for(size_t i = 0; i < nonMovableCount; ++i)
|
| 1564 | {
|
| 1565 | size_t index = (size_t)rand() % vmaAllocations.size();
|
| 1566 | vmaAllocations.erase(vmaAllocations.begin() + index);
|
| 1567 | }
|
| 1568 |
|
| 1569 | const uint32_t defragCount = 1;
|
| 1570 | for(uint32_t defragIndex = 0; defragIndex < defragCount; ++defragIndex)
|
| 1571 | {
|
| 1572 | std::vector<VkBool32> allocationsChanged(vmaAllocations.size());
|
| 1573 |
|
| 1574 | VmaDefragmentationInfo defragmentationInfo;
|
| 1575 | defragmentationInfo.maxAllocationsToMove = UINT_MAX;
|
| 1576 | defragmentationInfo.maxBytesToMove = SIZE_MAX;
|
| 1577 |
|
| 1578 | wprintf(L"Defragmentation #%u\n", defragIndex);
|
| 1579 |
|
| 1580 | time_point begTime = std::chrono::high_resolution_clock::now();
|
| 1581 |
|
| 1582 | VmaDefragmentationStats stats;
|
| 1583 | VkResult res = vmaDefragment(g_hAllocator, vmaAllocations.data(), vmaAllocations.size(), allocationsChanged.data(), &defragmentationInfo, &stats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1584 | TEST(res >= 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1585 |
|
| 1586 | float defragmentDuration = ToFloatSeconds(std::chrono::high_resolution_clock::now() - begTime);
|
| 1587 |
|
| 1588 | wprintf(L"Moved allocations %u, bytes %llu\n", stats.allocationsMoved, stats.bytesMoved);
|
| 1589 | wprintf(L"Freed blocks %u, bytes %llu\n", stats.deviceMemoryBlocksFreed, stats.bytesFreed);
|
| 1590 | wprintf(L"Time: %.2f s\n", defragmentDuration);
|
| 1591 |
|
| 1592 | for(size_t i = 0; i < vmaAllocations.size(); ++i)
|
| 1593 | {
|
| 1594 | if(allocationsChanged[i])
|
| 1595 | {
|
| 1596 | RecreateAllocationResource(allocations[i]);
|
| 1597 | }
|
| 1598 | }
|
| 1599 |
|
| 1600 | for(size_t i = 0; i < allocations.size(); ++i)
|
| 1601 | ValidateAllocationData(allocations[i]);
|
| 1602 |
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 1603 | //wchar_t fileName[MAX_PATH];
|
| 1604 | //swprintf(fileName, MAX_PATH, L"After_%02u.csv", defragIndex);
|
| 1605 | //SaveAllocatorStatsToFile(fileName);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1606 | }
|
| 1607 | }
|
| 1608 |
|
| 1609 | // Destroy all remaining allocations.
|
| 1610 | DestroyAllAllocations(allocations);
|
| 1611 | }
|
| 1612 |
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 1613 | static void TestDefragmentationGpu()
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1614 | {
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 1615 | wprintf(L"Test defragmentation GPU\n");
|
Adam Sawicki | 0570400 | 2018-11-08 16:07:29 +0100 | [diff] [blame] | 1616 | g_MemoryAliasingWarningEnabled = false;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1617 |
|
| 1618 | std::vector<AllocInfo> allocations;
|
| 1619 |
|
| 1620 | // Create that many allocations to surely fill 3 new blocks of 256 MB.
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1621 | const VkDeviceSize bufSizeMin = 5ull * 1024 * 1024;
|
| 1622 | const VkDeviceSize bufSizeMax = 10ull * 1024 * 1024;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1623 | const VkDeviceSize totalSize = 3ull * 256 * 1024 * 1024;
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1624 | const size_t bufCount = (size_t)(totalSize / bufSizeMin);
|
| 1625 | const size_t percentToLeave = 30;
|
| 1626 | const size_t percentNonMovable = 3;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1627 | RandomNumberGenerator rand = { 234522 };
|
| 1628 |
|
| 1629 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1630 |
|
| 1631 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1632 | allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1633 | allocCreateInfo.flags = 0;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1634 |
|
| 1635 | // Create all intended buffers.
|
| 1636 | for(size_t i = 0; i < bufCount; ++i)
|
| 1637 | {
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1638 | bufCreateInfo.size = align_up(rand.Generate() % (bufSizeMax - bufSizeMin) + bufSizeMin, 32ull);
|
| 1639 |
|
| 1640 | if(rand.Generate() % 100 < percentNonMovable)
|
| 1641 | {
|
| 1642 | bufCreateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT |
|
| 1643 | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
|
| 1644 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1645 | allocCreateInfo.pUserData = (void*)(uintptr_t)2;
|
| 1646 | }
|
| 1647 | else
|
| 1648 | {
|
| 1649 | // Different usage just to see different color in output from VmaDumpVis.
|
| 1650 | bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT |
|
| 1651 | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
|
| 1652 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1653 | // And in JSON dump.
|
| 1654 | allocCreateInfo.pUserData = (void*)(uintptr_t)1;
|
| 1655 | }
|
| 1656 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1657 | AllocInfo alloc;
|
| 1658 | alloc.CreateBuffer(bufCreateInfo, allocCreateInfo);
|
| 1659 | alloc.m_StartValue = rand.Generate();
|
| 1660 | allocations.push_back(alloc);
|
| 1661 | }
|
| 1662 |
|
| 1663 | // Destroy some percentage of them.
|
| 1664 | {
|
| 1665 | const size_t buffersToDestroy = round_div<size_t>(bufCount * (100 - percentToLeave), 100);
|
| 1666 | for(size_t i = 0; i < buffersToDestroy; ++i)
|
| 1667 | {
|
| 1668 | const size_t index = rand.Generate() % allocations.size();
|
| 1669 | allocations[index].Destroy();
|
| 1670 | allocations.erase(allocations.begin() + index);
|
| 1671 | }
|
| 1672 | }
|
| 1673 |
|
| 1674 | // Fill them with meaningful data.
|
| 1675 | UploadGpuData(allocations.data(), allocations.size());
|
| 1676 |
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1677 | wchar_t fileName[MAX_PATH];
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 1678 | swprintf_s(fileName, L"GPU_defragmentation_A_before.json");
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1679 | SaveAllocatorStatsToFile(fileName);
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1680 |
|
| 1681 | // Defragment using GPU only.
|
| 1682 | {
|
| 1683 | const size_t allocCount = allocations.size();
|
Adam Sawicki | 440307e | 2018-10-18 15:05:19 +0200 | [diff] [blame] | 1684 |
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1685 | std::vector<VmaAllocation> allocationPtrs;
|
| 1686 | std::vector<VkBool32> allocationChanged;
|
| 1687 | std::vector<size_t> allocationOriginalIndex;
|
| 1688 |
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1689 | for(size_t i = 0; i < allocCount; ++i)
|
| 1690 | {
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1691 | VmaAllocationInfo allocInfo = {};
|
| 1692 | vmaGetAllocationInfo(g_hAllocator, allocations[i].m_Allocation, &allocInfo);
|
| 1693 | if((uintptr_t)allocInfo.pUserData == 1) // Movable
|
| 1694 | {
|
| 1695 | allocationPtrs.push_back(allocations[i].m_Allocation);
|
| 1696 | allocationChanged.push_back(VK_FALSE);
|
| 1697 | allocationOriginalIndex.push_back(i);
|
| 1698 | }
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1699 | }
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1700 |
|
| 1701 | const size_t movableAllocCount = allocationPtrs.size();
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1702 |
|
| 1703 | BeginSingleTimeCommands();
|
| 1704 |
|
| 1705 | VmaDefragmentationInfo2 defragInfo = {};
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 1706 | defragInfo.flags = 0;
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1707 | defragInfo.allocationCount = (uint32_t)movableAllocCount;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1708 | defragInfo.pAllocations = allocationPtrs.data();
|
Adam Sawicki | 440307e | 2018-10-18 15:05:19 +0200 | [diff] [blame] | 1709 | defragInfo.pAllocationsChanged = allocationChanged.data();
|
| 1710 | defragInfo.maxGpuBytesToMove = VK_WHOLE_SIZE;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1711 | defragInfo.maxGpuAllocationsToMove = UINT32_MAX;
|
| 1712 | defragInfo.commandBuffer = g_hTemporaryCommandBuffer;
|
| 1713 |
|
| 1714 | VmaDefragmentationStats stats = {};
|
| 1715 | VmaDefragmentationContext ctx = VK_NULL_HANDLE;
|
| 1716 | VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &stats, &ctx);
|
| 1717 | TEST(res >= VK_SUCCESS);
|
| 1718 |
|
| 1719 | EndSingleTimeCommands();
|
| 1720 |
|
| 1721 | vmaDefragmentationEnd(g_hAllocator, ctx);
|
| 1722 |
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1723 | for(size_t i = 0; i < movableAllocCount; ++i)
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1724 | {
|
| 1725 | if(allocationChanged[i])
|
| 1726 | {
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1727 | const size_t origAllocIndex = allocationOriginalIndex[i];
|
| 1728 | RecreateAllocationResource(allocations[origAllocIndex]);
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1729 | }
|
| 1730 | }
|
| 1731 |
|
Adam Sawicki | 4d844e2 | 2019-01-24 16:21:05 +0100 | [diff] [blame] | 1732 | // If corruption detection is enabled, GPU defragmentation may not work on
|
| 1733 | // memory types that have this detection active, e.g. on Intel.
|
Adam Sawicki | a1f727c | 2019-01-24 16:25:11 +0100 | [diff] [blame] | 1734 | #if !defined(VMA_DEBUG_DETECT_CORRUPTION) || VMA_DEBUG_DETECT_CORRUPTION == 0
|
Adam Sawicki | 4d844e2 | 2019-01-24 16:21:05 +0100 | [diff] [blame] | 1735 | TEST(stats.allocationsMoved > 0 && stats.bytesMoved > 0);
|
| 1736 | TEST(stats.deviceMemoryBlocksFreed > 0 && stats.bytesFreed > 0);
|
Adam Sawicki | a1f727c | 2019-01-24 16:25:11 +0100 | [diff] [blame] | 1737 | #endif
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1738 | }
|
| 1739 |
|
| 1740 | ValidateGpuData(allocations.data(), allocations.size());
|
| 1741 |
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 1742 | swprintf_s(fileName, L"GPU_defragmentation_B_after.json");
|
Adam Sawicki | c6ede15 | 2018-11-16 17:04:14 +0100 | [diff] [blame] | 1743 | SaveAllocatorStatsToFile(fileName);
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1744 |
|
| 1745 | // Destroy all remaining buffers.
|
| 1746 | for(size_t i = allocations.size(); i--; )
|
| 1747 | {
|
| 1748 | allocations[i].Destroy();
|
| 1749 | }
|
Adam Sawicki | 0570400 | 2018-11-08 16:07:29 +0100 | [diff] [blame] | 1750 |
|
| 1751 | g_MemoryAliasingWarningEnabled = true;
|
Adam Sawicki | ff0f7b8 | 2018-10-18 14:44:05 +0200 | [diff] [blame] | 1752 | }
|
| 1753 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1754 | static void TestUserData()
|
| 1755 | {
|
| 1756 | VkResult res;
|
| 1757 |
|
| 1758 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1759 | bufCreateInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
| 1760 | bufCreateInfo.size = 0x10000;
|
| 1761 |
|
| 1762 | for(uint32_t testIndex = 0; testIndex < 2; ++testIndex)
|
| 1763 | {
|
| 1764 | // Opaque pointer
|
| 1765 | {
|
| 1766 |
|
| 1767 | void* numberAsPointer = (void*)(size_t)0xC2501FF3u;
|
| 1768 | void* pointerToSomething = &res;
|
| 1769 |
|
| 1770 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1771 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1772 | allocCreateInfo.pUserData = numberAsPointer;
|
| 1773 | if(testIndex == 1)
|
| 1774 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 1775 |
|
| 1776 | VkBuffer buf; VmaAllocation alloc; VmaAllocationInfo allocInfo;
|
| 1777 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1778 | TEST(res == VK_SUCCESS);
|
| 1779 | TEST(allocInfo.pUserData = numberAsPointer);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1780 |
|
| 1781 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1782 | TEST(allocInfo.pUserData == numberAsPointer);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1783 |
|
| 1784 | vmaSetAllocationUserData(g_hAllocator, alloc, pointerToSomething);
|
| 1785 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1786 | TEST(allocInfo.pUserData == pointerToSomething);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1787 |
|
| 1788 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1789 | }
|
| 1790 |
|
| 1791 | // String
|
| 1792 | {
|
| 1793 | const char* name1 = "Buffer name \\\"\'<>&% \nSecond line .,;=";
|
| 1794 | const char* name2 = "2";
|
| 1795 | const size_t name1Len = strlen(name1);
|
| 1796 |
|
| 1797 | char* name1Buf = new char[name1Len + 1];
|
| 1798 | strcpy_s(name1Buf, name1Len + 1, name1);
|
| 1799 |
|
| 1800 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1801 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1802 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT;
|
| 1803 | allocCreateInfo.pUserData = name1Buf;
|
| 1804 | if(testIndex == 1)
|
| 1805 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 1806 |
|
| 1807 | VkBuffer buf; VmaAllocation alloc; VmaAllocationInfo allocInfo;
|
| 1808 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1809 | TEST(res == VK_SUCCESS);
|
| 1810 | TEST(allocInfo.pUserData != nullptr && allocInfo.pUserData != name1Buf);
|
| 1811 | TEST(strcmp(name1, (const char*)allocInfo.pUserData) == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1812 |
|
| 1813 | delete[] name1Buf;
|
| 1814 |
|
| 1815 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1816 | TEST(strcmp(name1, (const char*)allocInfo.pUserData) == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1817 |
|
| 1818 | vmaSetAllocationUserData(g_hAllocator, alloc, (void*)name2);
|
| 1819 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1820 | TEST(strcmp(name2, (const char*)allocInfo.pUserData) == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1821 |
|
| 1822 | vmaSetAllocationUserData(g_hAllocator, alloc, nullptr);
|
| 1823 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1824 | TEST(allocInfo.pUserData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1825 |
|
| 1826 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1827 | }
|
| 1828 | }
|
| 1829 | }
|
| 1830 |
|
Adam Sawicki | 370ab18 | 2018-11-08 16:31:00 +0100 | [diff] [blame] | 1831 | static void TestInvalidAllocations()
|
| 1832 | {
|
| 1833 | VkResult res;
|
| 1834 |
|
| 1835 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1836 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1837 |
|
| 1838 | // Try to allocate 0 bytes.
|
| 1839 | {
|
| 1840 | VkMemoryRequirements memReq = {};
|
| 1841 | memReq.size = 0; // !!!
|
| 1842 | memReq.alignment = 4;
|
| 1843 | memReq.memoryTypeBits = UINT32_MAX;
|
| 1844 | VmaAllocation alloc = VK_NULL_HANDLE;
|
| 1845 | res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo, &alloc, nullptr);
|
| 1846 | TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && alloc == VK_NULL_HANDLE);
|
| 1847 | }
|
| 1848 |
|
| 1849 | // Try to create buffer with size = 0.
|
| 1850 | {
|
| 1851 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1852 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1853 | bufCreateInfo.size = 0; // !!!
|
| 1854 | VkBuffer buf = VK_NULL_HANDLE;
|
| 1855 | VmaAllocation alloc = VK_NULL_HANDLE;
|
| 1856 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, nullptr);
|
| 1857 | TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && buf == VK_NULL_HANDLE && alloc == VK_NULL_HANDLE);
|
| 1858 | }
|
| 1859 |
|
| 1860 | // Try to create image with one dimension = 0.
|
| 1861 | {
|
| 1862 | VkImageCreateInfo imageCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1863 | imageCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 1864 | imageCreateInfo.format = VK_FORMAT_B8G8R8A8_UNORM;
|
| 1865 | imageCreateInfo.extent.width = 128;
|
| 1866 | imageCreateInfo.extent.height = 0; // !!!
|
| 1867 | imageCreateInfo.extent.depth = 1;
|
| 1868 | imageCreateInfo.mipLevels = 1;
|
| 1869 | imageCreateInfo.arrayLayers = 1;
|
| 1870 | imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 1871 | imageCreateInfo.tiling = VK_IMAGE_TILING_LINEAR;
|
| 1872 | imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
| 1873 | imageCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 1874 | VkImage image = VK_NULL_HANDLE;
|
| 1875 | VmaAllocation alloc = VK_NULL_HANDLE;
|
| 1876 | res = vmaCreateImage(g_hAllocator, &imageCreateInfo, &allocCreateInfo, &image, &alloc, nullptr);
|
| 1877 | TEST(res == VK_ERROR_VALIDATION_FAILED_EXT && image == VK_NULL_HANDLE && alloc == VK_NULL_HANDLE);
|
| 1878 | }
|
| 1879 | }
|
| 1880 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1881 | static void TestMemoryRequirements()
|
| 1882 | {
|
| 1883 | VkResult res;
|
| 1884 | VkBuffer buf;
|
| 1885 | VmaAllocation alloc;
|
| 1886 | VmaAllocationInfo allocInfo;
|
| 1887 |
|
| 1888 | const VkPhysicalDeviceMemoryProperties* memProps;
|
| 1889 | vmaGetMemoryProperties(g_hAllocator, &memProps);
|
| 1890 |
|
| 1891 | VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1892 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 1893 | bufInfo.size = 128;
|
| 1894 |
|
| 1895 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1896 |
|
| 1897 | // No requirements.
|
| 1898 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1899 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1900 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1901 |
|
| 1902 | // Usage.
|
| 1903 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1904 | allocCreateInfo.requiredFlags = 0;
|
| 1905 | allocCreateInfo.preferredFlags = 0;
|
| 1906 | allocCreateInfo.memoryTypeBits = UINT32_MAX;
|
| 1907 |
|
| 1908 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1909 | TEST(res == VK_SUCCESS);
|
| 1910 | TEST(memProps->memoryTypes[allocInfo.memoryType].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1911 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1912 |
|
| 1913 | // Required flags, preferred flags.
|
| 1914 | allocCreateInfo.usage = VMA_MEMORY_USAGE_UNKNOWN;
|
| 1915 | allocCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
|
| 1916 | allocCreateInfo.preferredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT;
|
| 1917 | allocCreateInfo.memoryTypeBits = 0;
|
| 1918 |
|
| 1919 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1920 | TEST(res == VK_SUCCESS);
|
| 1921 | TEST(memProps->memoryTypes[allocInfo.memoryType].propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
|
| 1922 | TEST(memProps->memoryTypes[allocInfo.memoryType].propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1923 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1924 |
|
| 1925 | // memoryTypeBits.
|
| 1926 | const uint32_t memType = allocInfo.memoryType;
|
| 1927 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 1928 | allocCreateInfo.requiredFlags = 0;
|
| 1929 | allocCreateInfo.preferredFlags = 0;
|
| 1930 | allocCreateInfo.memoryTypeBits = 1u << memType;
|
| 1931 |
|
| 1932 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1933 | TEST(res == VK_SUCCESS);
|
| 1934 | TEST(allocInfo.memoryType == memType);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1935 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1936 |
|
| 1937 | }
|
| 1938 |
|
| 1939 | static void TestBasics()
|
| 1940 | {
|
| 1941 | VkResult res;
|
| 1942 |
|
| 1943 | TestMemoryRequirements();
|
| 1944 |
|
| 1945 | // Lost allocation
|
| 1946 | {
|
| 1947 | VmaAllocation alloc = VK_NULL_HANDLE;
|
| 1948 | vmaCreateLostAllocation(g_hAllocator, &alloc);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1949 | TEST(alloc != VK_NULL_HANDLE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1950 |
|
| 1951 | VmaAllocationInfo allocInfo;
|
| 1952 | vmaGetAllocationInfo(g_hAllocator, alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1953 | TEST(allocInfo.deviceMemory == VK_NULL_HANDLE);
|
| 1954 | TEST(allocInfo.size == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1955 |
|
| 1956 | vmaFreeMemory(g_hAllocator, alloc);
|
| 1957 | }
|
| 1958 |
|
| 1959 | // Allocation that is MAPPED and not necessarily HOST_VISIBLE.
|
| 1960 | {
|
| 1961 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1962 | bufCreateInfo.usage = VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
| 1963 | bufCreateInfo.size = 128;
|
| 1964 |
|
| 1965 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 1966 | allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 1967 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 1968 |
|
| 1969 | VkBuffer buf; VmaAllocation alloc; VmaAllocationInfo allocInfo;
|
| 1970 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1971 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1972 |
|
| 1973 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1974 |
|
| 1975 | // Same with OWN_MEMORY.
|
| 1976 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 1977 |
|
| 1978 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 1979 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1980 |
|
| 1981 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 1982 | }
|
| 1983 |
|
| 1984 | TestUserData();
|
Adam Sawicki | 370ab18 | 2018-11-08 16:31:00 +0100 | [diff] [blame] | 1985 |
|
| 1986 | TestInvalidAllocations();
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1987 | }
|
| 1988 |
|
| 1989 | void TestHeapSizeLimit()
|
| 1990 | {
|
Adam Sawicki | b3f5110 | 2019-11-18 13:05:56 +0100 | [diff] [blame] | 1991 | const VkDeviceSize HEAP_SIZE_LIMIT = 200ull * 1024 * 1024; // 200 MB
|
| 1992 | const VkDeviceSize BLOCK_SIZE = 20ull * 1024 * 1024; // 20 MB
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 1993 |
|
| 1994 | VkDeviceSize heapSizeLimit[VK_MAX_MEMORY_HEAPS];
|
| 1995 | for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i)
|
| 1996 | {
|
| 1997 | heapSizeLimit[i] = HEAP_SIZE_LIMIT;
|
| 1998 | }
|
| 1999 |
|
| 2000 | VmaAllocatorCreateInfo allocatorCreateInfo = {};
|
| 2001 | allocatorCreateInfo.physicalDevice = g_hPhysicalDevice;
|
| 2002 | allocatorCreateInfo.device = g_hDevice;
|
| 2003 | allocatorCreateInfo.pHeapSizeLimit = heapSizeLimit;
|
| 2004 |
|
| 2005 | VmaAllocator hAllocator;
|
| 2006 | VkResult res = vmaCreateAllocator(&allocatorCreateInfo, &hAllocator);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2007 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2008 |
|
| 2009 | struct Item
|
| 2010 | {
|
| 2011 | VkBuffer hBuf;
|
| 2012 | VmaAllocation hAlloc;
|
| 2013 | };
|
| 2014 | std::vector<Item> items;
|
| 2015 |
|
| 2016 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 2017 | bufCreateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 2018 |
|
| 2019 | // 1. Allocate two blocks of Own Memory, half the size of BLOCK_SIZE.
|
| 2020 | VmaAllocationInfo ownAllocInfo;
|
| 2021 | {
|
| 2022 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2023 | allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 2024 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 2025 |
|
| 2026 | bufCreateInfo.size = BLOCK_SIZE / 2;
|
| 2027 |
|
| 2028 | for(size_t i = 0; i < 2; ++i)
|
| 2029 | {
|
| 2030 | Item item;
|
| 2031 | res = vmaCreateBuffer(hAllocator, &bufCreateInfo, &allocCreateInfo, &item.hBuf, &item.hAlloc, &ownAllocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2032 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2033 | items.push_back(item);
|
| 2034 | }
|
| 2035 | }
|
| 2036 |
|
| 2037 | // Create pool to make sure allocations must be out of this memory type.
|
| 2038 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 2039 | poolCreateInfo.memoryTypeIndex = ownAllocInfo.memoryType;
|
| 2040 | poolCreateInfo.blockSize = BLOCK_SIZE;
|
| 2041 |
|
| 2042 | VmaPool hPool;
|
| 2043 | res = vmaCreatePool(hAllocator, &poolCreateInfo, &hPool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2044 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2045 |
|
| 2046 | // 2. Allocate normal buffers from all the remaining memory.
|
| 2047 | {
|
| 2048 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2049 | allocCreateInfo.pool = hPool;
|
| 2050 |
|
| 2051 | bufCreateInfo.size = BLOCK_SIZE / 2;
|
| 2052 |
|
| 2053 | const size_t bufCount = ((HEAP_SIZE_LIMIT / BLOCK_SIZE) - 1) * 2;
|
| 2054 | for(size_t i = 0; i < bufCount; ++i)
|
| 2055 | {
|
| 2056 | Item item;
|
| 2057 | res = vmaCreateBuffer(hAllocator, &bufCreateInfo, &allocCreateInfo, &item.hBuf, &item.hAlloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2058 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2059 | items.push_back(item);
|
| 2060 | }
|
| 2061 | }
|
| 2062 |
|
| 2063 | // 3. Allocation of one more (even small) buffer should fail.
|
| 2064 | {
|
| 2065 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2066 | allocCreateInfo.pool = hPool;
|
| 2067 |
|
| 2068 | bufCreateInfo.size = 128;
|
| 2069 |
|
| 2070 | VkBuffer hBuf;
|
| 2071 | VmaAllocation hAlloc;
|
| 2072 | res = vmaCreateBuffer(hAllocator, &bufCreateInfo, &allocCreateInfo, &hBuf, &hAlloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2073 | TEST(res == VK_ERROR_OUT_OF_DEVICE_MEMORY);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2074 | }
|
| 2075 |
|
| 2076 | // Destroy everything.
|
| 2077 | for(size_t i = items.size(); i--; )
|
| 2078 | {
|
| 2079 | vmaDestroyBuffer(hAllocator, items[i].hBuf, items[i].hAlloc);
|
| 2080 | }
|
| 2081 |
|
| 2082 | vmaDestroyPool(hAllocator, hPool);
|
| 2083 |
|
| 2084 | vmaDestroyAllocator(hAllocator);
|
| 2085 | }
|
| 2086 |
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2087 | #if VMA_DEBUG_MARGIN
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2088 | static void TestDebugMargin()
|
| 2089 | {
|
| 2090 | if(VMA_DEBUG_MARGIN == 0)
|
| 2091 | {
|
| 2092 | return;
|
| 2093 | }
|
| 2094 |
|
| 2095 | VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2096 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2097 |
|
| 2098 | VmaAllocationCreateInfo allocCreateInfo = {};
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2099 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2100 |
|
| 2101 | // Create few buffers of different size.
|
| 2102 | const size_t BUF_COUNT = 10;
|
| 2103 | BufferInfo buffers[BUF_COUNT];
|
| 2104 | VmaAllocationInfo allocInfo[BUF_COUNT];
|
| 2105 | for(size_t i = 0; i < 10; ++i)
|
| 2106 | {
|
| 2107 | bufInfo.size = (VkDeviceSize)(i + 1) * 64;
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2108 | // Last one will be mapped.
|
| 2109 | allocCreateInfo.flags = (i == BUF_COUNT - 1) ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2110 |
|
| 2111 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo, &buffers[i].Buffer, &buffers[i].Allocation, &allocInfo[i]);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2112 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2113 | // Margin is preserved also at the beginning of a block.
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2114 | TEST(allocInfo[i].offset >= VMA_DEBUG_MARGIN);
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2115 |
|
| 2116 | if(i == BUF_COUNT - 1)
|
| 2117 | {
|
| 2118 | // Fill with data.
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2119 | TEST(allocInfo[i].pMappedData != nullptr);
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2120 | // Uncomment this "+ 1" to overwrite past end of allocation and check corruption detection.
|
| 2121 | memset(allocInfo[i].pMappedData, 0xFF, bufInfo.size /* + 1 */);
|
| 2122 | }
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2123 | }
|
| 2124 |
|
| 2125 | // Check if their offsets preserve margin between them.
|
| 2126 | std::sort(allocInfo, allocInfo + BUF_COUNT, [](const VmaAllocationInfo& lhs, const VmaAllocationInfo& rhs) -> bool
|
| 2127 | {
|
| 2128 | if(lhs.deviceMemory != rhs.deviceMemory)
|
| 2129 | {
|
| 2130 | return lhs.deviceMemory < rhs.deviceMemory;
|
| 2131 | }
|
| 2132 | return lhs.offset < rhs.offset;
|
| 2133 | });
|
| 2134 | for(size_t i = 1; i < BUF_COUNT; ++i)
|
| 2135 | {
|
| 2136 | if(allocInfo[i].deviceMemory == allocInfo[i - 1].deviceMemory)
|
| 2137 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2138 | TEST(allocInfo[i].offset >= allocInfo[i - 1].offset + VMA_DEBUG_MARGIN);
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2139 | }
|
| 2140 | }
|
| 2141 |
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2142 | VkResult res = vmaCheckCorruption(g_hAllocator, UINT32_MAX);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2143 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2144 |
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2145 | // Destroy all buffers.
|
| 2146 | for(size_t i = BUF_COUNT; i--; )
|
| 2147 | {
|
| 2148 | vmaDestroyBuffer(g_hAllocator, buffers[i].Buffer, buffers[i].Allocation);
|
| 2149 | }
|
| 2150 | }
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 2151 | #endif
|
Adam Sawicki | 73b1665 | 2018-06-11 16:39:25 +0200 | [diff] [blame] | 2152 |
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2153 | static void TestLinearAllocator()
|
| 2154 | {
|
| 2155 | wprintf(L"Test linear allocator\n");
|
| 2156 |
|
| 2157 | RandomNumberGenerator rand{645332};
|
| 2158 |
|
| 2159 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 2160 | sampleBufCreateInfo.size = 1024; // Whatever.
|
| 2161 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 2162 |
|
| 2163 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 2164 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 2165 |
|
| 2166 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 2167 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2168 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2169 |
|
Adam Sawicki | ee08277 | 2018-06-20 17:45:49 +0200 | [diff] [blame] | 2170 | poolCreateInfo.blockSize = 1024 * 300;
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2171 | poolCreateInfo.flags = VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
|
| 2172 | poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
|
| 2173 |
|
| 2174 | VmaPool pool = nullptr;
|
| 2175 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2176 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2177 |
|
| 2178 | VkBufferCreateInfo bufCreateInfo = sampleBufCreateInfo;
|
| 2179 |
|
| 2180 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2181 | allocCreateInfo.pool = pool;
|
| 2182 |
|
| 2183 | constexpr size_t maxBufCount = 100;
|
| 2184 | std::vector<BufferInfo> bufInfo;
|
| 2185 |
|
| 2186 | constexpr VkDeviceSize bufSizeMin = 16;
|
| 2187 | constexpr VkDeviceSize bufSizeMax = 1024;
|
| 2188 | VmaAllocationInfo allocInfo;
|
| 2189 | VkDeviceSize prevOffset = 0;
|
| 2190 |
|
| 2191 | // Test one-time free.
|
| 2192 | for(size_t i = 0; i < 2; ++i)
|
| 2193 | {
|
| 2194 | // Allocate number of buffers of varying size that surely fit into this block.
|
| 2195 | VkDeviceSize bufSumSize = 0;
|
| 2196 | for(size_t i = 0; i < maxBufCount; ++i)
|
| 2197 | {
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2198 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2199 | BufferInfo newBufInfo;
|
| 2200 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2201 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2202 | TEST(res == VK_SUCCESS);
|
| 2203 | TEST(i == 0 || allocInfo.offset > prevOffset);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2204 | bufInfo.push_back(newBufInfo);
|
| 2205 | prevOffset = allocInfo.offset;
|
| 2206 | bufSumSize += bufCreateInfo.size;
|
| 2207 | }
|
| 2208 |
|
| 2209 | // Validate pool stats.
|
| 2210 | VmaPoolStats stats;
|
| 2211 | vmaGetPoolStats(g_hAllocator, pool, &stats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2212 | TEST(stats.size == poolCreateInfo.blockSize);
|
| 2213 | TEST(stats.unusedSize = poolCreateInfo.blockSize - bufSumSize);
|
| 2214 | TEST(stats.allocationCount == bufInfo.size());
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2215 |
|
| 2216 | // Destroy the buffers in random order.
|
| 2217 | while(!bufInfo.empty())
|
| 2218 | {
|
| 2219 | const size_t indexToDestroy = rand.Generate() % bufInfo.size();
|
| 2220 | const BufferInfo& currBufInfo = bufInfo[indexToDestroy];
|
| 2221 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2222 | bufInfo.erase(bufInfo.begin() + indexToDestroy);
|
| 2223 | }
|
| 2224 | }
|
| 2225 |
|
| 2226 | // Test stack.
|
| 2227 | {
|
| 2228 | // Allocate number of buffers of varying size that surely fit into this block.
|
| 2229 | for(size_t i = 0; i < maxBufCount; ++i)
|
| 2230 | {
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2231 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2232 | BufferInfo newBufInfo;
|
| 2233 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2234 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2235 | TEST(res == VK_SUCCESS);
|
| 2236 | TEST(i == 0 || allocInfo.offset > prevOffset);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2237 | bufInfo.push_back(newBufInfo);
|
| 2238 | prevOffset = allocInfo.offset;
|
| 2239 | }
|
| 2240 |
|
| 2241 | // Destroy few buffers from top of the stack.
|
| 2242 | for(size_t i = 0; i < maxBufCount / 5; ++i)
|
| 2243 | {
|
| 2244 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2245 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2246 | bufInfo.pop_back();
|
| 2247 | }
|
| 2248 |
|
| 2249 | // Create some more
|
| 2250 | for(size_t i = 0; i < maxBufCount / 5; ++i)
|
| 2251 | {
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2252 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2253 | BufferInfo newBufInfo;
|
| 2254 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2255 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2256 | TEST(res == VK_SUCCESS);
|
| 2257 | TEST(i == 0 || allocInfo.offset > prevOffset);
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2258 | bufInfo.push_back(newBufInfo);
|
| 2259 | prevOffset = allocInfo.offset;
|
| 2260 | }
|
| 2261 |
|
| 2262 | // Destroy the buffers in reverse order.
|
| 2263 | while(!bufInfo.empty())
|
| 2264 | {
|
| 2265 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2266 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2267 | bufInfo.pop_back();
|
| 2268 | }
|
| 2269 | }
|
| 2270 |
|
Adam Sawicki | ee08277 | 2018-06-20 17:45:49 +0200 | [diff] [blame] | 2271 | // Test ring buffer.
|
| 2272 | {
|
| 2273 | // Allocate number of buffers that surely fit into this block.
|
| 2274 | bufCreateInfo.size = bufSizeMax;
|
| 2275 | for(size_t i = 0; i < maxBufCount; ++i)
|
| 2276 | {
|
| 2277 | BufferInfo newBufInfo;
|
| 2278 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2279 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2280 | TEST(res == VK_SUCCESS);
|
| 2281 | TEST(i == 0 || allocInfo.offset > prevOffset);
|
Adam Sawicki | ee08277 | 2018-06-20 17:45:49 +0200 | [diff] [blame] | 2282 | bufInfo.push_back(newBufInfo);
|
| 2283 | prevOffset = allocInfo.offset;
|
| 2284 | }
|
| 2285 |
|
| 2286 | // Free and allocate new buffers so many times that we make sure we wrap-around at least once.
|
| 2287 | const size_t buffersPerIter = maxBufCount / 10 - 1;
|
| 2288 | const size_t iterCount = poolCreateInfo.blockSize / bufCreateInfo.size / buffersPerIter * 2;
|
| 2289 | for(size_t iter = 0; iter < iterCount; ++iter)
|
| 2290 | {
|
| 2291 | for(size_t bufPerIter = 0; bufPerIter < buffersPerIter; ++bufPerIter)
|
| 2292 | {
|
| 2293 | const BufferInfo& currBufInfo = bufInfo.front();
|
| 2294 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2295 | bufInfo.erase(bufInfo.begin());
|
| 2296 | }
|
| 2297 | for(size_t bufPerIter = 0; bufPerIter < buffersPerIter; ++bufPerIter)
|
| 2298 | {
|
| 2299 | BufferInfo newBufInfo;
|
| 2300 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2301 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2302 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | ee08277 | 2018-06-20 17:45:49 +0200 | [diff] [blame] | 2303 | bufInfo.push_back(newBufInfo);
|
| 2304 | }
|
| 2305 | }
|
| 2306 |
|
| 2307 | // Allocate buffers until we reach out-of-memory.
|
| 2308 | uint32_t debugIndex = 0;
|
| 2309 | while(res == VK_SUCCESS)
|
| 2310 | {
|
| 2311 | BufferInfo newBufInfo;
|
| 2312 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2313 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
| 2314 | if(res == VK_SUCCESS)
|
| 2315 | {
|
| 2316 | bufInfo.push_back(newBufInfo);
|
| 2317 | }
|
| 2318 | else
|
| 2319 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2320 | TEST(res == VK_ERROR_OUT_OF_DEVICE_MEMORY);
|
Adam Sawicki | ee08277 | 2018-06-20 17:45:49 +0200 | [diff] [blame] | 2321 | }
|
| 2322 | ++debugIndex;
|
| 2323 | }
|
| 2324 |
|
| 2325 | // Destroy the buffers in random order.
|
| 2326 | while(!bufInfo.empty())
|
| 2327 | {
|
| 2328 | const size_t indexToDestroy = rand.Generate() % bufInfo.size();
|
| 2329 | const BufferInfo& currBufInfo = bufInfo[indexToDestroy];
|
| 2330 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2331 | bufInfo.erase(bufInfo.begin() + indexToDestroy);
|
| 2332 | }
|
| 2333 | }
|
| 2334 |
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2335 | // Test double stack.
|
| 2336 | {
|
| 2337 | // Allocate number of buffers of varying size that surely fit into this block, alternate from bottom/top.
|
| 2338 | VkDeviceSize prevOffsetLower = 0;
|
| 2339 | VkDeviceSize prevOffsetUpper = poolCreateInfo.blockSize;
|
| 2340 | for(size_t i = 0; i < maxBufCount; ++i)
|
| 2341 | {
|
| 2342 | const bool upperAddress = (i % 2) != 0;
|
| 2343 | if(upperAddress)
|
| 2344 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
| 2345 | else
|
| 2346 | allocCreateInfo.flags &= ~VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2347 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2348 | BufferInfo newBufInfo;
|
| 2349 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2350 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2351 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2352 | if(upperAddress)
|
| 2353 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2354 | TEST(allocInfo.offset < prevOffsetUpper);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2355 | prevOffsetUpper = allocInfo.offset;
|
| 2356 | }
|
| 2357 | else
|
| 2358 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2359 | TEST(allocInfo.offset >= prevOffsetLower);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2360 | prevOffsetLower = allocInfo.offset;
|
| 2361 | }
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2362 | TEST(prevOffsetLower < prevOffsetUpper);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2363 | bufInfo.push_back(newBufInfo);
|
| 2364 | }
|
| 2365 |
|
| 2366 | // Destroy few buffers from top of the stack.
|
| 2367 | for(size_t i = 0; i < maxBufCount / 5; ++i)
|
| 2368 | {
|
| 2369 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2370 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2371 | bufInfo.pop_back();
|
| 2372 | }
|
| 2373 |
|
| 2374 | // Create some more
|
| 2375 | for(size_t i = 0; i < maxBufCount / 5; ++i)
|
| 2376 | {
|
| 2377 | const bool upperAddress = (i % 2) != 0;
|
| 2378 | if(upperAddress)
|
| 2379 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
| 2380 | else
|
| 2381 | allocCreateInfo.flags &= ~VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2382 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2383 | BufferInfo newBufInfo;
|
| 2384 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2385 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2386 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2387 | bufInfo.push_back(newBufInfo);
|
| 2388 | }
|
| 2389 |
|
| 2390 | // Destroy the buffers in reverse order.
|
| 2391 | while(!bufInfo.empty())
|
| 2392 | {
|
| 2393 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2394 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2395 | bufInfo.pop_back();
|
| 2396 | }
|
| 2397 |
|
| 2398 | // Create buffers on both sides until we reach out of memory.
|
| 2399 | prevOffsetLower = 0;
|
| 2400 | prevOffsetUpper = poolCreateInfo.blockSize;
|
| 2401 | res = VK_SUCCESS;
|
| 2402 | for(size_t i = 0; res == VK_SUCCESS; ++i)
|
| 2403 | {
|
| 2404 | const bool upperAddress = (i % 2) != 0;
|
| 2405 | if(upperAddress)
|
| 2406 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
| 2407 | else
|
| 2408 | allocCreateInfo.flags &= ~VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2409 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2410 | BufferInfo newBufInfo;
|
| 2411 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2412 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
| 2413 | if(res == VK_SUCCESS)
|
| 2414 | {
|
| 2415 | if(upperAddress)
|
| 2416 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2417 | TEST(allocInfo.offset < prevOffsetUpper);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2418 | prevOffsetUpper = allocInfo.offset;
|
| 2419 | }
|
| 2420 | else
|
| 2421 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2422 | TEST(allocInfo.offset >= prevOffsetLower);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2423 | prevOffsetLower = allocInfo.offset;
|
| 2424 | }
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2425 | TEST(prevOffsetLower < prevOffsetUpper);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2426 | bufInfo.push_back(newBufInfo);
|
| 2427 | }
|
| 2428 | }
|
| 2429 |
|
| 2430 | // Destroy the buffers in random order.
|
| 2431 | while(!bufInfo.empty())
|
| 2432 | {
|
| 2433 | const size_t indexToDestroy = rand.Generate() % bufInfo.size();
|
| 2434 | const BufferInfo& currBufInfo = bufInfo[indexToDestroy];
|
| 2435 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2436 | bufInfo.erase(bufInfo.begin() + indexToDestroy);
|
| 2437 | }
|
| 2438 |
|
| 2439 | // Create buffers on upper side only, constant size, until we reach out of memory.
|
| 2440 | prevOffsetUpper = poolCreateInfo.blockSize;
|
| 2441 | res = VK_SUCCESS;
|
| 2442 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
| 2443 | bufCreateInfo.size = bufSizeMax;
|
| 2444 | for(size_t i = 0; res == VK_SUCCESS; ++i)
|
| 2445 | {
|
| 2446 | BufferInfo newBufInfo;
|
| 2447 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2448 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
| 2449 | if(res == VK_SUCCESS)
|
| 2450 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2451 | TEST(allocInfo.offset < prevOffsetUpper);
|
Adam Sawicki | 680b225 | 2018-08-22 14:47:32 +0200 | [diff] [blame] | 2452 | prevOffsetUpper = allocInfo.offset;
|
| 2453 | bufInfo.push_back(newBufInfo);
|
| 2454 | }
|
| 2455 | }
|
| 2456 |
|
| 2457 | // Destroy the buffers in reverse order.
|
| 2458 | while(!bufInfo.empty())
|
| 2459 | {
|
| 2460 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2461 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2462 | bufInfo.pop_back();
|
| 2463 | }
|
| 2464 | }
|
| 2465 |
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2466 | // Test ring buffer with lost allocations.
|
| 2467 | {
|
| 2468 | // Allocate number of buffers until pool is full.
|
| 2469 | // Notice CAN_BECOME_LOST flag and call to vmaSetCurrentFrameIndex.
|
| 2470 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT;
|
| 2471 | res = VK_SUCCESS;
|
| 2472 | for(size_t i = 0; res == VK_SUCCESS; ++i)
|
| 2473 | {
|
| 2474 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 2475 |
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2476 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2477 |
|
| 2478 | BufferInfo newBufInfo;
|
| 2479 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2480 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
| 2481 | if(res == VK_SUCCESS)
|
| 2482 | bufInfo.push_back(newBufInfo);
|
| 2483 | }
|
| 2484 |
|
| 2485 | // Free first half of it.
|
| 2486 | {
|
| 2487 | const size_t buffersToDelete = bufInfo.size() / 2;
|
| 2488 | for(size_t i = 0; i < buffersToDelete; ++i)
|
| 2489 | {
|
| 2490 | vmaDestroyBuffer(g_hAllocator, bufInfo[i].Buffer, bufInfo[i].Allocation);
|
| 2491 | }
|
| 2492 | bufInfo.erase(bufInfo.begin(), bufInfo.begin() + buffersToDelete);
|
| 2493 | }
|
| 2494 |
|
| 2495 | // Allocate number of buffers until pool is full again.
|
Adam Sawicki | 0ebdf0c | 2018-08-22 17:02:44 +0200 | [diff] [blame] | 2496 | // This way we make sure ring buffers wraps around, front in in the middle.
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2497 | res = VK_SUCCESS;
|
| 2498 | for(size_t i = 0; res == VK_SUCCESS; ++i)
|
| 2499 | {
|
| 2500 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 2501 |
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2502 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2503 |
|
| 2504 | BufferInfo newBufInfo;
|
| 2505 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2506 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
| 2507 | if(res == VK_SUCCESS)
|
| 2508 | bufInfo.push_back(newBufInfo);
|
| 2509 | }
|
| 2510 |
|
| 2511 | VkDeviceSize firstNewOffset;
|
| 2512 | {
|
| 2513 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 2514 |
|
| 2515 | // Allocate a large buffer with CAN_MAKE_OTHER_LOST.
|
| 2516 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
|
| 2517 | bufCreateInfo.size = bufSizeMax;
|
| 2518 |
|
| 2519 | BufferInfo newBufInfo;
|
| 2520 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2521 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2522 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2523 | bufInfo.push_back(newBufInfo);
|
| 2524 | firstNewOffset = allocInfo.offset;
|
| 2525 |
|
| 2526 | // Make sure at least one buffer from the beginning became lost.
|
| 2527 | vmaGetAllocationInfo(g_hAllocator, bufInfo[0].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2528 | TEST(allocInfo.deviceMemory == VK_NULL_HANDLE);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2529 | }
|
| 2530 |
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2531 | #if 0 // TODO Fix and uncomment. Failing on Intel.
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2532 | // Allocate more buffers that CAN_MAKE_OTHER_LOST until we wrap-around with this.
|
| 2533 | size_t newCount = 1;
|
| 2534 | for(;;)
|
| 2535 | {
|
| 2536 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 2537 |
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2538 | bufCreateInfo.size = align_up<VkDeviceSize>(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2539 |
|
| 2540 | BufferInfo newBufInfo;
|
| 2541 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2542 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2543 |
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2544 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2545 | bufInfo.push_back(newBufInfo);
|
| 2546 | ++newCount;
|
| 2547 | if(allocInfo.offset < firstNewOffset)
|
| 2548 | break;
|
| 2549 | }
|
Adam Sawicki | fd366b6 | 2019-01-24 15:26:43 +0100 | [diff] [blame] | 2550 | #endif
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2551 |
|
Adam Sawicki | 0ebdf0c | 2018-08-22 17:02:44 +0200 | [diff] [blame] | 2552 | // Delete buffers that are lost.
|
| 2553 | for(size_t i = bufInfo.size(); i--; )
|
| 2554 | {
|
| 2555 | vmaGetAllocationInfo(g_hAllocator, bufInfo[i].Allocation, &allocInfo);
|
| 2556 | if(allocInfo.deviceMemory == VK_NULL_HANDLE)
|
| 2557 | {
|
| 2558 | vmaDestroyBuffer(g_hAllocator, bufInfo[i].Buffer, bufInfo[i].Allocation);
|
| 2559 | bufInfo.erase(bufInfo.begin() + i);
|
| 2560 | }
|
| 2561 | }
|
| 2562 |
|
| 2563 | // Test vmaMakePoolAllocationsLost
|
| 2564 | {
|
| 2565 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 2566 |
|
Adam Sawicki | 4d35a5d | 2019-01-24 15:51:59 +0100 | [diff] [blame] | 2567 | size_t lostAllocCount = 0;
|
Adam Sawicki | 0ebdf0c | 2018-08-22 17:02:44 +0200 | [diff] [blame] | 2568 | vmaMakePoolAllocationsLost(g_hAllocator, pool, &lostAllocCount);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2569 | TEST(lostAllocCount > 0);
|
Adam Sawicki | 0ebdf0c | 2018-08-22 17:02:44 +0200 | [diff] [blame] | 2570 |
|
| 2571 | size_t realLostAllocCount = 0;
|
| 2572 | for(size_t i = 0; i < bufInfo.size(); ++i)
|
| 2573 | {
|
| 2574 | vmaGetAllocationInfo(g_hAllocator, bufInfo[i].Allocation, &allocInfo);
|
| 2575 | if(allocInfo.deviceMemory == VK_NULL_HANDLE)
|
| 2576 | ++realLostAllocCount;
|
| 2577 | }
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2578 | TEST(realLostAllocCount == lostAllocCount);
|
Adam Sawicki | 0ebdf0c | 2018-08-22 17:02:44 +0200 | [diff] [blame] | 2579 | }
|
| 2580 |
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 2581 | // Destroy all the buffers in forward order.
|
| 2582 | for(size_t i = 0; i < bufInfo.size(); ++i)
|
| 2583 | vmaDestroyBuffer(g_hAllocator, bufInfo[i].Buffer, bufInfo[i].Allocation);
|
| 2584 | bufInfo.clear();
|
| 2585 | }
|
| 2586 |
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2587 | vmaDestroyPool(g_hAllocator, pool);
|
| 2588 | }
|
Adam Sawicki | f799c4f | 2018-08-23 10:40:30 +0200 | [diff] [blame] | 2589 |
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2590 | static void TestLinearAllocatorMultiBlock()
|
| 2591 | {
|
| 2592 | wprintf(L"Test linear allocator multi block\n");
|
| 2593 |
|
| 2594 | RandomNumberGenerator rand{345673};
|
| 2595 |
|
| 2596 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 2597 | sampleBufCreateInfo.size = 1024 * 1024;
|
| 2598 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 2599 |
|
| 2600 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 2601 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 2602 |
|
| 2603 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 2604 | poolCreateInfo.flags = VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
|
| 2605 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2606 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2607 |
|
| 2608 | VmaPool pool = nullptr;
|
| 2609 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2610 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2611 |
|
| 2612 | VkBufferCreateInfo bufCreateInfo = sampleBufCreateInfo;
|
| 2613 |
|
| 2614 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2615 | allocCreateInfo.pool = pool;
|
| 2616 |
|
| 2617 | std::vector<BufferInfo> bufInfo;
|
| 2618 | VmaAllocationInfo allocInfo;
|
| 2619 |
|
| 2620 | // Test one-time free.
|
| 2621 | {
|
| 2622 | // Allocate buffers until we move to a second block.
|
| 2623 | VkDeviceMemory lastMem = VK_NULL_HANDLE;
|
| 2624 | for(uint32_t i = 0; ; ++i)
|
| 2625 | {
|
| 2626 | BufferInfo newBufInfo;
|
| 2627 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2628 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2629 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2630 | bufInfo.push_back(newBufInfo);
|
| 2631 | if(lastMem && allocInfo.deviceMemory != lastMem)
|
| 2632 | {
|
| 2633 | break;
|
| 2634 | }
|
| 2635 | lastMem = allocInfo.deviceMemory;
|
| 2636 | }
|
| 2637 |
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2638 | TEST(bufInfo.size() > 2);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2639 |
|
| 2640 | // Make sure that pool has now two blocks.
|
| 2641 | VmaPoolStats poolStats = {};
|
| 2642 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2643 | TEST(poolStats.blockCount == 2);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2644 |
|
| 2645 | // Destroy all the buffers in random order.
|
| 2646 | while(!bufInfo.empty())
|
| 2647 | {
|
| 2648 | const size_t indexToDestroy = rand.Generate() % bufInfo.size();
|
| 2649 | const BufferInfo& currBufInfo = bufInfo[indexToDestroy];
|
| 2650 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2651 | bufInfo.erase(bufInfo.begin() + indexToDestroy);
|
| 2652 | }
|
| 2653 |
|
| 2654 | // Make sure that pool has now at most one block.
|
| 2655 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2656 | TEST(poolStats.blockCount <= 1);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2657 | }
|
| 2658 |
|
| 2659 | // Test stack.
|
| 2660 | {
|
| 2661 | // Allocate buffers until we move to a second block.
|
| 2662 | VkDeviceMemory lastMem = VK_NULL_HANDLE;
|
| 2663 | for(uint32_t i = 0; ; ++i)
|
| 2664 | {
|
| 2665 | BufferInfo newBufInfo;
|
| 2666 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2667 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2668 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2669 | bufInfo.push_back(newBufInfo);
|
| 2670 | if(lastMem && allocInfo.deviceMemory != lastMem)
|
| 2671 | {
|
| 2672 | break;
|
| 2673 | }
|
| 2674 | lastMem = allocInfo.deviceMemory;
|
| 2675 | }
|
| 2676 |
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2677 | TEST(bufInfo.size() > 2);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2678 |
|
| 2679 | // Add few more buffers.
|
| 2680 | for(uint32_t i = 0; i < 5; ++i)
|
| 2681 | {
|
| 2682 | BufferInfo newBufInfo;
|
| 2683 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2684 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2685 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2686 | bufInfo.push_back(newBufInfo);
|
| 2687 | }
|
| 2688 |
|
| 2689 | // Make sure that pool has now two blocks.
|
| 2690 | VmaPoolStats poolStats = {};
|
| 2691 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2692 | TEST(poolStats.blockCount == 2);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2693 |
|
| 2694 | // Delete half of buffers, LIFO.
|
| 2695 | for(size_t i = 0, countToDelete = bufInfo.size() / 2; i < countToDelete; ++i)
|
| 2696 | {
|
| 2697 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2698 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2699 | bufInfo.pop_back();
|
| 2700 | }
|
| 2701 |
|
| 2702 | // Add one more buffer.
|
| 2703 | BufferInfo newBufInfo;
|
| 2704 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2705 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2706 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2707 | bufInfo.push_back(newBufInfo);
|
| 2708 |
|
| 2709 | // Make sure that pool has now one block.
|
| 2710 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2711 | TEST(poolStats.blockCount == 1);
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 2712 |
|
| 2713 | // Delete all the remaining buffers, LIFO.
|
| 2714 | while(!bufInfo.empty())
|
| 2715 | {
|
| 2716 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2717 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2718 | bufInfo.pop_back();
|
| 2719 | }
|
Adam Sawicki | f799c4f | 2018-08-23 10:40:30 +0200 | [diff] [blame] | 2720 | }
|
| 2721 |
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 2722 | vmaDestroyPool(g_hAllocator, pool);
|
| 2723 | }
|
| 2724 |
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2725 | static void ManuallyTestLinearAllocator()
|
| 2726 | {
|
| 2727 | VmaStats origStats;
|
| 2728 | vmaCalculateStats(g_hAllocator, &origStats);
|
| 2729 |
|
| 2730 | wprintf(L"Manually test linear allocator\n");
|
| 2731 |
|
| 2732 | RandomNumberGenerator rand{645332};
|
| 2733 |
|
| 2734 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 2735 | sampleBufCreateInfo.size = 1024; // Whatever.
|
| 2736 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 2737 |
|
| 2738 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 2739 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 2740 |
|
| 2741 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 2742 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2743 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2744 |
|
| 2745 | poolCreateInfo.blockSize = 10 * 1024;
|
| 2746 | poolCreateInfo.flags = VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
|
| 2747 | poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
|
| 2748 |
|
| 2749 | VmaPool pool = nullptr;
|
| 2750 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2751 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2752 |
|
| 2753 | VkBufferCreateInfo bufCreateInfo = sampleBufCreateInfo;
|
| 2754 |
|
| 2755 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2756 | allocCreateInfo.pool = pool;
|
| 2757 |
|
| 2758 | std::vector<BufferInfo> bufInfo;
|
| 2759 | VmaAllocationInfo allocInfo;
|
| 2760 | BufferInfo newBufInfo;
|
| 2761 |
|
| 2762 | // Test double stack.
|
| 2763 | {
|
| 2764 | /*
|
| 2765 | Lower: Buffer 32 B, Buffer 1024 B, Buffer 32 B
|
| 2766 | Upper: Buffer 16 B, Buffer 1024 B, Buffer 128 B
|
| 2767 |
|
| 2768 | Totally:
|
| 2769 | 1 block allocated
|
| 2770 | 10240 Vulkan bytes
|
| 2771 | 6 new allocations
|
| 2772 | 2256 bytes in allocations
|
| 2773 | */
|
| 2774 |
|
| 2775 | bufCreateInfo.size = 32;
|
| 2776 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2777 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2778 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2779 | bufInfo.push_back(newBufInfo);
|
| 2780 |
|
| 2781 | bufCreateInfo.size = 1024;
|
| 2782 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2783 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2784 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2785 | bufInfo.push_back(newBufInfo);
|
| 2786 |
|
| 2787 | bufCreateInfo.size = 32;
|
| 2788 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2789 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2790 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2791 | bufInfo.push_back(newBufInfo);
|
| 2792 |
|
| 2793 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_UPPER_ADDRESS_BIT;
|
| 2794 |
|
| 2795 | bufCreateInfo.size = 128;
|
| 2796 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2797 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2798 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2799 | bufInfo.push_back(newBufInfo);
|
| 2800 |
|
| 2801 | bufCreateInfo.size = 1024;
|
| 2802 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2803 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2804 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2805 | bufInfo.push_back(newBufInfo);
|
| 2806 |
|
| 2807 | bufCreateInfo.size = 16;
|
| 2808 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 2809 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2810 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | fd11d75 | 2018-08-22 15:02:10 +0200 | [diff] [blame] | 2811 | bufInfo.push_back(newBufInfo);
|
| 2812 |
|
| 2813 | VmaStats currStats;
|
| 2814 | vmaCalculateStats(g_hAllocator, &currStats);
|
| 2815 | VmaPoolStats poolStats;
|
| 2816 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
| 2817 |
|
| 2818 | char* statsStr = nullptr;
|
| 2819 | vmaBuildStatsString(g_hAllocator, &statsStr, VK_TRUE);
|
| 2820 |
|
| 2821 | // PUT BREAKPOINT HERE TO CHECK.
|
| 2822 | // Inspect: currStats versus origStats, poolStats, statsStr.
|
| 2823 | int I = 0;
|
| 2824 |
|
| 2825 | vmaFreeStatsString(g_hAllocator, statsStr);
|
| 2826 |
|
| 2827 | // Destroy the buffers in reverse order.
|
| 2828 | while(!bufInfo.empty())
|
| 2829 | {
|
| 2830 | const BufferInfo& currBufInfo = bufInfo.back();
|
| 2831 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 2832 | bufInfo.pop_back();
|
| 2833 | }
|
| 2834 | }
|
| 2835 |
|
| 2836 | vmaDestroyPool(g_hAllocator, pool);
|
| 2837 | }
|
| 2838 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2839 | static void BenchmarkAlgorithmsCase(FILE* file,
|
| 2840 | uint32_t algorithm,
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 2841 | bool empty,
|
| 2842 | VmaAllocationCreateFlags allocStrategy,
|
| 2843 | FREE_ORDER freeOrder)
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2844 | {
|
| 2845 | RandomNumberGenerator rand{16223};
|
| 2846 |
|
| 2847 | const VkDeviceSize bufSizeMin = 32;
|
| 2848 | const VkDeviceSize bufSizeMax = 1024;
|
| 2849 | const size_t maxBufCapacity = 10000;
|
| 2850 | const uint32_t iterationCount = 10;
|
| 2851 |
|
| 2852 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 2853 | sampleBufCreateInfo.size = bufSizeMax;
|
| 2854 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 2855 |
|
| 2856 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 2857 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 2858 |
|
| 2859 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 2860 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2861 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2862 |
|
| 2863 | poolCreateInfo.blockSize = bufSizeMax * maxBufCapacity;
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2864 | poolCreateInfo.flags |= algorithm;
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2865 | poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
|
| 2866 |
|
| 2867 | VmaPool pool = nullptr;
|
| 2868 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2869 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2870 |
|
| 2871 | // Buffer created just to get memory requirements. Never bound to any memory.
|
| 2872 | VkBuffer dummyBuffer = VK_NULL_HANDLE;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2873 | res = vkCreateBuffer(g_hDevice, &sampleBufCreateInfo, g_Allocs, &dummyBuffer);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2874 | TEST(res == VK_SUCCESS && dummyBuffer);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2875 |
|
| 2876 | VkMemoryRequirements memReq = {};
|
| 2877 | vkGetBufferMemoryRequirements(g_hDevice, dummyBuffer, &memReq);
|
| 2878 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2879 | vkDestroyBuffer(g_hDevice, dummyBuffer, g_Allocs);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2880 |
|
| 2881 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 2882 | allocCreateInfo.pool = pool;
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 2883 | allocCreateInfo.flags = allocStrategy;
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2884 |
|
| 2885 | VmaAllocation alloc;
|
| 2886 | std::vector<VmaAllocation> baseAllocations;
|
| 2887 |
|
| 2888 | if(!empty)
|
| 2889 | {
|
Adam Sawicki | 1f7f8af | 2018-10-03 17:37:55 +0200 | [diff] [blame] | 2890 | // Make allocations up to 1/3 of pool size.
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2891 | VkDeviceSize totalSize = 0;
|
Adam Sawicki | 1f7f8af | 2018-10-03 17:37:55 +0200 | [diff] [blame] | 2892 | while(totalSize < poolCreateInfo.blockSize / 3)
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2893 | {
|
Adam Sawicki | 4d844e2 | 2019-01-24 16:21:05 +0100 | [diff] [blame] | 2894 | // This test intentionally allows sizes that are aligned to 4 or 16 bytes.
|
| 2895 | // This is theoretically allowed and already uncovered one bug.
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2896 | memReq.size = bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin);
|
| 2897 | res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo, &alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2898 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2899 | baseAllocations.push_back(alloc);
|
| 2900 | totalSize += memReq.size;
|
| 2901 | }
|
| 2902 |
|
| 2903 | // Delete half of them, choose randomly.
|
| 2904 | size_t allocsToDelete = baseAllocations.size() / 2;
|
| 2905 | for(size_t i = 0; i < allocsToDelete; ++i)
|
| 2906 | {
|
| 2907 | const size_t index = (size_t)rand.Generate() % baseAllocations.size();
|
| 2908 | vmaFreeMemory(g_hAllocator, baseAllocations[index]);
|
| 2909 | baseAllocations.erase(baseAllocations.begin() + index);
|
| 2910 | }
|
| 2911 | }
|
| 2912 |
|
| 2913 | // BENCHMARK
|
Adam Sawicki | 1f7f8af | 2018-10-03 17:37:55 +0200 | [diff] [blame] | 2914 | const size_t allocCount = maxBufCapacity / 3;
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2915 | std::vector<VmaAllocation> testAllocations;
|
| 2916 | testAllocations.reserve(allocCount);
|
| 2917 | duration allocTotalDuration = duration::zero();
|
| 2918 | duration freeTotalDuration = duration::zero();
|
| 2919 | for(uint32_t iterationIndex = 0; iterationIndex < iterationCount; ++iterationIndex)
|
| 2920 | {
|
| 2921 | // Allocations
|
| 2922 | time_point allocTimeBeg = std::chrono::high_resolution_clock::now();
|
| 2923 | for(size_t i = 0; i < allocCount; ++i)
|
| 2924 | {
|
| 2925 | memReq.size = bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin);
|
| 2926 | res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo, &alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 2927 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2928 | testAllocations.push_back(alloc);
|
| 2929 | }
|
| 2930 | allocTotalDuration += std::chrono::high_resolution_clock::now() - allocTimeBeg;
|
| 2931 |
|
| 2932 | // Deallocations
|
| 2933 | switch(freeOrder)
|
| 2934 | {
|
| 2935 | case FREE_ORDER::FORWARD:
|
| 2936 | // Leave testAllocations unchanged.
|
| 2937 | break;
|
| 2938 | case FREE_ORDER::BACKWARD:
|
| 2939 | std::reverse(testAllocations.begin(), testAllocations.end());
|
| 2940 | break;
|
| 2941 | case FREE_ORDER::RANDOM:
|
| 2942 | std::shuffle(testAllocations.begin(), testAllocations.end(), MyUniformRandomNumberGenerator(rand));
|
| 2943 | break;
|
| 2944 | default: assert(0);
|
| 2945 | }
|
| 2946 |
|
| 2947 | time_point freeTimeBeg = std::chrono::high_resolution_clock::now();
|
| 2948 | for(size_t i = 0; i < allocCount; ++i)
|
| 2949 | vmaFreeMemory(g_hAllocator, testAllocations[i]);
|
| 2950 | freeTotalDuration += std::chrono::high_resolution_clock::now() - freeTimeBeg;
|
| 2951 |
|
| 2952 | testAllocations.clear();
|
| 2953 | }
|
| 2954 |
|
| 2955 | // Delete baseAllocations
|
| 2956 | while(!baseAllocations.empty())
|
| 2957 | {
|
| 2958 | vmaFreeMemory(g_hAllocator, baseAllocations.back());
|
| 2959 | baseAllocations.pop_back();
|
| 2960 | }
|
| 2961 |
|
| 2962 | vmaDestroyPool(g_hAllocator, pool);
|
| 2963 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2964 | const float allocTotalSeconds = ToFloatSeconds(allocTotalDuration);
|
| 2965 | const float freeTotalSeconds = ToFloatSeconds(freeTotalDuration);
|
| 2966 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2967 | printf(" Algorithm=%s %s Allocation=%s FreeOrder=%s: allocations %g s, free %g s\n",
|
| 2968 | AlgorithmToStr(algorithm),
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 2969 | empty ? "Empty" : "Not empty",
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 2970 | GetAllocationStrategyName(allocStrategy),
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2971 | FREE_ORDER_NAMES[(size_t)freeOrder],
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2972 | allocTotalSeconds,
|
| 2973 | freeTotalSeconds);
|
| 2974 |
|
| 2975 | if(file)
|
| 2976 | {
|
| 2977 | std::string currTime;
|
| 2978 | CurrentTimeToStr(currTime);
|
| 2979 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2980 | fprintf(file, "%s,%s,%s,%u,%s,%s,%g,%g\n",
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2981 | CODE_DESCRIPTION, currTime.c_str(),
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2982 | AlgorithmToStr(algorithm),
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2983 | empty ? 1 : 0,
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 2984 | GetAllocationStrategyName(allocStrategy),
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2985 | FREE_ORDER_NAMES[(uint32_t)freeOrder],
|
| 2986 | allocTotalSeconds,
|
| 2987 | freeTotalSeconds);
|
| 2988 | }
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2989 | }
|
| 2990 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2991 | static void BenchmarkAlgorithms(FILE* file)
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2992 | {
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2993 | wprintf(L"Benchmark algorithms\n");
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 2994 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 2995 | if(file)
|
| 2996 | {
|
| 2997 | fprintf(file,
|
| 2998 | "Code,Time,"
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 2999 | "Algorithm,Empty,Allocation strategy,Free order,"
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 3000 | "Allocation time (s),Deallocation time (s)\n");
|
| 3001 | }
|
| 3002 |
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 3003 | uint32_t freeOrderCount = 1;
|
| 3004 | if(ConfigType >= CONFIG_TYPE::CONFIG_TYPE_LARGE)
|
| 3005 | freeOrderCount = 3;
|
| 3006 | else if(ConfigType >= CONFIG_TYPE::CONFIG_TYPE_SMALL)
|
| 3007 | freeOrderCount = 2;
|
| 3008 |
|
| 3009 | const uint32_t emptyCount = ConfigType >= CONFIG_TYPE::CONFIG_TYPE_SMALL ? 2 : 1;
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3010 | const uint32_t allocStrategyCount = GetAllocationStrategyCount();
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 3011 |
|
| 3012 | for(uint32_t freeOrderIndex = 0; freeOrderIndex < freeOrderCount; ++freeOrderIndex)
|
| 3013 | {
|
| 3014 | FREE_ORDER freeOrder = FREE_ORDER::COUNT;
|
| 3015 | switch(freeOrderIndex)
|
| 3016 | {
|
| 3017 | case 0: freeOrder = FREE_ORDER::BACKWARD; break;
|
| 3018 | case 1: freeOrder = FREE_ORDER::FORWARD; break;
|
| 3019 | case 2: freeOrder = FREE_ORDER::RANDOM; break;
|
| 3020 | default: assert(0);
|
| 3021 | }
|
| 3022 |
|
| 3023 | for(uint32_t emptyIndex = 0; emptyIndex < emptyCount; ++emptyIndex)
|
| 3024 | {
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3025 | for(uint32_t algorithmIndex = 0; algorithmIndex < 3; ++algorithmIndex)
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 3026 | {
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3027 | uint32_t algorithm = 0;
|
| 3028 | switch(algorithmIndex)
|
| 3029 | {
|
| 3030 | case 0:
|
| 3031 | break;
|
| 3032 | case 1:
|
| 3033 | algorithm = VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT;
|
| 3034 | break;
|
| 3035 | case 2:
|
| 3036 | algorithm = VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT;
|
| 3037 | break;
|
| 3038 | default:
|
| 3039 | assert(0);
|
| 3040 | }
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3041 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3042 | uint32_t currAllocStrategyCount = algorithm != 0 ? 1 : allocStrategyCount;
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3043 | for(uint32_t allocStrategyIndex = 0; allocStrategyIndex < currAllocStrategyCount; ++allocStrategyIndex)
|
| 3044 | {
|
| 3045 | VmaAllocatorCreateFlags strategy = 0;
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3046 | if(currAllocStrategyCount > 1)
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3047 | {
|
| 3048 | switch(allocStrategyIndex)
|
| 3049 | {
|
| 3050 | case 0: strategy = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT; break;
|
| 3051 | case 1: strategy = VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT; break;
|
| 3052 | case 2: strategy = VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT; break;
|
| 3053 | default: assert(0);
|
| 3054 | }
|
| 3055 | }
|
| 3056 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3057 | BenchmarkAlgorithmsCase(
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3058 | file,
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 3059 | algorithm,
|
Adam Sawicki | 1f7f8af | 2018-10-03 17:37:55 +0200 | [diff] [blame] | 3060 | (emptyIndex == 0), // empty
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 3061 | strategy,
|
| 3062 | freeOrder); // freeOrder
|
| 3063 | }
|
Adam Sawicki | 0a60713 | 2018-08-24 11:18:41 +0200 | [diff] [blame] | 3064 | }
|
| 3065 | }
|
| 3066 | }
|
| 3067 | }
|
| 3068 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3069 | static void TestPool_SameSize()
|
| 3070 | {
|
| 3071 | const VkDeviceSize BUF_SIZE = 1024 * 1024;
|
| 3072 | const size_t BUF_COUNT = 100;
|
| 3073 | VkResult res;
|
| 3074 |
|
| 3075 | RandomNumberGenerator rand{123};
|
| 3076 |
|
| 3077 | VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 3078 | bufferInfo.size = BUF_SIZE;
|
| 3079 | bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 3080 |
|
| 3081 | uint32_t memoryTypeBits = UINT32_MAX;
|
| 3082 | {
|
| 3083 | VkBuffer dummyBuffer;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3084 | res = vkCreateBuffer(g_hDevice, &bufferInfo, g_Allocs, &dummyBuffer);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3085 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3086 |
|
| 3087 | VkMemoryRequirements memReq;
|
| 3088 | vkGetBufferMemoryRequirements(g_hDevice, dummyBuffer, &memReq);
|
| 3089 | memoryTypeBits = memReq.memoryTypeBits;
|
| 3090 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3091 | vkDestroyBuffer(g_hDevice, dummyBuffer, g_Allocs);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3092 | }
|
| 3093 |
|
| 3094 | VmaAllocationCreateInfo poolAllocInfo = {};
|
| 3095 | poolAllocInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 3096 | uint32_t memTypeIndex;
|
| 3097 | res = vmaFindMemoryTypeIndex(
|
| 3098 | g_hAllocator,
|
| 3099 | memoryTypeBits,
|
| 3100 | &poolAllocInfo,
|
| 3101 | &memTypeIndex);
|
| 3102 |
|
| 3103 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 3104 | poolCreateInfo.memoryTypeIndex = memTypeIndex;
|
| 3105 | poolCreateInfo.blockSize = BUF_SIZE * BUF_COUNT / 4;
|
| 3106 | poolCreateInfo.minBlockCount = 1;
|
| 3107 | poolCreateInfo.maxBlockCount = 4;
|
| 3108 | poolCreateInfo.frameInUseCount = 0;
|
| 3109 |
|
| 3110 | VmaPool pool;
|
| 3111 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3112 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3113 |
|
Adam Sawicki | a020fb8 | 2019-11-02 14:43:06 +0100 | [diff] [blame] | 3114 | // Test pool name
|
| 3115 | {
|
| 3116 | static const char* const POOL_NAME = "Pool name";
|
| 3117 | vmaSetPoolName(g_hAllocator, pool, POOL_NAME);
|
| 3118 |
|
| 3119 | const char* fetchedPoolName = nullptr;
|
| 3120 | vmaGetPoolName(g_hAllocator, pool, &fetchedPoolName);
|
| 3121 | TEST(strcmp(fetchedPoolName, POOL_NAME) == 0);
|
| 3122 |
|
Adam Sawicki | a020fb8 | 2019-11-02 14:43:06 +0100 | [diff] [blame] | 3123 | vmaSetPoolName(g_hAllocator, pool, nullptr);
|
| 3124 | }
|
| 3125 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3126 | vmaSetCurrentFrameIndex(g_hAllocator, 1);
|
| 3127 |
|
| 3128 | VmaAllocationCreateInfo allocInfo = {};
|
| 3129 | allocInfo.pool = pool;
|
| 3130 | allocInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT |
|
| 3131 | VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
|
| 3132 |
|
| 3133 | struct BufItem
|
| 3134 | {
|
| 3135 | VkBuffer Buf;
|
| 3136 | VmaAllocation Alloc;
|
| 3137 | };
|
| 3138 | std::vector<BufItem> items;
|
| 3139 |
|
| 3140 | // Fill entire pool.
|
| 3141 | for(size_t i = 0; i < BUF_COUNT; ++i)
|
| 3142 | {
|
| 3143 | BufItem item;
|
| 3144 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3145 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3146 | items.push_back(item);
|
| 3147 | }
|
| 3148 |
|
| 3149 | // Make sure that another allocation would fail.
|
| 3150 | {
|
| 3151 | BufItem item;
|
| 3152 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3153 | TEST(res == VK_ERROR_OUT_OF_DEVICE_MEMORY);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3154 | }
|
| 3155 |
|
| 3156 | // Validate that no buffer is lost. Also check that they are not mapped.
|
| 3157 | for(size_t i = 0; i < items.size(); ++i)
|
| 3158 | {
|
| 3159 | VmaAllocationInfo allocInfo;
|
| 3160 | vmaGetAllocationInfo(g_hAllocator, items[i].Alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3161 | TEST(allocInfo.deviceMemory != VK_NULL_HANDLE);
|
| 3162 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3163 | }
|
| 3164 |
|
| 3165 | // Free some percent of random items.
|
| 3166 | {
|
| 3167 | const size_t PERCENT_TO_FREE = 10;
|
| 3168 | size_t itemsToFree = items.size() * PERCENT_TO_FREE / 100;
|
| 3169 | for(size_t i = 0; i < itemsToFree; ++i)
|
| 3170 | {
|
| 3171 | size_t index = (size_t)rand.Generate() % items.size();
|
| 3172 | vmaDestroyBuffer(g_hAllocator, items[index].Buf, items[index].Alloc);
|
| 3173 | items.erase(items.begin() + index);
|
| 3174 | }
|
| 3175 | }
|
| 3176 |
|
| 3177 | // Randomly allocate and free items.
|
| 3178 | {
|
| 3179 | const size_t OPERATION_COUNT = BUF_COUNT;
|
| 3180 | for(size_t i = 0; i < OPERATION_COUNT; ++i)
|
| 3181 | {
|
| 3182 | bool allocate = rand.Generate() % 2 != 0;
|
| 3183 | if(allocate)
|
| 3184 | {
|
| 3185 | if(items.size() < BUF_COUNT)
|
| 3186 | {
|
| 3187 | BufItem item;
|
| 3188 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3189 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3190 | items.push_back(item);
|
| 3191 | }
|
| 3192 | }
|
| 3193 | else // Free
|
| 3194 | {
|
| 3195 | if(!items.empty())
|
| 3196 | {
|
| 3197 | size_t index = (size_t)rand.Generate() % items.size();
|
| 3198 | vmaDestroyBuffer(g_hAllocator, items[index].Buf, items[index].Alloc);
|
| 3199 | items.erase(items.begin() + index);
|
| 3200 | }
|
| 3201 | }
|
| 3202 | }
|
| 3203 | }
|
| 3204 |
|
| 3205 | // Allocate up to maximum.
|
| 3206 | while(items.size() < BUF_COUNT)
|
| 3207 | {
|
| 3208 | BufItem item;
|
| 3209 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3210 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3211 | items.push_back(item);
|
| 3212 | }
|
| 3213 |
|
| 3214 | // Validate that no buffer is lost.
|
| 3215 | for(size_t i = 0; i < items.size(); ++i)
|
| 3216 | {
|
| 3217 | VmaAllocationInfo allocInfo;
|
| 3218 | vmaGetAllocationInfo(g_hAllocator, items[i].Alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3219 | TEST(allocInfo.deviceMemory != VK_NULL_HANDLE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3220 | }
|
| 3221 |
|
| 3222 | // Next frame.
|
| 3223 | vmaSetCurrentFrameIndex(g_hAllocator, 2);
|
| 3224 |
|
| 3225 | // Allocate another BUF_COUNT buffers.
|
| 3226 | for(size_t i = 0; i < BUF_COUNT; ++i)
|
| 3227 | {
|
| 3228 | BufItem item;
|
| 3229 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3230 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3231 | items.push_back(item);
|
| 3232 | }
|
| 3233 |
|
| 3234 | // Make sure the first BUF_COUNT is lost. Delete them.
|
| 3235 | for(size_t i = 0; i < BUF_COUNT; ++i)
|
| 3236 | {
|
| 3237 | VmaAllocationInfo allocInfo;
|
| 3238 | vmaGetAllocationInfo(g_hAllocator, items[i].Alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3239 | TEST(allocInfo.deviceMemory == VK_NULL_HANDLE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3240 | vmaDestroyBuffer(g_hAllocator, items[i].Buf, items[i].Alloc);
|
| 3241 | }
|
| 3242 | items.erase(items.begin(), items.begin() + BUF_COUNT);
|
| 3243 |
|
| 3244 | // Validate that no buffer is lost.
|
| 3245 | for(size_t i = 0; i < items.size(); ++i)
|
| 3246 | {
|
| 3247 | VmaAllocationInfo allocInfo;
|
| 3248 | vmaGetAllocationInfo(g_hAllocator, items[i].Alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3249 | TEST(allocInfo.deviceMemory != VK_NULL_HANDLE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3250 | }
|
| 3251 |
|
| 3252 | // Free one item.
|
| 3253 | vmaDestroyBuffer(g_hAllocator, items.back().Buf, items.back().Alloc);
|
| 3254 | items.pop_back();
|
| 3255 |
|
| 3256 | // Validate statistics.
|
| 3257 | {
|
| 3258 | VmaPoolStats poolStats = {};
|
| 3259 | vmaGetPoolStats(g_hAllocator, pool, &poolStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3260 | TEST(poolStats.allocationCount == items.size());
|
| 3261 | TEST(poolStats.size = BUF_COUNT * BUF_SIZE);
|
| 3262 | TEST(poolStats.unusedRangeCount == 1);
|
| 3263 | TEST(poolStats.unusedRangeSizeMax == BUF_SIZE);
|
| 3264 | TEST(poolStats.unusedSize == BUF_SIZE);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3265 | }
|
| 3266 |
|
| 3267 | // Free all remaining items.
|
| 3268 | for(size_t i = items.size(); i--; )
|
| 3269 | vmaDestroyBuffer(g_hAllocator, items[i].Buf, items[i].Alloc);
|
| 3270 | items.clear();
|
| 3271 |
|
| 3272 | // Allocate maximum items again.
|
| 3273 | for(size_t i = 0; i < BUF_COUNT; ++i)
|
| 3274 | {
|
| 3275 | BufItem item;
|
| 3276 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3277 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3278 | items.push_back(item);
|
| 3279 | }
|
| 3280 |
|
| 3281 | // Delete every other item.
|
| 3282 | for(size_t i = 0; i < BUF_COUNT / 2; ++i)
|
| 3283 | {
|
| 3284 | vmaDestroyBuffer(g_hAllocator, items[i].Buf, items[i].Alloc);
|
| 3285 | items.erase(items.begin() + i);
|
| 3286 | }
|
| 3287 |
|
| 3288 | // Defragment!
|
| 3289 | {
|
| 3290 | std::vector<VmaAllocation> allocationsToDefragment(items.size());
|
| 3291 | for(size_t i = 0; i < items.size(); ++i)
|
| 3292 | allocationsToDefragment[i] = items[i].Alloc;
|
| 3293 |
|
| 3294 | VmaDefragmentationStats defragmentationStats;
|
| 3295 | res = vmaDefragment(g_hAllocator, allocationsToDefragment.data(), items.size(), nullptr, nullptr, &defragmentationStats);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3296 | TEST(res == VK_SUCCESS);
|
| 3297 | TEST(defragmentationStats.deviceMemoryBlocksFreed == 2);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3298 | }
|
| 3299 |
|
| 3300 | // Free all remaining items.
|
| 3301 | for(size_t i = items.size(); i--; )
|
| 3302 | vmaDestroyBuffer(g_hAllocator, items[i].Buf, items[i].Alloc);
|
| 3303 | items.clear();
|
| 3304 |
|
| 3305 | ////////////////////////////////////////////////////////////////////////////////
|
| 3306 | // Test for vmaMakePoolAllocationsLost
|
| 3307 |
|
| 3308 | // Allocate 4 buffers on frame 10.
|
| 3309 | vmaSetCurrentFrameIndex(g_hAllocator, 10);
|
| 3310 | for(size_t i = 0; i < 4; ++i)
|
| 3311 | {
|
| 3312 | BufItem item;
|
| 3313 | res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocInfo, &item.Buf, &item.Alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3314 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3315 | items.push_back(item);
|
| 3316 | }
|
| 3317 |
|
| 3318 | // Touch first 2 of them on frame 11.
|
| 3319 | vmaSetCurrentFrameIndex(g_hAllocator, 11);
|
| 3320 | for(size_t i = 0; i < 2; ++i)
|
| 3321 | {
|
| 3322 | VmaAllocationInfo allocInfo;
|
| 3323 | vmaGetAllocationInfo(g_hAllocator, items[i].Alloc, &allocInfo);
|
| 3324 | }
|
| 3325 |
|
| 3326 | // vmaMakePoolAllocationsLost. Only remaining 2 should be lost.
|
| 3327 | size_t lostCount = 0xDEADC0DE;
|
| 3328 | vmaMakePoolAllocationsLost(g_hAllocator, pool, &lostCount);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3329 | TEST(lostCount == 2);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3330 |
|
| 3331 | // Make another call. Now 0 should be lost.
|
| 3332 | vmaMakePoolAllocationsLost(g_hAllocator, pool, &lostCount);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3333 | TEST(lostCount == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3334 |
|
| 3335 | // Make another call, with null count. Should not crash.
|
| 3336 | vmaMakePoolAllocationsLost(g_hAllocator, pool, nullptr);
|
| 3337 |
|
| 3338 | // END: Free all remaining items.
|
| 3339 | for(size_t i = items.size(); i--; )
|
| 3340 | vmaDestroyBuffer(g_hAllocator, items[i].Buf, items[i].Alloc);
|
| 3341 |
|
| 3342 | items.clear();
|
| 3343 |
|
Adam Sawicki | d292417 | 2018-06-11 12:48:46 +0200 | [diff] [blame] | 3344 | ////////////////////////////////////////////////////////////////////////////////
|
| 3345 | // Test for allocation too large for pool
|
| 3346 |
|
| 3347 | {
|
| 3348 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 3349 | allocCreateInfo.pool = pool;
|
| 3350 |
|
| 3351 | VkMemoryRequirements memReq;
|
| 3352 | memReq.memoryTypeBits = UINT32_MAX;
|
| 3353 | memReq.alignment = 1;
|
| 3354 | memReq.size = poolCreateInfo.blockSize + 4;
|
| 3355 |
|
| 3356 | VmaAllocation alloc = nullptr;
|
| 3357 | res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo, &alloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3358 | TEST(res == VK_ERROR_OUT_OF_DEVICE_MEMORY && alloc == nullptr);
|
Adam Sawicki | d292417 | 2018-06-11 12:48:46 +0200 | [diff] [blame] | 3359 | }
|
| 3360 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3361 | vmaDestroyPool(g_hAllocator, pool);
|
| 3362 | }
|
| 3363 |
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3364 | static bool ValidatePattern(const void* pMemory, size_t size, uint8_t pattern)
|
| 3365 | {
|
| 3366 | const uint8_t* pBytes = (const uint8_t*)pMemory;
|
| 3367 | for(size_t i = 0; i < size; ++i)
|
| 3368 | {
|
| 3369 | if(pBytes[i] != pattern)
|
| 3370 | {
|
| 3371 | return false;
|
| 3372 | }
|
| 3373 | }
|
| 3374 | return true;
|
| 3375 | }
|
| 3376 |
|
| 3377 | static void TestAllocationsInitialization()
|
| 3378 | {
|
| 3379 | VkResult res;
|
| 3380 |
|
| 3381 | const size_t BUF_SIZE = 1024;
|
| 3382 |
|
| 3383 | // Create pool.
|
| 3384 |
|
| 3385 | VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 3386 | bufInfo.size = BUF_SIZE;
|
| 3387 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 3388 |
|
| 3389 | VmaAllocationCreateInfo dummyBufAllocCreateInfo = {};
|
| 3390 | dummyBufAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 3391 |
|
| 3392 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 3393 | poolCreateInfo.blockSize = BUF_SIZE * 10;
|
| 3394 | poolCreateInfo.minBlockCount = 1; // To keep memory alive while pool exists.
|
| 3395 | poolCreateInfo.maxBlockCount = 1;
|
| 3396 | res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &bufInfo, &dummyBufAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3397 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3398 |
|
| 3399 | VmaAllocationCreateInfo bufAllocCreateInfo = {};
|
| 3400 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &bufAllocCreateInfo.pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3401 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3402 |
|
| 3403 | // Create one persistently mapped buffer to keep memory of this block mapped,
|
| 3404 | // so that pointer to mapped data will remain (more or less...) valid even
|
| 3405 | // after destruction of other allocations.
|
| 3406 |
|
| 3407 | bufAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 3408 | VkBuffer firstBuf;
|
| 3409 | VmaAllocation firstAlloc;
|
| 3410 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &bufAllocCreateInfo, &firstBuf, &firstAlloc, nullptr);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3411 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3412 |
|
| 3413 | // Test buffers.
|
| 3414 |
|
| 3415 | for(uint32_t i = 0; i < 2; ++i)
|
| 3416 | {
|
| 3417 | const bool persistentlyMapped = i == 0;
|
| 3418 | bufAllocCreateInfo.flags = persistentlyMapped ? VMA_ALLOCATION_CREATE_MAPPED_BIT : 0;
|
| 3419 | VkBuffer buf;
|
| 3420 | VmaAllocation alloc;
|
| 3421 | VmaAllocationInfo allocInfo;
|
| 3422 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &bufAllocCreateInfo, &buf, &alloc, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3423 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3424 |
|
| 3425 | void* pMappedData;
|
| 3426 | if(!persistentlyMapped)
|
| 3427 | {
|
| 3428 | res = vmaMapMemory(g_hAllocator, alloc, &pMappedData);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3429 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3430 | }
|
| 3431 | else
|
| 3432 | {
|
| 3433 | pMappedData = allocInfo.pMappedData;
|
| 3434 | }
|
| 3435 |
|
| 3436 | // Validate initialized content
|
| 3437 | bool valid = ValidatePattern(pMappedData, BUF_SIZE, 0xDC);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3438 | TEST(valid);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3439 |
|
| 3440 | if(!persistentlyMapped)
|
| 3441 | {
|
| 3442 | vmaUnmapMemory(g_hAllocator, alloc);
|
| 3443 | }
|
| 3444 |
|
| 3445 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 3446 |
|
| 3447 | // Validate freed content
|
| 3448 | valid = ValidatePattern(pMappedData, BUF_SIZE, 0xEF);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3449 | TEST(valid);
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 3450 | }
|
| 3451 |
|
| 3452 | vmaDestroyBuffer(g_hAllocator, firstBuf, firstAlloc);
|
| 3453 | vmaDestroyPool(g_hAllocator, bufAllocCreateInfo.pool);
|
| 3454 | }
|
| 3455 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3456 | static void TestPool_Benchmark(
|
| 3457 | PoolTestResult& outResult,
|
| 3458 | const PoolTestConfig& config)
|
| 3459 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3460 | TEST(config.ThreadCount > 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3461 |
|
| 3462 | RandomNumberGenerator mainRand{config.RandSeed};
|
| 3463 |
|
| 3464 | uint32_t allocationSizeProbabilitySum = std::accumulate(
|
| 3465 | config.AllocationSizes.begin(),
|
| 3466 | config.AllocationSizes.end(),
|
| 3467 | 0u,
|
| 3468 | [](uint32_t sum, const AllocationSize& allocSize) {
|
| 3469 | return sum + allocSize.Probability;
|
| 3470 | });
|
| 3471 |
|
| 3472 | VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 3473 | bufferInfo.size = 256; // Whatever.
|
| 3474 | bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 3475 |
|
| 3476 | VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 3477 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 3478 | imageInfo.extent.width = 256; // Whatever.
|
| 3479 | imageInfo.extent.height = 256; // Whatever.
|
| 3480 | imageInfo.extent.depth = 1;
|
| 3481 | imageInfo.mipLevels = 1;
|
| 3482 | imageInfo.arrayLayers = 1;
|
| 3483 | imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 3484 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; // LINEAR if CPU memory.
|
| 3485 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 3486 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // TRANSFER_SRC if CPU memory.
|
| 3487 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 3488 |
|
| 3489 | uint32_t bufferMemoryTypeBits = UINT32_MAX;
|
| 3490 | {
|
| 3491 | VkBuffer dummyBuffer;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3492 | VkResult res = vkCreateBuffer(g_hDevice, &bufferInfo, g_Allocs, &dummyBuffer);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3493 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3494 |
|
| 3495 | VkMemoryRequirements memReq;
|
| 3496 | vkGetBufferMemoryRequirements(g_hDevice, dummyBuffer, &memReq);
|
| 3497 | bufferMemoryTypeBits = memReq.memoryTypeBits;
|
| 3498 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3499 | vkDestroyBuffer(g_hDevice, dummyBuffer, g_Allocs);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3500 | }
|
| 3501 |
|
| 3502 | uint32_t imageMemoryTypeBits = UINT32_MAX;
|
| 3503 | {
|
| 3504 | VkImage dummyImage;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3505 | VkResult res = vkCreateImage(g_hDevice, &imageInfo, g_Allocs, &dummyImage);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3506 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3507 |
|
| 3508 | VkMemoryRequirements memReq;
|
| 3509 | vkGetImageMemoryRequirements(g_hDevice, dummyImage, &memReq);
|
| 3510 | imageMemoryTypeBits = memReq.memoryTypeBits;
|
| 3511 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 3512 | vkDestroyImage(g_hDevice, dummyImage, g_Allocs);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3513 | }
|
| 3514 |
|
| 3515 | uint32_t memoryTypeBits = 0;
|
| 3516 | if(config.UsesBuffers() && config.UsesImages())
|
| 3517 | {
|
| 3518 | memoryTypeBits = bufferMemoryTypeBits & imageMemoryTypeBits;
|
| 3519 | if(memoryTypeBits == 0)
|
| 3520 | {
|
| 3521 | PrintWarning(L"Cannot test buffers + images in the same memory pool on this GPU.");
|
| 3522 | return;
|
| 3523 | }
|
| 3524 | }
|
| 3525 | else if(config.UsesBuffers())
|
| 3526 | memoryTypeBits = bufferMemoryTypeBits;
|
| 3527 | else if(config.UsesImages())
|
| 3528 | memoryTypeBits = imageMemoryTypeBits;
|
| 3529 | else
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3530 | TEST(0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3531 |
|
| 3532 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 3533 | poolCreateInfo.memoryTypeIndex = 0;
|
| 3534 | poolCreateInfo.minBlockCount = 1;
|
| 3535 | poolCreateInfo.maxBlockCount = 1;
|
| 3536 | poolCreateInfo.blockSize = config.PoolSize;
|
| 3537 | poolCreateInfo.frameInUseCount = 1;
|
| 3538 |
|
| 3539 | VmaAllocationCreateInfo dummyAllocCreateInfo = {};
|
| 3540 | dummyAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 3541 | vmaFindMemoryTypeIndex(g_hAllocator, memoryTypeBits, &dummyAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
| 3542 |
|
| 3543 | VmaPool pool;
|
| 3544 | VkResult res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3545 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3546 |
|
| 3547 | // Start time measurement - after creating pool and initializing data structures.
|
| 3548 | time_point timeBeg = std::chrono::high_resolution_clock::now();
|
| 3549 |
|
| 3550 | ////////////////////////////////////////////////////////////////////////////////
|
| 3551 | // ThreadProc
|
| 3552 | auto ThreadProc = [&](
|
| 3553 | PoolTestThreadResult* outThreadResult,
|
| 3554 | uint32_t randSeed,
|
| 3555 | HANDLE frameStartEvent,
|
| 3556 | HANDLE frameEndEvent) -> void
|
| 3557 | {
|
| 3558 | RandomNumberGenerator threadRand{randSeed};
|
| 3559 |
|
| 3560 | outThreadResult->AllocationTimeMin = duration::max();
|
| 3561 | outThreadResult->AllocationTimeSum = duration::zero();
|
| 3562 | outThreadResult->AllocationTimeMax = duration::min();
|
| 3563 | outThreadResult->DeallocationTimeMin = duration::max();
|
| 3564 | outThreadResult->DeallocationTimeSum = duration::zero();
|
| 3565 | outThreadResult->DeallocationTimeMax = duration::min();
|
| 3566 | outThreadResult->AllocationCount = 0;
|
| 3567 | outThreadResult->DeallocationCount = 0;
|
| 3568 | outThreadResult->LostAllocationCount = 0;
|
| 3569 | outThreadResult->LostAllocationTotalSize = 0;
|
| 3570 | outThreadResult->FailedAllocationCount = 0;
|
| 3571 | outThreadResult->FailedAllocationTotalSize = 0;
|
| 3572 |
|
| 3573 | struct Item
|
| 3574 | {
|
| 3575 | VkDeviceSize BufferSize;
|
| 3576 | VkExtent2D ImageSize;
|
| 3577 | VkBuffer Buf;
|
| 3578 | VkImage Image;
|
| 3579 | VmaAllocation Alloc;
|
| 3580 |
|
| 3581 | VkDeviceSize CalcSizeBytes() const
|
| 3582 | {
|
| 3583 | return BufferSize +
|
| 3584 | ImageSize.width * ImageSize.height * 4;
|
| 3585 | }
|
| 3586 | };
|
| 3587 | std::vector<Item> unusedItems, usedItems;
|
| 3588 |
|
| 3589 | const size_t threadTotalItemCount = config.TotalItemCount / config.ThreadCount;
|
| 3590 |
|
| 3591 | // Create all items - all unused, not yet allocated.
|
| 3592 | for(size_t i = 0; i < threadTotalItemCount; ++i)
|
| 3593 | {
|
| 3594 | Item item = {};
|
| 3595 |
|
| 3596 | uint32_t allocSizeIndex = 0;
|
| 3597 | uint32_t r = threadRand.Generate() % allocationSizeProbabilitySum;
|
| 3598 | while(r >= config.AllocationSizes[allocSizeIndex].Probability)
|
| 3599 | r -= config.AllocationSizes[allocSizeIndex++].Probability;
|
| 3600 |
|
| 3601 | const AllocationSize& allocSize = config.AllocationSizes[allocSizeIndex];
|
| 3602 | if(allocSize.BufferSizeMax > 0)
|
| 3603 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3604 | TEST(allocSize.BufferSizeMin > 0);
|
| 3605 | TEST(allocSize.ImageSizeMin == 0 && allocSize.ImageSizeMax == 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3606 | if(allocSize.BufferSizeMax == allocSize.BufferSizeMin)
|
| 3607 | item.BufferSize = allocSize.BufferSizeMin;
|
| 3608 | else
|
| 3609 | {
|
| 3610 | item.BufferSize = allocSize.BufferSizeMin + threadRand.Generate() % (allocSize.BufferSizeMax - allocSize.BufferSizeMin);
|
| 3611 | item.BufferSize = item.BufferSize / 16 * 16;
|
| 3612 | }
|
| 3613 | }
|
| 3614 | else
|
| 3615 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3616 | TEST(allocSize.ImageSizeMin > 0 && allocSize.ImageSizeMax > 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3617 | if(allocSize.ImageSizeMax == allocSize.ImageSizeMin)
|
| 3618 | item.ImageSize.width = item.ImageSize.height = allocSize.ImageSizeMax;
|
| 3619 | else
|
| 3620 | {
|
| 3621 | item.ImageSize.width = allocSize.ImageSizeMin + threadRand.Generate() % (allocSize.ImageSizeMax - allocSize.ImageSizeMin);
|
| 3622 | item.ImageSize.height = allocSize.ImageSizeMin + threadRand.Generate() % (allocSize.ImageSizeMax - allocSize.ImageSizeMin);
|
| 3623 | }
|
| 3624 | }
|
| 3625 |
|
| 3626 | unusedItems.push_back(item);
|
| 3627 | }
|
| 3628 |
|
| 3629 | auto Allocate = [&](Item& item) -> VkResult
|
| 3630 | {
|
| 3631 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 3632 | allocCreateInfo.pool = pool;
|
| 3633 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT |
|
| 3634 | VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
|
| 3635 |
|
| 3636 | if(item.BufferSize)
|
| 3637 | {
|
| 3638 | bufferInfo.size = item.BufferSize;
|
| 3639 | PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult);
|
| 3640 | return vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocCreateInfo, &item.Buf, &item.Alloc, nullptr);
|
| 3641 | }
|
| 3642 | else
|
| 3643 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3644 | TEST(item.ImageSize.width && item.ImageSize.height);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3645 |
|
| 3646 | imageInfo.extent.width = item.ImageSize.width;
|
| 3647 | imageInfo.extent.height = item.ImageSize.height;
|
| 3648 | PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult);
|
| 3649 | return vmaCreateImage(g_hAllocator, &imageInfo, &allocCreateInfo, &item.Image, &item.Alloc, nullptr);
|
| 3650 | }
|
| 3651 | };
|
| 3652 |
|
| 3653 | ////////////////////////////////////////////////////////////////////////////////
|
| 3654 | // Frames
|
| 3655 | for(uint32_t frameIndex = 0; frameIndex < config.FrameCount; ++frameIndex)
|
| 3656 | {
|
| 3657 | WaitForSingleObject(frameStartEvent, INFINITE);
|
| 3658 |
|
| 3659 | // Always make some percent of used bufs unused, to choose different used ones.
|
| 3660 | const size_t bufsToMakeUnused = usedItems.size() * config.ItemsToMakeUnusedPercent / 100;
|
| 3661 | for(size_t i = 0; i < bufsToMakeUnused; ++i)
|
| 3662 | {
|
| 3663 | size_t index = threadRand.Generate() % usedItems.size();
|
| 3664 | unusedItems.push_back(usedItems[index]);
|
| 3665 | usedItems.erase(usedItems.begin() + index);
|
| 3666 | }
|
| 3667 |
|
| 3668 | // Determine which bufs we want to use in this frame.
|
| 3669 | const size_t usedBufCount = (threadRand.Generate() % (config.UsedItemCountMax - config.UsedItemCountMin) + config.UsedItemCountMin)
|
| 3670 | / config.ThreadCount;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3671 | TEST(usedBufCount < usedItems.size() + unusedItems.size());
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3672 | // Move some used to unused.
|
| 3673 | while(usedBufCount < usedItems.size())
|
| 3674 | {
|
| 3675 | size_t index = threadRand.Generate() % usedItems.size();
|
| 3676 | unusedItems.push_back(usedItems[index]);
|
| 3677 | usedItems.erase(usedItems.begin() + index);
|
| 3678 | }
|
| 3679 | // Move some unused to used.
|
| 3680 | while(usedBufCount > usedItems.size())
|
| 3681 | {
|
| 3682 | size_t index = threadRand.Generate() % unusedItems.size();
|
| 3683 | usedItems.push_back(unusedItems[index]);
|
| 3684 | unusedItems.erase(unusedItems.begin() + index);
|
| 3685 | }
|
| 3686 |
|
| 3687 | uint32_t touchExistingCount = 0;
|
| 3688 | uint32_t touchLostCount = 0;
|
| 3689 | uint32_t createSucceededCount = 0;
|
| 3690 | uint32_t createFailedCount = 0;
|
| 3691 |
|
| 3692 | // Touch all used bufs. If not created or lost, allocate.
|
| 3693 | for(size_t i = 0; i < usedItems.size(); ++i)
|
| 3694 | {
|
| 3695 | Item& item = usedItems[i];
|
| 3696 | // Not yet created.
|
| 3697 | if(item.Alloc == VK_NULL_HANDLE)
|
| 3698 | {
|
| 3699 | res = Allocate(item);
|
| 3700 | ++outThreadResult->AllocationCount;
|
| 3701 | if(res != VK_SUCCESS)
|
| 3702 | {
|
| 3703 | item.Alloc = VK_NULL_HANDLE;
|
| 3704 | item.Buf = VK_NULL_HANDLE;
|
| 3705 | ++outThreadResult->FailedAllocationCount;
|
| 3706 | outThreadResult->FailedAllocationTotalSize += item.CalcSizeBytes();
|
| 3707 | ++createFailedCount;
|
| 3708 | }
|
| 3709 | else
|
| 3710 | ++createSucceededCount;
|
| 3711 | }
|
| 3712 | else
|
| 3713 | {
|
| 3714 | // Touch.
|
| 3715 | VmaAllocationInfo allocInfo;
|
| 3716 | vmaGetAllocationInfo(g_hAllocator, item.Alloc, &allocInfo);
|
| 3717 | // Lost.
|
| 3718 | if(allocInfo.deviceMemory == VK_NULL_HANDLE)
|
| 3719 | {
|
| 3720 | ++touchLostCount;
|
| 3721 |
|
| 3722 | // Destroy.
|
| 3723 | {
|
| 3724 | PoolDeallocationTimeRegisterObj timeRegisterObj(*outThreadResult);
|
| 3725 | if(item.Buf)
|
| 3726 | vmaDestroyBuffer(g_hAllocator, item.Buf, item.Alloc);
|
| 3727 | else
|
| 3728 | vmaDestroyImage(g_hAllocator, item.Image, item.Alloc);
|
| 3729 | ++outThreadResult->DeallocationCount;
|
| 3730 | }
|
| 3731 | item.Alloc = VK_NULL_HANDLE;
|
| 3732 | item.Buf = VK_NULL_HANDLE;
|
| 3733 |
|
| 3734 | ++outThreadResult->LostAllocationCount;
|
| 3735 | outThreadResult->LostAllocationTotalSize += item.CalcSizeBytes();
|
| 3736 |
|
| 3737 | // Recreate.
|
| 3738 | res = Allocate(item);
|
| 3739 | ++outThreadResult->AllocationCount;
|
| 3740 | // Creation failed.
|
| 3741 | if(res != VK_SUCCESS)
|
| 3742 | {
|
| 3743 | ++outThreadResult->FailedAllocationCount;
|
| 3744 | outThreadResult->FailedAllocationTotalSize += item.CalcSizeBytes();
|
| 3745 | ++createFailedCount;
|
| 3746 | }
|
| 3747 | else
|
| 3748 | ++createSucceededCount;
|
| 3749 | }
|
| 3750 | else
|
| 3751 | ++touchExistingCount;
|
| 3752 | }
|
| 3753 | }
|
| 3754 |
|
| 3755 | /*
|
| 3756 | printf("Thread %u frame %u: Touch existing %u lost %u, create succeeded %u failed %u\n",
|
| 3757 | randSeed, frameIndex,
|
| 3758 | touchExistingCount, touchLostCount,
|
| 3759 | createSucceededCount, createFailedCount);
|
| 3760 | */
|
| 3761 |
|
| 3762 | SetEvent(frameEndEvent);
|
| 3763 | }
|
| 3764 |
|
| 3765 | // Free all remaining items.
|
| 3766 | for(size_t i = usedItems.size(); i--; )
|
| 3767 | {
|
| 3768 | PoolDeallocationTimeRegisterObj timeRegisterObj(*outThreadResult);
|
| 3769 | if(usedItems[i].Buf)
|
| 3770 | vmaDestroyBuffer(g_hAllocator, usedItems[i].Buf, usedItems[i].Alloc);
|
| 3771 | else
|
| 3772 | vmaDestroyImage(g_hAllocator, usedItems[i].Image, usedItems[i].Alloc);
|
| 3773 | ++outThreadResult->DeallocationCount;
|
| 3774 | }
|
| 3775 | for(size_t i = unusedItems.size(); i--; )
|
| 3776 | {
|
| 3777 | PoolDeallocationTimeRegisterObj timeRegisterOb(*outThreadResult);
|
| 3778 | if(unusedItems[i].Buf)
|
| 3779 | vmaDestroyBuffer(g_hAllocator, unusedItems[i].Buf, unusedItems[i].Alloc);
|
| 3780 | else
|
| 3781 | vmaDestroyImage(g_hAllocator, unusedItems[i].Image, unusedItems[i].Alloc);
|
| 3782 | ++outThreadResult->DeallocationCount;
|
| 3783 | }
|
| 3784 | };
|
| 3785 |
|
| 3786 | // Launch threads.
|
| 3787 | uint32_t threadRandSeed = mainRand.Generate();
|
| 3788 | std::vector<HANDLE> frameStartEvents{config.ThreadCount};
|
| 3789 | std::vector<HANDLE> frameEndEvents{config.ThreadCount};
|
| 3790 | std::vector<std::thread> bkgThreads;
|
| 3791 | std::vector<PoolTestThreadResult> threadResults{config.ThreadCount};
|
| 3792 | for(uint32_t threadIndex = 0; threadIndex < config.ThreadCount; ++threadIndex)
|
| 3793 | {
|
| 3794 | frameStartEvents[threadIndex] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
| 3795 | frameEndEvents[threadIndex] = CreateEvent(NULL, FALSE, FALSE, NULL);
|
| 3796 | bkgThreads.emplace_back(std::bind(
|
| 3797 | ThreadProc,
|
| 3798 | &threadResults[threadIndex],
|
| 3799 | threadRandSeed + threadIndex,
|
| 3800 | frameStartEvents[threadIndex],
|
| 3801 | frameEndEvents[threadIndex]));
|
| 3802 | }
|
| 3803 |
|
| 3804 | // Execute frames.
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3805 | TEST(config.ThreadCount <= MAXIMUM_WAIT_OBJECTS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3806 | for(uint32_t frameIndex = 0; frameIndex < config.FrameCount; ++frameIndex)
|
| 3807 | {
|
| 3808 | vmaSetCurrentFrameIndex(g_hAllocator, frameIndex);
|
| 3809 | for(size_t threadIndex = 0; threadIndex < config.ThreadCount; ++threadIndex)
|
| 3810 | SetEvent(frameStartEvents[threadIndex]);
|
| 3811 | WaitForMultipleObjects(config.ThreadCount, &frameEndEvents[0], TRUE, INFINITE);
|
| 3812 | }
|
| 3813 |
|
| 3814 | // Wait for threads finished
|
| 3815 | for(size_t i = 0; i < bkgThreads.size(); ++i)
|
| 3816 | {
|
| 3817 | bkgThreads[i].join();
|
| 3818 | CloseHandle(frameEndEvents[i]);
|
| 3819 | CloseHandle(frameStartEvents[i]);
|
| 3820 | }
|
| 3821 | bkgThreads.clear();
|
| 3822 |
|
| 3823 | // Finish time measurement - before destroying pool.
|
| 3824 | outResult.TotalTime = std::chrono::high_resolution_clock::now() - timeBeg;
|
| 3825 |
|
| 3826 | vmaDestroyPool(g_hAllocator, pool);
|
| 3827 |
|
| 3828 | outResult.AllocationTimeMin = duration::max();
|
| 3829 | outResult.AllocationTimeAvg = duration::zero();
|
| 3830 | outResult.AllocationTimeMax = duration::min();
|
| 3831 | outResult.DeallocationTimeMin = duration::max();
|
| 3832 | outResult.DeallocationTimeAvg = duration::zero();
|
| 3833 | outResult.DeallocationTimeMax = duration::min();
|
| 3834 | outResult.LostAllocationCount = 0;
|
| 3835 | outResult.LostAllocationTotalSize = 0;
|
| 3836 | outResult.FailedAllocationCount = 0;
|
| 3837 | outResult.FailedAllocationTotalSize = 0;
|
| 3838 | size_t allocationCount = 0;
|
| 3839 | size_t deallocationCount = 0;
|
| 3840 | for(size_t threadIndex = 0; threadIndex < config.ThreadCount; ++threadIndex)
|
| 3841 | {
|
| 3842 | const PoolTestThreadResult& threadResult = threadResults[threadIndex];
|
| 3843 | outResult.AllocationTimeMin = std::min(outResult.AllocationTimeMin, threadResult.AllocationTimeMin);
|
| 3844 | outResult.AllocationTimeMax = std::max(outResult.AllocationTimeMax, threadResult.AllocationTimeMax);
|
| 3845 | outResult.AllocationTimeAvg += threadResult.AllocationTimeSum;
|
| 3846 | outResult.DeallocationTimeMin = std::min(outResult.DeallocationTimeMin, threadResult.DeallocationTimeMin);
|
| 3847 | outResult.DeallocationTimeMax = std::max(outResult.DeallocationTimeMax, threadResult.DeallocationTimeMax);
|
| 3848 | outResult.DeallocationTimeAvg += threadResult.DeallocationTimeSum;
|
| 3849 | allocationCount += threadResult.AllocationCount;
|
| 3850 | deallocationCount += threadResult.DeallocationCount;
|
| 3851 | outResult.FailedAllocationCount += threadResult.FailedAllocationCount;
|
| 3852 | outResult.FailedAllocationTotalSize += threadResult.FailedAllocationTotalSize;
|
| 3853 | outResult.LostAllocationCount += threadResult.LostAllocationCount;
|
| 3854 | outResult.LostAllocationTotalSize += threadResult.LostAllocationTotalSize;
|
| 3855 | }
|
| 3856 | if(allocationCount)
|
| 3857 | outResult.AllocationTimeAvg /= allocationCount;
|
| 3858 | if(deallocationCount)
|
| 3859 | outResult.DeallocationTimeAvg /= deallocationCount;
|
| 3860 | }
|
| 3861 |
|
| 3862 | static inline bool MemoryRegionsOverlap(char* ptr1, size_t size1, char* ptr2, size_t size2)
|
| 3863 | {
|
| 3864 | if(ptr1 < ptr2)
|
| 3865 | return ptr1 + size1 > ptr2;
|
| 3866 | else if(ptr2 < ptr1)
|
| 3867 | return ptr2 + size2 > ptr1;
|
| 3868 | else
|
| 3869 | return true;
|
| 3870 | }
|
| 3871 |
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3872 | static void TestBudget()
|
| 3873 | {
|
| 3874 | wprintf(L"Testing budget...\n");
|
| 3875 |
|
| 3876 | uint32_t memTypeIndex = UINT32_MAX;
|
| 3877 |
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3878 | static const VkDeviceSize BUF_SIZE = 100ull * 1024 * 1024;
|
| 3879 | static const uint32_t BUF_COUNT = 4;
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3880 |
|
| 3881 | for(uint32_t testIndex = 0; testIndex < 2; ++testIndex)
|
| 3882 | {
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3883 | vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex);
|
| 3884 |
|
| 3885 | VmaBudget budgetBeg[VK_MAX_MEMORY_HEAPS] = {};
|
| 3886 | vmaGetBudget(g_hAllocator, budgetBeg);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3887 |
|
| 3888 | VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 3889 | bufInfo.size = BUF_SIZE;
|
| 3890 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 3891 |
|
| 3892 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 3893 | allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 3894 | if(testIndex == 0)
|
| 3895 | {
|
| 3896 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 3897 | }
|
| 3898 |
|
| 3899 | // CREATE BUFFERS
|
| 3900 | uint32_t heapIndex = 0;
|
| 3901 | BufferInfo bufInfos[BUF_COUNT] = {};
|
| 3902 | for(uint32_t bufIndex = 0; bufIndex < BUF_COUNT; ++bufIndex)
|
| 3903 | {
|
| 3904 | VmaAllocationInfo allocInfo;
|
| 3905 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo,
|
| 3906 | &bufInfos[bufIndex].Buffer, &bufInfos[bufIndex].Allocation, &allocInfo);
|
| 3907 | TEST(res == VK_SUCCESS);
|
| 3908 | if(bufIndex == 0)
|
| 3909 | {
|
| 3910 | heapIndex = MemoryTypeToHeap(allocInfo.memoryType);
|
| 3911 | }
|
| 3912 | else
|
| 3913 | {
|
| 3914 | // All buffers need to fall into the same heap.
|
| 3915 | TEST(MemoryTypeToHeap(allocInfo.memoryType) == heapIndex);
|
| 3916 | }
|
| 3917 | }
|
| 3918 |
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3919 | VmaBudget budgetWithBufs[VK_MAX_MEMORY_HEAPS] = {};
|
| 3920 | vmaGetBudget(g_hAllocator, budgetWithBufs);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3921 |
|
| 3922 | // DESTROY BUFFERS
|
| 3923 | for(size_t bufIndex = BUF_COUNT; bufIndex--; )
|
| 3924 | {
|
| 3925 | vmaDestroyBuffer(g_hAllocator, bufInfos[bufIndex].Buffer, bufInfos[bufIndex].Allocation);
|
| 3926 | }
|
| 3927 |
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3928 | VmaBudget budgetEnd[VK_MAX_MEMORY_HEAPS] = {};
|
| 3929 | vmaGetBudget(g_hAllocator, budgetEnd);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3930 |
|
| 3931 | // CHECK
|
| 3932 | for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i)
|
| 3933 | {
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3934 | TEST(budgetEnd[i].allocationBytes <= budgetEnd[i].blockBytes);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3935 | if(i == heapIndex)
|
| 3936 | {
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3937 | TEST(budgetEnd[i].allocationBytes == budgetBeg[i].allocationBytes);
|
| 3938 | TEST(budgetWithBufs[i].allocationBytes == budgetBeg[i].allocationBytes + BUF_SIZE * BUF_COUNT);
|
| 3939 | TEST(budgetWithBufs[i].blockBytes >= budgetEnd[i].blockBytes);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3940 | }
|
| 3941 | else
|
| 3942 | {
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 3943 | TEST(budgetEnd[i].allocationBytes == budgetEnd[i].allocationBytes &&
|
| 3944 | budgetEnd[i].allocationBytes == budgetWithBufs[i].allocationBytes);
|
| 3945 | TEST(budgetEnd[i].blockBytes == budgetEnd[i].blockBytes &&
|
| 3946 | budgetEnd[i].blockBytes == budgetWithBufs[i].blockBytes);
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3947 | }
|
| 3948 | }
|
| 3949 | }
|
| 3950 | }
|
| 3951 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3952 | static void TestMapping()
|
| 3953 | {
|
| 3954 | wprintf(L"Testing mapping...\n");
|
| 3955 |
|
| 3956 | VkResult res;
|
| 3957 | uint32_t memTypeIndex = UINT32_MAX;
|
| 3958 |
|
| 3959 | enum TEST
|
| 3960 | {
|
| 3961 | TEST_NORMAL,
|
| 3962 | TEST_POOL,
|
| 3963 | TEST_DEDICATED,
|
| 3964 | TEST_COUNT
|
| 3965 | };
|
| 3966 | for(uint32_t testIndex = 0; testIndex < TEST_COUNT; ++testIndex)
|
| 3967 | {
|
| 3968 | VmaPool pool = nullptr;
|
| 3969 | if(testIndex == TEST_POOL)
|
| 3970 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3971 | TEST(memTypeIndex != UINT32_MAX);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3972 | VmaPoolCreateInfo poolInfo = {};
|
| 3973 | poolInfo.memoryTypeIndex = memTypeIndex;
|
| 3974 | res = vmaCreatePool(g_hAllocator, &poolInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3975 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3976 | }
|
| 3977 |
|
| 3978 | VkBufferCreateInfo bufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 3979 | bufInfo.size = 0x10000;
|
| 3980 | bufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3981 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3982 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 3983 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 3984 | allocCreateInfo.pool = pool;
|
| 3985 | if(testIndex == TEST_DEDICATED)
|
| 3986 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3987 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3988 | VmaAllocationInfo allocInfo;
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 3989 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 3990 | // Mapped manually
|
| 3991 |
|
| 3992 | // Create 2 buffers.
|
| 3993 | BufferInfo bufferInfos[3];
|
| 3994 | for(size_t i = 0; i < 2; ++i)
|
| 3995 | {
|
| 3996 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo,
|
| 3997 | &bufferInfos[i].Buffer, &bufferInfos[i].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 3998 | TEST(res == VK_SUCCESS);
|
| 3999 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4000 | memTypeIndex = allocInfo.memoryType;
|
| 4001 | }
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 4002 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4003 | // Map buffer 0.
|
| 4004 | char* data00 = nullptr;
|
| 4005 | res = vmaMapMemory(g_hAllocator, bufferInfos[0].Allocation, (void**)&data00);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4006 | TEST(res == VK_SUCCESS && data00 != nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4007 | data00[0xFFFF] = data00[0];
|
| 4008 |
|
| 4009 | // Map buffer 0 second time.
|
| 4010 | char* data01 = nullptr;
|
| 4011 | res = vmaMapMemory(g_hAllocator, bufferInfos[0].Allocation, (void**)&data01);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4012 | TEST(res == VK_SUCCESS && data01 == data00);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4013 |
|
| 4014 | // Map buffer 1.
|
| 4015 | char* data1 = nullptr;
|
| 4016 | res = vmaMapMemory(g_hAllocator, bufferInfos[1].Allocation, (void**)&data1);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4017 | TEST(res == VK_SUCCESS && data1 != nullptr);
|
| 4018 | TEST(!MemoryRegionsOverlap(data00, (size_t)bufInfo.size, data1, (size_t)bufInfo.size));
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4019 | data1[0xFFFF] = data1[0];
|
| 4020 |
|
| 4021 | // Unmap buffer 0 two times.
|
| 4022 | vmaUnmapMemory(g_hAllocator, bufferInfos[0].Allocation);
|
| 4023 | vmaUnmapMemory(g_hAllocator, bufferInfos[0].Allocation);
|
| 4024 | vmaGetAllocationInfo(g_hAllocator, bufferInfos[0].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4025 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4026 |
|
| 4027 | // Unmap buffer 1.
|
| 4028 | vmaUnmapMemory(g_hAllocator, bufferInfos[1].Allocation);
|
| 4029 | vmaGetAllocationInfo(g_hAllocator, bufferInfos[1].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4030 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4031 |
|
| 4032 | // Create 3rd buffer - persistently mapped.
|
| 4033 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 4034 | res = vmaCreateBuffer(g_hAllocator, &bufInfo, &allocCreateInfo,
|
| 4035 | &bufferInfos[2].Buffer, &bufferInfos[2].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4036 | TEST(res == VK_SUCCESS && allocInfo.pMappedData != nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4037 |
|
| 4038 | // Map buffer 2.
|
| 4039 | char* data2 = nullptr;
|
| 4040 | res = vmaMapMemory(g_hAllocator, bufferInfos[2].Allocation, (void**)&data2);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4041 | TEST(res == VK_SUCCESS && data2 == allocInfo.pMappedData);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4042 | data2[0xFFFF] = data2[0];
|
| 4043 |
|
| 4044 | // Unmap buffer 2.
|
| 4045 | vmaUnmapMemory(g_hAllocator, bufferInfos[2].Allocation);
|
| 4046 | vmaGetAllocationInfo(g_hAllocator, bufferInfos[2].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4047 | TEST(allocInfo.pMappedData == data2);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4048 |
|
| 4049 | // Destroy all buffers.
|
| 4050 | for(size_t i = 3; i--; )
|
| 4051 | vmaDestroyBuffer(g_hAllocator, bufferInfos[i].Buffer, bufferInfos[i].Allocation);
|
| 4052 |
|
| 4053 | vmaDestroyPool(g_hAllocator, pool);
|
| 4054 | }
|
| 4055 | }
|
| 4056 |
|
Adam Sawicki | daa6a55 | 2019-06-25 15:26:37 +0200 | [diff] [blame] | 4057 | // Test CREATE_MAPPED with required DEVICE_LOCAL. There was a bug with it.
|
| 4058 | static void TestDeviceLocalMapped()
|
| 4059 | {
|
| 4060 | VkResult res;
|
| 4061 |
|
| 4062 | for(uint32_t testIndex = 0; testIndex < 3; ++testIndex)
|
| 4063 | {
|
| 4064 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 4065 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 4066 | bufCreateInfo.size = 4096;
|
| 4067 |
|
| 4068 | VmaPool pool = VK_NULL_HANDLE;
|
| 4069 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 4070 | allocCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
|
| 4071 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 4072 | if(testIndex == 2)
|
| 4073 | {
|
| 4074 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 4075 | res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
| 4076 | TEST(res == VK_SUCCESS);
|
| 4077 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
| 4078 | TEST(res == VK_SUCCESS);
|
| 4079 | allocCreateInfo.pool = pool;
|
| 4080 | }
|
| 4081 | else if(testIndex == 1)
|
| 4082 | {
|
| 4083 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT;
|
| 4084 | }
|
| 4085 |
|
| 4086 | VkBuffer buf = VK_NULL_HANDLE;
|
| 4087 | VmaAllocation alloc = VK_NULL_HANDLE;
|
| 4088 | VmaAllocationInfo allocInfo = {};
|
| 4089 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo);
|
| 4090 | TEST(res == VK_SUCCESS && alloc);
|
| 4091 |
|
| 4092 | VkMemoryPropertyFlags memTypeFlags = 0;
|
| 4093 | vmaGetMemoryTypeProperties(g_hAllocator, allocInfo.memoryType, &memTypeFlags);
|
| 4094 | const bool shouldBeMapped = (memTypeFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0;
|
| 4095 | TEST((allocInfo.pMappedData != nullptr) == shouldBeMapped);
|
| 4096 |
|
| 4097 | vmaDestroyBuffer(g_hAllocator, buf, alloc);
|
| 4098 | vmaDestroyPool(g_hAllocator, pool);
|
| 4099 | }
|
| 4100 | }
|
| 4101 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4102 | static void TestMappingMultithreaded()
|
| 4103 | {
|
| 4104 | wprintf(L"Testing mapping multithreaded...\n");
|
| 4105 |
|
| 4106 | static const uint32_t threadCount = 16;
|
| 4107 | static const uint32_t bufferCount = 1024;
|
| 4108 | static const uint32_t threadBufferCount = bufferCount / threadCount;
|
| 4109 |
|
| 4110 | VkResult res;
|
| 4111 | volatile uint32_t memTypeIndex = UINT32_MAX;
|
| 4112 |
|
| 4113 | enum TEST
|
| 4114 | {
|
| 4115 | TEST_NORMAL,
|
| 4116 | TEST_POOL,
|
| 4117 | TEST_DEDICATED,
|
| 4118 | TEST_COUNT
|
| 4119 | };
|
| 4120 | for(uint32_t testIndex = 0; testIndex < TEST_COUNT; ++testIndex)
|
| 4121 | {
|
| 4122 | VmaPool pool = nullptr;
|
| 4123 | if(testIndex == TEST_POOL)
|
| 4124 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4125 | TEST(memTypeIndex != UINT32_MAX);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4126 | VmaPoolCreateInfo poolInfo = {};
|
| 4127 | poolInfo.memoryTypeIndex = memTypeIndex;
|
| 4128 | res = vmaCreatePool(g_hAllocator, &poolInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4129 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4130 | }
|
| 4131 |
|
| 4132 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 4133 | bufCreateInfo.size = 0x10000;
|
| 4134 | bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 4135 |
|
| 4136 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 4137 | allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 4138 | allocCreateInfo.pool = pool;
|
| 4139 | if(testIndex == TEST_DEDICATED)
|
| 4140 | allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 4141 |
|
| 4142 | std::thread threads[threadCount];
|
| 4143 | for(uint32_t threadIndex = 0; threadIndex < threadCount; ++threadIndex)
|
| 4144 | {
|
| 4145 | threads[threadIndex] = std::thread([=, &memTypeIndex](){
|
| 4146 | // ======== THREAD FUNCTION ========
|
| 4147 |
|
| 4148 | RandomNumberGenerator rand{threadIndex};
|
| 4149 |
|
| 4150 | enum class MODE
|
| 4151 | {
|
| 4152 | // Don't map this buffer at all.
|
| 4153 | DONT_MAP,
|
| 4154 | // Map and quickly unmap.
|
| 4155 | MAP_FOR_MOMENT,
|
| 4156 | // Map and unmap before destruction.
|
| 4157 | MAP_FOR_LONGER,
|
| 4158 | // Map two times. Quickly unmap, second unmap before destruction.
|
| 4159 | MAP_TWO_TIMES,
|
| 4160 | // Create this buffer as persistently mapped.
|
| 4161 | PERSISTENTLY_MAPPED,
|
| 4162 | COUNT
|
| 4163 | };
|
| 4164 | std::vector<BufferInfo> bufInfos{threadBufferCount};
|
| 4165 | std::vector<MODE> bufModes{threadBufferCount};
|
| 4166 |
|
| 4167 | for(uint32_t bufferIndex = 0; bufferIndex < threadBufferCount; ++bufferIndex)
|
| 4168 | {
|
| 4169 | BufferInfo& bufInfo = bufInfos[bufferIndex];
|
| 4170 | const MODE mode = (MODE)(rand.Generate() % (uint32_t)MODE::COUNT);
|
| 4171 | bufModes[bufferIndex] = mode;
|
| 4172 |
|
| 4173 | VmaAllocationCreateInfo localAllocCreateInfo = allocCreateInfo;
|
| 4174 | if(mode == MODE::PERSISTENTLY_MAPPED)
|
| 4175 | localAllocCreateInfo.flags |= VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 4176 |
|
| 4177 | VmaAllocationInfo allocInfo;
|
| 4178 | VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &localAllocCreateInfo,
|
| 4179 | &bufInfo.Buffer, &bufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4180 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4181 |
|
| 4182 | if(memTypeIndex == UINT32_MAX)
|
| 4183 | memTypeIndex = allocInfo.memoryType;
|
| 4184 |
|
| 4185 | char* data = nullptr;
|
| 4186 |
|
| 4187 | if(mode == MODE::PERSISTENTLY_MAPPED)
|
| 4188 | {
|
| 4189 | data = (char*)allocInfo.pMappedData;
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4190 | TEST(data != nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4191 | }
|
| 4192 | else if(mode == MODE::MAP_FOR_MOMENT || mode == MODE::MAP_FOR_LONGER ||
|
| 4193 | mode == MODE::MAP_TWO_TIMES)
|
| 4194 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4195 | TEST(data == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4196 | res = vmaMapMemory(g_hAllocator, bufInfo.Allocation, (void**)&data);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4197 | TEST(res == VK_SUCCESS && data != nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4198 |
|
| 4199 | if(mode == MODE::MAP_TWO_TIMES)
|
| 4200 | {
|
| 4201 | char* data2 = nullptr;
|
| 4202 | res = vmaMapMemory(g_hAllocator, bufInfo.Allocation, (void**)&data2);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4203 | TEST(res == VK_SUCCESS && data2 == data);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4204 | }
|
| 4205 | }
|
| 4206 | else if(mode == MODE::DONT_MAP)
|
| 4207 | {
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4208 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4209 | }
|
| 4210 | else
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4211 | TEST(0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4212 |
|
| 4213 | // Test if reading and writing from the beginning and end of mapped memory doesn't crash.
|
| 4214 | if(data)
|
| 4215 | data[0xFFFF] = data[0];
|
| 4216 |
|
| 4217 | if(mode == MODE::MAP_FOR_MOMENT || mode == MODE::MAP_TWO_TIMES)
|
| 4218 | {
|
| 4219 | vmaUnmapMemory(g_hAllocator, bufInfo.Allocation);
|
| 4220 |
|
| 4221 | VmaAllocationInfo allocInfo;
|
| 4222 | vmaGetAllocationInfo(g_hAllocator, bufInfo.Allocation, &allocInfo);
|
| 4223 | if(mode == MODE::MAP_FOR_MOMENT)
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4224 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4225 | else
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4226 | TEST(allocInfo.pMappedData == data);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4227 | }
|
| 4228 |
|
| 4229 | switch(rand.Generate() % 3)
|
| 4230 | {
|
| 4231 | case 0: Sleep(0); break; // Yield.
|
| 4232 | case 1: Sleep(10); break; // 10 ms
|
| 4233 | // default: No sleep.
|
| 4234 | }
|
| 4235 |
|
| 4236 | // Test if reading and writing from the beginning and end of mapped memory doesn't crash.
|
| 4237 | if(data)
|
| 4238 | data[0xFFFF] = data[0];
|
| 4239 | }
|
| 4240 |
|
| 4241 | for(size_t bufferIndex = threadBufferCount; bufferIndex--; )
|
| 4242 | {
|
| 4243 | if(bufModes[bufferIndex] == MODE::MAP_FOR_LONGER ||
|
| 4244 | bufModes[bufferIndex] == MODE::MAP_TWO_TIMES)
|
| 4245 | {
|
| 4246 | vmaUnmapMemory(g_hAllocator, bufInfos[bufferIndex].Allocation);
|
| 4247 |
|
| 4248 | VmaAllocationInfo allocInfo;
|
| 4249 | vmaGetAllocationInfo(g_hAllocator, bufInfos[bufferIndex].Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4250 | TEST(allocInfo.pMappedData == nullptr);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4251 | }
|
| 4252 |
|
| 4253 | vmaDestroyBuffer(g_hAllocator, bufInfos[bufferIndex].Buffer, bufInfos[bufferIndex].Allocation);
|
| 4254 | }
|
| 4255 | });
|
| 4256 | }
|
| 4257 |
|
| 4258 | for(uint32_t threadIndex = 0; threadIndex < threadCount; ++threadIndex)
|
| 4259 | threads[threadIndex].join();
|
| 4260 |
|
| 4261 | vmaDestroyPool(g_hAllocator, pool);
|
| 4262 | }
|
| 4263 | }
|
| 4264 |
|
| 4265 | static void WriteMainTestResultHeader(FILE* file)
|
| 4266 | {
|
| 4267 | fprintf(file,
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4268 | "Code,Time,"
|
| 4269 | "Threads,Buffers and images,Sizes,Operations,Allocation strategy,Free order,"
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4270 | "Total Time (us),"
|
| 4271 | "Allocation Time Min (us),"
|
| 4272 | "Allocation Time Avg (us),"
|
| 4273 | "Allocation Time Max (us),"
|
| 4274 | "Deallocation Time Min (us),"
|
| 4275 | "Deallocation Time Avg (us),"
|
| 4276 | "Deallocation Time Max (us),"
|
| 4277 | "Total Memory Allocated (B),"
|
| 4278 | "Free Range Size Avg (B),"
|
| 4279 | "Free Range Size Max (B)\n");
|
| 4280 | }
|
| 4281 |
|
| 4282 | static void WriteMainTestResult(
|
| 4283 | FILE* file,
|
| 4284 | const char* codeDescription,
|
| 4285 | const char* testDescription,
|
| 4286 | const Config& config, const Result& result)
|
| 4287 | {
|
| 4288 | float totalTimeSeconds = ToFloatSeconds(result.TotalTime);
|
| 4289 | float allocationTimeMinSeconds = ToFloatSeconds(result.AllocationTimeMin);
|
| 4290 | float allocationTimeAvgSeconds = ToFloatSeconds(result.AllocationTimeAvg);
|
| 4291 | float allocationTimeMaxSeconds = ToFloatSeconds(result.AllocationTimeMax);
|
| 4292 | float deallocationTimeMinSeconds = ToFloatSeconds(result.DeallocationTimeMin);
|
| 4293 | float deallocationTimeAvgSeconds = ToFloatSeconds(result.DeallocationTimeAvg);
|
| 4294 | float deallocationTimeMaxSeconds = ToFloatSeconds(result.DeallocationTimeMax);
|
| 4295 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 4296 | std::string currTime;
|
| 4297 | CurrentTimeToStr(currTime);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4298 |
|
| 4299 | fprintf(file,
|
| 4300 | "%s,%s,%s,"
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4301 | "%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%I64u,%I64u,%I64u\n",
|
| 4302 | codeDescription,
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 4303 | currTime.c_str(),
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4304 | testDescription,
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4305 | totalTimeSeconds * 1e6f,
|
| 4306 | allocationTimeMinSeconds * 1e6f,
|
| 4307 | allocationTimeAvgSeconds * 1e6f,
|
| 4308 | allocationTimeMaxSeconds * 1e6f,
|
| 4309 | deallocationTimeMinSeconds * 1e6f,
|
| 4310 | deallocationTimeAvgSeconds * 1e6f,
|
| 4311 | deallocationTimeMaxSeconds * 1e6f,
|
| 4312 | result.TotalMemoryAllocated,
|
| 4313 | result.FreeRangeSizeAvg,
|
| 4314 | result.FreeRangeSizeMax);
|
| 4315 | }
|
| 4316 |
|
| 4317 | static void WritePoolTestResultHeader(FILE* file)
|
| 4318 | {
|
| 4319 | fprintf(file,
|
| 4320 | "Code,Test,Time,"
|
| 4321 | "Config,"
|
| 4322 | "Total Time (us),"
|
| 4323 | "Allocation Time Min (us),"
|
| 4324 | "Allocation Time Avg (us),"
|
| 4325 | "Allocation Time Max (us),"
|
| 4326 | "Deallocation Time Min (us),"
|
| 4327 | "Deallocation Time Avg (us),"
|
| 4328 | "Deallocation Time Max (us),"
|
| 4329 | "Lost Allocation Count,"
|
| 4330 | "Lost Allocation Total Size (B),"
|
| 4331 | "Failed Allocation Count,"
|
| 4332 | "Failed Allocation Total Size (B)\n");
|
| 4333 | }
|
| 4334 |
|
| 4335 | static void WritePoolTestResult(
|
| 4336 | FILE* file,
|
| 4337 | const char* codeDescription,
|
| 4338 | const char* testDescription,
|
| 4339 | const PoolTestConfig& config,
|
| 4340 | const PoolTestResult& result)
|
| 4341 | {
|
| 4342 | float totalTimeSeconds = ToFloatSeconds(result.TotalTime);
|
| 4343 | float allocationTimeMinSeconds = ToFloatSeconds(result.AllocationTimeMin);
|
| 4344 | float allocationTimeAvgSeconds = ToFloatSeconds(result.AllocationTimeAvg);
|
| 4345 | float allocationTimeMaxSeconds = ToFloatSeconds(result.AllocationTimeMax);
|
| 4346 | float deallocationTimeMinSeconds = ToFloatSeconds(result.DeallocationTimeMin);
|
| 4347 | float deallocationTimeAvgSeconds = ToFloatSeconds(result.DeallocationTimeAvg);
|
| 4348 | float deallocationTimeMaxSeconds = ToFloatSeconds(result.DeallocationTimeMax);
|
| 4349 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 4350 | std::string currTime;
|
| 4351 | CurrentTimeToStr(currTime);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4352 |
|
| 4353 | fprintf(file,
|
| 4354 | "%s,%s,%s,"
|
| 4355 | "ThreadCount=%u PoolSize=%llu FrameCount=%u TotalItemCount=%u UsedItemCount=%u...%u ItemsToMakeUnusedPercent=%u,"
|
| 4356 | "%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%I64u,%I64u,%I64u,%I64u\n",
|
| 4357 | // General
|
| 4358 | codeDescription,
|
| 4359 | testDescription,
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 4360 | currTime.c_str(),
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4361 | // Config
|
| 4362 | config.ThreadCount,
|
| 4363 | (unsigned long long)config.PoolSize,
|
| 4364 | config.FrameCount,
|
| 4365 | config.TotalItemCount,
|
| 4366 | config.UsedItemCountMin,
|
| 4367 | config.UsedItemCountMax,
|
| 4368 | config.ItemsToMakeUnusedPercent,
|
| 4369 | // Results
|
| 4370 | totalTimeSeconds * 1e6f,
|
| 4371 | allocationTimeMinSeconds * 1e6f,
|
| 4372 | allocationTimeAvgSeconds * 1e6f,
|
| 4373 | allocationTimeMaxSeconds * 1e6f,
|
| 4374 | deallocationTimeMinSeconds * 1e6f,
|
| 4375 | deallocationTimeAvgSeconds * 1e6f,
|
| 4376 | deallocationTimeMaxSeconds * 1e6f,
|
| 4377 | result.LostAllocationCount,
|
| 4378 | result.LostAllocationTotalSize,
|
| 4379 | result.FailedAllocationCount,
|
| 4380 | result.FailedAllocationTotalSize);
|
| 4381 | }
|
| 4382 |
|
| 4383 | static void PerformCustomMainTest(FILE* file)
|
| 4384 | {
|
| 4385 | Config config{};
|
| 4386 | config.RandSeed = 65735476;
|
| 4387 | //config.MaxBytesToAllocate = 4ull * 1024 * 1024; // 4 MB
|
| 4388 | config.MaxBytesToAllocate = 4ull * 1024 * 1024 * 1024; // 4 GB
|
| 4389 | config.MemUsageProbability[0] = 1; // VMA_MEMORY_USAGE_GPU_ONLY
|
| 4390 | config.FreeOrder = FREE_ORDER::FORWARD;
|
| 4391 | config.ThreadCount = 16;
|
| 4392 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 50;
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4393 | config.AllocationStrategy = 0;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4394 |
|
| 4395 | // Buffers
|
| 4396 | //config.AllocationSizes.push_back({4, 16, 1024});
|
| 4397 | config.AllocationSizes.push_back({4, 0x10000, 0xA00000}); // 64 KB ... 10 MB
|
| 4398 |
|
| 4399 | // Images
|
| 4400 | //config.AllocationSizes.push_back({4, 0, 0, 4, 32});
|
| 4401 | //config.AllocationSizes.push_back({4, 0, 0, 256, 2048});
|
| 4402 |
|
| 4403 | config.BeginBytesToAllocate = config.MaxBytesToAllocate * 5 / 100;
|
| 4404 | config.AdditionalOperationCount = 1024;
|
| 4405 |
|
| 4406 | Result result{};
|
| 4407 | VkResult res = MainTest(result, config);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4408 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4409 | WriteMainTestResult(file, "Foo", "CustomTest", config, result);
|
| 4410 | }
|
| 4411 |
|
| 4412 | static void PerformCustomPoolTest(FILE* file)
|
| 4413 | {
|
| 4414 | PoolTestConfig config;
|
| 4415 | config.PoolSize = 100 * 1024 * 1024;
|
| 4416 | config.RandSeed = 2345764;
|
| 4417 | config.ThreadCount = 1;
|
| 4418 | config.FrameCount = 200;
|
| 4419 | config.ItemsToMakeUnusedPercent = 2;
|
| 4420 |
|
| 4421 | AllocationSize allocSize = {};
|
| 4422 | allocSize.BufferSizeMin = 1024;
|
| 4423 | allocSize.BufferSizeMax = 1024 * 1024;
|
| 4424 | allocSize.Probability = 1;
|
| 4425 | config.AllocationSizes.push_back(allocSize);
|
| 4426 |
|
| 4427 | allocSize.BufferSizeMin = 0;
|
| 4428 | allocSize.BufferSizeMax = 0;
|
| 4429 | allocSize.ImageSizeMin = 128;
|
| 4430 | allocSize.ImageSizeMax = 1024;
|
| 4431 | allocSize.Probability = 1;
|
| 4432 | config.AllocationSizes.push_back(allocSize);
|
| 4433 |
|
| 4434 | config.PoolSize = config.CalcAvgResourceSize() * 200;
|
| 4435 | config.UsedItemCountMax = 160;
|
| 4436 | config.TotalItemCount = config.UsedItemCountMax * 10;
|
| 4437 | config.UsedItemCountMin = config.UsedItemCountMax * 80 / 100;
|
| 4438 |
|
| 4439 | g_MemoryAliasingWarningEnabled = false;
|
| 4440 | PoolTestResult result = {};
|
| 4441 | TestPool_Benchmark(result, config);
|
| 4442 | g_MemoryAliasingWarningEnabled = true;
|
| 4443 |
|
| 4444 | WritePoolTestResult(file, "Code desc", "Test desc", config, result);
|
| 4445 | }
|
| 4446 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4447 | static void PerformMainTests(FILE* file)
|
| 4448 | {
|
| 4449 | uint32_t repeatCount = 1;
|
| 4450 | if(ConfigType >= CONFIG_TYPE_MAXIMUM) repeatCount = 3;
|
| 4451 |
|
| 4452 | Config config{};
|
| 4453 | config.RandSeed = 65735476;
|
| 4454 | config.MemUsageProbability[0] = 1; // VMA_MEMORY_USAGE_GPU_ONLY
|
| 4455 | config.FreeOrder = FREE_ORDER::FORWARD;
|
| 4456 |
|
| 4457 | size_t threadCountCount = 1;
|
| 4458 | switch(ConfigType)
|
| 4459 | {
|
| 4460 | case CONFIG_TYPE_MINIMUM: threadCountCount = 1; break;
|
| 4461 | case CONFIG_TYPE_SMALL: threadCountCount = 2; break;
|
| 4462 | case CONFIG_TYPE_AVERAGE: threadCountCount = 3; break;
|
| 4463 | case CONFIG_TYPE_LARGE: threadCountCount = 5; break;
|
| 4464 | case CONFIG_TYPE_MAXIMUM: threadCountCount = 7; break;
|
| 4465 | default: assert(0);
|
| 4466 | }
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4467 |
|
Adam Sawicki | 0a3fb6c | 2018-08-27 14:40:27 +0200 | [diff] [blame] | 4468 | const size_t strategyCount = GetAllocationStrategyCount();
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4469 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4470 | for(size_t threadCountIndex = 0; threadCountIndex < threadCountCount; ++threadCountIndex)
|
| 4471 | {
|
| 4472 | std::string desc1;
|
| 4473 |
|
| 4474 | switch(threadCountIndex)
|
| 4475 | {
|
| 4476 | case 0:
|
| 4477 | desc1 += "1_thread";
|
| 4478 | config.ThreadCount = 1;
|
| 4479 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 0;
|
| 4480 | break;
|
| 4481 | case 1:
|
| 4482 | desc1 += "16_threads+0%_common";
|
| 4483 | config.ThreadCount = 16;
|
| 4484 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 0;
|
| 4485 | break;
|
| 4486 | case 2:
|
| 4487 | desc1 += "16_threads+50%_common";
|
| 4488 | config.ThreadCount = 16;
|
| 4489 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 50;
|
| 4490 | break;
|
| 4491 | case 3:
|
| 4492 | desc1 += "16_threads+100%_common";
|
| 4493 | config.ThreadCount = 16;
|
| 4494 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 100;
|
| 4495 | break;
|
| 4496 | case 4:
|
| 4497 | desc1 += "2_threads+0%_common";
|
| 4498 | config.ThreadCount = 2;
|
| 4499 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 0;
|
| 4500 | break;
|
| 4501 | case 5:
|
| 4502 | desc1 += "2_threads+50%_common";
|
| 4503 | config.ThreadCount = 2;
|
| 4504 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 50;
|
| 4505 | break;
|
| 4506 | case 6:
|
| 4507 | desc1 += "2_threads+100%_common";
|
| 4508 | config.ThreadCount = 2;
|
| 4509 | config.ThreadsUsingCommonAllocationsProbabilityPercent = 100;
|
| 4510 | break;
|
| 4511 | default:
|
| 4512 | assert(0);
|
| 4513 | }
|
| 4514 |
|
| 4515 | // 0 = buffers, 1 = images, 2 = buffers and images
|
| 4516 | size_t buffersVsImagesCount = 2;
|
| 4517 | if(ConfigType >= CONFIG_TYPE_LARGE) ++buffersVsImagesCount;
|
| 4518 | for(size_t buffersVsImagesIndex = 0; buffersVsImagesIndex < buffersVsImagesCount; ++buffersVsImagesIndex)
|
| 4519 | {
|
| 4520 | std::string desc2 = desc1;
|
| 4521 | switch(buffersVsImagesIndex)
|
| 4522 | {
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4523 | case 0: desc2 += ",Buffers"; break;
|
| 4524 | case 1: desc2 += ",Images"; break;
|
| 4525 | case 2: desc2 += ",Buffers+Images"; break;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4526 | default: assert(0);
|
| 4527 | }
|
| 4528 |
|
| 4529 | // 0 = small, 1 = large, 2 = small and large
|
| 4530 | size_t smallVsLargeCount = 2;
|
| 4531 | if(ConfigType >= CONFIG_TYPE_LARGE) ++smallVsLargeCount;
|
| 4532 | for(size_t smallVsLargeIndex = 0; smallVsLargeIndex < smallVsLargeCount; ++smallVsLargeIndex)
|
| 4533 | {
|
| 4534 | std::string desc3 = desc2;
|
| 4535 | switch(smallVsLargeIndex)
|
| 4536 | {
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4537 | case 0: desc3 += ",Small"; break;
|
| 4538 | case 1: desc3 += ",Large"; break;
|
| 4539 | case 2: desc3 += ",Small+Large"; break;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4540 | default: assert(0);
|
| 4541 | }
|
| 4542 |
|
| 4543 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4544 | config.MaxBytesToAllocate = 4ull * 1024 * 1024 * 1024; // 4 GB
|
| 4545 | else
|
| 4546 | config.MaxBytesToAllocate = 4ull * 1024 * 1024;
|
| 4547 |
|
| 4548 | // 0 = varying sizes min...max, 1 = set of constant sizes
|
| 4549 | size_t constantSizesCount = 1;
|
| 4550 | if(ConfigType >= CONFIG_TYPE_SMALL) ++constantSizesCount;
|
| 4551 | for(size_t constantSizesIndex = 0; constantSizesIndex < constantSizesCount; ++constantSizesIndex)
|
| 4552 | {
|
| 4553 | std::string desc4 = desc3;
|
| 4554 | switch(constantSizesIndex)
|
| 4555 | {
|
| 4556 | case 0: desc4 += " Varying_sizes"; break;
|
| 4557 | case 1: desc4 += " Constant_sizes"; break;
|
| 4558 | default: assert(0);
|
| 4559 | }
|
| 4560 |
|
| 4561 | config.AllocationSizes.clear();
|
| 4562 | // Buffers present
|
| 4563 | if(buffersVsImagesIndex == 0 || buffersVsImagesIndex == 2)
|
| 4564 | {
|
| 4565 | // Small
|
| 4566 | if(smallVsLargeIndex == 0 || smallVsLargeIndex == 2)
|
| 4567 | {
|
| 4568 | // Varying size
|
| 4569 | if(constantSizesIndex == 0)
|
| 4570 | config.AllocationSizes.push_back({4, 16, 1024});
|
| 4571 | // Constant sizes
|
| 4572 | else
|
| 4573 | {
|
| 4574 | config.AllocationSizes.push_back({1, 16, 16});
|
| 4575 | config.AllocationSizes.push_back({1, 64, 64});
|
| 4576 | config.AllocationSizes.push_back({1, 256, 256});
|
| 4577 | config.AllocationSizes.push_back({1, 1024, 1024});
|
| 4578 | }
|
| 4579 | }
|
| 4580 | // Large
|
| 4581 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4582 | {
|
| 4583 | // Varying size
|
| 4584 | if(constantSizesIndex == 0)
|
| 4585 | config.AllocationSizes.push_back({4, 0x10000, 0xA00000}); // 64 KB ... 10 MB
|
| 4586 | // Constant sizes
|
| 4587 | else
|
| 4588 | {
|
| 4589 | config.AllocationSizes.push_back({1, 0x10000, 0x10000});
|
| 4590 | config.AllocationSizes.push_back({1, 0x80000, 0x80000});
|
| 4591 | config.AllocationSizes.push_back({1, 0x200000, 0x200000});
|
| 4592 | config.AllocationSizes.push_back({1, 0xA00000, 0xA00000});
|
| 4593 | }
|
| 4594 | }
|
| 4595 | }
|
| 4596 | // Images present
|
| 4597 | if(buffersVsImagesIndex == 1 || buffersVsImagesIndex == 2)
|
| 4598 | {
|
| 4599 | // Small
|
| 4600 | if(smallVsLargeIndex == 0 || smallVsLargeIndex == 2)
|
| 4601 | {
|
| 4602 | // Varying size
|
| 4603 | if(constantSizesIndex == 0)
|
| 4604 | config.AllocationSizes.push_back({4, 0, 0, 4, 32});
|
| 4605 | // Constant sizes
|
| 4606 | else
|
| 4607 | {
|
| 4608 | config.AllocationSizes.push_back({1, 0, 0, 4, 4});
|
| 4609 | config.AllocationSizes.push_back({1, 0, 0, 8, 8});
|
| 4610 | config.AllocationSizes.push_back({1, 0, 0, 16, 16});
|
| 4611 | config.AllocationSizes.push_back({1, 0, 0, 32, 32});
|
| 4612 | }
|
| 4613 | }
|
| 4614 | // Large
|
| 4615 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4616 | {
|
| 4617 | // Varying size
|
| 4618 | if(constantSizesIndex == 0)
|
| 4619 | config.AllocationSizes.push_back({4, 0, 0, 256, 2048});
|
| 4620 | // Constant sizes
|
| 4621 | else
|
| 4622 | {
|
| 4623 | config.AllocationSizes.push_back({1, 0, 0, 256, 256});
|
| 4624 | config.AllocationSizes.push_back({1, 0, 0, 512, 512});
|
| 4625 | config.AllocationSizes.push_back({1, 0, 0, 1024, 1024});
|
| 4626 | config.AllocationSizes.push_back({1, 0, 0, 2048, 2048});
|
| 4627 | }
|
| 4628 | }
|
| 4629 | }
|
| 4630 |
|
| 4631 | // 0 = 100%, additional_operations = 0, 1 = 50%, 2 = 5%, 3 = 95% additional_operations = a lot
|
| 4632 | size_t beginBytesToAllocateCount = 1;
|
| 4633 | if(ConfigType >= CONFIG_TYPE_SMALL) ++beginBytesToAllocateCount;
|
| 4634 | if(ConfigType >= CONFIG_TYPE_AVERAGE) ++beginBytesToAllocateCount;
|
| 4635 | if(ConfigType >= CONFIG_TYPE_LARGE) ++beginBytesToAllocateCount;
|
| 4636 | for(size_t beginBytesToAllocateIndex = 0; beginBytesToAllocateIndex < beginBytesToAllocateCount; ++beginBytesToAllocateIndex)
|
| 4637 | {
|
| 4638 | std::string desc5 = desc4;
|
| 4639 |
|
| 4640 | switch(beginBytesToAllocateIndex)
|
| 4641 | {
|
| 4642 | case 0:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4643 | desc5 += ",Allocate_100%";
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4644 | config.BeginBytesToAllocate = config.MaxBytesToAllocate;
|
| 4645 | config.AdditionalOperationCount = 0;
|
| 4646 | break;
|
| 4647 | case 1:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4648 | desc5 += ",Allocate_50%+Operations";
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4649 | config.BeginBytesToAllocate = config.MaxBytesToAllocate * 50 / 100;
|
| 4650 | config.AdditionalOperationCount = 1024;
|
| 4651 | break;
|
| 4652 | case 2:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4653 | desc5 += ",Allocate_5%+Operations";
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4654 | config.BeginBytesToAllocate = config.MaxBytesToAllocate * 5 / 100;
|
| 4655 | config.AdditionalOperationCount = 1024;
|
| 4656 | break;
|
| 4657 | case 3:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4658 | desc5 += ",Allocate_95%+Operations";
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4659 | config.BeginBytesToAllocate = config.MaxBytesToAllocate * 95 / 100;
|
| 4660 | config.AdditionalOperationCount = 1024;
|
| 4661 | break;
|
| 4662 | default:
|
| 4663 | assert(0);
|
| 4664 | }
|
| 4665 |
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4666 | for(size_t strategyIndex = 0; strategyIndex < strategyCount; ++strategyIndex)
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4667 | {
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4668 | std::string desc6 = desc5;
|
| 4669 | switch(strategyIndex)
|
| 4670 | {
|
| 4671 | case 0:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4672 | desc6 += ",BestFit";
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4673 | config.AllocationStrategy = VMA_ALLOCATION_CREATE_STRATEGY_BEST_FIT_BIT;
|
| 4674 | break;
|
| 4675 | case 1:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4676 | desc6 += ",WorstFit";
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4677 | config.AllocationStrategy = VMA_ALLOCATION_CREATE_STRATEGY_WORST_FIT_BIT;
|
| 4678 | break;
|
| 4679 | case 2:
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4680 | desc6 += ",FirstFit";
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4681 | config.AllocationStrategy = VMA_ALLOCATION_CREATE_STRATEGY_FIRST_FIT_BIT;
|
| 4682 | break;
|
| 4683 | default:
|
| 4684 | assert(0);
|
| 4685 | }
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4686 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 4687 | desc6 += ',';
|
| 4688 | desc6 += FREE_ORDER_NAMES[(uint32_t)config.FreeOrder];
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4689 |
|
| 4690 | const char* testDescription = desc6.c_str();
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4691 |
|
| 4692 | for(size_t repeat = 0; repeat < repeatCount; ++repeat)
|
| 4693 | {
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4694 | printf("%s #%u\n", testDescription, (uint32_t)repeat);
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4695 |
|
| 4696 | Result result{};
|
| 4697 | VkResult res = MainTest(result, config);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4698 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4699 | if(file)
|
| 4700 | {
|
| 4701 | WriteMainTestResult(file, CODE_DESCRIPTION, testDescription, config, result);
|
| 4702 | }
|
Adam Sawicki | 0667e33 | 2018-08-24 17:26:44 +0200 | [diff] [blame] | 4703 | }
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4704 | }
|
| 4705 | }
|
| 4706 | }
|
| 4707 | }
|
| 4708 | }
|
| 4709 | }
|
| 4710 | }
|
| 4711 |
|
| 4712 | static void PerformPoolTests(FILE* file)
|
| 4713 | {
|
| 4714 | const size_t AVG_RESOURCES_PER_POOL = 300;
|
| 4715 |
|
| 4716 | uint32_t repeatCount = 1;
|
| 4717 | if(ConfigType >= CONFIG_TYPE_MAXIMUM) repeatCount = 3;
|
| 4718 |
|
| 4719 | PoolTestConfig config{};
|
| 4720 | config.RandSeed = 2346343;
|
| 4721 | config.FrameCount = 200;
|
| 4722 | config.ItemsToMakeUnusedPercent = 2;
|
| 4723 |
|
| 4724 | size_t threadCountCount = 1;
|
| 4725 | switch(ConfigType)
|
| 4726 | {
|
| 4727 | case CONFIG_TYPE_MINIMUM: threadCountCount = 1; break;
|
| 4728 | case CONFIG_TYPE_SMALL: threadCountCount = 2; break;
|
| 4729 | case CONFIG_TYPE_AVERAGE: threadCountCount = 2; break;
|
| 4730 | case CONFIG_TYPE_LARGE: threadCountCount = 3; break;
|
| 4731 | case CONFIG_TYPE_MAXIMUM: threadCountCount = 3; break;
|
| 4732 | default: assert(0);
|
| 4733 | }
|
| 4734 | for(size_t threadCountIndex = 0; threadCountIndex < threadCountCount; ++threadCountIndex)
|
| 4735 | {
|
| 4736 | std::string desc1;
|
| 4737 |
|
| 4738 | switch(threadCountIndex)
|
| 4739 | {
|
| 4740 | case 0:
|
| 4741 | desc1 += "1_thread";
|
| 4742 | config.ThreadCount = 1;
|
| 4743 | break;
|
| 4744 | case 1:
|
| 4745 | desc1 += "16_threads";
|
| 4746 | config.ThreadCount = 16;
|
| 4747 | break;
|
| 4748 | case 2:
|
| 4749 | desc1 += "2_threads";
|
| 4750 | config.ThreadCount = 2;
|
| 4751 | break;
|
| 4752 | default:
|
| 4753 | assert(0);
|
| 4754 | }
|
| 4755 |
|
| 4756 | // 0 = buffers, 1 = images, 2 = buffers and images
|
| 4757 | size_t buffersVsImagesCount = 2;
|
| 4758 | if(ConfigType >= CONFIG_TYPE_LARGE) ++buffersVsImagesCount;
|
| 4759 | for(size_t buffersVsImagesIndex = 0; buffersVsImagesIndex < buffersVsImagesCount; ++buffersVsImagesIndex)
|
| 4760 | {
|
| 4761 | std::string desc2 = desc1;
|
| 4762 | switch(buffersVsImagesIndex)
|
| 4763 | {
|
| 4764 | case 0: desc2 += " Buffers"; break;
|
| 4765 | case 1: desc2 += " Images"; break;
|
| 4766 | case 2: desc2 += " Buffers+Images"; break;
|
| 4767 | default: assert(0);
|
| 4768 | }
|
| 4769 |
|
| 4770 | // 0 = small, 1 = large, 2 = small and large
|
| 4771 | size_t smallVsLargeCount = 2;
|
| 4772 | if(ConfigType >= CONFIG_TYPE_LARGE) ++smallVsLargeCount;
|
| 4773 | for(size_t smallVsLargeIndex = 0; smallVsLargeIndex < smallVsLargeCount; ++smallVsLargeIndex)
|
| 4774 | {
|
| 4775 | std::string desc3 = desc2;
|
| 4776 | switch(smallVsLargeIndex)
|
| 4777 | {
|
| 4778 | case 0: desc3 += " Small"; break;
|
| 4779 | case 1: desc3 += " Large"; break;
|
| 4780 | case 2: desc3 += " Small+Large"; break;
|
| 4781 | default: assert(0);
|
| 4782 | }
|
| 4783 |
|
| 4784 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4785 | config.PoolSize = 6ull * 1024 * 1024 * 1024; // 6 GB
|
| 4786 | else
|
| 4787 | config.PoolSize = 4ull * 1024 * 1024;
|
| 4788 |
|
| 4789 | // 0 = varying sizes min...max, 1 = set of constant sizes
|
| 4790 | size_t constantSizesCount = 1;
|
| 4791 | if(ConfigType >= CONFIG_TYPE_SMALL) ++constantSizesCount;
|
| 4792 | for(size_t constantSizesIndex = 0; constantSizesIndex < constantSizesCount; ++constantSizesIndex)
|
| 4793 | {
|
| 4794 | std::string desc4 = desc3;
|
| 4795 | switch(constantSizesIndex)
|
| 4796 | {
|
| 4797 | case 0: desc4 += " Varying_sizes"; break;
|
| 4798 | case 1: desc4 += " Constant_sizes"; break;
|
| 4799 | default: assert(0);
|
| 4800 | }
|
| 4801 |
|
| 4802 | config.AllocationSizes.clear();
|
| 4803 | // Buffers present
|
| 4804 | if(buffersVsImagesIndex == 0 || buffersVsImagesIndex == 2)
|
| 4805 | {
|
| 4806 | // Small
|
| 4807 | if(smallVsLargeIndex == 0 || smallVsLargeIndex == 2)
|
| 4808 | {
|
| 4809 | // Varying size
|
| 4810 | if(constantSizesIndex == 0)
|
| 4811 | config.AllocationSizes.push_back({4, 16, 1024});
|
| 4812 | // Constant sizes
|
| 4813 | else
|
| 4814 | {
|
| 4815 | config.AllocationSizes.push_back({1, 16, 16});
|
| 4816 | config.AllocationSizes.push_back({1, 64, 64});
|
| 4817 | config.AllocationSizes.push_back({1, 256, 256});
|
| 4818 | config.AllocationSizes.push_back({1, 1024, 1024});
|
| 4819 | }
|
| 4820 | }
|
| 4821 | // Large
|
| 4822 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4823 | {
|
| 4824 | // Varying size
|
| 4825 | if(constantSizesIndex == 0)
|
| 4826 | config.AllocationSizes.push_back({4, 0x10000, 0xA00000}); // 64 KB ... 10 MB
|
| 4827 | // Constant sizes
|
| 4828 | else
|
| 4829 | {
|
| 4830 | config.AllocationSizes.push_back({1, 0x10000, 0x10000});
|
| 4831 | config.AllocationSizes.push_back({1, 0x80000, 0x80000});
|
| 4832 | config.AllocationSizes.push_back({1, 0x200000, 0x200000});
|
| 4833 | config.AllocationSizes.push_back({1, 0xA00000, 0xA00000});
|
| 4834 | }
|
| 4835 | }
|
| 4836 | }
|
| 4837 | // Images present
|
| 4838 | if(buffersVsImagesIndex == 1 || buffersVsImagesIndex == 2)
|
| 4839 | {
|
| 4840 | // Small
|
| 4841 | if(smallVsLargeIndex == 0 || smallVsLargeIndex == 2)
|
| 4842 | {
|
| 4843 | // Varying size
|
| 4844 | if(constantSizesIndex == 0)
|
| 4845 | config.AllocationSizes.push_back({4, 0, 0, 4, 32});
|
| 4846 | // Constant sizes
|
| 4847 | else
|
| 4848 | {
|
| 4849 | config.AllocationSizes.push_back({1, 0, 0, 4, 4});
|
| 4850 | config.AllocationSizes.push_back({1, 0, 0, 8, 8});
|
| 4851 | config.AllocationSizes.push_back({1, 0, 0, 16, 16});
|
| 4852 | config.AllocationSizes.push_back({1, 0, 0, 32, 32});
|
| 4853 | }
|
| 4854 | }
|
| 4855 | // Large
|
| 4856 | if(smallVsLargeIndex == 1 || smallVsLargeIndex == 2)
|
| 4857 | {
|
| 4858 | // Varying size
|
| 4859 | if(constantSizesIndex == 0)
|
| 4860 | config.AllocationSizes.push_back({4, 0, 0, 256, 2048});
|
| 4861 | // Constant sizes
|
| 4862 | else
|
| 4863 | {
|
| 4864 | config.AllocationSizes.push_back({1, 0, 0, 256, 256});
|
| 4865 | config.AllocationSizes.push_back({1, 0, 0, 512, 512});
|
| 4866 | config.AllocationSizes.push_back({1, 0, 0, 1024, 1024});
|
| 4867 | config.AllocationSizes.push_back({1, 0, 0, 2048, 2048});
|
| 4868 | }
|
| 4869 | }
|
| 4870 | }
|
| 4871 |
|
| 4872 | const VkDeviceSize avgResourceSize = config.CalcAvgResourceSize();
|
| 4873 | config.PoolSize = avgResourceSize * AVG_RESOURCES_PER_POOL;
|
| 4874 |
|
| 4875 | // 0 = 66%, 1 = 133%, 2 = 100%, 3 = 33%, 4 = 166%
|
| 4876 | size_t subscriptionModeCount;
|
| 4877 | switch(ConfigType)
|
| 4878 | {
|
| 4879 | case CONFIG_TYPE_MINIMUM: subscriptionModeCount = 2; break;
|
| 4880 | case CONFIG_TYPE_SMALL: subscriptionModeCount = 2; break;
|
| 4881 | case CONFIG_TYPE_AVERAGE: subscriptionModeCount = 3; break;
|
| 4882 | case CONFIG_TYPE_LARGE: subscriptionModeCount = 5; break;
|
| 4883 | case CONFIG_TYPE_MAXIMUM: subscriptionModeCount = 5; break;
|
| 4884 | default: assert(0);
|
| 4885 | }
|
| 4886 | for(size_t subscriptionModeIndex = 0; subscriptionModeIndex < subscriptionModeCount; ++subscriptionModeIndex)
|
| 4887 | {
|
| 4888 | std::string desc5 = desc4;
|
| 4889 |
|
| 4890 | switch(subscriptionModeIndex)
|
| 4891 | {
|
| 4892 | case 0:
|
| 4893 | desc5 += " Subscription_66%";
|
| 4894 | config.UsedItemCountMax = AVG_RESOURCES_PER_POOL * 66 / 100;
|
| 4895 | break;
|
| 4896 | case 1:
|
| 4897 | desc5 += " Subscription_133%";
|
| 4898 | config.UsedItemCountMax = AVG_RESOURCES_PER_POOL * 133 / 100;
|
| 4899 | break;
|
| 4900 | case 2:
|
| 4901 | desc5 += " Subscription_100%";
|
| 4902 | config.UsedItemCountMax = AVG_RESOURCES_PER_POOL;
|
| 4903 | break;
|
| 4904 | case 3:
|
| 4905 | desc5 += " Subscription_33%";
|
| 4906 | config.UsedItemCountMax = AVG_RESOURCES_PER_POOL * 33 / 100;
|
| 4907 | break;
|
| 4908 | case 4:
|
| 4909 | desc5 += " Subscription_166%";
|
| 4910 | config.UsedItemCountMax = AVG_RESOURCES_PER_POOL * 166 / 100;
|
| 4911 | break;
|
| 4912 | default:
|
| 4913 | assert(0);
|
| 4914 | }
|
| 4915 |
|
| 4916 | config.TotalItemCount = config.UsedItemCountMax * 5;
|
| 4917 | config.UsedItemCountMin = config.UsedItemCountMax * 80 / 100;
|
| 4918 |
|
| 4919 | const char* testDescription = desc5.c_str();
|
| 4920 |
|
| 4921 | for(size_t repeat = 0; repeat < repeatCount; ++repeat)
|
| 4922 | {
|
Adam Sawicki | 740b08f | 2018-08-27 13:42:07 +0200 | [diff] [blame] | 4923 | printf("%s #%u\n", testDescription, (uint32_t)repeat);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 4924 |
|
| 4925 | PoolTestResult result{};
|
| 4926 | g_MemoryAliasingWarningEnabled = false;
|
| 4927 | TestPool_Benchmark(result, config);
|
| 4928 | g_MemoryAliasingWarningEnabled = true;
|
| 4929 | WritePoolTestResult(file, CODE_DESCRIPTION, testDescription, config, result);
|
| 4930 | }
|
| 4931 | }
|
| 4932 | }
|
| 4933 | }
|
| 4934 | }
|
| 4935 | }
|
| 4936 | }
|
| 4937 |
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4938 | static void BasicTestBuddyAllocator()
|
| 4939 | {
|
| 4940 | wprintf(L"Basic test buddy allocator\n");
|
| 4941 |
|
| 4942 | RandomNumberGenerator rand{76543};
|
| 4943 |
|
| 4944 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 4945 | sampleBufCreateInfo.size = 1024; // Whatever.
|
| 4946 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 4947 |
|
| 4948 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 4949 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 4950 |
|
| 4951 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 4952 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4953 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4954 |
|
Adam Sawicki | d6e6d6b | 2018-09-21 14:07:02 +0200 | [diff] [blame] | 4955 | // Deliberately adding 1023 to test usable size smaller than memory block size.
|
| 4956 | poolCreateInfo.blockSize = 1024 * 1024 + 1023;
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4957 | poolCreateInfo.flags = VMA_POOL_CREATE_BUDDY_ALGORITHM_BIT;
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 4958 | //poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4959 |
|
| 4960 | VmaPool pool = nullptr;
|
| 4961 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4962 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4963 |
|
| 4964 | VkBufferCreateInfo bufCreateInfo = sampleBufCreateInfo;
|
| 4965 |
|
| 4966 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 4967 | allocCreateInfo.pool = pool;
|
| 4968 |
|
| 4969 | std::vector<BufferInfo> bufInfo;
|
| 4970 | BufferInfo newBufInfo;
|
| 4971 | VmaAllocationInfo allocInfo;
|
| 4972 |
|
| 4973 | bufCreateInfo.size = 1024 * 256;
|
| 4974 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 4975 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4976 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4977 | bufInfo.push_back(newBufInfo);
|
| 4978 |
|
| 4979 | bufCreateInfo.size = 1024 * 512;
|
| 4980 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 4981 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4982 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4983 | bufInfo.push_back(newBufInfo);
|
| 4984 |
|
| 4985 | bufCreateInfo.size = 1024 * 128;
|
| 4986 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 4987 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4988 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4989 | bufInfo.push_back(newBufInfo);
|
Adam Sawicki | a01d458 | 2018-09-21 14:22:35 +0200 | [diff] [blame] | 4990 |
|
| 4991 | // Test very small allocation, smaller than minimum node size.
|
| 4992 | bufCreateInfo.size = 1;
|
| 4993 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 4994 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 4995 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | a01d458 | 2018-09-21 14:22:35 +0200 | [diff] [blame] | 4996 | bufInfo.push_back(newBufInfo);
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 4997 |
|
Adam Sawicki | 9933c5c | 2018-09-21 14:57:24 +0200 | [diff] [blame] | 4998 | // Test some small allocation with alignment requirement.
|
| 4999 | {
|
| 5000 | VkMemoryRequirements memReq;
|
| 5001 | memReq.alignment = 256;
|
| 5002 | memReq.memoryTypeBits = UINT32_MAX;
|
| 5003 | memReq.size = 32;
|
| 5004 |
|
| 5005 | newBufInfo.Buffer = VK_NULL_HANDLE;
|
| 5006 | res = vmaAllocateMemory(g_hAllocator, &memReq, &allocCreateInfo,
|
| 5007 | &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 5008 | TEST(res == VK_SUCCESS);
|
| 5009 | TEST(allocInfo.offset % memReq.alignment == 0);
|
Adam Sawicki | 9933c5c | 2018-09-21 14:57:24 +0200 | [diff] [blame] | 5010 | bufInfo.push_back(newBufInfo);
|
| 5011 | }
|
| 5012 |
|
| 5013 | //SaveAllocatorStatsToFile(L"TEST.json");
|
| 5014 |
|
Adam Sawicki | 21017c6 | 2018-09-07 15:26:59 +0200 | [diff] [blame] | 5015 | VmaPoolStats stats = {};
|
| 5016 | vmaGetPoolStats(g_hAllocator, pool, &stats);
|
| 5017 | int DBG = 0; // Set breakpoint here to inspect `stats`.
|
| 5018 |
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 5019 | // Allocate enough new buffers to surely fall into second block.
|
| 5020 | for(uint32_t i = 0; i < 32; ++i)
|
| 5021 | {
|
| 5022 | bufCreateInfo.size = 1024 * (rand.Generate() % 32 + 1);
|
| 5023 | res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo,
|
| 5024 | &newBufInfo.Buffer, &newBufInfo.Allocation, &allocInfo);
|
Adam Sawicki | b8d34d5 | 2018-10-03 17:41:20 +0200 | [diff] [blame] | 5025 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 5026 | bufInfo.push_back(newBufInfo);
|
| 5027 | }
|
| 5028 |
|
| 5029 | SaveAllocatorStatsToFile(L"BuddyTest01.json");
|
| 5030 |
|
Adam Sawicki | a83793a | 2018-09-03 13:40:42 +0200 | [diff] [blame] | 5031 | // Destroy the buffers in random order.
|
| 5032 | while(!bufInfo.empty())
|
| 5033 | {
|
| 5034 | const size_t indexToDestroy = rand.Generate() % bufInfo.size();
|
| 5035 | const BufferInfo& currBufInfo = bufInfo[indexToDestroy];
|
| 5036 | vmaDestroyBuffer(g_hAllocator, currBufInfo.Buffer, currBufInfo.Allocation);
|
| 5037 | bufInfo.erase(bufInfo.begin() + indexToDestroy);
|
| 5038 | }
|
| 5039 |
|
| 5040 | vmaDestroyPool(g_hAllocator, pool);
|
| 5041 | }
|
| 5042 |
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5043 | static void BasicTestAllocatePages()
|
| 5044 | {
|
| 5045 | wprintf(L"Basic test allocate pages\n");
|
| 5046 |
|
| 5047 | RandomNumberGenerator rand{765461};
|
| 5048 |
|
| 5049 | VkBufferCreateInfo sampleBufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 5050 | sampleBufCreateInfo.size = 1024; // Whatever.
|
| 5051 | sampleBufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 5052 |
|
| 5053 | VmaAllocationCreateInfo sampleAllocCreateInfo = {};
|
| 5054 | sampleAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 5055 |
|
| 5056 | VmaPoolCreateInfo poolCreateInfo = {};
|
| 5057 | VkResult res = vmaFindMemoryTypeIndexForBufferInfo(g_hAllocator, &sampleBufCreateInfo, &sampleAllocCreateInfo, &poolCreateInfo.memoryTypeIndex);
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5058 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5059 |
|
| 5060 | // 1 block of 1 MB.
|
| 5061 | poolCreateInfo.blockSize = 1024 * 1024;
|
| 5062 | poolCreateInfo.minBlockCount = poolCreateInfo.maxBlockCount = 1;
|
| 5063 |
|
| 5064 | // Create pool.
|
| 5065 | VmaPool pool = nullptr;
|
| 5066 | res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool);
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5067 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5068 |
|
| 5069 | // Make 100 allocations of 4 KB - they should fit into the pool.
|
| 5070 | VkMemoryRequirements memReq;
|
| 5071 | memReq.memoryTypeBits = UINT32_MAX;
|
| 5072 | memReq.alignment = 4 * 1024;
|
| 5073 | memReq.size = 4 * 1024;
|
| 5074 |
|
| 5075 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 5076 | allocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
| 5077 | allocCreateInfo.pool = pool;
|
| 5078 |
|
| 5079 | constexpr uint32_t allocCount = 100;
|
| 5080 |
|
| 5081 | std::vector<VmaAllocation> alloc{allocCount};
|
| 5082 | std::vector<VmaAllocationInfo> allocInfo{allocCount};
|
| 5083 | res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), allocInfo.data());
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5084 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5085 | for(uint32_t i = 0; i < allocCount; ++i)
|
| 5086 | {
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5087 | TEST(alloc[i] != VK_NULL_HANDLE &&
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5088 | allocInfo[i].pMappedData != nullptr &&
|
| 5089 | allocInfo[i].deviceMemory == allocInfo[0].deviceMemory &&
|
| 5090 | allocInfo[i].memoryType == allocInfo[0].memoryType);
|
| 5091 | }
|
| 5092 |
|
| 5093 | // Free the allocations.
|
| 5094 | vmaFreeMemoryPages(g_hAllocator, allocCount, alloc.data());
|
| 5095 | std::fill(alloc.begin(), alloc.end(), nullptr);
|
| 5096 | std::fill(allocInfo.begin(), allocInfo.end(), VmaAllocationInfo{});
|
| 5097 |
|
| 5098 | // Try to make 100 allocations of 100 KB. This call should fail due to not enough memory.
|
| 5099 | // Also test optional allocationInfo = null.
|
| 5100 | memReq.size = 100 * 1024;
|
| 5101 | res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), nullptr);
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5102 | TEST(res != VK_SUCCESS);
|
| 5103 | TEST(std::find_if(alloc.begin(), alloc.end(), [](VmaAllocation alloc){ return alloc != VK_NULL_HANDLE; }) == alloc.end());
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5104 |
|
| 5105 | // Make 100 allocations of 4 KB, but with required alignment of 128 KB. This should also fail.
|
| 5106 | memReq.size = 4 * 1024;
|
| 5107 | memReq.alignment = 128 * 1024;
|
| 5108 | res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &allocCreateInfo, allocCount, alloc.data(), allocInfo.data());
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5109 | TEST(res != VK_SUCCESS);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5110 |
|
| 5111 | // Make 100 dedicated allocations of 4 KB.
|
| 5112 | memReq.alignment = 4 * 1024;
|
| 5113 | memReq.size = 4 * 1024;
|
| 5114 |
|
| 5115 | VmaAllocationCreateInfo dedicatedAllocCreateInfo = {};
|
| 5116 | dedicatedAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 5117 | dedicatedAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT | VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT;
|
| 5118 | res = vmaAllocateMemoryPages(g_hAllocator, &memReq, &dedicatedAllocCreateInfo, allocCount, alloc.data(), allocInfo.data());
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5119 | TEST(res == VK_SUCCESS);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5120 | for(uint32_t i = 0; i < allocCount; ++i)
|
| 5121 | {
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5122 | TEST(alloc[i] != VK_NULL_HANDLE &&
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5123 | allocInfo[i].pMappedData != nullptr &&
|
| 5124 | allocInfo[i].memoryType == allocInfo[0].memoryType &&
|
| 5125 | allocInfo[i].offset == 0);
|
| 5126 | if(i > 0)
|
| 5127 | {
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 5128 | TEST(allocInfo[i].deviceMemory != allocInfo[0].deviceMemory);
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5129 | }
|
| 5130 | }
|
| 5131 |
|
| 5132 | // Free the allocations.
|
| 5133 | vmaFreeMemoryPages(g_hAllocator, allocCount, alloc.data());
|
| 5134 | std::fill(alloc.begin(), alloc.end(), nullptr);
|
| 5135 | std::fill(allocInfo.begin(), allocInfo.end(), VmaAllocationInfo{});
|
| 5136 |
|
| 5137 | vmaDestroyPool(g_hAllocator, pool);
|
| 5138 | }
|
| 5139 |
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 5140 | // Test the testing environment.
|
| 5141 | static void TestGpuData()
|
| 5142 | {
|
| 5143 | RandomNumberGenerator rand = { 53434 };
|
| 5144 |
|
| 5145 | std::vector<AllocInfo> allocInfo;
|
| 5146 |
|
| 5147 | for(size_t i = 0; i < 100; ++i)
|
| 5148 | {
|
| 5149 | AllocInfo info = {};
|
| 5150 |
|
| 5151 | info.m_BufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
| 5152 | info.m_BufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT |
|
| 5153 | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
|
| 5154 | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
| 5155 | info.m_BufferInfo.size = 1024 * 1024 * (rand.Generate() % 9 + 1);
|
| 5156 |
|
| 5157 | VmaAllocationCreateInfo allocCreateInfo = {};
|
| 5158 | allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 5159 |
|
| 5160 | VkResult res = vmaCreateBuffer(g_hAllocator, &info.m_BufferInfo, &allocCreateInfo, &info.m_Buffer, &info.m_Allocation, nullptr);
|
| 5161 | TEST(res == VK_SUCCESS);
|
| 5162 |
|
| 5163 | info.m_StartValue = rand.Generate();
|
| 5164 |
|
| 5165 | allocInfo.push_back(std::move(info));
|
| 5166 | }
|
| 5167 |
|
| 5168 | UploadGpuData(allocInfo.data(), allocInfo.size());
|
| 5169 |
|
| 5170 | ValidateGpuData(allocInfo.data(), allocInfo.size());
|
| 5171 |
|
| 5172 | DestroyAllAllocations(allocInfo);
|
| 5173 | }
|
| 5174 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5175 | void Test()
|
| 5176 | {
|
| 5177 | wprintf(L"TESTING:\n");
|
| 5178 |
|
Adam Sawicki | 48b8a33 | 2019-11-02 15:24:33 +0100 | [diff] [blame] | 5179 | if(false)
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 5180 | {
|
Adam Sawicki | 1a8424f | 2018-12-13 11:01:16 +0100 | [diff] [blame] | 5181 | ////////////////////////////////////////////////////////////////////////////////
|
| 5182 | // Temporarily insert custom tests here:
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 5183 | return;
|
| 5184 | }
|
| 5185 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5186 | // # Simple tests
|
| 5187 |
|
| 5188 | TestBasics();
|
Adam Sawicki | f297534 | 2018-10-16 13:49:02 +0200 | [diff] [blame] | 5189 | //TestGpuData(); // Not calling this because it's just testing the testing environment.
|
Adam Sawicki | 212a4a6 | 2018-06-14 15:44:45 +0200 | [diff] [blame] | 5190 | #if VMA_DEBUG_MARGIN
|
| 5191 | TestDebugMargin();
|
| 5192 | #else
|
| 5193 | TestPool_SameSize();
|
| 5194 | TestHeapSizeLimit();
|
| 5195 | #endif
|
Adam Sawicki | e44c626 | 2018-06-15 14:30:39 +0200 | [diff] [blame] | 5196 | #if VMA_DEBUG_INITIALIZE_ALLOCATIONS
|
| 5197 | TestAllocationsInitialization();
|
| 5198 | #endif
|
Adam Sawicki | 40ffe98 | 2019-10-11 15:56:02 +0200 | [diff] [blame] | 5199 | TestBudget();
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5200 | TestMapping();
|
Adam Sawicki | daa6a55 | 2019-06-25 15:26:37 +0200 | [diff] [blame] | 5201 | TestDeviceLocalMapped();
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5202 | TestMappingMultithreaded();
|
Adam Sawicki | 0876c0d | 2018-06-20 15:18:11 +0200 | [diff] [blame] | 5203 | TestLinearAllocator();
|
Adam Sawicki | 8cfe05f | 2018-08-22 16:48:17 +0200 | [diff] [blame] | 5204 | ManuallyTestLinearAllocator();
|
Adam Sawicki | 70a683e | 2018-08-24 15:36:32 +0200 | [diff] [blame] | 5205 | TestLinearAllocatorMultiBlock();
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 5206 |
|
Adam Sawicki | 4338f66 | 2018-09-07 14:12:37 +0200 | [diff] [blame] | 5207 | BasicTestBuddyAllocator();
|
Adam Sawicki | 2e4d3ef | 2018-10-03 15:48:17 +0200 | [diff] [blame] | 5208 | BasicTestAllocatePages();
|
Adam Sawicki | 4338f66 | 2018-09-07 14:12:37 +0200 | [diff] [blame] | 5209 |
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 5210 | {
|
| 5211 | FILE* file;
|
Adam Sawicki | c6432d1 | 2018-09-21 16:44:16 +0200 | [diff] [blame] | 5212 | fopen_s(&file, "Algorithms.csv", "w");
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 5213 | assert(file != NULL);
|
Adam Sawicki | 8092715 | 2018-09-07 17:27:23 +0200 | [diff] [blame] | 5214 | BenchmarkAlgorithms(file);
|
Adam Sawicki | 33d2ce7 | 2018-08-27 13:59:13 +0200 | [diff] [blame] | 5215 | fclose(file);
|
| 5216 | }
|
| 5217 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5218 | TestDefragmentationSimple();
|
| 5219 | TestDefragmentationFull();
|
Adam Sawicki | 52076eb | 2018-11-22 16:14:50 +0100 | [diff] [blame] | 5220 | TestDefragmentationWholePool();
|
Adam Sawicki | 9a4f508 | 2018-11-23 17:26:05 +0100 | [diff] [blame] | 5221 | TestDefragmentationGpu();
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 5222 |
|
| 5223 | // # Detailed tests
|
| 5224 | FILE* file;
|
| 5225 | fopen_s(&file, "Results.csv", "w");
|
| 5226 | assert(file != NULL);
|
| 5227 |
|
| 5228 | WriteMainTestResultHeader(file);
|
| 5229 | PerformMainTests(file);
|
| 5230 | //PerformCustomMainTest(file);
|
| 5231 |
|
| 5232 | WritePoolTestResultHeader(file);
|
| 5233 | PerformPoolTests(file);
|
| 5234 | //PerformCustomPoolTest(file);
|
| 5235 |
|
| 5236 | fclose(file);
|
| 5237 |
|
| 5238 | wprintf(L"Done.\n");
|
| 5239 | }
|
| 5240 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 5241 | #endif // #ifdef _WIN32
|