Fix clearing all samples of multisample render targets.

Only libGLESv2 was clearing all the samples of a multisample buffer.
Since all known APIs always clear all the samples, this could be
handled in the Renderer.

Bug swiftshader:77

Change-Id: Ib9adc3c61d263420ed0a0ae4828a693bd360b076
Reviewed-on: https://swiftshader-review.googlesource.com/10788
Reviewed-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/D3D8/Direct3DDevice8.cpp b/src/D3D8/Direct3DDevice8.cpp
index 6294fbb..7f6e769 100644
--- a/src/D3D8/Direct3DDevice8.cpp
+++ b/src/D3D8/Direct3DDevice8.cpp
@@ -365,7 +365,7 @@
 
 		for(unsigned int i = 0; i < count; i++)
 		{
-			sw::SliceRect clearRect(rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2, 0);
+			sw::Rect clearRect(rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2);
 
 			clearRect.clip(viewport.X, viewport.Y, viewport.X + viewport.Width, viewport.Y + viewport.Height);
 
diff --git a/src/D3D9/Direct3DDevice9.cpp b/src/D3D9/Direct3DDevice9.cpp
index 9b68c47..4be7955 100644
--- a/src/D3D9/Direct3DDevice9.cpp
+++ b/src/D3D9/Direct3DDevice9.cpp
@@ -396,7 +396,7 @@
 
 		for(unsigned int i = 0; i < count; i++)
 		{
-			sw::SliceRect clearRect(rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2, 0);
+			sw::Rect clearRect(rects[i].x1, rects[i].y1, rects[i].x2, rects[i].y2);
 
 			clearRect.clip(viewport.X, viewport.Y, viewport.X + viewport.Width, viewport.Y + viewport.Height);
 
diff --git a/src/OpenGL/libGL/Device.cpp b/src/OpenGL/libGL/Device.cpp
index f75b060..736bb99 100644
--- a/src/OpenGL/libGL/Device.cpp
+++ b/src/OpenGL/libGL/Device.cpp
@@ -201,7 +201,7 @@
 			return;
 		}
 
-		sw::SliceRect clearRect = renderTarget->getRect();
+		sw::Rect clearRect = renderTarget->getRect();
 
 		if(scissorEnable)
 		{
@@ -225,7 +225,7 @@
 		}
 
 		z = clamp01(z);
-		sw::SliceRect clearRect = depthStencil->getRect();
+		sw::Rect clearRect = depthStencil->getRect();
 
 		if(scissorEnable)
 		{
@@ -242,7 +242,7 @@
 			return;
 		}
 
-		sw::SliceRect clearRect = depthStencil->getRect();
+		sw::Rect clearRect = depthStencil->getRect();
 
 		if(scissorEnable)
 		{
diff --git a/src/OpenGL/libGLES_CM/Device.cpp b/src/OpenGL/libGLES_CM/Device.cpp
index cb95d0c..26f53bc 100644
--- a/src/OpenGL/libGLES_CM/Device.cpp
+++ b/src/OpenGL/libGLES_CM/Device.cpp
@@ -172,7 +172,7 @@
 		rgba[2] = blue;
 		rgba[3] = alpha;
 
-		sw::SliceRect clearRect = renderTarget->getRect();
+		sw::Rect clearRect = renderTarget->getRect();
 
 		if(scissorEnable)
 		{
@@ -190,7 +190,7 @@
 		}
 
 		z = clamp01(z);
-		sw::SliceRect clearRect = depthBuffer->getRect();
+		sw::Rect clearRect = depthBuffer->getRect();
 
 		if(scissorEnable)
 		{
@@ -207,7 +207,7 @@
 			return;
 		}
 
-		sw::SliceRect clearRect = stencilBuffer->getRect();
+		sw::Rect clearRect = stencilBuffer->getRect();
 
 		if(scissorEnable)
 		{
diff --git a/src/OpenGL/libGLESv2/Context.cpp b/src/OpenGL/libGLESv2/Context.cpp
index c139068..c3f3b66 100644
--- a/src/OpenGL/libGLESv2/Context.cpp
+++ b/src/OpenGL/libGLESv2/Context.cpp
@@ -3329,7 +3329,7 @@
 
 		if(colorbuffer)
 		{
-			sw::SliceRect clearRect = colorbuffer->getRect();
+			sw::Rect clearRect = colorbuffer->getRect();
 
 			if(mState.scissorTestEnabled)
 			{
@@ -3368,7 +3368,7 @@
 		if(depthbuffer)
 		{
 			float depth = clamp01(value);
-			sw::SliceRect clearRect = depthbuffer->getRect();
+			sw::Rect clearRect = depthbuffer->getRect();
 
 			if(mState.scissorTestEnabled)
 			{
@@ -3392,7 +3392,7 @@
 		if(stencilbuffer)
 		{
 			unsigned char stencil = value < 0 ? 0 : static_cast<unsigned char>(value & 0x000000FF);
-			sw::SliceRect clearRect = stencilbuffer->getRect();
+			sw::Rect clearRect = stencilbuffer->getRect();
 
 			if(mState.scissorTestEnabled)
 			{
diff --git a/src/OpenGL/libGLESv2/Device.cpp b/src/OpenGL/libGLESv2/Device.cpp
index 8b8f016..53c794c 100644
--- a/src/OpenGL/libGLESv2/Device.cpp
+++ b/src/OpenGL/libGLESv2/Device.cpp
@@ -202,18 +202,14 @@
 		{
 			if(renderTarget[i])
 			{
-				sw::SliceRect clearRect = renderTarget[i]->getRect();
+				sw::Rect clearRect = renderTarget[i]->getRect();
 
 				if(scissorEnable)
 				{
 					clearRect.clip(scissorRect.x0, scissorRect.y0, scissorRect.x1, scissorRect.y1);
 				}
 
-				int depth = sw::max(renderTarget[i]->getDepth(), 1);
-				for(clearRect.slice = 0; clearRect.slice < depth; clearRect.slice++)
-				{
-					clear(rgba, FORMAT_A32B32G32R32F, renderTarget[i], clearRect, rgbaMask);
-				}
+				clear(rgba, FORMAT_A32B32G32R32F, renderTarget[i], clearRect, rgbaMask);
 			}
 		}
 	}
@@ -226,7 +222,7 @@
 		}
 
 		z = clamp01(z);
-		sw::SliceRect clearRect = depthBuffer->getRect();
+		sw::Rect clearRect = depthBuffer->getRect();
 
 		if(scissorEnable)
 		{
@@ -243,7 +239,7 @@
 			return;
 		}
 
-		sw::SliceRect clearRect = stencilBuffer->getRect();
+		sw::Rect clearRect = stencilBuffer->getRect();
 
 		if(scissorEnable)
 		{
diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index 7afbb9f..252d744 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -672,9 +672,15 @@
 		}
 	}
 
-	void Renderer::clear(void *pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask)
+	void Renderer::clear(void *value, Format format, Surface *dest, const Rect &clearRect, unsigned int rgbaMask)
 	{
-		blitter->clear(pixel, format, dest, dRect, rgbaMask);
+		SliceRect rect = clearRect;
+		int samples = dest->getDepth();
+
+		for(rect.slice = 0; rect.slice < samples; rect.slice++)
+		{
+			blitter->clear(value, format, dest, rect, rgbaMask);
+		}
 	}
 
 	void Renderer::blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter, bool isStencil)
diff --git a/src/Renderer/Renderer.hpp b/src/Renderer/Renderer.hpp
index dedd17a..d796475 100644
--- a/src/Renderer/Renderer.hpp
+++ b/src/Renderer/Renderer.hpp
@@ -326,7 +326,7 @@
 
 		void draw(DrawType drawType, unsigned int indexOffset, unsigned int count, bool update = true);
 
-		void clear(void* pixel, Format format, Surface *dest, const SliceRect &dRect, unsigned int rgbaMask);
+		void clear(void *value, Format format, Surface *dest, const Rect &rect, unsigned int rgbaMask);
 		void blit(Surface *source, const SliceRect &sRect, Surface *dest, const SliceRect &dRect, bool filter, bool isStencil = false);
 		void blit3D(Surface *source, Surface *dest);
 
diff --git a/src/Renderer/Surface.cpp b/src/Renderer/Surface.cpp
index 1a63ff9..6bcc657 100644
--- a/src/Renderer/Surface.cpp
+++ b/src/Renderer/Surface.cpp
@@ -3193,14 +3193,14 @@
 		resource->unlock();
 	}
 
-	bool Surface::isEntire(const SliceRect& rect) const
+	bool Surface::isEntire(const Rect& rect) const
 	{
 		return (rect.x0 == 0 && rect.y0 == 0 && rect.x1 == internal.width && rect.y1 == internal.height && internal.depth == 1);
 	}
 
-	SliceRect Surface::getRect() const
+	Rect Surface::getRect() const
 	{
-		return SliceRect(0, 0, internal.width, internal.height, 0);
+		return Rect(0, 0, internal.width, internal.height);
 	}
 
 	void Surface::clearDepth(float depth, int x0, int y0, int width, int height)
diff --git a/src/Renderer/Surface.hpp b/src/Renderer/Surface.hpp
index b54565e..6418c08 100644
--- a/src/Renderer/Surface.hpp
+++ b/src/Renderer/Surface.hpp
@@ -299,8 +299,8 @@
 		inline int getMultiSampleCount() const;
 		inline int getSuperSampleCount() const;
 
-		bool isEntire(const SliceRect& rect) const;
-		SliceRect getRect() const;
+		bool isEntire(const Rect& rect) const;
+		Rect getRect() const;
 		void clearDepth(float depth, int x0, int y0, int width, int height);
 		void clearStencil(unsigned char stencil, unsigned char mask, int x0, int y0, int width, int height);
 		void fill(const Color<float> &color, int x0, int y0, int width, int height);