feat: add GPU particle time interpolation

This commit is contained in:
OpenAI Codex
2026-09-02 09:45:10 +08:00
parent b0f855eb09
commit 4675a8a29d
20 changed files with 425 additions and 57 deletions

View File

@@ -54,16 +54,17 @@ struct ParticleSettings {
uint8_t maskMode;
uint8_t fixtureMode;
bool usesFormalSource;
float timeMix;
};
ParticleSettings particleSettings(const Immutable<style::Layer::Impl>& value) {
if (value->getTypeInfo() == style::RasterParticleLayer::Impl::staticTypeInfo()) {
const auto& layer = static_cast<const style::RasterParticleLayer::Impl&>(*value);
return {layer.active, layer.maskMode, 0, true};
return {layer.active, layer.maskMode, 0, true, layer.timeMix};
}
assert(value->getTypeInfo() == style::ParticleValidationLayer::Impl::staticTypeInfo());
const auto& layer = static_cast<const style::ParticleValidationLayer::Impl&>(*value);
return {layer.active, layer.maskMode, layer.fixtureMode, false};
return {layer.active, layer.maskMode, layer.fixtureMode, false, 0.0f};
}
enum class ValidationPass : uint8_t {
@@ -164,6 +165,10 @@ public:
uint64_t maskGeneration = 0;
uint64_t fieldGeneration = 0;
uint64_t gpuFieldGeneration = 0;
uint64_t nextFieldGeneration = 0;
uint64_t gpuNextFieldGeneration = 0;
uint64_t fieldUploadCount = 0;
float timeMix = 0.0f;
uint8_t fixtureMode = 255;
uint8_t loggedMaskMode = 255;
Size trailSize{};
@@ -174,9 +179,12 @@ public:
gfx::ShaderProgramBasePtr shader;
gfx::Texture2DPtr maskTexture;
gfx::Texture2DPtr fieldTexture;
gfx::Texture2DPtr nextFieldTexture;
gfx::Texture2DPtr stateTexture;
particle::ParticleFieldSnapshot field;
particle::ParticleFieldSnapshot nextField;
std::shared_ptr<PremultipliedImage> fieldImage;
std::shared_ptr<PremultipliedImage> nextFieldImage;
std::shared_ptr<PremultipliedImage> maskFixture;
std::shared_ptr<gfx::VertexVector<HeatmapTextureLayoutVertex>> vertices;
SegmentVector segments;
@@ -263,12 +271,15 @@ public:
if (resources->fieldTexture) {
drawable.setTexture(resources->fieldTexture, shaders::idParticleGpuValidationFieldTexture);
}
if (resources->nextFieldTexture) {
drawable.setTexture(resources->nextFieldTexture, shaders::idParticleGpuValidationNextFieldTexture);
}
if (stateTexture) {
drawable.setTexture(stateTexture, shaders::idParticleGpuValidationStateTexture);
}
const float frame = simulationStep;
const float markerX = 0.5f + 0.32f * std::sin(frame * 0.071f);
const float markerX = resources->timeMix;
const float markerY = 0.5f + 0.32f * std::cos(frame * 0.053f);
// RenderTarget clear is opaque black. State initialization must therefore
// be an explicit lifecycle signal, never inferred from sampled alpha.
@@ -472,6 +483,9 @@ bool buildQuad(gfx::Context& context,
if (resources->fieldTexture) {
builder->setTexture(resources->fieldTexture, shaders::idParticleGpuValidationFieldTexture);
}
if (resources->nextFieldTexture) {
builder->setTexture(resources->nextFieldTexture, shaders::idParticleGpuValidationNextFieldTexture);
}
if (stateTexture) {
builder->setTexture(stateTexture, shaders::idParticleGpuValidationStateTexture);
}
@@ -495,7 +509,7 @@ RenderRasterParticleLayer::RenderRasterParticleLayer(Immutable<style::Layer::Imp
: RenderLayer([&] {
const auto settings = particleSettings(impl_);
return makeMutable<style::ParticleRenderLayerProperties>(
std::move(impl_), settings.active, settings.maskMode, settings.fixtureMode);
std::move(impl_), settings.active, settings.maskMode, settings.fixtureMode, settings.timeMix);
}()) {
formalSource = baseImpl->getTypeInfo() == style::RasterParticleLayer::Impl::staticTypeInfo();
Log::Info(Event::Render,
@@ -515,7 +529,7 @@ RenderRasterParticleLayer::~RenderRasterParticleLayer() {
void RenderRasterParticleLayer::evaluate(const PropertyEvaluationParameters&) {
const auto settings = particleSettings(baseImpl);
auto properties = makeMutable<style::ParticleRenderLayerProperties>(
baseImpl, settings.active, settings.maskMode, settings.fixtureMode);
baseImpl, settings.active, settings.maskMode, settings.fixtureMode, settings.timeMix);
passes = RenderPass::Translucent;
properties->renderPasses = mbgl::underlying_type(passes);
evaluatedProperties = std::move(properties);
@@ -524,7 +538,8 @@ void RenderRasterParticleLayer::evaluate(const PropertyEvaluationParameters&) {
"ParticleRender: state=" + std::string(settings.active ? "ACTIVE" : "STATIC") +
" maskMode=" + std::to_string(settings.maskMode) + " id=" + getID() + " fixture=" +
(formalSource && sourceField ? sourceField->fixtureID : fixtureID(settings.fixtureMode)) +
" layerIndex=" + std::to_string(getLayerIndex()) + " stage=40-01");
" layerIndex=" + std::to_string(getLayerIndex()) + " timeMix=" + std::to_string(settings.timeMix) +
" stage=40-02");
}
bool RenderRasterParticleLayer::needsRepaint() const {
@@ -565,6 +580,7 @@ void RenderRasterParticleLayer::prepare(const LayerPrepareParameters& parameters
const auto& source = static_cast<const RenderRasterArraySource&>(*parameters.source);
if (source.impl().field) {
sourceField = *source.impl().field;
sourceNextField = source.impl().nextField ? *source.impl().nextField : *source.impl().field;
}
}
@@ -623,20 +639,26 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
owned->maskGeneration = ++nextMaskGeneration;
try {
if (formalSource) {
if (!sourceField) {
Log::Error(Event::Render, "ParticleField: raster-array source has no loaded field");
if (!sourceField || !sourceNextField) {
Log::Error(Event::Render, "ParticleField: raster-array source has no loaded time window");
return;
}
owned->field = *sourceField;
owned->nextField = *sourceNextField;
owned->fieldGeneration = owned->field.generation;
owned->nextFieldGeneration = owned->nextField.generation;
} else {
owned->fieldGeneration = ++nextFieldGeneration;
owned->field = particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode),
owned->fieldGeneration);
owned->nextField = owned->field;
owned->nextFieldGeneration = owned->fieldGeneration;
}
owned->fieldImage = makeVelocityFieldImage(owned->field);
owned->fieldTexture = context.createTexture2D();
if (!owned->fieldImage || !owned->fieldTexture) {
owned->nextFieldImage = makeVelocityFieldImage(owned->nextField);
owned->nextFieldTexture = context.createTexture2D();
if (!owned->fieldImage || !owned->fieldTexture || !owned->nextFieldImage || !owned->nextFieldTexture) {
Log::Error(Event::Render, "ParticleGPU: field resource creation failed");
return;
}
@@ -644,7 +666,13 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
owned->fieldTexture->setSamplerConfiguration({.filter = gfx::TextureFilterType::Nearest,
.wrapU = gfx::TextureWrapType::Repeat,
.wrapV = gfx::TextureWrapType::Clamp});
owned->nextFieldTexture->setImage(owned->nextFieldImage);
owned->nextFieldTexture->setSamplerConfiguration({.filter = gfx::TextureFilterType::Nearest,
.wrapU = gfx::TextureWrapType::Repeat,
.wrapV = gfx::TextureWrapType::Clamp});
owned->gpuFieldGeneration = owned->fieldGeneration;
owned->gpuNextFieldGeneration = owned->nextFieldGeneration;
owned->fieldUploadCount = 2;
owned->fixtureMode = fixtureMode;
} catch (const std::exception& error) {
Log::Error(Event::Render, "ParticleField: fixture load failed: " + std::string(error.what()));
@@ -675,6 +703,11 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
" fieldGeneration=" + std::to_string(resources->fieldGeneration) +
" gpuFieldGeneration=" + std::to_string(resources->gpuFieldGeneration) +
" resourceGeneration=" + std::to_string(resources->generation));
Log::Info(Event::Render,
"ParticleGPU: time window T0=" + resources->field.fixtureID + "@" +
std::to_string(resources->field.dataVersion) + " T1=" + resources->nextField.fixtureID + "@" +
std::to_string(resources->nextField.dataVersion) +
" uploads=" + std::to_string(resources->fieldUploadCount));
Log::Info(
Event::Render,
"ParticleValidation30_03: mask fixture ready id=coastal-mask-validation-v1 bounds=[-180,180]x[-85,85] "
@@ -684,48 +717,100 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
"ParticleValidation30_03: anchors A=0,0 B=0,-90 C=0,-172.8 D=0,172.8 canonical=Mercator-z0");
}
const bool sourceChanged = formalSource && sourceField &&
particle::fieldIdentityChanged(resources->field, *sourceField);
const bool sourceChanged = formalSource && sourceField && sourceNextField &&
(particle::fieldIdentityChanged(resources->field, *sourceField) ||
particle::fieldIdentityChanged(resources->nextField, *sourceNextField));
if ((!formalSource && resources->fixtureMode != fixtureMode) || sourceChanged) {
try {
const auto oldFixtureID = resources->field.fixtureID;
const auto oldFieldGeneration = resources->fieldGeneration;
const auto fieldGeneration = formalSource ? sourceField->generation : ++nextFieldGeneration;
auto field = formalSource
? *sourceField
: particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode), fieldGeneration);
auto image = makeVelocityFieldImage(field);
auto texture = context.createTexture2D();
if (!image || !texture) {
const auto oldT0 = resources->field;
const auto oldT1 = resources->nextField;
auto nextT0 = formalSource ? *sourceField
: particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode),
++nextFieldGeneration);
auto nextT1 = formalSource ? *sourceNextField : nextT0;
if (!nextT0.isTimeInterpolationCompatibleWith(nextT1)) {
Log::Error(Event::Render, "ParticleGPU: rejected incompatible time-window fields");
return;
}
const bool reuseOldT0 = !particle::fieldIdentityChanged(oldT0, nextT0);
const bool reuseOldT1 = !particle::fieldIdentityChanged(oldT1, nextT0);
const bool reuseNewT0 = !particle::fieldIdentityChanged(oldT0, nextT1);
const bool reuseNewT1 = !particle::fieldIdentityChanged(oldT1, nextT1);
const bool windowOverlap = reuseOldT0 || reuseOldT1 || reuseNewT0 || reuseNewT1;
const auto upload = [&](const particle::ParticleFieldSnapshot& field,
std::shared_ptr<PremultipliedImage>& image,
gfx::Texture2DPtr& texture) {
image = makeVelocityFieldImage(field);
texture = context.createTexture2D();
if (!image || !texture) return false;
texture->setImage(image);
texture->setSamplerConfiguration({.filter = gfx::TextureFilterType::Nearest,
.wrapU = gfx::TextureWrapType::Repeat,
.wrapV = gfx::TextureWrapType::Clamp});
++resources->fieldUploadCount;
return true;
};
auto takeSlot = [&](const particle::ParticleFieldSnapshot& wanted,
particle::ParticleFieldSnapshot& assigned,
std::shared_ptr<PremultipliedImage>& image,
gfx::Texture2DPtr& texture) {
if (!particle::fieldIdentityChanged(oldT0, wanted)) {
assigned = oldT0;
image = resources->fieldImage;
texture = resources->fieldTexture;
return true;
}
if (!particle::fieldIdentityChanged(oldT1, wanted)) {
assigned = oldT1;
image = resources->nextFieldImage;
texture = resources->nextFieldTexture;
return true;
}
assigned = wanted;
return upload(assigned, image, texture);
};
particle::ParticleFieldSnapshot assignedT0;
particle::ParticleFieldSnapshot assignedT1;
std::shared_ptr<PremultipliedImage> imageT0;
std::shared_ptr<PremultipliedImage> imageT1;
gfx::Texture2DPtr textureT0;
gfx::Texture2DPtr textureT1;
if (!takeSlot(nextT0, assignedT0, imageT0, textureT0) ||
!takeSlot(nextT1, assignedT1, imageT1, textureT1)) {
Log::Error(Event::Render, "ParticleGPU: field switch resource creation failed");
return;
}
texture->setImage(image);
texture->setSamplerConfiguration({.filter = gfx::TextureFilterType::Nearest,
.wrapU = gfx::TextureWrapType::Repeat,
.wrapV = gfx::TextureWrapType::Clamp});
resources->field = std::move(field);
resources->fieldImage = std::move(image);
resources->fieldTexture = std::move(texture);
resources->fieldGeneration = fieldGeneration;
resources->gpuFieldGeneration = fieldGeneration;
resources->field = std::move(assignedT0);
resources->nextField = std::move(assignedT1);
resources->fieldImage = std::move(imageT0);
resources->nextFieldImage = std::move(imageT1);
resources->fieldTexture = std::move(textureT0);
resources->nextFieldTexture = std::move(textureT1);
resources->fieldGeneration = resources->field.generation;
resources->nextFieldGeneration = resources->nextField.generation;
resources->gpuFieldGeneration = resources->fieldGeneration;
resources->gpuNextFieldGeneration = resources->nextFieldGeneration;
resources->fixtureMode = fixtureMode;
resources->resetSequence();
if (!windowOverlap) resources->resetSequence();
Log::Info(Event::Render,
"ParticleRender: fixture switched from=" + oldFixtureID + " to=" + resources->field.fixtureID +
" oldGeneration=" + std::to_string(oldFieldGeneration) +
" newGeneration=" + std::to_string(resources->fieldGeneration) +
" gpuFieldGeneration=" + std::to_string(resources->gpuFieldGeneration) +
" trail=cleared stateSeed=INITIALIZE upload reason=FIELD_CHANGED");
Log::Info(Event::Render,
"ParticleField: fixture validated id=" + resources->field.fixtureID +
" noData=" + std::to_string(resources->field.noData) +
" velocityCount=" + std::to_string(resources->field.velocity.size()));
"ParticleGPU: time window advance T0=" + oldT0.fixtureID + " T1=" + oldT1.fixtureID +
" -> T0=" + resources->field.fixtureID + " T1=" + resources->nextField.fixtureID +
" reuse=" + (windowOverlap ? "true" : "false") +
" uploads=" + std::to_string(resources->fieldUploadCount) + " simulationRebuild=" +
(windowOverlap ? "false" : "true") + " trailCleared=" + (windowOverlap ? "false" : "true"));
} catch (const std::exception& error) {
Log::Error(Event::Render, "ParticleField: fixture switch failed: " + std::string(error.what()));
return;
}
}
const auto requestedTimeMix = std::clamp(properties.timeMix, 0.0f, 1.0f);
if (resources->timeMix != requestedTimeMix) {
resources->timeMix = requestedTimeMix;
Log::Info(Event::Render,
"ParticleGPU: timeMix=" + std::to_string(resources->timeMix) + " T0=" + resources->field.fixtureID +
" T1=" + resources->nextField.fixtureID + " uploads=" +
std::to_string(resources->fieldUploadCount) + " simulationRebuild=0 trailRebuild=0");
}
if (!resources->lifecycleModeKnown || resources->lifecycleActive != active) {
Log::Info(Event::Render,