-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandList.cpp
272 lines (239 loc) · 6.84 KB
/
CommandList.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
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
#include "D3D12FrameWork/CommandList.h"
#include "D3D12FrameWork/CommandQueue.h"
#include "D3D12FrameWork/D3DDevice.h"
#include "D3D12FrameWork/Common.h"
#include "D3D12FrameWork/Texture.h"
#include "D3D12FrameWork/RootParameterBuffer.h"
#include "D3D12FrameWork/RenderComponent.h"
#include "D3D12FrameWork/GraphicPipelineStateObject.h"
#include "D3D12FrameWork/RootSignature.h"
namespace D3D12FrameWork {
CommandList::CommandList() {
}
bool CommandList::Init(D3DDevice* dev, D3D12_COMMAND_LIST_TYPE _type,
GlobalDescriptorHeapDesc const& _desc) {
auto hr = dev->GetDev()->CreateCommandAllocator(
_type,
IID_PPV_ARGS(m_pAllocator.ReleaseAndGetAddressOf())
);
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
hr = dev->GetDev()->CreateCommandList(
0,
_type,
m_pAllocator.Get(),
nullptr,
IID_PPV_ARGS(m_pCommandList.ReleaseAndGetAddressOf())
);
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
m_pCommandList->Close();
m_fence.Init(dev);
m_rpHeap.reset(
new GlobalDescriptorHeap()
);
return m_rpHeap->Init(dev, _desc);
}
bool CommandList::Init(D3DDevice* dev, D3D12_COMMAND_LIST_TYPE _type) {
auto hr = dev->GetDev()->CreateCommandAllocator(
_type,
IID_PPV_ARGS(m_pAllocator.ReleaseAndGetAddressOf())
);
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
hr = dev->GetDev()->CreateCommandList(
0,
_type,
m_pAllocator.Get(),
nullptr,
IID_PPV_ARGS(m_pCommandList.ReleaseAndGetAddressOf())
);
if (FAILED(hr)) {
assert(SUCCEEDED(hr));
return false;
}
m_pCommandList->Close();
m_fence.Init(dev);
return true;
}
void CommandList::Term() {
m_fence.WaitSignal();
}
void CommandList::Begin() {
m_fence.WaitSignal();
m_pAllocator->Reset();
m_pCommandList->Reset(m_pAllocator.Get(), nullptr);
}
void CommandList::TransitState(IResource const* _resource,
D3D12_RESOURCE_STATES _before,
D3D12_RESOURCE_STATES _after) {
D3D12_RESOURCE_BARRIER barrier = {};
barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
barrier.Flags = D3D12_RESOURCE_BARRIER_FLAG_NONE;
barrier.Transition.pResource = _resource->GetResource();
barrier.Transition.StateBefore = _before;
barrier.Transition.StateAfter = _after;
barrier.Transition.Subresource =
D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
m_pCommandList->ResourceBarrier(1, &barrier);
}
void
CommandList::SetAndClearRenderTargets(
RenderTargetView const* _resViews,
uint32_t _numRenderTargets,
//DsvHandle,
std::initializer_list<float> _clearColor
) {
m_pCommandList->OMSetRenderTargets(
_numRenderTargets,
&_resViews->GetHeapInfo().m_CpuHandle,
FALSE,
nullptr
);
float col[4];
int idx = 0;
for (auto c:_clearColor) {
col[idx] = c;
idx++;
}
if (idx < 4) {
for (int i = idx; i++; i < 4) {
col[i] = 1.0f;
}
}
m_pCommandList->ClearRenderTargetView(
_resViews->GetHeapInfo().m_CpuHandle,
col,
0,
nullptr
);
}
void CommandList::End() {
auto hr = m_pCommandList->Close();
if (FAILED(hr)) {
assert(false);
}
}
void
CommandList::RegisterRootParameterLayout(
uint32_t _rpIdx, RPBufferType _type, size_t numRegisters) {
m_rpHeap->RegisterRPChankInfo(
_rpIdx, _type, numRegisters
);
}
void
CommandList::SetGlobalDescriptorHeap() {
auto const heaps = m_rpHeap->GetHeaps();
m_pCommandList->SetDescriptorHeaps(
static_cast<UINT>(heaps.size()), heaps.data());
for (int i = 0; i < m_rpHeap->GetRPChankNum(); i++) {
m_pCommandList->SetGraphicsRootDescriptorTable(
i, m_rpHeap->GetRPDescriptorHeapChank(i).GpuHandle
);
}
}
void
CommandList::Draw(
RenderComponent * _pRc, D3DDevice* _pDev
) {
//本当なら入る描画処理
m_pCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
auto const& meshView = _pRc->Mesh()->GetView();
auto vbv = meshView.GetVBViews();
m_pCommandList->IASetVertexBuffers(0, static_cast<UINT>(vbv.size()), vbv.data());
if (_pRc->Material(0)->IndicesNum() != 0) {
m_pCommandList->IASetIndexBuffer(meshView.GetIBView());
}
auto index_offset = 0;
for (auto matSetIdx = 0u; matSetIdx < _pRc->NumMaterialSet(); matSetIdx++) {
bool isLayoutStable = true;
//後でindexを使う際はマテリアル毎にメッシュを分けて描画するようにする。
auto const& mat = _pRc->Material(matSetIdx);
auto const matSize = mat->Size();
for (int i = 0; i < matSize; i++) {
auto const& matView = mat->GetView(i);
//rootparameterにコピー.isLayoutStableはglobalheapchankの場所が変わっているかを表すけどいるのか?
isLayoutStable = isLayoutStable && m_rpHeap->CopyToHeapChank(_pDev,
matView.GetCopyDesc(),
i,
matView.GetType()
);
}
//rootparameterのセット
SetGlobalDescriptorHeap();
//要素が一つだけの時はindices num=0でも許す
if (mat->IndicesNum()==0 &&
_pRc->NumMaterialSet() == 1) {
m_pCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
m_pCommandList->DrawInstanced(meshView.NumVertices(), meshView.NumInstances(), 0, 0);
_pRc->AfterDraw(matSetIdx);
continue;
}
assert(mat->IndicesNum() != 0);
m_pCommandList->DrawIndexedInstanced(
mat->IndicesNum(), meshView.NumInstances(), index_offset, 0, 0);
index_offset += mat->IndicesNum();
_pRc->AfterDraw(0);
}
}
void
CommandList::DrawMesh(
RenderComponent* _pRc, D3DDevice* _pDev
) {
bool isLayoutStable = true;
//後でindexを使う際はマテリアル毎にメッシュを分けて描画するようにする。
auto const matSize = _pRc->Material(0)->Size();
for (int i = 0; i < matSize; i++) {
auto const& matView = _pRc->Material(0)->GetView(i);
//rootparameterにコピー.isLayoutStableはglobalheapchankの場所が変わっているかを表すけどいるのか?
isLayoutStable = isLayoutStable && m_rpHeap->CopyToHeapChank(_pDev,
matView.GetCopyDesc(),
i,
matView.GetType()
);
}
//rootparameterのセット
SetGlobalDescriptorHeap();
m_pCommandList->DispatchMesh(1, 1, 1);
_pRc->AfterDraw(0);
}
void CommandList::SetGraphicPipeline(IPipelineStateObject const* _pso) {
m_pCommandList->SetPipelineState(_pso->GetPipelineState());
m_pCommandList->SetGraphicsRootSignature(_pso->GetRootSignature()->GetDxRootSignature());
}
void CommandList::RegisterRootSignature(
RootSignature const* _rs
) {
auto const& rpDesc = _rs->GetRootParamDescs();
for (int i = 0; i < rpDesc.size(); i++) {
size_t numDescs = 0;
RPBufferType type;
for (auto const& range : rpDesc[i].Ranges) {
numDescs += range.NumDescriptors;
switch (range.Type)
{
case D3D12_DESCRIPTOR_RANGE_TYPE_CBV:
case D3D12_DESCRIPTOR_RANGE_TYPE_SRV:
case D3D12_DESCRIPTOR_RANGE_TYPE_UAV:
type = RPBufferType::CBV_SRV_UAV;
break;
case D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER:
type = RPBufferType::SMP;
break;
default:
assert(false);
break;
}
}
m_rpHeap->RegisterRPChankInfo(
i, type, numDescs
);
}
}
}