fix: close STEP-081 iOS Metal particle rendering
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
tei
2026-09-18 07:58:51 +09:00
parent 8fd517bac2
commit f1c0f92b2b
14 changed files with 503 additions and 44 deletions

View File

@@ -130,6 +130,7 @@ enum class ValidationPass : uint8_t {
Composite = 2,
FrozenComposite = 3,
TrailParticles = 4,
TrailParticleCoverage = 6,
};
const char* slotName(const uint8_t slot) {
@@ -390,9 +391,12 @@ public:
std::shared_ptr<PremultipliedImage> maskFixture;
std::shared_ptr<gfx::VertexVector<HeatmapTextureLayoutVertex>> vertices;
std::shared_ptr<gfx::VertexVector<HeatmapTextureLayoutVertex>> particleVertices;
std::shared_ptr<gfx::VertexVector<HeatmapTextureLayoutVertex>> particleCoverageVertices;
gfx::IndexVectorBasePtr particleIndices;
gfx::IndexVectorBasePtr particleCoverageIndices;
SegmentVector segments;
SegmentVector particleSegments;
SegmentVector particleCoverageSegments;
uint8_t stateWrite() const { return static_cast<uint8_t>(1 - stateRead); }
uint8_t trailWrite() const { return static_cast<uint8_t>(1 - trailRead); }
@@ -446,17 +450,23 @@ constexpr uint32_t MaxParticleVerticesPerSegment = std::numeric_limits<uint16_t>
void rebuildParticleDrawResources(const std::shared_ptr<ParticleGpuResources>& resources) {
auto vertices = std::make_shared<gfx::VertexVector<HeatmapTextureLayoutVertex>>();
// Each State texel is represented by a two-vertex line. Its endpoints are
// static texel indices; the vertex shader reads StateRead/StateWrite and
// projects the true previous-to-current displacement in GPU space.
auto coverageVertices = std::make_shared<gfx::VertexVector<HeatmapTextureLayoutVertex>>();
vertices->reserve(resources->particleCount * 2);
coverageVertices->reserve(resources->particleCount * 4);
for (uint32_t particleIndex = 0; particleIndex < resources->particleCount; ++particleIndex) {
const auto x = static_cast<int16_t>(particleIndex % resources->stateSize.width);
const auto y = static_cast<int16_t>(particleIndex / resources->stateSize.width);
// a_pos.x packs state X and endpoint (0=start, 1=end). State grids
// are at most 1024 wide, so the encoded value remains a signed short.
// Formal line path: x*2 + endpoint (0=previous, 1=current).
vertices->emplace_back(HeatmapBucket::textureVertex({static_cast<int16_t>(x * 2), y}));
vertices->emplace_back(HeatmapBucket::textureVertex({static_cast<int16_t>(x * 2 + 1), y}));
// Metal coverage path: x*4 + corner (0..3). The shader projects the
// true current State position, then applies only a 0.5-pixel clip-space
// corner offset. Simulation/world coordinates are never modified.
for (int16_t corner = 0; corner < 4; ++corner) {
coverageVertices->emplace_back(
HeatmapBucket::textureVertex({static_cast<int16_t>(x * 4 + corner), y}));
}
}
auto indices = std::make_shared<gfx::IndexVectorBase>();
@@ -465,15 +475,41 @@ void rebuildParticleDrawResources(const std::shared_ptr<ParticleGpuResources>& r
indices->extend(1, static_cast<uint16_t>(index));
}
constexpr uint32_t MaxCoverageVerticesPerSegment = MaxParticleVerticesPerSegment - (MaxParticleVerticesPerSegment % 4u);
constexpr uint32_t MaxCoverageParticlesPerSegment = MaxCoverageVerticesPerSegment / 4u;
auto coverageIndices = std::make_shared<gfx::IndexVectorBase>();
coverageIndices->reserve(MaxCoverageParticlesPerSegment * 6u);
for (uint32_t particle = 0; particle < MaxCoverageParticlesPerSegment; ++particle) {
const auto base = static_cast<uint16_t>(particle * 4u);
coverageIndices->extend(1, base + 0u);
coverageIndices->extend(1, base + 1u);
coverageIndices->extend(1, base + 2u);
coverageIndices->extend(1, base + 2u);
coverageIndices->extend(1, base + 1u);
coverageIndices->extend(1, base + 3u);
}
SegmentVector segments;
const auto vertexCount = resources->particleCount * 2;
for (uint32_t offset = 0; offset < vertexCount; offset += MaxParticleVerticesPerSegment) {
const auto count = std::min(vertexCount - offset, MaxParticleVerticesPerSegment);
const auto lineVertexCount = resources->particleCount * 2u;
for (uint32_t offset = 0; offset < lineVertexCount; offset += MaxParticleVerticesPerSegment) {
const auto count = std::min(lineVertexCount - offset, MaxParticleVerticesPerSegment);
segments.emplace_back(offset, 0, count, count);
}
SegmentVector coverageSegments;
const auto coverageVertexCount = resources->particleCount * 4u;
for (uint32_t offset = 0; offset < coverageVertexCount; offset += MaxCoverageVerticesPerSegment) {
const auto vertexCount = std::min(coverageVertexCount - offset, MaxCoverageVerticesPerSegment);
const auto particleCount = vertexCount / 4u;
coverageSegments.emplace_back(offset, 0, vertexCount, particleCount * 6u);
}
resources->particleVertices = std::move(vertices);
resources->particleCoverageVertices = std::move(coverageVertices);
resources->particleIndices = std::move(indices);
resources->particleCoverageIndices = std::move(coverageIndices);
resources->particleSegments = std::move(segments);
resources->particleCoverageSegments = std::move(coverageSegments);
}
class ParticleGpuValidationTweaker final : public gfx::DrawableTweaker {
@@ -598,11 +634,11 @@ public:
.fieldScale = {resources->fieldScale,
resources->nextFieldScale,
resources->viewportDensityActive ? 1.0f : 0.0f,
0.0f},
static_cast<float>(resources->trailSize.width)},
.advection = {resources->field.vectorSemantics == particle::VectorSemantics::Direction ? 1.0f : 0.0f,
resources->advectionScale,
0.00001f,
0.0f},
static_cast<float>(resources->trailSize.height)},
.particleColor = resources->particleColor,
.trailColor = resources->trailColor,
.weatherStops = {},
@@ -920,6 +956,67 @@ bool buildParticlePoints(gfx::Context& context,
return true;
}
bool buildParticleEndpointCoverage(gfx::Context& context,
const std::shared_ptr<ParticleGpuResources>& resources,
LayerGroup& group,
const std::string& name,
const gfx::Texture2DPtr& previousStateTexture,
const gfx::Texture2DPtr& currentStateTexture,
const gfx::Texture2DPtr& target,
const uint8_t stateSlot,
const uint8_t trailSlot) {
if (!resources->particleCoverageVertices || !resources->particleCoverageIndices ||
resources->particleCoverageSegments.empty() || !previousStateTexture || !currentStateTexture || !target) {
Log::Error(Event::Render, "ParticleGPU: event=PARTICLE_COVERAGE_REJECTED reason=MISSING_DRAW_RESOURCES");
return false;
}
auto attributes = context.createVertexAttributeArray();
if (const auto& attribute = attributes->set(shaders::idParticleGpuValidationPosVertexAttribute)) {
attribute->setSharedRawData(resources->particleCoverageVertices,
offsetof(HeatmapTextureLayoutVertex, a1),
0,
sizeof(HeatmapTextureLayoutVertex),
gfx::AttributeDataType::Short2);
}
auto builder = context.createDrawableBuilder(name);
builder->setShader(resources->shader);
builder->setRenderPass(RenderPass::Translucent);
builder->setEnableDepth(false);
builder->setColorMode(gfx::ColorMode::unblended());
builder->setCullFaceMode(gfx::CullFaceMode::disabled());
builder->setVertexAttributes(std::move(attributes));
builder->setRawVertices({}, resources->particleCoverageVertices->elements(), gfx::AttributeDataType::Short2);
builder->setSegments(gfx::Triangles(),
resources->particleCoverageIndices,
resources->particleCoverageSegments.data(),
resources->particleCoverageSegments.size());
builder->setTexture(previousStateTexture, shaders::idParticleGpuValidationPreviousTexture);
builder->setTexture(currentStateTexture, shaders::idParticleGpuValidationStateTexture);
builder->addTweaker(std::make_shared<ParticleGpuValidationTweaker>(resources,
ValidationPass::TrailParticleCoverage,
stateSlot,
trailSlot,
previousStateTexture,
target,
resources->maskTexture,
currentStateTexture,
0.0f,
0.0f,
VisualSimulationSecondsPerRealSecond,
0,
0));
builder->flush(context);
auto drawables = builder->clearDrawables();
if (drawables.size() != 1 || !drawables.front()) {
Log::Error(Event::Render, "ParticleGPU: event=PARTICLE_COVERAGE_REJECTED reason=DRAWABLE_FINALIZE_FAILED");
return false;
}
group.addDrawable(std::move(drawables.front()));
return true;
}
} // namespace
RenderRasterParticleLayer::RenderRasterParticleLayer(Immutable<style::Layer::Impl> impl_)
@@ -1704,6 +1801,21 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
trailWrite);
#if MLN_RENDER_BACKEND_METAL
const bool endpointCoverageBuilt = !resources->formalDisplay || resources->trailCameraInvalidated ||
buildParticleEndpointCoverage(
context,
resources,
trailGroup,
getID() + "-static-bootstrap-trail-particle-endpoints",
resources->stateTargets[resources->stateRead]->getTexture(),
resources->stateTargets[stateWrite]->getTexture(),
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
trailWrite);
#else
const bool endpointCoverageBuilt = true;
#endif
compositeGroup.clearDrawables();
bool compositeBuilt = true;
const auto center = transformState.getLatLng(LatLng::Unwrapped);
@@ -1728,7 +1840,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
false) &&
compositeBuilt;
}
if (!stateBuilt || !trailBuilt || !particlesBuilt || !compositeBuilt) {
if (!stateBuilt || !trailBuilt || !particlesBuilt || !endpointCoverageBuilt || !compositeBuilt) {
compositeGroup.clearDrawables();
return;
}
@@ -1871,6 +1983,20 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
trailWrite);
#if MLN_RENDER_BACKEND_METAL
const bool endpointCoverageBuilt = !resources->formalDisplay || resources->trailCameraInvalidated ||
buildParticleEndpointCoverage(context,
resources,
trailGroup,
getID() + "-trail-particle-endpoints",
resources->stateTargets[resources->stateRead]->getTexture(),
resources->stateTargets[stateWrite]->getTexture(),
resources->trailTargets[trailWrite]->getTexture(),
stateWrite,
trailWrite);
#else
const bool endpointCoverageBuilt = true;
#endif
compositeGroup.clearDrawables();
bool compositeBuilt = true;
const auto center = transformState.getLatLng(LatLng::Unwrapped);
@@ -1895,7 +2021,7 @@ void RenderRasterParticleLayer::update(gfx::ShaderRegistry& shaders,
false) &&
compositeBuilt;
}
if (!stateBuilt || !trailBuilt || !particlesBuilt || !compositeBuilt) {
if (!stateBuilt || !trailBuilt || !particlesBuilt || !endpointCoverageBuilt || !compositeBuilt) {
compositeGroup.clearDrawables();
return;
}