https://github.com/s-perron updated https://github.com/llvm/llvm-project/pull/188574
>From 76cc2e8a63ee0a7304d7c0554fae5c289bba8137 Mon Sep 17 00:00:00 2001 From: Steven Perron <[email protected]> Date: Wed, 11 Mar 2026 12:18:08 -0400 Subject: [PATCH] [HLSL] Add CalculateLevelOfDetail methods to Texture2D This adds the CalculateLevelOfDetail and CalculateLevelOfDetailUnclamped methods to Texture2D using the establish pattern used for other methods. Assisted-by: Gemini --- clang/include/clang/Basic/Builtins.td | 12 +++++ clang/lib/CodeGen/CGHLSLBuiltins.cpp | 20 +++++++++ clang/lib/CodeGen/CGHLSLRuntime.h | 3 ++ clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp | 32 ++++++++++++++ clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h | 1 + clang/lib/Sema/HLSLExternalSemaSource.cpp | 1 + clang/lib/Sema/SemaHLSL.cpp | 14 ++++++ clang/test/AST/HLSL/Texture2D-scalar-AST.hlsl | 44 +++++++++++++++++++ clang/test/AST/HLSL/Texture2D-vector-AST.hlsl | 44 +++++++++++++++++++ .../Texture2D-CalculateLevelOfDetail.hlsl | 44 +++++++++++++++++++ .../Texture2D-CalculateLevelOfDetail.hlsl | 33 ++++++++++++++ llvm/include/llvm/IR/IntrinsicsDirectX.td | 10 +++++ 12 files changed, 258 insertions(+) create mode 100644 clang/test/CodeGenHLSL/resources/Texture2D-CalculateLevelOfDetail.hlsl create mode 100644 clang/test/SemaHLSL/Resources/Texture2D-CalculateLevelOfDetail.hlsl diff --git a/clang/include/clang/Basic/Builtins.td b/clang/include/clang/Basic/Builtins.td index f1743c7286def..a1afa82955f6a 100644 --- a/clang/include/clang/Basic/Builtins.td +++ b/clang/include/clang/Basic/Builtins.td @@ -5078,6 +5078,18 @@ def HLSLResourceSampleCmpLevelZero : LangBuiltin<"HLSL_LANG"> { let Prototype = "void(...)"; } +def HLSLResourceCalculateLod : LangBuiltin<"HLSL_LANG"> { + let Spellings = ["__builtin_hlsl_resource_calculate_lod"]; + let Attributes = [NoThrow]; + let Prototype = "void(...)"; +} + +def HLSLResourceCalculateLodUnclamped : LangBuiltin<"HLSL_LANG"> { + let Spellings = ["__builtin_hlsl_resource_calculate_lod_unclamped"]; + let Attributes = [NoThrow]; + let Prototype = "void(...)"; +} + def HLSLResourceGather : LangBuiltin<"HLSL_LANG"> { let Spellings = ["__builtin_hlsl_resource_gather"]; let Attributes = [NoThrow]; diff --git a/clang/lib/CodeGen/CGHLSLBuiltins.cpp b/clang/lib/CodeGen/CGHLSLBuiltins.cpp index 6803d6471979d..71ddbdebf7245 100644 --- a/clang/lib/CodeGen/CGHLSLBuiltins.cpp +++ b/clang/lib/CodeGen/CGHLSLBuiltins.cpp @@ -717,6 +717,26 @@ Value *CodeGenFunction::EmitHLSLBuiltinExpr(unsigned BuiltinID, return Builder.CreateIntrinsic( RetTy, CGM.getHLSLRuntime().getSampleCmpLevelZeroIntrinsic(), Args); } + case Builtin::BI__builtin_hlsl_resource_calculate_lod: { + Value *HandleOp = EmitScalarExpr(E->getArg(0)); + Value *SamplerOp = EmitScalarExpr(E->getArg(1)); + Value *CoordOp = EmitScalarExpr(E->getArg(2)); + + return Builder.CreateIntrinsic( + ConvertType(E->getType()), + CGM.getHLSLRuntime().getCalculateLodIntrinsic(), + {HandleOp, SamplerOp, CoordOp}); + } + case Builtin::BI__builtin_hlsl_resource_calculate_lod_unclamped: { + Value *HandleOp = EmitScalarExpr(E->getArg(0)); + Value *SamplerOp = EmitScalarExpr(E->getArg(1)); + Value *CoordOp = EmitScalarExpr(E->getArg(2)); + + return Builder.CreateIntrinsic( + ConvertType(E->getType()), + CGM.getHLSLRuntime().getCalculateLodUnclampedIntrinsic(), + {HandleOp, SamplerOp, CoordOp}); + } case Builtin::BI__builtin_hlsl_resource_gather: { Value *HandleOp = EmitScalarExpr(E->getArg(0)); Value *SamplerOp = EmitScalarExpr(E->getArg(1)); diff --git a/clang/lib/CodeGen/CGHLSLRuntime.h b/clang/lib/CodeGen/CGHLSLRuntime.h index 691b9e54dce61..f473ac13816c0 100644 --- a/clang/lib/CodeGen/CGHLSLRuntime.h +++ b/clang/lib/CodeGen/CGHLSLRuntime.h @@ -194,6 +194,9 @@ class CGHLSLRuntime { group_memory_barrier_with_group_sync) GENERATE_HLSL_INTRINSIC_FUNCTION(GetDimensionsX, resource_getdimensions_x) GENERATE_HLSL_INTRINSIC_FUNCTION(LoadLevel, resource_load_level) + GENERATE_HLSL_INTRINSIC_FUNCTION(CalculateLod, resource_calculate_lod) + GENERATE_HLSL_INTRINSIC_FUNCTION(CalculateLodUnclamped, + resource_calculate_lod_unclamped) GENERATE_HLSL_INTRINSIC_FUNCTION(DdxCoarse, ddx_coarse) GENERATE_HLSL_INTRINSIC_FUNCTION(DdyCoarse, ddy_coarse) GENERATE_HLSL_INTRINSIC_FUNCTION(DdxFine, ddx_fine) diff --git a/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp b/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp index 3c84cd28732c9..0e3b640fa060a 100644 --- a/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp +++ b/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.cpp @@ -1885,6 +1885,38 @@ BuiltinTypeDeclBuilder::addSampleCmpLevelZeroMethods(ResourceDimension Dim) { .finalize(); } +BuiltinTypeDeclBuilder & +BuiltinTypeDeclBuilder::addCalculateLodMethods(ResourceDimension Dim) { + assert(!Record->isCompleteDefinition() && "record is already complete"); + ASTContext &AST = Record->getASTContext(); + QualType ReturnType = AST.FloatTy; + QualType SamplerStateType = + lookupBuiltinType(SemaRef, "SamplerState", Record->getDeclContext()); + uint32_t VecSize = getResourceDimensions(Dim); + QualType FloatTy = AST.FloatTy; + QualType LocationTy = AST.getExtVectorType(FloatTy, VecSize); + using PH = BuiltinTypeMethodBuilder::PlaceHolder; + + // float CalculateLevelOfDetail(SamplerState s, float2 location) + BuiltinTypeMethodBuilder(*this, "CalculateLevelOfDetail", ReturnType) + .addParam("Sampler", SamplerStateType) + .addParam("Location", LocationTy) + .accessHandleFieldOnResource(PH::_0) + .callBuiltin("__builtin_hlsl_resource_calculate_lod", ReturnType, + PH::Handle, PH::LastStmt, PH::_1) + .finalize(); + + // float CalculateLevelOfDetailUnclamped(SamplerState s, float2 location) + return BuiltinTypeMethodBuilder(*this, "CalculateLevelOfDetailUnclamped", + ReturnType) + .addParam("Sampler", SamplerStateType) + .addParam("Location", LocationTy) + .accessHandleFieldOnResource(PH::_0) + .callBuiltin("__builtin_hlsl_resource_calculate_lod_unclamped", + ReturnType, PH::Handle, PH::LastStmt, PH::_1) + .finalize(); +} + QualType BuiltinTypeDeclBuilder::getGatherReturnType() { ASTContext &AST = SemaRef.getASTContext(); QualType T = getHandleElementType(); diff --git a/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h b/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h index 430dc00630676..6f64d751a5811 100644 --- a/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h +++ b/clang/lib/Sema/HLSLBuiltinTypeDeclBuilder.h @@ -108,6 +108,7 @@ class BuiltinTypeDeclBuilder { BuiltinTypeDeclBuilder &addSampleLevelMethods(ResourceDimension Dim); BuiltinTypeDeclBuilder &addSampleCmpMethods(ResourceDimension Dim); BuiltinTypeDeclBuilder &addSampleCmpLevelZeroMethods(ResourceDimension Dim); + BuiltinTypeDeclBuilder &addCalculateLodMethods(ResourceDimension Dim); BuiltinTypeDeclBuilder &addGatherMethods(ResourceDimension Dim); BuiltinTypeDeclBuilder &addGatherCmpMethods(ResourceDimension Dim); BuiltinTypeDeclBuilder &addIncrementCounterMethod(); diff --git a/clang/lib/Sema/HLSLExternalSemaSource.cpp b/clang/lib/Sema/HLSLExternalSemaSource.cpp index 8f99acb4dd442..926c3d80e3bd9 100644 --- a/clang/lib/Sema/HLSLExternalSemaSource.cpp +++ b/clang/lib/Sema/HLSLExternalSemaSource.cpp @@ -271,6 +271,7 @@ static BuiltinTypeDeclBuilder setupTextureType(CXXRecordDecl *Decl, Sema &S, .addSampleLevelMethods(Dim) .addSampleCmpMethods(Dim) .addSampleCmpLevelZeroMethods(Dim) + .addCalculateLodMethods(Dim) .addGatherMethods(Dim) .addGatherCmpMethods(Dim); } diff --git a/clang/lib/Sema/SemaHLSL.cpp b/clang/lib/Sema/SemaHLSL.cpp index 2b977b2793efe..5d7cec49c2b19 100644 --- a/clang/lib/Sema/SemaHLSL.cpp +++ b/clang/lib/Sema/SemaHLSL.cpp @@ -3522,6 +3522,17 @@ static bool CheckTextureSamplerAndLocation(Sema &S, CallExpr *TheCall) { return false; } +static bool CheckCalculateLodBuiltin(Sema &S, CallExpr *TheCall) { + if (S.checkArgCount(TheCall, 3)) + return true; + + if (CheckTextureSamplerAndLocation(S, TheCall)) + return true; + + TheCall->setType(S.Context.FloatTy); + return false; +} + static bool CheckGatherBuiltin(Sema &S, CallExpr *TheCall, bool IsCmp) { if (S.checkArgCountRange(TheCall, IsCmp ? 5 : 4, IsCmp ? 6 : 5)) return true; @@ -3882,6 +3893,9 @@ bool SemaHLSL::CheckBuiltinFunctionCall(unsigned BuiltinID, CallExpr *TheCall) { return CheckSamplingBuiltin(SemaRef, TheCall, SampleKind::Cmp); case Builtin::BI__builtin_hlsl_resource_sample_cmp_level_zero: return CheckSamplingBuiltin(SemaRef, TheCall, SampleKind::CmpLevelZero); + case Builtin::BI__builtin_hlsl_resource_calculate_lod: + case Builtin::BI__builtin_hlsl_resource_calculate_lod_unclamped: + return CheckCalculateLodBuiltin(SemaRef, TheCall); case Builtin::BI__builtin_hlsl_resource_gather: return CheckGatherBuiltin(SemaRef, TheCall, /*IsCmp=*/false); case Builtin::BI__builtin_hlsl_resource_gather_cmp: diff --git a/clang/test/AST/HLSL/Texture2D-scalar-AST.hlsl b/clang/test/AST/HLSL/Texture2D-scalar-AST.hlsl index fbebbf4a29c31..10ae411f474b6 100644 --- a/clang/test/AST/HLSL/Texture2D-scalar-AST.hlsl +++ b/clang/test/AST/HLSL/Texture2D-scalar-AST.hlsl @@ -466,6 +466,48 @@ // CHECK-NEXT: DeclRefExpr {{.*}} 'vector<int, 2>' lvalue ParmVar {{.*}} 'Offset' 'vector<int, 2>' // CHECK-NEXT: AlwaysInlineAttr +// CHECK: CXXMethodDecl {{.*}} CalculateLevelOfDetail 'float (hlsl::SamplerState, vector<float, 2>)' +// CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' +// CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' +// CHECK-NEXT: CompoundStmt +// CHECK-NEXT: ReturnStmt +// CHECK-NEXT: CStyleCastExpr {{.*}} 'float' <Dependent> +// CHECK-NEXT: CallExpr {{.*}} '<dependent type>' +// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_calculate_lod' 'void (...) noexcept' +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]] +// CHECK-SAME{LITERAL}: [[hlsl::contained_type(element_type)]] +// CHECK-SAME{LITERAL}: [[hlsl::resource_dimension(2D)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::Texture2D<element_type>' lvalue implicit this +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(Sampler)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::SamplerState' lvalue ParmVar {{.*}} 'Sampler' 'hlsl::SamplerState' +// CHECK-NEXT: DeclRefExpr {{.*}} 'vector<float, 2>' lvalue ParmVar {{.*}} 'Location' 'vector<float, 2>' +// CHECK-NEXT: AlwaysInlineAttr + +// CHECK: CXXMethodDecl {{.*}} CalculateLevelOfDetailUnclamped 'float (hlsl::SamplerState, vector<float, 2>)' +// CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' +// CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' +// CHECK-NEXT: CompoundStmt +// CHECK-NEXT: ReturnStmt +// CHECK-NEXT: CStyleCastExpr {{.*}} 'float' <Dependent> +// CHECK-NEXT: CallExpr {{.*}} '<dependent type>' +// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_calculate_lod_unclamped' 'void (...) noexcept' +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]] +// CHECK-SAME{LITERAL}: [[hlsl::contained_type(element_type)]] +// CHECK-SAME{LITERAL}: [[hlsl::resource_dimension(2D)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::Texture2D<element_type>' lvalue implicit this +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(Sampler)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::SamplerState' lvalue ParmVar {{.*}} 'Sampler' 'hlsl::SamplerState' +// CHECK-NEXT: DeclRefExpr {{.*}} 'vector<float, 2>' lvalue ParmVar {{.*}} 'Location' 'vector<float, 2>' +// CHECK-NEXT: AlwaysInlineAttr + // CHECK: CXXMethodDecl {{.*}} Gather 'vector<element_type, 4> (hlsl::SamplerState, vector<float, 2>)' inline // CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' // CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' @@ -769,5 +811,7 @@ void main(float2 loc, float cmp) { t.SampleCmp(scs, loc, cmp, int2(1, 2), 1.0f); t.SampleCmpLevelZero(scs, loc, cmp); t.SampleCmpLevelZero(scs, loc, cmp, int2(1, 2)); + t.CalculateLevelOfDetail(s, loc); + t.CalculateLevelOfDetailUnclamped(s, loc); t.Gather(s, loc); } diff --git a/clang/test/AST/HLSL/Texture2D-vector-AST.hlsl b/clang/test/AST/HLSL/Texture2D-vector-AST.hlsl index 19effd16fcc7a..36b569def89e1 100644 --- a/clang/test/AST/HLSL/Texture2D-vector-AST.hlsl +++ b/clang/test/AST/HLSL/Texture2D-vector-AST.hlsl @@ -470,6 +470,48 @@ // CHECK-NEXT: DeclRefExpr {{.*}} 'vector<int, 2>' lvalue ParmVar {{.*}} 'Offset' 'vector<int, 2>' // CHECK-NEXT: AlwaysInlineAttr +// CHECK: CXXMethodDecl {{.*}} CalculateLevelOfDetail 'float (hlsl::SamplerState, vector<float, 2>)' +// CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' +// CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' +// CHECK-NEXT: CompoundStmt +// CHECK-NEXT: ReturnStmt +// CHECK-NEXT: CStyleCastExpr {{.*}} 'float' <Dependent> +// CHECK-NEXT: CallExpr {{.*}} '<dependent type>' +// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_calculate_lod' 'void (...) noexcept' +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]] +// CHECK-SAME{LITERAL}: [[hlsl::contained_type(vector<element_type, element_count>)]] +// CHECK-SAME{LITERAL}: [[hlsl::resource_dimension(2D)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::Texture2D<vector<element_type, element_count>>' lvalue implicit this +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(Sampler)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::SamplerState' lvalue ParmVar {{.*}} 'Sampler' 'hlsl::SamplerState' +// CHECK-NEXT: DeclRefExpr {{.*}} 'vector<float, 2>' lvalue ParmVar {{.*}} 'Location' 'vector<float, 2>' +// CHECK-NEXT: AlwaysInlineAttr + +// CHECK: CXXMethodDecl {{.*}} CalculateLevelOfDetailUnclamped 'float (hlsl::SamplerState, vector<float, 2>)' +// CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' +// CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' +// CHECK-NEXT: CompoundStmt +// CHECK-NEXT: ReturnStmt +// CHECK-NEXT: CStyleCastExpr {{.*}} 'float' <Dependent> +// CHECK-NEXT: CallExpr {{.*}} '<dependent type>' +// CHECK-NEXT: DeclRefExpr {{.*}} '<builtin fn type>' Function {{.*}} '__builtin_hlsl_resource_calculate_lod_unclamped' 'void (...) noexcept' +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(SRV)]] +// CHECK-SAME{LITERAL}: [[hlsl::contained_type(vector<element_type, element_count>)]] +// CHECK-SAME{LITERAL}: [[hlsl::resource_dimension(2D)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: CXXThisExpr {{.*}} 'hlsl::Texture2D<vector<element_type, element_count>>' lvalue implicit this +// CHECK-NEXT: MemberExpr {{.*}} '__hlsl_resource_t +// CHECK-SAME{LITERAL}: [[hlsl::resource_class(Sampler)]] +// CHECK-SAME: ' lvalue .__handle +// CHECK-NEXT: DeclRefExpr {{.*}} 'hlsl::SamplerState' lvalue ParmVar {{.*}} 'Sampler' 'hlsl::SamplerState' +// CHECK-NEXT: DeclRefExpr {{.*}} 'vector<float, 2>' lvalue ParmVar {{.*}} 'Location' 'vector<float, 2>' +// CHECK-NEXT: AlwaysInlineAttr + // CHECK: CXXMethodDecl {{.*}} Gather 'vector<element_type, 4> (hlsl::SamplerState, vector<float, 2>)' inline // CHECK-NEXT: ParmVarDecl {{.*}} Sampler 'hlsl::SamplerState' // CHECK-NEXT: ParmVarDecl {{.*}} Location 'vector<float, 2>' @@ -773,5 +815,7 @@ void main(float2 loc, float cmp) { t.SampleCmp(scs, loc, cmp, int2(1, 2), 1.0f); t.SampleCmpLevelZero(scs, loc, cmp); t.SampleCmpLevelZero(scs, loc, cmp, int2(1, 2)); + t.CalculateLevelOfDetail(s, loc); + t.CalculateLevelOfDetailUnclamped(s, loc); t.Gather(s, loc); } diff --git a/clang/test/CodeGenHLSL/resources/Texture2D-CalculateLevelOfDetail.hlsl b/clang/test/CodeGenHLSL/resources/Texture2D-CalculateLevelOfDetail.hlsl new file mode 100644 index 0000000000000..f0c50f22fb818 --- /dev/null +++ b/clang/test/CodeGenHLSL/resources/Texture2D-CalculateLevelOfDetail.hlsl @@ -0,0 +1,44 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -x hlsl -emit-llvm -disable-llvm-passes -finclude-default-header -o - %s | llvm-cxxfilt | FileCheck %s --check-prefixes=CHECK,DXIL +// RUN: %clang_cc1 -triple spirv-vulkan-library -x hlsl -emit-llvm -disable-llvm-passes -finclude-default-header -o - %s | llvm-cxxfilt | FileCheck %s --check-prefixes=CHECK,SPIRV + +Texture2D t; +SamplerState s; + +// CHECK: define hidden {{.*}} float @test_lod(float vector[2])(<2 x float> {{.*}} %[[LOC:.*]]) +// CHECK: %[[CALL:.*]] = call {{.*}} float @hlsl::Texture2D<float vector[4]>::CalculateLevelOfDetail(hlsl::SamplerState, float vector[2])(ptr {{.*}} @t, ptr {{.*}} byval(%"class.hlsl::SamplerState") {{.*}}, <2 x float> {{.*}} %{{.*}}) +// CHECK: ret float %[[CALL]] + +float test_lod(float2 loc : LOC) : SV_Target { + return t.CalculateLevelOfDetail(s, loc); +} + +// CHECK: define linkonce_odr hidden {{.*}} float @hlsl::Texture2D<float vector[4]>::CalculateLevelOfDetail(hlsl::SamplerState, float vector[2])(ptr {{.*}} %[[THIS:[^,]+]], ptr {{.*}} byval(%"class.hlsl::SamplerState") {{.*}} %[[SAMPLER:[^,]+]], <2 x float> {{.*}} %[[COORD:[^)]+]]) +// CHECK: %[[THIS_VAL:.*]] = load ptr, ptr %[[THIS]] +// CHECK: %[[HANDLE_GEP:.*]] = getelementptr inbounds nuw %"class.hlsl::Texture2D", ptr %[[THIS_VAL]], i32 0, i32 0 +// CHECK: %[[HANDLE:.*]] = load target{{.*}}, ptr %[[HANDLE_GEP]] +// CHECK: %[[SAMPLER_GEP:.*]] = getelementptr inbounds nuw %"class.hlsl::SamplerState", ptr %[[SAMPLER]], i32 0, i32 0 +// CHECK: %[[SAMPLER_H:.*]] = load target{{.*}}, ptr %[[SAMPLER_GEP]] +// CHECK: %[[COORD_VAL:.*]] = load <2 x float>, ptr %[[COORD]] +// DXIL: %[[RES:.*]] = call {{.*}} float @llvm.dx.resource.calculate.lod.tdx.Texture_v4f32_0_0_0_2t.tdx.Sampler_0t.v2f32(target("dx.Texture", <4 x float>, 0, 0, 0, 2) %[[HANDLE]], target("dx.Sampler", 0) %[[SAMPLER_H]], <2 x float> %[[COORD_VAL]]) +// SPIRV: %[[RES:.*]] = call {{.*}} float @llvm.spv.resource.calculate.lod.f32.tspirv.Image_f32_1_2_0_0_1_0t.tspirv.Samplert.v2f32(target("spirv.Image", float, 1, 2, 0, 0, 1, 0) %[[HANDLE]], target("spirv.Sampler") %[[SAMPLER_H]], <2 x float> %[[COORD_VAL]]) +// CHECK: ret float %[[RES]] + +// CHECK: define hidden {{.*}} float @test_lod_unclamped(float vector[2])(<2 x float> noundef nofpclass(nan inf) %[[LOC:.*]]) +// CHECK: %[[LOC_VAL:.*]] = load <2 x float>, ptr {{.*}} +// CHECK: %[[CALL:.*]] = call {{.*}} float @hlsl::Texture2D<float vector[4]>::CalculateLevelOfDetailUnclamped(hlsl::SamplerState, float vector[2])(ptr {{.*}} @t, ptr {{.*}} byval(%"class.hlsl::SamplerState") {{.*}}, <2 x float> {{.*}} %[[LOC_VAL]]) +// CHECK: ret float %[[CALL]] + +float test_lod_unclamped(float2 loc : LOC) : SV_Target { + return t.CalculateLevelOfDetailUnclamped(s, loc); +} + +// CHECK: define linkonce_odr hidden {{.*}} float @hlsl::Texture2D<float vector[4]>::CalculateLevelOfDetailUnclamped(hlsl::SamplerState, float vector[2])(ptr {{.*}} %[[THIS:[^,]+]], ptr {{.*}} byval(%"class.hlsl::SamplerState") {{.*}} %[[SAMPLER:[^,]+]], <2 x float> {{.*}} %[[COORD:[^)]+]]) +// CHECK: %[[THIS_VAL:.*]] = load ptr, ptr %[[THIS]] +// CHECK: %[[HANDLE_GEP:.*]] = getelementptr inbounds nuw %"class.hlsl::Texture2D", ptr %[[THIS_VAL]], i32 0, i32 0 +// CHECK: %[[HANDLE:.*]] = load target{{.*}}, ptr %[[HANDLE_GEP]] +// CHECK: %[[SAMPLER_GEP:.*]] = getelementptr inbounds nuw %"class.hlsl::SamplerState", ptr %[[SAMPLER]], i32 0, i32 0 +// CHECK: %[[SAMPLER_H:.*]] = load target{{.*}}, ptr %[[SAMPLER_GEP]] +// CHECK: %[[COORD_VAL:.*]] = load <2 x float>, ptr %[[COORD]] +// DXIL: %[[RES:.*]] = call {{.*}} float @llvm.dx.resource.calculate.lod.unclamped.tdx.Texture_v4f32_0_0_0_2t.tdx.Sampler_0t.v2f32(target("dx.Texture", <4 x float>, 0, 0, 0, 2) %[[HANDLE]], target("dx.Sampler", 0) %[[SAMPLER_H]], <2 x float> %[[COORD_VAL]]) +// SPIRV: %[[RES:.*]] = call {{.*}} float @llvm.spv.resource.calculate.lod.unclamped.f32.tspirv.Image_f32_1_2_0_0_1_0t.tspirv.Samplert.v2f32(target("spirv.Image", float, 1, 2, 0, 0, 1, 0) %[[HANDLE]], target("spirv.Sampler") %[[SAMPLER_H]], <2 x float> %[[COORD_VAL]]) +// CHECK: ret float %[[RES]] diff --git a/clang/test/SemaHLSL/Resources/Texture2D-CalculateLevelOfDetail.hlsl b/clang/test/SemaHLSL/Resources/Texture2D-CalculateLevelOfDetail.hlsl new file mode 100644 index 0000000000000..94412a289bb4a --- /dev/null +++ b/clang/test/SemaHLSL/Resources/Texture2D-CalculateLevelOfDetail.hlsl @@ -0,0 +1,33 @@ +// RUN: %clang_cc1 -triple dxil-pc-shadermodel6.0-library -finclude-default-header -fsyntax-only -verify %s + +Texture2D<float4> tex; +SamplerState samp; + +void main() { + float2 loc = float2(0, 0); + + tex.CalculateLevelOfDetail(samp, loc); + tex.CalculateLevelOfDetailUnclamped(samp, loc); + + // expected-error@+2 {{too few arguments to function call, expected 2, have 1}} + // expected-note@* {{'CalculateLevelOfDetail' declared here}} + tex.CalculateLevelOfDetail(samp); + + // expected-error@+2 {{too few arguments to function call, expected 2, have 1}} + // expected-note@* {{'CalculateLevelOfDetailUnclamped' declared here}} + tex.CalculateLevelOfDetailUnclamped(samp); + + // expected-error@+2 {{too many arguments to function call, expected 2, have 3}} + // expected-note@* {{'CalculateLevelOfDetail' declared here}} + tex.CalculateLevelOfDetail(samp, loc, 0); + + // expected-error@+2{{too many arguments to function call, expected 2, have 3}} + // expected-note@* {{'CalculateLevelOfDetailUnclamped' declared here}} + tex.CalculateLevelOfDetailUnclamped(samp, loc, 0); + + // expected-error@+1 {{cannot initialize a parameter of type 'vector<float, 2>' (vector of 2 'float' values) with an lvalue of type 'const char[8]'}} + tex.CalculateLevelOfDetail(samp, "invalid"); + + // expected-error@+1 {{cannot initialize a parameter of type 'vector<float, 2>' (vector of 2 'float' values) with an lvalue of type 'const char[8]'}} + tex.CalculateLevelOfDetailUnclamped(samp, "invalid"); +} diff --git a/llvm/include/llvm/IR/IntrinsicsDirectX.td b/llvm/include/llvm/IR/IntrinsicsDirectX.td index 86d1e1f045c14..8350922193338 100644 --- a/llvm/include/llvm/IR/IntrinsicsDirectX.td +++ b/llvm/include/llvm/IR/IntrinsicsDirectX.td @@ -126,6 +126,16 @@ def int_dx_resource_load_level llvm_any_ty], [IntrReadMem]>; +def int_dx_resource_calculate_lod + : DefaultAttrsIntrinsic<[llvm_float_ty], + [llvm_any_ty, llvm_any_ty, llvm_any_ty], + [IntrReadMem]>; + +def int_dx_resource_calculate_lod_unclamped + : DefaultAttrsIntrinsic<[llvm_float_ty], + [llvm_any_ty, llvm_any_ty, llvm_any_ty], + [IntrReadMem]>; + def int_dx_resource_samplecmp : DefaultAttrsIntrinsic<[llvm_any_ty], [llvm_any_ty, llvm_any_ty, llvm_any_ty, _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
