| | | 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 | | |
| | | 8 | | namespace Lattice.ECS.Framework |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// 裁剪系统 - 高性能空间裁剪 |
| | | 12 | | /// |
| | | 13 | | /// 设计目标: |
| | | 14 | | /// 1. 多线程安全(原子操作) |
| | | 15 | | /// 2. 64 实体一组(一个 long),位操作高效 |
| | | 16 | | /// 3. 与 Filter 集成,自动跳过裁剪实体 |
| | | 17 | | /// </summary> |
| | | 18 | | public unsafe struct CullingSystem |
| | | 19 | | { |
| | | 20 | | // 裁剪状态位图(每 bit 代表一个实体) |
| | | 21 | | // 1 = 被裁剪(不可见),0 = 未裁剪(可见) |
| | | 22 | | private long* _culled; |
| | | 23 | | private int _capacity; // 实体容量(64的倍数) |
| | | 24 | | |
| | | 25 | | /// <summary>是否已初始化</summary> |
| | 0 | 26 | | public bool IsInitialized => _culled != null; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// 初始化裁剪系统 |
| | | 30 | | /// </summary> |
| | | 31 | | public void Initialize(byte* buffer, int maxEntities) |
| | | 32 | | { |
| | | 33 | | // 容量对齐到 64 |
| | 0 | 34 | | _capacity = (maxEntities + 63) & ~63; |
| | 0 | 35 | | _culled = (long*)buffer; |
| | | 36 | | |
| | | 37 | | // 初始化为全部未裁剪 |
| | 0 | 38 | | Clear(); |
| | 0 | 39 | | } |
| | | 40 | | |
| | | 41 | | /// <summary> |
| | | 42 | | /// 清空所有裁剪标记 |
| | | 43 | | /// </summary> |
| | | 44 | | public void Clear() |
| | | 45 | | { |
| | 0 | 46 | | int blockCount = _capacity / 64; |
| | 0 | 47 | | for (int i = 0; i < blockCount; i++) |
| | | 48 | | { |
| | 0 | 49 | | _culled[i] = 0; |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// 标记实体为裁剪(线程安全) |
| | | 55 | | /// </summary> |
| | | 56 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 57 | | public void MarkCulled(int entityIndex) |
| | | 58 | | { |
| | 0 | 59 | | int block = entityIndex >> 6; // / 64 |
| | 0 | 60 | | int bit = entityIndex & 0x3F; // % 64 |
| | | 61 | | |
| | 0 | 62 | | long mask = 1L << bit; |
| | | 63 | | long current; |
| | | 64 | | long newValue; |
| | | 65 | | |
| | | 66 | | // CAS 循环确保线程安全 |
| | | 67 | | do |
| | | 68 | | { |
| | 0 | 69 | | current = _culled[block]; |
| | 0 | 70 | | newValue = current | mask; |
| | | 71 | | } |
| | 0 | 72 | | while (Interlocked.CompareExchange(ref _culled[block], newValue, current) != current); |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <summary> |
| | | 76 | | /// 批量标记裁剪(非线程安全,用于单线程裁剪计算) |
| | | 77 | | /// </summary> |
| | | 78 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 79 | | public void MarkCulledBatch(int entityIndex) |
| | | 80 | | { |
| | 0 | 81 | | int block = entityIndex >> 6; |
| | 0 | 82 | | int bit = entityIndex & 0x3F; |
| | 0 | 83 | | _culled[block] |= 1L << bit; |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// 检查实体是否被裁剪 |
| | | 88 | | /// </summary> |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 90 | | public bool IsCulled(int entityIndex) |
| | | 91 | | { |
| | 0 | 92 | | int block = entityIndex >> 6; |
| | 0 | 93 | | int bit = entityIndex & 0x3F; |
| | 0 | 94 | | return (_culled[block] & (1L << bit)) != 0; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// 取消裁剪标记 |
| | | 99 | | /// </summary> |
| | | 100 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 101 | | public void UnmarkCulled(int entityIndex) |
| | | 102 | | { |
| | 0 | 103 | | int block = entityIndex >> 6; |
| | 0 | 104 | | int bit = entityIndex & 0x3F; |
| | | 105 | | |
| | 0 | 106 | | long mask = ~(1L << bit); |
| | | 107 | | long current; |
| | | 108 | | long newValue; |
| | | 109 | | |
| | | 110 | | do |
| | | 111 | | { |
| | 0 | 112 | | current = _culled[block]; |
| | 0 | 113 | | newValue = current & mask; |
| | | 114 | | } |
| | 0 | 115 | | while (Interlocked.CompareExchange(ref _culled[block], newValue, current) != current); |
| | 0 | 116 | | } |
| | | 117 | | |
| | | 118 | | /// <summary> |
| | | 119 | | /// 获取指定 Block 的裁剪位图(用于批量检查) |
| | | 120 | | /// </summary> |
| | | 121 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 122 | | public long GetCulledBlock(int blockIndex) |
| | | 123 | | { |
| | 0 | 124 | | return _culled[blockIndex]; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// 计算所需缓冲区大小 |
| | | 129 | | /// </summary> |
| | | 130 | | public static int CalculateBufferSize(int maxEntities) |
| | | 131 | | { |
| | 0 | 132 | | int capacity = (maxEntities + 63) & ~63; |
| | 0 | 133 | | return sizeof(long) * (capacity / 64); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | } |