aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/swr/rasterizer/archrast/archrast.cpp
blob: ba99391ae76a8b7faefeab1d45dde06fe8a718ac (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
/****************************************************************************
 * Copyright (C) 2016 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 archrast.cpp
 *
 * @brief Implementation for archrast.
 *
 ******************************************************************************/
#include <atomic>
#include <map>

#include "common/os.h"
#include "archrast/archrast.h"
#include "archrast/eventmanager.h"
#include "gen_ar_eventhandlerfile.hpp"

namespace ArchRast
{
    //////////////////////////////////////////////////////////////////////////
    /// @brief struct that keeps track of depth and stencil event information
    struct DepthStencilStats
    {
        uint32_t earlyZTestPassCount       = 0;
        uint32_t earlyZTestFailCount       = 0;
        uint32_t lateZTestPassCount        = 0;
        uint32_t lateZTestFailCount        = 0;
        uint32_t earlyStencilTestPassCount = 0;
        uint32_t earlyStencilTestFailCount = 0;
        uint32_t lateStencilTestPassCount  = 0;
        uint32_t lateStencilTestFailCount  = 0;
    };

    struct CStats
    {
        uint32_t trivialRejectCount;
        uint32_t trivialAcceptCount;
        uint32_t mustClipCount;
    };

    struct TEStats
    {
        uint32_t inputPrims = 0;
        //@todo:: Change this to numPatches. Assumed: 1 patch per prim. If holds, its fine.
    };

    struct GSStateInfo
    {
        uint32_t inputPrimCount;
        uint32_t primGeneratedCount;
        uint32_t vertsInput;
    };

    struct RastStats
    {
        uint32_t rasterTiles = 0;
    };

    struct CullStats
    {
        uint32_t degeneratePrimCount = 0;
        uint32_t backfacePrimCount   = 0;
    };

    struct AlphaStats
    {
        uint32_t alphaTestCount  = 0;
        uint32_t alphaBlendCount = 0;
    };

    struct MemoryStats
    {
        struct MemoryTrackerKey
        {
            uint64_t address;
            uint64_t mask;
        };

        struct MemoryTrackerData
        {
            uint32_t accessCountRead;
            uint32_t accessCountWrite;
            uint32_t totalSizeRead;
            uint32_t totalSizeWrite;
            uint64_t tscMin;
            uint64_t tscMax;
        };

        struct AddressRangeComparator 
        {
            bool operator()(MemoryTrackerKey a, MemoryTrackerKey b) const 
            {
                return (a.address & a.mask) < (b.address & b.mask);
            }
        };

        typedef std::map<MemoryTrackerKey, MemoryTrackerData, AddressRangeComparator> MemoryTrackerMap;
        MemoryTrackerMap trackedMemory = {};

        void TrackMemoryAccess(uint64_t address, uint64_t addressMask, uint8_t isRead, uint64_t tsc, uint32_t size)
        {
            MemoryTrackerKey key;
            key.address = address;
            key.mask = addressMask;

            MemoryTrackerMap::iterator i = trackedMemory.lower_bound(key);
            if (i != trackedMemory.end() && !(trackedMemory.key_comp()(key, i->first)))
            {
                // already in map
                if (isRead)
                {
                    i->second.accessCountRead++;
                    i->second.totalSizeRead += size;
                }
                else
                {
                    i->second.accessCountWrite++;
                    i->second.totalSizeWrite += size;
                }
                i->second.tscMax = tsc;
            }
            else
            {
                // new entry
                MemoryTrackerData data;
                if (isRead)
                {
                    data.accessCountRead = 1;
                    data.totalSizeRead = size;
                    data.accessCountWrite = 0;
                    data.totalSizeWrite = 0;
                }
                else
                {
                    data.accessCountRead = 0;
                    data.totalSizeRead = 0;
                    data.accessCountWrite = 1;
                    data.totalSizeWrite = size;
                }
                data.tscMin = tsc;
                data.tscMax = tsc;
                trackedMemory.insert(i, MemoryTrackerMap::value_type(key, data));
            }
        }
    };

    //////////////////////////////////////////////////////////////////////////
    /// @brief Event handler that handles API thread events. This is shared
    ///        between the API and its caller (e.g. driver shim) but typically
    ///        there is only a single API thread per context. So you can save
    ///        information in the class to be used for other events.
    class EventHandlerApiStats : public EventHandlerFile
    {
    public:
        EventHandlerApiStats(uint32_t id) : EventHandlerFile(id)
        {
#if defined(_WIN32)
            // Attempt to copy the events.proto file to the ArchRast output dir. It's common for
            // tools to place the events.proto file in the DEBUG_OUTPUT_DIR when launching AR. If it
            // exists, this will attempt to copy it the first time we get here to package it with
            // the stats. Otherwise, the user would need to specify the events.proto location when
            // parsing the stats in post.
            std::stringstream eventsProtoSrcFilename, eventsProtoDstFilename;
            eventsProtoSrcFilename << KNOB_DEBUG_OUTPUT_DIR << "\\events.proto" << std::ends;
            eventsProtoDstFilename << mOutputDir.substr(0, mOutputDir.size() - 1)
                                   << "\\events.proto" << std::ends;

            // If event.proto already exists, we're done; else do the copy
            struct stat buf; // Use a Posix stat for file existence check
            if (!stat(eventsProtoDstFilename.str().c_str(), &buf) == 0)
            {
                // Now check to make sure the events.proto source exists
                if (stat(eventsProtoSrcFilename.str().c_str(), &buf) == 0)
                {
                    std::ifstream srcFile;
                    srcFile.open(eventsProtoSrcFilename.str().c_str(), std::ios::binary);
                    if (srcFile.is_open())
                    {
                        // Just do a binary buffer copy
                        std::ofstream dstFile;
                        dstFile.open(eventsProtoDstFilename.str().c_str(), std::ios::binary);
                        dstFile << srcFile.rdbuf();
                        dstFile.close();
                    }
                    srcFile.close();
                }
            }
#endif
        }

        virtual void Handle(const DrawInstancedEvent& event)
        {
            DrawInfoEvent e(event.data.drawId,
                            ArchRast::Instanced,
                            event.data.topology,
                            event.data.numVertices,
                            0,
                            0,
                            event.data.startVertex,
                            event.data.numInstances,
                            event.data.startInstance,
                            event.data.tsEnable,
                            event.data.gsEnable,
                            event.data.soEnable,
                            event.data.soTopology,
                            event.data.splitId);

            EventHandlerFile::Handle(e);
        }

        virtual void Handle(const DrawIndexedInstancedEvent& event)
        {
            DrawInfoEvent e(event.data.drawId,
                            ArchRast::IndexedInstanced,
                            event.data.topology,
                            0,
                            event.data.numIndices,
                            event.data.indexOffset,
                            event.data.baseVertex,
                            event.data.numInstances,
                            event.data.startInstance,
                            event.data.tsEnable,
                            event.data.gsEnable,
                            event.data.soEnable,
                            event.data.soTopology,
                            event.data.splitId);

            EventHandlerFile::Handle(e);
        }
    };

    //////////////////////////////////////////////////////////////////////////
    /// @brief Event handler that handles worker thread events. There is one
    ///        event handler per thread. The python script will need to sum
    ///        up counters across all of the threads.
    class EventHandlerWorkerStats : public EventHandlerFile
    {
    public:
        EventHandlerWorkerStats(uint32_t id) : EventHandlerFile(id), mNeedFlush(false)
        {
            memset(mShaderStats, 0, sizeof(mShaderStats));

            // compute address mask for memory tracking
            mAddressMask = 0;
            uint64_t addressRangeBytes = 64;
            while (addressRangeBytes > 0)
            {
                mAddressMask = (mAddressMask << 1) | 1;
                addressRangeBytes = addressRangeBytes >> 1;
            }
            mMemGranularity = mAddressMask + 1;
            mAddressMask = ~mAddressMask;
        }

        virtual void Handle(const EarlyDepthStencilInfoSingleSample& event)
        {
            // earlyZ test compute
            mDSSingleSample.earlyZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSSingleSample.earlyZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // earlyStencil test compute
            mDSSingleSample.earlyStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSSingleSample.earlyStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            // earlyZ test single and multi sample
            mDSCombined.earlyZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSCombined.earlyZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // earlyStencil test single and multi sample
            mDSCombined.earlyStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSCombined.earlyStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            mNeedFlush = true;
        }

        virtual void Handle(const EarlyDepthStencilInfoSampleRate& event)
        {
            // earlyZ test compute
            mDSSampleRate.earlyZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSSampleRate.earlyZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // earlyStencil test compute
            mDSSampleRate.earlyStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSSampleRate.earlyStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            // earlyZ test single and multi sample
            mDSCombined.earlyZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSCombined.earlyZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // earlyStencil test single and multi sample
            mDSCombined.earlyStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSCombined.earlyStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            mNeedFlush = true;
        }

        virtual void Handle(const EarlyDepthStencilInfoNullPS& event)
        {
            // earlyZ test compute
            mDSNullPS.earlyZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSNullPS.earlyZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // earlyStencil test compute
            mDSNullPS.earlyStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSNullPS.earlyStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);
            mNeedFlush = true;
        }

        virtual void Handle(const LateDepthStencilInfoSingleSample& event)
        {
            // lateZ test compute
            mDSSingleSample.lateZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSSingleSample.lateZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // lateStencil test compute
            mDSSingleSample.lateStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSSingleSample.lateStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            // lateZ test single and multi sample
            mDSCombined.lateZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSCombined.lateZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // lateStencil test single and multi sample
            mDSCombined.lateStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSCombined.lateStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            mNeedFlush = true;
        }

        virtual void Handle(const LateDepthStencilInfoSampleRate& event)
        {
            // lateZ test compute
            mDSSampleRate.lateZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSSampleRate.lateZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // lateStencil test compute
            mDSSampleRate.lateStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSSampleRate.lateStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            // lateZ test single and multi sample
            mDSCombined.lateZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSCombined.lateZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // lateStencil test single and multi sample
            mDSCombined.lateStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSCombined.lateStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);

            mNeedFlush = true;
        }

        virtual void Handle(const LateDepthStencilInfoNullPS& event)
        {
            // lateZ test compute
            mDSNullPS.lateZTestPassCount += _mm_popcnt_u32(event.data.depthPassMask);
            mDSNullPS.lateZTestFailCount +=
                _mm_popcnt_u32((!event.data.depthPassMask) & event.data.coverageMask);

            // lateStencil test compute
            mDSNullPS.lateStencilTestPassCount += _mm_popcnt_u32(event.data.stencilPassMask);
            mDSNullPS.lateStencilTestFailCount +=
                _mm_popcnt_u32((!event.data.stencilPassMask) & event.data.coverageMask);
            mNeedFlush = true;
        }

        virtual void Handle(const EarlyDepthInfoPixelRate& event)
        {
            // earlyZ test compute
            mDSPixelRate.earlyZTestPassCount += event.data.depthPassCount;
            mDSPixelRate.earlyZTestFailCount +=
                (_mm_popcnt_u32(event.data.activeLanes) - event.data.depthPassCount);
            mNeedFlush = true;
        }


        virtual void Handle(const LateDepthInfoPixelRate& event)
        {
            // lateZ test compute
            mDSPixelRate.lateZTestPassCount += event.data.depthPassCount;
            mDSPixelRate.lateZTestFailCount +=
                (_mm_popcnt_u32(event.data.activeLanes) - event.data.depthPassCount);
            mNeedFlush = true;
        }


        virtual void Handle(const ClipInfoEvent& event)
        {
            mClipper.mustClipCount += _mm_popcnt_u32(event.data.clipMask);
            mClipper.trivialRejectCount +=
                event.data.numInvocations - _mm_popcnt_u32(event.data.validMask);
            mClipper.trivialAcceptCount +=
                _mm_popcnt_u32(event.data.validMask & ~event.data.clipMask);
        }

        void UpdateStats(SWR_SHADER_STATS* pStatTotals, const SWR_SHADER_STATS* pStatUpdate)
        {
            pStatTotals->numInstExecuted += pStatUpdate->numInstExecuted;
            pStatTotals->numSampleExecuted += pStatUpdate->numSampleExecuted;
            pStatTotals->numSampleLExecuted += pStatUpdate->numSampleLExecuted;
            pStatTotals->numSampleBExecuted += pStatUpdate->numSampleBExecuted;
            pStatTotals->numSampleCExecuted += pStatUpdate->numSampleCExecuted;
            pStatTotals->numSampleCLZExecuted += pStatUpdate->numSampleCLZExecuted;
            pStatTotals->numSampleCDExecuted += pStatUpdate->numSampleCDExecuted;
            pStatTotals->numGather4Executed += pStatUpdate->numGather4Executed;
            pStatTotals->numGather4CExecuted += pStatUpdate->numGather4CExecuted;
            pStatTotals->numGather4CPOExecuted += pStatUpdate->numGather4CPOExecuted;
            pStatTotals->numGather4CPOCExecuted += pStatUpdate->numGather4CPOCExecuted;
            pStatTotals->numLodExecuted += pStatUpdate->numLodExecuted;
        }

        virtual void Handle(const VSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_VERTEX], pStats);
        }

        virtual void Handle(const GSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_GEOMETRY], pStats);
        }

        virtual void Handle(const DSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_DOMAIN], pStats);
        }

        virtual void Handle(const HSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_HULL], pStats);
        }

        virtual void Handle(const PSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_PIXEL], pStats);
            mNeedFlush = true;
        }

        virtual void Handle(const CSStats& event)
        {
            SWR_SHADER_STATS* pStats = (SWR_SHADER_STATS*)event.data.hStats;
            UpdateStats(&mShaderStats[SHADER_COMPUTE], pStats);
            mNeedFlush = true;
        }

        // Flush cached events for this draw
        virtual void FlushDraw(uint32_t drawId)
        {
            if (mNeedFlush == false)
                return;

            EventHandlerFile::Handle(PSInfo(drawId,
                                            mShaderStats[SHADER_PIXEL].numInstExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleLExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleBExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleCExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleCLZExecuted,
                                            mShaderStats[SHADER_PIXEL].numSampleCDExecuted,
                                            mShaderStats[SHADER_PIXEL].numGather4Executed,
                                            mShaderStats[SHADER_PIXEL].numGather4CExecuted,
                                            mShaderStats[SHADER_PIXEL].numGather4CPOExecuted,
                                            mShaderStats[SHADER_PIXEL].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_PIXEL].numLodExecuted));
            EventHandlerFile::Handle(CSInfo(drawId,
                                            mShaderStats[SHADER_COMPUTE].numInstExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleLExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleBExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleCExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleCLZExecuted,
                                            mShaderStats[SHADER_COMPUTE].numSampleCDExecuted,
                                            mShaderStats[SHADER_COMPUTE].numGather4Executed,
                                            mShaderStats[SHADER_COMPUTE].numGather4CExecuted,
                                            mShaderStats[SHADER_COMPUTE].numGather4CPOExecuted,
                                            mShaderStats[SHADER_COMPUTE].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_COMPUTE].numLodExecuted));

            // singleSample
            EventHandlerFile::Handle(EarlyZSingleSample(
                drawId, mDSSingleSample.earlyZTestPassCount, mDSSingleSample.earlyZTestFailCount));
            EventHandlerFile::Handle(LateZSingleSample(
                drawId, mDSSingleSample.lateZTestPassCount, mDSSingleSample.lateZTestFailCount));
            EventHandlerFile::Handle(
                EarlyStencilSingleSample(drawId,
                                         mDSSingleSample.earlyStencilTestPassCount,
                                         mDSSingleSample.earlyStencilTestFailCount));
            EventHandlerFile::Handle(
                LateStencilSingleSample(drawId,
                                        mDSSingleSample.lateStencilTestPassCount,
                                        mDSSingleSample.lateStencilTestFailCount));

            // sampleRate
            EventHandlerFile::Handle(EarlyZSampleRate(
                drawId, mDSSampleRate.earlyZTestPassCount, mDSSampleRate.earlyZTestFailCount));
            EventHandlerFile::Handle(LateZSampleRate(
                drawId, mDSSampleRate.lateZTestPassCount, mDSSampleRate.lateZTestFailCount));
            EventHandlerFile::Handle(
                EarlyStencilSampleRate(drawId,
                                       mDSSampleRate.earlyStencilTestPassCount,
                                       mDSSampleRate.earlyStencilTestFailCount));
            EventHandlerFile::Handle(LateStencilSampleRate(drawId,
                                                           mDSSampleRate.lateStencilTestPassCount,
                                                           mDSSampleRate.lateStencilTestFailCount));

            // combined
            EventHandlerFile::Handle(
                EarlyZ(drawId, mDSCombined.earlyZTestPassCount, mDSCombined.earlyZTestFailCount));
            EventHandlerFile::Handle(
                LateZ(drawId, mDSCombined.lateZTestPassCount, mDSCombined.lateZTestFailCount));
            EventHandlerFile::Handle(EarlyStencil(drawId,
                                                  mDSCombined.earlyStencilTestPassCount,
                                                  mDSCombined.earlyStencilTestFailCount));
            EventHandlerFile::Handle(LateStencil(drawId,
                                                 mDSCombined.lateStencilTestPassCount,
                                                 mDSCombined.lateStencilTestFailCount));

            // pixelRate
            EventHandlerFile::Handle(EarlyZPixelRate(
                drawId, mDSPixelRate.earlyZTestPassCount, mDSPixelRate.earlyZTestFailCount));
            EventHandlerFile::Handle(LateZPixelRate(
                drawId, mDSPixelRate.lateZTestPassCount, mDSPixelRate.lateZTestFailCount));


            // NullPS
            EventHandlerFile::Handle(
                EarlyZNullPS(drawId, mDSNullPS.earlyZTestPassCount, mDSNullPS.earlyZTestFailCount));
            EventHandlerFile::Handle(EarlyStencilNullPS(
                drawId, mDSNullPS.earlyStencilTestPassCount, mDSNullPS.earlyStencilTestFailCount));

            // Rasterized Subspans
            EventHandlerFile::Handle(RasterTiles(drawId, rastStats.rasterTiles));

            // Alpha Subspans
            EventHandlerFile::Handle(
                AlphaEvent(drawId, mAlphaStats.alphaTestCount, mAlphaStats.alphaBlendCount));

            // Primitive Culling
            EventHandlerFile::Handle(
                CullEvent(drawId, mCullStats.backfacePrimCount, mCullStats.degeneratePrimCount));

            mDSSingleSample = {};
            mDSSampleRate   = {};
            mDSCombined     = {};
            mDSPixelRate    = {};
            mDSNullPS = {};

            rastStats   = {};
            mCullStats  = {};
            mAlphaStats = {};

            mShaderStats[SHADER_PIXEL]   = {};
            mShaderStats[SHADER_COMPUTE] = {};

            mNeedFlush = false;
        }

        virtual void Handle(const FrontendDrawEndEvent& event)
        {
            // Clipper
            EventHandlerFile::Handle(ClipperEvent(event.data.drawId,
                                                  mClipper.trivialRejectCount,
                                                  mClipper.trivialAcceptCount,
                                                  mClipper.mustClipCount));

            // Tesselator
            EventHandlerFile::Handle(TessPrims(event.data.drawId, mTS.inputPrims));

            // Geometry Shader
            EventHandlerFile::Handle(GSInputPrims(event.data.drawId, mGS.inputPrimCount));
            EventHandlerFile::Handle(GSPrimsGen(event.data.drawId, mGS.primGeneratedCount));
            EventHandlerFile::Handle(GSVertsInput(event.data.drawId, mGS.vertsInput));

            EventHandlerFile::Handle(VSInfo(event.data.drawId,
                                            mShaderStats[SHADER_VERTEX].numInstExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleLExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleBExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleCExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleCLZExecuted,
                                            mShaderStats[SHADER_VERTEX].numSampleCDExecuted,
                                            mShaderStats[SHADER_VERTEX].numGather4Executed,
                                            mShaderStats[SHADER_VERTEX].numGather4CExecuted,
                                            mShaderStats[SHADER_VERTEX].numGather4CPOExecuted,
                                            mShaderStats[SHADER_VERTEX].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_VERTEX].numLodExecuted));
            EventHandlerFile::Handle(HSInfo(event.data.drawId,
                                            mShaderStats[SHADER_HULL].numInstExecuted,
                                            mShaderStats[SHADER_HULL].numSampleExecuted,
                                            mShaderStats[SHADER_HULL].numSampleLExecuted,
                                            mShaderStats[SHADER_HULL].numSampleBExecuted,
                                            mShaderStats[SHADER_HULL].numSampleCExecuted,
                                            mShaderStats[SHADER_HULL].numSampleCLZExecuted,
                                            mShaderStats[SHADER_HULL].numSampleCDExecuted,
                                            mShaderStats[SHADER_HULL].numGather4Executed,
                                            mShaderStats[SHADER_HULL].numGather4CExecuted,
                                            mShaderStats[SHADER_HULL].numGather4CPOExecuted,
                                            mShaderStats[SHADER_HULL].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_HULL].numLodExecuted));
            EventHandlerFile::Handle(DSInfo(event.data.drawId,
                                            mShaderStats[SHADER_DOMAIN].numInstExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleLExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleBExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleCExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleCLZExecuted,
                                            mShaderStats[SHADER_DOMAIN].numSampleCDExecuted,
                                            mShaderStats[SHADER_DOMAIN].numGather4Executed,
                                            mShaderStats[SHADER_DOMAIN].numGather4CExecuted,
                                            mShaderStats[SHADER_DOMAIN].numGather4CPOExecuted,
                                            mShaderStats[SHADER_DOMAIN].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_DOMAIN].numLodExecuted));
            EventHandlerFile::Handle(GSInfo(event.data.drawId,
                                            mShaderStats[SHADER_GEOMETRY].numInstExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleLExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleBExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleCExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleCLZExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numSampleCDExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numGather4Executed,
                                            mShaderStats[SHADER_GEOMETRY].numGather4CExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numGather4CPOExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numGather4CPOCExecuted,
                                            mShaderStats[SHADER_GEOMETRY].numLodExecuted));

            mShaderStats[SHADER_VERTEX]   = {};
            mShaderStats[SHADER_HULL]     = {};
            mShaderStats[SHADER_DOMAIN]   = {};
            mShaderStats[SHADER_GEOMETRY] = {};

            // Reset Internal Counters
            mClipper = {};
            mTS      = {};
            mGS      = {};
        }

        virtual void Handle(const MemoryAccessEvent& event)
        {
            uint64_t trackAddr = event.data.ptr;
            uint64_t nextAddr = (trackAddr & mAddressMask);
            uint32_t sizeTracked = 0;

            while (sizeTracked < event.data.size)
            {
                nextAddr += mMemGranularity;
                uint32_t size = nextAddr - trackAddr;
                size = std::min(event.data.size, size);
                mMemoryStats.TrackMemoryAccess(trackAddr, mAddressMask, event.data.isRead, event.data.tsc, size);
                sizeTracked += size;
                trackAddr = nextAddr;
            }            
        }

        virtual void Handle(const MemoryStatsEndEvent& event)
        {
            MemoryStats::MemoryTrackerMap::iterator i = mMemoryStats.trackedMemory.begin();
            while (i != mMemoryStats.trackedMemory.end())
            {
                MemoryStatsEvent mse(event.data.drawId, 
                                     i->first.address & mAddressMask, 
                                     i->second.accessCountRead, 
                                     i->second.accessCountWrite, 
                                     i->second.totalSizeRead, 
                                     i->second.totalSizeWrite, 
                                     i->second.tscMin, 
                                     i->second.tscMax);
                EventHandlerFile::Handle(mse);
                i++;
            }
            mMemoryStats.trackedMemory.clear();
        }

        virtual void Handle(const GSPrimInfo& event)
        {
            mGS.inputPrimCount += event.data.inputPrimCount;
            mGS.primGeneratedCount += event.data.primGeneratedCount;
            mGS.vertsInput += event.data.vertsInput;
        }

        virtual void Handle(const TessPrimCount& event) { mTS.inputPrims += event.data.primCount; }

        virtual void Handle(const RasterTileCount& event)
        {
            rastStats.rasterTiles += event.data.rasterTiles;
        }

        virtual void Handle(const CullInfoEvent& event)
        {
            mCullStats.degeneratePrimCount += _mm_popcnt_u32(
                event.data.validMask ^ (event.data.validMask & ~event.data.degeneratePrimMask));
            mCullStats.backfacePrimCount += _mm_popcnt_u32(
                event.data.validMask ^ (event.data.validMask & ~event.data.backfacePrimMask));
        }

        virtual void Handle(const AlphaInfoEvent& event)
        {
            mAlphaStats.alphaTestCount += event.data.alphaTestEnable;
            mAlphaStats.alphaBlendCount += event.data.alphaBlendEnable;
        }

    protected:
        bool mNeedFlush;
        // Per draw stats
        DepthStencilStats mDSSingleSample = {};
        DepthStencilStats mDSSampleRate   = {};
        DepthStencilStats mDSPixelRate    = {};
        DepthStencilStats mDSCombined     = {};
        DepthStencilStats mDSNullPS       = {};
        DepthStencilStats mDSOmZ          = {};
        CStats            mClipper        = {};
        TEStats           mTS             = {};
        GSStateInfo       mGS             = {};
        RastStats         rastStats       = {};
        CullStats         mCullStats      = {};
        AlphaStats        mAlphaStats     = {};

        SWR_SHADER_STATS mShaderStats[NUM_SHADER_TYPES];

        MemoryStats      mMemoryStats     = {};
        uint64_t         mAddressMask     = 0;
        uint64_t         mMemGranularity  = 0;

    };

    static EventManager* FromHandle(HANDLE hThreadContext)
    {
        return reinterpret_cast<EventManager*>(hThreadContext);
    }

    // Construct an event manager and associate a handler with it.
    HANDLE CreateThreadContext(AR_THREAD type)
    {
        // Can we assume single threaded here?
        static std::atomic<uint32_t> counter(0);
        uint32_t                     id = counter.fetch_add(1);

        EventManager* pManager = new EventManager();

        if (pManager)
        {
            EventHandlerFile* pHandler = nullptr;

            if (type == AR_THREAD::API)
            {
                pHandler = new EventHandlerApiStats(id);
                pManager->Attach(pHandler);
                pHandler->Handle(ThreadStartApiEvent());
            }
            else
            {
                pHandler = new EventHandlerWorkerStats(id);
                pManager->Attach(pHandler);
                pHandler->Handle(ThreadStartWorkerEvent());
            }

            pHandler->MarkHeader();

            return pManager;
        }

        SWR_INVALID("Failed to register thread.");
        return nullptr;
    }

    void DestroyThreadContext(HANDLE hThreadContext)
    {
        EventManager* pManager = FromHandle(hThreadContext);
        SWR_ASSERT(pManager != nullptr);

        delete pManager;
    }

    // Dispatch event for this thread.
    void Dispatch(HANDLE hThreadContext, const Event& event)
    {
        EventManager* pManager = FromHandle(hThreadContext);
        SWR_ASSERT(pManager != nullptr);

        pManager->Dispatch(event);
    }

    // Flush for this thread.
    void FlushDraw(HANDLE hThreadContext, uint32_t drawId)
    {
        EventManager* pManager = FromHandle(hThreadContext);
        SWR_ASSERT(pManager != nullptr);

        pManager->FlushDraw(drawId);
    }
} // namespace ArchRast