Vulkan: sampling stencil value in fragment shader

Unfortunately I can’t find any guidelines to set up depth/stencil texture correctly to be able get its both depth and stencil values in fragment shader.

I know that OpenGL requires extension described in Stencil Texturing chapter.
But I don’t see any vulkan extensions for that purpose. Are there any?

I’ve also seen related topics, but they don’t have enough details:

  • How to read the stencil buffer in Vulkan?
  • DirectX11: Read stencil bit from compute shader

I tried to create 2 separate ImageView objects with VK_IMAGE_ASPECT_DEPTH_BIT and VK_IMAGE_ASPECT_STENCIL_BIT respectively for texture with format D32_S8.

But when I try to bind depthStencil texture as VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE using stencil ImageView, I get validation error:

command buffer VkCommandBuffer 0x559511d8a800[] expects VkImage 0xc4c0be0000000582[depthBuffer_1] (subresource: aspectMask 0x4 array layer 0, mip level 0) to be in layout VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL--instead, current layout is VK_IMAGE_LAYOUT_UNDEFINED

What seems strange because renderdoc shows that depthStencil image is bound and has VK_IMAGE_LAYOUT_DEPTH_STENCIL_READ_ONLY_OPTIMAL layout.

Leave a Comment