Changeset 721e8be in opengl-game for vulkan-game.cpp


Ignore:
Timestamp:
Aug 26, 2019, 2:40:43 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
8a40f4b
Parents:
f00ee54
Message:

Add reusable functions that generate a descriptor set layout, a descriptor pool, and descriptor sets for a pipeline given a configuration for the pipeline's uniforms

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.cpp

    rf00ee54 r721e8be  
    7878};
    7979
     80struct DescriptorInfo {
     81   VkDescriptorType type;
     82   VkShaderStageFlags stageFlags;
     83
     84   vector<VkDescriptorBufferInfo>* bufferDataList;
     85   VkDescriptorImageInfo* imageData;
     86};
     87
    8088struct GraphicsPipelineInfo {
    8189   VkPipelineLayout pipelineLayout;
     
    8492   VkVertexInputBindingDescription bindingDescription;
    8593   vector<VkVertexInputAttributeDescription> attributeDescriptions;
     94
     95   vector<DescriptorInfo> descriptorInfoList;
    8696
    8797   VkDescriptorPool descriptorPool;
     
    195205      vector<VkBuffer> uniformBuffers;
    196206      vector<VkDeviceMemory> uniformBuffersMemory;
     207
     208      VkDescriptorImageInfo sceneImageInfo;
     209      VkDescriptorImageInfo overlayImageInfo;
     210
     211      vector<VkDescriptorBufferInfo> uniformBufferInfoList;
    197212
    198213      GraphicsPipelineInfo scenePipeline;
     
    308323         createTextureSampler();
    309324
     325         sceneImageInfo = {};
     326         sceneImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
     327         sceneImageInfo.imageView = textureImageView;
     328         sceneImageInfo.sampler = textureSampler;
     329
     330         overlayImageInfo = {};
     331         overlayImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
     332         overlayImageInfo.imageView = sdlOverlayImageView;
     333         overlayImageInfo.sampler = textureSampler;
     334
    310335         vector<Vertex> sceneVertices = {
    311336            {{-0.5f, -0.5f, -0.5f}, {1.0f, 0.0f, 0.0f}, {0.0f, 1.0f}},
     
    332357         addAttributeDescription(scenePipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&Vertex::texCoord));
    333358
    334          createSceneDescriptorSetLayout(scenePipeline);
     359         addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER, VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList, nullptr);
     360         addDescriptorInfo(scenePipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &sceneImageInfo);
     361
     362         createDescriptorSetLayout(scenePipeline);
    335363
    336364
     
    352380         addAttributeDescription(overlayPipeline, VK_FORMAT_R32G32_SFLOAT, offset_of(&OverlayVertex::texCoord));
    353381
    354          createOverlayDescriptorSetLayout(overlayPipeline);
     382         addDescriptorInfo(overlayPipeline, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, VK_SHADER_STAGE_FRAGMENT_BIT, nullptr, &overlayImageInfo);
     383
     384         createDescriptorSetLayout(overlayPipeline);
    355385
    356386         createBufferResources();
     
    761791      }
    762792
    763       void createSceneDescriptorSetLayout(GraphicsPipelineInfo& info) {
    764          VkDescriptorSetLayoutBinding uboLayoutBinding = {};
    765          uboLayoutBinding.binding = 0;
    766          uboLayoutBinding.descriptorCount = 1;
    767          uboLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    768          uboLayoutBinding.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
    769          uboLayoutBinding.pImmutableSamplers = nullptr;
    770 
    771          VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
    772          samplerLayoutBinding.binding = 1;
    773          samplerLayoutBinding.descriptorCount = 1;
    774          samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    775          samplerLayoutBinding.pImmutableSamplers = nullptr;
    776          samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    777 
    778          array<VkDescriptorSetLayoutBinding, 2> bindings = { uboLayoutBinding, samplerLayoutBinding };
    779          VkDescriptorSetLayoutCreateInfo layoutInfo = {};
    780          layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
    781          layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
    782          layoutInfo.pBindings = bindings.data();
    783 
    784          if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
    785             throw runtime_error("failed to create descriptor set layout!");
    786          }
    787       }
    788 
    789       void createOverlayDescriptorSetLayout(GraphicsPipelineInfo& info) {
    790          VkDescriptorSetLayoutBinding samplerLayoutBinding = {};
    791          samplerLayoutBinding.binding = 0;
    792          samplerLayoutBinding.descriptorCount = 1;
    793          samplerLayoutBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    794          samplerLayoutBinding.pImmutableSamplers = nullptr;
    795          samplerLayoutBinding.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
    796 
    797          array<VkDescriptorSetLayoutBinding, 1> bindings = { samplerLayoutBinding };
    798          VkDescriptorSetLayoutCreateInfo layoutInfo = {};
    799          layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
    800          layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
    801          layoutInfo.pBindings = bindings.data();
    802 
    803          if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
    804             throw runtime_error("failed to create descriptor set layout!");
    805          }
    806       }
    807 
    808793      void initGraphicsPipelineInfo(GraphicsPipelineInfo& info,
    809794            const void* vertexData, int vertexSize, size_t numVertices,
     
    833818
    834819         info.attributeDescriptions.push_back(attributeDesc);
     820      }
     821
     822      void addDescriptorInfo(GraphicsPipelineInfo& info, VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData, VkDescriptorImageInfo* imageData) {
     823         info.descriptorInfoList.push_back({ type, stageFlags, bufferData, imageData });
     824      }
     825
     826      void createDescriptorSetLayout(GraphicsPipelineInfo& info) {
     827         vector<VkDescriptorSetLayoutBinding> bindings(info.descriptorInfoList.size());
     828
     829         for (size_t i = 0; i < bindings.size(); i++) {
     830            bindings[i].binding = i;
     831            bindings[i].descriptorCount = 1;
     832            bindings[i].descriptorType = info.descriptorInfoList[i].type;
     833            bindings[i].stageFlags = info.descriptorInfoList[i].stageFlags;
     834            bindings[i].pImmutableSamplers = nullptr;
     835         }
     836
     837         VkDescriptorSetLayoutCreateInfo layoutInfo = {};
     838         layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
     839         layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
     840         layoutInfo.pBindings = bindings.data();
     841
     842         if (vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr, &info.descriptorSetLayout) != VK_SUCCESS) {
     843            throw runtime_error("failed to create descriptor set layout!");
     844         }
    835845      }
    836846
     
    14131423         uniformBuffers.resize(swapChainImages.size());
    14141424         uniformBuffersMemory.resize(swapChainImages.size());
     1425         uniformBufferInfoList.resize(swapChainImages.size());
    14151426
    14161427         for (size_t i = 0; i < swapChainImages.size(); i++) {
     
    14181429               VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT,
    14191430               uniformBuffers[i], uniformBuffersMemory[i]);
     1431
     1432            uniformBufferInfoList[i].buffer = uniformBuffers[i];
     1433            uniformBufferInfoList[i].offset = 0;
     1434            uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
    14201435         }
    14211436      }
     
    15031518      }
    15041519
    1505       void createSceneDescriptorPool(GraphicsPipelineInfo& info) {
    1506          array<VkDescriptorPoolSize, 2> poolSizes = {};
    1507          poolSizes[0].type = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    1508          poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
    1509          poolSizes[1].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    1510          poolSizes[1].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
     1520      void createDescriptorPool(GraphicsPipelineInfo& info) {
     1521         vector<VkDescriptorPoolSize> poolSizes(info.descriptorInfoList.size());
     1522
     1523         for (size_t i = 0; i < poolSizes.size(); i++) {
     1524            poolSizes[i].type = info.descriptorInfoList[i].type;
     1525            poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
     1526         }
    15111527
    15121528         VkDescriptorPoolCreateInfo poolInfo = {};
     
    15211537      }
    15221538
    1523       void createSceneDescriptorSets(GraphicsPipelineInfo& info) {
     1539      void createDescriptorSets(GraphicsPipelineInfo& info) {
    15241540         vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
    15251541
     
    15361552
    15371553         for (size_t i = 0; i < swapChainImages.size(); i++) {
    1538             VkDescriptorBufferInfo bufferInfo = {};
    1539             bufferInfo.buffer = uniformBuffers[i];
    1540             bufferInfo.offset = 0;
    1541             bufferInfo.range = sizeof(UniformBufferObject);
    1542 
    1543             VkDescriptorImageInfo imageInfo = {};
    1544             imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
    1545             imageInfo.imageView = textureImageView;
    1546             imageInfo.sampler = textureSampler;
    1547 
    1548             array<VkWriteDescriptorSet, 2> descriptorWrites = {};
    1549 
    1550             descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1551             descriptorWrites[0].dstSet = info.descriptorSets[i];
    1552             descriptorWrites[0].dstBinding = 0;
    1553             descriptorWrites[0].dstArrayElement = 0;
    1554             descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER;
    1555             descriptorWrites[0].descriptorCount = 1;
    1556             descriptorWrites[0].pBufferInfo = &bufferInfo;
    1557             descriptorWrites[0].pImageInfo = nullptr;
    1558             descriptorWrites[0].pTexelBufferView = nullptr;
    1559 
    1560             descriptorWrites[1].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1561             descriptorWrites[1].dstSet = info.descriptorSets[i];
    1562             descriptorWrites[1].dstBinding = 1;
    1563             descriptorWrites[1].dstArrayElement = 0;
    1564             descriptorWrites[1].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    1565             descriptorWrites[1].descriptorCount = 1;
    1566             descriptorWrites[1].pBufferInfo = nullptr;
    1567             descriptorWrites[1].pImageInfo = &imageInfo;
    1568             descriptorWrites[1].pTexelBufferView = nullptr;
    1569 
    1570             vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
    1571          }
    1572       }
    1573 
    1574       void createOverlayDescriptorPool(GraphicsPipelineInfo& info) {
    1575          array<VkDescriptorPoolSize, 1> poolSizes = {};
    1576          poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    1577          poolSizes[0].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
    1578 
    1579          VkDescriptorPoolCreateInfo poolInfo = {};
    1580          poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
    1581          poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
    1582          poolInfo.pPoolSizes = poolSizes.data();
    1583          poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
    1584 
    1585          if (vkCreateDescriptorPool(device, &poolInfo, nullptr, &info.descriptorPool) != VK_SUCCESS) {
    1586             throw runtime_error("failed to create descriptor pool!");
    1587          }
    1588       }
    1589 
    1590       void createOverlayDescriptorSets(GraphicsPipelineInfo& info) {
    1591          vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), info.descriptorSetLayout);
    1592 
    1593          VkDescriptorSetAllocateInfo allocInfo = {};
    1594          allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
    1595          allocInfo.descriptorPool = info.descriptorPool;
    1596          allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
    1597          allocInfo.pSetLayouts = layouts.data();
    1598 
    1599          info.descriptorSets.resize(swapChainImages.size());
    1600          if (vkAllocateDescriptorSets(device, &allocInfo, info.descriptorSets.data()) != VK_SUCCESS) {
    1601             throw runtime_error("failed to allocate descriptor sets!");
    1602          }
    1603 
    1604          for (size_t i = 0; i < swapChainImages.size(); i++) {
    1605             VkDescriptorImageInfo imageInfo = {};
    1606             imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
    1607             imageInfo.imageView = sdlOverlayImageView;
    1608             imageInfo.sampler = textureSampler;
    1609 
    1610             array<VkWriteDescriptorSet, 1> descriptorWrites = {};
    1611 
    1612             descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
    1613             descriptorWrites[0].dstSet = info.descriptorSets[i];
    1614             descriptorWrites[0].dstBinding = 0;
    1615             descriptorWrites[0].dstArrayElement = 0;
    1616             descriptorWrites[0].descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
    1617             descriptorWrites[0].descriptorCount = 1;
    1618             descriptorWrites[0].pBufferInfo = nullptr;
    1619             descriptorWrites[0].pImageInfo = &imageInfo;
    1620             descriptorWrites[0].pTexelBufferView = nullptr;
     1554            vector<VkWriteDescriptorSet> descriptorWrites(info.descriptorInfoList.size());
     1555
     1556            for (size_t j = 0; j < descriptorWrites.size(); j++) {
     1557               descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
     1558               descriptorWrites[j].dstSet = info.descriptorSets[i];
     1559               descriptorWrites[j].dstBinding = j;
     1560               descriptorWrites[j].dstArrayElement = 0;
     1561               descriptorWrites[j].descriptorType = info.descriptorInfoList[j].type;
     1562               descriptorWrites[j].descriptorCount = 1;
     1563               descriptorWrites[j].pBufferInfo = nullptr;
     1564               descriptorWrites[j].pImageInfo = nullptr;
     1565               descriptorWrites[j].pTexelBufferView = nullptr;
     1566
     1567               switch (descriptorWrites[j].descriptorType) {
     1568                  case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
     1569                     descriptorWrites[j].pBufferInfo = &(*info.descriptorInfoList[j].bufferDataList)[i];
     1570                     break;
     1571                  case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
     1572                     descriptorWrites[j].pImageInfo = info.descriptorInfoList[j].imageData;
     1573                     break;
     1574                  default:
     1575                     cout << "Unknown descriptor type: " << descriptorWrites[j].descriptorType << endl;
     1576               }
     1577            }
    16211578
    16221579            vkUpdateDescriptorSets(device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
     
    18801837
    18811838         createGraphicsPipeline("shaders/scene-vert.spv", "shaders/scene-frag.spv", scenePipeline);
    1882          createSceneDescriptorPool(scenePipeline);
    1883          createSceneDescriptorSets(scenePipeline);
     1839         createDescriptorPool(scenePipeline);
     1840         createDescriptorSets(scenePipeline);
    18841841
    18851842         createGraphicsPipeline("shaders/overlay-vert.spv", "shaders/overlay-frag.spv", overlayPipeline);
    1886          createOverlayDescriptorPool(overlayPipeline);
    1887          createOverlayDescriptorSets(overlayPipeline);
     1843         createDescriptorPool(overlayPipeline);
     1844         createDescriptorSets(overlayPipeline);
    18881845
    18891846         createCommandBuffers();
Note: See TracChangeset for help on using the changeset viewer.