Hash :
d33a2222
Author :
Date :
2021-04-26T16:56:15
Upstream Apple's direct-to-Metal backend: compile libANGLE. This change is meant to merge the metal backend changes from Apple's direct-to-Metal backend. Taken from Kyle Piddington's CL: https://chromium-review.googlesource.com/c/angle/angle/+/2857366/ The goal of this CL is to merge the metal backend code in a state that compiles, but not to switch the Metal backend over to using the direct-to-metal backend yet. Bug: angleproject:5505 Bug: angleproject:6127 Change-Id: If6783e06e0086b3a1dd25c6f53caca5cfc96cb86 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2950067 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Jonah Ryan-Davis <jonahr@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 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
//
// Copyright (c) 2020 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.
//
// SyncMtl:
// Defines the class interface for SyncMtl, implementing SyncImpl.
//
#include "libANGLE/renderer/metal/SyncMtl.h"
#include <chrono>
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
namespace rx
{
namespace mtl
{
// SharedEvent is only available on iOS 12.0+ or mac 10.14+
#if ANGLE_MTL_EVENT_AVAILABLE
Sync::Sync() {}
Sync::~Sync() {}
void Sync::onDestroy()
{
mMetalSharedEvent = nil;
mCv = nullptr;
mLock = nullptr;
}
angle::Result Sync::initialize(ContextMtl *contextMtl)
{
ANGLE_MTL_OBJC_SCOPE
{
mMetalSharedEvent = [[contextMtl->getMetalDevice() newSharedEvent] ANGLE_MTL_AUTORELEASE];
}
mSetCounter = mMetalSharedEvent.get().signaledValue;
mCv.reset(new std::condition_variable());
mLock.reset(new std::mutex());
return angle::Result::Continue;
}
angle::Result Sync::set(ContextMtl *contextMtl, GLenum condition, GLbitfield flags)
{
if (!mMetalSharedEvent)
{
ANGLE_TRY(initialize(contextMtl));
}
ASSERT(condition == GL_SYNC_GPU_COMMANDS_COMPLETE);
ASSERT(flags == 0);
mSetCounter++;
contextMtl->queueEventSignal(mMetalSharedEvent, mSetCounter);
return angle::Result::Continue;
}
angle::Result Sync::clientWait(ContextMtl *contextMtl,
bool flushCommands,
uint64_t timeout,
GLenum *outResult)
{
std::unique_lock<std::mutex> lg(*mLock);
if (mMetalSharedEvent.get().signaledValue >= mSetCounter)
{
*outResult = GL_ALREADY_SIGNALED;
return angle::Result::Continue;
}
if (flushCommands)
{
contextMtl->flushCommandBufer();
}
if (timeout == 0)
{
*outResult = GL_TIMEOUT_EXPIRED;
return angle::Result::Continue;
}
// Create references to mutex and condition variable since they might be released in
// onDestroy(), but the callback might still not be fired yet.
std::shared_ptr<std::condition_variable> cvRef = mCv;
std::shared_ptr<std::mutex> lockRef = mLock;
AutoObjCObj<MTLSharedEventListener> eventListener =
contextMtl->getDisplay()->getOrCreateSharedEventListener();
[mMetalSharedEvent.get() notifyListener:eventListener
atValue:mSetCounter
block:^(id<MTLSharedEvent> sharedEvent, uint64_t value) {
std::unique_lock<std::mutex> localLock(*lockRef);
cvRef->notify_one();
}];
if (!mCv->wait_for(lg, std::chrono::nanoseconds(timeout),
[this] { return mMetalSharedEvent.get().signaledValue >= mSetCounter; }))
{
*outResult = GL_TIMEOUT_EXPIRED;
return angle::Result::Incomplete;
}
ASSERT(mMetalSharedEvent.get().signaledValue >= mSetCounter);
*outResult = GL_CONDITION_SATISFIED;
return angle::Result::Continue;
}
void Sync::serverWait(ContextMtl *contextMtl)
{
contextMtl->serverWaitEvent(mMetalSharedEvent, mSetCounter);
}
angle::Result Sync::getStatus(bool *signaled)
{
*signaled = mMetalSharedEvent.get().signaledValue >= mSetCounter;
return angle::Result::Continue;
}
#endif // #if defined(__IPHONE_12_0) || defined(__MAC_10_14)
} // namespace mtl
// FenceNVMtl implementation
FenceNVMtl::FenceNVMtl() : FenceNVImpl() {}
FenceNVMtl::~FenceNVMtl() {}
void FenceNVMtl::onDestroy(const gl::Context *context)
{
mSync.onDestroy();
}
angle::Result FenceNVMtl::set(const gl::Context *context, GLenum condition)
{
ASSERT(condition == GL_ALL_COMPLETED_NV);
ContextMtl *contextMtl = mtl::GetImpl(context);
return mSync.set(contextMtl, GL_SYNC_GPU_COMMANDS_COMPLETE, 0);
}
angle::Result FenceNVMtl::test(const gl::Context *context, GLboolean *outFinished)
{
bool signaled = false;
ANGLE_TRY(mSync.getStatus(&signaled));
*outFinished = signaled ? GL_TRUE : GL_FALSE;
return angle::Result::Continue;
}
angle::Result FenceNVMtl::finish(const gl::Context *context)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
uint64_t timeout = 1000000000ul;
GLenum result;
do
{
ANGLE_TRY(mSync.clientWait(contextMtl, true, timeout, &result));
} while (result == GL_TIMEOUT_EXPIRED);
if (result == GL_WAIT_FAILED)
{
UNREACHABLE();
return angle::Result::Stop;
}
return angle::Result::Continue;
}
// SyncMtl implementation
SyncMtl::SyncMtl() : SyncImpl() {}
SyncMtl::~SyncMtl() {}
void SyncMtl::onDestroy(const gl::Context *context)
{
mSync.onDestroy();
}
angle::Result SyncMtl::set(const gl::Context *context, GLenum condition, GLbitfield flags)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
return mSync.set(contextMtl, condition, flags);
}
angle::Result SyncMtl::clientWait(const gl::Context *context,
GLbitfield flags,
GLuint64 timeout,
GLenum *outResult)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
ASSERT((flags & ~GL_SYNC_FLUSH_COMMANDS_BIT) == 0);
bool flush = (flags & GL_SYNC_FLUSH_COMMANDS_BIT) != 0;
return mSync.clientWait(contextMtl, flush, timeout, outResult);
}
angle::Result SyncMtl::serverWait(const gl::Context *context, GLbitfield flags, GLuint64 timeout)
{
ASSERT(flags == 0);
ASSERT(timeout == GL_TIMEOUT_IGNORED);
ContextMtl *contextMtl = mtl::GetImpl(context);
mSync.serverWait(contextMtl);
return angle::Result::Continue;
}
angle::Result SyncMtl::getStatus(const gl::Context *context, GLint *outResult)
{
bool signaled = false;
ANGLE_TRY(mSync.getStatus(&signaled));
*outResult = signaled ? GL_SIGNALED : GL_UNSIGNALED;
return angle::Result::Continue;
}
// EGLSyncMtl implementation
EGLSyncMtl::EGLSyncMtl(const egl::AttributeMap &attribs) : EGLSyncImpl()
{
ASSERT(attribs.isEmpty());
}
EGLSyncMtl::~EGLSyncMtl() {}
void EGLSyncMtl::onDestroy(const egl::Display *display)
{
mSync.onDestroy();
}
egl::Error EGLSyncMtl::initialize(const egl::Display *display,
const gl::Context *context,
EGLenum type)
{
ASSERT(type == EGL_SYNC_FENCE_KHR);
ASSERT(context != nullptr);
ContextMtl *contextMtl = mtl::GetImpl(context);
if (IsError(mSync.set(contextMtl, GL_SYNC_GPU_COMMANDS_COMPLETE, 0)))
{
return egl::Error(EGL_BAD_ALLOC, "eglCreateSyncKHR failed to create sync object");
}
return egl::NoError();
}
egl::Error EGLSyncMtl::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;
GLenum result;
ContextMtl *contextMtl = mtl::GetImpl(context);
if (IsError(mSync.clientWait(contextMtl, flush, static_cast<uint64_t>(timeout), &result)))
{
return egl::Error(EGL_BAD_ALLOC);
}
switch (result)
{
case GL_ALREADY_SIGNALED:
// fall through. EGL doesn't differentiate between event being already set, or set
// before timeout.
case GL_CONDITION_SATISFIED:
*outResult = EGL_CONDITION_SATISFIED_KHR;
return egl::NoError();
case GL_TIMEOUT_EXPIRED:
*outResult = EGL_TIMEOUT_EXPIRED_KHR;
return egl::NoError();
default:
UNREACHABLE();
*outResult = EGL_FALSE;
return egl::Error(EGL_BAD_ALLOC);
}
}
egl::Error EGLSyncMtl::serverWait(const egl::Display *display,
const gl::Context *context,
EGLint flags)
{
// Server wait requires a valid bound context.
ASSERT(context);
// No flags are currently implemented.
ASSERT(flags == 0);
ContextMtl *contextMtl = mtl::GetImpl(context);
mSync.serverWait(contextMtl);
return egl::NoError();
}
egl::Error EGLSyncMtl::getStatus(const egl::Display *display, EGLint *outStatus)
{
bool signaled = false;
if (IsError(mSync.getStatus(&signaled)))
{
return egl::Error(EGL_BAD_ALLOC);
}
*outStatus = signaled ? EGL_SIGNALED_KHR : EGL_UNSIGNALED_KHR;
return egl::NoError();
}
egl::Error EGLSyncMtl::dupNativeFenceFD(const egl::Display *display, EGLint *result) const
{
UNREACHABLE();
return egl::EglBadDisplay();
}
}