-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDX12Context.cpp
More file actions
351 lines (325 loc) · 16.4 KB
/
DX12Context.cpp
File metadata and controls
351 lines (325 loc) · 16.4 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include "DX12PCH.h"
#ifdef DEVELOPER_VERSION
#define USE_PIX
#include "pix3.h"
extern bool GDebugRender;
#endif
bsize ContextCounter = 0;
DX12Context::DX12Context()
{
{
D3D12_COMMAND_QUEUE_DESC QueueDesc = {};
QueueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
QueueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
R_CHK(Factory->Device->CreateCommandQueue(&QueueDesc, IID_PPV_ARGS(&m_CommandQueue)));
}
R_CHK(Factory->Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_CommandAllocator)));
R_CHK(Factory->Device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_CommandAllocator.Get(), nullptr, IID_PPV_ARGS(&m_CommandList)));
m_CommandList->Close();
R_CHK(Factory->Device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_Fence)));
m_FenceValue = 1;
m_FenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (m_FenceEvent == nullptr)
{
R_CHK(HRESULT_FROM_WIN32(GetLastError()));
}
m_CurrentPipelineIsCompute = false;
ContextCounter++;
}
DX12Context::~DX12Context()
{
ContextCounter--;
if (m_FenceValue)
if (m_Fence->GetCompletedValue() < m_FenceValue - 1)
{
R_CHK(m_Fence->SetEventOnCompletion(m_FenceValue - 1, m_FenceEvent));
WaitForSingleObject(m_FenceEvent, INFINITE);
}
CloseHandle(m_FenceEvent);
}
void DX12Context::BeginEvent(char const* name, BearColor color)
{
#ifdef DEVELOPER_VERSION
if (GDebugRender)
{
PIXBeginEvent(m_CommandList.Get(), PIX_COLOR(color.R8U, color.G8U, color.B8U), name);
}
#endif
}
void DX12Context::EndEvent()
{
#ifdef DEVELOPER_VERSION
if (GDebugRender)
{
PIXEndEvent(m_CommandList.Get());
}
#endif
}
void DX12Context::Reset()
{
m_ScissorRect.left = 0;
m_ScissorRect.right = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
m_ScissorRect.top = 0;
m_ScissorRect.bottom = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
m_CommandAllocator->Reset();
m_CommandList->Reset(m_CommandAllocator.Get(), nullptr);
}
void DX12Context::Wait()
{
if (m_Fence->GetCompletedValue() < m_FenceValue - 1)
{
R_CHK(m_Fence->SetEventOnCompletion(m_FenceValue - 1, m_FenceEvent));
WaitForSingleObject(m_FenceEvent, INFINITE);
}
}
void DX12Context::Flush(BearFactoryPointer<BearRHI::BearRHIViewport> viewport, bool wait)
{
m_CommandList->Close();
ID3D12CommandList* ppm_commandLists[] = { m_CommandList.Get() };
auto Viewport = static_cast<DX12Viewport*>(viewport.get());
Viewport->CommandQueue->ExecuteCommandLists(_countof(ppm_commandLists), ppm_commandLists);
Viewport->Swap();
Viewport->CommandQueue->Signal(m_Fence.Get(), m_FenceValue++);
if (wait)
Wait();
}
void DX12Context::Flush(bool wait)
{
m_CommandList->Close();
ID3D12CommandList* PPMCommandLists[] = { m_CommandList.Get() };
m_CommandQueue->ExecuteCommandLists(_countof(PPMCommandLists), PPMCommandLists);
R_CHK(m_CommandQueue->Signal(m_Fence.Get(), m_FenceValue++));
if (wait)
Wait();
}
void DX12Context::ClearFrameBuffer()
{
//m_CommandList->ClearState();
}
void DX12Context::SetPipeline(BearFactoryPointer<BearRHI::BearRHIPipeline> pipeline)
{
DX12Pipeline* Pipeline = reinterpret_cast<DX12Pipeline*>(pipeline.get()->QueryInterface(DX12Q_Pipeline));
BEAR_CHECK(Pipeline);
Pipeline->Set(m_CommandList.Get());
m_CurrentPipelineIsCompute = Pipeline->IsComputePipeline();
}
void DX12Context::SetViewportAsFrameBuffer(BearFactoryPointer<BearRHI::BearRHIViewport> viewport)
{
BEAR_CHECK(!viewport.empty());
auto Handle = static_cast<DX12Viewport*>(viewport.get())->GetHandle();
if (static_cast<DX12Viewport*>(viewport.get())->Description.Clear)
{
D3D12_RECT Rect = { 0,0,static_cast<LONG>(static_cast<DX12Viewport*>(viewport.get())->Width),static_cast<LONG>(static_cast<DX12Viewport*>(viewport.get())->Height) };
m_CommandList->ClearRenderTargetView(Handle, static_cast<DX12Viewport*>(viewport.get())->Description.ClearColor.R32G32B32A32, 1, &Rect);
}
m_CommandList->OMSetRenderTargets(1, &Handle, FALSE, nullptr);
}
void DX12Context::SetFrameBuffer(BearFactoryPointer<BearRHI::BearRHIFrameBuffer> frame_buffer)
{
BEAR_CHECK(!frame_buffer.empty());
auto FrameBuffer = static_cast<DX12FrameBuffer*>(frame_buffer.get());
D3D12_RECT Rect = { 0,0,static_cast<LONG>(FrameBuffer->Width),static_cast<LONG>(FrameBuffer->Height) };
for (bsize i = 0; i < FrameBuffer->Count; i++)
{
if (FrameBuffer->RenderPassRef->Description.RenderTargets[i].Clear)
m_CommandList->ClearRenderTargetView(FrameBuffer->RenderTargetRefs[i], FrameBuffer->RenderPassRef->Description.RenderTargets[i].Color.R32G32B32A32, 1, &Rect);
}
if (!FrameBuffer->Description.DepthStencil.empty() && FrameBuffer->RenderPassRef->Description.DepthStencil.Clear)
{
D3D12_CLEAR_FLAGS flags = D3D12_CLEAR_FLAG_DEPTH;
switch (FrameBuffer->RenderPassRef->Description.DepthStencil.Format)
{
case BearDepthStencilFormat::Depth24Stencil8:
case BearDepthStencilFormat::Depth32FStencil8:
flags |= D3D12_CLEAR_FLAG_STENCIL;
default:
break;
}
m_CommandList->ClearDepthStencilView(FrameBuffer->DepthStencilRef, flags, FrameBuffer->RenderPassRef->Description.DepthStencil.Depth, FrameBuffer->RenderPassRef->Description.DepthStencil.Stencil, 1, &Rect);
}
m_CommandList->OMSetRenderTargets(static_cast<UINT>(FrameBuffer->Count), FrameBuffer->RenderTargetRefs, FALSE, FrameBuffer->Description.DepthStencil.empty() ? nullptr : &FrameBuffer->DepthStencilRef);
}
void DX12Context::SetDescriptorHeap(BearFactoryPointer<BearRHI::BearRHIDescriptorHeap> descriptor_heap)
{
if (m_CurrentPipelineIsCompute)
{
static_cast<DX12DescriptorHeap*>(descriptor_heap.get())->SetCompute(m_CommandList.Get());
}
else
{
static_cast<DX12DescriptorHeap*>(descriptor_heap.get())->SetGraphics(m_CommandList.Get());// DescriptorHeap
}
}
void DX12Context::SetVertexBuffer(BearFactoryPointer<BearRHI::BearRHIVertexBuffer> buffer)
{
m_CommandList->IASetVertexBuffers(0, 1, &static_cast<DX12VertexBuffer*>(buffer.get())->VertexBufferView);
}
void DX12Context::SetIndexBuffer(BearFactoryPointer<BearRHI::BearRHIIndexBuffer> buffer)
{
m_CommandList->IASetIndexBuffer(&static_cast<DX12IndexBuffer*>(buffer.get())->IndexBufferView);
}
void DX12Context::SetViewport(float x, float y, float width, float height, float min_depth, float max_depth)
{
D3D12_VIEWPORT rect;
rect.TopLeftX = x;
rect.TopLeftY = y;
rect.Width = width;
rect.Height = height;
rect.MinDepth = min_depth;
rect.MaxDepth = max_depth;
m_CommandList->RSSetViewports(1, &rect);
}
void DX12Context::SetScissor(bool enable, float x, float y, float x1, float y1)
{
if (enable)
{
m_ScissorRect.left = static_cast<LONG>(x);
m_ScissorRect.right = static_cast<LONG>(x1);
m_ScissorRect.top = static_cast<LONG>(y);
m_ScissorRect.bottom = static_cast<LONG>(y1);
}
else
{
m_ScissorRect.left = 0;
m_ScissorRect.right = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
m_ScissorRect.top = 0;
m_ScissorRect.bottom = D3D12_REQ_TEXTURE2D_U_OR_V_DIMENSION;
}
m_CommandList->RSSetScissorRects(1, &m_ScissorRect);
}
void DX12Context::SetStencilRef(uint32 ref)
{
m_CommandList->OMSetStencilRef(ref);
}
void DX12Context::Draw(bsize count, bsize offset)
{
if (m_ScissorRect.left - m_ScissorRect.right == 0)return;
if (m_ScissorRect.top - m_ScissorRect.bottom == 0)return;
m_CommandList->DrawInstanced(static_cast<UINT>(count), 1, static_cast<UINT>(offset), 0);
}
void DX12Context::DrawIndex(bsize count, bsize offset_index, bsize offset_vertex)
{
if (m_ScissorRect.left - m_ScissorRect.right == 0)return;
if (m_ScissorRect.top - m_ScissorRect.bottom == 0)return;
m_CommandList->DrawIndexedInstanced(static_cast<UINT>(count), 1, static_cast<UINT>(offset_index), static_cast<UINT>(offset_vertex), 0);
}
void DX12Context::DispatchRays(bsize count_x, bsize count_y, bsize count_z, BearFactoryPointer<BearRHI::BearRHIRayTracingShaderTable> shader_table)
{
#ifdef RTX
D3D12_DISPATCH_RAYS_DESC Desc = {};
auto* ShaderTable = static_cast<DX12RayTracingShaderTable*>(shader_table.get());
Desc.RayGenerationShaderRecord = ShaderTable->RayGenerationShaderRecord;
Desc.MissShaderTable = ShaderTable->MissShaderTable;
Desc.HitGroupTable = ShaderTable->HitGroupTable;
Desc.CallableShaderTable = ShaderTable->CallableShaderTable;
Desc.Width = static_cast<UINT>(count_x);
Desc.Height = static_cast<UINT>(count_y);
Desc.Depth = static_cast<UINT>(count_z);
m_CommandList->DispatchRays(&Desc);
#endif
}
void DX12Context::DispatchMesh(bsize count_x, bsize count_y, bsize count_z)
{
#ifdef MESH_SHADING
m_CommandList->DispatchMesh(static_cast<UINT>(count_x), static_cast<UINT>(count_y), static_cast<UINT>(count_z));
#endif
}
void DX12Context::Lock(BearFactoryPointer<BearRHI::BearRHIViewport> viewport)
{
BEAR_CHECK(!viewport.empty());
static_cast<DX12Viewport*>(viewport.get())->Lock(m_CommandList.Get());
}
void DX12Context::Unlock(BearFactoryPointer<BearRHI::BearRHIViewport> viewport)
{
BEAR_CHECK(!viewport.empty());
static_cast<DX12Viewport*>(viewport.get())->Unlock(m_CommandList.Get());
}
void DX12Context::Lock(BearFactoryPointer<BearRHI::BearRHIFrameBuffer> frame_buffer)
{
BEAR_CHECK(!frame_buffer.empty());
static_cast<DX12FrameBuffer*>(frame_buffer.get())->Lock(m_CommandList.Get());
}
void DX12Context::Unlock(BearFactoryPointer<BearRHI::BearRHIFrameBuffer> frame_buffer)
{
BEAR_CHECK(!frame_buffer.empty());
static_cast<DX12FrameBuffer*>(frame_buffer.get())->Unlock(m_CommandList.Get());
}
void DX12Context::Copy(BearFactoryPointer<BearRHI::BearRHIIndexBuffer> dest, BearFactoryPointer<BearRHI::BearRHIIndexBuffer> source)
{
if (dest.empty() || source.empty())return;
if (static_cast<DX12IndexBuffer*>(dest.get())->IndexBuffer.Get() == nullptr)return;
if (static_cast<DX12IndexBuffer*>(source.get())->IndexBuffer.Get() == nullptr)return;
auto var1 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12IndexBuffer*>(dest.get())->IndexBuffer.Get(), D3D12_RESOURCE_STATE_INDEX_BUFFER, D3D12_RESOURCE_STATE_COPY_DEST);
m_CommandList->ResourceBarrier(1, &var1);
m_CommandList->CopyBufferRegion(static_cast<DX12IndexBuffer*>(dest.get())->IndexBuffer.Get(), 0, static_cast<DX12IndexBuffer*>(source.get())->IndexBuffer.Get(), 0, static_cast<DX12IndexBuffer*>(dest.get())->IndexBufferView.SizeInBytes);
auto var2 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12IndexBuffer*>(dest.get())->IndexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_INDEX_BUFFER);
m_CommandList->ResourceBarrier(1, &var2);
}
void DX12Context::Copy(BearFactoryPointer<BearRHI::BearRHITexture2D> dest, BearFactoryPointer<BearRHI::BearRHITexture2D> source)
{
{
if (dest.empty() || source.empty())return;
if (static_cast<DX12Texture2D*>(dest.get())->TextureBuffer.Get() == nullptr)return;
if (static_cast<DX12Texture2D*>(source.get())->TextureBuffer.Get() == nullptr)return;
auto dst = static_cast<DX12Texture2D*>(dest.get());
auto src = static_cast<DX12Texture2D*>(source.get());
if (src->TextureDesc.Width!= dst->TextureDesc.Width)return;
if (src->TextureDesc.Height != dst->TextureDesc.Height)return;;
if (src->TextureDesc.DepthOrArraySize != dst->TextureDesc.DepthOrArraySize)return;;
if (dst->TextureDesc.MipLevels!= 1)
if (src->TextureDesc.MipLevels != dst->TextureDesc.MipLevels)return;
auto var1 = CD3DX12_RESOURCE_BARRIER::Transition(dst->TextureBuffer.Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_DEST);
auto var2 = CD3DX12_RESOURCE_BARRIER::Transition(src->TextureBuffer.Get(), D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE, D3D12_RESOURCE_STATE_COPY_SOURCE);
auto var3 = CD3DX12_RESOURCE_BARRIER::Transition(dst->TextureBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
auto var4 = CD3DX12_RESOURCE_BARRIER::Transition(src->TextureBuffer.Get(), D3D12_RESOURCE_STATE_COPY_SOURCE, D3D12_RESOURCE_STATE_PIXEL_SHADER_RESOURCE | D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
m_CommandList->ResourceBarrier(1, &var1);
m_CommandList->ResourceBarrier(1, &var2);
CD3DX12_TEXTURE_COPY_LOCATION DSTBuffer[512];
CD3DX12_TEXTURE_COPY_LOCATION SRCBuffer[512];
for (bsize x = 0; x < dst->TextureDesc.DepthOrArraySize; x++)
for (bsize y = 0; y < dst->TextureDesc.MipLevels; y++)
{
CD3DX12_TEXTURE_COPY_LOCATION DST(dst->TextureBuffer.Get(), D3D12CalcSubresource(static_cast<UINT>(y), static_cast<UINT>(x), 0, dst->TextureDesc.MipLevels, dst->TextureDesc.DepthOrArraySize));
CD3DX12_TEXTURE_COPY_LOCATION SRC(src->TextureBuffer.Get(), D3D12CalcSubresource(static_cast<UINT>(y), static_cast<UINT>(x), 0, src->TextureDesc.MipLevels, src->TextureDesc.DepthOrArraySize));
DSTBuffer[x * dst->TextureDesc.MipLevels + y] = DST;
SRCBuffer[x * dst->TextureDesc.MipLevels + y] = SRC;
m_CommandList->CopyTextureRegion(&DSTBuffer[x * dst->TextureDesc.MipLevels + y], 0, 0, 0, &SRCBuffer[x * dst->TextureDesc.MipLevels + y], 0);
}
m_CommandList->ResourceBarrier(1, &var3);
m_CommandList->ResourceBarrier(1, &var4);
}
}
void DX12Context::Copy(BearFactoryPointer<BearRHI::BearRHIVertexBuffer> dest, BearFactoryPointer<BearRHI::BearRHIVertexBuffer> source)
{
if (dest.empty() || source.empty())return;
if (static_cast<DX12VertexBuffer*>(dest.get())->VertexBuffer.Get() == nullptr)return;
if (static_cast<DX12VertexBuffer*>(source.get())->VertexBuffer.Get() == nullptr)return;
auto var1 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12VertexBuffer*>(dest.get())->VertexBuffer.Get(), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, D3D12_RESOURCE_STATE_COPY_DEST);
m_CommandList->ResourceBarrier(1, &var1);
m_CommandList->CopyBufferRegion(static_cast<DX12VertexBuffer*>(dest.get())->VertexBuffer.Get(), 0, static_cast<DX12VertexBuffer*>(source.get())->VertexBuffer.Get(), 0, static_cast<DX12VertexBuffer*>(dest.get())->VertexBufferView.SizeInBytes);
auto var2 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12VertexBuffer*>(dest.get())->VertexBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
m_CommandList->ResourceBarrier(1, &var2);
}
void DX12Context::Copy(BearFactoryPointer<BearRHI::BearRHIUniformBuffer> dest, BearFactoryPointer<BearRHI::BearRHIUniformBuffer> source)
{
if (dest.empty() || source.empty())return;
if (static_cast<DX12UniformBuffer*>(dest.get())->UniformBuffer.Get() == nullptr)return;
if (static_cast<DX12UniformBuffer*>(source.get())->UniformBuffer.Get() == nullptr)return;
auto var1 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12UniformBuffer*>(dest.get())->UniformBuffer.Get(), D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER, D3D12_RESOURCE_STATE_COPY_DEST);
m_CommandList->ResourceBarrier(1, &var1);
m_CommandList->CopyBufferRegion(static_cast<DX12UniformBuffer*>(dest.get())->UniformBuffer.Get(), 0, static_cast<DX12UniformBuffer*>(source.get())->UniformBuffer.Get(), 0, static_cast<DX12UniformBuffer*>(dest.get())->GetCount()* static_cast<DX12UniformBuffer*>(dest.get())->GetStride());
auto var2 = CD3DX12_RESOURCE_BARRIER::Transition(static_cast<DX12UniformBuffer*>(dest.get())->UniformBuffer.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
m_CommandList->ResourceBarrier(1, &var2);
}
void DX12Context::Lock(BearFactoryPointer<BearRHI::BearRHIUnorderedAccess> unordered_access)
{
BEAR_CHECK(!unordered_access.empty());
DX12UnorderedAccess* UnorderedAccess = reinterpret_cast<DX12UnorderedAccess*>(unordered_access.get()->QueryInterface(DX12Q_UnorderedAccess));
UnorderedAccess->LockUAV(m_CommandList.Get());
}
void DX12Context::Unlock(BearFactoryPointer<BearRHI::BearRHIUnorderedAccess> unordered_access)
{
BEAR_CHECK(!unordered_access.empty());
DX12UnorderedAccess* UnorderedAccess = reinterpret_cast<DX12UnorderedAccess*>(unordered_access.get()->QueryInterface(DX12Q_UnorderedAccess));
UnorderedAccess->UnlockUAV(m_CommandList.Get());
}