Vulkan: Color Blend State for Multi Attachment Framebuffer #108419

Merged
Jeroen Bakker merged 1 commits from Jeroen-Bakker/blender:vulkan-color-blend-state into main 2023-05-30 13:46:16 +02:00
4 changed files with 63 additions and 18 deletions

View File

@ -111,6 +111,18 @@ class VKFrameBuffer : public FrameBuffer {
return vk_image_;
}
/**
* Is this framebuffer immutable?
*
* Framebuffers that are owned by GHOST are immutable and
* don't have any attachments assigned. It should be assumed that there is a single color texture
* in slot 0.
*/
bool is_immutable() const
{
return immutable_;
}
private:
void update_attachments();
void render_pass_free();

View File

@ -177,7 +177,8 @@ void VKPipeline::finalize(VKContext &context,
pipeline_create_info.pMultisampleState = &multisample_state;
/* States from the state manager. */
const VKPipelineStateManager &state_manager = state_manager_get();
VKPipelineStateManager &state_manager = state_manager_get();
state_manager.finalize_color_blend_state(framebuffer);
pipeline_create_info.pColorBlendState = &state_manager.pipeline_color_blend_state;
pipeline_create_info.pRasterizationState = &state_manager.rasterization_state;
pipeline_create_info.pDepthStencilState = &state_manager.depth_stencil_state;

View File

@ -6,6 +6,8 @@
*/
#include "vk_pipeline_state.hh"
#include "vk_framebuffer.hh"
#include "vk_texture.hh"
namespace blender::gpu {
VKPipelineStateManager::VKPipelineStateManager()
@ -20,14 +22,11 @@ VKPipelineStateManager::VKPipelineStateManager()
depth_stencil_state = {};
depth_stencil_state.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
/* TODO should be extracted from current frame-buffer and should not be done here and now. */
/* When the attachments differ the state should be forced. */
VkPipelineColorBlendAttachmentState color_blend_attachment = {};
color_blend_attachment.colorWriteMask = VK_COLOR_COMPONENT_R_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_A_BIT;
color_blend_attachments.append(color_blend_attachment);
pipeline_color_blend_state.attachmentCount = color_blend_attachments.size();
pipeline_color_blend_state.pAttachments = color_blend_attachments.data();
color_blend_attachment_template = {};
color_blend_attachment_template.colorWriteMask = VK_COLOR_COMPONENT_R_BIT |
VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT;
}
void VKPipelineStateManager::set_state(const GPUState &state, const GPUStateMutable &mutable_state)
@ -74,10 +73,42 @@ void VKPipelineStateManager::force_state(const GPUState &state,
set_state(state, mutable_state);
}
void VKPipelineStateManager::finalize_color_blend_state(const VKFrameBuffer &framebuffer)
{
color_blend_attachments.clear();
if (framebuffer.is_immutable()) {
/* Immutable framebuffers are owned by GHOST and don't have any attachments assigned. In this
* case we assume that there is a single color texture assigned.
*/
color_blend_attachments.append(color_blend_attachment_template);
}
else {
bool is_sequential = true;
for (int color_slot = 0; color_slot < GPU_FB_MAX_COLOR_ATTACHMENT; color_slot++) {
VKTexture *texture = unwrap(unwrap(framebuffer.color_tex(color_slot)));
if (texture) {
BLI_assert(is_sequential);
color_blend_attachments.append(color_blend_attachment_template);
}
else {
/* Test to detect if all color textures are sequential attached from the first slot. We
* assume at this moment that this is the case. Otherwise we need to rewire how attachments
* and bindings work.*/
is_sequential = false;
}
}
UNUSED_VARS_NDEBUG(is_sequential);
}
pipeline_color_blend_state.attachmentCount = color_blend_attachments.size();
pipeline_color_blend_state.pAttachments = color_blend_attachments.data();
}
void VKPipelineStateManager::set_blend(const eGPUBlend blend)
{
VkPipelineColorBlendStateCreateInfo &cb = pipeline_color_blend_state;
VkPipelineColorBlendAttachmentState &att_state = color_blend_attachments.last();
VkPipelineColorBlendAttachmentState &att_state = color_blend_attachment_template;
att_state.blendEnable = VK_TRUE;
att_state.alphaBlendOp = VK_BLEND_OP_ADD;
@ -186,20 +217,19 @@ void VKPipelineStateManager::set_write_mask(const eGPUWriteMask write_mask)
{
depth_stencil_state.depthWriteEnable = (write_mask & GPU_WRITE_DEPTH) ? VK_TRUE : VK_FALSE;
VkPipelineColorBlendAttachmentState &att_state = color_blend_attachments.last();
att_state.colorWriteMask = 0;
color_blend_attachment_template.colorWriteMask = 0;
if ((write_mask & GPU_WRITE_RED) != 0) {
att_state.colorWriteMask |= VK_COLOR_COMPONENT_R_BIT;
color_blend_attachment_template.colorWriteMask |= VK_COLOR_COMPONENT_R_BIT;
}
if ((write_mask & GPU_WRITE_GREEN) != 0) {
att_state.colorWriteMask |= VK_COLOR_COMPONENT_G_BIT;
color_blend_attachment_template.colorWriteMask |= VK_COLOR_COMPONENT_G_BIT;
}
if ((write_mask & GPU_WRITE_BLUE) != 0) {
att_state.colorWriteMask |= VK_COLOR_COMPONENT_B_BIT;
color_blend_attachment_template.colorWriteMask |= VK_COLOR_COMPONENT_B_BIT;
}
if ((write_mask & GPU_WRITE_ALPHA) != 0) {
att_state.colorWriteMask |= VK_COLOR_COMPONENT_A_BIT;
color_blend_attachment_template.colorWriteMask |= VK_COLOR_COMPONENT_A_BIT;
}
}
@ -234,8 +264,6 @@ void VKPipelineStateManager::set_depth_test(const eGPUDepthTest value)
depth_stencil_state.depthTestEnable = VK_FALSE;
depth_stencil_state.depthCompareOp = VK_COMPARE_OP_NEVER;
}
depth_stencil_state.depthBoundsTestEnable = VK_TRUE;
}
void VKPipelineStateManager::set_stencil_test(const eGPUStencilTest test,

View File

@ -12,6 +12,7 @@
#include "BLI_vector.hh"
namespace blender::gpu {
class VKFrameBuffer;
class VKPipelineStateManager {
private:
@ -20,6 +21,7 @@ class VKPipelineStateManager {
public:
VkPipelineColorBlendStateCreateInfo pipeline_color_blend_state;
VkPipelineColorBlendAttachmentState color_blend_attachment_template;
Vector<VkPipelineColorBlendAttachmentState> color_blend_attachments;
VkPipelineRasterizationStateCreateInfo rasterization_state;
VkPipelineDepthStencilStateCreateInfo depth_stencil_state;
@ -29,6 +31,8 @@ class VKPipelineStateManager {
void set_state(const GPUState &state, const GPUStateMutable &mutable_state);
void force_state(const GPUState &state, const GPUStateMutable &mutable_state);
void finalize_color_blend_state(const VKFrameBuffer &framebuffer);
private:
void set_blend(eGPUBlend blend);
void set_write_mask(eGPUWriteMask write_mask);