Hash :
d7aa0130
Author :
Date :
2021-04-26T16:56:15
Upstream Apple's direct-to-Metal backend: compile translator. This change is meant to merge the translator changes from Apple's direct-to-Metal backend. Taken from Kyle Piddington's CL: https://chromium-review.googlesource.com/c/angle/angle/+/2857366/ The goal of this CL is to merge the translator code in a state that compiles, but not to switch the Metal backend over to use this translator backend yet. Bug: angleproject:5505 Change-Id: I68a6354604498cd5fd1eb96c13fc56f3b38f2bd0 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/2897536 Reviewed-by: Jonah Ryan-Davis <jonahr@google.com> Commit-Queue: Jonah Ryan-Davis <jonahr@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
//
// Copyright 2019 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// DisplayMtl.mm: Metal implementation of DisplayImpl
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "gpu_info_util/SystemInfo.h"
#include "libANGLE/Context.h"
#include "libANGLE/Display.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/driver_utils.h"
#include "libANGLE/renderer/glslang_wrapper_utils.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DeviceMtl.h"
#include "libANGLE/renderer/metal/ImageMtl.h"
#include "libANGLE/renderer/metal/SurfaceMtl.h"
#include "libANGLE/renderer/metal/SyncMtl.h"
#include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/shaders/mtl_default_shaders_src_autogen.inc"
#include "libANGLE/trace.h"
#include "platform/Platform.h"
#include "EGL/eglext.h"
namespace rx
{
bool IsMetalDisplayAvailable()
{
// We only support macos 10.13+ and 11 for now. Since they are requirements for Metal 2.0.
#if TARGET_OS_SIMULATOR
if (ANGLE_APPLE_AVAILABLE_XCI(10.13, 13.0, 13))
#else
if (ANGLE_APPLE_AVAILABLE_XCI(10.13, 13.0, 11))
#endif
{
return true;
}
return false;
}
DisplayImpl *CreateMetalDisplay(const egl::DisplayState &state)
{
return new DisplayMtl(state);
}
struct DefaultShaderAsyncInfoMtl
{
mtl::AutoObjCPtr<id<MTLLibrary>> defaultShaders;
mtl::AutoObjCPtr<NSError *> defaultShadersCompileError;
// Synchronization primitives for compiling default shaders in back-ground
std::condition_variable cv;
std::mutex lock;
bool compiled = false;
};
// DisplayMtl implementation
DisplayMtl::DisplayMtl(const egl::DisplayState &state) : DisplayImpl(state), mUtils(this) {}
DisplayMtl::~DisplayMtl() {}
egl::Error DisplayMtl::initialize(egl::Display *display)
{
ASSERT(IsMetalDisplayAvailable());
angle::Result result = initializeImpl(display);
if (result != angle::Result::Continue)
{
return egl::EglNotInitialized();
}
return egl::NoError();
}
angle::Result DisplayMtl::initializeImpl(egl::Display *display)
{
ANGLE_MTL_OBJC_SCOPE
{
mMetalDevice = [MTLCreateSystemDefaultDevice() ANGLE_MTL_AUTORELEASE];
if (!mMetalDevice)
{
return angle::Result::Stop;
}
mMetalDeviceVendorId = mtl::GetDeviceVendorId(mMetalDevice);
mCmdQueue.set([[mMetalDevice.get() newCommandQueue] ANGLE_MTL_AUTORELEASE]);
mCapsInitialized = false;
{
ANGLE_TRACE_EVENT0("gpu.angle,startup", "GlslangWarmup");
sh::InitializeGlslang();
}
if (!mState.featuresAllDisabled)
{
initializeFeatures();
}
ANGLE_TRY(mFormatTable.initialize(this));
ANGLE_TRY(initializeShaderLibrary());
return mUtils.initialize();
}
}
void DisplayMtl::terminate()
{
mUtils.onDestroy();
mCmdQueue.reset();
mDefaultShadersAsyncInfo = nullptr;
mMetalDevice = nil;
#if ANGLE_MTL_EVENT_AVAILABLE
mSharedEventListener = nil;
#endif
mCapsInitialized = false;
mMetalDeviceVendorId = 0;
sh::FinalizeGlslang();
}
bool DisplayMtl::testDeviceLost()
{
return false;
}
egl::Error DisplayMtl::restoreLostDevice(const egl::Display *display)
{
return egl::NoError();
}
std::string DisplayMtl::getRendererDescription()
{
ANGLE_MTL_OBJC_SCOPE
{
std::string desc = "ANGLE Metal Renderer";
if (mMetalDevice)
{
desc += ": ";
desc += mMetalDevice.get().name.UTF8String;
}
return desc;
}
}
std::string DisplayMtl::getVendorString()
{
return GetVendorString(mMetalDeviceVendorId);
}
std::string DisplayMtl::getVersionString()
{
ANGLE_MTL_OBJC_SCOPE
{
NSProcessInfo *procInfo = [NSProcessInfo processInfo];
return procInfo.operatingSystemVersionString.UTF8String;
}
}
DeviceImpl *DisplayMtl::createDevice()
{
return new DeviceMtl();
}
egl::Error DisplayMtl::waitClient(const gl::Context *context)
{
auto contextMtl = GetImplAs<ContextMtl>(context);
angle::Result result = contextMtl->finishCommandBuffer();
if (result != angle::Result::Continue)
{
return egl::EglBadAccess();
}
return egl::NoError();
}
egl::Error DisplayMtl::waitNative(const gl::Context *context, EGLint engine)
{
UNIMPLEMENTED();
return egl::NoError();
}
SurfaceImpl *DisplayMtl::createWindowSurface(const egl::SurfaceState &state,
EGLNativeWindowType window,
const egl::AttributeMap &attribs)
{
return new WindowSurfaceMtl(this, state, window, attribs);
}
SurfaceImpl *DisplayMtl::createPbufferSurface(const egl::SurfaceState &state,
const egl::AttributeMap &attribs)
{
return new PBufferSurfaceMtl(this, state, attribs);
}
SurfaceImpl *DisplayMtl::createPbufferFromClientBuffer(const egl::SurfaceState &state,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs)
{
switch (buftype)
{
case EGL_IOSURFACE_ANGLE:
return new IOSurfaceSurfaceMtl(this, state, clientBuffer, attribs);
break;
default:
UNREACHABLE();
}
return nullptr;
}
SurfaceImpl *DisplayMtl::createPixmapSurface(const egl::SurfaceState &state,
NativePixmapType nativePixmap,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return static_cast<SurfaceImpl *>(0);
}
ImageImpl *DisplayMtl::createImage(const egl::ImageState &state,
const gl::Context *context,
EGLenum target,
const egl::AttributeMap &attribs)
{
return new ImageMtl(state, context);
}
rx::ContextImpl *DisplayMtl::createContext(const gl::State &state,
gl::ErrorSet *errorSet,
const egl::Config *configuration,
const gl::Context *shareContext,
const egl::AttributeMap &attribs)
{
return new ContextMtl(state, errorSet, this);
}
StreamProducerImpl *DisplayMtl::createStreamProducerD3DTexture(
egl::Stream::ConsumerType consumerType,
const egl::AttributeMap &attribs)
{
UNIMPLEMENTED();
return nullptr;
}
ShareGroupImpl *DisplayMtl::createShareGroup()
{
return new ShareGroupMtl();
}
ExternalImageSiblingImpl *DisplayMtl::createExternalImageSibling(const gl::Context *context,
EGLenum target,
EGLClientBuffer buffer,
const egl::AttributeMap &attribs)
{
switch (target)
{
case EGL_METAL_TEXTURE_ANGLE:
return new TextureImageSiblingMtl(buffer);
default:
UNREACHABLE();
return nullptr;
}
}
gl::Version DisplayMtl::getMaxSupportedESVersion() const
{
// NOTE(hqle): Supports GLES 3.0 on iOS GPU family 4+ for now.
if (supportsEitherGPUFamily(4, 1))
{
return mtl::kMaxSupportedGLVersion;
}
return gl::Version(2, 0);
}
gl::Version DisplayMtl::getMaxConformantESVersion() const
{
return std::min(getMaxSupportedESVersion(), gl::Version(2, 0));
}
EGLSyncImpl *DisplayMtl::createSync(const egl::AttributeMap &attribs)
{
return new EGLSyncMtl(attribs);
}
egl::Error DisplayMtl::makeCurrent(egl::Display *display,
egl::Surface *drawSurface,
egl::Surface *readSurface,
gl::Context *context)
{
if (!context)
{
return egl::NoError();
}
return egl::NoError();
}
void DisplayMtl::generateExtensions(egl::DisplayExtensions *outExtensions) const
{
outExtensions->flexibleSurfaceCompatibility = true;
outExtensions->iosurfaceClientBuffer = true;
outExtensions->surfacelessContext = true;
outExtensions->displayTextureShareGroup = true;
outExtensions->displaySemaphoreShareGroup = true;
outExtensions->mtlTextureClientBuffer = true;
if (mFeatures.hasEvents.enabled)
{
// MTLSharedEvent is only available since Metal 2.1
outExtensions->fenceSync = true;
outExtensions->waitSync = true;
}
// Note that robust resource initialization is not yet implemented. We only expose
// this extension so that ANGLE can be initialized in Chrome. WebGL will fail to use
// this extension (anglebug.com/4929)
outExtensions->robustResourceInitialization = true;
// EGL_KHR_image
outExtensions->image = true;
outExtensions->imageBase = true;
}
void DisplayMtl::generateCaps(egl::Caps *outCaps) const {}
void DisplayMtl::populateFeatureList(angle::FeatureList *features) {}
egl::ConfigSet DisplayMtl::generateConfigs()
{
// NOTE(hqle): generate more config permutations
egl::ConfigSet configs;
const gl::Version &maxVersion = getMaxSupportedESVersion();
ASSERT(maxVersion >= gl::Version(2, 0));
bool supportsES3 = maxVersion >= gl::Version(3, 0);
egl::Config config;
// Native stuff
config.nativeVisualID = 0;
config.nativeVisualType = 0;
config.nativeRenderable = EGL_TRUE;
config.colorBufferType = EGL_RGB_BUFFER;
config.luminanceSize = 0;
config.alphaMaskSize = 0;
config.transparentType = EGL_NONE;
// Pbuffer
config.bindToTextureTarget = EGL_TEXTURE_2D;
config.maxPBufferWidth = 4096;
config.maxPBufferHeight = 4096;
config.maxPBufferPixels = 4096 * 4096;
// Caveat
config.configCaveat = EGL_NONE;
// Misc
config.sampleBuffers = 0;
config.samples = 0;
config.level = 0;
config.bindToTextureRGB = EGL_FALSE;
config.bindToTextureRGBA = EGL_TRUE;
config.surfaceType = EGL_WINDOW_BIT | EGL_PBUFFER_BIT;
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
config.minSwapInterval = 0;
config.maxSwapInterval = 1;
#else
config.minSwapInterval = 1;
config.maxSwapInterval = 1;
#endif
config.renderTargetFormat = GL_RGBA8;
config.depthStencilFormat = GL_DEPTH24_STENCIL8;
config.conformant = EGL_OPENGL_ES2_BIT | (supportsES3 ? EGL_OPENGL_ES3_BIT_KHR : 0);
config.renderableType = config.conformant;
config.matchNativePixmap = EGL_NONE;
config.colorComponentType = EGL_COLOR_COMPONENT_TYPE_FIXED_EXT;
constexpr int samplesSupported[] = {0, 4};
for (int samples : samplesSupported)
{
config.samples = samples;
config.sampleBuffers = (samples == 0) ? 0 : 1;
// Buffer sizes
config.redSize = 8;
config.greenSize = 8;
config.blueSize = 8;
config.alphaSize = 8;
config.bufferSize = config.redSize + config.greenSize + config.blueSize + config.alphaSize;
// With DS
config.depthSize = 24;
config.stencilSize = 8;
configs.add(config);
// With D
config.depthSize = 24;
config.stencilSize = 0;
configs.add(config);
// With S
config.depthSize = 0;
config.stencilSize = 8;
configs.add(config);
// No DS
config.depthSize = 0;
config.stencilSize = 0;
configs.add(config);
}
return configs;
}
bool DisplayMtl::isValidNativeWindow(EGLNativeWindowType window) const
{
ANGLE_MTL_OBJC_SCOPE
{
NSObject *layer = (__bridge NSObject *)(window);
return [layer isKindOfClass:[CALayer class]];
}
}
egl::Error DisplayMtl::validateClientBuffer(const egl::Config *configuration,
EGLenum buftype,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
switch (buftype)
{
case EGL_IOSURFACE_ANGLE:
if (!IOSurfaceSurfaceMtl::ValidateAttributes(clientBuffer, attribs))
{
return egl::EglBadAttribute();
}
break;
default:
UNREACHABLE();
return egl::EglBadAttribute();
}
return egl::NoError();
}
egl::Error DisplayMtl::validateImageClientBuffer(const gl::Context *context,
EGLenum target,
EGLClientBuffer clientBuffer,
const egl::AttributeMap &attribs) const
{
switch (target)
{
case EGL_METAL_TEXTURE_ANGLE:
if (!TextureImageSiblingMtl::ValidateClientBuffer(this, clientBuffer))
{
return egl::EglBadAttribute();
}
break;
default:
UNREACHABLE();
return egl::EglBadAttribute();
}
return egl::NoError();
}
gl::Caps DisplayMtl::getNativeCaps() const
{
ensureCapsInitialized();
return mNativeCaps;
}
const gl::TextureCapsMap &DisplayMtl::getNativeTextureCaps() const
{
ensureCapsInitialized();
return mNativeTextureCaps;
}
const gl::Extensions &DisplayMtl::getNativeExtensions() const
{
ensureCapsInitialized();
return mNativeExtensions;
}
const gl::Limitations &DisplayMtl::getNativeLimitations() const
{
ensureCapsInitialized();
return mNativeLimitations;
}
void DisplayMtl::ensureCapsInitialized() const
{
if (mCapsInitialized)
{
return;
}
mCapsInitialized = true;
// Reset
mNativeCaps = gl::Caps();
// Fill extension and texture caps
initializeExtensions();
initializeTextureCaps();
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
mNativeCaps.maxElementIndex = std::numeric_limits<GLuint>::max() - 1;
mNativeCaps.max3DTextureSize = 2048;
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
mNativeCaps.max2DTextureSize = 16384;
// On macOS exclude [[position]] from maxVaryingVectors.
mNativeCaps.maxVaryingVectors = 31 - 1;
mNativeCaps.maxVertexOutputComponents = mNativeCaps.maxFragmentInputComponents = 124 - 4;
#else
if (supportsAppleGPUFamily(3))
{
mNativeCaps.max2DTextureSize = 16384;
mNativeCaps.maxVertexOutputComponents = mNativeCaps.maxFragmentInputComponents = 124;
mNativeCaps.maxVaryingVectors = mNativeCaps.maxVertexOutputComponents / 4;
}
else
{
mNativeCaps.max2DTextureSize = 8192;
mNativeCaps.maxVertexOutputComponents = mNativeCaps.maxFragmentInputComponents = 60;
mNativeCaps.maxVaryingVectors = mNativeCaps.maxVertexOutputComponents / 4;
}
#endif
mNativeCaps.maxArrayTextureLayers = 2048;
mNativeCaps.maxLODBias = 2.0; // default GLES3 limit
mNativeCaps.maxCubeMapTextureSize = mNativeCaps.max2DTextureSize;
mNativeCaps.maxRenderbufferSize = mNativeCaps.max2DTextureSize;
mNativeCaps.minAliasedPointSize = 1;
// NOTE(hqle): Metal has some problems drawing big point size even though
// Metal-Feature-Set-Tables.pdf says that max supported point size is 511. We limit it to 64 for
// now.
// http://anglebug.com/4816
mNativeCaps.maxAliasedPointSize = 64;
mNativeCaps.minAliasedLineWidth = 1.0f;
mNativeCaps.maxAliasedLineWidth = 1.0f;
mNativeCaps.maxDrawBuffers = mtl::kMaxRenderTargets;
mNativeCaps.maxFramebufferWidth = mNativeCaps.max2DTextureSize;
mNativeCaps.maxFramebufferHeight = mNativeCaps.max2DTextureSize;
mNativeCaps.maxColorAttachments = mtl::kMaxRenderTargets;
mNativeCaps.maxViewportWidth = mNativeCaps.max2DTextureSize;
mNativeCaps.maxViewportHeight = mNativeCaps.max2DTextureSize;
// MSAA
mNativeCaps.maxSamples = mFormatTable.getMaxSamples();
mNativeCaps.maxSampleMaskWords = 0;
mNativeCaps.maxColorTextureSamples = mNativeCaps.maxSamples;
mNativeCaps.maxDepthTextureSamples = mNativeCaps.maxSamples;
mNativeCaps.maxIntegerSamples = 1;
mNativeCaps.maxVertexAttributes = mtl::kMaxVertexAttribs;
mNativeCaps.maxVertexAttribBindings = mtl::kMaxVertexAttribs;
mNativeCaps.maxVertexAttribRelativeOffset = std::numeric_limits<GLint>::max();
mNativeCaps.maxVertexAttribStride = std::numeric_limits<GLint>::max();
// glGet() use signed integer as parameter so we have to use GLint's max here, not GLuint.
mNativeCaps.maxElementsIndices = std::numeric_limits<GLint>::max();
mNativeCaps.maxElementsVertices = std::numeric_limits<GLint>::max();
// Looks like all floats are IEEE according to the docs here:
mNativeCaps.vertexHighpFloat.setIEEEFloat();
mNativeCaps.vertexMediumpFloat.setIEEEFloat();
mNativeCaps.vertexLowpFloat.setIEEEFloat();
mNativeCaps.fragmentHighpFloat.setIEEEFloat();
mNativeCaps.fragmentMediumpFloat.setIEEEFloat();
mNativeCaps.fragmentLowpFloat.setIEEEFloat();
mNativeCaps.vertexHighpInt.setTwosComplementInt(32);
mNativeCaps.vertexMediumpInt.setTwosComplementInt(32);
mNativeCaps.vertexLowpInt.setTwosComplementInt(32);
mNativeCaps.fragmentHighpInt.setTwosComplementInt(32);
mNativeCaps.fragmentMediumpInt.setTwosComplementInt(32);
mNativeCaps.fragmentLowpInt.setTwosComplementInt(32);
GLuint maxDefaultUniformVectors = mtl::kDefaultUniformsMaxSize / (sizeof(GLfloat) * 4);
const GLuint maxDefaultUniformComponents = maxDefaultUniformVectors * 4;
// Uniforms are implemented using a uniform buffer, so the max number of uniforms we can
// support is the max buffer range divided by the size of a single uniform (4X float).
mNativeCaps.maxVertexUniformVectors = maxDefaultUniformVectors;
mNativeCaps.maxShaderUniformComponents[gl::ShaderType::Vertex] = maxDefaultUniformComponents;
mNativeCaps.maxFragmentUniformVectors = maxDefaultUniformVectors;
mNativeCaps.maxShaderUniformComponents[gl::ShaderType::Fragment] = maxDefaultUniformComponents;
mNativeCaps.maxShaderUniformBlocks[gl::ShaderType::Vertex] = mtl::kMaxShaderUBOs;
mNativeCaps.maxShaderUniformBlocks[gl::ShaderType::Fragment] = mtl::kMaxShaderUBOs;
mNativeCaps.maxCombinedUniformBlocks = mtl::kMaxGLUBOBindings;
// Note that we currently implement textures as combined image+samplers, so the limit is
// the minimum of supported samplers and sampled images.
mNativeCaps.maxCombinedTextureImageUnits = mtl::kMaxGLSamplerBindings;
mNativeCaps.maxShaderTextureImageUnits[gl::ShaderType::Fragment] = mtl::kMaxShaderSamplers;
mNativeCaps.maxShaderTextureImageUnits[gl::ShaderType::Vertex] = mtl::kMaxShaderSamplers;
// No info from Metal given, use default GLES3 spec values:
mNativeCaps.minProgramTexelOffset = -8;
mNativeCaps.maxProgramTexelOffset = 7;
// NOTE(hqle): support storage buffer.
const uint32_t maxPerStageStorageBuffers = 0;
mNativeCaps.maxShaderStorageBlocks[gl::ShaderType::Vertex] = maxPerStageStorageBuffers;
mNativeCaps.maxShaderStorageBlocks[gl::ShaderType::Fragment] = maxPerStageStorageBuffers;
mNativeCaps.maxCombinedShaderStorageBlocks = maxPerStageStorageBuffers;
// Fill in additional limits for UBOs and SSBOs.
mNativeCaps.maxUniformBufferBindings = mNativeCaps.maxCombinedUniformBlocks;
mNativeCaps.maxUniformBlockSize = mtl::kMaxUBOSize; // Default according to GLES 3.0 spec.
mNativeCaps.uniformBufferOffsetAlignment = 1;
mNativeCaps.maxShaderStorageBufferBindings = 0;
mNativeCaps.maxShaderStorageBlockSize = 0;
mNativeCaps.shaderStorageBufferOffsetAlignment = 0;
// UBO plus default uniform limits
const uint32_t maxCombinedUniformComponents =
maxDefaultUniformComponents + mtl::kMaxUBOSize * mtl::kMaxShaderUBOs / 4;
for (gl::ShaderType shaderType : gl::kAllGraphicsShaderTypes)
{
mNativeCaps.maxCombinedShaderUniformComponents[shaderType] = maxCombinedUniformComponents;
}
mNativeCaps.maxCombinedShaderOutputResources = 0;
mNativeCaps.maxTransformFeedbackInterleavedComponents =
gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_INTERLEAVED_COMPONENTS;
mNativeCaps.maxTransformFeedbackSeparateAttributes =
gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_SEPARATE_ATTRIBS;
mNativeCaps.maxTransformFeedbackSeparateComponents =
gl::IMPLEMENTATION_MAX_TRANSFORM_FEEDBACK_SEPARATE_COMPONENTS;
// GL_APPLE_clip_distance
mNativeCaps.maxClipDistances = 8;
// Metal doesn't support GL_TEXTURE_COMPARE_MODE=GL_NONE for shadow samplers
mNativeLimitations.noShadowSamplerCompareModeNone = true;
// Apple platforms require PVRTC1 textures to be squares.
mNativeLimitations.squarePvrtc1 = true;
// Direct-to-metal constants:
mNativeCaps.driverUniformsBindingIndex = mtl::kDriverUniformsBindingIndex;
mNativeCaps.defaultUniformsBindingIndex = mtl::kDefaultUniformsBindingIndex;
mNativeCaps.UBOArgumentBufferBindingIndex = mtl::kUBOArgumentBufferBindingIndex;
}
void DisplayMtl::initializeExtensions() const
{
// Reset
mNativeExtensions = gl::Extensions();
// Enable this for simple buffer readback testing, but some functionality is missing.
// NOTE(hqle): Support full mapBufferRange extension.
mNativeExtensions.mapBufferOES = true;
mNativeExtensions.mapBufferRange = true;
mNativeExtensions.textureStorage = true;
mNativeExtensions.drawBuffers = true;
mNativeExtensions.drawBuffersIndexedEXT = true;
mNativeExtensions.drawBuffersIndexedOES = true;
mNativeExtensions.fragDepth = true;
mNativeExtensions.framebufferBlitANGLE = true;
mNativeExtensions.framebufferMultisample = true;
mNativeExtensions.copyTexture = true;
mNativeExtensions.copyCompressedTexture = false;
// EXT_debug_marker is not implemented yet, but the entry points must be exposed for the Metal
// backend to be used in Chrome (http://anglebug.com/4946)
mNativeExtensions.debugMarker = true;
mNativeExtensions.robustness = true;
mNativeExtensions.textureBorderClampOES = false; // not implemented yet
mNativeExtensions.discardFramebuffer = true;
// Enable EXT_blend_minmax
mNativeExtensions.blendMinMax = true;
mNativeExtensions.eglImageOES = true;
mNativeExtensions.eglImageExternalOES = false;
// NOTE(hqle): Support GL_OES_EGL_image_external_essl3.
mNativeExtensions.eglImageExternalEssl3OES = false;
mNativeExtensions.memoryObject = false;
mNativeExtensions.memoryObjectFd = false;
mNativeExtensions.semaphore = false;
mNativeExtensions.semaphoreFd = false;
mNativeExtensions.instancedArraysANGLE = mFeatures.hasBaseVertexInstancedDraw.enabled;
mNativeExtensions.instancedArraysEXT = mNativeExtensions.instancedArraysANGLE;
mNativeExtensions.robustBufferAccessBehavior = false;
mNativeExtensions.eglSyncOES = false;
mNativeExtensions.occlusionQueryBoolean = true;
mNativeExtensions.disjointTimerQuery = false;
mNativeExtensions.queryCounterBitsTimeElapsed = false;
mNativeExtensions.queryCounterBitsTimestamp = false;
mNativeExtensions.textureFilterAnisotropic = true;
mNativeExtensions.maxTextureAnisotropy = 16;
mNativeExtensions.textureNPOTOES = true;
mNativeExtensions.texture3DOES = true;
mNativeExtensions.standardDerivativesOES = true;
mNativeExtensions.elementIndexUintOES = true;
// GL_APPLE_clip_distance
mNativeExtensions.clipDistanceAPPLE = true;
// GL_NV_pixel_buffer_object
mNativeExtensions.pixelBufferObjectNV = true;
if (mFeatures.hasEvents.enabled)
{
// MTLSharedEvent is only available since Metal 2.1
// GL_NV_fence
mNativeExtensions.fenceNV = true;
// GL_OES_EGL_sync
mNativeExtensions.eglSyncOES = true;
}
}
void DisplayMtl::initializeTextureCaps() const
{
mNativeTextureCaps.clear();
mFormatTable.generateTextureCaps(this, &mNativeTextureCaps,
&mNativeCaps.compressedTextureFormats);
// Re-verify texture extensions.
mNativeExtensions.setTextureExtensionSupport(mNativeTextureCaps);
// When ETC2/EAC formats are natively supported, enable ANGLE-specific extension string to
// expose them to WebGL. In other case, mark potentially-available ETC1 extension as emulated.
if (supportsAppleGPUFamily(1) && gl::DetermineCompressedTextureETCSupport(mNativeTextureCaps))
{
mNativeExtensions.compressedTextureETC = true;
}
else
{
mNativeLimitations.emulatedEtc1 = true;
}
// Enable ASTC sliced 3D, requires MTLGPUFamilyApple3
if (supportsAppleGPUFamily(3) && mNativeExtensions.textureCompressionASTCLDRKHR)
{
mNativeExtensions.textureCompressionSliced3dASTCKHR = true;
}
// Enable ASTC HDR, requires MTLGPUFamilyApple6
if (supportsAppleGPUFamily(6) && mNativeExtensions.textureCompressionASTCLDRKHR)
{
mNativeExtensions.textureCompressionASTCHDRKHR = true;
}
// Disable all depth buffer and stencil buffer readback extensions until we need them
mNativeExtensions.readDepthNV = false;
mNativeExtensions.readStencilNV = false;
mNativeExtensions.depthBufferFloat2NV = false;
}
void DisplayMtl::initializeFeatures()
{
bool isMetal2_1 = false;
bool isMetal2_2 = false;
if (ANGLE_APPLE_AVAILABLE_XCI(10.14, 13.0, 12.0))
{
isMetal2_1 = true;
}
if (ANGLE_APPLE_AVAILABLE_XCI(10.15, 13.0, 13.0))
{
isMetal2_2 = true;
}
bool isOSX = TARGET_OS_OSX;
bool isCatalyst = TARGET_OS_MACCATALYST;
bool isSimulator = TARGET_OS_SIMULATOR;
bool isARM = ANGLE_MTL_ARM;
ANGLE_FEATURE_CONDITION((&mFeatures), allowGenMultipleMipsPerPass, true);
ANGLE_FEATURE_CONDITION((&mFeatures), forceBufferGPUStorage, false);
ANGLE_FEATURE_CONDITION((&mFeatures), hasDepthTextureFiltering,
(isOSX || isCatalyst) && !isARM);
ANGLE_FEATURE_CONDITION((&mFeatures), hasExplicitMemBarrier,
isMetal2_1 && (isOSX || isCatalyst) && !isARM);
ANGLE_FEATURE_CONDITION((&mFeatures), hasDepthAutoResolve, supportsEitherGPUFamily(3, 2));
ANGLE_FEATURE_CONDITION((&mFeatures), hasStencilAutoResolve, supportsEitherGPUFamily(5, 2));
ANGLE_FEATURE_CONDITION((&mFeatures), allowMultisampleStoreAndResolve,
supportsEitherGPUFamily(3, 1));
// http://anglebug.com/4919
// Stencil blit shader is not compiled on Intel & NVIDIA, need investigation.
ANGLE_FEATURE_CONDITION((&mFeatures), hasStencilOutput,
isMetal2_1 && !isIntel() && !isNVIDIA());
ANGLE_FEATURE_CONDITION((&mFeatures), hasTextureSwizzle,
isMetal2_2 && supportsEitherGPUFamily(1, 2));
// http://crbug.com/1136673
// Fence sync is flaky on Nvidia
ANGLE_FEATURE_CONDITION((&mFeatures), hasEvents, isMetal2_1 && !isNVIDIA());
ANGLE_FEATURE_CONDITION((&mFeatures), hasCheapRenderPass, (isOSX || isCatalyst) && !isARM);
// http://anglebug.com/5235
// D24S8 is unreliable on AMD.
ANGLE_FEATURE_CONDITION((&mFeatures), forceD24S8AsUnsupported, isAMD());
// Base Vertex drawing is only supported since GPU family 3.
ANGLE_FEATURE_CONDITION((&mFeatures), hasBaseVertexInstancedDraw,
isOSX || isCatalyst || supportsAppleGPUFamily(3));
ANGLE_FEATURE_CONDITION((&mFeatures), hasNonUniformDispatch,
isOSX || isCatalyst || supportsAppleGPUFamily(4));
ANGLE_FEATURE_CONDITION((&mFeatures), allowSeparatedDepthStencilBuffers,
!isOSX && !isCatalyst && !isSimulator);
angle::PlatformMethods *platform = ANGLEPlatformCurrent();
platform->overrideFeaturesMtl(platform, &mFeatures);
ApplyFeatureOverrides(&mFeatures, getState());
}
angle::Result DisplayMtl::initializeShaderLibrary()
{
mDefaultShadersAsyncInfo.reset(new DefaultShaderAsyncInfoMtl);
// Create references to async info struct since it might be released in terminate(), but the
// callback might still not be fired yet.
std::shared_ptr<DefaultShaderAsyncInfoMtl> asyncRef = mDefaultShadersAsyncInfo;
// Compile the default shaders asynchronously
ANGLE_MTL_OBJC_SCOPE
{
auto nsSource = [[NSString alloc] initWithBytesNoCopy:gDefaultMetallibSrc
length:sizeof(gDefaultMetallibSrc)
encoding:NSUTF8StringEncoding
freeWhenDone:NO];
auto options = [[[MTLCompileOptions alloc] init] ANGLE_MTL_AUTORELEASE];
[getMetalDevice() newLibraryWithSource:nsSource
options:options
completionHandler:^(id<MTLLibrary> library, NSError *error) {
std::unique_lock<std::mutex> lg(asyncRef->lock);
asyncRef->defaultShaders = std::move(library);
asyncRef->defaultShadersCompileError = std::move(error);
asyncRef->compiled = true;
asyncRef->cv.notify_one();
}];
[nsSource ANGLE_MTL_AUTORELEASE];
}
return angle::Result::Continue;
}
id<MTLLibrary> DisplayMtl::getDefaultShadersLib()
{
std::unique_lock<std::mutex> lg(mDefaultShadersAsyncInfo->lock);
if (!mDefaultShadersAsyncInfo->compiled)
{
// Wait for async compilation
mDefaultShadersAsyncInfo->cv.wait(lg,
[this] { return mDefaultShadersAsyncInfo->compiled; });
}
if (mDefaultShadersAsyncInfo->defaultShadersCompileError &&
!mDefaultShadersAsyncInfo->defaultShaders)
{
ANGLE_MTL_OBJC_SCOPE
{
ERR() << "Internal error: "
<< mDefaultShadersAsyncInfo->defaultShadersCompileError.get()
.localizedDescription.UTF8String;
}
// This is not supposed to happen
UNREACHABLE();
}
return mDefaultShadersAsyncInfo->defaultShaders;
}
bool DisplayMtl::supportsAppleGPUFamily(uint8_t iOSFamily) const
{
#if TARGET_OS_MACCATALYST
return false;
#else
# if (__IPHONE_OS_VERSION_MAX_ALLOWED >= 130000) || (__TV_OS_VERSION_MAX_ALLOWED >= 130000) || \
(__MAC_OS_X_VERSION_MAX_ALLOWED >= 101500)
// If device supports [MTLDevice supportsFamily:], then use it.
if (ANGLE_APPLE_AVAILABLE_XI(10.15, 13.0))
{
MTLGPUFamily family;
switch (iOSFamily)
{
case 1:
family = MTLGPUFamilyApple1;
break;
case 2:
family = MTLGPUFamilyApple2;
break;
case 3:
family = MTLGPUFamilyApple3;
break;
case 4:
family = MTLGPUFamilyApple4;
break;
case 5:
family = MTLGPUFamilyApple5;
break;
# if TARGET_OS_IOS || (TARGET_OS_OSX && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 101600))
case 6:
family = MTLGPUFamilyApple6;
break;
# endif
# if (TARGET_OS_IOS && (__IPHONE_OS_VERSION_MAX_ALLOWED >= 140000)) || \
(TARGET_OS_OSX && (__MAC_OS_X_VERSION_MAX_ALLOWED >= 101600))
case 7:
family = MTLGPUFamilyApple7;
break;
# endif
default:
return false;
}
return [getMetalDevice() supportsFamily:family];
} // Metal 2.2
# endif // __IPHONE_OS_VERSION_MAX_ALLOWED
// If device doesn't support [MTLDevice supportsFamily:], then use
// [MTLDevice supportsFeatureSet:].
MTLFeatureSet featureSet;
switch (iOSFamily)
{
# if TARGET_OS_IOS
case 1:
featureSet = MTLFeatureSet_iOS_GPUFamily1_v1;
break;
case 2:
featureSet = MTLFeatureSet_iOS_GPUFamily2_v1;
break;
case 3:
featureSet = MTLFeatureSet_iOS_GPUFamily3_v1;
break;
case 4:
featureSet = MTLFeatureSet_iOS_GPUFamily4_v1;
break;
# if __IPHONE_OS_VERSION_MAX_ALLOWED >= 120000
case 5:
featureSet = MTLFeatureSet_iOS_GPUFamily5_v1;
break;
# endif // __IPHONE_OS_VERSION_MAX_ALLOWED
# elif TARGET_OS_TV
case 1:
case 2:
featureSet = MTLFeatureSet_tvOS_GPUFamily1_v1;
break;
case 3:
featureSet = MTLFeatureSet_tvOS_GPUFamily2_v1;
break;
# endif // TARGET_OS_IOS
default:
return false;
}
return [getMetalDevice() supportsFeatureSet:featureSet];
#endif // TARGET_OS_IOS || TARGET_OS_TV
}
bool DisplayMtl::supportsMacGPUFamily(uint8_t macFamily) const
{
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
# if defined(__MAC_10_15)
// If device supports [MTLDevice supportsFamily:], then use it.
if (ANGLE_APPLE_AVAILABLE_XC(10.15, 13.0))
{
MTLGPUFamily family;
switch (macFamily)
{
# if TARGET_OS_MACCATALYST
case 1:
family = MTLGPUFamilyMacCatalyst1;
break;
case 2:
family = MTLGPUFamilyMacCatalyst2;
break;
# else // TARGET_OS_MACCATALYST
case 1:
family = MTLGPUFamilyMac1;
break;
case 2:
family = MTLGPUFamilyMac2;
break;
# endif // TARGET_OS_MACCATALYST
default:
return false;
}
return [getMetalDevice() supportsFamily:family];
} // Metal 2.2
# endif
// If device doesn't support [MTLDevice supportsFamily:], then use
// [MTLDevice supportsFeatureSet:].
# if TARGET_OS_MACCATALYST
UNREACHABLE();
return false;
# else
MTLFeatureSet featureSet;
switch (macFamily)
{
case 1:
featureSet = MTLFeatureSet_macOS_GPUFamily1_v1;
break;
# if defined(__MAC_10_14)
case 2:
featureSet = MTLFeatureSet_macOS_GPUFamily2_v1;
break;
# endif
default:
return false;
}
return [getMetalDevice() supportsFeatureSet:featureSet];
# endif // TARGET_OS_MACCATALYST
#else // #if TARGET_OS_OSX || TARGET_OS_MACCATALYST
return false;
#endif
}
bool DisplayMtl::supportsEitherGPUFamily(uint8_t iOSFamily, uint8_t macFamily) const
{
return supportsAppleGPUFamily(iOSFamily) || supportsMacGPUFamily(macFamily);
}
bool DisplayMtl::isAMD() const
{
return angle::IsAMD(mMetalDeviceVendorId);
}
bool DisplayMtl::isIntel() const
{
return angle::IsIntel(mMetalDeviceVendorId);
}
bool DisplayMtl::isNVIDIA() const
{
return angle::IsNVIDIA(mMetalDeviceVendorId);
}
#if ANGLE_MTL_EVENT_AVAILABLE
mtl::AutoObjCObj<MTLSharedEventListener> DisplayMtl::getOrCreateSharedEventListener()
{
if (!mSharedEventListener)
{
ANGLE_MTL_OBJC_SCOPE
{
mSharedEventListener = [[[MTLSharedEventListener alloc] init] ANGLE_MTL_AUTORELEASE];
}
}
return mSharedEventListener;
}
#endif
} // namespace rx