summaryrefslogtreecommitdiff
path: root/src/gallium/drivers/swr/rasterizer/core/api.cpp
blob: 31f9fe2c2bbf3867b399802c89ccca80ac4e2206 (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
/****************************************************************************
 * Copyright (C) 2014-2018 Intel Corporation.   All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * @file api.cpp
 *
 * @brief API implementation
 *
 ******************************************************************************/

#include <cfloat>
#include <cmath>
#include <cstdio>
#include <new>

#include "core/api.h"
#include "core/backend.h"
#include "core/context.h"
#include "core/depthstencil.h"
#include "core/frontend.h"
#include "core/rasterizer.h"
#include "core/rdtsc_core.h"
#include "core/threads.h"
#include "core/tilemgr.h"
#include "core/clip.h"
#include "core/utils.h"
#include "core/tileset.h"

#include "common/os.h"

static const SWR_RECT g_MaxScissorRect = {0, 0, KNOB_MAX_SCISSOR_X, KNOB_MAX_SCISSOR_Y};

void SetupDefaultState(SWR_CONTEXT* pContext);

static INLINE SWR_CONTEXT* GetContext(HANDLE hContext)
{
    return (SWR_CONTEXT*)hContext;
}

void WakeAllThreads(SWR_CONTEXT* pContext)
{
    pContext->FifosNotEmpty.notify_all();
}

//////////////////////////////////////////////////////////////////////////
/// @brief Create SWR Context.
/// @param pCreateInfo - pointer to creation info.
HANDLE SwrCreateContext(SWR_CREATECONTEXT_INFO* pCreateInfo)
{
    void* pContextMem = AlignedMalloc(sizeof(SWR_CONTEXT), KNOB_SIMD_WIDTH * 4);
    memset(pContextMem, 0, sizeof(SWR_CONTEXT));
    SWR_CONTEXT* pContext = new (pContextMem) SWR_CONTEXT();

    pContext->privateStateSize = pCreateInfo->privateStateSize;

    // initialize callback functions
    pContext->pfnLoadTile                = pCreateInfo->pfnLoadTile;
    pContext->pfnStoreTile               = pCreateInfo->pfnStoreTile;
    pContext->pfnTranslateGfxptrForRead  = pCreateInfo->pfnTranslateGfxptrForRead;
    pContext->pfnTranslateGfxptrForWrite = pCreateInfo->pfnTranslateGfxptrForWrite;
    pContext->pfnMakeGfxPtr              = pCreateInfo->pfnMakeGfxPtr;
    pContext->pfnCreateMemoryContext     = pCreateInfo->pfnCreateMemoryContext;
    pContext->pfnDestroyMemoryContext    = pCreateInfo->pfnDestroyMemoryContext;
    pContext->pfnUpdateSoWriteOffset     = pCreateInfo->pfnUpdateSoWriteOffset;
    pContext->pfnUpdateStats             = pCreateInfo->pfnUpdateStats;
    pContext->pfnUpdateStatsFE           = pCreateInfo->pfnUpdateStatsFE;
    pContext->pfnUpdateStreamOut         = pCreateInfo->pfnUpdateStreamOut;


    pContext->hExternalMemory = pCreateInfo->hExternalMemory;

    pContext->MAX_DRAWS_IN_FLIGHT = KNOB_MAX_DRAWS_IN_FLIGHT;
    if (pCreateInfo->MAX_DRAWS_IN_FLIGHT != 0)
    {
        pContext->MAX_DRAWS_IN_FLIGHT = pCreateInfo->MAX_DRAWS_IN_FLIGHT;
    }

    pContext->dcRing.Init(pContext->MAX_DRAWS_IN_FLIGHT);
    pContext->dsRing.Init(pContext->MAX_DRAWS_IN_FLIGHT);

    pContext->pMacroTileManagerArray =
        (MacroTileMgr*)AlignedMalloc(sizeof(MacroTileMgr) * pContext->MAX_DRAWS_IN_FLIGHT, 64);
    pContext->pDispatchQueueArray =
        (DispatchQueue*)AlignedMalloc(sizeof(DispatchQueue) * pContext->MAX_DRAWS_IN_FLIGHT, 64);

    for (uint32_t dc = 0; dc < pContext->MAX_DRAWS_IN_FLIGHT; ++dc)
    {
        pContext->dcRing[dc].pArena = new CachingArena(pContext->cachingArenaAllocator);
        new (&pContext->pMacroTileManagerArray[dc]) MacroTileMgr(*pContext->dcRing[dc].pArena);
        new (&pContext->pDispatchQueueArray[dc]) DispatchQueue();

        pContext->dsRing[dc].pArena = new CachingArena(pContext->cachingArenaAllocator);
    }

    if (pCreateInfo->pThreadInfo)
    {
        pContext->threadInfo = *pCreateInfo->pThreadInfo;
    }
    else
    {
        pContext->threadInfo.MAX_WORKER_THREADS      = KNOB_MAX_WORKER_THREADS;
        pContext->threadInfo.BASE_NUMA_NODE          = KNOB_BASE_NUMA_NODE;
        pContext->threadInfo.BASE_CORE               = KNOB_BASE_CORE;
        pContext->threadInfo.BASE_THREAD             = KNOB_BASE_THREAD;
        pContext->threadInfo.MAX_NUMA_NODES          = KNOB_MAX_NUMA_NODES;
        pContext->threadInfo.MAX_CORES_PER_NUMA_NODE = KNOB_MAX_CORES_PER_NUMA_NODE;
        pContext->threadInfo.MAX_THREADS_PER_CORE    = KNOB_MAX_THREADS_PER_CORE;
        pContext->threadInfo.SINGLE_THREADED         = KNOB_SINGLE_THREADED;
    }

    if (pCreateInfo->pApiThreadInfo)
    {
        pContext->apiThreadInfo = *pCreateInfo->pApiThreadInfo;
    }
    else
    {
        pContext->apiThreadInfo.bindAPIThread0        = true;
        pContext->apiThreadInfo.numAPIReservedThreads = 1;
        pContext->apiThreadInfo.numAPIThreadsPerCore  = 1;
    }

    if (pCreateInfo->pWorkerPrivateState)
    {
        pContext->workerPrivateState = *pCreateInfo->pWorkerPrivateState;
    }

    memset((void*)&pContext->WaitLock, 0, sizeof(pContext->WaitLock));
    memset((void*)&pContext->FifosNotEmpty, 0, sizeof(pContext->FifosNotEmpty));
    new (&pContext->WaitLock) std::mutex();
    new (&pContext->FifosNotEmpty) std::condition_variable();

    CreateThreadPool(pContext, &pContext->threadPool);

    if (pContext->apiThreadInfo.bindAPIThread0)
    {
        BindApiThread(pContext, 0);
    }

    if (pContext->threadInfo.SINGLE_THREADED)
    {
        pContext->pSingleThreadLockedTiles = new TileSet();
    }

    pContext->ppScratch = new uint8_t*[pContext->NumWorkerThreads];
    pContext->pStats =
        (SWR_STATS*)AlignedMalloc(sizeof(SWR_STATS) * pContext->NumWorkerThreads, 64);

#if defined(KNOB_ENABLE_AR)
    // Setup ArchRast thread contexts which includes +1 for API thread.
    pContext->pArContext = new HANDLE[pContext->NumWorkerThreads + 1];
    pContext->pArContext[pContext->NumWorkerThreads] =
        ArchRast::CreateThreadContext(ArchRast::AR_THREAD::API);
#endif

#if defined(KNOB_ENABLE_RDTSC)
    pContext->pBucketMgr = new BucketManager(pCreateInfo->contextName);
    RDTSC_RESET(pContext->pBucketMgr);
    RDTSC_INIT(pContext->pBucketMgr, 0);
#endif

    // Allocate scratch space for workers.
    ///@note We could lazily allocate this but its rather small amount of memory.
    for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
    {
#if defined(_WIN32)
        uint32_t numaNode =
            pContext->threadPool.pThreadData ? pContext->threadPool.pThreadData[i].numaId : 0;
        pContext->ppScratch[i] = (uint8_t*)VirtualAllocExNuma(GetCurrentProcess(),
                                                              nullptr,
                                                              KNOB_WORKER_SCRATCH_SPACE_SIZE,
                                                              MEM_RESERVE | MEM_COMMIT,
                                                              PAGE_READWRITE,
                                                              numaNode);
#else
        pContext->ppScratch[i] =
            (uint8_t*)AlignedMalloc(KNOB_WORKER_SCRATCH_SPACE_SIZE, KNOB_SIMD_WIDTH * 4);
#endif

#if defined(KNOB_ENABLE_AR)
        // Initialize worker thread context for ArchRast.
        pContext->pArContext[i] = ArchRast::CreateThreadContext(ArchRast::AR_THREAD::WORKER);

        SWR_WORKER_DATA* pWorkerData = (SWR_WORKER_DATA*)pContext->threadPool.pThreadData[i].pWorkerPrivateData;
        pWorkerData->hArContext = pContext->pArContext[i];
#endif


    }

#if defined(KNOB_ENABLE_AR)
    // cache the API thread event manager, for use with sim layer
    pCreateInfo->hArEventManager = pContext->pArContext[pContext->NumWorkerThreads];
#endif

    // State setup AFTER context is fully initialized
    SetupDefaultState(pContext);

    // initialize hot tile manager
    pContext->pHotTileMgr = new HotTileMgr();

    // pass pointer to bucket manager back to caller
#ifdef KNOB_ENABLE_RDTSC
    pCreateInfo->pBucketMgr = pContext->pBucketMgr;
#endif

    pCreateInfo->contextSaveSize = sizeof(API_STATE);

    StartThreadPool(pContext, &pContext->threadPool);

    return (HANDLE)pContext;
}

void CopyState(DRAW_STATE& dst, const DRAW_STATE& src)
{
    memcpy((void*)&dst.state, (void*)&src.state, sizeof(API_STATE));
}

template <bool IsDraw>
void QueueWork(SWR_CONTEXT* pContext)
{
    DRAW_CONTEXT* pDC     = pContext->pCurDrawContext;
    uint32_t      dcIndex = pDC->drawId % pContext->MAX_DRAWS_IN_FLIGHT;

    if (IsDraw)
    {
        pDC->pTileMgr = &pContext->pMacroTileManagerArray[dcIndex];
        pDC->pTileMgr->initialize();
    }

    // Each worker thread looks at a DC for both FE and BE work at different times and so we
    // multiply threadDone by 2.  When the threadDone counter has reached 0 then all workers
    // have moved past this DC. (i.e. Each worker has checked this DC for both FE and BE work and
    // then moved on if all work is done.)
    pContext->pCurDrawContext->threadsDone = pContext->NumFEThreads + pContext->NumBEThreads;

    if (IsDraw)
    {
        InterlockedIncrement(&pContext->drawsOutstandingFE);
    }

    _ReadWriteBarrier();
    {
        std::unique_lock<std::mutex> lock(pContext->WaitLock);
        pContext->dcRing.Enqueue();
    }

    if (pContext->threadInfo.SINGLE_THREADED)
    {
        uint32_t mxcsr = SetOptimalVectorCSR();

        if (IsDraw)
        {
            uint32_t curDraw[2] = {pContext->pCurDrawContext->drawId,
                                   pContext->pCurDrawContext->drawId};
            WorkOnFifoFE(pContext, 0, curDraw[0]);
            WorkOnFifoBE(pContext, 0, curDraw[1], *pContext->pSingleThreadLockedTiles, 0, 0);
        }
        else
        {
            uint32_t curDispatch = pContext->pCurDrawContext->drawId;
            WorkOnCompute(pContext, 0, curDispatch);
        }

        // Dequeue the work here, if not already done, since we're single threaded (i.e. no
        // workers).
        while (CompleteDrawContext(pContext, pContext->pCurDrawContext) > 0)
        {
        }

        // restore csr
        RestoreVectorCSR(mxcsr);
    }
    else
    {
        RDTSC_BEGIN(pContext->pBucketMgr, APIDrawWakeAllThreads, pDC->drawId);
        WakeAllThreads(pContext);
        RDTSC_END(pContext->pBucketMgr, APIDrawWakeAllThreads, 1);
    }

    // Set current draw context to NULL so that next state call forces a new draw context to be
    // created and populated.
    pContext->pPrevDrawContext = pContext->pCurDrawContext;
    pContext->pCurDrawContext  = nullptr;
}

INLINE void QueueDraw(SWR_CONTEXT* pContext)
{
    QueueWork<true>(pContext);
}

INLINE void QueueDispatch(SWR_CONTEXT* pContext)
{
    QueueWork<false>(pContext);
}

DRAW_CONTEXT* GetDrawContext(SWR_CONTEXT* pContext, bool isSplitDraw = false)
{
    RDTSC_BEGIN(pContext->pBucketMgr, APIGetDrawContext, 0);
    // If current draw context is null then need to obtain a new draw context to use from ring.
    if (pContext->pCurDrawContext == nullptr)
    {
        // Need to wait for a free entry.
        while (pContext->dcRing.IsFull())
        {
            _mm_pause();
        }

        uint64_t curDraw = pContext->dcRing.GetHead();
        uint32_t dcIndex = curDraw % pContext->MAX_DRAWS_IN_FLIGHT;

        if ((pContext->frameCount - pContext->lastFrameChecked) > 2 ||
            (curDraw - pContext->lastDrawChecked) > 0x10000)
        {
            // Take this opportunity to clean-up old arena allocations
            pContext->cachingArenaAllocator.FreeOldBlocks();

            pContext->lastFrameChecked = pContext->frameCount;
            pContext->lastDrawChecked  = curDraw;
        }

        DRAW_CONTEXT* pCurDrawContext = &pContext->dcRing[dcIndex];
        pContext->pCurDrawContext     = pCurDrawContext;

        // Assign next available entry in DS ring to this DC.
        uint32_t dsIndex        = pContext->curStateId % pContext->MAX_DRAWS_IN_FLIGHT;
        pCurDrawContext->pState = &pContext->dsRing[dsIndex];

        // Copy previous state to current state.
        if (pContext->pPrevDrawContext)
        {
            DRAW_CONTEXT* pPrevDrawContext = pContext->pPrevDrawContext;

            // If we're splitting our draw then we can just use the same state from the previous
            // draw. In this case, we won't increment the DS ring index so the next non-split
            // draw can receive the state.
            if (isSplitDraw == false)
            {
                CopyState(*pCurDrawContext->pState, *pPrevDrawContext->pState);

                // Should have been cleaned up previously
                SWR_ASSERT(pCurDrawContext->pState->pArena->IsEmpty() == true);

                pCurDrawContext->pState->pPrivateState = nullptr;

                pContext->curStateId++; // Progress state ring index forward.
            }
            else
            {
                // If its a split draw then just copy the state pointer over
                // since its the same draw.
                pCurDrawContext->pState = pPrevDrawContext->pState;
                SWR_ASSERT(pPrevDrawContext->cleanupState == false);
            }
        }
        else
        {
            SWR_ASSERT(pCurDrawContext->pState->pArena->IsEmpty() == true);
            pContext->curStateId++; // Progress state ring index forward.
        }

        SWR_ASSERT(pCurDrawContext->pArena->IsEmpty() == true);

        // Reset dependency
        pCurDrawContext->dependent   = false;
        pCurDrawContext->dependentFE = false;

        pCurDrawContext->pContext  = pContext;
        pCurDrawContext->isCompute = false; // Dispatch has to set this to true.

        pCurDrawContext->doneFE                         = false;
        pCurDrawContext->FeLock                         = 0;
        pCurDrawContext->threadsDone                    = 0;
        pCurDrawContext->retireCallback.pfnCallbackFunc = nullptr;

        pCurDrawContext->dynState.Reset(pContext->NumWorkerThreads);

        // Assign unique drawId for this DC
        pCurDrawContext->drawId = pContext->dcRing.GetHead();

        pCurDrawContext->cleanupState = true;
    }
    else
    {
        SWR_ASSERT(isSplitDraw == false, "Split draw should only be used when obtaining a new DC");
    }

    RDTSC_END(pContext->pBucketMgr, APIGetDrawContext, 0);
    return pContext->pCurDrawContext;
}

API_STATE* GetDrawState(SWR_CONTEXT* pContext)
{
    DRAW_CONTEXT* pDC = GetDrawContext(pContext);
    SWR_ASSERT(pDC->pState != nullptr);

    return &pDC->pState->state;
}

void SwrDestroyContext(HANDLE hContext)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    pDC->FeWork.type    = SHUTDOWN;
    pDC->FeWork.pfnWork = ProcessShutdown;

    // enqueue
    QueueDraw(pContext);

    DestroyThreadPool(pContext, &pContext->threadPool);

    // free the fifos
    for (uint32_t i = 0; i < pContext->MAX_DRAWS_IN_FLIGHT; ++i)
    {
        AlignedFree(pContext->dcRing[i].dynState.pStats);
        delete pContext->dcRing[i].pArena;
        delete pContext->dsRing[i].pArena;
        pContext->pMacroTileManagerArray[i].~MacroTileMgr();
        pContext->pDispatchQueueArray[i].~DispatchQueue();
    }

    AlignedFree(pContext->pDispatchQueueArray);
    AlignedFree(pContext->pMacroTileManagerArray);

    // Free scratch space.
    for (uint32_t i = 0; i < pContext->NumWorkerThreads; ++i)
    {
#if defined(_WIN32)
        VirtualFree(pContext->ppScratch[i], 0, MEM_RELEASE);
#else
        AlignedFree(pContext->ppScratch[i]);
#endif

#if defined(KNOB_ENABLE_AR)
        ArchRast::DestroyThreadContext(pContext->pArContext[i]);
#endif
    }

#if defined(KNOB_ENABLE_RDTSC)
    delete pContext->pBucketMgr;
#endif

    delete[] pContext->ppScratch;
    AlignedFree(pContext->pStats);

    delete pContext->pHotTileMgr;
    delete pContext->pSingleThreadLockedTiles;

    pContext->~SWR_CONTEXT();
    AlignedFree(GetContext(hContext));
}

void SwrBindApiThread(HANDLE hContext, uint32_t apiThreadId)
{
    SWR_CONTEXT* pContext = GetContext(hContext);
    BindApiThread(pContext, apiThreadId);
}

void SWR_API SwrSaveState(HANDLE hContext, void* pOutputStateBlock, size_t memSize)
{
    SWR_CONTEXT* pContext = GetContext(hContext);
    auto         pSrc     = GetDrawState(pContext);
    assert(pOutputStateBlock && memSize >= sizeof(*pSrc));

    memcpy(pOutputStateBlock, pSrc, sizeof(*pSrc));
}

void SWR_API SwrRestoreState(HANDLE hContext, const void* pStateBlock, size_t memSize)
{
    SWR_CONTEXT* pContext = GetContext(hContext);
    auto         pDst     = GetDrawState(pContext);
    assert(pStateBlock && memSize >= sizeof(*pDst));

    memcpy((void*)pDst, (void*)pStateBlock, sizeof(*pDst));
}

void SetupDefaultState(SWR_CONTEXT* pContext)
{
    API_STATE* pState = GetDrawState(pContext);

    pState->rastState.cullMode     = SWR_CULLMODE_NONE;
    pState->rastState.frontWinding = SWR_FRONTWINDING_CCW;

    pState->depthBoundsState.depthBoundsTestEnable   = false;
    pState->depthBoundsState.depthBoundsTestMinValue = 0.0f;
    pState->depthBoundsState.depthBoundsTestMaxValue = 1.0f;
}

void SWR_API SwrSync(HANDLE            hContext,
                     PFN_CALLBACK_FUNC pfnFunc,
                     uint64_t          userData,
                     uint64_t          userData2,
                     uint64_t          userData3)
{
    SWR_ASSERT(pfnFunc != nullptr);

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APISync, 0);

    pDC->FeWork.type    = SYNC;
    pDC->FeWork.pfnWork = ProcessSync;

    // Setup callback function
    pDC->retireCallback.pfnCallbackFunc = pfnFunc;
    pDC->retireCallback.userData        = userData;
    pDC->retireCallback.userData2       = userData2;
    pDC->retireCallback.userData3       = userData3;

    AR_API_EVENT(SwrSyncEvent(pDC->drawId));

    // enqueue
    QueueDraw(pContext);

    RDTSC_END(pContext->pBucketMgr, APISync, 1);
}

void SwrStallBE(HANDLE hContext)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    pDC->dependent = true;
}

void SwrWaitForIdle(HANDLE hContext)
{
    SWR_CONTEXT* pContext = GetContext(hContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIWaitForIdle, 0);

    while (!pContext->dcRing.IsEmpty())
    {
        _mm_pause();
    }

    RDTSC_END(pContext->pBucketMgr, APIWaitForIdle, 1);
}

void SwrWaitForIdleFE(HANDLE hContext)
{
    SWR_CONTEXT* pContext = GetContext(hContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIWaitForIdle, 0);

    while (pContext->drawsOutstandingFE > 0)
    {
        _mm_pause();
    }

    RDTSC_END(pContext->pBucketMgr, APIWaitForIdle, 1);
}

void SwrSetVertexBuffers(HANDLE                         hContext,
                         uint32_t                       numBuffers,
                         const SWR_VERTEX_BUFFER_STATE* pVertexBuffers)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    for (uint32_t i = 0; i < numBuffers; ++i)
    {
        const SWR_VERTEX_BUFFER_STATE* pVB = &pVertexBuffers[i];
        pState->vertexBuffers[pVB->index]  = *pVB;
    }
}

void SwrSetIndexBuffer(HANDLE hContext, const SWR_INDEX_BUFFER_STATE* pIndexBuffer)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->indexBuffer = *pIndexBuffer;
}

void SwrSetFetchFunc(HANDLE hContext, PFN_FETCH_FUNC pfnFetchFunc)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->pfnFetchFunc = pfnFetchFunc;
}

void SwrSetSoFunc(HANDLE hContext, PFN_SO_FUNC pfnSoFunc, uint32_t streamIndex)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    SWR_ASSERT(streamIndex < MAX_SO_STREAMS);

    pState->pfnSoFunc[streamIndex] = pfnSoFunc;
}

void SwrSetSoState(HANDLE hContext, SWR_STREAMOUT_STATE* pSoState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->soState = *pSoState;
}

void SwrSetSoBuffers(HANDLE hContext, SWR_STREAMOUT_BUFFER* pSoBuffer, uint32_t slot)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    SWR_ASSERT((slot < MAX_SO_STREAMS), "There are only 4 SO buffer slots [0, 3]\nSlot requested: %d", slot);

    // remember buffer status in case of future resume StreamOut
    if ((pState->soBuffer[slot].pBuffer != 0) && (pSoBuffer->pBuffer == 0))
	pState->soPausedBuffer[slot] = pState->soBuffer[slot];

    // resume
    if (pState->soPausedBuffer[slot].pBuffer == pSoBuffer->pBuffer)
	pState->soBuffer[slot] = pState->soPausedBuffer[slot];
    else
        pState->soBuffer[slot] = *pSoBuffer;
}

void SwrSetVertexFunc(HANDLE hContext, PFN_VERTEX_FUNC pfnVertexFunc)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->pfnVertexFunc = pfnVertexFunc;
}

void SwrSetFrontendState(HANDLE hContext, SWR_FRONTEND_STATE* pFEState)
{
    API_STATE* pState     = GetDrawState(GetContext(hContext));
    pState->frontendState = *pFEState;
}

void SwrSetGsState(HANDLE hContext, SWR_GS_STATE* pGSState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));
    pState->gsState   = *pGSState;
}

void SwrSetGsFunc(HANDLE hContext, PFN_GS_FUNC pfnGsFunc)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));
    pState->pfnGsFunc = pfnGsFunc;
}

void SwrSetCsFunc(HANDLE      hContext,
                  PFN_CS_FUNC pfnCsFunc,
                  uint32_t    totalThreadsInGroup,
                  uint32_t    totalSpillFillSize,
                  uint32_t    scratchSpaceSizePerWarp,
                  uint32_t    numWarps)
{
    API_STATE* pState               = GetDrawState(GetContext(hContext));
    pState->pfnCsFunc               = pfnCsFunc;
    pState->totalThreadsInGroup     = totalThreadsInGroup;
    pState->totalSpillFillSize      = totalSpillFillSize;
    pState->scratchSpaceSizePerWarp = scratchSpaceSizePerWarp;
    pState->scratchSpaceNumWarps    = numWarps;
}

void SwrSetTsState(HANDLE hContext, SWR_TS_STATE* pState)
{
    API_STATE* pApiState = GetDrawState(GetContext(hContext));
    pApiState->tsState   = *pState;
}

void SwrSetHsFunc(HANDLE hContext, PFN_HS_FUNC pfnFunc)
{
    API_STATE* pApiState = GetDrawState(GetContext(hContext));
    pApiState->pfnHsFunc = pfnFunc;
}

void SwrSetDsFunc(HANDLE hContext, PFN_DS_FUNC pfnFunc)
{
    API_STATE* pApiState = GetDrawState(GetContext(hContext));
    pApiState->pfnDsFunc = pfnFunc;
}

void SwrSetDepthStencilState(HANDLE hContext, SWR_DEPTH_STENCIL_STATE* pDSState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->depthStencilState = *pDSState;
}

void SwrSetBackendState(HANDLE hContext, SWR_BACKEND_STATE* pBEState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->backendState = *pBEState;
}

void SwrSetDepthBoundsState(HANDLE hContext, SWR_DEPTH_BOUNDS_STATE* pDBState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));

    pState->depthBoundsState = *pDBState;
}

void SwrSetPixelShaderState(HANDLE hContext, SWR_PS_STATE* pPSState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));
    pState->psState   = *pPSState;
}

void SwrSetBlendState(HANDLE hContext, SWR_BLEND_STATE* pBlendState)
{
    API_STATE* pState = GetDrawState(GetContext(hContext));
    memcpy(&pState->blendState, pBlendState, sizeof(SWR_BLEND_STATE));
}

void SwrSetBlendFunc(HANDLE hContext, uint32_t renderTarget, PFN_BLEND_JIT_FUNC pfnBlendFunc)
{
    SWR_ASSERT(renderTarget < SWR_NUM_RENDERTARGETS);
    API_STATE* pState                  = GetDrawState(GetContext(hContext));
    pState->pfnBlendFunc[renderTarget] = pfnBlendFunc;
}

// update guardband multipliers for the viewport
void updateGuardbands(API_STATE* pState)
{
    uint32_t numGbs = pState->backendState.readViewportArrayIndex ? KNOB_NUM_VIEWPORTS_SCISSORS : 1;

    for (uint32_t i = 0; i < numGbs; ++i)
    {
        // guardband center is viewport center
        pState->gbState.left[i]   = KNOB_GUARDBAND_WIDTH / pState->vp[i].width;
        pState->gbState.right[i]  = KNOB_GUARDBAND_WIDTH / pState->vp[i].width;
        pState->gbState.top[i]    = KNOB_GUARDBAND_HEIGHT / pState->vp[i].height;
        pState->gbState.bottom[i] = KNOB_GUARDBAND_HEIGHT / pState->vp[i].height;
    }
}

void SwrSetRastState(HANDLE hContext, const SWR_RASTSTATE* pRastState)
{
    SWR_CONTEXT* pContext = GetContext(hContext);
    API_STATE*   pState   = GetDrawState(pContext);

    memcpy((void*)&pState->rastState, (void*)pRastState, sizeof(SWR_RASTSTATE));
}

void SwrSetViewports(HANDLE                       hContext,
                     uint32_t                     numViewports,
                     const SWR_VIEWPORT*          pViewports,
                     const SWR_VIEWPORT_MATRICES* pMatrices)
{
    SWR_ASSERT(numViewports <= KNOB_NUM_VIEWPORTS_SCISSORS, "Invalid number of viewports.");

    SWR_CONTEXT* pContext = GetContext(hContext);
    API_STATE*   pState   = GetDrawState(pContext);

    memcpy(&pState->vp[0], pViewports, sizeof(SWR_VIEWPORT) * numViewports);
    // @todo Faster to copy portions of the SOA or just copy all of it?
    memcpy(&pState->vpMatrices, pMatrices, sizeof(SWR_VIEWPORT_MATRICES));
}

void SwrSetScissorRects(HANDLE hContext, uint32_t numScissors, const SWR_RECT* pScissors)
{
    SWR_ASSERT(numScissors <= KNOB_NUM_VIEWPORTS_SCISSORS, "Invalid number of scissor rects.");

    API_STATE* pState = GetDrawState(GetContext(hContext));
    memcpy(&pState->scissorRects[0], pScissors, numScissors * sizeof(pScissors[0]));
};

void SetupMacroTileScissors(DRAW_CONTEXT* pDC)
{
    API_STATE* pState = &pDC->pState->state;
    uint32_t numScissors =
        pState->backendState.readViewportArrayIndex ? KNOB_NUM_VIEWPORTS_SCISSORS : 1;
    pState->scissorsTileAligned = true;

    for (uint32_t index = 0; index < numScissors; ++index)
    {
        SWR_RECT& scissorInFixedPoint = pState->scissorsInFixedPoint[index];

        // Set up scissor dimensions based on scissor or viewport
        if (pState->rastState.scissorEnable)
        {
            scissorInFixedPoint = pState->scissorRects[index];
        }
        else
        {
            // the vp width and height must be added to origin un-rounded then the result round to
            // -inf. The cast to int works for rounding assuming all [left, right, top, bottom] are
            // positive.
            scissorInFixedPoint.xmin = (int32_t)pState->vp[index].x;
            scissorInFixedPoint.xmax = (int32_t)(pState->vp[index].x + pState->vp[index].width);
            scissorInFixedPoint.ymin = (int32_t)pState->vp[index].y;
            scissorInFixedPoint.ymax = (int32_t)(pState->vp[index].y + pState->vp[index].height);
        }

        // Clamp to max rect
        scissorInFixedPoint &= g_MaxScissorRect;

        // Test for tile alignment
        bool tileAligned;
        tileAligned = (scissorInFixedPoint.xmin % KNOB_TILE_X_DIM) == 0;
        tileAligned &= (scissorInFixedPoint.ymin % KNOB_TILE_Y_DIM) == 0;
        tileAligned &= (scissorInFixedPoint.xmax % KNOB_TILE_X_DIM) == 0;
        tileAligned &= (scissorInFixedPoint.ymax % KNOB_TILE_Y_DIM) == 0;

        pState->scissorsTileAligned &= tileAligned;

        // Scale to fixed point
        scissorInFixedPoint.xmin *= FIXED_POINT_SCALE;
        scissorInFixedPoint.xmax *= FIXED_POINT_SCALE;
        scissorInFixedPoint.ymin *= FIXED_POINT_SCALE;
        scissorInFixedPoint.ymax *= FIXED_POINT_SCALE;

        // Make scissor inclusive
        scissorInFixedPoint.xmax -= 1;
        scissorInFixedPoint.ymax -= 1;
    }
}


// templated backend function tables

void SetupPipeline(DRAW_CONTEXT* pDC)
{
    DRAW_STATE*          pState       = pDC->pState;
    const SWR_RASTSTATE& rastState    = pState->state.rastState;
    const SWR_PS_STATE&  psState      = pState->state.psState;
    BACKEND_FUNCS&       backendFuncs = pState->backendFuncs;

    // setup backend
    if (psState.pfnPixelShader == nullptr)
    {
        backendFuncs.pfnBackend = gBackendNullPs[pState->state.rastState.sampleCount];
    }
    else
    {
        const uint32_t forcedSampleCount = (rastState.forcedSampleCount) ? 1 : 0;
        const bool     bMultisampleEnable =
            ((rastState.sampleCount > SWR_MULTISAMPLE_1X) || forcedSampleCount) ? 1 : 0;
        const uint32_t centroid =
            ((psState.barycentricsMask & SWR_BARYCENTRIC_CENTROID_MASK) > 0) ? 1 : 0;
        const uint32_t canEarlyZ =
            (psState.forceEarlyZ || (!psState.writesODepth && !psState.usesUAV)) ? 1 : 0;
        SWR_BARYCENTRICS_MASK barycentricsMask = (SWR_BARYCENTRICS_MASK)psState.barycentricsMask;

        // select backend function
        switch (psState.shadingRate)
        {
        case SWR_SHADING_RATE_PIXEL:
            if (bMultisampleEnable)
            {
                // always need to generate I & J per sample for Z interpolation
                barycentricsMask =
                    (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_SAMPLE_MASK);
                backendFuncs.pfnBackend =
                    gBackendPixelRateTable[rastState.sampleCount][rastState.bIsCenterPattern]
                                          [psState.inputCoverage][centroid][forcedSampleCount]
                                          [canEarlyZ]
                    ;
            }
            else
            {
                // always need to generate I & J per pixel for Z interpolation
                barycentricsMask =
                    (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_PIXEL_MASK);
                backendFuncs.pfnBackend =
                    gBackendSingleSample[psState.inputCoverage][centroid][canEarlyZ];
            }
            break;
        case SWR_SHADING_RATE_SAMPLE:
            SWR_ASSERT(rastState.bIsCenterPattern != true);
            // always need to generate I & J per sample for Z interpolation
            barycentricsMask =
                (SWR_BARYCENTRICS_MASK)(barycentricsMask | SWR_BARYCENTRIC_PER_SAMPLE_MASK);
            backendFuncs.pfnBackend =
                gBackendSampleRateTable[rastState.sampleCount][psState.inputCoverage][centroid]
                                       [canEarlyZ];
            break;
        default:
            SWR_ASSERT(0 && "Invalid shading rate");
            break;
        }
    }

    SWR_ASSERT(backendFuncs.pfnBackend);

    PFN_PROCESS_PRIMS pfnBinner;
#if USE_SIMD16_FRONTEND
    PFN_PROCESS_PRIMS_SIMD16 pfnBinner_simd16;
#endif
    switch (pState->state.topology)
    {
    case TOP_POINT_LIST:
        pState->pfnProcessPrims = ClipPoints;
        pfnBinner               = BinPoints;
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = ClipPoints_simd16;
        pfnBinner_simd16               = BinPoints_simd16;
#endif
        break;
    case TOP_LINE_LIST:
    case TOP_LINE_STRIP:
    case TOP_LINE_LOOP:
    case TOP_LINE_LIST_ADJ:
    case TOP_LISTSTRIP_ADJ:
        pState->pfnProcessPrims = ClipLines;
        pfnBinner               = BinLines;
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = ClipLines_simd16;
        pfnBinner_simd16               = BinLines_simd16;
#endif
        break;
    default:
        pState->pfnProcessPrims = ClipTriangles;
        pfnBinner               = GetBinTrianglesFunc((rastState.conservativeRast > 0));
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = ClipTriangles_simd16;
        pfnBinner_simd16 = GetBinTrianglesFunc_simd16((rastState.conservativeRast > 0));
#endif
        break;
    };


    // Disable clipper if viewport transform is disabled or if clipper is disabled
    if (pState->state.frontendState.vpTransformDisable || !pState->state.rastState.clipEnable)
    {
        pState->pfnProcessPrims = pfnBinner;
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = pfnBinner_simd16;
#endif
    }

    // Disable rasterizer and backend if no pixel, no depth/stencil, and no attributes
    if ((pState->state.psState.pfnPixelShader == nullptr) &&
        (pState->state.depthStencilState.depthTestEnable == FALSE) &&
        (pState->state.depthStencilState.depthWriteEnable == FALSE) &&
        (pState->state.depthStencilState.stencilTestEnable == FALSE) &&
        (pState->state.depthStencilState.stencilWriteEnable == FALSE) &&
        (pState->state.backendState.numAttributes == 0))
    {
        pState->pfnProcessPrims = nullptr;
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = nullptr;
#endif
    }

    if (pState->state.soState.rasterizerDisable == true)
    {
        pState->pfnProcessPrims = nullptr;
#if USE_SIMD16_FRONTEND
        pState->pfnProcessPrims_simd16 = nullptr;
#endif
    }


    // set up the frontend attribute count
    pState->state.feNumAttributes         = 0;
    const SWR_BACKEND_STATE& backendState = pState->state.backendState;
    if (backendState.swizzleEnable)
    {
        // attribute swizzling is enabled, iterate over the map and record the max attribute used
        for (uint32_t i = 0; i < backendState.numAttributes; ++i)
        {
            pState->state.feNumAttributes =
                std::max(pState->state.feNumAttributes,
                         (uint32_t)backendState.swizzleMap[i].sourceAttrib + 1);
        }
    }
    else
    {
        pState->state.feNumAttributes = pState->state.backendState.numAttributes;
    }

    if (pState->state.soState.soEnable)
    {
        uint64_t streamMasks = 0;
        for (uint32_t i = 0; i < 4; ++i)
        {
            streamMasks |= pState->state.soState.streamMasks[i];
        }

        DWORD maxAttrib;
        if (_BitScanReverse64(&maxAttrib, streamMasks))
        {
            pState->state.feNumAttributes =
                std::max(pState->state.feNumAttributes, (uint32_t)(maxAttrib + 1));
        }
    }

    // complicated logic to test for cases where we don't need backing hottile memory for a draw
    // have to check for the special case where depth/stencil test is enabled but depthwrite is
    // disabled.
    pState->state.depthHottileEnable =
        ((!(pState->state.depthStencilState.depthTestEnable &&
            !pState->state.depthStencilState.depthWriteEnable &&
            !pState->state.depthBoundsState.depthBoundsTestEnable &&
            pState->state.depthStencilState.depthTestFunc == ZFUNC_ALWAYS)) &&
         (pState->state.depthStencilState.depthTestEnable ||
          pState->state.depthStencilState.depthWriteEnable ||
          pState->state.depthBoundsState.depthBoundsTestEnable))
            ? true
            : false;

    pState->state.stencilHottileEnable =
        (((!(pState->state.depthStencilState.stencilTestEnable &&
             !pState->state.depthStencilState.stencilWriteEnable &&
             pState->state.depthStencilState.stencilTestFunc == ZFUNC_ALWAYS)) ||
          // for stencil we have to check the double sided state as well
          (!(pState->state.depthStencilState.doubleSidedStencilTestEnable &&
             !pState->state.depthStencilState.stencilWriteEnable &&
             pState->state.depthStencilState.backfaceStencilTestFunc == ZFUNC_ALWAYS))) &&
         (pState->state.depthStencilState.stencilTestEnable ||
          pState->state.depthStencilState.stencilWriteEnable))
            ? true
            : false;

    uint32_t hotTileEnable = pState->state.psState.renderTargetMask;

    // Disable hottile for surfaces with no writes
    if (psState.pfnPixelShader != nullptr)
    {
        DWORD    rt;
        uint32_t rtMask = pState->state.psState.renderTargetMask;
        while (_BitScanForward(&rt, rtMask))
        {
            rtMask &= ~(1 << rt);

            if (pState->state.blendState.renderTarget[rt].writeDisableAlpha &&
                pState->state.blendState.renderTarget[rt].writeDisableRed &&
                pState->state.blendState.renderTarget[rt].writeDisableGreen &&
                pState->state.blendState.renderTarget[rt].writeDisableBlue)
            {
                hotTileEnable &= ~(1 << rt);
            }
        }
    }

    pState->state.colorHottileEnable = hotTileEnable;

    // Setup depth quantization function
    if (pState->state.depthHottileEnable)
    {
        switch (pState->state.rastState.depthFormat)
        {
        case R32_FLOAT_X8X24_TYPELESS:
            pState->state.pfnQuantizeDepth = QuantizeDepth<R32_FLOAT_X8X24_TYPELESS>;
            break;
        case R32_FLOAT:
            pState->state.pfnQuantizeDepth = QuantizeDepth<R32_FLOAT>;
            break;
        case R24_UNORM_X8_TYPELESS:
            pState->state.pfnQuantizeDepth = QuantizeDepth<R24_UNORM_X8_TYPELESS>;
            break;
        case R16_UNORM:
            pState->state.pfnQuantizeDepth = QuantizeDepth<R16_UNORM>;
            break;
        default:
            SWR_INVALID("Unsupported depth format for depth quantiztion.");
            pState->state.pfnQuantizeDepth = QuantizeDepth<R32_FLOAT>;
        }
    }
    else
    {
        // set up pass-through quantize if depth isn't enabled
        pState->state.pfnQuantizeDepth = QuantizeDepth<R32_FLOAT>;
    }

    // Generate guardbands
    updateGuardbands(&pState->state);
}

//////////////////////////////////////////////////////////////////////////
/// @brief InitDraw
/// @param pDC - Draw context to initialize for this draw.
void InitDraw(DRAW_CONTEXT* pDC, bool isSplitDraw)
{
    // We don't need to re-setup the scissors/pipeline state again for split draw.
    if (isSplitDraw == false)
    {
        SetupMacroTileScissors(pDC);
        SetupPipeline(pDC);
    }

}

//////////////////////////////////////////////////////////////////////////
/// @brief We can split the draw for certain topologies for better performance.
/// @param totalVerts - Total vertices for draw
/// @param topology - Topology used for draw
uint32_t MaxVertsPerDraw(DRAW_CONTEXT* pDC, uint32_t totalVerts, PRIMITIVE_TOPOLOGY topology)
{
    API_STATE& state = pDC->pState->state;

    // We can not split draws that have streamout enabled because there is no practical way
    // to support multiple threads generating SO data for a single set of buffers.
    if (state.soState.soEnable)
    {
        return totalVerts;
    }

    // The Primitive Assembly code can only handle 1 RECT at a time. Specified with only 3 verts.
    if (topology == TOP_RECT_LIST)
    {
        return 3;
    }

    // Is split drawing disabled?
    if (KNOB_DISABLE_SPLIT_DRAW)
    {
        return totalVerts;
    }

    uint32_t vertsPerDraw = totalVerts;

    switch (topology)
    {
    case TOP_POINT_LIST:
    case TOP_TRIANGLE_LIST:
        vertsPerDraw = KNOB_MAX_PRIMS_PER_DRAW;
        break;

    case TOP_PATCHLIST_1:
    case TOP_PATCHLIST_2:
    case TOP_PATCHLIST_3:
    case TOP_PATCHLIST_4:
    case TOP_PATCHLIST_5:
    case TOP_PATCHLIST_6:
    case TOP_PATCHLIST_7:
    case TOP_PATCHLIST_8:
    case TOP_PATCHLIST_9:
    case TOP_PATCHLIST_10:
    case TOP_PATCHLIST_11:
    case TOP_PATCHLIST_12:
    case TOP_PATCHLIST_13:
    case TOP_PATCHLIST_14:
    case TOP_PATCHLIST_15:
    case TOP_PATCHLIST_16:
    case TOP_PATCHLIST_17:
    case TOP_PATCHLIST_18:
    case TOP_PATCHLIST_19:
    case TOP_PATCHLIST_20:
    case TOP_PATCHLIST_21:
    case TOP_PATCHLIST_22:
    case TOP_PATCHLIST_23:
    case TOP_PATCHLIST_24:
    case TOP_PATCHLIST_25:
    case TOP_PATCHLIST_26:
    case TOP_PATCHLIST_27:
    case TOP_PATCHLIST_28:
    case TOP_PATCHLIST_29:
    case TOP_PATCHLIST_30:
    case TOP_PATCHLIST_31:
    case TOP_PATCHLIST_32:
        if (pDC->pState->state.tsState.tsEnable)
        {
            uint32_t vertsPerPrim = topology - TOP_PATCHLIST_BASE;
            vertsPerDraw          = vertsPerPrim * KNOB_MAX_TESS_PRIMS_PER_DRAW;
        }
        break;
    default:
        // We are not splitting up draws for other topologies.
        break;
    }

    return vertsPerDraw;
}

//////////////////////////////////////////////////////////////////////////
/// @brief DrawInstanced
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param numVerts - How many vertices to read sequentially from vertex data (per instance).
/// @param startVertex - Specifies start vertex for draw. (vertex data)
/// @param numInstances - How many instances to render.
/// @param startInstance - Which instance to start sequentially fetching from in each buffer
/// (instanced data)
void DrawInstanced(HANDLE             hContext,
                   PRIMITIVE_TOPOLOGY topology,
                   uint32_t           numVertices,
                   uint32_t           startVertex,
                   uint32_t           numInstances  = 1,
                   uint32_t           startInstance = 0)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIDraw, pDC->drawId);

    uint32_t maxVertsPerDraw = MaxVertsPerDraw(pDC, numVertices, topology);
    uint32_t primsPerDraw    = GetNumPrims(topology, maxVertsPerDraw);
    uint32_t remainingVerts  = numVertices;

    API_STATE* pState  = &pDC->pState->state;
    pState->topology   = topology;
    pState->forceFront = false;

    // disable culling for points/lines
    uint32_t oldCullMode = pState->rastState.cullMode;
    if (topology == TOP_POINT_LIST)
    {
        pState->rastState.cullMode = SWR_CULLMODE_NONE;
        pState->forceFront         = true;
    }
    else if (topology == TOP_RECT_LIST)
    {
        pState->rastState.cullMode = SWR_CULLMODE_NONE;
    }

    int draw = 0;
    while (remainingVerts)
    {
        uint32_t numVertsForDraw =
            (remainingVerts < maxVertsPerDraw) ? remainingVerts : maxVertsPerDraw;

        bool          isSplitDraw = (draw > 0) ? !KNOB_DISABLE_SPLIT_DRAW : false;
        DRAW_CONTEXT* pDC         = GetDrawContext(pContext, isSplitDraw);
        InitDraw(pDC, isSplitDraw);

        pDC->FeWork.type                    = DRAW;
        pDC->FeWork.pfnWork                 = GetProcessDrawFunc(false, // IsIndexed
                                                 false, // bEnableCutIndex
                                                 pState->tsState.tsEnable,
                                                 pState->gsState.gsEnable,
                                                 pState->soState.soEnable,
                                                 pDC->pState->pfnProcessPrims != nullptr);
        pDC->FeWork.desc.draw.numVerts      = numVertsForDraw;
        pDC->FeWork.desc.draw.startVertex   = startVertex;
        pDC->FeWork.desc.draw.numInstances  = numInstances;
        pDC->FeWork.desc.draw.startInstance = startInstance;
        pDC->FeWork.desc.draw.startPrimID   = draw * primsPerDraw;
        pDC->FeWork.desc.draw.startVertexID = draw * maxVertsPerDraw;

        pDC->cleanupState = (remainingVerts == numVertsForDraw);

        // enqueue DC
        QueueDraw(pContext);

        AR_API_EVENT(DrawInstancedEvent(pDC->drawId,
                                        topology,
                                        numVertsForDraw,
                                        startVertex,
                                        numInstances,
                                        startInstance,
                                        pState->tsState.tsEnable,
                                        pState->gsState.gsEnable,
                                        pState->soState.soEnable,
                                        pState->gsState.outputTopology,
                                        draw));

        remainingVerts -= numVertsForDraw;
        draw++;
    }

    // restore culling state
    pDC                                   = GetDrawContext(pContext);
    pDC->pState->state.rastState.cullMode = oldCullMode;

    RDTSC_END(pContext->pBucketMgr, APIDraw, numVertices * numInstances);
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrDraw
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param startVertex - Specifies start vertex in vertex buffer for draw.
/// @param primCount - Number of vertices.
void SwrDraw(HANDLE             hContext,
             PRIMITIVE_TOPOLOGY topology,
             uint32_t           startVertex,
             uint32_t           numVertices)
{
    DrawInstanced(hContext, topology, numVertices, startVertex);
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrDrawInstanced
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param numVertsPerInstance - How many vertices to read sequentially from vertex data.
/// @param numInstances - How many instances to render.
/// @param startVertex - Specifies start vertex for draw. (vertex data)
/// @param startInstance - Which instance to start sequentially fetching from in each buffer
/// (instanced data)
void SwrDrawInstanced(HANDLE             hContext,
                      PRIMITIVE_TOPOLOGY topology,
                      uint32_t           numVertsPerInstance,
                      uint32_t           numInstances,
                      uint32_t           startVertex,
                      uint32_t           startInstance)
{
    DrawInstanced(
        hContext, topology, numVertsPerInstance, startVertex, numInstances, startInstance);
}

//////////////////////////////////////////////////////////////////////////
/// @brief DrawIndexedInstanced
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param numIndices - Number of indices to read sequentially from index buffer.
/// @param indexOffset - Starting index into index buffer.
/// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
/// @param numInstances - Number of instances to render.
/// @param startInstance - Which instance to start sequentially fetching from in each buffer
/// (instanced data)
void DrawIndexedInstance(HANDLE             hContext,
                         PRIMITIVE_TOPOLOGY topology,
                         uint32_t           numIndices,
                         uint32_t           indexOffset,
                         int32_t            baseVertex,
                         uint32_t           numInstances  = 1,
                         uint32_t           startInstance = 0)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);
    API_STATE*    pState   = &pDC->pState->state;

    RDTSC_BEGIN(pContext->pBucketMgr, APIDrawIndexed, pDC->drawId);

    uint32_t maxIndicesPerDraw = MaxVertsPerDraw(pDC, numIndices, topology);
    uint32_t primsPerDraw      = GetNumPrims(topology, maxIndicesPerDraw);
    uint32_t remainingIndices  = numIndices;

    uint32_t indexSize = 0;
    switch (pState->indexBuffer.format)
    {
    case R32_UINT:
        indexSize = sizeof(uint32_t);
        break;
    case R16_UINT:
        indexSize = sizeof(uint16_t);
        break;
    case R8_UINT:
        indexSize = sizeof(uint8_t);
        break;
    default:
        SWR_INVALID("Invalid index buffer format: %d", pState->indexBuffer.format);
    }

    int      draw = 0;
    gfxptr_t xpIB = pState->indexBuffer.xpIndices;
    xpIB += (uint64_t)indexOffset * (uint64_t)indexSize;

    pState->topology   = topology;
    pState->forceFront = false;

    // disable culling for points/lines
    uint32_t oldCullMode = pState->rastState.cullMode;
    if (topology == TOP_POINT_LIST)
    {
        pState->rastState.cullMode = SWR_CULLMODE_NONE;
        pState->forceFront         = true;
    }
    else if (topology == TOP_RECT_LIST)
    {
        pState->rastState.cullMode = SWR_CULLMODE_NONE;
    }

    while (remainingIndices)
    {
        uint32_t numIndicesForDraw =
            (remainingIndices < maxIndicesPerDraw) ? remainingIndices : maxIndicesPerDraw;

        // When breaking up draw, we need to obtain new draw context for each iteration.
        bool isSplitDraw = (draw > 0) ? !KNOB_DISABLE_SPLIT_DRAW : false;

        pDC = GetDrawContext(pContext, isSplitDraw);
        InitDraw(pDC, isSplitDraw);

        pDC->FeWork.type                 = DRAW;
        pDC->FeWork.pfnWork              = GetProcessDrawFunc(true, // IsIndexed
                                                 pState->frontendState.bEnableCutIndex,
                                                 pState->tsState.tsEnable,
                                                 pState->gsState.gsEnable,
                                                 pState->soState.soEnable,
                                                 pDC->pState->pfnProcessPrims != nullptr);
        pDC->FeWork.desc.draw.pDC        = pDC;
        pDC->FeWork.desc.draw.numIndices = numIndicesForDraw;
        pDC->FeWork.desc.draw.xpIB       = xpIB;
        pDC->FeWork.desc.draw.type       = pDC->pState->state.indexBuffer.format;

        pDC->FeWork.desc.draw.numInstances  = numInstances;
        pDC->FeWork.desc.draw.startInstance = startInstance;
        pDC->FeWork.desc.draw.baseVertex    = baseVertex;
        pDC->FeWork.desc.draw.startPrimID   = draw * primsPerDraw;

        pDC->cleanupState = (remainingIndices == numIndicesForDraw);

        // enqueue DC
        QueueDraw(pContext);

        AR_API_EVENT(DrawIndexedInstancedEvent(pDC->drawId,
                                               topology,
                                               numIndicesForDraw,
                                               indexOffset,
                                               baseVertex,
                                               numInstances,
                                               startInstance,
                                               pState->tsState.tsEnable,
                                               pState->gsState.gsEnable,
                                               pState->soState.soEnable,
                                               pState->gsState.outputTopology,
                                               draw));

        xpIB += maxIndicesPerDraw * indexSize;
        remainingIndices -= numIndicesForDraw;
        draw++;
    }

    // Restore culling state
    pDC                                   = GetDrawContext(pContext);
    pDC->pState->state.rastState.cullMode = oldCullMode;

    RDTSC_END(pContext->pBucketMgr, APIDrawIndexed, numIndices * numInstances);
}

//////////////////////////////////////////////////////////////////////////
/// @brief DrawIndexed
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param numIndices - Number of indices to read sequentially from index buffer.
/// @param indexOffset - Starting index into index buffer.
/// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
void SwrDrawIndexed(HANDLE             hContext,
                    PRIMITIVE_TOPOLOGY topology,
                    uint32_t           numIndices,
                    uint32_t           indexOffset,
                    int32_t            baseVertex)
{
    DrawIndexedInstance(hContext, topology, numIndices, indexOffset, baseVertex);
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrDrawIndexedInstanced
/// @param hContext - Handle passed back from SwrCreateContext
/// @param topology - Specifies topology for draw.
/// @param numIndices - Number of indices to read sequentially from index buffer.
/// @param numInstances - Number of instances to render.
/// @param indexOffset - Starting index into index buffer.
/// @param baseVertex - Vertex in vertex buffer to consider as index "0". Note value is signed.
/// @param startInstance - Which instance to start sequentially fetching from in each buffer
/// (instanced data)
void SwrDrawIndexedInstanced(HANDLE             hContext,
                             PRIMITIVE_TOPOLOGY topology,
                             uint32_t           numIndices,
                             uint32_t           numInstances,
                             uint32_t           indexOffset,
                             int32_t            baseVertex,
                             uint32_t           startInstance)
{
    DrawIndexedInstance(
        hContext, topology, numIndices, indexOffset, baseVertex, numInstances, startInstance);
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrInvalidateTiles
/// @param hContext - Handle passed back from SwrCreateContext
/// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to
/// invalidate.
/// @param invalidateRect - The pixel-coordinate rectangle to invalidate.  This will be expanded to
///                         be hottile size-aligned.
void SWR_API SwrInvalidateTiles(HANDLE          hContext,
                                uint32_t        attachmentMask,
                                const SWR_RECT& invalidateRect)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    pDC->FeWork.type                                       = DISCARDINVALIDATETILES;
    pDC->FeWork.pfnWork                                    = ProcessDiscardInvalidateTiles;
    pDC->FeWork.desc.discardInvalidateTiles.attachmentMask = attachmentMask;
    pDC->FeWork.desc.discardInvalidateTiles.rect           = invalidateRect;
    pDC->FeWork.desc.discardInvalidateTiles.rect &= g_MaxScissorRect;
    pDC->FeWork.desc.discardInvalidateTiles.newTileState   = SWR_TILE_INVALID;
    pDC->FeWork.desc.discardInvalidateTiles.createNewTiles = false;
    pDC->FeWork.desc.discardInvalidateTiles.fullTilesOnly  = false;

    // enqueue
    QueueDraw(pContext);

    AR_API_EVENT(SwrInvalidateTilesEvent(pDC->drawId));
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrDiscardRect
/// @param hContext - Handle passed back from SwrCreateContext
/// @param attachmentMask - The mask specifies which surfaces attached to the hottiles to discard.
/// @param rect - The pixel-coordinate rectangle to discard.  Only fully-covered hottiles will be
///               discarded.
void SWR_API SwrDiscardRect(HANDLE hContext, uint32_t attachmentMask, const SWR_RECT& rect)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    // Queue a load to the hottile
    pDC->FeWork.type                                       = DISCARDINVALIDATETILES;
    pDC->FeWork.pfnWork                                    = ProcessDiscardInvalidateTiles;
    pDC->FeWork.desc.discardInvalidateTiles.attachmentMask = attachmentMask;
    pDC->FeWork.desc.discardInvalidateTiles.rect           = rect;
    pDC->FeWork.desc.discardInvalidateTiles.rect &= g_MaxScissorRect;
    pDC->FeWork.desc.discardInvalidateTiles.newTileState   = SWR_TILE_RESOLVED;
    pDC->FeWork.desc.discardInvalidateTiles.createNewTiles = true;
    pDC->FeWork.desc.discardInvalidateTiles.fullTilesOnly  = true;

    // enqueue
    QueueDraw(pContext);

    AR_API_EVENT(SwrDiscardRectEvent(pDC->drawId));
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrDispatch
/// @param hContext - Handle passed back from SwrCreateContext
/// @param threadGroupCountX - Number of thread groups dispatched in X direction
/// @param threadGroupCountY - Number of thread groups dispatched in Y direction
/// @param threadGroupCountZ - Number of thread groups dispatched in Z direction
void SwrDispatch(HANDLE   hContext,
                 uint32_t threadGroupCountX,
                 uint32_t threadGroupCountY,
                 uint32_t threadGroupCountZ

)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIDispatch, pDC->drawId);
    AR_API_EVENT(
        DispatchEvent(pDC->drawId, threadGroupCountX, threadGroupCountY, threadGroupCountZ));
    pDC->isCompute = true; // This is a compute context.

    COMPUTE_DESC* pTaskData = (COMPUTE_DESC*)pDC->pArena->AllocAligned(sizeof(COMPUTE_DESC), 64);

    pTaskData->threadGroupCountX = threadGroupCountX;
    pTaskData->threadGroupCountY = threadGroupCountY;
    pTaskData->threadGroupCountZ = threadGroupCountZ;

    pTaskData->enableThreadDispatch = false;

    uint32_t totalThreadGroups = threadGroupCountX * threadGroupCountY * threadGroupCountZ;
    uint32_t dcIndex           = pDC->drawId % pContext->MAX_DRAWS_IN_FLIGHT;
    pDC->pDispatch             = &pContext->pDispatchQueueArray[dcIndex];
    pDC->pDispatch->initialize(totalThreadGroups, pTaskData, &ProcessComputeBE);

    QueueDispatch(pContext);
    RDTSC_END(pContext->pBucketMgr,
              APIDispatch,
              threadGroupCountX * threadGroupCountY * threadGroupCountZ);
}

// Deswizzles, converts and stores current contents of the hot tiles to surface
// described by pState
void SWR_API SwrStoreTiles(HANDLE          hContext,
                           uint32_t        attachmentMask,
                           SWR_TILE_STATE  postStoreTileState,
                           const SWR_RECT& storeRect)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIStoreTiles, pDC->drawId);

    pDC->FeWork.type                               = STORETILES;
    pDC->FeWork.pfnWork                            = ProcessStoreTiles;
    pDC->FeWork.desc.storeTiles.attachmentMask     = attachmentMask;
    pDC->FeWork.desc.storeTiles.postStoreTileState = postStoreTileState;
    pDC->FeWork.desc.storeTiles.rect               = storeRect;
    pDC->FeWork.desc.storeTiles.rect &= g_MaxScissorRect;

    // enqueue
    QueueDraw(pContext);

    AR_API_EVENT(SwrStoreTilesEvent(pDC->drawId));

    RDTSC_END(pContext->pBucketMgr, APIStoreTiles, 1);
}

//////////////////////////////////////////////////////////////////////////
/// @brief SwrClearRenderTarget - Clear attached render targets / depth / stencil
/// @param hContext - Handle passed back from SwrCreateContext
/// @param attachmentMask - combination of SWR_ATTACHMENT_*_BIT attachments to clear
/// @param renderTargetArrayIndex - the RT array index to clear
/// @param clearColor - color use for clearing render targets
/// @param z - depth value use for clearing depth buffer
/// @param stencil - stencil value used for clearing stencil buffer
/// @param clearRect - The pixel-coordinate rectangle to clear in all cleared buffers
void SWR_API SwrClearRenderTarget(HANDLE          hContext,
                                  uint32_t        attachmentMask,
                                  uint32_t        renderTargetArrayIndex,
                                  const float     clearColor[4],
                                  float           z,
                                  uint8_t         stencil,
                                  const SWR_RECT& clearRect)
{
    if (KNOB_TOSS_DRAW)
    {
        return;
    }

    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    RDTSC_BEGIN(pContext->pBucketMgr, APIClearRenderTarget, pDC->drawId);

    pDC->FeWork.type            = CLEAR;
    pDC->FeWork.pfnWork         = ProcessClear;
    pDC->FeWork.desc.clear.rect = clearRect;
    pDC->FeWork.desc.clear.rect &= g_MaxScissorRect;
    pDC->FeWork.desc.clear.attachmentMask         = attachmentMask;
    pDC->FeWork.desc.clear.renderTargetArrayIndex = renderTargetArrayIndex;
    pDC->FeWork.desc.clear.clearDepth             = z;
    pDC->FeWork.desc.clear.clearRTColor[0]        = clearColor[0];
    pDC->FeWork.desc.clear.clearRTColor[1]        = clearColor[1];
    pDC->FeWork.desc.clear.clearRTColor[2]        = clearColor[2];
    pDC->FeWork.desc.clear.clearRTColor[3]        = clearColor[3];
    pDC->FeWork.desc.clear.clearStencil           = stencil;

    // enqueue draw
    QueueDraw(pContext);

    RDTSC_END(pContext->pBucketMgr, APIClearRenderTarget, 1);
}

//////////////////////////////////////////////////////////////////////////
/// @brief Returns a pointer to the private context state for the current
///        draw operation. This is used for external componets such as the
///        sampler.
///        SWR is responsible for the allocation of the private context state.
/// @param hContext - Handle passed back from SwrCreateContext
VOID* SwrGetPrivateContextState(HANDLE hContext)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);
    DRAW_STATE*   pState   = pDC->pState;

    if (pState->pPrivateState == nullptr)
    {
        pState->pPrivateState = pState->pArena->AllocAligned(pContext->privateStateSize,
                                                             KNOB_SIMD_WIDTH * sizeof(float));
    }

    return pState->pPrivateState;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Clients can use this to allocate memory for draw/dispatch
///        operations. The memory will automatically be freed once operation
///        has completed. Client can use this to allocate binding tables,
///        etc. needed for shader execution.
/// @param hContext - Handle passed back from SwrCreateContext
/// @param size - Size of allocation
/// @param align - Alignment needed for allocation.
VOID* SwrAllocDrawContextMemory(HANDLE hContext, uint32_t size, uint32_t align)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    return pDC->pState->pArena->AllocAligned(size, align);
}

//////////////////////////////////////////////////////////////////////////
/// @brief Enables stats counting
/// @param hContext - Handle passed back from SwrCreateContext
/// @param enable - If true then counts are incremented.
void SwrEnableStatsFE(HANDLE hContext, bool enable)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    pDC->pState->state.enableStatsFE = enable;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Enables stats counting
/// @param hContext - Handle passed back from SwrCreateContext
/// @param enable - If true then counts are incremented.
void SwrEnableStatsBE(HANDLE hContext, bool enable)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);

    pDC->pState->state.enableStatsBE = enable;
}

//////////////////////////////////////////////////////////////////////////
/// @brief Mark end of frame - used for performance profiling
/// @param hContext - Handle passed back from SwrCreateContext
void SWR_API SwrEndFrame(HANDLE hContext)
{
    SWR_CONTEXT*  pContext = GetContext(hContext);
    DRAW_CONTEXT* pDC      = GetDrawContext(pContext);
    (void)pDC; // var used

    RDTSC_ENDFRAME(pContext->pBucketMgr);
    AR_API_EVENT(FrameEndEvent(pContext->frameCount, pDC->drawId));

    pContext->frameCount++;
}

void InitSimLoadTilesTable();
void InitSimStoreTilesTable();
void InitSimClearTilesTable();

void InitClearTilesTable();
void InitBackendFuncTables();

//////////////////////////////////////////////////////////////////////////
/// @brief Initialize swr backend and memory internal tables
void SwrInit()
{
    InitClearTilesTable();
    InitBackendFuncTables();
    InitRasterizerFunctions();
}

void SwrGetInterface(SWR_INTERFACE& out_funcs)
{
    out_funcs.pfnSwrCreateContext          = SwrCreateContext;
    out_funcs.pfnSwrDestroyContext         = SwrDestroyContext;
    out_funcs.pfnSwrBindApiThread          = SwrBindApiThread;
    out_funcs.pfnSwrSaveState              = SwrSaveState;
    out_funcs.pfnSwrRestoreState           = SwrRestoreState;
    out_funcs.pfnSwrSync                   = SwrSync;
    out_funcs.pfnSwrStallBE                = SwrStallBE;
    out_funcs.pfnSwrWaitForIdle            = SwrWaitForIdle;
    out_funcs.pfnSwrWaitForIdleFE          = SwrWaitForIdleFE;
    out_funcs.pfnSwrSetVertexBuffers       = SwrSetVertexBuffers;
    out_funcs.pfnSwrSetIndexBuffer         = SwrSetIndexBuffer;
    out_funcs.pfnSwrSetFetchFunc           = SwrSetFetchFunc;
    out_funcs.pfnSwrSetSoFunc              = SwrSetSoFunc;
    out_funcs.pfnSwrSetSoState             = SwrSetSoState;
    out_funcs.pfnSwrSetSoBuffers           = SwrSetSoBuffers;
    out_funcs.pfnSwrSetVertexFunc          = SwrSetVertexFunc;
    out_funcs.pfnSwrSetFrontendState       = SwrSetFrontendState;
    out_funcs.pfnSwrSetGsState             = SwrSetGsState;
    out_funcs.pfnSwrSetGsFunc              = SwrSetGsFunc;
    out_funcs.pfnSwrSetCsFunc              = SwrSetCsFunc;
    out_funcs.pfnSwrSetTsState             = SwrSetTsState;
    out_funcs.pfnSwrSetHsFunc              = SwrSetHsFunc;
    out_funcs.pfnSwrSetDsFunc              = SwrSetDsFunc;
    out_funcs.pfnSwrSetDepthStencilState   = SwrSetDepthStencilState;
    out_funcs.pfnSwrSetBackendState        = SwrSetBackendState;
    out_funcs.pfnSwrSetDepthBoundsState    = SwrSetDepthBoundsState;
    out_funcs.pfnSwrSetPixelShaderState    = SwrSetPixelShaderState;
    out_funcs.pfnSwrSetBlendState          = SwrSetBlendState;
    out_funcs.pfnSwrSetBlendFunc           = SwrSetBlendFunc;
    out_funcs.pfnSwrDraw                   = SwrDraw;
    out_funcs.pfnSwrDrawInstanced          = SwrDrawInstanced;
    out_funcs.pfnSwrDrawIndexed            = SwrDrawIndexed;
    out_funcs.pfnSwrDrawIndexedInstanced   = SwrDrawIndexedInstanced;
    out_funcs.pfnSwrInvalidateTiles        = SwrInvalidateTiles;
    out_funcs.pfnSwrDiscardRect            = SwrDiscardRect;
    out_funcs.pfnSwrDispatch               = SwrDispatch;
    out_funcs.pfnSwrStoreTiles             = SwrStoreTiles;
    out_funcs.pfnSwrClearRenderTarget      = SwrClearRenderTarget;
    out_funcs.pfnSwrSetRastState           = SwrSetRastState;
    out_funcs.pfnSwrSetViewports           = SwrSetViewports;
    out_funcs.pfnSwrSetScissorRects        = SwrSetScissorRects;
    out_funcs.pfnSwrGetPrivateContextState = SwrGetPrivateContextState;
    out_funcs.pfnSwrAllocDrawContextMemory = SwrAllocDrawContextMemory;
    out_funcs.pfnSwrEnableStatsFE          = SwrEnableStatsFE;
    out_funcs.pfnSwrEnableStatsBE          = SwrEnableStatsBE;
    out_funcs.pfnSwrEndFrame               = SwrEndFrame;
    out_funcs.pfnSwrInit                   = SwrInit;
}