AGENT SKILL

wave4-ijobchunk-full-anatomy

Low-level parallel chunk iteration with explicit enableable-component mask handling, type handle management, and per-chunk metadata access.

License 许可
MIT
最近更新
2026-07-25
Stars
★ 8
Forks
0

01 前言 Front Matter

name
wave4-ijobchunk-full-anatomy
tags
[ "jobs", "chunks" ]
metadata
{ "tier": 3, "internal-only": true }
description
Low-level parallel chunk iteration with explicit enableable-component mask handling, type handle management, and per-chunk metadata access.

03 SKILL.md 正文

IJobChunk — Full Anatomy with Enableable Mask

Intent

Low-level parallel chunk iteration with explicit enableable-component mask handling, type handle management, and per-chunk metadata access.

Use When

  • Per-chunk metadata needed: unfilteredChunkIndex, DidChange, SharedComponent values, ChunkHeader.
  • Fine-grained enableable-component masking required.
  • ECB.ParallelWriter sort key must be unfilteredChunkIndex for deterministic parallel playback.
  • IJobEntity source generation is insufficient.

Avoid When

Standard entity iteration without per-chunk metadata — use IJobEntity (less boilerplate, identical performance).

Senior Pattern

[BurstCompile]
public struct MyChunkJob : IJobChunk
{
    public ComponentTypeHandle<Velocity> VelocityHandle;
    [ReadOnly] public ComponentTypeHandle<Speed> SpeedHandle;
    public EntityTypeHandle EntityHandle;
    public EntityCommandBuffer.ParallelWriter Ecb;
    public float DeltaTime;

    [BurstCompile]
    public void Execute(in ArchetypeChunk chunk, int unfilteredChunkIndex,
        bool useEnableMask, in v128 chunkEnabledMask)
    {
        var velocities = chunk.GetNativeArray(ref VelocityHandle);
        var speeds     = chunk.GetNativeArray(ref SpeedHandle);
        var entities   = chunk.GetNativeArray(EntityHandle);

        var enumerator = new ChunkEntityEnumerator(useEnableMask, chunkEnabledMask, chunk.Count);
        while (enumerator.NextEntityIndex(out var i))
        {
            velocities[i] = new Velocity { Value = velocities[i].Value + speeds[i].Value * DeltaTime };
            if (speeds[i].Value > 100f)
                Ecb.RemoveComponent<Speed>(unfilteredChunkIndex, entities[i]);
        }
    }
}

// In OnUpdate:
var job = new MyChunkJob
{
    VelocityHandle = state.GetComponentTypeHandle<Velocity>(),
    SpeedHandle    = state.GetComponentTypeHandle<Speed>(true),
    EntityHandle   = state.GetEntityTypeHandle(),
    Ecb            = ecbSystem.CreateCommandBuffer().AsParallelWriter(),
    DeltaTime      = SystemAPI.Time.DeltaTime
};
state.Dependency = job.ScheduleParallel(myQuery, state.Dependency);

Anti-Patterns

  • Not passing useEnableMask/chunkEnabledMask to ChunkEntityEnumerator — silently processes disabled entities.
  • Caching ComponentTypeHandle<T> across frames — stale after structural changes; always call state.GetComponentTypeHandle<T>() in OnUpdate.
  • Using chunk.Count directly when query has enableable components — iterates disabled entities.

Runtime Risks

  • Stale type handles throw safety errors in development builds; silently corrupt data in release builds.
  • Wrong ECB.ParallelWriter sort key (not unfilteredChunkIndex) = non-deterministic command playback order.

Performance Notes

  • Direct NativeArray access via type handles = maximum cache locality (chunk memory is contiguous).
  • ScheduleParallel distributes whole chunks across workers.
  • Per-chunk scheduling overhead is lower than per-entity scheduling.

Architecture Guidance

Always use unfilteredChunkIndex as ECB.ParallelWriter sort key. Reserve IJobChunk for systems where per-chunk information genuinely changes behavior — otherwise use IJobEntity.

Related Skills

[[ijobchunk-chunk-job]], [[enableable-component]], [[ecb-parallel-writer]], [[job-dependency-chain]]

04 同仓库更多 Skills

dyCuong03
dyCuong03/unity-agent-team

dots-update-groups

How to choose the right ECS system group and ordering so per-frame and per-fixed-step work is deterministic and never races its own readers. Covers InitializationSystemGroup / SimulationSystemGroup / PresentationSystemGroup / FixedStepSimulationSystemGroup / VariableRateSimulationSystemGroup, UpdateBefore / UpdateAfter peer ordering, OrderFirst / OrderLast group-boundary placement, RateManager, and the cross-group ordering trap. Use when adding an ISystem, fixing "value visible one frame late" bugs, or coupling simulation to a fixed timestep.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-transform-patterns

The Entities 1.x transform model — LocalTransform (writable input) vs LocalToWorld (system-owned matrix), Parent/Child hierarchy, reparenting cost, uniform vs non-uniform scale (PostTransformMatrix), and when to opt out with custom transform systems. Use when designing parent/child hierarchies, runtime reparenting (mount/dismount/socket), or choosing transform components at the Baker boundary.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-singleton-patterns

Model cross-system shared configuration and game state as a single-entity component with a strict single-writer contract. Covers RequireForUpdate gating, SystemAPI.GetSingleton / GetSingletonRW / HasSingleton / TryGetSingleton, bake-time vs runtime-created vs ECB-system singletons, the "exactly one entity" runtime invariant, and the single-writer rule. Use when designing frame-stable global config, shared mutable state, or any component intended to exist on exactly one entity.

律师与法务
★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-hybrid-bridge

The one-way bridge between DOTS-owned runtime state and Unity-owned presentation. Covers UnityObjectRef for managed asset refs in components, the DOTS-writes / Unity-reads ownership rule, when to use managed IComponentData vs UnityObjectRef vs CompanionComponent, baker-bridge vs runtime-bridge, and SubScene boundary rules. Use when an entity must reference Unity assets (Mesh, Material, AudioClip, ScriptableObject), a MonoBehaviour view must read entity state, or input/UI must trigger ECS work.

★ 8
dyCuong03
dyCuong03/unity-agent-team

dots-event-driven-ecs

Model one-frame events and external requests in ECS without dual ownership, polling, or persistent state leaks. Covers request-tag pattern with ECB, enableable command components (no structural change), event buffers (IBufferElementData), explicit consumer ownership, one-frame latency rules, and the banned C# event / UnityEvent / static bus anti-patterns. Use when input/UI/network must trigger ECS work, when a one-frame signal fans out to N consumers (damage, hit, collision), or when state changes must be processed exactly once.

★ 8
dyCuong03
dyCuong03/unity-agent-team

valid_skill

A complete, well-formed test fixture skill for SkillHub validation testing. Covers basic task routing and code review for Unity projects.

代码审查测试与质量
★ 8