diff --git a/engine/src/vulkan/pipelines/graphics_pipeline.cpp b/engine/src/vulkan/pipelines/graphics_pipeline.cpp index 5c737c8..2e578e9 100644 --- a/engine/src/vulkan/pipelines/graphics_pipeline.cpp +++ b/engine/src/vulkan/pipelines/graphics_pipeline.cpp @@ -95,6 +95,12 @@ GraphicsPipeline::Builder& GraphicsPipeline::Builder::setVertexInput( return *this; } +GraphicsPipeline::Builder& GraphicsPipeline::Builder::setAlphaBlending( + bool enabled) { + this->alphaBlending = enabled; + return *this; +} + std::unique_ptr GraphicsPipeline::Builder::build() const { if (this->vertexShader == nullptr || this->fragmentShader == nullptr) { log::fatal( @@ -128,7 +134,8 @@ std::unique_ptr GraphicsPipeline::Builder::build() const { this->device, *this->vertexShader, *this->fragmentShader, this->pipelineLayout, this->colorAttachmentFormats, this->depthAttachmentFormat, this->stencilAttachmentFormat, - this->bindingDescriptions, this->attributeDescriptions)); + this->bindingDescriptions, this->attributeDescriptions, + this->alphaBlending)); } GraphicsPipeline::GraphicsPipeline( @@ -137,10 +144,21 @@ GraphicsPipeline::GraphicsPipeline( std::vector colorAttachmentFormats, vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat, std::vector bindingDescriptions, - std::vector attributeDescriptions) + std::vector attributeDescriptions, + bool alphaBlending) : Pipeline(device, vk::PipelineBindPoint::eGraphics) { 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.attributeDescriptions = std::move(attributeDescriptions); this->config.pipelineLayout = pipelineLayout; diff --git a/engine/src/vulkan/pipelines/graphics_pipeline.hpp b/engine/src/vulkan/pipelines/graphics_pipeline.hpp index 2a77943..fe0d46e 100644 --- a/engine/src/vulkan/pipelines/graphics_pipeline.hpp +++ b/engine/src/vulkan/pipelines/graphics_pipeline.hpp @@ -47,7 +47,8 @@ class GraphicsPipeline : public Pipeline { std::vector colorAttachmentFormats, vk::Format depthAttachmentFormat, vk::Format stencilAttachmentFormat, std::vector bindingDescriptions, - std::vector attributeDescriptions); + std::vector attributeDescriptions, + bool alphaBlending); void setDefaultGraphicsPipelineConfig(); @@ -69,6 +70,8 @@ class GraphicsPipeline::Builder { Builder& setVertexInput( std::vector bindingDescriptions, std::vector attributeDescriptions); + // Straight (non-premultiplied) "over" blending on every color attachment. + Builder& setAlphaBlending(bool enabled = true); std::unique_ptr build() const; @@ -83,6 +86,7 @@ class GraphicsPipeline::Builder { vk::Format stencilAttachmentFormat = vk::Format::eUndefined; std::vector bindingDescriptions{}; std::vector attributeDescriptions{}; + bool alphaBlending = false; }; } // namespace sophia