Vulkan VMA buffer is empty

371 Views Asked by At

I have been trying to implement Vulkan Memory Allocator in my application, but all I get is a blank screen. I use deferred and HDR pipeline and I allocate all my attachments using VMA. I tried outputting solid color in the fragment shader and it worked, so I suppose that the problem is that my vertex and index buffers are empty. I use staging buffer to copy data into vertex buffer. I fill the staging buffer like this:

void* data;
vmaMapMemory(_allocator, stagingAllocation, &data);
memcpy(data, bufferData, bufferSize);
vmaUnmapMemory(_allocator, stagingAllocation);

I also tried using vertex buffer without staging buffer and setting the flags

allocCreateInfo.flags = VMA_ALLOCATION_CREATE_HOST_ACCESS_SEQUENTIAL_WRITE_BIT | VMA_ALLOCATION_CREATE_MAPPED_BIT;

and copying data directly to vertex buffer with

memcpy(allocInfo.pMappedData, bufferData, bufferSize);

but that does not seem to work either.

Full code:

VkBuffer stagingBuffer;
VkBuffer vertexBuffer;

VmaAllocation stagingAllocation = createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_SRC_BIT, VMA_MEMORY_USAGE_CPU_ONLY, stagingBuffer);

void* data;
vmaMapMemory(_allocator, stagingAllocation, &data);
memcpy(data, bufferData, bufferSize);
vmaUnmapMemory(_allocator, stagingAllocation);

VmaAllocation allocation = createBuffer(bufferSize, VK_BUFFER_USAGE_TRANSFER_DST_BIT | VK_BUFFER_USAGE_VERTEX_BUFFER_BIT, VMA_MEMORY_USAGE_GPU_ONLY, vertexBuffer);

copyBuffer(stagingBuffer, vertexBuffer, bufferSize);

vmaDestroyBuffer(_allocator, stagingBuffer, stagingAllocation);

createBuffer function:

VmaAllocation createBuffer(VkDeviceSize size, VkBufferUsageFlags usage, VmaMemoryUsage memoryUsage, VkBuffer &buffer) {
  VkBufferCreateInfo bufferInfo{};
  bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
  bufferInfo.size = size;
  bufferInfo.usage = usage;
  bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
  
  VmaAllocationCreateInfo allocCreateInfo = {};
  allocCreateInfo.usage = memoryUsage;
  
  VmaAllocation allocation;
  if (vmaCreateBuffer(_allocation, &bufferInfo, & allocCreateInfo, &buffer, &allocation, nullptr) != VK_SUCCESS) {
    throw std::runtime_error("Failed to create buffer");
  }

  return allocation;
}

If I set allocCreateInfo.flags to 0, I get weird flickering. I have looked at many examples and all of them seem to be similar to my code.

I should also mention that it was working before I started using VMA and I have only modified the code I listed (plus the same code for image creation, but that seems to work fine).

I am on MacOS, Vulkan 1.2.

Any help would be greatly appreciated.

EDIT:

So after some hours of debugging, I figured it out. The problem was not in my vertex buffers, but rather in uniform buffers. I used flag VMA_MEMORY_USAGE_CPU_TO_GPU. After setting it to VMA_MEMORY_USAGE_CPU_ONLY, it started to work. This is only a temporary solution, since I do not want to store all my uniform buffers in cpu memory. If somebody knows what is wrong, please let me know.

Thank you

EDIT 2:

So turns out that VMA does not use VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT or VK_MEMORY_PROPERTY_HOST_COHERENT_BIT automatically when you set usage to be VMA_MEMORY_USAGE_CPU_TO_GPU. You need to include these flags in allocCreateInfo.requiredFlags. After doing this it works fine. However, I could not find this in the docs, so it was quite hard to debug.

0

There are 0 best solutions below