Hash :
2dd13ebb
Author :
Date :
2022-04-21T11:25:00
Track Surface color & depth/stencil init separately. This clears up some trace testing confusion due when robust resource init is enabled, and the app clears color but not depth on the default surface. Bug: angleproject:7221 Change-Id: Id97871aec32ad831b663aaa9116e04b582ab5a36 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/3600375 Reviewed-by: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Geoff Lang <geofflang@chromium.org> Commit-Queue: Jamie Madill <jmadill@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 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 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352
//
// 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.
//
// TextureMtl.mm:
// Implements the class methods for TextureMtl.
//
#include "libANGLE/renderer/metal/TextureMtl.h"
#include "common/Color.h"
#include "common/MemoryBuffer.h"
#include "common/debug.h"
#include "common/mathutil.h"
#include "image_util/imageformats.h"
#include "libANGLE/Surface.h"
#include "libANGLE/renderer/metal/BufferMtl.h"
#include "libANGLE/renderer/metal/ContextMtl.h"
#include "libANGLE/renderer/metal/DisplayMtl.h"
#include "libANGLE/renderer/metal/FrameBufferMtl.h"
#include "libANGLE/renderer/metal/ImageMtl.h"
#include "libANGLE/renderer/metal/SamplerMtl.h"
#include "libANGLE/renderer/metal/SurfaceMtl.h"
#include "libANGLE/renderer/metal/mtl_common.h"
#include "libANGLE/renderer/metal/mtl_format_utils.h"
#include "libANGLE/renderer/metal/mtl_utils.h"
#include "libANGLE/renderer/renderer_utils.h"
namespace rx
{
namespace
{
gl::ImageIndex GetZeroLevelIndex(const mtl::TextureRef &image)
{
switch (image->textureType())
{
case MTLTextureType2D:
return gl::ImageIndex::Make2D(0);
case MTLTextureTypeCube:
return gl::ImageIndex::MakeFromType(gl::TextureType::CubeMap, 0);
case MTLTextureType2DArray:
return gl::ImageIndex::Make2DArray(0 /** entire layers */);
case MTLTextureType2DMultisample:
return gl::ImageIndex::Make2DMultisample();
case MTLTextureType3D:
return gl::ImageIndex::Make3D(0 /** entire layers */);
default:
UNREACHABLE();
break;
}
return gl::ImageIndex();
}
// Slice is ignored if texture type is not Cube or 2D array
gl::ImageIndex GetCubeOrArraySliceMipIndex(const mtl::TextureRef &image,
uint32_t slice,
uint32_t level)
{
switch (image->textureType())
{
case MTLTextureType2D:
return gl::ImageIndex::Make2D(level);
case MTLTextureTypeCube:
{
auto cubeFace = static_cast<gl::TextureTarget>(
static_cast<int>(gl::TextureTarget::CubeMapPositiveX) + slice);
return gl::ImageIndex::MakeCubeMapFace(cubeFace, level);
}
case MTLTextureType2DArray:
return gl::ImageIndex::Make2DArray(level, slice);
case MTLTextureType2DMultisample:
return gl::ImageIndex::Make2DMultisample();
case MTLTextureType3D:
return gl::ImageIndex::Make3D(level);
default:
UNREACHABLE();
break;
}
return gl::ImageIndex();
}
// layer is ignored if texture type is not Cube or 2D array or 3D
gl::ImageIndex GetLayerMipIndex(const mtl::TextureRef &image, uint32_t layer, uint32_t level)
{
switch (image->textureType())
{
case MTLTextureType2D:
return gl::ImageIndex::Make2D(level);
case MTLTextureTypeCube:
{
auto cubeFace = static_cast<gl::TextureTarget>(
static_cast<int>(gl::TextureTarget::CubeMapPositiveX) + layer);
return gl::ImageIndex::MakeCubeMapFace(cubeFace, level);
}
case MTLTextureType2DArray:
return gl::ImageIndex::Make2DArray(level, layer);
case MTLTextureType2DMultisample:
return gl::ImageIndex::Make2DMultisample();
case MTLTextureType3D:
return gl::ImageIndex::Make3D(level, layer);
default:
UNREACHABLE();
break;
}
return gl::ImageIndex();
}
GLuint GetImageLayerIndexFrom(const gl::ImageIndex &index)
{
switch (index.getType())
{
case gl::TextureType::_2D:
case gl::TextureType::_2DMultisample:
case gl::TextureType::Rectangle:
return 0;
case gl::TextureType::CubeMap:
return index.cubeMapFaceIndex();
case gl::TextureType::_2DArray:
case gl::TextureType::_3D:
return index.getLayerIndex();
default:
UNREACHABLE();
}
return 0;
}
GLuint GetImageCubeFaceIndexOrZeroFrom(const gl::ImageIndex &index)
{
switch (index.getType())
{
case gl::TextureType::CubeMap:
return index.cubeMapFaceIndex();
default:
break;
}
return 0;
}
// Given texture type, get texture type of one image for a glTexImage call.
// For example, for texture 2d, one image is also texture 2d.
// for texture cube, one image is texture 2d.
gl::TextureType GetTextureImageType(gl::TextureType texType)
{
switch (texType)
{
case gl::TextureType::CubeMap:
return gl::TextureType::_2D;
case gl::TextureType::_2D:
case gl::TextureType::_2DArray:
case gl::TextureType::_2DMultisample:
case gl::TextureType::_3D:
case gl::TextureType::Rectangle:
return texType;
default:
UNREACHABLE();
return gl::TextureType::InvalidEnum;
}
}
// D24X8 by default writes depth data to high 24 bits of 32 bit integers. However, Metal separate
// depth stencil blitting expects depth data to be in low 24 bits of the data.
void WriteDepthStencilToDepth24(const uint8_t *srcPtr, uint8_t *dstPtr)
{
auto src = reinterpret_cast<const angle::DepthStencil *>(srcPtr);
auto dst = reinterpret_cast<uint32_t *>(dstPtr);
*dst = gl::floatToNormalized<24, uint32_t>(static_cast<float>(src->depth));
}
#if TARGET_OS_SIMULATOR
void CopyTextureData(const MTLSize ®ionSize,
size_t srcRowPitch,
size_t src2DImageSize,
const uint8_t *psrc,
size_t destRowPitch,
size_t dest2DImageSize,
uint8_t *pdst)
{
{
size_t rowCopySize = std::min(srcRowPitch, destRowPitch);
for (NSUInteger d = 0; d < regionSize.depth; ++d)
{
for (NSUInteger r = 0; r < regionSize.height; ++r)
{
const uint8_t *pCopySrc = psrc + d * src2DImageSize + r * srcRowPitch;
uint8_t *pCopyDst = pdst + d * dest2DImageSize + r * destRowPitch;
memcpy(pCopyDst, pCopySrc, rowCopySize);
}
}
}
}
#endif // TARGET_OS_SIMULATOR
void ConvertDepthStencilData(const MTLSize ®ionSize,
const angle::Format &srcAngleFormat,
size_t srcRowPitch,
size_t src2DImageSize,
const uint8_t *psrc,
const angle::Format &dstAngleFormat,
rx::PixelWriteFunction pixelWriteFunctionOverride,
size_t destRowPitch,
size_t dest2DImageSize,
uint8_t *pdst)
{
if (srcAngleFormat.id == dstAngleFormat.id)
{
size_t rowCopySize = std::min(srcRowPitch, destRowPitch);
for (NSUInteger d = 0; d < regionSize.depth; ++d)
{
for (NSUInteger r = 0; r < regionSize.height; ++r)
{
const uint8_t *pCopySrc = psrc + d * src2DImageSize + r * srcRowPitch;
uint8_t *pCopyDst = pdst + d * dest2DImageSize + r * destRowPitch;
memcpy(pCopyDst, pCopySrc, rowCopySize);
}
}
}
else
{
rx::PixelWriteFunction pixelWriteFunction = pixelWriteFunctionOverride
? pixelWriteFunctionOverride
: dstAngleFormat.pixelWriteFunction;
// This is only for depth & stencil case.
ASSERT(srcAngleFormat.depthBits || srcAngleFormat.stencilBits);
ASSERT(srcAngleFormat.pixelReadFunction && pixelWriteFunction);
// cache to store read result of source pixel
angle::DepthStencil depthStencilData;
auto sourcePixelReadData = reinterpret_cast<uint8_t *>(&depthStencilData);
ASSERT(srcAngleFormat.pixelBytes <= sizeof(depthStencilData));
for (NSUInteger d = 0; d < regionSize.depth; ++d)
{
for (NSUInteger r = 0; r < regionSize.height; ++r)
{
for (NSUInteger c = 0; c < regionSize.width; ++c)
{
const uint8_t *sourcePixelData =
psrc + d * src2DImageSize + r * srcRowPitch + c * srcAngleFormat.pixelBytes;
uint8_t *destPixelData = pdst + d * dest2DImageSize + r * destRowPitch +
c * dstAngleFormat.pixelBytes;
srcAngleFormat.pixelReadFunction(sourcePixelData, sourcePixelReadData);
pixelWriteFunction(sourcePixelReadData, destPixelData);
}
}
}
}
}
angle::Result CopyDepthStencilTextureContentsToStagingBuffer(
ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
const angle::Format &stagingAngleFormat,
rx::PixelWriteFunction pixelWriteFunctionOverride,
const MTLSize ®ionSize,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
size_t *bufferRowPitchOut,
size_t *buffer2DImageSizeOut,
mtl::BufferRef *bufferOut)
{
size_t stagingBufferRowPitch = regionSize.width * stagingAngleFormat.pixelBytes;
size_t stagingBuffer2DImageSize = stagingBufferRowPitch * regionSize.height;
size_t stagingBufferSize = stagingBuffer2DImageSize * regionSize.depth;
mtl::BufferRef stagingBuffer;
ANGLE_TRY(mtl::Buffer::MakeBuffer(contextMtl, stagingBufferSize, nullptr, &stagingBuffer));
uint8_t *pdst = stagingBuffer->map(contextMtl);
ConvertDepthStencilData(regionSize, textureAngleFormat, bytesPerRow, bytesPer2DImage, data,
stagingAngleFormat, pixelWriteFunctionOverride, stagingBufferRowPitch,
stagingBuffer2DImageSize, pdst);
stagingBuffer->unmap(contextMtl);
*bufferOut = stagingBuffer;
*bufferRowPitchOut = stagingBufferRowPitch;
*buffer2DImageSizeOut = stagingBuffer2DImageSize;
return angle::Result::Continue;
}
#if TARGET_OS_SIMULATOR
angle::Result CopyTextureContentsToStagingBuffer(ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
const MTLSize ®ionSize,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
size_t *bufferRowPitchOut,
size_t *buffer2DImageSizeOut,
mtl::BufferRef *bufferOut)
{
size_t stagingBufferRowPitch = regionSize.width * textureAngleFormat.pixelBytes;
size_t stagingBuffer2DImageSize = stagingBufferRowPitch * regionSize.height;
size_t stagingBufferSize = stagingBuffer2DImageSize * regionSize.depth;
mtl::BufferRef stagingBuffer;
ANGLE_TRY(mtl::Buffer::MakeBuffer(contextMtl, stagingBufferSize, nullptr, &stagingBuffer));
uint8_t *pdst = stagingBuffer->map(contextMtl);
CopyTextureData(regionSize, bytesPerRow, bytesPer2DImage, data, stagingBufferRowPitch,
stagingBuffer2DImageSize, pdst);
stagingBuffer->unmap(contextMtl);
*bufferOut = stagingBuffer;
*bufferRowPitchOut = stagingBufferRowPitch;
*buffer2DImageSizeOut = stagingBuffer2DImageSize;
return angle::Result::Continue;
}
angle::Result CopyCompressedTextureContentsToStagingBuffer(ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
const MTLSize ®ionSizeInBlocks,
const uint8_t *data,
size_t bytesPerBlockRow,
size_t bytesPer2DImage,
size_t *bufferRowPitchOut,
size_t *buffer2DImageSizeOut,
mtl::BufferRef *bufferOut)
{
size_t stagingBufferRowPitch = bytesPerBlockRow;
size_t stagingBuffer2DImageSize = bytesPer2DImage;
size_t stagingBufferSize = stagingBuffer2DImageSize * regionSizeInBlocks.depth;
mtl::BufferRef stagingBuffer;
ANGLE_TRY(mtl::Buffer::MakeBuffer(contextMtl, stagingBufferSize, nullptr, &stagingBuffer));
uint8_t *pdst = stagingBuffer->map(contextMtl);
CopyTextureData(regionSizeInBlocks, bytesPerBlockRow, bytesPer2DImage, data,
stagingBufferRowPitch, stagingBuffer2DImageSize, pdst);
stagingBuffer->unmap(contextMtl);
*bufferOut = stagingBuffer;
*bufferRowPitchOut = stagingBufferRowPitch;
*buffer2DImageSizeOut = stagingBuffer2DImageSize;
return angle::Result::Continue;
}
#endif
angle::Result UploadDepthStencilTextureContentsWithStagingBuffer(
ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
MTLRegion region,
const mtl::MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
const mtl::TextureRef &texture)
{
ASSERT(texture && texture->valid());
ASSERT(!texture->isCPUAccessible());
ASSERT(!textureAngleFormat.depthBits || !textureAngleFormat.stencilBits);
// Compressed texture is not supporte atm
ASSERT(!textureAngleFormat.isBlock);
// Copy data to staging buffer
size_t stagingBufferRowPitch;
size_t stagingBuffer2DImageSize;
mtl::BufferRef stagingBuffer;
ANGLE_TRY(CopyDepthStencilTextureContentsToStagingBuffer(
contextMtl, textureAngleFormat, textureAngleFormat, textureAngleFormat.pixelWriteFunction,
region.size, data, bytesPerRow, bytesPer2DImage, &stagingBufferRowPitch,
&stagingBuffer2DImageSize, &stagingBuffer));
// Copy staging buffer to texture.
mtl::BlitCommandEncoder *encoder = contextMtl->getBlitCommandEncoder();
encoder->copyBufferToTexture(stagingBuffer, 0, stagingBufferRowPitch, stagingBuffer2DImageSize,
region.size, texture, slice, mipmapLevel, region.origin,
MTLBlitOptionNone);
return angle::Result::Continue;
}
// Packed depth stencil upload using staging buffer
angle::Result UploadPackedDepthStencilTextureContentsWithStagingBuffer(
ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
MTLRegion region,
const mtl::MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
const mtl::TextureRef &texture)
{
ASSERT(texture && texture->valid());
ASSERT(!texture->isCPUAccessible());
ASSERT(textureAngleFormat.depthBits && textureAngleFormat.stencilBits);
// We have to split the depth & stencil data into 2 buffers.
angle::FormatID stagingDepthBufferFormatId;
angle::FormatID stagingStencilBufferFormatId;
// Custom depth write function. We cannot use those in imageformats.cpp since Metal has some
// special cases.
rx::PixelWriteFunction stagingDepthBufferWriteFunctionOverride = nullptr;
switch (textureAngleFormat.id)
{
case angle::FormatID::D24_UNORM_S8_UINT:
// D24_UNORM_X8_UINT writes depth data to high 24 bits. But Metal expects depth data to
// be in low 24 bits.
stagingDepthBufferFormatId = angle::FormatID::D24_UNORM_X8_UINT;
stagingDepthBufferWriteFunctionOverride = WriteDepthStencilToDepth24;
stagingStencilBufferFormatId = angle::FormatID::S8_UINT;
break;
case angle::FormatID::D32_FLOAT_S8X24_UINT:
stagingDepthBufferFormatId = angle::FormatID::D32_FLOAT;
stagingStencilBufferFormatId = angle::FormatID::S8_UINT;
break;
default:
ANGLE_MTL_UNREACHABLE(contextMtl);
}
const angle::Format &angleStagingDepthFormat = angle::Format::Get(stagingDepthBufferFormatId);
const angle::Format &angleStagingStencilFormat =
angle::Format::Get(stagingStencilBufferFormatId);
size_t stagingDepthBufferRowPitch, stagingStencilBufferRowPitch;
size_t stagingDepthBuffer2DImageSize, stagingStencilBuffer2DImageSize;
mtl::BufferRef stagingDepthbuffer, stagingStencilBuffer;
// Copy depth data to staging depth buffer
ANGLE_TRY(CopyDepthStencilTextureContentsToStagingBuffer(
contextMtl, textureAngleFormat, angleStagingDepthFormat,
stagingDepthBufferWriteFunctionOverride, region.size, data, bytesPerRow, bytesPer2DImage,
&stagingDepthBufferRowPitch, &stagingDepthBuffer2DImageSize, &stagingDepthbuffer));
// Copy stencil data to staging stencil buffer
ANGLE_TRY(CopyDepthStencilTextureContentsToStagingBuffer(
contextMtl, textureAngleFormat, angleStagingStencilFormat, nullptr, region.size, data,
bytesPerRow, bytesPer2DImage, &stagingStencilBufferRowPitch,
&stagingStencilBuffer2DImageSize, &stagingStencilBuffer));
mtl::BlitCommandEncoder *encoder = contextMtl->getBlitCommandEncoder();
encoder->copyBufferToTexture(stagingDepthbuffer, 0, stagingDepthBufferRowPitch,
stagingDepthBuffer2DImageSize, region.size, texture, slice,
mipmapLevel, region.origin, MTLBlitOptionDepthFromDepthStencil);
encoder->copyBufferToTexture(stagingStencilBuffer, 0, stagingStencilBufferRowPitch,
stagingStencilBuffer2DImageSize, region.size, texture, slice,
mipmapLevel, region.origin, MTLBlitOptionStencilFromDepthStencil);
return angle::Result::Continue;
}
#if TARGET_OS_SIMULATOR
angle::Result UploadTextureContentsWithStagingBuffer(ContextMtl *contextMtl,
const angle::Format &textureAngleFormat,
MTLRegion region,
const mtl::MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
const mtl::TextureRef &texture)
{
ASSERT(texture && texture->valid());
angle::FormatID stagingBufferFormatID = textureAngleFormat.id;
const angle::Format &angleStagingFormat = angle::Format::Get(stagingBufferFormatID);
size_t stagingBufferRowPitch;
size_t stagingBuffer2DImageSize;
mtl::BufferRef stagingBuffer;
// Block-compressed formats need a bit of massaging for copy.
if (textureAngleFormat.isBlock)
{
GLenum internalFormat = textureAngleFormat.glInternalFormat;
const gl::InternalFormat &fmt = gl::GetSizedInternalFormatInfo(internalFormat);
MTLRegion newRegion = region;
bytesPerRow =
(region.size.width + fmt.compressedBlockWidth - 1) / fmt.compressedBlockWidth * 16;
bytesPer2DImage = (region.size.height + fmt.compressedBlockHeight - 1) /
fmt.compressedBlockHeight * bytesPerRow;
newRegion.size.width =
(region.size.width + fmt.compressedBlockWidth - 1) / fmt.compressedBlockWidth;
newRegion.size.height =
(region.size.height + fmt.compressedBlockHeight - 1) / fmt.compressedBlockHeight;
ANGLE_TRY(CopyCompressedTextureContentsToStagingBuffer(
contextMtl, angleStagingFormat, newRegion.size, data, bytesPerRow, bytesPer2DImage,
&stagingBufferRowPitch, &stagingBuffer2DImageSize, &stagingBuffer));
}
// Copy to staging buffer before uploading to texture.
else
{
ANGLE_TRY(CopyTextureContentsToStagingBuffer(
contextMtl, angleStagingFormat, region.size, data, bytesPerRow, bytesPer2DImage,
&stagingBufferRowPitch, &stagingBuffer2DImageSize, &stagingBuffer));
}
mtl::BlitCommandEncoder *encoder = contextMtl->getBlitCommandEncoder();
encoder->copyBufferToTexture(stagingBuffer, 0, stagingBufferRowPitch, stagingBuffer2DImageSize,
region.size, texture, slice, mipmapLevel, region.origin, 0);
return angle::Result::Continue;
}
#endif // TARGET_OS_SIMULATOR
angle::Result UploadTextureContents(const gl::Context *context,
const angle::Format &textureAngleFormat,
const MTLRegion ®ion,
const mtl::MipmapNativeLevel &mipmapLevel,
uint32_t slice,
const uint8_t *data,
size_t bytesPerRow,
size_t bytesPer2DImage,
const mtl::TextureRef &texture)
{
ASSERT(texture && texture->valid());
ContextMtl *contextMtl = mtl::GetImpl(context);
#if TARGET_OS_SIMULATOR
if (!textureAngleFormat.depthBits && !textureAngleFormat.stencilBits)
{
ANGLE_TRY(UploadTextureContentsWithStagingBuffer(contextMtl, textureAngleFormat, region,
mipmapLevel, slice, data, bytesPerRow,
bytesPer2DImage, texture));
return angle::Result::Continue;
}
#else
if (texture->isCPUAccessible())
{
// If texture is CPU accessible, just call replaceRegion() directly.
texture->replaceRegion(contextMtl, region, mipmapLevel, slice, data, bytesPerRow,
bytesPer2DImage);
return angle::Result::Continue;
}
#endif // TARGET_OS_SIMULATOR
ASSERT(textureAngleFormat.depthBits || textureAngleFormat.stencilBits);
// Texture is not CPU accessible, we need to use staging buffer
if (textureAngleFormat.depthBits && textureAngleFormat.stencilBits)
{
ANGLE_TRY(UploadPackedDepthStencilTextureContentsWithStagingBuffer(
contextMtl, textureAngleFormat, region, mipmapLevel, slice, data, bytesPerRow,
bytesPer2DImage, texture));
}
else
{
ANGLE_TRY(UploadDepthStencilTextureContentsWithStagingBuffer(
contextMtl, textureAngleFormat, region, mipmapLevel, slice, data, bytesPerRow,
bytesPer2DImage, texture));
}
return angle::Result::Continue;
}
// This might be unused on platform not supporting swizzle.
ANGLE_MTL_UNUSED
GLenum OverrideSwizzleValue(const gl::Context *context,
GLenum swizzle,
const mtl::Format &format,
const gl::InternalFormat &glInternalFormat)
{
if (format.actualAngleFormat().depthBits)
{
ASSERT(!format.swizzled);
if (context->getState().getClientMajorVersion() >= 3 && glInternalFormat.sized)
{
// ES 3.0 spec: treat depth texture as red texture during sampling.
if (swizzle == GL_GREEN || swizzle == GL_BLUE)
{
return GL_NONE;
}
else if (swizzle == GL_ALPHA)
{
return GL_ONE;
}
}
else
{
// https://www.khronos.org/registry/OpenGL/extensions/OES/OES_depth_texture.txt
// Treat depth texture as luminance texture during sampling.
if (swizzle == GL_GREEN || swizzle == GL_BLUE)
{
return GL_RED;
}
else if (swizzle == GL_ALPHA)
{
return GL_ONE;
}
}
}
else if (format.swizzled)
{
// Combine the swizzles
switch (swizzle)
{
case GL_RED:
return format.swizzle[0];
case GL_GREEN:
return format.swizzle[1];
case GL_BLUE:
return format.swizzle[2];
case GL_ALPHA:
return format.swizzle[3];
default:
break;
}
}
return swizzle;
}
} // namespace
// TextureMtl implementation
TextureMtl::TextureMtl(const gl::TextureState &state) : TextureImpl(state) {}
TextureMtl::~TextureMtl() = default;
void TextureMtl::onDestroy(const gl::Context *context)
{
releaseTexture(true);
}
void TextureMtl::releaseTexture(bool releaseImages)
{
releaseTexture(releaseImages, false);
}
void TextureMtl::releaseTexture(bool releaseImages, bool releaseTextureObjectsOnly)
{
if (releaseImages)
{
mTexImageDefs.clear();
}
else if (mNativeTexture)
{
// Release native texture but keep its old per face per mipmap level image views.
retainImageDefinitions();
}
mNativeTexture = nullptr;
mNativeSwizzleSamplingView = nullptr;
// Clear render target cache for each texture's image. We don't erase them because they
// might still be referenced by a framebuffer.
for (auto &sliceRenderTargets : mPerLayerRenderTargets)
{
for (RenderTargetMtl &mipRenderTarget : sliceRenderTargets.second)
{
mipRenderTarget.reset();
}
}
for (mtl::TextureRef &view : mNativeLevelViews)
{
view.reset();
}
if (!releaseTextureObjectsOnly)
{
mMetalSamplerState = nil;
mFormat = mtl::Format();
}
}
angle::Result TextureMtl::ensureTextureCreated(const gl::Context *context)
{
if (mNativeTexture)
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
// Create actual texture object:
mCurrentBaseLevel = mState.getEffectiveBaseLevel();
const GLuint mips = mState.getMipmapMaxLevel() - mCurrentBaseLevel + 1;
gl::ImageDesc desc = mState.getBaseLevelDesc();
ANGLE_MTL_CHECK(contextMtl, desc.format.valid(), GL_INVALID_OPERATION);
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(desc.format.info->sizedInternalFormat);
mFormat = contextMtl->getPixelFormat(angleFormatId);
return createNativeTexture(context, mState.getType(), mips, desc.size);
}
angle::Result TextureMtl::createNativeTexture(const gl::Context *context,
gl::TextureType type,
GLuint mips,
const gl::Extents &size)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
// Create actual texture object:
mCurrentBaseLevel = mState.getEffectiveBaseLevel();
mCurrentMaxLevel = mState.getEffectiveMaxLevel();
mSlices = 1;
int numCubeFaces = 1;
switch (type)
{
case gl::TextureType::_2D:
ANGLE_TRY(mtl::Texture::Make2DTexture(
contextMtl, mFormat, size.width, size.height, mips,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &mNativeTexture));
break;
case gl::TextureType::CubeMap:
mSlices = numCubeFaces = 6;
ANGLE_TRY(mtl::Texture::MakeCubeTexture(
contextMtl, mFormat, size.width, mips,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &mNativeTexture));
break;
case gl::TextureType::_3D:
ANGLE_TRY(mtl::Texture::Make3DTexture(
contextMtl, mFormat, size.width, size.height, size.depth, mips,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &mNativeTexture));
break;
case gl::TextureType::_2DArray:
mSlices = size.depth;
ANGLE_TRY(mtl::Texture::Make2DArrayTexture(
contextMtl, mFormat, size.width, size.height, mips, mSlices,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &mNativeTexture));
break;
default:
UNREACHABLE();
}
ANGLE_TRY(checkForEmulatedChannels(context, mFormat, mNativeTexture));
// Transfer data from images to actual texture object
mtl::BlitCommandEncoder *encoder = nullptr;
for (int face = 0; face < numCubeFaces; ++face)
{
for (mtl::MipmapNativeLevel actualMip = mtl::kZeroNativeMipLevel; actualMip.get() < mips;
++actualMip)
{
GLuint imageMipLevel = mtl::GetGLMipLevel(actualMip, mState.getEffectiveBaseLevel());
mtl::TextureRef &imageToTransfer = mTexImageDefs[face][imageMipLevel].image;
// Only transfer if this mip & slice image has been defined and in correct size &
// format.
gl::Extents actualMipSize = mNativeTexture->size(actualMip);
if (imageToTransfer && imageToTransfer->sizeAt0() == actualMipSize &&
imageToTransfer->pixelFormat() == mNativeTexture->pixelFormat())
{
if (!encoder)
{
encoder = contextMtl->getBlitCommandEncoder();
}
encoder->copyTexture(imageToTransfer, 0, mtl::kZeroNativeMipLevel, mNativeTexture,
face, actualMip, imageToTransfer->arrayLength(), 1);
// Invalidate texture image definition at this index so that we can make it a
// view of the native texture at this index later.
imageToTransfer = nullptr;
}
}
}
// Create sampler state
ANGLE_TRY(ensureSamplerStateCreated(context));
return angle::Result::Continue;
}
angle::Result TextureMtl::ensureSamplerStateCreated(const gl::Context *context)
{
if (mMetalSamplerState)
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
mtl::SamplerDesc samplerDesc(mState.getSamplerState());
if (mFormat.actualAngleFormat().depthBits && !mFormat.getCaps().filterable)
{
// On devices not supporting filtering for depth textures, we need to convert to nearest
// here.
samplerDesc.minFilter = MTLSamplerMinMagFilterNearest;
samplerDesc.magFilter = MTLSamplerMinMagFilterNearest;
if (samplerDesc.mipFilter != MTLSamplerMipFilterNotMipmapped)
{
samplerDesc.mipFilter = MTLSamplerMipFilterNearest;
}
samplerDesc.maxAnisotropy = 1;
}
if (mState.getType() == gl::TextureType::Rectangle)
{
samplerDesc.rAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDesc.sAddressMode = MTLSamplerAddressModeClampToEdge;
samplerDesc.tAddressMode = MTLSamplerAddressModeClampToEdge;
}
mMetalSamplerState = contextMtl->getDisplay()->getStateCache().getSamplerState(
contextMtl->getMetalDevice(), samplerDesc);
return angle::Result::Continue;
}
angle::Result TextureMtl::onBaseMaxLevelsChanged(const gl::Context *context)
{
if (!mNativeTexture || (mCurrentBaseLevel == mState.getEffectiveBaseLevel() &&
mCurrentMaxLevel == mState.getEffectiveMaxLevel()))
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
// Release native texture but keep old image definitions so that it can be recreated from old
// image definitions with different base level
releaseTexture(false, true);
// Tell context to rebind textures
contextMtl->invalidateCurrentTextures();
return angle::Result::Continue;
}
angle::Result TextureMtl::ensureImageCreated(const gl::Context *context,
const gl::ImageIndex &index)
{
mtl::TextureRef &image = getImage(index);
if (!image)
{
// Image at this level hasn't been defined yet. We need to define it:
const gl::ImageDesc &desc = mState.getImageDesc(index);
ANGLE_TRY(redefineImage(context, index, mFormat, desc.size));
}
return angle::Result::Continue;
}
angle::Result TextureMtl::ensureNativeLevelViewsCreated()
{
ASSERT(mNativeTexture);
const GLuint baseLevel = mState.getEffectiveBaseLevel();
for (mtl::MipmapNativeLevel mip = mtl::kZeroNativeMipLevel;
mip.get() < mNativeTexture->mipmapLevels(); ++mip)
{
if (mNativeLevelViews[mip])
{
continue;
}
if (mNativeTexture->textureType() != MTLTextureTypeCube &&
mTexImageDefs[0][mtl::GetGLMipLevel(mip, baseLevel)].image)
{
// Reuse texture image view.
mNativeLevelViews[mip] = mTexImageDefs[0][mtl::GetGLMipLevel(mip, baseLevel)].image;
}
else
{
mNativeLevelViews[mip] = mNativeTexture->createMipView(mip);
}
}
return angle::Result::Continue;
}
mtl::TextureRef TextureMtl::createImageViewFromNativeTexture(
GLuint cubeFaceOrZero,
const mtl::MipmapNativeLevel &nativeLevel)
{
mtl::TextureRef image;
if (mNativeTexture->textureType() == MTLTextureTypeCube)
{
// Cube texture's image is per face.
image = mNativeTexture->createSliceMipView(cubeFaceOrZero, nativeLevel);
}
else
{
if (mNativeLevelViews[nativeLevel])
{
// Reuse the native level view
image = mNativeLevelViews[nativeLevel];
}
else
{
image = mNativeTexture->createMipView(nativeLevel);
}
}
return image;
}
void TextureMtl::retainImageDefinitions()
{
if (!mNativeTexture)
{
return;
}
const GLuint mips = mNativeTexture->mipmapLevels();
int numCubeFaces = 1;
switch (mState.getType())
{
case gl::TextureType::CubeMap:
numCubeFaces = 6;
break;
default:
break;
}
// Create image view per cube face, per mip level
for (int face = 0; face < numCubeFaces; ++face)
{
for (mtl::MipmapNativeLevel mip = mtl::kZeroNativeMipLevel; mip.get() < mips; ++mip)
{
GLuint imageMipLevel = mtl::GetGLMipLevel(mip, mCurrentBaseLevel);
ImageDefinitionMtl &imageDef = mTexImageDefs[face][imageMipLevel];
if (imageDef.image)
{
continue;
}
imageDef.image = createImageViewFromNativeTexture(face, mip);
imageDef.formatID = mFormat.intendedFormatId;
}
}
}
bool TextureMtl::isIndexWithinMinMaxLevels(const gl::ImageIndex &imageIndex) const
{
return imageIndex.getLevelIndex() >= static_cast<GLint>(mState.getEffectiveBaseLevel()) &&
imageIndex.getLevelIndex() <= static_cast<GLint>(mState.getEffectiveMaxLevel());
}
mtl::MipmapNativeLevel TextureMtl::getNativeLevel(const gl::ImageIndex &imageIndex) const
{
int baseLevel = mState.getEffectiveBaseLevel();
return mtl::GetNativeMipLevel(imageIndex.getLevelIndex(), baseLevel);
}
mtl::TextureRef &TextureMtl::getImage(const gl::ImageIndex &imageIndex)
{
return getImageDefinition(imageIndex).image;
}
ImageDefinitionMtl &TextureMtl::getImageDefinition(const gl::ImageIndex &imageIndex)
{
GLuint cubeFaceOrZero = GetImageCubeFaceIndexOrZeroFrom(imageIndex);
ImageDefinitionMtl &imageDef = mTexImageDefs[cubeFaceOrZero][imageIndex.getLevelIndex()];
if (!imageDef.image && mNativeTexture)
{
// If native texture is already created, and the image at this index is not available,
// then create a view of native texture at this index, so that modifications of the image
// are reflected back to native texture's respective index.
if (!isIndexWithinMinMaxLevels(imageIndex))
{
// Image below base level is skipped.
return imageDef;
}
mtl::MipmapNativeLevel nativeLevel = getNativeLevel(imageIndex);
if (nativeLevel.get() >= mNativeTexture->mipmapLevels())
{
// Image outside native texture's mip levels is skipped.
return imageDef;
}
imageDef.image = createImageViewFromNativeTexture(cubeFaceOrZero, nativeLevel);
imageDef.formatID = mFormat.intendedFormatId;
}
return imageDef;
}
RenderTargetMtl &TextureMtl::getRenderTarget(const gl::ImageIndex &imageIndex)
{
ASSERT(imageIndex.getType() == gl::TextureType::_2D ||
imageIndex.getType() == gl::TextureType::Rectangle ||
imageIndex.getType() == gl::TextureType::_2DMultisample || imageIndex.hasLayer());
GLuint layer = GetImageLayerIndexFrom(imageIndex);
RenderTargetMtl &rtt = mPerLayerRenderTargets[layer][imageIndex.getLevelIndex()];
if (!rtt.getTexture())
{
// Lazy initialization of render target:
mtl::TextureRef &image = getImage(imageIndex);
if (image)
{
if (imageIndex.getType() == gl::TextureType::CubeMap)
{
// Cube map is special, the image is already the view of its layer
rtt.set(image, mtl::kZeroNativeMipLevel, 0, mFormat);
}
else
{
rtt.set(image, mtl::kZeroNativeMipLevel, layer, mFormat);
}
}
}
return rtt;
}
angle::Result TextureMtl::setImage(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
const gl::Extents &size,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
const gl::InternalFormat &dstFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
return setImageImpl(context, index, dstFormatInfo, size, format, type, unpack, unpackBuffer,
pixels);
}
angle::Result TextureMtl::setSubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
GLenum format,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, type);
return setSubImageImpl(context, index, area, formatInfo, type, unpack, unpackBuffer, pixels);
}
angle::Result TextureMtl::setCompressedImage(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
const gl::Extents &size,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
const gl::State &glState = context->getState();
gl::Buffer *unpackBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelUnpack);
return setImageImpl(context, index, formatInfo, size, internalFormat, GL_UNSIGNED_BYTE, unpack,
unpackBuffer, pixels);
}
angle::Result TextureMtl::setCompressedSubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
GLenum format,
const gl::PixelUnpackState &unpack,
size_t imageSize,
const uint8_t *pixels)
{
const gl::InternalFormat &formatInfo = gl::GetInternalFormatInfo(format, GL_UNSIGNED_BYTE);
const gl::State &glState = context->getState();
gl::Buffer *unpackBuffer = glState.getTargetBuffer(gl::BufferBinding::PixelUnpack);
return setSubImageImpl(context, index, area, formatInfo, GL_UNSIGNED_BYTE, unpack, unpackBuffer,
pixels);
}
angle::Result TextureMtl::copyImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Rectangle &sourceArea,
GLenum internalFormat,
gl::Framebuffer *source)
{
gl::Extents newImageSize(sourceArea.width, sourceArea.height, 1);
const gl::InternalFormat &internalFormatInfo =
gl::GetInternalFormatInfo(internalFormat, GL_UNSIGNED_BYTE);
ContextMtl *contextMtl = mtl::GetImpl(context);
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(internalFormatInfo.sizedInternalFormat);
const mtl::Format &mtlFormat = contextMtl->getPixelFormat(angleFormatId);
FramebufferMtl *srcFramebufferMtl = mtl::GetImpl(source);
RenderTargetMtl *srcReadRT = srcFramebufferMtl->getColorReadRenderTarget(context);
RenderTargetMtl colorReadRT;
if (srcReadRT)
{
// Need to duplicate RenderTargetMtl since the srcReadRT would be invalidated in
// redefineImage(). This can happen if the source and this texture are the same texture.
// Duplication ensures the copyImage() will be able to proceed even if the source texture
// will be redefined.
colorReadRT.duplicateFrom(*srcReadRT);
}
ANGLE_TRY(redefineImage(context, index, mtlFormat, newImageSize));
gl::Extents fbSize = source->getReadColorAttachment()->getSize();
gl::Rectangle fbRect(0, 0, fbSize.width, fbSize.height);
if (context->isWebGL() && !fbRect.encloses(sourceArea))
{
ANGLE_TRY(initializeContents(context, GL_NONE, index));
}
return copySubImageImpl(context, index, gl::Offset(0, 0, 0), sourceArea, internalFormatInfo,
srcFramebufferMtl, &colorReadRT);
}
angle::Result TextureMtl::copySubImage(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
gl::Framebuffer *source)
{
const gl::InternalFormat ¤tFormat = *mState.getImageDesc(index).format.info;
FramebufferMtl *srcFramebufferMtl = mtl::GetImpl(source);
RenderTargetMtl *colorReadRT = srcFramebufferMtl->getColorReadRenderTarget(context);
return copySubImageImpl(context, index, destOffset, sourceArea, currentFormat,
srcFramebufferMtl, colorReadRT);
}
angle::Result TextureMtl::copyTexture(const gl::Context *context,
const gl::ImageIndex &index,
GLenum internalFormat,
GLenum type,
GLint sourceLevel,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
const gl::ImageDesc &sourceImageDesc = source->getTextureState().getImageDesc(
NonCubeTextureTypeToTarget(source->getType()), sourceLevel);
const gl::InternalFormat &internalFormatInfo = gl::GetInternalFormatInfo(internalFormat, type);
// Only 2D textures are supported.
ASSERT(sourceImageDesc.size.depth == 1);
ContextMtl *contextMtl = mtl::GetImpl(context);
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(internalFormatInfo.sizedInternalFormat);
const mtl::Format &mtlFormat = contextMtl->getPixelFormat(angleFormatId);
ANGLE_TRY(redefineImage(context, index, mtlFormat, sourceImageDesc.size));
return copySubTextureImpl(
context, index, gl::Offset(0, 0, 0), internalFormatInfo, sourceLevel,
gl::Box(0, 0, 0, sourceImageDesc.size.width, sourceImageDesc.size.height, 1), unpackFlipY,
unpackPremultiplyAlpha, unpackUnmultiplyAlpha, source);
}
angle::Result TextureMtl::copySubTexture(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
GLint sourceLevel,
const gl::Box &sourceBox,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
const gl::InternalFormat ¤tFormat = *mState.getImageDesc(index).format.info;
return copySubTextureImpl(context, index, destOffset, currentFormat, sourceLevel, sourceBox,
unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha, source);
}
angle::Result TextureMtl::copyCompressedTexture(const gl::Context *context,
const gl::Texture *source)
{
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result TextureMtl::setStorage(const gl::Context *context,
gl::TextureType type,
size_t mipmaps,
GLenum internalFormat,
const gl::Extents &size)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
const gl::InternalFormat &formatInfo = gl::GetSizedInternalFormatInfo(internalFormat);
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(formatInfo.sizedInternalFormat);
const mtl::Format &mtlFormat = contextMtl->getPixelFormat(angleFormatId);
return setStorageImpl(context, type, mipmaps, mtlFormat, size);
}
angle::Result TextureMtl::setStorageExternalMemory(const gl::Context *context,
gl::TextureType type,
size_t levels,
GLenum internalFormat,
const gl::Extents &size,
gl::MemoryObject *memoryObject,
GLuint64 offset,
GLbitfield createFlags,
GLbitfield usageFlags,
const void *imageCreateInfoPNext)
{
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result TextureMtl::setStorageMultisample(const gl::Context *context,
gl::TextureType type,
GLsizei samples,
GLint internalformat,
const gl::Extents &size,
bool fixedSampleLocations)
{
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result TextureMtl::setEGLImageTarget(const gl::Context *context,
gl::TextureType type,
egl::Image *image)
{
releaseTexture(true);
ContextMtl *contextMtl = mtl::GetImpl(context);
ImageMtl *imageMtl = mtl::GetImpl(image);
if (type != imageMtl->getImageTextureType())
{
return angle::Result::Stop;
}
mNativeTexture = imageMtl->getTexture();
const angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(image->getFormat().info->sizedInternalFormat);
mFormat = contextMtl->getPixelFormat(angleFormatId);
mSlices = mNativeTexture->cubeFacesOrArrayLength();
ANGLE_TRY(ensureSamplerStateCreated(context));
// Tell context to rebind textures
contextMtl->invalidateCurrentTextures();
return angle::Result::Continue;
}
angle::Result TextureMtl::setImageExternal(const gl::Context *context,
gl::TextureType type,
egl::Stream *stream,
const egl::Stream::GLTextureDescription &desc)
{
UNIMPLEMENTED();
return angle::Result::Stop;
}
angle::Result TextureMtl::generateMipmap(const gl::Context *context)
{
ANGLE_TRY(ensureTextureCreated(context));
ContextMtl *contextMtl = mtl::GetImpl(context);
if (!mNativeTexture)
{
return angle::Result::Continue;
}
const mtl::FormatCaps &caps = mFormat.getCaps();
//
bool sRGB = mFormat.actualInternalFormat().colorEncoding == GL_SRGB;
bool avoidGPUPath =
contextMtl->getDisplay()->getFeatures().forceNonCSBaseMipmapGeneration.enabled &&
mNativeTexture->widthAt0() < 5;
if (!avoidGPUPath && caps.writable && mState.getType() == gl::TextureType::_3D)
{
// http://anglebug.com/4921.
// Use compute for 3D mipmap generation.
ANGLE_TRY(ensureNativeLevelViewsCreated());
ANGLE_TRY(contextMtl->getDisplay()->getUtils().generateMipmapCS(contextMtl, mNativeTexture,
sRGB, &mNativeLevelViews));
}
else if (!avoidGPUPath && caps.filterable && caps.colorRenderable)
{
mtl::BlitCommandEncoder *blitEncoder = contextMtl->getBlitCommandEncoder();
blitEncoder->generateMipmapsForTexture(mNativeTexture);
}
else
{
ANGLE_TRY(generateMipmapCPU(context));
}
return angle::Result::Continue;
}
angle::Result TextureMtl::generateMipmapCPU(const gl::Context *context)
{
ASSERT(mNativeTexture && mNativeTexture->valid());
ContextMtl *contextMtl = mtl::GetImpl(context);
const angle::Format &angleFormat = mFormat.actualAngleFormat();
// This format must have mip generation function.
ANGLE_MTL_TRY(contextMtl, angleFormat.mipGenerationFunction);
for (uint32_t slice = 0; slice < mSlices; ++slice)
{
mtl::MipmapNativeLevel maxMipLevel =
mtl::GetNativeMipLevel(mNativeTexture->mipmapLevels() - 1, 0);
const mtl::MipmapNativeLevel firstLevel = mtl::kZeroNativeMipLevel;
uint32_t prevLevelWidth = mNativeTexture->widthAt0();
uint32_t prevLevelHeight = mNativeTexture->heightAt0();
uint32_t prevLevelDepth = mNativeTexture->depthAt0();
size_t prevLevelRowPitch = angleFormat.pixelBytes * prevLevelWidth;
size_t prevLevelDepthPitch = prevLevelRowPitch * prevLevelHeight;
std::unique_ptr<uint8_t[]> prevLevelData(new (std::nothrow)
uint8_t[prevLevelDepthPitch * prevLevelDepth]);
ANGLE_CHECK_GL_ALLOC(contextMtl, prevLevelData);
std::unique_ptr<uint8_t[]> dstLevelData;
// Download base level data
mNativeTexture->getBytes(
contextMtl, prevLevelRowPitch, prevLevelDepthPitch,
MTLRegionMake3D(0, 0, 0, prevLevelWidth, prevLevelHeight, prevLevelDepth), firstLevel,
slice, prevLevelData.get());
for (mtl::MipmapNativeLevel mip = firstLevel + 1; mip <= maxMipLevel; ++mip)
{
uint32_t dstWidth = mNativeTexture->width(mip);
uint32_t dstHeight = mNativeTexture->height(mip);
uint32_t dstDepth = mNativeTexture->depth(mip);
size_t dstRowPitch = angleFormat.pixelBytes * dstWidth;
size_t dstDepthPitch = dstRowPitch * dstHeight;
size_t dstDataSize = dstDepthPitch * dstDepth;
if (!dstLevelData)
{
// Allocate once and reuse the buffer
dstLevelData.reset(new (std::nothrow) uint8_t[dstDataSize]);
ANGLE_CHECK_GL_ALLOC(contextMtl, dstLevelData);
}
// Generate mip level
angleFormat.mipGenerationFunction(
prevLevelWidth, prevLevelHeight, 1, prevLevelData.get(), prevLevelRowPitch,
prevLevelDepthPitch, dstLevelData.get(), dstRowPitch, dstDepthPitch);
// Upload to texture
ANGLE_TRY(UploadTextureContents(
context, angleFormat, MTLRegionMake3D(0, 0, 0, dstWidth, dstHeight, dstDepth), mip,
slice, dstLevelData.get(), dstRowPitch, dstDepthPitch, mNativeTexture));
prevLevelWidth = dstWidth;
prevLevelHeight = dstHeight;
prevLevelDepth = dstDepth;
prevLevelRowPitch = dstRowPitch;
prevLevelDepthPitch = dstDepthPitch;
std::swap(prevLevelData, dstLevelData);
} // for mip level
} // For layers
return angle::Result::Continue;
}
angle::Result TextureMtl::setBaseLevel(const gl::Context *context, GLuint baseLevel)
{
return onBaseMaxLevelsChanged(context);
}
angle::Result TextureMtl::bindTexImage(const gl::Context *context, egl::Surface *surface)
{
releaseTexture(true);
auto pBuffer = GetImplAs<OffscreenSurfaceMtl>(surface);
mNativeTexture = pBuffer->getColorTexture();
mFormat = pBuffer->getColorFormat();
ANGLE_TRY(ensureSamplerStateCreated(context));
// Tell context to rebind textures
ContextMtl *contextMtl = mtl::GetImpl(context);
contextMtl->invalidateCurrentTextures();
return angle::Result::Continue;
}
angle::Result TextureMtl::releaseTexImage(const gl::Context *context)
{
releaseTexture(true);
return angle::Result::Continue;
}
angle::Result TextureMtl::getAttachmentRenderTarget(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &imageIndex,
GLsizei samples,
FramebufferAttachmentRenderTarget **rtOut)
{
ANGLE_TRY(ensureTextureCreated(context));
ContextMtl *contextMtl = mtl::GetImpl(context);
ANGLE_MTL_TRY(contextMtl, mNativeTexture);
*rtOut = &getRenderTarget(imageIndex);
return angle::Result::Continue;
}
angle::Result TextureMtl::syncState(const gl::Context *context,
const gl::Texture::DirtyBits &dirtyBits,
gl::Command source)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
for (size_t dirtyBit : dirtyBits)
{
switch (dirtyBit)
{
case gl::Texture::DIRTY_BIT_COMPARE_MODE:
case gl::Texture::DIRTY_BIT_COMPARE_FUNC:
// Tell context to rebind textures so that ProgramMtl has a chance to verify
// depth texture compare mode.
contextMtl->invalidateCurrentTextures();
// fall through
OS_FALLTHROUGH;
case gl::Texture::DIRTY_BIT_MIN_FILTER:
case gl::Texture::DIRTY_BIT_MAG_FILTER:
case gl::Texture::DIRTY_BIT_WRAP_S:
case gl::Texture::DIRTY_BIT_WRAP_T:
case gl::Texture::DIRTY_BIT_WRAP_R:
case gl::Texture::DIRTY_BIT_MAX_ANISOTROPY:
case gl::Texture::DIRTY_BIT_MIN_LOD:
case gl::Texture::DIRTY_BIT_MAX_LOD:
case gl::Texture::DIRTY_BIT_SRGB_DECODE:
case gl::Texture::DIRTY_BIT_BORDER_COLOR:
// Recreate sampler state
mMetalSamplerState = nil;
break;
case gl::Texture::DIRTY_BIT_MAX_LEVEL:
case gl::Texture::DIRTY_BIT_BASE_LEVEL:
ANGLE_TRY(onBaseMaxLevelsChanged(context));
break;
case gl::Texture::DIRTY_BIT_SWIZZLE_RED:
case gl::Texture::DIRTY_BIT_SWIZZLE_GREEN:
case gl::Texture::DIRTY_BIT_SWIZZLE_BLUE:
case gl::Texture::DIRTY_BIT_SWIZZLE_ALPHA:
{
// Recreate swizzle view.
mNativeSwizzleSamplingView = nullptr;
}
break;
default:
break;
}
}
ANGLE_TRY(ensureTextureCreated(context));
ANGLE_TRY(ensureSamplerStateCreated(context));
return angle::Result::Continue;
}
angle::Result TextureMtl::bindToShader(const gl::Context *context,
mtl::RenderCommandEncoder *cmdEncoder,
gl::ShaderType shaderType,
gl::Sampler *sampler,
int textureSlotIndex,
int samplerSlotIndex)
{
ASSERT(mNativeTexture);
float minLodClamp;
float maxLodClamp;
id<MTLSamplerState> samplerState;
if (!mNativeSwizzleSamplingView)
{
#if ANGLE_MTL_SWIZZLE_AVAILABLE
ContextMtl *contextMtl = mtl::GetImpl(context);
if ((mState.getSwizzleState().swizzleRequired() || mFormat.actualAngleFormat().depthBits ||
mFormat.swizzled) &&
contextMtl->getDisplay()->getFeatures().hasTextureSwizzle.enabled)
{
const gl::InternalFormat &glInternalFormat = *mState.getBaseLevelDesc().format.info;
MTLTextureSwizzleChannels swizzle = MTLTextureSwizzleChannelsMake(
mtl::GetTextureSwizzle(OverrideSwizzleValue(
context, mState.getSwizzleState().swizzleRed, mFormat, glInternalFormat)),
mtl::GetTextureSwizzle(OverrideSwizzleValue(
context, mState.getSwizzleState().swizzleGreen, mFormat, glInternalFormat)),
mtl::GetTextureSwizzle(OverrideSwizzleValue(
context, mState.getSwizzleState().swizzleBlue, mFormat, glInternalFormat)),
mtl::GetTextureSwizzle(OverrideSwizzleValue(
context, mState.getSwizzleState().swizzleAlpha, mFormat, glInternalFormat)));
mNativeSwizzleSamplingView = mNativeTexture->createSwizzleView(swizzle);
}
else
#endif // ANGLE_MTL_SWIZZLE_AVAILABLE
{
mNativeSwizzleSamplingView = mNativeTexture;
}
}
if (!sampler)
{
samplerState = mMetalSamplerState;
minLodClamp = mState.getSamplerState().getMinLod();
maxLodClamp = mState.getSamplerState().getMaxLod();
}
else
{
SamplerMtl *samplerMtl = mtl::GetImpl(sampler);
samplerState = samplerMtl->getSampler(mtl::GetImpl(context));
minLodClamp = sampler->getSamplerState().getMinLod();
maxLodClamp = sampler->getSamplerState().getMaxLod();
}
minLodClamp = std::max(minLodClamp, 0.f);
cmdEncoder->setTexture(shaderType, mNativeSwizzleSamplingView, textureSlotIndex);
cmdEncoder->setSamplerState(shaderType, samplerState, minLodClamp, maxLodClamp,
samplerSlotIndex);
return angle::Result::Continue;
}
angle::Result TextureMtl::redefineImage(const gl::Context *context,
const gl::ImageIndex &index,
const mtl::Format &mtlFormat,
const gl::Extents &size)
{
bool imageWithinLevelRange = false;
if (isIndexWithinMinMaxLevels(index) && mNativeTexture && mNativeTexture->valid())
{
imageWithinLevelRange = true;
mtl::MipmapNativeLevel nativeLevel = getNativeLevel(index);
// Calculate the expected size for the index we are defining. If the size is different
// from the given size, or the format is different, we are redefining the image so we
// must release it.
bool typeChanged = mNativeTexture->textureType() != mtl::GetTextureType(index.getType());
if (mFormat != mtlFormat || size != mNativeTexture->size(nativeLevel) || typeChanged)
{
// Keep other images data if texture type hasn't been changed.
releaseTexture(typeChanged);
}
}
// Early-out on empty textures, don't create a zero-sized storage.
if (size.empty())
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
// Cache last defined image format:
mFormat = mtlFormat;
ImageDefinitionMtl &imageDef = getImageDefinition(index);
// If native texture still exists, it means the size hasn't been changed, no need to create new
// image
if (mNativeTexture && imageDef.image && imageWithinLevelRange)
{
ASSERT(imageDef.image->textureType() ==
mtl::GetTextureType(GetTextureImageType(index.getType())) &&
imageDef.formatID == mFormat.intendedFormatId && imageDef.image->sizeAt0() == size);
}
else
{
imageDef.formatID = mtlFormat.intendedFormatId;
// Create image to hold texture's data at this level & slice:
switch (index.getType())
{
case gl::TextureType::_2D:
case gl::TextureType::CubeMap:
ANGLE_TRY(mtl::Texture::Make2DTexture(
contextMtl, mtlFormat, size.width, size.height, 1,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &imageDef.image));
break;
case gl::TextureType::_3D:
ANGLE_TRY(mtl::Texture::Make3DTexture(
contextMtl, mtlFormat, size.width, size.height, size.depth, 1,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &imageDef.image));
break;
case gl::TextureType::_2DArray:
ANGLE_TRY(mtl::Texture::Make2DArrayTexture(
contextMtl, mtlFormat, size.width, size.height, 1, size.depth,
/** renderTargetOnly */ false,
/** allowFormatView */ mFormat.hasDepthAndStencilBits(), &imageDef.image));
break;
default:
UNREACHABLE();
}
}
// Make sure emulated channels are properly initialized
ANGLE_TRY(checkForEmulatedChannels(context, mtlFormat, imageDef.image));
// Tell context to rebind textures
contextMtl->invalidateCurrentTextures();
return angle::Result::Continue;
}
// If mipmaps = 0, this function will create full mipmaps texture.
angle::Result TextureMtl::setStorageImpl(const gl::Context *context,
gl::TextureType type,
size_t mipmaps,
const mtl::Format &mtlFormat,
const gl::Extents &size)
{
// Don't need to hold old images data.
releaseTexture(true);
ContextMtl *contextMtl = mtl::GetImpl(context);
// Tell context to rebind textures
contextMtl->invalidateCurrentTextures();
mFormat = mtlFormat;
// Texture will be created later in ensureTextureCreated()
return angle::Result::Continue;
}
angle::Result TextureMtl::setImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::InternalFormat &dstFormatInfo,
const gl::Extents &size,
GLenum srcFormat,
GLenum srcType,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *pixels)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
angle::FormatID angleFormatId =
angle::Format::InternalFormatToID(dstFormatInfo.sizedInternalFormat);
const mtl::Format &mtlFormat = contextMtl->getPixelFormat(angleFormatId);
ANGLE_TRY(redefineImage(context, index, mtlFormat, size));
// Early-out on empty textures, don't create a zero-sized storage.
if (size.empty())
{
return angle::Result::Continue;
}
// Format of the supplied pixels.
const gl::InternalFormat *srcFormatInfo;
if (srcFormat != dstFormatInfo.format || srcType != dstFormatInfo.type)
{
srcFormatInfo = &gl::GetInternalFormatInfo(srcFormat, srcType);
}
else
{
srcFormatInfo = &dstFormatInfo;
}
return setSubImageImpl(context, index, gl::Box(0, 0, 0, size.width, size.height, size.depth),
*srcFormatInfo, srcType, unpack, unpackBuffer, pixels);
}
angle::Result TextureMtl::setSubImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Box &area,
const gl::InternalFormat &formatInfo,
GLenum type,
const gl::PixelUnpackState &unpack,
gl::Buffer *unpackBuffer,
const uint8_t *oriPixels)
{
if (!oriPixels && !unpackBuffer)
{
return angle::Result::Continue;
}
ContextMtl *contextMtl = mtl::GetImpl(context);
ANGLE_TRY(ensureImageCreated(context, index));
mtl::TextureRef &image = getImage(index);
GLuint sourceRowPitch = 0;
GLuint sourceDepthPitch = 0;
GLuint sourceSkipBytes = 0;
ANGLE_CHECK_GL_MATH(contextMtl, formatInfo.computeRowPitch(type, area.width, unpack.alignment,
unpack.rowLength, &sourceRowPitch));
ANGLE_CHECK_GL_MATH(
contextMtl, formatInfo.computeDepthPitch(area.height, unpack.imageHeight, sourceRowPitch,
&sourceDepthPitch));
ANGLE_CHECK_GL_MATH(contextMtl,
formatInfo.computeSkipBytes(type, sourceRowPitch, sourceDepthPitch, unpack,
index.usesTex3D(), &sourceSkipBytes));
// Check if partial image update is supported for this format
if (!formatInfo.supportSubImage())
{
// area must be the whole mip level
sourceRowPitch = 0;
gl::Extents size = image->sizeAt0();
if (area.x != 0 || area.y != 0 || area.width != size.width || area.height != size.height)
{
ANGLE_MTL_CHECK(contextMtl, false, GL_INVALID_OPERATION);
}
}
// Get corresponding source data's ANGLE format
angle::FormatID srcAngleFormatId;
if (formatInfo.sizedInternalFormat == GL_DEPTH_COMPONENT24)
{
// GL_DEPTH_COMPONENT24 is special case, its supplied data is 32 bit depth.
srcAngleFormatId = angle::FormatID::D32_UNORM;
}
else
{
srcAngleFormatId = angle::Format::InternalFormatToID(formatInfo.sizedInternalFormat);
}
const angle::Format &srcAngleFormat = angle::Format::Get(srcAngleFormatId);
const uint8_t *usablePixels = oriPixels + sourceSkipBytes;
// Upload to texture
if (index.getType() == gl::TextureType::_2DArray)
{
// OpenGL unifies texture array and texture 3d's box area by using z & depth as array start
// index & length for texture array. However, Metal treats them differently. We need to
// handle them in separate code.
MTLRegion mtlRegion = MTLRegionMake3D(area.x, area.y, 0, area.width, area.height, 1);
for (int slice = 0; slice < area.depth; ++slice)
{
int sliceIndex = slice + area.z;
const uint8_t *srcPixels = usablePixels + slice * sourceDepthPitch;
ANGLE_TRY(setPerSliceSubImage(context, sliceIndex, mtlRegion, formatInfo, type,
srcAngleFormat, sourceRowPitch, sourceDepthPitch,
unpackBuffer, srcPixels, image));
}
}
else
{
MTLRegion mtlRegion =
MTLRegionMake3D(area.x, area.y, area.z, area.width, area.height, area.depth);
ANGLE_TRY(setPerSliceSubImage(context, 0, mtlRegion, formatInfo, type, srcAngleFormat,
sourceRowPitch, sourceDepthPitch, unpackBuffer, usablePixels,
image));
}
return angle::Result::Continue;
}
angle::Result TextureMtl::setPerSliceSubImage(const gl::Context *context,
int slice,
const MTLRegion &mtlArea,
const gl::InternalFormat &internalFormat,
GLenum type,
const angle::Format &pixelsAngleFormat,
size_t pixelsRowPitch,
size_t pixelsDepthPitch,
gl::Buffer *unpackBuffer,
const uint8_t *pixels,
const mtl::TextureRef &image)
{
// If source pixels are luminance or RGB8, we need to convert them to RGBA
if (mFormat.needConversion(pixelsAngleFormat.id))
{
return convertAndSetPerSliceSubImage(context, slice, mtlArea, internalFormat, type,
pixelsAngleFormat, pixelsRowPitch, pixelsDepthPitch,
unpackBuffer, pixels, image);
}
// No conversion needed.
ContextMtl *contextMtl = mtl::GetImpl(context);
if (unpackBuffer)
{
uintptr_t offset = reinterpret_cast<uintptr_t>(pixels);
GLuint minRowPitch;
ANGLE_CHECK_GL_MATH(contextMtl, internalFormat.computeRowPitch(
type, static_cast<GLint>(mtlArea.size.width),
/** aligment */ 1, /** rowLength */ 0, &minRowPitch));
if (offset % mFormat.actualAngleFormat().pixelBytes || pixelsRowPitch < minRowPitch)
{
// offset is not divisible by pixelByte or the source row pitch is smaller than minimum
// row pitch, use convertAndSetPerSliceSubImage() function.
return convertAndSetPerSliceSubImage(context, slice, mtlArea, internalFormat, type,
pixelsAngleFormat, pixelsRowPitch,
pixelsDepthPitch, unpackBuffer, pixels, image);
}
BufferMtl *unpackBufferMtl = mtl::GetImpl(unpackBuffer);
if (mFormat.hasDepthAndStencilBits())
{
// NOTE(hqle): packed depth & stencil texture cannot copy from buffer directly, needs
// to split its depth & stencil data and copy separately.
const uint8_t *clientData = unpackBufferMtl->getClientShadowCopyData(contextMtl);
clientData += offset;
ANGLE_TRY(UploadTextureContents(context, mFormat.actualAngleFormat(), mtlArea,
mtl::kZeroNativeMipLevel, slice, clientData,
pixelsRowPitch, pixelsDepthPitch, image));
}
else
{
// Use blit encoder to copy
mtl::BlitCommandEncoder *blitEncoder = contextMtl->getBlitCommandEncoder();
blitEncoder->copyBufferToTexture(
unpackBufferMtl->getCurrentBuffer(), offset, pixelsRowPitch, pixelsDepthPitch,
mtlArea.size, image, slice, mtl::kZeroNativeMipLevel, mtlArea.origin,
mFormat.isPVRTC() ? mtl::kBlitOptionRowLinearPVRTC : MTLBlitOptionNone);
}
}
else
{
// Upload texture data directly
ANGLE_TRY(UploadTextureContents(context, mFormat.actualAngleFormat(), mtlArea,
mtl::kZeroNativeMipLevel, slice, pixels, pixelsRowPitch,
pixelsDepthPitch, image));
}
return angle::Result::Continue;
}
angle::Result TextureMtl::convertAndSetPerSliceSubImage(const gl::Context *context,
int slice,
const MTLRegion &mtlArea,
const gl::InternalFormat &internalFormat,
GLenum type,
const angle::Format &pixelsAngleFormat,
size_t pixelsRowPitch,
size_t pixelsDepthPitch,
gl::Buffer *unpackBuffer,
const uint8_t *pixels,
const mtl::TextureRef &image)
{
ASSERT(image && image->valid());
ContextMtl *contextMtl = mtl::GetImpl(context);
if (unpackBuffer)
{
ANGLE_MTL_CHECK(contextMtl,
reinterpret_cast<uintptr_t>(pixels) <= std::numeric_limits<uint32_t>::max(),
GL_INVALID_OPERATION);
uint32_t offset = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(pixels));
BufferMtl *unpackBufferMtl = mtl::GetImpl(unpackBuffer);
if (!mFormat.getCaps().writable || mFormat.hasDepthOrStencilBits() ||
mFormat.intendedAngleFormat().isBlock)
{
// Unsupported format, use CPU path.
const uint8_t *clientData = unpackBufferMtl->getClientShadowCopyData(contextMtl);
clientData += offset;
ANGLE_TRY(convertAndSetPerSliceSubImage(context, slice, mtlArea, internalFormat, type,
pixelsAngleFormat, pixelsRowPitch,
pixelsDepthPitch, nullptr, clientData, image));
}
else
{
// Use compute shader
mtl::CopyPixelsFromBufferParams params;
params.buffer = unpackBufferMtl->getCurrentBuffer();
params.bufferStartOffset = offset;
params.bufferRowPitch = static_cast<uint32_t>(pixelsRowPitch);
params.bufferDepthPitch = static_cast<uint32_t>(pixelsDepthPitch);
params.texture = image;
params.textureArea = mtl::MTLRegionToGLBox(mtlArea);
// If texture is not array, slice must be zero, if texture is array, mtlArea.origin.z
// must be zero.
// This is because this function uses Metal convention: where slice is only used for
// array textures, and z layer of mtlArea.origin is only used for 3D textures.
ASSERT(slice == 0 || params.textureArea.z == 0);
// For mtl::RenderUtils we convert to OpenGL convention: z layer is used as either array
// texture's slice or 3D texture's layer index.
params.textureArea.z += slice;
ANGLE_TRY(contextMtl->getDisplay()->getUtils().unpackPixelsFromBufferToTexture(
contextMtl, pixelsAngleFormat, params));
}
} // if (unpackBuffer)
else
{
LoadImageFunctionInfo loadFunctionInfo = mFormat.textureLoadFunctions
? mFormat.textureLoadFunctions(type)
: LoadImageFunctionInfo();
const angle::Format &dstFormat = angle::Format::Get(mFormat.actualFormatId);
const size_t dstRowPitch = dstFormat.pixelBytes * mtlArea.size.width;
// Check if original image data is compressed:
if (mFormat.intendedAngleFormat().isBlock)
{
if (mFormat.intendedFormatId != mFormat.actualFormatId)
{
ASSERT(loadFunctionInfo.loadFunction);
// Need to create a buffer to hold entire decompressed image.
const size_t dstDepthPitch = dstRowPitch * mtlArea.size.height;
angle::MemoryBuffer decompressBuf;
ANGLE_CHECK_GL_ALLOC(contextMtl,
decompressBuf.resize(dstDepthPitch * mtlArea.size.depth));
// Decompress
loadFunctionInfo.loadFunction(mtlArea.size.width, mtlArea.size.height,
mtlArea.size.depth, pixels, pixelsRowPitch,
pixelsDepthPitch, decompressBuf.data(), dstRowPitch,
dstDepthPitch);
// Upload to texture
ANGLE_TRY(UploadTextureContents(
context, dstFormat, mtlArea, mtl::kZeroNativeMipLevel, slice,
decompressBuf.data(), dstRowPitch, dstDepthPitch, image));
}
else
{
// Assert that we're filling the level in it's entierety.
ASSERT(mtlArea.size.width == static_cast<unsigned int>(image->sizeAt0().width));
ASSERT(mtlArea.size.height == static_cast<unsigned int>(image->sizeAt0().height));
const size_t dstDepthPitch = dstRowPitch * mtlArea.size.height;
ANGLE_TRY(UploadTextureContents(context, dstFormat, mtlArea,
mtl::kZeroNativeMipLevel, slice, pixels,
dstRowPitch, dstDepthPitch, image));
}
} // if (mFormat.intendedAngleFormat().isBlock)
else
{
// Create scratch row buffer
angle::MemoryBuffer conversionRow;
ANGLE_CHECK_GL_ALLOC(contextMtl, conversionRow.resize(dstRowPitch));
// Convert row by row:
MTLRegion mtlRow = mtlArea;
mtlRow.size.height = mtlRow.size.depth = 1;
for (NSUInteger d = 0; d < mtlArea.size.depth; ++d)
{
mtlRow.origin.z = mtlArea.origin.z + d;
for (NSUInteger r = 0; r < mtlArea.size.height; ++r)
{
const uint8_t *psrc = pixels + d * pixelsDepthPitch + r * pixelsRowPitch;
mtlRow.origin.y = mtlArea.origin.y + r;
// Convert pixels
if (loadFunctionInfo.loadFunction)
{
loadFunctionInfo.loadFunction(mtlRow.size.width, 1, 1, psrc, pixelsRowPitch,
0, conversionRow.data(), dstRowPitch, 0);
}
else if (mFormat.hasDepthOrStencilBits())
{
ConvertDepthStencilData(mtlRow.size, pixelsAngleFormat, pixelsRowPitch, 0,
psrc, dstFormat, nullptr, dstRowPitch, 0,
conversionRow.data());
}
else
{
CopyImageCHROMIUM(psrc, pixelsRowPitch, pixelsAngleFormat.pixelBytes, 0,
pixelsAngleFormat.pixelReadFunction, conversionRow.data(),
dstRowPitch, dstFormat.pixelBytes, 0,
dstFormat.pixelWriteFunction, internalFormat.format,
dstFormat.componentType, mtlRow.size.width, 1, 1, false,
false, false);
}
// Upload to texture
ANGLE_TRY(UploadTextureContents(context, dstFormat, mtlRow,
mtl::kZeroNativeMipLevel, slice,
conversionRow.data(), dstRowPitch, 0, image));
}
}
} // if (mFormat.intendedAngleFormat().isBlock)
} // if (unpackBuffer)
return angle::Result::Continue;
}
angle::Result TextureMtl::checkForEmulatedChannels(const gl::Context *context,
const mtl::Format &mtlFormat,
const mtl::TextureRef &texture)
{
bool emulatedChannels = mtl::IsFormatEmulated(mtlFormat);
// For emulated channels that GL texture intends to not have,
// we need to initialize their content.
if (emulatedChannels)
{
uint32_t mipmaps = texture->mipmapLevels();
uint32_t layers = texture->cubeFacesOrArrayLength();
for (uint32_t layer = 0; layer < layers; ++layer)
{
for (uint32_t mip = 0; mip < mipmaps; ++mip)
{
auto index = mtl::ImageNativeIndex::FromBaseZeroGLIndex(
GetCubeOrArraySliceMipIndex(texture, layer, mip));
ANGLE_TRY(mtl::InitializeTextureContents(context, texture, mtlFormat, index));
}
}
}
return angle::Result::Continue;
}
angle::Result TextureMtl::initializeContents(const gl::Context *context,
GLenum binding,
const gl::ImageIndex &index)
{
if (index.isLayered())
{
// InitializeTextureContents is only able to initialize one layer at a time.
const gl::ImageDesc &desc = mState.getImageDesc(index);
uint32_t layerCount;
if (index.isEntireLevelCubeMap())
{
layerCount = 6;
}
else
{
layerCount = desc.size.depth;
}
gl::ImageIndexIterator ite = index.getLayerIterator(layerCount);
while (ite.hasNext())
{
gl::ImageIndex layerIndex = ite.next();
ANGLE_TRY(initializeContents(context, GL_NONE, layerIndex));
}
return angle::Result::Continue;
}
else if (index.getLayerCount() > 1)
{
for (int layer = 0; layer < index.getLayerCount(); ++layer)
{
int layerIdx = layer + index.getLayerIndex();
gl::ImageIndex layerIndex =
gl::ImageIndex::MakeFromType(index.getType(), index.getLevelIndex(), layerIdx);
ANGLE_TRY(initializeContents(context, GL_NONE, layerIndex));
}
return angle::Result::Continue;
}
ASSERT(index.getLayerCount() == 1 && !index.isLayered());
ANGLE_TRY(ensureImageCreated(context, index));
ContextMtl *contextMtl = mtl::GetImpl(context);
ImageDefinitionMtl &imageDef = getImageDefinition(index);
const mtl::TextureRef &image = imageDef.image;
const mtl::Format &format = contextMtl->getPixelFormat(imageDef.formatID);
// For Texture's image definition, we always use zero mip level.
if (format.metalFormat == MTLPixelFormatInvalid)
{
return angle::Result::Stop;
}
return mtl::InitializeTextureContents(
context, image, format,
mtl::ImageNativeIndex::FromBaseZeroGLIndex(
GetLayerMipIndex(image, GetImageLayerIndexFrom(index), /** level */ 0)));
}
angle::Result TextureMtl::copySubImageImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::Rectangle &sourceArea,
const gl::InternalFormat &internalFormat,
const FramebufferMtl *source,
const RenderTargetMtl *colorReadRT)
{
if (!colorReadRT || !colorReadRT->getTexture())
{
// Is this an error?
return angle::Result::Continue;
}
gl::Extents fbSize = colorReadRT->getTexture()->size(colorReadRT->getLevelIndex());
gl::Rectangle clippedSourceArea;
if (!ClipRectangle(sourceArea, gl::Rectangle(0, 0, fbSize.width, fbSize.height),
&clippedSourceArea))
{
return angle::Result::Continue;
}
// If negative offsets are given, clippedSourceArea ensures we don't read from those offsets.
// However, that changes the sourceOffset->destOffset mapping. Here, destOffset is shifted by
// the same amount as clipped to correct the error.
const gl::Offset modifiedDestOffset(destOffset.x + clippedSourceArea.x - sourceArea.x,
destOffset.y + clippedSourceArea.y - sourceArea.y, 0);
ANGLE_TRY(ensureImageCreated(context, index));
if (!mFormat.getCaps().isRenderable())
{
return copySubImageCPU(context, index, modifiedDestOffset, clippedSourceArea,
internalFormat, source, colorReadRT);
}
// NOTE(hqle): Use compute shader.
return copySubImageWithDraw(context, index, modifiedDestOffset, clippedSourceArea,
internalFormat, source, colorReadRT);
}
angle::Result TextureMtl::copySubImageWithDraw(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &modifiedDestOffset,
const gl::Rectangle &clippedSourceArea,
const gl::InternalFormat &internalFormat,
const FramebufferMtl *source,
const RenderTargetMtl *colorReadRT)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
DisplayMtl *displayMtl = contextMtl->getDisplay();
const RenderTargetMtl &imageRtt = getRenderTarget(index);
mtl::RenderCommandEncoder *cmdEncoder = contextMtl->getRenderTargetCommandEncoder(imageRtt);
mtl::ColorBlitParams blitParams;
blitParams.dstTextureSize = imageRtt.getTexture()->size(imageRtt.getLevelIndex());
blitParams.dstRect = gl::Rectangle(modifiedDestOffset.x, modifiedDestOffset.y,
clippedSourceArea.width, clippedSourceArea.height);
blitParams.dstScissorRect = blitParams.dstRect;
blitParams.enabledBuffers.set(0);
blitParams.src = colorReadRT->getTexture();
blitParams.srcLevel = colorReadRT->getLevelIndex();
blitParams.srcLayer = colorReadRT->getLayerIndex();
blitParams.srcNormalizedCoords = mtl::NormalizedCoords(
clippedSourceArea, colorReadRT->getTexture()->size(blitParams.srcLevel));
blitParams.srcYFlipped = source->flipY();
blitParams.dstLuminance = internalFormat.isLUMA();
return displayMtl->getUtils().blitColorWithDraw(
context, cmdEncoder, colorReadRT->getFormat()->actualAngleFormat(), blitParams);
}
angle::Result TextureMtl::copySubImageCPU(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &modifiedDestOffset,
const gl::Rectangle &clippedSourceArea,
const gl::InternalFormat &internalFormat,
const FramebufferMtl *source,
const RenderTargetMtl *colorReadRT)
{
mtl::TextureRef &image = getImage(index);
ASSERT(image && image->valid());
ContextMtl *contextMtl = mtl::GetImpl(context);
const angle::Format &dstFormat = angle::Format::Get(mFormat.actualFormatId);
const int dstRowPitch = dstFormat.pixelBytes * clippedSourceArea.width;
angle::MemoryBuffer conversionRow;
ANGLE_CHECK_GL_ALLOC(contextMtl, conversionRow.resize(dstRowPitch));
gl::Rectangle srcRowArea = gl::Rectangle(clippedSourceArea.x, 0, clippedSourceArea.width, 1);
MTLRegion mtlDstRowArea = MTLRegionMake2D(modifiedDestOffset.x, 0, clippedSourceArea.width, 1);
uint32_t dstSlice = 0;
switch (index.getType())
{
case gl::TextureType::_2D:
case gl::TextureType::CubeMap:
dstSlice = 0;
break;
case gl::TextureType::_2DArray:
ASSERT(index.hasLayer());
dstSlice = index.getLayerIndex();
break;
case gl::TextureType::_3D:
ASSERT(index.hasLayer());
dstSlice = 0;
mtlDstRowArea.origin.z = index.getLayerIndex();
break;
default:
UNREACHABLE();
}
// Copy row by row:
for (int r = 0; r < clippedSourceArea.height; ++r)
{
mtlDstRowArea.origin.y = modifiedDestOffset.y + r;
srcRowArea.y = clippedSourceArea.y + r;
PackPixelsParams packParams(srcRowArea, dstFormat, dstRowPitch, false, nullptr, 0);
// Read pixels from framebuffer to memory:
gl::Rectangle flippedSrcRowArea = source->getCorrectFlippedReadArea(context, srcRowArea);
ANGLE_TRY(source->readPixelsImpl(context, flippedSrcRowArea, packParams, colorReadRT,
conversionRow.data()));
// Upload to texture
ANGLE_TRY(UploadTextureContents(context, dstFormat, mtlDstRowArea, mtl::kZeroNativeMipLevel,
dstSlice, conversionRow.data(), dstRowPitch, 0, image));
}
return angle::Result::Continue;
}
angle::Result TextureMtl::copySubTextureImpl(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::InternalFormat &internalFormat,
GLint sourceLevel,
const gl::Box &sourceBox,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const gl::Texture *source)
{
// Only 2D textures are supported.
ASSERT(sourceBox.depth == 1);
ASSERT(source->getType() == gl::TextureType::_2D);
gl::ImageIndex sourceIndex = gl::ImageIndex::Make2D(sourceLevel);
ContextMtl *contextMtl = mtl::GetImpl(context);
TextureMtl *sourceMtl = mtl::GetImpl(source);
ANGLE_TRY(ensureImageCreated(context, index));
ANGLE_TRY(sourceMtl->ensureImageCreated(context, sourceIndex));
const ImageDefinitionMtl &srcImageDef = sourceMtl->getImageDefinition(sourceIndex);
const mtl::TextureRef &sourceImage = srcImageDef.image;
const mtl::Format &sourceFormat = contextMtl->getPixelFormat(srcImageDef.formatID);
const angle::Format &sourceAngleFormat = sourceFormat.actualAngleFormat();
if (!mFormat.getCaps().isRenderable())
{
return copySubTextureCPU(context, index, destOffset, internalFormat,
mtl::kZeroNativeMipLevel, sourceBox, sourceAngleFormat,
unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha,
sourceImage);
}
return copySubTextureWithDraw(
context, index, destOffset, internalFormat, mtl::kZeroNativeMipLevel, sourceBox,
sourceAngleFormat, unpackFlipY, unpackPremultiplyAlpha, unpackUnmultiplyAlpha, sourceImage);
}
angle::Result TextureMtl::copySubTextureWithDraw(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::InternalFormat &internalFormat,
const mtl::MipmapNativeLevel &sourceNativeLevel,
const gl::Box &sourceBox,
const angle::Format &sourceAngleFormat,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const mtl::TextureRef &sourceTexture)
{
ContextMtl *contextMtl = mtl::GetImpl(context);
DisplayMtl *displayMtl = contextMtl->getDisplay();
mtl::TextureRef image = getImage(index);
ASSERT(image && image->valid());
if (internalFormat.colorEncoding == GL_SRGB)
{
image = image->getLinearColorView();
}
mtl::RenderCommandEncoder *cmdEncoder = contextMtl->getTextureRenderCommandEncoder(
image, mtl::ImageNativeIndex::FromBaseZeroGLIndex(GetZeroLevelIndex(image)));
mtl::ColorBlitParams blitParams;
blitParams.dstTextureSize = image->sizeAt0();
blitParams.dstRect =
gl::Rectangle(destOffset.x, destOffset.y, sourceBox.width, sourceBox.height);
blitParams.dstScissorRect = blitParams.dstRect;
blitParams.enabledBuffers.set(0);
blitParams.src = sourceTexture;
blitParams.srcLevel = sourceNativeLevel;
blitParams.srcLayer = 0;
blitParams.srcNormalizedCoords =
mtl::NormalizedCoords(sourceBox.toRect(), sourceTexture->size(sourceNativeLevel));
blitParams.srcYFlipped = false;
blitParams.dstLuminance = internalFormat.isLUMA();
blitParams.unpackFlipY = unpackFlipY;
blitParams.unpackPremultiplyAlpha = unpackPremultiplyAlpha;
blitParams.unpackUnmultiplyAlpha = unpackUnmultiplyAlpha;
return displayMtl->getUtils().copyTextureWithDraw(context, cmdEncoder, sourceAngleFormat,
mFormat.actualAngleFormat(), blitParams);
}
angle::Result TextureMtl::copySubTextureCPU(const gl::Context *context,
const gl::ImageIndex &index,
const gl::Offset &destOffset,
const gl::InternalFormat &internalFormat,
const mtl::MipmapNativeLevel &sourceNativeLevel,
const gl::Box &sourceBox,
const angle::Format &sourceAngleFormat,
bool unpackFlipY,
bool unpackPremultiplyAlpha,
bool unpackUnmultiplyAlpha,
const mtl::TextureRef &sourceTexture)
{
mtl::TextureRef &image = getImage(index);
ASSERT(image && image->valid());
ContextMtl *contextMtl = mtl::GetImpl(context);
const angle::Format &dstAngleFormat = mFormat.actualAngleFormat();
const int srcRowPitch = sourceAngleFormat.pixelBytes * sourceBox.width;
const int srcImageSize = srcRowPitch * sourceBox.height;
const int convRowPitch = dstAngleFormat.pixelBytes * sourceBox.width;
const int convImageSize = convRowPitch * sourceBox.height;
angle::MemoryBuffer conversionSrc, conversionDst;
ANGLE_CHECK_GL_ALLOC(contextMtl, conversionSrc.resize(srcImageSize));
ANGLE_CHECK_GL_ALLOC(contextMtl, conversionDst.resize(convImageSize));
MTLRegion mtlSrcArea =
MTLRegionMake2D(sourceBox.x, sourceBox.y, sourceBox.width, sourceBox.height);
MTLRegion mtlDstArea =
MTLRegionMake2D(destOffset.x, destOffset.y, sourceBox.width, sourceBox.height);
// Read pixels from source to memory:
sourceTexture->getBytes(contextMtl, srcRowPitch, 0, mtlSrcArea, sourceNativeLevel, 0,
conversionSrc.data());
// Convert to destination format
CopyImageCHROMIUM(conversionSrc.data(), srcRowPitch, sourceAngleFormat.pixelBytes, 0,
sourceAngleFormat.pixelReadFunction, conversionDst.data(), convRowPitch,
dstAngleFormat.pixelBytes, 0, dstAngleFormat.pixelWriteFunction,
internalFormat.format, internalFormat.componentType, sourceBox.width,
sourceBox.height, 1, unpackFlipY, unpackPremultiplyAlpha,
unpackUnmultiplyAlpha);
// Upload to texture
ANGLE_TRY(UploadTextureContents(context, dstAngleFormat, mtlDstArea, mtl::kZeroNativeMipLevel,
0, conversionDst.data(), convRowPitch, 0, image));
return angle::Result::Continue;
}
}