Hash :
e7418836
Author :
Date :
2023-08-16T14:25:52
Vulkan: Add context flushing as OOM fallback
* As a new fallback for out-of-memory errors, if an allocation results
in device OOM, the context is flushed and the allocation is retried.
* Functions related to buffer/image allocations now return a VkResult
value instead of angle::Result, which will be bubbled up to a higher
level for safer handling.
* The OOM is no longer handled at the level where the allocation
happens, but is moved up to the context.
* Added two functions to ContextVk for allocating memory for images and
buffer suballocations, which also include the fallback options.
* initBufferAllocation(): Uses BufferHelper::initSuballocation()
* initImageAllocation(): Uses ImageHelper::initMemory()
* Moved initNonZeroMemory() out of the following functions:
* BufferHelper::initSuballocation()
* Moved to ContextVk::initBufferAllocation().
* ImageHelper::initMemory()
* Moved to ContextVk::initImageAllocation().
* Also moved to new function:
ImageHelper::initMemoryAndNonZeroFillIfNeeded().
This function replaced the rest of initMemory() usages outside
initImageAllocation().
* New macros for memory allocation
* VK_RESULT_TRY()
* If the output of the command inside it is not VK_SUCCESS, it will
return with the error result from the command.
* VK_RESULT_CHECK()
* If the output of the command inside it is not VK_SUCCESS, it will
return with the input error.
* Added a test in which allocation would fail due to too much pending
garbage without the fix on some platforms. The test ends once there
has been a submission.
* New suite: UniformBufferMemoryTest
* Added a similar test for flushing texture-related pending garbage.
* New suite: Texture2DMemoryTestES3
Bug: b/280304441
Change-Id: I60248ce39eae80b5a8ffe4723d8a1c5641087f23
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4787949
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Amirali Abdolrashidi <abdolrashidi@google.com>
Reviewed-by: 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
//
// 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.
//
// OverlayVk.cpp:
// Implements the OverlayVk class.
//
#include "libANGLE/renderer/vulkan/OverlayVk.h"
#include "common/system_utils.h"
#include "libANGLE/Context.h"
#include "libANGLE/Overlay_font_autogen.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include <numeric>
namespace rx
{
OverlayVk::OverlayVk(const gl::OverlayState &state) : OverlayImpl(state) {}
OverlayVk::~OverlayVk() = default;
void OverlayVk::onDestroy(const gl::Context *context)
{
RendererVk *renderer = vk::GetImpl(context)->getRenderer();
VkDevice device = renderer->getDevice();
mFontImage.destroy(renderer);
mFontImageView.destroy(device);
}
angle::Result OverlayVk::createFont(ContextVk *contextVk)
{
RendererVk *renderer = contextVk->getRenderer();
// Create a buffer to stage font data upload.
VkBufferCreateInfo bufferCreateInfo = {};
bufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
bufferCreateInfo.size = gl::overlay::kFontTotalDataSize;
bufferCreateInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
bufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
vk::RendererScoped<vk::BufferHelper> fontDataBuffer(renderer);
ANGLE_TRY(fontDataBuffer.get().init(contextVk, bufferCreateInfo,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
uint8_t *mappedFontData;
ANGLE_TRY(fontDataBuffer.get().map(contextVk, &mappedFontData));
const uint8_t *fontData = mState.getFontData();
memcpy(mappedFontData, fontData, gl::overlay::kFontTotalDataSize * sizeof(*fontData));
ANGLE_TRY(fontDataBuffer.get().flush(renderer, 0, fontDataBuffer.get().getSize()));
fontDataBuffer.get().unmap(renderer);
// Don't use robust resource init for overlay widgets.
constexpr bool kNoRobustInit = false;
// Create the font image.
ANGLE_TRY(mFontImage.init(
contextVk, gl::TextureType::_2D,
VkExtent3D{gl::overlay::kFontGlyphWidth, gl::overlay::kFontGlyphHeight, 1},
renderer->getFormat(angle::FormatID::R8_UNORM), 1,
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT, gl::LevelIndex(0),
gl::overlay::kFontMipCount, gl::overlay::kFontCharacters, kNoRobustInit, false));
ANGLE_TRY(contextVk->initImageAllocation(&mFontImage, false, renderer->getMemoryProperties(),
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
vk::MemoryAllocationType::FontImage));
ANGLE_TRY(mFontImage.initImageView(
contextVk, gl::TextureType::_2DArray, VK_IMAGE_ASPECT_COLOR_BIT, gl::SwizzleState(),
&mFontImageView, vk::LevelIndex(0), gl::overlay::kFontMipCount,
vk::ImageHelper::kDefaultImageViewUsageFlags));
// Copy font data from staging buffer.
vk::CommandBufferAccess access;
access.onBufferTransferRead(&fontDataBuffer.get());
access.onImageTransferWrite(gl::LevelIndex(0), gl::overlay::kFontMipCount, 0,
gl::overlay::kFontCharacters, VK_IMAGE_ASPECT_COLOR_BIT,
&mFontImage);
vk::OutsideRenderPassCommandBuffer *fontDataUpload;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &fontDataUpload));
VkBufferImageCopy copy = {};
copy.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copy.imageSubresource.layerCount = gl::overlay::kFontCharacters;
copy.imageExtent.depth = 1;
for (uint32_t mip = 0; mip < gl::overlay::kFontMipCount; ++mip)
{
copy.bufferOffset = gl::overlay::kFontMipDataOffset[mip];
copy.bufferRowLength = gl::overlay::kFontGlyphWidth >> mip;
copy.bufferImageHeight = gl::overlay::kFontGlyphHeight >> mip;
copy.imageSubresource.mipLevel = mip;
copy.imageExtent.width = gl::overlay::kFontGlyphWidth >> mip;
copy.imageExtent.height = gl::overlay::kFontGlyphHeight >> mip;
fontDataUpload->copyBufferToImage(fontDataBuffer.get().getBuffer().getHandle(),
mFontImage.getImage(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©);
}
return angle::Result::Continue;
}
angle::Result OverlayVk::onPresent(ContextVk *contextVk,
vk::ImageHelper *imageToPresent,
const vk::ImageView *imageToPresentView,
bool is90DegreeRotation)
{
if (mState.getEnabledWidgetCount() == 0)
{
return angle::Result::Continue;
}
// Lazily initialize the font on first use
if (!mFontImage.valid())
{
ANGLE_TRY(createFont(contextVk));
}
RendererVk *renderer = contextVk->getRenderer();
vk::RendererScoped<vk::BufferHelper> textDataBuffer(renderer);
vk::RendererScoped<vk::BufferHelper> graphDataBuffer(renderer);
VkBufferCreateInfo textBufferCreateInfo = {};
textBufferCreateInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
textBufferCreateInfo.size = mState.getTextWidgetsBufferSize();
textBufferCreateInfo.usage = VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT;
textBufferCreateInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
VkBufferCreateInfo graphBufferCreateInfo = textBufferCreateInfo;
graphBufferCreateInfo.size = mState.getGraphWidgetsBufferSize();
ANGLE_TRY(textDataBuffer.get().init(contextVk, textBufferCreateInfo,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
ANGLE_TRY(graphDataBuffer.get().init(contextVk, graphBufferCreateInfo,
VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
uint8_t *textData;
uint8_t *graphData;
ANGLE_TRY(textDataBuffer.get().map(contextVk, &textData));
ANGLE_TRY(graphDataBuffer.get().map(contextVk, &graphData));
uint32_t textWidgetCount = 0;
uint32_t graphWidgetCount = 0;
gl::Extents presentImageExtents(imageToPresent->getExtents().width,
imageToPresent->getExtents().height, 1);
mState.fillWidgetData(presentImageExtents, textData, graphData, &textWidgetCount,
&graphWidgetCount);
ANGLE_TRY(textDataBuffer.get().flush(renderer, 0, textDataBuffer.get().getSize()));
ANGLE_TRY(graphDataBuffer.get().flush(renderer, 0, graphDataBuffer.get().getSize()));
textDataBuffer.get().unmap(renderer);
graphDataBuffer.get().unmap(renderer);
UtilsVk::OverlayDrawParameters params;
params.textWidgetCount = textWidgetCount;
params.graphWidgetCount = graphWidgetCount;
params.rotateXY = is90DegreeRotation;
return contextVk->getUtils().drawOverlay(contextVk, &textDataBuffer.get(),
&graphDataBuffer.get(), &mFontImage, &mFontImageView,
imageToPresent, imageToPresentView, params);
}
} // namespace rx