Changeset 3f32dfd in opengl-game
- Timestamp:
- Jan 24, 2021, 5:56:07 PM (4 years ago)
- Branches:
- feature/imgui-sdl
- Children:
- ce9dc9f
- Parents:
- 7f60b28
- git-author:
- Dmitry Portnoy <dportnoy@…> (01/24/21 17:47:00)
- git-committer:
- Dmitry Portnoy <dportnoy@…> (01/24/21 17:56:07)
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
r7f60b28 r3f32dfd 18 18 19 19 using namespace std; 20 21 // TODO: Update all instances of the "... != VK_SUCCESS" check to something similar to 22 // the agp error checking function, which prints an appropriate error message based on the error code 20 23 21 24 // TODO: Update all occurances of instance variables to use this-> (Actually, not sure if I really want to do this) … … 44 47 } 45 48 46 VulkanGame::VulkanGame(int maxFramesInFlight) : MAX_FRAMES_IN_FLIGHT(maxFramesInFlight) { 49 VulkanGame::VulkanGame() { 50 // TODO: Double-check whether initialization should happen in the header, where the variables are declared, or here 51 // Also, decide whether to use this-> for all instance variables, or only when necessary 52 53 this->debugMessenger = VK_NULL_HANDLE; 54 47 55 this->gui = nullptr; 48 56 this->window = nullptr; 49 57 50 this->debugMessenger = VK_NULL_HANDLE; 58 this->swapChainPresentMode = VK_PRESENT_MODE_MAX_ENUM_KHR; 59 this->swapChainMinImageCount = 0; 51 60 52 61 this->currentFrame = 0; … … 200 209 pickPhysicalDevice(deviceExtensions); 201 210 createLogicalDevice(validationLayers, deviceExtensions); 211 chooseSwapChainProperties(); 202 212 createSwapChain(); 203 213 createImageViews(); 204 214 createRenderPass(); 215 createResourceCommandPool(); 205 216 createCommandPool(); 206 217 … … 226 237 //ImGui::StyleColorsClassic(); 227 238 228 // TODO: Ma ke call this once and save the results since it's also called when creating the logical device239 // TODO: Maybe call this once and save the results since it's also called when creating the logical device 229 240 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 230 241 … … 234 245 init_info.PhysicalDevice = this->physicalDevice; 235 246 init_info.Device = this->device; 236 init_info.PipelineCache = VK_NULL_HANDLE; 247 init_info.QueueFamily = indices.graphicsFamily.value(); 248 init_info.Queue = graphicsQueue; 237 249 init_info.DescriptorPool = this->imguiDescriptorPool; // TODO: Create a descriptor pool for IMGUI 238 250 init_info.Allocator = nullptr; 239 init_info.MinImageCount = this->swapChain ImageCount;240 init_info.ImageCount = this->swapChainImage s.size();251 init_info.MinImageCount = this->swapChainMinImageCount; 252 init_info.ImageCount = this->swapChainImageCount; 241 253 init_info.CheckVkResultFn = check_vk_result; 242 254 ImGui_ImplVulkan_Init(&init_info, this->renderPass); … … 251 263 VkResult err; 252 264 253 // Use any command queue254 VkCommandPool command_pool = this->commandPool; // TODO: No need to create a separate variable. Just use this->commandPool directly255 265 VkCommandBuffer command_buffer; 256 266 … … 258 268 VkCommandBufferAllocateInfo info = {}; 259 269 info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; 260 info.commandPool = command_pool;270 info.commandPool = resourceCommandPool; 261 271 info.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 262 272 info.commandBufferCount = 1; … … 288 298 289 299 // This should make the command pool reusable for later 290 err = vkResetCommandPool(this->device, command_pool, 0);300 err = vkResetCommandPool(this->device, resourceCommandPool, 0); 291 301 check_vk_result(err); 292 302 } … … 918 928 919 929 // Copy the UI image to a vulkan texture 930 // TODO: I'm pretty sure this severely slows down the pipeline since this functions waits for the copy to be 931 // complete before continuing. See if I can find a more efficient method. 920 932 VulkanUtils::populateVulkanImageFromSDLTexture(device, physicalDevice, commandPool, uiOverlay, renderer, 921 933 sdlOverlayImage, graphicsQueue); … … 923 935 renderScene(); 924 936 } 925 926 // TODO: Should probably check the returned result927 vkDeviceWaitIdle(device);928 937 } 929 938 … … 931 940 // which are already handled by updateObject(). Move this code to a different place, 932 941 // where it will run just once per frame 933 void VulkanGame::updateScene( uint32_t currentImage) {942 void VulkanGame::updateScene() { 934 943 for (SceneObject<ModelVertex, SSBO_ModelObject>& model : this->modelObjects) { 935 944 model.model_transform = … … 1110 1119 explosion_UBO.cur_time = curTime; 1111 1120 1112 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[ currentImage], 0, object_VP_mats);1113 1114 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[ currentImage], 0, ship_VP_mats);1115 1116 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[ currentImage], 0, asteroid_VP_mats);1117 1118 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[ currentImage], 0, laser_VP_mats);1119 1120 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[ currentImage], 0, explosion_UBO);1121 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_modelPipeline[imageIndex], 0, object_VP_mats); 1122 1123 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_shipPipeline[imageIndex], 0, ship_VP_mats); 1124 1125 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_asteroidPipeline[imageIndex], 0, asteroid_VP_mats); 1126 1127 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_laserPipeline[imageIndex], 0, laser_VP_mats); 1128 1129 VulkanUtils::copyDataToMemory(device, uniformBuffersMemory_explosionPipeline[imageIndex], 0, explosion_UBO); 1121 1130 } 1122 1131 1123 1132 // TODO: Maybe move all/most of this to the base Screen class 1124 1133 void VulkanGame::renderScene() { 1125 vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, numeric_limits<uint64_t>::max());1126 1127 uint32_t imageIndex;1128 1129 1134 // TODO: Recreate the swap chain here if the user went to a new screen 1130 1135 1131 1136 VkResult result = vkAcquireNextImageKHR(device, swapChain, numeric_limits<uint64_t>::max(), 1132 imageA vailableSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex);1137 imageAcquiredSemaphores[currentFrame], VK_NULL_HANDLE, &imageIndex); 1133 1138 1134 1139 if (result == VK_ERROR_OUT_OF_DATE_KHR) { … … 1139 1144 } 1140 1145 1141 // TODO: Figure out a more elegant way to only do updates and render the UI once per scene render 1142 // Probably move some of the renderScene() code into a higher function that updates the UI, and renders 1143 // the UI and scene 1144 updateScene(imageIndex); 1146 if (vkWaitForFences(device, 1, &inFlightFences[imageIndex], VK_TRUE, numeric_limits<uint64_t>::max()) != VK_SUCCESS) { 1147 throw runtime_error("failed waiting for fence!"); 1148 } 1149 if (vkResetFences(device, 1, &inFlightFences[imageIndex]) != VK_SUCCESS) { 1150 throw runtime_error("failed to reset fence!"); 1151 } 1152 1153 updateScene(); 1154 1155 VkSemaphore waitSemaphores[] = { imageAcquiredSemaphores[currentFrame] }; 1156 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT }; 1157 VkSemaphore signalSemaphores[] = { renderCompleteSemaphores[currentFrame] }; 1145 1158 1146 1159 VkSubmitInfo submitInfo = {}; 1147 1160 submitInfo.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; 1148 1149 VkSemaphore waitSemaphores[] = { imageAvailableSemaphores[currentFrame] };1150 VkPipelineStageFlags waitStages[] = { VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT };1151 1152 1161 submitInfo.waitSemaphoreCount = 1; 1153 1162 submitInfo.pWaitSemaphores = waitSemaphores; … … 1155 1164 submitInfo.commandBufferCount = 1; 1156 1165 submitInfo.pCommandBuffers = &commandBuffers[imageIndex]; 1157 1158 VkSemaphore signalSemaphores[] = { renderFinishedSemaphores[currentFrame] };1159 1160 1166 submitInfo.signalSemaphoreCount = 1; 1161 1167 submitInfo.pSignalSemaphores = signalSemaphores; 1162 1168 1163 vkResetFences(device, 1, &inFlightFences[currentFrame]); 1164 1165 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[currentFrame]) != VK_SUCCESS) { 1169 if (vkQueueSubmit(graphicsQueue, 1, &submitInfo, inFlightFences[imageIndex]) != VK_SUCCESS) { 1166 1170 throw runtime_error("failed to submit draw command buffer!"); 1167 1171 } … … 1171 1175 presentInfo.waitSemaphoreCount = 1; 1172 1176 presentInfo.pWaitSemaphores = signalSemaphores; 1173 1174 VkSwapchainKHR swapChains[] = { swapChain };1175 1177 presentInfo.swapchainCount = 1; 1176 presentInfo.pSwapchains = swapChains;1178 presentInfo.pSwapchains = &swapChain; 1177 1179 presentInfo.pImageIndices = &imageIndex; 1178 1180 presentInfo.pResults = nullptr; … … 1187 1189 } 1188 1190 1189 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1190 currentFrame = (currentFrame + 1) % MAX_FRAMES_IN_FLIGHT; 1191 currentFrame = (currentFrame + 1) % swapChainImageCount; 1191 1192 } 1192 1193 1193 1194 void VulkanGame::cleanup() { 1195 // TODO: Should probably check the returned result 1196 vkDeviceWaitIdle(device); 1197 1194 1198 ImGui_ImplVulkan_Shutdown(); 1195 1199 ImGui_ImplSDL2_Shutdown(); 1196 1200 ImGui::DestroyContext(); 1197 1201 1202 // TODO: Probably move this into cleanupSwapChain once I finish the integration 1198 1203 destroyImguiDescriptorPool(); 1199 1204 … … 1213 1218 explosionPipeline.cleanupBuffers(); 1214 1219 1215 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) { 1216 vkDestroySemaphore(device, renderFinishedSemaphores[i], nullptr); 1217 vkDestroySemaphore(device, imageAvailableSemaphores[i], nullptr); 1218 vkDestroyFence(device, inFlightFences[i], nullptr); 1219 } 1220 1220 vkDestroyCommandPool(device, resourceCommandPool, nullptr); 1221 1221 vkDestroyCommandPool(device, commandPool, nullptr); 1222 1222 1223 vkDestroyDevice(device, nullptr); 1223 1224 vkDestroySurfaceKHR(instance, surface, nullptr); … … 1379 1380 1380 1381 if (extensionsSupported) { 1381 SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface); 1382 swapChainAdequate = !swapChainSupport.formats.empty() && !swapChainSupport.presentModes.empty(); 1382 vector<VkSurfaceFormatKHR> formats = VulkanUtils::querySwapChainFormats(physicalDevice, surface); 1383 vector<VkPresentModeKHR> presentModes = VulkanUtils::querySwapChainPresentModes(physicalDevice, surface); 1384 1385 swapChainAdequate = !formats.empty() && !presentModes.empty(); 1383 1386 } 1384 1387 … … 1445 1448 } 1446 1449 1450 void VulkanGame::chooseSwapChainProperties() { 1451 vector<VkSurfaceFormatKHR> availableFormats = VulkanUtils::querySwapChainFormats(physicalDevice, surface); 1452 vector<VkPresentModeKHR> availablePresentModes = VulkanUtils::querySwapChainPresentModes(physicalDevice, surface); 1453 1454 swapChainSurfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(availableFormats, 1455 { VK_FORMAT_B8G8R8A8_UNORM, VK_FORMAT_R8G8B8A8_UNORM, VK_FORMAT_B8G8R8_UNORM, VK_FORMAT_R8G8B8_UNORM }, 1456 VK_COLOR_SPACE_SRGB_NONLINEAR_KHR); 1457 1458 vector<VkPresentModeKHR> presentModes{ 1459 VK_PRESENT_MODE_MAILBOX_KHR, VK_PRESENT_MODE_IMMEDIATE_KHR, VK_PRESENT_MODE_FIFO_KHR 1460 }; 1461 //vector<VkPresentModeKHR> presentModes{ VK_PRESENT_MODE_FIFO_KHR }; 1462 1463 swapChainPresentMode = VulkanUtils::chooseSwapPresentMode(availablePresentModes, presentModes); 1464 1465 cout << "[vulkan] Selected PresentMode = " << swapChainPresentMode << endl; 1466 1467 VkSurfaceCapabilitiesKHR capabilities = VulkanUtils::querySwapChainCapabilities(physicalDevice, surface); 1468 1469 if (swapChainPresentMode == VK_PRESENT_MODE_MAILBOX_KHR) { 1470 swapChainMinImageCount = 3; 1471 } else if (swapChainPresentMode == VK_PRESENT_MODE_FIFO_KHR || swapChainPresentMode == VK_PRESENT_MODE_FIFO_RELAXED_KHR) { 1472 swapChainMinImageCount = 2; 1473 } else if (swapChainPresentMode == VK_PRESENT_MODE_IMMEDIATE_KHR) { 1474 swapChainMinImageCount = 1; 1475 } else { 1476 throw runtime_error("unexpected present mode!"); 1477 } 1478 1479 if (swapChainMinImageCount < capabilities.minImageCount) { 1480 swapChainMinImageCount = capabilities.minImageCount; 1481 } else if (capabilities.maxImageCount != 0 && swapChainMinImageCount > capabilities.maxImageCount) { 1482 swapChainMinImageCount = capabilities.maxImageCount; 1483 } 1484 } 1485 1447 1486 void VulkanGame::createSwapChain() { 1448 SwapChainSupportDetails swapChainSupport = VulkanUtils::querySwapChainSupport(physicalDevice, surface); 1449 1450 VkSurfaceFormatKHR surfaceFormat = VulkanUtils::chooseSwapSurfaceFormat(swapChainSupport.formats); 1451 VkPresentModeKHR presentMode = VulkanUtils::chooseSwapPresentMode(swapChainSupport.presentModes); 1452 VkExtent2D extent = VulkanUtils::chooseSwapExtent(swapChainSupport.capabilities, gui->getWindowWidth(), gui->getWindowHeight()); 1453 1454 swapChainImageCount = swapChainSupport.capabilities.minImageCount + 1; 1455 if (swapChainSupport.capabilities.maxImageCount > 0 && swapChainImageCount > swapChainSupport.capabilities.maxImageCount) { 1456 swapChainImageCount = swapChainSupport.capabilities.maxImageCount; 1457 } 1487 VkSurfaceCapabilitiesKHR capabilities = VulkanUtils::querySwapChainCapabilities(physicalDevice, surface); 1488 1489 swapChainExtent = VulkanUtils::chooseSwapExtent(capabilities, gui->getWindowWidth(), gui->getWindowHeight()); 1458 1490 1459 1491 VkSwapchainCreateInfoKHR createInfo = {}; 1460 1492 createInfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR; 1461 1493 createInfo.surface = surface; 1462 createInfo.minImageCount = swapChain ImageCount;1463 createInfo.imageFormat = s urfaceFormat.format;1464 createInfo.imageColorSpace = s urfaceFormat.colorSpace;1465 createInfo.imageExtent = extent;1494 createInfo.minImageCount = swapChainMinImageCount; 1495 createInfo.imageFormat = swapChainSurfaceFormat.format; 1496 createInfo.imageColorSpace = swapChainSurfaceFormat.colorSpace; 1497 createInfo.imageExtent = swapChainExtent; 1466 1498 createInfo.imageArrayLayers = 1; 1467 1499 createInfo.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT; 1468 1500 1501 // TODO: Maybe save this result so I don't have to recalculate it every time 1469 1502 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 1470 1503 uint32_t queueFamilyIndices[] = { indices.graphicsFamily.value(), indices.presentFamily.value() }; … … 1480 1513 } 1481 1514 1482 createInfo.preTransform = swapChainSupport.capabilities.currentTransform;1515 createInfo.preTransform = capabilities.currentTransform; 1483 1516 createInfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR; 1484 createInfo.presentMode = presentMode;1517 createInfo.presentMode = swapChainPresentMode; 1485 1518 createInfo.clipped = VK_TRUE; 1486 1519 createInfo.oldSwapchain = VK_NULL_HANDLE; … … 1490 1523 } 1491 1524 1492 vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr); 1525 if (vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, nullptr) != VK_SUCCESS) { 1526 throw runtime_error("failed to get swap chain image count!"); 1527 } 1528 1493 1529 swapChainImages.resize(swapChainImageCount); 1494 vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data()); 1495 1496 swapChainImageFormat = surfaceFormat.format; 1497 swapChainExtent = extent; 1530 if (vkGetSwapchainImagesKHR(device, swapChain, &swapChainImageCount, swapChainImages.data()) != VK_SUCCESS) { 1531 throw runtime_error("failed to get swap chain images!"); 1532 } 1498 1533 } 1499 1534 1500 1535 void VulkanGame::createImageViews() { 1501 swapChainImageViews.resize(swapChainImage s.size());1502 1503 for (size_t i = 0; i < swapChainImage s.size(); i++) {1504 swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChain ImageFormat,1536 swapChainImageViews.resize(swapChainImageCount); 1537 1538 for (size_t i = 0; i < swapChainImageCount; i++) { 1539 swapChainImageViews[i] = VulkanUtils::createImageView(device, swapChainImages[i], swapChainSurfaceFormat.format, 1505 1540 VK_IMAGE_ASPECT_COLOR_BIT); 1506 1541 } … … 1509 1544 void VulkanGame::createRenderPass() { 1510 1545 VkAttachmentDescription colorAttachment = {}; 1511 colorAttachment.format = swapChain ImageFormat;1546 colorAttachment.format = swapChainSurfaceFormat.format; 1512 1547 colorAttachment.samples = VK_SAMPLE_COUNT_1_BIT; 1513 1548 colorAttachment.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR; … … 1574 1609 } 1575 1610 1611 void VulkanGame::createResourceCommandPool() { 1612 QueueFamilyIndices indices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 1613 1614 VkCommandPoolCreateInfo poolInfo = {}; 1615 poolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO; 1616 poolInfo.queueFamilyIndex = indices.graphicsFamily.value(); 1617 poolInfo.flags = 0; 1618 1619 if (vkCreateCommandPool(device, &poolInfo, nullptr, &resourceCommandPool) != VK_SUCCESS) { 1620 throw runtime_error("failed to create resource command pool!"); 1621 } 1622 } 1623 1576 1624 void VulkanGame::createCommandPool() { 1577 QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface); ;1625 QueueFamilyIndices queueFamilyIndices = VulkanUtils::findQueueFamilies(physicalDevice, surface); 1578 1626 1579 1627 VkCommandPoolCreateInfo poolInfo = {}; … … 1588 1636 1589 1637 void VulkanGame::createImageResources() { 1590 VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,1638 VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent, 1591 1639 depthImage, graphicsQueue); 1592 1640 … … 1602 1650 sdlOverlayImageDescriptor.sampler = textureSampler; 1603 1651 1604 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",1652 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, resourceCommandPool, "textures/texture.jpg", 1605 1653 floorTextureImage, graphicsQueue); 1606 1654 … … 1610 1658 floorTextureImageDescriptor.sampler = textureSampler; 1611 1659 1612 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/laser.png",1660 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, resourceCommandPool, "textures/laser.png", 1613 1661 laserTextureImage, graphicsQueue); 1614 1662 … … 1646 1694 1647 1695 void VulkanGame::createFramebuffers() { 1648 swapChainFramebuffers.resize(swapChainImageViews.size()); 1649 1650 for (size_t i = 0; i < swapChainImageViews.size(); i++) { 1696 swapChainFramebuffers.resize(swapChainImageCount); 1697 1698 VkFramebufferCreateInfo framebufferInfo = {}; 1699 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; 1700 framebufferInfo.renderPass = renderPass; 1701 framebufferInfo.width = swapChainExtent.width; 1702 framebufferInfo.height = swapChainExtent.height; 1703 framebufferInfo.layers = 1; 1704 1705 for (uint32_t i = 0; i < swapChainImageCount; i++) { 1651 1706 array<VkImageView, 2> attachments = { 1652 1707 swapChainImageViews[i], … … 1654 1709 }; 1655 1710 1656 VkFramebufferCreateInfo framebufferInfo = {};1657 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;1658 framebufferInfo.renderPass = renderPass;1659 1711 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); 1660 1712 framebufferInfo.pAttachments = attachments.data(); 1661 framebufferInfo.width = swapChainExtent.width;1662 framebufferInfo.height = swapChainExtent.height;1663 framebufferInfo.layers = 1;1664 1713 1665 1714 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) { … … 1670 1719 1671 1720 void VulkanGame::createCommandBuffers() { 1672 commandBuffers.resize(swapChainImage s.size());1721 commandBuffers.resize(swapChainImageCount); 1673 1722 1674 1723 VkCommandBufferAllocateInfo allocInfo = {}; … … 1745 1794 } 1746 1795 1796 void VulkanGame::createSyncObjects() { 1797 imageAcquiredSemaphores.resize(swapChainImageCount); 1798 renderCompleteSemaphores.resize(swapChainImageCount); 1799 inFlightFences.resize(swapChainImageCount); 1800 1801 VkSemaphoreCreateInfo semaphoreInfo = {}; 1802 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO; 1803 1804 VkFenceCreateInfo fenceInfo = {}; 1805 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; 1806 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT; 1807 1808 for (size_t i = 0; i < swapChainImageCount; i++) { 1809 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAcquiredSemaphores[i]) != VK_SUCCESS) { 1810 throw runtime_error("failed to create image acquired sempahore for a frame!"); 1811 } 1812 1813 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderCompleteSemaphores[i]) != VK_SUCCESS) { 1814 throw runtime_error("failed to create render complete sempahore for a frame!"); 1815 } 1816 1817 if (vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) { 1818 throw runtime_error("failed to create fence for a frame!"); 1819 } 1820 } 1821 } 1822 1747 1823 void VulkanGame::createImguiDescriptorPool() { 1748 1824 vector<VkDescriptorPoolSize> pool_sizes{ … … 1773 1849 void VulkanGame::destroyImguiDescriptorPool() { 1774 1850 vkDestroyDescriptorPool(device, imguiDescriptorPool, nullptr); 1775 }1776 1777 void VulkanGame::createSyncObjects() {1778 imageAvailableSemaphores.resize(MAX_FRAMES_IN_FLIGHT);1779 renderFinishedSemaphores.resize(MAX_FRAMES_IN_FLIGHT);1780 inFlightFences.resize(MAX_FRAMES_IN_FLIGHT);1781 1782 VkSemaphoreCreateInfo semaphoreInfo = {};1783 semaphoreInfo.sType = VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO;1784 1785 VkFenceCreateInfo fenceInfo = {};1786 fenceInfo.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO;1787 fenceInfo.flags = VK_FENCE_CREATE_SIGNALED_BIT;1788 1789 for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {1790 if (vkCreateSemaphore(device, &semaphoreInfo, nullptr, &imageAvailableSemaphores[i]) != VK_SUCCESS ||1791 vkCreateSemaphore(device, &semaphoreInfo, nullptr, &renderFinishedSemaphores[i]) != VK_SUCCESS ||1792 vkCreateFence(device, &fenceInfo, nullptr, &inFlightFences[i]) != VK_SUCCESS) {1793 throw runtime_error("failed to create synchronization objects for a frame!");1794 }1795 }1796 1851 } 1797 1852 … … 2002 2057 void VulkanGame::createBufferSet(VkDeviceSize bufferSize, VkBufferUsageFlags flags, 2003 2058 vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory, vector<VkDescriptorBufferInfo>& bufferInfoList) { 2004 buffers.resize(swapChainImage s.size());2005 buffersMemory.resize(swapChainImage s.size());2006 bufferInfoList.resize(swapChainImage s.size());2007 2008 for (size_t i = 0; i < swapChainImage s.size(); i++) {2059 buffers.resize(swapChainImageCount); 2060 buffersMemory.resize(swapChainImageCount); 2061 bufferInfoList.resize(swapChainImageCount); 2062 2063 for (size_t i = 0; i < swapChainImageCount; i++) { 2009 2064 VulkanUtils::createBuffer(device, physicalDevice, bufferSize, flags, 2010 2065 VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, … … 2073 2128 createRenderPass(); 2074 2129 2075 VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,2130 VulkanUtils::createDepthImage(device, physicalDevice, resourceCommandPool, findDepthFormat(), swapChainExtent, 2076 2131 depthImage, graphicsQueue); 2077 2132 createFramebuffers(); … … 2125 2180 2126 2181 createCommandBuffers(); 2182 2183 createSyncObjects(); 2184 2185 imageIndex = 0; 2127 2186 } 2128 2187 … … 2168 2227 } 2169 2228 2229 for (size_t i = 0; i < swapChainImageCount; i++) { 2230 vkDestroySemaphore(device, imageAcquiredSemaphores[i], nullptr); 2231 vkDestroySemaphore(device, renderCompleteSemaphores[i], nullptr); 2232 vkDestroyFence(device, inFlightFences[i], nullptr); 2233 } 2234 2170 2235 vkDestroyRenderPass(device, renderPass, nullptr); 2171 2236 -
vulkan-game.hpp
r7f60b28 r3f32dfd 198 198 class VulkanGame { 199 199 public: 200 VulkanGame( int maxFramesInFlight);200 VulkanGame(); 201 201 ~VulkanGame(); 202 202 … … 234 234 void* pUserData); 235 235 236 // TODO: Make these consts static237 // Also, maybe move them into consts.hpp238 239 const int MAX_FRAMES_IN_FLIGHT;240 241 236 const float NEAR_CLIP = 0.1f; 242 237 const float FAR_CLIP = 100.0f; … … 268 263 VkQueue presentQueue; 269 264 265 // TODO: Maybe make a swapchain struct for convenience 266 VkSurfaceFormatKHR swapChainSurfaceFormat; 267 VkPresentModeKHR swapChainPresentMode; 268 VkExtent2D swapChainExtent; 269 uint32_t swapChainMinImageCount; 270 270 uint32_t swapChainImageCount; 271 271 VkSwapchainKHR swapChain; 272 272 vector<VkImage> swapChainImages; 273 VkFormat swapChainImageFormat;274 VkExtent2D swapChainExtent;275 273 vector<VkImageView> swapChainImageViews; 276 274 vector<VkFramebuffer> swapChainFramebuffers; 277 275 278 276 VkRenderPass renderPass; 277 278 VkCommandPool resourceCommandPool; 279 279 280 VkCommandPool commandPool; 281 vector<VkCommandPool> commandPools; // This is not used yet, but will be once 280 282 vector<VkCommandBuffer> commandBuffers; 281 283 282 284 VulkanImage depthImage; 283 285 284 vector<VkSemaphore> imageAvailableSemaphores; 285 vector<VkSemaphore> renderFinishedSemaphores; 286 // These are per frame 287 vector<VkSemaphore> imageAcquiredSemaphores; 288 vector<VkSemaphore> renderCompleteSemaphores; 289 290 // These are per swap chain image 286 291 vector<VkFence> inFlightFences; 287 292 288 size_t currentFrame; 293 uint32_t imageIndex; 294 uint32_t currentFrame; 289 295 290 296 bool framebufferResized; … … 380 386 void initMatrices(); 381 387 void mainLoop(); 382 void updateScene( uint32_t currentImage);388 void updateScene(); 383 389 void renderScene(); 384 390 void cleanup(); … … 392 398 void createLogicalDevice(const vector<const char*>& validationLayers, 393 399 const vector<const char*>& deviceExtensions); 400 void chooseSwapChainProperties(); 394 401 void createSwapChain(); 395 402 void createImageViews(); 396 403 void createRenderPass(); 397 VkFormat findDepthFormat(); 404 VkFormat findDepthFormat(); // TODO: Declare/define (in the cpp file) this function in some util functions section 405 void createResourceCommandPool(); 398 406 void createCommandPool(); 399 407 void createImageResources(); 400 401 void createTextureSampler();402 408 void createFramebuffers(); 403 409 void createCommandBuffers(); 404 410 void createSyncObjects(); 411 412 void createTextureSampler(); 405 413 406 414 void createImguiDescriptorPool();
Note:
See TracChangeset
for help on using the changeset viewer.