Changeset 603b5bc in opengl-game
- 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)
- Files:
-
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
graphics-pipeline_vulkan.cpp
re83b155 r603b5bc 263 263 } 264 264 265 void GraphicsPipeline_Vulkan::createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage) { 266 vkCmdBindPipeline(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipeline); 267 vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, 268 &descriptorSets[currentImage], 0, nullptr); 269 270 // TODO: Implement once I add vertex and index buffers to the pipeline 271 /* 272 VkBuffer vertexBuffers[] = { info.vertexBuffer }; 273 VkDeviceSize offsets[] = { 0 }; 274 vkCmdBindVertexBuffers(commandBuffers[currentImage], 0, 1, vertexBuffers, offsets); 275 276 vkCmdBindIndexBuffer(commandBuffers[currentImage], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16); 277 278 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0); 279 */ 280 } 281 265 282 VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector<char>& code) { 266 283 VkShaderModuleCreateInfo createInfo = {}; -
graphics-pipeline_vulkan.hpp
re83b155 r603b5bc 35 35 void createDescriptorSets(vector<VkImage>& swapChainImages); 36 36 37 void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage); 38 37 39 void cleanup(); 38 40 void cleanupBuffers(); -
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); -
vulkan-game.hpp
re83b155 r603b5bc 37 37 private: 38 38 GameGui* gui; 39 Viewport viewport;40 39 41 40 vector<GraphicsPipeline_Vulkan> graphicsPipelines; … … 59 58 vector<VkImage> swapChainImages; 60 59 VkFormat swapChainImageFormat; 60 VkExtent2D swapChainExtent; 61 61 vector<VkImageView> swapChainImageViews; 62 vector<VkFramebuffer> swapChainFramebuffers; 62 63 63 64 VkRenderPass renderPass; 64 65 VkCommandPool commandPool; 66 vector<VkCommandBuffer> commandBuffers; 65 67 66 // TODO: Create these (and wrap them inside a VulkanImage) 67 VkImage depthImage; 68 VkDeviceMemory depthImageMemory; 69 VkImageView depthImageView; 68 VulkanImage depthImage; 70 69 71 70 VkSampler textureSampler; … … 109 108 VkFormat findDepthFormat(); 110 109 void createCommandPool(); 111 void createVulkanResources(); 110 void createImageResources(); 111 112 112 void createTextureSampler(); 113 void createFramebuffers(); 113 114 void createUniformBuffers(); 115 void createCommandBuffers(); 114 116 115 117 void cleanupSwapChain(); -
vulkan-ref.cpp
re83b155 r603b5bc 178 178 vector<VkImage> swapChainImages; 179 179 VkFormat swapChainImageFormat; 180 /*** END OF REFACTORED CODE ***/181 // (This was taken out of vulkan-game for now and replaced with Viewport)182 // It will definitely be needed when creating command buffers and I could use it in a few other places183 // TODO: Check above ^184 180 VkExtent2D swapChainExtent; 185 /*** START OF REFACTORED CODE ***/186 181 vector<VkImageView> swapChainImageViews; 187 /*** END OF REFACTORED CODE ***/188 182 vector<VkFramebuffer> swapChainFramebuffers; 189 183 190 /*** START OF REFACTORED CODE ***/191 184 VkRenderPass renderPass; 192 185 193 186 VkCommandPool commandPool; 194 /*** END OF REFACTORED CODE ***/195 187 vector<VkCommandBuffer> commandBuffers; 196 188 197 /*** START OF REFACTORED CODE ***/198 189 // The images and the sampler are used to store data for specific attributes. I probably 199 190 // want to keep them separate from the GraphicsPipelineInfo objects and start passing … … 203 194 VkDeviceMemory depthImageMemory; 204 195 VkImageView depthImageView; 205 /*** END OF REFACTORED CODE ***/206 196 207 197 VkImage textureImage; … … 213 203 VkImageView sdlOverlayImageView; 214 204 215 /*** START OF REFACTORED CODE ***/216 205 VkSampler textureSampler; 217 206 … … 222 211 vector<VkBuffer> uniformBuffers; 223 212 vector<VkDeviceMemory> uniformBuffersMemory; 224 /*** END OF REFACTORED CODE ***/225 213 226 214 VkDescriptorImageInfo sceneImageInfo; 227 215 VkDescriptorImageInfo overlayImageInfo; 228 216 229 /*** START OF REFACTORED CODE ***/230 217 vector<VkDescriptorBufferInfo> uniformBufferInfoList; 231 /*** END OF REFACTORED CODE ***/232 218 233 219 GraphicsPipelineInfo scenePipeline; 234 220 GraphicsPipelineInfo overlayPipeline; 221 /*** END OF REFACTORED CODE ***/ 235 222 236 223 vector<VkSemaphore> imageAvailableSemaphores; … … 339 326 340 327 createCommandPool(); 341 /*** END OF REFACTORED CODE ***/342 328 343 329 createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView); 344 /*** START OF REFACTORED CODE ***/345 330 createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView); 346 331 createTextureSampler(); … … 413 398 414 399 createDescriptorSetLayout(overlayPipeline); 400 401 createBufferResources(); 415 402 /*** END OF REFACTORED CODE ***/ 416 417 createBufferResources();418 403 419 404 createSyncObjects(); … … 818 803 } 819 804 } 820 /*** END OF REFACTORED CODE ***/821 805 822 806 void initGraphicsPipelineInfo(GraphicsPipelineInfo& info, 823 807 const void* vertexData, int vertexSize, size_t numVertices, 824 808 const void* indexData, int indexSize, size_t numIndices) { 825 /*** START OF REFACTORED CODE ***/826 809 // Since there is only one array of vertex data, we use binding = 0 827 810 // I'll probably do that for the foreseeable future … … 1015 998 vkDestroyShaderModule(device, fragShaderModule, nullptr); 1016 999 } 1017 /*** END OF REFACTORED CODE ***/1018 1000 1019 1001 VkShaderModule createShaderModule(const vector<char>& code) { … … 1035 1017 1036 1018 for (size_t i = 0; i < swapChainImageViews.size(); i++) { 1037 array 1019 array<VkImageView, 2> attachments = { 1038 1020 swapChainImageViews[i], 1039 1021 depthImageView … … 1055 1037 } 1056 1038 1057 /*** START OF REFACTORED CODE ***/1058 1039 void createCommandPool() { 1059 1040 QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice); … … 1100 1081 return indices; 1101 1082 } 1102 /*** END OF REFACTORED CODE ***/1103 1083 1104 1084 void createDepthResources() { … … 1112 1092 } 1113 1093 1114 /*** START OF REFACTORED CODE ***/1115 1094 VkFormat findDepthFormat() { 1116 1095 return findSupportedFormat( … … 1239 1218 } 1240 1219 1220 /*** START OF REFACTORED CODE ***/ 1241 1221 void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, 1242 1222 VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) { … … 1364 1344 } 1365 1345 1366 /*** START OF REFACTORED CODE ***/1367 1346 VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) { 1368 1347 VkImageViewCreateInfo viewInfo = {}; … … 1649 1628 } 1650 1629 } 1651 /*** END OF REFACTORED CODE ***/1652 1630 1653 1631 void createCommandBuffers() { … … 1690 1668 vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE); 1691 1669 1670 /*** END OF REFACTORED CODE ***/ 1692 1671 createGraphicsPipelineCommands(scenePipeline, i); 1693 1672 createGraphicsPipelineCommands(overlayPipeline, i); 1673 /*** START OF REFACTORED CODE ***/ 1694 1674 1695 1675 vkCmdEndRenderPass(commandBuffers[i]); … … 1705 1685 vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1, 1706 1686 &info.descriptorSets[currentImage], 0, nullptr); 1687 /*** END OF REFACTORED CODE ***/ 1707 1688 1708 1689 VkBuffer vertexBuffers[] = { info.vertexBuffer }; … … 1713 1694 1714 1695 vkCmdDrawIndexed(commandBuffers[currentImage], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0); 1715 } 1696 /*** START OF REFACTORED CODE ***/ 1697 } 1698 /*** END OF REFACTORED CODE ***/ 1716 1699 1717 1700 void createSyncObjects() { … … 1955 1938 } 1956 1939 1940 /*** START OF REFACTORED CODE ***/ 1957 1941 void createBufferResources() { 1958 // TODO: The three functions below will be called in vulkan-game following the1959 // pipeline creation (createDescriptorSets()), and before createCommandBuffers()1960 1942 createDepthResources(); 1961 1943 createFramebuffers(); 1962 1944 createUniformBuffers(); 1963 1945 1964 /*** START OF REFACTORED CODE ***/1965 1946 createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline); 1966 1947 createDescriptorPool(scenePipeline); … … 1970 1951 createDescriptorPool(overlayPipeline); 1971 1952 createDescriptorSets(overlayPipeline); 1972 /*** END OF REFACTORED CODE ***/1973 1953 1974 1954 createCommandBuffers(); … … 1976 1956 1977 1957 void cleanup() { 1978 /*** START OF REFACTORED CODE ***/1979 1958 cleanupSwapChain(); 1980 1959 … … 2042 2021 2043 2022 void cleanupSwapChain() { 2044 /*** END OF REFACTORED CODE ***/2045 2023 vkDestroyImageView(device, depthImageView, nullptr); 2046 2024 vkDestroyImage(device, depthImage, nullptr); … … 2053 2031 vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data()); 2054 2032 2055 /*** START OF REFACTORED CODE ***/2056 2033 cleanupPipeline(scenePipeline); 2057 2034 cleanupPipeline(overlayPipeline); -
vulkan-utils.cpp
re83b155 r603b5bc 308 308 309 309 image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT); 310 } 311 312 void VulkanUtils::createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, 313 VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue) { 314 createImage(device, physicalDevice, extent.width, extent.height, depthFormat, VK_IMAGE_TILING_OPTIMAL, 315 VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT, image); 316 image.imageView = createImageView(device, image.image, depthFormat, VK_IMAGE_ASPECT_DEPTH_BIT); 317 318 transitionImageLayout(device, commandPool, image.image, depthFormat, VK_IMAGE_LAYOUT_UNDEFINED, 319 VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL, graphicsQueue); 310 320 } 311 321 -
vulkan-utils.hpp
re83b155 r603b5bc 70 70 static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice, 71 71 SDL_Texture* texture, VulkanImage& image); 72 static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool, 73 VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue); 72 74 static void createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height, 73 75 VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
Note:
See TracChangeset
for help on using the changeset viewer.