fix(step097): complete A2 clock and GPU field correctness
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
tei
2026-09-26 15:17:43 +09:00
parent 5bb1a0e635
commit f49ffa68a1
26 changed files with 610 additions and 86 deletions

View File

@@ -9,6 +9,7 @@
#include <mbgl/gfx/index_vector.hpp>
#include <mbgl/gfx/shader_registry.hpp>
#include <mbgl/gfx/texture2d.hpp>
#include <mbgl/particle/field/particle_gpu_math.hpp>
#include <mbgl/map/transform_state.hpp>
#include <mbgl/particle/field/offline_fixture_particle_adapter.hpp>
#include <mbgl/particle/renderer_debug_counters.hpp>
@@ -49,7 +50,6 @@ namespace {
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
// 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
@@ -183,10 +183,11 @@ std::shared_ptr<PremultipliedImage> makeVelocityFieldImage(const particle::Parti
for (uint32_t x = 0; x < field.width; ++x) {
const auto cell = (static_cast<size_t>(y) * field.width + x) * 2;
const auto pixel = (static_cast<size_t>(y) * field.width + x) * PremultipliedImage::channels;
image->data[pixel + 0] = static_cast<uint8_t>(
std::clamp(field.vector[cell] / scale * 0.5f + 0.5f, 0.0f, 1.0f) * 255.0f);
image->data[pixel + 1] = static_cast<uint8_t>(
std::clamp(field.vector[cell + 1] / scale * 0.5f + 0.5f, 0.0f, 1.0f) * 255.0f);
// RGBA8 UNORM has no exact midpoint. Byte 128 is an explicit
// signed zero; shaders round the sampled byte before decoding.
const bool valid = field.valid[y * field.width + x] != 0;
image->data[pixel + 0] = particle::encodeSignedVelocity8(field.vector[cell], scale, valid);
image->data[pixel + 1] = particle::encodeSignedVelocity8(field.vector[cell + 1], scale, valid);
image->data[pixel + 2] = 0;
image->data[pixel + 3] = field.valid[y * field.width + x] ? 255 : 0;
}
@@ -570,9 +571,9 @@ public:
(static_cast<float>(row) / std::max(1u, resources->field.height - 1));
const auto u = resources->field.vector[cell * 2];
const auto v = resources->field.vector[cell * 2 + 1];
const auto cosLatitude = std::max(0.01f, std::cos(static_cast<float>(latitude * M_PI / 180.0)));
const auto dx = u * simulationStep / EarthCircumferenceMeters;
const auto dy = -v * simulationStep / (EarthCircumferenceMeters * cosLatitude);
const auto delta = particle::mercatorVelocityDelta(u, v, latitude, simulationStep);
const auto dx = delta[0];
const auto dy = delta[1];
Log::Info(Event::Render,
"ParticleMotion: fieldId=" + resources->field.fieldID + " sampleU=" + std::to_string(u) +
" sampleV=" + std::to_string(v) +
@@ -1487,8 +1488,8 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
}
const bool sourceChanged = formalSource && sourceField && sourceNextField &&
(particle::fieldIdentityChanged(resources->field, *sourceField) ||
particle::fieldIdentityChanged(resources->nextField, *sourceNextField));
(!particle::sameFieldGpuEndpoint(resources->field, *sourceField) ||
!particle::sameFieldGpuEndpoint(resources->nextField, *sourceNextField));
if ((!formalSource && resources->fixtureMode != fixtureMode) || sourceChanged) {
try {
const auto oldT0 = resources->field;
@@ -1501,10 +1502,10 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
Log::Error(Event::Render, "ParticleGPU: rejected incompatible time-window fields");
return;
}
const bool reuseOldT0 = particle::sameFieldGpuContent(oldT0, nextT0);
const bool reuseOldT1 = particle::sameFieldGpuContent(oldT1, nextT0);
const bool reuseNewT0 = particle::sameFieldGpuContent(oldT0, nextT1);
const bool reuseNewT1 = particle::sameFieldGpuContent(oldT1, nextT1);
const bool reuseOldT0 = particle::sameFieldGpuEndpoint(oldT0, nextT0);
const bool reuseOldT1 = particle::sameFieldGpuEndpoint(oldT1, nextT0);
const bool reuseNewT0 = particle::sameFieldGpuEndpoint(oldT0, nextT1);
const bool reuseNewT1 = particle::sameFieldGpuEndpoint(oldT1, nextT1);
const bool windowOverlap = reuseOldT0 || reuseOldT1 || reuseNewT0 || reuseNewT1;
const auto upload = [&](const particle::ParticleFieldSnapshot& field,
std::shared_ptr<PremultipliedImage>& image,
@@ -1534,7 +1535,8 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
std::shared_ptr<PremultipliedImage>& image,
gfx::Texture2DPtr& texture,
float& scale) {
if (particle::sameFieldGpuContent(oldT0, wanted)) {
if (particle::gpuEndpointSource(oldT0, oldT1, wanted) == particle::GpuEndpointSource::OldT0 &&
resources->fieldTexture) {
assigned = wanted;
image = resources->fieldImage;
texture = resources->fieldTexture;
@@ -1547,7 +1549,8 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
" resourceGeneration=" + std::to_string(resources->generation));
return true;
}
if (particle::sameFieldGpuContent(oldT1, wanted)) {
if (particle::gpuEndpointSource(oldT0, oldT1, wanted) == particle::GpuEndpointSource::OldT1 &&
resources->nextFieldTexture) {
assigned = wanted;
image = resources->nextFieldImage;
texture = resources->nextFieldTexture;