-
Notifications
You must be signed in to change notification settings - Fork 226
/
SimpleMSAA.cpp
502 lines (391 loc) · 16.2 KB
/
SimpleMSAA.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
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
//--------------------------------------------------------------------------------------
// SimpleMSAA.cpp
//
// This sample demonstrates setting up a MSAA render target for DirectX 12
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleMSAA.h"
#include "ATGColors.h"
#include "ControllerFont.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using namespace DirectX::SimpleMath;
using Microsoft::WRL::ComPtr;
namespace
{
const DXGI_FORMAT c_backBufferFormat = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
const DXGI_FORMAT c_depthBufferFormat = DXGI_FORMAT_D32_FLOAT;
#if _XDK_VER < 0x3F6803F3 /* XDK Edition 170600 */
// This is a workaround for a bug in the validation layer when resolving sRGB formats.
const DXGI_FORMAT c_msaaFormat = DXGI_FORMAT_B8G8R8A8_TYPELESS;
const DXGI_FORMAT c_resolveFormat = DXGI_FORMAT_B8G8R8A8_UNORM;
#else
const DXGI_FORMAT c_msaaFormat = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
const DXGI_FORMAT c_resolveFormat = DXGI_FORMAT_B8G8R8A8_UNORM_SRGB;
#endif
// Xbox One supports 2x, 4x, or 8x MSAA
const unsigned int c_sampleCount = 4;
}
Sample::Sample() :
m_msaa(true),
m_frame(0)
{
unsigned int flags = 0;
#ifdef ENABLE_4K
flags |= DX::DeviceResources::c_Enable4K_UHD;
#endif
m_deviceResources = std::make_unique<DX::DeviceResources>(
c_backBufferFormat,
c_depthBufferFormat, /* If we were only doing MSAA rendering, we could skip the non-MSAA depth/stencil buffer with DXGI_FORMAT_UNKNOWN */
2,
flags);
//
// In Win32 'classic' DirectX 11, you can create the 'swapchain' backbuffer as a multisample buffer. Present took care of the
// resolve as part of the swapchain management. This is not recommended as doing it explictly gives you more control
// as well as the fact that this 'old-school' implicit resolve behavior is not supported for UWP.
//
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(IUnknown* window)
{
m_gamePad = std::make_unique<GamePad>();
m_deviceResources->SetWindow(window);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Frame %llu", m_frame);
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
PIXEndEvent();
m_frame++;
}
// Updates the world.
void Sample::Update(DX::StepTimer const& timer)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
float time = float(timer.GetTotalSeconds());
m_world = Matrix::CreateRotationZ(cosf(time / 4.f));
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
if (m_gamePadButtons.a == GamePad::ButtonStateTracker::PRESSED)
{
m_msaa = !m_msaa;
}
}
else
{
m_gamePadButtons.Reset();
}
PIXEndEvent();
}
#pragma endregion
#pragma region Frame Render
// Draws the scene.
void Sample::Render()
{
// Don't try to render anything before the first Update.
if (m_timer.GetFrameCount() == 0)
{
return;
}
// Prepare the command list to render a new frame.
m_deviceResources->Prepare(m_msaa ? D3D12_RESOURCE_STATE_RENDER_TARGET : D3D12_RESOURCE_STATE_PRESENT);
Clear();
auto commandList = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Render");
// Draw the scene.
ID3D12DescriptorHeap* heaps[] = { m_modelResources->Heap(), m_states->Heap() };
commandList->SetDescriptorHeaps(_countof(heaps), heaps);
// Must use PSOs with MSAA sample counts that match our render target.
if (m_msaa)
{
Model::UpdateEffectMatrices(m_modelMSAA, m_world, m_view, m_proj);
m_model->Draw(commandList, m_modelMSAA.cbegin());
}
else
{
Model::UpdateEffectMatrices(m_modelStandard, m_world, m_view, m_proj);
m_model->Draw(commandList, m_modelStandard.cbegin());
}
PIXEndEvent(commandList);
if (m_msaa)
{
// Resolve the MSAA render target.
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Resolve");
auto backBuffer = m_deviceResources->GetRenderTarget();
{
D3D12_RESOURCE_BARRIER barriers[2] =
{
CD3DX12_RESOURCE_BARRIER::Transition(
m_msaaRenderTarget.Get(),
D3D12_RESOURCE_STATE_RENDER_TARGET,
D3D12_RESOURCE_STATE_RESOLVE_SOURCE),
CD3DX12_RESOURCE_BARRIER::Transition(
backBuffer,
D3D12_RESOURCE_STATE_PRESENT,
D3D12_RESOURCE_STATE_RESOLVE_DEST)
};
commandList->ResourceBarrier(2, barriers);
}
commandList->ResolveSubresource(backBuffer, 0, m_msaaRenderTarget.Get(), 0, c_resolveFormat);
PIXEndEvent(commandList);
// Set render target for UI which is typically rendered without MSAA.
{
D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
backBuffer,
D3D12_RESOURCE_STATE_RESOLVE_DEST,
D3D12_RESOURCE_STATE_RENDER_TARGET);
commandList->ResourceBarrier(1, &barrier);
}
}
// Unbind depth/stencil for UI.
auto rtvDescriptor = m_deviceResources->GetRenderTargetView();
commandList->OMSetRenderTargets(1, &rtvDescriptor, FALSE, nullptr);
// Draw UI.
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Draw UI");
auto size = m_deviceResources->GetOutputSize();
auto safe = SimpleMath::Viewport::ComputeTitleSafeArea(size.right, size.bottom);
heaps[0] = m_resourceDescriptors->Heap();
commandList->SetDescriptorHeaps(1, heaps);
m_batch->Begin(commandList);
wchar_t str[32] = {};
swprintf_s(str, L"Sample count: %u", m_msaa ? c_sampleCount : 1);
m_smallFont->DrawString(m_batch.get(), str, XMFLOAT2(float(safe.left), float(safe.top)), ATG::Colors::White);
DX::DrawControllerString(m_batch.get(),
m_smallFont.get(), m_ctrlFont.get(),
L"[A] Toggle MSAA [View] Exit",
XMFLOAT2(float(safe.left),
float(safe.bottom) - m_smallFont->GetLineSpacing()),
ATG::Colors::LightGrey);
m_batch->End();
PIXEndEvent(commandList);
// Show the new frame.
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit(m_deviceResources->GetCommandQueue());
PIXEndEvent();
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto commandList = m_deviceResources->GetCommandList();
PIXBeginEvent(commandList, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
if (m_msaa)
{
D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(
m_msaaRenderTarget.Get(),
D3D12_RESOURCE_STATE_RESOLVE_SOURCE,
D3D12_RESOURCE_STATE_RENDER_TARGET);
commandList->ResourceBarrier(1, &barrier);
//
// Rather than operate on the swapchain render target, we set up to render the scene to our MSAA resources instead.
//
auto rtvDescriptor = m_msaaRTVDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
auto dsvDescriptor = m_msaaDSVDescriptorHeap->GetCPUDescriptorHandleForHeapStart();
commandList->OMSetRenderTargets(1, &rtvDescriptor, FALSE, &dsvDescriptor);
commandList->ClearRenderTargetView(rtvDescriptor, ATG::ColorsLinear::Background, 0, nullptr);
commandList->ClearDepthStencilView(dsvDescriptor, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
}
else
{
auto rtvDescriptor = m_deviceResources->GetRenderTargetView();
auto dsvDescriptor = m_deviceResources->GetDepthStencilView();
commandList->OMSetRenderTargets(1, &rtvDescriptor, FALSE, &dsvDescriptor);
commandList->ClearRenderTargetView(rtvDescriptor, ATG::ColorsLinear::Background, 0, nullptr);
commandList->ClearDepthStencilView(dsvDescriptor, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
}
// Set the viewport and scissor rect.
auto viewport = m_deviceResources->GetScreenViewport();
auto scissorRect = m_deviceResources->GetScissorRect();
commandList->RSSetViewports(1, &viewport);
commandList->RSSetScissorRects(1, &scissorRect);
PIXEndEvent(commandList);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnSuspending()
{
auto queue = m_deviceResources->GetCommandQueue();
queue->SuspendX(0);
}
void Sample::OnResuming()
{
auto queue = m_deviceResources->GetCommandQueue();
queue->ResumeX();
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto device = m_deviceResources->GetD3DDevice();
m_graphicsMemory = std::make_unique<GraphicsMemory>(device);
// Create descriptor heaps for MSAA render target views and depth stencil views.
D3D12_DESCRIPTOR_HEAP_DESC rtvDescriptorHeapDesc = {};
rtvDescriptorHeapDesc.NumDescriptors = 1;
rtvDescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
DX::ThrowIfFailed(device->CreateDescriptorHeap(&rtvDescriptorHeapDesc,
IID_GRAPHICS_PPV_ARGS(m_msaaRTVDescriptorHeap.ReleaseAndGetAddressOf())));
D3D12_DESCRIPTOR_HEAP_DESC dsvDescriptorHeapDesc = {};
dsvDescriptorHeapDesc.NumDescriptors = 1;
dsvDescriptorHeapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
DX::ThrowIfFailed(device->CreateDescriptorHeap(&dsvDescriptorHeapDesc,
IID_GRAPHICS_PPV_ARGS(m_msaaDSVDescriptorHeap.ReleaseAndGetAddressOf())));
// Setup test scene.
m_resourceDescriptors = std::make_unique<DescriptorHeap>(device, Descriptors::Count);
m_states = std::make_unique<CommonStates>(device);
m_model = Model::CreateFromSDKMESH(device, L"CityBlockConcrete.sdkmesh");
ResourceUploadBatch resourceUpload(device);
resourceUpload.Begin();
m_model->LoadStaticBuffers(device, resourceUpload);
m_modelResources = m_model->LoadTextures(device, resourceUpload);
m_fxFactory = std::make_unique<EffectFactory>(m_modelResources->Heap(), m_states->Heap());
{
RenderTargetState rtStateUI(c_backBufferFormat, DXGI_FORMAT_UNKNOWN);
SpriteBatchPipelineStateDescription pd(rtStateUI);
m_batch = std::make_unique<SpriteBatch>(device, resourceUpload, pd);
}
auto uploadResourcesFinished = resourceUpload.End(m_deviceResources->GetCommandQueue());
uploadResourcesFinished.wait();
{
RenderTargetState rtState(c_backBufferFormat, c_depthBufferFormat);
rtState.sampleDesc.Count = c_sampleCount;
EffectPipelineStateDescription pd(
nullptr,
CommonStates::Opaque,
CommonStates::DepthDefault,
CommonStates::CullClockwise,
rtState);
EffectPipelineStateDescription pdAlpha(
nullptr,
CommonStates::AlphaBlend,
CommonStates::DepthDefault,
CommonStates::CullClockwise,
rtState);
m_modelMSAA = m_model->CreateEffects(*m_fxFactory, pd, pdAlpha);
}
{
RenderTargetState rtState(c_backBufferFormat, c_depthBufferFormat);
EffectPipelineStateDescription pd(
nullptr,
CommonStates::Opaque,
CommonStates::DepthDefault,
CommonStates::CullClockwise,
rtState);
EffectPipelineStateDescription pdAlpha(
nullptr,
CommonStates::AlphaBlend,
CommonStates::DepthDefault,
CommonStates::CullClockwise,
rtState);
m_modelStandard = m_model->CreateEffects(*m_fxFactory, pd, pdAlpha);
}
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
auto output = m_deviceResources->GetOutputSize();
// Determine the render target size in pixels.
UINT backBufferWidth = std::max<UINT>(output.right - output.left, 1);
UINT backBufferHeight = std::max<UINT>(output.bottom - output.top, 1);
CD3DX12_HEAP_PROPERTIES heapProperties(D3D12_HEAP_TYPE_DEFAULT);
// Create an MSAA render target.
D3D12_RESOURCE_DESC msaaRTDesc = CD3DX12_RESOURCE_DESC::Tex2D(
c_msaaFormat,
backBufferWidth,
backBufferHeight,
1, // This render target view has only one texture.
1, // Use a single mipmap level
c_sampleCount
);
msaaRTDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
D3D12_CLEAR_VALUE msaaOptimizedClearValue = {};
msaaOptimizedClearValue.Format = c_backBufferFormat;
memcpy(msaaOptimizedClearValue.Color, ATG::ColorsLinear::Background, sizeof(float) * 4);
auto device = m_deviceResources->GetD3DDevice();
DX::ThrowIfFailed(device->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&msaaRTDesc,
D3D12_RESOURCE_STATE_RESOLVE_SOURCE,
&msaaOptimizedClearValue,
IID_GRAPHICS_PPV_ARGS(m_msaaRenderTarget.ReleaseAndGetAddressOf())
));
m_msaaRenderTarget->SetName(L"MSAA Render Target");
D3D12_RENDER_TARGET_VIEW_DESC rtvDesc = {};
rtvDesc.Format = c_backBufferFormat;
rtvDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
device->CreateRenderTargetView(
m_msaaRenderTarget.Get(), &rtvDesc,
m_msaaRTVDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
// Create an MSAA depth stencil view.
D3D12_RESOURCE_DESC depthStencilDesc = CD3DX12_RESOURCE_DESC::Tex2D(
c_depthBufferFormat,
backBufferWidth,
backBufferHeight,
1, // This depth stencil view has only one texture.
1, // Use a single mipmap level.
c_sampleCount
);
depthStencilDesc.Flags |= D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_CLEAR_VALUE depthOptimizedClearValue = {};
depthOptimizedClearValue.Format = c_depthBufferFormat;
depthOptimizedClearValue.DepthStencil.Depth = 1.0f;
depthOptimizedClearValue.DepthStencil.Stencil = 0;
DX::ThrowIfFailed(device->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&depthStencilDesc,
D3D12_RESOURCE_STATE_DEPTH_WRITE,
&depthOptimizedClearValue,
IID_GRAPHICS_PPV_ARGS(m_msaaDepthStencil.ReleaseAndGetAddressOf())
));
D3D12_DEPTH_STENCIL_VIEW_DESC dsvDesc = {};
dsvDesc.Format = c_depthBufferFormat;
dsvDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
device->CreateDepthStencilView(
m_msaaDepthStencil.Get(), &dsvDesc,
m_msaaDSVDescriptorHeap->GetCPUDescriptorHandleForHeapStart());
// Load UI.
ResourceUploadBatch resourceUpload(device);
resourceUpload.Begin();
m_smallFont = std::make_unique<SpriteFont>(device, resourceUpload,
(output.bottom > 1080) ? L"SegoeUI_36.spritefont" : L"SegoeUI_18.spritefont",
m_resourceDescriptors->GetCpuHandle(Descriptors::UIFont),
m_resourceDescriptors->GetGpuHandle(Descriptors::UIFont));
m_ctrlFont = std::make_unique<SpriteFont>(device, resourceUpload,
(output.bottom > 1080) ? L"XboxOneControllerLegend.spritefont" : L"XboxOneControllerLegendSmall.spritefont",
m_resourceDescriptors->GetCpuHandle(Descriptors::CtrlFont),
m_resourceDescriptors->GetGpuHandle(Descriptors::CtrlFont));
auto uploadResourcesFinished = resourceUpload.End(m_deviceResources->GetCommandQueue());
uploadResourcesFinished.wait();
// Setup test scene.
m_view = Matrix::CreateLookAt(Vector3(0, -211.f, -23.f), Vector3(6.f, 0.f, -37.f), -Vector3::UnitZ);
m_proj = Matrix::CreatePerspectiveFieldOfView(XM_PI / 4.f,
float(backBufferWidth) / float(backBufferHeight), 0.1f, 1000.f);
auto viewport = m_deviceResources->GetScreenViewport();
m_batch->SetViewport(viewport);
}
#pragma endregion