🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Vulkan - How to efficiently copy data to CPU *and* wait for it

Started by
1 comment, last by AndreyVK_D3D 3 years, 7 months ago

Let's say I want to execute the following commands:

cmd_buff start
dispatch (write to texture1)
copy (texture1 on gpu to buffer1 host-visible)
dispatch (write to texture2)
cmd_buff end

I'd like to know as soon as possible when buffer1's data are available.

My idea here is to have a waiting thread on which I'd wait for the copy to have completed. What I'd do is first split the above list of cmds into:

cmd_buff_1 start
dispatch (write to texture1)
copy (texture1 on gpu to buffer1 host-visible)
cmd_buff_1 end

and:

cmd_buff_2 start
dispatch (write to texture2)
cmd_buff_2 end

Now, I'd call vkQueueSubmit with cmd_buff_1 and with some fence1, followed by a call to another vkQueueSubmit with cmd_buff_2 with NULL fence.

On the waiting thread I'd call vkWaitForFences( fence1 ).

That's how I see such an operation. However, I'm wondering if that is optimal and if there was actually any way to put a direct sync still within cmd_buff_1 so that I wouldn't need to split the cmd buffer into two?

Advertisement

maxest said:
cmd_buff start

cmd_buff_1 start dispatch (write to texture1)

copy (texture1 on gpu to buffer1 host-visible)

cmd_buff_1 end

First of all you need to submit compute operation before reading data from GPU.

cmd_buff start

cmd_buff_1 start dispatch (write to texture1)

cmd_buff_1 end

submit(cmd_buff_1 )

Next, copy data from GPU to Host visible buffer. Try to use Transfer VkQueue(VK_QUEUE_TRANSFER_BIT) to improve performance, don't forget about Memory barrier for texture1 VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL:

cmd_copy_buff start

memoryBarrier(original_image_layout, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);

copy (texture1 on gpu to buffer1 host-visible)

cmd_copy_buff end

submit(cmd_copy_buff)

3DGraphics,Direct3D12,Vulkan,OpenCL,Algorithms

This topic is closed to new replies.

Advertisement