From 6492906ebe59b573f6243e7c44476944b9dd5592 Mon Sep 17 00:00:00 2001 From: Yong He Date: Thu, 7 Mar 2024 14:04:30 -0800 Subject: Fix SPIRV emit logic of `PrimitiveId` in fragment shader. (#3705) --- source/slang/slang-emit-spirv.cpp | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'source') diff --git a/source/slang/slang-emit-spirv.cpp b/source/slang/slang-emit-spirv.cpp index e37d09af7..098c2cc2b 100644 --- a/source/slang/slang-emit-spirv.cpp +++ b/source/slang/slang-emit-spirv.cpp @@ -3461,6 +3461,17 @@ struct SPIRVEmitContext varInst, builtinVal ); + + if (storageClass == SpvStorageClassInput || + storageClass == SpvStorageClassOutput) + { + switch (builtinVal) + { + case SpvBuiltInPrimitiveId: + _maybeEmitInterpolationModifierDecoration(IRInterpolationMode::NoInterpolation, varInst); + break; + } + } m_builtinGlobalVars[key] = varInst; return varInst; } @@ -3565,6 +3576,37 @@ struct SPIRVEmitContext } else if (semanticName == "sv_primitiveid") { + auto entryPoints = m_referencingEntryPoints.tryGetValue(inst); + // SPIRV requires `Geometry` capability being declared for a fragment + // shader, if that shader uses sv_primitiveid. + // We will check if this builtin is used by non-ray-tracing, non-geometry or + // non-tessellation shader stages, and if so include a declaration of + // Geometry capability. + bool needGeometryCapability = true; + if (entryPoints) + { + for (auto entryPoint : *entryPoints) + { + if (auto entryPointDecor = entryPoint->findDecoration()) + { + switch (entryPointDecor->getProfile().getStage()) + { + case Stage::Geometry: + case Stage::Intersection: + case Stage::Mesh: + case Stage::Amplification: + case Stage::AnyHit: + case Stage::ClosestHit: + case Stage::Hull: + case Stage::Domain: + needGeometryCapability = false; + break; + } + } + } + } + if (needGeometryCapability) + requireSPIRVCapability(SpvCapabilityGeometry); return getBuiltinGlobalVar(inst->getFullType(), SpvBuiltInPrimitiveId); } else if (semanticName == "sv_rendertargetarrayindex") -- cgit v1.2.3