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 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729
|
/******************************************************************************
* The MIT License (MIT)
*
* Copyright (c) 2019-2020 Baldur Karlsson
* Copyright (c) 2014 Crytek
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
******************************************************************************/
#include <float.h>
#include <algorithm>
#include "common/common.h"
#include "data/glsl_shaders.h"
#include "driver/shaders/spirv/glslang_compile.h"
#include "maths/camera.h"
#include "maths/formatpacking.h"
#include "maths/matrix.h"
#include "strings/string_utils.h"
#include "gl_driver.h"
#include "gl_replay.h"
#include "gl_resources.h"
#define OPENGL 1
#include "data/glsl/glsl_ubos_cpp.h"
void GetGLSLVersions(ShaderType &shaderType, int &glslVersion, int &glslBaseVer, int &glslCSVer)
{
if(IsGLES)
{
shaderType = ShaderType::GLSLES;
glslVersion = glslBaseVer = glslCSVer = 300;
if(GLCoreVersion >= 31)
glslVersion = glslBaseVer = glslCSVer = 310;
if(GLCoreVersion >= 32)
glslVersion = glslBaseVer = glslCSVer = 320;
}
else
{
shaderType = ShaderType::GLSL;
glslVersion = glslBaseVer = 150;
glslCSVer = 420;
}
}
GLuint CreateShader(GLenum shaderType, const rdcstr &src)
{
GLuint ret = GL.glCreateShader(shaderType);
const char *csrc = src.c_str();
GL.glShaderSource(ret, 1, &csrc, NULL);
GL.glCompileShader(ret);
char buffer[1024] = {};
GLint status = 0;
GL.glGetShaderiv(ret, eGL_COMPILE_STATUS, &status);
if(status == 0)
{
GL.glGetShaderInfoLog(ret, 1024, NULL, buffer);
RDCERR("%s compile error: %s", ToStr(shaderType).c_str(), buffer);
return 0;
}
return ret;
}
GLuint CreateSPIRVShader(GLenum shaderType, const rdcstr &src)
{
if(!HasExt[ARB_gl_spirv])
{
RDCERR("Compiling SPIR-V shader without ARB_gl_spirv - should be checked above!");
return 0;
}
rdcspv::CompilationSettings settings(rdcspv::InputLanguage::OpenGLGLSL,
rdcspv::ShaderStage(ShaderIdx(shaderType)));
rdcarray<uint32_t> spirv;
rdcstr s = rdcspv::Compile(settings, {src}, spirv);
if(spirv.empty())
{
RDCERR("Couldn't compile shader to SPIR-V: %s", s.c_str());
return 0;
}
GLuint ret = GL.glCreateShader(shaderType);
GL.glShaderBinary(1, &ret, eGL_SHADER_BINARY_FORMAT_SPIR_V, spirv.data(),
(GLsizei)spirv.size() * 4);
GL.glSpecializeShader(ret, "main", 0, NULL, NULL);
char buffer[1024] = {};
GLint status = 0;
GL.glGetShaderiv(ret, eGL_COMPILE_STATUS, &status);
if(status == 0)
{
GL.glGetShaderInfoLog(ret, 1024, NULL, buffer);
RDCERR("%s compile error: %s", ToStr(shaderType).c_str(), buffer);
return 0;
}
return ret;
}
GLuint CreateCShaderProgram(const rdcstr &src)
{
GLuint cs = CreateShader(eGL_COMPUTE_SHADER, src);
if(cs == 0)
return 0;
GLuint ret = GL.glCreateProgram();
GL.glAttachShader(ret, cs);
GL.glLinkProgram(ret);
char buffer[1024] = {};
GLint status = 0;
GL.glGetProgramiv(ret, eGL_LINK_STATUS, &status);
if(status == 0)
{
GL.glGetProgramInfoLog(ret, 1024, NULL, buffer);
RDCERR("Link error: %s", buffer);
}
GL.glDetachShader(ret, cs);
GL.glDeleteShader(cs);
return ret;
}
GLuint CreateShaderProgram(GLuint vs, GLuint fs, GLuint gs)
{
GLuint ret = GL.glCreateProgram();
GL.glAttachShader(ret, vs);
GL.glAttachShader(ret, fs);
if(gs)
GL.glAttachShader(ret, gs);
GL.glLinkProgram(ret);
char buffer[1024] = {};
GLint status = 0;
GL.glGetProgramiv(ret, eGL_LINK_STATUS, &status);
if(status == 0)
{
GL.glGetProgramInfoLog(ret, 1024, NULL, buffer);
RDCERR("Shader error: %s", buffer);
}
return ret;
}
GLuint CreateShaderProgram(const rdcstr &vsSrc, const rdcstr &fsSrc, const rdcstr &gsSrc)
{
GLuint vs = 0;
GLuint fs = 0;
GLuint gs = 0;
if(vsSrc.empty())
{
RDCERR("Must have vertex shader - no separable programs supported.");
return 0;
}
if(fsSrc.empty())
{
RDCERR("Must have fragment shader - no separable programs supported.");
return 0;
}
vs = CreateShader(eGL_VERTEX_SHADER, vsSrc);
if(vs == 0)
return 0;
fs = CreateShader(eGL_FRAGMENT_SHADER, fsSrc);
if(fs == 0)
return 0;
if(!gsSrc.empty())
{
gs = CreateShader(eGL_GEOMETRY_SHADER, gsSrc);
if(gs == 0)
return 0;
}
GLuint ret = CreateShaderProgram(vs, fs, gs);
GL.glDetachShader(ret, vs);
GL.glDetachShader(ret, fs);
if(gs)
GL.glDetachShader(ret, gs);
GL.glDeleteShader(vs);
GL.glDeleteShader(fs);
if(gs)
GL.glDeleteShader(gs);
return ret;
}
void GLReplay::CheckGLSLVersion(const char *sl, int &glslVersion)
{
// GL_SHADING_LANGUAGE_VERSION for OpenGL ES:
// "OpenGL ES GLSL ES N.M vendor-specific information"
static const char *const GLSL_ES_STR = "OpenGL ES GLSL ES";
if(strncmp(sl, GLSL_ES_STR, 17) == 0)
sl += 18;
if(sl[0] >= '0' && sl[0] <= '9' && sl[1] == '.' && sl[2] >= '0' && sl[2] <= '9')
{
int major = int(sl[0] - '0');
int minor = int(sl[2] - '0');
int ver = major * 100 + minor * 10;
if(ver > glslVersion)
glslVersion = ver;
}
if(sl[0] >= '0' && sl[0] <= '9' && sl[1] >= '0' && sl[1] <= '9' && sl[2] == '0')
{
int major = int(sl[0] - '0');
int minor = int(sl[1] - '0');
int ver = major * 100 + minor * 10;
if(ver > glslVersion)
glslVersion = ver;
}
}
GLuint GLReplay::CreateMeshProgram(GLuint vs, GLuint fs, GLuint gs)
{
GLuint program = CreateShaderProgram(vs, fs, gs);
// set attrib locations
GL.glBindAttribLocation(program, 0, "position");
GL.glBindAttribLocation(program, 1, "IN_secondary");
// relink
GL.glLinkProgram(program);
// check that the relink succeeded
char buffer[1024] = {};
GLint status = 0;
GL.glGetProgramiv(program, eGL_LINK_STATUS, &status);
if(status == 0)
{
GL.glGetProgramInfoLog(program, 1024, NULL, buffer);
RDCERR("Link error: %s", buffer);
}
// detach the shaders
GL.glDetachShader(program, vs);
GL.glDetachShader(program, fs);
if(gs)
GL.glDetachShader(program, gs);
// bind the UBO
BindUBO(program, "MeshUBOData", 0);
return program;
}
void GLReplay::ConfigureTexDisplayProgramBindings(GLuint program)
{
GLint location = -1;
GL.glUseProgram(program);
// since we split the shader up by type, not all texture slots are always available. Also on GLES
// some texture slots might be missing due to lack of extensions. So we need to check for a location
// of -1
#define SET_TEX_BINDING(name, bind) \
location = GL.glGetUniformLocation(program, name); \
if(location >= 0) \
GL.glUniform1i(location, bind);
SET_TEX_BINDING("texUInt1D", 1);
SET_TEX_BINDING("texUInt2D", 2);
SET_TEX_BINDING("texUInt3D", 3);
SET_TEX_BINDING("texUInt1DArray", 5);
SET_TEX_BINDING("texUInt2DArray", 6);
SET_TEX_BINDING("texUInt2DRect", 8);
SET_TEX_BINDING("texUIntBuffer", 9);
SET_TEX_BINDING("texUInt2DMS", 10);
SET_TEX_BINDING("texUInt2DMSArray", 11);
SET_TEX_BINDING("texSInt1D", 1);
SET_TEX_BINDING("texSInt2D", 2);
SET_TEX_BINDING("texSInt3D", 3);
SET_TEX_BINDING("texSInt1DArray", 5);
SET_TEX_BINDING("texSInt2DArray", 6);
SET_TEX_BINDING("texSInt2DRect", 8);
SET_TEX_BINDING("texSIntBuffer", 9);
SET_TEX_BINDING("texSInt2DMS", 10);
SET_TEX_BINDING("texSInt2DMSArray", 11);
SET_TEX_BINDING("tex1D", 1);
SET_TEX_BINDING("tex2D", 2);
SET_TEX_BINDING("tex3D", 3);
SET_TEX_BINDING("texCube", 4);
SET_TEX_BINDING("tex1DArray", 5);
SET_TEX_BINDING("tex2DArray", 6);
SET_TEX_BINDING("texCubeArray", 7);
SET_TEX_BINDING("tex2DRect", 8);
SET_TEX_BINDING("texBuffer", 9);
SET_TEX_BINDING("tex2DMS", 10);
SET_TEX_BINDING("tex2DMSArray", 11);
#undef SET_TEX_BINDING
}
void GLReplay::BindUBO(GLuint program, const char *name, GLuint binding)
{
GL.glUniformBlockBinding(program, GL.glGetUniformBlockIndex(program, name), binding);
}
void GLReplay::InitDebugData()
{
if(m_pDriver == NULL)
return;
// don't reflect any shaders or programs we make
m_pDriver->PushInternalShader();
m_HighlightCache.driver = m_pDriver->GetReplay();
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 0.0f);
{
WindowingData window = {WindowingSystem::Headless};
uint64_t id = MakeOutputWindow(window, true);
m_DebugCtx = NULL;
if(id == 0)
return;
m_DebugID = id;
m_DebugCtx = &m_OutputWindows[id];
MakeCurrentReplayContext(m_DebugCtx);
m_pDriver->RegisterDebugCallback();
}
WrappedOpenGL &drv = *m_pDriver;
DebugData.outWidth = 0.0f;
DebugData.outHeight = 0.0f;
rdcstr vs;
rdcstr fs;
rdcstr gs;
rdcstr cs;
ShaderType shaderType;
int glslVersion;
int glslBaseVer;
int glslCSVer; // compute shader
GetGLSLVersions(shaderType, glslVersion, glslBaseVer, glslCSVer);
rdcstr texSampleDefines;
if(IsGLES)
{
if(HasExt[OES_texture_cube_map_array] || HasExt[EXT_texture_cube_map_array] || GLCoreVersion >= 32)
texSampleDefines += "#define TEXSAMPLE_CUBE_ARRAY 1\n";
if(HasExt[OES_texture_cube_map_array])
texSampleDefines += "#extension GL_OES_texture_cube_map_array : require\n";
if(HasExt[EXT_texture_cube_map_array])
texSampleDefines += "#extension GL_EXT_texture_cube_map_array : require\n";
if(HasExt[EXT_texture_buffer])
texSampleDefines +=
"#define TEXSAMPLE_BUFFER 1\n"
"#extension GL_EXT_texture_buffer : require\n";
texSampleDefines += "#define HAS_BIT_CONVERSION 1\n";
}
else
{
if(HasExt[ARB_texture_cube_map_array])
texSampleDefines +=
"#define TEXSAMPLE_CUBE_ARRAY 1\n"
"#extension GL_ARB_texture_cube_map_array : require\n";
if(HasExt[ARB_texture_multisample])
texSampleDefines +=
"#define TEXSAMPLE_MULTISAMPLE 1\n"
"#extension GL_ARB_texture_multisample : require\n";
if(HasExt[ARB_gpu_shader5])
texSampleDefines +=
"#define HAS_BIT_CONVERSION 1\n"
"#extension GL_ARB_gpu_shader5 : require\n";
else if(HasExt[ARB_shader_bit_encoding])
texSampleDefines +=
"#define HAS_BIT_CONVERSION 1\n"
"#extension GL_ARB_shader_bit_encoding : require\n";
}
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_blit_vert), shaderType, glslBaseVer);
DebugData.fixedcolFragShaderSPIRV = DebugData.quadoverdrawFragShaderSPIRV = 0;
// pre-compile SPIR-V shaders up front since this is more expensive
if(HasExt[ARB_gl_spirv])
{
// SPIR-V shaders are always generated as desktop GL 430, for ease
rdcstr source =
GenerateGLSLShader(GetEmbeddedResource(glsl_fixedcol_frag), ShaderType::GLSPIRV, 430);
DebugData.fixedcolFragShaderSPIRV = CreateSPIRVShader(eGL_FRAGMENT_SHADER, source);
if(HasExt[ARB_gpu_shader5] && HasExt[ARB_shader_image_load_store])
{
rdcstr defines = "";
if(!HasExt[ARB_derivative_control])
{
defines += "#define dFdxFine dFdx\n\n";
defines += "#define dFdyFine dFdy\n\n";
}
source = GenerateGLSLShader(GetEmbeddedResource(glsl_quadwrite_frag), ShaderType::GLSPIRV,
430, defines);
DebugData.quadoverdrawFragShaderSPIRV = CreateSPIRVShader(eGL_FRAGMENT_SHADER, source);
}
}
// used to combine with custom shaders.
DebugData.texDisplayVertexShader = CreateShader(eGL_VERTEX_SHADER, vs);
for(int i = 0; i < 3; i++)
{
rdcstr defines = rdcstr("#define SHADER_BASETYPE ") + ToStr(i) + "\n";
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_texdisplay_frag), shaderType, glslBaseVer,
defines + texSampleDefines);
DebugData.texDisplayProg[i] = CreateShaderProgram(vs, fs);
BindUBO(DebugData.texDisplayProg[i], "TexDisplayUBOData", 0);
BindUBO(DebugData.texDisplayProg[i], "HeatmapData", 1);
ConfigureTexDisplayProgramBindings(DebugData.texDisplayProg[i]);
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_texremap_frag), shaderType, glslBaseVer,
defines + texSampleDefines);
DebugData.texRemapProg[i] = CreateShaderProgram(vs, fs);
BindUBO(DebugData.texRemapProg[i], "TexDisplayUBOData", 0);
ConfigureTexDisplayProgramBindings(DebugData.texRemapProg[i]);
}
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 0.2f);
if(GLCoreVersion >= 43 && !IsGLES)
{
GLint numsl = 0;
drv.glGetIntegerv(eGL_NUM_SHADING_LANGUAGE_VERSIONS, &numsl);
for(GLint i = 0; i < numsl; i++)
{
const char *sl = (const char *)drv.glGetStringi(eGL_SHADING_LANGUAGE_VERSION, (GLuint)i);
CheckGLSLVersion(sl, glslVersion);
}
}
else
{
const char *sl = (const char *)drv.glGetString(eGL_SHADING_LANGUAGE_VERSION);
CheckGLSLVersion(sl, glslVersion);
}
DebugData.glslVersion = glslVersion;
RDCLOG("GLSL version %d", glslVersion);
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_blit_vert), shaderType, glslBaseVer);
DebugData.fixedcolFragShader = DebugData.quadoverdrawFragShader = 0;
DebugData.quadoverdrawResolveProg = 0;
if(IsGLES)
{
// quad overdraw not supported on GLES.
// 1.
// dFdx doesn't support uints - potentially workaroundable with float casts, but highly
// doubtful GLES compilers will do that properly without exploding.
// 2.
// quad overdraw write shader must be linked with user shaders in program, which requires
// matching ESSL version and features required for it aren't exposed as extensions to older
// versions but only in core versions.
}
else if(HasExt[ARB_shader_image_load_store] && HasExt[ARB_gpu_shader5])
{
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_quadresolve_frag), shaderType, glslBaseVer);
DebugData.quadoverdrawResolveProg = CreateShaderProgram(vs, fs);
GL.glUseProgram(DebugData.quadoverdrawResolveProg);
GL.glUniform1i(GL.glGetUniformLocation(DebugData.quadoverdrawResolveProg, "overdrawImage"), 0);
}
else
{
RDCWARN(
"GL_ARB_shader_image_load_store/GL_ARB_gpu_shader5 not supported, disabling quad overdraw "
"feature.");
m_pDriver->AddDebugMessage(MessageCategory::Portability, MessageSeverity::Medium,
MessageSource::RuntimeWarning,
"GL_ARB_shader_image_load_store/GL_ARB_gpu_shader5 not supported, "
"disabling quad overdraw feature.");
}
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_checkerboard_frag), shaderType, glslBaseVer);
DebugData.checkerProg = CreateShaderProgram(vs, fs);
BindUBO(DebugData.checkerProg, "CheckerboardUBOData", 0);
for(size_t numViews = 0; numViews < ARRAY_COUNT(DebugData.discardProg); numViews++)
{
rdcstr defines;
if(numViews > 0 && IsGLES && HasExt[OVR_multiview])
defines = StringFormat::Fmt("#define NUM_VIEWS %zu", numViews + 1);
rdcstr blitvs =
GenerateGLSLShader(GetEmbeddedResource(glsl_blit_vert), shaderType, glslBaseVer, defines);
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_discard_frag), shaderType, glslBaseVer);
DebugData.discardProg[numViews] = CreateShaderProgram(blitvs, fs);
BindUBO(DebugData.discardProg[numViews], "DiscardUBOData", 0);
if(!IsGLES)
{
rdcstr name = "col0";
for(GLuint i = 0; i < 8; i++)
{
name[3] = char('0' + i);
GL.glBindFragDataLocation(DebugData.discardProg[numViews], i, name.c_str());
}
}
}
{
ResourceFormat fmt;
fmt.type = ResourceFormatType::Regular;
fmt.compType = CompType::Float;
fmt.compByteWidth = 4;
fmt.compCount = 1;
bytebuf pattern = GetDiscardPattern(DiscardType::InvalidateCall, fmt, 1, true);
drv.glGenBuffers(1, &DebugData.discardPatternBuffer);
drv.glBindBuffer(eGL_UNIFORM_BUFFER, DebugData.discardPatternBuffer);
drv.glNamedBufferDataEXT(DebugData.discardPatternBuffer, pattern.size(), pattern.data(),
eGL_STATIC_DRAW);
}
if(HasExt[ARB_geometry_shader4])
{
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer);
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_trisize_frag), shaderType, glslBaseVer);
gs = GenerateGLSLShader(GetEmbeddedResource(glsl_trisize_geom), shaderType, glslBaseVer);
// create the shaders
GLuint vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
GLuint trifsShad = CreateShader(eGL_FRAGMENT_SHADER, fs);
GLuint gsShad = CreateShader(eGL_GEOMETRY_SHADER, gs);
DebugData.trisizeProg = CreateMeshProgram(vsShad, trifsShad, gsShad);
// bind trisize-unique viewport size UBO
BindUBO(DebugData.trisizeProg, "ViewportSizeUBO", 2);
GL.glDeleteShader(trifsShad);
GL.glDeleteShader(gsShad);
// we have two fragment shaders, one that reads from the vs outputs and one that reads from the
// gs outputs
rdcstr vsfs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_frag), shaderType, glslBaseVer,
"#define SECONDARY_NAME vsout_secondary\n"
"#define NORM_NAME vsout_norm\n");
rdcstr gsfs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_frag), shaderType, glslBaseVer,
"#define SECONDARY_NAME gsout_secondary\n"
"#define NORM_NAME gsout_norm\n");
gs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_geom), shaderType, glslBaseVer);
// recreate the shaders
GLuint vsfsShad = CreateShader(eGL_FRAGMENT_SHADER, vsfs);
GLuint gsfsShad = CreateShader(eGL_FRAGMENT_SHADER, gsfs);
gsShad = CreateShader(eGL_GEOMETRY_SHADER, gs);
DebugData.meshProg[0] = CreateMeshProgram(vsShad, vsfsShad);
DebugData.meshgsProg[0] = CreateMeshProgram(vsShad, gsfsShad, gsShad);
if(HasExt[ARB_gpu_shader_fp64] && HasExt[ARB_vertex_attrib_64bit])
{
rdcstr extensions =
"#extension GL_ARB_gpu_shader_fp64 : require\n"
"#extension GL_ARB_vertex_attrib_64bit : require\n";
// position only dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions + "#define POSITION_TYPE dvec4\n");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[1] = CreateMeshProgram(vsShad, vsfsShad);
DebugData.meshgsProg[1] = CreateMeshProgram(vsShad, gsfsShad, gsShad);
// secondary only dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions + "#define SECONDARY_TYPE dvec4\n");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[2] = CreateMeshProgram(vsShad, vsfsShad);
DebugData.meshgsProg[2] = CreateMeshProgram(vsShad, gsfsShad, gsShad);
// both dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions +
"#define POSITION_TYPE dvec4\n"
"#define SECONDARY_TYPE dvec4\n");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[3] = CreateMeshProgram(vsShad, vsfsShad);
DebugData.meshgsProg[3] = CreateMeshProgram(vsShad, gsfsShad, gsShad);
}
else
{
// we don't warn about the lack of double support, assuming that if the driver doesn't support
// it then it's highly unlikely that the capture uses it.
DebugData.meshProg[1] = DebugData.meshProg[2] = DebugData.meshProg[3] = 0;
DebugData.meshgsProg[1] = DebugData.meshgsProg[2] = DebugData.meshgsProg[3] = 0;
}
GL.glDeleteShader(vsShad);
GL.glDeleteShader(vsfsShad);
GL.glDeleteShader(gsfsShad);
GL.glDeleteShader(gsShad);
}
else
{
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer);
// without a geometry shader, the fragment shader always reads from vs outputs
fs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_frag), shaderType, glslBaseVer,
"#define SECONDARY_NAME vsout_secondary\n"
"#define NORM_NAME vsout_norm\n");
// create the shaders
GLuint vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
GLuint fsShad = CreateShader(eGL_FRAGMENT_SHADER, fs);
DebugData.meshProg[0] = CreateMeshProgram(vsShad, fsShad);
RDCEraseEl(DebugData.meshgsProg);
DebugData.trisizeProg = 0;
const char *warning_msg =
"GL_ARB_geometry_shader4/GL_EXT_geometry_shader not supported, disabling triangle size and "
"lit solid shading feature.";
RDCWARN(warning_msg);
m_pDriver->AddDebugMessage(MessageCategory::Portability, MessageSeverity::Medium,
MessageSource::RuntimeWarning, warning_msg);
if(HasExt[ARB_gpu_shader_fp64] && HasExt[ARB_vertex_attrib_64bit])
{
rdcstr extensions =
"#extension GL_ARB_gpu_shader_fp64 : require\n"
"#extension GL_ARB_vertex_attrib_64bit : require\n";
// position only dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions + "#define POSITION_TYPE dvec4");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[1] = CreateMeshProgram(vsShad, fsShad);
// secondary only dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions + "#define SECONDARY_TYPE dvec4");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[2] = CreateMeshProgram(vsShad, fsShad);
// both dvec4
vs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_vert), shaderType, glslBaseVer,
extensions +
"#define POSITION_TYPE dvec4\n"
"#define SECONDARY_TYPE dvec4");
// delete old shader and recreate with new source
GL.glDeleteShader(vsShad);
vsShad = CreateShader(eGL_VERTEX_SHADER, vs);
DebugData.meshProg[3] = CreateMeshProgram(vsShad, fsShad);
}
else
{
// we don't warn about the lack of double support, assuming that if the driver doesn't support
// it then it's highly unlikely that the capture uses it.
DebugData.meshProg[1] = DebugData.meshProg[2] = DebugData.meshProg[3] = 0;
}
GL.glDeleteShader(vsShad);
GL.glDeleteShader(fsShad);
}
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 0.4f);
drv.glGenBuffers(ARRAY_COUNT(DebugData.UBOs), DebugData.UBOs);
for(size_t i = 0; i < ARRAY_COUNT(DebugData.UBOs); i++)
{
drv.glBindBuffer(eGL_UNIFORM_BUFFER, DebugData.UBOs[i]);
drv.glNamedBufferDataEXT(DebugData.UBOs[i], 2048, NULL, eGL_DYNAMIC_DRAW);
RDCCOMPILE_ASSERT(sizeof(TexDisplayUBOData) <= 2048, "UBO too small");
RDCCOMPILE_ASSERT(sizeof(FontUBOData) <= 2048, "UBO too small");
RDCCOMPILE_ASSERT(sizeof(HistogramUBOData) <= 2048, "UBO too small");
}
DebugData.overlayTexWidth = DebugData.overlayTexHeight = DebugData.overlayTexSamples = 0;
DebugData.overlayTex = DebugData.overlayFBO = 0;
DebugData.overlayProg = 0;
drv.glGenFramebuffers(1, &DebugData.customFBO);
drv.glBindFramebuffer(eGL_FRAMEBUFFER, DebugData.customFBO);
DebugData.customTex = 0;
drv.glGenFramebuffers(1, &DebugData.pickPixelFBO);
drv.glBindFramebuffer(eGL_FRAMEBUFFER, DebugData.pickPixelFBO);
if(HasExt[ARB_texture_buffer_object])
{
drv.glGenBuffers(1, &DebugData.dummyTexBufferStore);
drv.glBindBuffer(eGL_TEXTURE_BUFFER, DebugData.dummyTexBufferStore);
drv.glNamedBufferDataEXT(DebugData.dummyTexBufferStore, 32, NULL, eGL_STATIC_DRAW);
drv.glBindBuffer(eGL_TEXTURE_BUFFER, 0);
drv.glGenTextures(1, &DebugData.dummyTexBuffer);
drv.glBindTexture(eGL_TEXTURE_BUFFER, DebugData.dummyTexBuffer);
drv.glTextureBufferEXT(DebugData.dummyTexBuffer, eGL_TEXTURE_BUFFER, eGL_RGBA32F,
DebugData.dummyTexBufferStore);
drv.glBindTexture(eGL_TEXTURE_BUFFER, 0);
}
else
{
DebugData.dummyTexBuffer = DebugData.dummyTexBufferStore = 0;
}
drv.glGenTextures(1, &DebugData.pickPixelTex);
drv.glBindTexture(eGL_TEXTURE_2D, DebugData.pickPixelTex);
drv.glTextureImage2DEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, 0, eGL_RGBA32F, 1, 1, 0, eGL_RGBA,
eGL_FLOAT, NULL);
drv.glTextureParameteriEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, eGL_TEXTURE_MAX_LEVEL, 0);
drv.glTextureParameteriEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, eGL_TEXTURE_MIN_FILTER,
eGL_NEAREST);
drv.glTextureParameteriEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, eGL_TEXTURE_MAG_FILTER,
eGL_NEAREST);
drv.glTextureParameteriEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, eGL_TEXTURE_WRAP_S,
eGL_CLAMP_TO_EDGE);
drv.glTextureParameteriEXT(DebugData.pickPixelTex, eGL_TEXTURE_2D, eGL_TEXTURE_WRAP_T,
eGL_CLAMP_TO_EDGE);
drv.glFramebufferTexture2D(eGL_FRAMEBUFFER, eGL_COLOR_ATTACHMENT0, eGL_TEXTURE_2D,
DebugData.pickPixelTex, 0);
drv.glGenVertexArrays(1, &DebugData.emptyVAO);
drv.glBindVertexArray(DebugData.emptyVAO);
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 0.6f);
// histogram/minmax data
{
RDCEraseEl(DebugData.minmaxTileProgram);
RDCEraseEl(DebugData.histogramProgram);
RDCEraseEl(DebugData.minmaxResultProgram);
RDCCOMPILE_ASSERT(
ARRAY_COUNT(DebugData.minmaxTileProgram) >= (TEXDISPLAY_SINT_TEX | TEXDISPLAY_TYPEMASK) + 1,
"not enough programs");
if(HasExt[ARB_compute_shader] && HasExt[ARB_shader_storage_buffer_object] &&
HasExt[ARB_shading_language_420pack])
{
for(int t = 1; t <= RESTYPE_TEXTYPEMAX; t++)
{
// float, uint, sint
for(int i = 0; i < 3; i++)
{
int idx = t;
if(i == 1)
idx |= TEXDISPLAY_UINT_TEX;
if(i == 2)
idx |= TEXDISPLAY_SINT_TEX;
{
rdcstr defines;
defines += rdcstr("#define SHADER_RESTYPE ") + ToStr(t) + "\n";
defines += rdcstr("#define SHADER_BASETYPE ") + ToStr(i) + "\n";
defines += texSampleDefines;
cs = GenerateGLSLShader(GetEmbeddedResource(glsl_minmaxtile_comp), shaderType,
glslCSVer, defines);
DebugData.minmaxTileProgram[idx] = CreateCShaderProgram(cs);
BindUBO(DebugData.minmaxTileProgram[idx], "HistogramUBOData", 2);
ConfigureTexDisplayProgramBindings(DebugData.minmaxTileProgram[idx]);
}
{
rdcstr defines;
defines += rdcstr("#define SHADER_RESTYPE ") + ToStr(t) + "\n";
defines += rdcstr("#define SHADER_BASETYPE ") + ToStr(i) + "\n";
defines += texSampleDefines;
cs = GenerateGLSLShader(GetEmbeddedResource(glsl_histogram_comp), shaderType, glslCSVer,
defines);
DebugData.histogramProgram[idx] = CreateCShaderProgram(cs);
BindUBO(DebugData.histogramProgram[idx], "HistogramUBOData", 2);
ConfigureTexDisplayProgramBindings(DebugData.histogramProgram[idx]);
}
if(t == 1)
{
rdcstr defines;
defines += rdcstr("#define SHADER_RESTYPE ") + ToStr(t) + "\n";
defines += rdcstr("#define SHADER_BASETYPE ") + ToStr(i) + "\n";
cs = GenerateGLSLShader(GetEmbeddedResource(glsl_minmaxresult_comp), shaderType,
glslCSVer, defines);
DebugData.minmaxResultProgram[i] = CreateCShaderProgram(cs);
BindUBO(DebugData.minmaxResultProgram[i], "HistogramUBOData", 2);
ConfigureTexDisplayProgramBindings(DebugData.minmaxResultProgram[i]);
}
}
}
}
else
{
RDCWARN(
"GL_ARB_compute_shader or ARB_shader_storage_buffer_object not supported, disabling "
"min/max and histogram features.");
m_pDriver->AddDebugMessage(MessageCategory::Portability, MessageSeverity::Medium,
MessageSource::RuntimeWarning,
"GL_ARB_compute_shader or ARB_shader_storage_buffer_object not "
"supported, disabling min/max and histogram features.");
}
drv.glGenBuffers(1, &DebugData.minmaxTileResult);
drv.glGenBuffers(1, &DebugData.minmaxResult);
drv.glGenBuffers(1, &DebugData.histogramBuf);
const uint32_t maxTexDim = 16384;
const uint32_t blockPixSize = HGRAM_PIXELS_PER_TILE * HGRAM_TILES_PER_BLOCK;
const uint32_t maxBlocksNeeded = (maxTexDim * maxTexDim) / (blockPixSize * blockPixSize);
const size_t byteSize =
2 * sizeof(Vec4f) * HGRAM_TILES_PER_BLOCK * HGRAM_TILES_PER_BLOCK * maxBlocksNeeded;
drv.glNamedBufferDataEXT(DebugData.minmaxTileResult, byteSize, NULL, eGL_DYNAMIC_DRAW);
drv.glNamedBufferDataEXT(DebugData.minmaxResult, sizeof(Vec4f) * 2, NULL, eGL_DYNAMIC_READ);
drv.glNamedBufferDataEXT(DebugData.histogramBuf, sizeof(uint32_t) * HGRAM_NUM_BUCKETS, NULL,
eGL_DYNAMIC_READ);
}
if(HasExt[ARB_compute_shader] && HasExt[ARB_shading_language_420pack])
{
cs = GenerateGLSLShader(GetEmbeddedResource(glsl_mesh_comp), shaderType, glslCSVer);
DebugData.meshPickProgram = CreateCShaderProgram(cs);
BindUBO(DebugData.meshPickProgram, "MeshPickUBOData", 0);
}
else
{
DebugData.meshPickProgram = 0;
RDCWARN("GL_ARB_compute_shader not supported, disabling mesh picking.");
m_pDriver->AddDebugMessage(MessageCategory::Portability, MessageSeverity::Medium,
MessageSource::RuntimeWarning,
"GL_ARB_compute_shader not supported, disabling mesh picking.");
}
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 0.8f);
DebugData.pickResultBuf = 0;
if(DebugData.meshPickProgram)
{
drv.glGenBuffers(1, &DebugData.pickResultBuf);
drv.glBindBuffer(eGL_SHADER_STORAGE_BUFFER, DebugData.pickResultBuf);
drv.glNamedBufferDataEXT(DebugData.pickResultBuf,
sizeof(Vec4f) * DebugRenderData::maxMeshPicks + sizeof(uint32_t) * 4,
NULL, eGL_DYNAMIC_READ);
// sized/created on demand
DebugData.pickVBBuf = DebugData.pickIBBuf = 0;
DebugData.pickVBSize = DebugData.pickIBSize = 0;
}
drv.glGenVertexArrays(1, &DebugData.meshVAO);
drv.glBindVertexArray(DebugData.meshVAO);
drv.glGenBuffers(1, &DebugData.axisFrustumBuffer);
drv.glBindBuffer(eGL_ARRAY_BUFFER, DebugData.axisFrustumBuffer);
Vec3f TLN = Vec3f(-1.0f, 1.0f, 0.0f); // TopLeftNear, etc...
Vec3f TRN = Vec3f(1.0f, 1.0f, 0.0f);
Vec3f BLN = Vec3f(-1.0f, -1.0f, 0.0f);
Vec3f BRN = Vec3f(1.0f, -1.0f, 0.0f);
Vec3f TLF = Vec3f(-1.0f, 1.0f, 1.0f);
Vec3f TRF = Vec3f(1.0f, 1.0f, 1.0f);
Vec3f BLF = Vec3f(-1.0f, -1.0f, 1.0f);
Vec3f BRF = Vec3f(1.0f, -1.0f, 1.0f);
Vec3f axisFrustum[] = {
// axis marker vertices
Vec3f(0.0f, 0.0f, 0.0f), Vec3f(1.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f, 0.0f),
Vec3f(0.0f, 1.0f, 0.0f), Vec3f(0.0f, 0.0f, 0.0f), Vec3f(0.0f, 0.0f, 1.0f),
// frustum vertices
TLN, TRN, TRN, BRN, BRN, BLN, BLN, TLN,
TLN, TLF, TRN, TRF, BLN, BLF, BRN, BRF,
TLF, TRF, TRF, BRF, BRF, BLF, BLF, TLF,
};
drv.glNamedBufferDataEXT(DebugData.axisFrustumBuffer, sizeof(axisFrustum), axisFrustum,
eGL_STATIC_DRAW);
drv.glGenVertexArrays(1, &DebugData.axisVAO);
drv.glBindVertexArray(DebugData.axisVAO);
drv.glVertexAttribPointer(0, 3, eGL_FLOAT, GL_FALSE, sizeof(Vec3f), NULL);
drv.glEnableVertexAttribArray(0);
drv.glGenVertexArrays(1, &DebugData.frustumVAO);
drv.glBindVertexArray(DebugData.frustumVAO);
drv.glVertexAttribPointer(0, 3, eGL_FLOAT, GL_FALSE, sizeof(Vec3f),
(const void *)(sizeof(Vec3f) * 6));
drv.glEnableVertexAttribArray(0);
drv.glGenVertexArrays(1, &DebugData.triHighlightVAO);
drv.glBindVertexArray(DebugData.triHighlightVAO);
drv.glGenBuffers(1, &DebugData.triHighlightBuffer);
drv.glBindBuffer(eGL_ARRAY_BUFFER, DebugData.triHighlightBuffer);
drv.glNamedBufferDataEXT(DebugData.triHighlightBuffer, sizeof(Vec4f) * 24, NULL, eGL_DYNAMIC_DRAW);
drv.glVertexAttribPointer(0, 4, eGL_FLOAT, GL_FALSE, sizeof(Vec4f), NULL);
drv.glEnableVertexAttribArray(0);
MakeCurrentReplayContext(&m_ReplayCtx);
// try to identify the GPU we're running on.
{
RDCEraseEl(m_DriverInfo);
const char *vendor = (const char *)drv.glGetString(eGL_VENDOR);
const char *renderer = (const char *)drv.glGetString(eGL_RENDERER);
const char *version = (const char *)drv.glGetString(eGL_VERSION);
// we're just doing substring searches, so combine both for ease.
rdcstr combined = (vendor ? vendor : "");
combined += " ";
combined += (renderer ? renderer : "");
// make lowercase, for case-insensitive matching, and add preceding/trailing space for easier
// 'word' matching
combined = " " + strlower(combined) + " ";
RDCDEBUG("Identifying vendor from '%s'", combined.c_str());
struct pattern
{
const char *search;
GPUVendor vendor;
} patterns[] = {
{" arm ", GPUVendor::ARM},
{" mali ", GPUVendor::ARM},
{" mali-", GPUVendor::ARM},
{" amd ", GPUVendor::AMD},
{"advanced micro devices", GPUVendor::AMD},
{"ati technologies", GPUVendor::AMD},
{"radeon", GPUVendor::AMD},
{"broadcom", GPUVendor::Broadcom},
{"imagination", GPUVendor::Imagination},
{"powervr", GPUVendor::Imagination},
{"intel", GPUVendor::Intel},
{"geforce", GPUVendor::nVidia},
{"quadro", GPUVendor::nVidia},
{"nouveau", GPUVendor::nVidia},
{"nvidia", GPUVendor::nVidia},
{"adreno", GPUVendor::Qualcomm},
{"qualcomm", GPUVendor::Qualcomm},
{"vivante", GPUVendor::Verisilicon},
{"llvmpipe", GPUVendor::Software},
{"softpipe", GPUVendor::Software},
{"bluestacks", GPUVendor::Software},
};
for(const pattern &p : patterns)
{
if(combined.contains(p.search))
{
if(m_DriverInfo.vendor == GPUVendor::Unknown)
{
m_DriverInfo.vendor = p.vendor;
}
else
{
// either we already found this with another pattern, or we've identified two patterns and
// it's ambiguous. Keep the first one we found, arbitrarily, but print a warning.
if(m_DriverInfo.vendor != p.vendor)
{
RDCWARN("Already identified '%s' as %s, but now identified as %s", combined.c_str(),
ToStr(m_DriverInfo.vendor).c_str(), ToStr(p.vendor).c_str());
}
}
}
}
RDCDEBUG("Identified GPU vendor '%s'", ToStr(m_DriverInfo.vendor).c_str());
rdcstr versionString = version;
versionString += " / ";
versionString += renderer ? renderer : "";
versionString += " / ";
versionString += vendor ? vendor : "";
versionString.resize(RDCMIN(versionString.size(), ARRAY_COUNT(m_DriverInfo.version) - 1));
memcpy(m_DriverInfo.version, versionString.c_str(), versionString.size());
}
// these below need to be made on the replay context, as they are context-specific (not shared)
// and will be used on the replay context.
if(HasExt[ARB_transform_feedback2])
drv.glGenTransformFeedbacks(1, &DebugData.feedbackObj);
drv.glGenBuffers(1, &DebugData.feedbackBuffer);
DebugData.feedbackQueries.push_back(0);
drv.glGenQueries(1, &DebugData.feedbackQueries[0]);
if(HasExt[ARB_transform_feedback2])
drv.glBindTransformFeedback(eGL_TRANSFORM_FEEDBACK, DebugData.feedbackObj);
drv.glBindBuffer(eGL_TRANSFORM_FEEDBACK_BUFFER, DebugData.feedbackBuffer);
drv.glNamedBufferDataEXT(DebugData.feedbackBuffer, (GLsizeiptr)DebugData.feedbackBufferSize, NULL,
eGL_DYNAMIC_READ);
drv.glBindBufferBase(eGL_TRANSFORM_FEEDBACK_BUFFER, 0, DebugData.feedbackBuffer);
if(HasExt[ARB_transform_feedback2])
drv.glBindTransformFeedback(eGL_TRANSFORM_FEEDBACK, 0);
RenderDoc::Inst().SetProgress(LoadProgress::DebugManagerInit, 1.0f);
if(!HasExt[ARB_gpu_shader5])
{
RDCWARN(
"ARB_gpu_shader5 not supported, pixel picking and saving of integer textures may be "
"inaccurate.");
m_pDriver->AddDebugMessage(MessageCategory::Portability, MessageSeverity::Medium,
MessageSource::RuntimeWarning,
"ARB_gpu_shader5 not supported, pixel picking and saving of integer "
"textures may be inaccurate.");
m_Degraded = true;
}
if(!HasExt[ARB_stencil_texturing])
{
RDCWARN("ARB_stencil_texturing not supported, stencil values will not be displayed or picked.");
m_pDriver->AddDebugMessage(
MessageCategory::Portability, MessageSeverity::Medium, MessageSource::RuntimeWarning,
"ARB_stencil_texturing not supported, stencil values will not be displayed or picked.");
m_Degraded = true;
}
if(!HasExt[ARB_shader_image_load_store] || !HasExt[ARB_compute_shader] ||
!HasExt[ARB_shading_language_420pack])
{
RDCWARN(
"Don't have shader image load/store or compute shaders, functionality will be degraded.");
m_Degraded = true;
}
#if ENABLED(RDOC_APPLE)
// temporary hack - just never consider apple degraded, since there's never going to be an
// improvement so there's no point warning users.
m_Degraded = false;
#endif
m_pDriver->PopInternalShader();
}
void GLReplay::DeleteDebugData()
{
WrappedOpenGL &drv = *m_pDriver;
MakeCurrentReplayContext(&m_ReplayCtx);
GL.glDebugMessageCallback(NULL, NULL);
if(DebugData.overlayProg)
drv.glDeleteProgram(DebugData.overlayProg);
for(size_t i = 0; i < ARRAY_COUNT(DebugData.discardProg); i++)
if(DebugData.discardProg[i])
drv.glDeleteProgram(DebugData.discardProg[i]);
if(HasExt[ARB_transform_feedback2])
drv.glDeleteTransformFeedbacks(1, &DebugData.feedbackObj);
drv.glDeleteBuffers(1, &DebugData.feedbackBuffer);
drv.glDeleteQueries((GLsizei)DebugData.feedbackQueries.size(), DebugData.feedbackQueries.data());
MakeCurrentReplayContext(m_DebugCtx);
GL.glDebugMessageCallback(NULL, NULL);
ClearPostVSCache();
drv.glDeleteFramebuffers(1, &DebugData.overlayFBO);
drv.glDeleteTextures(1, &DebugData.overlayTex);
if(DebugData.quadoverdrawFragShader)
drv.glDeleteShader(DebugData.quadoverdrawFragShader);
if(DebugData.quadoverdrawFragShaderSPIRV)
drv.glDeleteShader(DebugData.quadoverdrawFragShaderSPIRV);
if(DebugData.quadoverdrawResolveProg)
drv.glDeleteProgram(DebugData.quadoverdrawResolveProg);
if(DebugData.texDisplayVertexShader)
drv.glDeleteShader(DebugData.texDisplayVertexShader);
for(int i = 0; i < 3; i++)
{
if(DebugData.texDisplayProg[i])
drv.glDeleteProgram(DebugData.texDisplayProg[i]);
if(DebugData.texRemapProg[i])
drv.glDeleteProgram(DebugData.texRemapProg[i]);
}
if(DebugData.checkerProg)
drv.glDeleteProgram(DebugData.checkerProg);
if(DebugData.fixedcolFragShader)
drv.glDeleteShader(DebugData.fixedcolFragShader);
if(DebugData.fixedcolFragShaderSPIRV)
drv.glDeleteShader(DebugData.fixedcolFragShaderSPIRV);
for(size_t i = 0; i < ARRAY_COUNT(DebugData.meshProg); i++)
{
if(DebugData.meshProg[i])
drv.glDeleteProgram(DebugData.meshProg[i]);
if(DebugData.meshgsProg[i])
drv.glDeleteProgram(DebugData.meshgsProg[i]);
}
if(DebugData.trisizeProg)
drv.glDeleteProgram(DebugData.trisizeProg);
drv.glDeleteBuffers(ARRAY_COUNT(DebugData.UBOs), DebugData.UBOs);
drv.glDeleteFramebuffers(1, &DebugData.pickPixelFBO);
drv.glDeleteTextures(1, &DebugData.pickPixelTex);
drv.glDeleteTextures(1, &DebugData.dummyTexBuffer);
drv.glDeleteBuffers(1, &DebugData.dummyTexBufferStore);
drv.glDeleteFramebuffers(1, &DebugData.customFBO);
drv.glDeleteTextures(1, &DebugData.customTex);
drv.glDeleteVertexArrays(1, &DebugData.emptyVAO);
for(int t = 1; t <= RESTYPE_TEXTYPEMAX; t++)
{
// float, uint, sint
for(int i = 0; i < 3; i++)
{
int idx = t;
if(i == 1)
idx |= TEXDISPLAY_UINT_TEX;
if(i == 2)
idx |= TEXDISPLAY_SINT_TEX;
if(DebugData.minmaxTileProgram[idx])
drv.glDeleteProgram(DebugData.minmaxTileProgram[idx]);
if(DebugData.histogramProgram[idx])
drv.glDeleteProgram(DebugData.histogramProgram[idx]);
if(t == 1)
{
if(DebugData.minmaxResultProgram[i])
drv.glDeleteProgram(DebugData.minmaxResultProgram[i]);
}
}
}
if(DebugData.meshPickProgram)
drv.glDeleteProgram(DebugData.meshPickProgram);
drv.glDeleteBuffers(1, &DebugData.pickIBBuf);
drv.glDeleteBuffers(1, &DebugData.pickVBBuf);
drv.glDeleteBuffers(1, &DebugData.pickResultBuf);
drv.glDeleteBuffers(1, &DebugData.minmaxTileResult);
drv.glDeleteBuffers(1, &DebugData.minmaxResult);
drv.glDeleteBuffers(1, &DebugData.histogramBuf);
drv.glDeleteVertexArrays(1, &DebugData.meshVAO);
drv.glDeleteVertexArrays(1, &DebugData.axisVAO);
drv.glDeleteVertexArrays(1, &DebugData.frustumVAO);
drv.glDeleteVertexArrays(1, &DebugData.triHighlightVAO);
drv.glDeleteBuffers(1, &DebugData.axisFrustumBuffer);
drv.glDeleteBuffers(1, &DebugData.triHighlightBuffer);
}
GLReplay::TextureSamplerState GLReplay::SetSamplerParams(GLenum target, GLuint texname,
TextureSamplerMode mode)
{
TextureSamplerState ret;
if(target == eGL_TEXTURE_BUFFER || target == eGL_TEXTURE_2D_MULTISAMPLE ||
target == eGL_TEXTURE_2D_MULTISAMPLE_ARRAY)
return ret;
// fetch previous texture sampler settings
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_MIN_FILTER, (GLint *)&ret.minFilter);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_MAG_FILTER, (GLint *)&ret.magFilter);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_S, (GLint *)&ret.wrapS);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_T, (GLint *)&ret.wrapT);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_R, (GLint *)&ret.wrapR);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_COMPARE_MODE, (GLint *)&ret.compareMode);
// disable depth comparison
GLenum compareMode = eGL_NONE;
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_COMPARE_MODE, (GLint *)&compareMode);
// always want to clamp
GLenum param = eGL_CLAMP_TO_EDGE;
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_S, (GLint *)¶m);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_T, (GLint *)¶m);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_R, (GLint *)¶m);
// depending on the mode, set min/mag filter
GLenum minFilter = eGL_NEAREST;
GLenum magFilter = eGL_NEAREST;
switch(mode)
{
case TextureSamplerMode::Point:
minFilter = eGL_NEAREST_MIPMAP_NEAREST;
magFilter = eGL_NEAREST;
break;
case TextureSamplerMode::PointNoMip:
minFilter = eGL_NEAREST;
magFilter = eGL_NEAREST;
break;
case TextureSamplerMode::Linear:
minFilter = eGL_LINEAR;
magFilter = eGL_LINEAR;
break;
}
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MIN_FILTER, (GLint *)&minFilter);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAG_FILTER, (GLint *)&magFilter);
return ret;
}
void GLReplay::RestoreSamplerParams(GLenum target, GLuint texname, TextureSamplerState state)
{
if(target == eGL_TEXTURE_BUFFER || target == eGL_TEXTURE_2D_MULTISAMPLE ||
target == eGL_TEXTURE_2D_MULTISAMPLE_ARRAY)
return;
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_S, (GLint *)&state.wrapS);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_T, (GLint *)&state.wrapT);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_WRAP_R, (GLint *)&state.wrapR);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MIN_FILTER, (GLint *)&state.minFilter);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAG_FILTER, (GLint *)&state.magFilter);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_COMPARE_MODE, (GLint *)&state.compareMode);
}
void GLReplay::FillWithDiscardPattern(DiscardType type, GLuint framebuffer, GLsizei numAttachments,
const GLenum *attachments, GLint x, GLint y, GLsizei width,
GLsizei height)
{
RDCASSERT(type == DiscardType::InvalidateCall);
WrappedOpenGL &drv = *m_pDriver;
GLMarkerRegion region("FillWithDiscardPattern FBO");
GLRenderState rs;
rs.FetchState(&drv);
// we use the original framebuffer, and mask off any attachments that shouldn't be discarded
drv.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, framebuffer);
int numviews = 1;
if(IsGLES && HasExt[OVR_multiview])
{
GL.glGetNamedFramebufferAttachmentParameterivEXT(
framebuffer, eGL_COLOR_ATTACHMENT0, eGL_FRAMEBUFFER_ATTACHMENT_TEXTURE_NUM_VIEWS_OVR,
&numviews);
}
GLuint prog = DebugData.discardProg[RDCCLAMP(numviews, 1, 4) - 1];
drv.glUseProgram(prog);
drv.glBindBufferBase(eGL_UNIFORM_BUFFER, 0, DebugData.discardPatternBuffer);
drv.glDisable(eGL_BLEND);
drv.glDisable(eGL_CULL_FACE);
drv.glEnable(eGL_DEPTH_TEST);
drv.glEnable(eGL_STENCIL_TEST);
drv.glDepthFunc(eGL_ALWAYS);
drv.glStencilFunc(eGL_ALWAYS, 0, 0xff);
if(!IsGLES)
drv.glPolygonMode(eGL_FRONT_AND_BACK, eGL_FILL);
// scissor to the rect
drv.glEnable(eGL_SCISSOR_TEST);
drv.glScissor(x, y, width, height);
GLint maxview[2] = {2048, 2048};
drv.glGetIntegerv(eGL_MAX_VIEWPORT_DIMS, maxview);
drv.glViewport(0, 0, maxview[0], maxview[1]);
// disable all masks at first
drv.glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
drv.glDepthMask(GL_FALSE);
drv.glStencilMask(0);
// enable masks specified by attachments
bool stencil = false;
for(GLsizei i = 0; i < numAttachments; i++)
{
if(attachments[i] == eGL_DEPTH_STENCIL_ATTACHMENT)
{
drv.glDepthMask(GL_TRUE);
drv.glStencilMask(0xff);
stencil = true;
}
else if(attachments[i] == eGL_DEPTH_ATTACHMENT)
{
drv.glDepthMask(GL_TRUE);
}
else if(attachments[i] == eGL_STENCIL_ATTACHMENT)
{
drv.glStencilMask(0xff);
stencil = true;
}
else
{
drv.glColorMaski(attachments[i] - eGL_COLOR_ATTACHMENT0, GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
}
}
GLint loc = drv.glGetUniformLocation(prog, "flags");
uint32_t flags = 0U;
if(height == 1)
flags |= 0x10U;
if(stencil)
{
drv.glStencilOp(eGL_REPLACE, eGL_REPLACE, eGL_REPLACE);
drv.glStencilFunc(eGL_ALWAYS, 0, 0xff);
drv.glProgramUniform1ui(prog, loc, flags | 1U);
drv.glDrawArrays(eGL_TRIANGLE_STRIP, 0, 4);
drv.glStencilFunc(eGL_ALWAYS, 0xff, 0xff);
drv.glProgramUniform1ui(prog, loc, flags | 2U);
drv.glDrawArrays(eGL_TRIANGLE_STRIP, 0, 4);
}
else
{
drv.glProgramUniform1ui(prog, loc, flags);
// if we're not writing stencil we can just do one draw
drv.glDrawArrays(eGL_TRIANGLE_STRIP, 0, 4);
}
rs.ApplyState(&drv);
}
void GLReplay::FillWithDiscardPattern(DiscardType type, ResourceId id, GLuint mip, GLint xoffset,
GLint yoffset, GLint zoffset, GLsizei width, GLsizei height,
GLsizei depth)
{
RDCASSERT(type == DiscardType::InvalidateCall);
WrappedOpenGL &drv = *m_pDriver;
GLMarkerRegion region("FillWithDiscardPattern Texture");
auto &texDetails = drv.m_Textures[id];
GLenum fmt = texDetails.internalFormat;
bytebuf &pattern = m_DiscardPatterns[fmt];
GLenum target = texDetails.curType;
if(pattern.empty())
pattern = GetDiscardPattern(type, MakeResourceFormat(target, fmt), 1, true);
bool compressed = IsCompressedFormat(fmt);
if(target == eGL_TEXTURE_CUBE_MAP)
depth = RDCMIN(depth, 6);
else
depth = RDCMIN(texDetails.depth, depth);
height = RDCMIN(texDetails.height, height);
width = RDCMIN(texDetails.width, width);
GLint dim = texDetails.dimension;
GLint oldRowLength = 0;
GL.glGetIntegerv(eGL_UNPACK_ROW_LENGTH, &oldRowLength);
// ensure even if we're uploading a sub-rect that the row length is the same.
GL.glPixelStorei(eGL_UNPACK_ROW_LENGTH, DiscardPatternWidth);
GLuint tex = texDetails.resource.name;
GLenum format = eGL_NONE;
GLenum datatype = eGL_NONE;
if(!compressed)
{
format = GetBaseFormat(fmt);
datatype = GetDataType(fmt);
}
for(GLsizei zc = 0; zc < depth; zc++)
{
GLsizei z = zoffset + zc;
if(texDetails.curType == eGL_TEXTURE_CUBE_MAP)
{
GLenum targets[] = {
eGL_TEXTURE_CUBE_MAP_POSITIVE_X, eGL_TEXTURE_CUBE_MAP_NEGATIVE_X,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Y, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
eGL_TEXTURE_CUBE_MAP_POSITIVE_Z, eGL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
};
target = targets[z % 6];
}
for(GLsizei yc = 0; yc < height; yc += DiscardPatternHeight)
{
GLsizei y = yoffset + yc;
for(GLsizei xc = 0; xc < width; xc += DiscardPatternWidth)
{
GLsizei x = xoffset + xc;
GLsizei subwidth = RDCMIN((GLsizei)DiscardPatternWidth, texDetails.width - x);
GLsizei subheight = RDCMIN((GLsizei)DiscardPatternHeight, texDetails.height - y);
if(compressed)
{
GLsizei dataSize = (GLsizei)GetCompressedByteSize(subwidth, subheight, 1, fmt);
if(dim == 1)
GL.glCompressedTextureSubImage1DEXT(tex, target, mip, x, subwidth, fmt, dataSize,
pattern.data());
else if(dim == 2)
GL.glCompressedTextureSubImage2DEXT(tex, target, mip, x, y, subwidth, subheight, fmt,
dataSize, pattern.data());
else if(dim == 3)
GL.glCompressedTextureSubImage3DEXT(tex, target, mip, x, y, z, subwidth, subheight, 1,
fmt, dataSize, pattern.data());
}
else
{
if(dim == 1)
GL.glTextureSubImage1DEXT(tex, target, mip, x, subwidth, format, datatype,
pattern.data());
else if(dim == 2)
GL.glTextureSubImage2DEXT(tex, target, mip, x, y, subwidth, subheight, format, datatype,
pattern.data());
else if(dim == 3)
GL.glTextureSubImage3DEXT(tex, target, mip, x, y, z, subwidth, subheight, 1, format,
datatype, pattern.data());
}
}
}
}
GL.glPixelStorei(eGL_UNPACK_ROW_LENGTH, oldRowLength);
}
void GLReplay::PickPixel(ResourceId texture, uint32_t x, uint32_t y, const Subresource &sub,
CompType typeCast, float pixel[4])
{
WrappedOpenGL &drv = *m_pDriver;
MakeCurrentReplayContext(m_DebugCtx);
drv.glBindFramebuffer(eGL_FRAMEBUFFER, DebugData.pickPixelFBO);
drv.glBindFramebuffer(eGL_READ_FRAMEBUFFER, DebugData.pickPixelFBO);
pixel[0] = pixel[1] = pixel[2] = pixel[3] = 0.0f;
drv.glClearBufferfv(eGL_COLOR, 0, pixel);
DebugData.outWidth = DebugData.outHeight = 1.0f;
drv.glViewport(0, 0, 1, 1);
TextureDisplay texDisplay;
texDisplay.red = texDisplay.green = texDisplay.blue = texDisplay.alpha = true;
texDisplay.flipY = false;
texDisplay.hdrMultiplier = -1.0f;
texDisplay.linearDisplayAsGamma = true;
texDisplay.subresource = sub;
texDisplay.customShaderId = ResourceId();
texDisplay.rangeMin = 0.0f;
texDisplay.rangeMax = 1.0f;
texDisplay.scale = 1.0f;
texDisplay.resourceId = texture;
texDisplay.typeCast = typeCast;
texDisplay.rawOutput = true;
texDisplay.xOffset = -float(x << sub.mip);
texDisplay.yOffset = -float(y << sub.mip);
RenderTextureInternal(texDisplay, eTexDisplay_MipShift);
drv.glReadPixels(0, 0, 1, 1, eGL_RGBA, eGL_FLOAT, (void *)pixel);
if(!HasExt[ARB_gpu_shader5])
{
auto &texDetails = m_pDriver->m_Textures[texDisplay.resourceId];
if(IsSIntFormat(texDetails.internalFormat))
{
int32_t casted[4] = {
(int32_t)pixel[0], (int32_t)pixel[1], (int32_t)pixel[2], (int32_t)pixel[3],
};
memcpy(pixel, casted, sizeof(casted));
}
else if(IsUIntFormat(texDetails.internalFormat))
{
uint32_t casted[4] = {
(uint32_t)pixel[0], (uint32_t)pixel[1], (uint32_t)pixel[2], (uint32_t)pixel[3],
};
memcpy(pixel, casted, sizeof(casted));
}
}
{
auto &texDetails = m_pDriver->m_Textures[texture];
// need to read stencil separately as GL can't read both depth and stencil
// at the same time.
if(texDetails.internalFormat == eGL_DEPTH24_STENCIL8 ||
texDetails.internalFormat == eGL_DEPTH32F_STENCIL8 ||
texDetails.internalFormat == eGL_DEPTH_STENCIL ||
texDetails.internalFormat == eGL_STENCIL_INDEX8)
{
texDisplay.red = texDisplay.blue = texDisplay.alpha = false;
RenderTextureInternal(texDisplay, eTexDisplay_MipShift);
uint32_t stencilpixel[4];
drv.glReadPixels(0, 0, 1, 1, eGL_RGBA, eGL_FLOAT, (void *)stencilpixel);
if(!HasExt[ARB_gpu_shader5])
{
// bits weren't aliased, so re-cast back to uint.
float fpix[4];
memcpy(fpix, stencilpixel, sizeof(fpix));
stencilpixel[0] = (uint32_t)fpix[0];
stencilpixel[1] = (uint32_t)fpix[1];
}
// not sure whether [0] or [1] will return stencil values, so use
// max of two because other channel should be 0
pixel[1] = float(RDCMAX(stencilpixel[0], stencilpixel[1])) / 255.0f;
// the first depth read will have read stencil instead.
// NULL it out so the UI sees only stencil
if(texDetails.internalFormat == eGL_STENCIL_INDEX8)
{
pixel[1] = float(RDCMAX(stencilpixel[0], stencilpixel[1])) / 255.0f;
pixel[0] = 0.0f;
}
}
}
}
bool GLReplay::GetMinMax(ResourceId texid, const Subresource &sub, CompType typeCast, float *minval,
float *maxval)
{
auto &texDetails = m_pDriver->m_Textures[texid];
if(!IsCompressedFormat(texDetails.internalFormat) &&
GetBaseFormat(texDetails.internalFormat) == eGL_DEPTH_STENCIL)
{
// for depth/stencil we need to run the code twice - once to fetch depth and once to fetch
// stencil - since we can't process float depth and int stencil at the same time
Vec4f depth[2] = {
{0.0f, 0.0f, 0.0f, 0.0f}, {1.0f, 1.0f, 1.0f, 1.0f},
};
Vec4u stencil[2] = {{0, 0, 0, 0}, {1, 1, 1, 1}};
bool success = GetMinMax(texid, sub, typeCast, false, &depth[0].x, &depth[1].x);
if(!success)
return false;
success = GetMinMax(texid, sub, typeCast, true, (float *)&stencil[0].x, (float *)&stencil[1].x);
if(!success)
return false;
// copy across into green channel, casting up to float, dividing by the range for this texture
float rangeScale = 1.0f;
switch(texDetails.internalFormat)
{
case eGL_STENCIL_INDEX1: rangeScale = 1.0f; break;
case eGL_STENCIL_INDEX4: rangeScale = 16.0f; break;
default: RDCWARN("Unexpected raw format for stencil visualization"); DELIBERATE_FALLTHROUGH();
case eGL_DEPTH24_STENCIL8:
case eGL_DEPTH32F_STENCIL8:
case eGL_DEPTH_STENCIL:
case eGL_STENCIL_INDEX8: rangeScale = 255.0f; break;
case eGL_STENCIL_INDEX16: rangeScale = 65535.0f; break;
}
depth[0].y = float(stencil[0].x) / rangeScale;
depth[1].y = float(stencil[1].x) / rangeScale;
memcpy(minval, &depth[0], sizeof(depth[0]));
memcpy(maxval, &depth[1], sizeof(depth[1]));
return true;
}
return GetMinMax(texid, sub, typeCast, false, minval, maxval);
}
bool GLReplay::GetMinMax(ResourceId texid, const Subresource &sub, CompType typeCast, bool stencil,
float *minval, float *maxval)
{
if(texid == ResourceId() || m_pDriver->m_Textures.find(texid) == m_pDriver->m_Textures.end())
return false;
if(!HasExt[ARB_compute_shader] || !HasExt[ARB_shading_language_420pack])
return false;
auto &texDetails = m_pDriver->m_Textures[texid];
TextureDescription details = GetTexture(texid);
int texSlot = 0;
int intIdx = 0;
bool renderbuffer = false;
switch(texDetails.curType)
{
case eGL_RENDERBUFFER:
texSlot = RESTYPE_TEX2D;
renderbuffer = true;
break;
case eGL_TEXTURE_1D: texSlot = RESTYPE_TEX1D; break;
default: RDCWARN("Unexpected texture type"); DELIBERATE_FALLTHROUGH();
case eGL_TEXTURE_2D: texSlot = RESTYPE_TEX2D; break;
case eGL_TEXTURE_2D_MULTISAMPLE: texSlot = RESTYPE_TEX2DMS; break;
case eGL_TEXTURE_2D_MULTISAMPLE_ARRAY: texSlot = RESTYPE_TEX2DMSARRAY; break;
case eGL_TEXTURE_RECTANGLE: texSlot = RESTYPE_TEXRECT; break;
case eGL_TEXTURE_BUFFER: texSlot = RESTYPE_TEXBUFFER; break;
case eGL_TEXTURE_3D: texSlot = RESTYPE_TEX3D; break;
case eGL_TEXTURE_CUBE_MAP: texSlot = RESTYPE_TEXCUBE; break;
case eGL_TEXTURE_1D_ARRAY: texSlot = RESTYPE_TEX1DARRAY; break;
case eGL_TEXTURE_2D_ARRAY: texSlot = RESTYPE_TEX2DARRAY; break;
case eGL_TEXTURE_CUBE_MAP_ARRAY: texSlot = RESTYPE_TEXCUBEARRAY; break;
}
GLenum target = texDetails.curType;
GLuint texname = texDetails.resource.name;
// do blit from renderbuffer to texture, then sample from texture
if(renderbuffer)
{
// need replay context active to do blit (as FBOs aren't shared)
MakeCurrentReplayContext(&m_ReplayCtx);
GLuint curDrawFBO = 0;
GLuint curReadFBO = 0;
GL.glGetIntegerv(eGL_DRAW_FRAMEBUFFER_BINDING, (GLint *)&curDrawFBO);
GL.glGetIntegerv(eGL_READ_FRAMEBUFFER_BINDING, (GLint *)&curReadFBO);
GL.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, texDetails.renderbufferFBOs[1]);
GL.glBindFramebuffer(eGL_READ_FRAMEBUFFER, texDetails.renderbufferFBOs[0]);
SafeBlitFramebuffer(
0, 0, texDetails.width, texDetails.height, 0, 0, texDetails.width, texDetails.height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, eGL_NEAREST);
GL.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, curDrawFBO);
GL.glBindFramebuffer(eGL_READ_FRAMEBUFFER, curReadFBO);
texname = texDetails.renderbufferReadTex;
target = eGL_TEXTURE_2D;
}
MakeCurrentReplayContext(m_DebugCtx);
RDCGLenum dsTexMode = eGL_NONE;
if(IsDepthStencilFormat(texDetails.internalFormat))
{
if(stencil)
{
dsTexMode = eGL_STENCIL_INDEX;
intIdx = 1;
}
else
{
dsTexMode = eGL_DEPTH_COMPONENT;
}
}
else
{
if(details.format.compType == CompType::UInt)
intIdx = 1;
if(details.format.compType == CompType::SInt)
intIdx = 2;
}
GL.glBindBufferBase(eGL_UNIFORM_BUFFER, 2, DebugData.UBOs[0]);
HistogramUBOData *cdata =
(HistogramUBOData *)GL.glMapBufferRange(eGL_UNIFORM_BUFFER, 0, sizeof(HistogramUBOData),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
cdata->HistogramTextureResolution.x = (float)RDCMAX(details.width >> sub.mip, 1U);
cdata->HistogramTextureResolution.y = (float)RDCMAX(details.height >> sub.mip, 1U);
cdata->HistogramTextureResolution.z = (float)RDCMAX(details.depth >> sub.mip, 1U);
if(texDetails.curType == eGL_TEXTURE_3D)
cdata->HistogramSlice =
(float)RDCCLAMP(sub.slice, 0U, uint32_t(details.depth >> sub.mip) - 1) + 0.001f;
else
cdata->HistogramSlice = (float)RDCCLAMP(sub.slice, 0U, details.arraysize - 1) + 0.001f;
cdata->HistogramMip = (int)sub.mip;
cdata->HistogramNumSamples = texDetails.samples;
cdata->HistogramSample = (int)RDCCLAMP(sub.sample, 0U, details.msSamp - 1);
if(sub.sample == ~0U)
cdata->HistogramSample = -int(details.msSamp);
cdata->HistogramMin = 0.0f;
cdata->HistogramMax = 1.0f;
cdata->HistogramChannels = 0xf;
cdata->HistogramYUVDownsampleRate = {};
cdata->HistogramYUVAChannels = {};
int progIdx = texSlot;
if(intIdx == 1)
progIdx |= TEXDISPLAY_UINT_TEX;
if(intIdx == 2)
progIdx |= TEXDISPLAY_SINT_TEX;
int blocksX = (int)ceil(cdata->HistogramTextureResolution.x /
float(HGRAM_PIXELS_PER_TILE * HGRAM_TILES_PER_BLOCK));
int blocksY = (int)ceil(cdata->HistogramTextureResolution.y /
float(HGRAM_PIXELS_PER_TILE * HGRAM_TILES_PER_BLOCK));
GL.glUnmapBuffer(eGL_UNIFORM_BUFFER);
GL.glActiveTexture((RDCGLenum)(eGL_TEXTURE0 + texSlot));
GL.glBindTexture(target, texname);
TextureSamplerMode mode = TextureSamplerMode::Point;
if(texSlot == RESTYPE_TEXRECT || texSlot == RESTYPE_TEXBUFFER)
mode = TextureSamplerMode::PointNoMip;
TextureSamplerState prevSampState = SetSamplerParams(target, texname, mode);
GLint origDSTexMode = eGL_DEPTH_COMPONENT;
if(dsTexMode != eGL_NONE && HasExt[ARB_stencil_texturing])
{
GL.glGetTextureParameterivEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, &origDSTexMode);
GL.glTextureParameteriEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, dsTexMode);
}
GLint baseLevel[4] = {-1};
GLint maxlevel[4] = {-1};
GLint forcedparam[4] = {};
bool levelsTex = (target != eGL_TEXTURE_BUFFER && target != eGL_TEXTURE_2D_MULTISAMPLE &&
target != eGL_TEXTURE_2D_MULTISAMPLE_ARRAY);
if(levelsTex)
{
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, baseLevel);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, maxlevel);
}
else
{
baseLevel[0] = maxlevel[0] = -1;
}
// ensure texture is mipmap complete and we can view all mips (if the range has been reduced) by
// forcing TEXTURE_MAX_LEVEL to cover all valid mips.
if(levelsTex && texid != DebugData.CustomShaderTexID)
{
forcedparam[0] = 0;
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, forcedparam);
forcedparam[0] = GLint(details.mips - 1);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, forcedparam);
}
else
{
maxlevel[0] = -1;
}
GL.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 0, DebugData.minmaxTileResult);
GL.glUseProgram(DebugData.minmaxTileProgram[progIdx]);
GL.glDispatchCompute(blocksX, blocksY, 1);
GL.glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
GL.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 0, DebugData.minmaxResult);
GL.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 1, DebugData.minmaxTileResult);
GL.glUseProgram(DebugData.minmaxResultProgram[intIdx]);
GL.glDispatchCompute(1, 1, 1);
GL.glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
Vec4f minmax[2];
GL.glBindBuffer(eGL_COPY_READ_BUFFER, DebugData.minmaxResult);
GL.glGetBufferSubData(eGL_COPY_READ_BUFFER, 0, sizeof(minmax), minmax);
if(baseLevel[0] >= 0)
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, baseLevel);
if(maxlevel[0] >= 0)
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, maxlevel);
RestoreSamplerParams(target, texname, prevSampState);
minval[0] = minmax[0].x;
minval[1] = minmax[0].y;
minval[2] = minmax[0].z;
minval[3] = minmax[0].w;
maxval[0] = minmax[1].x;
maxval[1] = minmax[1].y;
maxval[2] = minmax[1].z;
maxval[3] = minmax[1].w;
if(dsTexMode != eGL_NONE && HasExt[ARB_stencil_texturing])
GL.glTextureParameteriEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, origDSTexMode);
return true;
}
bool GLReplay::GetHistogram(ResourceId texid, const Subresource &sub, CompType typeCast, float minval,
float maxval, bool channels[4], rdcarray<uint32_t> &histogram)
{
if(minval >= maxval || texid == ResourceId())
return false;
if(m_pDriver->m_Textures.find(texid) == m_pDriver->m_Textures.end())
return false;
if(!HasExt[ARB_compute_shader] || !HasExt[ARB_shading_language_420pack])
return false;
auto &texDetails = m_pDriver->m_Textures[texid];
TextureDescription details = GetTexture(texid);
int texSlot = 0;
int intIdx = 0;
bool renderbuffer = false;
switch(texDetails.curType)
{
case eGL_RENDERBUFFER:
texSlot = RESTYPE_TEX2D;
renderbuffer = true;
break;
case eGL_TEXTURE_1D: texSlot = RESTYPE_TEX1D; break;
default: RDCWARN("Unexpected texture type"); DELIBERATE_FALLTHROUGH();
case eGL_TEXTURE_2D: texSlot = RESTYPE_TEX2D; break;
case eGL_TEXTURE_2D_MULTISAMPLE: texSlot = RESTYPE_TEX2DMS; break;
case eGL_TEXTURE_2D_MULTISAMPLE_ARRAY: texSlot = RESTYPE_TEX2DMSARRAY; break;
case eGL_TEXTURE_RECTANGLE: texSlot = RESTYPE_TEXRECT; break;
case eGL_TEXTURE_BUFFER: texSlot = RESTYPE_TEXBUFFER; break;
case eGL_TEXTURE_3D: texSlot = RESTYPE_TEX3D; break;
case eGL_TEXTURE_CUBE_MAP: texSlot = RESTYPE_TEXCUBE; break;
case eGL_TEXTURE_1D_ARRAY: texSlot = RESTYPE_TEX1DARRAY; break;
case eGL_TEXTURE_2D_ARRAY: texSlot = RESTYPE_TEX2DARRAY; break;
case eGL_TEXTURE_CUBE_MAP_ARRAY: texSlot = RESTYPE_TEXCUBEARRAY; break;
}
GLenum target = texDetails.curType;
GLuint texname = texDetails.resource.name;
// do blit from renderbuffer to texture, then sample from texture
if(renderbuffer)
{
// need replay context active to do blit (as FBOs aren't shared)
MakeCurrentReplayContext(&m_ReplayCtx);
GLuint curDrawFBO = 0;
GLuint curReadFBO = 0;
GL.glGetIntegerv(eGL_DRAW_FRAMEBUFFER_BINDING, (GLint *)&curDrawFBO);
GL.glGetIntegerv(eGL_READ_FRAMEBUFFER_BINDING, (GLint *)&curReadFBO);
GL.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, texDetails.renderbufferFBOs[1]);
GL.glBindFramebuffer(eGL_READ_FRAMEBUFFER, texDetails.renderbufferFBOs[0]);
SafeBlitFramebuffer(
0, 0, texDetails.width, texDetails.height, 0, 0, texDetails.width, texDetails.height,
GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT, eGL_NEAREST);
GL.glBindFramebuffer(eGL_DRAW_FRAMEBUFFER, curDrawFBO);
GL.glBindFramebuffer(eGL_READ_FRAMEBUFFER, curReadFBO);
texname = texDetails.renderbufferReadTex;
target = eGL_TEXTURE_2D;
}
MakeCurrentReplayContext(m_DebugCtx);
RDCGLenum dsTexMode = eGL_NONE;
if(IsDepthStencilFormat(texDetails.internalFormat))
{
// stencil-only, make sure we display it as such
if(texDetails.internalFormat == eGL_STENCIL_INDEX8)
{
channels[0] = false;
channels[1] = true;
channels[2] = false;
channels[3] = false;
}
// depth-only, make sure we display it as such
if(GetBaseFormat(texDetails.internalFormat) == eGL_DEPTH_COMPONENT)
{
channels[0] = true;
channels[1] = false;
channels[2] = false;
channels[3] = false;
}
if(!channels[0] && channels[1])
{
dsTexMode = eGL_STENCIL_INDEX;
// Stencil texture sampling is not normalized in OpenGL
intIdx = 1;
float rangeScale = 1.0f;
switch(texDetails.internalFormat)
{
case eGL_STENCIL_INDEX1: rangeScale = 1.0f; break;
case eGL_STENCIL_INDEX4: rangeScale = 16.0f; break;
default:
RDCWARN("Unexpected raw format for stencil visualization");
DELIBERATE_FALLTHROUGH();
case eGL_DEPTH24_STENCIL8:
case eGL_DEPTH32F_STENCIL8:
case eGL_DEPTH_STENCIL:
case eGL_STENCIL_INDEX8: rangeScale = 255.0f; break;
case eGL_STENCIL_INDEX16: rangeScale = 65535.0f; break;
}
minval *= rangeScale;
maxval *= rangeScale;
}
else
{
dsTexMode = eGL_DEPTH_COMPONENT;
}
}
else
{
if(details.format.compType == CompType::UInt)
intIdx = 1;
if(details.format.compType == CompType::SInt)
intIdx = 2;
}
GL.glBindBufferBase(eGL_UNIFORM_BUFFER, 2, DebugData.UBOs[0]);
HistogramUBOData *cdata =
(HistogramUBOData *)GL.glMapBufferRange(eGL_UNIFORM_BUFFER, 0, sizeof(HistogramUBOData),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
cdata->HistogramTextureResolution.x = (float)RDCMAX(details.width >> sub.mip, 1U);
cdata->HistogramTextureResolution.y = (float)RDCMAX(details.height >> sub.mip, 1U);
cdata->HistogramTextureResolution.z = (float)RDCMAX(details.depth >> sub.mip, 1U);
if(texDetails.curType == eGL_TEXTURE_3D)
cdata->HistogramSlice =
(float)RDCCLAMP(sub.slice, 0U, uint32_t(details.depth >> sub.mip) - 1) + 0.001f;
else
cdata->HistogramSlice = (float)RDCCLAMP(sub.slice, 0U, details.arraysize - 1) + 0.001f;
cdata->HistogramMip = sub.mip;
cdata->HistogramNumSamples = texDetails.samples;
cdata->HistogramSample = (int)RDCCLAMP(sub.sample, 0U, details.msSamp - 1);
if(sub.sample == ~0U)
cdata->HistogramSample = -int(details.msSamp);
cdata->HistogramMin = minval;
cdata->HistogramYUVDownsampleRate = {};
cdata->HistogramYUVAChannels = {};
// The calculation in the shader normalises each value between min and max, then multiplies by the
// number of buckets.
// But any value equal to HistogramMax must go into NUM_BUCKETS-1, so add a small delta.
cdata->HistogramMax = maxval + maxval * 1e-6f;
cdata->HistogramChannels = 0;
if(dsTexMode == eGL_NONE)
{
if(channels[0])
cdata->HistogramChannels |= 0x1;
if(channels[1])
cdata->HistogramChannels |= 0x2;
if(channels[2])
cdata->HistogramChannels |= 0x4;
if(channels[3])
cdata->HistogramChannels |= 0x8;
}
else
{
// Both depth and stencil texture mode use the red channel
cdata->HistogramChannels |= 0x1;
}
cdata->HistogramFlags = 0;
int progIdx = texSlot;
if(intIdx == 1)
progIdx |= TEXDISPLAY_UINT_TEX;
if(intIdx == 2)
progIdx |= TEXDISPLAY_SINT_TEX;
int blocksX = (int)ceil(cdata->HistogramTextureResolution.x /
float(HGRAM_PIXELS_PER_TILE * HGRAM_TILES_PER_BLOCK));
int blocksY = (int)ceil(cdata->HistogramTextureResolution.y /
float(HGRAM_PIXELS_PER_TILE * HGRAM_TILES_PER_BLOCK));
GL.glUnmapBuffer(eGL_UNIFORM_BUFFER);
GL.glActiveTexture((RDCGLenum)(eGL_TEXTURE0 + texSlot));
GL.glBindTexture(target, texname);
TextureSamplerMode mode = TextureSamplerMode::Point;
if(texSlot == RESTYPE_TEXRECT || texSlot == RESTYPE_TEXBUFFER)
mode = TextureSamplerMode::PointNoMip;
TextureSamplerState prevSampState = SetSamplerParams(target, texname, mode);
GLint origDSTexMode = eGL_DEPTH_COMPONENT;
if(dsTexMode != eGL_NONE && HasExt[ARB_stencil_texturing])
{
GL.glGetTextureParameterivEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, &origDSTexMode);
GL.glTextureParameteriEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, dsTexMode);
}
GLint baseLevel[4] = {-1};
GLint maxlevel[4] = {-1};
GLint forcedparam[4] = {};
bool levelsTex = (target != eGL_TEXTURE_BUFFER && target != eGL_TEXTURE_2D_MULTISAMPLE &&
target != eGL_TEXTURE_2D_MULTISAMPLE_ARRAY);
if(levelsTex)
{
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, baseLevel);
GL.glGetTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, maxlevel);
}
else
{
baseLevel[0] = maxlevel[0] = -1;
}
// ensure texture is mipmap complete and we can view all mips (if the range has been reduced) by
// forcing TEXTURE_MAX_LEVEL to cover all valid mips.
if(levelsTex && texid != DebugData.CustomShaderTexID)
{
forcedparam[0] = 0;
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, forcedparam);
forcedparam[0] = GLint(details.mips - 1);
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, forcedparam);
}
else
{
maxlevel[0] = -1;
}
GL.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 0, DebugData.histogramBuf);
GLuint zero = 0;
GL.glClearBufferData(eGL_SHADER_STORAGE_BUFFER, eGL_R32UI, eGL_RED_INTEGER, eGL_UNSIGNED_INT,
&zero);
GL.glUseProgram(DebugData.histogramProgram[progIdx]);
GL.glDispatchCompute(blocksX, blocksY, 1);
GL.glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
histogram.clear();
histogram.resize(HGRAM_NUM_BUCKETS);
GL.glBindBuffer(eGL_COPY_READ_BUFFER, DebugData.histogramBuf);
GL.glGetBufferSubData(eGL_COPY_READ_BUFFER, 0, sizeof(uint32_t) * HGRAM_NUM_BUCKETS, &histogram[0]);
if(baseLevel[0] >= 0)
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_BASE_LEVEL, baseLevel);
if(maxlevel[0] >= 0)
GL.glTextureParameterivEXT(texname, target, eGL_TEXTURE_MAX_LEVEL, maxlevel);
RestoreSamplerParams(target, texname, prevSampState);
if(dsTexMode != eGL_NONE && HasExt[ARB_stencil_texturing])
GL.glTextureParameteriEXT(texname, target, eGL_DEPTH_STENCIL_TEXTURE_MODE, origDSTexMode);
return true;
}
uint32_t GLReplay::PickVertex(uint32_t eventId, int32_t width, int32_t height,
const MeshDisplay &cfg, uint32_t x, uint32_t y)
{
WrappedOpenGL &drv = *m_pDriver;
if(!HasExt[ARB_compute_shader] || !HasExt[ARB_shading_language_420pack])
return ~0U;
MakeCurrentReplayContext(m_DebugCtx);
drv.glUseProgram(DebugData.meshPickProgram);
Matrix4f projMat = Matrix4f::Perspective(90.0f, 0.1f, 100000.0f, float(width) / float(height));
Matrix4f camMat = cfg.cam ? ((Camera *)cfg.cam)->GetMatrix() : Matrix4f::Identity();
Matrix4f pickMVP = projMat.Mul(camMat);
Matrix4f pickMVPProj;
if(cfg.position.unproject)
{
// the derivation of the projection matrix might not be right (hell, it could be an
// orthographic projection). But it'll be close enough likely.
Matrix4f guessProj =
cfg.position.farPlane != FLT_MAX
? Matrix4f::Perspective(cfg.fov, cfg.position.nearPlane, cfg.position.farPlane, cfg.aspect)
: Matrix4f::ReversePerspective(cfg.fov, cfg.position.nearPlane, cfg.aspect);
if(cfg.ortho)
guessProj = Matrix4f::Orthographic(cfg.position.nearPlane, cfg.position.farPlane);
if(cfg.position.flipY)
guessProj[5] *= -1.0f;
pickMVPProj = projMat.Mul(camMat.Mul(guessProj.Inverse()));
}
vec3 rayPos;
vec3 rayDir;
// convert mouse pos to world space ray
{
Matrix4f inversePickMVP = pickMVP.Inverse();
float pickX = ((float)x) / ((float)width);
float pickXCanonical = RDCLERP(-1.0f, 1.0f, pickX);
float pickY = ((float)y) / ((float)height);
// flip the Y axis
float pickYCanonical = RDCLERP(1.0f, -1.0f, pickY);
vec3 cameraToWorldNearPosition =
inversePickMVP.Transform(Vec3f(pickXCanonical, pickYCanonical, -1), 1);
vec3 cameraToWorldFarPosition =
inversePickMVP.Transform(Vec3f(pickXCanonical, pickYCanonical, 1), 1);
vec3 testDir = (cameraToWorldFarPosition - cameraToWorldNearPosition);
testDir.Normalise();
// Calculate the ray direction first in the regular way (above), so we can use the
// the output for testing if the ray we are picking is negative or not. This is similar
// to checking against the forward direction of the camera, but more robust
if(cfg.position.unproject)
{
Matrix4f inversePickMVPGuess = pickMVPProj.Inverse();
vec3 nearPosProj = inversePickMVPGuess.Transform(Vec3f(pickXCanonical, pickYCanonical, -1), 1);
vec3 farPosProj = inversePickMVPGuess.Transform(Vec3f(pickXCanonical, pickYCanonical, 1), 1);
rayDir = (farPosProj - nearPosProj);
rayDir.Normalise();
if(testDir.z < 0)
{
rayDir = -rayDir;
}
rayPos = nearPosProj;
}
else
{
rayDir = testDir;
rayPos = cameraToWorldNearPosition;
}
}
GLuint ib = 0;
uint32_t minIndex = 0;
uint32_t maxIndex = cfg.position.numIndices;
uint32_t idxclamp = 0;
if(cfg.position.baseVertex < 0)
idxclamp = uint32_t(-cfg.position.baseVertex);
if(cfg.position.indexByteStride && cfg.position.indexResourceId != ResourceId())
ib = m_pDriver->GetResourceManager()->GetCurrentResource(cfg.position.indexResourceId).name;
const bool fandecode =
(cfg.position.topology == Topology::TriangleFan && cfg.position.allowRestart);
uint32_t numIndices = cfg.position.numIndices;
// We copy into our own buffers to promote to the target type (uint32) that the shader expects.
// Most IBs will be 16-bit indices, most VBs will not be float4. We also apply baseVertex here
if(ib)
{
rdcarray<uint32_t> idxtmp;
// if it's a triangle fan that allows restart, we'll have to unpack it.
// Allocate enough space for the list on the GPU, and enough temporary space to upcast into
// first
if(fandecode)
{
idxtmp.resize(numIndices);
numIndices *= 3;
}
// resize up on demand
if(DebugData.pickIBBuf == 0 || DebugData.pickIBSize < numIndices * sizeof(uint32_t))
{
drv.glDeleteBuffers(1, &DebugData.pickIBBuf);
drv.glGenBuffers(1, &DebugData.pickIBBuf);
drv.glBindBuffer(eGL_SHADER_STORAGE_BUFFER, DebugData.pickIBBuf);
drv.glNamedBufferDataEXT(DebugData.pickIBBuf, numIndices * sizeof(uint32_t), NULL,
eGL_STREAM_DRAW);
DebugData.pickIBSize = numIndices * sizeof(uint32_t);
}
byte *idxs = new byte[numIndices * cfg.position.indexByteStride];
memset(idxs, 0, numIndices * cfg.position.indexByteStride);
rdcarray<uint32_t> outidxs;
outidxs.resize(numIndices);
drv.glBindBuffer(eGL_COPY_READ_BUFFER, ib);
GLuint bufsize = 0;
drv.glGetBufferParameteriv(eGL_COPY_READ_BUFFER, eGL_BUFFER_SIZE, (GLint *)&bufsize);
drv.glGetBufferSubData(eGL_COPY_READ_BUFFER, (GLintptr)cfg.position.indexByteOffset,
RDCMIN(uint32_t(bufsize) - uint32_t(cfg.position.indexByteOffset),
cfg.position.numIndices * cfg.position.indexByteStride),
idxs);
uint8_t *idxs8 = (uint8_t *)idxs;
uint16_t *idxs16 = (uint16_t *)idxs;
uint32_t *idxs32 = (uint32_t *)idxs;
size_t idxcount = 0;
if(cfg.position.indexByteStride == 1)
{
for(uint32_t i = 0; i < cfg.position.numIndices; i++)
{
uint32_t idx = idxs8[i];
if(idx < idxclamp)
idx = 0;
else if(cfg.position.baseVertex < 0)
idx -= idxclamp;
else if(cfg.position.baseVertex > 0)
idx += cfg.position.baseVertex;
if(i == 0)
{
minIndex = maxIndex = idx;
}
else
{
minIndex = RDCMIN(idx, minIndex);
maxIndex = RDCMAX(idx, maxIndex);
}
outidxs[i] = idx;
idxcount++;
}
}
else if(cfg.position.indexByteStride == 2)
{
for(uint32_t i = 0; i < cfg.position.numIndices; i++)
{
uint32_t idx = idxs16[i];
if(idx < idxclamp)
idx = 0;
else if(cfg.position.baseVertex < 0)
idx -= idxclamp;
else if(cfg.position.baseVertex > 0)
idx += cfg.position.baseVertex;
if(i == 0)
{
minIndex = maxIndex = idx;
}
else
{
minIndex = RDCMIN(idx, minIndex);
maxIndex = RDCMAX(idx, maxIndex);
}
outidxs[i] = idx;
idxcount++;
}
}
else
{
for(uint32_t i = 0; i < cfg.position.numIndices; i++)
{
uint32_t idx = idxs32[i];
if(idx < idxclamp)
idx = 0;
else if(cfg.position.baseVertex < 0)
idx -= idxclamp;
else if(cfg.position.baseVertex > 0)
idx += cfg.position.baseVertex;
if(i == 0)
{
minIndex = maxIndex = idx;
}
else
{
minIndex = RDCMIN(idx, minIndex);
maxIndex = RDCMAX(idx, maxIndex);
}
outidxs[i] = idx;
idxcount++;
}
}
// if it's a triangle fan that allows restart, unpack it
if(cfg.position.topology == Topology::TriangleFan && cfg.position.allowRestart)
{
// resize to how many indices were actually read
outidxs.resize(idxcount);
// patch the index buffer
PatchTriangleFanRestartIndexBufer(outidxs, cfg.position.restartIndex);
for(uint32_t &idx : idxtmp)
{
if(idx == cfg.position.restartIndex)
idx = 0;
}
numIndices = (uint32_t)outidxs.size();
}
drv.glBindBuffer(eGL_SHADER_STORAGE_BUFFER, DebugData.pickIBBuf);
drv.glBufferSubData(eGL_SHADER_STORAGE_BUFFER, 0, numIndices * sizeof(uint32_t), outidxs.data());
}
// unpack and linearise the data
{
bytebuf oldData;
GetBufferData(cfg.position.vertexResourceId, cfg.position.vertexByteOffset, 0, oldData);
// clamp maxIndex to upper bound in case we got invalid indices or primitive restart indices
maxIndex = RDCMIN(maxIndex, uint32_t(oldData.size() / RDCMAX(1U, cfg.position.vertexByteStride)));
if(DebugData.pickVBBuf == 0 || DebugData.pickVBSize < (maxIndex + 1) * sizeof(Vec4f))
{
drv.glDeleteBuffers(1, &DebugData.pickVBBuf);
drv.glGenBuffers(1, &DebugData.pickVBBuf);
drv.glBindBuffer(eGL_SHADER_STORAGE_BUFFER, DebugData.pickVBBuf);
drv.glNamedBufferDataEXT(DebugData.pickVBBuf, (maxIndex + 1) * sizeof(Vec4f), NULL,
eGL_DYNAMIC_DRAW);
DebugData.pickVBSize = (maxIndex + 1) * sizeof(Vec4f);
}
rdcarray<FloatVector> vbData;
vbData.resize(maxIndex + 1);
byte *data = &oldData[0];
byte *dataEnd = data + oldData.size();
bool valid;
// the index buffer may refer to vertices past the start of the vertex buffer, so we can't just
// conver the first N vertices we'll need.
// Instead we grab min and max above, and convert every vertex in that range. This might
// slightly over-estimate but not as bad as 0-max or the whole buffer.
for(uint32_t idx = minIndex; idx <= maxIndex; idx++)
vbData[idx] = HighlightCache::InterpretVertex(data, idx, cfg.position.vertexByteStride,
cfg.position.format, dataEnd, valid);
drv.glBindBuffer(eGL_SHADER_STORAGE_BUFFER, DebugData.pickVBBuf);
drv.glBufferSubData(eGL_SHADER_STORAGE_BUFFER, 0, (maxIndex + 1) * sizeof(Vec4f), vbData.data());
}
drv.glBindBufferBase(eGL_UNIFORM_BUFFER, 0, DebugData.UBOs[0]);
MeshPickUBOData *cdata =
(MeshPickUBOData *)drv.glMapBufferRange(eGL_UNIFORM_BUFFER, 0, sizeof(MeshPickUBOData),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
cdata->rayPos = rayPos;
cdata->rayDir = rayDir;
cdata->use_indices = cfg.position.indexByteStride ? 1U : 0U;
cdata->numVerts = numIndices;
bool isTriangleMesh = true;
switch(cfg.position.topology)
{
case Topology::TriangleList:
{
cdata->meshMode = MESH_TRIANGLE_LIST;
break;
}
case Topology::TriangleStrip:
{
cdata->meshMode = MESH_TRIANGLE_STRIP;
break;
}
case Topology::TriangleFan:
{
if(fandecode)
cdata->meshMode = MESH_TRIANGLE_LIST;
else
cdata->meshMode = MESH_TRIANGLE_FAN;
break;
}
case Topology::TriangleList_Adj:
{
cdata->meshMode = MESH_TRIANGLE_LIST_ADJ;
break;
}
case Topology::TriangleStrip_Adj:
{
cdata->meshMode = MESH_TRIANGLE_STRIP_ADJ;
break;
}
default: // points, lines, patchlists, unknown
{
cdata->meshMode = MESH_OTHER;
isTriangleMesh = false;
}
}
// line/point data
cdata->unproject = cfg.position.unproject;
cdata->mvp = cfg.position.unproject ? pickMVPProj : pickMVP;
cdata->coords = Vec2f((float)x, (float)y);
cdata->viewport = Vec2f((float)width, (float)height);
drv.glUnmapBuffer(eGL_UNIFORM_BUFFER);
uint32_t reset[4] = {};
drv.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 0, DebugData.pickResultBuf);
drv.glBufferSubData(eGL_SHADER_STORAGE_BUFFER, 0, sizeof(uint32_t) * 4, &reset);
drv.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 1, DebugData.pickVBBuf);
drv.glBindBufferRange(eGL_SHADER_STORAGE_BUFFER, 2, DebugData.pickIBBuf, (GLintptr)0,
(GLsizeiptr)(sizeof(uint32_t) * cfg.position.numIndices));
drv.glBindBufferBase(eGL_SHADER_STORAGE_BUFFER, 3, DebugData.pickResultBuf);
drv.glDispatchCompute(GLuint((cfg.position.numIndices) / 128 + 1), 1, 1);
drv.glMemoryBarrier(GL_ATOMIC_COUNTER_BARRIER_BIT | GL_SHADER_STORAGE_BARRIER_BIT);
uint32_t numResults = 0;
drv.glBindBuffer(eGL_COPY_READ_BUFFER, DebugData.pickResultBuf);
drv.glGetBufferSubData(eGL_COPY_READ_BUFFER, 0, sizeof(uint32_t), &numResults);
uint32_t ret = ~0U;
if(numResults > 0)
{
if(isTriangleMesh)
{
struct PickResult
{
uint32_t vertid;
vec3 intersectionPoint;
};
byte *mapped = (byte *)drv.glMapNamedBufferEXT(DebugData.pickResultBuf, eGL_READ_ONLY);
mapped += sizeof(uint32_t) * 4;
PickResult *pickResults = (PickResult *)mapped;
PickResult *closest = pickResults;
// distance from raycast hit to nearest worldspace position of the mouse
float closestPickDistance = (closest->intersectionPoint - rayPos).Length();
// min with size of results buffer to protect against overflows
for(uint32_t i = 1; i < RDCMIN((uint32_t)DebugRenderData::maxMeshPicks, numResults); i++)
{
float pickDistance = (pickResults[i].intersectionPoint - rayPos).Length();
if(pickDistance < closestPickDistance)
{
closest = pickResults + i;
}
}
drv.glUnmapNamedBufferEXT(DebugData.pickResultBuf);
ret = closest->vertid;
}
else
{
struct PickResult
{
uint32_t vertid;
uint32_t idx;
float len;
float depth;
};
byte *mapped = (byte *)drv.glMapNamedBufferEXT(DebugData.pickResultBuf, eGL_READ_ONLY);
mapped += sizeof(uint32_t) * 4;
PickResult *pickResults = (PickResult *)mapped;
PickResult *closest = pickResults;
// min with size of results buffer to protect against overflows
for(uint32_t i = 1; i < RDCMIN((uint32_t)DebugRenderData::maxMeshPicks, numResults); i++)
{
// We need to keep the picking order consistent in the face
// of random buffer appends, when multiple vertices have the
// identical position (e.g. if UVs or normals are different).
//
// We could do something to try and disambiguate, but it's
// never going to be intuitive, it's just going to flicker
// confusingly.
if(pickResults[i].len < closest->len ||
(pickResults[i].len == closest->len && pickResults[i].depth < closest->depth) ||
(pickResults[i].len == closest->len && pickResults[i].depth == closest->depth &&
pickResults[i].vertid < closest->vertid))
closest = pickResults + i;
}
drv.glUnmapNamedBufferEXT(DebugData.pickResultBuf);
ret = closest->vertid;
}
}
if(fandecode)
{
// undo the triangle list expansion
if(ret > 2)
ret = (ret + 3) / 3 + 1;
}
return ret;
}
void GLReplay::RenderCheckerboard(FloatVector dark, FloatVector light)
{
MakeCurrentReplayContext(m_DebugCtx);
WrappedOpenGL &drv = *m_pDriver;
drv.glUseProgram(DebugData.checkerProg);
drv.glDisable(eGL_DEPTH_TEST);
if(HasExt[EXT_framebuffer_sRGB])
drv.glEnable(eGL_FRAMEBUFFER_SRGB);
drv.glBindBufferBase(eGL_UNIFORM_BUFFER, 0, DebugData.UBOs[0]);
CheckerboardUBOData *ubo =
(CheckerboardUBOData *)drv.glMapBufferRange(eGL_UNIFORM_BUFFER, 0, sizeof(CheckerboardUBOData),
GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);
ubo->BorderWidth = 0.0f;
ubo->RectPosition = Vec2f();
ubo->RectSize = Vec2f();
ubo->CheckerSquareDimension = 64.0f;
ubo->InnerColor = Vec4f();
ubo->PrimaryColor = ConvertSRGBToLinear(dark);
ubo->SecondaryColor = ConvertSRGBToLinear(light);
drv.glUnmapBuffer(eGL_UNIFORM_BUFFER);
drv.glBindVertexArray(DebugData.emptyVAO);
drv.glDrawArrays(eGL_TRIANGLE_STRIP, 0, 4);
}
void GLReplay::RenderHighlightBox(float w, float h, float scale)
{
MakeCurrentReplayContext(m_DebugCtx);
WrappedOpenGL &drv = *m_pDriver;
GLint sz = GLint(scale);
struct rect
{
GLint x, y;
GLint w, h;
};
rect tl = {GLint(w / 2.0f + 0.5f), GLint(h / 2.0f + 0.5f), 1, 1};
rect scissors[4] = {
{tl.x, tl.y - (GLint)sz - 1, 1, sz + 1},
{tl.x + (GLint)sz, tl.y - (GLint)sz - 1, 1, sz + 2},
{tl.x, tl.y, sz, 1},
{tl.x, tl.y - (GLint)sz - 1, sz, 1},
};
// inner
drv.glEnable(eGL_SCISSOR_TEST);
drv.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
for(size_t i = 0; i < ARRAY_COUNT(scissors); i++)
{
drv.glScissor(scissors[i].x, scissors[i].y, scissors[i].w, scissors[i].h);
drv.glClear(eGL_COLOR_BUFFER_BIT);
}
scissors[0].x--;
scissors[1].x++;
scissors[2].x--;
scissors[3].x--;
scissors[0].y--;
scissors[1].y--;
scissors[2].y++;
scissors[3].y--;
scissors[0].h += 2;
scissors[1].h += 2;
scissors[2].w += 2;
scissors[3].w += 2;
// outer
drv.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
for(size_t i = 0; i < ARRAY_COUNT(scissors); i++)
{
drv.glScissor(scissors[i].x, scissors[i].y, scissors[i].w, scissors[i].h);
drv.glClear(eGL_COLOR_BUFFER_BIT);
}
drv.glDisable(eGL_SCISSOR_TEST);
}
|