From 56771d655d77395e4fe879cc19771d461aa01190 Mon Sep 17 00:00:00 2001 From: Tim Foley Date: Mon, 10 Feb 2020 13:09:37 -0800 Subject: Fix output GLSL for primitive ID in a geometry shader (#1214) We had been translating an `SV_PrimitiveID` input in a shader over to `gl_PrimitiveID` in GLSL. That translation seemed to work just fine for users, so we thought it was correct. It turns out that `gl_PrimitiveID` is the correct GLSL for a primitive ID input in every stage *except* a geometry shader. In a geometry shader, `gl_PrimitiveID` is a primitive ID *output*, and if you want the input case you have to write `gl_PrimitiveIDIn` (note the `In` suffix). This change sets aside my bewilderment at the above long enough to implement a workaround in the GLSL legalization step. I also modified our current geometry shader cross compilation test to make use of an input primitive ID. --- source/slang/slang-ir-glsl-legalize.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'source') diff --git a/source/slang/slang-ir-glsl-legalize.cpp b/source/slang/slang-ir-glsl-legalize.cpp index 263e56f55..2c889f341 100644 --- a/source/slang/slang-ir-glsl-legalize.cpp +++ b/source/slang/slang-ir-glsl-legalize.cpp @@ -414,9 +414,36 @@ GLSLSystemValueInfo* getGLSLSystemValueInfo( { // uint in hlsl, int in glsl // https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/gl_PrimitiveID.xhtml - name = "gl_PrimitiveID"; - requiredType = builder->getBasicType(BaseType::Int); + + switch( context->getStage() ) + { + default: + name = "gl_PrimitiveID"; + break; + + case Stage::Geometry: + // GLSL makes a confusing design choice here. + // + // All the non-GS stages use `gl_PrimitiveID` to access + // the *input* primitive ID, but a GS uses `gl_PrimitiveID` + // to acces an *output* primitive ID (that will be passed + // along to the fragment shader). + // + // For a GS to get an input primitive ID (the thing that + // other stages access with `gl_PrimitiveID`), the + // programmer must write `gl_PrimitiveIDIn`. + // + if( kind == LayoutResourceKind::VaryingInput ) + { + name = "gl_PrimitiveIDIn"; + } + else + { + name = "gl_PrimitiveID"; + } + break; + } } else if (semanticName == "sv_rendertargetarrayindex") { -- cgit v1.2.3