Hash :
5bda5f91
Author :
Date :
2021-07-26T20:37:52
Fix some instances of -Wunreachable-code-aggressive. Bug: chromium:1066980 Change-Id: I1e769b78c51d939067e8855beab32ec7c006c00e Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3055794 Reviewed-by: Nico Weber <thakis@chromium.org> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Nico Weber <thakis@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 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 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
//
// 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.
//
// BufferMtl.mm:
// Implements the class methods for BufferMtl.
//
#include "libANGLE/renderer/metal/BufferMtl.h"
#include "common/debug.h"
#include "common/utilities.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
namespace rx
{
namespace
{
// Start with a fairly small buffer size. We can increase this dynamically as we convert more data.
constexpr size_t kConvertedElementArrayBufferInitialSize = 1024 * 8;
template <typename IndexType>
angle::Result GetFirstLastIndices(const IndexType *indices,
size_t count,
std::pair<uint32_t, uint32_t> *outIndices)
{
IndexType first, last;
// Use memcpy to avoid unaligned memory access crash:
memcpy(&first, &indices[0], sizeof(first));
memcpy(&last, &indices[count - 1], sizeof(last));
outIndices->first = first;
outIndices->second = last;
return angle::Result::Continue;
}
} // namespace
// ConversionBufferMtl implementation.
ConversionBufferMtl::ConversionBufferMtl(ContextMtl *contextMtl,
size_t initialSize,
size_t alignment)
: dirty(true), convertedBuffer(nullptr), convertedOffset(0)
{
data.initialize(contextMtl, initialSize, alignment, 0);
}
ConversionBufferMtl::~ConversionBufferMtl() = default;
// IndexConversionBufferMtl implementation.
IndexConversionBufferMtl::IndexConversionBufferMtl(ContextMtl *context,
gl::DrawElementsType elemTypeIn,
bool primitiveRestartEnabledIn,
size_t offsetIn,
std::vector<IndexRange> restartRangesIn)
: ConversionBufferMtl(context,
kConvertedElementArrayBufferInitialSize,
mtl::kIndexBufferOffsetAlignment),
elemType(elemTypeIn),
offset(offsetIn),
primitiveRestartEnabled(primitiveRestartEnabledIn),
restartRanges(restartRangesIn)
{}
IndexRange IndexConversionBufferMtl::getRangeForConvertedBuffer(size_t count)
{
return IndexRange{0, count};
}
// UniformConversionBufferMtl implementation
UniformConversionBufferMtl::UniformConversionBufferMtl(ContextMtl *context, size_t offsetIn)
: ConversionBufferMtl(context, 0, mtl::kUniformBufferSettingOffsetMinAlignment),
offset(offsetIn)
{}
// VertexConversionBufferMtl implementation.
VertexConversionBufferMtl::VertexConversionBufferMtl(ContextMtl *context,
angle::FormatID formatIDIn,
GLuint strideIn,
size_t offsetIn)
: ConversionBufferMtl(context, 0, mtl::kVertexAttribBufferStrideAlignment),
formatID(formatIDIn),
stride(strideIn),
offset(offsetIn)
{}
// BufferMtl implementation
BufferMtl::BufferMtl(const gl::BufferState &state)
: BufferImpl(state), mBufferPool(/** alwaysAllocNewBuffer */ true)
{}
BufferMtl::~BufferMtl() {}
void BufferMtl::destroy(const gl::Context *context)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
mShadowCopy.clear();
mBufferPool.destroy(contextMtl);
mBuffer = nullptr;
clearConversionBuffers();
}
angle::Result BufferMtl::setData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t intendedSize,
gl::BufferUsage usage)
{
return setDataImpl(context, target, data, intendedSize, usage);
}
angle::Result BufferMtl::setSubData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
size_t offset)
{
return setSubDataImpl(context, data, size, offset);
}
angle::Result BufferMtl::copySubData(const gl::Context *context,
BufferImpl *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
if (!source)
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
auto srcMtl = GetAs<BufferMtl>(source);
if (srcMtl->clientShadowCopyDataNeedSync(contextMtl) || mBuffer->isBeingUsedByGPU(contextMtl))
{
// If shadow copy requires a synchronization then use blit command instead.
// It might break a pending render pass, but still faster than synchronization with
// GPU.
mtl::BlitCommandEncoder *blitEncoder = contextMtl->getBlitCommandEncoder();
blitEncoder->copyBuffer(srcMtl->getCurrentBuffer(), sourceOffset, mBuffer, destOffset,
size);
return angle::Result::Continue;
}
return setSubDataImpl(context, srcMtl->getClientShadowCopyData(contextMtl) + sourceOffset, size,
destOffset);
}
angle::Result BufferMtl::map(const gl::Context *context, GLenum access, void **mapPtr)
{
GLbitfield mapRangeAccess = 0;
if ((access & GL_WRITE_ONLY_OES) != 0 || (access & GL_READ_WRITE) != 0)
{
mapRangeAccess |= GL_MAP_WRITE_BIT;
}
return mapRange(context, 0, size(), mapRangeAccess, mapPtr);
}
angle::Result BufferMtl::mapRange(const gl::Context *context,
size_t offset,
size_t length,
GLbitfield access,
void **mapPtr)
{
if (access & GL_MAP_INVALIDATE_BUFFER_BIT)
{
ANGLE_TRY(setDataImpl(context, gl::BufferBinding::InvalidEnum, nullptr, size(),
mState.getUsage()));
}
if (mapPtr)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
if (mBufferPool.getMaxBuffers() == 1)
{
*mapPtr = mBuffer->mapWithOpt(contextMtl, (access & GL_MAP_WRITE_BIT) == 0,
access & GL_MAP_UNSYNCHRONIZED_BIT) +
offset;
}
else
{
*mapPtr = syncAndObtainShadowCopy(contextMtl) + offset;
}
}
return angle::Result::Continue;
}
angle::Result BufferMtl::unmap(const gl::Context *context, GLboolean *result)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
size_t offset = static_cast<size_t>(mState.getMapOffset());
size_t len = static_cast<size_t>(mState.getMapLength());
markConversionBuffersDirty();
if (mBufferPool.getMaxBuffers() == 1)
{
ASSERT(mBuffer);
if (mState.getAccessFlags() & GL_MAP_WRITE_BIT)
{
mBuffer->unmapAndFlushSubset(contextMtl, offset, len);
}
else
{
// Buffer is already mapped with readonly flag, so just unmap it, no flushing will
// occur.
mBuffer->unmap(contextMtl);
}
}
else
{
ASSERT(mShadowCopy.size());
if (mState.getAccessFlags() & GL_MAP_UNSYNCHRONIZED_BIT)
{
// Copy the mapped region without synchronization with GPU
uint8_t *ptr =
mBuffer->mapWithOpt(contextMtl, /* readonly */ false, /* noSync */ true) + offset;
std::copy(mShadowCopy.data() + offset, mShadowCopy.data() + offset + len, ptr);
mBuffer->unmapAndFlushSubset(contextMtl, offset, len);
}
else
{
// commit shadow copy data to GPU synchronously
ANGLE_TRY(commitShadowCopy(context));
}
}
if (result)
{
*result = true;
}
return angle::Result::Continue;
}
angle::Result BufferMtl::getIndexRange(const gl::Context *context,
gl::DrawElementsType type,
size_t offset,
size_t count,
bool primitiveRestartEnabled,
gl::IndexRange *outRange)
{
const uint8_t *indices = getClientShadowCopyData(mtl::GetImpl(context)) + offset;
*outRange = gl::ComputeIndexRange(type, indices, count, primitiveRestartEnabled);
return angle::Result::Continue;
}
angle::Result BufferMtl::getFirstLastIndices(ContextMtl *contextMtl,
gl::DrawElementsType type,
size_t offset,
size_t count,
std::pair<uint32_t, uint32_t> *outIndices)
{
const uint8_t *indices = getClientShadowCopyData(contextMtl) + offset;
switch (type)
{
case gl::DrawElementsType::UnsignedByte:
return GetFirstLastIndices(static_cast<const GLubyte *>(indices), count, outIndices);
case gl::DrawElementsType::UnsignedShort:
return GetFirstLastIndices(reinterpret_cast<const GLushort *>(indices), count,
outIndices);
case gl::DrawElementsType::UnsignedInt:
return GetFirstLastIndices(reinterpret_cast<const GLuint *>(indices), count,
outIndices);
default:
UNREACHABLE();
return angle::Result::Stop;
}
}
void BufferMtl::onDataChanged()
{
markConversionBuffersDirty();
}
/* public */
const uint8_t *BufferMtl::getClientShadowCopyData(ContextMtl *contextMtl)
{
if (mBufferPool.getMaxBuffers() == 1)
{
// Don't need shadow copy in this case, use the buffer directly
return mBuffer->mapReadOnly(contextMtl);
}
return syncAndObtainShadowCopy(contextMtl);
}
bool BufferMtl::clientShadowCopyDataNeedSync(ContextMtl *contextMtl)
{
return mBuffer->isCPUReadMemDirty();
}
void BufferMtl::ensureShadowCopySyncedFromGPU(ContextMtl *contextMtl)
{
if (mBuffer->isCPUReadMemDirty())
{
const uint8_t *ptr = mBuffer->mapReadOnly(contextMtl);
memcpy(mShadowCopy.data(), ptr, size());
mBuffer->unmap(contextMtl);
mBuffer->resetCPUReadMemDirty();
}
}
uint8_t *BufferMtl::syncAndObtainShadowCopy(ContextMtl *contextMtl)
{
ASSERT(mShadowCopy.size());
ensureShadowCopySyncedFromGPU(contextMtl);
return mShadowCopy.data();
}
ConversionBufferMtl *BufferMtl::getVertexConversionBuffer(ContextMtl *context,
angle::FormatID formatID,
GLuint stride,
size_t offset)
{
for (VertexConversionBufferMtl &buffer : mVertexConversionBuffers)
{
if (buffer.formatID == formatID && buffer.stride == stride && buffer.offset == offset)
{
return &buffer;
}
}
mVertexConversionBuffers.emplace_back(context, formatID, stride, offset);
ConversionBufferMtl *conv = &mVertexConversionBuffers.back();
const angle::Format &angleFormat = angle::Format::Get(formatID);
conv->data.updateAlignment(context, angleFormat.pixelBytes);
return conv;
}
IndexConversionBufferMtl *BufferMtl::getIndexConversionBuffer(ContextMtl *context,
gl::DrawElementsType elemType,
bool primitiveRestartEnabled,
size_t offset)
{
for (auto &buffer : mIndexConversionBuffers)
{
if (buffer.elemType == elemType && buffer.offset == offset &&
buffer.primitiveRestartEnabled == primitiveRestartEnabled)
{
return &buffer;
}
}
mIndexConversionBuffers.emplace_back(context, elemType, primitiveRestartEnabled, offset);
return &mIndexConversionBuffers.back();
}
ConversionBufferMtl *BufferMtl::getUniformConversionBuffer(ContextMtl *context, size_t offset)
{
for (UniformConversionBufferMtl &buffer : mUniformConversionBuffers)
{
if (buffer.offset == offset)
{
return &buffer;
}
}
mUniformConversionBuffers.emplace_back(context, offset);
return &mUniformConversionBuffers.back();
}
void BufferMtl::markConversionBuffersDirty()
{
for (VertexConversionBufferMtl &buffer : mVertexConversionBuffers)
{
buffer.dirty = true;
buffer.convertedBuffer = nullptr;
buffer.convertedOffset = 0;
}
for (IndexConversionBufferMtl &buffer : mIndexConversionBuffers)
{
buffer.dirty = true;
buffer.convertedBuffer = nullptr;
buffer.convertedOffset = 0;
}
for (UniformConversionBufferMtl &buffer : mUniformConversionBuffers)
{
buffer.dirty = true;
buffer.convertedBuffer = nullptr;
buffer.convertedOffset = 0;
}
}
void BufferMtl::clearConversionBuffers()
{
mVertexConversionBuffers.clear();
mIndexConversionBuffers.clear();
mUniformConversionBuffers.clear();
}
angle::Result BufferMtl::setDataImpl(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t intendedSize,
gl::BufferUsage usage)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
// Invalidate conversion buffers
if (mState.getSize() != static_cast<GLint64>(intendedSize))
{
clearConversionBuffers();
}
else
{
markConversionBuffersDirty();
}
size_t adjustedSize = std::max<size_t>(1, intendedSize);
// Ensures no validation layer issues in std140 with data types like vec3 being 12 bytes vs 16
// in MSL.
if (target == gl::BufferBinding::Uniform)
{
adjustedSize = roundUpPow2(adjustedSize, (size_t)16);
}
size_t maxBuffers;
switch (usage)
{
case gl::BufferUsage::StaticCopy:
case gl::BufferUsage::StaticDraw:
case gl::BufferUsage::StaticRead:
case gl::BufferUsage::DynamicRead:
case gl::BufferUsage::StreamRead:
maxBuffers = 1; // static/read buffer doesn't need high speed data update
mBufferPool.setAlwaysUseGPUMem();
break;
default:
// dynamic buffer, allow up to 10 update per frame/encoding without
// waiting for GPU.
if (adjustedSize <= mtl::kSharedMemBufferMaxBufSizeHint)
{
maxBuffers = 10;
mBufferPool.setAlwaysUseSharedMem();
}
else
{
maxBuffers = 1;
mBufferPool.setAlwaysUseGPUMem();
}
break;
}
// Re-create the buffer
mBuffer = nullptr;
ANGLE_TRY(mBufferPool.reset(contextMtl, adjustedSize, 1, maxBuffers));
if (maxBuffers > 1)
{
// We use shadow copy to maintain consistent data between buffers in pool
ANGLE_MTL_CHECK(contextMtl, mShadowCopy.resize(adjustedSize), GL_OUT_OF_MEMORY);
if (data)
{
// Transfer data to shadow copy buffer
auto ptr = static_cast<const uint8_t *>(data);
std::copy(ptr, ptr + intendedSize, mShadowCopy.data());
// Transfer data from shadow copy buffer to GPU buffer.
ANGLE_TRY(commitShadowCopy(context, adjustedSize));
}
else
{
// This is needed so that first buffer pointer could be available
ANGLE_TRY(commitShadowCopy(context, 0));
}
}
else
{
// We don't need shadow copy if there will be only one buffer in the pool.
ANGLE_MTL_CHECK(contextMtl, mShadowCopy.resize(0), GL_OUT_OF_MEMORY);
// Allocate one buffer to use
ANGLE_TRY(
mBufferPool.allocate(contextMtl, adjustedSize, nullptr, &mBuffer, nullptr, nullptr));
if (data)
{
ANGLE_TRY(setSubDataImpl(context, data, intendedSize, 0));
}
}
#ifndef NDEBUG
ANGLE_MTL_OBJC_SCOPE
{
mBuffer->get().label = [NSString stringWithFormat:@"BufferMtl=%p", this];
}
#endif
return angle::Result::Continue;
}
angle::Result BufferMtl::setSubDataImpl(const gl::Context *context,
const void *data,
size_t size,
size_t offset)
{
if (!data)
{
return angle::Result::Continue;
}
ASSERT(mBuffer);
ContextMtl *contextMtl = mtl::GetImpl(context);
ANGLE_MTL_TRY(contextMtl, offset <= mBuffer->size());
auto srcPtr = static_cast<const uint8_t *>(data);
auto sizeToCopy = std::min<size_t>(size, mBuffer->size() - offset);
markConversionBuffersDirty();
if (mBufferPool.getMaxBuffers() == 1)
{
ASSERT(mBuffer);
uint8_t *ptr = mBuffer->map(contextMtl);
std::copy(srcPtr, srcPtr + sizeToCopy, ptr + offset);
mBuffer->unmapAndFlushSubset(contextMtl, offset, sizeToCopy);
}
else
{
ASSERT(mShadowCopy.size());
// 1. Before copying data from client, we need to synchronize modified data from GPU to
// shadow copy first.
ensureShadowCopySyncedFromGPU(contextMtl);
// 2. Copy data from client to shadow copy.
std::copy(srcPtr, srcPtr + sizeToCopy, mShadowCopy.data() + offset);
// 3. Copy data from shadow copy to GPU.
ANGLE_TRY(commitShadowCopy(context));
}
return angle::Result::Continue;
}
angle::Result BufferMtl::commitShadowCopy(const gl::Context *context)
{
return commitShadowCopy(context, size());
}
angle::Result BufferMtl::commitShadowCopy(const gl::Context *context, size_t size)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
if (!size)
{
// Skip mapping if size to commit is zero.
// zero size is passed to allocate buffer only.
ANGLE_TRY(mBufferPool.allocate(contextMtl, mShadowCopy.size(), nullptr, &mBuffer, nullptr,
nullptr));
}
else
{
uint8_t *ptr = nullptr;
mBufferPool.releaseInFlightBuffers(contextMtl);
ANGLE_TRY(
mBufferPool.allocate(contextMtl, mShadowCopy.size(), &ptr, &mBuffer, nullptr, nullptr));
std::copy(mShadowCopy.data(), mShadowCopy.data() + size, ptr);
}
ANGLE_TRY(mBufferPool.commit(contextMtl));
return angle::Result::Continue;
}
// SimpleWeakBufferHolderMtl implementation
SimpleWeakBufferHolderMtl::SimpleWeakBufferHolderMtl()
{
mIsWeak = true;
}
} // namespace rx