Hash :
e755a537
Author :
Date :
2019-04-10T09:58:21
Vulkan: Add a new garbage type gated by fences. This allows Vulkan EGL objects such as EGL Syncs and EGL Images to give their garbage to the renderer before destroying. BUG=angleproject:2464 Change-Id: I59b8e1080e4292bd0856e59a928750c7e77a372e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1562522 Commit-Queue: Geoff Lang <geofflang@chromium.org> Reviewed-by: Jamie Madill <jmadill@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
//
// 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.
//
// SyncVk.cpp:
// Implements the class methods for SyncVk.
//
#include "libANGLE/renderer/vulkan/SyncVk.h"
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/DisplayVk.h"
namespace rx
{
FenceSyncVk::FenceSyncVk() {}
FenceSyncVk::~FenceSyncVk() {}
void FenceSyncVk::onDestroy(RendererVk *renderer)
{
if (mEvent.valid())
{
renderer->releaseObject(renderer->getCurrentQueueSerial(), &mEvent);
}
mFence.reset(renderer->getDevice());
}
void FenceSyncVk::onDestroy(DisplayVk *display)
{
std::vector<vk::GarbageObjectBase> garbage;
mEvent.dumpResources(&garbage);
display->getRenderer()->addGarbage(std::move(mFence), std::move(garbage));
}
angle::Result FenceSyncVk::initialize(ContextVk *contextVk)
{
ASSERT(!mEvent.valid());
RendererVk *renderer = contextVk->getRenderer();
VkDevice device = renderer->getDevice();
VkEventCreateInfo eventCreateInfo = {};
eventCreateInfo.sType = VK_STRUCTURE_TYPE_EVENT_CREATE_INFO;
eventCreateInfo.flags = 0;
vk::Scoped<vk::Event> event(device);
ANGLE_VK_TRY(contextVk, event.get().init(device, eventCreateInfo));
ANGLE_TRY(renderer->getSubmitFence(contextVk, &mFence));
mEvent = event.release();
renderer->getCommandGraph()->setFenceSync(mEvent);
return angle::Result::Continue;
}
angle::Result FenceSyncVk::clientWait(vk::Context *context,
ContextVk *contextVk,
bool flushCommands,
uint64_t timeout,
VkResult *outResult)
{
RendererVk *renderer = context->getRenderer();
// If the event is already set, don't wait
bool alreadySignaled = false;
ANGLE_TRY(getStatus(context, &alreadySignaled));
if (alreadySignaled)
{
*outResult = VK_EVENT_SET;
return angle::Result::Continue;
}
// If timeout is zero, there's no need to wait, so return timeout already.
if (timeout == 0)
{
*outResult = VK_TIMEOUT;
return angle::Result::Continue;
}
if (flushCommands && contextVk)
{
ANGLE_TRY(contextVk->flushImpl());
}
// Wait on the fence that's expected to be signaled on the first vkQueueSubmit after
// `initialize` was called.
VkResult status = mFence.get().wait(renderer->getDevice(), timeout);
// Check for errors, but don't consider timeout as such.
if (status != VK_TIMEOUT)
{
ANGLE_VK_TRY(context, status);
}
*outResult = status;
return angle::Result::Continue;
}
angle::Result FenceSyncVk::serverWait(vk::Context *context, ContextVk *contextVk)
{
context->getRenderer()->getCommandGraph()->waitFenceSync(mEvent);
return angle::Result::Continue;
}
angle::Result FenceSyncVk::getStatus(vk::Context *context, bool *signaled)
{
VkResult result = mEvent.getStatus(context->getRenderer()->getDevice());
if (result != VK_EVENT_SET && result != VK_EVENT_RESET)
{
ANGLE_VK_TRY(context, result);
}
*signaled = result == VK_EVENT_SET;
return angle::Result::Continue;
}
SyncVk::SyncVk() : SyncImpl() {}
SyncVk::~SyncVk() {}
void SyncVk::onDestroy(const gl::Context *context)
{
mFenceSync.onDestroy(vk::GetImpl(context)->getRenderer());
}
angle::Result SyncVk::set(const gl::Context *context, GLenum condition, GLbitfield flags)
{
ASSERT(condition == GL_SYNC_GPU_COMMANDS_COMPLETE);
ASSERT(flags == 0);
return mFenceSync.initialize(vk::GetImpl(context));
}
angle::Result SyncVk::clientWait(const gl::Context *context,
GLbitfield flags,
GLuint64 timeout,
GLenum *outResult)
{
ContextVk *contextVk = vk::GetImpl(context);
ASSERT((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) == 0);
bool flush = (flags & GL_SYNC_FLUSH_COMMANDS_BIT) != 0;
VkResult result;
ANGLE_TRY(mFenceSync.clientWait(contextVk, contextVk, flush, static_cast<uint64_t>(timeout),
&result));
switch (result)
{
case VK_EVENT_SET:
*outResult = GL_ALREADY_SIGNALED;
return angle::Result::Continue;
case VK_SUCCESS:
*outResult = GL_CONDITION_SATISFIED;
return angle::Result::Continue;
case VK_TIMEOUT:
*outResult = GL_TIMEOUT_EXPIRED;
return angle::Result::Incomplete;
default:
UNREACHABLE();
*outResult = GL_WAIT_FAILED;
return angle::Result::Stop;
}
}
angle::Result SyncVk::serverWait(const gl::Context *context, GLbitfield flags, GLuint64 timeout)
{
ASSERT(flags == 0);
ASSERT(timeout == GL_TIMEOUT_IGNORED);
ContextVk *contextVk = vk::GetImpl(context);
return mFenceSync.serverWait(contextVk, contextVk);
}
angle::Result SyncVk::getStatus(const gl::Context *context, GLint *outResult)
{
bool signaled = false;
ANGLE_TRY(mFenceSync.getStatus(vk::GetImpl(context), &signaled));
*outResult = signaled ? GL_SIGNALED : GL_UNSIGNALED;
return angle::Result::Continue;
}
EGLSyncVk::EGLSyncVk(const egl::AttributeMap &attribs) : EGLSyncImpl()
{
ASSERT(attribs.isEmpty());
}
EGLSyncVk::~EGLSyncVk() {}
void EGLSyncVk::onDestroy(const egl::Display *display)
{
mFenceSync.onDestroy(vk::GetImpl(display)->getRenderer());
}
egl::Error EGLSyncVk::initialize(const egl::Display *display,
const gl::Context *context,
EGLenum type)
{
ASSERT(type == EGL_SYNC_FENCE_KHR);
ASSERT(context != nullptr);
if (mFenceSync.initialize(vk::GetImpl(context)) == angle::Result::Stop)
{
return egl::Error(EGL_BAD_ALLOC, "eglCreateSyncKHR failed to create sync object");
}
return egl::NoError();
}
egl::Error EGLSyncVk::clientWait(const egl::Display *display,
const gl::Context *context,
EGLint flags,
EGLTime timeout,
EGLint *outResult)
{
ASSERT((flags & ~EGL_SYNC_FLUSH_COMMANDS_BIT_KHR) == 0);
bool flush = (flags & EGL_SYNC_FLUSH_COMMANDS_BIT_KHR) != 0;
VkResult result;
ContextVk *contextVk = context ? vk::GetImpl(context) : nullptr;
if (mFenceSync.clientWait(vk::GetImpl(display), contextVk, flush,
static_cast<uint64_t>(timeout), &result) == angle::Result::Stop)
{
return egl::Error(EGL_BAD_ALLOC);
}
switch (result)
{
case VK_EVENT_SET:
// fall through. EGL doesn't differentiate between event being already set, or set
// before timeout.
case VK_SUCCESS:
*outResult = EGL_CONDITION_SATISFIED_KHR;
return egl::NoError();
case VK_TIMEOUT:
*outResult = EGL_TIMEOUT_EXPIRED_KHR;
return egl::NoError();
default:
UNREACHABLE();
*outResult = EGL_FALSE;
return egl::Error(EGL_BAD_ALLOC);
}
}
egl::Error EGLSyncVk::serverWait(const egl::Display *display,
const gl::Context *context,
EGLint flags)
{
ASSERT(flags == 0);
ContextVk *contextVk = context ? vk::GetImpl(context) : nullptr;
if (mFenceSync.serverWait(vk::GetImpl(display), contextVk) == angle::Result::Stop)
{
return egl::Error(EGL_BAD_ALLOC);
}
return egl::NoError();
}
egl::Error EGLSyncVk::getStatus(const egl::Display *display, EGLint *outStatus)
{
bool signaled = false;
if (mFenceSync.getStatus(vk::GetImpl(display), &signaled) == angle::Result::Stop)
{
return egl::Error(EGL_BAD_ALLOC);
}
*outStatus = signaled ? EGL_SIGNALED_KHR : EGL_UNSIGNALED_KHR;
return egl::NoError();
}
} // namespace rx