summaryrefslogtreecommitdiffstats
path: root/tools/render-test/render-test-main.cpp
blob: 197ee2b46d0ea0fee87bb8d4ae2e40172ebe7786 (plain)
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
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
// render-test-main.cpp

#define _CRT_SECURE_NO_WARNINGS 1

#include "options.h"
#include "render.h"

#include "slang-support.h"
#include "surface.h"
#include "png-serialize-util.h"

#include "shader-renderer-util.h"

#include "../source/core/slang-io.h"
#include "../source/core/slang-string-util.h"

#include "core/slang-token-reader.h"

#include "shader-input-layout.h"
#include <stdio.h>
#include <stdlib.h>

#include "window.h"

#include "../../source/core/slang-test-tool-util.h"

#include "cpu-compute-util.h"

#if RENDER_TEST_CUDA
#   include "cuda/cuda-compute-util.h"
#endif

namespace renderer_test {

using Slang::Result;

int gWindowWidth = 1024;
int gWindowHeight = 768;

//
// For the purposes of a small example, we will define the vertex data for a
// single triangle directly in the source file. It should be easy to extend
// this example to load data from an external source, if desired.
//

struct Vertex
{
    float position[3];
    float color[3];
    float uv[2];
};

static const Vertex kVertexData[] =
{
    { { 0,  0, 0.5 }, {1, 0, 0} , {0, 0} },
    { { 0,  1, 0.5 }, {0, 0, 1} , {1, 0} },
    { { 1,  0, 0.5 }, {0, 1, 0} , {1, 1} },
};
static const int kVertexCount = SLANG_COUNT_OF(kVertexData);

using namespace Slang;

static void _outputProfileTime(uint64_t startTicks, uint64_t endTicks)
{
    WriterHelper out = StdWriters::getOut();
    double time = double(endTicks - startTicks) / ProcessUtil::getClockFrequency();
    out.print("profile-time=%g\n", time);
}

class ProgramVars;

struct ShaderOutputPlan
{
    struct Item
    {
        Index               inputLayoutEntryIndex;
        RefPtr<Resource>    resource;
    };

    List<Item> items;
};

class RenderTestApp : public WindowListener
{
public:
    // WindowListener
    virtual Result update(Window* window) SLANG_OVERRIDE;

    // At initialization time, we are going to load and compile our Slang shader
    // code, and then create the API objects we need for rendering.
    virtual Result initialize(
        SlangSession* session,
        Renderer* renderer,
        const Options& options,
        const ShaderCompilerUtil::Input& input) = 0;
    void runCompute();
    void renderFrame();
    void finalize();

    virtual void applyBinding(PipelineType pipelineType) = 0;
    virtual void setProjectionMatrix() = 0;
    virtual Result writeBindingOutput(BindRoot* bindRoot, const char* fileName) = 0;

    Result writeScreen(const char* filename);

protected:
    /// Called in initialize
    Result _initializeShaders(
        SlangSession* session,
        Renderer* renderer,
        Options::ShaderProgramType shaderType,
        const ShaderCompilerUtil::Input& input);

    uint64_t m_startTicks;

    // variables for state to be used for rendering...
    uintptr_t m_constantBufferSize;

    RefPtr<Renderer> m_renderer;

    RefPtr<InputLayout> m_inputLayout;
    RefPtr<BufferResource> m_vertexBuffer;
    RefPtr<ShaderProgram> m_shaderProgram;
    RefPtr<PipelineState> m_pipelineState;

    ShaderCompilerUtil::OutputAndLayout m_compilationOutput;

    ShaderInputLayout m_shaderInputLayout; ///< The binding layout

    Options m_options;
};

class LegacyRenderTestApp : public RenderTestApp
{
public:
    virtual void applyBinding(PipelineType pipelineType) SLANG_OVERRIDE;
    virtual void setProjectionMatrix() SLANG_OVERRIDE;
    virtual Result initialize(
        SlangSession* session,
        Renderer* renderer,
        const Options& options,
        const ShaderCompilerUtil::Input& input) SLANG_OVERRIDE;

    BindingStateImpl* getBindingState() const { return m_bindingState; }

    virtual Result writeBindingOutput(BindRoot* bindRoot, const char* fileName) override;

protected:
	uintptr_t m_constantBufferSize;
	RefPtr<BufferResource>	m_constantBuffer;
	RefPtr<BindingStateImpl>    m_bindingState;
    int m_numAddedConstantBuffers;                      ///< Constant buffers can be added to the binding directly. Will be added at the end.
};

class ShaderObjectRenderTestApp : public RenderTestApp
{
public:
    virtual void applyBinding(PipelineType pipelineType) SLANG_OVERRIDE;
    virtual void setProjectionMatrix() SLANG_OVERRIDE;
    virtual Result initialize(
        SlangSession* session,
        Renderer* renderer,
        const Options& options,
        const ShaderCompilerUtil::Input& input) SLANG_OVERRIDE;
    virtual Result writeBindingOutput(BindRoot* bindRoot, const char* fileName) override;

protected:
    RefPtr<ProgramVars> m_programVars;
    ShaderOutputPlan m_outputPlan;
};

struct ShaderOffset
{
    SlangInt uniformOffset = 0;
    SlangInt bindingRangeIndex = 0;
    SlangInt bindingArrayIndex = 0;
};

class ShaderObjectLayout : public RefObject
{
public:
    struct BindingRangeInfo
    {
        slang::BindingType bindingType;
        Index   count;
        Index   baseIndex;
        Index   descriptorSetIndex;
        Index   rangeIndexInDescriptorSet;
//        Index   subObjectRangeIndex = -1;
    };

    struct SubObjectRangeInfo
    {
        RefPtr<ShaderObjectLayout>  layout;
//        Index                       baseIndex;
//        Index                       count;
        Index                       bindingRangeIndex;
    };

    struct DescriptorSetInfo : public RefObject
    {
        RefPtr<DescriptorSetLayout> layout;
        Slang::Int                  space = -1;
    };

    struct Builder
    {
    public:
        Builder(Renderer* renderer)
            : m_renderer(renderer)
        {}

        List<BindingRangeInfo> m_bindingRanges;
        List<SubObjectRangeInfo> m_subObjectRanges;

        Index m_resourceViewCount = 0;
        Index m_samplerCount = 0;
        Index m_combinedTextureSamplerCount = 0;
        Index m_subObjectCount = 0;
        Index m_varyingInputCount = 0;
        Index m_varyingOutputCount = 0;

        struct DescriptorSetBuildInfo : public RefObject
        {
            List<DescriptorSetLayout::SlotRangeDesc> slotRangeDescs;
            Index space;
        };
        List<RefPtr<DescriptorSetBuildInfo>> m_descriptorSetBuildInfos;
        Dictionary<Index, Index> m_mapSpaceToDescriptorSetIndex;

        Index findOrAddDescriptorSet(Index space)
        {
            Index index;
            if(m_mapSpaceToDescriptorSetIndex.TryGetValue(space, index))
                return index;

            RefPtr<DescriptorSetBuildInfo> info = new DescriptorSetBuildInfo();
            info->space = space;

            index = m_descriptorSetBuildInfos.getCount();
            m_descriptorSetBuildInfos.add(info);

            m_mapSpaceToDescriptorSetIndex.Add(space, index);
            return index;
        }

        static DescriptorSlotType _mapDescriptorType(slang::BindingType slangBindingType)
        {
            switch(slangBindingType)
            {
            default: return DescriptorSlotType::Unknown;

        #define CASE(FROM, TO) \
            case slang::BindingType::FROM: return DescriptorSlotType::TO

            CASE(Sampler,                           Sampler);
            CASE(CombinedTextureSampler,            CombinedImageSampler);
            CASE(Texture,                           SampledImage);
            CASE(MutableTexture,                    StorageImage);
            CASE(TypedBuffer,                       UniformTexelBuffer);
            CASE(MutableTypedBuffer,                StorageTexelBuffer);
            CASE(RawBuffer,                         UniformBuffer);
            CASE(MutableRawBuffer,                  StorageBuffer);
            CASE(InputRenderTarget,                 InputAttachment);
            CASE(InlineUniformData,                 InlineUniformBlock);
            CASE(RayTracingAccelerationStructure,   RayTracingAccelerationStructure);
            CASE(ConstantBuffer,                    UniformBuffer);
            CASE(PushConstant,                      RootConstant);

        #undef CASE
            }
        }

        slang::TypeLayoutReflection* unwrapParameterGroups(slang::TypeLayoutReflection* typeLayout)
        {
            for(;;)
            {
                if(!typeLayout->getType())
                {
                    if(auto elementTypeLayout = typeLayout->getElementTypeLayout())
                        typeLayout = elementTypeLayout;
                }

                switch(typeLayout->getKind())
                {
                default:
                    return typeLayout;

                case slang::TypeReflection::Kind::ConstantBuffer:
                case slang::TypeReflection::Kind::ParameterBlock:
                    typeLayout = typeLayout->getElementTypeLayout();
                    continue;
                }
            }
        }

        void _addDescriptorSets(slang::TypeLayoutReflection* typeLayout, slang::VariableLayoutReflection* varLayout = nullptr)
        {
            SlangInt descriptorSetCount = typeLayout->getDescriptorSetCount();
            for(SlangInt s = 0; s < descriptorSetCount; ++s)
            {
                auto descriptorSetIndex = findOrAddDescriptorSet(typeLayout->getDescriptorSetSpaceOffset(s));
                auto descriptorSetInfo = m_descriptorSetBuildInfos[descriptorSetIndex];

                SlangInt descriptorRangeCount = typeLayout->getDescriptorSetDescriptorRangeCount(s);
                for(SlangInt r = 0; r < descriptorRangeCount; ++r)
                {
                    auto slangBindingType = typeLayout->getDescriptorSetDescriptorRangeType(s, r);
                    auto gfxDescriptorType = _mapDescriptorType(slangBindingType);

                    DescriptorSetLayout::SlotRangeDesc descriptorRangeDesc;
                    descriptorRangeDesc.binding = typeLayout->getDescriptorSetDescriptorRangeIndexOffset(s, r);
                    descriptorRangeDesc.count = typeLayout->getDescriptorSetDescriptorRangeDescriptorCount(s, r);
                    descriptorRangeDesc.type = gfxDescriptorType;

                    if(varLayout)
                    {
                        auto category = typeLayout->getDescriptorSetDescriptorRangeCategory(s, r);
                        descriptorRangeDesc.binding += varLayout->getOffset(category);
                    }

                    descriptorSetInfo->slotRangeDescs.add(descriptorRangeDesc);
                }
            }
        }

        Result setElementTypeLayout(slang::TypeLayoutReflection* typeLayout)
        {
            typeLayout = unwrapParameterGroups(typeLayout);

            m_elementTypeLayout = typeLayout;

            // First we will use the Slang layout information to allocate
            // the descriptor set layout(s) required to store values
            // of the given type.
            //
            _addDescriptorSets(typeLayout);

            // Next we will compute the binding ranges that are used to store
            // the logical contents of the object in memory. These will relate
            // to the descriptor ranges in the various sets, but not always
            // in a one-to-one fashion.

            SlangInt bindingRangeCount = typeLayout->getBindingRangeCount();
            for(SlangInt r = 0; r < bindingRangeCount; ++r)
            {
                slang::BindingType slangBindingType = typeLayout->getBindingRangeType(r);
                SlangInt count = typeLayout->getBindingRangeBindingCount(r);
                slang::TypeLayoutReflection* slangLeafTypeLayout = typeLayout->getBindingRangeLeafTypeLayout(r);

                SlangInt descriptorSetIndex = typeLayout->getBindingRangeDescriptorSetIndex(r);
                SlangInt rangeIndexInDescriptorSet = typeLayout->getBindingRangeFirstDescriptorRangeIndex(r);

                Index baseIndex = 0;
                switch(slangBindingType)
                {
                case slang::BindingType::ConstantBuffer:
                case slang::BindingType::ParameterBlock:
                case slang::BindingType::ExistentialValue:
                    baseIndex = m_subObjectCount;
                    m_subObjectCount += count;
                    break;

                case slang::BindingType::Sampler:
                    baseIndex = m_samplerCount;
                    m_samplerCount += count;
                    break;

                case slang::BindingType::CombinedTextureSampler:
                    baseIndex = m_combinedTextureSamplerCount;
                    m_combinedTextureSamplerCount += count;
                    break;

                case slang::BindingType::VaryingInput:
                    baseIndex = m_varyingInputCount;
                    m_varyingInputCount += count;
                    break;

                case slang::BindingType::VaryingOutput:
                    baseIndex = m_varyingOutputCount;
                    m_varyingOutputCount += count;
                    break;

                default:
                    baseIndex = m_resourceViewCount;
                    m_resourceViewCount += count;
                    break;
                }

                BindingRangeInfo bindingRangeInfo;
                bindingRangeInfo.bindingType = slangBindingType;
                bindingRangeInfo.count = count;
//                bindingRangeInfo.descriptorSetIndex = descriptorSetIndex;
//                bindingRangeInfo.rangeIndexInDescriptorSet = slotRangeIndex;
//                bindingRangeInfo.subObjectRangeIndex = subObjectRangeIndex;
                bindingRangeInfo.baseIndex = baseIndex;
                bindingRangeInfo.descriptorSetIndex = descriptorSetIndex;
                bindingRangeInfo.rangeIndexInDescriptorSet = rangeIndexInDescriptorSet;

                m_bindingRanges.add(bindingRangeInfo);

#if 0
                SlangInt binding = typeLayout->getBindingRangeIndexOffset(r);
                SlangInt space = typeLayout->getBindingRangeSpaceOffset(r);
                SlangInt subObjectRangeIndex = typeLayout->getBindingRangeSubObjectRangeIndex(r);

                DescriptorSetLayout::SlotRangeDesc slotRange;
                slotRange.type = _mapDescriptorType(slangBindingType);
                slotRange.count = count;
                slotRange.binding = binding;

                Index descriptorSetIndex = findOrAddDescriptorSet(space);
                RefPtr<DescriptorSetBuildInfo> descriptorSetInfo = m_descriptorSetInfos[descriptorSetIndex];

                Index slotRangeIndex = descriptorSetInfo->slotRanges.getCount();
                descriptorSetInfo->slotRanges.add(slotRange);
#endif
            }

            SlangInt subObjectRangeCount = typeLayout->getSubObjectRangeCount();
            for(SlangInt r = 0; r < subObjectRangeCount; ++r)
            {
                SlangInt bindingRangeIndex = typeLayout->getSubObjectRangeBindingRangeIndex(r);
                auto slangBindingType = typeLayout->getBindingRangeType(bindingRangeIndex);
                slang::TypeLayoutReflection* slangLeafTypeLayout = typeLayout->getBindingRangeLeafTypeLayout(bindingRangeIndex);

                // A sub-object range can either represent a sub-object of a known
                // type, like a `ConstantBuffer<Foo>` or `ParameterBlock<Foo>`
                // (in which case we can pre-compute a layout to use, based on
                // the type `Foo`) *or* it can represent a sub-object of some
                // existential type (e.g., `IBar`) in which case we cannot
                // know the appropraite type/layout of sub-object to allocate.
                //
                RefPtr<ShaderObjectLayout> subObjectLayout;
                if(slangBindingType != slang::BindingType::ExistentialValue)
                {
                    ShaderObjectLayout::createForElementType(m_renderer, slangLeafTypeLayout->getElementTypeLayout(), subObjectLayout.writeRef());
                }

                SubObjectRangeInfo subObjectRange;
                subObjectRange.bindingRangeIndex = bindingRangeIndex;
                subObjectRange.layout = subObjectLayout;

                m_subObjectRanges.add(subObjectRange);
            }

#if 0
            SlangInt subObjectRangeCount = typeLayout->getSubObjectRangeCount();
            for(SlangInt r = 0; r < subObjectRangeCount; ++r)
            {

                // TODO: Still need a way to map the binding ranges for
                // the sub-object over so that they can be used to
                // set/get the sub-object as needed.
            }
#endif
            return SLANG_OK;
        }

        SlangResult build(ShaderObjectLayout** outLayout)
        {
            RefPtr<ShaderObjectLayout> layout = new ShaderObjectLayout();
            SLANG_RETURN_ON_FAIL(layout->_init(this));

            *outLayout = layout.detach();
            return SLANG_OK;
        }

        Renderer* m_renderer = nullptr;
        slang::TypeLayoutReflection* m_elementTypeLayout = nullptr;
    };

    static Result createForElementType(Renderer* renderer, slang::TypeLayoutReflection* elementType, ShaderObjectLayout** outLayout)
    {
        Builder builder(renderer);
        builder.setElementTypeLayout(elementType);
        return builder.build(outLayout);
    }

    List<RefPtr<DescriptorSetInfo>> const& getDescriptorSets() { return m_descriptorSets; }

    List<BindingRangeInfo> const& getBindingRanges() { return m_bindingRanges; }

    Index getBindingRangeCount() { return m_bindingRanges.getCount(); }

    BindingRangeInfo const& getBindingRange(Index index) { return m_bindingRanges[index]; }

    slang::TypeLayoutReflection* getElementTypeLayout()
    {
        return m_elementTypeLayout;
    }

    Index getResourceViewCount() { return m_resourceViewCount; }
    Index getSamplerCount() { return m_samplerCount; }
    Index getCombinedTextureSamplerCount() { return m_combinedTextureSamplerCount; }
    Index getSubObjectCount() { return m_subObjectCount; }

    SubObjectRangeInfo const& getSubObjectRange(Index index) { return m_subObjectRanges[index]; }
    List<SubObjectRangeInfo> const& getSubObjectRanges() { return m_subObjectRanges; }

    Renderer* getRenderer()
    {
        return m_renderer;
    }

protected:
    Result _init(Builder const* builder)
    {
        auto renderer = builder->m_renderer;
        m_renderer = renderer;

        m_elementTypeLayout = builder->m_elementTypeLayout;
        m_bindingRanges = builder->m_bindingRanges;

        for(auto descriptorSetBuildInfo : builder->m_descriptorSetBuildInfos)
        {
            auto& slotRangeDescs = descriptorSetBuildInfo->slotRangeDescs;
            DescriptorSetLayout::Desc desc;
            desc.slotRangeCount = slotRangeDescs.getCount();
            desc.slotRanges = slotRangeDescs.getBuffer();

            RefPtr<DescriptorSetLayout> descriptorSetLayout;
            SLANG_RETURN_ON_FAIL(m_renderer->createDescriptorSetLayout(desc, descriptorSetLayout.writeRef()));

            RefPtr<DescriptorSetInfo> descriptorSetInfo = new DescriptorSetInfo();
            descriptorSetInfo->layout = descriptorSetLayout;
            descriptorSetInfo->space = descriptorSetBuildInfo->space;

            m_descriptorSets.add(descriptorSetInfo);
        }

        m_resourceViewCount = builder->m_resourceViewCount;
        m_samplerCount = builder->m_samplerCount;
        m_combinedTextureSamplerCount = builder->m_combinedTextureSamplerCount;
        m_subObjectCount = builder->m_subObjectCount;

        m_subObjectRanges = builder->m_subObjectRanges;

        return SLANG_OK;
    }

    Renderer*                       m_renderer;
    List<RefPtr<DescriptorSetInfo>> m_descriptorSets;
    List<BindingRangeInfo>          m_bindingRanges;
    slang::TypeLayoutReflection*    m_elementTypeLayout;
    Index                           m_resourceViewCount = 0;
    Index                           m_samplerCount = 0;
    Index                           m_combinedTextureSamplerCount = 0;
    Index                           m_subObjectCount = 0;
    List<SubObjectRangeInfo>        m_subObjectRanges;
};

class EntryPointLayout : public ShaderObjectLayout
{
    typedef ShaderObjectLayout Super;
public:

    struct VaryingInputInfo
    {
    };

    struct VaryingOutputInfo
    {
    };

    struct Builder : Super::Builder
    {
        Builder(Renderer* renderer)
            : Super::Builder(renderer)
        {}

        Result build(EntryPointLayout** outLayout)
        {
            RefPtr<EntryPointLayout> layout = new EntryPointLayout();
            SLANG_RETURN_ON_FAIL(layout->_init(this));

            *outLayout = layout.detach();
            return SLANG_OK;
        }

        void _addEntryPointParam(slang::VariableLayoutReflection* entryPointParam)
        {
            auto slangStage = entryPointParam->getStage();
            auto typeLayout = entryPointParam->getTypeLayout();

            SlangInt bindingRangeCount = typeLayout->getBindingRangeCount();
            for(SlangInt r = 0; r < bindingRangeCount; ++r)
            {
                slang::BindingType slangBindingType = typeLayout->getBindingRangeType(r);
                SlangInt count = typeLayout->getBindingRangeBindingCount(r);

                switch(slangBindingType)
                {
                default:
                    break;

                case slang::BindingType::VaryingInput:
                    {
                        VaryingInputInfo info;

                        m_varyingInputs.add(info);
                    }
                    break;

                case slang::BindingType::VaryingOutput:
                    {
                        VaryingOutputInfo info;
                        m_varyingOutputs.add(info);
                    }
                    break;
                }
            }
        }

        void addEntryPointParams(slang::EntryPointLayout* entryPointLayout)
        {
            m_slangEntryPointLayout = entryPointLayout;

            setElementTypeLayout(entryPointLayout->getTypeLayout());

            m_stage = translateStage(entryPointLayout->getStage());
            _addEntryPointParam(entryPointLayout->getVarLayout());
            _addEntryPointParam(entryPointLayout->getResultVarLayout());
        }

        slang::EntryPointLayout* m_slangEntryPointLayout = nullptr;

        gfx::StageType m_stage;
        List<VaryingInputInfo> m_varyingInputs;
        List<VaryingOutputInfo> m_varyingOutputs;
    };

    Result _init(Builder const* builder)
    {
        auto renderer = builder->m_renderer;

        SLANG_RETURN_ON_FAIL(Super::_init(builder));

        m_slangEntryPointLayout = builder->m_slangEntryPointLayout;
        m_stage = builder->m_stage;
        m_varyingInputs = builder->m_varyingInputs;
        m_varyingOutputs = builder->m_varyingOutputs;

        return SLANG_OK;
    }

    List<VaryingInputInfo> const& getVaryingInputs() { return m_varyingInputs; }
    List<VaryingOutputInfo> const& getVaryingOutputs() { return m_varyingOutputs; }

    gfx::StageType getStage() const { return m_stage; }

    slang::EntryPointLayout* getSlangLayout() const { return m_slangEntryPointLayout; };

    slang::EntryPointLayout* m_slangEntryPointLayout;
    gfx::StageType m_stage;
    List<VaryingInputInfo> m_varyingInputs;
    List<VaryingOutputInfo> m_varyingOutputs;
};

class ProgramLayout : public ShaderObjectLayout
{
    typedef ShaderObjectLayout Super;
public:

    struct EntryPointInfo
    {
        RefPtr<EntryPointLayout>    layout;
        Index                       rangeOffset;
    };


    struct Builder : Super::Builder
    {
        Builder(Renderer* renderer)
            : Super::Builder(renderer)
        {}

        Result build(ProgramLayout** outLayout)
        {
            RefPtr<ProgramLayout> layout = new ProgramLayout();
            SLANG_RETURN_ON_FAIL(layout->_init(this));

            *outLayout = layout.detach();
            return SLANG_OK;
       }

        void addGlobalParams(slang::VariableLayoutReflection* globalsLayout)
        {
            setElementTypeLayout(globalsLayout->getTypeLayout());
        }

        void addEntryPoint(EntryPointLayout* entryPointLayout)
        {
            EntryPointInfo info;
            info.layout = entryPointLayout;

            if(m_descriptorSetBuildInfos.getCount())
            {
                info.rangeOffset = m_descriptorSetBuildInfos[0]->slotRangeDescs.getCount();
            }

            auto slangEntryPointLayout = entryPointLayout->getSlangLayout();
            _addDescriptorSets(slangEntryPointLayout->getTypeLayout(), slangEntryPointLayout->getVarLayout());

            m_entryPoints.add(info);
        }

        List<EntryPointInfo> m_entryPoints;
    };

    Slang::Int getRenderTargetCount()
    {
        return m_renderTargetCount;
    }

    PipelineLayout* getPipelineLayout() { return m_pipelineLayout; }

    Index findEntryPointIndex(gfx::StageType stage)
    {
        auto entryPointCount = m_entryPoints.getCount();
        for(Index i = 0; i < entryPointCount; ++i)
        {
            auto entryPoint = m_entryPoints[i];
            if(entryPoint.layout->getStage() == stage)
                return i;
        }
        return -1;
    }

    EntryPointInfo const& getEntryPoint(Index index)
    {
        return m_entryPoints[index];
    }

    List<EntryPointInfo> const& getEntryPoints() const { return m_entryPoints; }

protected:
    Result _init(Builder const* builder)
    {
        auto renderer = builder->m_renderer;

        SLANG_RETURN_ON_FAIL(Super::_init(builder));

        m_entryPoints = builder->m_entryPoints;

        List<PipelineLayout::DescriptorSetDesc> pipelineDescriptorSets;
        _addDescriptorSetsRec(this, pipelineDescriptorSets);

#if 0
        _createInputLayout(builder);
#endif

        auto fragmentEntryPointIndex = findEntryPointIndex(gfx::StageType::Fragment);
        if(fragmentEntryPointIndex != -1)
        {
            auto fragmentEntryPoint = getEntryPoint(fragmentEntryPointIndex);
            m_renderTargetCount = fragmentEntryPoint.layout->getVaryingOutputs().getCount();
        }

        PipelineLayout::Desc pipelineLayoutDesc;
        pipelineLayoutDesc.renderTargetCount = m_renderTargetCount;
        pipelineLayoutDesc.descriptorSetCount = pipelineDescriptorSets.getCount();
        pipelineLayoutDesc.descriptorSets = pipelineDescriptorSets.getBuffer();

        SLANG_RETURN_ON_FAIL(renderer->createPipelineLayout(pipelineLayoutDesc, m_pipelineLayout.writeRef()));

        return SLANG_OK;
    }

    static void _addDescriptorSetsRec(ShaderObjectLayout* layout, List<PipelineLayout::DescriptorSetDesc>& ioPipelineDescriptorSets)
    {
        for(auto descriptorSetInfo : layout->getDescriptorSets())
        {
            PipelineLayout::DescriptorSetDesc pipelineDescriptorSet;
            pipelineDescriptorSet.layout = descriptorSetInfo->layout;
            pipelineDescriptorSet.space = descriptorSetInfo->space;

            ioPipelineDescriptorSets.add(pipelineDescriptorSet);
        }

        // TODO: next we need to recurse into the "sub-objects" of `layout` and
        // add their descriptor sets as well.
    }

#if 0
    Result _createInputLayout(Builder const* builder)
    {
        auto renderer = builder->m_renderer;

        List<InputElementDesc> const& inputElements = builder->getInputElements();
        SLANG_RETURN_ON_FAIL(renderer->createInputLayout(inputElements.getBuffer(), inputElements.getCount(), m_inputLayout.writeRef()));

        return SLANG_OK;
    }
#endif

    List<EntryPointInfo> m_entryPoints;
    gfx::UInt m_renderTargetCount = 0;

    RefPtr<PipelineLayout> m_pipelineLayout;
};

class ShaderObject : public RefObject
{
public:
    static Result create(Renderer* renderer, ShaderObjectLayout* layout, ShaderObject** outShaderObject)
    {
        RefPtr<ShaderObject> object = new ShaderObject();
        SLANG_RETURN_ON_FAIL(object->init(renderer, layout));

        *outShaderObject = object.detach();
        return SLANG_OK;
    }

    Renderer* getRenderer()
    {
        return m_layout->getRenderer();
    }

    ShaderObjectLayout* getLayout()
    {
        return m_layout;
    }

    slang::TypeLayoutReflection* getElementTypeLayout()
    {
        return m_layout->getElementTypeLayout();
    }

    SlangResult setData(ShaderOffset const& offset, void const* data, size_t size)
    {
        Renderer* renderer = getRenderer();

        char* dest = (char*) renderer->map(m_buffer, MapFlavor::HostWrite);
        memcpy(dest + offset.uniformOffset, data, size);
        renderer->unmap(m_buffer);

        return SLANG_OK;
    }

    SlangResult setObject(ShaderOffset const& offset, ShaderObject* object)
    {
        if(offset.bindingRangeIndex < 0) return SLANG_E_INVALID_ARG;
        if(offset.bindingRangeIndex >= m_layout->getBindingRangeCount()) return SLANG_E_INVALID_ARG;
        auto& bindingRange = m_layout->getBindingRange(offset.bindingRangeIndex);

        // TODO: Is this reasonable to store the base index directly in the binding range?
        m_objects[bindingRange.baseIndex + offset.bindingArrayIndex] = object;

//        auto& subObjectRange = m_layout->getSubObjectRange(bindingRange.subObjectRangeIndex);
//        m_objects[subObjectRange.baseIndex + offset.bindingArrayIndex] = object;

#if 0

        SLANG_ASSERT(bindingRange.descriptorSetIndex >= 0);
        SLANG_ASSERT(bindingRange.descriptorSetIndex < m_descriptorSets.getCount());
        auto& descriptorSet = m_descriptorSets[bindingRange.descriptorSetIndex];

        descriptorSet->setConstantBuffer(bindingRange.rangeIndexInDescriptorSet, offset.bindingArrayIndex, buffer);
        return SLANG_OK;
#else
        return SLANG_E_NOT_IMPLEMENTED;
#endif
    }

    SlangResult getObject(ShaderOffset const& offset, ShaderObject** outObject)
    {
        SLANG_ASSERT(outObject);
        if(offset.bindingRangeIndex < 0) return SLANG_E_INVALID_ARG;
        if(offset.bindingRangeIndex >= m_layout->getBindingRangeCount()) return SLANG_E_INVALID_ARG;
        auto& bindingRange = m_layout->getBindingRange(offset.bindingRangeIndex);

        *outObject = m_objects[bindingRange.baseIndex + offset.bindingArrayIndex];

//        auto& subObjectRange = m_layout->getSubObjectRange(bindingRange.subObjectRangeIndex);
//        *outObject = m_objects[subObjectRange.baseIndex + offset.bindingArrayIndex];

        return SLANG_OK;

#if 0
        SLANG_ASSERT(bindingRange.descriptorSetIndex >= 0);
        SLANG_ASSERT(bindingRange.descriptorSetIndex < m_descriptorSets.getCount());
        auto& descriptorSet = m_descriptorSets[bindingRange.descriptorSetIndex];

        descriptorSet->setConstantBuffer(bindingRange.rangeIndexInDescriptorSet, offset.bindingArrayIndex, buffer);
        return SLANG_OK;
#endif
    }

    ShaderObject* getObject(ShaderOffset const& offset)
    {
        ShaderObject* object = nullptr;
        SLANG_RETURN_NULL_ON_FAIL(getObject(offset, &object));
        return object;
    }

    SlangResult setResource(ShaderOffset const& offset, ResourceView* resourceView)
    {
        if(offset.bindingRangeIndex < 0) return SLANG_E_INVALID_ARG;
        if(offset.bindingRangeIndex >= m_layout->getBindingRangeCount()) return SLANG_E_INVALID_ARG;
        auto& bindingRange = m_layout->getBindingRange(offset.bindingRangeIndex);

        m_resourceViews[bindingRange.baseIndex + offset.bindingArrayIndex] = resourceView;
        return SLANG_OK;
    }

    SlangResult setSampler(ShaderOffset const& offset, SamplerState* sampler)
    {
        if(offset.bindingRangeIndex < 0) return SLANG_E_INVALID_ARG;
        if(offset.bindingRangeIndex >= m_layout->getBindingRangeCount()) return SLANG_E_INVALID_ARG;
        auto& bindingRange = m_layout->getBindingRange(offset.bindingRangeIndex);

        m_samplers[bindingRange.baseIndex + offset.bindingArrayIndex] = sampler;
        return SLANG_OK;
    }

    SlangResult setCombinedTextureSampler(ShaderOffset const& offset, ResourceView* textureView, SamplerState* sampler)
    {
        if(offset.bindingRangeIndex < 0) return SLANG_E_INVALID_ARG;
        if(offset.bindingRangeIndex >= m_layout->getBindingRangeCount()) return SLANG_E_INVALID_ARG;
        auto& bindingRange = m_layout->getBindingRange(offset.bindingRangeIndex);

        auto& slot = m_combinedTextureSamplers[bindingRange.baseIndex + offset.bindingArrayIndex];
        slot.textureView = textureView;
        slot.sampler = sampler;
        return SLANG_OK;
    }

protected:
    friend class ProgramVars;

    Result init(Renderer* renderer, ShaderObjectLayout* layout)
    {
        m_layout = layout;

        // If the layout tells us that there is any uniform data,
        // then we need to allocate a constant buffer to hold that data.
        //
        // TODO: Do we need to allocate a shadow copy for use from
        // the CPU?
        //
        // TODO: When/where do we bind this constant buffer into
        // a descriptor set for later use?
        //
        size_t uniformSize = layout->getElementTypeLayout()->getSize();
        if(uniformSize)
        {
            BufferResource::Desc bufferDesc;
            bufferDesc.init(uniformSize);
            bufferDesc.cpuAccessFlags |= Resource::AccessFlag::Write;
            SLANG_RETURN_ON_FAIL(renderer->createBufferResource(Resource::Usage::ConstantBuffer, bufferDesc, nullptr, m_buffer.writeRef()));
        }

#if 0
        // If the layout tells us there are any descriptor sets to
        // allocate, then we do so now.
        //
        for(auto descriptorSetInfo : layout->getDescriptorSets())
        {
            RefPtr<DescriptorSet> descriptorSet;
            SLANG_RETURN_ON_FAIL(renderer->createDescriptorSet(descriptorSetInfo->layout, descriptorSet.writeRef()));
            m_descriptorSets.add(descriptorSet);
        }
#endif

        m_resourceViews.setCount(layout->getResourceViewCount());
        m_samplers.setCount(layout->getSamplerCount());
        m_combinedTextureSamplers.setCount(layout->getCombinedTextureSamplerCount());

        // If the layout specifies that we have any sub-objects, then
        // we need to size the array to account for them.
        //
        Index subObjectCount = layout->getSubObjectCount();
        m_objects.setCount(subObjectCount);

        for(auto subObjectRangeInfo : layout->getSubObjectRanges())
        {
            RefPtr<ShaderObjectLayout> subObjectLayout = subObjectRangeInfo.layout;

            // In the case where the sub-object range represents an
            // existential-type leaf field (e.g., an `IBar`), we
            // cannot pre-allocate the objet(s) to go into that
            // range, since we can't possibly know what to allocate
            // at this point.
            //
            if(!subObjectLayout)
                continue;
            //
            // Otherwise, we will allocate a sub-object to fill
            // in each entry in this range, based on the layout
            // information we already have.

            auto& bindingRangeInfo = layout->getBindingRange(subObjectRangeInfo.bindingRangeIndex);
            for(Index i = 0; i < bindingRangeInfo.count; ++i)
            {
                RefPtr<ShaderObject> subObject;
                SLANG_RETURN_ON_FAIL(ShaderObject::create(renderer, subObjectLayout, subObject.writeRef()));
                m_objects[bindingRangeInfo.baseIndex + i] = subObject;
            }
        }

        return SLANG_OK;
    }

    Result apply(Renderer* renderer, PipelineType pipelineType, PipelineLayout* pipelineLayout, Index& ioRootIndex)
    {
        ShaderObjectLayout* layout = m_layout;

        // Create the descritpor sets required by the layout...
        //
        List<RefPtr<DescriptorSet>> descriptorSets;
        for(auto descriptorSetInfo : layout->getDescriptorSets())
        {
            RefPtr<DescriptorSet> descriptorSet;
            SLANG_RETURN_ON_FAIL(renderer->createDescriptorSet(descriptorSetInfo->layout, descriptorSet.writeRef()));
            descriptorSets.add(descriptorSet);
        }

        SLANG_RETURN_ON_FAIL(_bindIntoDescriptorSets(descriptorSets.getBuffer()));

        for(auto descriptorSet : descriptorSets)
        {
            renderer->setDescriptorSet(pipelineType, pipelineLayout, ioRootIndex++, descriptorSet);
        }

        return SLANG_OK;
    }

    Result _bindIntoDescriptorSet(DescriptorSet* descriptorSet, Index baseRangeIndex, Index subObjectRangeArrayIndex)
    {
        ShaderObjectLayout* layout = m_layout;

        if(m_buffer)
        {
            descriptorSet->setConstantBuffer(baseRangeIndex, subObjectRangeArrayIndex, m_buffer);
            baseRangeIndex++;
        }

        for(auto bindingRangeInfo : layout->getBindingRanges())
        {
            switch(bindingRangeInfo.bindingType)
            {
            case slang::BindingType::VaryingInput:
            case slang::BindingType::VaryingOutput:
                continue;

            default:
                break;
            }

            SLANG_ASSERT(bindingRangeInfo.descriptorSetIndex == 0);

            auto count = bindingRangeInfo.count;
            auto baseIndex = bindingRangeInfo.baseIndex;

            auto descriptorRangeIndex = baseRangeIndex + bindingRangeInfo.rangeIndexInDescriptorSet;
            auto descriptorArrayBaseIndex = subObjectRangeArrayIndex * count;

            switch(bindingRangeInfo.bindingType)
            {
            case slang::BindingType::ConstantBuffer:
            case slang::BindingType::ParameterBlock:
                break;

            case slang::BindingType::ExistentialValue:
                //
                // TODO: If the existential value is one that "fits" into the storage available,
                // then we should write its data directly into that area. Otherwise, we need
                // to bind its content as "pending" data which will come after any other data
                // beloning to the same set (that is, it's starting descriptorRangeIndex will
                // need to be one after the number of ranges accounted for in the original type)
                //
                break;

            case slang::BindingType::CombinedTextureSampler:
                for(Index i = 0; i < count; ++i)
                {
                    auto& slot = m_combinedTextureSamplers[baseIndex + i];
                    descriptorSet->setCombinedTextureSampler(descriptorRangeIndex, descriptorArrayBaseIndex + i, slot.textureView, slot.sampler);
                }
                break;

            case slang::BindingType::Sampler:
                for(Index i = 0; i < count; ++i)
                {
                    descriptorSet->setSampler(descriptorRangeIndex, descriptorArrayBaseIndex + i, m_samplers[baseIndex + i]);
                }
                break;

            default:
                for(Index i = 0; i < count; ++i)
                {
                    descriptorSet->setResource(descriptorRangeIndex, descriptorArrayBaseIndex + i, m_resourceViews[baseIndex + i]);
                }
                break;
            }
        }

        return SLANG_OK;
    }

    public:
    virtual Result _bindIntoDescriptorSets(RefPtr<DescriptorSet>* descriptorSets)
    {
        ShaderObjectLayout* layout = m_layout;

        if(m_buffer)
        {
            // TODO: look up binding infor for default constant buffer...
            descriptorSets[0]->setConstantBuffer(0, 0, m_buffer);
        }

        // Fill in the descriptor sets based on binding ranges
        //
        for(auto bindingRangeInfo : layout->getBindingRanges())
        {
            DescriptorSet* descriptorSet = descriptorSets[bindingRangeInfo.descriptorSetIndex];
            auto rangeIndex = bindingRangeInfo.rangeIndexInDescriptorSet;
            auto baseIndex = bindingRangeInfo.baseIndex;
            auto count = bindingRangeInfo.count;
            switch(bindingRangeInfo.bindingType)
            {
            case slang::BindingType::ConstantBuffer:
            case slang::BindingType::ParameterBlock:
                for(Index i = 0; i < count; ++i)
                {
                    ShaderObject* subObject = m_objects[baseIndex + i];

                    subObject->_bindIntoDescriptorSet(descriptorSet, rangeIndex, i);
                }
                break;

            case slang::BindingType::CombinedTextureSampler:
                for(Index i = 0; i < count; ++i)
                {
                    auto& slot = m_combinedTextureSamplers[baseIndex + i];
                    descriptorSet->setCombinedTextureSampler(rangeIndex, i, slot.textureView, slot.sampler);
                }
                break;

            case slang::BindingType::Sampler:
                for(Index i = 0; i < count; ++i)
                {
                    descriptorSet->setSampler(rangeIndex, i, m_samplers[baseIndex + i]);
                }
                break;

            case slang::BindingType::VaryingInput:
            case slang::BindingType::VaryingOutput:
                break;

            case slang::BindingType::ExistentialValue:
                // Here we are binding as if existential value is the same
                // as a constant buffer or parameter block, which will lead
                // to incorrect results...
                for(Index i = 0; i < count; ++i)
                {
                    ShaderObject* subObject = m_objects[baseIndex + i];

                    subObject->_bindIntoDescriptorSet(descriptorSet, rangeIndex, i);
                }
                break;

            default:
                for(Index i = 0; i < count; ++i)
                {
                    descriptorSet->setResource(rangeIndex, i, m_resourceViews[baseIndex + i]);
                }
                break;
            }
        }
        return SLANG_OK;
    }


    RefPtr<ShaderObjectLayout> m_layout = nullptr;
    RefPtr<BufferResource> m_buffer;

    List<RefPtr<ResourceView>> m_resourceViews;

    List<RefPtr<SamplerState>> m_samplers;

    struct CombinedTextureSamplerSlot
    {
        RefPtr<ResourceView> textureView;
        RefPtr<SamplerState> sampler;
    };
    List<CombinedTextureSamplerSlot> m_combinedTextureSamplers;

//    List<RefPtr<DescriptorSet>> m_descriptorSets;
    List<RefPtr<ShaderObject>> m_objects;
};

class EntryPointVars : public ShaderObject
{
    typedef ShaderObject Super;

public:
    static Result create(Renderer* renderer, EntryPointLayout* layout, EntryPointVars** outShaderObject)
    {
        RefPtr<EntryPointVars> object = new EntryPointVars();
        SLANG_RETURN_ON_FAIL(object->init(renderer, layout));

        *outShaderObject = object.detach();
        return SLANG_OK;
    }

    EntryPointLayout* getLayout()
    {
        return static_cast<EntryPointLayout*>(m_layout.Ptr());
    }

protected:
    Result init(Renderer* renderer, EntryPointLayout* layout)
    {
        SLANG_RETURN_ON_FAIL(Super::init(renderer, layout));
        return SLANG_OK;
    }
};

class ProgramVars : public ShaderObject
{
    typedef ShaderObject Super;

public:
    static Result create(Renderer* renderer, ProgramLayout* layout, ProgramVars** outShaderObject)
    {
        RefPtr<ProgramVars> object = new ProgramVars();
        SLANG_RETURN_ON_FAIL(object->init(renderer, layout));

        *outShaderObject = object.detach();
        return SLANG_OK;
    }

    ProgramLayout* getLayout()
    {
        return static_cast<ProgramLayout*>(m_layout.Ptr());
    }

    void apply(Renderer* renderer, PipelineType pipelineType)
    {
        auto pipelineLayout = getLayout()->getPipelineLayout();

        Index rootIndex = 0;
        ShaderObject::apply(renderer, pipelineType, pipelineLayout, rootIndex);

#if 0

        Index descriptorSetCount = m_descriptorSets.getCount();
        for(Index descriptorSetIndex = 0; descriptorSetIndex < descriptorSetCount; ++descriptorSetIndex)
        {
            renderer->setDescriptorSet(
                pipelineType,
                pipelineLayout,
                descriptorSetIndex,
                m_descriptorSets[descriptorSetIndex]);
        }
#endif

        // TODO: We also need to bind any descriptor sets that are
        // part of sub-objects of this object.
    }

    List<RefPtr<EntryPointVars>> const& getEntryPoints() const { return m_entryPoints; }

protected:

    virtual Result _bindIntoDescriptorSets(RefPtr<DescriptorSet>* descriptorSets)
    {
        SLANG_RETURN_ON_FAIL(Super::_bindIntoDescriptorSets(descriptorSets));

        auto entryPointCount = m_entryPoints.getCount();
        for( Index i = 0; i < entryPointCount; ++i )
        {
            auto entryPoint = m_entryPoints[i];
            auto& entryPointInfo = getLayout()->getEntryPoint(i);

            SLANG_RETURN_ON_FAIL(entryPoint->_bindIntoDescriptorSet(descriptorSets[0], entryPointInfo.rangeOffset, 0));
        }

        return SLANG_OK;
    }


    Result init(Renderer* renderer, ProgramLayout* layout)
    {
        SLANG_RETURN_ON_FAIL(Super::init(renderer, layout));

        for(auto entryPointInfo : layout->getEntryPoints())
        {
            RefPtr<EntryPointVars> entryPoint;
            SLANG_RETURN_ON_FAIL(EntryPointVars::create(renderer, entryPointInfo.layout, entryPoint.writeRef()));
            m_entryPoints.add(entryPoint);
        }

        return SLANG_OK;
    }

    List<RefPtr<EntryPointVars>> m_entryPoints;
};

    /// Represents a "pointer" to the storage for a shader parameter of a (dynamically) known type.
    ///
    /// A `ShaderCursor` serves as a pointer-like type for things stored inside a `ShaderObject`.
    ///
    /// A cursor that points to the entire content of a shader object can be formed as `ShaderCursor(someObject)`.
    /// A cursor pointing to a structure field or array element can be formed from another cursor
    /// using `getField` or `getElement` respectively.
    ///
    /// Given a cursor pointing to a value of some "primitive" type, we can set or get the value
    /// using operations like `setResource`, `getResource`, etc.
    ///
    /// Because type information for shader parameters is being reflected dynamically, all type
    /// checking for shader cursors occurs at runtime, and errors may occur when attempting to
    /// set a parameter using a value of an inappropriate type. As much as possible, `ShaderCursor`
    /// attempts to protect against these cases and return an error `Result` or an invalid
    /// cursor, rather than allowing operations to proceed with incorrect types.
    ///
struct ShaderCursor
{
    ShaderObject*                   m_baseObject = nullptr;
    slang::TypeLayoutReflection*    m_typeLayout = nullptr;
    ShaderOffset                    m_offset;

        /// Get the type (layout) of the value being pointed at by the cursor
    slang::TypeLayoutReflection* getTypeLayout() const
    {
        return m_typeLayout;
    }

        /// Is this cursor valid (that is, does it seem to point to an actual location)?
        ///
        /// This check is equivalent to checking whether a pointer is null, so it is
        /// a very weak sense of "valid." In particular, it is possible to form a
        /// `ShaderCursor` for which `isValid()` is true, but attempting to get or
        /// set the value would be an error (like dereferencing a garbage pointer).
        ///
    bool isValid() const
    {
        return m_baseObject != nullptr;
    }

    Result getDereferenced(ShaderCursor& outCursor) const
    {
        switch(m_typeLayout->getKind())
        {
        default:
            return SLANG_E_INVALID_ARG;

        case slang::TypeReflection::Kind::ConstantBuffer:
        case slang::TypeReflection::Kind::ParameterBlock:
            {
                ShaderObject* subObject = m_baseObject->getObject(m_offset);
                outCursor = ShaderCursor(subObject);
                return SLANG_OK;
            }
        
        }
    }

    ShaderCursor getDereferenced()
    {
        ShaderCursor result;
        getDereferenced(result);
        return result;
    }

        /// Form a cursor pointing to the field with the given `name` within the value this cursor points at.
        ///
        /// If the operation succeeds, then the field cursor is written to `outCursor`.
    Result getField(UnownedStringSlice const& name, ShaderCursor& outCursor)
    {
        // If this cursor is invalid, then can't possible fetch a field.
        //
        if(!isValid()) return SLANG_E_INVALID_ARG;

        // If the cursor is valid, we want to consider the type of data
        // it is referencing.
        //
        switch(m_typeLayout->getKind())
        {
            // The easy/expected case is when the value has a structure type.
            //
        case slang::TypeReflection::Kind::Struct:
            {
                // We start by looking up the index of a field matching `name`.
                //
                // If there is no such field, we have an error.
                //
                SlangInt fieldIndex = m_typeLayout->findFieldIndexByName(name.begin(), name.end());
                if(fieldIndex == -1)
                    return SLANG_E_INVALID_ARG;

                // Once we know the index of the field being referenced,
                // we create a cursor to point at the field, based on
                // the offset information already in this cursor, plus
                // offsets derived from the field's layout.
                //
                slang::VariableLayoutReflection* fieldLayout = m_typeLayout->getFieldByIndex((unsigned int) fieldIndex);
                ShaderCursor fieldCursor;

                // The field cursorwill point into the same parent object.
                //
                fieldCursor.m_baseObject = m_baseObject;

                // The type being pointed to is the tyep of the field.
                //
                fieldCursor.m_typeLayout = fieldLayout->getTypeLayout();

                // The byte offset is the current offset plus the relative offset of the field.
                // The offset in binding ranges is computed similarly.
                //
                fieldCursor.m_offset.uniformOffset = m_offset.uniformOffset + fieldLayout->getOffset();
                fieldCursor.m_offset.bindingRangeIndex = m_offset.bindingRangeIndex +  m_typeLayout->getFieldBindingRangeOffset(fieldIndex);

                // The index of the field within any binding ranges will be the same
                // as the index computed for the parent structure.
                //
                // Note: this case would arise for an array of structures with texture-type
                // fields. Suppose we have:
                //
                //      struct S { Texture2D t; Texture2D u; }
                //      S g[4];
                //
                // In this scenario, `g` holds two binding ranges:
                //
                // * Range #0 comprises 4 textures, representing `g[...].t`
                // * Range #1 comprises 4 textures, representing `g[...].u`
                //
                // A cursor for `g[2]` would have a `bindingRangeIndex` of zero but
                // a `bindingArrayIndex` of 2, iindicating that we could end up
                // referencing either range, but no matter what we know the index
                // is 2. Thus when we form a cursor for `g[2].u` we want to
                // apply the binding range offset to get a `bindingRangeIndex` of
                // 1, while the `bindingArrayIndex` is unmodified.
                //
                // The result is that `g[2].u` is stored in range #1 at array index 2.
                //
                fieldCursor.m_offset.bindingArrayIndex = m_offset.bindingArrayIndex;

                outCursor = fieldCursor;
                return SLANG_OK;
            }
            break;

        // In some cases the user might be trying to acess a field by name
        // from a cursor that references a constant buffer or parameter block,
        // and in these cases we want the access to Just Work.
        //
        case slang::TypeReflection::Kind::ConstantBuffer:
        case slang::TypeReflection::Kind::ParameterBlock:
            {
                // We basically need to "dereference" the current cursor
                // to go from a pointer to a constant buffer to a pointer
                // to the *contents* of the constant buffer.
                //
                ShaderCursor d = getDereferenced();
                return d.getField(name, outCursor);
            }
            break;
        }

        return SLANG_E_INVALID_ARG;
    }

    ShaderCursor getField(UnownedStringSlice const& name)
    {
        ShaderCursor cursor;
        getField(name, cursor);
        return cursor;
    }

    ShaderCursor getField(String const& name)
    {
        return getField(name.getUnownedSlice());
    }

    ShaderCursor getElement(Index index)
    {
        // TODO: need to auto-dereference various buffer types...

        if(m_typeLayout->getKind() == slang::TypeReflection::Kind::Array)
        {
            ShaderCursor elementCursor;
            elementCursor.m_baseObject = m_baseObject;
            elementCursor.m_typeLayout = m_typeLayout->getElementTypeLayout();
            elementCursor.m_offset.uniformOffset = m_offset.uniformOffset + index * m_typeLayout->getElementStride(SLANG_PARAMETER_CATEGORY_UNIFORM);
            elementCursor.m_offset.bindingRangeIndex = m_offset.bindingRangeIndex;
            elementCursor.m_offset.bindingArrayIndex = m_offset.bindingArrayIndex*m_typeLayout->getElementCount() + index;
            return elementCursor;
        }

        return ShaderCursor();
    }

    static int _peek(UnownedStringSlice const& slice)
    {
        const char* b = slice.begin();
        const char* e = slice.end();
        if(b == e) return -1;
        return *b;
    }

    static int _get(UnownedStringSlice& slice)
    {
        const char* b = slice.begin();
        const char* e = slice.end();
        if(b == e) return -1;
        auto result = *b++;
        slice = UnownedStringSlice(b, e);
        return result;
    }

    static Result followPath(UnownedStringSlice const& path, ShaderCursor& ioCursor)
    {
        ShaderCursor cursor = ioCursor;

        enum
        {
            ALLOW_NAME = 0x1,
            ALLOW_SUBSCRIPT = 0x2,
            ALLOW_DOT = 0x4,
        };
        int state = ALLOW_NAME | ALLOW_SUBSCRIPT;

        UnownedStringSlice rest = path;
        for(;;)
        {
            int c = _peek(rest);

            if(c == -1)
                break;
            else if( c == '.' )
            {
                if(!(state & ALLOW_DOT))
                    return SLANG_E_INVALID_ARG;

                _get(rest);
                state = ALLOW_NAME;
                continue;
            }
            else if( c == '[' )
            {
                if(!(state & ALLOW_SUBSCRIPT))
                    return SLANG_E_INVALID_ARG;

                _get(rest);
                Index index = 0;
                while(_peek(rest) != ']')
                {
                    int d = _get(rest);
                    if(d >= '0' && d <= '9')
                    {
                        index = index*10 + (d - '0');
                    }
                    else
                    {
                        return SLANG_E_INVALID_ARG;
                    }
                }

                if(_peek(rest) != ']')
                    return SLANG_E_INVALID_ARG;
                _get(rest);

                cursor = cursor.getElement(index);
                state = ALLOW_DOT | ALLOW_SUBSCRIPT;
                continue;
            }
            else
            {
                const char* nameBegin = rest.begin();
                for(;;)
                {
                    switch(_peek(rest))
                    {
                    default:
                        _get(rest);
                        continue;

                    case -1:
                    case '.':
                    case '[':
                        break;
                    }
                    break;
                }
                char const* nameEnd = rest.begin();
                UnownedStringSlice name(nameBegin, nameEnd);
                cursor = cursor.getField(name);
                state = ALLOW_DOT | ALLOW_SUBSCRIPT;
                continue;
            }
        }

        ioCursor = cursor;
        return SLANG_OK;
    }

    static Result followPath(String const& path, ShaderCursor& ioCursor)
    {
        return followPath(path.getUnownedSlice(), ioCursor);
    }

    ShaderCursor getPath(UnownedStringSlice const& path)
    {
        ShaderCursor result(*this);
        followPath(path, result);
        return result;
    }


    ShaderCursor getPath(String const& path)
    {
        ShaderCursor result(*this);
        followPath(path, result);
        return result;
    }

    ShaderCursor()
    {}

    ShaderCursor(ShaderObject* object)
        : m_baseObject(object)
        , m_typeLayout(object->getElementTypeLayout())
    {}

    SlangResult setData(void const* data, size_t size)
    {
        return m_baseObject->setData(m_offset, data, size);
    }

    SlangResult setObject(ShaderObject* object)
    {
        return m_baseObject->setObject(m_offset, object);
    }

    SlangResult setResource(ResourceView* resourceView)
    {
        return m_baseObject->setResource(m_offset, resourceView);
    }

    SlangResult setSampler(SamplerState* sampler)
    {
        return m_baseObject->setSampler(m_offset, sampler);
    }

    SlangResult setCombinedTextureSampler(ResourceView* textureView, SamplerState* sampler)
    {
        return m_baseObject->setCombinedTextureSampler(m_offset, textureView, sampler);
    }
};

SlangResult _assignVarsFromLayout(
    Renderer*                   renderer,
    ProgramVars*                shaderObject,
    ShaderInputLayout const&    layout,
    ShaderOutputPlan&           ioOutputPlan,
    slang::ProgramLayout*       slangReflection)
{
    ShaderCursor rootCursor = ShaderCursor(shaderObject);

    const int textureBindFlags = Resource::BindFlag::NonPixelShaderResource | Resource::BindFlag::PixelShaderResource;

    Index entryCount = layout.entries.getCount();
    for(Index entryIndex = 0; entryIndex < entryCount; ++entryIndex)
    {
        auto& entry = layout.entries[entryIndex];
        if(entry.name.getLength() == 0)
        {
            StdWriters::getError().print("error: entries in `ShaderInputLayout` must include a name\n");
            return SLANG_E_INVALID_ARG;
        }

        auto entryCursor = rootCursor.getPath(entry.name);

        if(!entryCursor.isValid())
        {
            for(auto entryPoint : shaderObject->getEntryPoints())
            {
                entryCursor = ShaderCursor(entryPoint).getPath(entry.name);
                if(entryCursor.isValid())
                    break;
            }
        }


        if(!entryCursor.isValid())
        {
            StdWriters::getError().print("error: could not find shader parameter matching '%s'\n", entry.name.begin());
            return SLANG_E_INVALID_ARG;
        }

        RefPtr<Resource> resource;
        switch(entry.type)
        {
        case ShaderInputType::Uniform:
            {
                const size_t bufferSize = entry.bufferData.getCount() * sizeof(uint32_t);

                ShaderCursor dataCursor = entryCursor;
                switch(dataCursor.getTypeLayout()->getKind())
                {
                case slang::TypeReflection::Kind::ConstantBuffer:
                case slang::TypeReflection::Kind::ParameterBlock:
                    dataCursor = dataCursor.getDereferenced();
                    break;

                default:
                    break;

                }

                dataCursor.setData(entry.bufferData.getBuffer(), bufferSize);
            }
            break;

        case ShaderInputType::Buffer:
            {
                const InputBufferDesc& srcBuffer = entry.bufferDesc;
                const size_t bufferSize = entry.bufferData.getCount() * sizeof(uint32_t);

                switch(srcBuffer.type)
                {
                case InputBufferType::ConstantBuffer:
                    {
                        // A `cbuffer` input line actually represents the data we
                        // want to write *into* the buffer, and shouldn't
                        // allocate a buffer itself.
                        //
                        entryCursor.getDereferenced().setData(entry.bufferData.getBuffer(), bufferSize);
                    }
                    break;

                case InputBufferType::RootConstantBuffer:
                    {
                        // A `root_constants` input line actually represents the data we
                        // want to write *into* the buffer, and shouldn't
                        // allocate a buffer itself.
                        //
                        // Note: we are not doing `.getDereferenced()` here because the
                        // `root_constant` line should be referring to a parameter value
                        // inside the root-constant range, and not the range/buffer itself.
                        //
                        entryCursor.setData(entry.bufferData.getBuffer(), bufferSize);
                    }
                    break;

                default:
                    {

                        RefPtr<BufferResource> bufferResource;
                        SLANG_RETURN_ON_FAIL(ShaderRendererUtil::createBufferResource(entry.bufferDesc, entry.isOutput, bufferSize, entry.bufferData.getBuffer(), renderer, bufferResource));
                        resource = bufferResource;

                        ResourceView::Desc viewDesc;
                        viewDesc.type = ResourceView::Type::UnorderedAccess;
                        viewDesc.format = srcBuffer.format;
                        auto bufferView = renderer->createBufferView(
                            bufferResource,
                            viewDesc);
                        entryCursor.setResource(bufferView);
                    }
                    break;
                }

#if 0
                switch(srcBuffer.type)
                {
                case InputBufferType::ConstantBuffer:
                    descriptorSet->setConstantBuffer(rangeIndex, 0, bufferResource);
                    break;

                case InputBufferType::StorageBuffer:
                    {
                        ResourceView::Desc viewDesc;
                        viewDesc.type = ResourceView::Type::UnorderedAccess;
                        viewDesc.format = srcBuffer.format;
                        auto bufferView = renderer->createBufferView(
                            bufferResource,
                            viewDesc);
                        descriptorSet->setResource(rangeIndex, 0, bufferView);
                    }
                    break;
                }

                if(srcEntry.isOutput)
                {
                    BindingStateImpl::OutputBinding binding;
                    binding.entryIndex = i;
                    binding.resource = bufferResource;
                    outputBindings.add(binding);
                }
#endif
            }
            break;

        case ShaderInputType::CombinedTextureSampler:
            {
                RefPtr<TextureResource> texture;
                SLANG_RETURN_ON_FAIL(ShaderRendererUtil::generateTextureResource(entry.textureDesc, textureBindFlags, renderer, texture));
                resource = texture;

                auto sampler = _createSamplerState(renderer, entry.samplerDesc);

                ResourceView::Desc viewDesc;
                viewDesc.type = ResourceView::Type::ShaderResource;
                auto textureView = renderer->createTextureView(
                    texture,
                    viewDesc);

                entryCursor.setCombinedTextureSampler(textureView, sampler);

#if 0
                descriptorSet->setCombinedTextureSampler(rangeIndex, 0, textureView, sampler);

                if(srcEntry.isOutput)
                {
                    BindingStateImpl::OutputBinding binding;
                    binding.entryIndex = i;
                    binding.resource = texture;
                    outputBindings.add(binding);
                }
#endif
            }
            break;

        case ShaderInputType::Texture:
            {
                RefPtr<TextureResource> texture;
                SLANG_RETURN_ON_FAIL(ShaderRendererUtil::generateTextureResource(entry.textureDesc, textureBindFlags, renderer, texture));
                resource = texture;

                // TODO: support UAV textures...

                ResourceView::Desc viewDesc;
                viewDesc.type = ResourceView::Type::ShaderResource;
                auto textureView = renderer->createTextureView(
                    texture,
                    viewDesc);

                if (!textureView)
                {
                    return SLANG_FAIL;
                }

                entryCursor.setResource(textureView);

#if 0
                descriptorSet->setResource(rangeIndex, 0, textureView);

                if(srcEntry.isOutput)
                {
                    BindingStateImpl::OutputBinding binding;
                    binding.entryIndex = i;
                    binding.resource = texture;
                    outputBindings.add(binding);
                }
#endif
            }
            break;

        case ShaderInputType::Sampler:
            {
                auto sampler = _createSamplerState(renderer, entry.samplerDesc);

                entryCursor.setSampler(sampler);
#if 0
                descriptorSet->setSampler(rangeIndex, 0, sampler);
#endif
            }
            break;

        case ShaderInputType::Object:
            {
                auto typeName = entry.objectDesc.typeName;
                auto slangType = slangReflection->findTypeByName(typeName.getBuffer());
                auto slangTypeLayout = slangReflection->getTypeLayout(slangType);

                RefPtr<ShaderObjectLayout> shaderObjectLayout;
                SLANG_RETURN_ON_FAIL(ShaderObjectLayout::createForElementType(renderer, slangTypeLayout, shaderObjectLayout.writeRef()));

                RefPtr<ShaderObject> shaderObject;
                SLANG_RETURN_ON_FAIL(ShaderObject::create(renderer, shaderObjectLayout, shaderObject.writeRef()));

                entryCursor.setObject(shaderObject);
            }
            break;

        default:
            assert(!"Unhandled type");
            return SLANG_FAIL;
        }

        if(entry.isOutput)
        {
            ShaderOutputPlan::Item item;
            item.inputLayoutEntryIndex = entryIndex;
            item.resource = resource;
            ioOutputPlan.items.add(item);
        }

    }
    return SLANG_OK;
}

void LegacyRenderTestApp::applyBinding(PipelineType pipelineType)
{
    m_bindingState->apply(m_renderer.Ptr(), pipelineType);
}

void ShaderObjectRenderTestApp::applyBinding(PipelineType pipelineType)
{
    m_programVars->apply(m_renderer.Ptr(), pipelineType);
}

SlangResult LegacyRenderTestApp::initialize(
    SlangSession* session,
    Renderer* renderer,
    const Options& options,
    const ShaderCompilerUtil::Input& input)
{
    m_options = options;

    SLANG_RETURN_ON_FAIL(_initializeShaders(session, renderer, options.shaderType, input));

    m_numAddedConstantBuffers = 0;
    m_renderer = renderer;

    // TODO(tfoley): use each API's reflection interface to query the constant-buffer size needed
    m_constantBufferSize = 16 * sizeof(float);

    BufferResource::Desc constantBufferDesc;
    constantBufferDesc.init(m_constantBufferSize);
    constantBufferDesc.cpuAccessFlags = Resource::AccessFlag::Write;

    m_constantBuffer =
        renderer->createBufferResource(Resource::Usage::ConstantBuffer, constantBufferDesc);
    if (!m_constantBuffer)
        return SLANG_FAIL;

    //! Hack -> if doing a graphics test, add an extra binding for our dynamic constant buffer
    //
    // TODO: Should probably be more sophisticated than this - with 'dynamic' constant buffer/s
    // binding always being specified in the test file
    RefPtr<BufferResource> addedConstantBuffer;
    switch (m_options.shaderType)
    {
    default:
        break;

    case Options::ShaderProgramType::Graphics:
    case Options::ShaderProgramType::GraphicsCompute:
        addedConstantBuffer = m_constantBuffer;
        m_numAddedConstantBuffers++;
        break;
    }

    BindingStateImpl* bindingState = nullptr;
    SLANG_RETURN_ON_FAIL(ShaderRendererUtil::createBindingState(
        m_shaderInputLayout, m_renderer, addedConstantBuffer, &bindingState));
    m_bindingState = bindingState;

    // Do other initialization that doesn't depend on the source language.

    // Input Assembler (IA)

    const InputElementDesc inputElements[] = {
        {"A", 0, Format::RGB_Float32, offsetof(Vertex, position)},
        {"A", 1, Format::RGB_Float32, offsetof(Vertex, color)},
        {"A", 2, Format::RG_Float32, offsetof(Vertex, uv)},
    };

    m_inputLayout = renderer->createInputLayout(inputElements, SLANG_COUNT_OF(inputElements));
    if (!m_inputLayout)
        return SLANG_FAIL;

    BufferResource::Desc vertexBufferDesc;
    vertexBufferDesc.init(kVertexCount * sizeof(Vertex));

    m_vertexBuffer = renderer->createBufferResource(
        Resource::Usage::VertexBuffer, vertexBufferDesc, kVertexData);
    if (!m_vertexBuffer)
        return SLANG_FAIL;

    {
        switch (m_options.shaderType)
        {
        default:
            assert(!"unexpected test shader type");
            return SLANG_FAIL;

        case Options::ShaderProgramType::Compute:
            {
                ComputePipelineStateDesc desc;
                desc.pipelineLayout = m_bindingState->pipelineLayout;
                desc.program = m_shaderProgram;

                m_pipelineState = renderer->createComputePipelineState(desc);
            }
            break;

        case Options::ShaderProgramType::Graphics:
        case Options::ShaderProgramType::GraphicsCompute:
            {
                GraphicsPipelineStateDesc desc;
                desc.pipelineLayout = m_bindingState->pipelineLayout;
                desc.program = m_shaderProgram;
                desc.inputLayout = m_inputLayout;
                desc.renderTargetCount = m_bindingState->m_numRenderTargets;

                m_pipelineState = renderer->createGraphicsPipelineState(desc);
            }
            break;
        }
    }

    // If success must have a pipeline state
    return m_pipelineState ? SLANG_OK : SLANG_FAIL;
}

SlangResult ShaderObjectRenderTestApp::initialize(
    SlangSession* session,
    Renderer* renderer,
    const Options& options,
    const ShaderCompilerUtil::Input& input)
{
    m_options = options;

    // We begin by compiling the shader file and entry points that specified via the options.
    //
    SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, input, m_compilationOutput));
    m_shaderInputLayout = m_compilationOutput.layout;

    // Once the shaders have been compiled we load them via the underlying API.
    //
    SLANG_RETURN_ON_FAIL(renderer->createProgram(m_compilationOutput.output.desc, m_shaderProgram.writeRef()));

    // If we are doing a non-pass-through compilation, then we will rely on
    // Slang's reflection API to tell us what the parameters of the program are.
    //
    RefPtr<ProgramLayout> programLayout = nullptr;

    // Okay, we will use Slang reflection to determine what the parameters of the shader are.
    //
    auto slangReflection = (slang::ProgramLayout*) spGetReflection(m_compilationOutput.output.getRequestForReflection());
    {
        ProgramLayout::Builder builder(renderer);
        builder.addGlobalParams(slangReflection->getGlobalParamsVarLayout());

        // TODO: Also need to reflect entry points here.

        SlangInt entryPointCount = slangReflection->getEntryPointCount();
        for(SlangInt e = 0; e < entryPointCount; ++e)
        {
            auto slangEntryPoint = slangReflection->getEntryPointByIndex(e);

            EntryPointLayout::Builder entryPointBuilder(renderer);
            entryPointBuilder.addEntryPointParams(slangEntryPoint);

            RefPtr<EntryPointLayout> entryPointLayout;
            SLANG_RETURN_ON_FAIL(entryPointBuilder.build(entryPointLayout.writeRef()));

            builder.addEntryPoint(entryPointLayout);
        }

        SLANG_RETURN_ON_FAIL(builder.build(programLayout.writeRef()));
    }

    // The shape of the parameters will determine the pipeline layout that we use.
    //
    RefPtr<PipelineLayout> pipelineLayout = programLayout->getPipelineLayout();

    // Once we have determined the layout of all the parameters we need to bind,
    // we will create a shader object to use for storing and binding those parameters.
    //
    RefPtr<ProgramVars> programVars;
    SLANG_RETURN_ON_FAIL(ProgramVars::create(renderer, programLayout, programVars.writeRef()));
    m_programVars = programVars;

    // Now we need to assign from the input parameter data that was parsed into
    // the program vars we allocated.
    //
    SLANG_RETURN_ON_FAIL(_assignVarsFromLayout(renderer, programVars, m_compilationOutput.layout, m_outputPlan, slangReflection));

	m_renderer = renderer;

    // TODO(tfoley): use each API's reflection interface to query the constant-buffer size needed
//    m_constantBufferSize = 16 * sizeof(float);

    {
        switch(m_options.shaderType)
        {
        default:
            assert(!"unexpected test shader type");
            return SLANG_FAIL;

        case Options::ShaderProgramType::Compute:
            {
                ComputePipelineStateDesc desc;
                desc.pipelineLayout = pipelineLayout;
                desc.program = m_shaderProgram;

                m_pipelineState = renderer->createComputePipelineState(desc);
            }
            break;

        case Options::ShaderProgramType::Graphics:
        case Options::ShaderProgramType::GraphicsCompute:
            {
                // TODO: We should conceivably be able to match up the "available" vertex
                // attributes, as defined by the vertex stream(s) on the model being
                // renderer, with the "required" vertex attributes as defiend on the
                // shader.
                //
                // For now we just create a fixed input layout for all graphics tests
                // since at present they all draw the same single triangle with a
                // fixed/known set of attributes.
                //
                const InputElementDesc inputElements[] = {
                    { "A", 0, Format::RGB_Float32, offsetof(Vertex, position) },
                    { "A", 1, Format::RGB_Float32, offsetof(Vertex, color) },
                    { "A", 2, Format::RG_Float32,  offsetof(Vertex, uv) },
                };

                RefPtr<InputLayout> inputLayout;
                SLANG_RETURN_ON_FAIL(renderer->createInputLayout(inputElements, SLANG_COUNT_OF(inputElements), inputLayout.writeRef()));

                BufferResource::Desc vertexBufferDesc;
                vertexBufferDesc.init(kVertexCount * sizeof(Vertex));

                SLANG_RETURN_ON_FAIL(renderer->createBufferResource(Resource::Usage::VertexBuffer, vertexBufferDesc, kVertexData, m_vertexBuffer.writeRef()));

                GraphicsPipelineStateDesc desc;
                desc.pipelineLayout = pipelineLayout;
                desc.program = m_shaderProgram;
                desc.inputLayout = inputLayout;
                desc.renderTargetCount = programLayout->getRenderTargetCount();

                m_pipelineState = renderer->createGraphicsPipelineState(desc);
            }
            break;
        }
    }

    // If success must have a pipeline state
    return m_pipelineState ? SLANG_OK : SLANG_FAIL;
}

Result RenderTestApp::_initializeShaders(SlangSession* session, Renderer* renderer, Options::ShaderProgramType shaderType, const ShaderCompilerUtil::Input& input)
{
    SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, m_options, input,  m_compilationOutput));
    m_shaderInputLayout = m_compilationOutput.layout;
    m_shaderProgram = renderer->createProgram(m_compilationOutput.output.desc);
    return m_shaderProgram ? SLANG_OK : SLANG_FAIL;
}


void LegacyRenderTestApp::setProjectionMatrix()
{
    auto mappedData = m_renderer->map(m_constantBuffer, MapFlavor::WriteDiscard);
    if (mappedData)
    {
        const ProjectionStyle projectionStyle =
            RendererUtil::getProjectionStyle(m_renderer->getRendererType());
        RendererUtil::getIdentityProjection(projectionStyle, (float*)mappedData);

        m_renderer->unmap(m_constantBuffer);
    }
}

void ShaderObjectRenderTestApp::setProjectionMatrix()
{
    const ProjectionStyle projectionStyle =
        RendererUtil::getProjectionStyle(m_renderer->getRendererType());

    float projectionMatrix[16];
    RendererUtil::getIdentityProjection(projectionStyle, projectionMatrix);
    ShaderCursor(m_programVars)
        .getField("Uniforms")
        .getDereferenced()
        .setData(projectionMatrix, sizeof(projectionMatrix));
}

void RenderTestApp::renderFrame()
{
    setProjectionMatrix();

    auto pipelineType = PipelineType::Graphics;

    m_renderer->setPipelineState(pipelineType, m_pipelineState);

	m_renderer->setPrimitiveTopology(PrimitiveTopology::TriangleList);
	m_renderer->setVertexBuffer(0, m_vertexBuffer, sizeof(Vertex));

    applyBinding(pipelineType);

	m_renderer->draw(3);
}

void RenderTestApp::runCompute()
{
    auto pipelineType = PipelineType::Compute;
    m_renderer->setPipelineState(pipelineType, m_pipelineState);
    applyBinding(pipelineType);

    m_startTicks = ProcessUtil::getClockTick();

	m_renderer->dispatchCompute(m_options.computeDispatchSize[0], m_options.computeDispatchSize[1], m_options.computeDispatchSize[2]);
}

void RenderTestApp::finalize()
{
}

Result LegacyRenderTestApp::writeBindingOutput(BindRoot* bindRoot, const char* fileName)
{
    // Submit the work
    m_renderer->submitGpuWork();
    // Wait until everything is complete
    m_renderer->waitForGpu();

    FILE * f = fopen(fileName, "wb");
    if (!f)
    {
        return SLANG_FAIL;
    }
    FileWriter writer(f, WriterFlags(0));

    for(auto binding : m_bindingState->outputBindings)
    {
        auto i = binding.entryIndex;
        const auto& layoutBinding = m_shaderInputLayout.entries[i];

        assert(layoutBinding.isOutput);
        
        if (binding.resource && binding.resource->isBuffer())
        {
            BufferResource* bufferResource = static_cast<BufferResource*>(binding.resource.Ptr());
            const size_t bufferSize = bufferResource->getDesc().sizeInBytes;

            unsigned int* ptr = (unsigned int*)m_renderer->map(bufferResource, MapFlavor::HostRead);
            if (!ptr)
            {
                return SLANG_FAIL;
            }

            const SlangResult res = ShaderInputLayout::writeBinding(bindRoot, m_shaderInputLayout.entries[i], ptr, bufferSize, &writer);

            m_renderer->unmap(bufferResource);

            SLANG_RETURN_ON_FAIL(res);
        }
        else
        {
            printf("invalid output type at %d.\n", int(i));
        }
    }
    
    return SLANG_OK;
}

Result ShaderObjectRenderTestApp::writeBindingOutput(BindRoot* bindRoot, const char* fileName)
{
    // Submit the work
    m_renderer->submitGpuWork();
    // Wait until everything is complete
    m_renderer->waitForGpu();

    FILE * f = fopen(fileName, "wb");
    if (!f)
    {
        return SLANG_FAIL;
    }
    FileWriter writer(f, WriterFlags(0));

    for(auto outputItem : m_outputPlan.items)
    {
        auto& inputEntry = m_shaderInputLayout.entries[outputItem.inputLayoutEntryIndex];
        assert(inputEntry.isOutput);

        auto resource = outputItem.resource;
        if (resource && resource->isBuffer())
        {
            BufferResource* bufferResource = static_cast<BufferResource*>(resource.Ptr());
            const size_t bufferSize = bufferResource->getDesc().sizeInBytes;

            unsigned int* ptr = (unsigned int*)m_renderer->map(bufferResource, MapFlavor::HostRead);
            if (!ptr)
            {
                return SLANG_FAIL;
            }

            const SlangResult res = ShaderInputLayout::writeBinding(bindRoot, inputEntry, ptr, bufferSize, &writer);

            m_renderer->unmap(bufferResource);

            SLANG_RETURN_ON_FAIL(res);
        }
        else
        {
            printf("invalid output type at %d.\n", int(outputItem.inputLayoutEntryIndex));
        }
    }
    return SLANG_OK;
}


Result RenderTestApp::writeScreen(const char* filename)
{
    Surface surface;
    SLANG_RETURN_ON_FAIL(m_renderer->captureScreenSurface(surface));
    return PngSerializeUtil::write(filename, surface);
}

Result RenderTestApp::update(Window* window)
{
    // Whenever we don't have Windows events to process, we render a frame.
    if (m_options.shaderType == Options::ShaderProgramType::Compute)
    {
        runCompute();
    }
    else
    {
        static const float kClearColor[] = { 0.25, 0.25, 0.25, 1.0 };
        m_renderer->setClearColor(kClearColor);
        m_renderer->clearFrame();

        renderFrame();
    }

    // If we are in a mode where output is requested, we need to snapshot the back buffer here
    if (m_options.outputPath || m_options.performanceProfile)
    {
        // Submit the work
        m_renderer->submitGpuWork();
        // Wait until everything is complete
        m_renderer->waitForGpu();

        if (m_options.performanceProfile)
        {
#if 0
            // It might not be enough on some APIs to 'waitForGpu' to mean the computation has completed. Let's lock an output
            // buffer to be sure
            if (m_bindingState->outputBindings.getCount() > 0)
            {
                const auto& binding = m_bindingState->outputBindings[0];
                auto i = binding.entryIndex;
                const auto& layoutBinding = m_shaderInputLayout.entries[i];

                assert(layoutBinding.isOutput);
                
                if (binding.resource && binding.resource->isBuffer())
                {
                    BufferResource* bufferResource = static_cast<BufferResource*>(binding.resource.Ptr());
                    const size_t bufferSize = bufferResource->getDesc().sizeInBytes;
                    unsigned int* ptr = (unsigned int*)m_renderer->map(bufferResource, MapFlavor::HostRead);
                    if (!ptr)
                    {                            
                        return SLANG_FAIL;
                    }
                    m_renderer->unmap(bufferResource);
                }
            }
#endif

            // Note we don't do the same with screen rendering -> as that will do a lot of work, which may swamp any computation
            // so can only really profile compute shaders at the moment

            const uint64_t endTicks = ProcessUtil::getClockTick();

            _outputProfileTime(m_startTicks, endTicks);
        }

        if (m_options.outputPath)
        {
            if (m_options.shaderType == Options::ShaderProgramType::Compute || m_options.shaderType == Options::ShaderProgramType::GraphicsCompute)
            {
                auto request = m_compilationOutput.output.getRequestForReflection();
                auto slangReflection = (slang::ShaderReflection*) spGetReflection(request);

                BindSet bindSet;
                GPULikeBindRoot bindRoot;
                bindRoot.init(&bindSet, slangReflection, 0);

                BindRoot* outputBindRoot = m_options.outputUsingType ? &bindRoot : nullptr;

                SLANG_RETURN_ON_FAIL(writeBindingOutput(outputBindRoot, m_options.outputPath));
            }
            else
            {
                SlangResult res = writeScreen(m_options.outputPath);
                if (SLANG_FAILED(res))
                {
                    fprintf(stderr, "ERROR: failed to write screen capture to file\n");
                    return res;
                }
            }
        }
        // We are done
        window->postQuit();
        return SLANG_OK;
    }

    m_renderer->presentFrame();
    return SLANG_OK;
}


static SlangResult _setSessionPrelude(const Options& options, const char* exePath, SlangSession* session)
{
    // Let's see if we need to set up special prelude for HLSL
    if (options.nvapiExtnSlot.getLength())
    {
        String rootPath;
        SLANG_RETURN_ON_FAIL(TestToolUtil::getRootPath(exePath, rootPath));

        String includePath;
        SLANG_RETURN_ON_FAIL(TestToolUtil::getIncludePath(rootPath, "external/nvapi/nvHLSLExtns.h", includePath));

        StringBuilder buf;
        // We have to choose a slot that NVAPI will use. 
        buf << "#define NV_SHADER_EXTN_SLOT " << options.nvapiExtnSlot << "\n";

        // Include the NVAPI header
        buf << "#include \"" << includePath << "\"\n\n";

        session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_HLSL, buf.getBuffer());
    }
    else
    {
        session->setLanguagePrelude(SLANG_SOURCE_LANGUAGE_HLSL, "");
    }

    return SLANG_OK;
}

} //  namespace renderer_test

static SlangResult _innerMain(Slang::StdWriters* stdWriters, SlangSession* session, int argcIn, const char*const* argvIn)
{
    using namespace renderer_test;
    using namespace Slang;

    StdWriters::setSingleton(stdWriters);

    Options options;

	// Parse command-line options
	SLANG_RETURN_ON_FAIL(Options::parse(argcIn, argvIn, StdWriters::getError(), options));

    // Declare window pointer before renderer, such that window is released after renderer
    RefPtr<renderer_test::Window> window;

    ShaderCompilerUtil::Input input;
    
    input.profile = "";
    input.target = SLANG_TARGET_NONE;
    input.args = &options.slangArgs[0];
    input.argCount = options.slangArgCount;

	SlangSourceLanguage nativeLanguage = SLANG_SOURCE_LANGUAGE_UNKNOWN;
	SlangPassThrough slangPassThrough = SLANG_PASS_THROUGH_NONE;
    char const* profileName = "";
	switch (options.rendererType)
	{
		case RendererType::DirectX11:
			input.target = SLANG_DXBC;
            input.profile = "sm_5_0";
			nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
            slangPassThrough = SLANG_PASS_THROUGH_FXC;
            
			break;

		case RendererType::DirectX12:
			input.target = SLANG_DXBC;
            input.profile = "sm_5_0";
			nativeLanguage = SLANG_SOURCE_LANGUAGE_HLSL;
            slangPassThrough = SLANG_PASS_THROUGH_FXC;
            
            if( options.useDXIL )
            {
                input.target = SLANG_DXIL;
                input.profile = "sm_6_0";
                slangPassThrough = SLANG_PASS_THROUGH_DXC;
            }
			break;

		case RendererType::OpenGl:
			input.target = SLANG_GLSL;
            input.profile = "glsl_430";
			nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
            slangPassThrough = SLANG_PASS_THROUGH_GLSLANG;
			break;

		case RendererType::Vulkan:
			input.target = SLANG_SPIRV;
            input.profile = "glsl_430";
			nativeLanguage = SLANG_SOURCE_LANGUAGE_GLSL;
            slangPassThrough = SLANG_PASS_THROUGH_GLSLANG;
			break;
        case RendererType::CPU:
            input.target = SLANG_HOST_CALLABLE;
            input.profile = "";
            nativeLanguage = SLANG_SOURCE_LANGUAGE_CPP;
            slangPassThrough = SLANG_PASS_THROUGH_GENERIC_C_CPP;
            break;
        case RendererType::CUDA:
            input.target = SLANG_PTX;
            input.profile = "";
            nativeLanguage = SLANG_SOURCE_LANGUAGE_CUDA;
            slangPassThrough = SLANG_PASS_THROUGH_NVRTC;
            break;

		default:
			fprintf(stderr, "error: unexpected\n");
			return SLANG_FAIL;
	}

    switch (options.inputLanguageID)
    {
        case Options::InputLanguageID::Slang:
            input.sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
            input.passThrough = SLANG_PASS_THROUGH_NONE;
            break;

        case Options::InputLanguageID::Native:
            input.sourceLanguage = nativeLanguage;
            input.passThrough = slangPassThrough;
            break;

        default:
            break;
    }

    switch( options.shaderType )
    {
    case Options::ShaderProgramType::Graphics:
    case Options::ShaderProgramType::GraphicsCompute:
        input.pipelineType = PipelineType::Graphics;
        break;

    case Options::ShaderProgramType::Compute:
        input.pipelineType = PipelineType::Compute;
        break;

    case Options::ShaderProgramType::RayTracing:
        input.pipelineType = PipelineType::RayTracing;
        break;

    default:
        break;
    }

    if (options.sourceLanguage != SLANG_SOURCE_LANGUAGE_UNKNOWN)
    {
        input.sourceLanguage = options.sourceLanguage;

        if (input.sourceLanguage == SLANG_SOURCE_LANGUAGE_C || input.sourceLanguage == SLANG_SOURCE_LANGUAGE_CPP)
        {
            input.passThrough = SLANG_PASS_THROUGH_GENERIC_C_CPP;
        }
    }

    // Use the profile name set on options if set
    input.profile = options.profileName ? options.profileName : input.profile;

    StringBuilder rendererName;
    rendererName << "[" << RendererUtil::toText(options.rendererType) << "] ";
    if (options.adapter.getLength())
    {
        rendererName << "'" << options.adapter << "'";
    }

    if (options.onlyStartup)
    {
        switch (options.rendererType)
        {
            case RendererType::CUDA:
            {
#if RENDER_TEST_CUDA
                return SLANG_SUCCEEDED(spSessionCheckPassThroughSupport(session, SLANG_PASS_THROUGH_NVRTC)) && CUDAComputeUtil::canCreateDevice() ? SLANG_OK : SLANG_FAIL;
#else
                return SLANG_FAIL;
#endif
            }
            case RendererType::CPU:
            {
                // As long as we have CPU, then this should work
                return spSessionCheckPassThroughSupport(session, SLANG_PASS_THROUGH_GENERIC_C_CPP);
            }
            default: break;
        }
    }

    Index nvapiExtnSlot = -1;

    // Let's see if we need to set up special prelude for HLSL
    if (options.nvapiExtnSlot.getLength() && options.nvapiExtnSlot[0] == 'u')
    {
        //
        Slang::Int value;
        UnownedStringSlice slice = options.nvapiExtnSlot.getUnownedSlice();
        UnownedStringSlice indexText(slice.begin() + 1 , slice.end());
        if (SLANG_SUCCEEDED(StringUtil::parseInt(indexText, value)))
        {
            nvapiExtnSlot = Index(value);
        }
    }

    // If can't set up a necessary prelude make not available (which will lead to the test being ignored)
    if (SLANG_FAILED(_setSessionPrelude(options, argvIn[0], session)))
    {
        return SLANG_E_NOT_AVAILABLE;
    }

    // If it's CPU testing we don't need a window or a renderer
    if (options.rendererType == RendererType::CPU)
    {
        // Check we have all the required features
        for (const auto& renderFeature : options.renderFeatures)
        {
            if (!CPUComputeUtil::hasFeature(renderFeature.getUnownedSlice()))
            {
                return SLANG_E_NOT_AVAILABLE;
            }
        }

        ShaderCompilerUtil::OutputAndLayout compilationAndLayout;
        SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, input, compilationAndLayout));

        {
            // Get the shared library -> it contains the executable code, we need to keep around if we recompile
            ComPtr<ISlangSharedLibrary> sharedLibrary;
            SLANG_RETURN_ON_FAIL(spGetEntryPointHostCallable(compilationAndLayout.output.getRequestForKernels(), 0, 0, sharedLibrary.writeRef()));

            // This is a hack to work around, reflection when compiling straight C/C++ code. In that case the code is just passed
            // straight through to the C++ compiler so no reflection. In these tests though we should have conditional code
            // (performance-profile.slang for example), such that there is both a slang and C++ code, and it is the job
            // of the test implementer to *ensure* that the straight C++ code has the same layout as the slang C++ backend.
            //
            // If we are running c/c++ we still need binding information, so compile again as slang source
            if (options.sourceLanguage == SLANG_SOURCE_LANGUAGE_C || input.sourceLanguage == SLANG_SOURCE_LANGUAGE_CPP)
            {
                ShaderCompilerUtil::Input slangInput = input;
                slangInput.sourceLanguage = SLANG_SOURCE_LANGUAGE_SLANG;
                slangInput.passThrough = SLANG_PASS_THROUGH_NONE;
                // We just want CPP, so we get suitable reflection
                slangInput.target = SLANG_CPP_SOURCE;

                SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, slangInput, compilationAndLayout));
            }

            // calculate binding
            CPUComputeUtil::Context context;
            SLANG_RETURN_ON_FAIL(CPUComputeUtil::createBindlessResources(compilationAndLayout, context));
            SLANG_RETURN_ON_FAIL(CPUComputeUtil::fillRuntimeHandleInBuffers(compilationAndLayout, context, sharedLibrary.get()));
            SLANG_RETURN_ON_FAIL(CPUComputeUtil::calcBindings(compilationAndLayout, context));

            // Get the execution info from the lib
            CPUComputeUtil::ExecuteInfo info;
            SLANG_RETURN_ON_FAIL(CPUComputeUtil::calcExecuteInfo(CPUComputeUtil::ExecuteStyle::GroupRange, sharedLibrary, options.computeDispatchSize, compilationAndLayout, context, info));

            const uint64_t startTicks = ProcessUtil::getClockTick();

            SLANG_RETURN_ON_FAIL(CPUComputeUtil::execute(info));

            if (options.performanceProfile)
            {
                const uint64_t endTicks = ProcessUtil::getClockTick();
                _outputProfileTime(startTicks, endTicks);
            }

            if (options.outputPath)
            {
                BindRoot* outputBindRoot = options.outputUsingType ? &context.m_bindRoot : nullptr;


                // Dump everything out that was written
                SLANG_RETURN_ON_FAIL(ShaderInputLayout::writeBindings(outputBindRoot, compilationAndLayout.layout, context.m_buffers, options.outputPath));

                // Check all execution styles produce the same result
                SLANG_RETURN_ON_FAIL(CPUComputeUtil::checkStyleConsistency(sharedLibrary, options.computeDispatchSize, compilationAndLayout));
            }
        }

        return SLANG_OK;
    }

    if (options.rendererType == RendererType::CUDA)
    {        
#if RENDER_TEST_CUDA
        // Check we have all the required features
        for (const auto& renderFeature : options.renderFeatures)
        {
            if (!CUDAComputeUtil::hasFeature(renderFeature.getUnownedSlice()))
            {
                return SLANG_E_NOT_AVAILABLE;
            }
        }

        ShaderCompilerUtil::OutputAndLayout compilationAndLayout;
        SLANG_RETURN_ON_FAIL(ShaderCompilerUtil::compileWithLayout(session, options, input, compilationAndLayout));

        const uint64_t startTicks = ProcessUtil::getClockTick();

        CUDAComputeUtil::Context context;
        SLANG_RETURN_ON_FAIL(CUDAComputeUtil::execute(compilationAndLayout, options.computeDispatchSize, context));

        if (options.performanceProfile)
        {
            const uint64_t endTicks = ProcessUtil::getClockTick();
            _outputProfileTime(startTicks, endTicks);
        }

        if (options.outputPath)
        {
            BindRoot* outputBindRoot = options.outputUsingType ? &context.m_bindRoot : nullptr;

            // Dump everything out that was written
            SLANG_RETURN_ON_FAIL(ShaderInputLayout::writeBindings(outputBindRoot, compilationAndLayout.layout, context.m_buffers, options.outputPath));
        }

        return SLANG_OK;
#else
        return SLANG_FAIL;
#endif
    }

    Slang::RefPtr<Renderer> renderer;
    {
        RendererUtil::CreateFunc createFunc = RendererUtil::getCreateFunc(options.rendererType);
        if (createFunc)
        {
            renderer = createFunc();
        }

        if (!renderer)
        {
            if (!options.onlyStartup)
            {
                fprintf(stderr, "Unable to create renderer %s\n", rendererName.getBuffer());
            }
            return SLANG_FAIL;
        }

        Renderer::Desc desc;
        desc.width = gWindowWidth;
        desc.height = gWindowHeight;
        desc.adapter = options.adapter;
        desc.requiredFeatures = options.renderFeatures;
        desc.nvapiExtnSlot = int(nvapiExtnSlot);

        window = renderer_test::Window::create();
        SLANG_RETURN_ON_FAIL(window->initialize(gWindowWidth, gWindowHeight));

        SlangResult res = renderer->initialize(desc, window->getHandle());
        if (SLANG_FAILED(res))
        {
            // Returns E_NOT_AVAILABLE only when specified features are not available.
            // Will cause to be ignored.
            if (!options.onlyStartup && res != SLANG_E_NOT_AVAILABLE)
            {
                fprintf(stderr, "Unable to initialize renderer %s\n", rendererName.getBuffer());
            }
            return res;
        }

        for (const auto& feature : options.renderFeatures)
        {
            // If doesn't have required feature... we have to give up
            if (!renderer->hasFeature(feature.getUnownedSlice()))
            {
                return SLANG_E_NOT_AVAILABLE;
            }
        }
    }
   
    // If the only test is we can startup, then we are done
    if (options.onlyStartup)
    {
        return SLANG_OK;
    }

	{
        RefPtr<RenderTestApp> app;
        if (options.useShaderObjects)
            app = new ShaderObjectRenderTestApp();
        else
            app = new LegacyRenderTestApp();
		SLANG_RETURN_ON_FAIL(app->initialize(session, renderer, options, input));
        window->show();
        return window->runLoop(app);
	}
}

SLANG_TEST_TOOL_API SlangResult innerMain(Slang::StdWriters* stdWriters, SlangSession* sharedSession, int inArgc, const char*const* inArgv)
{
    using namespace Slang;

    // Assume we will used the shared session
    ComPtr<slang::IGlobalSession> session(sharedSession);

    // The sharedSession always has a pre-loaded stdlib.
    // This differed test checks if the command line has an option to setup the stdlib.
    // If so we *don't* use the sharedSession, and create a new stdlib-less session just for this compilation. 
    if (TestToolUtil::hasDeferredStdLib(Index(inArgc - 1), inArgv + 1))
    {
        SLANG_RETURN_ON_FAIL(slang_createGlobalSessionWithoutStdLib(SLANG_API_VERSION, session.writeRef()));
    }

    SlangResult res = SLANG_FAIL;
    try
    {
        res = _innerMain(stdWriters, session, inArgc, inArgv);
    }
    catch (const Slang::Exception& exception)
    {
        stdWriters->getOut().put(exception.Message.getUnownedSlice());
        return SLANG_FAIL;
    }
    catch (...)
    {
        stdWriters->getOut().put(UnownedStringSlice::fromLiteral("Unhandled exception"));
        return SLANG_FAIL;
    }

    return res;
}

int main(int argc, char**  argv)
{
    using namespace Slang;
    SlangSession* session = spCreateSession(nullptr);

    TestToolUtil::setSessionDefaultPreludeFromExePath(argv[0], session);
    
    auto stdWriters = StdWriters::initDefaultSingleton();
    
    SlangResult res = innerMain(stdWriters, session, argc, argv);
    spDestroySession(session);

	return (int)TestToolUtil::getReturnCode(res);
}