diff --git a/include/dxc/Support/HLSLOptions.td b/include/dxc/Support/HLSLOptions.td index b7bf232070..e0e536bf80 100644 --- a/include/dxc/Support/HLSLOptions.td +++ b/include/dxc/Support/HLSLOptions.td @@ -390,6 +390,8 @@ def fspv_target_env_EQ : Joined<["-"], "fspv-target-env=">, 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, 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, Flags<[CoreOption, DriverOption]>, + HelpText<"Resource arrays use one binding per array element to match hlsl style">; def fspv_reduce_load_size: Flag<["-"], "fspv-reduce-load-size">, 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, Flags<[CoreOption, DriverOption, HelpHidden]>, diff --git a/include/dxc/Support/SPIRVOptions.h b/include/dxc/Support/SPIRVOptions.h index 1b88ef4def..5dffb0d2a6 100644 --- a/include/dxc/Support/SPIRVOptions.h +++ b/include/dxc/Support/SPIRVOptions.h @@ -64,6 +64,7 @@ struct SpirvCodeGenOptions { bool useLegacyBufferMatrixOrder; bool useScalarLayout; bool flattenResourceArrays; + bool hlslResourceArrays; bool reduceLoadSize; bool autoShiftBindings; bool supportNonzeroBaseInstance; diff --git a/lib/DxcSupport/HLSLOptions.cpp b/lib/DxcSupport/HLSLOptions.cpp index 4fa74d6759..cf77fcdacb 100644 --- a/lib/DxcSupport/HLSLOptions.cpp +++ b/lib/DxcSupport/HLSLOptions.cpp @@ -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 = @@ -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) || diff --git a/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp b/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp index 3084e27a87..b5b4c14540 100644 --- a/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp +++ b/tools/clang/lib/SPIRV/DeclResultIdMapper.cpp @@ -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()); diff --git a/tools/clang/test/CodeGenSPIRV/vk.binding.cl.hlsl-arrays.example1.hlsl b/tools/clang/test/CodeGenSPIRV/vk.binding.cl.hlsl-arrays.example1.hlsl new file mode 100644 index 0000000000..d3216ec9a6 --- /dev/null +++ b/tools/clang/test/CodeGenSPIRV/vk.binding.cl.hlsl-arrays.example1.hlsl @@ -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; +}