Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1 | //
|
Adam Sawicki | aa18374 | 2021-02-16 17:28:49 +0100 | [diff] [blame] | 2 | // Copyright (c) 2017-2021 Advanced Micro Devices, Inc. All rights reserved.
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 3 | //
|
| 4 | // Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 5 | // of this software and associated documentation files (the "Software"), to deal
|
| 6 | // in the Software without restriction, including without limitation the rights
|
| 7 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 8 | // copies of the Software, and to permit persons to whom the Software is
|
| 9 | // furnished to do so, subject to the following conditions:
|
| 10 | //
|
| 11 | // The above copyright notice and this permission notice shall be included in
|
| 12 | // all copies or substantial portions of the Software.
|
| 13 | //
|
| 14 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 15 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 16 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 17 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 18 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 19 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
| 20 | // THE SOFTWARE.
|
| 21 | //
|
| 22 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 23 | #ifdef _WIN32
|
Adam Sawicki | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 24 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 25 | #include "SparseBindingTest.h"
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 26 | #include "Tests.h"
|
| 27 | #include "VmaUsage.h"
|
| 28 | #include "Common.h"
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 29 | #include <atomic>
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 30 |
|
| 31 | static const char* const SHADER_PATH1 = "./";
|
| 32 | static const char* const SHADER_PATH2 = "../bin/";
|
| 33 | static const wchar_t* const WINDOW_CLASS_NAME = L"VULKAN_MEMORY_ALLOCATOR_SAMPLE";
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 34 | static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_KHRONOS_validation";
|
Adam Sawicki | 8ef0d20 | 2020-03-02 15:43:47 +0100 | [diff] [blame] | 35 | static const char* const APP_TITLE_A = "Vulkan Memory Allocator Sample 2.4.0";
|
| 36 | static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 2.4.0";
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 37 |
|
| 38 | static const bool VSYNC = true;
|
| 39 | static const uint32_t COMMAND_BUFFER_COUNT = 2;
|
Adam Sawicki | a68c01c | 2018-03-13 16:40:45 +0100 | [diff] [blame] | 40 | static void* const CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA = (void*)(intptr_t)43564544;
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 41 | static const bool USE_CUSTOM_CPU_ALLOCATION_CALLBACKS = true;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 42 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 43 | VkPhysicalDevice g_hPhysicalDevice;
|
| 44 | VkDevice g_hDevice;
|
| 45 | VmaAllocator g_hAllocator;
|
Adam Sawicki | 4ac8ff8 | 2019-11-18 14:47:33 +0100 | [diff] [blame] | 46 | VkInstance g_hVulkanInstance;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 47 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 48 | bool g_EnableValidationLayer = true;
|
| 49 | bool VK_KHR_get_memory_requirements2_enabled = false;
|
| 50 | bool VK_KHR_get_physical_device_properties2_enabled = false;
|
| 51 | bool VK_KHR_dedicated_allocation_enabled = false;
|
| 52 | bool VK_KHR_bind_memory2_enabled = false;
|
| 53 | bool VK_EXT_memory_budget_enabled = false;
|
| 54 | bool VK_AMD_device_coherent_memory_enabled = false;
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 55 | bool VK_EXT_buffer_device_address_enabled = false;
|
| 56 | bool VK_KHR_buffer_device_address_enabled = false;
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 57 | bool VK_EXT_memory_priority_enabled = false;
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 58 | bool VK_EXT_debug_utils_enabled = false;
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 59 | bool g_SparseBindingEnabled = false;
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 60 | bool g_BufferDeviceAddressEnabled = false;
|
| 61 |
|
| 62 | // # Pointers to functions from extensions
|
| 63 | PFN_vkGetBufferDeviceAddressEXT g_vkGetBufferDeviceAddressEXT;
|
Adam Sawicki | 6cc5e85 | 2018-03-13 16:37:54 +0100 | [diff] [blame] | 64 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 65 | static HINSTANCE g_hAppInstance;
|
| 66 | static HWND g_hWnd;
|
| 67 | static LONG g_SizeX = 1280, g_SizeY = 720;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 68 | static VkSurfaceKHR g_hSurface;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 69 | static VkQueue g_hPresentQueue;
|
| 70 | static VkSurfaceFormatKHR g_SurfaceFormat;
|
| 71 | static VkExtent2D g_Extent;
|
| 72 | static VkSwapchainKHR g_hSwapchain;
|
| 73 | static std::vector<VkImage> g_SwapchainImages;
|
| 74 | static std::vector<VkImageView> g_SwapchainImageViews;
|
| 75 | static std::vector<VkFramebuffer> g_Framebuffers;
|
| 76 | static VkCommandPool g_hCommandPool;
|
| 77 | static VkCommandBuffer g_MainCommandBuffers[COMMAND_BUFFER_COUNT];
|
| 78 | static VkFence g_MainCommandBufferExecutedFances[COMMAND_BUFFER_COUNT];
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 79 | VkFence g_ImmediateFence;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 80 | static uint32_t g_NextCommandBufferIndex;
|
| 81 | static VkSemaphore g_hImageAvailableSemaphore;
|
| 82 | static VkSemaphore g_hRenderFinishedSemaphore;
|
| 83 | static uint32_t g_GraphicsQueueFamilyIndex = UINT_MAX;
|
| 84 | static uint32_t g_PresentQueueFamilyIndex = UINT_MAX;
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 85 | static uint32_t g_SparseBindingQueueFamilyIndex = UINT_MAX;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 86 | static VkDescriptorSetLayout g_hDescriptorSetLayout;
|
| 87 | static VkDescriptorPool g_hDescriptorPool;
|
| 88 | static VkDescriptorSet g_hDescriptorSet; // Automatically destroyed with m_DescriptorPool.
|
| 89 | static VkSampler g_hSampler;
|
| 90 | static VkFormat g_DepthFormat;
|
| 91 | static VkImage g_hDepthImage;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 92 | static VmaAllocation g_hDepthImageAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 93 | static VkImageView g_hDepthImageView;
|
| 94 |
|
| 95 | static VkSurfaceCapabilitiesKHR g_SurfaceCapabilities;
|
| 96 | static std::vector<VkSurfaceFormatKHR> g_SurfaceFormats;
|
| 97 | static std::vector<VkPresentModeKHR> g_PresentModes;
|
| 98 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 99 | static const VkDebugUtilsMessageSeverityFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY =
|
| 100 | //VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT |
|
| 101 | //VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT |
|
| 102 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT |
|
| 103 | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
|
| 104 | static const VkDebugUtilsMessageTypeFlagsEXT DEBUG_UTILS_MESSENGER_MESSAGE_TYPE =
|
| 105 | VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT |
|
| 106 | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT |
|
| 107 | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;
|
| 108 | static PFN_vkCreateDebugUtilsMessengerEXT vkCreateDebugUtilsMessengerEXT_Func;
|
| 109 | static PFN_vkDestroyDebugUtilsMessengerEXT vkDestroyDebugUtilsMessengerEXT_Func;
|
Adam Sawicki | 41b4111 | 2021-03-02 15:11:18 +0100 | [diff] [blame^] | 110 | static PFN_vkSetDebugUtilsObjectNameEXT vkSetDebugUtilsObjectNameEXT_Func;
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 111 | static VkDebugUtilsMessengerEXT g_DebugUtilsMessenger;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 112 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 113 | static VkQueue g_hGraphicsQueue;
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 114 | VkQueue g_hSparseBindingQueue;
|
Adam Sawicki | 978fcf5 | 2018-12-05 14:38:48 +0100 | [diff] [blame] | 115 | VkCommandBuffer g_hTemporaryCommandBuffer;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 116 |
|
| 117 | static VkPipelineLayout g_hPipelineLayout;
|
| 118 | static VkRenderPass g_hRenderPass;
|
| 119 | static VkPipeline g_hPipeline;
|
| 120 |
|
| 121 | static VkBuffer g_hVertexBuffer;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 122 | static VmaAllocation g_hVertexBufferAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 123 | static VkBuffer g_hIndexBuffer;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 124 | static VmaAllocation g_hIndexBufferAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 125 | static uint32_t g_VertexCount;
|
| 126 | static uint32_t g_IndexCount;
|
| 127 |
|
| 128 | static VkImage g_hTextureImage;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 129 | static VmaAllocation g_hTextureImageAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 130 | static VkImageView g_hTextureImageView;
|
| 131 |
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 132 | static std::atomic_uint32_t g_CpuAllocCount;
|
| 133 |
|
Adam Sawicki | a68c01c | 2018-03-13 16:40:45 +0100 | [diff] [blame] | 134 | static void* CustomCpuAllocation(
|
| 135 | void* pUserData, size_t size, size_t alignment,
|
| 136 | VkSystemAllocationScope allocationScope)
|
| 137 | {
|
| 138 | assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 139 | void* const result = _aligned_malloc(size, alignment);
|
| 140 | if(result)
|
| 141 | {
|
| 142 | ++g_CpuAllocCount;
|
| 143 | }
|
| 144 | return result;
|
Adam Sawicki | a68c01c | 2018-03-13 16:40:45 +0100 | [diff] [blame] | 145 | }
|
| 146 |
|
| 147 | static void* CustomCpuReallocation(
|
| 148 | void* pUserData, void* pOriginal, size_t size, size_t alignment,
|
| 149 | VkSystemAllocationScope allocationScope)
|
| 150 | {
|
| 151 | assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 152 | void* const result = _aligned_realloc(pOriginal, size, alignment);
|
| 153 | if(pOriginal && !result)
|
| 154 | {
|
| 155 | --g_CpuAllocCount;
|
| 156 | }
|
| 157 | else if(!pOriginal && result)
|
| 158 | {
|
| 159 | ++g_CpuAllocCount;
|
| 160 | }
|
| 161 | return result;
|
Adam Sawicki | a68c01c | 2018-03-13 16:40:45 +0100 | [diff] [blame] | 162 | }
|
| 163 |
|
| 164 | static void CustomCpuFree(void* pUserData, void* pMemory)
|
| 165 | {
|
| 166 | assert(pUserData == CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA);
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 167 | if(pMemory)
|
| 168 | {
|
| 169 | const uint32_t oldAllocCount = g_CpuAllocCount.fetch_sub(1);
|
| 170 | TEST(oldAllocCount > 0);
|
| 171 | _aligned_free(pMemory);
|
| 172 | }
|
Adam Sawicki | a68c01c | 2018-03-13 16:40:45 +0100 | [diff] [blame] | 173 | }
|
| 174 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 175 | static const VkAllocationCallbacks g_CpuAllocationCallbacks = {
|
| 176 | CUSTOM_CPU_ALLOCATION_CALLBACK_USER_DATA, // pUserData
|
| 177 | &CustomCpuAllocation, // pfnAllocation
|
| 178 | &CustomCpuReallocation, // pfnReallocation
|
| 179 | &CustomCpuFree // pfnFree
|
| 180 | };
|
| 181 |
|
| 182 | const VkAllocationCallbacks* g_Allocs;
|
| 183 |
|
Adam Sawicki | 41b4111 | 2021-03-02 15:11:18 +0100 | [diff] [blame^] | 184 | void SetDebugUtilsObjectName(VkObjectType type, uint64_t handle, const char* name)
|
| 185 | {
|
| 186 | if(vkSetDebugUtilsObjectNameEXT_Func == nullptr)
|
| 187 | return;
|
| 188 |
|
| 189 | VkDebugUtilsObjectNameInfoEXT info = { VK_STRUCTURE_TYPE_DEBUG_UTILS_OBJECT_NAME_INFO_EXT };
|
| 190 | info.objectType = type;
|
| 191 | info.objectHandle = handle;
|
| 192 | info.pObjectName = name;
|
| 193 | vkSetDebugUtilsObjectNameEXT_Func(g_hDevice, &info);
|
| 194 | }
|
| 195 |
|
Adam Sawicki | 978fcf5 | 2018-12-05 14:38:48 +0100 | [diff] [blame] | 196 | void BeginSingleTimeCommands()
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 197 | {
|
| 198 | VkCommandBufferBeginInfo cmdBufBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
| 199 | cmdBufBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
| 200 | ERR_GUARD_VULKAN( vkBeginCommandBuffer(g_hTemporaryCommandBuffer, &cmdBufBeginInfo) );
|
| 201 | }
|
| 202 |
|
Adam Sawicki | 978fcf5 | 2018-12-05 14:38:48 +0100 | [diff] [blame] | 203 | void EndSingleTimeCommands()
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 204 | {
|
| 205 | ERR_GUARD_VULKAN( vkEndCommandBuffer(g_hTemporaryCommandBuffer) );
|
| 206 |
|
| 207 | VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
|
| 208 | submitInfo.commandBufferCount = 1;
|
| 209 | submitInfo.pCommandBuffers = &g_hTemporaryCommandBuffer;
|
| 210 |
|
| 211 | ERR_GUARD_VULKAN( vkQueueSubmit(g_hGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE) );
|
| 212 | ERR_GUARD_VULKAN( vkQueueWaitIdle(g_hGraphicsQueue) );
|
| 213 | }
|
| 214 |
|
Adam Sawicki | da6c194 | 2018-12-05 17:34:34 +0100 | [diff] [blame] | 215 | void LoadShader(std::vector<char>& out, const char* fileName)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 216 | {
|
| 217 | std::ifstream file(std::string(SHADER_PATH1) + fileName, std::ios::ate | std::ios::binary);
|
| 218 | if(file.is_open() == false)
|
| 219 | file.open(std::string(SHADER_PATH2) + fileName, std::ios::ate | std::ios::binary);
|
| 220 | assert(file.is_open());
|
| 221 | size_t fileSize = (size_t)file.tellg();
|
| 222 | if(fileSize > 0)
|
| 223 | {
|
| 224 | out.resize(fileSize);
|
| 225 | file.seekg(0);
|
| 226 | file.read(out.data(), fileSize);
|
| 227 | file.close();
|
| 228 | }
|
| 229 | else
|
| 230 | out.clear();
|
| 231 | }
|
| 232 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 233 | static VkBool32 VKAPI_PTR MyDebugReportCallback(
|
| 234 | VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
|
| 235 | VkDebugUtilsMessageTypeFlagsEXT messageTypes,
|
| 236 | const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,
|
| 237 | void* pUserData)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 238 | {
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 239 | assert(pCallbackData && pCallbackData->pMessageIdName && pCallbackData->pMessage);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 240 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 241 | switch(messageSeverity)
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 242 | {
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 243 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT:
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 244 | SetConsoleColor(CONSOLE_COLOR::WARNING);
|
| 245 | break;
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 246 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT:
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 247 | SetConsoleColor(CONSOLE_COLOR::ERROR_);
|
| 248 | break;
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 249 | case VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT:
|
| 250 | SetConsoleColor(CONSOLE_COLOR::NORMAL);
|
| 251 | break;
|
| 252 | default: // VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 253 | SetConsoleColor(CONSOLE_COLOR::INFO);
|
| 254 | }
|
| 255 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 256 | printf("%s \xBA %s\n", pCallbackData->pMessageIdName, pCallbackData->pMessage);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 257 |
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 258 | SetConsoleColor(CONSOLE_COLOR::NORMAL);
|
| 259 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 260 | if(messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT ||
|
| 261 | messageSeverity == VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 262 | {
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 263 | OutputDebugStringA(pCallbackData->pMessage);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 264 | OutputDebugStringA("\n");
|
| 265 | }
|
| 266 |
|
| 267 | return VK_FALSE;
|
| 268 | }
|
| 269 |
|
| 270 | static VkSurfaceFormatKHR ChooseSurfaceFormat()
|
| 271 | {
|
| 272 | assert(!g_SurfaceFormats.empty());
|
| 273 |
|
| 274 | if((g_SurfaceFormats.size() == 1) && (g_SurfaceFormats[0].format == VK_FORMAT_UNDEFINED))
|
| 275 | {
|
| 276 | VkSurfaceFormatKHR result = { VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
|
| 277 | return result;
|
| 278 | }
|
| 279 |
|
| 280 | for(const auto& format : g_SurfaceFormats)
|
| 281 | {
|
| 282 | if((format.format == VK_FORMAT_B8G8R8A8_UNORM) &&
|
| 283 | (format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR))
|
| 284 | {
|
| 285 | return format;
|
| 286 | }
|
| 287 | }
|
| 288 |
|
| 289 | return g_SurfaceFormats[0];
|
| 290 | }
|
| 291 |
|
| 292 | VkPresentModeKHR ChooseSwapPresentMode()
|
| 293 | {
|
| 294 | VkPresentModeKHR preferredMode = VSYNC ? VK_PRESENT_MODE_MAILBOX_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR;
|
| 295 |
|
| 296 | if(std::find(g_PresentModes.begin(), g_PresentModes.end(), preferredMode) !=
|
| 297 | g_PresentModes.end())
|
| 298 | {
|
| 299 | return preferredMode;
|
| 300 | }
|
| 301 |
|
| 302 | return VK_PRESENT_MODE_FIFO_KHR;
|
| 303 | }
|
| 304 |
|
| 305 | static VkExtent2D ChooseSwapExtent()
|
| 306 | {
|
| 307 | if(g_SurfaceCapabilities.currentExtent.width != UINT_MAX)
|
| 308 | return g_SurfaceCapabilities.currentExtent;
|
| 309 |
|
| 310 | VkExtent2D result = {
|
| 311 | std::max(g_SurfaceCapabilities.minImageExtent.width,
|
| 312 | std::min(g_SurfaceCapabilities.maxImageExtent.width, (uint32_t)g_SizeX)),
|
| 313 | std::max(g_SurfaceCapabilities.minImageExtent.height,
|
| 314 | std::min(g_SurfaceCapabilities.maxImageExtent.height, (uint32_t)g_SizeY)) };
|
| 315 | return result;
|
| 316 | }
|
| 317 |
|
| 318 | struct Vertex
|
| 319 | {
|
| 320 | float pos[3];
|
| 321 | float color[3];
|
| 322 | float texCoord[2];
|
| 323 | };
|
| 324 |
|
| 325 | static void CreateMesh()
|
| 326 | {
|
| 327 | assert(g_hAllocator);
|
| 328 |
|
| 329 | static Vertex vertices[] = {
|
| 330 | // -X
|
| 331 | { { -1.f, -1.f, -1.f}, {1.0f, 0.0f, 0.0f}, {0.f, 0.f} },
|
| 332 | { { -1.f, -1.f, 1.f}, {1.0f, 0.0f, 0.0f}, {1.f, 0.f} },
|
| 333 | { { -1.f, 1.f, -1.f}, {1.0f, 0.0f, 0.0f}, {0.f, 1.f} },
|
| 334 | { { -1.f, 1.f, 1.f}, {1.0f, 0.0f, 0.0f}, {1.f, 1.f} },
|
| 335 | // +X
|
| 336 | { { 1.f, -1.f, 1.f}, {0.0f, 1.0f, 0.0f}, {0.f, 0.f} },
|
| 337 | { { 1.f, -1.f, -1.f}, {0.0f, 1.0f, 0.0f}, {1.f, 0.f} },
|
| 338 | { { 1.f, 1.f, 1.f}, {0.0f, 1.0f, 0.0f}, {0.f, 1.f} },
|
| 339 | { { 1.f, 1.f, -1.f}, {0.0f, 1.0f, 0.0f}, {1.f, 1.f} },
|
| 340 | // -Z
|
| 341 | { { 1.f, -1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {0.f, 0.f} },
|
| 342 | { {-1.f, -1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {1.f, 0.f} },
|
| 343 | { { 1.f, 1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {0.f, 1.f} },
|
| 344 | { {-1.f, 1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {1.f, 1.f} },
|
| 345 | // +Z
|
| 346 | { {-1.f, -1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {0.f, 0.f} },
|
| 347 | { { 1.f, -1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {1.f, 0.f} },
|
| 348 | { {-1.f, 1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {0.f, 1.f} },
|
| 349 | { { 1.f, 1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {1.f, 1.f} },
|
| 350 | // -Y
|
| 351 | { {-1.f, -1.f, -1.f}, {0.0f, 1.0f, 1.0f}, {0.f, 0.f} },
|
| 352 | { { 1.f, -1.f, -1.f}, {0.0f, 1.0f, 1.0f}, {1.f, 0.f} },
|
| 353 | { {-1.f, -1.f, 1.f}, {0.0f, 1.0f, 1.0f}, {0.f, 1.f} },
|
| 354 | { { 1.f, -1.f, 1.f}, {0.0f, 1.0f, 1.0f}, {1.f, 1.f} },
|
| 355 | // +Y
|
| 356 | { { 1.f, 1.f, -1.f}, {1.0f, 0.0f, 1.0f}, {0.f, 0.f} },
|
| 357 | { {-1.f, 1.f, -1.f}, {1.0f, 0.0f, 1.0f}, {1.f, 0.f} },
|
| 358 | { { 1.f, 1.f, 1.f}, {1.0f, 0.0f, 1.0f}, {0.f, 1.f} },
|
| 359 | { {-1.f, 1.f, 1.f}, {1.0f, 0.0f, 1.0f}, {1.f, 1.f} },
|
| 360 | };
|
| 361 | static uint16_t indices[] = {
|
| 362 | 0, 1, 2, 3, USHRT_MAX,
|
| 363 | 4, 5, 6, 7, USHRT_MAX,
|
| 364 | 8, 9, 10, 11, USHRT_MAX,
|
| 365 | 12, 13, 14, 15, USHRT_MAX,
|
| 366 | 16, 17, 18, 19, USHRT_MAX,
|
| 367 | 20, 21, 22, 23, USHRT_MAX,
|
| 368 | };
|
| 369 |
|
| 370 | size_t vertexBufferSize = sizeof(Vertex) * _countof(vertices);
|
| 371 | size_t indexBufferSize = sizeof(uint16_t) * _countof(indices);
|
| 372 | g_IndexCount = (uint32_t)_countof(indices);
|
| 373 |
|
| 374 | // Create vertex buffer
|
| 375 |
|
| 376 | VkBufferCreateInfo vbInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 377 | vbInfo.size = vertexBufferSize;
|
| 378 | vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 379 | vbInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 380 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 381 | VmaAllocationCreateInfo vbAllocCreateInfo = {};
|
| 382 | vbAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
Adam Sawicki | 5268dbb | 2017-11-08 12:52:05 +0100 | [diff] [blame] | 383 | vbAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 384 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 385 | VkBuffer stagingVertexBuffer = VK_NULL_HANDLE;
|
| 386 | VmaAllocation stagingVertexBufferAlloc = VK_NULL_HANDLE;
|
| 387 | VmaAllocationInfo stagingVertexBufferAllocInfo = {};
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 388 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbAllocCreateInfo, &stagingVertexBuffer, &stagingVertexBufferAlloc, &stagingVertexBufferAllocInfo) );
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 389 |
|
| 390 | memcpy(stagingVertexBufferAllocInfo.pMappedData, vertices, vertexBufferSize);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 391 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 392 | // No need to flush stagingVertexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 393 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 394 | vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT;
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 395 | vbAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 396 | vbAllocCreateInfo.flags = 0;
|
| 397 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbAllocCreateInfo, &g_hVertexBuffer, &g_hVertexBufferAlloc, nullptr) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 398 |
|
| 399 | // Create index buffer
|
| 400 |
|
| 401 | VkBufferCreateInfo ibInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 402 | ibInfo.size = indexBufferSize;
|
| 403 | ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 404 | ibInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 405 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 406 | VmaAllocationCreateInfo ibAllocCreateInfo = {};
|
| 407 | ibAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
Adam Sawicki | 5268dbb | 2017-11-08 12:52:05 +0100 | [diff] [blame] | 408 | ibAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 409 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 410 | VkBuffer stagingIndexBuffer = VK_NULL_HANDLE;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 411 | VmaAllocation stagingIndexBufferAlloc = VK_NULL_HANDLE;
|
| 412 | VmaAllocationInfo stagingIndexBufferAllocInfo = {};
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 413 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibAllocCreateInfo, &stagingIndexBuffer, &stagingIndexBufferAlloc, &stagingIndexBufferAllocInfo) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 414 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 415 | memcpy(stagingIndexBufferAllocInfo.pMappedData, indices, indexBufferSize);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 416 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 417 | // No need to flush stagingIndexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 418 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 419 | ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_INDEX_BUFFER_BIT;
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 420 | ibAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 421 | ibAllocCreateInfo.flags = 0;
|
| 422 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibAllocCreateInfo, &g_hIndexBuffer, &g_hIndexBufferAlloc, nullptr) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 423 |
|
| 424 | // Copy buffers
|
| 425 |
|
| 426 | BeginSingleTimeCommands();
|
| 427 |
|
| 428 | VkBufferCopy vbCopyRegion = {};
|
| 429 | vbCopyRegion.srcOffset = 0;
|
| 430 | vbCopyRegion.dstOffset = 0;
|
| 431 | vbCopyRegion.size = vbInfo.size;
|
| 432 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, stagingVertexBuffer, g_hVertexBuffer, 1, &vbCopyRegion);
|
| 433 |
|
| 434 | VkBufferCopy ibCopyRegion = {};
|
| 435 | ibCopyRegion.srcOffset = 0;
|
| 436 | ibCopyRegion.dstOffset = 0;
|
| 437 | ibCopyRegion.size = ibInfo.size;
|
| 438 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, stagingIndexBuffer, g_hIndexBuffer, 1, &ibCopyRegion);
|
| 439 |
|
| 440 | EndSingleTimeCommands();
|
| 441 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 442 | vmaDestroyBuffer(g_hAllocator, stagingIndexBuffer, stagingIndexBufferAlloc);
|
| 443 | vmaDestroyBuffer(g_hAllocator, stagingVertexBuffer, stagingVertexBufferAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 444 | }
|
| 445 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 446 | static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
| 447 | {
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 448 | // Create staging buffer.
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 449 |
|
| 450 | const VkDeviceSize imageSize = sizeX * sizeY * 4;
|
| 451 |
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 452 | VkBufferCreateInfo stagingBufInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 453 | stagingBufInfo.size = imageSize;
|
| 454 | stagingBufInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 455 |
|
| 456 | VmaAllocationCreateInfo stagingBufAllocCreateInfo = {};
|
| 457 | stagingBufAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 458 | stagingBufAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_MAPPED_BIT;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 459 |
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 460 | VkBuffer stagingBuf = VK_NULL_HANDLE;
|
| 461 | VmaAllocation stagingBufAlloc = VK_NULL_HANDLE;
|
| 462 | VmaAllocationInfo stagingBufAllocInfo = {};
|
| 463 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &stagingBufInfo, &stagingBufAllocCreateInfo, &stagingBuf, &stagingBufAlloc, &stagingBufAllocInfo) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 464 |
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 465 | char* const pImageData = (char*)stagingBufAllocInfo.pMappedData;
|
| 466 | uint8_t* pRowData = (uint8_t*)pImageData;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 467 | for(uint32_t y = 0; y < sizeY; ++y)
|
| 468 | {
|
| 469 | uint32_t* pPixelData = (uint32_t*)pRowData;
|
| 470 | for(uint32_t x = 0; x < sizeY; ++x)
|
| 471 | {
|
| 472 | *pPixelData =
|
| 473 | ((x & 0x18) == 0x08 ? 0x000000FF : 0x00000000) |
|
| 474 | ((x & 0x18) == 0x10 ? 0x0000FFFF : 0x00000000) |
|
| 475 | ((y & 0x18) == 0x08 ? 0x0000FF00 : 0x00000000) |
|
| 476 | ((y & 0x18) == 0x10 ? 0x00FF0000 : 0x00000000);
|
| 477 | ++pPixelData;
|
| 478 | }
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 479 | pRowData += sizeX * 4;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 480 | }
|
| 481 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 482 | // No need to flush stagingImage memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 483 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 484 | // Create g_hTextureImage in GPU memory.
|
| 485 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 486 | VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 487 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 488 | imageInfo.extent.width = sizeX;
|
| 489 | imageInfo.extent.height = sizeY;
|
| 490 | imageInfo.extent.depth = 1;
|
| 491 | imageInfo.mipLevels = 1;
|
| 492 | imageInfo.arrayLayers = 1;
|
| 493 | imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 494 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 495 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 496 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
| 497 | imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 498 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 499 | imageInfo.flags = 0;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 500 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 501 | VmaAllocationCreateInfo imageAllocCreateInfo = {};
|
| 502 | imageAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 503 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 504 | ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &imageInfo, &imageAllocCreateInfo, &g_hTextureImage, &g_hTextureImageAlloc, nullptr) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 505 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 506 | // Transition image layouts, copy image.
|
| 507 |
|
| 508 | BeginSingleTimeCommands();
|
| 509 |
|
| 510 | VkImageMemoryBarrier imgMemBarrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER };
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 511 | imgMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 512 | imgMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 513 | imgMemBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 514 | imgMemBarrier.subresourceRange.baseMipLevel = 0;
|
| 515 | imgMemBarrier.subresourceRange.levelCount = 1;
|
| 516 | imgMemBarrier.subresourceRange.baseArrayLayer = 0;
|
| 517 | imgMemBarrier.subresourceRange.layerCount = 1;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 518 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 519 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
| 520 | imgMemBarrier.image = g_hTextureImage;
|
| 521 | imgMemBarrier.srcAccessMask = 0;
|
| 522 | imgMemBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
| 523 |
|
| 524 | vkCmdPipelineBarrier(
|
| 525 | g_hTemporaryCommandBuffer,
|
| 526 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
| 527 | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
| 528 | 0,
|
| 529 | 0, nullptr,
|
| 530 | 0, nullptr,
|
| 531 | 1, &imgMemBarrier);
|
| 532 |
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 533 | VkBufferImageCopy region = {};
|
| 534 | region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 535 | region.imageSubresource.layerCount = 1;
|
| 536 | region.imageExtent.width = sizeX;
|
| 537 | region.imageExtent.height = sizeY;
|
| 538 | region.imageExtent.depth = 1;
|
| 539 |
|
| 540 | vkCmdCopyBufferToImage(g_hTemporaryCommandBuffer, stagingBuf, g_hTextureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 541 |
|
| 542 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
| 543 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
| 544 | imgMemBarrier.image = g_hTextureImage;
|
| 545 | imgMemBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
| 546 | imgMemBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
| 547 |
|
| 548 | vkCmdPipelineBarrier(
|
| 549 | g_hTemporaryCommandBuffer,
|
| 550 | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
| 551 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
| 552 | 0,
|
| 553 | 0, nullptr,
|
| 554 | 0, nullptr,
|
| 555 | 1, &imgMemBarrier);
|
| 556 |
|
| 557 | EndSingleTimeCommands();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 558 |
|
Adam Sawicki | f43e9cd | 2019-02-20 14:55:31 +0100 | [diff] [blame] | 559 | vmaDestroyBuffer(g_hAllocator, stagingBuf, stagingBufAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 560 |
|
| 561 | // Create ImageView
|
| 562 |
|
| 563 | VkImageViewCreateInfo textureImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 564 | textureImageViewInfo.image = g_hTextureImage;
|
| 565 | textureImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 566 | textureImageViewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 567 | textureImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 568 | textureImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 569 | textureImageViewInfo.subresourceRange.levelCount = 1;
|
| 570 | textureImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 571 | textureImageViewInfo.subresourceRange.layerCount = 1;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 572 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &textureImageViewInfo, g_Allocs, &g_hTextureImageView) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 573 | }
|
| 574 |
|
| 575 | struct UniformBufferObject
|
| 576 | {
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame] | 577 | mat4 ModelViewProj;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 578 | };
|
| 579 |
|
| 580 | static void RegisterDebugCallbacks()
|
| 581 | {
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 582 | vkCreateDebugUtilsMessengerEXT_Func = (PFN_vkCreateDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
|
| 583 | g_hVulkanInstance, "vkCreateDebugUtilsMessengerEXT");
|
| 584 | vkDestroyDebugUtilsMessengerEXT_Func = (PFN_vkDestroyDebugUtilsMessengerEXT)vkGetInstanceProcAddr(
|
| 585 | g_hVulkanInstance, "vkDestroyDebugUtilsMessengerEXT");
|
Adam Sawicki | 41b4111 | 2021-03-02 15:11:18 +0100 | [diff] [blame^] | 586 | vkSetDebugUtilsObjectNameEXT_Func = (PFN_vkSetDebugUtilsObjectNameEXT)vkGetInstanceProcAddr(
|
| 587 | g_hVulkanInstance, "vkSetDebugUtilsObjectNameEXT");
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 588 | assert(vkCreateDebugUtilsMessengerEXT_Func);
|
| 589 | assert(vkDestroyDebugUtilsMessengerEXT_Func);
|
Adam Sawicki | 41b4111 | 2021-03-02 15:11:18 +0100 | [diff] [blame^] | 590 | assert(vkSetDebugUtilsObjectNameEXT_Func);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 591 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 592 | VkDebugUtilsMessengerCreateInfoEXT messengerCreateInfo = { VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT };
|
| 593 | messengerCreateInfo.messageSeverity = DEBUG_UTILS_MESSENGER_MESSAGE_SEVERITY;
|
| 594 | messengerCreateInfo.messageType = DEBUG_UTILS_MESSENGER_MESSAGE_TYPE;
|
| 595 | messengerCreateInfo.pfnUserCallback = MyDebugReportCallback;
|
| 596 | ERR_GUARD_VULKAN( vkCreateDebugUtilsMessengerEXT_Func(g_hVulkanInstance, &messengerCreateInfo, g_Allocs, &g_DebugUtilsMessenger) );
|
| 597 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 598 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 599 | static void UnregisterDebugCallbacks()
|
| 600 | {
|
| 601 | if(g_DebugUtilsMessenger)
|
| 602 | {
|
| 603 | vkDestroyDebugUtilsMessengerEXT_Func(g_hVulkanInstance, g_DebugUtilsMessenger, g_Allocs);
|
| 604 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 605 | }
|
| 606 |
|
| 607 | static bool IsLayerSupported(const VkLayerProperties* pProps, size_t propCount, const char* pLayerName)
|
| 608 | {
|
| 609 | const VkLayerProperties* propsEnd = pProps + propCount;
|
| 610 | return std::find_if(
|
| 611 | pProps,
|
| 612 | propsEnd,
|
| 613 | [pLayerName](const VkLayerProperties& prop) -> bool {
|
| 614 | return strcmp(pLayerName, prop.layerName) == 0;
|
| 615 | }) != propsEnd;
|
| 616 | }
|
| 617 |
|
| 618 | static VkFormat FindSupportedFormat(
|
| 619 | const std::vector<VkFormat>& candidates,
|
| 620 | VkImageTiling tiling,
|
| 621 | VkFormatFeatureFlags features)
|
| 622 | {
|
| 623 | for (VkFormat format : candidates)
|
| 624 | {
|
| 625 | VkFormatProperties props;
|
| 626 | vkGetPhysicalDeviceFormatProperties(g_hPhysicalDevice, format, &props);
|
| 627 |
|
| 628 | if ((tiling == VK_IMAGE_TILING_LINEAR) &&
|
| 629 | ((props.linearTilingFeatures & features) == features))
|
| 630 | {
|
| 631 | return format;
|
| 632 | }
|
| 633 | else if ((tiling == VK_IMAGE_TILING_OPTIMAL) &&
|
| 634 | ((props.optimalTilingFeatures & features) == features))
|
| 635 | {
|
| 636 | return format;
|
| 637 | }
|
| 638 | }
|
| 639 | return VK_FORMAT_UNDEFINED;
|
| 640 | }
|
| 641 |
|
| 642 | static VkFormat FindDepthFormat()
|
| 643 | {
|
| 644 | std::vector<VkFormat> formats;
|
| 645 | formats.push_back(VK_FORMAT_D32_SFLOAT);
|
| 646 | formats.push_back(VK_FORMAT_D32_SFLOAT_S8_UINT);
|
| 647 | formats.push_back(VK_FORMAT_D24_UNORM_S8_UINT);
|
| 648 |
|
| 649 | return FindSupportedFormat(
|
| 650 | formats,
|
| 651 | VK_IMAGE_TILING_OPTIMAL,
|
| 652 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
|
| 653 | }
|
| 654 |
|
| 655 | static void CreateSwapchain()
|
| 656 | {
|
| 657 | // Query surface formats.
|
| 658 |
|
| 659 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_hPhysicalDevice, g_hSurface, &g_SurfaceCapabilities) );
|
| 660 |
|
| 661 | uint32_t formatCount = 0;
|
| 662 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceFormatsKHR(g_hPhysicalDevice, g_hSurface, &formatCount, nullptr) );
|
| 663 | g_SurfaceFormats.resize(formatCount);
|
| 664 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceFormatsKHR(g_hPhysicalDevice, g_hSurface, &formatCount, g_SurfaceFormats.data()) );
|
| 665 |
|
| 666 | uint32_t presentModeCount = 0;
|
| 667 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfacePresentModesKHR(g_hPhysicalDevice, g_hSurface, &presentModeCount, nullptr) );
|
| 668 | g_PresentModes.resize(presentModeCount);
|
| 669 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfacePresentModesKHR(g_hPhysicalDevice, g_hSurface, &presentModeCount, g_PresentModes.data()) );
|
| 670 |
|
| 671 | // Create swap chain
|
| 672 |
|
| 673 | g_SurfaceFormat = ChooseSurfaceFormat();
|
| 674 | VkPresentModeKHR presentMode = ChooseSwapPresentMode();
|
| 675 | g_Extent = ChooseSwapExtent();
|
| 676 |
|
| 677 | uint32_t imageCount = g_SurfaceCapabilities.minImageCount + 1;
|
| 678 | if((g_SurfaceCapabilities.maxImageCount > 0) &&
|
| 679 | (imageCount > g_SurfaceCapabilities.maxImageCount))
|
| 680 | {
|
| 681 | imageCount = g_SurfaceCapabilities.maxImageCount;
|
| 682 | }
|
| 683 |
|
| 684 | VkSwapchainCreateInfoKHR swapChainInfo = { VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR };
|
| 685 | swapChainInfo.surface = g_hSurface;
|
| 686 | swapChainInfo.minImageCount = imageCount;
|
| 687 | swapChainInfo.imageFormat = g_SurfaceFormat.format;
|
| 688 | swapChainInfo.imageColorSpace = g_SurfaceFormat.colorSpace;
|
| 689 | swapChainInfo.imageExtent = g_Extent;
|
| 690 | swapChainInfo.imageArrayLayers = 1;
|
| 691 | swapChainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
| 692 | swapChainInfo.preTransform = g_SurfaceCapabilities.currentTransform;
|
| 693 | swapChainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
| 694 | swapChainInfo.presentMode = presentMode;
|
| 695 | swapChainInfo.clipped = VK_TRUE;
|
| 696 | swapChainInfo.oldSwapchain = g_hSwapchain;
|
| 697 |
|
| 698 | uint32_t queueFamilyIndices[] = { g_GraphicsQueueFamilyIndex, g_PresentQueueFamilyIndex };
|
| 699 | if(g_PresentQueueFamilyIndex != g_GraphicsQueueFamilyIndex)
|
| 700 | {
|
| 701 | swapChainInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
| 702 | swapChainInfo.queueFamilyIndexCount = 2;
|
| 703 | swapChainInfo.pQueueFamilyIndices = queueFamilyIndices;
|
| 704 | }
|
| 705 | else
|
| 706 | {
|
| 707 | swapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 708 | }
|
| 709 |
|
| 710 | VkSwapchainKHR hNewSwapchain = VK_NULL_HANDLE;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 711 | ERR_GUARD_VULKAN( vkCreateSwapchainKHR(g_hDevice, &swapChainInfo, g_Allocs, &hNewSwapchain) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 712 | if(g_hSwapchain != VK_NULL_HANDLE)
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 713 | vkDestroySwapchainKHR(g_hDevice, g_hSwapchain, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 714 | g_hSwapchain = hNewSwapchain;
|
| 715 |
|
| 716 | // Retrieve swapchain images.
|
| 717 |
|
| 718 | uint32_t swapchainImageCount = 0;
|
| 719 | ERR_GUARD_VULKAN( vkGetSwapchainImagesKHR(g_hDevice, g_hSwapchain, &swapchainImageCount, nullptr) );
|
| 720 | g_SwapchainImages.resize(swapchainImageCount);
|
| 721 | ERR_GUARD_VULKAN( vkGetSwapchainImagesKHR(g_hDevice, g_hSwapchain, &swapchainImageCount, g_SwapchainImages.data()) );
|
| 722 |
|
| 723 | // Create swapchain image views.
|
| 724 |
|
| 725 | for(size_t i = g_SwapchainImageViews.size(); i--; )
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 726 | vkDestroyImageView(g_hDevice, g_SwapchainImageViews[i], g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 727 | g_SwapchainImageViews.clear();
|
| 728 |
|
| 729 | VkImageViewCreateInfo swapchainImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 730 | g_SwapchainImageViews.resize(swapchainImageCount);
|
| 731 | for(uint32_t i = 0; i < swapchainImageCount; ++i)
|
| 732 | {
|
| 733 | swapchainImageViewInfo.image = g_SwapchainImages[i];
|
| 734 | swapchainImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 735 | swapchainImageViewInfo.format = g_SurfaceFormat.format;
|
| 736 | swapchainImageViewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 737 | swapchainImageViewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 738 | swapchainImageViewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 739 | swapchainImageViewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 740 | swapchainImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 741 | swapchainImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 742 | swapchainImageViewInfo.subresourceRange.levelCount = 1;
|
| 743 | swapchainImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 744 | swapchainImageViewInfo.subresourceRange.layerCount = 1;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 745 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &swapchainImageViewInfo, g_Allocs, &g_SwapchainImageViews[i]) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 746 | }
|
| 747 |
|
| 748 | // Create depth buffer
|
| 749 |
|
| 750 | g_DepthFormat = FindDepthFormat();
|
| 751 | assert(g_DepthFormat != VK_FORMAT_UNDEFINED);
|
| 752 |
|
| 753 | VkImageCreateInfo depthImageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 754 | depthImageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 755 | depthImageInfo.extent.width = g_Extent.width;
|
| 756 | depthImageInfo.extent.height = g_Extent.height;
|
| 757 | depthImageInfo.extent.depth = 1;
|
| 758 | depthImageInfo.mipLevels = 1;
|
| 759 | depthImageInfo.arrayLayers = 1;
|
| 760 | depthImageInfo.format = g_DepthFormat;
|
| 761 | depthImageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 762 | depthImageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 763 | depthImageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
| 764 | depthImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 765 | depthImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 766 | depthImageInfo.flags = 0;
|
| 767 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 768 | VmaAllocationCreateInfo depthImageAllocCreateInfo = {};
|
| 769 | depthImageAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 770 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 771 | ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &depthImageInfo, &depthImageAllocCreateInfo, &g_hDepthImage, &g_hDepthImageAlloc, nullptr) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 772 |
|
| 773 | VkImageViewCreateInfo depthImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 774 | depthImageViewInfo.image = g_hDepthImage;
|
| 775 | depthImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 776 | depthImageViewInfo.format = g_DepthFormat;
|
| 777 | depthImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
| 778 | depthImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 779 | depthImageViewInfo.subresourceRange.levelCount = 1;
|
| 780 | depthImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 781 | depthImageViewInfo.subresourceRange.layerCount = 1;
|
| 782 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 783 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &depthImageViewInfo, g_Allocs, &g_hDepthImageView) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 784 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 785 | // Create pipeline layout
|
| 786 | {
|
| 787 | if(g_hPipelineLayout != VK_NULL_HANDLE)
|
| 788 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 789 | vkDestroyPipelineLayout(g_hDevice, g_hPipelineLayout, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 790 | g_hPipelineLayout = VK_NULL_HANDLE;
|
| 791 | }
|
| 792 |
|
| 793 | VkPushConstantRange pushConstantRanges[1];
|
| 794 | ZeroMemory(&pushConstantRanges, sizeof pushConstantRanges);
|
| 795 | pushConstantRanges[0].offset = 0;
|
| 796 | pushConstantRanges[0].size = sizeof(UniformBufferObject);
|
| 797 | pushConstantRanges[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
| 798 |
|
| 799 | VkDescriptorSetLayout descriptorSetLayouts[] = { g_hDescriptorSetLayout };
|
| 800 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
|
| 801 | pipelineLayoutInfo.setLayoutCount = 1;
|
| 802 | pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts;
|
| 803 | pipelineLayoutInfo.pushConstantRangeCount = 1;
|
| 804 | pipelineLayoutInfo.pPushConstantRanges = pushConstantRanges;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 805 | ERR_GUARD_VULKAN( vkCreatePipelineLayout(g_hDevice, &pipelineLayoutInfo, g_Allocs, &g_hPipelineLayout) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 806 | }
|
| 807 |
|
| 808 | // Create render pass
|
| 809 | {
|
| 810 | if(g_hRenderPass != VK_NULL_HANDLE)
|
| 811 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 812 | vkDestroyRenderPass(g_hDevice, g_hRenderPass, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 813 | g_hRenderPass = VK_NULL_HANDLE;
|
| 814 | }
|
| 815 |
|
| 816 | VkAttachmentDescription attachments[2];
|
| 817 | ZeroMemory(attachments, sizeof(attachments));
|
| 818 |
|
| 819 | attachments[0].format = g_SurfaceFormat.format;
|
| 820 | attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
| 821 | attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
| 822 | attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
| 823 | attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
| 824 | attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
Adam Sawicki | 8eb9d8e | 2017-11-13 16:30:14 +0100 | [diff] [blame] | 825 | attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 826 | attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
| 827 |
|
| 828 | attachments[1].format = g_DepthFormat;
|
| 829 | attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
| 830 | attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
| 831 | attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
| 832 | attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
| 833 | attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
Adam Sawicki | 8eb9d8e | 2017-11-13 16:30:14 +0100 | [diff] [blame] | 834 | attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 835 | attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
| 836 |
|
| 837 | VkAttachmentReference colorAttachmentRef = {};
|
| 838 | colorAttachmentRef.attachment = 0;
|
| 839 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
| 840 |
|
| 841 | VkAttachmentReference depthStencilAttachmentRef = {};
|
| 842 | depthStencilAttachmentRef.attachment = 1;
|
| 843 | depthStencilAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
| 844 |
|
| 845 | VkSubpassDescription subpassDesc = {};
|
| 846 | subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
| 847 | subpassDesc.colorAttachmentCount = 1;
|
| 848 | subpassDesc.pColorAttachments = &colorAttachmentRef;
|
| 849 | subpassDesc.pDepthStencilAttachment = &depthStencilAttachmentRef;
|
| 850 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 851 | VkRenderPassCreateInfo renderPassInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO };
|
| 852 | renderPassInfo.attachmentCount = (uint32_t)_countof(attachments);
|
| 853 | renderPassInfo.pAttachments = attachments;
|
| 854 | renderPassInfo.subpassCount = 1;
|
| 855 | renderPassInfo.pSubpasses = &subpassDesc;
|
Adam Sawicki | 14137d1 | 2017-10-16 18:06:05 +0200 | [diff] [blame] | 856 | renderPassInfo.dependencyCount = 0;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 857 | ERR_GUARD_VULKAN( vkCreateRenderPass(g_hDevice, &renderPassInfo, g_Allocs, &g_hRenderPass) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 858 | }
|
| 859 |
|
| 860 | // Create pipeline
|
| 861 | {
|
| 862 | std::vector<char> vertShaderCode;
|
| 863 | LoadShader(vertShaderCode, "Shader.vert.spv");
|
| 864 | VkShaderModuleCreateInfo shaderModuleInfo = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO };
|
| 865 | shaderModuleInfo.codeSize = vertShaderCode.size();
|
| 866 | shaderModuleInfo.pCode = (const uint32_t*)vertShaderCode.data();
|
| 867 | VkShaderModule hVertShaderModule = VK_NULL_HANDLE;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 868 | ERR_GUARD_VULKAN( vkCreateShaderModule(g_hDevice, &shaderModuleInfo, g_Allocs, &hVertShaderModule) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 869 |
|
| 870 | std::vector<char> hFragShaderCode;
|
| 871 | LoadShader(hFragShaderCode, "Shader.frag.spv");
|
| 872 | shaderModuleInfo.codeSize = hFragShaderCode.size();
|
| 873 | shaderModuleInfo.pCode = (const uint32_t*)hFragShaderCode.data();
|
| 874 | VkShaderModule fragShaderModule = VK_NULL_HANDLE;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 875 | ERR_GUARD_VULKAN( vkCreateShaderModule(g_hDevice, &shaderModuleInfo, g_Allocs, &fragShaderModule) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 876 |
|
| 877 | VkPipelineShaderStageCreateInfo vertPipelineShaderStageInfo = { VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
|
| 878 | vertPipelineShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
| 879 | vertPipelineShaderStageInfo.module = hVertShaderModule;
|
| 880 | vertPipelineShaderStageInfo.pName = "main";
|
| 881 |
|
| 882 | VkPipelineShaderStageCreateInfo fragPipelineShaderStageInfo = { VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
|
| 883 | fragPipelineShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
| 884 | fragPipelineShaderStageInfo.module = fragShaderModule;
|
| 885 | fragPipelineShaderStageInfo.pName = "main";
|
| 886 |
|
| 887 | VkPipelineShaderStageCreateInfo pipelineShaderStageInfos[] = {
|
| 888 | vertPipelineShaderStageInfo,
|
| 889 | fragPipelineShaderStageInfo
|
| 890 | };
|
| 891 |
|
| 892 | VkVertexInputBindingDescription bindingDescription = {};
|
| 893 | bindingDescription.binding = 0;
|
| 894 | bindingDescription.stride = sizeof(Vertex);
|
| 895 | bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
| 896 |
|
| 897 | VkVertexInputAttributeDescription attributeDescriptions[3];
|
| 898 | ZeroMemory(attributeDescriptions, sizeof(attributeDescriptions));
|
| 899 |
|
| 900 | attributeDescriptions[0].binding = 0;
|
| 901 | attributeDescriptions[0].location = 0;
|
| 902 | attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
| 903 | attributeDescriptions[0].offset = offsetof(Vertex, pos);
|
| 904 |
|
| 905 | attributeDescriptions[1].binding = 0;
|
| 906 | attributeDescriptions[1].location = 1;
|
| 907 | attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
| 908 | attributeDescriptions[1].offset = offsetof(Vertex, color);
|
| 909 |
|
| 910 | attributeDescriptions[2].binding = 0;
|
| 911 | attributeDescriptions[2].location = 2;
|
| 912 | attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
|
| 913 | attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
|
| 914 |
|
| 915 | VkPipelineVertexInputStateCreateInfo pipelineVertexInputStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
|
| 916 | pipelineVertexInputStateInfo.vertexBindingDescriptionCount = 1;
|
| 917 | pipelineVertexInputStateInfo.pVertexBindingDescriptions = &bindingDescription;
|
| 918 | pipelineVertexInputStateInfo.vertexAttributeDescriptionCount = _countof(attributeDescriptions);
|
| 919 | pipelineVertexInputStateInfo.pVertexAttributeDescriptions = attributeDescriptions;
|
| 920 |
|
| 921 | VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
|
| 922 | pipelineInputAssemblyStateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
|
| 923 | pipelineInputAssemblyStateInfo.primitiveRestartEnable = VK_TRUE;
|
| 924 |
|
| 925 | VkViewport viewport = {};
|
| 926 | viewport.x = 0.f;
|
| 927 | viewport.y = 0.f;
|
| 928 | viewport.width = (float)g_Extent.width;
|
| 929 | viewport.height = (float)g_Extent.height;
|
| 930 | viewport.minDepth = 0.f;
|
| 931 | viewport.maxDepth = 1.f;
|
| 932 |
|
| 933 | VkRect2D scissor = {};
|
| 934 | scissor.offset.x = 0;
|
| 935 | scissor.offset.y = 0;
|
| 936 | scissor.extent = g_Extent;
|
| 937 |
|
| 938 | VkPipelineViewportStateCreateInfo pipelineViewportStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO };
|
| 939 | pipelineViewportStateInfo.viewportCount = 1;
|
| 940 | pipelineViewportStateInfo.pViewports = &viewport;
|
| 941 | pipelineViewportStateInfo.scissorCount = 1;
|
| 942 | pipelineViewportStateInfo.pScissors = &scissor;
|
| 943 |
|
| 944 | VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO };
|
| 945 | pipelineRasterizationStateInfo.depthClampEnable = VK_FALSE;
|
| 946 | pipelineRasterizationStateInfo.rasterizerDiscardEnable = VK_FALSE;
|
| 947 | pipelineRasterizationStateInfo.polygonMode = VK_POLYGON_MODE_FILL;
|
| 948 | pipelineRasterizationStateInfo.lineWidth = 1.f;
|
| 949 | pipelineRasterizationStateInfo.cullMode = VK_CULL_MODE_BACK_BIT;
|
| 950 | pipelineRasterizationStateInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
| 951 | pipelineRasterizationStateInfo.depthBiasEnable = VK_FALSE;
|
| 952 | pipelineRasterizationStateInfo.depthBiasConstantFactor = 0.f;
|
| 953 | pipelineRasterizationStateInfo.depthBiasClamp = 0.f;
|
| 954 | pipelineRasterizationStateInfo.depthBiasSlopeFactor = 0.f;
|
| 955 |
|
| 956 | VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
|
| 957 | pipelineMultisampleStateInfo.sampleShadingEnable = VK_FALSE;
|
| 958 | pipelineMultisampleStateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
| 959 | pipelineMultisampleStateInfo.minSampleShading = 1.f;
|
| 960 | pipelineMultisampleStateInfo.pSampleMask = nullptr;
|
| 961 | pipelineMultisampleStateInfo.alphaToCoverageEnable = VK_FALSE;
|
| 962 | pipelineMultisampleStateInfo.alphaToOneEnable = VK_FALSE;
|
| 963 |
|
| 964 | VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState = {};
|
| 965 | pipelineColorBlendAttachmentState.colorWriteMask =
|
| 966 | VK_COLOR_COMPONENT_R_BIT |
|
| 967 | VK_COLOR_COMPONENT_G_BIT |
|
| 968 | VK_COLOR_COMPONENT_B_BIT |
|
| 969 | VK_COLOR_COMPONENT_A_BIT;
|
| 970 | pipelineColorBlendAttachmentState.blendEnable = VK_FALSE;
|
| 971 | pipelineColorBlendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
| 972 | pipelineColorBlendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
| 973 | pipelineColorBlendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD; // Optional
|
| 974 | pipelineColorBlendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
| 975 | pipelineColorBlendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
| 976 | pipelineColorBlendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
|
| 977 |
|
| 978 | VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO };
|
| 979 | pipelineColorBlendStateInfo.logicOpEnable = VK_FALSE;
|
| 980 | pipelineColorBlendStateInfo.logicOp = VK_LOGIC_OP_COPY;
|
| 981 | pipelineColorBlendStateInfo.attachmentCount = 1;
|
| 982 | pipelineColorBlendStateInfo.pAttachments = &pipelineColorBlendAttachmentState;
|
| 983 |
|
| 984 | VkPipelineDepthStencilStateCreateInfo depthStencilStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO };
|
| 985 | depthStencilStateInfo.depthTestEnable = VK_TRUE;
|
| 986 | depthStencilStateInfo.depthWriteEnable = VK_TRUE;
|
| 987 | depthStencilStateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
|
| 988 | depthStencilStateInfo.depthBoundsTestEnable = VK_FALSE;
|
| 989 | depthStencilStateInfo.stencilTestEnable = VK_FALSE;
|
| 990 |
|
| 991 | VkGraphicsPipelineCreateInfo pipelineInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
|
| 992 | pipelineInfo.stageCount = 2;
|
| 993 | pipelineInfo.pStages = pipelineShaderStageInfos;
|
| 994 | pipelineInfo.pVertexInputState = &pipelineVertexInputStateInfo;
|
| 995 | pipelineInfo.pInputAssemblyState = &pipelineInputAssemblyStateInfo;
|
| 996 | pipelineInfo.pViewportState = &pipelineViewportStateInfo;
|
| 997 | pipelineInfo.pRasterizationState = &pipelineRasterizationStateInfo;
|
| 998 | pipelineInfo.pMultisampleState = &pipelineMultisampleStateInfo;
|
| 999 | pipelineInfo.pDepthStencilState = &depthStencilStateInfo;
|
| 1000 | pipelineInfo.pColorBlendState = &pipelineColorBlendStateInfo;
|
| 1001 | pipelineInfo.pDynamicState = nullptr;
|
| 1002 | pipelineInfo.layout = g_hPipelineLayout;
|
| 1003 | pipelineInfo.renderPass = g_hRenderPass;
|
| 1004 | pipelineInfo.subpass = 0;
|
| 1005 | pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
| 1006 | pipelineInfo.basePipelineIndex = -1;
|
| 1007 | ERR_GUARD_VULKAN( vkCreateGraphicsPipelines(
|
| 1008 | g_hDevice,
|
| 1009 | VK_NULL_HANDLE,
|
| 1010 | 1,
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1011 | &pipelineInfo,
|
| 1012 | g_Allocs,
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1013 | &g_hPipeline) );
|
| 1014 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1015 | vkDestroyShaderModule(g_hDevice, fragShaderModule, g_Allocs);
|
| 1016 | vkDestroyShaderModule(g_hDevice, hVertShaderModule, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1017 | }
|
| 1018 |
|
| 1019 | // Create frambuffers
|
| 1020 |
|
| 1021 | for(size_t i = g_Framebuffers.size(); i--; )
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1022 | vkDestroyFramebuffer(g_hDevice, g_Framebuffers[i], g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1023 | g_Framebuffers.clear();
|
| 1024 |
|
| 1025 | g_Framebuffers.resize(g_SwapchainImageViews.size());
|
| 1026 | for(size_t i = 0; i < g_SwapchainImages.size(); ++i)
|
| 1027 | {
|
| 1028 | VkImageView attachments[] = { g_SwapchainImageViews[i], g_hDepthImageView };
|
| 1029 |
|
| 1030 | VkFramebufferCreateInfo framebufferInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
|
| 1031 | framebufferInfo.renderPass = g_hRenderPass;
|
| 1032 | framebufferInfo.attachmentCount = (uint32_t)_countof(attachments);
|
| 1033 | framebufferInfo.pAttachments = attachments;
|
| 1034 | framebufferInfo.width = g_Extent.width;
|
| 1035 | framebufferInfo.height = g_Extent.height;
|
| 1036 | framebufferInfo.layers = 1;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1037 | ERR_GUARD_VULKAN( vkCreateFramebuffer(g_hDevice, &framebufferInfo, g_Allocs, &g_Framebuffers[i]) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1038 | }
|
| 1039 |
|
| 1040 | // Create semaphores
|
| 1041 |
|
| 1042 | if(g_hImageAvailableSemaphore != VK_NULL_HANDLE)
|
| 1043 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1044 | vkDestroySemaphore(g_hDevice, g_hImageAvailableSemaphore, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1045 | g_hImageAvailableSemaphore = VK_NULL_HANDLE;
|
| 1046 | }
|
| 1047 | if(g_hRenderFinishedSemaphore != VK_NULL_HANDLE)
|
| 1048 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1049 | vkDestroySemaphore(g_hDevice, g_hRenderFinishedSemaphore, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1050 | g_hRenderFinishedSemaphore = VK_NULL_HANDLE;
|
| 1051 | }
|
| 1052 |
|
| 1053 | VkSemaphoreCreateInfo semaphoreInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1054 | ERR_GUARD_VULKAN( vkCreateSemaphore(g_hDevice, &semaphoreInfo, g_Allocs, &g_hImageAvailableSemaphore) );
|
| 1055 | ERR_GUARD_VULKAN( vkCreateSemaphore(g_hDevice, &semaphoreInfo, g_Allocs, &g_hRenderFinishedSemaphore) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1056 | }
|
| 1057 |
|
| 1058 | static void DestroySwapchain(bool destroyActualSwapchain)
|
| 1059 | {
|
| 1060 | if(g_hImageAvailableSemaphore != VK_NULL_HANDLE)
|
| 1061 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1062 | vkDestroySemaphore(g_hDevice, g_hImageAvailableSemaphore, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1063 | g_hImageAvailableSemaphore = VK_NULL_HANDLE;
|
| 1064 | }
|
| 1065 | if(g_hRenderFinishedSemaphore != VK_NULL_HANDLE)
|
| 1066 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1067 | vkDestroySemaphore(g_hDevice, g_hRenderFinishedSemaphore, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1068 | g_hRenderFinishedSemaphore = VK_NULL_HANDLE;
|
| 1069 | }
|
| 1070 |
|
| 1071 | for(size_t i = g_Framebuffers.size(); i--; )
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1072 | vkDestroyFramebuffer(g_hDevice, g_Framebuffers[i], g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1073 | g_Framebuffers.clear();
|
| 1074 |
|
| 1075 | if(g_hDepthImageView != VK_NULL_HANDLE)
|
| 1076 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1077 | vkDestroyImageView(g_hDevice, g_hDepthImageView, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1078 | g_hDepthImageView = VK_NULL_HANDLE;
|
| 1079 | }
|
| 1080 | if(g_hDepthImage != VK_NULL_HANDLE)
|
| 1081 | {
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 1082 | vmaDestroyImage(g_hAllocator, g_hDepthImage, g_hDepthImageAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1083 | g_hDepthImage = VK_NULL_HANDLE;
|
| 1084 | }
|
| 1085 |
|
| 1086 | if(g_hPipeline != VK_NULL_HANDLE)
|
| 1087 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1088 | vkDestroyPipeline(g_hDevice, g_hPipeline, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1089 | g_hPipeline = VK_NULL_HANDLE;
|
| 1090 | }
|
| 1091 |
|
| 1092 | if(g_hRenderPass != VK_NULL_HANDLE)
|
| 1093 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1094 | vkDestroyRenderPass(g_hDevice, g_hRenderPass, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1095 | g_hRenderPass = VK_NULL_HANDLE;
|
| 1096 | }
|
| 1097 |
|
| 1098 | if(g_hPipelineLayout != VK_NULL_HANDLE)
|
| 1099 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1100 | vkDestroyPipelineLayout(g_hDevice, g_hPipelineLayout, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1101 | g_hPipelineLayout = VK_NULL_HANDLE;
|
| 1102 | }
|
| 1103 |
|
| 1104 | for(size_t i = g_SwapchainImageViews.size(); i--; )
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1105 | vkDestroyImageView(g_hDevice, g_SwapchainImageViews[i], g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1106 | g_SwapchainImageViews.clear();
|
| 1107 |
|
| 1108 | if(destroyActualSwapchain && (g_hSwapchain != VK_NULL_HANDLE))
|
| 1109 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1110 | vkDestroySwapchainKHR(g_hDevice, g_hSwapchain, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1111 | g_hSwapchain = VK_NULL_HANDLE;
|
| 1112 | }
|
| 1113 | }
|
| 1114 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1115 | static constexpr uint32_t GetVulkanApiVersion()
|
| 1116 | {
|
Adam Sawicki | 8ef0d20 | 2020-03-02 15:43:47 +0100 | [diff] [blame] | 1117 | #if VMA_VULKAN_VERSION == 1002000
|
| 1118 | return VK_API_VERSION_1_2;
|
| 1119 | #elif VMA_VULKAN_VERSION == 1001000
|
| 1120 | return VK_API_VERSION_1_1;
|
| 1121 | #elif VMA_VULKAN_VERSION == 1000000
|
| 1122 | return VK_API_VERSION_1_0;
|
| 1123 | #else
|
| 1124 | #error Invalid VMA_VULKAN_VERSION.
|
| 1125 | return UINT32_MAX;
|
| 1126 | #endif
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1127 | }
|
| 1128 |
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1129 | static void PrintEnabledFeatures()
|
| 1130 | {
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1131 | wprintf(L"Enabled extensions and features:\n");
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1132 | wprintf(L"Validation layer: %d\n", g_EnableValidationLayer ? 1 : 0);
|
| 1133 | wprintf(L"Sparse binding: %d\n", g_SparseBindingEnabled ? 1 : 0);
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1134 | wprintf(L"Buffer device address: %d\n", g_BufferDeviceAddressEnabled ? 1 : 0);
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1135 | if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
|
| 1136 | {
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1137 | wprintf(L"VK_KHR_get_memory_requirements2: %d\n", VK_KHR_get_memory_requirements2_enabled ? 1 : 0);
|
| 1138 | wprintf(L"VK_KHR_get_physical_device_properties2: %d\n", VK_KHR_get_physical_device_properties2_enabled ? 1 : 0);
|
| 1139 | wprintf(L"VK_KHR_dedicated_allocation: %d\n", VK_KHR_dedicated_allocation_enabled ? 1 : 0);
|
| 1140 | wprintf(L"VK_KHR_bind_memory2: %d\n", VK_KHR_bind_memory2_enabled ? 1 : 0);
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1141 | }
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1142 | wprintf(L"VK_EXT_memory_budget: %d\n", VK_EXT_memory_budget_enabled ? 1 : 0);
|
| 1143 | wprintf(L"VK_AMD_device_coherent_memory: %d\n", VK_AMD_device_coherent_memory_enabled ? 1 : 0);
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1144 | wprintf(L"VK_KHR_buffer_device_address: %d\n", VK_KHR_buffer_device_address_enabled ? 1 : 0);
|
| 1145 | wprintf(L"VK_EXT_buffer_device_address: %d\n", VK_EXT_buffer_device_address_enabled ? 1 : 0);
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1146 | wprintf(L"VK_EXT_memory_priority: %d\n", VK_EXT_memory_priority ? 1 : 0);
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1147 | }
|
| 1148 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1149 | void SetAllocatorCreateInfo(VmaAllocatorCreateInfo& outInfo)
|
| 1150 | {
|
| 1151 | outInfo = {};
|
| 1152 |
|
| 1153 | outInfo.physicalDevice = g_hPhysicalDevice;
|
| 1154 | outInfo.device = g_hDevice;
|
| 1155 | outInfo.instance = g_hVulkanInstance;
|
| 1156 | outInfo.vulkanApiVersion = GetVulkanApiVersion();
|
| 1157 |
|
| 1158 | if(VK_KHR_dedicated_allocation_enabled)
|
| 1159 | {
|
| 1160 | outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_DEDICATED_ALLOCATION_BIT;
|
| 1161 | }
|
| 1162 | if(VK_KHR_bind_memory2_enabled)
|
| 1163 | {
|
| 1164 | outInfo.flags |= VMA_ALLOCATOR_CREATE_KHR_BIND_MEMORY2_BIT;
|
| 1165 | }
|
| 1166 | #if !defined(VMA_MEMORY_BUDGET) || VMA_MEMORY_BUDGET == 1
|
Adam Sawicki | 6a93b8a | 2020-03-09 16:58:18 +0100 | [diff] [blame] | 1167 | if(VK_EXT_memory_budget_enabled && (
|
| 1168 | GetVulkanApiVersion() >= VK_API_VERSION_1_1 || VK_KHR_get_physical_device_properties2_enabled))
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1169 | {
|
| 1170 | outInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_BUDGET_BIT;
|
| 1171 | }
|
| 1172 | #endif
|
| 1173 | if(VK_AMD_device_coherent_memory_enabled)
|
| 1174 | {
|
| 1175 | outInfo.flags |= VMA_ALLOCATOR_CREATE_AMD_DEVICE_COHERENT_MEMORY_BIT;
|
| 1176 | }
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1177 | if(g_BufferDeviceAddressEnabled)
|
| 1178 | {
|
| 1179 | outInfo.flags |= VMA_ALLOCATOR_CREATE_BUFFER_DEVICE_ADDRESS_BIT;
|
| 1180 | }
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1181 | #if !defined(VMA_MEMORY_PRIORITY) || VMA_MEMORY_PRIORITY == 1
|
| 1182 | if(VK_EXT_memory_priority_enabled)
|
| 1183 | {
|
| 1184 | outInfo.flags |= VMA_ALLOCATOR_CREATE_EXT_MEMORY_PRIORITY_BIT;
|
| 1185 | }
|
| 1186 | #endif
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1187 |
|
| 1188 | if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS)
|
| 1189 | {
|
| 1190 | outInfo.pAllocationCallbacks = &g_CpuAllocationCallbacks;
|
| 1191 | }
|
| 1192 |
|
| 1193 | // Uncomment to enable recording to CSV file.
|
| 1194 | /*
|
| 1195 | static VmaRecordSettings recordSettings = {};
|
| 1196 | recordSettings.pFilePath = "VulkanSample.csv";
|
| 1197 | outInfo.pRecordSettings = &recordSettings;
|
| 1198 | */
|
| 1199 |
|
| 1200 | // Uncomment to enable HeapSizeLimit.
|
| 1201 | /*
|
| 1202 | static std::array<VkDeviceSize, VK_MAX_MEMORY_HEAPS> heapSizeLimit;
|
| 1203 | std::fill(heapSizeLimit.begin(), heapSizeLimit.end(), VK_WHOLE_SIZE);
|
| 1204 | heapSizeLimit[0] = 512ull * 1024 * 1024;
|
| 1205 | outInfo.pHeapSizeLimit = heapSizeLimit.data();
|
| 1206 | */
|
| 1207 | }
|
| 1208 |
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1209 | static void PrintPhysicalDeviceProperties(const VkPhysicalDeviceProperties& properties)
|
| 1210 | {
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1211 | wprintf(L"physicalDeviceProperties:\n");
|
| 1212 | wprintf(L" driverVersion: 0x%X\n", properties.driverVersion);
|
| 1213 | wprintf(L" vendorID: 0x%X (%s)\n", properties.vendorID, VendorIDToStr(properties.vendorID));
|
| 1214 | wprintf(L" deviceID: 0x%X\n", properties.deviceID);
|
| 1215 | wprintf(L" deviceType: %u (%s)\n", properties.deviceType, PhysicalDeviceTypeToStr(properties.deviceType));
|
| 1216 | wprintf(L" deviceName: %hs\n", properties.deviceName);
|
| 1217 | wprintf(L" limits:\n");
|
| 1218 | wprintf(L" maxMemoryAllocationCount: %u\n", properties.limits.maxMemoryAllocationCount);
|
| 1219 | wprintf(L" bufferImageGranularity: %llu B\n", properties.limits.bufferImageGranularity);
|
| 1220 | wprintf(L" nonCoherentAtomSize: %llu B\n", properties.limits.nonCoherentAtomSize);
|
| 1221 | }
|
| 1222 |
|
| 1223 | #if VMA_VULKAN_VERSION >= 1002000
|
| 1224 | static void PrintPhysicalDeviceVulkan11Properties(const VkPhysicalDeviceVulkan11Properties& properties)
|
| 1225 | {
|
| 1226 | wprintf(L"physicalDeviceVulkan11Properties:\n");
|
| 1227 | std::wstring sizeStr = SizeToStr(properties.maxMemoryAllocationSize);
|
| 1228 | wprintf(L" maxMemoryAllocationSize: %llu B (%s)\n", properties.maxMemoryAllocationSize, sizeStr.c_str());
|
| 1229 | }
|
| 1230 | static void PrintPhysicalDeviceVulkan12Properties(const VkPhysicalDeviceVulkan12Properties& properties)
|
| 1231 | {
|
| 1232 | wprintf(L"physicalDeviceVulkan12Properties:\n");
|
| 1233 | std::wstring str = DriverIDToStr(properties.driverID);
|
| 1234 | wprintf(L" driverID: %u (%s)\n", properties.driverID, str.c_str());
|
| 1235 | wprintf(L" driverName: %hs\n", properties.driverName);
|
| 1236 | wprintf(L" driverInfo: %hs\n", properties.driverInfo);
|
| 1237 | }
|
| 1238 | #endif // #if VMA_VULKAN_VERSION > 1002000
|
| 1239 |
|
| 1240 | static void AddFlagToStr(std::wstring& inout, const wchar_t* flagStr)
|
| 1241 | {
|
| 1242 | if(!inout.empty())
|
| 1243 | inout += L", ";
|
| 1244 | inout += flagStr;
|
| 1245 | }
|
| 1246 |
|
| 1247 | static std::wstring HeapFlagsToStr(VkMemoryHeapFlags flags)
|
| 1248 | {
|
| 1249 | std::wstring result;
|
| 1250 | if(flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT)
|
| 1251 | AddFlagToStr(result, L"DEVICE_LOCAL");
|
| 1252 | if(flags & VK_MEMORY_HEAP_MULTI_INSTANCE_BIT)
|
| 1253 | AddFlagToStr(result, L"MULTI_INSTANCE");
|
| 1254 | return result;
|
| 1255 | }
|
| 1256 |
|
| 1257 | static std::wstring PropertyFlagsToStr(VkMemoryPropertyFlags flags)
|
| 1258 | {
|
| 1259 | std::wstring result;
|
| 1260 | if(flags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
|
| 1261 | AddFlagToStr(result, L"DEVICE_LOCAL");
|
| 1262 | if(flags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT)
|
| 1263 | AddFlagToStr(result, L"HOST_VISIBLE");
|
| 1264 | if(flags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT)
|
| 1265 | AddFlagToStr(result, L"HOST_COHERENT");
|
| 1266 | if(flags & VK_MEMORY_PROPERTY_HOST_CACHED_BIT)
|
| 1267 | AddFlagToStr(result, L"HOST_CACHED");
|
| 1268 | if(flags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT)
|
| 1269 | AddFlagToStr(result, L"LAZILY_ALLOCATED");
|
| 1270 |
|
| 1271 | #if VMA_VULKAN_VERSION >= 1001000
|
| 1272 | if(flags & VK_MEMORY_PROPERTY_PROTECTED_BIT)
|
| 1273 | AddFlagToStr(result, L"PROTECTED");
|
| 1274 | #endif
|
| 1275 |
|
| 1276 | #if VK_AMD_device_coherent_memory
|
| 1277 | if(flags & VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD)
|
| 1278 | AddFlagToStr(result, L"DEVICE_COHERENT (AMD)");
|
| 1279 | if(flags & VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD)
|
| 1280 | AddFlagToStr(result, L"DEVICE_UNCACHED (AMD)");
|
| 1281 | #endif
|
| 1282 |
|
| 1283 | return result;
|
| 1284 | }
|
| 1285 |
|
| 1286 | static void PrintMemoryTypes()
|
| 1287 | {
|
| 1288 | wprintf(L"MEMORY HEAPS:\n");
|
| 1289 | const VkPhysicalDeviceMemoryProperties* memProps = nullptr;
|
| 1290 | vmaGetMemoryProperties(g_hAllocator, &memProps);
|
| 1291 |
|
| 1292 | wprintf(L"heapCount=%u, typeCount=%u\n", memProps->memoryHeapCount, memProps->memoryTypeCount);
|
| 1293 |
|
| 1294 | std::wstring sizeStr, flagsStr;
|
| 1295 | for(uint32_t heapIndex = 0; heapIndex < memProps->memoryHeapCount; ++heapIndex)
|
| 1296 | {
|
| 1297 | const VkMemoryHeap& heap = memProps->memoryHeaps[heapIndex];
|
| 1298 | sizeStr = SizeToStr(heap.size);
|
| 1299 | flagsStr = HeapFlagsToStr(heap.flags);
|
| 1300 | wprintf(L"Heap %u: %llu B (%s) %s\n", heapIndex, heap.size, sizeStr.c_str(), flagsStr.c_str());
|
| 1301 |
|
| 1302 | for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex)
|
| 1303 | {
|
| 1304 | const VkMemoryType& type = memProps->memoryTypes[typeIndex];
|
| 1305 | if(type.heapIndex == heapIndex)
|
| 1306 | {
|
| 1307 | flagsStr = PropertyFlagsToStr(type.propertyFlags);
|
| 1308 | wprintf(L" Type %u: %s\n", typeIndex, flagsStr.c_str());
|
| 1309 | }
|
| 1310 | }
|
| 1311 | }
|
| 1312 | }
|
| 1313 |
|
| 1314 | #if 0
|
| 1315 | template<typename It, typename MapFunc>
|
| 1316 | inline VkDeviceSize MapSum(It beg, It end, MapFunc mapFunc)
|
| 1317 | {
|
| 1318 | VkDeviceSize result = 0;
|
| 1319 | for(It it = beg; it != end; ++it)
|
| 1320 | result += mapFunc(*it);
|
| 1321 | return result;
|
| 1322 | }
|
| 1323 | #endif
|
| 1324 |
|
| 1325 | static bool CanCreateVertexBuffer(uint32_t allowedMemoryTypeBits)
|
| 1326 | {
|
| 1327 | VkBufferCreateInfo bufCreateInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 1328 | bufCreateInfo.size = 0x10000;
|
| 1329 | bufCreateInfo.usage = VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
| 1330 |
|
| 1331 | VkBuffer buf = VK_NULL_HANDLE;
|
| 1332 | VkResult res = vkCreateBuffer(g_hDevice, &bufCreateInfo, g_Allocs, &buf);
|
| 1333 | assert(res == VK_SUCCESS);
|
| 1334 |
|
| 1335 | VkMemoryRequirements memReq = {};
|
| 1336 | vkGetBufferMemoryRequirements(g_hDevice, buf, &memReq);
|
| 1337 |
|
| 1338 | vkDestroyBuffer(g_hDevice, buf, g_Allocs);
|
| 1339 |
|
| 1340 | return (memReq.memoryTypeBits & allowedMemoryTypeBits) != 0;
|
| 1341 | }
|
| 1342 |
|
| 1343 | static bool CanCreateOptimalSampledImage(uint32_t allowedMemoryTypeBits)
|
| 1344 | {
|
| 1345 | VkImageCreateInfo imgCreateInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 1346 | imgCreateInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 1347 | imgCreateInfo.extent.width = 256;
|
| 1348 | imgCreateInfo.extent.height = 256;
|
| 1349 | imgCreateInfo.extent.depth = 1;
|
| 1350 | imgCreateInfo.mipLevels = 1;
|
| 1351 | imgCreateInfo.arrayLayers = 1;
|
| 1352 | imgCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 1353 | imgCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 1354 | imgCreateInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 1355 | imgCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
| 1356 | imgCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 1357 |
|
| 1358 | VkImage img = VK_NULL_HANDLE;
|
| 1359 | VkResult res = vkCreateImage(g_hDevice, &imgCreateInfo, g_Allocs, &img);
|
| 1360 | assert(res == VK_SUCCESS);
|
| 1361 |
|
| 1362 | VkMemoryRequirements memReq = {};
|
| 1363 | vkGetImageMemoryRequirements(g_hDevice, img, &memReq);
|
| 1364 |
|
| 1365 | vkDestroyImage(g_hDevice, img, g_Allocs);
|
| 1366 |
|
| 1367 | return (memReq.memoryTypeBits & allowedMemoryTypeBits) != 0;
|
| 1368 | }
|
| 1369 |
|
| 1370 | static void PrintMemoryConclusions()
|
| 1371 | {
|
| 1372 | wprintf(L"Conclusions:\n");
|
| 1373 |
|
| 1374 | const VkPhysicalDeviceProperties* props = nullptr;
|
| 1375 | const VkPhysicalDeviceMemoryProperties* memProps = nullptr;
|
| 1376 | vmaGetPhysicalDeviceProperties(g_hAllocator, &props);
|
| 1377 | vmaGetMemoryProperties(g_hAllocator, &memProps);
|
| 1378 |
|
| 1379 | const uint32_t heapCount = memProps->memoryHeapCount;
|
| 1380 |
|
| 1381 | uint32_t deviceLocalHeapCount = 0;
|
| 1382 | uint32_t hostVisibleHeapCount = 0;
|
| 1383 | uint32_t deviceLocalAndHostVisibleHeapCount = 0;
|
| 1384 | VkDeviceSize deviceLocalHeapSumSize = 0;
|
| 1385 | VkDeviceSize hostVisibleHeapSumSize = 0;
|
| 1386 | VkDeviceSize deviceLocalAndHostVisibleHeapSumSize = 0;
|
| 1387 |
|
| 1388 | for(uint32_t heapIndex = 0; heapIndex < heapCount; ++heapIndex)
|
| 1389 | {
|
| 1390 | const VkMemoryHeap& heap = memProps->memoryHeaps[heapIndex];
|
| 1391 | const bool isDeviceLocal = (heap.flags & VK_MEMORY_HEAP_DEVICE_LOCAL_BIT) != 0;
|
| 1392 | bool isHostVisible = false;
|
| 1393 | for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex)
|
| 1394 | {
|
| 1395 | const VkMemoryType& type = memProps->memoryTypes[typeIndex];
|
| 1396 | if(type.heapIndex == heapIndex && (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))
|
| 1397 | {
|
| 1398 | isHostVisible = true;
|
| 1399 | break;
|
| 1400 | }
|
| 1401 | }
|
| 1402 | if(isDeviceLocal)
|
| 1403 | {
|
| 1404 | ++deviceLocalHeapCount;
|
| 1405 | deviceLocalHeapSumSize += heap.size;
|
| 1406 | }
|
| 1407 | if(isHostVisible)
|
| 1408 | {
|
| 1409 | ++hostVisibleHeapCount;
|
| 1410 | hostVisibleHeapSumSize += heap.size;
|
| 1411 | if(isDeviceLocal)
|
| 1412 | {
|
| 1413 | ++deviceLocalAndHostVisibleHeapCount;
|
| 1414 | deviceLocalAndHostVisibleHeapSumSize += heap.size;
|
| 1415 | }
|
| 1416 | }
|
| 1417 | }
|
| 1418 |
|
| 1419 | uint32_t hostVisibleNotHostCoherentTypeCount = 0;
|
| 1420 | uint32_t notDeviceLocalNotHostVisibleTypeCount = 0;
|
| 1421 | uint32_t amdSpecificTypeCount = 0;
|
| 1422 | uint32_t lazilyAllocatedTypeCount = 0;
|
| 1423 | uint32_t allTypeBits = 0;
|
| 1424 | uint32_t deviceLocalTypeBits = 0;
|
| 1425 | for(uint32_t typeIndex = 0; typeIndex < memProps->memoryTypeCount; ++typeIndex)
|
| 1426 | {
|
| 1427 | const VkMemoryType& type = memProps->memoryTypes[typeIndex];
|
| 1428 | allTypeBits |= 1u << typeIndex;
|
| 1429 | if(type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
|
| 1430 | {
|
| 1431 | deviceLocalTypeBits |= 1u << typeIndex;
|
| 1432 | }
|
| 1433 | if((type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) &&
|
| 1434 | (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) == 0)
|
| 1435 | {
|
| 1436 | ++hostVisibleNotHostCoherentTypeCount;
|
| 1437 | }
|
| 1438 | if((type.propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) == 0 &&
|
| 1439 | (type.propertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT) == 0)
|
| 1440 | {
|
| 1441 | ++notDeviceLocalNotHostVisibleTypeCount;
|
| 1442 | }
|
| 1443 | if(type.propertyFlags & (VK_MEMORY_PROPERTY_DEVICE_COHERENT_BIT_AMD | VK_MEMORY_PROPERTY_DEVICE_UNCACHED_BIT_AMD))
|
| 1444 | {
|
| 1445 | ++amdSpecificTypeCount;
|
| 1446 | }
|
| 1447 | if(type.propertyFlags & VK_MEMORY_PROPERTY_LAZILY_ALLOCATED_BIT)
|
| 1448 | {
|
| 1449 | ++lazilyAllocatedTypeCount;
|
| 1450 | }
|
| 1451 | }
|
| 1452 |
|
| 1453 | assert(deviceLocalHeapCount > 0);
|
| 1454 | if(deviceLocalHeapCount == heapCount)
|
| 1455 | wprintf(L"- All heaps are DEVICE_LOCAL.\n");
|
| 1456 | else
|
| 1457 | wprintf(L"- %u heaps are DEVICE_LOCAL, total %s.\n", deviceLocalHeapCount, SizeToStr(deviceLocalHeapSumSize).c_str());
|
| 1458 |
|
| 1459 | assert(hostVisibleHeapCount > 0);
|
| 1460 | if(hostVisibleHeapCount == heapCount)
|
| 1461 | wprintf(L"- All heaps are HOST_VISIBLE.\n");
|
| 1462 | else
|
| 1463 | wprintf(L"- %u heaps are HOST_VISIBLE, total %s.\n", deviceLocalHeapCount, SizeToStr(hostVisibleHeapSumSize).c_str());
|
| 1464 |
|
| 1465 | if(deviceLocalHeapCount < heapCount && hostVisibleHeapCount < heapCount)
|
| 1466 | {
|
| 1467 | if(deviceLocalAndHostVisibleHeapCount == 0)
|
| 1468 | wprintf(L"- No heaps are DEVICE_LOCAL and HOST_VISIBLE.\n");
|
| 1469 | if(deviceLocalAndHostVisibleHeapCount == heapCount)
|
| 1470 | wprintf(L"- All heaps are DEVICE_LOCAL and HOST_VISIBLE.\n");
|
| 1471 | else
|
| 1472 | wprintf(L"- %u heaps are DEVICE_LOCAL and HOST_VISIBLE, total %s.\n", deviceLocalHeapCount, SizeToStr(deviceLocalAndHostVisibleHeapSumSize).c_str());
|
| 1473 | }
|
| 1474 |
|
| 1475 | if(hostVisibleNotHostCoherentTypeCount == 0)
|
| 1476 | wprintf(L"- No types are HOST_VISIBLE but not HOST_COHERENT.\n");
|
| 1477 | else
|
| 1478 | wprintf(L"- %u types are HOST_VISIBLE but not HOST_COHERENT.\n", hostVisibleNotHostCoherentTypeCount);
|
| 1479 |
|
| 1480 | if(notDeviceLocalNotHostVisibleTypeCount == 0)
|
| 1481 | wprintf(L"- No types are not DEVICE_LOCAL and not HOST_VISIBLE.\n");
|
| 1482 | else
|
| 1483 | wprintf(L"- %u types are not DEVICE_LOCAL and not HOST_VISIBLE.\n", notDeviceLocalNotHostVisibleTypeCount);
|
| 1484 |
|
| 1485 | if(amdSpecificTypeCount == 0)
|
| 1486 | wprintf(L"- No types are AMD-specific DEVICE_COHERENT or DEVICE_UNCACHED.\n");
|
| 1487 | else
|
| 1488 | wprintf(L"- %u types are AMD-specific DEVICE_COHERENT or DEVICE_UNCACHED.\n", amdSpecificTypeCount);
|
| 1489 |
|
| 1490 | if(lazilyAllocatedTypeCount == 0)
|
| 1491 | wprintf(L"- No types are LAZILY_ALLOCATED.\n");
|
| 1492 | else
|
| 1493 | wprintf(L"- %u types are LAZILY_ALLOCATED.\n", lazilyAllocatedTypeCount);
|
| 1494 |
|
| 1495 | if(props->vendorID == VENDOR_ID_AMD &&
|
| 1496 | props->deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU &&
|
| 1497 | deviceLocalAndHostVisibleHeapSumSize > 256llu * 1024 * 1024)
|
| 1498 | {
|
| 1499 | wprintf(L"- AMD Smart Access Memory (SAM) is enabled!\n");
|
| 1500 | }
|
| 1501 |
|
| 1502 | if(deviceLocalHeapCount < heapCount)
|
| 1503 | {
|
| 1504 | const uint32_t nonDeviceLocalTypeBits = ~deviceLocalTypeBits & allTypeBits;
|
| 1505 |
|
| 1506 | if(CanCreateVertexBuffer(nonDeviceLocalTypeBits))
|
| 1507 | wprintf(L"- A buffer with VERTEX_BUFFER usage can be created in some non-DEVICE_LOCAL type.\n");
|
| 1508 | else
|
| 1509 | wprintf(L"- A buffer with VERTEX_BUFFER usage cannot be created in some non-DEVICE_LOCAL type.\n");
|
| 1510 |
|
| 1511 | if(CanCreateOptimalSampledImage(nonDeviceLocalTypeBits))
|
| 1512 | wprintf(L"- An image with OPTIMAL tiling and SAMPLED usage can be created in some non-DEVICE_LOCAL type.\n");
|
| 1513 | else
|
| 1514 | wprintf(L"- An image with OPTIMAL tiling and SAMPLED usage cannot be created in some non-DEVICE_LOCAL type.\n");
|
| 1515 | }
|
| 1516 |
|
| 1517 | //wprintf(L"\n");
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1518 | }
|
| 1519 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1520 | static void InitializeApplication()
|
| 1521 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1522 | if(USE_CUSTOM_CPU_ALLOCATION_CALLBACKS)
|
| 1523 | {
|
| 1524 | g_Allocs = &g_CpuAllocationCallbacks;
|
| 1525 | }
|
| 1526 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1527 | uint32_t instanceLayerPropCount = 0;
|
| 1528 | ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, nullptr) );
|
| 1529 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerPropCount);
|
| 1530 | if(instanceLayerPropCount > 0)
|
| 1531 | {
|
| 1532 | ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, instanceLayerProps.data()) );
|
| 1533 | }
|
| 1534 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 1535 | if(g_EnableValidationLayer)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1536 | {
|
| 1537 | if(IsLayerSupported(instanceLayerProps.data(), instanceLayerProps.size(), VALIDATION_LAYER_NAME) == false)
|
| 1538 | {
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1539 | wprintf(L"Layer \"%hs\" not supported.", VALIDATION_LAYER_NAME);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1540 | g_EnableValidationLayer = false;
|
| 1541 | }
|
| 1542 | }
|
| 1543 |
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 1544 | uint32_t availableInstanceExtensionCount = 0;
|
| 1545 | ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, nullptr) );
|
| 1546 | std::vector<VkExtensionProperties> availableInstanceExtensions(availableInstanceExtensionCount);
|
| 1547 | if(availableInstanceExtensionCount > 0)
|
| 1548 | {
|
| 1549 | ERR_GUARD_VULKAN( vkEnumerateInstanceExtensionProperties(nullptr, &availableInstanceExtensionCount, availableInstanceExtensions.data()) );
|
| 1550 | }
|
| 1551 |
|
| 1552 | std::vector<const char*> enabledInstanceExtensions;
|
| 1553 | enabledInstanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
| 1554 | enabledInstanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1555 |
|
| 1556 | std::vector<const char*> instanceLayers;
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 1557 | if(g_EnableValidationLayer)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1558 | {
|
| 1559 | instanceLayers.push_back(VALIDATION_LAYER_NAME);
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 1560 | }
|
| 1561 |
|
| 1562 | for(const auto& extensionProperties : availableInstanceExtensions)
|
| 1563 | {
|
| 1564 | if(strcmp(extensionProperties.extensionName, VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME) == 0)
|
| 1565 | {
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1566 | if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
|
| 1567 | {
|
| 1568 | enabledInstanceExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
|
| 1569 | VK_KHR_get_physical_device_properties2_enabled = true;
|
| 1570 | }
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 1571 | }
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 1572 | else if(strcmp(extensionProperties.extensionName, VK_EXT_DEBUG_UTILS_EXTENSION_NAME) == 0)
|
| 1573 | {
|
| 1574 | enabledInstanceExtensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);
|
| 1575 | VK_EXT_debug_utils_enabled = true;
|
| 1576 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1577 | }
|
| 1578 |
|
| 1579 | VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
|
| 1580 | appInfo.pApplicationName = APP_TITLE_A;
|
| 1581 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
| 1582 | appInfo.pEngineName = "Adam Sawicki Engine";
|
| 1583 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1584 | appInfo.apiVersion = GetVulkanApiVersion();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1585 |
|
| 1586 | VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
|
| 1587 | instInfo.pApplicationInfo = &appInfo;
|
Adam Sawicki | 353e367 | 2019-11-02 14:12:05 +0100 | [diff] [blame] | 1588 | instInfo.enabledExtensionCount = static_cast<uint32_t>(enabledInstanceExtensions.size());
|
| 1589 | instInfo.ppEnabledExtensionNames = enabledInstanceExtensions.data();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1590 | instInfo.enabledLayerCount = static_cast<uint32_t>(instanceLayers.size());
|
| 1591 | instInfo.ppEnabledLayerNames = instanceLayers.data();
|
| 1592 |
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1593 | wprintf(L"Vulkan API version used: ");
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1594 | switch(appInfo.apiVersion)
|
| 1595 | {
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1596 | case VK_API_VERSION_1_0: wprintf(L"1.0\n"); break;
|
| 1597 | case VK_API_VERSION_1_1: wprintf(L"1.1\n"); break;
|
| 1598 | case VK_API_VERSION_1_2: wprintf(L"1.2\n"); break;
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1599 | default: assert(0);
|
| 1600 | }
|
| 1601 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1602 | ERR_GUARD_VULKAN( vkCreateInstance(&instInfo, g_Allocs, &g_hVulkanInstance) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1603 |
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 1604 | if(VK_EXT_debug_utils_enabled)
|
| 1605 | {
|
| 1606 | RegisterDebugCallbacks();
|
| 1607 | }
|
| 1608 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1609 | // Create VkSurfaceKHR.
|
| 1610 | VkWin32SurfaceCreateInfoKHR surfaceInfo = { VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR };
|
| 1611 | surfaceInfo.hinstance = g_hAppInstance;
|
| 1612 | surfaceInfo.hwnd = g_hWnd;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1613 | VkResult result = vkCreateWin32SurfaceKHR(g_hVulkanInstance, &surfaceInfo, g_Allocs, &g_hSurface);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1614 | assert(result == VK_SUCCESS);
|
| 1615 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1616 | // Find physical device
|
| 1617 |
|
| 1618 | uint32_t deviceCount = 0;
|
| 1619 | ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, nullptr) );
|
| 1620 | assert(deviceCount > 0);
|
| 1621 |
|
| 1622 | std::vector<VkPhysicalDevice> physicalDevices(deviceCount);
|
| 1623 | ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, physicalDevices.data()) );
|
| 1624 |
|
| 1625 | g_hPhysicalDevice = physicalDevices[0];
|
| 1626 |
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1627 | // Query for device extensions
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1628 |
|
| 1629 | uint32_t physicalDeviceExtensionPropertyCount = 0;
|
| 1630 | ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties(g_hPhysicalDevice, nullptr, &physicalDeviceExtensionPropertyCount, nullptr) );
|
| 1631 | std::vector<VkExtensionProperties> physicalDeviceExtensionProperties{physicalDeviceExtensionPropertyCount};
|
| 1632 | if(physicalDeviceExtensionPropertyCount)
|
| 1633 | {
|
| 1634 | ERR_GUARD_VULKAN( vkEnumerateDeviceExtensionProperties(
|
| 1635 | g_hPhysicalDevice,
|
| 1636 | nullptr,
|
| 1637 | &physicalDeviceExtensionPropertyCount,
|
| 1638 | physicalDeviceExtensionProperties.data()) );
|
| 1639 | }
|
| 1640 |
|
| 1641 | for(uint32_t i = 0; i < physicalDeviceExtensionPropertyCount; ++i)
|
| 1642 | {
|
| 1643 | if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME) == 0)
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1644 | {
|
| 1645 | if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
|
| 1646 | {
|
| 1647 | VK_KHR_get_memory_requirements2_enabled = true;
|
| 1648 | }
|
| 1649 | }
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1650 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME) == 0)
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1651 | {
|
| 1652 | if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
|
| 1653 | {
|
| 1654 | VK_KHR_dedicated_allocation_enabled = true;
|
| 1655 | }
|
| 1656 | }
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1657 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_BIND_MEMORY_2_EXTENSION_NAME) == 0)
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1658 | {
|
| 1659 | if(GetVulkanApiVersion() == VK_API_VERSION_1_0)
|
| 1660 | {
|
| 1661 | VK_KHR_bind_memory2_enabled = true;
|
| 1662 | }
|
| 1663 | }
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1664 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_MEMORY_BUDGET_EXTENSION_NAME) == 0)
|
| 1665 | VK_EXT_memory_budget_enabled = true;
|
| 1666 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME) == 0)
|
| 1667 | VK_AMD_device_coherent_memory_enabled = true;
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1668 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME) == 0)
|
| 1669 | {
|
| 1670 | if(GetVulkanApiVersion() < VK_API_VERSION_1_2)
|
| 1671 | {
|
| 1672 | VK_KHR_buffer_device_address_enabled = true;
|
| 1673 | }
|
| 1674 | }
|
| 1675 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME) == 0)
|
| 1676 | {
|
| 1677 | if(GetVulkanApiVersion() < VK_API_VERSION_1_2)
|
| 1678 | {
|
| 1679 | VK_EXT_buffer_device_address_enabled = true;
|
| 1680 | }
|
| 1681 | }
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1682 | else if(strcmp(physicalDeviceExtensionProperties[i].extensionName, VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME) == 0)
|
| 1683 | VK_EXT_memory_priority_enabled = true;
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1684 | }
|
| 1685 |
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1686 | if(VK_EXT_buffer_device_address_enabled && VK_KHR_buffer_device_address_enabled)
|
| 1687 | VK_EXT_buffer_device_address_enabled = false;
|
| 1688 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1689 | // Query for features
|
| 1690 |
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1691 | #if VMA_VULKAN_VERSION >= 1001000
|
| 1692 | VkPhysicalDeviceProperties2 physicalDeviceProperties2 = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2 };
|
| 1693 |
|
| 1694 | #if VMA_VULKAN_VERSION >= 1002000
|
| 1695 | // Vulkan spec says structure VkPhysicalDeviceVulkan11Properties is "Provided by VK_VERSION_1_2" - is this a mistake? Assuming not...
|
| 1696 | VkPhysicalDeviceVulkan11Properties physicalDeviceVulkan11Properties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_1_PROPERTIES };
|
| 1697 | VkPhysicalDeviceVulkan12Properties physicalDeviceVulkan12Properties = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VULKAN_1_2_PROPERTIES };
|
| 1698 | PnextChainPushFront(&physicalDeviceProperties2, &physicalDeviceVulkan11Properties);
|
| 1699 | PnextChainPushFront(&physicalDeviceProperties2, &physicalDeviceVulkan12Properties);
|
| 1700 | #endif
|
| 1701 |
|
| 1702 | vkGetPhysicalDeviceProperties2(g_hPhysicalDevice, &physicalDeviceProperties2);
|
| 1703 |
|
| 1704 | PrintPhysicalDeviceProperties(physicalDeviceProperties2.properties);
|
| 1705 | #if VMA_VULKAN_VERSION >= 1002000
|
| 1706 | PrintPhysicalDeviceVulkan11Properties(physicalDeviceVulkan11Properties);
|
| 1707 | PrintPhysicalDeviceVulkan12Properties(physicalDeviceVulkan12Properties);
|
| 1708 | #endif
|
| 1709 |
|
| 1710 | #else // #if VMA_VULKAN_VERSION >= 1001000
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1711 | VkPhysicalDeviceProperties physicalDeviceProperties = {};
|
| 1712 | vkGetPhysicalDeviceProperties(g_hPhysicalDevice, &physicalDeviceProperties);
|
Adam Sawicki | e8a8544 | 2020-03-06 14:48:30 +0100 | [diff] [blame] | 1713 | PrintPhysicalDeviceProperties(physicalDeviceProperties);
|
| 1714 |
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1715 | #endif // #if VMA_VULKAN_VERSION >= 1001000
|
| 1716 |
|
| 1717 | wprintf(L"\n");
|
| 1718 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1719 | VkPhysicalDeviceFeatures2 physicalDeviceFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 };
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1720 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1721 | VkPhysicalDeviceCoherentMemoryFeaturesAMD physicalDeviceCoherentMemoryFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_COHERENT_MEMORY_FEATURES_AMD };
|
| 1722 | if(VK_AMD_device_coherent_memory_enabled)
|
| 1723 | {
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1724 | PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceCoherentMemoryFeatures);
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1725 | }
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1726 |
|
| 1727 | VkPhysicalDeviceBufferDeviceAddressFeaturesEXT physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT };
|
| 1728 | if(VK_KHR_buffer_device_address_enabled || VK_EXT_buffer_device_address_enabled || GetVulkanApiVersion() >= VK_API_VERSION_1_2)
|
| 1729 | {
|
| 1730 | PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceBufferDeviceAddressFeatures);
|
| 1731 | }
|
| 1732 |
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1733 | VkPhysicalDeviceMemoryPriorityFeaturesEXT physicalDeviceMemoryPriorityFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_MEMORY_PRIORITY_FEATURES_EXT };
|
| 1734 | if(VK_EXT_memory_priority_enabled)
|
| 1735 | {
|
| 1736 | PnextChainPushFront(&physicalDeviceFeatures, &physicalDeviceMemoryPriorityFeatures);
|
| 1737 | }
|
| 1738 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1739 | vkGetPhysicalDeviceFeatures2(g_hPhysicalDevice, &physicalDeviceFeatures);
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1740 |
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1741 | g_SparseBindingEnabled = physicalDeviceFeatures.features.sparseBinding != 0;
|
| 1742 |
|
| 1743 | // The extension is supported as fake with no real support for this feature? Don't use it.
|
| 1744 | if(VK_AMD_device_coherent_memory_enabled && !physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory)
|
| 1745 | VK_AMD_device_coherent_memory_enabled = false;
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1746 | if(VK_KHR_buffer_device_address_enabled || VK_EXT_buffer_device_address_enabled || GetVulkanApiVersion() >= VK_API_VERSION_1_2)
|
| 1747 | g_BufferDeviceAddressEnabled = physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress != VK_FALSE;
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1748 | if(VK_EXT_memory_priority_enabled && !physicalDeviceMemoryPriorityFeatures.memoryPriority)
|
| 1749 | VK_EXT_memory_priority_enabled = false;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1750 |
|
| 1751 | // Find queue family index
|
| 1752 |
|
| 1753 | uint32_t queueFamilyCount = 0;
|
| 1754 | vkGetPhysicalDeviceQueueFamilyProperties(g_hPhysicalDevice, &queueFamilyCount, nullptr);
|
| 1755 | assert(queueFamilyCount > 0);
|
| 1756 | std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
| 1757 | vkGetPhysicalDeviceQueueFamilyProperties(g_hPhysicalDevice, &queueFamilyCount, queueFamilies.data());
|
| 1758 | for(uint32_t i = 0;
|
| 1759 | (i < queueFamilyCount) &&
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1760 | (g_GraphicsQueueFamilyIndex == UINT_MAX ||
|
| 1761 | g_PresentQueueFamilyIndex == UINT_MAX ||
|
| 1762 | (g_SparseBindingEnabled && g_SparseBindingQueueFamilyIndex == UINT_MAX));
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1763 | ++i)
|
| 1764 | {
|
| 1765 | if(queueFamilies[i].queueCount > 0)
|
| 1766 | {
|
Adam Sawicki | da6c194 | 2018-12-05 17:34:34 +0100 | [diff] [blame] | 1767 | const uint32_t flagsForGraphicsQueue = VK_QUEUE_GRAPHICS_BIT | VK_QUEUE_COMPUTE_BIT;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1768 | if((g_GraphicsQueueFamilyIndex != 0) &&
|
Adam Sawicki | da6c194 | 2018-12-05 17:34:34 +0100 | [diff] [blame] | 1769 | ((queueFamilies[i].queueFlags & flagsForGraphicsQueue) == flagsForGraphicsQueue))
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1770 | {
|
| 1771 | g_GraphicsQueueFamilyIndex = i;
|
| 1772 | }
|
| 1773 |
|
| 1774 | VkBool32 surfaceSupported = 0;
|
| 1775 | VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(g_hPhysicalDevice, i, g_hSurface, &surfaceSupported);
|
| 1776 | if((res >= 0) && (surfaceSupported == VK_TRUE))
|
| 1777 | {
|
| 1778 | g_PresentQueueFamilyIndex = i;
|
| 1779 | }
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1780 |
|
| 1781 | if(g_SparseBindingEnabled &&
|
| 1782 | g_SparseBindingQueueFamilyIndex == UINT32_MAX &&
|
| 1783 | (queueFamilies[i].queueFlags & VK_QUEUE_SPARSE_BINDING_BIT) != 0)
|
| 1784 | {
|
| 1785 | g_SparseBindingQueueFamilyIndex = i;
|
| 1786 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1787 | }
|
| 1788 | }
|
| 1789 | assert(g_GraphicsQueueFamilyIndex != UINT_MAX);
|
| 1790 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1791 | g_SparseBindingEnabled = g_SparseBindingEnabled && g_SparseBindingQueueFamilyIndex != UINT32_MAX;
|
| 1792 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1793 | // Create logical device
|
| 1794 |
|
| 1795 | const float queuePriority = 1.f;
|
| 1796 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1797 | VkDeviceQueueCreateInfo queueCreateInfo[3] = {};
|
| 1798 | uint32_t queueCount = 1;
|
| 1799 | queueCreateInfo[0].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
| 1800 | queueCreateInfo[0].queueFamilyIndex = g_GraphicsQueueFamilyIndex;
|
| 1801 | queueCreateInfo[0].queueCount = 1;
|
| 1802 | queueCreateInfo[0].pQueuePriorities = &queuePriority;
|
| 1803 |
|
| 1804 | if(g_PresentQueueFamilyIndex != g_GraphicsQueueFamilyIndex)
|
| 1805 | {
|
| 1806 |
|
| 1807 | queueCreateInfo[queueCount].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
| 1808 | queueCreateInfo[queueCount].queueFamilyIndex = g_PresentQueueFamilyIndex;
|
| 1809 | queueCreateInfo[queueCount].queueCount = 1;
|
| 1810 | queueCreateInfo[queueCount].pQueuePriorities = &queuePriority;
|
| 1811 | ++queueCount;
|
| 1812 | }
|
| 1813 |
|
| 1814 | if(g_SparseBindingEnabled &&
|
| 1815 | g_SparseBindingQueueFamilyIndex != g_GraphicsQueueFamilyIndex &&
|
| 1816 | g_SparseBindingQueueFamilyIndex != g_PresentQueueFamilyIndex)
|
| 1817 | {
|
| 1818 |
|
| 1819 | queueCreateInfo[queueCount].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
| 1820 | queueCreateInfo[queueCount].queueFamilyIndex = g_SparseBindingQueueFamilyIndex;
|
| 1821 | queueCreateInfo[queueCount].queueCount = 1;
|
| 1822 | queueCreateInfo[queueCount].pQueuePriorities = &queuePriority;
|
| 1823 | ++queueCount;
|
| 1824 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1825 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1826 | std::vector<const char*> enabledDeviceExtensions;
|
| 1827 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1828 | if(VK_KHR_get_memory_requirements2_enabled)
|
| 1829 | enabledDeviceExtensions.push_back(VK_KHR_GET_MEMORY_REQUIREMENTS_2_EXTENSION_NAME);
|
| 1830 | if(VK_KHR_dedicated_allocation_enabled)
|
| 1831 | enabledDeviceExtensions.push_back(VK_KHR_DEDICATED_ALLOCATION_EXTENSION_NAME);
|
| 1832 | if(VK_KHR_bind_memory2_enabled)
|
| 1833 | enabledDeviceExtensions.push_back(VK_KHR_BIND_MEMORY_2_EXTENSION_NAME);
|
| 1834 | if(VK_EXT_memory_budget_enabled)
|
| 1835 | enabledDeviceExtensions.push_back(VK_EXT_MEMORY_BUDGET_EXTENSION_NAME);
|
| 1836 | if(VK_AMD_device_coherent_memory_enabled)
|
| 1837 | enabledDeviceExtensions.push_back(VK_AMD_DEVICE_COHERENT_MEMORY_EXTENSION_NAME);
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1838 | if(VK_KHR_buffer_device_address_enabled)
|
| 1839 | enabledDeviceExtensions.push_back(VK_KHR_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
|
| 1840 | if(VK_EXT_buffer_device_address_enabled)
|
| 1841 | enabledDeviceExtensions.push_back(VK_EXT_BUFFER_DEVICE_ADDRESS_EXTENSION_NAME);
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1842 | if(VK_EXT_memory_priority_enabled)
|
| 1843 | enabledDeviceExtensions.push_back(VK_EXT_MEMORY_PRIORITY_EXTENSION_NAME);
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1844 |
|
| 1845 | VkPhysicalDeviceFeatures2 deviceFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_FEATURES_2 };
|
| 1846 | deviceFeatures.features.samplerAnisotropy = VK_TRUE;
|
| 1847 | deviceFeatures.features.sparseBinding = g_SparseBindingEnabled ? VK_TRUE : VK_FALSE;
|
| 1848 |
|
| 1849 | if(VK_AMD_device_coherent_memory_enabled)
|
Adam Sawicki | 6cc5e85 | 2018-03-13 16:37:54 +0100 | [diff] [blame] | 1850 | {
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1851 | physicalDeviceCoherentMemoryFeatures.deviceCoherentMemory = VK_TRUE;
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1852 | PnextChainPushBack(&deviceFeatures, &physicalDeviceCoherentMemoryFeatures);
|
| 1853 | }
|
| 1854 | if(g_BufferDeviceAddressEnabled)
|
| 1855 | {
|
| 1856 | physicalDeviceBufferDeviceAddressFeatures = { VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_BUFFER_DEVICE_ADDRESS_FEATURES_EXT };
|
| 1857 | physicalDeviceBufferDeviceAddressFeatures.bufferDeviceAddress = VK_TRUE;
|
| 1858 | PnextChainPushBack(&deviceFeatures, &physicalDeviceBufferDeviceAddressFeatures);
|
Adam Sawicki | 6cc5e85 | 2018-03-13 16:37:54 +0100 | [diff] [blame] | 1859 | }
|
Adam Sawicki | f201205 | 2021-01-11 18:04:42 +0100 | [diff] [blame] | 1860 | if(VK_EXT_memory_priority_enabled)
|
| 1861 | {
|
| 1862 | PnextChainPushBack(&deviceFeatures, &physicalDeviceMemoryPriorityFeatures);
|
| 1863 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1864 |
|
| 1865 | VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1866 | deviceCreateInfo.pNext = &deviceFeatures;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1867 | deviceCreateInfo.enabledLayerCount = 0;
|
| 1868 | deviceCreateInfo.ppEnabledLayerNames = nullptr;
|
| 1869 | deviceCreateInfo.enabledExtensionCount = (uint32_t)enabledDeviceExtensions.size();
|
Adam Sawicki | 6cc5e85 | 2018-03-13 16:37:54 +0100 | [diff] [blame] | 1870 | deviceCreateInfo.ppEnabledExtensionNames = !enabledDeviceExtensions.empty() ? enabledDeviceExtensions.data() : nullptr;
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1871 | deviceCreateInfo.queueCreateInfoCount = queueCount;
|
| 1872 | deviceCreateInfo.pQueueCreateInfos = queueCreateInfo;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1873 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1874 | ERR_GUARD_VULKAN( vkCreateDevice(g_hPhysicalDevice, &deviceCreateInfo, g_Allocs, &g_hDevice) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1875 |
|
Adam Sawicki | e73e988 | 2020-03-20 18:05:42 +0100 | [diff] [blame] | 1876 | // Fetch pointers to extension functions
|
| 1877 | if(g_BufferDeviceAddressEnabled)
|
| 1878 | {
|
| 1879 | if(GetVulkanApiVersion() >= VK_API_VERSION_1_2)
|
| 1880 | {
|
| 1881 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddress");
|
| 1882 | //assert(g_vkGetBufferDeviceAddressEXT != nullptr);
|
| 1883 | /*
|
| 1884 | For some reason this doesn't work, the pointer is NULL :( None of the below methods help.
|
| 1885 |
|
| 1886 | Validation layers also report following error:
|
| 1887 | [ VUID-VkMemoryAllocateInfo-flags-03331 ] Object: VK_NULL_HANDLE (Type = 0) | If VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT_KHR is set, bufferDeviceAddress must be enabled. The Vulkan spec states: If VkMemoryAllocateFlagsInfo::flags includes VK_MEMORY_ALLOCATE_DEVICE_ADDRESS_BIT, the bufferDeviceAddress feature must be enabled (https://www.khronos.org/registry/vulkan/specs/1.1-extensions/html/vkspec.html#VUID-VkMemoryAllocateInfo-flags-03331)
|
| 1888 | Despite I'm posting VkPhysicalDeviceBufferDeviceAddressFeaturesEXT::bufferDeviceAddress = VK_TRUE in VkDeviceCreateInfo::pNext chain.
|
| 1889 |
|
| 1890 | if(g_vkGetBufferDeviceAddressEXT == nullptr)
|
| 1891 | {
|
| 1892 | g_vkGetBufferDeviceAddressEXT = &vkGetBufferDeviceAddress; // Doesn't run, cannot find entry point...
|
| 1893 | }
|
| 1894 |
|
| 1895 | if(g_vkGetBufferDeviceAddressEXT == nullptr)
|
| 1896 | {
|
| 1897 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetInstanceProcAddr(g_hVulkanInstance, "vkGetBufferDeviceAddress");
|
| 1898 | }
|
| 1899 | if(g_vkGetBufferDeviceAddressEXT == nullptr)
|
| 1900 | {
|
| 1901 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR");
|
| 1902 | }
|
| 1903 | if(g_vkGetBufferDeviceAddressEXT == nullptr)
|
| 1904 | {
|
| 1905 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressEXT");
|
| 1906 | }
|
| 1907 | */
|
| 1908 | }
|
| 1909 | else if(VK_KHR_buffer_device_address_enabled)
|
| 1910 | {
|
| 1911 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressKHR");
|
| 1912 | assert(g_vkGetBufferDeviceAddressEXT != nullptr);
|
| 1913 | }
|
| 1914 | else if(VK_EXT_buffer_device_address_enabled)
|
| 1915 | {
|
| 1916 | g_vkGetBufferDeviceAddressEXT = (PFN_vkGetBufferDeviceAddressEXT)vkGetDeviceProcAddr(g_hDevice, "vkGetBufferDeviceAddressEXT");
|
| 1917 | assert(g_vkGetBufferDeviceAddressEXT != nullptr);
|
| 1918 | }
|
| 1919 | }
|
| 1920 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1921 | // Create memory allocator
|
| 1922 |
|
| 1923 | VmaAllocatorCreateInfo allocatorInfo = {};
|
Adam Sawicki | 5088250 | 2020-02-07 16:51:31 +0100 | [diff] [blame] | 1924 | SetAllocatorCreateInfo(allocatorInfo);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1925 | ERR_GUARD_VULKAN( vmaCreateAllocator(&allocatorInfo, &g_hAllocator) );
|
| 1926 |
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1927 | PrintMemoryTypes();
|
| 1928 | wprintf(L"\n");
|
| 1929 | PrintMemoryConclusions();
|
| 1930 | wprintf(L"\n");
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1931 | PrintEnabledFeatures();
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 1932 | wprintf(L"\n");
|
Adam Sawicki | a4f2eb9 | 2020-03-06 14:39:42 +0100 | [diff] [blame] | 1933 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1934 | // Retrieve queues (don't need to be destroyed).
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1935 |
|
| 1936 | vkGetDeviceQueue(g_hDevice, g_GraphicsQueueFamilyIndex, 0, &g_hGraphicsQueue);
|
| 1937 | vkGetDeviceQueue(g_hDevice, g_PresentQueueFamilyIndex, 0, &g_hPresentQueue);
|
| 1938 | assert(g_hGraphicsQueue);
|
| 1939 | assert(g_hPresentQueue);
|
| 1940 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1941 | if(g_SparseBindingEnabled)
|
| 1942 | {
|
| 1943 | vkGetDeviceQueue(g_hDevice, g_SparseBindingQueueFamilyIndex, 0, &g_hSparseBindingQueue);
|
| 1944 | assert(g_hSparseBindingQueue);
|
| 1945 | }
|
| 1946 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1947 | // Create command pool
|
| 1948 |
|
| 1949 | VkCommandPoolCreateInfo commandPoolInfo = { VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO };
|
| 1950 | commandPoolInfo.queueFamilyIndex = g_GraphicsQueueFamilyIndex;
|
| 1951 | commandPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1952 | ERR_GUARD_VULKAN( vkCreateCommandPool(g_hDevice, &commandPoolInfo, g_Allocs, &g_hCommandPool) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1953 |
|
| 1954 | VkCommandBufferAllocateInfo commandBufferInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO };
|
| 1955 | commandBufferInfo.commandPool = g_hCommandPool;
|
| 1956 | commandBufferInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
| 1957 | commandBufferInfo.commandBufferCount = COMMAND_BUFFER_COUNT;
|
| 1958 | ERR_GUARD_VULKAN( vkAllocateCommandBuffers(g_hDevice, &commandBufferInfo, g_MainCommandBuffers) );
|
| 1959 |
|
| 1960 | VkFenceCreateInfo fenceInfo = { VK_STRUCTURE_TYPE_FENCE_CREATE_INFO };
|
| 1961 | fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;
|
| 1962 | for(size_t i = 0; i < COMMAND_BUFFER_COUNT; ++i)
|
| 1963 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1964 | ERR_GUARD_VULKAN( vkCreateFence(g_hDevice, &fenceInfo, g_Allocs, &g_MainCommandBufferExecutedFances[i]) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1965 | }
|
| 1966 |
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1967 | ERR_GUARD_VULKAN( vkCreateFence(g_hDevice, &fenceInfo, g_Allocs, &g_ImmediateFence) );
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 1968 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1969 | commandBufferInfo.commandBufferCount = 1;
|
| 1970 | ERR_GUARD_VULKAN( vkAllocateCommandBuffers(g_hDevice, &commandBufferInfo, &g_hTemporaryCommandBuffer) );
|
| 1971 |
|
| 1972 | // Create texture sampler
|
| 1973 |
|
| 1974 | VkSamplerCreateInfo samplerInfo = { VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO };
|
| 1975 | samplerInfo.magFilter = VK_FILTER_LINEAR;
|
| 1976 | samplerInfo.minFilter = VK_FILTER_LINEAR;
|
| 1977 | samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
| 1978 | samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
| 1979 | samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_REPEAT;
|
| 1980 | samplerInfo.anisotropyEnable = VK_TRUE;
|
| 1981 | samplerInfo.maxAnisotropy = 16;
|
| 1982 | samplerInfo.borderColor = VK_BORDER_COLOR_INT_OPAQUE_BLACK;
|
| 1983 | samplerInfo.unnormalizedCoordinates = VK_FALSE;
|
| 1984 | samplerInfo.compareEnable = VK_FALSE;
|
| 1985 | samplerInfo.compareOp = VK_COMPARE_OP_ALWAYS;
|
| 1986 | samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
| 1987 | samplerInfo.mipLodBias = 0.f;
|
| 1988 | samplerInfo.minLod = 0.f;
|
| 1989 | samplerInfo.maxLod = FLT_MAX;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 1990 | ERR_GUARD_VULKAN( vkCreateSampler(g_hDevice, &samplerInfo, g_Allocs, &g_hSampler) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1991 |
|
| 1992 | CreateTexture(128, 128);
|
| 1993 | CreateMesh();
|
| 1994 |
|
| 1995 | VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
|
| 1996 | samplerLayoutBinding.binding = 1;
|
| 1997 | samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
| 1998 | samplerLayoutBinding.descriptorCount = 1;
|
| 1999 | samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
|
| 2000 |
|
| 2001 | VkDescriptorSetLayoutCreateInfo descriptorSetLayoutInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO };
|
| 2002 | descriptorSetLayoutInfo.bindingCount = 1;
|
| 2003 | descriptorSetLayoutInfo.pBindings = &samplerLayoutBinding;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2004 | ERR_GUARD_VULKAN( vkCreateDescriptorSetLayout(g_hDevice, &descriptorSetLayoutInfo, g_Allocs, &g_hDescriptorSetLayout) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2005 |
|
| 2006 | // Create descriptor pool
|
| 2007 |
|
| 2008 | VkDescriptorPoolSize descriptorPoolSizes[2];
|
| 2009 | ZeroMemory(descriptorPoolSizes, sizeof(descriptorPoolSizes));
|
| 2010 | descriptorPoolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
|
| 2011 | descriptorPoolSizes[0].descriptorCount = 1;
|
| 2012 | descriptorPoolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
| 2013 | descriptorPoolSizes[1].descriptorCount = 1;
|
| 2014 |
|
| 2015 | VkDescriptorPoolCreateInfo descriptorPoolInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO };
|
| 2016 | descriptorPoolInfo.poolSizeCount = (uint32_t)_countof(descriptorPoolSizes);
|
| 2017 | descriptorPoolInfo.pPoolSizes = descriptorPoolSizes;
|
| 2018 | descriptorPoolInfo.maxSets = 1;
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2019 | ERR_GUARD_VULKAN( vkCreateDescriptorPool(g_hDevice, &descriptorPoolInfo, g_Allocs, &g_hDescriptorPool) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2020 |
|
| 2021 | // Create descriptor set layout
|
| 2022 |
|
| 2023 | VkDescriptorSetLayout descriptorSetLayouts[] = { g_hDescriptorSetLayout };
|
| 2024 | VkDescriptorSetAllocateInfo descriptorSetInfo = { VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO };
|
| 2025 | descriptorSetInfo.descriptorPool = g_hDescriptorPool;
|
| 2026 | descriptorSetInfo.descriptorSetCount = 1;
|
| 2027 | descriptorSetInfo.pSetLayouts = descriptorSetLayouts;
|
| 2028 | ERR_GUARD_VULKAN( vkAllocateDescriptorSets(g_hDevice, &descriptorSetInfo, &g_hDescriptorSet) );
|
| 2029 |
|
| 2030 | VkDescriptorImageInfo descriptorImageInfo = {};
|
| 2031 | descriptorImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
| 2032 | descriptorImageInfo.imageView = g_hTextureImageView;
|
| 2033 | descriptorImageInfo.sampler = g_hSampler;
|
| 2034 |
|
| 2035 | VkWriteDescriptorSet writeDescriptorSet = { VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET };
|
| 2036 | writeDescriptorSet.dstSet = g_hDescriptorSet;
|
| 2037 | writeDescriptorSet.dstBinding = 1;
|
| 2038 | writeDescriptorSet.dstArrayElement = 0;
|
| 2039 | writeDescriptorSet.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
| 2040 | writeDescriptorSet.descriptorCount = 1;
|
| 2041 | writeDescriptorSet.pImageInfo = &descriptorImageInfo;
|
| 2042 |
|
| 2043 | vkUpdateDescriptorSets(g_hDevice, 1, &writeDescriptorSet, 0, nullptr);
|
| 2044 |
|
| 2045 | CreateSwapchain();
|
| 2046 | }
|
| 2047 |
|
| 2048 | static void FinalizeApplication()
|
| 2049 | {
|
| 2050 | vkDeviceWaitIdle(g_hDevice);
|
| 2051 |
|
| 2052 | DestroySwapchain(true);
|
| 2053 |
|
| 2054 | if(g_hDescriptorPool != VK_NULL_HANDLE)
|
| 2055 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2056 | vkDestroyDescriptorPool(g_hDevice, g_hDescriptorPool, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2057 | g_hDescriptorPool = VK_NULL_HANDLE;
|
| 2058 | }
|
| 2059 |
|
| 2060 | if(g_hDescriptorSetLayout != VK_NULL_HANDLE)
|
| 2061 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2062 | vkDestroyDescriptorSetLayout(g_hDevice, g_hDescriptorSetLayout, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2063 | g_hDescriptorSetLayout = VK_NULL_HANDLE;
|
| 2064 | }
|
| 2065 |
|
| 2066 | if(g_hTextureImageView != VK_NULL_HANDLE)
|
| 2067 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2068 | vkDestroyImageView(g_hDevice, g_hTextureImageView, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2069 | g_hTextureImageView = VK_NULL_HANDLE;
|
| 2070 | }
|
| 2071 | if(g_hTextureImage != VK_NULL_HANDLE)
|
| 2072 | {
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 2073 | vmaDestroyImage(g_hAllocator, g_hTextureImage, g_hTextureImageAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2074 | g_hTextureImage = VK_NULL_HANDLE;
|
| 2075 | }
|
| 2076 |
|
| 2077 | if(g_hIndexBuffer != VK_NULL_HANDLE)
|
| 2078 | {
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 2079 | vmaDestroyBuffer(g_hAllocator, g_hIndexBuffer, g_hIndexBufferAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2080 | g_hIndexBuffer = VK_NULL_HANDLE;
|
| 2081 | }
|
| 2082 | if(g_hVertexBuffer != VK_NULL_HANDLE)
|
| 2083 | {
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 2084 | vmaDestroyBuffer(g_hAllocator, g_hVertexBuffer, g_hVertexBufferAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2085 | g_hVertexBuffer = VK_NULL_HANDLE;
|
| 2086 | }
|
| 2087 |
|
| 2088 | if(g_hSampler != VK_NULL_HANDLE)
|
| 2089 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2090 | vkDestroySampler(g_hDevice, g_hSampler, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2091 | g_hSampler = VK_NULL_HANDLE;
|
| 2092 | }
|
| 2093 |
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 2094 | if(g_ImmediateFence)
|
| 2095 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2096 | vkDestroyFence(g_hDevice, g_ImmediateFence, g_Allocs);
|
Adam Sawicki | 51fa966 | 2018-10-03 13:44:29 +0200 | [diff] [blame] | 2097 | g_ImmediateFence = VK_NULL_HANDLE;
|
| 2098 | }
|
| 2099 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2100 | for(size_t i = COMMAND_BUFFER_COUNT; i--; )
|
| 2101 | {
|
| 2102 | if(g_MainCommandBufferExecutedFances[i] != VK_NULL_HANDLE)
|
| 2103 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2104 | vkDestroyFence(g_hDevice, g_MainCommandBufferExecutedFances[i], g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2105 | g_MainCommandBufferExecutedFances[i] = VK_NULL_HANDLE;
|
| 2106 | }
|
| 2107 | }
|
| 2108 | if(g_MainCommandBuffers[0] != VK_NULL_HANDLE)
|
| 2109 | {
|
| 2110 | vkFreeCommandBuffers(g_hDevice, g_hCommandPool, COMMAND_BUFFER_COUNT, g_MainCommandBuffers);
|
| 2111 | ZeroMemory(g_MainCommandBuffers, sizeof(g_MainCommandBuffers));
|
| 2112 | }
|
| 2113 | if(g_hTemporaryCommandBuffer != VK_NULL_HANDLE)
|
| 2114 | {
|
| 2115 | vkFreeCommandBuffers(g_hDevice, g_hCommandPool, 1, &g_hTemporaryCommandBuffer);
|
| 2116 | g_hTemporaryCommandBuffer = VK_NULL_HANDLE;
|
| 2117 | }
|
| 2118 |
|
| 2119 | if(g_hCommandPool != VK_NULL_HANDLE)
|
| 2120 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2121 | vkDestroyCommandPool(g_hDevice, g_hCommandPool, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2122 | g_hCommandPool = VK_NULL_HANDLE;
|
| 2123 | }
|
| 2124 |
|
| 2125 | if(g_hAllocator != VK_NULL_HANDLE)
|
| 2126 | {
|
| 2127 | vmaDestroyAllocator(g_hAllocator);
|
| 2128 | g_hAllocator = nullptr;
|
| 2129 | }
|
| 2130 |
|
| 2131 | if(g_hDevice != VK_NULL_HANDLE)
|
| 2132 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2133 | vkDestroyDevice(g_hDevice, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2134 | g_hDevice = nullptr;
|
| 2135 | }
|
| 2136 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2137 | if(g_hSurface != VK_NULL_HANDLE)
|
| 2138 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2139 | vkDestroySurfaceKHR(g_hVulkanInstance, g_hSurface, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2140 | g_hSurface = VK_NULL_HANDLE;
|
| 2141 | }
|
| 2142 |
|
Adam Sawicki | d594faa | 2020-07-15 17:12:28 +0200 | [diff] [blame] | 2143 | UnregisterDebugCallbacks();
|
Adam Sawicki | 8caf0bd | 2020-07-15 16:56:30 +0200 | [diff] [blame] | 2144 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2145 | if(g_hVulkanInstance != VK_NULL_HANDLE)
|
| 2146 | {
|
Adam Sawicki | 1f84f62 | 2019-07-02 13:40:01 +0200 | [diff] [blame] | 2147 | vkDestroyInstance(g_hVulkanInstance, g_Allocs);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2148 | g_hVulkanInstance = VK_NULL_HANDLE;
|
| 2149 | }
|
| 2150 | }
|
| 2151 |
|
| 2152 | static void PrintAllocatorStats()
|
| 2153 | {
|
| 2154 | #if VMA_STATS_STRING_ENABLED
|
| 2155 | char* statsString = nullptr;
|
| 2156 | vmaBuildStatsString(g_hAllocator, &statsString, true);
|
| 2157 | printf("%s\n", statsString);
|
| 2158 | vmaFreeStatsString(g_hAllocator, statsString);
|
| 2159 | #endif
|
| 2160 | }
|
| 2161 |
|
| 2162 | static void RecreateSwapChain()
|
| 2163 | {
|
| 2164 | vkDeviceWaitIdle(g_hDevice);
|
| 2165 | DestroySwapchain(false);
|
| 2166 | CreateSwapchain();
|
| 2167 | }
|
| 2168 |
|
| 2169 | static void DrawFrame()
|
| 2170 | {
|
| 2171 | // Begin main command buffer
|
| 2172 | size_t cmdBufIndex = (g_NextCommandBufferIndex++) % COMMAND_BUFFER_COUNT;
|
| 2173 | VkCommandBuffer hCommandBuffer = g_MainCommandBuffers[cmdBufIndex];
|
| 2174 | VkFence hCommandBufferExecutedFence = g_MainCommandBufferExecutedFances[cmdBufIndex];
|
| 2175 |
|
| 2176 | ERR_GUARD_VULKAN( vkWaitForFences(g_hDevice, 1, &hCommandBufferExecutedFence, VK_TRUE, UINT64_MAX) );
|
| 2177 | ERR_GUARD_VULKAN( vkResetFences(g_hDevice, 1, &hCommandBufferExecutedFence) );
|
| 2178 |
|
| 2179 | VkCommandBufferBeginInfo commandBufferBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
| 2180 | commandBufferBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
| 2181 | ERR_GUARD_VULKAN( vkBeginCommandBuffer(hCommandBuffer, &commandBufferBeginInfo) );
|
| 2182 |
|
| 2183 | // Acquire swapchain image
|
| 2184 | uint32_t imageIndex = 0;
|
| 2185 | VkResult res = vkAcquireNextImageKHR(g_hDevice, g_hSwapchain, UINT64_MAX, g_hImageAvailableSemaphore, VK_NULL_HANDLE, &imageIndex);
|
| 2186 | if(res == VK_ERROR_OUT_OF_DATE_KHR)
|
| 2187 | {
|
| 2188 | RecreateSwapChain();
|
| 2189 | return;
|
| 2190 | }
|
| 2191 | else if(res < 0)
|
| 2192 | {
|
| 2193 | ERR_GUARD_VULKAN(res);
|
| 2194 | }
|
| 2195 |
|
| 2196 | // Record geometry pass
|
| 2197 |
|
| 2198 | VkClearValue clearValues[2];
|
| 2199 | ZeroMemory(clearValues, sizeof(clearValues));
|
| 2200 | clearValues[0].color.float32[0] = 0.25f;
|
| 2201 | clearValues[0].color.float32[1] = 0.25f;
|
| 2202 | clearValues[0].color.float32[2] = 0.5f;
|
| 2203 | clearValues[0].color.float32[3] = 1.0f;
|
| 2204 | clearValues[1].depthStencil.depth = 1.0f;
|
| 2205 |
|
| 2206 | VkRenderPassBeginInfo renderPassBeginInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO };
|
| 2207 | renderPassBeginInfo.renderPass = g_hRenderPass;
|
| 2208 | renderPassBeginInfo.framebuffer = g_Framebuffers[imageIndex];
|
| 2209 | renderPassBeginInfo.renderArea.offset.x = 0;
|
| 2210 | renderPassBeginInfo.renderArea.offset.y = 0;
|
| 2211 | renderPassBeginInfo.renderArea.extent = g_Extent;
|
| 2212 | renderPassBeginInfo.clearValueCount = (uint32_t)_countof(clearValues);
|
| 2213 | renderPassBeginInfo.pClearValues = clearValues;
|
| 2214 | vkCmdBeginRenderPass(hCommandBuffer, &renderPassBeginInfo, VK_SUBPASS_CONTENTS_INLINE);
|
| 2215 |
|
| 2216 | vkCmdBindPipeline(
|
| 2217 | hCommandBuffer,
|
| 2218 | VK_PIPELINE_BIND_POINT_GRAPHICS,
|
| 2219 | g_hPipeline);
|
| 2220 |
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame] | 2221 | mat4 view = mat4::LookAt(
|
| 2222 | vec3(0.f, 0.f, 0.f),
|
| 2223 | vec3(0.f, -2.f, 4.f),
|
| 2224 | vec3(0.f, 1.f, 0.f));
|
| 2225 | mat4 proj = mat4::Perspective(
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2226 | 1.0471975511966f, // 60 degrees
|
| 2227 | (float)g_Extent.width / (float)g_Extent.height,
|
| 2228 | 0.1f,
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame] | 2229 | 1000.f);
|
| 2230 | mat4 viewProj = view * proj;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2231 |
|
| 2232 | vkCmdBindDescriptorSets(
|
| 2233 | hCommandBuffer,
|
| 2234 | VK_PIPELINE_BIND_POINT_GRAPHICS,
|
| 2235 | g_hPipelineLayout,
|
| 2236 | 0,
|
| 2237 | 1,
|
| 2238 | &g_hDescriptorSet,
|
| 2239 | 0,
|
| 2240 | nullptr);
|
| 2241 |
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame] | 2242 | float rotationAngle = (float)GetTickCount() * 0.001f * (float)PI * 0.2f;
|
| 2243 | mat4 model = mat4::RotationY(rotationAngle);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2244 |
|
| 2245 | UniformBufferObject ubo = {};
|
Adam Sawicki | 82c3f33 | 2018-06-11 15:27:33 +0200 | [diff] [blame] | 2246 | ubo.ModelViewProj = model * viewProj;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2247 | vkCmdPushConstants(hCommandBuffer, g_hPipelineLayout, VK_SHADER_STAGE_VERTEX_BIT, 0, sizeof(UniformBufferObject), &ubo);
|
| 2248 |
|
| 2249 | VkBuffer vertexBuffers[] = { g_hVertexBuffer };
|
| 2250 | VkDeviceSize offsets[] = { 0 };
|
| 2251 | vkCmdBindVertexBuffers(hCommandBuffer, 0, 1, vertexBuffers, offsets);
|
| 2252 |
|
| 2253 | vkCmdBindIndexBuffer(hCommandBuffer, g_hIndexBuffer, 0, VK_INDEX_TYPE_UINT16);
|
| 2254 |
|
| 2255 | vkCmdDrawIndexed(hCommandBuffer, g_IndexCount, 1, 0, 0, 0);
|
| 2256 |
|
| 2257 | vkCmdEndRenderPass(hCommandBuffer);
|
| 2258 |
|
| 2259 | vkEndCommandBuffer(hCommandBuffer);
|
| 2260 |
|
| 2261 | // Submit command buffer
|
| 2262 |
|
| 2263 | VkSemaphore submitWaitSemaphores[] = { g_hImageAvailableSemaphore };
|
| 2264 | VkPipelineStageFlags submitWaitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };
|
| 2265 | VkSemaphore submitSignalSemaphores[] = { g_hRenderFinishedSemaphore };
|
| 2266 | VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
|
| 2267 | submitInfo.waitSemaphoreCount = 1;
|
| 2268 | submitInfo.pWaitSemaphores = submitWaitSemaphores;
|
| 2269 | submitInfo.pWaitDstStageMask = submitWaitStages;
|
| 2270 | submitInfo.commandBufferCount = 1;
|
| 2271 | submitInfo.pCommandBuffers = &hCommandBuffer;
|
| 2272 | submitInfo.signalSemaphoreCount = _countof(submitSignalSemaphores);
|
| 2273 | submitInfo.pSignalSemaphores = submitSignalSemaphores;
|
| 2274 | ERR_GUARD_VULKAN( vkQueueSubmit(g_hGraphicsQueue, 1, &submitInfo, hCommandBufferExecutedFence) );
|
| 2275 |
|
| 2276 | VkSemaphore presentWaitSemaphores[] = { g_hRenderFinishedSemaphore };
|
| 2277 |
|
| 2278 | VkSwapchainKHR swapchains[] = { g_hSwapchain };
|
| 2279 | VkPresentInfoKHR presentInfo = { VK_STRUCTURE_TYPE_PRESENT_INFO_KHR };
|
| 2280 | presentInfo.waitSemaphoreCount = _countof(presentWaitSemaphores);
|
| 2281 | presentInfo.pWaitSemaphores = presentWaitSemaphores;
|
| 2282 | presentInfo.swapchainCount = 1;
|
| 2283 | presentInfo.pSwapchains = swapchains;
|
| 2284 | presentInfo.pImageIndices = &imageIndex;
|
| 2285 | presentInfo.pResults = nullptr;
|
| 2286 | res = vkQueuePresentKHR(g_hPresentQueue, &presentInfo);
|
| 2287 | if(res == VK_ERROR_OUT_OF_DATE_KHR)
|
| 2288 | {
|
| 2289 | RecreateSwapChain();
|
| 2290 | }
|
| 2291 | else
|
| 2292 | ERR_GUARD_VULKAN(res);
|
| 2293 | }
|
| 2294 |
|
| 2295 | static void HandlePossibleSizeChange()
|
| 2296 | {
|
| 2297 | RECT clientRect;
|
| 2298 | GetClientRect(g_hWnd, &clientRect);
|
| 2299 | LONG newSizeX = clientRect.right - clientRect.left;
|
| 2300 | LONG newSizeY = clientRect.bottom - clientRect.top;
|
| 2301 | if((newSizeX > 0) &&
|
| 2302 | (newSizeY > 0) &&
|
| 2303 | ((newSizeX != g_SizeX) || (newSizeY != g_SizeY)))
|
| 2304 | {
|
| 2305 | g_SizeX = newSizeX;
|
| 2306 | g_SizeY = newSizeY;
|
| 2307 |
|
| 2308 | RecreateSwapChain();
|
| 2309 | }
|
| 2310 | }
|
| 2311 |
|
| 2312 | static LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
| 2313 | {
|
| 2314 | switch(msg)
|
| 2315 | {
|
| 2316 | case WM_CREATE:
|
| 2317 | // This is intentionally assigned here because we are now inside CreateWindow, before it returns.
|
| 2318 | g_hWnd = hWnd;
|
| 2319 | InitializeApplication();
|
Adam Sawicki | afd5056 | 2021-02-26 11:59:58 +0100 | [diff] [blame] | 2320 | //PrintAllocatorStats();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2321 | return 0;
|
| 2322 |
|
| 2323 | case WM_DESTROY:
|
| 2324 | FinalizeApplication();
|
| 2325 | PostQuitMessage(0);
|
| 2326 | return 0;
|
| 2327 |
|
| 2328 | // This prevents app from freezing when left Alt is pressed
|
| 2329 | // (which normally enters modal menu loop).
|
| 2330 | case WM_SYSKEYDOWN:
|
| 2331 | case WM_SYSKEYUP:
|
| 2332 | return 0;
|
| 2333 |
|
| 2334 | case WM_SIZE:
|
| 2335 | if((wParam == SIZE_MAXIMIZED) || (wParam == SIZE_RESTORED))
|
| 2336 | HandlePossibleSizeChange();
|
| 2337 | return 0;
|
| 2338 |
|
| 2339 | case WM_EXITSIZEMOVE:
|
| 2340 | HandlePossibleSizeChange();
|
| 2341 | return 0;
|
| 2342 |
|
| 2343 | case WM_KEYDOWN:
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2344 | switch(wParam)
|
| 2345 | {
|
| 2346 | case VK_ESCAPE:
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2347 | PostMessage(hWnd, WM_CLOSE, 0, 0);
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2348 | break;
|
| 2349 | case 'T':
|
Adam Sawicki | a7d7769 | 2018-10-03 16:15:27 +0200 | [diff] [blame] | 2350 | try
|
| 2351 | {
|
| 2352 | Test();
|
| 2353 | }
|
| 2354 | catch(const std::exception& ex)
|
| 2355 | {
|
| 2356 | printf("ERROR: %s\n", ex.what());
|
| 2357 | }
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2358 | break;
|
Adam Sawicki | da6c194 | 2018-12-05 17:34:34 +0100 | [diff] [blame] | 2359 | case 'S':
|
| 2360 | try
|
| 2361 | {
|
| 2362 | if(g_SparseBindingEnabled)
|
| 2363 | {
|
| 2364 | TestSparseBinding();
|
| 2365 | }
|
| 2366 | else
|
| 2367 | {
|
| 2368 | printf("Sparse binding not supported.\n");
|
| 2369 | }
|
| 2370 | }
|
| 2371 | catch(const std::exception& ex)
|
| 2372 | {
|
| 2373 | printf("ERROR: %s\n", ex.what());
|
| 2374 | }
|
| 2375 | break;
|
Adam Sawicki | b8333fb | 2018-03-13 16:15:53 +0100 | [diff] [blame] | 2376 | }
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2377 | return 0;
|
| 2378 |
|
| 2379 | default:
|
| 2380 | break;
|
| 2381 | }
|
| 2382 |
|
| 2383 | return DefWindowProc(hWnd, msg, wParam, lParam);
|
| 2384 | }
|
| 2385 |
|
| 2386 | int main()
|
| 2387 | {
|
| 2388 | g_hAppInstance = (HINSTANCE)GetModuleHandle(NULL);
|
| 2389 |
|
| 2390 | WNDCLASSEX wndClassDesc = { sizeof(WNDCLASSEX) };
|
| 2391 | wndClassDesc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
|
| 2392 | wndClassDesc.hbrBackground = NULL;
|
| 2393 | wndClassDesc.hCursor = LoadCursor(NULL, IDC_CROSS);
|
| 2394 | wndClassDesc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
| 2395 | wndClassDesc.hInstance = g_hAppInstance;
|
| 2396 | wndClassDesc.lpfnWndProc = WndProc;
|
| 2397 | wndClassDesc.lpszClassName = WINDOW_CLASS_NAME;
|
| 2398 |
|
| 2399 | const ATOM hWndClass = RegisterClassEx(&wndClassDesc);
|
| 2400 | assert(hWndClass);
|
| 2401 |
|
| 2402 | const DWORD style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME;
|
| 2403 | const DWORD exStyle = 0;
|
| 2404 |
|
| 2405 | RECT rect = { 0, 0, g_SizeX, g_SizeY };
|
| 2406 | AdjustWindowRectEx(&rect, style, FALSE, exStyle);
|
| 2407 |
|
Adam Sawicki | 86ccd63 | 2017-07-04 14:57:53 +0200 | [diff] [blame] | 2408 | CreateWindowEx(
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2409 | exStyle, WINDOW_CLASS_NAME, APP_TITLE_W, style,
|
| 2410 | CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
| 2411 | NULL, NULL, g_hAppInstance, NULL);
|
| 2412 |
|
| 2413 | MSG msg;
|
| 2414 | for(;;)
|
| 2415 | {
|
| 2416 | if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
| 2417 | {
|
| 2418 | if(msg.message == WM_QUIT)
|
| 2419 | break;
|
| 2420 | TranslateMessage(&msg);
|
| 2421 | DispatchMessage(&msg);
|
| 2422 | }
|
| 2423 | if(g_hDevice != VK_NULL_HANDLE)
|
| 2424 | DrawFrame();
|
| 2425 | }
|
| 2426 |
|
Adam Sawicki | 8317ba9 | 2019-11-18 13:14:11 +0100 | [diff] [blame] | 2427 | TEST(g_CpuAllocCount.load() == 0);
|
| 2428 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 2429 | return 0;
|
| 2430 | }
|
Adam Sawicki | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 2431 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 2432 | #else // #ifdef _WIN32
|
Adam Sawicki | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 2433 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 2434 | #include "VmaUsage.h"
|
Adam Sawicki | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 2435 |
|
| 2436 | int main()
|
| 2437 | {
|
| 2438 | }
|
| 2439 |
|
Adam Sawicki | f1a793c | 2018-03-13 15:42:22 +0100 | [diff] [blame] | 2440 | #endif // #ifdef _WIN32
|