From 47255bf44d04e5ba7b33d44f8cc738da4be9d53a Mon Sep 17 00:00:00 2001
From: Cosmic Linden <cosmic@lindenlab.com>
Date: Thu, 11 Apr 2024 17:26:02 -0700
Subject: secondlife/viewer#1184: Stop making terrain textures undeletable

---
 indra/llrender/llgltexture.cpp      |  3 +-
 indra/llrender/llgltexture.h        |  3 +-
 indra/newview/lldrawpoolterrain.cpp | 36 +----------------
 indra/newview/llvlcomposition.cpp   | 79 ++++++++++++++++++++++++++-----------
 indra/newview/llvlcomposition.h     |  3 ++
 5 files changed, 64 insertions(+), 60 deletions(-)

diff --git a/indra/llrender/llgltexture.cpp b/indra/llrender/llgltexture.cpp
index b616002b49..a590a8e745 100644
--- a/indra/llrender/llgltexture.cpp
+++ b/indra/llrender/llgltexture.cpp
@@ -95,7 +95,8 @@ void LLGLTexture::setBoostLevel(S32 level)
 		mBoostLevel = level ;
 		if(mBoostLevel != LLGLTexture::BOOST_NONE
 		   && mBoostLevel != LLGLTexture::BOOST_ICON
-           && mBoostLevel != LLGLTexture::BOOST_THUMBNAIL)
+           && mBoostLevel != LLGLTexture::BOOST_THUMBNAIL
+           && mBoostLevel != LLGLTexture::BOOST_TERRAIN)
 		{
 			setNoDelete() ;		
 		}
diff --git a/indra/llrender/llgltexture.h b/indra/llrender/llgltexture.h
index 1cc8fbe523..fa6faa2583 100644
--- a/indra/llrender/llgltexture.h
+++ b/indra/llrender/llgltexture.h
@@ -52,10 +52,11 @@ public:
 		BOOST_AVATAR		,
         BOOST_AVATAR_BAKED  ,
 		BOOST_SCULPTED      ,
+        BOOST_TERRAIN       , // Needed for minimap generation for now. Lower than BOOST_HIGH so the texture stats don't get forced, i.e. texture stats are manually managed by minimap/terrain instead.
 		
 		BOOST_HIGH 			= 10,
 		BOOST_BUMP          ,
-		BOOST_TERRAIN		, // has to be high priority for minimap / low detail
+        BOOST_UNUSED_1      , // Placeholder to avoid disrupting habits around texture debug
 		BOOST_SELECTED		,		
 		BOOST_AVATAR_BAKED_SELF	,
 		BOOST_AVATAR_SELF	, // needed for baking avatar
diff --git a/indra/newview/lldrawpoolterrain.cpp b/indra/newview/lldrawpoolterrain.cpp
index 2d198c5b4b..c5932a6ad9 100644
--- a/indra/newview/lldrawpoolterrain.cpp
+++ b/indra/newview/lldrawpoolterrain.cpp
@@ -115,41 +115,7 @@ void LLDrawPoolTerrain::boostTerrainDetailTextures()
     // Hack! Get the region that this draw pool is rendering from!
 	LLViewerRegion *regionp = mDrawFace[0]->getDrawable()->getVObj()->getRegion();
 	LLVLComposition *compp = regionp->getComposition();
-	for (S32 i = 0; i < 4; i++)
-	{
-        constexpr LLGLTexture::EBoostLevel level = LLGLTexture::BOOST_TERRAIN;
-        constexpr float stats = 1024.f * 1024.f;
-
-        LLPointer<LLViewerFetchedTexture>& tex = compp->mDetailTextures[i];
-        llassert(tex.notNull());
-		tex->setBoostLevel(level);
-        tex->addTextureStats(stats);
-
-        LLPointer<LLFetchedGLTFMaterial>& fetched_material = compp->mDetailMaterials[i];
-        if (fetched_material)
-        {
-            if (fetched_material->mBaseColorTexture)
-            {
-                fetched_material->mBaseColorTexture->setBoostLevel(level);
-                fetched_material->mBaseColorTexture->addTextureStats(stats);
-            }
-            if (fetched_material->mNormalTexture)
-            {
-                fetched_material->mNormalTexture->setBoostLevel(level);
-                fetched_material->mNormalTexture->addTextureStats(stats);
-            }
-            if (fetched_material->mMetallicRoughnessTexture)
-            {
-                fetched_material->mMetallicRoughnessTexture->setBoostLevel(level);
-                fetched_material->mMetallicRoughnessTexture->addTextureStats(stats);
-            }
-            if (fetched_material->mEmissiveTexture)
-            {
-                fetched_material->mEmissiveTexture->setBoostLevel(level);
-                fetched_material->mEmissiveTexture->addTextureStats(stats);
-            }
-        }
-	}
+    compp->boost();
 }
 
 void LLDrawPoolTerrain::beginDeferredPass(S32 pass)
diff --git a/indra/newview/llvlcomposition.cpp b/indra/newview/llvlcomposition.cpp
index 6c0691c6a9..6955949571 100644
--- a/indra/newview/llvlcomposition.cpp
+++ b/indra/newview/llvlcomposition.cpp
@@ -45,6 +45,7 @@
 extern LLColor4U MAX_WATER_COLOR;
 
 static const U32 BASE_SIZE = 128;
+static const F32 TERRAIN_DECODE_PRIORITY = 2048.f * 2048.f;
 
 namespace
 {
@@ -64,25 +65,38 @@ namespace
         return result;
     }
 
-    void unboost_minimap_texture(LLPointer<LLViewerFetchedTexture>& tex)
+    void boost_minimap_texture(LLViewerFetchedTexture* tex, F32 virtual_size)
+    {
+        llassert(tex);
+        if (!tex) { return; }
+
+        tex->setBoostLevel(LLGLTexture::BOOST_TERRAIN); // in case the raw image is at low detail
+        tex->addTextureStats(virtual_size); // priority
+    }
+
+    void boost_minimap_material(LLFetchedGLTFMaterial* mat, F32 virtual_size)
+    {
+        if (!mat) { return; }
+        if (mat->mBaseColorTexture) { boost_minimap_texture(mat->mBaseColorTexture, virtual_size); }
+        if (mat->mNormalTexture) { boost_minimap_texture(mat->mNormalTexture, virtual_size); }
+        if (mat->mMetallicRoughnessTexture) { boost_minimap_texture(mat->mMetallicRoughnessTexture, virtual_size); }
+        if (mat->mEmissiveTexture) { boost_minimap_texture(mat->mEmissiveTexture, virtual_size); }
+    }
+
+    void unboost_minimap_texture(LLViewerFetchedTexture* tex)
     {
         if (!tex) { return; }
         tex->setBoostLevel(LLGLTexture::BOOST_NONE);
         tex->setMinDiscardLevel(MAX_DISCARD_LEVEL + 1);
-
-        if (tex->getTextureState() == LLGLTexture::NO_DELETE)
-        {
-            tex->forceActive();
-        }
     }
 
-    void unboost_minimap_material(LLPointer<LLFetchedGLTFMaterial>& mat)
+    void unboost_minimap_material(LLFetchedGLTFMaterial* mat)
     {
         if (!mat) { return; }
-        unboost_minimap_texture(mat->mBaseColorTexture);
-        unboost_minimap_texture(mat->mNormalTexture);
-        unboost_minimap_texture(mat->mMetallicRoughnessTexture);
-        unboost_minimap_texture(mat->mEmissiveTexture);
+        if (mat->mBaseColorTexture) { unboost_minimap_texture(mat->mBaseColorTexture); }
+        if (mat->mNormalTexture) { unboost_minimap_texture(mat->mNormalTexture); }
+        if (mat->mMetallicRoughnessTexture) { unboost_minimap_texture(mat->mMetallicRoughnessTexture); }
+        if (mat->mEmissiveTexture) { unboost_minimap_texture(mat->mEmissiveTexture); }
     }
 };
 
@@ -96,11 +110,7 @@ LLTerrainMaterials::LLTerrainMaterials()
 
 LLTerrainMaterials::~LLTerrainMaterials()
 {
-    for (S32 i = 0; i < ASSET_COUNT; ++i)
-    {
-        unboost_minimap_texture(mDetailTextures[i]);
-        unboost_minimap_material(mDetailMaterials[i]);
-    }
+    unboost();
 }
 
 BOOL LLTerrainMaterials::generateMaterials()
@@ -118,6 +128,31 @@ BOOL LLTerrainMaterials::generateMaterials()
     return FALSE;
 }
 
+void LLTerrainMaterials::boost()
+{
+    for (S32 i = 0; i < ASSET_COUNT; ++i)
+    {
+        LLPointer<LLViewerFetchedTexture>& tex = mDetailTextures[i];
+        llassert(tex.notNull());
+        boost_minimap_texture(tex, TERRAIN_DECODE_PRIORITY);
+
+        LLPointer<LLFetchedGLTFMaterial>& mat = mDetailMaterials[i];
+        boost_minimap_material(mat, TERRAIN_DECODE_PRIORITY);
+    }
+}
+
+void LLTerrainMaterials::unboost()
+{
+    for (S32 i = 0; i < ASSET_COUNT; ++i)
+    {
+        LLPointer<LLViewerFetchedTexture>& tex = mDetailTextures[i];
+        unboost_minimap_texture(tex);
+
+        LLPointer<LLFetchedGLTFMaterial>& mat = mDetailMaterials[i];
+        unboost_minimap_material(mat);
+    }
+}
+
 LLUUID LLTerrainMaterials::getDetailAssetID(S32 asset)
 {
     llassert(mDetailTextures[asset] && mDetailMaterials[asset]);
@@ -135,7 +170,6 @@ LLPointer<LLViewerFetchedTexture> fetch_terrain_texture(const LLUUID& id)
     }
 
     LLPointer<LLViewerFetchedTexture> tex = LLViewerTextureManager::getFetchedTexture(id);
-    tex->setNoDelete();
     return tex;
 }
 
@@ -240,8 +274,7 @@ bool LLTerrainMaterials::textureReady(LLPointer<LLViewerFetchedTexture>& tex, bo
     {
         if (boost)
         {
-            tex->setBoostLevel(LLGLTexture::BOOST_TERRAIN); // in case we are at low detail
-            tex->addTextureStats(BASE_SIZE*BASE_SIZE);
+            boost_minimap_texture(tex, BASE_SIZE*BASE_SIZE);
         }
         return false;
     }
@@ -251,6 +284,8 @@ bool LLTerrainMaterials::textureReady(LLPointer<LLViewerFetchedTexture>& tex, bo
     {
         if (boost)
         {
+            boost_minimap_texture(tex, BASE_SIZE*BASE_SIZE);
+
             S32 width = tex->getFullWidth();
             S32 height = tex->getFullHeight();
             S32 min_dim = llmin(width, height);
@@ -260,9 +295,7 @@ bool LLTerrainMaterials::textureReady(LLPointer<LLViewerFetchedTexture>& tex, bo
                 ddiscard++;
                 min_dim /= 2;
             }
-            tex->setBoostLevel(LLGLTexture::BOOST_TERRAIN); // in case we are at low detail
             tex->setMinDiscardLevel(ddiscard);
-            tex->addTextureStats(BASE_SIZE*BASE_SIZE); // priority
         }
         return false;
     }
@@ -589,7 +622,7 @@ BOOL LLVLComposition::generateMinimapTileLand(const F32 x, const F32 y,
                 // Raw image is not ready, will enter here again later.
                 if (tex->getFetchPriority() <= 0.0f && !tex->hasSavedRawImage())
                 {
-                    tex->setBoostLevel(LLGLTexture::BOOST_TERRAIN);
+                    boost_minimap_texture(tex, TERRAIN_DECODE_PRIORITY);
                     tex->forceToRefetchTexture(ddiscard);
                 }
 
@@ -606,7 +639,7 @@ BOOL LLVLComposition::generateMinimapTileLand(const F32 x, const F32 y,
                     // Raw image is not ready, will enter here again later.
                     if (tex_emissive->getFetchPriority() <= 0.0f && !tex_emissive->hasSavedRawImage())
                     {
-                        tex_emissive->setBoostLevel(LLGLTexture::BOOST_TERRAIN);
+                        boost_minimap_texture(tex_emissive, TERRAIN_DECODE_PRIORITY);
                         tex_emissive->forceToRefetchTexture(ddiscard_emissive);
                     }
 
diff --git a/indra/newview/llvlcomposition.h b/indra/newview/llvlcomposition.h
index 7397ff1e8d..6a460e3285 100644
--- a/indra/newview/llvlcomposition.h
+++ b/indra/newview/llvlcomposition.h
@@ -58,6 +58,8 @@ public:
 
     BOOL generateMaterials();
 
+    void boost();
+
 	virtual LLUUID getDetailAssetID(S32 asset);
 	virtual void setDetailAssetID(S32 asset, const LLUUID& id);
     Type getMaterialType();
@@ -67,6 +69,7 @@ public:
     bool materialsReady(bool boost, bool strict);
 
 protected:
+    void unboost();
     static bool textureReady(LLPointer<LLViewerFetchedTexture>& tex, bool boost);
     // strict = true -> all materials must be sufficiently loaded
     // strict = false -> at least one material must be loaded
-- 
cgit v1.2.3