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

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

1#pragma once
2
3static void TestCloudNightVisionColors(Device11& gpu,CloudRenderer11& renderer,UINT samples,
4 const std::filesystem::path& referenceShaders={})
5{
6 if(!gpu.SupportsSamples(samples))return;
7 Pipeline pipeline(gpu.Device(),samples);Scene scene(gpu.Device(),samples);
8 CloudVolume::Parameters cloud;cloud.base=1.8f;cloud.weather=renderer.WeatherFrame();
9 XMFLOAT4X4 view,projection;
10 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,4096));
11 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};
12 renderer.SetHeadlights({});renderer.SetLocalLightning({},{},0);
13 const CloudVolume::Point glow{.125f,.375f,.25f};
14 const float sun=-.2f,incident=std::exp(-.5f*std::exp(-cloud.top/7.5f));
15 const CloudVolume::Point sunlight{incident*240.f/255,incident*245.f/255,incident};
16 const std::vector<float> altitudes{.5f,2.f,5.f,9.f,9.9f,10.2f,10.7f,12.f,25.f,40.f,100.f,500.f};
17 CloudRenderer11 previous;
18 if(samples==1 && !referenceShaders.empty())
19 {
20 HR(previous.Initialize(gpu.Device(),referenceShaders),"previous NVG cloud initialize");
21 previous.SetLightningEnabled(false);
22 previous.UpdateWeather(cloud.weather.current,60);
23 HR(previous.PrepareResources(gpu.Context()),"previous NVG weather upload");
24 previous.UpdateWeather(cloud.weather.current,60);
25 }
26 for(bool organic:{false,true})
27 {
28 cloud.organic=organic;
29 const auto render=[&](float altitude,bool active,bool forceHighGain=false,bool simulator=false,
30 bool priorExposure=false) {
31 const CloudVolume::Point eye{0,cloud.radius+altitude,0};
32 const auto column=CloudVolume::Boundaries(eye,cloud);
33 const float transmission=CloudVolume::DiffuseSunTransmission(eye,{std::sqrt(1-sun*sun),sun,0},cloud);
34 const auto scattered=CloudVolume::ScatteredSunlight(sunlight,.05f,transmission);
35 const auto optics=CloudNightVision::Evaluate(active,simulator,scattered,glow,sun,altitude,column.base,column.top);
36 const auto gain=forceHighGain ? NightVisionGain::Calculate(true,false,.0025f) : priorExposure
37 ? NightVisionGain::Calculate(active,simulator,CloudNightVision::Luminance(scattered)) : optics.gain;
38 const auto oldAmbient=CloudVolume::WithAirglow({std::clamp(scattered.x*gain.red,0.f,1.f),
39 std::clamp(scattered.y*gain.green,0.f,1.f),std::clamp(scattered.z*gain.blue,0.f,1.f)},
40 glow,sun,altitude,column.base,column.top);
41 const auto fog=priorExposure ? oldAmbient : optics.fog;
42 const XMFLOAT4 ambient=forceHighGain ? XMFLOAT4{} : XMFLOAT4(fog.x,fog.y,fog.z,1);
43 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,altitude>10 ? -.8f : .2f,-1,0),XMVectorSet(0,1,0,0)));
44 scene.Clear(gpu.Context());pipeline.Occlude(gpu,scene);
45 auto& draw=priorExposure ? previous : renderer;
46 HR(draw.Render(gpu.Context(),scene.target.Get(),scene.depth,viewport,view,projection,
47 {eye.x,eye.y,eye.z},cloud,{-std::sqrt(1-sun*sun),-sun,0},{sunlight.x,sunlight.y,sunlight.z,1},ambient,
48 {glow.x,glow.y,glow.z,1},forceHighGain ? XMFLOAT4{} : XMFLOAT4(ambient.x,ambient.y,ambient.z,42.16f),{},
49 gain.clouds ? XMFLOAT4(gain.red,gain.green,gain.blue,1) : XMFLOAT4(1,1,1,0)),"colored NVG cloud draw");
50 return pipeline.Pixels(gpu,scene,samples);
51 };
52 const auto averages=[&](const std::vector<Sample>& pixels) {
53 CloudVolume::Point result{};unsigned count=0;
54 for(UINT y=0;y<Height;++y)for(UINT x=0;x<3*Width/5;++x)for(UINT s=0;s<samples;++s)
55 {
56 const auto& pixel=pixels[(y*Width+x)*samples+s];
57 if(pixel.depth<1)continue;
58 result.x+=pixel.color[0];result.y+=pixel.color[1];result.z+=pixel.color[2];++count;
59 }
60 Check(count>100,"color fixture has visible exterior samples");
61 return CloudVolume::Scale(result,1.f/count);
62 };
63 const auto highlight=averages(render(5,true,true));
64 Check(highlight.y>.5f && highlight.x<highlight.y*.4f && highlight.z<highlight.y*.85f,
65 "actual shader preserves bright green airglow even at extreme exposure without fog");
66 std::vector<std::vector<Sample>> climb;
67 for(float altitude:altitudes)
68 {
69 const auto off=render(altitude,false);const auto alpha=NightVisionCloudOpacity(gpu,renderer);
70 const auto on=render(altitude,true);const auto onAlpha=NightVisionCloudOpacity(gpu,renderer);
71 Check(alpha==onAlpha,"color exposure does not change cloud density/opacity");
72 const auto returned=render(altitude,false);
73 const auto unassisted=averages(off),assisted=averages(on);
74 std::cout<<"NVG color "<<(organic ? "organic" : "legacy")<<' '<<samples<<"x "<<altitude
75 <<" km RGB "<<assisted.x<<','<<assisted.y<<','<<assisted.z<<" off G "<<unassisted.y<<'\n';
76 if(altitude<=12)
77 {
78 Check(assisted.y>unassisted.y*2,"climb retains enhanced cloud brightness with airglow enabled");
79 Check(assisted.y>assisted.x*1.2f && assisted.y>assisted.z,
80 "climb retains cloud hue inside and across the nominal top");
81 }
82 for(size_t i=0;i<on.size();++i)
83 {
84 Check(on[i].depth==off[i].depth,"color exposure preserves scene depth");
85 const bool protectedSample=off[i].depth<1 || (i/samples)%Width>=3*Width/5;
86 for(unsigned c=0;c<4;++c)
87 {
88 Check(returned[i].color[c]==off[i].color[c],"NVG off restores baseline immediately");
89 if(protectedSample)Check(on[i].color[c]==off[i].color[c],"cloud color optics preserve cockpit/HUD stencil");
90 }
91 }
92 if(samples==1 && organic && (altitude==2 || altitude==9 || altitude==10.7f || altitude==25))
93 {
94 for(unsigned mode=0;mode<2;++mode)
95 {
96 std::ofstream image("Output/nvg-color-"+std::to_string(int(altitude*1000))+(mode ? "-on.ppm" : "-off.ppm"),std::ios::binary);
97 image<<"P6\n"<<Width<<' '<<Height<<"\n255\n";
98 for(const auto& pixel:mode ? on : off)for(unsigned c=0;c<3;++c)
99 image.put(char(std::lround(pixel.color[c]*255)));
100 }
101 }
102 if(samples==1 && organic && altitude==25 && previous.Ready())
103 {
104 const auto before=render(altitude,true,false,false,true);
105 float maximum=0;double total=0;
106 std::ofstream image("Output/nvg-color-25000-before.ppm",std::ios::binary);
107 image<<"P6\n"<<Width<<' '<<Height<<"\n255\n";
108 for(size_t i=0;i<before.size();++i)for(unsigned c=0;c<3;++c)
109 {
110 const float difference=std::abs(before[i].color[c]-on[i].color[c]);
111 maximum=(std::max)(maximum,difference);total+=difference;
112 image.put(char(std::lround(before[i].color[c]*255)));
113 }
114 std::cout<<"NVG 25 km controlled previous/current comparison: mean RGB delta "
115 <<total/(before.size()*3)<<", maximum "<<maximum
116 <<". This fixture does not reproduce the accepted stream capture's exact conditions.\n";
117 }
118 climb.push_back(on);
119 }
120 for(size_t step=altitudes.size();step-->0;)
121 {
122 const auto down=render(altitudes[step],true);
123 for(size_t i=0;i<down.size();++i)for(unsigned c=0;c<4;++c)
124 Check(std::abs(down[i].color[c]-climb[step][i].color[c])<=1.01f/255,
125 "descent restores climb colors without latching exposure");
126 }
127 }
128 previous.Reset();
129 DebugMessages(gpu);
130 std::cout<<"PASS: native NVG cloud color/brightness, fog-free clipping, ascent/descent and protected samples.\n";
131}
float Luminance(CloudVolume::Point light)
Lighting Evaluate(bool active, bool simulator, CloudVolume::Point scattered, CloudVolume::Point airglow, float sunCosine, float altitude, float base, float top)
Point Scale(Point p, float s)
Definition CloudVolume.h:93
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters &params, uint32_t seed=DefaultSeed)
Column Boundaries(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Point ScatteredSunlight(Point incident, float sunCosine, float transmission)
Point WithAirglow(Point scattered, Point airglow, float sunCosine, float altitude, float base, float top)
VehicleSoundSpatial::Mix Calculate(const Sources &cache, int slot, const RiseMath::Vector3 &cameraFromVehicle, float yaw, float pitch, float roll, const RiseMath::Matrix &bodyToCameraFrame, const std::array< float, SlotCount > &weights)
constexpr float Width
constexpr float Height
Response Calculate(bool active, bool simulator, float luminance)
CloudWeather::Frame weather
Definition CloudVolume.h:35