Hash :
4aaeffd8
        
        Author :
  
        
        Date :
2024-11-25T11:33:30
        
      
Metal: Limit simulator texture size to 8k Regressed in commit d44204893a1725a74cd20b332fbb6f595b0975c3. Bug: angleproject:380712807 Change-Id: Ic5e298412b35451e87b76f70d5b1b253ed1406cb Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/6039222 Reviewed-by: Quyen Le <lehoangquyen@chromium.org> Auto-Submit: Kimmo Kinnunen <kkinnunen@apple.com> Commit-Queue: Kimmo Kinnunen <kkinnunen@apple.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
//
// 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 <sys/param.h>
#include "common/apple_platform_utils.h"
#include "common/system_utils.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/metal/CompilerMtl.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DeviceMtl.h"
#include "libANGLE/renderer/metal/IOSurfaceSurfaceMtl.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/trace.h"
#include "mtl_command_buffer.h"
#include "platform/PlatformMethods.h"
#if ANGLE_METAL_XCODE_BUILDS_SHADERS
#    include "libANGLE/renderer/metal/shaders/mtl_internal_shaders_metallib.h"
#elif ANGLE_METAL_HAS_PREBUILT_INTERNAL_SHADERS
#    include "mtl_internal_shaders_metallib.h"
#else
#    include "libANGLE/renderer/metal/shaders/mtl_internal_shaders_src_autogen.h"
#endif
#include "EGL/eglext.h"
namespace rx
{
static EGLint GetDepthSize(GLint internalformat)
{
    switch (internalformat)
    {
        case GL_STENCIL_INDEX8:
            return 0;
        case GL_DEPTH_COMPONENT16:
            return 16;
        case GL_DEPTH_COMPONENT24:
            return 24;
        case GL_DEPTH_COMPONENT32_OES:
            return 32;
        case GL_DEPTH_COMPONENT32F:
            return 32;
        case GL_DEPTH24_STENCIL8:
            return 24;
        case GL_DEPTH32F_STENCIL8:
            return 32;
        default:
            //    UNREACHABLE(internalformat);
            return 0;
    }
}
static EGLint GetStencilSize(GLint internalformat)
{
    switch (internalformat)
    {
        case GL_STENCIL_INDEX8:
            return 8;
        case GL_DEPTH_COMPONENT16:
            return 0;
        case GL_DEPTH_COMPONENT24:
            return 0;
        case GL_DEPTH_COMPONENT32_OES:
            return 0;
        case GL_DEPTH_COMPONENT32F:
            return 0;
        case GL_DEPTH24_STENCIL8:
            return 8;
        case GL_DEPTH32F_STENCIL8:
            return 8;
        default:
            //    UNREACHABLE(internalformat);
            return 0;
    }
}
bool IsMetalDisplayAvailable()
{
    return angle::IsMetalRendererAvailable();
}
DisplayImpl *CreateMetalDisplay(const egl::DisplayState &state)
{
    return new DisplayMtl(state);
}
DisplayMtl::DisplayMtl(const egl::DisplayState &state)
    : DisplayImpl(state), mDisplay(nullptr), mStateCache(mFeatures)
{}
DisplayMtl::~DisplayMtl() {}
egl::Error DisplayMtl::initialize(egl::Display *display)
{
    ASSERT(IsMetalDisplayAvailable());
    angle::Result result = initializeImpl(display);
    if (result != angle::Result::Continue)
    {
        terminate();
        return egl::EglNotInitialized();
    }
    return egl::NoError();
}
angle::Result DisplayMtl::initializeImpl(egl::Display *display)
{
    ANGLE_MTL_OBJC_SCOPE
    {
        mDisplay = display;
        mMetalDevice = getMetalDeviceMatchingAttribute(display->getAttributeMap());
        // If we can't create a device, fail initialization.
        if (!mMetalDevice.get())
        {
            return angle::Result::Stop;
        }
        mMetalDeviceVendorId = mtl::GetDeviceVendorId(mMetalDevice);
        mCapsInitialized = false;
        initializeFeatures();
        if (mFeatures.requireGpuFamily2.enabled && !supportsEitherGPUFamily(1, 2))
        {
            ANGLE_MTL_LOG("Could not initialize: Metal device does not support Mac GPU family 2.");
            return angle::Result::Stop;
        }
        if (mFeatures.disableMetalOnNvidia.enabled && isNVIDIA())
        {
            ANGLE_MTL_LOG("Could not initialize: Metal not supported on NVIDIA GPUs.");
            return angle::Result::Stop;
        }
        mCmdQueue.set([[mMetalDevice newCommandQueue] ANGLE_MTL_AUTORELEASE]);
        ANGLE_TRY(mFormatTable.initialize(this));
        ANGLE_TRY(initializeShaderLibrary());
        mUtils = std::make_unique<mtl::RenderUtils>(this);
        return angle::Result::Continue;
    }
}
void DisplayMtl::terminate()
{
    mUtils = nullptr;
    mCmdQueue.reset();
    mDefaultShaders = nil;
    mMetalDevice    = nil;
    mSharedEventListener = nil;
    mCapsInitialized = false;
    mMetalDeviceVendorId = 0;
    mComputedAMDBronze   = false;
    mIsAMDBronze         = false;
}
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(bool includeFullVersion)
{
    if (!includeFullVersion)
    {
        // For WebGL contexts it's inappropriate to include any
        // additional version information, but Chrome requires
        // something to be present here.
        return "Unspecified Version";
    }
    ANGLE_MTL_OBJC_SCOPE
    {
        NSProcessInfo *procInfo = [NSProcessInfo processInfo];
        return procInfo.operatingSystemVersionString.UTF8String;
    }
}
DeviceImpl *DisplayMtl::createDevice()
{
    return new DeviceMtl();
}
mtl::AutoObjCPtr<id<MTLDevice>> DisplayMtl::getMetalDeviceMatchingAttribute(
    const egl::AttributeMap &attribs)
{
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
    auto deviceList = mtl::adoptObjCObj(MTLCopyAllDevices());
    EGLAttrib high = attribs.get(EGL_PLATFORM_ANGLE_DEVICE_ID_HIGH_ANGLE, 0);
    EGLAttrib low  = attribs.get(EGL_PLATFORM_ANGLE_DEVICE_ID_LOW_ANGLE, 0);
    uint64_t deviceId =
        angle::GetSystemDeviceIdFromParts(static_cast<uint32_t>(high), static_cast<uint32_t>(low));
    // Check EGL_ANGLE_platform_angle_device_id to see if a device was specified.
    if (deviceId != 0)
    {
        for (id<MTLDevice> device in deviceList.get())
        {
            if ([device registryID] == deviceId)
            {
                return device;
            }
        }
    }
    auto externalGPUs =
        mtl::adoptObjCObj<NSMutableArray<id<MTLDevice>>>([[NSMutableArray alloc] init]);
    auto integratedGPUs =
        mtl::adoptObjCObj<NSMutableArray<id<MTLDevice>>>([[NSMutableArray alloc] init]);
    auto discreteGPUs =
        mtl::adoptObjCObj<NSMutableArray<id<MTLDevice>>>([[NSMutableArray alloc] init]);
    for (id<MTLDevice> device in deviceList.get())
    {
        if (device.removable)
        {
            [externalGPUs addObject:device];
        }
        else if (device.lowPower)
        {
            [integratedGPUs addObject:device];
        }
        else
        {
            [discreteGPUs addObject:device];
        }
    }
    // TODO(kpiddington: External GPU support. Do we prefer high power / low bandwidth for general
    // WebGL applications?
    //      Can we support hot-swapping in GPU's?
    if (attribs.get(EGL_POWER_PREFERENCE_ANGLE, 0) == EGL_HIGH_POWER_ANGLE)
    {
        // Search for a discrete GPU first.
        for (id<MTLDevice> device in discreteGPUs.get())
        {
            if (![device isHeadless])
                return device;
        }
    }
    else if (attribs.get(EGL_POWER_PREFERENCE_ANGLE, 0) == EGL_LOW_POWER_ANGLE)
    {
        // If we've selected a low power device, look through integrated devices.
        for (id<MTLDevice> device in integratedGPUs.get())
        {
            if (![device isHeadless])
                return device;
        }
    }
    const std::string preferredDeviceString = angle::GetPreferredDeviceString();
    if (!preferredDeviceString.empty())
    {
        for (id<MTLDevice> device in deviceList.get())
        {
            if ([device.name.lowercaseString
                    containsString:[NSString stringWithUTF8String:preferredDeviceString.c_str()]])
            {
                NSLog(@"Using Metal Device: %@", [device name]);
                return device;
            }
        }
    }
#endif
    // If we can't find anything, or are on a platform that doesn't support power options, create a
    // default device.
    return mtl::adoptObjCObj(MTLCreateSystemDefaultDevice());
}
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();
}
egl::Error DisplayMtl::waitUntilWorkScheduled()
{
    for (auto context : mState.contextMap)
    {
        auto contextMtl = GetImplAs<ContextMtl>(context.second);
        contextMtl->flushCommandBuffer(mtl::WaitUntilScheduled);
    }
    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);
        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, attribs, this);
}
StreamProducerImpl *DisplayMtl::createStreamProducerD3DTexture(
    egl::Stream::ConsumerType consumerType,
    const egl::AttributeMap &attribs)
{
    UNIMPLEMENTED();
    return nullptr;
}
ShareGroupImpl *DisplayMtl::createShareGroup(const egl::ShareGroupState &state)
{
    return new ShareGroupMtl(state);
}
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, attribs);
        default:
            UNREACHABLE();
            return nullptr;
    }
}
gl::Version DisplayMtl::getMaxSupportedESVersion() const
{
#if TARGET_OS_SIMULATOR
    // Simulator should be able to support ES3, despite not supporting iOS GPU
    // Family 3 in its entirety.
    // FIXME: None of the feature conditions are checked for simulator support.
    return gl::Version(3, 0);
#else
    if (supportsEitherGPUFamily(3, 1))
    {
        return mtl::kMaxSupportedGLVersion;
    }
    return gl::Version(2, 0);
#endif
}
gl::Version DisplayMtl::getMaxConformantESVersion() const
{
    return std::min(getMaxSupportedESVersion(), gl::Version(3, 0));
}
EGLSyncImpl *DisplayMtl::createSync()
{
    return new EGLSyncMtl();
}
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->iosurfaceClientBuffer      = true;
    outExtensions->surfacelessContext         = true;
    outExtensions->noConfigContext            = true;
    outExtensions->displayTextureShareGroup   = true;
    outExtensions->displaySemaphoreShareGroup = true;
    outExtensions->mtlTextureClientBuffer     = true;
    outExtensions->waitUntilWorkScheduled     = 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/42263503)
    outExtensions->robustResourceInitializationANGLE = true;
    // EGL_KHR_image
    outExtensions->image     = true;
    outExtensions->imageBase = true;
    // EGL_ANGLE_metal_create_context_ownership_identity
    outExtensions->metalCreateContextOwnershipIdentityANGLE = true;
    // EGL_ANGLE_metal_sync_shared_event
    outExtensions->mtlSyncSharedEventANGLE = true;
}
void DisplayMtl::generateCaps(egl::Caps *outCaps) const
{
    outCaps->textureNPOT = true;
}
void DisplayMtl::initializeFrontendFeatures(angle::FrontendFeatures *features) const
{
    // The Metal backend's handling of compile and link is thread-safe
    ANGLE_FEATURE_CONDITION(features, compileJobIsThreadSafe, true);
    ANGLE_FEATURE_CONDITION(features, linkJobIsThreadSafe, true);
}
void DisplayMtl::populateFeatureList(angle::FeatureList *features)
{
    mFeatures.populateFeatureList(features);
}
EGLenum DisplayMtl::EGLDrawingBufferTextureTarget()
{
    // TODO(anglebug.com/42264909): Apple's implementation conditionalized this on
    // MacCatalyst and whether it was running on ARM64 or X64, preferring
    // EGL_TEXTURE_RECTANGLE_ANGLE. Metal can bind IOSurfaces to regular 2D
    // textures, and rectangular textures don't work in the SPIR-V Metal
    // backend, so for the time being use EGL_TEXTURE_2D on all platforms.
    return EGL_TEXTURE_2D;
}
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 = EGLDrawingBufferTextureTarget();
    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.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;
        config.depthStencilFormat = GL_DEPTH24_STENCIL8;
        configs.add(config);
        // With D
        config.depthSize          = 24;
        config.stencilSize        = 0;
        config.depthStencilFormat = GL_DEPTH_COMPONENT24;
        configs.add(config);
        // With S
        config.depthSize          = 0;
        config.stencilSize        = 8;
        config.depthStencilFormat = GL_STENCIL_INDEX8;
        configs.add(config);
        // No DS
        config.depthSize          = 0;
        config.stencilSize        = 0;
        config.depthStencilFormat = GL_NONE;
        configs.add(config);
        // Tests like dEQP-GLES2.functional.depth_range.* assume EGL_DEPTH_SIZE is properly set even
        // if renderConfig attributes are set to glu::RenderConfig::DONT_CARE
        config.depthSize   = GetDepthSize(config.depthStencilFormat);
        config.stencilSize = GetStencilSize(config.depthStencilFormat);
        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:
            return TextureImageSiblingMtl::ValidateClientBuffer(this, clientBuffer, attribs);
        default:
            UNREACHABLE();
            return egl::EglBadAttribute();
    }
}
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;
}
const ShPixelLocalStorageOptions &DisplayMtl::getNativePixelLocalStorageOptions() const
{
    ensureCapsInitialized();
    return mNativePLSOptions;
}
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;
#elif TARGET_OS_SIMULATOR
    mNativeCaps.max2DTextureSize          = 8192;
    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            = std::log2(mNativeCaps.max2DTextureSize) + 1;
    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/42263403
    // NOTE(kpiddington): This seems to be fixed in macOS Monterey
    if (@available(macOS 12.0, *))
    {
        mNativeCaps.maxAliasedPointSize = 511;
    }
    else
    {
        mNativeCaps.maxAliasedPointSize = 64;
    }
    mNativeCaps.minAliasedLineWidth = 1.0f;
    mNativeCaps.maxAliasedLineWidth = 1.0f;
    if (supportsEitherGPUFamily(2, 1) && !mFeatures.limitMaxDrawBuffersForTesting.enabled)
    {
        mNativeCaps.maxDrawBuffers      = mtl::kMaxRenderTargets;
        mNativeCaps.maxColorAttachments = mtl::kMaxRenderTargets;
    }
    else
    {
        mNativeCaps.maxDrawBuffers      = mtl::kMaxRenderTargetsOlderGPUFamilies;
        mNativeCaps.maxColorAttachments = mtl::kMaxRenderTargetsOlderGPUFamilies;
    }
    ASSERT(static_cast<uint32_t>(mNativeCaps.maxDrawBuffers) <= mtl::kMaxRenderTargets);
    ASSERT(static_cast<uint32_t>(mNativeCaps.maxColorAttachments) <= mtl::kMaxRenderTargets);
    mNativeCaps.maxFramebufferWidth  = mNativeCaps.max2DTextureSize;
    mNativeCaps.maxFramebufferHeight = mNativeCaps.max2DTextureSize;
    mNativeCaps.maxViewportWidth     = mNativeCaps.max2DTextureSize;
    mNativeCaps.maxViewportHeight    = mNativeCaps.max2DTextureSize;
    bool isCatalyst = TARGET_OS_MACCATALYST;
    mMaxColorTargetBits = mtl::kMaxColorTargetBitsApple1To3;
    if (supportsMacGPUFamily(1) || isCatalyst)
    {
        mMaxColorTargetBits = mtl::kMaxColorTargetBitsMacAndCatalyst;
    }
    else if (supportsAppleGPUFamily(4))
    {
        mMaxColorTargetBits = mtl::kMaxColorTargetBitsApple4Plus;
    }
    if (mFeatures.limitMaxColorTargetBitsForTesting.enabled)
    {
        // Set so we have enough for RGBA8 on every attachment
        // but not enough for RGBA32UI.
        mMaxColorTargetBits = mNativeCaps.maxColorAttachments * 32;
    }
    // MSAA
    mNativeCaps.maxSamples             = mFormatTable.getMaxSamples();
    mNativeCaps.maxSampleMaskWords     = 1;
    mNativeCaps.maxColorTextureSamples = mNativeCaps.maxSamples;
    mNativeCaps.maxDepthTextureSamples = mNativeCaps.maxSamples;
    mNativeCaps.maxIntegerSamples      = mNativeCaps.maxSamples;
    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.
    if (supportsAppleGPUFamily(1))
    {
        mNativeCaps.uniformBufferOffsetAlignment =
            16;  // on Apple based GPU's We can ignore data types when setting constant buffer
                 // alignment at 16.
    }
    else
    {
        mNativeCaps.uniformBufferOffsetAlignment =
            256;  // constant buffers on all other GPUs must be aligned to 256.
    }
    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_OES_get_program_binary
    mNativeCaps.programBinaryFormats.push_back(GL_PROGRAM_BINARY_ANGLE);
    // GL_APPLE_clip_distance / GL_ANGLE_clip_cull_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;
}
void DisplayMtl::initializeExtensions() const
{
    // Reset
    mNativeExtensions = gl::Extensions();
    // Enable this for simple buffer readback testing, but some functionality is missing.
    // NOTE(hqle): Support full mapBufferRangeEXT extension.
    mNativeExtensions.mapbufferOES                  = true;
    mNativeExtensions.mapBufferRangeEXT             = true;
    mNativeExtensions.textureStorageEXT             = true;
    mNativeExtensions.clipControlEXT                = true;
    mNativeExtensions.drawBuffersEXT                = true;
    mNativeExtensions.drawBuffersIndexedEXT         = true;
    mNativeExtensions.drawBuffersIndexedOES         = true;
    mNativeExtensions.fboRenderMipmapOES            = true;
    mNativeExtensions.fragDepthEXT                  = true;
    mNativeExtensions.conservativeDepthEXT          = true;
    mNativeExtensions.framebufferBlitANGLE          = true;
    mNativeExtensions.framebufferBlitNV             = true;
    mNativeExtensions.framebufferMultisampleANGLE   = true;
    mNativeExtensions.polygonModeANGLE              = true;
    mNativeExtensions.polygonOffsetClampEXT         = true;
    mNativeExtensions.stencilTexturingANGLE         = true;
    mNativeExtensions.copyTextureCHROMIUM           = true;
    mNativeExtensions.copyCompressedTextureCHROMIUM = false;
    mNativeExtensions.textureMirrorClampToEdgeEXT   = true;
    mNativeExtensions.depthClampEXT                 = true;
    // 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/42263519)
    mNativeExtensions.debugMarkerEXT = true;
    mNativeExtensions.robustnessEXT               = true;
    mNativeExtensions.robustnessKHR               = true;
    mNativeExtensions.textureBorderClampOES       = false;  // not implemented yet
    mNativeExtensions.multiDrawIndirectEXT        = true;
    mNativeExtensions.translatedShaderSourceANGLE = true;
    mNativeExtensions.discardFramebufferEXT       = true;
    // TODO(anglebug.com/42264909): Apple's implementation exposed
    // mNativeExtensions.textureRectangle = true here and
    // EGL_TEXTURE_RECTANGLE_ANGLE as the eglBindTexImage texture target on
    // macOS. This no longer seems necessary as IOSurfaces can be bound to
    // regular 2D textures with Metal, and causes other problems such as
    // breaking the SPIR-V Metal compiler.
    mNativeExtensions.multisampledRenderToTextureEXT =
        (supportsAppleGPUFamily(1) ||
         mFeatures.enableMultisampledRenderToTextureOnNonTilers.enabled) &&
        mFeatures.hasShaderStencilOutput.enabled && mFeatures.hasDepthAutoResolve.enabled &&
        mFeatures.hasStencilAutoResolve.enabled;
    // Enable EXT_blend_minmax
    mNativeExtensions.blendMinmaxEXT = true;
    mNativeExtensions.EGLImageOES         = true;
    mNativeExtensions.EGLImageExternalOES = false;
    // NOTE(hqle): Support GL_OES_EGL_image_external_essl3.
    mNativeExtensions.EGLImageExternalEssl3OES = false;
    mNativeExtensions.memoryObjectEXT   = false;
    mNativeExtensions.memoryObjectFdEXT = false;
    mNativeExtensions.semaphoreEXT   = false;
    mNativeExtensions.semaphoreFdEXT = false;
    mNativeExtensions.instancedArraysANGLE = true;
    mNativeExtensions.instancedArraysEXT   = mNativeExtensions.instancedArraysANGLE;
    mNativeExtensions.robustBufferAccessBehaviorKHR = false;
    mNativeExtensions.EGLSyncOES = false;
    mNativeExtensions.occlusionQueryBooleanEXT = true;
    mNativeExtensions.disjointTimerQueryEXT = true;
    mNativeCaps.queryCounterBitsTimeElapsed = 64;
    mNativeCaps.queryCounterBitsTimestamp   = 0;
    mNativeExtensions.textureFilterAnisotropicEXT = true;
    mNativeCaps.maxTextureAnisotropy              = 16;
    mNativeExtensions.textureNpotOES = true;
    mNativeExtensions.texture3DOES = true;
    mNativeExtensions.textureShadowLodEXT = true;
    if ([mMetalDevice areProgrammableSamplePositionsSupported])
    {
        mNativeExtensions.textureMultisampleANGLE = true;
    }
    mNativeExtensions.sampleVariablesOES = true;
    if ([mMetalDevice supportsPullModelInterpolation])
    {
        mNativeExtensions.shaderMultisampleInterpolationOES = true;
        mNativeCaps.subPixelInterpolationOffsetBits         = 4;
        if (supportsAppleGPUFamily(1))
        {
            mNativeCaps.minInterpolationOffset = -0.5f;
            mNativeCaps.maxInterpolationOffset = +0.5f;
        }
        else
        {
            // On non-Apple GPUs, the actual range is usually
            // [-0.5, +0.4375] but due to framebuffer Y-flip
            // the effective range for the Y direction will be
            // [-0.4375, +0.5] when the default FBO is bound.
            mNativeCaps.minInterpolationOffset = -0.4375f;  // -0.5 + (2 ^ -4)
            mNativeCaps.maxInterpolationOffset = +0.4375f;  // +0.5 - (2 ^ -4)
        }
    }
    mNativeExtensions.shaderNoperspectiveInterpolationNV = true;
    mNativeExtensions.shaderTextureLodEXT = true;
    mNativeExtensions.standardDerivativesOES = true;
    mNativeExtensions.elementIndexUintOES = true;
    // GL_OES_get_program_binary
    mNativeExtensions.getProgramBinaryOES = true;
    // GL_APPLE_clip_distance
    mNativeExtensions.clipDistanceAPPLE = true;
    // GL_ANGLE_clip_cull_distance
    mNativeExtensions.clipCullDistanceANGLE = true;
    // GL_NV_pixel_buffer_object
    mNativeExtensions.pixelBufferObjectNV = true;
    mNativeExtensions.packReverseRowOrderANGLE = 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;
        // GL_ARB_sync
        mNativeExtensions.syncARB = true;
    }
    // GL_KHR_parallel_shader_compile
    mNativeExtensions.parallelShaderCompileKHR = true;
    mNativeExtensions.baseInstanceEXT             = mFeatures.hasBaseVertexInstancedDraw.enabled;
    mNativeExtensions.baseVertexBaseInstanceANGLE = mFeatures.hasBaseVertexInstancedDraw.enabled;
    mNativeExtensions.baseVertexBaseInstanceShaderBuiltinANGLE =
        mFeatures.hasBaseVertexInstancedDraw.enabled;
    // Metal uses the opposite provoking vertex as GLES so emulation is required to use the GLES
    // behaviour. Allow users to change the provoking vertex for improved performance.
    mNativeExtensions.provokingVertexANGLE = true;
    // GL_EXT_blend_func_extended
    mNativeExtensions.blendFuncExtendedEXT = true;
    mNativeCaps.maxDualSourceDrawBuffers   = 1;
    // GL_ANGLE_shader_pixel_local_storage.
    if (!mFeatures.disableProgrammableBlending.enabled && supportsAppleGPUFamily(1))
    {
        // Programmable blending is supported on all Apple GPU families, and is always coherent.
        mNativePLSOptions.type = ShPixelLocalStorageType::FramebufferFetch;
        // Raster order groups are NOT required to make framebuffer fetch coherent, however, they
        // may improve performance by allowing finer grained synchronization (e.g., by assigning
        // attachments to different raster order groups when they don't depend on each other).
        bool rasterOrderGroupsSupported =
            !mFeatures.disableRasterOrderGroups.enabled && supportsAppleGPUFamily(4);
        mNativePLSOptions.fragmentSyncType =
            rasterOrderGroupsSupported ? ShFragmentSynchronizationType::RasterOrderGroups_Metal
                                       : ShFragmentSynchronizationType::Automatic;
        mNativeExtensions.shaderPixelLocalStorageANGLE         = true;
        mNativeExtensions.shaderPixelLocalStorageCoherentANGLE = true;
    }
    else
    {
        MTLReadWriteTextureTier readWriteTextureTier = [mMetalDevice readWriteTextureSupport];
        if (readWriteTextureTier != MTLReadWriteTextureTierNone)
        {
            mNativePLSOptions.type = ShPixelLocalStorageType::ImageLoadStore;
            // Raster order groups are required to make PLS coherent when using read_write textures.
            bool rasterOrderGroupsSupported = !mFeatures.disableRasterOrderGroups.enabled &&
                                              [mMetalDevice areRasterOrderGroupsSupported];
            mNativePLSOptions.fragmentSyncType =
                rasterOrderGroupsSupported ? ShFragmentSynchronizationType::RasterOrderGroups_Metal
                                           : ShFragmentSynchronizationType::NotSupported;
            mNativePLSOptions.supportsNativeRGBA8ImageFormats =
                !mFeatures.disableRWTextureTier2Support.enabled &&
                readWriteTextureTier == MTLReadWriteTextureTier2;
            if (rasterOrderGroupsSupported && isAMD())
            {
                // anglebug.com/42266263 -- [[raster_order_group()]] does not work for read_write
                // textures on AMD when the render pass doesn't have a color attachment on slot 0.
                // To work around this we attach one of the PLS textures to GL_COLOR_ATTACHMENT0, if
                // there isn't one already.
                mNativePLSOptions.renderPassNeedsAMDRasterOrderGroupsWorkaround = true;
            }
            mNativeExtensions.shaderPixelLocalStorageANGLE         = true;
            mNativeExtensions.shaderPixelLocalStorageCoherentANGLE = rasterOrderGroupsSupported;
            // Set up PLS caps here because the higher level context won't have enough info to set
            // them up itself. Shader images and other ES3.1 caps aren't fully exposed yet.
            static_assert(mtl::kMaxShaderImages >=
                          gl::IMPLEMENTATION_MAX_PIXEL_LOCAL_STORAGE_PLANES);
            mNativeCaps.maxPixelLocalStoragePlanes =
                gl::IMPLEMENTATION_MAX_PIXEL_LOCAL_STORAGE_PLANES;
            mNativeCaps.maxColorAttachmentsWithActivePixelLocalStorage =
                gl::IMPLEMENTATION_MAX_DRAW_BUFFERS;
            mNativeCaps.maxCombinedDrawBuffersAndPixelLocalStoragePlanes =
                gl::IMPLEMENTATION_MAX_PIXEL_LOCAL_STORAGE_PLANES +
                gl::IMPLEMENTATION_MAX_DRAW_BUFFERS;
            mNativeCaps.maxShaderImageUniforms[gl::ShaderType::Fragment] =
                gl::IMPLEMENTATION_MAX_PIXEL_LOCAL_STORAGE_PLANES;
            mNativeCaps.maxImageUnits = gl::IMPLEMENTATION_MAX_PIXEL_LOCAL_STORAGE_PLANES;
        }
    }
    // "The GPUs in Apple3 through Apple8 families only support memory barriers for compute command
    // encoders, and for vertex-to-vertex and vertex-to-fragment stages of render command encoders."
    mHasFragmentMemoryBarriers = !supportsAppleGPUFamily(3);
}
void DisplayMtl::initializeTextureCaps() const
{
    mNativeTextureCaps.clear();
    mFormatTable.generateTextureCaps(this, &mNativeTextureCaps);
    // 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.compressedTextureEtcANGLE = true;
    }
    else
    {
        mNativeLimitations.emulatedEtc1 = true;
    }
    // Enable EXT_compressed_ETC1_RGB8_sub_texture if ETC1 is supported.
    mNativeExtensions.compressedETC1RGB8SubTextureEXT =
        mNativeExtensions.compressedETC1RGB8TextureOES;
    // Enable ASTC sliced 3D, requires MTLGPUFamilyApple3
    if (supportsAppleGPUFamily(3) && mNativeExtensions.textureCompressionAstcLdrKHR)
    {
        mNativeExtensions.textureCompressionAstcSliced3dKHR = 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::initializeLimitations()
{
    mNativeLimitations.noVertexAttributeAliasing = true;
}
void DisplayMtl::initializeFeatures()
{
    bool isOSX       = TARGET_OS_OSX;
    bool isCatalyst  = TARGET_OS_MACCATALYST;
    bool isSimulator = TARGET_OS_SIMULATOR;
    bool isARM       = ANGLE_APPLE_IS_ARM;
    ApplyFeatureOverrides(&mFeatures, getState().featureOverrides);
    if (mState.featureOverrides.allDisabled)
    {
        return;
    }
    ANGLE_FEATURE_CONDITION((&mFeatures), allowGenMultipleMipsPerPass, true);
    ANGLE_FEATURE_CONDITION((&mFeatures), forceBufferGPUStorage, false);
    ANGLE_FEATURE_CONDITION((&mFeatures), hasExplicitMemBarrier, (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));
    // Apple2 does not support comparison functions in MTLSamplerState
    ANGLE_FEATURE_CONDITION((&mFeatures), allowRuntimeSamplerCompareMode,
                            supportsEitherGPUFamily(3, 1));
    // AMD does not support sample_compare with gradients
    ANGLE_FEATURE_CONDITION((&mFeatures), allowSamplerCompareGradient, !isAMD());
    // http://anglebug.com/40644746
    // Stencil blit shader is not compiled on Intel & NVIDIA, need investigation.
    ANGLE_FEATURE_CONDITION((&mFeatures), hasShaderStencilOutput, !isIntel() && !isNVIDIA());
    ANGLE_FEATURE_CONDITION((&mFeatures), hasTextureSwizzle,
                            supportsEitherGPUFamily(3, 2) && !isSimulator);
    ANGLE_FEATURE_CONDITION((&mFeatures), avoidStencilTextureSwizzle, isIntel());
    // http://crbug.com/1136673
    // Fence sync is flaky on Nvidia
    ANGLE_FEATURE_CONDITION((&mFeatures), hasEvents, !isNVIDIA());
    ANGLE_FEATURE_CONDITION((&mFeatures), hasCheapRenderPass, (isOSX || isCatalyst) && !isARM);
    // http://anglebug.com/42263788
    // 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), allowSeparateDepthStencilBuffers,
                            supportsAppleGPUFamily(1) && !isSimulator);
    ANGLE_FEATURE_CONDITION((&mFeatures), emulateTransformFeedback, true);
    ANGLE_FEATURE_CONDITION((&mFeatures), intelExplicitBoolCastWorkaround,
                            isIntel() && GetMacOSVersion() < OSVersion(11, 0, 0));
    ANGLE_FEATURE_CONDITION((&mFeatures), intelDisableFastMath,
                            isIntel() && GetMacOSVersion() < OSVersion(12, 0, 0));
    ANGLE_FEATURE_CONDITION((&mFeatures), emulateAlphaToCoverage,
                            isSimulator || !supportsAppleGPUFamily(1));
    ANGLE_FEATURE_CONDITION((&mFeatures), writeHelperSampleMask, supportsAppleGPUFamily(1));
    ANGLE_FEATURE_CONDITION((&mFeatures), multisampleColorFormatShaderReadWorkaround, isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), copyIOSurfaceToNonIOSurfaceForReadOptimization,
                            isIntel() || isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), copyTextureToBufferForReadOptimization, isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), forceNonCSBaseMipmapGeneration, isIntel());
    ANGLE_FEATURE_CONDITION((&mFeatures), preemptivelyStartProvokingVertexCommandBuffer, isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), alwaysUseStagedBufferUpdates, isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), alwaysUseManagedStorageModeForBuffers, isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), alwaysUseSharedStorageModeForBuffers, isIntel());
    ANGLE_FEATURE_CONDITION((&mFeatures), useShadowBuffersWhenAppropriate, isIntel());
    // At least one of these must not be set.
    ASSERT(!mFeatures.alwaysUseManagedStorageModeForBuffers.enabled ||
           !mFeatures.alwaysUseSharedStorageModeForBuffers.enabled);
    ANGLE_FEATURE_CONDITION((&mFeatures), uploadDataToIosurfacesWithStagingBuffers, isAMD());
    // Render passes can be rendered without attachments on Apple4 , mac2 hardware.
    ANGLE_FEATURE_CONDITION(&(mFeatures), allowRenderpassWithoutAttachment,
                            supportsEitherGPUFamily(4, 2));
    ANGLE_FEATURE_CONDITION((&mFeatures), enableInMemoryMtlLibraryCache, true);
    ANGLE_FEATURE_CONDITION((&mFeatures), enableParallelMtlLibraryCompilation, true);
    // Uploading texture data via staging buffers improves performance on all tested systems.
    // http://anglebug.com/40644905: Disabled on intel due to some texture formats uploading
    // incorrectly with staging buffers
    ANGLE_FEATURE_CONDITION(&mFeatures, alwaysPreferStagedTextureUploads, true);
    ANGLE_FEATURE_CONDITION(&mFeatures, disableStagedInitializationOfPackedTextureFormats,
                            isIntel() || isAMD());
    ANGLE_FEATURE_CONDITION((&mFeatures), generateShareableShaders, true);
    // http://anglebug.com/42266609: NVIDIA GPUs are unsupported due to scarcity of the hardware.
    ANGLE_FEATURE_CONDITION((&mFeatures), disableMetalOnNvidia, true);
    // The AMDMTLBronzeDriver seems to have bugs flushing vertex data to the GPU during some kinds
    // of buffer uploads which require a flush to work around.
    ANGLE_FEATURE_CONDITION((&mFeatures), flushAfterStreamVertexData, isAMDBronzeDriver());
    // TODO(anglebug.com/40096869): GPUs that don't support Mac GPU family 2 or greater are
    // unsupported by the Metal backend.
    ANGLE_FEATURE_CONDITION((&mFeatures), requireGpuFamily2, true);
    // http://anglebug.com/42266744: Rescope global variables which are only used in one function to
    // be function local. Disabled on AMD FirePro devices: http://anglebug.com/40096898
    ANGLE_FEATURE_CONDITION((&mFeatures), rescopeGlobalVariables, !isAMDFireProDevice());
    // Apple-specific pre-transform for explicit cubemap derivatives
    ANGLE_FEATURE_CONDITION((&mFeatures), preTransformTextureCubeGradDerivatives,
                            supportsAppleGPUFamily(1));
    // Apple-specific cubemap derivative transformation is not compatible with
    // the textureGrad shadow sampler emulation. The latter is used only on AMD.
    ASSERT(!mFeatures.preTransformTextureCubeGradDerivatives.enabled ||
           mFeatures.allowSamplerCompareGradient.enabled);
    // Metal compiler optimizations may remove infinite loops causing crashes later in shader
    // execution. http://crbug.com/1513738
    // Disabled on Mac11 due to test failures. http://crbug.com/1522730
    ANGLE_FEATURE_CONDITION((&mFeatures), injectAsmStatementIntoLoopBodies,
                            !isOSX || GetMacOSVersion() >= OSVersion(12, 0, 0));
}
angle::Result DisplayMtl::initializeShaderLibrary()
{
    mtl::AutoObjCPtr<NSError *> err = nil;
#if ANGLE_METAL_XCODE_BUILDS_SHADERS || ANGLE_METAL_HAS_PREBUILT_INTERNAL_SHADERS
    mDefaultShaders = mtl::CreateShaderLibraryFromStaticBinary(getMetalDevice(), gDefaultMetallib,
                                                               std::size(gDefaultMetallib), &err);
#else
    const bool disableFastMath = false;
    const bool usesInvariance  = true;
    mDefaultShaders            = mtl::CreateShaderLibrary(getMetalDevice(), gDefaultMetallibSrc, {},
                                                          disableFastMath, usesInvariance, &err);
#endif
    if (err)
    {
        ERR() << "Internal error: " << err.get().localizedDescription.UTF8String;
        return angle::Result::Stop;
    }
    return angle::Result::Continue;
}
id<MTLLibrary> DisplayMtl::getDefaultShadersLib()
{
    return mDefaultShaders;
}
bool DisplayMtl::supportsAppleGPUFamily(uint8_t iOSFamily) const
{
    return mtl::SupportsAppleGPUFamily(getMetalDevice(), iOSFamily);
}
bool DisplayMtl::supportsMacGPUFamily(uint8_t macFamily) const
{
    return mtl::SupportsMacGPUFamily(getMetalDevice(), macFamily);
}
bool DisplayMtl::supportsEitherGPUFamily(uint8_t iOSFamily, uint8_t macFamily) const
{
    return supportsAppleGPUFamily(iOSFamily) || supportsMacGPUFamily(macFamily);
}
bool DisplayMtl::supports32BitFloatFiltering() const
{
#if !TARGET_OS_WATCH
    return [mMetalDevice supports32BitFloatFiltering];
#else
    return false;
#endif
}
bool DisplayMtl::supportsBCTextureCompression() const
{
    if (@available(macCatalyst 16.4, iOS 16.4, *))
    {
        return [mMetalDevice supportsBCTextureCompression];
    }
#if TARGET_OS_MACCATALYST
    return true;  // Always true on old Catalyst
#else
    return false;  // Always false on old iOS
#endif
}
bool DisplayMtl::supportsDepth24Stencil8PixelFormat() const
{
#if TARGET_OS_OSX || TARGET_OS_MACCATALYST
    return [mMetalDevice isDepth24Stencil8PixelFormatSupported];
#else
    return false;
#endif
}
bool DisplayMtl::isAMD() const
{
    return angle::IsAMD(mMetalDeviceVendorId);
}
bool DisplayMtl::isAMDBronzeDriver() const
{
    if (!isAMD())
    {
        return false;
    }
    if (mComputedAMDBronze)
    {
        return mIsAMDBronze;
    }
    // All devices known to be covered by AMDMTlBronzeDriver.
    //
    // Note that we can not compare substrings because some devices
    // (AMD Radeon Pro 560) are substrings of ones supported by a
    // later driver (AMD Radeon Pro 5600M).
    NSString *kMTLBronzeDeviceNames[22] = {
        @"FirePro D300",    @"FirePro D500",    @"FirePro D700",   @"Radeon R9 M290",
        @"Radeon R9 M290X", @"Radeon R9 M370X", @"Radeon R9 M380", @"Radeon R9 M390",
        @"Radeon R9 M395",  @"Radeon Pro 450",  @"Radeon Pro 455", @"Radeon Pro 460",
        @"Radeon Pro 555",  @"Radeon Pro 555X", @"Radeon Pro 560", @"Radeon Pro 560X",
        @"Radeon Pro 570",  @"Radeon Pro 570X", @"Radeon Pro 575", @"Radeon Pro 575X",
        @"Radeon Pro 580",  @"Radeon Pro 580X"};
    for (size_t i = 0; i < ArraySize(kMTLBronzeDeviceNames); ++i)
    {
        if ([[mMetalDevice name] hasSuffix:kMTLBronzeDeviceNames[i]])
        {
            mIsAMDBronze = true;
            break;
        }
    }
    mComputedAMDBronze = true;
    return mIsAMDBronze;
}
bool DisplayMtl::isAMDFireProDevice() const
{
    if (!isAMD())
    {
        return false;
    }
    return [[mMetalDevice name] containsString:@"FirePro"];
}
bool DisplayMtl::isIntel() const
{
    return angle::IsIntel(mMetalDeviceVendorId);
}
bool DisplayMtl::isNVIDIA() const
{
    return angle::IsNVIDIA(mMetalDeviceVendorId);
}
bool DisplayMtl::isSimulator() const
{
    return TARGET_OS_SIMULATOR;
}
mtl::AutoObjCObj<MTLSharedEventListener> DisplayMtl::getOrCreateSharedEventListener()
{
    if (!mSharedEventListener)
    {
        ANGLE_MTL_OBJC_SCOPE
        {
            mSharedEventListener = [[[MTLSharedEventListener alloc] init] ANGLE_MTL_AUTORELEASE];
            ASSERT(mSharedEventListener);  // Failure here most probably means a sandbox issue.
        }
    }
    return mSharedEventListener;
}
}  // namespace rx