Changeset b794178 in opengl-game for graphics-pipeline_vulkan.cpp


Ignore:
Timestamp:
Oct 17, 2019, 9:30:18 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
e83b155
Parents:
771b33a
Message:

In vulkangame, add the ability to create vulkan resoirces and descriptor sets for shader uniforms (images and ubos)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • graphics-pipeline_vulkan.cpp

    r771b33a rb794178  
    33#include <fstream>
    44#include <stdexcept>
    5 
    6 GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, Viewport viewport, int vertexSize) {
     5#include <iostream>
     6
     7using namespace std;
     8
     9// TODO: Remove any instances of cout and instead throw exceptions
     10
     11GraphicsPipeline_Vulkan::GraphicsPipeline_Vulkan(VkDevice device, VkRenderPass renderPass, Viewport viewport,
     12      int vertexSize) {
    713   this->device = device;
     14   this->renderPass = renderPass;
    815   this->viewport = viewport;
    916
     
    2532
    2633   this->attributeDescriptions.push_back(attributeDesc);
     34}
     35
     36void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, vector<VkDescriptorBufferInfo>* bufferData) {
     37   this->descriptorInfoList.push_back({ type, stageFlags, bufferData, nullptr });
     38}
     39
     40void GraphicsPipeline_Vulkan::addDescriptorInfo(VkDescriptorType type, VkShaderStageFlags stageFlags, VkDescriptorImageInfo* imageData) {
     41   this->descriptorInfoList.push_back({ type, stageFlags, nullptr, imageData });
    2742}
    2843
     
    131146   pipelineLayoutInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
    132147   pipelineLayoutInfo.setLayoutCount = 1;
    133 
    134    vkDestroyShaderModule(device, vertShaderModule, nullptr);
    135    vkDestroyShaderModule(device, fragShaderModule, nullptr);
     148   pipelineLayoutInfo.pSetLayouts = &this->descriptorSetLayout;
     149   pipelineLayoutInfo.pushConstantRangeCount = 0;
     150
     151   if (vkCreatePipelineLayout(this->device, &pipelineLayoutInfo, nullptr, &this->pipelineLayout) != VK_SUCCESS) {
     152      throw runtime_error("failed to create pipeline layout!");
     153   }
     154
     155   VkGraphicsPipelineCreateInfo pipelineInfo = {};
     156   pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
     157   pipelineInfo.stageCount = 2;
     158   pipelineInfo.pStages = shaderStages;
     159   pipelineInfo.pVertexInputState = &vertexInputInfo;
     160   pipelineInfo.pInputAssemblyState = &inputAssembly;
     161   pipelineInfo.pViewportState = &viewportState;
     162   pipelineInfo.pRasterizationState = &rasterizer;
     163   pipelineInfo.pMultisampleState = &multisampling;
     164   pipelineInfo.pDepthStencilState = &depthStencil;
     165   pipelineInfo.pColorBlendState = &colorBlending;
     166   pipelineInfo.pDynamicState = nullptr;
     167   pipelineInfo.layout = this->pipelineLayout;
     168   pipelineInfo.renderPass = this->renderPass;
     169   pipelineInfo.subpass = 0;
     170   pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
     171   pipelineInfo.basePipelineIndex = -1;
     172
     173   if (vkCreateGraphicsPipelines(this->device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &this->pipeline) != VK_SUCCESS) {
     174      throw runtime_error("failed to create graphics pipeline!");
     175   }
     176
     177   vkDestroyShaderModule(this->device, vertShaderModule, nullptr);
     178   vkDestroyShaderModule(this->device, fragShaderModule, nullptr);
     179}
     180
     181void GraphicsPipeline_Vulkan::createDescriptorSetLayout() {
     182   vector<VkDescriptorSetLayoutBinding> bindings(this->descriptorInfoList.size());
     183
     184   for (size_t i = 0; i < bindings.size(); i++) {
     185      bindings[i].binding = i;
     186      bindings[i].descriptorCount = 1;
     187      bindings[i].descriptorType = this->descriptorInfoList[i].type;
     188      bindings[i].stageFlags = this->descriptorInfoList[i].stageFlags;
     189      bindings[i].pImmutableSamplers = nullptr;
     190   }
     191
     192   VkDescriptorSetLayoutCreateInfo layoutInfo = {};
     193   layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
     194   layoutInfo.bindingCount = static_cast<uint32_t>(bindings.size());
     195   layoutInfo.pBindings = bindings.data();
     196
     197   if (vkCreateDescriptorSetLayout(this->device, &layoutInfo, nullptr, &this->descriptorSetLayout) != VK_SUCCESS) {
     198      throw runtime_error("failed to create descriptor set layout!");
     199   }
     200}
     201
     202void GraphicsPipeline_Vulkan::createDescriptorPool(vector<VkImage>& swapChainImages) {
     203   vector<VkDescriptorPoolSize> poolSizes(this->descriptorInfoList.size());
     204
     205   for (size_t i = 0; i < poolSizes.size(); i++) {
     206      poolSizes[i].type = this->descriptorInfoList[i].type;
     207      poolSizes[i].descriptorCount = static_cast<uint32_t>(swapChainImages.size());
     208   }
     209
     210   VkDescriptorPoolCreateInfo poolInfo = {};
     211   poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
     212   poolInfo.poolSizeCount = static_cast<uint32_t>(poolSizes.size());
     213   poolInfo.pPoolSizes = poolSizes.data();
     214   poolInfo.maxSets = static_cast<uint32_t>(swapChainImages.size());
     215
     216   if (vkCreateDescriptorPool(this->device, &poolInfo, nullptr, &this->descriptorPool) != VK_SUCCESS) {
     217      throw runtime_error("failed to create descriptor pool!");
     218   }
     219}
     220
     221void GraphicsPipeline_Vulkan::createDescriptorSets(vector<VkImage>& swapChainImages) {
     222   vector<VkDescriptorSetLayout> layouts(swapChainImages.size(), this->descriptorSetLayout);
     223
     224   VkDescriptorSetAllocateInfo allocInfo = {};
     225   allocInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO;
     226   allocInfo.descriptorPool = this->descriptorPool;
     227   allocInfo.descriptorSetCount = static_cast<uint32_t>(swapChainImages.size());
     228   allocInfo.pSetLayouts = layouts.data();
     229
     230   this->descriptorSets.resize(swapChainImages.size());
     231   if (vkAllocateDescriptorSets(device, &allocInfo, this->descriptorSets.data()) != VK_SUCCESS) {
     232      throw runtime_error("failed to allocate descriptor sets!");
     233   }
     234
     235   for (size_t i = 0; i < swapChainImages.size(); i++) {
     236      vector<VkWriteDescriptorSet> descriptorWrites(this->descriptorInfoList.size());
     237
     238      for (size_t j = 0; j < descriptorWrites.size(); j++) {
     239         descriptorWrites[j].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
     240         descriptorWrites[j].dstSet = this->descriptorSets[i];
     241         descriptorWrites[j].dstBinding = j;
     242         descriptorWrites[j].dstArrayElement = 0;
     243         descriptorWrites[j].descriptorType = this->descriptorInfoList[j].type;
     244         descriptorWrites[j].descriptorCount = 1;
     245         descriptorWrites[j].pBufferInfo = nullptr;
     246         descriptorWrites[j].pImageInfo = nullptr;
     247         descriptorWrites[j].pTexelBufferView = nullptr;
     248
     249         switch (descriptorWrites[j].descriptorType) {
     250            case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
     251               descriptorWrites[j].pBufferInfo = &(*this->descriptorInfoList[j].bufferDataList)[i];
     252               break;
     253            case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
     254               descriptorWrites[j].pImageInfo = this->descriptorInfoList[j].imageData;
     255               break;
     256            default:
     257               cout << "Unknown descriptor type: " << descriptorWrites[j].descriptorType << endl;
     258         }
     259      }
     260
     261      vkUpdateDescriptorSets(this->device, static_cast<uint32_t>(descriptorWrites.size()), descriptorWrites.data(), 0, nullptr);
     262   }
    136263}
    137264
     
    143270
    144271   VkShaderModule shaderModule;
    145    if (vkCreateShaderModule(device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
     272   if (vkCreateShaderModule(this->device, &createInfo, nullptr, &shaderModule) != VK_SUCCESS) {
    146273      throw runtime_error("failed to create shader module!");
    147274   }
     
    167294   return buffer;
    168295}
     296
     297void GraphicsPipeline_Vulkan::cleanup() {
     298   vkDestroyPipeline(this->device, this->pipeline, nullptr);
     299   vkDestroyDescriptorPool(this->device, this->descriptorPool, nullptr);
     300   vkDestroyPipelineLayout(this->device, this->pipelineLayout, nullptr);
     301}
     302
     303void GraphicsPipeline_Vulkan::cleanupBuffers() {
     304   vkDestroyDescriptorSetLayout(this->device, this->descriptorSetLayout, nullptr);
     305}
Note: See TracChangeset for help on using the changeset viewer.