1
2namespace
3{
4 struct CloudAlphaCounts { unsigned empty=0,partial=0,opaque=0; };
5 CloudAlphaCounts ReadCloudAlpha(Device11& gpu,ID3D11ShaderResourceView* image)
6 {
7 ComPtr<ID3D11Resource> resource;image->GetResource(&resource);
8 ComPtr<ID3D11Texture2D> texture,staging;HR(resource.As(&texture),"cloud alpha texture");
9 D3D11_TEXTURE2D_DESC desc{};texture->GetDesc(&desc);
10 Check(desc.Format==DXGI_FORMAT_B8G8R8A8_UNORM,"cloud alpha format");
11 desc.Usage=D3D11_USAGE_STAGING;desc.BindFlags=desc.MiscFlags=0;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
12 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&staging),"cloud alpha staging");
13 gpu.Context()->CopyResource(staging.Get(),texture.Get());D3D11_MAPPED_SUBRESOURCE mapped{};
14 HR(gpu.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"cloud alpha map");
15 CloudAlphaCounts counts;
16 for(UINT y=0;y<desc.Height;++y)for(UINT x=0;x<desc.Width;++x)
17 {
18 const auto alpha=static_cast<const unsigned char*>(mapped.pData)[y*mapped.RowPitch+x*4+3];
19 if(alpha==0)++counts.empty;else if(alpha==255)++counts.opaque;else ++counts.partial;
20 }
21 gpu.Context()->Unmap(staging.Get(),0);return counts;
22 }
23
24 void TestRainCloudOcclusion(Device11& gpu,CloudRenderer11&
renderer,CloudRenderer11& reference,UINT samples,
bool compareBuilds=
false,
const std::filesystem::path& output={},
bool organic=
false)
25 {
26 if(!gpu.SupportsSamples(samples))return;
27 if(!compareBuilds)reference.Pass().SetRainCloudOcclusionEnabled(false);
28 RainMsaaFixture fixture(gpu.Device(),samples);Scene scene(gpu.Device(),samples),original(gpu.Device(),samples);
30 const float pitchDegrees[]={0,30,45,80,30,-30,-45,-30,30};
31 const float altitudes[]={.3f,.3f,.3f,.3f,.8f,1.2f,12.f,.3f,.3f};
32 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,384));
33 CloudAlphaCounts totals;unsigned cases=0;
34 for(unsigned scenario=0;scenario<std::size(pitchDegrees);++scenario)for(unsigned shape=0;shape<4;++shape)
35 {
37 const float pitch=XMConvertToRadians(pitchDegrees[scenario]);
38 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,std::sin(pitch),-std::cos(pitch),0),XMVectorSet(0,1,0,0)));
39 const D3D11_VIEWPORT viewport=shape==3 ? D3D11_VIEWPORT{13.25f,7.5f,128,96,0,1} :
40 D3D11_VIEWPORT{0,0,float(Width),float(Height),0,1};
41 grid->cells.fill({0,.856f,scenario==5 ? .75f : 27.72f,scenario%2 ? 16.f : 2.f});
42 for(
int y=34;y<47;++y)
for(
int x=22;x<30;++x)
grid->cells[y*64+x]={.29f,.32f,3.f,2.f};
43 const bool flash=scenario%3==1;
44 for(
auto* current:{&
renderer,&reference})
45 {
46 current->SetRainGrid(
grid.get());current->SetLightningEnabled(flash);
47 current->SetLocalLightning({.3f,cloud.
radius+3,-1},{.4f,.4f,.4f,1},12,{.3f,cloud.
radius,-1});
49 target.Clear(gpu.Context());
const float background[]={.85f,.55f,.2f,1};
50 gpu.Context()->ClearRenderTargetView(
target.target.Get(),background);
51 if(shape==1)fixture.pipeline.Occlude(gpu,target);
52 if(shape==2)fixture.Draw(gpu,target,viewport,{8.1f,.006f,-projection._33,projection._43},false);
53 HR(current->Render(gpu.Context(),
target.target.Get(),
target.depth,viewport,view,projection,
54 {0,cloud.radius+altitudes[scenario],0},cloud,{0,-1,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{.3f,.4f,.5f,42.16f}),"cloud occlusion comparison");
55 }
56 const auto actual=fixture.pipeline.Pixels(gpu,scene,samples),expected=fixture.pipeline.Pixels(gpu,original,samples);
57 for(size_t i=0;i<actual.size();++i)
58 {
59 Check(actual[i].depth==expected[i].depth,"cloud fog rejection changed scene depth");
60 for(unsigned c=0;c<4;++c)Check(actual[i].color[c]==expected[i].color[c],"cloud fog rejection changed sample color");
61 }
62 if(compareBuilds && !
output.empty() && samples==4 && shape==0)
63 {
64 std::filesystem::create_directories(output);
65 const auto name=std::string(organic ? "organic-scene" : "scene")+std::to_string(scenario);
66 SaveFogComparison(output/(name+"-before.ppm"),expected,samples);
67 SaveFogComparison(output/(name+"-after.ppm"),actual,samples);
68 }
69 const auto alpha=ReadCloudAlpha(gpu,
renderer.Pass().Image());
70 totals.empty+=alpha.empty;totals.partial+=alpha.partial;totals.opaque+=alpha.opaque;
71 if(++cases==18){
renderer.ReleaseSizeDependent();reference.ReleaseSizeDependent();}
72 }
73 Check(totals.empty>0 && totals.partial>0 && totals.opaque>0,"occlusion fixtures must cover clear, translucent, and opaque cloud texels");
74 for(
auto* current:{&
renderer,&reference}){current->SetRainGrid(
nullptr);current->SetLightningEnabled(
false);}
75 DebugMessages(gpu);
76 std::cout<<"PASS: cloud fog occlusion organic="<<organic<<" "<<samples<<"x, "<<cases<<" scenes, exact sample color/depth; cloud texels empty/partial/opaque "
77 <<totals.empty<<"/"<<totals.partial<<"/"<<totals.opaque<<std::endl;
78 }
79
80 void BenchmarkRainCloudOcclusion(Device11& gpu,CloudRenderer11&
renderer,CloudRenderer11& reference,
bool compareBuilds=
false,
bool organic=
false)
81 {
82 constexpr UINT width=3440,height=1351,samples=4,batch=12;
83 Scene scene(gpu.Device(),samples,width,height);if(!compareBuilds)reference.Pass().SetRainCloudOcclusionEnabled(false);
86 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(width)/height,.001f,384));
87 const D3D11_VIEWPORT viewport{0,0,float(width),float(height),0,1};
88 ComPtr<ID3D11Query> disjoint,begin,end;D3D11_QUERY_DESC desc{D3D11_QUERY_TIMESTAMP_DISJOINT,0};
89 HR(gpu.Device()->CreateQuery(&desc,&disjoint),"sky benchmark disjoint");desc.Query=D3D11_QUERY_TIMESTAMP;
90 HR(gpu.Device()->CreateQuery(&desc,&begin),"sky benchmark begin");HR(gpu.Device()->CreateQuery(&desc,&end),"sky benchmark end");
91 auto* context=gpu.Context();
92 for(
float degrees:{0.f,30.f,45.f})
for(
unsigned repeat=0;repeat<(compareBuilds ? 8u : 3u);++repeat)
for(
auto* current:{repeat%2 ? &
renderer : &reference,repeat%2 ? &reference : &
renderer})
93 {
94 const float pitch=XMConvertToRadians(degrees);
95 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,std::sin(pitch),-std::cos(pitch),0),XMVectorSet(0,1,0,0)));
96 scene.Clear(context);current->SetRainGrid(
grid.get());current->SetLightningEnabled(
false);
97 const auto draw=[&] { HR(current->Render(context,scene.target.Get(),scene.depth,viewport,view,projection,
98 {0,cloud.radius+.300293f,0},cloud,{0,-1,0},{0,0,0,0},{0,0,0,0},{0,0,0,0},{.3f,.4f,.5f,42.16f}),"sky benchmark render"); };
99 for(unsigned warm=0;warm<3;++warm)draw();
100 context->Begin(disjoint.Get());context->End(begin.Get());for(unsigned frame=0;frame<batch;++frame)draw();
101 context->End(end.Get());context->End(disjoint.Get());context->Flush();
102 const auto read=[&](ID3D11Query* query,
void*
output,UINT bytes) {
103 const auto deadline=GetTickCount64()+30000;
HRESULT hr;
104 while((hr=context->GetData(query,output,bytes,0))==S_FALSE)
105 {if(GetTickCount64()>deadline)throw std::runtime_error("sky benchmark timeout");Sleep(1);}
106 HR(hr,"sky benchmark query");
107 };
108 D3D11_QUERY_DATA_TIMESTAMP_DISJOINT frequency{};UINT64 first=0,last=0;
109 read(disjoint.Get(),&frequency,
sizeof(frequency));
read(begin.Get(),&first,
sizeof(first));
read(end.Get(),&last,
sizeof(last));
110 Check(!frequency.Disjoint,"sky benchmark frequency");
111 const auto alpha=ReadCloudAlpha(gpu,current->Pass().Image());
112 std::cout<<"3440x1351 4x "<<(organic ? "organic " : "legacy ")<<"pitch "<<degrees<<" "<<(compareBuilds ? (current==&reference ? "before" : "after") : (current==&reference ? "original" : "occlusion"))<<" iteration "<<repeat<<": "
113 <<double(last-first)*1000/frequency.Frequency/batch<<" ms; opaque cloud texels "<<alpha.opaque<<"/"<<alpha.empty+alpha.partial+alpha.opaque<<std::endl;
114 }
115 renderer.SetRainGrid(
nullptr);reference.SetRainGrid(
nullptr);DebugMessages(gpu);
116 }
117}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
CloudWeather::Frame weather