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

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

1// Offscreen comparisons against the exact sample-frequency fog calculation.
2// No game window, display mode, weather settings, or flight controls are used.
3namespace
4{
5 // Resolve only for the review image; assertions still compare every sample.
6 void SaveFogComparison(const std::filesystem::path& path,const std::vector<Sample>& pixels,UINT samples)
7 {
8 std::ofstream file(path,std::ios::binary);file<<"P6\n"<<Width<<" "<<Height<<"\n255\n";
9 for(size_t i=0;i<pixels.size();i+=samples)for(unsigned c=0;c<3;++c)
10 {
11 float value=0;for(UINT sample=0;sample<samples;++sample)value+=pixels[i+sample].color[c];
12 const auto byte=static_cast<unsigned char>(std::lround(std::clamp(value/samples,0.f,1.f)*255));
13 file.write(reinterpret_cast<const char*>(&byte),1);
14 }
15 Check(bool(file),"write fog comparison image");
16 }
17
18 struct RainMsaaFixture
19 {
20 Pipeline pipeline;
21 ComPtr<ID3D11VertexShader> slope;
22 ComPtr<ID3D11PixelShader> sampleDepth;
23 ComPtr<ID3D11Buffer> constants;
24 RainMsaaFixture(ID3D11Device* device,UINT samples):pipeline(device,samples)
25 {
26 constexpr char shader[]=R"(
27cbuffer Fixture : register(b0) {float4 data;};
28float4 Slope(uint vertex:SV_VertexID):SV_Position
29{
30 float4 p=float4(vertex==2 ? 3 : -1,vertex==1 ? 3 : -1,0,1);
31 p.z=data.x+data.y*p.y;
32 return p;
33}
34struct Output {float4 color:SV_Target;float depth:SV_Depth;};
35Output Samples(float4 pixel:SV_Position,uint sample:SV_SampleIndex)
36{
37 Output o;o.color=float4(.85,.55,.2,1);
38 float distance=data.x+data.y*sample/max(1,SAMPLES-1);
39 o.depth=data.z+data.w/distance;
40 // Mixed sky/surface samples in one quarter, including far-field silhouettes.
41 if(pixel.x<40 && sample==SAMPLES-1)o.depth=1;
42 return o;
43})";
44 const auto count=std::to_string(samples);
45 const D3D_SHADER_MACRO definitions[]={{"SAMPLES",count.c_str()},{nullptr,nullptr}};
46 ComPtr<ID3DBlob> code,errors;
47 HR(D3DCompile(shader,strlen(shader),"RainMsaaFixture",definitions,nullptr,"Slope","vs_5_0",
48 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&code,&errors),"compile fog slope");
49 HR(device->CreateVertexShader(code->GetBufferPointer(),code->GetBufferSize(),nullptr,&slope),"fog slope VS");
50 HR(D3DCompile(shader,strlen(shader),"RainMsaaFixture",definitions,nullptr,"Samples","ps_5_0",
51 D3DCOMPILE_ENABLE_STRICTNESS|D3DCOMPILE_OPTIMIZATION_LEVEL3,0,&code,&errors),"compile fog sample depths");
52 HR(device->CreatePixelShader(code->GetBufferPointer(),code->GetBufferSize(),nullptr,&sampleDepth),"fog sample PS");
53 D3D11_BUFFER_DESC desc{};desc.ByteWidth=16;desc.BindFlags=D3D11_BIND_CONSTANT_BUFFER;
54 HR(device->CreateBuffer(&desc,nullptr,&constants),"fog fixture constants");
55 }
56 void Draw(Device11& gpu,Scene& scene,const D3D11_VIEWPORT& viewport,const XMFLOAT4& data,bool sloped)
57 {
58 auto* context=gpu.Context();context->ClearState();scene.Clear(context);auto* target=scene.target.Get();
59 context->UpdateSubresource(constants.Get(),0,nullptr,&data,0,0);auto* buffer=constants.Get();
60 context->VSSetConstantBuffers(0,1,&buffer);context->PSSetConstantBuffers(0,1,&buffer);
61 context->OMSetRenderTargets(1,&target,scene.depth.WriteView());context->RSSetState(pipeline.raster.Get());
62 context->RSSetViewports(1,&viewport);context->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
63 context->VSSetShader(sloped ? slope.Get() : pipeline.full.Get(),nullptr,0);
64 context->PSSetShader(sloped ? pipeline.blue.Get() : sampleDepth.Get(),nullptr,0);
65 context->OMSetDepthStencilState(pipeline.foreground.Get(),0);context->Draw(3,0);
66 context->OMSetRenderTargets(0,nullptr,nullptr);
67 }
68 };
69
70 void TestRainMsaa(Device11& gpu,CloudRenderer11& renderer,CloudRenderer11& reference,UINT samples,bool compareBuilds=false,bool organic=false)
71 {
72 if(!gpu.SupportsSamples(samples))return;
73 RainMsaaFixture fixture(gpu.Device(),samples);Scene scene(gpu.Device(),samples),original(gpu.Device(),samples);
74 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&view,XMMatrixIdentity());
75 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(Width)/Height,.001f,384));
76 const D3D11_VIEWPORT viewport{0,0,float(Width),float(Height),0,1};
77 CloudVolume::Parameters cloud;cloud.base=80;cloud.top=90;cloud.organic=organic;cloud.weather=renderer.WeatherFrame();
78 auto grid=std::make_unique<CloudRenderer11::RainGrid>();grid->cube=0;grid->u=grid->v=1019;grid->revision=1;
79 double worst=0;unsigned cases=0,unequalPixels=0;
80 for(float visibility:{.01f,.75f,3.f,27.72f,42.16f})for(bool wet:{false,true})
81 for(bool flash:{false,true})for(unsigned shape=0;shape<9;++shape)
82 {
83 grid->cells.fill({0,.856f,visibility,wet ? 16.f : 2.f});
84 // A spatial boundary with terrain/ceiling variation crosses the view.
85 for(int v=34;v<47;++v)for(int u=22;u<30;++u)grid->cells[v*64+u]={.29f,.32f,visibility*.5f,wet ? 2.f : 16.f};
86 const XMFLOAT4 depths=shape==0 ? XMFLOAT4(8.1f,.006f,-projection._33,projection._43) :
87 shape==1 ? XMFLOAT4(8.999f,.006f,-projection._33,projection._43) :
88 shape==2 ? XMFLOAT4(6.3f,12.f,-projection._33,projection._43) :
89 shape==5 ? XMFLOAT4(47.998f,.006f,-projection._33,projection._43) :
90 shape==6 ? XMFLOAT4(50.1f,.006f,-projection._33,projection._43) :
91 shape==7 ? XMFLOAT4(129.f,1.f,-projection._33,projection._43) :
92 shape==8 ? XMFLOAT4(127.99f,.03f,-projection._33,projection._43) :
93 XMFLOAT4(.9999f,.00006f,0,0);
94 for(auto* current:{&renderer,&reference})
95 {
96 current->SetRainGrid(grid.get());current->SetLightningEnabled(flash);
97 current->SetLocalLightning({.3f,cloud.radius+3,-1},{.4f,.4f,.4f,1},12,{.3f,cloud.radius,-1});
98 Scene& target=current==&renderer ? scene : original;
99 fixture.Draw(gpu,target,viewport,depths,shape==3 || shape==4);
100 if(shape==4)fixture.pipeline.Occlude(gpu,target);
101 HR(current->Render(gpu.Context(),target.target.Get(),target.depth,viewport,view,projection,
102 {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}),"fog MSAA comparison render");
103 }
104 const auto actual=fixture.pipeline.Pixels(gpu,scene,samples),expected=fixture.pipeline.Pixels(gpu,original,samples);
105 for(size_t i=0;i<actual.size();++i)
106 {
107 Check(actual[i].depth==expected[i].depth,"fog sharing changed original depth");
108 if(i%samples && actual[i].depth!=actual[i-i%samples].depth)++unequalPixels;
109 for(unsigned c=0;c<4;++c)
110 {
111 const double error=std::abs(double(actual[i].color[c])-expected[i].color[c]);
112 worst=std::max(worst,error);
113 Check(error<=(compareBuilds ? 0 : 1.01/255),"fog sample differs from reference");
114 if(flash || shape==2 || (shape==4 && (i/samples)%Width>=Width*3/5))
115 Check(error==0,"flash/discontinuity/cockpit fallback changed sample color");
116 }
117 }
118 ++cases;
119 if(cases==50){renderer.ReleaseSizeDependent();reference.ReleaseSizeDependent();}
120 }
121 if(samples>1)Check(unequalPixels>0,"fog MSAA fixture must contain unequal original depth samples");
122 for(auto* current:{&renderer,&reference}){current->SetRainGrid(nullptr);current->SetLightningEnabled(false);}
123 DebugMessages(gpu);
124 std::cout<<"PASS: fog MSAA organic="<<organic<<" "<<samples<<"x, "<<cases<<" scenes, max sample error "<<worst*255<<"/255, "<<unequalPixels<<" unequal samples"<<std::endl;
125 }
126
127 void BenchmarkRainMsaa(Device11& gpu,CloudRenderer11& renderer,CloudRenderer11& reference,bool compareBuilds=false,bool organic=false)
128 {
129 constexpr UINT width=3440,height=1351,samples=4,batch=12;
130 RainMsaaFixture fixture(gpu.Device(),samples);Scene scene(gpu.Device(),samples,width,height);
131 XMFLOAT4X4 view,projection;XMStoreFloat4x4(&view,XMMatrixIdentity());
132 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1.f,float(width)/height,.001f,384));
133 const D3D11_VIEWPORT viewport{0,0,float(width),float(height),0,1};
134 CloudVolume::Parameters cloud;cloud.base=80;cloud.top=90;cloud.organic=organic;cloud.weather=renderer.WeatherFrame();
135 auto grid=std::make_unique<CloudRenderer11::RainGrid>();grid->cube=0;grid->u=grid->v=1019;grid->revision=1;
136 grid->cells.fill({0,.856f,27.72f,2});
137 ComPtr<ID3D11Query> disjoint,begin,end;D3D11_QUERY_DESC desc{D3D11_QUERY_TIMESTAMP_DISJOINT,0};
138 HR(gpu.Device()->CreateQuery(&desc,&disjoint),"fog benchmark disjoint");desc.Query=D3D11_QUERY_TIMESTAMP;
139 HR(gpu.Device()->CreateQuery(&desc,&begin),"fog benchmark begin");HR(gpu.Device()->CreateQuery(&desc,&end),"fog benchmark end");
140 auto* context=gpu.Context();
141 for(bool slope:{false,true})for(unsigned repeat=0;repeat<(compareBuilds ? 8u : 3u);++repeat)for(auto* current:{repeat%2 ? &renderer : &reference,repeat%2 ? &reference : &renderer})
142 {
143 fixture.Draw(gpu,scene,viewport,{.9999f,slope ? .00006f : 0,0,0},true);
144 current->SetRainGrid(grid.get());current->SetLightningEnabled(false);
145 const auto draw=[&] {
146 HR(current->Render(context,scene.target.Get(),scene.depth,viewport,view,projection,
147 {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}),"fog benchmark render");
148 };
149 for(unsigned warm=0;warm<3;++warm)draw();
150 context->Begin(disjoint.Get());context->End(begin.Get());for(unsigned frame=0;frame<batch;++frame)draw();
151 context->End(end.Get());context->End(disjoint.Get());context->Flush();
152 const auto read=[&](ID3D11Query* query,void* output,UINT bytes) {
153 const auto deadline=GetTickCount64()+30000;HRESULT hr;
154 while((hr=context->GetData(query,output,bytes,0))==S_FALSE)
155 {if(GetTickCount64()>deadline)throw std::runtime_error("fog benchmark timeout");Sleep(1);}
156 HR(hr,"fog benchmark query");
157 };
158 D3D11_QUERY_DATA_TIMESTAMP_DISJOINT frequency{};UINT64 first=0,last=0;
159 read(disjoint.Get(),&frequency,sizeof(frequency));read(begin.Get(),&first,sizeof(first));read(end.Get(),&last,sizeof(last));
160 Check(!frequency.Disjoint,"fog benchmark frequency");
161 std::cout<<"3440x1351 4x "<<(organic ? "organic " : "legacy ")<<(slope ? "sloped" : "flat")<<" "<<(compareBuilds ? (current==&reference ? "before" : "after") : (current==&reference ? "exact" : "shared"))
162 <<" iteration "<<repeat<<": "<<double(last-first)*1000/frequency.Frequency/batch<<" ms"<<std::endl;
163 }
164 renderer.SetRainGrid(nullptr);reference.SetRainGrid(nullptr);DebugMessages(gpu);
165 }
166}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
Definition grid.h:14
char cube
Definition grid.h:45
HRESULT Draw(Rendering::RenderDevice *device, Rendering::RenderTexture *texture, Rendering::RenderBuffer *vertices, const RiseMath::Matrix &world, UINT segments, Rendering::RenderMaterial *volumeEffect=nullptr, float physicalScale=1)
CloudWeather::Frame weather
Definition CloudVolume.h:35