Browse Source

Remove a spinlock from frustum. Better perf that way

Panagiotis Christopoulos Charitos 7 years ago
parent
commit
f300c5a6d1
2 changed files with 19 additions and 55 deletions
  1. 2 37
      src/anki/collision/Frustum.cpp
  2. 17 18
      src/anki/collision/Frustum.h

+ 2 - 37
src/anki/collision/Frustum.cpp

@@ -18,38 +18,31 @@ Frustum& Frustum::operator=(const Frustum& b)
 	m_planesL = b.m_planesL;
 	m_planesL = b.m_planesL;
 	m_planesW = b.m_planesW;
 	m_planesW = b.m_planesW;
 	m_trf = b.m_trf;
 	m_trf = b.m_trf;
-	m_frustumDirty = b.m_frustumDirty;
 	return *this;
 	return *this;
 }
 }
 
 
 void Frustum::accept(MutableVisitor& v)
 void Frustum::accept(MutableVisitor& v)
 {
 {
-	update();
 	CompoundShape::accept(v);
 	CompoundShape::accept(v);
 }
 }
 
 
 void Frustum::accept(ConstVisitor& v) const
 void Frustum::accept(ConstVisitor& v) const
 {
 {
-	update();
 	CompoundShape::accept(v);
 	CompoundShape::accept(v);
 }
 }
 
 
 F32 Frustum::testPlane(const Plane& p) const
 F32 Frustum::testPlane(const Plane& p) const
 {
 {
-	update();
 	return CompoundShape::testPlane(p);
 	return CompoundShape::testPlane(p);
 }
 }
 
 
 void Frustum::computeAabb(Aabb& aabb) const
 void Frustum::computeAabb(Aabb& aabb) const
 {
 {
-	update();
 	CompoundShape::computeAabb(aabb);
 	CompoundShape::computeAabb(aabb);
 }
 }
 
 
 Bool Frustum::insideFrustum(const CollisionShape& b) const
 Bool Frustum::insideFrustum(const CollisionShape& b) const
 {
 {
-	update();
-
 	for(const Plane& plane : m_planesW)
 	for(const Plane& plane : m_planesW)
 	{
 	{
 		if(b.testPlane(plane) < 0.0)
 		if(b.testPlane(plane) < 0.0)
@@ -70,39 +63,11 @@ void Frustum::transform(const Transform& trf)
 void Frustum::resetTransform(const Transform& trf)
 void Frustum::resetTransform(const Transform& trf)
 {
 {
 	m_trf = trf;
 	m_trf = trf;
-
-	if(m_frustumDirty)
-	{
-		// Update everything
-		updateInternal();
-	}
-	else
-	{
-		// Inform the child about the change
-		onTransform();
-
-		// Transform the planes
-		for(U i = 0; i < m_planesL.getSize(); ++i)
-		{
-			m_planesW[i] = m_planesL[i].getTransformed(m_trf);
-		}
-	}
-}
-
-void Frustum::update() const
-{
-	Frustum& self = *const_cast<Frustum*>(this);
-	LockGuard<SpinLock> lock(self.m_lock);
-	if(self.m_frustumDirty)
-	{
-		self.updateInternal();
-	}
+	update();
 }
 }
 
 
-void Frustum::updateInternal()
+void Frustum::update()
 {
 {
-	ANKI_ASSERT(m_frustumDirty);
-	m_frustumDirty = false;
 	recalculate();
 	recalculate();
 
 
 	// Transform derived
 	// Transform derived

+ 17 - 18
src/anki/collision/Frustum.h

@@ -60,20 +60,22 @@ public:
 	{
 	{
 		return m_near;
 		return m_near;
 	}
 	}
+
 	void setNear(const F32 x)
 	void setNear(const F32 x)
 	{
 	{
 		m_near = x;
 		m_near = x;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	F32 getFar() const
 	F32 getFar() const
 	{
 	{
 		return m_far;
 		return m_far;
 	}
 	}
+
 	void setFar(const F32 x)
 	void setFar(const F32 x)
 	{
 	{
 		m_far = x;
 		m_far = x;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	const Transform& getTransform() const
 	const Transform& getTransform() const
@@ -124,9 +126,6 @@ protected:
 	/// Keep the transformation.
 	/// Keep the transformation.
 	Transform m_trf = Transform::getIdentity();
 	Transform m_trf = Transform::getIdentity();
 
 
-	/// It's true when the frustum changed
-	Bool8 m_frustumDirty = true;
-
 	/// Called when a viewing variable changes. It recalculates the planes and the other variables.
 	/// Called when a viewing variable changes. It recalculates the planes and the other variables.
 	virtual void recalculate() = 0;
 	virtual void recalculate() = 0;
 
 
@@ -134,15 +133,13 @@ protected:
 	virtual void onTransform() = 0;
 	virtual void onTransform() = 0;
 
 
 	/// Update if dirty
 	/// Update if dirty
-	void update() const;
-	void updateInternal();
+	void update();
 
 
 	/// Copy
 	/// Copy
 	Frustum& operator=(const Frustum& b);
 	Frustum& operator=(const Frustum& b);
 
 
 private:
 private:
 	FrustumType m_type;
 	FrustumType m_type;
-	SpinLock m_lock;
 };
 };
 
 
 /// Frustum shape for perspective cameras
 /// Frustum shape for perspective cameras
@@ -174,7 +171,7 @@ public:
 	void setFovX(F32 ang)
 	void setFovX(F32 ang)
 	{
 	{
 		m_fovX = ang;
 		m_fovX = ang;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	/// Get FOV on Y axis.
 	/// Get FOV on Y axis.
@@ -185,7 +182,7 @@ public:
 	void setFovY(F32 ang)
 	void setFovY(F32 ang)
 	{
 	{
 		m_fovY = ang;
 		m_fovY = ang;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	/// Set all the parameters and recalculate the planes and shape
 	/// Set all the parameters and recalculate the planes and shape
@@ -199,12 +196,11 @@ public:
 		m_fovY = fovY;
 		m_fovY = fovY;
 		m_near = near;
 		m_near = near;
 		m_far = far;
 		m_far = far;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	const Array<Vec4, 5>& getPoints() const
 	const Array<Vec4, 5>& getPoints() const
 	{
 	{
-		update();
 		return m_pointsW;
 		return m_pointsW;
 	}
 	}
 
 
@@ -269,40 +265,44 @@ public:
 	{
 	{
 		return m_left;
 		return m_left;
 	}
 	}
+
 	void setLeft(F32 f)
 	void setLeft(F32 f)
 	{
 	{
 		m_left = f;
 		m_left = f;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	F32 getRight() const
 	F32 getRight() const
 	{
 	{
 		return m_right;
 		return m_right;
 	}
 	}
+
 	void setRight(F32 f)
 	void setRight(F32 f)
 	{
 	{
 		m_right = f;
 		m_right = f;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	F32 getTop() const
 	F32 getTop() const
 	{
 	{
 		return m_top;
 		return m_top;
 	}
 	}
+
 	void setTop(F32 f)
 	void setTop(F32 f)
 	{
 	{
 		m_top = f;
 		m_top = f;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	F32 getBottom() const
 	F32 getBottom() const
 	{
 	{
 		return m_bottom;
 		return m_bottom;
 	}
 	}
+
 	void setBottom(F32 f)
 	void setBottom(F32 f)
 	{
 	{
 		m_bottom = f;
 		m_bottom = f;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	/// Set all
 	/// Set all
@@ -315,13 +315,12 @@ public:
 		m_far = far;
 		m_far = far;
 		m_top = top;
 		m_top = top;
 		m_bottom = bottom;
 		m_bottom = bottom;
-		m_frustumDirty = true;
+		update();
 	}
 	}
 
 
 	/// Needed for debug drawing
 	/// Needed for debug drawing
 	const Obb& getObb() const
 	const Obb& getObb() const
 	{
 	{
-		update();
 		return m_obbW;
 		return m_obbW;
 	}
 	}