Buffer management with Vulkan : transfer, Staging buffer

Hi guys !
I keep my promise and I am coming with explanations and implementation on how to use and manage one (or several) buffer in Vulkan application.

How I manage my resources ?

shared_ptr?

Firstly, I have a way to manage Vulkan resource that is a bit weird. The idea is to “emulate” the behaviour of shared_ptr and enable copy / move.
So, if you do that :

b1 and b2 are exactly the same Vulkan’s Image.

A counter?

To emulate the behaviour of a shared_ptr, I created one class that is simply a Counter.

A Vulkan Resource

A Vulkan resource lives through a device. So I wrote this little class that represents a Vulkan Resource :

Buffer in Vulkan

Unlike OpenGL, buffers in Vulkan are separated from memory. You must bind the memory to them. Since you can choose if you want the memory on the device_local heap or in the host_visible, you can chose which heap your buffer will use.

So what are buffer made with ?

Buffers are made with a size, a usage (vertex? uniform ?), one block of memory, one ptr if the buffer is HOST_VISIBLE etc.
My buffer class is :

It may be is a bit complicate, but it is not really that difficult. A buffer will be created with an usage, one size and one boolean to put or not this buffer in device_local memory.
The creation of the buffer is quite simple. You just have to give the size and the usage :

The last line is to get the memory requirements. It will give you the real size you need (padding or other things) and list of memory types that can be used with the buffer.
To get the memory type index, I developed this function which cares about device local memory or host visible memory :

This code was made with the specifications themselves.
Now we should allocate memory for our buffers :

As you can see, you allocate the memory, and you bind the memory. If the memory is host visible, you can map it.

Now we have a class to manage our buffers. But it is not finished at all !

Staging resources

We cannot write directly to the device_local memory. We must use something that we call a staging resource. Staging resources can be buffers or images. The idea is to bind a host visible memory to a staging resource, and transfer the memory through the staging resource to a resource with memory that resides in device_local memory.

staging buffer

Command Buffers submitting

Before to transfer anything, I wanted to have a class that manages the submitting of command buffers. When the work is done, the command submitter should notify transferer object that use it. I used an observer pattern :

The code is not difficult, it allocates if needed one command buffer and return it and use fencing to know if works are completed.

Buffer transferer

You guessed that our Buffer transferer must implement the abstract class :

The idea is to have several buffers ready to transfer data. Why this idea? Because users may don’t care about the CPU buffer and only want a GPU Buffer ! Thanks to that, if he wants to transfer data like glBufferSubData, he actually can !

The code to transfer a buffer is not complicated at all. However, you just have to be careful about the memory barrier. Personally, I use one from Transfer to ALL_Commands in this case.

I do not manage the reallocation if the dst buffer is too small, or loop / recursion the transfer when our staging buffer is too small, but with our architecture, It would not be difficult to manage these cases !

How to use it??

Simply like that !

I saw that a high number of my visits comes from twitter. If you want to follow me: it is here.

Kisses and see you soon to see how to load / manage images !

15 thoughts on “Buffer management with Vulkan : transfer, Staging buffer”

  1. Hi ! I’m learning your lessons and appeared trouble:
    i’m trying compile your examples and compilator giving that error:
    C2280: ‘”std::unique_ptr<ShaderModule,std::default_delete> &std::unique_ptr<_Ty,std::default_delete>::operator =(const std::unique_ptr<_Ty,std::default_delete> &)”: attempting to reference a deleted function.

    I think that this’s due to the string: mShaders.emplace_back(std::make_unique(device, “../Shaders/shader_vert.spv”));
    If replace “unique_ptr” on “shared_ptr” it worked. But why?? Please answer me. What’s wrong?

    P.S. Visual Studio 2015

  2. Swear on this: “std::vector<std::unique_ptr> someVariable;”
    attempting to reference a deleted function.(C2280)
    with shared_ptr worked.

    1. Okay I do not know why it did not work on Visual Studio 2015. (Maybe it does not succeed to call the move semantic assignment operator?).
      But, I have fixed all my samples.
      Why? Because, actually, Pipeline already has a shaderModule member, and there is no need to use pointer here.
      Thanks :).

  3. Thank you! These lessons also allow more and learn the so-called modern C ++ e.g. default constructors, shared_ptr etc.
    I’m waiting for new lessons)))

  4. One more question: in file Fence.hpp in constructor: “Fence(Device const &device, bool signaled);” “const” statement comes after “Device”. But in Fence.cpp vice versa: “const” statement comes before “Device”. All right? It is not so important?

    1. I take a bad habbit to put the “const” after the type. But my IDE(QtCreator) prefers to put it before. So, when I use auto completion, I have the const after in the header, and before in the src file…
      I am beginning to clean it, but it will take some times I guess. But it is not important at all ;).

  5. Hello! I’m trying to run compiled your samples but an error (Only when compiled in Release mode) in the string:
    memcpy ((char *) (* mTransfererBuffers) [* mIndex] .getPtr () + (* mSizeAlreadyUsed) [* mIndex], data, size); (class Transferer)
    If I’m Compiled samples in Debug mode then all running easy (But in release mode error access violation when reading the address 0x00000000)
    Please help!

  6. I’m found, bug was in string: assert(!m_Chunks.back()->Allocate(size, alignment, block));
    In release mode assertion failed (result: assert(false)) but in debug mode assert(true), maybe this bind with compiler optimization. I’m removed assert and left just: m_Chunks.back()->Allocate(size, alignment, block)
    And all is ok
    Thank you for your trouble:)

  7. Hello! How i’m may add depth test in your examples? I’m adding vk::PipelineDepthStencilStateCreateInfo in Pipeline class then modifies vk::GraphicsPipelineCreateInfo ci. In RenderPass class adding depthAttachment, modifies vk::RenderPassCreateInfo. Change vk::ClearValue (add depth stencil). But finally application crashing.
    Please write tutorial about depth testing and add example 🙂
    Thanks you!

    1. Hello,
      I will write a tutorial about mipmap first.
      But maybe this week end, I will do one example using depth testing, but it is not sure right now :).

    1. In my repository, there is a project Scene, it is not finished yet and it is not “error free”, but it works and draw Sponza

Leave a Reply