Skip to content

[SPIR-V] Combining "-fvk-allow-rwstructuredbuffer-arrays" with intrinsics results in invalid SPIRV. #6844

Closed
KhronosGroup/SPIRV-Tools
#5757

Description

Description

Using "-fvk-allow-rwstructuredbuffer-arrays" allowed the creation of arrays for rwstructuredbuffer for bindless in Vulkan. It also avoided the unwanted creation of counter buffers (usually for append/consume, which we never use). It works well! However for some reason when we mix it with the use of intrinsics like in the sample below (OpRayQueryGetIntersectionTriangleVertexPositionsKHR), it starts adding counter_var on our rwstructuredbuffer for some reason, and the resulting SPIRV fails validation.

Steps to Reproduce

Build provided shader with "dxc.exe -T cs_6_6 -spirv -fvk-allow-rwstructuredbuffer-arrays counters.hlsl".

Using the code as-is causes the shader to fail validation. Running with an additional "-Vd" in the command line allows us to see that the resulting SPIRV has an unwanted counter_var_BindlessBufferArray and ACSBuffer.

Notice that setting either USE_EXTENSION or USE_BINDLESS to 0 results in valid SPIRV, but both together fail.

// using DXC from Vulkan SDK 1.3.283.0 (Version: dxcompiler.dll: 1.8 - 1.8.0.4575 (4f4bb4208); dxil.dll: 1.6(101.6.2112.13))
// compile this code with command line "dxc.exe -T cs_6_6 -spirv -fvk-allow-rwstructuredbuffer-arrays counters.hlsl"


// Flip this to make use of the extension which makes the issue appear
#define USE_EXTENSION 1
#define USE_BINDLESS  1

#if USE_EXTENSION
[[vk::ext_capability(5391 /* RayQueryPositionFetchKHR */)]]
[[vk::ext_extension("SPV_KHR_ray_tracing_position_fetch")]]
[[vk::ext_instruction(5340 /* OpRayQueryGetIntersectionTriangleVertexPositionsKHR */)]]
float3 RayQueryGetIntersectionTriangleVertexPositionsKHR([[vk::ext_reference]] RayQuery<RAY_FLAG_NONE> RayQ, int committed)[3];
#endif

// Random datas for example
uint BufferOffset;
RaytracingAccelerationStructure TLAS;

#if USE_BINDLESS
uint BindlessIndex;
RWStructuredBuffer<float3> BindlessBufferArray[];
#else
RWStructuredBuffer<float3> MyBuffer;
#endif

[numthreads(8, 4, 1)] void main()
{
	RayDesc NativeRay;
	NativeRay.Origin = float3(0.0f, 0.0f, 0.0f);
	NativeRay.TMin = 0.0f;
	NativeRay.Direction = float3(0.0f, 0.0f, 1.0f);
	NativeRay.TMax = 10.0f;
		
	RayQuery<RAY_FLAG_NONE> RayQ;
	RayQ.TraceRayInline(TLAS, RAY_FLAG_NONE, 0, NativeRay);
	while (RayQ.Proceed()) {}
	
#if USE_BINDLESS
	RWStructuredBuffer<float3> MyBuffer = BindlessBufferArray[BindlessIndex];
#endif
	
	float3 ValueToWrite;
#if USE_EXTENSION
	ValueToWrite = RayQueryGetIntersectionTriangleVertexPositionsKHR(RayQ, true)[0];
#else
	ValueToWrite = RayQ.WorldRayDirection();
#endif

	MyBuffer[BufferOffset] = ValueToWrite;
}

Environment

  • Version: dxcompiler.dll: 1.8 - 1.8.0.4575 (4f4bb42); dxil.dll: 1.6(101.6.2112.13) (Vulkan SDK 1.3.283.0)
  • Host Operating System: Windows

Activity

  1. added
    bug
    needs-triage
    spirv
    on Aug 1, 2024
  2. s-perron commented on Aug 2, 2024

    @s-perron
    Collaborator

    I have a guess. I think this is a simple fix. Spirv-opt does not know about SPV_KHR_ray_tracing_position_fetch, so it turns off some optimizations. This is causing optimization that are needed to fix up illegal code. I'll give it a try and report back.

  3. s-perron commented on Aug 2, 2024

    @s-perron
    Collaborator

    Should be fixed by KhronosGroup/SPIRV-Tools#5757. We will need another PR to update the submodule after that PR merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugspirv

Type

No type

Projects

Relationships

None yet

Development

No branches or pull requests

Issue actions