Skip to content

Commit

Permalink
[SPIR-V] Allow SamplerComparisonState in combinedImageSampler (#6338)
Browse files Browse the repository at this point in the history
Previously, only `SamplerState` was allowed to be used as the sampler
type with `[[vk::combinedImageSampler]]`, but I don't see any reason
that `SamplerComparisonState` shouldn't also be permitted. Since
`SamplerComparisonState` is a sampler state plus a comparison state,
`SampleCmp` calls are translated to an `OpImageSampleDrefImplicitLod`
(sampling with depth-comparison) in Vulkan SPIR-V, which does accept an
`OpTypeSampledImage` (combined image sampler) as an operand.

Fixes #4696
  • Loading branch information
sudonatalie authored Feb 22, 2024
1 parent 5e658ac commit d9266c7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 2 additions & 3 deletions tools/clang/include/clang/Basic/Attr.td
Original file line number Diff line number Diff line change
Expand Up @@ -1258,14 +1258,13 @@ def ArrayOfBuffer
S->getType()->getAsArrayTypeUnsafe()->getElementType()->getAs<RecordType>()->getDecl()->getName() ==
"RasterizerOrderedBuffer")}]>;

// Global variable with "Texture" or "SamplerState" type
// Global variable with "Texture*" or "Sampler*" type
def TextureOrSampler
: SubsetSubject<
Var, [{S->hasGlobalStorage() && S->getType()->getAs<RecordType>() &&
S->getType()->getAs<RecordType>()->getDecl() &&
(S->getType()->getAs<RecordType>()->getDecl()->getName().startswith("Texture") ||
S->getType()->getAs<RecordType>()->getDecl()->getName() ==
"SamplerState")}]>;
S->getType()->getAs<RecordType>()->getDecl()->getName().startswith("Sampler"))}]>;

def VKBuiltIn : InheritableAttr {
let Spellings = [CXX11<"vk", "builtin">];
Expand Down
2 changes: 1 addition & 1 deletion tools/clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -2349,7 +2349,7 @@ def warn_attribute_wrong_decl_type : Warning<
"global variables of scalar type|"
"global variables of struct type|"
"global variables, cbuffers, and tbuffers|"
"Textures (e.g., Texture2D) and SamplerState|"
"Textures and Samplers|"
"RWTextures, RasterizerOrderedTextures, Buffers, RWBuffers, and RasterizerOrderedBuffers|"
"RWStructuredBuffers, AppendStructuredBuffers, and ConsumeStructuredBuffers|"
"SubpassInput, SubpassInputMS|"
Expand Down
11 changes: 11 additions & 0 deletions tools/clang/test/CodeGenSPIRV/vk.combined-image-sampler.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ Texture2D<float4> tex2 : register(t2);
// CHECK: OpDecorate %sam3 Binding 0
SamplerState sam3 : register(s0);

// CHECK: OpDecorate %tex4 DescriptorSet 0
// CHECK: OpDecorate %tex4 Binding 4
[[vk::combinedImageSampler]]
Texture2D tex4 : register(t4);
[[vk::combinedImageSampler]]
SamplerComparisonState sam4 : register(s4);

Texture2D<float4> getTexture(int i) {
if (i == 0) return tex0;
if (i == 1) return tex1;
Expand Down Expand Up @@ -59,6 +66,10 @@ float4 main(int3 offset: A) : SV_Target {
// CHECK: OpImageSampleExplicitLod %v4float [[tex2]]
ret += sampleLevel(2);

// CHECK: [[tex4:%[a-zA-Z0-9_]+]] = OpLoad %type_sampled_image %tex4
// CHECK: OpImageSampleDrefImplicitLod %float [[tex4]]
ret += tex4.SampleCmp(sam4, float2(1, 2), 10, 2);

// CHECK: [[tex0_0:%[a-zA-Z0-9_]+]] = OpLoad %type_sampled_image %tex0
// CHECK: [[img_extracted_from_tex0:%[a-zA-Z0-9_]+]] = OpImage %type_2d_image [[tex0_0]]
// CHECK: [[sam3:%[a-zA-Z0-9_]+]] = OpLoad %type_sampler %sam3
Expand Down

0 comments on commit d9266c7

Please sign in to comment.