Changeset 603b5bc in opengl-game for vulkan-game.cpp
- Timestamp:
- Oct 22, 2019, 7:53:48 PM (5 years ago)
- Branches:
- feature/imgui-sdl, master, points-test
- Children:
- 34bdf3a
- Parents:
- e83b155
- git-author:
- Dmitry Portnoy <dmitry.portnoy@…> (10/22/19 19:10:44)
- git-committer:
- Dmitry Portnoy <dmitry.portnoy@…> (10/22/19 19:53:48)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
vulkan-game.cpp
re83b155 r603b5bc 133 133 createCommandPool(); 134 134 135 createVulkanResources(); 136 137 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, viewport, sizeof(Vertex))); 135 createImageResources(); 136 137 createFramebuffers(); 138 createUniformBuffers(); 139 140 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, 141 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(Vertex))); 138 142 139 143 graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos)); … … 151 155 graphicsPipelines.back().createDescriptorSets(swapChainImages); 152 156 153 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, viewport, sizeof(OverlayVertex))); 157 graphicsPipelines.push_back(GraphicsPipeline_Vulkan(device, renderPass, 158 { 0, 0, (int)swapChainExtent.width, (int)swapChainExtent.height }, sizeof(OverlayVertex))); 154 159 155 160 graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos)); … … 163 168 graphicsPipelines.back().createDescriptorPool(swapChainImages); 164 169 graphicsPipelines.back().createDescriptorSets(swapChainImages); 170 171 createCommandBuffers(); 172 173 // TODO: Creating the descriptor pool and descriptor sets might need to be redone when the 174 // swap chain is recreated 165 175 166 176 cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl; … … 496 506 497 507 swapChainImageFormat = surfaceFormat.format; 498 viewport = { 0, 0, (int)extent.width, (int)extent.height };508 swapChainExtent = extent; 499 509 } 500 510 … … 588 598 } 589 599 590 void VulkanGame::createVulkanResources() { 600 void VulkanGame::createImageResources() { 601 VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent, 602 depthImage, graphicsQueue); 603 591 604 createTextureSampler(); 592 createUniformBuffers();593 594 // TODO: Make sure that Vulkan complains about these images not being destroyed and then destroy them595 605 596 606 VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg", 597 607 floorTextureImage, graphicsQueue); 598 VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);599 608 600 609 floorTextureImageDescriptor = {}; … … 602 611 floorTextureImageDescriptor.imageView = floorTextureImage.imageView; 603 612 floorTextureImageDescriptor.sampler = textureSampler; 613 614 VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage); 604 615 605 616 sdlOverlayImageDescriptor = {}; … … 635 646 } 636 647 648 void VulkanGame::createFramebuffers() { 649 swapChainFramebuffers.resize(swapChainImageViews.size()); 650 651 for (size_t i = 0; i < swapChainImageViews.size(); i++) { 652 array<VkImageView, 2> attachments = { 653 swapChainImageViews[i], 654 depthImage.imageView 655 }; 656 657 VkFramebufferCreateInfo framebufferInfo = {}; 658 framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO; 659 framebufferInfo.renderPass = renderPass; 660 framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size()); 661 framebufferInfo.pAttachments = attachments.data(); 662 framebufferInfo.width = swapChainExtent.width; 663 framebufferInfo.height = swapChainExtent.height; 664 framebufferInfo.layers = 1; 665 666 if (vkCreateFramebuffer(device, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS) { 667 throw runtime_error("failed to create framebuffer!"); 668 } 669 } 670 } 671 637 672 void VulkanGame::createUniformBuffers() { 638 673 VkDeviceSize bufferSize = sizeof(UniformBufferObject); … … 653 688 } 654 689 690 void VulkanGame::createCommandBuffers() { 691 commandBuffers.resize(swapChainImages.size()); 692 693 VkCommandBufferAllocateInfo allocInfo = {}; 694 allocInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; 695 allocInfo.commandPool = commandPool; 696 allocInfo.level = VK_COMMAND_BUFFER_LEVEL_PRIMARY; 697 allocInfo.commandBufferCount = (uint32_t) commandBuffers.size(); 698 699 if (vkAllocateCommandBuffers(device, &allocInfo, commandBuffers.data()) != VK_SUCCESS) { 700 throw runtime_error("failed to allocate command buffers!"); 701 } 702 703 for (size_t i = 0; i < commandBuffers.size(); i++) { 704 VkCommandBufferBeginInfo beginInfo = {}; 705 beginInfo.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; 706 beginInfo.flags = VK_COMMAND_BUFFER_USAGE_SIMULTANEOUS_USE_BIT; 707 beginInfo.pInheritanceInfo = nullptr; 708 709 if (vkBeginCommandBuffer(commandBuffers[i], &beginInfo) != VK_SUCCESS) { 710 throw runtime_error("failed to begin recording command buffer!"); 711 } 712 713 VkRenderPassBeginInfo renderPassInfo = {}; 714 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO; 715 renderPassInfo.renderPass = renderPass; 716 renderPassInfo.framebuffer = swapChainFramebuffers[i]; 717 renderPassInfo.renderArea.offset = { 0, 0 }; 718 renderPassInfo.renderArea.extent = swapChainExtent; 719 720 array<VkClearValue, 2> clearValues = {}; 721 clearValues[0].color = {{ 0.0f, 0.0f, 0.0f, 1.0f }}; 722 clearValues[1].depthStencil = { 1.0f, 0 }; 723 724 renderPassInfo.clearValueCount = static_cast<uint32_t>(clearValues.size()); 725 renderPassInfo.pClearValues = clearValues.data(); 726 727 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 728 729 // reateGraphicsPipelineCommands(scenePipeline, i); 730 // createGraphicsPipelineCommands(overlayPipeline, i); 731 for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) { 732 pipeline.createRenderCommands(commandBuffers[i], i); 733 } 734 735 vkCmdEndRenderPass(commandBuffers[i]); 736 737 if (vkEndCommandBuffer(commandBuffers[i]) != VK_SUCCESS) { 738 throw runtime_error("failed to record command buffer!"); 739 } 740 } 741 } 742 655 743 void VulkanGame::cleanupSwapChain() { 744 VulkanUtils::destroyVulkanImage(device, depthImage); 745 746 for (VkFramebuffer framebuffer : swapChainFramebuffers) { 747 vkDestroyFramebuffer(device, framebuffer, nullptr); 748 } 749 750 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 751 656 752 for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) { 657 753 pipeline.cleanup(); … … 666 762 vkDestroySwapchainKHR(device, swapChain, nullptr); 667 763 668 for (size_t i = 0; i < swapChainImages.size(); i++) {764 for (size_t i = 0; i < uniformBuffers.size(); i++) { 669 765 vkDestroyBuffer(device, uniformBuffers[i], nullptr); 670 766 vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
Note:
See TracChangeset
for help on using the changeset viewer.