Details on reading the depth buffer

I need to read out the depth buffer’s contents in response to a mouse click.
I’m using Sascha Willem’s example for reading a swap chain image to take a screenshot.

I recall there’s a transfer and or visible bit that must be set.
My code is checking that (VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT) are set.

I thought I was setting everything correctly.

VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT | VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
	VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;
_swapChain._depthImage.create(depthFormat, usage, _swapChain._extent.width, _swapChain._extent.height, _msaaSamples);

which then passes down to

_imageInfo.usage = usage;

if (vkCreateImage(dc.device_, &_imageInfo, nullptr, &_image) != VK_SUCCESS) {
	throw runtime_error("failed to create image!");
}

When I create the temporary buffer for the copy, the visible and coherent bits aren’t set. I’ve also tried

_imageInfo.usage = usage  |
	VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT;

When creating the temporary image - no luck so far.

The project and branch are located at

Any input would be appreciated.

This won’t work they way you’re passing the flags. You mix up image and memory flags, but memory flags can’t be passed as image usage flags. Memory flags can only be used when creating memory. Validation layers will tell you that that’s not valid usage.

So to make this work you need to set the memory property flags for the memory that backs the images, and not as the image usage flags.

First, thank you for the prompt reply.

I’m well aware that is the cause of the problem. That is why I’m seeking assistance.

It’s always easy to point out why someone else’s code doesn’t work. I came here looking for some insights into how to get it to work. Also, confirmation that it is possible before I waste more time pounding my head against a wall.

It’s perfectly possible. But you have to specify the memory flags when creating the memory backing the images instead of using them as image usage flags.

I already posted that solution above.