renderer: add generic scalar field color path

This commit is contained in:
OpenAI Codex
2026-09-10 22:41:55 +08:00
parent 9dabb95e44
commit 09ead7f201
10 changed files with 125 additions and 56 deletions

View File

@@ -1077,10 +1077,17 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
}
return;
}
// Scalar-only is a valid V3 Core snapshot, but this velocity-only
// renderer stays fail-closed until the V3-03 texture contract exists.
if (formalSource && (!sourceField->hasVector() || !sourceNextField->hasVector())) {
// Particle advection consumes only physical U/V velocity. Scalar-only
// fields and direction vectors are unavailable until V3-04 defines their
// separate advection contract.
if (formalSource && (!sourceField->hasVector() || !sourceNextField->hasVector() ||
sourceField->vectorSemantics != particle::VectorSemantics::Velocity ||
sourceNextField->vectorSemantics != particle::VectorSemantics::Velocity)) {
if (layerGroup) static_cast<LayerGroup&>(*layerGroup).clearDrawables();
if (gpuResources) {
removeTargets(gpuResources->registeredTargets, changes);
gpuResources.reset();
}
return;
}
if (!layerGroup || !isLayerRenderable()) return;

View File

@@ -35,22 +35,34 @@ std::array<float, 4> bounds(const particle::ParticleFieldSnapshot& field) {
static_cast<float>((field.east + 180.0) / 360.0), static_cast<float>(mercatorY(field.south))};
}
float scale(const particle::ParticleFieldSnapshot& field) {
bool supportsValueSource(const particle::ParticleFieldSnapshot& field, style::FieldValueSource valueSource) {
return valueSource == style::FieldValueSource::Scalar ? field.hasScalar() : field.hasVector();
}
float scale(const particle::ParticleFieldSnapshot& field, style::FieldValueSource valueSource) {
float result = 1.0f;
for (size_t index = 0; index < field.valid.size(); ++index) if (field.valid[index]) {
result = std::max(result, std::abs(field.vector[index * 2]));
result = std::max(result, std::abs(field.vector[index * 2 + 1]));
if (valueSource == style::FieldValueSource::Scalar) {
result = std::max(result, std::abs(field.scalar[index]));
} else {
result = std::max(result, std::abs(field.vector[index * 2]));
result = std::max(result, std::abs(field.vector[index * 2 + 1]));
}
}
return result;
}
std::shared_ptr<PremultipliedImage> image(const particle::ParticleFieldSnapshot& field, const float fieldScale) {
std::shared_ptr<PremultipliedImage> image(const particle::ParticleFieldSnapshot& field,
const float fieldScale,
style::FieldValueSource valueSource) {
auto result = std::make_shared<PremultipliedImage>(Size{field.width, field.height});
for (uint32_t y = 0; y < field.height; ++y) for (uint32_t x = 0; x < field.width; ++x) {
const auto cell = (static_cast<size_t>(y) * field.width + x) * 2;
const auto cell = static_cast<size_t>(y) * field.width + x;
const auto pixel = (static_cast<size_t>(y) * field.width + x) * PremultipliedImage::channels;
result->data[pixel] = static_cast<uint8_t>(std::clamp(field.vector[cell] / fieldScale * .5f + .5f, 0.0f, 1.0f) * 255.0f);
result->data[pixel + 1] = static_cast<uint8_t>(std::clamp(field.vector[cell + 1] / fieldScale * .5f + .5f, 0.0f, 1.0f) * 255.0f);
const float first = valueSource == style::FieldValueSource::Scalar ? field.scalar[cell] : field.vector[cell * 2];
const float second = valueSource == style::FieldValueSource::Scalar ? 0.0f : field.vector[cell * 2 + 1];
result->data[pixel] = static_cast<uint8_t>(std::clamp(first / fieldScale * .5f + .5f, 0.0f, 1.0f) * 255.0f);
result->data[pixel + 1] = static_cast<uint8_t>(std::clamp(second / fieldScale * .5f + .5f, 0.0f, 1.0f) * 255.0f);
result->data[pixel + 2] = 0;
result->data[pixel + 3] = field.valid[y * field.width + x] ? 255 : 0;
}
@@ -69,13 +81,14 @@ struct Resources {
gfx::Texture2DPtr texture1;
float scale0 = 1.0f;
float scale1 = 1.0f;
style::FieldValueSource valueSource = style::FieldValueSource::VectorMagnitude;
};
class Tweaker final : public gfx::DrawableTweaker {
public:
Tweaker(std::shared_ptr<Resources> resources_, float mix_, float opacity_, int16_t wrap_,
std::vector<style::WeatherColorStop> stops_)
: resources(std::move(resources_)), mix(mix_), opacity(opacity_), wrap(wrap_), stops(std::move(stops_)) {}
std::vector<style::WeatherColorStop> stops_, style::FieldValueSource valueSource_)
: resources(std::move(resources_)), mix(mix_), opacity(opacity_), wrap(wrap_), stops(std::move(stops_)), valueSource(valueSource_) {}
void init(gfx::Drawable&) override {}
void execute(gfx::Drawable& drawable, PaintParameters& parameters) override {
drawable.setTexture(resources->texture0, shaders::idParticleGpuValidationPreviousTexture);
@@ -94,8 +107,9 @@ public:
.matrix = util::cast<float>(parameters.matrixForTile({wrap, {0, 0, 0}})),
};
ubo.weatherRamp[0] = static_cast<float>(stops.size());
ubo.weatherRamp[1] = valueSource == style::FieldValueSource::VectorMagnitude ? 1.0f : 0.0f;
for (size_t i = 0; i < stops.size(); ++i) {
ubo.weatherStops[i][0] = stops[i].speedMps;
ubo.weatherStops[i][0] = stops[i].value;
ubo.weatherColors[i] = stops[i].color;
}
drawable.mutableUniformBuffers().createOrUpdate(shaders::idParticleGpuValidationUBO, &ubo, parameters.context);
@@ -106,16 +120,17 @@ private:
float opacity;
int16_t wrap;
std::vector<style::WeatherColorStop> stops;
style::FieldValueSource valueSource;
};
bool same(const particle::ParticleFieldSnapshot& a, const particle::ParticleFieldSnapshot& b) {
return particle::sameFieldGpuContent(a, b);
}
bool uploadField(gfx::Context& context, const particle::ParticleFieldSnapshot& field,
std::shared_ptr<PremultipliedImage>& outputImage, gfx::Texture2DPtr& outputTexture, float& outputScale) {
outputScale = scale(field);
outputImage = image(field, outputScale);
bool uploadField(gfx::Context& context, const particle::ParticleFieldSnapshot& field, style::FieldValueSource valueSource,
std::shared_ptr<PremultipliedImage>& outputImage, gfx::Texture2DPtr& outputTexture, float& outputScale) {
outputScale = scale(field, valueSource);
outputImage = image(field, outputScale, valueSource);
outputTexture = context.createTexture2D();
if (!outputTexture) return false;
outputTexture->setImage(outputImage);
@@ -173,9 +188,8 @@ void RenderWeatherColorLayer::update(gfx::ShaderRegistry& shaders, gfx::Context&
resources.reset();
return;
}
// V3-02 accepts scalar snapshots into the Source timeline, but the
// existing velocity-only renderer must remain unavailable until V3-03.
if (!sourceField->hasVector() || !sourceNextField->hasVector()) {
const auto valueSource = static_cast<const style::WeatherColorLayer::Impl&>(*baseImpl).valueSource;
if (!supportsValueSource(*sourceField, valueSource) || !supportsValueSource(*sourceNextField, valueSource)) {
if (layerGroup) static_cast<LayerGroup&>(*layerGroup).clearDrawables();
resources.reset();
return;
@@ -195,23 +209,27 @@ void RenderWeatherColorLayer::update(gfx::ShaderRegistry& shaders, gfx::Context&
state = std::make_shared<Resources>(); state->shader = context.getGenericShader(shaders, ShaderName);
state->vertices = std::make_shared<gfx::VertexVector<HeatmapTextureLayoutVertex>>(RenderStaticData::heatmapTextureVertices());
state->segments = RenderStaticData::heatmapTextureSegments();
if (!state->shader || !uploadField(context, *sourceField, state->image0, state->texture0, state->scale0) ||
!uploadField(context, *sourceNextField, state->image1, state->texture1, state->scale1)) return;
state->valueSource = valueSource;
if (!state->shader || !uploadField(context, *sourceField, valueSource, state->image0, state->texture0, state->scale0) ||
!uploadField(context, *sourceNextField, valueSource, state->image1, state->texture1, state->scale1)) return;
state->t0 = *sourceField; state->t1 = *sourceNextField; resources = state;
Log::Info(Event::Render, "WeatherColorLayer: event=WEATHER_COLOR_FIELD_READY T0=" + state->t0.fieldID +
" T1=" + state->t1.fieldID + " scale=0..30mps");
} else if (!same(state->t0, *sourceField) || !same(state->t1, *sourceNextField)) {
" T1=" + state->t1.fieldID + " valueSource=" +
(valueSource == style::FieldValueSource::Scalar ? "SCALAR" : "VECTOR_MAGNITUDE"));
} else if (state->valueSource != valueSource || !same(state->t0, *sourceField) || !same(state->t1, *sourceNextField)) {
const auto old0 = state->t0; const auto old1 = state->t1;
auto install = [&](const particle::ParticleFieldSnapshot& wanted, particle::ParticleFieldSnapshot& assigned,
std::shared_ptr<PremultipliedImage>& outputImage, gfx::Texture2DPtr& outputTexture, float& outputScale) {
if (same(old0, wanted)) { assigned = wanted; outputImage = state->image0; outputTexture = state->texture0; outputScale = state->scale0; return true; }
if (same(old1, wanted)) { assigned = wanted; outputImage = state->image1; outputTexture = state->texture1; outputScale = state->scale1; return true; }
assigned = wanted; return uploadField(context, assigned, outputImage, outputTexture, outputScale);
// The decoded bundle may be identical while its selected GPU
// representation changes (scalar versus vector magnitude).
if (state->valueSource == valueSource && same(old0, wanted)) { assigned = wanted; outputImage = state->image0; outputTexture = state->texture0; outputScale = state->scale0; return true; }
if (state->valueSource == valueSource && same(old1, wanted)) { assigned = wanted; outputImage = state->image1; outputTexture = state->texture1; outputScale = state->scale1; return true; }
assigned = wanted; return uploadField(context, assigned, valueSource, outputImage, outputTexture, outputScale);
};
particle::ParticleFieldSnapshot t0, t1; std::shared_ptr<PremultipliedImage> i0, i1; gfx::Texture2DPtr x0, x1; float s0 = 1, s1 = 1;
if (!install(*sourceField, t0, i0, x0, s0) || !install(*sourceNextField, t1, i1, x1, s1)) return;
state->t0 = std::move(t0); state->t1 = std::move(t1); state->image0 = std::move(i0); state->image1 = std::move(i1);
state->texture0 = std::move(x0); state->texture1 = std::move(x1); state->scale0 = s0; state->scale1 = s1;
state->texture0 = std::move(x0); state->texture1 = std::move(x1); state->scale0 = s0; state->scale1 = s1; state->valueSource = valueSource;
Log::Info(Event::Render, "WeatherColorLayer: event=WEATHER_COLOR_FIELD_READY T0=" + state->t0.fieldID + " T1=" + state->t1.fieldID);
}
const float mix = sourceTimeline ? timelineFrame.mix : 0.0f;
@@ -232,7 +250,7 @@ void RenderWeatherColorLayer::update(gfx::ShaderRegistry& shaders, gfx::Context&
builder->setTexture(state->texture0, shaders::idParticleGpuValidationFieldTexture);
builder->setTexture(state->texture1, shaders::idParticleGpuValidationNextFieldTexture);
builder->addTweaker(std::make_shared<Tweaker>(state, mix, opacity, wrap,
static_cast<const style::WeatherColorLayer::Impl&>(*baseImpl).colorStops)); builder->flush(context);
static_cast<const style::WeatherColorLayer::Impl&>(*baseImpl).colorStops, valueSource)); builder->flush(context);
auto drawables = builder->clearDrawables(); if (drawables.size() == 1 && drawables.front()) group.addDrawable(std::move(drawables.front()));
}
}

View File

@@ -47,6 +47,14 @@ bool WeatherColorLayer::setColorStops(std::vector<WeatherColorStop> stops) {
observer->onLayerChanged(*this);
return true;
}
FieldValueSource WeatherColorLayer::getFieldValueSource() const { return impl().valueSource; }
void WeatherColorLayer::setFieldValueSource(FieldValueSource valueSource) {
if (valueSource == getFieldValueSource()) return;
auto next = mutableImpl();
next->valueSource = valueSource;
baseImpl = std::move(next);
observer->onLayerChanged(*this);
}
std::optional<conversion::Error> WeatherColorLayer::setPropertyInternal(const std::string& name,
const conversion::Convertible& value) {
if (name != OpacityProperty) return conversion::Error{"unknown weather-color property: " + name};

View File

@@ -15,6 +15,7 @@ public:
float opacity = 0.62f;
// Empty preserves the historical shader palette until a Host supplies stops.
std::vector<WeatherColorStop> colorStops;
FieldValueSource valueSource = FieldValueSource::VectorMagnitude;
DECLARE_LAYER_TYPE_INFO;
};