Changeset 603b5bc in opengl-game


Ignore:
Timestamp:
Oct 22, 2019, 7:53:48 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
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)
Message:

In vulkangame, add code to create the frame buffers and command buffers

Files:
7 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.cpp

    re83b155 r603b5bc  
    263263}
    264264
     265void 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
    265282VkShaderModule GraphicsPipeline_Vulkan::createShaderModule(const vector<char>& code) {
    266283   VkShaderModuleCreateInfo createInfo = {};
  • graphics-pipeline_vulkan.hpp

    re83b155 r603b5bc  
    3535      void createDescriptorSets(vector<VkImage>& swapChainImages);
    3636
     37      void createRenderCommands(VkCommandBuffer& commandBuffer, uint32_t currentImage);
     38
    3739      void cleanup();
    3840      void cleanupBuffers();
  • vulkan-game.cpp

    re83b155 r603b5bc  
    133133   createCommandPool();
    134134
    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)));
    138142
    139143   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&Vertex::pos));
     
    151155   graphicsPipelines.back().createDescriptorSets(swapChainImages);
    152156
    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)));
    154159
    155160   graphicsPipelines.back().addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&OverlayVertex::pos));
     
    163168   graphicsPipelines.back().createDescriptorPool(swapChainImages);
    164169   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
    165175
    166176   cout << "Created " << graphicsPipelines.size() << " graphics pipelines" << endl;
     
    496506
    497507   swapChainImageFormat = surfaceFormat.format;
    498    viewport = { 0, 0, (int)extent.width, (int)extent.height };
     508   swapChainExtent = extent;
    499509}
    500510
     
    588598}
    589599
    590 void VulkanGame::createVulkanResources() {
     600void VulkanGame::createImageResources() {
     601   VulkanUtils::createDepthImage(device, physicalDevice, commandPool, findDepthFormat(), swapChainExtent,
     602      depthImage, graphicsQueue);
     603
    591604   createTextureSampler();
    592    createUniformBuffers();
    593 
    594    // TODO: Make sure that Vulkan complains about these images not being destroyed and then destroy them
    595605
    596606   VulkanUtils::createVulkanImageFromFile(device, physicalDevice, commandPool, "textures/texture.jpg",
    597607      floorTextureImage, graphicsQueue);
    598    VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
    599608
    600609   floorTextureImageDescriptor = {};
     
    602611   floorTextureImageDescriptor.imageView = floorTextureImage.imageView;
    603612   floorTextureImageDescriptor.sampler = textureSampler;
     613
     614   VulkanUtils::createVulkanImageFromSDLTexture(device, physicalDevice, uiOverlay, sdlOverlayImage);
    604615
    605616   sdlOverlayImageDescriptor = {};
     
    635646}
    636647
     648void 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
    637672void VulkanGame::createUniformBuffers() {
    638673   VkDeviceSize bufferSize = sizeof(UniformBufferObject);
     
    653688}
    654689
     690void 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
    655743void 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
    656752   for (GraphicsPipeline_Vulkan pipeline : graphicsPipelines) {
    657753      pipeline.cleanup();
     
    666762   vkDestroySwapchainKHR(device, swapChain, nullptr);
    667763
    668    for (size_t i = 0; i < swapChainImages.size(); i++) {
     764   for (size_t i = 0; i < uniformBuffers.size(); i++) {
    669765      vkDestroyBuffer(device, uniformBuffers[i], nullptr);
    670766      vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
  • vulkan-game.hpp

    re83b155 r603b5bc  
    3737   private:
    3838      GameGui* gui;
    39       Viewport viewport;
    4039
    4140      vector<GraphicsPipeline_Vulkan> graphicsPipelines;
     
    5958      vector<VkImage> swapChainImages;
    6059      VkFormat swapChainImageFormat;
     60      VkExtent2D swapChainExtent;
    6161      vector<VkImageView> swapChainImageViews;
     62      vector<VkFramebuffer> swapChainFramebuffers;
    6263
    6364      VkRenderPass renderPass;
    6465      VkCommandPool commandPool;
     66      vector<VkCommandBuffer> commandBuffers;
    6567
    66       // TODO: Create these (and wrap them inside a VulkanImage)
    67       VkImage depthImage;
    68       VkDeviceMemory depthImageMemory;
    69       VkImageView depthImageView;
     68      VulkanImage depthImage;
    7069
    7170      VkSampler textureSampler;
     
    109108      VkFormat findDepthFormat();
    110109      void createCommandPool();
    111       void createVulkanResources();
     110      void createImageResources();
     111
    112112      void createTextureSampler();
     113      void createFramebuffers();
    113114      void createUniformBuffers();
     115      void createCommandBuffers();
    114116
    115117      void cleanupSwapChain();
  • vulkan-ref.cpp

    re83b155 r603b5bc  
    178178      vector<VkImage> swapChainImages;
    179179      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 places
    183       // TODO: Check above ^
    184180      VkExtent2D swapChainExtent;
    185 /*** START OF REFACTORED CODE ***/
    186181      vector<VkImageView> swapChainImageViews;
    187 /*** END OF REFACTORED CODE ***/
    188182      vector<VkFramebuffer> swapChainFramebuffers;
    189183
    190 /*** START OF REFACTORED CODE ***/
    191184      VkRenderPass renderPass;
    192185
    193186      VkCommandPool commandPool;
    194 /*** END OF REFACTORED CODE ***/
    195187      vector<VkCommandBuffer> commandBuffers;
    196188
    197 /*** START OF REFACTORED CODE ***/
    198189      // The images and the sampler are used to store data for specific attributes. I probably
    199190      // want to keep them separate from the GraphicsPipelineInfo objects and start passing
     
    203194      VkDeviceMemory depthImageMemory;
    204195      VkImageView depthImageView;
    205 /*** END OF REFACTORED CODE ***/
    206196
    207197      VkImage textureImage;
     
    213203      VkImageView sdlOverlayImageView;
    214204
    215 /*** START OF REFACTORED CODE ***/
    216205      VkSampler textureSampler;
    217206
     
    222211      vector<VkBuffer> uniformBuffers;
    223212      vector<VkDeviceMemory> uniformBuffersMemory;
    224 /*** END OF REFACTORED CODE ***/
    225213
    226214      VkDescriptorImageInfo sceneImageInfo;
    227215      VkDescriptorImageInfo overlayImageInfo;
    228216
    229 /*** START OF REFACTORED CODE ***/
    230217      vector<VkDescriptorBufferInfo> uniformBufferInfoList;
    231 /*** END OF REFACTORED CODE ***/
    232218
    233219      GraphicsPipelineInfo scenePipeline;
    234220      GraphicsPipelineInfo overlayPipeline;
     221/*** END OF REFACTORED CODE ***/
    235222
    236223      vector<VkSemaphore> imageAvailableSemaphores;
     
    339326
    340327         createCommandPool();
    341 /*** END OF REFACTORED CODE ***/
    342328
    343329         createImageResources("textures/texture.jpg", textureImage, textureImageMemory, textureImageView);
    344 /*** START OF REFACTORED CODE ***/
    345330         createImageResourcesFromSDLTexture(uiOverlay, sdlOverlayImage, sdlOverlayImageMemory, sdlOverlayImageView);
    346331         createTextureSampler();
     
    413398
    414399         createDescriptorSetLayout(overlayPipeline);
     400
     401         createBufferResources();
    415402/*** END OF REFACTORED CODE ***/
    416 
    417          createBufferResources();
    418403
    419404         createSyncObjects();
     
    818803         }
    819804      }
    820 /*** END OF REFACTORED CODE ***/
    821805
    822806      void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
    823807            const void* vertexData, int vertexSize, size_t numVertices,
    824808            const void* indexData, int indexSize, size_t numIndices) {
    825 /*** START OF REFACTORED CODE ***/
    826809         // Since there is only one array of vertex data, we use binding = 0
    827810         // I'll probably do that for the foreseeable future
     
    1015998         vkDestroyShaderModule(device, fragShaderModule, nullptr);
    1016999      }
    1017 /*** END OF REFACTORED CODE ***/
    10181000
    10191001      VkShaderModule createShaderModule(const vector<char>& code) {
     
    10351017
    10361018         for (size_t i = 0; i < swapChainImageViews.size(); i++) {
    1037             array <VkImageView, 2> attachments = {
     1019            array<VkImageView, 2> attachments = {
    10381020               swapChainImageViews[i],
    10391021               depthImageView
     
    10551037      }
    10561038
    1057 /*** START OF REFACTORED CODE ***/
    10581039      void createCommandPool() {
    10591040         QueueFamilyIndices queueFamilyIndices = findQueueFamilies(physicalDevice);
     
    11001081         return indices;
    11011082      }
    1102 /*** END OF REFACTORED CODE ***/
    11031083
    11041084      void createDepthResources() {
     
    11121092      }
    11131093
    1114 /*** START OF REFACTORED CODE ***/
    11151094      VkFormat findDepthFormat() {
    11161095         return findSupportedFormat(
     
    12391218      }
    12401219
     1220/*** START OF REFACTORED CODE ***/
    12411221      void createImage(uint32_t width, uint32_t height, VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage,
    12421222            VkMemoryPropertyFlags properties, VkImage& image, VkDeviceMemory& imageMemory) {
     
    13641344      }
    13651345
    1366 /*** START OF REFACTORED CODE ***/
    13671346      VkImageView createImageView(VkImage image, VkFormat format, VkImageAspectFlags aspectFlags) {
    13681347         VkImageViewCreateInfo viewInfo = {};
     
    16491628         }
    16501629      }
    1651 /*** END OF REFACTORED CODE ***/
    16521630
    16531631      void createCommandBuffers() {
     
    16901668            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    16911669
     1670/*** END OF REFACTORED CODE ***/
    16921671            createGraphicsPipelineCommands(scenePipeline, i);
    16931672            createGraphicsPipelineCommands(overlayPipeline, i);
     1673/*** START OF REFACTORED CODE ***/
    16941674
    16951675            vkCmdEndRenderPass(commandBuffers[i]);
     
    17051685         vkCmdBindDescriptorSets(commandBuffers[currentImage], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
    17061686            &info.descriptorSets[currentImage], 0, nullptr);
     1687/*** END OF REFACTORED CODE ***/
    17071688
    17081689         VkBuffer vertexBuffers[] = { info.vertexBuffer };
     
    17131694
    17141695         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 ***/
    17161699
    17171700      void createSyncObjects() {
     
    19551938      }
    19561939
     1940/*** START OF REFACTORED CODE ***/
    19571941      void createBufferResources() {
    1958          // TODO: The three functions below will be called in vulkan-game following the
    1959          // pipeline creation (createDescriptorSets()), and before createCommandBuffers()
    19601942         createDepthResources();
    19611943         createFramebuffers();
    19621944         createUniformBuffers();
    19631945
    1964 /*** START OF REFACTORED CODE ***/
    19651946         createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
    19661947         createDescriptorPool(scenePipeline);
     
    19701951         createDescriptorPool(overlayPipeline);
    19711952         createDescriptorSets(overlayPipeline);
    1972 /*** END OF REFACTORED CODE ***/
    19731953
    19741954         createCommandBuffers();
     
    19761956
    19771957      void cleanup() {
    1978 /*** START OF REFACTORED CODE ***/
    19791958         cleanupSwapChain();
    19801959
     
    20422021
    20432022      void cleanupSwapChain() {
    2044 /*** END OF REFACTORED CODE ***/
    20452023         vkDestroyImageView(device, depthImageView, nullptr);
    20462024         vkDestroyImage(device, depthImage, nullptr);
     
    20532031         vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    20542032
    2055 /*** START OF REFACTORED CODE ***/
    20562033         cleanupPipeline(scenePipeline);
    20572034         cleanupPipeline(overlayPipeline);
  • vulkan-utils.cpp

    re83b155 r603b5bc  
    308308
    309309   image.imageView = createImageView(device, image.image, VK_FORMAT_R8G8B8A8_UNORM, VK_IMAGE_ASPECT_COLOR_BIT);
     310}
     311
     312void 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);
    310320}
    311321
  • vulkan-utils.hpp

    re83b155 r603b5bc  
    7070      static void createVulkanImageFromSDLTexture(VkDevice device, VkPhysicalDevice physicalDevice,
    7171            SDL_Texture* texture, VulkanImage& image);
     72      static void createDepthImage(VkDevice device, VkPhysicalDevice physicalDevice, VkCommandPool commandPool,
     73            VkFormat depthFormat, VkExtent2D extent, VulkanImage& image, VkQueue graphicsQueue);
    7274      static void createImage(VkDevice device, VkPhysicalDevice physicalDevice, uint32_t width, uint32_t height,
    7375            VkFormat format, VkImageTiling tiling, VkImageUsageFlags usage, VkMemoryPropertyFlags properties,
Note: See TracChangeset for help on using the changeset viewer.