Skip to content

Implement the smoothstep HLSL Function #99156

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

Closed
12 tasks
Tracked by #99235
farzonl opened this issue Jul 16, 2024 · 3 comments · Fixed by #132288
Closed
12 tasks
Tracked by #99235

Implement the smoothstep HLSL Function #99156

farzonl opened this issue Jul 16, 2024 · 3 comments · Fixed by #132288
Assignees
Labels
bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.

Comments

@farzonl
Copy link
Member

farzonl commented Jul 16, 2024

  • 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 xedge0 and 1.0 if xedge1 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 edge0edge1.

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 farzonl added backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues. labels Jul 16, 2024
@damyanp damyanp moved this to Ready in HLSL Support Oct 30, 2024
@damyanp damyanp moved this from Ready to Planning in HLSL Support Oct 30, 2024
@pow2clk
Copy link
Contributor

pow2clk commented Nov 19, 2024

Expanded in DXIL, opcode in SPIRV

@pow2clk pow2clk moved this from Planning to Designing in HLSL Support Nov 19, 2024
@farzonl farzonl moved this from Designing to Planning in HLSL Support Mar 17, 2025
@kmpeng
Copy link
Contributor

kmpeng commented Mar 17, 2025

I will be taking on this task.

@damyanp damyanp moved this from Planning to Needs Review in HLSL Support Mar 26, 2025
@github-project-automation github-project-automation bot moved this from Needs Review to Closed in HLSL Support Mar 29, 2025
@EugeneZelenko EugeneZelenko added clang:codegen IR generation bugs: mangling, exceptions, etc. and removed backend:DirectX backend:SPIR-V labels Mar 29, 2025
@llvmbot
Copy link
Member

llvmbot commented Mar 29, 2025

@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 xedge0 and 1.0 if xedge1 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 edge0edge1.

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>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bot:HLSL clang:codegen IR generation bugs: mangling, exceptions, etc. HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: Closed
Development

Successfully merging a pull request may close this issue.

5 participants