Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/NightVisionDisplay11Tests.h

Source at documentation-generation time. See Test catalog for fixture associations.

1#pragma once
2#include "NativeUiTest11.h"
3#include "../Viewscreen/CockpitLighting.h"
4
5inline unsigned RunNightVisionDisplay11Tests(NativeUiTest11& fixture)
6{
7 using namespace Rendering;
8 using Microsoft::WRL::ComPtr;
9 unsigned checks=0;
10 const auto check=[&](bool ok,const char* message) {++checks;if(!ok)throw std::runtime_error(message);};
11 fixture.Resize(128,64);
12 auto& device=fixture.device;auto& draws=device.Draws();
13 struct Vertex {float x,y,z,nx,ny,nz,u,v;};
14 const Vertex vertices[]={{-1,1,.5f,0,0,1,0,0},{1,1,.5f,0,0,1,.5f,0},{-1,-1,.5f,0,0,1,0,1},
15 {-1,-1,.5f,0,0,1,0,1},{1,1,.5f,0,0,1,.5f,0},{1,-1,.5f,0,0,1,.5f,1}};
16 Geometry11 geometry;NativeUiTest11::HR(geometry.Create(device.Native(),sizeof(vertices),false));
17 void* memory=nullptr;NativeUiTest11::HR(geometry.Lock(0,sizeof(vertices),&memory));
18 memcpy(memory,vertices,sizeof(vertices));NativeUiTest11::HR(geometry.Unlock());
19 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.normal=12;layout.uv[0]=24;
20 const auto rgba=[](unsigned r,unsigned g,unsigned b){return 0xff000000u|r|(g<<8)|(b<<16);};
21 std::vector<DWORD> atlas(256*64,rgba(0,0,0)),interlace(2*64);
22 for(unsigned y=0;y<64;++y) {
23 for(unsigned x=128;x<256;++x)atlas[y*256+x]=rgba(255,255,255); // Adjacent gauge must never leak.
24 for(unsigned x=0;x<2;++x)interlace[y*2+x]=y%2 ? rgba(224,224,224) : rgba(255,255,255);
25 }
26 const auto rect=[&](unsigned x0,unsigned y0,unsigned x1,unsigned y1,DWORD color) {
27 for(unsigned y=y0;y<y1;++y)for(unsigned x=x0;x<x1;++x)atlas[y*256+x]=color;
28 };
29 // Thin white symbol strokes, saturated warning colors and a dim panel patch.
30 rect(32,20,34,44,rgba(255,255,255));rect(44,20,46,44,rgba(255,255,255));
31 for(unsigned y:{20u,31u,42u})rect(32,y,46,y+2,rgba(255,255,255));
32 rect(62,24,72,40,rgba(255,0,0));rect(82,24,92,40,rgba(0,255,0));
33 rect(102,24,112,40,rgba(0,0,255));rect(8,48,20,56,rgba(64,64,64));
34 rect(32,48,46,56,rgba(0,200,200)); // Typical cyan text is below full white.
35 // One-pixel cyan numerals, closer to a small waypoint/range readout.
36 const unsigned digits[4][7]={{14,17,19,21,25,17,14},{4,12,4,4,4,4,14},
37 {30,1,1,14,1,1,30},{14,17,17,15,1,1,14}}; // 0, 1, 3, 9
38 for(unsigned digit=0;digit<4;++digit)for(unsigned y=0;y<7;++y)for(unsigned x=0;x<5;++x)
39 if(digits[digit][y]&(1u<<(4-x)))rect(8+digit*7+x,4+y,9+digit*7+x,5+y,rgba(0,200,200));
40 const auto texture=[&](UINT width,UINT height,const std::vector<DWORD>& data) {
41 D3D11_TEXTURE2D_DESC desc{};desc.Width=width;desc.Height=height;desc.MipLevels=desc.ArraySize=desc.SampleDesc.Count=1;
42 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
43 D3D11_SUBRESOURCE_DATA initial{data.data(),width*4,width*height*4};
44 ComPtr<ID3D11Texture2D> image;ComPtr<ID3D11ShaderResourceView> view;
45 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,&initial,&image));
46 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(image.Get(),nullptr,&view));return view;
47 };
48 const auto screen=texture(256,64,atlas),lines=texture(2,64,interlace);
49 UINT viewWidth=128,viewHeight=64;
50 const auto readPixels=[&]() {
51 D3D11_TEXTURE2D_DESC desc{};fixture.host.Device().SceneColor()->GetDesc(&desc);
52 const UINT samples=desc.SampleDesc.Count;
53 desc.SampleDesc={1,0};desc.BindFlags=desc.MiscFlags=0;
54 ComPtr<ID3D11Texture2D> resolved,staging;
55 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,nullptr,&resolved));
56 if(samples>1)device.Context()->ResolveSubresource(resolved.Get(),0,fixture.host.Device().SceneColor(),0,desc.Format);
57 else device.Context()->CopyResource(resolved.Get(),fixture.host.Device().SceneColor());
58 desc.Usage=D3D11_USAGE_STAGING;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
59 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,nullptr,&staging));
60 device.Context()->CopyResource(staging.Get(),resolved.Get());
61 D3D11_MAPPED_SUBRESOURCE map{};NativeUiTest11::HR(device.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&map));
62 std::vector<DWORD> result(size_t(desc.Width)*desc.Height);
63 for(UINT y=0;y<desc.Height;++y)memcpy(result.data()+size_t(y)*desc.Width,
64 static_cast<const BYTE*>(map.pData)+size_t(y)*map.RowPitch,desc.Width*4);
65 device.Context()->Unmap(staging.Get(),0);
66 for(auto& p:result)p=(p&0xff00ff00)|((p&255)<<16)|((p>>16)&255);
67 return result;
68 };
69 const auto render=[&](float strength,bool blank=false,bool useScreen=true) {
70 draws.BindScene();draws.Material(-1);draws.Fixed()=FixedState11{};
71 auto& state=draws.Fixed();state.legacyPixelCenters=false;state.lighting=false;
72 // Keep the glyphs at the same pixel size while changing the view height.
73 state.world._11=128.f/viewWidth;state.world._22=64.f/viewHeight;
74 state.world._41=128.f/viewWidth-1;state.world._42=1-64.f/viewHeight;
75 state.nightVisionDisplay=strength;state.displayUvBounds={0,0,.5f,1};
76 state.stages[0].color=TextureOp11::Select1;state.stages[0].color1=TextureArg11::Diffuse;
77 state.stages[0].alpha=TextureOp11::Select1;state.stages[0].alpha1=TextureArg11::Diffuse;
78 state.stages[1].color=TextureOp11::Select1;
79 state.stages[1].color1=blank ? TextureArg11::Specular : TextureArg11::Texture;
80 state.stages[1].texture=useScreen ? screen : nullptr;
81 state.stages[1].alpha=TextureOp11::Select1;state.stages[1].alpha1=TextureArg11::Texture;
82 state.stages[2].color=TextureOp11::Modulate;
83 state.stages[2].color1=TextureArg11::Texture;state.stages[2].color2=TextureArg11::Current;state.stages[2].texture=lines;
84 state.stages[1].sampler.Filter=state.stages[2].sampler.Filter=D3D11_FILTER_MIN_MAG_MIP_LINEAR;
85 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;draws.Pipeline().depth.DepthEnable=FALSE;
86 draws.Pipeline().depth.StencilEnable=FALSE;draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
87 device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
88 NativeUiTest11::HR(draws.Draw(geometry,layout,Primitive11::Triangles,0,2));return readPixels();
89 };
90 const auto off=render(0);fixture.Save(L"Output/nvg-display-fixture-off.png");
91 const auto strength=CockpitLighting::DisplayStrength(NightVisionGain::Calculate(true,false,.0025f));
92 const auto on=render(strength);fixture.Save(L"Output/nvg-display-fixture-on.png");
93 const auto at=[](const std::vector<DWORD>& pixels,unsigned x,unsigned y){return pixels[y*128+x]&0xffffff;};
94 check((at(on,32,24)&255)>=160 && (at(on,32,24)&255)<245 && at(off,32,24)==0xffffff,"bright symbol edge softens but retains a legible core");
95 check(at(off,31,24)==0 && (at(on,31,24)&255)>=40 && (at(on,31,24)&255)<128,"thin bright strokes acquire a visible local halo");
96 check(at(on,38,26)==0,"symbol counters stay dark and distinguishable");
97 check((at(on,10,4)&255)>=96 && (at(on,10,4)&255)<(at(off,10,4)&255),"one-pixel cyan numerals soften without disappearing");
98 unsigned long long offSignal=0,onSignal=0;
99 for(size_t i=0;i<on.size();++i)for(unsigned shift:{0u,8u,16u}) {
100 offSignal+=(off[i]>>shift)&255;onSignal+=(on[i]>>shift)&255;
101 }
102 check(onSignal>offSignal,"softening redistributes highlights while glare preserves overall brightness enhancement");
103 check(at(on,28,24)==0,"bloom does not wash out distant black background");
104 check(at(on,12,50)==at(off,12,50),"dim nonhighlight display regions retain their brightness");
105 const auto red=at(on,66,28),green=at(on,86,28),blue=at(on,106,28);
106 check((red>>16)>=224 && (red&255)>=48 && (red&255)<112,"red warning remains recognizably red despite highlight washout");
107 check(((green>>8)&255)>=224 && (green&255)>=48 && (green&255)<112,"green highlight retains its color distinction");
108 check((blue&255)>=224 && (blue>>16)>=48 && (blue>>16)<112,"blue highlight retains its color distinction");
109 check((at(on,38,50)>>16)>=40 && (at(on,38,50)>>16)<100,"ordinary cyan highlights have noticeable bounded washout");
110 check(at(on,33,25)<at(on,33,24),"existing interlace modulation remains visible");
111 for(unsigned y=0;y<64;++y) {
112 check(at(on,127,y)==0,"bright adjacent atlas gauge cannot bleed into this monitor");
113 for(unsigned x=0;x<128;++x)check((on[y*128+x]>>24)==(off[y*128+x]>>24),"display optics preserve alpha");
114 }
115 check(render(strength,true)==render(0,true),"low-power blanking cannot resurrect display content through bloom");
116 check(render(strength,false,false)==render(0,false,false),"missing display texture cannot create phantom highlights");
117 for(const auto reset:{NightVisionGain::Calculate(false,false,.0025f),
118 NightVisionGain::Calculate(true,true,.0025f),NightVisionGain::Calculate(true,false,1)})
119 check(render(CockpitLighting::DisplayStrength(reset))==off,"equipment/camera/daylight/simulator reset returns exact original display");
120 check(render(strength)==on,"re-entering NVG produces the same frame without history or delayed data");
121 check(render(0)==off,"following unassisted draw is unchanged");
122 for(const auto dimensions:{std::pair<UINT,UINT>{1280,720},{1920,1080},{3440,1331}}) {
123 viewWidth=dimensions.first;viewHeight=dimensions.second;fixture.Resize(viewWidth,viewHeight);
124 for(UINT samples:{1u,4u}) {
125 NativeUiTest11::HR(fixture.host.SetSamples(samples));device.Reset();
126 const auto plain=render(0),enhanced=render(strength);
127 const auto pixel=[&](const std::vector<DWORD>& pixels,UINT x,UINT y){return pixels[size_t(y)*viewWidth+x]&0xffffff;};
128 check((pixel(enhanced,32,24)&255)>=145 && (pixel(enhanced,32,24)&255)<245,"resolution/MSAA preserve legibility while softening white glyphs");
129 check((pixel(enhanced,38,50)>>16)>=32,"cyan washout survives native resolution and MSAA");
130 check((pixel(enhanced,31,24)&255)>=32,"visible edge spread survives native resolution and MSAA");
131 check((pixel(enhanced,10,4)&255)>=90,"one-pixel range text remains legible at native resolution and MSAA");
132 check(pixel(enhanced,26,24)==0,"scaled halo remains local and leaves distant black untouched");
133 check(pixel(enhanced,127,24)==0,"scaled halo cannot cross the atlas boundary");
134 if(viewHeight>1000)check(pixel(enhanced,30,24)>pixel(plain,30,24),"high-resolution bloom extends beyond one pixel");
135 check(render(0)==plain,"high-resolution on/off round trip has no persistence");
136 if(viewWidth==3440 && samples==4) {
137 render(strength);RECT crop{0,0,128,64};fixture.Save(L"Output/nvg-display-ultrawide-on.png",&crop);
138 render(0);fixture.Save(L"Output/nvg-display-ultrawide-off.png",&crop);
139 }
140 }
141 }
142 return checks;
143}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
HRESULT Create(ID3D11Device *device, UINT bytes, bool dynamic, bool index=false, bool indices32=false)
HRESULT Lock(UINT offset, UINT size, void **destination, bool readOnly=false, bool discard=false)
float DisplayStrength(const NightVisionGain::Response &vision)
Response Calculate(bool active, bool simulator, float luminance)