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

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

1#pragma once
2#include "NativeUiTest11.h"
3#include "../Viewscreen/NightVisionStars.h"
4#include <iostream>
5
6inline unsigned RunNightVisionStars11Tests(NativeUiTest11& fixture)
7{
8 using namespace Rendering;
9 using Microsoft::WRL::ComPtr;
10 unsigned checks=0;
11 const auto check=[&](bool ok,const char* message) {++checks;if(!ok)throw std::runtime_error(message);};
12 NativeUiTest11::HR(fixture.host.SetSamples(1));fixture.Resize(512,256);
13 auto& device=fixture.device;auto& draws=device.Draws();
14 ComPtr<RenderTexture> texture;
15 NativeUiTest11::HR(LoadTexture(&device,L"Textures/pointsprite.dds",&texture));
16 struct Vertex {float x,y,z,size;DWORD color;};
17 std::vector<Vertex> stars;
18 // Catalog-sized faint/median/bright stars, plus large warm/cool stars.
19 const DWORD colors[]={0xff343434,0xff3f3f3f,0xff656565,0xffffffff,
20 0xff403020,0xff203040,0xff806040,0xff406080};
21 for(unsigned row=0;row<3;++row)for(unsigned col=0;col<4;++col) {
22 const float y=row==0 ? 32.f : row==1 ? 96.f : 192.f;
23 stars.push_back({(64.f+128.f*col)/256.f-1.f,1.f-y/128.f,.5f,
24 row==0 ? 4.f : row==1 ? 16.f : 60.f,colors[row==2 ? col+4 : col]});
25 }
26 Geometry11 geometry;NativeUiTest11::HR(geometry.Create(device.Native(),UINT(stars.size()*sizeof(Vertex)),false));
27 void* memory=nullptr;NativeUiTest11::HR(geometry.Lock(0,geometry.Size(),&memory));
28 memcpy(memory,stars.data(),geometry.Size());NativeUiTest11::HR(geometry.Unlock());
29 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.pointSize=12;layout.diffuse=16;
30 const auto readPixels=[&]() {
31 D3D11_TEXTURE2D_DESC desc{};fixture.host.Device().SceneColor()->GetDesc(&desc);
32 const UINT samples=desc.SampleDesc.Count;
33 desc.SampleDesc={1,0};desc.BindFlags=desc.MiscFlags=0;
34 ComPtr<ID3D11Texture2D> resolved,staging;
35 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,nullptr,&resolved));
36 if(samples>1)device.Context()->ResolveSubresource(resolved.Get(),0,fixture.host.Device().SceneColor(),0,desc.Format);
37 else device.Context()->CopyResource(resolved.Get(),fixture.host.Device().SceneColor());
38 desc.Usage=D3D11_USAGE_STAGING;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
39 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,nullptr,&staging));
40 device.Context()->CopyResource(staging.Get(),resolved.Get());
41 D3D11_MAPPED_SUBRESOURCE map{};NativeUiTest11::HR(device.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&map));
42 std::vector<DWORD> result(size_t(desc.Width)*desc.Height);
43 for(UINT y=0;y<desc.Height;++y)memcpy(result.data()+size_t(y)*desc.Width,
44 static_cast<const BYTE*>(map.pData)+size_t(y)*map.RowPitch,desc.Width*4);
45 device.Context()->Unmap(staging.Get(),0);
46 for(auto& p:result)p=(p&0xff00ff00)|((p&255)<<16)|((p>>16)&255);
47 return result;
48 };
49 const auto render=[&](NightVisionGain::Response vision,float obscuration=0.f) {
50 draws.BindScene();draws.Material(-1);draws.Fixed()=FixedState11{};
51 auto& state=draws.Fixed();state.legacyPixelCenters=false;state.lighting=false;
52 state.pointSprite=true;
53 const float gain=NightVisionStars::Gain(vision);
54 state.nightVisionSurface={gain,gain,gain,gain>1.f ? 1.f : 0.f};
55 state.textureFactor={0,0,0,obscuration};
56 state.stages[0].color=TextureOp11::Modulate;
57 state.stages[0].color1=TextureArg11::Texture;state.stages[0].color2=TextureArg11::Diffuse;
58 state.stages[0].texture=texture->View();
59 state.stages[0].sampler.Filter=D3D11_FILTER_MIN_MAG_LINEAR_MIP_POINT;
60 state.stages[0].sampler.MaxLOD=D3D11_FLOAT32_MAX;
61 state.stages[1].color=TextureOp11::BlendFactorAlpha;
62 state.stages[1].color1=TextureArg11::Factor;state.stages[1].color2=TextureArg11::Current;
63 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;draws.Pipeline().depth.DepthEnable=FALSE;
64 draws.Pipeline().depth.StencilEnable=FALSE;
65 auto& blend=draws.Pipeline().blend.RenderTarget[0];blend.BlendEnable=TRUE;
66 blend.SrcBlend=D3D11_BLEND_INV_DEST_COLOR;blend.DestBlend=D3D11_BLEND_DEST_COLOR;
67 device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
68 NativeUiTest11::HR(draws.Draw(geometry,layout,Primitive11::Points,0,UINT(stars.size())));
69 state.nightVisionSurface={};
70 return readPixels();
71 };
72 const auto energy=[](const std::vector<DWORD>& pixels,unsigned x0,unsigned y0,unsigned x1,unsigned y1) {
73 uint64_t sum=0;
74 for(unsigned y=y0;y<y1;++y)for(unsigned x=x0;x<x1;++x) {
75 const auto p=pixels[y*512+x];sum+=(p&255)+((p>>8)&255)+((p>>16)&255);
76 }
77 return sum;
78 };
79 const auto vision=NightVisionGain::Calculate(true,false,.0025f);
80 check(NightVisionStars::Gain(vision)>7.9f && NightVisionStars::Gain(vision)<=8.f,"night star gain is strong but bounded");
81 for(UINT samples:{1u,4u}) {
82 NativeUiTest11::HR(fixture.host.SetSamples(samples));device.Reset();
83 const auto off=render({}),on=render(vision);
84 if(samples==1) {
85 render({});fixture.Save(L"Output/nvg-stars-fixture-off.png");
86 render(vision);fixture.Save(L"Output/nvg-stars-fixture-on.png");
87 }
88 for(unsigned col=0;col<4;++col) {
89 const auto plain=energy(off,col*128,0,col*128+128,64);
90 const auto enhanced=energy(on,col*128,0,col*128+128,64);
91 std::cout<<"Star sample "<<samples<<"x, "<<col<<": "<<plain<<" -> "<<enhanced<<'\n';
92 check(plain>0 && enhanced>plain*3,"four-pixel catalog stars gain plainly visible brightness with MSAA on or off");
93 if(col>0)check(enhanced>energy(on,(col-1)*128,0,col*128,64),"star brightness ordering survives enhancement");
94 }
95 check(energy(on,0,0,512,256)>3*energy(off,0,0,512,256),"whole star field gains visible signal");
96 check((on[192*512+64]>>16&255)>(on[192*512+64]&255)*1.8,"warm star stays warm through the highlight shoulder");
97 check((on[192*512+192]&255)>(on[192*512+192]>>16&255)*1.8,"cool star stays cool through the highlight shoulder");
98 check((on[192*512+64]&0xffffff)!=0xffffff,"bright star core does not clip to neutral white");
99 check(energy(on,0,0,32,256)==0,"black sky outside sprite support stays black");
100 check(energy(render(vision,1),0,0,512,256)==0,"opaque atmospheric blend still hides stars completely");
101 uint64_t prior=0;
102 for(float opacity:{1.f,.75f,.5f,.25f,0.f}) {
103 const auto signal=energy(render(vision,opacity),0,0,512,256);
104 check(signal>=prior,"increasing atmospheric transmission reveals stars monotonically");prior=signal;
105 }
106 for(float opacity:{0.f,.25f,.5f,.75f,1.f}) {
107 const auto signal=energy(render(vision,opacity),0,0,512,256);
108 check(signal<=prior,"descending into atmospheric obscuration attenuates stars monotonically");prior=signal;
109 }
110 for(const auto reset:{NightVisionGain::Calculate(false,false,.0025f),
111 NightVisionGain::Calculate(true,true,.0025f),NightVisionGain::Calculate(true,false,1.f)})
112 check(render(reset)==off,"off/external-view, simulator and daylight restore exact baseline star pixels");
113 check(render(vision)==on && render({})==off,"on/off round trip has no persistent star exposure");
114 check(draws.Fixed().nightVisionSurface.w==0,"star gain is cleared before the next draw");
115 std::cout<<"Stars "<<samples<<"x MSAA: faint 4px signal "<<energy(off,0,0,128,64)
116 <<" -> "<<energy(on,0,0,128,64)<<"; field "<<energy(off,0,0,512,256)
117 <<" -> "<<energy(on,0,0,512,256)<<'\n';
118 if(samples==1) {
119 render({});fixture.Save(L"Output/nvg-stars-fixture-off.png");
120 render(vision);fixture.Save(L"Output/nvg-stars-fixture-on.png");
121 }
122 }
123 float previous=1;
124 for(float luminance:{.35f,.3499f,.3f,.2f,.1f,.05f,.0025f,0.f}) {
125 const float gain=NightVisionStars::Gain(NightVisionGain::Calculate(true,false,luminance));
126 check(gain>=previous && gain<=8.f,"darkening low-light response increases bounded star gain without an altitude discontinuity");previous=gain;
127 }
128 return checks;
129}
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)
UINT Size() const
Definition Geometry11.h:38
Response Calculate(bool active, bool simulator, float luminance)
float Gain(const NightVisionGain::Response &vision)
HRESULT LoadTexture(RenderDevice *owner, const wchar_t *path, RenderTexture **result, UINT levels=0, UINT width=0, UINT height=0)