1
2
3#include "../Viewscreen/WindshieldRain.h"
4#include "../outputs/city-processing/reference/WindshieldRain.h"
5#include <chrono>
6#include <cstring>
7#include <iostream>
8#include <stdexcept>
9#include <type_traits>
10
11namespace
12{
13 unsigned checks=0;
14 void Check(bool value,const char* label)
15 {++checks;if(!value)throw std::runtime_error(label);}
16 template<class A,class B> bool Equal(const std::vector<A>& a,const std::vector<B>& b)
17 {
18 static_assert(sizeof(A)==sizeof(B));
19 return a.size()==b.size() && (a.empty() || !std::memcmp(a.data(),b.data(),a.size()*sizeof(A)));
20 }
21 struct Case {const char* name;float width,height,windX,windY;bool wipe;float elapsed=1.f/30;};
22 const Case cases[]={{"parked-front",1.5f,.8f,0,0,false},{"parked-side",.6f,.4f,0,0,false},
23 {"road",1.2f,.9f,8,-25,false},{"fast-flight",3,2,30,-90,false},
24 {"wipers",1.5f,.8f,2,-3,true},
25 {"tower-large",7.608279f,1.951346f,0,0,false,4.f/30},
26 {"tower-angled",4.37637f,1.951346f,2,-3,false,4.f/30},
27 {"tower-windy",7.608279f,1.951346f,20,0,false,4.f/30},
28 {"tower-windy-reverse",4.37637f,1.951346f,-20,-8,false,4.f/30}};
29 template<class S> void Step(S& s,const Case& c,unsigned frame)
30 {
31 const float rain=frame<300 ? 1.f : (frame<390 ? 0.f : .4f);
32 s.Update(frame==240 ? .25f : c.elapsed,rain,{0,11.27f},{c.windX,c.windY},2.596f);
33 if(c.wipe && frame%29==0)
34 {
35 const float x=float(frame%7)/7*c.width;
36 s.WipeTriangle({x,0},{x+.2f,c.height},{x+.4f,0});s.Rebuild();
37 }
38 if(frame==420){s.Clear();s.Impact({.2f,.2f},.003f);s.Rebuild();}
39 }
40 void Verify()
41 {
42
43
44
45 {
47 ReferenceRain::Surface old(1.5f,.8f,73);
48 for(unsigned i=0;i<120;++i)
49 {
50 Step(next,cases[0],i);Step(old,cases[0],i);
51 if(i%7==0){next.Impact({.3f,.2f},.003f);old.Impact({.3f,.2f},.003f);}
52 if(i%11==0){next.WipeTriangle({0,0},{.4f,0},{0,.4f});old.WipeTriangle({0,0},{.4f,0},{0,.4f});}
53 if(i%13==0)Check(Equal(next.Water(),old.Water()),"deferred field snapshot changed");
54 Check(next.Wet()==old.Wet(),"deferred wetness changed");
55 }
56 Check(Equal(next.Water(),old.Water()),"last deferred field changed");
57 }
58 for(const auto& c:cases)
59 {
61 ReferenceRain::Surface old(c.width,c.height,73);
62 std::vector<WindshieldRain::Surface::BeadSample> beads;
63 std::vector<WindshieldRain::Surface::WakeSample> wakes;
64 std::vector<ReferenceRain::Surface::BeadSample> oldBeads;
65 std::vector<ReferenceRain::Surface::WakeSample> oldWakes;
66 std::vector<float> scratch;
67 for(unsigned frame=0;frame<450;++frame)
68 {
69 Step(next,c,frame);Step(old,c,frame);
70 Check(Equal(next.Drops(),old.Drops()),"drop physics/order changed");
71 Check(Equal(next.Residue(),old.Residue()),"residue field changed");
72 Check(Equal(next.Water(),old.Water()),"wetness field changed");
73 Check(next.Revision()==old.Revision() && next.Wetness()==old.Wetness()
74 && next.Wet()==old.Wet(),"water state changed");
75 for(bool withWake:{false,true})
76 {
77 next.BeadMap(beads,withWake ? &wakes : nullptr,&scratch);
78 old.BeadMap(oldBeads,withWake ? &oldWakes : nullptr);
79 if(!Equal(beads,oldBeads))
80 {
81 size_t different=0;
82 for(size_t i=0;i<beads.size();++i)if(std::memcmp(&beads[i],&oldBeads[i],sizeof(beads[i])))
83 {
84 if(!different)std::cerr<<c.name<<" frame="<<frame<<" wakes="<<withWake<<" pixel="<<i
85 <<" old="<<oldBeads[i].x<<","<<oldBeads[i].y<<","<<oldBeads[i].radius
86 <<" new="<<beads[i].x<<","<<beads[i].y<<","<<beads[i].radius<<'\n';
87 ++different;
88 }
89 std::cerr<<"changed texels="<<different<<'\n';
90 }
91 Check(Equal(beads,oldBeads),"bead lookup pixels changed");
92 if(withWake)Check(Equal(wakes,oldWakes),"wake lookup pixels changed");
93 }
94 }
95 std::cout<<"PASS exact water and texture parity: "<<c.name<<'\n';
96 }
97 }
98 template<
class S> std::pair<double,double>
Time(
const Case& c)
99 {
100 S surface(c.width,c.height,73);
101 for(unsigned i=0;i<300;++i)Step(surface,c,i);
102 std::vector<typename S::BeadSample> beads;
103 std::vector<typename S::WakeSample> wakes;
104 std::vector<float> scratch;
105 double update=0,map=0;
106 for(unsigned i=0;i<150;++i)
107 {
108 const auto a=std::chrono::steady_clock::now();Step(surface,c,i);
109 const auto b=std::chrono::steady_clock::now();
110 if constexpr(std::is_same_v<S,WindshieldRain::Surface>)surface.BeadMap(beads,&wakes,&scratch);
111 else surface.BeadMap(beads,&wakes);
112 const auto d=std::chrono::steady_clock::now();
113 update+=std::chrono::duration<double,std::milli>(b-a).count();
114 map+=std::chrono::duration<double,std::milli>(d-b).count();
115 }
116 return {update/150,map/150};
117 }
118 void Benchmark()
119 {
120 for(const auto& c:cases)
121 {
122 std::vector<double> beforeUpdate,afterUpdate,beforeMap,afterMap;
123 for(unsigned trial=0;trial<6;++trial)
124 {
125 std::pair<double,double> a,b;
126 if(trial%2){b=Time<WindshieldRain::Surface>(c);a=Time<ReferenceRain::Surface>(c);}
127 else{a=Time<ReferenceRain::Surface>(c);b=Time<WindshieldRain::Surface>(c);}
128 if(trial){beforeUpdate.push_back(a.first);afterUpdate.push_back(b.first);beforeMap.push_back(a.second);afterMap.push_back(b.second);}
129 }
130 for(auto* v:{&beforeUpdate,&afterUpdate,&beforeMap,&afterMap})std::sort(v->begin(),v->end());
131 std::cout<<c.name<<" update_ms="<<beforeUpdate[2]<<" -> "<<afterUpdate[2]
132 <<" map_ms="<<beforeMap[2]<<" -> "<<afterMap[2]<<'\n';
133 }
134 }
135}
136int main(
int argc,
char** argv)
137{
138 try
139 {
140 if(argc==2 && !std::strcmp(argv[1],"--benchmark"))Benchmark();
141 else if(argc==1)Verify();else throw std::runtime_error("unknown argument");
142 std::cout<<"PASS "<<checks<<" checks\n";return 0;
143 }
144 catch(const std::exception& e){std::cerr<<"FAIL "<<e.what()<<'\n';return 1;}
145}