Derive Rasterizer from Function<>.

Bug 22652760

Change-Id: I1a8ff978fdc7ad795e469218a931523d624e7cde
Reviewed-on: https://swiftshader-review.googlesource.com/4546
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/QuadRasterizer.cpp b/src/Renderer/QuadRasterizer.cpp
index 5ca2622..3fec795 100644
--- a/src/Renderer/QuadRasterizer.cpp
+++ b/src/Renderer/QuadRasterizer.cpp
@@ -25,18 +25,6 @@
 
 	extern int clusterCount;
 
-	QuadRasterizer::Registers::Registers()
-	{
-		occlusion = 0;
-
-#if PERF_PROFILE
-		for(int i = 0; i < PERF_TIMERS; i++)
-		{
-			cycles[i] = 0;
-		}
-#endif
-	}
-
 	QuadRasterizer::QuadRasterizer(const PixelProcessor::State &state, const PixelShader *pixelShader) : Rasterizer(state), shader(pixelShader)
 	{
 	}
@@ -47,69 +35,63 @@
 
 	void QuadRasterizer::generate()
 	{
-		Function<Void(Pointer<Byte>, Int, Int, Pointer<Byte>)> function;
+		#if PERF_PROFILE
+			for(int i = 0; i < PERF_TIMERS; i++)
+			{
+				cycles[i] = 0;
+			}
+
+			Long pixelTime = Ticks();
+		#endif
+
+		primitive = Arg<0>();
+		Int count = Arg<1>();
+		Int cluster = Arg<2>();
+		data = Arg<3>();
+
+		constants = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,constants));
+		occlusion = 0;
+
+		Do
 		{
-			#if PERF_PROFILE
-				Long pixelTime = Ticks();
-			#endif
+			Int yMin = *Pointer<Int>(primitive + OFFSET(Primitive,yMin));
+			Int yMax = *Pointer<Int>(primitive + OFFSET(Primitive,yMax));
 
-			Pointer<Byte> primitive(function.Arg<0>());
-			Int count(function.Arg<1>());
-			Int cluster(function.Arg<2>());
-			Pointer<Byte> data(function.Arg<3>());
+			Int cluster2 = cluster + cluster;
+			yMin += clusterCount * 2 - 2 - cluster2;
+			yMin &= -clusterCount * 2;
+			yMin += cluster2;
 
-			Registers& r = *createRegisters(shader);
-			r.constants = *Pointer<Pointer<Byte> >(data + OFFSET(DrawData,constants));
-			r.cluster = cluster;
-			r.data = data;
-
-			Do
+			If(yMin < yMax)
 			{
-				r.primitive = primitive;
-
-				Int yMin = *Pointer<Int>(primitive + OFFSET(Primitive,yMin));
-				Int yMax = *Pointer<Int>(primitive + OFFSET(Primitive,yMax));
-
-				Int cluster2 = r.cluster + r.cluster;
-				yMin += clusterCount * 2 - 2 - cluster2;
-				yMin &= -clusterCount * 2;
-				yMin += cluster2;
-
-				If(yMin < yMax)
-				{
-					rasterize(r, yMin, yMax);
-				}
-
-				primitive += sizeof(Primitive) * state.multiSample;
-				count--;
-			}
-			Until(count == 0)
-
-			if(state.occlusionEnabled)
-			{
-				UInt clusterOcclusion = *Pointer<UInt>(data + OFFSET(DrawData,occlusion) + 4 * cluster);
-				clusterOcclusion += r.occlusion;
-				*Pointer<UInt>(data + OFFSET(DrawData,occlusion) + 4 * cluster) = clusterOcclusion;
+				rasterize(yMin, yMax);
 			}
 
-			#if PERF_PROFILE
-				r.cycles[PERF_PIXEL] = Ticks() - pixelTime;
+			primitive += sizeof(Primitive) * state.multiSample;
+			count--;
+		}
+		Until(count == 0)
 
-				for(int i = 0; i < PERF_TIMERS; i++)
-				{
-					*Pointer<Long>(data + OFFSET(DrawData,cycles[i]) + 8 * cluster) += r.cycles[i];
-				}
-			#endif
-
-			Return();
-
-			delete &r;
+		if(state.occlusionEnabled)
+		{
+			UInt clusterOcclusion = *Pointer<UInt>(data + OFFSET(DrawData,occlusion) + 4 * cluster);
+			clusterOcclusion += occlusion;
+			*Pointer<UInt>(data + OFFSET(DrawData,occlusion) + 4 * cluster) = clusterOcclusion;
 		}
 
-		routine = function(L"PixelRoutine_%0.8X", state.shaderID);
+		#if PERF_PROFILE
+			cycles[PERF_PIXEL] = Ticks() - pixelTime;
+
+			for(int i = 0; i < PERF_TIMERS; i++)
+			{
+				*Pointer<Long>(data + OFFSET(DrawData,cycles[i]) + 8 * cluster) += cycles[i];
+			}
+		#endif
+
+		Return();
 	}
 
-	void QuadRasterizer::rasterize(Registers &r, Int &yMin, Int &yMax)
+	void QuadRasterizer::rasterize(Int &yMin, Int &yMax)
 	{
 		Pointer<Byte> cBuffer[RENDERTARGETS];
 		Pointer<Byte> zBuffer;
@@ -119,49 +101,49 @@
 		{
 			if(state.colorWriteActive(index))
 			{
-				cBuffer[index] = *Pointer<Pointer<Byte> >(r.data + OFFSET(DrawData,colorBuffer[index])) + yMin * *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index]));
+				cBuffer[index] = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,colorBuffer[index])) + yMin * *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index]));
 			}
 		}
 
 		if(state.depthTestActive)
 		{
-			zBuffer = *Pointer<Pointer<Byte> >(r.data + OFFSET(DrawData,depthBuffer)) + yMin * *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
+			zBuffer = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,depthBuffer)) + yMin * *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
 		}
 
 		if(state.stencilActive)
 		{
-			sBuffer = *Pointer<Pointer<Byte> >(r.data + OFFSET(DrawData,stencilBuffer)) + yMin * *Pointer<Int>(r.data + OFFSET(DrawData,stencilPitchB));
+			sBuffer = *Pointer<Pointer<Byte>>(data + OFFSET(DrawData,stencilBuffer)) + yMin * *Pointer<Int>(data + OFFSET(DrawData,stencilPitchB));
 		}
 
 		Int y = yMin;
 
 		Do
 		{
-			Int x0a = Int(*Pointer<Short>(r.primitive + OFFSET(Primitive,outline->left) + (y + 0) * sizeof(Primitive::Span)));
-			Int x0b = Int(*Pointer<Short>(r.primitive + OFFSET(Primitive,outline->left) + (y + 1) * sizeof(Primitive::Span)));
+			Int x0a = Int(*Pointer<Short>(primitive + OFFSET(Primitive,outline->left) + (y + 0) * sizeof(Primitive::Span)));
+			Int x0b = Int(*Pointer<Short>(primitive + OFFSET(Primitive,outline->left) + (y + 1) * sizeof(Primitive::Span)));
 			Int x0 = Min(x0a, x0b);
 
 			for(unsigned int q = 1; q < state.multiSample; q++)
 			{
-				x0a = Int(*Pointer<Short>(r.primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->left) + (y + 0) * sizeof(Primitive::Span)));
-				x0b = Int(*Pointer<Short>(r.primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->left) + (y + 1) * sizeof(Primitive::Span)));
+				x0a = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->left) + (y + 0) * sizeof(Primitive::Span)));
+				x0b = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->left) + (y + 1) * sizeof(Primitive::Span)));
 				x0 = Min(x0, Min(x0a, x0b));
 			}
 
 			x0 &= 0xFFFFFFFE;
 
-			Int x1a = Int(*Pointer<Short>(r.primitive + OFFSET(Primitive,outline->right) + (y + 0) * sizeof(Primitive::Span)));
-			Int x1b = Int(*Pointer<Short>(r.primitive + OFFSET(Primitive,outline->right) + (y + 1) * sizeof(Primitive::Span)));
+			Int x1a = Int(*Pointer<Short>(primitive + OFFSET(Primitive,outline->right) + (y + 0) * sizeof(Primitive::Span)));
+			Int x1b = Int(*Pointer<Short>(primitive + OFFSET(Primitive,outline->right) + (y + 1) * sizeof(Primitive::Span)));
 			Int x1 = Max(x1a, x1b);
 
 			for(unsigned int q = 1; q < state.multiSample; q++)
 			{
-				x1a = Int(*Pointer<Short>(r.primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->right) + (y + 0) * sizeof(Primitive::Span)));
-				x1b = Int(*Pointer<Short>(r.primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->right) + (y + 1) * sizeof(Primitive::Span)));
+				x1a = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->right) + (y + 0) * sizeof(Primitive::Span)));
+				x1b = Int(*Pointer<Short>(primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline->right) + (y + 1) * sizeof(Primitive::Span)));
 				x1 = Max(x1, Max(x1a, x1b));
 			}
 
-			Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,yQuad), 16);
+			Float4 yyyy = Float4(Float(y)) + *Pointer<Float4>(primitive + OFFSET(Primitive,yQuad), 16);
 
 			if(interpolateZ())
 			{
@@ -171,10 +153,10 @@
 
 					if(state.multiSample > 1)
 					{
-						y -= *Pointer<Float4>(r.constants + OFFSET(Constants,Y) + q * sizeof(float4));
+						y -= *Pointer<Float4>(constants + OFFSET(Constants,Y) + q * sizeof(float4));
 					}
 
-					r.Dz[q] = *Pointer<Float4>(r.primitive + OFFSET(Primitive,z.C), 16) + y * *Pointer<Float4>(r.primitive + OFFSET(Primitive,z.B), 16);
+					Dz[q] = *Pointer<Float4>(primitive + OFFSET(Primitive,z.C), 16) + y * *Pointer<Float4>(primitive + OFFSET(Primitive,z.B), 16);
 				}
 			}
 
@@ -182,7 +164,7 @@
 			{
 				if(!state.stencilActive && state.depthTestActive && (state.depthCompareMode == DEPTH_LESSEQUAL || state.depthCompareMode == DEPTH_LESS))   // FIXME: Both modes ok?
 				{
-					Float4 xxxx = Float4(Float(x0)) + *Pointer<Float4>(r.primitive + OFFSET(Primitive,xQuad), 16);
+					Float4 xxxx = Float4(Float(x0)) + *Pointer<Float4>(primitive + OFFSET(Primitive,xQuad), 16);
 
 					Pointer<Byte> buffer;
 					Int pitch;
@@ -190,7 +172,7 @@
 					if(!state.quadLayoutDepthBuffer)
 					{
 						buffer = zBuffer + 4 * x0;
-						pitch = *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB));
+						pitch = *Pointer<Int>(data + OFFSET(DrawData,depthPitchB));
 					}
 					else
 					{
@@ -199,7 +181,7 @@
 
 					For(Int x = x0, x < x1, x += 2)
 					{
-						Float4 z = interpolate(xxxx, r.Dz[0], z, r.primitive + OFFSET(Primitive,z), false, false);
+						Float4 z = interpolate(xxxx, Dz[0], z, primitive + OFFSET(Primitive,z), false, false);
 
 						Float4 zValue;
 
@@ -254,7 +236,7 @@
 			{
 				if(interpolateW())
 				{
-					r.Dw = *Pointer<Float4>(r.primitive + OFFSET(Primitive,w.C), 16) + yyyy * *Pointer<Float4>(r.primitive + OFFSET(Primitive,w.B), 16);
+					Dw = *Pointer<Float4>(primitive + OFFSET(Primitive,w.C), 16) + yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive,w.B), 16);
 				}
 
 				for(int interpolant = 0; interpolant < 10; interpolant++)
@@ -263,11 +245,11 @@
 					{
 						if(state.interpolant[interpolant].component & (1 << component))
 						{
-							r.Dv[interpolant][component] = *Pointer<Float4>(r.primitive + OFFSET(Primitive,V[interpolant][component].C), 16);
+							Dv[interpolant][component] = *Pointer<Float4>(primitive + OFFSET(Primitive,V[interpolant][component].C), 16);
 
 							if(!(state.interpolant[interpolant].flat & (1 << component)))
 							{
-								r.Dv[interpolant][component] += yyyy * *Pointer<Float4>(r.primitive + OFFSET(Primitive,V[interpolant][component].B), 16);
+								Dv[interpolant][component] += yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive,V[interpolant][component].B), 16);
 							}
 						}
 					}
@@ -275,11 +257,11 @@
 
 				if(state.fog.component)
 				{
-					r.Df = *Pointer<Float4>(r.primitive + OFFSET(Primitive,f.C), 16);
+					Df = *Pointer<Float4>(primitive + OFFSET(Primitive,f.C), 16);
 
 					if(!state.fog.flat)
 					{
-						r.Df += yyyy * *Pointer<Float4>(r.primitive + OFFSET(Primitive,f.B), 16);
+						Df += yyyy * *Pointer<Float4>(primitive + OFFSET(Primitive,f.B), 16);
 					}
 				}
 
@@ -288,7 +270,7 @@
 
 				for(unsigned int q = 0; q < state.multiSample; q++)
 				{
-					xLeft[q] = *Pointer<Short4>(r.primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline) + y * sizeof(Primitive::Span));
+					xLeft[q] = *Pointer<Short4>(primitive + q * sizeof(Primitive) + OFFSET(Primitive,outline) + y * sizeof(Primitive::Span));
 					xRight[q] = xLeft[q];
 
 					xLeft[q] = Swizzle(xLeft[q], 0xA0) - Short4(1, 2, 1, 2);
@@ -306,7 +288,7 @@
 						cMask[q] = SignMask(Pack(mask, mask)) & 0x0000000F;
 					}
 
-					quad(r, cBuffer, zBuffer, sBuffer, cMask, x, y);
+					quad(cBuffer, zBuffer, sBuffer, cMask, x, y);
 				}
 			}
 
@@ -314,18 +296,18 @@
 			{
 				if(state.colorWriteActive(index))
 				{
-					cBuffer[index] += *Pointer<Int>(r.data + OFFSET(DrawData,colorPitchB[index])) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
+					cBuffer[index] += *Pointer<Int>(data + OFFSET(DrawData,colorPitchB[index])) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
 				}
 			}
 
 			if(state.depthTestActive)
 			{
-				zBuffer += *Pointer<Int>(r.data + OFFSET(DrawData,depthPitchB)) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
+				zBuffer += *Pointer<Int>(data + OFFSET(DrawData,depthPitchB)) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
 			}
 
 			if(state.stencilActive)
 			{
-				sBuffer += *Pointer<Int>(r.data + OFFSET(DrawData,stencilPitchB)) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
+				sBuffer += *Pointer<Int>(data + OFFSET(DrawData,stencilPitchB)) << (1 + sw::log2(clusterCount));   // FIXME: Precompute
 			}
 
 			y += 2 * clusterCount;