Skip to content

Game.Rendering.BatchRendererSystem

Assembly: Assembly-CSharp (game code)
Namespace: Game.Rendering

Type: class

Base: GameSystemBase

Summary:
BatchRendererSystem is a game system responsible for synchronizing and maintaining renderable batches between the native batch/sub-batch data structures and the managed Batch/Renderer objects. It coordinates with BatchManagerSystem and BatchMeshSystem to: - remove obsolete managed batches and their meshes, - recreate renderers for updated groups, - add new managed batch renderers for sub-batches that do not yet have a managed BatchID, - clear obsolete batch renderers and updated metadata via a Burst-compiled job (ClearUpdatedMetaDatasJob).

The system uses Preserve attributes to avoid managed stripping and interacts with NativeBatchGroups/NativeSubBatches and ManagedBatches. It completes native job dependencies on the main thread, then performs managed changes (add/remove) before scheduling a small Burst job to clear updated metadata and registering that job as a writer with BatchManagerSystem.


Fields

  • private BatchManagerSystem m_BatchManagerSystem
    Description: Reference to the BatchManagerSystem used to access native batch groups, sub-batches and managed batches. Obtained in OnCreate via World.GetOrCreateSystemManaged().

  • private BatchMeshSystem m_BatchMeshSystem
    Description: Reference to the BatchMeshSystem used to remove batch meshes when managed batches are removed. Obtained in OnCreate via World.GetOrCreateSystemManaged().

  • private struct ClearUpdatedMetaDatasJob : IJob (nested, Burst-compiled)
    Description: A small Burst-compiled job that holds a NativeBatchGroups instance and, when executed, calls ClearObsoleteManagedBatches() and ClearUpdatedMetaDatas() on it. Scheduled at the end of OnUpdate to clear metadata in native structures after managed changes are queued.

Properties

  • None (the system exposes no public properties)

Constructors

  • public BatchRendererSystem()
    Description: Default constructor. The class relies on OnCreate for initialization of references to other systems.

Methods

  • [Preserve] protected override void OnCreate() : System.Void
    Description: Initializes the system by retrieving/creating references to BatchManagerSystem and BatchMeshSystem from the World:
  • m_BatchManagerSystem = base.World.GetOrCreateSystemManaged();
  • m_BatchMeshSystem = base.World.GetOrCreateSystemManaged(); Notes: Marked with Preserve to prevent stripping.

  • [Preserve] protected override void OnUpdate() : System.Void
    Description: Core synchronization logic executed every update. Major steps:

  • Acquire NativeBatchGroups (readOnly: true) and NativeSubBatches (readOnly: false) from BatchManagerSystem. Receive JobHandle dependencies for any currently running producers.
  • Complete those dependencies on the main thread (dependencies.Complete()/dependencies2.Complete()) before performing managed changes.
  • Iterate obsolete managed batches from nativeBatchGroups, remove their meshes via m_BatchMeshSystem.RemoveBatch, remove them from managedBatches, and Dispose the CustomBatch instance.
  • Iterate updated metadata groups and call nativeSubBatches.RecreateRenderers(groupIndex) for each updated group.
  • Iterate obsolete batch renderers from nativeSubBatches and remove them from managedBatches.
  • Clear obsolete batch renderers in nativeSubBatches, then iterate updated batch renderers and for each sub-batch without a BatchID, add a managed renderer (managedBatches.AddBatchRenderer) and set the assigned BatchID into nativeSubBatches.
  • Clear updated batch renderers in nativeSubBatches.
  • Schedule a ClearUpdatedMetaDatasJob (Burst compiled) to clear updated metadata and obsolete managed batches in nativeBatchGroups. Register the returned JobHandle via m_BatchManagerSystem.AddNativeBatchGroupsWriter(jobHandle) so BatchManagerSystem knows a writer is active.

Notes: - OnUpdate mixes native (jobs) and managed work. It completes producer job handles before touching managed objects, then schedules a writer job to safely clear native metadata afterward. - Uses Preserve attribute to avoid stripping.

  • private struct ClearUpdatedMetaDatasJob.Execute() : System.Void
    Description: Implementation of IJob.Execute for the nested job. Calls:
  • m_NativeBatchGroups.ClearObsoleteManagedBatches();
  • m_NativeBatchGroups.ClearUpdatedMetaDatas();

Usage Example

// Example showing how BatchRendererSystem is obtained in another system.
// Typical retrieval is done in OnCreate of another GameSystemBase-derived system.

[Preserve]
protected override void OnCreate()
{
    base.OnCreate();
    // Acquire the BatchRendererSystem (creates it if missing).
    var batchRendererSystem = base.World.GetOrCreateSystemManaged<Game.Rendering.BatchRendererSystem>();
    // You normally don't call its internals directly; it runs its synchronization in OnUpdate.
}

Additional notes for modders: - The system is internal to the game's rendering pipeline; it expects the native BatchManager structures to be present and consistent. - Avoid calling OnUpdate yourself; interact with the system by modifying native batch groups/sub-batches via BatchManagerSystem APIs if required. - The Burst-compiled ClearUpdatedMetaDatasJob depends on a valid NativeBatchGroups instance; the job is scheduled from the main thread and the resulting JobHandle is passed to BatchManagerSystem as a writer so other systems can respect the dependency.