summaryrefslogtreecommitdiff
path: root/src/intel/vulkan/grl/gpu/bvh_build_BFS.cl
blob: d72f192056e20ea75922bfe0bd061ac3041d8616 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
//
// Copyright (C) 2009-2021 Intel Corporation
//
// SPDX-License-Identifier: MIT
//
//

#include "binned_sah_shared.h"

#include "libs/lsc_intrinsics.h"
#include "intrinsics.h"
#include "AABB.h"
#include "AABB3f.h"

#include "qbvh6.h"
#include "common.h"

#include "libs/lsc_intrinsics.h"

#define SGPRINT_16x(prefix,fmt,type,val)  {\
                                        type v0 = sub_group_broadcast( val, 0 );\
                                        type v1 = sub_group_broadcast( val, 1 );\
                                        type v2 = sub_group_broadcast( val, 2 );\
                                        type v3 = sub_group_broadcast( val, 3 );\
                                        type v4 = sub_group_broadcast( val, 4 );\
                                        type v5 = sub_group_broadcast( val, 5 );\
                                        type v6 = sub_group_broadcast( val, 6 );\
                                        type v7 = sub_group_broadcast( val, 7 );\
                                        type v8 = sub_group_broadcast( val, 8 );\
                                        type v9 = sub_group_broadcast( val, 9 );\
                                        type v10 = sub_group_broadcast( val, 10 );\
                                        type v11 = sub_group_broadcast( val, 11 );\
                                        type v12 = sub_group_broadcast( val, 12 );\
                                        type v13 = sub_group_broadcast( val, 13 );\
                                        type v14 = sub_group_broadcast( val, 14 );\
                                        type v15 = sub_group_broadcast( val, 15 );\
                                        sub_group_barrier(CLK_LOCAL_MEM_FENCE); \
                                        if( get_sub_group_local_id() == 0 ) { \
                                        printf(prefix fmt fmt fmt fmt fmt fmt fmt fmt \
                                                      fmt fmt fmt fmt fmt fmt fmt fmt"\n" , \
                                            v0,v1,v2,v3,v4,v5,v6,v7,v8,v9,v10,v11,v12,v13,v14,v15);}}


#define SGPRINT_6x(prefix,fmt,type,val)  {\
                                        type v0 = sub_group_broadcast( val, 0 );\
                                        type v1 = sub_group_broadcast( val, 1 );\
                                        type v2 = sub_group_broadcast( val, 2 );\
                                        type v3 = sub_group_broadcast( val, 3 );\
                                        type v4 = sub_group_broadcast( val, 4 );\
                                        type v5 = sub_group_broadcast( val, 5 );\
                                        sub_group_barrier(CLK_LOCAL_MEM_FENCE); \
                                        if( get_sub_group_local_id() == 0 ) { \
                                        printf(prefix fmt fmt fmt fmt fmt fmt "\n" , \
                                            v0,v1,v2,v3,v4,v5);}}

#define BFS_WG_SIZE  512

#define BFS_NUM_VCONTEXTS 256 // must be multiple of 64

#define TREE_ARITY 6

#define DFS_WG_SIZE  256
#define DFS_THRESHOLD 256


void BFSDispatchQueue_print(struct BFSDispatchQueue* q, uint n)
{
    for (uint i = 0; i < q->num_dispatches; i++)
        printf("   %u,ctx=%u,batch=%u\n", q->wg_count[i], q->records[i].context_id, q->records[i].batch_index);
}

void VContextScheduler_print(struct VContextScheduler* scheduler)
{
    if (get_local_id(0) == 0)
    {
        printf("SCHEDULER:\n");
        printf("    bfs=%u dfs=%u\n", scheduler->num_bfs_wgs, scheduler->num_dfs_wgs);

        printf("BFS QUEUE:\n");
        BFSDispatchQueue_print(&scheduler->bfs_queue, scheduler->num_bfs_wgs);


        printf("DFS QUEUE\n");
        for (uint i = 0; i < scheduler->num_dfs_wgs; i++)
        {
            struct DFSDispatchRecord* r = &scheduler->dfs_queue.records[i];
            printf("    (%u-%u) root=%u  depth=%u  batch_index=%u\n",
                r->primref_base, r->primref_base + r->num_primrefs,
                r->bvh2_base, r->tree_depth, r->batch_index);
        }

        printf("CONTEXTS:\n");
        for (uint i = 0; i < BFS_NUM_VCONTEXTS; i++)
        {
            if (scheduler->vcontext_state[i] != VCONTEXT_STATE_UNALLOCATED)
            {
                printf(" context: %u  state=%u\n", i, scheduler->vcontext_state[i]);
                printf("     prims: %u-%u\n", scheduler->contexts[i].dispatch_primref_begin, scheduler->contexts[i].dispatch_primref_end);
                printf("     depth: %u\n", scheduler->contexts[i].tree_depth);
                printf("     root: %u\n", scheduler->contexts[i].bvh2_root);
                printf("     batch: %u\n", scheduler->contexts[i].batch_index);
            }
        }



    }

}


inline float3 select_min(float3 v, bool mask)
{
    return (float3)(mask ? v.x : (float)(INFINITY),
        mask ? v.y : (float)(INFINITY),
        mask ? v.z : (float)(INFINITY));
}
inline float3 select_max(float3 v, bool mask)
{
    return (float3)(mask ? v.x : -(float)(INFINITY),
        mask ? v.y : -(float)(INFINITY),
        mask ? v.z : -(float)(INFINITY));
}

///////////////////////////////////////////////////////////////////////////

//  The 'LRBounds' structure uses negated-max to allow
//  both atomic_min and atomic_max to be issued fused into one message

struct AABB3f LRBounds_get_left_centroid( LRBounds* b )
{
    struct AABB3f* pbox = &b->boxes.left_centroid_bounds;
    return AABB3f_construct( AABB3f_load_lower(pbox), -AABB3f_load_upper(pbox) );
}
struct AABB3f LRBounds_get_right_centroid( LRBounds* b )
{
    struct AABB3f* pbox = &b->boxes.right_centroid_bounds;
    return AABB3f_construct( AABB3f_load_lower(pbox), -AABB3f_load_upper(pbox) );
}
struct AABB3f LRBounds_get_left_geom( LRBounds* b )
{
    struct AABB3f* pbox = &b->boxes.left_geom_bounds;
    return AABB3f_construct( AABB3f_load_lower(pbox), -AABB3f_load_upper(pbox) );
}
struct AABB3f LRBounds_get_right_geom( LRBounds* b )
{
    struct AABB3f* pbox = &b->boxes.right_geom_bounds;
    return AABB3f_construct( AABB3f_load_lower(pbox), -AABB3f_load_upper(pbox) );
}


void LRBounds_merge_left( local LRBounds* b, float3 CMin, float3 CMax, float3 GMin, float3 GMax )
{
    // All of the input vectors have come from sub-group reductions and are thus uniform
    //   Using atomic_min calls as below results in IGC generating 12 atomic_min messages and a large stack of movs
    //  The code below should result in 1 atomic_min message and a simularly large stack of movs

    float mergeVal0 = INFINITY;
    float mergeVal1 = INFINITY;
    uint i = get_sub_group_local_id();

    // insert the various merge values into one register
    //  We use two parallel variables here to enable some ILP

    uint imod = (i>=6) ? (i-6) : i;
    mergeVal0 = (imod==0)  ?  CMin.x : mergeVal0;
    mergeVal1 = (imod==0)  ?  GMin.x : mergeVal1;

    mergeVal0 = (imod==1)  ?  CMin.y : mergeVal0;
    mergeVal1 = (imod==1)  ?  GMin.y : mergeVal1;

    mergeVal0 = (imod==2)  ?  CMin.z : mergeVal0;
    mergeVal1 = (imod==2)  ?  GMin.z : mergeVal1;

    mergeVal0 = (imod==3)  ? -CMax.x : mergeVal0;
    mergeVal1 = (imod==3)  ? -GMax.x : mergeVal1;

    mergeVal0 = (imod==4)  ? -CMax.y : mergeVal0;
    mergeVal1 = (imod==4)  ? -GMax.y : mergeVal1;

    mergeVal0 = (imod==5)  ? -CMax.z : mergeVal0;
    mergeVal1 = (imod==5)  ? -GMax.z : mergeVal1;

    float merge = (i<6) ? mergeVal0 : mergeVal1;
    if( i < 12 )
        atomic_min( &b->scalars.Array[i], merge );

    //atomic_min( &b->boxes.left_centroid_bounds.lower[0], CMin.x );
    //atomic_min( &b->boxes.left_centroid_bounds.lower[1], CMin.y );
    //atomic_min( &b->boxes.left_centroid_bounds.lower[2], CMin.z );
    //atomic_min( &b->boxes.left_centroid_bounds.upper[0], -CMax.x );
    //atomic_min( &b->boxes.left_centroid_bounds.upper[1], -CMax.y );
    //atomic_min( &b->boxes.left_centroid_bounds.upper[2], -CMax.z );
    //atomic_min( &b->boxes.left_geom_bounds.lower[0],      GMin.x );
    //atomic_min( &b->boxes.left_geom_bounds.lower[1],      GMin.y );
    //atomic_min( &b->boxes.left_geom_bounds.lower[2],      GMin.z );
    //atomic_min( &b->boxes.left_geom_bounds.upper[0], -GMax.x );
    //atomic_min( &b->boxes.left_geom_bounds.upper[1], -GMax.y );
    //atomic_min( &b->boxes.left_geom_bounds.upper[2], -GMax.z );
}

void LRBounds_merge_right( local LRBounds* b, float3 CMin, float3 CMax, float3 GMin, float3 GMax )
{
    // All of the input vectors have come from sub-group reductions and are thus uniform
    //   Using atomic_min calls as below results in IGC generating 12 atomic_min messages and a large stack of movs
    //  The code below should result in 1 atomic_min message and a simularly large stack of movs

    float mergeVal0 = INFINITY;
    float mergeVal1 = INFINITY;
    uint i = get_sub_group_local_id();

    // insert the various merge values into one register
    //  We use two parallel variables here to enable some ILP

    uint imod = (i>=6) ? (i-6) : i;
    mergeVal0 = (imod==0)  ?  CMin.x : mergeVal0;
    mergeVal1 = (imod==0)  ?  GMin.x : mergeVal1;

    mergeVal0 = (imod==1)  ?  CMin.y : mergeVal0;
    mergeVal1 = (imod==1)  ?  GMin.y : mergeVal1;

    mergeVal0 = (imod==2)  ?  CMin.z : mergeVal0;
    mergeVal1 = (imod==2)  ?  GMin.z : mergeVal1;

    mergeVal0 = (imod==3)  ? -CMax.x : mergeVal0;
    mergeVal1 = (imod==3)  ? -GMax.x : mergeVal1;

    mergeVal0 = (imod==4)  ? -CMax.y : mergeVal0;
    mergeVal1 = (imod==4)  ? -GMax.y : mergeVal1;

    mergeVal0 = (imod==5)  ? -CMax.z : mergeVal0;
    mergeVal1 = (imod==5)  ? -GMax.z : mergeVal1;

    float merge = (i<6) ? mergeVal0 : mergeVal1;
    if( i < 12 )
        atomic_min( &b->scalars.Array[i+12], merge );

    //atomic_min( &b->boxes.right_centroid_bounds.lower[0],  CMin.x );
    //atomic_min( &b->boxes.right_centroid_bounds.lower[1],  CMin.y );
    //atomic_min( &b->boxes.right_centroid_bounds.lower[2],  CMin.z );
    //atomic_min( &b->boxes.right_centroid_bounds.upper[0], -CMax.x );
    //atomic_min( &b->boxes.right_centroid_bounds.upper[1], -CMax.y );
    //atomic_min( &b->boxes.right_centroid_bounds.upper[2], -CMax.z );
    //atomic_min( &b->boxes.right_geom_bounds.lower[0],      GMin.x );
    //atomic_min( &b->boxes.right_geom_bounds.lower[1],      GMin.y );
    //atomic_min( &b->boxes.right_geom_bounds.lower[2],      GMin.z );
    //atomic_min( &b->boxes.right_geom_bounds.upper[0],     -GMax.x );
    //atomic_min( &b->boxes.right_geom_bounds.upper[1],     -GMax.y );
    //atomic_min( &b->boxes.right_geom_bounds.upper[2],     -GMax.z );
}

void LRBounds_merge( global LRBounds* globalBounds, local LRBounds* localBounds )
{
    uint i = get_local_id(0);
    if( i < 24 )
        atomic_min(&globalBounds->scalars.Array[i], localBounds->scalars.Array[i] );
}


void LRBounds_init( LRBounds* bounds )
{
    uint i = get_local_id(0) * 4;
    if( i < 24 )
    {
        // compiler should merge it into a 4xdword send
        bounds->scalars.Array[i+0] = INFINITY;
        bounds->scalars.Array[i+1] = INFINITY;
        bounds->scalars.Array[i+2] = INFINITY;
        bounds->scalars.Array[i+3] = INFINITY;
    }

}


inline void LRBounds_init_subgroup( LRBounds* bounds)
{
    uint sg_size = get_sub_group_size();
    uint lane = get_sub_group_local_id();

    for (uint i = lane * 4; i < 24; i += sg_size * 4)
    {
        // compiler should merge it into a 4xdword send
        bounds->scalars.Array[i+0] = INFINITY;
        bounds->scalars.Array[i+1] = INFINITY;
        bounds->scalars.Array[i+2] = INFINITY;
        bounds->scalars.Array[i+3] = INFINITY;
    }

}

///////////////////////////////////////////////////////////////////////////

inline void BinInfo_init(struct BFS_BinInfo* bin_info)
{
    for (uint id = get_local_id(0) * 4; id < 18 * BFS_NUM_BINS; id += get_local_size(0) * 4)
    {
        float inf = INFINITY;
        // compiler should merge it into a 4xdword send
        bin_info->min_max[id+0] = inf;
        bin_info->min_max[id+1] = inf;
        bin_info->min_max[id+2] = inf;
        bin_info->min_max[id+3] = inf;
    }
    for (uint id = get_local_id(0) * 4; id < 3 * BFS_NUM_BINS; id += get_local_size(0) * 4)
    {
        // compiler should merge it into a 4xdword send
        bin_info->counts[id+0] = 0;
        bin_info->counts[id+1] = 0;
        bin_info->counts[id+2] = 0;
        bin_info->counts[id+3] = 0;
    }
}


// copy global to local
inline void BinInfo_copy( local struct BFS_BinInfo* local_bin_info, global struct BFS_BinInfo* global_bin_info )
{
    for (uint id = get_local_id(0); id < 18 * BFS_NUM_BINS; id += get_local_size(0))
    {
        float inf = INFINITY ;
        float f = global_bin_info->min_max[id];
        local_bin_info->min_max[id] = f;
    }
    for (uint id = get_local_id(0); id < 3 * BFS_NUM_BINS; id += get_local_size(0))
    {
        local_bin_info->counts[id] = global_bin_info->counts[id];
    }
}

inline void BinInfo_init_subgroup(struct BFS_BinInfo* bin_info)
{
    uint sg_size = get_sub_group_size();
    uint lane = get_sub_group_local_id();

    for (uint i = lane * 4; i < 3 * BFS_NUM_BINS; i += sg_size * 4)
    {
        // compiler should merge it into a 4xdword send
        bin_info->counts[i+0] = 0;
        bin_info->counts[i+1] = 0;
        bin_info->counts[i+2] = 0;
        bin_info->counts[i+3] = 0;
    }


    for (uint i = lane * 4; i < 18 * BFS_NUM_BINS; i += sg_size * 4)
    {
        // compiler should merge it into a 4xdword send
        bin_info->min_max[i+0] = INFINITY;
        bin_info->min_max[i+1] = INFINITY;
        bin_info->min_max[i+2] = INFINITY;
        bin_info->min_max[i+3] = INFINITY;
    }

}

float3 shuffle_down_float3( float3 a, float3 b, uint delta )
{
    return (float3)(
        intel_sub_group_shuffle_down( a.x, b.x, delta ),
        intel_sub_group_shuffle_down( a.y, b.y, delta ),
        intel_sub_group_shuffle_down( a.z, b.z, delta )
        );
}




void BinInfo_primref_ballot_loop( local struct BFS_BinInfo* bin_info, uint axis, uint bin, float3 lower, float3 upper, bool active_lane )
{
    local float* bins_min = &bin_info->min_max[0];
    local float* bins_max = &bin_info->min_max[3];

    varying uint place = (bin + axis*BFS_NUM_BINS);
    varying uint lane = get_sub_group_local_id();

    uniform uint active_mask = intel_sub_group_ballot(active_lane);

    while( active_mask )
    {
        uniform uint leader     = ctz( active_mask );
        uniform uint lead_place = intel_sub_group_shuffle( place, leader );
        varying bool matching_bin = lead_place == place && active_lane;

        varying float3 lo = (float3)(INFINITY,INFINITY,INFINITY);
        varying float3 hi = (float3)(-INFINITY,-INFINITY,-INFINITY);
        if (matching_bin)
        {
            lo = lower.xyz;
            hi = upper.xyz;
        }

        lo = sub_group_reduce_min_float3( lo );
        hi = sub_group_reduce_max_float3( hi );

        {
            // atomic min operation vectorized across 6 lanes
            //    [ lower.xyz ][-][upper.xyz][-]
            //
            // Lanes 3 and 7 are inactive

            uint lmod = lane % 4;
            uint ldiv = lane / 4;
            float vlo = lo.x;
            float vhi = hi.x;
            vlo = (lmod == 1) ? lo.y : vlo;
            vhi = (lmod == 1) ? hi.y : vhi;
            vlo = (lmod == 2) ? lo.z : vlo;
            vhi = (lmod == 2) ? hi.z : vhi;

            float v = (ldiv == 0) ? vlo : -vhi;

            if( (1<<lane) & 0x77 )
                atomic_min( &bin_info->min_max[ 6*lead_place + lmod + 3*ldiv ], v );
        }

      //if( lane == 0 )
      //    atomic_add_local(&bin_info->counts[lead_place], popcount(active_mask & intel_sub_group_ballot(matching_bin)) );

        active_mask = active_mask & intel_sub_group_ballot(!matching_bin);
    }
}

inline void BinInfo_add_primref(struct BinMapping* binMapping, local struct BFS_BinInfo* bin_info, PrimRef* primref, bool active_lane )
{

    const float4 lower = primref->lower;
    const float4 upper = primref->upper;
    const float4 p = lower + upper;
    const uint4 i = convert_uint4( (p - binMapping->ofs) * binMapping->scale );

    BinInfo_primref_ballot_loop( bin_info, 0, i.x, lower.xyz, upper.xyz, active_lane );
    BinInfo_primref_ballot_loop( bin_info, 1, i.y, lower.xyz, upper.xyz, active_lane );
    BinInfo_primref_ballot_loop( bin_info, 2, i.z, lower.xyz, upper.xyz, active_lane );

    if (active_lane)
    {
        atomic_inc_local( &bin_info->counts[i.x + 0 * BFS_NUM_BINS] );
        atomic_inc_local( &bin_info->counts[i.y + 1 * BFS_NUM_BINS] );
        atomic_inc_local( &bin_info->counts[i.z + 2 * BFS_NUM_BINS] );
    }
}

inline void BinInfo_merge(global struct BFS_BinInfo* global_info, local struct BFS_BinInfo* local_info)
{
    uint id = get_local_id(0);
    for (uint id = get_local_id(0); id < 18 * BFS_NUM_BINS; id += get_local_size(0))
    {
            float v = local_info->min_max[id];
            if( v != INFINITY )
                atomic_min(&global_info->min_max[id], v);
    }
    for (uint id = get_local_id(0); id < 3 * BFS_NUM_BINS; id += get_local_size(0))
    {
            uint c = local_info->counts[id];
            if( c )
                atomic_add_global(&global_info->counts[id], c);
    }
}

inline struct AABB3f BinInfo_get_AABB(struct BFS_BinInfo* bin_info, ushort bin, ushort axis)
{
    float* min = &bin_info->min_max[6*(bin + axis*BFS_NUM_BINS)];
    float* max = min + 3;
    struct AABB3f box;
    for (uint i = 0; i < 3; i++)
    {
        box.lower[i] = min[i];
        box.upper[i] = -max[i];
    }

    return box;
}

inline uint3 BinInfo_get_counts(struct BFS_BinInfo* bin_info, ushort bin)
{
    uint3 counts;
    counts.x = bin_info->counts[bin + 0 * BFS_NUM_BINS]; // TODO: block load these
    counts.y = bin_info->counts[bin + 1 * BFS_NUM_BINS];
    counts.z = bin_info->counts[bin + 2 * BFS_NUM_BINS];
    return counts;
}
inline uint BinInfo_get_count(struct BFS_BinInfo* bin_info, ushort bin, ushort axis)
{
    return bin_info->counts[bin + axis * BFS_NUM_BINS];
}


void BVH2_Initialize( struct BVH2* bvh )
{
    bvh->num_nodes = 1;
}

inline bool BVH2_IsInnerNode( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    return (n->meta_ss & 0x10000) != 0;
}
inline uint BVH2_GetRoot( struct BVH2* bvh )
{
    return 0;
}

//////////////////////////////////////////////
// BVH2NodeMetaData funcs
//////////////////////////////////////////////
struct BVH2NodeMetaData
{
    uint  meta_u;   // leaf:  primref start.  inner: offset from node to its first child
    uint  meta_ss;
};

inline struct BVH2NodeMetaData BVH2_GetNodeMetaData( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    struct BVH2NodeMetaData meta;
    meta.meta_u = n->meta_u;
    meta.meta_ss = n->meta_ss;
    return meta;
}

inline bool BVH2NodeMetaData_IsInnerNode( struct BVH2NodeMetaData* meta )
{
    return (meta->meta_ss & 0x10000) != 0;
}

inline ushort BVH2NodeMetaData_GetLeafPrimCount( struct BVH2NodeMetaData* meta )
{
    return meta->meta_ss & 0xffff;
}

inline uint BVH2NodeMetaData_GetLeafPrimStart( struct BVH2NodeMetaData* meta )
{
    return meta->meta_u;
}

inline uint BVH2NodeMetaData_GetMask( struct BVH2NodeMetaData* meta )
{
    return (meta->meta_ss>>24);
}

//////////////////////////////////////////////

inline ushort BVH2_GetLeafPrimCount( struct BVH2* bvh, uint node_index )
{
    struct BVH2Node* n = ((struct BVH2Node*)(bvh + 1)) + node_index;
    return n->meta_ss & 0xffff;
}
inline uint BVH2_GetLeafPrimStart( struct BVH2* bvh, uint node_index )
{
    struct BVH2Node* n = ((struct BVH2Node*)(bvh + 1)) + node_index;
    return n->meta_u;
}
inline uint2 BVH2_GetChildIndices( struct BVH2* bvh, uint node_index )
{
    struct BVH2Node* n = ((struct BVH2Node*)(bvh + 1)) + node_index;
    uint2 idx;
    idx.x = n->meta_u;
    idx.y = idx.x + (n->meta_ss & 0xffff);
    return idx;
}

inline float BVH2_GetNodeArea( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    return AABB3f_halfArea( &n->box );
}


inline struct AABB3f BVH2_GetNodeBox( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    return n->box;
}
inline void BVH2_SetNodeBox( global struct BVH2* bvh, uint node_index, struct AABB3f* box )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    n->box = *box;
}

inline void BVH2_SetNodeBox_lu( global struct BVH2* bvh, uint node_index, float3 lower, float3 upper )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    AABB3f_set( &n->box, lower, upper );
}

inline void BVH2_InitNodeBox( struct BVH2* bvh, uint node_index )
{
    struct BVH2Node* n = ((struct BVH2Node*)(bvh + 1)) + node_index;
    AABB3f_init( &n->box );
}

inline struct AABB BVH2_GetAABB( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    struct AABB r;
    r.lower.xyz = AABB3f_load_lower( &n->box );
    r.upper.xyz = AABB3f_load_upper( &n->box );
    return r;
}

inline void BVH2_WriteInnerNode( global struct BVH2* bvh, uint node_index, struct AABB3f* box, uint2 child_offsets, uint mask )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    n->box = *box;
    n->meta_u   = child_offsets.x;
    n->meta_ss  = 0x10000 + (child_offsets.y - child_offsets.x) + (mask<<24);
  //  n->is_inner  = true;
}

inline void BVH2_WriteLeafNode( global struct BVH2* bvh, uint node_index, struct AABB3f* box, uint prim_start, uint prim_count, uint mask  )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    n->box = *box;
    n->meta_u   = prim_start;
    n->meta_ss  = prim_count + (mask<<24);
    //  n->is_inner  = true;
}

inline uint BVH2_GetMask( global struct BVH2* bvh, uint node_index )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    return (n->meta_ss>>24);
}


uint BVH2_AllocateNodes( global struct BVH2* bvh, uint num_nodes )
{
    return atomic_add_global( &bvh->num_nodes, num_nodes );
}

inline void BVH2_AtomicMergeNodeBox( global struct BVH2* bvh, uint node_index, float3 lower, float3 upper )
{
    global struct BVH2Node* n = ((global struct BVH2Node*)(bvh + 1)) + node_index;
    AABB3f_atomic_merge_global_lu( &n->box, lower, upper );
}


void BVH2_print( global struct BVH2* bvh, uint start_node )
{
    if ( get_local_id( 0 ) == 0 && get_sub_group_id() == 0 )
    {
        uint num_nodes = bvh->num_nodes;

        uint2 stack[BFS_MAX_DEPTH * 2];
        uint sp = 0;

        printf( "allocated_nodes=%u\n", num_nodes );

        stack[sp++] = (uint2)(start_node, 0);
        while ( sp > 0 )
        {
            uint2 data = stack[--sp];
            uint node = data.x;
            uint depth = data.y;

            for ( uint i = 0; i < depth; i++ )
                printf( "    " );

            if ( BVH2_IsInnerNode( bvh, node ) )
            {
                uint2 kids = BVH2_GetChildIndices( bvh, node );
                printf( " %5u: inner: %u %u \n", node, kids.x, kids.y );
                stack[sp++] = (uint2)(kids.y, depth + 1);
                stack[sp++] = (uint2)(kids.x, depth + 1);

                struct AABB3f l = BVH2_GetNodeBox( bvh, kids.x );
                struct AABB3f r = BVH2_GetNodeBox( bvh, kids.y );
                struct AABB3f p = BVH2_GetNodeBox( bvh, node );

                float3 pl = AABB3f_load_lower( &p );
                float3 pu = AABB3f_load_upper( &p );
                float3 ll = AABB3f_load_lower( &l );
                float3 lu = AABB3f_load_upper( &l );
                float3 rl = AABB3f_load_lower( &r );
                float3 ru = AABB3f_load_upper( &r );
                if ( any( ll < pl ) || any( rl < pl ) ||
                     any( lu > pu ) || any( ru > pu ) )
                {
                    for ( uint i = 0; i < depth; i++ )
                        printf( "    " );

                    printf( "BAD_BOUNDS!!!!!!!! %u\n", node );
                }


            }
            else
            {

                uint start = BVH2_GetLeafPrimStart( bvh, node );
                uint count = BVH2_GetLeafPrimCount( bvh, node );
                printf( " %5u: leaf: start=%u count=%u\n  ",node,start,count );

            }
        }
    }
    barrier( CLK_LOCAL_MEM_FENCE );
}


global uint* SAHBuildGlobals_GetPrimrefIndices_In( struct SAHBuildGlobals* globals, bool odd_pass )
{
    uint num_refs = globals->num_primrefs;
    global uint* ib = (global uint*) globals->p_primref_index_buffers;
    return ib + (odd_pass ? num_refs : 0);
}

global uint* SAHBuildGlobals_GetPrimrefIndices_Out( struct SAHBuildGlobals* globals, bool odd_pass )
{
    uint num_refs = globals->num_primrefs;
    global uint* ib = (global uint*) globals->p_primref_index_buffers;
    return ib + (odd_pass ? 0 : num_refs);
}

global PrimRef* SAHBuildGlobals_GetPrimrefs( struct SAHBuildGlobals* globals )
{
    return (global PrimRef*) globals->p_primrefs_buffer;
}

global struct BVH2* SAHBuildGlobals_GetBVH2( struct SAHBuildGlobals* globals )
{
    return (global struct BVH2*)globals->p_bvh2;
}

uint SAHBuildGlobals_GetLeafSizeInBytes( struct SAHBuildGlobals* globals )
{
    return globals->leaf_size;
}

uint SAHBuildGlobals_GetLeafType( struct SAHBuildGlobals* globals )
{
    return globals->leaf_type;
}

uint SAHBuildGlobals_GetInternalNodeType( struct SAHBuildGlobals* globals )
{
    return NODE_TYPE_INTERNAL;
}

global struct BVHBase* SAHBuildGlobals_GetBVHBase( struct SAHBuildGlobals* globals )
{
    return (global struct BVHBase*) globals->p_bvh_base;
}

uint SAHBuildGlobals_GetTotalPrimRefs( struct SAHBuildGlobals* globals )
{
    return globals->num_primrefs;
}

inline bool SAHBuildGlobals_NeedBackPointers( struct SAHBuildGlobals* globals )
{
    return globals->flags & SAH_FLAG_NEED_BACKPOINTERS;
}
inline bool SAHBuildGlobals_NeedMasks( struct SAHBuildGlobals* globals )
{
    return globals->flags & SAH_FLAG_NEED_MASKS;
}


void SAHBuildGlobals_print( struct SAHBuildGlobals* globals )
{
    if ( get_local_id( 0 ) == 0 )
    {
        printf( "SAHBuildGlobals: %p\n", globals );
        printf( "  p_primref_index_buffers =%p\n", globals->p_primref_index_buffers );
        printf( "  p_primrefs_buffer =%p\n",       globals->p_primrefs_buffer );
        printf( "  p_bvh2 =%p\n",                  globals->p_bvh2 );
        printf( "  p_globals =%p\n",               globals->p_globals );
        printf( "  p_bvh_base =%p\n",              globals->p_bvh_base );
        printf( "  num_primrefs = %u\n",           globals->num_primrefs );
        printf( "  leaf_size = %u\n",              globals->leaf_size );
        printf( "  leaf_type = %u\n",              globals->leaf_type );
        printf( "  p_qnode_buffer = %p\n",         globals->p_qnode_root_buffer);
    }

    barrier( CLK_LOCAL_MEM_FENCE );
}


uint get_num_wgs(uint thread_count, uint WG_SIZE)
{
    return (thread_count + WG_SIZE - 1) / WG_SIZE;
}





struct BFSDispatchArgs
{
    global struct VContextScheduler* scheduler;
    global struct VContext* context;
    global struct BVH2* bvh2;
    global uint* primref_index_in;
    global uint* primref_index_out;
    global PrimRef* primref_buffer;

    uint   wg_primref_begin;
    uint   wg_primref_end;
    uint   dispatch_primref_begin;
    uint   dispatch_primref_end;
    uint   context_id;
    uint   num_wgs;
    uint   bvh2_root;
    uint   global_num_primrefs;
    bool   do_mask_processing;
};




// TODO_OPT:  Enable larger WGs
//    We need a way to do this in a portable fashion.
//     Gen12 can support larger WGs than Gen9 can
//
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( 512, 1, 1 )) )
kernel void
begin( global struct VContextScheduler* scheduler,
       dword leaf_size,
       dword leaf_type,
       global uint* primref_index_buffers,
       global PrimRef* primref_buffer,
       global struct BVH2* bvh2,
       global struct BVHBase* bvh_base,
       global struct Globals* globals,
       global struct SAHBuildGlobals* sah_globals,
       global uint2* qnode_root_buffer,
       dword sah_globals_flags
     )
{
    dword num_primrefs = globals->numPrimitives;
    if ( get_local_id( 0 ) == 0 )
    {
        sah_globals->p_primrefs_buffer       = (qword) primref_buffer;
        sah_globals->p_primref_index_buffers = (qword)primref_index_buffers;
        sah_globals->p_bvh2                  = (qword) bvh2;
        sah_globals->p_bvh_base              = (qword) bvh_base;
        sah_globals->leaf_size               = leaf_size;
        sah_globals->leaf_type               = leaf_type;
        sah_globals->num_primrefs            = num_primrefs;
        sah_globals->p_globals               = (qword) globals;
        sah_globals->p_qnode_root_buffer     = (gpuva_t) qnode_root_buffer;
        sah_globals->flags                   = sah_globals_flags;

        // initialize the spill stack
        scheduler->bfs2_spill_stack.size = 0;

        // initialize BVH2 node counter
        BVH2_Initialize( bvh2 );

        // configure first vcontext for first build
        scheduler->contexts[0].dispatch_primref_begin = 0;
        scheduler->contexts[0].dispatch_primref_end   = num_primrefs;
        scheduler->contexts[0].bvh2_root              = BVH2_GetRoot( bvh2 );
        scheduler->contexts[0].tree_depth             = 0;
        scheduler->contexts[0].batch_index            = 0;

        scheduler->bfs_queue.records[0].context_id = 0;

        scheduler->contexts[0].num_left = 0;
        scheduler->contexts[0].num_right = 0;
        scheduler->contexts[0].lr_mask = 0;

        // copy centroid bounds into the BVH2 root node'
        BVH2_SetNodeBox_lu( bvh2, BVH2_GetRoot( bvh2 ), globals->centroidBounds.lower.xyz, globals->centroidBounds.upper.xyz );

        // zero the trivial build counters.. these are only used by the batch-build path
        //  but single-wg QNode path (if used) depends on them
        scheduler->num_trivial_builds = 0;
        scheduler->num_single_builds = 0;

        // initialize the root-buffer counters
        sah_globals->root_buffer_num_produced     = 0;
        sah_globals->root_buffer_num_produced_hi  = 0;
        sah_globals->root_buffer_num_consumed     = 0;
        sah_globals->root_buffer_num_consumed_hi  = 0;
    }

    // initialize vcontext states
    for ( uint i = get_local_id( 0 ); i < BFS_NUM_VCONTEXTS; i += get_local_size( 0 ) )
        scheduler->vcontext_state[i] = (i==0) ? VCONTEXT_STATE_EXECUTING : VCONTEXT_STATE_UNALLOCATED;

    // initialize global bin info in vcontext - only context[0] will be used in first iteration
    BinInfo_init( &scheduler->contexts[0].global_bin_info );
    LRBounds_init( &scheduler->contexts[0].lr_bounds );

   // barrier( CLK_GLOBAL_MEM_FENCE  ); // lsc flush ... driver now does these as part of COMPUTE_WALKER
}

// TODO_OPT:  Enable larger WGs
//    We need a way to do this in a portable fashion.
//     Gen12 can support larger WGs than Gen9 can
//


// TODO_OPT:  Enable larger WGs
//    We need a way to do this in a portable fashion.
//     Gen12 can support larger WGs than Gen9 can
//
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(512, 1, 1)))
kernel void
categorize_builds_and_init_scheduler(
    global struct VContextScheduler* scheduler,
    global gpuva_t* globals_ptrs,                // OCL-C does not allow kernel parameters to be pointer-to-pointer, so we trick it...
    global struct SAHBuildBuffersInfo* buffers_info,
    global struct SAHBuildGlobals* builds_out,
    dword num_builds
)
{
    local uint num_trivial;
    local uint num_single;
    local uint num_full;

    if (get_group_id(0) == 0) // first workgroup performs build categorization
    {
        if (get_local_id(0) == 0)
        {
            num_trivial = 0;
            num_single = 0;
            num_full = 0;
        }

        barrier(CLK_LOCAL_MEM_FENCE);

        // first pass, count builds of each type
        uint triv = 0;
        uint single = 0;
        uint full = 0;
        for (uint i = get_local_id(0); i < num_builds; i += get_local_size(0))
        {
            global struct Globals* globals = (global struct Globals*) globals_ptrs[i];
            dword num_refs = globals->numPrimitives;

            if (num_refs <= TRIVIAL_BUILD_THRESHOLD)
                triv++;
            else if (num_refs <= SINGLE_WG_BUILD_THRESHOLD)
                single++;
            else
                full++;
        }

        // merge counts across work-group.  These variables are now offsets into this thread's ranges
        triv   = atomic_add_local(&num_trivial, triv);
        single = atomic_add_local(&num_single, single);
        full   = atomic_add_local(&num_full, full);

        barrier(CLK_LOCAL_MEM_FENCE);

        global struct SAHBuildGlobals* trivial_builds_out = builds_out;
        global struct SAHBuildGlobals* single_builds_out = builds_out + num_trivial;
        global struct SAHBuildGlobals* full_builds_out = builds_out + num_trivial + num_single;

        for (uint i = get_local_id(0); i < num_builds; i += get_local_size(0))
        {
            global struct Globals* globals = (global struct Globals*) globals_ptrs[i];
            global struct SAHBuildBuffersInfo* buffers = &buffers_info[i];

            dword num_refs = globals->numPrimitives;
            dword leaf_type = globals->leafPrimType;
            dword leaf_size = globals->leafSize;

            global struct SAHBuildGlobals* place;
            if (num_refs <= TRIVIAL_BUILD_THRESHOLD)
                place = trivial_builds_out + (triv++);
            else if (num_refs <= SINGLE_WG_BUILD_THRESHOLD)
                place = single_builds_out + (single++);
            else
                place = full_builds_out + (full++);

            place->p_primref_index_buffers = buffers->p_primref_index_buffers;
            place->p_primrefs_buffer    = buffers->p_primrefs_buffer;
            place->p_bvh2               = buffers->p_bvh2;
            place->p_bvh_base           = buffers->p_bvh_base;
            place->p_globals            = (gpuva_t)globals;
            place->num_primrefs         = num_refs;
            place->leaf_size            = leaf_size;
            place->leaf_type            = leaf_type;
            place->flags                = buffers->sah_globals_flags;
            place->p_qnode_root_buffer  = buffers->p_qnode_root_buffer;

            // only initialize BVH2 if it will actually be used by the build
            //   trivial passes will not use it
            if( num_refs > SINGLE_WG_BUILD_THRESHOLD )
            {
                // initialize BVH2 node counter
                global struct BVH2* bvh2 = SAHBuildGlobals_GetBVH2(place);
                BVH2_Initialize(bvh2);

                // copy centroid bounds into the BVH2 root node'
                BVH2_SetNodeBox_lu(bvh2, BVH2_GetRoot(bvh2), globals->centroidBounds.lower.xyz, globals->centroidBounds.upper.xyz);
            }
        }

        if (get_local_id(0) == 0)
        {
            scheduler->num_trivial_builds   = num_trivial;
            scheduler->num_single_builds    = num_single;
            scheduler->batched_build_offset = num_trivial + num_single;
            scheduler->batched_build_count  = num_full;
        }
    }
    else // second workgroup initializes the scheduler
    {
        // initialize vcontext states
        for (uint i = get_local_id(0); i < BFS_NUM_VCONTEXTS; i += get_local_size(0))
            scheduler->vcontext_state[i] = (i == 0) ? VCONTEXT_STATE_EXECUTING : VCONTEXT_STATE_UNALLOCATED;

        // initialize global bin info in vcontexts
        for (uint i = get_sub_group_id(); i < BFS_NUM_VCONTEXTS; i += get_num_sub_groups())
            BinInfo_init_subgroup(&scheduler->contexts[i].global_bin_info);

        // initialize the spill stack
        if (get_local_id(0) == 0)
            scheduler->bfs2_spill_stack.size = 0;
    }

    //barrier( CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE );// lsc flush ... driver now does these as part of COMPUTE_WALKER
}





GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(BFS_NUM_VCONTEXTS, 1, 1)))
kernel void
begin_batchable(
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* sah_globals
)
{
    ushort scheduler_build_offset = scheduler->batched_build_offset;
    ushort scheduler_num_builds   = scheduler->batched_build_count;

    ushort num_builds = min( scheduler_num_builds, (ushort)BFS_NUM_VCONTEXTS );

    uint num_wgs = 0;

    ushort tid = get_local_id(0);
    if ( tid < num_builds )
    {
        ushort batch_index = scheduler_build_offset + tid;

        uint num_primrefs = sah_globals[batch_index].num_primrefs;

        // configure first vcontext for first build
        scheduler->contexts[tid].dispatch_primref_begin = 0;
        scheduler->contexts[tid].dispatch_primref_end   = num_primrefs;
        scheduler->contexts[tid].bvh2_root              = BVH2_GetRoot( SAHBuildGlobals_GetBVH2(&sah_globals[batch_index]) );
        scheduler->contexts[tid].tree_depth             = 0;
        scheduler->contexts[tid].batch_index            = batch_index;
        scheduler->vcontext_state[tid] = VCONTEXT_STATE_EXECUTING;

        scheduler->contexts[tid].num_left = 0;
        scheduler->contexts[tid].num_right = 0;
        scheduler->contexts[tid].lr_mask   = 0;

        num_wgs = get_num_wgs( num_primrefs, BFS_WG_SIZE );

        scheduler->bfs_queue.wg_count[tid] = num_wgs;
        scheduler->bfs_queue.records[tid].batch_index = batch_index;
        scheduler->bfs_queue.records[tid].context_id  = tid;
    }

    num_wgs = work_group_reduce_add(num_wgs);

    if (tid == 0)
    {
        // write out build count and offset for next BFS iteration
        scheduler->batched_build_offset = scheduler_build_offset + num_builds;
        scheduler->batched_build_count  = scheduler_num_builds - num_builds;

        // write out initial WG count and loop termination mask for command streamer to consume
        scheduler->batched_build_wg_count  = num_wgs;
        scheduler->batched_build_loop_mask = (scheduler_num_builds > num_builds) ? 1 : 0;

        scheduler->bfs_queue.num_dispatches = num_builds;
    }

    for ( uint i = get_sub_group_id(); i < num_builds; i += get_num_sub_groups() )
        BinInfo_init_subgroup( &scheduler->contexts[i].global_bin_info );

    for ( uint i = get_sub_group_id(); i < num_builds; i += get_num_sub_groups() )
        LRBounds_init_subgroup( &scheduler->contexts[i].lr_bounds );
}



bool is_leaf( uint num_refs )
{
    return num_refs <= TREE_ARITY;
}

bool is_dfs( uint num_refs )
{
    return num_refs > TREE_ARITY&& num_refs <= DFS_THRESHOLD;
}

bool is_bfs( uint num_refs )
{
    return num_refs > DFS_THRESHOLD;
}

int2 is_leaf_2( uint2 num_refs )
{
    return num_refs.xy <= TREE_ARITY;
}
int2 is_bfs_2( uint2 num_refs )
{
    return num_refs.xy > DFS_THRESHOLD;
}

int2 is_dfs_2( uint2 num_refs )
{
    return num_refs.xy > TREE_ARITY && num_refs.xy <= DFS_THRESHOLD;
}

#if 0
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(16, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
sg_scheduler( global struct VContextScheduler* scheduler )
{
    local struct BFS1SpillStackEntry SLM_local_spill_stack[BFS_NUM_VCONTEXTS];
    local uchar SLM_context_state[BFS_NUM_VCONTEXTS];
    local vcontext_id_t SLM_free_list[BFS_NUM_VCONTEXTS];
    local vcontext_id_t SLM_exec_list[BFS_NUM_VCONTEXTS];


    varying ushort lane = get_sub_group_local_id();

    uniform uint free_list_size = 0;
    uniform uint exec_list_size = 0;

    // read context states, build lists of free and executing contexts
    for (varying uint i = lane; i < BFS_NUM_VCONTEXTS; i += get_sub_group_size())
    {
        uchar state = scheduler->vcontext_state[i];
        SLM_context_state[i] = state;

        uniform ushort exec_mask = intel_sub_group_ballot(state == VCONTEXT_STATE_EXECUTING);

        varying ushort prefix_exec = subgroup_bit_prefix_exclusive(exec_mask);
        varying ushort prefix_free = lane - prefix_exec;
        varying ushort exec_list_pos = exec_list_size + prefix_exec;
        varying ushort free_list_pos = free_list_size + prefix_free;

        if (state == VCONTEXT_STATE_EXECUTING)
            SLM_exec_list[exec_list_pos] = i;
        else
            SLM_free_list[free_list_pos] = i;

        uniform ushort num_exec = popcount(exec_mask);
        exec_list_size += num_exec;
        free_list_size += get_sub_group_size() - num_exec;
    }

    uniform uint total_bfs_dispatches = 0;
    uniform uint total_dfs_dispatches = 0;
    uniform uint bfs_spill_stack_size   = 0;
    uniform uint total_bfs_wgs      = 0;

    // process executing context.  accumulate bfs/dfs dispatches and free-list entries
    for (uint i = 0; i < exec_list_size; i+= get_sub_group_size() )
    {
        varying ushort num_dfs_dispatches     = 0;
        varying ushort num_bfs_spills         = 0;

        varying ushort num_bfs_children;
        varying ushort context_id;
        struct VContext* context;
        varying uint num_left      ;
        varying uint num_right     ;
        varying uint primref_begin ;
        varying uint primref_end   ;
        varying uint depth         ;

        bool active_lane = (i + lane) < exec_list_size;
        if ( active_lane )
        {
            context_id = SLM_exec_list[i + lane];
            context    = &scheduler->contexts[context_id];

            num_left      = context->num_left;
            num_right     = context->num_right;
            primref_begin = context->dispatch_primref_begin;
            primref_end   = context->dispatch_primref_end;
            depth         = context->tree_depth;

            // get dispatch counts

            num_dfs_dispatches = is_dfs(num_left) + is_dfs(num_right);
            num_bfs_children = is_bfs(num_left) + is_bfs(num_right);
            num_bfs_spills = (num_bfs_children == 2) ? 1 : 0;
        }

        // allocate space for DFS, BFS dispatches, and BFS spills
        varying uint dfs_pos               = total_dfs_dispatches + sub_group_scan_exclusive_add(num_dfs_dispatches);
        varying ushort mask_bfs_spills     = intel_sub_group_ballot(num_bfs_children & 2); // spill if #children == 2
        varying ushort mask_bfs_dispatches = intel_sub_group_ballot(num_bfs_children & 3); // dispatch if #children == 1 or 2
        varying uint bfs_spill_pos         = bfs_spill_stack_size + subgroup_bit_prefix_exclusive(mask_bfs_spills);
        varying uint bfs_dispatch_pos      = total_bfs_dispatches + subgroup_bit_prefix_exclusive(mask_bfs_dispatches);

        total_dfs_dispatches += sub_group_reduce_add(num_dfs_dispatches);
        bfs_spill_stack_size += popcount(mask_bfs_spills);
        total_bfs_dispatches += popcount(mask_bfs_dispatches);

        varying uint num_bfs_wgs = 0;
        if (active_lane)
        {
            if (num_dfs_dispatches)
            {
                if (is_dfs(num_left))
                {
                    scheduler->dfs_queue.records[dfs_pos].primref_base = primref_begin;
                    scheduler->dfs_queue.records[dfs_pos].num_primrefs = num_left;
                    scheduler->dfs_queue.records[dfs_pos].bvh2_base = context->left_bvh2_root;
                    scheduler->dfs_queue.records[dfs_pos].tree_depth = depth + 1;
                    dfs_pos++;
                }
                if (is_dfs(num_right))
                {
                    scheduler->dfs_queue.records[dfs_pos].primref_base = primref_begin + num_left;
                    scheduler->dfs_queue.records[dfs_pos].num_primrefs = num_right;
                    scheduler->dfs_queue.records[dfs_pos].bvh2_base = context->right_bvh2_root;
                    scheduler->dfs_queue.records[dfs_pos].tree_depth = depth + 1;
                }
            }

            uint num_bfs_children = is_bfs(num_left) + is_bfs(num_right);
            if (num_bfs_children == 2)
            {
                // spill the right child.. push an entry onto local spill stack
                SLM_local_spill_stack[bfs_spill_pos].primref_begin = primref_begin + num_left;
                SLM_local_spill_stack[bfs_spill_pos].primref_end = primref_end;
                SLM_local_spill_stack[bfs_spill_pos].bvh2_root = context->right_bvh2_root;
                SLM_local_spill_stack[bfs_spill_pos].tree_depth = depth + 1;

                // setup BFS1 dispatch for left child
                context->dispatch_primref_end = primref_begin + num_left;
                context->bvh2_root = context->left_bvh2_root;
                context->tree_depth = depth + 1;
                num_bfs_wgs = get_num_wgs(num_left, BFS_WG_SIZE);

                scheduler->bfs_queue.wg_count[bfs_dispatch_pos]           = num_bfs_wgs;
                scheduler->bfs_queue.records[bfs_dispatch_pos].context_id = context_id;
            }
            else if (num_bfs_children == 1)
            {
                // setup BFS1 dispatch for whichever child wants it
                if (is_bfs(num_left))
                {
                    // bfs on left child
                    context->dispatch_primref_end = context->dispatch_primref_begin + num_left;
                    context->bvh2_root = context->left_bvh2_root;
                    context->tree_depth = depth + 1;
                    num_bfs_wgs = get_num_wgs(num_left, BFS_WG_SIZE);
                }
                else
                {
                    // bfs on right child
                    context->dispatch_primref_begin = context->dispatch_primref_begin + num_left;
                    context->bvh2_root = context->right_bvh2_root;
                    context->tree_depth = depth + 1;
                    num_bfs_wgs = get_num_wgs(num_right, BFS_WG_SIZE);
                }

                scheduler->bfs_queue.wg_count[bfs_dispatch_pos]           = num_bfs_wgs;
                scheduler->bfs_queue.records[bfs_dispatch_pos].context_id = context_id;
            }
            else
            {
                // no bfs dispatch.. this context is now free
                SLM_context_state[context_id] = VCONTEXT_STATE_UNALLOCATED;
            }
        }

        // count bfs work groups
        total_bfs_wgs += sub_group_reduce_add(num_bfs_wgs);

        // add newly deallocated contexts to the free list
        uniform uint free_mask = intel_sub_group_ballot( active_lane && num_bfs_children == 0);
        varying uint free_list_pos = free_list_size + subgroup_bit_prefix_exclusive(free_mask);
        free_list_size += popcount(free_mask);

        if ( free_mask & (1<<lane) )
            SLM_free_list[free_list_pos] = context_id;

    }

    barrier(CLK_LOCAL_MEM_FENCE);

    // if we have more free contexts than spills, read additional spills from the scheduler's spill stack
    uniform uint memory_spill_stack_size = scheduler->bfs2_spill_stack.size;

    if(bfs_spill_stack_size < free_list_size && memory_spill_stack_size > 0 )
    {
        uniform uint read_count = min(free_list_size - bfs_spill_stack_size, memory_spill_stack_size);

        for (varying uint i = lane; i < read_count; i+= get_sub_group_size())
            SLM_local_spill_stack[bfs_spill_stack_size + i] = scheduler->bfs2_spill_stack.entries[memory_spill_stack_size - 1 - i];

        bfs_spill_stack_size += read_count;
        memory_spill_stack_size -= read_count;
    }

    // steal pending BFS work and assign it to free contexts
    uniform uint num_steals = min(bfs_spill_stack_size, free_list_size);

    for (uniform uint i = 0; i < num_steals; i += get_sub_group_size())
    {
        varying uint num_bfs_wgs = 0;

        if (i + lane < num_steals)
        {
            uint context_id = SLM_free_list[i+lane];
            struct VContext* context = &scheduler->contexts[context_id];
            struct BFS1SpillStackEntry entry = SLM_local_spill_stack[i+lane];

            context->dispatch_primref_begin = entry.primref_begin;
            context->dispatch_primref_end = entry.primref_end;
            context->bvh2_root = entry.bvh2_root;
            context->tree_depth = entry.tree_depth;

            num_bfs_wgs = get_num_wgs(entry.primref_end - entry.primref_begin, BFS_WG_SIZE);

            scheduler->bfs_queue.wg_count[total_bfs_dispatches + i + lane] = num_bfs_wgs;
            scheduler->bfs_queue.records[total_bfs_dispatches + i + lane].context_id = context_id;

            SLM_context_state[context_id] = VCONTEXT_STATE_EXECUTING;
        }

        total_bfs_wgs += sub_group_reduce_add( num_bfs_wgs );
    }

    total_bfs_dispatches += num_steals;

    //  write out excess spills to global spill stack
    uniform uint extra_spills = bfs_spill_stack_size - num_steals;
    for (varying uint i = lane; i < extra_spills; i += get_sub_group_size())
    {
        scheduler->bfs2_spill_stack.entries[memory_spill_stack_size + i] = SLM_local_spill_stack[num_steals+i];
    }


    // write out modified context states
    for ( varying uint i = lane; i < BFS_NUM_VCONTEXTS; i += get_sub_group_size())
        scheduler->vcontext_state[i] = SLM_context_state[i];


    if (get_local_id(0) == 0)
    {
        // write out new memory stack size
        scheduler->bfs2_spill_stack.size = memory_spill_stack_size + extra_spills;

        // store workgroup counters
        scheduler->bfs_queue.num_dispatches = total_bfs_dispatches;
        scheduler->num_bfs_wgs = total_bfs_wgs;
        scheduler->num_dfs_wgs = total_dfs_dispatches;
    }

  //  barrier(CLK_GLOBAL_MEM_FENCE); // make memory writes globally visible// lsc flush ... driver now does these as part of COMPUTE_WALKER
}
#endif

#define SCHEDULER_SG_SIZE 16
#define SCHEDULER_WG_SIZE BFS_NUM_VCONTEXTS
#define SCHEDULER_NUM_SGS (SCHEDULER_WG_SIZE / SCHEDULER_SG_SIZE)


struct BFSDispatchArgs get_bfs_args_from_record_batchable(
    struct BFSDispatchRecord* record,
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* globals_buffer );

GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(SCHEDULER_WG_SIZE, 1, 1)))
__attribute__((intel_reqd_sub_group_size(SCHEDULER_SG_SIZE)))
kernel void
scheduler(global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* sah_globals )
{
    local struct BFS1SpillStackEntry SLM_local_spill_stack[2 * BFS_NUM_VCONTEXTS];
    local uint SLM_local_spill_stack_size;
    local uint SLM_dfs_dispatch_count;

    if (get_local_id(0) == 0)
    {
        SLM_local_spill_stack_size = 0;
        SLM_dfs_dispatch_count = 0;
    }

    uint context_id = get_local_id(0);
    uint state = scheduler->vcontext_state[context_id];
    uint initial_state = state;

    uint batch_index = 0;
    global struct VContext* context = &scheduler->contexts[context_id];

    barrier(CLK_LOCAL_MEM_FENCE);


    uint global_spill_stack_size = scheduler->bfs2_spill_stack.size;


    if (state == VCONTEXT_STATE_EXECUTING)
    {
        uint left_bvh2_root;
        uint right_bvh2_root;

        uint num_left = context->num_left;
        uint num_right = context->num_right;

        uint primref_begin = context->dispatch_primref_begin;
        uint primref_end = context->dispatch_primref_end;

        uint depth = context->tree_depth;
        uint batch_index = context->batch_index;

        struct BFSDispatchRecord record;
        record.context_id = context_id;
        record.batch_index = context->batch_index;

        struct BFSDispatchArgs args = get_bfs_args_from_record_batchable( &record, scheduler, sah_globals);

        // do cleanup of bfs_pass2
        {
            // compute geom bounds
            struct AABB3f left_geom_bounds;
            struct AABB3f right_geom_bounds;
            struct AABB3f left_centroid_bounds;
            struct AABB3f right_centroid_bounds;
            uint2 lr_counts = (uint2)(num_left, num_right);

            {
                left_centroid_bounds    = LRBounds_get_left_centroid( &context->lr_bounds );
                left_geom_bounds        = LRBounds_get_left_geom(  &context->lr_bounds );
                right_centroid_bounds   = LRBounds_get_right_centroid( &context->lr_bounds );
                right_geom_bounds       = LRBounds_get_right_geom( &context->lr_bounds );
            }

            int2 v_is_leaf = is_leaf_2( lr_counts );
            int2 v_is_dfs  = is_dfs_2( lr_counts );
            int2 v_is_bfs  = is_bfs_2( lr_counts );
            uint left_mask  = args.do_mask_processing ? context->lr_mask & 0xff : 0xff;
            uint right_mask = args.do_mask_processing ? (context->lr_mask & 0xff00) >> 8 : 0xff;

            // how many BVH2 nodes do we need to allocate?  For DFS, we need to pre-allocate full subtree
            uint2 lr_node_counts = select( (uint2)(1,1), (2*lr_counts-1), v_is_dfs );
            uint left_node_count = lr_node_counts.x;
            uint right_node_count = lr_node_counts.y;

            // allocate the nodes
            uint first_node = BVH2_AllocateNodes( args.bvh2, left_node_count + right_node_count );

            // point our root node at its children
            left_bvh2_root  = first_node;
            right_bvh2_root = first_node + left_node_count;

            // store combined geom bounds in the root node's AABB.. we previously stored centroid bounds there
            //   but node creation requires geom bounds
            struct AABB3f geom_bounds = left_geom_bounds;
            AABB3f_extend(&geom_bounds, &right_geom_bounds);
            BVH2_WriteInnerNode( args.bvh2, args.bvh2_root, &geom_bounds, (uint2)(left_bvh2_root,right_bvh2_root), left_mask | right_mask );

//            printf(" node: %u  mask: %x\n", args.bvh2_root, left_mask|right_mask );

            // store the appropriate AABBs in the child nodes
            //   - BFS passes need centroid bounds
            //   - DFS passes need geom bounds
            //  Here we also write leaf connectivity information (prim start+count)
            //   this will be overwritten later if we are creating an inner node
            struct AABB3f left_box, right_box;
            left_box  = AABB3f_select( left_geom_bounds,  left_centroid_bounds,  v_is_bfs.xxx );
            right_box = AABB3f_select( right_geom_bounds, right_centroid_bounds, v_is_bfs.yyy );

            uint left_start  = primref_begin;
            uint right_start = primref_begin + num_left;
            BVH2_WriteLeafNode( args.bvh2, left_bvh2_root,  &left_box, left_start,  num_left, left_mask );
            BVH2_WriteLeafNode( args.bvh2, right_bvh2_root, &right_box, right_start, num_right, right_mask );

            // make input and output primref index buffers consistent in the event we're creating a leaf
            //   There should only ever be one leaf created, otherwise we'd have done a DFS pass sooner
            if (any( v_is_leaf.xy ))
            {
                uint start    = v_is_leaf.x ? left_start : right_start;
                uint num_refs = v_is_leaf.x ? num_left : num_right;

                for(uint i = 0; i < num_refs; i++)
                {
                    args.primref_index_in[start + i] = args.primref_index_out[start + i];
                }
            }
        }

        // when BFS2 finishes, we need to dispatch two child tasks.
        //   DFS dispatches can run free and do not need a context
        //   BFS dispatches need a context.
        //  In the case where both of the child nodes are BFS, the current context can immediately run one of the child dispatches
        //   and the other is spilled for an unallocated context to pick up

        uint num_dfs_dispatches = is_dfs(num_left) + is_dfs(num_right);
        if (num_dfs_dispatches)
        {
            uint dfs_pos = atomic_add_local(&SLM_dfs_dispatch_count, num_dfs_dispatches);
            if (is_dfs(num_left))
            {
                scheduler->dfs_queue.records[dfs_pos].primref_base = primref_begin;
                scheduler->dfs_queue.records[dfs_pos].num_primrefs = num_left;
                scheduler->dfs_queue.records[dfs_pos].bvh2_base = left_bvh2_root;
                scheduler->dfs_queue.records[dfs_pos].tree_depth = depth + 1;
                scheduler->dfs_queue.records[dfs_pos].batch_index = batch_index;
                dfs_pos++;
            }
            if (is_dfs(num_right))
            {
                scheduler->dfs_queue.records[dfs_pos].primref_base = primref_begin + num_left;
                scheduler->dfs_queue.records[dfs_pos].num_primrefs = num_right;
                scheduler->dfs_queue.records[dfs_pos].bvh2_base = right_bvh2_root;
                scheduler->dfs_queue.records[dfs_pos].tree_depth = depth + 1;
                scheduler->dfs_queue.records[dfs_pos].batch_index = batch_index;
            }
        }

        uint num_bfs_children = is_bfs(num_left) + is_bfs(num_right);
        if (num_bfs_children)
        {
            uint place = atomic_add_local(&SLM_local_spill_stack_size, num_bfs_children);
            if (is_bfs(num_left))
            {
                SLM_local_spill_stack[place].primref_begin = primref_begin;
                SLM_local_spill_stack[place].primref_end = primref_begin + num_left;
                SLM_local_spill_stack[place].bvh2_root = left_bvh2_root;
                SLM_local_spill_stack[place].tree_depth = depth + 1;
                SLM_local_spill_stack[place].batch_index = batch_index;
                place++;
            }
            if (is_bfs(num_right))
            {
                SLM_local_spill_stack[place].primref_begin = primref_begin + num_left;
                SLM_local_spill_stack[place].primref_end = primref_end;
                SLM_local_spill_stack[place].bvh2_root = right_bvh2_root;
                SLM_local_spill_stack[place].tree_depth = depth + 1;
                SLM_local_spill_stack[place].batch_index = batch_index;
                place++;
            }
        }
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    uint local_spill_stack_size = SLM_local_spill_stack_size;

    struct BFS1SpillStackEntry entry;
    state = VCONTEXT_STATE_UNALLOCATED;
    if (context_id < local_spill_stack_size)
    {
        // pull BFS work from the local spill stack if there's enough work there
        entry = SLM_local_spill_stack[context_id];
        state = VCONTEXT_STATE_EXECUTING;
    }
    else if ((context_id - local_spill_stack_size) < (global_spill_stack_size))
    {
        // if there isn't enough work on the local stack, consume from the global one
        uint global_pos = (global_spill_stack_size - 1) - (context_id - local_spill_stack_size);
        entry = scheduler->bfs2_spill_stack.entries[global_pos];
        state = VCONTEXT_STATE_EXECUTING;
    }

    // contexts which received work set themselves up for the next BFS1 dispatch
    uint num_bfs_wgs = 0;
    uint num_bfs_dispatches = 0;
    if (state == VCONTEXT_STATE_EXECUTING)
    {
        context->dispatch_primref_begin = entry.primref_begin;
        context->dispatch_primref_end = entry.primref_end;
        context->bvh2_root = entry.bvh2_root;
        context->tree_depth = entry.tree_depth;
        context->batch_index = entry.batch_index;

        context->num_left = 0;
        context->num_right = 0;
        context->lr_mask = 0;

        batch_index = entry.batch_index;
        num_bfs_wgs = get_num_wgs(entry.primref_end - entry.primref_begin, BFS_WG_SIZE);
        num_bfs_dispatches = 1;
    }


    if (local_spill_stack_size > BFS_NUM_VCONTEXTS)
    {
        // write out additional spills if we produced more work than we can consume
        uint excess_spills = local_spill_stack_size - BFS_NUM_VCONTEXTS;
        uint write_base = global_spill_stack_size;
        uint lid = get_local_id(0);
        if (lid < excess_spills)
            scheduler->bfs2_spill_stack.entries[write_base + lid] = SLM_local_spill_stack[BFS_NUM_VCONTEXTS + lid];

        if (lid == 0)
            scheduler->bfs2_spill_stack.size = global_spill_stack_size + excess_spills;
    }
    else if (global_spill_stack_size > 0)
    {
        // otherwise, if we consumed any spills from the global stack, update the stack size
        if (get_local_id(0) == 0)
        {
            uint global_spills_consumed = min(global_spill_stack_size, BFS_NUM_VCONTEXTS - local_spill_stack_size);
            scheduler->bfs2_spill_stack.size = global_spill_stack_size - global_spills_consumed;
        }
    }


    // Do various WG reductions..  the code below is a hand-written version of the following:
    //
    // uint bfs_dispatch_queue_pos     = work_group_scan_exclusive_add( num_bfs_dispatches );
    // uint reduce_num_bfs_wgs         = work_group_reduce_add(num_bfs_wgs);
    // uint reduce_num_bfs_dispatches  = work_group_reduce_add(num_bfs_dispatches);
    uint bfs_dispatch_queue_pos;
    uint reduce_num_bfs_dispatches;
    uint reduce_num_bfs_wgs;
    local uint partial_dispatches[SCHEDULER_WG_SIZE / SCHEDULER_SG_SIZE];
    local uint partial_wgs[SCHEDULER_WG_SIZE / SCHEDULER_SG_SIZE];
    {
        partial_dispatches[get_sub_group_id()] = sub_group_reduce_add(num_bfs_dispatches);
        partial_wgs[get_sub_group_id()] = sub_group_reduce_add(num_bfs_wgs);

        uint sg_prefix = sub_group_scan_exclusive_add(num_bfs_dispatches);

        uint prefix_dispatches = 0;
        uint total_dispatches = 0;
        uint total_wgs = 0;
        ushort lane = get_sub_group_local_id();

        barrier(CLK_LOCAL_MEM_FENCE);

        for (ushort i = 0; i < SCHEDULER_NUM_SGS; i += SCHEDULER_SG_SIZE) // this loop is intended to be fully unrolled after compilation
        {
            uint p_dispatch = partial_dispatches[i + lane];
            uint p_wg = partial_wgs[i + lane];

            prefix_dispatches += (i + lane < get_sub_group_id()) ? p_dispatch : 0;
            total_dispatches += p_dispatch;
            total_wgs += p_wg;
        }

        bfs_dispatch_queue_pos = sg_prefix + sub_group_reduce_add(prefix_dispatches);
        reduce_num_bfs_dispatches = sub_group_reduce_add(total_dispatches);
        reduce_num_bfs_wgs = sub_group_reduce_add(total_wgs);
    }

    // insert records into BFS queue
    if (num_bfs_dispatches)
    {
        scheduler->bfs_queue.wg_count[bfs_dispatch_queue_pos] = num_bfs_wgs;
        scheduler->bfs_queue.records[bfs_dispatch_queue_pos].context_id = context_id;
        scheduler->bfs_queue.records[bfs_dispatch_queue_pos].batch_index = batch_index;
    }


    // store modified vcontext state if it has changed
    if (initial_state != state)
        scheduler->vcontext_state[context_id] = state;


    // store workgroup counters
    if (get_local_id(0) == 0)
    {
        scheduler->bfs_queue.num_dispatches = reduce_num_bfs_dispatches;
        scheduler->num_bfs_wgs = reduce_num_bfs_wgs;
        scheduler->num_dfs_wgs = SLM_dfs_dispatch_count;
    }

    const uint contexts_to_clear = min( (uint)BFS_NUM_VCONTEXTS, (uint)(local_spill_stack_size+global_spill_stack_size) );

    for ( uint i = get_sub_group_id(); i < contexts_to_clear; i += get_num_sub_groups() )
        BinInfo_init_subgroup( &scheduler->contexts[i].global_bin_info );

    for ( uint i = get_sub_group_id(); i < contexts_to_clear; i += get_num_sub_groups() )
        LRBounds_init_subgroup( &scheduler->contexts[i].lr_bounds );
}

#if 0
uint record_search( struct BFSDispatchRecord* record_out, global struct BFSDispatchQueue* queue )
{
    uint group = get_group_id(0);
    ushort lane = get_sub_group_local_id();
    uint num_dispatches = queue->num_dispatches;
    uint base = 0;
    for (uint i = 0; i < num_dispatches; i += get_sub_group_size())
    {
        uint counts = intel_sub_group_block_read(&queue->wg_count[i]);

        for (uint j = 0; j < get_sub_group_size(); j++)
        {
            uint n = sub_group_broadcast(counts, j);
            if (group < n)
            {
                *record_out = queue->records[i + j];
                return group;
            }
            group -= n;
        }
    }

    return 0; // NOTE: unreachable in practice
}
#endif


uint record_search(struct BFSDispatchRecord* record_out, global struct BFSDispatchQueue* queue)
{
    uint group = get_group_id(0);

    uint num_dispatches = queue->num_dispatches;

    uint dispatch_id = 0;
    uint local_id = 0;
    uint i = 0;
    do
    {
        uint counts = intel_sub_group_block_read(&queue->wg_count[i]);
        uint prefix = sub_group_scan_exclusive_add(counts);

        uint g = group - prefix;
        uint ballot = intel_sub_group_ballot(g < counts);
        if (ballot)
        {
            uint lane = ctz(ballot);
            dispatch_id = i + lane;
            local_id = intel_sub_group_shuffle(g, lane);
            break;
        }

        group -= sub_group_broadcast(prefix + counts, get_sub_group_size() - 1);

        i += get_sub_group_size();
    } while (i < num_dispatches);


    *record_out = queue->records[dispatch_id];
    return local_id;
}




struct BFSDispatchArgs get_bfs_args(struct BFSDispatchRecord* record, global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* globals, uint local_group_id)
{
    uint context_id = record->context_id;
    struct VContext* context = &scheduler->contexts[context_id];
    bool odd_pass = context->tree_depth & 1;

    struct BFSDispatchArgs args;
    args.scheduler              = scheduler;
    args.primref_index_in       = SAHBuildGlobals_GetPrimrefIndices_In( globals, odd_pass );
    args.primref_index_out      = SAHBuildGlobals_GetPrimrefIndices_Out( globals, odd_pass );
    args.primref_buffer         = SAHBuildGlobals_GetPrimrefs( globals );
    args.wg_primref_begin       = context->dispatch_primref_begin + local_group_id * BFS_WG_SIZE;
    args.wg_primref_end         = min( args.wg_primref_begin + BFS_WG_SIZE, context->dispatch_primref_end );
    args.dispatch_primref_begin = context->dispatch_primref_begin;
    args.dispatch_primref_end   = context->dispatch_primref_end;
    args.context_id             = context_id;
    args.context                = &scheduler->contexts[context_id];
    args.num_wgs                = ((args.dispatch_primref_end - args.dispatch_primref_begin) + BFS_WG_SIZE - 1) / BFS_WG_SIZE;
    args.bvh2_root              = context->bvh2_root;
    args.bvh2 = SAHBuildGlobals_GetBVH2( globals );
    args.global_num_primrefs = SAHBuildGlobals_GetTotalPrimRefs( globals );
    args.do_mask_processing = SAHBuildGlobals_NeedMasks( globals );
    return args;
}

struct BFSDispatchArgs get_bfs_args_queue( global struct BFSDispatchQueue* queue,
                                           global struct VContextScheduler* scheduler,
                                           global struct SAHBuildGlobals* globals )
{

    // TODO_OPT:  Load this entire prefix array into SLM instead of searching..
    //    Or use sub-group ops

    struct BFSDispatchRecord record;
    uint local_group_id = record_search(&record, queue);

    return get_bfs_args(&record, scheduler, globals, local_group_id);
}


struct BFSDispatchArgs get_bfs_args_from_record( struct BFSDispatchRecord* record,
                                           global struct VContextScheduler* scheduler,
                                           global struct SAHBuildGlobals* globals )
{
    return get_bfs_args(record, scheduler, globals, 0);
}


struct BFSDispatchArgs get_bfs_args_batchable(
    global struct BFSDispatchQueue* queue,
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* globals_buffer )
{

    // TODO_OPT:  Load this entire prefix array into SLM instead of searching..
    //    Or use sub-group ops

    struct BFSDispatchRecord record;
    uint local_group_id = record_search(&record, queue);

    global struct SAHBuildGlobals* globals = globals_buffer + record.batch_index;

    return get_bfs_args(&record, scheduler, globals, local_group_id);
}


struct BFSDispatchArgs get_bfs_args_from_record_batchable(
    struct BFSDispatchRecord* record,
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* globals_buffer )
{
    global struct SAHBuildGlobals* globals = globals_buffer + record->batch_index;

    return get_bfs_args(record, scheduler, globals, 0);
}

struct BFSDispatchArgs get_bfs_args_initial( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* globals )
{
    uint context_id = 0;

    uint num_refs = SAHBuildGlobals_GetTotalPrimRefs( globals );

    struct BFSDispatchArgs args;
    args.scheduler = scheduler;
    args.primref_index_in   = SAHBuildGlobals_GetPrimrefIndices_In( globals, false );
    args.primref_index_out  = SAHBuildGlobals_GetPrimrefIndices_Out( globals, false );
    args.primref_buffer     = SAHBuildGlobals_GetPrimrefs( globals );
    args.wg_primref_begin   = get_group_id(0) * BFS_WG_SIZE;
    args.wg_primref_end     = min( args.wg_primref_begin + BFS_WG_SIZE, num_refs );
    args.dispatch_primref_begin = 0;
    args.dispatch_primref_end   = num_refs;
    args.context_id = context_id;
    args.context = &scheduler->contexts[context_id];
    args.num_wgs = ((args.dispatch_primref_end - args.dispatch_primref_begin) + BFS_WG_SIZE - 1) / BFS_WG_SIZE;
    args.bvh2 = SAHBuildGlobals_GetBVH2( globals );
    args.bvh2_root = BVH2_GetRoot( args.bvh2 );
    args.global_num_primrefs = SAHBuildGlobals_GetTotalPrimRefs( globals );
    args.do_mask_processing = SAHBuildGlobals_NeedMasks(globals);
    return args;
}


inline void BinMapping_init( struct BinMapping* binMapping, struct AABB3f* centBounds, const uint bins )
{
    const float4 eps = 1E-34f;
    const float4 omega = 1E+34f;
    float3 l = AABB3f_load_lower( centBounds );
    float3 u = AABB3f_load_upper( centBounds );
    float4 diag;
    diag.xyz = max( eps.xyz, u - l );
    diag.w = 0;
    float4 scale = (float4)(0.99f * (float)bins) / diag;
    scale = select( (float4)(0.0f), scale, diag > eps );
    scale = select( (float4)(0.0f), scale, diag < omega );
    binMapping->scale = scale;
    binMapping->ofs.xyz = l.xyz;
    binMapping->ofs.w = 0;
}


inline ulong getBestSplit( float3 sah, uint ID, const float4 scale, const ulong defaultSplit )
{
    ulong splitX = (((ulong)as_uint( sah.x )) << 32) | ((uint)ID << 2) | 0;
    ulong splitY = (((ulong)as_uint( sah.y )) << 32) | ((uint)ID << 2) | 1;
    ulong splitZ = (((ulong)as_uint( sah.z )) << 32) | ((uint)ID << 2) | 2;
    /* ignore zero sized dimensions */
    splitX = select( splitX, defaultSplit, (ulong)(scale.x == 0) );
    splitY = select( splitY, defaultSplit, (ulong)(scale.y == 0) );
    splitZ = select( splitZ, defaultSplit, (ulong)(scale.z == 0) );
    ulong bestSplit = min( min( splitX, splitY ), splitZ );
    bestSplit = sub_group_reduce_min( bestSplit );
    return bestSplit;
}



inline float left_to_right_area16( struct AABB3f* low )
{
    struct AABB3f low_prefix = AABB3f_sub_group_scan_exclusive_min_max( low );
    return halfArea_AABB3f( &low_prefix );
}

inline uint left_to_right_counts16( uint low )
{
    return sub_group_scan_exclusive_add( low );
}

inline float right_to_left_area16( struct AABB3f* low )
{
    const uint subgroupLocalID = get_sub_group_local_id();
    const uint subgroup_size = get_sub_group_size();
    const uint ID = subgroup_size - 1 - subgroupLocalID;
    struct AABB3f low_reverse = AABB3f_sub_group_shuffle( low, ID );
    struct AABB3f low_prefix = AABB3f_sub_group_scan_inclusive_min_max( &low_reverse );
    const float low_area = intel_sub_group_shuffle( halfArea_AABB3f( &low_prefix ), ID );
    return low_area;
}

inline uint right_to_left_counts16( uint low )
{
    const uint subgroupLocalID = get_sub_group_local_id();
    const uint subgroup_size = get_sub_group_size();
    const uint ID = subgroup_size - 1 - subgroupLocalID;
    const uint low_reverse = intel_sub_group_shuffle( low, ID );
    const uint low_prefix = sub_group_scan_inclusive_add( low_reverse );
    return intel_sub_group_shuffle( low_prefix, ID );
}

inline float2 left_to_right_area32( struct AABB3f* low, struct AABB3f* high )
{
    struct AABB3f low_prefix = AABB3f_sub_group_scan_exclusive_min_max( low );
    struct AABB3f low_reduce = AABB3f_sub_group_reduce( low );
    struct AABB3f high_prefix = AABB3f_sub_group_scan_exclusive_min_max( high );
    AABB3f_extend( &high_prefix, &low_reduce );
    const float low_area = halfArea_AABB3f( &low_prefix );
    const float high_area = halfArea_AABB3f( &high_prefix );
    return (float2)(low_area, high_area);
}

inline uint2 left_to_right_counts32( uint low, uint high )
{
    const uint low_prefix = sub_group_scan_exclusive_add( low );
    const uint low_reduce = sub_group_reduce_add( low );
    const uint high_prefix = sub_group_scan_exclusive_add( high );
    return (uint2)(low_prefix, low_reduce + high_prefix);
}

inline float2 right_to_left_area32( struct AABB3f* low, struct AABB3f* high )
{
    const uint subgroupLocalID = get_sub_group_local_id();
    const uint subgroup_size = get_sub_group_size();
    const uint ID = subgroup_size - 1 - subgroupLocalID;
    struct AABB3f low_reverse = AABB3f_sub_group_shuffle( high, ID );
    struct AABB3f high_reverse = AABB3f_sub_group_shuffle( low, ID );
    struct AABB3f low_prefix = AABB3f_sub_group_scan_inclusive_min_max( &low_reverse );
    struct AABB3f low_reduce = AABB3f_sub_group_reduce( &low_reverse );
    struct AABB3f high_prefix = AABB3f_sub_group_scan_inclusive_min_max( &high_reverse );
    AABB3f_extend( &high_prefix, &low_reduce );
    const float low_area = intel_sub_group_shuffle( halfArea_AABB3f( &high_prefix ), ID );
    const float high_area = intel_sub_group_shuffle( halfArea_AABB3f( &low_prefix ), ID );
    return (float2)(low_area, high_area);
}

inline uint2 right_to_left_counts32( uint low, uint high )
{
    const uint subgroupLocalID = get_sub_group_local_id();
    const uint subgroup_size = get_sub_group_size();
    const uint ID = subgroup_size - 1 - subgroupLocalID;
    const uint low_reverse = intel_sub_group_shuffle( high, ID );
    const uint high_reverse = intel_sub_group_shuffle( low, ID );
    const uint low_prefix = sub_group_scan_inclusive_add( low_reverse );
    const uint low_reduce = sub_group_reduce_add( low_reverse );
    const uint high_prefix = sub_group_scan_inclusive_add( high_reverse ) + low_reduce;
    return (uint2)(intel_sub_group_shuffle( high_prefix, ID ), intel_sub_group_shuffle( low_prefix, ID ));
}

inline uint fastDivideBy6_uint( uint v )
{
#if 1
    const ulong u = (ulong)v >> 1;
    return (uint)((u * 0x55555556ul) >> 32);
#else
    return v / 6;
#endif
}

inline uint3 fastDivideBy6_uint3( uint3 v )
{
    return (uint3)(fastDivideBy6_uint( v.x ), fastDivideBy6_uint( v.y ), fastDivideBy6_uint( v.z ));
}

#define SAH_LOG_BLOCK_SHIFT 2

inline struct BFS_Split BinInfo_reduce( struct BFS_BinInfo* binInfo, const float4 scale )
{
    const uint subgroupLocalID = get_sub_group_local_id();
    const uint subgroup_size = get_sub_group_size();

    struct AABB3f boundsX = BinInfo_get_AABB( binInfo, subgroupLocalID, 0 );

    const float lr_areaX = left_to_right_area16( &boundsX );
    const float rl_areaX = right_to_left_area16( &boundsX );

    struct AABB3f boundsY = BinInfo_get_AABB( binInfo, subgroupLocalID, 1 );

    const float lr_areaY = left_to_right_area16( &boundsY );
    const float rl_areaY = right_to_left_area16( &boundsY );

    struct AABB3f boundsZ = BinInfo_get_AABB( binInfo, subgroupLocalID, 2 );

    const float lr_areaZ = left_to_right_area16( &boundsZ );
    const float rl_areaZ = right_to_left_area16( &boundsZ );

    const uint3 counts = BinInfo_get_counts( binInfo, subgroupLocalID );

    const uint lr_countsX = left_to_right_counts16( counts.x );
    const uint rl_countsX = right_to_left_counts16( counts.x );
    const uint lr_countsY = left_to_right_counts16( counts.y );
    const uint rl_countsY = right_to_left_counts16( counts.y );
    const uint lr_countsZ = left_to_right_counts16( counts.z );
    const uint rl_countsZ = right_to_left_counts16( counts.z );

    const float3 lr_area = (float3)(lr_areaX, lr_areaY, lr_areaZ);
    const float3 rl_area = (float3)(rl_areaX, rl_areaY, rl_areaZ);

    const uint3 lr_count = fastDivideBy6_uint3( (uint3)(lr_countsX, lr_countsY, lr_countsZ) + 6 - 1 );
    const uint3 rl_count = fastDivideBy6_uint3( (uint3)(rl_countsX, rl_countsY, rl_countsZ) + 6 - 1 );
    float3 sah = fma( lr_area, convert_float3( lr_count ), rl_area * convert_float3( rl_count ) );

    /* first bin is invalid */
    sah.x = select( (float)(INFINITY), sah.x, subgroupLocalID != 0 );
    sah.y = select( (float)(INFINITY), sah.y, subgroupLocalID != 0 );
    sah.z = select( (float)(INFINITY), sah.z, subgroupLocalID != 0 );

    const ulong defaultSplit = (((ulong)as_uint( (float)(INFINITY) )) << 32);

    const ulong bestSplit = getBestSplit( sah, subgroupLocalID, scale, defaultSplit );

    struct BFS_Split split;
    split.sah = as_float( (uint)(bestSplit >> 32) );
    split.dim = (uint)bestSplit & 3;
    split.pos = (uint)bestSplit >> 2;

    return split;
}


struct BFS_BinInfoReduce3_SLM
{
    uint sah[3*BFS_NUM_BINS];
};



inline struct BFS_Split BinInfo_reduce3( local struct BFS_BinInfoReduce3_SLM* slm, struct BFS_BinInfo* binInfo, const float4 scale )
{
    // process each bin/axis combination across sub-groups
    for (uint i = get_sub_group_id(); i < 3 * BFS_NUM_BINS; i += get_num_sub_groups())
    {
        uint my_bin  = i % BFS_NUM_BINS;
        uint my_axis = i / BFS_NUM_BINS;

        float3 left_lower  = (float3)(INFINITY,INFINITY,INFINITY);
        float3 left_upper  = -left_lower;
        float3 right_lower = (float3)(INFINITY,INFINITY,INFINITY);
        float3 right_upper = -right_lower;

        // load the other bins and assign them to the left or to the right
        //  of this subgroup's bin
        uint lane = get_sub_group_local_id();
        struct AABB3f sg_bins = BinInfo_get_AABB(binInfo,lane,my_axis);

        bool is_left = (lane < my_bin);
        float3 lower = AABB3f_load_lower(&sg_bins);
        float3 upper = AABB3f_load_upper(&sg_bins);

        float3 lower_l = select_min( lower, is_left  );
        float3 upper_l = select_max( upper, is_left  );
        float3 lower_r = select_min( lower, !is_left );
        float3 upper_r = select_max( upper, !is_left );

        lower_l = sub_group_reduce_min_float3( lower_l );
        lower_r = sub_group_reduce_min_float3( lower_r );
        upper_l = sub_group_reduce_max_float3( upper_l );
        upper_r = sub_group_reduce_max_float3( upper_r );
        float3 dl = upper_l - lower_l;
        float3 dr = upper_r - lower_r;
        float area_l =  dl.x* (dl.y + dl.z) + (dl.y * dl.z);
        float area_r =  dr.x* (dr.y + dr.z) + (dr.y * dr.z);

        // get the counts
        uint sg_bin_count = BinInfo_get_count(binInfo, lane, my_axis);
        uint count_l = (is_left) ?  sg_bin_count : 0;
        uint count_r = (is_left) ?  0 : sg_bin_count;
        count_l = sub_group_reduce_add(count_l);
        count_r = sub_group_reduce_add(count_r);

        // compute sah
        count_l = fastDivideBy6_uint(count_l + 6 - 1);
        count_r = fastDivideBy6_uint(count_r + 6 - 1);
        float lr_partial = area_l * count_l;
        float rl_partial = area_r * count_r;
        float sah = lr_partial + rl_partial;

        // first bin is invalid
        sah = select((float)(INFINITY), sah, my_bin != 0);

        // ignore zero sized dimensions
        sah = select( sah, (float)(INFINITY), (scale.x == 0 && my_axis == 0) );
        sah = select( sah, (float)(INFINITY), (scale.y == 0 && my_axis == 1) );
        sah = select( sah, (float)(INFINITY), (scale.z == 0 && my_axis == 2) );

        // tuck the axis into the bottom bits of sah cost.
        //  The result is an integer between 0 and +inf (7F800000)
        //  If we have 3 axes with infinite sah cost, we will select axis 0
        slm->sah[i] = (as_uint(sah)&~0x3) | my_axis;
    }

    barrier( CLK_LOCAL_MEM_FENCE );

    // reduce split candidates down to one subgroup
    //  sah is strictly positive, so integer compares can be used
    //   which results in a faster sub_group_reduce_min()
    //
    uint best_sah = 0xffffffff;

    uint lid = get_sub_group_local_id();
    if (lid < BFS_NUM_BINS)
    {
        best_sah = slm->sah[lid];
        lid += BFS_NUM_BINS;
        best_sah = min( best_sah, slm->sah[lid] );
        lid += BFS_NUM_BINS;
        best_sah = min( best_sah, slm->sah[lid] );
    }

    uint reduced_bestsah = sub_group_reduce_min( best_sah );
    uint best_bin = ctz(intel_sub_group_ballot(best_sah == reduced_bestsah));
    uint best_axis = as_uint(reduced_bestsah) & 0x3;

    struct BFS_Split ret;
    ret.sah = as_float(reduced_bestsah);
    ret.dim = best_axis;
    ret.pos = best_bin;
    return ret;
}


struct BFS_BinInfoReduce_SLM
{
    struct
    {
        float sah;
        uint bin;
    } axisInfo[3];
};



inline struct BFS_Split BinInfo_reduce2( local struct BFS_BinInfoReduce_SLM* slm, struct BFS_BinInfo* binInfo, const float4 scale, uint num_primrefs)
{
    ushort my_axis = get_sub_group_id();
    ushort my_bin  = get_sub_group_local_id();

    if (my_axis < 3)
    {
        struct AABB3f aabb = BinInfo_get_AABB(binInfo, my_bin, my_axis);
        uint count         = BinInfo_get_count(binInfo, my_bin, my_axis);

        float lr_area = left_to_right_area16(&aabb);
        float rl_area = right_to_left_area16(&aabb);

        uint lr_count = sub_group_scan_exclusive_add(count);
        uint rl_count = num_primrefs - lr_count;

        lr_count = fastDivideBy6_uint(lr_count + 6 - 1);
        rl_count = fastDivideBy6_uint(rl_count + 6 - 1);
        float lr_partial = lr_area * lr_count;
        float rl_partial = rl_area * rl_count;
        float sah = lr_partial + rl_partial;

        // first bin is invalid
        sah = select((float)(INFINITY), sah, my_bin != 0);

        float best_sah = sub_group_reduce_min( sah );
        uint best_bin = ctz(intel_sub_group_ballot(sah == best_sah));

        // ignore zero sized dimensions
        best_sah = select( best_sah, (float)(INFINITY), (scale.x == 0 && my_axis == 0) );
        best_sah = select( best_sah, (float)(INFINITY), (scale.y == 0 && my_axis == 1) );
        best_sah = select( best_sah, (float)(INFINITY), (scale.z == 0 && my_axis == 2) );

        if (get_sub_group_local_id() == 0)
        {
            slm->axisInfo[my_axis].sah = best_sah;
            slm->axisInfo[my_axis].bin = best_bin;
        }
    }
    barrier( CLK_LOCAL_MEM_FENCE );

    float sah = (float)(INFINITY);
    if( get_sub_group_local_id() < 3 )
        sah = slm->axisInfo[get_sub_group_local_id()].sah;

    float bestsah = min(sub_group_broadcast(sah, 0), min(sub_group_broadcast(sah, 1), sub_group_broadcast(sah, 2)));
    uint bestAxis = ctz( intel_sub_group_ballot(bestsah == sah) );

    struct BFS_Split split;
    split.sah = bestsah;
    split.dim = bestAxis;
    split.pos = slm->axisInfo[bestAxis].bin;
    return split;
}


inline bool is_left( struct BinMapping* binMapping, struct BFS_Split* split, struct AABB* primref )
{
    const uint dim = split->dim;
    const float lower = primref->lower[dim];
    const float upper = primref->upper[dim];
    const float c = lower + upper;
    const uint pos = convert_uint_rtz( (c - binMapping->ofs[dim]) * binMapping->scale[dim] );
    return pos < split->pos;
}

struct BFS_Pass1_SLM
{
    struct BFS_BinInfo bin_info;
//    struct BFS_BinInfoReduce3_SLM reduce3;
};


void DO_BFS_pass1( local struct BFS_Pass1_SLM*  slm,
                   uint thread_primref_id,
                   bool thread_primref_valid,
                   struct BFSDispatchArgs args
                  )
{
    local struct BFS_BinInfo* local_bin_info = &slm->bin_info;
    global struct VContext* context  = args.context;
    struct AABB3f centroid_bounds    = BVH2_GetNodeBox( args.bvh2, args.bvh2_root ); // root AABB is initialized to centroid bounds

    struct BinMapping bin_mapping;
    BinMapping_init( &bin_mapping, &centroid_bounds, BFS_NUM_BINS );

    // fetch this thread's primref
    PrimRef ref;
    if ( thread_primref_valid )
        ref = args.primref_buffer[thread_primref_id];

    // init bin info
    BinInfo_init( local_bin_info );

    // fence on local bin-info init
    barrier( CLK_LOCAL_MEM_FENCE );

    // merge this thread's primref into local bin info
    BinInfo_add_primref( &bin_mapping, local_bin_info, &ref, thread_primref_valid );

    // fence on local bin-info update
    barrier( CLK_LOCAL_MEM_FENCE );

    BinInfo_merge(&context->global_bin_info, local_bin_info);
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size(BFS_WG_SIZE,1,1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass1_indexed(
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* sah_globals )
{
    local struct BFS_Pass1_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_queue( &scheduler->bfs_queue, scheduler, sah_globals );

    bool thread_primref_valid = (args.wg_primref_begin + get_local_id( 0 )) < args.wg_primref_end;
    uint thread_primref_id = 0;
    if ( thread_primref_valid )
        thread_primref_id = args.primref_index_in[args.wg_primref_begin + get_local_id( 0 )];

    DO_BFS_pass1( &slm, thread_primref_id, thread_primref_valid, args );
}


__attribute__( (reqd_work_group_size( BFS_WG_SIZE, 1, 1 )) )
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass1_initial( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* sah_globals )
{
    local struct BFS_Pass1_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_initial( scheduler, sah_globals );

    uint thread_primref_id    = args.wg_primref_begin + get_local_id( 0 );
    bool thread_primref_valid = thread_primref_id < args.wg_primref_end;

    DO_BFS_pass1( &slm, thread_primref_id, thread_primref_valid, args );
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(BFS_WG_SIZE, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass1_indexed_batchable(
    global struct VContextScheduler* scheduler,
    global struct SAHBuildGlobals* globals_buffer )
{
    local struct BFS_Pass1_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_batchable( &scheduler->bfs_queue, scheduler, globals_buffer );

    bool thread_primref_valid = (args.wg_primref_begin + get_local_id(0)) < args.wg_primref_end;
    uint thread_primref_id = 0;
    if (thread_primref_valid)
        thread_primref_id = args.primref_index_in[args.wg_primref_begin + get_local_id(0)];

    DO_BFS_pass1(&slm, thread_primref_id, thread_primref_valid, args);
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(BFS_WG_SIZE, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass1_initial_batchable( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* globals_buffer )
{
    local struct BFS_Pass1_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_batchable( &scheduler->bfs_queue, scheduler, globals_buffer );

    uint thread_primref_id = args.wg_primref_begin + get_local_id(0);
    bool thread_primref_valid = thread_primref_id < args.wg_primref_end;

    DO_BFS_pass1(&slm, thread_primref_id, thread_primref_valid, args);
}


/////////////////////////////////////////////////////////////////////////////////////////////////
///
///        BVH2 construction -- BFS Phase Pass2
///
/////////////////////////////////////////////////////////////////////////////////////////////////

struct BFS_Pass2_SLM
{
    struct BFS_BinInfoReduce3_SLM reduce3;
    //struct AABB3f left_centroid_bounds;
    //struct AABB3f right_centroid_bounds;
    //struct AABB3f left_geom_bounds;
    //struct AABB3f right_geom_bounds;
    LRBounds lr_bounds;
    uint left_count;
    uint right_count;
    uint lr_mask;
    uint left_primref_base;
    uint right_primref_base;
//    uint num_wgs;

//    uint output_indices[BFS_WG_SIZE];
};







void DO_BFS_pass2(
    local struct BFS_Pass2_SLM* slm,
    uint thread_primref_id,
    bool thread_primref_valid,
    struct BFSDispatchArgs args
)
{
    global struct VContext* context = args.context;

    struct AABB3f centroid_bounds = BVH2_GetNodeBox( args.bvh2, args.bvh2_root );

    // load the thread's primref
    PrimRef ref;
    if ( thread_primref_valid )
        ref = args.primref_buffer[thread_primref_id];

    struct BinMapping bin_mapping;
    BinMapping_init( &bin_mapping, &centroid_bounds, BFS_NUM_BINS );

    // initialize working SLM space
    LRBounds_init(&slm->lr_bounds);
    if(get_local_id(0) == 0)
    {
        slm->left_count  = 0;
        slm->right_count = 0;

        if( args.do_mask_processing )
            slm->lr_mask = 0;
    }

    // compute split - every workgroup does the same computation
    // local barrier inside BinInfo_reduce3
    struct BFS_Split split = BinInfo_reduce3( &slm->reduce3, &context->global_bin_info,bin_mapping.scale );

    uint wg_prim_count = args.wg_primref_end - args.wg_primref_begin;

    // partition primrefs into L/R subsets...
    bool go_left = false;
    if (split.sah == (float)(INFINITY))      // no valid split, split in the middle.. This can happen due to floating-point limit cases in huge scenes
        go_left = get_local_id(0) < (wg_prim_count / 2);
    else
        go_left = is_left( &bin_mapping, &split, &ref );

    // assign this primref a position in the output array, and expand corresponding centroid-bounds
    uint local_index;
    {
        float3 centroid = ref.lower.xyz + ref.upper.xyz;

        uint l_ballot = intel_sub_group_ballot(  go_left && thread_primref_valid );
        uint r_ballot = intel_sub_group_ballot( !go_left && thread_primref_valid );
        if (l_ballot)
        {
            bool active_lane = l_ballot & (1 << get_sub_group_local_id());
            float3 Cmin, Cmax, Gmin, Gmax;
            Cmin = select_min( centroid.xyz, active_lane );
            Cmax = select_max( centroid.xyz, active_lane );
            Gmin = select_min( ref.lower.xyz, active_lane );
            Gmax = select_max( ref.upper.xyz, active_lane );

            Cmin = sub_group_reduce_min_float3( Cmin );
            Cmax = sub_group_reduce_max_float3( Cmax );
            Gmin = sub_group_reduce_min_float3( Gmin );
            Gmax = sub_group_reduce_max_float3( Gmax );

            LRBounds_merge_left( &slm->lr_bounds, Cmin,Cmax,Gmin,Gmax );
        }

        if (r_ballot)
        {
            bool active_lane = r_ballot & (1 << get_sub_group_local_id());
            float3 Cmin, Cmax, Gmin, Gmax;
            Cmin = select_min(centroid.xyz, active_lane);
            Cmax = select_max(centroid.xyz, active_lane);
            Gmin = select_min(ref.lower.xyz, active_lane);
            Gmax = select_max(ref.upper.xyz, active_lane);

            Cmin = sub_group_reduce_min_float3(Cmin);
            Cmax = sub_group_reduce_max_float3(Cmax);
            Gmin = sub_group_reduce_min_float3(Gmin);
            Gmax = sub_group_reduce_max_float3(Gmax);

            LRBounds_merge_right( &slm->lr_bounds, Cmin,Cmax,Gmin,Gmax );
        }

        if( args.do_mask_processing )
        {
            uint mask =0;
            if (thread_primref_valid)
            {
                mask = PRIMREF_instanceMask(&ref) ;
                mask = go_left  ? mask : mask<<8;
            }

            // TODO OPT:  there is no 'sub_group_reduce_or'  and IGC does not do the reduction trick
            //   for atomics on sub-group uniform addresses
            for( uint i= get_sub_group_size()/2; i>0; i/= 2)
                mask = mask | intel_sub_group_shuffle_down(mask,mask,i);
            if( get_sub_group_local_id() == 0 )
                atomic_or_local( &slm->lr_mask, mask );
        }

        uint l_base = 0;
        uint r_base = 0;
        if( get_sub_group_local_id() == 0 && l_ballot )
            l_base = atomic_add_local( &slm->left_count, popcount(l_ballot) );
        if( get_sub_group_local_id() == 0 && r_ballot )
            r_base = atomic_add_local( &slm->right_count, popcount(r_ballot) );

        sub_group_barrier( CLK_LOCAL_MEM_FENCE );
        l_base = sub_group_broadcast(l_base,0);
        r_base = sub_group_broadcast(r_base,0);

        l_base = l_base + subgroup_bit_prefix_exclusive( l_ballot );
        r_base = r_base + subgroup_bit_prefix_exclusive( r_ballot );

        local_index = (go_left) ? l_base : r_base;
    }


    barrier( CLK_LOCAL_MEM_FENCE );

    // merge local into global
    // TODO_OPT:  Look at spreading some of this across subgroups
    if ( get_sub_group_id() == 0 )
    {
        // allocate primref space for this wg and merge local/global centroid bounds
        uint num_left  = slm->left_count;
        {
            if (num_left && get_sub_group_local_id() == 0)
            {
                num_left = atomic_add_global( &context->num_left, num_left );
                slm->left_primref_base = args.dispatch_primref_begin + num_left;
            }
        }
        uint num_right = slm->right_count;
        {
            if (num_right && get_sub_group_local_id() == 0)
            {
                num_right = atomic_add_global( &context->num_right, num_right );
                slm->right_primref_base = (args.dispatch_primref_end - 1) - num_right;
            }
        }

        if( args.do_mask_processing && get_sub_group_local_id() == 0 )
            atomic_or_global( &context->lr_mask, slm->lr_mask );
    }

    barrier( CLK_LOCAL_MEM_FENCE );

    LRBounds_merge( &context->lr_bounds, &slm->lr_bounds );

    // move thread's primref ID into correct position in output index buffer
    if (thread_primref_valid)
    {
        uint pos = go_left ? slm->left_primref_base + local_index
            : slm->right_primref_base - local_index;

        args.primref_index_out[pos] = thread_primref_id;
    }
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( BFS_WG_SIZE, 1, 1 )) )
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
BFS_pass2_indexed( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* sah_globals )
{
    local struct BFS_Pass2_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_queue( &scheduler->bfs_queue, scheduler, sah_globals );

    bool thread_primref_valid = (args.wg_primref_begin + get_local_id( 0 )) < args.wg_primref_end;
    uint thread_primref_id = 0;
    if ( thread_primref_valid )
        thread_primref_id = args.primref_index_in[args.wg_primref_begin + get_local_id( 0 )];

    DO_BFS_pass2( &slm, thread_primref_id, thread_primref_valid, args );
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( BFS_WG_SIZE, 1, 1 )) )
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
BFS_pass2_initial( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* sah_globals )
{
    local struct BFS_Pass2_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_initial( scheduler, sah_globals );

    uint thread_primref_id    = args.wg_primref_begin + get_local_id( 0 );
    bool thread_primref_valid = thread_primref_id < args.wg_primref_end;

    DO_BFS_pass2( &slm, thread_primref_id, thread_primref_valid, args );
}


__attribute__((reqd_work_group_size(BFS_WG_SIZE, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass2_indexed_batchable( global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* globals_buffer )
{
    local struct BFS_Pass2_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_batchable(&scheduler->bfs_queue, scheduler, globals_buffer );

    bool thread_primref_valid = (args.wg_primref_begin + get_local_id(0)) < args.wg_primref_end;
    uint thread_primref_id = 0;
    if (thread_primref_valid)
        thread_primref_id = args.primref_index_in[args.wg_primref_begin + get_local_id(0)];

    DO_BFS_pass2(&slm, thread_primref_id, thread_primref_valid, args);

}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(BFS_WG_SIZE, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
BFS_pass2_initial_batchable(global struct VContextScheduler* scheduler, global struct SAHBuildGlobals* globals_buffer)
{
    local struct BFS_Pass2_SLM slm;
    struct BFSDispatchArgs args = get_bfs_args_batchable(&scheduler->bfs_queue, scheduler, globals_buffer );

    uint thread_primref_id = args.wg_primref_begin + get_local_id(0);
    bool thread_primref_valid = thread_primref_id < args.wg_primref_end;

    DO_BFS_pass2(&slm, thread_primref_id, thread_primref_valid, args);
}




/////////////////////////////////////////////////////////////////////////////////////////////////
///
///        BVH2 construction -- DFS Phase
///
/////////////////////////////////////////////////////////////////////////////////////////////////

struct DFSArgs
{
    uint primref_base;
    uint global_bvh2_base;
    bool do_mask_processing;
    ushort num_primrefs;
    global uint* primref_indices_in;
    global uint* primref_indices_out;
    global PrimRef* primref_buffer;
    global struct BVH2* global_bvh2;
};


struct DFSPrimRefAABB
{
    half lower[3];
    half upper[3];
};

void DFSPrimRefAABB_init( struct DFSPrimRefAABB* bb )
{
    bb->lower[0] = 1;
    bb->lower[1] = 1;
    bb->lower[2] = 1;
    bb->upper[0] = 0;
    bb->upper[1] = 0;
    bb->upper[2] = 0;
}

void DFSPrimRefAABB_extend( struct DFSPrimRefAABB* aabb, struct DFSPrimRefAABB* v )
{
    aabb->lower[0] = min( aabb->lower[0], v->lower[0] );
    aabb->lower[1] = min( aabb->lower[1], v->lower[1] );
    aabb->lower[2] = min( aabb->lower[2], v->lower[2] );
    aabb->upper[0] = max( aabb->upper[0], v->upper[0] );
    aabb->upper[1] = max( aabb->upper[1], v->upper[1] );
    aabb->upper[2] = max( aabb->upper[2], v->upper[2] );
}

half DFSPrimRefAABB_halfArea( struct DFSPrimRefAABB* aabb )
{
    const half3 d = (half3)(aabb->upper[0] - aabb->lower[0], aabb->upper[1] - aabb->lower[1], aabb->upper[2] - aabb->lower[2]);
    return fma( d.x, (d.y + d.z), d.y * d.z );
}

struct DFSPrimRef
{
    struct DFSPrimRefAABB aabb;
    ushort2 meta;
};

void DFSPrimRef_SetBVH2Root( struct DFSPrimRef* ref, ushort root )
{
    ref->meta.y = root;
}

uint DFSPrimRef_GetInputIndex( struct DFSPrimRef* ref )
{
    return ref->meta.x;
}

uint DFSPrimRef_GetBVH2Parent( struct DFSPrimRef* ref )
{
    return ref->meta.y;
}


struct PrimRefSet
{
    struct DFSPrimRefAABB AABB[DFS_WG_SIZE];
    ushort2 meta[DFS_WG_SIZE];
    uint input_indices[DFS_WG_SIZE];
};




local struct DFSPrimRefAABB* PrimRefSet_GetAABBPointer( local struct PrimRefSet* refs, ushort id )
{
    return &refs->AABB[id];
}
struct DFSPrimRef PrimRefSet_GetPrimRef( local struct PrimRefSet* refs, ushort id )
{
    struct DFSPrimRef r;
    r.aabb = refs->AABB[id];
    r.meta = refs->meta[id];
    return r;
}
void PrimRefSet_SetPrimRef( local struct PrimRefSet* refs, struct DFSPrimRef ref, ushort id )
{
    refs->AABB[id] = ref.aabb;
    refs->meta[id] = ref.meta;
}

void PrimRefSet_SetPrimRef_FullPrecision( struct AABB3f* root_aabb, local struct PrimRefSet* refs, PrimRef ref, ushort id )
{
    float3 root_l = AABB3f_load_lower( root_aabb );
    float3 root_u = AABB3f_load_upper( root_aabb );
    float3 d = root_u - root_l;
    float scale = 1.0f / max( d.x, max( d.y, d.z ) );

    float3 l = ref.lower.xyz;
    float3 u = ref.upper.xyz;
    half3 lh = convert_half3_rtz( (l - root_l) * scale );
    half3 uh = convert_half3_rtp( (u - root_l) * scale );

    refs->AABB[id].lower[0] = lh.x;
    refs->AABB[id].lower[1] = lh.y;
    refs->AABB[id].lower[2] = lh.z;
    refs->AABB[id].upper[0] = uh.x;
    refs->AABB[id].upper[1] = uh.y;
    refs->AABB[id].upper[2] = uh.z;
    refs->meta[id].x = id;
    refs->meta[id].y = 0;
}



void DFS_CreatePrimRefSet( struct DFSArgs args,
                           local struct PrimRefSet* prim_refs )
{
    ushort id = get_local_id( 0 );
    ushort num_primrefs = args.num_primrefs;

    struct AABB3f box = BVH2_GetNodeBox( args.global_bvh2, args.global_bvh2_base );
    if ( id < num_primrefs )
    {
        PrimRef ref = args.primref_buffer[args.primref_indices_in[id]];
        prim_refs->input_indices[id] = args.primref_indices_in[id];
        PrimRefSet_SetPrimRef_FullPrecision( &box, prim_refs, ref, id );
    }
}

struct ThreadRangeInfo
{
    uchar start;
    uchar local_num_prims;
    uchar bvh2_root;
    bool  active;
};

struct BVHBuildLocals // size:  ~3.8K
{
    uchar2                 axis_and_left_count[ DFS_WG_SIZE ];
    struct ThreadRangeInfo range[ DFS_WG_SIZE ];
    uint                   sah[ DFS_WG_SIZE ];
};

#define LOCAL_BVH2_NODE_COUNT (2*(DFS_WG_SIZE) -1)

struct LocalBVH2
{
    uint nodes[LOCAL_BVH2_NODE_COUNT];
    uint num_nodes;

    // bit layout is for a node is
    //  uchar child_ptr;    // this is right_child_index >> 1.   right child's msb is always 0
    //  uchar primref_base; // index of the node's first primref.  will be 0 at the root
    //  uchar parent_dist;  // distance in nodes from this node to its parent
    //  uchar prim_counter; // number of prims in this subtree.  For a complete tree (256 prims), the root may be off by 1

    // for a WG size of 256, 8b is enough for parent distance, because the tree is built in level order
    //    the maximum distance between parent and child occurs for a complete tree.
    //    in this scenario the left-most leaf has index 255, its parent has index 127, the deltas to the children are 128 and 129
};


void LocalBVH2_Initialize( struct LocalBVH2* bvh2, ushort num_prims )
{
    bvh2->num_nodes = 1;
    bvh2->nodes[0] = min(num_prims,(ushort)255);
}



void LocalBVH2_Initialize_Presplit(struct LocalBVH2* bvh2, ushort num_prims, ushort left_count, ushort right_count )
{
    bvh2->num_nodes = 3;
    bvh2->nodes[0] = min(num_prims, (ushort)255);

    ushort bvh2_root = 0;
    ushort child_place = 1;

    uint child_ptr = (child_place + 1) >> 1;
    bvh2->nodes[bvh2_root] |= (child_ptr) << 24;

    uint parent_dist = child_place - bvh2_root;

    // initialize child nodes
    ushort primref_base_left = 0;
    ushort primref_base_right = left_count;
    uint left = (primref_base_left << 16) + ((parent_dist << 8)) + left_count;
    uint right = (primref_base_right << 16) + ((parent_dist + 1) << 8) + right_count;
    bvh2->nodes[child_place] = left;
    bvh2->nodes[child_place + 1] = right;
}


void LocalBVH2_CreateInnerNode( local struct LocalBVH2* bvh2, ushort bvh2_root, uint primref_base_left, uint primref_base_right )
{
    ushort child_place = atomic_add_local( &(bvh2-> num_nodes), 2 );

    uint child_ptr   = (child_place + 1) >> 1;
    bvh2->nodes[bvh2_root] |= (child_ptr) << 24;

    uint parent_dist = child_place - bvh2_root;

    // initialize child nodes
    uint left  = (primref_base_left << 16)  + ((parent_dist << 8));
    uint right = (primref_base_right << 16) + ((parent_dist + 1) << 8);
    bvh2->nodes[child_place]     = left;
    bvh2->nodes[child_place + 1] = right;
}

ushort2 LocalBVH2_GetChildIndices( struct LocalBVH2* bvh2, ushort bvh2_root )
{
    ushort right_idx = (bvh2->nodes[bvh2_root] & 0xff000000) >> 23;
    return (ushort2)(right_idx - 1, right_idx);
}


ushort LocalBVH2_IncrementPrimCount( local struct LocalBVH2* bvh2, ushort bvh2_root )
{
    // increment only the lower 8 bits.  Algorithm will not overflow by design
    return atomic_inc_local( &bvh2->nodes[bvh2_root] ) & 0xff;
}

ushort LocalBVH2_SetLeafPrimCount(local struct LocalBVH2* bvh2, ushort bvh2_root, ushort count)
{
    return bvh2->nodes[bvh2_root] |= (count& 0xff);
}

bool LocalBVH2_IsRoot( struct LocalBVH2* bvh2, ushort node_id )
{
    return node_id == 0;
}

ushort LocalBVH2_GetLeafPrimrefStart( struct LocalBVH2* bvh2, ushort bvh2_node_id )
{
    return (bvh2->nodes[bvh2_node_id] >> 16) & 255;
}

bool LocalBVH2_IsLeftChild( struct LocalBVH2* bvh2, ushort parent_node, ushort current_node )
{
    return (current_node & 1); // nodes are allocated in pairs.  first node is root, left child is an odd index
}

ushort LocalBVH2_GetParent( struct LocalBVH2* bvh2, ushort node )
{
    return node - ((bvh2->nodes[node] >> 8) & 255);
}

uint LocalBVH2_GetNodeCount( struct LocalBVH2* bvh2 )
{
    return bvh2->num_nodes;
}

bool LocalBVH2_IsLeaf( struct LocalBVH2* bvh2, ushort node_index )
{
    return (bvh2->nodes[node_index] & 255) <= TREE_ARITY;
}

ushort LocalBVH2_GetLeafPrimCount( struct LocalBVH2* bvh2, ushort node_index )
{
    return (bvh2->nodes[node_index] & 255);
}

void DFS_ConstructBVH2( local struct LocalBVH2* bvh2,
                        local struct PrimRefSet* prim_refs,
                        ushort bvh2_root,
                        ushort prim_range_start,
                        ushort local_num_prims,
                        ushort global_num_prims,
                        local struct BVHBuildLocals* locals,
                        local uint* num_active_threads )
{
    ushort tid = get_local_id( 0 );
    ushort primref_position = tid;

    bool active_thread = tid < global_num_prims;

    // Handle cases where initial binner creates leaves
    if ( active_thread && local_num_prims <= TREE_ARITY )
    {
        struct DFSPrimRef ref = PrimRefSet_GetPrimRef(prim_refs, primref_position);
        DFSPrimRef_SetBVH2Root(&ref, bvh2_root);
        PrimRefSet_SetPrimRef(prim_refs, ref, primref_position);
        active_thread = false;
        if (primref_position == prim_range_start)
            atomic_sub_local(num_active_threads, local_num_prims);
    }

    barrier( CLK_LOCAL_MEM_FENCE );

    locals->range[ tid ].start           = prim_range_start;
    locals->range[ tid ].local_num_prims = local_num_prims;
    locals->range[ tid ].bvh2_root       = bvh2_root;
    locals->range[ tid ].active          = active_thread;

    do
    {
        if(active_thread && prim_range_start == primref_position)
            locals->sah[primref_position] = UINT_MAX;

        barrier( CLK_LOCAL_MEM_FENCE );

        if ( active_thread )
        {
            local struct DFSPrimRefAABB* my_box = PrimRefSet_GetAABBPointer( prim_refs, primref_position );

            // each thread evaluates a possible split candidate.  Scan primrefs and compute sah cost
            //  do this axis-by-axis to keep register pressure low
            float best_sah = INFINITY;
            ushort best_axis = 3;
            ushort best_count = 0;

            struct DFSPrimRefAABB box_left[3];
            struct DFSPrimRefAABB box_right[3];
            float CSplit[3];
            ushort count_left[3];

            for ( ushort axis = 0; axis < 3; axis++ )
            {
                DFSPrimRefAABB_init( &box_left[axis] );
                DFSPrimRefAABB_init( &box_right[axis] );

                CSplit[axis] = my_box->lower[axis] + my_box->upper[axis];
                count_left[axis] = 0;
            }

            // scan primrefs in our subtree and partition using this thread's prim as a split plane
            {
                struct DFSPrimRefAABB box = *PrimRefSet_GetAABBPointer( prim_refs, prim_range_start );

                for ( ushort p = 1; p < local_num_prims; p++ )
                {
                        struct DFSPrimRefAABB next_box = *PrimRefSet_GetAABBPointer( prim_refs, prim_range_start + p ); //preloading box for next iteration

                        for( ushort axis = 0; axis < 3; axis++ )
                        {
                            float c = box.lower[axis] + box.upper[axis];

                            if ( c < CSplit[axis] )
                            {
                                // this primitive is to our left.
                                DFSPrimRefAABB_extend( &box_left[axis], &box );
                                count_left[axis]++;
                            }
                            else
                            {
                                // this primitive is to our right
                                DFSPrimRefAABB_extend( &box_right[axis], &box );
                            }
                        }

                        box = next_box;
                }

                // last iteration without preloading box
                for( ushort axis = 0; axis < 3; axis++ )
                {
                    float c = box.lower[axis] + box.upper[axis];

                    if ( c < CSplit[axis] )
                    {
                        // this primitive is to our left.
                        DFSPrimRefAABB_extend( &box_left[axis], &box );
                        count_left[axis]++;
                    }
                    else
                    {
                        // this primitive is to our right
                        DFSPrimRefAABB_extend( &box_right[axis], &box );
                    }
                }

            }

            for ( ushort axis = 0; axis < 3; axis++ )
            {
                float Al = DFSPrimRefAABB_halfArea( &box_left[axis] );
                float Ar = DFSPrimRefAABB_halfArea( &box_right[axis] );

                // Avoid NANs in SAH calculation in the corner case where all prims go right
                //  In this case we set Al=Ar, because such a split will only be selected if all primrefs
                //    are co-incident..  In that case, we will fall back to split-in-the-middle and both subtrees
                //    should store the same quantized area value
                if ( count_left[axis] == 0 )
                    Al = Ar;

                // compute sah cost
                ushort count_right = local_num_prims - count_left[axis];
                float sah = Ar * count_right + Al * count_left[axis];

                // keep this split if it is better than the previous one, or if the previous one was a corner-case
                if ( sah < best_sah || best_count == 0 )
                {
                    // yes, keep it
                    best_axis = axis;
                    best_sah = sah;
                    best_count = count_left[axis];
                }
            }

            // write split information to SLM
            locals->axis_and_left_count[primref_position].x = best_axis;
            locals->axis_and_left_count[primref_position].y = best_count;
            uint sah = as_uint(best_sah);
            // break ties by axis to ensure deterministic split selection
            //  otherwise builder can produce non-deterministic tree structure run to run
            //  based on the ordering of primitives (which can vary due to non-determinism in atomic counters)
            // Embed split axis and index into sah value; compute min over sah and max over axis
            sah = ( ( sah & ~1023 ) | ( 2 - best_axis ) << 8 | tid );

            // reduce on split candidates in our local subtree and decide the best one
            atomic_min_local( &locals->sah[ prim_range_start ], sah);
        }


        barrier( CLK_LOCAL_MEM_FENCE );

        ushort split_index = locals->sah[ prim_range_start ] & 255;
        ushort split_axis = locals->axis_and_left_count[split_index].x;
        ushort split_left_count = locals->axis_and_left_count[split_index].y;

        if ( (primref_position == split_index) && active_thread )
        {
            // first thread in a given subtree creates the inner node
            ushort start_left  = prim_range_start;
            ushort start_right = prim_range_start + split_left_count;
            if ( split_left_count == 0 )
                start_right = start_left + (local_num_prims / 2); // handle split-in-the-middle case

            LocalBVH2_CreateInnerNode( bvh2, bvh2_root, start_left, start_right );
        }


        barrier( CLK_LOCAL_MEM_FENCE );

        struct DFSPrimRef ref;
        ushort new_primref_position;

        if ( active_thread )
        {
            ushort2 kids = LocalBVH2_GetChildIndices( bvh2, bvh2_root );
            bool go_left;

            if ( split_left_count == 0 )
            {
                // We chose a split with no left-side prims
                //  This will only happen if all primrefs are located in the exact same position
                //   In that case, fall back to split-in-the-middle
                split_left_count = (local_num_prims / 2);
                go_left = (primref_position - prim_range_start < split_left_count);
            }
            else
            {
                // determine what side of the split this thread's primref belongs on
                local struct DFSPrimRefAABB* my_box    = PrimRefSet_GetAABBPointer( prim_refs, primref_position );
                local struct DFSPrimRefAABB* split_box = PrimRefSet_GetAABBPointer( prim_refs, split_index );
                float c = my_box->lower[split_axis] + my_box->upper[split_axis];
                float Csplit = split_box->lower[split_axis] + split_box->upper[split_axis];
                go_left = c < Csplit;
            }

            // adjust state variables for next loop iteration
            bvh2_root = (go_left) ? kids.x : kids.y;
            local_num_prims = (go_left) ? split_left_count : (local_num_prims - split_left_count);
            prim_range_start = (go_left) ? prim_range_start : prim_range_start + split_left_count;

            // determine the new primref position by incrementing a counter in the destination subtree
            new_primref_position = prim_range_start + LocalBVH2_IncrementPrimCount( bvh2, bvh2_root );

            // load our primref from its previous position
            ref = PrimRefSet_GetPrimRef( prim_refs, primref_position );
        }

        barrier( CLK_LOCAL_MEM_FENCE );

        if ( active_thread )
        {
            // write our primref into its sorted position and note which node it went in
            DFSPrimRef_SetBVH2Root( &ref, bvh2_root );
            PrimRefSet_SetPrimRef( prim_refs, ref, new_primref_position );
            primref_position = new_primref_position;


            // deactivate all threads whose subtrees are small enough to form a leaf
            if ( local_num_prims <= TREE_ARITY )
            {
                active_thread = false;
                if( primref_position == prim_range_start )
                    atomic_sub_local( num_active_threads, local_num_prims );
            }

            locals->range[ primref_position ].start           = prim_range_start;
            locals->range[ primref_position ].local_num_prims = local_num_prims;
            locals->range[ primref_position ].bvh2_root       = bvh2_root;
            locals->range[ primref_position ].active          = active_thread;
        }

        barrier( CLK_LOCAL_MEM_FENCE );

        // if we'll have next iteration then load from SLM
        if(*num_active_threads)
        {
            prim_range_start = locals->range[ tid ].start;
            local_num_prims  = locals->range[ tid ].local_num_prims;
            bvh2_root        = locals->range[ tid ].bvh2_root;
            active_thread    = locals->range[ tid ].active;
            primref_position = tid;
        }
        else
        {
            break;
        }

    } while ( true );

}


#define REFIT_BIT_DWORDS (LOCAL_BVH2_NODE_COUNT - DFS_WG_SIZE)/32

struct RefitBits
{
    uint bits[REFIT_BIT_DWORDS];
};

struct DFS_SLM
{
    union
    {
        struct LocalBVH2 bvh2;
        struct {
            struct AABB3f centroid_bounds;
            uint left_count;
            uint right_count;
            struct BFS_BinInfo bins;
            struct BFS_BinInfoReduce3_SLM reduce3;
        } binning;

    } u1;

    union
    {
        struct {
            struct PrimRefSet prim_refs;
            struct BVHBuildLocals locals;
        } pass0;

        struct AABB3f node_boxes[LOCAL_BVH2_NODE_COUNT];

    } u2;

    union
    {
        uchar bytes[DFS_WG_SIZE];
        uint dwords[DFS_WG_SIZE/4];
    } mask_info;

    struct RefitBits refit_bits;

};


void DFS_InitialBinningPass(
    local struct BFS_BinInfo* bins,
    local struct BFS_BinInfoReduce3_SLM* reduce3,
    uniform local struct AABB3f* centroid_bounds,
    local struct PrimRefSet* refs,
    local uint* left_counter,
    local uint* right_counter,
    ushort num_refs )
{
    uint tid = get_local_id(0);

    // initialize SLM structures
    if (tid == 0)
    {
        AABB3f_init(centroid_bounds);
        *left_counter = 0;
        *right_counter = 0;
    }

    BinInfo_init(bins);

    PrimRef ref;
    struct DFSPrimRef dfs_ref;

    if (tid < num_refs)
    {
        dfs_ref = PrimRefSet_GetPrimRef(refs, tid);
        struct DFSPrimRefAABB box = dfs_ref.aabb;
        ref.lower.xyz = (float3)(box.lower[0], box.lower[1], box.lower[2]);
        ref.upper.xyz = (float3)(box.upper[0], box.upper[1], box.upper[2]);
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    // compute centroid bounds so that we can bin
    if (tid < num_refs)
    {
        float3 centroid = ref.lower.xyz + ref.upper.xyz;
        Uniform_AABB3f_atomic_merge_local_sub_group_lu(centroid_bounds, centroid, centroid);
    }

    barrier(CLK_LOCAL_MEM_FENCE);

    // add primrefs to bins
    struct BinMapping mapping;
    BinMapping_init(&mapping, centroid_bounds, BFS_NUM_BINS);

    BinInfo_add_primref( &mapping, bins, &ref, tid<num_refs );

    barrier(CLK_LOCAL_MEM_FENCE);

    // compute split - every sub_group computes different bin
    struct BFS_Split split = BinInfo_reduce3(reduce3, bins, mapping.scale);


    bool go_left = false;
    uint local_pos = 0;
    if (tid < num_refs)
    {
        // partition primrefs into L/R subsets...
        if (split.sah == (float)(INFINITY))      // no valid split, split in the middle.. This can happen due to floating-point limit cases in huge scenes
            go_left = tid < (num_refs / 2);
        else
            go_left = is_left(&mapping, &split, &ref);

        if (go_left)
            local_pos = atomic_inc_local(left_counter);
        else
            local_pos = num_refs - (1+ atomic_inc_local(right_counter));

        PrimRefSet_SetPrimRef(refs, dfs_ref, local_pos);
    }

}


void Do_DFS( struct DFSArgs args, local struct DFS_SLM* slm, local uint* num_active_threads )
{
    local struct LocalBVH2* bvh2 = &slm->u1.bvh2;

    global struct BVH2* global_bvh2 = args.global_bvh2;

    PrimRef ref;
    uint parent_node;

    {
        local struct BVHBuildLocals* locals = &slm->u2.pass0.locals;
        local struct PrimRefSet* prim_refs = &slm->u2.pass0.prim_refs;

        DFS_CreatePrimRefSet(args, prim_refs);

        uint local_id = get_local_id(0);

        ushort bvh2_root = 0;
        ushort prim_range_start = 0;
        ushort local_num_prims = args.num_primrefs;

        if(local_id == 0)
            *num_active_threads = local_num_prims;

        // barrier for DFS_CreatePrimRefSet and num_active_threads
        barrier(CLK_LOCAL_MEM_FENCE);

        // initial binning pass if number of primrefs is large
        if( args.num_primrefs > 32 )
        {
            DFS_InitialBinningPass(&slm->u1.binning.bins, &slm->u1.binning.reduce3, &slm->u1.binning.centroid_bounds, prim_refs,
                &slm->u1.binning.left_count, &slm->u1.binning.right_count, args.num_primrefs);

            barrier(CLK_LOCAL_MEM_FENCE);

            ushort left_count = slm->u1.binning.left_count;
            ushort right_count = args.num_primrefs - left_count;
            if (get_local_id(0) == 0)
                LocalBVH2_Initialize_Presplit(bvh2, args.num_primrefs, left_count, right_count);

            bvh2_root        = (local_id < left_count) ? 1 : 2;
            local_num_prims = (local_id < left_count) ? left_count : right_count;
            prim_range_start = (local_id < left_count) ? 0 : left_count;
        }
        else
        {
            if (get_local_id(0) == 0)
                LocalBVH2_Initialize(bvh2, args.num_primrefs);
        }

        DFS_ConstructBVH2( bvh2, prim_refs, bvh2_root, prim_range_start, local_num_prims, args.num_primrefs, locals, num_active_threads);

        // move the prim refs into their sorted position
        //  keep this thread's primref around for later use
        if ( local_id < args.num_primrefs )
        {
            struct DFSPrimRef dfs_ref = PrimRefSet_GetPrimRef( prim_refs, local_id );

            uint input_id = DFSPrimRef_GetInputIndex( &dfs_ref );

            parent_node = DFSPrimRef_GetBVH2Parent( &dfs_ref );

            uint primref_index = prim_refs->input_indices[input_id];
            ref = args.primref_buffer[primref_index];
            args.primref_indices_out[local_id] = primref_index;
            args.primref_indices_in[local_id] = primref_index;
            // these buffers are not read again until the end of kernel
        }

        barrier( CLK_LOCAL_MEM_FENCE );

    }


    // initialize flags for determining when subtrees are done refit
    if ( get_local_id( 0 ) < REFIT_BIT_DWORDS )
        slm->refit_bits.bits[get_local_id( 0 )] = 0;


    // stash full-precision primref AABBs in slm storage
    local struct AABB3f* slm_boxes = &slm->u2.node_boxes[0];
    bool active_thread = get_local_id( 0 ) < args.num_primrefs;
    if( active_thread )
    {
        AABB3f_set( &slm_boxes[get_local_id( 0 )], ref.lower.xyz, ref.upper.xyz );

        // stash instance masks in SLM storage
        if( args.do_mask_processing )
            slm->mask_info.bytes[get_local_id(0)] = PRIMREF_instanceMask( &ref );
    }

    barrier( CLK_LOCAL_MEM_FENCE );

    // Refit leaf nodes
    uint box_index;
    if ( active_thread )
    {
        // the thread for the first primref in every leaf is the one that will ascend
        // remaining threads merge their AABB/mask into the first one and terminate
        uint first_ref = LocalBVH2_GetLeafPrimrefStart( bvh2, parent_node );
        if ( first_ref != get_local_id( 0 ) )
        {
            AABB3f_atomic_merge_local_lu( &slm_boxes[first_ref], ref.lower.xyz, ref.upper.xyz );

            if( args.do_mask_processing )
            {
                uint dword_index = first_ref/4;
                uint shift       = (first_ref%4)*8;
                uint mask = PRIMREF_instanceMask(&ref) << shift;
                atomic_or_local( &slm->mask_info.dwords[dword_index], mask );
            }
            active_thread = false; // switch off all primref threads except the first one
        }

        box_index = first_ref;
    }

    barrier( CLK_LOCAL_MEM_FENCE );

    if ( active_thread )
    {
        uint current_node = parent_node;
        parent_node = LocalBVH2_GetParent( bvh2, current_node );

        // write out the leaf node's AABB
        uint num_prims = LocalBVH2_GetLeafPrimCount( bvh2, current_node );
        uint prim_offs = args.primref_base + LocalBVH2_GetLeafPrimrefStart( bvh2, current_node );

        uint mask = 0xff;
        if( args.do_mask_processing )
            mask = slm->mask_info.bytes[box_index];

        BVH2_WriteLeafNode( global_bvh2, args.global_bvh2_base + current_node, &slm_boxes[box_index], prim_offs, num_prims, mask );

        // we no longer need the BVH2 bits for this node, so re-purpose the memory to store the AABB index
        bvh2->nodes[current_node] = box_index;

        // toggle flag bit in parent node.  The second thread to flip the bit is the one that gets to proceed
        uint thread_mask = (1 << (parent_node % 32));
        if ( (atomic_xor_local( &slm->refit_bits.bits[parent_node / 32], thread_mask ) & thread_mask) == 0 )
            active_thread = false;
    }

    // count how many active threads in sub_group we have and increment wg's number of active threads
    uint sg_active = sub_group_reduce_add(active_thread ? 1 : 0);
    if(get_sub_group_local_id() == 0)
    {
        atomic_add_local(num_active_threads, sg_active);
    }

    // refit internal nodes:
    // walk up the tree and refit AABBs

    do
    {
        barrier( CLK_LOCAL_MEM_FENCE ); // we need this barrier because we need to make sure all threads read num_active_threads before modifying it
        if ( active_thread )
        {
            uint current_node = parent_node;
            parent_node = LocalBVH2_GetParent( bvh2, current_node );

            // pull left/right box indices from current node
            ushort2 kids = LocalBVH2_GetChildIndices( bvh2, current_node );

            uint left_box = bvh2->nodes[kids.x];
            uint right_box = bvh2->nodes[kids.y];

            struct AABB3f left = slm_boxes[left_box];
            struct AABB3f right = slm_boxes[right_box];
            AABB3f_extend( &left, &right );

            uint2 child_offsets = (uint2)(
                args.global_bvh2_base + kids.x,
                args.global_bvh2_base + kids.y);

            uint mask = 0xff;
            if( args.do_mask_processing )
            {
                mask = slm->mask_info.bytes[left_box]
                     | slm->mask_info.bytes[right_box];
                slm->mask_info.bytes[left_box] = mask;
            }

            BVH2_WriteInnerNode( args.global_bvh2, args.global_bvh2_base+current_node, &left, child_offsets, mask );

            slm_boxes[left_box] = left;
            bvh2->nodes[current_node] = left_box;

            // stop at the root
            if ( LocalBVH2_IsRoot( bvh2, current_node ) )
            {
                active_thread = false;
                atomic_dec_local(num_active_threads);
            }
            else
            {
                // toggle flag bit in parent node.  The second thread to flip the bit is the one that gets to proceed
                uint mask = (1 << (parent_node % 32));
                if ( (atomic_xor_local( &slm->refit_bits.bits[parent_node / 32], mask ) & mask) == 0 )
                {
                    active_thread = false;
                    atomic_dec_local(num_active_threads);
                }
            }
        }

        barrier( CLK_LOCAL_MEM_FENCE );
    } while ( *num_active_threads > 0 );
}


GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size(DFS_WG_SIZE,1,1) ))
__attribute__( (intel_reqd_sub_group_size(16)) )
kernel void
DFS( global struct VContextScheduler* scheduler,
     global struct SAHBuildGlobals* globals_buffer )
{
    local struct DFS_SLM slm;
    local struct DFSDispatchRecord record;
    local uint num_active_threads;

    if ( get_local_id( 0 ) == 0  )
    {
        // pop an entry off the DFS dispatch queue
        //uint wg_index = atomic_dec_global( &scheduler->num_dfs_wgs ) - 1;
        //record = scheduler->dfs_queue.records[wg_index];

        // TODO:  The version above races, but is considerably faster... investigate
        uint wg_index = get_group_id(0);
        record = scheduler->dfs_queue.records[wg_index];
        write_mem_fence( CLK_LOCAL_MEM_FENCE );
        atomic_dec_global( &scheduler->num_dfs_wgs );
    }

    barrier( CLK_LOCAL_MEM_FENCE );


    bool odd_pass = record.tree_depth & 1;

    global struct SAHBuildGlobals* sah_globals = globals_buffer + record.batch_index;

    struct DFSArgs args;
    args.num_primrefs = record.num_primrefs;
    args.primref_indices_in   = SAHBuildGlobals_GetPrimrefIndices_In( sah_globals, odd_pass );
    args.primref_indices_out  = SAHBuildGlobals_GetPrimrefIndices_Out( sah_globals, odd_pass );
    args.primref_buffer       = SAHBuildGlobals_GetPrimrefs( sah_globals );
    args.global_bvh2          = SAHBuildGlobals_GetBVH2( sah_globals );
    args.primref_indices_in  += record.primref_base;
    args.primref_indices_out += record.primref_base;
    args.primref_base         = record.primref_base;
    args.global_bvh2_base     = record.bvh2_base;
    args.do_mask_processing   = SAHBuildGlobals_NeedMasks( sah_globals );

    Do_DFS( args, &slm, &num_active_threads );

}


/////////////////////////////////////////////////////////////////////////////////////////////////
///
///        BVH2 to BVH6
///
/////////////////////////////////////////////////////////////////////////////////////////////////



struct BuildFlatTreeArgs
{
    ushort leaf_size_in_bytes;
    ushort leaf_type;
    ushort inner_node_type;
    bool do_mask_processing;

    global uint* primref_indices;
    global PrimRef* primref_buffer;
    global struct Globals* globals;
    global struct BVHBase* bvh_base;
    global struct BVH2* bvh2;
};


// lane i in the return value is the index of the ith largest primref in the input
// the return value can be used with shuffle() to move data into its sorted position
//  the elements of 'key' must be unique.. only the first 6 elements are sorted
varying ushort SUBGROUP_get_sort_indices_N6( varying uint key )
{
    // each lane computes the number of items larger than it
    // this is its position in the descending order
    //   TODO_OPT:  Compiler can vectorize these uint16 adds by packing into lower and upper halves of same GPR.... make sure it does it
    //     if compiler is not generating optimal code, consider moving to Cm

    varying ushort cmp0 = (sub_group_broadcast(key, 0) > key) ? 1 : 0;
    varying ushort cmp1 = (sub_group_broadcast(key, 1) > key) ? 1 : 0;
    varying ushort cmp2 = (sub_group_broadcast(key, 2) > key) ? 1 : 0;
    varying ushort cmp3 = (sub_group_broadcast(key, 3) > key) ? 1 : 0;
    varying ushort cmp4 = (sub_group_broadcast(key, 4) > key) ? 1 : 0;
    varying ushort cmp5 = (sub_group_broadcast(key, 5) > key) ? 1 : 0;
    varying ushort a = cmp0 + cmp2 + cmp4;
    varying ushort b = cmp1 + cmp3 + cmp5;
    varying ushort num_larger = a + b;

    // each lane determines which of the input elements it should pull
    varying ushort lane = get_sub_group_local_id();
    a  = (sub_group_broadcast(num_larger, 0) == lane) ? 0 : 0;
    b  = (sub_group_broadcast(num_larger, 1) == lane) ? 1 : 0;
    a += (sub_group_broadcast(num_larger, 2) == lane) ? 2 : 0;
    b += (sub_group_broadcast(num_larger, 3) == lane) ? 3 : 0;
    a += (sub_group_broadcast(num_larger, 4) == lane) ? 4 : 0;
    b += (sub_group_broadcast(num_larger, 5) == lane) ? 5 : 0;
    return a + b;
}

uint SUBGROUP_area_to_sort_key( varying float area, uniform ushort num_children )
{
    varying ushort lane = get_sub_group_local_id();
    area = (lane < num_children) ? area : 0;        // put inactive nodes last

    // drop LSBs and break ties by lane number to ensure unique keys
    // use descending lane IDs to ensure that sort is stable if the upper MSBs are equal.
    //     If we do not do this it can lead to non-deterministic tree structure
    return (as_uint(area) & 0xffffff80) + (lane^(get_sub_group_size()-1));
}

// lane i in the return value is the index of the ith largest primref in the input
// the return value can be used with shuffle() to move data into its sorted position
//  the elements of 'key' must be unique.. only the first 6 elements are sorted
varying ushort SUBGROUP_get_sort_indices_N6_2xSIMD8_in_SIMD16( varying uint key )
{
    // each lane computes the number of items larger than it
    // this is its position in the descending order
    //   TODO_OPT:  Compiler can vectorize these uint16 adds by packing into lower and upper halves of same GPR.... make sure it does it
    //     if compiler is not generating optimal code, consider moving to Cm

    varying ushort cmp0 = (sub_group_broadcast(key, 0) > key) ? 1 : 0;
    varying ushort cmp1 = (sub_group_broadcast(key, 1) > key) ? 1 : 0;
    varying ushort cmp2 = (sub_group_broadcast(key, 2) > key) ? 1 : 0;
    varying ushort cmp3 = (sub_group_broadcast(key, 3) > key) ? 1 : 0;
    varying ushort cmp4 = (sub_group_broadcast(key, 4) > key) ? 1 : 0;
    varying ushort cmp5 = (sub_group_broadcast(key, 5) > key) ? 1 : 0;
    varying ushort a = cmp0 + cmp2 + cmp4;
    varying ushort b = cmp1 + cmp3 + cmp5;
    varying ushort num_larger = a + b;

    varying ushort cmp0_1 = (sub_group_broadcast(key, 8) > key) ? 1 : 0;
    varying ushort cmp1_1 = (sub_group_broadcast(key, 9) > key) ? 1 : 0;
    varying ushort cmp2_1 = (sub_group_broadcast(key, 10) > key) ? 1 : 0;
    varying ushort cmp3_1 = (sub_group_broadcast(key, 11) > key) ? 1 : 0;
    varying ushort cmp4_1 = (sub_group_broadcast(key, 12) > key) ? 1 : 0;
    varying ushort cmp5_1 = (sub_group_broadcast(key, 13) > key) ? 1 : 0;
    varying ushort a_1 = cmp0_1 + cmp2_1 + cmp4_1;
    varying ushort b_1 = cmp1_1 + cmp3_1 + cmp5_1;
    varying ushort num_larger_1 = a_1 + b_1;

    // each lane determines which of the input elements it should pull
    varying ushort lane = get_sub_group_local_id();
    if(lane < 8)
    {
        a  = (sub_group_broadcast(num_larger, 0) == lane) ? 0 : 0;
        b  = (sub_group_broadcast(num_larger, 1) == lane) ? 1 : 0;
        a += (sub_group_broadcast(num_larger, 2) == lane) ? 2 : 0;
        b += (sub_group_broadcast(num_larger, 3) == lane) ? 3 : 0;
        a += (sub_group_broadcast(num_larger, 4) == lane) ? 4 : 0;
        b += (sub_group_broadcast(num_larger, 5) == lane) ? 5 : 0;
    }
    else
    {
        a  = (sub_group_broadcast(num_larger_1, 8)  == lane-8) ? 8 : 8;
        b  = (sub_group_broadcast(num_larger_1, 9)  == lane-8) ? 1 : 0;
        a += (sub_group_broadcast(num_larger_1, 10) == lane-8) ? 2 : 0;
        b += (sub_group_broadcast(num_larger_1, 11) == lane-8) ? 3 : 0;
        a += (sub_group_broadcast(num_larger_1, 12) == lane-8) ? 4 : 0;
        b += (sub_group_broadcast(num_larger_1, 13) == lane-8) ? 5 : 0;
    }

    return a + b;
}

uint SUBGROUP_area_to_sort_key_2xSIMD8_in_SIMD16( varying float area, uniform ushort num_children )
{
    varying ushort lane = get_sub_group_local_id() % 8;
    area = (lane < num_children) ? area : 0;        // put inactive nodes last

    // drop LSBs and break ties by lane number to ensure unique keys
    // use descending lane IDs to ensure that sort is stable if the upper MSBs are equal.
    //     If we do not do this it can lead to non-deterministic tree structure
    return (as_uint(area) & 0xffffff80) + (lane^7);
}

ushort SUBGROUP_BuildFlatTreeNode(
    uniform struct BuildFlatTreeArgs args,
    uniform uint bvh2_root,
    uniform struct InternalNode* qnode,
    uniform uint qnode_index,
    varying uint3* sg_children_out // if an inner node is created, receives the indices of the 6 child nodes (X), and the QNode position (y), and num_children(z)
                                   //  if a leaf is created, receives number of primrefs (z)
) // return value is the number of child nodes or 0 for a leaf
{
    global struct BVH2* bvh2 = args.bvh2;
    varying ushort lane = get_sub_group_local_id();

    global struct BVHBase* base = args.bvh_base;


    if ( !BVH2_IsInnerNode( bvh2, bvh2_root ) )
    {
        uniform ushort num_prims   = BVH2_GetLeafPrimCount( bvh2, bvh2_root );
        uniform uint primref_start = BVH2_GetLeafPrimStart( bvh2, bvh2_root );
        varying uint primref_index = primref_start + ((lane < num_prims) ? lane : 0);

        varying uint ref_id = args.primref_indices[primref_index];
        varying PrimRef ref = args.primref_buffer[ref_id];
        uniform char* leaf_mem_base = (char*)BVHBase_GetQuadLeaves( args.bvh_base );
        uniform char* leaf_mem = leaf_mem_base + primref_start * args.leaf_size_in_bytes;

        uniform int offset = (int)(leaf_mem - (char*)qnode);
        offset = offset >> 6;

        varying uint key = SUBGROUP_area_to_sort_key(AABB_halfArea(&ref), num_prims );
        varying ushort sort_index = SUBGROUP_get_sort_indices_N6(key);
        ref = PrimRef_sub_group_shuffle(&ref, sort_index);
        ref_id = intel_sub_group_shuffle(ref_id, sort_index);

        if (lane < num_prims)
            args.primref_indices[primref_index] = ref_id;

        uint global_num_prims = args.globals->numPrimitives;
        char* bvh_mem = (char*) args.bvh_base;

        if(lane < num_prims)
            args.primref_indices[primref_index + global_num_prims] = qnode - (struct InternalNode*)bvh_mem;

        if (args.leaf_type == NODE_TYPE_INSTANCE)
            subgroup_setInstanceQBVHNodeN( offset, &ref, num_prims, (struct QBVHNodeN*)qnode, lane < num_prims ? PRIMREF_instanceMask(&ref) : 0 );
        else
            subgroup_setQBVHNodeN( offset, args.leaf_type, &ref, num_prims, (struct QBVHNodeN*)qnode, BVH_NODE_DEFAULT_MASK );

        sg_children_out->z = num_prims;
        return 0;
    }
    else
    {
        // collapse BVH2 into BVH6.
        // We will spread the root node's children across the subgroup, and keep adding SIMD lanes until we have enough
        uniform ushort num_children = 2;

        uniform uint2 kids = BVH2_GetChildIndices( bvh2, bvh2_root );
        varying uint sg_bvh2_node = kids.x;
        if ( lane == 1 )
            sg_bvh2_node = kids.y;

        do
        {
            // choose the inner node with maximum area to replace.
            // Its left child goes in its old location.  Its right child goes in a new lane

            // TODO_OPT:  We re-read the AABBs again and again to compute area
            //   ... store per-lane boxes instead and pre-compute areas

            varying float sg_area = BVH2_GetNodeArea( bvh2, sg_bvh2_node );
            varying bool sg_is_inner = BVH2_IsInnerNode( bvh2, sg_bvh2_node );
            sg_area = (sg_is_inner && lane < num_children) ? sg_area : 0; // prevent early exit if the largest child is a leaf

            uniform float max_area = sub_group_reduce_max_N6( sg_area );
            varying bool sg_reducable = max_area == sg_area && (lane < num_children) && sg_is_inner;
            uniform uint mask = intel_sub_group_ballot( sg_reducable );

            // TODO_OPT:  Some of these ops seem redundant.. look at trimming further

            if ( mask == 0 )
                break;

            // choose the inner node with maximum area to replace
            uniform ushort victim_child = ctz( mask );
            uniform uint victim_node = sub_group_broadcast( sg_bvh2_node, victim_child );
            kids = BVH2_GetChildIndices( bvh2, victim_node );

            if ( lane == victim_child )
                sg_bvh2_node = kids.x;
            else if ( lane == num_children )
                sg_bvh2_node = kids.y;

            num_children++;

        } while ( num_children < TREE_ARITY );

        // allocate inner node space
        uniform uint kids_offset;
        if (get_sub_group_local_id() == 0)
            kids_offset = allocate_inner_nodes( args.bvh_base, num_children );
        kids_offset = sub_group_broadcast(kids_offset, 0);

        uniform struct QBVHNodeN* kid = (((struct QBVHNodeN*)args.bvh_base) + kids_offset);
        uniform int offset = (int)((char*)kid - (char*)qnode) >> 6;

#if 0
        uniform uint kids_offset;
        if ( get_sub_group_local_id() == 0 )
            kids_offset = alloc_node_mem( args.globals, sizeof( struct QBVHNodeN ) * num_children );
        kids_offset = sub_group_broadcast( kids_offset, 0 );


        // create inner node
        uniform struct QBVHNodeN* kid = (struct QBVHNodeN*) ((char*)(args.bvh_base) + kids_offset);
        uniform int offset = (int)((char*)kid - (char*)qnode) >> 6;
#endif
        uniform uint child_type = args.inner_node_type;

        // sort child nodes in descending order by AABB area
        varying struct AABB box   = BVH2_GetAABB( bvh2, sg_bvh2_node );
        varying uint key          = SUBGROUP_area_to_sort_key(AABB_halfArea(&box), num_children );
        varying ushort sort_index = SUBGROUP_get_sort_indices_N6(key);
        box          = AABB_sub_group_shuffle(&box, sort_index);
        sg_bvh2_node = intel_sub_group_shuffle(sg_bvh2_node, sort_index);

        uniform uint node_mask = (args.do_mask_processing) ? BVH2_GetMask( bvh2, bvh2_root ) : 0xff;

        subgroup_setQBVHNodeN( offset, child_type, &box, num_children, (struct QBVHNodeN*)qnode, node_mask );

        // return child information
        *sg_children_out = (uint3)(sg_bvh2_node, qnode_index + offset + get_sub_group_local_id(), num_children );
        return num_children;
    }
}

ushort SUBGROUP_BuildFlatTreeNode_2xSIMD8_in_SIMD16(
    uniform struct BuildFlatTreeArgs args,
    varying uint bvh2_root,
    varying struct InternalNode* qnode_base,
    varying uint qnode_index,
    varying uint3* sg_children_out, // if an inner node is created, receives the indices of the 6 child nodes (X), and the QNode position (y), and num_children(z)
                                   //  if a leaf is created, receives number of primrefs (z)
    bool active_lane
) // return value is the number of child nodes or 0 for a leaf
{
    global struct BVH2* bvh2 = args.bvh2;
    varying ushort SIMD16_lane = get_sub_group_local_id();
    varying ushort SIMD8_lane = get_sub_group_local_id() % 8;
    varying ushort SIMD8_id = get_sub_group_local_id() / 8;
    varying ushort lane = get_sub_group_local_id();
    global struct BVHBase* base = args.bvh_base;

    struct BVH2NodeMetaData nodeMetaData = BVH2_GetNodeMetaData( bvh2, bvh2_root );

    bool is_leaf = active_lane && !BVH2NodeMetaData_IsInnerNode( &nodeMetaData );
    bool is_inner = active_lane && BVH2NodeMetaData_IsInnerNode( &nodeMetaData );

    uchar mask = BVH_NODE_DEFAULT_MASK;
    if(is_inner)
        mask = (args.do_mask_processing) ? BVH2NodeMetaData_GetMask( &nodeMetaData ) : 0xff;

    int offset;

    varying struct InternalNode* qnode = qnode_base + qnode_index;
    // TOOD: we don't need unions, I left them only for readability
    union {
        uint num_prims;
        uint num_children;
    } lane_num_data;

    union {
        PrimRef ref; // this is in fact AABB
        struct AABB box;
    } lane_box_data;

    union {
        uint ref_id;
        uint sg_bvh2_node;
    } lane_id_data;

    // for leafs
    varying uint primref_index;

    if(is_leaf)
    {
        lane_num_data.num_prims   = BVH2NodeMetaData_GetLeafPrimCount( &nodeMetaData );
        uint primref_start = BVH2NodeMetaData_GetLeafPrimStart( &nodeMetaData );
        primref_index = primref_start + ((SIMD8_lane < lane_num_data.num_prims) ? SIMD8_lane : 0);

        lane_id_data.ref_id = args.primref_indices[primref_index];
        lane_box_data.ref = args.primref_buffer[lane_id_data.ref_id];
        char* leaf_mem_base = (char*)BVHBase_GetQuadLeaves( args.bvh_base );
        char* leaf_mem = leaf_mem_base + primref_start * args.leaf_size_in_bytes;

        offset = (int)(leaf_mem - (char*)qnode);
        offset = offset >> 6;
    }


    if(intel_sub_group_ballot(is_inner))
    {
        // collapse BVH2 into BVH6.
        // We will spread the root node's children across the subgroup, and keep adding SIMD lanes until we have enough

        uint2 kids;
        if(is_inner)
        {
            lane_num_data.num_children = 2;
            kids = BVH2_GetChildIndices( bvh2, bvh2_root );

            lane_id_data.sg_bvh2_node = kids.x;
            if ( SIMD8_lane == 1 )
                lane_id_data.sg_bvh2_node = kids.y;
        }

        bool active = is_inner;
        do
        {
            // choose the inner node with maximum area to replace.
            // Its left child goes in its old location.  Its right child goes in a new lane

            // TODO_OPT:  We re-read the AABBs again and again to compute area
            //   ... store per-lane boxes instead and pre-compute areas

            varying float sg_area = 0;
            varying bool sg_is_inner = false;
            if(active)
            {
                sg_area = BVH2_GetNodeArea( bvh2, lane_id_data.sg_bvh2_node );
                sg_is_inner = BVH2_IsInnerNode( bvh2, lane_id_data.sg_bvh2_node );
                sg_area = (sg_is_inner && SIMD8_lane < lane_num_data.num_children) ? sg_area : 0; // prevent early exit if the largest child is a leaf
            }

            float max_area = sub_group_reduce_max_N6_2xSIMD8_in_SIMD16( sg_area );
            varying bool sg_reducable = max_area == sg_area && sg_is_inner && (SIMD8_lane < lane_num_data.num_children);
            uint mask = intel_sub_group_ballot( sg_reducable ) & (0xFF << SIMD8_id * 8); // we'll end up with two different masks for two SIMD8 in SIMD16 due to bits masking

            // TODO_OPT:  Some of these ops seem redundant.. look at trimming further

            if ( mask == 0 )
                active = false;

            // choose the inner node with maximum area to replace
            ushort victim_child = ctz( mask );
            uint victim_node = intel_sub_group_shuffle( lane_id_data.sg_bvh2_node, victim_child );
            if(active)
            {
                kids = BVH2_GetChildIndices( bvh2, victim_node );

                if ( SIMD16_lane == victim_child ) // we use SIMD16_lane, cause victim_child was calculated based on SIMD16 i.e. second node will have victim from 8..13
                    lane_id_data.sg_bvh2_node = kids.x;
                else if ( SIMD8_lane == lane_num_data.num_children )
                    lane_id_data.sg_bvh2_node = kids.y;

                lane_num_data.num_children++;

                if(lane_num_data.num_children >= TREE_ARITY)
                    active = false;
            }

        } while ( intel_sub_group_ballot(active) ); // if any active, then continue

        // sum children from both halfs of SIMD16 to allocate nodes only once per sub_group
        uniform ushort num_children = is_inner ? lane_num_data.num_children : 0;
        uniform ushort first_SIMD8_num_children = sub_group_broadcast(num_children, 0);
        uniform ushort second_SIMD8_num_children = sub_group_broadcast(num_children, 8);

        num_children = first_SIMD8_num_children + second_SIMD8_num_children;
        uint kids_offset;

        // allocate inner node space
        if(num_children && SIMD16_lane == 0)
            kids_offset = allocate_inner_nodes( args.bvh_base, num_children );
        kids_offset = sub_group_broadcast(kids_offset, 0);
        if((is_inner))
        {
            kids_offset += SIMD8_id * first_SIMD8_num_children;

            struct QBVHNodeN* kid = (((struct QBVHNodeN*)args.bvh_base) + kids_offset);

            offset = (int)((char*)kid - (char*)qnode) >> 6;
            lane_box_data.box = BVH2_GetAABB( bvh2, lane_id_data.sg_bvh2_node );
        }
    }

    // sort child nodes in descending order by AABB area
    varying uint key          = SUBGROUP_area_to_sort_key_2xSIMD8_in_SIMD16(AABB_halfArea(&lane_box_data.box), lane_num_data.num_children );
    varying ushort sort_index = SUBGROUP_get_sort_indices_N6_2xSIMD8_in_SIMD16(key);
    lane_box_data.box         = PrimRef_sub_group_shuffle(&lane_box_data.box, sort_index);
    lane_id_data.sg_bvh2_node = intel_sub_group_shuffle(lane_id_data.sg_bvh2_node, sort_index);

    char* bvh_mem = (char*) args.bvh_base;
    if (is_leaf && SIMD8_lane < lane_num_data.num_prims)
    {
        args.primref_indices[primref_index] = lane_id_data.ref_id;
        args.primref_indices[primref_index + args.globals->numPrimitives] = qnode - (struct InternalNode*)bvh_mem;
    }

    bool degenerated = false;
    uint node_type = is_leaf ? args.leaf_type : args.inner_node_type;

    if(args.leaf_type == NODE_TYPE_INSTANCE)
        degenerated = subgroup_setInstanceBox_2xSIMD8_in_SIMD16(&lane_box_data.box, lane_num_data.num_children, &mask, SIMD8_lane < lane_num_data.num_prims ? PRIMREF_instanceMask(&lane_box_data.ref) : 0, is_leaf);

    subgroup_setQBVHNodeN_setFields_2xSIMD8_in_SIMD16(offset, node_type, &lane_box_data.box, lane_num_data.num_children, mask, (struct QBVHNodeN*)(qnode), degenerated, active_lane);

    // return child information
    if(is_inner)
    {
        sg_children_out->x = lane_id_data.sg_bvh2_node;
        sg_children_out->y = qnode_index + offset + SIMD8_lane;
    }

    sg_children_out->z = lane_num_data.num_children;

    return is_inner ? lane_num_data.num_children : 0;
}

void check_primref_integrity( global struct SAHBuildGlobals* globals )
{
    global uint* primref_in = SAHBuildGlobals_GetPrimrefIndices_In( globals, 0 );
    global uint* primref_out = SAHBuildGlobals_GetPrimrefIndices_Out( globals, 0 );
    dword num_primrefs = SAHBuildGlobals_GetTotalPrimRefs( globals );
    if ( get_local_id( 0 ) == 0 )
    {
        for ( uint i = 0; i < num_primrefs; i++ )
        {
            primref_out[i] = 0;
        }

        for ( uint i = 0; i < num_primrefs; i++ )
            primref_out[primref_in[i]]++;

        for ( uint i = 0; i < num_primrefs; i++ )
            if ( primref_out[i] != 1 )
                printf( "Foo: %u   %u\n", i, primref_out[i] );
    }
}




void check_bvh2(global struct SAHBuildGlobals* globals )
{
    global struct BVH2* bvh2 = SAHBuildGlobals_GetBVH2(globals);
    global uint* primref_in = SAHBuildGlobals_GetPrimrefIndices_In(globals, 0);
    global uint* primref_out = SAHBuildGlobals_GetPrimrefIndices_Out(globals, 0);
    dword num_primrefs = SAHBuildGlobals_GetTotalPrimRefs(globals);

    if (get_local_id(0) == 0)
    {
        for (uint i = 0; i < num_primrefs; i++)
            primref_out[i] = 0;

        uint stack[256];
        uint sp=0;
        uint r = BVH2_GetRoot(bvh2);
        stack[sp++] = r;
        while (sp)
        {
            r = stack[--sp];
            if (BVH2_IsInnerNode(bvh2,r))
            {
                uint2 kids = BVH2_GetChildIndices( bvh2, r);
                if (kids.x >= bvh2->num_nodes || kids.y >= bvh2->num_nodes)
                {
                    printf("BVH2!! Bad node index found!\n");
                    return;
                }

                stack[sp++] = kids.x;
                stack[sp++] = kids.y;
            }
            else
            {
                uint ref = BVH2_GetLeafPrimStart(bvh2,r);
                uint count = BVH2_GetLeafPrimCount(bvh2,r);
                if( count == 0 )
                {
                    printf("BVH2!! Empty leaf found!\n");
                    return;
                }
                for (uint i = 0; i < count; i++)
                {
                    if (ref + i > num_primrefs)
                    {
                        printf("BVH2!! Bad leaf range!\n");
                        return;
                    }
                    uint c = primref_out[ref+i];
                    if (c != 0)
                    {
                        printf("BVH2!! overlapped prim ranges\n");
                        return;
                    }
                    primref_out[ref+i] = 1;
                    if (primref_in[ref + i] >= num_primrefs)
                    {
                        printf("BAD PRIMREF ID FOUND!\n");
                        return;
                    }
                }
            }
        }
    }

    printf("bvh2 is ok!\n");
}


#if 0
// TODO_OPT:  Enable larger WGs.  WGSize 512 at SIMD8 hangs on Gen9, but Gen12 can go bigger
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size(256,1,1)) )
__attribute__( (intel_reqd_sub_group_size(8) ) )
kernel void
build_qnodes( global struct SAHBuildGlobals* globals, global struct VContextScheduler* scheduler )
{
    globals = globals + (scheduler->num_trivial_builds + scheduler->num_single_builds);
    globals = globals + get_group_id(0);


    struct BuildFlatTreeArgs args;
    args.leaf_size_in_bytes = SAHBuildGlobals_GetLeafSizeInBytes( globals );
    args.leaf_type          = SAHBuildGlobals_GetLeafType( globals );
    args.inner_node_type    = SAHBuildGlobals_GetInternalNodeType( globals );
    args.primref_indices    = SAHBuildGlobals_GetPrimrefIndices_In( globals, 0 );
    args.primref_buffer     = SAHBuildGlobals_GetPrimrefs( globals );
    args.bvh_base           = SAHBuildGlobals_GetBVHBase( globals );
    args.bvh2               = SAHBuildGlobals_GetBVH2( globals );
    args.globals            = (global struct Globals*) globals->p_globals;
    args.do_mask_processing = SAHBuildGlobals_NeedMasks( globals );

    dword alloc_backpointers = SAHBuildGlobals_NeedBackPointers( globals );
    global uint2* root_buffer = (global uint2*) globals->p_qnode_root_buffer;
    global struct InternalNode* qnodes = (global struct InternalNode*) BVHBase_GetInternalNodes( args.bvh_base );
    global uint* back_pointers = (global uint*) BVHBase_GetBackPointers( args.bvh_base );

    local uint nodes_produced;
    if ( get_sub_group_id() == 0 )
    {
        // allocate first node
        if (get_sub_group_local_id() == 0)
            allocate_inner_nodes( args.bvh_base, 1 );

        // first subgroup does first node
        varying uint3 children_info;
        uniform ushort num_children = SUBGROUP_BuildFlatTreeNode(args, BVH2_GetRoot(args.bvh2), qnodes, 0, &children_info );

        if ( get_sub_group_local_id() < num_children )
            root_buffer[get_sub_group_local_id()] = children_info.xy;

        if ( alloc_backpointers )
        {
            // set root's backpointer
            if( get_sub_group_local_id() == 0 )
                back_pointers[0] = (0xffffffc0) | (children_info.z << 3);

            // point child backpointers at the parent
            if( get_sub_group_local_id() < num_children )
                back_pointers[children_info.y] = 0;
        }

        if ( get_sub_group_local_id() == 0 )
            nodes_produced = num_children;
    }

    barrier( CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE );


    uniform uint buffer_index = get_sub_group_id();
    uniform bool sg_active    = buffer_index < nodes_produced;

    while ( work_group_any( sg_active ) )
    {
        if( sg_active )
        {
            uniform uint bvh2_node    = root_buffer[buffer_index].x;
            uniform uint qnode_index  = root_buffer[buffer_index].y;

            // build a node
            varying uint3 children_info;
            uniform ushort num_children = SUBGROUP_BuildFlatTreeNode( args, bvh2_node, qnodes + qnode_index, qnode_index, &children_info );

            // handle backpointers
            if ( alloc_backpointers )
            {
                // update this node's backpointer with child count
                if ( get_sub_group_local_id() == 0 )
                    back_pointers[qnode_index] |= (children_info.z << 3);

                // point child backpointers at parent
                if ( get_sub_group_local_id() < num_children )
                    back_pointers[children_info.y] = (qnode_index << 6);
            }

            if ( num_children )
            {
                // allocate space in the child buffer
                uint root_buffer_position = 0;
                if ( get_sub_group_local_id() == 0 )
                    root_buffer_position = atomic_add_local( &nodes_produced, num_children );
                root_buffer_position = sub_group_broadcast( root_buffer_position, 0 );

                // store child indices in root buffer
                if ( get_sub_group_local_id() < num_children )
                    root_buffer[root_buffer_position + get_sub_group_local_id()] = children_info.xy;
            }
        }

        // sync everyone
        work_group_barrier( CLK_LOCAL_MEM_FENCE | CLK_GLOBAL_MEM_FENCE,
                            memory_scope_work_group );


        if( sg_active )
            buffer_index += get_num_sub_groups();

        sg_active = (buffer_index < nodes_produced);
    }
}
#endif







inline bool buffer_may_overflow( uint capacity, uint current_size, uint elements_processed_per_sub_group )
{
    uint num_consumed = min( get_num_sub_groups() * elements_processed_per_sub_group, current_size );
    uint space_available = (capacity - current_size) + num_consumed;
    uint space_needed = TREE_ARITY * num_consumed;
    return space_available < space_needed;
}

inline uint build_qnodes_pc(
    global struct SAHBuildGlobals* globals,
    bool alloc_backpointers,
    bool process_masks,
    uint first_qnode,
    uint first_bvh2_node,

    local uint2* SLM_local_root_buffer,
    local uint* SLM_ring_tail,
    const uint  RING_SIZE
)

{
    struct BuildFlatTreeArgs args;
    args.leaf_size_in_bytes = SAHBuildGlobals_GetLeafSizeInBytes( globals );
    args.leaf_type = SAHBuildGlobals_GetLeafType( globals );
    args.inner_node_type = SAHBuildGlobals_GetInternalNodeType( globals );
    args.primref_indices = SAHBuildGlobals_GetPrimrefIndices_In( globals, 0 );
    args.primref_buffer = SAHBuildGlobals_GetPrimrefs( globals );
    args.bvh_base = SAHBuildGlobals_GetBVHBase( globals );
    args.bvh2 = SAHBuildGlobals_GetBVH2( globals );
    args.globals = (global struct Globals*) globals->p_globals;
    args.do_mask_processing = process_masks;

    global struct InternalNode* qnodes = (global struct InternalNode*) BVHBase_GetInternalNodes( args.bvh_base );
    global uint* back_pointers = (global uint*) BVHBase_GetBackPointers( args.bvh_base );

    // first subgroup adds first node
    if ( get_sub_group_id() == 0 && get_sub_group_local_id() == 0)
    {
        SLM_local_root_buffer[0].x = first_bvh2_node;
        SLM_local_root_buffer[0].y = first_qnode;
        *SLM_ring_tail = 1;

    }

    uint ring_head = 0;
    uint ring_tail = 1;
    uint ring_size = 1;

    barrier( CLK_LOCAL_MEM_FENCE );

    const uniform uint elements_processed_in_sg = 2;

    while ( ring_size > 0 && !buffer_may_overflow( RING_SIZE, ring_size, elements_processed_in_sg ) )
    {
        ushort SIMD16_lane = get_sub_group_local_id();

        // SIMD16 as 2xSIMD8
        ushort SIMD8_lane = get_sub_group_local_id() % 8;
        ushort SIMD8_id = get_sub_group_local_id() / 8;
        bool active_lane;

        uniform uint nodes_consumed = min( get_num_sub_groups() * elements_processed_in_sg, ring_size ); // times two because we process two nodes in subgroup
        uniform bool sg_active = get_sub_group_id() * elements_processed_in_sg < nodes_consumed;
        ushort num_children = 0;
        varying uint3 children_info = 0;

        uint bvh2_node = 0;
        uint qnode_index = 0;

        if (sg_active)
        {
            ushort consumed_pos = get_sub_group_id() * elements_processed_in_sg + SIMD8_id;
            active_lane = consumed_pos < nodes_consumed ? true : false;
            consumed_pos = consumed_pos < nodes_consumed ? consumed_pos : consumed_pos-1;

            uint buffer_index = (ring_head + consumed_pos) % RING_SIZE;

            bvh2_node = SLM_local_root_buffer[buffer_index].x;
            qnode_index = SLM_local_root_buffer[buffer_index].y;
        }

        barrier( CLK_LOCAL_MEM_FENCE );

        if (sg_active)
        {
            // build a node
            num_children = SUBGROUP_BuildFlatTreeNode_2xSIMD8_in_SIMD16(args, bvh2_node, qnodes, qnode_index, &children_info, active_lane);

            // handle backpointers
            // TODO_OPT:  This should be separate shaders not a runtime branch
            //     doing it this way for now because GRLTLK does not make dynamic shader selection on host very easy.
            //     this needs to change... GRLTLK should

            if (alloc_backpointers && active_lane)
            {
                // update this node's backpointer with child count
                if (SIMD8_lane == 0)
                    back_pointers[qnode_index] |= (children_info.z << 3);

                // point child backpointers at parent
                if (SIMD8_lane < num_children)
                    back_pointers[children_info.y] = (qnode_index << 6);
            }

            // save data

            uniform ushort first_SIMD8_num_children  = sub_group_broadcast(num_children, 0);
            uniform ushort second_SIMD8_num_children = sub_group_broadcast(num_children, 8);
            uniform ushort SIMD16_num_children = first_SIMD8_num_children + second_SIMD8_num_children;

            uint root_buffer_position = 0;

            // allocate space in the child buffer
            if (SIMD16_lane == 0 && SIMD16_num_children)
                root_buffer_position = atomic_add_local(SLM_ring_tail, SIMD16_num_children);

            root_buffer_position = sub_group_broadcast( root_buffer_position, 0 );
            root_buffer_position += SIMD8_id * first_SIMD8_num_children; // update offset for second half of SIMD16

            // store child indices in root buffer
            if (SIMD8_lane < num_children)
            {
                uint store_pos = (root_buffer_position + SIMD8_lane) % RING_SIZE;
                SLM_local_root_buffer[store_pos] = children_info.xy;
            }
        }

        // sync everyone
        barrier( CLK_LOCAL_MEM_FENCE );

        ring_head += nodes_consumed;
        ring_tail = *SLM_ring_tail;
        ring_size = ring_tail - ring_head;
    }

    return ring_head;
}




inline void amplify_and_spill(
    global struct SAHBuildGlobals* globals,
    dword alloc_backpointers,
    uint first_qnode,
    uint first_bvh2_node,
    global uint2* global_root_buffer,
    local uint* root_buffer_counter,
    const uint  RING_SIZE
)

{
    struct BuildFlatTreeArgs args;
    args.leaf_size_in_bytes = SAHBuildGlobals_GetLeafSizeInBytes(globals);
    args.leaf_type = SAHBuildGlobals_GetLeafType(globals);
    args.inner_node_type = SAHBuildGlobals_GetInternalNodeType(globals);
    args.primref_indices = SAHBuildGlobals_GetPrimrefIndices_In(globals, 0);
    args.primref_buffer = SAHBuildGlobals_GetPrimrefs(globals);
    args.bvh_base = SAHBuildGlobals_GetBVHBase(globals);
    args.bvh2 = SAHBuildGlobals_GetBVH2(globals);
    args.globals = (global struct Globals*) globals->p_globals;

    global struct InternalNode* qnodes = (global struct InternalNode*) BVHBase_GetInternalNodes(args.bvh_base);
    global uint* back_pointers = (global uint*) BVHBase_GetBackPointers(args.bvh_base);


    varying uint3 children_info;
    uniform ushort num_children = SUBGROUP_BuildFlatTreeNode(args, first_bvh2_node, qnodes + first_qnode, first_qnode, &children_info);

    if (alloc_backpointers)
    {
        // set first node's backpointer
        if (get_sub_group_local_id() == 0)
        {
            // if first node is root, use root sentinel in backpointer
            //   otherwise, need to merge the child count in with the parent offset (which was already put there by the parent's thread)
            uint bp = 0xffffffc0;
            if (first_qnode != 0)
                bp = back_pointers[first_qnode];
            bp |= (children_info.z << 3);

            back_pointers[first_qnode] = bp;
        }

        // point child backpointers at the parent
        if (get_sub_group_local_id() < num_children)
            back_pointers[children_info.y] = (first_qnode << 6);
    }

    if (num_children)
    {
        uint spill_pos = 0;
        if (get_sub_group_local_id() == 0)
            spill_pos = atomic_add_local(root_buffer_counter,num_children);

        spill_pos = sub_group_broadcast(spill_pos, 0);

        if (get_sub_group_local_id() < num_children)
            global_root_buffer[spill_pos+get_sub_group_local_id()] = children_info.xy;
    }

}




inline void build_qnodes_pc_kickoff_func(
    global struct SAHBuildGlobals* globals,
    global uint2* root_buffer,
    bool alloc_backpointers,
    bool process_masks,

    local uint2* SLM_local_root_buffer,
    local uint* SLM_spill_pos,
    local uint* SLM_ring_tail,
    int RING_SIZE
)
{
    // allocate first node
    if ( get_sub_group_id() == 0 && get_sub_group_local_id() == 0 )
        allocate_inner_nodes( SAHBuildGlobals_GetBVHBase(globals), 1 );

    *SLM_spill_pos=0;

    uint ring_head = build_qnodes_pc( globals, alloc_backpointers, process_masks,
                     0, BVH2_GetRoot(SAHBuildGlobals_GetBVH2(globals)), SLM_local_root_buffer, SLM_ring_tail, RING_SIZE );


    uint n = *SLM_ring_tail - ring_head;
    if (n > 0)
    {
#if 0
        // do an additional round of amplification so we can get more nodes into the root buffer and go wider in the next phase
        /// JDB TODO: this is causing hangs on DG2 for metro, so disabling for now...
        for (uint i = get_sub_group_id(); i < n; i+= get_num_sub_groups() )
        {
            uint consume_pos = (ring_head + i) % RING_SIZE;
            uniform uint bvh2_root = SLM_local_root_buffer[consume_pos].x;
            uniform uint qnode_root = SLM_local_root_buffer[consume_pos].y;

            amplify_and_spill( globals, alloc_backpointers, qnode_root, bvh2_root, root_buffer, SLM_spill_pos, RING_SIZE );
        }

        barrier( CLK_LOCAL_MEM_FENCE );
#else
        for (uint i = get_local_id(0); i < n; i += get_local_size(0))
            root_buffer[i] = SLM_local_root_buffer[(ring_head + i) % RING_SIZE];
#endif

        if (get_local_id(0) == 0)
        {
            globals->root_buffer_num_produced = n;
            globals->root_buffer_num_produced_hi = 0;
            globals->root_buffer_num_consumed = 0;
            globals->root_buffer_num_consumed_hi = 0;
        }
    }
}




GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( 256, 1, 1 )) )
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
build_qnodes_pc_kickoff(
    global struct SAHBuildGlobals* globals,
    global uint2* root_buffer,
    dword sah_flags
)
{
    bool alloc_backpointers = sah_flags & SAH_FLAG_NEED_BACKPOINTERS;
    bool process_masks = sah_flags & SAH_FLAG_NEED_MASKS;


    const int RING_SIZE = 64;

    local uint2 SLM_local_root_buffer[RING_SIZE];
    local uint SLM_spill_pos;
    local uint SLM_ring_tail;

    build_qnodes_pc_kickoff_func(globals,
                                 root_buffer,
                                 alloc_backpointers,
                                 process_masks,
                                 SLM_local_root_buffer,
                                 &SLM_spill_pos,
                                 &SLM_ring_tail,
                                 RING_SIZE
                                 );
}




inline void build_qnodes_pc_amplify_func(
    global struct SAHBuildGlobals* globals,
    global uint2* root_buffer,
    bool alloc_backpointers,
    bool process_masks,

    local uint2* SLM_local_root_buffer,
    local uint*  SLM_broadcast,
    local uint*  SLM_ring_tail,
    int RING_SIZE
    )
{
    // TODO_OPT:  Probably don't need this atomic.. could clear 'num_consumed' every time
    //     and just use get_group_id()
    //

    if (get_local_id(0) == 0)
        *SLM_broadcast = atomic_inc_global(&globals->root_buffer_num_consumed);

    barrier( CLK_LOCAL_MEM_FENCE );

    uniform uint consume_pos = *SLM_broadcast;
    uniform uint bvh2_root = root_buffer[consume_pos].x;
    uniform uint qnode_root = root_buffer[consume_pos].y;

    uint ring_head = build_qnodes_pc(globals, alloc_backpointers,process_masks,
        qnode_root, bvh2_root, SLM_local_root_buffer, SLM_ring_tail, RING_SIZE);

    // TODO_OPT:  Instead of spilling the nodes, do one more round of amplification and write
    //   generated children directly into the root buffer.  This should allow faster amplification

    // spill root buffer contents
    uint n = *SLM_ring_tail - ring_head;
    if (n > 0)
    {

        if (get_local_id(0) == 0)
            *SLM_broadcast = atomic_add_global(&globals->root_buffer_num_produced, n);

        barrier( CLK_LOCAL_MEM_FENCE );
        uint produce_pos = *SLM_broadcast;

        for (uint i = get_local_id(0); i < n; i += get_local_size(0))
            root_buffer[produce_pos + i] = SLM_local_root_buffer[(ring_head + i) % RING_SIZE];
    }
}





// Process two nodes per wg during amplification phase.
// DOing it this way ensures maximum parallelism
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(16, 1, 1)))
__attribute__((intel_reqd_sub_group_size(16)))
kernel void
build_qnodes_pc_amplify(
    global struct SAHBuildGlobals* globals,
    global uint2* root_buffer,
    dword sah_flags )
{
    bool alloc_backpointers = sah_flags & SAH_FLAG_NEED_BACKPOINTERS;

    struct BuildFlatTreeArgs args;
    args.leaf_size_in_bytes = SAHBuildGlobals_GetLeafSizeInBytes(globals);
    args.leaf_type = SAHBuildGlobals_GetLeafType(globals);
    args.inner_node_type = SAHBuildGlobals_GetInternalNodeType(globals);
    args.primref_indices = SAHBuildGlobals_GetPrimrefIndices_In(globals, 0);
    args.primref_buffer = SAHBuildGlobals_GetPrimrefs(globals);
    args.bvh_base = SAHBuildGlobals_GetBVHBase(globals);
    args.bvh2 = SAHBuildGlobals_GetBVH2(globals);
    args.globals = (global struct Globals*) globals->p_globals;
    args.do_mask_processing = sah_flags & SAH_FLAG_NEED_MASKS;

    global struct InternalNode* qnodes = (global struct InternalNode*) BVHBase_GetInternalNodes(args.bvh_base);
    global uint* back_pointers = (global uint*) BVHBase_GetBackPointers(args.bvh_base);

    ushort SIMD16_lane = get_sub_group_local_id();

    // SIMD16 as 2xSIMD8
    ushort SIMD8_lane = get_sub_group_local_id() % 8;
    ushort SIMD8_id = get_sub_group_local_id() / 8;
    bool active_lane = false;

    uint consume_pos;
    consume_pos = globals->root_buffer_num_consumed + get_group_id(0) * 2; // times 2 because we process two nodes in workgroup
    consume_pos += SIMD8_id;

    active_lane = consume_pos < globals->root_buffer_num_to_consume ? true : false;
    consume_pos = consume_pos < globals->root_buffer_num_to_consume ? consume_pos : consume_pos-1;

    uint first_bvh2_node = root_buffer[consume_pos].x;
    uint first_qnode = root_buffer[consume_pos].y;

    varying uint3 children_info;
    ushort num_children = SUBGROUP_BuildFlatTreeNode_2xSIMD8_in_SIMD16(args, first_bvh2_node, qnodes, first_qnode, &children_info, active_lane);

    if (alloc_backpointers && active_lane)
    {
        // set first node's backpointer
        if (SIMD8_lane == 0)
        {
            // if first node is root, use root sentinel in backpointer
            //   otherwise, need to merge the child count in with the parent offset (which was already put there by the parent's thread)
            uint bp = 0xffffffc0;
            if (first_qnode != 0)
                bp = back_pointers[first_qnode];
            bp |= (children_info.z << 3);

            back_pointers[first_qnode] = bp;
        }

        // point child backpointers at the parent
        if (SIMD8_lane < num_children)
            back_pointers[children_info.y] = (first_qnode << 6);
    }

    // save data
    {
        // sum children from both halfs of SIMD16 to do only one atomic per sub_group
        uint produce_pos;
        uniform ushort first_SIMD8_num_children = sub_group_broadcast(num_children, 0);
        uniform ushort second_SIMD8_num_children = sub_group_broadcast(num_children, 8);
        uniform ushort SIMD16_num_children = first_SIMD8_num_children + second_SIMD8_num_children;

        if (SIMD16_lane == 0 && SIMD16_num_children)
            produce_pos = atomic_add_global(&globals->root_buffer_num_produced, SIMD16_num_children);

        produce_pos = sub_group_broadcast(produce_pos, 0);
        produce_pos += SIMD8_id * first_SIMD8_num_children; // update offset for second half of SIMD16

        if (SIMD8_lane < num_children)
        {
            root_buffer[produce_pos + SIMD8_lane] = children_info.xy;
        }
    }
}


//////////
//
// Batched version of qnode creation
//
//////////




GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(1, 1, 1)))
kernel void
build_qnodes_init_scheduler_batched(global struct QnodeScheduler* scheduler, dword num_builds, dword num_max_qnode_global_root_buffer_entries)
{

    scheduler->batched_build_offset = scheduler->num_trivial_builds + scheduler->num_single_builds;
    scheduler->batched_build_count = num_builds - scheduler->batched_build_offset;
    scheduler->num_max_qnode_global_root_buffer_entries = num_max_qnode_global_root_buffer_entries;

    const uint num_builds_to_process = scheduler->batched_build_count;
    const uint max_qnode_grb_entries = scheduler->num_max_qnode_global_root_buffer_entries;

    scheduler->batched_builds_to_process = num_builds_to_process;
    scheduler->num_qnode_grb_curr_entries = (num_builds_to_process + 15) / 16; // here we store number of workgroups for "build_qnodes_begin_batchable" kernel
    scheduler->num_qnode_grb_new_entries = num_builds_to_process;
    scheduler->qnode_global_root_buffer.curr_entries_offset = max_qnode_grb_entries;
}




GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__((reqd_work_group_size(16, 1, 1)))
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
build_qnodes_begin_batchable(global struct QnodeScheduler* scheduler,
                             global struct SAHBuildGlobals* builds_globals)
{
    const uint tid = get_group_id(0) * get_local_size(0) + get_local_id(0);

    const uint num_builds_to_process = scheduler->batched_builds_to_process;

    if(tid < num_builds_to_process)
    {
        const uint build_idx = scheduler->batched_build_offset + tid;

        uint bvh2_node = BVH2_GetRoot(SAHBuildGlobals_GetBVH2(&builds_globals[build_idx]));
        uint qnode = 0;
        struct QNodeGlobalRootBufferEntry entry = { bvh2_node, qnode, build_idx, 1};
        scheduler->qnode_global_root_buffer.entries[tid] = entry;

        builds_globals[build_idx].root_buffer_num_produced = 0;
        builds_globals[build_idx].root_buffer_num_produced_hi = 0;
        builds_globals[build_idx].root_buffer_num_consumed = 0;
        builds_globals[build_idx].root_buffer_num_consumed_hi = 0;

        // allocate first node for this build
        //allocate_inner_nodes( SAHBuildGlobals_GetBVHBase(&builds_globals[build_idx]), 1 );
        SAHBuildGlobals_GetBVHBase(&builds_globals[build_idx])->nodeDataCur++;
    }
}




GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( 1, 1, 1 )) )
kernel void
build_qnodes_scheduler(global struct QnodeScheduler* scheduler)
{
    const uint max_qnode_grb_entries = scheduler->num_max_qnode_global_root_buffer_entries;

    uint new_entries = min(scheduler->num_qnode_grb_new_entries, max_qnode_grb_entries);

    scheduler->num_qnode_grb_curr_entries = new_entries;
    scheduler->num_qnode_grb_new_entries = 0;
    scheduler->qnode_global_root_buffer.curr_entries_offset = scheduler->qnode_global_root_buffer.curr_entries_offset ? 0 : max_qnode_grb_entries;
}




// TODO_OPT:  Enable larger WGs.  WGSize 512 at SIMD8 hangs on Gen9, but Gen12 can go bigger
GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( 32, 1, 1 )) )
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
build_qnodes_pc_amplify_batched(
    global struct SAHBuildGlobals* builds_globals,
    global struct QnodeScheduler* scheduler
    )
{
    const uint group_id = get_group_id(0);

    global struct QNodeGlobalRootBuffer* global_root_buffer = &scheduler->qnode_global_root_buffer;
    const uint curr_entries_offset = global_root_buffer->curr_entries_offset;
    struct QNodeGlobalRootBufferEntry entry = global_root_buffer->entries[curr_entries_offset + group_id];

    const uint build_id = entry.build_idx;

    global struct SAHBuildGlobals* globals = &builds_globals[build_id];
    global uint2* root_buffer = (global uint2*)globals->p_qnode_root_buffer;
    bool alloc_backpointers = SAHBuildGlobals_NeedBackPointers(globals);
    bool process_masks = SAHBuildGlobals_NeedMasks(globals);

    const int RING_SIZE = 32; // for 2 SGs, 16 should result in 2 rounds:  one SG produces 6, then 2 SGs consume 2 and produce 12
                              // for 4 SGs, 32 results in 2 rounds:  one SG produces 6, 4 SGs consume 4 and produce 24, resulting in 26

    local uint2 SLM_local_root_buffer[RING_SIZE];
    local uint  SLM_broadcast;
    local uint  SLM_ring_tail;
    local uint  SLM_grb_broadcast;


    //// This below can be moved to separate function if needed for TLAS ////

    uniform uint bvh2_root = entry.bvh2_node;
    uniform uint qnode_root = entry.qnode;

    uint ring_head = build_qnodes_pc(globals, alloc_backpointers, process_masks,
        qnode_root, bvh2_root, SLM_local_root_buffer, &SLM_ring_tail, RING_SIZE);

    // spill root buffer contents
    uint n = SLM_ring_tail - ring_head;
    if (n > 0)
    {
        const uint max_qnode_grb_entries = scheduler->num_max_qnode_global_root_buffer_entries;

        if (get_local_id(0) == 0)
        {
            SLM_grb_broadcast = atomic_add_global(&scheduler->num_qnode_grb_new_entries, n);

            if(SLM_grb_broadcast >= max_qnode_grb_entries) // if global_root_buffer is full, then make space in build's root_buffer
                SLM_broadcast = atomic_add_global(&globals->root_buffer_num_produced, n);
            else if( (SLM_grb_broadcast + n) >= max_qnode_grb_entries) // if we exceed global_root_buffer with our entries, then make space in build's root_buffer
                SLM_broadcast = atomic_add_global(&globals->root_buffer_num_produced, n - (max_qnode_grb_entries - SLM_grb_broadcast));
        }

        barrier( CLK_LOCAL_MEM_FENCE );

        uint produce_pos = SLM_broadcast;

        uint grb_produce_num = n; // grb stands for global_root_buffer
        uint lrb_produce_num = 0; // lrb stands for local root buffer, meaning this build's root_buffer

        if(SLM_grb_broadcast >= max_qnode_grb_entries) // if global_root_buffer is full, don't write to it
        {
            grb_produce_num = 0;
            lrb_produce_num = n;
        }
        else if( (SLM_grb_broadcast + n) >= max_qnode_grb_entries) // if we exceed global_root_buffer with our entries, then decrease amount of entries and store rest in build's root buffer
        {
            grb_produce_num = max_qnode_grb_entries - SLM_grb_broadcast;
            lrb_produce_num = n - grb_produce_num;
        }

        // save data to global_root_buffer
        for(uint i = get_local_id(0); i < grb_produce_num; i += get_local_size(0))
        {
            const uint2 slm_record = SLM_local_root_buffer[(ring_head + i) % RING_SIZE];

            struct QNodeGlobalRootBufferEntry new_entry;
            new_entry.bvh2_node = slm_record.x;
            new_entry.qnode = slm_record.y;
            new_entry.build_idx = entry.build_idx;

            const uint new_entries_offset = curr_entries_offset ? 0 : max_qnode_grb_entries;
            global_root_buffer->entries[new_entries_offset + SLM_grb_broadcast + i] = new_entry;
        }

        // if anything left, write to build's root buffer
        for (uint i = get_local_id(0); i < lrb_produce_num; i += get_local_size(0))
            root_buffer[produce_pos + i] = SLM_local_root_buffer[(ring_head + i + grb_produce_num) % RING_SIZE];
    }
}




GRL_ANNOTATE_IGC_DO_NOT_SPILL
__attribute__( (reqd_work_group_size( 16, 1, 1 )) )
__attribute__( (intel_reqd_sub_group_size( 16 )) )
kernel void
build_qnodes_try_to_fill_grb_batched(
    global struct SAHBuildGlobals* builds_globals,
    global struct QnodeScheduler* scheduler
    )
{
    const uint build_id = scheduler->batched_build_offset + get_group_id(0);
    global struct SAHBuildGlobals* globals = &builds_globals[build_id];
    global uint2* root_buffer = (global uint2*)globals->p_qnode_root_buffer;

    global struct QNodeGlobalRootBuffer* qnode_root_buffer = (global struct QNodeGlobalRootBuffer*)&scheduler->qnode_global_root_buffer;

    const uint num_produced = globals->root_buffer_num_produced;
    const uint num_consumed = globals->root_buffer_num_consumed;
    const uint entries =  num_produced - num_consumed; // entries to build's root buffer

    if(!entries)
        return;

    uint global_root_buffer_offset;
    if(get_local_id(0) == 0)
        global_root_buffer_offset = atomic_add_global(&scheduler->num_qnode_grb_new_entries, entries);

    global_root_buffer_offset = sub_group_broadcast(global_root_buffer_offset, 0);

    const uint max_qnode_grb_entries = scheduler->num_max_qnode_global_root_buffer_entries;

    if(global_root_buffer_offset >= max_qnode_grb_entries) // if global_root_buffer is full, then return
        return;

    uint global_root_buffer_produce_num = entries;
    if(global_root_buffer_offset + entries >= max_qnode_grb_entries) // if we exceed global_root_buffer with our entries, then reduce number of entries to push
        global_root_buffer_produce_num = max_qnode_grb_entries - global_root_buffer_offset;

    for(uint i = get_local_id(0); i < global_root_buffer_produce_num; i += get_local_size(0))
    {
        const uint2 entry = root_buffer[num_consumed + i];

        struct QNodeGlobalRootBufferEntry new_entry;
        new_entry.bvh2_node = entry.x;
        new_entry.qnode = entry.y;
        new_entry.build_idx = build_id;

        const uint new_entries_offset = qnode_root_buffer->curr_entries_offset ? 0 : max_qnode_grb_entries;
        qnode_root_buffer->entries[new_entries_offset + global_root_buffer_offset + i] = new_entry;
    }

    if(get_local_id(0) == 0)
        globals->root_buffer_num_consumed += global_root_buffer_produce_num;
}