summaryrefslogtreecommitdiffhomepage
path: root/ovr_sdk_win_23.0.0/LibOVRKernel/Src/Util/Util_Direct3D.h
blob: af43fb4e5ae3cf6fe5a06ee3799c4740919b171f (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
/************************************************************************************

Filename    :   Util_Direct3D.h
Content     :   Shared code for Direct3D
Created     :   Oct 14, 2014
Authors     :   Chris Taylor

Copyright   :   Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved.

Licensed under the Oculus Master SDK License Version 1.0 (the "License");
you may not use the Oculus VR Rift SDK except in compliance with the License,
which is provided at the time of installation or download, or which
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

https://developer.oculus.com/licenses/oculusmastersdk-1.0

Unless required by applicable law or agreed to in writing, the Oculus VR SDK
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

************************************************************************************/

#ifndef OVR_Util_Direct3D_h
#define OVR_Util_Direct3D_h

#if defined(_WIN32)

// Include Windows correctly first before implicitly including it below
#include "Kernel/OVR_String.h"
#include "Kernel/OVR_Win32_IncludeWindows.h"

// Direct3D 11
#include <D3D11Shader.h>
#include <d3dcompiler.h>

#if _MSC_VER >= 1800
// Visual Studio 2013+ support newer D3D/DXGI headers.
#define OVR_D3D11_VER 2
#include <d3d11_2.h>
#define OVR_DXGI_VER 3
#include <dxgi1_3.h> // Used in place of 1.2 for IDXGIFactory2 debug version (when available)
#elif _MSC_VER >= 1700
// Visual Studio 2012+ only supports older D3D/DXGI headers.
#define OVR_D3D11_VER 1
#include <d3d11_1.h>
#else
// Visual Studio 2010+ only supports original D3D/DXGI headers.
#define OVR_D3D11_VER 1
#include <d3d11.h>
#endif

namespace OVR {
namespace D3DUtil {

String GetWindowsErrorString(HRESULT hr);

//-----------------------------------------------------------------------------
// Helper macros for verifying HRESULT values from Direct3D API calls
//
// These will assert on failure in debug mode, and in release or debug mode
// these functions will report the file and line where the error occurred,
// and what the error code was to the log at Error-level.

// Assert on HRESULT failure
bool VerifyHRESULT(const char* file, int line, HRESULT hr);

#define OVR_D3D_CHECK(hr) OVR::D3DUtil::VerifyHRESULT(__FILE__, __LINE__, hr)

// Internal implementation of the OVR_D3D_CHECK_RET family of functions.
#define OVR_D3D_CHECK_RET_IMPL(hr, returnExpression) \
  {                                                  \
    if (!OVR_D3D_CHECK(hr)) {                        \
      returnExpression                               \
    }                                                \
  }

// Returns provided value on failure.
// Example usage:
//     size_t Func() {
//         . . .
//         HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void
//         **)&pDXGIDevice.GetRawRef());
//         OVR_D3D_CHECK_RET_VAL(hr, 0);
//         . . .
//     }
#define OVR_D3D_CHECK_RET_VAL(hr, failureValue) OVR_D3D_CHECK_RET_IMPL(hr, return failureValue;)

// Returns void on failure
// Example usage:
//     void Func() {
//         . . .
//         HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void
//         **)&pDXGIDevice.GetRawRef());
//         OVR_D3D_CHECK_RET(hr);
//         . . .
//     }
#define OVR_D3D_CHECK_RET(hr) OVR_D3D_CHECK_RET_IMPL(hr, return;)

// Returns false on failure
// Example usage:
//     bool Func() {
//         . . .
//         HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void
//         **)&pDXGIDevice.GetRawRef());
//         OVR_D3D_CHECK_RET_FALSE(hr);
//         . . .
//     }
#define OVR_D3D_CHECK_RET_FALSE(hr) OVR_D3D_CHECK_RET_IMPL(hr, return false;)

// Returns nullptr on failure
// Example usage:
//     void* Func() {
//         . . .
//         HRESULT hr = Device->QueryInterface(__uuidof(IDXGIDevice), (void
//         **)&pDXGIDevice.GetRawRef());
//         OVR_D3D_CHECK_RET_NULL(hr);
//         . . .
//     }
#define OVR_D3D_CHECK_RET_NULL(hr) OVR_D3D_CHECK_RET_IMPL(hr, return nullptr;)

// If the result is a failure, it will write the exact compile error to the error log
void LogD3DCompileError(HRESULT hr, ID3DBlob* errorBlob);

#if defined(OVR_BUILD_DEBUG)

// Enable this to track down double-tagging object warnings from D3D.
#define OVR_D3D_TRACK_DOUBLE_TAGGING

#if defined(OVR_D3D_TRACK_DOUBLE_TAGGING)
#define OVR_D3D_CHECK_REUSE(child)               \
  char tagReuseBuffer[1024];                     \
  UINT reuseSize = (UINT)sizeof(tagReuseBuffer); \
  OVR_ASSERT(FAILED(child->GetPrivateData(WKPDID_D3DDebugObjectName, &reuseSize, tagReuseBuffer)));
#else
#define OVR_D3D_CHECK_REUSE(child) (void(0))
#endif

#define OVR_D3D_TAG_OBJECT(child)                                                            \
  if (child) {                                                                               \
    OVR_D3D_CHECK_REUSE(child);                                                              \
    const char* tagName = OVR_STRINGIZE(child) " " __FILE__ "(" OVR_STRINGIZE(__LINE__) ")"; \
    UINT tagDataSize = (UINT)strlen(tagName);                                                \
    HRESULT tagHR = child->SetPrivateData(WKPDID_D3DDebugObjectName, tagDataSize, tagName);  \
    OVR_D3D_CHECK(tagHR);                                                                    \
  }

#else // !Debug:

#define OVR_D3D_TAG_OBJECT(child) (void(0))

#endif // !Debug
} // namespace D3DUtil
} // namespace OVR

#endif // _WIN32

#endif // OVR_Util_Direct3D_h