Skip to content

Commit d50ebb9

Browse files
authored
[SPIR-V][vk::SampledTexture] #2. Add .CalculateLevelOfDetailUnclamped() and .Gather() methods for vk::SampledTexture2D type. (microsoft#8070)
Part of microsoft#7979 Implement methods for `vk::SampledTexture2D`: ```hlsl float CalculateLevelOfDetailUnclamped(float2 location); <Template Type>4 Gather(float2 location, [int2 offset], [uint status]); ```
1 parent e661673 commit d50ebb9

5 files changed

Lines changed: 80 additions & 2 deletions

File tree

tools/clang/include/clang/SPIRV/SpirvBuilder.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ class SpirvBuilder {
339339

340340
/// \brief Creates SPIR-V instructions for gathering the given image.
341341
///
342+
/// If the of `image` is a sampled image, then that image will be gathered.
343+
/// In this case, `sampler` must be `nullptr`. If `image` is not a sampled
344+
/// image, a sampled image will be created by combining `image` and `sampler`.
345+
///
342346
/// If compareVal is given a non-null value, OpImageDrefGather or
343347
/// OpImageSparseDrefGather will be generated; otherwise, OpImageGather or
344348
/// OpImageSparseGather will be generated.

tools/clang/lib/SPIRV/SpirvBuilder.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -714,8 +714,15 @@ SpirvInstruction *SpirvBuilder::createImageGather(
714714
assert(insertPoint && "null insert point");
715715

716716
// An OpSampledImage is required to do the image sampling.
717-
auto *sampledImage =
718-
createSampledImage(imageType, image, sampler, loc, range);
717+
// Skip creating OpSampledImage if the imageType is a sampled texture.
718+
SpirvInstruction *sampledImage = nullptr;
719+
if (isSampledTexture(imageType)) {
720+
assert(!sampler &&
721+
"sampler must be null when sampling from a sampled texture");
722+
sampledImage = image;
723+
} else {
724+
sampledImage = createSampledImage(imageType, image, sampler, loc, range);
725+
}
719726

720727
// TODO: Update ImageGather to accept minLod if necessary.
721728
const auto mask = composeImageOperandsMask(
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %dxc -T ps_6_8 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: OpCapability ImageQuery
4+
5+
vk::SampledTexture2D<float4> t1 : register(t0);
6+
7+
// CHECK: %type_2d_image = OpTypeImage %float 2D 0 0 0 1 Unknown
8+
// CHECK: %type_sampled_image = OpTypeSampledImage %type_2d_image
9+
// CHECK: [[ptr:%[a-zA-Z0-9_]+]] = OpTypePointer UniformConstant %type_sampled_image
10+
11+
// CHECK: %t1 = OpVariable [[ptr]] UniformConstant
12+
13+
void main() {
14+
float2 xy = float2(0.5, 0.5);
15+
16+
//CHECK: [[tex1:%[a-zA-Z0-9_]+]] = OpLoad %type_sampled_image %t1
17+
//CHECK-NEXT: [[xy_load:%[a-zA-Z0-9_]+]] = OpLoad %v2float %xy
18+
//CHECK-NEXT: [[query:%[a-zA-Z0-9_]+]] = OpImageQueryLod %v2float [[tex1]] [[xy_load]]
19+
//CHECK-NEXT: {{%[0-9]+}} = OpCompositeExtract %float [[query]] 1
20+
float lod1 = t1.CalculateLevelOfDetailUnclamped(xy);
21+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// RUN: %dxc -T ps_6_7 -E main -fcgl %s -spirv | FileCheck %s
2+
3+
// CHECK: OpCapability SparseResidency
4+
5+
// CHECK: [[v2fc:%[0-9]+]] = OpConstantComposite %v2float %float_0_5 %float_0_25
6+
// CHECK: [[v2ic:%[0-9]+]] = OpConstantComposite %v2int %int_2 %int_3
7+
8+
// CHECK: [[type_2d_image_1:%[a-zA-Z0-9_]+]] = OpTypeImage %float 2D 0 0 0 1 Unknown
9+
// CHECK: [[type_sampled_image_1:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_1]]
10+
// CHECK: [[ptr_type_1:%[a-zA-Z0-9_]+]] = OpTypePointer UniformConstant [[type_sampled_image_1]]
11+
12+
// CHECK: [[type_2d_image_2:%[a-zA-Z0-9_]+]] = OpTypeImage %uint 2D 0 0 0 1 Unknown
13+
// CHECK: [[type_sampled_image_2:%[a-zA-Z0-9_]+]] = OpTypeSampledImage [[type_2d_image_2]]
14+
// CHECK: [[ptr_type_2:%[a-zA-Z0-9_]+]] = OpTypePointer UniformConstant [[type_sampled_image_2]]
15+
16+
// CHECK: %SparseResidencyStruct = OpTypeStruct %uint %v4float
17+
18+
// CHECK: [[tex1:%[a-zA-Z0-9_]+]] = OpVariable [[ptr_type_1]] UniformConstant
19+
// CHECK: [[tex2:%[a-zA-Z0-9_]+]] = OpVariable [[ptr_type_2]] UniformConstant
20+
21+
vk::SampledTexture2D<float4> tex1 : register(t1);
22+
vk::SampledTexture2D<uint> tex2 : register(t2);
23+
24+
float4 main() : SV_Target {
25+
26+
// CHECK: [[tex1_load:%[a-zA-Z0-9_]+]] = OpLoad [[type_sampled_image_1]] [[tex1]]
27+
// CHECK: [[val1:%[a-zA-Z0-9_]+]] = OpImageGather %v4float [[tex1_load]] [[v2fc]] %int_0 None
28+
float4 val1 = tex1.Gather(float2(0.5, 0.25));
29+
30+
// CHECK: [[tex2_load:%[a-zA-Z0-9_]+]] = OpLoad [[type_sampled_image_2]] [[tex2]]
31+
// CHECK: [[val2:%[a-zA-Z0-9_]+]] = OpImageGather %v4uint [[tex2_load]] [[v2fc]] %int_0 ConstOffset [[v2ic]]
32+
uint4 val2 = tex2.Gather(float2(0.5, 0.25), int2(2, 3));
33+
34+
// CHECK: [[tex3_load:%[a-zA-Z0-9_]+]] = OpLoad [[type_sampled_image_1]] [[tex1]]
35+
// CHECK: [[val3:%[a-zA-Z0-9_]+]] = OpImageSparseGather %SparseResidencyStruct [[tex3_load]] [[v2fc]] %int_0 ConstOffset [[v2ic]]
36+
// CHECK: [[status_0:%[a-zA-Z0-9_]+]] = OpCompositeExtract %uint [[val3]] 0
37+
// CHECK: OpStore %status [[status_0]]
38+
uint status;
39+
float4 val3 = tex1.Gather(float2(0.5, 0.25), int2(2, 3), status);
40+
41+
return 1.0;
42+
}

utils/hct/gen_intrin_main.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,4 +1239,8 @@ namespace VkSampledTexture2DMethods {
12391239
$classT [[ro]] Sample(in float<2> x, in int<2> o, in float clamp) : tex2d_t_o_cl;
12401240
$classT [[]] Sample(in float<2> x, in int<2> o, in float clamp, out uint_only status) : tex2d_t_o_cl_s;
12411241
float [[ro]] CalculateLevelOfDetail(in float<2> x) : tex2d_t_calc_lod;
1242+
float [[ro]] CalculateLevelOfDetailUnclamped(in float<2> x) : tex2d_t_calc_lod_unclamped;
1243+
$match<0, -1> void<4> [[ro]] Gather(in float<2> x) : tex2d_t_gather;
1244+
$match<0, -1> void<4> [[ro]] Gather(in float<2> x, in int<2> o) : tex2d_t_gather_o;
1245+
$match<0, -1> void<4> [[]] Gather(in float<2> x, in int<2> o, out uint_only status) : tex2d_t_gather_o_s;
12421246
} namespace

0 commit comments

Comments
 (0)