llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.91k stars 11.52k forks source link

Implement the `f32tof16` HLSL Function #99113

Open farzonl opened 1 month ago

farzonl commented 1 month ago

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
130 LegacyF32ToF16 6.0 ()

SPIR-V

PackHalf2x16:

Description:

PackHalf2x16

Result is the unsigned integer obtained by converting the components of a two-component floating-point vector to the 16-bit OpTypeFloat, and then packing these two 16-bit integers into a 32-bit unsigned integer. The first vector component specifies the 16 least-significant bits of the result; the second component specifies the 16 most-significant bits.

The v operand must be a vector of 2 components whose type is a 32-bit floating-point.

Result Type must be a 32-bit integer type.

Number Operand 1 Operand 2 Operand 3 Operand 4

58

<id>
v

Test Case(s)

Example 1

//dxc f32tof16_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export uint4 fn(float4 p1) {
    return f32tof16(p1);
}

HLSL:

Converts an input into a float16 type.

Syntax

uint f32tof16(
  in float value
);

Parameters

*value* \[in\]
Type: **float** The input value.

Return value

Type: uint

The converted value, stored in the low-half of the uint.

Remarks

The following overloaded versions are also available:

uint2 f32tof16(float2 value);
uint3 f32tof16(float3 value);
uint4 f32tof16(float4 value);

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 4 and higher shader models yes

 

Although this function is available in Shader Model 4 and higher shader models, because it is emulated in 4.0 and 4.1, it is less perfomant on these shader models than it is on Shader Model 5.

This function is supported in the following types of shaders:

Vertex Hull Domain Geometry Pixel Compute
x x x x x x

 

See also

[Intrinsic Functions](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/dx-graphics-hlsl-intrinsic-functions.md)
[Shader Model 5](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/d3d11-graphics-reference-sm5.md)
farzonl commented 1 month ago

@damyanp Adding refinment priority. This is used in DirectML shaders PixelPacking_Velocity.hlsli