Skip to content

Commit 9919c82

Browse files
author
Tim Foley
authored
Parameter blocks (shader-slang#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.
1 parent 296e89c commit 9919c82

29 files changed

+1828
-200
lines changed

slang.h

+3
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ extern "C"
564564
SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT,
565565
SLANG_PARAMETER_CATEGORY_SPECIALIZATION_CONSTANT,
566566
SLANG_PARAMETER_CATEGORY_PUSH_CONSTANT_BUFFER,
567+
SLANG_PAREMTER_CATEGORY_PARAMETER_BLOCK,
567568

568569
//
569570
SLANG_PARAMETER_CATEGORY_COUNT,
@@ -817,6 +818,7 @@ namespace slang
817818
DescriptorTableSlot = SLANG_PARAMETER_CATEGORY_DESCRIPTOR_TABLE_SLOT,
818819
SpecializationConstant = SLANG_PARAMETER_CATEGORY_SPECIALIZATION_CONSTANT,
819820
PushConstantBuffer = SLANG_PARAMETER_CATEGORY_PUSH_CONSTANT_BUFFER,
821+
ParameterBlock = SLANG_PAREMTER_CATEGORY_PARAMETER_BLOCK,
820822
};
821823

822824
struct TypeLayoutReflection
@@ -1084,6 +1086,7 @@ namespace slang
10841086
#include "source/slang/dxc-support.cpp"
10851087
#include "source/slang/emit.cpp"
10861088
#include "source/slang/ir.cpp"
1089+
#include "source/slang/ir-legalize-types.cpp"
10871090
#include "source/slang/lexer.cpp"
10881091
#include "source/slang/mangle.cpp"
10891092
#include "source/slang/name.cpp"

source/slang/bytecode.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -653,7 +653,7 @@ BytecodeGenerationPtr<char> tryGenerateNameForSymbol(
653653
if (auto highLevelDeclDecoration = inst->findDecoration<IRHighLevelDeclDecoration>())
654654
{
655655
auto decl = highLevelDeclDecoration->decl;
656-
if (auto reflectionNameMod = decl->FindModifier<ParameterBlockReflectionName>())
656+
if (auto reflectionNameMod = decl->FindModifier<ParameterGroupReflectionName>())
657657
{
658658
return allocateString(context, reflectionNameMod->name);
659659
}

source/slang/compiler.h

+4
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,10 @@ namespace Slang
433433
// Construct pointer types on-demand
434434
RefPtr<PtrType> getPtrType(RefPtr<Type> valueType);
435435

436+
RefPtr<ArrayExpressionType> getArrayType(
437+
Type* elementType,
438+
IntVal* elementCount);
439+
436440
RefPtr<GroupSharedType> getGroupSharedType(RefPtr<Type> valueType);
437441

438442
SyntaxClass<RefObject> findSyntaxClass(Name* name);

source/slang/core.meta.slang

+2
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ sb << "__generic<T>\n";
170170
sb << "__intrinsic_type(" << kIROp_TextureBufferType << ")\n";
171171
sb << "__magic_type(TextureBuffer) struct TextureBuffer {};\n";
172172

173+
sb << "__generic<T>\n";
174+
sb << "__magic_type(ParameterBlockType) struct ParameterBlock {};\n";
173175

174176
static const char* kComponentNames[]{ "x", "y", "z", "w" };
175177
static const char* kVectorNames[]{ "", "x", "xy", "xyz", "xyzw" };

source/slang/core.meta.slang.h

+2
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ sb << "__generic<T>\n";
173173
sb << "__intrinsic_type(" << kIROp_TextureBufferType << ")\n";
174174
sb << "__magic_type(TextureBuffer) struct TextureBuffer {};\n";
175175

176+
sb << "__generic<T>\n";
177+
sb << "__magic_type(ParameterBlockType) struct ParameterBlock {};\n";
176178

177179
static const char* kComponentNames[]{ "x", "y", "z", "w" };
178180
static const char* kVectorNames[]{ "", "x", "xy", "xyz", "xyzw" };

0 commit comments

Comments
 (0)