-
Notifications
You must be signed in to change notification settings - Fork 0
/
VSyncWin.cpp
1150 lines (898 loc) · 27.9 KB
/
VSyncWin.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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "stdafx.h"
#include "VSyncWin.h"
#pragma comment (lib, "D3D11.lib")
#pragma comment (lib, "D2D1.lib" )
VSyncWin* VSyncWin::Self;
VSyncWin::VSyncWin() :
mBlitHandled(true),
mBlittingThreadRun(true),
mBlit(false),
mVSyncTimeStamp(0)
{
mCurrentBitmap = 1;
mLastUpdatedBitmap = 2;
mLastCurrentBitmap = 3;
mLastBlittedTimeStamp = Now();
}
VSyncWin::~VSyncWin()
{
}
TimeStamp VSyncWin::Now()
{
LARGE_INTEGER l;
::QueryPerformanceCounter(&l);
return l.QuadPart;
//return GetTickCount64();
}
TimeStamp gTS;
LARGE_INTEGER gS, gE, gSS;
TimeStamp VSyncWin::GetAdjustedVsyncTimeStamp(LARGE_INTEGER& aFrequency,
QPC_TIME& aQpcVblankTime)
{
char buf[1024];
TimeStamp vsync = Now();
LARGE_INTEGER qpcNow;
QueryPerformanceCounter(&qpcNow);
const int microseconds = 1000000;
int64_t adjust = qpcNow.QuadPart - aQpcVblankTime;
int64_t usAdjust = (adjust * microseconds) / aFrequency.QuadPart;
sprintf_s(buf, "\n((((( usadjust %lld and", usAdjust);
OutputDebugStringA(buf);
sprintf_s(buf, "\n(((((Vsync %llu", aQpcVblankTime);
OutputDebugStringA(buf);
vsync -= FromMicroseconds((double)usAdjust);
//if (IsWin10OrLater())
{
// On Windows 10 and on, DWMGetCompositionTimingInfo, mostly
// reports the upcoming vsync time, which is in the future.
// It can also sometimes report a vblank time in the past.
// Since large parts of Gecko assume TimeStamps can't be in future,
// use the previous vsync.
// Windows 10 and Intel HD vsync timestamps are messy and
// all over the place once in a while. Most of the time,
// it reports the upcoming vsync. Sometimes, that upcoming
// vsync is in the past. Sometimes that upcoming vsync is before
// the previously seen vsync. Sometimes, the previous vsync
// is still in the future. In these error cases,
// we try to normalize to Now().
TimeStamp upcomingVsync = vsync;
if (upcomingVsync < mPrevVsync) {
// Windows can report a vsync that's before
// the previous one. So update it to sometime in the future.
upcomingVsync = Now() + FromMilliseconds(1);
}
vsync = mPrevVsync;
sprintf_s(buf, "\n)))) vsync %llu---", vsync);
OutputDebugStringA(buf);
mPrevVsync = upcomingVsync;
}
// On Windows 7 and 8, DwmFlush wakes up AFTER qpcVBlankTime
// from DWMGetCompositionTimingInfo. We can return the adjusted vsync.
// Once in a while, the reported vsync timestamp can be in the future.
// Normalize the reported timestamp to now.
if (vsync >= Now()) {
vsync = Now();
}
return vsync;
}
void VSyncWin::VSyncLoop()
{
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
DWM_TIMING_INFO vblankTime;
// Make sure to init the cbSize, otherwise GetCompositionTiming will fail
vblankTime.cbSize = sizeof(DWM_TIMING_INFO);
LARGE_INTEGER frequency;
QueryPerformanceFrequency(&frequency);
TimeStamp vsync = Now();
// On Windows 10, DwmGetCompositionInfo returns the upcoming vsync.
// See GetAdjustedVsyncTimestamp.
// On start, set mPrevVsync to the "next" vsync
// So we'll use this timestamp on the 2nd loop iteration.
mPrevVsync = vsync + mSoftwareVsyncRate;
for (;;) {
#if !USING_DWM_FLUSH
TimeStamp here = Now();
::QueryPerformanceCounter(&gSS);
//Sleep(1);
//Sleep(200);
m_pOutput->WaitForVBlank();
//Sleep(1);
m_pSwapChain->Present(0, 0);
//DwmFlush();
//Gdi: NtGdiDdDDIWaitForVerticalBlankEvent()
//TimerBlit();
//gS = gSS;
//::PostMessageA(gHwnd, WM_BLIT_REQUEST, 0, 0);
//TimerBlit();
FILE *f;
fopen_s(&f, "C:\\VSyncThread", "rb");
//char buf[200];
LARGE_INTEGER fi;
char buf[200];
::QueryPerformanceFrequency(&fi);
sprintf_s(buf, "\n+++++ %d", int((Now() - here) * 1000000 / fi.QuadPart));
OutputDebugStringA(buf);
#else
::QueryPerformanceCounter(&gS);
gTS = vsync;
/*mWork = CreateThreadpoolWork(BlittingThread,
(void*)&gTS,
&mCallBackEnviron);
SubmitThreadpoolWork(mWork);*/
//mVSyncTimeStamp.store(vsync);
char buf[1000];
LARGE_INTEGER fi;
//::QueryPerformanceFrequency(&fi);
//sprintf_s(buf, "\n+++++%d", int((vsync - here) * 1000000 / fi.QuadPart));
//OutputDebugStringA(buf);
// Use a combination of DwmFlush + DwmGetCompositionTimingInfoPtr
// Using WaitForVBlank, the whole system dies :/
//m_pSwapChain->Present(0, 0);
//::PostMessageA(gHwnd, WM_BLIT_REQUEST, 0, 0);
//m_pSwapChain->Present(1, 0);
//TimerBlit();
HRESULT hr;
hr = DwmFlush();//WinUtils::dwmFlushProcPtr();
if (!SUCCEEDED(hr)) {
// // We don't actually know how long we had to wait on DWMFlush
// // Instead of trying to calculate how long DwmFlush actually took
// // Fallback to software vsync.
// //-->ScheduleSoftwareVsync(Now());
return;
}
//dwmflush does not exactly return at next vblank
//TimerBlit();
hr = DwmGetCompositionTimingInfo(0, &vblankTime);
/*vsync = SUCCEEDED(hr) ?
GetAdjustedVsyncTimeStamp(frequency, vblankTime.qpcVBlank) :
Now();*/
vsync = vblankTime.qpcVBlank;// +vblankTime.qpcRefreshPeriod;
mVSyncTimeStamp.store(vsync);
mBlit = true;
////char buf[200];
//LARGE_INTEGER l;
//
//QueryPerformanceFrequency(&l);
////if (((vsync - Now()) * 1000000 / l.QuadPart) > 10000)
// //continue;
///*LARGE_INTEGER le, lf;
//QueryPerformanceCounter(&le);
//QueryPerformanceFrequency(&lf);*/
//sprintf_s(buf, "\nvblank Time Now = %u", (vsync - Now()) * 1000000 / l.QuadPart);
//OutputDebugStringA(buf);
//sprintf_s(buf, "\n****** ---- %llu --", vsync);
//OutputDebugStringA(buf);
/*sprintf_s(buf, "\nv Timed %d ", ((le.QuadPart - gS.QuadPart) * 1000000 / lf.QuadPart));
OutputDebugStringA(buf);*/
#endif
} // end for
}
void VSyncWin::VSyncInit()
{
Self = this;
CoInitialize(NULL);
HRESULT hr;
//hr = CreateDXGIFactory2(__uuidof(IDXGIFactory1), (void**)(&pFactory));
//if (FAILED(hr))
//{
// exit(0);
//}
//UINT i = 0;
////IDXGIAdapter1*> vAdapters;
//while (pFactory->EnumAdapters1(i, &m_pAdapter) != DXGI_ERROR_NOT_FOUND)
//{
// //vAdapters.push_back(pAdapter);
// //pAdapter->EnumOutputs()
// ++i;
// break; //we want the first adapter
//}
//if (m_pAdapter->EnumOutputs(0, &m_pOutput) == DXGI_ERROR_NOT_FOUND)
//{
// //error
// exit(0);
//}
RECT rc;
GetClientRect(gHwnd, &rc);
UINT width = rc.right - rc.left;
UINT height = rc.bottom - rc.top;
// for d2d support.. make it 0 not work
UINT createDeviceFlags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
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,
D3D_FEATURE_LEVEL_9_3,
D3D_FEATURE_LEVEL_9_2,
D3D_FEATURE_LEVEL_9_1
};
UINT numFeatureLevels = ARRAYSIZE(featureLevels);
for (UINT driverTypeIndex = 0; driverTypeIndex < numDriverTypes; driverTypeIndex++)
{
mDriverType = driverTypes[driverTypeIndex];
hr = D3D11CreateDevice(nullptr, mDriverType, nullptr, createDeviceFlags, featureLevels, numFeatureLevels,
D3D11_SDK_VERSION, &m_pD3dDevice, &mFeatureLevel, &m_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, mDriverType, nullptr, createDeviceFlags, &featureLevels[1], numFeatureLevels - 1,
D3D11_SDK_VERSION, &m_pD3dDevice, &mFeatureLevel, &m_pImmediateContext);
}
if (SUCCEEDED(hr))
break;
}
if (FAILED(hr))
exit(0);
const double rate = 1000 / 60.0;
mSoftwareVsyncRate = FromMilliseconds(rate);
mPrevVsync = Now();
/*hr = pFactory->CreateSwapChain(m_pD3dDevice, &scd, &m_pSwapChain);
if ( FAILED(hr) )
exit(0);
*/
hr = m_pD3dDevice->QueryInterface(__uuidof(IDXGIDevice2), (void **)&m_pDXGIDevice);
if (FAILED(hr))
{
exit(0);
}
IDXGIAdapter * pDXGIAdapter;
hr = m_pDXGIDevice->GetParent(__uuidof(IDXGIAdapter), (void **)&pDXGIAdapter);
if (FAILED(hr))
{
exit(0);
}
pDXGIAdapter->GetParent(__uuidof(IDXGIFactory2), (void **)& pFactory);
if (FAILED(hr))
{
exit(0);
}
hr = pFactory->EnumAdapters1(0, &m_pAdapter);
if (FAILED(hr))
exit(0);
hr = m_pAdapter->EnumOutputs(0, &m_pOutput);
if (FAILED(hr))
exit(0);
DXGI_OUTPUT_DESC desc;
hr = m_pOutput->GetDesc(&desc);
if (FAILED(hr))
exit(0);
RECT r;
GetClientRect(gHwnd, &r);
gHeight = r.bottom - r.top;
gWidth = r.right - r.left;
DXGI_SWAP_CHAIN_DESC1 scd = { 0 };
//scd.BufferDesc.Width = gWidth;
//scd.BufferDesc.Height = gHeight;
//scd.BufferDesc.RefreshRate.Numerator = 60;
//scd.BufferDesc.RefreshRate.Denominator = 1;
//scd.BufferDesc.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
//scd.BufferDesc.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
//scd.BufferDesc.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
//scd.SampleDesc.Count = 1;
//scd.SampleDesc.Quality = 0;
//scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
//scd.BufferCount = 3; //1
//scd.OutputWindow = gHwnd;
//scd.Windowed = true;
//scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;// DXGI_SWAP_EFFECT_SEQUENTIAL;//DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL;
//scd.Flags = DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE;
scd.BufferUsage = DXGI_USAGE_BACK_BUFFER | DXGI_USAGE_RENDER_TARGET_OUTPUT; // how the swap chain should be used
scd.BufferCount = 3; // a front buffer and a back buffer
scd.Format = DXGI_FORMAT_B8G8R8A8_UNORM; // the most common swap chain format
scd.SwapEffect = DXGI_SWAP_EFFECT_FLIP_SEQUENTIAL; // the recommended flip mode
scd.SampleDesc.Count = 1;
scd.Scaling = DXGI_SCALING_NONE;
scd.Flags = DXGI_SWAP_CHAIN_FLAG_GDI_COMPATIBLE;
scd.Width = gWidth;
scd.Height = gHeight;
hr = pFactory->CreateSwapChainForHwnd(m_pD3dDevice, gHwnd, &scd, NULL, NULL, &m_pSwapChain);
if (FAILED(hr))
{
exit(0);
}
hr = m_pSwapChain->GetBuffer(0, __uuidof(m_pBackBuffer), reinterpret_cast<void**>(&m_pBackBuffer));
if (FAILED(hr))
exit(0);
hr = m_pD3dDevice->CreateRenderTargetView(m_pBackBuffer, NULL, &m_pRenderTarget );
if (FAILED(hr))
exit(0);
m_pImmediateContext->OMSetRenderTargets(1, &m_pRenderTarget, NULL);
DXGI_MODE_DESC requestedMode;
requestedMode.Width = gWidth;
requestedMode.Height = gHeight;
requestedMode.RefreshRate.Numerator = 0;
requestedMode.RefreshRate.Denominator = 0;
requestedMode.Format = DXGI_FORMAT_B8G8R8A8_UNORM;
requestedMode.ScanlineOrdering = DXGI_MODE_SCANLINE_ORDER_UNSPECIFIED;
requestedMode.Scaling = DXGI_MODE_SCALING_UNSPECIFIED;
DXGI_MODE_DESC mode;
hr = m_pOutput->FindClosestMatchingMode(&requestedMode, &mode, m_pD3dDevice);
if (FAILED(hr))
throw - 1;
hr = m_pSwapChain->ResizeTarget(&mode);
if (FAILED(hr))
throw -1;
mode.RefreshRate.Numerator = 0;
mode.RefreshRate.Denominator = 0;
hr = m_pSwapChain->ResizeTarget(&mode);
if (FAILED(hr))
throw - 1;
D2D1_FACTORY_OPTIONS options;
ZeroMemory(&options, sizeof(D2D1_FACTORY_OPTIONS));
hr = D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,&m_pD2D1Factory);
if (FAILED(hr))
{
exit(0);
}
hr = m_pD2D1Factory->CreateDevice(m_pDXGIDevice, &m_pD2D1Device);
if (FAILED(hr))
{
exit(0);
}
hr = m_pD2D1Device->CreateDeviceContext(
D2D1_DEVICE_CONTEXT_OPTIONS_NONE,
&m_pD2DContext
);
if (FAILED(hr))
{
exit(0);
}
// Direct2D needs the dxgi version of the backbuffer surface pointer.
hr = m_pSwapChain->GetBuffer(0, IID_PPV_ARGS(&m_pDxgiBackBuffer));
if (FAILED(hr))
exit(0);
auto d2dRTProps = D2D1::RenderTargetProperties(D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED), 96.0, 96.0);
/*D2D1_BITMAP_PROPERTIES1 bitmapProperties = D2D1::BitmapProperties1(
D2D1_BITMAP_OPTIONS_TARGET | D2D1_BITMAP_OPTIONS_CANNOT_DRAW,
D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_PREMULTIPLIED),
96.0,
96.0
);*/
// Get a D2D surface from the DXGI back buffer to use as the D2D render target.
/*hr = m_pD2DContext->CreateBitmapFromDxgiSurface(
m_pDxgiBackBuffer,
d2dRTProps,
&m_pD2DTargetBitmap
);
if (FAILED(hr))
exit(0);*/
hr = m_pD2D1Factory->CreateDxgiSurfaceRenderTarget(m_pDxgiBackBuffer, &d2dRTProps, &m_pD2DRenderTarget);
if (FAILED(hr))
exit(0);
//m_pD2DContext->SetTarget(m_pD2DTargetBitmap);
//m_pDXGIDevice->SetMaximumFrameLatency(3);
#if 0//VSYNC
//Creating a thread
std::thread vsync([this] { this->VSyncLoop(); });
vsync.detach();
#endif
#if 0//USING_DWM_FLUSH
std::thread VSyncBlit([this] { this->BlittingThread(); });
VSyncBlit.detach();
#endif
HDC hdc = GetDC(gHwnd);
mDCBitmap1 = CreateCompatibleDC(hdc);
mDCBitmap2 = CreateCompatibleDC(hdc);
mDCBitmap3 = CreateCompatibleDC(hdc);
//HDC hdc = GetDC(hWnd);
RECT rect;
GetWindowRect(gHwnd, &rect);
gWidth = rect.right - rect.left;
gHeight = rect.bottom - rect.top;
mBitmap1 = CreateCompatibleBitmap(hdc, gWidth, gHeight);
mBitmap2 = CreateCompatibleBitmap(hdc, gWidth, gHeight);
mBitmap3 = CreateCompatibleBitmap(hdc, gWidth, gHeight);
SelectObject(mDCBitmap1, mBitmap1);
SelectObject(mDCBitmap2, mBitmap2);
SelectObject(mDCBitmap3, mBitmap3);
DeleteDC(hdc);
InitializeThreadpoolEnvironment(&mCallBackEnviron);
mPool = CreateThreadpool(NULL);
if (NULL == mPool) {
OutputDebugStringA("CreateThreadpool failed. LastError: %u\n");
exit(0);
}
//SetThreadpoolThreadMaximum(mPool, 1);
//SetThreadpoolCallbackPool(&mCallBackEnviron, mPool);
}
inline int static GetTheCurrentbufferToUse(int currentBitmap, int lastUpdatedBitmap)
{
if (currentBitmap == 1)
{
if (lastUpdatedBitmap == 2)
return 3;
else
return 2;
}
else if (currentBitmap == 2)
{
if (lastUpdatedBitmap == 3)
return 1;
else
return 3;
}
else
{
if (lastUpdatedBitmap == 2)
return 1;
else
return 2;
}
}
int xxx;
int startXxx = 50;
void VSyncWin::BitBltEx(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop)
{
/**Triple Buffering ***/
#if 0
char buf[200];
int currentBitmapToUSe;
if (mCurrentBitmap != mLastUpdatedBitmap)
{
currentBitmapToUSe = GetTheCurrentbufferToUse(mCurrentBitmap, mLastUpdatedBitmap);
}
else
{
currentBitmapToUSe = GetTheCurrentbufferToUse(mCurrentBitmap, mLastCurrentBitmap);
}
switch (currentBitmapToUSe)
{
case 1:
if (mBitmapLock1.try_lock())
{
//mOldBitmap = static_cast<HBITMAP>(SelectObject(mDCBitmap1, mBitmap1));
BitBlt(mDCBitmap1, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
mTimestamp1 = Now();
//SelectObject(mDCBitmap1, mOldBitmap);
mBitmapLock1.unlock();
}
else
{
xxx++;
}
mLastUpdatedBitmap = 1;
break;
case 2:
if (mBitmapLock2.try_lock())
{
//mOldBitmap = static_cast<HBITMAP>(SelectObject(mDCBitmap2, mBitmap2));
BitBlt(mDCBitmap2, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
mTimestamp2 = Now();
//SelectObject(mDCBitmap2, mOldBitmap);
mBitmapLock2.unlock();
}
else
{
xxx++;
}
mLastUpdatedBitmap = 2;
break;
case 3:
if (mBitmapLock3.try_lock())
{
//mOldBitmap = static_cast<HBITMAP>(SelectObject(mDCBitmap3, mBitmap3));
BitBlt(mDCBitmap3, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop);
mTimestamp3 = Now();
//SelectObject(mDCBitmap3, mOldBitmap);
mBitmapLock3.unlock();
}
else
{
xxx++;
}
mLastUpdatedBitmap = 3;
break;
}
//sprintf_s(buf, "\nCurrentBitmapUsedToBuufer %d %d %lld\n", currentBitmapToUSe, 6 - (currentBitmapToUSe + mCurrentBitmap), Now());
//sprintf_s(buf, "\mCurrentBitmap.... %d %d %lld\n", mCurrentBitmap, mLastCurrentBitmap, Now());
//OutputDebugStringA(buf);
/*
//mCurrentBitmapMutex.lock();
{
if (mCurrentBitmap.load()) //if mCurrentBitmap = 1 means blit should use mDCBitmap1 (1st buffer)
{
//so we are copying to the other buffer (0)
BitBlt(mDCBitmap1, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
//flip
mCurrentBitmap.store(0);
}
else //if mCurrentBitmap = 0 means blit should use mDCBitmap2(2nd buffer)
{
BitBlt(mDCBitmap2, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
//flip(0)
mCurrentBitmap.store(1);
}
//mCurrentBitmapMutex.unlock();
}
*/
#else
HRESULT hr;
m_pD2DRenderTarget->BeginDraw();
m_pD2DRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::White));
ID2D1SolidColorBrush *m_pBlackBrush;
hr = m_pD2DRenderTarget->CreateSolidColorBrush(
D2D1::ColorF(D2D1::ColorF::Black),
&m_pBlackBrush
);
D2D1_ELLIPSE ellipse = D2D1::Ellipse(
D2D1::Point2F(startXxx, 100.f),
75.f,
50.f
);
startXxx += 16;
if (startXxx > gWidth)
startXxx = 0;
m_pD2DRenderTarget->DrawEllipse(ellipse, m_pBlackBrush, 30.f); m_pD2DRenderTarget->EndDraw();
//IDXGISurface1* pSurface1 = NULL;
//hr = m_pSwapChain->GetBuffer(0, __uuidof(IDXGISurface1), (void**)&pSurface1);
ID3D11RenderTargetView* dv = NULL;
float color[4] = { 5.0f, .2f, 1.0f, 1.0f };
//m_pImmediateContext->ClearRenderTargetView(m_pRenderTarget, color);
//HDC hDC;
//
//hr = pSurface1->GetDC(FALSE, &hDC);
//
////DRAW
//bool dd = BitBlt(hDC, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
////BitBlt(GetDC(gHwnd), nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, SRCCOPY);
//ReleaseDC(gHwnd ,hDC);
//pSurface1->ReleaseDC(NULL);
//pSurface1->Release();
FILE *f;
fopen_s(&f, "C:\\PresentWithSwapChain", "rb");
LARGE_INTEGER as, ae, af;
QueryPerformanceFrequency(&af);
QueryPerformanceCounter(&as);
DXGI_PRESENT_PARAMETERS pp;
pp.DirtyRectsCount = 0;
pp.pDirtyRects = NULL;
pp.pScrollRect = NULL;
pp.pScrollOffset = NULL;
//static int i = 0;
m_pSwapChain->Present(1, 0);
QueryPerformanceCounter(&ae);
char buf[200];
ae.QuadPart = ae.QuadPart - as.QuadPart;
ae.QuadPart *= 1000000;
ae.QuadPart /= af.QuadPart;
sprintf_s(buf, "\nval %d", ae.QuadPart);
OutputDebugStringA(buf);
#endif
}
void VSyncWin::TimerBlit()
{
#if 0
//mBitmapLock.lock();
{
//here we can blit
char buf[200];
#if VSYNC
if (mLastUpdatedBitmap == mCurrentBitmap)
return;
int temp = mCurrentBitmap;
mCurrentBitmap.store(mLastUpdatedBitmap);
mLastCurrentBitmap.store(temp);
if (mCurrentBitmap == 1)
{
//if (mBitmapLock1.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp1)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
// mBitmapLock1.unlock();
//}
//else
//{
// xxx++;
//}
////mCurrentBitmap.store(0);
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
}
else if (mCurrentBitmap == 2)
{
//if (mBitmapLock2.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp2)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
// mBitmapLock2.unlock();
//}
//else
//{
// xxx++;
//}
////mCurrentBitmap.store(1);
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
}
else
{
//if (mBitmapLock3.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp3)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
// mBitmapLock3.unlock();
//}
//else{
// xxx++;
//}
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
}
//mLastBlittedTimeStamp = Now();
/*switch (mCurrentBitmap)
{
case 1:
sprintf_s(buf, "\n^^^^^^^^^^^^^^^^^CurrentBitmap.... %lld\n", mTimestamp1);
break;
case 2:
sprintf_s(buf, "\n^^^^^^^^^^^^^^^^^CurrentBitmap.... %lld\n", mTimestamp2);
break;
case 3:
sprintf_s(buf, "\n^^^^^^^^^^^^^^^^^CurrentBitmap.... %lld\n", mTimestamp3);
break;
}
OutputDebugStringA(buf);*/
::QueryPerformanceCounter(&gE);
gE.QuadPart -= gSS.QuadPart;
gE.QuadPart *= 1000000;
LARGE_INTEGER f;
::QueryPerformanceFrequency(&f);
gE.QuadPart /= f.QuadPart;
//char buf[200];
sprintf_s(buf, "\n******* %d", gE.QuadPart);
OutputDebugStringA(buf);
#endif
//mBitmapLock.unlock();
}
//mBlitHandled = true;
#else
if (mLastUpdatedBitmap == mCurrentBitmap)
return;
IDXGISurface1* pSurface1 = NULL;
HRESULT hr;
hr = m_pSwapChain->GetBuffer(0, __uuidof(IDXGISurface1), (void**)&pSurface1);
if (FAILED(hr))
exit(0);
HDC hDC;
hr = pSurface1->GetDC(FALSE, &hDC);
if (FAILED(hr))
{
char msg[1024];
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&msg, 0, NULL);
OutputDebugStringA(msg);
}
int temp = mCurrentBitmap;
mCurrentBitmap.store(mLastUpdatedBitmap);
mLastCurrentBitmap.store(temp);
if (mCurrentBitmap == 1)
{
//BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
BitBlt(hDC, 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
}
else if (mCurrentBitmap == 2)
{
//BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
BitBlt(hDC, 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
}
else
{
//BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
BitBlt(hDC, 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
}
ReleaseDC(gHwnd, hDC);
pSurface1->ReleaseDC(NULL);
LARGE_INTEGER as, ae, af;
QueryPerformanceFrequency(&af);
QueryPerformanceCounter(&as);
//Present
m_pSwapChain->Present(1, DXGI_PRESENT_RESTART);
char buf[200];
QueryPerformanceCounter(&ae);
ae.QuadPart = ae.QuadPart - as.QuadPart;
ae.QuadPart *= 1000000;
ae.QuadPart /= af.QuadPart;
sprintf_s(buf, "\nval %d\n", ae.QuadPart);
OutputDebugStringA(buf);
#endif
}
void VSyncWin::TimerBlit(HDC hdc)
{
FILE *f;
fopen_s(&f, "c:\\Blit", "rb");
//mBitmapLock.lock();
{
//here we can blit
char buf[200];
#if VSYNC
if (mLastUpdatedBitmap == mCurrentBitmap)
return;
int temp = mCurrentBitmap;
mCurrentBitmap.store(mLastUpdatedBitmap);
mLastCurrentBitmap.store(temp);
if (mCurrentBitmap == 1)
{
//if (mBitmapLock1.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp1)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
// mBitmapLock1.unlock();
//}
//else
//{
// xxx++;
//}
//mCurrentBitmap.store(0);
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap1, 0, 0, SRCCOPY);
}
else if (mCurrentBitmap == 2)
{
//if (mBitmapLock2.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp2)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
// mBitmapLock2.unlock();
//}
//else
//{
// xxx++;
//}
//mCurrentBitmap.store(1);
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap2, 0, 0, SRCCOPY);
}
else
{
//if (mBitmapLock3.try_lock())
//{
// //if (mLastBlittedTimeStamp < mTimestamp3)
// BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
// mBitmapLock3.unlock();
//}
//else{
// xxx++;
//}
BitBlt(GetDC(gHwnd), 0, 0, gWidth, gHeight, mDCBitmap3, 0, 0, SRCCOPY);
}
mLastBlittedTimeStamp = Now();
sprintf_s(buf, "\mCurrentBitmap.... %d %lld\n", mCurrentBitmap, Now());
//OutputDebugStringA(buf);
::QueryPerformanceCounter(&gE);
gE.QuadPart -= gSS.QuadPart;
gE.QuadPart *= 1000000;
LARGE_INTEGER f;
::QueryPerformanceFrequency(&f);
gE.QuadPart /= f.QuadPart;
//char buf[200];
sprintf_s(buf, "\n*** %d", gE.QuadPart);
OutputDebugStringA(buf);
#endif
//mBitmapLock.unlock();
}
//mBlitHandled = true;
}
void VSyncWin::StaticTimer(LPVOID lpArg, // Data value
DWORD dwTimerLowValue, // Timer low value
DWORD dwTimerHighValue)
{
Self->TimerBlit();
//::PostMessageA(gHwnd, WM_BLIT_REQUEST, 0, 0);
::QueryPerformanceCounter(&gE);
gE.QuadPart -= gSS.QuadPart;