Skip to content

[SPIRV] ByteAddressBuffer templated load generates incorrect spirv when not using dx-layout/scalar-layout #5122

Closed

Description

@manas-kulkarni

Due to the lack of scalar block support on low-end devices, we have to use the std430 layout for storage buffers. Rather than converting all structures to follow the std430 layout and increase memory usage, we decided to utilize the templated load functionality of raw buffers.
But the generated spirv is inconsistent. The following code explains the issue

struct Bounds
{
    float3 pos;
    float3 scale;
};

ByteAddressBuffer inBuffer;
RWByteAddressBuffer outBuffer;

[numthreads(8, 8, 1)]
void main(uint dtid : SV_DispatchThreadID)
{
	Bounds b = inBuffer.Load<Bounds>(dtid * sizeof(Bounds));
	outBuffer.Store<Bounds>(dtid * sizeof(Bounds), b);
}

Command to compile to spirv .\dxc.exe -spirv /T cs_6_1 .\comp.hlsl (note no dx-layout or scalar-layout option is specified)

Generated glsl from spirv-cross. We also verified same issue is there in the raw spirv generated through dxc but glsl is more readable so posting that

#version 450
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;

layout(set = 0, binding = 0, std430) readonly buffer type_ByteAddressBuffer
{
    uint _m0[];
} inBuffer;

layout(set = 0, binding = 1, std430) buffer type_RWByteAddressBuffer
{
    uint _m0[];
} outBuffer;

void main()
{
    uint _28 = (gl_GlobalInvocationID.x * 24u) >> 2u;
    uint _32 = _28 + 1u;
    uint _34 = inBuffer._m0[_32];
    uint _36 = _28 + 2u;
    uint _38 = inBuffer._m0[_36];
    uint _40 = _28 + 4u;
    uint _42 = inBuffer._m0[_40];
    uint _44 = _28 + 5u;
    uint _46 = inBuffer._m0[_44];
    uint _48 = _28 + 6u;
    uint _50 = inBuffer._m0[_48];
    outBuffer._m0[_28] = floatBitsToUint(uintBitsToFloat(inBuffer._m0[_28]));
    outBuffer._m0[_32] = floatBitsToUint(uintBitsToFloat(_34));
    outBuffer._m0[_36] = floatBitsToUint(uintBitsToFloat(_38));
    outBuffer._m0[_40] = floatBitsToUint(uintBitsToFloat(_42));
    outBuffer._m0[_44] = floatBitsToUint(uintBitsToFloat(_46));
    outBuffer._m0[_48] = floatBitsToUint(uintBitsToFloat(_50));
}

uint _28 = (gl_GlobalInvocationID.x * 24u) >> 2u;
This is correct as sizeof(Bounds) is indeed 24
_40 = _28 + 4;
This is the problem as it assumes 1 uint implicit padding between Bounds::pos and Bounds::scale even when sizeof(Bounds) = 24. This breaks the consistency as sizeof treats Bounds as arbitrary blob of data (correct behavior) while the templated load treats it as std430 layout

Activity

  1. added
    bug
    spirv
    on May 9, 2023
  2. added a commit that references this issue on Jun 14, 2023
    30adc3f
  3. sudonatalie commented on Jul 27, 2023

    @sudonatalie
    Collaborator

    It looks like our implementation of std430 here does match the behavior of glslang, so we'll consider the padding correct in practice as modifying it (even if it would technically follow the spec) would break longstanding assumptions and backwards compatibility. Ideally, we would like to create consistency with a change to sizeof, but due to the way it's implemented here it's not feasible to fix without major changes to introduce the additional context that would be needed to calculate it correctly.

    Unfortunately due to those inherent design issues I don't think we can offer a solution, so I'm going to close this issue.

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

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions