/
GraphicsDriver.cpp
730 lines (594 loc) · 22 KB
/
GraphicsDriver.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
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
#include "ITPEnginePCH.h"
#include <DirectXTK/DDSTextureLoader.h>
#include <DirectXTK/WICTextureLoader.h>
using namespace DirectX;
namespace
{
ID3D11Device* g_pd3dDevice = nullptr;
ID3D11Device1* g_pd3dDevice1 = nullptr;
ID3D11DeviceContext* g_pImmediateContext = nullptr;
ID3D11DeviceContext1* g_pImmediateContext1 = nullptr;
IDXGISwapChain* g_pSwapChain = nullptr;
IDXGISwapChain1* g_pSwapChain1 = nullptr;
ID3D11Debug* g_d3dDebug = nullptr;
ID3D11RasterizerState* g_RasterizerState = nullptr;
void AutoReleaseD3D( ID3D11DeviceChild* inResource )
{
inResource->Release();
}
void CreateInternalDevice()
{
UINT createDeviceFlags = 0;
#ifdef _DEBUG
createDeviceFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_DRIVER_TYPE driverTypes[] =
{
D3D_DRIVER_TYPE_HARDWARE,
D3D_DRIVER_TYPE_WARP,
D3D_DRIVER_TYPE_REFERENCE,
};
UINT numDriverTypes = ARRAYSIZE( driverTypes );
D3D_FEATURE_LEVEL featureLevels[] =
{
D3D_FEATURE_LEVEL_11_1,
D3D_FEATURE_LEVEL_11_0,
D3D_FEATURE_LEVEL_10_1,
D3D_FEATURE_LEVEL_10_0,
};
UINT numFeatureLevels = ARRAYSIZE( featureLevels );
D3D_DRIVER_TYPE driverType = D3D_DRIVER_TYPE_NULL;
D3D_FEATURE_LEVEL featureLevel = D3D_FEATURE_LEVEL_11_0;
HRESULT hr = S_OK;
for( UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++ )
{
driverType = driverTypes[ driverTypeIndex ];
hr = D3D11CreateDevice( nullptr, driverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &g_pd3dDevice, &featureLevel, &g_pImmediateContext );
if( hr == E_INVALIDARG )
{
// DirectX 11.0 platforms will not recognize D3D_FEATURE_LEVEL_11_1 so we need to retry without it
hr = D3D11CreateDevice( nullptr, driverType, nullptr, createDeviceFlags, &featureLevels[ 1 ], numFeatureLevels - 1,
D3D11_SDK_VERSION, &g_pd3dDevice, &featureLevel, &g_pImmediateContext );
}
if( SUCCEEDED( hr ) )
{
/*
#ifdef _DEBUG
g_pd3dDevice->QueryInterface( __uuidof( ID3D11Debug ), reinterpret_cast< void** >( &g_d3dDebug ) );
#endif
*/
//yay!
return;
}
}
//uhoh
DbgAssert( hr == S_OK, "Problem Creating Graphics Device" );
}
HRESULT CreateSwapChain( UINT inBackBufferWidth, UINT inBackBufferHeight, HWND inWindow )
{
HRESULT hr;
// Obtain DXGI factory from device (since we used nullptr for pAdapter above)
IDXGIFactory1* dxgiFactory = nullptr;
{
IDXGIDevice* dxgiDevice = nullptr;
hr = g_pd3dDevice->QueryInterface( __uuidof( IDXGIDevice ), reinterpret_cast<void**>( &dxgiDevice ) );
if( SUCCEEDED( hr ) )
{
IDXGIAdapter* adapter = nullptr;
hr = dxgiDevice->GetAdapter( &adapter );
if( SUCCEEDED( hr ) )
{
hr = adapter->GetParent( __uuidof( IDXGIFactory1 ), reinterpret_cast<void**>( &dxgiFactory ) );
adapter->Release();
}
dxgiDevice->Release();
}
}
if( FAILED( hr ) )
{
return hr;
}
// Create swap chain
IDXGIFactory2* dxgiFactory2 = nullptr;
hr = dxgiFactory->QueryInterface( __uuidof( IDXGIFactory2 ), reinterpret_cast<void**>( &dxgiFactory2 ) );
if( dxgiFactory2 )
{
// DirectX 11.1 or later
hr = g_pd3dDevice->QueryInterface( __uuidof( ID3D11Device1 ), reinterpret_cast<void**>( &g_pd3dDevice1 ) );
if( SUCCEEDED( hr ) )
{
( void ) g_pImmediateContext->QueryInterface( __uuidof( ID3D11DeviceContext1 ), reinterpret_cast<void**>( &g_pImmediateContext1 ) );
}
DXGI_SWAP_CHAIN_DESC1 sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.Width = inBackBufferWidth;
sd.Height = inBackBufferHeight;
sd.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.BufferCount = 1;
hr = dxgiFactory2->CreateSwapChainForHwnd( g_pd3dDevice, inWindow, &sd, nullptr, nullptr, &g_pSwapChain1 );
if( SUCCEEDED( hr ) )
{
hr = g_pSwapChain1->QueryInterface( __uuidof( IDXGISwapChain ), reinterpret_cast<void**>( &g_pSwapChain ) );
}
dxgiFactory2->Release();
}
else
{
// DirectX 11.0 systems
DXGI_SWAP_CHAIN_DESC sd;
ZeroMemory( &sd, sizeof( sd ) );
sd.BufferCount = 1;
sd.BufferDesc.Width = inBackBufferWidth;
sd.BufferDesc.Height = inBackBufferHeight;
sd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
sd.BufferDesc.RefreshRate.Numerator = 60;
sd.BufferDesc.RefreshRate.Denominator = 1;
sd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
sd.OutputWindow = inWindow;
sd.SampleDesc.Count = 1;
sd.SampleDesc.Quality = 0;
sd.Windowed = TRUE;
hr = dxgiFactory->CreateSwapChain( g_pd3dDevice, &sd, &g_pSwapChain );
}
dxgiFactory->Release();
if( FAILED( hr ) )
{
return hr;
}
return S_OK;
}
RenderTargetPtr CreateBackBufferRenderTargetView()
{
HRESULT hr;
// Create a render target view
ID3D11Texture2D* pBackBuffer = nullptr;
hr = g_pSwapChain->GetBuffer( 0, __uuidof( ID3D11Texture2D ), reinterpret_cast<void**>( &pBackBuffer ) );
if( FAILED( hr ) )
{
return nullptr;
}
ID3D11RenderTargetView* toRet;
hr = g_pd3dDevice->CreateRenderTargetView( pBackBuffer, nullptr, &toRet );
pBackBuffer->Release();
if( FAILED( hr ) )
{
return nullptr;
}
return RenderTargetPtr( toRet, AutoReleaseD3D );
}
}
GraphicsDriver::GraphicsDriver( void* inWindow ) :
mCurrentRenderTarget( nullptr ),
mCurrentDepthStencil( nullptr )
{
HWND window = reinterpret_cast<HWND>(inWindow);
RECT rc;
GetClientRect(window, &rc );
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
CreateInternalDevice();
CreateSwapChain( width, height, window);
mBackBufferRenderTarget = CreateBackBufferRenderTargetView();
mWindowWidth = width;
mWindowHeight = height;
SetViewport( 0.f, 0.f, static_cast< float >( width ), static_cast< float >( height ) );
SetRasterizerState( CreateRasterizerState( EFM_Wireframe ) );
SetPrimitiveTopology( EPT_TriangleList );
SetRenderTarget( GetBackBufferRenderTarget() );
}
void GraphicsDriver::SetRenderTarget( RenderTargetPtr inRenderTarget )
{
mCurrentRenderTarget = inRenderTarget;
auto renderTarget = mCurrentRenderTarget.get();
g_pImmediateContext->OMSetRenderTargets( 1, &renderTarget, mCurrentDepthStencil.get() );
}
void GraphicsDriver::SetDepthStencil( DepthStencilPtr inDepthStencil )
{
mCurrentDepthStencil = inDepthStencil;
auto renderTarget = mCurrentRenderTarget.get();
g_pImmediateContext->OMSetRenderTargets( 1, &renderTarget, mCurrentDepthStencil.get() );
}
void GraphicsDriver::SetViewport( float inX, float inY, float inWidth, float inHeight )
{
// Setup the viewport
D3D11_VIEWPORT vp;
vp.TopLeftX = inX;
vp.TopLeftY = inY;
vp.Width = inWidth;
vp.Height = inHeight;
vp.MinDepth = 0.0f;
vp.MaxDepth = 1.0f;
g_pImmediateContext->RSSetViewports( 1, &vp );
}
//--------------------------------------------------------------------------------------
// Helper for compiling shaders with D3DCompile
//
// With VS 11, we could load up prebuilt .cso files instead...
//--------------------------------------------------------------------------------------
bool GraphicsDriver::CompileShaderFromFile( const char* inFileName, const char* szEntryPoint, const char* szShaderModel, std::vector< char >& outCompiledShaderCode )
{
HRESULT hr = S_OK;
DWORD dwShaderFlags = D3DCOMPILE_ENABLE_STRICTNESS;
#ifdef _DEBUG
// Set the D3DCOMPILE_DEBUG flag to embed debug information in the shaders.
// Setting this flag improves the shader debugging experience, but still allows
// the shaders to be optimized and to run exactly the way they will run in
// the release configuration of this program.
dwShaderFlags |= D3DCOMPILE_DEBUG;
// Disable optimizations to further improve shader debugging
dwShaderFlags |= D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
const size_t cSize = strlen(inFileName) + 1;
size_t retCount;
std::wstring wc(cSize, L'#');
mbstowcs_s(&retCount, &wc[0], cSize, inFileName, _TRUNCATE);
ID3DBlob* pErrorBlob = nullptr;
ID3DBlob* pBlobOut = nullptr;
hr = D3DCompileFromFile(wc.c_str(), nullptr, D3D_COMPILE_STANDARD_FILE_INCLUDE, szEntryPoint, szShaderModel,
dwShaderFlags, 0, &pBlobOut, &pErrorBlob);
if( FAILED( hr ) )
{
if( pErrorBlob )
{
static wchar_t szBuffer[4096];
_snwprintf_s(szBuffer, 4096, _TRUNCATE,
L"%hs",
(char*)pErrorBlob->GetBufferPointer());
OutputDebugString(szBuffer);
MessageBox(nullptr, szBuffer, L"Error", MB_OK);
pErrorBlob->Release();
DbgAssert( hr == S_OK, "Shader Compilation Failed" );
}
return false;
}
if( pErrorBlob )
{
pErrorBlob->Release();
}
//now copy to vector if we like it...
if( pBlobOut )
{
int compiledCodeSize = pBlobOut->GetBufferSize();
outCompiledShaderCode.resize( compiledCodeSize );
std::memcpy( outCompiledShaderCode.data(), pBlobOut->GetBufferPointer(), compiledCodeSize );
pBlobOut->Release();
}
return hr == S_OK;
}
VertexShaderPtr GraphicsDriver::CreateVertexShader( const std::vector< char >& inCompiledShaderCode )
{
ID3D11VertexShader* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreateVertexShader( inCompiledShaderCode.data(), inCompiledShaderCode.size(), nullptr, &toRet );
DbgAssert( hr == S_OK, "Failure Creating Vertex Shader From Compiled Shader Code" );
return VertexShaderPtr( toRet, AutoReleaseD3D );
}
PixelShaderPtr GraphicsDriver::CreatePixelShader( const std::vector< char >& inCompiledShaderCode )
{
ID3D11PixelShader* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreatePixelShader( inCompiledShaderCode.data(), inCompiledShaderCode.size(), nullptr, &toRet );
DbgAssert( hr == S_OK, "Failure Creating Pixel Shader From Compiled Shader Code" );
return PixelShaderPtr( toRet, AutoReleaseD3D );
}
InputLayoutPtr GraphicsDriver::CreateInputLayout( const InputLayoutElement* inElements, int inNumElements, const std::vector< char >& inCompiledVertexShader )
{
ID3D11InputLayout* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreateInputLayout(
inElements, inNumElements,
inCompiledVertexShader.data(), inCompiledVertexShader.size(),
&toRet );
DbgAssert( hr == S_OK, "Failure Creating Vertex Layout" );
return InputLayoutPtr( toRet, AutoReleaseD3D );
}
SamplerStatePtr GraphicsDriver::CreateSamplerState()
{
// Create the sample state
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory( &sampDesc, sizeof( sampDesc ) );
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;
ID3D11SamplerState* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreateSamplerState( &sampDesc, &toRet );
DbgAssert( hr == S_OK, "Failure Creating Sampler State" );
return SamplerStatePtr( toRet, AutoReleaseD3D );
}
void GraphicsDriver::SetInputLayout( InputLayoutPtr inLayout )
{
g_pImmediateContext->IASetInputLayout( inLayout.get() );
}
void GraphicsDriver::SetPrimitiveTopology(EPrimitiveTopology inTopology)
{
// Set primitive topology
g_pImmediateContext->IASetPrimitiveTopology(static_cast<D3D11_PRIMITIVE_TOPOLOGY>(inTopology));
}
void GraphicsDriver::SetVertexBuffer( GraphicsBufferPtr inBuffer, uint32_t inVertexSize )
{
auto buffer = inBuffer.get();
uint32_t offset = 0;
g_pImmediateContext->IASetVertexBuffers( 0, 1, &buffer, &inVertexSize, &offset );
}
void GraphicsDriver::SetIndexBuffer( GraphicsBufferPtr inBuffer )
{
g_pImmediateContext->IASetIndexBuffer( inBuffer.get(), DXGI_FORMAT_R16_UINT, 0 );
}
void GraphicsDriver::SetVSConstantBuffer( GraphicsBufferPtr inBuffer, int inStartSlot )
{
auto buffer = inBuffer.get();
g_pImmediateContext->VSSetConstantBuffers( inStartSlot, 1, &buffer );
}
void GraphicsDriver::SetPSConstantBuffer( GraphicsBufferPtr inBuffer, int inStartSlot )
{
auto buffer = inBuffer.get();
g_pImmediateContext->PSSetConstantBuffers( inStartSlot, 1, &buffer );
}
void GraphicsDriver::SetPSSamplerState( SamplerStatePtr inSamplerState, int inStartSlot )
{
auto sampler = inSamplerState.get();
g_pImmediateContext->PSSetSamplers( inStartSlot, 1, &sampler );
}
void GraphicsDriver::SetPSTexture(GraphicsTexturePtr inTexture, int inStartSlot)
{
auto texture = inTexture.get();
g_pImmediateContext->PSSetShaderResources( inStartSlot, 1, &texture );
}
void GraphicsDriver::SetDepthStencilState( DepthStencilStatePtr inDepthStencilState )
{
g_pImmediateContext->OMSetDepthStencilState( inDepthStencilState.get(), 1 );
}
void* GraphicsDriver::MapBuffer( GraphicsBufferPtr inBuffer )
{
D3D11_MAPPED_SUBRESOURCE mapped;
g_pImmediateContext->Map( inBuffer.get(), 0, D3D11_MAP_WRITE_DISCARD, 0, &mapped );
return mapped.pData;
}
void GraphicsDriver::UnmapBuffer( GraphicsBufferPtr inBuffer )
{
g_pImmediateContext->Unmap( inBuffer.get(), 0 );
}
GraphicsBufferPtr GraphicsDriver::CreateGraphicsBuffer(const void* inRawData, int inRawDataSize, EBindflags inBindFlags, ECPUAccessFlags inCPUAccessFlags, EGraphicsBufferUsage inUsage)
{
D3D11_BUFFER_DESC bd;
ZeroMemory( &bd, sizeof( bd ) );
bd.Usage = static_cast< D3D11_USAGE >( inUsage );
bd.ByteWidth = inRawDataSize;
bd.BindFlags = inBindFlags;
bd.CPUAccessFlags = inCPUAccessFlags;
D3D11_SUBRESOURCE_DATA initialData;
ZeroMemory( &initialData, sizeof( initialData ) );
initialData.pSysMem = inRawData;
ID3D11Buffer* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreateBuffer( &bd, inRawData ? &initialData : nullptr, &toRet );
DbgAssert( hr == S_OK, "Problem Creating Vertex Buffer" );
return GraphicsBufferPtr(toRet, AutoReleaseD3D);
}
GraphicsTexturePtr GraphicsDriver::CreateTextureFromFile(const char* inFileName, int& outWidth, int& outHeight)
{
std::string fileStr(inFileName);
std::string extension = fileStr.substr(fileStr.find_last_of('.'));
//erg, convert to widechar...
const size_t cSize = strlen(inFileName) + 1;
size_t retCount;
std::wstring wc(cSize, L'#');
mbstowcs_s(&retCount, &wc[0], cSize, inFileName, _TRUNCATE);
ID3D11ShaderResourceView* toRet = nullptr;
ID3D11Resource* texture = nullptr;
HRESULT hr = -1;
if (extension == ".dds" || extension == ".DDS")
{
hr = CreateDDSTextureFromFile(g_pd3dDevice, wc.c_str(), &texture, &toRet);
}
else if (extension == ".png" || extension == ".bmp" || extension == ".PNG" || extension == ".BMP")
{
hr = CreateWICTextureFromFile(g_pd3dDevice, wc.c_str(), &texture, &toRet);
}
else
{
OutputDebugString(L"GraphicsDriver can only load images of type DDS, PNG, or BMP.");
}
DbgAssert( hr == S_OK, "Problem Creating Texture From File" );
CD3D11_TEXTURE2D_DESC textureDesc;
((ID3D11Texture2D*)texture)->GetDesc(&textureDesc);
outWidth = textureDesc.Width;
outHeight = textureDesc.Height;
return GraphicsTexturePtr(toRet, AutoReleaseD3D);
}
GraphicsTexturePtr GraphicsDriver::CreateTextureFromMemory(const void* inPixels, int inWidth, int inHeight, ETextureFormat inTextureFormat)
{
D3D11_TEXTURE2D_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.Width = inWidth;
desc.Height = inHeight;
desc.MipLevels = 1;
desc.ArraySize = 1;
desc.Format = static_cast<DXGI_FORMAT>(inTextureFormat);
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DEFAULT;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
D3D11_SUBRESOURCE_DATA data;
ZeroMemory(&data, sizeof(data));
data.pSysMem = inPixels;
// Assuming RGBA or BGRA format
data.SysMemPitch = inWidth * 4;
data.SysMemSlicePitch = inWidth * inHeight * 4;
ID3D11Texture2D *pTexture = NULL;
HRESULT hr = g_pd3dDevice->CreateTexture2D(&desc, &data, &pTexture);
DbgAssert(hr == S_OK, "Problem Creating Texture From Memory");
ID3D11ShaderResourceView* toRet = nullptr;
hr = g_pd3dDevice->CreateShaderResourceView(pTexture, NULL, &toRet);
DbgAssert(hr == S_OK, "Failed to create a shader resource view from texture");
// Release the texture, because it will have a ref count via the view
pTexture->Release();
return GraphicsTexturePtr(toRet, AutoReleaseD3D);
}
DepthStencilPtr GraphicsDriver::CreateDepthStencil(int inWidth, int inHeight)
{
ID3D11Texture2D* depthStencilTexture;
// Create depth stencil texture
D3D11_TEXTURE2D_DESC descDepth;
ZeroMemory( &descDepth, sizeof( descDepth ) );
descDepth.Width = inWidth;
descDepth.Height = inHeight;
descDepth.MipLevels = 1;
descDepth.ArraySize = 1;
descDepth.Format = DXGI_FORMAT_D24_UNORM_S8_UINT;
descDepth.SampleDesc.Count = 1;
descDepth.SampleDesc.Quality = 0;
descDepth.Usage = D3D11_USAGE_DEFAULT;
descDepth.BindFlags = D3D11_BIND_DEPTH_STENCIL;
descDepth.CPUAccessFlags = 0;
descDepth.MiscFlags = 0;
HRESULT hr = g_pd3dDevice->CreateTexture2D( &descDepth, nullptr, &depthStencilTexture );
DbgAssert( hr == S_OK, "Problem Creating Depth Stencil" );
ID3D11DepthStencilView* toRet = nullptr;
// Create the depth stencil view
D3D11_DEPTH_STENCIL_VIEW_DESC descDSV;
ZeroMemory( &descDSV, sizeof( descDSV ) );
descDSV.Format = descDepth.Format;
descDSV.ViewDimension = D3D11_DSV_DIMENSION_TEXTURE2D;
descDSV.Texture2D.MipSlice = 0;
hr = g_pd3dDevice->CreateDepthStencilView( depthStencilTexture, &descDSV, &toRet );
DbgAssert( hr == S_OK, "Problem Creating Depth Stencil" );
return DepthStencilPtr( toRet, AutoReleaseD3D );
}
DepthStencilStatePtr GraphicsDriver::CreateDepthStencilState( bool inDepthTestEnable, EComparisonFunc inDepthComparisonFunction )
{
D3D11_DEPTH_STENCIL_DESC dsDesc;
// Depth test parameters
dsDesc.DepthEnable = inDepthTestEnable;
dsDesc.DepthWriteMask = D3D11_DEPTH_WRITE_MASK_ALL;
dsDesc.DepthFunc = static_cast< D3D11_COMPARISON_FUNC >( inDepthComparisonFunction );
// Stencil test parameters
dsDesc.StencilEnable = false;
dsDesc.StencilReadMask = 0xFF;
dsDesc.StencilWriteMask = 0xFF;
// Stencil operations if pixel is front-facing
dsDesc.FrontFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilDepthFailOp = D3D11_STENCIL_OP_INCR;
dsDesc.FrontFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.FrontFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Stencil operations if pixel is back-facing
dsDesc.BackFace.StencilFailOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilDepthFailOp = D3D11_STENCIL_OP_DECR;
dsDesc.BackFace.StencilPassOp = D3D11_STENCIL_OP_KEEP;
dsDesc.BackFace.StencilFunc = D3D11_COMPARISON_ALWAYS;
// Create depth stencil state
ID3D11DepthStencilState* toRet = nullptr;
HRESULT hr = g_pd3dDevice->CreateDepthStencilState( &dsDesc, &toRet );
DbgAssert( hr == S_OK, "Problem Creating Depth Stencil" );
return DepthStencilStatePtr( toRet, AutoReleaseD3D );
}
GraphicsDriver::~GraphicsDriver()
{
mCurrentRenderTarget.reset();
mCurrentDepthStencil.reset();
if( g_pImmediateContext )
{
g_pImmediateContext->ClearState();
g_pImmediateContext->Flush();
}
mBackBufferRenderTarget.reset();
if( g_pSwapChain1 ) g_pSwapChain1->Release();
if( g_pSwapChain ) g_pSwapChain->Release();
if( g_pImmediateContext1 ) g_pImmediateContext1->Release();
if( g_pImmediateContext ) g_pImmediateContext->Release();
if( g_pd3dDevice1 ) g_pd3dDevice1->Release();
if( g_pd3dDevice ) g_pd3dDevice->Release();
if( g_RasterizerState ) g_RasterizerState->Release();
if( g_d3dDebug )
{
//g_d3dDebug->ReportLiveDeviceObjects( D3D11_RLDO_DETAIL );
g_d3dDebug->Release();
}
}
void GraphicsDriver::ClearBackBuffer(const Vector3& inColor, float inAlpha)
{
ClearRenderTarget(mBackBufferRenderTarget, inColor, inAlpha);
}
void GraphicsDriver::ClearRenderTarget(RenderTargetPtr inRenderTarget, const Vector3& inColor, float inAlpha)
{
float color[4];
color[0] = inColor.x;
color[1] = inColor.y;
color[2] = inColor.z;
color[3] = inAlpha;
// Clear the back buffer
g_pImmediateContext->ClearRenderTargetView( inRenderTarget.get(), color );
}
void GraphicsDriver::ClearDepthStencil( DepthStencilPtr inDepthStencil, float inDepth )
{
// Clear the back buffer
g_pImmediateContext->ClearDepthStencilView( inDepthStencil.get(), D3D11_CLEAR_DEPTH, inDepth, 0 );
}
void GraphicsDriver::SetVertexShader( VertexShaderPtr inVertexShader )
{
g_pImmediateContext->VSSetShader( inVertexShader.get(), nullptr, 0 );
}
void GraphicsDriver::SetPixelShader( PixelShaderPtr inPixelShader )
{
g_pImmediateContext->PSSetShader( inPixelShader.get(), nullptr, 0 );
}
void GraphicsDriver::Draw( int inVertexCount, int inStartVertexIndex )
{
g_pImmediateContext->Draw( inVertexCount, inStartVertexIndex );
}
void GraphicsDriver::DrawIndexed( int inIndexCount, int inStartIndexLocation, int inBaseVertexLocation )
{
g_pImmediateContext->DrawIndexed( inIndexCount, inStartIndexLocation, inBaseVertexLocation );
}
void GraphicsDriver::Present()
{
g_pSwapChain->Present( 0, 0 );
}
RasterizerStatePtr GraphicsDriver::CreateRasterizerState( EFillMode inFillMode )
{
//let's set CCW as the front face, since right hand rule is nice...
D3D11_RASTERIZER_DESC rastDesc;
ZeroMemory( &rastDesc, sizeof( rastDesc ) );
//lab3
rastDesc.FillMode = static_cast< D3D11_FILL_MODE >( inFillMode );
rastDesc.CullMode = D3D11_CULL_NONE;
rastDesc.FrontCounterClockwise = false;
ID3D11RasterizerState* toRet;
auto hr = g_pd3dDevice->CreateRasterizerState( &rastDesc, &toRet );
DbgAssert( hr == S_OK, "Problem Creating Rasterizer State" );
return RasterizerStatePtr( toRet, AutoReleaseD3D );
}
void GraphicsDriver::SetRasterizerState(RasterizerStatePtr inRasterizerState)
{
g_pImmediateContext->RSSetState( inRasterizerState.get() );
}
BlendStatePtr GraphicsDriver::CreateBlendState(bool inEnableBlend)
{
D3D11_BLEND_DESC desc;
ZeroMemory(&desc, sizeof(desc));
desc.RenderTarget[0].BlendEnable = inEnableBlend;
desc.RenderTarget[0].SrcBlend = D3D11_BLEND_SRC_ALPHA;
desc.RenderTarget[0].DestBlend = D3D11_BLEND_INV_SRC_ALPHA;
desc.RenderTarget[0].BlendOp = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].SrcBlendAlpha = D3D11_BLEND_ONE;
desc.RenderTarget[0].DestBlendAlpha = D3D11_BLEND_ZERO;
desc.RenderTarget[0].BlendOpAlpha = D3D11_BLEND_OP_ADD;
desc.RenderTarget[0].RenderTargetWriteMask = 0x0f;
ID3D11BlendState* toRet;
auto hr = g_pd3dDevice->CreateBlendState(&desc, &toRet);
DbgAssert(hr == S_OK, "Problem Creating Blend State");
return BlendStatePtr(toRet, AutoReleaseD3D);
}
void GraphicsDriver::SetBlendState(BlendStatePtr inBlendState)
{
float blendFactor[4];
// Setup the blend factor.
blendFactor[0] = 0.0f;
blendFactor[1] = 0.0f;
blendFactor[2] = 0.0f;
blendFactor[3] = 0.0f;
g_pImmediateContext->OMSetBlendState(inBlendState.get(), blendFactor, 0xffffffff);
}