Skip to content

Commit

Permalink
Make the "hack" sampler explicit for now
Browse files Browse the repository at this point in the history
- We use this to work around the fact that, e.g., `Texture2D.Load` doesn't take a sampler, but the equivalent GLSL operation `texelFetch` requires one

- Previously we tried to hide the sampler from the user, hoping that glslang would drop it and we could just ignore it, but that doesn't work

- For now we'll go ahead and explicitly show the sampler in the reflection info so that an app can react appropriately

- We also generate a unique binding for the sampler, instead of the old behavior that fixed it with `binding = 0`
  - We still fix it with `set = 0`, so it might still surprise users
  • Loading branch information
tangent-vector committed Jul 22, 2017
1 parent 30fbf64 commit edf8caf
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/slang/emit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
58 changes: 58 additions & 0 deletions source/slang/parameter-binding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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<Variable> 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;
Expand Down
5 changes: 5 additions & 0 deletions source/slang/type-layout.h
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,11 @@ class ProgramLayout : public Layout
// and any entry-point-specific parameter data
// will (eventually) belong there...
List<RefPtr<EntryPointLayout>> entryPoints;

// HACK: binding to use when we have to create
// a dummy sampler just to appease glslang
int bindingForHackSampler = 0;
RefPtr<VarDeclBase> hackSamplerVar;
};

struct LayoutRulesFamilyImpl;
Expand Down
7 changes: 7 additions & 0 deletions tests/reflection/cross-compile.slang.expected
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ standard output = {
]
}
}
},
{
"name": "SLANG_hack_samplerForTexelFetch",
"binding": {"kind": "descriptorTableSlot", "index": 3},
"type": {
"kind": "samplerState"
}
}
]
}
Expand Down

0 comments on commit edf8caf

Please sign in to comment.