Search code examples
vertex-shadervulkanuniform

create array of ubo and present each at a time to its shader


i want to create array of ubo object in my cpu update it and then upload it to the gpu in one call like that. (for the example lets say i have only two objects).

    std::vector<UniformBufferObject> ubo(m_allObject.size());

    int index = 0;
    for (RenderableObject* rendObj : m_allObject)
    {
        ubo[index].proj = m_camera->getProjection();
        ubo[index].view = m_camera->getView();
        ubo[index].model = rendObj->getTransform().getModel();
        ubo[index].proj[1][1] *= -1;
        index++;
    }

    int size = sizeof(UniformBufferObject) *m_allObject.size();
    void* data;
    m_instance->getLogicalDevice().mapMemory(ykEngine::Buffer::m_uniformBuffer.m_bufferMemory, 0, size , vk::MemoryMapFlags(), &data);
    memcpy(data, ubo.data(), size);
    m_instance->getLogicalDevice().unmapMemory(ykEngine::Buffer::m_uniformBuffer.m_bufferMemory);

i created one buffer with the size of two ubo. (the create do work because it do work with ubo in size one).

        vk::DeviceSize bufferSize = sizeof(UniformBufferObject) * 2;

        createBuffer(logicalDevice, bufferSize, vk::BufferUsageFlagBits::eUniformBuffer, vk::MemoryPropertyFlagBits::eHostVisible | vk::MemoryPropertyFlagBits::eHostCoherent, m_uniformBuffer.m_buffer, m_uniformBuffer.m_bufferMemory);

and than i put an offset in the descriptor set creation :

    vk::DescriptorBufferInfo bufferInfo;
    bufferInfo.buffer = uniformBuffer;
    bufferInfo.offset = offsetForUBO;
    bufferInfo.range = sizeof(UniformBufferObject);

the offset is the size of UniformBufferObject * the index of the object.

every object have is own descriptorsetLayout but the samepipline

when i try to update the descriptor set i get the error :

enter image description here

i couldnt find any aligment enum that specify that information.

if anyone know how to do that it will help alot.

thanks.


Solution

  • i couldnt find any aligment enum that specify that information.

    Vulkan is not OpenGL; you don't use enums to query limits. Limits are defined by the VkPhysicalDeviceLimits struct, queried via vkGetPhysicalDeviceProperties/2KHR.

    The error tells you exactly which limitation you violated: minUniformBufferOffsetAlignment. Your implementation set this to 0x100, but your provided offset was insufficient for this.

    Also, you should not map buffers in the middle of a frame. All mapping in Vulkan is "persistent"; map it once and leave it that way until you're ready to delete the memory.