Probably for TU30

This commit is contained in:
GabsPuNs
2026-06-05 16:06:08 -04:00
parent 22a1aa3a74
commit 86b1da99ae
2 changed files with 108 additions and 236 deletions

View File

@@ -91,14 +91,14 @@ DWORD Level::tlsIdxLightCache = TlsAlloc();
void Level::enableLightingCache()
{
// Allocate 16K (needs 32K for large worlds) for a 16x16x16x4 BYTE cache of results, plus 128K required for toCheck array. Rounding up to 256 to keep as multiple of alignement - aligning to 128K boundary for possible cache locking.
void *cache = static_cast<unsigned char *>(XPhysicalAlloc(256 * 1024, MAXULONG_PTR, 128 * 1024, PAGE_READWRITE | MEM_LARGE_PAGES));
void *cache = _aligned_malloc(256 * 1024, 128 * 1024);
TlsSetValue(tlsIdxLightCache,cache);
}
void Level::destroyLightingCache()
{
lightCache_t *cache = static_cast<lightCache_t *>(TlsGetValue(tlsIdxLightCache));
XPhysicalFree(cache);
_aligned_free(cache);
}
inline int GetIndex(int x, int y, int z)
@@ -588,7 +588,7 @@ void Level::_init()
villageSiege = new VillageSiege(this);
scoreboard = new Scoreboard();
toCheckLevel = new int[ 32 * 32 * 32]; // 4J - brought forward from 1.8.2
toCheckLevel = new int[32 * 32 * 32]; // - 4J - brought forward from 1.8.2
InitializeCriticalSectionAndSpinCount(&m_checkLightCS, 5120); // 4J - added for 1.8.2 lighting
// 4J Added
@@ -3444,71 +3444,47 @@ void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc, bool f
// 4J - special mode added so we can do lava lighting updates without having all neighbouring chunks loaded in
if (!hasChunksAt(xc, yc, zc, 0)) return;
}
else
{
// 4J - this is normal java behaviour
if (!hasChunksAt(xc, yc, zc, 17)) return;
}
#if 0
/////////////////////////////////////////////////////////////////////////////////////////////
// Get the frequency of the timer
LARGE_INTEGER qwTicksPerSec, qwTime, qwNewTime, qwDeltaTime1, qwDeltaTime2;
float fElapsedTime1 = 0.0f;
float fElapsedTime2 = 0.0f;
QueryPerformanceFrequency( &qwTicksPerSec );
float fSecsPerTick = 1.0f / (float)qwTicksPerSec.QuadPart;
QueryPerformanceCounter( &qwTime );
/////////////////////////////////////////////////////////////////////////////////////////////
#endif
EnterCriticalSection(&m_checkLightCS);
initCachePartial(cache, xc, yc, zc);
// If we're in cached mode, then use memory allocated after the cached data itself for the toCheck array, in an attempt to make both that & the other cached data sit on the CPU L2 cache better.
int *toCheck;
if( cache == nullptr )
{
toCheck = toCheckLevel;
}
else
{
toCheck = reinterpret_cast<int *>(cache + (16 * 16 * 16));
}
int checkedPosition = 0;
int toCheckCount = 0;
//int darktcc = 0;
// 4J - added
int minXZ = - (dimension->getXZSize() * 16 ) / 2;
int maxXZ = (dimension->getXZSize() * 16 ) / 2 - 1;
if( ( xc > maxXZ ) || ( xc < minXZ ) || ( zc > maxXZ ) || ( zc < minXZ ) )
if( xc > maxXZ )
{
LeaveCriticalSection(&m_checkLightCS);
return;
}
// Lock 128K of cache (containing all the lighting cache + first 112K of toCheck array) on L2 to try and stop any cached data getting knocked out of L2 by other non-cached reads (or vice-versa)
// if( cache ) XLockL2(XLOCKL2_INDEX_TITLE, cache, 128 * 1024, XLOCKL2_LOCK_SIZE_1_WAY, 0 );
// If we're in cached mode, then use memory allocated after the cached data itself for the toCheck array, in an attempt to make both that & the other cached data sit on the CPU L2 cache better.
int *toCheck;
if( cache == nullptr )
toCheck = toCheckLevel;
else
toCheck = reinterpret_cast<int *>(cache + (16 * 16 * 16));
{
int checkedPosition = 0;
int toCheckCount = 0;
//int darktcc = 0;
if (xc >= minXZ && zc <= maxXZ && zc >= minXZ)
{
int centerCurrent = getBrightnessCached(cache, layer, xc, yc, zc);
int centerExpected = getExpectedLight(cache, xc, yc, zc, layer, false);
if( centerExpected != centerCurrent && cache )
{
initCacheComplete(cache, xc, yc, zc);
}
if (centerExpected > centerCurrent)
{
toCheck[toCheckCount++] = 32 | (32 << 6) | (32 << 12);
}
else if (centerExpected < centerCurrent)
{
// 4J - added tcn. This is the code that is run when checkLight has been called for a light source that has got darker / turned off.
@@ -3516,64 +3492,55 @@ void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc, bool f
// stage of the function to potentially have their brightnesses put back up again. We shouldn't need to consider All these tiles as starting points for this process, now just
// considering the edge tiles (defined as a tile where we have a neighbour that is brightner than can be explained by the original light source we are turning off)
int tcn = 0;
if (layer == LightLayer::Block || true)
toCheck[toCheckCount++] = 32 | (32 << 6) | (32 << 12) | (centerCurrent << 18);
while (checkedPosition < toCheckCount)
{
toCheck[toCheckCount++] = 32 | (32 << 6) | (32 << 12) | (centerCurrent << 18);
while (checkedPosition < toCheckCount)
int p = toCheck[checkedPosition++];
int x = ((p) & 63) - 32 + xc;
int y = ((p >> 6) & 63) - 32 + yc;
int z = ((p >> 12) & 63) - 32 + zc;
int expected = ((p >> 18) & 15);
int current = getBrightnessCached(cache, layer, x, y, z);
if (current == expected)
{
int p = toCheck[checkedPosition++];
int x = ((p) & 63) - 32 + xc;
int y = ((p >> 6) & 63) - 32 + yc;
int z = ((p >> 12) & 63) - 32 + zc;
int expected = ((p >> 18) & 15);
int current = getBrightnessCached(cache, layer, x, y, z);
if (current == expected)
setBrightnessCached(cache, &cacheUse, layer, x, y, z, 0);
// cexp--; // 4J - removed, change from 1.2.3
if (expected > 0)
{
setBrightnessCached(cache, &cacheUse, layer, x, y, z, 0);
// cexp--; // 4J - removed, change from 1.2.3
if (expected > 0)
int xd = Mth::abs(x - xc);
int yd = Mth::abs(y - yc);
int zd = Mth::abs(z - zc);
if (xd + yd + zd < 17)
{
int xd = Mth::abs(x - xc);
int yd = Mth::abs(y - yc);
int zd = Mth::abs(z - zc);
if (xd + yd + zd < 17)
bool edge = false;
for (int face = 0; face < 6; face++)
{
bool edge = false;
for (int face = 0; face < 6; face++)
{
int xx = x + Facing::STEP_X[face];
int yy = y + Facing::STEP_Y[face];
int zz = z + Facing::STEP_Z[face];
int xx = x + Facing::STEP_X[face];
int yy = y + Facing::STEP_Y[face];
int zz = z + Facing::STEP_Z[face];
// 4J - added - don't let this lighting creep out of the normal fixed world and into the infinite water chunks beyond
if( ( xx > maxXZ ) || ( xx < minXZ ) || ( zz > maxXZ ) || ( zz < minXZ ) ) continue;
if( ( yy < 0 ) || ( yy >= maxBuildHeight ) ) continue;
// 4J - added - don't let this lighting creep out of the normal fixed world and into the infinite water chunks beyond
if( ( xx > maxXZ ) || ( xx < minXZ ) || ( zz > maxXZ ) || ( zz < minXZ ) ) continue;
if( ( yy < 0 ) || ( yy >= maxBuildHeight ) ) continue;
// 4J - some changes here brought forward from 1.2.3
int block = max(1, getBlockingCached(cache, layer, nullptr, xx, yy, zz) );
current = getBrightnessCached(cache, layer, xx, yy, zz);
if ((current == expected - block) && (toCheckCount < (32 * 32 * 32))) // 4J - 32 * 32 * 32 was toCheck.length
{
toCheck[toCheckCount++] = (xx - xc + 32) | ((yy - yc + 32) << 6) | ((zz - zc + 32) << 12) | ((expected - block) << 18);
}
else
{
// 4J - added - keep track of which tiles form the edge of the region we are zeroing
if( current > ( expected - block ) )
{
edge = true;
}
}
}
// 4J - added - keep track of which tiles form the edge of the region we are zeroing - can store over the original elements in the array because tcn must be <= tcp
if( edge == true )
// 4J - some changes here brought forward from 1.2.3
int block = max(1, getBlockingCached(cache, layer, nullptr, xx, yy, zz) );
current = getBrightnessCached(cache, layer, xx, yy, zz);
if ((current == expected - block) && (toCheckCount < (32 * 32 * 32))) // 4J - 32 * 32 * 32 was toCheck.length
toCheck[toCheckCount++] = (xx - xc + 32) | ((yy - yc + 32) << 6) | ((zz - zc + 32) << 12) | ((expected - block) << 18);
else if( current > ( expected - block ) )
{
toCheck[tcn++] = p;
// 4J - added - keep track of which tiles form the edge of the region we are zeroing
edge = true;
}
}
// 4J - added - keep track of which tiles form the edge of the region we are zeroing - can store over the original elements in the array because tcn must be <= tcp
if( edge == true )
toCheck[tcn++] = p;
}
}
}
}
checkedPosition = 0;
@@ -3591,35 +3558,26 @@ void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc, bool f
// If force is set, then this is being used to in a special mode to try and light lava tiles as chunks are being loaded in. In this case, we
// don't want a lighting update to drag in any neighbouring chunks that aren't loaded yet.
if( force )
{
if( !hasChunkAt(x,y,z) )
{
continue;
}
}
if( force || (y < 0 && y > 256 && !hasChunkAt(x, y, z)) )
continue;
int current = getBrightnessCached(cache, layer, x, y, z);
// If rootOnlyEmissive flag is set, then only consider the starting tile to be possibly emissive.
bool propagatedOnly = false;
if (layer == LightLayer::Block)
{
if( rootOnlyEmissive )
{
propagatedOnly = ( x != xc ) || ( y != yc ) || ( z != zc );
}
}
int expected = getExpectedLight(cache, x, y, z, layer, propagatedOnly);
if (layer == LightLayer::Block && rootOnlyEmissive)
propagatedOnly = ( x != xc ) || ( y != yc ) || ( z != zc );
int expected = getExpectedLight(cache, x, y, z, layer, propagatedOnly);
if (expected != current)
{
setBrightnessCached(cache, &cacheUse, layer, x, y, z, expected);
if (expected > current)
{
int xd = abs(x - xc);
int yd = abs(y - yc);
int zd = abs(z - zc);
int xd = Mth::abs(x - xc);
int yd = Mth::abs(y - yc);
int zd = Mth::abs(z - zc);
bool withinBounds = toCheckCount < (32 * 32 * 32) - 6; // 4J - 32 * 32 * 32 was toCheck.length
if (xd + yd + zd < 17 && withinBounds)
{
@@ -3634,34 +3592,11 @@ void Level::checkLight(LightLayer::variety layer, int xc, int yc, int zc, bool f
}
}
}
// if( cache ) XUnlockL2(XLOCKL2_INDEX_TITLE);
#if 0
QueryPerformanceCounter( &qwNewTime );
qwDeltaTime1.QuadPart = qwNewTime.QuadPart - qwTime.QuadPart;
qwTime = qwNewTime;
#endif
flushCache(cache, cacheUse, layer);
#if 0
/////////////////////////////////////////////////////////////////
if( cache )
{
QueryPerformanceCounter( &qwNewTime );
qwDeltaTime2.QuadPart = qwNewTime.QuadPart - qwTime.QuadPart;
fElapsedTime1 = fSecsPerTick * ((FLOAT)(qwDeltaTime1.QuadPart));
fElapsedTime2 = fSecsPerTick * ((FLOAT)(qwDeltaTime2.QuadPart));
if( ( darktcc > 0 ) | ( tcc > 0 ) )
{
printf("%d %d %d %f + %f = %f\n", darktcc, tcc, darktcc + tcc, fElapsedTime1 * 1000.0f, fElapsedTime2 * 1000.0f, ( fElapsedTime1 + fElapsedTime2 ) * 1000.0f);
}
}
/////////////////////////////////////////////////////////////////
#endif
LeaveCriticalSection(&m_checkLightCS);
}
bool Level::tickPendingTicks(bool force)
{
return false;