<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/99192>99192</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            Implement the `RayTCurrent` HLSL Function
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            metabug,
            backend:DirectX,
            HLSL,
            backend:SPIR-V,
            bot:HLSL
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          farzonl
      </td>
    </tr>
</table>

<pre>
    - [ ] Implement `RayTCurrent` clang builtin,
- [ ] Link `RayTCurrent` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `RayTCurrent` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `RayTCurrent` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/RayTCurrent.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/RayTCurrent-errors.hlsl`
- [ ] Create the `int_dx_RayTCurrent` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_RayTCurrent` to  `154` in `DXIL.td`
- [ ] Create the  `RayTCurrent.ll` and `RayTCurrent_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_RayTCurrent` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `RayTCurrent` lowering and map  it to `int_spv_RayTCurrent` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/RayTCurrent.ll`

## DirectX

| DXIL Opcode | DXIL OpName | Shader Model | Shader Stages |
| ----------- | ----------- | ------------ | ------------- |
| 154 | RayTCurrent | 6.3 | ('library', 'intersection', 'anyhit', 'closesthit', 'miss') |

## SPIR-V

<div id="header">

# [RayTmaxKHR](https://registry.khronos.org/vulkan/specs/1.3-extensions/man/html/RayTmaxKHR.html)

## Short Description

<div class="sectionbody">

RayTmaxKHR - Maximum T value of a ray

</div>

</div>

<div id="content" class="loadable">

<div class="sect1">

## <a href="#_description" class="anchor"></a> Description

<div class="sectionbody">

<div class="dlist">

` RayTmaxKHR `  
A variable decorated with the ` RayTmaxKHR ` decoration will contain the
parametric <span class="eq"> t <sub>max</sub> </span> value of the ray
being processed. The value is independent of the space in which the ray
origin and direction exist. The value is initialized to the parameter
passed into the <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#glossary-pipeline-trace-ray"
rel="noopener" target="_blank">pipeline trace ray</a> instruction.

<div class="paragraph">

The <span class="eq"> t <sub>max</sub> </span> value changes throughout
the lifetime of the ray that produced the intersection. In the closest
hit shader, the value reflects the closest distance to the intersected
primitive. In the any-hit shader, it reflects the distance to the
primitive currently being intersected. In the intersection shader, it
reflects the distance to the closest primitive intersected so far or the
initial value. The value can change in the intersection shader after
calling ` OpReportIntersectionKHR ` if the corresponding any-hit shader
does not ignore the intersection. In a miss shader, the value is
identical to the parameter passed into the <a
href="https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html#glossary-pipeline-trace-ray"
rel="noopener" target="_blank">pipeline trace ray</a> instruction.

</div>

</div>

<div class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

- <a href="#VUID-RayTmaxKHR-RayTmaxKHR-04348"
 id="VUID-RayTmaxKHR-RayTmaxKHR-04348"></a> <span class="vuid">
 VUID-RayTmaxKHR-RayTmaxKHR-04348 </span>  
  The ` RayTmaxKHR ` decoration **must** be used only within the
  ` IntersectionKHR ` , ` AnyHitKHR ` , ` ClosestHitKHR ` , or
  ` MissKHR ` ` Execution ` ` Model `

- <a href="#VUID-RayTmaxKHR-RayTmaxKHR-04349"
 id="VUID-RayTmaxKHR-RayTmaxKHR-04349"></a> <span class="vuid">
 VUID-RayTmaxKHR-RayTmaxKHR-04349 </span>  
  The variable decorated with ` RayTmaxKHR ` **must** be declared using
  the ` Input ` ` Storage ` ` Class `

- <a href="#VUID-RayTmaxKHR-RayTmaxKHR-04350"
 id="VUID-RayTmaxKHR-RayTmaxKHR-04350"></a> <span class="vuid">
 VUID-RayTmaxKHR-RayTmaxKHR-04350 </span>  
  The variable decorated with ` RayTmaxKHR ` **must** be declared as a
  scalar 32-bit floating-point value

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

 
 ### Example 1
```hlsl
//dxc RayTCurrent_test.hlsl -T lib_6_8 -enable-16bit-types -O0

export float fn() {
    return RayTCurrent();
}
```
 ### SPIRV Example(s):

 ### Example 2
```hlsl
//dxc RayTCurrent_spirv_test.hlsl -T lib_6_8 -E fn -enable-16bit-types -spirv -fspv-target-env=universal1.5 -fcgl -O0

[shader("intersection")]
void fn() {
float   ret = RayTCurrent();
}
```
## HLSL:

A float representing the current parametric ending point for the ray.  

## Syntax

```
float RayTCurrent();

```


## Remarks

**RayTCurrent** defines the current ending point of the ray according to the following formula: Origin + (Direction * RayTCurrent).  *Origin* and *Direction* may be in either world or object space, which results in either a world or an object space ending point.

**RayTCurrent** is initialized in the call [**TraceRay**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/traceray-function.md) call with the [**RayDesc::TMax**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/raydesc.md) value and then is updated during the trace query as intersections are reported (in the any hit), and accepted.

In the [intersection shader](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/intersection-shader.md), it represents the distance to the closest intersection found so far.  It will be updated after () to the THit value provided if the hit was accepted.

In the [any hit shader](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/any-hit-shader.md), it represents the distance to the current intersection being reported.

In the [closest hit shader](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/closest-hit-shader.md), it represents the distance to the closest intersection accepted.

In the [miss shader](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/miss-shader.md), it is equal to *TMax* passed to the **TraceRay** call.


This function can be called from the following raytracing shader types:

* [**Any Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/any-hit-shader.md)
* [**Closest Hit Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/closest-hit-shader.md)
* [**Intersection Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/intersection-shader.md)
* [**Miss Shader**](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/miss-shader.md)





## See also

<dl> <dt>

[Direct3D 12 Raytracing HLSL Reference](https://github.com/MicrosoftDocs/win32/blob/docs/desktop-src//direct3dhlsl/direct3d-12-raytracing-hlsl-reference.md)
</dt> </dl>
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkWk1v27jW_jXMhpAhU7EbL7Jw7HgavO3biyRTzC6gxWOLNxSpISk3nl9_cUhalh077cz0Zha3CNCIpM7Hc76pcOfkWgNck9ENGc0veOsrY69X3P5htLpYGrG9zigZ3VAymtO7ulFQg_aUjPN7vn2ctdaC9mSc01JxvabLViovNWEzkk_3L36S-vl779Bv0ld4qFJOPUntrdROlm5QkXF-QG0qBHVQc1pWUD47ujL2BHFvcHGGRz5-evh0E7ksWl16afSMK4WnpMZTD1DzcFLq9aBsmlMcSyNgDfpNbre19D1mty-N3TOZ_ZKWv8fBg_MuUQwQEbbANcIWMyPgF9DIg7BFQs4RtuiJM0AAz2L2BnFEIVEOkt4dU87AWmPdSQYzC9wD9RUgZan9k3h5OgKpM2pC5K4z8lxaKP1vAy_epjv_7e7Tl-Yzbxqp10jSrM6z84bi5nB0uTcCEniby5FxB9FPuBZHG08JjLgfYY08lNrUr0xG2CIpSdji--C5ZvPj6D386-7-6wmt7jQNW3faedsGv38ABaU3Fn2Qlgdsj9gp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