When my scene updates (e.g a new model introduced), I re-record my draw commmand buffers into new command buffers (tempBuffer) using the function buildCommandBuffers.
I have an array of drawCmdBuffers which are used in the main draw routine.
To complicate matters slightly, my buildCommandBuffers function could be getting called from a timer in a separate thread, so the render function is potentially still looping while this update occurs. This is because my scene update can take a while and I need to lock the scene memory while I create the command buffer (this is a separate problem.. but every mesh added triggers a scene update and I need to be able to buffer those updates).
I have a fence for each command buffer, so once I've recorded into my "tempBuffer", I do the following:
VK_CHECK_RESULT(vkEndCommandBuffer(tempBuffers[i]));
// wait for this command buffer to finish (we might be re-recording)
if (imagesInFlight[_image_index] != VK_NULL_HANDLE)
{
vkWaitForFences(device, 1, &imagesInFlight[_image_index], VK_TRUE, UINT64_MAX);
}
// destroy in use command buffer
vkFreeCommandBuffers(device, cmdPool, 1, &drawCmdBuffers[i]);
drawCmdBuffers[i] = tempBuffers[i];
Is this safe? Or is there something I can do to stop access to drawCmdBuffers[i] while I'm updating it?