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

@@ -18,8 +18,7 @@ std::unique_ptr<style::Layer> ParticleValidationLayerFactory::createLayer(
std::unique_ptr<RenderLayer> ParticleValidationLayerFactory::createRenderLayer(
Immutable<style::Layer::Impl> impl) noexcept {
assert(impl->getTypeInfo() == getTypeInfo());
return std::make_unique<RenderParticleValidationLayer>(
staticImmutableCast<style::ParticleValidationLayer::Impl>(impl));
return std::make_unique<RenderParticleValidationLayer>(std::move(impl));
}
} // namespace mbgl

View File

@@ -0,0 +1,25 @@
#include <mbgl/layermanager/raster_particle_layer_factory.hpp>
#include <mbgl/renderer/layers/render_raster_particle_layer.hpp>
#include <mbgl/style/layers/raster_particle_layer.hpp>
#include <mbgl/style/layers/raster_particle_layer_impl.hpp>
namespace mbgl {
const style::LayerTypeInfo* RasterParticleLayerFactory::getTypeInfo() const noexcept {
return style::RasterParticleLayer::Impl::staticTypeInfo();
}
std::unique_ptr<style::Layer> RasterParticleLayerFactory::createLayer(
const std::string& id, const style::conversion::Convertible& value) noexcept {
const auto source = getSource(value);
return source ? std::make_unique<style::RasterParticleLayer>(id, *source) : nullptr;
}
std::unique_ptr<RenderLayer> RasterParticleLayerFactory::createRenderLayer(
Immutable<style::Layer::Impl> impl) noexcept {
assert(impl->getTypeInfo() == getTypeInfo());
return std::make_unique<RenderRasterParticleLayer>(std::move(impl));
}
} // namespace mbgl

View File

@@ -14,7 +14,8 @@ namespace mbgl::particle {
namespace {
bool knownFixture(const std::string& fixtureID) {
return fixtureID == "uniform-east" || fixtureID == "uniform-north" || fixtureID == "vortex";
return fixtureID == "uniform-east" || fixtureID == "uniform-north" || fixtureID == "vortex" ||
fixtureID == "nodata";
}
std::string readText(const std::filesystem::path& path) {
@@ -108,25 +109,28 @@ std::string sha256(const std::vector<char>& input) {
bool ParticleFieldSnapshot::isValid() const {
if (fixtureID.empty() || fieldType != "WIND" || displayDomain != "GLOBAL" || width == 0 || height == 0 ||
!(west < east) || !(south < north) || velocity.size() != static_cast<size_t>(width) * height * 2) {
!(west < east) || !(south < north) || velocity.size() != static_cast<size_t>(width) * height * 2 ||
valid.size() != static_cast<size_t>(width) * height) {
return false;
}
for (const float component : velocity) {
if (!std::isfinite(component) || component == noData) {
if (!std::isfinite(component)) {
return false;
}
}
return true;
}
ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fixtureID, const uint64_t generation) {
ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fixtureID,
const std::string& metadataText,
const std::vector<char>& bytes,
const uint64_t generation) {
if (!knownFixture(fixtureID)) {
throw std::invalid_argument("unknown offline particle fixture: " + fixtureID);
}
const auto directory = std::filesystem::path{"particle-fixtures"} / fixtureID;
rapidjson::Document metadata;
metadata.Parse(readText(directory / "metadata.json").c_str());
metadata.Parse(metadataText.c_str());
if (metadata.HasParseError() || !metadata.IsObject() || !member(metadata, "schema").IsString() ||
std::string(member(metadata, "schema").GetString()) != "maplibre-particle-fixture/v1" ||
!member(metadata, "fixtureId").IsString() ||
@@ -174,9 +178,11 @@ ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fix
field.east = bounds["east"].GetDouble();
field.north = bounds["north"].GetDouble();
field.noData = static_cast<float>(vector["noData"].GetDouble());
if (!std::isfinite(field.noData)) {
throw std::invalid_argument("non-finite fixture noData: " + fixtureID);
}
field.dataVersion = member(metadata, "dataVersion").GetUint64();
field.generation = generation;
const auto bytes = readBinary(directory / "field.f32");
const size_t expected = static_cast<size_t>(field.width) * field.height * 2 * sizeof(float);
if (bytes.size() != expected || member(integrity, "fieldByteLength").GetUint64() != expected) {
throw std::invalid_argument("fixture field length mismatch: " + fixtureID);
@@ -186,10 +192,27 @@ ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fix
}
field.velocity.resize(expected / sizeof(float));
std::memcpy(field.velocity.data(), bytes.data(), expected);
field.valid.resize(static_cast<size_t>(field.width) * field.height);
for (size_t cell = 0; cell < field.valid.size(); ++cell) {
const float u = field.velocity[cell * 2];
const float v = field.velocity[cell * 2 + 1];
if (!std::isfinite(u) || !std::isfinite(v)) {
throw std::invalid_argument("non-finite fixture velocity: " + fixtureID);
}
field.valid[cell] = (u == field.noData || v == field.noData) ? 0 : 1;
}
if (!field.isValid()) {
throw std::invalid_argument("invalid offline particle fixture: " + fixtureID);
}
return field;
}
ParticleFieldSnapshot OfflineFixtureParticleAdapter::load(const std::string& fixtureID, const uint64_t generation) {
if (!knownFixture(fixtureID)) {
throw std::invalid_argument("unknown offline particle fixture: " + fixtureID);
}
const auto directory = std::filesystem::path{"particle-fixtures"} / fixtureID;
return load(fixtureID, readText(directory / "metadata.json"), readBinary(directory / "field.f32"), generation);
}
} // namespace mbgl::particle

View File

@@ -24,6 +24,7 @@ struct ParticleFieldSnapshot {
// Row-major, little-endian contract decoded as interleaved canonical
// U-east/V-north values. A noData component makes the cell invalid.
std::vector<float> velocity;
std::vector<uint8_t> valid;
bool isValid() const;
};
@@ -33,6 +34,14 @@ struct ParticleFieldSnapshot {
// used by future Android Vulkan and iOS renderers.
class OfflineFixtureParticleAdapter final {
public:
// Generic loader boundary used by FileSource-backed platform inputs. It
// intentionally receives bytes only: no Android asset API crosses Core.
static ParticleFieldSnapshot load(const std::string& fixtureID,
const std::string& metadata,
const std::vector<char>& fieldBytes,
uint64_t generation);
// Linux fixture smoke-test helper. Android must use the byte loader above.
static ParticleFieldSnapshot load(const std::string& fixtureID, uint64_t generation);
};

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) {

View File

@@ -1,38 +1,11 @@
#pragma once
#include <mbgl/renderer/render_layer.hpp>
#include <mbgl/style/layers/particle_validation_layer_impl.hpp>
#include <mbgl/renderer/layers/render_raster_particle_layer.hpp>
namespace mbgl {
class ParticleGpuValidationResources;
// VALIDATION ONLY: proves Core RenderLayer/GPU ping-pong integration.
// STATIC redraws only the frozen Trail composite; it never advances State/Trail slots.
class RenderParticleValidationLayer final : public RenderLayer {
public:
explicit RenderParticleValidationLayer(Immutable<style::ParticleValidationLayer::Impl>);
~RenderParticleValidationLayer() override;
void update(gfx::ShaderRegistry&,
gfx::Context&,
const TransformState&,
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec&) override;
private:
void transition(const TransitionParameters&) override {}
void evaluate(const PropertyEvaluationParameters&) override;
bool hasTransition() const override { return false; }
bool hasCrossfade() const override { return false; }
bool needsRepaint() const override;
void prepare(const LayerPrepareParameters&) override {}
void markLayerRenderable(bool, UniqueChangeRequestVec&) override;
void layerRemoved(UniqueChangeRequestVec&) override;
void markContextDestroyed() override;
std::shared_ptr<ParticleGpuValidationResources> gpuValidation;
};
// Validation styles reuse the formal GPU renderer and do not own another
// particle simulation.
using RenderParticleValidationLayer = RenderRasterParticleLayer;
} // namespace mbgl

View File

@@ -0,0 +1,38 @@
#pragma once
#include <mbgl/particle/field/offline_fixture_particle_adapter.hpp>
#include <mbgl/renderer/render_layer.hpp>
namespace mbgl {
class ParticleGpuResources;
class RenderRasterParticleLayer final : public RenderLayer {
public:
explicit RenderRasterParticleLayer(Immutable<style::Layer::Impl>);
~RenderRasterParticleLayer() override;
void update(gfx::ShaderRegistry&,
gfx::Context&,
const TransformState&,
const std::shared_ptr<UpdateParameters>&,
const RenderTree&,
UniqueChangeRequestVec&) override;
private:
void transition(const TransitionParameters&) override {}
void evaluate(const PropertyEvaluationParameters&) override;
bool hasTransition() const override { return false; }
bool hasCrossfade() const override { return false; }
bool needsRepaint() const override;
void prepare(const LayerPrepareParameters&) override;
void markLayerRenderable(bool, UniqueChangeRequestVec&) override;
void layerRemoved(UniqueChangeRequestVec&) override;
void markContextDestroyed() override;
std::shared_ptr<ParticleGpuResources> gpuResources;
std::optional<particle::ParticleFieldSnapshot> sourceField;
bool formalSource = false;
};
} // namespace mbgl

View File

@@ -8,7 +8,9 @@
#include <mbgl/renderer/sources/render_image_source.hpp>
#include <mbgl/renderer/sources/render_raster_dem_source.hpp>
#include <mbgl/renderer/sources/render_raster_source.hpp>
#include <mbgl/renderer/sources/render_raster_array_source.hpp>
#include <mbgl/renderer/sources/render_vector_source.hpp>
#include <mbgl/style/sources/raster_array_source.hpp>
#include <mbgl/renderer/tile_parameters.hpp>
#include <mbgl/renderer/update_parameters.hpp>
#include <mbgl/tile/tile.hpp>
@@ -49,6 +51,8 @@ std::unique_ptr<RenderSource> RenderSource::create(const Immutable<Source::Impl>
case SourceType::CustomVector:
return std::make_unique<RenderCustomGeometrySource>(staticImmutableCast<CustomGeometrySource::Impl>(impl),
threadPool);
case SourceType::RasterArray:
return std::make_unique<RenderRasterArraySource>(staticImmutableCast<RasterArraySource::Impl>(impl));
}
// Not reachable, but placate GCC.

View File

@@ -0,0 +1,40 @@
#pragma once
#include <mbgl/renderer/render_source.hpp>
#include <mbgl/style/sources/raster_array_source.hpp>
#include <mbgl/style/sources/raster_array_source_impl.hpp>
namespace mbgl {
class RenderRasterArraySource final : public RenderSource {
public:
explicit RenderRasterArraySource(Immutable<style::RasterArraySource::Impl> impl)
: RenderSource(std::move(impl)) {}
bool isLoaded() const override { return impl().field.has_value(); }
void update(Immutable<style::Source::Impl> value,
const std::vector<Immutable<style::LayerProperties>>&,
bool needsRendering,
bool,
const TileParameters&) override {
baseImpl = std::move(value);
enabled = needsRendering && isLoaded();
}
std::unique_ptr<RenderItem> createRenderItem() override { return {}; }
void prepare(const SourcePrepareParameters&) override {}
void updateFadingTiles() override {}
bool hasFadingTiles() const override { return false; }
std::unordered_map<std::string, std::vector<Feature>> queryRenderedFeatures(
const ScreenLineString&,
const TransformState&,
const std::unordered_map<std::string, const RenderLayer*>&,
const RenderedQueryOptions&,
const mat4&) const override {
return {};
}
std::vector<Feature> querySourceFeatures(const SourceQueryOptions&) const override { return {}; }
void reduceMemoryUse() override {}
void dumpDebugLogs() const override {}
const style::RasterArraySource::Impl& impl() const {
return static_cast<const style::RasterArraySource::Impl&>(*baseImpl);
}
};
} // namespace mbgl

View File

@@ -10,6 +10,7 @@
#include <mbgl/style/sources/raster_dem_source.hpp>
#include <mbgl/style/sources/vector_source.hpp>
#include <mbgl/style/sources/image_source.hpp>
#include <mbgl/style/sources/raster_array_source.hpp>
#include <mbgl/util/geo.hpp>
namespace mbgl {
@@ -202,6 +203,26 @@ std::optional<std::unique_ptr<Source>> convertImageSource(const std::string& id,
return {std::move(result)};
}
std::optional<std::unique_ptr<Source>> convertRasterArraySource(const std::string& id,
const Convertible& value,
Error& error) {
const auto fixture = objectMember(value, "fixture");
const auto metadata = objectMember(value, "metadata");
const auto field = objectMember(value, "field");
if (!fixture || !metadata || !field) {
error.message = "raster-array source requires fixture, metadata, and field";
return std::nullopt;
}
const auto fixtureID = toString(*fixture);
const auto metadataURL = toString(*metadata);
const auto fieldURL = toString(*field);
if (!fixtureID || !metadataURL || !fieldURL) {
error.message = "raster-array fixture inputs must be strings";
return std::nullopt;
}
return {std::make_unique<RasterArraySource>(id, *fixtureID, *metadataURL, *fieldURL)};
}
} // namespace
std::optional<std::unique_ptr<Source>> Converter<std::unique_ptr<Source>>::operator()(const Convertible& value,
@@ -234,6 +255,8 @@ std::optional<std::unique_ptr<Source>> Converter<std::unique_ptr<Source>>::opera
return convertGeoJSONSource(id, value, error);
} else if (tname == "image") {
return convertImageSource(id, value, error);
} else if (tname == "raster-array") {
return convertRasterArraySource(id, value, error);
} else {
error.message = "invalid source type";
return std::nullopt;

View File

@@ -0,0 +1,20 @@
#pragma once
#include <mbgl/style/layer_properties.hpp>
namespace mbgl::style {
class ParticleRenderLayerProperties final : public LayerProperties {
public:
ParticleRenderLayerProperties(Immutable<Layer::Impl> impl, bool active_, uint8_t maskMode_, uint8_t fixtureMode_)
: LayerProperties(std::move(impl)),
active(active_),
maskMode(maskMode_),
fixtureMode(fixtureMode_) {}
expression::Dependency getDependencies() const noexcept override { return expression::Dependency::None; }
bool active;
uint8_t maskMode;
uint8_t fixtureMode;
};
} // namespace mbgl::style

View File

@@ -0,0 +1,99 @@
#include <mbgl/style/layers/raster_particle_layer.hpp>
#include <mbgl/style/conversion/constant.hpp>
#include <mbgl/style/conversion_impl.hpp>
#include <mbgl/style/layer_observer.hpp>
#include <mbgl/style/layers/raster_particle_layer_impl.hpp>
#include <cmath>
namespace mbgl::style {
namespace {
constexpr const char* ActiveProperty = "raster-particle-active";
constexpr const char* MaskModeProperty = "raster-particle-mask-mode";
const LayerTypeInfo typeInfo{.type = "raster-particle",
.source = LayerTypeInfo::Source::Required,
.pass3d = LayerTypeInfo::Pass3D::NotRequired,
.layout = LayerTypeInfo::Layout::NotRequired,
.fadingTiles = LayerTypeInfo::FadingTiles::NotRequired,
.crossTileIndex = LayerTypeInfo::CrossTileIndex::NotRequired,
.tileKind = LayerTypeInfo::TileKind::NotRequired};
} // namespace
const LayerTypeInfo* RasterParticleLayer::Impl::staticTypeInfo() noexcept {
return &typeInfo;
}
RasterParticleLayer::RasterParticleLayer(const std::string& id, const std::string& source)
: Layer(makeMutable<Impl>(id, source)) {}
RasterParticleLayer::RasterParticleLayer(Immutable<Impl> impl_)
: Layer(std::move(impl_)) {}
RasterParticleLayer::~RasterParticleLayer() {
weakFactory.invalidateWeakPtrs();
}
const RasterParticleLayer::Impl& RasterParticleLayer::impl() const {
return static_cast<const Impl&>(*baseImpl);
}
Mutable<RasterParticleLayer::Impl> RasterParticleLayer::mutableImpl() const {
return makeMutable<Impl>(impl());
}
bool RasterParticleLayer::isActive() const {
return impl().active;
}
uint8_t RasterParticleLayer::getMaskMode() const {
return impl().maskMode;
}
void RasterParticleLayer::setActive(bool active) {
if (active != isActive()) {
auto next = mutableImpl();
next->active = active;
baseImpl = std::move(next);
observer->onLayerChanged(*this);
}
}
void RasterParticleLayer::setMaskMode(uint8_t mode) {
if (mode <= 2 && mode != getMaskMode()) {
auto next = mutableImpl();
next->maskMode = mode;
baseImpl = std::move(next);
observer->onLayerChanged(*this);
}
}
std::optional<conversion::Error> RasterParticleLayer::setPropertyInternal(const std::string& name,
const conversion::Convertible& value) {
conversion::Error error;
if (name == ActiveProperty) {
auto converted = conversion::convert<bool>(value, error);
if (!converted) return error;
setActive(*converted);
return std::nullopt;
}
if (name == MaskModeProperty) {
auto converted = conversion::convert<float>(value, error);
if (!converted || *converted < 0 || *converted > 2 || std::floor(*converted) != *converted)
return conversion::Error{"raster-particle-mask-mode must be 0, 1, or 2"};
setMaskMode(static_cast<uint8_t>(*converted));
return std::nullopt;
}
return conversion::Error{"layer doesn't support this property"};
}
StyleProperty RasterParticleLayer::getProperty(const std::string& name) const {
if (name == ActiveProperty) return StyleProperty(Value{isActive()}, StyleProperty::Kind::Constant);
if (name == MaskModeProperty)
return StyleProperty(Value{static_cast<uint64_t>(getMaskMode())}, StyleProperty::Kind::Constant);
return {};
}
Value RasterParticleLayer::serialize() const {
auto result = Layer::serialize();
serializeProperty(result, getProperty(ActiveProperty), ActiveProperty, true);
serializeProperty(result, getProperty(MaskModeProperty), MaskModeProperty, true);
return result;
}
std::unique_ptr<Layer> RasterParticleLayer::cloneRef(const std::string& id) const {
auto next = mutableImpl();
next->id = id;
return std::unique_ptr<Layer>(new RasterParticleLayer(std::move(next)));
}
Mutable<Layer::Impl> RasterParticleLayer::mutableBaseImpl() const {
return staticMutableCast<Layer::Impl>(mutableImpl());
}
} // namespace mbgl::style

View File

@@ -0,0 +1,20 @@
#pragma once
#include <mbgl/style/layer_impl.hpp>
#include <mbgl/style/layers/raster_particle_layer.hpp>
namespace mbgl::style {
class RasterParticleLayer::Impl final : public Layer::Impl {
public:
Impl(const std::string& id, const std::string& source)
: Layer::Impl(id, source) {}
Impl(const Impl&) = default;
bool hasLayoutDifference(const Layer::Impl&) const override { return false; }
void stringifyLayout(rapidjson::Writer<rapidjson::StringBuffer>&) const override {}
bool active = true;
uint8_t maskMode = 0;
DECLARE_LAYER_TYPE_INFO;
};
} // namespace mbgl::style

View File

@@ -0,0 +1,80 @@
#include <mbgl/style/sources/raster_array_source.hpp>
#include <mbgl/particle/field/offline_fixture_particle_adapter.hpp>
#include <mbgl/storage/file_source.hpp>
#include <mbgl/storage/resource.hpp>
#include <mbgl/style/source_observer.hpp>
#include <mbgl/style/sources/raster_array_source_impl.hpp>
#include <mbgl/util/async_request.hpp>
#include <mbgl/util/logging.hpp>
namespace mbgl::style {
RasterArraySource::RasterArraySource(std::string id,
std::string fixtureID,
std::string metadataURL,
std::string fieldURL)
: Source(makeMutable<Impl>(std::move(id), std::move(fixtureID), std::move(metadataURL), std::move(fieldURL))) {}
RasterArraySource::~RasterArraySource() {
weakFactory.invalidateWeakPtrs();
}
const RasterArraySource::Impl& RasterArraySource::impl() const {
return static_cast<const Impl&>(*baseImpl);
}
void RasterArraySource::loadDescription(FileSource& fileSource) {
if (metadataRequest || fieldRequest) return;
metadataRequest = fileSource.request(
Resource::source(impl().metadataURL), [this, self = makeWeakPtr()](const Response& response) {
if (auto guard = self.lock(); self) {
if (response.error || response.noContent || !response.data) {
observer->onSourceError(
*this, std::make_exception_ptr(std::runtime_error("particle metadata request failed")));
return;
}
metadata = *response.data;
tryLoad();
}
});
fieldRequest = fileSource.request(
Resource::source(impl().fieldURL), [this, self = makeWeakPtr()](const Response& response) {
if (auto guard = self.lock(); self) {
if (response.error || response.noContent || !response.data) {
observer->onSourceError(
*this, std::make_exception_ptr(std::runtime_error("particle field request failed")));
return;
}
fieldBytes = std::vector<char>(response.data->begin(), response.data->end());
tryLoad();
}
});
}
void RasterArraySource::tryLoad() {
if (!metadata || !fieldBytes) return;
try {
auto field = particle::OfflineFixtureParticleAdapter::load(
impl().fixtureID, *metadata, *fieldBytes, ++generation);
baseImpl = makeMutable<Impl>(impl(), std::move(field));
loaded = true;
Log::Info(Event::Render,
"ParticleField: source loaded fixtureId=" + impl().fixtureID + " metadata=" + impl().metadataURL +
" field=" + impl().fieldURL);
observer->onSourceLoaded(*this);
observer->onSourceChanged(*this);
} catch (const std::exception& error) {
observer->onSourceError(*this, std::make_exception_ptr(std::runtime_error(error.what())));
}
}
bool RasterArraySource::supportsLayerType(const LayerTypeInfo* info) const {
return info && std::string(info->type) == "raster-particle";
}
Mutable<Source::Impl> RasterArraySource::createMutable() const noexcept {
return staticMutableCast<Source::Impl>(makeMutable<Impl>(impl()));
}
} // namespace mbgl::style

View File

@@ -0,0 +1,31 @@
#pragma once
#include <mbgl/particle/field/offline_fixture_particle_adapter.hpp>
#include <mbgl/style/sources/raster_array_source.hpp>
#include <mbgl/style/source_impl.hpp>
namespace mbgl::style {
class RasterArraySource::Impl final : public Source::Impl {
public:
Impl(std::string id, std::string fixtureID_, std::string metadataURL_, std::string fieldURL_)
: Source::Impl(SourceType::RasterArray, std::move(id)),
fixtureID(std::move(fixtureID_)),
metadataURL(std::move(metadataURL_)),
fieldURL(std::move(fieldURL_)) {}
Impl(const Impl&) = default;
Impl(const Impl& other, particle::ParticleFieldSnapshot field_)
: Source::Impl(other),
fixtureID(other.fixtureID),
metadataURL(other.metadataURL),
fieldURL(other.fieldURL),
field(std::move(field_)) {}
std::optional<std::string> getAttribution() const override { return {}; }
std::string fixtureID;
std::string metadataURL;
std::string fieldURL;
std::optional<particle::ParticleFieldSnapshot> field;
};
} // namespace mbgl::style

View File

@@ -12,7 +12,8 @@ MBGL_DEFINE_ENUM(SourceType,
{SourceType::Video, "video"},
{SourceType::Annotations, "annotations"},
{SourceType::Image, "image"},
{SourceType::CustomVector, "customvector"}});
{SourceType::CustomVector, "customvector"},
{SourceType::RasterArray, "raster-array"}});
MBGL_DEFINE_ENUM(VisibilityType,
{