Skip to content

Commit 6f759a3

Browse files
authored
[SPIR-V][vk::SampledTexture] #4. Add .Load() methods for vk::SampledTexture2D type. (#8096)
Part of #7979 Implement `Load()` methods for `vk::SampledTexture2D`: ```hlsl ParamType Load(int3 location, [], [ParamType offset], [uint status]); ``` Please see the last commit.
1 parent 737f1de commit 6f759a3

File tree

3 files changed

+70
-4
lines changed

3 files changed

+70
-4
lines changed

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4693,9 +4693,11 @@ SpirvInstruction *SpirvEmitter::processBufferTextureLoad(
46934693
// The result type of an OpImageFetch must be a vec4 of float or int.
46944694
const auto type = object->getType();
46954695
assert(isBuffer(type) || isRWBuffer(type) || isTexture(type) ||
4696-
isRWTexture(type) || isSubpassInput(type) || isSubpassInputMS(type));
4696+
isRWTexture(type) || isSubpassInput(type) || isSubpassInputMS(type) ||
4697+
isSampledTexture(type));
46974698

4698-
const bool doFetch = isBuffer(type) || isTexture(type);
4699+
const bool doFetch =
4700+
isBuffer(type) || isTexture(type) || isSampledTexture(type);
46994701
const bool rasterizerOrdered = isRasterizerOrderedView(type);
47004702

47014703
if (rasterizerOrdered) {
@@ -4759,6 +4761,18 @@ SpirvInstruction *SpirvEmitter::processBufferTextureLoad(
47594761

47604762
// OpImageFetch and OpImageRead can only fetch a vector of 4 elements.
47614763
const QualType texelType = astContext.getExtVectorType(elemType, 4u);
4764+
4765+
if (isSampledTexture(type)) {
4766+
LowerTypeVisitor lowerTypeVisitor(astContext, spvContext, spirvOptions,
4767+
spvBuilder);
4768+
const SpirvType *spvType = lowerTypeVisitor.lowerType(
4769+
type, SpirvLayoutRule::Void, llvm::None, loc);
4770+
// Get image type based on type, assuming type is a sampledimage type
4771+
const auto *sampledImageType = cast<SampledImageType>(spvType);
4772+
const SpirvType *imgType = sampledImageType->getImageType();
4773+
objectInfo =
4774+
spvBuilder.createUnaryOp(spv::Op::OpImage, imgType, objectInfo, loc);
4775+
}
47624776
auto *texel = spvBuilder.createImageFetchOrRead(
47634777
doFetch, texelType, type, objectInfo, location, lod, constOffset,
47644778
/*constOffsets*/ nullptr, sampleNumber, residencyCode, loc, range);
@@ -6345,7 +6359,8 @@ SpirvEmitter::processTextureSampleCmpLevel(const CXXMemberCallExpr *expr) {
63456359
SpirvInstruction *
63466360
SpirvEmitter::processBufferTextureLoad(const CXXMemberCallExpr *expr) {
63476361
// Signature:
6348-
// For Texture1D, Texture1DArray, Texture2D, Texture2DArray, Texture3D:
6362+
// For Texture1D, Texture1DArray, Texture2D, Texture2DArray, Texture3D
6363+
// and their SampledTexture variants:
63496364
// ret Object.Load(int Location
63506365
// [, int Offset]
63516366
// [, uint status]);
@@ -6403,7 +6418,7 @@ SpirvEmitter::processBufferTextureLoad(const CXXMemberCallExpr *expr) {
64036418
// and 1 for location.
64046419
const bool hasOffsetArg = numArgs - hasStatusArg - textureMS - 1 > 0;
64056420

6406-
if (isTexture(objectType)) {
6421+
if (isTexture(objectType) || isSampledTexture(objectType)) {
64076422
// .Load() has a second optional paramter for offset.
64086423
SpirvInstruction *location = doExpr(locationArg);
64096424
SpirvInstruction *constOffset = nullptr, *varOffset = nullptr;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %dxc -T ps_6_0 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
4+
vk::SampledTexture2D<float4> tex2D_F4 : register(t1);
5+
6+
// CHECK: OpCapability SparseResidency
7+
8+
// CHECK: [[v2ic:%[0-9]+]] = OpConstantComposite %v2int %int_1 %int_2
9+
10+
// CHECK: %SparseResidencyStruct = OpTypeStruct %uint %v4float
11+
12+
float4 main(int3 location: A) : SV_Target {
13+
uint status;
14+
15+
// CHECK: [[loc:%[0-9]+]] = OpLoad %v3int %location
16+
// CHECK-NEXT: [[coord_0:%[0-9]+]] = OpVectorShuffle %v2int [[loc]] [[loc]] 0 1
17+
// CHECK-NEXT: [[lod_0:%[0-9]+]] = OpCompositeExtract %int [[loc]] 2
18+
// CHECK-NEXT: [[tex:%[0-9]+]] = OpLoad %type_sampled_image %tex2D_F4
19+
// CHECK-NEXT: [[tex_img:%[0-9]+]] = OpImage %type_2d_image [[tex]]
20+
// CHECK-NEXT: {{%[0-9]+}} = OpImageFetch %v4float [[tex_img]] [[coord_0]] Lod [[lod_0]]
21+
float4 val1 = tex2D_F4.Load(location);
22+
23+
// CHECK: [[loc:%[0-9]+]] = OpLoad %v3int %location
24+
// CHECK-NEXT: [[coord_0:%[0-9]+]] = OpVectorShuffle %v2int [[loc]] [[loc]] 0 1
25+
// CHECK-NEXT: [[lod_0:%[0-9]+]] = OpCompositeExtract %int [[loc]] 2
26+
// CHECK-NEXT: [[tex:%[0-9]+]] = OpLoad %type_sampled_image %tex2D_F4
27+
// CHECK-NEXT: [[tex_img:%[0-9]+]] = OpImage %type_2d_image [[tex]]
28+
// CHECK-NEXT: {{%[0-9]+}} = OpImageFetch %v4float [[tex_img]] [[coord_0]] Lod|ConstOffset [[lod_0]] [[v2ic]]
29+
float4 val2 = tex2D_F4.Load(location, int2(1, 2));
30+
31+
/////////////////////////////////
32+
/// Using the Status argument ///
33+
/////////////////////////////////
34+
35+
// CHECK: [[loc:%[0-9]+]] = OpLoad %v3int %location
36+
// CHECK-NEXT: [[coord_0:%[0-9]+]] = OpVectorShuffle %v2int [[loc]] [[loc]] 0 1
37+
// CHECK-NEXT: [[lod_0:%[0-9]+]] = OpCompositeExtract %int [[loc]] 2
38+
// CHECK-NEXT: [[tex:%[0-9]+]] = OpLoad %type_sampled_image %tex2D_F4
39+
// CHECK-NEXT: [[tex_img:%[0-9]+]] = OpImage %type_2d_image [[tex]]
40+
// CHECK-NEXT:[[structResult:%[0-9]+]] = OpImageSparseFetch %SparseResidencyStruct [[tex_img]] [[coord_0]] Lod|ConstOffset [[lod_0]] [[v2ic]]
41+
// CHECK-NEXT: [[status:%[0-9]+]] = OpCompositeExtract %uint [[structResult]] 0
42+
// CHECK-NEXT: OpStore %status [[status]]
43+
// CHECK-NEXT: [[v4result:%[0-9]+]] = OpCompositeExtract %v4float [[structResult]] 1
44+
// CHECK-NEXT: OpStore %val3 [[v4result]]
45+
float4 val3 = tex2D_F4.Load(location, int2(1, 2), status);
46+
47+
return 1.0;
48+
}

utils/hct/gen_intrin_main.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,4 +1242,7 @@ namespace VkSampledTexture2DMethods {
12421242
void [[]] GetDimensions(in uint x, out float_like width, out $type2 height, out $type2 levels) : resinfo;
12431243
void [[]] GetDimensions(out uint_only width, out $type1 height) : resinfo_uint_o;
12441244
void [[]] GetDimensions(out float_like width, out $type1 height) : resinfo_o;
1245+
$classT [[ro]] Load(in int<3> x) : tex2d_t_load;
1246+
$classT [[ro]] Load(in int<3> x, in int<2> o) : tex2d_t_load_o;
1247+
$classT [[]] Load(in int<3> x, in int<2> o, out uint_only status) : tex2d_t_load_o_s;
12451248
} namespace

0 commit comments

Comments
 (0)