-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathD3D11GraphicsDevice.cpp
458 lines (381 loc) · 15.6 KB
/
D3D11GraphicsDevice.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
namespace ml
{
void D3D11GraphicsDevice::init(const WindowWin32 &window)
{
m_width = window.getWidth();
m_height = window.getHeight();
m_swapChainDesc.OutputWindow = window.getHandle();
m_swapChainDesc.BufferDesc.Width = m_width;
m_swapChainDesc.BufferDesc.Height = m_height;
UINT createDeviceFlags = 0;
//#ifdef _DEBUG
// createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
//#endif
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
D3D_VALIDATE(D3D11CreateDeviceAndSwapChain(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &m_swapChainDesc, &m_swapChain, &m_device, &m_featureLevel, &m_context));
createViews();
//
// Setup the rasterizer state
//
m_rasterDesc.AntialiasedLineEnable = false;
m_rasterDesc.CullMode = D3D11_CULL_NONE;
m_rasterDesc.DepthBias = 0;
m_rasterDesc.DepthBiasClamp = 0.0f;
m_rasterDesc.DepthClipEnable = true;
m_rasterDesc.FillMode = D3D11_FILL_SOLID;
m_rasterDesc.FrontCounterClockwise = false;
m_rasterDesc.MultisampleEnable = false;
m_rasterDesc.ScissorEnable = false;
m_rasterDesc.SlopeScaledDepthBias = 0.0f;
D3D_VALIDATE(m_device->CreateRasterizerState(&m_rasterDesc, &m_rasterState));
m_context->RSSetState(m_rasterState);
//
// Setup the depth state
//
D3D11_DEPTH_STENCIL_DESC depthStateDesc;
depthStateDesc.DepthEnable = true;
depthStateDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStateDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStateDesc.StencilEnable = false;
D3D_VALIDATE(m_device->CreateDepthStencilState(&depthStateDesc, &m_depthState));
m_context->OMSetDepthStencilState(m_depthState, 1);
//
// Setup the sampler state
//
D3D11_SAMPLER_DESC samplerStateDesc;
samplerStateDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.MipLODBias = 0.0f;
samplerStateDesc.MaxAnisotropy = 1;
samplerStateDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerStateDesc.MinLOD = -FLT_MAX;
samplerStateDesc.MaxLOD = FLT_MAX;
D3D_VALIDATE(m_device->CreateSamplerState(&samplerStateDesc, &m_samplerState));
m_context->PSSetSamplers(0, 1, &m_samplerState);
#ifdef MLIB_GRAPHICSDEVICE_TRANSPARENCY
ID3D11BlendState* d3dBlendState;
D3D11_BLEND_DESC omDesc;
ZeroMemory(&omDesc, sizeof(D3D11_BLEND_DESC));
omDesc.RenderTarget[0].BlendEnable = true;
omDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
omDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
omDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
omDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
omDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
D3D_VALIDATE(m_device->CreateBlendState(&omDesc, &d3dBlendState));
m_context->OMSetBlendState(d3dBlendState, 0, 0xffffffff);
#endif
#ifdef _DEBUG
m_device->QueryInterface(__uuidof(ID3D11Debug), reinterpret_cast<void**>(&m_debug));
#endif
m_shaderManager.init(*this);
registerDefaultShaders();
}
void D3D11GraphicsDevice::createViews() {
//
// Create a render target view
//
ID3D11Texture2D* backBuffer = nullptr;
D3D_VALIDATE(m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&backBuffer));
D3D_VALIDATE(m_device->CreateRenderTargetView(backBuffer, nullptr, &m_renderTargetView));
backBuffer->Release();
//
// Create the depth buffer
//
D3D11_TEXTURE2D_DESC depthDesc;
depthDesc.Width = m_width;
depthDesc.Height = m_height;
depthDesc.MipLevels = 1;
depthDesc.ArraySize = 1;
depthDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthDesc.SampleDesc.Count = 1;
depthDesc.SampleDesc.Quality = 0;
depthDesc.Usage = D3D11_USAGE_DEFAULT;
depthDesc.BindFlags = D3D11_BIND_DEPTH_STENCIL;
depthDesc.CPUAccessFlags = 0;
depthDesc.MiscFlags = 0;
D3D_VALIDATE(m_device->CreateTexture2D(&depthDesc, nullptr, &m_depthBuffer));
//
// Setup the depth stencil view
//
D3D11_DEPTH_STENCIL_VIEW_DESC depthViewDesc;
depthViewDesc.Flags = 0;
depthViewDesc.Format = DXGI_FORMAT_D32_FLOAT;
depthViewDesc.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
depthViewDesc.Texture2D.MipSlice = 0;
// Create the depth stencil stencil view
D3D_VALIDATE(m_device->CreateDepthStencilView(m_depthBuffer, &depthViewDesc, &m_depthStencilView));
m_context->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);
//
// Setup the viewport
//
m_viewportWidth = m_width;
m_viewportHeight = m_height;
D3D11_VIEWPORT viewport;
viewport.Width = (FLOAT)m_viewportWidth;
viewport.Height = (FLOAT)m_viewportHeight;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
m_context->RSSetViewports(1, &viewport);
}
void D3D11GraphicsDevice::initWithoutWindow()
{
m_width = 0;
m_height = 0;
ZeroMemory(&m_swapChainDesc, sizeof DXGI_SWAP_CHAIN_DESC);
UINT createDeviceFlags = 0;
//#ifdef _DEBUG
// createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
//#endif
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
D3D_VALIDATE(D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, createDeviceFlags, featureLevels, numFeatureLevels, D3D11_SDK_VERSION, &m_device, &m_featureLevel, &m_context));
m_depthBuffer = nullptr;
m_depthState = nullptr;
m_depthStencilView = nullptr;
//
// Setup the rasterizer state
//
m_rasterDesc.AntialiasedLineEnable = false;
m_rasterDesc.CullMode = D3D11_CULL_NONE;
m_rasterDesc.DepthBias = 0;
m_rasterDesc.DepthBiasClamp = 0.0f;
m_rasterDesc.DepthClipEnable = true;
m_rasterDesc.FillMode = D3D11_FILL_SOLID;
m_rasterDesc.FrontCounterClockwise = false;
m_rasterDesc.MultisampleEnable = false;
m_rasterDesc.ScissorEnable = false;
m_rasterDesc.SlopeScaledDepthBias = 0.0f;
D3D_VALIDATE(m_device->CreateRasterizerState(&m_rasterDesc, &m_rasterState));
m_context->RSSetState(m_rasterState);
//
// Setup the depth state
//
D3D11_DEPTH_STENCIL_DESC depthStateDesc;
depthStateDesc.DepthEnable = true;
depthStateDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
depthStateDesc.DepthFunc = D3D11_COMPARISON_LESS;
depthStateDesc.StencilEnable = false;
D3D_VALIDATE(m_device->CreateDepthStencilState(&depthStateDesc, &m_depthState));
m_context->OMSetDepthStencilState(m_depthState, 1);
//
// Setup the sampler state
//
D3D11_SAMPLER_DESC samplerStateDesc;
samplerStateDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
samplerStateDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
samplerStateDesc.MipLODBias = 0.0f;
samplerStateDesc.MaxAnisotropy = 1;
samplerStateDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
samplerStateDesc.MinLOD = -FLT_MAX;
samplerStateDesc.MaxLOD = FLT_MAX;
D3D_VALIDATE(m_device->CreateSamplerState(&samplerStateDesc, &m_samplerState));
m_context->PSSetSamplers(0, 1, &m_samplerState);
#ifdef MLIB_GRAPHICSDEVICE_TRANSPARENCY
ID3D11BlendState* d3dBlendState;
D3D11_BLEND_DESC omDesc;
ZeroMemory(&omDesc, sizeof(D3D11_BLEND_DESC));
omDesc.RenderTarget[0].BlendEnable = true;
omDesc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
omDesc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
omDesc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ZERO;
omDesc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
omDesc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
omDesc.RenderTarget[0].RenderTargetWriteMask = D3D11_COLOR_WRITE_ENABLE_ALL;
D3D_VALIDATE(m_device->CreateBlendState(&omDesc, &d3dBlendState));
m_context->OMSetBlendState(d3dBlendState, 0, 0xffffffff);
#endif
#ifdef _DEBUG
m_device->QueryInterface(__uuidof(ID3D11Debug), reinterpret_cast<void**>(&m_debug));
#endif
m_shaderManager.init(*this);
registerDefaultShaders();
}
void D3D11GraphicsDevice::registerDefaultShaders()
{
const std::string mLibShaderDir = util::getMLibDir() + "data/shaders/";
m_shaderManager.registerShader(mLibShaderDir + "defaultBasicTexture.hlsl", "defaultBasicTexture");
m_shaderManager.registerShader(mLibShaderDir + "defaultBasic.hlsl", "defaultBasic");
}
void D3D11GraphicsDevice::resize(const WindowWin32 &window)
{
m_width = window.getWidth();
m_height = window.getHeight();
if (m_width == 0 || m_height == 0) return; //when its minimized don't do anything (it'll get focused eventually)
SAFE_RELEASE(m_depthBuffer);
SAFE_RELEASE(m_depthStencilView);
SAFE_RELEASE(m_renderTargetView);
// Alternate between 0 and DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH when resizing buffers.
// When in windowed mode, we want 0 since this allows the app to change to the desktop
// resolution from windowed mode during alt+enter. However, in fullscreen mode, we want
// the ability to change display modes from the Device Settings dialog. Therefore, we
// want to set the DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH flag.
UINT flags = 0;
//if (bFullScreen)
// Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
m_swapChainDesc.OutputWindow = window.getHandle();
m_swapChainDesc.BufferDesc.Width = m_width;
m_swapChainDesc.BufferDesc.Height = m_height;
HRESULT hr = m_swapChain->ResizeBuffers(m_swapChainDesc.BufferCount, m_width, m_height, m_swapChainDesc.BufferDesc.Format, flags);
if (FAILED(hr))
{
std::cerr << "Failed to resize buffers, there are probably outstanding buffer references to the swap chain" << std::endl;
}
createViews();
// this asset list is a bad design choice and does not work with most things (problems with pointers into active memory), but is needed for canvas
/*for (auto* asset : m_assets) {
asset->onDeviceResize();
}*/
}
//void D3D11GraphicsDevice::registerAsset(GraphicsAsset* asset) {
// m_assets.insert(asset);
// //if (std::find(m_assets.begin(), m_assets.end(), asset) != m_assets.end()) m_assets.push_back(asset);
//}
//
//void D3D11GraphicsDevice::unregisterAsset(GraphicsAsset* asset) {
// auto it = m_assets.find(asset);
// if (it != m_assets.end()) {
// m_assets.erase(it);
// }
// else {
// throw MLIB_EXCEPTION("asset not found");
// }
//}
//
//void D3D11GraphicsDevice::printAssets() {
// std::cout << "D3D11GraphicsDevice Assets: " << std::endl;
// for (auto& asset : m_assets) {
// std::cout << "\t[ " << asset->getName() << " ] " << std::endl;
// }
//}
void D3D11GraphicsDevice::renderBeginFrame()
{
float clearColor[4] = { 0.0f, 0.0f, 0.0f, 0.0f };
m_context->ClearRenderTargetView(m_renderTargetView, clearColor);
m_context->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, 1.0f, 0);
}
void D3D11GraphicsDevice::bindRenderTarget()
{
m_context->OMSetRenderTargets(1, &m_renderTargetView, m_depthStencilView);
D3D11_VIEWPORT viewport;
viewport.Width = (FLOAT)m_viewportWidth;
viewport.Height = (FLOAT)m_viewportHeight;
viewport.MinDepth = 0.0f;
viewport.MaxDepth = 1.0f;
viewport.TopLeftX = 0;
viewport.TopLeftY = 0;
m_context->RSSetViewports(1, &viewport);
}
void D3D11GraphicsDevice::clear(const vec4f &clearColor, float clearDepth)
{
m_context->ClearRenderTargetView(m_renderTargetView, clearColor.array);
m_context->ClearDepthStencilView(m_depthStencilView, D3D11_CLEAR_DEPTH, clearDepth, 0);
}
void D3D11GraphicsDevice::renderEndFrame(bool vsync)
{
UINT syncInterval = vsync ? 1 : 0;
m_swapChain->Present(syncInterval, 0); //0 -> vsync off; 1 -> vsync on (== 60Hz max)
}
void D3D11GraphicsDevice::toggleWireframe()
{
m_context->RSGetState(&m_rasterState);
m_rasterState->GetDesc(&m_rasterDesc);
if (m_rasterDesc.FillMode == D3D11_FILL_SOLID)
m_rasterDesc.FillMode = D3D11_FILL_WIREFRAME;
else
m_rasterDesc.FillMode = D3D11_FILL_SOLID;
m_rasterState->Release();
D3D_VALIDATE(m_device->CreateRasterizerState(&m_rasterDesc, &m_rasterState));
m_context->RSSetState(m_rasterState);
}
void D3D11GraphicsDevice::setCullMode(D3D11_CULL_MODE mode)
{
m_context->RSGetState(&m_rasterState);
m_rasterState->GetDesc(&m_rasterDesc);
m_rasterDesc.CullMode = mode;
m_rasterState->Release();
D3D_VALIDATE(m_device->CreateRasterizerState(&m_rasterDesc, &m_rasterState));
m_context->RSSetState(m_rasterState);
}
void D3D11GraphicsDevice::toggleCullMode()
{
m_context->RSGetState(&m_rasterState);
m_rasterState->GetDesc(&m_rasterDesc);
if (m_rasterDesc.CullMode == D3D11_CULL_NONE)
m_rasterDesc.CullMode = D3D11_CULL_FRONT;
else
m_rasterDesc.CullMode = D3D11_CULL_NONE;
m_rasterState->Release();
D3D_VALIDATE(m_device->CreateRasterizerState(&m_rasterDesc, &m_rasterState));
m_context->RSSetState(m_rasterState);
}
void D3D11GraphicsDevice::captureBackBufferColorInternal(ColorImageR8G8B8A8& result)
{
ID3D11Texture2D* frameBuffer;
D3D_VALIDATE(m_swapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&frameBuffer));
D3D11_TEXTURE2D_DESC desc;
frameBuffer->GetDesc(&desc);
if (m_captureBufferColor == nullptr) {
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.Usage = D3D11_USAGE_STAGING;
D3D_VALIDATE(m_device->CreateTexture2D(&desc, nullptr, &m_captureBufferColor));
}
m_context->CopyResource(m_captureBufferColor, frameBuffer);
result.allocate(desc.Width, desc.Height);
D3D11_MAPPED_SUBRESOURCE resource;
UINT subresource = D3D11CalcSubresource(0, 0, 0);
HRESULT hr = m_context->Map(m_captureBufferColor, subresource, D3D11_MAP_READ_WRITE, 0, &resource);
const BYTE *data = (BYTE *)resource.pData;
//resource.pData; // TEXTURE DATA IS HERE
for (UINT y = 0; y < desc.Height; y++) {
memcpy(&result(0u, y), data + resource.RowPitch * y, desc.Width * sizeof(vec4uc));
}
m_context->Unmap(m_captureBufferColor, subresource);
frameBuffer->Release();
}
void D3D11GraphicsDevice::captureBackBufferDepthInternal(DepthImage32& result)
{
D3D11_TEXTURE2D_DESC desc;
m_depthBuffer->GetDesc(&desc);
if (m_captureBufferDepth == nullptr) {
desc.BindFlags = 0;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ | D3D11_CPU_ACCESS_WRITE;
desc.Usage = D3D11_USAGE_STAGING;
D3D_VALIDATE(m_device->CreateTexture2D(&desc, nullptr, &m_captureBufferDepth));
}
m_context->CopyResource(m_captureBufferDepth, m_depthBuffer);
result.allocate(desc.Width, desc.Height);
D3D11_MAPPED_SUBRESOURCE resource;
UINT subresource = D3D11CalcSubresource(0, 0, 0);
HRESULT hr = m_context->Map(m_captureBufferDepth, subresource, D3D11_MAP_READ_WRITE, 0, &resource);
const BYTE *data = (BYTE *)resource.pData;
//resource.pData; // TEXTURE DATA IS HERE
for (UINT y = 0; y < desc.Height; y++) {
memcpy(&result(0u, y), data + resource.RowPitch * y, desc.Width * sizeof(vec4uc));
}
m_context->Unmap(m_captureBufferDepth, subresource);
result.setInvalidValue(1.0f); //that's the far plane
}
}