Tuesday, September 27, 2022
HomeGame Developmentinstancing - Vulkan SSBO Array Of Structs

instancing – Vulkan SSBO Array Of Structs


I’m making an attempt to create a Vulkan Storage House Buffer Object which comprises an array of structs to be used in my vertex shader. I consider I am lacking one thing elementary right here as a result of the first struct within the array works however all others give rubbish knowledge. Heres what I am doing:

C++ I’ve obtained a easy struct outlined:

struct InstanceData {
    glm::mat4 mannequin;
};

Afterward I’ve a vector of those structs which I push knowledge into:

std::vector<InstanceData> instanceData;
instanceData.push_back(InstanceData());
instanceData.push_back(InstanceData());

And at last memcpy the information from our vector into our VkBuffer (managed by VMA):

memcpy(instanceStorageSpaceAllocations[currentImage]->GetMappedData(), instanceData.knowledge(), sizeof(InstanceData) * instanceData.measurement());

In our vertex shader, I’m trying to entry this knowledge like so:

format(std140, binding = 0) readonly buffer InstanceData {
    mat4 mannequin;
} ssbo[];

void foremost() {
    gl_Position = PushConstants.view_proj * ssbo[gl_InstanceIndex].mannequin * inPosition;
}

Again in C++, we VkCmdDrawIndexed with the quantity of situations at the moment in our instanceData vector:

vkCmdDrawIndexed(CmdBuffer, static_cast<uint32_t>(_GLTF->Indices.measurement()), 1, instanceData.measurement()-1, 0, 0);

So, nonetheless many situations of InstanceData we push into our instanceData vector shall be drawn by the GPU.

When organising our SSBO descriptor we do like so:

VkDescriptorBufferInfo bufferInfo = {};
    bufferInfo.buffer = StorageBuffers[i];
    bufferInfo.offset = 0;
    bufferInfo.vary = VK_WHOLE_SIZE;

VK_WHOLE_SIZE is used right here so it makes use of your complete measurement of the VkBuffer (as a result of we’ll resize the buffer when it turns into to small to carry all our InstanceData objects).

So this works for the first instanced draw (when instanceData.measurement() == 1), however any after leads to rubbish knowledge when accessing the SSBO from inside our shader.

Any concepts about what I am doing fallacious right here..?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments