Hash :
da3db87e
Author :
Date :
2021-07-06T14:00:58
Upstream latest changes to Metal backend from Apple to 7/1/2021 This CL merges in the ANGLE changes between these two WebKit commits: https://git.webkit.org/?p=WebKit.git;a=commit;h=8648b353ab1d7730438c2e08319e1a4d64982c31 https://git.webkit.org/?p=WebKit.git;a=commit;h=166e4924a52971d6a32ad48247a439b16c00e062 Include provoking vertex buffer out of bounds fix from https://bugs.webkit.org/show_bug.cgi?id=230107 Fix bad merge of resetting of dirty bits, breaking DepthStencilFormatsTest.DepthTextureRender test and perhaps others. Disable GL_APPLE_clip_distance when the direct-to-Metal compiler is active. It can not yet handle the gl_ClipDistance array. Disable use of rectangular textures for IOSurfaces. Metal can bind IOSurfaces to 2D textures, and this was passing all tests in the SPIR-V Metal backend. Introducing rectangular textures breaks the SPIR-V Metal backend, and the tests currently fail on the direct-to-Metal backend. Fix several bugs with ProvokingVertex, which was causing both the SpirV and Direct backends to incorrectly draw indices. (https://bugs.webkit.org/show_bug.cgi?id=230107) Skip the following tests on the Metal backend which is still failing RobustResourceInitTestES3.BlitDepthStencilAfterClearBuffer GLSLTest_ES3.GLVertexIDIntegerTextureDrawArrays/ES3_Metal With these changes, angle_end2end_tests again runs to completion. Bug: angleproject:6395 Change-Id: I3cc58f531426a95fc8f177a4ad87f56c1855a546 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3167010 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Reviewed-by: Kenneth Russell <kbr@chromium.org> Commit-Queue: Kyle Piddington <kpiddington@apple.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 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
//
// 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.
//
// mtl_buffer_pool.mm:
// Implements the class methods for BufferPool.
//
#include "libANGLE/renderer/metal/mtl_buffer_pool.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
namespace rx
{
namespace mtl
{
// BufferPool implementation.
BufferPool::BufferPool() : BufferPool(false) {}
BufferPool::BufferPool(bool alwaysAllocNewBuffer)
: BufferPool(alwaysAllocNewBuffer, BufferPoolMemPolicy::Auto)
{}
BufferPool::BufferPool(bool alwaysAllocNewBuffer, BufferPoolMemPolicy policy)
: mInitialSize(0),
mBuffer(nullptr),
mNextAllocationOffset(0),
mLastFlushOffset(0),
mSize(0),
mAlignment(1),
mBuffersAllocated(0),
mMaxBuffers(0),
mMemPolicy(policy),
mAlwaysAllocateNewBuffer(alwaysAllocNewBuffer)
{}
angle::Result BufferPool::reset(ContextMtl *contextMtl,
size_t initialSize,
size_t alignment,
size_t maxBuffers)
{
ANGLE_TRY(finalizePendingBuffer(contextMtl));
releaseInFlightBuffers(contextMtl);
mSize = 0;
if (mBufferFreeList.size() && mInitialSize <= mBufferFreeList.front()->size())
{
// Instead of deleteing old buffers, we should reset them to avoid excessive
// memory re-allocations
if (maxBuffers && mBufferFreeList.size() > maxBuffers)
{
mBufferFreeList.resize(maxBuffers);
mBuffersAllocated = maxBuffers;
}
mSize = mBufferFreeList.front()->size();
for (size_t i = 0; i < mBufferFreeList.size(); ++i)
{
BufferRef &buffer = mBufferFreeList[i];
if (!buffer->isBeingUsedByGPU(contextMtl))
{
// If buffer is not used by GPU, re-use it immediately.
continue;
}
bool useSharedMem = shouldAllocateInSharedMem(contextMtl);
if (IsError(buffer->resetWithSharedMemOpt(contextMtl, useSharedMem, mSize, nullptr)))
{
mBufferFreeList.clear();
mBuffersAllocated = 0;
mSize = 0;
break;
}
}
}
else
{
mBufferFreeList.clear();
mBuffersAllocated = 0;
}
mInitialSize = initialSize;
mMaxBuffers = maxBuffers;
updateAlignment(contextMtl, alignment);
return angle::Result::Continue;
}
void BufferPool::initialize(Context *context,
size_t initialSize,
size_t alignment,
size_t maxBuffers)
{
if (mBuffersAllocated)
{
// Invalid call, must call destroy() first.
UNREACHABLE();
}
mInitialSize = initialSize;
mMaxBuffers = maxBuffers;
updateAlignment(context, alignment);
}
BufferPool::~BufferPool() {}
bool BufferPool::shouldAllocateInSharedMem(ContextMtl *contextMtl) const
{
if (ANGLE_UNLIKELY(contextMtl->getDisplay()->getFeatures().forceBufferGPUStorage.enabled))
{
return false;
}
switch (mMemPolicy)
{
case BufferPoolMemPolicy::AlwaysSharedMem:
return true;
case BufferPoolMemPolicy::AlwaysGPUMem:
return false;
default:
return mSize <= kSharedMemBufferMaxBufSizeHint;
}
}
angle::Result BufferPool::allocateNewBuffer(ContextMtl *contextMtl)
{
if (mMaxBuffers > 0 && mBuffersAllocated >= mMaxBuffers)
{
// We reach the max number of buffers allowed.
// Try to deallocate old and smaller size inflight buffers.
releaseInFlightBuffers(contextMtl);
}
if (mMaxBuffers > 0 && mBuffersAllocated >= mMaxBuffers)
{
// If we reach this point, it means there was no buffer deallocated inside
// releaseInFlightBuffers() thus, the number of buffers allocated still exceeds number
// allowed.
ASSERT(!mBufferFreeList.empty());
// Reuse the buffer in free list:
if (mBufferFreeList.front()->isBeingUsedByGPU(contextMtl))
{
contextMtl->flushCommandBuffer(mtl::NoWait);
// Force the GPU to finish its rendering and make the old buffer available.
contextMtl->cmdQueue().ensureResourceReadyForCPU(mBufferFreeList.front());
}
mBuffer = mBufferFreeList.front();
mBufferFreeList.erase(mBufferFreeList.begin());
return angle::Result::Continue;
}
bool useSharedMem = shouldAllocateInSharedMem(contextMtl);
ANGLE_TRY(
Buffer::MakeBufferWithSharedMemOpt(contextMtl, useSharedMem, mSize, nullptr, &mBuffer));
ASSERT(mBuffer);
mBuffersAllocated++;
return angle::Result::Continue;
}
angle::Result BufferPool::allocate(ContextMtl *contextMtl,
size_t sizeInBytes,
uint8_t **ptrOut,
BufferRef *bufferOut,
size_t *offsetOut,
bool *newBufferAllocatedOut)
{
size_t sizeToAllocate = roundUp(sizeInBytes, mAlignment);
angle::base::CheckedNumeric<size_t> checkedNextWriteOffset = mNextAllocationOffset;
checkedNextWriteOffset += sizeToAllocate;
if (!mBuffer || !checkedNextWriteOffset.IsValid() ||
checkedNextWriteOffset.ValueOrDie() >= mSize ||
// If the current buffer has been modified by GPU, do not reuse it:
mBuffer->isCPUReadMemNeedSync() || mAlwaysAllocateNewBuffer)
{
if (mBuffer)
{
ANGLE_TRY(finalizePendingBuffer(contextMtl));
}
if (sizeToAllocate > mSize)
{
mSize = std::max(mInitialSize, sizeToAllocate);
// Clear the free list since the free buffers are now too small.
destroyBufferList(contextMtl, &mBufferFreeList);
}
// The front of the free list should be the oldest. Thus if it is in use the rest of the
// free list should be in use as well.
if (mBufferFreeList.empty() || mBufferFreeList.front()->isBeingUsedByGPU(contextMtl))
{
ANGLE_TRY(allocateNewBuffer(contextMtl));
}
else
{
mBuffer = mBufferFreeList.front();
mBufferFreeList.erase(mBufferFreeList.begin());
}
ASSERT(mBuffer->size() == mSize);
mNextAllocationOffset = 0;
mLastFlushOffset = 0;
if (newBufferAllocatedOut != nullptr)
{
*newBufferAllocatedOut = true;
}
}
else if (newBufferAllocatedOut != nullptr)
{
*newBufferAllocatedOut = false;
}
ASSERT(mBuffer != nullptr);
if (bufferOut != nullptr)
{
*bufferOut = mBuffer;
}
// Optionally map() the buffer if possible
if (ptrOut)
{
// We don't need to synchronize with GPU access, since allocation should return a
// non-overlapped region each time.
*ptrOut = mBuffer->mapWithOpt(contextMtl, /** readOnly */ false, /** noSync */ true) +
mNextAllocationOffset;
}
if (offsetOut)
{
*offsetOut = static_cast<size_t>(mNextAllocationOffset);
}
mNextAllocationOffset += static_cast<uint32_t>(sizeToAllocate);
return angle::Result::Continue;
}
angle::Result BufferPool::commit(ContextMtl *contextMtl, bool flushEntireBuffer)
{
if (mBuffer && mNextAllocationOffset > mLastFlushOffset)
{
if (flushEntireBuffer)
{
mBuffer->flush(contextMtl, 0, mLastFlushOffset);
}
else
{
mBuffer->flush(contextMtl, mLastFlushOffset, mNextAllocationOffset - mLastFlushOffset);
}
mLastFlushOffset = mNextAllocationOffset;
}
return angle::Result::Continue;
}
angle::Result BufferPool::finalizePendingBuffer(ContextMtl *contextMtl)
{
if (mBuffer)
{
ANGLE_TRY(commit(contextMtl));
// commit() already flushes so no need to flush here.
mBuffer->unmapNoFlush(contextMtl);
mInFlightBuffers.push_back(mBuffer);
mBuffer = nullptr;
}
mNextAllocationOffset = 0;
mLastFlushOffset = 0;
return angle::Result::Continue;
}
void BufferPool::releaseInFlightBuffers(ContextMtl *contextMtl)
{
for (auto &toRelease : mInFlightBuffers)
{
// If the dynamic buffer was resized we cannot reuse the retained buffer.
if (toRelease->size() < mSize
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
// Also release buffer if it was allocated in different policy
|| toRelease->useSharedMem() != shouldAllocateInSharedMem(contextMtl)
#endif
)
{
toRelease = nullptr;
mBuffersAllocated--;
}
else
{
mBufferFreeList.push_back(toRelease);
}
}
mInFlightBuffers.clear();
}
void BufferPool::destroyBufferList(ContextMtl *contextMtl, std::deque<BufferRef> *buffers)
{
ASSERT(mBuffersAllocated >= buffers->size());
mBuffersAllocated -= buffers->size();
buffers->clear();
}
void BufferPool::destroy(ContextMtl *contextMtl)
{
destroyBufferList(contextMtl, &mInFlightBuffers);
destroyBufferList(contextMtl, &mBufferFreeList);
reset();
if (mBuffer)
{
mBuffer->unmap(contextMtl);
mBuffer = nullptr;
}
}
void BufferPool::updateAlignment(Context *context, size_t alignment)
{
ASSERT(alignment > 0);
// NOTE(hqle): May check additional platform limits.
// If alignment has changed, make sure the next allocation is done at an aligned offset.
if (alignment != mAlignment)
{
mNextAllocationOffset = roundUp(mNextAllocationOffset, static_cast<uint32_t>(alignment));
mAlignment = alignment;
}
}
void BufferPool::reset()
{
mSize = 0;
mNextAllocationOffset = 0;
mLastFlushOffset = 0;
mMaxBuffers = 0;
mAlwaysAllocateNewBuffer = false;
mBuffersAllocated = 0;
}
}
}