1#pragma once
2#include <chrono>
3
4inline void SceneShipLightingChecks(NativeUiTest11& fixture,bool benchmark)
5{
9 const auto box=[](float x,float y,float z,float radius) {
12 };
13 std::array<Effect,2> sources{{
14 {{0,0,.004f},{0,0,-1},{1,0,0,1},0xaaaaaaaaUL,1},
15 {{.012f,0,.004f},{0,0,-1},{1,1,1,1},0x55555555UL,.5f}}};
16 for(unsigned bit=0;bit<32;++bit)
17 {
18 const auto frame=
Build(sources,identity,uint32_t(1)<<bit,1,
true);
19 Check(frame.count==1,"every bit in the 32-step sequence filters both nav and strobe lamps");
20 Check(frame.positions[0].w==(bit%2 ? .020f : .060f),"sequence bit selects the matching source before transforms");
21 }
22 sources[0].sequencing=1;sources[1].sequencing=2;
23 const auto nav=
Build(sources,identity,1,1,
true),flash=
Build(sources,identity,3,1,
true);
24 Check(
Select(flash,box(0,0,0,.03f),identity).count==2,
"nearby scene surfaces receive active lamps");
25 Check(
Select(flash,box(1,0,0,.03f),identity).count==0,
"distant scene batches reject all lamps on the CPU");
26 Check(
Select(flash,box(.04f,0,0,.001f),identity).mask==2,
"20 m nav range rejects a surface still inside 60 m strobe range");
28 Check(
Select(flash,box(1,0,0,.03f),translation).count==2,
"selection follows translated world bounds rather than model origin");
30 Check(
Select(flash,box(0,0,1,.03f),rotation*translation).count==2,
"rotated and translated bounds retain intersecting lights");
31 Check(
Select(flash,{},identity).
count==2,
"unknown bounds conservatively retain local lights");
32 Check(
Select(
Build(sources,identity,0,1,
true),box(0,0,0,.03f),identity).count==0,
33 "off-phase lights never enter scene selection");
35 for(unsigned i=0;i<32;++i)
36 {
38 crowded.
colors[i]={1,1,1,float(i+1)/32};
39 }
40 const auto chosen=
Select(crowded,box(0,0,0,.03f),identity);
42 "only the four strongest relevant lamps reach a scene batch, including slot 31");
43 const auto edge=nav.positions[0];
44 Check(
Select(nav,box(edge.x+edge.w+.00001f,edge.y,edge.z,0),identity).
count==0
45 &&
Select(nav,box(edge.x+edge.w-.00001f,edge.y,edge.z,0),identity).
count==1,
46 "bounds culling respects the exact finite lamp range");
47
48 auto& device=fixture.device;auto& draws=device.Draws();auto& values=draws.Materials().Values();
49 XMFLOAT4X4 world,projection;
50 XMStoreFloat4x4(&world,XMMatrixIdentity());
51 XMStoreFloat4x4(&projection,XMMatrixScaling(1/.03f,1/.03f,1)*XMMatrixTranslation(0,0,.5f));
52 values.Set("matWorldViewProj",projection);values.Set("matWorld",world);
53 values.Set("ShipSurfaceEye",XMFLOAT4(0,0,1,0));values.Set("Features",XMFLOAT4(0,0,0,0));
54 values.Set("StrikeColor",XMFLOAT4(0,0,0,0));values.Set("TerrainDetail",XMFLOAT4(0,0,0,0));
55 values.Set("CityOverlayEnabled",0);values.Set("flags",std::array<int,5>{});
56 std::array<ComPtr<ID3D11ShaderResourceView>,3> textures;
57 for(unsigned i=0;i<3;++i)
58 {
59 D3D11_TEXTURE2D_DESC td{};td.Width=td.Height=td.MipLevels=td.ArraySize=td.SampleDesc.Count=1;
60 td.Format=DXGI_FORMAT_R8G8B8A8_UNORM;td.BindFlags=D3D11_BIND_SHADER_RESOURCE;
61 DWORD pixel=i==2 ? 0xff404040 : i ? 0xff000000 : 0xffffffff;D3D11_SUBRESOURCE_DATA data{&pixel,4,4};
62 ComPtr<ID3D11Texture2D> texture;NativeUiTest11::HR(device.Native()->CreateTexture2D(&td,&data,&texture));
63 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(texture.Get(),nullptr,&textures[i]));
64 }
65 values.Texture("ColorMap",textures[0].Get());values.Texture("CloudMap",textures[0].Get());
66 values.Texture("ColorMap2",textures[1].Get());
67 D3D11_TEXTURE3D_DESC vd{};vd.Width=vd.Height=vd.Depth=vd.MipLevels=1;
68 vd.Format=DXGI_FORMAT_R8G8B8A8_UNORM;vd.BindFlags=D3D11_BIND_SHADER_RESOURCE;
69 DWORD black=0;D3D11_SUBRESOURCE_DATA vi{&black,4,4};
70 ComPtr<ID3D11Texture3D> volume;ComPtr<ID3D11ShaderResourceView> volumeView;
71 NativeUiTest11::HR(device.Native()->CreateTexture3D(&vd,&vi,&volume));
72 NativeUiTest11::HR(device.Native()->CreateShaderResourceView(volume.Get(),nullptr,&volumeView));
73 values.Texture("ColorMap3Volume",volumeView.Get());
74 struct SceneVertex {XMFLOAT3 position,normal;
DWORD diffuse,specular;XMFLOAT2 uv,uv2;};
75 const SceneVertex vertices[]={
76 {{-.03f,.03f,0},{0,0,1},0xffffffff,0,{0,0},{0,0}},{{.03f,.03f,0},{0,0,1},0xffffffff,0,{1,0},{1,0}},
77 {{-.03f,-.03f,0},{0,0,1},0xffffffff,0,{0,1},{0,1}},{{-.03f,-.03f,0},{0,0,1},0xffffffff,0,{0,1},{0,1}},
78 {{.03f,.03f,0},{0,0,1},0xffffffff,0,{1,0},{1,0}},{{.03f,-.03f,0},{0,0,1},0xffffffff,0,{1,1},{1,1}}};
79 Geometry11 mesh;NativeUiTest11::HR(mesh.Create(device.Native(),sizeof(vertices),false));
80 void* dest=nullptr;NativeUiTest11::HR(mesh.Lock(0,sizeof(vertices),&dest));
81 memcpy(dest,vertices,sizeof(vertices));NativeUiTest11::HR(mesh.Unlock());
82 VertexLayout11 layout;layout.stride=sizeof(SceneVertex);layout.diffuse=24;layout.specular=28;layout.uv[0]=32;layout.uv[1]=40;
83 const auto draw=[&](
unsigned pass,
const Selection& selected,
float surfaceZ=0) {
84 XMStoreFloat4x4(&world,XMMatrixTranslation(0,0,surfaceZ));values.Set("matWorld",world);
85 values.Set("ShipSurfaceLightCount",int(selected.count));
86 values.Set("ShipSurfaceLightPosition",selected.positions);values.Set("ShipSurfaceLightDirection",selected.directions);
87 values.Set("ShipSurfaceLightColor",selected.colors);
88 draws.BindScene();device.Context()->ClearRenderTargetView(fixture.host.Device().SceneTarget(),std::array<float,4>{}.data());
89 draws.Pipeline().raster.CullMode=D3D11_CULL_NONE;draws.Pipeline().depth.DepthEnable=FALSE;
90 draws.Pipeline().blend.RenderTarget[0].BlendEnable=FALSE;
91 auto actual=layout;if(pass!=0)actual.diffuse=actual.specular=VertexLayout11::Absent;
92 if(pass==11)actual.normal=12;
93 draws.Material(pass);NativeUiTest11::HR(draws.Draw(mesh,actual,Primitive11::Triangles,0,2));
94 };
95 const auto energy=[](const std::vector<DWORD>& pixels) {
96 uint64_t total=0;for(auto p:pixels)total+=(p&255)+((p>>8)&255)+((p>>16)&255);return total;
97 };
98 const auto red=
Select(nav,box(0,0,0,.03f),identity),burst=
Select(flash,box(0,0,0,.03f),identity);
99 for(unsigned pass:{0u,2u,11u})
100 {
101 draw(pass,{});const auto off=fixture.Pixels();Check(energy(off)==0,"unlit terrain/building/paving stays dark");
102 draw(pass,red);const auto lit=fixture.Pixels();
103 Check(energy(lit)>0 && ((lit[128*256+128]>>16)&255)>80,"active nav light illuminates nearby scene geometry");
104 Check((lit[0]&0xffffff)==0 && (lit[128*256+128]&0xffff)==0,"scene illumination retains local range and authored color");
105 fixture.Save("outputs/ship-surface-lighting/scene-nav-"+std::to_string(pass)+".png");
106 draw(pass,burst);Check(energy(fixture.Pixels())>energy(lit)*2,"scene surfaces flash with the active strobe phase");
107 const auto flashPixels=fixture.Pixels();
108 fixture.Save("outputs/ship-surface-lighting/scene-strobe-"+std::to_string(pass)+".png");
109 auto turned=burst;
110 for(auto& axis:turned.directions){axis.x=-axis.x;axis.y=-axis.y;axis.z=-axis.z;}
111 draw(pass,turned);Check(fixture.Pixels()==flashPixels,"scene bulbs and strobes emit equally in every direction");
112 draw(pass,red);Check(fixture.Pixels()==lit,"strobe off phase immediately restores the nav-only scene");
113 draw(pass,burst,.07f);Check(energy(fixture.Pixels())==0,"finite range prevents distant scene illumination");
114 values.Set("ShipSurfaceEye",XMFLOAT4(0,0,-1,0));draw(pass,burst);
115 Check(energy(fixture.Pixels())==0,"reverse-facing scene surfaces reject direct illumination");
116 values.Set("ShipSurfaceEye",XMFLOAT4(0,0,1,0));
117 draw(pass,{});Check(fixture.Pixels()==off,"clearing the scene selection removes stale lights");
118 values.Texture("ColorMap",textures[2].Get());values.Set("ambient",XMFLOAT4(1.2f,1.2f,1.2f,100000));
119 draw(pass,{});const auto bright=fixture.Pixels();draw(pass,red);
120 Check(fixture.Pixels()==bright,"activating a lamp never dims existing bright illumination on a shared scene batch");
121 values.Texture("ColorMap",textures[0].Get());values.Set("ambient",XMFLOAT4(0,0,0,100000));
122 std::cout<<"PASS scene lighting material "<<pass<<": phase, range, color, face direction and reset\n";
123 }
124 if(benchmark)
125 {
126 std::array<GeometryBounds,512> batches;
127 for(unsigned i=0;i<batches.size();++i)batches[i]=box(float(i%32)*.1f,float(i/32)*.1f,0,.03f);
128 volatile uint32_t selectedMask=0;
129 const auto cpuStart=std::chrono::steady_clock::now();
130 for(unsigned frame=0;frame<1000;++frame)
131 {
132 identity._41=float(frame%16)*.0001f;
133 for(
const auto& bounds:batches)selectedMask^=
Select(crowded,bounds,identity).
mask;
134 }
135 const auto cpuMs=std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-cpuStart).count()/1000;
136 std::cout<<"CPU selection 512 scene batches / 32 candidate lights: "<<cpuMs<<" ms/frame (checksum "<<selectedMask<<")\n";
137 std::cout<<fixture.host.Device().Status()<<'\n';
138 ComPtr<ID3D11Query> disjoint,start,end;
139 D3D11_QUERY_DESC q{D3D11_QUERY_TIMESTAMP_DISJOINT,0};NativeUiTest11::HR(device.Native()->CreateQuery(&q,&disjoint));
140 q.Query=D3D11_QUERY_TIMESTAMP;NativeUiTest11::HR(device.Native()->CreateQuery(&q,&start));NativeUiTest11::HR(device.Native()->CreateQuery(&q,&end));
141 const auto time=[&](
unsigned pass,
const Selection& lights) {
142 draw(pass,lights);auto* context=device.Context();for(unsigned i=0;i<20;++i)context->Draw(6,0);
143 context->Begin(disjoint.Get());context->End(start.Get());for(unsigned i=0;i<100;++i)context->Draw(6,0);
144 context->End(end.Get());context->End(disjoint.Get());context->Flush();
145 D3D11_QUERY_DATA_TIMESTAMP_DISJOINT f{};UINT64 a=0,b=0;
146 while(context->GetData(disjoint.Get(),&f,sizeof(f),0)==S_FALSE)Sleep(1);
147 while(context->GetData(start.Get(),&a,sizeof(a),0)==S_FALSE)Sleep(1);
148 while(context->GetData(end.Get(),&b,sizeof(b),0)==S_FALSE)Sleep(1);
149 Check(!f.Disjoint,"scene-light GPU timestamps are valid");return 1000.*double(b-a)/f.Frequency/100;
150 };
151 for(unsigned samples:{1u,4u})
152 {
153 if(!fixture.host.Device().SupportsSamples(samples))continue;
154 const unsigned width=samples==1 ? 1920 : 3440,height=samples==1 ? 1080 : 1440;
155 NativeUiTest11::HR(fixture.host.SetSamples(samples));fixture.Resize(width,height);
156 for(unsigned pass:{0u,2u,11u})
157 {
158 std::array<double,5> off{},on{};
159 for(unsigned i=0;i<5;++i){off[i]=time(pass,{});on[i]=time(pass,chosen);}
160 std::sort(off.begin(),off.end());std::sort(on.begin(),on.end());
161 std::cout<<"GPU "<<width<<'x'<<height<<' '<<samples<<"x MSAA material "<<pass
162 <<" median ms/draw: off="<<off[2]<<", four lights="<<on[2]<<", delta="<<on[2]-off[2]<<'\n';
163 }
164 }
165 }
166}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
Matrix * MatrixRotationY(Matrix *out, float value)
Matrix * MatrixIdentity(Matrix *out)
Matrix * MatrixTranslation(Matrix *out, float x, float y, float z)
Frame Build(const Effects &effects, const RiseMath::Matrix &modelToWorld, unsigned long phase, float power, bool enabled)
constexpr unsigned MaxSceneLights
Selection Select(const Frame &frame, const GeometryBounds &local, const RiseMath::Matrix &world)
void Include(const Vector &p)
std::array< RiseMath::Vector4, MaxLights > colors
std::array< RiseMath::Vector4, MaxLights > positions
std::array< RiseMath::Vector4, MaxLights > directions