From 42dafa5f09d3921617f54be712907ee12f1e8940 Mon Sep 17 00:00:00 2001
From: Cosmic Linden <cosmic@lindenlab.com>
Date: Fri, 15 Sep 2023 17:18:07 -0700
Subject: [PATCH] SL-20606: Refactor model matrix code

---
 indra/newview/lldrawpool.cpp        | 13 +++++++++----
 indra/newview/lldrawpool.h          |  2 ++
 indra/newview/lldrawpoolterrain.cpp | 15 ++-------------
 indra/newview/lldrawpooltree.cpp    | 13 ++-----------
 4 files changed, 15 insertions(+), 28 deletions(-)

diff --git a/indra/newview/lldrawpool.cpp b/indra/newview/lldrawpool.cpp
index f10c9047a38..556760632af 100644
--- a/indra/newview/lldrawpool.cpp
+++ b/indra/newview/lldrawpool.cpp
@@ -567,14 +567,19 @@ void LLRenderPass::pushRiggedMaskBatches(U32 type, bool texture, bool batch_text
 
 void LLRenderPass::applyModelMatrix(const LLDrawInfo& params)
 {
-	if (params.mModelMatrix != gGLLastMatrix)
+	applyModelMatrix(params.mModelMatrix);
+}
+
+void LLRenderPass::applyModelMatrix(const LLMatrix4* model_matrix)
+{
+	if (model_matrix != gGLLastMatrix)
 	{
-		gGLLastMatrix = params.mModelMatrix;
+		gGLLastMatrix = model_matrix;
 		gGL.matrixMode(LLRender::MM_MODELVIEW);
 		gGL.loadMatrix(gGLModelView);
-		if (params.mModelMatrix)
+		if (model_matrix)
 		{
-			gGL.multMatrix((GLfloat*) params.mModelMatrix->mMatrix);
+			gGL.multMatrix((GLfloat*) model_matrix->mMatrix);
 		}
 		gPipeline.mMatrixOpCount++;
 	}
diff --git a/indra/newview/lldrawpool.h b/indra/newview/lldrawpool.h
index 365c0222850..0a9c2cc4205 100644
--- a/indra/newview/lldrawpool.h
+++ b/indra/newview/lldrawpool.h
@@ -349,6 +349,8 @@ class LLRenderPass : public LLDrawPool
 	void resetDrawOrders() { }
 
 	static void applyModelMatrix(const LLDrawInfo& params);
+    // For rendering that doesn't use LLDrawInfo for some reason
+	static void applyModelMatrix(const LLMatrix4* model_matrix);
 	void pushBatches(U32 type, bool texture = true, bool batch_textures = false);
     void pushUntexturedBatches(U32 type);
 
diff --git a/indra/newview/lldrawpoolterrain.cpp b/indra/newview/lldrawpoolterrain.cpp
index 77189dceae2..43f75875918 100644
--- a/indra/newview/lldrawpoolterrain.cpp
+++ b/indra/newview/lldrawpoolterrain.cpp
@@ -204,19 +204,8 @@ void LLDrawPoolTerrain::drawLoop()
 		{
 			LLFace *facep = *iter;
 
-			LLMatrix4* model_matrix = &(facep->getDrawable()->getRegion()->mRenderMatrix);
-
-			if (model_matrix != gGLLastMatrix)
-			{
-				llassert(gGL.getMatrixMode() == LLRender::MM_MODELVIEW);
-				gGLLastMatrix = model_matrix;
-				gGL.loadMatrix(gGLModelView);
-				if (model_matrix)
-				{
-					gGL.multMatrix((GLfloat*) model_matrix->mMatrix);
-				}
-				gPipeline.mMatrixOpCount++;
-			}
+            llassert(gGL.getMatrixMode() == LLRender::MM_MODELVIEW);
+            LLRenderPass::applyModelMatrix(&facep->getDrawable()->getRegion()->mRenderMatrix);
 
 			facep->renderIndexed();
 		}
diff --git a/indra/newview/lldrawpooltree.cpp b/indra/newview/lldrawpooltree.cpp
index 9dcbc48697c..50c4a2c1b35 100644
--- a/indra/newview/lldrawpooltree.cpp
+++ b/indra/newview/lldrawpooltree.cpp
@@ -85,17 +85,8 @@ void LLDrawPoolTree::renderDeferred(S32 pass)
         {
             LLMatrix4* model_matrix = &(face->getDrawable()->getRegion()->mRenderMatrix);
 
-            if (model_matrix != gGLLastMatrix)
-            {
-                gGLLastMatrix = model_matrix;
-                gGL.loadMatrix(gGLModelView);
-                if (model_matrix)
-                {
-                    llassert(gGL.getMatrixMode() == LLRender::MM_MODELVIEW);
-                    gGL.multMatrix((GLfloat*)model_matrix->mMatrix);
-                }
-                gPipeline.mMatrixOpCount++;
-            }
+            llassert(gGL.getMatrixMode() == LLRender::MM_MODELVIEW);
+            LLRenderPass::applyModelMatrix(model_matrix);
 
             buff->setBuffer();
             buff->drawRange(LLRender::TRIANGLES, 0, buff->getNumVerts() - 1, buff->getNumIndices(), 0);
-- 
GitLab