Hash :
eaa6961d
Author :
Date :
2021-05-17T18:56:53
Revert "Vulkan: Disable BufferVk suballocation" This reverts commit 76181384075c6eb0a5788bf1b732a1e05f6d73bc. Reason for revert: Bug exposed by this is fixed in https://chromium-review.googlesource.com/c/angle/angle/+/2896168 Original change's description: > Vulkan: Disable BufferVk suballocation > > There are still unresolved bugs. > > Bug: angleproject:5719 > Bug: chromium:1209197 > Change-Id: I6a971c421d0ae266404d1ecbf8741a9747a4e809 > Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2897545 > Reviewed-by: Cody Northrop <cnorthrop@google.com> > Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> > Commit-Queue: Tim Van Patten <timvp@google.com> Bug: angleproject:5719 Bug: chromium:1209197 Change-Id: I5c24b5f6476eab98ed5a7b90b3d1796ffc7ca106 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2896169 Bot-Commit: Rubber Stamper <rubber-stamper@appspot.gserviceaccount.com> Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 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
//
// 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/RendererVk.h"
#include "libANGLE/trace.h"
namespace rx
{
namespace
{
// 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.
ANGLE_INLINE VkMemoryPropertyFlags GetPreferredMemoryType(gl::BufferBinding target,
gl::BufferUsage usage)
{
constexpr VkMemoryPropertyFlags kDeviceLocalFlags =
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT |
VK_MEMORY_PROPERTY_DEVICE_LOCAL_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);
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 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 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 kHostCachedFlags;
default:
UNREACHABLE();
return kHostCachedFlags;
}
}
ANGLE_INLINE VkMemoryPropertyFlags GetStorageMemoryType(GLbitfield storageFlags,
bool externalBuffer)
{
constexpr VkMemoryPropertyFlags kDeviceLocalHostVisibleFlags =
(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT);
constexpr VkMemoryPropertyFlags kDeviceLocalHostCoherentFlags =
(VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT | VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
const bool isCoherentMap = (storageFlags & GL_MAP_COHERENT_BIT_EXT) != 0;
const bool isPersistentMap = (storageFlags & GL_MAP_PERSISTENT_BIT_EXT) != 0;
if (isCoherentMap || isPersistentMap || 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 kDeviceLocalHostVisibleFlags;
}
size_t GetPreferredDynamicBufferInitialSize(RendererVk *renderer,
size_t dataSize,
gl::BufferUsage usage,
size_t *alignmentOut)
{
// The buffer may be used for a number of different operations, so its allocations should
// have an alignment that satisifies all.
const VkPhysicalDeviceLimits &limitsVk = renderer->getPhysicalDeviceProperties().limits;
// All known vendors have power-of-2 alignment requirements, so std::max can work instead of
// lcm.
ASSERT(gl::isPow2(limitsVk.minUniformBufferOffsetAlignment));
ASSERT(gl::isPow2(limitsVk.minStorageBufferOffsetAlignment));
ASSERT(gl::isPow2(limitsVk.minTexelBufferOffsetAlignment));
ASSERT(gl::isPow2(limitsVk.minMemoryMapAlignment));
*alignmentOut = std::max({static_cast<size_t>(limitsVk.minUniformBufferOffsetAlignment),
static_cast<size_t>(limitsVk.minStorageBufferOffsetAlignment),
static_cast<size_t>(limitsVk.minTexelBufferOffsetAlignment),
limitsVk.minMemoryMapAlignment});
// mBuffer will be allocated through a DynamicBuffer. If hinted to be DYNAMIC, have
// DynamicBuffer allocate bigger blocks to suballocate from. Otherwise, let it adapt to the
// buffer size automatically (which will allocate BufferHelpers with the same size as this
// buffer).
const bool isDynamic = usage == gl::BufferUsage::DynamicDraw ||
usage == gl::BufferUsage::DynamicCopy ||
usage == gl::BufferUsage::DynamicRead;
// Sub-allocate from a 4KB buffer. If the buffer allocations are bigger, the dynamic buffer
// will adapt to it automatically (and stop sub-allocating).
constexpr size_t kDynamicBufferMaxSize = 4 * 1024;
const size_t alignedSize = roundUp(dataSize, *alignmentOut);
const size_t suballocationCount = kDynamicBufferMaxSize / alignedSize;
const size_t initialSize = isDynamic ? alignedSize * suballocationCount : 0;
return initialSize;
}
ANGLE_INLINE bool SubDataSizeMeetsThreshold(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.
return subDataSize > (bufferSize / 2);
}
ANGLE_INLINE bool IsUsageDynamic(gl::BufferUsage usage)
{
return (usage == gl::BufferUsage::DynamicDraw || usage == gl::BufferUsage::DynamicCopy ||
usage == gl::BufferUsage::DynamicRead);
}
} // namespace
// ConversionBuffer implementation.
ConversionBuffer::ConversionBuffer(RendererVk *renderer,
VkBufferUsageFlags usageFlags,
size_t initialSize,
size_t alignment,
bool hostVisible)
: dirty(true), lastAllocationOffset(0)
{
data.init(renderer, usageFlags, alignment, initialSize, hostVisible,
vk::DynamicBufferPolicy::OneShotUse);
}
ConversionBuffer::~ConversionBuffer() = default;
ConversionBuffer::ConversionBuffer(ConversionBuffer &&other) = default;
// BufferVk::VertexConversionBuffer implementation.
BufferVk::VertexConversionBuffer::VertexConversionBuffer(RendererVk *renderer,
angle::FormatID formatIDIn,
GLuint strideIn,
size_t offsetIn,
bool hostVisible)
: ConversionBuffer(renderer,
vk::kVertexBufferUsageFlags,
kConvertedArrayBufferInitialSize,
vk::kVertexBufferAlignment,
hostVisible),
formatID(formatIDIn),
stride(strideIn),
offset(offsetIn)
{}
BufferVk::VertexConversionBuffer::VertexConversionBuffer(VertexConversionBuffer &&other) = default;
BufferVk::VertexConversionBuffer::~VertexConversionBuffer() = default;
// BufferVk implementation.
BufferVk::BufferVk(const gl::BufferState &state)
: BufferImpl(state), mBuffer(nullptr), mBufferOffset(0)
{}
BufferVk::~BufferVk() {}
void BufferVk::destroy(const gl::Context *context)
{
ContextVk *contextVk = vk::GetImpl(context);
release(contextVk);
}
void BufferVk::release(ContextVk *contextVk)
{
RendererVk *renderer = contextVk->getRenderer();
// For external buffers, mBuffer is not a reference to a chunk in mBufferPool.
// It was allocated explicitly and needs to be deallocated during release(...)
if (mBuffer && mBuffer->isExternalBuffer())
{
mBuffer->release(renderer);
}
mShadowBuffer.release();
mBufferPool.release(renderer);
mHostVisibleBufferPool.release(renderer);
mBuffer = nullptr;
mBufferOffset = 0;
for (ConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.data.release(renderer);
}
}
angle::Result BufferVk::initializeShadowBuffer(ContextVk *contextVk,
gl::BufferBinding target,
size_t size)
{
if (!contextVk->getRenderer()->getFeatures().shadowBuffers.enabled)
{
return angle::Result::Continue;
}
// For now, enable shadow buffers only for pixel unpack buffers.
// If usecases present themselves, we can enable them for other buffer types.
// Note: If changed, update the waitForIdle message in BufferVk::copySubData to reflect it.
if (target == gl::BufferBinding::PixelUnpack)
{
// Initialize the shadow buffer
mShadowBuffer.init(size);
// Allocate required memory. If allocation fails, treat it is a non-fatal error
// since we do not need the shadow buffer for functionality
ANGLE_TRY(mShadowBuffer.allocate(size));
}
return angle::Result::Continue;
}
void BufferVk::initializeHostVisibleBufferPool(ContextVk *contextVk)
{
// These buffers will only be used as transfer sources or transfer targets.
constexpr VkImageUsageFlags kUsageFlags =
VK_BUFFER_USAGE_TRANSFER_SRC_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
// These buffers need to be host visible.
constexpr VkMemoryPropertyFlags kDeviceLocalHostCoherentFlags =
(VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
constexpr size_t kBufferHelperAlignment = 1;
constexpr size_t kBufferHelperPoolInitialSize = 0;
mHostVisibleBufferPool.initWithFlags(
contextVk->getRenderer(), kUsageFlags, kBufferHelperAlignment, kBufferHelperPoolInitialSize,
kDeviceLocalHostCoherentFlags, vk::DynamicBufferPolicy::SporadicTextureUpload);
}
void BufferVk::updateShadowBuffer(const uint8_t *data, size_t size, size_t offset)
{
if (mShadowBuffer.valid())
{
mShadowBuffer.updateData(data, size, offset);
}
}
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.
release(contextVk);
// We could potentially use multiple backing buffers for different usages.
// For now keep a single buffer with all relevant usage flags.
VkImageUsageFlags usageFlags =
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_INDIRECT_BUFFER_BIT;
if (contextVk->getFeatures().supportsTransformFeedbackExtension.enabled)
{
usageFlags |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT;
}
std::unique_ptr<vk::BufferHelper> buffer = std::make_unique<vk::BufferHelper>();
VkBufferCreateInfo createInfo = {};
createInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
createInfo.flags = 0;
createInfo.size = size;
createInfo.usage = usageFlags;
createInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
createInfo.queueFamilyIndexCount = 0;
createInfo.pQueueFamilyIndices = nullptr;
ANGLE_TRY(buffer->initExternal(contextVk, memoryPropertyFlags, createInfo, clientBuffer));
ASSERT(!mBuffer);
mBuffer = buffer.release();
mBufferOffset = 0;
return angle::Result::Continue;
}
angle::Result BufferVk::setDataWithUsageFlags(const gl::Context *context,
gl::BufferBinding target,
GLeglClientBufferEXT clientBuffer,
const void *data,
size_t size,
gl::BufferUsage usage,
GLbitfield flags)
{
VkMemoryPropertyFlags memoryPropertyFlags = 0;
bool persistentMapRequired = false;
const bool isExternalBuffer = clientBuffer != nullptr;
switch (usage)
{
case gl::BufferUsage::InvalidEnum:
{
// glBufferStorage API call
memoryPropertyFlags = GetStorageMemoryType(flags, isExternalBuffer);
persistentMapRequired = (flags & GL_MAP_PERSISTENT_BIT_EXT) != 0;
break;
}
default:
{
// glBufferData API call
memoryPropertyFlags = GetPreferredMemoryType(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(vk::GetImpl(context), !persistentMapRequired,
VK_ERROR_MEMORY_MAP_FAILED);
// Since external buffer is not host visible, allocate a host visible buffer pool
// to handle map/unmap operations.
initializeHostVisibleBufferPool(vk::GetImpl(context));
}
return angle::Result::Continue;
}
return setDataWithMemoryType(context, target, data, size, memoryPropertyFlags,
persistentMapRequired, usage);
}
angle::Result BufferVk::setData(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
gl::BufferUsage usage)
{
// Assume host visible/coherent memory available.
VkMemoryPropertyFlags memoryPropertyFlags = GetPreferredMemoryType(target, usage);
return setDataWithMemoryType(context, target, data, size, memoryPropertyFlags, false, usage);
}
angle::Result BufferVk::setDataWithMemoryType(const gl::Context *context,
gl::BufferBinding target,
const void *data,
size_t size,
VkMemoryPropertyFlags memoryPropertyFlags,
bool persistentMapRequired,
gl::BufferUsage usage)
{
ContextVk *contextVk = vk::GetImpl(context);
RendererVk *renderer = contextVk->getRenderer();
if (size == 0)
{
// Nothing to do.
return angle::Result::Continue;
}
// BufferData call is re-specifying the entire buffer
// Release and init a new mBuffer with this new size
if (size != static_cast<size_t>(mState.getSize()))
{
// Release and re-create the memory and buffer.
release(contextVk);
// We could potentially use multiple backing buffers for different usages.
// For now keep a single buffer with all relevant usage flags.
VkImageUsageFlags usageFlags =
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 (contextVk->getFeatures().supportsTransformFeedbackExtension.enabled)
{
usageFlags |= VK_BUFFER_USAGE_TRANSFORM_FEEDBACK_BUFFER_BIT_EXT;
}
size_t bufferHelperAlignment = 0;
const size_t bufferHelperPoolInitialSize =
GetPreferredDynamicBufferInitialSize(renderer, size, usage, &bufferHelperAlignment);
mBufferPool.initWithFlags(renderer, usageFlags, bufferHelperAlignment,
bufferHelperPoolInitialSize, memoryPropertyFlags,
vk::DynamicBufferPolicy::FrequentSmallAllocations);
ANGLE_TRY(acquireBufferHelper(contextVk, size));
// persistentMapRequired may request that the server read from or write to the buffer while
// it is mapped. The client's pointer to the data store remains valid so long as the data
// store is mapped. So it cannot have shadow buffer
if (!persistentMapRequired)
{
// Initialize the shadow buffer
ANGLE_TRY(initializeShadowBuffer(contextVk, target, size));
}
}
if (data)
{
ANGLE_TRY(setDataImpl(contextVk, static_cast<const uint8_t *>(data), size, 0));
}
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 && mBuffer->valid());
ContextVk *contextVk = vk::GetImpl(context);
ANGLE_TRY(setDataImpl(contextVk, static_cast<const uint8_t *>(data), size, offset));
return angle::Result::Continue;
}
angle::Result BufferVk::copySubData(const gl::Context *context,
BufferImpl *source,
GLintptr sourceOffset,
GLintptr destOffset,
GLsizeiptr size)
{
ASSERT(mBuffer && mBuffer->valid());
ContextVk *contextVk = vk::GetImpl(context);
BufferVk *sourceVk = GetAs<BufferVk>(source);
VkDeviceSize sourceBufferOffset = 0;
vk::BufferHelper &sourceBuffer = sourceVk->getBufferAndOffset(&sourceBufferOffset);
ASSERT(sourceBuffer.valid());
// If the shadow buffer is enabled for the destination buffer then
// we need to update that as well. This will require us to complete
// all recorded and in-flight commands involving the source buffer.
if (mShadowBuffer.valid())
{
// Map the source buffer.
void *mapPtr;
ANGLE_TRY(sourceVk->mapRangeImpl(contextVk, sourceOffset, size, 0, &mapPtr));
// Update the shadow buffer with data from source buffer
updateShadowBuffer(static_cast<uint8_t *>(mapPtr), size, destOffset);
// Unmap the source buffer
ANGLE_TRY(sourceVk->unmapImpl(contextVk));
}
// Check for self-dependency.
vk::CommandBufferAccess access;
if (sourceBuffer.getBufferSerial() == mBuffer->getBufferSerial())
{
access.onBufferSelfCopy(mBuffer);
}
else
{
access.onBufferTransferRead(&sourceBuffer);
access.onBufferTransferWrite(mBuffer);
}
vk::CommandBuffer *commandBuffer;
ANGLE_TRY(contextVk->getOutsideRenderPassCommandBuffer(access, &commandBuffer));
// Enqueue a copy command on the GPU.
const VkBufferCopy copyRegion = {static_cast<VkDeviceSize>(sourceOffset) + sourceBufferOffset,
static_cast<VkDeviceSize>(destOffset) + mBufferOffset,
static_cast<VkDeviceSize>(size)};
commandBuffer->copyBuffer(sourceBuffer.getBuffer(), mBuffer->getBuffer(), 1, ©Region);
// The new destination buffer data may require a conversion for the next draw, so mark it dirty.
onDataChanged();
return angle::Result::Continue;
}
angle::Result BufferVk::handleDeviceLocalBufferMap(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize size,
void **mapPtr)
{
// The buffer is device local, create a copy of the buffer and return its CPU pointer.
bool needToReleasePreviousBuffers = false;
ANGLE_TRY(mHostVisibleBufferPool.allocate(
contextVk, static_cast<size_t>(size), reinterpret_cast<uint8_t **>(mapPtr), nullptr,
&mHostVisibleBufferOffset, &needToReleasePreviousBuffers));
if (needToReleasePreviousBuffers)
{
// Release previous buffers
mHostVisibleBufferPool.releaseInFlightBuffers(contextVk);
}
// Copy data from device local buffer to host visible staging buffer.
vk::BufferHelper *hostVisibleBuffer = mHostVisibleBufferPool.getCurrentBuffer();
ASSERT(hostVisibleBuffer && hostVisibleBuffer->valid());
VkBufferCopy copyRegion = {mBufferOffset + offset, mHostVisibleBufferOffset, size};
ANGLE_TRY(hostVisibleBuffer->copyFromBuffer(contextVk, mBuffer, 1, ©Region));
ANGLE_TRY(
hostVisibleBuffer->waitForIdle(contextVk, "GPU stall due to mapping device local buffer"));
return angle::Result::Continue;
}
angle::Result BufferVk::handleDeviceLocalBufferUnmap(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize size)
{
// Copy data from the host visible buffer into the device local buffer.
vk::BufferHelper *hostVisibleBuffer = mHostVisibleBufferPool.getCurrentBuffer();
ASSERT(hostVisibleBuffer && hostVisibleBuffer->valid());
VkBufferCopy copyRegion = {mHostVisibleBufferOffset, mBufferOffset + offset, size};
ANGLE_TRY(mBuffer->copyFromBuffer(contextVk, hostVisibleBuffer, 1, ©Region));
return angle::Result::Continue;
}
angle::Result BufferVk::map(const gl::Context *context, GLenum access, void **mapPtr)
{
ASSERT(mBuffer && mBuffer->valid());
return mapImpl(vk::GetImpl(context), mapPtr);
}
angle::Result BufferVk::mapRange(const gl::Context *context,
size_t offset,
size_t length,
GLbitfield access,
void **mapPtr)
{
ANGLE_TRACE_EVENT0("gpu.angle", "BufferVk::mapRange");
return mapRangeImpl(vk::GetImpl(context), offset, length, access, mapPtr);
}
angle::Result BufferVk::mapImpl(ContextVk *contextVk, void **mapPtr)
{
return mapRangeImpl(contextVk, 0, static_cast<VkDeviceSize>(mState.getSize()), 0, mapPtr);
}
angle::Result BufferVk::mapRangeImpl(ContextVk *contextVk,
VkDeviceSize offset,
VkDeviceSize length,
GLbitfield access,
void **mapPtr)
{
if (!mShadowBuffer.valid())
{
ASSERT(mBuffer && mBuffer->valid());
if ((access & GL_MAP_INVALIDATE_BUFFER_BIT) != 0 &&
mBuffer->isCurrentlyInUse(contextVk->getLastCompletedQueueSerial()) &&
!mBuffer->isExternalBuffer())
{
// We try to map buffer, but buffer is busy. Caller has told us it doesn't care about
// previous content. Instead of wait for GPU to finish, we just allocate a new buffer.
ANGLE_TRY(acquireBufferHelper(contextVk, static_cast<size_t>(mState.getSize())));
}
else if ((access & GL_MAP_UNSYNCHRONIZED_BIT) == 0)
{
ANGLE_TRY(mBuffer->waitForIdle(contextVk,
"GPU stall due to mapping buffer in use by the GPU"));
}
if (mBuffer->isHostVisible())
{
// If the buffer is host visible, map and return.
ANGLE_TRY(mBuffer->mapWithOffset(contextVk, reinterpret_cast<uint8_t **>(mapPtr),
static_cast<size_t>(mBufferOffset + offset)));
}
else
{
// Handle device local buffers.
ANGLE_TRY(handleDeviceLocalBufferMap(contextVk, offset, length, mapPtr));
}
}
else
{
// If the app requested a GL_MAP_UNSYNCHRONIZED_BIT access, the spec states -
// No GL error is generated if pending operations which source or modify the
// buffer overlap the mapped region, but the result of such previous and any
// subsequent operations is undefined
// To keep the code simple, irrespective of whether the access was GL_MAP_UNSYNCHRONIZED_BIT
// or not, just return the shadow buffer.
mShadowBuffer.map(static_cast<size_t>(offset), mapPtr);
}
return angle::Result::Continue;
}
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 && mBuffer->valid());
bool writeOperation = ((mState.getAccessFlags() & GL_MAP_WRITE_BIT) != 0);
if (!mShadowBuffer.valid() && mBuffer->isHostVisible())
{
mBuffer->unmap(contextVk->getRenderer());
}
else
{
size_t offset = static_cast<size_t>(mState.getMapOffset());
size_t size = static_cast<size_t>(mState.getMapLength());
// If it was a write operation we need to update the buffer with new data.
if (writeOperation)
{
if (mShadowBuffer.valid())
{
// We do not yet know if this data will ever be used. Perform a staged
// update which will get flushed if and when necessary.
const uint8_t *data = getShadowBuffer(offset);
ANGLE_TRY(stagedUpdate(contextVk, data, size, offset));
mShadowBuffer.unmap();
}
else
{
// The buffer is device local.
ASSERT(!mBuffer->isHostVisible());
ANGLE_TRY(handleDeviceLocalBufferUnmap(contextVk, offset, size));
}
}
}
if (writeOperation)
{
markConversionBuffersDirty();
}
return angle::Result::Continue;
}
angle::Result BufferVk::getSubData(const gl::Context *context,
GLintptr offset,
GLsizeiptr size,
void *outData)
{
ASSERT(offset + size <= getSize());
if (!mShadowBuffer.valid())
{
ASSERT(mBuffer && mBuffer->valid());
ContextVk *contextVk = vk::GetImpl(context);
void *mapPtr;
ANGLE_TRY(mapRangeImpl(contextVk, offset, size, 0, &mapPtr));
memcpy(outData, mapPtr, size);
ANGLE_TRY(unmapImpl(contextVk));
}
else
{
memcpy(outData, mShadowBuffer.getCurrentBuffer() + offset, size);
}
return angle::Result::Continue;
}
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);
RendererVk *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(), 0, &mapPtr));
*outRange = gl::ComputeIndexRange(type, mapPtr, count, primitiveRestartEnabled);
ANGLE_TRY(unmapImpl(contextVk));
return angle::Result::Continue;
}
angle::Result BufferVk::updateBuffer(ContextVk *contextVk,
const uint8_t *data,
size_t size,
size_t offset)
{
if (mBuffer->isHostVisible())
{
ANGLE_TRY(directUpdate(contextVk, data, size, offset));
}
else
{
ANGLE_TRY(stagedUpdate(contextVk, data, size, offset));
}
return angle::Result::Continue;
}
angle::Result BufferVk::directUpdate(ContextVk *contextVk,
const uint8_t *data,
size_t size,
size_t offset)
{
uint8_t *mapPointer = nullptr;
ANGLE_TRY(mBuffer->mapWithOffset(contextVk, &mapPointer,
static_cast<size_t>(mBufferOffset) + offset));
ASSERT(mapPointer);
memcpy(mapPointer, data, size);
// 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 (!IsUsageDynamic(mState.getUsage()))
{
mBuffer->unmap(contextVk->getRenderer());
}
ASSERT(mBuffer->isCoherent());
return angle::Result::Continue;
}
angle::Result BufferVk::stagedUpdate(ContextVk *contextVk,
const uint8_t *data,
size_t size,
size_t offset)
{
// Acquire a "new" staging buffer
uint8_t *mapPointer = nullptr;
VkDeviceSize stagingBufferOffset = 0;
vk::DynamicBuffer *stagingBuffer = contextVk->getStagingBuffer();
ANGLE_TRY(stagingBuffer->allocate(contextVk, size, &mapPointer, nullptr, &stagingBufferOffset,
nullptr));
ASSERT(mapPointer);
memcpy(mapPointer, data, size);
ASSERT(!stagingBuffer->isCoherent());
ANGLE_TRY(stagingBuffer->flush(contextVk));
// Enqueue a copy command on the GPU.
VkBufferCopy copyRegion = {stagingBufferOffset, mBufferOffset + offset, size};
ANGLE_TRY(
mBuffer->copyFromBuffer(contextVk, stagingBuffer->getCurrentBuffer(), 1, ©Region));
return angle::Result::Continue;
}
angle::Result BufferVk::acquireAndUpdate(ContextVk *contextVk,
const uint8_t *data,
size_t updateSize,
size_t offset)
{
// 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 *src = mBuffer;
size_t bufferSize = static_cast<size_t>(mState.getSize());
size_t offsetAfterSubdata = (offset + updateSize);
bool updateRegionBeforeSubData = (offset > 0);
bool updateRegionAfterSubData = (offsetAfterSubdata < bufferSize);
if (updateRegionBeforeSubData || updateRegionAfterSubData)
{
src->retain(&contextVk->getResourceUseList());
}
ANGLE_TRY(acquireBufferHelper(contextVk, bufferSize));
ANGLE_TRY(updateBuffer(contextVk, data, updateSize, offset));
constexpr int kMaxCopyRegions = 2;
angle::FixedVector<VkBufferCopy, kMaxCopyRegions> copyRegions;
if (updateRegionBeforeSubData)
{
copyRegions.push_back({0, mBufferOffset, offset});
}
if (updateRegionAfterSubData)
{
copyRegions.push_back({offsetAfterSubdata, mBufferOffset + offsetAfterSubdata,
(bufferSize - offsetAfterSubdata)});
}
if (!copyRegions.empty())
{
ANGLE_TRY(mBuffer->copyFromBuffer(contextVk, src, static_cast<uint32_t>(copyRegions.size()),
copyRegions.data()));
}
return angle::Result::Continue;
}
angle::Result BufferVk::setDataImpl(ContextVk *contextVk,
const uint8_t *data,
size_t size,
size_t offset)
{
// Update shadow buffer
updateShadowBuffer(data, size, offset);
// if the buffer is currently in use
// if it isn't an external buffer and sub data size meets threshold
// acquire a new BufferHelper from the pool
// else stage the update
// else update the buffer directly
if (mBuffer->isCurrentlyInUse(contextVk->getLastCompletedQueueSerial()))
{
if (!mBuffer->isExternalBuffer() &&
SubDataSizeMeetsThreshold(size, static_cast<size_t>(mState.getSize())))
{
ANGLE_TRY(acquireAndUpdate(contextVk, data, size, offset));
}
else
{
ANGLE_TRY(stagedUpdate(contextVk, data, size, offset));
}
}
else
{
ANGLE_TRY(updateBuffer(contextVk, data, size, offset));
}
// Update conversions
markConversionBuffersDirty();
return angle::Result::Continue;
}
ConversionBuffer *BufferVk::getVertexConversionBuffer(RendererVk *renderer,
angle::FormatID formatID,
GLuint stride,
size_t offset,
bool hostVisible)
{
for (VertexConversionBuffer &buffer : mVertexConversionBuffers)
{
if (buffer.formatID == formatID && buffer.stride == stride && buffer.offset == offset)
{
return &buffer;
}
}
mVertexConversionBuffers.emplace_back(renderer, formatID, stride, offset, hostVisible);
return &mVertexConversionBuffers.back();
}
void BufferVk::markConversionBuffersDirty()
{
for (VertexConversionBuffer &buffer : mVertexConversionBuffers)
{
buffer.dirty = true;
}
}
void BufferVk::onDataChanged()
{
markConversionBuffersDirty();
}
angle::Result BufferVk::acquireBufferHelper(ContextVk *contextVk, size_t sizeInBytes)
{
// This method should not be called if it is an ExternalBuffer
ASSERT(mBuffer == nullptr || mBuffer->isExternalBuffer() == false);
bool needToReleasePreviousBuffers = false;
size_t size = roundUpPow2(sizeInBytes, kBufferSizeGranularity);
ANGLE_TRY(mBufferPool.allocate(contextVk, size, nullptr, nullptr, &mBufferOffset,
&needToReleasePreviousBuffers));
if (needToReleasePreviousBuffers)
{
// Release previous buffers
mBufferPool.releaseInFlightBuffers(contextVk);
}
mBuffer = mBufferPool.getCurrentBuffer();
ASSERT(mBuffer);
return angle::Result::Continue;
}
} // namespace rx