feat: establish formal raster particle source chain

This commit is contained in:
OpenAI Codex
2026-08-31 12:17:06 +08:00
parent 343cecc778
commit 39c7ae22ed
40 changed files with 811 additions and 130 deletions

View File

@@ -1,4 +1,4 @@
#include <mbgl/renderer/layers/render_particle_validation_layer.hpp>
#include <mbgl/renderer/layers/render_raster_particle_layer.hpp>
#include <mbgl/gfx/color_mode.hpp>
#include <mbgl/gfx/context.hpp>
@@ -17,10 +17,14 @@
#include <mbgl/renderer/render_pass.hpp>
#include <mbgl/renderer/render_static_data.hpp>
#include <mbgl/renderer/render_target.hpp>
#include <mbgl/renderer/sources/render_raster_array_source.hpp>
#include <mbgl/shaders/particle_gpu_validation_ubo.hpp>
#include <mbgl/shaders/shader_defines.hpp>
#include <mbgl/shaders/shader_program_base.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/layers/raster_particle_layer_impl.hpp>
#include <mbgl/style/layers/particle_render_layer_properties.hpp>
#include <mbgl/style/layers/particle_validation_layer_impl.hpp>
#include <mbgl/tile/tile_id.hpp>
#include <mbgl/util/image.hpp>
#include <mbgl/util/logging.hpp>
@@ -45,9 +49,21 @@ std::atomic_uint64_t nextGpuGeneration{0};
std::atomic_uint64_t nextMaskGeneration{0};
std::atomic_uint64_t nextFieldGeneration{0};
const style::ParticleValidationLayer::Impl& impl(const Immutable<style::Layer::Impl>& value) {
struct ParticleSettings {
bool active;
uint8_t maskMode;
uint8_t fixtureMode;
bool usesFormalSource;
};
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};
}
assert(value->getTypeInfo() == style::ParticleValidationLayer::Impl::staticTypeInfo());
return static_cast<const style::ParticleValidationLayer::Impl&>(*value);
const auto& layer = static_cast<const style::ParticleValidationLayer::Impl&>(*value);
return {layer.active, layer.maskMode, layer.fixtureMode, false};
}
enum class ValidationPass : uint8_t {
@@ -87,7 +103,7 @@ std::shared_ptr<PremultipliedImage> makeVelocityFieldImage(const particle::Parti
image->data[pixel + 1] = static_cast<uint8_t>(
std::clamp(field.velocity[cell + 1] * 0.5f + 0.5f, 0.0f, 1.0f) * 255.0f);
image->data[pixel + 2] = 0;
image->data[pixel + 3] = 255;
image->data[pixel + 3] = field.valid[y * field.width + x] ? 255 : 0;
}
}
return image;
@@ -125,7 +141,7 @@ void removeTargets(const std::vector<RenderTargetPtr>& targets, UniqueChangeRequ
} // namespace
class ParticleGpuValidationResources {
class ParticleGpuResources {
public:
uint64_t generation = 0;
uint64_t committedFrame = 0;
@@ -203,7 +219,7 @@ namespace {
class ParticleGpuValidationTweaker final : public gfx::DrawableTweaker {
public:
ParticleGpuValidationTweaker(std::shared_ptr<ParticleGpuValidationResources> state_,
ParticleGpuValidationTweaker(std::shared_ptr<ParticleGpuResources> state_,
ValidationPass pass_,
uint8_t readSlot_,
uint8_t writeSlot_,
@@ -351,7 +367,7 @@ public:
}
private:
std::weak_ptr<ParticleGpuValidationResources> state;
std::weak_ptr<ParticleGpuResources> state;
ValidationPass pass;
uint8_t readSlot;
uint8_t writeSlot;
@@ -382,7 +398,7 @@ RenderTargetPtr createValidationTarget(gfx::Context& context,
}
bool buildQuad(gfx::Context& context,
const std::shared_ptr<ParticleGpuValidationResources>& resources,
const std::shared_ptr<ParticleGpuResources>& resources,
LayerGroup& group,
const std::string& name,
const gfx::Texture2DPtr& source,
@@ -448,79 +464,92 @@ bool buildQuad(gfx::Context& context,
} // namespace
RenderParticleValidationLayer::RenderParticleValidationLayer(Immutable<style::ParticleValidationLayer::Impl> impl_)
: RenderLayer(makeMutable<style::ParticleValidationLayerProperties>(
impl_, impl_->active, impl_->maskMode, impl_->fixtureMode)) {
Log::Info(
Event::Render,
"ParticleValidation: RenderLayer create id=" + getID() + " source-less=true pass=translucent stage=30-02");
RenderRasterParticleLayer::RenderRasterParticleLayer(Immutable<style::Layer::Impl> impl_)
: RenderLayer([&] {
const auto settings = particleSettings(impl_);
return makeMutable<style::ParticleRenderLayerProperties>(
std::move(impl_), settings.active, settings.maskMode, settings.fixtureMode);
}()) {
formalSource = baseImpl->getTypeInfo() == style::RasterParticleLayer::Impl::staticTypeInfo();
Log::Info(Event::Render,
"ParticleRender: RenderLayer create id=" + getID() +
" source=" + (formalSource ? baseImpl->source : std::string("validation-only")));
}
RenderParticleValidationLayer::~RenderParticleValidationLayer() {
if (gpuValidation) {
RenderRasterParticleLayer::~RenderRasterParticleLayer() {
if (gpuResources) {
Log::Info(Event::Render,
"ParticleValidation30_02: resources destroy generation=" + std::to_string(gpuValidation->generation) +
" contextDestroyed=" + (gpuValidation->contextDestroyed ? "true" : "false"));
"ParticleValidation30_02: resources destroy generation=" + std::to_string(gpuResources->generation) +
" contextDestroyed=" + (gpuResources->contextDestroyed ? "true" : "false"));
}
Log::Info(Event::Render, "ParticleValidation: RenderLayer destroy id=" + getID());
}
void RenderParticleValidationLayer::evaluate(const PropertyEvaluationParameters&) {
const bool active = impl(baseImpl).active;
const uint8_t maskMode = impl(baseImpl).maskMode;
const uint8_t fixtureMode = impl(baseImpl).fixtureMode;
auto properties = makeMutable<style::ParticleValidationLayerProperties>(
staticImmutableCast<style::ParticleValidationLayer::Impl>(baseImpl), active, maskMode, fixtureMode);
void RenderRasterParticleLayer::evaluate(const PropertyEvaluationParameters&) {
const auto settings = particleSettings(baseImpl);
auto properties = makeMutable<style::ParticleRenderLayerProperties>(
baseImpl, settings.active, settings.maskMode, settings.fixtureMode);
passes = RenderPass::Translucent;
properties->renderPasses = mbgl::underlying_type(passes);
evaluatedProperties = std::move(properties);
Log::Info(Event::Render,
"ParticleValidation: state=" + std::string(active ? "ACTIVE" : "STATIC") +
" maskMode=" + std::to_string(maskMode) + " id=" + getID() + " fixture=" + fixtureID(fixtureMode) +
"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");
}
bool RenderParticleValidationLayer::needsRepaint() const {
const auto& properties = static_cast<const style::ParticleValidationLayerProperties&>(*evaluatedProperties);
bool RenderRasterParticleLayer::needsRepaint() const {
const auto& properties = static_cast<const style::ParticleRenderLayerProperties&>(*evaluatedProperties);
return properties.active && baseImpl->visibility != style::VisibilityType::None;
}
void RenderParticleValidationLayer::markLayerRenderable(bool willRender, UniqueChangeRequestVec& changes) {
void RenderRasterParticleLayer::markLayerRenderable(bool willRender, UniqueChangeRequestVec& changes) {
RenderLayer::markLayerRenderable(willRender, changes);
if (!willRender && gpuValidation) {
removeTargets(gpuValidation->registeredTargets, changes);
gpuValidation->registeredTargets.clear();
gpuValidation->resetSequence();
if (!willRender && gpuResources) {
removeTargets(gpuResources->registeredTargets, changes);
gpuResources->registeredTargets.clear();
gpuResources->resetSequence();
Log::Info(Event::Render,
"ParticleValidation30_02: disabled reset generation=" + std::to_string(gpuValidation->generation));
"ParticleValidation30_02: disabled reset generation=" + std::to_string(gpuResources->generation));
}
}
void RenderParticleValidationLayer::layerRemoved(UniqueChangeRequestVec& changes) {
if (gpuValidation) {
removeTargets(gpuValidation->registeredTargets, changes);
gpuValidation->registeredTargets.clear();
void RenderRasterParticleLayer::layerRemoved(UniqueChangeRequestVec& changes) {
if (gpuResources) {
removeTargets(gpuResources->registeredTargets, changes);
gpuResources->registeredTargets.clear();
}
RenderLayer::layerRemoved(changes);
}
void RenderParticleValidationLayer::markContextDestroyed() {
if (gpuValidation) {
gpuValidation->contextDestroyed = true;
Log::Info(Event::Render,
"ParticleValidation30_02: context lost discard generation=" +
std::to_string(gpuValidation->generation) + " stateRead=" + slotName(gpuValidation->stateRead) +
" trailRead=" + slotName(gpuValidation->trailRead));
void RenderRasterParticleLayer::markContextDestroyed() {
if (gpuResources) {
gpuResources->contextDestroyed = true;
Log::Info(
Event::Render,
"ParticleValidation30_02: context lost discard generation=" + std::to_string(gpuResources->generation) +
" stateRead=" + slotName(gpuResources->stateRead) + " trailRead=" + slotName(gpuResources->trailRead));
}
}
void RenderParticleValidationLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& transformState,
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec& changes) {
void RenderRasterParticleLayer::prepare(const LayerPrepareParameters& parameters) {
if (!formalSource || !parameters.source || parameters.source->baseImpl->type != style::SourceType::RasterArray) {
return;
}
const auto& source = static_cast<const RenderRasterArraySource&>(*parameters.source);
if (source.impl().field) {
sourceField = *source.impl().field;
}
}
void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
gfx::Context& context,
const TransformState& transformState,
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec& changes) {
if (!layerGroup) {
if (auto group = context.createLayerGroup(layerIndex, 1, getID())) {
setLayerGroup(std::move(group), changes);
@@ -530,19 +559,19 @@ void RenderParticleValidationLayer::update(gfx::ShaderRegistry& shaders,
return;
}
const auto& properties = static_cast<const style::ParticleValidationLayerProperties&>(*evaluatedProperties);
const auto& properties = static_cast<const style::ParticleRenderLayerProperties&>(*evaluatedProperties);
const bool active = properties.active;
const uint8_t maskMode = properties.maskMode;
const uint8_t fixtureMode = properties.fixtureMode;
auto resources = gpuValidation;
auto resources = gpuResources;
if (resources && resources->contextDestroyed) {
// VALIDATION ONLY: follow the 30-02 resource-generation rule. No stale
// State, Trail, or Mask texture survives a notified context loss.
gpuValidation.reset();
gpuResources.reset();
resources.reset();
}
if (!resources) {
auto owned = std::make_shared<ParticleGpuValidationResources>();
auto owned = std::make_shared<ParticleGpuResources>();
owned->generation = ++nextGpuGeneration;
owned->shader = context.getGenericShader(shaders, ShaderName);
owned->vertices = std::make_shared<gfx::VertexVector<HeatmapTextureLayoutVertex>>(
@@ -568,9 +597,18 @@ void RenderParticleValidationLayer::update(gfx::ShaderRegistry& shaders,
.wrapV = gfx::TextureWrapType::Clamp});
owned->maskGeneration = ++nextMaskGeneration;
try {
owned->fieldGeneration = ++nextFieldGeneration;
owned->field = particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode),
owned->fieldGeneration);
if (formalSource) {
if (!sourceField) {
Log::Error(Event::Render, "ParticleField: raster-array source has no loaded field");
return;
}
owned->field = *sourceField;
owned->fieldGeneration = owned->field.generation;
} else {
owned->fieldGeneration = ++nextFieldGeneration;
owned->field = particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode),
owned->fieldGeneration);
}
owned->fieldImage = makeVelocityFieldImage(owned->field);
owned->fieldTexture = context.createTexture2D();
if (!owned->fieldImage || !owned->fieldTexture) {
@@ -587,8 +625,8 @@ void RenderParticleValidationLayer::update(gfx::ShaderRegistry& shaders,
Log::Error(Event::Render, "ParticleField: fixture load failed: " + std::string(error.what()));
return;
}
gpuValidation = std::move(owned);
resources = gpuValidation;
gpuResources = std::move(owned);
resources = gpuResources;
Log::Info(Event::Render,
"ParticleValidation30_02: State A/B create generation=" + std::to_string(resources->generation) +
" size=16x16 independent=true");
@@ -621,10 +659,13 @@ void RenderParticleValidationLayer::update(gfx::ShaderRegistry& shaders,
"ParticleValidation30_03: anchors A=0,0 B=0,-90 C=0,-172.8 D=0,172.8 canonical=Mercator-z0");
}
if (resources->fixtureMode != fixtureMode) {
const bool sourceChanged = formalSource && sourceField && resources->fieldGeneration != sourceField->generation;
if ((!formalSource && resources->fixtureMode != fixtureMode) || sourceChanged) {
try {
const auto fieldGeneration = ++nextFieldGeneration;
auto field = particle::OfflineFixtureParticleAdapter::load(fixtureID(fixtureMode), 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) {