Rectangle texture addressing fix

Texture rectangle coordinates were clamped in the [0, dim - 1] range,
but should have been sampled in the [0.5, dim - 0.5 range] according
to the spec (a related comment was added in the code).

Also, by having getAddressingModeW() return ADDRESSING_LAYER for
rectangle textures, the 3rd texture coordinate computation will be
skipped entirely, preventing the temporary variable 'fv' from being
overwritten.

Change-Id: I4bbc30b2a2b747eae2f2a1dfb710a986bff7849b
Reviewed-on: https://swiftshader-review.googlesource.com/18768
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Nicolas Capens <nicolascapens@google.com>
diff --git a/src/Renderer/Sampler.cpp b/src/Renderer/Sampler.cpp
index 85448b9..979079c 100644
--- a/src/Renderer/Sampler.cpp
+++ b/src/Renderer/Sampler.cpp
@@ -478,7 +478,8 @@
 	{
 		if(textureType == TEXTURE_2D_ARRAY ||
 		   textureType == TEXTURE_2D ||
-		   textureType == TEXTURE_CUBE)
+		   textureType == TEXTURE_CUBE ||
+		   textureType == TEXTURE_RECTANGLE)
 		{
 			return ADDRESSING_LAYER;
 		}
diff --git a/src/Shader/SamplerCore.cpp b/src/Shader/SamplerCore.cpp
index 8fa378f..6ede6f1 100644
--- a/src/Shader/SamplerCore.cpp
+++ b/src/Shader/SamplerCore.cpp
@@ -2370,7 +2370,12 @@
 
 			if(state.textureType == TEXTURE_RECTANGLE)
 			{
-				coord = Min(Max(coord, Float4(0.0f)), Float4(dim - Int4(1)));
+				// According to https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_texture_rectangle.txt
+				// "CLAMP_TO_EDGE causes the s coordinate to be clamped to the range[0.5, wt - 0.5].
+				//  CLAMP_TO_EDGE causes the t coordinate to be clamped to the range[0.5, ht - 0.5]."
+				// Unless SwiftShader implements support for ADDRESSING_BORDER, other modes should be equivalent
+				// to CLAMP_TO_EDGE. Rectangle textures have no support for any MIRROR or REPEAT modes.
+				coord = Min(Max(coord, Float4(0.5f)), Float4(dim) - Float4(0.5f));
 			}
 			else
 			{