From a701cce8e0959503156a010683f6d0d57beaae36 Mon Sep 17 00:00:00 2001
From: RunitaiLinden <davep@lindenlab.com>
Date: Wed, 1 May 2024 09:51:32 -0500
Subject: #1357 Preserve asset "extras" and switch to std::shared_ptr for
 referencing Asset from LLViewerObject to cut down rebuild time

---
 indra/newview/gltf/asset.cpp       |  6 +++---
 indra/newview/gltf/asset.h         |  4 +++-
 indra/newview/gltfscenemanager.cpp | 29 +++++++++++++----------------
 indra/newview/gltfscenemanager.h   | 10 ++++++++++
 indra/newview/llviewerobject.cpp   |  7 ++++---
 indra/newview/llviewerobject.h     | 11 +++++++++--
 6 files changed, 42 insertions(+), 25 deletions(-)

diff --git a/indra/newview/gltf/asset.cpp b/indra/newview/gltf/asset.cpp
index 475cbcb6e5..42f064699c 100644
--- a/indra/newview/gltf/asset.cpp
+++ b/indra/newview/gltf/asset.cpp
@@ -34,8 +34,6 @@
 
 using namespace LL::GLTF;
 
-#pragma optimize("", off)
-
 namespace LL
 {
     namespace GLTF
@@ -280,6 +278,7 @@ namespace LL
             dst.asset.version = src.mVersion;
             dst.asset.minVersion = src.mMinVersion;
             dst.asset.generator = "Linden Lab Experimental GLTF Export";
+            dst.asset.extras = src.mExtras;
 
             copy(src.mScenes, dst.scenes);
             copy(src.mNodes, dst.nodes);
@@ -739,9 +738,10 @@ const Asset& Asset::operator=(const tinygltf::Model& src)
     mMinVersion = src.asset.minVersion;
     mGenerator = src.asset.generator;
     mCopyright = src.asset.copyright;
+    mExtras = src.asset.extras;
 
     mDefaultScene = src.defaultScene;
-
+    
 
     mScenes.resize(src.scenes.size());
     for (U32 i = 0; i < src.scenes.size(); ++i)
diff --git a/indra/newview/gltf/asset.h b/indra/newview/gltf/asset.h
index cb28c4a572..5a62313705 100644
--- a/indra/newview/gltf/asset.h
+++ b/indra/newview/gltf/asset.h
@@ -265,7 +265,7 @@ namespace LL
         };
 
         // C++ representation of a GLTF Asset
-        class Asset : public LLRefCount
+        class Asset
         {
         public:
             std::vector<Scene> mScenes;
@@ -287,6 +287,8 @@ namespace LL
             std::string mCopyright;
 
             S32 mDefaultScene = INVALID_INDEX;
+            tinygltf::Value mExtras;
+
 
             // the last time update() was called according to gFrameTimeSeconds
             F32 mLastUpdateTime = gFrameTimeSeconds;
diff --git a/indra/newview/gltfscenemanager.cpp b/indra/newview/gltfscenemanager.cpp
index 0aedcd653d..6533f283e2 100644
--- a/indra/newview/gltfscenemanager.cpp
+++ b/indra/newview/gltfscenemanager.cpp
@@ -77,7 +77,7 @@ void GLTFSceneManager::load()
 void GLTFSceneManager::saveAs()
 {
     LLViewerObject* obj = LLSelectMgr::instance().getSelection()->getFirstRootObject();
-    if (obj && obj->mGLTFAsset.notNull())
+    if (obj && obj->mGLTFAsset)
     {
         LLFilePickerReplyThread::startPicker(
             [](const std::vector<std::string>& filenames, LLFilePicker::ELoadFilter load_filter, LLFilePicker::ESaveFilter save_filter)
@@ -103,7 +103,7 @@ void GLTFSceneManager::saveAs()
 void GLTFSceneManager::decomposeSelection()
 {
     LLViewerObject* obj = LLSelectMgr::instance().getSelection()->getFirstRootObject();
-    if (obj && obj->mGLTFAsset.notNull())
+    if (obj && obj->mGLTFAsset)
     {
         LLFilePickerReplyThread::startPicker(
             [](const std::vector<std::string>& filenames, LLFilePicker::ELoadFilter load_filter, LLFilePicker::ESaveFilter save_filter)
@@ -129,7 +129,7 @@ void GLTFSceneManager::decomposeSelection()
 void GLTFSceneManager::decomposeSelection(const std::string& filename)
 {
     LLViewerObject* obj = LLSelectMgr::instance().getSelection()->getFirstRootObject();
-    if (obj && obj->mGLTFAsset.notNull())
+    if (obj && obj->mGLTFAsset)
     {
         // copy asset out for decomposition
         Asset asset = *obj->mGLTFAsset;
@@ -148,9 +148,9 @@ void GLTFSceneManager::decomposeSelection(const std::string& filename)
 void GLTFSceneManager::save(const std::string& filename)
 {
     LLViewerObject* obj = LLSelectMgr::instance().getSelection()->getFirstRootObject();
-    if (obj && obj->mGLTFAsset.notNull())
+    if (obj && obj->mGLTFAsset)
     {
-        Asset* asset = obj->mGLTFAsset;
+        Asset* asset = obj->mGLTFAsset.get();
         tinygltf::Model model;
         asset->save(model);
 
@@ -163,7 +163,7 @@ void GLTFSceneManager::load(const std::string& filename)
     tinygltf::Model model;
     LLTinyGLTFHelper::loadModel(filename, model);
 
-    LLPointer<Asset> asset = new Asset();
+    std::shared_ptr<Asset> asset = std::make_shared<Asset>();
     *asset = model;
 
     gDebugProgram.bind(); // bind a shader to satisfy LLVertexBuffer assertions
@@ -210,10 +210,7 @@ void GLTFSceneManager::update()
             continue;
         }
 
-        Asset* asset = mObjects[i]->mGLTFAsset;
-
-        asset->update();
-     
+        mObjects[i]->mGLTFAsset->update();
     }
 }
 
@@ -235,7 +232,7 @@ void GLTFSceneManager::render(bool opaque, bool rigged)
             continue;
         }
 
-        Asset* asset = mObjects[i]->mGLTFAsset;
+        Asset* asset = mObjects[i]->mGLTFAsset.get();
 
         gGL.pushMatrix();
 
@@ -382,7 +379,7 @@ LLDrawable* GLTFSceneManager::lineSegmentIntersect(const LLVector4a& start, cons
         }
 
         // temporary debug -- always double check objects that have GLTF scenes hanging off of them even if the ray doesn't intersect the object bounds
-        if (lineSegmentIntersect((LLVOVolume*) mObjects[i].get(), mObjects[i]->mGLTFAsset, start, local_end, -1, pick_transparent, pick_rigged, pick_unselectable, node_hit, primitive_hit, &position, tex_coord, normal, tangent))
+        if (lineSegmentIntersect((LLVOVolume*) mObjects[i].get(), mObjects[i]->mGLTFAsset.get(), start, local_end, -1, pick_transparent, pick_rigged, pick_unselectable, node_hit, primitive_hit, &position, tex_coord, normal, tangent))
         {
             local_end = position;
             if (intersection)
@@ -509,7 +506,7 @@ void GLTFSceneManager::renderDebug()
 
         matMul(mat, modelview, modelview);
 
-        Asset* asset = obj->mGLTFAsset;
+        Asset* asset = obj->mGLTFAsset.get();
 
         for (auto& node : asset->mNodes)
         {
@@ -524,7 +521,7 @@ void GLTFSceneManager::renderDebug()
             continue;
         }
 
-        Asset* asset = obj->mGLTFAsset;
+        Asset* asset = obj->mGLTFAsset.get();
 
         LLMatrix4a mat = obj->getGLTFAssetToAgentTransform();
 
@@ -561,7 +558,7 @@ void GLTFSceneManager::renderDebug()
 
                 matMul(mat, modelview, modelview);
 
-                Asset* asset = obj->mGLTFAsset;
+                Asset* asset = obj->mGLTFAsset.get();
 
                 for (auto& node : asset->mNodes)
                 {
@@ -622,7 +619,7 @@ void GLTFSceneManager::renderDebug()
         if (drawable)
         {
             gGL.pushMatrix();
-            Asset* asset = drawable->getVObj()->mGLTFAsset;
+            Asset* asset = drawable->getVObj()->mGLTFAsset.get();
             Node* node = &asset->mNodes[node_hit];
             Primitive* primitive = &asset->mMeshes[node->mMesh].mPrimitives[primitive_hit];
 
diff --git a/indra/newview/gltfscenemanager.h b/indra/newview/gltfscenemanager.h
index 57d9e019a5..e380be7e3c 100644
--- a/indra/newview/gltfscenemanager.h
+++ b/indra/newview/gltfscenemanager.h
@@ -28,6 +28,16 @@
 
 #include "llsingleton.h"
 #include "llviewerobject.h"
+class LLVOVolume;
+class LLDrawable;
+
+namespace LL
+{
+    namespace GLTF
+    {
+        class Asset;
+    }
+}
 
 namespace LL
 {
diff --git a/indra/newview/llviewerobject.cpp b/indra/newview/llviewerobject.cpp
index 5b21040a04..cc6f4403dd 100644
--- a/indra/newview/llviewerobject.cpp
+++ b/indra/newview/llviewerobject.cpp
@@ -107,6 +107,7 @@
 #include "llmeshrepository.h"
 #include "llgltfmateriallist.h"
 #include "llgl.h"
+#include "gltf/asset.h"
 
 //#define DEBUG_UPDATE_TYPE
 
@@ -4420,7 +4421,7 @@ LLMatrix4a LLViewerObject::getGLTFNodeTransformAgent(S32 node_index) const
 {
     LLMatrix4a mat;
 
-    if (mGLTFAsset.notNull() && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
+    if (mGLTFAsset && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
     {
         auto& node = mGLTFAsset->mNodes[node_index];
 
@@ -4474,7 +4475,7 @@ void decomposeMatrix(const LLMatrix4a& mat, LLVector3& position, LLQuaternion& r
 
 void LLViewerObject::setGLTFNodeRotationAgent(S32 node_index, const LLQuaternion& rotation)
 {
-    if (mGLTFAsset.notNull() && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
+    if (mGLTFAsset && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
     {
         auto& node = mGLTFAsset->mNodes[node_index];
 
@@ -4506,7 +4507,7 @@ void LLViewerObject::setGLTFNodeRotationAgent(S32 node_index, const LLQuaternion
 
 void LLViewerObject::moveGLTFNode(S32 node_index, const LLVector3& offset)
 {
-    if (mGLTFAsset.notNull() && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
+    if (mGLTFAsset && node_index >= 0 && node_index < mGLTFAsset->mNodes.size())
     {
         auto& node = mGLTFAsset->mNodes[node_index];
 
diff --git a/indra/newview/llviewerobject.h b/indra/newview/llviewerobject.h
index 64c1ee6633..e314992532 100644
--- a/indra/newview/llviewerobject.h
+++ b/indra/newview/llviewerobject.h
@@ -45,7 +45,14 @@
 #include "llbbox.h"
 #include "llrigginginfo.h"
 #include "llreflectionmap.h"
-#include "gltf/asset.h"
+
+namespace LL
+{
+    namespace GLTF
+    {
+        class Asset;
+    }
+}
 
 class LLAgent;			// TODO: Get rid of this.
 class LLAudioSource;
@@ -736,7 +743,7 @@ public:
 	F32             mPhysicsRestitution;
 	
     // Associated GLTF Asset
-    LLPointer<LL::GLTF::Asset> mGLTFAsset;
+    std::shared_ptr<LL::GLTF::Asset> mGLTFAsset;
 
 	// Pipeline classes
 	LLPointer<LLDrawable> mDrawable;
-- 
cgit v1.2.3