-
Notifications
You must be signed in to change notification settings - Fork 226
/
SimpleComputeUWP.cpp
458 lines (359 loc) · 13.9 KB
/
SimpleComputeUWP.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
//--------------------------------------------------------------------------------------
// SimpleComputeUWP.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleComputeUWP.h"
#include "ATGColors.h"
#include "ControllerFont.h"
#include "ReadData.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using Microsoft::WRL::ComPtr;
namespace
{
const uint32_t s_numShaderThreads = 8; // make sure to update value in shader if this changes
const wchar_t* g_SampleTitle = L"SimpleCompute";
const wchar_t* g_SampleDescription = L"Demonstrates how to use DirectCompute";
const ATG::HelpButtonAssignment g_HelpButtons[] = {
{ ATG::HelpID::MENU_BUTTON, L"Show/Hide Help" },
{ ATG::HelpID::VIEW_BUTTON, L"Exit" },
{ ATG::HelpID::LEFT_STICK, L"Pan Viewport" },
{ ATG::HelpID::RIGHT_STICK, L"Zoom Viewport" },
{ ATG::HelpID::RIGHT_TRIGGER, L"Increase Zoom Speed" },
{ ATG::HelpID::Y_BUTTON, L"Reset Viewport to Default" },
};
}
Sample::Sample() :
m_showHelp(false),
m_gamepadPresent(false)
{
// Renders only 2D, so no need for a depth buffer.
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM, DXGI_FORMAT_UNKNOWN, 2, D3D_FEATURE_LEVEL_11_0);
m_deviceResources->RegisterDeviceNotify(this);
m_help = std::make_unique<ATG::Help>(g_SampleTitle, g_SampleDescription, g_HelpButtons, _countof(g_HelpButtons));
}
// Initialize the Direct3D resources required to run.
void Sample::Initialize(::IUnknown* window, int width, int height, DXGI_MODE_ROTATION rotation)
{
ResetWindow();
m_gamePad = std::make_unique<GamePad>();
m_keyboard = std::make_unique<Keyboard>();
m_keyboard->SetWindow(reinterpret_cast<ABI::Windows::UI::Core::ICoreWindow*>(window));
m_mouse = std::make_unique<Mouse>();
m_mouse->SetWindow(reinterpret_cast<ABI::Windows::UI::Core::ICoreWindow*>(window));
m_deviceResources->SetWindow(window, width, height, rotation);
m_deviceResources->CreateDeviceResources();
CreateDeviceDependentResources();
m_deviceResources->CreateWindowSizeDependentResources();
CreateWindowSizeDependentResources();
}
#pragma region Frame Update
// Executes basic render loop.
void Sample::Tick()
{
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
}
// Updates the world.
void Sample::Update(DX::StepTimer const& timer)
{
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Update");
float elapsedTime = float(timer.GetElapsedSeconds());
m_renderFPS.Tick(elapsedTime);
auto pad = m_gamePad->GetState(0);
m_gamepadPresent = pad.IsConnected();
if (m_gamepadPresent)
{
m_gamePadButtons.Update(pad);
if (m_gamePadButtons.menu == GamePad::ButtonStateTracker::PRESSED)
{
m_showHelp = !m_showHelp;
}
else if (m_showHelp && m_gamePadButtons.b == GamePad::ButtonStateTracker::PRESSED)
{
m_showHelp = false;
}
if (!m_showHelp)
{
if (pad.IsViewPressed())
{
ExitSample();
}
const float ThumbLeftX = pad.thumbSticks.leftX;
const float ThumbLeftY = pad.thumbSticks.leftY;
const float ThumbRightY = pad.thumbSticks.rightY;
const float RightTrigger = m_gamePadButtons.rightTrigger == DirectX::GamePad::ButtonStateTracker::HELD;
if (m_gamePadButtons.y == DirectX::GamePad::ButtonStateTracker::PRESSED)
{
ResetWindow();
}
if (ThumbLeftX != 0.0f || ThumbLeftY != 0.0f || ThumbRightY != 0.0f)
{
const float ScaleSpeed = 1.0f + RightTrigger * 4.0f;
const float WindowScale = 1.0f + ThumbRightY * -0.25f * ScaleSpeed * elapsedTime;
m_window.x *= WindowScale;
m_window.y *= WindowScale;
m_window.z += m_window.x * ThumbLeftX * elapsedTime * 0.5f;
m_window.w += m_window.y * ThumbLeftY * elapsedTime * 0.5f;
m_windowUpdated = true;
}
m_windowUpdated = true;
}
}
else
{
m_gamePadButtons.Reset();
}
auto kb = m_keyboard->GetState();
m_keyboardButtons.Update(kb);
if (m_keyboardButtons.IsKeyPressed(Keyboard::F1))
{
m_showHelp = !m_showHelp;
}
else if (m_showHelp && kb.Escape)
{
m_showHelp = false;
}
else
{
if (m_keyboardButtons.IsKeyPressed(Keyboard::Escape))
{
ExitSample();
}
if (m_keyboardButtons.IsKeyPressed(Keyboard::Home))
{
ResetWindow();
}
if (kb.W || kb.S || kb.A || kb.D || kb.PageUp || kb.PageDown)
{
const float ScaleSpeed = (kb.LeftShift || kb.RightShift) ? 4.f : 1.f;
float zoom = kb.PageDown ? 1.f : (kb.PageUp ? -1.f : 0.f);
float x = kb.D ? 1.f : (kb.A ? -1.f : 0.f);
float y = kb.W ? 1.f : (kb.S ? -1.f : 0.f);
const float WindowScale = 1.0f + zoom * ScaleSpeed * elapsedTime;
m_window.x *= WindowScale;
m_window.y *= WindowScale;
m_window.z += m_window.x * x * elapsedTime * 0.5f;
m_window.w += m_window.y * y * elapsedTime * 0.5f;
m_windowUpdated = true;
}
}
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;
}
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
if (m_showHelp)
{
m_help->Render();
}
else
{
// Flip colors for which async compute buffer is being rendered
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
UpdateFractalData();
context->CSSetConstantBuffers(0, 1, m_cbFractal.GetAddressOf());
context->CSSetShaderResources(0, 1, m_fractalColorMapSRV.GetAddressOf());
context->CSSetSamplers(0, 1, m_fractalBilinearSampler.GetAddressOf());
context->CSSetShader(m_csFractal.Get(), nullptr, 0);
context->CSSetUnorderedAccessViews(0, 1, m_fractalUAV.GetAddressOf(), nullptr);
D3D11_TEXTURE2D_DESC texDesc = {};
m_fractalTexture->GetDesc(&texDesc);
const uint32_t threadGroupX = texDesc.Width / s_numShaderThreads;
const uint32_t threadGroupY = texDesc.Height / s_numShaderThreads;
context->Dispatch(threadGroupX, threadGroupY, 1);
ID3D11UnorderedAccessView* nulluav[] = { nullptr };
context->CSSetUnorderedAccessViews(0, 1, nulluav, nullptr);
RECT outputSize = m_deviceResources->GetOutputSize();
RECT safeRect = SimpleMath::Viewport::ComputeTitleSafeArea(outputSize.right, outputSize.bottom);
XMFLOAT2 pos(float(safeRect.left), float(safeRect.top));
m_spriteBatch->Begin();
m_spriteBatch->Draw(m_fractalSRV.Get(), outputSize);
wchar_t outputString[256] = {};
swprintf_s(outputString, 256, L"Simple Compute Context %0.2f fps", m_renderFPS.GetFPS());
m_font->DrawString(m_spriteBatch.get(), outputString, pos);
pos.y += m_font->GetLineSpacing();
swprintf_s(outputString, 256, L"Synchronous compute %0.2f fps", m_renderFPS.GetFPS());
m_font->DrawString(m_spriteBatch.get(), outputString, pos);
const wchar_t* legend = m_gamepadPresent
? L"[View] Exit [Menu] Help"
: L"WASD: Pan viewport PageUp/Down: Zoom viewport Esc: Exit";
DX::DrawControllerString(m_spriteBatch.get(), m_font.get(), m_ctrlFont.get(),
legend,
XMFLOAT2(float(safeRect.left), float(safeRect.bottom) - m_font->GetLineSpacing()));
m_spriteBatch->End();
PIXEndEvent(context);
}
// Show the new frame.
PIXBeginEvent(PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
PIXEndEvent();
}
// Helper method to clear the back buffers.
void Sample::Clear()
{
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Clear");
// Clear the views.
auto renderTarget = m_deviceResources->GetRenderTargetView();
context->ClearRenderTargetView(renderTarget, ATG::Colors::Background);
context->OMSetRenderTargets(1, &renderTarget, nullptr);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers
void Sample::OnActivated()
{
}
void Sample::OnDeactivated()
{
}
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->ClearState();
m_deviceResources->Trim();
}
void Sample::OnResuming()
{
m_timer.ResetElapsedTime();
m_gamePadButtons.Reset();
m_keyboardButtons.Reset();
}
void Sample::OnWindowSizeChanged(int width, int height, DXGI_MODE_ROTATION rotation)
{
if (!m_deviceResources->WindowSizeChanged(width, height, rotation))
return;
CreateWindowSizeDependentResources();
}
void Sample::ValidateDevice()
{
m_deviceResources->ValidateDevice();
}
// Properties
void Sample::GetDefaultSize(int& width, int& height) const
{
width = 1280;
height = 720;
}
#pragma endregion
#pragma region Direct3D Resources
// These are the resources that depend on the device.
void Sample::CreateDeviceDependentResources()
{
auto device = m_deviceResources->GetD3DDevice();
auto context = m_deviceResources->GetD3DDeviceContext();
auto blob = DX::ReadData(L"Fractal.cso");
DX::ThrowIfFailed(
device->CreateComputeShader(blob.data(), blob.size(), nullptr, m_csFractal.ReleaseAndGetAddressOf()));
RECT outputSize = m_deviceResources->GetOutputSize();
CD3D11_TEXTURE2D_DESC TexDesc(
DXGI_FORMAT_R32G32B32A32_FLOAT,
outputSize.right,
outputSize.bottom,
1,
1,
D3D11_BIND_UNORDERED_ACCESS | D3D11_BIND_SHADER_RESOURCE
);
DX::ThrowIfFailed(
device->CreateTexture2D(&TexDesc, nullptr, m_fractalTexture.ReleaseAndGetAddressOf()));
DX::ThrowIfFailed(
device->CreateShaderResourceView(m_fractalTexture.Get(), nullptr, m_fractalSRV.ReleaseAndGetAddressOf()));
CD3D11_UNORDERED_ACCESS_VIEW_DESC UAVDesc(D3D11_UAV_DIMENSION_TEXTURE2D, TexDesc.Format);
DX::ThrowIfFailed(
device->CreateUnorderedAccessView(m_fractalTexture.Get(), &UAVDesc, m_fractalUAV.ReleaseAndGetAddressOf()));
CD3D11_BUFFER_DESC CBDesc(sizeof(CB_FractalCS), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
DX::ThrowIfFailed(
device->CreateBuffer(&CBDesc, nullptr, m_cbFractal.ReleaseAndGetAddressOf()));
TexDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
TexDesc.Width = 8;
TexDesc.Height = 1;
TexDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
const uint32_t GradientTexels[8] = { 0xFF000040, 0xFF000080, 0xFF0000C0, 0xFF0000FF, 0xFF0040FF, 0xFF0080FF, 0xFF00C0FF, 0xFF00FFFF };
D3D11_SUBRESOURCE_DATA InitData = {};
InitData.SysMemPitch = sizeof(GradientTexels);
InitData.pSysMem = GradientTexels;
DX::ThrowIfFailed(
device->CreateTexture2D(&TexDesc, &InitData, m_fractalColorMap.ReleaseAndGetAddressOf()));
DX::ThrowIfFailed(
device->CreateShaderResourceView(m_fractalColorMap.Get(), nullptr, m_fractalColorMapSRV.ReleaseAndGetAddressOf()));
CD3D11_SAMPLER_DESC SamplerDesc(D3D11_DEFAULT);
SamplerDesc.Filter = D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
DX::ThrowIfFailed(
device->CreateSamplerState(&SamplerDesc, m_fractalBilinearSampler.ReleaseAndGetAddressOf()));
m_fractalMaxIterations = 300;
m_spriteBatch = std::make_unique<SpriteBatch>(context);
m_font = std::make_unique<SpriteFont>(device, L"SegoeUI_18.spritefont");
m_ctrlFont = std::make_unique<SpriteFont>(device, L"XboxOneControllerLegendSmall.spritefont");
m_help->RestoreDevice(context);
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
auto size = m_deviceResources->GetOutputSize();
m_help->SetWindow(size);
m_spriteBatch->SetViewport(m_deviceResources->GetScreenViewport());
}
void Sample::OnDeviceLost()
{
m_cbFractal.Reset();
m_csFractal.Reset();
m_fractalTexture.Reset();
m_fractalUAV.Reset();
m_fractalSRV.Reset();
m_fractalColorMap.Reset();
m_fractalColorMapSRV.Reset();
m_fractalBilinearSampler.Reset();
m_spriteBatch.reset();
m_font.reset();
m_ctrlFont.reset();
m_help->ReleaseDevice();
}
void Sample::OnDeviceRestored()
{
CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
}
#pragma endregion
void Sample::ResetWindow()
{
m_window = XMFLOAT4(4.0f, 2.25f, -0.65f, 0.0f);
m_windowUpdated = true;
}
//--------------------------------------------------------------------------------------
// Name: UpdateFractalData
// Desc: Updates the dynamic constant buffer with fractal data
//--------------------------------------------------------------------------------------
void Sample::UpdateFractalData()
{
D3D11_TEXTURE2D_DESC texDesc = {};
m_fractalTexture->GetDesc(&texDesc);
auto context = m_deviceResources->GetD3DDeviceContext();
D3D11_MAPPED_SUBRESOURCE mapped;
DX::ThrowIfFailed(
context->Map(m_cbFractal.Get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)
);
auto pCBFractalData = reinterpret_cast<CB_FractalCS*> (mapped.pData);
pCBFractalData->MaxThreadIter = XMFLOAT4(static_cast<float>(texDesc.Width), static_cast<float>(texDesc.Height), static_cast<float>(m_fractalMaxIterations), 0);
pCBFractalData->Window = m_window;
context->Unmap(m_cbFractal.Get(), 0);
}