Skip to content

Implement the smoothstep HLSL Function #99156

@farzonl

Description

@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 < 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)

Metadata

Metadata

Assignees

Labels

HLSLHLSL Language Supportbot:HLSLclang:codegenIR generation bugs: mangling, exceptions, etc.metaissueIssue to collect references to a group of similar or related issues.

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions