hotline-rs 0.3.2

A high-performance, hot-reload graphics engine.
Documentation
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
#![allow(clippy::collapsible_if)] 

use crate::gfx::Buffer;
use crate::gfx::PipelineStatistics;
use crate::os;
use crate::gfx;
use crate::primitives;
use crate::image;

use crate::gfx::{ResourceState, RenderPass, CmdBuf, Subresource, QueryHeap, SwapChain, Texture};
use crate::reloader::{ReloadState, Reloader, ReloadResponder};
use serde::{Deserialize, Serialize};

use std::collections::HashMap;
use std::collections::HashSet;
use std::fs;
use std::sync::Arc;
use std::sync::Mutex;
use std::path::{Path, PathBuf};
use std::time::SystemTime;

use maths_rs::prelude::*;

/// Hash type for quick checks of changed resources from pmfx
pub type PmfxHash = u64;

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// To lookup resources in a shader, these are passed to compute shaders:
/// index = srv (read), uav (write)
/// dimension is the resource dimension where 2d textures will be (w, h, 1) and 3d will be (w, h, d)
#[repr(C)]
pub struct ResourceUse {
    pub index: u32,
    pub dimension: Vec3u
} 

/// Everything you need to render a world view; command buffers will be automatically reset and submitted for you.
pub struct View<D: gfx::Device> {
    /// Name of the graph view instance, this is the same as the key that is stored in the pmfx `views` map.
    pub graph_pass_name: String,
    /// Name of the pmfx view, this is the source view (camera, render targets)
    pub pmfx_view_name: String,
    /// Hash of the view name
    pub name_hash: PmfxHash,
    /// Colour hash (for debug markers, derived from name)
    pub colour_hash: u32,
    /// A pre-built render pass: multiple colour targets and depth possible
    pub pass: D::RenderPass,
    /// Pre-calculated viewport based on the output dimensions of the render target adjusted for user data from .pmfx
    pub viewport: gfx::Viewport,
    /// Pre-calculated viewport based on the output dimensions of the render target adjusted for user data from .pmfx
    pub scissor_rect: gfx::ScissorRect,
    /// Dimension of a resource for use when blitting
    pub blit_dimension: Vec2f,
    /// A command buffer ready to be used to buffer draw / render commands
    pub cmd_buf: D::CmdBuf,
    /// Name of camera this view intends to be used with
    pub camera: String,
    /// This is the name of a single pipeline used for all draw calls in the view. supplied in data as `pipelines: ["name"]`
    pub view_pipeline: String,
    // A vector of resource view indices supplied by info inside the `uses` section
    // they will be supplied in order they are specified in the `pmfx` file
    // and may be srv or uav depending on `ResourceUsage`
    pub use_indices: Vec<ResourceUse>,
}
pub type ViewRef<D> = Arc<Mutex<View<D>>>;

/// Equivalent to a `View` this is a graph node which only requires compute
pub struct ComputePass<D: gfx::Device> {
    /// A command buffer ready to be used to buffer compute cmmands
    pub cmd_buf: D::CmdBuf,
    /// The name of a single pipeline used for this compute pass
    pub pass_pipline: String,
    /// Hash of the view name
    pub name_hash: PmfxHash,
    /// Colour hash (for debug markers, derived from name)
    pub colour_hash: u32,
    /// The number of threads specified in the shader
    pub numthreads: gfx::Size3,
    /// We can calulcate this based on resource dimension / thread count
    pub group_count: gfx::Size3,
    // An vector of resource view indices supplied by info inside the `uses` section
    // they will be supplied in order they are specified in the `pmfx` file
    // and may be srv or uav depending on `ResourceUsage`
    pub use_indices: Vec<ResourceUse>
}
pub type ComputePassRef<D> = Arc<Mutex<ComputePass<D>>>;

/// Compact mesh representation referincing and index buffer, vertex buffer and num index count
#[derive(Clone)]
pub struct Mesh<D: gfx::Device> {
    /// Vertex buffer
    pub vb: D::Buffer,
    // Index Buffer
    pub ib: D::Buffer,
    /// Number of indices to draw from the index buffer
    pub num_indices: u32,
    /// Bounding aabb min
    pub aabb_min: Vec3f,
    /// Bounding aabb mix
    pub aabb_max: Vec3f,
}

/// Additional info to wrap with a texture for tracking changes from windwow sizes or other associated bounds
struct TrackedTexture<D: gfx::Device>  {
    /// The texture itself
    texture: D::Texture,
    /// Optional ratio, which will contain window name and scale info if present
    ratio: Option<TextureSizeRatio>,
    /// Tuple of (width, height, depth) to track the current size of the texture and compare for updates
    size: (u64, u64, u32),
    /// Track texture type
    _tex_type: gfx::TextureType,
}

/// Information to track changes to 
struct PmfxTrackingInfo {
    /// Filepath to the data which the pmfx File was deserialised from
    filepath: std::path::PathBuf,
    /// Modified time of the .pmfx file this instance is associated with
    modified_time: SystemTime,
}

// pipelines (name) > permutation (mask : u32) which is tuple (build_hash, pipeline)
type FormatPipelineMap<T> = HashMap<String, HashMap<u32, (PmfxHash, T)>>;

// hash of the view in .0, the view itself in .1 the source view name which was used to generate the instance is stored in .2, 
type TrackedView<D> = (PmfxHash, Arc<Mutex<View<D>>>, String);
type TrackedComputePass<D> = (PmfxHash, Arc<Mutex<ComputePass<D>>>);

/// Pmfx instance,containing render objects and resources
pub struct Pmfx<D: gfx::Device> {
    /// Serialisation structure of a .pmfx file containing render states, pipelines and textures
    pmfx: File,
    /// Tracking info for check on data reloads, grouped by pmfx name
    pmfx_tracking: HashMap<String, PmfxTrackingInfo>,
    /// Folder paths for 
    pmfx_folders: HashMap<String, String>, 
    /// Updated by calling 'update_window' this will cause any tracked textures to check for resizes and rebuild textures if necessary
    window_sizes: HashMap<String, (f32, f32)>,
    /// Nested structure of: format (u64) > FormatPipelineMap
    render_pipelines: HashMap<PmfxHash, FormatPipelineMap<D::RenderPipeline>>,
    /// Compute Pipelines grouped by name then as a tuple (build_hash, pipeline)
    compute_pipelines: HashMap<String, (PmfxHash, D::ComputePipeline)>,
    /// Shaders stored along with their build hash for quick checks if reload is necessary
    shaders: HashMap<String, (PmfxHash, D::Shader)>,
    /// Texture map of tracked texture info
    textures: HashMap<String, (PmfxHash, TrackedTexture<D>)>,
    /// Built views that are used in view function dispatches, the source view name which was used to generate the instnace is stored in .2 for hash checking
    views: HashMap<String, TrackedView<D>>,
    // Built compute passes that contain a command buffer and other compute dispatch info
    compute_passes: HashMap<String, TrackedComputePass<D>>,
    /// Pass timing and GPU pipeline statistics
    pass_stats: HashMap<String, PassStats<D>>,
    /// Map of camera constants that can be retrieved by name for use as push constants
    cameras: HashMap<String, CameraConstants>,
    /// Comtainer to hold world data for use on the GPU
    world_buffers: DynamicWorldBuffers<D>,
    /// Auto-generated barriers to insert between view passes to ensure correct resource states
    barriers: HashMap<String, D::CmdBuf>,
    /// Vector of view names to execute in designated order
    command_queue: Vec<String>,
    /// Tracking texture references of views
    view_texture_refs: HashMap<String, HashSet<String>>,
    /// Container to hold overall GPU stats
    total_stats: TotalStats,
    /// Heaps for shader resource view allocations
    pub shader_heap: D::Heap,
    /// Unit quad mesh for fullscreen passes on the raster pipeline
    pub unit_quad_mesh: Mesh<D>,
    /// Watches for filestamp changes and will trigger callbacks in the `PmfxReloadResponder`
    pub reloader: Reloader,
    /// Errors which occur through render systems can be pushed here for feedback to the user
    pub view_errors: Arc<Mutex<HashMap<String, String>>>,
    /// Tracks the currently active render graph name
    pub active_render_graph: String,
}

/// Contains frame statistics from the GPU for all pmfx jobs
pub struct TotalStats {
    /// Total GPU time spent in milliseconds
    pub gpu_time_ms: f64,
    /// Time of the first submission in seconds
    pub gpu_start: f64,
    /// Time of the final submission in seconds
    pub gpu_end: f64,
    /// Total pipeline statistics 
    pub pipeline_stats: PipelineStatistics
}

impl TotalStats {
    fn new() -> Self {
        Self {
            gpu_time_ms: 0.0,
            gpu_start: 0.0,
            gpu_end: 0.0,
            pipeline_stats: PipelineStatistics::default()
        }
    }
}

/// Resources to track and read back GPU-statistics for individual views
struct PassStats<D: gfx::Device> {
    write_index: usize,
    read_index: usize,
    frame_fence_value: u64,
    fences: Vec<u64>,
    timestamp_heap: D::QueryHeap,
    timestamp_buffers: Vec<[D::Buffer; 2]>,
    pipeline_stats_heap: D::QueryHeap,
    pipeline_stats_buffers: Vec<D::Buffer>,
    pipeline_query_index: usize,
    start_timestamp: f64,
    end_timestamp: f64
}

impl<D> PassStats<D> where D: gfx::Device {
    pub fn new_query_buffer(device: &mut D, elem_size: usize, num_elems: usize) -> D::Buffer {
        device.create_read_back_buffer(elem_size * num_elems).unwrap()
    }
    
    pub fn new(device: &mut D, num_buffers: usize) -> Self {
        let mut timestamp_buffers = Vec::new();
        let mut fences = Vec::new();
        let mut pipeline_stats_buffers = Vec::new();
        let timestamp_size_bytes = D::get_timestamp_size_bytes();
        let pipeline_statistics_size_bytes = D::get_pipeline_statistics_size_bytes();
        for _ in 0..num_buffers {
            timestamp_buffers.push([
                Self::new_query_buffer(device, timestamp_size_bytes, 1),
                Self::new_query_buffer(device, timestamp_size_bytes, 1)
            ]);
            pipeline_stats_buffers.push(
                Self::new_query_buffer(device, pipeline_statistics_size_bytes, 1),
            );
            fences.push(0)
        }
        Self {
            frame_fence_value: 0,
            write_index: 0,
            read_index: 0,
            fences,
            start_timestamp: 0.0,
            end_timestamp: 0.0,
            timestamp_heap: device.create_query_heap(&gfx::QueryHeapInfo {
                heap_type: gfx::QueryType::Timestamp,
                num_queries: 2,
            }),
            timestamp_buffers,
            pipeline_stats_heap: device.create_query_heap(&gfx::QueryHeapInfo {
                heap_type: gfx::QueryType::PipelineStatistics,
                num_queries: 1,
            }),
            pipeline_stats_buffers,
            pipeline_query_index: usize::max_value()
        }
    }
}

/// Serialisation layout for contents inside .pmfx file
#[derive(Serialize, Deserialize)]
struct File {
    shaders: HashMap<String, PmfxHash>,
    pipelines: HashMap<String, PipelinePermutations>,
    depth_stencil_states: HashMap<String, gfx::DepthStencilInfo>,
    raster_states: HashMap<String, gfx::RasterInfo>,
    blend_states: HashMap<String, BlendInfo>,
    render_target_blend_states: HashMap<String, gfx::RenderTargetBlendInfo>,
    textures: HashMap<String, TextureInfo>,
    views: HashMap<String, ViewInfo>,
    render_graphs: HashMap<String, HashMap<String, GraphPassInfo>>,
    dependencies: Vec<String>
}

/// pmfx File serialisation, 
impl File {
    /// creates a new empty pmfx
    fn new() -> Self {
        File {
            shaders: HashMap::new(),
            pipelines: HashMap::new(),
            depth_stencil_states: HashMap::new(),
            raster_states: HashMap::new(),
            blend_states: HashMap::new(),
            render_target_blend_states: HashMap::new(),
            textures: HashMap::new(),
            views: HashMap::new(),
            render_graphs: HashMap::new(),
            dependencies: Vec::new(),
        }
    }
}

/// Data to associate a Texture with a Window so when a window resizes we updat the texture dimensions to window size * scale
#[derive(Serialize, Deserialize, Clone)]
struct TextureSizeRatio {
    /// Window name to track size changes from
    window: String,
    /// Multiply the window dimensions * scale to get the final size of the texture
    scale: f32
}

/// Pmfx texture serialisation layout, this data is emitted from pmfx-shader compiler
#[derive(Serialize, Deserialize)]
struct TextureInfo {
    ratio: Option<TextureSizeRatio>,
    generate_mips: Option<bool>,
    filepath: Option<String>,
    src_data: Option<bool>,
    width: u64,
    height: u64,
    depth: u32,
    mip_levels: u32,
    array_layers: u32,
    samples: u32,
    cubemap: bool,
    format: gfx::Format,
    usage: Vec<ResourceState>,
    hash: u64,
}

/// Pmfx texture serialisation layout, this data is emitted from pmfx-shader compiler
#[derive(Serialize, Deserialize)]
struct BlendInfo {
    alpha_to_coverage_enabled: bool,
    independent_blend_enabled: bool,
    render_target: Vec<String>,
}

/// Pmfx pipeline serialisation layout, this data is emitted from pmfx-shader compiler
#[derive(Serialize, Deserialize, Clone)]
struct Pipeline {
    vs: Option<String>,
    ps: Option<String>,
    cs: Option<String>,
    numthreads: Option<(u32, u32, u32)>,
    vertex_layout: Option<gfx::InputLayout>,
    pipeline_layout: gfx::PipelineLayout,
    depth_stencil_state: Option<String>,
    raster_state: Option<String>,
    blend_state: Option<String>,
    topology: gfx::Topology,
    sample_mask: u32,
    hash: PmfxHash
}
type PipelinePermutations = HashMap<String, Pipeline>;

#[derive(Serialize, Deserialize, Clone)]
struct ViewInfo {
    render_target: Vec<String>,
    depth_stencil: Vec<String>,
    viewport: Vec<f32>,
    scissor: Vec<f32>,
    clear_colour: Option<Vec<f32>>,
    clear_depth: Option<f32>,
    clear_stencil: Option<u8>,
    camera: String,
    hash: PmfxHash
}

/// Resoure uage for a graph pass
#[derive(Serialize, Deserialize, Clone, Debug)]
enum ResourceUsage {
    /// Write to an un-ordeded access resource or rneder target resource
    Write,
    /// Read from the primary (resovled) resource
    Read,
    /// Read from an MSAA resource
    ReadMsaa,
    /// Read from resource and signal we want to read generated mip maps
    ReadMips
}

#[derive(Serialize, Deserialize, Clone)]
struct GraphPassInfo {
    /// For render passes, specifies the view (render target, camera etc
    view: Option<String>,
    /// Pipelines array that will use during this pass
    pipelines: Option<Vec<String>>,
    /// A function to call which can build draw or compute commands
    function: String,
    /// Dependency info for determining execute order
    depends_on: Option<Vec<String>>,
    /// Array of resources we wish to use during this pass, which can be passed to a shader (to know the srv indices)
    uses: Option<Vec<(String, ResourceUsage)>>,
    /// For compute passes the number of threads
    numthreads: Option<(u32, u32, u32)>,
    /// The name of a resource a compute shader wil distrubute work into
    target_dimension: Option<String>,
    /// Signify we want cubemap rendering
    cubemap: Option<bool>
}

/// A GPU buffer type which can resize and stretch like a vector
pub struct DynamicBuffer<D: gfx::Device, T: Sized> {
    len: usize,
    capacity: usize,
    buffers: Option<Vec<D::Buffer>>,
    usage: gfx::BufferUsage,
    bb: usize,
    num_buffers: usize,
    resource_type: std::marker::PhantomData<T>
}

impl<D, T> DynamicBuffer<D, T> where D: gfx::Device, T: Sized {
    /// creates a new empty buffer, you need to `reserve` space afterwards
    pub fn new(usage: gfx::BufferUsage, num_buffers: usize) -> Self {
        DynamicBuffer {
            len: 0,
            capacity: 0,
            buffers: None,
            usage,
            bb: 0,
            num_buffers,
            resource_type: std::marker::PhantomData
        }
    }

    /// Swap buffers once a frame for safe CPU writes an GPU in flight reads
    pub fn swap(&mut self) {
        self.bb = (self.bb + 1) % self.num_buffers
    }

    /// Access the internal buffer to write to this frame, for safe CPU writes an GPU in flight reads
    pub fn mut_buf(&mut self) -> &mut D::Buffer {
        &mut self.buffers.as_mut().unwrap()[self.bb]
    }

    /// Access immutable buffer to us in the current frame
    pub fn buf(&mut self) -> &D::Buffer {
        &self.buffers.as_ref().unwrap()[self.bb]
    }

    /// creates a new buffer if more cpaacity is required
    pub fn reserve(&mut self, device: &mut D, heap: &mut D::Heap, capacity: usize) {
        if capacity > self.capacity {
            let mut inner_buffers = Vec::new();
            for _ in 0..self.num_buffers {
                let buf = device.create_buffer_with_heap(&gfx::BufferInfo{
                    usage: self.usage,
                    cpu_access: gfx::CpuAccessFlags::WRITE | gfx::CpuAccessFlags::PERSISTENTLY_MAPPED,
                    format: gfx::Format::Unknown,
                    stride: std::mem::size_of::<T>(),
                    num_elements: capacity,
                    initial_state: if self.usage.contains(gfx::BufferUsage::CONSTANT_BUFFER) {
                        gfx::ResourceState::VertexConstantBuffer
                    }
                    else {
                        gfx::ResourceState::ShaderResource
                    }
                }, crate::data![], heap).unwrap();
                inner_buffers.push(buf);
            }

            self.capacity = capacity;
            self.buffers = Some(inner_buffers);
        }
    }

    /// Resets length to zero
    pub fn clear(&mut self) {
        self.len = 0
    }

    /// Returns the length of the data written to the buffer in elements
    pub fn len(&self) -> usize {
        self.len
    }

    /// Returns true if the len is 0
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the capacity of the buffer
    pub fn capacity(&self) -> usize {
        self.capacity
    }

    /// Write arbitrary data data to the buffer and update len, it should be either a u8 slice, a single &T or a slice of T
    pub fn write<T2: Sized>(&mut self, offset: usize, data: &[T2]) {
        self.mut_buf().write(
            offset,
            data
        ).unwrap();
        let write_offset = offset + data.len() * std::mem::size_of::<T2>();
        self.len = max(write_offset / std::mem::size_of::<T>(), self.len);
    }

    /// Push an item of type `T` to the dynamic buffer and update the len
    pub fn push(&mut self, item: &T) {
        let write_offset = self.len * std::mem::size_of::<T>();
        self.mut_buf().write(
            write_offset,
            gfx::as_u8_slice(item)
        ).unwrap();
        self.len += 1;
    }

    /// get's the appropriate resource index
    fn get_index(&self) -> usize {
        if let Some(buf) = &self.buffers {
            if self.usage.contains(gfx::BufferUsage::CONSTANT_BUFFER) {
                buf[self.bb].get_cbv_index().unwrap()
            }
            else {
                buf[self.bb].get_srv_index().unwrap()
            }
        }
        else {
            0
        }
    }

    pub fn get_lookup(&self) -> GpuBufferLookup {
        GpuBufferLookup {
            index: self.get_index() as u32,
            count: self.len as u32
        }
    }
}

pub struct DynamicWorldBuffers<D: gfx::Device> {
    /// Structured buffer containing bindless draw call information `DrawData`
    pub draw: DynamicBuffer<D, DrawData>,
    /// Structured buffer containing bindless draw call information `DrawData`
    pub extent: DynamicBuffer<D, ExtentData>,
    // Structured buffer containing `MaterialData`
    pub material: DynamicBuffer<D, MaterialData>,
    // Structured buffer containing `PointLightData`
    pub point_light: DynamicBuffer<D, PointLightData>,
    // Structured buffer containing `SpotLightData`
    pub spot_light: DynamicBuffer<D, SpotLightData>,
    // Structured buffer containing `DirectionalLightData`
    pub directional_light: DynamicBuffer<D, DirectionalLightData>,
    /// Structured buffer for shadow map matrices
    pub shadow_matrix: DynamicBuffer<D, Mat4f>,
    /// Constant buffer containing camera info
    pub camera: DynamicBuffer<D, CameraData>,
}

impl<D> Default for DynamicWorldBuffers<D> where D: gfx::Device {
    fn default() -> Self {
        Self {
            draw: DynamicBuffer::<D, DrawData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            extent: DynamicBuffer::<D, ExtentData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            material: DynamicBuffer::<D, MaterialData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            point_light: DynamicBuffer::<D, PointLightData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            spot_light: DynamicBuffer::<D, SpotLightData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            directional_light: DynamicBuffer::<D, DirectionalLightData>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
            camera: DynamicBuffer::<D, CameraData>::new(gfx::BufferUsage::CONSTANT_BUFFER, 3),
            shadow_matrix: DynamicBuffer::<D, Mat4f>::new(gfx::BufferUsage::SHADER_RESOURCE, 3),
        }
    }
}

/// Information to cerate `WorldBuffers` for rendering
#[derive(Default)]
pub struct WorldBufferReserveInfo {
    pub draw_capacity: usize,
    pub extent_capacity: usize,
    pub material_capacity: usize,
    pub point_light_capacity: usize,
    pub spot_light_capacity: usize,
    pub directional_light_capacity: usize,
    pub camera_capacity: usize,
    pub shadow_matrix_capacity: usize
}

/// GPU friendly structure containing camera view information
#[repr(C)]
#[derive(Clone)]
pub struct CameraConstants {
    pub view_matrix: maths_rs::Mat4f,
    pub view_projection_matrix: maths_rs::Mat4f,
    pub view_position: maths_rs::Vec4f
}

/// GPU friendly struct containing single entity draw data
#[repr(C)]
#[derive(Clone)]
pub struct DrawData {
    /// World matrix for transforming entity
    pub world_matrix: Mat34f, 
}

/// GPU friendly struct containing single entity draw data
#[repr(C)]
#[derive(Clone)]
pub struct ExtentData {
    /// Centre pos of aabb
    pub pos: Vec3f,
    /// Half extent of aabb
    pub extent: Vec3f 
}

/// GPU friendly structure containing lookup id's for bindless materials 
#[repr(C)]
#[derive(Clone)]
pub struct MaterialData {
    pub albedo_id: u32,
    pub normal_id: u32,
    pub roughness_id: u32,
    pub padding: u32
}

/// GPU lookup for shadow map srv and matrix index. to look into textures and shadows matrix in world buffers
#[repr(packed)]
#[derive(Clone, Copy, Default)]
pub struct ShadowMapInfo {
    pub srv_index: u32,
    pub matrix_index: u32
}

/// GPU friendly structure for point lights
#[repr(C)]
#[derive(Clone)]
pub struct PointLightData {
    pub pos: Vec3f,
    pub radius: f32,
    pub colour: Vec4f,
    pub shadow_map_info: ShadowMapInfo
}

/// GPU friendly structure for directional lights
#[repr(C)]
#[derive(Clone)]
pub struct DirectionalLightData {
    pub dir: Vec3f,
    pub colour: Vec4f,
    pub shadow_map_info: ShadowMapInfo
}

/// GPU friendly structure for spot lights
#[repr(C)]
#[derive(Clone)]
pub struct SpotLightData {
    pub pos: Vec3f,
    pub cutoff: f32,
    pub dir: Vec3f,
    pub falloff: f32,
    pub colour: Vec4f,
    pub shadow_map_info: ShadowMapInfo
}

/// GPU friendly structure for cameras
#[repr(C)]
#[derive(Clone)]
pub struct CameraData {
    pub view_projection_matrix: Mat4f,
    pub view_position: Vec4f,
    pub planes: [Vec4f; 6],
}

#[repr(C)]
#[derive(Clone, Default, Debug)]
pub struct GpuBufferLookup {
    /// index of the srv or cbv
    pub index: u32,
    /// number of elements in the buffer
    pub count: u32
}

#[repr(C)]
#[derive(Clone, Default, Debug)]
pub struct WorldBufferInfo {
    /// srv index of the draw buffer (contains entity world matrices and draw call data)
    pub draw: GpuBufferLookup,
    /// srv index of the extent buffer (contains data for culling entities)
    pub extent: GpuBufferLookup,
    /// srv index of the material buffer (contains ids of textures to look up and material parameters)
    pub material: GpuBufferLookup,
    /// srv index of the point light buffer
    pub point_light: GpuBufferLookup,
    /// srv index of the spot light buffer
    pub spot_light: GpuBufferLookup,
    /// srv index of the directional light buffer
    pub directional_light: GpuBufferLookup,
    /// cbv index of the camera
    pub camera: GpuBufferLookup,
    /// srv index of shadow matrices
    pub shadow_matrix: GpuBufferLookup
}

pub fn cubemap_camera_face(face: usize, pos: Vec3f, near: f32, far: f32) -> CameraConstants {
    let at = [
        vec3f(1.0, 0.0, 0.0),   //+x
        vec3f(-1.0, 0.0, 0.0),  //-x
        vec3f(0.0, 1.0, 0.0),   //+y
        vec3f(0.0, -1.0, 0.0),  //-y        
        vec3f(0.0, 0.0, 1.0),   //+z
        vec3f(0.0, 0.0, -1.0)   //-z
    ];

    let right = [
        vec3f(0.0, 0.0, -1.0),
        vec3f(0.0, 0.0, 1.0),
        vec3f(1.0, 0.0, 0.0),
        vec3f(1.0, 0.0, 0.0),
        vec3f(1.0, 0.0, 0.0),
        vec3f(-1.0, 0.0, -0.0)
    ];

    let up = [
        vec3f(0.0, 1.0, 0.0),
        vec3f(0.0, 1.0, 0.0),
        vec3f(0.0, 0.0, -1.0),
        vec3f(0.0, 0.0, 1.0),
        vec3f(0.0, 1.0, 0.0),
        vec3f(0.0, 1.0, 0.0)
    ];

    let view = Mat4f::from((
        Vec4f::from((right[face as usize], pos.x)),
        Vec4f::from((up[face as usize], pos.y)),
        Vec4f::from((at[face as usize], pos.z)),
        Vec4f::new(0.0, 0.0, 0.0, 1.0),
    ));

    let proj = Mat4f::create_perspective_projection_lh_yup(deg_to_rad(90.0), 1.0, near, far);

    let view = view.inverse();
    CameraConstants {
        view_matrix: view,
        view_projection_matrix: proj * view,
        view_position: Vec4f::from((pos, 1.0))
    }
}

/// creates a shader from an option of filename, returning optional shader back
fn create_shader_from_file<D: gfx::Device>(device: &D, folder: &Path, file: Option<String>) -> Result<Option<D::Shader>, super::Error> {
    if let Some(shader) = file {
        let shader_filepath = folder.join(shader);
        let shader_data = fs::read(shader_filepath)?;                
        let shader_info = gfx::ShaderInfo {
            shader_type: gfx::ShaderType::Vertex,
            compile_info: None
        };
        Ok(Some(device.create_shader(&shader_info, &shader_data)?))
    }
    else {
        Ok(None)
    }
}

/// get gfx info from a pmfx state, returning default if it does not exist
fn info_from_state<T: Default + Clone>(name: &Option<String>, map: &HashMap<String, T>) -> Result<T, super::Error> {
    if let Some(name) = &name {
        if map.contains_key(name) {
            Ok(map[name].clone())
        }
        else {
            Err(
                super::Error {
                    msg: format!("hotline::pmfx:: missing render state in pmfx config `{}`", name)
                }
            )
        }
    }
    else {
        Ok(T::default())
    }
}

/// get a gfx::BlendState from the pmfx description which unpacks blend states by name and then
/// array of render target blend states by name
fn blend_info_from_state(
    name: &Option<String>,
    blend_states: &HashMap<String, BlendInfo>,
    render_target_blend_states: &HashMap<String, gfx::RenderTargetBlendInfo>) -> Result<gfx::BlendInfo, super::Error> {
    if let Some(name) = &name {
        if blend_states.contains_key(name) {
            let mut rtinfo = Vec::new();
            for name in &blend_states[name].render_target {
                rtinfo.push(info_from_state(&Some(name.to_string()), render_target_blend_states)?);
            }
            Ok(gfx::BlendInfo {
                alpha_to_coverage_enabled: blend_states[name].alpha_to_coverage_enabled,
                independent_blend_enabled: blend_states[name].independent_blend_enabled,
                render_target: rtinfo
            })
        }
        else {
            Err(
                super::Error {
                    msg: format!("hotline::pmfx:: missing blend state in pmfx config `{}`", name)
                }
            )
        }
    }
    else {
        Ok(gfx::BlendInfo {
            alpha_to_coverage_enabled: false,
            independent_blend_enabled: false,
            render_target: vec![gfx::RenderTargetBlendInfo::default()],
        })
    }
}

/// translate pmfx::TextureInfo to gfx::TextureInfo as pmfx::TextureInfo is slightly better equipped for user enty
fn to_gfx_texture_info(pmfx_texture: &TextureInfo, ratio_size: (u64, u64)) -> gfx::TextureInfo {
    // size from ratio
    let (width, height) = ratio_size;

    // infer texture type from dimensions
    let tex_type = if pmfx_texture.cubemap { 
        gfx::TextureType::TextureCube
    } 
    else if pmfx_texture.depth > 1 {
        gfx::TextureType::Texture3D
    }
    else if height > 1 {
        gfx::TextureType::Texture2D
    }
    else {
        gfx::TextureType::Texture1D
    };

    // derive initial state from usage
    let initial_state = if pmfx_texture.usage.contains(&ResourceState::ShaderResource) {
        ResourceState::ShaderResource
    }
    else if pmfx_texture.usage.contains(&ResourceState::DepthStencil) {
        ResourceState::DepthStencil
    }
    else if pmfx_texture.usage.contains(&ResourceState::RenderTarget) {
        ResourceState::RenderTarget
    }
    else {
        ResourceState::ShaderResource
    };

    // texture type bitflags from vec of enum
    let mut usage = gfx::TextureUsage::NONE;
    for pmfx_usage in &pmfx_texture.usage {
        match pmfx_usage {
            ResourceState::ShaderResource => {
                usage |= gfx::TextureUsage::SHADER_RESOURCE
            }
            ResourceState::UnorderedAccess => {
                usage |= gfx::TextureUsage::UNORDERED_ACCESS
            }
            ResourceState::RenderTarget => {
                usage |= gfx::TextureUsage::RENDER_TARGET
            }
            ResourceState::DepthStencil => {
                usage |= gfx::TextureUsage::DEPTH_STENCIL
            }
            _ => {}
        }
    }

    let mut mip_levels = pmfx_texture.mip_levels;
    if let Some(mips) = pmfx_texture.generate_mips {
        if mips {
            usage |= gfx::TextureUsage::GENERATE_MIP_MAPS;
            mip_levels = gfx::mip_levels_for_dimension(width, height);
        }
    }

    gfx::TextureInfo {
        width,
        height,
        tex_type,
        initial_state,
        usage,
        mip_levels,
        depth: pmfx_texture.depth,
        array_layers: pmfx_texture.array_layers,
        samples: pmfx_texture.samples,
        format: pmfx_texture.format,
    }
}

fn to_gfx_clear_colour(clear_colour: Option<Vec<f32>>) -> Option<gfx::ClearColour> {
    if let Some(col) = clear_colour {
        match col.len() {
            len if len >= 4 => {
                Some( gfx::ClearColour {
                    r: col[0],
                    g: col[1],
                    b: col[2],
                    a: col[3],
                })
            }
            3 => {
                Some( gfx::ClearColour {
                    r: col[0],
                    g: col[1],
                    b: col[2],
                    a: 1.0
                })
            }
            2 => {
                Some( gfx::ClearColour {
                    r: col[0],
                    g: col[1],
                    b: 0.0,
                    a: 1.0
                })
            }
            1 => {
                Some( gfx::ClearColour {
                    r: col[0],
                    g: 0.0,
                    b: 0.0,
                    a: 1.0
                })
            }
            _ => None
        }
    }
    else {
        None
    }
}

fn to_gfx_clear_depth_stencil(clear_depth: Option<f32>, clear_stencil: Option<u8>) -> Option<gfx::ClearDepthStencil> {
    if clear_depth.is_some() || clear_stencil.is_some() {
        Some( gfx::ClearDepthStencil {
            depth: clear_depth,
            stencil: clear_stencil
        })
    }
    else {
        None
    }
}

impl<D> Pmfx<D> where D: gfx::Device {
    /// Create a new empty pmfx instance
    pub fn create(device: &mut D, shader_heap_size: usize) -> Self {
        // create a heap which pmfx can manage itself
        let shader_heap = device.create_heap(&gfx::HeapInfo {
            heap_type: gfx::HeapType::Shader,
            num_descriptors: shader_heap_size,
        });
        Pmfx {
            pmfx: File::new(),
            pmfx_tracking: HashMap::new(),
            pmfx_folders: HashMap::new(),
            render_pipelines: HashMap::new(),
            compute_pipelines: HashMap::new(),
            shaders: HashMap::new(),
            textures: HashMap::new(),
            views: HashMap::new(),
            compute_passes: HashMap::new(),
            pass_stats: HashMap::new(),
            cameras: HashMap::new(),
            barriers: HashMap::new(),
            command_queue: Vec::new(),
            view_texture_refs: HashMap::new(),
            window_sizes: HashMap::new(),
            active_render_graph: String::new(),
            reloader: Reloader::create(Box::new(PmfxReloadResponder::new())),
            world_buffers: DynamicWorldBuffers::default(),
            shader_heap,
            unit_quad_mesh: primitives::create_unit_quad_mesh(device),
            total_stats: TotalStats::new(),
            view_errors: Arc::new(Mutex::new(HashMap::new())),
        }
    }

    /// Resizes the set of world buffers used for rendering, this assumes that the buffers will be populated each frame
    /// creates new buffers if the requested count exceeds the capacity.
    pub fn reserve_world_buffers(&mut self, device: &mut D, info: WorldBufferReserveInfo) {
        self.world_buffers.draw.reserve(device, &mut self.shader_heap, info.draw_capacity);
        self.world_buffers.extent.reserve(device, &mut self.shader_heap, info.extent_capacity);
        self.world_buffers.material.reserve(device, &mut self.shader_heap, info.material_capacity);
        self.world_buffers.point_light.reserve(device, &mut self.shader_heap, info.point_light_capacity);
        self.world_buffers.spot_light.reserve(device, &mut self.shader_heap, info.spot_light_capacity);
        self.world_buffers.directional_light.reserve(device, &mut self.shader_heap, info.directional_light_capacity);
        self.world_buffers.camera.reserve(device, &mut self.shader_heap, info.camera_capacity);
        self.world_buffers.shadow_matrix.reserve(device, &mut self.shader_heap, info.shadow_matrix_capacity);
    }

    /// Returns a mutable refernce to the the world buffers, these are persistently mapped GPU buffers which can be
    /// updated using the `.write` function
    pub fn get_world_buffers_mut(&mut self) -> &mut DynamicWorldBuffers<D> {
        &mut self.world_buffers
    }

    /// Retunrs a `WorldBufferInfo` that contains the serv index and count of the various world buffers used
    /// during rendering
    pub fn get_world_buffer_info(&self) -> WorldBufferInfo {
        // construct on the fly
        WorldBufferInfo {
            draw: self.world_buffers.draw.get_lookup(),
            extent: self.world_buffers.extent.get_lookup(),
            material: self.world_buffers.material.get_lookup(),
            point_light: self.world_buffers.point_light.get_lookup(),
            spot_light: self.world_buffers.spot_light.get_lookup(),
            directional_light: self.world_buffers.directional_light.get_lookup(),
            camera: self.world_buffers.camera.get_lookup(),
            shadow_matrix: self.world_buffers.shadow_matrix.get_lookup()
        }
    }

    /// Load a pmfx from a folder, where the folder contains a pmfx info.json and shader binaries in separate files within the directory
    /// You can load multiple pmfx files which will be merged together, shaders are grouped by pmfx_name/ps_main.psc
    /// Render graphs and pipleines must have unique names, if multiple pmfx name a pipeline the same name  
    pub fn load(&mut self, filepath: &str) -> Result<(), super::Error> {        
        // get the name for indexing by pmfx name/folder
        let folder = Path::new(filepath);
        let pmfx_name = if let Some(name) = folder.file_name() {
            String::from(name.to_os_string().to_str().unwrap())
        }
        else {
            String::from(filepath)
        };

        // check if we are already loaded
        if let std::collections::hash_map::Entry::Vacant(e) = self.pmfx_tracking.entry(pmfx_name.to_string()) {
             println!("hotline_rs::pmfx:: loading: {}", pmfx_name);
             //  deserialise pmfx pipelines from file
             let info_filepath = folder.join(format!("{}.json", pmfx_name));
             let pmfx_data = fs::read(&info_filepath)?;
             let file : File = serde_json::from_slice(&pmfx_data)?;
 
             // create tracking info to check if the pmfx has been rebuilt
             let file_metadata = fs::metadata(&info_filepath)?;
             e.insert(PmfxTrackingInfo {
                 modified_time: file_metadata.modified()?,
                 filepath: info_filepath
             });
 
             // add files from pmfx for tracking
             for dep in &file.dependencies {
                 self.reloader.add_file(dep);
             }
 
             // merge into pmfx
             self.merge_pmfx(file, filepath);
         }

        Ok(())
    }

    /// Merges the pmfx file `other` in the current `Pmfx` instance
    fn merge_pmfx(&mut self, other: File, other_filepath: &str) {
        // prepend the pmfx name to the shaders so we can avoid collisions
        for name in other.pipelines.keys() {
            if !self.pmfx_folders.contains_key(name) {
                // insert lookup path for shaders as they go into a folder: pmfx/shaders.vsc
                self.pmfx_folders.insert(name.to_string(), String::from(other_filepath));
            }
        }
        // extend the maps
        self.pmfx.shaders.extend(other.shaders);
        self.pmfx.pipelines.extend(other.pipelines);
        self.pmfx.depth_stencil_states.extend(other.depth_stencil_states);
        self.pmfx.raster_states.extend(other.raster_states);
        self.pmfx.blend_states.extend(other.blend_states);
        self.pmfx.render_target_blend_states.extend(other.render_target_blend_states);
        self.pmfx.textures.extend(other.textures);
        self.pmfx.views.extend(other.views);
        self.pmfx.render_graphs.extend(other.render_graphs);
        self.pmfx.dependencies.extend(other.dependencies);
    }

    /// Removes items from currently loaded maps (states, pipelines etc), if they do not exist in expected keys (in data).
    /*
    fn remove_stale_from_map<T, U>(loaded_map: &mut HashMap<String, T>, expected_keys: &HashMap<String, U>) {
        let keys = loaded_map.keys().map(|s| s.to_string()).collect::<Vec<String>>();
        for item in &keys {
            if !expected_keys.contains_key(item) {
                loaded_map.remove(item);
            }
        }
    }

    /// Removes stale states, views and pipelines which no longer exist in data
    fn remove_stale(&mut self) {
        Self::remove_stale_from_map(&mut self.views, &self.pmfx.views);
        Self::remove_stale_from_map(&mut self.view_stats, &self.pmfx.views);
        Self::remove_stale_from_map(&mut self.textures, &self.pmfx.textures);
        Self::remove_stale_from_map(&mut self.shaders, &self.pmfx.shaders);
        Self::remove_stale_from_map(&mut self.compute_pipelines, &self.pmfx.pipelines);

        // render pipelines are a bit more compliated beause of pass formats
        let formats = self.render_pipelines.keys().map(|h| *h).collect::<Vec<PmfxHash>>();
        for format in &formats {
            if let Some(pipelines) = self.render_pipelines.get_mut(format) {
                Self::remove_stale_from_map(pipelines, &self.pmfx.pipelines);
            }
        }
    }
    */

    /// Internal utility which will create a shader from file or `None` if no file is passed, or the shader does not exist
    fn create_shader(&mut self, device: &D, folder: &Path, file: &Option<String>) -> Result<(), super::Error> {
        let folder = folder.parent().unwrap();
        if let Some(file) = file {
            if !self.shaders.contains_key(file) {
                println!("hotline_rs::pmfx:: compiling shader: {}", file);
                let shader = create_shader_from_file(device, folder, Some(file.to_string()))?;
                if let Some(shader) = shader {
                    println!("hotline_rs::pmfx:: success: {}", file);
                    let hash = self.pmfx.shaders.get(file).unwrap();
                    self.shaders.insert(file.to_string(), (*hash, shader));
                    Ok(())
                }
                else {
                    Ok(())
                }
            }
            else {
                Ok(())
            }
        }
        else {
            Ok(())
        }
    }

    /// Returns a shader reference for use when building pso
    pub fn get_shader<'stack>(&'stack self, file: &Option<String>) -> Option<&'stack D::Shader> {
        if let Some(file) = file {
            if self.shaders.contains_key(file) {
                Some(&self.shaders[file].1)
            }
            else {
                None
            }
        }
        else {
            None
        }
    }

    /// expands width and height for a texture account for ratio scaling linked to windows, pass the info.width / height
    /// of we have no ratio specified
    fn get_texture_size_from_ratio(&self, pmfx_texture: &TextureInfo) -> Result<(u64, u64), super::Error> {
        if let Some(ratio) = &pmfx_texture.ratio {
            if self.window_sizes.contains_key(&ratio.window) {
                let size = self.window_sizes[&ratio.window];
                let samples = pmfx_texture.samples as f32;
                // clamp to samples x samples so if we want 0 size we still have a valid texture
                let size = (max(size.0, samples), max(size.1, samples));
                Ok(((size.0 * ratio.scale) as u64, (size.1 * ratio.scale) as u64))
            }
            else {
                Err(super::Error {
                    msg: format!("hotline_rs::pmfx:: could not find window for ratio: {}", ratio.window),
                })
            }
        }
        else {
            Ok((pmfx_texture.width, pmfx_texture.height))
        }
    }

    /// Creates a texture if it has not already been created from information specified in .pmfx file
    pub fn create_texture(&mut self, device: &mut D, texture_name: &str) -> Result<(), super::Error> {
        if !self.textures.contains_key(texture_name) && self.pmfx.textures.contains_key(texture_name) {
            // create texture from info specified in .pmfx file
            println!("hotline_rs::pmfx:: creating texture: {}", texture_name);
            let pmfx_tex = &self.pmfx.textures[texture_name];

            let (tex, size, tex_type) = if let Some(filepath) = &pmfx_tex.filepath {
                // load texture from file
                let data_path = if let Some(src_data) = pmfx_tex.src_data {
                    if src_data {
                        super::get_src_data_path(filepath)
                    }
                    else {
                        super::get_data_path(filepath)
                    }
                }
                else {
                    super::get_data_path(filepath)
                };

                let img = image::load_from_file(&data_path)?;
                (device.create_texture_with_heaps::<u8>(
                    &img.info,
                    gfx::TextureHeapInfo {
                        shader: Some(&mut self.shader_heap),
                        ..Default::default()
                    },
                    super::data![&img.data]
                )?, (img.info.width, img.info.height), gfx::TextureType::Texture2D)
            }
            else {
                // create a new empty texture
                let size = self.get_texture_size_from_ratio(pmfx_tex)?;
                let gfx_info = to_gfx_texture_info(pmfx_tex, size);
                (device.create_texture_with_heaps::<u8>(
                    &gfx_info,
                    gfx::TextureHeapInfo {
                        shader: Some(&mut self.shader_heap),
                        ..Default::default()
                    },
                    None)?, size, gfx_info.tex_type)
            };

            self.textures.insert(texture_name.to_string(), (pmfx_tex.hash, TrackedTexture {
                texture: tex,
                ratio: self.pmfx.textures[texture_name].ratio.clone(),
                size: (size.0, size.1, pmfx_tex.depth),
                _tex_type: tex_type
            }));
        }
        Ok(())
    }

    /// Returns a texture reference if the texture exists or none otherwise
    pub fn get_texture<'stack>(&'stack self, texture_name: &str) -> Option<&'stack D::Texture> {
        if self.textures.contains_key(texture_name) {
            Some(&self.textures[texture_name].1.texture)
        }
        else {
            None
        }
    }

    /// Returns the tuple (width, height) of a texture
    pub fn get_texture_2d_size(&self, texture_name: &str) -> Option<(u64, u64)> {
        if self.textures.contains_key(texture_name) {
            let size = self.textures[texture_name].1.size;
            Some((size.0, size.1))
        }
        else {
            None
        }
    }

    /// Returns the tuple (width, height, depth) of a texture
    pub fn get_texture_3d_size(&self, texture_name: &str) -> Option<(u64, u64, u32)> {
        if self.textures.contains_key(texture_name) {
            Some(self.textures[texture_name].1.size)
        }
        else {
            None
        }
    }

    /// Return 2d or 3d texture dimension where applicable with 1 default in unused dimension and zero if the texture is not found
    pub fn get_texture_dimension(&self, texture_name: &str) -> Vec3u {
        if self.textures.contains_key(texture_name) {
            let size = self.textures[texture_name].1.size;
            Vec3u::new(size.0 as u32, size.1 as u32, size.2)
        }
        else {
            Vec3u::zero()
        }
    }

    /// Retruns a vector of resource use indices specified in `pmfx` pass and based on `ResourceUsage`
    /// creates resources that do not yet exist
    fn get_resource_use_indices(&mut self, device: &mut D, info: &GraphPassInfo) -> Result<Vec<ResourceUse>, super::Error> {
        // create textures we may use        
        let mut use_indices = Vec::new();
        if let Some(uses) = &info.uses {
            for (resource, usage) in uses {
                self.create_texture(device, resource)?;

                let tex = if let Some(tex) = self.get_texture(resource) {
                    tex
                }
                else {
                    return Err(super::Error{
                        msg: format!("missing texture: {} with usage: {:?}", resource, usage)
                    });
                };

                let index = match usage {
                    ResourceUsage::Write => {
                        if let Some(uav) = tex.get_uav_index() {
                            uav
                        }
                        else {
                            return Err(super::Error{
                                msg: format!("error: texture: {} was not created with TextureUsage::UNORDERED_ACCESS for usage: {:?}", resource, usage)
                            });
                        }
                    }
                    ResourceUsage::Read | ResourceUsage::ReadMips => {
                        if let Some(srv) = tex.get_srv_index() {
                            srv
                        }
                        else {
                            return Err(super::Error{
                                msg: format!("error: texture: {} was not created with TextureUsage::SHADER_RESOURCE for usage: {:?}", resource, usage)
                            });
                        }
                    }
                    ResourceUsage::ReadMsaa => {
                        if let Some(srv) = tex.get_msaa_srv_index() {
                            srv
                        }
                        else {
                            return Err(super::Error{
                                msg: format!("error: texture: {} was not created samples > 1 for usage: {:?}", resource, usage)
                            });
                        }
                    }
                };
                use_indices.push(ResourceUse {
                    index: index as u32,
                    dimension: self.get_texture_dimension(resource)
                });
            }
        }
        Ok(use_indices)
    }

    fn create_view_pass_inner(
        &mut self, device: 
        &mut D, view_name: &str, 
        graph_pass_name: &str, 
        info: &GraphPassInfo,
        pmfx_view: &ViewInfo,
        array_slice: usize,
        cubemap: bool
    ) -> Result<(), super::Error> {
        
        // make a custom name for multi pass
        let graph_pass_multi_name = if array_slice > 0 {
            format!("{}_{}", graph_pass_name, array_slice)
        }
        else {
            graph_pass_name.to_string()
        };

        // array of targets by name
        let mut size = (0, 0);
        let mut render_targets = Vec::new();
        for name in &pmfx_view.render_target {
            render_targets.push(self.get_texture(name).unwrap());
            size = self.get_texture_2d_size(name).unwrap();
        }

        // get depth stencil by name
        let depth_stencil = if !pmfx_view.depth_stencil.is_empty() {
            let name = &pmfx_view.depth_stencil[0];
            size = self.get_texture_2d_size(name).unwrap();
            Some(self.get_texture(name).unwrap())
        }
        else {
            None
        };

        // pass for render targets with depth stencil
        let render_target_pass = device
        .create_render_pass(&gfx::RenderPassInfo {
            render_targets: render_targets.to_vec(),
            rt_clear: to_gfx_clear_colour(pmfx_view.clear_colour.clone()),
            depth_stencil,
            ds_clear: to_gfx_clear_depth_stencil(pmfx_view.clear_depth, pmfx_view.clear_stencil),
            resolve: false,
            discard: false,
            array_slice: array_slice
        })?;

        // assing a view pipleine (if we supply 1 pipeline) for all draw calls in the view, otherwise leave it emptu
        let view_pipeline = if let Some(pipelines) = &info.pipelines {
            if pipelines.len() == 1 {
                pipelines[0].to_string()
            }
            else {
                String::new()
            }
        }
        else {
            String::new()
        };

        // hashes
        let mut hash = DefaultHasher::new();
        graph_pass_multi_name.hash(&mut hash);
        let name_hash : PmfxHash = hash.finish();

        // colour hash
        let mut hash = DefaultHasher::new();
        view_name.hash(&mut hash);
        let colour_hash : u32 = hash.finish() as u32 | 0xff000000;

        // validate viewport f32 count
        if pmfx_view.viewport.len() != 6 {
            return Err(super::Error {
                msg: format!("hotline_rs::pmfx:: viewport expects array of 6 floats, found {}", pmfx_view.viewport.len())
            });
        }

        // validate scissor f32 count
        if pmfx_view.scissor.len() != 4 {
            return Err(super::Error {
                msg: format!("hotline_rs::pmfx:: scissor expects array of 4 floats, found {}", pmfx_view.viewport.len())
            });
        }

        //
        let use_indices = self.get_resource_use_indices(device, info)?;

        // get blit dimension, tbh this should probably be a rect
        let target_size = if let Some(target) = &info.target_dimension {
            if let Some(dim) = self.get_texture_2d_size(target) {
                dim
            }
            else {
                (0, 0)
            }
        }
        else {
            (0, 0)
        };

        let camera_name = if cubemap {
            format!("{}_{}", pmfx_view.camera.to_string(), array_slice)
        }
        else {
            pmfx_view.camera.to_string()
        };

        let view = View::<D> {
            graph_pass_name: graph_pass_multi_name.to_string(),
            pmfx_view_name: view_name.to_string(),
            name_hash,
            colour_hash,
            pass: render_target_pass,
            viewport: gfx::Viewport {
                x: size.0 as f32 * pmfx_view.viewport[0],
                y: size.1 as f32 * pmfx_view.viewport[1],
                width: size.0 as f32 * pmfx_view.viewport[2],
                height: size.1 as f32 * pmfx_view.viewport[3],
                min_depth: pmfx_view.viewport[4],
                max_depth: pmfx_view.viewport[5],
            },
            scissor_rect: gfx::ScissorRect {
                left: (size.0 as f32 * pmfx_view.scissor[0]) as i32,
                top: (size.1 as f32 * pmfx_view.scissor[1]) as i32,
                right: (size.0 as f32 * pmfx_view.scissor[2]) as i32,
                bottom: (size.1 as f32 * pmfx_view.scissor[3]) as i32
            },
            cmd_buf: device.create_cmd_buf(2),
            camera: camera_name,
            view_pipeline,
            use_indices,
            blit_dimension: Vec2f::from((target_size.0 as f32, target_size.1 as f32))
        };

        self.views.insert(graph_pass_multi_name.to_string(), 
            (pmfx_view.hash, Arc::new(Mutex::new(view)), view_name.to_string()));

        // create stats
        self.pass_stats.insert(graph_pass_multi_name.to_string(), PassStats::new(device, 2));

        Ok(())
    }   

    /// Create a view pass from information specified in pmfx file
    fn create_view_pass(&mut self, device: &mut D, view_name: &str, graph_pass_name: &str, info: &GraphPassInfo) -> Result<(), super::Error> {
        if !self.views.contains_key(graph_pass_name) && self.pmfx.views.contains_key(view_name) {

            println!("hotline_rs::pmfx:: creating graph view: {} for {}", graph_pass_name, view_name);

            // create pass from targets
            let pmfx_view = self.pmfx.views[view_name].clone();

            // create textures for view 
            let mut cubemap = false;
            for name in &pmfx_view.render_target {
                self.create_texture(device, name)?;
                self.view_texture_refs.entry(name.to_string())
                    .or_insert(HashSet::new()).insert(graph_pass_name.to_string());

                if self.pmfx.textures[name].cubemap {
                    cubemap = true;
                }
            }

            // create textures for depth stencils
            for name in &pmfx_view.depth_stencil {
                self.create_texture(device, name)?;
                self.view_texture_refs.entry(name.to_string())
                .or_insert(HashSet::new()).insert(graph_pass_name.to_string());

                if self.pmfx.textures[name].cubemap {
                    cubemap = true;
                }
            }

            let mut pass_count = 1;
            if cubemap {
                pass_count = 6;
            }

            for i in 0..pass_count {
                self.create_view_pass_inner(
                    device, view_name, graph_pass_name, info, &pmfx_view, i, cubemap)?;
            }
        }

        Ok(())
    }

    /// Return a reference to a view if the view exists or error otherwise
    pub fn get_view(&self, view_name: &str) -> Result<ViewRef<D>, super::Error> {
        if self.views.contains_key(view_name) {
            Ok(self.views[view_name].1.clone())
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: view: {} not found", view_name)
            })
        }
    }

    /// Create a compute pass from info specified in the pmfx file
    fn create_compute_pass(&mut self, device: &mut D, graph_pass_name: &str, info: &GraphPassInfo) -> Result<(), super::Error> {
        let pass_pipeline = if let Some(pipelines) = &info.pipelines {
            if pipelines.len() == 1 {
                pipelines[0].to_string()
            }
            else {
                String::new()
            }
        }
        else {
            String::new()
        };

        // create textures we may use
        let use_indices = self.get_resource_use_indices(device, info)?;

        // get the target dimension
        let target_size = if let Some(target) = &info.target_dimension {
            if let Some(dim) = self.get_texture_3d_size(target) {
                dim
            }
            else if let Some(dim) = self.get_texture_2d_size(target) {
                (dim.0, dim.1, 1)
            }
            else {
                (1, 1, 1)
            }
        }
        else {
            (1, 1, 1)
        };

        // get the thread count
        let numthreads = if let Some(threads) = info.numthreads {
            threads
        }
        else {
            let pipeline = self.pmfx.pipelines.get(&pass_pipeline).unwrap();
            if let Some(num_threads) = pipeline["0"].numthreads {
                num_threads
            }
            else {
                (1, 1, 1)
            }
        };

        // hashes
        let mut hash = DefaultHasher::new();
        graph_pass_name.hash(&mut hash);
        let name_hash : PmfxHash = hash.finish();

        // colour hash
        let mut hash = DefaultHasher::new();
        graph_pass_name.hash(&mut hash);
        let colour_hash : u32 = hash.finish() as u32 | 0xff000000;
        
        let pass = ComputePass {
            cmd_buf: device.create_cmd_buf(2),
            pass_pipline: pass_pipeline,
            name_hash,
            colour_hash,
            group_count: gfx::Size3 {
                x: ceil(target_size.0 as f32 / numthreads.0 as f32) as u32,
                y: ceil(target_size.1 as f32 / numthreads.1 as f32) as u32,
                z: ceil(target_size.2 as f32 / numthreads.2 as f32) as u32,
            },
            numthreads: gfx::Size3 {
                x: numthreads.0,
                y: numthreads.1,
                z: numthreads.2,
            },
            use_indices
        };

        self.compute_passes.insert(
            graph_pass_name.to_string(),
            (0, Arc::new(Mutex::new(pass)))
        );

        // create stats
        self.pass_stats.insert(graph_pass_name.to_string(), PassStats::new(device, 2));

        Ok(())
    }

    /// Return a reference to a compute pass if the pass exists or error otherwise
    pub fn get_compute_pass(&self, pass_name: &str) -> Result<ComputePassRef<D>, super::Error> {
        if self.compute_passes.contains_key(pass_name) {
            Ok(self.compute_passes[pass_name].1.clone())
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: compute_pass: {} not found", pass_name)
            })
        }
    }

    /// Create all views required for a render graph if necessary, skip if a view already exists
    pub fn create_render_graph_views(&mut self, device: &mut D, graph_name: &str) -> Result<(), super::Error> {
        // create views for all of the nodes
        if self.pmfx.render_graphs.contains_key(graph_name) {
            let pmfx_graph = self.pmfx.render_graphs[graph_name].clone();
            for (graph_pass_name, node) in &pmfx_graph {
                if let Some(view) = &node.view {
                    // create view pass for view node
                    self.create_view_pass(device, view, graph_pass_name, node)?;
                }
                else {
                    // create compute pass for compute node
                    self.create_compute_pass(device, graph_pass_name, node)?;
                }
            }
        }
        Ok(())
    }

    pub fn generate_mip_maps(
        &mut self,
        device: &mut D,
        texture_name: &str) -> Result<(), super::Error> {
        if let Some(tex) = self.get_texture(texture_name) {
            let mut cmd_buf = device.create_cmd_buf(1);
            let barrier_name = format!("barrier_generate_mip_maps-{}", texture_name);
            cmd_buf.begin_event(0xffdc789a, &format!("generate_mip_maps: {}", &texture_name));
            cmd_buf.generate_mip_maps(tex, device, &self.shader_heap)?;
            cmd_buf.end_event();
            cmd_buf.close()?;
            self.barriers.insert(barrier_name.to_string(), cmd_buf);
            // add barrier placeholder in the command_queue
            self.command_queue.push(barrier_name);
        }
        Ok(())
    }

    fn create_resolve_transition(
        &mut self,
        device: &mut D,
        texture_barriers: &mut HashMap<String, ResourceState>, 
        view_name: &str, 
        texture_name: &str, 
        target_state: ResourceState) -> Result<(), super::Error> {
        if texture_barriers.contains_key(texture_name) {
            let state = texture_barriers[texture_name];
            let barrier_name = format!("barrier_resolve-{}-{} ({:?})", view_name, texture_name, target_state);
            if let Some(tex) = self.get_texture(texture_name) {
                // prevent resolving non msaa surfaces
                if !tex.is_resolvable() {
                    return Err(super::Error {
                        msg: format!("hotline_rs::pmfx:: texture: {} is not resolvable", texture_name),
                    });
                }

                // transition main resource into resolve src
                let mut cmd_buf = device.create_cmd_buf(1);
                cmd_buf.begin_event(0xffdc789a, &format!("resolve: {}", &texture_name));

                cmd_buf.transition_barrier(&gfx::TransitionBarrier {
                    texture: Some(self.get_texture(texture_name).unwrap()),
                    buffer: None,
                    state_before: state,
                    state_after: ResourceState::ResolveSrc,
                });

                // transition resolve resource into resolve dst
                cmd_buf.transition_barrier_subresource(&gfx::TransitionBarrier {
                        texture: Some(self.get_texture(texture_name).unwrap()),
                        buffer: None,
                        state_before: target_state,
                        state_after: ResourceState::ResolveDst,
                    },
                    Subresource::ResolveResource
                );
                
                // perform the resolve
                cmd_buf.resolve_texture_subresource(tex, 0)?;

                // transition the resolve to shader resource for sampling
                cmd_buf.transition_barrier_subresource(&gfx::TransitionBarrier {
                        texture: Some(self.get_texture(texture_name).unwrap()),
                        buffer: None,
                        state_before: ResourceState::ResolveDst,
                        state_after: target_state,
                    },
                    Subresource::ResolveResource
                );

                cmd_buf.end_event();

                // insert barrier
                cmd_buf.close()?;
                self.barriers.insert(barrier_name.to_string(), cmd_buf);

                // update track state
                texture_barriers.remove(texture_name);
                texture_barriers.insert(texture_name.to_string(), ResourceState::ResolveSrc);
            }

            // add barrier placeholder in the command_queue
            self.command_queue.push(barrier_name);
        }
        Ok(())
    }

    fn create_texture_transition_barrier(
        &mut self,
        device: &mut D,
        texture_barriers: &mut HashMap<String, ResourceState>, 
        view_name: &str, 
        texture_name: &str, 
        target_state: ResourceState) -> Result<(), super::Error> {
        if texture_barriers.contains_key(texture_name) {
            let state = texture_barriers[texture_name];
            if state != target_state {
                // add barrier placeholder in the command_queue
                let barrier_name = format!("barrier_{}-{} ({:?})", view_name, texture_name, target_state);
                self.command_queue.push(barrier_name.to_string());          

                // create a command buffer
                let mut cmd_buf = device.create_cmd_buf(1);
                cmd_buf.begin_event(
                    0xfff1b023, 
                    &format!("transition_barrier: {} ({} -> {})", &texture_name, state, target_state)
                );
                cmd_buf.transition_barrier(&gfx::TransitionBarrier {
                    texture: Some(self.get_texture(texture_name).unwrap()),
                    buffer: None,
                    state_before: state,
                    state_after: target_state,
                });
                cmd_buf.end_event();
                cmd_buf.close()?;
                self.barriers.insert(barrier_name, cmd_buf);
    
                // update track state
                texture_barriers.remove(texture_name);
                texture_barriers.insert(texture_name.to_string(), target_state);
            }
        }
        Ok(())
    }

    /// Unloads all views, so that a subsequent call to `create_render_graph` wiill build from clean
    /// make sure call this after `SwapChain::wait_for_last_fame()` so any dropped resources will
    /// not be in use on the GPU
    pub fn unload_views(&mut self) {
        self.views.clear();
    }

    /// Create a render graph wih automatic resource barrier generation from info specified insie .pmfx file
    pub fn create_render_graph(&mut self, device: &mut D, graph_name: &str) -> Result<(), super::Error> {        
        // go through the graph sequentially, as the command lists are executed in order but generated 
        if self.pmfx.render_graphs.contains_key(graph_name) {

            // create views for any nodes in the graph
            self.create_render_graph_views(device, graph_name)?;

            // currently we just have 1 single execute graph and barrier set
            self.barriers.clear();
            self.command_queue.clear();

            let mut barriers = self.pmfx.textures.iter().filter(|tex|{
                tex.1.usage.contains(&ResourceState::ShaderResource) || 
                tex.1.usage.contains(&ResourceState::RenderTarget) ||
                tex.1.usage.contains(&ResourceState::DepthStencil)
            }).map(|tex|{
              (tex.0.to_string(), ResourceState::ShaderResource)  
            }).collect::<HashMap<String, ResourceState>>();

            // loop over the graph multiple times adding views in depends on order, until we add all the views
            let mut to_add = self.pmfx.render_graphs[graph_name].len();
           
            let mut added = 0;
            let mut dependencies = HashSet::new();
            while added < to_add {
                let pmfx_graph = self.pmfx.render_graphs[graph_name].clone();
                for (graph_pass_name, instance) in &pmfx_graph {

                    if let Some(view) = &instance.view {
                        // allow missing views to be safely handled
                        if !self.pmfx.views.contains_key(view) {
                            println!("hotline_rs::pmfx:: [warning] missing view {}", view);
                            to_add -= 1;
                            continue;
                        }
                    }
    
                    // already added this pass
                    if dependencies.contains(graph_pass_name) {
                        continue;
                    }
    
                    // wait for dependencies
                    if let Some(depends_on) = &instance.depends_on {
                        let mut passes = false;
                        if !depends_on.is_empty() {
                            for d in depends_on {
                                if !pmfx_graph.contains_key(d) {
                                    passes = true;
                                    println!("hotline_rs::pmfx:: [warning] graph pass {} missing dependency {}. ignoring", 
                                        graph_pass_name, d);
                                }
                                else if dependencies.contains(d) {
                                    passes = true;
                                }
                                else {
                                    passes = false;
                                }
                            }
                        }

                        if !passes {
                            continue;
                        }
                    }

                    if let Some(uses) = &instance.uses {
                        // check resource uses
                        for u in uses {
                            let mut resolve = false;
                            let mut gen_mips = false;

                            let resolvable = if let Some(tex) = self.get_texture(&u.0) {
                                if tex.is_resolvable() {
                                    true
                                }
                                else {
                                    false
                                }
                            }
                            else {
                                false
                            };

                            let res_state = match u.1 {
                                ResourceUsage::Write => {
                                    ResourceState::UnorderedAccess
                                },
                                ResourceUsage::Read => {
                                    resolve = resolvable;
                                    ResourceState::ShaderResource
                                },
                                ResourceUsage::ReadMips => {
                                    resolve = resolvable;
                                    gen_mips = true;
                                    ResourceState::ShaderResource
                                },
                                ResourceUsage::ReadMsaa => {
                                    ResourceState::ShaderResource
                                },
                            };

                            // resolve and generate mips
                            if resolve {
                                self.create_resolve_transition(
                                    device, 
                                    &mut barriers, 
                                    &graph_pass_name, 
                                    &u.0,
                                    ResourceState::ShaderResource,
                                )?;
                            }
                            
                            // generate mips on non msaa resources
                            if gen_mips {
                                // generate_mip_maps mips expects us to be in ShaderResource state
                                self.create_texture_transition_barrier(
                                    device, 
                                    &mut barriers, 
                                    &graph_pass_name, 
                                    &u.0,
                                    ResourceState::ShaderResource)?;

                                self.generate_mip_maps(device, &u.0)?;

                                // generate_mip_maps transitions to ShaderResource
                                *barriers.get_mut(&u.0).unwrap() = ResourceState::ShaderResource;
                            }

                            // transition to target state
                            self.create_texture_transition_barrier(
                                device, 
                                &mut barriers, 
                                &graph_pass_name, 
                                &u.0,
                                res_state)?;
                        }
                    }
                    
                    if let Some(view) = &instance.view {
                        // create transitions by inspecting view info
                        let pmfx_view = self.pmfx.views[view].clone();
        
                        // if we need to write to a target we must make sure it is transitioned into render target state
                        for rt_name in pmfx_view.render_target {
                            self.create_texture_transition_barrier(
                                device, &mut barriers, view, &rt_name, ResourceState::RenderTarget)?;
        
                        }
        
                        // same for depth stencils
                        for ds_name in pmfx_view.depth_stencil {
                            self.create_texture_transition_barrier(
                                device, &mut barriers, view, &ds_name, ResourceState::DepthStencil)?;
        
                        }

                        // create pipelines requested for this view instance with the pass format
                        if let Some(pipelines) = &instance.pipelines {
                            for pipeline in pipelines {
                                let view = self.get_view(graph_pass_name)?;
                                let view = view.clone();
                                let view = view.lock().unwrap();
                                self.create_render_pipeline(device, pipeline, &view.pass)?;
                            }
                        }
                    }
                    else if let Some(pipelines) = &instance.pipelines {
                        for pipeline in pipelines {
                            self.create_compute_pipeline(device, pipeline)?;
                        }
                    }

                    // add single pass
                    self.command_queue.push(graph_pass_name.to_string());

                    // add additional 5 passes for cubemaps
                    if let Some(cubemap) = instance.cubemap {
                        if cubemap {
                            for i in 1..6 {
                                self.command_queue.push(format!("{}_{}", graph_pass_name, i));
                            }
                        }
                    }

                    // push a view on
                    added += 1;
                    dependencies.insert(graph_pass_name.to_string());
                }
            }
            
            // finally all targets which are in the 'barriers' array are transitioned to shader resources (for debug views)
            let srvs = barriers.keys().map(|k|{
                k.to_string()
            }).collect::<Vec<String>>();

            for name in srvs {
                let result = self.create_resolve_transition(
                    device, &mut barriers, "eof", &name, ResourceState::ShaderResource);
               
                if result.is_err() {
                    // TODO: tell user without spewing out errors
                }

                self.create_texture_transition_barrier(
                    device, &mut barriers, "eof", &name, ResourceState::ShaderResource)?;
            }

            // track the current render graph for if we need to rebuild due to resize, or file modification
            self.active_render_graph = graph_name.to_string();

            Ok(())
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: could not find render graph: {}", graph_name),
            })
        }
    }

    /// Create a ComputePipeline instance for the combination of pmfx_pipeline settings
    pub fn create_compute_pipeline(&mut self, device: &D, pipeline_name: &str) -> Result<(), super::Error> {              
        if self.pmfx.pipelines.contains_key(pipeline_name) {
            // first create shaders if necessary
            let folder = self.pmfx_folders.get(pipeline_name)
                .unwrap_or_else(|| panic!("hotline_rs::pmfx:: expected to find pipeline {} in pmfx_folders", pipeline_name)).to_string();

            for (_, pipeline) in self.pmfx.pipelines[pipeline_name].clone() {
                self.create_shader(device, Path::new(&folder), &pipeline.cs)?;
            }

            for (_, pipeline) in self.pmfx.pipelines[pipeline_name].clone() {    
                let cs = self.get_shader(&pipeline.cs);
                if let Some(cs) = cs {
                    let pso = device.create_compute_pipeline(&gfx::ComputePipelineInfo {
                        cs,
                        pipeline_layout: pipeline.pipeline_layout.clone(),
                    })?;
                    println!("hotline_rs::pmfx:: compiled compute pipeline: {}", pipeline_name);

                    // TODO: permutations
                    //let mask = permutation.parse().unwrap();
                    //permutations.insert(mask, (pipeline.hash, pso));
                    
                    self.compute_pipelines.insert(pipeline_name.to_string(), (pipeline.hash, pso));
                }
            }

            Ok(())
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: could not find pipeline: {}", pipeline_name),
            })
        }
    }

    /// Create a RenderPipeline instance for the combination of pmfx_pipeline settings and an associated RenderPass
    pub fn create_render_pipeline(&mut self, device: &D, pipeline_name: &str, pass: &D::RenderPass) -> Result<(), super::Error> {              
        if self.pmfx.pipelines.contains_key(pipeline_name) {
            // first create shaders if necessary
            let folder = self.pmfx_folders.get(pipeline_name)
                .unwrap_or_else(|| panic!("hotline_rs::pmfx:: expected to find pipeline {} in pmfx_folders", pipeline_name)).to_string();
            
            for (_, pipeline) in self.pmfx.pipelines[pipeline_name].clone() {
                self.create_shader(device, Path::new(&folder), &pipeline.vs)?;
                self.create_shader(device, Path::new(&folder), &pipeline.ps)?;
            }
            
            // create entry for this format if it does not exist
            let fmt = pass.get_format_hash();
            let format_pipeline = self.render_pipelines.entry(fmt).or_insert(HashMap::new());
            
            // create entry for this pipeline permutation set if it does not exist
            if !format_pipeline.contains_key(pipeline_name) {
                println!("hotline_rs::pmfx:: creating pipeline: {}", pipeline_name);
                format_pipeline.insert(pipeline_name.to_string(), HashMap::new());
                // we create a pipeline per-permutation
                for (permutation, pipeline) in self.pmfx.pipelines[pipeline_name].clone() {    
                    let vertex_layout = pipeline.vertex_layout.as_ref().unwrap();
                    let pso = device.create_render_pipeline(&gfx::RenderPipelineInfo {
                        vs: self.get_shader(&pipeline.vs),
                        fs: self.get_shader(&pipeline.ps),
                        input_layout: vertex_layout.to_vec(),
                        pipeline_layout: pipeline.pipeline_layout.clone(),
                        raster_info: info_from_state(&pipeline.raster_state, &self.pmfx.raster_states)?,
                        depth_stencil_info: info_from_state(&pipeline.depth_stencil_state, &self.pmfx.depth_stencil_states)?,
                        blend_info: blend_info_from_state(
                            &pipeline.blend_state, &self.pmfx.blend_states, &self.pmfx.render_target_blend_states)?,
                        topology: pipeline.topology,
                        sample_mask:pipeline.sample_mask,
                        pass: Some(pass),
                        ..Default::default()
                    })?;
                    
                    println!("hotline_rs::pmfx:: compiled render pipeline: {}", pipeline_name);
                    let format_pipeline = self.render_pipelines.get_mut(&fmt).unwrap();
                    let permutations = format_pipeline.get_mut(pipeline_name).unwrap();  

                    let mask = permutation.parse().unwrap();
                    permutations.insert(mask, (pipeline.hash, pso));
                }
            }

            Ok(())
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: could not find pipeline: {}", pipeline_name),
            })
        }
    }

    /// Returns a pmfx defined pipeline compatible with the supplied format hash if it exists
    pub fn get_render_pipeline_for_format<'stack>(&'stack self, pipeline_name: &str, format_hash: u64) -> Result<&'stack D::RenderPipeline, super::Error> {
        self.get_render_pipeline_permutation_for_format(pipeline_name, 0, format_hash)
    }

    /// Returns a pmfx pipline for a random / unknown render target format... prefer to use `get_render_pipeline_for_format` 
    /// if you know the format the target you are rendering in to.
    pub fn get_render_pipeline<'stack>(&'stack self, pipeline_name: &str) -> Result<&'stack D::RenderPipeline, super::Error> {
        for format in self.render_pipelines.values() {
            if format.contains_key(pipeline_name) {
                return Ok(&format[pipeline_name][&0].1);
            }
        }
        Err(super::Error {
            msg: format!("hotline_rs::pmfx:: could not find render pipeline for any format: {}", pipeline_name),
        })
    }

    /// Returns a pmfx defined pipeline compatible with the supplied format hash if it exists
    pub fn get_render_pipeline_permutation_for_format<'stack>(&'stack self, pipeline_name: &str, permutation: u32, format_hash: u64) -> Result<&'stack D::RenderPipeline, super::Error> {
        if let Some(formats) = &self.render_pipelines.get(&format_hash) {
            if formats.contains_key(pipeline_name) {
                Ok(&formats[pipeline_name][&permutation].1)
            }
            else {
                Err(super::Error {
                    msg: format!("hotline_rs::pmfx:: could not find render pipeline for format: {} ({})", pipeline_name, format_hash),
                })
            }
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: could not find render pipeline: {}", pipeline_name),
            })
        }
    }

    /// Fetch a prebuilt ComputePipeline
    pub fn get_compute_pipeline<'stack>(&'stack self, pipeline_name: &str) -> Result<&'stack D::ComputePipeline, super::Error> {
        if self.compute_pipelines.contains_key(pipeline_name) {
            Ok(&self.compute_pipelines[pipeline_name].1)
        }
        else {
            Err(super::Error {
                msg: format!("hotline_rs::pmfx:: could not find compute pipeline: {}", pipeline_name),
            })
        }
    }

    /// Obtain stats for the current frame and caulcuate time deltas between start / end
    fn gather_stats(&mut self, device: &mut D, swap_chain: &D::SwapChain) {
        let mut min_frame_timestamp = f64::max_value();
        let mut max_frame_timestamp = f64::zero();
        let mut total_pipeline_stats = PipelineStatistics::default();
        let timestamp_size_bytes = D::get_timestamp_size_bytes();
        for (name, stats) in &mut self.pass_stats {
            if self.command_queue.contains(name) {
                stats.frame_fence_value = swap_chain.get_frame_fence_value();
                let i = stats.read_index;
                let write_fence = stats.fences[i];
                if write_fence < swap_chain.get_frame_fence_value() {
                    // start timestamp
                    let timestamps = device.read_timestamps(
                        swap_chain, &stats.timestamp_buffers[i][0], timestamp_size_bytes, write_fence);
                    if !timestamps.is_empty() {
                        stats.start_timestamp = timestamps[0];
                        min_frame_timestamp = min(stats.start_timestamp, min_frame_timestamp);
    
                    }
                    // end timestamp
                    let timestamps = device.read_timestamps(
                        swap_chain, &stats.timestamp_buffers[i][1], timestamp_size_bytes, write_fence);
                    if !timestamps.is_empty() {
                        stats.end_timestamp = timestamps[0];
                        max_frame_timestamp = max(stats.end_timestamp, max_frame_timestamp);
                    }
                    // pipeline stats
                    let pipeline_stats = device.read_pipeline_statistics(
                        swap_chain, &stats.pipeline_stats_buffers[i], write_fence);
                    if let Some(pipeline_stats) = pipeline_stats {
                        total_pipeline_stats += pipeline_stats;
                    }
                }
            }
        }
        self.total_stats.gpu_start = min_frame_timestamp;
        self.total_stats.gpu_end = max_frame_timestamp;
        self.total_stats.gpu_time_ms = (max_frame_timestamp - min_frame_timestamp) * 1000.0;
        self.total_stats.pipeline_stats = total_pipeline_stats;
    }

    /// Start a new frame and syncronise command buffers to the designated swap chain
    pub fn new_frame(&mut self, device: &mut D, swap_chain: &D::SwapChain) -> Result<(), super::Error> {
        // check if we have any reloads available
        if self.reloader.check_for_reload() == ReloadState::Available {
            // wait for last GPU frame so we can drop the resources
            swap_chain.wait_for_last_frame();
            self.reload(device)?;
            self.reloader.complete_reload();
        }

        // gather render stats
        self.gather_stats(device, swap_chain);

        // reset command buffers
        self.reset(swap_chain);

        // swap the world buffers
        self.world_buffers.camera.swap();
        self.world_buffers.draw.swap();
        self.world_buffers.point_light.swap();
        self.world_buffers.directional_light.swap();
        self.world_buffers.spot_light.swap();

        Ok(())
    }

    /// Reload all active resources based on hashes
    pub fn reload(&mut self, device: &mut D) -> Result<(), super::Error> {        
        let reload_paths = self.pmfx_tracking.iter_mut().filter(|(_, tracking)| {
            fs::metadata(&tracking.filepath).unwrap().modified().unwrap() > tracking.modified_time
        }).map(|tracking| {
            tracking.1.filepath.to_string_lossy().to_string()
        }).collect::<Vec<String>>();

        let mut rebuild_graph = false;
        for reload_filepath in reload_paths {
            if !reload_filepath.is_empty() {
                println!("hotline_rs::pmfx:: reload from {}", reload_filepath);
                let pmfx_data = fs::read(&reload_filepath).expect("hotline_rs::pmfx:: failed to read file");
                
                let file : File = serde_json::from_slice(&pmfx_data)?;
                self.merge_pmfx(file, PathBuf::from(&reload_filepath).parent().unwrap().to_str().unwrap());

                // remove stale states
                // self.remove_stale();

                // find textures that need reloading
                let reload_textures = self.textures.iter().filter(|(k, v)| {
                    self.pmfx.textures.get(*k).map_or_else(|| false, |src| {
                        src.hash != v.0
                    })
                }).map(|(k, _)| {
                    k.to_string()
                }).collect::<HashSet<String>>();

                // Get views to reload from changed textures
                let reload_texture_views = reload_textures.iter().fold(HashSet::new(), |mut v, t|{
                    v.extend(self.get_view_texture_refs(t));
                    v
                });

                // Find views that have changed by hash
                let mut reload_views = Vec::new();
                for (name, view) in &self.views {
                    if self.pmfx.views.contains_key(&view.2) &&
                    (self.pmfx.views.get(&view.2).unwrap().hash != view.0 || reload_texture_views.contains(name)) {
                        reload_views.push((view.2.to_string(), name.to_string()));
                    }
                }

                // find pipelines that need reloading
                let mut reload_pipelines = Vec::new();
                for (hash, formats) in &self.render_pipelines {
                    for (name, permutations) in formats {
                        for (mask, pipeline) in permutations {

                            let build_hash = self.pmfx.pipelines
                                .get(name).unwrap()
                                .get(&mask.to_string()).unwrap()
                                .hash;

                            if pipeline.0 != build_hash {
                                reload_pipelines.push((*hash, name.to_string(), *mask));
                            }
                        }
                    }
                }

                // find shaders that need reloading
                let mut reload_shaders = Vec::new();
                for (name, shader) in &self.shaders {
                    if self.pmfx.shaders.contains_key(name) {
                        if *self.pmfx.shaders.get(name).unwrap() != shader.0 {
                            reload_shaders.push(name.to_string());
                        }
                    }
                }

                // reload textures
                self.recreate_textures(device, &reload_textures)?;

                // reload views
                for view in &reload_views {
                    println!("hotline::pmfx:: reloading view: {}", view.1);
                    self.views.remove(&view.1);
                    self.pass_stats.remove(&view.1);
                    rebuild_graph = true;
                }

                // reload shaders
                for shader in &reload_shaders {
                    println!("hotline::pmfx:: reloading shader: {}", shader);
                    self.shaders.remove(shader);
                }
                
                // reload pipelines tuple = (format_hash, pipeline_name, permutation_mask)
                for pipeline in &reload_pipelines {
                    println!("hotline::pmfx:: reloading pipeline: {}", pipeline.1);
                    
                    // TODO: here we could only remove affected permutations
                    let format_pipelines = self.render_pipelines.get_mut(&pipeline.0).unwrap();
                    format_pipelines.remove(&pipeline.1);

                    // find first with the same format
                    let compatiblew_view = self.views.iter().find(|(_, view)| {
                        let pass = &view.1.lock().unwrap().pass;
                        pass.get_format_hash() == pipeline.0
                    }).map(|v| v.0);

                    // create pipeline with the pass from compatible view
                    if let Some(compatiblew_view) = compatiblew_view {
                        let view = self.get_view(compatiblew_view)?.clone();
                        let view = view.lock().unwrap();
                        self.create_render_pipeline(device, &pipeline.1, &view.pass)?;
                    }
                    else {
                        println!("hotline::pmfx:: warning pipeline was not reloaded: {}", pipeline.1);
                    }
                }

                // update the timestamp on the tracking info
                self.pmfx_tracking.get_mut(&reload_filepath).map(|t| {
                    t.modified_time = SystemTime::now();
                    t
                });
            }

            // 
            if rebuild_graph {
                self.create_render_graph(device, &self.active_render_graph.to_string())?;
            }
        }
        Ok(())
    }

    /// Recreate the textures in `texture_names` call this when you know size / sample count has changed
    /// and the tracking info is updated
    fn recreate_textures(&mut self, device: &mut D, texture_names: &HashSet<String>) -> Result<(), super::Error> {
        for texture_name in texture_names {
            // remove the old and destroy
            self.textures.remove(texture_name);
            // create with new dimensions from 'window_sizes'
            self.create_texture(device, texture_name)?;
        }
        Ok(())
    }

    /// Returns `Vec<String>` containing view names associated with the texture name
    fn get_view_texture_refs(&self, texture_name: &str) -> HashSet<String> {
        if self.view_texture_refs.contains_key(texture_name) {
            self.view_texture_refs[texture_name].clone()
        }
        else {
            HashSet::new()
        }
    }

    pub fn get_window_size(&self, window_name: &str) -> (f32, f32) {
        if self.window_sizes.contains_key(window_name) {
            self.window_sizes[window_name]
        }
        else {
            (0.0, 0.0)
        }
    }

    pub fn get_window_aspect(&self, window_name: &str) -> f32 {
        if self.window_sizes.contains_key(window_name) {
            let size = self.window_sizes[window_name];
            size.0 / size.1
        }
        else {
            0.0
        }
    }

    /// Update render targets or views associated with a window, this will resize textures and rebuild views
    /// which need to be modified if a window size changes
    pub fn update_window(&mut self, device: &mut D, size: (f32, f32), name: &str) -> Result<(), super::Error> {
        let mut rebuild_views = HashSet::new();
        let mut recreate_texture_names = HashSet::new();
        if self.window_sizes.contains_key(name) {
            if self.window_sizes[name] != size {
                // update tracked textures
                for (texture_name, texture) in &self.textures {
                    if let Some(ratio) = &texture.1.ratio {
                        if ratio.window == name {
                            recreate_texture_names.insert(texture_name.to_string());
                            if self.view_texture_refs.contains_key(texture_name) {
                                for view_name in &self.view_texture_refs[texture_name] {
                                    self.views.remove(view_name);
                                    self.pass_stats.remove(view_name);
                                    rebuild_views.insert(view_name.to_string());
                                }
                            }
                        }
                    }
                }
                // update the size
                self.window_sizes.remove(name);
            }
        }
        // insert window to track
        self.window_sizes.insert(name.to_string(), size);

        // recreate textures for the new sizes
        self.recreate_textures(device, &recreate_texture_names)?;

        // recreate the active render graph
        if !rebuild_views.is_empty() {
            if !self.active_render_graph.is_empty() {
                self.create_render_graph(device, &self.active_render_graph.to_string())?;
            }
        }

        Ok(())
    }

    /// Update camera constants for the named camera, will create a new entry if one does not exist
    pub fn update_camera_constants(&mut self, name: &str, constants: &CameraConstants) {
        *self.cameras.entry(name.to_string()).or_insert(constants.clone()) = constants.clone();
    }

    /// Update camera constants for the named camera, will create a new entry if one does not exist
    pub fn update_cubemap_camera_constants(&mut self, name: &str, pos: Vec3f, near: f32, far: f32) {
        // add a camera for each face
        for i in 0..6 {
            let name = format!("{}_{}", name, i);
            let constants = cubemap_camera_face(i, pos, near, far);
            *self.cameras.entry(name.to_string()).or_insert(constants.clone()) = constants.clone();
        }
    }

    /// Borrow camera constants to push into a command buffer, return `None` if they do not exist
    pub fn get_camera_constants(&self, name: &str) -> Result<&CameraConstants, super::Error> {
        if let Some(cam) = &self.cameras.get(name) {
            Ok(cam)
        }
        else {
            Err(super::Error {
                msg: format!("hotline::pmfx:: could not find camera {}", name)
            })
        }
    }

    fn stats_start(cmd_buf: &mut D::CmdBuf, pass_stats: &mut PassStats<D>) {
        // sync to the frame
        pass_stats.fences[pass_stats.write_index] = pass_stats.frame_fence_value;

        // view timestamps
        pass_stats.timestamp_heap.reset();
        let buf = &mut pass_stats.timestamp_buffers[pass_stats.write_index][0];
        cmd_buf.timestamp_query(&mut pass_stats.timestamp_heap, buf);

        // view pipeline stats
        pass_stats.pipeline_stats_heap.reset();
        pass_stats.pipeline_query_index = cmd_buf.begin_query(
            &mut pass_stats.pipeline_stats_heap, 
            gfx::QueryType::PipelineStatistics
        );
    }

    fn stats_end(cmd_buf: &mut D::CmdBuf, pass_stats: &mut PassStats<D>) {
        // end timestamp
        let buf = &mut pass_stats.timestamp_buffers[pass_stats.write_index][1];
        cmd_buf.timestamp_query(&mut pass_stats.timestamp_heap, buf);

        // end pipeline stats query
        if pass_stats.pipeline_query_index != usize::max_value() {
            let buf = &mut pass_stats.pipeline_stats_buffers[pass_stats.write_index];
            cmd_buf.end_query(
                &mut pass_stats.pipeline_stats_heap, 
                gfx::QueryType::PipelineStatistics,
                pass_stats.pipeline_query_index,
                buf,
            );
            pass_stats.pipeline_query_index = usize::max_value();
        }
    }

    /// Resets all command buffers, this assumes they have been used and need to be reset for the next frame
    pub fn reset(&mut self, swap_chain: &D::SwapChain) {
        // TODO: collapse minimise
        for (name, view) in &self.views {
            // rest only command buffers that are in use
            if self.command_queue.contains(name) {
                let view = view.clone();
                let mut view = view.1.lock().unwrap();
                view.cmd_buf.reset(swap_chain);

                // inserts markers for timing and tracking pipeline stats
                let mut stats = self.pass_stats.remove(name).unwrap();
                Self::stats_start(&mut view.cmd_buf, &mut stats);
                self.pass_stats.insert(name.to_string(), stats);
            }
        }
        for (name, pass) in &self.compute_passes {
            // rest only command buffers that are in use
            if self.command_queue.contains(name) {
                let pass = pass.clone();
                let mut pass = pass.1.lock().unwrap();
                pass.cmd_buf.reset(swap_chain);

                // inserts markers for timing and tracking pipeline stats
                let mut stats = self.pass_stats.remove(name).unwrap();
                Self::stats_start(&mut pass.cmd_buf, &mut stats);
                self.pass_stats.insert(name.to_string(), stats);
            }
        }
    }

    /// Returns a vector of information to call render functions. It returns a tuple (function_name, view_name)
    /// which is called as so: `function_name(view)` so functions can be re-used for different views
    pub fn get_render_graph_function_info(&self, render_graph: &str) -> Vec<(String, String)> {
        if self.pmfx.render_graphs.contains_key(render_graph) {
            let mut passes = Vec::new();
            for (name, pass) in &self.pmfx.render_graphs[render_graph] {
                passes.push((pass.function.to_string(), name.to_string()));
                // add additional cubemap passes
                if let Some(cubemap) = pass.cubemap {
                    if cubemap {
                        for i in 1..6 {
                            passes.push((pass.function.to_string(), format!("{}_{}", name, i).to_string()));
                        }
                    }
                }
            }
            passes
        }
        else {
            Vec::new()
        }
    }

    /// Returns the build hash for the render graph so you can compare if the graph has rebuilt and needs reloading
    pub fn get_render_graph_hash(&self, render_graph: &str) -> PmfxHash {
        // this could be calculated at build time
        if self.pmfx.render_graphs.contains_key(render_graph) {
            self.pmfx.render_graphs[render_graph].keys().fold(DefaultHasher::new(), |mut hasher, name|{
                name.hash(&mut hasher);
                hasher
            }).finish()
        }
        else {
            0
        }
    }

    pub fn get_render_graph_execute_order(&self) -> &Vec<String> {
        &self.command_queue
    }

    /// Execute command buffers in order
    pub fn execute(
        &mut self,
        device: &mut D) {
        for node in &self.command_queue {
            if self.barriers.contains_key(node) {
                // transition barriers
                device.execute(&self.barriers[node]);
            }
            // TODO: collapse minimise
            else if self.views.contains_key(node) {
                // execute a view
                let view = self.views[node].clone();
                let view = &mut view.1.lock().unwrap();

                // inserts markers for timing and tracking pipeline stats
                let mut stats = self.pass_stats.remove(node).unwrap();
                Self::stats_end(&mut view.cmd_buf, &mut stats);
                self.pass_stats.insert(node.to_string(), stats);

                view.cmd_buf.close().unwrap();
                device.execute(&view.cmd_buf);
            }
            else if self.compute_passes.contains_key(node) {
                // dispatch a compute pass
                let pass = self.compute_passes[node].clone();
                let pass = &mut pass.1.lock().unwrap();

                // inserts markers for timing and tracking pipeline stats
                let mut stats = self.pass_stats.remove(node).unwrap();
                Self::stats_end(&mut pass.cmd_buf, &mut stats);
                self.pass_stats.insert(node.to_string(), stats);

                pass.cmd_buf.close().unwrap();
                device.execute(&pass.cmd_buf);
            }
        }
    }

    /// Log an error with an assosiated view and message.
    pub fn log_error(&self, view_name: &str, msg: &str) {
        let mut errors = self.view_errors.lock().unwrap();
        errors.entry(view_name.to_string()).or_insert(msg.to_string());
    }

    /// Return the total statistics for the previous frame
    pub fn get_total_stats(&self) -> &TotalStats {
        &self.total_stats
    }
}


use crate::imgui;
impl<D, A> imgui::UserInterface<D, A> for Pmfx<D> where D: gfx::Device, A: os::App, D::RenderPipeline: gfx::Pipeline {
    fn show_ui(&mut self, imgui: &mut imgui::ImGui<D, A>, open: bool) -> bool {
        if open {
            let mut imgui_open = open;
            if imgui.begin("textures", &mut imgui_open, imgui::WindowFlags::ALWAYS_HORIZONTAL_SCROLLBAR) {
                for texture in self.textures.values() {
                    
                    let thumb_size = 256.0;
                    let aspect = texture.1.size.0 as f32 / texture.1.size.1 as f32;
                    let w = thumb_size * aspect;
                    let h = thumb_size;

                    imgui.image(&texture.1.texture, w, h);

                    imgui.same_line();
                    imgui.spacing();
                    imgui.same_line();
                }
            }
            imgui.end();

            if imgui.begin("pmfx", &mut imgui_open, imgui::WindowFlags::ALWAYS_HORIZONTAL_SCROLLBAR) {
                imgui.text("Shaders");
                imgui.separator();
                for shader in self.pmfx.shaders.keys() {
                    imgui.text(shader);
                }
                imgui.separator();

                imgui.text("Pipelines");
                imgui.separator();
                for pipeline in self.pmfx.pipelines.keys() {
                    imgui.text(pipeline);
                }
                imgui.separator();

                imgui.text("Render Graphs");
                imgui.separator();
                for graph in self.pmfx.render_graphs.keys() {
                    imgui.text(graph);
                }
                imgui.separator();

                imgui.text("Cameras");
                imgui.separator();
                for camera in self.cameras.keys() {
                    imgui.text(camera);
                }
                imgui.separator();
            }
            imgui.end();

            if imgui.begin("perf", &mut imgui_open, imgui::WindowFlags::NONE) {
                imgui.text(&format!("gpu: {:.2} (ms)", self.total_stats.gpu_time_ms));
                imgui.separator();
                imgui.text("pipeline statistics");
                imgui.separator();
                imgui.text(&format!("input_assembler_vertices: {}", self.total_stats.pipeline_stats.input_assembler_vertices));
                imgui.text(&format!("input_assembler_primitives: {}", self.total_stats.pipeline_stats.input_assembler_primitives));
                imgui.text(&format!("vertex_shader_invocations: {}", self.total_stats.pipeline_stats.vertex_shader_invocations));
                imgui.text(&format!("pixel_shader_primitives: {}", self.total_stats.pipeline_stats.pixel_shader_primitives));
                imgui.text(&format!("compute_shader_invocations: {}", self.total_stats.pipeline_stats.compute_shader_invocations));
            }
            imgui.end();

            imgui_open
        } 
        else {
            false
        }
    }
}

struct PmfxReloadResponder {
    files: Vec<String>,
    start_time: SystemTime
}

impl PmfxReloadResponder {
    fn new() -> Self {
        PmfxReloadResponder {
            files: Vec::new(),
            start_time: SystemTime::now()
        }
    }
}

impl ReloadResponder for PmfxReloadResponder {
    fn add_file(&mut self, filepath: &str) {
        self.files.push(filepath.to_string());
    }  

    fn get_files(&self) -> Vec<String> {
        self.files.to_vec()
    }

    fn get_last_mtime(&self) -> SystemTime {
        self.start_time
    }

    fn build(&mut self) -> std::process::ExitStatus {
        let hotline_path = super::get_data_path("../..");
        let pmbuild = super::get_data_path("../../hotline-data/pmbuild.cmd");
        let output = std::process::Command::new(pmbuild)
            .current_dir(hotline_path)
            .arg("win32-data")
            .arg("-pmfx")
            .output()
            .expect("hotline::hot_lib:: hot pmfx failed to compile!");

        if !output.stdout.is_empty() {
            println!("{}", String::from_utf8(output.stdout).unwrap());
        }

        if !output.stderr.is_empty() {
            println!("{}", String::from_utf8(output.stderr).unwrap());
        }

        if output.status.success() {
            self.start_time = SystemTime::now();
        }

        output.status
    }
}