![]() |
VR-Forces Developer's Guide
|
Manages asynchronous and budgeted foreground loading for all DtModel subclasses.
DtBackgroundLoader is a per-DtDe singleton that coordinates the two-phase load pipeline used by every DtModel-derived object:
Phase 1 – backgroundLoad(): CPU-heavy work (file I/O, mesh construction, etc.) that is safe to run on a background thread. Dispatched as TBB tasks via updateQueue().
Phase 2 – foregroundLoad(): work that must run on the main thread (OSG state upload, ICO submission, etc.). Executed within a per-frame time budget so the renderer is not stalled.
Objects that do not support multithreaded loading (supportsMultithreadedLoad() returns false) bypass Phase 1 and have both phases run on the main thread via processForegroundFullLoadBudgeted(), also within the time budget.
Load flow summary:
Cache payload exchange: backgroundLoad() implementations accumulate cache entries via DtBackgroundLoadContext::addToPayload() instead of calling setCachedData() directly. When a TBB task finishes, ts_stagePayload() posts the payload to myStagedPayloads under a brief mutex lock. At the top of each updateQueue() call, commitStagedPayloads() drains that list on the main thread and commits each payload to its definition via setCachedData(). The mutex is held only for a pointer-swap – no I/O or heavy work occurs under it. In the synchronous bypass paths (executeBackgroundLoad), the payload is committed inline immediately after backgroundLoad() returns, since both calls occur on the same thread.
Book-keeping: myActivelyLoadingModelDefs tracks which model definition names currently have a TBB task in flight, preventing duplicate dispatches for the same definition. Entries are inserted at dispatch time and removed in processForegroundLoadBudgeted() (success) or processFailedTBBTasks() (failure).
Thread safety: Methods prefixed ts_ are safe to call from any thread. All other methods must be called from the main thread only.

Classes | |
| struct | DtBackgroundLoadStats |
| struct | DtPendingIcoQueue |
Public Member Functions | |
| virtual | ~DtBackgroundLoader () |
| virtual void | cleanup () |
| virtual void | updateQueue (int max_thread_count, int active_threads) |
| virtual DtLoaderHandle * | queueForLoad (DtModel *object) |
| virtual void | loadIfPossibleOrAddToQueue (DtModel *object) |
| virtual void | executeBackgroundLoad (DtModel *object) |
| virtual bool | hasActiveAsyncTasks () const |
| virtual int | numModelDefinitionsToIco () const |
| virtual std::string | nextModelDefinitionToIco () |
| virtual int | numOfActiveTasks () const |
| virtual bool | multithreadedLoadingActive () const |
| virtual void | setMultithreadedLoadingActive (bool val) |
| virtual DtBackgroundLoadStats | queryStats () |
| virtual void | waitForActiveTasksAndCommit () |
time management functions | |
how long the system can spend in the main thread before it's stopped. defaults to 2 ms | |
| virtual void | setForegroundTimeBudget (double budget) |
| virtual double | foregroundTimeBudget () const |
| virtual void | incrementForegroundTime (double time_elapse) |
| virtual bool | isForegroundTimeBudgetExpired () const |
| virtual double | timeSpentThisFrame () const |
Public Member Functions inherited from makVrv::DtVirtualBaseClass | |
| DtVirtualBaseClass () | |
| virtual | ~DtVirtualBaseClass () |
Static Public Member Functions | |
| static DtBackgroundLoader & | instance (DtDe &de) |
Protected Member Functions | |
| DtBackgroundLoader (DtDe &de) | |
| virtual void | ts_setModelDefinitionLoaded (const std::string &modelDefinition) |
| virtual bool | ts_isModelDefinitionLoaded (const std::string &modelDefinition) const |
| virtual void | ts_stagePayload (DtDefinitionCachePayload *payload) |
| virtual void | commitStagedPayloads () |
| virtual void | disposeLoaderHandle (DtLoaderHandle *handle) |
| virtual void | timedFullLoad (DtModel *object) |
| virtual void | drainCompletedTBBTasks () |
| virtual void | processFailedTBBTasks () |
| virtual void | processForegroundLoadBudgeted () |
| virtual void | processForegroundFullLoadBudgeted () |
| virtual void | deleteAllPendingHandles () |
| virtual void | dispatchBackgroundTasks (int maxThreadCount, int activeThreads) |
Private Member Functions | |
| DtBackgroundLoader ()=delete | |
| DtBackgroundLoader (const DtBackgroundLoader &)=delete | |
| DtBackgroundLoader & | operator= (const DtBackgroundLoader &)=delete |
Friends | |
| class | DtBackgroundLoadAsyncTask |
|
virtual |
destructor
|
protected |
not allowed directly
|
privatedelete |
not allowed
|
privatedelete |
not allowed
|
static |
singleton access
|
privatedelete |
not allowed
|
virtual |
Blocks until all in-flight background load tasks have completed. Must be called before destroying any resource that a background task may access (e.g. DtModelDefinitionManager).
|
virtual |
called from the foreground main event loop moves stuff from the producer queue into the consumer one, manages time and does foreground loading
|
virtual |
takes ownership of the handle
|
virtual |
returns the configured foreground time budget in seconds
|
virtual |
returns the configured foreground time budget in seconds
|
virtual |
updates how much time has been spend already, allows per frame adjustment of budget
|
virtual |
returns if foreground time is greater then budget
|
virtual |
returns time spend working in the foreground.
|
virtual |
Spawns off a background task if definition hasn't been loaded yet, otherwise runs the foreground loading code if there's enough time. If there's not enough time for foreground loading, the object is queued for the next frame. Called by DtModel based classes when setModelDefinition() is called.
|
virtual |
Executes only the background load portion for the given model, setting up the lease, mutex and context. Does not call foregroundLoad() or update time budgeting. Used by setModelDefinition() in the non-multithreaded path where foregroundLoad() is called by the caller. The payload accumulated during backgroundLoad() is committed to the definition inline, since this always runs on the main thread.
|
virtual |
returns true if there are tasks active in the background
|
virtual |
used by model preloader to decide if there's data to process
|
virtual |
name of the next model to look up definition of and process its ICO data.
|
virtual |
number of current async tasks running
|
virtual |
is the system active or disabled, defaults to on
|
virtual |
controls if the system is active
|
virtual |
Returns a point-in-time snapshot of loader state for display in a GUI.
|
virtual |
Blocks until all in-flight TBB tasks complete, then commits any staged payloads. Does not disable multithreaded loading or delete handles. Used by DtModelDefinitionManager::clearModelInstancingCache() to ensure no background thread is reading cached data before it is cleared. Must be called from the main thread.
|
protectedvirtual |
|
protectedvirtual |
|
protectedvirtual |
Posts a completed payload to myStagedPayloads for later commit on the main thread. Takes ownership of the pointer. Safe to call from any thread. The mutex is held only for the duration of the push_back. Passing a null or empty payload is safe and is a no-op.
|
protectedvirtual |
Drains myStagedPayloads under a brief mutex lock, then commits each payload to its definition on the main thread. Called at the top of updateQueue() before any foreground processing begins. Main thread only.
|
protectedvirtual |
Releases a DtLoaderHandle, routing to the correct owner.
If handle->myObject is non-null, the model is still alive and owns the handle via myInLoadQueueHandle. removedFromLoadQueue() is called, which deletes the handle and clears that pointer. Do NOT delete the handle afterwards.
If handle->myObject is null, the model was destroyed before this call. DtModel::~DtModel cleared myObject but deliberately did not delete the handle, so the loader is the sole owner and must delete it here.
Main thread only.
|
protectedvirtual |
Runs the entire load process (background and foreground) for the given model.
|
protectedvirtual |
Drains the mutex-protected TBB result lists into their main-thread staging lists ready for processing this frame.
|
protectedvirtual |
Cleans up handles from failed background tasks and removes their definition names from myActivelyLoadingModelDefs so future load attempts can be dispatched normally.
|
protectedvirtual |
Processes models that have finished background loading, calling foregroundLoad() on each. Stops early if the foreground time budget is exceeded, leaving remaining items for the next frame.
|
protectedvirtual |
Processes models that cannot use a background thread, calling foregroundLoad() and backgroundLoad() on each. Stops early if the foreground time budget is exceeded, leaving remaining items for the next frame.
|
protectedvirtual |
Deletes all pending handles across all four post-TBB lists without calling foregroundLoad(). Safe to call after DtModelDefinitionObjects have been torn down, specifically from cleanup().
|
protectedvirtual |
Iterates myForegroundQueue and assigns background-capable models to new or existing DtBackgroundLoadAsyncTask instances, then enqueues those tasks into myTaskArena. New tasks are created only when spare thread capacity exists (maxThreadCount - activeThreads - myNumberOfActiveTasks > 2); additional models are batched onto an already-created task otherwise. Handles whose model was destroyed are disposed via disposeLoaderHandle() and removed from the queue. Called at the end of updateQueue(). Main thread only.
|
friend |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
mutableprotected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |
|
protected |