Skip to content

[HLSL] fix D3DCOLORtoUBYTE4 return type to be int #151353

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 3 commits into from
Jul 31, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion clang/lib/Headers/hlsl/hlsl_intrinsic_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace hlsl {
namespace __detail {

constexpr vector<uint, 4> d3d_color_to_ubyte4_impl(vector<float, 4> V) {
constexpr int4 d3d_color_to_ubyte4_impl(float4 V) {
// Use the same scaling factor used by FXC, and DXC for DXIL
// (i.e., 255.001953)
// https://github.com/microsoft/DirectXShaderCompiler/blob/070d0d5a2beacef9eeb51037a9b04665716fd6f3/lib/HLSL/HLOperationLower.cpp#L666C1-L697C2
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Headers/hlsl/hlsl_intrinsics.h
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ const inline float4 lit(float NDotL, float NDotH, float M) {
/// 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) {
constexpr int4 D3DCOLORtoUBYTE4(float4 V) {
return __detail::d3d_color_to_ubyte4_impl(V);
}

Expand Down
10 changes: 9 additions & 1 deletion clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
// CHECK-LABEL: D3DCOLORtoUBYTE4
int4 test_D3DCOLORtoUBYTE4(float4 p1) {
// CHECK: %[[SCALED:.*]] = fmul [[FMFLAGS:.*]][[FLOAT_TYPE:<4 x float>]] %{{.*}}, splat (float 0x406FE01000000000)
// CHECK: %[[CONVERTED:.*]] = fptoui [[FLOAT_TYPE]] %[[SCALED]] to [[INT_TYPE:<4 x i32>]]
// CHECK: %[[CONVERTED:.*]] = fptosi [[FLOAT_TYPE]] %[[SCALED]] to [[INT_TYPE:<4 x i32>]]
// CHECK: %[[SHUFFLED:.*]] = shufflevector [[INT_TYPE]] %[[CONVERTED]], [[INT_TYPE]] poison, <4 x i32> <i32 2, i32 1, i32 0, i32 3>
// CHECK: ret [[INT_TYPE]] %[[SHUFFLED]]
return D3DCOLORtoUBYTE4(p1);
}

// Note this test confirms issue 150673 is fixed
// by confirming the negative does not become a poison
// CHECK-LABEL: test_constant_inputs
int4 test_constant_inputs() {
// CHECK: ret <4 x i32> <i32 -12877, i32 2833, i32 0, i32 25500>
return D3DCOLORtoUBYTE4(float4(0, 11.11, -50.5, 100));
}
2 changes: 1 addition & 1 deletion clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ struct S {
int4 struct_arg(S v) {
return D3DCOLORtoUBYTE4(v);
// expected-error@-1 {{no matching function for call to 'D3DCOLORtoUBYTE4'}}
// 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}}
// expected-note@hlsl/hlsl_intrinsics.h:* {{candidate function not viable: no known conversion from 'S' to 'float4' (aka 'vector<float, 4>') for 1st argument}}
}