Hash :
c916fe8a
Author :
Date :
2020-04-02T16:57:49
Don't call syncState inside FBO queries. This prevents a syncState ordering issue that was confusing FBO sync when robust resource init is enabled. Also cleans up some redundant format processing for the half float extensions. Bug: angleproject:4517 Change-Id: Ieb13fc5203cf824a3e8affda96ea5cbbd89d78ee Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2134411 Reviewed-by: Geoff Lang <geofflang@chromium.org> Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Commit-Queue: 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 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 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736
//
// 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.
//
// FramebufferMtl.mm:
// Implements the class methods for FramebufferMtl.
//
#include "libANGLE/renderer/metal/ContextMtl.h"
#include <TargetConditionals.h>
#include "common/MemoryBuffer.h"
#include "common/angleutils.h"
#include "common/debug.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "libANGLE/renderer/metal/FrameBufferMtl.h"
#include "libANGLE/renderer/metal/SurfaceMtl.h"
#include "libANGLE/renderer/metal/mtl_utils.h"
#include "libANGLE/renderer/renderer_utils.h"
namespace rx
{
// FramebufferMtl implementation
FramebufferMtl::FramebufferMtl(const gl::FramebufferState &state, bool flipY)
: FramebufferImpl(state), mFlipY(flipY)
{
reset();
}
FramebufferMtl::~FramebufferMtl() {}
void FramebufferMtl::reset()
{
for (auto &rt : mColorRenderTargets)
{
rt = nullptr;
}
mDepthRenderTarget = mStencilRenderTarget = nullptr;
}
void FramebufferMtl::destroy(const gl::Context *context)
{
reset();
}
angle::Result FramebufferMtl::discard(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
return invalidate(context, count, attachments);
}
angle::Result FramebufferMtl::invalidate(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
return invalidateImpl(mtl::GetImpl(context), count, attachments);
}
angle::Result FramebufferMtl::invalidateSub(const gl::Context *context,
size_t count,
const GLenum *attachments,
const gl::Rectangle &area)
{
// NOTE(hqle): ES 3.0 feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result FramebufferMtl::clear(const gl::Context *context, GLbitfield mask)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
mtl::ClearRectParams clearOpts;
bool clearColor = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_COLOR_BUFFER_BIT));
bool clearDepth = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_DEPTH_BUFFER_BIT));
bool clearStencil = IsMaskFlagSet(mask, static_cast<GLbitfield>(GL_STENCIL_BUFFER_BIT));
gl::DrawBufferMask clearColorBuffers;
if (clearColor)
{
clearColorBuffers = mState.getEnabledDrawBuffers();
clearOpts.clearColor = contextMtl->getClearColorValue();
}
if (clearDepth)
{
clearOpts.clearDepth = contextMtl->getClearDepthValue();
}
if (clearStencil)
{
clearOpts.clearStencil = contextMtl->getClearStencilValue();
}
return clearImpl(context, clearColorBuffers, &clearOpts);
}
angle::Result FramebufferMtl::clearBufferfv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLfloat *values)
{
// NOTE(hqle): ES 3.0 feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result FramebufferMtl::clearBufferuiv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLuint *values)
{
// NOTE(hqle): ES 3.0 feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result FramebufferMtl::clearBufferiv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLint *values)
{
// NOTE(hqle): ES 3.0 feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result FramebufferMtl::clearBufferfi(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
GLfloat depth,
GLint stencil)
{
// NOTE(hqle): ES 3.0 feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
const gl::InternalFormat &FramebufferMtl::getImplementationColorReadFormat(
const gl::Context *context) const
{
ContextMtl *contextMtl = mtl::GetImpl(context);
GLenum sizedFormat = mState.getReadAttachment()->getFormat().info->sizedInternalFormat;
angle::FormatID formatID = angle::Format::InternalFormatToID(sizedFormat);
const mtl::Format &mtlFormat = contextMtl->getDisplay()->getPixelFormat(formatID);
GLenum implFormat = mtlFormat.actualAngleFormat().fboImplementationInternalFormat;
return gl::GetSizedInternalFormatInfo(implFormat);
}
angle::Result FramebufferMtl::readPixels(const gl::Context *context,
const gl::Rectangle &area,
GLenum format,
GLenum type,
void *pixels)
{
// Clip read area to framebuffer.
const gl::Extents &fbSize = getState().getReadAttachment()->getSize();
const gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
gl::Rectangle clippedArea;
if (!ClipRectangle(area, fbRect, &clippedArea))
{
// nothing to read
return angle::Result::Continue;
}
gl::Rectangle flippedArea = getReadPixelArea(clippedArea);
ContextMtl *contextMtl = mtl::GetImpl(context);
const gl::State &glState = context->getState();
const gl::PixelPackState &packState = glState.getPackState();
const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
GLuint outputPitch = 0;
ANGLE_CHECK_GL_MATH(contextMtl,
sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
packState.rowLength, &outputPitch));
GLuint outputSkipBytes = 0;
ANGLE_CHECK_GL_MATH(contextMtl, sizedFormatInfo.computeSkipBytes(
type, outputPitch, 0, packState, false, &outputSkipBytes));
outputSkipBytes += (clippedArea.x - area.x) * sizedFormatInfo.pixelBytes +
(clippedArea.y - area.y) * outputPitch;
const angle::Format &angleFormat = GetFormatFromFormatType(format, type);
PackPixelsParams params(flippedArea, angleFormat, outputPitch, packState.reverseRowOrder,
glState.getTargetBuffer(gl::BufferBinding::PixelPack), 0);
if (mFlipY)
{
params.reverseRowOrder = !params.reverseRowOrder;
}
ANGLE_TRY(readPixelsImpl(context, flippedArea, params, getColorReadRenderTarget(),
static_cast<uint8_t *>(pixels) + outputSkipBytes));
return angle::Result::Continue;
}
angle::Result FramebufferMtl::blit(const gl::Context *context,
const gl::Rectangle &sourceArea,
const gl::Rectangle &destArea,
GLbitfield mask,
GLenum filter)
{
// NOTE(hqle): MSAA feature.
UNIMPLEMENTED();
return angle::Result::Stop;
}
bool FramebufferMtl::checkStatus(const gl::Context *context) const
{
if (!mState.attachmentsHaveSameDimensions())
{
return false;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
if (!contextMtl->getDisplay()->getFeatures().allowSeparatedDepthStencilBuffers.enabled &&
mState.hasSeparateDepthAndStencilAttachments())
{
return false;
}
return true;
}
angle::Result FramebufferMtl::syncState(const gl::Context *context,
const gl::Framebuffer::DirtyBits &dirtyBits)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
ASSERT(dirtyBits.any());
for (size_t dirtyBit : dirtyBits)
{
switch (dirtyBit)
{
case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
ANGLE_TRY(updateDepthRenderTarget(context));
break;
case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
ANGLE_TRY(updateStencilRenderTarget(context));
break;
case gl::Framebuffer::DIRTY_BIT_DEPTH_BUFFER_CONTENTS:
case gl::Framebuffer::DIRTY_BIT_STENCIL_BUFFER_CONTENTS:
// NOTE(hqle): What are we supposed to do?
break;
case gl::Framebuffer::DIRTY_BIT_DRAW_BUFFERS:
case gl::Framebuffer::DIRTY_BIT_READ_BUFFER:
case gl::Framebuffer::DIRTY_BIT_DEFAULT_WIDTH:
case gl::Framebuffer::DIRTY_BIT_DEFAULT_HEIGHT:
case gl::Framebuffer::DIRTY_BIT_DEFAULT_SAMPLES:
case gl::Framebuffer::DIRTY_BIT_DEFAULT_FIXED_SAMPLE_LOCATIONS:
break;
default:
{
static_assert(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0, "FB dirty bits");
if (dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_MAX)
{
size_t colorIndexGL = static_cast<size_t>(
dirtyBit - gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0);
ANGLE_TRY(updateColorRenderTarget(context, colorIndexGL));
}
else
{
ASSERT(dirtyBit >= gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_0 &&
dirtyBit < gl::Framebuffer::DIRTY_BIT_COLOR_BUFFER_CONTENTS_MAX);
// NOTE: might need to notify context.
}
break;
}
}
}
auto oldRenderPassDesc = mRenderPassDesc;
ANGLE_TRY(prepareRenderPass(context, mState.getEnabledDrawBuffers(), &mRenderPassDesc));
if (!oldRenderPassDesc.equalIgnoreLoadStoreOptions(mRenderPassDesc))
{
FramebufferMtl *currentDrawFramebuffer =
mtl::GetImpl(context->getState().getDrawFramebuffer());
if (currentDrawFramebuffer == this)
{
contextMtl->onDrawFrameBufferChange(context, this);
}
}
return angle::Result::Continue;
}
angle::Result FramebufferMtl::getSamplePosition(const gl::Context *context,
size_t index,
GLfloat *xy) const
{
UNIMPLEMENTED();
return angle::Result::Stop;
}
RenderTargetMtl *FramebufferMtl::getColorReadRenderTarget() const
{
if (mState.getReadIndex() >= mColorRenderTargets.size())
{
return nullptr;
}
return mColorRenderTargets[mState.getReadIndex()];
}
gl::Rectangle FramebufferMtl::getCompleteRenderArea() const
{
return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
}
const mtl::RenderPassDesc &FramebufferMtl::getRenderPassDesc(ContextMtl *context)
{
return mRenderPassDesc;
}
void FramebufferMtl::onStartedDrawingToFrameBuffer(const gl::Context *context)
{
// Compute loadOp based on previous storeOp and reset storeOp flags:
for (mtl::RenderPassColorAttachmentDesc &colorAttachment : mRenderPassDesc.colorAttachments)
{
if (colorAttachment.storeAction == MTLStoreActionDontCare)
{
// If we previously discarded attachment's content, then don't need to load it.
colorAttachment.loadAction = MTLLoadActionDontCare;
}
else
{
colorAttachment.loadAction = MTLLoadActionLoad;
}
colorAttachment.storeAction = MTLStoreActionStore; // Default action is store
}
// Depth load/store
if (mRenderPassDesc.depthAttachment.storeAction == MTLStoreActionDontCare)
{
mRenderPassDesc.depthAttachment.loadAction = MTLLoadActionDontCare;
}
else
{
mRenderPassDesc.depthAttachment.loadAction = MTLLoadActionLoad;
}
mRenderPassDesc.depthAttachment.storeAction = MTLStoreActionStore;
// Stencil load/store
if (mRenderPassDesc.stencilAttachment.storeAction == MTLStoreActionDontCare)
{
mRenderPassDesc.stencilAttachment.loadAction = MTLLoadActionDontCare;
}
else
{
mRenderPassDesc.stencilAttachment.loadAction = MTLLoadActionLoad;
}
mRenderPassDesc.stencilAttachment.storeAction = MTLStoreActionStore;
}
angle::Result FramebufferMtl::updateColorRenderTarget(const gl::Context *context,
size_t colorIndexGL)
{
ASSERT(colorIndexGL < mtl::kMaxRenderTargets);
// Reset load store action
mRenderPassDesc.colorAttachments[colorIndexGL].reset();
return updateCachedRenderTarget(context, mState.getColorAttachment(colorIndexGL),
&mColorRenderTargets[colorIndexGL]);
}
angle::Result FramebufferMtl::updateDepthRenderTarget(const gl::Context *context)
{
// Reset load store action
mRenderPassDesc.depthAttachment.reset();
return updateCachedRenderTarget(context, mState.getDepthAttachment(), &mDepthRenderTarget);
}
angle::Result FramebufferMtl::updateStencilRenderTarget(const gl::Context *context)
{
// Reset load store action
mRenderPassDesc.stencilAttachment.reset();
return updateCachedRenderTarget(context, mState.getStencilAttachment(), &mStencilRenderTarget);
}
angle::Result FramebufferMtl::updateCachedRenderTarget(const gl::Context *context,
const gl::FramebufferAttachment *attachment,
RenderTargetMtl **cachedRenderTarget)
{
RenderTargetMtl *newRenderTarget = nullptr;
if (attachment)
{
ASSERT(attachment->isAttached());
ANGLE_TRY(attachment->getRenderTarget(context, attachment->getRenderToTextureSamples(),
&newRenderTarget));
}
*cachedRenderTarget = newRenderTarget;
return angle::Result::Continue;
}
angle::Result FramebufferMtl::prepareRenderPass(const gl::Context *context,
gl::DrawBufferMask drawColorBuffers,
mtl::RenderPassDesc *pDescOut)
{
auto &desc = *pDescOut;
desc.numColorAttachments = static_cast<uint32_t>(drawColorBuffers.count());
size_t attachmentIdx = 0;
for (size_t colorIndexGL : drawColorBuffers)
{
if (colorIndexGL >= mtl::kMaxRenderTargets)
{
continue;
}
const RenderTargetMtl *colorRenderTarget = mColorRenderTargets[colorIndexGL];
ASSERT(colorRenderTarget);
mtl::RenderPassColorAttachmentDesc &colorAttachment =
desc.colorAttachments[attachmentIdx++];
colorRenderTarget->toRenderPassAttachmentDesc(&colorAttachment);
}
if (mDepthRenderTarget)
{
mDepthRenderTarget->toRenderPassAttachmentDesc(&desc.depthAttachment);
}
if (mStencilRenderTarget)
{
mStencilRenderTarget->toRenderPassAttachmentDesc(&desc.stencilAttachment);
}
return angle::Result::Continue;
}
// Override clear color based on texture's write mask
void FramebufferMtl::overrideClearColor(const mtl::TextureRef &texture,
MTLClearColor clearColor,
MTLClearColor *colorOut)
{
*colorOut = mtl::EmulatedAlphaClearColor(clearColor, texture->getColorWritableMask());
}
angle::Result FramebufferMtl::clearWithLoadOp(const gl::Context *context,
gl::DrawBufferMask clearColorBuffers,
const mtl::ClearRectParams &clearOpts)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
bool startedRenderPass = contextMtl->hasStartedRenderPass(mRenderPassDesc);
mtl::RenderCommandEncoder *encoder = nullptr;
mtl::RenderPassDesc tempDesc = mRenderPassDesc;
if (startedRenderPass)
{
encoder = contextMtl->getRenderCommandEncoder();
}
size_t attachmentCount = 0;
for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
{
ASSERT(colorIndexGL < mtl::kMaxRenderTargets);
uint32_t attachmentIdx = static_cast<uint32_t>(attachmentCount++);
mtl::RenderPassColorAttachmentDesc &colorAttachment =
tempDesc.colorAttachments[attachmentIdx];
const mtl::TextureRef &texture = colorAttachment.texture;
if (clearColorBuffers.test(colorIndexGL))
{
if (startedRenderPass)
{
// Render pass already started, and we want to clear this buffer,
// then discard its content before clearing.
encoder->setColorStoreAction(MTLStoreActionDontCare, attachmentIdx);
}
colorAttachment.loadAction = MTLLoadActionClear;
overrideClearColor(texture, clearOpts.clearColor.value(), &colorAttachment.clearColor);
}
else if (startedRenderPass)
{
// If render pass already started and we don't want to clear this buffer,
// then store it with current render encoder and load it before clearing step
encoder->setColorStoreAction(MTLStoreActionStore, attachmentIdx);
colorAttachment.loadAction = MTLLoadActionLoad;
}
}
MTLStoreAction preClearDethpStoreAction = MTLStoreActionStore,
preClearStencilStoreAction = MTLStoreActionStore;
if (clearOpts.clearDepth.valid())
{
preClearDethpStoreAction = MTLStoreActionDontCare;
tempDesc.depthAttachment.loadAction = MTLLoadActionClear;
tempDesc.depthAttachment.clearDepth = clearOpts.clearDepth.value();
}
else if (startedRenderPass)
{
// If render pass already started and we don't want to clear this buffer,
// then store it with current render encoder and load it before clearing step
preClearDethpStoreAction = MTLStoreActionStore;
tempDesc.depthAttachment.loadAction = MTLLoadActionLoad;
}
if (clearOpts.clearStencil.valid())
{
preClearStencilStoreAction = MTLStoreActionDontCare;
tempDesc.stencilAttachment.loadAction = MTLLoadActionClear;
tempDesc.stencilAttachment.clearStencil = clearOpts.clearStencil.value();
}
else if (startedRenderPass)
{
// If render pass already started and we don't want to clear this buffer,
// then store it with current render encoder and load it before clearing step
preClearStencilStoreAction = MTLStoreActionStore;
tempDesc.stencilAttachment.loadAction = MTLLoadActionLoad;
}
// End current render encoder.
if (startedRenderPass)
{
encoder->setDepthStencilStoreAction(preClearDethpStoreAction, preClearStencilStoreAction);
contextMtl->endEncoding(encoder);
}
// Start new render encoder with loadOp=Clear
contextMtl->getRenderCommandEncoder(tempDesc);
return angle::Result::Continue;
}
angle::Result FramebufferMtl::clearWithDraw(const gl::Context *context,
gl::DrawBufferMask clearColorBuffers,
const mtl::ClearRectParams &clearOpts)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
DisplayMtl *display = contextMtl->getDisplay();
// Start new render encoder if not already.
mtl::RenderCommandEncoder *encoder = contextMtl->getRenderCommandEncoder(mRenderPassDesc);
display->getUtils().clearWithDraw(context, encoder, clearOpts);
return angle::Result::Continue;
}
angle::Result FramebufferMtl::clearImpl(const gl::Context *context,
gl::DrawBufferMask clearColorBuffers,
mtl::ClearRectParams *pClearOpts)
{
auto &clearOpts = *pClearOpts;
if (!clearOpts.clearColor.valid() && !clearOpts.clearDepth.valid() &&
!clearOpts.clearStencil.valid())
{
// No Op.
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
const gl::Rectangle renderArea(0, 0, mState.getDimensions().width,
mState.getDimensions().height);
clearOpts.clearArea = ClipRectToScissor(contextMtl->getState(), renderArea, false);
clearOpts.flipY = mFlipY;
// Discard clear altogether if scissor has 0 width or height.
if (clearOpts.clearArea.width == 0 || clearOpts.clearArea.height == 0)
{
return angle::Result::Continue;
}
MTLColorWriteMask colorMask = contextMtl->getColorMask();
uint32_t stencilMask = contextMtl->getStencilMask();
if (!contextMtl->getDepthMask())
{
// Disable depth clearing, since depth write is disable
clearOpts.clearDepth.reset();
}
if (clearOpts.clearArea == renderArea &&
(!clearOpts.clearColor.valid() || colorMask == MTLColorWriteMaskAll) &&
(!clearOpts.clearStencil.valid() ||
(stencilMask & mtl::kStencilMaskAll) == mtl::kStencilMaskAll))
{
return clearWithLoadOp(context, clearColorBuffers, clearOpts);
}
return clearWithDraw(context, clearColorBuffers, clearOpts);
}
angle::Result FramebufferMtl::invalidateImpl(ContextMtl *contextMtl,
size_t count,
const GLenum *attachments)
{
gl::DrawBufferMask invalidateColorBuffers;
bool invalidateDepthBuffer = false;
bool invalidateStencilBuffer = false;
for (size_t i = 0; i < count; ++i)
{
const GLenum attachment = attachments[i];
switch (attachment)
{
case GL_DEPTH:
case GL_DEPTH_ATTACHMENT:
invalidateDepthBuffer = true;
break;
case GL_STENCIL:
case GL_STENCIL_ATTACHMENT:
invalidateStencilBuffer = true;
break;
case GL_DEPTH_STENCIL_ATTACHMENT:
invalidateDepthBuffer = true;
invalidateStencilBuffer = true;
break;
default:
ASSERT(
(attachment >= GL_COLOR_ATTACHMENT0 && attachment <= GL_COLOR_ATTACHMENT15) ||
(attachment == GL_COLOR));
invalidateColorBuffers.set(
attachment == GL_COLOR ? 0u : (attachment - GL_COLOR_ATTACHMENT0));
}
}
// Set the appropriate storeOp for attachments.
// If we already start the render pass, then need to set the store action now.
bool renderPassStarted = contextMtl->hasStartedRenderPass(mRenderPassDesc);
mtl::RenderCommandEncoder *encoder =
renderPassStarted ? contextMtl->getRenderCommandEncoder() : nullptr;
for (uint32_t i = 0; i < mRenderPassDesc.numColorAttachments; ++i)
{
if (invalidateColorBuffers.test(i))
{
mtl::RenderPassColorAttachmentDesc &colorAttachment =
mRenderPassDesc.colorAttachments[i];
colorAttachment.storeAction = MTLStoreActionDontCare;
if (renderPassStarted)
{
encoder->setColorStoreAction(MTLStoreActionDontCare, i);
}
}
}
if (invalidateDepthBuffer && mDepthRenderTarget)
{
mRenderPassDesc.depthAttachment.storeAction = MTLStoreActionDontCare;
if (renderPassStarted)
{
encoder->setDepthStoreAction(MTLStoreActionDontCare);
}
}
if (invalidateStencilBuffer && mStencilRenderTarget)
{
mRenderPassDesc.stencilAttachment.storeAction = MTLStoreActionDontCare;
if (renderPassStarted)
{
encoder->setStencilStoreAction(MTLStoreActionDontCare);
}
}
return angle::Result::Continue;
}
gl::Rectangle FramebufferMtl::getReadPixelArea(const gl::Rectangle &glArea)
{
RenderTargetMtl *colorReadRT = getColorReadRenderTarget();
ASSERT(colorReadRT);
gl::Rectangle flippedArea = glArea;
if (mFlipY)
{
flippedArea.y =
colorReadRT->getTexture()->height(static_cast<uint32_t>(colorReadRT->getLevelIndex())) -
flippedArea.y - flippedArea.height;
}
return flippedArea;
}
angle::Result FramebufferMtl::readPixelsImpl(const gl::Context *context,
const gl::Rectangle &area,
const PackPixelsParams &packPixelsParams,
RenderTargetMtl *renderTarget,
uint8_t *pixels)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
if (packPixelsParams.packBuffer)
{
// NOTE(hqle): PBO is not supported atm
ANGLE_MTL_CHECK(contextMtl, false, GL_INVALID_OPERATION);
}
if (!renderTarget)
{
return angle::Result::Continue;
}
const mtl::TextureRef &texture = renderTarget->getTexture();
if (!texture)
{
return angle::Result::Continue;
}
const mtl::Format &readFormat = *renderTarget->getFormat();
const angle::Format &readAngleFormat = readFormat.actualAngleFormat();
// NOTE(hqle): resolve MSAA texture before readback
int srcRowPitch = area.width * readAngleFormat.pixelBytes;
angle::MemoryBuffer readPixelRowBuffer;
ANGLE_CHECK_GL_ALLOC(contextMtl, readPixelRowBuffer.resize(srcRowPitch));
auto packPixelsRowParams = packPixelsParams;
MTLRegion mtlSrcRowRegion = MTLRegionMake2D(area.x, area.y, area.width, 1);
int rowOffset = packPixelsParams.reverseRowOrder ? -1 : 1;
int startRow = packPixelsParams.reverseRowOrder ? (area.y1() - 1) : area.y;
// Copy pixels row by row
packPixelsRowParams.area.height = 1;
packPixelsRowParams.reverseRowOrder = false;
for (int r = startRow, i = 0; i < area.height;
++i, r += rowOffset, pixels += packPixelsRowParams.outputPitch)
{
mtlSrcRowRegion.origin.y = r;
packPixelsRowParams.area.y = packPixelsParams.area.y + i;
// Read the pixels data to the row buffer
texture->getBytes(contextMtl, srcRowPitch, mtlSrcRowRegion,
static_cast<uint32_t>(renderTarget->getLevelIndex()),
readPixelRowBuffer.data());
// Convert to destination format
PackPixels(packPixelsRowParams, readAngleFormat, srcRowPitch, readPixelRowBuffer.data(),
pixels);
}
return angle::Result::Continue;
}
}