-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
315 lines (262 loc) · 11 KB
/
Copy pathRenderer.cpp
File metadata and controls
315 lines (262 loc) · 11 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
#include "Renderer.h"
#include "DX12Lib/Helpers.h"
#include "Mock12.h"
Renderer::~Renderer()
{
Reset();
}
void Renderer::init()
{
dxgiAdapter = GetAdapter(false);
if (dxgiAdapter)
{
device = CreateDevice(dxgiAdapter);
}
if (device)
{
directCommandQueue = std::make_shared<CommandQueue>(device, D3D12_COMMAND_LIST_TYPE_DIRECT);
computeCommandQueue = std::make_shared<CommandQueue>(device, D3D12_COMMAND_LIST_TYPE_COMPUTE);
copyCommandQueue = std::make_shared<CommandQueue>(device, D3D12_COMMAND_LIST_TYPE_COPY);
copyCommandList = copyCommandQueue->GetCommandList().Get();
tearingSupported = CheckTearingSupport();
char buffer[512];
sprintf_s(buffer, "D3D12 Init: RayTracing: %d\n", (int)IsRayTracingSupported());
OutputDebugStringA(buffer);
}
// ray tracing
ThrowIfFailed(device->QueryInterface(IID_PPV_ARGS(&dxrDevice)), L"Couldn't get DirectX Raytracing interface for the device.\n");
// Allocate a heap for a large number of descriptors
descriptorHeapR.CreateDescriptorHeap(*this, 1024 * 8, D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE);
descriptorHeapR.maxSize = device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV);
}
void Renderer::UpdateBufferResource(
ID3D12GraphicsCommandList2* commandList,
ID3D12Resource** pDestinationResource,
ID3D12Resource** pIntermediateResource,
size_t numElements, size_t elementSize, const void* bufferData,
D3D12_RESOURCE_FLAGS flags)
{
size_t bufferSize = numElements * elementSize;
CD3DX12_HEAP_PROPERTIES a(D3D12_HEAP_TYPE_DEFAULT);
CD3DX12_RESOURCE_DESC b = CD3DX12_RESOURCE_DESC::Buffer(bufferSize, flags);
// Create a committed resource for the GPU resource in a default heap.
ThrowIfFailed(device->CreateCommittedResource(
&a,
D3D12_HEAP_FLAG_NONE,
&b,
D3D12_RESOURCE_STATE_COMMON,
nullptr,
IID_PPV_ARGS(pDestinationResource)));
// Create an committed resource for the upload.
if (bufferData)
{
CD3DX12_HEAP_PROPERTIES a(D3D12_HEAP_TYPE_UPLOAD);
CD3DX12_RESOURCE_DESC b = CD3DX12_RESOURCE_DESC::Buffer(bufferSize);
ThrowIfFailed(device->CreateCommittedResource(
&a,
D3D12_HEAP_FLAG_NONE,
&b,
D3D12_RESOURCE_STATE_GENERIC_READ,
nullptr,
IID_PPV_ARGS(pIntermediateResource)));
{
CD3DX12_RESOURCE_BARRIER barrier = CD3DX12_RESOURCE_BARRIER::Transition(*pDestinationResource, D3D12_RESOURCE_STATE_COMMON, D3D12_RESOURCE_STATE_COPY_DEST);
commandList->ResourceBarrier(1, &barrier);
}
D3D12_SUBRESOURCE_DATA subresourceData = {};
subresourceData.pData = bufferData;
subresourceData.RowPitch = bufferSize;
subresourceData.SlicePitch = subresourceData.RowPitch;
UpdateSubresources(commandList,
*pDestinationResource, *pIntermediateResource,
0, 0, 1, &subresourceData);
}
}
ComPtr<IDXGIAdapter4> Renderer::GetAdapter(bool bUseWarp)
{
ComPtr<IDXGIFactory4> dxgiFactory;
UINT createFactoryFlags = 0;
#if defined(_DEBUG)
createFactoryFlags = DXGI_CREATE_FACTORY_DEBUG;
#endif
ThrowIfFailed(CreateDXGIFactory2(createFactoryFlags, IID_PPV_ARGS(&dxgiFactory)));
ComPtr<IDXGIAdapter1> dxgiAdapter1;
ComPtr<IDXGIAdapter4> dxgiAdapter4;
if (bUseWarp)
{
ThrowIfFailed(dxgiFactory->EnumWarpAdapter(IID_PPV_ARGS(&dxgiAdapter1)));
ThrowIfFailed(dxgiAdapter1.As(&dxgiAdapter4));
}
else
{
SIZE_T maxDedicatedVideoMemory = 0;
for (UINT i = 0; dxgiFactory->EnumAdapters1(i, &dxgiAdapter1) != DXGI_ERROR_NOT_FOUND; ++i)
{
DXGI_ADAPTER_DESC1 dxgiAdapterDesc1;
dxgiAdapter1->GetDesc1(&dxgiAdapterDesc1);
// Check to see if the adapter can create a D3D12 device without actually
// creating it. The adapter with the largest dedicated video memory
// is favored.
if ((dxgiAdapterDesc1.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) == 0 &&
SUCCEEDED(D3D12CreateDevice(dxgiAdapter1.Get(),
D3D_FEATURE_LEVEL_11_0, __uuidof(ID3D12Device), nullptr)) &&
dxgiAdapterDesc1.DedicatedVideoMemory > maxDedicatedVideoMemory)
{
maxDedicatedVideoMemory = dxgiAdapterDesc1.DedicatedVideoMemory;
ThrowIfFailed(dxgiAdapter1.As(&dxgiAdapter4));
}
}
}
return dxgiAdapter4;
}
ComPtr<ID3D12Device2> Renderer::CreateDevice(ComPtr<IDXGIAdapter4> adapter)
{
ComPtr<ID3D12Device2> _d3d12Device2;
ThrowIfFailed(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&_d3d12Device2)));
// todo: use define
// use Mock12
// ComPtr<ID3D12Device2> d3d12Device2 = new Mock12Device2(_d3d12Device2);
// don't use Mock12
ComPtr<ID3D12Device2> d3d12Device2 = _d3d12Device2;
// NAME_D3D12_OBJECT(d3d12Device2);
// Enable debug messages in debug mode.
#if defined(_DEBUG)
ComPtr<ID3D12InfoQueue> pInfoQueue;
if (SUCCEEDED(d3d12Device2.As(&pInfoQueue)))
{
pInfoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_CORRUPTION, TRUE);
pInfoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_ERROR, TRUE);
pInfoQueue->SetBreakOnSeverity(D3D12_MESSAGE_SEVERITY_WARNING, TRUE);
// Suppress whole categories of messages
//D3D12_MESSAGE_CATEGORY Categories[] = {};
// Suppress messages based on their severity level
D3D12_MESSAGE_SEVERITY Severities[] =
{
D3D12_MESSAGE_SEVERITY_INFO
};
// Suppress individual messages by their ID
D3D12_MESSAGE_ID DenyIds[] = {
D3D12_MESSAGE_ID_CLEARRENDERTARGETVIEW_MISMATCHINGCLEARVALUE, // I'm really not sure how to avoid this message.
D3D12_MESSAGE_ID_MAP_INVALID_NULLRANGE, // This warning occurs when using capture frame while graphics debugging.
D3D12_MESSAGE_ID_UNMAP_INVALID_NULLRANGE, // This warning occurs when using capture frame while graphics debugging.
};
D3D12_INFO_QUEUE_FILTER NewFilter = {};
//NewFilter.DenyList.NumCategories = _countof(Categories);
//NewFilter.DenyList.pCategoryList = Categories;
NewFilter.DenyList.NumSeverities = _countof(Severities);
NewFilter.DenyList.pSeverityList = Severities;
NewFilter.DenyList.NumIDs = _countof(DenyIds);
NewFilter.DenyList.pIDList = DenyIds;
ThrowIfFailed(pInfoQueue->PushStorageFilter(&NewFilter));
}
#endif
// Determine maximum supported feature level for this device
static const D3D_FEATURE_LEVEL s_featureLevels[] =
{
D3D_FEATURE_LEVEL_12_1,
D3D_FEATURE_LEVEL_12_0,
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
};
D3D12_FEATURE_DATA_FEATURE_LEVELS featLevels =
{
_countof(s_featureLevels), s_featureLevels, D3D_FEATURE_LEVEL_11_0
};
HRESULT hr = d3d12Device2->CheckFeatureSupport(D3D12_FEATURE_FEATURE_LEVELS, &featLevels, sizeof(featLevels));
if (SUCCEEDED(hr))
{
D3D_FEATURE_LEVEL d3dFeatureLevel = featLevels.MaxSupportedFeatureLevel;
char buffer[512];
sprintf_s(buffer, "D3D12 d3dFeatureLevel = %x\n", (int)d3dFeatureLevel);
OutputDebugStringA(buffer);
// D3D_FEATURE_LEVEL_12_1 0xc100 Intel Arc
}
else
{
// m_d3dFeatureLevel = m_d3dMinFeatureLevel;
}
return d3d12Device2;
}
bool Renderer::CheckTearingSupport()
{
BOOL allowTearing = FALSE;
// Rather than create the DXGI 1.5 factory interface directly, we create the
// DXGI 1.4 interface and query for the 1.5 interface. This is to enable the
// graphics debugging tools which will not support the 1.5 factory interface
// until a future update.
ComPtr<IDXGIFactory4> factory4;
if (SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory4))))
{
ComPtr<IDXGIFactory5> factory5;
if (SUCCEEDED(factory4.As(&factory5)))
{
factory5->CheckFeatureSupport(DXGI_FEATURE_PRESENT_ALLOW_TEARING,
&allowTearing, sizeof(allowTearing));
}
}
return allowTearing == TRUE;
}
bool Renderer::IsRayTracingSupported() const
{
if (!device)
return false;
D3D12_FEATURE_DATA_D3D12_OPTIONS5 featureSupportData = {};
return SUCCEEDED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS5, &featureSupportData, sizeof(featureSupportData)))
&& featureSupportData.RaytracingTier != D3D12_RAYTRACING_TIER_NOT_SUPPORTED;
}
UINT Renderer::GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE type) const
{
return device->GetDescriptorHandleIncrementSize(type);
}
void Renderer::Flush()
{
directCommandQueue->Flush();
computeCommandQueue->Flush();
copyCommandQueue->Flush();
}
void Renderer::Reset()
{
}
UINT Renderer::CreateBufferSRV(D3DBuffer* buffer, UINT numElements, UINT elementSize)
{
// SRV
D3D12_SHADER_RESOURCE_VIEW_DESC srvDesc = {};
srvDesc.ViewDimension = D3D12_SRV_DIMENSION_BUFFER;
srvDesc.Shader4ComponentMapping = D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING;
srvDesc.Buffer.NumElements = numElements;
if (elementSize == 0)
{
srvDesc.Format = DXGI_FORMAT_R32_TYPELESS;
srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_RAW;
srvDesc.Buffer.StructureByteStride = 0;
}
else
{
srvDesc.Format = DXGI_FORMAT_UNKNOWN;
srvDesc.Buffer.Flags = D3D12_BUFFER_SRV_FLAG_NONE;
srvDesc.Buffer.StructureByteStride = elementSize;
}
UINT descriptorIndex = descriptorHeapR.AllocateDescriptor(&buffer->cpuDescriptorHandle);
device->CreateShaderResourceView(buffer->resource.Get(), &srvDesc, buffer->cpuDescriptorHandle);
buffer->gpuDescriptorHandle = CD3DX12_GPU_DESCRIPTOR_HANDLE(descriptorHeapR.descriptorHeap->GetGPUDescriptorHandleForHeapStart(), descriptorIndex, descriptorHeapR.maxSize);
return descriptorIndex;
}
uint32 Renderer::GetFormatBitsPerPixel(DXGI_FORMAT dxgiFormat)
{
if (dxgiFormat == DXGI_FORMAT_R32G32B32A32_FLOAT) return 128;
else if (dxgiFormat == DXGI_FORMAT_R16G16B16A16_FLOAT) return 64;
else if (dxgiFormat == DXGI_FORMAT_R16G16B16A16_UNORM) return 64;
else if (dxgiFormat == DXGI_FORMAT_R8G8B8A8_UNORM) return 32;
else if (dxgiFormat == DXGI_FORMAT_B8G8R8A8_UNORM) return 32;
else if (dxgiFormat == DXGI_FORMAT_B8G8R8X8_UNORM) return 32;
else if (dxgiFormat == DXGI_FORMAT_R10G10B10_XR_BIAS_A2_UNORM) return 32;
else if (dxgiFormat == DXGI_FORMAT_R10G10B10A2_UNORM) return 32;
else if (dxgiFormat == DXGI_FORMAT_B5G5R5A1_UNORM) return 16;
else if (dxgiFormat == DXGI_FORMAT_B5G6R5_UNORM) return 16;
else if (dxgiFormat == DXGI_FORMAT_R32_FLOAT) return 32;
else if (dxgiFormat == DXGI_FORMAT_R16_FLOAT) return 16;
else if (dxgiFormat == DXGI_FORMAT_R16_UNORM) return 16;
else if (dxgiFormat == DXGI_FORMAT_R8_UNORM) return 8;
else if (dxgiFormat == DXGI_FORMAT_A8_UNORM) return 8;
return 0;
}