Hash :
70d1ef67
Author :
Date :
2024-11-20T11:34:39
Vulkan: Ensure onFramebufferBoundary is called for offscreen There is peak memory regression observed from crrev.com/c/6022549. What I suspect happening is that for offscreen or single buffered case, glFlush/glFinish is called but bail out because it already submitted or deferred. So we end up not calling onFramebufferBoundary(). This CL ensures we always call onFramebufferBoundary from these two functions for single buffer or offscreen. Also fixed a bug when onSharedPresentContextFlush is called we may end up calling onFramebufferBoundary. To make API names consistent, existing flushImpl() is renamed to flushAndSubmitCommands() and a new flushIMpl is added to wrap around most logic inside flush(). Bug: angleproject:372268711 Change-Id: I54eed8a81f4153d52ab962f213cacc87a73b89ac Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6037491 Reviewed-by: Yuxin Hu <yuxinhu@google.com> Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: Charlie Lao <cclao@google.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 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 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
//
// 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.
//
// BufferVk.cpp:
// Implements the class methods for BufferVk.
//
#include "libANGLE/renderer/vulkan/BufferVk.h"
#include "common/FixedVector.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "common/utilities.h"
#include "libANGLE/Context.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/vk_renderer.h"
namespace rx
{
VkBufferUsageFlags GetDefaultBufferUsageFlags(vk::Renderer *renderer)
{
// We could potentially use multiple backing buffers for different usages.
// For now keep a single buffer with all relevant usage flags.
VkBufferUsageFlags defaultBufferUsageFlags =
VK_BUFFER_USAGE_VERTEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT |
VK_BUFFER_USAGE_INDEX_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
VK_BUFFER_USAGE_UNIFORM_TEXEL_BUFFER_BIT | VK_BUFFER_USAGE_STORAGE_TEXEL_BUFFER_BIT |
VK_BUFFER_USAGE_INDIRECT_BUFFER_BIT;
if (renderer->getFeatures().supportsTransformFeedbackExtension.enabled)
{
defaultBufferUsageFlags |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT |
VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_COUNTER_BUFFER_BIT_EXT;
}
return defaultBufferUsageFlags;
}
namespace
{
constexpr VkMemoryPropertyFlags kDeviceLocalFlags = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT;
constexpr VkMemoryPropertyFlags kDeviceLocalHostCoherentFlags =
(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
constexpr VkMemoryPropertyFlags kHostCachedFlags =
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
constexpr VkMemoryPropertyFlags kHostUncachedFlags =
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
constexpr VkMemoryPropertyFlags kHostCachedNonCoherentFlags =
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_CACHED_BIT);
// Vertex attribute buffers are used as storage buffers for conversion in compute, where access to
// the buffer is made in 4-byte chunks. Assume the size of the buffer is 4k+n where n is in [0, 3).
// On some hardware, reading 4 bytes from address 4k returns 0, making it impossible to read the
// last n bytes. By rounding up the buffer sizes to a multiple of 4, the problem is alleviated.
constexpr size_t kBufferSizeGranularity = 4;
static_assert(gl::isPow2(kBufferSizeGranularity), "use as alignment, must be power of two");
// Start with a fairly small buffer size. We can increase this dynamically as we convert more data.
constexpr size_t kConvertedArrayBufferInitialSize = 1024 * 8;
// Buffers that have a static usage pattern will be allocated in
// device local memory to speed up access to and from the GPU.
// Dynamic usage patterns or that are frequently mapped
// will now request host cached memory to speed up access from the CPU.
VkMemoryPropertyFlags GetPreferredMemoryType(vk::Renderer *renderer,
gl::BufferBinding target,
gl::BufferUsage usage)
{
if (target == gl::BufferBinding::PixelUnpack)
{
return kHostCachedFlags;
}
switch (usage)
{
case gl::BufferUsage::StaticCopy:
case gl::BufferUsage::StaticDraw:
case gl::BufferUsage::StaticRead:
// For static usage, request a device local memory
return renderer->getFeatures().preferDeviceLocalMemoryHostVisible.enabled
? kDeviceLocalHostCoherentFlags
: kDeviceLocalFlags;
case gl::BufferUsage::DynamicDraw:
case gl::BufferUsage::StreamDraw:
// For non-static usage where the CPU performs a write-only access, request
// a host uncached memory
return renderer->getFeatures().preferHostCachedForNonStaticBufferUsage.enabled
? kHostCachedFlags
: kHostUncachedFlags;
case gl::BufferUsage::DynamicCopy:
case gl::BufferUsage::DynamicRead:
case gl::BufferUsage::StreamCopy:
case gl::BufferUsage::StreamRead:
// For all other types of usage, request a host cached memory
return renderer->getFeatures()
.preferCachedNoncoherentForDynamicStreamBufferUsage.enabled
? kHostCachedNonCoherentFlags
: kHostCachedFlags;
default:
UNREACHABLE();
return kHostCachedFlags;
}
}
VkMemoryPropertyFlags GetStorageMemoryType(vk::Renderer *renderer,
GLbitfield storageFlags,
bool externalBuffer)
{
const bool hasMapAccess =
(storageFlags & (GL_MAP_READ_BIT | GL_MAP_WRITE_BIT | GL_MAP_PERSISTENT_BIT_EXT)) != 0;
if (renderer->getFeatures().preferDeviceLocalMemoryHostVisible.enabled)
{
const bool canUpdate = (storageFlags & GL_DYNAMIC_STORAGE_BIT_EXT) != 0;
if (canUpdate || hasMapAccess || externalBuffer)
{
// We currently allocate coherent memory for persistently mapped buffers.
// GL_EXT_buffer_storage allows non-coherent memory, but currently the implementation of
// |glMemoryBarrier(CLIENT_MAPPED_BUFFER_BARRIER_BIT_EXT)| relies on the mapping being
// coherent.
//
// If persistently mapped buffers ever use non-coherent memory, then said
// |glMemoryBarrier| call must result in |vkInvalidateMappedMemoryRanges| for all
// persistently mapped buffers.
return kDeviceLocalHostCoherentFlags;
}
return kDeviceLocalFlags;
}
return hasMapAccess ? kHostCachedFlags : kDeviceLocalFlags;
}
bool ShouldAllocateNewMemoryForUpdate(ContextVk *contextVk, size_t subDataSize, size_t bufferSize)
{
// A sub-data update with size > 50% of buffer size meets the threshold to acquire a new
// BufferHelper from the pool.
size_t halfBufferSize = bufferSize / 2;
if (subDataSize > halfBufferSize)
{
return true;
}
// If the GPU is busy, it is possible to use the CPU for updating sub-data instead, but since it
// would need to create a duplicate of the buffer, a large enough buffer copy could result in a
// performance regression.
if (contextVk->getFeatures().preferCPUForBufferSubData.enabled)
{
// If the buffer is small enough, the cost of barrier associated with the GPU copy likely
// exceeds the overhead with the CPU copy. Duplicating the buffer allows the CPU to write to
// the buffer immediately, thus avoiding the barrier that prevents parallel operation.
constexpr size_t kCpuCopyBufferSizeThreshold = 32 * 1024;
if (bufferSize < kCpuCopyBufferSizeThreshold)
{
return true;
}
// To use CPU for the sub-data update in larger buffers, the update should be sizable enough
// compared to the whole buffer size. The threshold is chosen based on perf data collected
// from Pixel devices. At 1/8 of buffer size, the CPU overhead associated with extra data
// copy weighs less than serialization caused by barriers.
size_t subDataThreshold = bufferSize / 8;
if (subDataSize > subDataThreshold)
{
return true;
}
}
return false;
}
bool ShouldUseCPUToCopyData(ContextVk *contextVk,
const vk::BufferHelper &buffer,
size_t copySize,
size_t bufferSize)
{
vk::Renderer *renderer = contextVk->getRenderer();
// If the buffer is not host-visible, or if it's busy on the GPU, can't read from it from the
// CPU
if (!buffer.isHostVisible() || !renderer->hasResourceUseFinished(buffer.getWriteResourceUse()))
{
return false;
}
// For some GPUs (e.g. ARM) we always prefer using CPU to do copy instead of using the GPU to
// avoid pipeline bubbles. If the GPU is currently busy and data copy size is less than certain
// threshold, we choose to use CPU to do the copy over GPU to achieve better parallelism.
return renderer->getFeatures().preferCPUForBufferSubData.enabled ||
(renderer->isCommandQueueBusy() &&
copySize < renderer->getMaxCopyBytesUsingCPUWhenPreservingBufferData());
}
bool RenderPassUsesBufferForReadOnly(ContextVk *contextVk, const vk::BufferHelper &buffer)
{
if (!contextVk->hasActiveRenderPass())
{
return false;
}
vk::RenderPassCommandBufferHelper &renderPassCommands =
contextVk->getStartedRenderPassCommands();
return renderPassCommands.usesBuffer(buffer) && !renderPassCommands.usesBufferForWrite(buffer);
}
// If a render pass is open which uses the buffer in read-only mode, render pass break can be
// avoided by using acquireAndUpdate. This can be costly however if the update is very small, and
// is limited to platforms where render pass break is itself costly (i.e. tiled-based renderers).
bool ShouldAvoidRenderPassBreakOnUpdate(ContextVk *contextVk,
const vk::BufferHelper &buffer,
size_t bufferSize)
{
// Only avoid breaking the render pass if the buffer is not so big such that duplicating it
// would outweight the cost of breaking the render pass. A value of 1KB is temporary chosen as
// a heuristic, and can be adjusted when such a situation is encountered.
constexpr size_t kPreferDuplicateOverRenderPassBreakMaxBufferSize = 1024;
if (!contextVk->getFeatures().preferCPUForBufferSubData.enabled ||
bufferSize > kPreferDuplicateOverRenderPassBreakMaxBufferSize)
{
return false;
}
return RenderPassUsesBufferForReadOnly(contextVk, buffer);
}
BufferUsageType GetBufferUsageType(gl::BufferUsage usage)
{
return (usage == gl::BufferUsage::DynamicDraw || usage == gl::BufferUsage::DynamicCopy ||
usage == gl::BufferUsage::DynamicRead)
? BufferUsageType::Dynamic
: BufferUsageType::Static;
}
angle::Result GetMemoryTypeIndex(ContextVk *contextVk,
VkDeviceSize size,
VkMemoryPropertyFlags memoryPropertyFlags,
uint32_t *memoryTypeIndexOut)
{
vk::Renderer *renderer = contextVk->getRenderer();
const vk::Allocator &allocator = renderer->getAllocator();
bool persistentlyMapped = renderer->getFeatures().persistentlyMappedBuffers.enabled;
VkBufferUsageFlags defaultBufferUsageFlags = GetDefaultBufferUsageFlags(renderer);
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.flags = 0;
createInfo.size = size;
createInfo.usage = defaultBufferUsageFlags;
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
// Host visible is required, all other bits are preferred, (i.e., optional)
VkMemoryPropertyFlags requiredFlags =
(memoryPropertyFlags & VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
VkMemoryPropertyFlags preferredFlags =
(memoryPropertyFlags & (~VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT));
// Check that the allocation is not too large.
uint32_t memoryTypeIndex = 0;
ANGLE_VK_TRY(contextVk, allocator.findMemoryTypeIndexForBufferInfo(
createInfo, requiredFlags, preferredFlags, persistentlyMapped,
&memoryTypeIndex));
*memoryTypeIndexOut = memoryTypeIndex;
return angle::Result::Continue;
}
bool IsSelfCopy(const BufferDataSource &dataSource, const vk::BufferHelper &destination)
{
return dataSource.data == nullptr &&
dataSource.buffer->getBufferSerial() == destination.getBufferSerial();
}
angle::Result CopyBuffers(ContextVk *contextVk,
vk::BufferHelper *srcBuffer,
vk::BufferHelper *dstBuffer,
uint32_t regionCount,
const VkBufferCopy *copyRegions)
{
ASSERT(srcBuffer->valid() && dstBuffer->valid());
// Enqueue a copy command on the GPU
vk::CommandBufferAccess access;
if (srcBuffer->getBufferSerial() == dstBuffer->getBufferSerial())
{
access.onBufferSelfCopy(srcBuffer);
}
else
{
access.onBufferTransferRead(srcBuffer);
access.onBufferTransferWrite(dstBuffer);
}
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
commandBuffer->copyBuffer(srcBuffer->getBuffer(), dstBuffer->getBuffer(), regionCount,
copyRegions);
return angle::Result::Continue;
}
} // namespace
// ConversionBuffer implementation.
ConversionBuffer::ConversionBuffer(vk::Renderer *renderer,
VkBufferUsageFlags usageFlags,
size_t initialSize,
size_t alignment,
bool hostVisible)
: mEntireBufferDirty(true)
{
mData = std::make_unique<vk::BufferHelper>();
mDirtyRanges.reserve(32);
}
ConversionBuffer::~ConversionBuffer()
{
ASSERT(!mData || !mData->valid());
mDirtyRanges.clear();
}
ConversionBuffer::ConversionBuffer(ConversionBuffer &&other) = default;
// dirtyRanges may be overlap or continuous. In order to reduce the redunant conversion, we try to
// consolidate the dirty ranges. First we sort it by the range's low. Then we walk the range again
// and check it with previous range and merge them if possible. That merge will remove the
// overlapped area as well as reduce the number of ranges.
void ConversionBuffer::consolidateDirtyRanges()
{
ASSERT(!mEntireBufferDirty);
auto comp = [](const RangeDeviceSize &a, const RangeDeviceSize &b) -> bool {
return a.low() < b.low();
};
std::sort(mDirtyRanges.begin(), mDirtyRanges.end(), comp);
size_t prev = 0;
for (size_t i = 1; i < mDirtyRanges.size(); i++)
{
if (mDirtyRanges[prev].intersectsOrContinuous(mDirtyRanges[i]))
{
mDirtyRanges[prev].merge(mDirtyRanges[i]);
mDirtyRanges[i].invalidate();
}
else
{
prev = i;
}
}
}
// VertexConversionBuffer implementation.
VertexConversionBuffer::VertexConversionBuffer(vk::Renderer *renderer, const CacheKey &cacheKey)
: ConversionBuffer(renderer,
vk::kVertexBufferUsageFlags,
kConvertedArrayBufferInitialSize,
vk::kVertexBufferAlignment,
cacheKey.hostVisible),
mCacheKey(cacheKey)
{}
VertexConversionBuffer::VertexConversionBuffer(VertexConversionBuffer &&other) = default;
VertexConversionBuffer::~VertexConversionBuffer() = default;
// BufferVk implementation.
BufferVk::BufferVk(const gl::BufferState &state)
: BufferImpl(state),
mClientBuffer(nullptr),
mMemoryTypeIndex(0),
mMemoryPropertyFlags(0),
mIsStagingBufferMapped(false),
mHasValidData(false),
mIsMappedForWrite(false),
mUsageType(BufferUsageType::Static)
{
mMappedRange.invalidate();
}
BufferVk::~BufferVk() {}
void BufferVk::destroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
(void)release(contextVk);
}
void BufferVk::releaseConversionBuffers(vk::Renderer *renderer)
{
for (ConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.release(renderer);
}
mVertexConversionBuffers.clear();
}
angle::Result BufferVk::release(ContextVk *contextVk)
{
vk::Renderer *renderer = contextVk->getRenderer();
if (mBuffer.valid())
{
ANGLE_TRY(contextVk->releaseBufferAllocation(&mBuffer));
}
if (mStagingBuffer.valid())
{
mStagingBuffer.release(renderer);
}
releaseConversionBuffers(renderer);
return angle::Result::Continue;
}
angle::Result BufferVk::setExternalBufferData(const gl::Context *context,
gl::BufferBinding target,
GLeglClientBufferEXT clientBuffer,
size_t size,
VkMemoryPropertyFlags memoryPropertyFlags)
{
ContextVk *contextVk = vk::GetImpl(context);
// Release and re-create the memory and buffer.
ANGLE_TRY(release(contextVk));
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.flags = 0;
createInfo.size = size;
createInfo.usage = GetDefaultBufferUsageFlags(contextVk->getRenderer());
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
return mBuffer.initExternal(contextVk, memoryPropertyFlags, createInfo, clientBuffer);
}
angle::Result BufferVk::setDataWithUsageFlags(const gl::Context *context,
gl::BufferBinding target,
GLeglClientBufferEXT clientBuffer,
const void *data,
size_t size,
gl::BufferUsage usage,
GLbitfield flags)
{
ContextVk *contextVk = vk::GetImpl(context);
VkMemoryPropertyFlags memoryPropertyFlags = 0;
bool persistentMapRequired = false;
const bool isExternalBuffer = clientBuffer != nullptr;
switch (usage)
{
case gl::BufferUsage::InvalidEnum:
{
// glBufferStorage API call
memoryPropertyFlags =
GetStorageMemoryType(contextVk->getRenderer(), flags, isExternalBuffer);
persistentMapRequired = (flags & GL_MAP_PERSISTENT_BIT_EXT) != 0;
break;
}
default:
{
// glBufferData API call
memoryPropertyFlags = GetPreferredMemoryType(contextVk->getRenderer(), target, usage);
break;
}
}
if (isExternalBuffer)
{
ANGLE_TRY(setExternalBufferData(context, target, clientBuffer, size, memoryPropertyFlags));
if (!mBuffer.isHostVisible())
{
// If external buffer's memory does not support host visible memory property, we cannot
// support a persistent map request.
ANGLE_VK_CHECK(contextVk, !persistentMapRequired, VK_ERROR_MEMORY_MAP_FAILED);
}
mClientBuffer = clientBuffer;
return angle::Result::Continue;
}
return setDataWithMemoryType(context, target, data, size, memoryPropertyFlags, usage);
}
angle::Result BufferVk::setData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
gl::BufferUsage usage)
{
ContextVk *contextVk = vk::GetImpl(context);
// Assume host visible/coherent memory available.
VkMemoryPropertyFlags memoryPropertyFlags =
GetPreferredMemoryType(contextVk->getRenderer(), target, usage);
return setDataWithMemoryType(context, target, data, size, memoryPropertyFlags, usage);
}
angle::Result BufferVk::setDataWithMemoryType(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
VkMemoryPropertyFlags memoryPropertyFlags,
gl::BufferUsage usage)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
// Since the buffer is being entirely reinitialized, reset the valid-data flag. If the caller
// passed in data to fill the buffer, the flag will be updated when the data is copied to the
// buffer.
mHasValidData = false;
if (size == 0)
{
// Nothing to do.
return angle::Result::Continue;
}
if (!mVertexConversionBuffers.empty())
{
for (ConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.clearDirty();
}
}
const BufferUsageType usageType = GetBufferUsageType(usage);
const BufferUpdateType updateType =
calculateBufferUpdateTypeOnFullUpdate(renderer, size, memoryPropertyFlags, usageType, data);
if (updateType == BufferUpdateType::StorageRedefined)
{
mUsageType = usageType;
mMemoryPropertyFlags = memoryPropertyFlags;
ANGLE_TRY(GetMemoryTypeIndex(contextVk, size, memoryPropertyFlags, &mMemoryTypeIndex));
ANGLE_TRY(acquireBufferHelper(contextVk, size, mUsageType));
}
else if (size != static_cast<size_t>(mState.getSize()))
{
if (mBuffer.onBufferUserSizeChange(renderer))
{
// If we have a dedicated VkBuffer created with user size, even if the storage is
// reused, we have to recreate that VkBuffer with user size when user size changes.
// When this happens, we must notify other objects that observing this buffer, such as
// vertex array. The reason vertex array is observing the buffer's storage change is
// because they uses VkBuffer. Now VkBuffer have changed, vertex array needs to
// re-process it just like storage has been reallocated.
onStateChange(angle::SubjectMessage::InternalMemoryAllocationChanged);
}
}
if (data != nullptr)
{
BufferDataSource dataSource = {};
dataSource.data = data;
// Handle full-buffer updates similarly to glBufferSubData
ANGLE_TRY(setDataImpl(contextVk, size, dataSource, size, 0, updateType));
}
return angle::Result::Continue;
}
angle::Result BufferVk::setSubData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
size_t offset)
{
ASSERT(mBuffer.valid());
BufferDataSource dataSource = {};
dataSource.data = data;
ContextVk *contextVk = vk::GetImpl(context);
return setDataImpl(contextVk, static_cast<size_t>(mState.getSize()), dataSource, size, offset,
BufferUpdateType::ContentsUpdate);
}
angle::Result BufferVk::copySubData(const gl::Context *context,
BufferImpl *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
ASSERT(mBuffer.valid());
ContextVk *contextVk = vk::GetImpl(context);
BufferVk *sourceVk = GetAs<BufferVk>(source);
BufferDataSource dataSource = {};
dataSource.buffer = &sourceVk->getBuffer();
dataSource.bufferOffset = static_cast<VkDeviceSize>(sourceOffset);
ASSERT(dataSource.buffer->valid());
return setDataImpl(contextVk, static_cast<size_t>(mState.getSize()), dataSource, size,
destOffset, BufferUpdateType::ContentsUpdate);
}
angle::Result BufferVk::allocStagingBuffer(ContextVk *contextVk,
vk::MemoryCoherency coherency,
VkDeviceSize size,
uint8_t **mapPtr)
{
ASSERT(!mIsStagingBufferMapped);
if (mStagingBuffer.valid())
{
if (size <= mStagingBuffer.getSize() && IsCached(coherency) == mStagingBuffer.isCached() &&
contextVk->getRenderer()->hasResourceUseFinished(mStagingBuffer.getResourceUse()))
{
// If size is big enough and it is idle, then just reuse the existing staging buffer
*mapPtr = mStagingBuffer.getMappedMemory();
mIsStagingBufferMapped = true;
return angle::Result::Continue;
}
mStagingBuffer.release(contextVk->getRenderer());
}
ANGLE_TRY(
contextVk->initBufferForBufferCopy(&mStagingBuffer, static_cast<size_t>(size), coherency));
*mapPtr = mStagingBuffer.getMappedMemory();
mIsStagingBufferMapped = true;
return angle::Result::Continue;
}
angle::Result BufferVk::flushStagingBuffer(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize size)
{
vk::Renderer *renderer = contextVk->getRenderer();
ASSERT(mIsStagingBufferMapped);
ASSERT(mStagingBuffer.valid());
if (!mStagingBuffer.isCoherent())
{
ANGLE_TRY(mStagingBuffer.flush(renderer));
}
VkBufferCopy copyRegion = {mStagingBuffer.getOffset(), mBuffer.getOffset() + offset, size};
ANGLE_TRY(CopyBuffers(contextVk, &mStagingBuffer, &mBuffer, 1, ©Region));
return angle::Result::Continue;
}
angle::Result BufferVk::handleDeviceLocalBufferMap(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize size,
uint8_t **mapPtr)
{
vk::Renderer *renderer = contextVk->getRenderer();
ANGLE_TRY(
allocStagingBuffer(contextVk, vk::MemoryCoherency::CachedPreferCoherent, size, mapPtr));
ANGLE_TRY(mStagingBuffer.flush(renderer));
// Copy data from device local buffer to host visible staging buffer.
VkBufferCopy copyRegion = {mBuffer.getOffset() + offset, mStagingBuffer.getOffset(), size};
ANGLE_TRY(CopyBuffers(contextVk, &mBuffer, &mStagingBuffer, 1, ©Region));
ANGLE_TRY(mStagingBuffer.waitForIdle(contextVk, "GPU stall due to mapping device local buffer",
RenderPassClosureReason::DeviceLocalBufferMap));
// Since coherent is prefer, we may end up getting non-coherent. Always call invalidate here (it
// will check memory flag before it actually calls into driver).
ANGLE_TRY(mStagingBuffer.invalidate(renderer));
return angle::Result::Continue;
}
angle::Result BufferVk::mapHostVisibleBuffer(ContextVk *contextVk,
VkDeviceSize offset,
GLbitfield access,
uint8_t **mapPtr)
{
ANGLE_TRY(mBuffer.mapWithOffset(contextVk, mapPtr, static_cast<size_t>(offset)));
// Invalidate non-coherent for READ case.
if (!mBuffer.isCoherent() && (access & GL_MAP_READ_BIT) != 0)
{
ANGLE_TRY(mBuffer.invalidate(contextVk->getRenderer()));
}
return angle::Result::Continue;
}
angle::Result BufferVk::map(const gl::Context *context, GLenum access, void **mapPtr)
{
ASSERT(mBuffer.valid());
ASSERT(access == GL_WRITE_ONLY_OES);
return mapImpl(vk::GetImpl(context), GL_MAP_WRITE_BIT, mapPtr);
}
angle::Result BufferVk::mapRange(const gl::Context *context,
size_t offset,
size_t length,
GLbitfield access,
void **mapPtr)
{
return mapRangeImpl(vk::GetImpl(context), offset, length, access, mapPtr);
}
angle::Result BufferVk::mapImpl(ContextVk *contextVk, GLbitfield access, void **mapPtr)
{
return mapRangeImpl(contextVk, 0, static_cast<VkDeviceSize>(mState.getSize()), access, mapPtr);
}
angle::Result BufferVk::ghostMappedBuffer(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize length,
GLbitfield access,
void **mapPtr)
{
// We shouldn't get here if it is external memory
ASSERT(!isExternalBuffer());
++contextVk->getPerfCounters().buffersGhosted;
// If we are creating a new buffer because the GPU is using it as read-only, then we
// also need to copy the contents of the previous buffer into the new buffer, in
// case the caller only updates a portion of the new buffer.
vk::BufferHelper src = std::move(mBuffer);
ANGLE_TRY(acquireBufferHelper(contextVk, static_cast<size_t>(mState.getSize()),
BufferUsageType::Dynamic));
// Before returning the new buffer, map the previous buffer and copy its entire
// contents into the new buffer.
uint8_t *srcMapPtr = nullptr;
uint8_t *dstMapPtr = nullptr;
ANGLE_TRY(src.map(contextVk, &srcMapPtr));
ANGLE_TRY(mBuffer.map(contextVk, &dstMapPtr));
ASSERT(src.isCoherent());
ASSERT(mBuffer.isCoherent());
// No need to copy over [offset, offset + length), just around it
if ((access & GL_MAP_INVALIDATE_RANGE_BIT) != 0)
{
if (offset != 0)
{
memcpy(dstMapPtr, srcMapPtr, static_cast<size_t>(offset));
}
size_t totalSize = static_cast<size_t>(mState.getSize());
size_t remainingStart = static_cast<size_t>(offset + length);
size_t remainingSize = totalSize - remainingStart;
if (remainingSize != 0)
{
memcpy(dstMapPtr + remainingStart, srcMapPtr + remainingStart, remainingSize);
}
}
else
{
memcpy(dstMapPtr, srcMapPtr, static_cast<size_t>(mState.getSize()));
}
ANGLE_TRY(contextVk->releaseBufferAllocation(&src));
// Return the already mapped pointer with the offset adjustment to avoid the call to unmap().
*mapPtr = dstMapPtr + offset;
return angle::Result::Continue;
}
angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize length,
GLbitfield access,
void **mapPtr)
{
vk::Renderer *renderer = contextVk->getRenderer();
ASSERT(mBuffer.valid());
// Record map call parameters in case this call is from angle internal (the access/offset/length
// will be inconsistent from mState).
mIsMappedForWrite = (access & GL_MAP_WRITE_BIT) != 0;
mMappedRange = RangeDeviceSize(offset, offset + length);
uint8_t **mapPtrBytes = reinterpret_cast<uint8_t **>(mapPtr);
bool hostVisible = mBuffer.isHostVisible();
// MAP_UNSYNCHRONIZED_BIT, so immediately map.
if ((access & GL_MAP_UNSYNCHRONIZED_BIT) != 0)
{
if (hostVisible)
{
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
return handleDeviceLocalBufferMap(contextVk, offset, length, mapPtrBytes);
}
// Read case
if ((access & GL_MAP_WRITE_BIT) == 0)
{
// If app is not going to write, all we need is to ensure GPU write is finished.
// Concurrent reads from CPU and GPU is allowed.
if (!renderer->hasResourceUseFinished(mBuffer.getWriteResourceUse()))
{
// If there are unflushed write commands for the resource, flush them.
if (contextVk->hasUnsubmittedUse(mBuffer.getWriteResourceUse()))
{
ANGLE_TRY(contextVk->flushAndSubmitCommands(
nullptr, nullptr, RenderPassClosureReason::BufferWriteThenMap));
}
ANGLE_TRY(renderer->finishResourceUse(contextVk, mBuffer.getWriteResourceUse()));
}
if (hostVisible)
{
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
return handleDeviceLocalBufferMap(contextVk, offset, length, mapPtrBytes);
}
// Write case
if (!hostVisible)
{
return handleDeviceLocalBufferMap(contextVk, offset, length, mapPtrBytes);
}
// Write case, buffer not in use.
if (isExternalBuffer() || !isCurrentlyInUse(contextVk->getRenderer()))
{
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
// Write case, buffer in use.
//
// Here, we try to map the buffer, but it's busy. Instead of waiting for the GPU to
// finish, we just allocate a new buffer if:
// 1.) Caller has told us it doesn't care about previous contents, or
// 2.) The GPU won't write to the buffer.
bool rangeInvalidate = (access & GL_MAP_INVALIDATE_RANGE_BIT) != 0;
bool entireBufferInvalidated =
((access & GL_MAP_INVALIDATE_BUFFER_BIT) != 0) ||
(rangeInvalidate && offset == 0 && static_cast<VkDeviceSize>(mState.getSize()) == length);
if (entireBufferInvalidated)
{
ANGLE_TRY(acquireBufferHelper(contextVk, static_cast<size_t>(mState.getSize()),
BufferUsageType::Dynamic));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
bool smallMapRange = (length < static_cast<VkDeviceSize>(mState.getSize()) / 2);
if (smallMapRange && rangeInvalidate)
{
ANGLE_TRY(allocStagingBuffer(contextVk, vk::MemoryCoherency::CachedNonCoherent,
static_cast<size_t>(length), mapPtrBytes));
return angle::Result::Continue;
}
if (renderer->hasResourceUseFinished(mBuffer.getWriteResourceUse()))
{
// This will keep the new buffer mapped and update mapPtr, so return immediately.
return ghostMappedBuffer(contextVk, offset, length, access, mapPtr);
}
// Write case (worst case, buffer in use for write)
ANGLE_TRY(mBuffer.waitForIdle(contextVk, "GPU stall due to mapping buffer in use by the GPU",
RenderPassClosureReason::BufferInUseWhenSynchronizedMap));
return mapHostVisibleBuffer(contextVk, offset, access, mapPtrBytes);
}
angle::Result BufferVk::unmap(const gl::Context *context, GLboolean *result)
{
ANGLE_TRY(unmapImpl(vk::GetImpl(context)));
// This should be false if the contents have been corrupted through external means. Vulkan
// doesn't provide such information.
*result = true;
return angle::Result::Continue;
}
angle::Result BufferVk::unmapImpl(ContextVk *contextVk)
{
ASSERT(mBuffer.valid());
if (mIsStagingBufferMapped)
{
ASSERT(mStagingBuffer.valid());
// The buffer is device local or optimization of small range map.
if (mIsMappedForWrite)
{
ANGLE_TRY(flushStagingBuffer(contextVk, mMappedRange.low(), mMappedRange.length()));
}
mIsStagingBufferMapped = false;
}
else
{
ASSERT(mBuffer.isHostVisible());
vk::Renderer *renderer = contextVk->getRenderer();
if (!mBuffer.isCoherent())
{
ANGLE_TRY(mBuffer.flush(renderer));
}
mBuffer.unmap(renderer);
}
if (mIsMappedForWrite)
{
if (mMappedRange == RangeDeviceSize(0, static_cast<VkDeviceSize>(getSize())))
{
dataUpdated();
}
else
{
dataRangeUpdated(mMappedRange);
}
}
// Reset the mapping parameters
mIsMappedForWrite = false;
mMappedRange.invalidate();
return angle::Result::Continue;
}
angle::Result BufferVk::getSubData(const gl::Context *context,
GLintptr offset,
GLsizeiptr size,
void *outData)
{
ASSERT(offset + size <= getSize());
ASSERT(mBuffer.valid());
ContextVk *contextVk = vk::GetImpl(context);
void *mapPtr;
ANGLE_TRY(mapRangeImpl(contextVk, offset, size, GL_MAP_READ_BIT, &mapPtr));
memcpy(outData, mapPtr, size);
return unmapImpl(contextVk);
}
angle::Result BufferVk::getIndexRange(const gl::Context *context,
gl::DrawElementsType type,
size_t offset,
size_t count,
bool primitiveRestartEnabled,
gl::IndexRange *outRange)
{
ContextVk *contextVk = vk::GetImpl(context);
vk::Renderer *renderer = contextVk->getRenderer();
// This is a workaround for the mock ICD not implementing buffer memory state.
// Could be removed if https://github.com/KhronosGroup/Vulkan-Tools/issues/84 is fixed.
if (renderer->isMockICDEnabled())
{
outRange->start = 0;
outRange->end = 0;
return angle::Result::Continue;
}
ANGLE_TRACE_EVENT0("gpu.angle", "BufferVk::getIndexRange");
void *mapPtr;
ANGLE_TRY(mapRangeImpl(contextVk, offset, getSize(), GL_MAP_READ_BIT, &mapPtr));
*outRange = gl::ComputeIndexRange(type, mapPtr, count, primitiveRestartEnabled);
ANGLE_TRY(unmapImpl(contextVk));
return angle::Result::Continue;
}
angle::Result BufferVk::updateBuffer(ContextVk *contextVk,
size_t bufferSize,
const BufferDataSource &dataSource,
size_t updateSize,
size_t updateOffset)
{
// To copy on the CPU, destination must be host-visible. The source should also be either a CPU
// pointer or other a host-visible buffer that is not being written to by the GPU.
const bool shouldCopyOnCPU =
mBuffer.isHostVisible() &&
(dataSource.data != nullptr ||
ShouldUseCPUToCopyData(contextVk, *dataSource.buffer, updateSize, bufferSize));
if (shouldCopyOnCPU)
{
ANGLE_TRY(directUpdate(contextVk, dataSource, updateSize, updateOffset));
}
else
{
ANGLE_TRY(stagedUpdate(contextVk, dataSource, updateSize, updateOffset));
}
return angle::Result::Continue;
}
angle::Result BufferVk::directUpdate(ContextVk *contextVk,
const BufferDataSource &dataSource,
size_t size,
size_t offset)
{
vk::Renderer *renderer = contextVk->getRenderer();
uint8_t *srcPointerMapped = nullptr;
const uint8_t *srcPointer = nullptr;
uint8_t *dstPointer = nullptr;
// Map the destination buffer.
ASSERT(mBuffer.isHostVisible());
ANGLE_TRY(mBuffer.mapWithOffset(contextVk, &dstPointer, offset));
ASSERT(dstPointer);
// If source data is coming from a buffer, map it. If this is a self-copy, avoid double-mapping
// the buffer.
if (dataSource.data != nullptr)
{
srcPointer = static_cast<const uint8_t *>(dataSource.data);
}
else
{
ANGLE_TRY(dataSource.buffer->mapWithOffset(contextVk, &srcPointerMapped,
static_cast<size_t>(dataSource.bufferOffset)));
srcPointer = srcPointerMapped;
}
memcpy(dstPointer, srcPointer, size);
// External memory may end up with noncoherent
if (!mBuffer.isCoherent())
{
ANGLE_TRY(mBuffer.flush(renderer, offset, size));
}
// Unmap the destination and source buffers if applicable.
//
// If the buffer has dynamic usage then the intent is frequent client side updates to the
// buffer. Don't CPU unmap the buffer, we will take care of unmapping when releasing the buffer
// to either the renderer or mBufferFreeList.
if (GetBufferUsageType(mState.getUsage()) == BufferUsageType::Static)
{
mBuffer.unmap(renderer);
}
if (srcPointerMapped != nullptr)
{
dataSource.buffer->unmap(renderer);
}
return angle::Result::Continue;
}
angle::Result BufferVk::stagedUpdate(ContextVk *contextVk,
const BufferDataSource &dataSource,
size_t size,
size_t offset)
{
// If data is coming from a CPU pointer, stage it in a temporary staging buffer.
// Otherwise, do a GPU copy directly from the given buffer.
if (dataSource.data != nullptr)
{
uint8_t *mapPointer = nullptr;
ANGLE_TRY(allocStagingBuffer(contextVk, vk::MemoryCoherency::CachedNonCoherent, size,
&mapPointer));
memcpy(mapPointer, dataSource.data, size);
ANGLE_TRY(flushStagingBuffer(contextVk, offset, size));
mIsStagingBufferMapped = false;
}
else
{
// Check for self-dependency.
vk::CommandBufferAccess access;
if (dataSource.buffer->getBufferSerial() == mBuffer.getBufferSerial())
{
access.onBufferSelfCopy(&mBuffer);
}
else
{
access.onBufferTransferRead(dataSource.buffer);
access.onBufferTransferWrite(&mBuffer);
}
vk::OutsideRenderPassCommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
// Enqueue a copy command on the GPU.
const VkBufferCopy copyRegion = {dataSource.bufferOffset + dataSource.buffer->getOffset(),
static_cast<VkDeviceSize>(offset) + mBuffer.getOffset(),
static_cast<VkDeviceSize>(size)};
commandBuffer->copyBuffer(dataSource.buffer->getBuffer(), mBuffer.getBuffer(), 1,
©Region);
}
return angle::Result::Continue;
}
angle::Result BufferVk::acquireAndUpdate(ContextVk *contextVk,
size_t bufferSize,
const BufferDataSource &dataSource,
size_t updateSize,
size_t updateOffset,
BufferUpdateType updateType)
{
// We shouldn't get here if this is external memory
ASSERT(!isExternalBuffer());
// If StorageRedefined, we cannot use mState.getSize() to allocate a new buffer.
ASSERT(updateType != BufferUpdateType::StorageRedefined);
ASSERT(mBuffer.valid());
ASSERT(mBuffer.getSize() >= bufferSize);
// Here we acquire a new BufferHelper and directUpdate() the new buffer.
// If the subData size was less than the buffer's size we additionally enqueue
// a GPU copy of the remaining regions from the old mBuffer to the new one.
vk::BufferHelper prevBuffer;
size_t offsetAfterSubdata = (updateOffset + updateSize);
bool updateRegionBeforeSubData = mHasValidData && (updateOffset > 0);
bool updateRegionAfterSubData = mHasValidData && (offsetAfterSubdata < bufferSize);
uint8_t *prevMapPtrBeforeSubData = nullptr;
uint8_t *prevMapPtrAfterSubData = nullptr;
if (updateRegionBeforeSubData || updateRegionAfterSubData)
{
prevBuffer = std::move(mBuffer);
// The total bytes that we need to copy from old buffer to new buffer
size_t copySize = bufferSize - updateSize;
// If the buffer is host visible and the GPU is not writing to it, we use the CPU to do the
// copy. We need to save the source buffer pointer before we acquire a new buffer.
if (ShouldUseCPUToCopyData(contextVk, prevBuffer, copySize, bufferSize))
{
uint8_t *mapPointer = nullptr;
// prevBuffer buffer will be recycled (or released and unmapped) by acquireBufferHelper
ANGLE_TRY(prevBuffer.map(contextVk, &mapPointer));
ASSERT(mapPointer);
prevMapPtrBeforeSubData = mapPointer;
prevMapPtrAfterSubData = mapPointer + offsetAfterSubdata;
}
}
ANGLE_TRY(acquireBufferHelper(contextVk, bufferSize, BufferUsageType::Dynamic));
ANGLE_TRY(updateBuffer(contextVk, bufferSize, dataSource, updateSize, updateOffset));
constexpr int kMaxCopyRegions = 2;
angle::FixedVector<VkBufferCopy, kMaxCopyRegions> copyRegions;
if (updateRegionBeforeSubData)
{
if (prevMapPtrBeforeSubData)
{
BufferDataSource beforeSrc = {};
beforeSrc.data = prevMapPtrBeforeSubData;
ANGLE_TRY(directUpdate(contextVk, beforeSrc, updateOffset, 0));
}
else
{
copyRegions.push_back({prevBuffer.getOffset(), mBuffer.getOffset(), updateOffset});
}
}
if (updateRegionAfterSubData)
{
size_t copySize = bufferSize - offsetAfterSubdata;
if (prevMapPtrAfterSubData)
{
BufferDataSource afterSrc = {};
afterSrc.data = prevMapPtrAfterSubData;
ANGLE_TRY(directUpdate(contextVk, afterSrc, copySize, offsetAfterSubdata));
}
else
{
copyRegions.push_back({prevBuffer.getOffset() + offsetAfterSubdata,
mBuffer.getOffset() + offsetAfterSubdata, copySize});
}
}
if (!copyRegions.empty())
{
ANGLE_TRY(CopyBuffers(contextVk, &prevBuffer, &mBuffer,
static_cast<uint32_t>(copyRegions.size()), copyRegions.data()));
}
if (prevBuffer.valid())
{
ANGLE_TRY(contextVk->releaseBufferAllocation(&prevBuffer));
}
return angle::Result::Continue;
}
angle::Result BufferVk::setDataImpl(ContextVk *contextVk,
size_t bufferSize,
const BufferDataSource &dataSource,
size_t updateSize,
size_t updateOffset,
BufferUpdateType updateType)
{
// if the buffer is currently in use
// if it isn't an external buffer and not a self-copy and sub data size meets threshold
// acquire a new BufferHelper from the pool
// else stage the update
// else update the buffer directly
if (isCurrentlyInUse(contextVk->getRenderer()))
{
// The acquire-and-update path creates a new buffer, which is sometimes more efficient than
// trying to update the existing one. Firstly, this is not done in the following
// situations:
//
// - For external buffers, the underlying storage cannot be reallocated.
// - If storage has just been redefined, this path is not taken because a new buffer has
// already been created by the caller. Besides, this path uses mState.getSize(), which the
// frontend updates only after this call in situations where the storage may be redefined.
// This could happen if the buffer memory is DEVICE_LOCAL and
// renderer->getFeatures().allocateNonZeroMemory.enabled is true. In this case a
// copyToBuffer is immediately issued after allocation and isCurrentlyInUse will be true.
// - If this is a self copy through glCopyBufferSubData, |dataSource| will contain a
// reference to |mBuffer|, in which case source information is lost after acquiring a new
// buffer.
//
// Additionally, this path is taken only if either of the following conditions are true:
//
// - If BufferVk does not have any valid data. This means that there is no data to be
// copied from the old buffer to the new one after acquiring it. This could happen when
// the application calls glBufferData with the same size and we reuse the existing buffer
// storage.
// - If the buffer is used read-only in the current render pass. In this case, acquiring a
// new buffer is preferred to avoid breaking the render pass.
// - The update modifies a significant portion of the buffer
// - The preferCPUForBufferSubData feature is enabled.
//
const bool canAcquireAndUpdate = !isExternalBuffer() &&
updateType != BufferUpdateType::StorageRedefined &&
!IsSelfCopy(dataSource, mBuffer);
if (canAcquireAndUpdate &&
(!mHasValidData || ShouldAvoidRenderPassBreakOnUpdate(contextVk, mBuffer, bufferSize) ||
ShouldAllocateNewMemoryForUpdate(contextVk, updateSize, bufferSize)))
{
ANGLE_TRY(acquireAndUpdate(contextVk, bufferSize, dataSource, updateSize, updateOffset,
updateType));
}
else
{
if (canAcquireAndUpdate && RenderPassUsesBufferForReadOnly(contextVk, mBuffer))
{
ANGLE_VK_PERF_WARNING(contextVk, GL_DEBUG_SEVERITY_LOW,
"Breaking the render pass on small upload to large buffer");
}
ANGLE_TRY(stagedUpdate(contextVk, dataSource, updateSize, updateOffset));
}
}
else
{
ANGLE_TRY(updateBuffer(contextVk, bufferSize, dataSource, updateSize, updateOffset));
}
// Update conversions.
if (updateOffset == 0 && updateSize == bufferSize)
{
dataUpdated();
}
else
{
dataRangeUpdated(RangeDeviceSize(updateOffset, updateOffset + updateSize));
}
return angle::Result::Continue;
}
VertexConversionBuffer *BufferVk::getVertexConversionBuffer(
vk::Renderer *renderer,
const VertexConversionBuffer::CacheKey &cacheKey)
{
for (VertexConversionBuffer &buffer : mVertexConversionBuffers)
{
if (buffer.match(cacheKey))
{
ASSERT(buffer.valid());
return &buffer;
}
}
mVertexConversionBuffers.emplace_back(renderer, cacheKey);
return &mVertexConversionBuffers.back();
}
void BufferVk::dataRangeUpdated(const RangeDeviceSize &range)
{
for (VertexConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.addDirtyBufferRange(range);
}
// Now we have valid data
mHasValidData = true;
}
void BufferVk::dataUpdated()
{
for (VertexConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.setEntireBufferDirty();
}
// Now we have valid data
mHasValidData = true;
}
void BufferVk::onDataChanged()
{
dataUpdated();
}
angle::Result BufferVk::acquireBufferHelper(ContextVk *contextVk,
size_t sizeInBytes,
BufferUsageType usageType)
{
vk::Renderer *renderer = contextVk->getRenderer();
size_t size = roundUpPow2(sizeInBytes, kBufferSizeGranularity);
size_t alignment = renderer->getDefaultBufferAlignment();
if (mBuffer.valid())
{
ANGLE_TRY(contextVk->releaseBufferAllocation(&mBuffer));
}
// Allocate the buffer directly
ANGLE_TRY(
contextVk->initBufferAllocation(&mBuffer, mMemoryTypeIndex, size, alignment, usageType));
// Tell the observers (front end) that a new buffer was created, so the necessary
// dirty bits can be set. This allows the buffer views pointing to the old buffer to
// be recreated and point to the new buffer, along with updating the descriptor sets
// to use the new buffer.
onStateChange(angle::SubjectMessage::InternalMemoryAllocationChanged);
return angle::Result::Continue;
}
bool BufferVk::isCurrentlyInUse(vk::Renderer *renderer) const
{
return !renderer->hasResourceUseFinished(mBuffer.getResourceUse());
}
// When a buffer is being completely changed, calculate whether it's better to allocate a new buffer
// or overwrite the existing one.
BufferUpdateType BufferVk::calculateBufferUpdateTypeOnFullUpdate(
vk::Renderer *renderer,
size_t size,
VkMemoryPropertyFlags memoryPropertyFlags,
BufferUsageType usageType,
const void *data) const
{
// 0-sized updates should be no-op'd before this call.
ASSERT(size > 0);
// If there is no existing buffer, this cannot be a content update.
if (!mBuffer.valid())
{
return BufferUpdateType::StorageRedefined;
}
const bool inUseAndRespecifiedWithoutData = data == nullptr && isCurrentlyInUse(renderer);
bool redefineStorage = shouldRedefineStorage(renderer, usageType, memoryPropertyFlags, size);
// Create a new buffer if the buffer is busy and it's being redefined without data.
// Additionally, a new buffer is created if any of the parameters change (memory type, usage,
// size).
return redefineStorage || inUseAndRespecifiedWithoutData ? BufferUpdateType::StorageRedefined
: BufferUpdateType::ContentsUpdate;
}
bool BufferVk::shouldRedefineStorage(vk::Renderer *renderer,
BufferUsageType usageType,
VkMemoryPropertyFlags memoryPropertyFlags,
size_t size) const
{
if (mUsageType != usageType)
{
return true;
}
if (mMemoryPropertyFlags != memoryPropertyFlags)
{
return true;
}
if (size > mBuffer.getSize())
{
return true;
}
else
{
size_t paddedBufferSize =
(renderer->getFeatures().padBuffersToMaxVertexAttribStride.enabled)
? (size + static_cast<size_t>(renderer->getMaxVertexAttribStride()))
: size;
size_t sizeInBytes = roundUpPow2(paddedBufferSize, kBufferSizeGranularity);
size_t alignedSize = roundUp(sizeInBytes, renderer->getDefaultBufferAlignment());
if (alignedSize > mBuffer.getSize())
{
return true;
}
}
return false;
}
} // namespace rx