1
+ // --------------------------------------------------------------------------------------------------
2
+ // Revolution Engine
3
+ // --------------------------------------------------------------------------------------------------
4
+ // Copyright 2023 Carmelo J Fdez-Aguera
5
+ //
6
+ // Permission is hereby granted, free of charge, to any person obtaining a copy of this software
7
+ // and associated documentation files (the "Software"), to deal in the Software without restriction,
8
+ // including without limitation the rights to use, copy, modify, merge, publish, distribute,
9
+ // sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
10
+ // furnished to do so, subject to the following conditions:
11
+ //
12
+ // The above copyright notice and this permission notice shall be included in all copies or
13
+ // substantial portions of the Software.
14
+ //
15
+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
16
+ // NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18
+ // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19
+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
+ #include " ContextDX12.h"
21
+
22
+ #include " d3dx12.h"
23
+ #include < dxgi1_6.h>
24
+
25
+ #include < wrl.h>
26
+ #include < core/tools/log.h>
27
+
28
+ template <class T >
29
+ using ComPtr = Microsoft::WRL::ComPtr<T>;
30
+
31
+ namespace rev ::gfx
32
+ {
33
+ bool ContextDX12::init (bool useValidationLayers)
34
+ {
35
+ if (!initPhysicalDevice (useValidationLayers))
36
+ {
37
+ return false ;
38
+ }
39
+
40
+ if (!initLogicalDevice (useValidationLayers))
41
+ {
42
+ return false ;
43
+ }
44
+
45
+ return true ;
46
+ }
47
+
48
+ bool ContextDX12::initPhysicalDevice (bool useValidationLayers)
49
+ {
50
+ // Create device factory
51
+ ComPtr<IDXGIFactory6> dxgiFactory;
52
+
53
+ UINT factoryFlags = 0 ;
54
+ if (useValidationLayers)
55
+ {
56
+ factoryFlags |= DXGI_CREATE_FACTORY_DEBUG;
57
+ }
58
+
59
+ CreateDXGIFactory2 (factoryFlags, IID_PPV_ARGS (&dxgiFactory));
60
+
61
+ // Iterate over available adapters
62
+ ComPtr<IDXGIAdapter1> dxgiAdapter1;
63
+
64
+ if (dxgiFactory->EnumAdapterByGpuPreference (0 ,
65
+ DXGI_GPU_PREFERENCE_HIGH_PERFORMANCE,
66
+ IID_PPV_ARGS (&dxgiAdapter1)) == DXGI_ERROR_NOT_FOUND)
67
+ {
68
+ return false ;
69
+ }
70
+
71
+ // TODO: Get device info
72
+ // DXGI_ADAPTER_DESC1 adapterDesc;
73
+ // dxgiAdapter1->GetDesc1(&adapterDesc);
74
+ // std::wstring descMsg = adapterDesc.Description;
75
+ // std::cout << descMsg << std::endl;
76
+
77
+ dxgiAdapter1.As (&m_dxgiAdapter);
78
+ return true ;
79
+ }
80
+
81
+ bool ContextDX12::initLogicalDevice (bool breakOnValidation)
82
+ {
83
+ if (breakOnValidation)
84
+ {
85
+
86
+ ComPtr<ID3D12Debug> spDebugController0;
87
+ ComPtr<ID3D12Debug1> spDebugController1;
88
+ if (!SUCCEEDED (D3D12GetDebugInterface (IID_PPV_ARGS (&spDebugController0))))
89
+ {
90
+ return false ;
91
+ }
92
+ if (!SUCCEEDED (spDebugController0->QueryInterface (IID_PPV_ARGS (&spDebugController1))))
93
+ {
94
+ return false ;
95
+ }
96
+ spDebugController1->SetEnableGPUBasedValidation (true );
97
+ spDebugController1->EnableDebugLayer ();
98
+ }
99
+
100
+ ComPtr<ID3D12Device2> d3d12Device;
101
+ auto hRes = D3D12CreateDevice (m_dxgiAdapter.Get (), D3D_FEATURE_LEVEL_12_1, IID_PPV_ARGS (&d3d12Device));
102
+ if (!SUCCEEDED (hRes))
103
+ {
104
+ return false ;
105
+ }
106
+
107
+ if (breakOnValidation)
108
+ {
109
+ ComPtr<ID3D12InfoQueue> pInfoQueue;
110
+ if (SUCCEEDED (d3d12Device->QueryInterface (IID_PPV_ARGS (&pInfoQueue))))
111
+ {
112
+ pInfoQueue->SetBreakOnSeverity (D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE );
113
+ pInfoQueue->SetBreakOnSeverity (D3D12_MESSAGE_SEVERITY_ERROR, TRUE );
114
+ pInfoQueue->SetBreakOnSeverity (D3D12_MESSAGE_SEVERITY_WARNING, TRUE );
115
+
116
+ // D3D12_MESSAGE_CATEGORY Categories[] = {};
117
+ D3D12_MESSAGE_SEVERITY Severities[] =
118
+ {
119
+ D3D12_MESSAGE_SEVERITY_INFO
120
+ };
121
+
122
+ // Suppress individual messages by their ID
123
+ // D3D12_MESSAGE_ID DenyIds[] = {};
124
+
125
+ D3D12_INFO_QUEUE_FILTER NewFilter = {};
126
+ // NewFilter.DenyList.NumCategories = _countof(Categories);
127
+ // NewFilter.DenyList.pCategoryList = Categories;
128
+ NewFilter.DenyList .NumSeverities = _countof (Severities);
129
+ NewFilter.DenyList .pSeverityList = Severities;
130
+ // NewFilter.DenyList.NumIDs = _countof(DenyIds);
131
+ // NewFilter.DenyList.pIDList = DenyIds;
132
+
133
+ if (!SUCCEEDED (pInfoQueue->PushStorageFilter (&NewFilter)))
134
+ {
135
+ return false ;
136
+ }
137
+ }
138
+ else
139
+ {
140
+ std::cout << " Unable to set dx12 validation\n " ;
141
+
142
+ return true ;
143
+ }
144
+ }
145
+
146
+ return true ;
147
+ }
148
+ }
0 commit comments