Zero out texel index after sample and layer offset

This ensures that if the sample or layer coordinate were not forced to
be in range but the 'valid' mask indicates whether they're in range,
we sample texel 0 instead of potentially outside the image memory.

Note this does not yet validate the sample coordinate.

Bug: b/162419571
Change-Id: I08561862cbf371c1109c8949cd90557110d4ee24
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/47228
Presubmit-Ready: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Chris Forbes <chrisforbes@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Kokoro-Result: kokoro <noreply+kokoro@google.com>
diff --git a/src/Pipeline/SamplerCore.cpp b/src/Pipeline/SamplerCore.cpp
index 1c9124b..5c5f881 100644
--- a/src/Pipeline/SamplerCore.cpp
+++ b/src/Pipeline/SamplerCore.cpp
@@ -1436,13 +1436,6 @@
 		indices += As<UInt4>(wwww);
 	}
 
-	if(borderModeActive())
-	{
-		// Texels out of range are still sampled before being replaced
-		// with the border color, so sample them at linear index 0.
-		indices &= As<UInt4>(valid);
-	}
-
 	if(function.sample)
 	{
 		indices += Min(As<UInt4>(sampleId), *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sampleMax), 16)) *
@@ -1454,6 +1447,13 @@
 		indices += As<UInt4>(cubeArrayId) * *Pointer<UInt4>(mipmap + OFFSET(Mipmap, sliceP)) * UInt4(6);
 	}
 
+	if(borderModeActive())
+	{
+		// Texels out of range are still sampled before being replaced
+		// with the border color, so sample them at linear index 0.
+		indices &= As<UInt4>(valid);
+	}
+
 	for(int i = 0; i < 4; i++)
 	{
 		index[i] = Extract(As<Int4>(indices), i);