Optimizations

This solves an FPS problem when 10 or more entities take damage
This commit is contained in:
GabsPuNs
2026-05-28 22:22:22 -04:00
parent 160044b48f
commit 8ffadc0f15
11 changed files with 230 additions and 480 deletions

View File

@@ -295,7 +295,7 @@ void Textures::loadIndexedTextures()
intArray Textures::loadTexturePixels(TEXTURE_NAME texId, const wstring& resourceName)
{
TexturePack *skin = skins->getSelected();
//TexturePack *skin = skins->getSelected();
{
intArray id = pixelsMap[resourceName];
@@ -1097,172 +1097,147 @@ void Textures::removeHttpTexture(const wstring& url)
int Textures::loadMemTexture(const wstring& url, const wstring& backup)
{
MemTexture *texture = nullptr;
auto it = memTextures.find(url);
if (it != memTextures.end())
{
texture = (*it).second;
}
texture = it->second.get();
if(texture == nullptr && app.IsFileInMemoryTextures(url))
{
// If we haven't loaded it yet, but we have the data for it then add it
texture = addMemTexture(url, new MobSkinMemTextureProcessor() );
}
if(texture != nullptr)
{
texture->ticksSinceLastUse = 0;
if (texture->loadedImage != nullptr && !texture->isLoaded)
{
// 4J - Disable mipmapping in general for skins & capes. Have seen problems with edge-on polys for some eg mumbo jumbo
if( ( url.substr(0,7) == L"dlcskin" ) ||
( url.substr(0,7) == L"dlccape" ) )
{
MIPMAP = false;
}
bool isDlc = (url.compare(0, 7, L"dlcskin") == 0) || (url.compare(0, 7, L"dlccape") == 0);
if (isDlc)
MIPMAP = false;
if (texture->id < 0)
{
texture->id = getTexture(texture->loadedImage, C4JRender::TEXTURE_FORMAT_RxGyBzAw, MIPMAP);
}
else
{
loadTexture(texture->loadedImage, texture->id);
}
texture->isLoaded = true;
MIPMAP = true;
}
}
if (texture == nullptr || texture->id < 0)
{
if (backup.empty() ) return -1;
if (backup.empty() )
return -1;
return loadTexture(TN_COUNT,backup);
}
return texture->id;
}
int Textures::loadMemTexture(const wstring& url, int backup)
{
MemTexture *texture = nullptr;
auto it = memTextures.find(url);
if (it != memTextures.end())
{
texture = (*it).second;
}
texture = it->second.get();
if(texture == nullptr && app.IsFileInMemoryTextures(url))
{
// If we haven't loaded it yet, but we have the data for it then add it
texture = addMemTexture(url, new MobSkinMemTextureProcessor() );
}
if(texture != nullptr)
{
texture->ticksSinceLastUse = 0;
if (texture->loadedImage != nullptr && !texture->isLoaded)
{
// 4J - Disable mipmapping in general for skins & capes. Have seen problems with edge-on polys for some eg mumbo jumbo
if( ( url.substr(0,7) == L"dlcskin" ) ||
( url.substr(0,7) == L"dlccape" ) )
{
MIPMAP = false;
}
bool isDlc = (url.compare(0, 7, L"dlcskin") == 0) || (url.compare(0, 7, L"dlccape") == 0);
if (isDlc) MIPMAP = false;
if (texture->id < 0)
{
texture->id = getTexture(texture->loadedImage, C4JRender::TEXTURE_FORMAT_RxGyBzAw, MIPMAP);
}
else
{
loadTexture(texture->loadedImage, texture->id);
}
texture->isLoaded = true;
MIPMAP = true;
}
}
if (texture == nullptr || texture->id < 0)
{
return loadTexture(backup);
}
return texture->id;
}
MemTexture *Textures::addMemTexture(const wstring& name,MemTextureProcessor *processor)
{
MemTexture *texture = nullptr;
std::unique_ptr<MemTextureProcessor> safeProcessor(processor);
auto it = memTextures.find(name);
if (it != memTextures.end())
{
texture = (*it).second;
}
if(texture == nullptr)
{
// can we find it in the app mem files?
PBYTE pbData=nullptr;
DWORD dwBytes=0;
app.GetMemFileDetails(name,&pbData,&dwBytes);
MemTexture* texture = it->second.get();
if (texture)
texture->count++;
if(dwBytes!=0)
{
texture = new MemTexture(name, pbData, dwBytes, processor);
memTextures[name] = texture;
}
else
{
// 4J Stu - Make an entry for this anyway and we can populate it later
memTextures[name] = nullptr;
}
return texture;
}
// can we find it in the app mem files?
PBYTE pbData = nullptr;
DWORD dwBytes = 0;
app.GetMemFileDetails(name,&pbData,&dwBytes);
if(dwBytes!=0)
{
memTextures[name] = std::make_unique<MemTexture>(name, pbData, dwBytes, safeProcessor.get());
return memTextures[name].get();
}
else
{
texture->count++;
// 4J Stu - Make an entry for this anyway and we can populate it later
memTextures[name] = nullptr;
return nullptr;
}
delete processor;
return texture;
}
// MemTexture *Textures::getMemTexture(const wstring& url, MemTextureProcessor *processor)
// {
// MemTexture *texture = memTextures[url];
// if (texture != nullptr)
// {
// texture->count++;
// }
// return texture;
// }
void Textures::removeMemTexture(const wstring& url)
{
MemTexture *texture = nullptr;
auto it = memTextures.find(url);
if (it != memTextures.end())
{
texture = (*it).second;
if (it == memTextures.end()) return;
// If it's nullptr then we should just remove the entry
if( texture == nullptr ) memTextures.erase(url);
}
if(texture != nullptr)
if (it->second == nullptr)
{
memTextures.erase(it);
return;
}
MemTexture* texture = it->second.get();
texture->count--;
if (texture->count == 0)
{
texture->count--;
if (texture->count == 0)
{
if (texture->id >= 0) releaseTexture(texture->id);
memTextures.erase(url);
delete texture;
}
if (texture->id >= 0)
releaseTexture(texture->id);
memTextures.erase(it);
}
}
void Textures::tick(bool updateTextures, bool tickDynamics) // 4J added updateTextures parameter & tickDynamics
{
MemSect(22);
if(tickDynamics)
if(tickDynamics && updateTextures)
{
// 4J - added - if we aren't updating the final renderer textures, just tick each of the dynamic textures instead. This is used so that in frames were we have multiple
// ticks due to framerate compensation, that we don't lock the renderer textures twice needlessly and force the CPU to sync with the GPU.
if( !updateTextures )
{
MemSect(0);
return;
}
// 4J - added - tell renderer that we're about to do a block of dynamic texture updates, so we can unlock the resources after they are done rather than a series of locks/unlocks
//RenderManager.TextureDynamicUpdateStart();
terrain->cycleAnimationFrames();
@@ -1274,31 +1249,26 @@ void Textures::tick(bool updateTextures, bool tickDynamics) // 4J added updateTe
// have their ticksSinceLastUse reset in Textures::loadMemTexture.
for (auto it = memTextures.begin(); it != memTextures.end();)
{
MemTexture *tex = it->second;
if( tex && ( ++tex->ticksSinceLastUse > MemTexture::UNUSED_TICKS_TO_FREE ) )
MemTexture* tex = it->second.get();
if( tex && ( ++tex->ticksSinceLastUse > MemTexture::UNUSED_TICKS_TO_FREE ) )
{
if (tex->id >= 0) releaseTexture(tex->id);
delete tex;
if (tex->id >= 0)
releaseTexture(tex->id);
it = memTextures.erase(it);
}
else
{
it++;
}
++it;
}
MemSect(0);
}
void Textures::reloadAll()
{
TexturePack *skin = skins->getSelected();
//TexturePack *skin = skins->getSelected();
for( int i = 0; i < TN_COUNT - 2; i++ )
{
releaseTexture(preLoadedIdx[i]);
}
idMap.clear();
loadedImages.clear();
@@ -1306,66 +1276,10 @@ void Textures::reloadAll()
loadIndexedTextures();
pixelsMap.clear();
// 4J Stu - These are not used any more
//WaterColor::init(loadTexturePixels(L"misc/watercolor.png"));
//GrassColor::init(loadTexturePixels(L"misc/grasscolor.png"));
//FoliageColor::init(loadTexturePixels(L"misc/foliagecolor.png"));
stitch();
skins->clearInvalidTexturePacks();
#if 0
for(auto it = loadedImages.begin(); it != loadedImages.end(); it++ )
{
BufferedImage *image = it->second;
loadTexture(image, it->first);
}
for(auto it = httpTextures.begin(); it != httpTextures.end(); it++ )
{
it->second->isLoaded = false;
}
for(auto it = memTextures.begin(); it != memTextures.end(); it++ )
{
it->second->isLoaded = false;
}
for( auto it = idMap.begin(); it != idMap.end(); it++ )
{
wstring name = it->first;
int id = idMap[name];
BufferedImage *image;
wstring prefix = L"%blur%";
bool blur = name.substr(0, prefix.size()).compare(prefix) == 0; //name.startsWith("%blur%");
if (blur) name = name.substr(6);
prefix = L"%clamp%";
bool clamp = name.substr(0, prefix.size()).compare(prefix) == 0; //name.startsWith("%clamp%");
if (clamp) name = name.substr(7);
image = readImage(skin->getResource(name));
loadTexture(image, id, blur, clamp);
delete image;
}
for( auto it = pixelsMap.begin(); it != pixelsMap.end(); it++ )
{
wstring name = it->first;
BufferedImage *image = readImage(skin->getResource(name));
loadTexturePixels(image, pixelsMap[name]);
delete image;
}
#endif
// Recalculate fonts
//Minecraft::GetInstance()->font->loadCharacterWidths();
//Minecraft::GetInstance()->altFont->loadCharacterWidths();
}
void Textures::stitch()