-
Notifications
You must be signed in to change notification settings - Fork 226
/
SimpleInstancing.cpp
588 lines (477 loc) · 21.4 KB
/
SimpleInstancing.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//--------------------------------------------------------------------------------------
// SimpleInstancing.cpp
//
// Advanced Technology Group (ATG)
// Copyright (C) Microsoft Corporation. All rights reserved.
//--------------------------------------------------------------------------------------
#include "pch.h"
#include "SimpleInstancing.h"
#include "ATGColors.h"
#include "ControllerFont.h"
#include "ReadData.h"
extern void ExitSample() noexcept;
using namespace DirectX;
using namespace DirectX::PackedVector;
using Microsoft::WRL::ComPtr;
namespace
{
//--------------------------------------------------------------------------------------
// Constants
//--------------------------------------------------------------------------------------
const uint32_t c_maxInstances = 20000;
const uint32_t c_startInstanceCount = 5000;
const uint32_t c_minInstanceCount = 1000;
const float c_boxBounds = 60.0f;
const size_t c_cubeIndexCount = 36;
const float c_velocityMultiplier = 500.0f;
//--------------------------------------------------------------------------------------
// Cube vertex definition
//--------------------------------------------------------------------------------------
struct Vertex
{
XMFLOAT3 pos;
XMFLOAT3 norm;
};
}
Sample::Sample() :
m_frame(0),
m_usedInstanceCount(c_startInstanceCount),
m_lights{},
m_pitch(0.0f),
m_yaw(0.0f)
{
XMStoreFloat4x4(&m_proj, XMMatrixIdentity());
// Use gamma-correct rendering.
m_deviceResources = std::make_unique<DX::DeviceResources>(DXGI_FORMAT_B8G8R8A8_UNORM_SRGB, DXGI_FORMAT_D32_FLOAT, 2,
DX::DeviceResources::c_Enable4K_UHD);
}
// 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 elapsedTime = float(timer.GetElapsedSeconds());
auto pad = m_gamePad->GetState(0);
if (pad.IsConnected())
{
m_gamePadButtons.Update(pad);
if (pad.IsViewPressed())
{
ExitSample();
}
if (m_gamePadButtons.a == GamePad::ButtonStateTracker::ButtonState::PRESSED)
{
ResetSimulation();
}
if (m_gamePadButtons.rightShoulder == GamePad::ButtonStateTracker::ButtonState::PRESSED)
{
m_usedInstanceCount = std::min(c_maxInstances, m_usedInstanceCount + 1000);
}
else if (m_gamePadButtons.leftShoulder == GamePad::ButtonStateTracker::ButtonState::PRESSED)
{
m_usedInstanceCount = std::max(c_minInstanceCount, m_usedInstanceCount - 1000);
}
if (pad.IsLeftStickPressed())
{
m_yaw = m_pitch = 0.f;
}
else
{
m_yaw += pad.thumbSticks.leftX * 0.1f;
m_pitch += pad.thumbSticks.leftY * 0.1f;
}
}
else
{
m_gamePadButtons.Reset();
}
// Limit to avoid looking directly up or down
const float limit = XM_PI / 2.0f - 0.01f;
m_pitch = std::max(-limit, std::min(+limit, m_pitch));
if (m_yaw > XM_PI)
{
m_yaw -= XM_PI * 2.f;
}
else if (m_yaw < -XM_PI)
{
m_yaw += XM_PI * 2.f;
}
XMVECTOR lookAt = XMVectorSet(
sinf(m_yaw),
m_pitch,
cosf(m_yaw),
0);
// Update transforms and constant buffer.
XMMATRIX camera = XMMatrixLookAtLH(g_XMZero, lookAt, g_XMIdentityR1);
XMMATRIX proj = XMLoadFloat4x4(&m_proj);
XMMATRIX clip = XMMatrixTranspose(XMMatrixMultiply(camera, proj));
ReplaceBufferContents(m_vertexConstants.Get(), sizeof(XMMATRIX), &clip);
// Overwrite our current instance vertex buffer with new data.
ReplaceBufferContents(m_instanceData.Get(), sizeof(Instance) * m_usedInstanceCount, m_CPUInstanceData.get());
// Update instance data for the next frame.
for (size_t i = 1; i < m_usedInstanceCount; ++i)
{
// Update positions...
float velocityMultiplier = i <= c_pointLightCount ? 5.0f * c_velocityMultiplier : c_velocityMultiplier;
XMVECTOR position = XMLoadFloat4(&m_CPUInstanceData[i].positionAndScale);
position += m_velocities[i] * elapsedTime * velocityMultiplier;
XMStoreFloat4(&m_CPUInstanceData[i].positionAndScale, position);
float X = m_CPUInstanceData[i].positionAndScale.x;
float Y = m_CPUInstanceData[i].positionAndScale.y;
float Z = m_CPUInstanceData[i].positionAndScale.z;
bool bounce = false;
// If an instance pops out of bounds in any dimension, reverse velocity in that dimension...
if (X < -c_boxBounds || X > c_boxBounds)
{
m_velocities[i] *= XMVectorSet(-1.0f, 1.0f, 1.0f, 1.0f);
bounce = true;
}
if (Y < -c_boxBounds || Y > c_boxBounds)
{
m_velocities[i] *= XMVectorSet(1.0f, -1.0f, 1.0f, 1.0f);
bounce = true;
}
if (Z < -c_boxBounds || Z > c_boxBounds)
{
m_velocities[i] *= XMVectorSet(1.0f, 1.0f, -1.0f, 1.0f);
bounce = true;
}
// Apply bounce here.
if (bounce)
{
position = XMLoadFloat4(&m_CPUInstanceData[i].positionAndScale);
position += m_velocities[i] * elapsedTime * c_velocityMultiplier;
XMStoreFloat4(&m_CPUInstanceData[i].positionAndScale, position);
}
// Set up constant buffer with point light info.
if (i <= c_pointLightCount)
{
m_lights.pointPositions[i - 1] = m_CPUInstanceData[i].positionAndScale;
}
XMVECTOR q = XMLoadFloat4(&m_CPUInstanceData[i].quaternion);
q = XMQuaternionNormalizeEst(XMQuaternionMultiply(m_rotationQuaternions[i], q));
XMStoreFloat4(&m_CPUInstanceData[i].quaternion, q);
}
// Update the D3D11 constant buffer with the new lighting constant data.
ReplaceBufferContents(m_pixelConstants.Get(), sizeof(Lights), &m_lights);
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 render target to render a new frame.
m_deviceResources->Prepare();
Clear();
auto context = m_deviceResources->GetD3DDeviceContext();
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Render");
// Use the default blend
context->OMSetBlendState(nullptr, nullptr, D3D11_DEFAULT_SAMPLE_MASK);
// Set input assembler state.
context->IASetInputLayout(m_inputLayout.Get());
// We're rendering a triangle list.
context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
// Set up the vertex buffers. We have 3 streams:
// Stream 1 contains per-primitive vertices defining the cubes.
// Stream 2 contains the per-instance data for scale, position and orientation
// Stream 3 contains the per-instance data for color.
UINT Strides[] = { sizeof(Vertex), sizeof(Instance), sizeof(uint32_t) };
UINT Offsets[] = { 0, 0, 0 };
ID3D11Buffer* Buffers[] = { m_vertexBuffer.Get(), m_instanceData.Get(), m_boxColors.Get() };
context->IASetVertexBuffers(0, _countof(Strides), Buffers, Strides, Offsets);
// The per-instance data is referenced by index...
context->IASetIndexBuffer(m_indexBuffer.Get(), DXGI_FORMAT_R16_UINT, 0);
// Apply the constants for the vertex and pixel shaders.
context->VSSetConstantBuffers(0, 1, m_vertexConstants.GetAddressOf());
context->PSSetConstantBuffers(0, 1, m_pixelConstants.GetAddressOf());
// Set shaders.
context->VSSetShader(m_vertexShader.Get(), nullptr, 0);
context->PSSetShader(m_pixelShader.Get(), nullptr, 0);
// Draw the entire scene...
context->DrawIndexedInstanced(c_cubeIndexCount, m_usedInstanceCount, 0, 0, 0);
// Draw UI
auto size = m_deviceResources->GetOutputSize();
auto safe = SimpleMath::Viewport::ComputeTitleSafeArea(size.right, size.bottom);
m_batch->Begin();
wchar_t str[32] = {};
swprintf_s(str, L"Instancing count: %u", m_usedInstanceCount);
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"[LThumb] Rotate [A] Reset [LB]/[RB] Change instance count [View] Exit",
XMFLOAT2(float(safe.left),
float(safe.bottom) - m_smallFont->GetLineSpacing()),
ATG::Colors::LightGrey);
m_batch->End();
PIXEndEvent(context);
// Show the new frame.
PIXBeginEvent(context, PIX_COLOR_DEFAULT, L"Present");
m_deviceResources->Present();
m_graphicsMemory->Commit();
PIXEndEvent(context);
}
// 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();
auto depthStencil = m_deviceResources->GetDepthStencilView();
// Use linear clear color for gamma-correct rendering
context->ClearRenderTargetView(renderTarget, ATG::ColorsLinear::Background);
context->ClearDepthStencilView(depthStencil, D3D11_CLEAR_DEPTH | D3D11_CLEAR_STENCIL, 1.0f, 0);
context->OMSetRenderTargets(1, &renderTarget, depthStencil);
// Set the viewport.
auto viewport = m_deviceResources->GetScreenViewport();
context->RSSetViewports(1, &viewport);
PIXEndEvent(context);
}
#pragma endregion
#pragma region Message Handlers
// Message handlers.
void Sample::OnSuspending()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Suspend(0);
}
void Sample::OnResuming()
{
auto context = m_deviceResources->GetD3DDeviceContext();
context->Resume();
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, m_deviceResources->GetBackBufferCount());
auto context = m_deviceResources->GetD3DDeviceContext();
m_batch = std::make_unique<SpriteBatch>(context);
// Create input layout (must match declaration of Vertex).
static const D3D11_INPUT_ELEMENT_DESC inputElementDesc[] =
{
// SemanticName SemanticIndex Format InputSlot AlignedByteOffset InputSlotClass InstancedDataStepRate
{ "POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // Vertex local position
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_VERTEX_DATA, 0 }, // Vertex normal
{ "I_ROTATION", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, 0, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // Instance rotation quaternion
{ "I_POSSCALE", 0, DXGI_FORMAT_R32G32B32A32_FLOAT, 1, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // Instance position and scale (scale in "w")
{ "I_COLOR", 0, DXGI_FORMAT_R8G8B8A8_UNORM, 2, D3D11_APPEND_ALIGNED_ELEMENT, D3D11_INPUT_PER_INSTANCE_DATA, 1 }, // Instance color
};
// Load and create shaders.
{
auto shaderBytecode = DX::ReadData(L"VertexShader.cso");
DX::ThrowIfFailed(
device->CreateVertexShader(shaderBytecode.data(), shaderBytecode.size(), nullptr, m_vertexShader.ReleaseAndGetAddressOf())
);
DX::ThrowIfFailed(
device->CreateInputLayout(inputElementDesc, _countof(inputElementDesc), shaderBytecode.data(), shaderBytecode.size(), m_inputLayout.ReleaseAndGetAddressOf())
);
}
{
auto shaderBytecode = DX::ReadData(L"PixelShader.cso");
DX::ThrowIfFailed(
device->CreatePixelShader(shaderBytecode.data(), shaderBytecode.size(), nullptr, m_pixelShader.ReleaseAndGetAddressOf())
);
}
// Create and initialize the vertex buffer defining a cube.
{
static const Vertex vertices[] =
{
{ XMFLOAT3(-1, -1, -1), XMFLOAT3(0, 0, -1) },
{ XMFLOAT3( 1, -1, -1), XMFLOAT3(0, 0, -1) },
{ XMFLOAT3( 1, 1, -1), XMFLOAT3(0, 0, -1) },
{ XMFLOAT3(-1, 1, -1), XMFLOAT3(0, 0, -1) }, // Z negative face
{ XMFLOAT3( 1, -1, 1), XMFLOAT3(0, 0, 1) },
{ XMFLOAT3(-1, -1, 1), XMFLOAT3(0, 0, 1) },
{ XMFLOAT3(-1, 1, 1), XMFLOAT3(0, 0, 1) },
{ XMFLOAT3( 1, 1, 1), XMFLOAT3(0, 0, 1) }, // Z Positive face
{ XMFLOAT3(-1, -1, -1), XMFLOAT3(-1, 0, 0) },
{ XMFLOAT3(-1, 1, -1), XMFLOAT3(-1, 0, 0) },
{ XMFLOAT3(-1, 1, 1), XMFLOAT3(-1, 0, 0) },
{ XMFLOAT3(-1, -1, 1), XMFLOAT3(-1, 0, 0) }, // X negative face
{ XMFLOAT3( 1, 1, -1), XMFLOAT3( 1, 0, 0) },
{ XMFLOAT3( 1, -1, -1), XMFLOAT3( 1, 0, 0) },
{ XMFLOAT3( 1, -1, 1), XMFLOAT3( 1, 0, 0) },
{ XMFLOAT3( 1, 1, 1), XMFLOAT3( 1, 0, 0) }, // X Positive face
{ XMFLOAT3(-1, -1, 1), XMFLOAT3(0, -1, 0) },
{ XMFLOAT3( 1, -1, 1), XMFLOAT3(0, -1, 0) },
{ XMFLOAT3( 1, -1, -1), XMFLOAT3(0, -1, 0) },
{ XMFLOAT3(-1, -1, -1), XMFLOAT3(0, -1, 0) }, // Y negative face
{ XMFLOAT3( 1, 1, 1), XMFLOAT3(0, 1, 0) },
{ XMFLOAT3(-1, 1, 1), XMFLOAT3(0, 1, 0) },
{ XMFLOAT3(-1, 1, -1), XMFLOAT3(0, 1, 0) },
{ XMFLOAT3( 1, 1, -1), XMFLOAT3(0, 1, 0) }, // Y Positive face
};
D3D11_SUBRESOURCE_DATA initialData = { vertices, 0, 0 };
CD3D11_BUFFER_DESC bufferDesc(sizeof(vertices), D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_IMMUTABLE);
bufferDesc.StructureByteStride = sizeof(Vertex);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, &initialData, m_vertexBuffer.ReleaseAndGetAddressOf())
);
}
// Create vertex buffers with per-instance data.
{
CD3D11_BUFFER_DESC bufferDesc(sizeof(Instance) * c_maxInstances, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
bufferDesc.StructureByteStride = sizeof(Instance);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, nullptr, m_instanceData.ReleaseAndGetAddressOf())
);
}
// Create a static vertex buffer with color data.
{
static const XMVECTORF32 c_bigColor = { 1.f, 1.f, 1.f, 0.f };
uint32_t colors[c_maxInstances];
colors[0] = XMCOLOR(c_bigColor);
for (uint32_t i = 1; i < c_maxInstances; ++i)
{
if (i <= c_pointLightCount)
{
m_lights.pointColors[i - 1] = XMFLOAT4(FloatRand(0.25f, 1.0f), FloatRand(0.25f, 1.0f), FloatRand(0.25f, 1.0f), 1.0f);
colors[i] = XMCOLOR(m_lights.pointColors[i - 1].x, m_lights.pointColors[i - 1].y, m_lights.pointColors[i - 1].z, 1.f);
}
else
{
colors[i] = XMCOLOR(FloatRand(0.25f, 1.0f), FloatRand(0.25f, 1.0f), FloatRand(0.25f, 1.0f), 0.f);
}
}
D3D11_SUBRESOURCE_DATA initialData = { colors, 0, 0 };
CD3D11_BUFFER_DESC bufferDesc(sizeof(uint32_t) * c_maxInstances, D3D11_BIND_VERTEX_BUFFER, D3D11_USAGE_IMMUTABLE);
bufferDesc.StructureByteStride = sizeof(uint32_t);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, &initialData, m_boxColors.ReleaseAndGetAddressOf())
);
}
// Create and initialize the index buffer for the cube geometry.
{
static const uint16_t indices[] =
{
0, 2, 1,
0, 3, 2,
4, 6, 5,
4, 7, 6,
8, 10, 9,
8, 11, 10,
12, 14, 13,
12, 15, 14,
16, 18, 17,
16, 19, 18,
20, 22, 21,
20, 23, 22,
};
D3D11_SUBRESOURCE_DATA initialData = { indices, 0, 0 };
CD3D11_BUFFER_DESC bufferDesc(sizeof(indices), D3D11_BIND_INDEX_BUFFER, D3D11_USAGE_IMMUTABLE);
bufferDesc.StructureByteStride = sizeof(uint16_t);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, &initialData, m_indexBuffer.ReleaseAndGetAddressOf())
);
}
// Create the vertex shader constant buffer.
{
CD3D11_BUFFER_DESC bufferDesc(sizeof(XMMATRIX), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, nullptr, m_vertexConstants.ReleaseAndGetAddressOf())
);
}
// Create the pixel shader (lighting) constant buffer.
{
static_assert((sizeof(Lights) % 16) == 0, "Constant buffer must always be 16-byte aligned");
CD3D11_BUFFER_DESC bufferDesc(sizeof(Lights), D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE);
DX::ThrowIfFailed(
device->CreateBuffer(&bufferDesc, nullptr, m_pixelConstants.ReleaseAndGetAddressOf())
);
}
m_CPUInstanceData.reset(new Instance[c_maxInstances]);
m_rotationQuaternions.reset(reinterpret_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * c_maxInstances, 16)));
m_velocities.reset(reinterpret_cast<XMVECTOR*>(_aligned_malloc(sizeof(XMVECTOR) * c_maxInstances, 16)));
// Set up the position and scale for the container box. Scale is negative to turn the box inside-out
// (this effectively reverses the normals and backface culling).
// Scale the outside box to slightly larger than our scene boundary, so bouncing boxes never actually clip it.
m_CPUInstanceData[0].positionAndScale = XMFLOAT4(0.0f, 0.0f, 0.0f, -(c_boxBounds + 5));
m_CPUInstanceData[0].quaternion = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
// Initialize the directional light.
XMStoreFloat4(&m_lights.directional, XMVector3Normalize(XMVectorSet(1.0f, 4.0f, -2.0f, 0)));
// Initialize the positions/state of all the cubes in the scene.
ResetSimulation();
}
// Allocate all memory resources that change on a window SizeChanged event.
void Sample::CreateWindowSizeDependentResources()
{
// Initialize the projection matrix.
auto size = m_deviceResources->GetOutputSize();
auto device = m_deviceResources->GetD3DDevice();
m_smallFont = std::make_unique<SpriteFont>(device, (size.bottom > 1080) ? L"SegoeUI_36.spritefont" : L"SegoeUI_18.spritefont");
m_ctrlFont = std::make_unique<SpriteFont>(device, (size.bottom > 1080) ? L"XboxOneControllerLegend.spritefont" : L"XboxOneControllerLegendSmall.spritefont");
XMMATRIX proj = XMMatrixPerspectiveFovLH(XM_PIDIV4, float(size.right) / float(size.bottom), 0.1f, 500.0f);
XMStoreFloat4x4(&m_proj, proj);
}
#pragma endregion
void Sample::ReplaceBufferContents(ID3D11Buffer* buffer, size_t bufferSize, const void* data)
{
auto context = m_deviceResources->GetD3DDeviceContext();
D3D11_MAPPED_SUBRESOURCE mapped;
DX::ThrowIfFailed(
context->Map(buffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped)
);
memcpy(mapped.pData, data, bufferSize);
context->Unmap(buffer, 0);
}
void Sample::ResetSimulation()
{
// Reset positions to starting point, and orientations to identity.
// Note that instance 0 is the scene bounding box, and the position, orientation and scale are static (i.e. never update).
for (size_t i = 1; i < c_maxInstances; ++i)
{
m_CPUInstanceData[i].positionAndScale = XMFLOAT4(0.0f, 0.0f, c_boxBounds / 2.0f, FloatRand(0.1f, 0.4f));
m_CPUInstanceData[i].quaternion = XMFLOAT4(0.0f, 0.0f, 0.0f, 1.0f);
// For the first c_pointLightCount in the updated array, we scale up by a small factor so they stand out, and
// update the light constant data with their positions.
if (i <= c_pointLightCount)
{
m_CPUInstanceData[i].positionAndScale.w = 1.53f;
m_lights.pointPositions[i - 1] = m_CPUInstanceData[i].positionAndScale;
}
// Apply a random spin to each instance.
m_rotationQuaternions[i] = XMQuaternionRotationAxis(XMVector3Normalize(XMVectorSet(FloatRand(), FloatRand(), FloatRand(), 0)), FloatRand(0.001f, 0.1f));
// ...and a random velocity.
m_velocities[i] = XMVectorSet(FloatRand(-0.01f, 0.01f), FloatRand(-0.01f, 0.01f), FloatRand(-0.01f, 0.01f), 0);
}
}
inline float Sample::FloatRand(float lowerBound, float upperBound)
{
if (lowerBound == upperBound)
return lowerBound;
std::uniform_real_distribution<float> dist(lowerBound, upperBound);
return dist(m_randomEngine);
}