diff options
| -rw-r--r-- | source/slang/emit.cpp | 10 | ||||
| -rw-r--r-- | source/slang/lower.cpp | 33 | ||||
| -rw-r--r-- | source/slang/profile.h | 5 | ||||
| -rw-r--r-- | tests/cross-compile/gl-layer-pick-version.slang | 11 | ||||
| -rw-r--r-- | tests/cross-compile/gl-layer-pick-version.slang.glsl | 25 |
5 files changed, 80 insertions, 4 deletions
diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 8c6a46196..0eb371b88 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3604,6 +3604,13 @@ String emitEntryPoint( auto translationUnitSyntax = translationUnit->SyntaxNode.Ptr(); + // We perform lowering of the program before emitting *anything*, + // because the lowering process might change how we emit some + // boilerplate at the start of the ouput for GLSL (e.g., what + // version we require). + auto lowered = lowerEntryPoint(entryPoint, programLayout, target); + sharedContext.program = lowered.program; + // There may be global-scope modifiers that we should emit now visitor.emitGLSLPreprocessorDirectives(translationUnitSyntax); @@ -3624,9 +3631,6 @@ String emitEntryPoint( break; } - auto lowered = lowerEntryPoint(entryPoint, programLayout, target); - - sharedContext.program = lowered.program; visitor.EmitDeclsInContainer(lowered.program.Ptr()); diff --git a/source/slang/lower.cpp b/source/slang/lower.cpp index 856da9b6c..98f6d8273 100644 --- a/source/slang/lower.cpp +++ b/source/slang/lower.cpp @@ -221,7 +221,8 @@ public: struct SharedLoweringContext { - CompileRequest* compileRequest; + CompileRequest* compileRequest; + EntryPointRequest* entryPointRequest; ProgramLayout* programLayout; EntryPointLayout* entryPointLayout; @@ -2329,6 +2330,31 @@ struct LoweringVisitor return false; } + void requireGLSLVersion(ProfileVersion version) + { + if (shared->target != CodeGenTarget::GLSL) + return; + + auto entryPoint = shared->entryPointRequest; + auto profile = entryPoint->profile; + auto currentVersion = profile.GetVersion(); + if (profile.getFamily() == ProfileFamily::GLSL) + { + // Check if this profile is newer + if ((UInt)version > (UInt)profile.GetVersion()) + { + profile.setVersion(version); + entryPoint->profile = profile; + } + } + else + { + // Non-GLSL target? Set it to a GLSL one. + profile.setVersion(version); + entryPoint->profile = profile; + } + } + void lowerSimpleShaderParameterToGLSLGlobal( VaryingParameterInfo const& info, RefPtr<ExpressionType> varType, @@ -2460,6 +2486,10 @@ struct LoweringVisitor } else if (ns == "sv_rendertargetarrayindex") { + if (info.direction == VaryingParameterDirection::Input) + { + requireGLSLVersion(ProfileVersion::GLSL_430); + } globalVarExpr = createGLSLBuiltinRef("gl_Layer"); } else if (ns == "sv_sampleindex") @@ -3040,6 +3070,7 @@ LoweredEntryPoint lowerEntryPoint( { SharedLoweringContext sharedContext; sharedContext.compileRequest = entryPoint->compileRequest; + sharedContext.entryPointRequest = entryPoint; sharedContext.programLayout = programLayout; sharedContext.target = target; diff --git a/source/slang/profile.h b/source/slang/profile.h index 6d55d3195..3b5242ada 100644 --- a/source/slang/profile.h +++ b/source/slang/profile.h @@ -70,6 +70,11 @@ namespace Slang ProfileVersion GetVersion() const { return ProfileVersion(uint32_t(raw) & 0xFFFF); } ProfileFamily getFamily() const { return getProfileFamily(GetVersion()); } + void setVersion(ProfileVersion version) + { + raw = (raw & ~0xFFFF) | uint32_t(version); + } + static Profile LookUp(char const* name); RawVal raw = Unknown; diff --git a/tests/cross-compile/gl-layer-pick-version.slang b/tests/cross-compile/gl-layer-pick-version.slang new file mode 100644 index 000000000..c68d68427 --- /dev/null +++ b/tests/cross-compile/gl-layer-pick-version.slang @@ -0,0 +1,11 @@ +//TEST:CROSS_COMPILE: -profile ps_5_0 -entry main -target spirv-assembly + +struct VS_OUT +{ + nointerpolation uint layerID : SV_RenderTargetArrayIndex; +}; + +float4 main(VS_OUT vsOut) : SV_Target +{ + return float4(float(vsOut.layerID)); +} diff --git a/tests/cross-compile/gl-layer-pick-version.slang.glsl b/tests/cross-compile/gl-layer-pick-version.slang.glsl new file mode 100644 index 000000000..55d419405 --- /dev/null +++ b/tests/cross-compile/gl-layer-pick-version.slang.glsl @@ -0,0 +1,25 @@ +//TEST_IGNORE_FILE: +#version 430 + +struct VS_OUT +{ + uint layerID; +}; + +vec4 main_(VS_OUT vsOut) : SV_Target +{ + return vec4(float(vsOut.layerID)); +} + +out vec4 SLANG_out_main_result; + +void main() +{ + VS_OUT vsOut; + vsOut.layerID = gl_Layer; + + vec4 main_result; + main_result = main_(vsOut); + + SLANG_out_main_result = main_result; +}
\ No newline at end of file |
