1#include "ExteriorRainLegacy.h"
2#include "Reference9/ExteriorRainRenderer.h"
3#include "Reference9/CloudVolumeRenderer.h"
4#include <chrono>
5#include <iostream>
6#include <fstream>
7#include <iomanip>
8#include <algorithm>
9#include <string>
10#include <vector>
11#include <cstring>
12
13using Microsoft::WRL::ComPtr;
14using Clock=std::chrono::steady_clock;
15static void HR(
HRESULT hr,
const char* operation) {
16 if(FAILED(hr)) { std::cerr<<operation<<": 0x"<<std::hex<<hr<<std::endl; std::exit(1); }
17}
18static double Ms(Clock::time_point a,Clock::time_point b) { return std::chrono::duration<double,std::milli>(b-a).count(); }
19template<class T> static T Read(IDirect3DQuery9* query) {
20 T value{};
const auto start=Clock::now();
HRESULT hr;
21 while((hr=query->GetData(&value,sizeof(value),D3DGETDATA_FLUSH))==S_FALSE) {
22 if(Ms(start,Clock::now())>10000) { std::cerr<<"GPU query timed out\n"; std::exit(1); }
23 Sleep(0);
24 }
25 HR(hr,"GPU query"); return value;
26}
27static double Percentile(std::vector<double> data,double fraction) {
28 std::sort(data.begin(),data.end()); return data[size_t((data.size()-1)*fraction)];
29}
30#include "ExteriorRainVerification.h"
32int main(
int argc,
char** argv)
33{
34 const char* csvPath=argc>1 ? argv[1] : "Output/exterior-rain-baseline.csv";
35 const bool verifyOnly=argc>2 && std::string(argv[2])=="verify";
36 const bool shader=argc>2 && std::string(argv[2])=="shader";
37 const int repeats=argc>3 ? (std::max)(1,std::atoi(argv[3])) : 1;
38 const char*
renderer=shader ?
"shader" :
"legacy";
39 std::ofstream csv(csvPath);
40 if(!csv) return 1;
41 csv<<"renderer,width,height,cloud,scenario,drops,samples,update_median_ms,update_p95_ms,submit_median_ms,submit_p95_ms,gpu_raw_median_ms,gpu_raw_p95_ms,upload_bytes,completion_median_ms,repeats,total_median_ms,total_p95_ms\n";
42 std::cout<<std::fixed<<std::setprecision(6);
43 Case cases[5]{};
44 cases[0].name="drizzle"; cases[0].p.precipitation=4.3f; cases[0].p.intensity=.1f;
45 cases[1].name="downpour";
46 cases[2].name="flight_headlight"; cases[2].p.headlightCount=true; cases[2].p.sunlight={.06f,.07f,.1f,1};
47 cases[2].p.groundVelocity={0,0,-.12f}; cases[2].p.wind={.008f,0,0}; cases[2].p.angularVelocity={0,0,0};
48 cases[3].name="snow"; cases[3].p.snow=1; cases[3].p.wind={.004f,0,0};
49 cases[4].name="ground_headlight"; cases[4].p.headlightCount=true;
50 cases[4].p.sunlight={.015f,.017f,.02f,1}; cases[4].p.wind={.008f,0,0};
51 for(int width : {1920,3840}) {
52 if((repeats>1 || verifyOnly) && width!=1920) continue;
53 const int height=width*9/16;
54 HWND window=CreateWindowW(L"STATIC",L"Rain benchmark",WS_POPUP,0,0,width,height,nullptr,nullptr,nullptr,nullptr);
55 ComPtr<IDirect3D9> d3d; d3d.Attach(Direct3DCreate9(D3D_SDK_VERSION));
56 if(!d3d || !window) return 1;
57 D3DADAPTER_IDENTIFIER9 adapter{}; HR(d3d->GetAdapterIdentifier(0,0,&adapter),"adapter");
58 std::cout<<"Adapter: "<<adapter.Description<<" driver "<<adapter.DriverVersion.HighPart<<'.'<<adapter.DriverVersion.LowPart<<"\n";
59 D3DPRESENT_PARAMETERS pp{}; pp.Windowed=TRUE; pp.SwapEffect=D3DSWAPEFFECT_DISCARD;
60 pp.hDeviceWindow=window; pp.BackBufferWidth=width; pp.BackBufferHeight=height;
61 pp.EnableAutoDepthStencil=TRUE; pp.AutoDepthStencilFormat=D3DFMT_D24S8;
62 pp.PresentationInterval=D3DPRESENT_INTERVAL_IMMEDIATE;
63 ComPtr<IDirect3DDevice9> device;
64 HR(d3d->CreateDevice(0,D3DDEVTYPE_HAL,window,D3DCREATE_HARDWARE_VERTEXPROCESSING,&pp,&device),"HAL device");
65 ComPtr<IDirect3DSurface9>
target;
66 HR(device->CreateRenderTarget(width,height,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_NONE,0,FALSE,&target,nullptr),"target");
67 HR(device->SetRenderTarget(0,
target.Get()),
"bind target");
68 ComPtr<IDirect3DQuery9> begin,end,disjoint,frequency,completed;
69 HR(device->CreateQuery(D3DQUERYTYPE_EVENT,&completed),"completion query");
70 HR(device->CreateQuery(D3DQUERYTYPE_TIMESTAMP,&begin),"timestamp support");
71 HR(device->CreateQuery(D3DQUERYTYPE_TIMESTAMP,&end),"timestamp support");
72 HR(device->CreateQuery(D3DQUERYTYPE_TIMESTAMPDISJOINT,&disjoint),"disjoint support");
73 HR(device->CreateQuery(D3DQUERYTYPE_TIMESTAMPFREQ,&frequency),"frequency support");
75 D3DXMATRIX world,view,projection; D3DXMatrixIdentity(&world); D3DXMatrixIdentity(&view);
76 D3DXMatrixPerspectiveFovRH(&projection,D3DX_PI/3,float(width)/height,.001f,384.f);
78 HR(device->Clear(0,nullptr,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL,0xff203040,1,0),"clear");
79 HR(device->BeginScene(),"begin cloud setup");
80 HR(clouds.
Render(device.Get(),view,projection,{0,weather.radius+5,0},
weather,{-.5f,-.8f,.2f},{.8f,.8f,.8f,1},{.4f,.4f,.4f,1}),
"cloud setup");
81 HR(device->EndScene(),"end cloud setup");
82 if(width==1920 && repeats==1) {
83 VerifyRain(device.Get(),clouds,
target.Get(),width,height);
84 HR(d3d->CheckDeviceMultiSampleType(0,D3DDEVTYPE_HAL,D3DFMT_A8R8G8B8,TRUE,D3DMULTISAMPLE_4_SAMPLES,nullptr),"4x MSAA support");
85 ComPtr<IDirect3DSurface9> multisample,multidepth,oldDepth;
86 HR(device->GetDepthStencilSurface(&oldDepth),"original depth");
87 HR(device->CreateRenderTarget(width,height,D3DFMT_A8R8G8B8,D3DMULTISAMPLE_4_SAMPLES,0,FALSE,&multisample,nullptr),"MSAA target");
88 HR(device->CreateDepthStencilSurface(width,height,D3DFMT_D24S8,D3DMULTISAMPLE_4_SAMPLES,0,TRUE,&multidepth,nullptr),"MSAA depth");
89 HR(device->SetDepthStencilSurface(nullptr),"detach depth");
90 HR(device->SetRenderTarget(0,multisample.Get()),"bind MSAA target");
91 HR(device->SetDepthStencilSurface(multidepth.Get()),"bind MSAA depth");
92 VerifyRain(device.Get(),clouds,multisample.Get(),width,height);
93 HR(device->SetDepthStencilSurface(nullptr),"detach MSAA depth");
94 HR(device->SetRenderTarget(0,
target.Get()),
"restore target");
95 HR(device->SetDepthStencilSurface(oldDepth.Get()),"restore depth");
96 std::cout<<"4x MSAA verification passed."<<std::endl;
97 }
98 for(bool cloud : {false,true}) for(const auto& scenario:cases) {
99 if(verifyOnly) continue;
100 if(repeats>1 && std::string(scenario.name)!="downpour") continue;
101 ExteriorRainLegacy rain; rain.Initialize(device.Get());
103 unsigned count=0;
104 std::vector<double> update,submit,gpu,completion,total;
105 for(int frame=0;frame<(repeats>1 ? 40 : 360);++frame) {
106 HR(device->Clear(0,nullptr,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER|D3DCLEAR_STENCIL,0xff203040,1,0),"clear");
107 HR(completed->Issue(D3DISSUE_END),"clear fence"); Read<BOOL>(completed.Get());
108 const auto frameStart=Clock::now();
109 const auto start=Clock::now();
110 if(shader) particles.
Update(1.f/60,scenario.p,world);
else rain.Update(1.f/60,scenario.p);
111 const auto updated=Clock::now();
112 count=shader ? particles.
DropCount() : rain.raindrops;
113 device->SetTransform(D3DTS_WORLD,&world); device->SetTransform(D3DTS_VIEW,&view); device->SetTransform(D3DTS_PROJECTION,&projection);
114 device->SetVertexShader(nullptr); device->SetPixelShader(nullptr);
115 device->SetRenderState(D3DRS_LIGHTING,FALSE); device->SetRenderState(D3DRS_FOGENABLE,FALSE);
116 device->SetRenderState(D3DRS_ZENABLE,TRUE); device->SetRenderState(D3DRS_ZWRITEENABLE,FALSE);
117 device->SetRenderState(D3DRS_ALPHABLENDENABLE,TRUE);
118 device->SetRenderState(D3DRS_SRCBLEND,D3DBLEND_SRCALPHA); device->SetRenderState(D3DRS_DESTBLEND,D3DBLEND_INVSRCALPHA);
119 device->SetRenderState(D3DRS_STENCILENABLE,FALSE);
120 device->SetTextureStageState(0,D3DTSS_COLOROP,D3DTOP_SELECTARG2); device->SetTextureStageState(0,D3DTSS_COLORARG2,D3DTA_DIFFUSE);
121 device->SetTextureStageState(0,D3DTSS_ALPHAOP,D3DTOP_SELECTARG2); device->SetTextureStageState(0,D3DTSS_ALPHAARG2,D3DTA_DIFFUSE);
122 HR(disjoint->Issue(D3DISSUE_BEGIN),"disjoint begin"); HR(begin->Issue(D3DISSUE_END),"start timestamp");
123 HR(device->BeginScene(),"begin rain");
124 const auto drawStart=Clock::now();
125 for(int repeat=0;repeat<repeats;++repeat) {
126 if(shader) HR(particles.
Render(device.Get(),cloud ? clouds.
Effect() :
nullptr),
"shader rain");
127 else { rain.Bind(device.Get());
128 if(cloud) HR(clouds.
RenderRain(device.Get(),world,rain.raindrops),
"cloud rain");
129 else HR(device->DrawPrimitive(D3DPT_LINELIST,0,rain.raindrops),"legacy rain");
130 } }
131 const auto submitted=Clock::now();
132 HR(device->EndScene(),"end rain");
133 HR(end->Issue(D3DISSUE_END),"end timestamp"); HR(disjoint->Issue(D3DISSUE_END),"disjoint end");
134 HR(frequency->Issue(D3DISSUE_END),"frequency");
135 HR(completed->Issue(D3DISSUE_END),"completion"); Read<BOOL>(completed.Get());
136 const auto gpuCompleted=Clock::now();
137 const auto finish=Read<UINT64>(end.Get()),first=Read<UINT64>(begin.Get()),hz=Read<UINT64>(frequency.Get());
138 const bool invalid=Read<BOOL>(disjoint.Get())!=FALSE;
139 if(frame>=(repeats>1 ? 10 : 60) && !invalid && hz) { total.push_back(Ms(frameStart,gpuCompleted)); completion.push_back(Ms(drawStart,gpuCompleted)); update.push_back(Ms(start,updated)); submit.push_back(Ms(drawStart,submitted)); gpu.push_back(double(finish-first)*1000/double(hz)); }
140 }
141 if(update.size()<size_t(repeats>1 ? 25 : 250)) { std::cerr<<"Too many disjoint timing samples\n"; return 1; }
142 csv<<std::fixed<<std::setprecision(6)<<
renderer<<
","<<width<<
','<<height<<
','<<cloud<<
','<<scenario.name<<
','<<count<<
','<<update.size()<<
','
143 <<Percentile(update,.5)<<','<<Percentile(update,.95)<<','<<Percentile(submit,.5)<<','<<Percentile(submit,.95)<<','
144 <<Percentile(gpu,.5)<<','<<Percentile(gpu,.95)<<','<<(shader ? 0 : count*32)<<','<<Percentile(completion,.5)<<','<<repeats<<','<<Percentile(total,.5)<<','<<Percentile(total,.95)<<'\n'; csv.flush();
145 std::cout<<width<<" cloud="<<cloud<<' '<<scenario.name<<" update="<<Percentile(update,.5)<<" submit="<<Percentile(submit,.5)<<" GPU(raw/unvalidated)="<<Percentile(gpu,.5)<<" total="<<Percentile(total,.5)<<" completion="<<Percentile(completion,.5)<<" ms\n"<<std::flush;
146 if(width==1920 && !cloud) HR(D3DXSaveSurfaceToFileA((std::string(
"Output/rain-")+
renderer+
"-"+scenario.name+
".png").c_str(),D3DXIFF_PNG,
target.Get(),
nullptr,
nullptr),
"save baseline preview");
147 }
149 DestroyWindow(window);
150 }
151 return 0;
152}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
HRESULT RenderRain(Rendering::RenderDevice *device, const RiseMath::Matrix &world, UINT drops)
HRESULT Initialize(Rendering::RenderDevice *device)
Rendering::CloudRenderer11 * Effect()
HRESULT Render(Rendering::RenderDevice *device, const RiseMath::Matrix &view, const RiseMath::Matrix &projection, const RiseMath::Vector3 &eye, const CloudVolume::Parameters ¶meters, const RiseMath::Vector3 &sunlight, const RiseMath::Color &light, const RiseMath::Color &ambient, const RiseMath::Color &airglow=RiseMath::Color(.125f,.375f,.25f, 1), const RiseMath::Vector4 &horizon=RiseMath::Vector4(0, 0, 0, 0), const RiseMath::Vector4 &atmosphere=RiseMath::Vector4(0, 0, 0, 0), const RiseMath::Vector4 &nightVisionGain=RiseMath::Vector4(1, 1, 1, 0))
HRESULT Initialize(Rendering::RenderDevice *device)
unsigned DropCount() const
HRESULT Render(Rendering::RenderDevice *device, Rendering::CloudRenderer11 *clouds=nullptr)
void Update(float elapsed, const ExteriorRain::Parameters ¶meters, const RiseMath::Matrix &localToWorld)
BasicParameters< RiseMath::Vector3, RiseMath::Color > Parameters