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

    <tr>
        <th>Summary</th>
        <td>
            Implement the `WorldRayDirection` 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 `WorldRayDirection` clang builtin,
- [ ] Link `WorldRayDirection` clang builtin with `hlsl_intrinsics.h`
- [ ] Add sema checks for `WorldRayDirection` to `CheckHLSLBuiltinFunctionCall` in `SemaChecking.cpp`
- [ ] Add codegen for `WorldRayDirection` to `EmitHLSLBuiltinExpr` in `CGBuiltin.cpp`
- [ ] Add codegen tests to `clang/test/CodeGenHLSL/builtins/WorldRayDirection.hlsl`
- [ ] Add sema tests to `clang/test/SemaHLSL/BuiltIns/WorldRayDirection-errors.hlsl`
- [ ] Create the `int_dx_WorldRayDirection` intrinsic in `IntrinsicsDirectX.td`
- [ ] Create the `DXILOpMapping` of `int_dx_WorldRayDirection` to  `148` in `DXIL.td`
- [ ] Create the  `WorldRayDirection.ll` and `WorldRayDirection_errors.ll` tests in `llvm/test/CodeGen/DirectX/`
- [ ] Create the `int_spv_WorldRayDirection` intrinsic in `IntrinsicsSPIRV.td`
- [ ] In SPIRVInstructionSelector.cpp create the `WorldRayDirection` lowering and map  it to `int_spv_WorldRayDirection` in `SPIRVInstructionSelector::selectIntrinsic`.
- [ ] Create SPIR-V backend test case in `llvm/test/CodeGen/SPIRV/hlsl-intrinsics/WorldRayDirection.ll`

## DirectX

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

## SPIR-V

<div id="header">

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

## Short Description

<div class="sectionbody">

WorldRayDirectionKHR - Ray direction in world space

</div>

</div>

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

<div class="sect1">

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

<div class="sectionbody">

<div class="dlist">

` WorldRayDirectionKHR `  
A variable decorated with the ` WorldRayDirectionKHR ` decoration will
specify the direction of the ray being processed, in world space. The
value is 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 class="sidebarblock">

<div class="content">

<div class="title">

Valid Usage

</div>

<div class="ulist">

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

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

- <a href="#VUID-WorldRayDirectionKHR-WorldRayDirectionKHR-04430"
 id="VUID-WorldRayDirectionKHR-WorldRayDirectionKHR-04430"></a>
  <span class="vuid">
  VUID-WorldRayDirectionKHR-WorldRayDirectionKHR-04430 </span>  
  The variable decorated with ` WorldRayDirectionKHR ` **must** be
  declared as a three-component vector of 32-bit floating-point values

</div>

</div>

</div>

</div>

</div>

</div>


## Test Case(s)

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

export float3 fn() {
    return WorldRayDirection();
}
```
 ### SPIRV Example(s):

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

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

The world-space direction for the current ray.

## Syntax

```
float3 WorldRayDirection();

```



## Remarks

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/eJzEWV9z4jgS_zTiRWXKyIHAAw8Ewk5qZ26vktm5fUvJcoN1yJJLkplwn_6qZWNMcMjOztRMiqpgqd1_fr9uqSW4c3KrAeZkfEfGqwGvfG7sfMPt_4xWg9Rkh3lEyfiOkvGKPhSlggK0p2QS_8dYlT3yw0paEF4aTSYxFYrrLU0rqbzUhC1JvDi9_lHq3d97k36VPkfRXDn1LLW3Ujsp3DAnk_hM5yLLqIOCU5GD2Dm6MfZNE97g1BIFP3x8-nhX21pXOkgsuVIoJTVKPUHBg6TU26Eoyz67wmSwBf03bN4X0ndM3r-U9mRq-Vsz_J4dD867RmOAi7A1jhG2XpoMfgONNghbN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