Hash :
cd901cdd
Author :
Date :
2023-03-03T17:04:27
Vulkan: Add and use rx::vk::ReleasableResource class.
This is a follow up for CL:
Vulkan: Enforce ContextPriority in ShareGroup and with EGLImage
Bug: angleproject:8039
Change-Id: I9e654557d4a1ce9aee4b04f1211eeb6ae3f0e482
Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4306721
Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org>
Reviewed-by: Charlie Lao <cclao@google.com>
Commit-Queue: Igor Nazarov <i.nazarov@samsung.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 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
//
// Copyright 2017 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.
//
// ResourceVk:
// Resource lifetime tracking in the Vulkan back-end.
//
#ifndef LIBANGLE_RENDERER_VULKAN_RESOURCEVK_H_
#define LIBANGLE_RENDERER_VULKAN_RESOURCEVK_H_
#include "libANGLE/HandleAllocator.h"
#include "libANGLE/renderer/vulkan/vk_utils.h"
#include <queue>
namespace rx
{
namespace vk
{
// We expect almost all reasonable usage case should have at most 4 current contexts now. When
// exceeded, it should still work, but storage will grow.
static constexpr size_t kMaxFastQueueSerials = 4;
// Serials is an array of queue serials, which when paired with the index of the serials in the
// array result in QueueSerials. The array may expand if needed. Since it owned by Resource object
// which is protected by shared lock, it is safe to reallocate storage if needed. When it passes to
// renderer at garbage collection time, we will make a copy. The array size is expected to be small.
// But in future if we run into situation that array size is too big, we can change to packed array
// of QueueSerials.
using Serials = angle::FastVector<Serial, kMaxFastQueueSerials>;
// Tracks how a resource is used by ANGLE and by a VkQueue. The serial indicates the most recent use
// of a resource in the VkQueue. We use the monotonically incrementing serial number to determine if
// a resource is currently in use.
class ResourceUse final
{
public:
ResourceUse() = default;
~ResourceUse() = default;
ResourceUse(const QueueSerial &queueSerial) { setQueueSerial(queueSerial); }
ResourceUse(const Serials &otherSerials) { mSerials = otherSerials; }
// Copy constructor
ResourceUse(const ResourceUse &other) : mSerials(other.mSerials) {}
ResourceUse &operator=(const ResourceUse &other)
{
mSerials = other.mSerials;
return *this;
}
// Move constructor
ResourceUse(ResourceUse &&other) : mSerials(other.mSerials) { other.mSerials.clear(); }
ResourceUse &operator=(ResourceUse &&other)
{
mSerials = other.mSerials;
other.mSerials.clear();
return *this;
}
bool valid() const { return mSerials.size() > 0; }
void reset() { mSerials.clear(); }
const Serials &getSerials() const { return mSerials; }
void setSerial(SerialIndex index, Serial serial)
{
ASSERT(index != kInvalidQueueSerialIndex);
if (ANGLE_UNLIKELY(mSerials.size() <= index))
{
mSerials.resize(index + 1, kZeroSerial);
}
ASSERT(mSerials[index] <= serial);
mSerials[index] = serial;
}
void setQueueSerial(const QueueSerial &queueSerial)
{
setSerial(queueSerial.getIndex(), queueSerial.getSerial());
}
// Returns true if there is at least one serial is greater than
bool operator>(const AtomicQueueSerialFixedArray &serials) const
{
ASSERT(mSerials.size() <= serials.size());
for (SerialIndex i = 0; i < mSerials.size(); ++i)
{
if (mSerials[i] > serials[i])
{
return true;
}
}
return false;
}
// Returns true if it contains a serial that is greater than
bool operator>(const QueueSerial &queuSerial) const
{
return mSerials.size() > queuSerial.getIndex() &&
mSerials[queuSerial.getIndex()] > queuSerial.getSerial();
}
// Returns true if all serials are less than or equal
bool operator<=(const AtomicQueueSerialFixedArray &serials) const
{
ASSERT(mSerials.size() <= serials.size());
for (SerialIndex i = 0; i < mSerials.size(); ++i)
{
if (mSerials[i] > serials[i])
{
return false;
}
}
return true;
}
bool usedByCommandBuffer(const QueueSerial &commandBufferQueueSerial) const
{
ASSERT(commandBufferQueueSerial.valid());
// Return true if we have the exact queue serial in the array.
return mSerials.size() > commandBufferQueueSerial.getIndex() &&
mSerials[commandBufferQueueSerial.getIndex()] ==
commandBufferQueueSerial.getSerial();
}
// Merge other's serials into this object.
void merge(const ResourceUse &other)
{
if (mSerials.size() < other.mSerials.size())
{
mSerials.resize(other.mSerials.size(), kZeroSerial);
}
for (SerialIndex i = 0; i < other.mSerials.size(); ++i)
{
if (mSerials[i] < other.mSerials[i])
{
mSerials[i] = other.mSerials[i];
}
}
}
private:
// The most recent time of use in a VkQueue.
Serials mSerials;
};
std::ostream &operator<<(std::ostream &os, const ResourceUse &use);
class SharedGarbage
{
public:
SharedGarbage();
SharedGarbage(SharedGarbage &&other);
SharedGarbage(const ResourceUse &use, GarbageList &&garbage);
~SharedGarbage();
SharedGarbage &operator=(SharedGarbage &&rhs);
bool destroyIfComplete(RendererVk *renderer);
bool hasResourceUseSubmitted(RendererVk *renderer) const;
private:
ResourceUse mLifetime;
GarbageList mGarbage;
};
using SharedGarbageList = std::queue<SharedGarbage>;
// This is a helper class for back-end objects used in Vk command buffers. They keep a record
// of their use in ANGLE and VkQueues via ResourceUse.
class Resource : angle::NonCopyable
{
public:
virtual ~Resource() {}
// Complete all recorded and in-flight commands involving this resource
angle::Result waitForIdle(ContextVk *contextVk,
const char *debugMessage,
RenderPassClosureReason reason);
void setSerial(SerialIndex index, Serial serial) { mUse.setSerial(index, serial); }
void setQueueSerial(const QueueSerial &queueSerial)
{
mUse.setSerial(queueSerial.getIndex(), queueSerial.getSerial());
}
void mergeResourceUse(const ResourceUse &use) { mUse.merge(use); }
// Check if this resource is used by a command buffer.
bool usedByCommandBuffer(const QueueSerial &commandBufferQueueSerial) const
{
return mUse.usedByCommandBuffer(commandBufferQueueSerial);
}
const ResourceUse &getResourceUse() const { return mUse; }
protected:
Resource() {}
Resource(Resource &&other) : Resource() { mUse = std::move(other.mUse); }
Resource &operator=(Resource &&rhs)
{
std::swap(mUse, rhs.mUse);
return *this;
}
// Current resource lifetime.
ResourceUse mUse;
};
// Similar to |Resource| above, this tracks object usage. This includes additional granularity to
// track whether an object is used for read-only or read/write access.
class ReadWriteResource : public Resource
{
public:
virtual ~ReadWriteResource() override {}
// Complete all recorded and in-flight commands involving this resource
angle::Result waitForIdle(ContextVk *contextVk,
const char *debugMessage,
RenderPassClosureReason reason)
{
return Resource::waitForIdle(contextVk, debugMessage, reason);
}
void setWriteQueueSerial(const QueueSerial &writeQueueSerial)
{
mUse.setQueueSerial(writeQueueSerial);
mWriteUse.setQueueSerial(writeQueueSerial);
}
// Check if this resource is used by a command buffer.
bool usedByCommandBuffer(const QueueSerial &commandBufferQueueSerial) const
{
return mUse.usedByCommandBuffer(commandBufferQueueSerial);
}
bool writtenByCommandBuffer(const QueueSerial &commandBufferQueueSerial) const
{
return mWriteUse.usedByCommandBuffer(commandBufferQueueSerial);
}
const ResourceUse &getWriteResourceUse() const { return mWriteUse; }
protected:
ReadWriteResource() {}
ReadWriteResource(ReadWriteResource &&other) { *this = std::move(other); }
ReadWriteResource &operator=(ReadWriteResource &&other)
{
Resource::operator=(std::move(other));
mWriteUse = std::move(other.mWriteUse);
return *this;
}
// Track write use of the object. Only updated for setWriteQueueSerial().
ResourceUse mWriteUse;
};
// Adds "void release(RendererVk *)" method for collecting garbage.
// Enables RendererScoped<> for classes that support DeviceScoped<>.
template <class T>
class ReleasableResource final : public Resource
{
public:
// Calls collectGarbage() on the object.
void release(RendererVk *renderer);
const T &get() const { return mObject; }
T &get() { return mObject; }
private:
T mObject;
};
} // namespace vk
} // namespace rx
#endif // LIBANGLE_RENDERER_VULKAN_RESOURCEVK_H_