Hash :
0f75fc3d
Author :
Date :
2025-01-20T14:10:41
Vulkan: Transition foreign images to the FOREIGN queue on submit Vulkan's interaction with AHB and dmabuf images is through the FOREIGN queue family. When ANGLE uses these images, it must take ownership of the images by doing a queue family ownership transfer (QFOT) away from the FOREIGN queue family and into the graphics queue family used by the Vulkan backend. Prior to this change, ANGLE would do the QFOT away from FOREIGN once such a foreign image is imported into an EGL image. Afterwards, usage in ANGLE works correctly. What ANGLE did not handle is when a foreign entity wants to use these images _after_ ANGLE has used them. For the above to work correctly, ANGLE must do a QFOT back into FOREIGN before the image can be used by the foreign entity. Unfortunately, EGL does not provide a clear point for this hand-off to happen. ANGLE has no choice then to proactively transition the images back into FOREIGN at some point "just in case". For some native drivers, this hand-off to FOREIGN can be quite frequent. For example, on Android for most vendors there is no actual layout transition between graphics and FOREIGN queue families (the actual data layout is the same), so a cache flush/invalidate at strategic points (such as the end of the command buffer) is sufficient as equivalent to transition to FOREIGN (and another at the beginning of the command buffer as equivalent to transition from FOREIGN). As a layer over Vulkan's formalism, ANGLE is less lucky; it has to enumerate exactly which image is being transitioned to and away from FOREIGN. Transitions away from FOREIGN are in principle easy. As long as the image is marked as being in the FOREIGN queue family, it will automatically transition to the graphics queue family on first use. In this change, when a foreign image is transitioned out of the FOREIGN queue, it's added to a list of images to be transitioned back to FOREIGN at submit time. Once submission is done, the image may or may not actually be used by a foreign entity, but ANGLE cannot know that. The next time the image is used in ANGLE, it is transitioned out of FOREIGN. Verifying correctness with multi-threading is tricky, and relies on GL's requirement that access in one context is followed by a synchronization and rebind in another context before it can be used there. This means that the image's transition to FOREIGN (at the end of one submission) naturally happens before the transition back from FOREIGN (at the beginning of the next submission). Because the set of images to transition is tracked in the context, submissions in other contexts don't interfere with the above logic. The situation can be more complicated with one-off submissions, but fortunately, no such usage of foreign images is present. Another wrinkle is simultaneous usage of the image as read-only in two contexts. According to GL, this is not a hazard and requires no synchronization. However this is broken in ANGLE even for non-foreign images (see http://anglebug.com/42266349), because as what _seems_ like read-only usage of the image from GL's point of view (like sampling from the image), there are associated write operations from Vulkan's point of view (image layout transitions and QFOT). This change does not attempt to address this corner case. Bug: angleproject:42263241 Bug: angleproject:42262454 Bug: angleproject:390443243 Bug: chromium:382527242 Change-Id: Idd4ef1fecfa3fccf1a4063f1bddb08d28b85386b Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6184604 Reviewed-by: Charlie Lao <cclao@google.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org>
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 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
//
// Copyright 2016 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.
//
// RenderbufferVk.cpp:
// Implements the class methods for RenderbufferVk.
//
#include "libANGLE/renderer/vulkan/RenderbufferVk.h"
#include "libANGLE/Context.h"
#include "libANGLE/Image.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/ImageVk.h"
#include "libANGLE/renderer/vulkan/TextureVk.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"
namespace rx
{
namespace
{
angle::SubjectIndex kRenderbufferImageSubjectIndex = 0;
} // namespace
RenderbufferVk::RenderbufferVk(const gl::RenderbufferState &state)
: RenderbufferImpl(state),
mOwnsImage(false),
mImage(nullptr),
mImageObserverBinding(this, kRenderbufferImageSubjectIndex)
{}
RenderbufferVk::~RenderbufferVk() {}
void RenderbufferVk::onDestroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
releaseAndDeleteImage(contextVk);
}
angle::Result RenderbufferVk::setStorageImpl(const gl::Context *context,
GLsizei samples,
GLenum internalformat,
GLsizei width,
GLsizei height,
gl::MultisamplingMode mode)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Format &format = renderer->getFormat(internalformat);
angle::FormatID textureFormatID = format.getActualRenderableImageFormatID();
if (!mOwnsImage)
{
releaseAndDeleteImage(contextVk);
}
if (mImage != nullptr && mImage->valid())
{
// Check against the state if we need to recreate the storage.
if (internalformat != mState.getFormat().info->internalFormat ||
width != mState.getWidth() || height != mState.getHeight() ||
samples != mState.getSamples() || mode != mState.getMultisamplingMode())
{
releaseImage(contextVk);
}
}
if ((mImage != nullptr && mImage->valid()) || width == 0 || height == 0)
{
return angle::Result::Continue;
}
if (mImage == nullptr)
{
mImage = new vk::ImageHelper();
mOwnsImage = true;
mImageSiblingSerial = {};
mImageObserverBinding.bind(mImage);
mImageViews.init(renderer);
}
const angle::Format &textureFormat = format.getActualRenderableImageFormat();
const bool isDepthStencilFormat = textureFormat.hasDepthOrStencilBits();
ASSERT(textureFormat.redBits > 0 || isDepthStencilFormat);
const bool isRenderToTexture = mode == gl::MultisamplingMode::MultisampledRenderToTexture;
const bool hasRenderToTextureEXT =
renderer->getFeatures().supportsMultisampledRenderToSingleSampled.enabled;
// Transfer and sampled usage are used for various utilities such as readback or blit.
VkImageUsageFlags usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_TRANSFER_SRC_BIT |
VK_IMAGE_USAGE_SAMPLED_BIT;
// Renderbuffer's normal usage is as framebuffer attachment.
usage |= isDepthStencilFormat ? VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
: VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
// When used to emulate multisampled render to texture, it can be read as input attachment.
if (isRenderToTexture && !hasRenderToTextureEXT)
{
usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
}
// For framebuffer fetch and advanced blend emulation, color will be read as input attachment.
// For depth/stencil framebuffer fetch, depth/stencil will also be read as input attachment.
if (!isDepthStencilFormat ||
renderer->getFeatures().supportsShaderFramebufferFetchDepthStencil.enabled)
{
usage |= VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT;
}
VkImageCreateFlags createFlags = vk::kVkImageCreateFlagsNone;
if (isRenderToTexture &&
renderer->getFeatures().supportsMultisampledRenderToSingleSampled.enabled)
{
createFlags |= VK_IMAGE_CREATE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_BIT_EXT;
}
if (contextVk->getFeatures().limitSampleCountTo2.enabled)
{
samples = std::min(samples, 2);
}
const uint32_t imageSamples = isRenderToTexture ? 1 : samples;
bool robustInit = contextVk->isRobustResourceInitEnabled();
VkExtent3D extents = {static_cast<uint32_t>(width), static_cast<uint32_t>(height), 1u};
ANGLE_TRY(mImage->initExternal(
contextVk, gl::TextureType::_2D, extents, format.getIntendedFormatID(), textureFormatID,
imageSamples, usage, createFlags, vk::ImageLayout::Undefined, nullptr, gl::LevelIndex(0), 1,
1, robustInit, false, vk::YcbcrConversionDesc{}, nullptr));
VkMemoryPropertyFlags flags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
ANGLE_TRY(contextVk->initImageAllocation(mImage, false, renderer->getMemoryProperties(), flags,
vk::MemoryAllocationType::RenderBufferStorageImage));
// If multisampled render to texture, an implicit multisampled image is created which is used as
// the color or depth/stencil attachment. At the end of the render pass, this image is
// automatically resolved into |mImage| and its contents are discarded.
if (isRenderToTexture && !hasRenderToTextureEXT)
{
mMultisampledImageViews.init(renderer);
ANGLE_TRY(mMultisampledImage.initImplicitMultisampledRenderToTexture(
contextVk, false, renderer->getMemoryProperties(), gl::TextureType::_2D, samples,
*mImage, mImage->getExtents(), robustInit));
mRenderTarget.init(&mMultisampledImage, &mMultisampledImageViews, mImage, &mImageViews,
mImageSiblingSerial, gl::LevelIndex(0), 0, 1,
RenderTargetTransience::MultisampledTransient);
}
else
{
mRenderTarget.init(mImage, &mImageViews, nullptr, nullptr, mImageSiblingSerial,
gl::LevelIndex(0), 0, 1, RenderTargetTransience::Default);
}
return angle::Result::Continue;
}
angle::Result RenderbufferVk::setStorage(const gl::Context *context,
GLenum internalformat,
GLsizei width,
GLsizei height)
{
// The ES 3.0 spec(section 4.4.2.1) states that RenderbufferStorage is equivalent to calling
// RenderbufferStorageMultisample with samples equal to zero.
return setStorageImpl(context, 0, internalformat, width, height,
gl::MultisamplingMode::Regular);
}
angle::Result RenderbufferVk::setStorageMultisample(const gl::Context *context,
GLsizei samples,
GLenum internalformat,
GLsizei width,
GLsizei height,
gl::MultisamplingMode mode)
{
return setStorageImpl(context, samples, internalformat, width, height, mode);
}
angle::Result RenderbufferVk::setStorageEGLImageTarget(const gl::Context *context,
egl::Image *image)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
ANGLE_TRY(contextVk->getShareGroup()->lockDefaultContextsPriority(contextVk));
releaseAndDeleteImage(contextVk);
ImageVk *imageVk = vk::GetImpl(image);
mImage = imageVk->getImage();
mOwnsImage = false;
mImageSiblingSerial = imageVk->generateSiblingSerial();
mImageObserverBinding.bind(mImage);
mImageViews.init(renderer);
// Update ImageViewHelper's colorspace related state
EGLenum imageColorspaceAttribute = image->getColorspaceAttribute();
if (imageColorspaceAttribute != EGL_GL_COLORSPACE_DEFAULT_EXT)
{
egl::ImageColorspace imageColorspace =
(imageColorspaceAttribute == EGL_GL_COLORSPACE_SRGB_KHR) ? egl::ImageColorspace::SRGB
: egl::ImageColorspace::Linear;
ASSERT(mImage != nullptr);
mImageViews.updateEglImageColorspace(*mImage, imageColorspace);
}
mRenderTarget.init(mImage, &mImageViews, nullptr, nullptr, mImageSiblingSerial,
imageVk->getImageLevel(), imageVk->getImageLayer(), 1,
RenderTargetTransience::Default);
return angle::Result::Continue;
}
angle::Result RenderbufferVk::copyRenderbufferSubData(const gl::Context *context,
const gl::Renderbuffer *srcBuffer,
GLint srcLevel,
GLint srcX,
GLint srcY,
GLint srcZ,
GLint dstLevel,
GLint dstX,
GLint dstY,
GLint dstZ,
GLsizei srcWidth,
GLsizei srcHeight,
GLsizei srcDepth)
{
RenderbufferVk *sourceVk = vk::GetImpl(srcBuffer);
// Make sure the source/destination targets are initialized and all staged updates are flushed.
ANGLE_TRY(sourceVk->ensureImageInitialized(context));
ANGLE_TRY(ensureImageInitialized(context));
return vk::ImageHelper::CopyImageSubData(context, sourceVk->getImage(), srcLevel, srcX, srcY,
srcZ, mImage, dstLevel, dstX, dstY, dstZ, srcWidth,
srcHeight, srcDepth);
}
angle::Result RenderbufferVk::copyTextureSubData(const gl::Context *context,
const gl::Texture *srcTexture,
GLint srcLevel,
GLint srcX,
GLint srcY,
GLint srcZ,
GLint dstLevel,
GLint dstX,
GLint dstY,
GLint dstZ,
GLsizei srcWidth,
GLsizei srcHeight,
GLsizei srcDepth)
{
ContextVk *contextVk = vk::GetImpl(context);
TextureVk *sourceVk = vk::GetImpl(srcTexture);
// Make sure the source/destination targets are initialized and all staged updates are flushed.
ANGLE_TRY(sourceVk->ensureImageInitialized(contextVk, ImageMipLevels::EnabledLevels));
ANGLE_TRY(ensureImageInitialized(context));
return vk::ImageHelper::CopyImageSubData(context, &sourceVk->getImage(), srcLevel, srcX, srcY,
srcZ, mImage, dstLevel, dstX, dstY, dstZ, srcWidth,
srcHeight, srcDepth);
}
angle::Result RenderbufferVk::getAttachmentRenderTarget(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
ASSERT(mImage && mImage->valid());
*rtOut = &mRenderTarget;
return angle::Result::Continue;
}
angle::Result RenderbufferVk::initializeContents(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex)
{
// Note: stageSubresourceRobustClear only uses the intended format to count channels.
mImage->stageRobustResourceClear(imageIndex);
return mImage->flushAllStagedUpdates(vk::GetImpl(context));
}
void RenderbufferVk::releaseOwnershipOfImage(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
ASSERT(!mImageSiblingSerial.valid());
mOwnsImage = false;
releaseAndDeleteImage(contextVk);
}
void RenderbufferVk::releaseAndDeleteImage(ContextVk *contextVk)
{
releaseImage(contextVk);
SafeDelete(mImage);
mImageObserverBinding.bind(nullptr);
}
void RenderbufferVk::releaseImage(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
if (mImage == nullptr)
{
ASSERT(mImageViews.isImageViewGarbageEmpty() &&
mMultisampledImageViews.isImageViewGarbageEmpty());
}
else
{
mRenderTarget.releaseImageAndViews(contextVk);
mImageViews.release(renderer, mImage->getResourceUse());
mMultisampledImageViews.release(renderer, mImage->getResourceUse());
}
if (mImage && mOwnsImage)
{
mImage->releaseImageFromShareContexts(renderer, contextVk, mImageSiblingSerial);
mImage->releaseStagedUpdates(renderer);
}
else
{
if (mImage)
{
mImage->finalizeImageLayoutInShareContexts(renderer, contextVk, mImageSiblingSerial);
}
mImage = nullptr;
mImageObserverBinding.bind(nullptr);
}
if (mMultisampledImage.valid())
{
mMultisampledImage.releaseImageFromShareContexts(renderer, contextVk, mImageSiblingSerial);
}
}
const gl::InternalFormat &RenderbufferVk::getImplementationSizedFormat() const
{
GLenum internalFormat = mImage->getActualFormat().glInternalFormat;
return gl::GetSizedInternalFormatInfo(internalFormat);
}
GLenum RenderbufferVk::getColorReadFormat(const gl::Context *context)
{
const gl::InternalFormat &sizedFormat = getImplementationSizedFormat();
return sizedFormat.format;
}
GLenum RenderbufferVk::getColorReadType(const gl::Context *context)
{
const gl::InternalFormat &sizedFormat = getImplementationSizedFormat();
return sizedFormat.type;
}
angle::Result RenderbufferVk::getRenderbufferImage(const gl::Context *context,
const gl::PixelPackState &packState,
gl::Buffer *packBuffer,
GLenum format,
GLenum type,
void *pixels)
{
// Storage not defined.
if (!mImage || !mImage->valid())
{
return angle::Result::Continue;
}
ContextVk *contextVk = vk::GetImpl(context);
ANGLE_TRY(mImage->flushAllStagedUpdates(contextVk));
gl::MaybeOverrideLuminance(format, type, getColorReadFormat(context),
getColorReadType(context));
return mImage->readPixelsForGetImage(contextVk, packState, packBuffer, gl::LevelIndex(0), 0, 0,
format, type, pixels);
}
angle::Result RenderbufferVk::ensureImageInitialized(const gl::Context *context)
{
ANGLE_TRY(setStorageImpl(context, mState.getSamples(), mState.getFormat().info->internalFormat,
mState.getWidth(), mState.getHeight(), mState.getMultisamplingMode()));
return mImage->flushAllStagedUpdates(vk::GetImpl(context));
}
void RenderbufferVk::onSubjectStateChange(angle::SubjectIndex index, angle::SubjectMessage message)
{
ASSERT(index == kRenderbufferImageSubjectIndex &&
(message == angle::SubjectMessage::SubjectChanged ||
message == angle::SubjectMessage::InitializationComplete));
// Forward the notification to the parent class that the staging buffer changed.
if (message == angle::SubjectMessage::SubjectChanged)
{
onStateChange(angle::SubjectMessage::SubjectChanged);
}
}
} // namespace rx