diff options
| author | Tim Foley <tfoleyNV@users.noreply.github.com> | 2017-11-06 10:37:27 -0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2017-11-06 10:37:27 -0800 |
| commit | 9919c823938ae929b16efac9d507f6d5eb122bf4 (patch) | |
| tree | 8fc65791d416cffa8267180177d3f3d179679460 /source/slang/parameter-binding.cpp | |
| parent | 296e89ca4f3d6d99126bf2ee59666bc946add431 (diff) | |
Parameter blocks (#245)
* Rename existing ParameterBlock to ParameterGroup
We are planning to add a new `ParameterBlock<T>` type, which maps to the notion of a "parameter block" as used in the Spire research work.
Unfortunately, the compiler codebase already uses the term `ParameterBlock` as catch-all to encompass all of HLSL `cbuffer`/`tbuffer` and GLSL `uniform`/`buffer`/`in`/`out` blocks (all of which are lexical `{}`-enclosed blocks that define parameters...).
This change instead renames all of the existing concepts over to `ParameterGroup`, which isn't an ideal name, but at least doesn't directly overlap the new terminology or any existing terminology.
The new `ParameterBlockType` case will probably be a subclass of `ParameterGroupType`, since it is a logical extension of the underlying concept.
* Add Shader Model 5.1 profiles
The HLSL `register(..., space0)` syntax is only allowed on "SM5.1" and later profiles (which is supported by the newer version of `d3dcompiler_47.dll` that comes with the Win10 SDK, but not the older version of `d3dcompiler_47.dll` - good luck figuring out which you have!).
This change adds those profiles to our master list of profiles, and nothing else.
* First pass at support for `ParameterBlock<T>`
- Add the type declaration in stdlib
- Add a special case of `ParameterGroupType` for parameter blocks
- Handle parameter blocks in type layout (currently handling them identically to constant buffers for now, which isn't going to be right in the long term)
- Add an IR pass that basically replaces `ParameterBlock<T>` with `T`
- Eventually this should replace it with either `T` or `ConstantBuffer<T>`, depending on whether the layout that was computed required a constant buffer to hold any "free" uniforms
- Add first stab at an IR pass to "scalarize" global variables using aggregate types with resources inside.
- This currently only applies to global variables, so it won't handle things passed through functions, or used as local variables
- It also only supports cases where the references to the original variable are always references to its fields, and not the whole value itself
- Add a single test case that technically passes with this level of support, but probably isn't very representative of what we need from the feature
* Fold parameter-block desugaring into a more complete "type legalization" pass
The basic problem that was arising is that once you desugar `ParameterBlock<T>` into `T`, you then need todeal with splitting `T` into its constituent fields if it contains any resource types.
Handling those transformations by following the usual use-def chains wasn't really helping, because you might need systematic rewriting that can really only be handled bottom-up.
This change adds a new pass that is intended to perform multiple kinds of type "legalization" at once:
- It will turn `ParameterBlock<T>` into `T`
- It may at some point also convert `ConstantBuffer<T>` into `T` as well
- It will turn an value of an aggregate type that contains resources into N different values (one per field)
- As a result of this, it will also deal with AOS-to-SOA conversion of these types
Legalization is applied to *every* function/instruction/value, so that it can make large-scale changes that would be tough to manage with a work list.
This pass needs to be run *after* generics have been fully specialized, so that we know we are always dealing with fully concrete types, so that their legalization for a given target is completely known.
This is still work in progress; there's more to be done to get this working with all our test cases, and finish the remaining `ParameterBlock<T>` work.
* Improve binding/layout information when using parameter blocks
- When doing type layout for a parameter block, don't include the resources consumed by the element type in the resource usage for the parameter block
- Note that this is pretty much identical to how a `ConstantBuffer<T>` does not report any `LayoutResourceKind::Uniform` usage, except that `ParameterBlock<T>` is *also* going to hide underlying texture/sampler reigster usage
- The one exception here is that any nested items that use up entire `space`s or `set`s those need to be exposed in the resource usage of the parent (I don't have a test for this)
- When type legalization needs to scalarize things, it must propagate layout information down to the new leaf variables. In general, the register/index for a new leaf parameter should be the sum of the offsets for all of the parent variables along the "chain" from the original variable down to the leaf (we aren't dealing with arrays here just yet).
- When type legalization decides to eliminate a pointer(-like) type (e.g., desugar `ParameterBlock<T>` over to `T`), actually deal with that in terms of the `LegalVal`s created, so that we can know to turn a `load` into a no-op when applied to a value that got indirection removed.
- Hack up the "complex" parameter-block test so that it actually passes (the big hack here is that the HLSL baseline is using names that are generated by the IR, and are unlikely to be stable as we add/remove transformations).
- Note: I can't make these be compute tests right now, because regsiter spaces/sets are a feature of D3D12/Vulkan, and our test runner isn't using those APIs.
Diffstat (limited to 'source/slang/parameter-binding.cpp')
| -rw-r--r-- | source/slang/parameter-binding.cpp | 56 |
1 files changed, 51 insertions, 5 deletions
diff --git a/source/slang/parameter-binding.cpp b/source/slang/parameter-binding.cpp index 18294bb3e..05b9d924e 100644 --- a/source/slang/parameter-binding.cpp +++ b/source/slang/parameter-binding.cpp @@ -205,6 +205,9 @@ struct SharedParameterBindingContext // This is only used for varying input/output. // Dictionary<TranslationUnitRequest*, RefPtr<UsedRangeSet>> translationUnitUsedRangeSets; + + // Which register spaces have been claimed so far? + UsedRanges usedSpaces; }; static DiagnosticSink* getSink(SharedParameterBindingContext* shared) @@ -379,7 +382,7 @@ static bool findLayoutArg( static Name* getReflectionName(VarDeclBase* varDecl) { - if (auto reflectionNameModifier = varDecl->FindModifier<ParameterBlockReflectionName>()) + if (auto reflectionNameModifier = varDecl->FindModifier<ParameterGroupReflectionName>()) return reflectionNameModifier->nameAndLoc.name; return varDecl->getName(); @@ -398,7 +401,7 @@ RefPtr<Type> tryGetEffectiveTypeForGLSLVaryingInput( return nullptr; auto type = varDecl->getType(); - if( varDecl->HasModifier<InModifier>() || type->As<GLSLInputParameterBlockType>()) + if( varDecl->HasModifier<InModifier>() || type->As<GLSLInputParameterGroupType>()) { // Special case to handle "arrayed" shader inputs, as used // for Geometry and Hull input @@ -436,7 +439,7 @@ RefPtr<Type> tryGetEffectiveTypeForGLSLVaryingOutput( return nullptr; auto type = varDecl->getType(); - if( varDecl->HasModifier<OutModifier>() || type->As<GLSLOutputParameterBlockType>()) + if( varDecl->HasModifier<OutModifier>() || type->As<GLSLOutputParameterGroupType>()) { // Special case to handle "arrayed" shader outputs, as used // for Hull Shader output @@ -716,6 +719,24 @@ static RefPtr<UsedRangeSet> findUsedRangeSetForSpace( return usedRangeSet; } +// Record that a particular register space (or set, in the GLSL case) +// has been used in at least one binding, and so it should not +// be used by auto-generated bindings that need to claim entire +// spaces. +static void markSpaceUsed( + ParameterBindingContext* context, + UInt space) +{ + context->shared->usedSpaces.Add(nullptr, space, space+1); +} + +static UInt allocateUnusedSpaces( + ParameterBindingContext* context, + UInt count) +{ + return context->shared->usedSpaces.Allocate(nullptr, count); +} + static RefPtr<UsedRangeSet> findUsedRangeSetForTranslationUnit( ParameterBindingContext* context, TranslationUnitRequest* translationUnit) @@ -773,6 +794,12 @@ static void addExplicitParameterBinding( if (!usedRangeSet) { usedRangeSet = findUsedRangeSetForSpace(context, semanticInfo.space); + + // Record that the particular binding space was + // used by an explicit binding, so that we don't + // claim it for auto-generated bindings that + // need to grab a full space + markSpaceUsed(context, semanticInfo.space); } auto overlappedParameterInfo = usedRangeSet->usedResourceRanges[(int)semanticInfo.kind].Add( parameterInfo, @@ -950,6 +977,26 @@ static void completeBindingsForParameter( continue; } + auto count = typeRes.count; + + // We need to special-case the scenario where + // a parameter wants to claim an entire register + // space to itself (for a parameter block), since + // that can't be handled like other resources. + if (kind == LayoutResourceKind::ParameterBlock) + { + // We need to snag a register space of our own. + + UInt space = allocateUnusedSpaces(context, count); + + bindingInfo.count = count; + bindingInfo.index = space; + bindingInfo.space = 0; + + continue; + } + + // For now we only auto-generate bindings in space zero // // TODO: we may want to support searching for a space with @@ -970,7 +1017,6 @@ static void completeBindingsForParameter( break; } - auto count = typeRes.count; bindingInfo.count = count; bindingInfo.index = usedRangeSet->usedResourceRanges[(int)kind].Allocate(parameterInfo, (int) count); @@ -1699,7 +1745,7 @@ void generateParameterBindings( // up a global constant buffer type layout to hold them if( anyGlobalUniforms ) { - auto globalConstantBufferLayout = createParameterBlockTypeLayout( + auto globalConstantBufferLayout = createParameterGroupTypeLayout( nullptr, globalScopeRules, globalScopeRules->GetObjectLayout(ShaderParameterKind::ConstantBuffer), |
