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

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

1#include "NativeUiTest11.h"
2#include "../Viewscreen/ShipSurfaceLights.h"
3#include "../Viewscreen/LandingLights.h"
4#include "../Rendering/ShaderBytecode11.h"
5#include <array>
6#include <iostream>
7using namespace Rendering;
8using namespace DirectX;
9using Microsoft::WRL::ComPtr;
10namespace
11{
12 void Check(bool value,const char* message) {if(!value)throw std::runtime_error(message);}
13 struct Effect
14 {
15 RiseMath::Vector3 position,normal;
16 RiseMath::Color diffuse;
17 unsigned long sequencing;
18 float power;
19 };
20 struct Vertex {XMFLOAT3 position,normal;XMFLOAT2 uv,logo;};
21}
22#include "SceneShipLighting11Tests.h"
23int main(int argc,char** argv)
24{
25 try
26 {
27 bool debug=true,warp=false,benchmark=false;
28 std::string nativeReference;
29 for(int i=1;i<argc;++i)
30 if(std::string(argv[i])=="--no-debug-layer")debug=false;
31 else if(std::string(argv[i])=="--benchmark")benchmark=true;
32 else if(std::string(argv[i])=="--warp")warp=true;
33 else if(std::string(argv[i])=="--native-reference" && i+1<argc)nativeReference=argv[++i];
34 else throw std::runtime_error("unknown argument");
35 const std::array<Effect,3> effects{{
36 {{0,0,.004f},{0,0,-1},{1,0,0,1},1,1},
37 {{.012f,0,.004f},{0,0,-1},{1,1,1,1},2,.5f},
38 {{0,0,.004f},{0,0,-1},{0,1,0,1},3,2}}};
39 RiseMath::Matrix model;
41 const auto nav=ShipSurfaceLights::Build(effects,model,1,1,true);
42 const auto flash=ShipSurfaceLights::Build(effects,model,3,1,true);
43 Check(nav.count==1 && flash.count==2,"flash masks enable the correct emitters and exclude landing beams");
44 Check(ShipSurfaceLights::Build(effects,model,3,0,true).count==0
45 && ShipSurfaceLights::Build(effects,model,3,1,false).count==0
46 && ShipSurfaceLights::Build(effects,model,0,1,true).count==0,
47 "power, ownship enable and sequence independently disable local lights");
48 const auto half=ShipSurfaceLights::Build(effects,model,1,.5f,true);
49 Check(half.colors[0].w==nav.colors[0].w*.5f,"local lamp intensity follows electrical power");
50 auto withoutAxis=effects;
51 withoutAxis[0].normal={0,0,0};
52 Check(ShipSurfaceLights::Build(withoutAxis,model,1,1,true).count==1,
53 "omnidirectional bulbs do not require an emission axis");
54 Check(ShipSurfaceLights::Facing({0,0,0},{0,0,1},{0,0,.1f})==1
55 && ShipSurfaceLights::Facing({0,0,0},{0,0,1},{0,0,-.1f})==0
56 && ShipSurfaceLights::Facing({0,0,0},{0,0,1},{0,0,0})==0,
57 "ownship sprites face the real camera and stay finite at zero distance");
58 RiseMath::Matrix rotation,translation;
60 RiseMath::MatrixTranslation(&translation,1,2,3);
61 const auto moved=ShipSurfaceLights::Build(effects,rotation*translation,1,1,true);
62 Check(std::abs(moved.positions[0].x-1.0038f)<.00001f
63 && std::abs(moved.positions[0].y-2)<.00001f && std::abs(moved.positions[0].z-3)<.00001f
64 && moved.directions[0].x<-.999f,"lamps retain their exterior pose and mounting offset");
65 std::array<Effect,40> many;many.fill(effects[0]);
66 Check(ShipSurfaceLights::Build(many,model,1,1,true).count==ShipSurfaceLights::MaxLights,
67 "authored emitter count cannot overrun shader arrays");
68
69 NativeUiTest11 fixture(256,256,warp,debug);
70 auto& device=fixture.device;auto& draws=device.Draws();auto& values=draws.Materials().Values();
71 std::array<Program11,2> baseline;
72 if(!nativeReference.empty())for(unsigned pass:{4u,5u})
73 {
74 const auto vs=ReadShaderBytecode11(nativeReference+"/CustomUI-VS"+std::to_string(pass)+"-1.cso");
75 const auto ps=ReadShaderBytecode11(nativeReference+"/CustomUI-PS"+std::to_string(pass)+"-1.cso");
76 NativeUiTest11::HR(baseline[pass-4].Create(device.Native(),vs.data(),vs.size(),ps.data(),ps.size()));
77 }
78 XMFLOAT4X4 identity,projection;
79 XMStoreFloat4x4(&identity,XMMatrixIdentity());
80 XMStoreFloat4x4(&projection,XMMatrixScaling(1/.03f,1/.03f,1)*XMMatrixTranslation(0,0,.5f));
81 values.Set("matWorld",identity);values.Set("matWorldViewProj",projection);
82 values.Set("ambient",XMFLOAT4(0,0,0,100000));values.Set("Tangent",XMFLOAT4(1,1,1,0));
83 values.Set("Sunlight",XMFLOAT4(0,0,0,0));values.Set("Radiosity",XMFLOAT4(0,0,0,0));
84 values.Set("WeapPos",XMFLOAT4(0,0,0,0));values.Set("shiplight",XMFLOAT4(0,0,0,0));
85 values.Set("clouduv",XMFLOAT4(0,0,0,0));values.Set("volumeFog",XMFLOAT4(0,0,0,0));
86 values.Set("LandingLightCount",0);values.Set("Features",XMFLOAT4(1,0,1,0));
87 D3D11_TEXTURE2D_DESC td{};td.Width=td.Height=td.MipLevels=td.ArraySize=td.SampleDesc.Count=1;
88 td.Format=DXGI_FORMAT_R8G8B8A8_UNORM;td.BindFlags=D3D11_BIND_SHADER_RESOURCE;
89 DWORD white=0xffffffff;D3D11_SUBRESOURCE_DATA initial{&white,4,4};
90 ComPtr<ID3D11Texture2D> texture;ComPtr<ID3D11ShaderResourceView> view;
91 NativeUiTest11::HR(device.Native()->CreateTexture2D(&td,&initial,&texture));
92 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(texture.Get(),nullptr,&view));
93 values.Texture("ColorMap",view.Get());
94 Geometry11 mesh;NativeUiTest11::HR(mesh.Create(device.Native(),6*sizeof(Vertex),false));
95 VertexLayout11 layout;layout.stride=sizeof(Vertex);layout.normal=12;layout.uv[0]=24;layout.uv[1]=32;
96 const auto render=[&](unsigned pass,const ShipSurfaceLights::Frame& lamps,float normalZ=1.f,float surfaceZ=0.f) {
97 values.Set("ShipSurfaceLightCount",int(lamps.count));
98 values.Set("ShipSurfaceLightPosition",lamps.positions);
99 values.Set("ShipSurfaceLightDirection",lamps.directions);
100 values.Set("ShipSurfaceLightColor",lamps.colors);
101 const Vertex vertices[]={
102 {{-.03f,.03f,surfaceZ},{0,0,normalZ},{0,0},{0,0}},{{.03f,.03f,surfaceZ},{0,0,normalZ},{1,0},{0,0}},
103 {{-.03f,-.03f,surfaceZ},{0,0,normalZ},{0,1},{0,0}},{{-.03f,-.03f,surfaceZ},{0,0,normalZ},{0,1},{0,0}},
104 {{.03f,.03f,surfaceZ},{0,0,normalZ},{1,0},{0,0}},{{.03f,-.03f,surfaceZ},{0,0,normalZ},{1,1},{0,0}}};
105 void* destination=nullptr;NativeUiTest11::HR(mesh.Lock(0,sizeof(vertices),&destination));
106 memcpy(destination,vertices,sizeof(vertices));NativeUiTest11::HR(mesh.Unlock());
107 draws.BindScene();device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
108 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;
109 draws.Pipeline().depth.DepthEnable=FALSE;draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
110 draws.Material(pass);NativeUiTest11::HR(draws.Draw(mesh,layout,Primitive11::Triangles,0,2));
111 const auto pixels=fixture.Pixels();
112 if(!nativeReference.empty())
113 {
114 device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
115 // Reuse the production geometry/input layout; its signature is
116 // unchanged by a constant-buffer layout change.
117 ComPtr<ID3D11InputLayout> input;
118 device.Context()->IAGetInputLayout(&input);
119 NativeUiTest11::HR(baseline[pass-4].Bind(device.Context(),values));
120 device.Context()->IASetInputLayout(input.Get());
121 device.Context()->Draw(6,0);
122 Check(pixels==fixture.Pixels(),"all hull-light pixels match the frozen native shaders exactly");
123 }
124 return pixels;
125 };
126 const auto energy=[](const std::vector<DWORD>& pixels) {
127 uint64_t sum=0;for(auto p:pixels)sum+=(p&255)+((p>>8)&255)+((p>>16)&255);return sum;
128 };
129 std::filesystem::create_directories("outputs/ship-surface-lighting");
130 for(unsigned pass:{4u,5u})
131 {
132 const auto off=render(pass,{});Check(energy(off)==0,"vacuum without active lights stays dark");
133 const auto red=render(pass,nav);
134 Check(energy(red)>0 && ((red[128*256+128]>>16)&255)>80,
135 "positioned nav light illuminates nearby hull in clear air and space");
136 Check((red[0]&0xffffff)==0 && (red[128*256+128]&0xffff)==0,
137 "nav illumination is local and keeps the authored red color");
138 fixture.Save("outputs/ship-surface-lighting/nav-"+std::to_string(pass)+".png");
139 const auto burst=render(pass,flash);
140 Check(energy(burst)>energy(red)*2,"strobe on-phase creates a visible localized flash");
141 fixture.Save("outputs/ship-surface-lighting/strobe-"+std::to_string(pass)+".png");
142 auto turned=flash;
143 for(auto& axis:turned.directions){axis.x=-axis.x;axis.y=-axis.y;axis.z=-axis.z;}
144 Check(render(pass,turned)==burst,"bulb and strobe surface illumination ignores the emitter axis");
145 Check(energy(render(pass,nav))==energy(red),"strobe off-phase immediately removes its hull illumination");
146 Check(energy(render(pass,flash,-1))==0,"back-facing hull surfaces receive no direct lamp light");
147 Check(energy(render(pass,flash,-1,.02f))>0,"bulbs and strobes illuminate receiving surfaces behind their mounting axis");
148 Check(energy(render(pass,flash,1,.07f))==0,"light range rejects distant surfaces");
149 Check(energy(render(pass,{}))==0,"clearing the lamp count prevents lighting leaking into later draws");
150 Check(energy(render(pass,ShipSurfaceLights::Build(many,model,1,1,true)))>energy(red),
151 "all 32 light slots contribute without overrunning the dedicated buffer");
152 Check(energy(render(pass,nav))==energy(red),"reducing the light count removes stale array entries");
153 }
154 if (nativeReference.empty())
155 {
156 std::array<LandingLights::Light,2> lenses{};
157 lenses[0].origin={-.015f,0,0};lenses[1].origin={.015f,0,0};
158 values.Texture("ColorMap2",view.Get());
159 const auto sample=[](const std::vector<DWORD>& pixels,unsigned x) {return pixels[128*256+x]&255;};
160 for(unsigned pass:{4u,5u})
161 {
162 values.Set("clouduv",XMFLOAT4(2,1,0,43));
163 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{1,1},model));
164 const auto healthy=render(pass,{});
165 Check(sample(healthy,64)>250 && sample(healthy,192)>250,"both intact headlight lenses glow");
166 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{0,1},model));
167 const auto broken=render(pass,{});
168 Check(sample(broken,64)==0 && sample(broken,192)>250,
169 "destroyed lens loses its emission while the opposite lens remains lit");
170 fixture.Save("outputs/ship-surface-lighting/headlight-broken-"+std::to_string(pass)+".png");
171 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{.5f,1},model));
172 const auto dim=render(pass,{});
173 Check(sample(dim,64)>=126 && sample(dim,64)<=129 && sample(dim,192)>250,
174 "damaged lens receives half-brightness electrical output");
175 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{0,1},model));
176 Check(render(pass,{})==broken,"flicker off uses the same lens pixels as a broken circuit");
177 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{1,1},model));
178 Check(render(pass,{})==healthy,"repair immediately restores headlight lens emission");
179 values.Set("HeadlightEmission",LandingLights::EmissionSources(lenses,2,{0,0},model));
180 values.Set("clouduv",XMFLOAT4(0,1,0,43));
181 Check(render(pass,{})==healthy,"remote and simulator materials preserve baseline emission");
182 values.Set("clouduv",XMFLOAT4(2,1,0,44));
183 Check(render(pass,{})==healthy,"headlight damage cannot dim unrelated brakelight materials");
184 }
185 values.Set("clouduv",XMFLOAT4(0,0,0,0));
186 std::cout<<"PASS: GPU headlight lens damage, opposite lamp, dimming, flicker, repair and remote/simulator isolation in both vehicle passes\n";
187 }
188 SceneShipLightingChecks(fixture,benchmark);
189 if(debug)fixture.DebugClean();
190 std::cout<<"PASS: ownship nav/strobe selection, pose, power, flash timing, localized hull pixels, surface angles and state reset\n";
191 return 0;
192 }
193 catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
194}
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)
Definition DDS.h:24
Sources Create(int count, Kind kind, float sideArmKm, float forwardKm)
Definition EngineAudio.h:55
std::array< RiseMath::Vector4, MaxLights > EmissionSources(const std::array< Light, MaxLights > &lights, unsigned count, const std::array< float, MaxLights > &brightness, const RiseMath::Matrix &modelToWorld)
std::vector< char > ReadShaderBytecode11(const std::filesystem::path &path, HMODULE resourceModule=nullptr)
Matrix * MatrixRotationY(Matrix *out, float value)
Definition ClientMath.h:127
Matrix * MatrixIdentity(Matrix *out)
Definition ClientMath.h:115
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
Definition ClientMath.h:125
constexpr float HalfPi
Definition ClientMath.h:12
void Bind(Rendering::RenderDevice &device, Rendering::RenderTexture *texture)
constexpr unsigned MaxLights
Frame Build(const Effects &effects, const RiseMath::Matrix &modelToWorld, unsigned long phase, float power, bool enabled)
float Facing(const RiseMath::Vector3 &position, const RiseMath::Vector3 &direction, const RiseMath::Vector3 &eye)