Skip to content

Instantly share code, notes, and snippets.

@nyorain
Created March 24, 2018 20:10
Show Gist options
  • Save nyorain/941c8da1f2c4b782cf66fa4fab560674 to your computer and use it in GitHub Desktop.
Save nyorain/941c8da1f2c4b782cf66fa4fab560674 to your computer and use it in GitHub Desktop.
// GraphicsPipelineInfo
GraphicsPipelineInfo::GraphicsPipelineInfo(vk::RenderPass renderPass,
vk::PipelineLayout layout, vk::SampleCountBits samples,
vpp::ShaderProgram&& prog)
{
static const auto dynStates = {
vk::DynamicState::viewport,
vk::DynamicState::scissor};
blendAttachment.blendEnable = true;
blendAttachment.alphaBlendOp = vk::BlendOp::add;
blendAttachment.colorBlendOp = vk::BlendOp::add;
blendAttachment.srcColorBlendFactor = vk::BlendFactor::srcAlpha;
blendAttachment.dstColorBlendFactor = vk::BlendFactor::oneMinusSrcAlpha;
blendAttachment.srcAlphaBlendFactor = vk::BlendFactor::srcAlpha;
blendAttachment.dstAlphaBlendFactor = vk::BlendFactor::oneMinusSrcAlpha;
blendAttachment.colorWriteMask =
vk::ColorComponentBits::r |
vk::ColorComponentBits::g |
vk::ColorComponentBits::b |
vk::ColorComponentBits::a;
program = std::move(prog);
info.stageCount = program.vkStageInfos().size();
info.pStages = program.vkStageInfos().data();
info.layout = layout;
info.renderPass = renderPass;
info.pVertexInputState = &vertex;
assembly.topology = vk::PrimitiveTopology::triangleFan;
info.pInputAssemblyState = &assembly;
rasterization.polygonMode = vk::PolygonMode::fill;
rasterization.cullMode = vk::CullModeBits::none;
rasterization.frontFace = vk::FrontFace::counterClockwise;
rasterization.depthClampEnable = false;
rasterization.rasterizerDiscardEnable = false;
rasterization.depthBiasEnable = false;
rasterization.lineWidth = 1.f;
info.pRasterizationState = &rasterization;
multisample.rasterizationSamples = samples;
info.pMultisampleState = &multisample;
blend.attachmentCount = 1;
blend.pAttachments = &blendAttachment;
info.pColorBlendState = &blend;
viewport.scissorCount = 1;
viewport.viewportCount = 1;
info.pViewportState = &viewport;
dynamic.dynamicStateCount = dynStates.size();
dynamic.pDynamicStates = dynStates.begin();
info.pDynamicState = &dynamic;
}
// TODO: this really has issues/unexpected behaviors
GraphicsPipelineInfo::GraphicsPipelineInfo(const GraphicsPipelineInfo& other)
{
info = other.info;
assembly = other.assembly;
rasterization = other.rasterization;
multisample = other.multisample;
blend = other.blend;
viewport = other.viewport;
dynamic = other.dynamic;
vertex = other.vertex;
program = other.program;
blendAttachment = other.blendAttachment;
info.pDynamicState = &dynamic;
info.pViewportState = &viewport;
info.pVertexInputState = &vertex;
info.pMultisampleState = &multisample;
info.pColorBlendState = &blend;
info.pInputAssemblyState = &assembly;
info.pRasterizationState = &rasterization;
info.pColorBlendState = &blend;
info.stageCount = program.vkStageInfos().size();
info.pStages = program.vkStageInfos().data();
blend.pAttachments = &blendAttachment;
}
GraphicsPipelineInfo& GraphicsPipelineInfo::operator=(const GraphicsPipelineInfo& other)
{
info = other.info;
assembly = other.assembly;
rasterization = other.rasterization;
multisample = other.multisample;
blendAttachment = other.blendAttachment;
blend = other.blend;
viewport = other.viewport;
dynamic = other.dynamic;
vertex = other.vertex;
program = other.program;
blendAttachment = other.blendAttachment;
info.pDynamicState = &dynamic;
info.pViewportState = &viewport;
info.pMultisampleState = &multisample;
info.pVertexInputState = &vertex;
info.pColorBlendState = &blend;
info.pInputAssemblyState = &assembly;
info.pRasterizationState = &rasterization;
info.pColorBlendState = &blend;
info.stageCount = program.vkStageInfos().size();
info.pStages = program.vkStageInfos().data();
blend.pAttachments = &blendAttachment;
return *this;
}
/// Checklist for things you have to set yourself:
/// - vertex input state stuff (all of vertexInfo), empty by default
/// - [assembly info] default is triangle fan
/// - [blend attachment] default is enabled, simple default add
/// - when changing the program make sure to update the info.stages (size + data)
class GraphicsPipelineInfo {
public:
vk::GraphicsPipelineCreateInfo info {};
vk::PipelineVertexInputStateCreateInfo vertex {};
vk::PipelineInputAssemblyStateCreateInfo assembly {};
vk::PipelineRasterizationStateCreateInfo rasterization {};
vk::PipelineMultisampleStateCreateInfo multisample {};
vk::PipelineColorBlendAttachmentState blendAttachment {};
vk::PipelineColorBlendStateCreateInfo blend {};
vk::PipelineViewportStateCreateInfo viewport {};
vk::PipelineDynamicStateCreateInfo dynamic {};
public:
GraphicsPipelineInfo(vk::RenderPass, vk::PipelineLayout,
vk::SampleCountBits, vpp::ShaderProgram&&);
GraphicsPipelineInfo(const GraphicsPipelineInfo& other);
GraphicsPipelineInfo& operator=(const GraphicsPipelineInfo& other);
void program(vpp::ShaderProgram);
auto& program() const { return program; }
void addBlendAttachment(const vk::PipelineColorBlendAttachmentState&);
auto& blendAttachments() const { return blendAttachments; }
auto blendAttachments(Span<const vk::PipelineColorBlendAttachmentState>);
private:
vpp::ShaderProgram program {};
std::vector<vk::PipelineColorBlendAttachmentState> blendAttachments {};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment