Adding in simple alpha blending

This commit is contained in:
2026-09-25 22:34:25 -07:00
parent b44fe94d5c
commit 5644782a1f
2 changed files with 25 additions and 3 deletions
@@ -95,6 +95,12 @@ GraphicsPipeline::Builder& GraphicsPipeline::Builder::setVertexInput(
return *this; return *this;
} }
GraphicsPipeline::Builder& GraphicsPipeline::Builder::setAlphaBlending(
bool enabled) {
this->alphaBlending = enabled;
return *this;
}
std::unique_ptr<GraphicsPipeline> GraphicsPipeline::Builder::build() const { std::unique_ptr<GraphicsPipeline> GraphicsPipeline::Builder::build() const {
if (this->vertexShader == nullptr || this->fragmentShader == nullptr) { if (this->vertexShader == nullptr || this->fragmentShader == nullptr) {
log::fatal( log::fatal(
@@ -128,7 +134,8 @@ std::unique_ptr<GraphicsPipeline> GraphicsPipeline::Builder::build() const {
this->device, *this->vertexShader, *this->fragmentShader, this->device, *this->vertexShader, *this->fragmentShader,
this->pipelineLayout, this->colorAttachmentFormats, this->pipelineLayout, this->colorAttachmentFormats,
this->depthAttachmentFormat, this->stencilAttachmentFormat, this->depthAttachmentFormat, this->stencilAttachmentFormat,
this->bindingDescriptions, this->attributeDescriptions)); this->bindingDescriptions, this->attributeDescriptions,
this->alphaBlending));
} }
GraphicsPipeline::GraphicsPipeline( GraphicsPipeline::GraphicsPipeline(
@@ -137,10 +144,21 @@ GraphicsPipeline::GraphicsPipeline(
std::vector<vk::Format> colorAttachmentFormats, std::vector<vk::Format> colorAttachmentFormats,
vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat, vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat,
std::vector<vk::VertexInputBindingDescription> bindingDescriptions, std::vector<vk::VertexInputBindingDescription> bindingDescriptions,
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions) std::vector<vk::VertexInputAttributeDescription> attributeDescriptions,
bool alphaBlending)
: Pipeline(device, vk::PipelineBindPoint::eGraphics) { : Pipeline(device, vk::PipelineBindPoint::eGraphics) {
setDefaultGraphicsPipelineConfig(); setDefaultGraphicsPipelineConfig();
if (alphaBlending) {
vk::PipelineColorBlendAttachmentState& blend =
this->config.colorBlendAttachment;
blend.blendEnable = vk::True;
blend.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
blend.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
blend.srcAlphaBlendFactor = vk::BlendFactor::eOne;
blend.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
}
this->config.bindingDescriptions = std::move(bindingDescriptions); this->config.bindingDescriptions = std::move(bindingDescriptions);
this->config.attributeDescriptions = std::move(attributeDescriptions); this->config.attributeDescriptions = std::move(attributeDescriptions);
this->config.pipelineLayout = pipelineLayout; this->config.pipelineLayout = pipelineLayout;
@@ -47,7 +47,8 @@ class GraphicsPipeline : public Pipeline {
std::vector<vk::Format> colorAttachmentFormats, std::vector<vk::Format> colorAttachmentFormats,
vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat, vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat,
std::vector<vk::VertexInputBindingDescription> bindingDescriptions, std::vector<vk::VertexInputBindingDescription> bindingDescriptions,
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions); std::vector<vk::VertexInputAttributeDescription> attributeDescriptions,
bool alphaBlending);
void setDefaultGraphicsPipelineConfig(); void setDefaultGraphicsPipelineConfig();
@@ -69,6 +70,8 @@ class GraphicsPipeline::Builder {
Builder& setVertexInput( Builder& setVertexInput(
std::vector<vk::VertexInputBindingDescription> bindingDescriptions, std::vector<vk::VertexInputBindingDescription> bindingDescriptions,
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions); std::vector<vk::VertexInputAttributeDescription> attributeDescriptions);
// Straight (non-premultiplied) "over" blending on every color attachment.
Builder& setAlphaBlending(bool enabled = true);
std::unique_ptr<GraphicsPipeline> build() const; std::unique_ptr<GraphicsPipeline> build() const;
@@ -83,6 +86,7 @@ class GraphicsPipeline::Builder {
vk::Format stencilAttachmentFormat = vk::Format::eUndefined; vk::Format stencilAttachmentFormat = vk::Format::eUndefined;
std::vector<vk::VertexInputBindingDescription> bindingDescriptions{}; std::vector<vk::VertexInputBindingDescription> bindingDescriptions{};
std::vector<vk::VertexInputAttributeDescription> attributeDescriptions{}; std::vector<vk::VertexInputAttributeDescription> attributeDescriptions{};
bool alphaBlending = false;
}; };
} // namespace sophia } // namespace sophia