https://github.com/Icohedron created https://github.com/llvm/llvm-project/pull/122202
Fixes #99092. 1. Defines the function `D3DCOLORtoUBYTE4` in `clang/lib/Headers/hlsl/hlsl_intrinsics.h`. 2. Implements the function `D3DCOLORtoUBYTE4` as `d3d_color_to_ubyte4` in `clang/lib/Headers/hlsl/hlsl_detail.h` 3. Adds a HLSL codegen test to `clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl` 4. Adds error tests to `clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl` >From 0ef5e0217c04aa16c2f54b5f0edf0a6dc4cdc28d Mon Sep 17 00:00:00 2001 From: Icohedron <cheung.de...@gmail.com> Date: Thu, 9 Jan 2025 01:14:52 +0000 Subject: [PATCH] Implement D3DCOLORtoUBYTE4 intrinsic --- clang/lib/Headers/hlsl/hlsl_detail.h | 8 ++++++++ clang/lib/Headers/hlsl/hlsl_intrinsics.h | 17 +++++++++++++++++ .../CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl | 12 ++++++++++++ .../BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl | 13 +++++++++++++ 4 files changed, 50 insertions(+) create mode 100644 clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl create mode 100644 clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl diff --git a/clang/lib/Headers/hlsl/hlsl_detail.h b/clang/lib/Headers/hlsl/hlsl_detail.h index 8d5fd941331531..470fa4214a12f8 100644 --- a/clang/lib/Headers/hlsl/hlsl_detail.h +++ b/clang/lib/Headers/hlsl/hlsl_detail.h @@ -33,6 +33,14 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) { return __builtin_bit_cast(U, F); } +constexpr vector<uint, 4> d3d_color_to_ubyte4(vector<float, 4> V) { + // Use the same scaling factor used by FXC (i.e., 255.001953) + // Excerpt from stackoverflow discussion: + // "Built-in rounding, necessary because of truncation. 0.001953 * 256 = 0.5" + // https://stackoverflow.com/questions/52103720/why-does-d3dcolortoubyte4-multiplies-components-by-255-001953f + return V.zyxw * 255.001953f; +} + } // namespace __detail } // namespace hlsl #endif //_HLSL_HLSL_DETAILS_H_ diff --git a/clang/lib/Headers/hlsl/hlsl_intrinsics.h b/clang/lib/Headers/hlsl/hlsl_intrinsics.h index b745997f1d5a2b..e44403c6c802e0 100644 --- a/clang/lib/Headers/hlsl/hlsl_intrinsics.h +++ b/clang/lib/Headers/hlsl/hlsl_intrinsics.h @@ -1857,6 +1857,23 @@ half3 cross(half3, half3); _HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross) float3 cross(float3, float3); +//===----------------------------------------------------------------------===// +// D3DCOLORtoUBYTE4 builtins +//===----------------------------------------------------------------------===// + +/// \fn T D3DCOLORtoUBYTE4(T x) +/// \brief Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4. +/// \param x [in] The floating-point vector4 to convert. +/// +/// The return value is the UBYTE4 representation of the \a x parameter. +/// +/// This function swizzles and scales components of the \a x parameter. Use this +/// function to compensate for the lack of UBYTE4 support in some hardware. + +constexpr vector<uint, 4> D3DCOLORtoUBYTE4(vector<float, 4> V) { + return __detail::d3d_color_to_ubyte4(V); +} + //===----------------------------------------------------------------------===// // rcp builtins //===----------------------------------------------------------------------===// diff --git a/clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl b/clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl new file mode 100644 index 00000000000000..6e48800dae6698 --- /dev/null +++ b/clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -finclude-default-header -triple \ +// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ +// RUN: -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK + +// CHECK-LABEL: D3DCOLORtoUBYTE4 +int4 test_D3DCOLORtoUBYTE4 ( float4 p1 ) { + // CHECK: %[[SCALED:.*]] = fmul <4 x float> %{{.*}}, splat (float 0x406FE01000000000) + // CHECK: %[[CONVERTED:.*]] = fptoui <4 x float> %[[SCALED]] to <4 x i32> + // CHECK: %[[SHUFFLED:.*]] = shufflevector <4 x i32> %{{.*}}, <4 x i32> poison, <4 x i32> <i32 2, i32 1, i32 0, i32 3> + // CHECK: ret <4 x i32> %[[SHUFFLED]] + return D3DCOLORtoUBYTE4 ( p1 ); +} diff --git a/clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl b/clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl new file mode 100644 index 00000000000000..fb41c212bd0be3 --- /dev/null +++ b/clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl @@ -0,0 +1,13 @@ +// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify + +int4 test_too_few_arg() { + return D3DCOLORtoUBYTE4(); + // expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires single argument 'V', but no arguments were provided}} +} + +int4 test_too_many_arg(float4 v) { + return D3DCOLORtoUBYTE4(v, v); + // expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}} + // expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires single argument 'V', but 2 arguments were provided}} +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits