feat(nft): let a consumer supply its own NFTDetector

Open
#115 2 comments 0 reactions 1 assignee View on GitHub

Maintainers usually reply within 1 day

@kalwalt is already working on this.

Since Nov 17, 2026.

Assessment

This issue has not been assessed yet.

Description

C/C++ code documentation Emscripten enhancement

Summary

Give ARToolKitNFTCore an injection point for an externally supplied NFTDetector, so a consumer can run the KPM pass wherever it wants — in a Web Worker, for instance — without WebARKitLib knowing anything about it.

No worker code, no JavaScript and no new dependency enters this repository: only a third Detector value, a factory in the config, a hook that tells the detector what the core accepted, and one documentation fix.

Why

kalwalt/WebAR-Canvas#16 needs the KPM pass off the browser's main thread: while nothing is tracked the core detects on every frame, and a pass costs about 45 ms, which holds the app near 20 fps.

The threaded path already in this repository does solve it — measured in that issue's spike, the mean main-thread tick goes from 20.0 ms to 1.07 ms and long frames from 181 to 1 per 10 s. But WEBARKIT_NFT_THREADS compiles to pthreads, which on the web means SharedArrayBuffer, which means the page must be cross-origin isolated (COOP/COEP). That was verified, not assumed: without the headers the app does not start at all (DataCloneError: SharedArrayBuffer transfer requires self.crossOriginIsolated), and a pthreads build therefore cannot fall back at run time — the consumer has to ship two builds and choose at load.

A detector that lives in a Web Worker with its own WASM instance needs none of that: no shared memory, no isolation, one build, and a page that works on GitHub Pages and inside other sites. The cost is a frame copy per pass and code in the consumer, which is where it belongs.

What is already here

NFTDetector is already the right abstraction: start(), collect(), idle(), waitIdle(), with SyncKpmDetector and ThreadedKpmDetector behind it, and its documentation already allows a pass that does not finish inside start().

What is missing is only the way in. ARToolKitNFTCore::createDetector() is private, not virtual, and switches on config.detector over the two implementations compiled into this library. The detector is created lazily, on the first addNFTMarkers(), and reused for later batches.

Proposed change

  1. A third value, NFTTrackingConfig::Detector::External.
  2. A factory in NFTTrackingConfig, in the style of the clock pointer that is already there, with a context pointer beside it, because one tracker owns one detector and a bare function pointer carries no state:
class NFTDetector;  // forward-declared, so the header still needs no AR/ar.h
 
std::unique_ptr<NFTDetector> (*makeDetector)(void *user) = nullptr;
void *makeDetectorUser = nullptr;
  1. A branch at the top of createDetector() for External, which returns config.makeDetector(config.makeDetectorUser) and logs an error when the factory is null. It works in builds without WEBARKIT_NFT_THREADS too.
  2. A hook on NFTDetector, default no-op, telling the detector what the core accepted:
/** Called after the core accepted a batch of markers, with the pages it assigned. Default: nothing. */
virtual void markersAdded(const std::vector<int> &pages,
                          const std::vector<std::string> &datasetPathnames) {}
 
/** Called when the core loaded or replaced the camera. Default: nothing. */
virtual void cameraChanged(/* see below */) {}

The signatures are a proposal to refine. The requirement behind them: whatever these hooks pass must be usable by a detector that does not share memory with the core — values or file paths, never a pointer into the core's heap (an ARParamLT * would be useless to another WASM instance).

Without this hook the consumer has to forward the camera and the dataset paths itself and rely on loading markers one at a time so that its own numbering matches the core's. That works, but the agreement is implicit: if the core's page numbering ever changed, an external detector would report poses for the wrong markers and nothing would notice. The hook makes the contract explicit, which is why it is worth the few lines.

  1. waitIdle()'s documentation. It currently says "Blocks until no pass is running". A detector that runs in a worker cannot block the main thread; it can only abandon the pass. That is safe here — the three dropRunningSearch() call sites (setupAR2(), deleteCamera(), and addNFTMarkers() before kpmSetRefDataSet()) exist so that no pass reads a KPM handle while it is replaced, and the code's own comment already says the abandoned pass's result belongs to the old marker set and is dropped. A detector that shares no memory has nothing to protect; the core only needs idle() to be true afterwards and the next collect() to return false.
    So the comment should read, in substance: ends the running pass — waits for it (threaded) or abandons it (external); its result is never collected. An external detector is then expected to discard a late result of an abandoned pass, typically with a generation counter of its own.

Non-goals

  • No worker, JavaScript or Emscripten code in this repository.
  • Sync and Threaded behave exactly as today; singleThreadPreset() and threadedPreset() are untouched.
  • No existing API breaks: the enum gains a value, the config gains two fields defaulting to null, NFTDetector gains two virtuals with default implementations.

Acceptance criteria

  • A test detector supplied through the factory receives start(), is collected from, and drives the core to a pose, in a build with WEBARKIT_NFT_THREADS off.
  • config.detector == External with a null factory fails with a clear error instead of crashing.
  • The hooks report the same pages the core returns from addNFTMarkers(), for a batch of more than one marker.
  • Abandoning a pass leaves the core able to start the next one; a result delivered after the abandon is not mistaken for a current one.
  • The existing single-thread and threaded tests still pass, unchanged.

Size

About 30 lines of library code and about 40 of tests, as estimated from the spike in kalwalt/WebAR-Canvas#16.

References

  • kalwalt/WebAR-Canvas#16 — the consumer, with the spike's measurements and the isolation findings.
  • #94 and kalwalt/WebAR-Canvas#11 — the precedent for an upstream change driven by a consumer.
Dominant language
C++
Stars
21
Forks
8
Avg merge
3h 42m
Merged PRs (30d)
22

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from webarkit/WebARKitLib

All issues in webarkit/WebARKitLib

Similar issues

More C++ issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.