add TerrainTaintsArray, a specialized BitArray for terrain taints. Start using it

This commit is contained in:
UbitUmarov
2022-03-29 22:18:28 +01:00
parent c11827e1e2
commit 3f30f17187
3 changed files with 388 additions and 127 deletions

View File

@@ -74,9 +74,12 @@ namespace OpenSim.Framework
private float[,] m_heightmap;
// Remember subregions of the heightmap that has changed.
private BitArray m_taint;
private TerrainTaintsArray m_taints;
private readonly int m_taintSizeX;
private readonly int m_taintSizeY;
private readonly int m_mapStride;
private readonly int m_mapPatchsStride;
// legacy CompressionFactor
public float CompressionFactor { get; private set; }
@@ -107,7 +110,7 @@ namespace OpenSim.Framework
{
m_heightmap[x, y] = value;
int yy = y / Constants.TerrainPatchSize;
m_taint[x / Constants.TerrainPatchSize + yy * m_taintSizeX] = true;
m_taints.Set(x / Constants.TerrainPatchSize + yy * m_taintSizeX, true);
}
}
}
@@ -118,19 +121,24 @@ namespace OpenSim.Framework
set { this[x, y] = value; }
}
public TerrainTaintsArray GetTaints()
{
return m_taints;
}
public void ClearTaint()
{
m_taint.SetAll(false);
m_taints.SetAll(false);
}
public void TaintAllTerrain()
{
m_taint.SetAll(true);
m_taints.SetAll(true);
}
private void SetAllTaint(bool setting)
{
m_taint.SetAll(setting);
m_taints.SetAll(setting);
}
public void ClearLand()
@@ -145,6 +153,12 @@ namespace OpenSim.Framework
m_heightmap[xx, yy] = pHeight;
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTainted()
{
return m_taints.IsTaited();
}
// Return 'true' of the patch that contains these region coordinates has been modified.
// Note that checking the taint clears it.
// There is existing code that relies on this feature.
@@ -153,59 +167,51 @@ namespace OpenSim.Framework
{
yy /= Constants.TerrainPatchSize;
int indx = xx / Constants.TerrainPatchSize + yy * m_taintSizeX;
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAt(int xx, int yy)
{
yy /= Constants.TerrainPatchSize;
return m_taint[xx / Constants.TerrainPatchSize + yy * m_taintSizeX];
return m_taints.Get(xx / Constants.TerrainPatchSize + yy * m_taintSizeX);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int xx, int yy, bool clearOnTest)
{
int indx = xx + yy * m_taintSizeX;
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int xx, int yy)
{
return m_taint[xx + yy * m_taintSizeX];
return m_taints.Get(xx + yy * m_taintSizeX);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int indx, bool clearOnTest)
{
bool ret = m_taint[indx];
if (ret && clearOnTest)
m_taint[indx] = false;
return ret;
return m_taints.Get(indx, clearOnTest);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatchWithClear(int indx)
{
if(m_taint[indx])
{
m_taint[indx] = false;
return true;
}
return false;
return m_taints.GetAndClear(indx);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public bool IsTaintedAtPatch(int indx)
{
return m_taint[indx];
return m_taints.Get(indx);
}
[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
public int GetAndClearNextTaint(int startIndex)
{
return m_taints.GetAndClearNextTrue(startIndex);
}
// TerrainData.GetDatabaseBlob
// The user wants something to store in the database.
@@ -233,6 +239,7 @@ namespace OpenSim.Framework
{
TerrainData ret = new TerrainData(SizeX, SizeY, SizeZ);
ret.m_heightmap = (float[,])this.m_heightmap.Clone();
return ret;
}
@@ -266,14 +273,12 @@ namespace OpenSim.Framework
zmax = float.MinValue;
zmin = float.MaxValue;
int stride = m_heightmap.GetLength(1);
int mstride = 16 * stride;
int mpy = 16 * py;
int mpy = Constants.TerrainPatchSize * py;
fixed (float* map = m_heightmap)
{
float* p = map + px * mstride;
float* pend = p + mstride;
float* p = map + px * m_mapPatchsStride;
float* pend = p + m_mapPatchsStride;
while (p < pend)
{
float* yt = p + mpy;
@@ -287,7 +292,7 @@ namespace OpenSim.Framework
zmin = val;
yt++;
}
p += stride;
p += m_mapStride;
}
}
}
@@ -295,12 +300,9 @@ namespace OpenSim.Framework
public unsafe void GetPatchBlock(float* block, int px, int py, float sub, float premult)
{
int k = 0;
int stride = m_heightmap.GetLength(1);
int mstride = 16 * stride;
int startX = px * mstride;
int endX = startX + mstride;
int mpy = 16 * py;
int startX = px * m_mapPatchsStride;
int endX = startX + m_mapPatchsStride;
int mpy = py * Constants.TerrainPatchSize;
fixed (float* map = m_heightmap)
{
float* yp = map + mpy;
@@ -308,7 +310,7 @@ namespace OpenSim.Framework
while (yp < yend)
{
for (int x = startX; x < endX; x += stride)
for (int x = startX; x < endX; x += m_mapStride)
{
block[k++] = (yp[x] - sub) * premult;
}
@@ -362,9 +364,13 @@ namespace OpenSim.Framework
m_taintSizeX = SizeX / Constants.TerrainPatchSize;
m_taintSizeY = SizeY / Constants.TerrainPatchSize;
m_mapStride = SizeY;
m_mapPatchsStride = m_mapStride * Constants.TerrainPatchSize;
SizeZ = (int)Constants.RegionHeight;
CompressionFactor = 100.0f;
m_heightmap = new float[SizeX, SizeY];
for (int ii = 0; ii < SizeX; ii++)
{
@@ -376,7 +382,7 @@ namespace OpenSim.Framework
}
// m_log.DebugFormat("{0} new by doubles. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
m_taint = new BitArray(m_taintSizeX * m_taintSizeY, false);
m_taints = new TerrainTaintsArray(m_taintSizeX * m_taintSizeY);
}
// Create underlying structures but don't initialize the heightmap assuming the caller will immediately do that
@@ -387,9 +393,12 @@ namespace OpenSim.Framework
SizeZ = pZ;
m_taintSizeX = SizeX / Constants.TerrainPatchSize;
m_taintSizeY = SizeY / Constants.TerrainPatchSize;
m_mapStride = SizeY;
m_mapPatchsStride = m_mapStride * Constants.TerrainPatchSize;
CompressionFactor = 100.0f;
m_heightmap = new float[SizeX, SizeY];
m_taint = new BitArray(m_taintSizeX * m_taintSizeY, false);
m_taints = new TerrainTaintsArray(m_taintSizeX * m_taintSizeY);
// m_log.DebugFormat("{0} new by dimensions. sizeX={1}, sizeY={2}, sizeZ={3}", LogHeader, SizeX, SizeY, SizeZ);
ClearLand(0f);

View File

@@ -0,0 +1,312 @@
/*
* Copyright (c) Contributors, http://opensimulator.org/
* See CONTRIBUTORS.TXT for a full list of copyright holders.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of the OpenSimulator Project nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
using System.Runtime.CompilerServices;
using System.Threading;
namespace OpenSim.Framework
{
public class TerrainTaintsArray
{
public const int VectorNumberBits = 32;
public const int VectorNumberBitsLog2 = 5;
public const int FALSEWORD = 0;
public const int TRUEWORD = ~FALSEWORD;
private int[] m_data;
private readonly int m_nbits;
private volatile int m_ntainted;
private object m_mainlock = new object();
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public TerrainTaintsArray(int lenght) : this(lenght, false) { }
public TerrainTaintsArray(int lenght, bool preset)
{
m_nbits = lenght;
int nInts = calclen(m_nbits);
m_data = new int[nInts];
if (preset)
{
for (int i = 0; i < m_data.Length; i++)
m_data[i] = TRUEWORD;
m_ntainted = m_nbits;
}
else
m_ntainted = 0;
}
public int Length
{
get
{
return m_nbits;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int TaitedCount()
{
return m_ntainted;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsTaited()
{
return m_ntainted > 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Get(int bitindex)
{
int indexh = bitindex >> VectorNumberBitsLog2;
int mask = 1 << (bitindex & (VectorNumberBits - 1));
return (m_data[indexh] & mask) != 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Get(int bitindex, bool clear)
{
int indexh = bitindex >> VectorNumberBitsLog2;
int mask = 1 << (bitindex & (VectorNumberBits - 1));
lock (m_mainlock)
{
if ((m_data[indexh] & mask) != 0)
{
if (clear)
{
m_data[indexh] ^= mask;
--m_ntainted;
}
return true;
}
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public unsafe bool GetAndClear(int bitindex)
{
int indexh = bitindex >> VectorNumberBitsLog2;
int mask = 1 << (bitindex & (VectorNumberBits - 1));
lock (m_mainlock)
{
if ((m_data[indexh] & mask) != 0)
{
m_data[indexh] ^= mask;
--m_ntainted;
return true;
}
}
return false;
}
public void Set(int bitindex, bool val)
{
int indexh = bitindex >> VectorNumberBitsLog2;
int mask = 1 << (bitindex & (VectorNumberBits - 1));
lock (m_mainlock)
{
if (val)
{
if ((m_data[indexh] & mask) == 0)
{
m_data[indexh] |= mask;
++m_ntainted;
}
}
else
{
if ((m_data[indexh] & mask) != 0)
{
m_data[indexh] ^= mask;
--m_ntainted;
}
}
}
}
public bool this[int bitindex]
{
get
{
return Get(bitindex);
}
set
{
int indexh = bitindex >> VectorNumberBitsLog2;
int mask = 1 << (bitindex & (VectorNumberBits - 1));
lock (m_mainlock)
{
if (value)
{
if ((m_data[indexh] & mask) == 0)
{
m_data[indexh] |= mask;
++m_ntainted;
}
}
else
{
if ((m_data[indexh] & mask) != 0)
{
m_data[indexh] ^= mask;
--m_ntainted;
}
}
}
}
}
public void SetAll(bool val)
{
lock (m_mainlock)
{
if (val)
{
for (int i = 0; i < m_data.Length; ++i)
m_data[i] = TRUEWORD;
m_ntainted = m_nbits;
}
else
{
for (int i = 0; i < m_data.Length; ++i)
m_data[i] = 0;
m_ntainted = 0;
}
}
}
public bool IsVectorOfFalse(int vectorIndex)
{
return m_data[vectorIndex] == 0;
}
public bool IsVectorOfBitFalse(int bitindex)
{
return m_data[(bitindex >> VectorNumberBitsLog2)] == 0;
}
public bool IsVectorTrue(int vectorIndex)
{
return m_data[vectorIndex] == unchecked(((int)0xffffffff));
}
public bool IsVectorOfBitTrue(int bitindex)
{
return m_data[(bitindex >> VectorNumberBitsLog2)] == unchecked(((int)0xffffffff));
}
public void Or(TerrainTaintsArray other)
{
if (m_nbits != other.m_nbits)
return;
lock (m_mainlock)
{
lock (other.m_mainlock)
{
for (int i = 0; i < m_data.Length; ++i)
{
int tr = other.m_data[i];
if (tr == 0)
continue;
int tt = m_data[i] | tr;
tr ^= tt;
if (tr != 0)
{
m_data[i] = tt;
for (int j = 1; j != 0; j <<= 1)
{
if ((tr & j) != 0)
++m_ntainted;
}
}
}
}
}
}
public int GetNextTrue(int startBitIndex)
{
if (startBitIndex < 0 || startBitIndex >= m_nbits)
return -1;
int indexh = startBitIndex >> VectorNumberBitsLog2;
for (; indexh < m_data.Length; ++indexh)
{
int cur = m_data[indexh];
if (cur == 0)
continue;
for (int j = 0; j < VectorNumberBits; ++j)
{
if ((cur & (1 << j)) != 0)
return (indexh << VectorNumberBitsLog2) | j;
}
}
return -1;
}
public int GetAndClearNextTrue(int startBitIndex)
{
if (m_ntainted <= 0 || startBitIndex < 0 || startBitIndex >= m_nbits)
return -1;
int indexh = startBitIndex >> VectorNumberBitsLog2;
lock (m_mainlock)
{
for (; indexh < m_data.Length; ++indexh)
{
int cur = m_data[indexh];
if (cur == 0)
continue;
for (int j = 0; j < VectorNumberBits; ++j)
{
int mask = (1 << j);
if ((cur & mask) != 0)
{
m_data[indexh] ^= mask;
--m_ntainted;
return (indexh << VectorNumberBitsLog2) | j;
}
}
}
}
return -1;
}
private int calclen(int bitsLen)
{
return bitsLen > 0 ? ((bitsLen - 1) >> VectorNumberBitsLog2) + 1 : 0;
}
}
}