How set blending ?

I create graphic pipeline with blend (VkGraphicsPipelineCreateInfo pipeline.pColorBlendState = &cb)
Vulkan have next blend parameters:
[b]
VkPipelineColorBlendStateCreateInfo cb
cb.sType = VK_STRUCTURE_TYPE_PIPELINE_COLOR_BLEND_STATE_CREATE_INFO

VkPipelineColorBlendAttachmentState att_state0
 att_state0.colorWriteMask = 0xf 
 att_state0.blendEnable = VK_TRUE
 att_state0.alphaBlendOp = VK_BLEND_OP_ADD
 att_state0.colorBlendOp = VK_BLEND_OP_ADD
 att_state0.srcColorBlendFactor =  VK_BLEND_FACTOR_SRC_COLOR 
 att_state0.dstColorBlendFactor =  VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA 
 att_state0.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE 
 att_state0.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO
 cb.attachmentCount = 1
 cb.pAttachments = &att_state0
 cb.logicOpEnable = 0 
 cb.logicOp = VK_LOGIC_OP_NO_OP
Ejj = 1.0 
 cb.blendConstants0 = Ejj
 cb.blendConstants1 = Ejj
 cb.blendConstants2 = Ejj
 cb.blendConstants3 = Ejj   

[/b]
Where is read about all this parameters and what his do ?

      • Updated - - -

I changed next parameters, but not see on screen changes :

att_state0.alphaBlendOp = VK_BLEND_OP_ADD
att_state0.colorBlendOp = VK_BLEND_OP_ADD
att_state0.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_COLOR
att_state0.dstColorBlendFactor = VK_BLEND_FACTOR_ONE_MINUS_SRC_ALPHA
att_state0.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE
att_state0.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO

I try this but nothing changed on screen :

cb.logicOpEnable = VK_TRUE
cb.logicOp = VK_LOGIC_OP_AND
cb.blendConstants0 = 0.7
cb.blendConstants1 = 0.1
cb.blendConstants2 = 0.2
cb.blendConstants3 = 0.5
att_state0.alphaBlendOp = VK_BLEND_OP_MIN
att_state0.colorBlendOp = VK_BLEND_OP_ADD
att_state0.srcColorBlendFactor = VK_BLEND_FACTOR_SRC_COLOR
att_state0.dstColorBlendFactor = VK_BLEND_FACTOR_ONE
att_state0.srcAlphaBlendFactor = VK_BLEND_FACTOR_ONE
att_state0.dstAlphaBlendFactor = VK_BLEND_FACTOR_ZERO

Blending is described in detail in chapter 26.1 of the specs

A basic blending setup (for additive blending) could look like this :

blendAttachmentState.colorWriteMask = 0xF;
blendAttachmentState.blendEnable = VK_TRUE;
blendAttachmentState.colorBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.srcColorBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.dstColorBlendFactor = VK_BLEND_FACTOR_ONE;
blendAttachmentState.alphaBlendOp = VK_BLEND_OP_ADD;
blendAttachmentState.srcAlphaBlendFactor = VK_BLEND_FACTOR_SRC_ALPHA;
blendAttachmentState.dstAlphaBlendFactor = VK_BLEND_FACTOR_DST_ALPHA;

Note that if you do blending in a separate renderpass you need to keep the contents from the last renderpass by specifying using the correct storeOp.

1 Like

Troubles with blending.
Not see hero through leafs.
How fix ?

Your blending works as expected. Your problem is depth sorting. If you blend, you need to do some sorting. Either sort all blended triangles back-to-front, use something like depth peeling (or any other OIT tech) or just render everything that get’s blended last. Note that this is not specific to Vulkan but applies for all 3D APIs.

An alternative (which works fine with foliage) may be using discard in the FS to discard fragments below a certain alpha value. I’m using this for rendering foliage in my indirect drawing example (https://github.com/SaschaWillems/Vulkan/tree/master/indirectdraw). This requires no depth sorting and may be an alternative for your scenario.

O ! I fixed :slight_smile:

PS: Sascha Willems thanks for your post. I thinking about this. And now do right.