1#include "../Viewscreen/CockpitLightning.h"
2
3
4void CockpitLightningChecks(NativeUiTest11& fixture,bool benchmark)
5{
7 struct GlassVertex {float x,y,z;};
8 const GlassVertex glass[]={{-.003f,-.003f,.002f},{.003f,-.003f,.002f},{-.003f,.003f,.002f},
9 {-.003f,.003f,.002f},{.003f,-.003f,.002f},{.003f,.003f,.002f}};
12 bool visible=true;
14 Check(component==7,"cached glass retains its cockpit component");return visible ? &pane : nullptr;
15 };
16 const auto frame=
LocalLightning::Build({0,3000,4},{0,3000,4},{0,3000,0},{0,0,0},{.8f,.9f,1,1},.2f/
LocalLightning::PeakRadiance,20,
true);
17 const auto on=windows.
Sample(frame,transform);
18 Check(on[0].diffuse.z>.09f && on[0].direction.z==-1,"admitted channel light has the real source direction and distance");
19 auto rear=frame;rear.ground.z=rear.top.z=-4;
20 const auto blocked=windows.
Sample(rear,transform);
21 Check(blocked[0].diffuse.z==0 && blocked[1].diffuse.z==0 && blocked[0].ambient.z>0,
22 "light behind the solid cabin is blocked; weaker scattered cloud fill remains");
24 Check(windows.
Sample(rear,transform)[0].diffuse.z>0 && windows.
Sample(frame,transform)[0].diffuse.z==0,
25 "window admission follows vehicle/component rotation");
27 auto translated=frame;translated.eye.x=1;translated.ground.x=translated.top.x=1;pane._41=1;
28 Check(std::abs(windows.
Sample(translated,transform)[0].diffuse.z-on[0].diffuse.z)<1e-6f,
29 "camera, source and glass translations preserve illumination");
31 auto half=frame;half.color.w*=.5f;
32 Check(std::abs(windows.
Sample(half,transform)[0].diffuse.z/on[0].diffuse.z-.5f)<1e-6f,
"cockpit follows the exterior pulse linearly");
33 auto distant=frame;distant.ground.z=distant.top.z=100;
34 Check(windows.
Sample(distant,transform)[0].diffuse.z==0,
"distant bolts do not directly light the cabin");
35 auto above=frame;above.eye.y=1;
36 Check(windows.
Sample(above,transform)[0].kind==0,
"above-deck cockpit receives no below-cloud channel light");
37 Check(windows.
Sample(frame,transform,0)[0].kind==0,
"simulator/closed bay exposure disables the flash");
38 Check(std::abs(windows.
Sample(frame,transform,.5f)[0].diffuse.z/on[0].diffuse.z-.5f)<1e-6f,
39 "partially open parent bay reduces incident light");
40 visible=false;
41 Check(windows.
Sample(frame,transform)[0].kind==0,
"hidden windows do not admit direct light or diffuse fill");
42 visible=true;
43 Check(
Apertures{}.
Sample(frame,transform)[0].kind==0,
"windowless cabins remain isolated");
44 unsigned lookups=0;
46 Check(lookups==0,"inactive pulses skip all window transforms and ray tests");
47
48 auto& device=fixture.device;auto& draws=device.Draws();
49 const auto saved=draws.Capture();
50 struct Vertex {float x,y,z,nx,ny,nz,u,v;};
51 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},
52 {-1,-1,.5f,0,0,1,0,1},{1,1,.5f,0,0,1,1,0},{1,-1,.5f,0,0,1,1,1}};
53 unsigned primitives=2;
54 Geometry11 geometry;NativeUiTest11::HR(geometry.Create(device.Native(),sizeof(vertices),false));
55 void* memory=nullptr;NativeUiTest11::HR(geometry.Lock(0,sizeof(vertices),&memory));memcpy(memory,vertices,sizeof(vertices));NativeUiTest11::HR(geometry.Unlock());
56 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.normal=12;layout.uv[0]=24;
57 D3D11_TEXTURE2D_DESC desc{};desc.Width=desc.Height=desc.MipLevels=desc.ArraySize=desc.SampleDesc.Count=1;
58 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.BindFlags=D3D11_BIND_SHADER_RESOURCE;
59 DWORD pixel=0xff8090a0;D3D11_SUBRESOURCE_DATA data{&pixel,4,4};
60 Microsoft::WRL::ComPtr<ID3D11Texture2D> texture;Microsoft::WRL::ComPtr<ID3D11ShaderResourceView> view;
61 NativeUiTest11::HR(device.Native()->CreateTexture2D(&desc,&data,&texture));
62 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(texture.Get(),nullptr,&view));
63 const auto draw=[&](
const Lights& lights,
bool reverse=
false,
bool monitor=
false) {
64 draws.BindScene();draws.Material(-1);draws.Fixed()=FixedState11{};
65 auto& state=draws.Fixed();state.legacyPixelCenters=false;
66 if(reverse)XMStoreFloat4x4(&state.world,XMMatrixRotationY(XM_PI)*XMMatrixTranslation(0,0,1));
67 state.textureFactor={.025f,.02f,.015f,1};
68 state.stages[0].color=TextureOp11::AddSmooth;state.stages[0].color1=TextureArg11::Factor;state.stages[0].color2=TextureArg11::Diffuse;
69 state.stages[0].alpha=TextureOp11::Select1;state.stages[0].alpha1=TextureArg11::Factor;
70 state.stages[1].color=monitor ? TextureOp11::Select1 : TextureOp11::Modulate;
71 state.stages[1].color1=TextureArg11::Texture;state.stages[1].color2=TextureArg11::Current;state.stages[1].texture=view;
72 state.stages[1].alpha=TextureOp11::Select1;state.stages[1].alpha1=TextureArg11::Texture;
73 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;draws.Pipeline().depth.DepthEnable=FALSE;
74 draws.Pipeline().depth.StencilEnable=FALSE;draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
75 device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
76 {
78 NativeUiTest11::HR(draws.Draw(geometry,layout,Primitive11::Triangles,0,primitives));
79 }
80 Check(state.lights[
Slots[0]].kind==0 && state.lights[
Slots[1]].kind==0,
"cockpit light slots are restored before world drawing");
81 };
82 const auto pixels=[&](
const Lights& lights,
bool reverse=
false,
bool monitor=
false){draw(lights,reverse,monitor);
return fixture.Pixels();};
83 const auto off=pixels({});
84 const double dark=Energy(off),front=Energy(pixels(on)),back=Energy(pixels(on,true));
85 Check(front>back*3 && back>dark,"real cockpit pipeline produces a directional peak and weaker reverse-face fill");
86 Check(Energy(pixels(blocked))<back,"opaque-side strikes produce only weak cloud bounce");
87 Check(pixels({},false,true)==pixels(on,false,true),"emissive monitor texture bypasses cockpit flash illumination");
88 const auto bright=
LocalLightning::Build({0,3000,4},{0,3000,4},{0,3000,0},{0,0,0},{.8f,.9f,1,1},1,20,
true);
89 const auto peak=windows.
Sample(bright,transform);
90 Check(Energy(pixels(peak))>front*3,"full return stroke brightly illuminates the cockpit interior");
91 Check(pixels({},false,true)==pixels(peak,false,true),"full-power lightning still leaves emissive displays unchanged");
92 Check(off==pixels(windows.
Sample({},transform)),
"finished flash exactly restores the original cockpit pixels");
93 FixedState11 restore;restore.lights[4].kind=3;restore.lights[4].diffuse={.1f,.2f,.3f,1};
95 Check(restore.lights[4].kind==3 && restore.lights[4].diffuse.z==.3f,"preexisting nonzero light slots are preserved");
96 windows.
Clear();Check(windows.
Sample(frame,transform)[0].kind==0,
"vehicle changes clear the previous window mesh");
97 std::cout<<"PASS cockpit: dark="<<dark<<", facing="<<front<<", reverse="<<back<<'\n';
98 if(benchmark)
99 {
100
101 for(
unsigned i=0;i<128;++i)windows.
Add(7,glass,6);
102 float sum=0;const auto begin=std::chrono::steady_clock::now();
103 for(unsigned i=0;i<20000;++i)
104 sum+=windows.
Sample((i&1) ? frame : rear,[&](
unsigned){return &pane;})[0].diffuse.z;
105 std::cout<<"CPU cockpit 256 triangles, mixed admitted/blocked flash, us="
106 <<std::chrono::duration<double,std::micro>(std::chrono::steady_clock::now()-begin).count()/20000<<" checksum="<<sum<<'\n';
107 fixture.Resize(1920,1080);
108
109
110 std::vector<Vertex> dense;
111 for(unsigned y=0;y<50;++y)for(unsigned x=0;x<50;++x)for(auto vertex:vertices)
112 {
113 vertex.x=(vertex.x+1)/50-1+float(x)*.04f;
114 vertex.y=(vertex.y+1)/50-1+float(y)*.04f;
115 dense.push_back(vertex);
116 }
117 const UINT bytes=UINT(dense.size()*sizeof(Vertex));primitives=UINT(dense.size()/3);
118 NativeUiTest11::HR(geometry.Create(device.Native(),bytes,false));
119 NativeUiTest11::HR(geometry.Lock(0,bytes,&memory));memcpy(memory,dense.data(),bytes);NativeUiTest11::HR(geometry.Unlock());
120 std::array<double,5> inactive{},active{};
121 draw(on);for(unsigned i=0;i<200;++i)device.Context()->Draw(primitives*3,0);
122 const auto timing=[&](
const Lights& lights) {
123 draw(lights);return CloudGpuTime(device,[&]{device.Context()->Draw(primitives*3,0);},100);
124 };
125 for(unsigned i=0;i<5;++i)
126 {
127
128 if(i&1){active[i]=timing(on);inactive[i]=timing({});}
129 else {inactive[i]=timing({});active[i]=timing(on);}
130 }
131 std::sort(inactive.begin(),inactive.end());std::sort(active.begin(),active.end());
132 std::cout<<"GPU cockpit 15000 vertices fullscreen median ms: inactive="<<inactive[2]<<", flash="<<active[2]<<'\n';
133 fixture.Resize(256,256);
134 }
135 NativeUiTest11::HR(draws.Restore(saved));
136}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
void Add(unsigned component, const Vertex *vertices, size_t count)
Lights Sample(const LocalLightning::Frame &frame, Transform transform, float exposure=1) const
constexpr unsigned Slots[2]
std::array< Rendering::FixedLight11, 2 > Lights
Frame Build(const RiseMath::Vector3 &ground, const RiseMath::Vector3 &top, const RiseMath::Vector3 &origin, const RiseMath::Vector3 &camera, const RiseMath::Color &color, float brightness, float visibilityKm, bool enabled, bool separateFog=false)
constexpr float PeakRadiance
Matrix * MatrixRotationY(Matrix *out, float value)
Matrix * MatrixIdentity(Matrix *out)