Refactor point and line clip flag calculation.

Move clip flag calculation for the new polygon vertices of points and lines
from the renderer to the clipper.

Change-Id: I41ac3647d8e9376586a1011d1cf28d83e9c963a2
Reviewed-on: https://swiftshader-review.googlesource.com/5423
Tested-by: Nicolas Capens <capn@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/Renderer/Clipper.cpp b/src/Renderer/Clipper.cpp
index d5aeaed..3256d68 100644
--- a/src/Renderer/Clipper.cpp
+++ b/src/Renderer/Clipper.cpp
@@ -28,30 +28,39 @@
 	{
 	}
 
+	unsigned int Clipper::computeClipFlags(const float4 &v)
+	{
+		return ((v.x > v.w)  ? CLIP_RIGHT  : 0) |
+		       ((v.y > v.w)  ? CLIP_TOP    : 0) |
+		       ((v.z > v.w)  ? CLIP_FAR    : 0) |
+		       ((v.x < -v.w) ? CLIP_LEFT   : 0) |
+		       ((v.y < -v.w) ? CLIP_BOTTOM : 0) |
+		       ((v.z < 0)    ? CLIP_NEAR   : 0) |
+		       Clipper::CLIP_FINITE;   // FIXME: xyz finite
+	}
+
 	bool Clipper::clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw)
 	{
-		DrawData &data = *draw.data;
-
-		polygon.b = 0;
-
-		if(clipFlagsOr & 0x0000003F)
+		if(clipFlagsOr & CLIP_FRUSTUM)
 		{
 			if(clipFlagsOr & CLIP_NEAR)   clipNear(polygon);
 			if(polygon.n >= 3) {
 			if(clipFlagsOr & CLIP_FAR)    clipFar(polygon);
 			if(polygon.n >= 3) {
-			if(clipFlagsOr & CLIP_LEFT)   clipLeft(polygon, data);
+			if(clipFlagsOr & CLIP_LEFT)   clipLeft(polygon);
 			if(polygon.n >= 3) {
-			if(clipFlagsOr & CLIP_RIGHT)  clipRight(polygon, data);
+			if(clipFlagsOr & CLIP_RIGHT)  clipRight(polygon);
 			if(polygon.n >= 3) {
-			if(clipFlagsOr & CLIP_TOP)    clipTop(polygon, data);
+			if(clipFlagsOr & CLIP_TOP)    clipTop(polygon);
 			if(polygon.n >= 3) {
-			if(clipFlagsOr & CLIP_BOTTOM) clipBottom(polygon, data);
+			if(clipFlagsOr & CLIP_BOTTOM) clipBottom(polygon);
 			}}}}}
 		}
 
-		if(clipFlagsOr & 0x00003F00)
+		if(clipFlagsOr & CLIP_USER)
 		{
+			DrawData &data = *draw.data;
+
 			if(polygon.n >= 3) {
 			if(draw.clipFlags & CLIP_PLANE0) clipPlane(polygon, data.clipPlane[0]);
 			if(polygon.n >= 3) {
@@ -72,8 +81,6 @@
 
 	void Clipper::clipNear(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -114,8 +121,6 @@
 
 	void Clipper::clipFar(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -154,10 +159,8 @@
 		polygon.i += 1;
 	}
 
-	void Clipper::clipLeft(Polygon &polygon, const DrawData &data)
+	void Clipper::clipLeft(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -196,10 +199,8 @@
 		polygon.i += 1;
 	}
 
-	void Clipper::clipRight(Polygon &polygon, const DrawData &data)
+	void Clipper::clipRight(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -238,10 +239,8 @@
 		polygon.i += 1;
 	}
 
-	void Clipper::clipTop(Polygon &polygon, const DrawData &data)
+	void Clipper::clipTop(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -280,10 +279,8 @@
 		polygon.i += 1;
 	}
 
-	void Clipper::clipBottom(Polygon &polygon, const DrawData &data)
+	void Clipper::clipBottom(Polygon &polygon)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
@@ -324,8 +321,6 @@
 
 	void Clipper::clipPlane(Polygon &polygon, const Plane &p)
 	{
-		if(polygon.n == 0) return;
-
 		const float4 **V = polygon.P[polygon.i];
 		const float4 **T = polygon.P[polygon.i + 1];
 
diff --git a/src/Renderer/Clipper.hpp b/src/Renderer/Clipper.hpp
index ad1af93e..9c7d110 100644
--- a/src/Renderer/Clipper.hpp
+++ b/src/Renderer/Clipper.hpp
@@ -29,6 +29,7 @@
 	public:
 		enum ClipFlags
 		{
+			// Indicates the vertex is outside the respective frustum plane
 			CLIP_RIGHT  = 1 << 0,
 			CLIP_TOP    = 1 << 1,
 			CLIP_FAR    = 1 << 2,
@@ -36,30 +37,35 @@
 			CLIP_BOTTOM = 1 << 4,
 			CLIP_NEAR   = 1 << 5,
 
-			CLIP_FINITE = 1 << 7,
+			CLIP_FRUSTUM = 0x003F,
+
+			CLIP_FINITE = 1 << 7,   // All position coordinates are finite
 
 			// User-defined clipping planes
-			CLIP_PLANE0	= 1 << 8,
-			CLIP_PLANE1	= 1 << 9,
-			CLIP_PLANE2	= 1 << 10,
-			CLIP_PLANE3	= 1 << 11,
-			CLIP_PLANE4	= 1 << 12,
-			CLIP_PLANE5	= 1 << 13
+			CLIP_PLANE0 = 1 << 8,
+			CLIP_PLANE1 = 1 << 9,
+			CLIP_PLANE2 = 1 << 10,
+			CLIP_PLANE3 = 1 << 11,
+			CLIP_PLANE4 = 1 << 12,
+			CLIP_PLANE5 = 1 << 13,
+
+			CLIP_USER = 0x3F00
 		};
 
 		Clipper();
 
 		~Clipper();
 
+		unsigned int computeClipFlags(const float4 &v);
 		bool clip(Polygon &polygon, int clipFlagsOr, const DrawCall &draw);
 
 	private:
 		void clipNear(Polygon &polygon);
 		void clipFar(Polygon &polygon);
-		void clipLeft(Polygon &polygon, const DrawData &data);
-		void clipRight(Polygon &polygon, const DrawData &data);
-		void clipTop(Polygon &polygon, const DrawData &data);
-		void clipBottom(Polygon &polygon, const DrawData &data);
+		void clipLeft(Polygon &polygon);
+		void clipRight(Polygon &polygon);
+		void clipTop(Polygon &polygon);
+		void clipBottom(Polygon &polygon);
 		void clipPlane(Polygon &polygon, const Plane &plane);
 
 		void clipEdge(float4 &Vo, const float4 &Vi, const float4 &Vj, float di, float dj) const;
diff --git a/src/Renderer/Polygon.hpp b/src/Renderer/Polygon.hpp
index 0b94132..8ee8562 100644
--- a/src/Renderer/Polygon.hpp
+++ b/src/Renderer/Polygon.hpp
@@ -29,6 +29,7 @@
 
 			n = 3;
 			i = 0;
+			b = 0;
 		}
 
 		Polygon(const float4 *P, int n)
@@ -40,14 +41,15 @@
 
 			this->n = n;
 			this->i = 0;
+			this->b = 0;
 		}
 
 		float4 B[16];              // Buffer for clipped vertices
 		const float4 *P[16][16];   // Pointers to clipped polygon's vertices
 
-		int i;
-		int b;
-		int n;
+		int n;   // Number of vertices
+		int i;   // Level of P to use
+		int b;   // Next available new vertex
 	};
 }
 
diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index 2123e08..983f4ec 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -1726,19 +1726,19 @@
 
 			P[0].x += -dy0w + -dx0w;
 			P[0].y += -dx0h + +dy0h;
-			C[0] = computeClipFlags(P[0], data);
+			C[0] = clipper->computeClipFlags(P[0]);
 
 			P[1].x += -dy1w + +dx1w;
 			P[1].y += -dx1h + +dy1h;
-			C[1] = computeClipFlags(P[1], data);
+			C[1] = clipper->computeClipFlags(P[1]);
 
 			P[2].x += +dy1w + +dx1w;
 			P[2].y += +dx1h + -dy1h;
-			C[2] = computeClipFlags(P[2], data);
+			C[2] = clipper->computeClipFlags(P[2]);
 
 			P[3].x += +dy0w + -dx0w;
 			P[3].y += +dx0h + +dy0h;
-			C[3] = computeClipFlags(P[3], data);
+			C[3] = clipper->computeClipFlags(P[3]);
 
 			if((C[0] & C[1] & C[2] & C[3]) == Clipper::CLIP_FINITE)
 			{
@@ -1778,28 +1778,28 @@
 			float dy1 = lineWidth * 0.5f * P1.w / H;
 
 			P[0].x += -dx0;
-			C[0] = computeClipFlags(P[0], data);
+			C[0] = clipper->computeClipFlags(P[0]);
 
 			P[1].y += +dy0;
-			C[1] = computeClipFlags(P[1], data);
+			C[1] = clipper->computeClipFlags(P[1]);
 
 			P[2].x += +dx0;
-			C[2] = computeClipFlags(P[2], data);
+			C[2] = clipper->computeClipFlags(P[2]);
 
 			P[3].y += -dy0;
-			C[3] = computeClipFlags(P[3], data);
+			C[3] = clipper->computeClipFlags(P[3]);
 
 			P[4].x += -dx1;
-			C[4] = computeClipFlags(P[4], data);
+			C[4] = clipper->computeClipFlags(P[4]);
 
 			P[5].y += +dy1;
-			C[5] = computeClipFlags(P[5], data);
+			C[5] = clipper->computeClipFlags(P[5]);
 
 			P[6].x += +dx1;
-			C[6] = computeClipFlags(P[6], data);
+			C[6] = clipper->computeClipFlags(P[6]);
 
 			P[7].y += -dy1;
-			C[7] = computeClipFlags(P[7], data);
+			C[7] = clipper->computeClipFlags(P[7]);
 
 			if((C[0] & C[1] & C[2] & C[3] & C[4] & C[5] & C[6] & C[7]) == Clipper::CLIP_FINITE)
 			{
@@ -1905,19 +1905,19 @@
 
 		P[0].x -= X;
 		P[0].y += Y;
-		C[0] = computeClipFlags(P[0], data);
+		C[0] = clipper->computeClipFlags(P[0]);
 
 		P[1].x += X;
 		P[1].y += Y;
-		C[1] = computeClipFlags(P[1], data);
+		C[1] = clipper->computeClipFlags(P[1]);
 
 		P[2].x += X;
 		P[2].y -= Y;
-		C[2] = computeClipFlags(P[2], data);
+		C[2] = clipper->computeClipFlags(P[2]);
 
 		P[3].x -= X;
 		P[3].y -= Y;
-		C[3] = computeClipFlags(P[3], data);
+		C[3] = clipper->computeClipFlags(P[3]);
 
 		triangle.v1 = triangle.v0;
 		triangle.v2 = triangle.v0;
@@ -1945,17 +1945,6 @@
 		return false;
 	}
 
-	unsigned int Renderer::computeClipFlags(const float4 &v, const DrawData &data)
-	{
-		return ((v.x > v.w)  << 0) |
-		       ((v.y > v.w)  << 1) |
-		       ((v.z > v.w)  << 2) |
-		       ((v.x < -v.w) << 3) |
-		       ((v.y < -v.w) << 4) |
-		       ((v.z < 0)    << 5) |
-		       Clipper::CLIP_FINITE;   // FIXME: xyz finite
-	}
-
 	void Renderer::initializeThreads()
 	{
 		unitCount = ceilPow2(threadCount);
diff --git a/src/Renderer/Renderer.hpp b/src/Renderer/Renderer.hpp
index 0d66211..74d755d 100644
--- a/src/Renderer/Renderer.hpp
+++ b/src/Renderer/Renderer.hpp
@@ -345,7 +345,7 @@
 		virtual void setSwizzleG(SamplerType type, int sampler, SwizzleType swizzleG);
 		virtual void setSwizzleB(SamplerType type, int sampler, SwizzleType swizzleB);
 		virtual void setSwizzleA(SamplerType type, int sampler, SwizzleType swizzleA);
-		
+
 		virtual void setPointSpriteEnable(bool pointSpriteEnable);
 		virtual void setPointScaleEnable(bool pointScaleEnable);
 		virtual void setLineWidth(float width);
@@ -416,7 +416,6 @@
 		bool isReadWriteTexture(int sampler);
 		void updateClipper();
 		void updateConfiguration(bool initialUpdate = false);
-		static unsigned int computeClipFlags(const float4 &v, const DrawData &data);
 		void initializeThreads();
 		void terminateThreads();