Vulkan:支持sprite的alpha通道

时间:2018-03-17 07:36:33

标签: vulkan

我试图绘制一些精灵,其中考虑了图像的alpha通道。

以下结构的正确值集是什么,以支持片段着色器中纹理的Alpha通道?

vk::PipelineColorBlendAttachmentState colorBlendAttachment; 
colorBlendAttachment.colorWriteMask = vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG | vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
colorBlendAttachment.blendEnable    = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eOne;
colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eZero;
colorBlendAttachment.colorBlendOp        = vk::BlendOp::eAdd;   
colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eOne;
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eZero;  
colorBlendAttachment.alphaBlendOp        = vk::BlendOp::eSubtract;


vk::PipelineColorBlendStateCreateInfo colorBlending;    
colorBlending.logicOpEnable     = VK_FALSE;
colorBlending.logicOp           = vk::LogicOp::eCopy;
colorBlending.attachmentCount   = 1;
colorBlending.pAttachments      = &colorBlendAttachment;
colorBlending.blendConstants[0] = 0.0f;
colorBlending.blendConstants[1] = 0.0f;
colorBlending.blendConstants[2] = 0.0f;
colorBlending.blendConstants[3] = 0.0f;

1 个答案:

答案 0 :(得分:1)

根据Ekzusy的回答,这里有两种方式:

使用' discard'片段着色器中的关键字。

// Read data from some texture.
vec4 color = texture(...); 
// This makes the alpha channel (w component) act as a boolean.    
if (color.w < 1) { discard; }

对于我原来的问题,这些值将会:

vk::PipelineColorBlendAttachmentState colorBlendAttachment;
colorBlendAttachment.colorWriteMask = 
  vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
  vk::ColorComponentFlagBits::eB | vk::ColorComponentFlagBits::eA;
colorBlendAttachment.blendEnable = VK_TRUE;
colorBlendAttachment.srcColorBlendFactor = vk::BlendFactor::eSrcAlpha;
colorBlendAttachment.dstColorBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
colorBlendAttachment.colorBlendOp = vk::BlendOp::eAdd;
colorBlendAttachment.srcAlphaBlendFactor = vk::BlendFactor::eSrcAlpha;
colorBlendAttachment.dstAlphaBlendFactor = vk::BlendFactor::eOneMinusSrcAlpha;
colorBlendAttachment.alphaBlendOp = vk::BlendOp::eSubtract;