From c1ae2f33b5bc22ec013c57c716b6ef438619edb0 Mon Sep 17 00:00:00 2001 From: Greg Fischer Date: Tue, 21 Jun 2022 17:33:57 -0600 Subject: [PATCH] Do not generate samplerBuffer for spirv1.6 and beyond This type was removed from spirv1.6. If samplerBuffer is specified in GLSL, generate textureBuffer. If samplerBuffer type is constructed, just return the buffer. Fixes #2956 --- SPIRV/GlslangToSpv.cpp | 20 +++++-- .../spv.1.6.samplerBuffer.frag.out | 43 +++++++++++++++ Test/baseResults/spv.1.6.separate.frag.out | 52 +++++++++++++++++++ Test/spv.1.6.samplerBuffer.frag | 11 ++++ Test/spv.1.6.separate.frag | 14 +++++ gtests/Spv.FromFile.cpp | 2 + 6 files changed, 137 insertions(+), 5 deletions(-) create mode 100644 Test/baseResults/spv.1.6.samplerBuffer.frag.out create mode 100644 Test/baseResults/spv.1.6.separate.frag.out create mode 100644 Test/spv.1.6.samplerBuffer.frag create mode 100644 Test/spv.1.6.separate.frag diff --git a/SPIRV/GlslangToSpv.cpp b/SPIRV/GlslangToSpv.cpp index 63547692b..3c869bc96 100644 --- a/SPIRV/GlslangToSpv.cpp +++ b/SPIRV/GlslangToSpv.cpp @@ -2936,9 +2936,17 @@ bool TGlslangToSpvTraverser::visitAggregate(glslang::TVisit visit, glslang::TInt std::vector arguments; translateArguments(*node, arguments, lvalueCoherentFlags); spv::Id constructed; - if (node->getOp() == glslang::EOpConstructTextureSampler) - constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); - else if (node->getOp() == glslang::EOpConstructStruct || + if (node->getOp() == glslang::EOpConstructTextureSampler) { + const glslang::TType& texType = node->getSequence()[0]->getAsTyped()->getType(); + if (glslangIntermediate->getSpv().spv >= glslang::EShTargetSpv_1_6 && + texType.getSampler().isBuffer()) { + // SamplerBuffer is not supported in spirv1.6 so + // `samplerBuffer(textureBuffer, sampler)` is a no-op + // and textureBuffer is the result going forward + constructed = arguments[0]; + } else + constructed = builder.createOp(spv::OpSampledImage, resultType(), arguments); + } else if (node->getOp() == glslang::EOpConstructStruct || node->getOp() == glslang::EOpConstructCooperativeMatrix || node->getType().isArray()) { std::vector constituents; @@ -4173,8 +4181,10 @@ spv::Id TGlslangToSpvTraverser::convertGlslangToSpvType(const glslang::TType& ty spvType = builder.makeImageType(getSampledType(sampler), TranslateDimensionality(sampler), sampler.isShadow(), sampler.isArrayed(), sampler.isMultiSample(), sampler.isImageClass() ? 2 : 1, TranslateImageFormat(type)); - if (sampler.isCombined()) { - // already has both image and sampler, make the combined type + if (sampler.isCombined() && + (!sampler.isBuffer() || glslangIntermediate->getSpv().spv < glslang::EShTargetSpv_1_6)) { + // Already has both image and sampler, make the combined type. Only combine sampler to + // buffer if before SPIR-V 1.6. spvType = builder.makeSampledImageType(spvType); } } diff --git a/Test/baseResults/spv.1.6.samplerBuffer.frag.out b/Test/baseResults/spv.1.6.samplerBuffer.frag.out new file mode 100644 index 000000000..8a0275f94 --- /dev/null +++ b/Test/baseResults/spv.1.6.samplerBuffer.frag.out @@ -0,0 +1,43 @@ +spv.1.6.samplerBuffer.frag +// Module Version 10600 +// Generated by (magic number): 8000a +// Id's are bound by 23 + + Capability Shader + Capability SampledBuffer + Capability ImageQuery + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint Fragment 4 "main" 9 13 + ExecutionMode 4 OriginUpperLeft + Source GLSL 140 + Name 4 "main" + Name 9 "o" + Name 13 "sampB" + Decorate 9(o) Location 0 + Decorate 13(sampB) DescriptorSet 0 + Decorate 13(sampB) Binding 0 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypeVector 6(float) 4 + 8: TypePointer Output 7(fvec4) + 9(o): 8(ptr) Variable Output + 10: TypeInt 32 1 + 11: TypeImage 10(int) Buffer sampled format:Unknown + 12: TypePointer UniformConstant 11 + 13(sampB): 12(ptr) Variable UniformConstant + 17: 6(float) Constant 1120403456 + 19: TypeInt 32 0 + 20: 19(int) Constant 3 + 21: TypePointer Output 6(float) + 4(main): 2 Function None 3 + 5: Label + 14: 11 Load 13(sampB) + 15: 10(int) ImageQuerySize 14 + 16: 6(float) ConvertSToF 15 + 18: 6(float) FDiv 16 17 + 22: 21(ptr) AccessChain 9(o) 20 + Store 22 18 + Return + FunctionEnd diff --git a/Test/baseResults/spv.1.6.separate.frag.out b/Test/baseResults/spv.1.6.separate.frag.out new file mode 100644 index 000000000..e15655e1c --- /dev/null +++ b/Test/baseResults/spv.1.6.separate.frag.out @@ -0,0 +1,52 @@ +spv.1.6.separate.frag +// Module Version 10600 +// Generated by (magic number): 8000a +// Id's are bound by 27 + + Capability Shader + Capability SampledBuffer + 1: ExtInstImport "GLSL.std.450" + MemoryModel Logical GLSL450 + EntryPoint Fragment 4 "main" 9 13 18 24 + ExecutionMode 4 OriginUpperLeft + Source GLSL 400 + Name 4 "main" + Name 9 "texBuffer" + Name 13 "s" + Name 18 "itexBuffer" + Name 24 "utexBuffer" + Decorate 9(texBuffer) DescriptorSet 0 + Decorate 9(texBuffer) Binding 1 + Decorate 13(s) DescriptorSet 0 + Decorate 13(s) Binding 0 + Decorate 18(itexBuffer) DescriptorSet 0 + Decorate 18(itexBuffer) Binding 2 + Decorate 24(utexBuffer) DescriptorSet 0 + Decorate 24(utexBuffer) Binding 3 + 2: TypeVoid + 3: TypeFunction 2 + 6: TypeFloat 32 + 7: TypeImage 6(float) Buffer sampled format:Unknown + 8: TypePointer UniformConstant 7 + 9(texBuffer): 8(ptr) Variable UniformConstant + 11: TypeSampler + 12: TypePointer UniformConstant 11 + 13(s): 12(ptr) Variable UniformConstant + 15: TypeInt 32 1 + 16: TypeImage 15(int) Buffer sampled format:Unknown + 17: TypePointer UniformConstant 16 + 18(itexBuffer): 17(ptr) Variable UniformConstant + 21: TypeInt 32 0 + 22: TypeImage 21(int) Buffer sampled format:Unknown + 23: TypePointer UniformConstant 22 + 24(utexBuffer): 23(ptr) Variable UniformConstant + 4(main): 2 Function None 3 + 5: Label + 10: 7 Load 9(texBuffer) + 14: 11 Load 13(s) + 19: 16 Load 18(itexBuffer) + 20: 11 Load 13(s) + 25: 22 Load 24(utexBuffer) + 26: 11 Load 13(s) + Return + FunctionEnd diff --git a/Test/spv.1.6.samplerBuffer.frag b/Test/spv.1.6.samplerBuffer.frag new file mode 100644 index 000000000..d12ff3df8 --- /dev/null +++ b/Test/spv.1.6.samplerBuffer.frag @@ -0,0 +1,11 @@ +#version 140 + +out vec4 o; + +uniform isamplerBuffer sampB; + +void main() +{ + o.w = float(textureSize(sampB)) / 100.0; +} + diff --git a/Test/spv.1.6.separate.frag b/Test/spv.1.6.separate.frag new file mode 100644 index 000000000..3e51be482 --- /dev/null +++ b/Test/spv.1.6.separate.frag @@ -0,0 +1,14 @@ +#version 400 + +uniform sampler s; + +uniform textureBuffer texBuffer; +uniform itextureBuffer itexBuffer; +uniform utextureBuffer utexBuffer; + +void main() +{ + samplerBuffer (texBuffer, s); + isamplerBuffer (itexBuffer, s); + usamplerBuffer (utexBuffer, s); +} diff --git a/gtests/Spv.FromFile.cpp b/gtests/Spv.FromFile.cpp index f8bed532c..300d6642f 100644 --- a/gtests/Spv.FromFile.cpp +++ b/gtests/Spv.FromFile.cpp @@ -639,6 +639,8 @@ INSTANTIATE_TEST_SUITE_P( "spv.1.6.conditionalDiscard.frag", "spv.1.6.helperInvocation.frag", "spv.1.6.specConstant.comp", + "spv.1.6.samplerBuffer.frag", + "spv.1.6.separate.frag", })), FileNameAsCustomTestSuffix );