Game.Rendering.InitializeLightsSystem
Assembly: Assembly-CSharp.dll
Namespace: Game.Rendering
Type: class
Base: GameSystemBase
Summary:
InitializeLightsSystem is a Unity ECS system used by the game's rendering pipeline to initialize and maintain per-instance procedural emissive/light data for prefabs. It coordinates with ProceduralEmissiveSystem and PreCullingSystem to:
- Allocate and free native heap blocks for emissive/light buffers (using a NativeHeapAllocator returned by ProceduralEmissiveSystem).
- Create and resize DynamicBuffer
The system is responsible for ensuring proper dependencies by combining its scheduled job with returned dependency handles and by registering as a heap writer and culling-data reader with the respective systems.
Fields
-
private ProceduralEmissiveSystem m_ProceduralEmissiveSystem
Holds a managed reference to the ProceduralEmissiveSystem. Used to get a NativeHeapAllocator, allocation info, and allocation remove queue, and to register the scheduled job as a heap writer. -
private PreCullingSystem m_PreCullingSystem
Managed reference to the PreCullingSystem. Used to obtain updated culling data (PreCullingData array) and to register the job as a culling-data reader. -
private TypeHandle __TypeHandle
Container struct that caches ComponentLookup/BufferLookup handles used by the scheduled job (PrefabRef, ProceduralLight, SubMesh, Emissive, LightState). It has an __AssignHandles(ref SystemState) method that binds lookups from the SystemState.
Properties
- None (the system does not expose public properties).
Constructors
public InitializeLightsSystem()
Default constructor. Marked with [Preserve] in the source to avoid stripping; no runtime initialization beyond standard System construction. Actual system initialization occurs in OnCreate.
Methods
[Preserve] protected override void OnCreate()
Initializes managed references to dependent systems:- Calls base.OnCreate().
-
Gets or creates ProceduralEmissiveSystem and PreCullingSystem from World and stores them in m_ProceduralEmissiveSystem and m_PreCullingSystem.
-
[Preserve] protected override void OnUpdate()
Main per-frame logic. Steps: - Requests heap allocator, allocation info, allocation-remove queue, current time and dependency handle from ProceduralEmissiveSystem via GetHeapAllocator(...).
- Requests updated culling data from PreCullingSystem (GetUpdatedData).
- Schedules the Burst-compiled ProceduralInitializeJob with combined dependencies: base.Dependency, the dependencies returned by PreCullingSystem, and the dependency from ProceduralEmissiveSystem.
- Registers the scheduled JobHandle with ProceduralEmissiveSystem.AddHeapWriter and PreCullingSystem.AddCullingDataReader.
-
Stores the JobHandle back into base.Dependency so the scheduler tracks it.
-
protected override void OnCreateForCompiler()
Called by compiler-generated initialization paths; assigns queries (currently an empty query) and calls __TypeHandle.__AssignHandles to populate component/buffer lookups. -
[MethodImpl(MethodImplOptions.AggressiveInlining)] private void __AssignQueries(ref SystemState state)
Generated no-op in this implementation; placeholder for query initialization.
Nested / Job types (important behavior):
[BurstCompile] private struct ProceduralInitializeJob : IJob
The Burst job that does most of the work. Key points:- Input lookups: ComponentLookup
, BufferLookup , BufferLookup . - Read/write buffer lookups: BufferLookup
, BufferLookup . - Additional inputs: NativeList
m_CullingData (the updated culling entries), int m_CurrentTime. - Heap/alloc management: NativeHeapAllocator m_HeapAllocator, NativeReference
m_AllocationInfo, NativeQueue m_AllocationRemoves. - Execute() iterates m_CullingData and for each entry with Emissive flag and updated flags:
- If not NearCamera, calls Remove(...) to clear emissive and light buffers and enqueue allocations for removal.
- If NearCamera, calls Update(...) to:
- Find associated SubMesh buffer from the prefab reference.
- Determine total number of procedural lights across submeshes.
- Allocate a NativeHeapBlock via m_HeapAllocator for each submesh that has procedural lights.
- Resize DynamicBuffer
to match submesh count and DynamicBuffer to match total lights. - Initialize Emissive entries with buffer allocation, offsets and mark updated; initialize LightState entries to default zero intensity and color.
- If allocation fails, m_HeapAllocator.Resize is invoked to grow heap and retry.
- Deallocate(...) inspects existing Emissive entries and enqueues their allocations (if present) in m_AllocationRemoves with the current time for later reclamation.
-
The job must be scheduled with dependencies returned by the systems and uses Burst for performance.
-
private struct TypeHandle
Holds ComponentLookup/BufferLookup instances and the method __AssignHandles(ref SystemState) used to bind them to the SystemState. Lookups include: - ComponentLookup
(read-only) - BufferLookup
(read-only) - BufferLookup
(read-only) - BufferLookup
(read/write) - BufferLookup
(read/write)
Other helper/interop methods:
- private void __AssignQueries(ref SystemState state)
— empty generated method used in compiler-init paths.
- protected override void OnCreateForCompiler()
— ensures lookups are assigned for compiled contexts.
Usage Example
[Preserve]
protected override void OnCreate()
{
base.OnCreate();
// Typical initialization performed by this system:
m_ProceduralEmissiveSystem = World.GetOrCreateSystemManaged<ProceduralEmissiveSystem>();
m_PreCullingSystem = World.GetOrCreateSystemManaged<PreCullingSystem>();
}
Notes and implementation hints for modders: - This system expects ProceduralEmissiveSystem to provide a NativeHeapAllocator and synchronization handles — do not bypass those mechanisms when integrating with or altering allocation behavior. - Emissive allocations are not freed immediately; they are enqueued with a remove time (m_CurrentTime) so ProceduralEmissiveSystem can reclaim memory safely later. - The job resizes emissive/light buffers on demand. If you add or change ProceduralLight/SubMesh relationships in prefabs, be aware that this will trigger allocations/deallocations at runtime for affected entities. - Because the job is Burst-compiled and uses native containers, any changes to job fields or container usage must adhere to Unity's job-system safety rules (e.g., proper readonly annotations and dependency chaining).