Skip to content

Commit b52f9ba

Browse files
authored
[SPIR-V] Don't flatten arrays during legalization (microsoft#6767)
When struct contains opaque resources, we must legalize the SPIR-V to move those resources out of the struct (Vulkan doesn't allow composite to store opaque resources). Before this change, any resource array would also be flattened due to the pass flattening everything by default. Patched SPIRV-Tools to allow some level of selection in what we flatten. Not perfect, as we would flatten all arrays or all composites and cannot pick only one variable to flatten, but for now this should be enough. Fixes microsoft#6745 Signed-off-by: Nathan Gauër <[email protected]> --------- Signed-off-by: Nathan Gauër <[email protected]>
1 parent 8652894 commit b52f9ba

File tree

5 files changed

+148
-91
lines changed

5 files changed

+148
-91
lines changed

external/SPIRV-Tools

Submodule SPIRV-Tools updated 72 files

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14805,13 +14805,19 @@ bool SpirvEmitter::spirvToolsLegalize(std::vector<uint32_t> *mod,
1480514805
}
1480614806
optimizer.RegisterLegalizationPasses(spirvOptions.preserveInterface);
1480714807
// Add flattening of resources if needed.
14808-
if (spirvOptions.flattenResourceArrays ||
14809-
declIdMapper.requiresFlatteningCompositeResources()) {
14808+
if (spirvOptions.flattenResourceArrays) {
1481014809
optimizer.RegisterPass(
1481114810
spvtools::CreateReplaceDescArrayAccessUsingVarIndexPass());
1481214811
optimizer.RegisterPass(
1481314812
spvtools::CreateAggressiveDCEPass(spirvOptions.preserveInterface));
14814-
optimizer.RegisterPass(spvtools::CreateDescriptorScalarReplacementPass());
14813+
optimizer.RegisterPass(
14814+
spvtools::CreateDescriptorArrayScalarReplacementPass());
14815+
optimizer.RegisterPass(
14816+
spvtools::CreateAggressiveDCEPass(spirvOptions.preserveInterface));
14817+
}
14818+
if (declIdMapper.requiresFlatteningCompositeResources()) {
14819+
optimizer.RegisterPass(
14820+
spvtools::CreateDescriptorCompositeScalarReplacementPass());
1481514821
// ADCE should be run after desc_sroa in order to remove potentially
1481614822
// illegal types such as structures containing opaque types.
1481714823
optimizer.RegisterPass(
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// RUN: %dxc -T ps_6_6 -E main -spirv %s | FileCheck %s
2+
3+
// CHECK-DAG: OpName %Textures "Textures"
4+
// CHECK-DAG: OpDecorate %Textures DescriptorSet 0
5+
// CHECK-DAG: OpDecorate %Textures Binding 0
6+
Texture2D<float4> Textures[10];
7+
8+
struct StructOfResources {
9+
Texture2D Texture;
10+
SamplerState Sampler;
11+
};
12+
13+
// CHECK-DAG: OpName %TheStruct_Texture "TheStruct.Texture"
14+
// CHECK-DAG: OpDecorate %TheStruct_Texture DescriptorSet 0
15+
// CHECK-DAG: OpDecorate %TheStruct_Texture Binding 10
16+
17+
// CHECK-DAG: OpName %TheStruct_Sampler "TheStruct.Sampler"
18+
// CHECK-DAG: OpDecorate %TheStruct_Sampler DescriptorSet 0
19+
// CHECK-DAG: OpDecorate %TheStruct_Sampler Binding 11
20+
StructOfResources TheStruct;
21+
22+
float4 main() : SV_Target
23+
{
24+
// CHECK: [[ptr:%[0-9]+]] = OpAccessChain %_ptr_UniformConstant_type_2d_image %Textures %int_0
25+
// CHECK: [[tex:%[0-9]+]] = OpLoad %type_2d_image [[ptr]]
26+
// CHECK: [[smp:%[0-9]+]] = OpLoad %type_sampler %TheStruct_Sampler
27+
// CHECK: [[x:%[0-9]+]] = OpSampledImage %type_sampled_image [[tex]] [[smp]]
28+
return Textures[0].Sample(TheStruct.Sampler, float2(0, 0))
29+
// CHECK: [[tex:%[0-9]+]] = OpLoad %type_2d_image %TheStruct_Texture
30+
// CHECK: [[x:%[0-9]+]] = OpSampledImage %type_sampled_image [[tex]] [[smp]]
31+
+ TheStruct.Texture.Sample(TheStruct.Sampler, float2(0, 0));
32+
}

0 commit comments

Comments
 (0)