Skip to content
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

Implement the faceforward HLSL Function #99114

Open
12 tasks
Tracked by #99235
farzonl opened this issue Jul 16, 2024 · 2 comments
Open
12 tasks
Tracked by #99235

Implement the faceforward HLSL Function #99114

farzonl opened this issue Jul 16, 2024 · 2 comments
Labels
backend:SPIR-V bot:HLSL 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 faceforward in the hlsl_intrinsics.h
  • Implement faceforward spirv target builtin in clang/include/clang/Basic/BuiltinsSPIRV.td
  • Add a spirv fast path in hlsl_detail.h in the form
#if (__has_builtin(__builtin_spirv_faceforward))
  return __builtin_spirv_faceforward(...);
#else
  return ...; // regular behavior
#endif

An alternative to doing a built-in here would be to do pattern matching. Say we have code like this

faceforward(p1, p2, p3) ->
    dResult = dot(p2, p3)
    if(dResult < 0)
        return 0 - dResult;
   return dResult;

We would expect the code gen to look something like this

faceforward(p1, p2, p3) ->
  dResult       = dot(p2, p3)
  cmpResult  = fcmp dResult, 0.0
  subResult   = fsub -0.0 p1
  select(cmpResult, p1, subResult)

that means we could pattern match in SPIRVCombine.td and SPIRVPreLegalizerCombiner.cpp to

select(
    fcmp(
        dot(p2, p3), 
        0),
    p1,
    0 - p1) 
  • Add codegen for spirv faceforward builtin to EmitSPIRVBuiltinExpr in CGBuiltin.cpp
  • Add HLSL codegen tests to clang/test/CodeGenHLSL/builtins/faceforward.hlsl
  • Add SPIRV builtin codegen tests to clang/test/CodeGenSPIRV/Builtins/faceforward.c
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/faceforward-errors.hlsl
  • Add spirv sema tests to clang/test/CodeGenSPIRV/Builtins/faceforward-errors.c
  • Create the int_spv_faceforward intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the faceforward lowering and map it to int_spv_faceforward in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/faceforward.ll
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/opencl/faceforward.ll

DirectX

No DXIL Opcodes were found.

SPIR-V

FaceForward:

Description:

FaceForward

If the dot product of Nref and I is negative, the result is N,
otherwise it is -N.

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.

Number Operand 1 Operand 2 Operand 3 Operand 4

70

<id>
N

<id>
I

<id>
Nref

Test Case(s)

Example 1

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

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

HLSL:

Flips the surface-normal (if needed) to face in a direction opposite to i; returns the result in n.

ret faceforward(n, i, ng)

This function uses the following formula: -n sign(dot(i, ng)).

Parameters

Item Description
n
[in] The resulting floating-point surface-normal vector.
i
[in] A floating-point, incident vector that points from the view position to the shading position.
ng
[in] A floating-point surface-normal vector.

Return Value

A floating-point, surface normal vector that is facing the view direction.

Type Description

Name Template Type Component Type Size
n vector float any
i vector float same dimension(s) as input n
ng vector float same dimensions as input n
ret vector float same dimensions as input n

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) vs_1_1 and ps_1_4

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
@davidcook-msft
Copy link

@farzonl to update this item outlining the difference between the DirectX impl and the SPIR-V impl

@davidcook-msft davidcook-msft moved this from Planning to Designing in HLSL Support Nov 12, 2024
@farzonl farzonl moved this from Designing to Planning in HLSL Support Jan 10, 2025
@damyanp
Copy link
Contributor

damyanp commented Jan 21, 2025

@farzonl - update to mention pattern matching implementation option

@damyanp damyanp moved this from Planning to Ready in HLSL Support Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: Ready
Development

No branches or pull requests

3 participants