-
Notifications
You must be signed in to change notification settings - Fork 34
/
d3drender.cpp
441 lines (376 loc) · 13.7 KB
/
d3drender.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
//===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
#include "d3drender.h"
#include <Evntprov.h>
#pragma comment( lib, "dxgi.lib" )
#pragma comment( lib, "d3d11.lib" )
#pragma comment( lib, "Rpcrt4.lib" )
#define Log( ... )
namespace
{
inline bool operator==( const LUID &A, const LUID &B )
{
return A.HighPart == B.HighPart && A.LowPart == B.LowPart;
}
bool FindDXGIOutput( IDXGIFactory *pFactory, int32_t nWidth, int32_t nHeight, IDXGIAdapter **pOutAdapter, IDXGIOutput **pOutOutput, int32_t *pOutX, int32_t *pOutY )
{
IDXGIAdapter *pDXGIAdapter;
for ( UINT nAdapterIndex = 0; pFactory->EnumAdapters( nAdapterIndex, &pDXGIAdapter ) != DXGI_ERROR_NOT_FOUND; nAdapterIndex++ )
{
IDXGIOutput *pDXGIOutput;
for ( UINT nOutputIndex = 0; pDXGIAdapter->EnumOutputs( nOutputIndex, &pDXGIOutput ) != DXGI_ERROR_NOT_FOUND; nOutputIndex++ )
{
DXGI_OUTPUT_DESC desc;
pDXGIOutput->GetDesc( &desc );
if ( desc.DesktopCoordinates.right - desc.DesktopCoordinates.left == nWidth &&
desc.DesktopCoordinates.bottom - desc.DesktopCoordinates.top == nHeight )
{
*pOutAdapter = pDXGIAdapter;
*pOutOutput = pDXGIOutput;
*pOutX = desc.DesktopCoordinates.left;
*pOutY = desc.DesktopCoordinates.top;
return true;
}
pDXGIOutput->Release();
}
pDXGIAdapter->Release();
}
return false;
}
bool CreateDevice( IDXGIAdapter *pDXGIAdapter, ID3D11Device **pD3D11Device, ID3D11DeviceContext **pD3D11Context )
{
UINT creationFlags = 0;
#if _DEBUG
creationFlags |= D3D11_CREATE_DEVICE_DEBUG;
#endif
D3D_FEATURE_LEVEL eFeatureLevel;
HRESULT hRes = D3D11CreateDevice( pDXGIAdapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, creationFlags, NULL, 0, D3D11_SDK_VERSION, pD3D11Device, &eFeatureLevel, pD3D11Context );
#if _DEBUG
// CreateDevice fails on Win10 in debug if the Win10 SDK isn't installed.
if ( pD3D11Device == NULL )
{
hRes = D3D11CreateDevice( pDXGIAdapter, D3D_DRIVER_TYPE_UNKNOWN, NULL, 0, NULL, 0, D3D11_SDK_VERSION, pD3D11Device, &eFeatureLevel, pD3D11Context );
}
#endif
if ( FAILED( hRes ) )
{
Log( "Failed to create D3D11 device! (err=%u)", hRes );
return false;
}
if ( eFeatureLevel < D3D_FEATURE_LEVEL_11_0 )
{
Log( "DX11 level hardware required!" );
return false;
}
return true;
}
class CEventHelper
{
public:
CEventHelper()
{
UuidFromString( ( RPC_CSTR ) "8c8f13b1-60eb-4b6a-a433-de86104115ac", &guid );
EventRegister( &guid, nullptr, nullptr, &handle );
}
REGHANDLE handle;
GUID guid;
};
CEventHelper s_eventHelper;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void EventWriteString( const wchar_t* pwchEvent )
{
::EventWriteString( s_eventHelper.handle, 0, 0, pwchEvent );
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
CD3DRender::CD3DRender()
: m_pDXGIFactory( NULL )
, m_pDXGIOutput( NULL )
, m_pDXGISwapChain( NULL )
, m_pD3D11Device( NULL )
, m_pD3D11Context( NULL )
, m_nDisplayWidth( 0 )
, m_nDisplayHeight( 0 )
, m_nDisplayX( 0 )
, m_nDisplayY( 0 )
{
// Initialize DXGI
{
// Need to use DXGI 1.1 for shared texture support.
IDXGIFactory1 *pDXGIFactory1;
if ( FAILED( CreateDXGIFactory1( __uuidof( IDXGIFactory1 ), ( void ** )&pDXGIFactory1 ) ) )
{
Log( "Failed to create DXGIFactory1!" );
return;
}
else if ( FAILED( pDXGIFactory1->QueryInterface( __uuidof( IDXGIFactory ), ( void ** )&m_pDXGIFactory ) ) )
{
pDXGIFactory1->Release();
Log( "Failed to get DXGIFactory interface!" );
return;
}
pDXGIFactory1->Release();
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
CD3DRender::~CD3DRender()
{
SAFE_RELEASE( m_pDXGIFactory );
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::GetDisplayPos( int32_t *pDisplayX, int32_t *pDisplayY )
{
*pDisplayX = m_nDisplayX;
*pDisplayY = m_nDisplayY;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::GetDisplaySize( uint32_t *pDisplayWidth, uint32_t *pDisplayHeight )
{
*pDisplayWidth = m_nDisplayWidth;
*pDisplayHeight = m_nDisplayHeight;
}
//--------------------------------------------------------------------------------------------------
// Purpose: Return the DXGI index and name of the adapter currently in use.
//--------------------------------------------------------------------------------------------------
bool CD3DRender::GetAdapterInfo( int32_t *pAdapterIndex, wchar_t *pBuffer, uint32_t nBufferCount )
{
if ( m_pD3D11Device == NULL )
return false;
bool bSuccess = false;
IDXGIDevice *pDXGIDevice;
if ( SUCCEEDED( m_pD3D11Device->QueryInterface( __uuidof( IDXGIDevice ), ( void ** )&pDXGIDevice ) ) )
{
IDXGIAdapter *pDXGIAdapter;
if ( SUCCEEDED( pDXGIDevice->GetParent( __uuidof( IDXGIAdapter ), ( void ** )&pDXGIAdapter ) ) )
{
DXGI_ADAPTER_DESC adapterDesc;
pDXGIAdapter->GetDesc( &adapterDesc );
IDXGIFactory *pDXGIFactory;
if ( SUCCEEDED( pDXGIAdapter->GetParent( __uuidof( IDXGIFactory ), ( void ** )&pDXGIFactory ) ) )
{
IDXGIAdapter *pEnumeratedAdapter;
for ( UINT nAdapterIndex = 0; pDXGIFactory->EnumAdapters( nAdapterIndex, &pEnumeratedAdapter ) != DXGI_ERROR_NOT_FOUND; nAdapterIndex++ )
{
DXGI_ADAPTER_DESC enumeratedDesc;
pEnumeratedAdapter->GetDesc( &enumeratedDesc );
pEnumeratedAdapter->Release();
if ( enumeratedDesc.AdapterLuid == adapterDesc.AdapterLuid )
{
if ( pAdapterIndex )
*pAdapterIndex = nAdapterIndex;
if ( pBuffer && nBufferCount )
wcsncpy_s( pBuffer, nBufferCount, adapterDesc.Description, ARRAYSIZE( adapterDesc.Description ) );
bSuccess = true;
break;
}
}
pDXGIFactory->Release();
}
pDXGIAdapter->Release();
}
pDXGIDevice->Release();
}
return bSuccess;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
ID3D11Texture2D *CD3DRender::GetSharedTexture( HANDLE hSharedTexture )
{
if ( !hSharedTexture )
return NULL;
for ( SharedTextures_t::iterator it = m_SharedTextureCache.begin();
it != m_SharedTextureCache.end(); ++it )
{
if ( it->m_hSharedTexture == hSharedTexture )
{
return it->m_pTexture;
}
}
ID3D11Texture2D *pTexture;
if ( SUCCEEDED( m_pD3D11Device->OpenSharedResource(
hSharedTexture, __uuidof( ID3D11Texture2D ), ( void ** )&pTexture ) ) )
{
SharedTextureEntry_t entry { hSharedTexture, pTexture };
m_SharedTextureCache.push_back( entry );
return pTexture;
}
return NULL;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
bool CD3DRender::Initialize( uint32_t nAdapterIndex )
{
Shutdown();
if ( m_pDXGIFactory == NULL )
return false;
IDXGIAdapter *pDXGIAdapter;
if ( FAILED( m_pDXGIFactory->EnumAdapters( nAdapterIndex, &pDXGIAdapter ) ) )
return false;
bool bSuccess = CreateDevice( pDXGIAdapter, &m_pD3D11Device, &m_pD3D11Context );
pDXGIAdapter->Release();
return bSuccess;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
bool CD3DRender::Initialize( uint32_t nDisplayWidth, uint32_t nDisplayHeight )
{
Shutdown();
if ( m_pDXGIFactory == NULL )
return false;
m_nDisplayWidth = nDisplayWidth;
m_nDisplayHeight = nDisplayHeight;
IDXGIAdapter *pDXGIAdapter;
if ( !FindDXGIOutput( m_pDXGIFactory, m_nDisplayWidth, m_nDisplayHeight, &pDXGIAdapter, &m_pDXGIOutput, &m_nDisplayX, &m_nDisplayY ) )
return false;
bool bSuccess = CreateDevice( pDXGIAdapter, &m_pD3D11Device, &m_pD3D11Context );
pDXGIAdapter->Release();
return bSuccess;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::Shutdown()
{
SetFullscreen( FALSE );
SAFE_RELEASE( m_pD3D11Context );
SAFE_RELEASE( m_pD3D11Device );
SAFE_RELEASE( m_pDXGISwapChain );
SAFE_RELEASE( m_pDXGIOutput );
m_nDisplayWidth = 0;
m_nDisplayHeight = 0;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
bool CD3DRender::CreateSwapChain( HWND hWnd, const DXGI_RATIONAL &refreshRate )
{
if ( !m_pD3D11Device )
return false;
if ( !m_pDXGIOutput )
return false;
if ( !m_pDXGIFactory )
return false;
// Determine which video mode to use
DXGI_MODE_DESC modeDesc;
ZeroMemory( &modeDesc, sizeof( modeDesc ) );
modeDesc.Width = m_nDisplayWidth;
modeDesc.Height = m_nDisplayHeight;
modeDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
modeDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
modeDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
modeDesc.RefreshRate = refreshRate;
DXGI_MODE_DESC modeOut = modeDesc;
if ( FAILED( m_pDXGIOutput->FindClosestMatchingMode( &modeDesc, &modeOut, m_pD3D11Device ) ) )
{
Log( "Failed to find closest matching mode!" );
return false;
}
// Create a fullscreen swap chain for the window
DXGI_SWAP_CHAIN_DESC swapChainDesc;
ZeroMemory( &swapChainDesc, sizeof( swapChainDesc ) );
swapChainDesc.BufferCount = 2;
swapChainDesc.BufferDesc = modeOut;
swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
swapChainDesc.OutputWindow = hWnd;
swapChainDesc.SampleDesc.Count = 1;
swapChainDesc.SampleDesc.Quality = 0;
swapChainDesc.Windowed = TRUE;
swapChainDesc.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
swapChainDesc.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH;
if ( FAILED( m_pDXGIFactory->CreateSwapChain( m_pD3D11Device, &swapChainDesc, &m_pDXGISwapChain ) ) )
{
Log( "Failed to create swap chain!" );
return false;
}
m_pDXGIFactory->MakeWindowAssociation( swapChainDesc.OutputWindow, DXGI_MWA_NO_WINDOW_CHANGES | DXGI_MWA_NO_ALT_ENTER );
//!! Probably don't need this as long as we Flush after Present.
IDXGIDevice1 *pDXGIDevice1;
if ( SUCCEEDED( m_pD3D11Device->QueryInterface( __uuidof( IDXGIDevice1 ), ( void ** )&pDXGIDevice1 ) ) )
{
pDXGIDevice1->SetGPUThreadPriority( 7 );
pDXGIDevice1->SetMaximumFrameLatency( 1 );
pDXGIDevice1->Release();
}
return true;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::SetFullscreen( BOOL bFullscreen )
{
if ( m_pDXGISwapChain && m_pDXGIOutput )
{
// Bail if no change is necessary.
BOOL bState;
if ( SUCCEEDED( m_pDXGISwapChain->GetFullscreenState( &bState, NULL ) ) )
{
if ( bState == bFullscreen )
return;
}
// Bail if not ready to go fullscreen yet.
if ( bFullscreen )
{
if ( m_pDXGISwapChain->Present( 0, DXGI_PRESENT_TEST ) != S_OK )
return;
}
if ( m_pDXGISwapChain->SetFullscreenState( bFullscreen, bFullscreen ? m_pDXGIOutput : NULL ) == S_OK )
{
UpdateBuffers(); // WM_SIZE doesn't get sent since window was created at proper size
}
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::UpdateBuffers()
{
if ( m_pDXGISwapChain )
{
DXGI_SWAP_CHAIN_DESC swapChainDesc;
if ( SUCCEEDED( m_pDXGISwapChain->GetDesc( &swapChainDesc ) ) )
{
m_pD3D11Context->ClearState(); //OMSetRenderTargets(0, NULL, NULL);
m_pDXGISwapChain->ResizeBuffers( 0, ( UINT )m_nDisplayWidth, ( UINT )m_nDisplayHeight, swapChainDesc.BufferDesc.Format, swapChainDesc.Flags );
}
}
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
bool CD3DRender::GetAdapterLuid( int32_t nAdapterIndex, uint64_t *pAdapterLuid )
{
bool bSuccess = false;
if ( m_pDXGIFactory != NULL )
{
IDXGIAdapter *pDXGIAdapter;
if ( SUCCEEDED( m_pDXGIFactory->EnumAdapters( nAdapterIndex, &pDXGIAdapter ) ) )
{
DXGI_ADAPTER_DESC adapterDesc;
pDXGIAdapter->GetDesc( &adapterDesc );
pDXGIAdapter->Release();
*pAdapterLuid = *( uint64_t * )&adapterDesc.AdapterLuid;
bSuccess = true;
}
}
return bSuccess;
}
//--------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------
void CD3DRender::CopyTextureData( BYTE *pDst, uint32_t nDstRowPitch,
const BYTE *pSrc, uint32_t nSrcRowPitch,
uint32_t nWidth, uint32_t nHeight, uint32_t nPitch )
{
if ( nDstRowPitch == nSrcRowPitch )
{
memcpy( pDst, pSrc, nSrcRowPitch * nHeight );
}
else
{
uint32_t nMinRowPitch = min( nDstRowPitch, nSrcRowPitch );
for ( uint32_t i = 0; i < nHeight; i++ )
{
memcpy( pDst, pSrc, nMinRowPitch );
pDst += nDstRowPitch;
pSrc += nSrcRowPitch;
}
}
}