feat: add host particle field submission contract
Some checks failed
validate-scripts / validate-scripts (push) Has been cancelled

This commit is contained in:
OpenAI Codex
2026-09-04 18:35:49 +08:00
parent 285d23a58f
commit 1e8eca7912
16 changed files with 756 additions and 8 deletions

View File

@@ -38,6 +38,21 @@ RasterArrayTimelineRuntime::RasterArrayTimelineRuntime(std::vector<Entry> entrie
}
}
bool RasterArrayTimelineRuntime::append(Entry entry) {
std::lock_guard<std::mutex> lock(mutex);
if (!entry.field || !entry.field->isValid() || entries.size() >= cacheCapacity_ ||
entries.back().timestamp >= entry.timestamp) {
return false;
}
const bool continuesEndedTail = state == RasterArrayPlaybackState::Ended && t0 + 1 == entries.size() - 1;
entries.push_back(std::move(entry));
if (continuesEndedTail) {
state = RasterArrayPlaybackState::Paused;
lastTick = {};
}
return true;
}
RasterArrayTimelineFrame RasterArrayTimelineRuntime::frameLocked() const {
return {entries[t0].field, entries[t0 + 1].field, mix, t0, t0 + 1, windowRevision, state};
}
@@ -52,6 +67,16 @@ float RasterArrayTimelineRuntime::playbackSpeed() const {
return speed;
}
size_t RasterArrayTimelineRuntime::size() const {
std::lock_guard<std::mutex> lock(mutex);
return entries.size();
}
RasterArrayTimelineRuntime::Entry RasterArrayTimelineRuntime::lastEntry() const {
std::lock_guard<std::mutex> lock(mutex);
return entries.back();
}
bool RasterArrayTimelineRuntime::moveToLocked(const size_t nextT0) {
if (nextT0 + 1 >= entries.size()) return false;
t0 = nextT0;
@@ -207,6 +232,9 @@ RasterArraySource::RasterArraySource(std::string id,
RasterArraySource::RasterArraySource(std::string id, std::vector<RasterArrayLocalFieldDescriptor> localFields)
: Source(makeMutable<Impl>(std::move(id), std::move(localFields))) {}
RasterArraySource::RasterArraySource(std::string id)
: Source(makeMutable<Impl>(std::move(id))) {}
RasterArraySource::~RasterArraySource() {
weakFactory.invalidateWeakPtrs();
}
@@ -216,6 +244,13 @@ const RasterArraySource::Impl& RasterArraySource::impl() const {
}
void RasterArraySource::loadDescription(FileSource& fileSource) {
if (impl().hostFieldSubmission) {
if (loaded) return;
loaded = true;
Log::Info(Event::Render, "ParticleField: event=HOST_FIELD_INGRESS_READY sourceId=" + getID());
observer->onSourceLoaded(*this);
return;
}
if (metadataRequest || fieldRequest) return;
if (!impl().localFields.empty()) {
localFields.reserve(impl().localFields.size());
@@ -493,8 +528,8 @@ void RasterArraySource::tryInitializeTimeline() {
RasterArrayRuntimeFieldUpdateResult RasterArraySource::submitInitialRuntimeFieldWindow(
particle::ParticleFieldSnapshot t0, particle::ParticleFieldSnapshot t1) {
if (loaded || !t0.isValid() || !t1.isValid() || t0.generation == 0 || t1.generation == 0 ||
t0.validTime >= t1.validTime || !t0.isTimeInterpolationCompatibleWith(t1)) {
if (impl().field || impl().nextField || !t0.isValid() || !t1.isValid() || t0.generation == 0 ||
t1.generation == 0 || t0.validTime >= t1.validTime || !t0.isTimeInterpolationCompatibleWith(t1)) {
return rejectRuntimeFieldUpdate(RasterArrayRuntimeFieldUpdateResult::RejectedInvalidSnapshot,
"INVALID_INITIAL_WINDOW");
}
@@ -588,6 +623,139 @@ RasterArrayRuntimeFieldUpdateResult RasterArraySource::repeatRuntimeFieldWindow(
return updateRuntimeFieldWindow(*impl().nextField);
}
const char* rasterArrayHostFieldSubmitResultName(const RasterArrayHostFieldSubmitResult result) {
switch (result) {
case RasterArrayHostFieldSubmitResult::RejectedSourceMode:
return "REJECTED_SOURCE_MODE";
case RasterArrayHostFieldSubmitResult::RejectedAdapter:
return "REJECTED_ADAPTER";
case RasterArrayHostFieldSubmitResult::RejectedIncompatibleSnapshot:
return "REJECTED_INCOMPATIBLE_SNAPSHOT";
case RasterArrayHostFieldSubmitResult::RejectedNonMonotonicSnapshot:
return "REJECTED_NON_MONOTONIC_SNAPSHOT";
case RasterArrayHostFieldSubmitResult::RejectedCapacity:
return "REJECTED_CAPACITY";
case RasterArrayHostFieldSubmitResult::AcceptedPendingInitialWindow:
return "ACCEPTED_PENDING_INITIAL_WINDOW";
case RasterArrayHostFieldSubmitResult::InitialWindowReady:
return "INITIAL_WINDOW_READY";
case RasterArrayHostFieldSubmitResult::AcceptedNextSnapshot:
return "ACCEPTED_NEXT_SNAPSHOT";
case RasterArrayHostFieldSubmitResult::Duplicate:
return "DUPLICATE_NOOP";
}
return "UNKNOWN";
}
RasterArrayHostFieldSubmitResult RasterArraySource::submitHostField(const std::string& metadataText,
const std::vector<char>& payload) {
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REQUEST sourceId=" + getID() + " metadataBytes=" +
std::to_string(metadataText.size()) + " payloadBytes=" + std::to_string(payload.size()));
if (!impl().hostFieldSubmission) {
Log::Warning(
Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REJECTED sourceId=" + getID() + " result=REJECTED_SOURCE_MODE");
return RasterArrayHostFieldSubmitResult::RejectedSourceMode;
}
auto decoded = particle::LocalParticleFieldFeed::load(metadataText, payload);
if (!decoded) {
Log::Warning(
Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REJECTED sourceId=" + getID() +
" result=REJECTED_ADAPTER errorCode=" + particle::particleFieldAdapterErrorName(decoded.error) +
" payloadBytes=" + std::to_string(payload.size()) + " error=" + decoded.message);
return RasterArrayHostFieldSubmitResult::RejectedAdapter;
}
auto snapshot = std::move(*decoded.snapshot);
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_SNAPSHOT_READY sourceId=" + getID() + " fieldId=" + snapshot.fieldID +
" generation=" + std::to_string(snapshot.generation) +
" validTime=" + std::to_string(snapshot.validTime) + " width=" + std::to_string(snapshot.width) +
" height=" + std::to_string(snapshot.height) +
" layout=interleaved-uv encoding=float32-le payloadBytes=" + std::to_string(payload.size()));
std::shared_ptr<const particle::ParticleFieldSnapshot> previous;
if (timeline) {
previous = timeline->lastEntry().field;
} else if (pendingHostField) {
previous = std::make_shared<const particle::ParticleFieldSnapshot>(*pendingHostField);
}
if (previous && particle::sameFieldGpuContent(*previous, snapshot)) {
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_DUPLICATE sourceId=" + getID() +
" fieldId=" + snapshot.fieldID + " generation=" + std::to_string(snapshot.generation) +
" validTime=" + std::to_string(snapshot.validTime) + " result=DUPLICATE_NOOP");
return RasterArrayHostFieldSubmitResult::Duplicate;
}
if (previous && (snapshot.validTime <= previous->validTime || snapshot.generation <= previous->generation)) {
Log::Warning(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REJECTED sourceId=" + getID() +
" fieldId=" + snapshot.fieldID + " generation=" + std::to_string(snapshot.generation) +
" validTime=" + std::to_string(snapshot.validTime) +
" result=REJECTED_NON_MONOTONIC_SNAPSHOT");
return RasterArrayHostFieldSubmitResult::RejectedNonMonotonicSnapshot;
}
if (previous && !previous->isTimeInterpolationCompatibleWith(snapshot)) {
Log::Warning(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REJECTED sourceId=" + getID() +
" fieldId=" + snapshot.fieldID + " generation=" + std::to_string(snapshot.generation) +
" validTime=" + std::to_string(snapshot.validTime) + " result=REJECTED_INCOMPATIBLE_SNAPSHOT");
return RasterArrayHostFieldSubmitResult::RejectedIncompatibleSnapshot;
}
if (!pendingHostField && !timeline) {
pendingHostField = std::move(snapshot);
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_ACCEPTED sourceId=" + getID() +
" kind=PENDING_INITIAL_T0 fieldId=" + pendingHostField->fieldID +
" result=ACCEPTED_PENDING_INITIAL_WINDOW");
return RasterArrayHostFieldSubmitResult::AcceptedPendingInitialWindow;
}
if (!timeline) {
const auto initialT0 = *pendingHostField;
const auto initialT1 = snapshot;
if (submitInitialRuntimeFieldWindow(initialT0, initialT1) != RasterArrayRuntimeFieldUpdateResult::Applied) {
return RasterArrayHostFieldSubmitResult::RejectedIncompatibleSnapshot;
}
std::vector<RasterArrayTimelineRuntime::Entry> entries;
entries.push_back({std::make_shared<const particle::ParticleFieldSnapshot>(initialT0), initialT0.validTime});
entries.push_back({std::make_shared<const particle::ParticleFieldSnapshot>(initialT1), initialT1.validTime});
timeline = std::make_shared<RasterArrayTimelineRuntime>(std::move(entries));
auto updated = makeMutable<Impl>(impl());
updated->timeline = timeline;
baseImpl = std::move(updated);
pendingHostField.reset();
observer->onSourceChanged(*this);
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_INITIAL_WINDOW_READY sourceId=" + getID() +
" T0=" + initialT0.fieldID + " T1=" + initialT1.fieldID + " result=INITIAL_WINDOW_READY");
Log::Info(Event::Render, "ParticleTimeline: event=READY snapshots=2 source=HOST_FIELD_SUBMISSION");
return RasterArrayHostFieldSubmitResult::InitialWindowReady;
}
if (timeline->size() >= timeline->cacheCapacity()) {
Log::Warning(Event::Render,
"ParticleField: event=HOST_FIELD_SUBMIT_REJECTED sourceId=" + getID() +
" fieldId=" + snapshot.fieldID + " result=REJECTED_CAPACITY");
return RasterArrayHostFieldSubmitResult::RejectedCapacity;
}
const auto fieldID = snapshot.fieldID;
const auto generation = snapshot.generation;
const auto validTime = snapshot.validTime;
if (!timeline->append({std::make_shared<const particle::ParticleFieldSnapshot>(std::move(snapshot)), validTime})) {
return RasterArrayHostFieldSubmitResult::RejectedCapacity;
}
Log::Info(Event::Render,
"ParticleField: event=HOST_FIELD_NEXT_SUBMIT sourceId=" + getID() + " fieldId=" + fieldID +
" generation=" + std::to_string(generation) + " validTime=" + std::to_string(validTime) +
" snapshots=" + std::to_string(timeline->size()) + " result=ACCEPTED_NEXT_SNAPSHOT");
return RasterArrayHostFieldSubmitResult::AcceptedNextSnapshot;
}
bool RasterArraySource::controlRuntimeTimeline(const int command, const float value) {
if (!timeline) {
Log::Warning(Event::Render, "ParticleTimeline: event=REJECTED reason=TIMELINE_NOT_READY sourceId=" + getID());