1#pragma once
2
3static void Require(bool condition,const char* message)
4{
5 if(!condition) { std::cerr<<"FAIL: "<<message<<std::endl; std::exit(1); }
6}
7
8
10{
11 D3DXVECTOR3 velocity;
12 const float period=rain.
Offsets()[group].w;
13 for(unsigned axis=0;axis<3;++axis) {
14 const float phase=rain.
Offsets()[group][axis];
15 velocity[axis]=(phase>period*.5f ? phase-period : phase)/dt;
16 }
17 return velocity;
18}
19static void VerifyRainMotion()
20{
21 constexpr float dt=1.f/60;
22
23
24 const D3DXVECTOR3 aircraft(.012f,.022f,-.111f),windFrom(-.003f,-.001f,-.008f);
25 for(const D3DXVECTOR3 angles : {D3DXVECTOR3(0,0,0),D3DXVECTOR3(.178f,.7f,.2f),
26 D3DXVECTOR3(D3DX_PI/2,0,0),D3DXVECTOR3(0,0,D3DX_PI/2)}) {
27 D3DXMATRIX attitude,inverse;
28 D3DXMatrixRotationYawPitchRoll(&attitude,angles.y,angles.x,angles.z);
29 D3DXMatrixInverse(&inverse,nullptr,&attitude);
32 D3DXVec3TransformNormal(&p.
wind,&windFrom,&inverse);
34 for(unsigned group=0;group<4;++group) {
35
36
37 constexpr float fallKms[]={.005903285f,.006018008f,.006111935f,.006188836f};
38 const D3DXVECTOR3 expected(-.009f,-.021f-fallKms[group],.119f);
39 const D3DXVECTOR3 error=RainVelocity(rain,group,dt)-expected;
40 Require(D3DXVec3Length(&error)<.000002f,"rain follows relative airflow through climb, slip, pitch and roll");
41 }
42 }
43 D3DXMATRIX identity; D3DXMatrixIdentity(&identity);
48 const D3DXVECTOR3 flow=RainVelocity(drifting,0,dt);
49 Require(std::abs(flow.x)<.000002f && std::abs(flow.z)<.000002f && flow.y<0,
50 "moving with the air leaves only gravitational fall");
51}
52
53
54
55static bool RainRayMeetsHeadlight(const D3DXVECTOR3& eye,const D3DXVECTOR3& ray,
57{
58 const D3DXVECTOR3 offset=eye-p.
headlights[0].position;
59 const double b=D3DXVec3Dot(&offset,&ray);
60 const double discriminant=b*b-D3DXVec3LengthSq(&offset)+p.
headlights[0].range*p.
headlights[0].range;
61 if(discriminant<0) return false;
62 double first=(std::max)(0.,-b-std::sqrt(discriminant)),last=-b+std::sqrt(discriminant);
63 const double originAxis=D3DXVec3Dot(&offset,&p.
headlights[0].direction);
64 const double rayAxis=D3DXVec3Dot(&ray,&p.
headlights[0].direction);
65 if(rayAxis>1e-8) first=(std::max)(first,-originAxis/rayAxis);
66 else if(rayAxis< -1e-8) last=(std::min)(last,-originAxis/rayAxis);
67 else if(originAxis<0) return false;
68 if(last<=first) return false;
69 const double cosine=std::cos(.202),cosine2=cosine*cosine;
70 const double a=rayAxis*rayAxis-cosine2;
71 const double linear=2*(originAxis*rayAxis-cosine2*b);
72 const double c=originAxis*originAxis-cosine2*D3DXVec3LengthSq(&offset);
73 const auto cone=[&](double t) { return (a*t+linear)*t+c; };
74 const double middle=a< -1e-8 ? (std::clamp)(-linear/(2*a),first,last) : first;
75 return (std::max)({cone(first),cone(last),cone(middle)})>=0;
76}
77static void VerifyRain(IDirect3DDevice9* device,
CloudVolumeRenderer& clouds,IDirect3DSurface9* target,
int width,
int height)
78{
79 VerifyRainMotion();
80 D3DXMATRIX identity,projection;
81 D3DXMatrixIdentity(&identity);
82 D3DXMatrixPerspectiveFovRH(&projection,D3DX_PI/3,float(width)/height,.001f,384.f);
83 device->SetTransform(D3DTS_WORLD,&identity); device->SetTransform(D3DTS_VIEW,&identity); device->SetTransform(D3DTS_PROJECTION,&projection);
84 ComPtr<IDirect3DSurface9> readback;
85 ComPtr<IDirect3DSurface9> resolved;
86 D3DSURFACE_DESC targetDescription{}; HR(
target->GetDesc(&targetDescription),
"verification target description");
87 if(targetDescription.MultiSampleType!=D3DMULTISAMPLE_NONE)
88 HR(device->CreateRenderTarget(width,height,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,FALSE,&resolved,nullptr),"MSAA resolve target");
89 HR(device->CreateOffscreenPlainSurface(width,height,D3DFMT_A8R8G8B8,D3DPOOL_SYSTEMMEM,&readback,nullptr),"verification readback");
91 HR(device->Clear(0,nullptr,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL,background,1,0),"verification clear");
92 if(blockers) {
93 const D3DRECT nearObject{0,0,width/4,height},
cockpit{width*3/4,0,width,height};
94 HR(device->Clear(1,&nearObject,D3DCLEAR_ZBUFFER,0,0,0),"near object");
95 HR(device->Clear(1,&
cockpit,D3DCLEAR_STENCIL,0,1,3),
"cockpit stencil");
96 }
97 device->SetRenderState(D3DRS_ZWRITEENABLE,TRUE);
98 device->SetRenderState(D3DRS_CULLMODE,D3DCULL_CW);
99 device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_DESTCOLOR);
100 HR(device->BeginScene(),"verification begin");
101 HR(rain.
Render(device,cloud ? clouds.
Effect() :
nullptr),
"verification draw");
102 HR(device->EndScene(),"verification end");
104 device->GetRenderState(D3DRS_ZWRITEENABLE,&state); Require(state==TRUE,"restore depth writes");
105 device->GetRenderState(D3DRS_CULLMODE,&state); Require(state==D3DCULL_CW,"restore cull state");
106 device->GetRenderState(D3DRS_SRCBLEND,&state); Require(state==D3DBLEND_DESTCOLOR,"restore blend state");
107 if(resolved) HR(device->StretchRect(target,nullptr,resolved.Get(),nullptr,D3DTEXF_NONE),"resolve MSAA particles");
108 HR(device->GetRenderTargetData(resolved ? resolved.Get() : target,readback.Get()),"verification pixels");
109 D3DLOCKED_RECT locked{}; HR(readback->LockRect(&locked,nullptr,D3DLOCK_READONLY),"verification lock");
110 std::vector<DWORD> pixels(size_t(width)*height);
111 for(int y=0;y<height;++y) std::memcpy(pixels.data()+size_t(y)*width,static_cast<char*>(locked.pBits)+y*locked.Pitch,width*4);
112 HR(readback->UnlockRect(),"verification unlock");
113 return pixels;
114 };
115 const auto energy=[](const std::vector<DWORD>& pixels) {
116 double sum=0;
for(
DWORD pixel:pixels) sum+=(pixel&255)+((pixel>>8)&255)+((pixel>>16)&255);
return sum;
117 };
118
119
120 D3DXMATRIX groundProjection,screenRotation;
121 const float nearWidth=.001f*std::tan(D3DXToRadian(115.f)*.5f);
122 D3DXMatrixPerspectiveRH(&groundProjection,nearWidth,nearWidth*height/width,.001f,384.f);
123 D3DXMatrixRotationZ(&screenRotation,D3DX_PI);
124 groundProjection=screenRotation*groundProjection;
125 device->SetTransform(D3DTS_PROJECTION,&groundProjection);
126 for(int scenario=0;scenario<3;++scenario) {
130 const char* name=scenario==0 ? "ground-drizzle" : scenario==1 ? "ground-downpour" : "ground-night";
132 fixture.
Update(1,ground,identity);
134 const auto pixels=render(fixture,false,false,0xff102c20);
135 unsigned covered=0;
136 for(
DWORD pixel:pixels) {
137 const int contrast=(std::max)({std::abs(int(pixel&255)-32),
138 std::abs(int((pixel>>8)&255)-44),std::abs(int((pixel>>16)&255)-16)});
139 if(contrast>=3) ++covered;
140 }
141 std::cout<<
"Rain coverage "<<name<<
": drops="<<fixture.
DropCount()<<
" visible_pixels="<<covered
142 <<" coverage="<<100.*covered/pixels.size()<<"%"<<std::endl;
143
144
145 const double coverage=double(covered)/pixels.size();
146 if(scenario==0) Require(coverage<.001,"drizzle remains lighter than a downpour");
147 if(scenario==1) Require(coverage>.007,"maximum rain has substantial parked daytime coverage");
148 if(scenario==2) Require(coverage>.0025,"maximum rain stays visible in the headlight cone");
149 if(!resolved) HR(D3DXSaveSurfaceToFileA((std::string("Output/rain-coverage-")+name+".png").c_str(),
150 D3DXIFF_PNG,target,nullptr,nullptr),"save ground rain coverage");
151 }
152 device->SetTransform(D3DTS_PROJECTION,&projection);
154 std::vector<DWORD> reference;
155 for(int fps : {30,60,144}) {
157 for(
int frame=0;frame<fps;++frame) rain.
Update(1.f/fps,p,identity);
158 auto pixels=render(rain);
159 if(reference.empty()) reference=pixels;
160 else {
161 double error=0;
162 for(size_t i=0;i<pixels.size();++i) error+=std::abs(int(pixels[i]&255)-int(reference[i]&255));
163 std::cout<<"Rain "<<fps<<" FPS mean channel difference: "<<error/pixels.size()<<std::endl;
164 Require(error/pixels.size()<.02,"same simulated instant at 30/60/144 FPS");
165 }
166 Require(energy(pixels)>10000,"visible rain");
167 }
169 rain.
Update(1,p,identity);
170 const auto clear=render(rain);
171 D3DXMATRIX externalView; D3DXMatrixTranslation(&externalView,-.2f,0,0);
172 device->SetTransform(D3DTS_VIEW,&externalView);
173 Require(energy(render(rain))>energy(clear)*.4,"external camera retains a surrounding rain volume");
174 device->SetTransform(D3DTS_VIEW,&identity);
175 D3DXMATRIX turned; D3DXMatrixRotationY(&turned,D3DX_PI*.5f);
177 Require(clear==render(rain),"yaw does not rotate the world particle field");
178 for(int axis=0;axis<2;++axis) {
179 D3DXMATRIX attitude;
180 if(axis==0) D3DXMatrixRotationX(&attitude,D3DX_PI*.5f);
181 else D3DXMatrixRotationZ(&attitude,D3DX_PI*.5f);
183 tilted.
Update(1,p,attitude);
184 Require(clear==render(tilted),"pitch and roll cannot tilt rain gravity");
185 }
186
187
188
189 D3DXMATRIX climbAttitude,climbView;
190 D3DXMatrixRotationX(&climbAttitude,D3DXToRadian(10.2f));
191 D3DXMatrixInverse(&climbView,nullptr,&climbAttitude);
193 const D3DXVECTOR3 climbWorld(0,.022f,-.111f);
194 D3DXVec3TransformNormal(&climb.
groundVelocity,&climbWorld,&climbView);
196 constexpr float motionDt=1.f/60;
197 climbing.
Update(motionDt,climb,climbAttitude);
198 for(unsigned group=0;group<4;++group) {
199 D3DXVECTOR3 velocity=RainVelocity(climbing,group,motionDt);
200 D3DXVec3TransformNormal(&velocity,&velocity,&climbView);
201 Require(velocity.z>.1f && std::abs(velocity.x/velocity.z)<.001f
202 && velocity.y/velocity.z<0 && velocity.y/velocity.z>-.12f,
203 "climbing rain expands from near straight ahead rather than overhead");
204 const D3DXVECTOR2 focus(-velocity.x/velocity.z,-velocity.y/velocity.z);
205 for(float x : {-.025f,.025f}) for(float y : {-.015f,.015f}) {
206 const D3DXVECTOR3 oldPosition(x,y,-.05f),newPosition=oldPosition+velocity*motionDt;
207 const D3DXVECTOR2 before(x/.05f,y/.05f);
208 const D3DXVECTOR2 after(newPosition.x/-newPosition.z,newPosition.y/-newPosition.z);
209 const D3DXVECTOR2 movement=after-before,radial=before-focus;
210 Require(D3DXVec2Dot(&movement,&radial)>0,"forward-flight streaks move outward in every screen quadrant");
211 }
212 }
213 device->SetTransform(D3DTS_VIEW,&climbView);
214 Require(energy(render(climbing))>10000,"visible E-10 climbing rain");
215 if(!resolved) {
216 render(climbing,false,false,0xff203040);
217 HR(D3DXSaveSurfaceToFileA("Output/rain-shader-e10-climb.png",D3DXIFF_PNG,target,nullptr,nullptr),"save E-10 climb preview");
218 }
219 device->SetTransform(D3DTS_VIEW,&identity);
220 const auto obscured=render(rain,true);
221 Require(energy(obscured)<energy(clear)*.95 && energy(obscured)>0,"cloud attenuation preserves nearby rain");
222 const auto blocked=render(rain,true,true);
223 for(int y=0;y<height;++y) for(int x=0;x<width;++x)
224 if(x<width/4 || x>=width*3/4) Require((blocked[size_t(y)*width+x]&0xffffff)==0,"depth and cockpit occlude particles");
225 const auto phaseBefore=rain.
Offsets()[0];
227 HR(rain.
Initialize(device),
"recreate particle resources");
228 Require(rain.
Offsets()[0]==phaseBefore,
"device recreation preserves motion phase");
229 Require(clear==render(rain),"device recreation preserves rendered field");
231 Require(rain.
DropCount()==0 && energy(render(rain))==0,
"dry weather has no draw");
233 Require(energy(render(rain))==0,"unlit night has no luminous rain");
234 const auto unlitAgainstSky=render(rain,false,false,0xff203040);
235 for(
DWORD pixel:unlitAgainstSky) Require(pixel==0xff203040,
"unlit transparent rain must not paint dark streaks over the sky");
237 Require(energy(render(rain))>1000,"headlights illuminate the local rain cone");
238
239
240
241 device->SetTransform(D3DTS_PROJECTION,&groundProjection);
242 for(int scenario=0;scenario<3;++scenario) {
247 D3DXMATRIX attitude=identity,lampView=identity;
248 if(scenario==1) D3DXMatrixTranslation(&lampView,-.015f,0,0);
249 if(scenario==2) {
250 D3DXMatrixRotationYawPitchRoll(&attitude,.25f,.18f,.1f);
251 D3DXMatrixInverse(&lampView,nullptr,&attitude);
254 }
255 device->SetTransform(D3DTS_VIEW,&lampView);
256 D3DXMATRIX inverseView,inverseViewProjection,viewProjection=lampView*groundProjection;
257 D3DXMatrixInverse(&inverseView,nullptr,&lampView);
258 D3DXMatrixInverse(&inverseViewProjection,nullptr,&viewProjection);
259 const D3DXVECTOR3 eye(inverseView._41,inverseView._42,inverseView._43);
261 coneRain.
Update(1,lamp,attitude);
262 for(bool cloud : {false,true}) {
263 const auto pixels=render(coneRain,cloud);
264 unsigned lit=0,outside=0;
265 for(int y=0;y<height;++y) for(int x=0;x<width;++x) {
266 const DWORD pixel=pixels[size_t(y)*width+x];
267 if((std::max)({pixel&255,(pixel>>8)&255,(pixel>>16)&255})<3) continue;
268 ++lit;
269 const D3DXVECTOR3 ndc(2*(x+.5f)/width-1,1-2*(y+.5f)/height,.5f);
270 D3DXVECTOR3 point,ray;
271 D3DXVec3TransformCoord(&point,&ndc,&inverseViewProjection);
272 ray=point-eye; D3DXVec3Normalize(&ray,&ray);
273 if(!RainRayMeetsHeadlight(eye,ray,lamp)) ++outside;
274 }
275 std::cout<<"Rain headlight cone scenario="<<scenario<<" cloud="<<cloud
276 <<" lit_pixels="<<lit<<" outside_cone="<<outside<<std::endl;
277 Require(lit>10,"forward-mounted lamp still illuminates rain inside its cone");
278 Require(outside==0,"rain highlights and streak ends stay within the cloud headlight cone");
279 if(!resolved && scenario==0 && !cloud) HR(D3DXSaveSurfaceToFileA(
280 "Output/rain-headlight-nose-cone.png",D3DXIFF_PNG,target,nullptr,nullptr),"save nose-mounted headlight rain");
281 }
283 coneRain.
Update(0,lamp,identity);
284 Require(energy(render(coneRain))==0,"rain behind a forward-mounted lamp is unlit");
286 coneRain.
Update(0,lamp,identity);
287 Require(energy(render(coneRain))==0,"zero headlight range illuminates no rain");
288 }
289 device->SetTransform(D3DTS_VIEW,&identity);
290 device->SetTransform(D3DTS_PROJECTION,&projection);
291
292 for(int i=0;i<100000;++i) {
294 rain.
Update(1,p,identity);
295 }
296 for(int group=0;group<4;++group) for(int axis=0;axis<3;++axis)
297 Require(rain.
Offsets()[group][axis]>=0 && rain.
Offsets()[group][axis]<rain.
Offsets()[group].w,
"bounded phase after long flight");
298 std::cout<<"Exterior rain verification passed: headlight cone containment, maximum-rain coverage, E-10 climb/outward flow, relative wind, attitude invariance, frame rates, turning, lighting, depth/stencil, dry weather, device recreation, long flight."<<std::endl;
299}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
Rendering::CloudRenderer11 * Effect()
HRESULT Initialize(Rendering::RenderDevice *device)
unsigned DropCount() const
HRESULT Render(Rendering::RenderDevice *device, Rendering::CloudRenderer11 *clouds=nullptr)
const DirectX::XMFLOAT4 * Offsets() const
void Update(float elapsed, const ExteriorRain::Parameters ¶meters, const RiseMath::Matrix &localToWorld)
BasicParameters< RiseMath::Vector3, RiseMath::Color > Parameters
constexpr unsigned MaxDrops