Search code examples
glslvulkan

Resource pointer using buffer_reference doesn't point to the right thing


I'm having trouble working with buffer_reference with vulkan. I'm passing a buffer device address to my shader (a closest hit shader) through a uniform block but when I look at it with Nsight it doesn't point to the right values.

Here's my shader code

#version 460
#extension GL_EXT_ray_tracing : require
#extension GL_EXT_scalar_block_layout : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_buffer_reference2 : require

layout(buffer_reference, scalar) readonly buffer Indices
{
    uvec3 Index[];
};

layout(binding = 4, std140) uniform cb4
{
    Indices     indexBuffer;
};

layout(binding = 5, scalar) readonly buffer RefIndex
{
    uvec3 data[];
};

I bound the buffer directly as well as RefIndex so I can compare the content in Nsight and it is not the same.

indexBuffer in the uniform block is given the returned value of vkGetBufferDeviceAddress of the same VkBuffer bound as RefIndex.

What am I missing ?


Solution

  • Turns out buffer_reference expects by default an alignment to 16 bytes. Setting buffer_reference_alignment to 4 bytes and moving the variable length array on level higher solves the issue.

    layout(buffer_reference, std430, buffer_reference_align = 4) readonly buffer Indices
    {
        uint Index;
    };
    
    // ...
    
    Indices indices    = Indices(indexBuffer);    
    uvec3   triIndices = uvec3(indices[gl_PrimitiveID * 3].Index, indices[gl_PrimitiveID * 3 + 1].Index, indices[gl_PrimitiveID * 3 + 2].Index);