diff --git a/third_party/vkmemalloc/.gitignore b/third_party/vkmemalloc/.gitignore index 98d315649a..88ea8d1152 100644 --- a/third_party/vkmemalloc/.gitignore +++ b/third_party/vkmemalloc/.gitignore @@ -1,13 +1,2 @@ -/bin/* +build/ -/build/* -!/build/src/ -/build/src/* -!/build/src/Release/ -/build/src/Release/* -!/build/src/Release/VmaSample.exe -!/build/src/VmaReplay/ -/build/src/VmaReplay/* -!/build/src/VmaReplay/Release/ -/build/src/VmaReplay/Release/* -!/build/src/VmaReplay/Release/VmaReplay.exe diff --git a/third_party/vkmemalloc/.travis.yml b/third_party/vkmemalloc/.travis.yml index 342a73c463..96a178a3c0 100644 --- a/third_party/vkmemalloc/.travis.yml +++ b/third_party/vkmemalloc/.travis.yml @@ -1,7 +1,7 @@ language: cpp sudo: required os: linux -dist: bionic +dist: trusty branches: only: @@ -12,22 +12,24 @@ compiler: - gcc before_script: - - sudo apt-get install - - eval "${MATRIX_EVAL}" + - sudo apt-get install install: + - wget https://github.com/premake/premake-core/releases/download/v5.0.0-alpha11/premake-5.0.0-alpha11-linux.tar.gz?Human=true -O premake-5.0.0-alpha11-linux.tar.gz + - tar -xzvf premake-5.0.0-alpha11-linux.tar.gz - sudo apt-get -qq update - sudo apt-get install -y libassimp-dev libglm-dev graphviz libxcb-dri3-0 libxcb-present0 libpciaccess0 cmake libpng-dev libxcb-dri3-dev libx11-dev libx11-xcb-dev libmirclient-dev libwayland-dev libxrandr-dev - - export VK_VERSION=1.2.131.1 - - wget -O vulkansdk-linux-x86_64-$VK_VERSION.tar.gz https://sdk.lunarg.com/sdk/download/$VK_VERSION/linux/vulkansdk-linux-x86_64-$VK_VERSION.tar.gz?Human=true + - export VK_VERSION=1.1.82.1 + - wget -O vulkansdk-linux-x86_64-$VK_VERSION.tar.gz https://sdk.lunarg.com/sdk/download/$VK_VERSION/linux/vulkansdk-linux-x86_64-$VK_VERSION.tar.gz - tar zxf vulkansdk-linux-x86_64-$VK_VERSION.tar.gz - export VULKAN_SDK=$TRAVIS_BUILD_DIR/$VK_VERSION/x86_64 script: - - mkdir build - - cd build - - cmake .. - - make + - cd premake + - ../premake5 gmake + - cd ../build + - make config=debug_linux-x64 + - cd .. notifications: email: diff --git a/third_party/vkmemalloc/CMakeLists.txt b/third_party/vkmemalloc/CMakeLists.txt deleted file mode 100644 index 3c323ceb51..0000000000 --- a/third_party/vkmemalloc/CMakeLists.txt +++ /dev/null @@ -1,33 +0,0 @@ -cmake_minimum_required(VERSION 3.9) - -project(VulkanMemoryAllocator) - -find_package(Vulkan REQUIRED) - -# VulkanMemoryAllocator contains an sample application and VmaReplay which are not build by default -option(VMA_BUILD_SAMPLE "Build VulkanMemoryAllocator sample application" OFF) -option(VMA_BUILD_SAMPLE_SHADERS "Build VulkanMemoryAllocator sample application's shaders" OFF) -option(VMA_BUILD_REPLAY "Build VulkanMemoryAllocator replay application" OFF) - -message(STATUS "VMA_BUILD_SAMPLE = ${VMA_BUILD_SAMPLE}") -message(STATUS "VMA_BUILD_SAMPLE_SHADERS = ${VMA_BUILD_SAMPLE_SHADERS}") -message(STATUS "VMA_BUILD_REPLAY = ${VMA_BUILD_REPLAY}") - -option(VMA_RECORDING_ENABLED "Enable VMA memory recording for debugging" OFF) -option(VMA_USE_STL_CONTAINERS "Use C++ STL containers instead of VMA's containers" OFF) -option(VMA_STATIC_VULKAN_FUNCTIONS "Link statically with Vulkan API" OFF) -option(VMA_DYNAMIC_VULKAN_FUNCTIONS "Fetch pointers to Vulkan functions internally (no static linking)" ON) -option(VMA_DEBUG_ALWAYS_DEDICATED_MEMORY "Every allocation will have its own memory block" OFF) -option(VMA_DEBUG_INITIALIZE_ALLOCATIONS "Automatically fill new allocations and destroyed allocations with some bit pattern" OFF) -option(VMA_DEBUG_GLOBAL_MUTEX "Enable single mutex protecting all entry calls to the library" OFF) -option(VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT "Never exceed VkPhysicalDeviceLimits::maxMemoryAllocationCount and return error" OFF) - -message(STATUS "VMA_RECORDING_ENABLED = ${VMA_RECORDING_ENABLED}") -message(STATUS "VMA_USE_STL_CONTAINERS = ${VMA_USE_STL_CONTAINERS}") -message(STATUS "VMA_DYNAMIC_VULKAN_FUNCTIONS = ${VMA_DYNAMIC_VULKAN_FUNCTIONS}") -message(STATUS "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY = ${VMA_DEBUG_ALWAYS_DEDICATED_MEMORY}") -message(STATUS "VMA_DEBUG_INITIALIZE_ALLOCATIONS = ${VMA_DEBUG_INITIALIZE_ALLOCATIONS}") -message(STATUS "VMA_DEBUG_GLOBAL_MUTEX = ${VMA_DEBUG_GLOBAL_MUTEX}") -message(STATUS "VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT = ${VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT}") - -add_subdirectory(src) diff --git a/third_party/vkmemalloc/LICENSE.txt b/third_party/vkmemalloc/LICENSE.txt index 71e824f805..67b0d01dcc 100644 --- a/third_party/vkmemalloc/LICENSE.txt +++ b/third_party/vkmemalloc/LICENSE.txt @@ -1,4 +1,4 @@ -Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/README.md b/third_party/vkmemalloc/README.md index b1e2dba2c4..030b517592 100644 --- a/third_party/vkmemalloc/README.md +++ b/third_party/vkmemalloc/README.md @@ -2,7 +2,7 @@ Easy to integrate Vulkan memory allocation library. -**Documentation:** See [Vulkan Memory Allocator](https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/) (generated from Doxygen-style comments in [include/vk_mem_alloc.h](include/vk_mem_alloc.h)) +**Documentation:** See [Vulkan Memory Allocator](https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/) (generated from Doxygen-style comments in [src/vk_mem_alloc.h](src/vk_mem_alloc.h)) **License:** MIT. See [LICENSE.txt](LICENSE.txt) @@ -13,7 +13,7 @@ Easy to integrate Vulkan memory allocation library. **Build status:** - Windows: [![Build status](https://ci.appveyor.com/api/projects/status/4vlcrb0emkaio2pn/branch/master?svg=true)](https://ci.appveyor.com/project/adam-sawicki-amd/vulkanmemoryallocator/branch/master) -- Linux: [![Build Status](https://travis-ci.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.svg?branch=master)](https://travis-ci.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator) +- Linux: [![Build Status](https://travis-ci.org/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator.svg?branch=master)](https://travis-ci.org/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator) # Problem @@ -37,21 +37,17 @@ This library can help game developers to manage memory allocations and resource Additional features: - Well-documented - description of all functions and structures provided, along with chapters that contain general description and example code. -- Thread-safety: Library is designed to be used in multithreaded code. Access to a single device memory block referred by different buffers and textures (binding, mapping) is synchronized internally. +- Thread-safety: Library is designed to be used by multithreaded code. - Configuration: Fill optional members of CreateInfo structure to provide custom CPU memory allocator, pointers to Vulkan functions and other parameters. - Customization: Predefine appropriate macros to provide your own implementation of all external facilities used by the library, from assert, mutex, and atomic, to vector and linked list. - Support for memory mapping, reference-counted internally. Support for persistently mapped memory: Just allocate with appropriate flag and you get access to mapped pointer. +- Support for memory budget. VK_EXT_memory_budget extension is used internally if available to query for current usage and budget. If not available, it falls back to an estimation based on memory heap sizes. - Support for non-coherent memory. Functions that flush/invalidate memory. `nonCoherentAtomSize` is respected automatically. -- Support for resource aliasing (overlap). - Support for sparse binding and sparse residency: Convenience functions that allocate or free multiple memory pages at once. - Custom memory pools: Create a pool with desired parameters (e.g. fixed or limited maximum size) and allocate memory out of it. - Linear allocator: Create a pool with linear algorithm and use it for much faster allocations and deallocations in free-at-once, stack, double stack, or ring buffer fashion. -- Support for Vulkan 1.0, 1.1, 1.2. -- Support for extensions (and equivalent functionality included in new Vulkan versions): - - VK_EXT_memory_budget: Used internally if available to query for current usage and budget. If not available, it falls back to an estimation based on memory heap sizes. - - VK_KHR_dedicated_allocation: Just enable it and it will be used automatically by the library. - - VK_AMD_device_coherent_memory - - VK_KHR_buffer_device_address +- Support for Vulkan 1.0 as well as 1.1. +- Support for VK_KHR_dedicated_allocation extension: Just enable it and it will be used automatically by the library. - Defragmentation of GPU and CPU memory: Let the library move data around to free some memory blocks and make your allocations better compacted. - Lost allocations: Allocate memory with appropriate flags and let the library remove allocations that are not used for many frames to make room for new ones. - Statistics: Obtain detailed statistics about the amount of memory used, unused, number of allocated blocks, number of allocations etc. - globally, per memory heap, and per memory type. @@ -94,57 +90,15 @@ With this one function call: `VmaAllocation` is an object that represents memory assigned to this buffer. It can be queried for parameters like Vulkan memory handle and offset. -# How to build - -On Windows it is recommended to use [CMake UI](https://cmake.org/runningcmake/). Alternatively you can generate a Visual Studio project map using CMake in command line: `cmake -B./build/ -DCMAKE_BUILD_TYPE=Debug -G "Visual Studio 16 2019" -A x64 ./` - -On Linux: - -``` -mkdir build -cd build -cmake .. -make -``` - -The following targets are available - -| Target | Description | CMake option | Default setting | -| ------------- | ------------- | ------------- | ------------- | -| VmaSample | VMA sample application | `VMA_BUILD_SAMPLE` | `OFF` | -| VmaBuildSampleShaders | Shaders for VmaSample | `VMA_BUILD_SAMPLE_SHADERS` | `OFF` | -| VmaReplay | Replay tool for VMA .csv trace files | `VMA_BUILD_REPLAY` | `OFF` | - -Please note that while VulkanMemoryAllocator library is supported on other platforms besides Windows, VmaSample and VmaReplay are not. - -These CMake options are available - -| CMake option | Description | Default setting | -| ------------- | ------------- | ------------- | -| `VMA_RECORDING_ENABLED` | Enable VMA memory recording for debugging | `OFF` | -| `VMA_USE_STL_CONTAINERS` | Use C++ STL containers instead of VMA's containers | `OFF` | -| `VMA_STATIC_VULKAN_FUNCTIONS` | Link statically with Vulkan API | `OFF` | -| `VMA_DYNAMIC_VULKAN_FUNCTIONS` | Fetch pointers to Vulkan functions internally (no static linking) | `ON` | -| `VMA_DEBUG_ALWAYS_DEDICATED_MEMORY` | Every allocation will have its own memory block | `OFF` | -| `VMA_DEBUG_INITIALIZE_ALLOCATIONS` | Automatically fill new allocations and destroyed allocations with some bit pattern | `OFF` | -| `VMA_DEBUG_GLOBAL_MUTEX` | Enable single mutex protecting all entry calls to the library | `OFF` | -| `VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT` | Never exceed [VkPhysicalDeviceLimits::maxMemoryAllocationCount](https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#limits-maxMemoryAllocationCount) and return error | `OFF` | - -# Binaries - -The release comes with precompiled binary executables for "VulkanSample" application which contains test suite and "VmaReplay" tool. They are compiled using Visual Studio 2019, so they require appropriate libraries to work, including "MSVCP140.dll", "VCRUNTIME140.dll", "VCRUNTIME140_1.dll". If their launch fails with error message telling about those files missing, please download and install [Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019](https://support.microsoft.com/en-us/help/2977003/the-latest-supported-visual-c-downloads), "x64" version. - # Read more See **[Documentation](https://gpuopen-librariesandsdks.github.io/VulkanMemoryAllocator/html/)**. # Software using this library -- **[Detroit: Become Human](https://gpuopen.com/learn/porting-detroit-3/)** - **[Vulkan Samples](https://github.com/LunarG/VulkanSamples)** - official Khronos Vulkan samples. License: Apache-style. - **[Anvil](https://github.com/GPUOpen-LibrariesAndSDKs/Anvil)** - cross-platform framework for Vulkan. License: MIT. - **[Filament](https://github.com/google/filament)** - physically based rendering engine for Android, Windows, Linux and macOS, from Google. Apache License 2.0. -- **[Atypical Games - proprietary game engine](https://developer.samsung.com/galaxy-gamedev/gamedev-blog/infinitejet.html)** - **[Flax Engine](https://flaxengine.com/)** - **[Lightweight Java Game Library (LWJGL)](https://www.lwjgl.org/)** - includes binding of the library for Java. License: BSD. - **[PowerVR SDK](https://github.com/powervr-graphics/Native_SDK)** - C++ cross-platform 3D graphics SDK, from Imagination. License: MIT. @@ -154,7 +108,6 @@ See **[Documentation](https://gpuopen-librariesandsdks.github.io/VulkanMemoryAll - **[vkDOOM3](https://github.com/DustinHLand/vkDOOM3)** - Vulkan port of GPL DOOM 3 BFG Edition. License: GNU GPL. - **[vkQuake2](https://github.com/kondrak/vkQuake2)** - vanilla Quake 2 with Vulkan support. License: GNU GPL. - **[Vulkan Best Practice for Mobile Developers](https://github.com/ARM-software/vulkan_best_practice_for_mobile_developers)** from ARM. License: MIT. -- **[RPCS3](https://github.com/RPCS3/rpcs3)** - PlayStation 3 emulator/debugger. License: GNU GPLv2. [Many other projects on GitHub](https://github.com/search?q=AMD_VULKAN_MEMORY_ALLOCATOR_H&type=Code) and some game development studios that use Vulkan in their games. @@ -165,6 +118,5 @@ See **[Documentation](https://gpuopen-librariesandsdks.github.io/VulkanMemoryAll - **[VulkanMemoryAllocator-Hpp](https://github.com/malte-v/VulkanMemoryAllocator-Hpp)** - C++ binding for this library. License: CC0-1.0. - **[PyVMA](https://github.com/realitix/pyvma)** - Python wrapper for this library. Author: Jean-Sébastien B. (@realitix). License: Apache 2.0. - **[vk-mem](https://github.com/gwihlidal/vk-mem-rs)** - Rust binding for this library. Author: Graham Wihlidal. License: Apache 2.0 or MIT. -- **[Haskell bindings](https://hackage.haskell.org/package/VulkanMemoryAllocator)**, **[github](https://github.com/expipiplus1/vulkan/tree/master/VulkanMemoryAllocator)** - Haskell bindings for this library. Author: Joe Hermaszewski (@expipiplus1). License BSD-3-Clause. - **[vma_sample_sdl](https://github.com/rextimmy/vma_sample_sdl)** - SDL port of the sample app of this library (with the goal of running it on multiple platforms, including MacOS). Author: @rextimmy. License: MIT. - **[vulkan-malloc](https://github.com/dylanede/vulkan-malloc)** - Vulkan memory allocation library for Rust. Based on version 1 of this library. Author: Dylan Ede (@dylanede). License: MIT / Apache 2.0. diff --git a/third_party/vkmemalloc/src/.editorconfig b/third_party/vkmemalloc/src/.editorconfig deleted file mode 100644 index 8abf0efc44..0000000000 --- a/third_party/vkmemalloc/src/.editorconfig +++ /dev/null @@ -1,5 +0,0 @@ -root = true - -[**.{cpp,h}] -indent_style = space -indent_size = 4 diff --git a/third_party/vkmemalloc/src/CMakeLists.txt b/third_party/vkmemalloc/src/CMakeLists.txt deleted file mode 100644 index eccf82e80b..0000000000 --- a/third_party/vkmemalloc/src/CMakeLists.txt +++ /dev/null @@ -1,92 +0,0 @@ -set(VMA_LIBRARY_SOURCE_FILES - VmaUsage.cpp -) - -add_library(VulkanMemoryAllocator ${VMA_LIBRARY_SOURCE_FILES}) - -set_target_properties( - VulkanMemoryAllocator PROPERTIES - - CXX_EXTENSIONS OFF - # Use C++14 - CXX_STANDARD 14 - CXX_STANDARD_REQUIRED ON -) - -target_include_directories(VulkanMemoryAllocator PUBLIC ${PROJECT_SOURCE_DIR}/include) - -# Only link to Vulkan if static linking is used -if (NOT ${VMA_DYNAMIC_VULKAN_FUNCTIONS}) - target_link_libraries(VulkanMemoryAllocator PUBLIC Vulkan::Vulkan) -endif() - -target_compile_definitions( - VulkanMemoryAllocator - - PUBLIC - VMA_USE_STL_CONTAINERS=$ - VMA_DYNAMIC_VULKAN_FUNCTIONS=$ - VMA_DEBUG_ALWAYS_DEDICATED_MEMORY=$ - VMA_DEBUG_INITIALIZE_ALLOCATIONS=$ - VMA_DEBUG_GLOBAL_MUTEX=$ - VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT=$ - VMA_RECORDING_ENABLED=$ -) - -if (VMA_BUILD_SAMPLE) - if(WIN32) - set(VMA_SAMPLE_SOURCE_FILES - Common.cpp - SparseBindingTest.cpp - Tests.cpp - VulkanSample.cpp - ) - - add_executable(VmaSample ${VMA_SAMPLE_SOURCE_FILES}) - - # Visual Studio specific settings - if(${CMAKE_GENERATOR} MATCHES "Visual Studio.*") - # Use Unicode instead of multibyte set - add_compile_definitions(UNICODE _UNICODE) - - # Set VmaSample as startup project - set_property(DIRECTORY ${PROJECT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT "VmaSample") - - # Enable multithreaded compiling - target_compile_options(VmaSample PRIVATE "/MP") - - # Set working directory for Visual Studio debugger - set_target_properties( - VmaSample - PROPERTIES VS_DEBUGGER_WORKING_DIRECTORY "${PROJECT_SOURCE_DIR}/bin" - ) - endif() - - set_target_properties( - VmaSample PROPERTIES - - CXX_EXTENSIONS OFF - # Use C++14 - CXX_STANDARD 14 - CXX_STANDARD_REQUIRED ON - ) - - target_link_libraries( - VmaSample - - PRIVATE - VulkanMemoryAllocator - Vulkan::Vulkan - ) - else() - message(STATUS "VmaSample application is not supported to Linux") - endif() -endif() - -if(VMA_BUILD_SAMPLE_SHADERS) - add_subdirectory(Shaders) -endif() - -if(VMA_BUILD_REPLAY) - add_subdirectory(VmaReplay) -endif() diff --git a/third_party/vkmemalloc/src/Common.cpp b/third_party/vkmemalloc/src/Common.cpp index 2dcbb680cf..790cdda38a 100644 --- a/third_party/vkmemalloc/src/Common.cpp +++ b/third_party/vkmemalloc/src/Common.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -45,7 +45,7 @@ void SetConsoleColor(CONSOLE_COLOR color) switch(color) { case CONSOLE_COLOR::INFO: - attr = FOREGROUND_INTENSITY; + attr = FOREGROUND_INTENSITY;; break; case CONSOLE_COLOR::NORMAL: attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; @@ -177,152 +177,4 @@ void SaveFile(const wchar_t* filePath, const void* data, size_t dataSize) assert(0); } -std::wstring SizeToStr(size_t size) -{ - if(size == 0) - return L"0"; - wchar_t result[32]; - double size2 = (double)size; - if (size2 >= 1024.0*1024.0*1024.0*1024.0) - { - swprintf_s(result, L"%.2f TB", size2 / (1024.0*1024.0*1024.0*1024.0)); - } - else if (size2 >= 1024.0*1024.0*1024.0) - { - swprintf_s(result, L"%.2f GB", size2 / (1024.0*1024.0*1024.0)); - } - else if (size2 >= 1024.0*1024.0) - { - swprintf_s(result, L"%.2f MB", size2 / (1024.0*1024.0)); - } - else if (size2 >= 1024.0) - { - swprintf_s(result, L"%.2f KB", size2 / 1024.0); - } - else - swprintf_s(result, L"%llu B", size); - return result; -} - -bool ConvertCharsToUnicode(std::wstring *outStr, const std::string &s, unsigned codePage) -{ - if (s.empty()) - { - outStr->clear(); - return true; - } - - // Phase 1 - Get buffer size. - const int size = MultiByteToWideChar(codePage, 0, s.data(), (int)s.length(), NULL, 0); - if (size == 0) - { - outStr->clear(); - return false; - } - - // Phase 2 - Do conversion. - std::unique_ptr buf(new wchar_t[(size_t)size]); - int result = MultiByteToWideChar(codePage, 0, s.data(), (int)s.length(), buf.get(), size); - if (result == 0) - { - outStr->clear(); - return false; - } - - outStr->assign(buf.get(), (size_t)size); - return true; -} - -bool ConvertCharsToUnicode(std::wstring *outStr, const char *s, size_t sCharCount, unsigned codePage) -{ - if (sCharCount == 0) - { - outStr->clear(); - return true; - } - - assert(sCharCount <= (size_t)INT_MAX); - - // Phase 1 - Get buffer size. - int size = MultiByteToWideChar(codePage, 0, s, (int)sCharCount, NULL, 0); - if (size == 0) - { - outStr->clear(); - return false; - } - - // Phase 2 - Do conversion. - std::unique_ptr buf(new wchar_t[(size_t)size]); - int result = MultiByteToWideChar(codePage, 0, s, (int)sCharCount, buf.get(), size); - if (result == 0) - { - outStr->clear(); - return false; - } - - outStr->assign(buf.get(), (size_t)size); - return true; -} - -const wchar_t* PhysicalDeviceTypeToStr(VkPhysicalDeviceType type) -{ - // Skipping common prefix VK_PHYSICAL_DEVICE_TYPE_ - static const wchar_t* const VALUES[] = { - L"OTHER", - L"INTEGRATED_GPU", - L"DISCRETE_GPU", - L"VIRTUAL_GPU", - L"CPU", - }; - return (uint32_t)type < _countof(VALUES) ? VALUES[(uint32_t)type] : L""; -} - -const wchar_t* VendorIDToStr(uint32_t vendorID) -{ - switch(vendorID) - { - // Skipping common prefix VK_VENDOR_ID_ for these: - case 0x10001: return L"VIV"; - case 0x10002: return L"VSI"; - case 0x10003: return L"KAZAN"; - case 0x10004: return L"CODEPLAY"; - case 0x10005: return L"MESA"; - case 0x10006: return L"POCL"; - // Others... - case VENDOR_ID_AMD: return L"AMD"; - case VENDOR_ID_NVIDIA: return L"NVIDIA"; - case VENDOR_ID_INTEL: return L"Intel"; - case 0x1010: return L"ImgTec"; - case 0x13B5: return L"ARM"; - case 0x5143: return L"Qualcomm"; - } - return L""; -} - -#if VMA_VULKAN_VERSION >= 1002000 -const wchar_t* DriverIDToStr(VkDriverId driverID) -{ - // Skipping common prefix VK_DRIVER_ID_ - static const wchar_t* const VALUES[] = { - L"", - L"AMD_PROPRIETARY", - L"AMD_OPEN_SOURCE", - L"MESA_RADV", - L"NVIDIA_PROPRIETARY", - L"INTEL_PROPRIETARY_WINDOWS", - L"INTEL_OPEN_SOURCE_MESA", - L"IMAGINATION_PROPRIETARY", - L"QUALCOMM_PROPRIETARY", - L"ARM_PROPRIETARY", - L"GOOGLE_SWIFTSHADER", - L"GGP_PROPRIETARY", - L"BROADCOM_PROPRIETARY", - L"MESA_LLVMPIPE", - L"MOLTENVK", - }; - return (uint32_t)driverID < _countof(VALUES) ? VALUES[(uint32_t)driverID] : L""; -} -#endif // #if VMA_VULKAN_VERSION >= 1002000 - - #endif // #ifdef _WIN32 diff --git a/third_party/vkmemalloc/src/Common.h b/third_party/vkmemalloc/src/Common.h index a718234682..ec564d6b81 100644 --- a/third_party/vkmemalloc/src/Common.h +++ b/third_party/vkmemalloc/src/Common.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -48,30 +48,27 @@ typedef std::chrono::high_resolution_clock::time_point time_point; typedef std::chrono::high_resolution_clock::duration duration; -#define STRINGIZE(x) STRINGIZE2(x) -#define STRINGIZE2(x) #x -#define LINE_STRING STRINGIZE(__LINE__) -#define TEST(expr) do { if(!(expr)) { \ - assert(0 && #expr); \ - throw std::runtime_error(__FILE__ "(" LINE_STRING "): ( " #expr " ) == false"); \ - } } while(false) -#define ERR_GUARD_VULKAN(expr) do { if((expr) < 0) { \ - assert(0 && #expr); \ - throw std::runtime_error(__FILE__ "(" LINE_STRING "): VkResult( " #expr " ) < 0"); \ - } } while(false) +#ifdef _DEBUG + #define TEST(expr) do { \ + if(!(expr)) { \ + assert(0 && #expr); \ + } \ + } while(0) +#else + #define TEST(expr) do { \ + if(!(expr)) { \ + throw std::runtime_error("TEST FAILED: " #expr); \ + } \ + } while(0) +#endif -static const uint32_t VENDOR_ID_AMD = 0x1002; -static const uint32_t VENDOR_ID_NVIDIA = 0x10DE; -static const uint32_t VENDOR_ID_INTEL = 0x8086; +#define ERR_GUARD_VULKAN(expr) TEST((expr) >= 0) -extern VkInstance g_hVulkanInstance; extern VkPhysicalDevice g_hPhysicalDevice; extern VkDevice g_hDevice; extern VkInstance g_hVulkanInstance; extern VmaAllocator g_hAllocator; -extern bool VK_AMD_device_coherent_memory_enabled; - -void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo); +extern bool g_MemoryAliasingWarningEnabled; inline float ToFloatSeconds(duration d) { @@ -97,29 +94,6 @@ static inline T align_up(T val, T align) static const float PI = 3.14159265358979323846264338327950288419716939937510582f; -template -inline void PnextChainPushFront(MainT* mainStruct, NewT* newStruct) -{ - newStruct->pNext = mainStruct->pNext; - mainStruct->pNext = newStruct; -} -template -inline void PnextChainPushBack(MainT* mainStruct, NewT* newStruct) -{ - struct VkAnyStruct - { - VkStructureType sType; - void* pNext; - }; - VkAnyStruct* lastStruct = (VkAnyStruct*)mainStruct; - while(lastStruct->pNext != nullptr) - { - lastStruct = (VkAnyStruct*)lastStruct->pNext; - } - newStruct->pNext = nullptr; - lastStruct->pNext = newStruct; -} - struct vec3 { float x, y, z; @@ -322,18 +296,6 @@ void PrintErrorF(const wchar_t* format, ...); void SaveFile(const wchar_t* filePath, const void* data, size_t dataSize); -std::wstring SizeToStr(size_t size); -// As codePage use e.g. CP_ACP for native Windows 1-byte codepage or CP_UTF8. -bool ConvertCharsToUnicode(std::wstring *outStr, const std::string &s, unsigned codePage); -bool ConvertCharsToUnicode(std::wstring *outStr, const char *s, size_t sCharCount, unsigned codePage); - -const wchar_t* PhysicalDeviceTypeToStr(VkPhysicalDeviceType type); -const wchar_t* VendorIDToStr(uint32_t vendorID); - -#if VMA_VULKAN_VERSION >= 1002000 -const wchar_t* DriverIDToStr(VkDriverId driverID); -#endif - #endif // #ifdef _WIN32 #endif diff --git a/third_party/vkmemalloc/Doxyfile b/third_party/vkmemalloc/src/Doxyfile similarity index 90% rename from third_party/vkmemalloc/Doxyfile rename to third_party/vkmemalloc/src/Doxyfile index 0d50ff3335..3efbdb923e 100644 --- a/third_party/vkmemalloc/Doxyfile +++ b/third_party/vkmemalloc/src/Doxyfile @@ -1,4 +1,4 @@ -# Doxyfile 1.9.1 +# Doxyfile 1.8.16 # This file describes the settings to be used by the documentation system # doxygen (www.doxygen.org) for a project. @@ -58,7 +58,7 @@ PROJECT_LOGO = # entered, it will be relative to the location where doxygen was started. If # left blank the current directory will be used. -OUTPUT_DIRECTORY = docs +OUTPUT_DIRECTORY = ../docs # If the CREATE_SUBDIRS tag is set to YES then doxygen will create 4096 sub- # directories (in 2 levels) under the output directory of each output format and @@ -227,14 +227,6 @@ QT_AUTOBRIEF = NO MULTILINE_CPP_IS_BRIEF = NO -# By default Python docstrings are displayed as preformatted text and doxygen's -# special commands cannot be used. By setting PYTHON_DOCSTRING to NO the -# doxygen's special commands can be used and the contents of the docstring -# documentation blocks is shown as doxygen documentation. -# The default value is: YES. - -PYTHON_DOCSTRING = YES - # If the INHERIT_DOCS tag is set to YES then an undocumented member inherits the # documentation from any documented member that it re-implements. # The default value is: YES. @@ -271,6 +263,12 @@ TAB_SIZE = 4 ALIASES = +# This tag can be used to specify a number of word-keyword mappings (TCL only). +# A mapping has the form "name=value". For example adding "class=itcl::class" +# will allow you to use the command class in the itcl::class meaning. + +TCL_SUBST = + # Set the OPTIMIZE_OUTPUT_FOR_C tag to YES if your project consists of C sources # only. Doxygen will then generate output that is more tailored for C. For # instance, some of the names that are used will be different. The list of all @@ -311,22 +309,19 @@ OPTIMIZE_OUTPUT_SLICE = NO # parses. With this tag you can assign which parser to use for a given # extension. Doxygen has a built-in mapping, but you can override or extend it # using this tag. The format is ext=language, where ext is a file extension, and -# language is one of the parsers supported by doxygen: IDL, Java, JavaScript, -# Csharp (C#), C, C++, D, PHP, md (Markdown), Objective-C, Python, Slice, VHDL, +# language is one of the parsers supported by doxygen: IDL, Java, Javascript, +# Csharp (C#), C, C++, D, PHP, md (Markdown), Objective-C, Python, Slice, # Fortran (fixed format Fortran: FortranFixed, free formatted Fortran: # FortranFree, unknown formatted Fortran: Fortran. In the later case the parser # tries to guess whether the code is fixed or free formatted code, this is the -# default for Fortran type files). For instance to make doxygen treat .inc files -# as Fortran files (default is PHP), and .f files as C (default is Fortran), -# use: inc=Fortran f=C. +# default for Fortran type files), VHDL, tcl. For instance to make doxygen treat +# .inc files as Fortran files (default is PHP), and .f files as C (default is +# Fortran), use: inc=Fortran f=C. # # Note: For files without extension you can use no_extension as a placeholder. # # Note that for custom extensions you also need to set FILE_PATTERNS otherwise -# the files are not read by doxygen. When specifying no_extension you should add -# * to the FILE_PATTERNS. -# -# Note see also the list of default file extension mappings. +# the files are not read by doxygen. EXTENSION_MAPPING = @@ -460,19 +455,6 @@ TYPEDEF_HIDES_STRUCT = NO LOOKUP_CACHE_SIZE = 0 -# The NUM_PROC_THREADS specifies the number threads doxygen is allowed to use -# during processing. When set to 0 doxygen will based this on the number of -# cores available in the system. You can set it explicitly to a value larger -# than 0 to get more control over the balance between CPU load and processing -# speed. At this moment only the input processing can be done using multiple -# threads. Since this is still an experimental feature the default is set to 1, -# which efficively disables parallel processing. Please report any issues you -# encounter. Generating dot graphs in parallel is controlled by the -# DOT_NUM_THREADS setting. -# Minimum value: 0, maximum value: 32, default value: 1. - -NUM_PROC_THREADS = 1 - #--------------------------------------------------------------------------- # Build related configuration options #--------------------------------------------------------------------------- @@ -536,13 +518,6 @@ EXTRACT_LOCAL_METHODS = NO EXTRACT_ANON_NSPACES = NO -# If this flag is set to YES, the name of an unnamed parameter in a declaration -# will be determined by the corresponding definition. By default unnamed -# parameters remain unnamed in the output. -# The default value is: YES. - -RESOLVE_UNNAMED_PARAMS = YES - # If the HIDE_UNDOC_MEMBERS tag is set to YES, doxygen will hide all # undocumented members inside documented classes or files. If set to NO these # members will be included in the various overviews, but no documentation @@ -560,8 +535,8 @@ HIDE_UNDOC_MEMBERS = NO HIDE_UNDOC_CLASSES = NO # If the HIDE_FRIEND_COMPOUNDS tag is set to YES, doxygen will hide all friend -# declarations. If set to NO, these declarations will be included in the -# documentation. +# (class|struct|union) declarations. If set to NO, these declarations will be +# included in the documentation. # The default value is: NO. HIDE_FRIEND_COMPOUNDS = NO @@ -580,18 +555,11 @@ HIDE_IN_BODY_DOCS = NO INTERNAL_DOCS = NO -# With the correct setting of option CASE_SENSE_NAMES doxygen will better be -# able to match the capabilities of the underlying filesystem. In case the -# filesystem is case sensitive (i.e. it supports files in the same directory -# whose names only differ in casing), the option must be set to YES to properly -# deal with such files in case they appear in the input. For filesystems that -# are not case sensitive the option should be be set to NO to properly deal with -# output files written for symbols that only differ in casing, such as for two -# classes, one named CLASS and the other named Class, and to also support -# references to files without having to specify the exact matching casing. On -# Windows (including Cygwin) and MacOS, users should typically set this option -# to NO, whereas on Linux or other Unix flavors it should typically be set to -# YES. +# If the CASE_SENSE_NAMES tag is set to NO then doxygen will only generate file +# names in lower-case letters. If set to YES, upper-case letters are also +# allowed. This is useful if you have classes or files whose names only differ +# in case and if your file system supports case sensitive file names. Windows +# (including Cygwin) ands Mac users are advised to set this option to NO. # The default value is: system dependent. CASE_SENSE_NAMES = NO @@ -830,10 +798,7 @@ WARN_IF_DOC_ERROR = YES WARN_NO_PARAMDOC = NO # If the WARN_AS_ERROR tag is set to YES then doxygen will immediately stop when -# a warning is encountered. If the WARN_AS_ERROR tag is set to FAIL_ON_WARNINGS -# then doxygen will continue running as if WARN_AS_ERROR tag is set to NO, but -# at the end of the doxygen process doxygen will return with a non-zero status. -# Possible values are: NO, YES and FAIL_ON_WARNINGS. +# a warning is encountered. # The default value is: NO. WARN_AS_ERROR = NO @@ -864,13 +829,13 @@ WARN_LOGFILE = # spaces. See also FILE_PATTERNS and EXTENSION_MAPPING # Note: If this tag is empty the current directory is searched. -INPUT = include/vk_mem_alloc.h +INPUT = vk_mem_alloc.h # This tag can be used to specify the character encoding of the source files # that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses # libiconv (or the iconv built into libc) for the transcoding. See the libiconv -# documentation (see: -# https://www.gnu.org/software/libiconv/) for the list of possible encodings. +# documentation (see: https://www.gnu.org/software/libiconv/) for the list of +# possible encodings. # The default value is: UTF-8. INPUT_ENCODING = UTF-8 @@ -883,15 +848,11 @@ INPUT_ENCODING = UTF-8 # need to set EXTENSION_MAPPING for the extension otherwise the files are not # read by doxygen. # -# Note the list of default checked file patterns might differ from the list of -# default file extension mappings. -# # If left blank the following patterns are tested:*.c, *.cc, *.cxx, *.cpp, # *.c++, *.java, *.ii, *.ixx, *.ipp, *.i++, *.inl, *.idl, *.ddl, *.odl, *.h, # *.hh, *.hxx, *.hpp, *.h++, *.cs, *.d, *.php, *.php4, *.php5, *.phtml, *.inc, -# *.m, *.markdown, *.md, *.mm, *.dox (to be provided as doxygen C comment), -# *.py, *.pyw, *.f90, *.f95, *.f03, *.f08, *.f18, *.f, *.for, *.vhd, *.vhdl, -# *.ucf, *.qsf and *.ice. +# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08, +# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf, *.qsf and *.ice. FILE_PATTERNS = *.c \ *.cc \ @@ -1149,22 +1110,16 @@ USE_HTAGS = NO VERBATIM_HEADERS = YES # If the CLANG_ASSISTED_PARSING tag is set to YES then doxygen will use the -# clang parser (see: -# http://clang.llvm.org/) for more accurate parsing at the cost of reduced -# performance. This can be particularly helpful with template rich C++ code for -# which doxygen's built-in parser lacks the necessary type information. +# clang parser (see: http://clang.llvm.org/) for more accurate parsing at the +# cost of reduced performance. This can be particularly helpful with template +# rich C++ code for which doxygen's built-in parser lacks the necessary type +# information. # Note: The availability of this option depends on whether or not doxygen was # generated with the -Duse_libclang=ON option for CMake. # The default value is: NO. CLANG_ASSISTED_PARSING = NO -# If clang assisted parsing is enabled and the CLANG_ADD_INC_PATHS tag is set to -# YES then doxygen will add the directory of each input to the include path. -# The default value is: YES. - -CLANG_ADD_INC_PATHS = YES - # If clang assisted parsing is enabled you can provide the compiler with command # line options that you would normally use when invoking the compiler. Note that # the include paths will already be set by doxygen for the files and directories @@ -1174,13 +1129,10 @@ CLANG_ADD_INC_PATHS = YES CLANG_OPTIONS = # If clang assisted parsing is enabled you can provide the clang parser with the -# path to the directory containing a file called compile_commands.json. This -# file is the compilation database (see: -# http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html) containing the -# options used when the source files were built. This is equivalent to -# specifying the -p option to a clang tool, such as clang-check. These options -# will then be passed to the parser. Any options specified with CLANG_OPTIONS -# will be added as well. +# path to the compilation database (see: +# http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html) used when the files +# were built. This is equivalent to specifying the "-p" option to a clang tool, +# such as clang-check. These options will then be passed to the parser. # Note: The availability of this option depends on whether or not doxygen was # generated with the -Duse_libclang=ON option for CMake. @@ -1197,6 +1149,13 @@ CLANG_DATABASE_PATH = ALPHABETICAL_INDEX = YES +# The COLS_IN_ALPHA_INDEX tag can be used to specify the number of columns in +# which the alphabetical index list will be split. +# Minimum value: 1, maximum value: 20, default value: 5. +# This tag requires that the tag ALPHABETICAL_INDEX is set to YES. + +COLS_IN_ALPHA_INDEX = 5 + # In case all classes in a project start with a common prefix, all classes will # be put under the same header in the alphabetical index. The IGNORE_PREFIX tag # can be used to specify a prefix (or a list of prefixes) that should be ignored @@ -1335,9 +1294,9 @@ HTML_TIMESTAMP = NO # If the HTML_DYNAMIC_MENUS tag is set to YES then the generated HTML # documentation will contain a main index with vertical navigation menus that -# are dynamically created via JavaScript. If disabled, the navigation index will +# are dynamically created via Javascript. If disabled, the navigation index will # consists of multiple levels of tabs that are statically embedded in every HTML -# page. Disable this option to support browsers that do not have JavaScript, +# page. Disable this option to support browsers that do not have Javascript, # like the Qt help browser. # The default value is: YES. # This tag requires that the tag GENERATE_HTML is set to YES. @@ -1367,11 +1326,10 @@ HTML_INDEX_NUM_ENTRIES = 100 # If the GENERATE_DOCSET tag is set to YES, additional index files will be # generated that can be used as input for Apple's Xcode 3 integrated development -# environment (see: -# https://developer.apple.com/xcode/), introduced with OSX 10.5 (Leopard). To -# create a documentation set, doxygen will generate a Makefile in the HTML -# output directory. Running make will produce the docset in that directory and -# running make install will install the docset in +# environment (see: https://developer.apple.com/xcode/), introduced with OSX +# 10.5 (Leopard). To create a documentation set, doxygen will generate a +# Makefile in the HTML output directory. Running make will produce the docset in +# that directory and running make install will install the docset in # ~/Library/Developer/Shared/Documentation/DocSets so that Xcode will find it at # startup. See https://developer.apple.com/library/archive/featuredarticles/Doxy # genXcode/_index.html for more information. @@ -1413,8 +1371,8 @@ DOCSET_PUBLISHER_NAME = Publisher # If the GENERATE_HTMLHELP tag is set to YES then doxygen generates three # additional HTML index files: index.hhp, index.hhc, and index.hhk. The # index.hhp is a project file that can be read by Microsoft's HTML Help Workshop -# (see: -# https://www.microsoft.com/en-us/download/details.aspx?id=21138) on Windows. +# (see: https://www.microsoft.com/en-us/download/details.aspx?id=21138) on +# Windows. # # The HTML Help Workshop contains a compiler that can convert all HTML output # generated by doxygen into a single compiled HTML file (.chm). Compiled HTML @@ -1444,7 +1402,7 @@ CHM_FILE = HHC_LOCATION = # The GENERATE_CHI flag controls if a separate .chi index file is generated -# (YES) or that it should be included in the main .chm file (NO). +# (YES) or that it should be included in the master .chm file (NO). # The default value is: NO. # This tag requires that the tag GENERATE_HTMLHELP is set to YES. @@ -1489,8 +1447,7 @@ QCH_FILE = # The QHP_NAMESPACE tag specifies the namespace to use when generating Qt Help # Project output. For more information please see Qt Help Project / Namespace -# (see: -# https://doc.qt.io/archives/qt-4.8/qthelpproject.html#namespace). +# (see: https://doc.qt.io/archives/qt-4.8/qthelpproject.html#namespace). # The default value is: org.doxygen.Project. # This tag requires that the tag GENERATE_QHP is set to YES. @@ -1498,8 +1455,8 @@ QHP_NAMESPACE = org.doxygen.Project # The QHP_VIRTUAL_FOLDER tag specifies the namespace to use when generating Qt # Help Project output. For more information please see Qt Help Project / Virtual -# Folders (see: -# https://doc.qt.io/archives/qt-4.8/qthelpproject.html#virtual-folders). +# Folders (see: https://doc.qt.io/archives/qt-4.8/qthelpproject.html#virtual- +# folders). # The default value is: doc. # This tag requires that the tag GENERATE_QHP is set to YES. @@ -1507,16 +1464,16 @@ QHP_VIRTUAL_FOLDER = doc # If the QHP_CUST_FILTER_NAME tag is set, it specifies the name of a custom # filter to add. For more information please see Qt Help Project / Custom -# Filters (see: -# https://doc.qt.io/archives/qt-4.8/qthelpproject.html#custom-filters). +# Filters (see: https://doc.qt.io/archives/qt-4.8/qthelpproject.html#custom- +# filters). # This tag requires that the tag GENERATE_QHP is set to YES. QHP_CUST_FILTER_NAME = # The QHP_CUST_FILTER_ATTRS tag specifies the list of the attributes of the # custom filter to add. For more information please see Qt Help Project / Custom -# Filters (see: -# https://doc.qt.io/archives/qt-4.8/qthelpproject.html#custom-filters). +# Filters (see: https://doc.qt.io/archives/qt-4.8/qthelpproject.html#custom- +# filters). # This tag requires that the tag GENERATE_QHP is set to YES. QHP_CUST_FILTER_ATTRS = @@ -1528,9 +1485,9 @@ QHP_CUST_FILTER_ATTRS = QHP_SECT_FILTER_ATTRS = -# The QHG_LOCATION tag can be used to specify the location (absolute path -# including file name) of Qt's qhelpgenerator. If non-empty doxygen will try to -# run qhelpgenerator on the generated .qhp file. +# The QHG_LOCATION tag can be used to specify the location of Qt's +# qhelpgenerator. If non-empty doxygen will try to run qhelpgenerator on the +# generated .qhp file. # This tag requires that the tag GENERATE_QHP is set to YES. QHG_LOCATION = @@ -1607,17 +1564,6 @@ TREEVIEW_WIDTH = 250 EXT_LINKS_IN_WINDOW = NO -# If the HTML_FORMULA_FORMAT option is set to svg, doxygen will use the pdf2svg -# tool (see https://github.com/dawbarton/pdf2svg) or inkscape (see -# https://inkscape.org) to generate formulas as SVG images instead of PNGs for -# the HTML output. These images will generally look nicer at scaled resolutions. -# Possible values are: png (the default) and svg (looks nicer but requires the -# pdf2svg or inkscape tool). -# The default value is: png. -# This tag requires that the tag GENERATE_HTML is set to YES. - -HTML_FORMULA_FORMAT = png - # Use this tag to change the font size of LaTeX formulas included as images in # the HTML documentation. When you change the font size after a successful # doxygen run you need to manually remove any form_*.png images from the HTML @@ -1638,14 +1584,8 @@ FORMULA_FONTSIZE = 10 FORMULA_TRANSPARENT = YES -# The FORMULA_MACROFILE can contain LaTeX \newcommand and \renewcommand commands -# to create new LaTeX commands to be used in formulas as building blocks. See -# the section "Including formulas" for details. - -FORMULA_MACROFILE = - # Enable the USE_MATHJAX option to render LaTeX formulas using MathJax (see -# https://www.mathjax.org) which uses client side JavaScript for the rendering +# https://www.mathjax.org) which uses client side Javascript for the rendering # instead of using pre-rendered bitmaps. Use this if you do not have LaTeX # installed or if you want to formulas look prettier in the HTML output. When # enabled you may also need to install MathJax separately and configure the path @@ -1657,7 +1597,7 @@ USE_MATHJAX = NO # When MathJax is enabled you can set the default output format to be used for # the MathJax output. See the MathJax site (see: -# http://docs.mathjax.org/en/v2.7-latest/output.html) for more details. +# http://docs.mathjax.org/en/latest/output.html) for more details. # Possible values are: HTML-CSS (which is slower, but has the best # compatibility), NativeMML (i.e. MathML) and SVG. # The default value is: HTML-CSS. @@ -1673,7 +1613,7 @@ MATHJAX_FORMAT = HTML-CSS # Content Delivery Network so you can quickly see the result without installing # MathJax. However, it is strongly recommended to install a local copy of # MathJax from https://www.mathjax.org before deployment. -# The default value is: https://cdn.jsdelivr.net/npm/mathjax@2. +# The default value is: https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/. # This tag requires that the tag USE_MATHJAX is set to YES. MATHJAX_RELPATH = http://cdn.mathjax.org/mathjax/latest @@ -1687,8 +1627,7 @@ MATHJAX_EXTENSIONS = # The MATHJAX_CODEFILE tag can be used to specify a file with javascript pieces # of code that will be used on startup of the MathJax code. See the MathJax site -# (see: -# http://docs.mathjax.org/en/v2.7-latest/output.html) for more details. For an +# (see: http://docs.mathjax.org/en/latest/output.html) for more details. For an # example see the documentation. # This tag requires that the tag USE_MATHJAX is set to YES. @@ -1716,7 +1655,7 @@ MATHJAX_CODEFILE = SEARCHENGINE = YES # When the SERVER_BASED_SEARCH tag is enabled the search engine will be -# implemented using a web server instead of a web client using JavaScript. There +# implemented using a web server instead of a web client using Javascript. There # are two flavors of web server based searching depending on the EXTERNAL_SEARCH # setting. When disabled, doxygen will generate a PHP script for searching and # an index file used by the script. When EXTERNAL_SEARCH is enabled the indexing @@ -1735,8 +1674,7 @@ SERVER_BASED_SEARCH = NO # # Doxygen ships with an example indexer (doxyindexer) and search engine # (doxysearch.cgi) which are based on the open source search engine library -# Xapian (see: -# https://xapian.org/). +# Xapian (see: https://xapian.org/). # # See the section "External Indexing and Searching" for details. # The default value is: NO. @@ -1749,9 +1687,8 @@ EXTERNAL_SEARCH = NO # # Doxygen ships with an example indexer (doxyindexer) and search engine # (doxysearch.cgi) which are based on the open source search engine library -# Xapian (see: -# https://xapian.org/). See the section "External Indexing and Searching" for -# details. +# Xapian (see: https://xapian.org/). See the section "External Indexing and +# Searching" for details. # This tag requires that the tag SEARCHENGINE is set to YES. SEARCHENGINE_URL = @@ -1915,11 +1852,9 @@ LATEX_EXTRA_FILES = PDF_HYPERLINKS = YES -# If the USE_PDFLATEX tag is set to YES, doxygen will use the engine as -# specified with LATEX_CMD_NAME to generate the PDF file directly from the LaTeX -# files. Set this option to YES, to get a higher quality PDF documentation. -# -# See also section LATEX_CMD_NAME for selecting the engine. +# If the USE_PDFLATEX tag is set to YES, doxygen will use pdflatex to generate +# the PDF file directly from the LaTeX files. Set this option to YES, to get a +# higher quality PDF documentation. # The default value is: YES. # This tag requires that the tag GENERATE_LATEX is set to YES. @@ -2253,13 +2188,7 @@ INCLUDE_FILE_PATTERNS = # recursively expanded use the := operator instead of the = operator. # This tag requires that the tag ENABLE_PREPROCESSING is set to YES. -PREDEFINED = VMA_CALL_PRE= \ - VMA_CALL_POST= \ - VMA_NOT_NULL= \ - VMA_NULLABLE= \ - VMA_LEN_IF_NOT_NULL(len)= \ - VMA_NOT_NULL_NON_DISPATCHABLE= \ - VMA_NULLABLE_NON_DISPATCHABLE= +PREDEFINED = VMA_CALL_PRE= VMA_CALL_POST= # If the MACRO_EXPANSION and EXPAND_ONLY_PREDEF tags are set to YES then this # tag can be used to specify a list of macro names that should be expanded. The @@ -2436,31 +2365,9 @@ UML_LOOK = NO # but if the number exceeds 15, the total amount of fields shown is limited to # 10. # Minimum value: 0, maximum value: 100, default value: 10. -# This tag requires that the tag UML_LOOK is set to YES. - -UML_LIMIT_NUM_FIELDS = 10 - -# If the DOT_UML_DETAILS tag is set to NO, doxygen will show attributes and -# methods without types and arguments in the UML graphs. If the DOT_UML_DETAILS -# tag is set to YES, doxygen will add type and arguments for attributes and -# methods in the UML graphs. If the DOT_UML_DETAILS tag is set to NONE, doxygen -# will not generate fields with class member information in the UML graphs. The -# class diagrams will look similar to the default class diagrams but using UML -# notation for the relationships. -# Possible values are: NO, YES and NONE. -# The default value is: NO. -# This tag requires that the tag UML_LOOK is set to YES. - -DOT_UML_DETAILS = NO - -# The DOT_WRAP_THRESHOLD tag can be used to set the maximum number of characters -# to display on a single line. If the actual line length exceeds this threshold -# significantly it will wrapped across multiple lines. Some heuristics are apply -# to avoid ugly line breaks. -# Minimum value: 0, maximum value: 1000, default value: 17. # This tag requires that the tag HAVE_DOT is set to YES. -DOT_WRAP_THRESHOLD = 17 +UML_LIMIT_NUM_FIELDS = 10 # If the TEMPLATE_RELATIONS tag is set to YES then the inheritance and # collaboration graphs will show the relations between templates and their @@ -2651,11 +2558,9 @@ DOT_MULTI_TARGETS = NO GENERATE_LEGEND = YES -# If the DOT_CLEANUP tag is set to YES, doxygen will remove the intermediate +# If the DOT_CLEANUP tag is set to YES, doxygen will remove the intermediate dot # files that are used to generate the various graphs. -# -# Note: This setting is not only used for dot files but also for msc and -# plantuml temporary files. # The default value is: YES. +# This tag requires that the tag HAVE_DOT is set to YES. DOT_CLEANUP = YES diff --git a/third_party/vkmemalloc/src/Shaders/CMakeLists.txt b/third_party/vkmemalloc/src/Shaders/CMakeLists.txt deleted file mode 100644 index f85ab78cfd..0000000000 --- a/third_party/vkmemalloc/src/Shaders/CMakeLists.txt +++ /dev/null @@ -1,32 +0,0 @@ -# This file will only be executed if VMA_BUILD_SAMPLE_SHADERS is set to ON - -find_program(GLSL_VALIDATOR glslangValidator REQUIRED) - -if(NOT GLSL_VALIDATOR) - message(FATAL_ERROR "glslangValidator not found!") -endif() - -set(SHADERS - Shader.vert - Shader.frag - SparseBindingTest.comp -) - -# Compile each shader using glslangValidator -foreach(SHADER ${SHADERS}) - get_filename_component(FILE_NAME ${SHADER} NAME) - - # Put the .spv files into the bin folder - set(SPIRV ${PROJECT_SOURCE_DIR}/bin/${FILE_NAME}.spv) - - add_custom_command( - OUTPUT ${SPIRV} - # Use the same file name and append .spv to the compiled shader - COMMAND ${GLSL_VALIDATOR} -V ${CMAKE_CURRENT_SOURCE_DIR}/${SHADER} -o ${SPIRV} - DEPENDS ${SHADER} - ) - - list(APPEND SPIRV_FILES ${SPIRV}) -endforeach() - -add_custom_target(VmaSampleShaders ALL DEPENDS ${SPIRV_FILES}) diff --git a/third_party/vkmemalloc/src/Shaders/Shader.frag b/third_party/vkmemalloc/src/Shaders/Shader.frag index 207ec8bcd8..4060483c30 100644 --- a/third_party/vkmemalloc/src/Shaders/Shader.frag +++ b/third_party/vkmemalloc/src/Shaders/Shader.frag @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/Shaders/Shader.vert b/third_party/vkmemalloc/src/Shaders/Shader.vert index 7ef5ddbdcc..06ff262f06 100644 --- a/third_party/vkmemalloc/src/Shaders/Shader.vert +++ b/third_party/vkmemalloc/src/Shaders/Shader.vert @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/Shaders/SparseBindingTest.comp b/third_party/vkmemalloc/src/Shaders/SparseBindingTest.comp index b94027d5ee..7c8889e5fd 100644 --- a/third_party/vkmemalloc/src/Shaders/SparseBindingTest.comp +++ b/third_party/vkmemalloc/src/Shaders/SparseBindingTest.comp @@ -1,5 +1,5 @@ // -// Copyright (c) 2018-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2018-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/SparseBindingTest.cpp b/third_party/vkmemalloc/src/SparseBindingTest.cpp index b56a048013..8fbd5e9b31 100644 --- a/third_party/vkmemalloc/src/SparseBindingTest.cpp +++ b/third_party/vkmemalloc/src/SparseBindingTest.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -532,8 +532,6 @@ SparseBindingImage::~SparseBindingImage() void TestSparseBinding() { - wprintf(L"TESTING SPARSE BINDING:\n"); - struct ImageInfo { std::unique_ptr image; @@ -547,7 +545,7 @@ void TestSparseBinding() RandomNumberGenerator rand(4652467); - for(uint32_t frameIndex = 0; frameIndex < frameCount; ++frameIndex) + for(uint32_t i = 0; i < frameCount; ++i) { // Bump frame index. ++g_FrameIndex; @@ -562,11 +560,11 @@ void TestSparseBinding() images.push_back(std::move(imageInfo)); // Delete all images that expired. - for(size_t imageIndex = images.size(); imageIndex--; ) + for(size_t i = images.size(); i--; ) { - if(g_FrameIndex >= images[imageIndex].endFrame) + if(g_FrameIndex >= images[i].endFrame) { - images.erase(images.begin() + imageIndex); + images.erase(images.begin() + i); } } } @@ -590,8 +588,6 @@ void TestSparseBinding() // Free remaining images. images.clear(); - - wprintf(L"Done.\n"); } #endif // #ifdef _WIN32 diff --git a/third_party/vkmemalloc/src/SparseBindingTest.h b/third_party/vkmemalloc/src/SparseBindingTest.h index 98087fa7f2..69b95d63b0 100644 --- a/third_party/vkmemalloc/src/SparseBindingTest.h +++ b/third_party/vkmemalloc/src/SparseBindingTest.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/Tests.cpp b/third_party/vkmemalloc/src/Tests.cpp index 7cccb4dedd..5a6057f655 100644 --- a/third_party/vkmemalloc/src/Tests.cpp +++ b/third_party/vkmemalloc/src/Tests.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -34,12 +34,8 @@ static const char* CODE_DESCRIPTION = "Foo"; extern VkCommandBuffer g_hTemporaryCommandBuffer; extern const VkAllocationCallbacks* g_Allocs; -extern bool VK_KHR_buffer_device_address_enabled; -extern bool VK_EXT_memory_priority_enabled; -extern PFN_vkGetBufferDeviceAddressKHR g_vkGetBufferDeviceAddressKHR; void BeginSingleTimeCommands(); void EndSingleTimeCommands(); -void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, const char* name); #ifndef VMA_DEBUG_MARGIN #define VMA_DEBUG_MARGIN 0 @@ -692,7 +688,6 @@ struct AllocInfo VmaAllocation m_Allocation = VK_NULL_HANDLE; VkBuffer m_Buffer = VK_NULL_HANDLE; VkImage m_Image = VK_NULL_HANDLE; - VkImageLayout m_ImageLayout = VK_IMAGE_LAYOUT_UNDEFINED; uint32_t m_StartValue = 0; union { @@ -700,17 +695,9 @@ struct AllocInfo VkImageCreateInfo m_ImageInfo; }; - // After defragmentation. - VkBuffer m_NewBuffer = VK_NULL_HANDLE; - VkImage m_NewImage = VK_NULL_HANDLE; - void CreateBuffer( const VkBufferCreateInfo& bufCreateInfo, const VmaAllocationCreateInfo& allocCreateInfo); - void CreateImage( - const VkImageCreateInfo& imageCreateInfo, - const VmaAllocationCreateInfo& allocCreateInfo, - VkImageLayout layout); void Destroy(); }; @@ -722,28 +709,16 @@ void AllocInfo::CreateBuffer( VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &m_Buffer, &m_Allocation, nullptr); TEST(res == VK_SUCCESS); } -void AllocInfo::CreateImage( - const VkImageCreateInfo& imageCreateInfo, - const VmaAllocationCreateInfo& allocCreateInfo, - VkImageLayout layout) -{ - m_ImageInfo = imageCreateInfo; - m_ImageLayout = layout; - VkResult res = vmaCreateImage(g_hAllocator, &imageCreateInfo, &allocCreateInfo, &m_Image, &m_Allocation, nullptr); - TEST(res == VK_SUCCESS); -} void AllocInfo::Destroy() { if(m_Image) { - assert(!m_Buffer); vkDestroyImage(g_hDevice, m_Image, g_Allocs); m_Image = VK_NULL_HANDLE; } if(m_Buffer) { - assert(!m_Image); vkDestroyBuffer(g_hDevice, m_Buffer, g_Allocs); m_Buffer = VK_NULL_HANDLE; } @@ -929,88 +904,7 @@ static void UploadGpuData(const AllocInfo* allocInfo, size_t allocInfoCount) } else { - TEST(currAllocInfo.m_ImageInfo.format == VK_FORMAT_R8G8B8A8_UNORM && "Only RGBA8 images are currently supported."); - TEST(currAllocInfo.m_ImageInfo.mipLevels == 1 && "Only single mip images are currently supported."); - - const VkDeviceSize size = (VkDeviceSize)currAllocInfo.m_ImageInfo.extent.width * currAllocInfo.m_ImageInfo.extent.height * sizeof(uint32_t); - - VkBuffer stagingBuf = VK_NULL_HANDLE; - void* stagingBufMappedPtr = nullptr; - if(!stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr)) - { - TEST(cmdBufferStarted); - EndSingleTimeCommands(); - stagingBufs.ReleaseAllBuffers(); - cmdBufferStarted = false; - - bool ok = stagingBufs.AcquireBuffer(size, stagingBuf, stagingBufMappedPtr); - TEST(ok); - } - - // Fill staging buffer. - { - assert(size % sizeof(uint32_t) == 0); - uint32_t *stagingValPtr = (uint32_t *)stagingBufMappedPtr; - uint32_t val = currAllocInfo.m_StartValue; - for(size_t i = 0; i < size / sizeof(uint32_t); ++i) - { - *stagingValPtr = val; - ++stagingValPtr; - ++val; - } - } - - // Issue copy command from staging buffer to destination buffer. - if(!cmdBufferStarted) - { - cmdBufferStarted = true; - BeginSingleTimeCommands(); - } - - - // Transfer to transfer dst layout - VkImageSubresourceRange subresourceRange = { - VK_IMAGE_ASPECT_COLOR_BIT, - 0, VK_REMAINING_MIP_LEVELS, - 0, VK_REMAINING_ARRAY_LAYERS - }; - - VkImageMemoryBarrier barrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER }; - barrier.srcAccessMask = 0; - barrier.dstAccessMask = 0; - barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; - barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - barrier.image = currAllocInfo.m_Image; - barrier.subresourceRange = subresourceRange; - - vkCmdPipelineBarrier(g_hTemporaryCommandBuffer, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, - 0, nullptr, - 0, nullptr, - 1, &barrier); - - // Copy image date - VkBufferImageCopy copy = {}; - copy.bufferOffset = 0; - copy.bufferRowLength = 0; - copy.bufferImageHeight = 0; - copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; - copy.imageSubresource.layerCount = 1; - copy.imageExtent = currAllocInfo.m_ImageInfo.extent; - - vkCmdCopyBufferToImage(g_hTemporaryCommandBuffer, stagingBuf, currAllocInfo.m_Image, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©); - - // Transfer to desired layout - barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; - barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - barrier.newLayout = currAllocInfo.m_ImageLayout; - - vkCmdPipelineBarrier(g_hTemporaryCommandBuffer, VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT, 0, - 0, nullptr, - 0, nullptr, - 1, &barrier); + TEST(0 && "Images not currently supported."); } } @@ -1722,6 +1616,7 @@ void TestDefragmentationFull() static void TestDefragmentationGpu() { wprintf(L"Test defragmentation GPU\n"); + g_MemoryAliasingWarningEnabled = false; std::vector allocations; @@ -1855,587 +1750,10 @@ static void TestDefragmentationGpu() { allocations[i].Destroy(); } + + g_MemoryAliasingWarningEnabled = true; } -static void ProcessDefragmentationStepInfo(VmaDefragmentationPassInfo &stepInfo) -{ - std::vector beginImageBarriers; - std::vector finalizeImageBarriers; - - VkPipelineStageFlags beginSrcStageMask = 0; - VkPipelineStageFlags beginDstStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; - - VkPipelineStageFlags finalizeSrcStageMask = VK_PIPELINE_STAGE_TRANSFER_BIT; - VkPipelineStageFlags finalizeDstStageMask = 0; - - bool wantsMemoryBarrier = false; - - VkMemoryBarrier beginMemoryBarrier = { VK_STRUCTURE_TYPE_MEMORY_BARRIER }; - VkMemoryBarrier finalizeMemoryBarrier = { VK_STRUCTURE_TYPE_MEMORY_BARRIER }; - - for(uint32_t i = 0; i < stepInfo.moveCount; ++i) - { - VmaAllocationInfo info; - vmaGetAllocationInfo(g_hAllocator, stepInfo.pMoves[i].allocation, &info); - - AllocInfo *allocInfo = (AllocInfo *)info.pUserData; - - if(allocInfo->m_Image) - { - VkImage newImage; - - const VkResult result = vkCreateImage(g_hDevice, &allocInfo->m_ImageInfo, g_Allocs, &newImage); - TEST(result >= VK_SUCCESS); - - vkBindImageMemory(g_hDevice, newImage, stepInfo.pMoves[i].memory, stepInfo.pMoves[i].offset); - allocInfo->m_NewImage = newImage; - - // Keep track of our pipeline stages that we need to wait/signal on - beginSrcStageMask |= VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - finalizeDstStageMask |= VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - - // We need one pipeline barrier and two image layout transitions here - // First we'll have to turn our newly created image into VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL - // And the second one is turning the old image into VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL - - VkImageSubresourceRange subresourceRange = { - VK_IMAGE_ASPECT_COLOR_BIT, - 0, VK_REMAINING_MIP_LEVELS, - 0, VK_REMAINING_ARRAY_LAYERS - }; - - VkImageMemoryBarrier barrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER }; - barrier.srcAccessMask = 0; - barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; - barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED; - barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - barrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - barrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED; - barrier.image = newImage; - barrier.subresourceRange = subresourceRange; - - beginImageBarriers.push_back(barrier); - - // Second barrier to convert the existing image. This one actually needs a real barrier - barrier.srcAccessMask = VK_ACCESS_MEMORY_WRITE_BIT; - barrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT; - barrier.oldLayout = allocInfo->m_ImageLayout; - barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL; - barrier.image = allocInfo->m_Image; - - beginImageBarriers.push_back(barrier); - - // And lastly we need a barrier that turns our new image into the layout of the old one - barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT; - barrier.dstAccessMask = VK_ACCESS_MEMORY_READ_BIT; - barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL; - barrier.newLayout = allocInfo->m_ImageLayout; - barrier.image = newImage; - - finalizeImageBarriers.push_back(barrier); - } - else if(allocInfo->m_Buffer) - { - VkBuffer newBuffer; - - const VkResult result = vkCreateBuffer(g_hDevice, &allocInfo->m_BufferInfo, g_Allocs, &newBuffer); - TEST(result >= VK_SUCCESS); - - vkBindBufferMemory(g_hDevice, newBuffer, stepInfo.pMoves[i].memory, stepInfo.pMoves[i].offset); - allocInfo->m_NewBuffer = newBuffer; - - // Keep track of our pipeline stages that we need to wait/signal on - beginSrcStageMask |= VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - finalizeDstStageMask |= VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT; - - beginMemoryBarrier.srcAccessMask |= VK_ACCESS_MEMORY_WRITE_BIT; - beginMemoryBarrier.dstAccessMask |= VK_ACCESS_TRANSFER_READ_BIT; - - finalizeMemoryBarrier.srcAccessMask |= VK_ACCESS_TRANSFER_WRITE_BIT; - finalizeMemoryBarrier.dstAccessMask |= VK_ACCESS_MEMORY_READ_BIT; - - wantsMemoryBarrier = true; - } - } - - if(!beginImageBarriers.empty() || wantsMemoryBarrier) - { - const uint32_t memoryBarrierCount = wantsMemoryBarrier ? 1 : 0; - - vkCmdPipelineBarrier(g_hTemporaryCommandBuffer, beginSrcStageMask, beginDstStageMask, 0, - memoryBarrierCount, &beginMemoryBarrier, - 0, nullptr, - (uint32_t)beginImageBarriers.size(), beginImageBarriers.data()); - } - - for(uint32_t i = 0; i < stepInfo.moveCount; ++ i) - { - VmaAllocationInfo info; - vmaGetAllocationInfo(g_hAllocator, stepInfo.pMoves[i].allocation, &info); - - AllocInfo *allocInfo = (AllocInfo *)info.pUserData; - - if(allocInfo->m_Image) - { - std::vector imageCopies; - - // Copy all mips of the source image into the target image - VkOffset3D offset = { 0, 0, 0 }; - VkExtent3D extent = allocInfo->m_ImageInfo.extent; - - VkImageSubresourceLayers subresourceLayers = { - VK_IMAGE_ASPECT_COLOR_BIT, - 0, - 0, 1 - }; - - for(uint32_t mip = 0; mip < allocInfo->m_ImageInfo.mipLevels; ++ mip) - { - subresourceLayers.mipLevel = mip; - - VkImageCopy imageCopy{ - subresourceLayers, - offset, - subresourceLayers, - offset, - extent - }; - - imageCopies.push_back(imageCopy); - - extent.width = std::max(uint32_t(1), extent.width >> 1); - extent.height = std::max(uint32_t(1), extent.height >> 1); - extent.depth = std::max(uint32_t(1), extent.depth >> 1); - } - - vkCmdCopyImage( - g_hTemporaryCommandBuffer, - allocInfo->m_Image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL, - allocInfo->m_NewImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, - (uint32_t)imageCopies.size(), imageCopies.data()); - } - else if(allocInfo->m_Buffer) - { - VkBufferCopy region = { - 0, - 0, - allocInfo->m_BufferInfo.size }; - - vkCmdCopyBuffer(g_hTemporaryCommandBuffer, - allocInfo->m_Buffer, allocInfo->m_NewBuffer, - 1, ®ion); - } - } - - if(!finalizeImageBarriers.empty() || wantsMemoryBarrier) - { - const uint32_t memoryBarrierCount = wantsMemoryBarrier ? 1 : 0; - - vkCmdPipelineBarrier(g_hTemporaryCommandBuffer, finalizeSrcStageMask, finalizeDstStageMask, 0, - memoryBarrierCount, &finalizeMemoryBarrier, - 0, nullptr, - (uint32_t)finalizeImageBarriers.size(), finalizeImageBarriers.data()); - } -} - - -static void TestDefragmentationIncrementalBasic() -{ - wprintf(L"Test defragmentation incremental basic\n"); - - std::vector allocations; - - // Create that many allocations to surely fill 3 new blocks of 256 MB. - const std::array imageSizes = { 256, 512, 1024 }; - const VkDeviceSize bufSizeMin = 5ull * 1024 * 1024; - const VkDeviceSize bufSizeMax = 10ull * 1024 * 1024; - const VkDeviceSize totalSize = 3ull * 256 * 1024 * 1024; - const size_t imageCount = totalSize / ((size_t)imageSizes[0] * imageSizes[0] * 4) / 2; - const size_t bufCount = (size_t)(totalSize / bufSizeMin) / 2; - const size_t percentToLeave = 30; - RandomNumberGenerator rand = { 234522 }; - - VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.extent.depth = 1; - imageInfo.mipLevels = 1; - imageInfo.arrayLayers = 1; - imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; - imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - allocCreateInfo.flags = 0; - - // Create all intended images. - for(size_t i = 0; i < imageCount; ++i) - { - const uint32_t size = imageSizes[rand.Generate() % 3]; - - imageInfo.extent.width = size; - imageInfo.extent.height = size; - - AllocInfo alloc; - alloc.CreateImage(imageInfo, allocCreateInfo, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - alloc.m_StartValue = 0; - - allocations.push_back(alloc); - } - - // And all buffers - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - - for(size_t i = 0; i < bufCount; ++i) - { - bufCreateInfo.size = align_up(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16); - bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - - AllocInfo alloc; - alloc.CreateBuffer(bufCreateInfo, allocCreateInfo); - alloc.m_StartValue = 0; - - allocations.push_back(alloc); - } - - // Destroy some percentage of them. - { - const size_t allocationsToDestroy = round_div((imageCount + bufCount) * (100 - percentToLeave), 100); - for(size_t i = 0; i < allocationsToDestroy; ++i) - { - const size_t index = rand.Generate() % allocations.size(); - allocations[index].Destroy(); - allocations.erase(allocations.begin() + index); - } - } - - { - // Set our user data pointers. A real application should probably be more clever here - const size_t allocationCount = allocations.size(); - for(size_t i = 0; i < allocationCount; ++i) - { - AllocInfo &alloc = allocations[i]; - vmaSetAllocationUserData(g_hAllocator, alloc.m_Allocation, &alloc); - } - } - - // Fill them with meaningful data. - UploadGpuData(allocations.data(), allocations.size()); - - wchar_t fileName[MAX_PATH]; - swprintf_s(fileName, L"GPU_defragmentation_incremental_basic_A_before.json"); - SaveAllocatorStatsToFile(fileName); - - // Defragment using GPU only. - { - const size_t allocCount = allocations.size(); - - std::vector allocationPtrs; - - for(size_t i = 0; i < allocCount; ++i) - { - allocationPtrs.push_back(allocations[i].m_Allocation); - } - - const size_t movableAllocCount = allocationPtrs.size(); - - VmaDefragmentationInfo2 defragInfo = {}; - defragInfo.flags = VMA_DEFRAGMENTATION_FLAG_INCREMENTAL; - defragInfo.allocationCount = (uint32_t)movableAllocCount; - defragInfo.pAllocations = allocationPtrs.data(); - defragInfo.maxGpuBytesToMove = VK_WHOLE_SIZE; - defragInfo.maxGpuAllocationsToMove = UINT32_MAX; - - VmaDefragmentationStats stats = {}; - VmaDefragmentationContext ctx = VK_NULL_HANDLE; - VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &stats, &ctx); - TEST(res >= VK_SUCCESS); - - res = VK_NOT_READY; - - std::vector moveInfo; - moveInfo.resize(movableAllocCount); - - while(res == VK_NOT_READY) - { - VmaDefragmentationPassInfo stepInfo = {}; - stepInfo.pMoves = moveInfo.data(); - stepInfo.moveCount = (uint32_t)moveInfo.size(); - - res = vmaBeginDefragmentationPass(g_hAllocator, ctx, &stepInfo); - TEST(res >= VK_SUCCESS); - - BeginSingleTimeCommands(); - std::vector newHandles; - ProcessDefragmentationStepInfo(stepInfo); - EndSingleTimeCommands(); - - res = vmaEndDefragmentationPass(g_hAllocator, ctx); - - // Destroy old buffers/images and replace them with new handles. - for(size_t i = 0; i < stepInfo.moveCount; ++i) - { - VmaAllocation const alloc = stepInfo.pMoves[i].allocation; - VmaAllocationInfo vmaAllocInfo; - vmaGetAllocationInfo(g_hAllocator, alloc, &vmaAllocInfo); - AllocInfo* allocInfo = (AllocInfo*)vmaAllocInfo.pUserData; - if(allocInfo->m_Buffer) - { - assert(allocInfo->m_NewBuffer && !allocInfo->m_Image && !allocInfo->m_NewImage); - vkDestroyBuffer(g_hDevice, allocInfo->m_Buffer, g_Allocs); - allocInfo->m_Buffer = allocInfo->m_NewBuffer; - allocInfo->m_NewBuffer = VK_NULL_HANDLE; - } - else if(allocInfo->m_Image) - { - assert(allocInfo->m_NewImage && !allocInfo->m_Buffer && !allocInfo->m_NewBuffer); - vkDestroyImage(g_hDevice, allocInfo->m_Image, g_Allocs); - allocInfo->m_Image = allocInfo->m_NewImage; - allocInfo->m_NewImage = VK_NULL_HANDLE; - } - else - assert(0); - } - } - - TEST(res >= VK_SUCCESS); - vmaDefragmentationEnd(g_hAllocator, ctx); - - // If corruption detection is enabled, GPU defragmentation may not work on - // memory types that have this detection active, e.g. on Intel. -#if !defined(VMA_DEBUG_DETECT_CORRUPTION) || VMA_DEBUG_DETECT_CORRUPTION == 0 - TEST(stats.allocationsMoved > 0 && stats.bytesMoved > 0); - TEST(stats.deviceMemoryBlocksFreed > 0 && stats.bytesFreed > 0); -#endif - } - - //ValidateGpuData(allocations.data(), allocations.size()); - - swprintf_s(fileName, L"GPU_defragmentation_incremental_basic_B_after.json"); - SaveAllocatorStatsToFile(fileName); - - // Destroy all remaining buffers and images. - for(size_t i = allocations.size(); i--; ) - { - allocations[i].Destroy(); - } -} - -void TestDefragmentationIncrementalComplex() -{ - wprintf(L"Test defragmentation incremental complex\n"); - - std::vector allocations; - - // Create that many allocations to surely fill 3 new blocks of 256 MB. - const std::array imageSizes = { 256, 512, 1024 }; - const VkDeviceSize bufSizeMin = 5ull * 1024 * 1024; - const VkDeviceSize bufSizeMax = 10ull * 1024 * 1024; - const VkDeviceSize totalSize = 3ull * 256 * 1024 * 1024; - const size_t imageCount = (size_t)(totalSize / (imageSizes[0] * imageSizes[0] * 4)) / 2; - const size_t bufCount = (size_t)(totalSize / bufSizeMin) / 2; - const size_t percentToLeave = 30; - RandomNumberGenerator rand = { 234522 }; - - VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.extent.depth = 1; - imageInfo.mipLevels = 1; - imageInfo.arrayLayers = 1; - imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; - imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - allocCreateInfo.flags = 0; - - // Create all intended images. - for(size_t i = 0; i < imageCount; ++i) - { - const uint32_t size = imageSizes[rand.Generate() % 3]; - - imageInfo.extent.width = size; - imageInfo.extent.height = size; - - AllocInfo alloc; - alloc.CreateImage(imageInfo, allocCreateInfo, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL); - alloc.m_StartValue = 0; - - allocations.push_back(alloc); - } - - // And all buffers - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - - for(size_t i = 0; i < bufCount; ++i) - { - bufCreateInfo.size = align_up(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16); - bufCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; - - AllocInfo alloc; - alloc.CreateBuffer(bufCreateInfo, allocCreateInfo); - alloc.m_StartValue = 0; - - allocations.push_back(alloc); - } - - // Destroy some percentage of them. - { - const size_t allocationsToDestroy = round_div((imageCount + bufCount) * (100 - percentToLeave), 100); - for(size_t i = 0; i < allocationsToDestroy; ++i) - { - const size_t index = rand.Generate() % allocations.size(); - allocations[index].Destroy(); - allocations.erase(allocations.begin() + index); - } - } - - { - // Set our user data pointers. A real application should probably be more clever here - const size_t allocationCount = allocations.size(); - for(size_t i = 0; i < allocationCount; ++i) - { - AllocInfo &alloc = allocations[i]; - vmaSetAllocationUserData(g_hAllocator, alloc.m_Allocation, &alloc); - } - } - - // Fill them with meaningful data. - UploadGpuData(allocations.data(), allocations.size()); - - wchar_t fileName[MAX_PATH]; - swprintf_s(fileName, L"GPU_defragmentation_incremental_complex_A_before.json"); - SaveAllocatorStatsToFile(fileName); - - std::vector additionalAllocations; - -#define MakeAdditionalAllocation() \ - do { \ - { \ - bufCreateInfo.size = align_up(bufSizeMin + rand.Generate() % (bufSizeMax - bufSizeMin), 16); \ - bufCreateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT; \ - \ - AllocInfo alloc; \ - alloc.CreateBuffer(bufCreateInfo, allocCreateInfo); \ - \ - additionalAllocations.push_back(alloc); \ - } \ - } while(0) - - // Defragment using GPU only. - { - const size_t allocCount = allocations.size(); - - std::vector allocationPtrs; - - for(size_t i = 0; i < allocCount; ++i) - { - VmaAllocationInfo allocInfo = {}; - vmaGetAllocationInfo(g_hAllocator, allocations[i].m_Allocation, &allocInfo); - - allocationPtrs.push_back(allocations[i].m_Allocation); - } - - const size_t movableAllocCount = allocationPtrs.size(); - - VmaDefragmentationInfo2 defragInfo = {}; - defragInfo.flags = VMA_DEFRAGMENTATION_FLAG_INCREMENTAL; - defragInfo.allocationCount = (uint32_t)movableAllocCount; - defragInfo.pAllocations = allocationPtrs.data(); - defragInfo.maxGpuBytesToMove = VK_WHOLE_SIZE; - defragInfo.maxGpuAllocationsToMove = UINT32_MAX; - - VmaDefragmentationStats stats = {}; - VmaDefragmentationContext ctx = VK_NULL_HANDLE; - VkResult res = vmaDefragmentationBegin(g_hAllocator, &defragInfo, &stats, &ctx); - TEST(res >= VK_SUCCESS); - - res = VK_NOT_READY; - - std::vector moveInfo; - moveInfo.resize(movableAllocCount); - - MakeAdditionalAllocation(); - - while(res == VK_NOT_READY) - { - VmaDefragmentationPassInfo stepInfo = {}; - stepInfo.pMoves = moveInfo.data(); - stepInfo.moveCount = (uint32_t)moveInfo.size(); - - res = vmaBeginDefragmentationPass(g_hAllocator, ctx, &stepInfo); - TEST(res >= VK_SUCCESS); - - MakeAdditionalAllocation(); - - BeginSingleTimeCommands(); - ProcessDefragmentationStepInfo(stepInfo); - EndSingleTimeCommands(); - - res = vmaEndDefragmentationPass(g_hAllocator, ctx); - - // Destroy old buffers/images and replace them with new handles. - for(size_t i = 0; i < stepInfo.moveCount; ++i) - { - VmaAllocation const alloc = stepInfo.pMoves[i].allocation; - VmaAllocationInfo vmaAllocInfo; - vmaGetAllocationInfo(g_hAllocator, alloc, &vmaAllocInfo); - AllocInfo* allocInfo = (AllocInfo*)vmaAllocInfo.pUserData; - if(allocInfo->m_Buffer) - { - assert(allocInfo->m_NewBuffer && !allocInfo->m_Image && !allocInfo->m_NewImage); - vkDestroyBuffer(g_hDevice, allocInfo->m_Buffer, g_Allocs); - allocInfo->m_Buffer = allocInfo->m_NewBuffer; - allocInfo->m_NewBuffer = VK_NULL_HANDLE; - } - else if(allocInfo->m_Image) - { - assert(allocInfo->m_NewImage && !allocInfo->m_Buffer && !allocInfo->m_NewBuffer); - vkDestroyImage(g_hDevice, allocInfo->m_Image, g_Allocs); - allocInfo->m_Image = allocInfo->m_NewImage; - allocInfo->m_NewImage = VK_NULL_HANDLE; - } - else - assert(0); - } - - MakeAdditionalAllocation(); - } - - TEST(res >= VK_SUCCESS); - vmaDefragmentationEnd(g_hAllocator, ctx); - - // If corruption detection is enabled, GPU defragmentation may not work on - // memory types that have this detection active, e.g. on Intel. -#if !defined(VMA_DEBUG_DETECT_CORRUPTION) || VMA_DEBUG_DETECT_CORRUPTION == 0 - TEST(stats.allocationsMoved > 0 && stats.bytesMoved > 0); - TEST(stats.deviceMemoryBlocksFreed > 0 && stats.bytesFreed > 0); -#endif - } - - //ValidateGpuData(allocations.data(), allocations.size()); - - swprintf_s(fileName, L"GPU_defragmentation_incremental_complex_B_after.json"); - SaveAllocatorStatsToFile(fileName); - - // Destroy all remaining buffers. - for(size_t i = allocations.size(); i--; ) - { - allocations[i].Destroy(); - } - - for(size_t i = additionalAllocations.size(); i--; ) - { - additionalAllocations[i].Destroy(); - } -} - - static void TestUserData() { VkResult res; @@ -2621,25 +1939,10 @@ static void TestMemoryRequirements() } -static void TestGetAllocatorInfo() -{ - wprintf(L"Test vnaGetAllocatorInfo\n"); - - VmaAllocatorInfo allocInfo = {}; - vmaGetAllocatorInfo(g_hAllocator, &allocInfo); - TEST(allocInfo.instance == g_hVulkanInstance); - TEST(allocInfo.physicalDevice == g_hPhysicalDevice); - TEST(allocInfo.device == g_hDevice); -} - static void TestBasics() { - wprintf(L"Test basics\n"); - VkResult res; - TestGetAllocatorInfo(); - TestMemoryRequirements(); // Lost allocation @@ -2686,41 +1989,6 @@ static void TestBasics() TestInvalidAllocations(); } -static void TestAllocationVersusResourceSize() -{ - wprintf(L"Test allocation versus resource size\n"); - - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 22921; // Prime number - bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY; - - for(uint32_t i = 0; i < 2; ++i) - { - allocCreateInfo.flags = (i == 1) ? VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT : 0; - - AllocInfo info; - info.CreateBuffer(bufCreateInfo, allocCreateInfo); - - VmaAllocationInfo allocInfo = {}; - vmaGetAllocationInfo(g_hAllocator, info.m_Allocation, &allocInfo); - //wprintf(L" Buffer size = %llu, allocation size = %llu\n", bufCreateInfo.size, allocInfo.size); - - // Map and test accessing entire area of the allocation, not only the buffer. - void* mappedPtr = nullptr; - VkResult res = vmaMapMemory(g_hAllocator, info.m_Allocation, &mappedPtr); - TEST(res == VK_SUCCESS); - - memset(mappedPtr, 0xCC, (size_t)allocInfo.size); - - vmaUnmapMemory(g_hAllocator, info.m_Allocation); - - info.Destroy(); - } -} - static void TestPool_MinBlockCount() { #if defined(VMA_DEBUG_MARGIN) && VMA_DEBUG_MARGIN > 0 @@ -2797,8 +2065,8 @@ static void TestPool_MinBlockCount() void TestHeapSizeLimit() { - const VkDeviceSize HEAP_SIZE_LIMIT = 100ull * 1024 * 1024; // 100 MB - const VkDeviceSize BLOCK_SIZE = 10ull * 1024 * 1024; // 10 MB + const VkDeviceSize HEAP_SIZE_LIMIT = 200ull * 1024 * 1024; // 200 MB + const VkDeviceSize BLOCK_SIZE = 20ull * 1024 * 1024; // 20 MB VkDeviceSize heapSizeLimit[VK_MAX_MEMORY_HEAPS]; for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i) @@ -3798,72 +3066,6 @@ static void BenchmarkAlgorithmsCase(FILE* file, } } -static void TestBufferDeviceAddress() -{ - wprintf(L"Test buffer device address\n"); - - assert(VK_KHR_buffer_device_address_enabled); - - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 0x10000; - bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | - VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT; // !!! - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - - for(uint32_t testIndex = 0; testIndex < 2; ++testIndex) - { - // 1st is placed, 2nd is dedicated. - if(testIndex == 1) - allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; - - BufferInfo bufInfo = {}; - VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, - &bufInfo.Buffer, &bufInfo.Allocation, nullptr); - TEST(res == VK_SUCCESS); - - VkBufferDeviceAddressInfoEXT bufferDeviceAddressInfo = { VK_STRUCTURE_TYPE_BUFFER_DEVICE_ADDRESS_INFO_EXT }; - bufferDeviceAddressInfo.buffer = bufInfo.Buffer; - TEST(g_vkGetBufferDeviceAddressKHR != nullptr); - VkDeviceAddress addr = g_vkGetBufferDeviceAddressKHR(g_hDevice, &bufferDeviceAddressInfo); - TEST(addr != 0); - - vmaDestroyBuffer(g_hAllocator, bufInfo.Buffer, bufInfo.Allocation); - } -} - -static void TestMemoryPriority() -{ - wprintf(L"Test memory priority\n"); - - assert(VK_EXT_memory_priority_enabled); - - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 0x10000; - bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT; - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - allocCreateInfo.priority = 1.f; - - for(uint32_t testIndex = 0; testIndex < 2; ++testIndex) - { - // 1st is placed, 2nd is dedicated. - if(testIndex == 1) - allocCreateInfo.flags |= VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; - - BufferInfo bufInfo = {}; - VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, - &bufInfo.Buffer, &bufInfo.Allocation, nullptr); - TEST(res == VK_SUCCESS); - - // There is nothing we can do to validate the priority. - - vmaDestroyBuffer(g_hAllocator, bufInfo.Buffer, bufInfo.Allocation); - } -} - static void BenchmarkAlgorithms(FILE* file) { wprintf(L"Benchmark algorithms\n"); @@ -4345,27 +3547,27 @@ static void TestPool_Benchmark( return sum + allocSize.Probability; }); - VkBufferCreateInfo bufferTemplateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufferTemplateInfo.size = 256; // Whatever. - bufferTemplateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; + VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; + bufferInfo.size = 256; // Whatever. + bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - VkImageCreateInfo imageTemplateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - imageTemplateInfo.imageType = VK_IMAGE_TYPE_2D; - imageTemplateInfo.extent.width = 256; // Whatever. - imageTemplateInfo.extent.height = 256; // Whatever. - imageTemplateInfo.extent.depth = 1; - imageTemplateInfo.mipLevels = 1; - imageTemplateInfo.arrayLayers = 1; - imageTemplateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - imageTemplateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; // LINEAR if CPU memory. - imageTemplateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; - imageTemplateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // TRANSFER_SRC if CPU memory. - imageTemplateInfo.samples = VK_SAMPLE_COUNT_1_BIT; + VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; + imageInfo.imageType = VK_IMAGE_TYPE_2D; + imageInfo.extent.width = 256; // Whatever. + imageInfo.extent.height = 256; // Whatever. + imageInfo.extent.depth = 1; + imageInfo.mipLevels = 1; + imageInfo.arrayLayers = 1; + imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; + imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; // LINEAR if CPU memory. + imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; + imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // TRANSFER_SRC if CPU memory. + imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; uint32_t bufferMemoryTypeBits = UINT32_MAX; { VkBuffer dummyBuffer; - VkResult res = vkCreateBuffer(g_hDevice, &bufferTemplateInfo, g_Allocs, &dummyBuffer); + VkResult res = vkCreateBuffer(g_hDevice, &bufferInfo, g_Allocs, &dummyBuffer); TEST(res == VK_SUCCESS); VkMemoryRequirements memReq; @@ -4378,7 +3580,7 @@ static void TestPool_Benchmark( uint32_t imageMemoryTypeBits = UINT32_MAX; { VkImage dummyImage; - VkResult res = vkCreateImage(g_hDevice, &imageTemplateInfo, g_Allocs, &dummyImage); + VkResult res = vkCreateImage(g_hDevice, &imageInfo, g_Allocs, &dummyImage); TEST(res == VK_SUCCESS); VkMemoryRequirements memReq; @@ -4406,67 +3608,32 @@ static void TestPool_Benchmark( TEST(0); VmaPoolCreateInfo poolCreateInfo = {}; + poolCreateInfo.memoryTypeIndex = 0; poolCreateInfo.minBlockCount = 1; poolCreateInfo.maxBlockCount = 1; poolCreateInfo.blockSize = config.PoolSize; poolCreateInfo.frameInUseCount = 1; - const VkPhysicalDeviceMemoryProperties* memProps = nullptr; - vmaGetMemoryProperties(g_hAllocator, &memProps); + VmaAllocationCreateInfo dummyAllocCreateInfo = {}; + dummyAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; + vmaFindMemoryTypeIndex(g_hAllocator, memoryTypeBits, &dummyAllocCreateInfo, &poolCreateInfo.memoryTypeIndex); - VmaPool pool = VK_NULL_HANDLE; - VkResult res; - // Loop over memory types because we sometimes allocate a big block here, - // while the most eligible DEVICE_LOCAL heap may be only 256 MB on some GPUs. - while(memoryTypeBits) - { - VmaAllocationCreateInfo dummyAllocCreateInfo = {}; - dummyAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - vmaFindMemoryTypeIndex(g_hAllocator, memoryTypeBits, &dummyAllocCreateInfo, &poolCreateInfo.memoryTypeIndex); - - const uint32_t heapIndex = memProps->memoryTypes[poolCreateInfo.memoryTypeIndex].heapIndex; - // Protection against validation layer error when trying to allocate a block larger than entire heap size, - // which may be only 256 MB on some platforms. - if(poolCreateInfo.blockSize * poolCreateInfo.minBlockCount < memProps->memoryHeaps[heapIndex].size) - { - res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool); - if(res == VK_SUCCESS) - break; - } - memoryTypeBits &= ~(1u << poolCreateInfo.memoryTypeIndex); - } - TEST(pool); + VmaPool pool; + VkResult res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool); + TEST(res == VK_SUCCESS); // Start time measurement - after creating pool and initializing data structures. time_point timeBeg = std::chrono::high_resolution_clock::now(); //////////////////////////////////////////////////////////////////////////////// // ThreadProc - auto ThreadProc = [&config, allocationSizeProbabilitySum, pool]( + auto ThreadProc = [&]( PoolTestThreadResult* outThreadResult, uint32_t randSeed, HANDLE frameStartEvent, HANDLE frameEndEvent) -> void { RandomNumberGenerator threadRand{randSeed}; - VkResult res = VK_SUCCESS; - - VkBufferCreateInfo bufferInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufferInfo.size = 256; // Whatever. - bufferInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - - VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - imageInfo.imageType = VK_IMAGE_TYPE_2D; - imageInfo.extent.width = 256; // Whatever. - imageInfo.extent.height = 256; // Whatever. - imageInfo.extent.depth = 1; - imageInfo.mipLevels = 1; - imageInfo.arrayLayers = 1; - imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL; // LINEAR if CPU memory. - imageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; - imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; // TRANSFER_SRC if CPU memory. - imageInfo.samples = VK_SAMPLE_COUNT_1_BIT; outThreadResult->AllocationTimeMin = duration::max(); outThreadResult->AllocationTimeSum = duration::zero(); @@ -4483,62 +3650,16 @@ static void TestPool_Benchmark( struct Item { - VkDeviceSize BufferSize = 0; - VkExtent2D ImageSize = { 0, 0 }; - VkBuffer Buf = VK_NULL_HANDLE; - VkImage Image = VK_NULL_HANDLE; - VmaAllocation Alloc = VK_NULL_HANDLE; + VkDeviceSize BufferSize; + VkExtent2D ImageSize; + VkBuffer Buf; + VkImage Image; + VmaAllocation Alloc; - Item() { } - Item(Item&& src) : - BufferSize(src.BufferSize), ImageSize(src.ImageSize), Buf(src.Buf), Image(src.Image), Alloc(src.Alloc) - { - src.BufferSize = 0; - src.ImageSize = {0, 0}; - src.Buf = VK_NULL_HANDLE; - src.Image = VK_NULL_HANDLE; - src.Alloc = VK_NULL_HANDLE; - } - Item(const Item& src) = delete; - ~Item() - { - DestroyResources(); - } - Item& operator=(Item&& src) - { - if(&src != this) - { - DestroyResources(); - BufferSize = src.BufferSize; ImageSize = src.ImageSize; - Buf = src.Buf; Image = src.Image; Alloc = src.Alloc; - src.BufferSize = 0; - src.ImageSize = {0, 0}; - src.Buf = VK_NULL_HANDLE; - src.Image = VK_NULL_HANDLE; - src.Alloc = VK_NULL_HANDLE; - } - return *this; - } - Item& operator=(const Item& src) = delete; - void DestroyResources() - { - if(Buf) - { - assert(Image == VK_NULL_HANDLE); - vmaDestroyBuffer(g_hAllocator, Buf, Alloc); - Buf = VK_NULL_HANDLE; - } - else - { - vmaDestroyImage(g_hAllocator, Image, Alloc); - Image = VK_NULL_HANDLE; - } - Alloc = VK_NULL_HANDLE; - } VkDeviceSize CalcSizeBytes() const { return BufferSize + - 4ull * ImageSize.width * ImageSize.height; + ImageSize.width * ImageSize.height * 4; } }; std::vector unusedItems, usedItems; @@ -4580,13 +3701,11 @@ static void TestPool_Benchmark( } } - unusedItems.push_back(std::move(item)); + unusedItems.push_back(item); } auto Allocate = [&](Item& item) -> VkResult { - assert(item.Buf == VK_NULL_HANDLE && item.Image == VK_NULL_HANDLE && item.Alloc == VK_NULL_HANDLE); - VmaAllocationCreateInfo allocCreateInfo = {}; allocCreateInfo.pool = pool; allocCreateInfo.flags = VMA_ALLOCATION_CREATE_CAN_BECOME_LOST_BIT | @@ -4595,14 +3714,8 @@ static void TestPool_Benchmark( if(item.BufferSize) { bufferInfo.size = item.BufferSize; - VkResult res = VK_SUCCESS; - { - PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult); - res = vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocCreateInfo, &item.Buf, &item.Alloc, nullptr); - } - if(res == VK_SUCCESS) - SetDebugUtilsObjectName(VK_OBJECT_TYPE_BUFFER, (uint64_t)item.Buf, "TestPool_Benchmark_Buffer"); - return res; + PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult); + return vmaCreateBuffer(g_hAllocator, &bufferInfo, &allocCreateInfo, &item.Buf, &item.Alloc, nullptr); } else { @@ -4610,14 +3723,8 @@ static void TestPool_Benchmark( imageInfo.extent.width = item.ImageSize.width; imageInfo.extent.height = item.ImageSize.height; - VkResult res = VK_SUCCESS; - { - PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult); - res = vmaCreateImage(g_hAllocator, &imageInfo, &allocCreateInfo, &item.Image, &item.Alloc, nullptr); - } - if(res == VK_SUCCESS) - SetDebugUtilsObjectName(VK_OBJECT_TYPE_IMAGE, (uint64_t)item.Image, "TestPool_Benchmark_Image"); - return res; + PoolAllocationTimeRegisterObj timeRegisterObj(*outThreadResult); + return vmaCreateImage(g_hAllocator, &imageInfo, &allocCreateInfo, &item.Image, &item.Alloc, nullptr); } }; @@ -4632,10 +3739,8 @@ static void TestPool_Benchmark( for(size_t i = 0; i < bufsToMakeUnused; ++i) { size_t index = threadRand.Generate() % usedItems.size(); - auto it = usedItems.begin() + index; - Item item = std::move(*it); - usedItems.erase(it); - unusedItems.push_back(std::move(item)); + unusedItems.push_back(usedItems[index]); + usedItems.erase(usedItems.begin() + index); } // Determine which bufs we want to use in this frame. @@ -4646,19 +3751,15 @@ static void TestPool_Benchmark( while(usedBufCount < usedItems.size()) { size_t index = threadRand.Generate() % usedItems.size(); - auto it = usedItems.begin() + index; - Item item = std::move(*it); - usedItems.erase(it); - unusedItems.push_back(std::move(item)); + unusedItems.push_back(usedItems[index]); + usedItems.erase(usedItems.begin() + index); } // Move some unused to used. while(usedBufCount > usedItems.size()) { size_t index = threadRand.Generate() % unusedItems.size(); - auto it = unusedItems.begin() + index; - Item item = std::move(*it); - unusedItems.erase(it); - usedItems.push_back(std::move(item)); + usedItems.push_back(unusedItems[index]); + unusedItems.erase(unusedItems.begin() + index); } uint32_t touchExistingCount = 0; @@ -4677,7 +3778,8 @@ static void TestPool_Benchmark( ++outThreadResult->AllocationCount; if(res != VK_SUCCESS) { - assert(item.Alloc == VK_NULL_HANDLE && item.Buf == VK_NULL_HANDLE && item.Image == VK_NULL_HANDLE); + item.Alloc = VK_NULL_HANDLE; + item.Buf = VK_NULL_HANDLE; ++outThreadResult->FailedAllocationCount; outThreadResult->FailedAllocationTotalSize += item.CalcSizeBytes(); ++createFailedCount; @@ -4698,9 +3800,14 @@ static void TestPool_Benchmark( // Destroy. { PoolDeallocationTimeRegisterObj timeRegisterObj(*outThreadResult); - item.DestroyResources(); + if(item.Buf) + vmaDestroyBuffer(g_hAllocator, item.Buf, item.Alloc); + else + vmaDestroyImage(g_hAllocator, item.Image, item.Alloc); ++outThreadResult->DeallocationCount; } + item.Alloc = VK_NULL_HANDLE; + item.Buf = VK_NULL_HANDLE; ++outThreadResult->LostAllocationCount; outThreadResult->LostAllocationTotalSize += item.CalcSizeBytes(); @@ -4711,7 +3818,6 @@ static void TestPool_Benchmark( // Creation failed. if(res != VK_SUCCESS) { - TEST(item.Alloc == VK_NULL_HANDLE && item.Buf == VK_NULL_HANDLE && item.Image == VK_NULL_HANDLE); ++outThreadResult->FailedAllocationCount; outThreadResult->FailedAllocationTotalSize += item.CalcSizeBytes(); ++createFailedCount; @@ -4738,13 +3844,19 @@ static void TestPool_Benchmark( for(size_t i = usedItems.size(); i--; ) { PoolDeallocationTimeRegisterObj timeRegisterObj(*outThreadResult); - usedItems[i].DestroyResources(); + if(usedItems[i].Buf) + vmaDestroyBuffer(g_hAllocator, usedItems[i].Buf, usedItems[i].Alloc); + else + vmaDestroyImage(g_hAllocator, usedItems[i].Image, usedItems[i].Alloc); ++outThreadResult->DeallocationCount; } for(size_t i = unusedItems.size(); i--; ) { PoolDeallocationTimeRegisterObj timeRegisterOb(*outThreadResult); - unusedItems[i].DestroyResources(); + if(unusedItems[i].Buf) + vmaDestroyBuffer(g_hAllocator, unusedItems[i].Buf, unusedItems[i].Alloc); + else + vmaDestroyImage(g_hAllocator, unusedItems[i].Image, unusedItems[i].Alloc); ++outThreadResult->DeallocationCount; } }; @@ -5032,103 +4144,13 @@ static void TestMemoryUsage() } } -static uint32_t FindDeviceCoherentMemoryTypeBits() -{ - VkPhysicalDeviceMemoryProperties memProps; - vkGetPhysicalDeviceMemoryProperties(g_hPhysicalDevice, &memProps); - - uint32_t memTypeBits = 0; - for(uint32_t i = 0; i < memProps.memoryTypeCount; ++i) - { - if(memProps.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD) - memTypeBits |= 1u << i; - } - return memTypeBits; -} - -static void TestDeviceCoherentMemory() -{ - if(!VK_AMD_device_coherent_memory_enabled) - return; - - uint32_t deviceCoherentMemoryTypeBits = FindDeviceCoherentMemoryTypeBits(); - // Extension is enabled, feature is enabled, and the device still doesn't support any such memory type? - // OK then, so it's just fake! - if(deviceCoherentMemoryTypeBits == 0) - return; - - wprintf(L"Testing device coherent memory...\n"); - - // 1. Try to allocate buffer from a memory type that is DEVICE_COHERENT. - - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 0x10000; - bufCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.flags = VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT; - allocCreateInfo.requiredFlags = VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD; - - AllocInfo alloc = {}; - VmaAllocationInfo allocInfo = {}; - VkResult res = vmaCreateBuffer(g_hAllocator, &bufCreateInfo, &allocCreateInfo, &alloc.m_Buffer, &alloc.m_Allocation, &allocInfo); - - // Make sure it succeeded and was really created in such memory type. - TEST(res == VK_SUCCESS); - TEST((1u << allocInfo.memoryType) & deviceCoherentMemoryTypeBits); - - alloc.Destroy(); - - // 2. Try to create a pool in such memory type. - { - VmaPoolCreateInfo poolCreateInfo = {}; - - res = vmaFindMemoryTypeIndex(g_hAllocator, UINT32_MAX, &allocCreateInfo, &poolCreateInfo.memoryTypeIndex); - TEST(res == VK_SUCCESS); - TEST((1u << poolCreateInfo.memoryTypeIndex) & deviceCoherentMemoryTypeBits); - - VmaPool pool = VK_NULL_HANDLE; - res = vmaCreatePool(g_hAllocator, &poolCreateInfo, &pool); - TEST(res == VK_SUCCESS); - - vmaDestroyPool(g_hAllocator, pool); - } - - // 3. Try the same with a local allocator created without VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT. - - VmaAllocatorCreateInfo allocatorCreateInfo = {}; - SetAllocatorCreateInfo(allocatorCreateInfo); - allocatorCreateInfo.flags &= ~VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT; - - VmaAllocator localAllocator = VK_NULL_HANDLE; - res = vmaCreateAllocator(&allocatorCreateInfo, &localAllocator); - TEST(res == VK_SUCCESS && localAllocator); - - res = vmaCreateBuffer(localAllocator, &bufCreateInfo, &allocCreateInfo, &alloc.m_Buffer, &alloc.m_Allocation, &allocInfo); - - // Make sure it failed. - TEST(res != VK_SUCCESS && !alloc.m_Buffer && !alloc.m_Allocation); - - // 4. Try to find memory type. - { - uint32_t memTypeIndex = UINT_MAX; - res = vmaFindMemoryTypeIndex(localAllocator, UINT32_MAX, &allocCreateInfo, &memTypeIndex); - TEST(res != VK_SUCCESS); - } - - vmaDestroyAllocator(localAllocator); -} - static void TestBudget() { wprintf(L"Testing budget...\n"); - static const VkDeviceSize BUF_SIZE = 10ull * 1024 * 1024; + static const VkDeviceSize BUF_SIZE = 100ull * 1024 * 1024; static const uint32_t BUF_COUNT = 4; - const VkPhysicalDeviceMemoryProperties* memProps = {}; - vmaGetMemoryProperties(g_hAllocator, &memProps); - for(uint32_t testIndex = 0; testIndex < 2; ++testIndex) { vmaSetCurrentFrameIndex(g_hAllocator, ++g_FrameIndex); @@ -5136,10 +4158,8 @@ static void TestBudget() VmaBudget budgetBeg[VK_MAX_MEMORY_HEAPS] = {}; vmaGetBudget(g_hAllocator, budgetBeg); - for(uint32_t i = 0; i < memProps->memoryHeapCount; ++i) + for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i) { - TEST(budgetBeg[i].budget > 0); - TEST(budgetBeg[i].budget <= memProps->memoryHeaps[i].size); TEST(budgetBeg[i].allocationBytes <= budgetBeg[i].blockBytes); } @@ -5187,7 +4207,7 @@ static void TestBudget() vmaGetBudget(g_hAllocator, budgetEnd); // CHECK - for(uint32_t i = 0; i < memProps->memoryHeapCount; ++i) + for(uint32_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i) { TEST(budgetEnd[i].allocationBytes <= budgetEnd[i].blockBytes); if(i == heapIndex) @@ -5207,87 +4227,6 @@ static void TestBudget() } } -static void TestAliasing() -{ - wprintf(L"Testing aliasing...\n"); - - /* - This is just a simple test, more like a code sample to demonstrate it's possible. - */ - - // A 512x512 texture to be sampled. - VkImageCreateInfo img1CreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - img1CreateInfo.imageType = VK_IMAGE_TYPE_2D; - img1CreateInfo.extent.width = 512; - img1CreateInfo.extent.height = 512; - img1CreateInfo.extent.depth = 1; - img1CreateInfo.mipLevels = 10; - img1CreateInfo.arrayLayers = 1; - img1CreateInfo.format = VK_FORMAT_R8G8B8A8_SRGB; - img1CreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - img1CreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - img1CreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - img1CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; - - // A full screen texture to be used as color attachment. - VkImageCreateInfo img2CreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - img2CreateInfo.imageType = VK_IMAGE_TYPE_2D; - img2CreateInfo.extent.width = 1920; - img2CreateInfo.extent.height = 1080; - img2CreateInfo.extent.depth = 1; - img2CreateInfo.mipLevels = 1; - img2CreateInfo.arrayLayers = 1; - img2CreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - img2CreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - img2CreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; - img2CreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; - img2CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; - - VkImage img1 = VK_NULL_HANDLE; - ERR_GUARD_VULKAN(vkCreateImage(g_hDevice, &img1CreateInfo, g_Allocs, &img1)); - VkImage img2 = VK_NULL_HANDLE; - ERR_GUARD_VULKAN(vkCreateImage(g_hDevice, &img2CreateInfo, g_Allocs, &img2)); - - VkMemoryRequirements img1MemReq = {}; - vkGetImageMemoryRequirements(g_hDevice, img1, &img1MemReq); - VkMemoryRequirements img2MemReq = {}; - vkGetImageMemoryRequirements(g_hDevice, img2, &img2MemReq); - - VkMemoryRequirements finalMemReq = {}; - finalMemReq.size = std::max(img1MemReq.size, img2MemReq.size); - finalMemReq.alignment = std::max(img1MemReq.alignment, img2MemReq.alignment); - finalMemReq.memoryTypeBits = img1MemReq.memoryTypeBits & img2MemReq.memoryTypeBits; - if(finalMemReq.memoryTypeBits != 0) - { - wprintf(L" size: max(%llu, %llu) = %llu\n", - img1MemReq.size, img2MemReq.size, finalMemReq.size); - wprintf(L" alignment: max(%llu, %llu) = %llu\n", - img1MemReq.alignment, img2MemReq.alignment, finalMemReq.alignment); - wprintf(L" memoryTypeBits: %u & %u = %u\n", - img1MemReq.memoryTypeBits, img2MemReq.memoryTypeBits, finalMemReq.memoryTypeBits); - - VmaAllocationCreateInfo allocCreateInfo = {}; - allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - - VmaAllocation alloc = VK_NULL_HANDLE; - ERR_GUARD_VULKAN(vmaAllocateMemory(g_hAllocator, &finalMemReq, &allocCreateInfo, &alloc, nullptr)); - - ERR_GUARD_VULKAN(vmaBindImageMemory(g_hAllocator, alloc, img1)); - ERR_GUARD_VULKAN(vmaBindImageMemory(g_hAllocator, alloc, img2)); - - // You can use img1, img2 here, but not at the same time! - - vmaFreeMemory(g_hAllocator, alloc); - } - else - { - wprintf(L" Textures cannot alias!\n"); - } - - vkDestroyImage(g_hDevice, img2, g_Allocs); - vkDestroyImage(g_hDevice, img1, g_Allocs); -} - static void TestMapping() { wprintf(L"Testing mapping...\n"); @@ -5775,16 +4714,16 @@ static void PerformCustomPoolTest(FILE* file) config.TotalItemCount = config.UsedItemCountMax * 10; config.UsedItemCountMin = config.UsedItemCountMax * 80 / 100; + g_MemoryAliasingWarningEnabled = false; PoolTestResult result = {}; TestPool_Benchmark(result, config); + g_MemoryAliasingWarningEnabled = true; WritePoolTestResult(file, "Code desc", "Test desc", config, result); } static void PerformMainTests(FILE* file) { - wprintf(L"MAIN TESTS:\n"); - uint32_t repeatCount = 1; if(ConfigType >= CONFIG_TYPE_MAXIMUM) repeatCount = 3; @@ -6050,8 +4989,6 @@ static void PerformMainTests(FILE* file) static void PerformPoolTests(FILE* file) { - wprintf(L"POOL TESTS:\n"); - const size_t AVG_RESOURCES_PER_POOL = 300; uint32_t repeatCount = 1; @@ -6264,7 +5201,9 @@ static void PerformPoolTests(FILE* file) printf("%s #%u\n", testDescription, (uint32_t)repeat); PoolTestResult result{}; + g_MemoryAliasingWarningEnabled = false; TestPool_Benchmark(result, config); + g_MemoryAliasingWarningEnabled = true; WritePoolTestResult(file, CODE_DESCRIPTION, testDescription, config, result); } } @@ -6525,7 +5464,6 @@ void Test() // # Simple tests TestBasics(); - TestAllocationVersusResourceSize(); //TestGpuData(); // Not calling this because it's just testing the testing environment. #if VMA_DEBUG_MARGIN TestDebugMargin(); @@ -6538,9 +5476,7 @@ void Test() TestAllocationsInitialization(); #endif TestMemoryUsage(); - TestDeviceCoherentMemory(); TestBudget(); - TestAliasing(); TestMapping(); TestDeviceLocalMapped(); TestMappingMultithreaded(); @@ -6551,11 +5487,6 @@ void Test() BasicTestBuddyAllocator(); BasicTestAllocatePages(); - if(VK_KHR_buffer_device_address_enabled) - TestBufferDeviceAddress(); - if(VK_EXT_memory_priority_enabled) - TestMemoryPriority(); - { FILE* file; fopen_s(&file, "Algorithms.csv", "w"); @@ -6568,8 +5499,6 @@ void Test() TestDefragmentationFull(); TestDefragmentationWholePool(); TestDefragmentationGpu(); - TestDefragmentationIncrementalBasic(); - TestDefragmentationIncrementalComplex(); // # Detailed tests FILE* file; @@ -6586,7 +5515,7 @@ void Test() fclose(file); - wprintf(L"Done, all PASSED.\n"); + wprintf(L"Done.\n"); } #endif // #ifdef _WIN32 diff --git a/third_party/vkmemalloc/src/Tests.h b/third_party/vkmemalloc/src/Tests.h index 01cf1a8750..df793185bb 100644 --- a/third_party/vkmemalloc/src/Tests.h +++ b/third_party/vkmemalloc/src/Tests.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaReplay/CMakeLists.txt b/third_party/vkmemalloc/src/VmaReplay/CMakeLists.txt deleted file mode 100644 index a53f9c9c79..0000000000 --- a/third_party/vkmemalloc/src/VmaReplay/CMakeLists.txt +++ /dev/null @@ -1,24 +0,0 @@ -# This file will only be executed if VMA_BUILD_REPLAY is set to ON - -if (WIN32) - set(VMA_REPLAY_SOURCE_FILES - Common.cpp - Constants.cpp - VmaReplay.cpp - VmaUsage.cpp - ) - - add_executable(VmaReplay ${VMA_REPLAY_SOURCE_FILES}) - - # Enable multithreaded compiling - target_compile_options(VmaReplay PRIVATE "/MP") - - target_link_libraries( - VmaReplay - - PRIVATE - Vulkan::Vulkan - ) -else() - message(STATUS "VmaReplay is not supported on Linux") -endif() diff --git a/third_party/vkmemalloc/src/VmaReplay/Common.cpp b/third_party/vkmemalloc/src/VmaReplay/Common.cpp index e7880a9321..104d390c47 100644 --- a/third_party/vkmemalloc/src/VmaReplay/Common.cpp +++ b/third_party/vkmemalloc/src/VmaReplay/Common.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaReplay/Common.h b/third_party/vkmemalloc/src/VmaReplay/Common.h index e42b47b048..5d52e5c164 100644 --- a/third_party/vkmemalloc/src/VmaReplay/Common.h +++ b/third_party/vkmemalloc/src/VmaReplay/Common.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaReplay/Constants.cpp b/third_party/vkmemalloc/src/VmaReplay/Constants.cpp index 6d2b15a0ca..a7dc27dfb7 100644 --- a/third_party/vkmemalloc/src/VmaReplay/Constants.cpp +++ b/third_party/vkmemalloc/src/VmaReplay/Constants.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaReplay/Constants.h b/third_party/vkmemalloc/src/VmaReplay/Constants.h index 03b76ff90d..4a7479c774 100644 --- a/third_party/vkmemalloc/src/VmaReplay/Constants.h +++ b/third_party/vkmemalloc/src/VmaReplay/Constants.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -37,7 +37,7 @@ enum CMD_LINE_OPT CMD_LINE_OPT_USER_DATA, CMD_LINE_OPT_VK_KHR_DEDICATED_ALLOCATION, CMD_LINE_OPT_VK_EXT_MEMORY_BUDGET, - CMD_LINE_OPT_VK_LAYER_KHRONOS_VALIDATION, + CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION, CMD_LINE_OPT_MEM_STATS, CMD_LINE_OPT_DUMP_STATS_AFTER_LINE, CMD_LINE_OPT_DEFRAGMENT_AFTER_LINE, diff --git a/third_party/vkmemalloc/src/VmaReplay/VmaReplay.cpp b/third_party/vkmemalloc/src/VmaReplay/VmaReplay.cpp index d6b23c09f1..a8f474cd19 100644 --- a/third_party/vkmemalloc/src/VmaReplay/VmaReplay.cpp +++ b/third_party/vkmemalloc/src/VmaReplay/VmaReplay.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2018-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2018-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -670,9 +670,8 @@ static uint32_t g_PhysicalDeviceIndex = 0; static RangeSequence g_LineRanges; static bool g_UserDataEnabled = true; static bool g_MemStatsEnabled = false; -VULKAN_EXTENSION_REQUEST g_VK_LAYER_KHRONOS_validation = VULKAN_EXTENSION_REQUEST::DEFAULT; -VULKAN_EXTENSION_REQUEST g_VK_EXT_memory_budget_request = VULKAN_EXTENSION_REQUEST::DEFAULT; -VULKAN_EXTENSION_REQUEST g_VK_AMD_device_coherent_memory_request = VULKAN_EXTENSION_REQUEST::DEFAULT; +VULKAN_EXTENSION_REQUEST g_VK_LAYER_LUNARG_standard_validation = VULKAN_EXTENSION_REQUEST::DEFAULT; +VULKAN_EXTENSION_REQUEST g_VK_EXT_memory_budget_request = VULKAN_EXTENSION_REQUEST::DEFAULT; struct StatsAfterLineEntry { @@ -809,8 +808,7 @@ static void VKAPI_CALL AllocateDeviceMemoryCallback( VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, - VkDeviceSize size, - void* pUserData) + VkDeviceSize size) { g_Statistics->RegisterDeviceMemoryAllocation(memoryType, size); } @@ -820,8 +818,7 @@ static void VKAPI_CALL FreeDeviceMemoryCallback( VmaAllocator allocator, uint32_t memoryType, VkDeviceMemory memory, - VkDeviceSize size, - void* pUserData) + VkDeviceSize size) { // Nothing. } @@ -1087,9 +1084,8 @@ private: Extension_VK_KHR_dedicated_allocation, Extension_VK_KHR_bind_memory2, Extension_VK_EXT_memory_budget, - Extension_VK_AMD_device_coherent_memory, Macro_VMA_DEBUG_ALWAYS_DEDICATED_MEMORY, - Macro_VMA_MIN_ALIGNMENT, + Macro_VMA_DEBUG_ALIGNMENT, Macro_VMA_DEBUG_MARGIN, Macro_VMA_DEBUG_INITIALIZE_ALLOCATIONS, Macro_VMA_DEBUG_DETECT_CORRUPTION, @@ -1222,8 +1218,6 @@ bool ConfigurationParser::Parse(LineSplit& lineSplit) SetOption(currLineNumber, OPTION::Extension_VK_KHR_bind_memory2, csvSplit.GetRange(2)); else if(StrRangeEq(subOptionName, "VK_EXT_memory_budget")) SetOption(currLineNumber, OPTION::Extension_VK_EXT_memory_budget, csvSplit.GetRange(2)); - else if(StrRangeEq(subOptionName, "VK_AMD_device_coherent_memory")) - SetOption(currLineNumber, OPTION::Extension_VK_AMD_device_coherent_memory, csvSplit.GetRange(2)); else printf("Line %zu: Unrecognized configuration option.\n", currLineNumber); } @@ -1237,8 +1231,8 @@ bool ConfigurationParser::Parse(LineSplit& lineSplit) const StrRange subOptionName = csvSplit.GetRange(1); if(StrRangeEq(subOptionName, "VMA_DEBUG_ALWAYS_DEDICATED_MEMORY")) SetOption(currLineNumber, OPTION::Macro_VMA_DEBUG_ALWAYS_DEDICATED_MEMORY, csvSplit.GetRange(2)); - else if(StrRangeEq(subOptionName, "VMA_MIN_ALIGNMENT") || StrRangeEq(subOptionName, "VMA_DEBUG_ALIGNMENT")) - SetOption(currLineNumber, OPTION::Macro_VMA_MIN_ALIGNMENT, csvSplit.GetRange(2)); + else if(StrRangeEq(subOptionName, "VMA_DEBUG_ALIGNMENT")) + SetOption(currLineNumber, OPTION::Macro_VMA_DEBUG_ALIGNMENT, csvSplit.GetRange(2)); else if(StrRangeEq(subOptionName, "VMA_DEBUG_MARGIN")) SetOption(currLineNumber, OPTION::Macro_VMA_DEBUG_MARGIN, csvSplit.GetRange(2)); else if(StrRangeEq(subOptionName, "VMA_DEBUG_INITIALIZE_ALLOCATIONS")) @@ -1494,16 +1488,53 @@ void ConfigurationParser::CompareMemProps( //////////////////////////////////////////////////////////////////////////////// // class Player -static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation"; +static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_LUNARG_standard_validation"; -static VkBool32 VKAPI_PTR MyDebugReportCallback( - VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, - VkDebugUtilsMessageTypeFlagsEXT messageTypes, - const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData) +static const bool g_MemoryAliasingWarningEnabled = false; + +static VKAPI_ATTR VkBool32 VKAPI_CALL MyDebugReportCallback( + VkDebugReportFlagsEXT flags, + VkDebugReportObjectTypeEXT objectType, + uint64_t object, + size_t location, + int32_t messageCode, + const char* pLayerPrefix, + const char* pMessage, + void* pUserData) { - assert(pCallbackData && pCallbackData->pMessageIdName && pCallbackData->pMessage); - printf("%s \xBA %s\n", pCallbackData->pMessageIdName, pCallbackData->pMessage); + // "Non-linear image 0xebc91 is aliased with linear buffer 0xeb8e4 which may indicate a bug." + if(!g_MemoryAliasingWarningEnabled && flags == VK_DEBUG_REPORT_WARNING_BIT_EXT && + (strstr(pMessage, " is aliased with non-linear ") || strstr(pMessage, " is aliased with linear "))) + { + return VK_FALSE; + } + + // Ignoring because when VK_KHR_dedicated_allocation extension is enabled, + // vkGetBufferMemoryRequirements2KHR function is used instead, while Validation + // Layer seems to be unaware of it. + if (strstr(pMessage, "but vkGetBufferMemoryRequirements() has not been called on that buffer") != nullptr) + { + return VK_FALSE; + } + if (strstr(pMessage, "but vkGetImageMemoryRequirements() has not been called on that image") != nullptr) + { + return VK_FALSE; + } + + /* + "Mapping an image with layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL can result in undefined behavior if this memory is used by the device. Only GENERAL or PREINITIALIZED should be used." + Ignoring because we map entire VkDeviceMemory blocks, where different types of + images and buffers may end up together, especially on GPUs with unified memory + like Intel. + */ + if(strstr(pMessage, "Mapping an image with layout") != nullptr && + strstr(pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) + { + return VK_FALSE; + } + + printf("%s \xBA %s\n", pLayerPrefix, pMessage); + return VK_FALSE; } @@ -1572,9 +1603,10 @@ private: const VkPhysicalDeviceProperties* m_DevProps = nullptr; const VkPhysicalDeviceMemoryProperties* m_MemProps = nullptr; - PFN_vkCreateDebugUtilsMessengerEXT m_vkCreateDebugUtilsMessengerEXT = nullptr; - PFN_vkDestroyDebugUtilsMessengerEXT m_vkDestroyDebugUtilsMessengerEXT = nullptr; - VkDebugUtilsMessengerEXT m_DebugUtilsMessenger = VK_NULL_HANDLE; + PFN_vkCreateDebugReportCallbackEXT m_pvkCreateDebugReportCallbackEXT; + PFN_vkDebugReportMessageEXT m_pvkDebugReportMessageEXT; + PFN_vkDestroyDebugReportCallbackEXT m_pvkDestroyDebugReportCallbackEXT; + VkDebugReportCallbackEXT m_hCallback; uint32_t m_VmaFrameIndex = 0; @@ -1621,7 +1653,6 @@ private: int InitVulkan(); void FinalizeVulkan(); void RegisterDebugCallbacks(); - void UnregisterDebugCallbacks(); // If parmeter count doesn't match, issues warning and returns false. bool ValidateFunctionParameterCount(size_t lineNumber, const CsvSplit& csvSplit, size_t expectedParamCount, bool lastUnbound); @@ -1981,7 +2012,7 @@ int Player::InitVulkan() IsLayerSupported(instanceLayerProps.data(), instanceLayerProps.size(), VALIDATION_LAYER_NAME); bool validationLayersEnabled = false; - switch(g_VK_LAYER_KHRONOS_validation) + switch(g_VK_LAYER_LUNARG_standard_validation) { case VULKAN_EXTENSION_REQUEST::DISABLED: break; @@ -2016,10 +2047,10 @@ int Player::InitVulkan() if(validationLayersEnabled) { instanceLayers.push_back(VALIDATION_LAYER_NAME); + enabledInstanceExtensions.push_back("VK_EXT_debug_report"); } bool VK_KHR_get_physical_device_properties2_enabled = false; - bool VK_EXT_debug_utils_enabled = false; for(const auto& extensionProperties : availableInstanceExtensions) { if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0) @@ -2027,14 +2058,6 @@ int Player::InitVulkan() enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); VK_KHR_get_physical_device_properties2_enabled = true; } - else if(strcmp(extensionProperties.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) - { - if(validationLayersEnabled) - { - enabledInstanceExtensions.push_back("VK_EXT_debug_utils"); - VK_EXT_debug_utils_enabled = true; - } - } } VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO }; @@ -2058,7 +2081,7 @@ int Player::InitVulkan() return RESULT_ERROR_VULKAN; } - if(VK_EXT_debug_utils_enabled) + if(validationLayersEnabled) { RegisterDebugCallbacks(); } @@ -2200,11 +2223,6 @@ int Player::InitVulkan() default: assert(0); } - if(g_VK_AMD_device_coherent_memory_request == VULKAN_EXTENSION_REQUEST::ENABLED) - { - printf("WARNING: AMD_device_coherent_memory requested but not currently supported by the player.\n"); - } - if(m_MemoryBudgetEnabled) { enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME); @@ -2359,7 +2377,11 @@ void Player::FinalizeVulkan() m_Device = nullptr; } - UnregisterDebugCallbacks(); + if(m_pvkDestroyDebugReportCallbackEXT && m_hCallback != VK_NULL_HANDLE) + { + m_pvkDestroyDebugReportCallbackEXT(m_VulkanInstance, m_hCallback, nullptr); + m_hCallback = VK_NULL_HANDLE; + } if(m_VulkanInstance != VK_NULL_HANDLE) { @@ -2370,41 +2392,29 @@ void Player::FinalizeVulkan() void Player::RegisterDebugCallbacks() { - static const VkDebugUtilsMessageSeverityFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY = - //VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | - //VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; - static const VkDebugUtilsMessageTypeFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_TYPE = - VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; + m_pvkCreateDebugReportCallbackEXT = + reinterpret_cast + (vkGetInstanceProcAddr(m_VulkanInstance, "vkCreateDebugReportCallbackEXT")); + m_pvkDebugReportMessageEXT = + reinterpret_cast + (vkGetInstanceProcAddr(m_VulkanInstance, "vkDebugReportMessageEXT")); + m_pvkDestroyDebugReportCallbackEXT = + reinterpret_cast + (vkGetInstanceProcAddr(m_VulkanInstance, "vkDestroyDebugReportCallbackEXT")); + assert(m_pvkCreateDebugReportCallbackEXT); + assert(m_pvkDebugReportMessageEXT); + assert(m_pvkDestroyDebugReportCallbackEXT); - m_vkCreateDebugUtilsMessengerEXT = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr( - m_VulkanInstance, "vkCreateDebugUtilsMessengerEXT"); - m_vkDestroyDebugUtilsMessengerEXT = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr( - m_VulkanInstance, "vkDestroyDebugUtilsMessengerEXT"); - assert(m_vkCreateDebugUtilsMessengerEXT); - assert(m_vkDestroyDebugUtilsMessengerEXT); + VkDebugReportCallbackCreateInfoEXT callbackCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT }; + callbackCreateInfo.flags = //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | + VK_DEBUG_REPORT_ERROR_BIT_EXT | + VK_DEBUG_REPORT_WARNING_BIT_EXT | + VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT /*| + VK_DEBUG_REPORT_DEBUG_BIT_EXT*/; + callbackCreateInfo.pfnCallback = &MyDebugReportCallback; - VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT }; - messengerCreateInfo.messageSeverity = DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY; - messengerCreateInfo.messageType = DEBUG_UTILS_MESSENGER_MESSAGE_TYPE; - messengerCreateInfo.pfnUserCallback = MyDebugReportCallback; - VkResult res = m_vkCreateDebugUtilsMessengerEXT(m_VulkanInstance, &messengerCreateInfo, nullptr, &m_DebugUtilsMessenger); - if(res != VK_SUCCESS) - { - printf("ERROR: vkCreateDebugUtilsMessengerEXT failed (%d)\n", res); - m_DebugUtilsMessenger = VK_NULL_HANDLE; - } -} - -void Player::UnregisterDebugCallbacks() -{ - if(m_DebugUtilsMessenger) - { - m_vkDestroyDebugUtilsMessengerEXT(m_VulkanInstance, m_DebugUtilsMessenger, nullptr); - } + VkResult res = m_pvkCreateDebugReportCallbackEXT(m_VulkanInstance, &callbackCreateInfo, nullptr, &m_hCallback); + assert(res == VK_SUCCESS); } void Player::Defragment() @@ -3683,8 +3693,7 @@ void Player::ExecuteResizeAllocation(size_t lineNumber, const CsvSplit& csvSplit const auto it = m_Allocations.find(origPtr); if(it != m_Allocations.end()) { - // Do nothing - the function was deprecated and has been removed. - //vmaResizeAllocation(m_Allocator, it->second.allocation, newSize); + vmaResizeAllocation(m_Allocator, it->second.allocation, newSize); UpdateMemStats(); } else @@ -3971,7 +3980,7 @@ static void PrintCommandLineSyntax() " --PhysicalDevice - Choice of Vulkan physical device. Default: 0.\n" " --UserData - 0 to disable or 1 to enable setting pUserData during playback.\n" " Default is 1. Affects both creation of buffers and images, as well as calls to vmaSetAllocationUserData.\n" - " --VK_LAYER_KHRONOS_validation - 0 to disable or 1 to enable validation layers.\n" + " --VK_LAYER_LUNARG_standard_validation - 0 to disable or 1 to enable validation layers.\n" " By default the layers are silently enabled if available.\n" " --VK_EXT_memory_budget - 0 to disable or 1 to enable this extension.\n" " By default the extension is silently enabled if available.\n" @@ -4194,7 +4203,7 @@ static int main2(int argc, char** argv) cmdLineParser.RegisterOpt(CMD_LINE_OPT_PHYSICAL_DEVICE, "PhysicalDevice", true); cmdLineParser.RegisterOpt(CMD_LINE_OPT_USER_DATA, "UserData", true); cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_EXT_MEMORY_BUDGET, "VK_EXT_memory_budget", true); - cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_LAYER_KHRONOS_VALIDATION, VALIDATION_LAYER_NAME, true); + cmdLineParser.RegisterOpt(CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION, VALIDATION_LAYER_NAME, true); cmdLineParser.RegisterOpt(CMD_LINE_OPT_MEM_STATS, "MemStats", true); cmdLineParser.RegisterOpt(CMD_LINE_OPT_DUMP_STATS_AFTER_LINE, "DumpStatsAfterLine", true); cmdLineParser.RegisterOpt(CMD_LINE_OPT_DEFRAGMENT_AFTER_LINE, "DefragmentAfterLine", true); @@ -4268,12 +4277,12 @@ static int main2(int argc, char** argv) } } break; - case CMD_LINE_OPT_VK_LAYER_KHRONOS_VALIDATION: + case CMD_LINE_OPT_VK_LAYER_LUNARG_STANDARD_VALIDATION: { bool newValue; if(StrRangeToBool(StrRange(cmdLineParser.GetParameter()), newValue)) { - g_VK_LAYER_KHRONOS_validation = newValue ? + g_VK_LAYER_LUNARG_standard_validation = newValue ? VULKAN_EXTENSION_REQUEST::ENABLED : VULKAN_EXTENSION_REQUEST::DISABLED; } diff --git a/third_party/vkmemalloc/src/VmaReplay/VmaUsage.cpp b/third_party/vkmemalloc/src/VmaReplay/VmaUsage.cpp index 6353bcf7ca..ee781cc217 100644 --- a/third_party/vkmemalloc/src/VmaReplay/VmaUsage.cpp +++ b/third_party/vkmemalloc/src/VmaReplay/VmaUsage.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaReplay/VmaUsage.h b/third_party/vkmemalloc/src/VmaReplay/VmaUsage.h index 22ba27e420..1c96761b52 100644 --- a/third_party/vkmemalloc/src/VmaReplay/VmaUsage.h +++ b/third_party/vkmemalloc/src/VmaReplay/VmaUsage.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -45,6 +45,6 @@ #pragma warning(disable: 4189) // local variable is initialized but not referenced #pragma warning(disable: 4324) // structure was padded due to alignment specifier -#include "../../include/vk_mem_alloc.h" +#include "../vk_mem_alloc.h" #pragma warning(pop) diff --git a/third_party/vkmemalloc/src/VmaUsage.cpp b/third_party/vkmemalloc/src/VmaUsage.cpp index cd2d783b42..6dc7d7be1b 100644 --- a/third_party/vkmemalloc/src/VmaUsage.cpp +++ b/third_party/vkmemalloc/src/VmaUsage.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal diff --git a/third_party/vkmemalloc/src/VmaUsage.h b/third_party/vkmemalloc/src/VmaUsage.h index 52c0513e8b..5d82451953 100644 --- a/third_party/vkmemalloc/src/VmaUsage.h +++ b/third_party/vkmemalloc/src/VmaUsage.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -48,15 +48,18 @@ include all public interface declarations. Example: */ //#define VMA_HEAVY_ASSERT(expr) assert(expr) +//#define VMA_USE_STL_CONTAINERS 1 //#define VMA_DEDICATED_ALLOCATION 0 //#define VMA_DEBUG_MARGIN 16 //#define VMA_DEBUG_DETECT_CORRUPTION 1 +//#define VMA_DEBUG_INITIALIZE_ALLOCATIONS 1 +//#define VMA_RECORDING_ENABLED 1 //#define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY 256 //#define VMA_USE_STL_SHARED_MUTEX 0 +//#define VMA_DEBUG_GLOBAL_MUTEX 1 //#define VMA_MEMORY_BUDGET 0 -#define VMA_VULKAN_VERSION 1002000 // Vulkan 1.2 -//#define VMA_VULKAN_VERSION 1001000 // Vulkan 1.1 +#define VMA_VULKAN_VERSION 1001000 // Vulkan 1.1 //#define VMA_VULKAN_VERSION 1000000 // Vulkan 1.0 /* @@ -80,10 +83,9 @@ include all public interface declarations. Example: #pragma clang diagnostic ignored "-Wunused-private-field" #pragma clang diagnostic ignored "-Wunused-parameter" #pragma clang diagnostic ignored "-Wmissing-field-initializers" - #pragma clang diagnostic ignored "-Wnullability-completeness" #endif -#include "../include/vk_mem_alloc.h" +#include "vk_mem_alloc.h" #ifdef __clang__ #pragma clang diagnostic pop @@ -93,4 +95,4 @@ include all public interface declarations. Example: #pragma warning(pop) #endif -#endif +#endif \ No newline at end of file diff --git a/third_party/vkmemalloc/src/VulkanSample.cpp b/third_party/vkmemalloc/src/VulkanSample.cpp index 0d07cfbb1a..e9872d5e4b 100644 --- a/third_party/vkmemalloc/src/VulkanSample.cpp +++ b/third_party/vkmemalloc/src/VulkanSample.cpp @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -27,51 +27,33 @@ #include "VmaUsage.h" #include "Common.h" #include -#include - -#pragma comment(lib, "shlwapi.lib") static const char* const SHADER_PATH1 = "./"; static const char* const SHADER_PATH2 = "../bin/"; static const wchar_t* const WINDOW_CLASS_NAME = L"VULKAN_MEMORY_ALLOCATOR_SAMPLE"; -static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation"; -static const char* const APP_TITLE_A = "Vulkan Memory Allocator Sample 2.4.0"; -static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 2.4.0"; +static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_LUNARG_standard_validation"; +static const char* const APP_TITLE_A = "Vulkan Memory Allocator Sample 2.3.0"; +static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 2.3.0"; static const bool VSYNC = true; static const uint32_t COMMAND_BUFFER_COUNT = 2; static void* const CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA = (void*)(intptr_t)43564544; static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = true; -enum class ExitCode : int -{ - GPUList = 2, - Help = 1, - Success = 0, - RuntimeError = -1, - CommandLineError = -2, -}; - VkPhysicalDevice g_hPhysicalDevice; VkDevice g_hDevice; VmaAllocator g_hAllocator; VkInstance g_hVulkanInstance; +bool g_MemoryAliasingWarningEnabled = true; -bool g_EnableValidationLayer = true; -bool VK_KHR_get_memory_requirements2_enabled = false; -bool VK_KHR_get_physical_device_properties2_enabled = false; -bool VK_KHR_dedicated_allocation_enabled = false; -bool VK_KHR_bind_memory2_enabled = false; -bool VK_EXT_memory_budget_enabled = false; -bool VK_AMD_device_coherent_memory_enabled = false; -bool VK_KHR_buffer_device_address_enabled = false; -bool VK_EXT_memory_priority_enabled = false; -bool VK_EXT_debug_utils_enabled = false; +static bool g_EnableValidationLayer = true; +static bool VK_KHR_get_memory_requirements2_enabled = false; +static bool VK_KHR_get_physical_device_properties2_enabled = false; +static bool VK_KHR_dedicated_allocation_enabled = false; +static bool VK_KHR_bind_memory2_enabled = false; +static bool VK_EXT_memory_budget_enabled = false; bool g_SparseBindingEnabled = false; -// # Pointers to functions from extensions -PFN_vkGetBufferDeviceAddressKHR g_vkGetBufferDeviceAddressKHR; - static HINSTANCE g_hAppInstance; static HWND g_hWnd; static LONG g_SizeX = 1280, g_SizeY = 720; @@ -106,18 +88,10 @@ static VkSurfaceCapabilitiesKHR g_SurfaceCapabilities; static std::vector g_SurfaceFormats; static std::vector g_PresentModes; -static const VkDebugUtilsMessageSeverityFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY = - //VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | - //VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; -static const VkDebugUtilsMessageTypeFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_TYPE = - VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | - VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; -static PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT_Func; -static PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT_Func; -static PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT_Func; +static PFN_vkCreateDebugReportCallbackEXT g_pvkCreateDebugReportCallbackEXT; +static PFN_vkDebugReportMessageEXT g_pvkDebugReportMessageEXT; +static PFN_vkDestroyDebugReportCallbackEXT g_pvkDestroyDebugReportCallbackEXT; +static VkDebugReportCallbackEXT g_hCallback; static VkQueue g_hGraphicsQueue; VkQueue g_hSparseBindingQueue; @@ -190,75 +164,6 @@ static const VkAllocationCallbacks g_CpuAllocationCallbacks = { const VkAllocationCallbacks* g_Allocs; -struct GPUSelection -{ - uint32_t Index = UINT32_MAX; - std::wstring Substring; -}; - -class VulkanUsage -{ -public: - void Init(); - ~VulkanUsage(); - void PrintPhysicalDeviceList() const; - // If failed, returns VK_NULL_HANDLE. - VkPhysicalDevice SelectPhysicalDevice(const GPUSelection& GPUSelection) const; - -private: - VkDebugUtilsMessengerEXT m_DebugUtilsMessenger = VK_NULL_HANDLE; - - void RegisterDebugCallbacks(); - static bool IsLayerSupported(const VkLayerProperties* pProps, size_t propCount, const char* pLayerName); -}; - -struct CommandLineParameters -{ - bool m_Help = false; - bool m_List = false; - GPUSelection m_GPUSelection; - - bool Parse(int argc, wchar_t** argv) - { - for(int i = 1; i < argc; ++i) - { - if(_wcsicmp(argv[i], L"-h") == 0 || _wcsicmp(argv[i], L"--Help") == 0) - { - m_Help = true; - } - else if(_wcsicmp(argv[i], L"-l") == 0 || _wcsicmp(argv[i], L"--List") == 0) - { - m_List = true; - } - else if((_wcsicmp(argv[i], L"-g") == 0 || _wcsicmp(argv[i], L"--GPU") == 0) && i + 1 < argc) - { - m_GPUSelection.Substring = argv[i + 1]; - ++i; - } - else if((_wcsicmp(argv[i], L"-i") == 0 || _wcsicmp(argv[i], L"--GPUIndex") == 0) && i + 1 < argc) - { - m_GPUSelection.Index = _wtoi(argv[i + 1]); - ++i; - } - else - return false; - } - return true; - } -} g_CommandLineParameters; - -void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, const char* name) -{ - if(vkSetDebugUtilsObjectNameEXT_Func == nullptr) - return; - - VkDebugUtilsObjectNameInfoEXT info = { VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT }; - info.objectType = type; - info.objectHandle = handle; - info.pObjectName = name; - vkSetDebugUtilsObjectNameEXT_Func(g_hDevice, &info); -} - void BeginSingleTimeCommands() { VkCommandBufferBeginInfo cmdBufBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO }; @@ -296,37 +201,67 @@ void LoadShader(std::vector& out, const char* fileName) out.clear(); } -static VkBool32 VKAPI_PTR MyDebugReportCallback( - VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, - VkDebugUtilsMessageTypeFlagsEXT messageTypes, - const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, - void* pUserData) +VKAPI_ATTR VkBool32 VKAPI_CALL MyDebugReportCallback( + VkDebugReportFlagsEXT flags, + VkDebugReportObjectTypeEXT objectType, + uint64_t object, + size_t location, + int32_t messageCode, + const char* pLayerPrefix, + const char* pMessage, + void* pUserData) { - assert(pCallbackData && pCallbackData->pMessageIdName && pCallbackData->pMessage); - - switch(messageSeverity) + // "Non-linear image 0xebc91 is aliased with linear buffer 0xeb8e4 which may indicate a bug." + if(!g_MemoryAliasingWarningEnabled && flags == VK_DEBUG_REPORT_WARNING_BIT_EXT && + (strstr(pMessage, " is aliased with non-linear ") || strstr(pMessage, " is aliased with linear "))) { - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT: + return VK_FALSE; + } + + // Ignoring because when VK_KHR_dedicated_allocation extension is enabled, + // vkGetBufferMemoryRequirements2KHR function is used instead, while Validation + // Layer seems to be unaware of it. + if (strstr(pMessage, "but vkGetBufferMemoryRequirements() has not been called on that buffer") != nullptr) + { + return VK_FALSE; + } + if (strstr(pMessage, "but vkGetImageMemoryRequirements() has not been called on that image") != nullptr) + { + return VK_FALSE; + } + + /* + "Mapping an image with layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL can result in undefined behavior if this memory is used by the device. Only GENERAL or PREINITIALIZED should be used." + Ignoring because we map entire VkDeviceMemory blocks, where different types of + images and buffers may end up together, especially on GPUs with unified memory + like Intel. + */ + if(strstr(pMessage, "Mapping an image with layout") != nullptr && + strstr(pMessage, "can result in undefined behavior if this memory is used by the device") != nullptr) + { + return VK_FALSE; + } + + switch(flags) + { + case VK_DEBUG_REPORT_WARNING_BIT_EXT: SetConsoleColor(CONSOLE_COLOR::WARNING); break; - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT: + case VK_DEBUG_REPORT_ERROR_BIT_EXT: SetConsoleColor(CONSOLE_COLOR::ERROR_); break; - case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT: - SetConsoleColor(CONSOLE_COLOR::NORMAL); - break; - default: // VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT + default: SetConsoleColor(CONSOLE_COLOR::INFO); } - printf("%s \xBA %s\n", pCallbackData->pMessageIdName, pCallbackData->pMessage); + printf("%s \xBA %s\n", pLayerPrefix, pMessage); SetConsoleColor(CONSOLE_COLOR::NORMAL); - if(messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT || - messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) + if(flags == VK_DEBUG_REPORT_WARNING_BIT_EXT || + flags == VK_DEBUG_REPORT_ERROR_BIT_EXT) { - OutputDebugStringA(pCallbackData->pMessage); + OutputDebugStringA(pMessage); OutputDebugStringA("\n"); } @@ -381,223 +316,6 @@ static VkExtent2D ChooseSwapExtent() return result; } -static constexpr uint32_t GetVulkanApiVersion() -{ -#if VMA_VULKAN_VERSION == 1002000 - return VK_API_VERSION_1_2; -#elif VMA_VULKAN_VERSION == 1001000 - return VK_API_VERSION_1_1; -#elif VMA_VULKAN_VERSION == 1000000 - return VK_API_VERSION_1_0; -#else -#error Invalid VMA_VULKAN_VERSION. - return UINT32_MAX; -#endif -} - -void VulkanUsage::Init() -{ - g_hAppInstance = (HINSTANCE)GetModuleHandle(NULL); - - if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS) - { - g_Allocs = &g_CpuAllocationCallbacks; - } - - uint32_t instanceLayerPropCount = 0; - ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, nullptr) ); - std::vector instanceLayerProps(instanceLayerPropCount); - if(instanceLayerPropCount > 0) - { - ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, instanceLayerProps.data()) ); - } - - if(g_EnableValidationLayer) - { - if(IsLayerSupported(instanceLayerProps.data(), instanceLayerProps.size(), VALIDATION_LAYER_NAME) == false) - { - wprintf(L"Layer \"%hs\" not supported.", VALIDATION_LAYER_NAME); - g_EnableValidationLayer = false; - } - } - - uint32_t availableInstanceExtensionCount = 0; - ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, nullptr) ); - std::vector availableInstanceExtensions(availableInstanceExtensionCount); - if(availableInstanceExtensionCount > 0) - { - ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, availableInstanceExtensions.data()) ); - } - - std::vector enabledInstanceExtensions; - enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); - enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); - - std::vector instanceLayers; - if(g_EnableValidationLayer) - { - instanceLayers.push_back(VALIDATION_LAYER_NAME); - } - - for(const auto& extensionProperties : availableInstanceExtensions) - { - if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0) - { - if(GetVulkanApiVersion() == VK_API_VERSION_1_0) - { - enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); - VK_KHR_get_physical_device_properties2_enabled = true; - } - } - else if(strcmp(extensionProperties.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0) - { - enabledInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); - VK_EXT_debug_utils_enabled = true; - } - } - - VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO }; - appInfo.pApplicationName = APP_TITLE_A; - appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); - appInfo.pEngineName = "Adam Sawicki Engine"; - appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); - appInfo.apiVersion = GetVulkanApiVersion(); - - VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO }; - instInfo.pApplicationInfo = &appInfo; - instInfo.enabledExtensionCount = static_cast(enabledInstanceExtensions.size()); - instInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data(); - instInfo.enabledLayerCount = static_cast(instanceLayers.size()); - instInfo.ppEnabledLayerNames = instanceLayers.data(); - - wprintf(L"Vulkan API version used: "); - switch(appInfo.apiVersion) - { - case VK_API_VERSION_1_0: wprintf(L"1.0\n"); break; - case VK_API_VERSION_1_1: wprintf(L"1.1\n"); break; - case VK_API_VERSION_1_2: wprintf(L"1.2\n"); break; - default: assert(0); - } - - ERR_GUARD_VULKAN( vkCreateInstance(&instInfo, g_Allocs, &g_hVulkanInstance) ); - - if(VK_EXT_debug_utils_enabled) - { - RegisterDebugCallbacks(); - } -} - -VulkanUsage::~VulkanUsage() -{ - if(m_DebugUtilsMessenger) - { - vkDestroyDebugUtilsMessengerEXT_Func(g_hVulkanInstance, m_DebugUtilsMessenger, g_Allocs); - } - - if(g_hVulkanInstance) - { - vkDestroyInstance(g_hVulkanInstance, g_Allocs); - g_hVulkanInstance = VK_NULL_HANDLE; - } -} - -void VulkanUsage::PrintPhysicalDeviceList() const -{ - uint32_t deviceCount = 0; - ERR_GUARD_VULKAN(vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, nullptr)); - std::vector physicalDevices(deviceCount); - if(deviceCount > 0) - { - ERR_GUARD_VULKAN(vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, physicalDevices.data())); - } - - for(size_t i = 0; i < deviceCount; ++i) - { - VkPhysicalDeviceProperties props = {}; - vkGetPhysicalDeviceProperties(physicalDevices[i], &props); - wprintf(L"Physical device %zu: %hs\n", i, props.deviceName); - } -} - -VkPhysicalDevice VulkanUsage::SelectPhysicalDevice(const GPUSelection& GPUSelection) const -{ - uint32_t deviceCount = 0; - ERR_GUARD_VULKAN(vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, nullptr)); - std::vector physicalDevices(deviceCount); - if(deviceCount > 0) - { - ERR_GUARD_VULKAN(vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, physicalDevices.data())); - } - - if(GPUSelection.Index != UINT32_MAX) - { - // Cannot specify both index and name. - if(!GPUSelection.Substring.empty()) - { - return VK_NULL_HANDLE; - } - - return GPUSelection.Index < deviceCount ? physicalDevices[GPUSelection.Index] : VK_NULL_HANDLE; - } - - if(!GPUSelection.Substring.empty()) - { - VkPhysicalDevice result = VK_NULL_HANDLE; - std::wstring name; - for(uint32_t i = 0; i < deviceCount; ++i) - { - VkPhysicalDeviceProperties props = {}; - vkGetPhysicalDeviceProperties(physicalDevices[i], &props); - if(ConvertCharsToUnicode(&name, props.deviceName, strlen(props.deviceName), CP_UTF8) && - StrStrI(name.c_str(), GPUSelection.Substring.c_str())) - { - // Second matching device found - error. - if(result != VK_NULL_HANDLE) - { - return VK_NULL_HANDLE; - } - // First matching device found. - result = physicalDevices[i]; - } - } - // Found or not, return it. - return result; - } - - // Select first one. - return deviceCount > 0 ? physicalDevices[0] : VK_NULL_HANDLE; -} - -void VulkanUsage::RegisterDebugCallbacks() -{ - vkCreateDebugUtilsMessengerEXT_Func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr( - g_hVulkanInstance, "vkCreateDebugUtilsMessengerEXT"); - vkDestroyDebugUtilsMessengerEXT_Func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr( - g_hVulkanInstance, "vkDestroyDebugUtilsMessengerEXT"); - vkSetDebugUtilsObjectNameEXT_Func = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr( - g_hVulkanInstance, "vkSetDebugUtilsObjectNameEXT"); - assert(vkCreateDebugUtilsMessengerEXT_Func); - assert(vkDestroyDebugUtilsMessengerEXT_Func); - assert(vkSetDebugUtilsObjectNameEXT_Func); - - VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT }; - messengerCreateInfo.messageSeverity = DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY; - messengerCreateInfo.messageType = DEBUG_UTILS_MESSENGER_MESSAGE_TYPE; - messengerCreateInfo.pfnUserCallback = MyDebugReportCallback; - ERR_GUARD_VULKAN( vkCreateDebugUtilsMessengerEXT_Func(g_hVulkanInstance, &messengerCreateInfo, g_Allocs, &m_DebugUtilsMessenger) ); -} - -bool VulkanUsage::IsLayerSupported(const VkLayerProperties* pProps, size_t propCount, const char* pLayerName) -{ - const VkLayerProperties* propsEnd = pProps + propCount; - return std::find_if( - pProps, - propsEnd, - [pLayerName](const VkLayerProperties& prop) -> bool { - return strcmp(pLayerName, prop.layerName) == 0; - }) != propsEnd; -} - struct Vertex { float pos[3]; @@ -860,6 +578,46 @@ struct UniformBufferObject mat4 ModelViewProj; }; +static void RegisterDebugCallbacks() +{ + g_pvkCreateDebugReportCallbackEXT = + reinterpret_cast + (vkGetInstanceProcAddr(g_hVulkanInstance, "vkCreateDebugReportCallbackEXT")); + g_pvkDebugReportMessageEXT = + reinterpret_cast + (vkGetInstanceProcAddr(g_hVulkanInstance, "vkDebugReportMessageEXT")); + g_pvkDestroyDebugReportCallbackEXT = + reinterpret_cast + (vkGetInstanceProcAddr(g_hVulkanInstance, "vkDestroyDebugReportCallbackEXT")); + assert(g_pvkCreateDebugReportCallbackEXT); + assert(g_pvkDebugReportMessageEXT); + assert(g_pvkDestroyDebugReportCallbackEXT); + + VkDebugReportCallbackCreateInfoEXT callbackCreateInfo; + callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT; + callbackCreateInfo.pNext = nullptr; + callbackCreateInfo.flags = //VK_DEBUG_REPORT_INFORMATION_BIT_EXT | + VK_DEBUG_REPORT_ERROR_BIT_EXT | + VK_DEBUG_REPORT_WARNING_BIT_EXT | + VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT /*| + VK_DEBUG_REPORT_DEBUG_BIT_EXT*/; + callbackCreateInfo.pfnCallback = &MyDebugReportCallback; + callbackCreateInfo.pUserData = nullptr; + + ERR_GUARD_VULKAN( g_pvkCreateDebugReportCallbackEXT(g_hVulkanInstance, &callbackCreateInfo, g_Allocs, &g_hCallback) ); +} + +static bool IsLayerSupported(const VkLayerProperties* pProps, size_t propCount, const char* pLayerName) +{ + const VkLayerProperties* propsEnd = pProps + propCount; + return std::find_if( + pProps, + propsEnd, + [pLayerName](const VkLayerProperties& prop) -> bool { + return strcmp(pLayerName, prop.layerName) == 0; + }) != propsEnd; +} + static VkFormat FindSupportedFormat( const std::vector& candidates, VkImageTiling tiling, @@ -1357,404 +1115,74 @@ static void DestroySwapchain(bool destroyActualSwapchain) } } -static void PrintEnabledFeatures() -{ - wprintf(L"Enabled extensions and features:\n"); - wprintf(L"Validation layer: %d\n", g_EnableValidationLayer ? 1 : 0); - wprintf(L"Sparse binding: %d\n", g_SparseBindingEnabled ? 1 : 0); - if(GetVulkanApiVersion() == VK_API_VERSION_1_0) - { - wprintf(L"VK_KHR_get_memory_requirements2: %d\n", VK_KHR_get_memory_requirements2_enabled ? 1 : 0); - wprintf(L"VK_KHR_get_physical_device_properties2: %d\n", VK_KHR_get_physical_device_properties2_enabled ? 1 : 0); - wprintf(L"VK_KHR_dedicated_allocation: %d\n", VK_KHR_dedicated_allocation_enabled ? 1 : 0); - wprintf(L"VK_KHR_bind_memory2: %d\n", VK_KHR_bind_memory2_enabled ? 1 : 0); - } - wprintf(L"VK_EXT_memory_budget: %d\n", VK_EXT_memory_budget_enabled ? 1 : 0); - wprintf(L"VK_AMD_device_coherent_memory: %d\n", VK_AMD_device_coherent_memory_enabled ? 1 : 0); - if(GetVulkanApiVersion() < VK_API_VERSION_1_2) - { - wprintf(L"VK_KHR_buffer_device_address: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0); - } - else - { - wprintf(L"bufferDeviceAddress: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0); - } - wprintf(L"VK_EXT_memory_priority: %d\n", VK_EXT_memory_priority ? 1 : 0); -} - -void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo) -{ - outInfo = {}; - - outInfo.physicalDevice = g_hPhysicalDevice; - outInfo.device = g_hDevice; - outInfo.instance = g_hVulkanInstance; - outInfo.vulkanApiVersion = GetVulkanApiVersion(); - - if(VK_KHR_dedicated_allocation_enabled) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT; - } - if(VK_KHR_bind_memory2_enabled) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT; - } -#if !defined(VMA_MEMORY_BUDGET) || VMA_MEMORY_BUDGET == 1 - if(VK_EXT_memory_budget_enabled && ( - GetVulkanApiVersion() >= VK_API_VERSION_1_1 || VK_KHR_get_physical_device_properties2_enabled)) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; - } -#endif - if(VK_AMD_device_coherent_memory_enabled) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT; - } - if(VK_KHR_buffer_device_address_enabled) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT; - } -#if !defined(VMA_MEMORY_PRIORITY) || VMA_MEMORY_PRIORITY == 1 - if(VK_EXT_memory_priority_enabled) - { - outInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT; - } -#endif - - if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS) - { - outInfo.pAllocationCallbacks = &g_CpuAllocationCallbacks; - } - - // Uncomment to enable recording to CSV file. - /* - static VmaRecordSettings recordSettings = {}; - recordSettings.pFilePath = "VulkanSample.csv"; - outInfo.pRecordSettings = &recordSettings; - */ - - // Uncomment to enable HeapSizeLimit. - /* - static std::array heapSizeLimit; - std::fill(heapSizeLimit.begin(), heapSizeLimit.end(), VK_WHOLE_SIZE); - heapSizeLimit[0] = 512ull * 1024 * 1024; - outInfo.pHeapSizeLimit = heapSizeLimit.data(); - */ -} - -static void PrintPhysicalDeviceProperties(const VkPhysicalDeviceProperties& properties) -{ - wprintf(L"physicalDeviceProperties:\n"); - wprintf(L" driverVersion: 0x%X\n", properties.driverVersion); - wprintf(L" vendorID: 0x%X (%s)\n", properties.vendorID, VendorIDToStr(properties.vendorID)); - wprintf(L" deviceID: 0x%X\n", properties.deviceID); - wprintf(L" deviceType: %u (%s)\n", properties.deviceType, PhysicalDeviceTypeToStr(properties.deviceType)); - wprintf(L" deviceName: %hs\n", properties.deviceName); - wprintf(L" limits:\n"); - wprintf(L" maxMemoryAllocationCount: %u\n", properties.limits.maxMemoryAllocationCount); - wprintf(L" bufferImageGranularity: %llu B\n", properties.limits.bufferImageGranularity); - wprintf(L" nonCoherentAtomSize: %llu B\n", properties.limits.nonCoherentAtomSize); -} - -#if VMA_VULKAN_VERSION >= 1002000 -static void PrintPhysicalDeviceVulkan11Properties(const VkPhysicalDeviceVulkan11Properties& properties) -{ - wprintf(L"physicalDeviceVulkan11Properties:\n"); - std::wstring sizeStr = SizeToStr(properties.maxMemoryAllocationSize); - wprintf(L" maxMemoryAllocationSize: %llu B (%s)\n", properties.maxMemoryAllocationSize, sizeStr.c_str()); -} -static void PrintPhysicalDeviceVulkan12Properties(const VkPhysicalDeviceVulkan12Properties& properties) -{ - wprintf(L"physicalDeviceVulkan12Properties:\n"); - std::wstring str = DriverIDToStr(properties.driverID); - wprintf(L" driverID: %u (%s)\n", properties.driverID, str.c_str()); - wprintf(L" driverName: %hs\n", properties.driverName); - wprintf(L" driverInfo: %hs\n", properties.driverInfo); -} -#endif // #if VMA_VULKAN_VERSION > 1002000 - -static void AddFlagToStr(std::wstring& inout, const wchar_t* flagStr) -{ - if(!inout.empty()) - inout += L", "; - inout += flagStr; -} - -static std::wstring HeapFlagsToStr(VkMemoryHeapFlags flags) -{ - std::wstring result; - if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) - AddFlagToStr(result, L"DEVICE_LOCAL"); - if(flags & VK_MEMORY_HEAP_MULTI_INSTANCE_BIT) - AddFlagToStr(result, L"MULTI_INSTANCE"); - return result; -} - -static std::wstring PropertyFlagsToStr(VkMemoryPropertyFlags flags) -{ - std::wstring result; - if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) - AddFlagToStr(result, L"DEVICE_LOCAL"); - if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) - AddFlagToStr(result, L"HOST_VISIBLE"); - if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) - AddFlagToStr(result, L"HOST_COHERENT"); - if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT) - AddFlagToStr(result, L"HOST_CACHED"); - if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) - AddFlagToStr(result, L"LAZILY_ALLOCATED"); - -#if VMA_VULKAN_VERSION >= 1001000 - if(flags & VK_MEMORY_PROPERTY_PROTECTED_BIT) - AddFlagToStr(result, L"PROTECTED"); -#endif - -#if VK_AMD_device_coherent_memory - if(flags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD) - AddFlagToStr(result, L"DEVICE_COHERENT (AMD)"); - if(flags & VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD) - AddFlagToStr(result, L"DEVICE_UNCACHED (AMD)"); -#endif - - return result; -} - -static void PrintMemoryTypes() -{ - wprintf(L"MEMORY HEAPS:\n"); - const VkPhysicalDeviceMemoryProperties* memProps = nullptr; - vmaGetMemoryProperties(g_hAllocator, &memProps); - - wprintf(L"heapCount=%u, typeCount=%u\n", memProps->memoryHeapCount, memProps->memoryTypeCount); - - std::wstring sizeStr, flagsStr; - for(uint32_t heapIndex = 0; heapIndex < memProps->memoryHeapCount; ++heapIndex) - { - const VkMemoryHeap& heap = memProps->memoryHeaps[heapIndex]; - sizeStr = SizeToStr(heap.size); - flagsStr = HeapFlagsToStr(heap.flags); - wprintf(L"Heap %u: %llu B (%s) %s\n", heapIndex, heap.size, sizeStr.c_str(), flagsStr.c_str()); - - for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex) - { - const VkMemoryType& type = memProps->memoryTypes[typeIndex]; - if(type.heapIndex == heapIndex) - { - flagsStr = PropertyFlagsToStr(type.propertyFlags); - wprintf(L" Type %u: %s\n", typeIndex, flagsStr.c_str()); - } - } - } -} - -#if 0 -template -inline VkDeviceSize MapSum(It beg, It end, MapFunc mapFunc) -{ - VkDeviceSize result = 0; - for(It it = beg; it != end; ++it) - result += mapFunc(*it); - return result; -} -#endif - -static bool CanCreateVertexBuffer(uint32_t allowedMemoryTypeBits) -{ - VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO }; - bufCreateInfo.size = 0x10000; - bufCreateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT; - - VkBuffer buf = VK_NULL_HANDLE; - VkResult res = vkCreateBuffer(g_hDevice, &bufCreateInfo, g_Allocs, &buf); - assert(res == VK_SUCCESS); - - VkMemoryRequirements memReq = {}; - vkGetBufferMemoryRequirements(g_hDevice, buf, &memReq); - - vkDestroyBuffer(g_hDevice, buf, g_Allocs); - - return (memReq.memoryTypeBits & allowedMemoryTypeBits) != 0; -} - -static bool CanCreateOptimalSampledImage(uint32_t allowedMemoryTypeBits) -{ - VkImageCreateInfo imgCreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; - imgCreateInfo.imageType = VK_IMAGE_TYPE_2D; - imgCreateInfo.extent.width = 256; - imgCreateInfo.extent.height = 256; - imgCreateInfo.extent.depth = 1; - imgCreateInfo.mipLevels = 1; - imgCreateInfo.arrayLayers = 1; - imgCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; - imgCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; - imgCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED; - imgCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; - imgCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; - - VkImage img = VK_NULL_HANDLE; - VkResult res = vkCreateImage(g_hDevice, &imgCreateInfo, g_Allocs, &img); - assert(res == VK_SUCCESS); - - VkMemoryRequirements memReq = {}; - vkGetImageMemoryRequirements(g_hDevice, img, &memReq); - - vkDestroyImage(g_hDevice, img, g_Allocs); - - return (memReq.memoryTypeBits & allowedMemoryTypeBits) != 0; -} - -static void PrintMemoryConclusions() -{ - wprintf(L"Conclusions:\n"); - - const VkPhysicalDeviceProperties* props = nullptr; - const VkPhysicalDeviceMemoryProperties* memProps = nullptr; - vmaGetPhysicalDeviceProperties(g_hAllocator, &props); - vmaGetMemoryProperties(g_hAllocator, &memProps); - - const uint32_t heapCount = memProps->memoryHeapCount; - - uint32_t deviceLocalHeapCount = 0; - uint32_t hostVisibleHeapCount = 0; - uint32_t deviceLocalAndHostVisibleHeapCount = 0; - VkDeviceSize deviceLocalHeapSumSize = 0; - VkDeviceSize hostVisibleHeapSumSize = 0; - VkDeviceSize deviceLocalAndHostVisibleHeapSumSize = 0; - - for(uint32_t heapIndex = 0; heapIndex < heapCount; ++heapIndex) - { - const VkMemoryHeap& heap = memProps->memoryHeaps[heapIndex]; - const bool isDeviceLocal = (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0; - bool isHostVisible = false; - for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex) - { - const VkMemoryType& type = memProps->memoryTypes[typeIndex]; - if(type.heapIndex == heapIndex && (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)) - { - isHostVisible = true; - break; - } - } - if(isDeviceLocal) - { - ++deviceLocalHeapCount; - deviceLocalHeapSumSize += heap.size; - } - if(isHostVisible) - { - ++hostVisibleHeapCount; - hostVisibleHeapSumSize += heap.size; - if(isDeviceLocal) - { - ++deviceLocalAndHostVisibleHeapCount; - deviceLocalAndHostVisibleHeapSumSize += heap.size; - } - } - } - - uint32_t hostVisibleNotHostCoherentTypeCount = 0; - uint32_t notDeviceLocalNotHostVisibleTypeCount = 0; - uint32_t amdSpecificTypeCount = 0; - uint32_t lazilyAllocatedTypeCount = 0; - uint32_t allTypeBits = 0; - uint32_t deviceLocalTypeBits = 0; - for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex) - { - const VkMemoryType& type = memProps->memoryTypes[typeIndex]; - allTypeBits |= 1u << typeIndex; - if(type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) - { - deviceLocalTypeBits |= 1u << typeIndex; - } - if((type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) && - (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0) - { - ++hostVisibleNotHostCoherentTypeCount; - } - if((type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0 && - (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) - { - ++notDeviceLocalNotHostVisibleTypeCount; - } - if(type.propertyFlags & (VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD | VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD)) - { - ++amdSpecificTypeCount; - } - if(type.propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT) - { - ++lazilyAllocatedTypeCount; - } - } - - assert(deviceLocalHeapCount > 0); - if(deviceLocalHeapCount == heapCount) - wprintf(L"- All heaps are DEVICE_LOCAL.\n"); - else - wprintf(L"- %u heaps are DEVICE_LOCAL, total %s.\n", deviceLocalHeapCount, SizeToStr(deviceLocalHeapSumSize).c_str()); - - assert(hostVisibleHeapCount > 0); - if(hostVisibleHeapCount == heapCount) - wprintf(L"- All heaps are HOST_VISIBLE.\n"); - else - wprintf(L"- %u heaps are HOST_VISIBLE, total %s.\n", deviceLocalHeapCount, SizeToStr(hostVisibleHeapSumSize).c_str()); - - if(deviceLocalHeapCount < heapCount && hostVisibleHeapCount < heapCount) - { - if(deviceLocalAndHostVisibleHeapCount == 0) - wprintf(L"- No heaps are DEVICE_LOCAL and HOST_VISIBLE.\n"); - if(deviceLocalAndHostVisibleHeapCount == heapCount) - wprintf(L"- All heaps are DEVICE_LOCAL and HOST_VISIBLE.\n"); - else - wprintf(L"- %u heaps are DEVICE_LOCAL and HOST_VISIBLE, total %s.\n", deviceLocalAndHostVisibleHeapCount, SizeToStr(deviceLocalAndHostVisibleHeapSumSize).c_str()); - } - - if(hostVisibleNotHostCoherentTypeCount == 0) - wprintf(L"- No types are HOST_VISIBLE but not HOST_COHERENT.\n"); - else - wprintf(L"- %u types are HOST_VISIBLE but not HOST_COHERENT.\n", hostVisibleNotHostCoherentTypeCount); - - if(notDeviceLocalNotHostVisibleTypeCount == 0) - wprintf(L"- No types are not DEVICE_LOCAL and not HOST_VISIBLE.\n"); - else - wprintf(L"- %u types are not DEVICE_LOCAL and not HOST_VISIBLE.\n", notDeviceLocalNotHostVisibleTypeCount); - - if(amdSpecificTypeCount == 0) - wprintf(L"- No types are AMD-specific DEVICE_COHERENT or DEVICE_UNCACHED.\n"); - else - wprintf(L"- %u types are AMD-specific DEVICE_COHERENT or DEVICE_UNCACHED.\n", amdSpecificTypeCount); - - if(lazilyAllocatedTypeCount == 0) - wprintf(L"- No types are LAZILY_ALLOCATED.\n"); - else - wprintf(L"- %u types are LAZILY_ALLOCATED.\n", lazilyAllocatedTypeCount); - - if(props->vendorID == VENDOR_ID_AMD && - props->deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && - deviceLocalAndHostVisibleHeapSumSize > 256llu * 1024 * 1024) - { - wprintf(L"- AMD Smart Access Memory (SAM) is enabled!\n"); - } - - if(deviceLocalHeapCount < heapCount) - { - const uint32_t nonDeviceLocalTypeBits = ~deviceLocalTypeBits & allTypeBits; - - if(CanCreateVertexBuffer(nonDeviceLocalTypeBits)) - wprintf(L"- A buffer with VERTEX_BUFFER usage can be created in some non-DEVICE_LOCAL type.\n"); - else - wprintf(L"- A buffer with VERTEX_BUFFER usage cannot be created in some non-DEVICE_LOCAL type.\n"); - - if(CanCreateOptimalSampledImage(nonDeviceLocalTypeBits)) - wprintf(L"- An image with OPTIMAL tiling and SAMPLED usage can be created in some non-DEVICE_LOCAL type.\n"); - else - wprintf(L"- An image with OPTIMAL tiling and SAMPLED usage cannot be created in some non-DEVICE_LOCAL type.\n"); - } - - //wprintf(L"\n"); -} - static void InitializeApplication() { + if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS) + { + g_Allocs = &g_CpuAllocationCallbacks; + } + + uint32_t instanceLayerPropCount = 0; + ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, nullptr) ); + std::vector instanceLayerProps(instanceLayerPropCount); + if(instanceLayerPropCount > 0) + { + ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, instanceLayerProps.data()) ); + } + + if(g_EnableValidationLayer == true) + { + if(IsLayerSupported(instanceLayerProps.data(), instanceLayerProps.size(), VALIDATION_LAYER_NAME) == false) + { + printf("Layer \"%s\" not supported.", VALIDATION_LAYER_NAME); + g_EnableValidationLayer = false; + } + } + + uint32_t availableInstanceExtensionCount = 0; + ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, nullptr) ); + std::vector availableInstanceExtensions(availableInstanceExtensionCount); + if(availableInstanceExtensionCount > 0) + { + ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, availableInstanceExtensions.data()) ); + } + + std::vector enabledInstanceExtensions; + enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME); + enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME); + + std::vector instanceLayers; + if(g_EnableValidationLayer == true) + { + instanceLayers.push_back(VALIDATION_LAYER_NAME); + enabledInstanceExtensions.push_back("VK_EXT_debug_report"); + } + + for(const auto& extensionProperties : availableInstanceExtensions) + { + if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0) + { + enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME); + VK_KHR_get_physical_device_properties2_enabled = true; + } + } + + VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO }; + appInfo.pApplicationName = APP_TITLE_A; + appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0); + appInfo.pEngineName = "Adam Sawicki Engine"; + appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0); + appInfo.apiVersion = VMA_VULKAN_VERSION == 1001000 ? VK_API_VERSION_1_1 : VK_API_VERSION_1_0; + + VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO }; + instInfo.pApplicationInfo = &appInfo; + instInfo.enabledExtensionCount = static_cast(enabledInstanceExtensions.size()); + instInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data(); + instInfo.enabledLayerCount = static_cast(instanceLayers.size()); + instInfo.ppEnabledLayerNames = instanceLayers.data(); + + ERR_GUARD_VULKAN( vkCreateInstance(&instInfo, g_Allocs, &g_hVulkanInstance) ); + // Create VkSurfaceKHR. VkWin32SurfaceCreateInfoKHR surfaceInfo = { VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR }; surfaceInfo.hinstance = g_hAppInstance; @@ -1762,122 +1190,29 @@ static void InitializeApplication() VkResult result = vkCreateWin32SurfaceKHR(g_hVulkanInstance, &surfaceInfo, g_Allocs, &g_hSurface); assert(result == VK_SUCCESS); - // Query for device extensions + if(g_EnableValidationLayer == true) + RegisterDebugCallbacks(); - uint32_t physicalDeviceExtensionPropertyCount = 0; - ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties(g_hPhysicalDevice, nullptr, &physicalDeviceExtensionPropertyCount, nullptr) ); - std::vector physicalDeviceExtensionProperties{physicalDeviceExtensionPropertyCount}; - if(physicalDeviceExtensionPropertyCount) - { - ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties( - g_hPhysicalDevice, - nullptr, - &physicalDeviceExtensionPropertyCount, - physicalDeviceExtensionProperties.data()) ); - } + // Find physical device - for(uint32_t i = 0; i < physicalDeviceExtensionPropertyCount; ++i) - { - if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME) == 0) - { - if(GetVulkanApiVersion() == VK_API_VERSION_1_0) - { - VK_KHR_get_memory_requirements2_enabled = true; - } - } - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME) == 0) - { - if(GetVulkanApiVersion() == VK_API_VERSION_1_0) - { - VK_KHR_dedicated_allocation_enabled = true; - } - } - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_BIND_MEMORY_2_EXTENSION_NAME) == 0) - { - if(GetVulkanApiVersion() == VK_API_VERSION_1_0) - { - VK_KHR_bind_memory2_enabled = true; - } - } - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME) == 0) - VK_EXT_memory_budget_enabled = true; - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME) == 0) - VK_AMD_device_coherent_memory_enabled = true; - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME) == 0) - { - if(GetVulkanApiVersion() < VK_API_VERSION_1_2) - { - VK_KHR_buffer_device_address_enabled = true; - } - } - else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME) == 0) - VK_EXT_memory_priority_enabled = true; - } + uint32_t deviceCount = 0; + ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, nullptr) ); + assert(deviceCount > 0); - if(GetVulkanApiVersion() >= VK_API_VERSION_1_2) - VK_KHR_buffer_device_address_enabled = true; // Promoted to core Vulkan 1.2. + std::vector physicalDevices(deviceCount); + ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, physicalDevices.data()) ); + + g_hPhysicalDevice = physicalDevices[0]; // Query for features -#if VMA_VULKAN_VERSION >= 1001000 - VkPhysicalDeviceProperties2 physicalDeviceProperties2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 }; - -#if VMA_VULKAN_VERSION >= 1002000 - // Vulkan spec says structure VkPhysicalDeviceVulkan11Properties is "Provided by VK_VERSION_1_2" - is this a mistake? Assuming not... - VkPhysicalDeviceVulkan11Properties physicalDeviceVulkan11Properties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES }; - VkPhysicalDeviceVulkan12Properties physicalDeviceVulkan12Properties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES }; - PnextChainPushFront(&physicalDeviceProperties2, &physicalDeviceVulkan11Properties); - PnextChainPushFront(&physicalDeviceProperties2, &physicalDeviceVulkan12Properties); -#endif - - vkGetPhysicalDeviceProperties2(g_hPhysicalDevice, &physicalDeviceProperties2); - - PrintPhysicalDeviceProperties(physicalDeviceProperties2.properties); -#if VMA_VULKAN_VERSION >= 1002000 - PrintPhysicalDeviceVulkan11Properties(physicalDeviceVulkan11Properties); - PrintPhysicalDeviceVulkan12Properties(physicalDeviceVulkan12Properties); -#endif - -#else // #if VMA_VULKAN_VERSION >= 1001000 VkPhysicalDeviceProperties physicalDeviceProperties = {}; vkGetPhysicalDeviceProperties(g_hPhysicalDevice, &physicalDeviceProperties); - PrintPhysicalDeviceProperties(physicalDeviceProperties); -#endif // #if VMA_VULKAN_VERSION >= 1001000 + VkPhysicalDeviceFeatures physicalDeviceFeatures = {}; + vkGetPhysicalDeviceFeatures(g_hPhysicalDevice, &physicalDeviceFeatures); - wprintf(L"\n"); - - VkPhysicalDeviceFeatures2 physicalDeviceFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 }; - - VkPhysicalDeviceCoherentMemoryFeaturesAMD physicalDeviceCoherentMemoryFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COHERENT_MEMORY_FEATURES_AMD }; - if(VK_AMD_device_coherent_memory_enabled) - { - PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceCoherentMemoryFeatures); - } - - VkPhysicalDeviceBufferDeviceAddressFeaturesKHR physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_KHR }; - if(VK_KHR_buffer_device_address_enabled) - { - PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceBufferDeviceAddressFeatures); - } - - VkPhysicalDeviceMemoryPriorityFeaturesEXT physicalDeviceMemoryPriorityFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PRIORITY_FEATURES_EXT }; - if(VK_EXT_memory_priority_enabled) - { - PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceMemoryPriorityFeatures); - } - - vkGetPhysicalDeviceFeatures2(g_hPhysicalDevice, &physicalDeviceFeatures); - - g_SparseBindingEnabled = physicalDeviceFeatures.features.sparseBinding != 0; - - // The extension is supported as fake with no real support for this feature? Don't use it. - if(VK_AMD_device_coherent_memory_enabled && !physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory) - VK_AMD_device_coherent_memory_enabled = false; - if(VK_KHR_buffer_device_address_enabled && !physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress) - VK_KHR_buffer_device_address_enabled = false; - if(VK_EXT_memory_priority_enabled && !physicalDeviceMemoryPriorityFeatures.memoryPriority) - VK_EXT_memory_priority_enabled = false; + g_SparseBindingEnabled = physicalDeviceFeatures.sparseBinding != 0; // Find queue family index @@ -1954,80 +1289,106 @@ static void InitializeApplication() ++queueCount; } + VkPhysicalDeviceFeatures deviceFeatures = {}; + //deviceFeatures.fillModeNonSolid = VK_TRUE; + deviceFeatures.samplerAnisotropy = VK_TRUE; + deviceFeatures.sparseBinding = g_SparseBindingEnabled ? VK_TRUE : VK_FALSE; + + // Determine list of device extensions to enable. std::vector enabledDeviceExtensions; enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME); - if(VK_KHR_get_memory_requirements2_enabled) - enabledDeviceExtensions.push_back(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME); - if(VK_KHR_dedicated_allocation_enabled) - enabledDeviceExtensions.push_back(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME); - if(VK_KHR_bind_memory2_enabled) - enabledDeviceExtensions.push_back(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME); - if(VK_EXT_memory_budget_enabled) - enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME); - if(VK_AMD_device_coherent_memory_enabled) - enabledDeviceExtensions.push_back(VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME); - if(VK_KHR_buffer_device_address_enabled && GetVulkanApiVersion() < VK_API_VERSION_1_2) - enabledDeviceExtensions.push_back(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME); - if(VK_EXT_memory_priority_enabled) - enabledDeviceExtensions.push_back(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME); + { + uint32_t propertyCount = 0; + ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties(g_hPhysicalDevice, nullptr, &propertyCount, nullptr) ); - VkPhysicalDeviceFeatures2 deviceFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 }; - deviceFeatures.features.samplerAnisotropy = VK_TRUE; - deviceFeatures.features.sparseBinding = g_SparseBindingEnabled ? VK_TRUE : VK_FALSE; + if(propertyCount) + { + std::vector properties{propertyCount}; + ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties(g_hPhysicalDevice, nullptr, &propertyCount, properties.data()) ); - if(VK_AMD_device_coherent_memory_enabled) - { - physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory = VK_TRUE; - PnextChainPushBack(&deviceFeatures, &physicalDeviceCoherentMemoryFeatures); - } - if(VK_KHR_buffer_device_address_enabled) - { - physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_KHR }; - physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress = VK_TRUE; - PnextChainPushBack(&deviceFeatures, &physicalDeviceBufferDeviceAddressFeatures); - } - if(VK_EXT_memory_priority_enabled) - { - PnextChainPushBack(&deviceFeatures, &physicalDeviceMemoryPriorityFeatures); + for(uint32_t i = 0; i < propertyCount; ++i) + { + if(strcmp(properties[i].extensionName, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME) == 0) + { + enabledDeviceExtensions.push_back(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME); + VK_KHR_get_memory_requirements2_enabled = true; + } + else if(strcmp(properties[i].extensionName, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME) == 0) + { + enabledDeviceExtensions.push_back(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME); + VK_KHR_dedicated_allocation_enabled = true; + } + else if(strcmp(properties[i].extensionName, VK_KHR_BIND_MEMORY_2_EXTENSION_NAME) == 0) + { + enabledDeviceExtensions.push_back(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME); + VK_KHR_bind_memory2_enabled = true; + } + else if(strcmp(properties[i].extensionName, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME) == 0) + { + enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME); + VK_EXT_memory_budget_enabled = true; + } + } + } } VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO }; - deviceCreateInfo.pNext = &deviceFeatures; deviceCreateInfo.enabledLayerCount = 0; deviceCreateInfo.ppEnabledLayerNames = nullptr; deviceCreateInfo.enabledExtensionCount = (uint32_t)enabledDeviceExtensions.size(); deviceCreateInfo.ppEnabledExtensionNames = !enabledDeviceExtensions.empty() ? enabledDeviceExtensions.data() : nullptr; deviceCreateInfo.queueCreateInfoCount = queueCount; deviceCreateInfo.pQueueCreateInfos = queueCreateInfo; + deviceCreateInfo.pEnabledFeatures = &deviceFeatures; ERR_GUARD_VULKAN( vkCreateDevice(g_hPhysicalDevice, &deviceCreateInfo, g_Allocs, &g_hDevice) ); - // Fetch pointers to extension functions - if(VK_KHR_buffer_device_address_enabled) - { - if(GetVulkanApiVersion() >= VK_API_VERSION_1_2) - { - g_vkGetBufferDeviceAddressKHR = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddress"); - } - else if(VK_KHR_buffer_device_address_enabled) - { - g_vkGetBufferDeviceAddressKHR = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR"); - } - assert(g_vkGetBufferDeviceAddressKHR != nullptr); - } - // Create memory allocator VmaAllocatorCreateInfo allocatorInfo = {}; - SetAllocatorCreateInfo(allocatorInfo); - ERR_GUARD_VULKAN( vmaCreateAllocator(&allocatorInfo, &g_hAllocator) ); + allocatorInfo.physicalDevice = g_hPhysicalDevice; + allocatorInfo.device = g_hDevice; + allocatorInfo.instance = g_hVulkanInstance; + allocatorInfo.vulkanApiVersion = appInfo.apiVersion; - PrintMemoryTypes(); - wprintf(L"\n"); - PrintMemoryConclusions(); - wprintf(L"\n"); - PrintEnabledFeatures(); - wprintf(L"\n"); + if(VK_KHR_dedicated_allocation_enabled) + { + allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT; + } + if(VK_KHR_bind_memory2_enabled) + { + allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT; + } +#if !defined(VMA_MEMORY_BUDGET) || VMA_MEMORY_BUDGET == 1 + if(VK_EXT_memory_budget_enabled && VK_KHR_get_physical_device_properties2_enabled) + { + allocatorInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT; + } +#endif + + if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS) + { + allocatorInfo.pAllocationCallbacks = &g_CpuAllocationCallbacks; + } + + // Uncomment to enable recording to CSV file. + /* + { + VmaRecordSettings recordSettings = {}; + recordSettings.pFilePath = "VulkanSample.csv"; + allocatorInfo.pRecordSettings = &recordSettings; + } + */ + + // Uncomment to enable HeapSizeLimit. + /* + std::array heapSizeLimit; + std::fill(heapSizeLimit.begin(), heapSizeLimit.end(), VK_WHOLE_SIZE); + heapSizeLimit[0] = 512ull * 1024 * 1024; + allocatorInfo.pHeapSizeLimit = heapSizeLimit.data(); + */ + + ERR_GUARD_VULKAN( vmaCreateAllocator(&allocatorInfo, &g_hAllocator) ); // Retrieve queues (don't need to be destroyed). @@ -2232,11 +1593,23 @@ static void FinalizeApplication() g_hDevice = nullptr; } + if(g_pvkDestroyDebugReportCallbackEXT && g_hCallback != VK_NULL_HANDLE) + { + g_pvkDestroyDebugReportCallbackEXT(g_hVulkanInstance, g_hCallback, g_Allocs); + g_hCallback = VK_NULL_HANDLE; + } + if(g_hSurface != VK_NULL_HANDLE) { vkDestroySurfaceKHR(g_hVulkanInstance, g_hSurface, g_Allocs); g_hSurface = VK_NULL_HANDLE; } + + if(g_hVulkanInstance != VK_NULL_HANDLE) + { + vkDestroyInstance(g_hVulkanInstance, g_Allocs); + g_hVulkanInstance = VK_NULL_HANDLE; + } } static void PrintAllocatorStats() @@ -2399,18 +1772,6 @@ static void HandlePossibleSizeChange() } } -#define CATCH_PRINT_ERROR(extraCatchCode) \ - catch(const std::exception& ex) \ - { \ - fwprintf(stderr, L"ERROR: %hs\n", ex.what()); \ - extraCatchCode \ - } \ - catch(...) \ - { \ - fwprintf(stderr, L"UNKNOWN ERROR.\n"); \ - extraCatchCode \ - } - static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) @@ -2418,20 +1779,12 @@ static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_CREATE: // This is intentionally assigned here because we are now inside CreateWindow, before it returns. g_hWnd = hWnd; - try - { - InitializeApplication(); - } - CATCH_PRINT_ERROR(return -1;) - //PrintAllocatorStats(); + InitializeApplication(); + PrintAllocatorStats(); return 0; case WM_DESTROY: - try - { - FinalizeApplication(); - } - CATCH_PRINT_ERROR(;) + FinalizeApplication(); PostQuitMessage(0); return 0; @@ -2443,21 +1796,11 @@ static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) case WM_SIZE: if((wParam == SIZE_MAXIMIZED) || (wParam == SIZE_RESTORED)) - { - try - { - HandlePossibleSizeChange(); - } - CATCH_PRINT_ERROR(DestroyWindow(hWnd);) - } + HandlePossibleSizeChange(); return 0; case WM_EXITSIZEMOVE: - try - { - HandlePossibleSizeChange(); - } - CATCH_PRINT_ERROR(DestroyWindow(hWnd);) + HandlePossibleSizeChange(); return 0; case WM_KEYDOWN: @@ -2471,18 +1814,17 @@ static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) { Test(); } - CATCH_PRINT_ERROR(;) + catch(const std::exception& ex) + { + printf("ERROR: %s\n", ex.what()); + } break; case 'S': try { if(g_SparseBindingEnabled) { - try - { - TestSparseBinding(); - } - CATCH_PRINT_ERROR(;) + TestSparseBinding(); } else { @@ -2504,24 +1846,10 @@ static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) return DefWindowProc(hWnd, msg, wParam, lParam); } -static void PrintLogo() +int main() { - wprintf(L"%s\n", APP_TITLE_W); -} + g_hAppInstance = (HINSTANCE)GetModuleHandle(NULL); -static void PrintHelp() -{ - wprintf( - L"Command line syntax:\n" - L"-h, --Help Print this information\n" - L"-l, --List Print list of GPUs\n" - L"-g S, --GPU S Select GPU with name containing S\n" - L"-i N, --GPUIndex N Select GPU index N\n" - ); -} - -int MainWindow() -{ WNDCLASSEX wndClassDesc = { sizeof(WNDCLASSEX) }; wndClassDesc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS; wndClassDesc.hbrBackground = NULL; @@ -2559,51 +1887,11 @@ int MainWindow() DrawFrame(); } - return (int)msg.wParam;; + TEST(g_CpuAllocCount.load() == 0); + + return 0; } -int Main2(int argc, wchar_t** argv) -{ - PrintLogo(); - - if(!g_CommandLineParameters.Parse(argc, argv)) - { - wprintf(L"ERROR: Invalid command line syntax.\n"); - PrintHelp(); - return (int)ExitCode::CommandLineError; - } - - if(g_CommandLineParameters.m_Help) - { - PrintHelp(); - return (int)ExitCode::Help; - } - - VulkanUsage vulkanUsage; - vulkanUsage.Init(); - - if(g_CommandLineParameters.m_List) - { - vulkanUsage.PrintPhysicalDeviceList(); - return (int)ExitCode::GPUList; - } - - g_hPhysicalDevice = vulkanUsage.SelectPhysicalDevice(g_CommandLineParameters.m_GPUSelection); - TEST(g_hPhysicalDevice); - - return MainWindow(); -} - -int wmain(int argc, wchar_t** argv) -{ - try - { - return Main2(argc, argv); - TEST(g_CpuAllocCount.load() == 0); - } - CATCH_PRINT_ERROR(return (int)ExitCode::RuntimeError;) -} - #else // #ifdef _WIN32 #include "VmaUsage.h" diff --git a/third_party/vkmemalloc/include/vk_mem_alloc.h b/third_party/vkmemalloc/src/vk_mem_alloc.h similarity index 82% rename from third_party/vkmemalloc/include/vk_mem_alloc.h rename to third_party/vkmemalloc/src/vk_mem_alloc.h index 852f86ab82..32258b40eb 100644 --- a/third_party/vkmemalloc/include/vk_mem_alloc.h +++ b/third_party/vkmemalloc/src/vk_mem_alloc.h @@ -1,5 +1,5 @@ // -// Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. +// Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal @@ -23,11 +23,15 @@ #ifndef AMD_VULKAN_MEMORY_ALLOCATOR_H #define AMD_VULKAN_MEMORY_ALLOCATOR_H +#ifdef __cplusplus +extern "C" { +#endif + /** \mainpage Vulkan Memory Allocator -Version 3.0.0-development (2021-02-16) +Version 2.3.0 (2019-12-04) -Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved. \n +Copyright (c) 2017-2019 Advanced Micro Devices, Inc. All rights reserved. \n License: MIT Documentation of all members: vk_mem_alloc.h @@ -53,7 +57,6 @@ Documentation of all members: vk_mem_alloc.h - \subpage staying_within_budget - [Querying for budget](@ref staying_within_budget_querying_for_budget) - [Controlling memory usage](@ref staying_within_budget_controlling_memory_usage) - - \subpage resource_aliasing - \subpage custom_memory_pools - [Choosing memory type index](@ref custom_memory_pools_MemTypeIndex) - [Linear allocation algorithm](@ref linear_algorithm) @@ -63,10 +66,10 @@ Documentation of all members: vk_mem_alloc.h - [Ring buffer](@ref linear_algorithm_ring_buffer) - [Buddy allocation algorithm](@ref buddy_algorithm) - \subpage defragmentation - - [Defragmenting CPU memory](@ref defragmentation_cpu) - - [Defragmenting GPU memory](@ref defragmentation_gpu) - - [Additional notes](@ref defragmentation_additional_notes) - - [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm) + - [Defragmenting CPU memory](@ref defragmentation_cpu) + - [Defragmenting GPU memory](@ref defragmentation_gpu) + - [Additional notes](@ref defragmentation_additional_notes) + - [Writing custom allocation algorithm](@ref defragmentation_custom_algorithm) - \subpage lost_allocations - \subpage statistics - [Numeric statistics](@ref statistics_numeric_statistics) @@ -89,8 +92,6 @@ Documentation of all members: vk_mem_alloc.h - [Device memory allocation callbacks](@ref allocation_callbacks) - [Device heap memory limit](@ref heap_memory_limit) - \subpage vk_khr_dedicated_allocation - - \subpage enabling_buffer_device_address - - \subpage vk_amd_device_coherent_memory - \subpage general_considerations - [Thread safety](@ref general_considerations_thread_safety) - [Validation layer warnings](@ref general_considerations_validation_layer_warnings) @@ -122,7 +123,7 @@ To do it properly: -# Include "vk_mem_alloc.h" file in each CPP file where you want to use the library. This includes declarations of all members of the library. --# In exactly one CPP file define following macro before this include. +-# In exacly one CPP file define following macro before this include. It enables also internal definitions. \code @@ -142,42 +143,24 @@ before including these headers (like `WIN32_LEAN_AND_MEAN` or `WINVER` for Windows, `VK_USE_PLATFORM_WIN32_KHR` for Vulkan), you must define them before every `#include` of this library. -You may need to configure the way you import Vulkan functions. - -- By default, VMA assumes you you link statically with Vulkan API. If this is not the case, - `#define VMA_STATIC_VULKAN_FUNCTIONS 0` before `#include` of the VMA implementation and use another way. -- You can `#define VMA_DYNAMIC_VULKAN_FUNCTIONS 1` and make sure `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` globals are defined. - All the remaining Vulkan functions will be fetched automatically. -- Finally, you can provide your own pointers to all Vulkan functions needed by VMA using structure member - VmaAllocatorCreateInfo::pVulkanFunctions, if you fetched them in some custom way e.g. using some loader like [Volk](https://github.com/zeux/volk). - \section quick_start_initialization Initialization At program startup: --# Initialize Vulkan to have `VkPhysicalDevice`, `VkDevice` and `VkInstance` object. +-# Initialize Vulkan to have `VkPhysicalDevice` and `VkDevice` object. -# Fill VmaAllocatorCreateInfo structure and create #VmaAllocator object by calling vmaCreateAllocator(). \code VmaAllocatorCreateInfo allocatorInfo = {}; -allocatorInfo.vulkanApiVersion = VK_API_VERSION_1_2; allocatorInfo.physicalDevice = physicalDevice; allocatorInfo.device = device; -allocatorInfo.instance = instance; VmaAllocator allocator; vmaCreateAllocator(&allocatorInfo, &allocator); \endcode -Only members `physicalDevice`, `device`, `instance` are required. -However, you should inform the library which Vulkan version do you use by setting -VmaAllocatorCreateInfo::vulkanApiVersion and which extensions did you enable -by setting VmaAllocatorCreateInfo::flags (like #VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT for VK_KHR_buffer_device_address). -Otherwise, VMA would use only features of Vulkan 1.0 core with no extensions. - - \section quick_start_resource_allocation Resource allocation When you want to create a buffer or image: @@ -319,7 +302,6 @@ VmaAllocation allocation; vmaCreateBuffer(allocator, &bufferInfo, &allocInfo, &buffer, &allocation, nullptr); \endcode - \section choosing_memory_type_custom_memory_pools Custom memory pools If you allocate from custom memory pool, all the ways of specifying memory @@ -440,10 +422,12 @@ There are some exceptions though, when you should consider mapping memory only f block is migrated by WDDM to system RAM, which degrades performance. It doesn't matter if that particular memory block is actually used by the command buffer being submitted. +- On Mac/MoltenVK there is a known bug - [Issue #175](https://github.com/KhronosGroup/MoltenVK/issues/175) + which requires unmapping before GPU can see updated texture. - Keeping many large memory blocks mapped may impact performance or stability of some debugging tools. \section memory_mapping_cache_control Cache flush and invalidate - + Memory in Vulkan doesn't need to be unmapped before using it on GPU, but unless a memory types has `VK_MEMORY_PROPERTY_HOST_COHERENT_BIT` flag set, you need to manually **invalidate** cache before reading of mapped pointer @@ -452,8 +436,7 @@ Map/unmap operations don't do that automatically. Vulkan provides following functions for this purpose `vkFlushMappedMemoryRanges()`, `vkInvalidateMappedMemoryRanges()`, but this library provides more convenient functions that refer to given allocation object: vmaFlushAllocation(), -vmaInvalidateAllocation(), -or multiple objects at once: vmaFlushAllocations(), vmaInvalidateAllocations(). +vmaInvalidateAllocation(). Regions of memory specified for flush/invalidate must be aligned to `VkPhysicalDeviceLimits::nonCoherentAtomSize`. This is automatically ensured by the library. @@ -495,7 +478,7 @@ vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allo VkMemoryPropertyFlags memFlags; vmaGetMemoryTypeProperties(allocator, allocInfo.memoryType, &memFlags); -if((memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0) +if((memFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0) { // Allocation ended up in mappable memory. You can map it and access it directly. void* mappedData; @@ -530,7 +513,7 @@ VmaAllocation alloc; VmaAllocationInfo allocInfo; vmaCreateBuffer(allocator, &bufCreateInfo, &allocCreateInfo, &buf, &alloc, &allocInfo); -if(allocInfo.pMappedData != nullptr) +if(allocInfo.pUserData != nullptr) { // Allocation ended up in mappable memory. // It's persistently mapped. You can access it directly. @@ -616,114 +599,6 @@ set to more than 0 will try to allocate memory blocks without checking whether t fit within budget. -\page resource_aliasing Resource aliasing (overlap) - -New explicit graphics APIs (Vulkan and Direct3D 12), thanks to manual memory -management, give an opportunity to alias (overlap) multiple resources in the -same region of memory - a feature not available in the old APIs (Direct3D 11, OpenGL). -It can be useful to save video memory, but it must be used with caution. - -For example, if you know the flow of your whole render frame in advance, you -are going to use some intermediate textures or buffers only during a small range of render passes, -and you know these ranges don't overlap in time, you can bind these resources to -the same place in memory, even if they have completely different parameters (width, height, format etc.). - -![Resource aliasing (overlap)](../gfx/Aliasing.png) - -Such scenario is possible using VMA, but you need to create your images manually. -Then you need to calculate parameters of an allocation to be made using formula: - -- allocation size = max(size of each image) -- allocation alignment = max(alignment of each image) -- allocation memoryTypeBits = bitwise AND(memoryTypeBits of each image) - -Following example shows two different images bound to the same place in memory, -allocated to fit largest of them. - -\code -// A 512x512 texture to be sampled. -VkImageCreateInfo img1CreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; -img1CreateInfo.imageType = VK_IMAGE_TYPE_2D; -img1CreateInfo.extent.width = 512; -img1CreateInfo.extent.height = 512; -img1CreateInfo.extent.depth = 1; -img1CreateInfo.mipLevels = 10; -img1CreateInfo.arrayLayers = 1; -img1CreateInfo.format = VK_FORMAT_R8G8B8A8_SRGB; -img1CreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; -img1CreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; -img1CreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; -img1CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; - -// A full screen texture to be used as color attachment. -VkImageCreateInfo img2CreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO }; -img2CreateInfo.imageType = VK_IMAGE_TYPE_2D; -img2CreateInfo.extent.width = 1920; -img2CreateInfo.extent.height = 1080; -img2CreateInfo.extent.depth = 1; -img2CreateInfo.mipLevels = 1; -img2CreateInfo.arrayLayers = 1; -img2CreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; -img2CreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; -img2CreateInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED; -img2CreateInfo.usage = VK_IMAGE_USAGE_SAMPLED_BIT | VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; -img2CreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; - -VkImage img1; -res = vkCreateImage(device, &img1CreateInfo, nullptr, &img1); -VkImage img2; -res = vkCreateImage(device, &img2CreateInfo, nullptr, &img2); - -VkMemoryRequirements img1MemReq; -vkGetImageMemoryRequirements(device, img1, &img1MemReq); -VkMemoryRequirements img2MemReq; -vkGetImageMemoryRequirements(device, img2, &img2MemReq); - -VkMemoryRequirements finalMemReq = {}; -finalMemReq.size = std::max(img1MemReq.size, img2MemReq.size); -finalMemReq.alignment = std::max(img1MemReq.alignment, img2MemReq.alignment); -finalMemReq.memoryTypeBits = img1MemReq.memoryTypeBits & img2MemReq.memoryTypeBits; -// Validate if(finalMemReq.memoryTypeBits != 0) - -VmaAllocationCreateInfo allocCreateInfo = {}; -allocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY; - -VmaAllocation alloc; -res = vmaAllocateMemory(allocator, &finalMemReq, &allocCreateInfo, &alloc, nullptr); - -res = vmaBindImageMemory(allocator, alloc, img1); -res = vmaBindImageMemory(allocator, alloc, img2); - -// You can use img1, img2 here, but not at the same time! - -vmaFreeMemory(allocator, alloc); -vkDestroyImage(allocator, img2, nullptr); -vkDestroyImage(allocator, img1, nullptr); -\endcode - -Remember that using resources that alias in memory requires proper synchronization. -You need to issue a memory barrier to make sure commands that use `img1` and `img2` -don't overlap on GPU timeline. -You also need to treat a resource after aliasing as uninitialized - containing garbage data. -For example, if you use `img1` and then want to use `img2`, you need to issue -an image memory barrier for `img2` with `oldLayout` = `VK_IMAGE_LAYOUT_UNDEFINED`. - -Additional considerations: - -- Vulkan also allows to interpret contents of memory between aliasing resources consistently in some cases. -See chapter 11.8. "Memory Aliasing" of Vulkan specification or `VK_IMAGE_CREATE_ALIAS_BIT` flag. -- You can create more complex layout where different images and buffers are bound -at different offsets inside one large allocation. For example, one can imagine -a big texture used in some render passes, aliasing with a set of many small buffers -used between in some further passes. To bind a resource at non-zero offset of an allocation, -use vmaBindBufferMemory2() / vmaBindImageMemory2(). -- Before allocating memory for the resources you want to alias, check `memoryTypeBits` -returned in memory requirements of each resource to make sure the bits overlap. -Some GPUs may expose multiple memory types suitable e.g. only for buffers or -images with `COLOR_ATTACHMENT` usage, so the sets of memory types supported by your -resources may be disjoint. Aliasing them is not possible in that case. - - \page custom_memory_pools Custom memory pools A memory pool contains a number of `VkDeviceMemory` blocks. @@ -947,7 +822,7 @@ allocations. To mitigate this problem, you can use defragmentation feature: structure #VmaDefragmentationInfo2, function vmaDefragmentationBegin(), vmaDefragmentationEnd(). -Given set of allocations, +Given set of allocations, this function can move them to compact used memory, ensure more continuous free space and possibly also free some `VkDeviceMemory` blocks. @@ -1013,9 +888,9 @@ for(uint32_t i = 0; i < allocCount; ++i) // Create new buffer with same parameters. VkBufferCreateInfo bufferInfo = ...; vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]); - + // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning. - + // Bind new buffer to new memory region. Data contained in it is already moved. VmaAllocationInfo allocInfo; vmaGetAllocationInfo(allocator, allocations[i], &allocInfo); @@ -1091,9 +966,9 @@ for(uint32_t i = 0; i < allocCount; ++i) // Create new buffer with same parameters. VkBufferCreateInfo bufferInfo = ...; vkCreateBuffer(device, &bufferInfo, nullptr, &buffers[i]); - + // You can make dummy call to vkGetBufferMemoryRequirements here to silence validation layer warning. - + // Bind new buffer to new memory region. Data contained in it is already moved. VmaAllocationInfo allocInfo; vmaGetAllocationInfo(allocator, allocations[i], &allocInfo); @@ -1130,7 +1005,7 @@ See [Validation layer warnings](@ref general_considerations_validation_layer_war Please don't expect memory to be fully compacted after defragmentation. Algorithms inside are based on some heuristics that try to maximize number of Vulkan -memory blocks to make totally empty to release them, as well as to maximize continuous +memory blocks to make totally empty to release them, as well as to maximimze continuous empty space inside remaining blocks, while minimizing the number and size of allocations that need to be moved. Some fragmentation may still remain - this is normal. @@ -1393,9 +1268,6 @@ printf("Image name: %s\n", imageName); That string is also printed in JSON report created by vmaBuildStatsString(). -\note Passing string name to VMA allocation doesn't automatically set it to the Vulkan buffer or image created with it. -You must do it manually using an extension like VK_EXT_debug_utils, which is independent of this library. - \page debugging_memory_usage Debugging incorrect memory usage @@ -1487,7 +1359,7 @@ which indicates a serious bug. You can also explicitly request checking margins of all allocations in all memory blocks that belong to specified memory types by using function vmaCheckCorruption(), -or in memory blocks that belong to specified custom pool, by using function +or in memory blocks that belong to specified custom pool, by using function vmaCheckPoolCorruption(). Margin validation (corruption detection) works only for memory types that are @@ -1628,7 +1500,6 @@ This is a more complex situation. Different solutions are possible, and the best one depends on specific GPU type, but you can use this simple approach for the start. Prefer to write to such resource sequentially (e.g. using `memcpy`). Don't perform random access or any reads from it on CPU, as it may be very slow. -Also note that textures written directly from the host through a mapped pointer need to be in LINEAR not OPTIMAL layout. \subsection usage_patterns_readback Readback @@ -1661,17 +1532,17 @@ directly instead of submitting explicit transfer (see below). For resources that you frequently write on CPU and read on GPU, many solutions are possible: -# Create one copy in video memory using #VMA_MEMORY_USAGE_GPU_ONLY, - second copy in system memory using #VMA_MEMORY_USAGE_CPU_ONLY and submit explicit transfer each time. --# Create just a single copy using #VMA_MEMORY_USAGE_CPU_TO_GPU, map it and fill it on CPU, + second copy in system memory using #VMA_MEMORY_USAGE_CPU_ONLY and submit explicit tranfer each time. +-# Create just single copy using #VMA_MEMORY_USAGE_CPU_TO_GPU, map it and fill it on CPU, read it directly on GPU. --# Create just a single copy using #VMA_MEMORY_USAGE_CPU_ONLY, map it and fill it on CPU, +-# Create just single copy using #VMA_MEMORY_USAGE_CPU_ONLY, map it and fill it on CPU, read it directly on GPU. Which solution is the most efficient depends on your resource and especially on the GPU. It is best to measure it and then make the decision. Some general recommendations: -- On integrated graphics use (2) or (3) to avoid unnecessary time and memory overhead +- On integrated graphics use (2) or (3) to avoid unnecesary time and memory overhead related to using a second copy and making transfer. - For small resources (e.g. constant buffers) use (2). Discrete AMD cards have special 256 MiB pool of video memory that is directly mappable. @@ -1692,10 +1563,6 @@ solutions are possible: You should take some measurements to decide which option is faster in case of your specific resource. -Note that textures accessed directly from the host through a mapped pointer need to be in LINEAR layout, -which may slow down their usage on the device. -Textures accessed only by the device and transfer operations can use OPTIMAL layout. - If you don't want to specialize your code for specific types of GPUs, you can still make an simple optimization for cases when your resource ends up in mappable memory to use it directly in this case instead of creating CPU-side staging copy. @@ -1718,31 +1585,12 @@ and empty otherwise. \section config_Vulkan_functions Pointers to Vulkan functions -There are multiple ways to import pointers to Vulkan functions in the library. -In the simplest case you don't need to do anything. -If the compilation or linking of your program or the initialization of the #VmaAllocator -doesn't work for you, you can try to reconfigure it. - -First, the allocator tries to fetch pointers to Vulkan functions linked statically, -like this: - -\code -m_VulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkAllocateMemory; -\endcode - -If you want to disable this feature, set configuration macro: `#define VMA_STATIC_VULKAN_FUNCTIONS 0`. - -Second, you can provide the pointers yourself by setting member VmaAllocatorCreateInfo::pVulkanFunctions. -You can fetch them e.g. using functions `vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` or -by using a helper library like [volk](https://github.com/zeux/volk). - -Third, VMA tries to fetch remaining pointers that are still null by calling -`vkGetInstanceProcAddr` and `vkGetDeviceProcAddr` on its own. -If you want to disable this feature, set configuration macro: `#define VMA_DYNAMIC_VULKAN_FUNCTIONS 0`. - -Finally, all the function pointers required by the library (considering selected -Vulkan version and enabled extensions) are checked with `VMA_ASSERT` if they are not null. +The library uses Vulkan functions straight from the `vulkan.h` header by default. +If you want to provide your own pointers to these functions, e.g. fetched using +`vkGetInstanceProcAddr()` and `vkGetDeviceProcAddr()`: +-# Define `VMA_STATIC_VULKAN_FUNCTIONS 0`. +-# Provide valid pointers through VmaAllocatorCreateInfo::pVulkanFunctions. \section custom_memory_allocator Custom host memory allocator @@ -1764,7 +1612,7 @@ VmaAllocatorCreateInfo::pDeviceMemoryCallbacks. When device memory of certain heap runs out of free space, new allocations may fail (returning error code) or they may succeed, silently pushing some existing memory blocks from GPU VRAM to system RAM (which degrades performance). This -behavior is implementation-dependent - it depends on GPU vendor and graphics +behavior is implementation-dependant - it depends on GPU vendor and graphics driver. On AMD cards it can be controlled while creating Vulkan device object by using @@ -1822,115 +1670,11 @@ unaware of it. To learn more about this extension, see: -- [VK_KHR_dedicated_allocation in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap50.html#VK_KHR_dedicated_allocation) +- [VK_KHR_dedicated_allocation in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.0-extensions/html/vkspec.html#VK_KHR_dedicated_allocation) - [VK_KHR_dedicated_allocation unofficial manual](http://asawicki.info/articles/VK_KHR_dedicated_allocation.php5) -\page vk_amd_device_coherent_memory VK_AMD_device_coherent_memory - -VK_AMD_device_coherent_memory is a device extension that enables access to -additional memory types with `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and -`VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flag. It is useful mostly for -allocation of buffers intended for writing "breadcrumb markers" in between passes -or draw calls, which in turn are useful for debugging GPU crash/hang/TDR cases. - -When the extension is available but has not been enabled, Vulkan physical device -still exposes those memory types, but their usage is forbidden. VMA automatically -takes care of that - it returns `VK_ERROR_FEATURE_NOT_PRESENT` when an attempt -to allocate memory of such type is made. - -If you want to use this extension in connection with VMA, follow these steps: - -\section vk_amd_device_coherent_memory_initialization Initialization - -1) Call `vkEnumerateDeviceExtensionProperties` for the physical device. -Check if the extension is supported - if returned array of `VkExtensionProperties` contains "VK_AMD_device_coherent_memory". - -2) Call `vkGetPhysicalDeviceFeatures2` for the physical device instead of old `vkGetPhysicalDeviceFeatures`. -Attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to `VkPhysicalDeviceFeatures2::pNext` to be returned. -Check if the device feature is really supported - check if `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true. - -3) While creating device with `vkCreateDevice`, enable this extension - add "VK_AMD_device_coherent_memory" -to the list passed as `VkDeviceCreateInfo::ppEnabledExtensionNames`. - -4) While creating the device, also don't set `VkDeviceCreateInfo::pEnabledFeatures`. -Fill in `VkPhysicalDeviceFeatures2` structure instead and pass it as `VkDeviceCreateInfo::pNext`. -Enable this device feature - attach additional structure `VkPhysicalDeviceCoherentMemoryFeaturesAMD` to -`VkPhysicalDeviceFeatures2::pNext` and set its member `deviceCoherentMemory` to `VK_TRUE`. - -5) While creating #VmaAllocator with vmaCreateAllocator() inform VMA that you -have enabled this extension and feature - add #VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT -to VmaAllocatorCreateInfo::flags. - -\section vk_amd_device_coherent_memory_usage Usage - -After following steps described above, you can create VMA allocations and custom pools -out of the special `DEVICE_COHERENT` and `DEVICE_UNCACHED` memory types on eligible -devices. There are multiple ways to do it, for example: - -- You can request or prefer to allocate out of such memory types by adding - `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` to VmaAllocationCreateInfo::requiredFlags - or VmaAllocationCreateInfo::preferredFlags. Those flags can be freely mixed with - other ways of \ref choosing_memory_type, like setting VmaAllocationCreateInfo::usage. -- If you manually found memory type index to use for this purpose, force allocation - from this specific index by setting VmaAllocationCreateInfo::memoryTypeBits `= 1u << index`. - -\section vk_amd_device_coherent_memory_more_information More information - -To learn more about this extension, see [VK_AMD_device_coherent_memory in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap44.html#VK_AMD_device_coherent_memory) - -Example use of this extension can be found in the code of the sample and test suite -accompanying this library. - - -\page enabling_buffer_device_address Enabling buffer device address - -Device extension VK_KHR_buffer_device_address -allow to fetch raw GPU pointer to a buffer and pass it for usage in a shader code. -It is promoted to core Vulkan 1.2. - -If you want to use this feature in connection with VMA, follow these steps: - -\section enabling_buffer_device_address_initialization Initialization - -1) (For Vulkan version < 1.2) Call `vkEnumerateDeviceExtensionProperties` for the physical device. -Check if the extension is supported - if returned array of `VkExtensionProperties` contains -"VK_KHR_buffer_device_address". - -2) Call `vkGetPhysicalDeviceFeatures2` for the physical device instead of old `vkGetPhysicalDeviceFeatures`. -Attach additional structure `VkPhysicalDeviceBufferDeviceAddressFeatures*` to `VkPhysicalDeviceFeatures2::pNext` to be returned. -Check if the device feature is really supported - check if `VkPhysicalDeviceBufferDeviceAddressFeatures*::bufferDeviceAddress` is true. - -3) (For Vulkan version < 1.2) While creating device with `vkCreateDevice`, enable this extension - add -"VK_KHR_buffer_device_address" to the list passed as `VkDeviceCreateInfo::ppEnabledExtensionNames`. - -4) While creating the device, also don't set `VkDeviceCreateInfo::pEnabledFeatures`. -Fill in `VkPhysicalDeviceFeatures2` structure instead and pass it as `VkDeviceCreateInfo::pNext`. -Enable this device feature - attach additional structure `VkPhysicalDeviceBufferDeviceAddressFeatures*` to -`VkPhysicalDeviceFeatures2::pNext` and set its member `bufferDeviceAddress` to `VK_TRUE`. - -5) While creating #VmaAllocator with vmaCreateAllocator() inform VMA that you -have enabled this feature - add #VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT -to VmaAllocatorCreateInfo::flags. - -\section enabling_buffer_device_address_usage Usage - -After following steps described above, you can create buffers with `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT*` using VMA. -The library automatically adds `VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT*` to -allocated memory blocks wherever it might be needed. - -Please note that the library supports only `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT*`. -The second part of this functionality related to "capture and replay" is not supported, -as it is intended for usage in debugging tools like RenderDoc, not in everyday Vulkan usage. - -\section enabling_buffer_device_address_more_information More information - -To learn more about this extension, see [VK_KHR_buffer_device_address in Vulkan specification](https://www.khronos.org/registry/vulkan/specs/1.2-extensions/html/chap46.html#VK_KHR_buffer_device_address) - -Example use of this extension can be found in the code of the sample and test suite -accompanying this library. - \page general_considerations General considerations \section general_considerations_thread_safety Thread safety @@ -1964,7 +1708,7 @@ to just ignore them. up together, especially on GPUs with unified memory like Intel. - *Non-linear image 0xebc91 is aliased with linear buffer 0xeb8e4 which may indicate a bug.* - It happens when you use lost allocations, and a new image or buffer is - created in place of an existing object that became lost. + created in place of an existing object that bacame lost. - It may happen also when you use [defragmentation](@ref defragmentation). \section general_considerations_allocation_algorithm Allocation algorithm @@ -1987,7 +1731,7 @@ The library uses following algorithm for allocation, in order: Features deliberately excluded from the scope of this library: -- Data transfer. Uploading (streaming) and downloading data of buffers and images +- Data transfer. Uploading (straming) and downloading data of buffers and images between CPU and GPU memory and related synchronization is responsibility of the user. Defining some "texture" object that would automatically stream its data from a staging copy in CPU memory to GPU memory would rather be a feature of another, @@ -1996,8 +1740,6 @@ Features deliberately excluded from the scope of this library: explicit memory type index and dedicated allocation anyway, so they don't interact with main features of this library. Such special purpose allocations should be made manually, using `vkCreateBuffer()` and `vkAllocateMemory()`. -- Sub-allocation of parts of one large buffer. Although recommended as a good practice, - it is the user's responsibility to implement such logic on top of VMA. - Recreation of buffers and images. Although the library has functions for buffer and image creation (vmaCreateBuffer(), vmaCreateImage()), you need to recreate these objects yourself after defragmentation. That's because the big @@ -2007,9 +1749,8 @@ Features deliberately excluded from the scope of this library: objects in CPU memory (not Vulkan memory), allocation failures are not checked and handled gracefully, because that would complicate code significantly and is usually not needed in desktop PC applications anyway. - Success of an allocation is just checked with an assert. - Code free of any compiler warnings. Maintaining the library to compile and - work correctly on so many different platforms is hard enough. Being free of + work correctly on so many different platforms is hard enough. Being free of any warnings, on any version of any compiler, is simply not feasible. - This is a C++ library with C interface. Bindings or ports to any other programming languages are welcomed as external projects and @@ -2017,10 +1758,6 @@ Features deliberately excluded from the scope of this library: */ -#ifdef __cplusplus -extern "C" { -#endif - /* Define this macro to 0/1 to disable/enable support for recording functionality, available through VmaAllocatorCreateInfo::pRecordSettings. @@ -2029,50 +1766,23 @@ available through VmaAllocatorCreateInfo::pRecordSettings. #define VMA_RECORDING_ENABLED 0 #endif -#if !defined(NOMINMAX) && defined(VMA_IMPLEMENTATION) +#ifndef NOMINMAX #define NOMINMAX // For windows.h #endif -#if defined(__ANDROID__) && defined(VK_NO_PROTOTYPES) && VMA_STATIC_VULKAN_FUNCTIONS - extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; - extern PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; - extern PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties; - extern PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties; - extern PFN_vkAllocateMemory vkAllocateMemory; - extern PFN_vkFreeMemory vkFreeMemory; - extern PFN_vkMapMemory vkMapMemory; - extern PFN_vkUnmapMemory vkUnmapMemory; - extern PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges; - extern PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges; - extern PFN_vkBindBufferMemory vkBindBufferMemory; - extern PFN_vkBindImageMemory vkBindImageMemory; - extern PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements; - extern PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; - extern PFN_vkCreateBuffer vkCreateBuffer; - extern PFN_vkDestroyBuffer vkDestroyBuffer; - extern PFN_vkCreateImage vkCreateImage; - extern PFN_vkDestroyImage vkDestroyImage; - extern PFN_vkCmdCopyBuffer vkCmdCopyBuffer; - #if VMA_VULKAN_VERSION >= 1001000 - extern PFN_vkGetBufferMemoryRequirements2 vkGetBufferMemoryRequirements2; - extern PFN_vkGetImageMemoryRequirements2 vkGetImageMemoryRequirements2; - extern PFN_vkBindBufferMemory2 vkBindBufferMemory2; - extern PFN_vkBindImageMemory2 vkBindImageMemory2; - extern PFN_vkGetPhysicalDeviceMemoryProperties2 vkGetPhysicalDeviceMemoryProperties2; - #endif // #if VMA_VULKAN_VERSION >= 1001000 -#endif // #if defined(__ANDROID__) && VMA_STATIC_VULKAN_FUNCTIONS && VK_NO_PROTOTYPES - #ifndef VULKAN_H_ #include #endif +#if VMA_RECORDING_ENABLED + #include +#endif + // Define this macro to declare maximum supported Vulkan version in format AAABBBCCC, // where AAA = major, BBB = minor, CCC = patch. // If you want to use version > 1.0, it still needs to be enabled via VmaAllocatorCreateInfo::vulkanApiVersion. #if !defined(VMA_VULKAN_VERSION) - #if defined(VK_VERSION_1_2) - #define VMA_VULKAN_VERSION 1002000 - #elif defined(VK_VERSION_1_1) + #if defined(VK_VERSION_1_1) #define VMA_VULKAN_VERSION 1001000 #else #define VMA_VULKAN_VERSION 1000000 @@ -2103,27 +1813,9 @@ available through VmaAllocatorCreateInfo::pRecordSettings. #endif #endif -// Defined to 1 when VK_KHR_buffer_device_address device extension or equivalent core Vulkan 1.2 feature is defined in its headers. -#if !defined(VMA_BUFFER_DEVICE_ADDRESS) - #if VK_KHR_buffer_device_address || VMA_VULKAN_VERSION >= 1002000 - #define VMA_BUFFER_DEVICE_ADDRESS 1 - #else - #define VMA_BUFFER_DEVICE_ADDRESS 0 - #endif -#endif - -// Defined to 1 when VK_EXT_memory_priority device extension is defined in Vulkan headers. -#if !defined(VMA_MEMORY_PRIORITY) - #if VK_EXT_memory_priority - #define VMA_MEMORY_PRIORITY 1 - #else - #define VMA_MEMORY_PRIORITY 0 - #endif -#endif - // Define these macros to decorate all public functions with additional code, // before and after returned type, appropriately. This may be useful for -// exporting the functions when compiling VMA as a separate library. Example: +// exporing the functions when compiling VMA as a separate library. Example: // #define VMA_CALL_PRE __declspec(dllexport) // #define VMA_CALL_POST __cdecl #ifndef VMA_CALL_PRE @@ -2133,59 +1825,6 @@ available through VmaAllocatorCreateInfo::pRecordSettings. #define VMA_CALL_POST #endif -// Define this macro to decorate pointers with an attribute specifying the -// length of the array they point to if they are not null. -// -// The length may be one of -// - The name of another parameter in the argument list where the pointer is declared -// - The name of another member in the struct where the pointer is declared -// - The name of a member of a struct type, meaning the value of that member in -// the context of the call. For example -// VMA_LEN_IF_NOT_NULL("VkPhysicalDeviceMemoryProperties::memoryHeapCount"), -// this means the number of memory heaps available in the device associated -// with the VmaAllocator being dealt with. -#ifndef VMA_LEN_IF_NOT_NULL - #define VMA_LEN_IF_NOT_NULL(len) -#endif - -// The VMA_NULLABLE macro is defined to be _Nullable when compiling with Clang. -// see: https://clang.llvm.org/docs/AttributeReference.html#nullable -#ifndef VMA_NULLABLE - #ifdef __clang__ - #define VMA_NULLABLE _Nullable - #else - #define VMA_NULLABLE - #endif -#endif - -// The VMA_NOT_NULL macro is defined to be _Nonnull when compiling with Clang. -// see: https://clang.llvm.org/docs/AttributeReference.html#nonnull -#ifndef VMA_NOT_NULL - #ifdef __clang__ - #define VMA_NOT_NULL _Nonnull - #else - #define VMA_NOT_NULL - #endif -#endif - -// If non-dispatchable handles are represented as pointers then we can give -// then nullability annotations -#ifndef VMA_NOT_NULL_NON_DISPATCHABLE - #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) - #define VMA_NOT_NULL_NON_DISPATCHABLE VMA_NOT_NULL - #else - #define VMA_NOT_NULL_NON_DISPATCHABLE - #endif -#endif - -#ifndef VMA_NULLABLE_NON_DISPATCHABLE - #if defined(__LP64__) || defined(_WIN64) || (defined(__x86_64__) && !defined(__ILP32__) ) || defined(_M_X64) || defined(__ia64) || defined (_M_IA64) || defined(__aarch64__) || defined(__powerpc64__) - #define VMA_NULLABLE_NON_DISPATCHABLE VMA_NULLABLE - #else - #define VMA_NULLABLE_NON_DISPATCHABLE - #endif -#endif - /** \struct VmaAllocator \brief Represents main object of this library initialized. @@ -2199,18 +1838,16 @@ VK_DEFINE_HANDLE(VmaAllocator) /// Callback function called after successful vkAllocateMemory. typedef void (VKAPI_PTR *PFN_vmaAllocateDeviceMemoryFunction)( - VmaAllocator VMA_NOT_NULL allocator, - uint32_t memoryType, - VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory, - VkDeviceSize size, - void* VMA_NULLABLE pUserData); + VmaAllocator allocator, + uint32_t memoryType, + VkDeviceMemory memory, + VkDeviceSize size); /// Callback function called before vkFreeMemory. typedef void (VKAPI_PTR *PFN_vmaFreeDeviceMemoryFunction)( - VmaAllocator VMA_NOT_NULL allocator, - uint32_t memoryType, - VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory, - VkDeviceSize size, - void* VMA_NULLABLE pUserData); + VmaAllocator allocator, + uint32_t memoryType, + VkDeviceMemory memory, + VkDeviceSize size); /** \brief Set of callbacks that the library will call for `vkAllocateMemory` and `vkFreeMemory`. @@ -2221,11 +1858,9 @@ Used in VmaAllocatorCreateInfo::pDeviceMemoryCallbacks. */ typedef struct VmaDeviceMemoryCallbacks { /// Optional, can be null. - PFN_vmaAllocateDeviceMemoryFunction VMA_NULLABLE pfnAllocate; + PFN_vmaAllocateDeviceMemoryFunction pfnAllocate; /// Optional, can be null. - PFN_vmaFreeDeviceMemoryFunction VMA_NULLABLE pfnFree; - /// Optional, can be null. - void* VMA_NULLABLE pUserData; + PFN_vmaFreeDeviceMemoryFunction pfnFree; } VmaDeviceMemoryCallbacks; /// Flags for created #VmaAllocator. @@ -2240,7 +1875,7 @@ typedef enum VmaAllocatorCreateFlagBits { The flag works only if VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_0`. When it's `VK_API_VERSION_1_1`, the flag is ignored because the extension has been promoted to Vulkan 1.1. - Using this extension will automatically allocate dedicated blocks of memory for + Using this extenion will automatically allocate dedicated blocks of memory for some buffers and images instead of suballocating place for them out of bigger memory blocks (as if you explicitly used #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT flag) when it is recommended by the driver. It may improve performance on some @@ -2287,59 +1922,6 @@ typedef enum VmaAllocatorCreateFlagBits { be more accurate than an estimation used by the library otherwise. */ VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT = 0x00000008, - /** - Enables usage of VK_AMD_device_coherent_memory extension. - - You may set this flag only if you: - - - found out that this device extension is supported and enabled it while creating Vulkan device passed as VmaAllocatorCreateInfo::device, - - checked that `VkPhysicalDeviceCoherentMemoryFeaturesAMD::deviceCoherentMemory` is true and set it while creating the Vulkan device, - - want it to be used internally by this library. - - The extension and accompanying device feature provide access to memory types with - `VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD` and `VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD` flags. - They are useful mostly for writing breadcrumb markers - a common method for debugging GPU crash/hang/TDR. - - When the extension is not enabled, such memory types are still enumerated, but their usage is illegal. - To protect from this error, if you don't create the allocator with this flag, it will refuse to allocate any memory or create a custom pool in such memory type, - returning `VK_ERROR_FEATURE_NOT_PRESENT`. - */ - VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT = 0x00000010, - /** - Enables usage of "buffer device address" feature, which allows you to use function - `vkGetBufferDeviceAddress*` to get raw GPU pointer to a buffer and pass it for usage inside a shader. - - You may set this flag only if you: - - 1. (For Vulkan version < 1.2) Found as available and enabled device extension - VK_KHR_buffer_device_address. - This extension is promoted to core Vulkan 1.2. - 2. Found as available and enabled device feature `VkPhysicalDeviceBufferDeviceAddressFeatures::bufferDeviceAddress`. - - When this flag is set, you can create buffers with `VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT` using VMA. - The library automatically adds `VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT` to - allocated memory blocks wherever it might be needed. - - For more information, see documentation chapter \ref enabling_buffer_device_address. - */ - VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT = 0x00000020, - /** - Enables usage of VK_EXT_memory_priority extension in the library. - - You may set this flag only if you found available and enabled this device extension, - along with `VkPhysicalDeviceMemoryPriorityFeaturesEXT::memoryPriority == VK_TRUE`, - while creating Vulkan device passed as VmaAllocatorCreateInfo::device. - - When this flag is used, VmaAllocationCreateInfo::priority and VmaPoolCreateInfo::priority - are used to set priorities of allocated Vulkan memory. Without it, these variables are ignored. - - A priority must be a floating-point value between 0 and 1, indicating the priority of the allocation relative to other memory allocations. - Larger values are higher priority. The granularity of the priorities is implementation-dependent. - It is automatically passed to every call to `vkAllocateMemory` done by the library using structure `VkMemoryPriorityAllocateInfoEXT`. - The value to be used for default priority is 0.5. - For more details, see the documentation of the VK_EXT_memory_priority extension. - */ - VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT = 0x00000040, VMA_ALLOCATOR_CREATE_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF } VmaAllocatorCreateFlagBits; @@ -2350,33 +1932,33 @@ typedef VkFlags VmaAllocatorCreateFlags; Used in VmaAllocatorCreateInfo::pVulkanFunctions. */ typedef struct VmaVulkanFunctions { - PFN_vkGetPhysicalDeviceProperties VMA_NULLABLE vkGetPhysicalDeviceProperties; - PFN_vkGetPhysicalDeviceMemoryProperties VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties; - PFN_vkAllocateMemory VMA_NULLABLE vkAllocateMemory; - PFN_vkFreeMemory VMA_NULLABLE vkFreeMemory; - PFN_vkMapMemory VMA_NULLABLE vkMapMemory; - PFN_vkUnmapMemory VMA_NULLABLE vkUnmapMemory; - PFN_vkFlushMappedMemoryRanges VMA_NULLABLE vkFlushMappedMemoryRanges; - PFN_vkInvalidateMappedMemoryRanges VMA_NULLABLE vkInvalidateMappedMemoryRanges; - PFN_vkBindBufferMemory VMA_NULLABLE vkBindBufferMemory; - PFN_vkBindImageMemory VMA_NULLABLE vkBindImageMemory; - PFN_vkGetBufferMemoryRequirements VMA_NULLABLE vkGetBufferMemoryRequirements; - PFN_vkGetImageMemoryRequirements VMA_NULLABLE vkGetImageMemoryRequirements; - PFN_vkCreateBuffer VMA_NULLABLE vkCreateBuffer; - PFN_vkDestroyBuffer VMA_NULLABLE vkDestroyBuffer; - PFN_vkCreateImage VMA_NULLABLE vkCreateImage; - PFN_vkDestroyImage VMA_NULLABLE vkDestroyImage; - PFN_vkCmdCopyBuffer VMA_NULLABLE vkCmdCopyBuffer; + PFN_vkGetPhysicalDeviceProperties vkGetPhysicalDeviceProperties; + PFN_vkGetPhysicalDeviceMemoryProperties vkGetPhysicalDeviceMemoryProperties; + PFN_vkAllocateMemory vkAllocateMemory; + PFN_vkFreeMemory vkFreeMemory; + PFN_vkMapMemory vkMapMemory; + PFN_vkUnmapMemory vkUnmapMemory; + PFN_vkFlushMappedMemoryRanges vkFlushMappedMemoryRanges; + PFN_vkInvalidateMappedMemoryRanges vkInvalidateMappedMemoryRanges; + PFN_vkBindBufferMemory vkBindBufferMemory; + PFN_vkBindImageMemory vkBindImageMemory; + PFN_vkGetBufferMemoryRequirements vkGetBufferMemoryRequirements; + PFN_vkGetImageMemoryRequirements vkGetImageMemoryRequirements; + PFN_vkCreateBuffer vkCreateBuffer; + PFN_vkDestroyBuffer vkDestroyBuffer; + PFN_vkCreateImage vkCreateImage; + PFN_vkDestroyImage vkDestroyImage; + PFN_vkCmdCopyBuffer vkCmdCopyBuffer; #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000 - PFN_vkGetBufferMemoryRequirements2KHR VMA_NULLABLE vkGetBufferMemoryRequirements2KHR; - PFN_vkGetImageMemoryRequirements2KHR VMA_NULLABLE vkGetImageMemoryRequirements2KHR; + PFN_vkGetBufferMemoryRequirements2KHR vkGetBufferMemoryRequirements2KHR; + PFN_vkGetImageMemoryRequirements2KHR vkGetImageMemoryRequirements2KHR; #endif #if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000 - PFN_vkBindBufferMemory2KHR VMA_NULLABLE vkBindBufferMemory2KHR; - PFN_vkBindImageMemory2KHR VMA_NULLABLE vkBindImageMemory2KHR; + PFN_vkBindBufferMemory2KHR vkBindBufferMemory2KHR; + PFN_vkBindImageMemory2KHR vkBindImageMemory2KHR; #endif #if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000 - PFN_vkGetPhysicalDeviceMemoryProperties2KHR VMA_NULLABLE vkGetPhysicalDeviceMemoryProperties2KHR; + PFN_vkGetPhysicalDeviceMemoryProperties2KHR vkGetPhysicalDeviceMemoryProperties2KHR; #endif } VmaVulkanFunctions; @@ -2388,7 +1970,7 @@ typedef enum VmaRecordFlagBits { It may degrade performance though. */ VMA_RECORD_FLUSH_AFTER_CALL_BIT = 0x00000001, - + VMA_RECORD_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF } VmaRecordFlagBits; typedef VkFlags VmaRecordFlags; @@ -2405,7 +1987,7 @@ typedef struct VmaRecordSettings It will be opened for the whole time #VmaAllocator object is alive. If opening this file fails, creation of the whole allocator object fails. */ - const char* VMA_NOT_NULL pFilePath; + const char* pFilePath; } VmaRecordSettings; /// Description of a Allocator to be created. @@ -2415,19 +1997,19 @@ typedef struct VmaAllocatorCreateInfo VmaAllocatorCreateFlags flags; /// Vulkan physical device. /** It must be valid throughout whole lifetime of created allocator. */ - VkPhysicalDevice VMA_NOT_NULL physicalDevice; + VkPhysicalDevice physicalDevice; /// Vulkan device. /** It must be valid throughout whole lifetime of created allocator. */ - VkDevice VMA_NOT_NULL device; + VkDevice device; /// Preferred size of a single `VkDeviceMemory` block to be allocated from large heaps > 1 GiB. Optional. /** Set to 0 to use default, which is currently 256 MiB. */ VkDeviceSize preferredLargeHeapBlockSize; /// Custom CPU memory allocation callbacks. Optional. /** Optional, can be null. When specified, will also be used for all CPU-side memory allocations. */ - const VkAllocationCallbacks* VMA_NULLABLE pAllocationCallbacks; + const VkAllocationCallbacks* pAllocationCallbacks; /// Informative callbacks for `vkAllocateMemory`, `vkFreeMemory`. Optional. /** Optional, can be null. */ - const VmaDeviceMemoryCallbacks* VMA_NULLABLE pDeviceMemoryCallbacks; + const VmaDeviceMemoryCallbacks* pDeviceMemoryCallbacks; /** \brief Maximum number of additional frames that are in use at the same time as current frame. This value is used only when you make allocations with @@ -2466,31 +2048,38 @@ typedef struct VmaAllocatorCreateInfo blocks to system RAM. This driver behavior can also be controlled using VK_AMD_memory_overallocation_behavior extension. */ - const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL("VkPhysicalDeviceMemoryProperties::memoryHeapCount") pHeapSizeLimit; + const VkDeviceSize* pHeapSizeLimit; + /** \brief Pointers to Vulkan functions. Can be null if you leave define `VMA_STATIC_VULKAN_FUNCTIONS 1`. - /** \brief Pointers to Vulkan functions. Can be null. + If you leave define `VMA_STATIC_VULKAN_FUNCTIONS 1` in configuration section, + you can pass null as this member, because the library will fetch pointers to + Vulkan functions internally in a static way, like: - For details see [Pointers to Vulkan functions](@ref config_Vulkan_functions). + vulkanFunctions.vkAllocateMemory = &vkAllocateMemory; + + Fill this member if you want to provide your own pointers to Vulkan functions, + e.g. fetched using `vkGetInstanceProcAddr()` and `vkGetDeviceProcAddr()`. */ - const VmaVulkanFunctions* VMA_NULLABLE pVulkanFunctions; + const VmaVulkanFunctions* pVulkanFunctions; /** \brief Parameters for recording of VMA calls. Can be null. If not null, it enables recording of calls to VMA functions to a file. If support for recording is not enabled using `VMA_RECORDING_ENABLED` macro, creation of the allocator object fails with `VK_ERROR_FEATURE_NOT_PRESENT`. */ - const VmaRecordSettings* VMA_NULLABLE pRecordSettings; - /** \brief Handle to Vulkan instance object. + const VmaRecordSettings* pRecordSettings; + /** \brief Optional handle to Vulkan instance object. - Starting from version 3.0.0 this member is no longer optional, it must be set! + Optional, can be null. Must be set if #VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT flas is used + or if `vulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)`. */ - VkInstance VMA_NOT_NULL instance; + VkInstance instance; /** \brief Optional. The highest version of Vulkan that the application is designed to use. - + It must be a value in the format as created by macro `VK_MAKE_VERSION` or a constant like: `VK_API_VERSION_1_1`, `VK_API_VERSION_1_0`. The patch version number specified is ignored. Only the major and minor versions are considered. - It must be less or equal (preferably equal) to value as passed to `vkCreateInstance` as `VkApplicationInfo::apiVersion`. - Only versions 1.0, 1.1, 1.2 are supported by the current implementation. + It must be less or euqal (preferably equal) to value as passed to `vkCreateInstance` as `VkApplicationInfo::apiVersion`. + Only versions 1.0 and 1.1 are supported by the current implementation. Leaving it initialized to zero is equivalent to `VK_API_VERSION_1_0`. */ uint32_t vulkanApiVersion; @@ -2498,56 +2087,28 @@ typedef struct VmaAllocatorCreateInfo /// Creates Allocator object. VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateAllocator( - const VmaAllocatorCreateInfo* VMA_NOT_NULL pCreateInfo, - VmaAllocator VMA_NULLABLE * VMA_NOT_NULL pAllocator); + const VmaAllocatorCreateInfo* pCreateInfo, + VmaAllocator* pAllocator); /// Destroys allocator object. VMA_CALL_PRE void VMA_CALL_POST vmaDestroyAllocator( - VmaAllocator VMA_NULLABLE allocator); - -/** \brief Information about existing #VmaAllocator object. -*/ -typedef struct VmaAllocatorInfo -{ - /** \brief Handle to Vulkan instance object. - - This is the same value as has been passed through VmaAllocatorCreateInfo::instance. - */ - VkInstance VMA_NOT_NULL instance; - /** \brief Handle to Vulkan physical device object. - - This is the same value as has been passed through VmaAllocatorCreateInfo::physicalDevice. - */ - VkPhysicalDevice VMA_NOT_NULL physicalDevice; - /** \brief Handle to Vulkan device object. - - This is the same value as has been passed through VmaAllocatorCreateInfo::device. - */ - VkDevice VMA_NOT_NULL device; -} VmaAllocatorInfo; - -/** \brief Returns information about existing #VmaAllocator object - handle to Vulkan device etc. - -It might be useful if you want to keep just the #VmaAllocator handle and fetch other required handles to -`VkPhysicalDevice`, `VkDevice` etc. every time using this function. -*/ -VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocatorInfo(VmaAllocator VMA_NOT_NULL allocator, VmaAllocatorInfo* VMA_NOT_NULL pAllocatorInfo); + VmaAllocator allocator); /** PhysicalDeviceProperties are fetched from physicalDevice by the allocator. You can access it here, without fetching it again on your own. */ VMA_CALL_PRE void VMA_CALL_POST vmaGetPhysicalDeviceProperties( - VmaAllocator VMA_NOT_NULL allocator, - const VkPhysicalDeviceProperties* VMA_NULLABLE * VMA_NOT_NULL ppPhysicalDeviceProperties); + VmaAllocator allocator, + const VkPhysicalDeviceProperties** ppPhysicalDeviceProperties); /** PhysicalDeviceMemoryProperties are fetched from physicalDevice by the allocator. You can access it here, without fetching it again on your own. */ VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryProperties( - VmaAllocator VMA_NOT_NULL allocator, - const VkPhysicalDeviceMemoryProperties* VMA_NULLABLE * VMA_NOT_NULL ppPhysicalDeviceMemoryProperties); + VmaAllocator allocator, + const VkPhysicalDeviceMemoryProperties** ppPhysicalDeviceMemoryProperties); /** \brief Given Memory Type Index, returns Property Flags of this memory type. @@ -2556,9 +2117,9 @@ This is just a convenience function. Same information can be obtained using vmaGetMemoryProperties(). */ VMA_CALL_PRE void VMA_CALL_POST vmaGetMemoryTypeProperties( - VmaAllocator VMA_NOT_NULL allocator, + VmaAllocator allocator, uint32_t memoryTypeIndex, - VkMemoryPropertyFlags* VMA_NOT_NULL pFlags); + VkMemoryPropertyFlags* pFlags); /** \brief Sets index of the current frame. @@ -2569,7 +2130,7 @@ when a new frame begins. Allocations queried using vmaGetAllocationInfo() cannot become lost in the current frame. */ VMA_CALL_PRE void VMA_CALL_POST vmaSetCurrentFrameIndex( - VmaAllocator VMA_NOT_NULL allocator, + VmaAllocator allocator, uint32_t frameIndex); /** \brief Calculated statistics of memory usage in entire allocator. @@ -2608,8 +2169,8 @@ Note that when using allocator from multiple threads, returned information may i become outdated. */ VMA_CALL_PRE void VMA_CALL_POST vmaCalculateStats( - VmaAllocator VMA_NOT_NULL allocator, - VmaStats* VMA_NOT_NULL pStats); + VmaAllocator allocator, + VmaStats* pStats); /** \brief Statistics of current memory usage and available budget, in bytes, for specific memory heap. */ @@ -2618,32 +2179,32 @@ typedef struct VmaBudget /** \brief Sum size of all `VkDeviceMemory` blocks allocated from particular heap, in bytes. */ VkDeviceSize blockBytes; - + /** \brief Sum size of all allocations created in particular heap, in bytes. - + Usually less or equal than `blockBytes`. Difference `blockBytes - allocationBytes` is the amount of memory allocated but unused - available for new allocations or wasted due to fragmentation. - + It might be greater than `blockBytes` if there are some allocations in lost state, as they account to this value as well. */ VkDeviceSize allocationBytes; - + /** \brief Estimated current memory usage of the program, in bytes. - + Fetched from system using `VK_EXT_memory_budget` extension if enabled. - + It might be different than `blockBytes` (usually higher) due to additional implicit objects also occupying the memory, like swapchain, pipelines, descriptor heaps, command buffers, or `VkDeviceMemory` blocks allocated outside of this library, if any. */ VkDeviceSize usage; - + /** \brief Estimated amount of memory available to the program, in bytes. - + Fetched from system using `VK_EXT_memory_budget` extension if enabled. - + It might be different (most probably smaller) than `VkMemoryHeap::size[heapIndex]` due to factors external to the program, like other programs also consuming system resources. Difference `budget - usage` is the amount of additional memory that can probably @@ -2663,8 +2224,8 @@ Note that when using allocator from multiple threads, returned information may i become outdated. */ VMA_CALL_PRE void VMA_CALL_POST vmaGetBudget( - VmaAllocator VMA_NOT_NULL allocator, - VmaBudget* VMA_NOT_NULL pBudget); + VmaAllocator allocator, + VmaBudget* pBudget); #ifndef VMA_STATS_STRING_ENABLED #define VMA_STATS_STRING_ENABLED 1 @@ -2676,13 +2237,13 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetBudget( /** @param[out] ppStatsString Must be freed using vmaFreeStatsString() function. */ VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString( - VmaAllocator VMA_NOT_NULL allocator, - char* VMA_NULLABLE * VMA_NOT_NULL ppStatsString, + VmaAllocator allocator, + char** ppStatsString, VkBool32 detailedMap); VMA_CALL_PRE void VMA_CALL_POST vmaFreeStatsString( - VmaAllocator VMA_NOT_NULL allocator, - char* VMA_NULLABLE pStatsString); + VmaAllocator allocator, + char* pStatsString); #endif // #if VMA_STATS_STRING_ENABLED @@ -2708,7 +2269,7 @@ typedef enum VmaMemoryUsage It is roughly equivalent of `D3D12_HEAP_TYPE_DEFAULT`. Usage: - + - Resources written and read by device, e.g. images used as attachments. - Resources transferred from host once (immutable) or infrequently and read by device multiple times, e.g. textures to be sampled, vertex buffers, uniform @@ -2733,7 +2294,7 @@ typedef enum VmaMemoryUsage Memory that is both mappable on host (guarantees to be `HOST_VISIBLE`) and preferably fast to access by GPU. CPU access is typically uncached. Writes may be write-combined. - Usage: Resources written frequently by host (dynamic), read by device. E.g. textures (with LINEAR layout), vertex buffers, uniform buffers updated every frame or every draw call. + Usage: Resources written frequently by host (dynamic), read by device. E.g. textures, vertex buffers, uniform buffers updated every frame or every draw call. */ VMA_MEMORY_USAGE_CPU_TO_GPU = 3, /** Memory mappable on host (guarantees to be `HOST_VISIBLE`) and cached. @@ -2753,7 +2314,7 @@ typedef enum VmaMemoryUsage VMA_MEMORY_USAGE_CPU_COPY = 5, /** Lazily allocated GPU memory having `VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT`. Exists mostly on mobile platforms. Using it on desktop PC or other GPUs with no such memory type present will fail the allocation. - + Usage: Memory for transient attachment images (color attachments, depth attachments etc.), created with `VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT`. Allocations with this usage are always created as dedicated - it implies #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. @@ -2766,28 +2327,28 @@ typedef enum VmaMemoryUsage /// Flags to be passed as VmaAllocationCreateInfo::flags. typedef enum VmaAllocationCreateFlagBits { /** \brief Set this flag if the allocation should have its own memory block. - + Use it for special, big resources, like fullscreen images used as attachments. - + You should not use this flag if VmaAllocationCreateInfo::pool is not null. */ VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT = 0x00000001, /** \brief Set this flag to only try to allocate from existing `VkDeviceMemory` blocks and never create new such block. - + If new allocation cannot be placed in any of the existing blocks, allocation fails with `VK_ERROR_OUT_OF_DEVICE_MEMORY` error. - + You should not use #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT and #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT at the same time. It makes no sense. - + If VmaAllocationCreateInfo::pool is not null, this flag is implied and ignored. */ VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT = 0x00000002, /** \brief Set this flag to use a memory that will be persistently mapped and retrieve pointer to it. - + Pointer to mapped memory will be returned through VmaAllocationInfo::pMappedData. - It is valid to use this flag for allocation made from memory type that is not + Is it valid to use this flag for allocation made from memory type that is not `HOST_VISIBLE`. This flag is then ignored and memory is not mapped. This is useful if you need an allocation that is efficient to use on GPU (`DEVICE_LOCAL`) and still want to map it directly if possible on platforms that @@ -2880,19 +2441,19 @@ typedef struct VmaAllocationCreateInfo /// Use #VmaAllocationCreateFlagBits enum. VmaAllocationCreateFlags flags; /** \brief Intended usage of memory. - + You can leave #VMA_MEMORY_USAGE_UNKNOWN if you specify memory requirements in other way. \n If `pool` is not null, this member is ignored. */ VmaMemoryUsage usage; /** \brief Flags that must be set in a Memory Type chosen for an allocation. - + Leave 0 if you specify memory requirements in other way. \n If `pool` is not null, this member is ignored.*/ VkMemoryPropertyFlags requiredFlags; /** \brief Flags that preferably should be set in a memory type chosen for an allocation. - - Set to 0 if no additional flags are preferred. \n + + Set to 0 if no additional flags are prefered. \n If `pool` is not null, this member is ignored. */ VkMemoryPropertyFlags preferredFlags; /** \brief Bitmask containing one bit set for every memory type acceptable for this allocation. @@ -2908,21 +2469,14 @@ typedef struct VmaAllocationCreateInfo Leave `VK_NULL_HANDLE` to allocate from default pool. If not null, members: `usage`, `requiredFlags`, `preferredFlags`, `memoryTypeBits` are ignored. */ - VmaPool VMA_NULLABLE pool; + VmaPool pool; /** \brief Custom general-purpose pointer that will be stored in #VmaAllocation, can be read as VmaAllocationInfo::pUserData and changed using vmaSetAllocationUserData(). - + If #VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is used, it must be either null or pointer to a null-terminated string. The string will be then copied to internal buffer, so it doesn't need to be valid after allocation call. */ - void* VMA_NULLABLE pUserData; - /** \brief A floating-point value between 0 and 1, indicating the priority of the allocation relative to other memory allocations. - - It is used only when #VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT flag was used during creation of the #VmaAllocator object - and this allocation ends up as dedicated or is explicitly forced as dedicated using #VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. - Otherwise, it has the priority of a memory block where it is placed and this variable is ignored. - */ - float priority; + void* pUserData; } VmaAllocationCreateInfo; /** @@ -2942,10 +2496,10 @@ type of resource you want to use it for. Please check parameters of your resource, like image layout (OPTIMAL versus LINEAR) or mip level count. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex( - VmaAllocator VMA_NOT_NULL allocator, + VmaAllocator allocator, uint32_t memoryTypeBits, - const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo, - uint32_t* VMA_NOT_NULL pMemoryTypeIndex); + const VmaAllocationCreateInfo* pAllocationCreateInfo, + uint32_t* pMemoryTypeIndex); /** \brief Helps to find memoryTypeIndex, given VkBufferCreateInfo and VmaAllocationCreateInfo. @@ -2960,10 +2514,10 @@ It is just a convenience function, equivalent to calling: - `vkDestroyBuffer` */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForBufferInfo( - VmaAllocator VMA_NOT_NULL allocator, - const VkBufferCreateInfo* VMA_NOT_NULL pBufferCreateInfo, - const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo, - uint32_t* VMA_NOT_NULL pMemoryTypeIndex); + VmaAllocator allocator, + const VkBufferCreateInfo* pBufferCreateInfo, + const VmaAllocationCreateInfo* pAllocationCreateInfo, + uint32_t* pMemoryTypeIndex); /** \brief Helps to find memoryTypeIndex, given VkImageCreateInfo and VmaAllocationCreateInfo. @@ -2978,10 +2532,10 @@ It is just a convenience function, equivalent to calling: - `vkDestroyImage` */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo( - VmaAllocator VMA_NOT_NULL allocator, - const VkImageCreateInfo* VMA_NOT_NULL pImageCreateInfo, - const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo, - uint32_t* VMA_NOT_NULL pMemoryTypeIndex); + VmaAllocator allocator, + const VkImageCreateInfo* pImageCreateInfo, + const VmaAllocationCreateInfo* pAllocationCreateInfo, + uint32_t* pMemoryTypeIndex); /// Flags to be passed as VmaPoolCreateInfo::flags. typedef enum VmaPoolCreateFlagBits { @@ -3068,7 +2622,7 @@ typedef struct VmaPoolCreateInfo { /** \brief Maximum number of blocks that can be allocated in this pool. Optional. Set to 0 to use default, which is `SIZE_MAX`, which means no limit. - + Set to same value as VmaPoolCreateInfo::minBlockCount to have fixed amount of memory allocated throughout whole lifetime of this pool. */ @@ -3087,12 +2641,6 @@ typedef struct VmaPoolCreateInfo { become lost, set this value to 0. */ uint32_t frameInUseCount; - /** \brief A floating-point value between 0 and 1, indicating the priority of the allocations in this pool relative to other memory allocations. - - It is used only when #VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT flag was used during creation of the #VmaAllocator object. - Otherwise, this variable is ignored. - */ - float priority; } VmaPoolCreateInfo; /** \brief Describes parameter of existing #VmaPool. @@ -3129,15 +2677,15 @@ typedef struct VmaPoolStats { @param[out] pPool Handle to created pool. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreatePool( - VmaAllocator VMA_NOT_NULL allocator, - const VmaPoolCreateInfo* VMA_NOT_NULL pCreateInfo, - VmaPool VMA_NULLABLE * VMA_NOT_NULL pPool); + VmaAllocator allocator, + const VmaPoolCreateInfo* pCreateInfo, + VmaPool* pPool); /** \brief Destroys #VmaPool object and frees Vulkan device memory. */ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool( - VmaAllocator VMA_NOT_NULL allocator, - VmaPool VMA_NULLABLE pool); + VmaAllocator allocator, + VmaPool pool); /** \brief Retrieves statistics of existing #VmaPool object. @@ -3146,9 +2694,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool( @param[out] pPoolStats Statistics of specified pool. */ VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolStats( - VmaAllocator VMA_NOT_NULL allocator, - VmaPool VMA_NOT_NULL pool, - VmaPoolStats* VMA_NOT_NULL pPoolStats); + VmaAllocator allocator, + VmaPool pool, + VmaPoolStats* pPoolStats); /** \brief Marks all allocations in given pool as lost if they are not used in current frame or VmaPoolCreateInfo::frameInUseCount back from now. @@ -3157,9 +2705,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolStats( @param[out] pLostAllocationCount Number of allocations marked as lost. Optional - pass null if you don't need this information. */ VMA_CALL_PRE void VMA_CALL_POST vmaMakePoolAllocationsLost( - VmaAllocator VMA_NOT_NULL allocator, - VmaPool VMA_NOT_NULL pool, - size_t* VMA_NULLABLE pLostAllocationCount); + VmaAllocator allocator, + VmaPool pool, + size_t* pLostAllocationCount); /** \brief Checks magic number in margins around all allocations in given memory pool in search for corruptions. @@ -3175,7 +2723,7 @@ Possible return values: `VMA_ASSERT` is also fired in that case. - Other value: Error returned by Vulkan, e.g. memory mapping failure. */ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator VMA_NOT_NULL allocator, VmaPool VMA_NOT_NULL pool); +VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckPoolCorruption(VmaAllocator allocator, VmaPool pool); /** \brief Retrieves name of a custom pool. @@ -3184,9 +2732,9 @@ containing name of the pool that was previously set. The pointer becomes invalid destroyed or its name is changed using vmaSetPoolName(). */ VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName( - VmaAllocator VMA_NOT_NULL allocator, - VmaPool VMA_NOT_NULL pool, - const char* VMA_NULLABLE * VMA_NOT_NULL ppName); + VmaAllocator allocator, + VmaPool pool, + const char** ppName); /** \brief Sets name of a custom pool. @@ -3194,9 +2742,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName( Function makes internal copy of the string, so it can be changed or freed immediately after this call. */ VMA_CALL_PRE void VMA_CALL_POST vmaSetPoolName( - VmaAllocator VMA_NOT_NULL allocator, - VmaPool VMA_NOT_NULL pool, - const char* VMA_NULLABLE pName); + VmaAllocator allocator, + VmaPool pool, + const char* pName); /** \struct VmaAllocation \brief Represents single memory allocation. @@ -3228,25 +2776,20 @@ VK_DEFINE_HANDLE(VmaAllocation) */ typedef struct VmaAllocationInfo { /** \brief Memory type index that this allocation was allocated from. - + It never changes. */ uint32_t memoryType; /** \brief Handle to Vulkan memory object. Same memory object can be shared by multiple allocations. - + It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost. If the allocation is lost, it is equal to `VK_NULL_HANDLE`. */ - VkDeviceMemory VMA_NULLABLE_NON_DISPATCHABLE deviceMemory; - /** \brief Offset in `VkDeviceMemory` object to the beginning of this allocation, in bytes. `(deviceMemory, offset)` pair is unique to this allocation. - - You usually don't need to use this offset. If you create a buffer or an image together with the allocation using e.g. function - vmaCreateBuffer(), vmaCreateImage(), functions that operate on these resources refer to the beginning of the buffer or image, - not entire device memory block. Functions like vmaMapMemory(), vmaBindBufferMemory() also refer to the beginning of the allocation - and apply this offset automatically. + VkDeviceMemory deviceMemory; + /** \brief Offset into deviceMemory object to the beginning of this allocation, in bytes. (deviceMemory, offset) pair is unique to this allocation. It can change after call to vmaDefragment() if this allocation is passed to the function, or if allocation is lost. */ @@ -3254,28 +2797,22 @@ typedef struct VmaAllocationInfo { /** \brief Size of this allocation, in bytes. It never changes, unless allocation is lost. - - \note Allocation size returned in this variable may be greater than the size - requested for the resource e.g. as `VkBufferCreateInfo::size`. Whole size of the - allocation is accessible for operations on memory e.g. using a pointer after - mapping with vmaMapMemory(), but operations on the resource e.g. using - `vkCmdCopyBuffer` must be limited to the size of the resource. */ VkDeviceSize size; /** \brief Pointer to the beginning of this allocation as mapped data. If the allocation hasn't been mapped using vmaMapMemory() and hasn't been - created with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag, this value is null. + created with #VMA_ALLOCATION_CREATE_MAPPED_BIT flag, this value null. It can change after call to vmaMapMemory(), vmaUnmapMemory(). It can also change after call to vmaDefragment() if this allocation is passed to the function. */ - void* VMA_NULLABLE pMappedData; + void* pMappedData; /** \brief Custom general-purpose pointer that was passed as VmaAllocationCreateInfo::pUserData or set using vmaSetAllocationUserData(). It can change after call to vmaSetAllocationUserData() for this allocation. */ - void* VMA_NULLABLE pUserData; + void* pUserData; } VmaAllocationInfo; /** \brief General purpose memory allocation. @@ -3289,11 +2826,11 @@ It is recommended to use vmaAllocateMemoryForBuffer(), vmaAllocateMemoryForImage vmaCreateBuffer(), vmaCreateImage() instead whenever possible. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory( - VmaAllocator VMA_NOT_NULL allocator, - const VkMemoryRequirements* VMA_NOT_NULL pVkMemoryRequirements, - const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation, - VmaAllocationInfo* VMA_NULLABLE pAllocationInfo); + VmaAllocator allocator, + const VkMemoryRequirements* pVkMemoryRequirements, + const VmaAllocationCreateInfo* pCreateInfo, + VmaAllocation* pAllocation, + VmaAllocationInfo* pAllocationInfo); /** \brief General purpose memory allocation for multiple allocation objects at once. @@ -3315,12 +2852,12 @@ If any allocation fails, all allocations already made within this function call returned result is not `VK_SUCCESS`, `pAllocation` array is always entirely filled with `VK_NULL_HANDLE`. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( - VmaAllocator VMA_NOT_NULL allocator, - const VkMemoryRequirements* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pVkMemoryRequirements, - const VmaAllocationCreateInfo* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pCreateInfo, + VmaAllocator allocator, + const VkMemoryRequirements* pVkMemoryRequirements, + const VmaAllocationCreateInfo* pCreateInfo, size_t allocationCount, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations, - VmaAllocationInfo* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationInfo); + VmaAllocation* pAllocations, + VmaAllocationInfo* pAllocationInfo); /** @param[out] pAllocation Handle to allocated memory. @@ -3329,27 +2866,27 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( You should free the memory using vmaFreeMemory(). */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer( - VmaAllocator VMA_NOT_NULL allocator, - VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer, - const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation, - VmaAllocationInfo* VMA_NULLABLE pAllocationInfo); + VmaAllocator allocator, + VkBuffer buffer, + const VmaAllocationCreateInfo* pCreateInfo, + VmaAllocation* pAllocation, + VmaAllocationInfo* pAllocationInfo); /// Function similar to vmaAllocateMemoryForBuffer(). VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage( - VmaAllocator VMA_NOT_NULL allocator, - VkImage VMA_NOT_NULL_NON_DISPATCHABLE image, - const VmaAllocationCreateInfo* VMA_NOT_NULL pCreateInfo, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation, - VmaAllocationInfo* VMA_NULLABLE pAllocationInfo); + VmaAllocator allocator, + VkImage image, + const VmaAllocationCreateInfo* pCreateInfo, + VmaAllocation* pAllocation, + VmaAllocationInfo* pAllocationInfo); /** \brief Frees memory previously allocated using vmaAllocateMemory(), vmaAllocateMemoryForBuffer(), or vmaAllocateMemoryForImage(). Passing `VK_NULL_HANDLE` as `allocation` is valid. Such function call is just skipped. */ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory( - VmaAllocator VMA_NOT_NULL allocator, - const VmaAllocation VMA_NULLABLE allocation); + VmaAllocator allocator, + VmaAllocation allocation); /** \brief Frees memory and destroys multiple allocations. @@ -3362,13 +2899,24 @@ Allocations in `pAllocations` array can come from any memory pools and types. Passing `VK_NULL_HANDLE` as elements of `pAllocations` array is valid. Such entries are just skipped. */ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages( - VmaAllocator VMA_NOT_NULL allocator, + VmaAllocator allocator, size_t allocationCount, - const VmaAllocation VMA_NULLABLE * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations); + VmaAllocation* pAllocations); + +/** \brief Deprecated. + +In version 2.2.0 it used to try to change allocation's size without moving or reallocating it. +In current version it returns `VK_SUCCESS` only if `newSize` equals current allocation's size. +Otherwise returns `VK_ERROR_OUT_OF_POOL_MEMORY`, indicating that allocation's size could not be changed. +*/ +VMA_CALL_PRE VkResult VMA_CALL_POST vmaResizeAllocation( + VmaAllocator allocator, + VmaAllocation allocation, + VkDeviceSize newSize); /** \brief Returns current information about specified allocation and atomically marks it as used in current frame. -Current paramteres of given allocation are returned in `pAllocationInfo`. +Current paramters of given allocation are returned in `pAllocationInfo`. This function also atomically "touches" allocation - marks it as used in current frame, just like vmaTouchAllocation(). @@ -3383,9 +2931,9 @@ you can avoid calling it too often. - If you just want to check if allocation is not lost, vmaTouchAllocation() will work faster. */ VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - VmaAllocationInfo* VMA_NOT_NULL pAllocationInfo); + VmaAllocator allocator, + VmaAllocation allocation, + VmaAllocationInfo* pAllocationInfo); /** \brief Returns `VK_TRUE` if allocation is not lost and atomically marks it as used in current frame. @@ -3402,8 +2950,8 @@ If the allocation has been created without #VMA_ALLOCATION_CREATE_CAN_BECOME_LOS this function always returns `VK_TRUE`. */ VMA_CALL_PRE VkBool32 VMA_CALL_POST vmaTouchAllocation( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation); + VmaAllocator allocator, + VmaAllocation allocation); /** \brief Sets pUserData in given allocation to new value. @@ -3419,9 +2967,9 @@ allocation's `pUserData`. It is opaque, so you can use it however you want - e.g as a pointer, ordinal number or some handle to you own data. */ VMA_CALL_PRE void VMA_CALL_POST vmaSetAllocationUserData( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - void* VMA_NULLABLE pUserData); + VmaAllocator allocator, + VmaAllocation allocation, + void* pUserData); /** \brief Creates new allocation that is in lost state from the beginning. @@ -3434,15 +2982,15 @@ not bound to any image or buffer. It has size = 0. It cannot be turned into a real, non-empty allocation. */ VMA_CALL_PRE void VMA_CALL_POST vmaCreateLostAllocation( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation); + VmaAllocator allocator, + VmaAllocation* pAllocation); /** \brief Maps memory represented by given allocation and returns pointer to it. Maps memory represented by given allocation to make it accessible to CPU code. When succeeded, `*ppData` contains pointer to first byte of this memory. If the allocation is part of bigger `VkDeviceMemory` block, the pointer is -correctly offsetted to the beginning of region assigned to this particular +correctly offseted to the beginning of region assigned to this particular allocation. Mapping is internally reference-counted and synchronized, so despite raw Vulkan @@ -3476,9 +3024,9 @@ If the allocation is made from a memory types that is not `HOST_COHERENT`, you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaMapMemory( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - void* VMA_NULLABLE * VMA_NOT_NULL ppData); + VmaAllocator allocator, + VmaAllocation allocation, + void** ppData); /** \brief Unmaps memory represented by given allocation, mapped previously using vmaMapMemory(). @@ -3489,8 +3037,8 @@ If the allocation is made from a memory types that is not `HOST_COHERENT`, you also need to use vmaInvalidateAllocation() / vmaFlushAllocation(), as required by Vulkan specification. */ VMA_CALL_PRE void VMA_CALL_POST vmaUnmapMemory( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation); + VmaAllocator allocator, + VmaAllocation allocation); /** \brief Flushes memory of given allocation. @@ -3509,15 +3057,8 @@ Unmap operation doesn't do that automatically. Warning! `offset` and `size` are relative to the contents of given `allocation`. If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively. Do not pass allocation's offset as `offset`!!! - -This function returns the `VkResult` from `vkFlushMappedMemoryRanges` if it is -called, otherwise `VK_SUCCESS`. */ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - VkDeviceSize offset, - VkDeviceSize size); +VMA_CALL_PRE void VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size); /** \brief Invalidates memory of given allocation. @@ -3536,57 +3077,8 @@ Map operation doesn't do that automatically. Warning! `offset` and `size` are relative to the contents of given `allocation`. If you mean whole allocation, you can pass 0 and `VK_WHOLE_SIZE`, respectively. Do not pass allocation's offset as `offset`!!! - -This function returns the `VkResult` from `vkInvalidateMappedMemoryRanges` if -it is called, otherwise `VK_SUCCESS`. */ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - VkDeviceSize offset, - VkDeviceSize size); - -/** \brief Flushes memory of given set of allocations. - -Calls `vkFlushMappedMemoryRanges()` for memory associated with given ranges of given allocations. -For more information, see documentation of vmaFlushAllocation(). - -\param allocator -\param allocationCount -\param allocations -\param offsets If not null, it must point to an array of offsets of regions to flush, relative to the beginning of respective allocations. Null means all ofsets are zero. -\param sizes If not null, it must point to an array of sizes of regions to flush in respective allocations. Null means `VK_WHOLE_SIZE` for all allocations. - -This function returns the `VkResult` from `vkFlushMappedMemoryRanges` if it is -called, otherwise `VK_SUCCESS`. -*/ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocations( - VmaAllocator VMA_NOT_NULL allocator, - uint32_t allocationCount, - const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) allocations, - const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) offsets, - const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) sizes); - -/** \brief Invalidates memory of given set of allocations. - -Calls `vkInvalidateMappedMemoryRanges()` for memory associated with given ranges of given allocations. -For more information, see documentation of vmaInvalidateAllocation(). - -\param allocator -\param allocationCount -\param allocations -\param offsets If not null, it must point to an array of offsets of regions to flush, relative to the beginning of respective allocations. Null means all ofsets are zero. -\param sizes If not null, it must point to an array of sizes of regions to flush in respective allocations. Null means `VK_WHOLE_SIZE` for all allocations. - -This function returns the `VkResult` from `vkInvalidateMappedMemoryRanges` if it is -called, otherwise `VK_SUCCESS`. -*/ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocations( - VmaAllocator VMA_NOT_NULL allocator, - uint32_t allocationCount, - const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) allocations, - const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) offsets, - const VkDeviceSize* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) sizes); +VMA_CALL_PRE void VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size); /** \brief Checks magic number in margins around all allocations in given memory types (in both default and custom pools) in search for corruptions. @@ -3604,7 +3096,7 @@ Possible return values: `VMA_ASSERT` is also fired in that case. - Other value: Error returned by Vulkan, e.g. memory mapping failure. */ -VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator VMA_NOT_NULL allocator, uint32_t memoryTypeBits); +VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator allocator, uint32_t memoryTypeBits); /** \struct VmaDefragmentationContext \brief Represents Opaque object that represents started defragmentation process. @@ -3616,7 +3108,6 @@ VK_DEFINE_HANDLE(VmaDefragmentationContext) /// Flags to be used in vmaDefragmentationBegin(). None at the moment. Reserved for future use. typedef enum VmaDefragmentationFlagBits { - VMA_DEFRAGMENTATION_FLAG_INCREMENTAL = 0x1, VMA_DEFRAGMENTATION_FLAG_BITS_MAX_ENUM = 0x7FFFFFFF } VmaDefragmentationFlagBits; typedef VkFlags VmaDefragmentationFlags; @@ -3640,13 +3131,13 @@ typedef struct VmaDefragmentationInfo2 { It is safe to pass allocations that are in the lost state - they are ignored. All allocations not present in this array are considered non-moveable during this defragmentation. */ - const VmaAllocation VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations; + VmaAllocation* pAllocations; /** \brief Optional, output. Pointer to array that will be filled with information whether the allocation at certain index has been changed during defragmentation. The array should have `allocationCount` elements. You can pass null if you are not interested in this information. */ - VkBool32* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationsChanged; + VkBool32* pAllocationsChanged; /** \brief Numer of pools in `pPools` array. */ uint32_t poolCount; @@ -3665,9 +3156,9 @@ typedef struct VmaDefragmentationInfo2 { Using this array is equivalent to specifying all allocations from the pools in `pAllocations`. It might be more efficient. */ - const VmaPool VMA_NOT_NULL * VMA_NULLABLE VMA_LEN_IF_NOT_NULL(poolCount) pPools; + VmaPool* pPools; /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on CPU side, like `memcpy()`, `memmove()`. - + `VK_WHOLE_SIZE` means no limit. */ VkDeviceSize maxCpuBytesToMove; @@ -3677,7 +3168,7 @@ typedef struct VmaDefragmentationInfo2 { */ uint32_t maxCpuAllocationsToMove; /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places using transfers on GPU side, posted to `commandBuffer`. - + `VK_WHOLE_SIZE` means no limit. */ VkDeviceSize maxGpuBytesToMove; @@ -3694,31 +3185,16 @@ typedef struct VmaDefragmentationInfo2 { Passing null means that only CPU defragmentation will be performed. */ - VkCommandBuffer VMA_NULLABLE commandBuffer; + VkCommandBuffer commandBuffer; } VmaDefragmentationInfo2; -typedef struct VmaDefragmentationPassMoveInfo { - VmaAllocation VMA_NOT_NULL allocation; - VkDeviceMemory VMA_NOT_NULL_NON_DISPATCHABLE memory; - VkDeviceSize offset; -} VmaDefragmentationPassMoveInfo; - -/** \brief Parameters for incremental defragmentation steps. - -To be used with function vmaBeginDefragmentationPass(). -*/ -typedef struct VmaDefragmentationPassInfo { - uint32_t moveCount; - VmaDefragmentationPassMoveInfo* VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(moveCount) pMoves; -} VmaDefragmentationPassInfo; - /** \brief Deprecated. Optional configuration parameters to be passed to function vmaDefragment(). \deprecated This is a part of the old interface. It is recommended to use structure #VmaDefragmentationInfo2 and function vmaDefragmentationBegin() instead. */ typedef struct VmaDefragmentationInfo { /** \brief Maximum total numbers of bytes that can be copied while moving allocations to different places. - + Default is `VK_WHOLE_SIZE`, which means no limit. */ VkDeviceSize maxBytesToMove; @@ -3771,10 +3247,10 @@ For more information and important limitations regarding defragmentation, see do [Defragmentation](@ref defragmentation). */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationBegin( - VmaAllocator VMA_NOT_NULL allocator, - const VmaDefragmentationInfo2* VMA_NOT_NULL pInfo, - VmaDefragmentationStats* VMA_NULLABLE pStats, - VmaDefragmentationContext VMA_NULLABLE * VMA_NOT_NULL pContext); + VmaAllocator allocator, + const VmaDefragmentationInfo2* pInfo, + VmaDefragmentationStats* pStats, + VmaDefragmentationContext *pContext); /** \brief Ends defragmentation process. @@ -3782,18 +3258,8 @@ Use this function to finish defragmentation started by vmaDefragmentationBegin() It is safe to pass `context == null`. The function then does nothing. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationEnd( - VmaAllocator VMA_NOT_NULL allocator, - VmaDefragmentationContext VMA_NULLABLE context); - -VMA_CALL_PRE VkResult VMA_CALL_POST vmaBeginDefragmentationPass( - VmaAllocator VMA_NOT_NULL allocator, - VmaDefragmentationContext VMA_NULLABLE context, - VmaDefragmentationPassInfo* VMA_NOT_NULL pInfo -); -VMA_CALL_PRE VkResult VMA_CALL_POST vmaEndDefragmentationPass( - VmaAllocator VMA_NOT_NULL allocator, - VmaDefragmentationContext VMA_NULLABLE context -); + VmaAllocator allocator, + VmaDefragmentationContext context); /** \brief Deprecated. Compacts memory by moving allocations. @@ -3836,12 +3302,12 @@ you should measure that on your platform. For more information, see [Defragmentation](@ref defragmentation) chapter. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragment( - VmaAllocator VMA_NOT_NULL allocator, - const VmaAllocation VMA_NOT_NULL * VMA_NOT_NULL VMA_LEN_IF_NOT_NULL(allocationCount) pAllocations, + VmaAllocator allocator, + VmaAllocation* pAllocations, size_t allocationCount, - VkBool32* VMA_NULLABLE VMA_LEN_IF_NOT_NULL(allocationCount) pAllocationsChanged, - const VmaDefragmentationInfo* VMA_NULLABLE pDefragmentationInfo, - VmaDefragmentationStats* VMA_NULLABLE pDefragmentationStats); + VkBool32* pAllocationsChanged, + const VmaDefragmentationInfo *pDefragmentationInfo, + VmaDefragmentationStats* pDefragmentationStats); /** \brief Binds buffer to allocation. @@ -3856,26 +3322,26 @@ allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from mul It is recommended to use function vmaCreateBuffer() instead of this one. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer); + VmaAllocator allocator, + VmaAllocation allocation, + VkBuffer buffer); /** \brief Binds buffer to allocation with additional parameters. -@param allocationLocalOffset Additional offset to be added while binding, relative to the beginning of the `allocation`. Normally it should be 0. +@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0. @param pNext A chain of structures to be attached to `VkBindBufferMemoryInfoKHR` structure used internally. Normally it should be null. This function is similar to vmaBindBufferMemory(), but it provides additional parameters. If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag -or with VmaAllocatorCreateInfo::vulkanApiVersion `>= VK_API_VERSION_1_1`. Otherwise the call fails. +or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory2( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, + VmaAllocator allocator, + VmaAllocation allocation, VkDeviceSize allocationLocalOffset, - VkBuffer VMA_NOT_NULL_NON_DISPATCHABLE buffer, - const void* VMA_NULLABLE pNext); + VkBuffer buffer, + const void* pNext); /** \brief Binds image to allocation. @@ -3890,26 +3356,26 @@ allocations, calls to `vkBind*Memory()` or `vkMapMemory()` won't happen from mul It is recommended to use function vmaCreateImage() instead of this one. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, - VkImage VMA_NOT_NULL_NON_DISPATCHABLE image); + VmaAllocator allocator, + VmaAllocation allocation, + VkImage image); /** \brief Binds image to allocation with additional parameters. -@param allocationLocalOffset Additional offset to be added while binding, relative to the beginning of the `allocation`. Normally it should be 0. +@param allocationLocalOffset Additional offset to be added while binding, relative to the beginnig of the `allocation`. Normally it should be 0. @param pNext A chain of structures to be attached to `VkBindImageMemoryInfoKHR` structure used internally. Normally it should be null. This function is similar to vmaBindImageMemory(), but it provides additional parameters. If `pNext` is not null, #VmaAllocator object must have been created with #VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT flag -or with VmaAllocatorCreateInfo::vulkanApiVersion `>= VK_API_VERSION_1_1`. Otherwise the call fails. +or with VmaAllocatorCreateInfo::vulkanApiVersion `== VK_API_VERSION_1_1`. Otherwise the call fails. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindImageMemory2( - VmaAllocator VMA_NOT_NULL allocator, - VmaAllocation VMA_NOT_NULL allocation, + VmaAllocator allocator, + VmaAllocation allocation, VkDeviceSize allocationLocalOffset, - VkImage VMA_NOT_NULL_NON_DISPATCHABLE image, - const void* VMA_NULLABLE pNext); + VkImage image, + const void* pNext); /** @param[out] pBuffer Buffer that was created. @@ -3929,25 +3395,21 @@ If the function succeeded, you must destroy both buffer and allocation when you no longer need them using either convenience function vmaDestroyBuffer() or separately, using `vkDestroyBuffer()` and vmaFreeMemory(). -If #VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT flag was used, +If VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT flag was used, VK_KHR_dedicated_allocation extension is used internally to query driver whether it requires or prefers the new buffer to have dedicated allocation. If yes, and if dedicated allocation is possible (VmaAllocationCreateInfo::pool is null -and #VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT is not used), it creates dedicated +and VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT is not used), it creates dedicated allocation for this buffer, just like when using -#VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. - -\note This function creates a new `VkBuffer`. Sub-allocation of parts of one large buffer, -although recommended as a good practice, is out of scope of this library and could be implemented -by the user as a higher-level logic on top of VMA. +VMA_ALLOCATION_CREATE_DEDICATED_MEMORY_BIT. */ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer( - VmaAllocator VMA_NOT_NULL allocator, - const VkBufferCreateInfo* VMA_NOT_NULL pBufferCreateInfo, - const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo, - VkBuffer VMA_NULLABLE_NON_DISPATCHABLE * VMA_NOT_NULL pBuffer, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation, - VmaAllocationInfo* VMA_NULLABLE pAllocationInfo); + VmaAllocator allocator, + const VkBufferCreateInfo* pBufferCreateInfo, + const VmaAllocationCreateInfo* pAllocationCreateInfo, + VkBuffer* pBuffer, + VmaAllocation* pAllocation, + VmaAllocationInfo* pAllocationInfo); /** \brief Destroys Vulkan buffer and frees allocated memory. @@ -3961,18 +3423,18 @@ vmaFreeMemory(allocator, allocation); It it safe to pass null as buffer and/or allocation. */ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyBuffer( - VmaAllocator VMA_NOT_NULL allocator, - VkBuffer VMA_NULLABLE_NON_DISPATCHABLE buffer, - VmaAllocation VMA_NULLABLE allocation); + VmaAllocator allocator, + VkBuffer buffer, + VmaAllocation allocation); /// Function similar to vmaCreateBuffer(). VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage( - VmaAllocator VMA_NOT_NULL allocator, - const VkImageCreateInfo* VMA_NOT_NULL pImageCreateInfo, - const VmaAllocationCreateInfo* VMA_NOT_NULL pAllocationCreateInfo, - VkImage VMA_NULLABLE_NON_DISPATCHABLE * VMA_NOT_NULL pImage, - VmaAllocation VMA_NULLABLE * VMA_NOT_NULL pAllocation, - VmaAllocationInfo* VMA_NULLABLE pAllocationInfo); + VmaAllocator allocator, + const VkImageCreateInfo* pImageCreateInfo, + const VmaAllocationCreateInfo* pAllocationCreateInfo, + VkImage* pImage, + VmaAllocation* pAllocation, + VmaAllocationInfo* pAllocationInfo); /** \brief Destroys Vulkan image and frees allocated memory. @@ -3986,9 +3448,9 @@ vmaFreeMemory(allocator, allocation); It it safe to pass null as image and/or allocation. */ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyImage( - VmaAllocator VMA_NOT_NULL allocator, - VkImage VMA_NULLABLE_NON_DISPATCHABLE image, - VmaAllocation VMA_NULLABLE allocation); + VmaAllocator allocator, + VkImage image, + VmaAllocation allocation); #ifdef __cplusplus } @@ -4007,17 +3469,6 @@ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyImage( #include #include #include -#include - -#if VMA_RECORDING_ENABLED - #include - #if defined(_WIN32) - #include - #else - #include - #include - #endif -#endif /******************************************************************************* CONFIGURATION SECTION @@ -4031,23 +3482,12 @@ Define this macro to 1 to make the library fetch pointers to Vulkan functions internally, like: vulkanFunctions.vkAllocateMemory = &vkAllocateMemory; + +Define to 0 if you are going to provide you own pointers to Vulkan functions via +VmaAllocatorCreateInfo::pVulkanFunctions. */ #if !defined(VMA_STATIC_VULKAN_FUNCTIONS) && !defined(VK_NO_PROTOTYPES) - #define VMA_STATIC_VULKAN_FUNCTIONS 1 -#endif - -/* -Define this macro to 1 to make the library fetch pointers to Vulkan functions -internally, like: - - vulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkGetDeviceProcAddr(m_hDevice, vkAllocateMemory); -*/ -#if !defined(VMA_DYNAMIC_VULKAN_FUNCTIONS) - #define VMA_DYNAMIC_VULKAN_FUNCTIONS 1 - #if defined(VK_NO_PROTOTYPES) - extern PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr; - extern PFN_vkGetDeviceProcAddr vkGetDeviceProcAddr; - #endif +#define VMA_STATIC_VULKAN_FUNCTIONS 1 #endif // Define this macro to 1 to make the library use STL containers instead of its own implementation. @@ -4110,7 +3550,7 @@ remove them if not needed. #if defined(__ANDROID_API__) && (__ANDROID_API__ < 16) #include -static void* vma_aligned_alloc(size_t alignment, size_t size) +void *aligned_alloc(size_t alignment, size_t size) { // alignment must be >= sizeof(void*) if(alignment < sizeof(void*)) @@ -4122,25 +3562,8 @@ static void* vma_aligned_alloc(size_t alignment, size_t size) } #elif defined(__APPLE__) || defined(__ANDROID__) || (defined(__linux__) && defined(__GLIBCXX__) && !defined(_GLIBCXX_HAVE_ALIGNED_ALLOC)) #include - -#if defined(__APPLE__) -#include -#endif - -static void* vma_aligned_alloc(size_t alignment, size_t size) +void *aligned_alloc(size_t alignment, size_t size) { -#if defined(__APPLE__) && (defined(MAC_OS_X_VERSION_10_16) || defined(__IPHONE_14_0)) -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_16 || __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_14_0 - // For C++14, usr/include/malloc/_malloc.h declares aligned_alloc()) only - // with the MacOSX11.0 SDK in Xcode 12 (which is what adds - // MAC_OS_X_VERSION_10_16), even though the function is marked - // availabe for 10.15. That's why the preprocessor checks for 10.16 but - // the __builtin_available checks for 10.15. - // People who use C++17 could call aligned_alloc with the 10.15 SDK already. - if (__builtin_available(macOS 10.15, iOS 13, *)) - return aligned_alloc(alignment, size); -#endif -#endif // alignment must be >= sizeof(void*) if(alignment < sizeof(void*)) { @@ -4152,28 +3575,6 @@ static void* vma_aligned_alloc(size_t alignment, size_t size) return pointer; return VMA_NULL; } -#elif defined(_WIN32) -static void* vma_aligned_alloc(size_t alignment, size_t size) -{ - return _aligned_malloc(size, alignment); -} -#else -static void* vma_aligned_alloc(size_t alignment, size_t size) -{ - return aligned_alloc(alignment, size); -} -#endif - -#if defined(_WIN32) -static void vma_aligned_free(void* ptr) -{ - _aligned_free(ptr); -} -#else -static void vma_aligned_free(void* VMA_NULLABLE ptr) -{ - free(ptr); -} #endif // If your compiler is not compatible with C++11 and definition of @@ -4183,20 +3584,20 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) // Normal assert to check for programmer's errors, especially in Debug configuration. #ifndef VMA_ASSERT - #ifdef NDEBUG - #define VMA_ASSERT(expr) - #else + #ifdef _DEBUG #define VMA_ASSERT(expr) assert(expr) + #else + #define VMA_ASSERT(expr) #endif #endif // Assert that will be called very often, like inside data structures e.g. operator[]. // Making it non-empty can make program slow. #ifndef VMA_HEAVY_ASSERT - #ifdef NDEBUG - #define VMA_HEAVY_ASSERT(expr) - #else + #ifdef _DEBUG #define VMA_HEAVY_ASSERT(expr) //VMA_ASSERT(expr) + #else + #define VMA_HEAVY_ASSERT(expr) #endif #endif @@ -4205,16 +3606,19 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) #endif #ifndef VMA_SYSTEM_ALIGNED_MALLOC - #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) vma_aligned_alloc((alignment), (size)) + #if defined(_WIN32) + #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (_aligned_malloc((size), (alignment))) + #else + #define VMA_SYSTEM_ALIGNED_MALLOC(size, alignment) (aligned_alloc((alignment), (size) )) + #endif #endif -#ifndef VMA_SYSTEM_ALIGNED_FREE - // VMA_SYSTEM_FREE is the old name, but might have been defined by the user - #if defined(VMA_SYSTEM_FREE) - #define VMA_SYSTEM_ALIGNED_FREE(ptr) VMA_SYSTEM_FREE(ptr) +#ifndef VMA_SYSTEM_FREE + #if defined(_WIN32) + #define VMA_SYSTEM_FREE(ptr) _aligned_free(ptr) #else - #define VMA_SYSTEM_ALIGNED_FREE(ptr) vma_aligned_free(ptr) - #endif + #define VMA_SYSTEM_FREE(ptr) free(ptr) + #endif #endif #ifndef VMA_MIN @@ -4245,15 +3649,15 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) // Define this macro to 1 to enable functions: vmaBuildStatsString, vmaFreeStatsString. #if VMA_STATS_STRING_ENABLED - static inline void VmaUint32ToStr(char* VMA_NOT_NULL outStr, size_t strLen, uint32_t num) + static inline void VmaUint32ToStr(char* outStr, size_t strLen, uint32_t num) { snprintf(outStr, strLen, "%u", static_cast(num)); } - static inline void VmaUint64ToStr(char* VMA_NOT_NULL outStr, size_t strLen, uint64_t num) + static inline void VmaUint64ToStr(char* outStr, size_t strLen, uint64_t num) { snprintf(outStr, strLen, "%llu", static_cast(num)); } - static inline void VmaPtrToStr(char* VMA_NOT_NULL outStr, size_t strLen, const void* ptr) + static inline void VmaPtrToStr(char* outStr, size_t strLen, const void* ptr) { snprintf(outStr, strLen, "%p", ptr); } @@ -4265,7 +3669,6 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) public: void Lock() { m_Mutex.lock(); } void Unlock() { m_Mutex.unlock(); } - bool TryLock() { return m_Mutex.try_lock(); } private: std::mutex m_Mutex; }; @@ -4282,10 +3685,8 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) public: void LockRead() { m_Mutex.lock_shared(); } void UnlockRead() { m_Mutex.unlock_shared(); } - bool TryLockRead() { return m_Mutex.try_lock_shared(); } void LockWrite() { m_Mutex.lock(); } void UnlockWrite() { m_Mutex.unlock(); } - bool TryLockWrite() { return m_Mutex.try_lock(); } private: std::shared_mutex m_Mutex; }; @@ -4299,10 +3700,8 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) VmaRWMutex() { InitializeSRWLock(&m_Lock); } void LockRead() { AcquireSRWLockShared(&m_Lock); } void UnlockRead() { ReleaseSRWLockShared(&m_Lock); } - bool TryLockRead() { return TryAcquireSRWLockShared(&m_Lock) != FALSE; } void LockWrite() { AcquireSRWLockExclusive(&m_Lock); } void UnlockWrite() { ReleaseSRWLockExclusive(&m_Lock); } - bool TryLockWrite() { return TryAcquireSRWLockExclusive(&m_Lock) != FALSE; } private: SRWLOCK m_Lock; }; @@ -4314,10 +3713,8 @@ static void vma_aligned_free(void* VMA_NULLABLE ptr) public: void LockRead() { m_Mutex.Lock(); } void UnlockRead() { m_Mutex.Unlock(); } - bool TryLockRead() { return m_Mutex.TryLock(); } void LockWrite() { m_Mutex.Lock(); } void UnlockWrite() { m_Mutex.Unlock(); } - bool TryLockWrite() { return m_Mutex.TryLock(); } private: VMA_MUTEX m_Mutex; }; @@ -4346,16 +3743,12 @@ If providing your own implementation, you need to implement a subset of std::ato #define VMA_DEBUG_ALWAYS_DEDICATED_MEMORY (0) #endif -#ifndef VMA_MIN_ALIGNMENT +#ifndef VMA_DEBUG_ALIGNMENT /** Minimum alignment of all allocations, in bytes. - Set to more than 1 for debugging purposes. Must be power of two. + Set to more than 1 for debugging purposes only. Must be power of two. */ - #ifdef VMA_DEBUG_ALIGNMENT // Old name - #define VMA_MIN_ALIGNMENT VMA_DEBUG_ALIGNMENT - #else - #define VMA_MIN_ALIGNMENT (1) - #endif + #define VMA_DEBUG_ALIGNMENT (1) #endif #ifndef VMA_DEBUG_MARGIN @@ -4399,14 +3792,6 @@ If providing your own implementation, you need to implement a subset of std::ato #define VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY (1) #endif -#ifndef VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT - /* - Set this to 1 to make VMA never exceed VkPhysicalDeviceLimits::maxMemoryAllocationCount - and return error instead of leaving up to Vulkan implementation what to do in such cases. - */ - #define VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT (0) -#endif - #ifndef VMA_SMALL_HEAP_MAX_SIZE /// Maximum size of a memory heap in Vulkan to consider it "small". #define VMA_SMALL_HEAP_MAX_SIZE (1024ull * 1024 * 1024) @@ -4436,12 +3821,6 @@ static const uint8_t VMA_ALLOCATION_FILL_PATTERN_DESTROYED = 0xEF; END OF CONFIGURATION */ -// # Copy of some Vulkan definitions so we don't need to check their existence just to handle few constants. - -static const uint32_t VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY = 0x00000040; -static const uint32_t VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY = 0x00000080; -static const uint32_t VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_COPY = 0x00020000; - static const uint32_t VMA_ALLOCATION_INTERNAL_STRATEGY_MIN_OFFSET = 0x10000000u; static VkAllocationCallbacks VmaEmptyAllocationCallbacks = { @@ -4450,12 +3829,34 @@ static VkAllocationCallbacks VmaEmptyAllocationCallbacks = { // Returns number of bits set to 1 in (v). static inline uint32_t VmaCountBitsSet(uint32_t v) { - uint32_t c = v - ((v >> 1) & 0x55555555); - c = ((c >> 2) & 0x33333333) + (c & 0x33333333); - c = ((c >> 4) + c) & 0x0F0F0F0F; - c = ((c >> 8) + c) & 0x00FF00FF; - c = ((c >> 16) + c) & 0x0000FFFF; - return c; + uint32_t c = v - ((v >> 1) & 0x55555555); + c = ((c >> 2) & 0x33333333) + (c & 0x33333333); + c = ((c >> 4) + c) & 0x0F0F0F0F; + c = ((c >> 8) + c) & 0x00FF00FF; + c = ((c >> 16) + c) & 0x0000FFFF; + return c; +} + +// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16. +// Use types like uint32_t, uint64_t as T. +template +static inline T VmaAlignUp(T val, T align) +{ + return (val + align - 1) / align * align; +} +// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8. +// Use types like uint32_t, uint64_t as T. +template +static inline T VmaAlignDown(T val, T align) +{ + return val / align * align; +} + +// Division with mathematical rounding to nearest number. +template +static inline T VmaRoundDiv(T x, T y) +{ + return (x + (y / (T)2)) / y; } /* @@ -4469,34 +3870,10 @@ inline bool VmaIsPow2(T x) return (x & (x-1)) == 0; } -// Aligns given value up to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 16. -// Use types like uint32_t, uint64_t as T. -template -static inline T VmaAlignUp(T val, T alignment) -{ - VMA_HEAVY_ASSERT(VmaIsPow2(alignment)); - return (val + alignment - 1) & ~(alignment - 1); -} -// Aligns given value down to nearest multiply of align value. For example: VmaAlignUp(11, 8) = 8. -// Use types like uint32_t, uint64_t as T. -template -static inline T VmaAlignDown(T val, T alignment) -{ - VMA_HEAVY_ASSERT(VmaIsPow2(alignment)); - return val & ~(alignment - 1); -} - -// Division with mathematical rounding to nearest number. -template -static inline T VmaRoundDiv(T x, T y) -{ - return (x + (y / (T)2)) / y; -} - // Returns smallest power of 2 greater or equal to v. static inline uint32_t VmaNextPow2(uint32_t v) { - v--; + v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; @@ -4507,7 +3884,7 @@ static inline uint32_t VmaNextPow2(uint32_t v) } static inline uint64_t VmaNextPow2(uint64_t v) { - v--; + v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; @@ -4652,7 +4029,7 @@ static inline bool VmaIsBufferImageGranularityConflict( { VMA_SWAP(suballocType1, suballocType2); } - + switch(suballocType1) { case VMA_SUBALLOCATION_TYPE_FREE: @@ -4786,7 +4163,7 @@ static IterT VmaBinaryFindFirstNotLess(IterT beg, IterT end, const KeyT &key, co size_t down = 0, up = (end - beg); while(down < up) { - const size_t mid = down + (up - down) / 2; // Overflow-safe midpoint calculation + const size_t mid = (down + up) / 2; if(cmp(*(beg+mid), key)) { down = mid + 1; @@ -4838,23 +4215,15 @@ static bool VmaValidatePointerArray(uint32_t count, const T* arr) return true; } -template -static inline void VmaPnextChainPushFront(MainT* mainStruct, NewT* newStruct) -{ - newStruct->pNext = mainStruct->pNext; - mainStruct->pNext = newStruct; -} - //////////////////////////////////////////////////////////////////////////////// // Memory allocation static void* VmaMalloc(const VkAllocationCallbacks* pAllocationCallbacks, size_t size, size_t alignment) { - void* result = VMA_NULL; if((pAllocationCallbacks != VMA_NULL) && (pAllocationCallbacks->pfnAllocation != VMA_NULL)) { - result = (*pAllocationCallbacks->pfnAllocation)( + return (*pAllocationCallbacks->pfnAllocation)( pAllocationCallbacks->pUserData, size, alignment, @@ -4862,10 +4231,8 @@ static void* VmaMalloc(const VkAllocationCallbacks* pAllocationCallbacks, size_t } else { - result = VMA_SYSTEM_ALIGNED_MALLOC(size, alignment); + return VMA_SYSTEM_ALIGNED_MALLOC(size, alignment); } - VMA_ASSERT(result != VMA_NULL && "CPU memory allocation failed."); - return result; } static void VmaFree(const VkAllocationCallbacks* pAllocationCallbacks, void* ptr) @@ -4877,7 +4244,7 @@ static void VmaFree(const VkAllocationCallbacks* pAllocationCallbacks, void* ptr } else { - VMA_SYSTEM_ALIGNED_FREE(ptr); + VMA_SYSTEM_FREE(ptr); } } @@ -4948,7 +4315,7 @@ class VmaStlAllocator public: const VkAllocationCallbacks* const m_pCallbacks; typedef T value_type; - + VmaStlAllocator(const VkAllocationCallbacks* pCallbacks) : m_pCallbacks(pCallbacks) { } template VmaStlAllocator(const VmaStlAllocator& src) : m_pCallbacks(src.m_pCallbacks) { } @@ -4967,7 +4334,6 @@ public: } VmaStlAllocator& operator=(const VmaStlAllocator& x) = delete; - VmaStlAllocator(const VmaStlAllocator&) = default; }; #if VMA_USE_STL_VECTOR @@ -5012,12 +4378,12 @@ public: m_Capacity(count) { } - + // This version of the constructor is here for compatibility with pre-C++14 std::vector. // value is unused. VmaVector(size_t count, const T& value, const AllocatorT& allocator) : VmaVector(count, allocator) {} - + VmaVector(const VmaVector& src) : m_Allocator(src.m_Allocator), m_pArray(src.m_Count ? (T*)VmaAllocateArray(src.m_Allocator.m_pCallbacks, src.m_Count) : VMA_NULL), @@ -5029,7 +4395,7 @@ public: memcpy(m_pArray, src.m_pArray, m_Count * sizeof(T)); } } - + ~VmaVector() { VmaFree(m_Allocator.m_pCallbacks, m_pArray); @@ -5047,12 +4413,12 @@ public: } return *this; } - + bool empty() const { return m_Count == 0; } size_t size() const { return m_Count; } T* data() { return m_pArray; } const T* data() const { return m_pArray; } - + T& operator[](size_t index) { VMA_HEAVY_ASSERT(index < m_Count); @@ -5088,12 +4454,12 @@ public: void reserve(size_t newCapacity, bool freeMemory = false) { newCapacity = VMA_MAX(newCapacity, m_Count); - + if((newCapacity < m_Capacity) && !freeMemory) { newCapacity = m_Capacity; } - + if(newCapacity != m_Capacity) { T* const newArray = newCapacity ? VmaAllocateArray(m_Allocator, newCapacity) : VMA_NULL; @@ -5107,13 +4473,17 @@ public: } } - void resize(size_t newCount) + void resize(size_t newCount, bool freeMemory = false) { size_t newCapacity = m_Capacity; if(newCount > m_Capacity) { newCapacity = VMA_MAX(newCount, VMA_MAX(m_Capacity * 3 / 2, (size_t)8)); } + else if(freeMemory) + { + newCapacity = newCount; + } if(newCapacity != m_Capacity) { @@ -5131,25 +4501,9 @@ public: m_Count = newCount; } - void clear() + void clear(bool freeMemory = false) { - resize(0); - } - - void shrink_to_fit() - { - if(m_Capacity > m_Count) - { - T* newArray = VMA_NULL; - if(m_Count > 0) - { - newArray = VmaAllocateArray(m_Allocator.m_pCallbacks, m_Count); - memcpy(newArray, m_pArray, m_Count * sizeof(T)); - } - VmaFree(m_Allocator.m_pCallbacks, m_pArray); - m_Capacity = m_Count; - m_pArray = newArray; - } + resize(0, freeMemory); } void insert(size_t index, const T& src) @@ -5255,187 +4609,6 @@ bool VmaVectorRemoveSorted(VectorT& vector, const typename VectorT::value_type& return false; } -//////////////////////////////////////////////////////////////////////////////// -// class VmaSmallVector - -/* -This is a vector (a variable-sized array), optimized for the case when the array is small. - -It contains some number of elements in-place, which allows it to avoid heap allocation -when the actual number of elements is below that threshold. This allows normal "small" -cases to be fast without losing generality for large inputs. -*/ - -template -class VmaSmallVector -{ -public: - typedef T value_type; - - VmaSmallVector(const AllocatorT& allocator) : - m_Count(0), - m_DynamicArray(allocator) - { - } - VmaSmallVector(size_t count, const AllocatorT& allocator) : - m_Count(count), - m_DynamicArray(count > N ? count : 0, allocator) - { - } - template - VmaSmallVector(const VmaSmallVector& src) = delete; - template - VmaSmallVector& operator=(const VmaSmallVector& rhs) = delete; - - bool empty() const { return m_Count == 0; } - size_t size() const { return m_Count; } - T* data() { return m_Count > N ? m_DynamicArray.data() : m_StaticArray; } - const T* data() const { return m_Count > N ? m_DynamicArray.data() : m_StaticArray; } - - T& operator[](size_t index) - { - VMA_HEAVY_ASSERT(index < m_Count); - return data()[index]; - } - const T& operator[](size_t index) const - { - VMA_HEAVY_ASSERT(index < m_Count); - return data()[index]; - } - - T& front() - { - VMA_HEAVY_ASSERT(m_Count > 0); - return data()[0]; - } - const T& front() const - { - VMA_HEAVY_ASSERT(m_Count > 0); - return data()[0]; - } - T& back() - { - VMA_HEAVY_ASSERT(m_Count > 0); - return data()[m_Count - 1]; - } - const T& back() const - { - VMA_HEAVY_ASSERT(m_Count > 0); - return data()[m_Count - 1]; - } - - void resize(size_t newCount, bool freeMemory = false) - { - if(newCount > N && m_Count > N) - { - // Any direction, staying in m_DynamicArray - m_DynamicArray.resize(newCount); - if(freeMemory) - { - m_DynamicArray.shrink_to_fit(); - } - } - else if(newCount > N && m_Count <= N) - { - // Growing, moving from m_StaticArray to m_DynamicArray - m_DynamicArray.resize(newCount); - if(m_Count > 0) - { - memcpy(m_DynamicArray.data(), m_StaticArray, m_Count * sizeof(T)); - } - } - else if(newCount <= N && m_Count > N) - { - // Shrinking, moving from m_DynamicArray to m_StaticArray - if(newCount > 0) - { - memcpy(m_StaticArray, m_DynamicArray.data(), newCount * sizeof(T)); - } - m_DynamicArray.resize(0); - if(freeMemory) - { - m_DynamicArray.shrink_to_fit(); - } - } - else - { - // Any direction, staying in m_StaticArray - nothing to do here - } - m_Count = newCount; - } - - void clear(bool freeMemory = false) - { - m_DynamicArray.clear(); - if(freeMemory) - { - m_DynamicArray.shrink_to_fit(); - } - m_Count = 0; - } - - void insert(size_t index, const T& src) - { - VMA_HEAVY_ASSERT(index <= m_Count); - const size_t oldCount = size(); - resize(oldCount + 1); - T* const dataPtr = data(); - if(index < oldCount) - { - // I know, this could be more optimal for case where memmove can be memcpy directly from m_StaticArray to m_DynamicArray. - memmove(dataPtr + (index + 1), dataPtr + index, (oldCount - index) * sizeof(T)); - } - dataPtr[index] = src; - } - - void remove(size_t index) - { - VMA_HEAVY_ASSERT(index < m_Count); - const size_t oldCount = size(); - if(index < oldCount - 1) - { - // I know, this could be more optimal for case where memmove can be memcpy directly from m_DynamicArray to m_StaticArray. - T* const dataPtr = data(); - memmove(dataPtr + index, dataPtr + (index + 1), (oldCount - index - 1) * sizeof(T)); - } - resize(oldCount - 1); - } - - void push_back(const T& src) - { - const size_t newIndex = size(); - resize(newIndex + 1); - data()[newIndex] = src; - } - - void pop_back() - { - VMA_HEAVY_ASSERT(m_Count > 0); - resize(size() - 1); - } - - void push_front(const T& src) - { - insert(0, src); - } - - void pop_front() - { - VMA_HEAVY_ASSERT(m_Count > 0); - remove(0); - } - - typedef T* iterator; - - iterator begin() { return data(); } - iterator end() { return data() + m_Count; } - -private: - size_t m_Count; - T m_StaticArray[N]; // Used when m_Size <= N - VmaVector m_DynamicArray; // Used when m_Size > N -}; - //////////////////////////////////////////////////////////////////////////////// // class VmaPoolAllocator @@ -5451,7 +4624,7 @@ class VmaPoolAllocator public: VmaPoolAllocator(const VkAllocationCallbacks* pAllocationCallbacks, uint32_t firstBlockCapacity); ~VmaPoolAllocator(); - template T* Alloc(Types... args); + T* Alloc(); void Free(T* ptr); private: @@ -5467,7 +4640,7 @@ private: uint32_t Capacity; uint32_t FirstFreeIndex; }; - + const VkAllocationCallbacks* m_pAllocationCallbacks; const uint32_t m_FirstBlockCapacity; VmaVector< ItemBlock, VmaStlAllocator > m_ItemBlocks; @@ -5493,7 +4666,7 @@ VmaPoolAllocator::~VmaPoolAllocator() } template -template T* VmaPoolAllocator::Alloc(Types... args) +T* VmaPoolAllocator::Alloc() { for(size_t i = m_ItemBlocks.size(); i--; ) { @@ -5504,7 +4677,7 @@ template T* VmaPoolAllocator::Alloc(Types... args) Item* const pItem = &block.pItems[block.FirstFreeIndex]; block.FirstFreeIndex = pItem->NextFreeIndex; T* result = (T*)&pItem->Value; - new(result)T(std::forward(args)...); // Explicit constructor call. + new(result)T(); // Explicit constructor call. return result; } } @@ -5514,7 +4687,7 @@ template T* VmaPoolAllocator::Alloc(Types... args) Item* const pItem = &newBlock.pItems[0]; newBlock.FirstFreeIndex = pItem->NextFreeIndex; T* result = (T*)&pItem->Value; - new(result)T(std::forward(args)...); // Explicit constructor call. + new(result)T(); // Explicit constructor call. return result; } @@ -5525,11 +4698,11 @@ void VmaPoolAllocator::Free(T* ptr) for(size_t i = m_ItemBlocks.size(); i--; ) { ItemBlock& block = m_ItemBlocks[i]; - + // Casting to union. Item* pItemPtr; memcpy(&pItemPtr, &ptr, sizeof(pItemPtr)); - + // Check if pItemPtr is in address range of this block. if((pItemPtr >= block.pItems) && (pItemPtr < block.pItems + block.Capacity)) { @@ -5606,7 +4779,7 @@ public: ItemType* PushFront(const T& value); void PopBack(); void PopFront(); - + // Item can be null - it means PushBack. ItemType* InsertBefore(ItemType* pItem); // Item can be null - it means PushFront. @@ -5916,7 +5089,7 @@ public: VMA_HEAVY_ASSERT(m_pList == rhs.m_pList); return m_pItem != rhs.m_pItem; } - + private: VmaRawList* m_pList; VmaListItem* m_pItem; @@ -5944,7 +5117,7 @@ public: m_pItem(src.m_pItem) { } - + const T& operator*() const { VMA_HEAVY_ASSERT(m_pItem != VMA_NULL); @@ -5999,7 +5172,7 @@ public: VMA_HEAVY_ASSERT(m_pList == rhs.m_pList); return m_pItem != rhs.m_pItem; } - + private: const_iterator(const VmaRawList* pList, const VmaListItem* pItem) : m_pList(pList), @@ -6035,222 +5208,6 @@ private: #endif // #if VMA_USE_STL_LIST -//////////////////////////////////////////////////////////////////////////////// -// class VmaIntrusiveLinkedList - -/* -Expected interface of ItemTypeTraits: -struct MyItemTypeTraits -{ - typedef MyItem ItemType; - static ItemType* GetPrev(const ItemType* item) { return item->myPrevPtr; } - static ItemType* GetNext(const ItemType* item) { return item->myNextPtr; } - static ItemType*& AccessPrev(ItemType* item) { return item->myPrevPtr; } - static ItemType*& AccessNext(ItemType* item) { return item->myNextPtr; } -}; -*/ -template -class VmaIntrusiveLinkedList -{ -public: - typedef typename ItemTypeTraits::ItemType ItemType; - static ItemType* GetPrev(const ItemType* item) { return ItemTypeTraits::GetPrev(item); } - static ItemType* GetNext(const ItemType* item) { return ItemTypeTraits::GetNext(item); } - // Movable, not copyable. - VmaIntrusiveLinkedList() { } - VmaIntrusiveLinkedList(const VmaIntrusiveLinkedList& src) = delete; - VmaIntrusiveLinkedList(VmaIntrusiveLinkedList&& src) : - m_Front(src.m_Front), m_Back(src.m_Back), m_Count(src.m_Count) - { - src.m_Front = src.m_Back = VMA_NULL; - src.m_Count = 0; - } - ~VmaIntrusiveLinkedList() - { - VMA_HEAVY_ASSERT(IsEmpty()); - } - VmaIntrusiveLinkedList& operator=(const VmaIntrusiveLinkedList& src) = delete; - VmaIntrusiveLinkedList& operator=(VmaIntrusiveLinkedList&& src) - { - if(&src != this) - { - VMA_HEAVY_ASSERT(IsEmpty()); - m_Front = src.m_Front; - m_Back = src.m_Back; - m_Count = src.m_Count; - src.m_Front = src.m_Back = VMA_NULL; - src.m_Count = 0; - } - return *this; - } - void RemoveAll() - { - if(!IsEmpty()) - { - ItemType* item = m_Back; - while(item != VMA_NULL) - { - ItemType* const prevItem = ItemTypeTraits::AccessPrev(item); - ItemTypeTraits::AccessPrev(item) = VMA_NULL; - ItemTypeTraits::AccessNext(item) = VMA_NULL; - item = prevItem; - } - m_Front = VMA_NULL; - m_Back = VMA_NULL; - m_Count = 0; - } - } - size_t GetCount() const { return m_Count; } - bool IsEmpty() const { return m_Count == 0; } - ItemType* Front() { return m_Front; } - const ItemType* Front() const { return m_Front; } - ItemType* Back() { return m_Back; } - const ItemType* Back() const { return m_Back; } - void PushBack(ItemType* item) - { - VMA_HEAVY_ASSERT(ItemTypeTraits::GetPrev(item) == VMA_NULL && ItemTypeTraits::GetNext(item) == VMA_NULL); - if(IsEmpty()) - { - m_Front = item; - m_Back = item; - m_Count = 1; - } - else - { - ItemTypeTraits::AccessPrev(item) = m_Back; - ItemTypeTraits::AccessNext(m_Back) = item; - m_Back = item; - ++m_Count; - } - } - void PushFront(ItemType* item) - { - VMA_HEAVY_ASSERT(ItemTypeTraits::GetPrev(item) == VMA_NULL && ItemTypeTraits::GetNext(item) == VMA_NULL); - if(IsEmpty()) - { - m_Front = item; - m_Back = item; - m_Count = 1; - } - else - { - ItemTypeTraits::AccessNext(item) = m_Front; - ItemTypeTraits::AccessPrev(m_Front) = item; - m_Front = item; - ++m_Count; - } - } - ItemType* PopBack() - { - VMA_HEAVY_ASSERT(m_Count > 0); - ItemType* const backItem = m_Back; - ItemType* const prevItem = ItemTypeTraits::GetPrev(backItem); - if(prevItem != VMA_NULL) - { - ItemTypeTraits::AccessNext(prevItem) = VMA_NULL; - } - m_Back = prevItem; - --m_Count; - ItemTypeTraits::AccessPrev(backItem) = VMA_NULL; - ItemTypeTraits::AccessNext(backItem) = VMA_NULL; - return backItem; - } - ItemType* PopFront() - { - VMA_HEAVY_ASSERT(m_Count > 0); - ItemType* const frontItem = m_Front; - ItemType* const nextItem = ItemTypeTraits::GetNext(frontItem); - if(nextItem != VMA_NULL) - { - ItemTypeTraits::AccessPrev(nextItem) = VMA_NULL; - } - m_Front = nextItem; - --m_Count; - ItemTypeTraits::AccessPrev(frontItem) = VMA_NULL; - ItemTypeTraits::AccessNext(frontItem) = VMA_NULL; - return frontItem; - } - - // MyItem can be null - it means PushBack. - void InsertBefore(ItemType* existingItem, ItemType* newItem) - { - VMA_HEAVY_ASSERT(newItem != VMA_NULL && ItemTypeTraits::GetPrev(newItem) == VMA_NULL && ItemTypeTraits::GetNext(newItem) == VMA_NULL); - if(existingItem != VMA_NULL) - { - ItemType* const prevItem = ItemTypeTraits::GetPrev(existingItem); - ItemTypeTraits::AccessPrev(newItem) = prevItem; - ItemTypeTraits::AccessNext(newItem) = existingItem; - ItemTypeTraits::AccessPrev(existingItem) = newItem; - if(prevItem != VMA_NULL) - { - ItemTypeTraits::AccessNext(prevItem) = newItem; - } - else - { - VMA_HEAVY_ASSERT(m_Front == existingItem); - m_Front = newItem; - } - ++m_Count; - } - else - PushBack(newItem); - } - // MyItem can be null - it means PushFront. - void InsertAfter(ItemType* existingItem, ItemType* newItem) - { - VMA_HEAVY_ASSERT(newItem != VMA_NULL && ItemTypeTraits::GetPrev(newItem) == VMA_NULL && ItemTypeTraits::GetNext(newItem) == VMA_NULL); - if(existingItem != VMA_NULL) - { - ItemType* const nextItem = ItemTypeTraits::GetNext(existingItem); - ItemTypeTraits::AccessNext(newItem) = nextItem; - ItemTypeTraits::AccessPrev(newItem) = existingItem; - ItemTypeTraits::AccessNext(existingItem) = newItem; - if(nextItem != VMA_NULL) - { - ItemTypeTraits::AccessPrev(nextItem) = newItem; - } - else - { - VMA_HEAVY_ASSERT(m_Back == existingItem); - m_Back = newItem; - } - ++m_Count; - } - else - return PushFront(newItem); - } - void Remove(ItemType* item) - { - VMA_HEAVY_ASSERT(item != VMA_NULL && m_Count > 0); - if(ItemTypeTraits::GetPrev(item) != VMA_NULL) - { - ItemTypeTraits::AccessNext(ItemTypeTraits::AccessPrev(item)) = ItemTypeTraits::GetNext(item); - } - else - { - VMA_HEAVY_ASSERT(m_Front == item); - m_Front = ItemTypeTraits::GetNext(item); - } - - if(ItemTypeTraits::GetNext(item) != VMA_NULL) - { - ItemTypeTraits::AccessPrev(ItemTypeTraits::AccessNext(item)) = ItemTypeTraits::GetPrev(item); - } - else - { - VMA_HEAVY_ASSERT(m_Back == item); - m_Back = ItemTypeTraits::GetPrev(item); - } - ItemTypeTraits::AccessPrev(item) = VMA_NULL; - ItemTypeTraits::AccessNext(item) = VMA_NULL; - --m_Count; - } -private: - ItemType* m_Front = VMA_NULL; - ItemType* m_Back = VMA_NULL; - size_t m_Count = 0; -}; - //////////////////////////////////////////////////////////////////////////////// // class VmaMap @@ -6294,7 +5251,7 @@ public: void insert(const PairType& pair); iterator find(const KeyT& key); void erase(iterator it); - + private: VmaVector< PairType, VmaStlAllocator > m_Vector; }; @@ -6381,24 +5338,25 @@ public: This struct is allocated using VmaPoolAllocator. */ - VmaAllocation_T(uint32_t currentFrameIndex, bool userDataString) : - m_Alignment{1}, - m_Size{0}, - m_pUserData{VMA_NULL}, - m_LastUseFrameIndex{currentFrameIndex}, - m_MemoryTypeIndex{0}, - m_Type{(uint8_t)ALLOCATION_TYPE_NONE}, - m_SuballocationType{(uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN}, - m_MapCount{0}, - m_Flags{userDataString ? (uint8_t)FLAG_USER_DATA_STRING : (uint8_t)0} + void Ctor(uint32_t currentFrameIndex, bool userDataString) { + m_Alignment = 1; + m_Size = 0; + m_MemoryTypeIndex = 0; + m_pUserData = VMA_NULL; + m_LastUseFrameIndex = currentFrameIndex; + m_Type = (uint8_t)ALLOCATION_TYPE_NONE; + m_SuballocationType = (uint8_t)VMA_SUBALLOCATION_TYPE_UNKNOWN; + m_MapCount = 0; + m_Flags = userDataString ? (uint8_t)FLAG_USER_DATA_STRING : 0; + #if VMA_STATS_STRING_ENABLED m_CreationFrameIndex = currentFrameIndex; m_BufferImageUsage = 0; #endif } - ~VmaAllocation_T() + void Dtor() { VMA_ASSERT((m_MapCount & ~MAP_COUNT_FLAG_PERSISTENT_MAP) == 0 && "Allocation was not unmapped before destruction."); @@ -6443,7 +5401,7 @@ public: void ChangeBlockAllocation( VmaAllocator hAllocator, VmaDeviceMemoryBlock* block, - VkDeviceSize offset); + VkDeviceSize offset); void ChangeOffset(VkDeviceSize newOffset); @@ -6465,8 +5423,6 @@ public: m_MapCount = (pMappedData != VMA_NULL) ? MAP_COUNT_FLAG_PERSISTENT_MAP : 0; m_DedicatedAllocation.m_hMemory = hMemory; m_DedicatedAllocation.m_pMappedData = pMappedData; - m_DedicatedAllocation.m_Prev = VMA_NULL; - m_DedicatedAllocation.m_Next = VMA_NULL; } ALLOCATION_TYPE GetType() const { return (ALLOCATION_TYPE)m_Type; } @@ -6488,7 +5444,7 @@ public: bool IsPersistentMap() const { return (m_MapCount & MAP_COUNT_FLAG_PERSISTENT_MAP) != 0; } void* GetMappedData() const; bool CanBecomeLost() const; - + uint32_t GetLastUseFrameIndex() const { return m_LastUseFrameIndex.load(); @@ -6501,7 +5457,7 @@ public: - If hAllocation.LastUseFrameIndex + frameInUseCount < allocator.CurrentFrameIndex, makes it lost by setting LastUseFrameIndex = VMA_FRAME_INDEX_LOST and returns true. - Else, returns false. - + If hAllocation is already lost, assert - you should not call it then. If hAllocation was not created with CAN_BECOME_LOST_BIT, assert. */ @@ -6564,8 +5520,6 @@ private: { VkDeviceMemory m_hMemory; void* m_pMappedData; // Not null means memory is mapped. - VmaAllocation_T* m_Prev; - VmaAllocation_T* m_Next; }; union @@ -6582,32 +5536,6 @@ private: #endif void FreeUserDataString(VmaAllocator hAllocator); - - friend struct VmaDedicatedAllocationListItemTraits; -}; - -struct VmaDedicatedAllocationListItemTraits -{ - typedef VmaAllocation_T ItemType; - static ItemType* GetPrev(const ItemType* item) - { - VMA_HEAVY_ASSERT(item->GetType() == VmaAllocation_T::ALLOCATION_TYPE_DEDICATED); - return item->m_DedicatedAllocation.m_Prev; - } - static ItemType* GetNext(const ItemType* item) - { - VMA_HEAVY_ASSERT(item->GetType() == VmaAllocation_T::ALLOCATION_TYPE_DEDICATED); - return item->m_DedicatedAllocation.m_Next; - } - static ItemType*& AccessPrev(ItemType* item) - { - VMA_HEAVY_ASSERT(item->GetType() == VmaAllocation_T::ALLOCATION_TYPE_DEDICATED); - return item->m_DedicatedAllocation.m_Prev; - } - static ItemType*& AccessNext(ItemType* item){ - VMA_HEAVY_ASSERT(item->GetType() == VmaAllocation_T::ALLOCATION_TYPE_DEDICATED); - return item->m_DedicatedAllocation.m_Next; - } }; /* @@ -6825,7 +5753,7 @@ public: //////////////////////////////////////////////////////////////////////////////// // For defragmentation - + bool IsBufferImageGranularityConflictPossible( VkDeviceSize bufferImageGranularity, VmaSuballocationType& inOutPrevSuballocType) const; @@ -7036,7 +5964,7 @@ private: SuballocationVectorType& AccessSuballocations2nd() { return m_1stVectorIndex ? m_Suballocations0 : m_Suballocations1; } const SuballocationVectorType& AccessSuballocations1st() const { return m_1stVectorIndex ? m_Suballocations1 : m_Suballocations0; } const SuballocationVectorType& AccessSuballocations2nd() const { return m_1stVectorIndex ? m_Suballocations0 : m_Suballocations1; } - + // Number of items in 1st vector with hAllocation = null at the beginning. size_t m_1stNullItemsBeginCount; // Number of other items in 1st vector with hAllocation = null somewhere in the middle. @@ -7074,7 +6002,7 @@ private: - m_UsableSize is this size aligned down to a power of two. All allocations and calculations happen relative to m_UsableSize. - GetUnusableSize() is the difference between them. - It is reported as separate, unused range, not available for allocations. + It is repoted as separate, unused range, not available for allocations. Node at level 0 has size = m_UsableSize. Each next level contains nodes with size 2 times smaller than current level. @@ -7247,7 +6175,7 @@ public: uint32_t algorithm); // Always call before destruction. void Destroy(VmaAllocator allocator); - + VmaPool GetParentPool() const { return m_hParentPool; } VkDeviceMemory GetDeviceMemory() const { return m_hMemory; } uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; } @@ -7295,6 +6223,14 @@ private: void* m_pMappedData; }; +struct VmaPointerLess +{ + bool operator()(const void* lhs, const void* rhs) const + { + return lhs < rhs; + } +}; + struct VmaDefragmentationMove { size_t srcBlockIndex; @@ -7302,9 +6238,6 @@ struct VmaDefragmentationMove VkDeviceSize srcOffset; VkDeviceSize dstOffset; VkDeviceSize size; - VmaAllocation hAllocation; - VmaDeviceMemoryBlock* pSrcBlock; - VmaDeviceMemoryBlock* pDstBlock; }; class VmaDefragmentationAlgorithm; @@ -7329,8 +6262,7 @@ public: VkDeviceSize bufferImageGranularity, uint32_t frameInUseCount, bool explicitBlockSize, - uint32_t algorithm, - float priority); + uint32_t algorithm); ~VmaBlockVector(); VkResult CreateMinBlocks(); @@ -7375,21 +6307,12 @@ public: // Saves results in pCtx->res. void Defragment( class VmaBlockVectorDefragmentationContext* pCtx, - VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags, + VmaDefragmentationStats* pStats, VkDeviceSize& maxCpuBytesToMove, uint32_t& maxCpuAllocationsToMove, VkDeviceSize& maxGpuBytesToMove, uint32_t& maxGpuAllocationsToMove, VkCommandBuffer commandBuffer); void DefragmentationEnd( class VmaBlockVectorDefragmentationContext* pCtx, - uint32_t flags, - VmaDefragmentationStats* pStats); - - uint32_t ProcessDefragmentations( - class VmaBlockVectorDefragmentationContext *pCtx, - VmaDefragmentationPassMoveInfo* pMove, uint32_t maxMoves); - - void CommitDefragmentations( - class VmaBlockVectorDefragmentationContext *pCtx, VmaDefragmentationStats* pStats); //////////////////////////////////////////////////////////////////////////////// @@ -7413,7 +6336,6 @@ private: const uint32_t m_FrameInUseCount; const bool m_ExplicitBlockSize; const uint32_t m_Algorithm; - const float m_Priority; VMA_RW_MUTEX m_Mutex; /* There can be at most one allocation that is completely empty (except when minBlockCount > 0) - @@ -7461,7 +6383,7 @@ private: // Saves result to pCtx->res. void ApplyDefragmentationMovesGpu( class VmaBlockVectorDefragmentationContext* pDefragCtx, - VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, + const VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkCommandBuffer commandBuffer); /* @@ -7498,18 +6420,6 @@ public: private: uint32_t m_Id; char* m_Name; - VmaPool_T* m_PrevPool = VMA_NULL; - VmaPool_T* m_NextPool = VMA_NULL; - friend struct VmaPoolListItemTraits; -}; - -struct VmaPoolListItemTraits -{ - typedef VmaPool_T ItemType; - static ItemType* GetPrev(const ItemType* item) { return item->m_PrevPool; } - static ItemType* GetNext(const ItemType* item) { return item->m_NextPool; } - static ItemType*& AccessPrev(ItemType* item) { return item->m_PrevPool; } - static ItemType*& AccessNext(ItemType* item) { return item->m_NextPool; } }; /* @@ -7542,8 +6452,7 @@ public: virtual VkResult Defragment( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - VmaDefragmentationFlags flags) = 0; + uint32_t maxAllocationsToMove) = 0; virtual VkDeviceSize GetBytesMoved() const = 0; virtual uint32_t GetAllocationsMoved() const = 0; @@ -7588,8 +6497,7 @@ public: virtual VkResult Defragment( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - VmaDefragmentationFlags flags); + uint32_t maxAllocationsToMove); virtual VkDeviceSize GetBytesMoved() const { return m_BytesMoved; } virtual uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; } @@ -7690,8 +6598,7 @@ private: VkResult DefragmentRound( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - bool freeOldAllocations); + uint32_t maxAllocationsToMove); size_t CalcBlocksWithNonMovableCount() const; @@ -7717,8 +6624,7 @@ public: virtual VkResult Defragment( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - VmaDefragmentationFlags flags); + uint32_t maxAllocationsToMove); virtual VkDeviceSize GetBytesMoved() const { return m_BytesMoved; } virtual uint32_t GetAllocationsMoved() const { return m_AllocationsMoved; } @@ -7798,7 +6704,7 @@ private: } } } - + if(bestIndex != SIZE_MAX) { outBlockInfoIndex = m_FreeSpaces[bestIndex].blockInfoIndex; @@ -7866,10 +6772,6 @@ public: VkResult res; bool mutexLocked; VmaVector< VmaBlockDefragmentationContext, VmaStlAllocator > blockContexts; - VmaVector< VmaDefragmentationMove, VmaStlAllocator > defragmentationMoves; - uint32_t defragmentationMovesProcessed; - uint32_t defragmentationMovesCommitted; - bool hasDefragmentationPlan; VmaBlockVectorDefragmentationContext( VmaAllocator hAllocator, @@ -7885,7 +6787,7 @@ public: void AddAllocation(VmaAllocation hAlloc, VkBool32* pChanged); void AddAll() { m_AllAllocations = true; } - void Begin(bool overlappingMoveSupported, VmaDefragmentationFlags flags); + void Begin(bool overlappingMoveSupported); private: const VmaAllocator m_hAllocator; @@ -7919,37 +6821,28 @@ public: VmaDefragmentationStats* pStats); ~VmaDefragmentationContext_T(); - void AddPools(uint32_t poolCount, const VmaPool* pPools); + void AddPools(uint32_t poolCount, VmaPool* pPools); void AddAllocations( uint32_t allocationCount, - const VmaAllocation* pAllocations, + VmaAllocation* pAllocations, VkBool32* pAllocationsChanged); /* Returns: - `VK_SUCCESS` if succeeded and object can be destroyed immediately. - `VK_NOT_READY` if succeeded but the object must remain alive until vmaDefragmentationEnd(). - - Negative value if error occurred and object can be destroyed immediately. + - Negative value if error occured and object can be destroyed immediately. */ VkResult Defragment( VkDeviceSize maxCpuBytesToMove, uint32_t maxCpuAllocationsToMove, VkDeviceSize maxGpuBytesToMove, uint32_t maxGpuAllocationsToMove, - VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags); - - VkResult DefragmentPassBegin(VmaDefragmentationPassInfo* pInfo); - VkResult DefragmentPassEnd(); + VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats); private: const VmaAllocator m_hAllocator; const uint32_t m_CurrFrameIndex; const uint32_t m_Flags; VmaDefragmentationStats* const m_pStats; - - VkDeviceSize m_MaxCpuBytesToMove; - uint32_t m_MaxCpuAllocationsToMove; - VkDeviceSize m_MaxGpuBytesToMove; - uint32_t m_MaxGpuAllocationsToMove; - // Owner of these objects. VmaBlockVectorDefragmentationContext* m_DefaultPoolContexts[VK_MAX_MEMORY_TYPES]; // Owner of these objects. @@ -7969,8 +6862,7 @@ public: uint32_t vulkanApiVersion, bool dedicatedAllocationExtensionEnabled, bool bindMemory2ExtensionEnabled, - bool memoryBudgetExtensionEnabled, - bool deviceCoherentMemoryExtensionEnabled); + bool memoryBudgetExtensionEnabled); ~VmaRecorder(); void RecordCreateAllocator(uint32_t frameIndex); @@ -8067,7 +6959,8 @@ private: VmaRecordFlags m_Flags; FILE* m_File; VMA_MUTEX m_FileMutex; - std::chrono::time_point m_RecordingStartTime; + int64_t m_Freq; + int64_t m_StartCounter; void GetBasicParams(CallParams& outParams); @@ -8100,7 +6993,7 @@ class VmaAllocationObjectAllocator public: VmaAllocationObjectAllocator(const VkAllocationCallbacks* pAllocationCallbacks); - template VmaAllocation Allocate(Types... args); + VmaAllocation Allocate(); void Free(VmaAllocation hAlloc); private: @@ -8167,16 +7060,13 @@ public: bool m_UseKhrDedicatedAllocation; // Can be set only if m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0). bool m_UseKhrBindMemory2; // Can be set only if m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0). bool m_UseExtMemoryBudget; - bool m_UseAmdDeviceCoherentMemory; - bool m_UseKhrBufferDeviceAddress; - bool m_UseExtMemoryPriority; VkDevice m_hDevice; VkInstance m_hInstance; bool m_AllocationCallbacksSpecified; VkAllocationCallbacks m_AllocationCallbacks; VmaDeviceMemoryCallbacks m_DeviceMemoryCallbacks; VmaAllocationObjectAllocator m_AllocationObjectAllocator; - + // Each bit (1 << i) is set if HeapSizeLimit is enabled for that heap, so cannot allocate more than the heap size. uint32_t m_HeapSizeLimitMask; @@ -8186,12 +7076,12 @@ public: // Default pools. VmaBlockVector* m_pBlockVectors[VK_MAX_MEMORY_TYPES]; - typedef VmaIntrusiveLinkedList DedicatedAllocationLinkedList; - DedicatedAllocationLinkedList m_DedicatedAllocations[VK_MAX_MEMORY_TYPES]; + // Each vector is sorted by memory (handle value). + typedef VmaVector< VmaAllocation, VmaStlAllocator > AllocationVectorType; + AllocationVectorType* m_pDedicatedAllocations[VK_MAX_MEMORY_TYPES]; VMA_RW_MUTEX m_DedicatedAllocationsMutex[VK_MAX_MEMORY_TYPES]; VmaCurrentBudgetData m_Budget; - VMA_ATOMIC_UINT32 m_DeviceMemoryCount; // Total number of VkDeviceMemory objects. VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo); VkResult Init(const VmaAllocatorCreateInfo* pCreateInfo); @@ -8206,8 +7096,6 @@ public: return m_VulkanFunctions; } - VkPhysicalDevice GetPhysicalDevice() const { return m_PhysicalDevice; } - VkDeviceSize GetBufferImageGranularity() const { return VMA_MAX( @@ -8233,8 +7121,8 @@ public: VkDeviceSize GetMemoryTypeMinAlignment(uint32_t memTypeIndex) const { return IsMemoryTypeNonCoherent(memTypeIndex) ? - VMA_MAX((VkDeviceSize)VMA_MIN_ALIGNMENT, m_PhysicalDeviceProperties.limits.nonCoherentAtomSize) : - (VkDeviceSize)VMA_MIN_ALIGNMENT; + VMA_MAX((VkDeviceSize)VMA_DEBUG_ALIGNMENT, m_PhysicalDeviceProperties.limits.nonCoherentAtomSize) : + (VkDeviceSize)VMA_DEBUG_ALIGNMENT; } bool IsIntegratedGpu() const @@ -8242,8 +7130,6 @@ public: return m_PhysicalDeviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_INTEGRATED_GPU; } - uint32_t GetGlobalMemoryTypeBits() const { return m_GlobalMemoryTypeBits; } - #if VMA_RECORDING_ENABLED VmaRecorder* GetRecorder() const { return m_pRecorder; } #endif @@ -8265,7 +7151,6 @@ public: bool requiresDedicatedAllocation, bool prefersDedicatedAllocation, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, // UINT32_MAX when unknown. VkImage dedicatedImage, const VmaAllocationCreateInfo& createInfo, VmaSuballocationType suballocType, @@ -8277,6 +7162,10 @@ public: size_t allocationCount, const VmaAllocation* pAllocations); + VkResult ResizeAllocation( + const VmaAllocation alloc, + VkDeviceSize newSize); + void CalculateStats(VmaStats* pStats); void GetBudget( @@ -8293,12 +7182,6 @@ public: VkResult DefragmentationEnd( VmaDefragmentationContext context); - VkResult DefragmentationPassBegin( - VmaDefragmentationPassInfo* pInfo, - VmaDefragmentationContext context); - VkResult DefragmentationPassEnd( - VmaDefragmentationContext context); - void GetAllocationInfo(VmaAllocation hAllocation, VmaAllocationInfo* pAllocationInfo); bool TouchAllocation(VmaAllocation hAllocation); @@ -8348,15 +7231,10 @@ public: VkImage hImage, const void* pNext); - VkResult FlushOrInvalidateAllocation( + void FlushOrInvalidateAllocation( VmaAllocation hAllocation, VkDeviceSize offset, VkDeviceSize size, VMA_CACHE_OPERATION op); - VkResult FlushOrInvalidateAllocations( - uint32_t allocationCount, - const VmaAllocation* allocations, - const VkDeviceSize* offsets, const VkDeviceSize* sizes, - VMA_CACHE_OPERATION op); void FillAllocation(const VmaAllocation hAllocation, uint8_t pattern); @@ -8372,36 +7250,20 @@ private: VkPhysicalDevice m_PhysicalDevice; VMA_ATOMIC_UINT32 m_CurrentFrameIndex; VMA_ATOMIC_UINT32 m_GpuDefragmentationMemoryTypeBits; // UINT32_MAX means uninitialized. - + VMA_RW_MUTEX m_PoolsMutex; - typedef VmaIntrusiveLinkedList PoolList; - // Protected by m_PoolsMutex. - PoolList m_Pools; + // Protected by m_PoolsMutex. Sorted by pointer value. + VmaVector > m_Pools; uint32_t m_NextPoolId; VmaVulkanFunctions m_VulkanFunctions; - // Global bit mask AND-ed with any memoryTypeBits to disallow certain memory types. - uint32_t m_GlobalMemoryTypeBits; - #if VMA_RECORDING_ENABLED VmaRecorder* m_pRecorder; #endif void ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunctions); -#if VMA_STATIC_VULKAN_FUNCTIONS == 1 - void ImportVulkanFunctions_Static(); -#endif - - void ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVulkanFunctions); - -#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1 - void ImportVulkanFunctions_Dynamic(); -#endif - - void ValidateVulkanFunctions(); - VkDeviceSize CalcPreferredBlockSize(uint32_t memTypeIndex); VkResult AllocateMemoryOfType( @@ -8409,7 +7271,6 @@ private: VkDeviceSize alignment, bool dedicatedAllocation, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, VkImage dedicatedImage, const VmaAllocationCreateInfo& createInfo, uint32_t memTypeIndex, @@ -8437,9 +7298,7 @@ private: bool map, bool isUserDataString, void* pUserData, - float priority, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, VkImage dedicatedImage, size_t allocationCount, VmaAllocation* pAllocations); @@ -8452,13 +7311,6 @@ private: */ uint32_t CalculateGpuDefragmentationMemoryTypeBits() const; - uint32_t CalculateGlobalMemoryTypeBits() const; - - bool GetFlushOrInvalidateRange( - VmaAllocation allocation, - VkDeviceSize offset, VkDeviceSize size, - VkMappedMemoryRange& outRange) const; - #if VMA_MEMORY_BUDGET void UpdateVulkanBudget(); #endif // #if VMA_MEMORY_BUDGET @@ -8595,10 +7447,10 @@ public: void BeginObject(bool singleLine = false); void EndObject(); - + void BeginArray(bool singleLine = false); void EndArray(); - + void WriteString(const char* pStr); void BeginString(const char* pStr = VMA_NULL); void ContinueString(const char* pStr); @@ -8606,7 +7458,7 @@ public: void ContinueString(uint64_t n); void ContinueString_Pointer(const void* ptr); void EndString(const char* pStr = VMA_NULL); - + void WriteNumber(uint32_t n); void WriteNumber(uint64_t n); void WriteBool(bool b); @@ -8854,7 +7706,7 @@ void VmaJsonWriter::WriteIndent(bool oneLess) if(!m_Stack.empty() && !m_Stack.back().singleLineMode) { m_SB.AddNewLine(); - + size_t count = m_Stack.size(); if(count > 0 && oneLess) { @@ -9268,7 +8120,7 @@ void VmaBlockMetadata::PrintDetailedMap_Allocation(class VmaJsonWriter& json, VmaAllocation hAllocation) const { json.BeginObject(true); - + json.WriteString("Offset"); json.WriteNumber(offset); @@ -9282,7 +8134,7 @@ void VmaBlockMetadata::PrintDetailedMap_UnusedRange(class VmaJsonWriter& json, VkDeviceSize size) const { json.BeginObject(true); - + json.WriteString("Offset"); json.WriteNumber(offset); @@ -9342,7 +8194,7 @@ void VmaBlockMetadata_Generic::Init(VkDeviceSize size) bool VmaBlockMetadata_Generic::Validate() const { VMA_VALIDATE(!m_Suballocations.empty()); - + // Expected offset of new suballocation as calculated from previous ones. VkDeviceSize calculatedOffset = 0; // Expected number of free suballocations as calculated from traversing their list. @@ -9360,7 +8212,7 @@ bool VmaBlockMetadata_Generic::Validate() const ++suballocItem) { const VmaSuballocation& subAlloc = *suballocItem; - + // Actual offset of this suballocation doesn't match expected one. VMA_VALIDATE(subAlloc.offset == calculatedOffset); @@ -9403,7 +8255,7 @@ bool VmaBlockMetadata_Generic::Validate() const for(size_t i = 0; i < m_FreeSuballocationsBySize.size(); ++i) { VmaSuballocationList::iterator suballocItem = m_FreeSuballocationsBySize[i]; - + // Only free suballocations can be registered in m_FreeSuballocationsBySize. VMA_VALIDATE(suballocItem->type == VMA_SUBALLOCATION_TYPE_FREE); // They must be sorted by size ascending. @@ -9412,7 +8264,7 @@ bool VmaBlockMetadata_Generic::Validate() const lastSize = suballocItem->size; } - // Check if totals match calculated values. + // Check if totals match calculacted values. VMA_VALIDATE(ValidateFreeSuballocationList()); VMA_VALIDATE(calculatedOffset == GetSize()); VMA_VALIDATE(calculatedSumFreeSize == m_SumFreeSize); @@ -9445,7 +8297,7 @@ void VmaBlockMetadata_Generic::CalcAllocationStatInfo(VmaStatInfo& outInfo) cons const uint32_t rangeCount = (uint32_t)m_Suballocations.size(); outInfo.allocationCount = rangeCount - m_FreeCount; outInfo.unusedRangeCount = m_FreeCount; - + outInfo.unusedBytes = m_SumFreeSize; outInfo.usedBytes = GetSize() - outInfo.unusedBytes; @@ -9704,7 +8556,7 @@ bool VmaBlockMetadata_Generic::MakeRequestedAllocationsLost( VMA_HEAVY_ASSERT(Validate()); VMA_ASSERT(pAllocationRequest->item != m_Suballocations.end()); VMA_ASSERT(pAllocationRequest->item->type == VMA_SUBALLOCATION_TYPE_FREE); - + return true; } @@ -9881,7 +8733,7 @@ bool VmaBlockMetadata_Generic::CheckAllocation( VMA_ASSERT(allocType != VMA_SUBALLOCATION_TYPE_FREE); VMA_ASSERT(suballocItem != m_Suballocations.cend()); VMA_ASSERT(pOffset != VMA_NULL); - + *itemsToMakeLostCount = 0; *pSumFreeSize = 0; *pSumItemSize = 0; @@ -9914,19 +8766,19 @@ bool VmaBlockMetadata_Generic::CheckAllocation( // Start from offset equal to beginning of this suballocation. *pOffset = suballocItem->offset; - + // Apply VMA_DEBUG_MARGIN at the beginning. if(VMA_DEBUG_MARGIN > 0) { *pOffset += VMA_DEBUG_MARGIN; } - + // Apply alignment. *pOffset = VmaAlignUp(*pOffset, allocAlignment); // Check previous suballocations for BufferImageGranularity conflicts. // Make bigger alignment if necessary. - if(bufferImageGranularity > 1 && bufferImageGranularity != allocAlignment) + if(bufferImageGranularity > 1) { bool bufferImageGranularityConflict = false; VmaSuballocationList::const_iterator prevSuballocItem = suballocItem; @@ -9951,14 +8803,14 @@ bool VmaBlockMetadata_Generic::CheckAllocation( *pOffset = VmaAlignUp(*pOffset, bufferImageGranularity); } } - + // Now that we have final *pOffset, check if we are past suballocItem. // If yes, return false - this function should be called for another suballocItem as starting point. if(*pOffset >= suballocItem->offset + suballocItem->size) { return false; } - + // Calculate padding at the beginning based on current offset. const VkDeviceSize paddingBegin = *pOffset - suballocItem->offset; @@ -10010,7 +8862,7 @@ bool VmaBlockMetadata_Generic::CheckAllocation( // Check next suballocations for BufferImageGranularity conflicts. // If conflict exists, we must mark more allocations lost or fail. - if(allocSize % bufferImageGranularity || *pOffset % bufferImageGranularity) + if(bufferImageGranularity > 1) { VmaSuballocationList::const_iterator nextSuballocItem = lastSuballocItem; ++nextSuballocItem; @@ -10057,19 +8909,19 @@ bool VmaBlockMetadata_Generic::CheckAllocation( // Start from offset equal to beginning of this suballocation. *pOffset = suballoc.offset; - + // Apply VMA_DEBUG_MARGIN at the beginning. if(VMA_DEBUG_MARGIN > 0) { *pOffset += VMA_DEBUG_MARGIN; } - + // Apply alignment. *pOffset = VmaAlignUp(*pOffset, allocAlignment); - + // Check previous suballocations for BufferImageGranularity conflicts. // Make bigger alignment if necessary. - if(bufferImageGranularity > 1 && bufferImageGranularity != allocAlignment) + if(bufferImageGranularity > 1) { bool bufferImageGranularityConflict = false; VmaSuballocationList::const_iterator prevSuballocItem = suballocItem; @@ -10094,7 +8946,7 @@ bool VmaBlockMetadata_Generic::CheckAllocation( *pOffset = VmaAlignUp(*pOffset, bufferImageGranularity); } } - + // Calculate padding at the beginning based on current offset. const VkDeviceSize paddingBegin = *pOffset - suballoc.offset; @@ -10109,7 +8961,7 @@ bool VmaBlockMetadata_Generic::CheckAllocation( // Check next suballocations for BufferImageGranularity conflicts. // If conflict exists, allocation cannot be made here. - if(allocSize % bufferImageGranularity || *pOffset % bufferImageGranularity) + if(bufferImageGranularity > 1) { VmaSuballocationList::const_iterator nextSuballocItem = suballocItem; ++nextSuballocItem; @@ -10141,7 +8993,7 @@ void VmaBlockMetadata_Generic::MergeFreeWithNext(VmaSuballocationList::iterator { VMA_ASSERT(item != m_Suballocations.end()); VMA_ASSERT(item->type == VMA_SUBALLOCATION_TYPE_FREE); - + VmaSuballocationList::iterator nextItem = item; ++nextItem; VMA_ASSERT(nextItem != m_Suballocations.end()); @@ -10158,7 +9010,7 @@ VmaSuballocationList::iterator VmaBlockMetadata_Generic::FreeSuballocation(VmaSu VmaSuballocation& suballoc = *suballocItem; suballoc.type = VMA_SUBALLOCATION_TYPE_FREE; suballoc.hAllocation = VK_NULL_HANDLE; - + // Update totals. ++m_FreeCount; m_SumFreeSize += suballoc.size; @@ -10166,7 +9018,7 @@ VmaSuballocationList::iterator VmaBlockMetadata_Generic::FreeSuballocation(VmaSu // Merge with previous and/or next suballocation if it's also free. bool mergeWithNext = false; bool mergeWithPrev = false; - + VmaSuballocationList::iterator nextItem = suballocItem; ++nextItem; if((nextItem != m_Suballocations.end()) && (nextItem->type == VMA_SUBALLOCATION_TYPE_FREE)) @@ -10464,7 +9316,7 @@ VkDeviceSize VmaBlockMetadata_Linear::GetUnusedRangeSizeMax() const { return size; } - + const SuballocationVectorType& suballocations1st = AccessSuballocations1st(); switch(m_2ndVectorMode) @@ -10472,7 +9324,7 @@ VkDeviceSize VmaBlockMetadata_Linear::GetUnusedRangeSizeMax() const case SECOND_VECTOR_EMPTY: /* Available space is after end of 1st, as well as before beginning of 1st (which - would make it a ring buffer). + whould make it a ring buffer). */ { const size_t suballocations1stCount = suballocations1st.size(); @@ -10551,7 +9403,7 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const if(nextAlloc2ndIndex < suballoc2ndCount) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10562,13 +9414,13 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize); outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. outInfo.usedBytes += suballoc.size; outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size); outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc2ndIndex; @@ -10608,7 +9460,7 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const if(nextAlloc1stIndex < suballoc1stCount) { const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10619,13 +9471,13 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize); outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. outInfo.usedBytes += suballoc.size; outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size); outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc1stIndex; @@ -10664,7 +9516,7 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const if(nextAlloc2ndIndex != SIZE_MAX) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10675,13 +9527,13 @@ void VmaBlockMetadata_Linear::CalcAllocationStatInfo(VmaStatInfo& outInfo) const outInfo.unusedRangeSizeMin = VMA_MIN(outInfo.unusedRangeSizeMin, unusedRangeSize); outInfo.unusedRangeSizeMax = VMA_MIN(outInfo.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. outInfo.usedBytes += suballoc.size; outInfo.allocationSizeMin = VMA_MIN(outInfo.allocationSizeMin, suballoc.size); outInfo.allocationSizeMax = VMA_MIN(outInfo.allocationSizeMax, suballoc.size); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; --nextAlloc2ndIndex; @@ -10737,7 +9589,7 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const if(nextAlloc2ndIndex < suballoc2ndCount) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10747,11 +9599,11 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const ++inoutStats.unusedRangeCount; inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++inoutStats.allocationCount; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc2ndIndex; @@ -10790,7 +9642,7 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const if(nextAlloc1stIndex < suballoc1stCount) { const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10800,11 +9652,11 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const ++inoutStats.unusedRangeCount; inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++inoutStats.allocationCount; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc1stIndex; @@ -10842,7 +9694,7 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const if(nextAlloc2ndIndex != SIZE_MAX) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -10852,11 +9704,11 @@ void VmaBlockMetadata_Linear::AddPoolStats(VmaPoolStats& inoutStats) const ++inoutStats.unusedRangeCount; inoutStats.unusedRangeSizeMax = VMA_MAX(inoutStats.unusedRangeSizeMax, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++inoutStats.allocationCount; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; --nextAlloc2ndIndex; @@ -10914,19 +9766,19 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc2ndIndex < suballoc2ndCount) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { // There is free space from lastOffset to suballoc.offset. ++unusedRangeCount; } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++alloc2ndCount; usedBytes += suballoc.size; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc2ndIndex; @@ -10963,19 +9815,19 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc1stIndex < suballoc1stCount) { const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { // There is free space from lastOffset to suballoc.offset. ++unusedRangeCount; } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++alloc1stCount; usedBytes += suballoc.size; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc1stIndex; @@ -11010,19 +9862,19 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc2ndIndex != SIZE_MAX) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { // There is free space from lastOffset to suballoc.offset. ++unusedRangeCount; } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. ++alloc2ndCount; usedBytes += suballoc.size; - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; --nextAlloc2ndIndex; @@ -11065,7 +9917,7 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc2ndIndex < suballoc2ndCount) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -11073,11 +9925,11 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset; PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc2ndIndex; @@ -11112,7 +9964,7 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc1stIndex < suballoc1stCount) { const VmaSuballocation& suballoc = suballocations1st[nextAlloc1stIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -11120,11 +9972,11 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset; PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; ++nextAlloc1stIndex; @@ -11160,7 +10012,7 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const if(nextAlloc2ndIndex != SIZE_MAX) { const VmaSuballocation& suballoc = suballocations2nd[nextAlloc2ndIndex]; - + // 1. Process free space before this allocation. if(lastOffset < suballoc.offset) { @@ -11168,11 +10020,11 @@ void VmaBlockMetadata_Linear::PrintDetailedMap(class VmaJsonWriter& json) const const VkDeviceSize unusedRangeSize = suballoc.offset - lastOffset; PrintDetailedMap_UnusedRange(json, lastOffset, unusedRangeSize); } - + // 2. Process this allocation. // There is allocation with suballoc.offset, suballoc.size. PrintDetailedMap_Allocation(json, suballoc.offset, suballoc.hAllocation); - + // 3. Prepare for next iteration. lastOffset = suballoc.offset + suballoc.size; --nextAlloc2ndIndex; @@ -11277,7 +10129,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_UpperAddress( // Check next suballocations from 2nd for BufferImageGranularity conflicts. // Make bigger alignment if necessary. - if(bufferImageGranularity > 1 && bufferImageGranularity != allocAlignment && !suballocations2nd.empty()) + if(bufferImageGranularity > 1 && !suballocations2nd.empty()) { bool bufferImageGranularityConflict = false; for(size_t nextSuballocIndex = suballocations2nd.size(); nextSuballocIndex--; ) @@ -11382,7 +10234,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress( // Check previous suballocations for BufferImageGranularity conflicts. // Make bigger alignment if necessary. - if(bufferImageGranularity > 1 && bufferImageGranularity != allocAlignment && !suballocations1st.empty()) + if(bufferImageGranularity > 1 && !suballocations1st.empty()) { bool bufferImageGranularityConflict = false; for(size_t prevSuballocIndex = suballocations1st.size(); prevSuballocIndex--; ) @@ -11414,7 +10266,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress( { // Check next suballocations for BufferImageGranularity conflicts. // If conflict exists, allocation cannot be made here. - if((allocSize % bufferImageGranularity || resultOffset % bufferImageGranularity) && m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK) + if(bufferImageGranularity > 1 && m_2ndVectorMode == SECOND_VECTOR_DOUBLE_STACK) { for(size_t nextSuballocIndex = suballocations2nd.size(); nextSuballocIndex--; ) { @@ -11472,7 +10324,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress( // Check previous suballocations for BufferImageGranularity conflicts. // Make bigger alignment if necessary. - if(bufferImageGranularity > 1 && bufferImageGranularity != allocAlignment && !suballocations2nd.empty()) + if(bufferImageGranularity > 1 && !suballocations2nd.empty()) { bool bufferImageGranularityConflict = false; for(size_t prevSuballocIndex = suballocations2nd.size(); prevSuballocIndex--; ) @@ -11530,7 +10382,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress( // Check next suballocations for BufferImageGranularity conflicts. // If conflict exists, we must mark more allocations lost or fail. - if(allocSize % bufferImageGranularity || resultOffset % bufferImageGranularity) + if(bufferImageGranularity > 1) { while(index1st < suballocations1st.size()) { @@ -11576,7 +10428,7 @@ bool VmaBlockMetadata_Linear::CreateAllocationRequest_LowerAddress( { // Check next suballocations for BufferImageGranularity conflicts. // If conflict exists, allocation cannot be made here. - if(allocSize % bufferImageGranularity || resultOffset % bufferImageGranularity) + if(bufferImageGranularity > 1) { for(size_t nextSuballocIndex = index1st; nextSuballocIndex < suballocations1st.size(); @@ -11624,7 +10476,7 @@ bool VmaBlockMetadata_Linear::MakeRequestedAllocationsLost( } VMA_ASSERT(m_2ndVectorMode == SECOND_VECTOR_EMPTY || m_2ndVectorMode == SECOND_VECTOR_RING_BUFFER); - + // We always start from 1st. SuballocationVectorType* suballocations = &AccessSuballocations1st(); size_t index = m_1stNullItemsBeginCount; @@ -11672,15 +10524,15 @@ bool VmaBlockMetadata_Linear::MakeRequestedAllocationsLost( } CleanupAfterFree(); - //VMA_HEAVY_ASSERT(Validate()); // Already called by CleanupAfterFree(). - + //VMA_HEAVY_ASSERT(Validate()); // Already called by ClanupAfterFree(). + return true; } uint32_t VmaBlockMetadata_Linear::MakeAllocationsLost(uint32_t currentFrameIndex, uint32_t frameInUseCount) { uint32_t lostAllocationCount = 0; - + SuballocationVectorType& suballocations1st = AccessSuballocations1st(); for(size_t i = m_1stNullItemsBeginCount, count = suballocations1st.size(); i < count; ++i) { @@ -12104,7 +10956,7 @@ bool VmaBlockMetadata_Buddy::Validate() const node = node->free.next) { VMA_VALIDATE(node->type == Node::TYPE_FREE); - + if(node->free.next == VMA_NULL) { VMA_VALIDATE(m_FreeList[level].back == node); @@ -12290,7 +11142,7 @@ void VmaBlockMetadata_Buddy::Alloc( const uint32_t targetLevel = AllocSizeToLevel(allocSize); uint32_t currLevel = (uint32_t)(uintptr_t)request.customData; - + Node* currNode = m_FreeList[currLevel].front; VMA_ASSERT(currNode != VMA_NULL && currNode->type == Node::TYPE_FREE); while(currNode->offset != request.offset) @@ -12298,14 +11150,14 @@ void VmaBlockMetadata_Buddy::Alloc( currNode = currNode->free.next; VMA_ASSERT(currNode != VMA_NULL && currNode->type == Node::TYPE_FREE); } - + // Go down, splitting free nodes. while(currLevel < targetLevel) { // currNode is already first free node at currLevel. // Remove it from list of free nodes at this currLevel. RemoveFromFreeList(currLevel, currNode); - + const uint32_t childrenLevel = currLevel + 1; // Create two free sub-nodes. @@ -12467,7 +11319,7 @@ void VmaBlockMetadata_Buddy::FreeAtOffset(VmaAllocation alloc, VkDeviceSize offs vma_delete(GetAllocationCallbacks(), node->buddy); vma_delete(GetAllocationCallbacks(), node); parent->type = Node::TYPE_FREE; - + node = parent; --level; //m_SumFreeSize += LevelToNodeSize(level) % 2; // Useful only when level node sizes can be non power of 2. @@ -12581,7 +11433,7 @@ void VmaBlockMetadata_Buddy::PrintDetailedMapNode(class VmaJsonWriter& json, con PrintDetailedMap_UnusedRange(json, node->offset, levelNodeSize); break; case Node::TYPE_ALLOCATION: - { + { PrintDetailedMap_Allocation(json, node->offset, node->allocation.alloc); const VkDeviceSize allocSize = node->allocation.alloc->GetSize(); if(allocSize < levelNodeSize) @@ -12670,7 +11522,7 @@ bool VmaDeviceMemoryBlock::Validate() const { VMA_VALIDATE((m_hMemory != VK_NULL_HANDLE) && (m_pMetadata->GetSize() != 0)); - + return m_pMetadata->Validate(); } @@ -12875,8 +11727,7 @@ VmaPool_T::VmaPool_T( (createInfo.flags & VMA_POOL_CREATE_IGNORE_BUFFER_IMAGE_GRANULARITY_BIT) != 0 ? 1 : hAllocator->GetBufferImageGranularity(), createInfo.frameInUseCount, createInfo.blockSize != 0, // explicitBlockSize - createInfo.flags & VMA_POOL_CREATE_ALGORITHM_MASK, - createInfo.priority), // algorithm + createInfo.flags & VMA_POOL_CREATE_ALGORITHM_MASK), // algorithm m_Id(0), m_Name(VMA_NULL) { @@ -12884,14 +11735,13 @@ VmaPool_T::VmaPool_T( VmaPool_T::~VmaPool_T() { - VMA_ASSERT(m_PrevPool == VMA_NULL && m_NextPool == VMA_NULL); } void VmaPool_T::SetName(const char* pName) { const VkAllocationCallbacks* allocs = m_BlockVector.GetAllocator()->GetAllocationCallbacks(); VmaFreeString(allocs, m_Name); - + if(pName != VMA_NULL) { m_Name = VmaCreateStringCopy(allocs, pName); @@ -12916,8 +11766,7 @@ VmaBlockVector::VmaBlockVector( VkDeviceSize bufferImageGranularity, uint32_t frameInUseCount, bool explicitBlockSize, - uint32_t algorithm, - float priority) : + uint32_t algorithm) : m_hAllocator(hAllocator), m_hParentPool(hParentPool), m_MemoryTypeIndex(memoryTypeIndex), @@ -12928,7 +11777,6 @@ VmaBlockVector::VmaBlockVector( m_FrameInUseCount(frameInUseCount), m_ExplicitBlockSize(explicitBlockSize), m_Algorithm(algorithm), - m_Priority(priority), m_HasEmptyBlock(false), m_Blocks(VmaStlAllocator(hAllocator->GetAllocationCallbacks())), m_NextBlockId(0) @@ -13035,13 +11883,9 @@ VkResult VmaBlockVector::Allocate( if(res != VK_SUCCESS) { // Free all already created allocations. - const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex); while(allocIndex--) { - VmaAllocation_T* const alloc = pAllocations[allocIndex]; - const VkDeviceSize allocSize = alloc->GetSize(); - Free(alloc); - m_hAllocator->m_Budget.RemoveAllocation(heapIndex, allocSize); + Free(pAllocations[allocIndex]); } memset(pAllocations, 0, sizeof(VmaAllocation) * allocationCount); } @@ -13061,7 +11905,8 @@ VkResult VmaBlockVector::AllocatePage( bool canMakeOtherLost = (createInfo.flags & VMA_ALLOCATION_CREATE_CAN_MAKE_OTHER_LOST_BIT) != 0; const bool mapped = (createInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0; const bool isUserDataString = (createInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0; - + + const bool withinBudget = (createInfo.flags & VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT) != 0; VkDeviceSize freeMemory; { const uint32_t heapIndex = m_hAllocator->MemoryTypeIndexToHeapIndex(m_MemoryTypeIndex); @@ -13069,7 +11914,7 @@ VkResult VmaBlockVector::AllocatePage( m_hAllocator->GetBudget(&heapBudget, heapIndex, 1); freeMemory = (heapBudget.usage < heapBudget.budget) ? (heapBudget.budget - heapBudget.usage) : 0; } - + const bool canFallbackToDedicated = !IsCustomPool(); const bool canCreateNewBlock = ((createInfo.flags & VMA_ALLOCATION_CREATE_NEVER_ALLOCATE_BIT) == 0) && @@ -13380,7 +12225,8 @@ VkResult VmaBlockVector::AllocatePage( &bestRequest)) { // Allocate from this pBlock. - *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString); + *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(); + (*pAllocation)->Ctor(currentFrameIndex, isUserDataString); pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation); UpdateHasEmptyBlock(); (*pAllocation)->InitBlockAllocation( @@ -13583,8 +12429,9 @@ VkResult VmaBlockVector::AllocateFromBlock( return res; } } - - *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(currentFrameIndex, isUserDataString); + + *pAllocation = m_hAllocator->m_AllocationObjectAllocator.Allocate(); + (*pAllocation)->Ctor(currentFrameIndex, isUserDataString); pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation); UpdateHasEmptyBlock(); (*pAllocation)->InitBlockAllocation( @@ -13618,26 +12465,6 @@ VkResult VmaBlockVector::CreateBlock(VkDeviceSize blockSize, size_t* pNewBlockIn VkMemoryAllocateInfo allocInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO }; allocInfo.memoryTypeIndex = m_MemoryTypeIndex; allocInfo.allocationSize = blockSize; - -#if VMA_BUFFER_DEVICE_ADDRESS - // Every standalone block can potentially contain a buffer with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT - always enable the feature. - VkMemoryAllocateFlagsInfoKHR allocFlagsInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR }; - if(m_hAllocator->m_UseKhrBufferDeviceAddress) - { - allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR; - VmaPnextChainPushFront(&allocInfo, &allocFlagsInfo); - } -#endif // #if VMA_BUFFER_DEVICE_ADDRESS - -#if VMA_MEMORY_PRIORITY - VkMemoryPriorityAllocateInfoEXT priorityInfo = { VK_STRUCTURE_TYPE_MEMORY_PRIORITY_ALLOCATE_INFO_EXT }; - if(m_hAllocator->m_UseExtMemoryPriority) - { - priorityInfo.priority = m_Priority; - VmaPnextChainPushFront(&allocInfo, &priorityInfo); - } -#endif // #if VMA_MEMORY_PRIORITY - VkDeviceMemory mem = VK_NULL_HANDLE; VkResult res = m_hAllocator->AllocateVulkanMemory(&allocInfo, &mem); if(res < 0) @@ -13788,7 +12615,7 @@ void VmaBlockVector::ApplyDefragmentationMovesCpu( void VmaBlockVector::ApplyDefragmentationMovesGpu( class VmaBlockVectorDefragmentationContext* pDefragCtx, - VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, + const VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkCommandBuffer commandBuffer) { const size_t blockCount = m_Blocks.size(); @@ -13801,13 +12628,8 @@ void VmaBlockVector::ApplyDefragmentationMovesGpu( for(size_t moveIndex = 0; moveIndex < moveCount; ++moveIndex) { const VmaDefragmentationMove& move = moves[moveIndex]; - - //if(move.type == VMA_ALLOCATION_TYPE_UNKNOWN) - { - // Old school move still require us to map the whole block - pDefragCtx->blockContexts[move.srcBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED; - pDefragCtx->blockContexts[move.dstBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED; - } + pDefragCtx->blockContexts[move.srcBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED; + pDefragCtx->blockContexts[move.dstBlockIndex].flags |= VmaBlockDefragmentationContext::BLOCK_FLAG_USED; } VMA_ASSERT(pDefragCtx->res == VK_SUCCESS); @@ -13981,13 +12803,13 @@ void VmaBlockVector::PrintDetailedMap(class VmaJsonWriter& json) void VmaBlockVector::Defragment( class VmaBlockVectorDefragmentationContext* pCtx, - VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags, + VmaDefragmentationStats* pStats, VkDeviceSize& maxCpuBytesToMove, uint32_t& maxCpuAllocationsToMove, VkDeviceSize& maxGpuBytesToMove, uint32_t& maxGpuAllocationsToMove, VkCommandBuffer commandBuffer) { pCtx->res = VK_SUCCESS; - + const VkMemoryPropertyFlags memPropFlags = m_hAllocator->m_MemProps.memoryTypes[m_MemoryTypeIndex].propertyFlags; const bool isHostVisible = (memPropFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) != 0; @@ -14018,28 +12840,19 @@ void VmaBlockVector::Defragment( if(m_hAllocator->m_UseMutex) { - if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL) - { - if(!m_Mutex.TryLockWrite()) - { - pCtx->res = VK_ERROR_INITIALIZATION_FAILED; - return; - } - } - else - { - m_Mutex.LockWrite(); - pCtx->mutexLocked = true; - } + m_Mutex.LockWrite(); + pCtx->mutexLocked = true; } - pCtx->Begin(overlappingMoveSupported, flags); + pCtx->Begin(overlappingMoveSupported); // Defragment. const VkDeviceSize maxBytesToMove = defragmentOnGpu ? maxGpuBytesToMove : maxCpuBytesToMove; const uint32_t maxAllocationsToMove = defragmentOnGpu ? maxGpuAllocationsToMove : maxCpuAllocationsToMove; - pCtx->res = pCtx->GetAlgorithm()->Defragment(pCtx->defragmentationMoves, maxBytesToMove, maxAllocationsToMove, flags); + VmaVector< VmaDefragmentationMove, VmaStlAllocator > moves = + VmaVector< VmaDefragmentationMove, VmaStlAllocator >(VmaStlAllocator(m_hAllocator->GetAllocationCallbacks())); + pCtx->res = pCtx->GetAlgorithm()->Defragment(moves, maxBytesToMove, maxAllocationsToMove); // Accumulate statistics. if(pStats != VMA_NULL) @@ -14061,27 +12874,16 @@ void VmaBlockVector::Defragment( maxCpuAllocationsToMove -= allocationsMoved; } } - - if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL) - { - if(m_hAllocator->m_UseMutex) - m_Mutex.UnlockWrite(); - - if(pCtx->res >= VK_SUCCESS && !pCtx->defragmentationMoves.empty()) - pCtx->res = VK_NOT_READY; - - return; - } - + if(pCtx->res >= VK_SUCCESS) { if(defragmentOnGpu) { - ApplyDefragmentationMovesGpu(pCtx, pCtx->defragmentationMoves, commandBuffer); + ApplyDefragmentationMovesGpu(pCtx, moves, commandBuffer); } else { - ApplyDefragmentationMovesCpu(pCtx, pCtx->defragmentationMoves); + ApplyDefragmentationMovesCpu(pCtx, moves); } } } @@ -14089,36 +12891,22 @@ void VmaBlockVector::Defragment( void VmaBlockVector::DefragmentationEnd( class VmaBlockVectorDefragmentationContext* pCtx, - uint32_t flags, VmaDefragmentationStats* pStats) { - if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL && m_hAllocator->m_UseMutex) + // Destroy buffers. + for(size_t blockIndex = pCtx->blockContexts.size(); blockIndex--; ) { - VMA_ASSERT(pCtx->mutexLocked == false); - - // Incremental defragmentation doesn't hold the lock, so when we enter here we don't actually have any - // lock protecting us. Since we mutate state here, we have to take the lock out now - m_Mutex.LockWrite(); - pCtx->mutexLocked = true; + VmaBlockDefragmentationContext& blockCtx = pCtx->blockContexts[blockIndex]; + if(blockCtx.hBuffer) + { + (*m_hAllocator->GetVulkanFunctions().vkDestroyBuffer)( + m_hAllocator->m_hDevice, blockCtx.hBuffer, m_hAllocator->GetAllocationCallbacks()); + } } - // If the mutex isn't locked we didn't do any work and there is nothing to delete. - if(pCtx->mutexLocked || !m_hAllocator->m_UseMutex) + if(pCtx->res >= VK_SUCCESS) { - // Destroy buffers. - for(size_t blockIndex = pCtx->blockContexts.size(); blockIndex--;) - { - VmaBlockDefragmentationContext &blockCtx = pCtx->blockContexts[blockIndex]; - if(blockCtx.hBuffer) - { - (*m_hAllocator->GetVulkanFunctions().vkDestroyBuffer)(m_hAllocator->m_hDevice, blockCtx.hBuffer, m_hAllocator->GetAllocationCallbacks()); - } - } - - if(pCtx->res >= VK_SUCCESS) - { - FreeEmptyBlocks(pStats); - } + FreeEmptyBlocks(pStats); } if(pCtx->mutexLocked) @@ -14128,48 +12916,6 @@ void VmaBlockVector::DefragmentationEnd( } } -uint32_t VmaBlockVector::ProcessDefragmentations( - class VmaBlockVectorDefragmentationContext *pCtx, - VmaDefragmentationPassMoveInfo* pMove, uint32_t maxMoves) -{ - VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex); - - const uint32_t moveCount = VMA_MIN(uint32_t(pCtx->defragmentationMoves.size()) - pCtx->defragmentationMovesProcessed, maxMoves); - - for(uint32_t i = 0; i < moveCount; ++ i) - { - VmaDefragmentationMove& move = pCtx->defragmentationMoves[pCtx->defragmentationMovesProcessed + i]; - - pMove->allocation = move.hAllocation; - pMove->memory = move.pDstBlock->GetDeviceMemory(); - pMove->offset = move.dstOffset; - - ++ pMove; - } - - pCtx->defragmentationMovesProcessed += moveCount; - - return moveCount; -} - -void VmaBlockVector::CommitDefragmentations( - class VmaBlockVectorDefragmentationContext *pCtx, - VmaDefragmentationStats* pStats) -{ - VmaMutexLockWrite lock(m_Mutex, m_hAllocator->m_UseMutex); - - for(uint32_t i = pCtx->defragmentationMovesCommitted; i < pCtx->defragmentationMovesProcessed; ++ i) - { - const VmaDefragmentationMove &move = pCtx->defragmentationMoves[i]; - - move.pSrcBlock->m_pMetadata->FreeAtOffset(move.srcOffset); - move.hAllocation->ChangeBlockAllocation(m_hAllocator, move.pDstBlock, move.dstOffset); - } - - pCtx->defragmentationMovesCommitted = pCtx->defragmentationMovesProcessed; - FreeEmptyBlocks(pStats); -} - size_t VmaBlockVector::CalcAllocationCount() const { size_t result = 0; @@ -14320,8 +13066,7 @@ void VmaDefragmentationAlgorithm_Generic::AddAllocation(VmaAllocation hAlloc, Vk VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - bool freeOldAllocations) + uint32_t maxAllocationsToMove) { if(m_Blocks.empty()) { @@ -14376,7 +13121,7 @@ VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound( srcAllocIndex = m_Blocks[srcBlockIndex]->m_Allocations.size() - 1; } } - + BlockInfo* pSrcBlockInfo = m_Blocks[srcBlockIndex]; AllocationInfo& allocInfo = pSrcBlockInfo->m_Allocations[srcAllocIndex]; @@ -14413,16 +13158,12 @@ VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound( return VK_SUCCESS; } - VmaDefragmentationMove move = {}; + VmaDefragmentationMove move; move.srcBlockIndex = pSrcBlockInfo->m_OriginalBlockIndex; move.dstBlockIndex = pDstBlockInfo->m_OriginalBlockIndex; move.srcOffset = srcOffset; move.dstOffset = dstAllocRequest.offset; move.size = size; - move.hAllocation = allocInfo.m_hAllocation; - move.pSrcBlock = pSrcBlockInfo->m_pBlock; - move.pDstBlock = pDstBlockInfo->m_pBlock; - moves.push_back(move); pDstBlockInfo->m_pBlock->m_pMetadata->Alloc( @@ -14430,12 +13171,9 @@ VkResult VmaDefragmentationAlgorithm_Generic::DefragmentRound( suballocType, size, allocInfo.m_hAllocation); - - if(freeOldAllocations) - { - pSrcBlockInfo->m_pBlock->m_pMetadata->FreeAtOffset(srcOffset); - allocInfo.m_hAllocation->ChangeBlockAllocation(m_hAllocator, pDstBlockInfo->m_pBlock, dstAllocRequest.offset); - } + pSrcBlockInfo->m_pBlock->m_pMetadata->FreeAtOffset(srcOffset); + + allocInfo.m_hAllocation->ChangeBlockAllocation(m_hAllocator, pDstBlockInfo->m_pBlock, dstAllocRequest.offset); if(allocInfo.m_pChanged != VMA_NULL) { @@ -14488,8 +13226,7 @@ size_t VmaDefragmentationAlgorithm_Generic::CalcBlocksWithNonMovableCount() cons VkResult VmaDefragmentationAlgorithm_Generic::Defragment( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - VmaDefragmentationFlags flags) + uint32_t maxAllocationsToMove) { if(!m_AllAllocations && m_AllocationCount == 0) { @@ -14517,7 +13254,7 @@ VkResult VmaDefragmentationAlgorithm_Generic::Defragment( } pBlockInfo->CalcHasNonMovableAllocations(); - + // This is a choice based on research. // Option 1: pBlockInfo->SortAllocationsByOffsetDescending(); @@ -14535,7 +13272,7 @@ VkResult VmaDefragmentationAlgorithm_Generic::Defragment( VkResult result = VK_SUCCESS; for(uint32_t round = 0; (round < roundCount) && (result == VK_SUCCESS); ++round) { - result = DefragmentRound(moves, maxBytesToMove, maxAllocationsToMove, !(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL)); + result = DefragmentRound(moves, maxBytesToMove, maxAllocationsToMove); } return result; @@ -14587,8 +13324,7 @@ VmaDefragmentationAlgorithm_Fast::~VmaDefragmentationAlgorithm_Fast() VkResult VmaDefragmentationAlgorithm_Fast::Defragment( VmaVector< VmaDefragmentationMove, VmaStlAllocator >& moves, VkDeviceSize maxBytesToMove, - uint32_t maxAllocationsToMove, - VmaDefragmentationFlags flags) + uint32_t maxAllocationsToMove) { VMA_ASSERT(m_AllAllocations || m_pBlockVector->CalcAllocationCount() == m_AllocationCount); @@ -14644,7 +13380,6 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( } const VkDeviceSize srcAllocOffset = srcSuballocIt->offset; - VmaDefragmentationMove move = {}; // Try to place it in one of free spaces from the database. size_t freeSpaceInfoIndex; VkDeviceSize dstAllocOffset; @@ -14667,7 +13402,7 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( suballoc.hAllocation->ChangeOffset(dstAllocOffset); m_BytesMoved += srcAllocSize; ++m_AllocationsMoved; - + VmaSuballocationList::iterator nextSuballocIt = srcSuballocIt; ++nextSuballocIt; pSrcMetadata->m_Suballocations.erase(srcSuballocIt); @@ -14675,12 +13410,10 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( InsertSuballoc(pFreeSpaceMetadata, suballoc); - move.srcBlockIndex = srcOrigBlockIndex; - move.dstBlockIndex = freeSpaceOrigBlockIndex; - move.srcOffset = srcAllocOffset; - move.dstOffset = dstAllocOffset; - move.size = srcAllocSize; - + VmaDefragmentationMove move = { + srcOrigBlockIndex, freeSpaceOrigBlockIndex, + srcAllocOffset, dstAllocOffset, + srcAllocSize }; moves.push_back(move); } // Different block @@ -14703,12 +13436,10 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( InsertSuballoc(pFreeSpaceMetadata, suballoc); - move.srcBlockIndex = srcOrigBlockIndex; - move.dstBlockIndex = freeSpaceOrigBlockIndex; - move.srcOffset = srcAllocOffset; - move.dstOffset = dstAllocOffset; - move.size = srcAllocSize; - + VmaDefragmentationMove move = { + srcOrigBlockIndex, freeSpaceOrigBlockIndex, + srcAllocOffset, dstAllocOffset, + srcAllocSize }; moves.push_back(move); } } @@ -14763,13 +13494,10 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( m_BytesMoved += srcAllocSize; ++m_AllocationsMoved; ++srcSuballocIt; - - move.srcBlockIndex = srcOrigBlockIndex; - move.dstBlockIndex = dstOrigBlockIndex; - move.srcOffset = srcAllocOffset; - move.dstOffset = dstAllocOffset; - move.size = srcAllocSize; - + VmaDefragmentationMove move = { + srcOrigBlockIndex, dstOrigBlockIndex, + srcAllocOffset, dstAllocOffset, + srcAllocSize }; moves.push_back(move); } } @@ -14795,12 +13523,10 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( pDstMetadata->m_Suballocations.push_back(suballoc); - move.srcBlockIndex = srcOrigBlockIndex; - move.dstBlockIndex = dstOrigBlockIndex; - move.srcOffset = srcAllocOffset; - move.dstOffset = dstAllocOffset; - move.size = srcAllocSize; - + VmaDefragmentationMove move = { + srcOrigBlockIndex, dstOrigBlockIndex, + srcAllocOffset, dstAllocOffset, + srcAllocSize }; moves.push_back(move); } } @@ -14808,7 +13534,7 @@ VkResult VmaDefragmentationAlgorithm_Fast::Defragment( } m_BlockInfos.clear(); - + PostprocessMetadata(); return VK_SUCCESS; @@ -14850,7 +13576,7 @@ void VmaDefragmentationAlgorithm_Fast::PostprocessMetadata() VmaBlockMetadata_Generic* const pMetadata = (VmaBlockMetadata_Generic*)m_pBlockVector->GetBlock(blockIndex)->m_pMetadata; const VkDeviceSize blockSize = pMetadata->GetSize(); - + // No allocations in this block - entire area is free. if(pMetadata->m_Suballocations.empty()) { @@ -14950,10 +13676,6 @@ VmaBlockVectorDefragmentationContext::VmaBlockVectorDefragmentationContext( res(VK_SUCCESS), mutexLocked(false), blockContexts(VmaStlAllocator(hAllocator->GetAllocationCallbacks())), - defragmentationMoves(VmaStlAllocator(hAllocator->GetAllocationCallbacks())), - defragmentationMovesProcessed(0), - defragmentationMovesCommitted(0), - hasDefragmentationPlan(0), m_hAllocator(hAllocator), m_hCustomPool(hCustomPool), m_pBlockVector(pBlockVector), @@ -14975,7 +13697,7 @@ void VmaBlockVectorDefragmentationContext::AddAllocation(VmaAllocation hAlloc, V m_Allocations.push_back(info); } -void VmaBlockVectorDefragmentationContext::Begin(bool overlappingMoveSupported, VmaDefragmentationFlags flags) +void VmaBlockVectorDefragmentationContext::Begin(bool overlappingMoveSupported) { const bool allAllocations = m_AllAllocations || m_Allocations.size() == m_pBlockVector->CalcAllocationCount(); @@ -14989,12 +13711,10 @@ void VmaBlockVectorDefragmentationContext::Begin(bool overlappingMoveSupported, - VMA_DEBUG_MARGIN is 0. - All allocations in this block vector are moveable. - There is no possibility of image/buffer granularity conflict. - - The defragmentation is not incremental */ if(VMA_DEBUG_MARGIN == 0 && allAllocations && - !m_pBlockVector->IsBufferImageGranularityConflictPossible() && - !(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL)) + !m_pBlockVector->IsBufferImageGranularityConflictPossible()) { m_pAlgorithm = vma_new(m_hAllocator, VmaDefragmentationAlgorithm_Fast)( m_hAllocator, m_pBlockVector, m_CurrFrameIndex, overlappingMoveSupported); @@ -15040,7 +13760,7 @@ VmaDefragmentationContext_T::~VmaDefragmentationContext_T() for(size_t i = m_CustomPoolContexts.size(); i--; ) { VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_CustomPoolContexts[i]; - pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_Flags, m_pStats); + pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_pStats); vma_delete(m_hAllocator, pBlockVectorCtx); } for(size_t i = m_hAllocator->m_MemProps.memoryTypeCount; i--; ) @@ -15048,13 +13768,13 @@ VmaDefragmentationContext_T::~VmaDefragmentationContext_T() VmaBlockVectorDefragmentationContext* pBlockVectorCtx = m_DefaultPoolContexts[i]; if(pBlockVectorCtx) { - pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_Flags, m_pStats); + pBlockVectorCtx->GetBlockVector()->DefragmentationEnd(pBlockVectorCtx, m_pStats); vma_delete(m_hAllocator, pBlockVectorCtx); } } } -void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, const VmaPool* pPools) +void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, VmaPool* pPools) { for(uint32_t poolIndex = 0; poolIndex < poolCount; ++poolIndex) { @@ -15064,7 +13784,7 @@ void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, const VmaPool* pP if(pool->m_BlockVector.GetAlgorithm() == 0) { VmaBlockVectorDefragmentationContext* pBlockVectorDefragCtx = VMA_NULL; - + for(size_t i = m_CustomPoolContexts.size(); i--; ) { if(m_CustomPoolContexts[i]->GetCustomPool() == pool) @@ -15073,7 +13793,7 @@ void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, const VmaPool* pP break; } } - + if(!pBlockVectorDefragCtx) { pBlockVectorDefragCtx = vma_new(m_hAllocator, VmaBlockVectorDefragmentationContext)( @@ -15091,7 +13811,7 @@ void VmaDefragmentationContext_T::AddPools(uint32_t poolCount, const VmaPool* pP void VmaDefragmentationContext_T::AddAllocations( uint32_t allocationCount, - const VmaAllocation* pAllocations, + VmaAllocation* pAllocations, VkBool32* pAllocationsChanged) { // Dispatch pAllocations among defragmentators. Create them when necessary. @@ -15161,30 +13881,13 @@ void VmaDefragmentationContext_T::AddAllocations( VkResult VmaDefragmentationContext_T::Defragment( VkDeviceSize maxCpuBytesToMove, uint32_t maxCpuAllocationsToMove, VkDeviceSize maxGpuBytesToMove, uint32_t maxGpuAllocationsToMove, - VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats, VmaDefragmentationFlags flags) + VkCommandBuffer commandBuffer, VmaDefragmentationStats* pStats) { if(pStats) { memset(pStats, 0, sizeof(VmaDefragmentationStats)); } - if(flags & VMA_DEFRAGMENTATION_FLAG_INCREMENTAL) - { - // For incremental defragmetnations, we just earmark how much we can move - // The real meat is in the defragmentation steps - m_MaxCpuBytesToMove = maxCpuBytesToMove; - m_MaxCpuAllocationsToMove = maxCpuAllocationsToMove; - - m_MaxGpuBytesToMove = maxGpuBytesToMove; - m_MaxGpuAllocationsToMove = maxGpuAllocationsToMove; - - if(m_MaxCpuBytesToMove == 0 && m_MaxCpuAllocationsToMove == 0 && - m_MaxGpuBytesToMove == 0 && m_MaxGpuAllocationsToMove == 0) - return VK_SUCCESS; - - return VK_NOT_READY; - } - if(commandBuffer == VK_NULL_HANDLE) { maxGpuBytesToMove = 0; @@ -15204,7 +13907,7 @@ VkResult VmaDefragmentationContext_T::Defragment( VMA_ASSERT(pBlockVectorCtx->GetBlockVector()); pBlockVectorCtx->GetBlockVector()->Defragment( pBlockVectorCtx, - pStats, flags, + pStats, maxCpuBytesToMove, maxCpuAllocationsToMove, maxGpuBytesToMove, maxGpuAllocationsToMove, commandBuffer); @@ -15224,7 +13927,7 @@ VkResult VmaDefragmentationContext_T::Defragment( VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector()); pBlockVectorCtx->GetBlockVector()->Defragment( pBlockVectorCtx, - pStats, flags, + pStats, maxCpuBytesToMove, maxCpuAllocationsToMove, maxGpuBytesToMove, maxGpuAllocationsToMove, commandBuffer); @@ -15237,132 +13940,6 @@ VkResult VmaDefragmentationContext_T::Defragment( return res; } -VkResult VmaDefragmentationContext_T::DefragmentPassBegin(VmaDefragmentationPassInfo* pInfo) -{ - VmaDefragmentationPassMoveInfo* pCurrentMove = pInfo->pMoves; - uint32_t movesLeft = pInfo->moveCount; - - // Process default pools. - for(uint32_t memTypeIndex = 0; - memTypeIndex < m_hAllocator->GetMemoryTypeCount(); - ++memTypeIndex) - { - VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_DefaultPoolContexts[memTypeIndex]; - if(pBlockVectorCtx) - { - VMA_ASSERT(pBlockVectorCtx->GetBlockVector()); - - if(!pBlockVectorCtx->hasDefragmentationPlan) - { - pBlockVectorCtx->GetBlockVector()->Defragment( - pBlockVectorCtx, - m_pStats, m_Flags, - m_MaxCpuBytesToMove, m_MaxCpuAllocationsToMove, - m_MaxGpuBytesToMove, m_MaxGpuAllocationsToMove, - VK_NULL_HANDLE); - - if(pBlockVectorCtx->res < VK_SUCCESS) - continue; - - pBlockVectorCtx->hasDefragmentationPlan = true; - } - - const uint32_t processed = pBlockVectorCtx->GetBlockVector()->ProcessDefragmentations( - pBlockVectorCtx, - pCurrentMove, movesLeft); - - movesLeft -= processed; - pCurrentMove += processed; - } - } - - // Process custom pools. - for(size_t customCtxIndex = 0, customCtxCount = m_CustomPoolContexts.size(); - customCtxIndex < customCtxCount; - ++customCtxIndex) - { - VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_CustomPoolContexts[customCtxIndex]; - VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector()); - - if(!pBlockVectorCtx->hasDefragmentationPlan) - { - pBlockVectorCtx->GetBlockVector()->Defragment( - pBlockVectorCtx, - m_pStats, m_Flags, - m_MaxCpuBytesToMove, m_MaxCpuAllocationsToMove, - m_MaxGpuBytesToMove, m_MaxGpuAllocationsToMove, - VK_NULL_HANDLE); - - if(pBlockVectorCtx->res < VK_SUCCESS) - continue; - - pBlockVectorCtx->hasDefragmentationPlan = true; - } - - const uint32_t processed = pBlockVectorCtx->GetBlockVector()->ProcessDefragmentations( - pBlockVectorCtx, - pCurrentMove, movesLeft); - - movesLeft -= processed; - pCurrentMove += processed; - } - - pInfo->moveCount = pInfo->moveCount - movesLeft; - - return VK_SUCCESS; -} -VkResult VmaDefragmentationContext_T::DefragmentPassEnd() -{ - VkResult res = VK_SUCCESS; - - // Process default pools. - for(uint32_t memTypeIndex = 0; - memTypeIndex < m_hAllocator->GetMemoryTypeCount(); - ++memTypeIndex) - { - VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_DefaultPoolContexts[memTypeIndex]; - if(pBlockVectorCtx) - { - VMA_ASSERT(pBlockVectorCtx->GetBlockVector()); - - if(!pBlockVectorCtx->hasDefragmentationPlan) - { - res = VK_NOT_READY; - continue; - } - - pBlockVectorCtx->GetBlockVector()->CommitDefragmentations( - pBlockVectorCtx, m_pStats); - - if(pBlockVectorCtx->defragmentationMoves.size() != pBlockVectorCtx->defragmentationMovesCommitted) - res = VK_NOT_READY; - } - } - - // Process custom pools. - for(size_t customCtxIndex = 0, customCtxCount = m_CustomPoolContexts.size(); - customCtxIndex < customCtxCount; - ++customCtxIndex) - { - VmaBlockVectorDefragmentationContext *pBlockVectorCtx = m_CustomPoolContexts[customCtxIndex]; - VMA_ASSERT(pBlockVectorCtx && pBlockVectorCtx->GetBlockVector()); - - if(!pBlockVectorCtx->hasDefragmentationPlan) - { - res = VK_NOT_READY; - continue; - } - - pBlockVectorCtx->GetBlockVector()->CommitDefragmentations( - pBlockVectorCtx, m_pStats); - - if(pBlockVectorCtx->defragmentationMoves.size() != pBlockVectorCtx->defragmentationMovesCommitted) - res = VK_NOT_READY; - } - - return res; -} - //////////////////////////////////////////////////////////////////////////////// // VmaRecorder @@ -15372,7 +13949,8 @@ VmaRecorder::VmaRecorder() : m_UseMutex(true), m_Flags(0), m_File(VMA_NULL), - m_RecordingStartTime(std::chrono::high_resolution_clock::now()) + m_Freq(INT64_MAX), + m_StartCounter(INT64_MAX) { } @@ -15381,23 +13959,15 @@ VkResult VmaRecorder::Init(const VmaRecordSettings& settings, bool useMutex) m_UseMutex = useMutex; m_Flags = settings.flags; -#if defined(_WIN32) + QueryPerformanceFrequency((LARGE_INTEGER*)&m_Freq); + QueryPerformanceCounter((LARGE_INTEGER*)&m_StartCounter); + // Open file for writing. errno_t err = fopen_s(&m_File, settings.pFilePath, "wb"); - if(err != 0) { return VK_ERROR_INITIALIZATION_FAILED; } -#else - // Open file for writing. - m_File = fopen(settings.pFilePath, "wb"); - - if(m_File == 0) - { - return VK_ERROR_INITIALIZATION_FAILED; - } -#endif // Write header. fprintf(m_File, "%s\n", "Vulkan Memory Allocator,Calls recording"); @@ -15857,8 +14427,7 @@ VmaRecorder::UserDataString::UserDataString(VmaAllocationCreateFlags allocFlags, } else { - // If VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT is not specified, convert the string's memory address to a string and store it. - snprintf(m_PtrStr, 17, "%p", pUserData); + sprintf_s(m_PtrStr, "%p", pUserData); m_Str = m_PtrStr; } } @@ -15874,8 +14443,7 @@ void VmaRecorder::WriteConfiguration( uint32_t vulkanApiVersion, bool dedicatedAllocationExtensionEnabled, bool bindMemory2ExtensionEnabled, - bool memoryBudgetExtensionEnabled, - bool deviceCoherentMemoryExtensionEnabled) + bool memoryBudgetExtensionEnabled) { fprintf(m_File, "Config,Begin\n"); @@ -15908,10 +14476,9 @@ void VmaRecorder::WriteConfiguration( fprintf(m_File, "Extension,VK_KHR_dedicated_allocation,%u\n", dedicatedAllocationExtensionEnabled ? 1 : 0); fprintf(m_File, "Extension,VK_KHR_bind_memory2,%u\n", bindMemory2ExtensionEnabled ? 1 : 0); fprintf(m_File, "Extension,VK_EXT_memory_budget,%u\n", memoryBudgetExtensionEnabled ? 1 : 0); - fprintf(m_File, "Extension,VK_AMD_device_coherent_memory,%u\n", deviceCoherentMemoryExtensionEnabled ? 1 : 0); fprintf(m_File, "Macro,VMA_DEBUG_ALWAYS_DEDICATED_MEMORY,%u\n", VMA_DEBUG_ALWAYS_DEDICATED_MEMORY ? 1 : 0); - fprintf(m_File, "Macro,VMA_MIN_ALIGNMENT,%llu\n", (VkDeviceSize)VMA_MIN_ALIGNMENT); + fprintf(m_File, "Macro,VMA_DEBUG_ALIGNMENT,%llu\n", (VkDeviceSize)VMA_DEBUG_ALIGNMENT); fprintf(m_File, "Macro,VMA_DEBUG_MARGIN,%llu\n", (VkDeviceSize)VMA_DEBUG_MARGIN); fprintf(m_File, "Macro,VMA_DEBUG_INITIALIZE_ALLOCATIONS,%u\n", VMA_DEBUG_INITIALIZE_ALLOCATIONS ? 1 : 0); fprintf(m_File, "Macro,VMA_DEBUG_DETECT_CORRUPTION,%u\n", VMA_DEBUG_DETECT_CORRUPTION ? 1 : 0); @@ -15925,22 +14492,11 @@ void VmaRecorder::WriteConfiguration( void VmaRecorder::GetBasicParams(CallParams& outParams) { - #if defined(_WIN32) - outParams.threadId = GetCurrentThreadId(); - #else - // Use C++11 features to get thread id and convert it to uint32_t. - // There is room for optimization since sstream is quite slow. - // Is there a better way to convert std::this_thread::get_id() to uint32_t? - std::thread::id thread_id = std::this_thread::get_id(); - std::stringstream thread_id_to_string_converter; - thread_id_to_string_converter << thread_id; - std::string thread_id_as_string = thread_id_to_string_converter.str(); - outParams.threadId = static_cast(std::stoi(thread_id_as_string.c_str())); - #endif + outParams.threadId = GetCurrentThreadId(); - auto current_time = std::chrono::high_resolution_clock::now(); - - outParams.time = std::chrono::duration(current_time - m_RecordingStartTime).count(); + LARGE_INTEGER counter; + QueryPerformanceCounter(&counter); + outParams.time = (double)(counter.QuadPart - m_StartCounter) / (double)m_Freq; } void VmaRecorder::PrintPointerList(uint64_t count, const VmaAllocation* pItems) @@ -15973,10 +14529,10 @@ VmaAllocationObjectAllocator::VmaAllocationObjectAllocator(const VkAllocationCal { } -template VmaAllocation VmaAllocationObjectAllocator::Allocate(Types... args) +VmaAllocation VmaAllocationObjectAllocator::Allocate() { VmaMutexLock mutexLock(m_Mutex); - return m_Allocator.Alloc(std::forward(args)...); + return m_Allocator.Alloc(); } void VmaAllocationObjectAllocator::Free(VmaAllocation hAlloc) @@ -15994,9 +14550,6 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : m_UseKhrDedicatedAllocation((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT) != 0), m_UseKhrBindMemory2((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT) != 0), m_UseExtMemoryBudget((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT) != 0), - m_UseAmdDeviceCoherentMemory((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT) != 0), - m_UseKhrBufferDeviceAddress((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT) != 0), - m_UseExtMemoryPriority((pCreateInfo->flags & VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT) != 0), m_hDevice(pCreateInfo->device), m_hInstance(pCreateInfo->instance), m_AllocationCallbacksSpecified(pCreateInfo->pAllocationCallbacks != VMA_NULL), @@ -16004,13 +14557,12 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : *pCreateInfo->pAllocationCallbacks : VmaEmptyAllocationCallbacks), m_AllocationObjectAllocator(&m_AllocationCallbacks), m_HeapSizeLimitMask(0), - m_DeviceMemoryCount(0), m_PreferredLargeHeapBlockSize(0), m_PhysicalDevice(pCreateInfo->physicalDevice), m_CurrentFrameIndex(0), m_GpuDefragmentationMemoryTypeBits(UINT32_MAX), - m_NextPoolId(0), - m_GlobalMemoryTypeBits(UINT32_MAX) + m_Pools(VmaStlAllocator(GetAllocationCallbacks())), + m_NextPoolId(0) #if VMA_RECORDING_ENABLED ,m_pRecorder(VMA_NULL) #endif @@ -16027,7 +14579,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : VMA_ASSERT(VMA_DEBUG_MARGIN % sizeof(uint32_t) == 0); } - VMA_ASSERT(pCreateInfo->physicalDevice && pCreateInfo->device && pCreateInfo->instance); + VMA_ASSERT(pCreateInfo->physicalDevice && pCreateInfo->device); if(m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0)) { @@ -16050,41 +14602,23 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT set but required extension is disabled by preprocessor macros."); } #endif -#if !(VMA_BUFFER_DEVICE_ADDRESS) - if(m_UseKhrBufferDeviceAddress) - { - VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT is set but required extension or Vulkan 1.2 is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro."); - } -#endif -#if VMA_VULKAN_VERSION < 1002000 - if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 2, 0)) - { - VMA_ASSERT(0 && "vulkanApiVersion >= VK_API_VERSION_1_2 but required Vulkan version is disabled by preprocessor macros."); - } -#endif #if VMA_VULKAN_VERSION < 1001000 if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) { VMA_ASSERT(0 && "vulkanApiVersion >= VK_API_VERSION_1_1 but required Vulkan version is disabled by preprocessor macros."); } #endif -#if !(VMA_MEMORY_PRIORITY) - if(m_UseExtMemoryPriority) - { - VMA_ASSERT(0 && "VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT is set but required extension is not available in your Vulkan header or its support in VMA has been disabled by a preprocessor macro."); - } -#endif memset(&m_DeviceMemoryCallbacks, 0 ,sizeof(m_DeviceMemoryCallbacks)); memset(&m_PhysicalDeviceProperties, 0, sizeof(m_PhysicalDeviceProperties)); memset(&m_MemProps, 0, sizeof(m_MemProps)); - + memset(&m_pBlockVectors, 0, sizeof(m_pBlockVectors)); + memset(&m_pDedicatedAllocations, 0, sizeof(m_pDedicatedAllocations)); memset(&m_VulkanFunctions, 0, sizeof(m_VulkanFunctions)); if(pCreateInfo->pDeviceMemoryCallbacks != VMA_NULL) { - m_DeviceMemoryCallbacks.pUserData = pCreateInfo->pDeviceMemoryCallbacks->pUserData; m_DeviceMemoryCallbacks.pfnAllocate = pCreateInfo->pDeviceMemoryCallbacks->pfnAllocate; m_DeviceMemoryCallbacks.pfnFree = pCreateInfo->pDeviceMemoryCallbacks->pfnFree; } @@ -16094,7 +14628,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : (*m_VulkanFunctions.vkGetPhysicalDeviceProperties)(m_PhysicalDevice, &m_PhysicalDeviceProperties); (*m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties)(m_PhysicalDevice, &m_MemProps); - VMA_ASSERT(VmaIsPow2(VMA_MIN_ALIGNMENT)); + VMA_ASSERT(VmaIsPow2(VMA_DEBUG_ALIGNMENT)); VMA_ASSERT(VmaIsPow2(VMA_DEBUG_MIN_BUFFER_IMAGE_GRANULARITY)); VMA_ASSERT(VmaIsPow2(m_PhysicalDeviceProperties.limits.bufferImageGranularity)); VMA_ASSERT(VmaIsPow2(m_PhysicalDeviceProperties.limits.nonCoherentAtomSize)); @@ -16102,8 +14636,6 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : m_PreferredLargeHeapBlockSize = (pCreateInfo->preferredLargeHeapBlockSize != 0) ? pCreateInfo->preferredLargeHeapBlockSize : static_cast(VMA_DEFAULT_LARGE_HEAP_BLOCK_SIZE); - m_GlobalMemoryTypeBits = CalculateGlobalMemoryTypeBits(); - if(pCreateInfo->pHeapSizeLimit != VMA_NULL) { for(uint32_t heapIndex = 0; heapIndex < GetMemoryHeapCount(); ++heapIndex) @@ -16134,10 +14666,11 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) : GetBufferImageGranularity(), pCreateInfo->frameInUseCount, false, // explicitBlockSize - false, // linearAlgorithm - 0.5f); // priority (0.5 is the default per Vulkan spec) + false); // linearAlgorithm // No need to call m_pBlockVectors[memTypeIndex][blockVectorTypeIndex]->CreateMinBlocks here, // becase minBlockCount is 0. + m_pDedicatedAllocations[memTypeIndex] = vma_new(this, AllocationVectorType)(VmaStlAllocator(GetAllocationCallbacks())); + } } @@ -16161,8 +14694,7 @@ VkResult VmaAllocator_T::Init(const VmaAllocatorCreateInfo* pCreateInfo) m_VulkanApiVersion, m_UseKhrDedicatedAllocation, m_UseKhrBindMemory2, - m_UseExtMemoryBudget, - m_UseAmdDeviceCoherentMemory); + m_UseExtMemoryBudget); m_pRecorder->RecordCreateAllocator(GetCurrentFrameIndex()); #else VMA_ASSERT(0 && "VmaAllocatorCreateInfo::pRecordSettings used, but not supported due to VMA_RECORDING_ENABLED not defined to 1."); @@ -16189,43 +14721,24 @@ VmaAllocator_T::~VmaAllocator_T() vma_delete(this, m_pRecorder); } #endif + + VMA_ASSERT(m_Pools.empty()); - VMA_ASSERT(m_Pools.IsEmpty()); - - for(size_t memTypeIndex = GetMemoryTypeCount(); memTypeIndex--; ) + for(size_t i = GetMemoryTypeCount(); i--; ) { - if(!m_DedicatedAllocations[memTypeIndex].IsEmpty()) + if(m_pDedicatedAllocations[i] != VMA_NULL && !m_pDedicatedAllocations[i]->empty()) { VMA_ASSERT(0 && "Unfreed dedicated allocations found."); } - vma_delete(this, m_pBlockVectors[memTypeIndex]); + vma_delete(this, m_pDedicatedAllocations[i]); + vma_delete(this, m_pBlockVectors[i]); } } void VmaAllocator_T::ImportVulkanFunctions(const VmaVulkanFunctions* pVulkanFunctions) { #if VMA_STATIC_VULKAN_FUNCTIONS == 1 - ImportVulkanFunctions_Static(); -#endif - - if(pVulkanFunctions != VMA_NULL) - { - ImportVulkanFunctions_Custom(pVulkanFunctions); - } - -#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1 - ImportVulkanFunctions_Dynamic(); -#endif - - ValidateVulkanFunctions(); -} - -#if VMA_STATIC_VULKAN_FUNCTIONS == 1 - -void VmaAllocator_T::ImportVulkanFunctions_Static() -{ - // Vulkan 1.0 m_VulkanFunctions.vkGetPhysicalDeviceProperties = (PFN_vkGetPhysicalDeviceProperties)vkGetPhysicalDeviceProperties; m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties = (PFN_vkGetPhysicalDeviceMemoryProperties)vkGetPhysicalDeviceMemoryProperties; m_VulkanFunctions.vkAllocateMemory = (PFN_vkAllocateMemory)vkAllocateMemory; @@ -16243,137 +14756,89 @@ void VmaAllocator_T::ImportVulkanFunctions_Static() m_VulkanFunctions.vkCreateImage = (PFN_vkCreateImage)vkCreateImage; m_VulkanFunctions.vkDestroyImage = (PFN_vkDestroyImage)vkDestroyImage; m_VulkanFunctions.vkCmdCopyBuffer = (PFN_vkCmdCopyBuffer)vkCmdCopyBuffer; - - // Vulkan 1.1 #if VMA_VULKAN_VERSION >= 1001000 if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) { - m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR = (PFN_vkGetBufferMemoryRequirements2)vkGetBufferMemoryRequirements2; - m_VulkanFunctions.vkGetImageMemoryRequirements2KHR = (PFN_vkGetImageMemoryRequirements2)vkGetImageMemoryRequirements2; - m_VulkanFunctions.vkBindBufferMemory2KHR = (PFN_vkBindBufferMemory2)vkBindBufferMemory2; - m_VulkanFunctions.vkBindImageMemory2KHR = (PFN_vkBindImageMemory2)vkBindImageMemory2; - m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = (PFN_vkGetPhysicalDeviceMemoryProperties2)vkGetPhysicalDeviceMemoryProperties2; + VMA_ASSERT(m_hInstance != VK_NULL_HANDLE); + m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR = + (PFN_vkGetBufferMemoryRequirements2KHR)vkGetDeviceProcAddr(m_hDevice, "vkGetBufferMemoryRequirements2"); + m_VulkanFunctions.vkGetImageMemoryRequirements2KHR = + (PFN_vkGetImageMemoryRequirements2KHR)vkGetDeviceProcAddr(m_hDevice, "vkGetImageMemoryRequirements2"); + m_VulkanFunctions.vkBindBufferMemory2KHR = + (PFN_vkBindBufferMemory2KHR)vkGetDeviceProcAddr(m_hDevice, "vkBindBufferMemory2"); + m_VulkanFunctions.vkBindImageMemory2KHR = + (PFN_vkBindImageMemory2KHR)vkGetDeviceProcAddr(m_hDevice, "vkBindImageMemory2"); + m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = + (PFN_vkGetPhysicalDeviceMemoryProperties2KHR)vkGetInstanceProcAddr(m_hInstance, "vkGetPhysicalDeviceMemoryProperties2"); } #endif -} - +#if VMA_DEDICATED_ALLOCATION + if(m_UseKhrDedicatedAllocation) + { + m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR = + (PFN_vkGetBufferMemoryRequirements2KHR)vkGetDeviceProcAddr(m_hDevice, "vkGetBufferMemoryRequirements2KHR"); + m_VulkanFunctions.vkGetImageMemoryRequirements2KHR = + (PFN_vkGetImageMemoryRequirements2KHR)vkGetDeviceProcAddr(m_hDevice, "vkGetImageMemoryRequirements2KHR"); + } +#endif +#if VMA_BIND_MEMORY2 + if(m_UseKhrBindMemory2) + { + m_VulkanFunctions.vkBindBufferMemory2KHR = + (PFN_vkBindBufferMemory2KHR)vkGetDeviceProcAddr(m_hDevice, "vkBindBufferMemory2KHR"); + m_VulkanFunctions.vkBindImageMemory2KHR = + (PFN_vkBindImageMemory2KHR)vkGetDeviceProcAddr(m_hDevice, "vkBindImageMemory2KHR"); + } +#endif // #if VMA_BIND_MEMORY2 +#if VMA_MEMORY_BUDGET + if(m_UseExtMemoryBudget && m_VulkanApiVersion < VK_MAKE_VERSION(1, 1, 0)) + { + VMA_ASSERT(m_hInstance != VK_NULL_HANDLE); + m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties2KHR = + (PFN_vkGetPhysicalDeviceMemoryProperties2KHR)vkGetInstanceProcAddr(m_hInstance, "vkGetPhysicalDeviceMemoryProperties2KHR"); + } +#endif // #if VMA_MEMORY_BUDGET #endif // #if VMA_STATIC_VULKAN_FUNCTIONS == 1 -void VmaAllocator_T::ImportVulkanFunctions_Custom(const VmaVulkanFunctions* pVulkanFunctions) -{ - VMA_ASSERT(pVulkanFunctions != VMA_NULL); - #define VMA_COPY_IF_NOT_NULL(funcName) \ if(pVulkanFunctions->funcName != VMA_NULL) m_VulkanFunctions.funcName = pVulkanFunctions->funcName; - VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceProperties); - VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties); - VMA_COPY_IF_NOT_NULL(vkAllocateMemory); - VMA_COPY_IF_NOT_NULL(vkFreeMemory); - VMA_COPY_IF_NOT_NULL(vkMapMemory); - VMA_COPY_IF_NOT_NULL(vkUnmapMemory); - VMA_COPY_IF_NOT_NULL(vkFlushMappedMemoryRanges); - VMA_COPY_IF_NOT_NULL(vkInvalidateMappedMemoryRanges); - VMA_COPY_IF_NOT_NULL(vkBindBufferMemory); - VMA_COPY_IF_NOT_NULL(vkBindImageMemory); - VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements); - VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements); - VMA_COPY_IF_NOT_NULL(vkCreateBuffer); - VMA_COPY_IF_NOT_NULL(vkDestroyBuffer); - VMA_COPY_IF_NOT_NULL(vkCreateImage); - VMA_COPY_IF_NOT_NULL(vkDestroyImage); - VMA_COPY_IF_NOT_NULL(vkCmdCopyBuffer); - + if(pVulkanFunctions != VMA_NULL) + { + VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceProperties); + VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties); + VMA_COPY_IF_NOT_NULL(vkAllocateMemory); + VMA_COPY_IF_NOT_NULL(vkFreeMemory); + VMA_COPY_IF_NOT_NULL(vkMapMemory); + VMA_COPY_IF_NOT_NULL(vkUnmapMemory); + VMA_COPY_IF_NOT_NULL(vkFlushMappedMemoryRanges); + VMA_COPY_IF_NOT_NULL(vkInvalidateMappedMemoryRanges); + VMA_COPY_IF_NOT_NULL(vkBindBufferMemory); + VMA_COPY_IF_NOT_NULL(vkBindImageMemory); + VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements); + VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements); + VMA_COPY_IF_NOT_NULL(vkCreateBuffer); + VMA_COPY_IF_NOT_NULL(vkDestroyBuffer); + VMA_COPY_IF_NOT_NULL(vkCreateImage); + VMA_COPY_IF_NOT_NULL(vkDestroyImage); + VMA_COPY_IF_NOT_NULL(vkCmdCopyBuffer); #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000 - VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements2KHR); - VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements2KHR); + VMA_COPY_IF_NOT_NULL(vkGetBufferMemoryRequirements2KHR); + VMA_COPY_IF_NOT_NULL(vkGetImageMemoryRequirements2KHR); #endif - #if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000 - VMA_COPY_IF_NOT_NULL(vkBindBufferMemory2KHR); - VMA_COPY_IF_NOT_NULL(vkBindImageMemory2KHR); + VMA_COPY_IF_NOT_NULL(vkBindBufferMemory2KHR); + VMA_COPY_IF_NOT_NULL(vkBindImageMemory2KHR); #endif - #if VMA_MEMORY_BUDGET - VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties2KHR); + VMA_COPY_IF_NOT_NULL(vkGetPhysicalDeviceMemoryProperties2KHR); #endif + } #undef VMA_COPY_IF_NOT_NULL -} -#if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1 - -void VmaAllocator_T::ImportVulkanFunctions_Dynamic() -{ -#define VMA_FETCH_INSTANCE_FUNC(memberName, functionPointerType, functionNameString) \ - if(m_VulkanFunctions.memberName == VMA_NULL) \ - m_VulkanFunctions.memberName = \ - (functionPointerType)vkGetInstanceProcAddr(m_hInstance, functionNameString); -#define VMA_FETCH_DEVICE_FUNC(memberName, functionPointerType, functionNameString) \ - if(m_VulkanFunctions.memberName == VMA_NULL) \ - m_VulkanFunctions.memberName = \ - (functionPointerType)vkGetDeviceProcAddr(m_hDevice, functionNameString); - - VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceProperties, PFN_vkGetPhysicalDeviceProperties, "vkGetPhysicalDeviceProperties"); - VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceMemoryProperties, PFN_vkGetPhysicalDeviceMemoryProperties, "vkGetPhysicalDeviceMemoryProperties"); - VMA_FETCH_DEVICE_FUNC(vkAllocateMemory, PFN_vkAllocateMemory, "vkAllocateMemory"); - VMA_FETCH_DEVICE_FUNC(vkFreeMemory, PFN_vkFreeMemory, "vkFreeMemory"); - VMA_FETCH_DEVICE_FUNC(vkMapMemory, PFN_vkMapMemory, "vkMapMemory"); - VMA_FETCH_DEVICE_FUNC(vkUnmapMemory, PFN_vkUnmapMemory, "vkUnmapMemory"); - VMA_FETCH_DEVICE_FUNC(vkFlushMappedMemoryRanges, PFN_vkFlushMappedMemoryRanges, "vkFlushMappedMemoryRanges"); - VMA_FETCH_DEVICE_FUNC(vkInvalidateMappedMemoryRanges, PFN_vkInvalidateMappedMemoryRanges, "vkInvalidateMappedMemoryRanges"); - VMA_FETCH_DEVICE_FUNC(vkBindBufferMemory, PFN_vkBindBufferMemory, "vkBindBufferMemory"); - VMA_FETCH_DEVICE_FUNC(vkBindImageMemory, PFN_vkBindImageMemory, "vkBindImageMemory"); - VMA_FETCH_DEVICE_FUNC(vkGetBufferMemoryRequirements, PFN_vkGetBufferMemoryRequirements, "vkGetBufferMemoryRequirements"); - VMA_FETCH_DEVICE_FUNC(vkGetImageMemoryRequirements, PFN_vkGetImageMemoryRequirements, "vkGetImageMemoryRequirements"); - VMA_FETCH_DEVICE_FUNC(vkCreateBuffer, PFN_vkCreateBuffer, "vkCreateBuffer"); - VMA_FETCH_DEVICE_FUNC(vkDestroyBuffer, PFN_vkDestroyBuffer, "vkDestroyBuffer"); - VMA_FETCH_DEVICE_FUNC(vkCreateImage, PFN_vkCreateImage, "vkCreateImage"); - VMA_FETCH_DEVICE_FUNC(vkDestroyImage, PFN_vkDestroyImage, "vkDestroyImage"); - VMA_FETCH_DEVICE_FUNC(vkCmdCopyBuffer, PFN_vkCmdCopyBuffer, "vkCmdCopyBuffer"); - -#if VMA_VULKAN_VERSION >= 1001000 - if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) - { - VMA_FETCH_DEVICE_FUNC(vkGetBufferMemoryRequirements2KHR, PFN_vkGetBufferMemoryRequirements2, "vkGetBufferMemoryRequirements2"); - VMA_FETCH_DEVICE_FUNC(vkGetImageMemoryRequirements2KHR, PFN_vkGetImageMemoryRequirements2, "vkGetImageMemoryRequirements2"); - VMA_FETCH_DEVICE_FUNC(vkBindBufferMemory2KHR, PFN_vkBindBufferMemory2, "vkBindBufferMemory2"); - VMA_FETCH_DEVICE_FUNC(vkBindImageMemory2KHR, PFN_vkBindImageMemory2, "vkBindImageMemory2"); - VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceMemoryProperties2KHR, PFN_vkGetPhysicalDeviceMemoryProperties2, "vkGetPhysicalDeviceMemoryProperties2"); - } -#endif - -#if VMA_DEDICATED_ALLOCATION - if(m_UseKhrDedicatedAllocation) - { - VMA_FETCH_DEVICE_FUNC(vkGetBufferMemoryRequirements2KHR, PFN_vkGetBufferMemoryRequirements2KHR, "vkGetBufferMemoryRequirements2KHR"); - VMA_FETCH_DEVICE_FUNC(vkGetImageMemoryRequirements2KHR, PFN_vkGetImageMemoryRequirements2KHR, "vkGetImageMemoryRequirements2KHR"); - } -#endif - -#if VMA_BIND_MEMORY2 - if(m_UseKhrBindMemory2) - { - VMA_FETCH_DEVICE_FUNC(vkBindBufferMemory2KHR, PFN_vkBindBufferMemory2KHR, "vkBindBufferMemory2KHR"); - VMA_FETCH_DEVICE_FUNC(vkBindImageMemory2KHR, PFN_vkBindImageMemory2KHR, "vkBindImageMemory2KHR"); - } -#endif // #if VMA_BIND_MEMORY2 - -#if VMA_MEMORY_BUDGET - if(m_UseExtMemoryBudget) - { - VMA_FETCH_INSTANCE_FUNC(vkGetPhysicalDeviceMemoryProperties2KHR, PFN_vkGetPhysicalDeviceMemoryProperties2KHR, "vkGetPhysicalDeviceMemoryProperties2KHR"); - } -#endif // #if VMA_MEMORY_BUDGET - -#undef VMA_FETCH_DEVICE_FUNC -#undef VMA_FETCH_INSTANCE_FUNC -} - -#endif // #if VMA_DYNAMIC_VULKAN_FUNCTIONS == 1 - -void VmaAllocator_T::ValidateVulkanFunctions() -{ + // If these asserts are hit, you must either #define VMA_STATIC_VULKAN_FUNCTIONS 1 + // or pass valid pointers as VmaAllocatorCreateInfo::pVulkanFunctions. VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceProperties != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkGetPhysicalDeviceMemoryProperties != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkAllocateMemory != VMA_NULL); @@ -16391,7 +14856,6 @@ void VmaAllocator_T::ValidateVulkanFunctions() VMA_ASSERT(m_VulkanFunctions.vkCreateImage != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkDestroyImage != VMA_NULL); VMA_ASSERT(m_VulkanFunctions.vkCmdCopyBuffer != VMA_NULL); - #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000 if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0) || m_UseKhrDedicatedAllocation) { @@ -16399,7 +14863,6 @@ void VmaAllocator_T::ValidateVulkanFunctions() VMA_ASSERT(m_VulkanFunctions.vkGetImageMemoryRequirements2KHR != VMA_NULL); } #endif - #if VMA_BIND_MEMORY2 || VMA_VULKAN_VERSION >= 1001000 if(m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0) || m_UseKhrBindMemory2) { @@ -16407,7 +14870,6 @@ void VmaAllocator_T::ValidateVulkanFunctions() VMA_ASSERT(m_VulkanFunctions.vkBindImageMemory2KHR != VMA_NULL); } #endif - #if VMA_MEMORY_BUDGET || VMA_VULKAN_VERSION >= 1001000 if(m_UseExtMemoryBudget || m_VulkanApiVersion >= VK_MAKE_VERSION(1, 1, 0)) { @@ -16429,7 +14891,6 @@ VkResult VmaAllocator_T::AllocateMemoryOfType( VkDeviceSize alignment, bool dedicatedAllocation, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, VkImage dedicatedImage, const VmaAllocationCreateInfo& createInfo, uint32_t memTypeIndex, @@ -16487,9 +14948,7 @@ VkResult VmaAllocator_T::AllocateMemoryOfType( (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0, (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0, finalCreateInfo.pUserData, - finalCreateInfo.priority, dedicatedBuffer, - dedicatedBufferUsage, dedicatedImage, allocationCount, pAllocations); @@ -16515,40 +14974,32 @@ VkResult VmaAllocator_T::AllocateMemoryOfType( { return VK_ERROR_OUT_OF_DEVICE_MEMORY; } - - // Protection against creating each allocation as dedicated when we reach or exceed heap size/budget, - // which can quickly deplete maxMemoryAllocationCount: Don't try dedicated allocations when above - // 3/4 of the maximum allocation count. - if(m_DeviceMemoryCount.load() > m_PhysicalDeviceProperties.limits.maxMemoryAllocationCount * 3 / 4) - { - return VK_ERROR_OUT_OF_DEVICE_MEMORY; - } - - res = AllocateDedicatedMemory( - size, - suballocType, - memTypeIndex, - (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT) != 0, - (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0, - (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0, - finalCreateInfo.pUserData, - finalCreateInfo.priority, - dedicatedBuffer, - dedicatedBufferUsage, - dedicatedImage, - allocationCount, - pAllocations); - if(res == VK_SUCCESS) - { - // Succeeded: AllocateDedicatedMemory function already filld pMemory, nothing more to do here. - VMA_DEBUG_LOG(" Allocated as DedicatedMemory"); - return VK_SUCCESS; - } else { - // Everything failed: Return error code. - VMA_DEBUG_LOG(" vkAllocateMemory FAILED"); - return res; + res = AllocateDedicatedMemory( + size, + suballocType, + memTypeIndex, + (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_WITHIN_BUDGET_BIT) != 0, + (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_MAPPED_BIT) != 0, + (finalCreateInfo.flags & VMA_ALLOCATION_CREATE_USER_DATA_COPY_STRING_BIT) != 0, + finalCreateInfo.pUserData, + dedicatedBuffer, + dedicatedImage, + allocationCount, + pAllocations); + if(res == VK_SUCCESS) + { + // Succeeded: AllocateDedicatedMemory function already filld pMemory, nothing more to do here. + VMA_DEBUG_LOG(" Allocated as DedicatedMemory"); + return VK_SUCCESS; + } + else + { + // Everything failed: Return error code. + VMA_DEBUG_LOG(" vkAllocateMemory FAILED"); + return res; + } } } } @@ -16561,9 +15012,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( bool map, bool isUserDataString, void* pUserData, - float priority, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, VkImage dedicatedImage, size_t allocationCount, VmaAllocation* pAllocations) @@ -16593,47 +15042,16 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( { VMA_ASSERT(dedicatedImage == VK_NULL_HANDLE); dedicatedAllocInfo.buffer = dedicatedBuffer; - VmaPnextChainPushFront(&allocInfo, &dedicatedAllocInfo); + allocInfo.pNext = &dedicatedAllocInfo; } else if(dedicatedImage != VK_NULL_HANDLE) { dedicatedAllocInfo.image = dedicatedImage; - VmaPnextChainPushFront(&allocInfo, &dedicatedAllocInfo); + allocInfo.pNext = &dedicatedAllocInfo; } } #endif // #if VMA_DEDICATED_ALLOCATION || VMA_VULKAN_VERSION >= 1001000 -#if VMA_BUFFER_DEVICE_ADDRESS - VkMemoryAllocateFlagsInfoKHR allocFlagsInfo = { VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_FLAGS_INFO_KHR }; - if(m_UseKhrBufferDeviceAddress) - { - bool canContainBufferWithDeviceAddress = true; - if(dedicatedBuffer != VK_NULL_HANDLE) - { - canContainBufferWithDeviceAddress = dedicatedBufferUsage == UINT32_MAX || // Usage flags unknown - (dedicatedBufferUsage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_EXT) != 0; - } - else if(dedicatedImage != VK_NULL_HANDLE) - { - canContainBufferWithDeviceAddress = false; - } - if(canContainBufferWithDeviceAddress) - { - allocFlagsInfo.flags = VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR; - VmaPnextChainPushFront(&allocInfo, &allocFlagsInfo); - } - } -#endif // #if VMA_BUFFER_DEVICE_ADDRESS - -#if VMA_MEMORY_PRIORITY - VkMemoryPriorityAllocateInfoEXT priorityInfo = { VK_STRUCTURE_TYPE_MEMORY_PRIORITY_ALLOCATE_INFO_EXT }; - if(m_UseExtMemoryPriority) - { - priorityInfo.priority = priority; - VmaPnextChainPushFront(&allocInfo, &priorityInfo); - } -#endif // #if VMA_MEMORY_PRIORITY - size_t allocIndex; VkResult res = VK_SUCCESS; for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex) @@ -16655,13 +15073,14 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( if(res == VK_SUCCESS) { - // Register them in m_DedicatedAllocations. + // Register them in m_pDedicatedAllocations. { VmaMutexLockWrite lock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex); - DedicatedAllocationLinkedList& dedicatedAllocations = m_DedicatedAllocations[memTypeIndex]; + AllocationVectorType* pDedicatedAllocations = m_pDedicatedAllocations[memTypeIndex]; + VMA_ASSERT(pDedicatedAllocations); for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex) { - dedicatedAllocations.PushBack(pAllocations[allocIndex]); + VmaVectorInsertSorted(*pDedicatedAllocations, pAllocations[allocIndex]); } } @@ -16674,7 +15093,7 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( { VmaAllocation currAlloc = pAllocations[allocIndex]; VkDeviceMemory hMemory = currAlloc->GetMemory(); - + /* There is no need to call this, because Vulkan spec allows to skip vkUnmapMemory before vkFreeMemory. @@ -16684,10 +15103,11 @@ VkResult VmaAllocator_T::AllocateDedicatedMemory( (*m_VulkanFunctions.vkUnmapMemory)(m_hDevice, hMemory); } */ - + FreeVulkanMemory(memTypeIndex, currAlloc->GetSize(), hMemory); m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), currAlloc->GetSize()); currAlloc->SetUserData(this, VMA_NULL); + currAlloc->Dtor(); m_AllocationObjectAllocator.Free(currAlloc); } @@ -16733,7 +15153,8 @@ VkResult VmaAllocator_T::AllocateDedicatedMemoryPage( } } - *pAllocation = m_AllocationObjectAllocator.Allocate(m_CurrentFrameIndex.load(), isUserDataString); + *pAllocation = m_AllocationObjectAllocator.Allocate(); + (*pAllocation)->Ctor(m_CurrentFrameIndex.load(), isUserDataString); (*pAllocation)->InitDedicatedAllocation(memTypeIndex, hMemory, suballocType, pMappedData, size); (*pAllocation)->SetUserData(this, pUserData); m_Budget.AddAllocation(MemoryTypeIndexToHeapIndex(memTypeIndex), size); @@ -16760,7 +15181,7 @@ void VmaAllocator_T::GetBufferMemoryRequirements( VkMemoryDedicatedRequirementsKHR memDedicatedReq = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR }; VkMemoryRequirements2KHR memReq2 = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR }; - VmaPnextChainPushFront(&memReq2, &memDedicatedReq); + memReq2.pNext = &memDedicatedReq; (*m_VulkanFunctions.vkGetBufferMemoryRequirements2KHR)(m_hDevice, &memReqInfo, &memReq2); @@ -16792,7 +15213,7 @@ void VmaAllocator_T::GetImageMemoryRequirements( VkMemoryDedicatedRequirementsKHR memDedicatedReq = { VK_STRUCTURE_TYPE_MEMORY_DEDICATED_REQUIREMENTS_KHR }; VkMemoryRequirements2KHR memReq2 = { VK_STRUCTURE_TYPE_MEMORY_REQUIREMENTS_2_KHR }; - VmaPnextChainPushFront(&memReq2, &memDedicatedReq); + memReq2.pNext = &memDedicatedReq; (*m_VulkanFunctions.vkGetImageMemoryRequirements2KHR)(m_hDevice, &memReqInfo, &memReq2); @@ -16814,7 +15235,6 @@ VkResult VmaAllocator_T::AllocateMemory( bool requiresDedicatedAllocation, bool prefersDedicatedAllocation, VkBuffer dedicatedBuffer, - VkBufferUsageFlags dedicatedBufferUsage, VkImage dedicatedImage, const VmaAllocationCreateInfo& createInfo, VmaSuballocationType suballocType, @@ -16901,7 +15321,6 @@ VkResult VmaAllocator_T::AllocateMemory( alignmentForMemType, requiresDedicatedAllocation || prefersDedicatedAllocation, dedicatedBuffer, - dedicatedBufferUsage, dedicatedImage, createInfo, memTypeIndex, @@ -16927,13 +15346,12 @@ VkResult VmaAllocator_T::AllocateMemory( alignmentForMemType = VMA_MAX( vkMemReq.alignment, GetMemoryTypeMinAlignment(memTypeIndex)); - + res = AllocateMemoryOfType( vkMemReq.size, alignmentForMemType, requiresDedicatedAllocation || prefersDedicatedAllocation, dedicatedBuffer, - dedicatedBufferUsage, dedicatedImage, createInfo, memTypeIndex, @@ -17010,11 +15428,28 @@ void VmaAllocator_T::FreeMemory( // Do this regardless of whether the allocation is lost. Lost allocations still account to Budget.AllocationBytes. m_Budget.RemoveAllocation(MemoryTypeIndexToHeapIndex(allocation->GetMemoryTypeIndex()), allocation->GetSize()); allocation->SetUserData(this, VMA_NULL); + allocation->Dtor(); m_AllocationObjectAllocator.Free(allocation); } } } +VkResult VmaAllocator_T::ResizeAllocation( + const VmaAllocation alloc, + VkDeviceSize newSize) +{ + // This function is deprecated and so it does nothing. It's left for backward compatibility. + if(newSize == 0 || alloc->GetLastUseFrameIndex() == VMA_FRAME_INDEX_LOST) + { + return VK_ERROR_VALIDATION_FAILED_EXT; + } + if(newSize == alloc->GetSize()) + { + return VK_SUCCESS; + } + return VK_ERROR_OUT_OF_POOL_MEMORY; +} + void VmaAllocator_T::CalculateStats(VmaStats* pStats) { // Initialize. @@ -17023,7 +15458,7 @@ void VmaAllocator_T::CalculateStats(VmaStats* pStats) InitStatInfo(pStats->memoryType[i]); for(size_t i = 0; i < VK_MAX_MEMORY_HEAPS; ++i) InitStatInfo(pStats->memoryHeap[i]); - + // Process default pools. for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex) { @@ -17035,9 +15470,9 @@ void VmaAllocator_T::CalculateStats(VmaStats* pStats) // Process custom pools. { VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex); - for(VmaPool pool = m_Pools.Front(); pool != VMA_NULL; pool = m_Pools.GetNext(pool)) + for(size_t poolIndex = 0, poolCount = m_Pools.size(); poolIndex < poolCount; ++poolIndex) { - pool->m_BlockVector.AddStats(pStats); + m_Pools[poolIndex]->m_BlockVector.AddStats(pStats); } } @@ -17046,12 +15481,12 @@ void VmaAllocator_T::CalculateStats(VmaStats* pStats) { const uint32_t memHeapIndex = MemoryTypeIndexToHeapIndex(memTypeIndex); VmaMutexLockRead dedicatedAllocationsLock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex); - DedicatedAllocationLinkedList& dedicatedAllocList = m_DedicatedAllocations[memTypeIndex]; - for(VmaAllocation alloc = dedicatedAllocList.Front(); - alloc != VMA_NULL; alloc = dedicatedAllocList.GetNext(alloc)) + AllocationVectorType* const pDedicatedAllocVector = m_pDedicatedAllocations[memTypeIndex]; + VMA_ASSERT(pDedicatedAllocVector); + for(size_t allocIndex = 0, allocCount = pDedicatedAllocVector->size(); allocIndex < allocCount; ++allocIndex) { VmaStatInfo allocationStatInfo; - alloc->DedicatedAllocCalcStatsInfo(allocationStatInfo); + (*pDedicatedAllocVector)[allocIndex]->DedicatedAllocCalcStatsInfo(allocationStatInfo); VmaAddStatInfo(pStats->total, allocationStatInfo); VmaAddStatInfo(pStats->memoryType[memTypeIndex], allocationStatInfo); VmaAddStatInfo(pStats->memoryHeap[memHeapIndex], allocationStatInfo); @@ -17140,7 +15575,7 @@ VkResult VmaAllocator_T::DefragmentationBegin( VkResult res = (*pContext)->Defragment( info.maxCpuBytesToMove, info.maxCpuAllocationsToMove, info.maxGpuBytesToMove, info.maxGpuAllocationsToMove, - info.commandBuffer, pStats, info.flags); + info.commandBuffer, pStats); if(res != VK_NOT_READY) { @@ -17158,19 +15593,6 @@ VkResult VmaAllocator_T::DefragmentationEnd( return VK_SUCCESS; } -VkResult VmaAllocator_T::DefragmentationPassBegin( - VmaDefragmentationPassInfo* pInfo, - VmaDefragmentationContext context) -{ - return context->DefragmentPassBegin(pInfo); -} -VkResult VmaAllocator_T::DefragmentationPassEnd( - VmaDefragmentationContext context) -{ - return context->DefragmentPassEnd(); - -} - void VmaAllocator_T::GetAllocationInfo(VmaAllocation hAllocation, VmaAllocationInfo* pAllocationInfo) { if(hAllocation->CanBecomeLost()) @@ -17309,12 +15731,6 @@ VkResult VmaAllocator_T::CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPoo { return VK_ERROR_INITIALIZATION_FAILED; } - // Memory type index out of range or forbidden. - if(pCreateInfo->memoryTypeIndex >= GetMemoryTypeCount() || - ((1u << pCreateInfo->memoryTypeIndex) & m_GlobalMemoryTypeBits) == 0) - { - return VK_ERROR_FEATURE_NOT_PRESENT; - } const VkDeviceSize preferredBlockSize = CalcPreferredBlockSize(newCreateInfo.memoryTypeIndex); @@ -17332,7 +15748,7 @@ VkResult VmaAllocator_T::CreatePool(const VmaPoolCreateInfo* pCreateInfo, VmaPoo { VmaMutexLockWrite lock(m_PoolsMutex, m_UseMutex); (*pPool)->SetId(m_NextPoolId++); - m_Pools.PushBack(*pPool); + VmaVectorInsertSorted(m_Pools, *pPool); } return VK_SUCCESS; @@ -17343,7 +15759,8 @@ void VmaAllocator_T::DestroyPool(VmaPool pool) // Remove from m_Pools. { VmaMutexLockWrite lock(m_PoolsMutex, m_UseMutex); - m_Pools.Remove(pool); + bool success = VmaVectorRemoveSorted(m_Pools, pool); + VMA_ASSERT(success && "Pool not found in Allocator."); } vma_delete(this, pool); @@ -17408,11 +15825,11 @@ VkResult VmaAllocator_T::CheckCorruption(uint32_t memoryTypeBits) // Process custom pools. { VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex); - for(VmaPool pool = m_Pools.Front(); pool != VMA_NULL; pool = m_Pools.GetNext(pool)) + for(size_t poolIndex = 0, poolCount = m_Pools.size(); poolIndex < poolCount; ++poolIndex) { - if(((1u << pool->m_BlockVector.GetMemoryTypeIndex()) & memoryTypeBits) != 0) + if(((1u << m_Pools[poolIndex]->m_BlockVector.GetMemoryTypeIndex()) & memoryTypeBits) != 0) { - VkResult localRes = pool->m_BlockVector.CheckCorruption(); + VkResult localRes = m_Pools[poolIndex]->m_BlockVector.CheckCorruption(); switch(localRes) { case VK_ERROR_FEATURE_NOT_PRESENT: @@ -17432,46 +15849,13 @@ VkResult VmaAllocator_T::CheckCorruption(uint32_t memoryTypeBits) void VmaAllocator_T::CreateLostAllocation(VmaAllocation* pAllocation) { - *pAllocation = m_AllocationObjectAllocator.Allocate(VMA_FRAME_INDEX_LOST, false); + *pAllocation = m_AllocationObjectAllocator.Allocate(); + (*pAllocation)->Ctor(VMA_FRAME_INDEX_LOST, false); (*pAllocation)->InitLost(); } -// An object that increments given atomic but decrements it back in the destructor unless Commit() is called. -template -struct AtomicTransactionalIncrement -{ -public: - typedef std::atomic AtomicT; - ~AtomicTransactionalIncrement() - { - if(m_Atomic) - --(*m_Atomic); - } - T Increment(AtomicT* atomic) - { - m_Atomic = atomic; - return m_Atomic->fetch_add(1); - } - void Commit() - { - m_Atomic = nullptr; - } - -private: - AtomicT* m_Atomic = nullptr; -}; - VkResult VmaAllocator_T::AllocateVulkanMemory(const VkMemoryAllocateInfo* pAllocateInfo, VkDeviceMemory* pMemory) { - AtomicTransactionalIncrement deviceMemoryCountIncrement; - const uint64_t prevDeviceMemoryCount = deviceMemoryCountIncrement.Increment(&m_DeviceMemoryCount); -#if VMA_DEBUG_DONT_EXCEED_MAX_MEMORY_ALLOCATION_COUNT - if(prevDeviceMemoryCount >= m_PhysicalDeviceProperties.limits.maxMemoryAllocationCount) - { - return VK_ERROR_TOO_MANY_OBJECTS; - } -#endif - const uint32_t heapIndex = MemoryTypeIndexToHeapIndex(pAllocateInfo->memoryTypeIndex); // HeapSizeLimit is in effect for this heap. @@ -17509,10 +15893,8 @@ VkResult VmaAllocator_T::AllocateVulkanMemory(const VkMemoryAllocateInfo* pAlloc // Informative callback. if(m_DeviceMemoryCallbacks.pfnAllocate != VMA_NULL) { - (*m_DeviceMemoryCallbacks.pfnAllocate)(this, pAllocateInfo->memoryTypeIndex, *pMemory, pAllocateInfo->allocationSize, m_DeviceMemoryCallbacks.pUserData); + (*m_DeviceMemoryCallbacks.pfnAllocate)(this, pAllocateInfo->memoryTypeIndex, *pMemory, pAllocateInfo->allocationSize); } - - deviceMemoryCountIncrement.Commit(); } else { @@ -17527,15 +15909,13 @@ void VmaAllocator_T::FreeVulkanMemory(uint32_t memoryType, VkDeviceSize size, Vk // Informative callback. if(m_DeviceMemoryCallbacks.pfnFree != VMA_NULL) { - (*m_DeviceMemoryCallbacks.pfnFree)(this, memoryType, hMemory, size, m_DeviceMemoryCallbacks.pUserData); + (*m_DeviceMemoryCallbacks.pfnFree)(this, memoryType, hMemory, size); } // VULKAN CALL vkFreeMemory. (*m_VulkanFunctions.vkFreeMemory)(m_hDevice, hMemory, GetAllocationCallbacks()); m_Budget.m_BlockBytes[MemoryTypeIndexToHeapIndex(memoryType)] -= size; - - --m_DeviceMemoryCount; } VkResult VmaAllocator_T::BindVulkanBuffer( @@ -17698,71 +16078,80 @@ VkResult VmaAllocator_T::BindImageMemory( return res; } -VkResult VmaAllocator_T::FlushOrInvalidateAllocation( +void VmaAllocator_T::FlushOrInvalidateAllocation( VmaAllocation hAllocation, VkDeviceSize offset, VkDeviceSize size, VMA_CACHE_OPERATION op) { - VkResult res = VK_SUCCESS; - - VkMappedMemoryRange memRange = {}; - if(GetFlushOrInvalidateRange(hAllocation, offset, size, memRange)) + const uint32_t memTypeIndex = hAllocation->GetMemoryTypeIndex(); + if(size > 0 && IsMemoryTypeNonCoherent(memTypeIndex)) { + const VkDeviceSize allocationSize = hAllocation->GetSize(); + VMA_ASSERT(offset <= allocationSize); + + const VkDeviceSize nonCoherentAtomSize = m_PhysicalDeviceProperties.limits.nonCoherentAtomSize; + + VkMappedMemoryRange memRange = { VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE }; + memRange.memory = hAllocation->GetMemory(); + + switch(hAllocation->GetType()) + { + case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED: + memRange.offset = VmaAlignDown(offset, nonCoherentAtomSize); + if(size == VK_WHOLE_SIZE) + { + memRange.size = allocationSize - memRange.offset; + } + else + { + VMA_ASSERT(offset + size <= allocationSize); + memRange.size = VMA_MIN( + VmaAlignUp(size + (offset - memRange.offset), nonCoherentAtomSize), + allocationSize - memRange.offset); + } + break; + + case VmaAllocation_T::ALLOCATION_TYPE_BLOCK: + { + // 1. Still within this allocation. + memRange.offset = VmaAlignDown(offset, nonCoherentAtomSize); + if(size == VK_WHOLE_SIZE) + { + size = allocationSize - offset; + } + else + { + VMA_ASSERT(offset + size <= allocationSize); + } + memRange.size = VmaAlignUp(size + (offset - memRange.offset), nonCoherentAtomSize); + + // 2. Adjust to whole block. + const VkDeviceSize allocationOffset = hAllocation->GetOffset(); + VMA_ASSERT(allocationOffset % nonCoherentAtomSize == 0); + const VkDeviceSize blockSize = hAllocation->GetBlock()->m_pMetadata->GetSize(); + memRange.offset += allocationOffset; + memRange.size = VMA_MIN(memRange.size, blockSize - memRange.offset); + + break; + } + + default: + VMA_ASSERT(0); + } + switch(op) { case VMA_CACHE_FLUSH: - res = (*GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hDevice, 1, &memRange); + (*GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hDevice, 1, &memRange); break; case VMA_CACHE_INVALIDATE: - res = (*GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hDevice, 1, &memRange); + (*GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hDevice, 1, &memRange); break; default: VMA_ASSERT(0); } } // else: Just ignore this call. - return res; -} - -VkResult VmaAllocator_T::FlushOrInvalidateAllocations( - uint32_t allocationCount, - const VmaAllocation* allocations, - const VkDeviceSize* offsets, const VkDeviceSize* sizes, - VMA_CACHE_OPERATION op) -{ - typedef VmaStlAllocator RangeAllocator; - typedef VmaSmallVector RangeVector; - RangeVector ranges = RangeVector(RangeAllocator(GetAllocationCallbacks())); - - for(uint32_t allocIndex = 0; allocIndex < allocationCount; ++allocIndex) - { - const VmaAllocation alloc = allocations[allocIndex]; - const VkDeviceSize offset = offsets != VMA_NULL ? offsets[allocIndex] : 0; - const VkDeviceSize size = sizes != VMA_NULL ? sizes[allocIndex] : VK_WHOLE_SIZE; - VkMappedMemoryRange newRange; - if(GetFlushOrInvalidateRange(alloc, offset, size, newRange)) - { - ranges.push_back(newRange); - } - } - - VkResult res = VK_SUCCESS; - if(!ranges.empty()) - { - switch(op) - { - case VMA_CACHE_FLUSH: - res = (*GetVulkanFunctions().vkFlushMappedMemoryRanges)(m_hDevice, (uint32_t)ranges.size(), ranges.data()); - break; - case VMA_CACHE_INVALIDATE: - res = (*GetVulkanFunctions().vkInvalidateMappedMemoryRanges)(m_hDevice, (uint32_t)ranges.size(), ranges.data()); - break; - default: - VMA_ASSERT(0); - } - } - // else: Just ignore this call. - return res; } void VmaAllocator_T::FreeDedicatedMemory(const VmaAllocation allocation) @@ -17772,12 +16161,14 @@ void VmaAllocator_T::FreeDedicatedMemory(const VmaAllocation allocation) const uint32_t memTypeIndex = allocation->GetMemoryTypeIndex(); { VmaMutexLockWrite lock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex); - DedicatedAllocationLinkedList& dedicatedAllocations = m_DedicatedAllocations[memTypeIndex]; - dedicatedAllocations.Remove(allocation); + AllocationVectorType* const pDedicatedAllocations = m_pDedicatedAllocations[memTypeIndex]; + VMA_ASSERT(pDedicatedAllocations); + bool success = VmaVectorRemoveSorted(*pDedicatedAllocations, allocation); + VMA_ASSERT(success); } VkDeviceMemory hMemory = allocation->GetMemory(); - + /* There is no need to call this, because Vulkan spec allows to skip vkUnmapMemory before vkFreeMemory. @@ -17787,7 +16178,7 @@ void VmaAllocator_T::FreeDedicatedMemory(const VmaAllocation allocation) (*m_VulkanFunctions.vkUnmapMemory)(m_hDevice, hMemory); } */ - + FreeVulkanMemory(memTypeIndex, allocation->GetSize(), hMemory); VMA_DEBUG_LOG(" Freed DedicatedMemory MemoryTypeIndex=%u", memTypeIndex); @@ -17818,91 +16209,6 @@ uint32_t VmaAllocator_T::CalculateGpuDefragmentationMemoryTypeBits() const return memoryTypeBits; } -uint32_t VmaAllocator_T::CalculateGlobalMemoryTypeBits() const -{ - // Make sure memory information is already fetched. - VMA_ASSERT(GetMemoryTypeCount() > 0); - - uint32_t memoryTypeBits = UINT32_MAX; - - if(!m_UseAmdDeviceCoherentMemory) - { - // Exclude memory types that have VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD. - for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex) - { - if((m_MemProps.memoryTypes[memTypeIndex].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY) != 0) - { - memoryTypeBits &= ~(1u << memTypeIndex); - } - } - } - - return memoryTypeBits; -} - -bool VmaAllocator_T::GetFlushOrInvalidateRange( - VmaAllocation allocation, - VkDeviceSize offset, VkDeviceSize size, - VkMappedMemoryRange& outRange) const -{ - const uint32_t memTypeIndex = allocation->GetMemoryTypeIndex(); - if(size > 0 && IsMemoryTypeNonCoherent(memTypeIndex)) - { - const VkDeviceSize nonCoherentAtomSize = m_PhysicalDeviceProperties.limits.nonCoherentAtomSize; - const VkDeviceSize allocationSize = allocation->GetSize(); - VMA_ASSERT(offset <= allocationSize); - - outRange.sType = VK_STRUCTURE_TYPE_MAPPED_MEMORY_RANGE; - outRange.pNext = VMA_NULL; - outRange.memory = allocation->GetMemory(); - - switch(allocation->GetType()) - { - case VmaAllocation_T::ALLOCATION_TYPE_DEDICATED: - outRange.offset = VmaAlignDown(offset, nonCoherentAtomSize); - if(size == VK_WHOLE_SIZE) - { - outRange.size = allocationSize - outRange.offset; - } - else - { - VMA_ASSERT(offset + size <= allocationSize); - outRange.size = VMA_MIN( - VmaAlignUp(size + (offset - outRange.offset), nonCoherentAtomSize), - allocationSize - outRange.offset); - } - break; - case VmaAllocation_T::ALLOCATION_TYPE_BLOCK: - { - // 1. Still within this allocation. - outRange.offset = VmaAlignDown(offset, nonCoherentAtomSize); - if(size == VK_WHOLE_SIZE) - { - size = allocationSize - offset; - } - else - { - VMA_ASSERT(offset + size <= allocationSize); - } - outRange.size = VmaAlignUp(size + (offset - outRange.offset), nonCoherentAtomSize); - - // 2. Adjust to whole block. - const VkDeviceSize allocationOffset = allocation->GetOffset(); - VMA_ASSERT(allocationOffset % nonCoherentAtomSize == 0); - const VkDeviceSize blockSize = allocation->GetBlock()->m_pMetadata->GetSize(); - outRange.offset += allocationOffset; - outRange.size = VMA_MIN(outRange.size, blockSize - outRange.offset); - - break; - } - default: - VMA_ASSERT(0); - } - return true; - } - return false; -} - #if VMA_MEMORY_BUDGET void VmaAllocator_T::UpdateVulkanBudget() @@ -17912,7 +16218,7 @@ void VmaAllocator_T::UpdateVulkanBudget() VkPhysicalDeviceMemoryProperties2KHR memProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PROPERTIES_2_KHR }; VkPhysicalDeviceMemoryBudgetPropertiesEXT budgetProps = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_BUDGET_PROPERTIES_EXT }; - VmaPnextChainPushFront(&memProps, &budgetProps); + memProps.pNext = &budgetProps; GetVulkanFunctions().vkGetPhysicalDeviceMemoryProperties2KHR(m_PhysicalDevice, &memProps); @@ -17924,20 +16230,6 @@ void VmaAllocator_T::UpdateVulkanBudget() m_Budget.m_VulkanUsage[heapIndex] = budgetProps.heapUsage[heapIndex]; m_Budget.m_VulkanBudget[heapIndex] = budgetProps.heapBudget[heapIndex]; m_Budget.m_BlockBytesAtBudgetFetch[heapIndex] = m_Budget.m_BlockBytes[heapIndex].load(); - - // Some bugged drivers return the budget incorrectly, e.g. 0 or much bigger than heap size. - if(m_Budget.m_VulkanBudget[heapIndex] == 0) - { - m_Budget.m_VulkanBudget[heapIndex] = m_MemProps.memoryHeaps[heapIndex].size * 8 / 10; // 80% heuristics. - } - else if(m_Budget.m_VulkanBudget[heapIndex] > m_MemProps.memoryHeaps[heapIndex].size) - { - m_Budget.m_VulkanBudget[heapIndex] = m_MemProps.memoryHeaps[heapIndex].size; - } - if(m_Budget.m_VulkanUsage[heapIndex] == 0 && m_Budget.m_BlockBytesAtBudgetFetch[heapIndex] > 0) - { - m_Budget.m_VulkanUsage[heapIndex] = m_Budget.m_BlockBytesAtBudgetFetch[heapIndex]; - } } m_Budget.m_OperationsSinceBudgetFetch = 0; } @@ -17985,8 +16277,9 @@ void VmaAllocator_T::PrintDetailedMap(VmaJsonWriter& json) for(uint32_t memTypeIndex = 0; memTypeIndex < GetMemoryTypeCount(); ++memTypeIndex) { VmaMutexLockRead dedicatedAllocationsLock(m_DedicatedAllocationsMutex[memTypeIndex], m_UseMutex); - DedicatedAllocationLinkedList& dedicatedAllocList = m_DedicatedAllocations[memTypeIndex]; - if(!dedicatedAllocList.IsEmpty()) + AllocationVectorType* const pDedicatedAllocVector = m_pDedicatedAllocations[memTypeIndex]; + VMA_ASSERT(pDedicatedAllocVector); + if(pDedicatedAllocVector->empty() == false) { if(dedicatedAllocationsStarted == false) { @@ -17998,14 +16291,14 @@ void VmaAllocator_T::PrintDetailedMap(VmaJsonWriter& json) json.BeginString("Type "); json.ContinueString(memTypeIndex); json.EndString(); - + json.BeginArray(); - for(VmaAllocation alloc = dedicatedAllocList.Front(); - alloc != VMA_NULL; alloc = dedicatedAllocList.GetNext(alloc)) + for(size_t i = 0; i < pDedicatedAllocVector->size(); ++i) { json.BeginObject(true); - alloc->PrintParameters(json); + const VmaAllocation hAlloc = (*pDedicatedAllocVector)[i]; + hAlloc->PrintParameters(json); json.EndObject(); } @@ -18046,17 +16339,18 @@ void VmaAllocator_T::PrintDetailedMap(VmaJsonWriter& json) // Custom pools { VmaMutexLockRead lock(m_PoolsMutex, m_UseMutex); - if(!m_Pools.IsEmpty()) + const size_t poolCount = m_Pools.size(); + if(poolCount > 0) { json.WriteString("Pools"); json.BeginObject(); - for(VmaPool pool = m_Pools.Front(); pool != VMA_NULL; pool = m_Pools.GetNext(pool)) + for(size_t poolIndex = 0; poolIndex < poolCount; ++poolIndex) { json.BeginString(); - json.ContinueString(pool->GetId()); + json.ContinueString(m_Pools[poolIndex]->GetId()); json.EndString(); - pool->m_BlockVector.PrintDetailedMap(json); + m_Pools[poolIndex]->m_BlockVector.PrintDetailedMap(json); } json.EndObject(); } @@ -18074,7 +16368,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateAllocator( { VMA_ASSERT(pCreateInfo && pAllocator); VMA_ASSERT(pCreateInfo->vulkanApiVersion == 0 || - (VK_VERSION_MAJOR(pCreateInfo->vulkanApiVersion) == 1 && VK_VERSION_MINOR(pCreateInfo->vulkanApiVersion) <= 2)); + (VK_VERSION_MAJOR(pCreateInfo->vulkanApiVersion) == 1 && VK_VERSION_MINOR(pCreateInfo->vulkanApiVersion) <= 1)); VMA_DEBUG_LOG("vmaCreateAllocator"); *pAllocator = vma_new(pCreateInfo->pAllocationCallbacks, VmaAllocator_T)(pCreateInfo); return (*pAllocator)->Init(pCreateInfo); @@ -18091,14 +16385,6 @@ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyAllocator( } } -VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocatorInfo(VmaAllocator allocator, VmaAllocatorInfo* pAllocatorInfo) -{ - VMA_ASSERT(allocator && pAllocatorInfo); - pAllocatorInfo->instance = allocator->m_hInstance; - pAllocatorInfo->physicalDevice = allocator->GetPhysicalDevice(); - pAllocatorInfo->device = allocator->m_hDevice; -} - VMA_CALL_PRE void VMA_CALL_POST vmaGetPhysicalDeviceProperties( VmaAllocator allocator, const VkPhysicalDeviceProperties **ppPhysicalDeviceProperties) @@ -18178,7 +16464,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString( json.WriteString("Total"); VmaPrintStatInfo(json, stats.total); - + for(uint32_t heapIndex = 0; heapIndex < allocator->GetMemoryHeapCount(); ++heapIndex) { json.BeginString("Heap "); @@ -18250,22 +16536,6 @@ VMA_CALL_PRE void VMA_CALL_POST vmaBuildStatsString( { json.WriteString("LAZILY_ALLOCATED"); } -#if VMA_VULKAN_VERSION >= 1001000 - if((flags & VK_MEMORY_PROPERTY_PROTECTED_BIT) != 0) - { - json.WriteString("PROTECTED"); - } -#endif // #if VMA_VULKAN_VERSION >= 1001000 -#if VK_AMD_device_coherent_memory - if((flags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY) != 0) - { - json.WriteString("DEVICE_COHERENT"); - } - if((flags & VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY) != 0) - { - json.WriteString("DEVICE_UNCACHED"); - } -#endif // #if VK_AMD_device_coherent_memory json.EndArray(); if(stats.memoryType[typeIndex].blockCount > 0) @@ -18325,13 +16595,11 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex( VMA_ASSERT(pAllocationCreateInfo != VMA_NULL); VMA_ASSERT(pMemoryTypeIndex != VMA_NULL); - memoryTypeBits &= allocator->GetGlobalMemoryTypeBits(); - if(pAllocationCreateInfo->memoryTypeBits != 0) { memoryTypeBits &= pAllocationCreateInfo->memoryTypeBits; } - + uint32_t requiredFlags = pAllocationCreateInfo->requiredFlags; uint32_t preferredFlags = pAllocationCreateInfo->preferredFlags; uint32_t notPreferredFlags = 0; @@ -18372,13 +16640,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndex( break; } - // Avoid DEVICE_COHERENT unless explicitly requested. - if(((pAllocationCreateInfo->requiredFlags | pAllocationCreateInfo->preferredFlags) & - (VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY | VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD_COPY)) == 0) - { - notPreferredFlags |= VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD_COPY; - } - *pMemoryTypeIndex = UINT32_MAX; uint32_t minCost = UINT32_MAX; for(uint32_t memTypeIndex = 0, memTypeBit = 1; @@ -18479,25 +16740,25 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFindMemoryTypeIndexForImageInfo( } VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreatePool( - VmaAllocator allocator, - const VmaPoolCreateInfo* pCreateInfo, - VmaPool* pPool) + VmaAllocator allocator, + const VmaPoolCreateInfo* pCreateInfo, + VmaPool* pPool) { VMA_ASSERT(allocator && pCreateInfo && pPool); - + VMA_DEBUG_LOG("vmaCreatePool"); - + VMA_DEBUG_GLOBAL_MUTEX_LOCK - + VkResult res = allocator->CreatePool(pCreateInfo, pPool); - + #if VMA_RECORDING_ENABLED if(allocator->GetRecorder() != VMA_NULL) { allocator->GetRecorder()->RecordCreatePool(allocator->GetCurrentFrameIndex(), *pCreateInfo, *pPool); } #endif - + return res; } @@ -18506,16 +16767,16 @@ VMA_CALL_PRE void VMA_CALL_POST vmaDestroyPool( VmaPool pool) { VMA_ASSERT(allocator); - + if(pool == VK_NULL_HANDLE) { return; } - + VMA_DEBUG_LOG("vmaDestroyPool"); - + VMA_DEBUG_GLOBAL_MUTEX_LOCK - + #if VMA_RECORDING_ENABLED if(allocator->GetRecorder() != VMA_NULL) { @@ -18573,8 +16834,8 @@ VMA_CALL_PRE void VMA_CALL_POST vmaGetPoolName( VmaPool pool, const char** ppName) { - VMA_ASSERT(allocator && pool && ppName); - + VMA_ASSERT(allocator && pool); + VMA_DEBUG_LOG("vmaGetPoolName"); VMA_DEBUG_GLOBAL_MUTEX_LOCK @@ -18616,12 +16877,11 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory( VMA_DEBUG_GLOBAL_MUTEX_LOCK - VkResult result = allocator->AllocateMemory( + VkResult result = allocator->AllocateMemory( *pVkMemoryRequirements, false, // requiresDedicatedAllocation false, // prefersDedicatedAllocation VK_NULL_HANDLE, // dedicatedBuffer - UINT32_MAX, // dedicatedBufferUsage VK_NULL_HANDLE, // dedicatedImage *pCreateInfo, VMA_SUBALLOCATION_TYPE_UNKNOWN, @@ -18638,13 +16898,13 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemory( *pAllocation); } #endif - + if(pAllocationInfo != VMA_NULL && result == VK_SUCCESS) { allocator->GetAllocationInfo(*pAllocation, pAllocationInfo); } - return result; + return result; } VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( @@ -18666,12 +16926,11 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( VMA_DEBUG_GLOBAL_MUTEX_LOCK - VkResult result = allocator->AllocateMemory( + VkResult result = allocator->AllocateMemory( *pVkMemoryRequirements, false, // requiresDedicatedAllocation false, // prefersDedicatedAllocation VK_NULL_HANDLE, // dedicatedBuffer - UINT32_MAX, // dedicatedBufferUsage VK_NULL_HANDLE, // dedicatedImage *pCreateInfo, VMA_SUBALLOCATION_TYPE_UNKNOWN, @@ -18689,7 +16948,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( pAllocations); } #endif - + if(pAllocationInfo != VMA_NULL && result == VK_SUCCESS) { for(size_t i = 0; i < allocationCount; ++i) @@ -18698,7 +16957,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryPages( } } - return result; + return result; } VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer( @@ -18726,7 +16985,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer( requiresDedicatedAllocation, prefersDedicatedAllocation, buffer, // dedicatedBuffer - UINT32_MAX, // dedicatedBufferUsage VK_NULL_HANDLE, // dedicatedImage *pCreateInfo, VMA_SUBALLOCATION_TYPE_BUFFER, @@ -18751,7 +17009,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForBuffer( allocator->GetAllocationInfo(*pAllocation, pAllocationInfo); } - return result; + return result; } VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage( @@ -18778,7 +17036,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage( requiresDedicatedAllocation, prefersDedicatedAllocation, VK_NULL_HANDLE, // dedicatedBuffer - UINT32_MAX, // dedicatedBufferUsage image, // dedicatedImage *pCreateInfo, VMA_SUBALLOCATION_TYPE_IMAGE_UNKNOWN, @@ -18803,7 +17060,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaAllocateMemoryForImage( allocator->GetAllocationInfo(*pAllocation, pAllocationInfo); } - return result; + return result; } VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory( @@ -18811,14 +17068,14 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory( VmaAllocation allocation) { VMA_ASSERT(allocator); - + if(allocation == VK_NULL_HANDLE) { return; } - + VMA_DEBUG_LOG("vmaFreeMemory"); - + VMA_DEBUG_GLOBAL_MUTEX_LOCK #if VMA_RECORDING_ENABLED @@ -18829,7 +17086,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory( allocation); } #endif - + allocator->FreeMemory( 1, // allocationCount &allocation); @@ -18838,7 +17095,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemory( VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages( VmaAllocator allocator, size_t allocationCount, - const VmaAllocation* pAllocations) + VmaAllocation* pAllocations) { if(allocationCount == 0) { @@ -18846,9 +17103,9 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages( } VMA_ASSERT(allocator); - + VMA_DEBUG_LOG("vmaFreeMemoryPages"); - + VMA_DEBUG_GLOBAL_MUTEX_LOCK #if VMA_RECORDING_ENABLED @@ -18860,10 +17117,24 @@ VMA_CALL_PRE void VMA_CALL_POST vmaFreeMemoryPages( pAllocations); } #endif - + allocator->FreeMemory(allocationCount, pAllocations); } +VMA_CALL_PRE VkResult VMA_CALL_POST vmaResizeAllocation( + VmaAllocator allocator, + VmaAllocation allocation, + VkDeviceSize newSize) +{ + VMA_ASSERT(allocator && allocation); + + VMA_DEBUG_LOG("vmaResizeAllocation"); + + VMA_DEBUG_GLOBAL_MUTEX_LOCK + + return allocator->ResizeAllocation(allocation, newSize); +} + VMA_CALL_PRE void VMA_CALL_POST vmaGetAllocationInfo( VmaAllocator allocator, VmaAllocation allocation, @@ -18990,7 +17261,7 @@ VMA_CALL_PRE void VMA_CALL_POST vmaUnmapMemory( allocator->Unmap(allocation); } -VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size) +VMA_CALL_PRE void VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size) { VMA_ASSERT(allocator && allocation); @@ -18998,7 +17269,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, V VMA_DEBUG_GLOBAL_MUTEX_LOCK - const VkResult res = allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_FLUSH); + allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_FLUSH); #if VMA_RECORDING_ENABLED if(allocator->GetRecorder() != VMA_NULL) @@ -19008,11 +17279,9 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocation(VmaAllocator allocator, V allocation, offset, size); } #endif - - return res; } -VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size) +VMA_CALL_PRE void VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocator, VmaAllocation allocation, VkDeviceSize offset, VkDeviceSize size) { VMA_ASSERT(allocator && allocation); @@ -19020,7 +17289,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocat VMA_DEBUG_GLOBAL_MUTEX_LOCK - const VkResult res = allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_INVALIDATE); + allocator->FlushOrInvalidateAllocation(allocation, offset, size, VMA_CACHE_INVALIDATE); #if VMA_RECORDING_ENABLED if(allocator->GetRecorder() != VMA_NULL) @@ -19030,72 +17299,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocation(VmaAllocator allocat allocation, offset, size); } #endif - - return res; -} - -VMA_CALL_PRE VkResult VMA_CALL_POST vmaFlushAllocations( - VmaAllocator allocator, - uint32_t allocationCount, - const VmaAllocation* allocations, - const VkDeviceSize* offsets, - const VkDeviceSize* sizes) -{ - VMA_ASSERT(allocator); - - if(allocationCount == 0) - { - return VK_SUCCESS; - } - - VMA_ASSERT(allocations); - - VMA_DEBUG_LOG("vmaFlushAllocations"); - - VMA_DEBUG_GLOBAL_MUTEX_LOCK - - const VkResult res = allocator->FlushOrInvalidateAllocations(allocationCount, allocations, offsets, sizes, VMA_CACHE_FLUSH); - -#if VMA_RECORDING_ENABLED - if(allocator->GetRecorder() != VMA_NULL) - { - //TODO - } -#endif - - return res; -} - -VMA_CALL_PRE VkResult VMA_CALL_POST vmaInvalidateAllocations( - VmaAllocator allocator, - uint32_t allocationCount, - const VmaAllocation* allocations, - const VkDeviceSize* offsets, - const VkDeviceSize* sizes) -{ - VMA_ASSERT(allocator); - - if(allocationCount == 0) - { - return VK_SUCCESS; - } - - VMA_ASSERT(allocations); - - VMA_DEBUG_LOG("vmaInvalidateAllocations"); - - VMA_DEBUG_GLOBAL_MUTEX_LOCK - - const VkResult res = allocator->FlushOrInvalidateAllocations(allocationCount, allocations, offsets, sizes, VMA_CACHE_INVALIDATE); - -#if VMA_RECORDING_ENABLED - if(allocator->GetRecorder() != VMA_NULL) - { - //TODO - } -#endif - - return res; } VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator allocator, uint32_t memoryTypeBits) @@ -19111,7 +17314,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCheckCorruption(VmaAllocator allocator, u VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragment( VmaAllocator allocator, - const VmaAllocation* pAllocations, + VmaAllocation* pAllocations, size_t allocationCount, VkBool32* pAllocationsChanged, const VmaDefragmentationInfo *pDefragmentationInfo, @@ -19208,42 +17411,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaDefragmentationEnd( } } -VMA_CALL_PRE VkResult VMA_CALL_POST vmaBeginDefragmentationPass( - VmaAllocator allocator, - VmaDefragmentationContext context, - VmaDefragmentationPassInfo* pInfo - ) -{ - VMA_ASSERT(allocator); - VMA_ASSERT(pInfo); - - VMA_DEBUG_LOG("vmaBeginDefragmentationPass"); - - VMA_DEBUG_GLOBAL_MUTEX_LOCK - - if(context == VK_NULL_HANDLE) - { - pInfo->moveCount = 0; - return VK_SUCCESS; - } - - return allocator->DefragmentationPassBegin(pInfo, context); -} -VMA_CALL_PRE VkResult VMA_CALL_POST vmaEndDefragmentationPass( - VmaAllocator allocator, - VmaDefragmentationContext context) -{ - VMA_ASSERT(allocator); - - VMA_DEBUG_LOG("vmaEndDefragmentationPass"); - VMA_DEBUG_GLOBAL_MUTEX_LOCK - - if(context == VK_NULL_HANDLE) - return VK_SUCCESS; - - return allocator->DefragmentationPassEnd(context); -} - VMA_CALL_PRE VkResult VMA_CALL_POST vmaBindBufferMemory( VmaAllocator allocator, VmaAllocation allocation, @@ -19318,15 +17485,9 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer( { return VK_ERROR_VALIDATION_FAILED_EXT; } - if((pBufferCreateInfo->usage & VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT_COPY) != 0 && - !allocator->m_UseKhrBufferDeviceAddress) - { - VMA_ASSERT(0 && "Creating a buffer with VK_BUFFER_USAGE_SHADER_DEVICE_ADDRESS_BIT is not valid if VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT was not used."); - return VK_ERROR_VALIDATION_FAILED_EXT; - } - + VMA_DEBUG_LOG("vmaCreateBuffer"); - + VMA_DEBUG_GLOBAL_MUTEX_LOCK *pBuffer = VK_NULL_HANDLE; @@ -19347,13 +17508,30 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateBuffer( allocator->GetBufferMemoryRequirements(*pBuffer, vkMemReq, requiresDedicatedAllocation, prefersDedicatedAllocation); + // Make sure alignment requirements for specific buffer usages reported + // in Physical Device Properties are included in alignment reported by memory requirements. + if((pBufferCreateInfo->usage & VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT) != 0) + { + VMA_ASSERT(vkMemReq.alignment % + allocator->m_PhysicalDeviceProperties.limits.minTexelBufferOffsetAlignment == 0); + } + if((pBufferCreateInfo->usage & VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT) != 0) + { + VMA_ASSERT(vkMemReq.alignment % + allocator->m_PhysicalDeviceProperties.limits.minUniformBufferOffsetAlignment == 0); + } + if((pBufferCreateInfo->usage & VK_BUFFER_USAGE_STORAGE_BUFFER_BIT) != 0) + { + VMA_ASSERT(vkMemReq.alignment % + allocator->m_PhysicalDeviceProperties.limits.minStorageBufferOffsetAlignment == 0); + } + // 3. Allocate memory using allocator. res = allocator->AllocateMemory( vkMemReq, requiresDedicatedAllocation, prefersDedicatedAllocation, *pBuffer, // dedicatedBuffer - pBufferCreateInfo->usage, // dedicatedBufferUsage VK_NULL_HANDLE, // dedicatedImage *pAllocationCreateInfo, VMA_SUBALLOCATION_TYPE_BUFFER, @@ -19481,7 +17659,7 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage( VmaSuballocationType suballocType = pImageCreateInfo->tiling == VK_IMAGE_TILING_OPTIMAL ? VMA_SUBALLOCATION_TYPE_IMAGE_OPTIMAL : VMA_SUBALLOCATION_TYPE_IMAGE_LINEAR; - + // 2. Allocate memory using allocator. VkMemoryRequirements vkMemReq = {}; bool requiresDedicatedAllocation = false; @@ -19494,7 +17672,6 @@ VMA_CALL_PRE VkResult VMA_CALL_POST vmaCreateImage( requiresDedicatedAllocation, prefersDedicatedAllocation, VK_NULL_HANDLE, // dedicatedBuffer - UINT32_MAX, // dedicatedBufferUsage *pImage, // dedicatedImage *pAllocationCreateInfo, suballocType, diff --git a/third_party/vkmemalloc/tnt/CMakeLists.txt b/third_party/vkmemalloc/tnt/CMakeLists.txt index 1e551077bd..12f959c002 100644 --- a/third_party/vkmemalloc/tnt/CMakeLists.txt +++ b/third_party/vkmemalloc/tnt/CMakeLists.txt @@ -3,7 +3,7 @@ project(vkmemalloc) set(TARGET vkmemalloc) -set(PUBLIC_HDR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include) +set(PUBLIC_HDR_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../src) set(PUBLIC_HDRS ${PUBLIC_HDR_DIR}/vk_mem_alloc.h) diff --git a/third_party/vkmemalloc/tnt/README b/third_party/vkmemalloc/tnt/README index 8b12a3c16c..8b30495f09 100644 --- a/third_party/vkmemalloc/tnt/README +++ b/third_party/vkmemalloc/tnt/README @@ -1,8 +1,8 @@ This folder was created as follows: -curl -L -O https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/archive/8d4a9e9.zip -unzip 8d4a9e9.zip +curl -L -O https://github.com/GPUOpen-LibrariesAndSDKs/VulkanMemoryAllocator/archive/v2.3.0.zip +unzip v2.3.0.zip mv VulkanMemoryAllocator-* vkmemalloc cd vkmemalloc -rm -rf build docs media tools +rm -rf build docs media tools bin premake rsync -r ./ ~/github/filament/third_party/vkmemalloc/ --delete --exclude tnt