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
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
31 changes: 31 additions & 0 deletions clang/test/CodeGenHLSL/builtins/lit.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// RUN: %clang_cc1 -finclude-default-header -x hlsl -triple dxil-pc-shadermodel6.3-library %s -fnative-half-type -emit-llvm -o - | FileCheck %s

// CHECK-LABEL: test_lit_half
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt half %{{.*}}, 0xH0000
// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, half 0xH0000, half %{{.*}}
// CHECK: %vecinit.i = insertelement <4 x half> <half 0xH3C00, half poison, half poison, half poison>, half %{{.*}}, i32 1
// CHECK: %vecinit2.i = insertelement <4 x half> %{{.*}}, half 0xH3C00, i32 3
// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt half %{{.*}}, 0xH0000
// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i
// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn half @llvm.log.f16(half %{{.*}})
// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn half %elt.log.i, %{{.*}}
// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn half @llvm.exp.f16(half %mul.i)
// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, half 0xH0000, half %{{.*}}
// CHECK: %vecins.i = insertelement <4 x half> %{{.*}}, half %hlsl.select7.i, i32 2
// CHECK: ret <4 x half> %{{.*}}
half4 test_lit_half(half NDotL, half NDotH, half M) { return lit(NDotL, NDotH, M); }

// CHECK-LABEL: test_lit_float
// CHECK: %cmp.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00
// CHECK: %hlsl.select.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}}
// CHECK: %vecinit.i = insertelement <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, float %{{.*}}, i32 1
// CHECK: %vecinit2.i = insertelement <4 x float> %{{.*}}, float 1.000000e+00, i32 3
// CHECK: %cmp4.i = fcmp reassoc nnan ninf nsz arcp afn olt float %{{.*}}, 0.000000e+00
// CHECK: %hlsl.or.i = or i1 %{{.*}}, %cmp4.i
// CHECK: %elt.log.i = call reassoc nnan ninf nsz arcp afn float @llvm.log.f32(float %{{.*}})
// CHECK: %mul.i = fmul reassoc nnan ninf nsz arcp afn float %elt.log.i, %{{.*}}
// CHECK: %elt.exp.i = call reassoc nnan ninf nsz arcp afn float @llvm.exp.f32(float %mul.i)
// CHECK: %hlsl.select7.i = select reassoc nnan ninf nsz arcp afn i1 %{{.*}}, float 0.000000e+00, float %{{.*}}
// CHECK: %vecins.i = insertelement <4 x float> %{{.*}}, float %hlsl.select7.i, i32 2
// CHECK: ret <4 x float> %{{.*}}
float4 test_lit_float(float NDotL, float NDotH, float M) { return lit(NDotL, NDotH, M); }
Loading