llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Implement the `smoothstep` HLSL Function

Open farzonl opened this issue 2 years ago • 3 comments

  • [ ] Implement smoothstep using HLSL source in hlsl_intrinsics.h
  • [ ] Implement the smoothstep SPIR-V target built-in in clang/include/clang/Basic/BuiltinsSPIRV.td
  • [ ] Add sema checks for smoothstep to CheckSPIRVBuiltinFunctionCall in clang/lib/Sema/SemaSPIRV.cpp
  • [ ] Add codegen for spv smoothstep to EmitSPIRVBuiltinExpr in CGBuiltin.cpp
  • [ ] Add codegen tests to clang/test/CodeGenHLSL/builtins/smoothstep.hlsl
  • [ ] Add spv codegen test to clang/test/CodeGenSPIRV/Builtins/smoothstep.c
  • [ ] Add sema tests to clang/test/SemaHLSL/BuiltIns/smoothstep-errors.hlsl
  • [ ] Add spv sema tests to clang/test/SemaSPIRV/BuiltIns/smoothstep-errors.c
  • [ ] Create the int_spv_smoothstep intrinsic in IntrinsicsSPIRV.td
  • [ ] In SPIRVInstructionSelector.cpp create the smoothstep lowering and map it to int_spv_smoothstep in SPIRVInstructionSelector::selectIntrinsic.
  • [ ] Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/smoothstep.ll
  • [ ] Check for what OpenCL support is needed.

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
7 Saturate 6.0 ()

SPIR-V

SmoothStep:

Description:

SmoothStep

Result is 0.0 if x ≤ edge0 and 1.0 if x ≥ edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 < x < edge1. This is equivalent to:

t * t * (3 - 2 * t), where t = clamp ((x - edge0) / (edge1 - edge0), 0, 1)

Result is undefined if edge0 ≥ edge1.

The operands must all be a scalar or vector whose component type is floating-point.

Result Type and the type of all operands must be the same type. Results are computed per component.

Number Operand 1 Operand 2 Operand 3 Operand 4

49

<id>
edge0

<id>
edge1

<id>
x

Test Case(s)

Example 1

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

export float4 fn(float4 p1, float4 p2, float4 p3) {
    return smoothstep(p1, p2, p3);
}

HLSL:

Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].

ret smoothstep(min, max, x)

Parameters

Item Description
min
[in] The minimum range of the x parameter.
max
[in] The maximum range of the x parameter.
x
[in] The specified value to be interpolated.

Return Value

Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].

Remarks

Use the smoothstep HLSL intrinsic function to create a smooth transition between two values. For example, you can use this function to blend two colors smoothly.

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any
min same as input x float same dimension(s) as input x
max same as input x float same dimension(s) as input x
ret same as input x float same dimension(s) as input x

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) yes (vs_1_1 only)

See also

Intrinsic Functions (DirectX HLSL)

farzonl avatar Jul 16 '24 20:07 farzonl

Expanded in DXIL, opcode in SPIRV

pow2clk avatar Nov 19 '24 19:11 pow2clk

I will be taking on this task.

kmpeng avatar Mar 17 '25 21:03 kmpeng

@llvm/issue-subscribers-clang-codegen

Author: Farzon Lotfi (farzonl)

- [ ] Implement `smoothstep` using HLSL source in `hlsl_intrinsics.h` - [ ] Implement the `smoothstep` SPIR-V target built-in in clang/include/clang/Basic/BuiltinsSPIRV.td - [ ] Add sema checks for `smoothstep` to `CheckSPIRVBuiltinFunctionCall ` in `clang/lib/Sema/SemaSPIRV.cpp` - [ ] Add codegen for spv `smoothstep` to `EmitSPIRVBuiltinExpr ` in `CGBuiltin.cpp` - [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/smoothstep.hlsl` - [ ] Add spv codegen test to `clang/test/CodeGenSPIRV/Builtins/smoothstep.c` - [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/smoothstep-errors.hlsl` - [ ] Add spv sema tests to `clang/test/SemaSPIRV/BuiltIns/smoothstep-errors.c` - [ ] Create the `int_spv_smoothstep` intrinsic in `IntrinsicsSPIRV.td` - [ ] In SPIRVInstructionSelector.cpp create the `smoothstep` lowering and map it to `int_spv_smoothstep` in `SPIRVInstructionSelector::selectIntrinsic`. - [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/smoothstep.ll` - [ ] Check for what OpenCL support is needed.

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
7 Saturate 6.0 ()

SPIR-V

SmoothStep:

Description:

SmoothStep

Result is 0.0 if x ≤ edge0 and 1.0 if x ≥ edge1 and performs smooth Hermite interpolation between 0 and 1 when edge0 &lt; x &lt; edge1. This is equivalent to:

t * t * (3 - 2 * t), where t = clamp ((x - edge0) / (edge1 - edge0), 0, 1)

Result is undefined if edge0 ≥ edge1.

The operands must all be a scalar or vector whose component type is floating-point.

Result Type and the type of all operands must be the same 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>Number</th> <th>Operand 1</th> <th>Operand 2</th> <th>Operand 3</th> <th>Operand 4</th> </tr> </thead> <tbody> <tr> <td class="tableblock halign-left valign-top"><p>49</p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>edge0</em></p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>edge1</em></p></td> <td class="tableblock halign-left valign-top"><p><em>&lt;id&gt;</em><br /> <em>x</em></p></td> <td></td> </tr> </tbody> </table>

Test Case(s)

Example 1

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

export float4 fn(float4 p1, float4 p2, float4 p3) {
    return smoothstep(p1, p2, p3);
}

HLSL:

Returns a smooth Hermite interpolation between 0 and 1, if x is in the range [min, max].

ret smoothstep(min, max, x)

Parameters

Item Description
<span id="min"></span><span id="MIN"></span>min<br/> [in] The minimum range of the x parameter.<br/>
<span id="max"></span><span id="MAX"></span>max<br/> [in] The maximum range of the x parameter.<br/>
<span id="x"></span><span id="X"></span>x<br/> [in] The specified value to be interpolated.<br/>

Return Value

Returns 0 if x is less than min; 1 if x is greater than max; otherwise, a value between 0 and 1 if x is in the range [min, max].

Remarks

Use the smoothstep HLSL intrinsic function to create a smooth transition between two values. For example, you can use this function to blend two colors smoothly.

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any
min same as input x float same dimension(s) as input x
max same as input x float same dimension(s) as input x
ret same as input x float same dimension(s) as input x

Minimum Shader Model

This function is supported in the following shader models.

Shader Model Supported
Shader Model 2 (DirectX HLSL) and higher shader models yes
Shader Model 1 (DirectX HLSL) yes (vs_1_1 only)

See also

<dl> <dt>

Intrinsic Functions (DirectX HLSL) </dt> </dl>

llvmbot avatar Mar 29 '25 04:03 llvmbot