From fe5a2e717e3e1f3b72a015066ad40f58e99d21d7 Mon Sep 17 00:00:00 2001
From: Kitty Barnett <develop@catznip.com>
Date: Sun, 11 Jul 2021 23:26:01 +0200
Subject: [PATCH] [FIXED] Crash when @setsphere is issued with ALM enabled
 without shadows, ambient occlusion, depth of field, or antialiasing

---
 indra/newview/pipeline.cpp   |  5 ++++-
 indra/newview/rlvactions.cpp |  6 ++++++
 indra/newview/rlvactions.h   |  4 ++++
 indra/newview/rlvhandler.cpp | 11 +++++++++--
 4 files changed, 23 insertions(+), 3 deletions(-)

diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index bf0e3ed1601..9532770e17f 100644
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -966,7 +966,10 @@ bool LLPipeline::allocateScreenBuffer(U32 resX, U32 resY, U32 samples)
 			mFXAABuffer.release();
 		}
 		
-		if (shadow_detail > 0 || ssao || RenderDepthOfField || samples > 0)
+//		if (shadow_detail > 0 || ssao || RenderDepthOfField || samples > 0)
+// [RLVa:KB] - @setsphere
+		if (shadow_detail > 0 || ssao || RenderDepthOfField || samples > 0 || RlvActions::hasPostProcess())
+// [/RLVa:KB]
 		{ //only need mDeferredLight for shadows OR ssao OR dof OR fxaa
 			if (!mDeferredLight.allocate(resX, resY, GL_RGBA, FALSE, FALSE, LLTexUnit::TT_RECT_TEXTURE, FALSE)) return false;
 		}
diff --git a/indra/newview/rlvactions.cpp b/indra/newview/rlvactions.cpp
index 6629fc519b3..912a8092f59 100644
--- a/indra/newview/rlvactions.cpp
+++ b/indra/newview/rlvactions.cpp
@@ -18,6 +18,7 @@
 #include "llagent.h"
 #include "llimview.h"
 #include "llviewercamera.h"
+#include "llvisualeffect.h"
 #include "llvoavatarself.h"
 #include "llworld.h"
 
@@ -397,6 +398,11 @@ bool RlvActions::canChangeEnvironment(const LLUUID& idRlvObject)
 	return (idRlvObject.isNull()) ? !gRlvHandler.hasBehaviour(RLV_BHVR_SETENV) : !gRlvHandler.hasBehaviourExcept(RLV_BHVR_SETENV, idRlvObject);
 }
 
+bool RlvActions::hasPostProcess()
+{
+	return LLVfxManager::instance().hasEffect(EVisualEffect::RlvSphere);
+}
+
 // ============================================================================
 // World interaction
 //
diff --git a/indra/newview/rlvactions.h b/indra/newview/rlvactions.h
index 217eb21a588..36b59c2a1b5 100644
--- a/indra/newview/rlvactions.h
+++ b/indra/newview/rlvactions.h
@@ -236,6 +236,10 @@ class RlvActions
 	 */
 	static bool canChangeEnvironment(const LLUUID& idRlvObject = LLUUID::null);
 
+	/*
+	 * Returns true if a postprocessing shader is currently active
+	 */
+	static bool hasPostProcess();
 
 	// =================
 	// World interaction
diff --git a/indra/newview/rlvhandler.cpp b/indra/newview/rlvhandler.cpp
index 26022657b9e..0bf2c0f3f43 100644
--- a/indra/newview/rlvhandler.cpp
+++ b/indra/newview/rlvhandler.cpp
@@ -2124,6 +2124,8 @@ ERlvCmdRet RlvBehaviourHandler<RLV_BHVR_SETSPHERE>::onCommand(const RlvCommand&
 	{
 		if (gRlvHandler.hasBehaviour(rlvCmd.getObjectID(), rlvCmd.getBehaviourType()))
 		{
+			LLVfxManager::instance().addEffect(new RlvSphereEffect(rlvCmd.getObjectID()));
+
 			Rlv::forceAtmosphericShadersIfAvailable();
 
 			// If we're not using deferred but are using Windlight shaders we need to force use of FBO and depthmap texture
@@ -2137,8 +2139,13 @@ ERlvCmdRet RlvBehaviourHandler<RLV_BHVR_SETSPHERE>::onCommand(const RlvCommand&
 				gPipeline.resetVertexBuffers();
 				LLViewerShaderMgr::instance()->setShaders();
 			}
-
-			LLVfxManager::instance().addEffect(new RlvSphereEffect(rlvCmd.getObjectID()));
+			else if (!gPipeline.mDeferredLight.isComplete())
+			{
+				// In case of deferred with no shadows, no ambient occlusion, no depth of field, and no antialiasing
+				gPipeline.releaseGLBuffers();
+				gPipeline.createGLBuffers();
+				RLV_ASSERT(gPipeline.mDeferredLight.isComplete());
+			}
 		}
 		else
 		{
-- 
GitLab