Files
VoxelForge/Standalone/Source/StreamingWorld.h
T
Fr0zkaandClaude Opus 5.5 97c0455939 standalone: shape-world look round 2 -- shards gone, calmer layered relief, still 0.72 s / 1.6 s cold
- Removed the thin automatic room-support slabs that left floating caps (in-view detached thin
  sheets 9 -> 0; the audit now also fails on isolated small components).
- Bounded 16-voxel floor/wall blend (18 rejected: it created a sub-voxel sheet), 3-voxel
  strata/cave blend, C1 tunnel radius profile (0 radius/slope steps at joins).
- Surface relief: broad octave with amplitude modulation instead of uniform bumps.
- Cold, no cache: 64 m full detail 0.924 s (8 workers) / 0.715 s (16); whole view 2.016 / 1.605 s.
- 1-worker and 6-worker hashes match; 5,642/5,642 passage samples open.
Not solved: regular bands in the narrow passage shafts, a few slivers on tile boundaries, faint
wall/floor plates; close-view mean surface distance to the owner field got worse (see REPORT.md).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MmSoF64BbRFmk9C3p1VsBJ
2026-10-04 04:09:59 +02:00

506 lines
22 KiB
C++

#pragma once
#include "WorldMesher.h"
#include <condition_variable>
#include <algorithm>
#include <array>
#include <bit>
#include <cstdint>
#include <cstring>
#include <fstream>
#include <unordered_set>
struct FStandaloneTileKeyHash
{
std::size_t operator()(const FVoxelTileKey& Key) const noexcept
{ return static_cast<std::size_t>(GetTypeHash(Key)); }
};
struct FStandaloneDesiredTileSet
{
std::vector<FVoxelTileKey> Tiles;
FIntVector CenterChunk=FIntVector::ZeroValue;
int32 MinimumChunkZ=MIN_int32;
int32 MaximumChunkZ=MAX_int32;
int32 StrateTopChunkZ=0;
int32 StrateBottomChunkZ=0;
bool bHasStrate=false;
};
struct FStandaloneCpuTile
{
FVoxelTileKey Key;
uint64 RequestedCrossing=0;
std::vector<FStandaloneCpuVertex> Vertices;
std::vector<uint32> Indices;
uint64 DensitySamples=0;
double WorkerSeconds=0.0;
uint64 DensityHash=14695981039346656037ull;
bool bHasDensityHash=false;
std::array<uint64,VoxelDensityProfile::BucketCount> ProfileCycles{};
};
struct FStandaloneMeshCacheMetadata
{
uint64 SourceDataHash=0;
int32 Seed=0;
int32 ClipRadius=3;
int32 FullResClipLevels=2;
int32 CoarseTileCells=16;
int32 StrateContentCutMinLevel=9;
int32 SkirtCells=0;
int32 LODOctaveDrop=0;
int32 bGenerateSkirts=0;
uint32 AlgorithmVersion=1;
uint32 TunnelMode=0;
uint32 VoxelSizeCm=25;
};
class FStandaloneMeshCache
{
public:
static constexpr uint32 CurrentVersion=1;
void Clear(){Tiles.clear();bLoaded=false;BytesLoaded=0;}
bool IsLoaded()const{return bLoaded;}
size_t Num()const{return Tiles.size();}
uint64 GetBytesLoaded()const{return BytesLoaded;}
const FStandaloneCpuTile* Find(const FVoxelTileKey& Key)const
{
const auto It=Tiles.find(Key);
return It==Tiles.end()?nullptr:&It->second;
}
static bool Save(const std::string& Path,const FStandaloneMeshCacheMetadata& Metadata,
std::vector<FStandaloneCpuTile>& InTiles,std::string& Error)
{
std::vector<FStandaloneCpuTile*> Ordered;
Ordered.reserve(InTiles.size());
for(FStandaloneCpuTile& Tile:InTiles)Ordered.push_back(&Tile);
std::sort(Ordered.begin(),Ordered.end(),[](const FStandaloneCpuTile* A,const FStandaloneCpuTile* B)
{
if(A->Key.Level!=B->Key.Level)return A->Key.Level<B->Key.Level;
if(A->Key.Coord.Z!=B->Key.Coord.Z)return A->Key.Coord.Z<B->Key.Coord.Z;
if(A->Key.Coord.Y!=B->Key.Coord.Y)return A->Key.Coord.Y<B->Key.Coord.Y;
return A->Key.Coord.X<B->Key.Coord.X;
});
std::ofstream File(Path,std::ios::binary|std::ios::trunc);
if(!File){Error="could not open mesh cache for writing: "+Path;return false;}
struct FHeader
{
char Magic[8];uint32 Version;uint32 Endian;uint64 SourceDataHash;
int32 Seed,ClipRadius,FullResClipLevels,CoarseTileCells,StrateContentCutMinLevel;
int32 SkirtCells,LODOctaveDrop,bGenerateSkirts;
uint32 AlgorithmVersion,TunnelMode,VoxelSizeCm;
uint64 TileCount;
};
static_assert(sizeof(FHeader)==80);
const FHeader Header{{'V','F','M','E','S','H','1','\0'},1,0x01020304u,
Metadata.SourceDataHash,Metadata.Seed,Metadata.ClipRadius,Metadata.FullResClipLevels,
Metadata.CoarseTileCells,Metadata.StrateContentCutMinLevel,Metadata.SkirtCells,
Metadata.LODOctaveDrop,Metadata.bGenerateSkirts,Metadata.AlgorithmVersion,
Metadata.TunnelMode,Metadata.VoxelSizeCm,static_cast<uint64>(Ordered.size())};
File.write(reinterpret_cast<const char*>(&Header),sizeof(Header));
struct FRecord
{
int32 Level,X,Y,Z;uint64 Samples,DensityHash;uint32 VertexCount,IndexCount;
};
static_assert(sizeof(FRecord)==40);
for(const FStandaloneCpuTile* Tile:Ordered)
{
if(Tile->Vertices.size()>UINT32_MAX||Tile->Indices.size()>UINT32_MAX
||Tile->Indices.size()%3!=0)
{Error="mesh cache tile exceeds the 32-bit record limits";return false;}
const FRecord Record{Tile->Key.Level,Tile->Key.Coord.X,Tile->Key.Coord.Y,
Tile->Key.Coord.Z,Tile->DensitySamples,Tile->DensityHash,
static_cast<uint32>(Tile->Vertices.size()),static_cast<uint32>(Tile->Indices.size())};
File.write(reinterpret_cast<const char*>(&Record),sizeof(Record));
if(!Tile->Vertices.empty())File.write(reinterpret_cast<const char*>(Tile->Vertices.data()),
static_cast<std::streamsize>(Tile->Vertices.size()*sizeof(FStandaloneCpuVertex)));
if(!Tile->Indices.empty())File.write(reinterpret_cast<const char*>(Tile->Indices.data()),
static_cast<std::streamsize>(Tile->Indices.size()*sizeof(uint32)));
if(!File){Error="mesh cache write failed: "+Path;return false;}
}
File.flush();
if(!File){Error="mesh cache flush failed: "+Path;return false;}
return true;
}
bool Load(const std::string& Path,const FStandaloneMeshCacheMetadata& Expected,std::string& Error)
{
Clear();
std::ifstream File(Path,std::ios::binary);
if(!File){Error="cache file is missing or unreadable";return false;}
struct FHeader
{
char Magic[8];uint32 Version;uint32 Endian;uint64 SourceDataHash;
int32 Seed,ClipRadius,FullResClipLevels,CoarseTileCells,StrateContentCutMinLevel;
int32 SkirtCells,LODOctaveDrop,bGenerateSkirts;
uint32 AlgorithmVersion,TunnelMode,VoxelSizeCm;
uint64 TileCount;
};
static_assert(sizeof(FHeader)==80);
FHeader H{};File.read(reinterpret_cast<char*>(&H),sizeof(H));
const char ExpectedMagic[8]={'V','F','M','E','S','H','1','\0'};
if(!File||std::memcmp(H.Magic,ExpectedMagic,sizeof(ExpectedMagic))!=0
||H.Version!=1||H.Endian!=0x01020304u||H.TileCount>1000000)
{Error="mesh cache header is invalid";return false;}
if(H.SourceDataHash!=Expected.SourceDataHash||H.Seed!=Expected.Seed
||H.ClipRadius!=Expected.ClipRadius||H.FullResClipLevels!=Expected.FullResClipLevels
||H.CoarseTileCells!=Expected.CoarseTileCells
||H.StrateContentCutMinLevel!=Expected.StrateContentCutMinLevel
||H.SkirtCells!=Expected.SkirtCells||H.LODOctaveDrop!=Expected.LODOctaveDrop
||H.bGenerateSkirts!=Expected.bGenerateSkirts
||H.AlgorithmVersion!=Expected.AlgorithmVersion||H.TunnelMode!=Expected.TunnelMode
||H.VoxelSizeCm!=Expected.VoxelSizeCm)
{Error="mesh cache metadata does not match this world configuration";return false;}
struct FRecord
{
int32 Level,X,Y,Z;uint64 Samples,DensityHash;uint32 VertexCount,IndexCount;
};
static_assert(sizeof(FRecord)==40);
std::unordered_map<FVoxelTileKey,FStandaloneCpuTile,FStandaloneTileKeyHash> Loaded;
Loaded.reserve(static_cast<size_t>(H.TileCount)*2+1);
for(uint64 I=0;I<H.TileCount;++I)
{
FRecord Record{};File.read(reinterpret_cast<char*>(&Record),sizeof(Record));
if(!File||Record.Level<0||Record.Level>8||Record.VertexCount>10000000
||Record.IndexCount>60000000)
{Error="mesh cache contains an invalid tile record";return false;}
FStandaloneCpuTile Tile;Tile.Key=FVoxelTileKey(FIntVector(Record.X,Record.Y,Record.Z),Record.Level);
Tile.DensitySamples=Record.Samples;Tile.DensityHash=Record.DensityHash;Tile.bHasDensityHash=true;
Tile.Vertices.resize(Record.VertexCount);Tile.Indices.resize(Record.IndexCount);
if(!Tile.Vertices.empty())File.read(reinterpret_cast<char*>(Tile.Vertices.data()),
static_cast<std::streamsize>(Tile.Vertices.size()*sizeof(FStandaloneCpuVertex)));
if(!Tile.Indices.empty())File.read(reinterpret_cast<char*>(Tile.Indices.data()),
static_cast<std::streamsize>(Tile.Indices.size()*sizeof(uint32)));
if(!File){Error="mesh cache ended inside tile geometry";return false;}
for(const uint32 Index:Tile.Indices)
if(Index>=Tile.Vertices.size())
{Error="mesh cache contains an out-of-range mesh index";return false;}
for(const FStandaloneCpuVertex& Vertex:Tile.Vertices)
for(const float Value:Vertex.Position)
if(!std::isfinite(Value))
{Error="mesh cache contains a non-finite vertex";return false;}
for(const FStandaloneCpuVertex& Vertex:Tile.Vertices)
for(const float Value:Vertex.Normal)
if(!std::isfinite(Value))
{Error="mesh cache contains a non-finite normal";return false;}
if(!Loaded.emplace(Tile.Key,std::move(Tile)).second)
{Error="mesh cache contains duplicate tile keys";return false;}
}
const std::streamoff End=File.tellg();
if(End<0){Error="could not determine mesh cache size";return false;}
BytesLoaded=static_cast<uint64>(End);Tiles.swap(Loaded);bLoaded=true;
return true;
}
private:
std::unordered_map<FVoxelTileKey,FStandaloneCpuTile,FStandaloneTileKeyHash> Tiles;
bool bLoaded=false;
uint64 BytesLoaded=0;
};
struct FStandaloneStreamingUpdate
{
FIntVector CenterChunk=FIntVector::ZeroValue;
int32 DesiredTiles=0;
int32 RetainedResidentTiles=0;
int32 NewJobs=0;
int32 CancelledQueuedJobs=0;
int32 FullResTiles=0;
std::vector<std::pair<uint64,uint64>> CancelledByCrossing;
};
struct FStandaloneTileJob
{
FVoxelTileKey Key;
int32 BandZMin=MIN_int32;
int32 BandZMax=MAX_int32;
uint64 RequestedCrossing=0;
double PriorityDistanceSquared=0.0;
};
struct FStandaloneTileJobHeapOrder
{
bool operator()(const FStandaloneTileJob& A,const FStandaloneTileJob& B)const
{
if(A.PriorityDistanceSquared!=B.PriorityDistanceSquared)
return A.PriorityDistanceSquared>B.PriorityDistanceSquared;
if(A.Key.Level!=B.Key.Level)return A.Key.Level>B.Key.Level;
if(A.Key.Coord.Z!=B.Key.Coord.Z)return A.Key.Coord.Z>B.Key.Coord.Z;
if(A.Key.Coord.Y!=B.Key.Coord.Y)return A.Key.Coord.Y>B.Key.Coord.Y;
return A.Key.Coord.X>B.Key.Coord.X;
}
};
namespace StandaloneWorldMesher
{
inline double TileDistanceSquaredChunks(const FVoxelTileKey& Tile,const FIntVector& Center)
{
const double Scale=static_cast<double>(1<<Tile.Level);
const double DX=(static_cast<double>(Tile.Coord.X)+0.5)*Scale-Center.X;
const double DY=(static_cast<double>(Tile.Coord.Y)+0.5)*Scale-Center.Y;
const double DZ=(static_cast<double>(Tile.Coord.Z)+0.5)*Scale-Center.Z;
return DX*DX+DY*DY+DZ*DZ;
}
inline FStandaloneDesiredTileSet BuildDesiredSet(
const FStandaloneOwnerWorld& World,const FVector& CameraPositionVoxels)
{
FStandaloneDesiredTileSet Desired;
Desired.CenterChunk=CameraChunk(CameraPositionVoxels);
if(World.Settings->bClampViewToStrate)
Desired.bHasStrate=World.Manager->GetStrateChunkZBounds(
Desired.CenterChunk.Z,Desired.StrateTopChunkZ,Desired.StrateBottomChunkZ);
VoxelClipmapDesiredTiles::ResolveVerticalBounds(
Desired.CenterChunk.Z,World.Settings->ViewDistanceUp,World.Settings->ViewDistanceDown,
World.Settings->StrateViewMarginChunks,World.Settings->bClampViewToStrate,
Desired.bHasStrate,Desired.StrateTopChunkZ,Desired.StrateBottomChunkZ,
Desired.MinimumChunkZ,Desired.MaximumChunkZ);
VoxelClipmapDesiredTiles::FParameters Parameters;
Parameters.ClipRadius=World.ClipRadius;
Parameters.MaxClipLevel=FMath::Clamp(World.MaxClipLevel,0,8);
Parameters.RenderDistanceChunks=World.RenderDistanceChunks;
TArray<FVoxelTileKey> Tiles;
VoxelClipmapDesiredTiles::Build(Desired.CenterChunk,Desired.MinimumChunkZ,
Desired.MaximumChunkZ,Parameters,Tiles);
Desired.Tiles.reserve(static_cast<size_t>(Tiles.Num()));
for(const FVoxelTileKey& Tile:Tiles)Desired.Tiles.push_back(Tile);
std::sort(Desired.Tiles.begin(),Desired.Tiles.end(),[&](const FVoxelTileKey& A,const FVoxelTileKey& B)
{
const double DA=TileDistanceSquaredChunks(A,Desired.CenterChunk);
const double DB=TileDistanceSquaredChunks(B,Desired.CenterChunk);
return DA==DB?A.Level<B.Level:DA<DB;
});
return Desired;
}
}
class FStandaloneStreamingGenerator
{
public:
explicit FStandaloneStreamingGenerator(FStandaloneOwnerWorld& InWorld,int32 InWorkerCount=8,
bool bInHashDensity=false,
const FStandaloneMeshCache* InMeshCache=nullptr,
int32 InMaxLevelFilter=-1)
:World(&InWorld),WorkerCount(FMath::Clamp(InWorkerCount,1,16)),bHashDensity(bInHashDensity),
MeshCache(InMeshCache),MaxLevelFilter(InMaxLevelFilter)
{
VoxelGenLOD::InitializeConsoleSwitches();
UVoxelMarchingCubesMesher::InitializeConsoleSwitches();
}
FStandaloneStreamingGenerator(const FStandaloneStreamingGenerator&)=delete;
FStandaloneStreamingGenerator& operator=(const FStandaloneStreamingGenerator&)=delete;
~FStandaloneStreamingGenerator(){Stop();}
FStandaloneStreamingUpdate Start(const FVector& CameraPositionVoxels,
const std::vector<FVoxelTileKey>& ResidentKeys={},
uint64 CrossingIndex=0)
{
Stop();
{
std::lock_guard<std::mutex> Lock(Mutex);
bStop=false;
}
for(int32 I=0;I<WorkerCount;++I)Workers.emplace_back([this](){WorkerLoop();});
return UpdateDesired(CameraPositionVoxels,ResidentKeys,CrossingIndex);
}
void Stop()
{
{
std::lock_guard<std::mutex> Lock(Mutex);
bStop=true;
Queue.clear();
}
Wake.notify_all();
for(std::thread& Worker:Workers)if(Worker.joinable())Worker.join();
Workers.clear();
std::lock_guard<std::mutex> Lock(Mutex);
Ready.clear();Outstanding.clear();Desired.clear();
}
FStandaloneStreamingUpdate UpdateDesired(
const FVector& CameraPositionVoxels,const std::vector<FVoxelTileKey>& ResidentKeys,
uint64 CrossingIndex=0)
{
FStandaloneDesiredTileSet NewDesired=StandaloneWorldMesher::BuildDesiredSet(*World,CameraPositionVoxels);
if(MaxLevelFilter>=0)
NewDesired.Tiles.erase(std::remove_if(NewDesired.Tiles.begin(),NewDesired.Tiles.end(),
[&](const FVoxelTileKey& Key){return Key.Level>MaxLevelFilter;}),NewDesired.Tiles.end());
FStandaloneStreamingUpdate Stats;
Stats.CenterChunk=NewDesired.CenterChunk;
Stats.DesiredTiles=static_cast<int32>(NewDesired.Tiles.size());
std::unordered_set<FVoxelTileKey,FStandaloneTileKeyHash> Resident;
Resident.reserve(ResidentKeys.size()*2+1);
for(const FVoxelTileKey& Key:ResidentKeys)Resident.insert(Key);
for(const FVoxelTileKey& Key:NewDesired.Tiles)
{
if(Key.Level<World->FullResClipLevels)++Stats.FullResTiles;
if(Resident.contains(Key))++Stats.RetainedResidentTiles;
}
std::lock_guard<std::mutex> Lock(Mutex);
Desired.clear();Desired.reserve(NewDesired.Tiles.size()*2+1);
for(const FVoxelTileKey& Key:NewDesired.Tiles)Desired.insert(Key);
FullResTileCount=Stats.FullResTiles;
std::unordered_map<uint64,uint64> CancelledByCrossing;
auto QueueEnd=std::remove_if(Queue.begin(),Queue.end(),[&](const FStandaloneTileJob& Job)
{
if(Desired.contains(Job.Key))return false;
Outstanding.erase(Job.Key);++Stats.CancelledQueuedJobs;
++CancelledByCrossing[Job.RequestedCrossing];return true;
});
Queue.erase(QueueEnd,Queue.end());
Stats.CancelledByCrossing.reserve(CancelledByCrossing.size());
for(const auto& Pair:CancelledByCrossing)Stats.CancelledByCrossing.push_back(Pair);
for(FStandaloneTileJob& Job:Queue)
Job.PriorityDistanceSquared=StandaloneWorldMesher::TileDistanceSquaredChunks(
Job.Key,NewDesired.CenterChunk);
const int32 BandMin=World->StrateContentCutMinLevel<9&&NewDesired.bHasStrate
? NewDesired.StrateBottomChunkZ*CHUNK_SIZE:MIN_int32;
const int32 BandMax=World->StrateContentCutMinLevel<9&&NewDesired.bHasStrate
? (NewDesired.StrateTopChunkZ+1)*CHUNK_SIZE:MAX_int32;
for(const FVoxelTileKey& Key:NewDesired.Tiles)
{
if(Resident.contains(Key)||Outstanding.contains(Key))continue;
Queue.push_back({Key,BandMin,BandMax,CrossingIndex,
StandaloneWorldMesher::TileDistanceSquaredChunks(Key,NewDesired.CenterChunk)});
Outstanding.insert(Key);++Stats.NewJobs;
}
CurrentDesired=std::move(NewDesired);
std::make_heap(Queue.begin(),Queue.end(),FStandaloneTileJobHeapOrder{});
Wake.notify_all();
return Stats;
}
std::vector<FStandaloneCpuTile> TakeReady()
{
std::vector<FStandaloneCpuTile> Out;
std::lock_guard<std::mutex> Lock(Mutex);
Out.swap(Ready);
for(const FStandaloneCpuTile& Tile:Out)Outstanding.erase(Tile.Key);
return Out;
}
bool IsDesired(const FVoxelTileKey& Key)const
{std::lock_guard<std::mutex> Lock(Mutex);return Desired.contains(Key);}
int32 DesiredCount()const
{std::lock_guard<std::mutex> Lock(Mutex);return static_cast<int32>(Desired.size());}
int32 FullResDesiredCount()const
{std::lock_guard<std::mutex> Lock(Mutex);return FullResTileCount;}
FIntVector CenterChunk()const
{std::lock_guard<std::mutex> Lock(Mutex);return CurrentDesired.CenterChunk;}
int32 GetWorkerCount()const{return WorkerCount;}
uint64 GetCacheHits()const{return CacheHitCount.load(std::memory_order_relaxed);}
private:
FStandaloneCpuTile Generate(const FStandaloneTileJob& Job)
{
const auto StartTime=FClock::now();
FStandaloneCpuTile Out;Out.Key=Job.Key;Out.RequestedCrossing=Job.RequestedCrossing;
if(MeshCache)
{
if(const FStandaloneCpuTile* Cached=MeshCache->Find(Job.Key))
{
Out=*Cached;Out.RequestedCrossing=Job.RequestedCrossing;
Out.WorkerSeconds=std::chrono::duration<double>(FClock::now()-StartTime).count();
CacheHitCount.fetch_add(1,std::memory_order_relaxed);
return Out;
}
}
UVoxelMarchingCubesMesher Mesher;
Mesher.SetGenerator(World->Generator.get());
Mesher.bGenerateSkirts=World->bGenerateSkirts;
Mesher.SkirtCells=static_cast<float>(World->SkirtCells);
Mesher.LODOctaveDrop=World->LODOctaveDrop;
const int32 Level=Job.Key.Level;
const int32 Extent=Job.Key.ExtentVoxels();
const int32 Cells=Level<World->FullResClipLevels?CHUNK_SIZE:FMath::Max(1,World->CoarseTileCells);
const int32 Step=FMath::Max(1,Extent/Cells);
int64 SampleCount=0;
const bool bProfile=VoxelDensityProfile::GetMode()!=VoxelDensityProfile::EMode::Disabled;
const VoxelDensityProfile::FSnapshot Before=bProfile
? VoxelDensityProfile::SnapshotCurrentThread():VoxelDensityProfile::FSnapshot{};
std::vector<float> DensityValues;
FVoxelMeshData Mesh=Mesher.GenerateMesh(Job.Key.OriginVoxels(),Step,Cells,nullptr,
Job.BandZMin,Job.BandZMax,&SampleCount,nullptr,nullptr,
bHashDensity?&DensityValues:nullptr);
if(bHashDensity)
{
Out.bHasDensityHash=true;
for(const float Density:DensityValues)
{
Out.DensityHash^=std::bit_cast<uint32>(Density);
Out.DensityHash*=1099511628211ull;
}
}
if(bProfile)
{
const VoxelDensityProfile::FSnapshot After=VoxelDensityProfile::SnapshotCurrentThread();
for(size_t I=0;I<Out.ProfileCycles.size();++I)
{
const size_t Bucket=I;
Out.ProfileCycles[I]=After.Cycles[Bucket]>=Before.Cycles[Bucket]
?After.Cycles[Bucket]-Before.Cycles[Bucket]:0;
}
}
Out.DensitySamples=static_cast<uint64>(FMath::Max<int64>(0,SampleCount));
Out.Vertices.reserve(static_cast<size_t>(Mesh.Vertices.Num()));
for(int32 I=0;I<Mesh.Vertices.Num();++I)
{
const FVector& P=Mesh.Vertices[I];const FVector& N=Mesh.Normals[I];
Out.Vertices.push_back({
{static_cast<float>(P.X*0.01),static_cast<float>(P.Y*0.01),static_cast<float>(P.Z*0.01)},
{static_cast<float>(N.X),static_cast<float>(N.Y),static_cast<float>(N.Z)}});
}
Out.Indices.reserve(static_cast<size_t>(Mesh.Triangles.Num()));
for(const int32 Index:Mesh.Triangles)Out.Indices.push_back(static_cast<uint32>(Index));
Out.WorkerSeconds=std::chrono::duration<double>(FClock::now()-StartTime).count();
return Out;
}
void WorkerLoop()
{
for(;;)
{
FStandaloneTileJob Job;
{
std::unique_lock<std::mutex> Lock(Mutex);
Wake.wait(Lock,[&](){return bStop||!Queue.empty();});
if(bStop)return;
std::pop_heap(Queue.begin(),Queue.end(),FStandaloneTileJobHeapOrder{});
Job=Queue.back();Queue.pop_back();
}
FStandaloneCpuTile Tile=Generate(Job);
{
std::lock_guard<std::mutex> Lock(Mutex);
if(bStop)return;
Ready.push_back(std::move(Tile));
}
}
}
using FClock=std::chrono::steady_clock;
FStandaloneOwnerWorld* World=nullptr;
int32 WorkerCount=8;
bool bHashDensity=false;
const FStandaloneMeshCache* MeshCache=nullptr;
int32 MaxLevelFilter=-1;
std::atomic<uint64> CacheHitCount{0};
mutable std::mutex Mutex;
std::condition_variable Wake;
bool bStop=true;
std::vector<std::thread> Workers;
std::vector<FStandaloneTileJob> Queue;
std::vector<FStandaloneCpuTile> Ready;
std::unordered_set<FVoxelTileKey,FStandaloneTileKeyHash> Desired;
std::unordered_set<FVoxelTileKey,FStandaloneTileKeyHash> Outstanding;
FStandaloneDesiredTileSet CurrentDesired;
int32 FullResTileCount=0;
};