Skip to content

Commit 6bc83c2

Browse files
committed
Implement function
1 parent 8030b4e commit 6bc83c2

File tree

4 files changed

+50
-7
lines changed

4 files changed

+50
-7
lines changed

tools/clang/lib/AST/ASTContextHLSL.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,18 @@ CXXRecordDecl *hlsl::DeclareVkSampledTexture2DType(ASTContext &context,
14341434
context, "op", "", static_cast<int>(hlsl::IntrinsicOp::MOP_Sample)));
14351435
sampleDecl4->addAttr(HLSLCXXOverloadAttr::CreateImplicit(context));
14361436

1437+
// CalculateLevelOfDetail(location)
1438+
CXXMethodDecl *lodDecl = CreateObjectFunctionDeclarationWithParams(
1439+
context, recordDecl, floatType, ArrayRef<QualType>(float2Type),
1440+
ArrayRef<StringRef>(StringRef("location")),
1441+
context.DeclarationNames.getIdentifier(
1442+
&context.Idents.get("CalculateLevelOfDetail")),
1443+
/*isConst*/ true);
1444+
lodDecl->addAttr(HLSLIntrinsicAttr::CreateImplicit(
1445+
context, "op", "",
1446+
static_cast<int>(hlsl::IntrinsicOp::MOP_CalculateLevelOfDetail)));
1447+
lodDecl->addAttr(HLSLCXXOverloadAttr::CreateImplicit(context));
1448+
14371449
Builder.completeDefinition();
14381450
return recordDecl;
14391451
}

tools/clang/lib/SPIRV/SpirvEmitter.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4451,15 +4451,25 @@ SpirvEmitter::processTextureLevelOfDetail(const CXXMemberCallExpr *expr,
44514451
// Texture2D(Array).CalculateLevelOfDetail(SamplerState S, float2 xy);
44524452
// TextureCube(Array).CalculateLevelOfDetail(SamplerState S, float3 xyz);
44534453
// Texture3D.CalculateLevelOfDetail(SamplerState S, float3 xyz);
4454+
// SampledTexture2D.CalculateLevelOfDetail(float2 xy);
44544455
// Return type is always a single float (LOD).
4455-
assert(expr->getNumArgs() == 2u);
4456-
const auto *object = expr->getImplicitObjectArgument();
4457-
auto *objectInfo = loadIfGLValue(object);
4458-
auto *samplerState = doExpr(expr->getArg(0));
4459-
auto *coordinate = doExpr(expr->getArg(1));
44604456

4461-
auto *sampledImage = spvBuilder.createSampledImage(
4462-
object->getType(), objectInfo, samplerState, expr->getExprLoc());
4457+
const auto *imageExpr = expr->getImplicitObjectArgument();
4458+
const QualType imageType = imageExpr->getType();
4459+
// numarg is 1 if isSampledTexture(imageType). otherwise 2.
4460+
assert(expr->getNumArgs() == (isSampledTexture(imageType) ? 1u : 2u));
4461+
4462+
auto *objectInfo = loadIfGLValue(imageExpr);
4463+
auto *samplerState =
4464+
isSampledTexture(imageType) ? nullptr : doExpr(expr->getArg(0));
4465+
auto *coordinate = isSampledTexture(imageType) ? doExpr(expr->getArg(0))
4466+
: doExpr(expr->getArg(1));
4467+
4468+
auto *sampledImage =
4469+
isSampledTexture(imageType)
4470+
? objectInfo
4471+
: spvBuilder.createSampledImage(imageExpr->getType(), objectInfo,
4472+
samplerState, expr->getExprLoc());
44634473

44644474
// The result type of OpImageQueryLod must be a float2.
44654475
const QualType queryResultType =
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %dxc -T ps_6_0 -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]] 0
20+
float lod = t1.CalculateLevelOfDetail(xy);
21+
}

tools/clang/test/CodeGenSPIRV/vk.sampledtexture2d.hlsl renamed to tools/clang/test/CodeGenSPIRV/vk.sampledtexture.sample.hlsl

File renamed without changes.

0 commit comments

Comments
 (0)