microsoft / DirectXShaderCompiler

This repo hosts the source for the DirectX Shader Compiler which is based on LLVM/Clang.
Other
2.99k stars 671 forks source link

[SPIR-V] Accessing HitTriangleVertexPositionsKHR fails #5455

Open mklefrancois opened 11 months ago

mklefrancois commented 11 months ago

Description

SPV_KHR_ray_tracing_position_fetch is adding a new HitTriangleVertexPositionsKHR built-in, but even with the use of [[vk::ext_decorate]], it does not seem possible to access the values in a HLSL Closest-Hit shader.

Steps to Reproduce

In GLSL, the triangle vertex positions can be accessed with gl_HitTriangleVertexPositionsEXT and the built-in is declared as

in    vec3  gl_HitTriangleVertexPositionsEXT[3];

In HLSL I'm missing the information on how to declare and access this information.

The following was tried

#define BuiltIn 11
#define RayTracingPositionFetchKHR 5336
#define HitTriangleVertexPositionsKHR 5391
[[vk::ext_extension("SPV_KHR_ray_tracing_position_fetch")]]
[[vk::ext_capability(RayTracingPositionFetchKHR)]] 

static float3 gl_HitTriangleVertexPositions[3];

[shader("closesthit")]
void rchitMain(inout HitPayload payload, in BuiltInTriangleIntersectionAttributes attr)
{
  [[vk::ext_decorate(BuiltIn, HitTriangleVertexPositionsKHR)]]
  float3 HitTriangleVertexPositions[3];
  gl_HitTriangleVertexPositions[0] = HitTriangleVertexPositions[0];
  gl_HitTriangleVertexPositions[1] = HitTriangleVertexPositions[1];
  gl_HitTriangleVertexPositions[2] = HitTriangleVertexPositions[2];
...
}

Actual Behavior This will compile, but this ends up with %66 = OpUndef %_arr_v3float_uint_3

As Attribute to CHIT I also tried to pass the information as attribute, like this.

struct MyAttributes
{
  [[vk::ext_decorate(BuiltIn, HitTriangleVertexPositionsKHR)]] float3 HitTriangleVertexPositions[3];
  BuiltInTriangleIntersectionAttributes intersect;
};

[shader("closesthit")]
void rchitMain(inout HitPayload payload, in MyAttributes attr)
{
  gl_HitTriangleVertexPositions[0] = attr.HitTriangleVertexPositions[0];

Result

This also fails to create something like : OpDecorate %_arr_v3float_uint_3 BuiltIn HitTriangleVertexPositions And the values received are uninitialized.

Environment

s-perron commented 11 months ago

It is not currently possible to access this builtin. We are designing a change the inline spir-v to make it possible. See https://github.com/microsoft/hlsl-specs/pull/59.

SaschaWillems commented 4 months ago

Any update on this?

cassiebeckley commented 4 months ago

You may now use vk::ext_builtin_output to define this builtin:

#define HitTriangleVertexPositionsKHR 5391

[[vk::ext_builtin_output(HitTriangleVertexPositionsKHR)]]
static float3 gl_HitTriangleVertexPositions[3];

Our documentation should be updated; we have a tracking issue #6412 for that.

SaschaWillems commented 3 months ago

Is this included in the last github release, or do a I need a newer, more recent version of dxc?

mklefrancois commented 3 months ago

I tried it with DXC from the latest SDK, and I get the following error

CUSTOMBUILD : fatal error : failed to legalize SPIR-V: Invalid built-in operand: 5391
SaschaWillems commented 3 months ago

Same for the download from the github releases page. I guess the fix came in very recently, so there is no binary available yet.

cassiebeckley commented 3 months ago

This feature has been available for several months, and is in the latest release. I've double-checked the opcode for HitTriangleVertexPositionsKHR, and it appears to be 5335, per https://github.com/KhronosGroup/SPIRV-Headers/blob/8b246ff75c6615ba4532fe4fde20f1be090c3764/include/spirv/unified1/spv.d#L770.

cassiebeckley commented 3 months ago

You will need to add the RayTracingPositionFetchKHR capability and "SPV_KHR_ray_tracing_position_fetch" extension as well.

#define HitTriangleVertexPositionsKHR 5335
#define RayTracingPositionFetchKHR 5336

[[vk::ext_extension("SPV_KHR_ray_tracing_position_fetch")]]
[[vk::ext_capability(RayTracingPositionFetchKHR)]]
[[vk::ext_builtin_output(HitTriangleVertexPositionsKHR)]]
static float3 gl_HitTriangleVertexPositions[3];
SaschaWillems commented 3 months ago

I can't get this to work. I tried different variants and always get a compilation error with the latest github release. Currently I get this:

fatal error: generated SPIR-V is invalid: OpEntryPoint Entry Point <id> '2[%main]'s callgraph contains function <id> '2[%main]', which cannot be used with the current execution model:
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models
[VUID-StandaloneSpirv-None-04644] in Vulkan environment, Output Storage Class must not be used in GLCompute, RayGenerationKHR, IntersectionKHR, AnyHitKHR, ClosestHitKHR, MissKHR, or CallableKHR execution models

  %main = OpFunction %void None %34

note: please file a bug report on https://github.com/Microsoft/DirectXShaderCompiler/issues with source code if possible

It looks like it does have trouble when above declaration is put above the main entry point.

E.g.:

#define HitTriangleVertexPositionsKHR 5335
#define RayTracingPositionFetchKHR 5336

[[vk::ext_extension("SPV_KHR_ray_tracing_position_fetch")]]
[[vk::ext_capability(RayTracingPositionFetchKHR)]]
[[vk::ext_builtin_output(HitTriangleVertexPositionsKHR)]]
static float3 gl_HitTriangleVertexPositions[3];

[shader("closesthit")]
void main(inout Payload p, in Attributes attribs)
{
cassiebeckley commented 3 months ago

Hm, I've looked at this in closer detail and it seems like vk::ext_builtin_output might be broken for arrays. I'll reopen and assign this to myself, and try to look into what will be required to fix it.

SaschaWillems commented 3 months ago

Any update on this? It's kinda blocking me from porting some GLSL shaders. Not urgent, but would love to get this working with HLSL :)

devshgraphicsprogramming commented 3 months ago

Any update on this? It's kinda blocking me from porting some GLSL shaders. Not urgent, but would love to get this working with HLSL :)

It works: https://godbolt.org/z/sfG8veGnf

@cassiebeckley just gave you the wrong attribute, its supposed to be const static and ext_builtin_input

The vertex positions are INPUTS not ouputs.

SaschaWillems commented 2 months ago

@devshgraphicsprogramming Can confirm this works. Thank you very much :)