Skip to content

Implement the fwidth HLSL Function #99120

@farzonl

Description

@farzonl
  • Implement fwidth clang builtin,
  • Link fwidth clang builtin with hlsl_intrinsics.h
  • Add sema checks for fwidth to CheckHLSLBuiltinFunctionCall in SemaChecking.cpp
  • Add codegen for fwidth to EmitHLSLBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/fwidth.hlsl
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/fwidth-errors.hlsl
  • Create the int_dx_fwidth intrinsic in IntrinsicsDirectX.td
  • Create the DXILOpMapping of int_dx_fwidth to 83 in DXIL.td
  • Create the fwidth.ll and fwidth_errors.ll tests in llvm/test/CodeGen/DirectX/
  • Create the int_spv_fwidth intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the fwidth lowering and map it to int_spv_fwidth in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/fwidth.ll

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
83 DerivCoarseX 6.0 ('library', 'pixel', 'compute', 'amplification', 'mesh', 'node')

SPIR-V

OpFwidth:

Description:

Result is the same as computing the sum of the absolute values of
OpDPdx and OpDPdy on P.

Result Type must be a scalar or vector of floating-point
type
using the IEEE 754 encoding. The component width must
be 32 bits.

The type of P must be the same as Result Type. P is the value to
take the derivative of.

This instruction is only valid in the Fragment Execution
Model
.

Capability:
Shader

Word Count Opcode Results Operands

4

209

<id>
Result Type

Result <id>

<id>
P

Test Case(s)

Example 1

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

export float4 fn(float4 p1) {
    return fwidth(p1);
}

HLSL:

Returns the absolute value of the partial derivatives of the specified value.

ret fwidth(x)

This function computes the following: abs(ddx(x)) + abs(ddy(x)).

This function is only supported in pixel shaders.

Parameters

Item Description
x
[in] The specified value.

Return Value

The absolute value of the partial derivatives of the x parameter.

Type Description

Name Template Type Component Type Size
x scalar, vector, or matrix float any
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 3 (DirectX HLSL) and higher shader models yes
Shader Model 2 (DirectX HLSL) yes (ps_2_x only)
Shader Model 1 (DirectX HLSL) no

See also

Intrinsic Functions (DirectX HLSL)

Metadata

Metadata

Labels

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions