Skip to content

Commit 565c80f

Browse files
committed
Speed up reading vis data
1 parent 899b9d1 commit 565c80f

1 file changed

Lines changed: 26 additions & 25 deletions

File tree

SourceUtils/ValveBsp/VisibilityLump.cs

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public int NumClusters
3737

3838
private readonly ValveBspFile _bspFile;
3939
private ByteOffset[] _offsets;
40-
private HashSet<int>[] _vpsList;
40+
private HashSet<int>[] _pvsList;
4141

4242
public VisibilityLump( ValveBspFile bspFile, LumpType type )
4343
{
@@ -50,40 +50,36 @@ public HashSet<int> this[ int clusterIndex ]
5050
get
5151
{
5252
EnsureLoaded();
53-
var set = _vpsList[clusterIndex];
54-
return set ?? (_vpsList[clusterIndex] = ReadSet( _offsets[clusterIndex].Pvs ));
53+
return _pvsList[clusterIndex];
5554
}
5655
}
5756

58-
private HashSet<int> ReadSet( int byteOffset )
57+
private HashSet<int> ReadSet( Stream stream, int byteOffset )
5958
{
60-
using ( var stream = _bspFile.GetLumpStream( LumpType ) )
61-
{
62-
stream.Seek( byteOffset, SeekOrigin.Begin );
59+
stream.Seek( byteOffset, SeekOrigin.Begin );
6360

64-
var set = new HashSet<int>();
61+
var set = new HashSet<int>();
6562

66-
var clusters = NumClusters;
67-
var offset = 0;
68-
while ( offset < clusters )
63+
var clusters = NumClusters;
64+
var offset = 0;
65+
while ( offset < clusters )
66+
{
67+
var bits = stream.ReadByte();
68+
if ( bits == 0 )
6969
{
70-
var bits = stream.ReadByte();
71-
if ( bits == 0 )
72-
{
73-
offset += stream.ReadByte() * 8;
74-
continue;
75-
}
76-
77-
for ( var i = 0; i < 8 && offset + i < clusters; ++i )
78-
{
79-
if ( (bits & (1 << i)) != 0 ) set.Add( offset + i );
80-
}
70+
offset += stream.ReadByte() * 8;
71+
continue;
72+
}
8173

82-
offset += 8;
74+
for ( var i = 0; i < 8 && offset + i < clusters; ++i )
75+
{
76+
if ( (bits & (1 << i)) != 0 ) set.Add( offset + i );
8377
}
8478

85-
return set;
79+
offset += 8;
8680
}
81+
82+
return set;
8783
}
8884

8985
private void EnsureLoaded()
@@ -101,8 +97,13 @@ private void EnsureLoaded()
10197
}
10298

10399
_numClusters = reader.ReadInt32();
104-
_vpsList = new HashSet<int>[_numClusters];
100+
_pvsList = new HashSet<int>[_numClusters];
105101
_offsets = LumpReader<ByteOffset>.ReadLumpFromStream( reader.BaseStream, _numClusters );
102+
103+
for ( var index = 0; index < _pvsList.Length; ++index )
104+
{
105+
_pvsList[index] = ReadSet( reader.BaseStream, _offsets[index].Pvs );
106+
}
106107
}
107108
}
108109
}

0 commit comments

Comments
 (0)