openvr icon indicating copy to clipboard operation
openvr copied to clipboard

VulkanTextureWithArrayData Submit raises validation errors due to incorrect barrier usage

Open pushrax opened this issue 2 years ago • 1 comments

When submitting an image array with two layers, the left eye works, but the right eye causes validation errors. We submit the right eye with this code:

vr::VRVulkanTextureArrayData_t vulkanData;
// ...
vulkanData.m_unArraySize = 2; // undocumented - I assume this supposed to be the underlying array size? same errors happen when it’s set to 1 anyway
vulkanData.m_unArrayIndex = 1;

vr::Texture_t texture = {&vulkanData, vr::TextureType_Vulkan, vr::ColorSpace_Auto};
vr::VRCompositor()->Submit(vr::Eye_Right, &texture, 0, vr::Submit_VulkanTextureWithArrayData);

This triggers these Vulkan validation errors:

[ VUID-VkImageMemoryBarrier-subresourceRange-01488 ] Object 0: handle = 0x1d4e2e0000000062, type = VK_OBJECT_TYPE_IMAGE; | MessageID = 0x82a5db18 | vkCmdPipelineBarrier: subresourceRange.baseArrayLayer (= 1) is greater or equal to the arrayLayers of the image when it was created (i.e. greater or equal to 1). The Vulkan spec states: subresourceRange.baseArrayLayer must be less than the arrayLayers specified in VkImageCreateInfo when image was created (https://vulkan.lunarg.com/doc/view/1.2.182.0/windows/1.2-extensions/vkspec.html#VUID-VkImageMemoryBarrier-subresourceRange-01488)
[ VUID-VkImageMemoryBarrier-subresourceRange-01725 ] Object 0: handle = 0x1d4e2e0000000062, type = VK_OBJECT_TYPE_IMAGE; | MessageID = 0xe9dbfc10 | vkCmdPipelineBarrier: subresourceRange.baseArrayLayer + .layerCount (= 1 + 1 = 2) is greater than the arrayLayers of the image when it was created (i.e. greater than 1). The Vulkan spec states: If subresourceRange.layerCount is not VK_REMAINING_ARRAY_LAYERS, subresourceRange.baseArrayLayer + subresourceRange.layerCount must be less than or equal to the arrayLayers specified in VkImageCreateInfo when image was created (https://vulkan.lunarg.com/doc/view/1.2.182.0/windows/1.2-extensions/vkspec.html#VUID-VkImageMemoryBarrier-subresourceRange-01725)
[ UNASSIGNED-CoreValidation-DrawState-InvalidImageLayout ] Object 0: handle = 0x16e1635e4b8, type = VK_OBJECT_TYPE_COMMAND_BUFFER; | MessageID = 0x4dae5635 | Submitted command buffer expects VkImage 0x1d4e2e0000000062[] (subresource: aspectMask 0x1 array layer 0, mip level 0) to be in layout VK_IMAGE_LAYOUT_GENERAL--instead, current layout is VK_IMAGE_LAYOUT_PREINITIALIZED.

The image pointers referred to by these errors are different than the ones passed into the call to Submit. They refer to images that appear to be created inside OpenVR, with a single array layer. The image we submit definitely has two array layers.

Aside from the validation errors, the program appears to work fine and renders both eyes correctly from our array input. It looks to me like the vkCmdBlitImage in Submit works correctly, but there is an incorrect barrier. It tries to apply the barrier to layer 1, when it should be applying it to layer 0. This directly causes the first two validation errors. As a result, layer 0 is never transitioned to VK_IMAGE_LAYOUT_GENERAL, which causes the last validation error.

This looks like a bug, but might we be doing something wrong on the client side that prevents OpenVR from correctly handling array textures?

pushrax avatar Sep 20 '21 07:09 pushrax

I was able to work around this by submitting the left eye texture (at array index 0) to the right eye for the first 4 frames, which sets up the HMD swapchain images correctly. Then subsequent frames can use array index 1 for the right eye without issue.

vulkanData.m_unArrayIndex = (uint32)eye;

static int testFrameCount = 0;
if (testFrameCount < 4 && vrEye == vr::Eye_Right) {
    vulkanData.m_unArrayIndex = 0;
    testFrameCount++;
}

This makes it very clear there's a bug in the barrier setup code. Should be an easy fix, the barrier for the HMD swapchain image should always use array layer 0, rather than the layer passed in VRVulkanTextureArrayData_t.

cc @JoeLudwig @Plagman

pushrax avatar Sep 20 '21 20:09 pushrax