-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathafx_d3d_helper.hpp
213 lines (184 loc) · 5.39 KB
/
afx_d3d_helper.hpp
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
// Copyright (c) 2024 Advanced Micro Devices, Inc.
//
// This file is part of the AMD Render Pipeline Shaders SDK which is
// released under the MIT LICENSE.
//
// See file LICENSE.txt for full license details.
#pragma once
#include <stdexcept>
#include <wrl/client.h>
#include <wrl/wrappers/corewrappers.h>
#include <stdlib.h>
#include <wchar.h>
#include <dxgi1_6.h>
#include <d3dcompiler.h>
// Note that while ComPtr is used to manage the lifetime of resources on the CPU,
// it has no understanding of the lifetime of resources on the GPU. Apps must account
// for the GPU lifetime of resources to avoid destroying objects that may still be
// referenced by the GPU.
using Microsoft::WRL::ComPtr;
inline std::string HrToString(HRESULT hr)
{
char s_str[64] = {};
sprintf_s(s_str, "HRESULT of 0x%08X", static_cast<UINT>(hr));
return std::string(s_str);
}
class HrException : public std::runtime_error
{
public:
HrException(HRESULT hr) : std::runtime_error(HrToString(hr)), m_hr(hr) {}
HRESULT Error() const { return m_hr; }
private:
const HRESULT m_hr;
};
#define SAFE_RELEASE(p) if (p) (p)->Release()
inline void ThrowIfFailed(HRESULT hr)
{
if (FAILED(hr))
{
throw HrException(hr);
}
}
template<typename TBlob>
inline void ThrowIfFailedEx(HRESULT hr, TBlob& errorBlob)
{
if (errorBlob)
{
::OutputDebugStringA((const char*)errorBlob->GetBufferPointer());
}
if (FAILED(hr))
{
throw HrException(hr);
}
}
template <UINT pathSize>
inline void GetExeDirPath(WCHAR (&path)[pathSize])
{
if (path == nullptr)
{
throw std::exception();
}
DWORD size = GetModuleFileNameW(nullptr, path, pathSize);
if ((size == 0) || (size == pathSize))
{
// Method failed or path was truncated.
throw std::exception();
}
WCHAR* lastSlash = wcsrchr(path, L'\\');
if (lastSlash)
{
*(lastSlash + 1) = L'\0';
}
}
// Assign a name to the object to aid with debugging.
#if defined(_DEBUG) || defined(DBG)
template<typename TObject>
inline void SetName(TObject* pObject, LPCWSTR name)
{
pObject->SetName(name);
}
template <typename TObject>
inline void SetNameIndexed(TObject* pObject, LPCWSTR name, UINT index)
{
WCHAR fullName[50];
if (swprintf_s(fullName, L"%s[%u]", name, index) > 0)
{
pObject->SetName(fullName);
}
}
#else
template <typename TObject>
inline void SetName(TObject*, LPCWSTR)
{
}
template <typename TObject>
inline void SetNameIndexed(TObject*, LPCWSTR, UINT)
{
}
#endif
// Naming helper for ComPtr<T>.
// Assigns the name of the variable as the name of the object.
// The indexed variant will include the index in the name of the object.
#define NAME_D3D12_OBJECT(x) SetName((x).Get(), L#x)
#define NAME_D3D12_OBJECT_INDEXED(x, n) SetNameIndexed((x)[n].Get(), L#x, n)
#ifdef D3D_COMPILE_STANDARD_FILE_INCLUDE
inline ComPtr<ID3DBlob> CompileShader(
const std::wstring& filename,
const D3D_SHADER_MACRO* defines,
const std::string& entrypoint,
const std::string& target)
{
UINT compileFlags = 0;
#if defined(_DEBUG) || defined(DBG)
compileFlags = D3DCOMPILE_DEBUG | D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
HRESULT hr;
ComPtr<ID3DBlob> byteCode = nullptr;
ComPtr<ID3DBlob> errors;
hr = D3DCompileFromFile(filename.c_str(), defines, D3D_COMPILE_STANDARD_FILE_INCLUDE,
entrypoint.c_str(), target.c_str(), compileFlags, 0, &byteCode, &errors);
if (errors != nullptr)
{
OutputDebugStringA((char*)errors->GetBufferPointer());
}
ThrowIfFailed(hr);
return byteCode;
}
#endif
// Resets all elements in a ComPtr array.
template<class T>
void ResetComPtrArray(T* comPtrArray)
{
for (auto &i : *comPtrArray)
{
i.Reset();
}
}
// Resets all elements in a unique_ptr array.
template<class T>
void ResetUniquePtrArray(T* uniquePtrArray)
{
for (auto &i : *uniquePtrArray)
{
i.reset();
}
}
template <typename TFunc>
void FindAdapter(IDXGIFactory4* pFactory, TFunc testDeviceCapability, IDXGIAdapter1** ppAdapter, bool& useWarpDevice)
{
ComPtr<IDXGIAdapter1> selectedAdapter = nullptr;
if (!useWarpDevice)
{
for (UINT adapterIndex = 0; S_OK == pFactory->EnumAdapters1(adapterIndex, &selectedAdapter); ++adapterIndex)
{
DXGI_ADAPTER_DESC1 desc;
ThrowIfFailed(selectedAdapter->GetDesc1(&desc));
static const wchar_t* const warpAdapterNames[] = {L"Microsoft Basic Render Driver",
L"Microsoft Basic Display Adapter"};
if (std::cend(warpAdapterNames) !=
std::find_if(
std::cbegin(warpAdapterNames), std::cend(warpAdapterNames), [&](const wchar_t* warpAdapterName) {
return wcscmp(desc.Description, warpAdapterName) == 0;
}))
{
// Skip warp
continue;
}
// Check to see if the adapter supports desired feature level, but don't create
// the actual device yet.
if (testDeviceCapability(selectedAdapter.Get()))
{
break;
}
}
}
if ((selectedAdapter == nullptr) || useWarpDevice)
{
ThrowIfFailed(pFactory->EnumWarpAdapter(IID_PPV_ARGS(ppAdapter)));
useWarpDevice = true;
}
else
{
*ppAdapter = selectedAdapter.Detach();
}
}