Rise
The Vieneo Province
Loading...
Searching...
No Matches
Tests/DisplayProfile11Tests.cpp

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

1#include "../Rendering/Device11.h"
2#include "../Rendering/Font11.h"
3#include "../Rendering/FrameProfiler11.h"
4#include <d3d11sdklayers.h>
5#include <filesystem>
6#include <fstream>
7#include <iostream>
8#include <map>
9#include <sstream>
10#include <stdexcept>
11#include <vector>
12using namespace Rendering;
13using Microsoft::WRL::ComPtr;
14namespace
15{
16 void Check(bool ok,const char* message){if(!ok)throw std::runtime_error(message);}
17 void HR(HRESULT hr,const char* message){Check(SUCCEEDED(hr),message);}
18 std::vector<std::string> Split(const std::string& line)
19 {std::vector<std::string> result;std::istringstream in(line);std::string s;while(std::getline(in,s,','))result.push_back(s);return result;}
20}
21int main(int argc,char** argv)
22{
23 try
24 {
25 bool enabled=false;std::filesystem::path shaderPath="outputs/direct3d11/shaders";
26 for(int i=1;i<argc;++i)
27 {
28 enabled|=std::string(argv[i])=="--profile-render";
29 if(std::string(argv[i])=="--shaders" && i+1<argc)shaderPath=argv[++i];
30 }
31 const auto shaders=std::filesystem::absolute(shaderPath);
32 const auto root=std::filesystem::absolute(std::filesystem::path("outputs/display-profiler-tests")/std::to_string(GetCurrentProcessId()));
33 std::filesystem::create_directories(root);std::filesystem::current_path(root);
34 Device11 gpu;HR(gpu.Create({D3D_DRIVER_TYPE_WARP,true}),"create WARP device");
35 Sprite11 sprite;HR(sprite.Initialize(gpu.Device(),shaders),"create sprite");
36 Font11 font;LOGFONTW description{};description.lfHeight=-16;wcscpy_s(description.lfFaceName,L"Arial");
37 HR(font.Initialize(gpu.Device(),description),"create font");
38 D3D11_TEXTURE2D_DESC desc{};desc.Width=desc.Height=128;desc.MipLevels=desc.ArraySize=desc.SampleDesc.Count=1;
39 desc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;desc.BindFlags=D3D11_BIND_RENDER_TARGET;
40 ComPtr<ID3D11Texture2D> target,staging;ComPtr<ID3D11RenderTargetView> rtv;
41 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&target),"create target");
42 HR(gpu.Device()->CreateRenderTargetView(target.Get(),nullptr,&rtv),"create RTV");
43 desc.Usage=D3D11_USAGE_STAGING;desc.BindFlags=0;desc.CPUAccessFlags=D3D11_CPU_ACCESS_READ;
44 HR(gpu.Device()->CreateTexture2D(&desc,nullptr,&staging),"create staging");
45 ComPtr<ID3D11DepthStencilState> depth;D3D11_DEPTH_STENCIL_DESC ds{};ds.DepthFunc=D3D11_COMPARISON_ALWAYS;
46 HR(gpu.Device()->CreateDepthStencilState(&ds,&depth),"disable depth");
47 auto& profiler=FrameProfiler11::Get();std::vector<uint32_t> baseline;
48 // Frame zero is unprofiled. Subsequent frames must retain identical pixels,
49 // including a cold cache on the first captured frame and warm cache after.
50 for(unsigned frame=0;frame<9;++frame)
51 {
52 if(frame==1){std::ofstream("render-profile.capture").close();Sleep(150);HR(font.Initialize(gpu.Device(),description),"reset font cache");}
53 profiler.BeginFrame(gpu.Device(),gpu.Context(),128,128,1,false,1./60);
54 Check(profiler.Capturing()==(enabled && frame>0),"two-key capture gate");
55 {ProfileDisplayCpu11 excluded("Outside display detail");}
56 {
57 ProfileCpuScope11 update("CPU update");
58 // Surpass the old combined CPU/GPU row limit. These label
59 // strings are destroyed before delayed CSV serialization.
60 for(int i=0;i<220;++i)
61 {
62 const auto label="Transient component #"+std::to_string(i);
63 ProfileCpuScope11 row(label.c_str());
64 }
65 ProfileCpuScope11 safeLabel("CSV label","comma,quote\"line\nbreak");
66 }
67 profiler.BeginGpu();
68 auto* output=rtv.Get();gpu.Context()->OMSetRenderTargets(1,&output,nullptr);
69 const D3D11_VIEWPORT viewport{0,0,128,128,0,1};gpu.Context()->RSSetViewports(1,&viewport);
70 gpu.Context()->OMSetDepthStencilState(depth.Get(),0);
71 const float clear[]={0,0,0,1};gpu.Context()->ClearRenderTargetView(output,clear);
72 {
73 ProfileScope11 displays("Cockpit display rendering");ProfileDisplayDetail11 detail;
74 HR(sprite.Begin(gpu.Context()),"sprite Begin");
75 for(int screen=0;screen<2;++screen)
76 {
77 ProfileCpuScope11 screenTimer("Screen",screen==0 ? "PFD" : "EICAS");
78 {
79 ProfileCpuScope11 page("Page","Main");ProfileCpuScope11 instrument("Repeated instrument #1");
80 for(int call=0;call<3;++call)
81 {
82 RECT rect{4,4+screen*40,124,40+screen*40};
83 Check(font.Draw(&sprite,screen==0 ? L"Altitude 15000" : L"Engine 100",-1,&rect,DT_NOCLIP,0xffffffff)>0,"draw instrument text");
84 if(call==0)HR(sprite.Flush(),"instrument-local flush");
85 }
86 }
87 HR(sprite.Flush(),"screen flush");
88 }
89 HR(sprite.End(),"sprite End");
90 }
91 Check(!profiler.DisplayDetailActive(),"display detail ends with display pass");
92 profiler.EndGpu();{ProfileScope11 present("Present wait",false);}profiler.EndFrame();
93 gpu.Context()->CopyResource(staging.Get(),target.Get());
94 D3D11_MAPPED_SUBRESOURCE mapped{};HR(gpu.Context()->Map(staging.Get(),0,D3D11_MAP_READ,0,&mapped),"read pixels");
95 std::vector<uint32_t> pixels(128*128);
96 for(unsigned y=0;y<128;++y)memcpy(pixels.data()+y*128,static_cast<const char*>(mapped.pData)+y*mapped.RowPitch,128*4);
97 gpu.Context()->Unmap(staging.Get(),0);
98 if(frame==0)
99 {
100 bool visible=false;for(auto pixel:pixels)visible|=(pixel&0xffffff)!=0;
101 Check(visible,"font fixture actually renders visible text");baseline=pixels;
102 }
103 else Check(pixels==baseline,"profiling preserves rendered pixels");
104 }
105 std::filesystem::remove("render-profile.capture");
106 Sleep(150);
107 for(int i=0;i<30;++i){profiler.BeginFrame(gpu.Device(),gpu.Context(),128,128,1,false,.016);Sleep(2);}
108 profiler.Reset();
109 ComPtr<ID3D11InfoQueue> messages;HR(gpu.Device()->QueryInterface(IID_PPV_ARGS(&messages)),"debug queue");
110 for(UINT64 i=0;i<messages->GetNumStoredMessages();++i)
111 {
112 SIZE_T size=0;messages->GetMessage(i,nullptr,&size);std::vector<char> bytes(size);
113 auto* message=reinterpret_cast<D3D11_MESSAGE*>(bytes.data());HR(messages->GetMessage(i,message,&size),"debug message");
114 Check(message->Severity>D3D11_MESSAGE_SEVERITY_WARNING,message->pDescription);
115 }
116 const auto csv=root/"outputs"/("render-profile-"+std::to_string(GetCurrentProcessId())+".csv");
117 if(!enabled){Check(!std::filesystem::exists(csv),"normal launch creates no capture even with trigger");std::cout<<"PASS: disabled capture; identical pixels; clean D3D11 debug layer\n";return 0;}
118 std::ifstream input(csv);Check(bool(input),"capture exists");std::string line;std::getline(input,line);
119 std::map<std::string,std::map<int,std::string>> paths;
120 unsigned textRows=0,misses=0,uploads=0,localFlushes=0,screenFlushes=0,transients=0,safeLabels=0;
121 std::map<std::string,unsigned> rowsPerFrame;
122 while(std::getline(input,line))
123 {
124 const auto row=Split(line);Check(row.size()==58,"CSV fields remain valid with dynamic labels, collection timings and markers");
125 Check(row[20]=="0" && row[21]=="0","detailed capture has no overflow or dropped frames");
126 auto& tree=paths[row[0]];const int index=std::stoi(row[14]),parent=std::stoi(row[15]);
127 tree[index]=(parent<0 ? "" : tree.at(parent)+"/")+row[16];const auto& path=tree[index];++rowsPerFrame[row[0]];
128 Check(row[16]!="Outside display detail","low-level timers stay within display pass");
129 if(row[16].find("Transient component #")==0){++transients;Check(parent>=0 && tree.at(parent)=="CPU update","owned labels retain hierarchy");}
130 if(row[16]=="CSV label: comma quote\"line break")++safeLabels;
131 if(row[16]=="Text draw")
132 {
133 ++textRows;Check(row[29]=="3" && row[28]=="scope","repeated text is one hierarchical row with three calls");
134 Check(path.find("/Page: Main/Repeated instrument #1/Text draw")!=std::string::npos,"instrument text hierarchy");
135 Check(row[18].empty() && row[19]=="0","CPU detail consumes no GPU timestamps");
136 }
137 if(row[16]=="Text rasterize cache miss"){++misses;Check(row[0]=="1" && row[29]=="1","only cold text rasterizes");}
138 if(row[16]=="Text texture creation"){++uploads;Check(std::stoull(row[31])>0,"text texture payload recorded");}
139 if(row[16]=="Sprite flush")
140 {
141 if(path.find("/Repeated instrument #1/Sprite flush")!=std::string::npos)++localFlushes;
142 if(tree.at(parent)=="GPU frame/Cockpit display rendering/Screen: PFD" || tree.at(parent)=="GPU frame/Cockpit display rendering/Screen: EICAS")++screenFlushes;
143 }
144 }
145 Check(rowsPerFrame.size()==8 && textRows==16 && misses==2 && uploads==2,"cold/warm text counters across eight captured frames");
146 Check(localFlushes==16 && screenFlushes==16,"instrument and screen flush attribution");
147 Check(transients==8*220 && safeLabels==8,"owned CSV-safe names survive query delay");
148 std::cout<<"PASS: 8 frames; 16 instrument text rows; 2 cold text uploads; correct flush parents; >220 CPU rows/frame; identical pixels; clean D3D11 debug layer\n";
149 std::cout<<csv.string()<<'\n';return 0;
150 }
151 catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
152}
HRESULT(WINAPI *CUniBuffer::_ScriptApplyDigitSubstitution)(const SCRIPT_DIGITSUBSTITUTE *
ID3D11DeviceContext * Context() const
Definition Device11.h:66
ID3D11Device * Device() const
Definition Device11.h:65
HRESULT Create(const DeviceOptions11 &options={})
Definition Device11.cpp:84
int Draw(Sprite11 *sprite, const wchar_t *text, int count, RECT *rectangle, UINT format, uint32_t color)
Definition Font11.cpp:215
HRESULT Initialize(ID3D11Device *device, const LOGFONTW &description)
Definition Font11.cpp:22
static FrameProfiler11 & Get()
HRESULT Initialize(ID3D11Device *device, const std::filesystem::path &shaderDirectory={})
Definition Sprite11.cpp:26
HRESULT Begin(ID3D11DeviceContext *context, const SpriteOptions11 &options={})
Definition Sprite11.cpp:47
path
Definition erin.py:153