Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1 | //
|
| 2 | // Copyright (c) 2017 Advanced Micro Devices, Inc. All rights reserved.
|
| 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 | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 23 | #ifdef WIN32
|
| 24 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 25 | #define NOMINMAX
|
| 26 | #define WIN32_LEAN_AND_MEAN
|
| 27 | #include <Windows.h>
|
| 28 |
|
| 29 | #define VK_USE_PLATFORM_WIN32_KHR
|
| 30 | #include <vulkan/vulkan.h>
|
| 31 |
|
Adam Sawicki | 86ccd63 | 2017-07-04 14:57:53 +0200 | [diff] [blame] | 32 | #pragma warning(push, 4)
|
| 33 | #pragma warning(disable: 4127) // warning C4127: conditional expression is constant
|
Adam Sawicki | 0851377 | 2017-07-13 16:14:04 +0200 | [diff] [blame] | 34 | #pragma warning(disable: 4100) // warning C4100: '...': unreferenced formal parameter
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 35 | #pragma warning(disable: 4189) // warning C4189: '...': local variable is initialized but not referenced
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 36 | #define VMA_IMPLEMENTATION
|
| 37 | #include "vk_mem_alloc.h"
|
Adam Sawicki | 86ccd63 | 2017-07-04 14:57:53 +0200 | [diff] [blame] | 38 | #pragma warning(pop)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 39 |
|
| 40 | #define MATHFU_COMPILE_WITHOUT_SIMD_SUPPORT
|
| 41 | #include <mathfu/glsl_mappings.h>
|
| 42 | #include <mathfu/constants.h>
|
| 43 |
|
| 44 | #include <fstream>
|
| 45 | #include <vector>
|
| 46 | #include <string>
|
| 47 | #include <memory>
|
| 48 | #include <algorithm>
|
| 49 | #include <numeric>
|
| 50 | #include <array>
|
| 51 | #include <type_traits>
|
| 52 | #include <utility>
|
| 53 |
|
| 54 | #include <cmath>
|
| 55 | #include <cassert>
|
| 56 | #include <cstdlib>
|
| 57 | #include <cstdio>
|
| 58 |
|
| 59 | #define ERR_GUARD_VULKAN(Expr) do { VkResult res__ = (Expr); if (res__ < 0) assert(0); } while(0)
|
| 60 |
|
| 61 | static const char* const SHADER_PATH1 = "./";
|
| 62 | static const char* const SHADER_PATH2 = "../bin/";
|
| 63 | static const wchar_t* const WINDOW_CLASS_NAME = L"VULKAN_MEMORY_ALLOCATOR_SAMPLE";
|
| 64 | static const char* const VALIDATION_LAYER_NAME = "VK_LAYER_LUNARG_standard_validation";
|
| 65 | static const char* const APP_TITLE_A = "Vulkan Memory Allocator Sample 1.0";
|
| 66 | static const wchar_t* const APP_TITLE_W = L"Vulkan Memory Allocator Sample 1.0";
|
| 67 |
|
| 68 | static const bool VSYNC = true;
|
| 69 | static const uint32_t COMMAND_BUFFER_COUNT = 2;
|
| 70 |
|
| 71 | static bool g_EnableValidationLayer = true;
|
| 72 |
|
| 73 | static HINSTANCE g_hAppInstance;
|
| 74 | static HWND g_hWnd;
|
| 75 | static LONG g_SizeX = 1280, g_SizeY = 720;
|
| 76 | static VkInstance g_hVulkanInstance;
|
| 77 | static VkSurfaceKHR g_hSurface;
|
| 78 | static VkPhysicalDevice g_hPhysicalDevice;
|
| 79 | static VkQueue g_hPresentQueue;
|
| 80 | static VkSurfaceFormatKHR g_SurfaceFormat;
|
| 81 | static VkExtent2D g_Extent;
|
| 82 | static VkSwapchainKHR g_hSwapchain;
|
| 83 | static std::vector<VkImage> g_SwapchainImages;
|
| 84 | static std::vector<VkImageView> g_SwapchainImageViews;
|
| 85 | static std::vector<VkFramebuffer> g_Framebuffers;
|
| 86 | static VkCommandPool g_hCommandPool;
|
| 87 | static VkCommandBuffer g_MainCommandBuffers[COMMAND_BUFFER_COUNT];
|
| 88 | static VkFence g_MainCommandBufferExecutedFances[COMMAND_BUFFER_COUNT];
|
| 89 | static uint32_t g_NextCommandBufferIndex;
|
| 90 | static VkSemaphore g_hImageAvailableSemaphore;
|
| 91 | static VkSemaphore g_hRenderFinishedSemaphore;
|
| 92 | static uint32_t g_GraphicsQueueFamilyIndex = UINT_MAX;
|
| 93 | static uint32_t g_PresentQueueFamilyIndex = UINT_MAX;
|
| 94 | static VkDescriptorSetLayout g_hDescriptorSetLayout;
|
| 95 | static VkDescriptorPool g_hDescriptorPool;
|
| 96 | static VkDescriptorSet g_hDescriptorSet; // Automatically destroyed with m_DescriptorPool.
|
| 97 | static VkSampler g_hSampler;
|
| 98 | static VkFormat g_DepthFormat;
|
| 99 | static VkImage g_hDepthImage;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 100 | static VmaAllocation g_hDepthImageAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 101 | static VkImageView g_hDepthImageView;
|
| 102 |
|
| 103 | static VkSurfaceCapabilitiesKHR g_SurfaceCapabilities;
|
| 104 | static std::vector<VkSurfaceFormatKHR> g_SurfaceFormats;
|
| 105 | static std::vector<VkPresentModeKHR> g_PresentModes;
|
| 106 |
|
| 107 | static PFN_vkCreateDebugReportCallbackEXT g_pvkCreateDebugReportCallbackEXT;
|
| 108 | static PFN_vkDebugReportMessageEXT g_pvkDebugReportMessageEXT;
|
| 109 | static PFN_vkDestroyDebugReportCallbackEXT g_pvkDestroyDebugReportCallbackEXT;
|
| 110 | static VkDebugReportCallbackEXT g_hCallback;
|
| 111 |
|
| 112 | static VkDevice g_hDevice;
|
| 113 | static VmaAllocator g_hAllocator;
|
| 114 | static VkQueue g_hGraphicsQueue;
|
| 115 | static VkCommandBuffer g_hTemporaryCommandBuffer;
|
| 116 |
|
| 117 | static VkPipelineLayout g_hPipelineLayout;
|
| 118 | static VkRenderPass g_hRenderPass;
|
| 119 | static VkPipeline g_hPipeline;
|
| 120 |
|
| 121 | static VkBuffer g_hVertexBuffer;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 122 | static VmaAllocation g_hVertexBufferAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 123 | static VkBuffer g_hIndexBuffer;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 124 | static VmaAllocation g_hIndexBufferAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 125 | static uint32_t g_VertexCount;
|
| 126 | static uint32_t g_IndexCount;
|
| 127 |
|
| 128 | static VkImage g_hTextureImage;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 129 | static VmaAllocation g_hTextureImageAlloc;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 130 | static VkImageView g_hTextureImageView;
|
| 131 |
|
| 132 | static void BeginSingleTimeCommands()
|
| 133 | {
|
| 134 | VkCommandBufferBeginInfo cmdBufBeginInfo = { VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO };
|
| 135 | cmdBufBeginInfo.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT;
|
| 136 | ERR_GUARD_VULKAN( vkBeginCommandBuffer(g_hTemporaryCommandBuffer, &cmdBufBeginInfo) );
|
| 137 | }
|
| 138 |
|
| 139 | static void EndSingleTimeCommands()
|
| 140 | {
|
| 141 | ERR_GUARD_VULKAN( vkEndCommandBuffer(g_hTemporaryCommandBuffer) );
|
| 142 |
|
| 143 | VkSubmitInfo submitInfo = { VK_STRUCTURE_TYPE_SUBMIT_INFO };
|
| 144 | submitInfo.commandBufferCount = 1;
|
| 145 | submitInfo.pCommandBuffers = &g_hTemporaryCommandBuffer;
|
| 146 |
|
| 147 | ERR_GUARD_VULKAN( vkQueueSubmit(g_hGraphicsQueue, 1, &submitInfo, VK_NULL_HANDLE) );
|
| 148 | ERR_GUARD_VULKAN( vkQueueWaitIdle(g_hGraphicsQueue) );
|
| 149 | }
|
| 150 |
|
| 151 | static void LoadShader(std::vector<char>& out, const char* fileName)
|
| 152 | {
|
| 153 | std::ifstream file(std::string(SHADER_PATH1) + fileName, std::ios::ate | std::ios::binary);
|
| 154 | if(file.is_open() == false)
|
| 155 | file.open(std::string(SHADER_PATH2) + fileName, std::ios::ate | std::ios::binary);
|
| 156 | assert(file.is_open());
|
| 157 | size_t fileSize = (size_t)file.tellg();
|
| 158 | if(fileSize > 0)
|
| 159 | {
|
| 160 | out.resize(fileSize);
|
| 161 | file.seekg(0);
|
| 162 | file.read(out.data(), fileSize);
|
| 163 | file.close();
|
| 164 | }
|
| 165 | else
|
| 166 | out.clear();
|
| 167 | }
|
| 168 |
|
| 169 | VKAPI_ATTR VkBool32 VKAPI_CALL MyDebugReportCallback(
|
| 170 | VkDebugReportFlagsEXT flags,
|
| 171 | VkDebugReportObjectTypeEXT objectType,
|
| 172 | uint64_t object,
|
| 173 | size_t location,
|
| 174 | int32_t messageCode,
|
| 175 | const char* pLayerPrefix,
|
| 176 | const char* pMessage,
|
| 177 | void* pUserData)
|
| 178 | {
|
| 179 | printf("%s \xBA %s\n", pLayerPrefix, pMessage);
|
| 180 |
|
| 181 | if((flags == VK_DEBUG_REPORT_WARNING_BIT_EXT) ||
|
| 182 | (flags == VK_DEBUG_REPORT_ERROR_BIT_EXT))
|
| 183 | {
|
| 184 | OutputDebugStringA(pMessage);
|
| 185 | OutputDebugStringA("\n");
|
| 186 | }
|
| 187 |
|
| 188 | return VK_FALSE;
|
| 189 | }
|
| 190 |
|
| 191 | static VkSurfaceFormatKHR ChooseSurfaceFormat()
|
| 192 | {
|
| 193 | assert(!g_SurfaceFormats.empty());
|
| 194 |
|
| 195 | if((g_SurfaceFormats.size() == 1) && (g_SurfaceFormats[0].format == VK_FORMAT_UNDEFINED))
|
| 196 | {
|
| 197 | VkSurfaceFormatKHR result = { VK_FORMAT_B8G8R8A8_UNORM, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR };
|
| 198 | return result;
|
| 199 | }
|
| 200 |
|
| 201 | for(const auto& format : g_SurfaceFormats)
|
| 202 | {
|
| 203 | if((format.format == VK_FORMAT_B8G8R8A8_UNORM) &&
|
| 204 | (format.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR))
|
| 205 | {
|
| 206 | return format;
|
| 207 | }
|
| 208 | }
|
| 209 |
|
| 210 | return g_SurfaceFormats[0];
|
| 211 | }
|
| 212 |
|
| 213 | VkPresentModeKHR ChooseSwapPresentMode()
|
| 214 | {
|
| 215 | VkPresentModeKHR preferredMode = VSYNC ? VK_PRESENT_MODE_MAILBOX_KHR : VK_PRESENT_MODE_IMMEDIATE_KHR;
|
| 216 |
|
| 217 | if(std::find(g_PresentModes.begin(), g_PresentModes.end(), preferredMode) !=
|
| 218 | g_PresentModes.end())
|
| 219 | {
|
| 220 | return preferredMode;
|
| 221 | }
|
| 222 |
|
| 223 | return VK_PRESENT_MODE_FIFO_KHR;
|
| 224 | }
|
| 225 |
|
| 226 | static VkExtent2D ChooseSwapExtent()
|
| 227 | {
|
| 228 | if(g_SurfaceCapabilities.currentExtent.width != UINT_MAX)
|
| 229 | return g_SurfaceCapabilities.currentExtent;
|
| 230 |
|
| 231 | VkExtent2D result = {
|
| 232 | std::max(g_SurfaceCapabilities.minImageExtent.width,
|
| 233 | std::min(g_SurfaceCapabilities.maxImageExtent.width, (uint32_t)g_SizeX)),
|
| 234 | std::max(g_SurfaceCapabilities.minImageExtent.height,
|
| 235 | std::min(g_SurfaceCapabilities.maxImageExtent.height, (uint32_t)g_SizeY)) };
|
| 236 | return result;
|
| 237 | }
|
| 238 |
|
| 239 | struct Vertex
|
| 240 | {
|
| 241 | float pos[3];
|
| 242 | float color[3];
|
| 243 | float texCoord[2];
|
| 244 | };
|
| 245 |
|
| 246 | static void CreateMesh()
|
| 247 | {
|
| 248 | assert(g_hAllocator);
|
| 249 |
|
| 250 | static Vertex vertices[] = {
|
| 251 | // -X
|
| 252 | { { -1.f, -1.f, -1.f}, {1.0f, 0.0f, 0.0f}, {0.f, 0.f} },
|
| 253 | { { -1.f, -1.f, 1.f}, {1.0f, 0.0f, 0.0f}, {1.f, 0.f} },
|
| 254 | { { -1.f, 1.f, -1.f}, {1.0f, 0.0f, 0.0f}, {0.f, 1.f} },
|
| 255 | { { -1.f, 1.f, 1.f}, {1.0f, 0.0f, 0.0f}, {1.f, 1.f} },
|
| 256 | // +X
|
| 257 | { { 1.f, -1.f, 1.f}, {0.0f, 1.0f, 0.0f}, {0.f, 0.f} },
|
| 258 | { { 1.f, -1.f, -1.f}, {0.0f, 1.0f, 0.0f}, {1.f, 0.f} },
|
| 259 | { { 1.f, 1.f, 1.f}, {0.0f, 1.0f, 0.0f}, {0.f, 1.f} },
|
| 260 | { { 1.f, 1.f, -1.f}, {0.0f, 1.0f, 0.0f}, {1.f, 1.f} },
|
| 261 | // -Z
|
| 262 | { { 1.f, -1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {0.f, 0.f} },
|
| 263 | { {-1.f, -1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {1.f, 0.f} },
|
| 264 | { { 1.f, 1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {0.f, 1.f} },
|
| 265 | { {-1.f, 1.f, -1.f}, {0.0f, 0.0f, 1.0f}, {1.f, 1.f} },
|
| 266 | // +Z
|
| 267 | { {-1.f, -1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {0.f, 0.f} },
|
| 268 | { { 1.f, -1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {1.f, 0.f} },
|
| 269 | { {-1.f, 1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {0.f, 1.f} },
|
| 270 | { { 1.f, 1.f, 1.f}, {1.0f, 1.0f, 0.0f}, {1.f, 1.f} },
|
| 271 | // -Y
|
| 272 | { {-1.f, -1.f, -1.f}, {0.0f, 1.0f, 1.0f}, {0.f, 0.f} },
|
| 273 | { { 1.f, -1.f, -1.f}, {0.0f, 1.0f, 1.0f}, {1.f, 0.f} },
|
| 274 | { {-1.f, -1.f, 1.f}, {0.0f, 1.0f, 1.0f}, {0.f, 1.f} },
|
| 275 | { { 1.f, -1.f, 1.f}, {0.0f, 1.0f, 1.0f}, {1.f, 1.f} },
|
| 276 | // +Y
|
| 277 | { { 1.f, 1.f, -1.f}, {1.0f, 0.0f, 1.0f}, {0.f, 0.f} },
|
| 278 | { {-1.f, 1.f, -1.f}, {1.0f, 0.0f, 1.0f}, {1.f, 0.f} },
|
| 279 | { { 1.f, 1.f, 1.f}, {1.0f, 0.0f, 1.0f}, {0.f, 1.f} },
|
| 280 | { {-1.f, 1.f, 1.f}, {1.0f, 0.0f, 1.0f}, {1.f, 1.f} },
|
| 281 | };
|
| 282 | static uint16_t indices[] = {
|
| 283 | 0, 1, 2, 3, USHRT_MAX,
|
| 284 | 4, 5, 6, 7, USHRT_MAX,
|
| 285 | 8, 9, 10, 11, USHRT_MAX,
|
| 286 | 12, 13, 14, 15, USHRT_MAX,
|
| 287 | 16, 17, 18, 19, USHRT_MAX,
|
| 288 | 20, 21, 22, 23, USHRT_MAX,
|
| 289 | };
|
| 290 |
|
| 291 | size_t vertexBufferSize = sizeof(Vertex) * _countof(vertices);
|
| 292 | size_t indexBufferSize = sizeof(uint16_t) * _countof(indices);
|
| 293 | g_IndexCount = (uint32_t)_countof(indices);
|
| 294 |
|
| 295 | // Create vertex buffer
|
| 296 |
|
| 297 | VkBufferCreateInfo vbInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 298 | vbInfo.size = vertexBufferSize;
|
| 299 | vbInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 300 | vbInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 301 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 302 | VmaAllocationCreateInfo vbAllocCreateInfo = {};
|
| 303 | vbAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 304 | vbAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 305 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 306 | VkBuffer stagingVertexBuffer = VK_NULL_HANDLE;
|
| 307 | VmaAllocation stagingVertexBufferAlloc = VK_NULL_HANDLE;
|
| 308 | VmaAllocationInfo stagingVertexBufferAllocInfo = {};
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 309 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &vbInfo, &vbAllocCreateInfo, &stagingVertexBuffer, &stagingVertexBufferAlloc, &stagingVertexBufferAllocInfo) );
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 310 |
|
| 311 | memcpy(stagingVertexBufferAllocInfo.pMappedData, vertices, vertexBufferSize);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 312 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 313 | // No need to flush stagingVertexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 314 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 315 | 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] | 316 | vbAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 317 | vbAllocCreateInfo.flags = 0;
|
| 318 | 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] | 319 |
|
| 320 | // Create index buffer
|
| 321 |
|
| 322 | VkBufferCreateInfo ibInfo = { VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO };
|
| 323 | ibInfo.size = indexBufferSize;
|
| 324 | ibInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
| 325 | ibInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 326 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 327 | VmaAllocationCreateInfo ibAllocCreateInfo = {};
|
| 328 | ibAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 329 | ibAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 330 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 331 | VkBuffer stagingIndexBuffer = VK_NULL_HANDLE;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 332 | VmaAllocation stagingIndexBufferAlloc = VK_NULL_HANDLE;
|
| 333 | VmaAllocationInfo stagingIndexBufferAllocInfo = {};
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 334 | ERR_GUARD_VULKAN( vmaCreateBuffer(g_hAllocator, &ibInfo, &ibAllocCreateInfo, &stagingIndexBuffer, &stagingIndexBufferAlloc, &stagingIndexBufferAllocInfo) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 335 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 336 | memcpy(stagingIndexBufferAllocInfo.pMappedData, indices, indexBufferSize);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 337 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 338 | // No need to flush stagingIndexBuffer memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 339 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 340 | 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] | 341 | ibAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
| 342 | ibAllocCreateInfo.flags = 0;
|
| 343 | 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] | 344 |
|
| 345 | // Copy buffers
|
| 346 |
|
| 347 | BeginSingleTimeCommands();
|
| 348 |
|
| 349 | VkBufferCopy vbCopyRegion = {};
|
| 350 | vbCopyRegion.srcOffset = 0;
|
| 351 | vbCopyRegion.dstOffset = 0;
|
| 352 | vbCopyRegion.size = vbInfo.size;
|
| 353 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, stagingVertexBuffer, g_hVertexBuffer, 1, &vbCopyRegion);
|
| 354 |
|
| 355 | VkBufferCopy ibCopyRegion = {};
|
| 356 | ibCopyRegion.srcOffset = 0;
|
| 357 | ibCopyRegion.dstOffset = 0;
|
| 358 | ibCopyRegion.size = ibInfo.size;
|
| 359 | vkCmdCopyBuffer(g_hTemporaryCommandBuffer, stagingIndexBuffer, g_hIndexBuffer, 1, &ibCopyRegion);
|
| 360 |
|
| 361 | EndSingleTimeCommands();
|
| 362 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 363 | vmaDestroyBuffer(g_hAllocator, stagingIndexBuffer, stagingIndexBufferAlloc);
|
| 364 | vmaDestroyBuffer(g_hAllocator, stagingVertexBuffer, stagingVertexBufferAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 365 | }
|
| 366 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 367 | static void ImageBarrier(
|
| 368 | VkImage image,
|
| 369 | VkImageAspectFlags aspectMask,
|
| 370 | uint32_t mipLevelCount,
|
| 371 | VkImageLayout oldLayout,
|
| 372 | VkImageLayout newLayout,
|
| 373 | VkAccessFlags srcAccessMask,
|
| 374 | VkAccessFlags dstAccessMask,
|
| 375 | VkPipelineStageFlags srcStageMask,
|
| 376 | VkPipelineStageFlags dstStageMask)
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 377 | {
|
| 378 | BeginSingleTimeCommands();
|
| 379 |
|
| 380 | VkImageMemoryBarrier imgMemBarrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER };
|
| 381 | imgMemBarrier.oldLayout = oldLayout;
|
| 382 | imgMemBarrier.newLayout = newLayout;
|
| 383 | imgMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 384 | imgMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 385 | imgMemBarrier.image = image;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 386 | imgMemBarrier.subresourceRange.aspectMask = aspectMask;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 387 | imgMemBarrier.subresourceRange.baseMipLevel = 0;
|
| 388 | imgMemBarrier.subresourceRange.levelCount = mipLevelCount;
|
| 389 | imgMemBarrier.subresourceRange.baseArrayLayer = 0;
|
| 390 | imgMemBarrier.subresourceRange.layerCount = 1;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 391 | imgMemBarrier.srcAccessMask = srcAccessMask;
|
| 392 | imgMemBarrier.dstAccessMask = dstAccessMask;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 393 |
|
| 394 | vkCmdPipelineBarrier(
|
| 395 | g_hTemporaryCommandBuffer,
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 396 | srcStageMask,
|
| 397 | dstStageMask,
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 398 | 0,
|
| 399 | 0, nullptr,
|
| 400 | 0, nullptr,
|
| 401 | 1, &imgMemBarrier);
|
| 402 |
|
| 403 | EndSingleTimeCommands();
|
| 404 | }
|
| 405 |
|
| 406 | static void CreateTexture(uint32_t sizeX, uint32_t sizeY)
|
| 407 | {
|
| 408 | // Create Image
|
| 409 |
|
| 410 | const VkDeviceSize imageSize = sizeX * sizeY * 4;
|
| 411 |
|
| 412 | VkImageCreateInfo stagingImageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 413 | stagingImageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 414 | stagingImageInfo.extent.width = sizeX;
|
| 415 | stagingImageInfo.extent.height = sizeY;
|
| 416 | stagingImageInfo.extent.depth = 1;
|
| 417 | stagingImageInfo.mipLevels = 1;
|
| 418 | stagingImageInfo.arrayLayers = 1;
|
| 419 | stagingImageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 420 | stagingImageInfo.tiling = VK_IMAGE_TILING_LINEAR;
|
| 421 | stagingImageInfo.initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 422 | stagingImageInfo.usage = VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
| 423 | stagingImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 424 | stagingImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 425 | stagingImageInfo.flags = 0;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 426 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 427 | VmaAllocationCreateInfo stagingImageAllocCreateInfo = {};
|
| 428 | stagingImageAllocCreateInfo.usage = VMA_MEMORY_USAGE_CPU_ONLY;
|
| 429 | stagingImageAllocCreateInfo.flags = VMA_ALLOCATION_CREATE_PERSISTENT_MAP_BIT;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 430 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 431 | VkImage stagingImage = VK_NULL_HANDLE;
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 432 | VmaAllocation stagingImageAlloc = VK_NULL_HANDLE;
|
| 433 | VmaAllocationInfo stagingImageAllocInfo = {};
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 434 | ERR_GUARD_VULKAN( vmaCreateImage(g_hAllocator, &stagingImageInfo, &stagingImageAllocCreateInfo, &stagingImage, &stagingImageAlloc, &stagingImageAllocInfo) );
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 435 |
|
| 436 | VkImageSubresource imageSubresource = {};
|
| 437 | imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 438 | imageSubresource.mipLevel = 0;
|
| 439 | imageSubresource.arrayLayer = 0;
|
| 440 |
|
| 441 | VkSubresourceLayout imageLayout = {};
|
| 442 | vkGetImageSubresourceLayout(g_hDevice, stagingImage, &imageSubresource, &imageLayout);
|
| 443 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 444 | char* const pMipLevelData = (char*)stagingImageAllocInfo.pMappedData + imageLayout.offset;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 445 | uint8_t* pRowData = (uint8_t*)pMipLevelData;
|
| 446 | for(uint32_t y = 0; y < sizeY; ++y)
|
| 447 | {
|
| 448 | uint32_t* pPixelData = (uint32_t*)pRowData;
|
| 449 | for(uint32_t x = 0; x < sizeY; ++x)
|
| 450 | {
|
| 451 | *pPixelData =
|
| 452 | ((x & 0x18) == 0x08 ? 0x000000FF : 0x00000000) |
|
| 453 | ((x & 0x18) == 0x10 ? 0x0000FFFF : 0x00000000) |
|
| 454 | ((y & 0x18) == 0x08 ? 0x0000FF00 : 0x00000000) |
|
| 455 | ((y & 0x18) == 0x10 ? 0x00FF0000 : 0x00000000);
|
| 456 | ++pPixelData;
|
| 457 | }
|
| 458 | pRowData += imageLayout.rowPitch;
|
| 459 | }
|
| 460 |
|
Adam Sawicki | 2f16fa5 | 2017-07-04 14:43:20 +0200 | [diff] [blame] | 461 | // No need to flush stagingImage memory because CPU_ONLY memory is always HOST_COHERENT.
|
| 462 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 463 | // Create g_hTextureImage in GPU memory.
|
| 464 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 465 | VkImageCreateInfo imageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 466 | imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 467 | imageInfo.extent.width = sizeX;
|
| 468 | imageInfo.extent.height = sizeY;
|
| 469 | imageInfo.extent.depth = 1;
|
| 470 | imageInfo.mipLevels = 1;
|
| 471 | imageInfo.arrayLayers = 1;
|
| 472 | imageInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 473 | imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 474 | imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 475 | imageInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
| 476 | imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 477 | imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 478 | imageInfo.flags = 0;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 479 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 480 | VmaAllocationCreateInfo imageAllocCreateInfo = {};
|
| 481 | imageAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 482 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 483 | 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] | 484 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 485 | // Transition image layouts, copy image.
|
| 486 |
|
| 487 | BeginSingleTimeCommands();
|
| 488 |
|
| 489 | VkImageMemoryBarrier imgMemBarrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER };
|
| 490 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_PREINITIALIZED;
|
| 491 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
|
| 492 | imgMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 493 | imgMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 494 | imgMemBarrier.image = stagingImage;
|
| 495 | imgMemBarrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 496 | imgMemBarrier.subresourceRange.baseMipLevel = 0;
|
| 497 | imgMemBarrier.subresourceRange.levelCount = 1;
|
| 498 | imgMemBarrier.subresourceRange.baseArrayLayer = 0;
|
| 499 | imgMemBarrier.subresourceRange.layerCount = 1;
|
| 500 | imgMemBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
| 501 | imgMemBarrier.dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
|
| 502 |
|
| 503 | vkCmdPipelineBarrier(
|
| 504 | g_hTemporaryCommandBuffer,
|
| 505 | VK_PIPELINE_STAGE_HOST_BIT,
|
| 506 | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
| 507 | 0,
|
| 508 | 0, nullptr,
|
| 509 | 0, nullptr,
|
| 510 | 1, &imgMemBarrier);
|
| 511 |
|
| 512 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 513 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
| 514 | imgMemBarrier.image = g_hTextureImage;
|
| 515 | imgMemBarrier.srcAccessMask = 0;
|
| 516 | imgMemBarrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
| 517 |
|
| 518 | vkCmdPipelineBarrier(
|
| 519 | g_hTemporaryCommandBuffer,
|
| 520 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
| 521 | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
| 522 | 0,
|
| 523 | 0, nullptr,
|
| 524 | 0, nullptr,
|
| 525 | 1, &imgMemBarrier);
|
| 526 |
|
| 527 | VkImageCopy imageCopy = {};
|
| 528 | imageCopy.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 529 | imageCopy.srcSubresource.baseArrayLayer = 0;
|
| 530 | imageCopy.srcSubresource.mipLevel = 0;
|
| 531 | imageCopy.srcSubresource.layerCount = 1;
|
| 532 | imageCopy.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 533 | imageCopy.dstSubresource.baseArrayLayer = 0;
|
| 534 | imageCopy.dstSubresource.mipLevel = 0;
|
| 535 | imageCopy.dstSubresource.layerCount = 1;
|
| 536 | imageCopy.srcOffset.x = 0;
|
| 537 | imageCopy.srcOffset.y = 0;
|
| 538 | imageCopy.srcOffset.z = 0;
|
| 539 | imageCopy.dstOffset.x = 0;
|
| 540 | imageCopy.dstOffset.y = 0;
|
| 541 | imageCopy.dstOffset.z = 0;
|
| 542 | imageCopy.extent.width = sizeX;
|
| 543 | imageCopy.extent.height = sizeY;
|
| 544 | imageCopy.extent.depth = 1;
|
| 545 | vkCmdCopyImage(
|
| 546 | g_hTemporaryCommandBuffer,
|
| 547 | stagingImage, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
|
| 548 | g_hTextureImage, VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL,
|
| 549 | 1, &imageCopy);
|
| 550 |
|
| 551 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
| 552 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
| 553 | imgMemBarrier.image = g_hTextureImage;
|
| 554 | imgMemBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
| 555 | imgMemBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
| 556 |
|
| 557 | vkCmdPipelineBarrier(
|
| 558 | g_hTemporaryCommandBuffer,
|
| 559 | VK_PIPELINE_STAGE_TRANSFER_BIT,
|
| 560 | VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT,
|
| 561 | 0,
|
| 562 | 0, nullptr,
|
| 563 | 0, nullptr,
|
| 564 | 1, &imgMemBarrier);
|
| 565 |
|
| 566 | EndSingleTimeCommands();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 567 |
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 568 | vmaDestroyImage(g_hAllocator, stagingImage, stagingImageAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 569 |
|
| 570 | // Create ImageView
|
| 571 |
|
| 572 | VkImageViewCreateInfo textureImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 573 | textureImageViewInfo.image = g_hTextureImage;
|
| 574 | textureImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 575 | textureImageViewInfo.format = VK_FORMAT_R8G8B8A8_UNORM;
|
| 576 | textureImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 577 | textureImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 578 | textureImageViewInfo.subresourceRange.levelCount = 1;
|
| 579 | textureImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 580 | textureImageViewInfo.subresourceRange.layerCount = 1;
|
| 581 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &textureImageViewInfo, nullptr, &g_hTextureImageView) );
|
| 582 | }
|
| 583 |
|
| 584 | struct UniformBufferObject
|
| 585 | {
|
| 586 | mathfu::vec4_packed ModelViewProj[4];
|
| 587 | };
|
| 588 |
|
| 589 | static void RegisterDebugCallbacks()
|
| 590 | {
|
| 591 | g_pvkCreateDebugReportCallbackEXT =
|
| 592 | reinterpret_cast<PFN_vkCreateDebugReportCallbackEXT>
|
| 593 | (vkGetInstanceProcAddr(g_hVulkanInstance, "vkCreateDebugReportCallbackEXT"));
|
| 594 | g_pvkDebugReportMessageEXT =
|
| 595 | reinterpret_cast<PFN_vkDebugReportMessageEXT>
|
| 596 | (vkGetInstanceProcAddr(g_hVulkanInstance, "vkDebugReportMessageEXT"));
|
| 597 | g_pvkDestroyDebugReportCallbackEXT =
|
| 598 | reinterpret_cast<PFN_vkDestroyDebugReportCallbackEXT>
|
| 599 | (vkGetInstanceProcAddr(g_hVulkanInstance, "vkDestroyDebugReportCallbackEXT"));
|
| 600 | assert(g_pvkCreateDebugReportCallbackEXT);
|
| 601 | assert(g_pvkDebugReportMessageEXT);
|
| 602 | assert(g_pvkDestroyDebugReportCallbackEXT);
|
| 603 |
|
| 604 | VkDebugReportCallbackCreateInfoEXT callbackCreateInfo;
|
| 605 | callbackCreateInfo.sType = VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT;
|
| 606 | callbackCreateInfo.pNext = nullptr;
|
| 607 | callbackCreateInfo.flags = //VK_DEBUG_REPORT_INFORMATION_BIT_EXT |
|
| 608 | VK_DEBUG_REPORT_ERROR_BIT_EXT |
|
| 609 | VK_DEBUG_REPORT_WARNING_BIT_EXT |
|
| 610 | VK_DEBUG_REPORT_PERFORMANCE_WARNING_BIT_EXT /*|
|
| 611 | VK_DEBUG_REPORT_DEBUG_BIT_EXT*/;
|
| 612 | callbackCreateInfo.pfnCallback = &MyDebugReportCallback;
|
| 613 | callbackCreateInfo.pUserData = nullptr;
|
| 614 |
|
| 615 | ERR_GUARD_VULKAN( g_pvkCreateDebugReportCallbackEXT(g_hVulkanInstance, &callbackCreateInfo, nullptr, &g_hCallback) );
|
| 616 | }
|
| 617 |
|
| 618 | static bool IsLayerSupported(const VkLayerProperties* pProps, size_t propCount, const char* pLayerName)
|
| 619 | {
|
| 620 | const VkLayerProperties* propsEnd = pProps + propCount;
|
| 621 | return std::find_if(
|
| 622 | pProps,
|
| 623 | propsEnd,
|
| 624 | [pLayerName](const VkLayerProperties& prop) -> bool {
|
| 625 | return strcmp(pLayerName, prop.layerName) == 0;
|
| 626 | }) != propsEnd;
|
| 627 | }
|
| 628 |
|
| 629 | static VkFormat FindSupportedFormat(
|
| 630 | const std::vector<VkFormat>& candidates,
|
| 631 | VkImageTiling tiling,
|
| 632 | VkFormatFeatureFlags features)
|
| 633 | {
|
| 634 | for (VkFormat format : candidates)
|
| 635 | {
|
| 636 | VkFormatProperties props;
|
| 637 | vkGetPhysicalDeviceFormatProperties(g_hPhysicalDevice, format, &props);
|
| 638 |
|
| 639 | if ((tiling == VK_IMAGE_TILING_LINEAR) &&
|
| 640 | ((props.linearTilingFeatures & features) == features))
|
| 641 | {
|
| 642 | return format;
|
| 643 | }
|
| 644 | else if ((tiling == VK_IMAGE_TILING_OPTIMAL) &&
|
| 645 | ((props.optimalTilingFeatures & features) == features))
|
| 646 | {
|
| 647 | return format;
|
| 648 | }
|
| 649 | }
|
| 650 | return VK_FORMAT_UNDEFINED;
|
| 651 | }
|
| 652 |
|
| 653 | static VkFormat FindDepthFormat()
|
| 654 | {
|
| 655 | std::vector<VkFormat> formats;
|
| 656 | formats.push_back(VK_FORMAT_D32_SFLOAT);
|
| 657 | formats.push_back(VK_FORMAT_D32_SFLOAT_S8_UINT);
|
| 658 | formats.push_back(VK_FORMAT_D24_UNORM_S8_UINT);
|
| 659 |
|
| 660 | return FindSupportedFormat(
|
| 661 | formats,
|
| 662 | VK_IMAGE_TILING_OPTIMAL,
|
| 663 | VK_FORMAT_FEATURE_DEPTH_STENCIL_ATTACHMENT_BIT);
|
| 664 | }
|
| 665 |
|
| 666 | static void CreateSwapchain()
|
| 667 | {
|
| 668 | // Query surface formats.
|
| 669 |
|
| 670 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceCapabilitiesKHR(g_hPhysicalDevice, g_hSurface, &g_SurfaceCapabilities) );
|
| 671 |
|
| 672 | uint32_t formatCount = 0;
|
| 673 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceFormatsKHR(g_hPhysicalDevice, g_hSurface, &formatCount, nullptr) );
|
| 674 | g_SurfaceFormats.resize(formatCount);
|
| 675 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfaceFormatsKHR(g_hPhysicalDevice, g_hSurface, &formatCount, g_SurfaceFormats.data()) );
|
| 676 |
|
| 677 | uint32_t presentModeCount = 0;
|
| 678 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfacePresentModesKHR(g_hPhysicalDevice, g_hSurface, &presentModeCount, nullptr) );
|
| 679 | g_PresentModes.resize(presentModeCount);
|
| 680 | ERR_GUARD_VULKAN( vkGetPhysicalDeviceSurfacePresentModesKHR(g_hPhysicalDevice, g_hSurface, &presentModeCount, g_PresentModes.data()) );
|
| 681 |
|
| 682 | // Create swap chain
|
| 683 |
|
| 684 | g_SurfaceFormat = ChooseSurfaceFormat();
|
| 685 | VkPresentModeKHR presentMode = ChooseSwapPresentMode();
|
| 686 | g_Extent = ChooseSwapExtent();
|
| 687 |
|
| 688 | uint32_t imageCount = g_SurfaceCapabilities.minImageCount + 1;
|
| 689 | if((g_SurfaceCapabilities.maxImageCount > 0) &&
|
| 690 | (imageCount > g_SurfaceCapabilities.maxImageCount))
|
| 691 | {
|
| 692 | imageCount = g_SurfaceCapabilities.maxImageCount;
|
| 693 | }
|
| 694 |
|
| 695 | VkSwapchainCreateInfoKHR swapChainInfo = { VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR };
|
| 696 | swapChainInfo.surface = g_hSurface;
|
| 697 | swapChainInfo.minImageCount = imageCount;
|
| 698 | swapChainInfo.imageFormat = g_SurfaceFormat.format;
|
| 699 | swapChainInfo.imageColorSpace = g_SurfaceFormat.colorSpace;
|
| 700 | swapChainInfo.imageExtent = g_Extent;
|
| 701 | swapChainInfo.imageArrayLayers = 1;
|
| 702 | swapChainInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
| 703 | swapChainInfo.preTransform = g_SurfaceCapabilities.currentTransform;
|
| 704 | swapChainInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
| 705 | swapChainInfo.presentMode = presentMode;
|
| 706 | swapChainInfo.clipped = VK_TRUE;
|
| 707 | swapChainInfo.oldSwapchain = g_hSwapchain;
|
| 708 |
|
| 709 | uint32_t queueFamilyIndices[] = { g_GraphicsQueueFamilyIndex, g_PresentQueueFamilyIndex };
|
| 710 | if(g_PresentQueueFamilyIndex != g_GraphicsQueueFamilyIndex)
|
| 711 | {
|
| 712 | swapChainInfo.imageSharingMode = VK_SHARING_MODE_CONCURRENT;
|
| 713 | swapChainInfo.queueFamilyIndexCount = 2;
|
| 714 | swapChainInfo.pQueueFamilyIndices = queueFamilyIndices;
|
| 715 | }
|
| 716 | else
|
| 717 | {
|
| 718 | swapChainInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 719 | }
|
| 720 |
|
| 721 | VkSwapchainKHR hNewSwapchain = VK_NULL_HANDLE;
|
| 722 | ERR_GUARD_VULKAN( vkCreateSwapchainKHR(g_hDevice, &swapChainInfo, nullptr, &hNewSwapchain) );
|
| 723 | if(g_hSwapchain != VK_NULL_HANDLE)
|
| 724 | vkDestroySwapchainKHR(g_hDevice, g_hSwapchain, nullptr);
|
| 725 | g_hSwapchain = hNewSwapchain;
|
| 726 |
|
| 727 | // Retrieve swapchain images.
|
| 728 |
|
| 729 | uint32_t swapchainImageCount = 0;
|
| 730 | ERR_GUARD_VULKAN( vkGetSwapchainImagesKHR(g_hDevice, g_hSwapchain, &swapchainImageCount, nullptr) );
|
| 731 | g_SwapchainImages.resize(swapchainImageCount);
|
| 732 | ERR_GUARD_VULKAN( vkGetSwapchainImagesKHR(g_hDevice, g_hSwapchain, &swapchainImageCount, g_SwapchainImages.data()) );
|
| 733 |
|
| 734 | // Create swapchain image views.
|
| 735 |
|
| 736 | for(size_t i = g_SwapchainImageViews.size(); i--; )
|
| 737 | vkDestroyImageView(g_hDevice, g_SwapchainImageViews[i], nullptr);
|
| 738 | g_SwapchainImageViews.clear();
|
| 739 |
|
| 740 | VkImageViewCreateInfo swapchainImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 741 | g_SwapchainImageViews.resize(swapchainImageCount);
|
| 742 | for(uint32_t i = 0; i < swapchainImageCount; ++i)
|
| 743 | {
|
| 744 | swapchainImageViewInfo.image = g_SwapchainImages[i];
|
| 745 | swapchainImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 746 | swapchainImageViewInfo.format = g_SurfaceFormat.format;
|
| 747 | swapchainImageViewInfo.components.r = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 748 | swapchainImageViewInfo.components.g = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 749 | swapchainImageViewInfo.components.b = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 750 | swapchainImageViewInfo.components.a = VK_COMPONENT_SWIZZLE_IDENTITY;
|
| 751 | swapchainImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
| 752 | swapchainImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 753 | swapchainImageViewInfo.subresourceRange.levelCount = 1;
|
| 754 | swapchainImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 755 | swapchainImageViewInfo.subresourceRange.layerCount = 1;
|
| 756 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &swapchainImageViewInfo, nullptr, &g_SwapchainImageViews[i]) );
|
| 757 | }
|
| 758 |
|
| 759 | // Create depth buffer
|
| 760 |
|
| 761 | g_DepthFormat = FindDepthFormat();
|
| 762 | assert(g_DepthFormat != VK_FORMAT_UNDEFINED);
|
| 763 |
|
| 764 | VkImageCreateInfo depthImageInfo = { VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO };
|
| 765 | depthImageInfo.imageType = VK_IMAGE_TYPE_2D;
|
| 766 | depthImageInfo.extent.width = g_Extent.width;
|
| 767 | depthImageInfo.extent.height = g_Extent.height;
|
| 768 | depthImageInfo.extent.depth = 1;
|
| 769 | depthImageInfo.mipLevels = 1;
|
| 770 | depthImageInfo.arrayLayers = 1;
|
| 771 | depthImageInfo.format = g_DepthFormat;
|
| 772 | depthImageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
| 773 | depthImageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 774 | depthImageInfo.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT;
|
| 775 | depthImageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
| 776 | depthImageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
| 777 | depthImageInfo.flags = 0;
|
| 778 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 779 | VmaAllocationCreateInfo depthImageAllocCreateInfo = {};
|
| 780 | depthImageAllocCreateInfo.usage = VMA_MEMORY_USAGE_GPU_ONLY;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 781 |
|
Adam Sawicki | 976f920 | 2017-09-12 20:45:14 +0200 | [diff] [blame] | 782 | 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] | 783 |
|
| 784 | VkImageViewCreateInfo depthImageViewInfo = { VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO };
|
| 785 | depthImageViewInfo.image = g_hDepthImage;
|
| 786 | depthImageViewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
| 787 | depthImageViewInfo.format = g_DepthFormat;
|
| 788 | depthImageViewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_DEPTH_BIT;
|
| 789 | depthImageViewInfo.subresourceRange.baseMipLevel = 0;
|
| 790 | depthImageViewInfo.subresourceRange.levelCount = 1;
|
| 791 | depthImageViewInfo.subresourceRange.baseArrayLayer = 0;
|
| 792 | depthImageViewInfo.subresourceRange.layerCount = 1;
|
| 793 |
|
| 794 | ERR_GUARD_VULKAN( vkCreateImageView(g_hDevice, &depthImageViewInfo, nullptr, &g_hDepthImageView) );
|
| 795 |
|
Adam Sawicki | 10844a8 | 2017-08-16 17:32:09 +0200 | [diff] [blame] | 796 | // Transition image layout of g_hDepthImage.
|
| 797 |
|
| 798 | BeginSingleTimeCommands();
|
| 799 |
|
| 800 | VkImageMemoryBarrier imgMemBarrier = { VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER };
|
| 801 | imgMemBarrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 802 | imgMemBarrier.newLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
| 803 | imgMemBarrier.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 804 | imgMemBarrier.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
|
| 805 | imgMemBarrier.image = g_hDepthImage;
|
| 806 | imgMemBarrier.subresourceRange.aspectMask = g_DepthFormat == VK_FORMAT_D32_SFLOAT ?
|
| 807 | VK_IMAGE_ASPECT_DEPTH_BIT :
|
| 808 | VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
| 809 | imgMemBarrier.subresourceRange.baseMipLevel = 0;
|
| 810 | imgMemBarrier.subresourceRange.levelCount = 1;
|
| 811 | imgMemBarrier.subresourceRange.baseArrayLayer = 0;
|
| 812 | imgMemBarrier.subresourceRange.layerCount = 1;
|
| 813 | imgMemBarrier.srcAccessMask = 0;
|
| 814 | imgMemBarrier.dstAccessMask = VK_ACCESS_DEPTH_STENCIL_ATTACHMENT_WRITE_BIT;
|
| 815 |
|
| 816 | vkCmdPipelineBarrier(
|
| 817 | g_hTemporaryCommandBuffer,
|
| 818 | VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
| 819 | VK_PIPELINE_STAGE_EARLY_FRAGMENT_TESTS_BIT | VK_PIPELINE_STAGE_LATE_FRAGMENT_TESTS_BIT,
|
| 820 | 0,
|
| 821 | 0, nullptr,
|
| 822 | 0, nullptr,
|
| 823 | 1, &imgMemBarrier);
|
| 824 |
|
| 825 | EndSingleTimeCommands();
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 826 |
|
| 827 | // Create pipeline layout
|
| 828 | {
|
| 829 | if(g_hPipelineLayout != VK_NULL_HANDLE)
|
| 830 | {
|
| 831 | vkDestroyPipelineLayout(g_hDevice, g_hPipelineLayout, nullptr);
|
| 832 | g_hPipelineLayout = VK_NULL_HANDLE;
|
| 833 | }
|
| 834 |
|
| 835 | VkPushConstantRange pushConstantRanges[1];
|
| 836 | ZeroMemory(&pushConstantRanges, sizeof pushConstantRanges);
|
| 837 | pushConstantRanges[0].offset = 0;
|
| 838 | pushConstantRanges[0].size = sizeof(UniformBufferObject);
|
| 839 | pushConstantRanges[0].stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
|
| 840 |
|
| 841 | VkDescriptorSetLayout descriptorSetLayouts[] = { g_hDescriptorSetLayout };
|
| 842 | VkPipelineLayoutCreateInfo pipelineLayoutInfo = { VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO };
|
| 843 | pipelineLayoutInfo.setLayoutCount = 1;
|
| 844 | pipelineLayoutInfo.pSetLayouts = descriptorSetLayouts;
|
| 845 | pipelineLayoutInfo.pushConstantRangeCount = 1;
|
| 846 | pipelineLayoutInfo.pPushConstantRanges = pushConstantRanges;
|
| 847 | ERR_GUARD_VULKAN( vkCreatePipelineLayout(g_hDevice, &pipelineLayoutInfo, nullptr, &g_hPipelineLayout) );
|
| 848 | }
|
| 849 |
|
| 850 | // Create render pass
|
| 851 | {
|
| 852 | if(g_hRenderPass != VK_NULL_HANDLE)
|
| 853 | {
|
| 854 | vkDestroyRenderPass(g_hDevice, g_hRenderPass, nullptr);
|
| 855 | g_hRenderPass = VK_NULL_HANDLE;
|
| 856 | }
|
| 857 |
|
| 858 | VkAttachmentDescription attachments[2];
|
| 859 | ZeroMemory(attachments, sizeof(attachments));
|
| 860 |
|
| 861 | attachments[0].format = g_SurfaceFormat.format;
|
| 862 | attachments[0].samples = VK_SAMPLE_COUNT_1_BIT;
|
| 863 | attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
| 864 | attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;
|
| 865 | attachments[0].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
| 866 | attachments[0].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
| 867 | attachments[0].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 868 | attachments[0].finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
|
| 869 |
|
| 870 | attachments[1].format = g_DepthFormat;
|
| 871 | attachments[1].samples = VK_SAMPLE_COUNT_1_BIT;
|
| 872 | attachments[1].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
|
| 873 | attachments[1].storeOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
| 874 | attachments[1].stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
|
| 875 | attachments[1].stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
|
| 876 | attachments[1].initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
| 877 | attachments[1].finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
| 878 |
|
| 879 | VkAttachmentReference colorAttachmentRef = {};
|
| 880 | colorAttachmentRef.attachment = 0;
|
| 881 | colorAttachmentRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
|
| 882 |
|
| 883 | VkAttachmentReference depthStencilAttachmentRef = {};
|
| 884 | depthStencilAttachmentRef.attachment = 1;
|
| 885 | depthStencilAttachmentRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
|
| 886 |
|
| 887 | VkSubpassDescription subpassDesc = {};
|
| 888 | subpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
|
| 889 | subpassDesc.colorAttachmentCount = 1;
|
| 890 | subpassDesc.pColorAttachments = &colorAttachmentRef;
|
| 891 | subpassDesc.pDepthStencilAttachment = &depthStencilAttachmentRef;
|
| 892 |
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 893 | VkRenderPassCreateInfo renderPassInfo = { VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO };
|
| 894 | renderPassInfo.attachmentCount = (uint32_t)_countof(attachments);
|
| 895 | renderPassInfo.pAttachments = attachments;
|
| 896 | renderPassInfo.subpassCount = 1;
|
| 897 | renderPassInfo.pSubpasses = &subpassDesc;
|
Adam Sawicki | 14137d1 | 2017-10-16 18:06:05 +0200 | [diff] [blame] | 898 | renderPassInfo.dependencyCount = 0;
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 899 | ERR_GUARD_VULKAN( vkCreateRenderPass(g_hDevice, &renderPassInfo, nullptr, &g_hRenderPass) );
|
| 900 | }
|
| 901 |
|
| 902 | // Create pipeline
|
| 903 | {
|
| 904 | std::vector<char> vertShaderCode;
|
| 905 | LoadShader(vertShaderCode, "Shader.vert.spv");
|
| 906 | VkShaderModuleCreateInfo shaderModuleInfo = { VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO };
|
| 907 | shaderModuleInfo.codeSize = vertShaderCode.size();
|
| 908 | shaderModuleInfo.pCode = (const uint32_t*)vertShaderCode.data();
|
| 909 | VkShaderModule hVertShaderModule = VK_NULL_HANDLE;
|
| 910 | ERR_GUARD_VULKAN( vkCreateShaderModule(g_hDevice, &shaderModuleInfo, nullptr, &hVertShaderModule) );
|
| 911 |
|
| 912 | std::vector<char> hFragShaderCode;
|
| 913 | LoadShader(hFragShaderCode, "Shader.frag.spv");
|
| 914 | shaderModuleInfo.codeSize = hFragShaderCode.size();
|
| 915 | shaderModuleInfo.pCode = (const uint32_t*)hFragShaderCode.data();
|
| 916 | VkShaderModule fragShaderModule = VK_NULL_HANDLE;
|
| 917 | ERR_GUARD_VULKAN( vkCreateShaderModule(g_hDevice, &shaderModuleInfo, nullptr, &fragShaderModule) );
|
| 918 |
|
| 919 | VkPipelineShaderStageCreateInfo vertPipelineShaderStageInfo = { VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
|
| 920 | vertPipelineShaderStageInfo.stage = VK_SHADER_STAGE_VERTEX_BIT;
|
| 921 | vertPipelineShaderStageInfo.module = hVertShaderModule;
|
| 922 | vertPipelineShaderStageInfo.pName = "main";
|
| 923 |
|
| 924 | VkPipelineShaderStageCreateInfo fragPipelineShaderStageInfo = { VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO };
|
| 925 | fragPipelineShaderStageInfo.stage = VK_SHADER_STAGE_FRAGMENT_BIT;
|
| 926 | fragPipelineShaderStageInfo.module = fragShaderModule;
|
| 927 | fragPipelineShaderStageInfo.pName = "main";
|
| 928 |
|
| 929 | VkPipelineShaderStageCreateInfo pipelineShaderStageInfos[] = {
|
| 930 | vertPipelineShaderStageInfo,
|
| 931 | fragPipelineShaderStageInfo
|
| 932 | };
|
| 933 |
|
| 934 | VkVertexInputBindingDescription bindingDescription = {};
|
| 935 | bindingDescription.binding = 0;
|
| 936 | bindingDescription.stride = sizeof(Vertex);
|
| 937 | bindingDescription.inputRate = VK_VERTEX_INPUT_RATE_VERTEX;
|
| 938 |
|
| 939 | VkVertexInputAttributeDescription attributeDescriptions[3];
|
| 940 | ZeroMemory(attributeDescriptions, sizeof(attributeDescriptions));
|
| 941 |
|
| 942 | attributeDescriptions[0].binding = 0;
|
| 943 | attributeDescriptions[0].location = 0;
|
| 944 | attributeDescriptions[0].format = VK_FORMAT_R32G32B32_SFLOAT;
|
| 945 | attributeDescriptions[0].offset = offsetof(Vertex, pos);
|
| 946 |
|
| 947 | attributeDescriptions[1].binding = 0;
|
| 948 | attributeDescriptions[1].location = 1;
|
| 949 | attributeDescriptions[1].format = VK_FORMAT_R32G32B32_SFLOAT;
|
| 950 | attributeDescriptions[1].offset = offsetof(Vertex, color);
|
| 951 |
|
| 952 | attributeDescriptions[2].binding = 0;
|
| 953 | attributeDescriptions[2].location = 2;
|
| 954 | attributeDescriptions[2].format = VK_FORMAT_R32G32_SFLOAT;
|
| 955 | attributeDescriptions[2].offset = offsetof(Vertex, texCoord);
|
| 956 |
|
| 957 | VkPipelineVertexInputStateCreateInfo pipelineVertexInputStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO };
|
| 958 | pipelineVertexInputStateInfo.vertexBindingDescriptionCount = 1;
|
| 959 | pipelineVertexInputStateInfo.pVertexBindingDescriptions = &bindingDescription;
|
| 960 | pipelineVertexInputStateInfo.vertexAttributeDescriptionCount = _countof(attributeDescriptions);
|
| 961 | pipelineVertexInputStateInfo.pVertexAttributeDescriptions = attributeDescriptions;
|
| 962 |
|
| 963 | VkPipelineInputAssemblyStateCreateInfo pipelineInputAssemblyStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO };
|
| 964 | pipelineInputAssemblyStateInfo.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP;
|
| 965 | pipelineInputAssemblyStateInfo.primitiveRestartEnable = VK_TRUE;
|
| 966 |
|
| 967 | VkViewport viewport = {};
|
| 968 | viewport.x = 0.f;
|
| 969 | viewport.y = 0.f;
|
| 970 | viewport.width = (float)g_Extent.width;
|
| 971 | viewport.height = (float)g_Extent.height;
|
| 972 | viewport.minDepth = 0.f;
|
| 973 | viewport.maxDepth = 1.f;
|
| 974 |
|
| 975 | VkRect2D scissor = {};
|
| 976 | scissor.offset.x = 0;
|
| 977 | scissor.offset.y = 0;
|
| 978 | scissor.extent = g_Extent;
|
| 979 |
|
| 980 | VkPipelineViewportStateCreateInfo pipelineViewportStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO };
|
| 981 | pipelineViewportStateInfo.viewportCount = 1;
|
| 982 | pipelineViewportStateInfo.pViewports = &viewport;
|
| 983 | pipelineViewportStateInfo.scissorCount = 1;
|
| 984 | pipelineViewportStateInfo.pScissors = &scissor;
|
| 985 |
|
| 986 | VkPipelineRasterizationStateCreateInfo pipelineRasterizationStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO };
|
| 987 | pipelineRasterizationStateInfo.depthClampEnable = VK_FALSE;
|
| 988 | pipelineRasterizationStateInfo.rasterizerDiscardEnable = VK_FALSE;
|
| 989 | pipelineRasterizationStateInfo.polygonMode = VK_POLYGON_MODE_FILL;
|
| 990 | pipelineRasterizationStateInfo.lineWidth = 1.f;
|
| 991 | pipelineRasterizationStateInfo.cullMode = VK_CULL_MODE_BACK_BIT;
|
| 992 | pipelineRasterizationStateInfo.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE;
|
| 993 | pipelineRasterizationStateInfo.depthBiasEnable = VK_FALSE;
|
| 994 | pipelineRasterizationStateInfo.depthBiasConstantFactor = 0.f;
|
| 995 | pipelineRasterizationStateInfo.depthBiasClamp = 0.f;
|
| 996 | pipelineRasterizationStateInfo.depthBiasSlopeFactor = 0.f;
|
| 997 |
|
| 998 | VkPipelineMultisampleStateCreateInfo pipelineMultisampleStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO };
|
| 999 | pipelineMultisampleStateInfo.sampleShadingEnable = VK_FALSE;
|
| 1000 | pipelineMultisampleStateInfo.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
|
| 1001 | pipelineMultisampleStateInfo.minSampleShading = 1.f;
|
| 1002 | pipelineMultisampleStateInfo.pSampleMask = nullptr;
|
| 1003 | pipelineMultisampleStateInfo.alphaToCoverageEnable = VK_FALSE;
|
| 1004 | pipelineMultisampleStateInfo.alphaToOneEnable = VK_FALSE;
|
| 1005 |
|
| 1006 | VkPipelineColorBlendAttachmentState pipelineColorBlendAttachmentState = {};
|
| 1007 | pipelineColorBlendAttachmentState.colorWriteMask =
|
| 1008 | VK_COLOR_COMPONENT_R_BIT |
|
| 1009 | VK_COLOR_COMPONENT_G_BIT |
|
| 1010 | VK_COLOR_COMPONENT_B_BIT |
|
| 1011 | VK_COLOR_COMPONENT_A_BIT;
|
| 1012 | pipelineColorBlendAttachmentState.blendEnable = VK_FALSE;
|
| 1013 | pipelineColorBlendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
| 1014 | pipelineColorBlendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
| 1015 | pipelineColorBlendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD; // Optional
|
| 1016 | pipelineColorBlendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE; // Optional
|
| 1017 | pipelineColorBlendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO; // Optional
|
| 1018 | pipelineColorBlendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD; // Optional
|
| 1019 |
|
| 1020 | VkPipelineColorBlendStateCreateInfo pipelineColorBlendStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO };
|
| 1021 | pipelineColorBlendStateInfo.logicOpEnable = VK_FALSE;
|
| 1022 | pipelineColorBlendStateInfo.logicOp = VK_LOGIC_OP_COPY;
|
| 1023 | pipelineColorBlendStateInfo.attachmentCount = 1;
|
| 1024 | pipelineColorBlendStateInfo.pAttachments = &pipelineColorBlendAttachmentState;
|
| 1025 |
|
| 1026 | VkPipelineDepthStencilStateCreateInfo depthStencilStateInfo = { VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO };
|
| 1027 | depthStencilStateInfo.depthTestEnable = VK_TRUE;
|
| 1028 | depthStencilStateInfo.depthWriteEnable = VK_TRUE;
|
| 1029 | depthStencilStateInfo.depthCompareOp = VK_COMPARE_OP_LESS;
|
| 1030 | depthStencilStateInfo.depthBoundsTestEnable = VK_FALSE;
|
| 1031 | depthStencilStateInfo.stencilTestEnable = VK_FALSE;
|
| 1032 |
|
| 1033 | VkGraphicsPipelineCreateInfo pipelineInfo = { VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO };
|
| 1034 | pipelineInfo.stageCount = 2;
|
| 1035 | pipelineInfo.pStages = pipelineShaderStageInfos;
|
| 1036 | pipelineInfo.pVertexInputState = &pipelineVertexInputStateInfo;
|
| 1037 | pipelineInfo.pInputAssemblyState = &pipelineInputAssemblyStateInfo;
|
| 1038 | pipelineInfo.pViewportState = &pipelineViewportStateInfo;
|
| 1039 | pipelineInfo.pRasterizationState = &pipelineRasterizationStateInfo;
|
| 1040 | pipelineInfo.pMultisampleState = &pipelineMultisampleStateInfo;
|
| 1041 | pipelineInfo.pDepthStencilState = &depthStencilStateInfo;
|
| 1042 | pipelineInfo.pColorBlendState = &pipelineColorBlendStateInfo;
|
| 1043 | pipelineInfo.pDynamicState = nullptr;
|
| 1044 | pipelineInfo.layout = g_hPipelineLayout;
|
| 1045 | pipelineInfo.renderPass = g_hRenderPass;
|
| 1046 | pipelineInfo.subpass = 0;
|
| 1047 | pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
|
| 1048 | pipelineInfo.basePipelineIndex = -1;
|
| 1049 | ERR_GUARD_VULKAN( vkCreateGraphicsPipelines(
|
| 1050 | g_hDevice,
|
| 1051 | VK_NULL_HANDLE,
|
| 1052 | 1,
|
| 1053 | &pipelineInfo, nullptr,
|
| 1054 | &g_hPipeline) );
|
| 1055 |
|
| 1056 | vkDestroyShaderModule(g_hDevice, fragShaderModule, nullptr);
|
| 1057 | vkDestroyShaderModule(g_hDevice, hVertShaderModule, nullptr);
|
| 1058 | }
|
| 1059 |
|
| 1060 | // Create frambuffers
|
| 1061 |
|
| 1062 | for(size_t i = g_Framebuffers.size(); i--; )
|
| 1063 | vkDestroyFramebuffer(g_hDevice, g_Framebuffers[i], nullptr);
|
| 1064 | g_Framebuffers.clear();
|
| 1065 |
|
| 1066 | g_Framebuffers.resize(g_SwapchainImageViews.size());
|
| 1067 | for(size_t i = 0; i < g_SwapchainImages.size(); ++i)
|
| 1068 | {
|
| 1069 | VkImageView attachments[] = { g_SwapchainImageViews[i], g_hDepthImageView };
|
| 1070 |
|
| 1071 | VkFramebufferCreateInfo framebufferInfo = { VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO };
|
| 1072 | framebufferInfo.renderPass = g_hRenderPass;
|
| 1073 | framebufferInfo.attachmentCount = (uint32_t)_countof(attachments);
|
| 1074 | framebufferInfo.pAttachments = attachments;
|
| 1075 | framebufferInfo.width = g_Extent.width;
|
| 1076 | framebufferInfo.height = g_Extent.height;
|
| 1077 | framebufferInfo.layers = 1;
|
| 1078 | ERR_GUARD_VULKAN( vkCreateFramebuffer(g_hDevice, &framebufferInfo, nullptr, &g_Framebuffers[i]) );
|
| 1079 | }
|
| 1080 |
|
| 1081 | // Create semaphores
|
| 1082 |
|
| 1083 | if(g_hImageAvailableSemaphore != VK_NULL_HANDLE)
|
| 1084 | {
|
| 1085 | vkDestroySemaphore(g_hDevice, g_hImageAvailableSemaphore, nullptr);
|
| 1086 | g_hImageAvailableSemaphore = VK_NULL_HANDLE;
|
| 1087 | }
|
| 1088 | if(g_hRenderFinishedSemaphore != VK_NULL_HANDLE)
|
| 1089 | {
|
| 1090 | vkDestroySemaphore(g_hDevice, g_hRenderFinishedSemaphore, nullptr);
|
| 1091 | g_hRenderFinishedSemaphore = VK_NULL_HANDLE;
|
| 1092 | }
|
| 1093 |
|
| 1094 | VkSemaphoreCreateInfo semaphoreInfo = { VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO };
|
| 1095 | ERR_GUARD_VULKAN( vkCreateSemaphore(g_hDevice, &semaphoreInfo, nullptr, &g_hImageAvailableSemaphore) );
|
| 1096 | ERR_GUARD_VULKAN( vkCreateSemaphore(g_hDevice, &semaphoreInfo, nullptr, &g_hRenderFinishedSemaphore) );
|
| 1097 | }
|
| 1098 |
|
| 1099 | static void DestroySwapchain(bool destroyActualSwapchain)
|
| 1100 | {
|
| 1101 | if(g_hImageAvailableSemaphore != VK_NULL_HANDLE)
|
| 1102 | {
|
| 1103 | vkDestroySemaphore(g_hDevice, g_hImageAvailableSemaphore, nullptr);
|
| 1104 | g_hImageAvailableSemaphore = VK_NULL_HANDLE;
|
| 1105 | }
|
| 1106 | if(g_hRenderFinishedSemaphore != VK_NULL_HANDLE)
|
| 1107 | {
|
| 1108 | vkDestroySemaphore(g_hDevice, g_hRenderFinishedSemaphore, nullptr);
|
| 1109 | g_hRenderFinishedSemaphore = VK_NULL_HANDLE;
|
| 1110 | }
|
| 1111 |
|
| 1112 | for(size_t i = g_Framebuffers.size(); i--; )
|
| 1113 | vkDestroyFramebuffer(g_hDevice, g_Framebuffers[i], nullptr);
|
| 1114 | g_Framebuffers.clear();
|
| 1115 |
|
| 1116 | if(g_hDepthImageView != VK_NULL_HANDLE)
|
| 1117 | {
|
| 1118 | vkDestroyImageView(g_hDevice, g_hDepthImageView, nullptr);
|
| 1119 | g_hDepthImageView = VK_NULL_HANDLE;
|
| 1120 | }
|
| 1121 | if(g_hDepthImage != VK_NULL_HANDLE)
|
| 1122 | {
|
Adam Sawicki | 819860e | 2017-07-04 14:30:38 +0200 | [diff] [blame] | 1123 | vmaDestroyImage(g_hAllocator, g_hDepthImage, g_hDepthImageAlloc);
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1124 | g_hDepthImage = VK_NULL_HANDLE;
|
| 1125 | }
|
| 1126 |
|
| 1127 | if(g_hPipeline != VK_NULL_HANDLE)
|
| 1128 | {
|
| 1129 | vkDestroyPipeline(g_hDevice, g_hPipeline, nullptr);
|
| 1130 | g_hPipeline = VK_NULL_HANDLE;
|
| 1131 | }
|
| 1132 |
|
| 1133 | if(g_hRenderPass != VK_NULL_HANDLE)
|
| 1134 | {
|
| 1135 | vkDestroyRenderPass(g_hDevice, g_hRenderPass, nullptr);
|
| 1136 | g_hRenderPass = VK_NULL_HANDLE;
|
| 1137 | }
|
| 1138 |
|
| 1139 | if(g_hPipelineLayout != VK_NULL_HANDLE)
|
| 1140 | {
|
| 1141 | vkDestroyPipelineLayout(g_hDevice, g_hPipelineLayout, nullptr);
|
| 1142 | g_hPipelineLayout = VK_NULL_HANDLE;
|
| 1143 | }
|
| 1144 |
|
| 1145 | for(size_t i = g_SwapchainImageViews.size(); i--; )
|
| 1146 | vkDestroyImageView(g_hDevice, g_SwapchainImageViews[i], nullptr);
|
| 1147 | g_SwapchainImageViews.clear();
|
| 1148 |
|
| 1149 | if(destroyActualSwapchain && (g_hSwapchain != VK_NULL_HANDLE))
|
| 1150 | {
|
| 1151 | vkDestroySwapchainKHR(g_hDevice, g_hSwapchain, nullptr);
|
| 1152 | g_hSwapchain = VK_NULL_HANDLE;
|
| 1153 | }
|
| 1154 | }
|
| 1155 |
|
| 1156 | static void InitializeApplication()
|
| 1157 | {
|
| 1158 | uint32_t instanceLayerPropCount = 0;
|
| 1159 | ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, nullptr) );
|
| 1160 | std::vector<VkLayerProperties> instanceLayerProps(instanceLayerPropCount);
|
| 1161 | if(instanceLayerPropCount > 0)
|
| 1162 | {
|
| 1163 | ERR_GUARD_VULKAN( vkEnumerateInstanceLayerProperties(&instanceLayerPropCount, instanceLayerProps.data()) );
|
| 1164 | }
|
| 1165 |
|
| 1166 | if(g_EnableValidationLayer == true)
|
| 1167 | {
|
| 1168 | if(IsLayerSupported(instanceLayerProps.data(), instanceLayerProps.size(), VALIDATION_LAYER_NAME) == false)
|
| 1169 | {
|
| 1170 | printf("Layer \"%s\" not supported.", VALIDATION_LAYER_NAME);
|
| 1171 | g_EnableValidationLayer = false;
|
| 1172 | }
|
| 1173 | }
|
| 1174 |
|
| 1175 | std::vector<const char*> instanceExtensions;
|
| 1176 | instanceExtensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
|
| 1177 | instanceExtensions.push_back(VK_KHR_WIN32_SURFACE_EXTENSION_NAME);
|
| 1178 |
|
| 1179 | std::vector<const char*> instanceLayers;
|
| 1180 | if(g_EnableValidationLayer == true)
|
| 1181 | {
|
| 1182 | instanceLayers.push_back(VALIDATION_LAYER_NAME);
|
| 1183 | instanceExtensions.push_back("VK_EXT_debug_report");
|
| 1184 | }
|
| 1185 |
|
| 1186 | VkApplicationInfo appInfo = { VK_STRUCTURE_TYPE_APPLICATION_INFO };
|
| 1187 | appInfo.pApplicationName = APP_TITLE_A;
|
| 1188 | appInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
|
| 1189 | appInfo.pEngineName = "Adam Sawicki Engine";
|
| 1190 | appInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
|
| 1191 | appInfo.apiVersion = VK_API_VERSION_1_0;
|
| 1192 |
|
| 1193 | VkInstanceCreateInfo instInfo = { VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO };
|
| 1194 | instInfo.pApplicationInfo = &appInfo;
|
| 1195 | instInfo.enabledExtensionCount = static_cast<uint32_t>(instanceExtensions.size());
|
| 1196 | instInfo.ppEnabledExtensionNames = instanceExtensions.data();
|
| 1197 | instInfo.enabledLayerCount = static_cast<uint32_t>(instanceLayers.size());
|
| 1198 | instInfo.ppEnabledLayerNames = instanceLayers.data();
|
| 1199 |
|
| 1200 | ERR_GUARD_VULKAN( vkCreateInstance(&instInfo, NULL, &g_hVulkanInstance) );
|
| 1201 |
|
| 1202 | // Create VkSurfaceKHR.
|
| 1203 | VkWin32SurfaceCreateInfoKHR surfaceInfo = { VK_STRUCTURE_TYPE_WIN32_SURFACE_CREATE_INFO_KHR };
|
| 1204 | surfaceInfo.hinstance = g_hAppInstance;
|
| 1205 | surfaceInfo.hwnd = g_hWnd;
|
| 1206 | VkResult result = vkCreateWin32SurfaceKHR(g_hVulkanInstance, &surfaceInfo, NULL, &g_hSurface);
|
| 1207 | assert(result == VK_SUCCESS);
|
| 1208 |
|
| 1209 | if(g_EnableValidationLayer == true)
|
| 1210 | RegisterDebugCallbacks();
|
| 1211 |
|
| 1212 | // Find physical device
|
| 1213 |
|
| 1214 | uint32_t deviceCount = 0;
|
| 1215 | ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, nullptr) );
|
| 1216 | assert(deviceCount > 0);
|
| 1217 |
|
| 1218 | std::vector<VkPhysicalDevice> physicalDevices(deviceCount);
|
| 1219 | ERR_GUARD_VULKAN( vkEnumeratePhysicalDevices(g_hVulkanInstance, &deviceCount, physicalDevices.data()) );
|
| 1220 |
|
| 1221 | g_hPhysicalDevice = physicalDevices[0];
|
| 1222 |
|
| 1223 | // Query for features
|
| 1224 |
|
| 1225 | VkPhysicalDeviceProperties physicalDeviceProperties = {};
|
| 1226 | vkGetPhysicalDeviceProperties(g_hPhysicalDevice, &physicalDeviceProperties);
|
| 1227 |
|
| 1228 | //VkPhysicalDeviceFeatures physicalDeviceFreatures = {};
|
| 1229 | //vkGetPhysicalDeviceFeatures(g_PhysicalDevice, &physicalDeviceFreatures);
|
| 1230 |
|
| 1231 | // Find queue family index
|
| 1232 |
|
| 1233 | uint32_t queueFamilyCount = 0;
|
| 1234 | vkGetPhysicalDeviceQueueFamilyProperties(g_hPhysicalDevice, &queueFamilyCount, nullptr);
|
| 1235 | assert(queueFamilyCount > 0);
|
| 1236 | std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);
|
| 1237 | vkGetPhysicalDeviceQueueFamilyProperties(g_hPhysicalDevice, &queueFamilyCount, queueFamilies.data());
|
| 1238 | for(uint32_t i = 0;
|
| 1239 | (i < queueFamilyCount) &&
|
| 1240 | (g_GraphicsQueueFamilyIndex == UINT_MAX || g_PresentQueueFamilyIndex == UINT_MAX);
|
| 1241 | ++i)
|
| 1242 | {
|
| 1243 | if(queueFamilies[i].queueCount > 0)
|
| 1244 | {
|
| 1245 | if((g_GraphicsQueueFamilyIndex != 0) &&
|
| 1246 | ((queueFamilies[i].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0))
|
| 1247 | {
|
| 1248 | g_GraphicsQueueFamilyIndex = i;
|
| 1249 | }
|
| 1250 |
|
| 1251 | VkBool32 surfaceSupported = 0;
|
| 1252 | VkResult res = vkGetPhysicalDeviceSurfaceSupportKHR(g_hPhysicalDevice, i, g_hSurface, &surfaceSupported);
|
| 1253 | if((res >= 0) && (surfaceSupported == VK_TRUE))
|
| 1254 | {
|
| 1255 | g_PresentQueueFamilyIndex = i;
|
| 1256 | }
|
| 1257 | }
|
| 1258 | }
|
| 1259 | assert(g_GraphicsQueueFamilyIndex != UINT_MAX);
|
| 1260 |
|
| 1261 | // Create logical device
|
| 1262 |
|
| 1263 | const float queuePriority = 1.f;
|
| 1264 |
|
| 1265 | VkDeviceQueueCreateInfo deviceQueueCreateInfo[2] = { VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO };
|
| 1266 | deviceQueueCreateInfo[0].queueFamilyIndex = g_GraphicsQueueFamilyIndex;
|
| 1267 | deviceQueueCreateInfo[0].queueCount = 1;
|
| 1268 | deviceQueueCreateInfo[0].pQueuePriorities = &queuePriority;
|
| 1269 | deviceQueueCreateInfo[1].sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO;
|
| 1270 | deviceQueueCreateInfo[1].queueFamilyIndex = g_PresentQueueFamilyIndex;
|
| 1271 | deviceQueueCreateInfo[1].queueCount = 1;
|
| 1272 | deviceQueueCreateInfo[1].pQueuePriorities = &queuePriority;
|
| 1273 |
|
| 1274 | VkPhysicalDeviceFeatures deviceFeatures = {};
|
| 1275 | deviceFeatures.fillModeNonSolid = VK_TRUE;
|
| 1276 | deviceFeatures.samplerAnisotropy = VK_TRUE;
|
| 1277 |
|
| 1278 | std::vector<const char*> enabledDeviceExtensions;
|
| 1279 | enabledDeviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
|
| 1280 |
|
| 1281 | VkDeviceCreateInfo deviceCreateInfo = { VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO };
|
| 1282 | deviceCreateInfo.enabledLayerCount = 0;
|
| 1283 | deviceCreateInfo.ppEnabledLayerNames = nullptr;
|
| 1284 | deviceCreateInfo.enabledExtensionCount = (uint32_t)enabledDeviceExtensions.size();
|
| 1285 | deviceCreateInfo.ppEnabledExtensionNames = enabledDeviceExtensions.data();
|
| 1286 | deviceCreateInfo.queueCreateInfoCount = g_PresentQueueFamilyIndex != g_GraphicsQueueFamilyIndex ? 2 : 1;
|
| 1287 | deviceCreateInfo.pQueueCreateInfos = deviceQueueCreateInfo;
|
| 1288 | deviceCreateInfo.pEnabledFeatures = &deviceFeatures;
|
| 1289 |
|
| 1290 | ERR_GUARD_VULKAN( vkCreateDevice(g_hPhysicalDevice, &deviceCreateInfo, nullptr, &g_hDevice) );
|
| 1291 |
|
| 1292 | // Create memory allocator
|
| 1293 |
|
| 1294 | VmaAllocatorCreateInfo allocatorInfo = {};
|
| 1295 | allocatorInfo.physicalDevice = g_hPhysicalDevice;
|
| 1296 | allocatorInfo.device = g_hDevice;
|
| 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:
|
| 1707 | if(wParam == VK_ESCAPE)
|
| 1708 | PostMessage(hWnd, WM_CLOSE, 0, 0);
|
| 1709 | return 0;
|
| 1710 |
|
| 1711 | default:
|
| 1712 | break;
|
| 1713 | }
|
| 1714 |
|
| 1715 | return DefWindowProc(hWnd, msg, wParam, lParam);
|
| 1716 | }
|
| 1717 |
|
| 1718 | int main()
|
| 1719 | {
|
| 1720 | g_hAppInstance = (HINSTANCE)GetModuleHandle(NULL);
|
| 1721 |
|
| 1722 | WNDCLASSEX wndClassDesc = { sizeof(WNDCLASSEX) };
|
| 1723 | wndClassDesc.style = CS_VREDRAW | CS_HREDRAW | CS_DBLCLKS;
|
| 1724 | wndClassDesc.hbrBackground = NULL;
|
| 1725 | wndClassDesc.hCursor = LoadCursor(NULL, IDC_CROSS);
|
| 1726 | wndClassDesc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
|
| 1727 | wndClassDesc.hInstance = g_hAppInstance;
|
| 1728 | wndClassDesc.lpfnWndProc = WndProc;
|
| 1729 | wndClassDesc.lpszClassName = WINDOW_CLASS_NAME;
|
| 1730 |
|
| 1731 | const ATOM hWndClass = RegisterClassEx(&wndClassDesc);
|
| 1732 | assert(hWndClass);
|
| 1733 |
|
| 1734 | const DWORD style = WS_VISIBLE | WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME;
|
| 1735 | const DWORD exStyle = 0;
|
| 1736 |
|
| 1737 | RECT rect = { 0, 0, g_SizeX, g_SizeY };
|
| 1738 | AdjustWindowRectEx(&rect, style, FALSE, exStyle);
|
| 1739 |
|
Adam Sawicki | 86ccd63 | 2017-07-04 14:57:53 +0200 | [diff] [blame] | 1740 | CreateWindowEx(
|
Adam Sawicki | e6e498f | 2017-06-16 17:21:31 +0200 | [diff] [blame] | 1741 | exStyle, WINDOW_CLASS_NAME, APP_TITLE_W, style,
|
| 1742 | CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
|
| 1743 | NULL, NULL, g_hAppInstance, NULL);
|
| 1744 |
|
| 1745 | MSG msg;
|
| 1746 | for(;;)
|
| 1747 | {
|
| 1748 | if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
|
| 1749 | {
|
| 1750 | if(msg.message == WM_QUIT)
|
| 1751 | break;
|
| 1752 | TranslateMessage(&msg);
|
| 1753 | DispatchMessage(&msg);
|
| 1754 | }
|
| 1755 | if(g_hDevice != VK_NULL_HANDLE)
|
| 1756 | DrawFrame();
|
| 1757 | }
|
| 1758 |
|
| 1759 | return 0;
|
| 1760 | }
|
Adam Sawicki | 59a3e7e | 2017-08-21 15:47:30 +0200 | [diff] [blame] | 1761 |
|
| 1762 | #else // #ifdef WIN32
|
| 1763 |
|
| 1764 | #define VMA_IMPLEMENTATION
|
| 1765 | #include "vk_mem_alloc.h"
|
| 1766 |
|
| 1767 | int main()
|
| 1768 | {
|
| 1769 | }
|
| 1770 |
|
| 1771 | #endif // #ifdef WIN32
|
| 1772 |
|