From db44e06d66cfa11e55660663ea33e604b604ace3 Mon Sep 17 00:00:00 2001
From: Cosmic Linden <cosmic@lindenlab.com>
Date: Wed, 1 Mar 2023 16:15:26 -0800
Subject: [PATCH] SL-19294: Fix Blinn-Phong texture transforms affecting GLTF
 materials

---
 indra/newview/llface.cpp     | 95 +++++++++++++++++++++---------------
 indra/newview/llvovolume.cpp |  3 ++
 2 files changed, 58 insertions(+), 40 deletions(-)

diff --git a/indra/newview/llface.cpp b/indra/newview/llface.cpp
index a74950dad2f..d05e367c12c 100644
--- a/indra/newview/llface.cpp
+++ b/indra/newview/llface.cpp
@@ -1524,7 +1524,8 @@ BOOL LLFace::getGeometryVolume(const LLVolume& volume,
 					     || mVertexBuffer->hasDataType(LLVertexBuffer::TYPE_TEXCOORD2);
 			}
 			
-			bool do_tex_mat = tex_mode && mTextureMatrix;
+            // For GLTF materials: Transforms will be applied later
+			bool do_tex_mat = tex_mode && mTextureMatrix && !gltf_mat;
 
 			if (!do_bump)
 			{ //not bump mapped, might be able to do a cheap update
@@ -1614,7 +1615,7 @@ BOOL LLFace::getGeometryVolume(const LLVolume& volume,
 							*tex_coords0++ = tc;	
 						}
 					}
-					else
+					else if (do_xform)
 					{
 						for (S32 i = 0; i < num_vertices; i++)
 						{	
@@ -1627,6 +1628,20 @@ BOOL LLFace::getGeometryVolume(const LLVolume& volume,
 						
 							xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
 
+							*tex_coords0++ = tc;	
+						}
+					}
+					else
+					{
+						for (S32 i = 0; i < num_vertices; i++)
+						{	
+							LLVector2 tc(vf.mTexCoords[i]);
+							LLVector4a& norm = vf.mNormals[i];
+							LLVector4a& center = *(vf.mCenter);
+							LLVector4a vec = vf.mPositions[i];	
+							vec.mul(scalea);
+							planarProjection(tc, norm, center, vec);
+
 							*tex_coords0++ = tc;	
 						}
 					}
@@ -1694,44 +1709,44 @@ BOOL LLFace::getGeometryVolume(const LLVolume& volume,
 					}
 					
 
-				for (S32 i = 0; i < num_vertices; i++)
-				{	
-					LLVector2 tc(vf.mTexCoords[i]);
-			
-					LLVector4a& norm = vf.mNormals[i];
-				
-					LLVector4a& center = *(vf.mCenter);
-		   
-					if (texgen != LLTextureEntry::TEX_GEN_DEFAULT)
-					{
-						LLVector4a vec = vf.mPositions[i];
-				
-						vec.mul(scalea);
-
-							if (texgen == LLTextureEntry::TEX_GEN_PLANAR)
-						{
-								planarProjection(tc, norm, center, vec);
-						}		
-					}
-
-					if (tex_mode && mTextureMatrix)
-					{
-						LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
-						tmp = tmp * *mTextureMatrix;
-						tc.mV[0] = tmp.mV[0];
-						tc.mV[1] = tmp.mV[1];
-					}
-					else
-					{
-						xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
-					}
-
-						*dst++ = tc;
-					if (do_bump)
-					{
-						bump_tc.push_back(tc);
-					}
-				}
+                    for (S32 i = 0; i < num_vertices; i++)
+                    {   
+                        LLVector2 tc(vf.mTexCoords[i]);
+                
+                        LLVector4a& norm = vf.mNormals[i];
+                    
+                        LLVector4a& center = *(vf.mCenter);
+               
+                        if (texgen != LLTextureEntry::TEX_GEN_DEFAULT)
+                        {
+                            LLVector4a vec = vf.mPositions[i];
+                    
+                            vec.mul(scalea);
+
+                            if (texgen == LLTextureEntry::TEX_GEN_PLANAR)
+                            {
+                                planarProjection(tc, norm, center, vec);
+                            }       
+                        }
+
+                        if (tex_mode && mTextureMatrix)
+                        {
+                            LLVector3 tmp(tc.mV[0], tc.mV[1], 0.f);
+                            tmp = tmp * *mTextureMatrix;
+                            tc.mV[0] = tmp.mV[0];
+                            tc.mV[1] = tmp.mV[1];
+                        }
+                        else if (do_xform)
+                        {
+                            xform(tc, cos_ang, sin_ang, os, ot, ms, mt);
+                        }
+
+                        *dst++ = tc;
+                        if (do_bump)
+                        {
+                            bump_tc.push_back(tc);
+                        }
+                    }
 				}
 
 				if ((!mat && !gltf_mat) && do_bump)
diff --git a/indra/newview/llvovolume.cpp b/indra/newview/llvovolume.cpp
index 42e764b492e..d88713a690b 100644
--- a/indra/newview/llvovolume.cpp
+++ b/indra/newview/llvovolume.cpp
@@ -5878,6 +5878,9 @@ void LLVolumeGeometryManager::rebuildGeom(LLSpatialGroup* group)
                                 if (gltf_mat != nullptr)
                                 {
                                     // all gltf materials have all vertex attributes for now
+                                    // *TODO: More finely enumerate vertex attributes for GLTF materials so
+                                    // that LLFace::getGeometryVolume can use a more optimal computation path
+                                    // as appropriate. (see: "whole expensive loop") -Cosmic,2023-03-01
                                     add_face(sNormSpecFaces, normspec_count, facep);
                                 }
                                 else
-- 
GitLab