--- a/rts/Game/WebbarGroundTrackDelta.h +++ b/rts/Game/WebbarGroundTrackDelta.h @@ -8,6 +8,7 @@ #include namespace WebbarGroundTrackDelta { constexpr uint32_t Budget=16u*1024u*1024u, HeaderWords=24, PageWords=4096u*24u, MaxRows=174719; +constexpr uint32_t MaxSources=MaxRows*2u; using Row=std::array; using Scope=std::array; constexpr std::array CompactWords{4,5,6,7,8,9,10,11,12,20,21,25,28,29,30,31,32,33,34,35,36,37,38,39}; @@ -21,6 +22,8 @@ std::vector rows; std::vector keys,values,ids,seen,order,free,changed,retired; Message message; + struct SourceCache {uint32_t stamp=0,id=0,slot=0;}; + std::vector sourceCache;std::vector sourceSeen,inputSlots,inputIds; uint32_t Home(uint32_t id)const{return(id*2654435761u)&mask;} uint32_t Bucket(uint32_t id)const{auto at=Home(id);while(keys[at]&&keys[at]!=id)at=(at+1)&mask;return at;} uint32_t Find(uint32_t id)const{if(keys.empty())return 0;const auto at=Bucket(id);return keys[at]?values[at]+1:0;} @@ -41,7 +44,57 @@ } public: Packer()=default; - void Invalidate(){std::fill(keys.begin(),keys.end(),0);std::fill(ids.begin(),ids.end(),0);std::fill(seen.begin(),seen.end(),0);free.clear();changed.clear();retired.clear();used=count=revision=0;valid=false;} + void ForgetStampedSources(){std::fill(sourceCache.begin(),sourceCache.end(),SourceCache{});std::fill(sourceSeen.begin(),sourceSeen.end(),0);} + struct Stamped {const void* record;uint32_t index,stamp;}; + // Private feasibility API: stamps are authenticated native-policy mutation + // revisions, never caller-controlled wire data. Existing Pack is unchanged. + template const Message& PackStamped(uint32_t n,const Scope& nextScope,uint32_t sourceCount,At&& at){ + if(n>MaxRows||WireBytes(uint64_t(n)*24)>Budget)throw std::runtime_error("Native track logical-frame budget"); + if(sourceCount>MaxSources)throw std::runtime_error("Private source stamp bound"); + Ensure(n); + auto reserveBounded=[](auto& v,uint32_t needed,uint32_t limit){if(v.capacity()>=needed)return;size_t next=std::max(size_t(32),v.capacity());while(next const Message& { + auto& result=Snapshot(n,nextScope,[&](uint32_t i){return at(i).record;}); + std::fill(sourceSeen.begin(),sourceSeen.end(),0); + for(uint32_t i=0;i=sourceCount||!e.stamp||sourceSeen[e.index])throw std::runtime_error("Private source stamp identity");sourceSeen[e.index]=1;sourceCache[e.index]={e.stamp,ids[i],i+1};} + std::fill(sourceSeen.begin(),sourceSeen.end(),0);return result; + }; + if(!valid||scope!=nextScope||wrapped)return snapshot(); + changed.clear();retired.clear();bool orderChanged=n!=count; + if(++revision>=0x7fffffffu){std::fill(seen.begin(),seen.end(),0);std::fill(sourceSeen.begin(),sourceSeen.end(),0);revision=1;} + for(uint32_t i=0;i=sourceCount||!e.stamp||sourceSeen[e.index]==revision)throw std::runtime_error("Private source stamp identity");sourceSeen[e.index]=revision; + const auto c=sourceCache[e.index];const bool same=c.stamp==e.stamp&&c.id; + const auto id=same?c.id:Identity(e.record); + const auto found=(c.id==id&&c.slot&&c.slot<=used&&ids[c.slot-1]==id)?c.slot:Find(id); + inputSlots[i]=found;inputIds[i]=id; + if(found){if(seen[found-1]==revision)throw std::runtime_error("Duplicate current native track");seen[found-1]=revision;} + } + for(uint32_t slot=0;slot=capacity)throw std::runtime_error("Native track cache full");slot=used++;}else{slot=free.back();free.pop_back();}Insert(id,slot);ids[slot]=id;seen[slot]=revision;} + if(seen[slot]!=revision)throw std::runtime_error("Duplicate new native track");seen[slot]=revision|0x80000000u; + if(dirty||c.stamp!=e.stamp||c.id!=id||c.slot!=slot+1){const auto row=Compact(e.record);if((row[20]>>8)!=id)throw std::runtime_error("Private native row changed during publication");dirty=dirty||rows[slot]!=row;if(dirty)rows[slot]=row;} + if(dirty)changed.push_back(slot); + sourceCache[e.index]={e.stamp,id,slot+1}; + if(order[i]!=slot){order[i]=slot;orderChanged=true;} + } + for(uint32_t i=0;i=WireBytes(uint64_t(n)*24)||WireBytes(payloadWords)>Budget)return snapshot(); + Begin(n,false,base,orderChanged,size_t(payloadWords));message.payload.push_back(uint32_t(retired.size()));message.payload.insert(message.payload.end(),retired.begin(),retired.end()); + for(auto slot:changed){message.payload.push_back(slot);message.payload.insert(message.payload.end(),3,0);message.payload.insert(message.payload.end(),rows[slot].begin(),rows[slot].end());} + if(orderChanged)message.payload.insert(message.payload.end(),order.begin(),order.begin()+n);return message; + } + size_t StampedReservedBytes()const{return ReservedBytes()+sourceCache.capacity()*sizeof(SourceCache)+(sourceSeen.capacity()+inputSlots.capacity()+inputIds.capacity())*4;} + + void Invalidate(){std::fill(sourceCache.begin(),sourceCache.end(),SourceCache{});std::fill(sourceSeen.begin(),sourceSeen.end(),0);std::fill(keys.begin(),keys.end(),0);std::fill(ids.begin(),ids.end(),0);std::fill(seen.begin(),seen.end(),0);free.clear();changed.clear();retired.clear();used=count=revision=0;valid=false;} template const Message& Pack(uint32_t n,const Scope& nextScope,At&& at){ if(n>MaxRows||WireBytes(uint64_t(n)*24)>Budget)throw std::runtime_error("Native track logical-frame budget");Ensure(n);const auto base=serial;const bool wrapped=++serial==0;if(wrapped)serial=1; if(!valid||scope!=nextScope||wrapped)return Snapshot(n,nextScope,at); --- a/rts/Game/WebbarNativeGroundTracks.h +++ b/rts/Game/WebbarNativeGroundTracks.h @@ -13,6 +13,23 @@ bool reported=false,deltaFailed=false; std::unique_ptr delta; std::vector deltaRows; + std::vector deltaSourceStamps; + std::unique_ptr deltaOracle; + uint32_t oracleChecks=0; + void ReserveSourceStamps(){ + if(decals.size()>deltaSourceStamps.capacity()){size_t next=std::max(size_t(32),deltaSourceStamps.capacity());while(nextWebbarGroundTrackDelta::MaxSources){delta->ForgetStampedSources();deltaSourceStamps.clear();return;} + ReserveSourceStamps(); + if(wrapped){delta->ForgetStampedSources();deltaSourceStamps.assign(decals.size(),version);return;} + deltaSourceStamps.resize(decals.size(),version); + auto range=decalsUpdateList.GetNext(); + while(range){const auto [offset,count]=decalsUpdateList.GetOffsetAndSize(*range);if(offset>deltaSourceStamps.size()||count>deltaSourceStamps.size()-offset)throw std::runtime_error("Native dirty-range bound");std::fill(deltaSourceStamps.begin()+offset,deltaSourceStamps.begin()+offset+count,version);range=decalsUpdateList.GetNext(range);} + }catch(const std::exception& error){deltaFailed=true;delta.reset();deltaOracle.reset();std::vector().swap(deltaSourceStamps);MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackDeltaError'])Module['webbarNativeGroundTrackDeltaError'](UTF8ToString($0));},error.what());}} + + std::array perspective{-2,-2,-2,-2,-2}; Reader():CEventClient("[WebBAR original ground tracks]",314159,false){ Perspective();eventHandler.AddClient(this); @@ -73,7 +90,8 @@ if(uint64_t(count)*96+pages*96>16u*1024u*1024u){reader->reported=true;MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackError'])Module['webbarNativeGroundTrackError']('Original ground track drawing exceeds bounded paged transport (drawable='+$0+', retained='+$1+')');},count,retained);return;} if(reader->decalsUpdateList.NeedUpdate()){ - if(++reader->version==0)++reader->version; + const bool wrapped=++reader->version==0;if(wrapped)++reader->version; + reader->StampChanges(wrapped); reader->decalsUpdateList.ResetNeedUpdateAll(); } @@ -89,19 +107,33 @@ static_assert(offsetof(GroundDecal,userDefined)==160); const auto flags=uint32_t(gu->spectating)|(uint32_t(gu->spectatingFullView)<<1); const WebbarGroundTrackDelta::Scope scope{drawEpoch,generation,reader->epoch,uint32_t(gu->myPlayerNum),uint32_t(gu->myTeam),uint32_t(gu->myAllyTeam),flags,control>>1}; - const auto& delta=reader->delta->Pack(uint32_t(count),scope,[&](uint32_t at){return drawable[at];}); + const bool stamped=reader->decals.size()<=WebbarGroundTrackDelta::MaxSources; + if(stamped&&reader->deltaSourceStamps.size()!=reader->decals.size()){ + reader->ReserveSourceStamps();reader->delta->ForgetStampedSources();reader->deltaSourceStamps.assign(reader->decals.size(),reader->version); + } + if(!stamped){reader->delta->ForgetStampedSources();reader->deltaSourceStamps.clear();} + const auto& delta=stamped?reader->delta->PackStamped(uint32_t(count),scope,uint32_t(reader->decals.size()),[&](uint32_t at){const uint32_t index=uint32_t(drawable[at]-reader->decals.data());return WebbarGroundTrackDelta::Packer::Stamped{drawable[at],index,reader->deltaSourceStamps.at(index)};}):reader->delta->Pack(uint32_t(count),scope,[&](uint32_t at){return drawable[at];}); + // Private observer: original full-validation codec in the same native draw. + // Disabled for performance qualification. It starts no second engine. + if(std::getenv("WEBBAR_GROUND_TRACK_STAMP_ORACLE")){ + if(!reader->deltaOracle)reader->deltaOracle=std::make_unique(); + const auto& original=reader->deltaOracle->Pack(uint32_t(count),scope,[&](uint32_t at){return drawable[at];}); + if(delta.snapshot!=original.snapshot||delta.orderChanged!=original.orderChanged||delta.serial!=original.serial||delta.base!=original.base||delta.count!=original.count||delta.changed!=original.changed||delta.retired!=original.retired||delta.payload!=original.payload)throw std::runtime_error("Native stamped-track oracle differs"); + ++reader->oracleChecks; + MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackPolicyOracle'])Module['webbarNativeGroundTrackPolicyOracle']({checks:$0,count:$1,retained:$2,changed:$3,retired:$4,stamped:!!$5});},reader->oracleChecks,count,retained,delta.changed,delta.retired,int(stamped)); + }else reader->deltaOracle.reset(); std::array header{0x31544757u,3,uint32_t(gs->frameNum),drawEpoch,uint32_t(sequence),0,0,reader->epoch,uint32_t(gu->myPlayerNum),uint32_t(gu->myTeam),uint32_t(gu->myAllyTeam),flags,request.flags}; const float time[]={float(gs->frameNum),spring_tomsecs(globalRendering->grTime)*.001f,(gs->GetLuaSimFrame()+request.fraction)/GAME_SPEED,request.fraction};std::memcpy(header.data()+16,time,sizeof(time));header[21]=generation; WebbarGroundTrackDelta::EmitPages(delta,header,[](const uint32_t* page,size_t bytes){MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTracks'])Module['webbarNativeGroundTracks'](HEAPU8.slice($0,$0+$1).buffer);},page,bytes);}); return; }catch(const std::exception& error){ - reader->deltaFailed=true;reader->delta.reset();std::vector().swap(reader->deltaRows); + reader->deltaFailed=true;reader->delta.reset();reader->deltaOracle.reset();std::vector().swap(reader->deltaSourceStamps);std::vector().swap(reader->deltaRows); MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackDeltaError'])Module['webbarNativeGroundTrackDeltaError'](UTF8ToString($0));},error.what()); // Complete this same native request through the original WGT2 path. The // optional codec must not strand pose admission or disable valid tracks. } } - if(reader->delta){reader->delta.reset();std::vector().swap(reader->deltaRows);} + if(reader->delta){reader->delta.reset();reader->deltaOracle.reset();std::vector().swap(reader->deltaSourceStamps);std::vector().swap(reader->deltaRows);} std::vector packet{0x31544757u,2,uint32_t(gs->frameNum),drawEpoch,uint32_t(sequence),0,96,reader->epoch, uint32_t(gu->myPlayerNum),uint32_t(gu->myTeam),uint32_t(gu->myAllyTeam),uint32_t(gu->spectating)|(uint32_t(gu->spectatingFullView)<<1),request.flags,15,0,0};