From a2c7b0485576c6bb92f6d0eddc762f5e37d5caac Mon Sep 17 00:00:00 2001
From: Xiaohong Bao <bao@lindenlab.com>
Date: Wed, 7 Aug 2013 22:53:27 -0600
Subject: [PATCH] more fix for SH-4397: Object cache occlusion culling results
 are not always correct

---
 indra/newview/llspatialpartition.cpp | 2 +-
 indra/newview/llspatialpartition.h   | 2 +-
 indra/newview/llvieweroctree.h       | 2 +-
 indra/newview/llviewerregion.cpp     | 7 +------
 indra/newview/llvocache.cpp          | 8 ++++----
 indra/newview/llvocache.h            | 2 +-
 indra/newview/pipeline.cpp           | 7 ++++++-
 7 files changed, 15 insertions(+), 15 deletions(-)

diff --git a/indra/newview/llspatialpartition.cpp b/indra/newview/llspatialpartition.cpp
index a5045e6a4c0..a1b7bfe72a0 100755
--- a/indra/newview/llspatialpartition.cpp
+++ b/indra/newview/llspatialpartition.cpp
@@ -1476,7 +1476,7 @@ S32 LLSpatialPartition::cull(LLCamera &camera, std::vector<LLDrawable *>* result
 	return 0;
 	}
 	
-S32 LLSpatialPartition::cull(LLCamera &camera)
+S32 LLSpatialPartition::cull(LLCamera &camera, bool do_occlusion)
 {
 #if LL_OCTREE_PARANOIA_CHECK
 	((LLSpatialGroup*)mOctree->getListener(0))->checkStates();
diff --git a/indra/newview/llspatialpartition.h b/indra/newview/llspatialpartition.h
index 6202fb11cbf..1774baf0fe8 100755
--- a/indra/newview/llspatialpartition.h
+++ b/indra/newview/llspatialpartition.h
@@ -422,7 +422,7 @@ class LLSpatialPartition: public LLViewerOctreePartition, public LLGeometryManag
 	virtual void rebuildMesh(LLSpatialGroup* group);
 
 	BOOL visibleObjectsInFrustum(LLCamera& camera);
-	/*virtual*/ S32 cull(LLCamera &camera); // Cull on arbitrary frustum
+	/*virtual*/ S32 cull(LLCamera &camera, bool do_occlusion=false); // Cull on arbitrary frustum
 	S32 cull(LLCamera &camera, std::vector<LLDrawable *>* results, BOOL for_select); // Cull on arbitrary frustum
 	
 	BOOL isVisible(const LLVector3& v);
diff --git a/indra/newview/llvieweroctree.h b/indra/newview/llvieweroctree.h
index 7c85231ce0d..7fdb5661d8d 100644
--- a/indra/newview/llvieweroctree.h
+++ b/indra/newview/llvieweroctree.h
@@ -357,7 +357,7 @@ class LLViewerOctreePartition
 	virtual ~LLViewerOctreePartition();
 
 	// Cull on arbitrary frustum
-	virtual S32 cull(LLCamera &camera) = 0;
+	virtual S32 cull(LLCamera &camera, bool do_occlusion) = 0;
 	BOOL isOcclusionEnabled();
 
 public:	
diff --git a/indra/newview/llviewerregion.cpp b/indra/newview/llviewerregion.cpp
index 49bb05d88e2..117ccdea336 100755
--- a/indra/newview/llviewerregion.cpp
+++ b/indra/newview/llviewerregion.cpp
@@ -1204,13 +1204,8 @@ BOOL LLViewerRegion::idleUpdate(F32 max_update_time)
 	{
 		throttle = -1; //cancel the throttling
 
-		S32 occlusion = LLPipeline::sUseOcclusion;
-		LLPipeline::sUseOcclusion = 0; //disable occlusion
-		
 		//apply octree cullings here to pick up visible objects because rendering pipeline stops view culling at this moment
-		mImpl->mVOCachePartition->cull(*LLViewerCamera::getInstance());
-		
-		LLPipeline::sUseOcclusion = occlusion;
+		mImpl->mVOCachePartition->cull(*LLViewerCamera::getInstance(), false);
 	}	
 	else if(throttle < 0) //just recoved from the login/teleport screen
 	{
diff --git a/indra/newview/llvocache.cpp b/indra/newview/llvocache.cpp
index 11f31fcb9a2..82485d7fdcf 100755
--- a/indra/newview/llvocache.cpp
+++ b/indra/newview/llvocache.cpp
@@ -495,7 +495,7 @@ class LLVOCacheOctreeCull : public LLViewerOctreeCull
 		  mPartition(part)
 	{
 		mLocalShift = shift;
-		mUseObjectCacheOcclusion = (use_object_cache_occlusion && LLPipeline::sUseOcclusion);
+		mUseObjectCacheOcclusion = use_object_cache_occlusion;
 	}
 
 	virtual bool earlyFail(LLviewerOctreeGroup* base_group)
@@ -586,10 +586,10 @@ class LLVOCacheOctreeCull : public LLViewerOctreeCull
 	bool                mUseObjectCacheOcclusion;
 };
 
-S32 LLVOCachePartition::cull(LLCamera &camera)
+S32 LLVOCachePartition::cull(LLCamera &camera, bool do_occlusion)
 {
 	static LLCachedControl<bool> use_object_cache_occlusion(gSavedSettings,"UseObjectCacheOcclusion");
-
+	
 	if(!LLViewerRegion::sVOCacheCullingEnabled)
 	{
 		return 0;
@@ -601,7 +601,7 @@ S32 LLVOCachePartition::cull(LLCamera &camera)
 	LLVector3 region_agent = mRegionp->getOriginAgent();
 	camera.calcRegionFrustumPlanes(region_agent);
 
-	LLVOCacheOctreeCull culler(&camera, mRegionp, region_agent, use_object_cache_occlusion, this);
+	LLVOCacheOctreeCull culler(&camera, mRegionp, region_agent, do_occlusion && use_object_cache_occlusion, this);
 	culler.traverse(mOctree);
 
 	return 0;
diff --git a/indra/newview/llvocache.h b/indra/newview/llvocache.h
index ef9edf991bc..04463088fa5 100755
--- a/indra/newview/llvocache.h
+++ b/indra/newview/llvocache.h
@@ -159,7 +159,7 @@ class LLVOCachePartition : public LLViewerOctreePartition, public LLTrace::MemTr
 
 	void addEntry(LLViewerOctreeEntry* entry);
 	void removeEntry(LLViewerOctreeEntry* entry);
-	/*virtual*/ S32 cull(LLCamera &camera);
+	/*virtual*/ S32 cull(LLCamera &camera, bool do_occlusion);
 	void addOccluders(LLviewerOctreeGroup* gp);
 	void resetOccluders();
 
diff --git a/indra/newview/pipeline.cpp b/indra/newview/pipeline.cpp
index 1696f1962c3..1abaaa49ac0 100755
--- a/indra/newview/pipeline.cpp
+++ b/indra/newview/pipeline.cpp
@@ -2408,6 +2408,11 @@ static LLFastTimer::DeclareTimer FTM_CULL("Object Culling");
 
 void LLPipeline::updateCull(LLCamera& camera, LLCullResult& result, S32 water_clip, LLPlane* planep)
 {
+	static LLCachedControl<bool> use_occlusion(gSavedSettings,"UseOcclusion");
+	static bool can_use_occlusion = LLGLSLShader::sNoFixedFunction
+									&& LLFeatureManager::getInstance()->isFeatureAvailable("UseOcclusion") 
+									&& gGLManager.mHasOcclusionQuery;
+
 	LLFastTimer t(FTM_CULL);
 
 	grabReferences(result);
@@ -2530,7 +2535,7 @@ void LLPipeline::updateCull(LLCamera& camera, LLCullResult& result, S32 water_cl
 		LLVOCachePartition* vo_part = region->getVOCachePartition();
 		if(vo_part)
 		{
-			vo_part->cull(camera);
+			vo_part->cull(camera, can_use_occlusion && use_occlusion && !gUseWireframe);
 		}
 	}
 
-- 
GitLab