mirror of
https://git.citron-emu.org/citron/emulator
synced 2025-12-21 03:23:34 +00:00
shader_recompiler: Fix alpha-to-coverage fragment output interface
The Vulkan spec requires fragment shaders to declare an output covering Location 0, Component 3 (alpha) when alpha-to-coverage is enabled. This change: - Tracks alpha_to_coverage_enabled through RuntimeInfo from pipeline state - Forces declaration of frag_color[0] with full RGBA when enabled - Initializes alpha to 1.0 in shader epilogue if not explicitly written Signed-off-by: Zephyron <zephyron@citron-emu.org>
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include "shader_recompiler/backend/spirv/emit_spirv.h"
|
||||
@@ -61,6 +62,29 @@ Id ComparisonFunction(EmitContext& ctx, CompareFunction comparison, Id operand_1
|
||||
throw InvalidArgument("Comparison function {}", comparison);
|
||||
}
|
||||
|
||||
void InitializeAlphaToCoverage(EmitContext& ctx) {
|
||||
if (!ctx.runtime_info.alpha_to_coverage_enabled || !Sirit::ValidId(ctx.frag_color[0])) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Load the current color value
|
||||
const Id current_color{ctx.OpLoad(ctx.F32[4], ctx.frag_color[0])};
|
||||
|
||||
// Extract RGB components
|
||||
const Id r{ctx.OpCompositeExtract(ctx.F32[1], current_color, 0u)};
|
||||
const Id g{ctx.OpCompositeExtract(ctx.F32[1], current_color, 1u)};
|
||||
const Id b{ctx.OpCompositeExtract(ctx.F32[1], current_color, 2u)};
|
||||
|
||||
// Set alpha to 1.0 for alpha-to-coverage
|
||||
const Id alpha{ctx.Const(1.0f)};
|
||||
|
||||
// Reconstruct the color with alpha = 1.0
|
||||
const Id new_color{ctx.OpCompositeConstruct(ctx.F32[4], r, g, b, alpha)};
|
||||
|
||||
// Store the updated color
|
||||
ctx.OpStore(ctx.frag_color[0], new_color);
|
||||
}
|
||||
|
||||
void AlphaTest(EmitContext& ctx) {
|
||||
if (!ctx.runtime_info.alpha_test_func) {
|
||||
return;
|
||||
@@ -121,6 +145,7 @@ void EmitEpilogue(EmitContext& ctx) {
|
||||
ConvertDepthMode(ctx);
|
||||
}
|
||||
if (ctx.stage == Stage::Fragment) {
|
||||
InitializeAlphaToCoverage(ctx);
|
||||
AlphaTest(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
@@ -1664,7 +1665,8 @@ void EmitContext::DefineOutputs(const IR::Program& program) {
|
||||
break;
|
||||
case Stage::Fragment:
|
||||
for (u32 index = 0; index < 8; ++index) {
|
||||
if (!info.stores_frag_color[index] && !profile.need_declared_frag_colors) {
|
||||
if (!info.stores_frag_color[index] && !profile.need_declared_frag_colors &&
|
||||
!(index == 0 && runtime_info.alpha_to_coverage_enabled)) {
|
||||
continue;
|
||||
}
|
||||
frag_color[index] = DefineOutput(*this, F32[4], std::nullopt);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
@@ -93,6 +94,7 @@ struct RuntimeInfo {
|
||||
std::optional<float> fixed_state_point_size;
|
||||
std::optional<CompareFunction> alpha_test_func;
|
||||
float alpha_test_reference{};
|
||||
bool alpha_to_coverage_enabled{};
|
||||
|
||||
/// Static Y negate value
|
||||
bool y_negate{};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2021 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#pragma once
|
||||
@@ -317,6 +318,7 @@ struct Info {
|
||||
bool uses_rescaling_uniform{};
|
||||
bool uses_cbuf_indirect{};
|
||||
bool uses_render_area{};
|
||||
bool alpha_to_coverage_enabled{};
|
||||
|
||||
IR::Type used_constant_buffer_types{};
|
||||
IR::Type used_storage_buffer_types{};
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
// SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
|
||||
// SPDX-FileCopyrightText: Copyright 2025 citron Emulator Project
|
||||
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||
|
||||
#include <algorithm>
|
||||
@@ -227,6 +228,7 @@ Shader::RuntimeInfo MakeRuntimeInfo(std::span<const Shader::IR::Program> program
|
||||
info.alpha_test_func = MaxwellToCompareFunction(
|
||||
key.state.UnpackComparisonOp(key.state.alpha_test_func.Value()));
|
||||
info.alpha_test_reference = Common::BitCast<float>(key.state.alpha_test_ref);
|
||||
info.alpha_to_coverage_enabled = key.state.alpha_to_coverage_enabled != 0;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user