summaryrefslogtreecommitdiffstats
path: root/src/3rdparty/angle/src/common/debug.cpp
blob: 5f55ff1e394ef1b265155486287c8f3a736dc160 (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
//
// Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//

// debug.cpp: Debugging utilities.

#include "common/debug.h"
#include "common/platform.h"
#include "common/angleutils.h"

#include <stdarg.h>
#include <vector>
#include <fstream>
#include <cstdio>

namespace gl
{
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
// Wraps the D3D9/D3D11 debug annotation functions.
class DebugAnnotationWrapper
{
  public:
    DebugAnnotationWrapper() { };
    virtual ~DebugAnnotationWrapper() { };
    virtual void beginEvent(const std::wstring &eventName) = 0;
    virtual void endEvent() = 0;
    virtual void setMarker(const std::wstring &markerName) = 0;
    virtual bool getStatus() = 0;
};

#if defined(ANGLE_ENABLE_D3D9)
class D3D9DebugAnnotationWrapper : public DebugAnnotationWrapper
{
  public:
    void beginEvent(const std::wstring &eventName)
    {
        D3DPERF_BeginEvent(0, eventName.c_str());
    }

    void endEvent()
    {
        D3DPERF_EndEvent();
    }

    void setMarker(const std::wstring &markerName)
    {
        D3DPERF_SetMarker(0, markerName.c_str());
    }

    bool getStatus()
    {
        return !!D3DPERF_GetStatus();
    }
};
#endif // ANGLE_ENABLE_D3D9

#if defined(ANGLE_ENABLE_D3D11)
class D3D11DebugAnnotationWrapper : public DebugAnnotationWrapper
{
  public:

    D3D11DebugAnnotationWrapper()
      : mInitialized(false),
        mD3d11Module(NULL),
        mUserDefinedAnnotation(NULL)
    {
        // D3D11 devices can't be created during DllMain.
        // We defer device creation until the object is actually used.
    }

    ~D3D11DebugAnnotationWrapper()
    {
        if (mInitialized)
        {
            SafeRelease(mUserDefinedAnnotation);
            FreeLibrary(mD3d11Module);
        }
    }

    virtual void beginEvent(const std::wstring &eventName)
    {
        initializeDevice();

        mUserDefinedAnnotation->BeginEvent(eventName.c_str());
    }

    virtual void endEvent()
    {
        initializeDevice();

        mUserDefinedAnnotation->EndEvent();
    }

    virtual void setMarker(const std::wstring &markerName)
    {
        initializeDevice();

        mUserDefinedAnnotation->SetMarker(markerName.c_str());
    }

    virtual bool getStatus()
    {
        // ID3DUserDefinedAnnotation::GetStatus doesn't work with the Graphics Diagnostics tools in Visual Studio 2013.

#if defined(_DEBUG) && defined(ANGLE_ENABLE_WINDOWS_STORE)
        // In the Windows Store, we can use IDXGraphicsAnalysis. The call to GetDebugInterface1 only succeeds if the app is under capture.
        // This should only be called in DEBUG mode.
        // If an app links against DXGIGetDebugInterface1 in release mode then it will fail Windows Store ingestion checks.
        IDXGraphicsAnalysis* graphicsAnalysis;
        DXGIGetDebugInterface1(0, IID_PPV_ARGS(&graphicsAnalysis));
        bool underCapture = (graphicsAnalysis != NULL);
        SafeRelease(graphicsAnalysis);
        return underCapture;
#endif

        // Otherwise, we have to return true here.
        return true;
    }

  protected:

    void initializeDevice()
    {
        if (!mInitialized)
        {
#if !defined(ANGLE_ENABLE_WINDOWS_STORE)
            mD3d11Module = LoadLibrary(TEXT("d3d11.dll"));
            ASSERT(mD3d11Module);

            PFN_D3D11_CREATE_DEVICE D3D11CreateDevice = (PFN_D3D11_CREATE_DEVICE)GetProcAddress(mD3d11Module, "D3D11CreateDevice");
            ASSERT(D3D11CreateDevice != NULL);
#endif // !ANGLE_ENABLE_WINDOWS_STORE

            ID3D11Device* device = NULL;
            ID3D11DeviceContext* context = NULL;

            HRESULT hr = E_FAIL;

            // Create a D3D_DRIVER_TYPE_NULL device, which is much cheaper than other types of device.
            hr =  D3D11CreateDevice(NULL, D3D_DRIVER_TYPE_NULL, NULL, 0, NULL, 0, D3D11_SDK_VERSION, &device, NULL, &context);
            ASSERT(SUCCEEDED(hr));

            hr = context->QueryInterface(__uuidof(mUserDefinedAnnotation), reinterpret_cast<void**>(&mUserDefinedAnnotation));
            ASSERT(SUCCEEDED(hr) && mUserDefinedAnnotation != NULL);

            SafeRelease(device);
            SafeRelease(context);

            mInitialized = true;
        }
    }

    bool mInitialized;
    HMODULE mD3d11Module;
    ID3DUserDefinedAnnotation* mUserDefinedAnnotation;
};
#endif // ANGLE_ENABLE_D3D11

static DebugAnnotationWrapper* g_DebugAnnotationWrapper = NULL;

void InitializeDebugAnnotations()
{
#if defined(ANGLE_ENABLE_D3D9)
    g_DebugAnnotationWrapper = new D3D9DebugAnnotationWrapper();
#elif defined(ANGLE_ENABLE_D3D11)
    // If the project uses D3D9 then we can use the D3D9 debug annotations, even with the D3D11 renderer.
    // However, if D3D9 is unavailable (e.g. in Windows Store), then we use D3D11 debug annotations.
    // The D3D11 debug annotations are methods on ID3DUserDefinedAnnotation, which is implemented by the DeviceContext.
    // This doesn't have to be the same DeviceContext that the renderer uses, though.
    g_DebugAnnotationWrapper = new D3D11DebugAnnotationWrapper();
#endif
}

void UninitializeDebugAnnotations()
{
    if (g_DebugAnnotationWrapper != NULL)
    {
        SafeDelete(g_DebugAnnotationWrapper);
    }
}

#endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS

enum DebugTraceOutputType
{
   DebugTraceOutputTypeNone,
   DebugTraceOutputTypeSetMarker,
   DebugTraceOutputTypeBeginEvent
};

static void output(bool traceInDebugOnly, DebugTraceOutputType outputType, const char *format, va_list vararg)
{
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
    static std::vector<char> buffer(512);

    if (perfActive())
    {
        size_t len = FormatStringIntoVector(format, vararg, buffer);
        std::wstring formattedWideMessage(buffer.begin(), buffer.begin() + len);

        switch (outputType)
        {
            case DebugTraceOutputTypeNone:
                break;
            case DebugTraceOutputTypeBeginEvent:
                g_DebugAnnotationWrapper->beginEvent(formattedWideMessage);
                break;
            case DebugTraceOutputTypeSetMarker:
                g_DebugAnnotationWrapper->setMarker(formattedWideMessage);
                break;
        }
    }
#endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS

#if defined(ANGLE_ENABLE_DEBUG_TRACE)
#if defined(NDEBUG)
    if (traceInDebugOnly)
    {
        return;
    }
#endif // NDEBUG
    std::string formattedMessage = FormatString(format, vararg);

    static std::ofstream file(TRACE_OUTPUT_FILE, std::ofstream::app);
    if (file)
    {
        file.write(formattedMessage.c_str(), formattedMessage.length());
        file.flush();
    }

#if defined(ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER)
    OutputDebugStringA(formattedMessage.c_str());
#endif // ANGLE_ENABLE_DEBUG_TRACE_TO_DEBUGGER

#endif // ANGLE_ENABLE_DEBUG_TRACE
}

void trace(bool traceInDebugOnly, const char *format, ...)
{
    va_list vararg;
    va_start(vararg, format);
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
    output(traceInDebugOnly, DebugTraceOutputTypeSetMarker, format, vararg);
#else
    output(traceInDebugOnly, DebugTraceOutputTypeNone, format, vararg);
#endif
    va_end(vararg);
}

bool perfActive()
{
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
    static bool active = g_DebugAnnotationWrapper->getStatus();
    return active;
#else
    return false;
#endif
}

ScopedPerfEventHelper::ScopedPerfEventHelper(const char* format, ...)
{
#if !defined(ANGLE_ENABLE_DEBUG_TRACE)
    if (!perfActive())
    {
        return;
    }
#endif // !ANGLE_ENABLE_DEBUG_TRACE
    va_list vararg;
    va_start(vararg, format);
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
    output(true, DebugTraceOutputTypeBeginEvent, format, vararg);
#else
    output(true, DebugTraceOutputTypeNone, format, vararg);
#endif // ANGLE_ENABLE_DEBUG_ANNOTATIONS
    va_end(vararg);
}

ScopedPerfEventHelper::~ScopedPerfEventHelper()
{
#if defined(ANGLE_ENABLE_DEBUG_ANNOTATIONS)
    if (perfActive())
    {
        g_DebugAnnotationWrapper->endEvent();
    }
#endif
}
}