Skip to content

Commit

Permalink
Vulkan blend factor: Bugfix and minor optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
hrydgard committed Oct 13, 2019
1 parent 29950c0 commit 1e3711e
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 10 deletions.
10 changes: 9 additions & 1 deletion GPU/Vulkan/PipelineManagerVulkan.cpp
Expand Up @@ -126,7 +126,15 @@ static int SetupVertexAttribsPretransformed(VkVertexInputAttributeDescription at
}

static bool UsesBlendConstant(int factor) {
return factor == VK_BLEND_FACTOR_CONSTANT_ALPHA || factor == VK_BLEND_FACTOR_CONSTANT_COLOR;
switch (factor) {
case VK_BLEND_FACTOR_CONSTANT_ALPHA:
case VK_BLEND_FACTOR_CONSTANT_COLOR:
case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_ALPHA:
case VK_BLEND_FACTOR_ONE_MINUS_CONSTANT_COLOR:
return true;
default:
return false;
}
}

static VulkanPipeline *CreateVulkanPipeline(VkDevice device, VkPipelineCache pipelineCache,
Expand Down
4 changes: 1 addition & 3 deletions GPU/Vulkan/StateMappingVulkan.cpp
Expand Up @@ -413,8 +413,6 @@ void DrawEngineVulkan::ApplyDrawStateLate(VulkanRenderManager *renderManager, bo
renderManager->SetStencilParams(dynState_.stencilWriteMask, dynState_.stencilCompareMask, applyStencilRef ? stencilRef : dynState_.stencilRef);
}
if (gstate_c.IsDirty(DIRTY_BLEND_STATE) && useBlendConstant) {
float bc[4];
Uint8x4ToFloat4(bc, dynState_.blendColor);
renderManager->SetBlendFactor(bc);
renderManager->SetBlendFactor(dynState_.blendColor);
}
}
15 changes: 15 additions & 0 deletions ext/native/math/dataconv.h
Expand Up @@ -46,6 +46,21 @@ inline void Uint8x4ToFloat4(float f[4], uint32_t u) {
#endif
}

// Could be SSE optimized.
inline uint32_t Float4ToUint8x4(const float f[4]) {
int i4[4];
for (int i = 0; i < 4; i++) {
if (f[i] > 1.0f) {
i4[i] = 255;
} else if (f[i] < 0.0f) {
i4[i] = 0;
} else {
i4[i] = (int)(f[i] * 255.0f);
}
}
return i4[0] | (i4[1] << 8) | (i4[2] << 16) | (i4[3] << 24);
}

inline void Uint8x3ToFloat4_AlphaUint8(float f[4], uint32_t u, uint8_t alpha) {
#if defined(_M_SSE) || PPSSPP_PLATFORM(ARM_NEON)
Uint8x4ToFloat4(f, (u & 0xFFFFFF) | (alpha << 24));
Expand Down
8 changes: 6 additions & 2 deletions ext/native/thin3d/VulkanQueueRunner.cpp
Expand Up @@ -851,7 +851,7 @@ void VulkanQueueRunner::LogRenderPass(const VKRStep &pass) {
ILOG(" BindPipeline(%x)", (int)(intptr_t)cmd.pipeline.pipeline);
break;
case VKRRenderCommand::BLEND:
ILOG(" Blend(%f, %f, %f, %f)", cmd.blendColor.color[0], cmd.blendColor.color[1], cmd.blendColor.color[2], cmd.blendColor.color[3]);
ILOG(" BlendColor(%08x)", cmd.blendColor.color);
break;
case VKRRenderCommand::CLEAR:
ILOG(" Clear");
Expand Down Expand Up @@ -1015,8 +1015,12 @@ void VulkanQueueRunner::PerformRenderPass(const VKRStep &step, VkCommandBuffer c
break;

case VKRRenderCommand::BLEND:
vkCmdSetBlendConstants(cmd, c.blendColor.color);
{
float bc[4];
Uint8x4ToFloat4(bc, c.blendColor.color);
vkCmdSetBlendConstants(cmd, bc);
break;
}

case VKRRenderCommand::PUSH_CONSTANTS:
vkCmdPushConstants(cmd, c.push.pipelineLayout, c.push.stages, c.push.offset, c.push.size, c.push.data);
Expand Down
2 changes: 1 addition & 1 deletion ext/native/thin3d/VulkanQueueRunner.h
Expand Up @@ -141,7 +141,7 @@ struct VkRenderData {
uint8_t stencilRef;
} stencil;
struct {
float color[4];
uint32_t color;
} blendColor;
struct {
VkPipelineLayout pipelineLayout;
Expand Down
4 changes: 2 additions & 2 deletions ext/native/thin3d/VulkanRenderManager.h
Expand Up @@ -164,10 +164,10 @@ class VulkanRenderManager {
curRenderStep_->commands.push_back(data);
}

void SetBlendFactor(float color[4]) {
void SetBlendFactor(uint32_t color) {
_dbg_assert_(G3D, curRenderStep_ && curRenderStep_->stepType == VKRStepType::RENDER);
VkRenderData data{ VKRRenderCommand::BLEND };
CopyFloat4(data.blendColor.color, color);
data.blendColor.color = color;
curRenderStep_->commands.push_back(data);
}

Expand Down
3 changes: 2 additions & 1 deletion ext/native/thin3d/thin3d_vulkan.cpp
Expand Up @@ -1055,7 +1055,8 @@ void VKContext::SetViewports(int count, Viewport *viewports) {
}

void VKContext::SetBlendFactor(float color[4]) {
renderManager_.SetBlendFactor(color);
uint32_t col = Float4ToUint8x4(color);
renderManager_.SetBlendFactor(col);
}

void VKContext::SetStencilRef(uint8_t stencilRef) {
Expand Down

0 comments on commit 1e3711e

Please sign in to comment.