| | | 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 System.Threading; |
| | | 7 | | using Lattice.Core; |
| | | 8 | | |
| | | 9 | | namespace Lattice.ECS.Core.Parallel |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// 并行查询兼容层。 |
| | | 13 | | /// 为既有并行遍历调用方保留外形,内部按致密索引顺序只读扫描组件。 |
| | | 14 | | /// |
| | | 15 | | /// 使用方式: |
| | | 16 | | /// 1. 将迭代范围分割给多个线程 |
| | | 17 | | /// 2. 每个线程访问不同的 Block,无竞争 |
| | | 18 | | /// 3. 主线程等待所有工作完成 |
| | | 19 | | /// |
| | | 20 | | /// 注意:只支持只读操作!写入需要额外同步。 |
| | | 21 | | /// </summary> |
| | | 22 | | public unsafe class ParallelFilter<T> where T : unmanaged, IComponent |
| | | 23 | | { |
| | | 24 | | private readonly Storage<T>* _storage; |
| | | 25 | | private readonly JobSystem* _jobSystem; |
| | | 26 | | |
| | | 27 | | public ParallelFilter(Storage<T>* storage, JobSystem* jobSystem) |
| | | 28 | | { |
| | | 29 | | _storage = storage; |
| | | 30 | | _jobSystem = jobSystem; |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | /// <summary> |
| | | 34 | | /// 并行遍历所有组件(只读) |
| | | 35 | | /// </summary> |
| | | 36 | | public void ForEach(ParallelForDelegate<T> action) |
| | | 37 | | { |
| | | 38 | | if (_storage == null || _storage->Count == 0 || action == null) return; |
| | | 39 | | |
| | | 40 | | int totalItems = _storage->Count; |
| | | 41 | | |
| | | 42 | | // 创建上下文 |
| | | 43 | | var context = new ForEachContext<T> |
| | | 44 | | { |
| | | 45 | | Storage = _storage, |
| | | 46 | | Action = action |
| | | 47 | | }; |
| | | 48 | | |
| | | 49 | | // 调度并行任务 |
| | | 50 | | JobDelegate jobDelegate = (void* userData, int startIndex, int endIndex) => |
| | | 51 | | { |
| | | 52 | | var ctx = (ForEachContext<T>*)userData; |
| | | 53 | | var storage = ctx->Storage; |
| | | 54 | | |
| | | 55 | | for (int i = startIndex; i < endIndex; i++) |
| | | 56 | | { |
| | | 57 | | storage->GetDenseEntryByLinearIndex(i, out EntityRef entity, out T* component); |
| | | 58 | | ctx->Action(entity, component); |
| | | 59 | | } |
| | | 60 | | }; |
| | | 61 | | |
| | | 62 | | var config = JobConfig.Create(jobDelegate, &context, totalItems, 64); |
| | | 63 | | var handle = _jobSystem->ScheduleParallel(config); |
| | | 64 | | _jobSystem->WaitForComplete(handle); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | private unsafe struct ForEachContext<T1> where T1 : unmanaged, IComponent |
| | | 68 | | { |
| | | 69 | | public Storage<T1>* Storage; |
| | | 70 | | public ParallelForDelegate<T1> Action; |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// 并行 For 委托 |
| | | 76 | | /// </summary> |
| | | 77 | | public unsafe delegate void ParallelForDelegate<T>(EntityRef entity, T* component) where T : unmanaged, IComponent; |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// 线程安全的只读 Frame 访问视图。 |
| | | 81 | | /// 新代码可直接在该视图上使用 Query API 构建只读查询。 |
| | | 82 | | /// </summary> |
| | | 83 | | public unsafe readonly struct FrameReadOnly |
| | | 84 | | { |
| | | 85 | | private readonly Frame _frame; |
| | | 86 | | |
| | | 87 | | public FrameReadOnly(Frame frame) |
| | | 88 | | { |
| | | 89 | | _frame = frame ?? throw new ArgumentNullException(nameof(frame)); |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 93 | | public bool IsValid(EntityRef entity) |
| | | 94 | | { |
| | | 95 | | return _frame.IsValid(entity); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 99 | | public bool Has<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 100 | | { |
| | | 101 | | return _frame.Has<T>(entity); |
| | | 102 | | } |
| | | 103 | | |
| | | 104 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 105 | | public T Get<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 106 | | { |
| | | 107 | | return _frame.Get<T>(entity); |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 111 | | public T* GetPointer<T>(EntityRef entity) where T : unmanaged, IComponent |
| | | 112 | | { |
| | | 113 | | return _frame.GetPointer<T>(entity); |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 117 | | public bool TryGet<T>(EntityRef entity, out T component) where T : unmanaged, IComponent |
| | | 118 | | { |
| | | 119 | | return _frame.TryGet<T>(entity, out component); |
| | | 120 | | } |
| | | 121 | | |
| | | 122 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 123 | | public bool HasGlobal<T>() where T : unmanaged, IComponent |
| | | 124 | | { |
| | | 125 | | return _frame.HasGlobal<T>(); |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 129 | | public T GetGlobal<T>() where T : unmanaged, IComponent |
| | | 130 | | { |
| | | 131 | | return _frame.GetGlobal<T>(); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 135 | | public bool TryGetGlobal<T>(out T component) where T : unmanaged, IComponent |
| | | 136 | | { |
| | | 137 | | return _frame.TryGetGlobal(out component); |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 141 | | public EntityRef GetGlobalEntity<T>() where T : unmanaged, IComponent |
| | | 142 | | { |
| | | 143 | | return _frame.GetGlobalEntity<T>(); |
| | | 144 | | } |
| | | 145 | | |
| | | 146 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 147 | | public bool TryGetGlobalEntity<T>(out EntityRef entity) where T : unmanaged, IComponent |
| | | 148 | | { |
| | | 149 | | return _frame.TryGetGlobalEntity<T>(out entity); |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 153 | | public Query<T> Query<T>() where T : unmanaged, IComponent |
| | | 154 | | { |
| | | 155 | | return _frame.Query<T>(); |
| | | 156 | | } |
| | | 157 | | |
| | | 158 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 159 | | public Query<T1, T2> Query<T1, T2>() |
| | | 160 | | where T1 : unmanaged, IComponent |
| | | 161 | | where T2 : unmanaged, IComponent |
| | | 162 | | { |
| | | 163 | | return _frame.Query<T1, T2>(); |
| | | 164 | | } |
| | | 165 | | |
| | | 166 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 167 | | public Query<T1, T2, T3> Query<T1, T2, T3>() |
| | | 168 | | where T1 : unmanaged, IComponent |
| | | 169 | | where T2 : unmanaged, IComponent |
| | | 170 | | where T3 : unmanaged, IComponent |
| | | 171 | | { |
| | | 172 | | return _frame.Query<T1, T2, T3>(); |
| | | 173 | | } |
| | | 174 | | |
| | | 175 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 176 | | public Query<T1, T2, T3, T4> Query<T1, T2, T3, T4>() |
| | | 177 | | where T1 : unmanaged, IComponent |
| | | 178 | | where T2 : unmanaged, IComponent |
| | | 179 | | where T3 : unmanaged, IComponent |
| | | 180 | | where T4 : unmanaged, IComponent |
| | | 181 | | { |
| | | 182 | | return _frame.Query<T1, T2, T3, T4>(); |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 186 | | public ComponentBlockIterator<T> GetComponentBlockIterator<T>() where T : unmanaged, IComponent |
| | | 187 | | { |
| | | 188 | | return _frame.GetComponentBlockIterator<T>(); |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | /// <summary> |
| | | 193 | | /// 原子计数器 - 多线程统计 |
| | | 194 | | /// </summary> |
| | | 195 | | public unsafe struct AtomicCounter |
| | | 196 | | { |
| | | 197 | | private long _value; |
| | | 198 | | |
| | 0 | 199 | | public long Value => Interlocked.Read(ref _value); |
| | | 200 | | |
| | | 201 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 202 | | public long Increment() |
| | | 203 | | { |
| | 0 | 204 | | return Interlocked.Increment(ref _value); |
| | | 205 | | } |
| | | 206 | | |
| | | 207 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 208 | | public long Add(long amount) |
| | | 209 | | { |
| | 0 | 210 | | return Interlocked.Add(ref _value, amount); |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 214 | | public void Reset() |
| | | 215 | | { |
| | 0 | 216 | | Interlocked.Exchange(ref _value, 0); |
| | 0 | 217 | | } |
| | | 218 | | } |
| | | 219 | | |
| | | 220 | | /// <summary> |
| | | 221 | | /// 原子标志数组 - 多线程标记(如裁剪系统) |
| | | 222 | | /// </summary> |
| | | 223 | | public unsafe struct AtomicFlagArray |
| | | 224 | | { |
| | | 225 | | private long* _bits; |
| | | 226 | | private int _capacity; |
| | | 227 | | |
| | | 228 | | public void Initialize(byte* buffer, int capacity) |
| | | 229 | | { |
| | | 230 | | _capacity = (capacity + 63) & ~63; // 对齐到 64 |
| | | 231 | | _bits = (long*)buffer; |
| | | 232 | | Clear(); |
| | | 233 | | } |
| | | 234 | | |
| | | 235 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 236 | | public void Set(int index) |
| | | 237 | | { |
| | | 238 | | int block = index >> 6; |
| | | 239 | | int bit = index & 0x3F; |
| | | 240 | | long mask = 1L << bit; |
| | | 241 | | Interlocked.Or(ref _bits[block], mask); |
| | | 242 | | } |
| | | 243 | | |
| | | 244 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 245 | | public void Clear(int index) |
| | | 246 | | { |
| | | 247 | | int block = index >> 6; |
| | | 248 | | int bit = index & 0x3F; |
| | | 249 | | long mask = ~(1L << bit); |
| | | 250 | | Interlocked.And(ref _bits[block], mask); |
| | | 251 | | } |
| | | 252 | | |
| | | 253 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 254 | | public bool IsSet(int index) |
| | | 255 | | { |
| | | 256 | | int block = index >> 6; |
| | | 257 | | int bit = index & 0x3F; |
| | | 258 | | return (_bits[block] & (1L << bit)) != 0; |
| | | 259 | | } |
| | | 260 | | |
| | | 261 | | public void Clear() |
| | | 262 | | { |
| | | 263 | | int blockCount = _capacity >> 6; |
| | | 264 | | for (int i = 0; i < blockCount; i++) |
| | | 265 | | _bits[i] = 0; |
| | | 266 | | } |
| | | 267 | | |
| | | 268 | | public static int CalculateBufferSize(int capacity) |
| | | 269 | | { |
| | | 270 | | return sizeof(long) * ((capacity + 63) & ~63) / 64; |
| | | 271 | | } |
| | | 272 | | } |
| | | 273 | | } |