Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update texture for every frame in vulkan?

As my question title says, I want update texture for every frame.

I got an idea : create a VkImage as a texture buffer with bellow configurations :
initialLayout = VK_IMAGE_LAYOUT_PREINITIALIZED
usage= VK_IMAGE_USAGE_SAMPLED_BIT

and it's memory type is VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT

In draw loop :

first frame :

  1. map texure data to VkImage(use vkMapMemory).
  2. change VkImage layout from VK_IMAGE_LAYOUT_PREINITIALIZED to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL.
  3. use this VkImage as texture buffer.

second frame:

The layout will be VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL after the first frame , can I map next texure data to this VkImage directly without change it's layout ? if I can not do that, which layout can I change this VkImage to ?

In vkspec 11.4 it says :

The new layout used in a transition must not be VK_IMAGE_LAYOUT_UNDEFINED or VK_IMAGE_LAYOUT_PREINITIALIZED

So , I can not change layout back to _PREINITIALIZED .
Any help will be appreciated.

like image 978
libei Avatar asked Nov 13 '16 14:11

libei


1 Answers

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.
like image 190
krOoze Avatar answered Sep 28 '22 02:09

krOoze