-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[HLSL] Implement D3DCOLORtoUBYTE4 intrinsic #122202
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
Conversation
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-hlsl @llvm/pr-subscribers-clang Author: Deric Cheung (Icohedron) ChangesFixes #99092.
Full diff: https://github.com/llvm/llvm-project/pull/122202.diff 4 Files Affected:
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}}
+}
|
// 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation of D3DCOLORtoUBYTE4
scales the vector by 255.001953
. This matches the way it was done in DXC.
However, it differs from the SPIRV custom implementation which scales the vector by 255.002
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the change from DXC for SPIR-V is fine. The DXIL behaviour is the correct behaviour.
5159990
to
a4bbeda
Compare
a4bbeda
to
25bf1be
Compare
25bf1be
to
5610b22
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great job, LGTM. Just a couple personal preference nits
@@ -1857,6 +1857,23 @@ half3 cross(half3, half3); | |||
_HLSL_BUILTIN_ALIAS(__builtin_hlsl_cross) | |||
float3 cross(float3, float3); | |||
|
|||
//===----------------------------------------------------------------------===// | |||
// D3DCOLORtoUBYTE4 builtins |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// D3DCOLORtoUBYTE4 builtins | |
// D3DCOLORtoUBYTE4 builtin |
clang/lib/Headers/hlsl/hlsl_detail.h
Outdated
@@ -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) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constexpr vector<uint, 4> d3d_color_to_ubyte4(vector<float, 4> V) { | |
constexpr vector<uint, 4> D3DColorToUByte4Impl(vector<float, 4> V) { |
I think this follows more to how the length builtin was defined
@@ -0,0 +1,12 @@ | |||
// RUN: %clang_cc1 -finclude-default-header -triple \ | |||
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// RUN: dxil-pc-shadermodel6.3-library %s -fnative-half-type \ | |
// RUN: dxil-pc-shadermodel6.3-library %s \ |
|
||
// CHECK-LABEL: D3DCOLORtoUBYTE4 | ||
int4 test_D3DCOLORtoUBYTE4(float4 p1) { | ||
// CHECK: %[[SCALED:.*]] = fmul [[FMFLAGS:.*]]<4 x float> %{{.*}}, splat (float 0x406FE01000000000) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
// CHECK: %[[SCALED:.*]] = fmul [[FMFLAGS:.*]]<4 x float> %{{.*}}, splat (float 0x406FE01000000000) | |
// CHECK: %[[SCALED:.*]] = fmul [[FMFLAGS:.*]] [[FLOAT_TYPE:<4 x float>]] %{{.*}}, splat (float 0x406FE01000000000) |
Imo pattern capturing makes it slightly easier to read and quickly know which types are the same
// 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; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the change from DXC for SPIR-V is fine. The DXIL behaviour is the correct behaviour.
clang/lib/Headers/hlsl/hlsl_detail.h
Outdated
// 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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be better to refer to the implementation in DXC here, rather than copying the comment in DXC about compatibility with FXC.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I revised the comment to mention that the scaling factor is the same as FXC and also the DXC DXIL implementation.
I also kept the link to the stackoverflow discussion, as the comments in the DXC DXIL implementation referred to it but did not provide a link, which I think is kind of important.
Are these new comments more suitable? Or should I just simply just provide a link to the DXC implementation and cut out the repetition?
@Icohedron Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
Fixes #99092.
D3DCOLORtoUBYTE4
inclang/lib/Headers/hlsl/hlsl_intrinsics.h
.D3DCOLORtoUBYTE4
asd3d_color_to_ubyte4
inclang/lib/Headers/hlsl/hlsl_detail.h
clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl
clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl