You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[SPIR-V] Problem loading struct with vk::RawBufferLoad. #4924
struct GPUObjDesc
{
float3 test;
float3 test2;
float3 test3;
soulsl::address vertex_address; // Address of the Vertex buffer
soulsl::address index_address; // Address of the index buffer
soulsl::address material_address; // Address of the material buffer
soulsl::address material_index_address; // Address of the triangle material index buffer
};
GPUObjDesc gpu_obj_desc = vk::RawBufferLoad<GPUObjDesc>(push_constant.gpu_obj_buffer_address);
float3 test2 = vk::RawBufferLoad<float3>(push_constant.gpu_obj_buffer_address + sizeof(float3));
float3 test3 = vk::RawBufferLoad<float3>(push_constant.gpu_obj_buffer_address + 2 * sizeof(float3));
For some reason gpu_obj_desc.test2 and gpu_obj_desc.test3 return the value of gpu_obj_desc.test. But test2 and test3 return the correct value. So it behaves correctly if I load the float3 by offsetting manually. I compile the hlsl with -fvk-use-dx-layout and -fspv-target-env=vulkan1.3. I have bindless mechanism as well that use ByteAddressBuffer.Load, that mechanism work perfectly fine when I load this same struct.
Actually I have look at the disassembly spir-v. And I wonder why it translates to SPIR-V differently from ByteAddressBuffer.Load. In ByteAddressBuffer.Load, dxc will load the member one by one using OpAccessChain (one OpAccessChain per uint) but vk::RawBufferLoad will only use one OpLoad
Version: dxcompiler.dll: 1.7 - 1.7.2212.12 (8c9d92b); dxil.dll: 1.7(101.7.2212.14)
Command : ./dxc.exe -HV 2021 -spirv -fvk-use-dx-layout -fspv-target-env="vulkan1.3" -fspv-extension=SPV_EXT_descriptor_indexing -fspv-extension=SPV_KHR_physical_storage_buffer -E cs_main -T cs_6_5 .\test.hlsl -Fo .\test.spv -Fc .\test.ds
%229 is scene and It does correctly extract the first element for %230(scene.cube_color)
Will this method use the layout we indicate to compiler, for example if I request to use dxc_layout.
Loading the member one by one by ourself will compile to similar code that use ByteAddressBuffer.Load, so I guess that's why it works.
Is there any reason, why it doesn't load the memory the same way ByteAddressBuffer.Load does?
Buffer Reference is in my opinion one of the top feature of GLSL/Vulkan, and this vk::RawBufferLoad feature is really great. Unfortunately there is this bug with structures.
As an example, let's take this structure
struct TestBuffer
{
vec3 a;
vec3 b;
};
And fill TestBuffer[2] with the values as {{{1,2,3},{4,56}},{{7,8,9},{10,11,12}}}
Note: If the struct contains more members, they all have a copy of what was loaded for the first member. And if a following member is an integer, it is similar as doing asint() on the value.
The workaround, is to extract each member separately.
fatal error: generated SPIR-V is invalid: Structure id 7 decorated as Block for variable in PhysicalStorageBuffer storage class must follow relaxed storage buffer layout rules: member 0 is missing an Offset decoration
%LineStyle = OpTypeStruct %float %_arr_uint_uint_1
if doesn't matter if I annotate my struct members with [[vk::offset(x)]] or [[vk::ext_decorate(35,x)]], I still get this
Note that the "sizeof(float3)" is 12. This means that the offset for the individual loads are at 12 and 24. But the offsets in %GPUObjDesc_0 are 0, 16, and 32. So the values come from different memory locations.
This is really bad because it makes it impossible to know the memory layout so you can fill in the data on the CPU side.
The only action I think we need to take is to determine which layout is being used, and update the documentation. EDIT: I am concerned that changing the layout at this point will break existing code. Since it is not well documented, I cannot say the current behaviour is incorrect.
It seems to change depending on the layout option used:
fatal error: generated SPIR-V is invalid: Structure id 7 decorated as Block for variable in PhysicalStorageBuffer storage class must follow relaxed storage buffer layout rules: member 0 is missing an Offset decoration
%LineStyle = OpTypeStruct %float %_arr_uint_uint_1
if doesn't matter if I annotate my struct members with [[vk::offset(x)]] or [[vk::ext_decorate(35,x)]], I still get this
As with other RawBuffer related issues, we will not be updating that feature. We will leave the layout as it for now.
However, we do need to carefully consider the layout for vk::BufferPointer. The current spec is too vague, and we will have to make it more detailed. The conversation in this issue should inform that conversation.
Activity
Keenuts commentedon Jan 16, 2023
Hi, Thanks for the report!
In order for us to reproduce the issue, we'd like a bit more information from your end:
(Example of a good issue summary: [SPIR-V] Shader failes to compile when activating debug infos #4911)
Tried to guess a shader using those things (minus soulsl fields since those are not defined here), but OpAccessChain looks fine.
kevyuu commentedon Jan 16, 2023
Actually I have look at the disassembly spir-v. And I wonder why it translates to SPIR-V differently from ByteAddressBuffer.Load. In ByteAddressBuffer.Load, dxc will load the member one by one using OpAccessChain (one OpAccessChain per uint) but vk::RawBufferLoad will only use one OpLoad
Resulting SPIR-V
Version: dxcompiler.dll: 1.7 - 1.7.2212.12 (8c9d92b); dxil.dll: 1.7(101.7.2212.14)
Command : ./dxc.exe -HV 2021 -spirv -fvk-use-dx-layout -fspv-target-env="vulkan1.3" -fspv-extension=SPV_EXT_descriptor_indexing -fspv-extension=SPV_KHR_physical_storage_buffer -E cs_main -T cs_6_5 .\test.hlsl -Fo .\test.spv -Fc .\test.ds
%229 is scene and It does correctly extract the first element for %230(scene.cube_color)
Will this method use the layout we indicate to compiler, for example if I request to use dxc_layout.
Loading the member one by one by ourself will compile to similar code that use ByteAddressBuffer.Load, so I guess that's why it works.
Is there any reason, why it doesn't load the memory the same way ByteAddressBuffer.Load does?
mklefrancois commentedon Jul 18, 2023
Buffer Reference is in my opinion one of the top feature of GLSL/Vulkan, and this
vk::RawBufferLoadfeature is really great. Unfortunately there is this bug with structures.As an example, let's take this structure
And fill TestBuffer[2] with the values as {{{1,2,3},{4,56}},{{7,8,9},{10,11,12}}}
In the shader, we can retrieve the values as such
The result show the correct result for the first member, but the following one contains the same data as
a.Applying an offset will give again the right value for the first member, and copy to all other members.
Note: If the struct contains more members, they all have a copy of what was loaded for the first member. And if a following member is an integer, it is similar as doing
asint()on the value.The workaround, is to extract each member separately.
But this is not always simple, especially when the structure have members that can be
#ifdefout.Extra, a very nice addition to
vk::RawBufferLoadwould be to support the[]operand, of some other ways to load the n element of the buffer.devshgraphicsprogramming commentedon Aug 31, 2023
Its now broken in new and fun ways
https://godbolt.org/z/onYcs95bc
if doesn't matter if I annotate my struct members with
[[vk::offset(x)]]or[[vk::ext_decorate(35,x)]], I still get thiss-perron commentedon Sep 29, 2023
Here is a sample that shows the problem: https://godbolt.org/z/cbPnTaG45
Note that the "sizeof(float3)" is 12. This means that the offset for the individual loads are at 12 and 24. But the offsets in
%GPUObjDesc_0are 0, 16, and 32. So the values come from different memory locations.From the description of the
vk::RawBufferLoad, the memory layout of the struct is not specificed.This is really bad because it makes it impossible to know the memory layout so you can fill in the data on the CPU side.
The only action I think we need to take is to determine which layout is being used, and update the documentation. EDIT: I am concerned that changing the layout at this point will break existing code. Since it is not well documented, I cannot say the current behaviour is incorrect.
It seems to change depending on the layout option used:
default: 16
DX: 16,
GL: 16
scalar: 12
s-perron commentedon Sep 29, 2023
This example shows that the byte address buffer uses different layouts than the rawbuffer loads: https://godbolt.org/z/nE4ns5WPe.
default: 16,
DX: 12,
GL: 16
scalar: 12
devshgraphicsprogramming commentedon Apr 17, 2024
This seems to have been fixed, but comes back if I launder the address through a bitcast (possible with prop 0011) now
https://godbolt.org/z/TnEM3Ka5d
P.S. my Suggestion is to only allow
RawBufferand friends with-fvk-use-scalar-layouts-perron commentedon Aug 23, 2024
As with other RawBuffer related issues, we will not be updating that feature. We will leave the layout as it for now.
However, we do need to carefully consider the layout for vk::BufferPointer. The current spec is too vague, and we will have to make it more detailed. The conversation in this issue should inform that conversation.
s-perron commentedon Aug 23, 2024
I opened microsoft/hlsl-specs#306 to follow up on the vk::BufferPointer layout rules.
devshgraphicsprogramming commentedon Aug 23, 2024
It also seems to have gotten fixed between 1.8.2405 and 2407