简体   繁体   中英

Vulkan - vertex buffer update

I want to perform the vertex buffer update. I'm starting from empty buffer and I will add new points. The amount of added points is not frame-dependent. It can by one per frame, few or none.

In OpenGL I just allocated memory for all points which will be added (constant max size), use the glBufferSubData and modify the number of points which will be rendered (so only part of the buffer will be visible).

In Vulkan - I suppose I will need to use createBuffer with constant max size but what about modifications? I didn't found the "dedicated" approach for it. I think about sth like:

void* data;
vkMapMemory(src, data);
memcpy(data, modifiedInput);
vkUnmapMemory(src);

VkBufferCopy copyParams = {};
copyRegion.srcOffset = currOffset;//starting from last point in buffer
copyRegion.dstOffset = copyRegion.srcOffset;
copyRegion.size = size;

vkCmdCopyBuffer(src, dest, copyParams);
currOffset += size;

I'm not sure it's the correct way. Do I need to recreate the command buffers in this case or I should use totally different approach?

That is a good enough way to add data to a buffer.

Though there is a small bug. It should be.

VkBufferCopy copyParams = {};
copyRegion.srcOffset = 0; //start copy from start of src
copyRegion.dstOffset = currOffset; //starting from last point in existing buffer
copyRegion.size = size;

Assuming you only upload the new points. And ensure you don't overflow the buffer; splitting the copy into 2 copies when it wraps.

You can also use vkCmdUpdateBuffer to skip using the staging buffer. Though that should only be for small bits of data.

You still need some synchronization between the vkCmdCopyBuffer and where you use the new points.

You'll need to rerecord the command buffer per frame anyway to make use of the new amount of points (unless you use indirect rendering?).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM