-
Notifications
You must be signed in to change notification settings - Fork 19
/
Game.cpp
4749 lines (4016 loc) · 154 KB
/
Game.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
//*********************************************************
//
// Copyright (c) Microsoft. All rights reserved.
// This code is licensed under the MIT License (MIT).
// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************
#include "pch.h"
#include "winioctl.h"
#include "ntddvdeo.h"
//#include "BasicMath.h"
#include "ColorSpaces.h"
#include "Game.h"
#include "BandedGradientEffect.h"
#include "SineSweepEffect.h"
#include "ToneSpikeEffect.h"
#include <winrt\Windows.Devices.Display.h>
#include <winrt\Windows.Devices.Enumeration.h>
#define BRIGHTNESS_SLIDER_FACTOR (m_rawOutDesc.MaxLuminance / m_outputDesc.MaxLuminance)
//using namespace concurrency;
using namespace winrt::Windows::Devices;
using namespace winrt::Windows::Devices::Display;
using namespace winrt::Windows::Devices::Enumeration;
void ACPipeline();
extern void ExitGame();
using namespace DirectX;
using Microsoft::WRL::ComPtr;
Game::Game(PWSTR appTitle)
{
m_appTitle = appTitle;
ConstructorInternal();
}
void Game::ConstructorInternal()
{
m_currentTest = TestPattern::StartOfTest;
m_currentColor = 0; // Which of R/G/B to test.
m_currentProfileTile = 0; // which intensity profile tile we are on
m_maxPQCode = 1;
m_maxProfileTile = 1;
m_gradientColor = D2D1::ColorF(0.25f, 0.25f, 0.25f);
m_gradientAnimationBase = 0.25f;
m_flashOn = false;
m_testingTier = DisplayHDR400;
m_outputDesc = { 0 };
m_rawOutDesc.MaxLuminance = 0.f;
m_rawOutDesc.MaxFullFrameLuminance = 0.f;
m_rawOutDesc.MinLuminance = 0.f;
m_totalTime = 0;
m_showExplanatoryText = true;
m_gamutVolume = 0.0f;
// These are sRGB code values for HDR10:
m_maxEffectivesRGBValue = -1; // not set yet
m_maxFullFramesRGBValue = -1;
m_minEffectivesRGBValue = -1;
// These are PQ code values for HDR10:
m_maxEffectivePQValue = -1; // not set yet
m_maxFullFramePQValue = -1;
m_minEffectivePQValue = -1;
// enable adjustment to tests 5.1 and 5.2 for active dimming
m_activeDimming50PQValue = 0.0f;
m_activeDimming05PQValue = 0.0f;
m_deviceResources = std::make_unique<DX::DeviceResources>();
m_testPatternResources[TestPattern::BitDepthPrecision] = TestPatternResources{ std::wstring(L"7. Bit-Depth/Precision") , std::wstring() , std::wstring(L"BandedGradientEffect.cso") , CLSID_CustomBandedGradientEffect };
m_testPatternResources[TestPattern::SharpeningFilter] = TestPatternResources{ std::wstring(L"Fresnel zone plate (sharpening test)") , std::wstring() , std::wstring(L"SineSweepEffect.cso") , CLSID_CustomSineSweepEffect };
m_testPatternResources[TestPattern::ToneMapSpike] = TestPatternResources{ std::wstring(L"ST.2084 Spike (Tone map test)") , std::wstring() , std::wstring(L"ToneSpikeEffect.cso") , CLSID_CustomToneSpikeEffect };
m_testPatternResources[TestPattern::OnePixelLinesBW] = TestPatternResources{ std::wstring(L"Single pixel lines (black/white)") , std::wstring(L"OnePixelLinesBW1200x700.png") , std::wstring() , {} };
m_testPatternResources[TestPattern::OnePixelLinesRG] = TestPatternResources{ std::wstring(L"Single pixel lines (red/green)") , std::wstring(L"OnePixelLinesRG1200x700.png") , std::wstring() , {} };
m_testPatternResources[TestPattern::TextQuality] = TestPatternResources{ std::wstring(L"Antialiased text (ClearType and grayscale)"), std::wstring(L"CalibriBoth96Dpi.png") , std::wstring() , {} };
m_hideTextString = std::wstring(L"Press SPACE to hide this text.");
m_deviceResources->RegisterDeviceNotify(this);
}
// Initialize the Direct3D resources required to run.
void Game::Initialize(HWND window, int width, int height)
{
m_deviceResources->SetWindow(window, width, height);
m_deviceResources->CreateDeviceResources();
m_deviceResources->SetDpi(96.0f); // TODO: using default 96 DPI for now
m_deviceResources->CreateWindowSizeDependentResources();
CreateDeviceIndependentResources();
CreateDeviceDependentResources();
CreateWindowSizeDependentResources();
m_timer.SetFixedTimeStep(true);
m_timer.SetTargetElapsedSeconds(1.0 / 60);
}
// Returns whether the reported display metadata consists of
// default values generated by Windows.
bool Game::CheckForDefaults()
{
return (
// Default SDR display values (RS2)
(270.0f == m_rawOutDesc.MaxLuminance &&
270.0f == m_rawOutDesc.MaxFullFrameLuminance &&
0.5f == m_rawOutDesc.MinLuminance) ||
// Default HDR display values (RS2)
(550.0f == m_rawOutDesc.MaxLuminance &&
450.0f == m_rawOutDesc.MaxFullFrameLuminance &&
0.5f == m_rawOutDesc.MinLuminance) ||
// Default HDR display values (RS3)
(1499.0f == m_rawOutDesc.MaxLuminance &&
799.0f == m_rawOutDesc.MaxFullFrameLuminance &&
0.01f == m_rawOutDesc.MinLuminance)
);
}
// Default to tier based on what the EDID specified
Game::TestingTier Game::GetTestingTier()
{
#define SAFETY (0.970)
if (m_rawOutDesc.MaxLuminance >= SAFETY * 10000.0f)
return TestingTier::DisplayHDR10000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 6000.0f)
return TestingTier::DisplayHDR6000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 4000.0f)
return TestingTier::DisplayHDR4000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 3000.0f)
return TestingTier::DisplayHDR3000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 2000.0f)
return TestingTier::DisplayHDR2000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 1400.0f)
return TestingTier::DisplayHDR1400;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 1000.0f)
return TestingTier::DisplayHDR1000;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 600.0f)
return TestingTier::DisplayHDR600;
else if (m_rawOutDesc.MaxLuminance >= SAFETY * 500.0f)
return TestingTier::DisplayHDR500;
else
return TestingTier::DisplayHDR400;
}
WCHAR *Game::GetTierName(Game::TestingTier tier)
{
if (tier == DisplayHDR400) return L"DisplayHDR400";
else if (tier == DisplayHDR500) return L"DisplayHDR500";
else if (tier == DisplayHDR600) return L"DisplayHDR600";
else if (tier == DisplayHDR1000) return L"DisplayHDR1000";
else if (tier == DisplayHDR1400) return L"DisplayHDR1400";
else if (tier == DisplayHDR2000) return L"DisplayHDR2000";
else if (tier == DisplayHDR3000) return L"DisplayHDR3000";
else if (tier == DisplayHDR4000) return L"DisplayHDR4000";
else if (tier == DisplayHDR6000) return L"DisplayHDR6000";
else if (tier == DisplayHDR10000) return L"DisplayHDR10000";
else return L"Unsupported DisplayHDR Tier";
}
float Game::GetTierLuminance(Game::TestingTier tier)
{
if (tier == DisplayHDR400) return 400.f;
else if (tier == DisplayHDR500) return 500.f;
else if (tier == DisplayHDR600) return 600.f;
else if (tier == DisplayHDR1000) return 1015.27f;
else if (tier == DisplayHDR1400) return 1400.f;
else if (tier == DisplayHDR2000) return 2000.f;
else if (tier == DisplayHDR3000) return 3000.f;
else if (tier == DisplayHDR4000) return 4000.f;
else if (tier == DisplayHDR6000) return 6000.f;
else if (tier == DisplayHDR10000) return 10000.f;
else return -1.0f;
}
void Game::InitEffectiveValues()
{
if (m_maxEffectivePQValue < 0.0)
{
float val = 1023.0f * Apply2084(m_rawOutDesc.MaxLuminance / 10000.f);
m_maxEffectivePQValue = val - 5.0f;
}
if (m_maxFullFramePQValue < 0.0)
{
float val = 1023.0f * Apply2084(m_rawOutDesc.MaxFullFrameLuminance / 10000.f);
m_maxFullFramePQValue = val - 5.0f;
}
if (m_minEffectivePQValue < 0.0)
{
float val = 1023.0f * Apply2084(m_rawOutDesc.MinLuminance / 10000.f);
m_minEffectivePQValue = val + 5.0f;
}
if (m_maxEffectivesRGBValue < 0.0)
{
float val = 255;
m_maxEffectivesRGBValue = val - 5.0f;
}
if (m_maxFullFramesRGBValue < 0.0)
{
float val = 255;
m_maxFullFramesRGBValue = val - 5.0f;
}
if (m_minEffectivesRGBValue < 0.0)
{
float val = 10;
m_minEffectivesRGBValue = val + 5.0f;
}
}
#pragma region Frame Update
// Executes the basic game loop.
void Game::Tick()
{
m_timer.Tick([&]()
{
Update(m_timer);
});
Render();
}
// Update any parameters used for animations.
void Game::Update(DX::StepTimer const& timer)
{
m_totalTime = float(timer.GetTotalSeconds());
D2D1_COLOR_F endColor = D2D1::ColorF(D2D1::ColorF::Black, 1);
switch (m_currentTest)
{
case TestPattern::PanelCharacteristics:
UpdateDxgiColorimetryInfo();
break;
case TestPattern::WarmUp:
if (m_newTestSelected)
{
m_testTimeRemainingSec = 60.0f*30.0f; // 30 minutes
}
else
{
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
}
break;
case TestPattern::Cooldown:
if (m_newTestSelected)
{
m_testTimeRemainingSec = 60.0f; // One minute
}
else
{
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
}
if (m_testTimeRemainingSec <= 0.0f)
SetTestPattern(m_cachedTest);
break;
case TestPattern::FlashTest:
case TestPattern::FlashTestMAX:
if (m_newTestSelected)
{
m_testTimeRemainingSec = 4.0f; // 4 seconds
m_flashOn = false;
}
else
{
// TODO make this show 10ths of a second.
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
if (m_testTimeRemainingSec <= 0.0001)
{
if (!m_flashOn)
{
m_flashOn = true;
m_testTimeRemainingSec = 2;
}
else if (m_flashOn)
{
m_flashOn = false;
m_testTimeRemainingSec = 10;
}
}
}
break;
case TestPattern::LongDurationWhite:
case TestPattern::FullFramePeak:
if (m_newTestSelected)
{
m_testTimeRemainingSec = 1800.0f; // 30 minutes
}
else
{
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
}
break;
case TestPattern::TenPercentPeak: // test 1.
case TestPattern::TenPercentPeakMAX: // test 1.MAX
if (m_newTestSelected)
{
m_testTimeRemainingSec = 1800.0f; // 30 minutes
}
else
{
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
}
break;
case TestPattern::ActiveDimming: break;
case TestPattern::ActiveDimmingDark: break;
case TestPattern::BitDepthPrecision:
// TODO: How exactly are we choosing this to correspond with expected banding?
// We don't know what internal EOTF is being used by the display, so how
// do we draw a ruler that matches with anything but sRGB?
endColor = D2D1::ColorF(0.25f, 0.25f, 0.25f);
break;
case TestPattern::RiseFallTime:
if (m_newTestSelected)
{
m_testTimeRemainingSec = 3.0f;
}
else
{
m_testTimeRemainingSec -= static_cast<float>(timer.GetElapsedSeconds());
m_testTimeRemainingSec = std::max(0.0f, m_testTimeRemainingSec);
if (m_testTimeRemainingSec <= 0.0001)
{
if (!m_flashOn)
{
m_flashOn = true;
m_testTimeRemainingSec = 5;
}
else if (m_flashOn)
{
m_flashOn = false;
m_testTimeRemainingSec = 5;
}
}
}
break;
case TestPattern::StaticGradient:
endColor = m_gradientColor;
break;
case TestPattern::AnimatedGrayGradient:
// Color channels vary between 0.0 and 2 * m_gradientEndPoint.
endColor = D2D1::ColorF(
m_gradientAnimationBase * sin(m_totalTime) + m_gradientAnimationBase,
m_gradientAnimationBase * sin(m_totalTime) + m_gradientAnimationBase,
m_gradientAnimationBase * sin(m_totalTime) + m_gradientAnimationBase);
break;
case TestPattern::AnimatedColorGradient:
// Color channels vary between 0.0 and 2 * m_gradientEndPoint.
endColor = D2D1::ColorF(
m_gradientAnimationBase * sin(m_totalTime * 2.0f) + m_gradientAnimationBase,
m_gradientAnimationBase * sin(m_totalTime * 1.0f) + m_gradientAnimationBase,
m_gradientAnimationBase * sin(m_totalTime * 0.5f) + m_gradientAnimationBase);
break;
default:
// Not all test patterns have animations, so not implemented is fine.
break;
}
// TODO: This should go into a shared method for the gradient test patterns.
D2D1_GRADIENT_STOP gradientStops[2];
gradientStops[0].color = D2D1::ColorF(D2D1::ColorF::Black, 1);
gradientStops[0].position = 0.0f;
gradientStops[1].color = endColor;
gradientStops[1].position = 1.0f;
auto ctx = m_deviceResources->GetD2DDeviceContext();
D2D1_BUFFER_PRECISION prec = D2D1_BUFFER_PRECISION_UNKNOWN;
switch (m_deviceResources->GetBackBufferFormat())
{
case DXGI_FORMAT_B8G8R8A8_UNORM:
prec = D2D1_BUFFER_PRECISION_8BPC_UNORM;
break;
case DXGI_FORMAT_R16G16B16A16_UNORM:
prec = D2D1_BUFFER_PRECISION_16BPC_UNORM;
break;
case DXGI_FORMAT_R16G16B16A16_FLOAT:
prec = D2D1_BUFFER_PRECISION_16BPC_FLOAT;
break;
default:
DX::ThrowIfFailed(E_INVALIDARG);
break;
}
ComPtr<ID2D1GradientStopCollection1> stopCollection;
DX::ThrowIfFailed(ctx->CreateGradientStopCollection(
gradientStops,
ARRAYSIZE(gradientStops),
D2D1_COLOR_SPACE_SRGB, // TODO: Why must I convert from sRGB to scRGB?
D2D1_COLOR_SPACE_SCRGB,
prec,
D2D1_EXTEND_MODE_CLAMP,
D2D1_COLOR_INTERPOLATION_MODE_PREMULTIPLIED, // No alpha, doesn't matter
&stopCollection));
auto rect = m_deviceResources->GetLogicalSize();
DX::ThrowIfFailed(ctx->CreateLinearGradientBrush(
D2D1::LinearGradientBrushProperties(
D2D1::Point2F(0, 0),
D2D1::Point2F(rect.right, 0)), // Assumes rect origin is 0,0?
stopCollection.Get(),
&m_gradientBrush));
if (m_dxgiColorInfoStale)
{
UpdateDxgiColorimetryInfo();
}
}
void Game::UpdateDxgiColorimetryInfo()
{
// Output information is cached on the DXGI Factory. If it is stale we need to create
// a new factory and re-enumerate the displays.
auto d3dDevice = m_deviceResources->GetD3DDevice();
ComPtr<IDXGIDevice3> dxgiDevice;
DX::ThrowIfFailed(d3dDevice->QueryInterface(IID_PPV_ARGS(&dxgiDevice)));
ComPtr<IDXGIAdapter> dxgiAdapter;
DX::ThrowIfFailed(dxgiDevice->GetAdapter(&dxgiAdapter));
ComPtr<IDXGIFactory4> dxgiFactory;
DX::ThrowIfFailed(dxgiAdapter->GetParent(IID_PPV_ARGS(&dxgiFactory)));
// if (!dxgiFactory->IsCurrent())
{
DX::ThrowIfFailed(CreateDXGIFactory1(IID_PPV_ARGS(&dxgiFactory)));
}
// Get information about the display we are presenting to.
ComPtr<IDXGIOutput> output;
auto sc = m_deviceResources->GetSwapChain();
DX::ThrowIfFailed(sc->GetContainingOutput(&output));
ComPtr<IDXGIOutput6> output6;
output.As(&output6);
DX::ThrowIfFailed(output6->GetDesc1(&m_outputDesc));
// Get raw (not OS-modified) luminance data:
DISPLAY_DEVICE device = {};
device.cb = sizeof(device);
DisplayMonitor foundMonitor{ nullptr };
for (UINT deviceIndex = 0; EnumDisplayDevices(m_outputDesc.DeviceName, deviceIndex, &device, EDD_GET_DEVICE_INTERFACE_NAME); deviceIndex++)
{
if (device.StateFlags & DISPLAY_DEVICE_ACTIVE)
{
foundMonitor = DisplayMonitor::FromInterfaceIdAsync(device.DeviceID).get();
if (foundMonitor)
{
break;
}
}
}
if (!foundMonitor)
{
}
// save the raw (not OS-modified) luminance data:
m_rawOutDesc.MaxLuminance = foundMonitor.MaxLuminanceInNits();
m_rawOutDesc.MaxFullFrameLuminance = foundMonitor.MaxAverageFullFrameLuminanceInNits();
m_rawOutDesc.MinLuminance = foundMonitor.MinLuminanceInNits();
// TODO: Should also get color primaries...
// get PQ code at MaxLuminance
m_maxPQCode = (int) roundf(1023.0f*Apply2084(m_rawOutDesc.MaxLuminance / 10000.f));
m_dxgiColorInfoStale = false;
// ACPipeline();
}
// reset metadata to default state (same as panel properties) so it need do no tone mapping
// Note: OS does this on boot and on app exit.
void Game::SetMetadataNeutral()
{
m_Metadata.MaxContentLightLevel = static_cast<UINT16>(m_rawOutDesc.MaxLuminance);
m_Metadata.MaxFrameAverageLightLevel = static_cast<UINT16>(m_rawOutDesc.MaxFullFrameLuminance);
m_Metadata.MaxMasteringLuminance = static_cast<UINT>(m_rawOutDesc.MaxLuminance);
m_Metadata.MinMasteringLuminance = static_cast<UINT>(m_rawOutDesc.MinLuminance * 10000.0f);
m_MetadataGamut = ColorGamut::GAMUT_Native;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(m_outputDesc.RedPrimary[0] * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(m_outputDesc.RedPrimary[1] * 50000.0f);
m_Metadata.GreenPrimary[0]= static_cast<UINT16>(m_outputDesc.GreenPrimary[0] * 50000.0f);
m_Metadata.GreenPrimary[1]= static_cast<UINT16>(m_outputDesc.GreenPrimary[1] * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(m_outputDesc.BluePrimary[0] * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(m_outputDesc.BluePrimary[1] * 50000.0f);
m_Metadata.WhitePoint[0] = static_cast<UINT16>(m_outputDesc.WhitePoint[0] * 50000.0f);
m_Metadata.WhitePoint[1] = static_cast<UINT16>(m_outputDesc.WhitePoint[1] * 50000.0f);
auto sc = m_deviceResources->GetSwapChain();
DX::ThrowIfFailed(sc->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(DXGI_HDR_METADATA_HDR10), &m_Metadata));
}
void Game::SetMetadata(float max, float avg, ColorGamut gamut)
{
m_Metadata.MaxContentLightLevel = static_cast<UINT16>(max);
m_Metadata.MaxFrameAverageLightLevel = static_cast<UINT16>(avg);
m_Metadata.MaxMasteringLuminance = static_cast<UINT>(max);
m_Metadata.MinMasteringLuminance = static_cast<UINT>(0.0 * 10000.0f);
switch (gamut)
{
case GAMUT_Native:
m_MetadataGamut = ColorGamut::GAMUT_Native;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(m_outputDesc.RedPrimary[0] * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(m_outputDesc.RedPrimary[1] * 50000.0f);
m_Metadata.GreenPrimary[0] = static_cast<UINT16>(m_outputDesc.GreenPrimary[0] * 50000.0f);
m_Metadata.GreenPrimary[1] = static_cast<UINT16>(m_outputDesc.GreenPrimary[1] * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(m_outputDesc.BluePrimary[0] * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(m_outputDesc.BluePrimary[1] * 50000.0f);
break;
case GAMUT_sRGB:
m_MetadataGamut = ColorGamut::GAMUT_sRGB;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(primaryR_709.x * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(primaryR_709.y * 50000.0f);
m_Metadata.GreenPrimary[0] = static_cast<UINT16>(primaryG_709.x * 50000.0f);
m_Metadata.GreenPrimary[1] = static_cast<UINT16>(primaryG_709.y * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(primaryB_709.x * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(primaryB_709.y * 50000.0f);
break;
case GAMUT_Adobe:
m_MetadataGamut = ColorGamut::GAMUT_Adobe;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(primaryR_Adobe.x * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(primaryR_Adobe.y * 50000.0f);
m_Metadata.GreenPrimary[0] = static_cast<UINT16>(primaryG_Adobe.x * 50000.0f);
m_Metadata.GreenPrimary[1] = static_cast<UINT16>(primaryG_Adobe.y * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(primaryB_Adobe.x * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(primaryB_Adobe.y * 50000.0f);
break;
case GAMUT_DCIP3:
m_MetadataGamut = ColorGamut::GAMUT_DCIP3;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(primaryR_DCIP3.x * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(primaryR_DCIP3.y * 50000.0f);
m_Metadata.GreenPrimary[0] = static_cast<UINT16>(primaryG_DCIP3.x * 50000.0f);
m_Metadata.GreenPrimary[1] = static_cast<UINT16>(primaryG_DCIP3.y * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(primaryB_DCIP3.x * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(primaryB_DCIP3.y * 50000.0f);
break;
case GAMUT_BT2100:
m_MetadataGamut = ColorGamut::GAMUT_BT2100;
m_Metadata.RedPrimary[0] = static_cast<UINT16>(primaryR_2020.x * 50000.0f);
m_Metadata.RedPrimary[1] = static_cast<UINT16>(primaryR_2020.y * 50000.0f);
m_Metadata.GreenPrimary[0] = static_cast<UINT16>(primaryG_2020.x * 50000.0f);
m_Metadata.GreenPrimary[1] = static_cast<UINT16>(primaryG_2020.y * 50000.0f);
m_Metadata.BluePrimary[0] = static_cast<UINT16>(primaryB_2020.x * 50000.0f);
m_Metadata.BluePrimary[1] = static_cast<UINT16>(primaryB_2020.y * 50000.0f);
break;
}
m_Metadata.WhitePoint[0] = static_cast<UINT16>(D6500White.x * 50000.0f);
m_Metadata.WhitePoint[1] = static_cast<UINT16>(D6500White.y * 50000.0f);
auto sc = m_deviceResources->GetSwapChain();
DX::ThrowIfFailed(sc->SetHDRMetaData(DXGI_HDR_METADATA_TYPE_HDR10, sizeof(DXGI_HDR_METADATA_HDR10), &m_Metadata));
}
// dump out the metadata to a string for display
void Game::PrintMetadata( ID2D1DeviceContext2* ctx, bool blackText /* = false */ )
{
std::wstringstream text;
// print luminance levels
text << "MaxCLL: ";
text << std::to_wstring((int)m_Metadata.MaxContentLightLevel);
text << " MaxFALL: ";
text << std::to_wstring((int)m_Metadata.MaxFrameAverageLightLevel);
text << " MinCLL: ";
text << std::to_wstring(m_Metadata.MinMasteringLuminance / 10000.0f);
// print gamut in use
text << " Gamut: ";
switch ( m_MetadataGamut )
{
case GAMUT_Native:
text << "Native\n";
break;
case GAMUT_sRGB:
text << "sRGB\n";
break;
case GAMUT_Adobe:
text << "Adobe\n";
break;
case GAMUT_DCIP3:
text << "DCI-P3\n";
break;
case GAMUT_BT2100:
text << "bt.2100\n";
break;
}
RenderText(ctx, m_largeFormat.Get(), text.str(), m_MetadataTextRect, blackText);
}
void Game::GenerateTestPattern_StartOfTest(ID2D1DeviceContext2* ctx)
{
auto fmt = m_deviceResources->GetBackBufferFormat();
std::wstringstream text;
if (m_newTestSelected) SetMetadataNeutral();
text << m_appTitle;
text << L"\n\nVersion 1.10\n\n";
//text << L"ALT-ENTER: Toggle fullscreen: all measurements should be made in fullscreen\n";
text << L"->, PAGE DN: Move to next test\n";
text << L"<-, PAGE UP: Move to previous test\n";
text << L"NUMBER KEY: Jump to test number\n";
text << L"SPACE: Hide text and target circle\n";
text << L"C: Start 60s cool-down\n";
text << L"ALT-ENTER: Toggle fullscreen\n";
text << L"ESCAPE: Exit fullscreen\n";
text << L"ALT-F4: Exit app\n";
text << L"\nCopyright © VESA\nLast updated: " << __DATE__;
RenderText(ctx, m_largeFormat.Get(), text.str(), m_largeTextRect);
if (m_showExplanatoryText)
{
std::wstringstream title;
title << L"Home. Start Screen\n" << m_hideTextString;
RenderText(ctx, m_largeFormat.Get(), title.str(), m_testTitleRect);
}
m_newTestSelected = false;
}
bool Game::CheckHDR_On()
{
// TODO this should query an HDR bit if one is available
bool HDR_On = false;
switch (m_outputDesc.ColorSpace)
{
case DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709: // sRGB
break;
case DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020: // HDR10 PC
HDR_On = true;
break;
default:
break;
}
return HDR_On;
}
void Game::GenerateTestPattern_ConnectionProperties(ID2D1DeviceContext2* ctx)
{
if (m_newTestSelected) SetMetadataNeutral();
std::wstringstream text;
text << L"\n\nConnection to: ";
WCHAR *DisplayName = wcsrchr(m_outputDesc.DeviceName, '\\');
text << ++DisplayName;
text << L"\n\nConnection bit depth: ";
text << std::to_wstring(m_outputDesc.BitsPerColor);
text << L"\n\nConnection colorspace: [";
text << std::to_wstring(m_outputDesc.ColorSpace);
switch (m_outputDesc.ColorSpace)
{
case DXGI_COLOR_SPACE_RGB_FULL_G22_NONE_P709: // sRGB
text << L"] sRGB/SDR";
break;
case DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020: // HDR10 PC
text << L"] HDR/advanced color on";
break;
default:
text << L"] Unknown";
break;
}
bool HDR_On = CheckHDR_On();
if (!HDR_On)
{
text << L"\n\nBefore starting tests, make sure \"HDR and Advanced color\"";
text << L"\n is enabled in the Display settings panel.";
}
if ( HDR_On )
{
text << L"\n\nSupported PQ Values - ";
text << L"\nMax Effective Value: ";
text << std::to_wstring((int)m_maxEffectivePQValue);
text << L" (";
text << std::to_wstring(Remove2084(m_maxEffectivePQValue/1023.0f)*10000.0f);
text << L" nits)";
text << L"\nMax FullFrame Value: ";
text << std::to_wstring((int)m_maxFullFramePQValue);
text << L" (";
text << std::to_wstring(Remove2084(m_maxFullFramePQValue / 1023.0f)*10000.0f);
text << L" nits)";
text << L"\nMin Effective Value: ";
text << std::to_wstring((int)m_minEffectivePQValue);
text << L" ( ";
text << std::to_wstring(Remove2084(m_minEffectivePQValue/1023.0f)*10000.0f);
text << L" nits)";
}
else
{
text << L"\n\nSupported sRGB Values - ";
text << L"\nMax Effective Value: ";
text << std::to_wstring((int)m_maxEffectivesRGBValue);
text << L" (";
text << std::to_wstring(RemoveSRGBCurve(m_maxEffectivesRGBValue/255.0f)*80.0f);
text << L" nits)";
text << L"\nMax FullFrame Value: ";
text << std::to_wstring((int)m_maxFullFramesRGBValue);
text << L" (";
text << std::to_wstring(RemoveSRGBCurve(m_maxFullFramesRGBValue/255.0f)*80.0f);
text << L" nits)";
text << L"\nMin Effective Value: ";
text << std::to_wstring((int)m_minEffectivesRGBValue);
text << L" ( ";
text << std::to_wstring(RemoveSRGBCurve(m_minEffectivesRGBValue/255.0f)*80.0f);
text << L" nits)";
}
RenderText(ctx, m_monospaceFormat.Get(), text.str(), m_largeTextRect);
if (m_showExplanatoryText)
{
std::wstring title = L"Connection properties:\nPress SPACE to hide this text\n";
RenderText(ctx, m_largeFormat.Get(), title, m_testTitleRect);
}
m_newTestSelected = false;
}
void Game::GenerateTestPattern_PanelCharacteristics(ID2D1DeviceContext2* ctx)
{
if (m_newTestSelected) SetMetadataNeutral();
std::wstringstream text;
text << fixed << setw(9) << setprecision(2);
// TODO: ensure these values are up to date.
if (CheckForDefaults())
{
text << L"***WARNING: These are OS-provided defaults. Display did not provide any data.***\n";
}
// else
{
text << L"\nHDR Testing Tier: " << GetTierName(m_testingTier);
text << L" - Change using Up/Down Arrow Keys" << L"\n";
}
// text << m_outputDesc.DeviceName;
text << L"\nBase Levels:";
text << L"\n Max peak luminance: ";
text << m_rawOutDesc.MaxLuminance;
text << L"\n Max frame-average luminance: ";
text << m_rawOutDesc.MaxFullFrameLuminance;
text << L"\n Min luminance: ";
text << setprecision(5);
text << m_rawOutDesc.MinLuminance;
text << L"\nAdjusted Levels:";
text << L"\n Max peak luminance: ";
text << setprecision(2);
text << m_outputDesc.MaxLuminance;
text << L"\n Max frame-average luminance: ";
text << m_outputDesc.MaxFullFrameLuminance;
text << L"\n Min luminance: ";
text << setprecision(5);
text << m_outputDesc.MinLuminance;
text << L"\nCurr. Brightness Slider Factor: ";
text << BRIGHTNESS_SLIDER_FACTOR;
// text << L"\nContrast ratio (peak/min): ";
// text << std::to_wstring(m_outputDesc.MaxLuminance / m_outputDesc.MinLuminance );
text << L"\n";
// Compute area of this device's gamut in uv coordinates
// first, get primaries of display in 1931 xy coordinates
float2 red_xy, grn_xy, blu_xy, wht_xy;
red_xy = m_outputDesc.RedPrimary;
grn_xy = m_outputDesc.GreenPrimary;
blu_xy = m_outputDesc.BluePrimary;
wht_xy = m_outputDesc.WhitePoint;
text << L"\nCIE 1931 x y";
text << L"\nRed Primary : " << std::to_wstring(red_xy.x) << L" " << std::to_wstring(red_xy.y);
text << L"\nGreen Primary: " << std::to_wstring(grn_xy.x) << L" " << std::to_wstring(grn_xy.y);
text << L"\nBlue Primary : " << std::to_wstring(blu_xy.x) << L" " << std::to_wstring(blu_xy.y);
text << L"\nWhite Point : " << std::to_wstring(wht_xy.x) << L" " << std::to_wstring(wht_xy.y);
float gamutAreaDevice = ComputeGamutArea(red_xy, grn_xy, blu_xy);
// rec.709/sRGB gamut area in uv coordinates
float gamutAreasRGB = ComputeGamutArea(primaryR_709, primaryG_709, primaryB_709);
// AdobeRGB gamut area in uv coordinates
float GamutAreaAdobe = ComputeGamutArea(primaryR_Adobe, primaryG_Adobe, primaryB_Adobe);
// DCI-P3 gamut area in uv coordinates
float gamutAreaDCIP3 = ComputeGamutArea(primaryR_DCIP3, primaryG_DCIP3, primaryB_DCIP3);
// BT.2020 gamut area in uv coordinates
float gamutAreaBT2100 = ComputeGamutArea(primaryR_2020, primaryG_2020, primaryB_2020);
// ACES gamut area in uv coordinates
float gamutAreaACES = ComputeGamutArea(primaryR_ACES, primaryG_ACES, primaryB_ACES);
const float gamutAreaHuman = 0.195f; // TODO get this actual data!
// Compute extent that this device gamut covers known popular ones:
float coverageSRGB = ComputeGamutCoverage(red_xy, grn_xy, blu_xy, primaryR_709, primaryG_709, primaryB_709);
float coverageAdobe = ComputeGamutCoverage(red_xy, grn_xy, blu_xy, primaryR_Adobe, primaryG_Adobe, primaryB_Adobe);
float coverageDCIP3 = ComputeGamutCoverage(red_xy, grn_xy, blu_xy, primaryR_DCIP3, primaryG_DCIP3, primaryB_DCIP3);
float coverage2100 = ComputeGamutCoverage(red_xy, grn_xy, blu_xy, primaryR_2020, primaryG_2020, primaryB_2020);
float coverageACES = ComputeGamutCoverage(red_xy, grn_xy, blu_xy, primaryR_ACES, primaryG_ACES, primaryB_ACES);
// display coverage values
text << L"\n\nGamut Coverage based on reported primaries";
text << L"\n % sRGB : " << std::to_wstring(coverageSRGB*100.f);
text << L"\n % AdobeRGB : " << std::to_wstring(coverageAdobe*100.f);
text << L"\n % DCI-P3 : " << std::to_wstring(coverageDCIP3*100.f);
text << L"\n % BT.2100 : " << std::to_wstring(coverage2100*100.f);
text << L"\n % Human eye : " << std::to_wstring(gamutAreaDevice / gamutAreaHuman*100.f);
// test code:
float theory = gamutAreaDevice / gamutAreaBT2100;
// text << L"\n % Theory : " << std::to_wstring( theory*100.f);
// text << L"\n % Error : " << std::to_wstring((coverage2100 - theory) / theory * 100.f);
RenderText(ctx, m_monospaceFormat.Get(), text.str(), m_largeTextRect);
if (m_showExplanatoryText)
{
std::wstring title = L"Reported Panel Characteristics\n" + m_hideTextString;
RenderText(ctx, m_largeFormat.Get(), title, m_testTitleRect);
PrintMetadata(ctx);
}
m_newTestSelected = false;
// TODO should bail here if no valid data
}
void setBrightnessSliderPercent(UCHAR percent)
{
HANDLE display = CreateFile(
L"\\\\.\\LCD",
(GENERIC_READ | GENERIC_WRITE),
NULL,
NULL,
OPEN_EXISTING,
0,
NULL);
if (display == INVALID_HANDLE_VALUE)
{
throw new std::runtime_error("Failed to open handle to display for setting brightness");
}
else
{
DWORD ret;
DISPLAY_BRIGHTNESS displayBrightness{};
displayBrightness.ucACBrightness = percent;
displayBrightness.ucDCBrightness = percent;
displayBrightness.ucDisplayPolicy = DISPLAYPOLICY_BOTH;
bool result = !DeviceIoControl(
display,
IOCTL_VIDEO_SET_DISPLAY_BRIGHTNESS,
&displayBrightness,
sizeof(displayBrightness),
NULL,
0,
&ret,
NULL);
if (result)
{
throw new std::runtime_error("Failed to set brightness");
}
}
}
void Game::GenerateTestPattern_ResetInstructions(ID2D1DeviceContext2* ctx)
{
if (m_newTestSelected)
{
SetMetadataNeutral();
// This works but we don't know what to set it to.
// So it is commented out until we do
// setBrightnessSliderPercent(33);
// EDID settings don't update afterwards anyway until next app run
}
if (m_showExplanatoryText)
{
std::wstring title = L"Start of performance tests\n" + m_hideTextString;
RenderText(ctx, m_largeFormat.Get(), title, m_testTitleRect);
}
std::wstringstream text;
text << L"For external displays, use their OSD to reset\n";
text << L"all settings to the default or factory state.\n\n";
text << L"For internal panels, set the OS \'Brightness\' slider to default.\n";
text << L"Set the \'SDR color appearance\' slider to the leftmost point.\n";
text << L"DO NOT CHANGE BRIGHTNESS SLIDERS AFTER APP START.\n\n";
text << L"Be sure the \'Night light' setting is off.\n";
text << L"Disable any Ambient Light Sensor capability.\n\n";
text << L"The display should be at normal operating temperature.\n";
text << L" This can take as much as 30 minutes for some panels.\n";
text << L" Also note, ambient temperature may affect scores.\n\n";
text << L"Use the following screen to check correct brightness levels";
RenderText(ctx, m_largeFormat.Get(), text.str(), m_largeTextRect);
m_newTestSelected = false;
}
void Game::DrawLogo(ID2D1DeviceContext2 *ctx, float c)
{
ComPtr<ID2D1SolidColorBrush> centerBrush;
DX::ThrowIfFailed(ctx->CreateSolidColorBrush(D2D1::ColorF(c, c, c), ¢erBrush));
auto logSize = m_deviceResources->GetLogicalSize();
float size = sqrt((logSize.right - logSize.left) * (logSize.bottom - logSize.top));
size = size * sqrtf(0.10); // dimension of a square of 10% screen area
size = size * 0.3333333f;
float2 center;
center.x = (logSize.right - logSize.left)*0.50f;
center.y = (logSize.bottom - logSize.top)*0.50f;
D2D1_RECT_F centerSquare1 =
{
center.x - size * 1.03f,
center.y - size * 1.03f,
center.x - size * 0.03f,
center.y - size * 0.03f
};
ctx->FillRectangle(¢erSquare1, centerBrush.Get());
D2D1_RECT_F centerSquare2 =