Skip to content

Commit 8e949f8

Browse files
authored
[SPIR-V][vk::SampledTexture] #9. Support vk::SampledTexture2DMS and vk::SampledTexture2DMSArray type (microsoft#8209)
Part of microsoft#7979 `vk::SampledTexture2DMS` and `vk::SampledTexture2DMSArray`. The function definition is exactly the same as that of Texture2DMS and Texture2DMSArray, which do not take a sampler argument. Added a new test for `GetSamplePosition()` and `sample[][]`
1 parent f62b9b4 commit 8e949f8

11 files changed

Lines changed: 314 additions & 16 deletions

include/dxc/dxcapi.internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ enum LEGAL_INTRINSIC_COMPTYPES {
142142
LICOMPTYPE_VK_BUFFER_POINTER = 56,
143143
LICOMPTYPE_VK_SAMPLED_TEXTURE2D = 57,
144144
LICOMPTYPE_VK_SAMPLED_TEXTURE2D_ARRAY = 58,
145-
LICOMPTYPE_COUNT = 59
145+
LICOMPTYPE_VK_SAMPLED_TEXTURE2DMS = 59,
146+
LICOMPTYPE_VK_SAMPLED_TEXTURE2DMS_ARRAY = 60,
147+
LICOMPTYPE_COUNT = 61
146148
#else
147149
LICOMPTYPE_COUNT = 56
148150
#endif

tools/clang/include/clang/SPIRV/AstTypeProbe.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,10 @@ bool isTexture(QualType);
257257
/// Texture2DMSArray type.
258258
bool isTextureMS(QualType);
259259

260+
/// \brief Returns true if the given type is an HLSL SampledTexture2DMS or
261+
/// SampledTexture2DMSArray type.
262+
bool isSampledTextureMS(QualType);
263+
260264
/// \brief Returns true if the given type is an HLSL SampledTexture type.
261265
bool isSampledTexture(QualType);
262266

tools/clang/lib/SPIRV/AstTypeProbe.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,8 @@ bool isSampledTexture(QualType type) {
931931
const auto name = rt->getDecl()->getName();
932932
// TODO(https://github.com/microsoft/DirectXShaderCompiler/issues/7979): Add
933933
// other sampled texture types as needed.
934-
if (name == "SampledTexture2D" || name == "SampledTexture2DArray")
934+
if (name == "SampledTexture2D" || name == "SampledTexture2DArray" ||
935+
name == "SampledTexture2DMS" || name == "SampledTexture2DMSArray")
935936
return true;
936937
}
937938
return false;
@@ -946,6 +947,15 @@ bool isTextureMS(QualType type) {
946947
return false;
947948
}
948949

950+
bool isSampledTextureMS(QualType type) {
951+
if (const auto *rt = type->getAs<RecordType>()) {
952+
const auto name = rt->getDecl()->getName();
953+
if (name == "SampledTexture2DMS" || name == "SampledTexture2DMSArray")
954+
return true;
955+
}
956+
return false;
957+
}
958+
949959
bool isSampler(QualType type) {
950960
if (const auto *rt = type->getAs<RecordType>()) {
951961
const auto name = rt->getDecl()->getName();

tools/clang/lib/SPIRV/LowerTypeVisitor.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,8 @@ const SpirvType *LowerTypeVisitor::lowerVkTypeInVkNamespace(
849849
assert(visitedTypeStack.size() == visitedTypeStackSize);
850850
return pointerType;
851851
}
852-
if (name == "SampledTexture2D" || name == "SampledTexture2DArray") {
852+
if (name == "SampledTexture2D" || name == "SampledTexture2DArray" ||
853+
name == "SampledTexture2DMS" || name == "SampledTexture2DMSArray") {
853854
const auto sampledType = hlsl::GetHLSLResourceResultType(type);
854855
auto loweredType = lowerType(getElementType(astContext, sampledType), rule,
855856
/*isRowMajor*/ llvm::None, srcLoc);
@@ -860,11 +861,14 @@ const SpirvType *LowerTypeVisitor::lowerVkTypeInVkNamespace(
860861
loweredType = spvContext.getUIntType(32);
861862
}
862863

863-
const bool isArray = (name == "SampledTexture2DArray");
864+
const bool isArray =
865+
(name == "SampledTexture2DArray" || name == "SampledTexture2DMSArray");
866+
const bool isMS =
867+
(name == "SampledTexture2DMS" || name == "SampledTexture2DMSArray");
864868

865869
const auto *imageType = spvContext.getImageType(
866-
loweredType, spv::Dim::Dim2D, ImageType::WithDepth::No, isArray,
867-
false /* ms */, ImageType::WithSampler::Yes, spv::ImageFormat::Unknown);
870+
loweredType, spv::Dim::Dim2D, ImageType::WithDepth::No, isArray, isMS,
871+
ImageType::WithSampler::Yes, spv::ImageFormat::Unknown);
868872
return spvContext.getSampledImageType(imageType);
869873
}
870874
emitError("unknown type %0 in vk namespace", srcLoc) << type;

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4283,9 +4283,21 @@ SpirvInstruction *SpirvEmitter::processRWByteAddressBufferAtomicMethods(
42834283
SpirvInstruction *
42844284
SpirvEmitter::processGetSamplePosition(const CXXMemberCallExpr *expr) {
42854285
const auto *object = expr->getImplicitObjectArgument()->IgnoreParens();
4286+
auto *objectInstr = loadIfGLValue(object);
4287+
if (isSampledTexture(object->getType())) {
4288+
LowerTypeVisitor lowerTypeVisitor(astContext, spvContext, spirvOptions,
4289+
spvBuilder);
4290+
const SpirvType *spvType =
4291+
lowerTypeVisitor.lowerType(object->getType(), SpirvLayoutRule::Void,
4292+
llvm::None, expr->getExprLoc());
4293+
const auto *sampledType = cast<SampledImageType>(spvType);
4294+
const SpirvType *imgType = sampledType->getImageType();
4295+
objectInstr = spvBuilder.createUnaryOp(spv::Op::OpImage, imgType,
4296+
objectInstr, expr->getExprLoc());
4297+
}
42864298
auto *sampleCount = spvBuilder.createImageQuery(
42874299
spv::Op::OpImageQuerySamples, astContext.UnsignedIntTy,
4288-
expr->getExprLoc(), loadIfGLValue(object));
4300+
expr->getExprLoc(), objectInstr);
42894301
if (!spirvOptions.noWarnEmulatedFeatures)
42904302
emitWarning("GetSamplePosition is emulated using many SPIR-V instructions "
42914303
"due to lack of direct SPIR-V equivalent, so it only supports "
@@ -4403,7 +4415,8 @@ SpirvEmitter::processBufferTextureGetDimensions(const CXXMemberCallExpr *expr) {
44034415
mipLevel = expr->getArg(0);
44044416
numLevels = expr->getArg(numArgs - 1);
44054417
}
4406-
if (isTextureMS(type)) {
4418+
4419+
if (isSampledTextureMS(type) || isTextureMS(type)) {
44074420
numSamples = expr->getArg(numArgs - 1);
44084421
}
44094422

@@ -4744,7 +4757,7 @@ SpirvInstruction *SpirvEmitter::processBufferTextureLoad(
47444757

47454758
// For Texture2DMS and Texture2DMSArray, Sample must be used rather than Lod.
47464759
SpirvInstruction *sampleNumber = nullptr;
4747-
if (isTextureMS(type) || isSubpassInputMS(type)) {
4760+
if (isSampledTextureMS(type) || isTextureMS(type) || isSubpassInputMS(type)) {
47484761
sampleNumber = lod;
47494762
lod = nullptr;
47504763
}
@@ -6509,7 +6522,8 @@ SpirvEmitter::processBufferTextureLoad(const CXXMemberCallExpr *expr) {
65096522

65106523
const auto numArgs = expr->getNumArgs();
65116524
const auto *locationArg = expr->getArg(0);
6512-
const bool textureMS = isTextureMS(objectType);
6525+
const bool textureMS =
6526+
isTextureMS(objectType) || isSampledTextureMS(objectType);
65136527
const bool hasStatusArg =
65146528
expr->getArg(numArgs - 1)->getType()->isUnsignedIntegerType();
65156529
auto *status = hasStatusArg ? doExpr(expr->getArg(numArgs - 1)) : nullptr;

tools/clang/lib/Sema/SemaHLSL.cpp

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,8 @@ enum ArBasicKind {
202202
AR_OBJECT_VK_BUFFER_POINTER,
203203
AR_OBJECT_VK_SAMPLED_TEXTURE2D,
204204
AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY,
205+
AR_OBJECT_VK_SAMPLED_TEXTURE2DMS,
206+
AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY,
205207
#endif // ENABLE_SPIRV_CODEGEN
206208
// SPIRV change ends
207209

@@ -564,6 +566,8 @@ const UINT g_uBasicKindProps[] = {
564566
BPROP_OBJECT, // AR_OBJECT_VK_BUFFER_POINTER use recordType
565567
BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_VK_SAMPLED_TEXTURE2D
566568
BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY
569+
BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS
570+
BPROP_OBJECT | BPROP_RBUFFER, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY
567571
#endif // ENABLE_SPIRV_CODEGEN
568572
// SPIRV change ends
569573

@@ -1276,6 +1280,10 @@ static const ArBasicKind g_VKSampledTexture2DCT[] = {
12761280
AR_OBJECT_VK_SAMPLED_TEXTURE2D, AR_BASIC_UNKNOWN};
12771281
static const ArBasicKind g_VKSampledTexture2DArrayCT[] = {
12781282
AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY, AR_BASIC_UNKNOWN};
1283+
static const ArBasicKind g_VKSampledTexture2DMSCT[] = {
1284+
AR_OBJECT_VK_SAMPLED_TEXTURE2DMS, AR_BASIC_UNKNOWN};
1285+
static const ArBasicKind g_VKSampledTexture2DMSArrayCT[] = {
1286+
AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY, AR_BASIC_UNKNOWN};
12791287
#endif
12801288

12811289
// Basic kinds, indexed by a LEGAL_INTRINSIC_COMPTYPES value.
@@ -1338,9 +1346,11 @@ const ArBasicKind *g_LegalIntrinsicCompTypes[] = {
13381346
g_LinAlgCT, // LICOMPTYPE_LINALG
13391347
g_BuiltInTrianglePositionsCT, // LICOMPTYPE_BUILTIN_TRIANGLE_POSITIONS
13401348
#ifdef ENABLE_SPIRV_CODEGEN
1341-
g_VKBufferPointerCT, // LICOMPTYPE_VK_BUFFER_POINTER
1342-
g_VKSampledTexture2DCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2D
1343-
g_VKSampledTexture2DArrayCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2D_ARRAY
1349+
g_VKBufferPointerCT, // LICOMPTYPE_VK_BUFFER_POINTER
1350+
g_VKSampledTexture2DCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2D
1351+
g_VKSampledTexture2DArrayCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2D_ARRAY
1352+
g_VKSampledTexture2DMSCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2DMS
1353+
g_VKSampledTexture2DMSArrayCT, // LICOMPTYPE_VK_SAMPLED_TEXTURE2DMS_ARRAY
13441354
#endif
13451355
};
13461356
static_assert(
@@ -1401,7 +1411,8 @@ static const ArBasicKind g_ArBasicKindsAsTypes[] = {
14011411
AR_OBJECT_VK_INTEGRAL_CONSTANT, AR_OBJECT_VK_LITERAL,
14021412
AR_OBJECT_VK_SPV_INTRINSIC_TYPE, AR_OBJECT_VK_SPV_INTRINSIC_RESULT_ID,
14031413
AR_OBJECT_VK_BUFFER_POINTER, AR_OBJECT_VK_SAMPLED_TEXTURE2D,
1404-
AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY,
1414+
AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY, AR_OBJECT_VK_SAMPLED_TEXTURE2DMS,
1415+
AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY,
14051416
#endif // ENABLE_SPIRV_CODEGEN
14061417
// SPIRV change ends
14071418

@@ -1515,6 +1526,8 @@ static const uint8_t g_ArBasicKindsTemplateCount[] = {
15151526
2, // AR_OBJECT_VK_BUFFER_POINTER
15161527
1, // AR_OBJECT_VK_SAMPLED_TEXTURE2D
15171528
1, // AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY
1529+
1, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS
1530+
1, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY
15181531
#endif // ENABLE_SPIRV_CODEGEN
15191532
// SPIRV change ends
15201533

@@ -1670,6 +1683,8 @@ static const SubscriptOperatorRecord g_ArBasicKindsSubscripts[] = {
16701683
{0, MipsFalse, SampleFalse}, // AR_OBJECT_VK_BUFFER_POINTER
16711684
{2, MipsTrue, SampleFalse}, // AR_OBJECT_VK_SAMPLED_TEXTURE2D
16721685
{3, MipsTrue, SampleFalse}, // AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY
1686+
{2, MipsFalse, SampleTrue}, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS
1687+
{3, MipsFalse, SampleTrue}, // AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY
16731688
#endif // ENABLE_SPIRV_CODEGEN
16741689
// SPIRV change ends
16751690

@@ -1841,6 +1856,8 @@ static const char *g_ArBasicTypeNames[] = {
18411856
"BufferPointer",
18421857
"SampledTexture2D",
18431858
"SampledTexture2DArray",
1859+
"SampledTexture2DMS",
1860+
"SampledTexture2DMSArray",
18441861
#endif // ENABLE_SPIRV_CODEGEN
18451862
// SPIRV change ends
18461863

@@ -2502,6 +2519,14 @@ static void GetIntrinsicMethods(ArBasicKind kind,
25022519
*intrinsics = g_VkSampledTexture2DArrayMethods;
25032520
*intrinsicCount = _countof(g_VkSampledTexture2DArrayMethods);
25042521
break;
2522+
case AR_OBJECT_VK_SAMPLED_TEXTURE2DMS:
2523+
*intrinsics = g_VkSampledTexture2DMSMethods;
2524+
*intrinsicCount = _countof(g_VkSampledTexture2DMSMethods);
2525+
break;
2526+
case AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY:
2527+
*intrinsics = g_VkSampledTexture2DMSArrayMethods;
2528+
*intrinsicCount = _countof(g_VkSampledTexture2DMSArrayMethods);
2529+
break;
25052530
#endif
25062531
case AR_OBJECT_HIT_OBJECT:
25072532
*intrinsics = g_DxHitObjectMethods;
@@ -4108,7 +4133,9 @@ class HLSLExternalSource : public ExternalSemaSource {
41084133
recordDecl->setImplicit(true);
41094134
m_vkBufferPointerTemplateDecl = recordDecl->getDescribedClassTemplate();
41104135
} else if (kind == AR_OBJECT_VK_SAMPLED_TEXTURE2D ||
4111-
kind == AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY) {
4136+
kind == AR_OBJECT_VK_SAMPLED_TEXTURE2D_ARRAY ||
4137+
kind == AR_OBJECT_VK_SAMPLED_TEXTURE2DMS ||
4138+
kind == AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY) {
41124139
if (!m_vkNSDecl)
41134140
continue;
41144141
QualType float4Type =
@@ -5103,6 +5130,9 @@ class HLSLExternalSource : public ExternalSemaSource {
51035130
ResClass = DXIL::ResourceClass::SRV;
51045131
return true;
51055132
case AR_OBJECT_TEXTURE2DMS:
5133+
#ifdef ENABLE_SPIRV_CODEGEN
5134+
case AR_OBJECT_VK_SAMPLED_TEXTURE2DMS:
5135+
#endif
51065136
ResKind = DXIL::ResourceKind::Texture2DMS;
51075137
ResClass = DXIL::ResourceClass::SRV;
51085138
return true;
@@ -5111,6 +5141,9 @@ class HLSLExternalSource : public ExternalSemaSource {
51115141
ResClass = DXIL::ResourceClass::UAV;
51125142
return true;
51135143
case AR_OBJECT_TEXTURE2DMS_ARRAY:
5144+
#ifdef ENABLE_SPIRV_CODEGEN
5145+
case AR_OBJECT_VK_SAMPLED_TEXTURE2DMS_ARRAY:
5146+
#endif
51145147
ResKind = DXIL::ResourceKind::Texture2DMSArray;
51155148
ResClass = DXIL::ResourceClass::SRV;
51165149
return true;

tools/clang/test/CodeGenSPIRV/vk.sampledtexture.get-dimensions.hlsl

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,19 @@
77
// CHECK: [[type_2d_sampled_image:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image]]
88
// CHECK: [[type_2d_image_array:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 1 0 1 Unknown
99
// CHECK: [[type_2d_sampled_image_array:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_array]]
10+
// CHECK: [[type_2d_image_ms:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 0 1 1 Unknown
11+
// CHECK: [[type_2d_sampled_image_ms:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_ms]]
12+
// CHECK: [[type_2d_image_ms_array:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 1 1 1 Unknown
13+
// CHECK: [[type_2d_sampled_image_ms_array:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_ms_array]]
1014

1115
vk::SampledTexture2D<float4> tex2d;
1216
vk::SampledTexture2DArray<float4> tex2dArray;
17+
vk::SampledTexture2DMS<float4> tex2dMS;
18+
vk::SampledTexture2DMSArray<float4> tex2dMSArray;
1319

1420
void main() {
1521
uint mipLevel = 1;
16-
uint width, height, numLevels, elements;
22+
uint width, height, numLevels, elements, numSamples;
1723

1824
// CHECK: [[t1_load:%[0-9]+]] = OpLoad [[type_2d_sampled_image]] %tex2d
1925
// CHECK-NEXT: [[image1:%[0-9]+]] = OpImage [[type_2d_image]] [[t1_load]]
@@ -61,6 +67,30 @@ void main() {
6167
// CHECK-NEXT: OpStore %numLevels [[query_level_4]]
6268
tex2dArray.GetDimensions(mipLevel, width, height, elements, numLevels);
6369

70+
// CHECK: [[t3_load:%[0-9]+]] = OpLoad [[type_2d_sampled_image_ms]] %tex2dMS
71+
// CHECK-NEXT: [[image5:%[0-9]+]] = OpImage [[type_2d_image_ms]] [[t3_load]]
72+
// CHECK-NEXT: [[query5:%[0-9]+]] = OpImageQuerySize %v2uint [[image5]]
73+
// CHECK-NEXT: [[query5_0:%[0-9]+]] = OpCompositeExtract %uint [[query5]] 0
74+
// CHECK-NEXT: OpStore %width [[query5_0]]
75+
// CHECK-NEXT: [[query5_1:%[0-9]+]] = OpCompositeExtract %uint [[query5]] 1
76+
// CHECK-NEXT: OpStore %height [[query5_1]]
77+
// CHECK-NEXT: [[query5_samples:%[0-9]+]] = OpImageQuerySamples %uint [[image5]]
78+
// CHECK-NEXT: OpStore %numSamples [[query5_samples]]
79+
tex2dMS.GetDimensions(width, height, numSamples);
80+
81+
// CHECK: [[t4_load:%[0-9]+]] = OpLoad [[type_2d_sampled_image_ms_array]] %tex2dMSArray
82+
// CHECK-NEXT: [[image6:%[0-9]+]] = OpImage [[type_2d_image_ms_array]] [[t4_load]]
83+
// CHECK-NEXT: [[query6:%[0-9]+]] = OpImageQuerySize %v3uint [[image6]]
84+
// CHECK-NEXT: [[query6_0:%[0-9]+]] = OpCompositeExtract %uint [[query6]] 0
85+
// CHECK-NEXT: OpStore %width [[query6_0]]
86+
// CHECK-NEXT: [[query6_1:%[0-9]+]] = OpCompositeExtract %uint [[query6]] 1
87+
// CHECK-NEXT: OpStore %height [[query6_1]]
88+
// CHECK-NEXT: [[query6_2:%[0-9]+]] = OpCompositeExtract %uint [[query6]] 2
89+
// CHECK-NEXT: OpStore %elements [[query6_2]]
90+
// CHECK-NEXT: [[query6_samples:%[0-9]+]] = OpImageQuerySamples %uint [[image6]]
91+
// CHECK-NEXT: OpStore %numSamples [[query6_samples]]
92+
tex2dMSArray.GetDimensions(width, height, elements, numSamples);
93+
6494
float f_width, f_height, f_numLevels;
6595
// CHECK: [[t1_load:%[0-9]+]] = OpLoad [[type_2d_sampled_image]] %tex2d
6696
// CHECK-NEXT: [[image1:%[0-9]+]] = OpImage [[type_2d_image]] [[t1_load]]

0 commit comments

Comments
 (0)