Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct Pipeline
- {
- VkPipeline pipeline = nullptr;
- VkPipelineLayout layout = nullptr;
- };
- Pipeline buildGBufferPipeline(VkDevice device,
- VkExtent2D size,
- int attachCount,
- VkRenderPass pass,
- const std::vector<VkDescriptorSetLayout>& setLayouts,
- const Mesh::VertexInputDescription& attributesDesc,
- const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& shaders)
- {
- Pipeline result;
- VkViewport viewport;
- viewport.x = 0.0f;
- viewport.y = 0.0f;
- viewport.width = (float)size.width;
- viewport.height = (float)size.height;
- viewport.minDepth = 0.0f;
- viewport.maxDepth = 1.0f;
- VkRect2D scissor;
- scissor.offset = {0, 0};
- scissor.extent = size;
- VkPushConstantRange pushConstant;
- pushConstant.offset = 0;
- pushConstant.size = sizeof(float) * 4;
- pushConstant.stageFlags = VK_SHADER_STAGE_VERTEX_BIT;
- VkPipelineLayoutCreateInfo info{};
- info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
- info.pNext = nullptr;
- info.flags = 0;
- info.pushConstantRangeCount = 1;
- info.pPushConstantRanges = &pushConstant;
- info.setLayoutCount = setLayouts.size();
- info.pSetLayouts = setLayouts.data();
- vkCreatePipelineLayout(device, &info, nullptr, &result.layout);
- VkPipelineViewportStateCreateInfo viewportState = {};
- viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
- viewportState.pNext = nullptr;
- viewportState.viewportCount = 1;
- viewportState.pViewports = &viewport;
- viewportState.scissorCount = 1;
- viewportState.pScissors = &scissor;
- VkPipelineColorBlendAttachmentState attachState{};
- attachState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
- attachState.blendEnable = VK_FALSE;
- std::vector<VkPipelineColorBlendAttachmentState> states;
- states.resize(attachCount, attachState);
- VkPipelineColorBlendStateCreateInfo colorBlending = {};
- colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
- colorBlending.pNext = nullptr;
- colorBlending.logicOpEnable = VK_FALSE;
- colorBlending.logicOp = VK_LOGIC_OP_COPY;
- colorBlending.attachmentCount = states.size();
- colorBlending.pAttachments = states.data();
- VkPipelineMultisampleStateCreateInfo multisampling{};
- multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
- multisampling.pNext = nullptr;
- multisampling.sampleShadingEnable = VK_FALSE;
- multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
- multisampling.minSampleShading = 1.0f;
- multisampling.pSampleMask = nullptr;
- multisampling.alphaToCoverageEnable = VK_FALSE;
- multisampling.alphaToOneEnable = VK_FALSE;
- VkPipelineRasterizationStateCreateInfo rasterizerInfo{};
- rasterizerInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
- rasterizerInfo.pNext = nullptr;
- rasterizerInfo.depthClampEnable = VK_FALSE;
- rasterizerInfo.rasterizerDiscardEnable = VK_FALSE;
- rasterizerInfo.polygonMode = VK_POLYGON_MODE_FILL;
- rasterizerInfo.lineWidth = 1.0f;
- rasterizerInfo.cullMode = VK_CULL_MODE_FRONT_BIT;
- rasterizerInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
- rasterizerInfo.depthBiasEnable = VK_FALSE;
- rasterizerInfo.depthBiasConstantFactor = 0.0f;
- rasterizerInfo.depthBiasClamp = 0.0f;
- rasterizerInfo.depthBiasSlopeFactor = 0.0f;
- VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
- inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
- inputAssembly.pNext = nullptr;
- inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
- inputAssembly.primitiveRestartEnable = VK_FALSE;
- VkPipelineVertexInputStateCreateInfo attribInfo{};
- attribInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
- attribInfo.pNext = nullptr;
- attribInfo.vertexBindingDescriptionCount = 0;
- attribInfo.vertexAttributeDescriptionCount = 0;
- attribInfo.pVertexAttributeDescriptions = attributesDesc.attributes.data();
- attribInfo.vertexAttributeDescriptionCount = attributesDesc.attributes.size();
- attribInfo.pVertexBindingDescriptions = attributesDesc.bindings.data();
- attribInfo.vertexBindingDescriptionCount = attributesDesc.bindings.size();
- VkPipelineDepthStencilStateCreateInfo depthInfo{};
- depthInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
- depthInfo.pNext = nullptr;
- depthInfo.depthTestEnable = VK_TRUE;
- depthInfo.depthWriteEnable = VK_TRUE;
- depthInfo.depthCompareOp = VK_COMPARE_OP_LESS_OR_EQUAL;
- depthInfo.depthBoundsTestEnable = VK_FALSE;
- depthInfo.minDepthBounds = 0.0f;
- depthInfo.maxDepthBounds = 1.0f;
- depthInfo.stencilTestEnable = VK_FALSE;
- std::vector<VkPipelineShaderStageCreateInfo> shadersInfo;
- for (const auto& shader : shaders)
- shadersInfo.push_back(loadShader(device, shader.first, shader.second));
- VkGraphicsPipelineCreateInfo pipelineInfo = {};
- pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
- pipelineInfo.pNext = nullptr;
- pipelineInfo.stageCount = shadersInfo.size();
- pipelineInfo.pStages = shadersInfo.data();
- pipelineInfo.pVertexInputState = &attribInfo;
- pipelineInfo.pInputAssemblyState = &inputAssembly;
- pipelineInfo.pViewportState = &viewportState;
- pipelineInfo.pRasterizationState = &rasterizerInfo;
- pipelineInfo.pMultisampleState = &multisampling;
- pipelineInfo.pColorBlendState = &colorBlending;
- pipelineInfo.layout = result.layout;
- pipelineInfo.renderPass = pass;
- pipelineInfo.subpass = 0;
- pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
- pipelineInfo.pDepthStencilState = &depthInfo;
- vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &result.pipeline);
- return result;
- }
- Pipeline buildDeferredPBRPipeline(VkDevice device,
- VkExtent2D size,
- VkRenderPass pass,
- const std::vector<VkDescriptorSetLayout>& setLayouts,
- const std::vector<std::pair<std::string, VkShaderStageFlagBits>>& shaders)
- {
- Pipeline result;
- VkViewport viewport;
- viewport.x = 0.0f;
- viewport.y = 0.0f;
- viewport.width = (float)size.width;
- viewport.height = (float)size.height;
- viewport.minDepth = 0.0f;
- viewport.maxDepth = 1.0f;
- VkRect2D scissor;
- scissor.offset = {0, 0};
- scissor.extent = size;
- VkPushConstantRange pushConstant;
- pushConstant.offset = 0;
- pushConstant.size = sizeof(int);
- pushConstant.stageFlags = VK_SHADER_STAGE_FRAGMENT_BIT;
- VkPipelineLayoutCreateInfo info{};
- info.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO;
- info.pNext = nullptr;
- info.flags = 0;
- info.pushConstantRangeCount = 1;
- info.pPushConstantRanges = &pushConstant;
- info.setLayoutCount = setLayouts.size();
- info.pSetLayouts = setLayouts.data();
- vkCreatePipelineLayout(device, &info, nullptr, &result.layout);
- VkPipelineViewportStateCreateInfo viewportState{};
- viewportState.sType = VK_STRUCTURE_TYPE_PIPELINE_VIEWPORT_STATE_CREATE_INFO;
- viewportState.pNext = nullptr;
- viewportState.viewportCount = 1;
- viewportState.pViewports = &viewport;
- viewportState.scissorCount = 1;
- viewportState.pScissors = &scissor;
- VkPipelineColorBlendAttachmentState attachState{};
- attachState.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
- attachState.blendEnable = VK_FALSE;
- VkPipelineColorBlendStateCreateInfo colorBlending{};
- colorBlending.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
- colorBlending.pNext = nullptr;
- colorBlending.logicOpEnable = VK_FALSE;
- colorBlending.logicOp = VK_LOGIC_OP_COPY;
- colorBlending.attachmentCount = 1;
- colorBlending.pAttachments = &attachState;
- VkPipelineMultisampleStateCreateInfo multisampling{};
- multisampling.sType = VK_STRUCTURE_TYPE_PIPELINE_MULTISAMPLE_STATE_CREATE_INFO;
- multisampling.pNext = nullptr;
- multisampling.sampleShadingEnable = VK_FALSE;
- multisampling.rasterizationSamples = VK_SAMPLE_COUNT_1_BIT;
- multisampling.minSampleShading = 1.0f;
- multisampling.pSampleMask = nullptr;
- multisampling.alphaToCoverageEnable = VK_FALSE;
- multisampling.alphaToOneEnable = VK_FALSE;
- VkPipelineRasterizationStateCreateInfo rasterizerInfo{};
- rasterizerInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_RASTERIZATION_STATE_CREATE_INFO;
- rasterizerInfo.pNext = nullptr;
- rasterizerInfo.depthClampEnable = VK_FALSE;
- rasterizerInfo.rasterizerDiscardEnable = VK_FALSE;
- rasterizerInfo.polygonMode = VK_POLYGON_MODE_FILL;
- rasterizerInfo.lineWidth = 1.0f;
- rasterizerInfo.cullMode = VK_CULL_MODE_FRONT_BIT;
- rasterizerInfo.frontFace = VK_FRONT_FACE_CLOCKWISE;
- rasterizerInfo.depthBiasEnable = VK_FALSE;
- rasterizerInfo.depthBiasConstantFactor = 0.0f;
- rasterizerInfo.depthBiasClamp = 0.0f;
- rasterizerInfo.depthBiasSlopeFactor = 0.0f;
- VkPipelineInputAssemblyStateCreateInfo inputAssembly{};
- inputAssembly.sType = VK_STRUCTURE_TYPE_PIPELINE_INPUT_ASSEMBLY_STATE_CREATE_INFO;
- inputAssembly.pNext = nullptr;
- inputAssembly.topology = VK_PRIMITIVE_TOPOLOGY_TRIANGLE_LIST;
- inputAssembly.primitiveRestartEnable = VK_FALSE;
- std::vector<VkPipelineShaderStageCreateInfo> shadersInfo;
- for (const auto& shader : shaders)
- shadersInfo.push_back(loadShader(device, shader.first, shader.second));
- VkGraphicsPipelineCreateInfo pipelineInfo{};
- pipelineInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO;
- pipelineInfo.pNext = nullptr;
- pipelineInfo.stageCount = shadersInfo.size();
- pipelineInfo.pStages = shadersInfo.data();
- pipelineInfo.pInputAssemblyState = &inputAssembly;
- pipelineInfo.pViewportState = &viewportState;
- pipelineInfo.pRasterizationState = &rasterizerInfo;
- pipelineInfo.pMultisampleState = &multisampling;
- pipelineInfo.pColorBlendState = &colorBlending;
- pipelineInfo.layout = result.layout;
- pipelineInfo.renderPass = pass;
- pipelineInfo.subpass = 0;
- pipelineInfo.basePipelineHandle = VK_NULL_HANDLE;
- vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr, &result.pipeline);
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement