Skip to content

Commit 7a96ef6

Browse files
Vulkan Type Conversions: fixed setting attachment color write masks
1 parent 3e17a10 commit 7a96ef6

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed

Graphics/GraphicsEngineVulkan/src/VulkanTypeConversions.cpp

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -708,14 +708,14 @@ VkPipelineRasterizationStateCreateInfo RasterizerStateDesc_To_VkRasterizationSta
708708

709709
// If depth clamping is enabled, before the incoming fragment's zf is compared to za, zf is clamped to
710710
// [min(n,f), max(n,f)], where n and f are the minDepth and maxDepth depth range values of the viewport
711-
// used by this fragment, respectively (25.10)
711+
// used by this fragment, respectively
712712
// This value is the opposite of clip enable
713713
RSStateCI.depthClampEnable = RasterizerDesc.DepthClipEnable ? VK_FALSE : VK_TRUE;
714714

715-
RSStateCI.rasterizerDiscardEnable = VK_FALSE; // Whether primitives are discarded immediately before the rasterization stage.
716-
RSStateCI.polygonMode = FillModeToVkPolygonMode(RasterizerDesc.FillMode); // 24.7.2
717-
RSStateCI.cullMode = CullModeToVkCullMode(RasterizerDesc.CullMode); // 24.7.1
718-
RSStateCI.frontFace = RasterizerDesc.FrontCounterClockwise ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE; // 24.7.1
715+
RSStateCI.rasterizerDiscardEnable = VK_FALSE; // Whether primitives are discarded immediately before the rasterization stage.
716+
RSStateCI.polygonMode = FillModeToVkPolygonMode(RasterizerDesc.FillMode);
717+
RSStateCI.cullMode = CullModeToVkCullMode(RasterizerDesc.CullMode);
718+
RSStateCI.frontFace = RasterizerDesc.FrontCounterClockwise ? VK_FRONT_FACE_COUNTER_CLOCKWISE : VK_FRONT_FACE_CLOCKWISE;
719719
// Depth bias (24.7.3)
720720
RSStateCI.depthBiasEnable = (RasterizerDesc.DepthBias != 0 || RasterizerDesc.SlopeScaledDepthBias != 0.f) ? VK_TRUE : VK_FALSE;
721721
RSStateCI.depthBiasConstantFactor =
@@ -726,7 +726,7 @@ VkPipelineRasterizationStateCreateInfo RasterizerStateDesc_To_VkRasterizationSta
726726
RasterizerDesc.SlopeScaledDepthBias; // a scalar factor applied to a fragment's slope in depth bias calculations.
727727
RSStateCI.lineWidth = 1.f; // If the wide lines feature is not enabled, and no element of the pDynamicStates member of
728728
// pDynamicState is VK_DYNAMIC_STATE_LINE_WIDTH, the lineWidth member of
729-
// pRasterizationState must be 1.0 (9.2)
729+
// pRasterizationState must be 1.0
730730

731731
return RSStateCI;
732732
}
@@ -792,12 +792,12 @@ VkStencilOpState StencilOpDescToVkStencilOpState(const StencilOpDesc& desc, Uint
792792

793793
// The s least significant bits of compareMask, where s is the number of bits in the stencil framebuffer attachment,
794794
// are bitwise ANDed with both the reference and the stored stencil value, and the resulting masked values are those
795-
// that participate in the comparison controlled by compareOp (25.9)
795+
// that participate in the comparison controlled by compareOp
796796
StencilState.compareMask = StencilReadMask;
797797

798798
// The least significant s bits of writeMask, where s is the number of bits in the stencil framebuffer
799799
// attachment, specify an integer mask. Where a 1 appears in this mask, the corresponding bit in the stencil
800-
// value in the depth / stencil attachment is written; where a 0 appears, the bit is not written (25.9)
800+
// value in the depth / stencil attachment is written; where a 0 appears, the bit is not written
801801
StencilState.writeMask = StencilWriteMask;
802802

803803
StencilState.reference = 0; // Set dynamically
@@ -808,7 +808,7 @@ VkStencilOpState StencilOpDescToVkStencilOpState(const StencilOpDesc& desc, Uint
808808

809809
VkPipelineDepthStencilStateCreateInfo DepthStencilStateDesc_To_VkDepthStencilStateCI(const DepthStencilStateDesc& DepthStencilDesc)
810810
{
811-
// Depth-stencil state (25.7)
811+
// Depth-stencil state
812812
VkPipelineDepthStencilStateCreateInfo DSStateCI = {};
813813

814814
DSStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_DEPTH_STENCIL_STATE_CREATE_INFO;
@@ -821,7 +821,7 @@ VkPipelineDepthStencilStateCreateInfo DepthStencilStateDesc_To_VkDepthStencilSta
821821
DSStateCI.stencilTestEnable = DepthStencilDesc.StencilEnable ? VK_TRUE : VK_FALSE; // 25.9
822822
DSStateCI.front = StencilOpDescToVkStencilOpState(DepthStencilDesc.FrontFace, DepthStencilDesc.StencilReadMask, DepthStencilDesc.StencilWriteMask);
823823
DSStateCI.back = StencilOpDescToVkStencilOpState(DepthStencilDesc.BackFace, DepthStencilDesc.StencilReadMask, DepthStencilDesc.StencilWriteMask);
824-
// Depth Bounds Test (25.8)
824+
// Depth Bounds Test
825825
DSStateCI.minDepthBounds = 0; // must be between 0.0 and 1.0, inclusive
826826
DSStateCI.maxDepthBounds = 1; // must be between 0.0 and 1.0, inclusive
827827

@@ -833,7 +833,6 @@ class BlendFactorToVkBlendFactorMapper
833833
public:
834834
BlendFactorToVkBlendFactorMapper()
835835
{
836-
// 26.1.1
837836
m_Map[BLEND_FACTOR_ZERO] = VK_BLEND_FACTOR_ZERO;
838837
m_Map[BLEND_FACTOR_ONE] = VK_BLEND_FACTOR_ONE;
839838
m_Map[BLEND_FACTOR_SRC_COLOR] = VK_BLEND_FACTOR_SRC_COLOR;
@@ -869,7 +868,6 @@ class LogicOperationToVkLogicOp
869868
public:
870869
LogicOperationToVkLogicOp()
871870
{
872-
// 26.2
873871
m_Map[LOGIC_OP_CLEAR] = VK_LOGIC_OP_CLEAR;
874872
m_Map[LOGIC_OP_SET] = VK_LOGIC_OP_SET;
875873
m_Map[LOGIC_OP_COPY] = VK_LOGIC_OP_COPY;
@@ -903,7 +901,6 @@ class BlendOperationToVkBlendOp
903901
public:
904902
BlendOperationToVkBlendOp()
905903
{
906-
// 26.1.3
907904
m_Map[BLEND_OPERATION_ADD] = VK_BLEND_OP_ADD;
908905
m_Map[BLEND_OPERATION_SUBTRACT] = VK_BLEND_OP_SUBTRACT;
909906
m_Map[BLEND_OPERATION_REV_SUBTRACT] = VK_BLEND_OP_REVERSE_SUBTRACT;
@@ -921,6 +918,20 @@ class BlendOperationToVkBlendOp
921918
std::array<VkBlendOp, BLEND_OPERATION_NUM_OPERATIONS> m_Map = {};
922919
};
923920

921+
VkColorComponentFlags ColorMaskToVkColorComponentFlags(COLOR_MASK ColorMask)
922+
{
923+
VkColorComponentFlags Flags = 0;
924+
if (ColorMask & COLOR_MASK_RED)
925+
Flags |= VK_COLOR_COMPONENT_R_BIT;
926+
if (ColorMask & COLOR_MASK_GREEN)
927+
Flags |= VK_COLOR_COMPONENT_G_BIT;
928+
if (ColorMask & COLOR_MASK_BLUE)
929+
Flags |= VK_COLOR_COMPONENT_B_BIT;
930+
if (ColorMask & COLOR_MASK_ALPHA)
931+
Flags |= VK_COLOR_COMPONENT_A_BIT;
932+
return Flags;
933+
}
934+
924935
VkPipelineColorBlendAttachmentState RenderTargetBlendDescToVkColorBlendAttachmentState(const RenderTargetBlendDesc& RTBlendDesc)
925936
{
926937
static const BlendFactorToVkBlendFactorMapper BFtoVKBF;
@@ -934,11 +945,7 @@ VkPipelineColorBlendAttachmentState RenderTargetBlendDescToVkColorBlendAttachmen
934945
AttachmentBlendState.srcAlphaBlendFactor = BFtoVKBF[RTBlendDesc.SrcBlendAlpha];
935946
AttachmentBlendState.dstAlphaBlendFactor = BFtoVKBF[RTBlendDesc.DestBlendAlpha];
936947
AttachmentBlendState.alphaBlendOp = BOtoVKBO[RTBlendDesc.BlendOpAlpha];
937-
AttachmentBlendState.colorWriteMask =
938-
((RTBlendDesc.RenderTargetWriteMask & COLOR_MASK_RED) ? VK_COLOR_COMPONENT_R_BIT : 0) |
939-
((RTBlendDesc.RenderTargetWriteMask & COLOR_MASK_GREEN) ? VK_COLOR_COMPONENT_G_BIT : 0) |
940-
((RTBlendDesc.RenderTargetWriteMask & COLOR_MASK_BLUE) ? VK_COLOR_COMPONENT_B_BIT : 0) |
941-
((RTBlendDesc.RenderTargetWriteMask & COLOR_MASK_ALPHA) ? VK_COLOR_COMPONENT_A_BIT : 0);
948+
AttachmentBlendState.colorWriteMask = ColorMaskToVkColorComponentFlags(RTBlendDesc.RenderTargetWriteMask);
942949

943950
return AttachmentBlendState;
944951
}
@@ -947,22 +954,29 @@ void BlendStateDesc_To_VkBlendStateCI(const BlendStateDesc&
947954
VkPipelineColorBlendStateCreateInfo& ColorBlendStateCI,
948955
std::vector<VkPipelineColorBlendAttachmentState>& ColorBlendAttachments)
949956
{
950-
// Color blend state (26.1)
957+
const RenderTargetBlendDesc& RT0BlendState = BSDesc.RenderTargets[0];
958+
959+
// Color blend state
951960
static const LogicOperationToVkLogicOp LogicOpToVkLogicOp;
952961
ColorBlendStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO;
953962
ColorBlendStateCI.pNext = nullptr;
954-
ColorBlendStateCI.flags = 0; // reserved for future use
955-
ColorBlendStateCI.logicOpEnable = BSDesc.RenderTargets[0].LogicOperationEnable; // 26.2
956-
ColorBlendStateCI.logicOp = LogicOpToVkLogicOp[BSDesc.RenderTargets[0].LogicOp];
963+
ColorBlendStateCI.flags = 0; // reserved for future use
964+
ColorBlendStateCI.logicOpEnable = RT0BlendState.LogicOperationEnable;
965+
ColorBlendStateCI.logicOp = LogicOpToVkLogicOp[RT0BlendState.LogicOp];
957966
ColorBlendStateCI.blendConstants[0] = 0.f; // We use dynamic blend constants
958967
ColorBlendStateCI.blendConstants[1] = 0.f;
959968
ColorBlendStateCI.blendConstants[2] = 0.f;
960969
ColorBlendStateCI.blendConstants[3] = 0.f;
961970
// attachmentCount must equal the colorAttachmentCount for the subpass in which this pipeline is used.
962971
for (uint32_t attachment = 0; attachment < ColorBlendStateCI.attachmentCount; ++attachment)
963972
{
964-
const RenderTargetBlendDesc& RTBlendState = BSDesc.IndependentBlendEnable ? BSDesc.RenderTargets[attachment] : BSDesc.RenderTargets[0];
965-
ColorBlendAttachments[attachment] = RenderTargetBlendDescToVkColorBlendAttachmentState(RTBlendState);
973+
const RenderTargetBlendDesc& RTBlendState = BSDesc.RenderTargets[attachment];
974+
VkPipelineColorBlendAttachmentState& Attachment = ColorBlendAttachments[attachment];
975+
976+
Attachment = RenderTargetBlendDescToVkColorBlendAttachmentState(BSDesc.IndependentBlendEnable ? RTBlendState : RT0BlendState);
977+
978+
// Color write mask should always be set for each attachment even if blend is disabled
979+
Attachment.colorWriteMask = ColorMaskToVkColorComponentFlags(RTBlendState.RenderTargetWriteMask);
966980
}
967981
}
968982

@@ -990,7 +1004,7 @@ void InputLayoutDesc_To_VkVertexInputStateCI(const InputLayoutDesc&
9901004
std::array<VkVertexInputAttributeDescription, MAX_LAYOUT_ELEMENTS>& AttributeDescription,
9911005
std::array<VkVertexInputBindingDivisorDescriptionEXT, MAX_LAYOUT_ELEMENTS>& VertexBindingDivisors)
9921006
{
993-
// Vertex input description (20.2)
1007+
// Vertex input description
9941008
VertexInputStateCI.sType = VK_STRUCTURE_TYPE_PIPELINE_VERTEX_INPUT_STATE_CREATE_INFO;
9951009
VertexInputStateCI.pNext = nullptr;
9961010
VertexInputStateCI.flags = 0; // reserved for future use.

0 commit comments

Comments
 (0)