Hash :
11366cbb
Author :
Date :
2023-02-06T09:53:27
Vulkan: Bug fix in flushDescriptorSetUpdates(...) Return the size of mWriteDescriptorSets instead. Bug: angleproject:6776 Change-Id: I2af36975b82c34bf4f4f70a374602546f84ace90 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/4225117 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Commit-Queue: mohan maiya <m.maiya@samsung.com> 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
//
// 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.
//
// DisplayVk.cpp:
// Implements the class methods for DisplayVk.
//
#include "libANGLE/renderer/vulkan/DisplayVk.h"
#include "common/debug.h"
#include "common/system_utils.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/renderer/vulkan/BufferVk.h"
#include "libANGLE/renderer/vulkan/ContextVk.h"
#include "libANGLE/renderer/vulkan/DeviceVk.h"
#include "libANGLE/renderer/vulkan/ImageVk.h"
#include "libANGLE/renderer/vulkan/RendererVk.h"
#include "libANGLE/renderer/vulkan/SurfaceVk.h"
#include "libANGLE/renderer/vulkan/SyncVk.h"
#include "libANGLE/renderer/vulkan/TextureVk.h"
#include "libANGLE/renderer/vulkan/VkImageImageSiblingVk.h"
namespace rx
{
namespace
{
// For DesciptorSetUpdates
constexpr size_t kDescriptorBufferInfosInitialSize = 8;
constexpr size_t kDescriptorImageInfosInitialSize = 4;
constexpr size_t kDescriptorWriteInfosInitialSize =
kDescriptorBufferInfosInitialSize + kDescriptorImageInfosInitialSize;
constexpr size_t kDescriptorBufferViewsInitialSize = 0;
// How often monolithic pipelines should be created, if preferMonolithicPipelinesOverLibraries is
// enabled. Pipeline creation is typically O(hundreds of microseconds). A value of 2ms is chosen
// arbitrarily; it ensures that there is always at most a single pipeline job in progress, while
// maintaining a high throughput of 500 pipelines / second for heavier applications.
constexpr double kMonolithicPipelineJobPeriod = 0.002;
// Query surface format and colorspace support.
void GetSupportedFormatColorspaces(VkPhysicalDevice physicalDevice,
const angle::FeaturesVk &featuresVk,
VkSurfaceKHR surface,
std::vector<VkSurfaceFormat2KHR> *surfaceFormatsOut)
{
ASSERT(surfaceFormatsOut);
surfaceFormatsOut->clear();
constexpr VkSurfaceFormat2KHR kSurfaceFormat2Initializer = {
VK_STRUCTURE_TYPE_SURFACE_FORMAT_2_KHR,
nullptr,
{VK_FORMAT_UNDEFINED, VK_COLOR_SPACE_SRGB_NONLINEAR_KHR}};
if (featuresVk.supportsSurfaceCapabilities2Extension.enabled)
{
VkPhysicalDeviceSurfaceInfo2KHR surfaceInfo2 = {};
surfaceInfo2.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_SURFACE_INFO_2_KHR;
surfaceInfo2.surface = surface;
uint32_t surfaceFormatCount = 0;
// Query the count first
VkResult result = vkGetPhysicalDeviceSurfaceFormats2KHR(physicalDevice, &surfaceInfo2,
&surfaceFormatCount, nullptr);
ASSERT(result == VK_SUCCESS);
ASSERT(surfaceFormatCount > 0);
// Query the VkSurfaceFormat2KHR list
std::vector<VkSurfaceFormat2KHR> surfaceFormats2(surfaceFormatCount,
kSurfaceFormat2Initializer);
result = vkGetPhysicalDeviceSurfaceFormats2KHR(physicalDevice, &surfaceInfo2,
&surfaceFormatCount, surfaceFormats2.data());
ASSERT(result == VK_SUCCESS);
*surfaceFormatsOut = std::move(surfaceFormats2);
}
else
{
uint32_t surfaceFormatCount = 0;
// Query the count first
VkResult result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface,
&surfaceFormatCount, nullptr);
ASSERT(result == VK_SUCCESS);
// Query the VkSurfaceFormatKHR list
std::vector<VkSurfaceFormatKHR> surfaceFormats(surfaceFormatCount);
result = vkGetPhysicalDeviceSurfaceFormatsKHR(physicalDevice, surface, &surfaceFormatCount,
surfaceFormats.data());
ASSERT(result == VK_SUCCESS);
// Copy over data from std::vector<VkSurfaceFormatKHR> to std::vector<VkSurfaceFormat2KHR>
std::vector<VkSurfaceFormat2KHR> surfaceFormats2(surfaceFormatCount,
kSurfaceFormat2Initializer);
for (size_t index = 0; index < surfaceFormatCount; index++)
{
surfaceFormats2[index].surfaceFormat.format = surfaceFormats[index].format;
}
*surfaceFormatsOut = std::move(surfaceFormats2);
}
}
} // namespace
// Time interval in seconds that we should try to prune default buffer pools.
constexpr double kTimeElapsedForPruneDefaultBufferPool = 0.25;
// Set to true will log bufferpool stats into INFO stream
#define ANGLE_ENABLE_BUFFER_POOL_STATS_LOGGING 0
DisplayVk::DisplayVk(const egl::DisplayState &state)
: DisplayImpl(state),
vk::Context(new RendererVk()),
mScratchBuffer(1000u),
mSavedError({VK_SUCCESS, "", "", 0}),
mSupportedColorspaceFormatsMap{}
{}
DisplayVk::~DisplayVk()
{
delete mRenderer;
}
egl::Error DisplayVk::initialize(egl::Display *display)
{
ASSERT(mRenderer != nullptr && display != nullptr);
angle::Result result = mRenderer->initialize(this, display, getWSIExtension(), getWSILayer());
ANGLE_TRY(angle::ToEGL(result, this, EGL_NOT_INITIALIZED));
// Query and cache supported surface format and colorspace for later use.
initSupportedSurfaceFormatColorspaces();
return egl::NoError();
}
void DisplayVk::terminate()
{
mRenderer->reloadVolkIfNeeded();
ASSERT(mRenderer);
mRenderer->onDestroy(this);
}
egl::Error DisplayVk::makeCurrent(egl::Display * /*display*/,
egl::Surface * /*drawSurface*/,
egl::Surface * /*readSurface*/,
gl::Context * /*context*/)
{
// Ensure the appropriate global DebugAnnotator is used
ASSERT(mRenderer);
mRenderer->setGlobalDebugAnnotator();
return egl::NoError();
}
bool DisplayVk::testDeviceLost()
{
return mRenderer->isDeviceLost();
}
egl::Error DisplayVk::restoreLostDevice(const egl::Display *display)
{
// A vulkan device cannot be restored, the entire renderer would have to be re-created along
// with any other EGL objects that reference it.
return egl::EglBadDisplay();
}
std::string DisplayVk::getRendererDescription()
{
if (mRenderer)
{
return mRenderer->getRendererDescription();
}
return std::string();
}
std::string DisplayVk::getVendorString()
{
if (mRenderer)
{
return mRenderer->getVendorString();
}
return std::string();
}
std::string DisplayVk::getVersionString(bool includeFullVersion)
{
if (mRenderer)
{
return mRenderer->getVersionString(includeFullVersion);
}
return std::string();
}
DeviceImpl *DisplayVk::createDevice()
{
return new DeviceVk();
}
egl::Error DisplayVk::waitClient(const gl::Context *context)
{
ANGLE_TRACE_EVENT0("gpu.angle", "DisplayVk::waitClient");
ContextVk *contextVk = vk::GetImpl(context);
return angle::ToEGL(contextVk->finishImpl(RenderPassClosureReason::EGLWaitClient), this,
EGL_BAD_ACCESS);
}
egl::Error DisplayVk::waitNative(const gl::Context *context, EGLint engine)
{
ANGLE_TRACE_EVENT0("gpu.angle", "DisplayVk::waitNative");
return angle::ResultToEGL(waitNativeImpl());
}
angle::Result DisplayVk::waitNativeImpl()
{
return angle::Result::Continue;
}
SurfaceImpl *DisplayVk::createWindowSurface(const egl::SurfaceState &state,
EGLNativeWindowType window,
const egl::AttributeMap &attribs)
{
return createWindowSurfaceVk(state, window);
}
SurfaceImpl *DisplayVk::createPbufferSurface(const egl::SurfaceState &state,
const egl::AttributeMap &attribs)
{
ASSERT(mRenderer);
return new OffscreenSurfaceVk(state, mRenderer);
}
SurfaceImpl *DisplayVk::createPbufferFromClientBuffer(const egl::SurfaceState &state,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return static_cast<SurfaceImpl *>(0);
}
SurfaceImpl *DisplayVk::createPixmapSurface(const egl::SurfaceState &state,
NativePixmapType nativePixmap,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return static_cast<SurfaceImpl *>(0);
}
ImageImpl *DisplayVk::createImage(const egl::ImageState &state,
const gl::Context *context,
EGLenum target,
const egl::AttributeMap &attribs)
{
return new ImageVk(state, context);
}
ShareGroupImpl *DisplayVk::createShareGroup()
{
return new ShareGroupVk();
}
bool DisplayVk::isConfigFormatSupported(VkFormat format) const
{
// Requires VK_GOOGLE_surfaceless_query extension to be supported.
ASSERT(mRenderer->getFeatures().supportsSurfacelessQueryExtension.enabled);
// A format is considered supported if it is supported in atleast 1 colorspace.
using ColorspaceFormatSetItem =
const std::pair<const VkColorSpaceKHR, std::unordered_set<VkFormat>>;
for (ColorspaceFormatSetItem &colorspaceFormatSetItem : mSupportedColorspaceFormatsMap)
{
if (colorspaceFormatSetItem.second.count(format) > 0)
{
return true;
}
}
return false;
}
bool DisplayVk::isSurfaceFormatColorspacePairSupported(VkSurfaceKHR surface,
VkFormat format,
VkColorSpaceKHR colorspace) const
{
if (mSupportedColorspaceFormatsMap.size() > 0)
{
return mSupportedColorspaceFormatsMap.count(colorspace) > 0 &&
mSupportedColorspaceFormatsMap.at(colorspace).count(format) > 0;
}
else
{
const angle::FeaturesVk &featuresVk = mRenderer->getFeatures();
std::vector<VkSurfaceFormat2KHR> surfaceFormats;
GetSupportedFormatColorspaces(mRenderer->getPhysicalDevice(), featuresVk, surface,
&surfaceFormats);
if (!featuresVk.supportsSurfaceCapabilities2Extension.enabled)
{
if (surfaceFormats.size() == 1u &&
surfaceFormats[0].surfaceFormat.format == VK_FORMAT_UNDEFINED)
{
return true;
}
}
for (const VkSurfaceFormat2KHR &surfaceFormat : surfaceFormats)
{
if (surfaceFormat.surfaceFormat.format == format &&
surfaceFormat.surfaceFormat.colorSpace == colorspace)
{
return true;
}
}
}
return false;
}
bool DisplayVk::isColorspaceSupported(VkColorSpaceKHR colorspace) const
{
return mSupportedColorspaceFormatsMap.count(colorspace) > 0;
}
void DisplayVk::initSupportedSurfaceFormatColorspaces()
{
const angle::FeaturesVk &featuresVk = mRenderer->getFeatures();
if (featuresVk.supportsSurfacelessQueryExtension.enabled &&
featuresVk.supportsSurfaceCapabilities2Extension.enabled)
{
// Use the VK_GOOGLE_surfaceless_query extension to query supported surface formats and
// colorspaces by using a VK_NULL_HANDLE for the VkSurfaceKHR handle.
std::vector<VkSurfaceFormat2KHR> surfaceFormats;
GetSupportedFormatColorspaces(mRenderer->getPhysicalDevice(), featuresVk, VK_NULL_HANDLE,
&surfaceFormats);
for (const VkSurfaceFormat2KHR &surfaceFormat : surfaceFormats)
{
// Cache supported VkFormat and VkColorSpaceKHR for later use
VkFormat format = surfaceFormat.surfaceFormat.format;
VkColorSpaceKHR colorspace = surfaceFormat.surfaceFormat.colorSpace;
ASSERT(format != VK_FORMAT_UNDEFINED);
mSupportedColorspaceFormatsMap[colorspace].insert(format);
}
ASSERT(mSupportedColorspaceFormatsMap.size() > 0);
}
else
{
mSupportedColorspaceFormatsMap.clear();
}
}
ContextImpl *DisplayVk::createContext(const gl::State &state,
gl::ErrorSet *errorSet,
const egl::Config *configuration,
const gl::Context *shareContext,
const egl::AttributeMap &attribs)
{
return new ContextVk(state, errorSet, mRenderer);
}
StreamProducerImpl *DisplayVk::createStreamProducerD3DTexture(
egl::Stream::ConsumerType consumerType,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return static_cast<StreamProducerImpl *>(0);
}
EGLSyncImpl *DisplayVk::createSync(const egl::AttributeMap &attribs)
{
return new EGLSyncVk(attribs);
}
gl::Version DisplayVk::getMaxSupportedESVersion() const
{
return mRenderer->getMaxSupportedESVersion();
}
gl::Version DisplayVk::getMaxConformantESVersion() const
{
return mRenderer->getMaxConformantESVersion();
}
Optional<gl::Version> DisplayVk::getMaxSupportedDesktopVersion() const
{
return gl::Version{4, 6};
}
egl::Error DisplayVk::validateImageClientBuffer(const gl::Context *context,
EGLenum target,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
switch (target)
{
case EGL_VULKAN_IMAGE_ANGLE:
{
VkImage *vkImage = reinterpret_cast<VkImage *>(clientBuffer);
if (!vkImage || *vkImage == VK_NULL_HANDLE)
{
return egl::EglBadParameter() << "clientBuffer is invalid.";
}
GLenum internalFormat =
static_cast<GLenum>(attribs.get(EGL_TEXTURE_INTERNAL_FORMAT_ANGLE, GL_NONE));
switch (internalFormat)
{
case GL_RGBA:
case GL_BGRA_EXT:
case GL_RGB:
case GL_RED_EXT:
case GL_RG_EXT:
case GL_RGB10_A2_EXT:
case GL_R16_EXT:
case GL_RG16_EXT:
case GL_NONE:
break;
default:
return egl::EglBadParameter() << "Invalid EGLImage texture internal format: 0x"
<< std::hex << internalFormat;
}
uint64_t hi = static_cast<uint64_t>(attribs.get(EGL_VULKAN_IMAGE_CREATE_INFO_HI_ANGLE));
uint64_t lo = static_cast<uint64_t>(attribs.get(EGL_VULKAN_IMAGE_CREATE_INFO_LO_ANGLE));
uint64_t info = ((hi & 0xffffffff) << 32) | (lo & 0xffffffff);
if (reinterpret_cast<const VkImageCreateInfo *>(info)->sType !=
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO)
{
return egl::EglBadParameter()
<< "EGL_VULKAN_IMAGE_CREATE_INFO_HI_ANGLE and "
"EGL_VULKAN_IMAGE_CREATE_INFO_LO_ANGLE are not pointing to a "
"valid VkImageCreateInfo structure.";
}
return egl::NoError();
}
default:
return DisplayImpl::validateImageClientBuffer(context, target, clientBuffer, attribs);
}
}
ExternalImageSiblingImpl *DisplayVk::createExternalImageSibling(const gl::Context *context,
EGLenum target,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
switch (target)
{
case EGL_VULKAN_IMAGE_ANGLE:
return new VkImageImageSiblingVk(buffer, attribs);
default:
return DisplayImpl::createExternalImageSibling(context, target, buffer, attribs);
}
}
void DisplayVk::generateExtensions(egl::DisplayExtensions *outExtensions) const
{
outExtensions->createContextRobustness = getRenderer()->getNativeExtensions().robustnessEXT;
outExtensions->surfaceOrientation = true;
outExtensions->displayTextureShareGroup = true;
outExtensions->displaySemaphoreShareGroup = true;
outExtensions->robustResourceInitializationANGLE = true;
// The Vulkan implementation will always say that EGL_KHR_swap_buffers_with_damage is supported.
// When the Vulkan driver supports VK_KHR_incremental_present, it will use it. Otherwise, it
// will ignore the hint and do a regular swap.
outExtensions->swapBuffersWithDamage = true;
outExtensions->fenceSync = true;
outExtensions->waitSync = true;
outExtensions->image = true;
outExtensions->imageBase = true;
outExtensions->imagePixmap = false; // ANGLE does not support pixmaps
outExtensions->glTexture2DImage = true;
outExtensions->glTextureCubemapImage = true;
outExtensions->glTexture3DImage = getRenderer()->getFeatures().supportsImage2dViewOf3d.enabled;
outExtensions->glRenderbufferImage = true;
outExtensions->imageNativeBuffer =
getRenderer()->getFeatures().supportsAndroidHardwareBuffer.enabled;
outExtensions->surfacelessContext = true;
outExtensions->glColorspace = true;
outExtensions->imageGlColorspace =
outExtensions->glColorspace && getRenderer()->getFeatures().supportsImageFormatList.enabled;
#if defined(ANGLE_PLATFORM_ANDROID)
outExtensions->getNativeClientBufferANDROID = true;
outExtensions->framebufferTargetANDROID = true;
#endif // defined(ANGLE_PLATFORM_ANDROID)
// EGL_EXT_image_dma_buf_import is only exposed if EGL_EXT_image_dma_buf_import_modifiers can
// also be exposed. The Vulkan extensions that support these EGL extensions are not split in
// the same way; both Vulkan extensions are needed for EGL_EXT_image_dma_buf_import, and with
// both Vulkan extensions, EGL_EXT_image_dma_buf_import_modifiers is also supportable.
outExtensions->imageDmaBufImportEXT =
getRenderer()->getFeatures().supportsExternalMemoryDmaBufAndModifiers.enabled;
outExtensions->imageDmaBufImportModifiersEXT = outExtensions->imageDmaBufImportEXT;
// Disable context priority when non-zero memory init is enabled. This enforces a queue order.
outExtensions->contextPriority = !getRenderer()->getFeatures().allocateNonZeroMemory.enabled;
outExtensions->noConfigContext = true;
#if defined(ANGLE_PLATFORM_ANDROID)
outExtensions->nativeFenceSyncANDROID =
getRenderer()->getFeatures().supportsAndroidNativeFenceSync.enabled;
#endif // defined(ANGLE_PLATFORM_ANDROID)
#if defined(ANGLE_PLATFORM_GGP)
outExtensions->ggpStreamDescriptor = true;
outExtensions->swapWithFrameToken = getRenderer()->getFeatures().supportsGGPFrameToken.enabled;
#endif // defined(ANGLE_PLATFORM_GGP)
outExtensions->bufferAgeEXT = true;
outExtensions->protectedContentEXT =
(getRenderer()->getFeatures().supportsProtectedMemory.enabled &&
getRenderer()->getFeatures().supportsSurfaceProtectedSwapchains.enabled);
outExtensions->createSurfaceSwapIntervalANGLE = true;
outExtensions->mutableRenderBufferKHR =
getRenderer()->getFeatures().supportsSharedPresentableImageExtension.enabled;
outExtensions->vulkanImageANGLE = true;
outExtensions->lockSurface3KHR =
getRenderer()->getFeatures().supportsLockSurfaceExtension.enabled;
outExtensions->partialUpdateKHR = true;
outExtensions->timestampSurfaceAttributeANGLE =
getRenderer()->getFeatures().supportsTimestampSurfaceAttribute.enabled;
outExtensions->eglColorspaceAttributePassthroughANGLE =
outExtensions->glColorspace &&
getRenderer()->getFeatures().eglColorspaceAttributePassthrough.enabled;
// If EGL_KHR_gl_colorspace extension is supported check if other colorspace extensions
// can be supported as well.
if (outExtensions->glColorspace)
{
if (isColorspaceSupported(VK_COLOR_SPACE_DISPLAY_P3_NONLINEAR_EXT))
{
outExtensions->glColorspaceDisplayP3 = true;
outExtensions->glColorspaceDisplayP3Passthrough = true;
}
outExtensions->glColorspaceDisplayP3Linear =
isColorspaceSupported(VK_COLOR_SPACE_DISPLAY_P3_LINEAR_EXT);
outExtensions->glColorspaceScrgb =
isColorspaceSupported(VK_COLOR_SPACE_EXTENDED_SRGB_NONLINEAR_EXT);
outExtensions->glColorspaceScrgbLinear =
isColorspaceSupported(VK_COLOR_SPACE_EXTENDED_SRGB_LINEAR_EXT);
}
}
void DisplayVk::generateCaps(egl::Caps *outCaps) const
{
outCaps->textureNPOT = true;
outCaps->stencil8 = getRenderer()->getNativeExtensions().textureStencil8OES;
}
const char *DisplayVk::getWSILayer() const
{
return nullptr;
}
bool DisplayVk::isUsingSwapchain() const
{
return true;
}
bool DisplayVk::getScratchBuffer(size_t requstedSizeBytes,
angle::MemoryBuffer **scratchBufferOut) const
{
return mScratchBuffer.get(requstedSizeBytes, scratchBufferOut);
}
void DisplayVk::handleError(VkResult result,
const char *file,
const char *function,
unsigned int line)
{
ASSERT(result != VK_SUCCESS);
mSavedError.errorCode = result;
mSavedError.file = file;
mSavedError.function = function;
mSavedError.line = line;
if (result == VK_ERROR_DEVICE_LOST)
{
WARN() << "Internal Vulkan error (" << result << "): " << VulkanResultString(result)
<< ", in " << file << ", " << function << ":" << line << ".";
mRenderer->notifyDeviceLost();
}
}
// TODO(jmadill): Remove this. http://anglebug.com/3041
egl::Error DisplayVk::getEGLError(EGLint errorCode)
{
std::stringstream errorStream;
errorStream << "Internal Vulkan error (" << mSavedError.errorCode
<< "): " << VulkanResultString(mSavedError.errorCode) << ", in " << mSavedError.file
<< ", " << mSavedError.function << ":" << mSavedError.line << ".";
std::string errorString = errorStream.str();
return egl::Error(errorCode, 0, std::move(errorString));
}
void DisplayVk::initializeFrontendFeatures(angle::FrontendFeatures *features) const
{
mRenderer->initializeFrontendFeatures(features);
}
void DisplayVk::populateFeatureList(angle::FeatureList *features)
{
mRenderer->getFeatures().populateFeatureList(features);
}
ShareGroupVk::ShareGroupVk() : mLastMonolithicPipelineJobTime(0), mOrphanNonEmptyBufferBlock(false)
{
mLastPruneTime = angle::GetCurrentSystemTime();
}
void ShareGroupVk::addContext(ContextVk *contextVk)
{
mContexts.insert(contextVk);
if (contextVk->getState().hasDisplayTextureShareGroup())
{
mOrphanNonEmptyBufferBlock = true;
}
}
void ShareGroupVk::removeContext(ContextVk *contextVk)
{
mContexts.erase(contextVk);
}
void ShareGroupVk::onDestroy(const egl::Display *display)
{
RendererVk *renderer = vk::GetImpl(display)->getRenderer();
for (std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
{
if (pool)
{
pool->destroy(renderer, mOrphanNonEmptyBufferBlock);
}
}
if (mSmallBufferPool)
{
mSmallBufferPool->destroy(renderer, mOrphanNonEmptyBufferBlock);
}
mPipelineLayoutCache.destroy(renderer);
mDescriptorSetLayoutCache.destroy(renderer);
mMetaDescriptorPools[DescriptorSetIndex::UniformsAndXfb].destroy(renderer);
mMetaDescriptorPools[DescriptorSetIndex::Texture].destroy(renderer);
mMetaDescriptorPools[DescriptorSetIndex::ShaderResource].destroy(renderer);
mFramebufferCache.destroy(renderer);
resetPrevTexture();
}
angle::Result ShareGroupVk::onMutableTextureUpload(ContextVk *contextVk, TextureVk *newTexture)
{
return mTextureUpload.onMutableTextureUpload(contextVk, newTexture);
}
void ShareGroupVk::onTextureRelease(TextureVk *textureVk)
{
mTextureUpload.onTextureRelease(textureVk);
}
angle::Result ShareGroupVk::scheduleMonolithicPipelineCreationTask(
ContextVk *contextVk,
vk::WaitableMonolithicPipelineCreationTask *taskOut)
{
ASSERT(contextVk->getFeatures().preferMonolithicPipelinesOverLibraries.enabled);
// Limit to a single task to avoid hogging all the cores.
if (mMonolithicPipelineCreationEvent && !mMonolithicPipelineCreationEvent->isReady())
{
return angle::Result::Continue;
}
// Additionally, rate limit the job postings.
double currentTime = angle::GetCurrentSystemTime();
if (currentTime - mLastMonolithicPipelineJobTime < kMonolithicPipelineJobPeriod)
{
return angle::Result::Continue;
}
mLastMonolithicPipelineJobTime = currentTime;
const vk::RenderPass *compatibleRenderPass = nullptr;
// Pull in a compatible RenderPass to be used by the task. This is done at the last minute,
// just before the task is scheduled, to minimize the time this reference to the render pass
// cache is held. If the render pass cache needs to be cleared, the main thread will wait for
// the job to complete.
ANGLE_TRY(contextVk->getCompatibleRenderPass(taskOut->getTask()->getRenderPassDesc(),
&compatibleRenderPass));
taskOut->setRenderPass(compatibleRenderPass);
egl::Display *display = contextVk->getRenderer()->getDisplay();
mMonolithicPipelineCreationEvent =
display->getMultiThreadPool()->postWorkerTask(taskOut->getTask());
taskOut->onSchedule(mMonolithicPipelineCreationEvent);
return angle::Result::Continue;
}
void ShareGroupVk::waitForCurrentMonolithicPipelineCreationTask()
{
if (mMonolithicPipelineCreationEvent)
{
mMonolithicPipelineCreationEvent->wait();
}
}
angle::Result TextureUpload::onMutableTextureUpload(ContextVk *contextVk, TextureVk *newTexture)
{
// This feature is currently disabled in the case of display-level texture sharing.
ASSERT(!contextVk->hasDisplayTextureShareGroup());
// If the previous texture is null, it should be set to the current texture. We also have to
// make sure that the previous texture pointer is still a mutable texture. Otherwise, we skip
// the optimization.
if (mPrevUploadedMutableTexture == nullptr || mPrevUploadedMutableTexture->isImmutable())
{
mPrevUploadedMutableTexture = newTexture;
return angle::Result::Continue;
}
// Skip the optimization if we have not switched to a new texture yet.
if (mPrevUploadedMutableTexture == newTexture)
{
return angle::Result::Continue;
}
// If the mutable texture is consistently specified, we initialize a full mip chain for it.
if (mPrevUploadedMutableTexture->isMutableTextureConsistentlySpecifiedForFlush())
{
ANGLE_TRY(mPrevUploadedMutableTexture->ensureImageInitialized(
contextVk, ImageMipLevels::FullMipChain));
contextVk->getPerfCounters().mutableTexturesUploaded++;
}
// Update the mutable texture pointer with the new pointer for the next potential flush.
mPrevUploadedMutableTexture = newTexture;
return angle::Result::Continue;
}
void TextureUpload::onTextureRelease(TextureVk *textureVk)
{
if (mPrevUploadedMutableTexture == textureVk)
{
resetPrevTexture();
}
}
// UpdateDescriptorSetsBuilder implementation.
UpdateDescriptorSetsBuilder::UpdateDescriptorSetsBuilder()
{
// Reserve reasonable amount of spaces so that for majority of apps we don't need to grow at all
mDescriptorBufferInfos.reserve(kDescriptorBufferInfosInitialSize);
mDescriptorImageInfos.reserve(kDescriptorImageInfosInitialSize);
mWriteDescriptorSets.reserve(kDescriptorWriteInfosInitialSize);
mBufferViews.reserve(kDescriptorBufferViewsInitialSize);
}
UpdateDescriptorSetsBuilder::~UpdateDescriptorSetsBuilder() = default;
template <typename T, const T *VkWriteDescriptorSet::*pInfo>
void UpdateDescriptorSetsBuilder::growDescriptorCapacity(std::vector<T> *descriptorVector,
size_t newSize)
{
const T *const oldInfoStart = descriptorVector->empty() ? nullptr : &(*descriptorVector)[0];
size_t newCapacity = std::max(descriptorVector->capacity() << 1, newSize);
descriptorVector->reserve(newCapacity);
if (oldInfoStart)
{
// patch mWriteInfo with new BufferInfo/ImageInfo pointers
for (VkWriteDescriptorSet &set : mWriteDescriptorSets)
{
if (set.*pInfo)
{
size_t index = set.*pInfo - oldInfoStart;
set.*pInfo = &(*descriptorVector)[index];
}
}
}
}
template <typename T, const T *VkWriteDescriptorSet::*pInfo>
T *UpdateDescriptorSetsBuilder::allocDescriptorInfos(std::vector<T> *descriptorVector, size_t count)
{
size_t oldSize = descriptorVector->size();
size_t newSize = oldSize + count;
if (newSize > descriptorVector->capacity())
{
// If we have reached capacity, grow the storage and patch the descriptor set with new
// buffer info pointer
growDescriptorCapacity<T, pInfo>(descriptorVector, newSize);
}
descriptorVector->resize(newSize);
return &(*descriptorVector)[oldSize];
}
VkDescriptorBufferInfo *UpdateDescriptorSetsBuilder::allocDescriptorBufferInfos(size_t count)
{
return allocDescriptorInfos<VkDescriptorBufferInfo, &VkWriteDescriptorSet::pBufferInfo>(
&mDescriptorBufferInfos, count);
}
VkDescriptorImageInfo *UpdateDescriptorSetsBuilder::allocDescriptorImageInfos(size_t count)
{
return allocDescriptorInfos<VkDescriptorImageInfo, &VkWriteDescriptorSet::pImageInfo>(
&mDescriptorImageInfos, count);
}
VkWriteDescriptorSet *UpdateDescriptorSetsBuilder::allocWriteDescriptorSets(size_t count)
{
size_t oldSize = mWriteDescriptorSets.size();
size_t newSize = oldSize + count;
mWriteDescriptorSets.resize(newSize);
return &mWriteDescriptorSets[oldSize];
}
VkBufferView *UpdateDescriptorSetsBuilder::allocBufferViews(size_t count)
{
return allocDescriptorInfos<VkBufferView, &VkWriteDescriptorSet::pTexelBufferView>(
&mBufferViews, count);
}
uint32_t UpdateDescriptorSetsBuilder::flushDescriptorSetUpdates(VkDevice device)
{
if (mWriteDescriptorSets.empty())
{
ASSERT(mDescriptorBufferInfos.empty());
ASSERT(mDescriptorImageInfos.empty());
return 0;
}
vkUpdateDescriptorSets(device, static_cast<uint32_t>(mWriteDescriptorSets.size()),
mWriteDescriptorSets.data(), 0, nullptr);
uint32_t retVal = static_cast<uint32_t>(mWriteDescriptorSets.size());
mWriteDescriptorSets.clear();
mDescriptorBufferInfos.clear();
mDescriptorImageInfos.clear();
mBufferViews.clear();
return retVal;
}
vk::BufferPool *ShareGroupVk::getDefaultBufferPool(RendererVk *renderer,
VkDeviceSize size,
uint32_t memoryTypeIndex)
{
if (size <= kMaxSizeToUseSmallBufferPool &&
memoryTypeIndex ==
renderer->getVertexConversionBufferMemoryTypeIndex(vk::MemoryHostVisibility::Visible))
{
if (!mSmallBufferPool)
{
const vk::Allocator &allocator = renderer->getAllocator();
VkBufferUsageFlags usageFlags = GetDefaultBufferUsageFlags(renderer);
VkMemoryPropertyFlags memoryPropertyFlags;
allocator.getMemoryTypeProperties(memoryTypeIndex, &memoryPropertyFlags);
std::unique_ptr<vk::BufferPool> pool = std::make_unique<vk::BufferPool>();
pool->initWithFlags(renderer, vma::VirtualBlockCreateFlagBits::BUDDY, usageFlags, 0,
memoryTypeIndex, memoryPropertyFlags);
mSmallBufferPool = std::move(pool);
}
return mSmallBufferPool.get();
}
else if (!mDefaultBufferPools[memoryTypeIndex])
{
const vk::Allocator &allocator = renderer->getAllocator();
VkBufferUsageFlags usageFlags = GetDefaultBufferUsageFlags(renderer);
VkMemoryPropertyFlags memoryPropertyFlags;
allocator.getMemoryTypeProperties(memoryTypeIndex, &memoryPropertyFlags);
std::unique_ptr<vk::BufferPool> pool = std::make_unique<vk::BufferPool>();
pool->initWithFlags(renderer, vma::VirtualBlockCreateFlagBits::GENERAL, usageFlags, 0,
memoryTypeIndex, memoryPropertyFlags);
mDefaultBufferPools[memoryTypeIndex] = std::move(pool);
}
return mDefaultBufferPools[memoryTypeIndex].get();
}
void ShareGroupVk::pruneDefaultBufferPools(RendererVk *renderer)
{
mLastPruneTime = angle::GetCurrentSystemTime();
// Bail out if no suballocation have been destroyed since last prune.
if (renderer->getSuballocationDestroyedSize() == 0)
{
return;
}
for (std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
{
if (pool)
{
pool->pruneEmptyBuffers(renderer);
}
}
if (mSmallBufferPool)
{
mSmallBufferPool->pruneEmptyBuffers(renderer);
}
renderer->onBufferPoolPrune();
#if ANGLE_ENABLE_BUFFER_POOL_STATS_LOGGING
logBufferPools();
#endif
}
bool ShareGroupVk::isDueForBufferPoolPrune(RendererVk *renderer)
{
// Ensure we periodically prune to maintain the heuristic information
double timeElapsed = angle::GetCurrentSystemTime() - mLastPruneTime;
if (timeElapsed > kTimeElapsedForPruneDefaultBufferPool)
{
return true;
}
// If we have destroyed a lot of memory, also prune to ensure memory gets freed as soon as
// possible
if (renderer->getSuballocationDestroyedSize() >= kMaxTotalEmptyBufferBytes)
{
return true;
}
return false;
}
void ShareGroupVk::calculateTotalBufferCount(size_t *bufferCount, VkDeviceSize *totalSize) const
{
*bufferCount = 0;
*totalSize = 0;
for (const std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
{
if (pool)
{
*bufferCount += pool->getBufferCount();
*totalSize += pool->getMemorySize();
}
}
if (mSmallBufferPool)
{
*bufferCount += mSmallBufferPool->getBufferCount();
*totalSize += mSmallBufferPool->getMemorySize();
}
}
void ShareGroupVk::logBufferPools() const
{
size_t totalBufferCount;
VkDeviceSize totalMemorySize;
calculateTotalBufferCount(&totalBufferCount, &totalMemorySize);
INFO() << "BufferBlocks count:" << totalBufferCount << " memorySize:" << totalMemorySize / 1024
<< " UnusedBytes/memorySize (KBs):";
for (const std::unique_ptr<vk::BufferPool> &pool : mDefaultBufferPools)
{
if (pool && pool->getBufferCount() > 0)
{
std::ostringstream log;
pool->addStats(&log);
INFO() << "\t" << log.str();
}
}
if (mSmallBufferPool && mSmallBufferPool->getBufferCount() > 0)
{
std::ostringstream log;
mSmallBufferPool->addStats(&log);
INFO() << "\t" << log.str();
}
}
} // namespace rx