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

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

1// Compare original MSAA samples on the GPU: a CPU readback of two full-size
2// float Sample arrays would put unnecessary pressure on the Win32 address space.
3namespace
4{
5 void SaveFullCloudImage(Device11& gpu,const Scene& scene,const std::filesystem::path& path)
6 {
7 D3D11_TEXTURE2D_DESC desc{};scene.color->GetDesc(&desc);
8 desc.SampleDesc={1,0};desc.BindFlags=0;
9 ComPtr<ID3D11Texture2D> resolved,staging;
10 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&resolved),"full image resolve target");
11 desc.Usage=D3D11_USAGE_STAGING;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
12 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&staging),"full image staging");
13 gpu.Context()->ResolveSubresource(resolved.Get(),0,scene.color.Get(),0,desc.Format);
14 gpu.Context()->CopyResource(staging.Get(),resolved.Get());
15 D3D11_MAPPED_SUBRESOURCE mapped{};HR(gpu.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"full image readback");
16 std::ofstream file(path,std::ios::binary);file<<"P6\n"<<desc.Width<<' '<<desc.Height<<"\n255\n";
17 std::vector<char> row(size_t(desc.Width)*3);
18 for(UINT y=0;y<desc.Height;++y)
19 {
20 const auto* rowBytes=static_cast<const char*>(mapped.pData)+size_t(y)*mapped.RowPitch;
21 for(UINT x=0;x<desc.Width;++x)for(unsigned c=0;c<3;++c)row[size_t(x)*3+c]=rowBytes[size_t(x)*4+c];
22 file.write(row.data(),row.size());
23 }
24 gpu.Context()->Unmap(staging.Get(),0);Check(bool(file),"write full comparison image");
25 }
26
27 void TestCloudFullResolution(Device11& gpu,CloudRenderer11& renderer,CloudRenderer11& before,const std::filesystem::path& output,bool glowBoundaries=false)
28 {
29 before.Pass().SetVisibilityOptimizationEnabled(false);
30 constexpr UINT width=3440,height=1351,samples=4;
31 Check(gpu.SupportsSamples(samples),"full comparison needs 4x MSAA");
32 constexpr char compareSource[]=R"(
33Texture2DMS<float4,4> beforeColor : register(t0);
34Texture2DMS<float4,4> afterColor : register(t1);
35Texture2DMS<float,4> beforeDepth : register(t2);
36Texture2DMS<float,4> afterDepth : register(t3);
37RWStructuredBuffer<uint4> differences : register(u0);
38[numthreads(16,16,1)] void Compare(uint3 id:SV_DispatchThreadID)
39{
40 uint width,height,count;beforeColor.GetDimensions(width,height,count);
41 if(id.x>=width || id.y>=height)return;
42 [unroll] for(uint sample=0;sample<4;++sample)
43 {
44 float4 a=beforeColor.Load(id.xy,sample),b=afterColor.Load(id.xy,sample);
45 if(any(a!=b))
46 {
47 InterlockedAdd(differences[0].x,1);
48 float4 delta=abs(a-b);
49 InterlockedMax(differences[0].z,asuint(max(max(delta.x,delta.y),max(delta.z,delta.w))));
50 }
51 if(beforeDepth.Load(id.xy,sample)!=afterDepth.Load(id.xy,sample))InterlockedAdd(differences[0].y,1);
52 }
53})";
54 ComPtr<ID3DBlob> code,errors;ComPtr<ID3D11ComputeShader> compare;
55 HR(D3DCompile(compareSource,strlen(compareSource),"CloudFullResolution",nullptr,nullptr,"Compare","cs_5_0",
56 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&code,&errors),"full comparison shader");
57 HR(gpu.Device()->CreateComputeShader(code->GetBufferPointer(),code->GetBufferSize(),nullptr,&compare),"full comparison CS");
58 D3D11_BUFFER_DESC buffer{};buffer.ByteWidth=16;buffer.BindFlags=D3D11_BIND_UNORDERED_ACCESS;
59 buffer.MiscFlags=D3D11_RESOURCE_MISC_BUFFER_STRUCTURED;buffer.StructureByteStride=16;
60 ComPtr<ID3D11Buffer> counts,readback;ComPtr<ID3D11UnorderedAccessView> countsView;
61 HR(gpu.Device()->CreateBuffer(&buffer,nullptr,&counts),"full comparison counters");
62 D3D11_UNORDERED_ACCESS_VIEW_DESC uav{};uav.ViewDimension=D3D11_UAV_DIMENSION_BUFFER;uav.Buffer.NumElements=1;
63 HR(gpu.Device()->CreateUnorderedAccessView(counts.Get(),&uav,&countsView),"full comparison counter UAV");
64 buffer.BindFlags=buffer.MiscFlags=buffer.StructureByteStride=0;buffer.Usage=D3D11_USAGE_STAGING;buffer.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
65 HR(gpu.Device()->CreateBuffer(&buffer,nullptr,&readback),"full comparison counter readback");
66 const auto compareScenes=[&](Scene& a,Scene& b,UINT w,UINT h)
67 {
68 auto* context=gpu.Context();context->ClearState();const UINT zero[4]{};context->ClearUnorderedAccessViewUint(countsView.Get(),zero);
69 ID3D11ShaderResourceView* resources[]={a.view.Get(),b.view.Get(),a.depth.ShaderView(),b.depth.ShaderView()};
70 auto* u=countsView.Get();context->CSSetShader(compare.Get(),nullptr,0);context->CSSetShaderResources(0,4,resources);context->CSSetUnorderedAccessViews(0,1,&u,nullptr);
71 context->Dispatch((w+15)/16,(h+15)/16,1);context->ClearState();context->CopyResource(readback.Get(),counts.Get());
72 D3D11_MAPPED_SUBRESOURCE mapped{};HR(context->Map(readback.Get(),0,D3D11_MAP_READ,0,&mapped),"full comparison counts");
73 const auto* values=static_cast<const UINT*>(mapped.pData);const std::array<UINT,2> result{values[0],values[1]};context->Unmap(readback.Get(),0);
74 return result;
75 };
76 {
77 Scene a(gpu.Device(),samples,1,1),b(gpu.Device(),samples,1,1);a.Clear(gpu.Context());b.Clear(gpu.Context());
78 const float pink[]={1,0,1,1};gpu.Context()->ClearRenderTargetView(b.target.Get(),pink);
79 gpu.Context()->ClearDepthStencilView(b.depth.WriteView(),D3D11_CLEAR_DEPTH,.5f,0);
80 const auto different=compareScenes(a,b,1,1);
81 Check(different[0]==samples && different[1]==samples,"GPU comparator detects every deliberately changed sample");
82 b.Clear(gpu.Context());const auto equal=compareScenes(a,b,1,1);
83 Check(equal[0]==0 && equal[1]==0,"GPU comparator accepts identical samples");
84 }
85 if(!output.empty())std::filesystem::create_directories(output);
86 RainMsaaFixture fixture(gpu.Device(),samples);Scene reference(gpu.Device(),samples,width,height),scene(gpu.Device(),samples,width,height);
87 const D3D11_VIEWPORT viewport{0,0,float(width),float(height),0,1};
88 XMFLOAT4X4 projection;XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(width)/height,.001f,384));
89 auto grid=std::make_unique<CloudRenderer11::RainGrid>();grid->cube=0;grid->u=grid->v=1019;grid->revision=1;
90 grid->cells.fill({0,2.668316f,27.72f,2});
91 // Check the exact zero-contribution bound on both sides, including
92 // amplified airglow and signed inputs that must retain the reference.
93 for(unsigned glowCase=0;glowCase<(glowBoundaries ? 7u : 1u);++glowCase)
94 for(float visibility:{27.72f,.1f,1.f,5.f})for(bool organic:{false,true})for(unsigned scenario=0;scenario<(organic ? 11u : 7u);++scenario)
95 {
96 if(!organic && visibility!=27.72f)continue;
97 if(glowCase && (!organic || scenario<6 || scenario>8))continue;
98 // Exercise both the occupied orbital view and the empty sky view:
99 // the latter exposed a driver/composite regression in a live capture.
100 grid->revision++; grid->cells.fill({0,2.668316f,visibility,organic ? 16.f : 2.f});
101 const bool orbital=organic && (scenario==4 || scenario==5);
102 const bool local=scenario>=(organic ? 6u : 4u) && scenario<=8;
103 const bool localNight=scenario==(organic ? 7u : 5u);
104 const float altitude=orbital ? 936.93f : local ? 2.074707f : scenario==2 ? 3.2f : scenario==3 ? 15.f : 2.074707f;
105 const float pitch=orbital ? (scenario==4 ? -100.f : 100.f) : scenario==3 ? -.4f : scenario==1 || local ? .2f : 0;
106 XMFLOAT4X4 view;XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,pitch,-1,0),XMVectorSet(0,1,0,0)));
107 CloudVolume::Parameters cloud;cloud.base=2.668316f;cloud.top=11.132220f;cloud.organic=organic;cloud.weather=renderer.WeatherFrame();
108 if(local)
109 {
110 auto regional=std::make_shared<CloudBaseWeather::Grid>();regional->cube=0;regional->u=regional->v=1019;
111 regional->cells.fill({cloud.base,.6f,1,0});
112 for(unsigned y=0;y<10;++y)for(unsigned x=0;x<10;++x)regional->cells[y*64+x].base=.4f;
113 regional->Bounds();cloud.localBase={regional,1};
114 }
115 for(unsigned version=0;version<2;++version)
116 {
117 auto& clouds=version ? renderer : before;auto& target=version ? scene : reference;auto* context=gpu.Context();
118 if(scenario==1 || local || scenario==10)fixture.Draw(gpu,target,viewport,{.9997f,.00024f,0,0},true);
119 else {context->ClearState();target.Clear(context);}
120 // A cockpit band is deliberately stencil-only, separate from
121 // world depth, as in the actual renderer.
122 auto* rtv=target.target.Get();context->OMSetRenderTargets(1,&rtv,target.depth.WriteView());
123 if(scenario<9)
124 {
125 const D3D11_VIEWPORT cockpit{0,height*.78f,float(width),height*.22f,0,1};context->RSSetViewports(1,&cockpit);
126 context->RSSetState(fixture.pipeline.raster.Get());context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
127 context->VSSetShader(fixture.pipeline.full.Get(),nullptr,0);context->PSSetShader(fixture.pipeline.blue.Get(),nullptr,0);
128 context->OMSetDepthStencilState(fixture.pipeline.stencil.Get(),3);context->Draw(3,0);context->OMSetRenderTargets(0,nullptr,nullptr);
129 }
130 clouds.ResetCloudHistory();clouds.SetRainGrid(grid.get());clouds.SetLightningEnabled(scenario==1 || localNight);
131 clouds.SetLocalLightning({0,cloud.radius+9,-1},scenario==1 || localNight ? CloudRenderer11::Color(2,2,3,1) : CloudRenderer11::Color(),2,{0,cloud.radius,-1});
132 std::vector<CloudRenderer11::Headlight> lights;
133 if(scenario==(organic ? 8u : 6u))lights.push_back({{0,cloud.radius+altitude,0},{0,.4472136f,-.8944272f},{4,4,4,1},4,{0,.8944272f,.4472136f}});
134 clouds.SetHeadlights(lights);
135 const bool night=glowCase || scenario==2 || localNight;
136 XMFLOAT4 glow(.125f,.375f,.25f,1),nvg(1,1,1,0);
137 const float scale=glowCase==1 ? .1f : glowCase==2 ? .99998f : glowCase==3 ? .999995f : glowCase==4 ? 1.001f : .25f;
138 if(glowCase)glow={.3f*scale,.4f*scale,.5f*scale,1};
139 if(glowCase==5)nvg={2,1.5f,3,1};
140 if(glowCase==6){glow={-.01f,.02f,-.03f,1};nvg={-1,2,3,1};}
141 HR(clouds.Render(context,target.target.Get(),target.depth,viewport,view,projection,{0,cloud.radius+altitude,0},cloud,
142 {0,night ? 1.f : -1.f,0},night ? XMFLOAT4(0,0,0,1) : XMFLOAT4(1,1,1,1),
143 {.3f,.4f,.5f,1},glow,{.3f,.4f,.5f,42.16f},{},nvg),"full comparison render");
144 }
145 const auto different=compareScenes(reference,scene,width,height);const UINT colors=different[0],depths=different[1];
146 std::cout<<"Full resolution visibility="<<visibility<<" organic="<<organic<<" scene="<<scenario<<": "<<width<<'x'<<height<<" 4x, "
147 <<uint64_t(width)*height*samples<<" original samples; glow case="<<glowCase<<"; color mismatches="<<colors<<", depth mismatches="<<depths<<std::endl;
148 Check(colors==0 && depths==0,"full resolution original color/depth changed");
149 if(!output.empty())
150 {
151 const std::string prefix=(glowCase ? "glow"+std::to_string(glowCase)+"-" : "")+(organic ? "organic" : "legacy")+std::string("-visibility")+std::to_string(visibility)+"-full-scene"+std::to_string(scenario);
152 SaveFullCloudImage(gpu,reference,output/(prefix+"-before.ppm"));SaveFullCloudImage(gpu,scene,output/(prefix+"-after.ppm"));
153 }
154 }
155 renderer.SetRainGrid(nullptr);before.SetRainGrid(nullptr);renderer.SetHeadlights({});before.SetHeadlights({});DebugMessages(gpu);
156 std::cout<<"PASS: full-resolution original MSAA sample comparison\n";
157 }
158}
Definition grid.h:14
char cube
Definition grid.h:45
uint32_t Color(Echo echo)
CloudWeather::Frame weather
Definition CloudVolume.h:35
CloudBaseWeather::Frame localBase
Definition CloudVolume.h:36