0
\$\begingroup\$

I have been passing glm::vec3's in an array to a storage buffer to be processed by a compute shader, but once it has been modified I am getting some malformed data back.

Shader

#version 450

#extension GL_ARB_separate_shader_objects : enable

layout(binding = 0) buffer DataObject {
    vec3[] floatArray;
} data;

void main()
{
    data.floatArray[gl_GlobalInvocationID.x].x = 45.0123f;
    data.floatArray[gl_GlobalInvocationID.x].y = 45.0123f;
    data.floatArray[gl_GlobalInvocationID.x].z = 45.0123f;
}

Data being passed

struct DataObject
{
    glm::vec3 payload[2];
};

...

Buffer payload_buffer;
VkDeviceSize buffer_size = sizeof(DataObject);
CreateBuffer(engine, buffer_size, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, payload_buffer);


// Create the data that will be saved
DataObject payload;

// Move the data to the GPU
memcpy(payload_buffer.mapped_memory, &payload, (::size_t)buffer_size);

The data that is returned is as follows

v1 x:45.0123f y:45.0123f z:45.0123f
v2 x:0 y:45.0123f z:45.0123f

Has anyone else come across this issue? and if so how would i resolve it?

\$\endgroup\$
1
  • \$\begingroup\$ It could be any number of things. It could have to do with synchronization, something that CreateBuffer is doing wrong, what your compute shader is doing, or many other things. There's no way we can diagnose your problem from this information. \$\endgroup\$ Oct 26, 2017 at 18:00

1 Answer 1

1
\$\begingroup\$

It's alignment. Vec3s in storage buffers must be aligned to 16 bytes (same as vec4).

replace DataObject with:

struct DataObject
{
    glm::vec4 payload[2];
};
\$\endgroup\$
2
  • \$\begingroup\$ That worked great thanks. If there a reference doc or something that shows the different data types that can and can't be passed to the storage buffer in this way? \$\endgroup\$
    – 0xen
    Oct 27, 2017 at 14:15
  • \$\begingroup\$ @0xen to be exact the default layout picked for buffer is std430 which aligns vec3 to 16 bytes. \$\endgroup\$ Oct 27, 2017 at 14:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .