Skip to content

Commit 5610b22

Browse files
committed
[HLSL] Implement the D3DCOLORtoUBYTE4 intrinsic
1 parent a6aa936 commit 5610b22

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

clang/lib/Headers/hlsl/hlsl_detail.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ constexpr enable_if_t<sizeof(U) == sizeof(T), U> bit_cast(T F) {
3333
return __builtin_bit_cast(U, F);
3434
}
3535

36+
constexpr vector<uint, 4> d3d_color_to_ubyte4(vector<float, 4> V) {
37+
// Use the same scaling factor used by FXC (i.e., 255.001953)
38+
// Excerpt from stackoverflow discussion:
39+
// "Built-in rounding, necessary because of truncation. 0.001953 * 256 = 0.5"
40+
// https://stackoverflow.com/questions/52103720/why-does-d3dcolortoubyte4-multiplies-components-by-255-001953f
41+
return V.zyxw * 255.001953f;
42+
}
43+
3644
} // namespace __detail
3745
} // namespace hlsl
3846
#endif //_HLSL_HLSL_DETAILS_H_

clang/lib/Headers/hlsl/hlsl_intrinsics.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,23 @@ half3 cross(half3, half3);
18571857
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross)
18581858
float3 cross(float3, float3);
18591859

1860+
//===----------------------------------------------------------------------===//
1861+
// D3DCOLORtoUBYTE4 builtins
1862+
//===----------------------------------------------------------------------===//
1863+
1864+
/// \fn T D3DCOLORtoUBYTE4(T x)
1865+
/// \brief Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.
1866+
/// \param x [in] The floating-point vector4 to convert.
1867+
///
1868+
/// The return value is the UBYTE4 representation of the \a x parameter.
1869+
///
1870+
/// This function swizzles and scales components of the \a x parameter. Use this
1871+
/// function to compensate for the lack of UBYTE4 support in some hardware.
1872+
1873+
constexpr vector<uint, 4> D3DCOLORtoUBYTE4(vector<float, 4> V) {
1874+
return __detail::d3d_color_to_ubyte4(V);
1875+
}
1876+
18601877
//===----------------------------------------------------------------------===//
18611878
// rcp builtins
18621879
//===----------------------------------------------------------------------===//
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple \
2+
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \
3+
// RUN: -emit-llvm -O1 -o - | FileCheck %s --check-prefixes=CHECK
4+
5+
// CHECK-LABEL: D3DCOLORtoUBYTE4
6+
int4 test_D3DCOLORtoUBYTE4(float4 p1) {
7+
// CHECK: %[[SCALED:.*]] = fmul [[FMFLAGS:.*]]<4 x float> %{{.*}}, splat (float 0x406FE01000000000)
8+
// CHECK: %[[CONVERTED:.*]] = fptoui <4 x float> %[[SCALED]] to <4 x i32>
9+
// CHECK: %[[SHUFFLED:.*]] = shufflevector <4 x i32> %{{.*}}, <4 x i32> poison, <4 x i32> <i32 2, i32 1, i32 0, i32 3>
10+
// CHECK: ret <4 x i32> %[[SHUFFLED]]
11+
return D3DCOLORtoUBYTE4(p1);
12+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// RUN: %clang_cc1 -finclude-default-header -triple dxil-pc-shadermodel6.6-library %s -emit-llvm-only -disable-llvm-passes -verify
2+
3+
int4 test_too_few_arg() {
4+
return D3DCOLORtoUBYTE4();
5+
// expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}}
6+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires single argument 'V', but no arguments were provided}}
7+
}
8+
9+
int4 test_too_many_arg(float4 v) {
10+
return D3DCOLORtoUBYTE4(v, v);
11+
// expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}}
12+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: requires single argument 'V', but 2 arguments were provided}}
13+
}
14+
15+
int4 float2_arg(float2 v) {
16+
return D3DCOLORtoUBYTE4(v);
17+
// expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}}
18+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'vector<[...], 2>' to 'vector<[...], 4>' for 1st argument}}
19+
}
20+
21+
struct S {
22+
float4 f;
23+
};
24+
25+
int4 struct_arg(S v) {
26+
return D3DCOLORtoUBYTE4(v);
27+
// expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}}
28+
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'S' to 'vector<float, 4>' (vector of 4 'float' values) for 1st argument}}
29+
}

0 commit comments

Comments
 (0)