| | | 1 | | // Copyright (c) 2026 Sideline Authors. All rights reserved. |
| | | 2 | | // Licensed under GPL-3.0. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Runtime.CompilerServices; |
| | | 6 | | using Lattice.Core; |
| | | 7 | | |
| | | 8 | | namespace Lattice.ECS.Core |
| | | 9 | | { |
| | | 10 | | internal unsafe interface IFrameOwningGroup |
| | | 11 | | { |
| | | 12 | | bool DependsOn(int typeId); |
| | | 13 | | |
| | | 14 | | void Reinitialize(Allocator* allocator, int entityCapacity); |
| | | 15 | | |
| | | 16 | | void SyncEntity(Frame frame, EntityRef entity, int changedTypeId); |
| | | 17 | | |
| | | 18 | | void Rebuild(Frame frame); |
| | | 19 | | } |
| | | 20 | | |
| | | 21 | | /// <summary> |
| | | 22 | | /// 显式注册的双组件 Owning Group。 |
| | | 23 | | /// 该结构由 Frame 自动维护,适合极少数固定热点系统的高频遍历。 |
| | | 24 | | /// </summary> |
| | | 25 | | public unsafe sealed class OwningGroup<T1, T2> : IFrameOwningGroup |
| | | 26 | | where T1 : unmanaged, IComponent |
| | | 27 | | where T2 : unmanaged, IComponent |
| | | 28 | | { |
| | | 29 | | private readonly int _typeId1; |
| | | 30 | | private readonly int _typeId2; |
| | | 31 | | private int _entityCapacity; |
| | | 32 | | private int* _entityToDenseIndex; |
| | | 33 | | private FullOwningGroup<T1, T2> _group; |
| | | 34 | | |
| | | 35 | | internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2) |
| | | 36 | | { |
| | | 37 | | _typeId1 = typeId1; |
| | | 38 | | _typeId2 = typeId2; |
| | | 39 | | Reinitialize(allocator, entityCapacity); |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | public int Count => _group.Count; |
| | | 43 | | |
| | | 44 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 45 | | public bool NextBlock(int* blockIndex, out EntityRef* entities, out T1* data1, out T2* data2, out int count) |
| | | 46 | | { |
| | | 47 | | return _group.NextBlock(blockIndex, out entities, out data1, out data2, out count); |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 51 | | public bool Next(int* globalIndex, out EntityRef entity, out T1* c1, out T2* c2) |
| | | 52 | | { |
| | | 53 | | return _group.Next(globalIndex, out entity, out c1, out c2); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 57 | | public bool DependsOn(int typeId) |
| | | 58 | | { |
| | | 59 | | return typeId == _typeId1 || typeId == _typeId2; |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | public void Reinitialize(Allocator* allocator, int entityCapacity) |
| | | 63 | | { |
| | | 64 | | _entityCapacity = entityCapacity; |
| | | 65 | | _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity); |
| | | 66 | | _group = default; |
| | | 67 | | _group.Initialize(allocator); |
| | | 68 | | ResetDenseIndexMap(); |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId) |
| | | 72 | | { |
| | | 73 | | _ = changedTypeId; |
| | | 74 | | |
| | | 75 | | int denseIndex = GetDenseIndex(entity.Index); |
| | | 76 | | if (!ShouldInclude(frame, entity)) |
| | | 77 | | { |
| | | 78 | | if (denseIndex >= 0) |
| | | 79 | | { |
| | | 80 | | RemoveDenseIndex(entity.Index, denseIndex); |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | T1* component1 = frame.GetPointer<T1>(entity); |
| | | 87 | | T2* component2 = frame.GetPointer<T2>(entity); |
| | | 88 | | if (component1 == null || component2 == null) |
| | | 89 | | { |
| | | 90 | | if (denseIndex >= 0) |
| | | 91 | | { |
| | | 92 | | RemoveDenseIndex(entity.Index, denseIndex); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | return; |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | if (denseIndex >= 0) |
| | | 99 | | { |
| | | 100 | | if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2)) |
| | | 101 | | { |
| | | 102 | | *group1 = *component1; |
| | | 103 | | *group2 = *component2; |
| | | 104 | | } |
| | | 105 | | |
| | | 106 | | return; |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | int newIndex = _group.Count; |
| | | 110 | | _group.Add(entity, *component1, *component2); |
| | | 111 | | _entityToDenseIndex[entity.Index] = newIndex; |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public void Rebuild(Frame frame) |
| | | 115 | | { |
| | | 116 | | ResetDenseIndexMap(); |
| | | 117 | | while (_group.Count > 0) |
| | | 118 | | { |
| | | 119 | | _group.RemoveAtLinearIndex(_group.Count - 1, out _); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | var query = frame.Query<T1, T2>(); |
| | | 123 | | var enumerator = query.GetEnumerator(); |
| | | 124 | | while (enumerator.MoveNext()) |
| | | 125 | | { |
| | | 126 | | int denseIndex = _group.Count; |
| | | 127 | | _group.Add(enumerator.Entity, *enumerator.Component1Ptr, *enumerator.Component2Ptr); |
| | | 128 | | _entityToDenseIndex[enumerator.Entity.Index] = denseIndex; |
| | | 129 | | } |
| | | 130 | | } |
| | | 131 | | |
| | | 132 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 133 | | private bool ShouldInclude(Frame frame, EntityRef entity) |
| | | 134 | | { |
| | | 135 | | if (!frame.IsValid(entity)) |
| | | 136 | | { |
| | | 137 | | return false; |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | return frame.HasComponentBit(entity.Index, _typeId1) |
| | | 141 | | && frame.HasComponentBit(entity.Index, _typeId2); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 145 | | private int GetDenseIndex(int entityIndex) |
| | | 146 | | { |
| | | 147 | | return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1; |
| | | 148 | | } |
| | | 149 | | |
| | | 150 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 151 | | private void RemoveDenseIndex(int entityIndex, int denseIndex) |
| | | 152 | | { |
| | | 153 | | if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity)) |
| | | 154 | | { |
| | | 155 | | return; |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | _entityToDenseIndex[entityIndex] = -1; |
| | | 159 | | if (movedEntity != EntityRef.None) |
| | | 160 | | { |
| | | 161 | | _entityToDenseIndex[movedEntity.Index] = denseIndex; |
| | | 162 | | } |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | private void ResetDenseIndexMap() |
| | | 166 | | { |
| | | 167 | | for (int i = 0; i < _entityCapacity; i++) |
| | | 168 | | { |
| | | 169 | | _entityToDenseIndex[i] = -1; |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | /// <summary> |
| | | 175 | | /// 显式注册的三组件 Owning Group。 |
| | | 176 | | /// </summary> |
| | | 177 | | public unsafe sealed class OwningGroup<T1, T2, T3> : IFrameOwningGroup |
| | | 178 | | where T1 : unmanaged, IComponent |
| | | 179 | | where T2 : unmanaged, IComponent |
| | | 180 | | where T3 : unmanaged, IComponent |
| | | 181 | | { |
| | | 182 | | private readonly int _typeId1; |
| | | 183 | | private readonly int _typeId2; |
| | | 184 | | private readonly int _typeId3; |
| | | 185 | | private int _entityCapacity; |
| | | 186 | | private int* _entityToDenseIndex; |
| | | 187 | | private FullOwningGroup<T1, T2, T3> _group; |
| | | 188 | | |
| | 1 | 189 | | internal OwningGroup(Allocator* allocator, int entityCapacity, int typeId1, int typeId2, int typeId3) |
| | | 190 | | { |
| | 1 | 191 | | _typeId1 = typeId1; |
| | 1 | 192 | | _typeId2 = typeId2; |
| | 1 | 193 | | _typeId3 = typeId3; |
| | 1 | 194 | | Reinitialize(allocator, entityCapacity); |
| | 1 | 195 | | } |
| | | 196 | | |
| | 3 | 197 | | public int Count => _group.Count; |
| | | 198 | | |
| | | 199 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 200 | | public bool NextBlock(int* blockIndex, out EntityRef* entities, out T1* data1, out T2* data2, out T3* data3, out |
| | | 201 | | { |
| | 0 | 202 | | return _group.NextBlock(blockIndex, out entities, out data1, out data2, out data3, out count); |
| | | 203 | | } |
| | | 204 | | |
| | | 205 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 206 | | public bool Next(int* globalIndex, out EntityRef entity, out T1* c1, out T2* c2, out T3* c3) |
| | | 207 | | { |
| | 4 | 208 | | return _group.Next(globalIndex, out entity, out c1, out c2, out c3); |
| | | 209 | | } |
| | | 210 | | |
| | | 211 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 212 | | public bool DependsOn(int typeId) |
| | | 213 | | { |
| | 3 | 214 | | return typeId == _typeId1 || typeId == _typeId2 || typeId == _typeId3; |
| | | 215 | | } |
| | | 216 | | |
| | | 217 | | public void Reinitialize(Allocator* allocator, int entityCapacity) |
| | | 218 | | { |
| | 1 | 219 | | _entityCapacity = entityCapacity; |
| | 1 | 220 | | _entityToDenseIndex = (int*)allocator->Alloc(sizeof(int) * entityCapacity); |
| | 1 | 221 | | _group = default; |
| | 1 | 222 | | _group.Initialize(allocator); |
| | 1 | 223 | | ResetDenseIndexMap(); |
| | 1 | 224 | | } |
| | | 225 | | |
| | | 226 | | public void SyncEntity(Frame frame, EntityRef entity, int changedTypeId) |
| | | 227 | | { |
| | | 228 | | _ = changedTypeId; |
| | | 229 | | |
| | 3 | 230 | | int denseIndex = GetDenseIndex(entity.Index); |
| | 3 | 231 | | if (!ShouldInclude(frame, entity)) |
| | | 232 | | { |
| | 1 | 233 | | if (denseIndex >= 0) |
| | | 234 | | { |
| | 1 | 235 | | RemoveDenseIndex(entity.Index, denseIndex); |
| | | 236 | | } |
| | | 237 | | |
| | 1 | 238 | | return; |
| | | 239 | | } |
| | | 240 | | |
| | 2 | 241 | | T1* component1 = frame.GetPointer<T1>(entity); |
| | 2 | 242 | | T2* component2 = frame.GetPointer<T2>(entity); |
| | 2 | 243 | | T3* component3 = frame.GetPointer<T3>(entity); |
| | 2 | 244 | | if (component1 == null || component2 == null || component3 == null) |
| | | 245 | | { |
| | 0 | 246 | | if (denseIndex >= 0) |
| | | 247 | | { |
| | 0 | 248 | | RemoveDenseIndex(entity.Index, denseIndex); |
| | | 249 | | } |
| | | 250 | | |
| | 0 | 251 | | return; |
| | | 252 | | } |
| | | 253 | | |
| | 2 | 254 | | if (denseIndex >= 0) |
| | | 255 | | { |
| | 1 | 256 | | if (_group.GetAtLinearIndex(denseIndex, out EntityRef _, out T1* group1, out T2* group2, out T3* group3) |
| | | 257 | | { |
| | 1 | 258 | | *group1 = *component1; |
| | 1 | 259 | | *group2 = *component2; |
| | 1 | 260 | | *group3 = *component3; |
| | | 261 | | } |
| | | 262 | | |
| | 1 | 263 | | return; |
| | | 264 | | } |
| | | 265 | | |
| | 1 | 266 | | int newIndex = _group.Count; |
| | 1 | 267 | | _group.Add(entity, *component1, *component2, *component3); |
| | 1 | 268 | | _entityToDenseIndex[entity.Index] = newIndex; |
| | 1 | 269 | | } |
| | | 270 | | |
| | | 271 | | public void Rebuild(Frame frame) |
| | | 272 | | { |
| | 1 | 273 | | ResetDenseIndexMap(); |
| | 1 | 274 | | while (_group.Count > 0) |
| | | 275 | | { |
| | 0 | 276 | | _group.RemoveAtLinearIndex(_group.Count - 1, out _); |
| | | 277 | | } |
| | | 278 | | |
| | 1 | 279 | | var query = frame.Query<T1, T2, T3>(); |
| | 1 | 280 | | var enumerator = query.GetEnumerator(); |
| | 1 | 281 | | while (enumerator.MoveNext()) |
| | | 282 | | { |
| | 0 | 283 | | int denseIndex = _group.Count; |
| | 0 | 284 | | _group.Add( |
| | 0 | 285 | | enumerator.Entity, |
| | 0 | 286 | | *enumerator.Component1Ptr, |
| | 0 | 287 | | *enumerator.Component2Ptr, |
| | 0 | 288 | | *enumerator.Component3Ptr); |
| | 0 | 289 | | _entityToDenseIndex[enumerator.Entity.Index] = denseIndex; |
| | | 290 | | } |
| | 1 | 291 | | } |
| | | 292 | | |
| | | 293 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 294 | | private bool ShouldInclude(Frame frame, EntityRef entity) |
| | | 295 | | { |
| | 3 | 296 | | if (!frame.IsValid(entity)) |
| | | 297 | | { |
| | 0 | 298 | | return false; |
| | | 299 | | } |
| | | 300 | | |
| | 3 | 301 | | return frame.HasComponentBit(entity.Index, _typeId1) |
| | 3 | 302 | | && frame.HasComponentBit(entity.Index, _typeId2) |
| | 3 | 303 | | && frame.HasComponentBit(entity.Index, _typeId3); |
| | | 304 | | } |
| | | 305 | | |
| | | 306 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 307 | | private int GetDenseIndex(int entityIndex) |
| | | 308 | | { |
| | 3 | 309 | | return (uint)entityIndex < (uint)_entityCapacity ? _entityToDenseIndex[entityIndex] : -1; |
| | | 310 | | } |
| | | 311 | | |
| | | 312 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 313 | | private void RemoveDenseIndex(int entityIndex, int denseIndex) |
| | | 314 | | { |
| | 1 | 315 | | if (!_group.RemoveAtLinearIndex(denseIndex, out EntityRef movedEntity)) |
| | | 316 | | { |
| | 0 | 317 | | return; |
| | | 318 | | } |
| | | 319 | | |
| | 1 | 320 | | _entityToDenseIndex[entityIndex] = -1; |
| | 1 | 321 | | if (movedEntity != EntityRef.None) |
| | | 322 | | { |
| | 0 | 323 | | _entityToDenseIndex[movedEntity.Index] = denseIndex; |
| | | 324 | | } |
| | 1 | 325 | | } |
| | | 326 | | |
| | | 327 | | private void ResetDenseIndexMap() |
| | | 328 | | { |
| | 36 | 329 | | for (int i = 0; i < _entityCapacity; i++) |
| | | 330 | | { |
| | 16 | 331 | | _entityToDenseIndex[i] = -1; |
| | | 332 | | } |
| | 2 | 333 | | } |
| | | 334 | | } |
| | | 335 | | } |