-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathD3DDevice.cpp
231 lines (207 loc) · 5.32 KB
/
D3DDevice.cpp
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
#include "D3D12FrameWork/D3DDevice.h"
#include "D3D12FrameWork/CommandQueue.h"
#include "D3D12FrameWork/CommandList.h"
#include "D3D12FrameWork/SwapChain.h"
#include "D3D12FrameWork/Fence.h"
#include "D3D12FrameWork/BufferView.h"
#include "D3D12FrameWork/TextureView.h"
#include "D3D12FrameWork/SamplerView.h"
#include <vector>
namespace D3D12FrameWork {
D3DDevice::D3DDevice() {
}
D3DDevice::~D3DDevice() {
Term();
}
bool D3DDevice::Init(HWND const& _hwnd, LONG _width,LONG _height) {
uint32_t factoryFlags = 0;
#if defined(DEBUG) || defined(_DEBUG)
//デバッグレイヤーの有効か
ID3D12Debug* debug;
if (SUCCEEDED(D3D12GetDebugInterface(IID_PPV_ARGS(&debug)))) {
debug->EnableDebugLayer();
debug->Release();
debug = nullptr;
}
#endif // defined(DEBUG) || defined(_DEBUG)
auto hr = CreateDXGIFactory2(0, IID_PPV_ARGS(m_pFactory.ReleaseAndGetAddressOf()));
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
hr = D3D12CreateDevice(
nullptr,
D3D_FEATURE_LEVEL_11_0,//デバイスによってチェックする必要あり
IID_PPV_ARGS(m_pDevice.ReleaseAndGetAddressOf())
);
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
//ディスクリプタヒープの作製
D3D12_DESCRIPTOR_HEAP_DESC heapDesc = {};
heapDesc.NodeMask = 0;
// //CBV,SRV,UAV
{
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heapDesc.NumDescriptors = 100;
m_pCbvSrvUavHeap =std::make_unique<DescriptorAllocator>();
if (!m_pCbvSrvUavHeap->Init(
this,
heapDesc
)) {
assert(false);
return false;
}
}
// //RTV
{
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heapDesc.NumDescriptors = 100;
m_pRtvHeap = std::make_unique<DescriptorAllocator>();
if (!m_pRtvHeap->Init(
this,
heapDesc
)) {
assert(false);
return false;
}
}
// Dsv
{
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heapDesc.NumDescriptors = 20;
m_pDsvHeap = std::make_unique<DescriptorAllocator>();
if (!m_pDsvHeap->Init(
this,
heapDesc
)) {
assert(false);
return false;
}
}
// Smp
{
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
heapDesc.NumDescriptors = 20;
m_pSmpHeap = std::make_unique<DescriptorAllocator>();
if (!m_pSmpHeap->Init(
this,
heapDesc
)) {
assert(false);
return false;
}
}
m_pCommandQueue = std::make_unique<CommandQueue>();
if (!m_pCommandQueue->Init(this, D3D12_COMMAND_LIST_TYPE_DIRECT)) {
assert(false);
return false;
}
m_pSwapChain = std::make_unique<SwapChain>();
if (!m_pSwapChain->Init(this, m_pCommandQueue.get(), _hwnd,_width, _height)) {
assert(false);
return false;
}
////fence
//m_pFence = std::make_unique<Fence>();
//if (!m_pFence->Init(this)) {
// assert(false);
// return false;
//}
if (!ConstantBufferView::CreateNullView(this)) {
assert(false);
return false;
}
if (!ShaderResourceView::CreateNullView(this)) {
assert(false);
return false;
}
if (!SamplerView::CreateNullView(this)) {
assert(false);
return false;
}
return true;
}
void
D3DDevice::WaitPresent() {
if (m_pSwapChain.get()) m_pSwapChain.get()->WaitPresent();
}
void
D3DDevice::Term() {
//m_pFence->WaitSignal();
WaitPresent();
//順番に破棄する必要がある.
m_deathList.SyncKill();
ShaderResourceView::DestroyNullView();
ConstantBufferView::DestroyNullView();
SamplerView::DestroyNullView();
//m_pFence.reset();
//delete graphic systems
m_pSwapChain.reset();
m_pCommandQueue.reset();
//delete Heaps
m_pDsvHeap.reset();
m_pRtvHeap.reset();
m_pCbvSrvUavHeap.reset();
//delete dx12Systems
m_pDevice.Reset();
m_pFactory.Reset();
}
void
D3DDevice::EndAndExecuteCommandList(CommandList** _cmdLists, uint32_t _numCmdLists) {
std::vector<ID3D12CommandList*> pCmdLists;
pCmdLists.resize(_numCmdLists);
for (int i = 0; i < _numCmdLists;i++) {
_cmdLists[i]->End();
pCmdLists[i] = _cmdLists[i]->GetList();
}
m_pCommandQueue->GetQueue()->ExecuteCommandLists(
_numCmdLists,
pCmdLists.data()
);
for (int i = 0; i < _numCmdLists; i++) {
_cmdLists[i]->Signal(m_pCommandQueue.get());
}
}
void
D3DDevice::Present(uint32_t _interval) {
m_pSwapChain->Present(_interval);
//m_pFence->Signal(m_pCommandQueue.get());
//m_pFence->WaitSignal();
}
bool D3DDevice::CheckMeshShaderSupport() {
{
//shade model をチェック
D3D12_FEATURE_DATA_SHADER_MODEL shaderModel = {
D3D_SHADER_MODEL_6_5
};
auto hr = m_pDevice->CheckFeatureSupport(
D3D12_FEATURE_SHADER_MODEL, &shaderModel, sizeof(shaderModel)
);
if (FAILED(hr) ||
shaderModel.HighestShaderModel < D3D_SHADER_MODEL_6_5) {
OutputDebugStringA("Error: Shader Model 6.5 is not supported.");
return false;
}
}
//msをチェック
{
D3D12_FEATURE_DATA_D3D12_OPTIONS7 features = {};
auto hr = m_pDevice->CheckFeatureSupport(
D3D12_FEATURE_D3D12_OPTIONS7, &features,
sizeof(features)
);
if (FAILED(hr) ||
features.MeshShaderTier == D3D12_MESH_SHADER_TIER_NOT_SUPPORTED) {
OutputDebugStringA("Error: Mesh Shaders aren't supported");
return false;
}
}
return true;
}
}