Skip to content

Commit 19ba28b

Browse files
authored
[SPIR-V][vk::SampledTexture] #7. Add subscriptor indexing methods for vk::SampledTexture2D type. (microsoft#8158)
Part of microsoft#7979 Support subscriptor indexing `[]` and `.mips[]` for `SampledTexture2D`. `.sample[]` will not be allowed, as in `Texture2D`.
1 parent d4b2d04 commit 19ba28b

5 files changed

Lines changed: 54 additions & 6 deletions

File tree

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6595,7 +6595,8 @@ SpirvEmitter::doCXXOperatorCallExpr(const CXXOperatorCallExpr *expr,
65956595

65966596
// For Textures, regular indexing (operator[]) uses slice 0.
65976597
if (isBufferTextureIndexing(expr, &baseExpr, &indexExpr)) {
6598-
auto *lod = isTexture(baseExpr->getType())
6598+
auto *lod = (isTexture(baseExpr->getType()) ||
6599+
isSampledTexture(baseExpr->getType()))
65996600
? spvBuilder.getConstantInt(astContext.UnsignedIntTy,
66006601
llvm::APInt(32, 0))
66016602
: nullptr;
@@ -7894,7 +7895,7 @@ bool SpirvEmitter::isTextureMipsSampleIndexing(const CXXOperatorCallExpr *expr,
78947895

78957896
const Expr *object = memberExpr->getBase();
78967897
const auto objectType = object->getType();
7897-
if (!isTexture(objectType))
7898+
if (!isTexture(objectType) && !isSampledTexture(objectType))
78987899
return false;
78997900

79007901
if (base)
@@ -7918,7 +7919,7 @@ bool SpirvEmitter::isBufferTextureIndexing(const CXXOperatorCallExpr *indexExpr,
79187919
const Expr *object = indexExpr->getArg(0);
79197920
const auto objectType = object->getType();
79207921
if (isBuffer(objectType) || isRWBuffer(objectType) || isTexture(objectType) ||
7921-
isRWTexture(objectType)) {
7922+
isRWTexture(objectType) || isSampledTexture(objectType)) {
79227923
if (base)
79237924
*base = object;
79247925
if (index)

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1661,7 +1661,7 @@ static const SubscriptOperatorRecord g_ArBasicKindsSubscripts[] = {
16611661
{0, MipsFalse, SampleFalse}, // AR_OBJECT_VK_SPV_INTRINSIC_TYPE
16621662
{0, MipsFalse, SampleFalse}, // AR_OBJECT_VK_SPV_INTRINSIC_RESULT_ID
16631663
{0, MipsFalse, SampleFalse}, // AR_OBJECT_VK_BUFFER_POINTER
1664-
{0, MipsFalse, SampleFalse}, // AR_OBJECT_VK_SAMPLED_TEXTURE2D
1664+
{2, MipsTrue, SampleFalse}, // AR_OBJECT_VK_SAMPLED_TEXTURE2D
16651665
#endif // ENABLE_SPIRV_CODEGEN
16661666
// SPIRV change ends
16671667

@@ -4101,6 +4101,8 @@ class HLSLExternalSource : public ExternalSemaSource {
41014101
LookupVectorType(HLSLScalarType::HLSLScalarType_float, 4);
41024102
recordDecl = DeclareVkSampledTextureType(
41034103
*m_context, m_vkNSDecl, "SampledTexture2D", float4Type);
4104+
if (Attr)
4105+
recordDecl->addAttr(Attr);
41044106
m_vkSampledTextureTemplateDecl =
41054107
recordDecl->getDescribedClassTemplate();
41064108
}
@@ -5046,6 +5048,7 @@ class HLSLExternalSource : public ExternalSemaSource {
50465048
ResClass = DXIL::ResourceClass::UAV;
50475049
return true;
50485050
case AR_OBJECT_TEXTURE2D:
5051+
case AR_OBJECT_VK_SAMPLED_TEXTURE2D:
50495052
ResKind = DXIL::ResourceKind::Texture2D;
50505053
ResClass = DXIL::ResourceClass::SRV;
50515054
return true;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %dxc -T ps_6_7 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: [[cu12:%[0-9]+]] = OpConstantComposite %v2uint %uint_1 %uint_2
4+
5+
// CHECK: [[type_2d_image_1:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 0 0 1 Unknown
6+
// CHECK: [[type_sampled_image_1:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_1]]
7+
// CHECK: [[ptr_type_1:%[a-zA-Z0-9_]+]] = OpTypePointer UniformConstant [[type_sampled_image_1]]
8+
9+
// CHECK: [[tex1:%[a-zA-Z0-9_]+]] = OpVariable [[ptr_type_1]] UniformConstant
10+
11+
vk::SampledTexture2D<float4> tex1 : register(t1);
12+
13+
void main() {
14+
// CHECK: OpStore %pos1 [[cu12]]
15+
// CHECK-NEXT: [[pos1:%[0-9]+]] = OpLoad %v2uint %pos1
16+
// CHECK-NEXT: [[tex1_load:%[a-zA-Z0-9_]+]] = OpLoad [[type_sampled_image_1]] [[tex1]]
17+
// CHECK-NEXT: [[tex_img:%[0-9]+]] = OpImage [[type_2d_image_1]] [[tex1_load]]
18+
// CHECK-NEXT: [[result1:%[0-9]+]] = OpImageFetch %v4float [[tex_img]] [[pos1]] Lod %uint_0
19+
// CHECK-NEXT: OpStore %a1 [[result1]]
20+
uint2 pos1 = uint2(1,2);
21+
float4 a1 = tex1[pos1];
22+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %dxc -T ps_6_7 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: [[cu12:%[0-9]+]] = OpConstantComposite %v2uint %uint_1 %uint_2
4+
5+
// CHECK: [[type_2d_image_1:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 0 0 1 Unknown
6+
// CHECK: [[type_sampled_image_1:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_1]]
7+
// CHECK: [[ptr_type_1:%[a-zA-Z0-9_]+]] = OpTypePointer UniformConstant [[type_sampled_image_1]]
8+
9+
// CHECK: [[tex1:%[a-zA-Z0-9_]+]] = OpVariable [[ptr_type_1]] UniformConstant
10+
11+
vk::SampledTexture2D<float4> tex1 : register(t1);
12+
13+
void main() {
14+
// CHECK: OpStore %pos1 [[cu12]]
15+
// CHECK-NEXT: [[pos1:%[0-9]+]] = OpLoad %v2uint %pos1
16+
// CHECK-NEXT: [[tex1_load:%[a-zA-Z0-9_]+]] = OpLoad [[type_sampled_image_1]] [[tex1]]
17+
// CHECK-NEXT: [[tex_img:%[0-9]+]] = OpImage [[type_2d_image_1]] [[tex1_load]]
18+
// CHECK-NEXT: [[result1:%[0-9]+]] = OpImageFetch %v4float [[tex_img]] [[pos1]] Lod %uint_2
19+
// CHECK-NEXT: OpStore %a1 [[result1]]
20+
uint2 pos1 = uint2(1,2);
21+
float4 a1 = tex1.mips[2][pos1];
22+
}

tools/clang/test/SemaHLSL/vk.sampledtexture.struct.error.hlsl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
struct Struct { float f; };
55

6-
vk::SampledTexture2D<Struct> t;
6+
vk::SampledTexture2D<Struct> t; // expected-error {{elements of typed buffers and textures must be scalars or vectors}}
77

88
float4 main(float2 f2 : F2) : SV_TARGET
99
{
10-
return t.Sample(f2); // expected-error {{cannot Sample from resource containing}} expected-error {{cannot initialize return object}}
10+
return t.Sample(f2);
1111
}

0 commit comments

Comments
 (0)