Skip to content

[HLSL] Implement the lit intrinsic #134171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,18 @@ constexpr vector<T, N> smoothstep_vec_impl(vector<T, N> Min, vector<T, N> Max,
#endif
}

template <typename T> constexpr vector<T, 4> lit_impl(T NDotL, T NDotH, T M) {
bool DiffuseCond = NDotL < 0;
T Diffuse = select<T>(DiffuseCond, 0, NDotL);
vector<T, 4> Result = {1, Diffuse, 0, 1};
// clang-format off
bool SpecularCond = or(DiffuseCond, (NDotH < 0));
// clang-format on
T SpecularExp = exp(log(NDotH) * M);
Result[2] = select<T>(SpecularCond, 0, SpecularExp);
return Result;
}

} // namespace __detail
} // namespace hlsl

Expand Down
24 changes: 24 additions & 0 deletions clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,30 @@ const inline float length(__detail::HLSL_FIXED_VECTOR<float, N> X) {
return __detail::length_vec_impl(X);
}

//===----------------------------------------------------------------------===//
// lit builtins
//===----------------------------------------------------------------------===//

/// \fn vector<T, 4> lit(T NDotL, T NDotH, T M)
/// \brief Returns a lighting coefficient vector.
/// \param NDotL The dot product of the normalized surface normal and the
/// light vector.
/// \param NDotH The dot product of the half-angle vector and the surface
/// normal.
/// \param M A specular exponent.
///
/// This function returns a lighting coefficient vector (ambient, diffuse,
/// specular, 1).

_HLSL_16BIT_AVAILABILITY(shadermodel, 6.2)
const inline half4 lit(half NDotL, half NDotH, half M) {
return __detail::lit_impl(NDotL, NDotH, M);
}

const inline float4 lit(float NDotL, float NDotH, float M) {
return __detail::lit_impl(NDotL, NDotH, M);
}

//===----------------------------------------------------------------------===//
// D3DCOLORtoUBYTE4 builtin
//===----------------------------------------------------------------------===//
Expand Down
34 changes: 34 additions & 0 deletions clang/test/CodeGenHLSL/builtins/lit.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 5
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -O1 -o - | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't do -01 here. Drop the check-next and just check for the instructions you are expecting like
select, or, exp, log, and select again

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for future reference, when do we want to use -O1 and/or -disable-llvm-passes in tests? I haven't been able to figure out a consistent rule looking through the other codegen tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally you should prefer -disable-llvm-passes since it makes the test run faster


// CHECK-LABEL: test_lit_half
// CHECK-SAME: half noundef nofpclass(nan inf) [[NDOTL:%.*]], half noundef nofpclass(nan inf) [[NDOTH:%.*]], half noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0:[0-9]+]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[NDOTL]], 0xH0000
// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.maxnum.f16(half [[NDOTL]], half 0xH0000)
// CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x half> <half 0xH3C00, half poison, half poison, half 0xH3C00>, half [[HLSL_SELECT_I]], i64 1
// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt half [[NDOTH]], 0xH0000
// CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]]
// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.log.f16(half [[NDOTH]])
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn half [[ELT_LOG_I]], [[M]]
// CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn half @llvm.exp.f16(half [[MUL_I]])
// CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], half 0xH0000, half [[ELT_EXP_I]]
// CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x half> [[VECINIT2_I]], half [[HLSL_SELECT7_I]], i64 2
// CHECK-NEXT: ret <4 x half> [[VECINS_I]]
half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M); }

// CHECK-LABEL: test_lit_float
// CHECK-SAME: float noundef nofpclass(nan inf) [[NDOTL:%.*]], float noundef nofpclass(nan inf) [[NDOTH:%.*]], float noundef nofpclass(nan inf) [[M:%.*]]) local_unnamed_addr #[[ATTR0]] {
// CHECK-NEXT: [[ENTRY:.*:]]
// CHECK-NEXT: [[CMP_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[NDOTL]], 0.000000e+00
// CHECK-NEXT: [[HLSL_SELECT_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.maxnum.f32(float [[NDOTL]], float 0.000000e+00)
// CHECK-NEXT: [[VECINIT2_I:%.*]] = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float 1.000000e+00>, float [[HLSL_SELECT_I]], i64 1
// CHECK-NEXT: [[CMP4_I:%.*]] = fcmp reassoc nnan ninf nsz arcp afn olt float [[NDOTH]], 0.000000e+00
// CHECK-NEXT: [[HLSL_OR_I:%.*]] = or i1 [[CMP_I]], [[CMP4_I]]
// CHECK-NEXT: [[ELT_LOG_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float [[NDOTH]])
// CHECK-NEXT: [[MUL_I:%.*]] = fmul reassoc nnan ninf nsz arcp afn float [[ELT_LOG_I]], [[M]]
// CHECK-NEXT: [[ELT_EXP_I:%.*]] = tail call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float [[MUL_I]])
// CHECK-NEXT: [[HLSL_SELECT7_I:%.*]] = select reassoc nnan ninf nsz arcp afn i1 [[HLSL_OR_I]], float 0.000000e+00, float [[ELT_EXP_I]]
// CHECK-NEXT: [[VECINS_I:%.*]] = insertelement <4 x float> [[VECINIT2_I]], float [[HLSL_SELECT7_I]], i64 2
// CHECK-NEXT: ret <4 x float> [[VECINS_I]]
float4 test_lit_float(float NDotL, float NDotH, float M) { return lit(NDotL, NDotH, M); }
16 changes: 16 additions & 0 deletions clang/test/SemaHLSL/BuiltIns/lit-errors.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.6-library %s -fnative-half-type -emit-llvm-only -disable-llvm-passes -verify -verify-ignore-unexpected=note

float4 test_double_inputs(double p0, double p1, double p2) {
return lit(p0, p1, p2);
// expected-error@-1 {{call to 'lit' is ambiguous}}
}

float4 test_int_inputs(int p0, int p1, int p2) {
return lit(p0, p1, p2);
// expected-error@-1 {{call to 'lit' is ambiguous}}
}

float4 test_bool_inputs(bool p0, bool p1, bool p2) {
return lit(p0, p1, p2);
// expected-error@-1 {{call to 'lit' is ambiguous}}
}