1
2#include "../Rendering/DeviceDiagnostics11.h"
3#include "../LoadingEyeReveal.h"
4static void TestLoadingEyeReveal()
5{
7 Check(eye.
Progress(1000)==0,
"eye starts inside the pupil");
8 Check(eye.
Progress(1039)==.5f,
"eye retains the original 78-second reveal");
9 Check(eye.
Progress(1001)==.5f,
"a backwards clock cannot reverse the reveal");
10 Check(eye.
Progress(1078)==1,
"eye reaches its original final scale on time");
11 Check(eye.
Progress(1200)==1,
"completed eye reveal holds through prolonged loading");
13 Check(eye.
Progress(2000)==0,
"a new loading session starts in the pupil");
14 Check(eye.
Progress(2085)==1,
"shader stall catches up immediately before drawing");
15 Check(eye.
Progress(2086)==1,
"eye does not resume moving after a stall past completion");
16 for(unsigned hz:{20u,60u,144u})
17 {
19 float previous=0;
20 for(unsigned frame=1;frame<=hz*90;++frame)
21 {
22 const float progress=eye.
Progress(3000+
double(frame)/hz);
23 Check(progress>=previous && progress<=1,"outward reveal is monotonic and bounded at every frame rate");
24 if(frame>=hz*78)Check(progress==1,"final eye size holds at every frame rate");
25 previous=progress;
26 }
27 }
28 std::cout<<"PASS: loading eye direction, duration, stalls and final hold"<<std::endl;
29}
30static unsigned CloudPixelPrograms(Device11& gpu)
31{
32 ComPtr<ID3D11Debug> debug;ComPtr<ID3D11InfoQueue> queue;
33 HR(gpu.Device()->QueryInterface(IID_PPV_ARGS(&debug)),"warmup shader diagnostics");
34 HR(gpu.Device()->QueryInterface(IID_PPV_ARGS(&queue)),"warmup debug queue");
35
36
37 const auto messageLimit=queue->GetMessageCountLimit();
38 HR(queue->SetMessageCountLimit(65536),"warmup residency message capacity");
39 queue->ClearStoredMessages();
40 HR(debug->ReportLiveDeviceObjects(D3D11_RLDO_FLAGS(D3D11_RLDO_DETAIL|D3D11_RLDO_IGNORE_INTERNAL)),"warmup residency");
41 unsigned count=0;
42 for(UINT64 i=0;i<queue->GetNumStoredMessagesAllowedByRetrievalFilter();++i)
43 {
44 SIZE_T size=0;queue->GetMessage(i,nullptr,&size);std::vector<char> data(size);
45 auto* message=reinterpret_cast<D3D11_MESSAGE*>(data.data());HR(queue->GetMessage(i,message,&size),"warmup live shader");
46 if(message->ID==D3D11_MESSAGE_ID_LIVE_PIXELSHADER || message->ID==D3D11_MESSAGE_ID_LIVE_PIXELSHADER_WIN7)++count;
47 }
48 queue->ClearStoredMessages();HR(queue->SetMessageCountLimit(messageLimit),"restore diagnostic capacity");return count;
49}
50static void TestCloudLoadingCancellation(Device11& gpu,const std::filesystem::path& directory)
51{
52 const auto resident=CloudPixelPrograms(gpu);
53 SetEnvironmentVariableA("RISE_LOADING_WORKER_DELAY_MS","200");
54 for(bool present:{false,true})
55 {
56 CloudPass11 pass;HR(pass.Initialize(gpu.Device(),directory),"cancellation initialize");
57 Check(pass.PrepareLoadingStep(gpu.Context(),4,true)==S_FALSE,"cancellation starts pending");
58 if(present)
59 {
60 pass.LoadingFramePresented();
61 for(unsigned i=0;i<40;++i)
62 {HR(pass.PrepareLoadingStep(gpu.Context(),4,true),"pending cancellation poll");pass.LoadingFramePresented();Sleep(1);}
63 }
64 const auto begin=GetTickCount64();pass.Reset();
65 Check(GetTickCount64()-begin<100,"reset does not wait for worker creation or first use");
66
67 HR(pass.Initialize(gpu.Device(),directory),"cancelled device reinitialize");
68 Check(pass.PrepareLoadingStep(gpu.Context(),2,true)==S_FALSE,"restart uses a fresh loading plan");
69 Check(CloudPass11::TestLoadingActiveWorkers()<=1,"at most one preparation worker after restart");
70 pass.Reset();
71 const auto cancelled=GetTickCount64();
72 while(CloudPass11::TestLoadingActiveWorkers())
73 {Check(GetTickCount64()-cancelled<30000,"cancelled worker drains safely");Sleep(1);}
74 }
75 SetEnvironmentVariableA("RISE_LOADING_WORKER_DELAY_MS",nullptr);
76 Check(CloudPixelPrograms(gpu)==resident,"cancelled preparation releases primary shader references");
77 DebugMessages(gpu);
78 std::cout<<"PASS: nonblocking cancellation before/after presentation, restart, bounded workers and resource release"<<std::endl;
79}
80static void TestCloudLoadingPreparation(Device11& gpu,CloudRenderer11&
renderer,UINT samples,
bool organic=
true)
81{
82 TestLoadingEyeReveal();
83 struct PresentationFixture
84 {
85 HWND window=nullptr;
86 ~PresentationFixture(){if(window)DestroyWindow(window);}
87 } fixture;
88 if(GetEnvironmentVariableA("RISE_LOADING_PRESENT",nullptr,0))
89 {
90 WNDCLASSW wc{};wc.lpfnWndProc=DefWindowProcW;wc.hInstance=GetModuleHandleW(nullptr);
91 wc.lpszClassName=L"RiseLoadingPresentationFixture";RegisterClassW(&wc);
92
93 fixture.window=CreateWindowExW(WS_EX_TOOLWINDOW|WS_EX_NOACTIVATE,wc.lpszClassName,L"",WS_POPUP,
94 0,0,64,64,nullptr,nullptr,wc.hInstance,nullptr);
95 Check(fixture.window!=nullptr,"private loading presentation window");
96 HR(gpu.AttachWindow(fixture.window,64,64,1),"loading fixture swapchain");
97 }
98 Scene scene(gpu.Device(),samples,160,90);scene.Clear(gpu.Context());
99 auto* originalTarget=scene.target.Get();gpu.Context()->OMSetRenderTargets(1,&originalTarget,scene.depth.WriteView());
100 const D3D11_VIEWPORT viewport{0,0,160,90,0,1};gpu.Context()->RSSetViewports(1,&viewport);
101 const auto selected=
renderer.Pass().SelectedOrganicPath();
102 unsigned steps=0,announcements=0,backgroundFrames=0;
HRESULT result=S_FALSE;
103 double maxPollMs=0,maxPresentMs=0;
104 LARGE_INTEGER frequency{};QueryPerformanceFrequency(&frequency);
105 const auto milliseconds=[&](LARGE_INTEGER a,LARGE_INTEGER b) {
106 return double(b.QuadPart-a.QuadPart)*1000/double(frequency.QuadPart);
107 };
108 const auto initialResident=CloudPixelPrograms(gpu);
109 std::string previousShader;
110 const auto started=GetTickCount64();
111 while(result==S_FALSE)
112 {
113 ++steps;Check(GetTickCount64()-started<600000,"bounded asynchronous loading duration");
114 FrameProfiler11::Get().BeginFrame(gpu.Device(),gpu.Context(),160,90,samples,false,.016);
115 FrameProfiler11::Get().BeginGpu();
116 LARGE_INTEGER pollBegin{},pollEnd{};QueryPerformanceCounter(&pollBegin);
117 result=
renderer.Pass().PrepareLoadingStep(gpu.Context(),samples,organic);HR(result,
"loading preparation step");
118 QueryPerformanceCounter(&pollEnd);
119 const auto pollMs=milliseconds(pollBegin,pollEnd);maxPollMs=(std::max)(maxPollMs,pollMs);
120 if(CloudPass11::TestLoadingActiveWorkers())++backgroundFrames;
121 const auto progress=
renderer.Pass().LoadingStatus();
122 Check(progress.current>0 && progress.current<=progress.total && !progress.shader.empty(),"loading status identifies the current program");
123 if(steps==1)
124 {
125 const auto announcedResident=CloudPixelPrograms(gpu);
126 std::cout<<"Announcement residency: before="<<initialResident<<", after="<<announcedResident<<std::endl;
127 Check(announcedResident==initialResident,"first program is announced before any GPU program creation");
128 }
129 if(progress.shader!=previousShader)
130 {
131 ++announcements;previousShader=progress.shader;
132 std::cout<<"Announce "<<progress.current<<"/"<<progress.total<<": "<<progress.shader<<std::endl;
133 }
134 if(result==S_FALSE)
135 {
136 Check(
renderer.Pass().PrepareLoadingStep(gpu.Context(),samples,organic)==S_FALSE,
"unpresented loading frame cannot advance or complete preparation");
137 const auto& unchanged=
renderer.Pass().LoadingStatus();
138
139
140 Check(unchanged.current>0 && !unchanged.complete,"pending worker does not bypass final presentation");
141 }
142 else Check(progress.complete,"complete loading status follows the final presentation");
143 ComPtr<ID3D11RenderTargetView> restored;gpu.Context()->OMGetRenderTargets(1,&restored,nullptr);
144 Check(restored.Get()==originalTarget,"loading step restores application render target");
145 D3D11_VIEWPORT after{};UINT count=1;gpu.Context()->RSGetViewports(&count,&after);
146 Check(count==1 && after.Width==160 && after.Height==90,"loading step restores viewport");
147 Check(
renderer.Pass().SelectedOrganicPath()==selected,
"loading preparation preserves active cloud path");
148 if(fixture.window)
149 {
150 LARGE_INTEGER begin{},end{};QueryPerformanceCounter(&begin);
151 const float animation[]={float(steps%120)/120.f,.2f,.4f,1};
152 gpu.Context()->ClearRenderTargetView(gpu.SceneTarget(),animation);
153 HR(gpu.Present(0),"loading fixture presentation");QueryPerformanceCounter(&end);
154 const auto presentMs=milliseconds(begin,end);maxPresentMs=(std::max)(maxPresentMs,presentMs);
155 if(pollMs>50 || presentMs>50)
156 std::cout<<"Slow loading frame "<<steps<<", shader="<<progress.shader
157 <<", poll_ms="<<pollMs<<", present_ms="<<presentMs<<std::endl;
158
159
160 gpu.Context()->OMSetRenderTargets(1,&originalTarget,scene.depth.WriteView());
161 gpu.Context()->RSSetViewports(1,&viewport);
162 }
163
164
165 renderer.Pass().LoadingFramePresented();
166 FrameProfiler11::Get().EndFrame();
167 DebugMessages(gpu);
168 if(result==S_FALSE)Sleep(1);
169 }
170 const unsigned resident=CloudPixelPrograms(gpu);
171 Check(announcements>=
renderer.Pass().LoadingStatus().total,
"every selected job was announced before preparation");
172 Check(backgroundFrames>0,"creation and first-use preparation ran asynchronously");
173 std::cout<<"Loading responsiveness: frames="<<steps<<", background_frames="<<backgroundFrames
174 <<", max_poll_ms="<<maxPollMs<<", max_present_ms="<<maxPresentMs<<std::endl;
175 if(GetEnvironmentVariableA("RISE_LOADING_REQUIRE_RESPONSIVE",nullptr,0))
176 {
177 Check(maxPollMs<100,"loading poll including real-device first draw stays responsive");
178 Check(maxPresentMs<100,"loading animation presents during private-device shader preparation");
179 }
180 if(GetEnvironmentVariableA("RISE_LOADING_FAIL_TOP_JOB",nullptr,0))
181 Check(
renderer.Pass().TestTopLoadingFallbacks()==1,
"optional Top warmup failure restarts original plan exactly once");
182 Check(resident>(organic ? 10u : 6u) && resident<40,"only selected MSAA ascent programs resident");
183 Check(
renderer.Pass().PrepareLoadingStep(gpu.Context(),samples,organic)==S_OK,
"repeated loading preparation is complete");
185 Check(
renderer.Pass().PrepareLoadingStep(gpu.Context(),samples,organic)==S_OK,
"resize retains prepared programs");
186 Check(CloudPixelPrograms(gpu)==resident,"resize does not recreate or expand shader residency");
187 XMFLOAT4X4 view,projection;
188 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,-.2f,-1,0),XMVectorSet(0,1,0,0)));
189 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1,160.f/90,.001f,10000));
191 for(float altitude:{.5f,1.f,10.f,22.7f,30.f,117.f,117.f,3000.f,117.f,30.f,22.f,1.f,.5f})for(bool fog:{false,true})
192 {
193 scene.Clear(gpu.Context());
194 const auto drawStarted=GetTickCount64();
195 HR(
renderer.Render(gpu.Context(),scene.target.Get(),scene.depth,viewport,view,projection,
196 {0,cloud.radius+altitude,0},cloud,{0,-1,0},{1,1,1,1},{.3f,.4f,.5f,1},{},{.5f,.5f,.7f,fog ? 20.f : 0.f}),"prepared ascent/descent draw");
197 if(GetEnvironmentVariableA("RISE_LOADING_REQUIRE_PRIMED",nullptr,0))
198 {
199 D3D11_QUERY_DESC desc{D3D11_QUERY_EVENT,0};ComPtr<ID3D11Query> finished;
200 HR(gpu.Device()->CreateQuery(&desc,&finished),"prepared draw completion query");
201 gpu.Context()->End(finished.Get());gpu.Context()->Flush();
HRESULT complete=S_FALSE;
202 while((complete=gpu.Context()->GetData(finished.Get(),nullptr,0,0))==S_FALSE)
203 {HR(gpu.RemovalReason(),"prepared draw device state");Check(GetTickCount64()-drawStarted<30000,"prepared draw timeout");Sleep(1);}
204 HR(complete,"prepared draw completed");
205 const auto elapsed=GetTickCount64()-drawStarted;
206 std::cout<<"Prepared draw altitude="<<altitude<<", fog="<<fog<<", complete_ms="<<elapsed<<std::endl;
207 Check(elapsed<1000,"prepared transition avoids multi-second driver specialization");
208 }
209 DebugMessages(gpu);
210 Check(CloudPixelPrograms(gpu)==resident,"ascent/descent fog and cloud-path transitions create no new shaders");
211 }
212 gpu.Context()->ClearState();gpu.Context()->Flush();
213 FrameProfiler11::Get().Reset();
214 std::cout<<"PASS: loading preparation "<<steps<<" steps, "<<resident<<" pixel programs, "<<GetTickCount64()-started<<" ms; resize and ascent/descent reuse programs"<<std::endl;
215}
216
217
218
219static void CheckCloudStartupResidency(Device11& gpu)
220{
221 ComPtr<ID3D11Debug> debug;ComPtr<ID3D11InfoQueue> queue;
222 if(FAILED(gpu.Device()->QueryInterface(IID_PPV_ARGS(&debug))))return;
223 HR(gpu.Device()->QueryInterface(IID_PPV_ARGS(&queue)),"startup shader diagnostics");
224 gpu.Context()->ClearState();gpu.Context()->Flush();queue->ClearStoredMessages();
225 HR(debug->ReportLiveDeviceObjects(D3D11_RLDO_FLAGS(D3D11_RLDO_DETAIL|D3D11_RLDO_IGNORE_INTERNAL)),"startup shader residency");
226 unsigned pixels=0;
227 for(UINT64 i=0;i<queue->GetNumStoredMessagesAllowedByRetrievalFilter();++i)
228 {
229 SIZE_T size=0;queue->GetMessage(i,nullptr,&size);std::vector<char> data(size);
230 auto* message=reinterpret_cast<D3D11_MESSAGE*>(data.data());HR(queue->GetMessage(i,message,&size),"startup live device object");
231 if(message->ID==D3D11_MESSAGE_ID_LIVE_PIXELSHADER || message->ID==D3D11_MESSAGE_ID_LIVE_PIXELSHADER_WIN7)++pixels;
232 }
233 queue->ClearStoredMessages();
234 Check(pixels<=4,"cold startup eagerly created inactive cloud/MSAA programs");
235 std::cout<<"PASS: cold startup has "<<pixels<<" resident pixel shaders (geometry only)"<<std::endl;
236}
237static void TestCloudStartup(Device11& gpu,CloudRenderer11&
renderer,
bool organic)
238{
239 CheckCloudStartupResidency(gpu);
240 Scene scene(gpu.Device(),4,3440,1351);
241 const D3D11_VIEWPORT viewport{0,0,3440,1351,0,1};
242 XMFLOAT4X4 view,projection;
243 XMStoreFloat4x4(&view,XMMatrixLookAtRH(XMVectorZero(),XMVectorSet(0,-.1763f,-1,0),XMVectorSet(0,1,0,0)));
244 XMStoreFloat4x4(&projection,XMMatrixPerspectiveFovRH(1,3440.f/1351,.001f,10000));
246 auto grid=std::make_shared<CloudBaseWeather::Grid>();
248 for(
auto& cell:
grid->cells)cell={cloud.
base,.6f,1,0};
grid->Bounds();
249 Pipeline geometry(gpu.Device(),4);
250 struct Pressure {std::vector<void*> blocks;~Pressure(){for(auto block:blocks)VirtualFree(block,0,MEM_RELEASE);}} pressure;
251 if(GetEnvironmentVariableA("RISE_TEST_LOW_ADDRESS_SPACE",nullptr,0))
252 {
253 MEMORYSTATUSEX memory{};memory.dwLength=sizeof(memory);
254 while(GlobalMemoryStatusEx(&memory) && memory.ullAvailVirtual>160ull*1024*1024)
255 {auto block=VirtualAlloc(nullptr,16*1024*1024,MEM_RESERVE,PAGE_NOACCESS);if(!block)break;pressure.blocks.push_back(block);}
256 }
258 CloudRenderer11::RainGrid rain;rain.cube=
grid->
cube;rain.u=
grid->u;rain.v=
grid->v;
259 for(
auto& cell:rain.cells)cell={0,cloud.
base,20,8};
260 CloudRenderer11::Headlight lamp;
261 lamp.position={0,cloud.
radius+.0005f,0};lamp.direction={0,0,-1};lamp.up={0,1,0};
262 lamp.color={1,1,1,1};lamp.rangeKm=lamp.profile.rangeKm=2;lamp.extinctionPerKm=3.912f/.2f;
264 for(bool spatialRain:{false,true})for(bool local:{false,true})for(bool fog:{false,true})for(bool finite:{false,true})
265 {
267 renderer.SetRainGrid(spatialRain ? &rain :
nullptr);
268 scene.Clear(gpu.Context());
269 if(finite)geometry.Hull(gpu.Context(),scene,viewport,true);
270 std::cout<<"Startup draw: organic="<<organic<<" localBase="<<local<<" fog="<<fog<<" finiteDepth="<<finite<<" rain="<<spatialRain<<std::endl;
271 const auto started=GetTickCount64();
272 HR(
renderer.Render(gpu.Context(),scene.target.Get(),scene.depth,viewport,view,projection,
273 {0,cloud.radius+.0005f,0},cloud,{0,-1,0},{1,1,1,1},{.3f,.4f,.5f,1},{},
274 {.5f,.5f,.7f,fog ? 20.f : 0.f}),"startup ground render");
275 D3D11_QUERY_DESC desc{D3D11_QUERY_EVENT,0};ComPtr<ID3D11Query> done;
276 HR(gpu.Device()->CreateQuery(&desc,&done),"startup completion query");
277 gpu.Context()->End(done.Get());gpu.Context()->Flush();
279 while((hr=gpu.Context()->GetData(done.Get(),nullptr,0,D3D11_ASYNC_GETDATA_DONOTFLUSH))==S_FALSE)
280 {HR(gpu.RemovalReason(),"startup device removal");Check(GetTickCount64()-started<30000,"startup completion timeout");Sleep(1);}
281 if(FAILED(hr))std::cerr<<"Startup removal reason: 0x"<<std::hex<<unsigned(gpu.RemovalReason())<<std::dec<<std::endl;
282 HR(hr,"startup GPU completion");DebugMessages(gpu);
283 std::cout<<"Startup completed in "<<GetTickCount64()-started<<" ms wall time"<<std::endl;
284 }
286}
287static void CheckCloudDeviceReleased(Device11& gpu,CloudRenderer11&
renderer)
288{
289 renderer.Reset();gpu.Context()->ClearState();gpu.Context()->Flush();
290 ComPtr<ID3D11Debug> debug;ComPtr<ID3D11InfoQueue> queue;
291 if(FAILED(gpu.Device()->QueryInterface(IID_PPV_ARGS(&debug))))return;
292 HR(gpu.Device()->QueryInterface(IID_PPV_ARGS(&queue)),"device lifetime diagnostics");
293 queue->ClearStoredMessages();
294 HR(debug->ReportLiveDeviceObjects(D3D11_RLDO_FLAGS(D3D11_RLDO_DETAIL|D3D11_RLDO_IGNORE_INTERNAL)),"cloud release report");
295 unsigned samplers=0;
296 for(UINT64 i=0;i<queue->GetNumStoredMessagesAllowedByRetrievalFilter();++i)
297 {
298 SIZE_T size=0;queue->GetMessage(i,nullptr,&size);std::vector<char> data(size);
299 auto* message=reinterpret_cast<D3D11_MESSAGE*>(data.data());HR(queue->GetMessage(i,message,&size),"live device object");
300 if(message->ID==D3D11_MESSAGE_ID_LIVE_SAMPLER || message->ID==D3D11_MESSAGE_ID_LIVE_SAMPLER_WIN7)
301 {++samplers;std::cout<<message->pDescription<<std::endl;}
302 }
303 queue->ClearStoredMessages();
304 Check(samplers==0,"cloud Reset retained sampler references to the old device");
305 std::cout<<"PASS: cloud reset releases every sampler reference to the previous device"<<std::endl;
306}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
float Progress(double now)
std::string MemoryDiagnostics11(ID3D11Device *device)
CloudWeather::Frame weather
CloudBaseWeather::Frame localBase