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

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

1// Included by CloudScene11Tests.cpp after its scene/readback helpers.
2void TestClearance(Device11& gpu,CloudRenderer11& renderer,bool organic=false)
3{
4 const char probeSource[]=R"(
5#include "CloudVolume.hlsl"
6float4 CheckClearance(float4 pixel:SV_Position):SV_Target
7{
8 uint index=uint(pixel.y)*160+uint(pixel.x);
9 float vertical=1-2*(index+.5)/19200;
10 float longitude=index*2.39996323;
11 float horizontal=sqrt(max(0,1-vertical*vertical));
12 float3 direction=float3(cos(longitude)*horizontal,vertical,sin(longitude)*horizontal);
13 float distance=riseClearAirRadius*(index%4==0 ? 1 : frac(index*.618033989));
14 float footprint=footprintAt(direction,distance);
15 float3 p=eye.xyz+direction*distance;
16 float d=cloudSample(p,footprint).x;
17 float optimized=RiseCloudDensity(p,footprint);
18 // Float readback preserves the magnitude and location of disagreements.
19 return float4(d,optimized,1,1);
20}
21)";
22 const D3D_SHADER_MACRO macros[]={{"RISE_SCENE_SAMPLES","1"},
23 {organic ? "RISE_CLOUD_EXPERIMENTAL_FLIGHT" : nullptr,"1"},{nullptr,nullptr}};
24 ComPtr<ID3DBlob> code,errors;
25 char sourceDirectory[MAX_PATH]{};
26 GetEnvironmentVariableA("RISE_CLEARANCE_SOURCE_DIRECTORY",sourceDirectory,MAX_PATH);
27 const auto sourcePath=(std::filesystem::path(sourceDirectory[0] ? sourceDirectory : "Rendering/Shaders")/"CloudClearanceProbe.hlsl").string();
28 const auto hr=D3DCompile(probeSource,strlen(probeSource),sourcePath.c_str(),
29 macros,D3D_COMPILE_STANDARD_FILE_INCLUDE,"CheckClearance","ps_5_0",
30 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_WARNINGS_ARE_ERRORS|D3DCOMPILE_PACK_MATRIX_ROW_MAJOR|D3DCOMPILE_OPTIMIZATION_LEVEL3,
31 0,&code,&errors);
32 if(errors)std::cerr<<static_cast<const char*>(errors->GetBufferPointer());HR(hr,"compile clearance probe");
33 auto vertex=Compile("Fullscreen","vs_5_0",1);Program11 probe;
34 HR(probe.Create(gpu.Device(),vertex->GetBufferPointer(),vertex->GetBufferSize(),code->GetBufferPointer(),code->GetBufferSize()),"clearance probe program");
35 Pipeline pipeline(gpu.Device(),1);Scene scene(gpu.Device(),1,Width,Height,DXGI_FORMAT_R32G32B32A32_FLOAT);
36 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};
37 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&view,XMMatrixIdentity());
38 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,384));
39 unsigned accepted=0,rejected=0,mismatches=0;float maxDifference=0;auto* context=gpu.Context();
40 for(unsigned weatherCase=0;weatherCase<8;++weatherCase)
41 {
42 auto map=std::make_shared<CloudWeather::Map>();
43 const unsigned width=weatherCase==4 || weatherCase==5 ? 2048u : 64u,height=width/2;
44 map->mips.push_back({width,height,std::vector<uint8_t>(width*height)});
45 for(unsigned y=0;y<height;++y)for(unsigned x=0;x<width;++x)
46 map->mips[0].pixels[y*width+x]=weatherCase==0 ? 0 : weatherCase==1 ? 153 : weatherCase==2 ? 255
47 : weatherCase==6 ? 64 : weatherCase==7 ? 127
48 : weatherCase==5 ? uint8_t(((x/3+y/3)%2) ? 255 : 64) : uint8_t((x*17+y*23)%256);
50 renderer.UpdateWeather(map,60);
51 for(unsigned i=0;i<100 && renderer.WeatherFrame().current!=map;++i)
52 HR(renderer.PrepareResources(context),"clearance weather upload");
53 Check(renderer.WeatherFrame().current==map,"complete clearance weather upload");
54 // Include both transition textures, then the fully committed map.
55 renderer.UpdateWeather(map,weatherCase==3 || weatherCase==5 ? 30 : 60);
56 for(unsigned position=0;position<80;++position)
57 {
58 CloudVolume::Parameters cloud;cloud.organic=organic;cloud.weather=renderer.WeatherFrame();
59 if(position>=32)cloud.base=position<48 ? 0.f : .05f;
60 if(position>=64)cloud.base=.45f;
61 cloud.noiseOffset={position%2 ? 3199.99f : -3199.99f,float(position)*37.1f,-float(position)*113.3f};
62 const float longitude=position==16 ? 3.14159f : position==17 ? -3.14159f : float(position)*1.31f;
63 const float latitude=position==15 || position==30 ? 1.5707f : position==31 ? -1.5707f : float(int(position%7)-3)*.35f;
64 const CloudVolume::Point unit{std::sin(longitude)*std::cos(latitude),std::cos(longitude)*std::cos(latitude),std::sin(latitude)};
65 const float top=CloudVolume::Boundaries(CloudVolume::Scale(unit,cloud.radius),cloud,renderer.Seed()).top;
66 // Low ceilings must not reuse the old sea-level clear-air floor.
67 // Sweep below/through that boundary with the original GPU density.
68 const float altitude=position>=64 ? .30f+float(position%16)*.05f
69 : position>=32 ? -.25f+float(position%16)*.025f
70 : position%4==0 ? cloud.base-.4f : position%4==1 ? top-.1f : top+(position%4==2 ? .3f : 3.f);
71 const auto eye=CloudVolume::Scale(unit,cloud.radius+altitude);
72 scene.Clear(context);
73 HR(renderer.Render(context,scene.target.Get(),scene.depth,viewport,view,projection,{eye.x,eye.y,eye.z},cloud,
74 {0,-1,0},{1,1,1,1},{.3f,.4f,.5f,1}),"clearance test setup");
75 const bool clear=renderer.ClearAirRadius()>0;
76 if(clear)++accepted;else ++rejected;
77 auto probeParameters=renderer.Parameters();
78 // Check the cheaper density against the original even in occupied
79 // air, including either side of its near-base specialization.
80 if(!clear)probeParameters.Set("riseClearAirRadius",.5f);
81 context->ClearState();auto* target=scene.target.Get();
82 context->OMSetRenderTargets(1,&target,nullptr);context->RSSetViewports(1,&viewport);context->RSSetState(pipeline.raster.Get());
83 context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
84 HR(probe.Bind(context,probeParameters),"bind clearance probe");context->Draw(3,0);
85 context->OMSetRenderTargets(0,nullptr,nullptr);
86 const auto pixels=pipeline.Pixels(gpu,scene,1);
87 unsigned index=0;
88 for(const auto& pixel:pixels)
89 {
90 Check(pixel.color[2]==1,"clearance probe executed");
91 if(clear)Check(pixel.color[0]==0,"CPU clear-air bound contained nonzero GPU cloud density");
92 const float difference=std::abs(pixel.color[0]-pixel.color[1]);
93 maxDifference=(std::max)(maxDifference,difference);
94 if(difference>.00002f)
95 {
96 if(mismatches<8)std::cout<<"Density mismatch weather "<<weatherCase<<" position "<<position
97 <<" pixel "<<index<<" full "<<pixel.color[0]<<" shortcut "<<pixel.color[1]<<"\n";
98 ++mismatches;
99 }
100 ++index;
101 }
102 }
103 }
104 std::cout<<"Density shortcut comparison: "<<mismatches<<" mismatches, maximum error "<<maxDifference<<"\n";
105 Check(mismatches==0,"optimized density differs from original cloud density");
106 Check(accepted>=24,"clearance proof must exercise clear-air optimization");
107 Check(rejected>=8,"clearance proof must retain cloud-boundary fallback");
108 DebugMessages(gpu);
109 std::cout<<"PASS: camera clearance, "<<accepted<<" proven regions / "<<rejected<<" fallbacks, "
110 <<accepted*Width*Height<<" clear-air probes, "<<(accepted+rejected)*Width*Height
111 <<" matching original/optimized density probes\n";
112}
Point Scale(Point p, float s)
Definition CloudVolume.h:93
Column Boundaries(Point p, const Parameters &params, uint32_t seed=DefaultSeed)
void BuildMips(Map &map)
constexpr float Width
constexpr float Height
CloudWeather::Frame weather
Definition CloudVolume.h:35