--- a/rts/Game/WebbarNativeGroundTracks.h +++ b/rts/Game/WebbarNativeGroundTracks.h @@ -45,28 +45,58 @@ else reader->Perspective(); } static void Free(){delete reader;reader=nullptr;} +// GroundDecalsVertProg.glsl rejects alphaMax <= 0 before emitting geometry. +// Keep the original CPU history untouched: a different native draw time (e.g. +// after pause/seek) can make one of these entries drawable again. The error +// allowance retains the floating-point boundary on both GPU implementations. +static bool DrawVisible(const GroundDecal& decal,float adjustedFrame){ + const double decay=(double(adjustedFrame)-decal.createFrameMax)*decal.alphaFalloff; + const double alphaMax=(double(decal.alpha)-decay)*decal.visMult; + const double roundoff=8.0*std::numeric_limits::epsilon()*(std::abs(double(decal.alpha))+std::abs(decay))*std::abs(double(decal.visMult)); + return !(alphaMax < -roundoff); +} static void Publish(const WebbarBeamDrawRequest& request,int32_t sequence,uint32_t drawEpoch){ if(!Enabled()||!(request.flags&2u)||request.frame!=uint32_t(gs->frameNum))return; Prepare();if(!reader||reader->reported)return; // Optional WGT1 drawing state; bridge9, simulation and saved command format // stay intact. UV words carry authenticated original material IDs; browser // atlas addressing replaces them, while all native geometry/age bits survive. - size_t count=0;for(const auto& decal:reader->decals)if(decal.info.type==uint8_t(GroundDecal::Type::DECAL_TRACK))++count; - if(count>65536){reader->reported=true;MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackError'])Module['webbarNativeGroundTrackError']('Original ground track drawing packet exceeds bounded transport');});return;} + const float adjustedFrame=float(gs->frameNum)+request.fraction; + size_t count=0,retained=0;for(const auto& decal:reader->decals)if(decal.info.type==uint8_t(GroundDecal::Type::DECAL_TRACK)){++retained;if(DrawVisible(decal,adjustedFrame))++count;} + // Each page stays below the old single-packet bound. Compact native records + // and page headers together retain the existing16MiB logical-frame budget. + const uint64_t pages=std::max(uint64_t(1),(uint64_t(count)+4095)/4096); + 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; reader->decalsUpdateList.ResetNeedUpdateAll(); } - std::vector packet{0x31544757u,1,uint32_t(gs->frameNum),drawEpoch,uint32_t(sequence),uint32_t(count),160,reader->epoch, + 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}; const float time[]={float(gs->frameNum),spring_tomsecs(globalRendering->grTime)*.001f,(gs->GetLuaSimFrame()+request.fraction)/GAME_SPEED,request.fraction}; for(float value:time){uint32_t word;std::memcpy(&word,&value,4);packet.push_back(word);}packet.resize(24);packet[20]=reader->version;packet[21]=generation; + packet.reserve(24+4096*24);packet[23]=uint32_t(count);size_t offset=0; + const auto flush=[&](){ + packet[5]=uint32_t((packet.size()-24)/24);packet[22]=uint32_t(offset); + MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTracks'])Module['webbarNativeGroundTracks'](HEAPU8.slice($0,$0+$1).buffer);},packet.data(),packet.size()*4); + offset+=packet[5];packet.resize(24); + }; + static 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}; for(const auto& decal:reader->decals){ - if(decal.info.type!=uint8_t(GroundDecal::Type::DECAL_TRACK))continue; + if(decal.info.type!=uint8_t(GroundDecal::Type::DECAL_TRACK)||!DrawVisible(decal,adjustedFrame))continue; static_assert(offsetof(GroundDecal,userDefined)==160); - const size_t at=packet.size();packet.resize(at+40);std::memcpy(packet.data()+at,&decal,160); + std::array words;std::memcpy(words.data(),&decal,160); + // Omitted fields are exact original AddTrack defaults, not quantisation. + static constexpr std::array zeroWords{0,1,2,3,13,14,15,16,17,18,19,22,23,24,26}; + uint32_t zero=0;for(size_t word:zeroWords)zero|=words[word]; + if(zero||words[27]!=0x3f800000u){ + reader->reported=true;MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTrackError'])Module['webbarNativeGroundTrackError']('Original ground track compact defaults differ');});return; + } + for(size_t word:compactWords)packet.push_back(words[word]); + if(packet.size()==24+4096*24)flush(); } - MAIN_THREAD_EM_ASM({if(Module['webbarNativeGroundTracks'])Module['webbarNativeGroundTracks'](HEAPU8.slice($0,$0+$1).buffer);},packet.data(),packet.size()*4); + if(packet.size()>24||count==0)flush(); } } static void PrepareWebbarNativeGroundTracks(){WebbarNativeGroundTracks::Prepare();} --- a/rts/Game/WebbarNativeExplosionDecals.h +++ b/rts/Game/WebbarNativeExplosionDecals.h @@ -9,6 +9,9 @@ class WebbarNativeExplosionDecalReader final:public CLuaHandle { public: + static constexpr size_t PageRecords=4096,MaxBytes=16u*1024u*1024u; + static bool Fits(size_t count){return count*80u+std::max(size_t(1),(count+PageRecords-1)/PageRecords)*96u<=MaxBytes;} + const char* failure="Original persistent decal policy unavailable"; bool ready=false,reported=false; uint32_t epoch=0; std::array perspective{-2,-2,-2,-2,-2}; @@ -85,8 +88,8 @@ } bool State(std::vector& packet){ Perspective();if(!ready||packet.size()!=24)return false; - const int top=lua_gettop(L);lua_getglobal(L,"WebbarExplosionDecalStandaloneState"); - bool ok=RunCallIn(L,LuaHashString("WebbarExplosionDecalStandaloneState"),0,1)&&lua_istable(L,-1); + const int top=lua_gettop(L);lua_getglobal(L,"WebbarExplosionDecalStandaloneNativeState"); + bool ok=RunCallIn(L,LuaHashString("WebbarExplosionDecalStandaloneNativeState"),0,1)&&lua_istable(L,-1); const int state=lua_gettop(L); const auto integer=[&](const char* key,uint32_t& word){ lua_getfield(L,state,key);const double value=lua_tonumber(L,-1); @@ -97,18 +100,19 @@ if(ok){ lua_getfield(L,state,"groups");const int groups=lua_gettop(L);ok=lua_istable(L,groups)&&lua_objlen(L,groups)==3; for(int group=1;ok&&group<=3;++group){ - lua_rawgeti(L,groups,group);const int rows=lua_gettop(L);ok=lua_istable(L,rows); - const size_t count=ok?lua_objlen(L,rows):0; - ok=ok&&count<=65536-packet[5];if(ok){packet[12+group]=count;packet[5]+=count;} - for(size_t i=1;ok&&i<=count;++i){ - lua_rawgeti(L,rows,i);const int row=lua_gettop(L);ok=lua_istable(L,row)&&lua_objlen(L,row)==20; - for(int j=1;ok&&j<=20;++j){ - lua_rawgeti(L,row,j);const float value=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(value); - if(ok){uint32_t word;std::memcpy(&word,&value,4);packet.push_back(word);}lua_pop(L,1); - } - lua_pop(L,1); + lua_rawgeti(L,groups,group);const int vbo=lua_gettop(L);ok=lua_istable(L,vbo); + lua_getfield(L,vbo,"usedElements");const double size=lua_tonumber(L,-1); + ok=ok&&lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(size)&&size>=0&&size<=(MaxBytes-96u)/80u-packet[5]&&std::floor(size)==size; + if(std::isfinite(size)&&size>(MaxBytes-96u)/80u-packet[5])failure="Original persistent decal transport exceeds 16 MiB"; + const size_t count=ok?size_t(size):0;lua_pop(L,1); + lua_getfield(L,vbo,"instanceStep");ok=ok&&lua_type(L,-1)==LUA_TNUMBER&&lua_tonumber(L,-1)==20;lua_pop(L,1); + lua_getfield(L,vbo,"instanceData");const int data=lua_gettop(L);ok=ok&&lua_istable(L,data)&&lua_objlen(L,data)>=count*20; + if(ok){packet[12+group]=count;packet[5]+=count;if(!Fits(packet[5])){failure="Original persistent decal transport exceeds 16 MiB";ok=false;}} + for(size_t i=1;ok&&i<=count*20;++i){ + lua_rawgeti(L,data,i);const float value=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(value); + if(ok){uint32_t word;std::memcpy(&word,&value,4);packet.push_back(word);}lua_pop(L,1); } - lua_pop(L,1); + lua_pop(L,2); } lua_pop(L,1); } @@ -128,16 +132,26 @@ static void Publish(const WebbarBeamDrawRequest& request,int32_t sequence,uint32_t drawEpoch){ if(!Enabled()||!(request.flags&2u)||request.frame!=uint32_t(gs->frameNum))return; Prepare();if(reader&&reader->reported)return; - // Optional WXD1 visual state. It changes neither bridge9 nor saved commands. - std::vector packet{0x31445857u,1,uint32_t(gs->frameNum),drawEpoch,uint32_t(sequence),0,80,reader?reader->epoch:0, + // Optional WXD2 paged visual state. Original rows and group order are unchanged. It changes neither bridge9 nor saved commands. + std::vector packet{0x31445857u,2,uint32_t(gs->frameNum),drawEpoch,uint32_t(sequence),0,80,reader?reader->epoch:0, uint32_t(gu->myPlayerNum),uint32_t(gu->myTeam),uint32_t(gu->myAllyTeam),uint32_t(gu->spectating)|(uint32_t(gu->spectatingFullView)<<1),request.flags,0,0,0}; const float time[]={float(gs->frameNum),spring_tomsecs(globalRendering->grTime)*.001f,(gs->GetLuaSimFrame()+request.fraction)/GAME_SPEED,request.fraction}; for(float value:time){uint32_t word;std::memcpy(&word,&value,4);packet.push_back(word);}packet.resize(24);packet[21]=generation; if(!reader||!reader->State(packet)){ if(reader)reader->reported=true; - MAIN_THREAD_EM_ASM({if(Module['webbarNativeExplosionDecalError'])Module['webbarNativeExplosionDecalError']('Original persistent decal policy unavailable');});return; + const char* error=reader?reader->failure:"Original persistent decal policy unavailable"; + MAIN_THREAD_EM_ASM({if(Module['webbarNativeExplosionDecalError'])Module['webbarNativeExplosionDecalError'](UTF8ToString($0));},error);return; } - MAIN_THREAD_EM_ASM({if(Module['webbarNativeExplosionDecals'])Module['webbarNativeExplosionDecals'](HEAPU8.slice($0,$0+$1).buffer);},packet.data(),packet.size()*4); + // One bounded native state read, then synchronous pages; the browser couples + // only the complete logical frame. The native widget retains all history. + const size_t total=packet[5];std::vector page;page.reserve(24+std::min(total,WebbarNativeExplosionDecalReader::PageRecords)*20); + for(size_t offset=0;;){ + const size_t count=std::min(WebbarNativeExplosionDecalReader::PageRecords,total-offset); + page.assign(packet.begin(),packet.begin()+24);page[5]=count;page[22]=offset;page[23]=total; + page.insert(page.end(),packet.begin()+24+offset*20,packet.begin()+24+(offset+count)*20); + MAIN_THREAD_EM_ASM({if(Module['webbarNativeExplosionDecals'])Module['webbarNativeExplosionDecals'](HEAPU8.slice($0,$0+$1).buffer);},page.data(),page.size()*4); + if(offset+count>=total)break;offset+=count; + } } } static void PrepareWebbarNativeExplosionDecals(){WebbarNativeExplosionDecals::Prepare();} --- a/rts/Game/WebbarExplosionDecalPolicy.h +++ b/rts/Game/WebbarExplosionDecalPolicy.h @@ -4718,6 +4718,12 @@ Sync=function(name,...) if not ready or(name~='explosion_light' and name~='cob_UnitScriptDecal')then return false end assert(actions[name])(name,...);return true + end, + NativeState=function() + assert(ready and not removed,'Original decal host unavailable') + -- Borrow the unchanged original VBO storage for this synchronous native read. + -- The native reader validates counts, stride and finite active values. + return {frame=Spring.GetGameFrame(),version=WG.decalsgl4.GetVersion(),groups=WG.decalsgl4.GetVBOData()} end, State=function() assert(ready and not removed,'Original decal host unavailable') @@ -4764,4 +4770,6 @@ function WebbarExplosionDecalStandaloneSync(...)return standaloneDecals.Sync(...)end function WebbarExplosionDecalStandaloneState()return standaloneDecals.State()end +function WebbarExplosionDecalStandaloneNativeState()return standaloneDecals.NativeState()end + )WEBBAR_DECALS"; --- a/rts/Game/WebbarNativeHealthbarPolicy.h +++ b/rts/Game/WebbarNativeHealthbarPolicy.h @@ -8611,6 +8611,7 @@ end} local removed,ready=false,false local globals,actions,batches,uniforms={},{},{},{} +local nativeCapture=false local function noop()end local widgetHandler={AddAction=noop,RemoveAction=noop, RemoveWidget=function()removed=true;ready=false end, @@ -8633,6 +8634,10 @@ function v:draw(primitive) draw(self,primitive) assert(self.instanceStep==29 and self.usedElements<=65536,'Original deferred-light storage differs') + if nativeCapture then + batches[#batches+1]={name=self.myName,type=assert(uniforms.pointbeamcone),attached=uniforms.attachedtounitID==1,vbo=self} + return + end local batch={name=self.myName,type=assert(uniforms.pointbeamcone),attached=uniforms.attachedtounitID==1,rows={}} for i=1,self.usedElements do local row={params={},piece=self.instanceData[(i-1)*29+25]} @@ -53362,6 +53367,15 @@ batches={};uniforms={} stock:Update(dt);stock:DrawWorld() local total=0;for _,batch in ipairs(batches)do total=total+#batch.rows end + assert(total<=65536,'Original deferred-light publication capacity exceeded') + return {frame=Spring.GetGameFrame(),batches=batches,uniforms=uniforms,config=stock:GetConfigData()} + end, + NativeDraw=function(dt) + if not ready then return nil end + batches={};uniforms={};nativeCapture=true + local ok,failure=pcall(function()stock:Update(dt);stock:DrawWorld()end) + nativeCapture=false;if not ok then error(failure)end + local total=0;for _,batch in ipairs(batches)do total=total+batch.vbo.usedElements end assert(total<=65536,'Original deferred-light publication capacity exceeded') return {frame=Spring.GetGameFrame(),batches=batches,uniforms=uniforms,config=stock:GetConfigData()} end, @@ -53404,6 +53418,10 @@ function GameFrame(frame)ordinaryFrame(frame);invoke('GameFrame',frame)end function PlayerChanged(player)ordinaryPlayer(player);invoke('PlayerChanged',player)end function WebbarDeferredLightSync(...)return invoke('Sync',...)end + function WebbarDeferredLightNativeState(dt) + assert(alive,webbarDeferredLightFailure) + return assert(invoke('NativeDraw',dt),webbarDeferredLightFailure) + end function WebbarDeferredLightState(dt) assert(alive,webbarDeferredLightFailure) return assert(invoke('Draw',dt),webbarDeferredLightFailure) --- a/rts/Game/WebbarDeferredLightReader.inc +++ b/rts/Game/WebbarDeferredLightReader.inc @@ -76,8 +76,8 @@ deferredView.SetFrustumScales(float4(0,0,request.flameCamera[2],request.flameCamera[3]));deferredView.UpdateFrustum();deferredHasCamera=true; const double now=spring_tomsecs(globalRendering->grTime),dt=deferredLastAt>0?(now-deferredLastAt)*.001:0; deferredLastAt=now;if(!std::isfinite(dt)||dt<0)return false; - const int top=lua_gettop(L);lua_getglobal(L,"WebbarDeferredLightState");lua_pushnumber(L,dt); - bool ok=RunCallIn(L,LuaHashString("WebbarDeferredLightState"),1,1)&&lua_istable(L,-1); + const int top=lua_gettop(L);lua_getglobal(L,"WebbarDeferredLightNativeState");lua_pushnumber(L,dt); + bool ok=RunCallIn(L,LuaHashString("WebbarDeferredLightNativeState"),1,1)&&lua_istable(L,-1); const int result=lua_gettop(L); const auto field=[&](int table,const char* name,double& value){ lua_getfield(L,table,name);const bool number=lua_type(L,-1)==LUA_TNUMBER;value=lua_tonumber(L,-1);lua_pop(L,1);return number&&std::isfinite(value); @@ -102,27 +102,26 @@ double shape=0;ok=group>previous&&field(batch,"type",shape)&&shape==shapes[group];previous=group; if(!ok){lua_pop(L,1);break;} lua_getfield(L,batch,"attached");const bool attached=lua_toboolean(L,-1);ok=ok&&lua_type(L,-1)==LUA_TBOOLEAN&&attached==(group>=8);lua_pop(L,1); - lua_getfield(L,batch,"rows");const int rows=lua_gettop(L);ok=ok&&lua_istable(L,rows);const size_t count=ok?lua_objlen(L,rows):0;total+=count;ok=ok&&total<=65536&&(group!=2||count==0); + lua_getfield(L,batch,"vbo");const int vbo=lua_gettop(L);ok=lua_istable(L,vbo); + double used=0,step=0;ok=ok&&field(vbo,"usedElements",used)&&used>=0&&used<=65536-total&&used==std::floor(used)&&field(vbo,"instanceStep",step)&&step==29; + const size_t count=ok?size_t(used):0;total+=count;ok=ok&&(group!=2||count==0); + lua_getfield(L,vbo,"instanceData");const int params=lua_gettop(L);ok=ok&&lua_istable(L,params)&&lua_objlen(L,params)>=count*29; + lua_getfield(L,vbo,"indextoUnitID");const int owners=lua_gettop(L);ok=ok&&(!attached||lua_istable(L,owners)); for(size_t r=1;ok&&r<=count;r++){ - lua_rawgeti(L,rows,r);const int row=lua_gettop(L);ok=lua_istable(L,row); - if(!ok){lua_pop(L,1);break;} std::array data{}; data[0]=uint32_t(shape);data[1]=attached;data[2]=0xffffffffu;data[6]=uint32_t(group); - double piece=0;ok=ok&&field(row,"piece",piece)&&piece>=0&&piece<=65535&&piece==std::floor(piece);data[5]=uint32_t(ok?piece:0); + lua_rawgeti(L,params,(r-1)*29+25);const double piece=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(piece)&&piece>=0&&piece<=65535&&piece==std::floor(piece);lua_pop(L,1);data[5]=uint32_t(ok?piece:0); bool admitted=true; if(ok&&attached){ - double id=0;ok=field(row,"unit",id)&&id>=0&&id<=65535&&id==std::floor(id); + lua_rawgeti(L,owners,r);const double id=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(id)&&id>=0&&id<=65535&&id==std::floor(id);lua_pop(L,1); const CUnit* unit=ok&&idisDead&&!unit->noDraw&&!unit->IsInVoid()&&(gu->spectatingFullView||unit->allyteam==gu->myAllyTeam||unit->IsInLosForAllyTeam(gu->myAllyTeam)); if(admitted){data[2]=unit->id;data[3]=unit->unitDef->id;data[4]=uint32_t(unit->creationFrame);} } - lua_getfield(L,row,"params");const int params=lua_gettop(L);ok=ok&&lua_istable(L,params)&&lua_objlen(L,params)==24; - for(int j=0;ok&&j<24;j++){lua_rawgeti(L,params,j+1);const float value=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(value);if(ok)std::memcpy(&data[8+j],&value,4);lua_pop(L,1);} - lua_pop(L,1); + for(int j=0;ok&&j<24;j++){lua_rawgeti(L,params,(r-1)*29+j+1);const float value=lua_tonumber(L,-1);ok=lua_type(L,-1)==LUA_TNUMBER&&std::isfinite(value);if(ok)std::memcpy(&data[8+j],&value,4);lua_pop(L,1);} if(ok&&admitted){packet.insert(packet.end(),data.begin(),data.end());++packet[5];} - lua_pop(L,1); } - lua_pop(L,2); + lua_pop(L,4); } lua_pop(L,1); }