0
\$\begingroup\$

enter image description here

Why these numbers?? I have created a dynamic ubo and two objects, one for view and proj and the other for model.

Here I update the objects:

Render::UniformBufferObject ubo{};
        Render::UniformBufferObject2 ubo2{};
        

        ubo2.model = &model_transform.GetWorldMatrix();
        
        ubo.view =  &EditorCamera_transform.GetWorldMatrix().Inverse();
          
        ubo.proj = &current_camera->GetProjectionMatrix();

        // map memory without a staging buffer
        void* data;
        auto memory = uniform_buffer->get_ubo_memory();
        Render::VulkanMemoryAllocator::get_instance()->map_buffer_allocation(memory[current_image], &data);
        memcpy(data, &ubo, sizeof(ubo));
        memcpy(data, &ubo2, sizeof(ubo2));
        Render::VulkanMemoryAllocator::get_instance()->unmap_buffer_allocation(memory[current_image]); 

Here binding and draw command:

std::array<uint32_t, 2> dynamicOffsets =
            { i * static_cast<uint32_t>(Render::StandardUniformBuffer::required_alignment(sizeof(Maths::Matrix4)*2)),
              i* static_cast<uint32_t>(Render::StandardUniformBuffer::required_alignment(sizeof(Maths::Matrix4)))
            };

            cmd.bindDescriptorSets(
                vk::PipelineBindPoint::eGraphics,
                *pipeline->get_pipeline_layout(),
                0,
                2,
                descriptor_set->get_descriptor_sets()->data(),
                2,
                dynamicOffsets.data());

            cmd.drawIndexed(static_cast<uint32_t>(index_buffer->get_primative_indices_size()), 1, 0, 0, 0);
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It's actually hard to say anything for sure without knowing a lot more about your code base, but do Render::UniformBufferObject and Render::UniformBufferObject2 consist of pointers to matrices, and not actual matrices (unless the & operator is overloaded somehow) ?

You're not supposed to write pointers to values into UBOs and expect the GPU to dereference them.

You also seem to be overwriting the content you wrote from ubo with contents in ubo2 ; data points to the same location in both of the memcpy calls.

Assuming Render::UniformBufferObject and Render::UniformBufferObject2 actually consists of matrix value members:

struct Render::UniformBufferObject {
  glm::mat4 view, proj
};
struct Render::UniformBufferObject2 {
  glm::mat4 model
};

Here's how I'd write the values to the uniform buffer, assuming Render::StandardUniformBuffer::required_alignment works the way I expect it to:

// Assuming uniform_buffer has enough size taking UBO offset alignment into account.
void* data;
auto memory = uniform_buffer->get_ubo_memory();
Render::VulkanMemoryAllocator::get_instance()->map_buffer_allocation(memory[current_image], &data);

// Write to the location we'll be telling vkCmdBindDescriptorSets in the form of dynamic offsets.
auto& ubo = reinterpret_cast<Render::UniformBufferObject*>(static_cast<uint8_t*>(data) + 0);
auto& ubo2 = reinterpret_cast<Render::UniformBufferObject2*>(static_cast<uint8_t*>(data) + Render::StandardUniformBuffer::required_alignment(sizeof(Render::UniformBufferObject)));

ubo.view = EditorCamera_transform.GetWorldMatrix().Inverse();
ubo.proj = current_camera->GetProjectionMatrix();
ubo2.model = model_transform.GetWorldMatrix();

// Remember to call vkFlushMappedMemoryRanges if the backing memory is NOT host coherent.

Render::VulkanMemoryAllocator::get_instance()->unmap_buffer_allocation(memory[current_image]);

And the dynamic offsets for vkCmdBindDescriptorSets:

// Offsets where we wrote the values:
std::array<uint32_t, 2> dynamicOffsets = {
  0,
  Render::StandardUniformBuffer::required_alignment(sizeof(Render::UniformBufferObject))
};
\$\endgroup\$

You must log in to answer this question.

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