video_core: Emulate scaled min/max blending (#4768)

* video_core: Emulate scaled min/max blending

* video_core: Warn when scaled min/max blend emulation is used

* Emulate min/max alpha blend with operand-zeroing factors
This commit is contained in:
Mehran authored and GitHub committed 2026-08-29 16:27:38 +03:00
1 parent fc74821b41
commit 3e458471e5
4 files changed
+53 -6

No files matched your search

@@ -246,6 +246,10 @@ void EmitSetAttribute(EmitContext& ctx, IR::Attribute attr, Id value, u32 elemen
if (IR::IsMrt(attr)) {
const u32 index{u32(attr) - u32(IR::Attribute::RenderTarget0)};
const auto& info{ctx.frag_outputs.at(index)};
if (element < 3 && ctx.runtime_info.fs_info.color_buffers[index].blend_self_scale) {
// Emulates GCN's factor-scaled min/max blend: min/max(src*src, dst*dst).
value = ctx.OpFMul(ctx.F32[1], value, value);
}
if (info.num_components == 1) {
return op_store(info.id);
} else {
+4
View File
@@ -172,6 +172,10 @@ struct PsColorBuffer {
AmdGpu::NumberFormat num_format : 4;
AmdGpu::NumberConversion num_conversion : 3;
AmdGpu::ShaderExportFormat export_format : 4;
// GCN applies blend factors to min/max ops while Vulkan ignores them. For the self-scaled
// pattern min/max(src*src, dst*dst) the shader squares its color output instead, keeping
// the attachment in the squared domain end to end.
u32 blend_self_scale : 1;
AmdGpu::CompMapping swizzle;
bool operator==(const PsColorBuffer& other) const = default;
@@ -303,12 +303,33 @@ GraphicsPipeline::GraphicsPipeline(
const auto alpha_blend =
control.separate_alpha_blend ? LiverpoolToVK::BlendOp(control.alpha_func) : color_blend;
// Vulkan ignores blend factors for min/max, but a factor that zeroes one operand
// makes the operation collapse to a plain selection: min(s, 0) is 0 and max(s, 0)
// is s for normalized alpha. Rewrite those to the equivalent add so the result is
// exact instead of leaving the other operand to survive.
auto eff_src_alpha = src_alpha;
auto eff_dst_alpha = dst_alpha;
auto eff_alpha_blend = alpha_blend;
if (alpha_blend == vk::BlendOp::eMin || alpha_blend == vk::BlendOp::eMax) {
const bool takes_max = alpha_blend == vk::BlendOp::eMax;
if (src_alpha == vk::BlendFactor::eOne && dst_alpha == vk::BlendFactor::eZero) {
eff_alpha_blend = vk::BlendOp::eAdd;
eff_src_alpha = takes_max ? vk::BlendFactor::eOne : vk::BlendFactor::eZero;
eff_dst_alpha = vk::BlendFactor::eZero;
} else if (src_alpha == vk::BlendFactor::eZero && dst_alpha == vk::BlendFactor::eOne) {
eff_alpha_blend = vk::BlendOp::eAdd;
eff_src_alpha = vk::BlendFactor::eZero;
eff_dst_alpha = takes_max ? vk::BlendFactor::eOne : vk::BlendFactor::eZero;
}
}
const auto color_scaled_min_max =
(color_blend == vk::BlendOp::eMin || color_blend == vk::BlendOp::eMax) &&
(src_color != vk::BlendFactor::eOne || dst_color != vk::BlendFactor::eOne);
(src_color != vk::BlendFactor::eOne || dst_color != vk::BlendFactor::eOne) &&
!key.color_buffers[i].blend_self_scale;
const auto alpha_scaled_min_max =
(alpha_blend == vk::BlendOp::eMin || alpha_blend == vk::BlendOp::eMax) &&
(src_alpha != vk::BlendFactor::eOne || dst_alpha != vk::BlendFactor::eOne);
(eff_alpha_blend == vk::BlendOp::eMin || eff_alpha_blend == vk::BlendOp::eMax) &&
(eff_src_alpha != vk::BlendFactor::eOne || eff_dst_alpha != vk::BlendFactor::eOne);
if (color_scaled_min_max || alpha_scaled_min_max) {
LOG_WARNING(
Render_Vulkan,
@@ -320,9 +341,9 @@ GraphicsPipeline::GraphicsPipeline(
.srcColorBlendFactor = src_color,
.dstColorBlendFactor = dst_color,
.colorBlendOp = color_blend,
.srcAlphaBlendFactor = src_alpha,
.dstAlphaBlendFactor = dst_alpha,
.alphaBlendOp = alpha_blend,
.srcAlphaBlendFactor = eff_src_alpha,
.dstAlphaBlendFactor = eff_dst_alpha,
.alphaBlendOp = eff_alpha_blend,
.colorWriteMask =
instance.IsDynamicColorWriteMaskSupported()
? vk::ColorComponentFlagBits::eR | vk::ColorComponentFlagBits::eG |
@@ -330,6 +351,16 @@ GraphicsPipeline::GraphicsPipeline(
: key.write_masks[i],
};
// The shader squares its color output for this attachment (see PsColorBuffer), so the
// factors must not scale the operands again.
if (key.color_buffers[i].blend_self_scale) {
LOG_WARNING(
Render_Vulkan,
"Emulating scaled min/max blend with squared shader output on attachment {}", i);
attachments[i].srcColorBlendFactor = vk::BlendFactor::eOne;
attachments[i].dstColorBlendFactor = vk::BlendFactor::eOne;
}
// On GCN GPU there is an additional mask which allows to control color components exported
// from a pixel shader. A situation possible, when the game may mask out the alpha channel,
// while it is still need to be used in blending ops. For such cases, HW will default alpha
@@ -417,6 +417,14 @@ bool PipelineCache::RefreshGraphicsKey() {
color_buffer.num_conversion = col_buf.GetNumberConversion();
color_buffer.export_format = regs.color_export_format.GetFormat(cb);
color_buffer.swizzle = col_buf.Swizzle();
const auto& bc = regs.blend_control[cb];
color_buffer.blend_self_scale =
bc.enable && !col_buf.info.blend_bypass &&
(bc.color_func == AmdGpu::BlendControl::BlendFunc::Min ||
bc.color_func == AmdGpu::BlendControl::BlendFunc::Max) &&
bc.color_src_factor == AmdGpu::BlendControl::BlendFactor::SrcColor &&
bc.color_dst_factor == AmdGpu::BlendControl::BlendFactor::DstColor;
}
// Compile and bind shader stages