fix: stabilize particle lifecycle and view-space trails
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
OpenAI Codex
2026-09-05 10:17:27 +08:00
parent 97aa019f6f
commit 4c84a79122
7 changed files with 351 additions and 49 deletions

View File

@@ -49,9 +49,12 @@ constexpr auto ShaderName = "ParticleGpuValidationShader";
constexpr Size MaskSize{16, 16};
constexpr float EarthCircumferenceMeters = 40075016.68557849f;
// This is a validation visual clock, not a multiplier on canonical U/V. At
// Japan validation zooms, real-time m/s advances only fractions of a pixel per
// second; one real second therefore represents 6000 simulation seconds.
constexpr float VisualSimulationSecondsPerRealSecond = 6000.0f;
// 60 Hz it advances 20 simulation seconds per frame: a typical 5 m/s wind
// moves about 100 m (roughly 0.65 px at z9), while a 30 m/s sample remains
// below a four-pixel segment at that zoom.
constexpr float VisualSimulationSecondsPerRealSecond = 1200.0f;
constexpr float ParticleLifecycleSeconds = 3600.0f;
constexpr float TrailFadeReferenceFramesPerSecond = 60.0f;
std::atomic_uint64_t nextGpuGeneration{0};
std::atomic_uint64_t nextMaskGeneration{0};
@@ -244,10 +247,12 @@ public:
bool cameraDrawLogged = false;
bool worldWrapLogged = false;
bool antimeridianLogged = false;
bool trailCameraKnown = false;
uint64_t lastCameraLogFrame = 0;
std::chrono::steady_clock::time_point lastPlaybackHeartbeat{};
std::chrono::steady_clock::time_point lastSimulationTick{};
std::chrono::steady_clock::time_point lastMotionLog{};
std::chrono::steady_clock::time_point lastLifecycleLog{};
uint64_t maskGeneration = 0;
uint64_t fieldGeneration = 0;
uint64_t gpuFieldGeneration = 0;
@@ -269,6 +274,11 @@ public:
uint8_t loggedMaskMode = 255;
Size trailSize{};
Size stateSize{};
double trailCameraLatitude = 0.0;
double trailCameraLongitude = 0.0;
double trailCameraZoom = 0.0;
double trailCameraBearing = 0.0;
double trailCameraPitch = 0.0;
std::array<RenderTargetPtr, 2> stateTargets;
std::array<RenderTargetPtr, 2> trailTargets;
@@ -343,8 +353,8 @@ constexpr uint32_t MaxParticleVerticesPerSegment = std::numeric_limits<uint16_t>
void rebuildParticleDrawResources(const std::shared_ptr<ParticleGpuResources>& resources) {
auto vertices = std::make_shared<gfx::VertexVector<HeatmapTextureLayoutVertex>>();
// Each State texel is represented by a two-vertex line. Its endpoints are
// static texel indices; State position, interpolated Field velocity and
// orientation remain GPU-owned in the vertex shader.
// static texel indices; the vertex shader reads StateRead/StateWrite and
// projects the true previous-to-current displacement in GPU space.
vertices->reserve(resources->particleCount * 2);
for (uint32_t particleIndex = 0; particleIndex < resources->particleCount; ++particleIndex) {
const auto x = static_cast<int16_t>(particleIndex % resources->stateSize.width);
@@ -450,6 +460,15 @@ public:
" simulationFrame=" + std::to_string(resources->committedFrame));
}
}
if (resources->formalDisplay && now - resources->lastLifecycleLog >= std::chrono::seconds(1)) {
resources->lastLifecycleLog = now;
Log::Info(Event::Render,
"ParticleLifecycle: activeSlots=" + std::to_string(resources->particleCount) +
" respawn=GPU_HASH_ON_INVALID_OR_AGE_EXPIRED lifecycleSeconds=" +
std::to_string(ParticleLifecycleSeconds) +
" fieldId=" + resources->field.fieldID + " simulationFrame=" +
std::to_string(resources->committedFrame) + " readback=disabled");
}
}
drawable.setTexture(source, shaders::idParticleGpuValidationPreviousTexture);
@@ -473,7 +492,10 @@ public:
const bool seedState = pass == ValidationPass::State && !resources->stateSeeded;
const shaders::ParticleGpuValidationUBO ubo = {
.pass = {static_cast<float>(pass), frame, maskMode == 0 ? 0.0f : 1.0f, static_cast<float>(maskMode)},
.marker = {markerX, resources->speedColorEnabled ? 1.0f : 0.0f, 0.035f, 0.965f},
.marker = {markerX,
resources->speedColorEnabled ? 1.0f : 0.0f,
resources->formalDisplay ? ParticleLifecycleSeconds : 0.035f,
resources->formalDisplay ? TrailFadeReferenceFramesPerSecond : 0.965f},
.mask = {static_cast<float>(resources->stateSize.width),
static_cast<float>(resources->stateSize.height),
resources->formalDisplay ? 1.0f : 0.0f,
@@ -490,7 +512,8 @@ public:
if (pass == ValidationPass::Composite || pass == ValidationPass::FrozenComposite) {
const auto center = parameters.state.getLatLng(LatLng::Unwrapped);
const bool primaryWorldCopy = worldWrap == wrapForLongitude(center.longitude());
const bool primaryWorldCopy = resources->formalDisplay ? worldWrap == 0
: worldWrap == wrapForLongitude(center.longitude());
if (primaryWorldCopy && !resources->displayModeLogged) {
Log::Info(Event::Render,
"ParticleRender: displayMode=" +
@@ -725,12 +748,13 @@ bool buildParticlePoints(gfx::Context& context,
const std::shared_ptr<ParticleGpuResources>& resources,
LayerGroup& group,
const std::string& name,
const gfx::Texture2DPtr& stateTexture,
const gfx::Texture2DPtr& previousStateTexture,
const gfx::Texture2DPtr& currentStateTexture,
const gfx::Texture2DPtr& target,
const uint8_t stateSlot,
const uint8_t trailSlot) {
if (!resources->particleVertices || !resources->particleIndices || resources->particleSegments.empty() ||
!stateTexture || !target) {
!previousStateTexture || !currentStateTexture || !target) {
Log::Error(Event::Render, "ParticleGPU: event=PARTICLE_DRAW_REJECTED reason=MISSING_DRAW_RESOURCES");
return false;
}
@@ -755,16 +779,16 @@ bool buildParticlePoints(gfx::Context& context,
resources->particleIndices,
resources->particleSegments.data(),
resources->particleSegments.size());
builder->setTexture(stateTexture, shaders::idParticleGpuValidationPreviousTexture);
builder->setTexture(stateTexture, shaders::idParticleGpuValidationStateTexture);
builder->setTexture(previousStateTexture, shaders::idParticleGpuValidationPreviousTexture);
builder->setTexture(currentStateTexture, shaders::idParticleGpuValidationStateTexture);
builder->addTweaker(std::make_shared<ParticleGpuValidationTweaker>(resources,
ValidationPass::TrailParticles,
stateSlot,
trailSlot,
stateTexture,
previousStateTexture,
target,
resources->maskTexture,
stateTexture,
currentStateTexture,
0.0f,
0,
0));
@@ -1327,6 +1351,34 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->registeredTargets.clear();
const auto viewport = validViewportSize(transformState.getSize());
const auto trailCamera = transformState.getLatLng(LatLng::Unwrapped);
const auto cameraChanged = resources->formalDisplay && resources->trailCameraKnown &&
(resources->trailSize != viewport ||
std::abs(resources->trailCameraLatitude - trailCamera.latitude()) > 0.00001 ||
std::abs(resources->trailCameraLongitude - trailCamera.longitude()) > 0.00001 ||
std::abs(resources->trailCameraZoom - transformState.getZoom()) > 0.0001 ||
std::abs(resources->trailCameraBearing - transformState.getBearing()) > 0.0001 ||
std::abs(resources->trailCameraPitch - transformState.getPitch()) > 0.0001);
if (cameraChanged) {
const auto reason = resources->trailSize != viewport ? "resize"
: std::abs(resources->trailCameraZoom - transformState.getZoom()) > 0.0001 ? "zoom"
: std::abs(resources->trailCameraBearing - transformState.getBearing()) > 0.0001 ? "rotate"
: std::abs(resources->trailCameraPitch - transformState.getPitch()) > 0.0001 ? "pitch"
: "pan";
resources->trailTargets = {};
resources->trailSize = {};
resources->trailRead = 0;
resources->trailSeeded = false;
Log::Info(Event::Render,
"ParticleTrail: event=CAMERA_RESET reason=" + std::string(reason) + " statePreserved=true "
"fieldResubmit=false timelineReset=false");
}
resources->trailCameraKnown = true;
resources->trailCameraLatitude = trailCamera.latitude();
resources->trailCameraLongitude = trailCamera.longitude();
resources->trailCameraZoom = transformState.getZoom();
resources->trailCameraBearing = transformState.getBearing();
resources->trailCameraPitch = transformState.getPitch();
const auto ensureTrailTargets = [&] {
if (resources->trailTargets[0] && resources->trailTargets[1] && resources->trailSize == viewport) {
return true;
@@ -1413,6 +1465,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources,
trailGroup,
getID() + "-static-bootstrap-trail-particles",
resources->stateTargets[resources->stateRead]->getTexture(),
resources->stateTargets[stateWrite]->getTexture(),
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
@@ -1421,7 +1474,9 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
bool compositeBuilt = true;
const auto center = transformState.getLatLng(LatLng::Unwrapped);
const auto centerWrap = wrapForLongitude(center.longitude());
for (int16_t wrap = static_cast<int16_t>(centerWrap - 1); wrap <= centerWrap + 1; ++wrap) {
const auto firstWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap - 1);
const auto lastWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap + 1);
for (int16_t wrap = firstWrap; wrap <= lastWrap; ++wrap) {
compositeBuilt = buildQuad(context,
resources,
compositeGroup,
@@ -1462,7 +1517,9 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
compositeGroup.clearDrawables();
const auto center = transformState.getLatLng(LatLng::Unwrapped);
const auto centerWrap = wrapForLongitude(center.longitude());
for (int16_t wrap = static_cast<int16_t>(centerWrap - 1); wrap <= centerWrap + 1; ++wrap) {
const auto firstWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap - 1);
const auto lastWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap + 1);
for (int16_t wrap = firstWrap; wrap <= lastWrap; ++wrap) {
buildQuad(context,
resources,
compositeGroup,
@@ -1501,6 +1558,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
wallDeltaSeconds = std::chrono::duration<float>(now - resources->lastSimulationTick).count();
}
resources->lastSimulationTick = now;
const float trailWallDeltaSeconds = std::clamp(wallDeltaSeconds, 0.0f, 0.250f);
const float visualSimulationDeltaSeconds =
std::clamp(wallDeltaSeconds, 1.0f / 240.0f, 0.050f) * VisualSimulationSecondsPerRealSecond;
@@ -1527,7 +1585,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->trailTargets[trailWrite]->getTexture(),
resources->maskTexture,
resources->stateTargets[stateWrite]->getTexture(),
1.0f,
trailWallDeltaSeconds,
ValidationPass::Trail,
resources->trailRead,
trailWrite,
@@ -1539,6 +1597,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources,
trailGroup,
getID() + "-trail-particles",
resources->stateTargets[resources->stateRead]->getTexture(),
resources->stateTargets[stateWrite]->getTexture(),
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
@@ -1547,7 +1606,9 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
bool compositeBuilt = true;
const auto center = transformState.getLatLng(LatLng::Unwrapped);
const auto centerWrap = wrapForLongitude(center.longitude());
for (int16_t wrap = static_cast<int16_t>(centerWrap - 1); wrap <= centerWrap + 1; ++wrap) {
const auto firstWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap - 1);
const auto lastWrap = resources->formalDisplay ? int16_t{0} : static_cast<int16_t>(centerWrap + 1);
for (int16_t wrap = firstWrap; wrap <= lastWrap; ++wrap) {
compositeBuilt = buildQuad(context,
resources,
compositeGroup,