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