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 refract HLSL Function #99153

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

Implement the refract HLSL Function #99153

farzonl opened this issue Jul 16, 2024 · 1 comment
Assignees
Labels
backend:DirectX 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 refract using HLSL source in hlsl_intrinsics.h
  • Implement the refract SPIR-V target built-in in clang/include/clang/Basic/BuiltinsSPIRV.td
  • Add sema checks for refract to CheckSPIRVBuiltinFunctionCall in clang/lib/Sema/SemaSPIRV.cpp
  • Add codegen for spv refract to EmitSPIRVBuiltinExpr in CGBuiltin.cpp
  • Add codegen tests to clang/test/CodeGenHLSL/builtins/refract.hlsl
  • Add spv codegen test to clang/test/CodeGenSPIRV/Builtins/refract.c
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/refract-errors.hlsl
  • Add spv sema tests to clang/test/SemaSPIRV/BuiltIns/refract-errors.c
  • Create the int_spv_refract intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the refract lowering and map it to int_spv_refract in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/refract.ll
  • Check for what OpenCL support is needed.

DirectX

DXIL Opcode DXIL OpName Shader Model Shader Stages
56 Dot4 6.0 ()

SPIR-V

Refract:

Description:

Refract

For the incident vector I and surface normal N, and the ratio of
indices of refraction eta, the result is the refraction vector. The
result is computed by

k = 1.0 - eta * eta * (1.0 - dot(N, I) * dot(N, I))

if k < 0.0 the result is 0.0

otherwise, the result is eta * I - (eta * dot(N, I) +
sqrt(k)) * N

The input parameters for the incident vector I and the surface normal
N must already be normalized to get the desired results.

The type of I and N must be a scalar or vector with a floating-point
component type.

The type of eta must be a 16-bit or 32-bit floating-point scalar.

Result Type, the type of I, and the type of N must all be the same
type.

Number Operand 1 Operand 2 Operand 3 Operand 4

72

<id>
I

<id>
N

<id>
eta

Test Case(s)

Example 1

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

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

HLSL:

Returns a refraction vector using an entering ray, a surface normal, and a refraction index.

ret refract(i, n, ?)

Parameters

Item Description
i
[in] A floating-point, ray direction vector.
n
[in] A floating-point, surface normal vector.
?
[in] A floating-point, refraction index scalar.

Return Value

A floating-point, refraction vector. If the angle between the entering ray i and the surface normal n is too great for a given refraction index ?, the return value is (0,0,0).

Type Description

Name Template Type Component Type Size
i vector float any
n vector float same dimension(s) as input i
? scalar float 1
refraction vector vector float same dimension(s) as input i

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 pow2clk moved this from Planning to Designing in HLSL Support Nov 19, 2024
@farzonl farzonl moved this from Designing to Planning in HLSL Support Feb 4, 2025
@damyanp damyanp moved this from Planning to Ready in HLSL Support Feb 4, 2025
@damyanp damyanp moved this from Ready to Active in HLSL Support Feb 6, 2025
@raoanag
Copy link

raoanag commented Feb 6, 2025

Started looking into this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend:DirectX backend:SPIR-V bot:HLSL HLSL HLSL Language Support metabug Issue to collect references to a group of similar or related issues.
Projects
Status: Active
Development

No branches or pull requests

2 participants