Skip to content

Implement the reflect HLSL Function #99152

@farzonl

Description

@farzonl
  • implement reflect in the hlsl_intrinsics.h
  • Implement reflect 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_reflect))
  return __builtin_spirv_reflect(...);
#else
  return ...; // regular behavior
#endif
  • Add codegen for spirv reflect builtin to EmitSPIRVBuiltinExpr in CGBuiltin.cpp
  • Add HLSL codegen tests to clang/test/CodeGenHLSL/builtins/reflect.hlsl
  • Add SPIRV builtin codegen tests to clang/test/CodeGenSPIRV/Builtins/reflect.c
  • Add sema tests to clang/test/SemaHLSL/BuiltIns/reflect-errors.hlsl
  • Add spirv sema tests to clang/test/CodeGenSPIRV/Builtins/reflect-errors.c
  • Create the int_spv_reflect intrinsic in IntrinsicsSPIRV.td
  • In SPIRVInstructionSelector.cpp create the reflect lowering and map it to int_spv_reflect in SPIRVInstructionSelector::selectIntrinsic.
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/hlsl-intrinsics/reflect.ll
  • Create SPIR-V backend test case in llvm/test/CodeGen/SPIRV/opencl/reflect.ll

DirectX

There were no DXIL opcodes found for reflect. That should make it easy to do entirely in the frontend.
In DXC it was done via emitting llvmir. We do not want to do that in upstream.

SPIR-V

Reflect:

Description:

Reflect

For the incident vector I and surface orientation N, the result is
the reflection direction:

I - 2 * dot(N, I) * N

N must already be normalized in order to achieve the desired result.

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

71

<id>
I

<id>
N

Test Case(s)

Example 1

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

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

HLSL:

Returns a reflection vector using an incident ray and a surface normal.

ret reflect(i, n)

Parameters

Item Description
i
[in] A floating-point, incident vector.
n
[in] A floating-point, normal vector.

Return Value

A floating-point, reflection vector.

Remarks

This function calculates the reflection vector using the following formula: v = i - 2 * n * dot(i n) .

Type Description

Name Template Type Component Type Size
i vector float any
n vector float same dimension(s) as input i
ret 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 1 (DirectX HLSL) and higher shader models yes

See also

Intrinsic Functions (DirectX HLSL)

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

Status

Closed

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions