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

  1. Create your VkImage with UNDEFINED

1st to Nth frame (aka render loop)

  1. Transition image to GENERAL
  2. Synchronize (likely with VkFence)
  3. Map the image, write it, unmap it (weell, the mapping and unmaping can perhaps be outside render loop)
  4. Synchronize (potentially done implicitly)
  5. Transition image to whatever layout you need next
  6. Do your rendering and whatnot
  7. 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

  1. Create your VkBuffer b with UNDEFINED backed by HOST_VISIBLE memory and map it
  2. Create your VkImage i with UNDEFINED backed by DEVICE_LOCAL memory
  3. Prepare your synchronization primitives between i and b: 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:

  1. Transition b to GENERAL
  2. Synchronize to CPU (likely waiting on VkFence or vkQueueIdle)
  3. invalidate(if non-coherent), write it, flush(if non-coherent)
  4. Synchronize to GPU (done implicitly if 3. before queue submission)
  5. Transition b to TRANSFER
  6. Synchronize to make sure i is not in use (likely waiting on a VkSemaphore)
  7. Transition i to TRANSFER
  8. Do vkCmdCopy* from b to i
  9. Synchronize to make known I am finished with i (likely signalling a VkSemaphore)
  10. 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:

  1. Synchronize to make sure i is free to use (likely waiting on a VkSemaphore)
  2. Transition i to whatever needed
  3. Do your rendering
  4. Synchronize to make known I am finished with i (likely signalling a VkSemaphore)
  5. 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. VkBufferViews 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.