Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SPIR-V] HLSL style register assignment for resource arrays #6932

Closed
2 changes: 2 additions & 0 deletions include/dxc/Support/HLSLOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, Group<spirv_Group>,
HelpText<"Specify the target environment: vulkan1.0 (default), vulkan1.1, vulkan1.1spirv1.4, vulkan1.2, vulkan1.3, or universal1.5">;
def fspv_flatten_resource_arrays: Flag<["-"], "fspv-flatten-resource-arrays">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
HelpText<"Flatten arrays of resources so each array element takes one binding number">;
def fspv_hlsl_resource_arrays: Flag<["-"], "fspv-hlsl-resource-arrays">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
HelpText<"Resource arrays use one binding per array element to match hlsl style">;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not an accurate description of what is happening. In Vulkan, an array of resources take just a single binding. In the test below, MyTextures[5] will still use a single binding. And then binding 1-4 are left unused.

Also the name of option is not very descriptive. What is an HLSL resource array? You should reference something in DirectX, you seem to want to try to match DX's binding model in someway. Even that I believe is very difficult because Vulkan's binding model is very different.

def fspv_reduce_load_size: Flag<["-"], "fspv-reduce-load-size">, Group<spirv_Group>, Flags<[CoreOption, DriverOption]>,
HelpText<"Replaces loads of composite objects to reduce memory pressure for the loads">;
def fspv_fix_func_call_arguments: Flag<["-"], "fspv-fix-func-call-arguments">, Group<spirv_Group>, Flags<[CoreOption, DriverOption, HelpHidden]>,
Expand Down
1 change: 1 addition & 0 deletions include/dxc/Support/SPIRVOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ struct SpirvCodeGenOptions {
bool useLegacyBufferMatrixOrder;
bool useScalarLayout;
bool flattenResourceArrays;
bool hlslResourceArrays;
bool reduceLoadSize;
bool autoShiftBindings;
bool supportNonzeroBaseInstance;
Expand Down
4 changes: 4 additions & 0 deletions lib/DxcSupport/HLSLOptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,8 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
Args.hasFlag(OPT_Wno_vk_emulated_features, OPT_INVALID, false);
opts.SpirvOptions.flattenResourceArrays =
Args.hasFlag(OPT_fspv_flatten_resource_arrays, OPT_INVALID, false);
opts.SpirvOptions.hlslResourceArrays =
Args.hasFlag(OPT_fspv_hlsl_resource_arrays, OPT_INVALID, false);
opts.SpirvOptions.reduceLoadSize =
Args.hasFlag(OPT_fspv_reduce_load_size, OPT_INVALID, false);
opts.SpirvOptions.fixFuncCallArguments =
Expand Down Expand Up @@ -1283,6 +1285,8 @@ int ReadDxcOpts(const OptTable *optionTable, unsigned flagsToInclude,
Args.hasFlag(OPT_fspv_use_legacy_buffer_matrix_order, OPT_INVALID,
false) ||
Args.hasFlag(OPT_fspv_flatten_resource_arrays, OPT_INVALID, false) ||
Args.hasFlag(OPT_fspv_implicit_hlsl_resource_arrays, OPT_INVALID,
false) ||
Args.hasFlag(OPT_fspv_reduce_load_size, OPT_INVALID, false) ||
Args.hasFlag(OPT_fspv_reflect, OPT_INVALID, false) ||
Args.hasFlag(OPT_fspv_fix_func_call_arguments, OPT_INVALID, false) ||
Expand Down
5 changes: 4 additions & 1 deletion tools/clang/lib/SPIRV/DeclResultIdMapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2617,8 +2617,11 @@ bool DeclResultIdMapper::decorateResourceBindings() {
// resources also gets only one binding number. However, for array of
// resources (e.g. array of textures), DX uses one binding number per array
// element. We can match this behavior via a command line option.
// hlslResourceArrays - One binding number per array element
// assignment
uint32_t numBindingsToUse = 1;
if (spirvOptions.flattenResourceArrays || needsFlatteningCompositeResources)
if (spirvOptions.flattenResourceArrays ||
needsFlatteningCompositeResources || spirvOptions.hlslResourceArrays)
numBindingsToUse = getNumBindingsUsedByResourceType(
var.getSpirvInstr()->getAstResultType());

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// RUN: %dxc -T ps_6_0 -E main -fspv-hlsl-resource-arrays -fcgl %s -spirv | FileCheck %s

// CHECK: OpDecorate %MyTextures Binding 0
// CHECK: OpDecorate %NextTexture Binding 5
// CHECK: OpDecorate %AnotherTexture Binding 6
// CHECK: OpDecorate %MySamplers Binding 7
Texture2D MyTextures[5];
Texture2D NextTexture;
Texture2D AnotherTexture;
SamplerState MySamplers[2];

float4 main(float2 TexCoord : TexCoord) : SV_Target0
{
float4 result =
MyTextures[0].Sample(MySamplers[0], TexCoord) +
MyTextures[1].Sample(MySamplers[0], TexCoord) +
MyTextures[2].Sample(MySamplers[0], TexCoord) +
MyTextures[3].Sample(MySamplers[1], TexCoord) +
MyTextures[4].Sample(MySamplers[1], TexCoord) +
AnotherTexture.Sample(MySamplers[1], TexCoord) +
NextTexture.Sample(MySamplers[1], TexCoord);
return result;
}
Loading