From 7634df683e902d335a804536704817abf756df42 Mon Sep 17 00:00:00 2001
From: Rye Mutt <rye@alchemyviewer.org>
Date: Mon, 8 Nov 2021 23:13:26 -0500
Subject: [PATCH] Replace more GLH code with SIMD

---
 indra/llrender/llrender.h                  |  16 +++-
 indra/newview/altoolalign.cpp              |   4 +-
 indra/newview/lldrawpoolwater.cpp          |   3 +-
 indra/newview/llmanipscale.cpp             |   4 +-
 indra/newview/llmaniptranslate.cpp         |   4 +-
 indra/newview/llpanelprimmediacontrols.cpp |  40 +++++---
 indra/newview/llviewercamera.cpp           | 102 +++++++--------------
 indra/newview/llviewercamera.h             |  10 +-
 indra/newview/llviewerdisplay.cpp          |  49 +++++-----
 indra/newview/pipeline.cpp                 |  15 +--
 10 files changed, 113 insertions(+), 134 deletions(-)

diff --git a/indra/llrender/llrender.h b/indra/llrender/llrender.h
index 79ad9a8ba93..00b37978ac9 100644
--- a/indra/llrender/llrender.h
+++ b/indra/llrender/llrender.h
@@ -542,10 +542,18 @@ extern LLRender gGL;
 
 // This rotation matrix moves the default OpenGL reference frame 
 // (-Z at, Y up) to Cory's favorite reference frame (X at, Z up)
-const F32 OGL_TO_CFR_ROTATION[16] = {  0.f,  0.f, -1.f,  0.f, 	// -Z becomes X
-									  -1.f,  0.f,  0.f,  0.f, 	// -X becomes Y
-									   0.f,  1.f,  0.f,  0.f,	//  Y becomes Z
-									   0.f,  0.f,  0.f,  1.f };
+inline constexpr F32 OGL_TO_CFR_ROTATION[16] = {  0.f,  0.f, -1.f,  0.f, 	// -Z becomes X
+												 -1.f,  0.f,  0.f,  0.f, 	// -X becomes Y
+												  0.f,  1.f,  0.f,  0.f,	//  Y becomes Z
+												  0.f,  0.f,  0.f,  1.f };
+
+// This rotation matrix moves the default OpenGL reference frame 
+// (-Z at, Y up) to Cory's favorite reference frame (X at, Z up)
+inline static LL_ALIGN_16(const LLMatrix4a) OGL_TO_CFR_ROTATION_4A(
+	LLVector4a(0.f, 0.f, -1.f, 0.f), 	// -Z becomes X
+	LLVector4a(-1.f, 0.f, 0.f, 0.f), 	// -X becomes Y
+	LLVector4a(0.f, 1.f, 0.f, 0.f),		//  Y becomes Z
+	LLVector4a(0.f, 0.f, 0.f, 1.f));
 
 glh::matrix4f copy_matrix(F32* src);
 glh::matrix4f get_current_modelview();
diff --git a/indra/newview/altoolalign.cpp b/indra/newview/altoolalign.cpp
index 674a92cc141..c804a7c070d 100644
--- a/indra/newview/altoolalign.cpp
+++ b/indra/newview/altoolalign.cpp
@@ -130,8 +130,8 @@ BOOL ALToolAlign::findSelectedManipulator(S32 x, S32 y)
 	{
 		transform.initAll(LLVector3(1.f, 1.f, 1.f), mBBox.getRotation(), mBBox.getCenterAgent());
 
-		LLMatrix4 projection_matrix = camera->getProjection();
-		LLMatrix4 model_matrix = camera->getModelview();
+		LLMatrix4 projection_matrix(camera->getProjection().getF32ptr());
+		LLMatrix4 model_matrix(camera->getModelview().getF32ptr());
 
 		transform *= model_matrix;
 		transform *= projection_matrix;
diff --git a/indra/newview/lldrawpoolwater.cpp b/indra/newview/lldrawpoolwater.cpp
index d42592b8ef0..0627b089295 100644
--- a/indra/newview/lldrawpoolwater.cpp
+++ b/indra/newview/lldrawpoolwater.cpp
@@ -279,8 +279,7 @@ void LLDrawPoolWater::render(S32 pass)
 
 		gGL.matrixMode(LLRender::MM_TEXTURE);
 		gGL.loadIdentity();
-		LLMatrix4a camera_rot;
-		camera_rot.loadu((F32*)LLViewerCamera::getInstanceFast()->getModelview().mMatrix);
+		LLMatrix4a camera_rot = LLViewerCamera::getInstanceFast()->getModelview();
 		camera_rot.extractRotation_affine();
 		camera_rot.invert();
 
diff --git a/indra/newview/llmanipscale.cpp b/indra/newview/llmanipscale.cpp
index bdade5bdc82..c67baf79753 100644
--- a/indra/newview/llmanipscale.cpp
+++ b/indra/newview/llmanipscale.cpp
@@ -477,8 +477,8 @@ void LLManipScale::highlightManipulators(S32 x, S32 y)
 		}
 		else
 		{
-			LLMatrix4 projMatrix = LLViewerCamera::getInstanceFast()->getProjection();
-			LLMatrix4 modelView = LLViewerCamera::getInstanceFast()->getModelview();
+			LLMatrix4 projMatrix(LLViewerCamera::getInstanceFast()->getProjection().getF32ptr());
+			LLMatrix4 modelView(LLViewerCamera::getInstanceFast()->getModelview().getF32ptr());
 			transform.initAll(LLVector3(1.f, 1.f, 1.f), bbox.getRotation(), bbox.getPositionAgent());
 
 			transform *= modelView;
diff --git a/indra/newview/llmaniptranslate.cpp b/indra/newview/llmaniptranslate.cpp
index d7b8ae3fcd6..cc0da11d65d 100644
--- a/indra/newview/llmaniptranslate.cpp
+++ b/indra/newview/llmaniptranslate.cpp
@@ -801,8 +801,8 @@ void LLManipTranslate::highlightManipulators(S32 x, S32 y)
 	}
 	
 	//LLBBox bbox = LLSelectMgr::getInstanceFast()->getBBoxOfSelection();
-	LLMatrix4 projMatrix = LLViewerCamera::getInstanceFast()->getProjection();
-	LLMatrix4 modelView = LLViewerCamera::getInstanceFast()->getModelview();
+	LLMatrix4 projMatrix( LLViewerCamera::getInstanceFast()->getProjection().getF32ptr() );
+	LLMatrix4 modelView( LLViewerCamera::getInstanceFast()->getModelview().getF32ptr() );
 
 	LLVector3 object_position = getPivotPoint();
 	
diff --git a/indra/newview/llpanelprimmediacontrols.cpp b/indra/newview/llpanelprimmediacontrols.cpp
index 13b362086cb..fb4645f199f 100644
--- a/indra/newview/llpanelprimmediacontrols.cpp
+++ b/indra/newview/llpanelprimmediacontrols.cpp
@@ -67,7 +67,7 @@
 glh::matrix4f get_current_modelview();
 glh::matrix4f get_current_projection();
 // Functions pulled from llviewerdisplay.cpp
-bool get_hud_matrices(glh::matrix4f &proj, glh::matrix4f &model);
+bool get_hud_matrices(LLMatrix4a& proj, LLMatrix4a& model);
 
 // Warning: make sure these two match!
 const LLPanelPrimMediaControls::EZoomLevel LLPanelPrimMediaControls::kZoomLevels[] = { ZOOM_NONE, ZOOM_MEDIUM };
@@ -647,37 +647,47 @@ void LLPanelPrimMediaControls::updateShape()
 		vert_it = vect_face.begin();
 		vert_end = vect_face.end();
 		
-		glh::matrix4f mat;
+		LLMatrix4a mat;
 		if (!is_hud) 
 		{
-			mat = get_current_projection() * get_current_modelview();
+			LLMatrix4a proj, modelview;
+			proj.loadu(gGLProjection);
+			modelview.loadu(gGLModelView);
+			mat.setMul(proj, modelview);
 		}
 		else {
-			glh::matrix4f proj, modelview;
+			LLMatrix4a proj, modelview;
 			if (get_hud_matrices(proj, modelview))
-				mat = proj * modelview;
+			{
+				mat.setMul(proj,modelview);
+			}
 		}
-		LLVector3 min = LLVector3(1,1,1);
-		LLVector3 max = LLVector3(-1,-1,-1);
+		LLVector4a min;
+		min.splat(1.f);
+		LLVector4a max;
+		max.splat(-1.f);
 		for(; vert_it != vert_end; ++vert_it)
 		{
 			// project silhouette vertices into screen space
-			glh::vec3f screen_vert = glh::vec3f(vert_it->mV); 
-			mat.mult_matrix_vec(screen_vert);
-			
+			LLVector4a screen_vert;
+			screen_vert.load3(vert_it->mV,1.f);
+
+			mat.perspectiveTransform(screen_vert,screen_vert);
+
 			// add to screenspace bounding box
-			update_min_max(min, max, LLVector3(screen_vert.v));
+			min.setMin(screen_vert,min);
+			max.setMax(screen_vert,max);
 		}
 		
 		// convert screenspace bbox to pixels (in screen coords)
 		LLRect window_rect = gViewerWindow->getWorldViewRectScaled();
 		LLCoordGL screen_min;
-		screen_min.mX = ll_round((F32)window_rect.mLeft + (F32)window_rect.getWidth() * (min.mV[VX] + 1.f) * 0.5f);
-		screen_min.mY = ll_round((F32)window_rect.mBottom + (F32)window_rect.getHeight() * (min.mV[VY] + 1.f) * 0.5f);
+		screen_min.mX = ll_round((F32)window_rect.mLeft + (F32)window_rect.getWidth() * (min.getF32ptr()[VX] + 1.f) * 0.5f);
+		screen_min.mY = ll_round((F32)window_rect.mBottom + (F32)window_rect.getHeight() * (min.getF32ptr()[VY] + 1.f) * 0.5f);
 		
 		LLCoordGL screen_max;
-		screen_max.mX = ll_round((F32)window_rect.mLeft + (F32)window_rect.getWidth() * (max.mV[VX] + 1.f) * 0.5f);
-		screen_max.mY = ll_round((F32)window_rect.mBottom + (F32)window_rect.getHeight() * (max.mV[VY] + 1.f) * 0.5f);
+		screen_max.mX = ll_round((F32)window_rect.mLeft + (F32)window_rect.getWidth() * (max.getF32ptr()[VX] + 1.f) * 0.5f);
+		screen_max.mY = ll_round((F32)window_rect.mBottom + (F32)window_rect.getHeight() * (max.getF32ptr()[VY] + 1.f) * 0.5f);
 		
 		// grow panel so that screenspace bounding box fits inside "media_region" element of panel
 		LLRect media_panel_rect;
diff --git a/indra/newview/llviewercamera.cpp b/indra/newview/llviewercamera.cpp
index 8b4521ae2bf..a1557315c5a 100644
--- a/indra/newview/llviewercamera.cpp
+++ b/indra/newview/llviewercamera.cpp
@@ -63,28 +63,6 @@ LLTrace::CountStatHandle<> LLViewerCamera::sAngularVelocityStat("camera_angular_
 
 LLViewerCamera::eCameraID LLViewerCamera::sCurCameraID = LLViewerCamera::CAMERA_WORLD;
 
-//glu pick matrix implementation borrowed from Mesa3D
-glh::matrix4f gl_pick_matrix(GLfloat x, GLfloat y, GLfloat width, GLfloat height, GLint* viewport)
-{
-	GLfloat m[16];
-	GLfloat sx, sy;
-	GLfloat tx, ty;
-
-	sx = viewport[2] / width;
-	sy = viewport[3] / height;
-	tx = (viewport[2] + 2.f * (viewport[0] - x)) / width;
-	ty = (viewport[3] + 2.f * (viewport[1] - y)) / height;
-
-	#define M(row,col) m[col*4+row]
-	M(0,0) = sx; M(0,1) = 0.f; M(0,2) = 0.f; M(0,3) = tx;
-	M(1,0) = 0.f; M(1,1) = sy; M(1,2) = 0.f; M(1,3) = ty;
-	M(2,0) = 0.f; M(2,1) = 0.f; M(2,2) = 1.f; M(2,3) = 0.f;
-	M(3,0) = 0.f; M(3,1) = 0.f; M(3,2) = 0.f; M(3,3) = 1.f;
-	#undef M
-
-	return glh::matrix4f(m);
-}
-
 // Build time optimization, generate this once in .cpp file
 template class LLViewerCamera* LLSingleton<class LLViewerCamera>::getInstance();
 
@@ -147,37 +125,26 @@ void LLViewerCamera::updateCameraLocation(const LLVector3 &center,
 	mScreenPixelArea =(S32)((F32)getViewHeightInPixels() * ((F32)getViewHeightInPixels() * getAspect()));
 }
 
-const LLMatrix4 &LLViewerCamera::getProjection() const
+const LLMatrix4a &LLViewerCamera::getProjection() const
 {
 	calcProjection(getFar());
 	return mProjectionMatrix;
 
 }
 
-const LLMatrix4 &LLViewerCamera::getModelview() const
+const LLMatrix4a &LLViewerCamera::getModelview() const
 {
-	LLMatrix4 cfr(OGL_TO_CFR_ROTATION);
-	getMatrixToLocal(mModelviewMatrix);
-	mModelviewMatrix *= cfr;
+	LLMatrix4 modelview;
+	getMatrixToLocal(modelview);
+	LLMatrix4a modelviewa;
+	modelviewa.loadu((F32*)modelview.mMatrix);
+	mModelviewMatrix.setMul(OGL_TO_CFR_ROTATION_4A, modelviewa);
 	return mModelviewMatrix;
 }
 
 void LLViewerCamera::calcProjection(const F32 far_distance) const
 {
-	F32 fov_y, z_far, z_near, aspect, f;
-	fov_y = getView();
-	z_far = far_distance;
-	z_near = getNear();
-	aspect = getAspect();
-
-	f = 1/tan(fov_y*0.5f);
-
-	mProjectionMatrix.setZero();
-	mProjectionMatrix.mMatrix[0][0] = f/aspect;
-	mProjectionMatrix.mMatrix[1][1] = f;
-	mProjectionMatrix.mMatrix[2][2] = (z_far + z_near)/(z_near - z_far);
-	mProjectionMatrix.mMatrix[3][2] = (2*z_far*z_near)/(z_near - z_far);
-	mProjectionMatrix.mMatrix[2][3] = -1;
+	mProjectionMatrix = ALGLMath::genPersp( getView()*RAD_TO_DEG, getAspect(), getNear(), far_distance );
 }
 
 // Sets up opengl state for 3D drawing.  If for selection, also
@@ -279,20 +246,24 @@ void LLViewerCamera::setPerspective(BOOL for_selection,
 	gGL.matrixMode(LLRender::MM_PROJECTION);
 	gGL.loadIdentity();
 
-	glh::matrix4f proj_mat;
+	LLMatrix4a proj_mat;
+	proj_mat.setIdentity();
 
 	if (for_selection)
 	{
 		// make a tiny little viewport
 		// anything drawn into this viewport will be "selected"
 
-		GLint viewport[4];
-		viewport[0] = gViewerWindow->getWorldViewRectRaw().mLeft;
-		viewport[1] = gViewerWindow->getWorldViewRectRaw().mBottom;
-		viewport[2] = gViewerWindow->getWorldViewRectRaw().getWidth();
-		viewport[3] = gViewerWindow->getWorldViewRectRaw().getHeight();
+		const LLRect& rect = gViewerWindow->getWorldViewRectRaw();
 		
-		proj_mat = gl_pick_matrix(x+width/2.f, y_from_bot+height/2.f, (GLfloat) width, (GLfloat) height, viewport);
+		const F32 scale_x = rect.getWidth() / F32(width);
+		const F32 scale_y = rect.getHeight() / F32(height);
+		const F32 trans_x = scale_x + (2.f * (rect.mLeft - x)) / F32(width) - 1.f;
+		const F32 trans_y = scale_y + (2.f * (rect.mBottom - y_from_bot)) / F32(height) - 1.f;
+
+		//Generate a pick matrix
+		proj_mat.applyScale_affine(scale_x, scale_y, 1.f);
+		proj_mat.setTranslate_affine(LLVector3(trans_x, trans_y, 0.f));
 
 		if (limit_select_distance)
 		{
@@ -328,37 +299,28 @@ void LLViewerCamera::setPerspective(BOOL for_selection,
 		float offset = mZoomFactor - 1.f;
 		int pos_y = mZoomSubregion / llceil(mZoomFactor);
 		int pos_x = mZoomSubregion - (pos_y*llceil(mZoomFactor));
-		glh::matrix4f translate;
-		translate.set_translate(glh::vec3f(offset - (F32)pos_x * 2.f, offset - (F32)pos_y * 2.f, 0.f));
-		glh::matrix4f scale;
-		scale.set_scale(glh::vec3f(mZoomFactor, mZoomFactor, 1.f));
 
-		proj_mat = scale*proj_mat;
-		proj_mat = translate*proj_mat;
+		proj_mat.applyTranslation_affine(offset - (F32)pos_x * 2.f, offset - (F32)pos_y * 2.f, 0.f);
+		proj_mat.applyScale_affine(mZoomFactor,mZoomFactor,1.f);
 	}
 
 	calcProjection(z_far); // Update the projection matrix cache
 
-	proj_mat *= gl_perspective(fov_y,aspect,z_near,z_far);
-
-	gGL.loadMatrix(proj_mat.m);
-
-	for (U32 i = 0; i < 16; i++)
-	{
-		gGLProjection[i] = proj_mat.m[i];
-	}
+	proj_mat.mul(ALGLMath::genPersp(fov_y,aspect,z_near,z_far));
+	
+	gGL.loadMatrix(proj_mat);
+	
+	set_current_projection(proj_mat.getF32ptr());
 
 	gGL.matrixMode(LLRender::MM_MODELVIEW);
 
-	glh::matrix4f modelview((GLfloat*) OGL_TO_CFR_ROTATION);
-
-	GLfloat			ogl_matrix[16];
-
-	getOpenGLTransform(ogl_matrix);
+	LLMatrix4a ogl_matrix;
+	getOpenGLTransform(ogl_matrix.getF32ptr());
 
-	modelview *= glh::matrix4f(ogl_matrix);
+	LLMatrix4a modelview;
+	modelview.setMul(OGL_TO_CFR_ROTATION_4A, ogl_matrix);
 	
-	gGL.loadMatrix(modelview.m);
+	gGL.loadMatrix(modelview);
 	
 	if (for_selection && (width > 1 || height > 1))
 	{
@@ -376,7 +338,7 @@ void LLViewerCamera::setPerspective(BOOL for_selection,
 	if (!for_selection && mZoomFactor == 1.f)
 	{
 		// Save GL matrices for access elsewhere in code, especially project_world_to_screen
-		set_current_modelview(modelview);
+		set_current_modelview(modelview.getF32ptr());
 	}
 
 	updateFrustumPlanes(*this);
diff --git a/indra/newview/llviewercamera.h b/indra/newview/llviewercamera.h
index ca0dee8ecfc..1716b7c872e 100644
--- a/indra/newview/llviewercamera.h
+++ b/indra/newview/llviewercamera.h
@@ -35,6 +35,7 @@
 #include "lltrace.h"
 
 class LLViewerObject;
+
 const BOOL FOR_SELECTION = TRUE;
 const BOOL NOT_FOR_SELECTION = FALSE;
 
@@ -83,8 +84,8 @@ class LLViewerCamera final : public LLCamera, public LLSingleton<LLViewerCamera>
 	static void updateCameraAngle(void* user_data, const LLSD& value);
 	void setPerspective(BOOL for_selection, S32 x, S32 y_from_bot, S32 width, S32 height, BOOL limit_select_distance, F32 z_near = 0, F32 z_far = 0);
 
-	const LLMatrix4 &getProjection() const;
-	const LLMatrix4 &getModelview() const;
+	const LLMatrix4a &getProjection() const;
+	const LLMatrix4a &getModelview() const;
 
 	// Warning!  These assume the current global matrices are correct
 	void projectScreenToPosAgent(const S32 screen_x, const S32 screen_y, LLVector3* pos_agent ) const;
@@ -134,8 +135,9 @@ class LLViewerCamera final : public LLCamera, public LLSingleton<LLViewerCamera>
 	LLVector3 mVelocityDir ;
 	F32       mAverageSpeed ;
 	F32       mAverageAngularSpeed ;
-	mutable LLMatrix4	mProjectionMatrix;	// Cache of perspective matrix
-	mutable LLMatrix4	mModelviewMatrix;
+
+	LL_ALIGN_16(mutable LLMatrix4a	mProjectionMatrix);	// Cache of perspective matrix
+	LL_ALIGN_16(mutable LLMatrix4a	mModelviewMatrix);
 	F32					mCameraFOVDefault;
 	F32					mPrevCameraFOVDefault;
 	F32					mSavedFOVDefault;
diff --git a/indra/newview/llviewerdisplay.cpp b/indra/newview/llviewerdisplay.cpp
index ae7172566d6..106494b7ef0 100644
--- a/indra/newview/llviewerdisplay.cpp
+++ b/indra/newview/llviewerdisplay.cpp
@@ -30,6 +30,7 @@
 
 #include "llgl.h"
 #include "llrender.h"
+#include "alglmath.h"
 #include "llglheaders.h"
 #include "llagent.h"
 #include "llagentcamera.h"
@@ -1234,7 +1235,7 @@ LLRect get_whole_screen_region()
 	return whole_screen;
 }
 
-bool get_hud_matrices(const LLRect& screen_region, glh::matrix4f &proj, glh::matrix4f &model)
+bool get_hud_matrices(const LLRect& screen_region, LLMatrix4a &proj, LLMatrix4a &model)
 {
 	if (isAgentAvatarValid() && gAgentAvatarp->hasHUDAttachment())
 	{
@@ -1243,28 +1244,24 @@ bool get_hud_matrices(const LLRect& screen_region, glh::matrix4f &proj, glh::mat
 		LLBBox hud_bbox = gAgentAvatarp->getHUDBBox();
 		
 		F32 hud_depth = llmax(1.f, hud_bbox.getExtentLocal().mV[VX] * 1.1f);
-		proj = gl_ortho(-0.5f * vwrCamera.getAspect(), 0.5f * vwrCamera.getAspect(), -0.5f, 0.5f, 0.f, hud_depth);
-		proj.element(2,2) = -0.01f;
-		
-		F32 aspect_ratio = vwrCamera.getAspect();
+		proj = ALGLMath::genOrtho(-0.5f * vwrCamera.getAspect(), 0.5f * vwrCamera.getAspect(), -0.5f, 0.5f, 0.f, hud_depth);
+		proj.getRow<2>().copyComponent<2>(LLVector4a(-0.01f));
+
+		F32 aspect_ratio = LLViewerCamera::getInstance()->getAspect();
 		
-		glh::matrix4f mat;
 		F32 scale_x = (F32)gViewerWindow->getWorldViewWidthScaled() / (F32)screen_region.getWidth();
 		F32 scale_y = (F32)gViewerWindow->getWorldViewHeightScaled() / (F32)screen_region.getHeight();
-		mat.set_scale(glh::vec3f(scale_x, scale_y, 1.f));
-		mat.set_translate(
-			glh::vec3f(clamp_rescale((F32)(screen_region.getCenterX() - screen_region.mLeft), 0.f, (F32)gViewerWindow->getWorldViewWidthScaled(), 0.5f * scale_x * aspect_ratio, -0.5f * scale_x * aspect_ratio),
-					   clamp_rescale((F32)(screen_region.getCenterY() - screen_region.mBottom), 0.f, (F32)gViewerWindow->getWorldViewHeightScaled(), 0.5f * scale_y, -0.5f * scale_y),
-					   0.f));
-		proj *= mat;
-		
-		glh::matrix4f tmp_model((GLfloat*) OGL_TO_CFR_ROTATION);
-		
-		mat.set_scale(glh::vec3f(zoom_level, zoom_level, zoom_level));
-		mat.set_translate(glh::vec3f(-hud_bbox.getCenterLocal().mV[VX] + (hud_depth * 0.5f), 0.f, 0.f));
-		
-		tmp_model *= mat;
-		model = tmp_model;		
+
+		proj.applyTranslation_affine(
+			clamp_rescale((F32)(screen_region.getCenterX() - screen_region.mLeft), 0.f, (F32)gViewerWindow->getWorldViewWidthScaled(), 0.5f * scale_x * aspect_ratio, -0.5f * scale_x * aspect_ratio),
+			clamp_rescale((F32)(screen_region.getCenterY() - screen_region.mBottom), 0.f, (F32)gViewerWindow->getWorldViewHeightScaled(), 0.5f * scale_y, -0.5f * scale_y),
+			0.f);
+		proj.applyScale_affine(scale_x, scale_y, 1.f);
+
+		model = OGL_TO_CFR_ROTATION_4A;
+		model.applyTranslation_affine(LLVector3(-hud_bbox.getCenterLocal().mV[VX] + (hud_depth * 0.5f), 0.f, 0.f));
+		model.applyScale_affine(zoom_level);
+
 		return TRUE;
 	}
 	else
@@ -1273,7 +1270,7 @@ bool get_hud_matrices(const LLRect& screen_region, glh::matrix4f &proj, glh::mat
 	}
 }
 
-bool get_hud_matrices(glh::matrix4f &proj, glh::matrix4f &model)
+bool get_hud_matrices(LLMatrix4a &proj, LLMatrix4a &model)
 {
 	LLRect whole_screen = get_whole_screen_region();
 	return get_hud_matrices(whole_screen, proj, model);
@@ -1287,18 +1284,18 @@ bool setup_hud_matrices()
 
 bool setup_hud_matrices(const LLRect& screen_region)
 {
-	glh::matrix4f proj, model;
+	LLMatrix4a proj, model;
 	bool result = get_hud_matrices(screen_region, proj, model);
 	if (!result) return result;
 	
 	// set up transform to keep HUD objects in front of camera
 	gGL.matrixMode(LLRender::MM_PROJECTION);
-	gGL.loadMatrix(proj.m);
-	set_current_projection(proj);
+	gGL.loadMatrix(proj);
+	set_current_projection(proj.getF32ptr());
 	
 	gGL.matrixMode(LLRender::MM_MODELVIEW);
-	gGL.loadMatrix(model.m);
-	set_current_modelview(model);
+	gGL.loadMatrix(model);
+	set_current_modelview(model.getF32ptr());
 	return TRUE;
 }
 
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 0c22bd51c4d..6ef02884b2b 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -6107,17 +6107,18 @@ void LLPipeline::setupAvatarLights(bool for_edit)
     LLEnvironment& environment = LLEnvironment::instanceFast();
     bool sun_up = environment.getIsSunUp();
 
-
 	if (for_edit)
 	{
 		LLColor4 diffuse(1.f, 1.f, 1.f, 0.f);
-		LLVector4 light_pos_cam(-8.f, 0.25f, 10.f, 0.f);  // w==0 => directional light
-		LLMatrix4 camera_mat = LLViewerCamera::getInstanceFast()->getModelview();
-		LLMatrix4 camera_rot(camera_mat.getMat3());
+		LLVector4a light_pos_cam(-8.f, 0.25f, 10.f, 0.f);  // w==0 => directional light
+		LLMatrix4a camera_rot = LLViewerCamera::getInstanceFast()->getModelview();
+		camera_rot.extractRotation_affine();
 		camera_rot.invert();
-		LLVector4 light_pos = light_pos_cam * camera_rot;
+		LLVector4a light_pos;
+		
+		camera_rot.rotate(light_pos_cam,light_pos);
 		
-		light_pos.normalize();
+		light_pos.normalize3fast();
 
 		LLLightState* light = gGL.getLight(1);
 
@@ -6126,7 +6127,7 @@ void LLPipeline::setupAvatarLights(bool for_edit)
 		light->setDiffuse(diffuse);
 		light->setAmbient(LLColor4::black);
 		light->setSpecular(LLColor4::black);
-		light->setPosition(light_pos);
+		light->setPosition(LLVector4(light_pos.getF32ptr()));
 		light->setConstantAttenuation(1.f);
 		light->setLinearAttenuation(0.f);
 		light->setQuadraticAttenuation(0.f);
-- 
GitLab