diff --git a/source/slang/emit.cpp b/source/slang/emit.cpp index 61e473c7d5..5a59674f53 100644 --- a/source/slang/emit.cpp +++ b/source/slang/emit.cpp @@ -3848,7 +3848,10 @@ String emitEntryPoint( if (sharedContext.needHackSamplerForTexelFetch) { - finalResultBuilder << "layout(set = 0, binding = 0) uniform sampler SLANG_hack_samplerForTexelFetch;\n"; + finalResultBuilder + << "layout(set = 0, binding = " + << programLayout->bindingForHackSampler + << ") uniform sampler SLANG_hack_samplerForTexelFetch;\n"; } finalResultBuilder << code; diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp index 2f2bbec059..6128261c7d 100644 --- a/source/slang/parameter-binding.cpp +++ b/source/slang/parameter-binding.cpp @@ -1501,6 +1501,31 @@ static void collectParameters( } } +static bool isGLSLCrossCompilerNeeded(CompileRequest* request) +{ + switch (request->Target) + { + default: + return false; + + case CodeGenTarget::GLSL: + case CodeGenTarget::SPIRV: + case CodeGenTarget::SPIRVAssembly: + break; + } + + if (request->loadedModulesList.Count() != 0) + return true; + + for (auto tu : request->translationUnits) + { + if (tu->sourceLanguage != SourceLanguage::GLSL) + return true; + } + + return false; +} + void generateParameterBindings( CompileRequest* request) { @@ -1649,6 +1674,39 @@ void generateParameterBindings( globalScopeLayout = globalConstantBufferLayout; } + // Final final step: pick a binding for the "hack sampler", if needed... + // + // We only want to do this if the GLSL cross-compilation support is + // being invoked, so that we don't gum up other shaders. + if(isGLSLCrossCompilerNeeded(request)) + { + UInt space = 0; + auto hackSamplerUsedRanges = findUsedRangeSetForSpace(&context, space); + + UInt binding = hackSamplerUsedRanges->usedResourceRanges[(int)LayoutResourceKind::DescriptorTableSlot].Allocate(nullptr, 1); + + programLayout->bindingForHackSampler = (int)binding; + + RefPtr var = new Variable(); + var->Name.Content = "SLANG_hack_samplerForTexelFetch"; + var->Type.type = new SamplerStateType(); + + auto typeLayout = new TypeLayout(); + typeLayout->type = var->Type.type; + typeLayout->addResourceUsage(LayoutResourceKind::DescriptorTableSlot, 1); + + auto varLayout = new VarLayout(); + varLayout->varDecl = makeDeclRef(var.Ptr()); + varLayout->typeLayout = typeLayout; + auto resInfo = varLayout->AddResourceInfo(LayoutResourceKind::DescriptorTableSlot); + resInfo->index = binding; + resInfo->space = space; + + programLayout->hackSamplerVar = var; + + globalScopeStructLayout->fields.Add(varLayout); + } + // We now have a bunch of layout information, which we should // record into a suitable object that represents the program programLayout->globalScopeLayout = globalScopeLayout; diff --git a/source/slang/type-layout.h b/source/slang/type-layout.h index 81ca1350cd..7be1c595fb 100644 --- a/source/slang/type-layout.h +++ b/source/slang/type-layout.h @@ -383,6 +383,11 @@ class ProgramLayout : public Layout // and any entry-point-specific parameter data // will (eventually) belong there... List> entryPoints; + + // HACK: binding to use when we have to create + // a dummy sampler just to appease glslang + int bindingForHackSampler = 0; + RefPtr hackSamplerVar; }; struct LayoutRulesFamilyImpl; diff --git a/tests/reflection/cross-compile.slang.expected b/tests/reflection/cross-compile.slang.expected index 3bb730f664..3a8bea41ee 100644 --- a/tests/reflection/cross-compile.slang.expected +++ b/tests/reflection/cross-compile.slang.expected @@ -42,6 +42,13 @@ standard output = { ] } } + }, + { + "name": "SLANG_hack_samplerForTexelFetch", + "binding": {"kind": "descriptorTableSlot", "index": 3}, + "type": { + "kind": "samplerState" + } } ] }