aboutsummaryrefslogtreecommitdiff
path: root/test/src/D3D11/GPUProfiler.h
blob: 7e190ae663427b4fb3187e1be5422038d856f53b (plain) (blame)
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
/*
* Copyright (c) 2008-2016, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA CORPORATION and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA CORPORATION is strictly prohibited.
*/

#pragma once

#include "..\..\..\src\Renderer_DX11.h"

#pragma warning( disable : 4996 )
#pragma warning( disable : 4995 )

#define FRAME_COUNT 100
#define SAMPLE_COUNT 5

#if ENABLE_RENDER_TIMES

namespace D3D11
{

class GPUProfiler
{
public:
    enum State
    {
        STATE_INIT,
        STATE_RUN,
        STATE_DONE,
    };

    GPUProfiler()
    : m_State(STATE_INIT)
    , m_FrameId(0)
    , m_SampleId(0)
    {
    }

    bool IsProfiling()
    {
        return (m_State == STATE_RUN);
    }

    bool IsDone()
    {
        return (m_State == STATE_DONE);
    }

    void StartProfiling()
    {
        m_State = STATE_RUN;
        m_FrameId = 0;
    }

    void Tick(const GFSDK::SSAO::RenderTimes &RenderTimes, const char* FilenameCSV)
    {
        if (IsProfiling())
        {
            if (m_FrameId == FRAME_COUNT)
            {
                WriteSample(RenderTimes, FilenameCSV);
            }

            ++m_FrameId;
        }
    }

private:
    State m_State;
    UINT m_FrameId;
    UINT m_SampleId;

    void WriteSample(const GFSDK::SSAO::RenderTimes &RenderTimes, const char* FilenameCSV)
    {
        if (m_SampleId == 0)
        {
            WriteHeader(FilenameCSV);
        }
        AppendTimings(RenderTimes, FilenameCSV);

        if (++m_SampleId == SAMPLE_COUNT)
        {
            m_State = STATE_DONE;
        }
        else
        {
            StartProfiling();
        }
    }

    void WriteHeader(const char* FilenameCSV)
    {
        FILE *fp = fopen(FilenameCSV, "w");
        if (fp)
        {
            fprintf(fp,"Total,LinearizeZ,DeinterleaveZ,NearAO,FarAO,ReinterleaveAO,BlurX,BlurY\n");
            fclose(fp);
        }
    }

    void AppendTimings(const GFSDK::SSAO::RenderTimes &RenderTimes, const char* FilenameCSV)
    {
        FILE *fp = fopen(FilenameCSV, "a+");
        if (fp)
        {
            fprintf(fp, "%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f,%0.2f\n",
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_TOTAL],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_LINEAR_Z],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_DEINTERLEAVE_Z],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_NORMAL],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_COARSE_AO],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_INTERLEAVE_AO],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_BLURX],
                RenderTimes.GPUTimeMS[GFSDK::SSAO::RENDER_PASS_0][GFSDK::SSAO::REGIME_TIME_BLURY]);

            fclose(fp);
        }
    }
};

} //namespace D3D11

#endif //ENABLE_RENDER_TIMES