diff --git a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp index 9e6968c50..860cf55d5 100644 --- a/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp +++ b/src/shader_recompiler/backend/spirv/emit_spirv_context_get_set.cpp @@ -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 { diff --git a/src/shader_recompiler/runtime_info.h b/src/shader_recompiler/runtime_info.h index e67015b3a..44b4c069b 100644 --- a/src/shader_recompiler/runtime_info.h +++ b/src/shader_recompiler/runtime_info.h @@ -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; diff --git a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp index b0c45df9a..2ec2a5288 100644 --- a/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp +++ b/src/video_core/renderer_vulkan/vk_graphics_pipeline.cpp @@ -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 diff --git a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp index 855471e38..5323c5291 100644 --- a/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp +++ b/src/video_core/renderer_vulkan/vk_pipeline_cache.cpp @@ -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