Adding functionality to stencils

Two minor features are required for the blitFramebuffer
implementation in OpenGL ES 3:
- lockStencil with x,y coordinates
- getting the stencil format (new getStencilFormat function)

Change-Id: I83ccccefe70dbbedd05b5d82bc75d30f689c4abe
Reviewed-on: https://swiftshader-review.googlesource.com/7490
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <capn@google.com>
diff --git a/src/D3D9/Direct3DDevice9.cpp b/src/D3D9/Direct3DDevice9.cpp
index 515ab4b..126b0df 100644
--- a/src/D3D9/Direct3DDevice9.cpp
+++ b/src/D3D9/Direct3DDevice9.cpp
@@ -6271,8 +6271,8 @@
 
 			if(source->hasStencil())
 			{
-				byte *sourceBuffer = (byte*)source->lockStencil(0, sw::PUBLIC);
-				byte *destBuffer = (byte*)dest->lockStencil(0, sw::PUBLIC);
+				byte *sourceBuffer = (byte*)source->lockStencil(0, 0, 0, sw::PUBLIC);
+				byte *destBuffer = (byte*)dest->lockStencil(0, 0, 0, sw::PUBLIC);
 
 				unsigned int width = source->getWidth();
 				unsigned int height = source->getHeight();
diff --git a/src/OpenGL/common/Image.cpp b/src/OpenGL/common/Image.cpp
index b7362d8..c734ba2 100644
--- a/src/OpenGL/common/Image.cpp
+++ b/src/OpenGL/common/Image.cpp
@@ -1627,7 +1627,7 @@
 	{
 		LoadImageData<D24>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
 
-		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
+		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, 0, 0, sw::PUBLIC));
 
 		if(stencil)
 		{
@@ -1641,7 +1641,7 @@
 	{
 		LoadImageData<D32F>(xoffset, yoffset, zoffset, width, height, depth, inputPitch, inputHeight, getPitch(), getHeight(), input, buffer);
 
-		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
+		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, 0, 0, sw::PUBLIC));
 
 		if(stencil)
 		{
diff --git a/src/OpenGL/libGL/Device.cpp b/src/OpenGL/libGL/Device.cpp
index 57c669f..86d01b9 100644
--- a/src/OpenGL/libGL/Device.cpp
+++ b/src/OpenGL/libGL/Device.cpp
@@ -570,8 +570,8 @@
 
 			if(source->hasStencil())
 			{
-				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, PUBLIC);
-				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, PUBLIC);
+				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, 0, 0, PUBLIC);
+				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, 0, 0, PUBLIC);
 
 				unsigned int width = source->getWidth();
 				unsigned int height = source->getHeight();
diff --git a/src/OpenGL/libGL/Image.cpp b/src/OpenGL/libGL/Image.cpp
index 49204f5..6a8c01d 100644
--- a/src/OpenGL/libGL/Image.cpp
+++ b/src/OpenGL/libGL/Image.cpp
@@ -672,7 +672,7 @@
 			}
 		}
 
-		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, sw::PUBLIC));
+		unsigned char *stencil = reinterpret_cast<unsigned char*>(lockStencil(0, 0, 0, sw::PUBLIC));
 
 		if(stencil)
 		{
diff --git a/src/OpenGL/libGLES_CM/Device.cpp b/src/OpenGL/libGLES_CM/Device.cpp
index 6587d36..b66c323 100644
--- a/src/OpenGL/libGLES_CM/Device.cpp
+++ b/src/OpenGL/libGLES_CM/Device.cpp
@@ -455,8 +455,8 @@
 
 			if(source->hasStencil())
 			{
-				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, PUBLIC);
-				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, PUBLIC);
+				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, 0, 0, PUBLIC);
+				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, 0, 0, PUBLIC);
 
 				unsigned int width = source->getWidth();
 				unsigned int height = source->getHeight();
diff --git a/src/OpenGL/libGLESv2/Device.cpp b/src/OpenGL/libGLESv2/Device.cpp
index 3307f45..225ceeb 100644
--- a/src/OpenGL/libGLESv2/Device.cpp
+++ b/src/OpenGL/libGLESv2/Device.cpp
@@ -621,8 +621,8 @@
 
 			if(source->hasStencil())
 			{
-				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, PUBLIC);
-				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, PUBLIC);
+				sw::byte *sourceBuffer = (sw::byte*)source->lockStencil(0, 0, 0, PUBLIC);
+				sw::byte *destBuffer = (sw::byte*)dest->lockStencil(0, 0, 0, PUBLIC);
 
 				copyBuffer(sourceBuffer, destBuffer, source->getWidth(), source->getHeight(), source->getInternalPitchB(), dest->getInternalPitchB(), egl::Image::bytes(source->getInternalFormat()), flipX, flipY);
 
diff --git a/src/Renderer/Renderer.cpp b/src/Renderer/Renderer.cpp
index fe6c7f9..0700a88 100644
--- a/src/Renderer/Renderer.cpp
+++ b/src/Renderer/Renderer.cpp
@@ -634,7 +634,7 @@
 
 				if(draw->stencilBuffer)
 				{
-					data->stencilBuffer = (unsigned char*)context->stencilBuffer->lockStencil(q * ms, MANAGED);
+					data->stencilBuffer = (unsigned char*)context->stencilBuffer->lockStencil(0, 0, q * ms, MANAGED);
 					data->stencilPitchB = context->stencilBuffer->getStencilPitchB();
 					data->stencilSliceB = context->stencilBuffer->getStencilSliceB();
 				}
diff --git a/src/Renderer/Surface.cpp b/src/Renderer/Surface.cpp
index 5ddea4a..d972574 100644
--- a/src/Renderer/Surface.cpp
+++ b/src/Renderer/Surface.cpp
@@ -1426,7 +1426,7 @@
 		internal.unlockRect();
 	}
 
-	void *Surface::lockStencil(int front, Accessor client)
+	void *Surface::lockStencil(int x, int y, int front, Accessor client)
 	{
 		resource->lock(client);
 
@@ -1435,7 +1435,7 @@
 			stencil.buffer = allocateBuffer(stencil.width, stencil.height, stencil.depth, stencil.format);
 		}
 
-		return stencil.lockRect(0, 0, front, LOCK_READWRITE);   // FIXME
+		return stencil.lockRect(x, y, front, LOCK_READWRITE);   // FIXME
 	}
 
 	void Surface::unlockStencil()
@@ -3258,7 +3258,7 @@
 		unsigned int fill = maskedS;
 		fill = fill | (fill << 8) | (fill << 16) + (fill << 24);
 
-		char *buffer = (char*)lockStencil(0, PUBLIC);
+		char *buffer = (char*)lockStencil(0, 0, 0, PUBLIC);
 
 		// Stencil buffers are assumed to use quad layout
 		for(int z = 0; z < stencil.depth; z++)
diff --git a/src/Renderer/Surface.hpp b/src/Renderer/Surface.hpp
index cd89d74..dd96ffd 100644
--- a/src/Renderer/Surface.hpp
+++ b/src/Renderer/Surface.hpp
@@ -283,8 +283,9 @@
 		inline int getInternalSliceB() const;
 		inline int getInternalSliceP() const;
 
-		void *lockStencil(int front, Accessor client);
+		void *lockStencil(int x, int y, int front, Accessor client);
 		void unlockStencil();
+		inline Format getStencilFormat() const;
 		inline int getStencilPitchB() const;
 		inline int getStencilSliceB() const;
 
@@ -573,6 +574,11 @@
 		return internal.sliceP;
 	}
 
+	Format Surface::getStencilFormat() const
+	{
+		return stencil.format;
+	}
+
 	int Surface::getStencilPitchB() const
 	{
 		return stencil.pitchB;