Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/NightVisionCockpit11Tests.cpp

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

1#include "NativeUiTest11.h"
2#include "../Viewscreen/CockpitLighting.h"
3#include "NightVisionDisplay11Tests.h"
4#include "NightVisionStars11Tests.h"
5#include <iostream>
6
7using namespace Rendering;
8using Microsoft::WRL::ComPtr;
9namespace {
10unsigned checks=0;
11void Check(bool ok,const char* message) { ++checks; if(!ok)throw std::runtime_error(message); }
12float Energy(const std::vector<DWORD>& pixels) {
13 double sum=0; for(auto p:pixels)sum+=((p>>16)&255)+((p>>8)&255)+(p&255);
14 return float(sum/(pixels.size()*3));
15}
16}
17
18int main() try
19{
20 NativeUiTest11 fixture(64,64,false,false);
21 auto& device=fixture.device;auto& draws=device.Draws();
22 std::cout<<fixture.host.Device().Status()<<'\n';
23 struct Vertex {float x,y,z,nx,ny,nz,u,v;};
24 const Vertex vertices[]={{-1,1,.5f,0,0,1,0,0},{1,1,.5f,0,0,1,1,0},{-1,-1,.5f,0,0,1,0,1},
25 {-1,-1,.5f,0,0,1,0,1},{1,1,.5f,0,0,1,1,0},{1,-1,.5f,0,0,1,1,1}};
26 Geometry11 geometry;NativeUiTest11::HR(geometry.Create(device.Native(),sizeof(vertices),false));
27 void* memory=nullptr;NativeUiTest11::HR(geometry.Lock(0,sizeof(vertices),&memory));
28 memcpy(memory,vertices,sizeof(vertices));NativeUiTest11::HR(geometry.Unlock());
29 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.normal=12;layout.uv[0]=24;
30 const auto texture=[&](const std::array<DWORD,4>& pixels) {
31 D3D11_TEXTURE2D_DESC desc{};desc.Width=4;desc.Height=desc.MipLevels=desc.ArraySize=desc.SampleDesc.Count=1;
32 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
33 D3D11_SUBRESOURCE_DATA data{pixels.data(),16,16};
34 ComPtr<ID3D11Texture2D> image;ComPtr<ID3D11ShaderResourceView> view;
35 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,&data,&image));
36 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(image.Get(),nullptr,&view));return view;
37 };
38 // Texture colors/black provide monitor contrast and a dark painted panel.
39 const auto screen=texture({0xff000000,0xff20c0f0,0xffe08030,0xffa0a0a0});
40 const auto paint=texture({0xff303030,0xff404040,0xff505050,0xff606060});
41 const auto labels=texture({0xff000000,0xff000000,0xff000000,0xffffffff});
42 const auto pixels=[&](CloudVolume::Point fill,bool monitor,bool litLabels,bool dome,NightVisionGain::Response vision={},float lampPower=1.f) {
43 draws.BindScene();draws.Material(-1);draws.Fixed()=FixedState11{};
44 auto& state=draws.Fixed();state.legacyPixelCenters=false;
45 state.lights[0].kind=1;state.lights[0].ambient={fill.x,fill.y,fill.z,1};
46 const auto lamp=CloudVolume::Scale(dome ? CloudVolume::Point{191.f/255,181.f/255,168.f/255}
47 : CloudVolume::Point{64.f/255,0,0},lampPower);
48 state.textureFactor={lamp.x,lamp.y,lamp.z,1};
49 if(vision.clouds && !monitor)state.nightVisionSurface={vision.red,vision.green,vision.blue,1};
50 // Native stages used by cockpit::Render: cabin lamp + diffuse, painted
51 // surface, then label emission. Monitors select their texture directly.
52 state.stages[0].color=TextureOp11::AddSmooth;
53 state.stages[0].color1=TextureArg11::Factor;state.stages[0].color2=TextureArg11::Diffuse;
54 state.stages[0].alpha=TextureOp11::Select1;state.stages[0].alpha1=TextureArg11::Factor;
55 state.stages[1].color=monitor ? TextureOp11::Select1 : TextureOp11::Modulate;
56 state.stages[1].color1=TextureArg11::Texture;state.stages[1].color2=TextureArg11::Current;
57 state.stages[1].texture=monitor ? screen : paint;
58 state.stages[1].alpha=TextureOp11::Select1;state.stages[1].alpha1=TextureArg11::Texture;
59 if(litLabels) {
60 state.stages[2].color=TextureOp11::AddSmooth;
61 state.stages[2].color1=TextureArg11::Texture;state.stages[2].color2=TextureArg11::Current;
62 state.stages[2].texture=labels;
63 }
64 for(auto& stage:state.stages)stage.sampler.Filter=D3D11_FILTER_MIN_MAG_MIP_POINT;
65 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;draws.Pipeline().depth.DepthEnable=FALSE;
66 draws.Pipeline().depth.StencilEnable=FALSE;draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
67 device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
68 NativeUiTest11::HR(draws.Draw(geometry,layout,Primitive11::Triangles,0,2));return fixture.Pixels();
69 };
70 const CloudVolume::Point rawCloud{.0025f,.0025f,.0025f},sky{.0015f,.002f,.0025f};
71 const auto gain=NightVisionGain::Calculate(true,false,.0025f);
72 const CloudVolume::Point enhanced{CloudVolume::Saturate(rawCloud.x*gain.red),
73 CloudVolume::Saturate(rawCloud.y*gain.green),CloudVolume::Saturate(rawCloud.z*gain.blue)};
74 for(bool dome:{false,true}) for(float height:{-10.f,-.5f,0.f,.5f,1.f,20.f,100.f,20.f,1.f,0.f,-10.f}) {
75 // Feed both states the same unamplified light, as cockpit::Render now
76 // does. Gain is applied after stage 0 combines the lamp and sky fill.
77 const auto off=CloudVolume::VehicleAmbient(rawCloud,sky,height);
78 const auto on=off;
79 const auto baseline=pixels(off,false,false,dome),assisted=pixels(on,false,false,dome,gain);
80 Check(Energy(assisted)>Energy(baseline)+2,"NVG brightens cockpit surfaces above and below deck with cabin lights on or off");
81 Check(pixels(off,true,false,dome)==pixels(on,true,false,dome,gain),"surface-light gain does not multiply separately rendered instrument pixels");
82 const auto marked=pixels(on,false,true,dome,gain);
83 for(unsigned y=4;y<60;++y) {
84 Check((marked[y*64+56]&0xffffff)==0xffffff,"emissive label remains white");
85 Check((marked[y*64+8]&255)<128,"painted panel stays dark enough to distinguish illuminated labels");
86 }
87 Check(baseline==pixels(off,false,false,dome),"removing goggles restores original cockpit pixels");
88 if(!dome) for(unsigned x:{8u,24u,40u,56u}) {
89 const auto p=assisted[32*64+x];
90 Check(((p>>16)&255)>8*(std::max)((p>>8)&255,p&255),
91 "red cabin light keeps a red cast on panels and monitor frames through ascent and descent");
92 }
93 Check(((assisted[32*64+56]>>16)&255)>((assisted[32*64+8]>>16)&255)+10,
94 "NVG preserves painted surface contrast rather than replacing it with a flat tint");
95 }
96 const auto dark=CockpitLighting::Ambient({0,0,0},{0,0,0},100,gain);
97 for(bool dome:{false,true}) {
98 Check(Energy(pixels(dark,false,false,dome,gain))>Energy(pixels(dark,false,false,dome))+2,
99 "zero atmospheric light still enhances reflected cabin illumination in orbit");
100 Check(pixels(dark,true,false,dome,gain)==pixels(dark,true,false,dome),"orbital cabin-light gain does not multiply separately rendered instrument pixels");
101 Check(pixels(dark,false,true,dome,gain)!=pixels(dark,false,true,dome),"labels remain separate while their orbital panel brightens");
102 }
103 // Reproduce the old input combination: separately clipped red lamp and
104 // nearly white ambient. The repaired mixture must retain red dominance.
105 const auto oldGray=pixels(enhanced,false,false,false,{},255.f/64);
106 const auto red=pixels(rawCloud,false,false,false,gain);
107 Check(((oldGray[32*64+40]>>8)&255)>50,"regression fixture reproduces the old neutral washout");
108 Check(((red[32*64+40]>>8)&255)<5,"mixed-light exposure removes the neutral washout");
109 std::cout<<"BCL panel RGB: old "<<((oldGray[32*64+40]>>16)&255)<<','<<((oldGray[32*64+40]>>8)&255)<<','<<(oldGray[32*64+40]&255)
110 <<"; repaired "<<((red[32*64+40]>>16)&255)<<','<<((red[32*64+40]>>8)&255)<<','<<(red[32*64+40]&255)<<'\n';
111 for(bool dome:{false,true}) {
112 for(const auto reset:{NightVisionGain::Calculate(false,false,.0025f),
113 NightVisionGain::Calculate(true,true,.0025f),NightVisionGain::Calculate(true,false,1.f)})
114 Check(pixels(rawCloud,false,true,dome,reset)==pixels(rawCloud,false,true,dome),
115 "surface response clears for equipment/camera, simulator and daylight");
116 Check(Energy(pixels({0,0,0},false,false,dome,gain,0))==0,"unpowered cockpit in darkness gains no artificial light");
117 float previous=-1;
118 for(float power:{0.f,.000001f,.0001f,.01f,.1f,1.f}) {
119 const auto value=Energy(pixels({0,0,0},false,false,dome,gain,power));
120 Check(value>=previous,"dimming cannot brighten the assisted cabin");previous=value;
121 }
122 }
123 const auto unlit=pixels(rawCloud,false,false,false,gain,0);
124 Check(Energy(unlit)>Energy(pixels(rawCloud,false,false,false,{},0))+10,
125 "without cabin lamps, NVG still enhances faint outside fill");
126 Check(((unlit[32*64+40]>>16)&255)<(unlit[32*64+40]&255),"no synthetic red tint when the red lamp is off");
127 // Crossing the cloud-top blend in either direction has no exposure state.
128 const auto edgeA=CloudVolume::VehicleAmbient(rawCloud,sky,.2499f);
129 const auto edgeB=CloudVolume::VehicleAmbient(rawCloud,sky,.2501f);
130 Check(pixels(edgeA,false,false,false,gain)==pixels(edgeB,false,false,false,gain),"cloud-top boundary has no visible step");
131 checks+=RunNightVisionDisplay11Tests(fixture);
132 checks+=RunNightVisionStars11Tests(fixture);
133 std::cout<<"PASS: "<<checks<<" native cockpit NVG surface, monitor, label, display optics, stars and round-trip checks.\n";
134 return 0;
135}
136catch(const std::exception& error) {std::cerr<<"FAIL: "<<error.what()<<'\n';return 1;}
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)
Point Scale(Point p, float s)
Definition CloudVolume.h:93
Point VehicleAmbient(Point cloudLight, Point skyLight, float altitudeAboveTop)
Definition CloudVolume.h:84
float Saturate(float x)
Definition CloudVolume.h:49
CloudVolume::Point Ambient(CloudVolume::Point cloudLight, CloudVolume::Point skyLight, float altitudeAboveTop, const NightVisionGain::Response &vision)
Response Calculate(bool active, bool simulator, float luminance)