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

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

1#define main CityLogoUnitMain
2#include "CityLogoTests.cpp"
3#undef main
4#include "../Viewscreen/CityCampaignLogos.h"
5#include "../Rendering/ClientRenderer11.h"
6#include <d3d11sdklayers.h>
7#include "../Rendering/GifAnimationWorker.h"
8
9template<class Ready> void WaitFor(Ready ready, const char* message)
10{
11 const auto until=GetTickCount64()+10000;
12 while(!ready()){Check(GetTickCount64()<until,message);Sleep(1);}
13}
14
15int main()
16{
17 try
18 {
19 Rendering::Device11 gpu;Check(SUCCEEDED(gpu.Create({D3D_DRIVER_TYPE_WARP,true})),"WARP device");
20 Rendering::DrawContext11 draws;Check(SUCCEEDED(draws.Initialize(gpu,"Debug/NativeShaders11")),"production draw context");
22 std::vector<CityLogos::AssignmentAsset> requests;
23 const auto send=[&](const CityLogos::AssignmentAsset& a){requests.push_back(a);};
24 auto bytes=Header(1,1);Frame(bytes,0,0,1,1,1,3,{1});Frame(bytes,0,0,1,1,1,5,{2});bytes.push_back(0x3b);
25 const auto read=[&](int32_t group)
26 {
27 auto* texture=cache.Texture(group);Check(texture!=nullptr,"available current frame");
28 Rendering::LockedRectangle mapped{};Check(SUCCEEDED(texture->LockRect(0,&mapped,nullptr,Rendering::LockFlags::READONLY)),"texture readback");
29 const DWORD pixel=*static_cast<const DWORD*>(mapped.pBits);Check(SUCCEEDED(texture->UnlockRect(0)),"readback unlock");return pixel;
30 };
31 const auto awaitFrame=[&](int32_t group,unsigned frame,uint64_t now,bool measure=false)
32 {
33 WaitFor([&]{
34 cache.MarkDrawn(group);cache.Update(&device,now,send,measure);
35 return cache.Texture(group) && cache.Measure(group).frame==frame;
36 },"background frame available");
37 };
38 Check(!cache.Texture(4002),"geometry before assignment has no overlay");
39 Check(cache.Assign(4002,"campaign",9) && cache.Assign(-32768,"campaign",9),"groups assigned before artwork");
40 Check(!cache.Texture(4002),"pending asset uses ordinary shading");
41 cache.Update(&device,100,send);Check(requests.size()==1,"shared download");const auto token=requests.back().token;
42 Check(cache.Receive(&device,token,bytes.data(),bytes.size(),100),"GIF transfer accepted");
43 awaitFrame(4002,0,100);
44 Check(cache.Texture(4002)==cache.Texture(-32768),"instances share the GPU asset");
45 Check(read(4002)==0xffff0000,"first GIF frame is straight-alpha red");
46 auto measurement=cache.Measure(4002);
47 Check(measurement.filename && *measurement.filename=="campaign" && !measurement.metadata && measurement.frameCount==2,
48 "draw requests lazy frame statistics without a GPU readback");
49 cache.MarkDrawn(4002);cache.Update(&device,101,send);
50 Check(!cache.Measure(4002).metadata,"ADVIEW off performs no GIF metadata analysis");
51 cache.MarkDrawn(-32768);cache.Update(&device,102,send,true);measurement=cache.Measure(4002);
52 Check(measurement.metadata && measurement.metadata->alphaPixels==1 && std::abs(measurement.metadata->meanLuminance-.2126)<1e-9,
53 "current composed GIF frame supplies exact alpha and luminance metadata");
54 const auto hash=measurement.hash;
55 auto* texture=cache.Texture(4002);cache.MarkDrawn(4002);cache.Update(&device,130,send,true);
56 awaitFrame(4002,1,130,true);
57 Check(read(-32768)==0xff00ff00 && cache.Texture(4002)==texture,"animation advances in place once per frame");
58 measurement=cache.Measure(4002);
59 Check(measurement.metadata && measurement.metadata->hash==hash && measurement.frame==1 &&
60 cache.Measure(-32768).metadata==measurement.metadata,"groups share statistics while animation retains one creative identity");
61 Check(measurement.average.analyzedFraction==1 && std::abs(measurement.average.luminance-(.2126*3+.7152*5)/8)<1e-9,
62 "whole-GIF summary respects unequal frame delays");
63 Check(cache.Assign(4002,"CAMPAIGN",9),"repeat packet uses cache");cache.Update(&device,131,send);
64 Check(requests.size()==1 && read(4002)==0xff00ff00,"cache hit neither requests nor restarts animation");
65 Check(!measurement.lifetime.expired(),"duplicate assignment preserves pending measurement validity");
66 Check(cache.Assign(4002,"next",5),"new assignment clears old overlay");Check(!cache.Texture(4002),"no stale artwork during replacement");
67 Check(measurement.lifetime.expired() && cache.Texture(-32768)==texture,
68 "group reassignment invalidates pending exposure even when another group retains the old GIF");
69 cache.Update(&device,140,send);const auto old=requests.back().token;
70 Check(cache.Assign(4002,"latest",7),"replace pending assignment");cache.Update(&device,141,send);const auto latest=requests.back().token;
71 Check(!cache.Receive(&device,old,bytes.data(),bytes.size(),142) && !cache.Texture(4002),"stale download rejected");
72 Check(!cache.Receive(&device,latest,nullptr,0,143),"explicit transfer failure");
73 cache.Update(&device,30142,send);Check(requests.back().token!=latest,"retry has new correlation token");
74 Check(cache.Receive(&device,requests.back().token,bytes.data(),bytes.size(),30142),"late retry recovers assignment");
75 awaitFrame(4002,0,30142);
76 Check(read(4002)==0xffff0000 && cache.Texture(4002)!=cache.Texture(-32768),"new filename has independent playback");
77 Check(!cache.Receive(&device,0,bytes.data(),bytes.size(),30143),"vehicle ordinal cannot enter GIF cache");
78 cache.Reset();Check(!cache.Texture(4002) && !cache.Texture(-32768),"device teardown releases association and textures");
79 Check(measurement.lifetime.expired(),"pending billboard queries can reject removed campaign assets");
80
81 // Material lookup also happens for culled batches; only drawn signs
82 // keep shared artwork awake, independently of ADVIEW.
83 Check(cache.Assign(1,"visible",8) && cache.Assign(2,"visible",8),"visibility fixture shares artwork");
84 cache.Update(&device,10000,send);
85 Check(cache.Receive(&device,requests.back().token,bytes.data(),bytes.size(),10000),"visibility fixture received");
86 awaitFrame(1,0,10000);
87 texture=cache.Texture(1);cache.Measure(1);cache.MarkDrawn(999);
88 cache.Update(&device,10040,send,true);
89 Check(read(1)==0xffff0000 && cache.Measure(1).frame==0 && !cache.Measure(1).metadata,
90 "undrawn artwork skips decoding, texture updates and ADVIEW analysis despite lookups");
91 cache.MarkDrawn(2);cache.MarkDrawn(1);cache.Update(&device,10050,send);
92 awaitFrame(1,1,10050);
93 Check(read(1)==0xff00ff00 && cache.Texture(2)==texture && cache.Measure(1).frame==1,
94 "either visible group wakes the shared animation with ADVIEW disabled");
95 cache.Update(&device,10080,send,true);cache.Update(&device,10100,send,true);
96 Check(read(2)==0xff00ff00 && cache.Measure(2).frame==1 && !cache.Measure(2).metadata,
97 "draw requests are consumed and off-screen artwork stays paused");
98 cache.MarkDrawn(2);cache.Update(&device,10170,send,true);
99 awaitFrame(1,0,10170,true);
100 Check(read(1)==0xffff0000 && cache.Texture(1)==texture && cache.Measure(1).frame==0,
101 "returning artwork catches up across skipped loops without replacing its texture");
102 Check(cache.Measure(1).metadata!=nullptr,"visible artwork resumes metadata analysis");
103 cache.Reset();
104
105 // Two paused partial analyses must release their slots so newly visible
106 // artwork can finish its bounded scan instead of starving indefinitely.
107 CityCampaignLogos largeCache;std::vector<CityLogos::AssignmentAsset> largeRequests;
108 const auto largeSend=[&](const CityLogos::AssignmentAsset& a){largeRequests.push_back(a);};
109 auto largeBytes=Header(512,512);Frame(largeBytes,0,0,1,1,1,3,{1});largeBytes.push_back(0x3b);
110 Check(largeCache.Assign(1,"large-a",8) && largeCache.Assign(2,"large-b",8) && largeCache.Assign(3,"large-c",8),
111 "partial analysis fixtures assigned");
112 largeCache.Update(&device,20000,largeSend);
113 for(const auto& request:largeRequests)
114 Check(largeCache.Receive(&device,request.token,largeBytes.data(),largeBytes.size(),20000),"large fixture received");
115 WaitFor([&]{largeCache.Update(&device,20000,largeSend);
116 return largeCache.Texture(1) && largeCache.Texture(2) && largeCache.Texture(3);},"initial large frames available");
117 for(int group=1;group<=3;++group)largeCache.Measure(group);
118 for(unsigned tick=0;tick<2;++tick)
119 {
120 for(int group=1;group<=3;++group)largeCache.MarkDrawn(group);
121 largeCache.Update(&device,20001+tick,largeSend,true);
122 }
123 for(unsigned tick=0;tick<4;++tick)
124 {
125 largeCache.MarkDrawn(3);largeCache.Update(&device,20003+tick,largeSend,true);
126 }
127 Check(!largeCache.Measure(1).metadata && !largeCache.Measure(2).metadata && largeCache.Measure(3).metadata,
128 "off-screen partial snapshots release analysis slots for visible artwork");
129 for(unsigned tick=0;tick<4;++tick)
130 {
131 largeCache.MarkDrawn(1);largeCache.Update(&device,20007+tick,largeSend,true);
132 }
133 Check(largeCache.Measure(1).metadata && largeCache.Measure(1).metadata->alphaPixels==1,
134 "resumed analysis restarts a discarded partial scan correctly");
135 largeCache.Reset();
136
137 // Exercise skipped frames, partial patches, disposal and looping through
138 // the actual background worker, comparing every result to the old decoder.
139 auto sequence=Header(512,512);
140 for(unsigned i=0;i<128;++i)Frame(sequence,i%16,i/16,1,1,1+i%3,1,{static_cast<unsigned char>(1+i%3)});
141 sequence.push_back(0x3b);
142 GifAnimationDecoder reference;Check(SUCCEEDED(reference.Open(sequence.data(),sequence.size())),"reference GIF opens");
143 {
144 GifAnimationWorker worker;
145 auto state=worker.Open(sequence.data(),sequence.size());
146 std::shared_ptr<const GifAnimationWorker::Result> completed;
147 WaitFor([&]{completed=state->Take();return bool(completed);},"initial worker decode");
148 for(size_t target:{size_t(127),size_t(0),size_t(83),size_t(2)})
149 {
150 worker.Request(state,target);
151 WaitFor([&]{completed=state->Take();return bool(completed);},"worker catch-up decode");
152 const std::vector<uint32_t>* expected=nullptr;
153 Check(SUCCEEDED(reference.RenderFrame(target,expected)) && completed->frame==target &&
154 completed->pixels==*expected,"asynchronous catch-up preserves exact disposal pixels");
155 }
156 // While publication is blocked, the game can keep polling and using
157 // its last immutable result; Take must not wait on this mutex.
158 std::atomic<bool> held{false},release{false};
159 std::thread publisher([&]{
160 std::unique_lock<std::mutex> hold(state->resultMutex);held.store(true);
161 while(!release.load())Sleep(1);
162 });
163 while(!held.load())Sleep(1);
164 worker.Request(state,126);
165 const bool didNotWait=!state->Take();
166 release.store(true);publisher.join();
167 Check(didNotWait,"renderer never waits for a busy publication");
168 WaitFor([&]{completed=state->Take();return bool(completed);},"blocked publication recovers");
169 worker.Cancel(state);
170 for(unsigned i=0;i<20;++i)
171 {
172 auto cancelled=worker.Open(sequence.data(),sequence.size());
173 worker.Request(cancelled,127);worker.Cancel(cancelled);
174 }
175 auto invalid=worker.Open("broken",6);
176 WaitFor([&]{return FAILED(invalid->error.load());},"invalid background decode is reported");
177 worker.Cancel(invalid);
178 }
179 // A malformed but correlated download fails asynchronously and retries.
180 Check(cache.Assign(3,"broken",7),"malformed assignment");
181 cache.Update(&device,40000,send);
182 Check(cache.Receive(&device,requests.back().token,"broken",6,40000),"malformed transfer queued");
183 WaitFor([&]{cache.Update(&device,40000,send);return !cache.HasAnimatedArtwork();},"failed decode leaves cache");
184 const auto beforeRetry=requests.size();cache.Update(&device,70000,send);
185 Check(requests.size()==beforeRetry+1 && !cache.Texture(3),"failed worker decode schedules a fresh download");
186 cache.Reset();
187
188 Microsoft::WRL::ComPtr<ID3D11InfoQueue> queue;Check(SUCCEEDED(gpu.Device()->QueryInterface(IID_PPV_ARGS(&queue))),"debug queue available");
189 for(UINT64 i=0;i<queue->GetNumStoredMessages();++i)
190 {
191 SIZE_T size=0;queue->GetMessage(i,nullptr,&size);std::vector<unsigned char> storage(size);
192 auto* message=reinterpret_cast<D3D11_MESSAGE*>(storage.data());queue->GetMessage(i,message,&size);
193 Check(message->Severity>D3D11_MESSAGE_SEVERITY_WARNING,"no GPU integration warnings");
194 }
195 std::cout<<"PASS: "<<checks<<" production city cache/GIF/texture checks on WARP\n";return 0;
196 }
197 catch(const std::exception& e){std::cerr<<"FAIL: "<<e.what()<<'\n';return 1;}
198}
SCRIPT_CONTROL SCRIPT_STATE const void DWORD
bool Receive(Rendering::RenderDevice *device, int32_t token, const void *bytes, size_t size, uint64_t now)
Rendering::RenderTexture * Texture(int32_t group) const
bool Assign(int32_t group, const char *filename, size_t capacity)
bool HasAnimatedArtwork() const
void Update(Rendering::RenderDevice *device, uint64_t now, const std::function< void(const CityLogos::AssignmentAsset &)> &send, bool measure=false)
void MarkDrawn(int32_t group) const
Measurement Measure(int32_t group) const
HRESULT Open(const wchar_t *path)
HRESULT RenderFrame(std::size_t index, const std::vector< std::uint32_t > *&pixels)
std::shared_ptr< State > Open(const void *bytes, size_t size)
void Cancel(const std::shared_ptr< State > &state)
void Request(const std::shared_ptr< State > &state, size_t frame)
ID3D11Device * Device() const
Definition Device11.h:65
HRESULT Create(const DeviceOptions11 &options={})
Definition Device11.cpp:84
HRESULT Initialize(Device11 &device, const std::filesystem::path &shaderDirectory={})
constexpr uint32_t READONLY
const Rendering::AdCreativeMetadata * metadata