wip(standalone): GPU step 18 -- warm-up marker: hybrid now wins cached and ties first launch

App-owned marker (bin/VoxelForgeGpuWarmup.state: adapter, driver, shader sources and
binaries). Miss -> CPU builds the first view, shader warm-up runs after it. Match ->
warm-up overlaps startup, at Normal thread priority (was Idle).
My runs outside the sandbox (Step18.exe, marker removed first, interleaved):
  CPU 1.395 / 1.363 / 1.329 s
  GPU 1.409 (marker miss, cold driver: 0 GPU tiles, warm-up 683 ms after present)
      / 1.290 / 1.278 / 1.289 s (marker match: warm-up queue 4.6 / 3.6 / 191 ms,
      GPU took 1,394 / 1,410 / 1,115 tiles, CPU mesh sum 5.1 -> 1.3-2.0 s,
      GPU busy 206-227 ms)
Cached target was <= 1.26 s: not met. With the GPU taking almost every tile, the
wall barely moves, so the rest of the 1.28 s is outside meshing. Known risk: if the
marker matches but the driver cache was evicted, the launch pays ~2.2 s (Luna's
sandbox runs).
Audits: hashes w1/w8, walk 5,642/5,642, near 960 / far 1,114 tiles 0 diffs vs CPU
and WARP, thin 353 cells, survival 6.14 GB, ten captures (2+2 edge pixels as step 17).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MmSoF64BbRFmk9C3p1VsBJ
This commit is contained in:
Fr0zkaandClaude Opus 5.5 committed 2026-10-07 21:07:05 +02:00
1 parent 6f2d33e0a9
commit 2fea2710f7
6 files changed
+292 -15

No files matched your search

+208 -13
View File
@@ -1395,6 +1395,13 @@ namespace
bool bGpuComplete=false,bTimingResolved=false;
};
enum class EGpuWarmupLaunchPolicy
{
Auto,
ForceDeferred,
ForceEager
};
uint64 HashStandaloneFile(const std::string& Path)
{
std::ifstream File(Path,std::ios::binary);
@@ -1510,6 +1517,11 @@ namespace
bool bGpuFarShaderPrewarm=false,bDisableGpuWarmupPresentHold=false;
bool bGpuFarWarmupResourcesOnly=false;
bool bGpuFarWarmupNormalPriority=false,bGpuFarWarmupBeforeWorkers=false;
EGpuWarmupLaunchPolicy GpuWarmupLaunchPolicy=EGpuWarmupLaunchPolicy::Auto;
bool bGpuWarmupDeferredUntilFirstView=false,bGpuWarmupDeferredStarted=false;
bool bGpuWarmupCacheKeyAvailable=false;
std::filesystem::path GpuWarmupStatePath;
std::string GpuWarmupCacheKey,GpuWarmupCacheIdentity;
bool bGpuFarWarmupKernelFences=false;
bool bGpuFarWarmupOnly=false;
std::string GpuWarmupEntryFilter;
@@ -1952,6 +1964,156 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
return true;
}
bool BuildGpuWarmupCacheKey()
{
GpuWarmupCacheKey.clear();
GpuWarmupCacheIdentity.clear();
if(!Device||GpuFarShaderPath.empty())return false;
IDXGIDevice* DxgiDevice=nullptr;
if(FAILED(Device->QueryInterface(__uuidof(IDXGIDevice),
reinterpret_cast<void**>(&DxgiDevice)))||!DxgiDevice)return false;
IDXGIAdapter* Adapter=nullptr;
const HRESULT AdapterResult=DxgiDevice->GetAdapter(&Adapter);
DxgiDevice->Release();
if(FAILED(AdapterResult)||!Adapter)return false;
DXGI_ADAPTER_DESC AdapterDesc{};
const HRESULT DescResult=Adapter->GetDesc(&AdapterDesc);
LARGE_INTEGER DriverVersion{};
const HRESULT DriverResult=Adapter->CheckInterfaceSupport(
__uuidof(IDXGIDevice),&DriverVersion);
Adapter->Release();
if(FAILED(DescResult)||FAILED(DriverResult))return false;
static constexpr std::array<const char*,14> WarmupEntries={
"EvaluateFarFieldCS","EvaluateNearFieldCS","ClassifyCS","ClassifyFarCS",
"ScanGroupsCS","EmitCS","ApplyNearFieldDetailCS","EmitNearCS",
"BuildFarConnectorRingsCS","EmitFarConnectorPanelsCS","ClassifyFarSkirtsCS",
"ScanFarSkirtsCS","EmitFarSkirtsCS","NoiseProbeCS"};
uint64 ShaderSetHash=14695981039346656037ull;
const auto MixText=[&](std::string_view Text)
{
for(const unsigned char Byte:Text)
{ShaderSetHash^=Byte;ShaderSetHash*=1099511628211ull;}
ShaderSetHash^=0xffu;ShaderSetHash*=1099511628211ull;
};
const auto MixFile=[&](const std::filesystem::path& Path,bool bRequired)
{
std::error_code Ec;
const bool bExists=std::filesystem::is_regular_file(Path,Ec)&&!Ec;
MixText(Path.filename().string());
if(!bExists)
{
MixText("missing");
return !bRequired;
}
const uint64 FileHash=HashStandaloneFile(Path.string());
if(FileHash==0)return false;
const uint64 FileSize=std::filesystem::file_size(Path,Ec);
if(Ec)return false;
MixText(std::to_string(FileSize));
MixText(std::to_string(FileHash));
return true;
};
MixText("gpu-far-warmup-signature-v1");
const std::filesystem::path ShaderImplementation=GpuFarShaderPath.parent_path()
.parent_path()/"gpu_field_port"/"GpuFieldPort.cpp";
if(!MixFile(GpuFarShaderPath,true)||!MixFile(ShaderImplementation,true))return false;
const std::filesystem::path PrecompiledDirectory=GpuFarShaderPath.parent_path()
.parent_path()/"gpu_field_port"/"precompiled";
for(const char* Entry:WarmupEntries)
if(!MixFile(PrecompiledDirectory/(std::string(Entry)+".cso"),false))return false;
std::ostringstream Identity;
Identity<<"adapter_luid="<<std::hex<<std::uppercase
<<static_cast<uint32>(AdapterDesc.AdapterLuid.HighPart)<<":"<<AdapterDesc.AdapterLuid.LowPart
<<" vendor="<<AdapterDesc.VendorId<<" device="<<AdapterDesc.DeviceId
<<" subsystem="<<AdapterDesc.SubSysId<<" revision="<<AdapterDesc.Revision
<<" driver_version=0x"<<static_cast<uint64>(DriverVersion.QuadPart)
<<" shader_set=0x"<<ShaderSetHash;
GpuWarmupCacheIdentity=Identity.str();
GpuWarmupCacheKey="VF_GPU_WARMUP_V1 "+GpuWarmupCacheIdentity;
bGpuWarmupCacheKeyAvailable=true;
return true;
}
void ResolveGpuWarmupLaunchPolicy()
{
if(!bGpuFarEnabled||!bGpuFarShaderPrewarm)return;
const bool bKeyAvailable=BuildGpuWarmupCacheKey();
bool bCacheMarkerMatches=false;
if(bKeyAvailable&&!GpuWarmupStatePath.empty())
{
std::ifstream Marker(GpuWarmupStatePath,std::ios::binary);
std::string SavedKey;
if(Marker&&std::getline(Marker,SavedKey))
bCacheMarkerMatches=SavedKey==GpuWarmupCacheKey;
}
if(bGpuFarWarmupBeforeWorkers)
{
bGpuWarmupDeferredUntilFirstView=false;
std::printf("[GpuWarmupLaunchDecision] mode=before_workers cache_marker=%s key_available=%s identity=%s\n",
bCacheMarkerMatches?"match":"miss",bKeyAvailable?"yes":"no",
bKeyAvailable?GpuWarmupCacheIdentity.c_str():"unavailable");
return;
}
if(GpuWarmupLaunchPolicy==EGpuWarmupLaunchPolicy::ForceDeferred)
bGpuWarmupDeferredUntilFirstView=true;
else if(GpuWarmupLaunchPolicy==EGpuWarmupLaunchPolicy::ForceEager)
bGpuWarmupDeferredUntilFirstView=false;
else
bGpuWarmupDeferredUntilFirstView=!bCacheMarkerMatches;
const char* Mode=GpuWarmupLaunchPolicy==EGpuWarmupLaunchPolicy::ForceDeferred
?"forced_cold_start":(GpuWarmupLaunchPolicy==EGpuWarmupLaunchPolicy::ForceEager
?"forced_cached_start":"auto");
std::printf("[GpuWarmupLaunchDecision] mode=%s cache_marker=%s route=%s key_available=%s state_path=%s identity=%s\n",
Mode,bCacheMarkerMatches?"match":"miss",
bGpuWarmupDeferredUntilFirstView?"after_first_CPU_view":"startup_overlap",
bKeyAvailable?"yes":"no",GpuWarmupStatePath.string().c_str(),
bKeyAvailable?GpuWarmupCacheIdentity.c_str():"unavailable");
}
void MarkGpuWarmupCacheReady()
{
if(!bGpuFarShaderPrewarm||bGpuFarWarmupResourcesOnly
||!GpuWarmupEntryFilter.empty()||!bGpuWarmupCacheKeyAvailable
||GpuWarmupStatePath.empty())return;
const std::filesystem::path TemporaryPath=GpuWarmupStatePath.wstring()
+L"."+std::to_wstring(GetCurrentProcessId())+L".tmp";
{
std::ofstream Marker(TemporaryPath,std::ios::binary|std::ios::trunc);
if(!Marker)
{
std::fprintf(stderr,"[GpuWarmupCacheState] write_failed path=%s reason=open_failed\n",
TemporaryPath.string().c_str());
return;
}
Marker<<GpuWarmupCacheKey;
Marker.flush();
if(!Marker.good())
{
std::fprintf(stderr,"[GpuWarmupCacheState] write_failed path=%s reason=flush_failed\n",
TemporaryPath.string().c_str());
return;
}
}
if(!MoveFileExW(TemporaryPath.c_str(),GpuWarmupStatePath.c_str(),
MOVEFILE_REPLACE_EXISTING|MOVEFILE_WRITE_THROUGH))
{
const DWORD Error=GetLastError();
DeleteFileW(TemporaryPath.c_str());
std::fprintf(stderr,"[GpuWarmupCacheState] write_failed path=%s win32_error=%lu\n",
GpuWarmupStatePath.string().c_str(),static_cast<unsigned long>(Error));
return;
}
std::printf("[GpuWarmupCacheState] result=recorded path=%s identity=%s\n",
GpuWarmupStatePath.string().c_str(),GpuWarmupCacheIdentity.c_str());
}
bool SetGpuWarmupContextProtection(bool bProtected)
{
if(!Context)return false;
@@ -1975,18 +2137,16 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
std::fprintf(stderr,"[GpuWarmupContextProtection] enable_failed; async immediate-context protection is unavailable\n");
GpuShaderWarmupThread=std::thread([this]()
{
// Keep cold driver compilation from stealing CPU time from the CPU fallback
// workers. Cached shader warm-up is only a few milliseconds, so this does not
// delay the normal GPU route when the driver cache is warm.
const bool bPreserveAuditPriority=bGpuFarWarmupBeforeWorkers||bGpuFarAuditEnabled;
const int WarmupPriority=bGpuFarWarmupNormalPriority?THREAD_PRIORITY_HIGHEST:
(bPreserveAuditPriority?THREAD_PRIORITY_BELOW_NORMAL:THREAD_PRIORITY_IDLE);
(bPreserveAuditPriority?THREAD_PRIORITY_BELOW_NORMAL:THREAD_PRIORITY_NORMAL);
const bool bPrioritySet=SetThreadPriority(GetCurrentThread(),WarmupPriority)!=0;
std::printf("[GpuShaderWarmupThread] priority=%s process=BelowNormal thread_relative=%s\n",
bPrioritySet?(bGpuFarWarmupNormalPriority?"Normal":
(bPreserveAuditPriority?"BelowNormal":"Idle")):"set_failed",
bGpuFarWarmupNormalPriority?"Highest":
(bPreserveAuditPriority?"BelowNormal":"Idle"));
const char* RelativePriority=bGpuFarWarmupNormalPriority?"Highest":
(bPreserveAuditPriority?"BelowNormal":"Normal");
std::printf("[GpuShaderWarmupThread] priority=%s process=BelowNormal thread_relative=%s purpose=%s\n",
bPrioritySet?RelativePriority:"set_failed",RelativePriority,
bGpuWarmupDeferredUntilFirstView?"post_first_CPU_view":
(bPreserveAuditPriority?"audit_or_before_workers":"cache_candidate_startup"));
std::fflush(stdout);
bGpuShaderWarmupQueueSucceeded=VoxelForgeGpuFarViewer::QueueFarShaderWarmup(
Device,Context,GpuFarShaderPath,!bGpuFarWarmupResourcesOnly,GpuWarmupEntryFilter,
@@ -1995,7 +2155,8 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
bGpuShaderWarmupQueueFinished.store(true,std::memory_order_release);
});
std::printf("[GpuShaderWarmup] queue_thread=started overlap=%s\n",
bGpuFarWarmupBeforeWorkers?"none_before_generation_workers":"CPU_world_configuration");
bGpuWarmupDeferredUntilFirstView?"after_first_CPU_view":
(bGpuFarWarmupBeforeWorkers?"none_before_generation_workers":"CPU_world_configuration"));
std::fflush(stdout);
}
@@ -2260,9 +2421,12 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
{std::fprintf(stderr,"World load failed: %s\n",Error.c_str());MessageBoxA(Window,Error.c_str(),"VoxelForge viewer",MB_ICONERROR);return;}
std::printf("[ColdTimeline] stage=generation_setup configured_ms=%.3f\n",
std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-ColdProcessStart).count());
if(bGpuFarEnabled&&!EnsureGpuFarPipeline(Error))
const bool bPipelineCanStartNow=!bGpuWarmupDeferredUntilFirstView
||bGpuWarmupDeferredStarted;
if(bGpuFarEnabled&&bPipelineCanStartNow&&!EnsureGpuFarPipeline(Error))
{MessageBoxA(Window,Error.c_str(),"VoxelForge GPU pipeline",MB_ICONERROR);return;}
if(bGpuFarShaderPrewarm&&!bGpuFarWarmupBeforeWorkers)
if(bGpuFarShaderPrewarm&&!bGpuFarWarmupBeforeWorkers
&&!bGpuWarmupDeferredUntilFirstView)
StartGpuShaderWarmupAsync();
const FVector CameraVoxels(Position.X*4.0,Position.Y*4.0,Position.Z*4.0);
BuildTargetChunk=StandaloneWorldMesher::CameraChunk(CameraVoxels);
@@ -2925,6 +3089,7 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
return false;
}
bGpuWarmupCompleted=true;
MarkGpuWarmupCacheReady();
SetGpuWarmupContextProtection(false);
std::printf("[GpuShaderWarmupComplete] completion_wall_ms=%.3f\n",
std::chrono::duration<double,std::milli>(
@@ -3011,6 +3176,24 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
void PumpGpuFarGeneration()
{
if(!bGpuFarEnabled||!ShapeWorld||!ShapeWorld->hasFarWorld()||!Device||!Context)return;
if(bGpuWarmupDeferredUntilFirstView&&!bGpuWarmupDeferredStarted)
{
if(!bWholeViewPresented)return;
std::string DeferredPipelineError;
if(!EnsureGpuFarPipeline(DeferredPipelineError))
{
std::fprintf(stderr,"[GpuPipelineInitError] deferred_after_first_CPU_view: %s\n",
DeferredPipelineError.c_str());
bGpuFarEnabled=false;bGpuWarmupDeferredStarted=true;
AddGpuFarCpuFallbackTiles(DesiredTiles);
return;
}
bGpuWarmupDeferredStarted=true;
std::printf("[GpuWarmupDeferredStart] at_ms=%.3f first_CPU_view_presented=true\n",
std::chrono::duration<double,std::milli>(
std::chrono::steady_clock::now()-ColdProcessStart).count());
StartGpuShaderWarmupAsync();
}
std::string PipelineError;
if(!EnsureGpuFarPipeline(PipelineError))
{bGpuFarEnabled=false;AddGpuFarCpuFallbackTiles(DesiredTiles);return;}
@@ -4663,7 +4846,8 @@ float3 Lit=Rock*(Ambient+0.88*Diffuse)+float3(0.50,0.69,0.82)*Head*0.75;
std::chrono::duration<double,std::milli>(GpuPumpEnd-CpuPumpEnd).count());
return;
}
if(bGpuFarEnabled&&bGpuFarShaderPrewarm&&!bGpuWarmupCompleted
if(bGpuFarEnabled&&bGpuFarShaderPrewarm&&!bGpuWarmupDeferredUntilFirstView
&&!bGpuWarmupCompleted
&&!bGpuShaderWarmupQueueFinished.load(std::memory_order_acquire))
{
if(!bGpuWarmupPresentSkipReported)
@@ -6544,8 +6728,12 @@ int main(int Argc,char** Argv)
wchar_t ExecutablePath[MAX_PATH]{};
const DWORD ExecutablePathLength=GetModuleFileNameW(nullptr,ExecutablePath,MAX_PATH);
if(ExecutablePathLength>0&&ExecutablePathLength<MAX_PATH)
{
App.GpuFarShaderPath=std::filesystem::path(ExecutablePath).parent_path().parent_path()
/"Research"/"OwnerShapeWorld"/"gpu_first_port"/"VoxelForgeGpuMesher.hlsl";
App.GpuWarmupStatePath=std::filesystem::path(ExecutablePath).parent_path()
/"VoxelForgeGpuWarmup.state";
}
std::string JoinedCommandLine;
for(int32 I=1;I<Argc;++I)
{if(!JoinedCommandLine.empty())JoinedCommandLine.push_back(' ');JoinedCommandLine+=Argv[I];}
@@ -6609,6 +6797,10 @@ int main(int Argc,char** Argv)
App.bHoldCpuFrameDuringGpuWarmup=true;
else if(Arg=="--gpu-far-warmup-normal-priority")
App.bGpuFarWarmupNormalPriority=true;
else if(Arg=="--gpu-far-force-cold-start")
App.GpuWarmupLaunchPolicy=EGpuWarmupLaunchPolicy::ForceDeferred;
else if(Arg=="--gpu-far-force-cached-start")
App.GpuWarmupLaunchPolicy=EGpuWarmupLaunchPolicy::ForceEager;
else if(Arg=="--gpu-far-warmup-before-workers")
App.bGpuFarEnabled=true,App.bGpuFarShaderPrewarm=true,App.bGpuFarWarmupBeforeWorkers=true,
App.bShapeWorld=true,App.bForceGenerate=true,App.bPresentCpuFrameBeforeGpu=true,
@@ -6875,6 +7067,7 @@ int main(int Argc,char** Argv)
std::printf("[ColdTimeline] stage=viewer_device_init end_ms=%.3f duration_ms=%.3f\n",
std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-ColdProcessStart).count(),
std::chrono::duration<double,std::milli>(std::chrono::steady_clock::now()-DeviceInitStart).count());
App.ResolveGpuWarmupLaunchPolicy();
if(!App.bGpuFarWarmupOnly)
{
ShowWindow(App.Window,SW_SHOW);UpdateWindow(App.Window);
@@ -6898,6 +7091,7 @@ int main(int Argc,char** Argv)
if(!VoxelForgeGpuFarViewer::CompleteFarShaderWarmup(App.Device,App.Context,
App.GpuFarShaderPath,WarmupError))
{std::fprintf(stderr,"[GpuShaderWarmupError] completion: %s\n",WarmupError.c_str());return 5;}
App.MarkGpuWarmupCacheReady();
uint64 WarmupDxgiUsage=0,WarmupDxgiBudget=0;
if(VoxelForgeGpuFarViewer::QueryFarGpuMemory(App.Device,App.Context,App.GpuFarShaderPath,
WarmupDxgiUsage,WarmupDxgiBudget,WarmupError))
@@ -6922,6 +7116,7 @@ int main(int Argc,char** Argv)
if(!VoxelForgeGpuFarViewer::CompleteFarShaderWarmup(App.Device,App.Context,
App.GpuFarShaderPath,WarmupError))
{std::fprintf(stderr,"[GpuShaderWarmupError] completion: %s\n",WarmupError.c_str());return 5;}
App.MarkGpuWarmupCacheReady();
App.SetGpuWarmupContextProtection(false);
App.bGpuWarmupCompleted=true;
std::printf("[GpuShaderWarmupBeforeWorkers] completed_before_generation_workers=true\n");