fix(agc): fragment SampleId, Layer and FragDepth interfaces

The recompiler emits SampleId and Layer fragment inputs (pixel ancillary
data) and a FragDepth output with the DepthReplacing execution mode
(depth export), but the validator refused them, so those draws were
skipped. They are now accepted, as are the Geometry and SampleRateShading
capabilities those inputs need in fragment shaders. The device enables
both features when it has them.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Michon
2026-10-02 17:54:34 +02:00
parent 2b8f0b215e
commit 78726f3836
2 changed files with 37 additions and 2 deletions
@@ -118,7 +118,7 @@ struct Module {
Require(value == spv::BuiltInPosition && signature == "f32x4" && !position, "unsupported or duplicate vertex built-in output");
position = true;
} else {
Require(storage == spv::StorageClassInput && ((value == spv::BuiltInFragCoord && signature == "f32x4") || ((value == spv::BuiltInFrontFacing || value == spv::BuiltInHelperInvocation) && signature == "bool")), "unsupported fragment built-in");
Require((storage == spv::StorageClassInput && ((value == spv::BuiltInFragCoord && signature == "f32x4") || ((value == spv::BuiltInFrontFacing || value == spv::BuiltInHelperInvocation) && signature == "bool") || ((value == spv::BuiltInSampleId || value == spv::BuiltInLayer) && signature == "i32"))) || (storage == spv::StorageClassOutput && value == spv::BuiltInFragDepth && signature == "f32"), "unsupported fragment built-in");
}
}
};
@@ -208,6 +208,8 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
capability == spv::CapabilityImage1D ||
capability == spv::CapabilityImageGatherExtended ||
capability == spv::CapabilityImageQuery ||
(fragment && capability == spv::CapabilityGeometry) ||
(fragment && capability == spv::CapabilitySampleRateShading) ||
capability == spv::CapabilityStorageImageWriteWithoutFormat ||
capability == spv::CapabilityStorageImageReadWithoutFormat ||
capability == spv::CapabilityInt64 ||
@@ -369,7 +371,7 @@ Module Inspect(const CompiledShader& compiled, const State& state, const VkPhysi
mode(spv::ExecutionModeVertexOrderCw, {});
Require(module.modes.size() == 3, "unsupported tessellation-evaluation execution mode");
} else if (fragment) {
for (const auto& [name, operands] : module.modes) Require(operands.empty() && (name == spv::ExecutionModeOriginUpperLeft || name == spv::ExecutionModeEarlyFragmentTests || name == spv::ExecutionModeDepthLess || name == spv::ExecutionModeDepthGreater), "unsupported fragment execution mode");
for (const auto& [name, operands] : module.modes) Require(operands.empty() && (name == spv::ExecutionModeOriginUpperLeft || name == spv::ExecutionModeEarlyFragmentTests || name == spv::ExecutionModeDepthReplacing || name == spv::ExecutionModeDepthLess || name == spv::ExecutionModeDepthGreater), "unsupported fragment execution mode");
} else Require(module.modes.empty(), "unsupported vertex execution mode");
std::set<std::pair<std::uint32_t, std::uint32_t>> descriptors;
bool push = false;
@@ -1384,6 +1384,9 @@ struct ModuleShape {
std::uint32_t parameterLocation = 0;
bool rectParameters = false;
bool secondTarget = false;
bool sampleId = false;
bool layer = false;
bool fragDepth = false;
};
void emit(std::vector<std::uint32_t>& out, spv::Op op, std::initializer_list<std::uint32_t> operands) {
@@ -1449,6 +1452,28 @@ std::vector<std::uint32_t> makeModule(const ModuleShape& shape) {
emit(annotations, spv::OpDecorate, {variable, spv::DecorationBuiltIn, shape.barycentricNoPerspective ? spv::BuiltInBaryCoordNoPerspKHR : spv::BuiltInBaryCoordKHR});
extraInterface.push_back(variable);
}
if (shape.sampleId || shape.layer) {
const auto intType = id();
const auto pointer = id();
emit(declarations, spv::OpTypeInt, {intType, 32, 1});
emit(declarations, spv::OpTypePointer, {pointer, spv::StorageClassInput, intType});
for (const auto [wanted, builtin] : {std::pair{shape.sampleId, spv::BuiltInSampleId}, std::pair{shape.layer, spv::BuiltInLayer}}) {
if (!wanted) continue;
const auto variable = id();
emit(declarations, spv::OpVariable, {pointer, variable, spv::StorageClassInput});
emit(annotations, spv::OpDecorate, {variable, spv::DecorationBuiltIn, static_cast<std::uint32_t>(builtin)});
emit(annotations, spv::OpDecorate, {variable, spv::DecorationFlat});
extraInterface.push_back(variable);
}
}
if (shape.fragDepth) {
const auto pointer = id();
const auto variable = id();
emit(declarations, spv::OpTypePointer, {pointer, spv::StorageClassOutput, floatType});
emit(declarations, spv::OpVariable, {pointer, variable, spv::StorageClassOutput});
emit(annotations, spv::OpDecorate, {variable, spv::DecorationBuiltIn, spv::BuiltInFragDepth});
extraInterface.push_back(variable);
}
if (shape.perVertex) {
const auto length = id();
const auto array = id();
@@ -1516,6 +1541,8 @@ std::vector<std::uint32_t> makeModule(const ModuleShape& shape) {
emit(function, spv::OpFunctionEnd, {});
std::vector<std::uint32_t> words{spv::MagicNumber, 0x10300, 0, next, 0};
emit(words, spv::OpCapability, {spv::CapabilityShader});
if (shape.sampleId) emit(words, spv::OpCapability, {spv::CapabilitySampleRateShading});
if (shape.layer) emit(words, spv::OpCapability, {spv::CapabilityGeometry});
if (shape.barycentric) {
emit(words, spv::OpCapability, {spv::CapabilityFragmentBarycentricKHR});
const std::string extension = "SPV_KHR_fragment_shader_barycentric";
@@ -1533,6 +1560,7 @@ std::vector<std::uint32_t> makeModule(const ModuleShape& shape) {
words.insert(words.end(), extraInterface.begin(), extraInterface.end());
if (shape.fragmentMode) emit(words, spv::OpExecutionMode, {main, *shape.fragmentMode});
if (shape.fragment) emit(words, spv::OpExecutionMode, {main, spv::ExecutionModeOriginUpperLeft});
if (shape.fragDepth) emit(words, spv::OpExecutionMode, {main, spv::ExecutionModeDepthReplacing});
words.insert(words.end(), annotations.begin(), annotations.end());
words.insert(words.end(), declarations.begin(), declarations.end());
words.insert(words.end(), function.begin(), function.end());
@@ -1782,6 +1810,11 @@ void validationTests() {
AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, true);
expectFailure([&] { AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, false); }, "fragmentShaderBarycentric");
}
pixel.spirv = makeModule({.fragment = true, .sampleId = true, .layer = true, .fragDepth = true});
AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, false);
vertex.spirv = makeModule({.parameterOutput = true, .sampleId = true});
expectFailure([&] { AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, false); }, "unsupported device capability 35");
vertex.spirv = makeModule({.parameterOutput = true});
pixel.spirv = makeModule({.fragment = true, .barycentric = true, .barycentricComponents = 4});
expectFailure([&] { AgcDriver::Graphics::ValidateShaders(shaders, state, subgroup, true); }, "invalid barycentric built-in");
pixel.spirv = makeModule({.fragment = true, .barycentric = true, .perVertex = true, .perVertexLength = 2});