Hash :
f0370a41
Author :
Date :
2025-09-09T19:44:57
Vulkan: Use the GENERAL layout if VK_KHR_unified_image_layouts This lets ANGLE simplify synchronization by generally being able to use memory barriers instead of listing image barriers separately. Although the more specific access masks from VK_KHR_synchronization2 is possibly necessary for some hardware to work optimally (VK_ACCESS_2_SHADER_SAMPLED_READ_BIT in particular). It also lets ANGLE optimize a very specific scenario. Take an image used in the following scenario: 1. Copy to image in a transfer operation 2. Sample from image in the fragment shader of render pass 1 3. Sample from image in the vertex shader of shader pass 2 When GENERAL is not used, there's a layout transition between steps 1 and 2, changing the layout from TRANSFER_DST to SHADER_READ_ONLY_OPTIMAL (with dst stage == fragment shader). Later, at step 3, we need to make sure the vertex shader at least waits for this layout transition to finish... a dependency which is not expressible in Vulkan: * There cannot be a dependency to step 1, because the layout transition is not necessarily done * There is no stage mask that signifies the end of a layout transition. Without GENERAL, ANGLE has no choice but to issue a fragment->vertex dependency before step 3, serializing render pass 2's vertex pass with render pass 1's fragment pass on tilers. When using the GENERAL layout instead, step 3 can issue a transfer->vertex memory barrier, including using a VkEvent, parallelizing the two render passes. The above optimization is possible after this change, but not yet implemented. Bug: angleproject:422982681 Change-Id: Ieaae6f92b8b7d1e9c80c810a759c64b1e81d2dc1 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6936485 Reviewed-by: Yuxin Hu <yuxinhu@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Charlie Lao <cclao@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// VulkanHelper.h : Helper for vulkan.
#ifndef ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_
#define ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_
#include <mutex>
#include "common/angleutils.h"
#include "common/vulkan/vk_headers.h"
#include "test_utils/ANGLETest.h"
#include "vulkan/vulkan_fuchsia_ext.h"
namespace angle
{
class VulkanQueueMutex
{
public:
void init(EGLDisplay dpy);
void lock();
void unlock();
private:
EGLDisplay display;
};
class VulkanHelper
{
public:
VulkanHelper();
~VulkanHelper();
void initialize(bool useSwiftshader, bool enableValidationLayers);
void initializeFromANGLE();
VkInstance getInstance() const { return mInstance; }
VkPhysicalDevice getPhysicalDevice() const { return mPhysicalDevice; }
VkDevice getDevice() const { return mDevice; }
VkQueue getGraphicsQueue() const { return mGraphicsQueue; }
std::unique_lock<VulkanQueueMutex> getGraphicsQueueLock()
{
if (mInitializedFromANGLE)
{
return std::unique_lock<VulkanQueueMutex>(mGraphicsQueueMutex);
}
return std::unique_lock<VulkanQueueMutex>();
}
VkResult createImage2D(VkFormat format,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags,
VkExtent3D extent,
VkImage *imageOut,
VkDeviceMemory *deviceMemoryOut,
VkDeviceSize *deviceMemorySizeOut,
VkImageCreateInfo *imageCreateInfoOut);
bool canCreateImageExternal(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags,
VkExternalMemoryHandleTypeFlagBits handleType) const;
VkResult createImage2DExternal(VkFormat format,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags,
const void *imageCreateInfoPNext,
VkExtent3D extent,
VkExternalMemoryHandleTypeFlags handleTypes,
VkImage *imageOut,
VkDeviceMemory *deviceMemoryOut,
VkDeviceSize *deviceMemorySizeOut);
// VK_KHR_external_memory_fd
bool canCreateImageOpaqueFd(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags) const;
VkResult createImage2DOpaqueFd(VkFormat format,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags,
const void *imageCreateInfoPNext,
VkExtent3D extent,
VkImage *imageOut,
VkDeviceMemory *deviceMemoryOut,
VkDeviceSize *deviceMemorySizeOut);
VkResult exportMemoryOpaqueFd(VkDeviceMemory deviceMemory, int *fd);
// VK_FUCHSIA_external_memory
bool canCreateImageZirconVmo(VkFormat format,
VkImageType type,
VkImageTiling tiling,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags) const;
VkResult createImage2DZirconVmo(VkFormat format,
VkImageCreateFlags createFlags,
VkImageUsageFlags usageFlags,
const void *imageCreateInfoPNext,
VkExtent3D extent,
VkImage *imageOut,
VkDeviceMemory *deviceMemoryOut,
VkDeviceSize *deviceMemorySizeOut);
VkResult exportMemoryZirconVmo(VkDeviceMemory deviceMemory, zx_handle_t *vmo);
// VK_KHR_external_semaphore_fd
bool canCreateSemaphoreOpaqueFd() const;
VkResult createSemaphoreOpaqueFd(VkSemaphore *semaphore);
VkResult exportSemaphoreOpaqueFd(VkSemaphore semaphore, int *fd);
// VK_FUCHSIA_external_semaphore
bool canCreateSemaphoreZirconEvent() const;
VkResult createSemaphoreZirconEvent(VkSemaphore *semaphore);
VkResult exportSemaphoreZirconEvent(VkSemaphore semaphore, zx_handle_t *event);
// Performs a queue ownership transfer to VK_QUEUE_FAMILY_EXTERNAL on an
// image owned by our instance. The current image layout must be |oldLayout|
// and will be in |newLayout| after the memory barrier. |semaphore|
// will be signaled upon completion of the release operation.
void releaseImageAndSignalSemaphore(VkImage image,
VkImageLayout oldLayout,
VkImageLayout newLayout,
VkSemaphore semaphore);
// Just signal the given semaphore
void signalSemaphore(VkSemaphore semaphore);
// Performs a queue ownership transfer from VK_QUEUE_FAMILY_EXTERNAL on an
// image owned by an external instance. The current image layout must be
// |oldLayout| and will be in |newLayout| after the memory barrier. The
// barrier will wait for |semaphore|.
void waitSemaphoreAndAcquireImage(VkImage image,
VkImageLayout oldLayout,
VkImageLayout newLayout,
VkSemaphore semaphore);
// Writes pixels into an image. Currently only VK_FORMAT_R8G8B8A8_UNORM
// and VK_FORMAT_B8G8R8A8_UNORM formats are supported.
void writePixels(VkImage dstImage,
VkImageLayout srcImageLayout,
VkFormat imageFormat,
VkOffset3D imageOffset,
VkExtent3D imageExtent,
const void *pixels,
size_t pixelsSize);
// Copies pixels out of an image. Currently only VK_FORMAT_R8G8B8A8_UNORM
// and VK_FORMAT_B8G8R8A8_UNORM formats are supported.
void readPixels(VkImage srcImage,
VkImageLayout srcImageLayout,
VkFormat srcImageFormat,
VkOffset3D imageOffset,
VkExtent3D imageExtent,
void *pixels,
size_t pixelsSize);
bool useUnifiedImageLayouts() const { return mHasUnifiedImageLayouts; }
private:
bool mInitializedFromANGLE = false;
VkInstance mInstance = VK_NULL_HANDLE;
VkPhysicalDevice mPhysicalDevice = VK_NULL_HANDLE;
VkDevice mDevice = VK_NULL_HANDLE;
VkQueue mGraphicsQueue = VK_NULL_HANDLE;
VkCommandPool mCommandPool = VK_NULL_HANDLE;
VulkanQueueMutex mGraphicsQueueMutex;
VkPhysicalDeviceMemoryProperties mMemoryProperties = {};
uint32_t mGraphicsQueueFamilyIndex = UINT32_MAX;
bool mHasExternalMemoryFd = false;
bool mHasExternalMemoryFuchsia = false;
bool mHasExternalSemaphoreFd = false;
bool mHasExternalSemaphoreFuchsia = false;
bool mHasUnifiedImageLayouts = false;
PFN_vkGetPhysicalDeviceImageFormatProperties2 vkGetPhysicalDeviceImageFormatProperties2 =
nullptr;
PFN_vkGetMemoryFdKHR vkGetMemoryFdKHR = nullptr;
PFN_vkGetSemaphoreFdKHR vkGetSemaphoreFdKHR = nullptr;
ANGLE_MAYBE_UNUSED_PRIVATE_FIELD PFN_vkGetPhysicalDeviceExternalSemaphorePropertiesKHR
vkGetPhysicalDeviceExternalSemaphorePropertiesKHR = nullptr;
PFN_vkGetMemoryZirconHandleFUCHSIA vkGetMemoryZirconHandleFUCHSIA = nullptr;
PFN_vkGetSemaphoreZirconHandleFUCHSIA vkGetSemaphoreZirconHandleFUCHSIA = nullptr;
};
} // namespace angle
#endif // ANGLE_TESTS_TESTUTILS_VULKANHELPER_H_