-
Notifications
You must be signed in to change notification settings - Fork 34
/
driver_virtual_display.cpp
746 lines (616 loc) · 23.6 KB
/
driver_virtual_display.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
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
//===================== Copyright (c) Valve Corporation. All Rights Reserved. ======================
//
// Example OpenVR driver for demonstrating IVRVirtualDisplay interface.
//
//==================================================================================================
#include "openvr_driver.h"
#include "sharedstate.h"
#include "threadtools.h"
#include "systemtime.h"
#include "d3drender.h"
namespace
{
//-----------------------------------------------------------------------------
// Settings
//-----------------------------------------------------------------------------
static const char * const k_pch_VirtualDisplay_Section = "driver_virtual_display";
static const char * const k_pch_VirtualDisplay_SerialNumber_String = "serialNumber";
static const char * const k_pch_VirtualDisplay_ModelNumber_String = "modelNumber";
static const char * const k_pch_VirtualDisplay_AdditionalLatencyInSeconds_Float = "additionalLatencyInSeconds";
static const char * const k_pch_VirtualDisplay_DisplayWidth_Int32 = "displayWidth";
static const char * const k_pch_VirtualDisplay_DisplayHeight_Int32 = "displayHeight";
static const char * const k_pch_VirtualDisplay_DisplayRefreshRateNumerator_Int32 = "displayRefreshRateNumerator";
static const char * const k_pch_VirtualDisplay_DisplayRefreshRateDenominator_Int32 = "displayRefreshRateDenominator";
static const char * const k_pch_VirtualDisplay_AdapterIndex_Int32 = "adapterIndex";
//-----------------------------------------------------------------------------
void Log( const char *pFormat, ... )
{
va_list args;
va_start( args, pFormat );
char buffer[ 1024 ];
vsprintf_s( buffer, pFormat, args );
strcat_s( buffer, "\n" );
vr::VRDriverLog()->Log( buffer );
va_end( args );
}
//-----------------------------------------------------------------------------
// Interface to separate process standing in for an actual remote device.
// This needs to be a separate process because D3D blocks gpu work within
// a process on Present.
//-----------------------------------------------------------------------------
class CRemoteDevice
{
public:
CRemoteDevice()
: m_flFrameIntervalInSeconds( 0.0f )
, m_pNewFrame( NULL )
{
ZeroMemory( &m_procInfo, sizeof( m_procInfo ) );
}
~CRemoteDevice()
{}
bool Initialize(
uint32_t nWindowX, uint32_t nWindowY, uint32_t nWindowWidth, uint32_t nWindowHeight,
uint32_t nRefreshRateNumerator, uint32_t nRefreshRateDenominator )
{
if ( nWindowWidth == 0 || nWindowHeight == 0 ||
nRefreshRateNumerator == 0 || nRefreshRateDenominator == 0 )
{
Log( "RemoteDevice: Invalid parameters. w=%d h=%d refresh=%d/%d",
nWindowWidth, nWindowHeight, nRefreshRateNumerator, nRefreshRateDenominator );
return false;
}
m_flFrameIntervalInSeconds = float( nRefreshRateDenominator ) / nRefreshRateNumerator;
if ( !m_sharedState.IsValid() )
{
Log( "RemoteDevice: Failed to initialize shared memory!" );
return false;
}
else
{
CSharedState::Ptr data( &m_sharedState );
data->m_nTextureWidth = 0;
data->m_nTextureHeight = 0;
data->m_flLastVsyncTimeInSeconds = 0.0f;
data->m_nVsyncCounter = 0;
data->m_nSystemBaseTimeTicks = SystemTime::GetBaseTicks();
}
m_pNewFrame = new IPCEvent( "RemoteDisplay_NewFrame", false, false );
if ( m_pNewFrame == NULL )
{
Log( "RemoteDevice: Failed to create IPC event!" );
return false;
}
std::string sInstallPath = vr::VRProperties()->GetStringProperty( vr::VRDriverHandle(), vr::Prop_InstallPath_String );
std::string sWorkingPath = sInstallPath + "\\bin\\win32";
char buffer[ 256 ];
sprintf_s( buffer, "\"%s\\virtual_display.exe\" %d %d %d %d %d %d",
sWorkingPath.c_str(),
nWindowX, nWindowY, nWindowWidth, nWindowHeight,
nRefreshRateNumerator, nRefreshRateDenominator );
Log( "Launching %s", buffer );
STARTUPINFO startupInfo = { 0 };
startupInfo.cb = sizeof( STARTUPINFO );
if ( !CreateProcess( NULL, buffer, NULL, NULL, FALSE, NULL, NULL, sWorkingPath.c_str(), &startupInfo, &m_procInfo ) )
{
Log( "RemoteDevice: Failed to launch external process." );
return false;
}
return true;
}
void Shutdown()
{
if ( m_procInfo.hProcess )
{
TerminateProcess( m_procInfo.hProcess, 0 );
CloseHandle( m_procInfo.hProcess );
CloseHandle( m_procInfo.hThread );
ZeroMemory( &m_procInfo, sizeof( m_procInfo ) );
}
}
float GetFrameIntervalInSeconds() const
{
return m_flFrameIntervalInSeconds;
}
void Transmit( uint32_t nWidth, uint32_t nHeight, DXGI_FORMAT format,
const BYTE *pData, uint32_t nRowPitch, double flVsyncTimeInSeconds )
{
EventWriteString( L"[VDispDvr] Transmit(begin)" );
nWidth = min( nWidth, SharedState_t::MAX_TEXTURE_WIDTH );
nHeight = min( nHeight, SharedState_t::MAX_TEXTURE_HEIGHT );
if ( 1 )
{
CSharedState::Ptr data( &m_sharedState );
data->m_nTextureWidth = nWidth;
data->m_nTextureHeight = nHeight;
data->m_nTextureFormat = format;
data->m_flVsyncTimeInSeconds = flVsyncTimeInSeconds;
CD3DRender::CopyTextureData( data->m_nTextureData, nWidth * SharedState_t::TEXTURE_PITCH,
pData, nRowPitch, nWidth, nHeight, SharedState_t::TEXTURE_PITCH );
}
m_pNewFrame->SetEvent();
EventWriteString( L"[VDispDvr] Transmit(end)" );
}
void GetTimingInfo( double *pflLastVsyncTimeInSeconds, uint32_t *pnVsyncCounter )
{
CSharedState::Ptr data( &m_sharedState );
*pflLastVsyncTimeInSeconds = data->m_flLastVsyncTimeInSeconds;
*pnVsyncCounter = data->m_nVsyncCounter;
}
private:
CSharedState m_sharedState;
IPCEvent *m_pNewFrame;
float m_flFrameIntervalInSeconds;
PROCESS_INFORMATION m_procInfo;
};
//----------------------------------------------------------------------------
// Blocks on reading backbuffer from gpu, so WaitForPresent can return
// as soon as we know rendering made it this frame. This step of the pipeline
// should run about 3ms per frame.
//----------------------------------------------------------------------------
class CEncoder : public CThread
{
public:
CEncoder( CD3DRender *pD3DRender, CRemoteDevice *pRemoteDevice )
: m_pRemoteDevice( pRemoteDevice )
, m_pD3DRender( pD3DRender )
, m_pStagingTexture( NULL )
, m_bExiting( false )
{
m_encodeFinished.Set();
}
~CEncoder()
{
SAFE_RELEASE( m_pStagingTexture );
}
bool CopyToStaging( ID3D11Texture2D *pTexture )
{
// Create a staging texture to copy frame data into that can in turn
// be read back (for blocking until rendering is finished).
if ( m_pStagingTexture == NULL )
{
D3D11_TEXTURE2D_DESC srcDesc;
pTexture->GetDesc( &srcDesc );
D3D11_TEXTURE2D_DESC stagingTextureDesc;
ZeroMemory( &stagingTextureDesc, sizeof( stagingTextureDesc ) );
stagingTextureDesc.Width = srcDesc.Width;
stagingTextureDesc.Height = srcDesc.Height;
stagingTextureDesc.Format = srcDesc.Format;
stagingTextureDesc.MipLevels = 1;
stagingTextureDesc.ArraySize = 1;
stagingTextureDesc.SampleDesc.Count = 1;
stagingTextureDesc.Usage = D3D11_USAGE_STAGING;
stagingTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
if ( FAILED( m_pD3DRender->GetDevice()->CreateTexture2D( &stagingTextureDesc, NULL, &m_pStagingTexture ) ) )
{
Log( "Failed to create staging texture!" );
return false;
}
}
m_pD3DRender->GetContext()->CopyResource( m_pStagingTexture, pTexture );
return true;
}
void Run() override
{
SetThreadPriority( GetCurrentThread(), THREAD_PRIORITY_MOST_URGENT );
while ( !m_bExiting )
{
EventWriteString( L"[VDispDvr] Encoder waiting for new frame..." );
m_newFrameReady.Wait();
if ( m_bExiting )
break;
if ( m_pStagingTexture )
{
D3D11_TEXTURE2D_DESC desc;
m_pStagingTexture->GetDesc( &desc );
EventWriteString( L"[VDispDvr] Map:Staging(begin)" );
D3D11_MAPPED_SUBRESOURCE mapped = { 0 };
if ( SUCCEEDED( m_pD3DRender->GetContext()->Map( m_pStagingTexture, 0, D3D11_MAP_READ, 0, &mapped ) ) )
{
EventWriteString( L"[VDispDvr] Map:Staging(end)" );
m_pRemoteDevice->Transmit( desc.Width, desc.Height, desc.Format,
( const BYTE * )mapped.pData, mapped.RowPitch, m_flVsyncTimeInSeconds );
m_pD3DRender->GetContext()->Unmap( m_pStagingTexture, 0 );
}
}
m_encodeFinished.Set();
}
}
void Stop()
{
m_bExiting = true;
m_newFrameReady.Set();
Join();
}
void NewFrameReady( double flVsyncTimeInSeconds )
{
m_flVsyncTimeInSeconds = flVsyncTimeInSeconds;
m_encodeFinished.Reset();
m_newFrameReady.Set();
}
void WaitForEncode()
{
m_encodeFinished.Wait();
}
private:
CThreadEvent m_newFrameReady, m_encodeFinished;
CRemoteDevice *m_pRemoteDevice;
CD3DRender *m_pD3DRender;
ID3D11Texture2D *m_pStagingTexture;
double m_flVsyncTimeInSeconds;
bool m_bExiting;
};
}
//-----------------------------------------------------------------------------
// Purpose: This object represents our device (registered below).
// It implements the IVRVirtualDisplay component interface to provide us
// hooks into the render pipeline.
//-----------------------------------------------------------------------------
class CDisplayRedirectLatest : public vr::ITrackedDeviceServerDriver, public vr::IVRVirtualDisplay
{
public:
CDisplayRedirectLatest()
: m_unObjectId( vr::k_unTrackedDeviceIndexInvalid )
, m_nGraphicsAdapterLuid( 0 )
, m_flLastVsyncTimeInSeconds( 0.0 )
, m_nVsyncCounter( 0 )
, m_pD3DRender( NULL )
, m_pFlushTexture( NULL )
, m_pRemoteDevice( NULL )
, m_pEncoder( NULL )
{
vr::VRSettings()->GetString( k_pch_VirtualDisplay_Section,
vr::k_pch_Null_SerialNumber_String, m_rchSerialNumber, ARRAYSIZE( m_rchSerialNumber ) );
vr::VRSettings()->GetString( k_pch_VirtualDisplay_Section,
vr::k_pch_Null_ModelNumber_String, m_rchModelNumber, ARRAYSIZE( m_rchModelNumber ) );
m_flAdditionalLatencyInSeconds = max( 0.0f,
vr::VRSettings()->GetFloat( k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_AdditionalLatencyInSeconds_Float ) );
int32_t nDisplayWidth = vr::VRSettings()->GetInt32(
k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_DisplayWidth_Int32 );
int32_t nDisplayHeight = vr::VRSettings()->GetInt32(
k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_DisplayHeight_Int32 );
int32_t nDisplayRefreshRateNumerator = vr::VRSettings()->GetInt32(
k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_DisplayRefreshRateNumerator_Int32 );
int32_t nDisplayRefreshRateDenominator = vr::VRSettings()->GetInt32(
k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_DisplayRefreshRateDenominator_Int32 );
int32_t nAdapterIndex = vr::VRSettings()->GetInt32(
k_pch_VirtualDisplay_Section,
k_pch_VirtualDisplay_AdapterIndex_Int32 );
m_pD3DRender = new CD3DRender();
// First initialize using the specified display dimensions to determine
// which graphics adapter the headset is attached to (if any).
if ( !m_pD3DRender->Initialize( nDisplayWidth, nDisplayHeight ) )
{
Log( "Could not find headset with display size %dx%d.", nDisplayWidth, nDisplayHeight );
return;
}
int32_t nDisplayX, nDisplayY;
m_pD3DRender->GetDisplayPos( &nDisplayX, &nDisplayY );
int32_t nDisplayAdapterIndex;
const int32_t nBufferSize = 128;
wchar_t wchAdapterDescription[ nBufferSize ];
if ( !m_pD3DRender->GetAdapterInfo( &nDisplayAdapterIndex, wchAdapterDescription, nBufferSize ) )
{
Log( "Failed to get headset adapter info!" );
return;
}
char chAdapterDescription[ nBufferSize ];
wcstombs_s( 0, chAdapterDescription, nBufferSize, wchAdapterDescription, nBufferSize );
Log( "Headset connected to %s.", chAdapterDescription );
// If no adapter specified, choose the first one the headset *isn't* plugged into.
if ( nAdapterIndex < 0 )
{
nAdapterIndex = ( nDisplayAdapterIndex == 0 ) ? 1 : 0;
}
else if ( nDisplayAdapterIndex == nAdapterIndex )
{
Log( "Headset needs to be plugged into a separate graphics card." );
return;
}
// Store off the LUID of the primary gpu we want to use.
if ( !m_pD3DRender->GetAdapterLuid( nAdapterIndex, &m_nGraphicsAdapterLuid ) )
{
Log( "Failed to get adapter index for graphics adapter!" );
return;
}
// Now reinitialize using the other graphics card.
if ( !m_pD3DRender->Initialize( nAdapterIndex ) )
{
Log( "Could not create graphics device for adapter %d. Requires a minimum of two graphics cards.", nAdapterIndex );
return;
}
if ( !m_pD3DRender->GetAdapterInfo( &nDisplayAdapterIndex, wchAdapterDescription, nBufferSize ) )
{
Log( "Failed to get primary adapter info!" );
return;
}
wcstombs_s( 0, chAdapterDescription, nBufferSize, wchAdapterDescription, nBufferSize );
Log( "Using %s as primary graphics adapter.", chAdapterDescription );
// Spawn our separate process to manage headset presentation.
m_pRemoteDevice = new CRemoteDevice();
if ( !m_pRemoteDevice->Initialize(
nDisplayX, nDisplayY, nDisplayWidth, nDisplayHeight,
nDisplayRefreshRateNumerator, nDisplayRefreshRateDenominator ) )
{
return;
}
// Spin up a separate thread to handle the overlapped encoding/transmit step.
m_pEncoder = new CEncoder( m_pD3DRender, m_pRemoteDevice );
m_pEncoder->Start();
}
virtual ~CDisplayRedirectLatest()
{
if ( m_pEncoder )
{
m_pEncoder->Stop();
delete m_pEncoder;
}
if ( m_pRemoteDevice )
{
m_pRemoteDevice->Shutdown();
delete m_pRemoteDevice;
}
if ( m_pFlushTexture )
{
m_pFlushTexture->Release();
}
if ( m_pD3DRender )
{
m_pD3DRender->Shutdown();
delete m_pD3DRender;
}
}
bool IsValid() const
{
return m_pEncoder != NULL;
}
// ITrackedDeviceServerDriver
virtual vr::EVRInitError Activate( uint32_t unObjectId ) override
{
m_unObjectId = unObjectId;
vr::PropertyContainerHandle_t ulContainer =
vr::VRProperties()->TrackedDeviceToPropertyContainer( unObjectId );
vr::VRProperties()->SetStringProperty( ulContainer,
vr::Prop_ModelNumber_String, m_rchModelNumber );
vr::VRProperties()->SetFloatProperty( ulContainer,
vr::Prop_SecondsFromVsyncToPhotons_Float, m_flAdditionalLatencyInSeconds );
vr::VRProperties()->SetUint64Property( ulContainer,
vr::Prop_GraphicsAdapterLuid_Uint64, m_nGraphicsAdapterLuid );
return vr::VRInitError_None;
}
virtual void Deactivate() override
{
m_unObjectId = vr::k_unTrackedDeviceIndexInvalid;
}
virtual void *GetComponent( const char *pchComponentNameAndVersion ) override
{
if ( !_stricmp( pchComponentNameAndVersion, vr::IVRVirtualDisplay_Version ) )
{
return static_cast< vr::IVRVirtualDisplay * >( this );
}
return NULL;
}
virtual void EnterStandby() override
{
}
virtual void DebugRequest( const char *pchRequest, char *pchResponseBuffer, uint32_t unResponseBufferSize ) override
{
if( unResponseBufferSize >= 1 )
pchResponseBuffer[0] = 0;
}
virtual vr::DriverPose_t GetPose() override
{
vr::DriverPose_t pose = { 0 };
pose.poseIsValid = true;
pose.result = vr::TrackingResult_Running_OK;
pose.deviceIsConnected = true;
pose.qWorldFromDriverRotation.w = 1;
pose.qWorldFromDriverRotation.x = 0;
pose.qWorldFromDriverRotation.y = 0;
pose.qWorldFromDriverRotation.z = 0;
pose.qDriverFromHeadRotation.w = 1;
pose.qDriverFromHeadRotation.x = 0;
pose.qDriverFromHeadRotation.y = 0;
pose.qDriverFromHeadRotation.z = 0;
return pose;
}
std::string GetSerialNumber()
{
return m_rchSerialNumber;
}
// IVRVirtualDisplay
virtual void Present( vr::SharedTextureHandle_t backbufferTextureHandle ) override
{
// Open and cache our shared textures to avoid re-opening every frame.
ID3D11Texture2D *pTexture = m_pD3DRender->GetSharedTexture( ( HANDLE )backbufferTextureHandle );
if ( pTexture == NULL )
{
EventWriteString( L"[VDispDvr] Texture is NULL!" );
}
else
{
EventWriteString( L"[VDispDvr] Waiting for previous encode to finish..." );
// Wait for the encoder to be ready. This is important because the encoder thread
// blocks on transmit which uses our shared d3d context (which is not thread safe).
m_pEncoder->WaitForEncode();
EventWriteString( L"[VDispDvr] Done" );
// Access to shared texture must be wrapped in AcquireSync/ReleaseSync
// to ensure the compositor has finished rendering to it before it gets used.
// This enforces scheduling of work on the gpu between processes.
IDXGIKeyedMutex *pKeyedMutex = NULL;
if ( SUCCEEDED( pTexture->QueryInterface( __uuidof( IDXGIKeyedMutex ), ( void ** )&pKeyedMutex ) ) )
{
if ( pKeyedMutex->AcquireSync( 0, 10 ) != S_OK )
{
pKeyedMutex->Release();
EventWriteString( L"[VDispDvr] ACQUIRESYNC FAILED!!!" );
return;
}
}
EventWriteString( L"[VDispDvr] AcquiredSync" );
if ( m_pFlushTexture == NULL )
{
D3D11_TEXTURE2D_DESC srcDesc;
pTexture->GetDesc( &srcDesc );
// Create a second small texture for copying and reading a single pixel from
// in order to block on the cpu until rendering is finished.
D3D11_TEXTURE2D_DESC flushTextureDesc;
ZeroMemory( &flushTextureDesc, sizeof( flushTextureDesc ) );
flushTextureDesc.Width = 32;
flushTextureDesc.Height = 32;
flushTextureDesc.MipLevels = 1;
flushTextureDesc.ArraySize = 1;
flushTextureDesc.Format = srcDesc.Format;
flushTextureDesc.SampleDesc.Count = 1;
flushTextureDesc.Usage = D3D11_USAGE_STAGING;
flushTextureDesc.BindFlags = 0;
flushTextureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
if ( FAILED( m_pD3DRender->GetDevice()->CreateTexture2D( &flushTextureDesc, NULL, &m_pFlushTexture ) ) )
{
Log( "Failed to create flush texture!" );
return;
}
}
// Copy a single pixel so we can block until rendering is finished in WaitForPresent.
D3D11_BOX box = { 0, 0, 0, 1, 1, 1 };
m_pD3DRender->GetContext()->CopySubresourceRegion( m_pFlushTexture, 0, 0, 0, 0, pTexture, 0, &box );
EventWriteString( L"[VDispDvr] Flush-Begin" );
// This can go away, but is useful to see it as a separate packet on the gpu in traces.
m_pD3DRender->GetContext()->Flush();
EventWriteString( L"[VDispDvr] Flush-End" );
// Copy entire texture to staging so we can read the pixels to send to remote device.
m_pEncoder->CopyToStaging( pTexture );
EventWriteString( L"[VDispDvr] Flush-Staging(begin)" );
m_pD3DRender->GetContext()->Flush();
EventWriteString( L"[VDispDvr] Flush-Staging(end)" );
if ( pKeyedMutex )
{
pKeyedMutex->ReleaseSync( 0 );
pKeyedMutex->Release();
}
EventWriteString( L"[VDispDvr] ReleasedSync" );
}
}
virtual void WaitForPresent() override
{
EventWriteString( L"[VDispDvr] WaitForPresent(begin)" );
// First wait for rendering to finish on the gpu.
if ( m_pFlushTexture )
{
D3D11_MAPPED_SUBRESOURCE mapped = { 0 };
if ( SUCCEEDED( m_pD3DRender->GetContext()->Map( m_pFlushTexture, 0, D3D11_MAP_READ, 0, &mapped ) ) )
{
EventWriteString( L"[VDispDvr] Mapped FlushTexture" );
m_pD3DRender->GetContext()->Unmap( m_pFlushTexture, 0 );
}
}
EventWriteString( L"[VDispDvr] RenderingFinished" );
// Now that we know rendering is done, we can fire off our thread that reads the
// backbuffer into system memory. We also pass in the earliest time that this frame
// should get presented. This is the real vsync that starts our frame.
m_pEncoder->NewFrameReady( m_flLastVsyncTimeInSeconds + m_flAdditionalLatencyInSeconds );
// Get latest timing info to work with. This gets us sync'd up with the hardware in
// the first place, and also avoids any drifting over time.
double flLastVsyncTimeInSeconds;
uint32_t nVsyncCounter;
m_pRemoteDevice->GetTimingInfo( &flLastVsyncTimeInSeconds, &nVsyncCounter );
// Account for encoder/transmit latency.
// This is where the conversion from real to virtual vsync happens.
flLastVsyncTimeInSeconds -= m_flAdditionalLatencyInSeconds;
float flFrameIntervalInSeconds = m_pRemoteDevice->GetFrameIntervalInSeconds();
// Realign our last time interval given updated timing reference.
int32_t nTimeRefToLastVsyncFrames =
( int32_t )roundf( float( m_flLastVsyncTimeInSeconds - flLastVsyncTimeInSeconds ) / flFrameIntervalInSeconds );
m_flLastVsyncTimeInSeconds = flLastVsyncTimeInSeconds + flFrameIntervalInSeconds * nTimeRefToLastVsyncFrames;
// We could probably just use this instead, but it seems safer to go off the system timer calculation.
assert( m_nVsyncCounter == nVsyncCounter + nTimeRefToLastVsyncFrames );
double flNow = SystemTime::GetInSeconds();
// Find the next frame interval (keeping in mind we may get here during running start).
int32_t nLastVsyncToNextVsyncFrames =
( int32_t )( float( flNow - m_flLastVsyncTimeInSeconds ) / flFrameIntervalInSeconds );
nLastVsyncToNextVsyncFrames = max( nLastVsyncToNextVsyncFrames, 0 ) + 1;
// And store it for use in GetTimeSinceLastVsync (below) and updating our next frame.
m_flLastVsyncTimeInSeconds += flFrameIntervalInSeconds * nLastVsyncToNextVsyncFrames;
m_nVsyncCounter = nVsyncCounter + nTimeRefToLastVsyncFrames + nLastVsyncToNextVsyncFrames;
EventWriteString( L"[VDispDvr] WaitForPresent(end)" );
}
virtual bool GetTimeSinceLastVsync( float *pfSecondsSinceLastVsync, uint64_t *pulFrameCounter ) override
{
*pfSecondsSinceLastVsync = ( float )( SystemTime::GetInSeconds() - m_flLastVsyncTimeInSeconds );
*pulFrameCounter = m_nVsyncCounter;
return true;
}
private:
uint32_t m_unObjectId;
char m_rchSerialNumber[ 1024 ];
char m_rchModelNumber[ 1024 ];
uint64_t m_nGraphicsAdapterLuid;
float m_flAdditionalLatencyInSeconds;
double m_flLastVsyncTimeInSeconds;
uint32_t m_nVsyncCounter;
CD3DRender *m_pD3DRender;
ID3D11Texture2D *m_pFlushTexture;
CRemoteDevice *m_pRemoteDevice;
CEncoder *m_pEncoder;
};
//-----------------------------------------------------------------------------
// Purpose: Server interface implementation.
//-----------------------------------------------------------------------------
class CServerDriver_DisplayRedirect : public vr::IServerTrackedDeviceProvider
{
public:
CServerDriver_DisplayRedirect()
: m_pDisplayRedirectLatest( NULL )
{}
virtual vr::EVRInitError Init( vr::IVRDriverContext *pContext ) override;
virtual void Cleanup() override;
virtual const char * const *GetInterfaceVersions() override
{ return vr::k_InterfaceVersions; }
virtual const char *GetTrackedDeviceDriverVersion()
{ return vr::ITrackedDeviceServerDriver_Version; }
virtual void RunFrame() override {}
virtual bool ShouldBlockStandbyMode() override { return false; }
virtual void EnterStandby() override {}
virtual void LeaveStandby() override {}
private:
CDisplayRedirectLatest *m_pDisplayRedirectLatest;
};
vr::EVRInitError CServerDriver_DisplayRedirect::Init( vr::IVRDriverContext *pContext )
{
VR_INIT_SERVER_DRIVER_CONTEXT( pContext );
m_pDisplayRedirectLatest = new CDisplayRedirectLatest();
if ( m_pDisplayRedirectLatest->IsValid() )
{
vr::VRServerDriverHost()->TrackedDeviceAdded(
m_pDisplayRedirectLatest->GetSerialNumber().c_str(),
vr::TrackedDeviceClass_DisplayRedirect,
m_pDisplayRedirectLatest );
}
return vr::VRInitError_None;
}
void CServerDriver_DisplayRedirect::Cleanup()
{
delete m_pDisplayRedirectLatest;
m_pDisplayRedirectLatest = NULL;
VR_CLEANUP_SERVER_DRIVER_CONTEXT();
}
CServerDriver_DisplayRedirect g_serverDriverDisplayRedirect;
//-----------------------------------------------------------------------------
// Purpose: Entry point for vrserver when loading drivers.
//-----------------------------------------------------------------------------
extern "C" __declspec( dllexport )
void *HmdDriverFactory( const char *pInterfaceName, int *pReturnCode )
{
if ( 0 == strcmp( vr::IServerTrackedDeviceProvider_Version, pInterfaceName ) )
{
return &g_serverDriverDisplayRedirect;
}
if( pReturnCode )
*pReturnCode = vr::VRInitError_Init_InterfaceNotFound;
return NULL;
}