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

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

1static void TestAtmosphericVehicleLighting()
2{
3 using namespace CloudVolume;
4 constexpr float scaleHeight=7.5f,deckTop=10.f;
5 const Point cloud{.48f,.50f,.53f},zenith{154.f/255,160.f/255,1};
6 const auto fill=[&](float altitude,float aboveTop,Point sky) {
7 return VehicleAmbient(cloud,Scale(sky,AtmosphereWeight(altitude,scaleHeight)),aboveTop);
8 };
9 const auto below=fill(3,-7,zenith),inside=fill(9,-1,zenith);
10 Check(below.x==cloud.x && below.y==cloud.y && below.z==cloud.z
11 && inside.x==cloud.x && inside.y==cloud.y && inside.z==cloud.z,
12 "sky fill preserves existing in/below-cloud illumination");
13 const auto high=fill(18,8,zenith);
14 Check(high.x>.20f && high.y>.20f && high.z>.35f && high.z>high.x,
15 "18 km shaded hull retains substantial blue sky fill above the cloud cutoff");
16 // An unlit-facing surface in the real vehicle shader takes the strongest
17 // ambient/lamp channel. A representative nav/strobe wash should no longer
18 // dominate it in daytime; the same lights remain visible in the dark.
19 const float strobe=.20f*ShiplightScatterWeight(18,deckTop,scaleHeight,ClearVisibilityKm,0);
20 Check(strobe>0 && strobe<high.x && strobe<high.y && strobe<high.z,
21 "18 km daytime sky dominates broad strobe spill on shaded surfaces");
22 const auto dark=fill(18,8,{0,0,0});
23 Check(dark.x==0 && dark.y==0 && dark.z==0 && strobe>dark.x,
24 "night does not receive an artificial daylight floor");
25 for(float top:{10.f,18.f,22.f})
26 for(float offset:{-.5f,0.f,1.f})
27 {
28 const auto a=fill(top+offset-.001f,offset-.001f,zenith);
29 const auto b=fill(top+offset+.001f,offset+.001f,zenith);
30 Check(std::abs(a.x-b.x)<.002f && std::abs(a.y-b.y)<.002f && std::abs(a.z-b.z)<.002f,
31 "ambient stays continuous across low and high cloud tops");
32 }
33 float previousSky=1,previousGlow=1;
34 for(float altitude=0;altitude<=100;altitude+=.1f)
35 {
36 const float sky=AtmosphereWeight(altitude,scaleHeight);
37 const float glow=ShiplightScatterWeight(altitude,deckTop,scaleHeight,42.16f,0);
38 Check(std::isfinite(sky) && sky>=0 && sky<=previousSky
39 && std::isfinite(glow) && glow>=0 && glow<=previousGlow,
40 "sky fill and shiplight scattering fade monotonically toward space");
41 if(altitude<=deckTop)Check(glow<.003f,"clear ground air cannot restore the broad shiplight wash");
42 previousSky=sky;previousGlow=glow;
43 }
44 for(float altitude:{45.f,100.f,3000.f})
45 {
46 const auto space=fill(altitude,altitude-deckTop,zenith);
47 Check(space.x==0 && space.y==0 && space.z==0
48 && ShiplightScatterWeight(altitude,deckTop,scaleHeight,.01f,1)==0,
49 "vacuum has neither atmospheric ambient nor broad shiplight glow");
50 }
51 const auto scatter=[&](float visibility,float opacity=0.f) {
52 return ShiplightScatterWeight(0,deckTop,scaleHeight,visibility,opacity);
53 };
54 Check(scatter(42.16f)<.003f && scatter(10)<.011f && scatter(2)<.051f,
55 "clear air and good visibility produce negligible broad reflection");
56 Check(scatter(.5f)>.19f && scatter(.1f)>.99f,
57 "strong atmospheric wash is reserved for low visibility");
58 float previousScatter=0;
59 for(float visibility:{168.64f,42.16f,10.f,5.f,2.f,1.f,.5f,.1f,0.f})
60 {
61 const float value=scatter(visibility);
62 Check(std::isfinite(value) && value>=previousScatter && value<=1,
63 "decreasing visibility increases scattering smoothly and remains bounded");
64 previousScatter=value;
65 }
66 Parameters params;
67 const auto ground=SampleMedium({0,params.radius,0},params);
68 const auto mist=SampleMedium({0,params.radius+5,0},params);
69 const auto opacity=[](const Medium& medium) {return 1.f-std::exp(-medium.extinction*ShiplightScatterDistanceKm);};
70 Check(scatter(42.16f,opacity(ground))<.003f && scatter(42.16f,opacity(mist))>.25f,
71 "actual local cloud droplets produce scattering while clear air below the deck does not");
72 std::cout<<"PASS: clear-ground shiplight factor "<<scatter(42.16f)<<"; 100 m fog "<<scatter(.1f)<<'\n';
73 std::cout<<"PASS: atmospheric vehicle lighting; 18 km ambient "<<high.x<<", "<<high.y<<", "<<high.z
74 <<"; 3000 km ambient and shiplight scatter both zero\n";
75}
76
77// Regression for encoded-RGB cloud attenuation, including the production HLSL.
78// Task: codex://threads/01a103d2-137e-7433-836e-da1d78903c9c
79static void TestCloudDiffuseLighting(Device11& gpu)
80{
81 constexpr unsigned columns=65,rows=33,count=columns*rows;
82 const auto shaderPath=std::filesystem::absolute("Rendering/Shaders/CloudVolume.hlsl");
83 std::ifstream input(shaderPath,std::ios::binary);
84 Check(bool(input),"open production cloud shader for lighting regression");
85 std::string shader((std::istreambuf_iterator<char>(input)),std::istreambuf_iterator<char>());
86 shader+=R"(
87RWStructuredBuffer<float> lightingAudit : register(u0);
88[numthreads(8,8,1)] void CloudLightingAudit(uint3 id : SV_DispatchThreadID)
89{
90 if(id.x>=65 || id.y>=33)return;
91 lightingAudit[id.y*65+id.x]=diffuseSunDisplayAttenuation(id.x/64.0,(int(id.y)-2)/30.0);
92}
93)";
94 ComPtr<ID3DBlob> code,errors;
95 const HRESULT compiled=D3DCompile(shader.data(),shader.size(),shaderPath.string().c_str(),nullptr,
96 D3D_COMPILE_STANDARD_FILE_INCLUDE,"CloudLightingAudit","cs_5_0",
97 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_WARNINGS_ARE_ERRORS|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&code,&errors);
98 if(errors)std::cerr<<static_cast<const char*>(errors->GetBufferPointer());
99 HR(compiled,"compile production cloud attenuation probe");
100 ComPtr<ID3D11ComputeShader> compute;
101 HR(gpu.Device()->CreateComputeShader(code->GetBufferPointer(),code->GetBufferSize(),nullptr,&compute),"lighting compute shader");
102 D3D11_BUFFER_DESC desc{};desc.ByteWidth=count*sizeof(float);desc.Usage=D3D11_USAGE_DEFAULT;
103 desc.BindFlags=D3D11_BIND_UNORDERED_ACCESS;desc.MiscFlags=D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;
104 desc.StructureByteStride=sizeof(float);
105 ComPtr<ID3D11Buffer> result,staging;
106 HR(gpu.Device()->CreateBuffer(&desc,nullptr,&result),"lighting result buffer");
107 D3D11_UNORDERED_ACCESS_VIEW_DESC view{};view.ViewDimension=D3D11_UAV_DIMENSION_BUFFER;view.Buffer.NumElements=count;
108 ComPtr<ID3D11UnorderedAccessView> unordered;
109 HR(gpu.Device()->CreateUnorderedAccessView(result.Get(),&view,&unordered),"lighting result UAV");
110 desc.Usage=D3D11_USAGE_STAGING;desc.BindFlags=desc.MiscFlags=desc.StructureByteStride=0;
111 desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
112 HR(gpu.Device()->CreateBuffer(&desc,nullptr,&staging),"lighting staging buffer");
113 gpu.Context()->ClearState();
114 auto* output=unordered.Get();gpu.Context()->CSSetUnorderedAccessViews(0,1,&output,nullptr);
115 gpu.Context()->CSSetShader(compute.Get(),nullptr,0);
116 gpu.Context()->Dispatch((columns+7)/8,(rows+7)/8,1);
117 gpu.Context()->ClearState();gpu.Context()->CopyResource(staging.Get(),result.Get());
118 D3D11_MAPPED_SUBRESOURCE mapped{};HR(gpu.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"read lighting probe");
119 const auto* data=static_cast<const float*>(mapped.pData);
120 const std::vector<float> actual(data,data+count);gpu.Context()->Unmap(staging.Get(),0);
121 float maximumError=0;
122 for(unsigned y=0;y<rows;++y)
123 {
124 const float sun=(int(y)-2)/30.f;
125 float previous=0;
126 for(unsigned x=0;x<columns;++x)
127 {
128 const float transmission=x/64.f,linear=.06f+.94f*transmission;
129 const float cpu=CloudVolume::DiffuseSunDisplayAttenuation(transmission,sun);
130 maximumError=(std::max)(maximumError,std::abs(cpu-actual[y*columns+x]));
131 Check(std::isfinite(cpu) && cpu>=linear-1e-6f && cpu<=1.000001f,"cloud display attenuation stays bounded");
132 Check(cpu>=previous,"more transmitted sunlight cannot darken the scene");previous=cpu;
133 if(sun<=.05f)Check(std::abs(cpu-linear)<1e-6f,"twilight and night retain legacy attenuation");
134 if(sun>=.25f)Check(std::abs(std::pow(cpu,2.2f)-linear)<2e-6f,"daytime display retains intended light ratio");
135 if(x==64)Check(std::abs(cpu-1)<1e-6f,"exposed cloud tops keep incident lighting");
136 }
137 }
138 Check(maximumError<.00002f,"CPU scene and GPU cloud attenuation agree");
139 const auto dark=CloudVolume::ScatteredSunlight({.94f,.96f,1},0,0);
140 Check(dark.x==0 && dark.y==0 && dark.z==0,"attenuation correction creates no light without sunlight");
141 const auto noon=CloudVolume::ScatteredSunlight({.94f,.96f,1},1,0);
142 Check(noon.z>.278f && noon.z<.279f && std::abs(noon.x/noon.z-.94f)<1e-6f,
143 "dense daytime cloud floor preserves color and avoids crushed RGB");
144 // Exercise the real density column, not just scalar transfer inputs.
145 CloudVolume::Parameters params;params.base=.49f;
146 const CloudVolume::Point ground{0,params.radius,0},up{0,1,0};
147 const float groundTransmission=CloudVolume::DiffuseSunTransmission(ground,up,params);
148 const auto groundLight=CloudVolume::ScatteredSunlight({1,1,1},1,groundTransmission);
149 Check(groundLight.x>.47f && groundLight.x<.54f,"490 m calm ceiling retains readable daytime illumination");
150 Check(CloudVolume::Transmittance(ground,{0,params.radius+50,0},params)<.005f,
151 "brighter diffuse daylight does not restore the direct sun through cloud");
152 std::cout<<"PASS: "<<count<<" CPU/GPU diffuse-light cases, max error "<<maximumError
153 <<"; Deois calm-cloud factor "<<groundLight.x<<'\n';
154}
SCRIPT_CONTROL SCRIPT_STATE const void int
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
float DiffuseSunDisplayAttenuation(float transmission, float sunCosine)
Point Scale(Point p, float s)
Definition CloudVolume.h:93
float AtmosphereWeight(float altitude, float scaleHeight)
Definition CloudVolume.h:65
Point VehicleAmbient(Point cloudLight, Point skyLight, float altitudeAboveTop)
Definition CloudVolume.h:84
constexpr float ShiplightScatterDistanceKm
Definition CloudVolume.h:70
constexpr float ClearVisibilityKm
float Transmittance(Point from, Point to, const Parameters &params, uint32_t seed=DefaultSeed)
float DiffuseSunTransmission(Point p, Point towardSun, const Parameters &params, uint32_t seed=DefaultSeed)
float ShiplightScatterWeight(float altitude, float deckTop, float scaleHeight, float visibilityKm, float localCloudOpacity)
Definition CloudVolume.h:71
Medium SampleMedium(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
Point ScatteredSunlight(Point incident, float sunCosine, float transmission)