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