From dc8e414209222319fcb11d739092bff3231e3b60 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Thu, 25 Jan 2024 23:35:23 -0500
Subject: [PATCH] Fixes for material preview from cosmic

---
 indra/newview/llfetchedgltfmaterial.h      | 2 ++
 indra/newview/llgltfmaterialpreviewmgr.cpp | 9 ++++++---
 indra/newview/lltexturectrl.cpp            | 4 ++--
 indra/newview/pipeline.cpp                 | 9 +++------
 indra/newview/pipeline.h                   | 1 -
 5 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/indra/newview/llfetchedgltfmaterial.h b/indra/newview/llfetchedgltfmaterial.h
index 2559aa46cc3..b49c667fe0a 100644
--- a/indra/newview/llfetchedgltfmaterial.h
+++ b/indra/newview/llfetchedgltfmaterial.h
@@ -40,6 +40,8 @@ class LLFetchedGLTFMaterial: public LLGLTFMaterial
     virtual ~LLFetchedGLTFMaterial();
 
     LLFetchedGLTFMaterial& operator=(const LLFetchedGLTFMaterial& rhs);
+    // LLGLTFMaterial::operator== is defined, but LLFetchedGLTFMaterial::operator== is not.
+    bool operator==(const LLGLTFMaterial& rhs) const = delete;
 
     // If this material is loaded, fire the given function
     void onMaterialComplete(std::function<void()> material_complete);
diff --git a/indra/newview/llgltfmaterialpreviewmgr.cpp b/indra/newview/llgltfmaterialpreviewmgr.cpp
index d020f5a2ff8..35cc370e838 100644
--- a/indra/newview/llgltfmaterialpreviewmgr.cpp
+++ b/indra/newview/llgltfmaterialpreviewmgr.cpp
@@ -34,6 +34,7 @@
 #include "llenvironment.h"
 #include "llselectmgr.h"
 #include "llviewercamera.h"
+#include "llviewercontrol.h"
 #include "llviewerobject.h"
 #include "llviewershadermgr.h"
 #include "llviewertexturelist.h"
@@ -95,7 +96,7 @@ namespace
 {
     void fetch_texture_for_ui(LLPointer<LLViewerFetchedTexture>& img, const LLUUID& id)
     {
-        if (id.notNull())
+        if (!img && id.notNull())
         {
             if (LLAvatarAppearanceDefines::LLAvatarAppearanceDictionary::isBakedImageId(id))
             {
@@ -334,7 +335,7 @@ void set_preview_sphere_material(PreviewSphere& preview_sphere, LLPointer<LLFetc
         info->mGLTFMaterial = material;
         LLVertexBuffer* buf = info->mVertexBuffer.get();
         LLStrider<LLColor4U> colors;
-        const S32 count = info->mEnd - info->mStart;
+        const S32 count = info->mEnd - info->mStart + 1;
         buf->getColorStrider(colors, info->mStart, count);
         for (S32 i = 0; i < count; ++i)
         {
@@ -417,7 +418,8 @@ BOOL LLGLTFPreviewTexture::render()
     SetTemporarily<LLPipeline::RenderTargetPack*> use_auxiliary_render_target(&gPipeline.mRT, &gPipeline.mAuxillaryRT);
 
     const LLVector4a light_dir(1.0f, 1.0f, 1.0f, 0.f);
-    SetTemporarily<S32> sun_light_only(&LLPipeline::RenderLocalLightCount, 0);
+    const S32 old_local_light_count = gSavedSettings.get<S32>("RenderLocalLightCount");
+    gSavedSettings.set<S32>("RenderLocalLightCount", 0);
 
     gPipeline.mReflectionMapManager.forceDefaultProbeAndUpdateUniforms();
 
@@ -522,6 +524,7 @@ BOOL LLGLTFPreviewTexture::render()
     // Clean up
     gPipeline.setupHWLights();
     gPipeline.mReflectionMapManager.forceDefaultProbeAndUpdateUniforms(false);
+    gSavedSettings.set<S32>("RenderLocalLightCount", old_local_light_count);
 
     return TRUE;
 }
diff --git a/indra/newview/lltexturectrl.cpp b/indra/newview/lltexturectrl.cpp
index 1d681f8bdd5..0ec647449d6 100644
--- a/indra/newview/lltexturectrl.cpp
+++ b/indra/newview/lltexturectrl.cpp
@@ -669,7 +669,7 @@ void LLFloaterTexturePicker::draw()
             {
                 mGLTFMaterial = (LLFetchedGLTFMaterial*) gGLTFMaterialList.getMaterial(mImageAssetID);
                 llassert(mGLTFMaterial == nullptr || dynamic_cast<LLFetchedGLTFMaterial*>(gGLTFMaterialList.getMaterial(mImageAssetID)) != nullptr);
-                if (mGLTFPreview.isNull() || mGLTFMaterial.isNull() || (old_material.notNull() && (*old_material.get() != *mGLTFMaterial.get())))
+                if (mGLTFPreview.isNull() || mGLTFMaterial.isNull() || (old_material.notNull() && (old_material.get() != mGLTFMaterial.get())))
                 {
                     // Only update the preview if needed, since gGLTFMaterialPreviewMgr does not cache the preview.
                     if (mGLTFMaterial.isNull())
@@ -2243,7 +2243,7 @@ void LLTextureCtrl::draw()
             if (mInventoryPickType == PICK_MATERIAL)
             {
                 mGLTFMaterial = gGLTFMaterialList.getMaterial(mImageAssetID);
-                if (mGLTFPreview.isNull() || mGLTFMaterial.isNull() || (old_material.notNull() && (*old_material.get() != *mGLTFMaterial.get())))
+                if (mGLTFPreview.isNull() || mGLTFMaterial.isNull() || (old_material.notNull() && (old_material.get() != mGLTFMaterial.get())))
                 {
                     // Only update the preview if needed, since gGLTFMaterialPreviewMgr does not cache the preview.
                     if (mGLTFMaterial.isNull())
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index df1a57193a7..82f04395208 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -179,7 +179,6 @@ F32 LLPipeline::CameraFocusTransitionTime;
 F32 LLPipeline::CameraFNumber;
 F32 LLPipeline::CameraFocalLength;
 F32 LLPipeline::CameraFieldOfView;
-S32 LLPipeline::RenderLocalLightCount;
 F32 LLPipeline::RenderShadowNoise;
 F32 LLPipeline::RenderShadowBlurSize;
 F32 LLPipeline::RenderSSAOScale;
@@ -550,7 +549,6 @@ void LLPipeline::init()
 	connectRefreshCachedSettingsSafe("CameraFNumber");
 	connectRefreshCachedSettingsSafe("CameraFocalLength");
 	connectRefreshCachedSettingsSafe("CameraFieldOfView");
-	connectRefreshCachedSettingsSafe("RenderLocalLightCount");
 	connectRefreshCachedSettingsSafe("RenderShadowNoise");
 	connectRefreshCachedSettingsSafe("RenderShadowBlurSize");
 	connectRefreshCachedSettingsSafe("RenderSSAOScale");
@@ -1091,7 +1089,6 @@ void LLPipeline::refreshCachedSettings()
 	CameraFNumber = gSavedSettings.getF32("CameraFNumber");
 	CameraFocalLength = gSavedSettings.getF32("CameraFocalLength");
 	CameraFieldOfView = gSavedSettings.getF32("CameraFieldOfView");
-	RenderLocalLightCount = gSavedSettings.getS32("RenderLocalLightCount");
 	RenderShadowNoise = gSavedSettings.getF32("RenderShadowNoise");
 	RenderShadowBlurSize = gSavedSettings.getF32("RenderShadowBlurSize");
 	RenderSSAOScale = gSavedSettings.getF32("RenderSSAOScale");
@@ -5362,7 +5359,7 @@ void LLPipeline::calcNearbyLights(LLCamera& camera)
 		return;
 	}
 
-    const S32 local_light_count = LLPipeline::RenderLocalLightCount;
+	static LLCachedControl<S32> local_light_count(gSavedSettings, "RenderLocalLightCount", 256);
 
 	if (local_light_count >= 1)
 	{
@@ -5629,7 +5626,7 @@ void LLPipeline::setupHWLights()
 
 	mLightMovingMask = 0;
 	
-    const S32 local_light_count = LLPipeline::RenderLocalLightCount;
+	static LLCachedControl<S32> local_light_count(gSavedSettings, "RenderLocalLightCount", 256);
 
 	if (local_light_count >= 1)
 	{
@@ -8015,7 +8012,7 @@ void LLPipeline::renderDeferredLighting()
             unbindDeferredShader(soften_shader);
         }
 
-        const S32 local_light_count = LLPipeline::RenderLocalLightCount;
+		static LLCachedControl<S32> local_light_count(gSavedSettings, "RenderLocalLightCount", 256);
 
         if (local_light_count > 0)
         {
diff --git a/indra/newview/pipeline.h b/indra/newview/pipeline.h
index a7e4df363a8..c3cc62965c5 100644
--- a/indra/newview/pipeline.h
+++ b/indra/newview/pipeline.h
@@ -1006,7 +1006,6 @@ class LLPipeline
 	static F32 CameraFNumber;
 	static F32 CameraFocalLength;
 	static F32 CameraFieldOfView;
-	static S32 RenderLocalLightCount;
 	static F32 RenderShadowNoise;
 	static F32 RenderShadowBlurSize;
 	static F32 RenderSSAOScale;
-- 
GitLab