Hash :
b16d69c3
Author :
Date :
2019-05-13T16:28:27
Vulkan: Add support for surface multisampling A multisample image is created for the surface if multisampling is enabled. Prior to present, this multisample image is resolved into the swapchain image. FramebufferVk::readPixelsImpl similarly has got the ability to resolve the region of interest into a temporary image prior to readback. Tests are added to render a point, line and a triangle on a 4x multisampled surface. Bug: angleproject:3204 Change-Id: I34aca502fa1918b5cbf000ff11521c350372e051 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1610188 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Shahbaz Youssefi <syoussefi@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 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
//
// 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.
//
// FramebufferVk.cpp:
// Implements the class methods for FramebufferVk.
//
#include "libANGLE/renderer/vulkan/FramebufferVk.h"
#include <vulkan/vulkan.h>
#include <array>
#include "common/debug.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/formatutils.h"
#include "libANGLE/renderer/renderer_utils.h"
#include "libANGLE/renderer/vulkan/CommandGraph.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/DisplayVk.h"
#include "libANGLE/renderer/vulkan/RenderTargetVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/renderer/vulkan/SurfaceVk.h"
#include "libANGLE/renderer/vulkan/vk_format_utils.h"
#include "third_party/trace_event/trace_event.h"
namespace rx
{
namespace
{
// The value to assign an alpha channel that's emulated. The type is unsigned int, though it will
// automatically convert to the actual data type.
constexpr unsigned int kEmulatedAlphaValue = 1;
constexpr size_t kMinReadPixelsBufferSize = 128000;
// Clear values are only used when loadOp=Clear is set in clearWithRenderPassOp. When starting a
// new render pass, the clear value is set to an unlikely value (bright pink) to stand out better
// in case of a bug.
constexpr VkClearValue kUninitializedClearValue = {{{0.95, 0.05, 0.95, 0.95}}};
const gl::InternalFormat &GetReadAttachmentInfo(const gl::Context *context,
RenderTargetVk *renderTarget)
{
GLenum implFormat =
renderTarget->getImageFormat().imageFormat().fboImplementationInternalFormat;
return gl::GetSizedInternalFormatInfo(implFormat);
}
bool ClipToRenderTarget(const gl::Rectangle &area,
RenderTargetVk *renderTarget,
gl::Rectangle *rectOut)
{
const gl::Extents renderTargetExtents = renderTarget->getExtents();
gl::Rectangle renderTargetRect(0, 0, renderTargetExtents.width, renderTargetExtents.height);
return ClipRectangle(area, renderTargetRect, rectOut);
}
bool HasSrcAndDstBlitProperties(RendererVk *renderer,
RenderTargetVk *srcRenderTarget,
RenderTargetVk *dstRenderTarget)
{
const VkFormat srcFormat = srcRenderTarget->getImageFormat().vkImageFormat;
const VkFormat dstFormat = dstRenderTarget->getImageFormat().vkImageFormat;
// Verifies if the draw and read images have the necessary prerequisites for blitting.
return renderer->hasImageFormatFeatureBits(srcFormat, VK_FORMAT_FEATURE_BLIT_SRC_BIT) &&
renderer->hasImageFormatFeatureBits(dstFormat, VK_FORMAT_FEATURE_BLIT_DST_BIT);
}
// Special rules apply to VkBufferImageCopy with depth/stencil. The components are tightly packed
// into a depth or stencil section of the destination buffer. See the spec:
// https://www.khronos.org/registry/vulkan/specs/1.1-extensions/man/html/VkBufferImageCopy.html
const angle::Format &GetDepthStencilImageToBufferFormat(const angle::Format &imageFormat,
VkImageAspectFlagBits copyAspect)
{
if (copyAspect == VK_IMAGE_ASPECT_STENCIL_BIT)
{
ASSERT(imageFormat.id == angle::FormatID::D24_UNORM_S8_UINT ||
imageFormat.id == angle::FormatID::D32_FLOAT_S8X24_UINT ||
imageFormat.id == angle::FormatID::S8_UINT);
return angle::Format::Get(angle::FormatID::S8_UINT);
}
ASSERT(copyAspect == VK_IMAGE_ASPECT_DEPTH_BIT);
switch (imageFormat.id)
{
case angle::FormatID::D16_UNORM:
return imageFormat;
case angle::FormatID::D24_UNORM_X8_UINT:
return imageFormat;
case angle::FormatID::D24_UNORM_S8_UINT:
return angle::Format::Get(angle::FormatID::D24_UNORM_X8_UINT);
case angle::FormatID::D32_FLOAT:
return imageFormat;
case angle::FormatID::D32_FLOAT_S8X24_UINT:
return angle::Format::Get(angle::FormatID::D32_FLOAT);
default:
UNREACHABLE();
return imageFormat;
}
}
void SetEmulatedAlphaValue(const vk::Format &format, VkClearColorValue *value)
{
if (format.vkFormatIsInt)
{
if (format.vkFormatIsUnsigned)
{
value->uint32[3] = kEmulatedAlphaValue;
}
else
{
value->int32[3] = kEmulatedAlphaValue;
}
}
else
{
value->float32[3] = kEmulatedAlphaValue;
}
}
} // anonymous namespace
// static
FramebufferVk *FramebufferVk::CreateUserFBO(RendererVk *renderer, const gl::FramebufferState &state)
{
return new FramebufferVk(renderer, state, nullptr);
}
// static
FramebufferVk *FramebufferVk::CreateDefaultFBO(RendererVk *renderer,
const gl::FramebufferState &state,
WindowSurfaceVk *backbuffer)
{
return new FramebufferVk(renderer, state, backbuffer);
}
FramebufferVk::FramebufferVk(RendererVk *renderer,
const gl::FramebufferState &state,
WindowSurfaceVk *backbuffer)
: FramebufferImpl(state),
mBackbuffer(backbuffer),
mActiveColorComponents(0),
mReadPixelBuffer(VK_BUFFER_USAGE_TRANSFER_DST_BIT, kMinReadPixelsBufferSize, true),
mBlitPixelBuffer(VK_BUFFER_USAGE_TRANSFER_SRC_BIT, kMinReadPixelsBufferSize, true)
{
mBlitPixelBuffer.init(1, renderer);
mReadPixelBuffer.init(4, renderer);
}
FramebufferVk::~FramebufferVk() = default;
void FramebufferVk::destroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
RendererVk *renderer = contextVk->getRenderer();
mFramebuffer.release(renderer);
mReadPixelBuffer.release(renderer);
mBlitPixelBuffer.release(renderer);
}
angle::Result FramebufferVk::discard(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
return angle::Result::Stop;
}
angle::Result FramebufferVk::invalidate(const gl::Context *context,
size_t count,
const GLenum *attachments)
{
ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
return angle::Result::Stop;
}
angle::Result FramebufferVk::invalidateSub(const gl::Context *context,
size_t count,
const GLenum *attachments,
const gl::Rectangle &area)
{
ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
return angle::Result::Stop;
}
angle::Result FramebufferVk::clear(const gl::Context *context, GLbitfield mask)
{
ContextVk *contextVk = vk::GetImpl(context);
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();
}
const VkClearColorValue &clearColorValue = contextVk->getClearColorValue().color;
const VkClearDepthStencilValue &clearDepthStencilValue =
contextVk->getClearDepthStencilValue().depthStencil;
return clearImpl(context, clearColorBuffers, clearDepth, clearStencil, clearColorValue,
clearDepthStencilValue);
}
angle::Result FramebufferVk::clearImpl(const gl::Context *context,
gl::DrawBufferMask clearColorBuffers,
bool clearDepth,
bool clearStencil,
const VkClearColorValue &clearColorValue,
const VkClearDepthStencilValue &clearDepthStencilValue)
{
ContextVk *contextVk = vk::GetImpl(context);
const gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
// Discard clear altogether if scissor has 0 width or height.
if (scissoredRenderArea.width == 0 || scissoredRenderArea.height == 0)
{
return angle::Result::Continue;
}
mFramebuffer.updateQueueSerial(contextVk->getRenderer()->getCurrentQueueSerial());
// This function assumes that only enabled attachments are asked to be cleared.
ASSERT((clearColorBuffers & mState.getEnabledDrawBuffers()) == clearColorBuffers);
// Adjust clear behavior based on whether the respective attachments are present; if asked to
// clear a non-existent attachment, don't attempt to clear it.
VkColorComponentFlags colorMaskFlags = contextVk->getClearColorMask();
bool clearColor = clearColorBuffers.any();
const gl::FramebufferAttachment *depthAttachment = mState.getDepthAttachment();
clearDepth = clearDepth && depthAttachment;
ASSERT(!clearDepth || depthAttachment->isAttached());
const gl::FramebufferAttachment *stencilAttachment = mState.getStencilAttachment();
clearStencil = clearStencil && stencilAttachment;
ASSERT(!clearStencil || stencilAttachment->isAttached());
uint8_t stencilMask =
static_cast<uint8_t>(contextVk->getState().getDepthStencilState().stencilWritemask);
// The front-end should ensure we don't attempt to clear color if all channels are masked.
ASSERT(!clearColor || colorMaskFlags != 0);
// The front-end should ensure we don't attempt to clear depth if depth write is disabled.
ASSERT(!clearDepth || contextVk->getState().getDepthStencilState().depthMask);
// The front-end should ensure we don't attempt to clear stencil if all bits are masked.
ASSERT(!clearStencil || stencilMask != 0);
// If there is nothing to clear, return right away (for example, if asked to clear depth, but
// there is no depth attachment).
if (!clearColor && !clearDepth && !clearStencil)
{
return angle::Result::Continue;
}
VkClearDepthStencilValue modifiedDepthStencilValue = clearDepthStencilValue;
// We can use render pass load ops if clearing depth, unmasked color or unmasked stencil. If
// there's a depth mask, depth clearing is already disabled.
bool maskedClearColor =
clearColor && (mActiveColorComponents & colorMaskFlags) != mActiveColorComponents;
bool maskedClearStencil = stencilMask != 0xFF;
bool clearColorWithRenderPassLoadOp = clearColor && !maskedClearColor;
bool clearStencilWithRenderPassLoadOp = clearStencil && !maskedClearStencil;
// At least one of color, depth or stencil should be clearable with render pass loadOp for us
// to use this clear path.
bool clearAnyWithRenderPassLoadOp =
clearColorWithRenderPassLoadOp || clearDepth || clearStencilWithRenderPassLoadOp;
if (clearAnyWithRenderPassLoadOp)
{
// Clearing color is indicated by the set bits in this mask. If not clearing colors with
// render pass loadOp, the default value of all-zeros means the clear is not done in
// clearWithRenderPassOp below. In that case, only clear depth/stencil with render pass
// loadOp.
gl::DrawBufferMask clearBuffersWithRenderPassLoadOp;
if (clearColorWithRenderPassLoadOp)
{
clearBuffersWithRenderPassLoadOp = clearColorBuffers;
}
ANGLE_TRY(clearWithRenderPassOp(
contextVk, scissoredRenderArea, clearBuffersWithRenderPassLoadOp, clearDepth,
clearStencilWithRenderPassLoadOp, clearColorValue, modifiedDepthStencilValue));
// On some hardware, having inline commands at this point results in corrupted output. In
// that case, end the render pass immediately. http://anglebug.com/2361
if (contextVk->getRenderer()->getFeatures().restartRenderPassAfterLoadOpClear.enabled)
{
mFramebuffer.finishCurrentCommands(contextVk->getRenderer());
}
// Fallback to other methods for whatever isn't cleared here.
clearDepth = false;
if (clearColorWithRenderPassLoadOp)
{
clearColorBuffers.reset();
clearColor = false;
}
if (clearStencilWithRenderPassLoadOp)
{
clearStencil = false;
}
// If nothing left to clear, early out.
if (!clearColor && !clearStencil)
{
return angle::Result::Continue;
}
}
// Note: depth clear is always done through render pass loadOp.
ASSERT(clearDepth == false);
// The most costly clear mode is when we need to mask out specific color channels or stencil
// bits. This can only be done with a draw call.
return clearWithDraw(contextVk, scissoredRenderArea, clearColorBuffers, clearStencil,
colorMaskFlags, stencilMask, clearColorValue,
static_cast<uint8_t>(modifiedDepthStencilValue.stencil));
}
angle::Result FramebufferVk::clearBufferfv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLfloat *values)
{
VkClearValue clearValue = {};
bool clearDepth = false;
gl::DrawBufferMask clearColorBuffers;
if (buffer == GL_DEPTH)
{
clearDepth = true;
clearValue.depthStencil.depth = values[0];
}
else
{
clearColorBuffers.set(drawbuffer);
clearValue.color.float32[0] = values[0];
clearValue.color.float32[1] = values[1];
clearValue.color.float32[2] = values[2];
clearValue.color.float32[3] = values[3];
}
return clearImpl(context, clearColorBuffers, clearDepth, false, clearValue.color,
clearValue.depthStencil);
}
angle::Result FramebufferVk::clearBufferuiv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLuint *values)
{
VkClearValue clearValue = {};
gl::DrawBufferMask clearColorBuffers;
clearColorBuffers.set(drawbuffer);
clearValue.color.uint32[0] = values[0];
clearValue.color.uint32[1] = values[1];
clearValue.color.uint32[2] = values[2];
clearValue.color.uint32[3] = values[3];
return clearImpl(context, clearColorBuffers, false, false, clearValue.color,
clearValue.depthStencil);
}
angle::Result FramebufferVk::clearBufferiv(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
const GLint *values)
{
VkClearValue clearValue = {};
bool clearStencil = false;
gl::DrawBufferMask clearColorBuffers;
if (buffer == GL_STENCIL)
{
clearStencil = true;
clearValue.depthStencil.stencil =
gl::clamp(values[0], 0, std::numeric_limits<uint8_t>::max());
}
else
{
clearColorBuffers.set(drawbuffer);
clearValue.color.int32[0] = values[0];
clearValue.color.int32[1] = values[1];
clearValue.color.int32[2] = values[2];
clearValue.color.int32[3] = values[3];
}
return clearImpl(context, clearColorBuffers, false, clearStencil, clearValue.color,
clearValue.depthStencil);
}
angle::Result FramebufferVk::clearBufferfi(const gl::Context *context,
GLenum buffer,
GLint drawbuffer,
GLfloat depth,
GLint stencil)
{
VkClearValue clearValue = {};
clearValue.depthStencil.depth = depth;
clearValue.depthStencil.stencil = gl::clamp(stencil, 0, std::numeric_limits<uint8_t>::max());
return clearImpl(context, gl::DrawBufferMask(), true, true, clearValue.color,
clearValue.depthStencil);
}
GLenum FramebufferVk::getImplementationColorReadFormat(const gl::Context *context) const
{
return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).format;
}
GLenum FramebufferVk::getImplementationColorReadType(const gl::Context *context) const
{
return GetReadAttachmentInfo(context, mRenderTargetCache.getColorRead(mState)).type;
}
angle::Result FramebufferVk::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);
ContextVk *contextVk = vk::GetImpl(context);
RendererVk *renderer = contextVk->getRenderer();
gl::Rectangle clippedArea;
if (!ClipRectangle(area, fbRect, &clippedArea))
{
// nothing to read
return angle::Result::Continue;
}
gl::Rectangle flippedArea = clippedArea;
if (contextVk->isViewportFlipEnabledForReadFBO())
{
flippedArea.y = fbRect.height - flippedArea.y - flippedArea.height;
}
const gl::State &glState = context->getState();
const gl::PixelPackState &packState = glState.getPackState();
const gl::InternalFormat &sizedFormatInfo = gl::GetInternalFormatInfo(format, type);
GLuint outputPitch = 0;
ANGLE_VK_CHECK_MATH(contextVk,
sizedFormatInfo.computeRowPitch(type, area.width, packState.alignment,
packState.rowLength, &outputPitch));
GLuint outputSkipBytes = 0;
ANGLE_VK_CHECK_MATH(contextVk, 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 (contextVk->isViewportFlipEnabledForReadFBO())
{
params.reverseRowOrder = !params.reverseRowOrder;
}
ANGLE_TRY(readPixelsImpl(contextVk, flippedArea, params, VK_IMAGE_ASPECT_COLOR_BIT,
getColorReadRenderTarget(),
static_cast<uint8_t *>(pixels) + outputSkipBytes));
mReadPixelBuffer.releaseRetainedBuffers(renderer);
return angle::Result::Continue;
}
RenderTargetVk *FramebufferVk::getDepthStencilRenderTarget() const
{
return mRenderTargetCache.getDepthStencil();
}
angle::Result FramebufferVk::blitWithCopy(ContextVk *contextVk,
const gl::Rectangle ©Area,
RenderTargetVk *readRenderTarget,
RenderTargetVk *drawRenderTarget,
bool blitDepthBuffer,
bool blitStencilBuffer)
{
VkImageAspectFlags aspectMask =
vk::GetDepthStencilAspectFlagsForCopy(blitDepthBuffer, blitStencilBuffer);
vk::CommandBuffer *commandBuffer = nullptr;
ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
vk::ImageHelper *writeImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
writeImage->changeLayout(writeImage->getAspectFlags(), vk::ImageLayout::TransferDst,
commandBuffer);
vk::ImageHelper *readImage = readRenderTarget->getImageForRead(
&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
VkImageSubresourceLayers readSubresource = {};
readSubresource.aspectMask = aspectMask;
readSubresource.mipLevel = 0;
readSubresource.baseArrayLayer = 0;
readSubresource.layerCount = 1;
VkImageSubresourceLayers writeSubresource = readSubresource;
vk::ImageHelper::Copy(readImage, writeImage, gl::Offset(), gl::Offset(),
gl::Extents(copyArea.width, copyArea.height, 1), readSubresource,
writeSubresource, commandBuffer);
return angle::Result::Continue;
}
RenderTargetVk *FramebufferVk::getColorReadRenderTarget() const
{
RenderTargetVk *renderTarget = mRenderTargetCache.getColorRead(mState);
ASSERT(renderTarget && renderTarget->getImage().valid());
return renderTarget;
}
angle::Result FramebufferVk::blitWithReadback(ContextVk *contextVk,
const gl::Rectangle ©Area,
VkImageAspectFlagBits aspect,
RenderTargetVk *readRenderTarget,
RenderTargetVk *drawRenderTarget)
{
RendererVk *renderer = contextVk->getRenderer();
const angle::Format &readFormat = readRenderTarget->getImageFormat().imageFormat();
ASSERT(aspect == VK_IMAGE_ASPECT_DEPTH_BIT || aspect == VK_IMAGE_ASPECT_STENCIL_BIT);
// This path is only currently used for y-flipping depth/stencil blits.
PackPixelsParams packPixelsParams;
packPixelsParams.reverseRowOrder = true;
packPixelsParams.area.width = copyArea.width;
packPixelsParams.area.height = copyArea.height;
packPixelsParams.area.x = copyArea.x;
packPixelsParams.area.y = copyArea.y;
// Read back depth values into the destination buffer.
const angle::Format ©Format = GetDepthStencilImageToBufferFormat(readFormat, aspect);
packPixelsParams.destFormat = ©Format;
packPixelsParams.outputPitch = copyFormat.pixelBytes * copyArea.width;
// Allocate a space in the destination buffer to write to.
size_t blitAllocationSize = copyFormat.pixelBytes * copyArea.width * copyArea.height;
uint8_t *destPtr = nullptr;
VkBuffer destBufferHandle = VK_NULL_HANDLE;
VkDeviceSize destOffset = 0;
ANGLE_TRY(mBlitPixelBuffer.allocate(contextVk, blitAllocationSize, &destPtr, &destBufferHandle,
&destOffset, nullptr));
ANGLE_TRY(
readPixelsImpl(contextVk, copyArea, packPixelsParams, aspect, readRenderTarget, destPtr));
VkBufferImageCopy copyRegion = {};
copyRegion.bufferOffset = destOffset;
copyRegion.bufferImageHeight = copyArea.height;
copyRegion.bufferRowLength = copyArea.width;
copyRegion.imageExtent.width = copyArea.width;
copyRegion.imageExtent.height = copyArea.height;
copyRegion.imageExtent.depth = 1;
copyRegion.imageSubresource.mipLevel = 0;
copyRegion.imageSubresource.aspectMask = aspect;
copyRegion.imageSubresource.baseArrayLayer = 0;
copyRegion.imageSubresource.layerCount = 1;
copyRegion.imageOffset.x = copyArea.x;
copyRegion.imageOffset.y = copyArea.y;
copyRegion.imageOffset.z = 0;
ANGLE_TRY(mBlitPixelBuffer.flush(contextVk));
// Reinitialize the commandBuffer after a read pixels because it calls
// renderer->finish which makes command buffers obsolete.
vk::CommandBuffer *commandBuffer = nullptr;
ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
// We read the bytes of the image in a buffer, now we have to copy them into the
// destination target.
vk::ImageHelper *imageForWrite = drawRenderTarget->getImageForWrite(&mFramebuffer);
imageForWrite->changeLayout(imageForWrite->getAspectFlags(), vk::ImageLayout::TransferDst,
commandBuffer);
commandBuffer->copyBufferToImage(destBufferHandle, imageForWrite->getImage(),
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Region);
mBlitPixelBuffer.releaseRetainedBuffers(renderer);
return angle::Result::Continue;
}
angle::Result FramebufferVk::blit(const gl::Context *context,
const gl::Rectangle &sourceArea,
const gl::Rectangle &destArea,
GLbitfield mask,
GLenum filter)
{
ContextVk *contextVk = vk::GetImpl(context);
RendererVk *renderer = contextVk->getRenderer();
const gl::State &glState = context->getState();
const gl::Framebuffer *sourceFramebuffer = glState.getReadFramebuffer();
bool blitColorBuffer = (mask & GL_COLOR_BUFFER_BIT) != 0;
bool blitDepthBuffer = (mask & GL_DEPTH_BUFFER_BIT) != 0;
bool blitStencilBuffer = (mask & GL_STENCIL_BUFFER_BIT) != 0;
FramebufferVk *sourceFramebufferVk = vk::GetImpl(sourceFramebuffer);
bool flipSource = contextVk->isViewportFlipEnabledForReadFBO();
bool flipDest = contextVk->isViewportFlipEnabledForDrawFBO();
gl::Rectangle readRect = sourceArea;
gl::Rectangle drawRect = destArea;
if (glState.isScissorTestEnabled())
{
const gl::Rectangle scissorRect = glState.getScissor();
if (!ClipRectangle(sourceArea, scissorRect, &readRect))
{
return angle::Result::Continue;
}
if (!ClipRectangle(destArea, scissorRect, &drawRect))
{
return angle::Result::Continue;
}
}
// After cropping for the scissor, we also want to crop for the size of the buffers.
if (blitColorBuffer)
{
RenderTargetVk *readRenderTarget = sourceFramebufferVk->getColorReadRenderTarget();
gl::Rectangle readRenderTargetRect;
if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect))
{
return angle::Result::Continue;
}
for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
{
RenderTargetVk *drawRenderTarget = mRenderTargetCache.getColors()[colorIndexGL];
ASSERT(drawRenderTarget);
ASSERT(HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget));
gl::Rectangle drawRenderTargetRect;
if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect))
{
return angle::Result::Continue;
}
ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect,
readRenderTarget, drawRenderTarget, filter, true, false,
false, flipSource, flipDest));
}
}
if (blitDepthBuffer || blitStencilBuffer)
{
RenderTargetVk *readRenderTarget = sourceFramebufferVk->getDepthStencilRenderTarget();
ASSERT(readRenderTarget);
gl::Rectangle readRenderTargetRect;
if (!ClipToRenderTarget(readRect, readRenderTarget, &readRenderTargetRect))
{
return angle::Result::Continue;
}
RenderTargetVk *drawRenderTarget = mRenderTargetCache.getDepthStencil();
ASSERT(drawRenderTarget);
gl::Rectangle drawRenderTargetRect;
if (!ClipToRenderTarget(drawRect, drawRenderTarget, &drawRenderTargetRect))
{
return angle::Result::Continue;
}
ASSERT(readRenderTargetRect == drawRenderTargetRect);
ASSERT(filter == GL_NEAREST);
if (HasSrcAndDstBlitProperties(renderer, readRenderTarget, drawRenderTarget))
{
ANGLE_TRY(blitWithCommand(contextVk, readRenderTargetRect, drawRenderTargetRect,
readRenderTarget, drawRenderTarget, filter, false,
blitDepthBuffer, blitStencilBuffer, flipSource, flipDest));
}
else
{
if (flipSource || flipDest)
{
if (blitDepthBuffer)
{
ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect,
VK_IMAGE_ASPECT_DEPTH_BIT, readRenderTarget,
drawRenderTarget));
}
if (blitStencilBuffer)
{
ANGLE_TRY(blitWithReadback(contextVk, readRenderTargetRect,
VK_IMAGE_ASPECT_STENCIL_BIT, readRenderTarget,
drawRenderTarget));
}
}
else
{
ANGLE_TRY(blitWithCopy(contextVk, readRenderTargetRect, readRenderTarget,
drawRenderTarget, blitDepthBuffer, blitStencilBuffer));
}
}
}
return angle::Result::Continue;
}
angle::Result FramebufferVk::blitWithCommand(ContextVk *contextVk,
const gl::Rectangle &readRectIn,
const gl::Rectangle &drawRectIn,
RenderTargetVk *readRenderTarget,
RenderTargetVk *drawRenderTarget,
GLenum filter,
bool colorBlit,
bool depthBlit,
bool stencilBlit,
bool flipSource,
bool flipDest)
{
// Since blitRenderbufferRect is called for each render buffer that needs to be blitted,
// it should never be the case that both color and depth/stencil need to be blitted at
// at the same time.
ASSERT(colorBlit != (depthBlit || stencilBlit));
vk::ImageHelper *dstImage = drawRenderTarget->getImageForWrite(&mFramebuffer);
vk::CommandBuffer *commandBuffer = nullptr;
ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
const vk::Format &readImageFormat = readRenderTarget->getImageFormat();
VkImageAspectFlags aspectMask =
colorBlit ? VK_IMAGE_ASPECT_COLOR_BIT
: vk::GetDepthStencilAspectFlags(readImageFormat.imageFormat());
vk::ImageHelper *srcImage = readRenderTarget->getImageForRead(
&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
const gl::Extents sourceFrameBufferExtents = readRenderTarget->getExtents();
gl::Rectangle readRect = readRectIn;
if (flipSource)
{
readRect.y = sourceFrameBufferExtents.height - readRect.y - readRect.height;
}
VkImageBlit blit = {};
blit.srcOffsets[0] = {readRect.x0(), flipSource ? readRect.y1() : readRect.y0(), 0};
blit.srcOffsets[1] = {readRect.x1(), flipSource ? readRect.y0() : readRect.y1(), 1};
blit.srcSubresource.aspectMask = aspectMask;
blit.srcSubresource.mipLevel = 0;
blit.srcSubresource.baseArrayLayer = 0;
blit.srcSubresource.layerCount = 1;
blit.dstSubresource.aspectMask = aspectMask;
blit.dstSubresource.mipLevel = 0;
blit.dstSubresource.baseArrayLayer = 0;
blit.dstSubresource.layerCount = 1;
const gl::Extents drawFrameBufferExtents = drawRenderTarget->getExtents();
gl::Rectangle drawRect = drawRectIn;
if (flipDest)
{
drawRect.y = drawFrameBufferExtents.height - drawRect.y - drawRect.height;
}
blit.dstOffsets[0] = {drawRect.x0(), flipDest ? drawRect.y1() : drawRect.y0(), 0};
blit.dstOffsets[1] = {drawRect.x1(), flipDest ? drawRect.y0() : drawRect.y1(), 1};
// Requirement of the copyImageToBuffer, the dst image must be in
// VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL layout.
dstImage->changeLayout(aspectMask, vk::ImageLayout::TransferDst, commandBuffer);
commandBuffer->blitImage(srcImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
dstImage->getImage(), VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &blit,
gl_vk::GetFilter(filter));
return angle::Result::Continue;
}
bool FramebufferVk::checkStatus(const gl::Context *context) const
{
// if we have both a depth and stencil buffer, they must refer to the same object
// since we only support packed_depth_stencil and not separate depth and stencil
if (mState.hasSeparateDepthAndStencilAttachments())
{
return false;
}
return true;
}
angle::Result FramebufferVk::syncState(const gl::Context *context,
const gl::Framebuffer::DirtyBits &dirtyBits)
{
ContextVk *contextVk = vk::GetImpl(context);
RendererVk *renderer = contextVk->getRenderer();
ASSERT(dirtyBits.any());
for (size_t dirtyBit : dirtyBits)
{
switch (dirtyBit)
{
case gl::Framebuffer::DIRTY_BIT_DEPTH_ATTACHMENT:
case gl::Framebuffer::DIRTY_BIT_STENCIL_ATTACHMENT:
ANGLE_TRY(mRenderTargetCache.updateDepthStencilRenderTarget(context, mState));
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:
{
ASSERT(gl::Framebuffer::DIRTY_BIT_COLOR_ATTACHMENT_0 == 0 &&
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(
mRenderTargetCache.updateColorRenderTarget(context, mState, colorIndexGL));
// Update cached masks for masked clears.
RenderTargetVk *renderTarget = mRenderTargetCache.getColors()[colorIndexGL];
if (renderTarget)
{
const angle::Format &emulatedFormat =
renderTarget->getImageFormat().imageFormat();
updateActiveColorMasks(
colorIndexGL, emulatedFormat.redBits > 0, emulatedFormat.greenBits > 0,
emulatedFormat.blueBits > 0, emulatedFormat.alphaBits > 0);
const angle::Format &sourceFormat =
renderTarget->getImageFormat().angleFormat();
mEmulatedAlphaAttachmentMask.set(
colorIndexGL, sourceFormat.alphaBits == 0 && emulatedFormat.alphaBits > 0);
contextVk->updateColorMask(context->getState().getBlendState());
}
else
{
updateActiveColorMasks(colorIndexGL, false, false, false, false);
}
break;
}
}
}
mActiveColorComponents = gl_vk::GetColorComponentFlags(
mActiveColorComponentMasksForClear[0].any(), mActiveColorComponentMasksForClear[1].any(),
mActiveColorComponentMasksForClear[2].any(), mActiveColorComponentMasksForClear[3].any());
mFramebuffer.release(renderer);
// Will freeze the current set of dependencies on this FBO. The next time we render we will
// create a new entry in the command graph.
mFramebuffer.finishCurrentCommands(renderer);
// Notify the ContextVk to update the pipeline desc.
updateRenderPassDesc();
contextVk->onFramebufferChange(mRenderPassDesc);
return angle::Result::Continue;
}
void FramebufferVk::updateRenderPassDesc()
{
mRenderPassDesc = {};
mRenderPassDesc.setSamples(getSamples());
const auto &colorRenderTargets = mRenderTargetCache.getColors();
const gl::DrawBufferMask enabledDrawBuffers = mState.getEnabledDrawBuffers();
for (size_t colorIndexGL = 0; colorIndexGL < enabledDrawBuffers.size(); ++colorIndexGL)
{
if (enabledDrawBuffers[colorIndexGL])
{
RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
ASSERT(colorRenderTarget);
mRenderPassDesc.packColorAttachment(
colorIndexGL, colorRenderTarget->getImage().getFormat().angleFormatID);
}
else
{
mRenderPassDesc.packColorAttachmentGap(colorIndexGL);
}
}
RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
if (depthStencilRenderTarget)
{
mRenderPassDesc.packDepthStencilAttachment(
depthStencilRenderTarget->getImage().getFormat().angleFormatID);
}
}
angle::Result FramebufferVk::getFramebuffer(ContextVk *contextVk, vk::Framebuffer **framebufferOut)
{
// If we've already created our cached Framebuffer, return it.
if (mFramebuffer.valid())
{
*framebufferOut = &mFramebuffer.getFramebuffer();
return angle::Result::Continue;
}
vk::RenderPass *compatibleRenderPass = nullptr;
ANGLE_TRY(contextVk->getRenderer()->getCompatibleRenderPass(contextVk, mRenderPassDesc,
&compatibleRenderPass));
// If we've a Framebuffer provided by a Surface (default FBO/backbuffer), query it.
if (mBackbuffer)
{
return mBackbuffer->getCurrentFramebuffer(contextVk, *compatibleRenderPass, framebufferOut);
}
// Gather VkImageViews over all FBO attachments, also size of attached region.
std::vector<VkImageView> attachments;
gl::Extents attachmentsSize;
const auto &colorRenderTargets = mRenderTargetCache.getColors();
for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
{
RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
ASSERT(colorRenderTarget);
attachments.push_back(colorRenderTarget->getDrawImageView()->getHandle());
ASSERT(attachmentsSize.empty() || attachmentsSize == colorRenderTarget->getExtents());
attachmentsSize = colorRenderTarget->getExtents();
}
RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
if (depthStencilRenderTarget)
{
attachments.push_back(depthStencilRenderTarget->getDrawImageView()->getHandle());
ASSERT(attachmentsSize.empty() ||
attachmentsSize == depthStencilRenderTarget->getExtents());
attachmentsSize = depthStencilRenderTarget->getExtents();
}
VkFramebufferCreateInfo framebufferInfo = {};
framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
framebufferInfo.flags = 0;
framebufferInfo.renderPass = compatibleRenderPass->getHandle();
framebufferInfo.attachmentCount = static_cast<uint32_t>(attachments.size());
framebufferInfo.pAttachments = attachments.data();
framebufferInfo.width = static_cast<uint32_t>(attachmentsSize.width);
framebufferInfo.height = static_cast<uint32_t>(attachmentsSize.height);
framebufferInfo.layers = 1;
ANGLE_TRY(mFramebuffer.init(contextVk, framebufferInfo));
*framebufferOut = &mFramebuffer.getFramebuffer();
return angle::Result::Continue;
}
angle::Result FramebufferVk::clearWithRenderPassOp(
ContextVk *contextVk,
const gl::Rectangle &clearArea,
gl::DrawBufferMask clearColorBuffers,
bool clearDepth,
bool clearStencil,
const VkClearColorValue &clearColorValue,
const VkClearDepthStencilValue &clearDepthStencilValue)
{
// Start a new render pass if:
//
// - no render pass has started,
// - there is a render pass started but it contains commands; we cannot modify its ops, so new
// render pass is needed,
// - the current render area doesn't match the clear area. We need the render area to be
// exactly as specified by the scissor for the loadOp to clear only that area. See
// onScissorChange for more information.
if (!mFramebuffer.valid() || !mFramebuffer.renderPassStartedButEmpty() ||
mFramebuffer.getRenderPassRenderArea() != clearArea)
{
vk::CommandBuffer *commandBuffer;
ANGLE_TRY(startNewRenderPass(contextVk, clearArea, &commandBuffer));
}
size_t attachmentIndexVk = 0;
// Go through clearColorBuffers and set the appropriate loadOp and clear values.
for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
{
if (clearColorBuffers.test(colorIndexGL))
{
RenderTargetVk *renderTarget = getColorReadRenderTarget();
// If the render target doesn't have alpha, but its emulated format has it, clear the
// alpha to 1.
VkClearColorValue value = clearColorValue;
if (mEmulatedAlphaAttachmentMask[colorIndexGL])
{
SetEmulatedAlphaValue(renderTarget->getImageFormat(), &value);
}
mFramebuffer.clearRenderPassColorAttachment(attachmentIndexVk, value);
}
++attachmentIndexVk;
}
// Set the appropriate loadOp and clear values for depth and stencil.
RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
if (depthStencilRenderTarget)
{
if (clearDepth)
{
mFramebuffer.clearRenderPassDepthAttachment(attachmentIndexVk,
clearDepthStencilValue.depth);
}
if (clearStencil)
{
mFramebuffer.clearRenderPassStencilAttachment(attachmentIndexVk,
clearDepthStencilValue.stencil);
}
}
return angle::Result::Continue;
}
angle::Result FramebufferVk::clearWithDraw(ContextVk *contextVk,
const gl::Rectangle &clearArea,
gl::DrawBufferMask clearColorBuffers,
bool clearStencil,
VkColorComponentFlags colorMaskFlags,
uint8_t stencilMask,
const VkClearColorValue &clearColorValue,
uint8_t clearStencilValue)
{
RendererVk *renderer = contextVk->getRenderer();
UtilsVk::ClearFramebufferParameters params = {};
params.renderPassDesc = &getRenderPassDesc();
params.renderAreaHeight = mState.getDimensions().height;
params.clearArea = clearArea;
params.colorClearValue = clearColorValue;
params.stencilClearValue = clearStencilValue;
params.stencilMask = stencilMask;
params.clearColor = true;
params.clearStencil = clearStencil;
const auto &colorRenderTargets = mRenderTargetCache.getColors();
for (size_t colorIndexGL : clearColorBuffers)
{
const RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
ASSERT(colorRenderTarget);
params.colorFormat = &colorRenderTarget->getImage().getFormat().imageFormat();
params.colorAttachmentIndexGL = colorIndexGL;
params.colorMaskFlags = colorMaskFlags;
if (mEmulatedAlphaAttachmentMask[colorIndexGL])
{
params.colorMaskFlags &= ~VK_COLOR_COMPONENT_A_BIT;
}
ANGLE_TRY(renderer->getUtils().clearFramebuffer(contextVk, this, params));
// Clear stencil only once!
params.clearStencil = false;
}
// If there was no color clear, clear stencil alone.
if (params.clearStencil)
{
params.clearColor = false;
ANGLE_TRY(renderer->getUtils().clearFramebuffer(contextVk, this, params));
}
return angle::Result::Continue;
}
angle::Result FramebufferVk::getSamplePosition(const gl::Context *context,
size_t index,
GLfloat *xy) const
{
ANGLE_VK_UNREACHABLE(vk::GetImpl(context));
return angle::Result::Stop;
}
angle::Result FramebufferVk::startNewRenderPass(ContextVk *contextVk,
const gl::Rectangle &renderArea,
vk::CommandBuffer **commandBufferOut)
{
vk::Framebuffer *framebuffer = nullptr;
ANGLE_TRY(getFramebuffer(contextVk, &framebuffer));
vk::AttachmentOpsArray renderPassAttachmentOps;
std::vector<VkClearValue> attachmentClearValues;
vk::CommandBuffer *writeCommands = nullptr;
ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &writeCommands));
// Initialize RenderPass info.
const auto &colorRenderTargets = mRenderTargetCache.getColors();
for (size_t colorIndexGL : mState.getEnabledDrawBuffers())
{
RenderTargetVk *colorRenderTarget = colorRenderTargets[colorIndexGL];
ASSERT(colorRenderTarget);
ANGLE_TRY(colorRenderTarget->onColorDraw(contextVk, &mFramebuffer, writeCommands));
renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL);
attachmentClearValues.emplace_back(kUninitializedClearValue);
}
RenderTargetVk *depthStencilRenderTarget = mRenderTargetCache.getDepthStencil();
if (depthStencilRenderTarget)
{
ANGLE_TRY(
depthStencilRenderTarget->onDepthStencilDraw(contextVk, &mFramebuffer, writeCommands));
renderPassAttachmentOps.initWithLoadStore(attachmentClearValues.size(),
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL,
VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL);
attachmentClearValues.emplace_back(kUninitializedClearValue);
}
return mFramebuffer.beginRenderPass(contextVk, *framebuffer, renderArea, mRenderPassDesc,
renderPassAttachmentOps, attachmentClearValues,
commandBufferOut);
}
void FramebufferVk::updateActiveColorMasks(size_t colorIndexGL, bool r, bool g, bool b, bool a)
{
mActiveColorComponentMasksForClear[0].set(colorIndexGL, r);
mActiveColorComponentMasksForClear[1].set(colorIndexGL, g);
mActiveColorComponentMasksForClear[2].set(colorIndexGL, b);
mActiveColorComponentMasksForClear[3].set(colorIndexGL, a);
}
const gl::DrawBufferMask &FramebufferVk::getEmulatedAlphaAttachmentMask() const
{
return mEmulatedAlphaAttachmentMask;
}
angle::Result FramebufferVk::readPixelsImpl(ContextVk *contextVk,
const gl::Rectangle &area,
const PackPixelsParams &packPixelsParams,
VkImageAspectFlagBits copyAspectFlags,
RenderTargetVk *renderTarget,
void *pixels)
{
TRACE_EVENT0("gpu.angle", "FramebufferVk::readPixelsImpl");
RendererVk *renderer = contextVk->getRenderer();
ANGLE_TRY(renderTarget->ensureImageInitialized(contextVk));
vk::CommandBuffer *commandBuffer = nullptr;
ANGLE_TRY(mFramebuffer.recordCommands(contextVk, &commandBuffer));
// Note that although we're reading from the image, we need to update the layout below.
vk::ImageHelper *srcImage =
renderTarget->getImageForRead(&mFramebuffer, vk::ImageLayout::TransferSrc, commandBuffer);
const angle::Format *readFormat = &srcImage->getFormat().imageFormat();
if (copyAspectFlags != VK_IMAGE_ASPECT_COLOR_BIT)
{
readFormat = &GetDepthStencilImageToBufferFormat(*readFormat, copyAspectFlags);
}
size_t level = renderTarget->getLevelIndex();
size_t layer = renderTarget->getLayerIndex();
VkOffset3D srcOffset = {area.x, area.y, 0};
VkExtent3D srcExtent = {static_cast<uint32_t>(area.width), static_cast<uint32_t>(area.height),
1};
// If the source image is multisampled, we need to resolve it into a temporary image before
// performing a readback.
bool isMultisampled = srcImage->getSamples() > 1;
vk::Scoped<vk::ImageHelper> resolvedImage(contextVk->getDevice());
if (isMultisampled)
{
ANGLE_TRY(resolvedImage.get().init2DStaging(
contextVk, renderer->getMemoryProperties(), gl::Extents(area.width, area.height, 1),
srcImage->getFormat(),
VK_IMAGE_USAGE_TRANSFER_SRC_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT, 1));
resolvedImage.get().updateQueueSerial(renderer->getCurrentQueueSerial());
// TODO(syoussefi): resolve only works on color images (not depth/stencil). If readback
// on multisampled depth/stencil image is done, we would need a different path. One
// possible solution would be a compute shader that directly reads from the multisampled
// image, performs the resolve and outputs to the buffer in one go.
// http://anglebug.com/3200
ASSERT(copyAspectFlags == VK_IMAGE_ASPECT_COLOR_BIT);
VkImageResolve resolveRegion = {};
resolveRegion.srcSubresource.aspectMask = copyAspectFlags;
resolveRegion.srcSubresource.mipLevel = level;
resolveRegion.srcSubresource.baseArrayLayer = layer;
resolveRegion.srcSubresource.layerCount = 1;
resolveRegion.srcOffset = srcOffset;
resolveRegion.dstSubresource.aspectMask = copyAspectFlags;
resolveRegion.dstSubresource.mipLevel = 0;
resolveRegion.dstSubresource.baseArrayLayer = 0;
resolveRegion.dstSubresource.layerCount = 1;
resolveRegion.dstOffset = {};
resolveRegion.extent = srcExtent;
srcImage->resolve(&resolvedImage.get(), resolveRegion, commandBuffer);
resolvedImage.get().changeLayout(copyAspectFlags, vk::ImageLayout::TransferSrc,
commandBuffer);
// Make the resolved image the target of buffer copy.
srcImage = &resolvedImage.get();
level = 0;
layer = 0;
srcOffset = {0, 0, 0};
}
VkBuffer bufferHandle = VK_NULL_HANDLE;
uint8_t *readPixelBuffer = nullptr;
VkDeviceSize stagingOffset = 0;
size_t allocationSize = readFormat->pixelBytes * area.width * area.height;
ANGLE_TRY(mReadPixelBuffer.allocate(contextVk, allocationSize, &readPixelBuffer, &bufferHandle,
&stagingOffset, nullptr));
VkBufferImageCopy region = {};
region.bufferImageHeight = srcExtent.height;
region.bufferOffset = stagingOffset;
region.bufferRowLength = srcExtent.width;
region.imageExtent = srcExtent;
region.imageOffset = srcOffset;
region.imageSubresource.aspectMask = copyAspectFlags;
region.imageSubresource.baseArrayLayer = layer;
region.imageSubresource.layerCount = 1;
region.imageSubresource.mipLevel = level;
commandBuffer->copyImageToBuffer(srcImage->getImage(), srcImage->getCurrentLayout(),
bufferHandle, 1, ®ion);
// Triggers a full finish.
// TODO(jmadill): Don't block on asynchronous readback.
ANGLE_TRY(contextVk->finishImpl());
// The buffer we copied to needs to be invalidated before we read from it because its not been
// created with the host coherent bit.
ANGLE_TRY(mReadPixelBuffer.invalidate(contextVk));
PackPixels(packPixelsParams, *readFormat, area.width * readFormat->pixelBytes, readPixelBuffer,
static_cast<uint8_t *>(pixels));
return angle::Result::Continue;
}
gl::Extents FramebufferVk::getReadImageExtents() const
{
ASSERT(getColorReadRenderTarget()->getExtents().width == mState.getDimensions().width);
ASSERT(getColorReadRenderTarget()->getExtents().height == mState.getDimensions().height);
return getColorReadRenderTarget()->getExtents();
}
gl::Rectangle FramebufferVk::getCompleteRenderArea() const
{
return gl::Rectangle(0, 0, mState.getDimensions().width, mState.getDimensions().height);
}
gl::Rectangle FramebufferVk::getScissoredRenderArea(ContextVk *contextVk) const
{
const gl::Rectangle renderArea(0, 0, mState.getDimensions().width,
mState.getDimensions().height);
bool invertViewport = contextVk->isViewportFlipEnabledForDrawFBO();
return ClipRectToScissor(contextVk->getState(), renderArea, invertViewport);
}
void FramebufferVk::onScissorChange(ContextVk *contextVk)
{
gl::Rectangle scissoredRenderArea = getScissoredRenderArea(contextVk);
// If the scissor has grown beyond the previous scissoredRenderArea, make sure the render pass
// is restarted. Otherwise, we can continue using the same renderpass area.
//
// Without a scissor, the render pass area covers the whole of the framebuffer. With a
// scissored clear, the render pass area could be smaller than the framebuffer size. When the
// scissor changes, if the scissor area is completely encompassed by the render pass area, it's
// possible to continue using the same render pass. However, if the current render pass area
// is too small, we need to start a new one. The latter can happen if a scissored clear starts
// a render pass, the scissor is disabled and a draw call is issued to affect the whole
// framebuffer.
mFramebuffer.updateQueueSerial(contextVk->getRenderer()->getCurrentQueueSerial());
if (mFramebuffer.hasStartedRenderPass() &&
!mFramebuffer.getRenderPassRenderArea().encloses(scissoredRenderArea))
{
mFramebuffer.finishCurrentCommands(contextVk->getRenderer());
}
}
RenderTargetVk *FramebufferVk::getFirstRenderTarget() const
{
for (auto *renderTarget : mRenderTargetCache.getColors())
{
if (renderTarget)
{
return renderTarget;
}
}
return mRenderTargetCache.getDepthStencil();
}
GLint FramebufferVk::getSamples() const
{
RenderTargetVk *firstRT = getFirstRenderTarget();
return firstRT ? firstRT->getImage().getSamples() : 0;
}
} // namespace rx