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
VkImage
(use vkMapMemory
). VkImage
layout from VK_IMAGE_LAYOUT_PREINITIALIZED
to VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
. VkImage
as texture buffer.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.
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
VkImage
with LAYOUT_UNDEFINED
1st to Nth frame (aka render loop)
LAYOUT_GENERAL
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
VkBuffer
b
backed by HOST_VISIBLE
memory and map itVkImage
i
with LAYOUT_UNDEFINED
backed by DEVICE_LOCAL
memoryi
and b
: E.g. two Semaphores, or Events could be used or Barriers if the transfer is in the same queue1st to Nth frame (aka render loop)
Operations on b
and i
can be pretty detached (even can be on different queues) so:
For b
:
i
to TRANSFER
, including proper synchronization in the associated BarriervkCmdCopy*
from b
to i
b
to make sure it is ready to be used again (likely with Fence-like sync primitive)For i
:
i
, per abovei
are finishedi
to whatever needed (including proper Barrier sync parameters)i
This makes i
independent of host. CPU can write new data (in b
), while i
is still busy being read.