How to update texture for every frame in vulkan?
For your case you do not need LAYOUT_PREINITIALIZED
. That would only complicate your code (forcing you to provide separate code for the first frame).
LAYOUT_PREINITIALIZED
is indeed a very special layout intended only for the start of the life of the Image. It is more useful for static textures.
Start with LAYOUT_UNDEFINED
and use LAYOUT_GENERAL
when you need to write the Image from CPU side.
I propose this scheme:
berfore render loop
- Create your
VkImage
withUNDEFINED
1st to Nth frame (aka render loop)
- Transition image to
GENERAL
- Synchronize (likely with
VkFence
) - Map the image, write it, unmap it (weell, the mapping and unmaping can perhaps be outside render loop)
- Synchronize (potentially done implicitly)
- Transition image to whatever layout you need next
- Do your rendering and whatnot
- start over at 1.
It is a naive implementation but should suffice for ordinary hobbyist uses.
Double buffered access can be implemented — that is e.g. VkBuffer
for CPU access and VkImage
of the same for GPU access. And VkCmdCopy*
must be done for the data hand-off.
It is not that much more complicated than the above approach and there can be some performance benefits (if you need those at your stage of your project). You usually want your resources in device local memory, which often is not also host visible.
It would go something like:
berfore render loop
- Create your
VkBuffer
b
withUNDEFINED
backed byHOST_VISIBLE
memory and map it - Create your
VkImage
i
withUNDEFINED
backed byDEVICE_LOCAL
memory - Prepare your synchronization primitives between
i
andb
: E.g. two Semaphores, or Events could be used or Barriers if the transfer is in the same queue
1st to Nth frame (aka render loop)
Operations on b
and i
can be pretty detached (even can be on different queues) so:
For b
:
- Transition
b
toGENERAL
- Synchronize to CPU (likely waiting on
VkFence
orvkQueueIdle
) - invalidate(if non-coherent), write it, flush(if non-coherent)
- Synchronize to GPU (done implicitly if 3. before queue submission)
- Transition
b
toTRANSFER
- Synchronize to make sure
i
is not in use (likely waiting on aVkSemaphore
) - Transition
i
toTRANSFER
- Do
vkCmdCopy*
fromb
toi
- Synchronize to make known I am finished with
i
(likely signalling aVkSemaphore
) - start over at 1.
(The fence at 2. and semaphore at 6. have to be pre-signalled or skipped for first frame to work)
For i
:
- Synchronize to make sure
i
is free to use (likely waiting on aVkSemaphore
) - Transition
i
to whatever needed - Do your rendering
- Synchronize to make known I am finished with
i
(likely signalling aVkSemaphore
) - start over at 1.
You have a number of problems here.
First:
create a VkImage as a texture buffer
There's no such thing. The equivalent of an OpenGL buffer texture is a Vulkan buffer view. This does not use a VkImage
of any sort. VkBufferView
s do not have an image layout.
Second, assuming that you are working with a VkImage
of some sort, you have recognized the layout problem. You cannot modify the memory behind the texture unless the texture is in the GENERAL
layout (among other things). So you have to force a transition to that, wait until the transition command has actually completed execution, then do your modifications.
Third, Vulkan is asynchronous in its execution, and unlike OpenGL, it will not hide this from you. The image in question may still be accessed by the shader when you want to change it. So usually, you need to double buffer these things.
On frame 1, you set the data for image 1, then render with it. On frame 2, you set the data for image 2, then render with it. On frame 3, you overwrite the data for image 1 (using events to ensure that the GPU has actually finished frame 1).
Alternatively, you can use double-buffering without possible CPU waiting, by using staging buffers. That is, instead of writing to images directly, you write to host-visible memory. Then you use a vkCmdCopyBufferToImage
command to copy that data into the image. This way, the CPU doesn't have to wait on events or fences to make sure that the image is in the GENERAL
layout before sending data.
And BTW, Vulkan is not OpenGL. Mapping of memory is always persistent; there's no reason to unmap a piece of memory if you're going to map it every frame.