Advertisement
Guest User

Untitled

a guest
Mar 2nd, 2023
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.49 KB | None | 0 0
  1. struct Pipeline
  2. {
  3.     VkPipeline pipeline = nullptr;
  4.     VkPipelineLayout layout = nullptr;
  5. };
  6.  
  7. Pipeline buildGBufferPipeline(VkDevice device,
  8.                                   VkExtent2D size,
  9.                                   int attachCount,
  10.                                   VkRenderPass pass,
  11.                                   const std::vector<VkDescriptorSetLayout>& setLayouts,
  12.                                   const Mesh::VertexInputDescription& attributesDesc,
  13.                                   const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& shaders)
  14.     {
  15.         Pipeline result;
  16.  
  17.         VkViewport viewport;
  18.  
  19.         viewport.x = 0.0f;
  20.         viewport.y = 0.0f;
  21.         viewport.width = (float)size.width;
  22.         viewport.height = (float)size.height;
  23.         viewport.minDepth = 0.0f;
  24.         viewport.maxDepth = 1.0f;
  25.  
  26.         VkRect2D scissor;
  27.         scissor.offset = {0, 0};
  28.         scissor.extent = size;
  29.  
  30.         VkPushConstantRange pushConstant;
  31.         pushConstant.offset = 0;
  32.         pushConstant.size = sizeof(float) * 4;
  33.         pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
  34.  
  35.         VkPipelineLayoutCreateInfo info{};
  36.         info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  37.         info.pNext = nullptr;
  38.         info.flags = 0;
  39.         info.pushConstantRangeCount = 1;
  40.         info.pPushConstantRanges = &pushConstant;
  41.         info.setLayoutCount = setLayouts.size();
  42.         info.pSetLayouts = setLayouts.data();
  43.  
  44.         vkCreatePipelineLayout(device, &info, nullptr, &result.layout);
  45.  
  46.         VkPipelineViewportStateCreateInfo viewportState = {};
  47.         viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  48.         viewportState.pNext = nullptr;
  49.         viewportState.viewportCount = 1;
  50.         viewportState.pViewports = &viewport;
  51.         viewportState.scissorCount = 1;
  52.         viewportState.pScissors = &scissor;
  53.  
  54.         VkPipelineColorBlendAttachmentState attachState{};
  55.         attachState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  56.         attachState.blendEnable = VK_FALSE;
  57.  
  58.         std::vector<VkPipelineColorBlendAttachmentState> states;
  59.  
  60.         states.resize(attachCount, attachState);
  61.  
  62.         VkPipelineColorBlendStateCreateInfo colorBlending = {};
  63.         colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  64.         colorBlending.pNext = nullptr;
  65.         colorBlending.logicOpEnable = VK_FALSE;
  66.         colorBlending.logicOp = VK_LOGIC_OP_COPY;
  67.         colorBlending.attachmentCount = states.size();
  68.         colorBlending.pAttachments = states.data();
  69.  
  70.         VkPipelineMultisampleStateCreateInfo multisampling{};
  71.         multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  72.         multisampling.pNext = nullptr;
  73.         multisampling.sampleShadingEnable = VK_FALSE;
  74.         multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  75.         multisampling.minSampleShading = 1.0f;
  76.         multisampling.pSampleMask = nullptr;
  77.         multisampling.alphaToCoverageEnable = VK_FALSE;
  78.         multisampling.alphaToOneEnable = VK_FALSE;
  79.  
  80.         VkPipelineRasterizationStateCreateInfo rasterizerInfo{};
  81.         rasterizerInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  82.         rasterizerInfo.pNext = nullptr;
  83.         rasterizerInfo.depthClampEnable = VK_FALSE;
  84.         rasterizerInfo.rasterizerDiscardEnable = VK_FALSE;
  85.         rasterizerInfo.polygonMode = VK_POLYGON_MODE_FILL;
  86.         rasterizerInfo.lineWidth = 1.0f;
  87.         rasterizerInfo.cullMode = VK_CULL_MODE_FRONT_BIT;
  88.         rasterizerInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
  89.         rasterizerInfo.depthBiasEnable = VK_FALSE;
  90.         rasterizerInfo.depthBiasConstantFactor = 0.0f;
  91.         rasterizerInfo.depthBiasClamp = 0.0f;
  92.         rasterizerInfo.depthBiasSlopeFactor = 0.0f;
  93.  
  94.         VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
  95.         inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  96.         inputAssembly.pNext = nullptr;
  97.         inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  98.         inputAssembly.primitiveRestartEnable = VK_FALSE;
  99.  
  100.         VkPipelineVertexInputStateCreateInfo attribInfo{};
  101.         attribInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
  102.         attribInfo.pNext = nullptr;
  103.         attribInfo.vertexBindingDescriptionCount = 0;
  104.         attribInfo.vertexAttributeDescriptionCount = 0;
  105.         attribInfo.pVertexAttributeDescriptions = attributesDesc.attributes.data();
  106.         attribInfo.vertexAttributeDescriptionCount = attributesDesc.attributes.size();
  107.         attribInfo.pVertexBindingDescriptions = attributesDesc.bindings.data();
  108.         attribInfo.vertexBindingDescriptionCount = attributesDesc.bindings.size();
  109.  
  110.         VkPipelineDepthStencilStateCreateInfo depthInfo{};
  111.         depthInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
  112.         depthInfo.pNext = nullptr;
  113.         depthInfo.depthTestEnable = VK_TRUE;
  114.         depthInfo.depthWriteEnable = VK_TRUE;
  115.         depthInfo.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
  116.         depthInfo.depthBoundsTestEnable = VK_FALSE;
  117.         depthInfo.minDepthBounds = 0.0f;
  118.         depthInfo.maxDepthBounds = 1.0f;
  119.         depthInfo.stencilTestEnable = VK_FALSE;
  120.  
  121.         std::vector<VkPipelineShaderStageCreateInfo> shadersInfo;
  122.  
  123.         for (const auto& shader : shaders)
  124.             shadersInfo.push_back(loadShader(device, shader.first, shader.second));
  125.  
  126.         VkGraphicsPipelineCreateInfo pipelineInfo = {};
  127.         pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  128.         pipelineInfo.pNext = nullptr;
  129.         pipelineInfo.stageCount = shadersInfo.size();
  130.         pipelineInfo.pStages = shadersInfo.data();
  131.         pipelineInfo.pVertexInputState = &attribInfo;
  132.         pipelineInfo.pInputAssemblyState = &inputAssembly;
  133.         pipelineInfo.pViewportState = &viewportState;
  134.         pipelineInfo.pRasterizationState = &rasterizerInfo;
  135.         pipelineInfo.pMultisampleState = &multisampling;
  136.         pipelineInfo.pColorBlendState = &colorBlending;
  137.         pipelineInfo.layout = result.layout;
  138.         pipelineInfo.renderPass = pass;
  139.         pipelineInfo.subpass = 0;
  140.         pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
  141.         pipelineInfo.pDepthStencilState = &depthInfo;
  142.  
  143.         vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &result.pipeline);
  144.  
  145.         return result;
  146.     }
  147.  
  148. Pipeline buildDeferredPBRPipeline(VkDevice device,
  149.                                       VkExtent2D size,
  150.                                       VkRenderPass pass,
  151.                                       const std::vector<VkDescriptorSetLayout>& setLayouts,
  152.                                       const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& shaders)
  153.     {
  154.         Pipeline result;
  155.  
  156.         VkViewport viewport;
  157.         viewport.x = 0.0f;
  158.         viewport.y = 0.0f;
  159.         viewport.width = (float)size.width;
  160.         viewport.height = (float)size.height;
  161.         viewport.minDepth = 0.0f;
  162.         viewport.maxDepth = 1.0f;
  163.  
  164.         VkRect2D scissor;
  165.         scissor.offset = {0, 0};
  166.         scissor.extent = size;
  167.  
  168.         VkPushConstantRange pushConstant;
  169.         pushConstant.offset = 0;
  170.         pushConstant.size = sizeof(int);
  171.         pushConstant.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
  172.  
  173.         VkPipelineLayoutCreateInfo info{};
  174.         info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
  175.         info.pNext = nullptr;
  176.         info.flags = 0;
  177.         info.pushConstantRangeCount = 1;
  178.         info.pPushConstantRanges = &pushConstant;
  179.         info.setLayoutCount = setLayouts.size();
  180.         info.pSetLayouts = setLayouts.data();
  181.  
  182.         vkCreatePipelineLayout(device, &info, nullptr, &result.layout);
  183.  
  184.         VkPipelineViewportStateCreateInfo viewportState{};
  185.         viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
  186.         viewportState.pNext = nullptr;
  187.         viewportState.viewportCount = 1;
  188.         viewportState.pViewports = &viewport;
  189.         viewportState.scissorCount = 1;
  190.         viewportState.pScissors = &scissor;
  191.  
  192.         VkPipelineColorBlendAttachmentState attachState{};
  193.         attachState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
  194.         attachState.blendEnable = VK_FALSE;
  195.  
  196.         VkPipelineColorBlendStateCreateInfo colorBlending{};
  197.         colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
  198.         colorBlending.pNext = nullptr;
  199.         colorBlending.logicOpEnable = VK_FALSE;
  200.         colorBlending.logicOp = VK_LOGIC_OP_COPY;
  201.         colorBlending.attachmentCount = 1;
  202.         colorBlending.pAttachments = &attachState;
  203.  
  204.         VkPipelineMultisampleStateCreateInfo multisampling{};
  205.         multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
  206.         multisampling.pNext = nullptr;
  207.         multisampling.sampleShadingEnable = VK_FALSE;
  208.         multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
  209.         multisampling.minSampleShading = 1.0f;
  210.         multisampling.pSampleMask = nullptr;
  211.         multisampling.alphaToCoverageEnable = VK_FALSE;
  212.         multisampling.alphaToOneEnable = VK_FALSE;
  213.  
  214.         VkPipelineRasterizationStateCreateInfo rasterizerInfo{};
  215.         rasterizerInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
  216.         rasterizerInfo.pNext = nullptr;
  217.         rasterizerInfo.depthClampEnable = VK_FALSE;
  218.         rasterizerInfo.rasterizerDiscardEnable = VK_FALSE;
  219.         rasterizerInfo.polygonMode = VK_POLYGON_MODE_FILL;
  220.         rasterizerInfo.lineWidth = 1.0f;
  221.         rasterizerInfo.cullMode = VK_CULL_MODE_FRONT_BIT;
  222.         rasterizerInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
  223.         rasterizerInfo.depthBiasEnable = VK_FALSE;
  224.         rasterizerInfo.depthBiasConstantFactor = 0.0f;
  225.         rasterizerInfo.depthBiasClamp = 0.0f;
  226.         rasterizerInfo.depthBiasSlopeFactor = 0.0f;
  227.  
  228.         VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
  229.         inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
  230.         inputAssembly.pNext = nullptr;
  231.         inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
  232.         inputAssembly.primitiveRestartEnable = VK_FALSE;
  233.  
  234.         std::vector<VkPipelineShaderStageCreateInfo> shadersInfo;
  235.  
  236.         for (const auto& shader : shaders)
  237.             shadersInfo.push_back(loadShader(device, shader.first, shader.second));
  238.  
  239.         VkGraphicsPipelineCreateInfo pipelineInfo{};
  240.         pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
  241.         pipelineInfo.pNext = nullptr;
  242.         pipelineInfo.stageCount = shadersInfo.size();
  243.         pipelineInfo.pStages = shadersInfo.data();
  244.         pipelineInfo.pInputAssemblyState = &inputAssembly;
  245.         pipelineInfo.pViewportState = &viewportState;
  246.         pipelineInfo.pRasterizationState = &rasterizerInfo;
  247.         pipelineInfo.pMultisampleState = &multisampling;
  248.         pipelineInfo.pColorBlendState = &colorBlending;
  249.         pipelineInfo.layout = result.layout;
  250.         pipelineInfo.renderPass = pass;
  251.         pipelineInfo.subpass = 0;
  252.         pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
  253.  
  254.         vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &result.pipeline);
  255.  
  256.         return result;
  257.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement