<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/99092>99092</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Implement the `D3DCOLORtoUBYTE4` HLSL Function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            metabug,
            HLSL,
            backend:SPIR-V,
            bot:HLSL
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          farzonl
      </td>
    </tr>
</table>

<pre>
    - [ ] Implement `D3DCOLORtoUBYTE4` clang builtin,
- [ ] Link `D3DCOLORtoUBYTE4` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `D3DCOLORtoUBYTE4` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `D3DCOLORtoUBYTE4` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/D3DCOLORtoUBYTE4.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/D3DCOLORtoUBYTE4-errors.hlsl`
- [ ] Create the `int_spv_D3DCOLORtoUBYTE4` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `D3DCOLORtoUBYTE4` lowering and map it to `int_spv_D3DCOLORtoUBYTE4` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/D3DCOLORtoUBYTE4.ll`

## DirectX

There were no DXIL opcodes found for `D3DCOLORtoUBYTE4`.

## SPIR-V

# [OpConvertFToS](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#OpConvertFToS):

## Description:
  
Convert value numerically from floating point to signed integer, with
round toward 0.0.  
  
*Result Type* must be a scalar or vector of [*integer type*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Integer).
Behavior is undefined if *Result Type* is not wide enough to hold the
converted value.  
  
*Float Value* must be a scalar or vector of [*floating-point
type*](https://registry.khronos.org/SPIR-V/specs/unified1/SPIRV.html#Floating). It must have the same number of components as *Result
Type*.  
  
Results are computed per component.

<table>
<colgroup>
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
<col style="width: 20%" />
</colgroup>
<thead>
<tr>
<th>Word Count</th>
<th>Opcode</th>
<th>Results</th>
<th>Operands</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p>4</p></td>
<td class="tableblock halign-left valign-top"><p>110</p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Result Type</em></p></td>
<td class="tableblock halign-left valign-top"><p><a
href="#ResultId"><em>Result &lt;id&gt;</em></a></p></td>
<td
class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br />
<em>Float Value</em></p></td>
</tr>
</tbody>
</table>



## Test Case(s)

 
 ### Example 1
```hlsl
//dxc D3DCOLORtoUBYTE4_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export int4 fn(float4 p1) {
    return D3DCOLORtoUBYTE4(p1);
}
```
## HLSL:

Converts a floating-point, 4D vector set by a D3DCOLOR to a UBYTE4.



| *ret* D3DCOLORtoUBYTE4(*x*) |
|-----------------------------|



 

This function swizzles and scales components of the *x* parameter. Use this function to compensate for the lack of UBYTE4 support in some hardware.

## Parameters



| Item | Description |
|--------------------------------------------------------|----------------------------------------------------------|
| <span id="x"></span><span id="X"></span>*x*<br/> | \[in\] The floating-point vector4 to convert.<br/> |



 

## Return Value

The UBYTE4 representation of the *x* parameter.

## Type Description



| Name  | [**Template Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**Component Type**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | Size |
|-------|-------------------------------------------------------------------------------------|----------------------------------------------------------------|------|
| *x*   | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**float**](/windows/desktop/WinProg/windows-data-types) | 4    |
| *ret* | [**vector**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md) | [**integer**](/windows/desktop/WinProg/windows-data-types) | 4    |



 

## Minimum Shader Model

This function is supported in the following shader models.



| Shader Model | Supported |
|------------------------------------------------------------------------------------|-----------|
| [Shader Model 2 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm2.md) and higher shader models | yes       |
| [Shader Model 1 (DirectX HLSL)](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-sm1.md) | vs\_1\_1  |



 

## See also

<dl> <dt>

[**Intrinsic Functions (DirectX HLSL)**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUWdtvGjkX_2uclyPQ4AESHnggQ9gvUvu1arLdfk-RGR8Yf_HYI9sTkv71q2MPt4Sk1W6r7SIEjC_n8jtXG-G9WhvEKRtdstH8TLShsm66Eu6rNfpsaeXTtAdsdAlsNIfrutFYownAxtk8nxcf3n34FOzvl_-7vRqycQalFmYNy1bpoAzjBctm-93vlLn_ro2wUaGilZX2-k6Z4JTxqvT9io2zI5IzKcFjLaCssLz3sLLuNQ7B0kxB6_7z7ubdZWK1aE0ZlDWF0JpWKUOrbrAWcaUy637ZNKfYllbiGs23WV7VKhxwvHps3J5T8Vs3_C02AX3wHcUIFuMLGmN8UViJv6EhHowvOgw944vnMvUJz1chfIMD4dGRj-JenyTfQ-es8ye5F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