Changeset d22ae72 in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Aug 20, 2019, 7:12:21 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
28c13da
Parents:
b8b32bd
Message:

Create a separapte descriptor pool and vector of descriptor sets for each graphics pipeline

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rb8b32bd rd22ae72  
    112112
    113113struct GraphicsPipelineInfo {
     114   VkPipelineLayout pipelineLayout;
    114115   VkPipeline pipeline;
     116
     117   VkDescriptorPool descriptorPool;
     118   VkDescriptorSetLayout descriptorSetLayout;
     119   vector<VkDescriptorSet> descriptorSets;
    115120
    116121   VkBuffer vertexBuffer;
     
    187192
    188193      VkRenderPass renderPass;
    189       VkDescriptorSetLayout descriptorSetLayout;
    190       VkPipelineLayout pipelineLayout;
    191       VkDescriptorPool descriptorPool;
    192       vector<VkDescriptorSet> descriptorSets;
    193 
    194194      VkCommandPool commandPool;
    195195
     
    313313         createImageViews();
    314314         createRenderPass();
    315          createDescriptorSetLayout();
    316          createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &scenePipeline.pipeline);
    317          createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &overlayPipeline.pipeline);
     315
     316         createDescriptorSetLayout(scenePipeline);
     317         createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", scenePipeline);
     318
     319         createDescriptorSetLayout(overlayPipeline);
     320         createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", overlayPipeline);
     321
    318322         createCommandPool();
    319323         createDepthResources();
     
    358362
    359363         createUniformBuffers();
    360          createDescriptorPool();
    361          createDescriptorSets();
     364
     365         createDescriptorPool(scenePipeline);
     366         createDescriptorSets(scenePipeline);
     367
     368         createDescriptorPool(overlayPipeline);
     369         createDescriptorSets(overlayPipeline);
     370
    362371         createCommandBuffers();
    363372         createSyncObjects();
     
    766775      }
    767776
    768       void createDescriptorSetLayout() {
     777      void createDescriptorSetLayout(GraphicsPipelineInfo& info) {
    769778         VkDescriptorSetLayoutBinding uboLayoutBinding = {};
    770779         uboLayoutBinding.binding = 0;
     
    794803         layoutInfo.pBindings = bindings.data();
    795804
    796          if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &descriptorSetLayout) != VK_SUCCESS) {
     805         if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
    797806            throw runtime_error("failed to create descriptor set layout!");
    798807         }
    799808      }
    800809
    801       void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, VkPipeline* pipeline) {
     810      void createGraphicsPipeline(string vertShaderFile, string fragShaderFile, GraphicsPipelineInfo& info) {
    802811         auto vertShaderCode = readFile(vertShaderFile);
    803812         auto fragShaderCode = readFile(fragShaderFile);
     
    906915         pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    907916         pipelineLayoutInfo.setLayoutCount = 1;
    908          pipelineLayoutInfo.pSetLayouts = &descriptorSetLayout;
     917         pipelineLayoutInfo.pSetLayouts = &info.descriptorSetLayout;
    909918         pipelineLayoutInfo.pushConstantRangeCount = 0;
    910919
    911          if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &pipelineLayout) != VK_SUCCESS) {
     920         if (vkCreatePipelineLayout(device, &pipelineLayoutInfo, nullptr, &info.pipelineLayout) != VK_SUCCESS) {
    912921            throw runtime_error("failed to create pipeline layout!");
    913922         }
     
    925934         pipelineInfo.pColorBlendState = &colorBlending;
    926935         pipelineInfo.pDynamicState = nullptr;
    927          pipelineInfo.layout = pipelineLayout;
     936         pipelineInfo.layout = info.pipelineLayout;
    928937         pipelineInfo.renderPass = renderPass;
    929938         pipelineInfo.subpass = 0;
     
    931940         pipelineInfo.basePipelineIndex = -1;
    932941
    933          if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, pipeline) != VK_SUCCESS) {
     942         if (vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &info.pipeline) != VK_SUCCESS) {
    934943            throw runtime_error("failed to create graphics pipeline!");
    935944         }
     
    14871496      }
    14881497
    1489       void createDescriptorPool() {
     1498      void createDescriptorPool(GraphicsPipelineInfo& info) {
    14901499         array<VkDescriptorPoolSize, 3> poolSizes = {};
    14911500         poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
     
    15021511         poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
    15031512
    1504          if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool) != VK_SUCCESS) {
     1513         if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) {
    15051514            throw runtime_error("failed to create descriptor pool!");
    15061515         }
    15071516      }
    15081517
    1509       void createDescriptorSets() {
    1510          vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), descriptorSetLayout);
     1518      void createDescriptorSets(GraphicsPipelineInfo& info) {
     1519         vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
    15111520
    15121521         VkDescriptorSetAllocateInfo allocInfo = {};
    15131522         allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
    1514          allocInfo.descriptorPool = descriptorPool;
     1523         allocInfo.descriptorPool = info.descriptorPool;
    15151524         allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
    15161525         allocInfo.pSetLayouts = layouts.data();
    15171526
    1518          descriptorSets.resize(swapChainImages.size());
    1519          if (vkAllocateDescriptorSets(device, &allocInfo, descriptorSets.data()) != VK_SUCCESS) {
     1527         info.descriptorSets.resize(swapChainImages.size());
     1528         if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) {
    15201529            throw runtime_error("failed to allocate descriptor sets!");
    15211530         }
     
    15401549
    15411550            descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1542             descriptorWrites[0].dstSet = descriptorSets[i];
     1551            descriptorWrites[0].dstSet = info.descriptorSets[i];
    15431552            descriptorWrites[0].dstBinding = 0;
    15441553            descriptorWrites[0].dstArrayElement = 0;
     
    15501559
    15511560            descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1552             descriptorWrites[1].dstSet = descriptorSets[i];
     1561            descriptorWrites[1].dstSet = info.descriptorSets[i];
    15531562            descriptorWrites[1].dstBinding = 1;
    15541563            descriptorWrites[1].dstArrayElement = 0;
     
    15601569
    15611570            descriptorWrites[2].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1562             descriptorWrites[2].dstSet = descriptorSets[i];
     1571            descriptorWrites[2].dstSet = info.descriptorSets[i];
    15631572            descriptorWrites[2].dstBinding = 2;
    15641573            descriptorWrites[2].dstArrayElement = 0;
     
    16121621            vkCmdBeginRenderPass(commandBuffers[i], &renderPassInfo, VK_SUBPASS_CONTENTS_INLINE);
    16131622
    1614             vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, scenePipeline.pipeline);
    1615 
    1616             vkCmdBindDescriptorSets(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, pipelineLayout, 0, 1, &descriptorSets[i], 0, nullptr);
    1617 
    1618             VkBuffer vertexBuffers[] = { scenePipeline.vertexBuffer };
    1619             VkDeviceSize offsets[] = { 0 };
    1620             vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffers, offsets);
    1621 
    1622             vkCmdBindIndexBuffer(commandBuffers[i], scenePipeline.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
    1623 
    1624             vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(scenePipeline.numIndices), 1, 0, 0, 0);
    1625 
    1626             vkCmdBindPipeline(commandBuffers[i], VK_PIPELINE_BIND_POINT_GRAPHICS, overlayPipeline.pipeline);
    1627 
    1628             VkBuffer vertexBuffersOverlay[] = { overlayPipeline.vertexBuffer };
    1629             vkCmdBindVertexBuffers(commandBuffers[i], 0, 1, vertexBuffersOverlay, offsets);
    1630 
    1631             vkCmdBindIndexBuffer(commandBuffers[i], overlayPipeline.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
    1632 
    1633             vkCmdDrawIndexed(commandBuffers[i], static_cast<uint32_t>(overlayPipeline.numIndices), 1, 0, 0, 0);
     1623            createGraphicsPipelineCommands(scenePipeline, i);
     1624            createGraphicsPipelineCommands(overlayPipeline, i);
    16341625
    16351626            vkCmdEndRenderPass(commandBuffers[i]);
     
    16391630            }
    16401631         }
     1632      }
     1633
     1634      void createGraphicsPipelineCommands(GraphicsPipelineInfo& info, size_t imageIdx) {
     1635         vkCmdBindPipeline(commandBuffers[imageIdx], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipeline);
     1636         vkCmdBindDescriptorSets(commandBuffers[imageIdx], VK_PIPELINE_BIND_POINT_GRAPHICS, info.pipelineLayout, 0, 1,
     1637            &info.descriptorSets[imageIdx], 0, nullptr);
     1638
     1639         VkBuffer vertexBuffers[] = { info.vertexBuffer };
     1640         VkDeviceSize offsets[] = { 0 };
     1641         vkCmdBindVertexBuffers(commandBuffers[imageIdx], 0, 1, vertexBuffers, offsets);
     1642
     1643         vkCmdBindIndexBuffer(commandBuffers[imageIdx], info.indexBuffer, 0, VK_INDEX_TYPE_UINT16);
     1644
     1645         vkCmdDrawIndexed(commandBuffers[imageIdx], static_cast<uint32_t>(info.numIndices), 1, 0, 0, 0);
    16411646      }
    16421647
     
    18241829         createImageViews();
    18251830         createRenderPass();
    1826          createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &scenePipeline.pipeline);
    1827          createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", &overlayPipeline.pipeline);
     1831
     1832         createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", scenePipeline);
     1833         createGraphicsPipeline("shaders/vert.spv", "shaders/frag.spv", overlayPipeline);
     1834
    18281835         createDepthResources();
    18291836         createFramebuffers();
    18301837         createUniformBuffers();
    1831          createDescriptorPool();
    1832          createDescriptorSets();
     1838
     1839         createDescriptorPool(scenePipeline);
     1840         createDescriptorSets(scenePipeline);
     1841
     1842         createDescriptorPool(overlayPipeline);
     1843         createDescriptorSets(overlayPipeline);
     1844
    18331845         createCommandBuffers();
    18341846      }
     
    18511863         vkFreeMemory(device, sdlOverlayImageMemory, nullptr);
    18521864
    1853          vkDestroyDescriptorSetLayout(device, descriptorSetLayout, nullptr);
    1854 
    1855          vkDestroyBuffer(device, scenePipeline.vertexBuffer, nullptr);
    1856          vkFreeMemory(device, scenePipeline.vertexBufferMemory, nullptr);
    1857          vkDestroyBuffer(device, scenePipeline.indexBuffer, nullptr);
    1858          vkFreeMemory(device, scenePipeline.indexBufferMemory, nullptr);
    1859 
    1860          vkDestroyBuffer(device, overlayPipeline.vertexBuffer, nullptr);
    1861          vkFreeMemory(device, overlayPipeline.vertexBufferMemory, nullptr);
    1862          vkDestroyBuffer(device, overlayPipeline.indexBuffer, nullptr);
    1863          vkFreeMemory(device, overlayPipeline.indexBufferMemory, nullptr);
     1865         cleanupPipelineBuffers(scenePipeline);
     1866         cleanupPipelineBuffers(overlayPipeline);
    18641867
    18651868         for (size_t i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) {
     
    19201923         vkFreeCommandBuffers(device, commandPool, static_cast<uint32_t>(commandBuffers.size()), commandBuffers.data());
    19211924
    1922          vkDestroyPipeline(device, scenePipeline.pipeline, nullptr);
    1923          vkDestroyPipeline(device, overlayPipeline.pipeline, nullptr);
    1924 
    1925          vkDestroyPipelineLayout(device, pipelineLayout, nullptr);
     1925         cleanupPipeline(scenePipeline);
     1926         cleanupPipeline(overlayPipeline);
     1927
    19261928         vkDestroyRenderPass(device, renderPass, nullptr);
    19271929
     
    19361938            vkFreeMemory(device, uniformBuffersMemory[i], nullptr);
    19371939         }
    1938 
    1939          vkDestroyDescriptorPool(device, descriptorPool, nullptr);
     1940      }
     1941
     1942      void cleanupPipeline(GraphicsPipelineInfo& pipeline) {
     1943         vkDestroyPipeline(device, pipeline.pipeline, nullptr);
     1944         vkDestroyDescriptorPool(device, pipeline.descriptorPool, nullptr);
     1945         vkDestroyPipelineLayout(device, pipeline.pipelineLayout, nullptr);
     1946      }
     1947
     1948      void cleanupPipelineBuffers(GraphicsPipelineInfo& pipeline) {
     1949         vkDestroyDescriptorSetLayout(device, pipeline.descriptorSetLayout, nullptr);
     1950
     1951         vkDestroyBuffer(device, pipeline.vertexBuffer, nullptr);
     1952         vkFreeMemory(device, pipeline.vertexBufferMemory, nullptr);
     1953         vkDestroyBuffer(device, pipeline.indexBuffer, nullptr);
     1954         vkFreeMemory(device, pipeline.indexBufferMemory, nullptr);
    19401955      }
    19411956
Note: See TracChangeset for help on using the changeset viewer.