-
Notifications
You must be signed in to change notification settings - Fork 293
Expand file tree
/
Copy pathgraphicsplugin_d3d12.cpp
More file actions
749 lines (616 loc) · 36.5 KB
/
graphicsplugin_d3d12.cpp
File metadata and controls
749 lines (616 loc) · 36.5 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
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
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
// Copyright (c) 2017-2026 The Khronos Group Inc.
//
// SPDX-License-Identifier: Apache-2.0
#include "pch.h"
#include "common.h"
#include "geometry.h"
#include "graphicsplugin.h"
#include "options.h"
#include "graphics_plugin_impl_helpers.h"
#include "check.h"
#if defined(XR_USE_GRAPHICS_API_D3D12)
#include <common/xr_linear.h>
#include <DirectXColors.h>
#include <D3Dcompiler.h>
#include "d3d_common.h"
#include "d3d12_utils.h"
using namespace Microsoft::WRL;
using namespace DirectX;
namespace {
void InitializeD3D12DeviceForAdapter(IDXGIAdapter1* adapter, D3D_FEATURE_LEVEL minimumFeatureLevel, ID3D12Device** device) {
#if !defined(NDEBUG)
ComPtr<ID3D12Debug> debugCtrl;
if (SUCCEEDED(D3D12GetDebugInterface(__uuidof(ID3D12Debug), &debugCtrl))) {
debugCtrl->EnableDebugLayer();
}
#endif
CHECK_HRCMD(D3D12CreateDevice(adapter, minimumFeatureLevel, __uuidof(ID3D12Device), reinterpret_cast<void**>(device)));
}
template <uint32_t alignment>
constexpr uint32_t AlignTo(uint32_t n) {
static_assert((alignment & (alignment - 1)) == 0, "The alignment must be power-of-two");
return (n + alignment - 1) & ~(alignment - 1);
}
ComPtr<ID3D12Resource> CreateBuffer(ID3D12Device* d3d12Device, uint32_t size, D3D12_HEAP_TYPE heapType) {
D3D12_RESOURCE_STATES d3d12ResourceState;
if (heapType == D3D12_HEAP_TYPE_UPLOAD) {
d3d12ResourceState = D3D12_RESOURCE_STATE_GENERIC_READ;
size = AlignTo<D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT>(size);
} else {
d3d12ResourceState = D3D12_RESOURCE_STATE_COMMON;
}
D3D12_HEAP_PROPERTIES heapProp{};
heapProp.Type = heapType;
heapProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
heapProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
D3D12_RESOURCE_DESC buffDesc{};
buffDesc.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER;
buffDesc.Alignment = 0;
buffDesc.Width = size;
buffDesc.Height = 1;
buffDesc.DepthOrArraySize = 1;
buffDesc.MipLevels = 1;
buffDesc.Format = DXGI_FORMAT_UNKNOWN;
buffDesc.SampleDesc.Count = 1;
buffDesc.SampleDesc.Quality = 0;
buffDesc.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR;
buffDesc.Flags = D3D12_RESOURCE_FLAG_NONE;
ComPtr<ID3D12Resource> buffer;
CHECK_HRCMD(d3d12Device->CreateCommittedResource(&heapProp, D3D12_HEAP_FLAG_NONE, &buffDesc, d3d12ResourceState, nullptr,
__uuidof(ID3D12Resource),
reinterpret_cast<void**>(buffer.ReleaseAndGetAddressOf())));
return buffer;
}
class SwapchainImageContext {
public:
std::vector<XrSwapchainImageBaseHeader*> Create(ID3D12Device* d3d12Device, uint32_t capacity) {
m_d3d12Device = d3d12Device;
m_swapchainImages.resize(capacity);
std::vector<XrSwapchainImageBaseHeader*> bases(capacity);
for (uint32_t i = 0; i < capacity; ++i) {
m_swapchainImages[i] = {XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR};
bases[i] = reinterpret_cast<XrSwapchainImageBaseHeader*>(&m_swapchainImages[i]);
}
CHECK_HRCMD(m_d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator),
reinterpret_cast<void**>(m_commandAllocator.ReleaseAndGetAddressOf())));
m_viewProjectionCBuffer = CreateBuffer(m_d3d12Device, sizeof(ViewProjectionConstantBuffer), D3D12_HEAP_TYPE_UPLOAD);
return bases;
}
ID3D12Resource* GetDepthStencilTexture(ID3D12Resource* colorTexture) {
if (!m_depthStencilTexture) {
// This back-buffer has no corresponding depth-stencil texture, so create one with matching dimensions.
const D3D12_RESOURCE_DESC colorDesc = colorTexture->GetDesc();
D3D12_HEAP_PROPERTIES heapProp{};
heapProp.Type = D3D12_HEAP_TYPE_DEFAULT;
heapProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
heapProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
D3D12_RESOURCE_DESC depthDesc{};
depthDesc.Dimension = colorDesc.Dimension;
depthDesc.Alignment = colorDesc.Alignment;
depthDesc.Width = colorDesc.Width;
depthDesc.Height = colorDesc.Height;
depthDesc.DepthOrArraySize = colorDesc.DepthOrArraySize;
depthDesc.MipLevels = 1;
depthDesc.Format = DXGI_FORMAT_R32_TYPELESS;
depthDesc.SampleDesc.Count = 1;
depthDesc.Layout = colorDesc.Layout;
depthDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_CLEAR_VALUE clearValue{};
clearValue.DepthStencil.Depth = 1.0f;
clearValue.Format = DXGI_FORMAT_D32_FLOAT;
CHECK_HRCMD(m_d3d12Device->CreateCommittedResource(
&heapProp, D3D12_HEAP_FLAG_NONE, &depthDesc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clearValue,
__uuidof(ID3D12Resource), reinterpret_cast<void**>(m_depthStencilTexture.ReleaseAndGetAddressOf())));
}
return m_depthStencilTexture.Get();
}
ID3D12CommandAllocator* GetCommandAllocator() const { return m_commandAllocator.Get(); }
uint64_t GetFrameFenceValue() const { return m_fenceValue; }
void SetFrameFenceValue(uint64_t fenceValue) { m_fenceValue = fenceValue; }
void ResetCommandAllocator() { CHECK_HRCMD(m_commandAllocator->Reset()); }
void RequestModelCBuffer(uint32_t requiredSize) {
if (!m_modelCBuffer || (requiredSize > m_modelCBuffer->GetDesc().Width)) {
m_modelCBuffer = CreateBuffer(m_d3d12Device, requiredSize, D3D12_HEAP_TYPE_UPLOAD);
}
}
ID3D12Resource* GetModelCBuffer() const { return m_modelCBuffer.Get(); }
ID3D12Resource* GetViewProjectionCBuffer() const { return m_viewProjectionCBuffer.Get(); }
private:
ID3D12Device* m_d3d12Device{nullptr};
std::vector<XrSwapchainImageD3D12KHR> m_swapchainImages;
ComPtr<ID3D12CommandAllocator> m_commandAllocator;
ComPtr<ID3D12Resource> m_depthStencilTexture;
ComPtr<ID3D12Resource> m_modelCBuffer;
ComPtr<ID3D12Resource> m_viewProjectionCBuffer;
uint64_t m_fenceValue = 0;
};
struct D3D12GraphicsPlugin : public IGraphicsPlugin {
D3D12GraphicsPlugin(const std::shared_ptr<Options>& options, std::shared_ptr<IPlatformPlugin>)
: m_vertexShaderBytes(CompileShader(ShaderHlsl, "MainVS", "vs_5_1")),
m_pixelShaderBytes(CompileShader(ShaderHlsl, "MainPS", "ps_5_1")),
m_clearColor(options->GetBackgroundClearColor()) {}
~D3D12GraphicsPlugin() override { CloseHandle(m_fenceEvent); }
std::vector<std::string> GetInstanceExtensions() const override { return {XR_KHR_D3D12_ENABLE_EXTENSION_NAME}; }
void InitializeDevice(XrInstance instance, XrSystemId systemId) override {
PFN_xrGetD3D12GraphicsRequirementsKHR pfnGetD3D12GraphicsRequirementsKHR = nullptr;
CHECK_XRCMD(xrGetInstanceProcAddr(instance, "xrGetD3D12GraphicsRequirementsKHR",
reinterpret_cast<PFN_xrVoidFunction*>(&pfnGetD3D12GraphicsRequirementsKHR)));
// Create the D3D12 device for the adapter associated with the system.
XrGraphicsRequirementsD3D12KHR graphicsRequirements{XR_TYPE_GRAPHICS_REQUIREMENTS_D3D12_KHR};
CHECK_XRCMD(pfnGetD3D12GraphicsRequirementsKHR(instance, systemId, &graphicsRequirements));
const ComPtr<IDXGIAdapter1> adapter = GetAdapter(graphicsRequirements.adapterLuid);
// Create a list of feature levels which are both supported by the OpenXR runtime and this application.
InitializeD3D12DeviceForAdapter(adapter.Get(), graphicsRequirements.minFeatureLevel, m_device.ReleaseAndGetAddressOf());
D3D12_COMMAND_QUEUE_DESC queueDesc = {};
queueDesc.Flags = D3D12_COMMAND_QUEUE_FLAG_NONE;
queueDesc.Type = D3D12_COMMAND_LIST_TYPE_DIRECT;
CHECK_HRCMD(m_device->CreateCommandQueue(&queueDesc, __uuidof(ID3D12CommandQueue),
reinterpret_cast<void**>(m_cmdQueue.ReleaseAndGetAddressOf())));
InitializeResources();
m_graphicsBinding.device = m_device.Get();
m_graphicsBinding.queue = m_cmdQueue.Get();
}
void InitializeResources() {
{
D3D12_DESCRIPTOR_HEAP_DESC heapDesc{};
heapDesc.NumDescriptors = 1;
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_RTV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
CHECK_HRCMD(m_device->CreateDescriptorHeap(&heapDesc, __uuidof(ID3D12DescriptorHeap),
reinterpret_cast<void**>(m_rtvHeap.ReleaseAndGetAddressOf())));
}
{
D3D12_DESCRIPTOR_HEAP_DESC heapDesc{};
heapDesc.NumDescriptors = 1;
heapDesc.Type = D3D12_DESCRIPTOR_HEAP_TYPE_DSV;
heapDesc.Flags = D3D12_DESCRIPTOR_HEAP_FLAG_NONE;
CHECK_HRCMD(m_device->CreateDescriptorHeap(&heapDesc, __uuidof(ID3D12DescriptorHeap),
reinterpret_cast<void**>(m_dsvHeap.ReleaseAndGetAddressOf())));
}
D3D12_ROOT_PARAMETER rootParams[2];
rootParams[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParams[0].Descriptor.ShaderRegister = 0;
rootParams[0].Descriptor.RegisterSpace = 0;
rootParams[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
rootParams[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_CBV;
rootParams[1].Descriptor.ShaderRegister = 1;
rootParams[1].Descriptor.RegisterSpace = 0;
rootParams[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_VERTEX;
D3D12_ROOT_SIGNATURE_DESC rootSignatureDesc{};
rootSignatureDesc.NumParameters = (UINT)ArraySize(rootParams);
rootSignatureDesc.pParameters = rootParams;
rootSignatureDesc.Flags = D3D12_ROOT_SIGNATURE_FLAG_ALLOW_INPUT_ASSEMBLER_INPUT_LAYOUT;
ComPtr<ID3DBlob> rootSignatureBlob;
ComPtr<ID3DBlob> error;
CHECK_HRCMD(D3D12SerializeRootSignature(&rootSignatureDesc, D3D_ROOT_SIGNATURE_VERSION_1_0,
rootSignatureBlob.ReleaseAndGetAddressOf(), error.ReleaseAndGetAddressOf()));
CHECK_HRCMD(m_device->CreateRootSignature(0, rootSignatureBlob->GetBufferPointer(), rootSignatureBlob->GetBufferSize(),
__uuidof(ID3D12RootSignature),
reinterpret_cast<void**>(m_rootSignature.ReleaseAndGetAddressOf())));
SwapchainImageContext initializeContext;
std::vector<XrSwapchainImageBaseHeader*> _ = initializeContext.Create(m_device.Get(), 1);
ComPtr<ID3D12GraphicsCommandList> cmdList;
CHECK_HRCMD(m_device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, initializeContext.GetCommandAllocator(), nullptr,
__uuidof(ID3D12GraphicsCommandList),
reinterpret_cast<void**>(cmdList.ReleaseAndGetAddressOf())));
ComPtr<ID3D12Resource> cubeVertexBufferUpload;
m_cubeVertexBuffer = CreateBuffer(m_device.Get(), sizeof(Geometry::c_cubeVertices), D3D12_HEAP_TYPE_DEFAULT);
{
cubeVertexBufferUpload = CreateBuffer(m_device.Get(), sizeof(Geometry::c_cubeVertices), D3D12_HEAP_TYPE_UPLOAD);
void* data;
const D3D12_RANGE readRange{0, 0};
CHECK_HRCMD(cubeVertexBufferUpload->Map(0, &readRange, &data));
memcpy(data, Geometry::c_cubeVertices, sizeof(Geometry::c_cubeVertices));
cubeVertexBufferUpload->Unmap(0, nullptr);
cmdList->CopyBufferRegion(m_cubeVertexBuffer.Get(), 0, cubeVertexBufferUpload.Get(), 0,
sizeof(Geometry::c_cubeVertices));
}
ComPtr<ID3D12Resource> cubeIndexBufferUpload;
m_cubeIndexBuffer = CreateBuffer(m_device.Get(), sizeof(Geometry::c_cubeIndices), D3D12_HEAP_TYPE_DEFAULT);
{
cubeIndexBufferUpload = CreateBuffer(m_device.Get(), sizeof(Geometry::c_cubeIndices), D3D12_HEAP_TYPE_UPLOAD);
void* data;
const D3D12_RANGE readRange{0, 0};
CHECK_HRCMD(cubeIndexBufferUpload->Map(0, &readRange, &data));
memcpy(data, Geometry::c_cubeIndices, sizeof(Geometry::c_cubeIndices));
cubeIndexBufferUpload->Unmap(0, nullptr);
cmdList->CopyBufferRegion(m_cubeIndexBuffer.Get(), 0, cubeIndexBufferUpload.Get(), 0, sizeof(Geometry::c_cubeIndices));
}
CHECK_HRCMD(cmdList->Close());
ID3D12CommandList* cmdLists[] = {cmdList.Get()};
m_cmdQueue->ExecuteCommandLists((UINT)ArraySize(cmdLists), cmdLists);
CHECK_HRCMD(m_device->CreateFence(m_fenceValue, D3D12_FENCE_FLAG_NONE, __uuidof(ID3D12Fence),
reinterpret_cast<void**>(m_fence.ReleaseAndGetAddressOf())));
m_fenceEvent = CreateEvent(nullptr, FALSE, FALSE, nullptr);
CHECK(m_fenceEvent != nullptr);
WaitForGpu();
}
// Select the preferred swapchain format from the list of available formats.
int64_t SelectColorSwapchainFormat(bool throwIfNotFound, span<const int64_t> imageFormatArray) const override {
// List of supported color swapchain formats.
return SelectSwapchainFormat( //
throwIfNotFound, imageFormatArray,
{
DXGI_FORMAT_R8G8B8A8_UNORM_SRGB,
DXGI_FORMAT_B8G8R8A8_UNORM_SRGB,
DXGI_FORMAT_R8G8B8A8_UNORM,
DXGI_FORMAT_B8G8R8A8_UNORM,
});
}
// Select the preferred swapchain format from the list of available formats.
int64_t SelectDepthSwapchainFormat(bool throwIfNotFound, span<const int64_t> imageFormatArray) const override {
// List of supported depth swapchain formats.
return SelectSwapchainFormat( //
throwIfNotFound, imageFormatArray,
{
DXGI_FORMAT_D32_FLOAT,
DXGI_FORMAT_D24_UNORM_S8_UINT,
DXGI_FORMAT_D16_UNORM,
DXGI_FORMAT_D32_FLOAT_S8X24_UINT,
});
}
const XrBaseInStructure* GetGraphicsBinding() const override {
return reinterpret_cast<const XrBaseInStructure*>(&m_graphicsBinding);
}
struct D3D12FallbackDepthTexture {
public:
D3D12FallbackDepthTexture() = default;
void Reset() {
m_texture = nullptr;
m_xrImage.texture = nullptr;
}
bool Allocated() const { return m_texture != nullptr; }
void Allocate(ID3D12Device* d3d12Device, UINT width, UINT height, uint16_t arraySize, UINT sampleCount,
D3D12_RESOURCE_DIMENSION dimension, uint64_t alignment, D3D12_TEXTURE_LAYOUT layout) {
Reset();
D3D12_HEAP_PROPERTIES heapProp{};
heapProp.Type = D3D12_HEAP_TYPE_DEFAULT;
heapProp.CPUPageProperty = D3D12_CPU_PAGE_PROPERTY_UNKNOWN;
heapProp.MemoryPoolPreference = D3D12_MEMORY_POOL_UNKNOWN;
D3D12_RESOURCE_DESC depthDesc{};
depthDesc.Dimension = dimension;
depthDesc.Alignment = alignment;
depthDesc.Width = width;
depthDesc.Height = height;
depthDesc.DepthOrArraySize = arraySize;
depthDesc.MipLevels = 1;
depthDesc.Format = DXGI_FORMAT_R32_TYPELESS;
depthDesc.SampleDesc.Count = sampleCount;
depthDesc.Layout = layout;
depthDesc.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL;
D3D12_CLEAR_VALUE clearValue{};
clearValue.DepthStencil.Depth = 1.0f;
clearValue.Format = DXGI_FORMAT_D32_FLOAT;
XRC_CHECK_THROW_HRCMD(d3d12Device->CreateCommittedResource(
&heapProp, D3D12_HEAP_FLAG_NONE, &depthDesc, D3D12_RESOURCE_STATE_DEPTH_WRITE, &clearValue,
__uuidof(ID3D12Resource), reinterpret_cast<void**>(m_texture.ReleaseAndGetAddressOf())));
XRC_CHECK_THROW_HRCMD(m_texture->SetName(L"hello_xr fallback depth tex"));
m_xrImage.texture = m_texture.Get();
}
const XrSwapchainImageD3D12KHR& GetTexture() const { return m_xrImage; }
private:
ComPtr<ID3D12Resource> m_texture{};
XrSwapchainImageD3D12KHR m_xrImage{XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR};
};
class D3D12SwapchainImageData : public SwapchainImageDataBase<XrSwapchainImageD3D12KHR> {
void init() {
XRC_CHECK_THROW_HRCMD(
m_d3d12Device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, __uuidof(ID3D12CommandAllocator),
reinterpret_cast<void**>(commandAllocator.ReleaseAndGetAddressOf())));
XRC_CHECK_THROW_HRCMD(commandAllocator->SetName(L"hello_xr swapchain command allocator"));
viewProjectionCBuffer =
D3D12CreateBuffer(m_d3d12Device.Get(), sizeof(ViewProjectionConstantBuffer), D3D12_HEAP_TYPE_UPLOAD);
XRC_CHECK_THROW_HRCMD(viewProjectionCBuffer->SetName(L"hello_xr view proj cbuffer"));
}
public:
D3D12SwapchainImageData(ComPtr<ID3D12Device> d3d12Device, uint32_t capacity, const XrSwapchainCreateInfo& createInfo)
: SwapchainImageDataBase(XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR, capacity, createInfo),
m_d3d12Device(std::move(d3d12Device)),
m_internalDepthTextures(capacity) {
init();
}
D3D12SwapchainImageData(ComPtr<ID3D12Device> d3d12Device, uint32_t capacity, const XrSwapchainCreateInfo& createInfo,
XrSwapchain depthSwapchain, const XrSwapchainCreateInfo& depthCreateInfo)
: SwapchainImageDataBase(XR_TYPE_SWAPCHAIN_IMAGE_D3D12_KHR, capacity, createInfo, depthSwapchain, depthCreateInfo),
m_d3d12Device(std::move(d3d12Device)),
m_internalDepthTextures(capacity) {
init();
}
void Reset() override {
m_internalDepthTextures.clear();
m_d3d12Device = nullptr;
SwapchainImageDataBase::Reset();
}
const XrSwapchainImageD3D12KHR& GetFallbackDepthSwapchainImage(uint32_t i) override {
if (!m_internalDepthTextures[i].Allocated()) {
XRC_CHECK_THROW(GetTypedImage(i).texture != nullptr);
/// @todo we should not need to do this introspection, I think
D3D12_RESOURCE_DESC colorDesc = GetTypedImage(i).texture->GetDesc();
m_internalDepthTextures[i].Allocate(m_d3d12Device.Get(), this->Width(), this->Height(), (uint16_t)this->ArraySize(),
this->SampleCount(), colorDesc.Dimension, colorDesc.Alignment,
colorDesc.Layout);
}
return m_internalDepthTextures[i].GetTexture();
}
ID3D12CommandAllocator* GetCommandAllocator() const { return commandAllocator.Get(); }
void ResetCommandAllocator() { XRC_CHECK_THROW_HRCMD(commandAllocator->Reset()); }
void RequestModelCBuffer(uint32_t requiredSize) {
if (!modelCBuffer || (requiredSize > modelCBuffer->GetDesc().Width)) {
modelCBuffer = D3D12CreateBuffer(m_d3d12Device.Get(), requiredSize, D3D12_HEAP_TYPE_UPLOAD);
XRC_CHECK_THROW_HRCMD(modelCBuffer->SetName(L"hello_xr model cbuffer"));
}
}
ID3D12Resource* GetModelCBuffer() const { return modelCBuffer.Get(); }
ID3D12Resource* GetViewProjectionCBuffer() const { return viewProjectionCBuffer.Get(); }
private:
ComPtr<ID3D12Device> m_d3d12Device;
ComPtr<ID3D12CommandAllocator> commandAllocator;
ComPtr<ID3D12Resource> modelCBuffer;
ComPtr<ID3D12Resource> viewProjectionCBuffer;
std::vector<D3D12FallbackDepthTexture> m_internalDepthTextures;
};
ISwapchainImageData* AllocateSwapchainImageData(size_t size, const XrSwapchainCreateInfo& swapchainCreateInfo) override {
auto d3d12Device = m_device;
auto typedResult = std::make_unique<D3D12SwapchainImageData>(d3d12Device.Get(), uint32_t(size), swapchainCreateInfo);
// Cast our derived type to the caller-expected type.
auto ret = static_cast<ISwapchainImageData*>(typedResult.get());
m_swapchainImageDataMap.Adopt(std::move(typedResult));
return ret;
}
ISwapchainImageData* AllocateSwapchainImageDataWithDepthSwapchain(
size_t size, const XrSwapchainCreateInfo& colorSwapchainCreateInfo, XrSwapchain depthSwapchain,
const XrSwapchainCreateInfo& depthSwapchainCreateInfo) override {
auto d3d12Device = m_device;
auto typedResult = std::make_unique<D3D12SwapchainImageData>(d3d12Device.Get(), uint32_t(size), colorSwapchainCreateInfo,
depthSwapchain, depthSwapchainCreateInfo);
// Cast our derived type to the caller-expected type.
auto ret = static_cast<ISwapchainImageData*>(typedResult.get());
m_swapchainImageDataMap.Adopt(std::move(typedResult));
return ret;
}
ID3D12PipelineState* GetOrCreatePipelineState(DXGI_FORMAT colorSwapchainFormat, DXGI_FORMAT depthSwapchainFormat) {
auto swapchainFormats = std::make_pair(colorSwapchainFormat, depthSwapchainFormat);
auto iter = m_pipelineStates.find(swapchainFormats);
if (iter != m_pipelineStates.end()) {
return iter->second.Get();
}
const D3D12_INPUT_ELEMENT_DESC inputElementDescs[] = {
{"POSITION", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT,
D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA, 0},
{"COLOR", 0, DXGI_FORMAT_R32G32B32_FLOAT, 0, D3D12_APPEND_ALIGNED_ELEMENT, D3D12_INPUT_CLASSIFICATION_PER_VERTEX_DATA,
0},
};
D3D12_GRAPHICS_PIPELINE_STATE_DESC pipelineStateDesc{};
pipelineStateDesc.pRootSignature = m_rootSignature.Get();
pipelineStateDesc.VS = {m_vertexShaderBytes->GetBufferPointer(), m_vertexShaderBytes->GetBufferSize()};
pipelineStateDesc.PS = {m_pixelShaderBytes->GetBufferPointer(), m_pixelShaderBytes->GetBufferSize()};
{
pipelineStateDesc.BlendState.AlphaToCoverageEnable = false;
pipelineStateDesc.BlendState.IndependentBlendEnable = false;
for (size_t i = 0; i < ArraySize(pipelineStateDesc.BlendState.RenderTarget); ++i) {
pipelineStateDesc.BlendState.RenderTarget[i].BlendEnable = false;
pipelineStateDesc.BlendState.RenderTarget[i].SrcBlend = D3D12_BLEND_ONE;
pipelineStateDesc.BlendState.RenderTarget[i].DestBlend = D3D12_BLEND_ZERO;
pipelineStateDesc.BlendState.RenderTarget[i].BlendOp = D3D12_BLEND_OP_ADD;
pipelineStateDesc.BlendState.RenderTarget[i].SrcBlendAlpha = D3D12_BLEND_ONE;
pipelineStateDesc.BlendState.RenderTarget[i].DestBlendAlpha = D3D12_BLEND_ZERO;
pipelineStateDesc.BlendState.RenderTarget[i].BlendOpAlpha = D3D12_BLEND_OP_ADD;
pipelineStateDesc.BlendState.RenderTarget[i].LogicOp = D3D12_LOGIC_OP_NOOP;
pipelineStateDesc.BlendState.RenderTarget[i].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
}
}
pipelineStateDesc.SampleMask = 0xFFFFFFFF;
{
pipelineStateDesc.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
pipelineStateDesc.RasterizerState.CullMode = D3D12_CULL_MODE_BACK;
pipelineStateDesc.RasterizerState.FrontCounterClockwise = FALSE;
pipelineStateDesc.RasterizerState.DepthBias = D3D12_DEFAULT_DEPTH_BIAS;
pipelineStateDesc.RasterizerState.DepthBiasClamp = D3D12_DEFAULT_DEPTH_BIAS_CLAMP;
pipelineStateDesc.RasterizerState.SlopeScaledDepthBias = D3D12_DEFAULT_SLOPE_SCALED_DEPTH_BIAS;
pipelineStateDesc.RasterizerState.DepthClipEnable = TRUE;
pipelineStateDesc.RasterizerState.MultisampleEnable = FALSE;
pipelineStateDesc.RasterizerState.AntialiasedLineEnable = FALSE;
pipelineStateDesc.RasterizerState.ForcedSampleCount = 0;
pipelineStateDesc.RasterizerState.ConservativeRaster = D3D12_CONSERVATIVE_RASTERIZATION_MODE_OFF;
}
{
pipelineStateDesc.DepthStencilState.DepthEnable = TRUE;
pipelineStateDesc.DepthStencilState.DepthWriteMask = D3D12_DEPTH_WRITE_MASK_ALL;
pipelineStateDesc.DepthStencilState.DepthFunc = D3D12_COMPARISON_FUNC_LESS;
pipelineStateDesc.DepthStencilState.StencilEnable = FALSE;
pipelineStateDesc.DepthStencilState.StencilReadMask = D3D12_DEFAULT_STENCIL_READ_MASK;
pipelineStateDesc.DepthStencilState.StencilWriteMask = D3D12_DEFAULT_STENCIL_WRITE_MASK;
pipelineStateDesc.DepthStencilState.FrontFace = pipelineStateDesc.DepthStencilState.BackFace = {
D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_STENCIL_OP_KEEP, D3D12_COMPARISON_FUNC_ALWAYS};
}
{
pipelineStateDesc.InputLayout.pInputElementDescs = inputElementDescs;
pipelineStateDesc.InputLayout.NumElements = (UINT)ArraySize(inputElementDescs);
}
pipelineStateDesc.IBStripCutValue = D3D12_INDEX_BUFFER_STRIP_CUT_VALUE_0xFFFF;
pipelineStateDesc.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
pipelineStateDesc.NumRenderTargets = 1;
pipelineStateDesc.RTVFormats[0] = colorSwapchainFormat;
pipelineStateDesc.DSVFormat = depthSwapchainFormat;
pipelineStateDesc.SampleDesc = {1, 0};
pipelineStateDesc.NodeMask = 0;
pipelineStateDesc.CachedPSO = {nullptr, 0};
pipelineStateDesc.Flags = D3D12_PIPELINE_STATE_FLAG_NONE;
ComPtr<ID3D12PipelineState> pipelineState;
CHECK_HRCMD(m_device->CreateGraphicsPipelineState(&pipelineStateDesc, __uuidof(ID3D12PipelineState),
reinterpret_cast<void**>(pipelineState.ReleaseAndGetAddressOf())));
ID3D12PipelineState* pipelineStateRaw = pipelineState.Get();
m_pipelineStates.emplace(swapchainFormats, std::move(pipelineState));
return pipelineStateRaw;
}
void RenderView(const XrCompositionLayerProjectionView& layerView, const XrSwapchainImageBaseHeader* swapchainImage,
int64_t colorSwapchainFormat, int64_t depthSwapchainFormat, const std::vector<Cube>& cubes) override {
CHECK(layerView.subImage.imageArrayIndex == 0); // Texture arrays not supported.
D3D12SwapchainImageData* swapchainData;
uint32_t imageIndex;
std::tie(swapchainData, imageIndex) = m_swapchainImageDataMap.GetDataAndIndexFromBasePointer(swapchainImage);
// XXX TODO
// CpuWaitForFence(swapchainContext.GetFrameFenceValue());
auto d3d12Device = m_device;
ComPtr<ID3D12GraphicsCommandList> cmdList;
XRC_CHECK_THROW_HRCMD(d3d12Device->CreateCommandList(
0, D3D12_COMMAND_LIST_TYPE_DIRECT, swapchainData->GetCommandAllocator(), nullptr, __uuidof(ID3D12GraphicsCommandList),
reinterpret_cast<void**>(cmdList.ReleaseAndGetAddressOf())));
ID3D12PipelineState* pipelineState =
GetOrCreatePipelineState((DXGI_FORMAT)colorSwapchainFormat, (DXGI_FORMAT)depthSwapchainFormat);
cmdList->SetPipelineState(pipelineState);
cmdList->SetGraphicsRootSignature(m_rootSignature.Get());
ID3D12Resource* const colorTexture = reinterpret_cast<const XrSwapchainImageD3D12KHR*>(swapchainImage)->texture;
const D3D12_RESOURCE_DESC colorTextureDesc = colorTexture->GetDesc();
const D3D12_VIEWPORT viewport = {(float)layerView.subImage.imageRect.offset.x,
(float)layerView.subImage.imageRect.offset.y,
(float)layerView.subImage.imageRect.extent.width,
(float)layerView.subImage.imageRect.extent.height,
0,
1};
cmdList->RSSetViewports(1, &viewport);
const D3D12_RECT scissorRect = {layerView.subImage.imageRect.offset.x, layerView.subImage.imageRect.offset.y,
layerView.subImage.imageRect.offset.x + layerView.subImage.imageRect.extent.width,
layerView.subImage.imageRect.offset.y + layerView.subImage.imageRect.extent.height};
cmdList->RSSetScissorRects(1, &scissorRect);
// Create RenderTargetView with original swapchain format (swapchain is typeless).
D3D12_CPU_DESCRIPTOR_HANDLE renderTargetView = m_rtvHeap->GetCPUDescriptorHandleForHeapStart();
D3D12_RENDER_TARGET_VIEW_DESC renderTargetViewDesc{};
renderTargetViewDesc.Format = (DXGI_FORMAT)colorSwapchainFormat;
if (colorTextureDesc.DepthOrArraySize > 1) {
if (colorTextureDesc.SampleDesc.Count > 1) {
renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMSARRAY;
renderTargetViewDesc.Texture2DMSArray.ArraySize = colorTextureDesc.DepthOrArraySize;
} else {
renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DARRAY;
renderTargetViewDesc.Texture2DArray.ArraySize = colorTextureDesc.DepthOrArraySize;
}
} else {
if (colorTextureDesc.SampleDesc.Count > 1) {
renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2DMS;
} else {
renderTargetViewDesc.ViewDimension = D3D12_RTV_DIMENSION_TEXTURE2D;
}
}
m_device->CreateRenderTargetView(colorTexture, &renderTargetViewDesc, renderTargetView);
ID3D12Resource* depthStencilTexture = swapchainData->GetDepthImageForColorIndex(imageIndex).texture;
const D3D12_RESOURCE_DESC depthStencilTextureDesc = depthStencilTexture->GetDesc();
D3D12_CPU_DESCRIPTOR_HANDLE depthStencilView = m_dsvHeap->GetCPUDescriptorHandleForHeapStart();
D3D12_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc{};
depthStencilViewDesc.Format = (DXGI_FORMAT)depthSwapchainFormat;
if (depthStencilTextureDesc.DepthOrArraySize > 1) {
if (depthStencilTextureDesc.SampleDesc.Count > 1) {
depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMSARRAY;
depthStencilViewDesc.Texture2DMSArray.ArraySize = colorTextureDesc.DepthOrArraySize;
} else {
depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DARRAY;
depthStencilViewDesc.Texture2DArray.ArraySize = colorTextureDesc.DepthOrArraySize;
}
} else {
if (depthStencilTextureDesc.SampleDesc.Count > 1) {
depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2DMS;
} else {
depthStencilViewDesc.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D;
}
}
m_device->CreateDepthStencilView(depthStencilTexture, &depthStencilViewDesc, depthStencilView);
// Clear swapchain and depth buffer. NOTE: This will clear the entire render target view, not just the specified view.
cmdList->ClearRenderTargetView(renderTargetView, static_cast<const FLOAT*>(m_clearColor.data()), 0, nullptr);
cmdList->ClearDepthStencilView(depthStencilView, D3D12_CLEAR_FLAG_DEPTH, 1.0f, 0, 0, nullptr);
D3D12_CPU_DESCRIPTOR_HANDLE renderTargets[] = {renderTargetView};
cmdList->OMSetRenderTargets((UINT)ArraySize(renderTargets), renderTargets, true, &depthStencilView);
const XMMATRIX spaceToView = XMMatrixInverse(nullptr, LoadXrPose(layerView.pose));
XrMatrix4x4f projectionMatrix;
XrMatrix4x4f_CreateProjectionFov(&projectionMatrix, GRAPHICS_D3D, layerView.fov, 0.05f, 100.0f);
// Set shaders and constant buffers.
ID3D12Resource* viewProjectionCBuffer = swapchainData->GetViewProjectionCBuffer();
ViewProjectionConstantBuffer viewProjection;
XMStoreFloat4x4(&viewProjection.ViewProjection, XMMatrixTranspose(spaceToView * LoadXrMatrix(projectionMatrix)));
{
void* data;
const D3D12_RANGE readRange{0, 0};
CHECK_HRCMD(viewProjectionCBuffer->Map(0, &readRange, &data));
memcpy(data, &viewProjection, sizeof(viewProjection));
viewProjectionCBuffer->Unmap(0, nullptr);
}
cmdList->SetGraphicsRootConstantBufferView(1, viewProjectionCBuffer->GetGPUVirtualAddress());
// Set cube primitive data.
const D3D12_VERTEX_BUFFER_VIEW vertexBufferView[] = {
{m_cubeVertexBuffer->GetGPUVirtualAddress(), sizeof(Geometry::c_cubeVertices), sizeof(Geometry::Vertex)}};
cmdList->IASetVertexBuffers(0, (UINT)ArraySize(vertexBufferView), vertexBufferView);
D3D12_INDEX_BUFFER_VIEW indexBufferView{m_cubeIndexBuffer->GetGPUVirtualAddress(), sizeof(Geometry::c_cubeIndices),
DXGI_FORMAT_R16_UINT};
cmdList->IASetIndexBuffer(&indexBufferView);
cmdList->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
constexpr uint32_t cubeCBufferSize = AlignTo<D3D12_CONSTANT_BUFFER_DATA_PLACEMENT_ALIGNMENT>(sizeof(ModelConstantBuffer));
swapchainData->RequestModelCBuffer(static_cast<uint32_t>(cubeCBufferSize * cubes.size()));
ID3D12Resource* modelCBuffer = swapchainData->GetModelCBuffer();
// Render each cube
uint32_t offset = 0;
for (const Cube& cube : cubes) {
// Compute and update the model transform.
ModelConstantBuffer model;
XMStoreFloat4x4(&model.Model,
XMMatrixTranspose(XMMatrixScaling(cube.Scale.x, cube.Scale.y, cube.Scale.z) * LoadXrPose(cube.Pose)));
{
uint8_t* data;
const D3D12_RANGE readRange{0, 0};
CHECK_HRCMD(modelCBuffer->Map(0, &readRange, reinterpret_cast<void**>(&data)));
memcpy(data + offset, &model, sizeof(model));
const D3D12_RANGE writeRange{offset, offset + cubeCBufferSize};
modelCBuffer->Unmap(0, &writeRange);
}
cmdList->SetGraphicsRootConstantBufferView(0, modelCBuffer->GetGPUVirtualAddress() + offset);
// Draw the cube.
cmdList->DrawIndexedInstanced((UINT)ArraySize(Geometry::c_cubeIndices), 1, 0, 0, 0);
offset += cubeCBufferSize;
}
CHECK_HRCMD(cmdList->Close());
ID3D12CommandList* cmdLists[] = {cmdList.Get()};
m_cmdQueue->ExecuteCommandLists((UINT)ArraySize(cmdLists), cmdLists);
SignalFence();
}
void SignalFence() {
++m_fenceValue;
CHECK_HRCMD(m_cmdQueue->Signal(m_fence.Get(), m_fenceValue));
}
void CpuWaitForFence(uint64_t fenceValue) {
if (m_fence->GetCompletedValue() < fenceValue) {
CHECK_HRCMD(m_fence->SetEventOnCompletion(fenceValue, m_fenceEvent));
const uint32_t retVal = WaitForSingleObjectEx(m_fenceEvent, INFINITE, FALSE);
if (retVal != WAIT_OBJECT_0) {
CHECK_HRCMD(E_FAIL);
}
}
}
void WaitForGpu() {
SignalFence();
CpuWaitForFence(m_fenceValue);
}
void UpdateOptions(const std::shared_ptr<Options>& options) override { m_clearColor = options->GetBackgroundClearColor(); }
private:
const ComPtr<ID3DBlob> m_vertexShaderBytes;
const ComPtr<ID3DBlob> m_pixelShaderBytes;
ComPtr<ID3D12Device> m_device;
ComPtr<ID3D12CommandQueue> m_cmdQueue;
ComPtr<ID3D12Fence> m_fence;
uint64_t m_fenceValue = 0;
HANDLE m_fenceEvent = INVALID_HANDLE_VALUE;
std::list<SwapchainImageContext> m_swapchainImageContexts;
// XXX std::map<const XrSwapchainImageBaseHeader*, SwapchainImageContext*> m_swapchainImageContextMap;
SwapchainImageDataMap<D3D12SwapchainImageData> m_swapchainImageDataMap;
XrGraphicsBindingD3D12KHR m_graphicsBinding{XR_TYPE_GRAPHICS_BINDING_D3D12_KHR};
ComPtr<ID3D12RootSignature> m_rootSignature;
std::map<std::pair<DXGI_FORMAT, DXGI_FORMAT>, ComPtr<ID3D12PipelineState>> m_pipelineStates;
ComPtr<ID3D12Resource> m_cubeVertexBuffer;
ComPtr<ID3D12Resource> m_cubeIndexBuffer;
ComPtr<ID3D12DescriptorHeap> m_rtvHeap;
ComPtr<ID3D12DescriptorHeap> m_dsvHeap;
std::array<float, 4> m_clearColor;
};
} // namespace
std::shared_ptr<IGraphicsPlugin> CreateGraphicsPlugin_D3D12(const std::shared_ptr<Options>& options,
std::shared_ptr<IPlatformPlugin> platformPlugin) {
return std::make_shared<D3D12GraphicsPlugin>(options, platformPlugin);
}
#endif