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