Game.Rendering.BatchManagerSystem
Assembly: Assembly-CSharp
Namespace: Game.Rendering
Type: class
Base: GameSystemBase, IPreDeserialize
Summary:
BatchManagerSystem is the central batching and culling manager used by the rendering pipeline. It owns native batch/group/instance containers, coordinates managed GPU/CPU upload via ManagedBatches, builds per-frame culling draw commands, handles property refreshes (material & instance properties), merges batch groups, and exposes APIs for other systems/mods to query and manipulate batch data. The system schedules multiple Burst-compiled jobs for allocation, sub-batch generation, merging groups, and culling. It also integrates with the engine's BatchRendererGroup through OnPerformCulling.
Fields
-
public const uint GPU_INSTANCE_MEMORY_DEFAULT
{{ GPU instance memory default in bytes (67108864). Used when allocating native buffers for instance data. }} -
public const uint GPU_INSTANCE_MEMORY_INCREMENT
{{ Increment used when growing GPU instance memory (16777216). }} -
public const uint GPU_UPLOADER_CHUNK_SIZE
{{ Chunk size used by the GPU uploader (2097152). }} -
public const uint GPU_UPLOADER_OPERATION_SIZE
{{ Operation size used by the GPU uploader (65536). }} -
public const int MAX_GROUP_BATCH_COUNT
{{ Maximum number of split batches per group (16). }} -
private RenderingSystem m_RenderingSystem
{{ Reference to the RenderingSystem; read flags like motionVectors and lodCrossFade and shadow culling data. }} -
private ManagedBatchSystem m_ManagedBatchSystem
{{ Used to manage textures, VT setup and ManagedBatches integration. }} -
private BatchDataSystem m_BatchDataSystem
{{ Notifies when instance properties change (calls InstancePropertiesUpdated). }} -
private TextureStreamingSystem m_TextureStreamingSystem
{{ Used for getting VT texture parameter blocks when virtual texturing is in use. }} -
private PrefabSystem m_PrefabSystem
{{ Used to query RenderPrefab and component data needed to update batch properties or VT registration. }} -
private NativeBatchGroups<CullingData, GroupData, BatchData, InstanceData> m_NativeBatchGroups
{{ Native container holding per-group and per-batch data. }} -
private NativeBatchInstances<CullingData, GroupData, BatchData, InstanceData> m_NativeBatchInstances
{{ Native container holding per-instance culling and instance-specific data. }} -
private NativeSubBatches<CullingData, GroupData, BatchData, InstanceData> m_NativeSubBatches
{{ Native container for sub-batch generation and renderer recreation. }} -
private ManagedBatches<OptionalProperties> m_ManagedBatches
{{ Managed (C#) helper for uploading material/batch properties and VT textures; used to register material/instance properties and produce native batch properties. }} -
private NativeList<PropertyData> m_MaterialProperties
{{ List of material property metadata (PropertyData) registered from MaterialProperty enum. }} -
private NativeList<PropertyData> m_ObjectProperties
{{ List of instance property metadata for object meshes. }} -
private NativeList<PropertyData> m_NetProperties
{{ List of instance property metadata for net meshes. }} -
private NativeList<PropertyData> m_LaneProperties
{{ List of instance property metadata for lane meshes. }} -
private NativeList<PropertyData> m_ZoneProperties
{{ List of instance property metadata for zone meshes. }} -
private NativeList<Entity> m_MergeMeshes
{{ Temporary list of mesh entities queued for group merge operations. }} -
private NativeParallelMultiHashMap<Entity, int> m_MergeGroups
{{ Mapping mesh entity -> merge group indexes used when merging groups across meshes. }} -
private EntityQuery m_MeshSettingsQuery
{{ Query used to read MeshSettingsData singleton if present. }} -
private bool m_LastMotionVectorsEnabled
{{ Cache of last-known motion vectors enabled state (used to detect changes). }} -
private bool m_LastLodFadeEnabled
{{ Cache of last-known LOD crossfade state. }} -
private bool m_PropertiesChanged
{{ Flag set when global material/instance property definitions change. }} -
private bool m_MotionVectorsChanged
{{ Flag set when motion vectors toggle changed. }} -
private bool m_LodFadeChanged
{{ Flag set when LOD fade toggle changed. }} -
private bool m_VirtualTexturingChanged
{{ Flag set when virtual texturing state changed and needs refresh. }} -
private JobHandle m_NativeBatchGroupsReadDependencies
{{ Job handle tracking jobs that read from m_NativeBatchGroups (to compose dependencies). }} -
private JobHandle m_NativeBatchGroupsWriteDependencies
{{ Job handle tracking jobs that write m_NativeBatchGroups. }} -
private JobHandle m_NativeBatchInstancesReadDependencies
{{ Job handle tracking jobs reading m_NativeBatchInstances. }} -
private JobHandle m_NativeBatchInstancesWriteDependencies
{{ Job handle tracking jobs writing m_NativeBatchInstances. }} -
private JobHandle m_NativeSubBatchesReadDependencies
{{ Job handle tracking readers of m_NativeSubBatches. }} -
private JobHandle m_NativeSubBatchesWriteDependencies
{{ Job handle tracking writers of m_NativeSubBatches. }} -
private JobHandle m_MergeDependencies
{{ Job handle for outstanding merge jobs (MergeGroupsJob + cleanup). }} -
private TypeHandle __TypeHandle
{{ Generated type handle used to fetch BufferLookup handles for buffer access inside jobs. }} -
Nested/Burst job & helper types (MergeGroupsJob, MergeCleanupJob, InitializeLodFadeJob, AllocateBuffersJob, GenerateSubBatchesJob, AllocateCullingJob, CullingPlanesJob, FinalizeCullingJob, BatchCullingJob, ActiveGroupData, CullingSplitData, TypeHandle)
{{ These types implement the various high-performance jobs responsible for merging batch groups, allocating buffers, generating sub-batches, and performing culling (including shadow cascades and split handling). They are Burst-compiled and operate on the native containers. See Methods summary for patterns of use. }}
Properties
- (no public properties)
{{ The system exposes functionality via public methods (GetNativeBatchGroups, GetNativeBatchInstances, GetManagedBatches, etc.) rather than public C# properties. }}
Constructors
public BatchManagerSystem()
{{ Standard parameterless constructor; initialization of native containers happens in OnCreate. Marked with [Preserve] attribute to avoid stripping. }}
Methods
-
protected override void OnCreate()
{{ Initializes subsystems (RenderingSystem, ManagedBatchSystem, BatchDataSystem, TextureStreamingSystem, PrefabSystem), allocates native batch/group/instance/sub-batch containers, creates ManagedBatches instance, registers material & instance properties, and prepares merge data structures. Called when the system is created. }} -
protected override void OnDestroy()
{{ Completes outstanding job dependencies, finalizes managed uploads, disposes ManagedBatches and all native containers and native lists, and clears job handles. Ensures a clean teardown and that all GPU uploads are finished. }} -
public void PreDeserialize(Context context)
{{ Implementation of IPreDeserialize. Completes pending jobs, ends uploads, and removes instances from native containers prior to deserialization so the native state can be reloaded safely. }} -
public bool CheckPropertyUpdates()
{{ Checks whether material/instance property-related settings have changed (motion vectors, LOD fade, virtual texturing) and returns true if any update is required (or if virtual texturing changed). Also updates internal changed flags. }} -
public void VirtualTexturingUpdated()
{{ Marks that virtual texturing configuration changed and batches need to be refreshed. }} -
protected override void OnUpdate()
{{ Main per-frame update. If properties/VT changed it calls RefreshProperties; completes merge jobs and executes MergeGroups if queued; schedules AllocateBuffersJob and GenerateSubBatchesJob and wires their job handles into the system dependency tracking. Adds writers for native containers to enforce dependency ordering. }} -
public NativeBatchGroups<CullingData, GroupData, BatchData, InstanceData> GetNativeBatchGroups(bool readOnly, out JobHandle dependencies)
{{ Return the native batch groups container and provide the appropriate dependencies JobHandle to respect current reader/writer job tracking. If readOnly==true returns write-dependencies (so consumer must wait writers) else returns combined read/write dependency. }} -
public NativeBatchInstances<CullingData, GroupData, BatchData, InstanceData> GetNativeBatchInstances(bool readOnly, out JobHandle dependencies)
{{ Return the native batch instances container and its JobHandle dependencies (same pattern as GetNativeBatchGroups). }} -
public NativeSubBatches<CullingData, GroupData, BatchData, InstanceData> GetNativeSubBatches(bool readOnly, out JobHandle dependencies)
{{ Return the native sub-batches container and the required dependencies handle. }} -
public ManagedBatches<OptionalProperties> GetManagedBatches()
{{ Returns the ManagedBatches instance so other systems/mods can register textures, properties, or access managed batch facilities. }} -
public bool IsMotionVectorsEnabled()
{{ Returns last cached motion vector enable state. }} -
public bool IsLodFadeEnabled()
{{ Returns last cached LOD crossfade enable state. }} -
public void AddNativeBatchGroupsReader(JobHandle jobHandle)
{{ Combine jobHandle into the m_NativeBatchGroupsReadDependencies. Use this when scheduling jobs that read the groups container. }} -
public void AddNativeBatchGroupsWriter(JobHandle jobHandle)
{{ Set m_NativeBatchGroupsWriteDependencies to jobHandle. Use when scheduling jobs that will write into the groups container. }} -
public void AddNativeBatchInstancesReader(JobHandle jobHandle)
{{ Combine jobHandle into m_NativeBatchInstancesReadDependencies. }} -
public void AddNativeBatchInstancesWriter(JobHandle jobHandle)
{{ Set m_NativeBatchInstancesWriteDependencies to jobHandle. }} -
public void AddNativeSubBatchesReader(JobHandle jobHandle)
{{ Combine jobHandle into m_NativeSubBatchesReadDependencies. }} -
public void AddNativeSubBatchesWriter(JobHandle jobHandle)
{{ Set m_NativeSubBatchesWriteDependencies to jobHandle. }} -
public void MergeGroups(Entity meshEntity, int mergeIndex)
{{ Queue a merge request that will be processed in the next OnUpdate MergeGroups step. Ensures previous merge dependencies are completed before enqueuing. If this meshEntity was not previously queued it is added to m_MergeMeshes; the mapping (meshEntity -> mergeIndex) is stored in m_MergeGroups. }} -
public (int, int) GetVTTextureParamBlockID(int stackConfigIndex)
{{ Helper to fetch the material property block IDs used for virtual texturing texture parameter blocks. Accepts stackConfigIndex (0 or 1) and returns two property IDs. Throws if index is out of range. }} -
public PropertyData GetPropertyData(MaterialProperty property)
{{ Lookup for material property metadata from the m_MaterialProperties list. }} -
public PropertyData GetPropertyData(ObjectProperty property)
{{ Lookup for object instance property metadata. }} -
public PropertyData GetPropertyData(NetProperty property)
{{ Lookup for net instance property metadata. }} -
public PropertyData GetPropertyData(LaneProperty property)
{{ Lookup for lane instance property metadata. }} -
public PropertyData GetPropertyData(ZoneProperty property)
{{ Lookup for zone instance property metadata. }} -
private void RefreshProperties(bool propertiesChanged, bool motionVectorsChanged, bool lodFadeChanged, bool virtualTexturingChanged)
{{ Iterates all valid native batch groups and their batches, reevaluates and (re)generates batch properties using ManagedBatches where necessary, updates VT registration for batches, updates batch defaults, and optionally initializes LOD fade values for existing instances. If instance properties changed it triggers BatchDataSystem.InstancePropertiesUpdated. This method completes native dependencies before changing native data. }} -
private void MergeGroups()
{{ Performs the queued merges by starting native group & instance updaters, scheduling a MergeGroupsJob (Burst) to perform per-mesh merges into target groups, schedules cleanup job to clear the merge queues, and wires job dependencies into native container dependency tracking. }} -
private JobHandle OnPerformCulling(BatchRendererGroup rendererGroup, BatchCullingContext cullingContext, BatchCullingOutput cullingOutput, IntPtr userContext)
{{ This callback is provided to the engine's BatchRendererGroup. It prepares active group data, builds culling split data & plane packets, then schedules AllocateCullingJob, CullingPlanesJob (precompute split data & plane packets), BatchCullingJob (parallel per-active-group culling/build draw commands), and FinalizeCullingJob (compact draw command arrays). It disposes temporaries and propagates reader dependencies for native containers. Returns a JobHandle that the renderer will wait on before using cullingOutput. }} -
Several nested Burst jobs and helpers: MergeGroupsJob, MergeCleanupJob, InitializeLodFadeJob, AllocateBuffersJob, GenerateSubBatchesJob, AllocateCullingJob, CullingPlanesJob, FinalizeCullingJob, BatchCullingJob.
{{ These are implemented as IJob / IJobParallelFor and perform all heavy-lifting on worker threads with Burst: merging groups, clearing merge lists, initializing per-instance LOD fade values, allocating property/instance buffers, generating sub-batches, preparing culling allocations, computing plane packets & split masks, finalizing draw ranges, and the actual culling & draw-command generation. They operate directly on the native batch/group/instance containers and often use unsafe pointers for performance. }}
Usage Example
// From another system or mod code — get the system and call some helpers.
var batchManager = World.GetOrCreateSystemManaged<Game.Rendering.BatchManagerSystem>();
// Queue a merge (meshEntity is an Entity that has Mesh batches, mergeIndex is the target group index)
batchManager.MergeGroups(meshEntity, mergeIndex);
// Query managed batches for registering textures or other managed operations
var managedBatches = batchManager.GetManagedBatches();
// Get VT texture parameter IDs (0 or 1)
var (paramA, paramB) = batchManager.GetVTTextureParamBlockID(0);
// Query property metadata for a material/instance property enum (example enum value)
PropertyData matProp = batchManager.GetPropertyData((MaterialProperty)0); // replace with real enum
Notes: - The system expects other systems to respect the dependency model: use the AddNative*Reader/Writer helpers when scheduling jobs that access the native containers. - Most heavy work runs in Burst jobs and uses native containers; avoid accessing internal native data directly from managed code unless you have completed the appropriate JobHandle dependencies. - Virtual texturing, motion vectors and LOD fade toggles are detected and trigger refreshing of batch properties and instance data.