This repository has been archived by the owner on Dec 25, 2023. It is now read-only.
/
SceneObject.cpp
695 lines (581 loc) · 31.6 KB
/
SceneObject.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
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
//*********************************************************
//
// Copyright 2020 Intel Corporation
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files(the "Software"), to deal in the Software
// without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to
// whom the Software is furnished to do so, subject to the
// following conditions :
// The above copyright notice and this permission notice shall
// be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
//*********************************************************
#pragma once
#include "pch.h"
#include <DirectXMath.h>
#include <wrl.h>
#include <string>
#include <shlwapi.h> // for PathFindExtension
#pragma comment(lib, "Shlwapi.lib")
#include "SceneObject.h"
#include "SharedConstants.h"
#include "Scene.h"
#include "TerrainGenerator.h"
//-------------------------------------------------------------------------
// constructor
//-------------------------------------------------------------------------
SceneObjects::BaseObject::BaseObject(
const std::wstring& in_filename,
TileUpdateManager* in_pTileUpdateManager,
StreamingHeap* in_pStreamingHeap,
ID3D12Device* in_pDevice,
D3D12_CPU_DESCRIPTOR_HANDLE in_srvBaseCPU,
BaseObject* in_pSharedObject) :
m_matrix(DirectX::XMMatrixIdentity()),m_srvUavCbvDescriptorSize(0)
, m_pTileUpdateManager(in_pTileUpdateManager)
{
//---------------------------------------
// create root signature
//---------------------------------------
if (in_pSharedObject)
{
m_rootSignature = in_pSharedObject->m_rootSignature;
m_rootSignatureFB = in_pSharedObject->m_rootSignatureFB;
m_pipelineState = in_pSharedObject->m_pipelineState;
m_pipelineStateFB = in_pSharedObject->m_pipelineStateFB;
}
else
{
//--------------------------------------------
// uav and srvs for streaming texture
//--------------------------------------------
std::vector<CD3DX12_DESCRIPTOR_RANGE1> ranges;
// these three descriptors are expected to be consecutive in the descriptor heap:
// t0: streaming/reserved/paired texture
// t1: min mip map
// u0: feedback map
// t0: streaming/reserved/paired texture
ranges.push_back(CD3DX12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE,
UINT(Descriptors::HeapOffsetTexture)));
// t1: min mip map
// the min mip view is "volatile" because it changes if the # of objects changes. it's re-created every frame for simplicity
std::vector<CD3DX12_DESCRIPTOR_RANGE1> sharedRanges;
sharedRanges.push_back(CD3DX12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_SRV, 1, 1, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE | D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE, 0));
// b0: constant buffers
std::vector<CD3DX12_DESCRIPTOR_RANGE1> cbvRanges;
cbvRanges.push_back(CD3DX12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_CBV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DATA_STATIC, 0));
// s0: sampler
std::vector<CD3DX12_DESCRIPTOR_RANGE1> samplerRanges;
samplerRanges.push_back(CD3DX12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DESCRIPTORS_VOLATILE, 0));
std::vector<CD3DX12_ROOT_PARAMETER1> rootParameters;
rootParameters.resize((UINT)RootSigParams::NumParams);
//-----------------------------
// Root Signature Descriptor Tables & 32-bit constants
//-----------------------------
// per-object textures & UAVs
CD3DX12_ROOT_PARAMETER1 rootParam;
rootParam.InitAsDescriptorTable((UINT)ranges.size(), ranges.data(), D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[(UINT)RootSigParams::ParamObjectTextures] = rootParam;
// shared textures
rootParam.InitAsDescriptorTable((UINT)sharedRanges.size(), sharedRanges.data(), D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[(UINT)RootSigParams::ParamSharedTextures] = rootParam;
// constant buffers used by both vertex & pixel shaders
rootParam.InitAsDescriptorTable((UINT)cbvRanges.size(), cbvRanges.data(), D3D12_SHADER_VISIBILITY_ALL);
rootParameters[(UINT)RootSigParams::ParamConstantBuffers] = rootParam;
// samplers
rootParam.InitAsDescriptorTable((UINT)samplerRanges.size(), samplerRanges.data(), D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[(UINT)RootSigParams::ParamSamplers] = rootParam;
UINT num32BitValues = sizeof(ModelConstantData) / 4;
rootParam.InitAsConstants(num32BitValues, 1, 0, D3D12_SHADER_VISIBILITY_ALL);
rootParameters[(UINT)RootSigParams::Param32BitConstants] = rootParam;
// root sig without feedback map bound
{
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDesc;
rootSignatureDesc.Init_1_1((UINT)rootParameters.size(), rootParameters.data(), 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
ComPtr<ID3DBlob> signature;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3D12SerializeVersionedRootSignature(&rootSignatureDesc, &signature, &error));
ThrowIfFailed(in_pDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignature)));
}
// root sig with feedback map bound
{
// add a UAV for the feedback map
ranges.push_back(CD3DX12_DESCRIPTOR_RANGE1(D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, D3D12_DESCRIPTOR_RANGE_FLAG_DATA_VOLATILE,
UINT(Descriptors::HeapOffsetFeedback)));
// add uav range to previous root param
rootParam.InitAsDescriptorTable((UINT)ranges.size(), ranges.data(), D3D12_SHADER_VISIBILITY_PIXEL);
rootParameters[(UINT)RootSigParams::ParamObjectTextures] = rootParam;
// re-serialize
CD3DX12_VERSIONED_ROOT_SIGNATURE_DESC rootSignatureDesc;
rootSignatureDesc.Init_1_1((UINT)rootParameters.size(), rootParameters.data(), 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT);
ComPtr<ID3DBlob> signature;
ComPtr<ID3DBlob> error;
ThrowIfFailed(D3D12SerializeVersionedRootSignature(&rootSignatureDesc, &signature, &error));
ThrowIfFailed(in_pDevice->CreateRootSignature(0, signature->GetBufferPointer(), signature->GetBufferSize(), IID_PPV_ARGS(&m_rootSignatureFB)));
}
}
//---------------------------------------
// create streaming resources
//---------------------------------------
{
m_srvUavCbvDescriptorSize = in_pDevice->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
// The tile update manager queries the streaming texture for its tile dimensions
// The feedback resource will be allocated with a mip region size matching the tile size
m_pStreamingResource = std::unique_ptr<StreamingResource>(in_pTileUpdateManager->CreateStreamingResource(in_filename, in_pStreamingHeap));
// sampler feedback view
CD3DX12_CPU_DESCRIPTOR_HANDLE feedbackHandle(in_srvBaseCPU, (UINT)Descriptors::HeapOffsetFeedback, m_srvUavCbvDescriptorSize);
m_pStreamingResource->CreateFeedbackView(in_pDevice, feedbackHandle);
// texture view
CD3DX12_CPU_DESCRIPTOR_HANDLE textureHandle(in_srvBaseCPU, (UINT)Descriptors::HeapOffsetTexture, m_srvUavCbvDescriptorSize);
m_pStreamingResource->CreateStreamingView(in_pDevice, textureHandle);
}
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::CreatePipelineState(
const wchar_t* in_ps, const wchar_t* in_psFB, const wchar_t* in_vs,
ID3D12Device* in_pDevice, UINT in_sampleCount,
const D3D12_RASTERIZER_DESC& in_rasterizerDesc,
const D3D12_DEPTH_STENCIL_DESC& in_depthStencilDesc)
{
// Define the vertex input layout
D3D12_INPUT_ELEMENT_DESC inputElementDescs[] = {
{ "POS", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 0, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "NORMAL", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, 12, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
{ "TEX", 0, DXGI_FORMAT_R32G32_FLOAT, 0, 24, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0 },
};
std::wstring firstPassVsPath = GetAssetFullPath(in_vs);
std::wstring firstPassPsPath = GetAssetFullPath(in_ps);
std::ifstream vsFile(firstPassVsPath, std::fstream::binary);
std::vector<char> vsBytes = std::vector<char>((std::istreambuf_iterator<char>(vsFile)), std::istreambuf_iterator<char>());
std::ifstream psFile(firstPassPsPath, std::fstream::binary);
std::vector<char> psBytes = std::vector<char>((std::istreambuf_iterator<char>(psFile)), std::istreambuf_iterator<char>());
// Describe and create the graphics pipeline state object (PSO).
D3D12_GRAPHICS_PIPELINE_STATE_DESC psoDesc = {};
psoDesc.pRootSignature = m_rootSignature.Get();
psoDesc.VS.BytecodeLength = vsBytes.size();
psoDesc.VS.pShaderBytecode = vsBytes.data();
psoDesc.PS.BytecodeLength = psBytes.size();
psoDesc.PS.pShaderBytecode = psBytes.data();
psoDesc.BlendState = CD3DX12_BLEND_DESC(D3D12_DEFAULT);
psoDesc.SampleMask = UINT_MAX;
psoDesc.RasterizerState = in_rasterizerDesc;
psoDesc.DepthStencilState = in_depthStencilDesc;
psoDesc.InputLayout = { inputElementDescs, _countof(inputElementDescs) };
psoDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
psoDesc.NumRenderTargets = 1;
psoDesc.RTVFormats[0] = SharedConstants::SWAP_CHAIN_FORMAT;
psoDesc.DSVFormat = SharedConstants::DEPTH_FORMAT;
psoDesc.SampleDesc.Count = in_sampleCount;
ThrowIfFailed(in_pDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineState)));
// create a second PSO that writes to a feedback map using our root signature with the feedback map UAV bound
{
psoDesc.pRootSignature = m_rootSignatureFB.Get();
firstPassPsPath = GetAssetFullPath(in_psFB);
std::ifstream psFeedback(firstPassPsPath, std::fstream::binary);
std::vector<char> psFeedbackBytes = std::vector<char>((std::istreambuf_iterator<char>(psFeedback)), std::istreambuf_iterator<char>());
psoDesc.PS.BytecodeLength = psFeedbackBytes.size();
psoDesc.PS.pShaderBytecode = psFeedbackBytes.data();
ThrowIfFailed(in_pDevice->CreateGraphicsPipelineState(&psoDesc, IID_PPV_ARGS(&m_pipelineStateFB)));
}
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
std::wstring SceneObjects::BaseObject::GetAssetFullPath(const std::wstring& in_filename)
{
constexpr size_t PATHBUFFERSIZE = MAX_PATH * 4;
TCHAR buffer[PATHBUFFERSIZE];
GetAssetsPath(buffer, PATHBUFFERSIZE);
std::wstring directory = buffer;
return directory + in_filename;
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::SetModelConstants(ModelConstantData& out_modelConstantData,
const DirectX::XMMATRIX&, const DirectX::XMMATRIX&, const DirectX::XMMATRIX& in_viewInverse)
{
out_modelConstantData.g_combinedTransform = m_combinedMatrix;
out_modelConstantData.g_worldTransform = m_matrix;
DirectX::XMVECTOR vEyePt = in_viewInverse.r[3];
DirectX::XMStoreFloat4(&(out_modelConstantData.g_eyePos), vEyePt);
out_modelConstantData.g_minmipmapWidth = m_pStreamingResource->GetMinMipMapWidth();
out_modelConstantData.g_minmipmapHeight = m_pStreamingResource->GetMinMipMapHeight();
out_modelConstantData.g_minmipmapOffset = m_pStreamingResource->GetMinMipMapOffset();
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::CopyGeometry(const BaseObject* in_pObjectForSharedHeap)
{
m_lods.resize(in_pObjectForSharedHeap->m_lods.size());
for (UINT i = 0; i < m_lods.size(); i++)
{
m_lods[i].m_numIndices = in_pObjectForSharedHeap->m_lods[i].m_numIndices;
m_lods[i].m_indexBufferView = in_pObjectForSharedHeap->m_lods[i].m_indexBufferView;
m_lods[i].m_vertexBufferView = in_pObjectForSharedHeap->m_lods[i].m_vertexBufferView;
}
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::SetGeometry(ID3D12Resource* in_pVertexBuffer, UINT in_numVertices, UINT in_vertexSize,
ID3D12Resource* in_pIndexBuffer, UINT in_numIndices, UINT in_lod)
{
if (in_lod >= m_lods.size())
{
m_lods.resize(in_lod + 1);
}
auto& lod = m_lods[in_lod];
lod.m_vertexBuffer = in_pVertexBuffer;
lod.m_indexBuffer = in_pIndexBuffer;
in_pVertexBuffer->Release();
in_pIndexBuffer->Release();
lod.m_numIndices = in_numIndices;
lod.m_vertexBufferView = { lod.m_vertexBuffer->GetGPUVirtualAddress(), in_numVertices * in_vertexSize, in_vertexSize };
lod.m_indexBufferView = { lod.m_indexBuffer->GetGPUVirtualAddress(), UINT(in_numIndices * sizeof(UINT32)), DXGI_FORMAT_R32_UINT };
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::BaseObject::Draw(ID3D12GraphicsCommandList1* in_pCommandList, const SceneObjects::DrawParams& in_drawParams)
{
//-------------------------------------------
// draw object to multi-sampled rendertarget
//-------------------------------------------
if (m_pStreamingResource->GetPackedMipsResident())
{
// choose LoD if applicable
UINT lod = 0;
if (m_lods.size() > 1)
{
DirectX::XMVECTOR eye = in_drawParams.m_viewInverse.r[3];
DirectX::XMVECTOR pos = m_matrix.r[3];
DirectX::XMVECTOR distance = DirectX::XMVectorSubtract(pos, eye);
distance = DirectX::XMVector3LengthEst(distance);
// FIXME? could store bounding sphere diameter in object
DirectX::XMVECTOR scale = DirectX::XMVector3LengthEst(m_matrix.r[0]);
float objectSize = DirectX::XMVectorGetX(scale);
float distanceToEye = DirectX::XMVectorGetX(distance);
// slow lod progression with distance
distanceToEye = std::powf(distanceToEye, 0.95f);
float lodBias = 0.1f * (float)SharedConstants::SPHERE_LOD_BIAS;
float ratio = distanceToEye / (objectSize * lodBias);
// clamp to the number of lods
lod = std::min((UINT)m_lods.size() - 1, UINT(ratio));
}
const auto& geometry = m_lods[lod];
// if feedback is enabled, 2 things:
// 1. tell the tile update manager to queue a readback of the resolved feedback
// 2. draw the object with a shader that calls WriteSamplerFeedback()
if (m_feedbackEnabled)
{
auto feedbackDescriptor = CD3DX12_GPU_DESCRIPTOR_HANDLE(in_drawParams.m_srvBaseGPU,
UINT(SceneObjects::Descriptors::HeapOffsetFeedback), m_srvUavCbvDescriptorSize);
m_pTileUpdateManager->QueueFeedback(GetStreamingResource(), feedbackDescriptor);
SetRootSigPsoFB(in_pCommandList);
}
else
{
SetRootSigPso(in_pCommandList);
}
// uavs and srvs
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)RootSigParams::ParamObjectTextures, in_drawParams.m_srvBaseGPU);
// shared min mip map resource
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSharedTextures, in_drawParams.m_sharedMinMipMap);
// constant buffers
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamConstantBuffers, in_drawParams.m_constantBuffers);
// samplers
in_pCommandList->SetGraphicsRootDescriptorTable((UINT)SceneObjects::RootSigParams::ParamSamplers, in_drawParams.m_samplers);
ModelConstantData modelConstantData{};
SetModelConstants(modelConstantData, in_drawParams.m_projection, in_drawParams.m_view, in_drawParams.m_viewInverse);
UINT num32BitValues = sizeof(ModelConstantData) / 4;
in_pCommandList->SetGraphicsRoot32BitConstants((UINT)RootSigParams::Param32BitConstants, num32BitValues, &modelConstantData, 0);
in_pCommandList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
in_pCommandList->IASetIndexBuffer(&geometry.m_indexBufferView);
in_pCommandList->IASetVertexBuffers(0, 1, &geometry.m_vertexBufferView);
in_pCommandList->DrawIndexedInstanced(geometry.m_numIndices, 1, 0, 0, 0);
}
}
//-----------------------------------------------------------------------------
// helper class
//-----------------------------------------------------------------------------
class Staging
{
public:
Staging(ID3D12Resource* out_pBuffer, const D3D12_RESOURCE_DESC& in_desc,
D3D12_RESOURCE_STATES in_finalState) :
m_finalState(in_finalState), m_pBuffer(out_pBuffer)
{
ComPtr<ID3D12Device> device;
out_pBuffer->GetDevice(IID_PPV_ARGS(&device));
ThrowIfFailed(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&m_commandAllocator)));
device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, m_commandAllocator.Get(), nullptr, IID_PPV_ARGS(&m_commandList));
m_commandList->SetName(L"Staging::m_commandList");
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
ThrowIfFailed(device->CreateCommandQueue(&queueDesc, IID_PPV_ARGS(&m_commandQueue)));
ThrowIfFailed(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&m_renderFence)));
const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_UPLOAD);
ThrowIfFailed(device->CreateCommittedResource(
&heapProperties, D3D12_HEAP_FLAG_NONE,
&in_desc, D3D12_RESOURCE_STATE_GENERIC_READ, nullptr, IID_PPV_ARGS(&m_stagingResource)));
}
ID3D12Resource* GetResource() { return m_stagingResource.Get(); }
ID3D12GraphicsCommandList* GetCommandList() { return m_commandList.Get(); }
~Staging()
{
D3D12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(m_pBuffer, D3D12_RESOURCE_STATE_COPY_DEST, m_finalState);
m_commandList->ResourceBarrier(1, &barrier);
// submit all our initialization commands
m_commandList->Close();
ID3D12CommandList* ppCommandLists[] = { m_commandList.Get() };
m_commandQueue->ExecuteCommandLists(_countof(ppCommandLists), ppCommandLists);
HANDLE renderFenceEvent = ::CreateEvent(nullptr, FALSE, FALSE, nullptr);
if (renderFenceEvent == nullptr)
{
ThrowIfFailed(HRESULT_FROM_WIN32(GetLastError()));
}
ThrowIfFailed(m_commandQueue->Signal(m_renderFence.Get(), 1));
ThrowIfFailed(m_renderFence->SetEventOnCompletion(1, renderFenceEvent));
WaitForSingleObject(renderFenceEvent, INFINITE);
::CloseHandle(renderFenceEvent);
}
private:
template<typename T> using ComPtr = Microsoft::WRL::ComPtr<T>;
ComPtr<ID3D12CommandAllocator> m_commandAllocator;
ComPtr<ID3D12GraphicsCommandList> m_commandList;
ComPtr<ID3D12CommandQueue> m_commandQueue;
ComPtr<ID3D12Fence> m_renderFence;
ComPtr<ID3D12Resource> m_stagingResource;
ID3D12Resource* m_pBuffer;
D3D12_RESOURCE_STATES m_finalState;
};
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SceneObjects::InitializeBuffer(ID3D12Resource* out_pBuffer,
const void* in_pBytes, size_t in_numBytes, D3D12_RESOURCE_STATES in_finalState)
{
D3D12_RESOURCE_DESC desc = out_pBuffer->GetDesc();
Staging staging(out_pBuffer, desc, in_finalState);
D3D12_SUBRESOURCE_DATA data{ in_pBytes, LONG_PTR(in_numBytes), LONG_PTR(in_numBytes) };
UpdateSubresources<1>(staging.GetCommandList(), out_pBuffer, staging.GetResource(), 0, 0, 1, &data);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SceneObjects::InitializeTexture(ID3D12Resource* out_pTexture,
size_t in_numSubResources, const D3D12_SUBRESOURCE_DATA* in_pSubresources, D3D12_RESOURCE_STATES in_finalState)
{
UINT64 nStageSize = GetRequiredIntermediateSize(out_pTexture, 0, static_cast<UINT>(in_numSubResources));
D3D12_RESOURCE_DESC desc = CD3DX12_RESOURCE_DESC::Buffer(nStageSize);
Staging staging(out_pTexture, desc, in_finalState);
UpdateSubresources(staging.GetCommandList(), out_pTexture, staging.GetResource(), 0, 0, static_cast<UINT>(in_numSubResources), in_pSubresources);
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SceneObjects::CreateSphereResources(
ID3D12Resource** out_ppVertexBuffer, ID3D12Resource** out_ppIndexBuffer,
ID3D12Device* in_pDevice, const SphereGen::Properties& in_sphereProperties)
{
std::vector<SphereGen::Vertex> sphereVerts;
std::vector<UINT32> sphereIndices;
SphereGen::Create(sphereVerts, sphereIndices, &in_sphereProperties);
// build vertex buffer
{
UINT vertexBufferSize = UINT(sphereVerts.size()) * sizeof(sphereVerts[0]);
const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
const auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize);
ThrowIfFailed(in_pDevice->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(out_ppVertexBuffer)));
InitializeBuffer(*out_ppVertexBuffer, sphereVerts.data(),
vertexBufferSize, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
}
// build index buffer
{
UINT indexBufferSize = UINT(sphereIndices.size()) * sizeof(sphereIndices[0]);
const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
const auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(indexBufferSize);
ThrowIfFailed(in_pDevice->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(out_ppIndexBuffer)));
InitializeBuffer(*out_ppIndexBuffer, sphereIndices.data(),
indexBufferSize, D3D12_RESOURCE_STATE_INDEX_BUFFER);
}
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void SceneObjects::CreateSphere(SceneObjects::BaseObject* out_pObject,
ID3D12Device* in_pDevice,
const SphereGen::Properties& in_sphereProperties,
UINT in_numLods)
{
const float lodStepFactor = 1.0f / in_numLods;
float lodScaleFactor = 1.0f;
SphereGen::Properties sphereProperties = in_sphereProperties;
for (UINT lod = 0; lod < in_numLods; lod++)
{
sphereProperties.m_numLat = UINT(in_sphereProperties.m_numLat * lodScaleFactor);
sphereProperties.m_numLong = UINT(in_sphereProperties.m_numLong * lodScaleFactor);
lodScaleFactor -= lodStepFactor;
std::vector<SphereGen::Vertex> sphereVerts;
std::vector<UINT32> sphereIndices;
SphereGen::Create(sphereVerts, sphereIndices, &sphereProperties);
ID3D12Resource* pVertexBuffer{ nullptr };
ID3D12Resource* pIndexBuffer{ nullptr };
CreateSphereResources(&pVertexBuffer, &pIndexBuffer, in_pDevice, sphereProperties);
out_pObject->SetGeometry(
pVertexBuffer, (UINT)sphereVerts.size(), (UINT)sizeof(SphereGen::Vertex),
pIndexBuffer, (UINT)sphereIndices.size(), lod);
}
}
//=========================================================================
//=========================================================================
SceneObjects::Terrain::Terrain(const std::wstring& in_filename,
TileUpdateManager* in_pTileUpdateManager,
StreamingHeap* in_pStreamingHeap,
ID3D12Device* in_pDevice,
UINT in_sampleCount,
D3D12_CPU_DESCRIPTOR_HANDLE in_srvBaseCPU,
const CommandLineArgs& in_args) :
BaseObject(in_filename, in_pTileUpdateManager, in_pStreamingHeap,
in_pDevice, in_srvBaseCPU, nullptr)
{
D3D12_RASTERIZER_DESC rasterizerDesc = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
D3D12_DEPTH_STENCIL_DESC depthStencilDesc = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
CreatePipelineState(L"terrainPS.cso", L"terrainPS-FB.cso", L"terrainVS.cso", in_pDevice, in_sampleCount, rasterizerDesc, depthStencilDesc);
UINT numQuads = (in_args.m_terrainSideSize - 1) * (in_args.m_terrainSideSize - 1);
UINT numIndices = numQuads * 6;
D3D12_INDEX_BUFFER_VIEW indexBufferView{};
D3D12_VERTEX_BUFFER_VIEW vertexBufferView{};
ID3D12Resource* pVertexBuffer{ nullptr };
ID3D12Resource* pIndexBuffer{ nullptr };
TerrainGenerator mesh(in_args);
// build vertex buffer
{
auto& vertices = mesh.GetVertices();
UINT vertexBufferSize = UINT(vertices.size()) * sizeof(vertices[0]);
const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
const auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(vertexBufferSize);
ThrowIfFailed(in_pDevice->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(&pVertexBuffer)));
InitializeBuffer(pVertexBuffer, vertices.data(), vertexBufferSize, D3D12_RESOURCE_STATE_VERTEX_AND_CONSTANT_BUFFER);
}
// build index buffer
{
std::vector<UINT> indices(numIndices);
mesh.GenerateIndices(&(indices[0]));
UINT indexBufferSize = numIndices * sizeof(indices[0]);
const auto heapProperties = CD3DX12_HEAP_PROPERTIES(D3D12_HEAP_TYPE_DEFAULT);
const auto resourceDesc = CD3DX12_RESOURCE_DESC::Buffer(indexBufferSize);
ThrowIfFailed(in_pDevice->CreateCommittedResource(
&heapProperties,
D3D12_HEAP_FLAG_NONE,
&resourceDesc,
D3D12_RESOURCE_STATE_COPY_DEST,
nullptr,
IID_PPV_ARGS(&pIndexBuffer)));
InitializeBuffer(pIndexBuffer, indices.data(), indexBufferSize, D3D12_RESOURCE_STATE_INDEX_BUFFER);
}
SetGeometry(pVertexBuffer, (UINT)mesh.GetVertices().size(), (UINT)sizeof(TerrainGenerator::Vertex),
pIndexBuffer, numIndices);
}
//=========================================================================
// planets have multiple LoDs
// Texture Coordinates may optionally be mirrored in U
//=========================================================================
SceneObjects::Planet::Planet(const std::wstring& in_filename,
TileUpdateManager* in_pTileUpdateManager,
StreamingHeap* in_pStreamingHeap,
ID3D12Device* in_pDevice,
UINT in_sampleCount,
D3D12_CPU_DESCRIPTOR_HANDLE in_srvBaseCPU,
const SphereGen::Properties& in_sphereProperties) :
BaseObject(in_filename, in_pTileUpdateManager, in_pStreamingHeap,
in_pDevice, in_srvBaseCPU, nullptr)
{
D3D12_RASTERIZER_DESC rasterizerDesc = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
D3D12_DEPTH_STENCIL_DESC depthStencilDesc = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
CreatePipelineState(L"terrainPS.cso", L"terrainPS-FB.cso", L"terrainVS.cso", in_pDevice, in_sampleCount, rasterizerDesc, depthStencilDesc);
const UINT numLevelsOfDetail = SharedConstants::NUM_SPHERE_LEVELS_OF_DETAIL;
CreateSphere(this, in_pDevice, in_sphereProperties, numLevelsOfDetail);
float d = SharedConstants::SPHERE_SCALE * 200;
GetModelMatrix() = DirectX::XMMatrixScaling(d, d, d);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
SceneObjects::Planet::Planet(const std::wstring& in_filename,
TileUpdateManager* in_pTileUpdateManager,
StreamingHeap* in_pStreamingHeap,
ID3D12Device* in_pDevice,
D3D12_CPU_DESCRIPTOR_HANDLE in_srvBaseCPU,
BaseObject* in_pSharedObject) :
BaseObject(in_filename, in_pTileUpdateManager, in_pStreamingHeap,
in_pDevice, in_srvBaseCPU, in_pSharedObject)
{
CopyGeometry(in_pSharedObject);
}
//=============================================================================
// sky is like a planet with front culling instead of backface culling
// it is always positioned around 0,0,0. The camera only rotates relative to the sky
// it has only 1 LoD
// shading is much simpler: no lighting
//=============================================================================
SceneObjects::Sky::Sky(const std::wstring& in_filename,
TileUpdateManager* in_pTileUpdateManager,
StreamingHeap* in_pStreamingHeap,
ID3D12Device* in_pDevice,
UINT in_sampleCount,
D3D12_CPU_DESCRIPTOR_HANDLE in_srvBaseCPU) :
BaseObject(in_filename, in_pTileUpdateManager, in_pStreamingHeap,
in_pDevice, in_srvBaseCPU, nullptr)
{
D3D12_RASTERIZER_DESC rasterizerDesc = CD3DX12_RASTERIZER_DESC(D3D12_DEFAULT);
rasterizerDesc.CullMode = D3D12_CULL_MODE_FRONT;
D3D12_DEPTH_STENCIL_DESC depthStencilDesc = CD3DX12_DEPTH_STENCIL_DESC(D3D12_DEFAULT);
depthStencilDesc.DepthEnable = false;
CreatePipelineState(L"skyPS.cso", L"skyPS-FB.cso", L"skyVS.cso", in_pDevice, in_sampleCount, rasterizerDesc, depthStencilDesc);
SphereGen::Properties sphereProperties;
sphereProperties.m_numLong = 80;
sphereProperties.m_numLat = 81;
sphereProperties.m_mirrorU = true;
CreateSphere(this, in_pDevice, sphereProperties);
float d = SharedConstants::SPHERE_SCALE * 200;
GetModelMatrix() = DirectX::XMMatrixScaling(d, d, d);
}
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
void SceneObjects::Sky::SetModelConstants(ModelConstantData& out_modelConstantData,
const DirectX::XMMATRIX& in_projection, const DirectX::XMMATRIX& in_view, const DirectX::XMMATRIX&)
{
DirectX::XMMATRIX view = in_view;
view.r[3] = DirectX::XMVectorSet(0, 0, 0, 1);
out_modelConstantData.g_combinedTransform = m_matrix * view * in_projection;
out_modelConstantData.g_worldTransform = DirectX::XMMatrixIdentity();
out_modelConstantData.g_minmipmapWidth = m_pStreamingResource->GetMinMipMapWidth();
out_modelConstantData.g_minmipmapHeight = m_pStreamingResource->GetMinMipMapHeight();
out_modelConstantData.g_minmipmapOffset = m_pStreamingResource->GetMinMipMapOffset();
}