< Summary

Information
Class: Lattice.ECS.Framework.CullingSystem
Assembly: Lattice
File(s): /home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/CullingSystem.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 33
Coverable lines: 33
Total lines: 136
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 6
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
get_IsInitialized()100%210%
Initialize(...)100%210%
Clear()0%620%
MarkCulled(...)0%620%
MarkCulledBatch(...)100%210%
IsCulled(...)100%210%
UnmarkCulled(...)0%620%
GetCulledBlock(...)100%210%
CalculateBufferSize(...)100%210%

File(s)

/home/runner/work/Sideline/Sideline/godot/scripts/lattice/ECS/Framework/CullingSystem.cs

#LineLine coverage
 1// Copyright (c) 2026 Sideline Authors. All rights reserved.
 2// Licensed under GPL-3.0.
 3
 4using System;
 5using System.Runtime.CompilerServices;
 6using System.Threading;
 7
 8namespace 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>
 026        public bool IsInitialized => _culled != null;
 27
 28        /// <summary>
 29        /// 初始化裁剪系统
 30        /// </summary>
 31        public void Initialize(byte* buffer, int maxEntities)
 32        {
 33            // 容量对齐到 64
 034            _capacity = (maxEntities + 63) & ~63;
 035            _culled = (long*)buffer;
 36
 37            // 初始化为全部未裁剪
 038            Clear();
 039        }
 40
 41        /// <summary>
 42        /// 清空所有裁剪标记
 43        /// </summary>
 44        public void Clear()
 45        {
 046            int blockCount = _capacity / 64;
 047            for (int i = 0; i < blockCount; i++)
 48            {
 049                _culled[i] = 0;
 50            }
 051        }
 52
 53        /// <summary>
 54        /// 标记实体为裁剪(线程安全)
 55        /// </summary>
 56        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 57        public void MarkCulled(int entityIndex)
 58        {
 059            int block = entityIndex >> 6;      // / 64
 060            int bit = entityIndex & 0x3F;      // % 64
 61
 062            long mask = 1L << bit;
 63            long current;
 64            long newValue;
 65
 66            // CAS 循环确保线程安全
 67            do
 68            {
 069                current = _culled[block];
 070                newValue = current | mask;
 71            }
 072            while (Interlocked.CompareExchange(ref _culled[block], newValue, current) != current);
 073        }
 74
 75        /// <summary>
 76        /// 批量标记裁剪(非线程安全,用于单线程裁剪计算)
 77        /// </summary>
 78        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 79        public void MarkCulledBatch(int entityIndex)
 80        {
 081            int block = entityIndex >> 6;
 082            int bit = entityIndex & 0x3F;
 083            _culled[block] |= 1L << bit;
 084        }
 85
 86        /// <summary>
 87        /// 检查实体是否被裁剪
 88        /// </summary>
 89        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 90        public bool IsCulled(int entityIndex)
 91        {
 092            int block = entityIndex >> 6;
 093            int bit = entityIndex & 0x3F;
 094            return (_culled[block] & (1L << bit)) != 0;
 95        }
 96
 97        /// <summary>
 98        /// 取消裁剪标记
 99        /// </summary>
 100        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 101        public void UnmarkCulled(int entityIndex)
 102        {
 0103            int block = entityIndex >> 6;
 0104            int bit = entityIndex & 0x3F;
 105
 0106            long mask = ~(1L << bit);
 107            long current;
 108            long newValue;
 109
 110            do
 111            {
 0112                current = _culled[block];
 0113                newValue = current & mask;
 114            }
 0115            while (Interlocked.CompareExchange(ref _culled[block], newValue, current) != current);
 0116        }
 117
 118        /// <summary>
 119        /// 获取指定 Block 的裁剪位图(用于批量检查)
 120        /// </summary>
 121        [MethodImpl(MethodImplOptions.AggressiveInlining)]
 122        public long GetCulledBlock(int blockIndex)
 123        {
 0124            return _culled[blockIndex];
 125        }
 126
 127        /// <summary>
 128        /// 计算所需缓冲区大小
 129        /// </summary>
 130        public static int CalculateBufferSize(int maxEntities)
 131        {
 0132            int capacity = (maxEntities + 63) & ~63;
 0133            return sizeof(long) * (capacity / 64);
 134        }
 135    }
 136}