-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirect3D.cpp
More file actions
521 lines (455 loc) · 24.1 KB
/
Copy pathDirect3D.cpp
File metadata and controls
521 lines (455 loc) · 24.1 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
// ===========================================================================
/// <summary>
/// Direct3D.cpp
/// DirectXIntroduction
/// created by Mehrdad Soleimanimajd on 19.07.2019
/// </summary>
/// <created>ʆϒʅ, 19.07.2019</created>
/// <changed>ʆϒʅ, 04.07.2023</changed>
// ===========================================================================
#include "Direct3D.h"
Direct3D::Direct3D (TheCore* coreObj) :
core (coreObj), colourFormat (DXGI_FORMAT_B8G8R8A8_UNORM),
// reserve 8 bits for red, green, blue and transparency each in unsigned normalized integer
displayModesCount (0), displayModeIndex (0), videoCardMemory (0), videoCardDescription (L""),
fullscreen (false), vSync (false), initialized (false), allocated (false)
{
try
{
HRESULT hR; // functions return value
unsigned long rC {0}; // reference counts
fullscreen = PointerProvider::getConfiguration ()->getSettings ().fullscreen;
//vSync = PointerProvider::getConfiguration ()->getSettings ().vsync;
// DXGI (DirectX graphics interface) factory creation
IDXGIFactory1* dxgiFactory; // DXGI factory
hR = CreateDXGIFactory1 (__uuidof(IDXGIFactory1), (void**) &dxgiFactory);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of DXGI factory failed!");
return;
}
// adapter creation for physical graphics interface using DXGI factory (0: primary device)
IDXGIAdapter1* dxgiAdapter; // DXGI adapter
hR = dxgiFactory->EnumAdapters1 (0, &dxgiAdapter);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of DXGI adapter failed!");
return;
}
// primary output adapter (monitor) enumeration
IDXGIOutput* dxgiOutput; // DXGI output
hR = dxgiAdapter->EnumOutputs (0, &dxgiOutput);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Acquiring the output device failed!");
return;
}
// two calls to enumerate the number and supported monitor display modes for the colour format:
// -- the number of supported display modes (refer to MSDN for flags parameter)
hR = dxgiOutput->GetDisplayModeList (colourFormat, DXGI_ENUM_MODES_INTERLACED, &displayModesCount, nullptr);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Acquiring the number of supported display modes failed!");
return;
}
// -- acquiring all the supported display modes for current monitor / video card combination (refer to MSDN for flags parameter)
displayModes = new (std::nothrow) DXGI_MODE_DESC [displayModesCount];
if (!displayModes)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Dynamic memory allocation for supported display modes failed!");
return;
}
hR = dxgiOutput->GetDisplayModeList (colourFormat, DXGI_ENUM_MODES_INTERLACED, &displayModesCount, displayModes);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Acquiring all the supported display modes failed!");
return;
}
for (unsigned int i = 0; i < displayModesCount; i++)
{
// support check for current resolution of the client window (input streamed from setting file)
if (displayModes [i].Width == core->appWindow->getWidth ())
if (displayModes [i].Height == core->appWindow->getHeight ())
{
displayMode = displayModes [i];
displayModeIndex = i;
break;
} else
{
// not supported: set to the lowest supported resolution
displayMode = displayModes [0];
displayModeIndex = 0;
PointerProvider::getFileLogger ()->push (logType::warning, std::this_thread::get_id (), L"mainThread",
L"The chosen resolution is not supported!");
// rewrite a not valid configurations with defaults: the file is probably modified from outside of the application
if (!PointerProvider::getConfiguration ()->apply (PointerProvider::getConfiguration ()->getDefaults ()))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Rewriting the Configuration file with default settings failed.");
}
}
}
rC = dxgiOutput->Release ();
dxgiOutput = nullptr;
// acquiring the video card description
DXGI_ADAPTER_DESC1 dxgiAdapterDesc;
hR = dxgiAdapter->GetDesc1 (&dxgiAdapterDesc);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Acquiring the description of video card failed!");
return;
}
videoCardMemory = (unsigned int) (dxgiAdapterDesc.DedicatedVideoMemory / 1024 / 1024);
videoCardDescription = dxgiAdapterDesc.Description;
rC = dxgiAdapter->Release ();
dxgiAdapter = nullptr;
// flag: needed to get Direct2D interoperability with Direct3D resources
unsigned int deviceFlags = D3D10_CREATE_DEVICE_BGRA_SUPPORT;
// device creation debug option flag
#ifndef _NOT_DEBUGGING
deviceFlags |= D3D10_CREATE_DEVICE_DEBUG; // creation with debug layer
#endif // !_NOT_DEBUGGING
// creation of the device
// first parameter: pointer to the present adapter on system
D3D10_FEATURE_LEVEL1 featureLevel {D3D10_FEATURE_LEVEL_10_1};
hR = D3D10CreateDevice1 (nullptr, D3D10_DRIVER_TYPE_HARDWARE, NULL,
deviceFlags, featureLevel, D3D10_1_SDK_VERSION, &device);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"The creation of Direct3D device failed!");
return;
}
#ifndef _NOT_DEBUGGING
// acquiring the device's debug layer
// note that live report is available from Direct3D 11
hR = device->QueryInterface (__uuidof(ID3D10Debug), &debug);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"The creation of Direct3D device debug layer failed!");
return;
}
#endif // !_NOT_DEBUGGING
// filling a swap chain description structure (the type of swap chain)
DXGI_SWAP_CHAIN_DESC swapChainDesc;
swapChainDesc.BufferDesc.Width = 0; // back buffer size, 0: automatic adjustment from already calculated client window area
swapChainDesc.BufferDesc.Height = 0; // the same
if (vSync) // lock to system settings 60Hz
{
// back buffer to front buffer (screen) draw rate
swapChainDesc.BufferDesc.RefreshRate.Numerator = displayMode.RefreshRate.Numerator;
swapChainDesc.BufferDesc.RefreshRate.Denominator = displayMode.RefreshRate.Denominator;
} else // draw as many times as possible (may cause some visual artefacts)
{
// note that not supported values may cause the DirectX to perform a blit instead of a buffer flip,
// ending in degraded performance and unknown errors when debugging.
swapChainDesc.BufferDesc.RefreshRate.Numerator = 0;
swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
}
swapChainDesc.BufferDesc.Format = colourFormat; // display format
swapChainDesc.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED; // scan-line drawing
swapChainDesc.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED; // image size adjustment to back buffer resolution
// number of multi samplings per pixel and image quality (1 and 0: disable multi sampling (no anti-aliasing))
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // back buffer as render output target
swapChainDesc.BufferCount = 3; // including the front buffer (one front buffer and two back buffers)
swapChainDesc.OutputWindow = core->getHandle (); // handle to main window
swapChainDesc.Windowed = true; // recommendation: windowed creation and switch to full screen
// flip (in windowed mode: blit) and discard the content of back buffer after presentation
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD;
swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow switching the display mode (advanced)
// swap chain creation
hR = dxgiFactory->CreateSwapChain (device.Get (), &swapChainDesc, &swapChain);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of swap chain failed!");
return;
}
rC = dxgiFactory->Release ();
dxgiFactory = nullptr;
initialized = true;
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"Direct3D is successfully initialized.");
displayModeSetter ();
allocateResources ();
if (!allocated)
PointerProvider::getFileLogger ()->push (logType::info, std::this_thread::get_id (), L"mainThread",
L"Allocation of Direct3D resources failed.");
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
const bool& Direct3D::isInitialized (void)
{
return initialized;
};
const ID3D10Device1& Direct3D::getDevice (void)
{
return *device.Get ();
};
const bool& Direct3D::isFullscreen (void)
{
return fullscreen;
};
void Direct3D::displayModeSetter (void)
{
try
{
HRESULT hR;
if (fullscreen)
{
// switch to fullscreen mode
hR = swapChain->SetFullscreenState (true, nullptr);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Switching to fullscreen mode failed.");
fullscreen = false;
return;
}
} else
{
// switch to windowed mode
hR = swapChain->SetFullscreenState (false, nullptr);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Switching to windowed mode failed.");
fullscreen = true;
return;
}
}
// setting the resolution
hR = swapChain->ResizeTarget (&displayMode);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Setting a supported resolution failed.");
}
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void Direct3D::allocateResources (void)
{
try
{
allocated = false;
HRESULT hR;
unsigned long rC {0};
// resizing the swap chain buffers (on resize of the client window)
// BufferCount and SwapChainFlags: 0 do not change the current
// 0 for the next two parameters to adjust to the current client window size automatically
// next parameter: set to DXGI_FORMAT_UNKNOWN to preserve the current
hR = swapChain->ResizeBuffers (0, 0, 0, DXGI_FORMAT_UNKNOWN, 0);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Resizing the swap chain failed!");
return;
}
// obtain a pointer to the current back buffer of swap chain
// the zero-th buffer is accessible, since already created using flip discarding effect.
// second parameter: interface type (most cases 2D- texture)
// the last parameter returns a pointer to the actual back buffer
ID3D10Texture2D* rtBuffer; // render target view buffer
hR = swapChain->GetBuffer (0, __uuidof(ID3D10Texture2D), (LPVOID*) &rtBuffer);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Acquiring the back buffer failed!");
return;
}
// render target view creation (attach the obtained back buffer to swap chain)
// first parameter: the resource for which the render target is created for
// second parameter describes data type of the specified resource (mipmap but 0 for now)
// the last parameter returns a pointer to the created render target view
hR = device->CreateRenderTargetView (rtBuffer, nullptr, &rTview);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of render target view failed!");
return;
}
// depth-stencil buffer creation
// depth buffer purpose: to render polygons properly in 3D space
// stencil buffer purpose: to achieve effects such as motion blur, volumetric shadows etc.
CD3D10_TEXTURE2D_DESC depthBufferDesc;
rtBuffer->GetDesc (&depthBufferDesc); // retrieves the back buffer description and fill
//descDepth.Width = ;
//descDepth.Height = ;
//descDepth.MipLevels = 1;
//descDepth.ArraySize = 1;
depthBufferDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT; // 24 bits for depth and 8 bits for stencil
//descDepth.SampleDesc.Count = 1; // multi-sampling (anti-aliasing) match to settings of render target
//descDepth.SampleDesc.Quality = 0;
depthBufferDesc.Usage = D3D10_USAGE_DEFAULT; // value: only GPU will be reading and writing to the resource
depthBufferDesc.BindFlags = D3D10_BIND_DEPTH_STENCIL; // how to bind to the different pipeline stages
depthBufferDesc.CPUAccessFlags = 0;
depthBufferDesc.MiscFlags = 0;
rC = rtBuffer->Release ();
rtBuffer = nullptr;
// texture creation:
// the second parameter: pointer to initial data (zero for any data, since depth-stencil buffer)
// note texture 2d function: sorted and rasterized polygons are just coloured pixels in 2d representation
hR = device->CreateTexture2D (&depthBufferDesc, nullptr, &dSbuffer);
//hR = dsBuffer->QueryInterface ( __uuidof(IDXGISurface1), &dsSurface );
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of depth-stencil buffer failed!");
return;
}
// depth-stencil state description
// to control the depth test and its type, that Direct3D performs for each pixel
D3D10_DEPTH_STENCIL_DESC depthStencilDesc;
depthStencilDesc.DepthEnable = true;
depthStencilDesc.DepthWriteMask = D3D10_DEPTH_WRITE_MASK_ALL;
depthStencilDesc.DepthFunc = D3D10_COMPARISON_LESS;
depthStencilDesc.StencilEnable = true;
depthStencilDesc.StencilReadMask = 0xFF;
depthStencilDesc.StencilWriteMask = 0xFF;
// stencil operations (if pixel is front facing)
depthStencilDesc.FrontFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilDepthFailOp = D3D10_STENCIL_OP_INCR;
depthStencilDesc.FrontFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
depthStencilDesc.FrontFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// stencil operations (if pixel is front facing)
depthStencilDesc.BackFace.StencilFailOp = D3D10_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilDepthFailOp = D3D10_STENCIL_OP_DECR;
depthStencilDesc.BackFace.StencilPassOp = D3D10_STENCIL_OP_KEEP;
depthStencilDesc.BackFace.StencilFunc = D3D10_COMPARISON_ALWAYS;
// depth stencil state creation
hR = device->CreateDepthStencilState (&depthStencilDesc, &dSstate);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation of depth-stencil state failed!");
return;
}
// set the active depth stencil state
device->OMSetDepthStencilState (dSstate.Get (), 1);
// depth-stencil view description
// purpose: so the Direct3D use the depth buffer as a depth stencil texture.
D3D10_DEPTH_STENCIL_VIEW_DESC depthStencilViewDesc;
depthStencilViewDesc.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
depthStencilViewDesc.ViewDimension = D3D10_DSV_DIMENSION_TEXTURE2D;
depthStencilViewDesc.Texture2D.MipSlice = 0;
// depth-stencil view creation
// the second parameter: zero to access the mipmap level 0
hR = device->CreateDepthStencilView (dSbuffer.Get (), &depthStencilViewDesc, &dSview);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation the depth-stencil view failed!");
return;
}
// binding render target view and depth-stencil view to output render pipeline (for now one render target view)
// purpose: rendered graphics by the pipeline will be drawn to the back buffer.
// second parameter: pointer to first element of a list of render target view pointers
device->OMSetRenderTargets (1, rTview.GetAddressOf (), dSview.Get ());
// rasterizer description (determines how and which polygons is to be rendered)
// for example: render scenes in wireframe mode, draw both front and back faces of polygons
// note that by default DirectX creates one rasterizer the same as below, on which developers have no control.
D3D10_RASTERIZER_DESC rasterizerDesc;
rasterizerDesc.AntialiasedLineEnable = false;
rasterizerDesc.CullMode = D3D10_CULL_BACK;
rasterizerDesc.DepthBias = 0;
rasterizerDesc.DepthBiasClamp = 0.0f;
rasterizerDesc.DepthClipEnable = true;
rasterizerDesc.FillMode = D3D10_FILL_SOLID;
rasterizerDesc.FrontCounterClockwise = false;
rasterizerDesc.MultisampleEnable = false;
rasterizerDesc.ScissorEnable = false;
rasterizerDesc.SlopeScaledDepthBias = 0.0f;
// rasterizer creation
hR = device->CreateRasterizerState (&rasterizerDesc, &rasterizerState);
if (FAILED (hR))
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
L"Creation the rasterizer state failed!");
return;
}
// set the active rasterizer state
device->RSSetState (rasterizerState.Get ());
// viewport structure: set the viewport to entire back buffer (what area should be rendered to)
// with other words: so Direct3D can map clip space coordinates to the render target space
D3D10_VIEWPORT viewPort;
viewPort.Width = depthBufferDesc.Width;
viewPort.Height = depthBufferDesc.Height;
viewPort.MinDepth = 0.0f; // minimum and maximum depth buffer values
viewPort.MaxDepth = 1.0f;
viewPort.TopLeftX = 0; // first four integers: viewport rectangle (relative to client window rectangle)
viewPort.TopLeftY = 0;
// setting the viewport
// the second parameter is a pointer to an array of viewports
device->RSSetViewports (1, &viewPort);
clearBuffers ();
allocated = true;
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void Direct3D::clearBuffers (void)
{
try
{
const float blue [] {0.11f, 0.33f, 0.55f, 1.0f};
// filling the entire back buffer with a single colour
device->ClearRenderTargetView (rTview.Get (), blue);
// second parameter: the type of data to clear (obviously set to clear both depth-stencil)
// the values are used to override the entire depth-stencil buffer with
if (dSview)
device->ClearDepthStencilView (dSview.Get (), D3D10_CLEAR_DEPTH | D3D10_CLEAR_STENCIL, 1.0f, 0);
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};
void Direct3D::present (void)
{
try
{
HRESULT hR;
// SyncInterval parameter: the way a frame is synchronized with VBLANK:
// in flip mode: the n = 0 sets the DirectX to cancel the remaining time of previously rendered scene
// and discard this frame if a newer frame is on the queue. (screen tearing might occur)
// the n = 1 to 4 values: synchronize the presentation after n-th vertical blank.
// the second parameter: not waiting for v-sync.
if (vSync)
hR = {swapChain->Present (1, DXGI_PRESENT_DO_NOT_WAIT)}; // consider screen refresh rate
else
hR = {swapChain->Present (0, DXGI_PRESENT_DO_NOT_WAIT)}; // as fast as possible
if ((FAILED (hR)) &&
(hR != DXGI_ERROR_WAS_STILL_DRAWING)) // occurs, if the calling thread is blocked
{
PointerProvider::getFileLogger ()->push (logType::warning, std::this_thread::get_id (), L"mainThread",
L"The presentation of the scene failed!");
}
// rebind: the process is needed after each call to present, since in flip and discard mode the view targets are released.
if (dSview)
device->OMSetRenderTargets (1, rTview.GetAddressOf (), dSview.Get ());
} catch (const std::exception& ex)
{
PointerProvider::getFileLogger ()->push (logType::error, std::this_thread::get_id (), L"mainThread",
Converter::strConverter (ex.what ()));
}
};