Changeset 621664a in opengl-game
- Timestamp:
- Jul 29, 2019, 4:01:12 AM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- fba08f2
- Parents:
- f5d5686
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
rf5d5686 r621664a 97 97 98 98 struct UniformBufferObject { 99 glm::mat4 model;100 glm::mat4 view;101 glm::mat4 proj;99 alignas(16) glm::mat4 model; 100 alignas(16) glm::mat4 view; 101 alignas(16) glm::mat4 proj; 102 102 }; 103 103 … … 110 110 111 111 const vector<uint16_t> indices = { 112 0, 1, 2, 2, 3, 0 112 0, 1, 2, 2, 3, 0 113 113 }; 114 114 … … 117 117 const VkAllocationCallbacks* pAllocator, 118 118 VkDebugUtilsMessengerEXT* pDebugMessenger) { 119 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr( 120 instance, "vkCreateDebugUtilsMessengerEXT"); 119 auto func = (PFN_vkCreateDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkCreateDebugUtilsMessengerEXT"); 121 120 122 121 if (func != nullptr) { … … 130 129 VkDebugUtilsMessengerEXT debugMessenger, 131 130 const VkAllocationCallbacks* pAllocator) { 132 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr( 133 instance, "vkDestroyDebugUtilsMessengerEXT"); 131 auto func = (PFN_vkDestroyDebugUtilsMessengerEXT) vkGetInstanceProcAddr(instance, "vkDestroyDebugUtilsMessengerEXT"); 134 132 135 133 if (func != nullptr) { … … 148 146 cleanup(); 149 147 } 148 150 149 private: 151 150 GameGui* gui = new GameGui_SDL(); … … 167 166 VkFormat swapChainImageFormat; 168 167 VkExtent2D swapChainExtent; 169 170 168 vector<VkImageView> swapChainImageViews; 169 vector<VkFramebuffer> swapChainFramebuffers; 170 171 171 VkRenderPass renderPass; 172 172 VkDescriptorSetLayout descriptorSetLayout; … … 190 190 vector<VkDeviceMemory> uniformBuffersMemory; 191 191 192 vector<VkFramebuffer> swapChainFramebuffers;193 192 vector<VkCommandBuffer> commandBuffers; 194 193 … … 240 239 } 241 240 242 void recreateSwapChain() {243 int width = 0, height = 0;244 gui->GetWindowSize(&width, &height);245 246 while (width == 0 || height == 0 ||247 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) {248 SDL_WaitEvent(nullptr);249 gui->GetWindowSize(&width, &height);250 }251 252 vkDeviceWaitIdle(device);253 254 cleanupSwapChain();255 256 createSwapChain();257 createImageViews();258 createRenderPass();259 createGraphicsPipeline();260 createFramebuffers();261 createUniformBuffers();262 createDescriptorPool();263 createDescriptorSets();264 createCommandBuffers();265 }266 267 void cleanupSwapChain() {268 for (auto framebuffer : swapChainFramebuffers) {269 vkDestroyFramebuffer(device, framebuffer, nullptr);270 }271 272 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());273 274 vkDestroyPipeline(device, graphicsPipeline, nullptr);275 vkDestroyPipelineLayout(device, pipelineLayout, nullptr);276 vkDestroyRenderPass(device, renderPass, nullptr);277 278 for (auto imageView : swapChainImageViews) {279 vkDestroyImageView(device, imageView, nullptr);280 }281 282 vkDestroySwapchainKHR(device, swapChain, nullptr);283 284 for (size_t i = 0; i < swapChainImages.size(); i++) {285 vkDestroyBuffer(device, uniformBuffers[i], nullptr);286 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);287 }288 289 vkDestroyDescriptorPool(device, descriptorPool, nullptr);290 }291 292 241 void createInstance() { 293 242 if (enableValidationLayers && !checkValidationLayerSupport()) { … … 335 284 } 336 285 286 bool checkValidationLayerSupport() { 287 uint32_t layerCount; 288 vkEnumerateInstanceLayerProperties(&layerCount, nullptr); 289 290 vector<VkLayerProperties> availableLayers(layerCount); 291 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data()); 292 293 for (const char* layerName : validationLayers) { 294 bool layerFound = false; 295 296 for (const auto& layerProperties : availableLayers) { 297 if (strcmp(layerName, layerProperties.layerName) == 0) { 298 layerFound = true; 299 break; 300 } 301 } 302 303 if (!layerFound) { 304 return false; 305 } 306 } 307 308 return true; 309 } 310 311 vector<const char*> getRequiredExtensions() { 312 vector<const char*> extensions = gui->GetRequiredExtensions(); 313 314 if (enableValidationLayers) { 315 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME); 316 } 317 318 return extensions; 319 } 320 337 321 void setupDebugMessenger() { 338 322 if (!enableValidationLayers) return; … … 342 326 343 327 if (CreateDebugUtilsMessengerEXT(instance, &createInfo, nullptr, &debugMessenger) != VK_SUCCESS) { 344 throw runtime_error("failed to setup debug messenger!"); 345 } 328 throw runtime_error("failed to set up debug messenger!"); 329 } 330 } 331 332 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) { 333 createInfo = {}; 334 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT; 335 createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT; 336 createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT; 337 createInfo.pfnUserCallback = debugCallback; 346 338 } 347 339 … … 350 342 351 343 if (sdlSurface == nullptr) { 352 344 cout << "Could not get SDL Surface! =(" << endl; 353 345 } 354 346 … … 393 385 394 386 QueueFamilyIndices indices = findQueueFamilies(device); 395 396 387 bool extensionsSupported = checkDeviceExtensionSupport(device); 397 398 388 bool swapChainAdequate = false; 399 389 … … 431 421 for (uint32_t queueFamily : uniqueQueueFamilies) { 432 422 VkDeviceQueueCreateInfo queueCreateInfo = {}; 433 434 423 queueCreateInfo.sType = VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO; 435 424 queueCreateInfo.queueFamilyIndex = queueFamily; … … 444 433 VkDeviceCreateInfo createInfo = {}; 445 434 createInfo.sType = VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO; 446 447 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size());; 435 createInfo.queueCreateInfoCount = static_cast<uint32_t>(queueCreateInfos.size()); 448 436 createInfo.pQueueCreateInfos = queueCreateInfos.data(); 449 437 … … 470 458 } 471 459 472 bool checkValidationLayerSupport() {473 uint32_t layerCount;474 vkEnumerateInstanceLayerProperties(&layerCount, nullptr);475 476 vector<VkLayerProperties> availableLayers(layerCount);477 vkEnumerateInstanceLayerProperties(&layerCount, availableLayers.data());478 479 for (const char* layerName : validationLayers) {480 bool layerFound = false;481 482 for (const auto& layerProperties : availableLayers) {483 if (strcmp(layerName, layerProperties.layerName) == 0) {484 layerFound = true;485 break;486 }487 }488 489 if (!layerFound) {490 return false;491 }492 }493 494 return true;495 }496 497 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) {498 QueueFamilyIndices indices;499 500 uint32_t queueFamilyCount = 0;501 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr);502 503 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount);504 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());505 506 int i = 0;507 for (const auto& queueFamily : queueFamilies) {508 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) {509 indices.graphicsFamily = i;510 }511 512 VkBool32 presentSupport = false;513 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport);514 515 if (queueFamily.queueCount > 0 && presentSupport) {516 indices.presentFamily = i;517 }518 519 if (indices.isComplete()) {520 break;521 }522 523 i++;524 }525 526 return indices;527 }528 529 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) {530 SwapChainSupportDetails details;531 532 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities);533 534 uint32_t formatCount;535 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr);536 537 if (formatCount != 0) {538 details.formats.resize(formatCount);539 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data());540 }541 542 uint32_t presentModeCount;543 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr);544 545 if (presentModeCount != 0) {546 details.presentModes.resize(presentModeCount);547 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data());548 }549 550 return details;551 }552 553 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) {554 for (const auto& availableFormat : availableFormats) {555 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) {556 return availableFormat;557 }558 }559 560 return availableFormats[0];561 }562 563 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) {564 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR;565 566 for (const auto& availablePresentMode : availablePresentModes) {567 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) {568 return availablePresentMode;569 } else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) {570 bestMode = availablePresentMode;571 }572 }573 574 return bestMode;575 }576 577 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) {578 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) {579 return capabilities.currentExtent;580 } else {581 int width, height;582 gui->GetWindowSize(&width, &height);583 584 VkExtent2D actualExtent = {585 static_cast<uint32_t>(width),586 static_cast<uint32_t>(height)587 };588 589 actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width));590 actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height));591 592 return actualExtent;593 }594 }595 596 void populateDebugMessengerCreateInfo(VkDebugUtilsMessengerCreateInfoEXT& createInfo) {597 createInfo = {};598 createInfo.sType = VK_STRUCTURE_TYPE_DEBUG_UTILS_MESSENGER_CREATE_INFO_EXT;599 createInfo.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;600 createInfo.messageType = VK_DEBUG_UTILS_MESSAGE_TYPE_GENERAL_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_VALIDATION_BIT_EXT | VK_DEBUG_UTILS_MESSAGE_TYPE_PERFORMANCE_BIT_EXT;601 createInfo.pfnUserCallback = debugCallback;602 }603 604 vector<const char*> getRequiredExtensions() {605 vector<const char*> extensions = gui->GetRequiredExtensions();606 607 if (enableValidationLayers) {608 extensions.push_back(VK_EXT_DEBUG_UTILS_EXTENSION_NAME);609 }610 611 return extensions;612 }613 614 460 void createSwapChain() { 615 461 SwapChainSupportDetails swapChainSupport = querySwapChainSupport(physicalDevice); … … 620 466 621 467 uint32_t imageCount = swapChainSupport.capabilities.minImageCount + 1; 622 if (swapChainSupport.capabilities.maxImageCount > 0 && 623 imageCount > swapChainSupport.capabilities.maxImageCount) { 468 if (swapChainSupport.capabilities.maxImageCount > 0 && imageCount > swapChainSupport.capabilities.maxImageCount) { 624 469 imageCount = swapChainSupport.capabilities.maxImageCount; 625 470 } 626 471 627 472 VkSwapchainCreateInfoKHR createInfo = {}; 628 629 473 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; 630 474 createInfo.surface = surface; … … 645 489 } else { 646 490 createInfo.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE; 647 createInfo.queueFamilyIndexCount = 0; // Optional491 createInfo.queueFamilyIndexCount = 0; 648 492 createInfo.pQueueFamilyIndices = nullptr; 649 493 } … … 667 511 } 668 512 513 SwapChainSupportDetails querySwapChainSupport(VkPhysicalDevice device) { 514 SwapChainSupportDetails details; 515 516 vkGetPhysicalDeviceSurfaceCapabilitiesKHR(device, surface, &details.capabilities); 517 518 uint32_t formatCount; 519 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, nullptr); 520 521 if (formatCount != 0) { 522 details.formats.resize(formatCount); 523 vkGetPhysicalDeviceSurfaceFormatsKHR(device, surface, &formatCount, details.formats.data()); 524 } 525 526 uint32_t presentModeCount; 527 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, nullptr); 528 529 if (presentModeCount != 0) { 530 details.presentModes.resize(presentModeCount); 531 vkGetPhysicalDeviceSurfacePresentModesKHR(device, surface, &presentModeCount, details.presentModes.data()); 532 } 533 534 return details; 535 } 536 537 VkSurfaceFormatKHR chooseSwapSurfaceFormat(const vector<VkSurfaceFormatKHR>& availableFormats) { 538 for (const auto& availableFormat : availableFormats) { 539 if (availableFormat.format == VK_FORMAT_B8G8R8A8_UNORM && availableFormat.colorSpace == VK_COLOR_SPACE_SRGB_NONLINEAR_KHR) { 540 return availableFormat; 541 } 542 } 543 544 return availableFormats[0]; 545 } 546 547 VkPresentModeKHR chooseSwapPresentMode(const vector<VkPresentModeKHR>& availablePresentModes) { 548 VkPresentModeKHR bestMode = VK_PRESENT_MODE_FIFO_KHR; 549 550 for (const auto& availablePresentMode : availablePresentModes) { 551 if (availablePresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { 552 return availablePresentMode; 553 } 554 else if (availablePresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { 555 bestMode = availablePresentMode; 556 } 557 } 558 559 return bestMode; 560 } 561 562 VkExtent2D chooseSwapExtent(const VkSurfaceCapabilitiesKHR& capabilities) { 563 if (capabilities.currentExtent.width != numeric_limits<uint32_t>::max()) { 564 return capabilities.currentExtent; 565 } 566 else { 567 int width, height; 568 gui->GetWindowSize(&width, &height); 569 570 VkExtent2D actualExtent = { 571 static_cast<uint32_t>(width), 572 static_cast<uint32_t>(height) 573 }; 574 575 actualExtent.width = max(capabilities.minImageExtent.width, min(capabilities.maxImageExtent.width, actualExtent.width)); 576 actualExtent.height = max(capabilities.minImageExtent.height, min(capabilities.maxImageExtent.height, actualExtent.height)); 577 578 return actualExtent; 579 } 580 } 581 669 582 void createImageViews() { 670 583 swapChainImageViews.resize(swapChainImages.size()); 671 584 672 for (size_t i =0; i<swapChainImages.size(); i++) {585 for (size_t i = 0; i < swapChainImages.size(); i++) { 673 586 VkImageViewCreateInfo createInfo = {}; 674 587 createInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; 675 588 createInfo.image = swapChainImages[i]; 676 677 589 createInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; 678 590 createInfo.format = swapChainImageFormat; … … 715 627 subpass.pColorAttachments = &colorAttachmentRef; 716 628 717 VkSubpassDependency 629 VkSubpassDependency dependency = {}; 718 630 dependency.srcSubpass = VK_SUBPASS_EXTERNAL; 719 631 dependency.dstSubpass = 0; … … 819 731 rasterizer.cullMode = VK_CULL_MODE_BACK_BIT; 820 732 rasterizer.frontFace = VK_FRONT_FACE_COUNTER_CLOCKWISE; 821 rasterizer.depthBiasEnable = false;733 rasterizer.depthBiasEnable = VK_FALSE; 822 734 823 735 VkPipelineMultisampleStateCreateInfo multisampling = {}; … … 923 835 924 836 if (vkCreateCommandPool(device, &poolInfo, nullptr, &commandPool) != VK_SUCCESS) { 925 throw runtime_error("failed to create command pool!"); 926 } 837 throw runtime_error("failed to create graphics command pool!"); 838 } 839 } 840 841 QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) { 842 QueueFamilyIndices indices; 843 844 uint32_t queueFamilyCount = 0; 845 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); 846 847 vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount); 848 vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data()); 849 850 int i = 0; 851 for (const auto& queueFamily : queueFamilies) { 852 if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { 853 indices.graphicsFamily = i; 854 } 855 856 VkBool32 presentSupport = false; 857 vkGetPhysicalDeviceSurfaceSupportKHR(device, i, surface, &presentSupport); 858 859 if (queueFamily.queueCount > 0 && presentSupport) { 860 indices.presentFamily = i; 861 } 862 863 if (indices.isComplete()) { 864 break; 865 } 866 867 i++; 868 } 869 870 return indices; 927 871 } 928 872 … … 964 908 } 965 909 966 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, 967 VkImageUsageFlags usage, VkMemoryPropertyFlags properties, VkImage& image, 968 VkDeviceMemory& imageMemory) { 910 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, 911 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) { 969 912 VkImageCreateInfo imageInfo = {}; 970 913 imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; … … 989 932 vkGetImageMemoryRequirements(device, image, &memRequirements); 990 933 991 VkMemoryAllocateInfo allocInfo ;934 VkMemoryAllocateInfo allocInfo = {}; 992 935 allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; 993 936 allocInfo.allocationSize = memRequirements.size; … … 1001 944 } 1002 945 1003 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, 1004 VkImageLayout newLayout) { 946 void transitionImageLayout(VkImage image, VkFormat format, VkImageLayout oldLayout, VkImageLayout newLayout) { 1005 947 VkCommandBuffer commandBuffer = beginSingleTimeCommands(); 1006 948 … … 1080 1022 VkBuffer stagingBuffer; 1081 1023 VkDeviceMemory stagingBufferMemory; 1082 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 1024 createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, 1083 1025 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1084 1026 stagingBuffer, stagingBufferMemory); … … 1086 1028 void* data; 1087 1029 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 1088 memcpy(data, vertices.data(), (size_t) bufferSize);1030 memcpy(data, vertices.data(), (size_t) bufferSize); 1089 1031 vkUnmapMemory(device, stagingBufferMemory); 1090 1032 … … 1109 1051 void* data; 1110 1052 vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, &data); 1111 memcpy(data, indices.data(), (size_t) bufferSize);1053 memcpy(data, indices.data(), (size_t) bufferSize); 1112 1054 vkUnmapMemory(device, stagingBufferMemory); 1113 1055 … … 1121 1063 } 1122 1064 1123 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, 1124 VkBuffer& buffer, VkDeviceMemory& bufferMemory) { 1065 void createUniformBuffers() { 1066 VkDeviceSize bufferSize = sizeof(UniformBufferObject); 1067 1068 uniformBuffers.resize(swapChainImages.size()); 1069 uniformBuffersMemory.resize(swapChainImages.size()); 1070 1071 for (size_t i = 0; i < swapChainImages.size(); i++) { 1072 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT, 1073 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, 1074 uniformBuffers[i], uniformBuffersMemory[i]); 1075 } 1076 } 1077 1078 void createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VkMemoryPropertyFlags properties, VkBuffer& buffer, VkDeviceMemory& bufferMemory) { 1125 1079 VkBufferCreateInfo bufferInfo = {}; 1126 1080 bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO; … … 1140 1094 allocInfo.allocationSize = memRequirements.size; 1141 1095 allocInfo.memoryTypeIndex = findMemoryType(memRequirements.memoryTypeBits, properties); 1142 1096 1143 1097 if (vkAllocateMemory(device, &allocInfo, nullptr, &bufferMemory) != VK_SUCCESS) { 1144 1098 throw runtime_error("failed to allocate buffer memory!"); … … 1204 1158 } 1205 1159 1206 void createUniformBuffers() {1207 VkDeviceSize bufferSize = sizeof(UniformBufferObject);1208 1209 uniformBuffers.resize(swapChainImages.size());1210 uniformBuffersMemory.resize(swapChainImages.size());1211 1212 for (size_t i = 0; i < swapChainImages.size(); i++) {1213 createBuffer(bufferSize, VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT,1214 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,1215 uniformBuffers[i], uniformBuffersMemory[i]);1216 }1217 }1218 1219 1160 void createDescriptorPool() { 1220 1161 VkDescriptorPoolSize poolSize = {}; … … 1235 1176 void createDescriptorSets() { 1236 1177 vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout); 1178 1237 1179 VkDescriptorSetAllocateInfo allocInfo = {}; 1238 1180 allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO; … … 1274 1216 allocInfo.commandPool = commandPool; 1275 1217 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 1276 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size();1218 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size(); 1277 1219 1278 1220 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) { 1279 throw runtime_error("failed to create command buffers!");1221 throw runtime_error("failed to allocate command buffers!"); 1280 1222 } 1281 1223 … … 1302 1244 1303 1245 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 1246 1304 1247 vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, graphicsPipeline); 1305 1248 … … 1312 1255 1313 1256 vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(indices.size()), 1, 0, 0, 0); 1257 1314 1258 vkCmdEndRenderPass(commandBuffers[i]); 1315 1259 … … 1380 1324 uint32_t imageIndex; 1381 1325 1382 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 1326 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), 1327 imageAvailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 1383 1328 1384 1329 if (result == VK_ERROR_OUT_OF_DATE_KHR) { … … 1416 1361 VkPresentInfoKHR presentInfo = {}; 1417 1362 presentInfo.sType = VK_STRUCTURE_TYPE_PRESENT_INFO_KHR; 1418 1419 1363 presentInfo.waitSemaphoreCount = 1; 1420 1364 presentInfo.pWaitSemaphores = signalSemaphores; … … 1447 1391 ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f)); 1448 1392 ubo.view = glm::lookAt(glm::vec3(2.0f, 2.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 1.0f)); 1449 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float) 1393 ubo.proj = glm::perspective(glm::radians(45.0f), swapChainExtent.width / (float)swapChainExtent.height, 0.1f, 10.0f); 1450 1394 ubo.proj[1][1] *= -1; 1451 1395 … … 1456 1400 } 1457 1401 1402 void recreateSwapChain() { 1403 int width = 0, height = 0; 1404 1405 gui->GetWindowSize(&width, &height); 1406 1407 while (width == 0 || height == 0 || 1408 (SDL_GetWindowFlags(window) & SDL_WINDOW_MINIMIZED) != 0) { 1409 SDL_WaitEvent(nullptr); 1410 gui->GetWindowSize(&width, &height); 1411 } 1412 1413 vkDeviceWaitIdle(device); 1414 1415 cleanupSwapChain(); 1416 1417 createSwapChain(); 1418 createImageViews(); 1419 createRenderPass(); 1420 createGraphicsPipeline(); 1421 createFramebuffers(); 1422 createUniformBuffers(); 1423 createDescriptorPool(); 1424 createDescriptorSets(); 1425 createCommandBuffers(); 1426 } 1427 1458 1428 void cleanup() { 1459 1429 cleanupSwapChain(); … … 1471 1441 1472 1442 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { 1443 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr); 1473 1444 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); 1474 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr);1475 1445 vkDestroyFence(device, inFlightFences[i], nullptr); 1476 1446 } … … 1492 1462 } 1493 1463 1464 void cleanupSwapChain() { 1465 for (auto framebuffer : swapChainFramebuffers) { 1466 vkDestroyFramebuffer(device, framebuffer, nullptr); 1467 } 1468 1469 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 1470 1471 vkDestroyPipeline(device, graphicsPipeline, nullptr); 1472 vkDestroyPipelineLayout(device, pipelineLayout, nullptr); 1473 vkDestroyRenderPass(device, renderPass, nullptr); 1474 1475 for (auto imageView : swapChainImageViews) { 1476 vkDestroyImageView(device, imageView, nullptr); 1477 } 1478 1479 vkDestroySwapchainKHR(device, swapChain, nullptr); 1480 1481 for (size_t i = 0; i < swapChainImages.size(); i++) { 1482 vkDestroyBuffer(device, uniformBuffers[i], nullptr); 1483 vkFreeMemory(device, uniformBuffersMemory[i], nullptr); 1484 } 1485 1486 vkDestroyDescriptorPool(device, descriptorPool, nullptr); 1487 } 1488 1494 1489 static VKAPI_ATTR VkBool32 VKAPI_CALL debugCallback( 1495 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,1496 VkDebugUtilsMessageTypeFlagsEXT messageType,1497 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData,1498 void* pUserData) {1490 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity, 1491 VkDebugUtilsMessageTypeFlagsEXT messageType, 1492 const VkDebugUtilsMessengerCallbackDataEXT* pCallbackData, 1493 void* pUserData) { 1499 1494 cerr << "validation layer: " << pCallbackData->pMessage << endl; 1500 1495 … … 1509 1504 } 1510 1505 1511 size_t fileSize = (size_t) file.tellg();1506 size_t fileSize = (size_t) file.tellg(); 1512 1507 vector<char> buffer(fileSize); 1513 1508
Note:
See TracChangeset
for help on using the changeset viewer.