1#pragma once
2#include "../Viewscreen/NightVisionGain.h"
3
4static std::vector<unsigned char> NightVisionCloudOpacity(Device11& gpu,CloudRenderer11&
renderer)
5{
6 ComPtr<ID3D11Resource> resource;
renderer.Pass().Image()->GetResource(&resource);
7 ComPtr<ID3D11Texture2D> texture;HR(resource.As(&texture),"NVG cloud image texture");
8 D3D11_TEXTURE2D_DESC desc{};texture->GetDesc(&desc);
9 const bool halfFloat=desc.Format==DXGI_FORMAT_R16G16B16A16_FLOAT;
10 Check(desc.Format==DXGI_FORMAT_B8G8R8A8_UNORM || halfFloat,"NVG opacity readback format");
11 desc.Usage=D3D11_USAGE_STAGING;desc.BindFlags=desc.MiscFlags=0;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
12 ComPtr<ID3D11Texture2D> staging;HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&staging),"NVG opacity staging");
13 gpu.Context()->CopyResource(staging.Get(),texture.Get());
14 D3D11_MAPPED_SUBRESOURCE mapped{};HR(gpu.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"NVG opacity map");
15 const unsigned alphaBytes=halfFloat ? 2u : 1u,pixelBytes=alphaBytes*4;
16 std::vector<unsigned char> alpha(size_t(desc.Width)*desc.Height*alphaBytes);
17 for(UINT y=0;y<desc.Height;++y)for(UINT x=0;x<desc.Width;++x)
18 memcpy(alpha.data()+(size_t(y)*desc.Width+x)*alphaBytes,
19 static_cast<const unsigned char*>(mapped.pData)+y*mapped.RowPitch+x*pixelBytes+3*alphaBytes,alphaBytes);
20 gpu.Context()->Unmap(staging.Get(),0);return alpha;
21}
22
23static void TestNightVisionClouds(Device11& gpu,CloudRenderer11&
renderer,UINT samples)
24{
25 if(!gpu.SupportsSamples(samples))return;
26 Pipeline pipeline(gpu.Device(),samples);Scene scene(gpu.Device(),samples);
28 XMFLOAT4X4 view,projection;
29 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,384));
30 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};
31
32
34 const XMFLOAT4 gain(response.red,response.green,response.blue,1);
36 for(float altitude:{.5f,1.2f,2.6f,5.f,25.f})
37 {
38 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,altitude>10 ? -.6f : .2f,-1,0),XMVectorSet(0,1,0,0)));
39 const auto render=[&](bool boostedAmbient,bool boostedInterior,bool sources=true) {
40 scene.Clear(gpu.Context());pipeline.Occlude(gpu,scene);
41 const XMFLOAT4 ambient=boostedAmbient ? XMFLOAT4(.0025f*gain.x,.0025f*gain.y,1,1) : XMFLOAT4(.0025f,.0025f,.0025f,1);
42 const XMFLOAT4 direct=sources ? XMFLOAT4(.825f,.842f,.876f,1) : XMFLOAT4();
43 HR(
renderer.Render(gpu.Context(),scene.target.Get(),scene.depth,viewport,view,projection,
44 {0,cloud.radius+altitude,0},cloud,{-.998749f,-.05f,0},direct,ambient,
45 {0,0,0,1},{ambient.x,ambient.y,ambient.z,42.16f},{0,0,0,0},
46 boostedInterior ? gain : XMFLOAT4(1,1,1,0)),"NVG cloud draw");
47 return pipeline.Pixels(gpu,scene,samples);
48 };
49 const auto off=render(
false,
false);
const auto offAlpha=NightVisionCloudOpacity(gpu,
renderer);
50 const auto missingInterior=render(true,false);
51 const auto on=render(
true,
true);
const auto onAlpha=NightVisionCloudOpacity(gpu,
renderer);
52 Check(offAlpha==onAlpha,"NVG leaves actual cloud opacity unchanged");
53 const auto disabledAgain=render(false,false);
54 double offLight=0,missingLight=0,onLight=0;unsigned exterior=0;
55 for(UINT y=0;y<
Height;++y)
for(UINT x=0;x<
Width;++x)
for(UINT s=0;s<samples;++s)
56 {
57 const size_t i=(y*
Width+x)*samples+s;
58 Check(off[i].depth==on[i].depth,"NVG leaves scene depth unchanged");
59 const bool protectedSample=off[i].depth<1 || x>=3*
Width/5;
60 for(unsigned c=0;c<4;++c)
61 {
62 Check(off[i].color[c]==disabledAgain[i].color[c],"disabling NVG restores the same frame without lingering gain");
63 if(protectedSample)Check(off[i].color[c]==on[i].color[c],"NVG cloud pass preserves cockpit and foreground samples");
64 }
65 if(!protectedSample)
66 {
67 offLight+=off[i].color[1];missingLight+=missingInterior[i].color[1];onLight+=on[i].color[1];++exterior;
68 }
69 }
70 Check(exterior>100,"NVG fixture contains exterior pixels");
71 if(altitude>=2.6f)
72 Check(onLight/exterior>missingLight/exterior+.10,"NVG remains effective beyond the cloud-base lighting blend");
73 Check(onLight/exterior>offLight/exterior+.15,"NVG enhances below, inside and above-deck cloud views");
74 const auto ambientOnly=render(true,false,false),ambientOnlyGain=render(true,true,false);
75 for(size_t i=0;i<ambientOnly.size();++i)for(unsigned c=0;c<4;++c)
76 Check(ambientOnly[i].color[c]==ambientOnlyGain[i].color[c],"pre-amplified cloud ambient/fog is not amplified twice");
77 if(samples==1)
78 {
79 for(unsigned variant=0;variant<2;++variant)
80 {
81 std::ofstream image("Output/nvg-cloud-"+std::to_string(int(altitude*1000))+(variant ? "-after.ppm" : "-before.ppm"),std::ios::binary);
83 const auto& pixels=variant ? on : missingInterior;
84 for(const auto& pixel:pixels)for(unsigned c=0;c<3;++c)image.put(char(std::lround(pixel.color[c]*255)));
85 }
86 }
87 std::cout<<"PASS: NVG "<<samples<<"x at "<<altitude<<" km, exterior green off/ambient-only/full "
88 <<offLight/exterior<<'/'<<missingLight/exterior<<'/'<<onLight/exterior<<'\n';
89 }
90 DebugMessages(gpu);
91}
Response Calculate(bool active, bool simulator, float luminance)
CloudWeather::Frame weather