Support sample image instruction operand

- Added support for the sample operand in SamplerCore, which simply
  involves offsetting the buffer by the the sampleId * samplePitch.
- Also added a check so that sampleId is within the expected range
  and doesn't cause reading memory out of bounds.

Bug: b/135265531
Change-Id: Ie828d07db41d36befb34037156736a6576af0676
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/38728
Tested-by: Alexis Hétu <sugoi@google.com>
Reviewed-by: Chris Forbes <chrisforbes@google.com>
Kokoro-Presubmit: kokoro <noreply+kokoro@google.com>
diff --git a/src/Pipeline/SpirvShaderSampling.cpp b/src/Pipeline/SpirvShaderSampling.cpp
index 6b5afa2..8f7fe8f 100644
--- a/src/Pipeline/SpirvShaderSampling.cpp
+++ b/src/Pipeline/SpirvShaderSampling.cpp
@@ -120,6 +120,7 @@
 		Vector4f dsx = {0, 0, 0, 0};
 		Vector4f dsy = {0, 0, 0, 0};
 		Vector4f offset = {0, 0, 0, 0};
+		SIMD::Int sampleId = 0;
 		SamplerFunction samplerFunction = instruction.getSamplerFunction();
 
 		uint32_t i = 0;
@@ -169,7 +170,10 @@
 			offset[j] = in[i];
 		}
 
-		// TODO(b/133868964): Handle 'Sample' operand.
+		if(instruction.sample)
+		{
+			sampleId = As<SIMD::Int>(in[i]);
+		}
 
 		SamplerCore s(constants, samplerState);
 
@@ -199,7 +203,7 @@
 					dPdy.y = Float(0.0f);
 				}
 
-				Vector4f sample = s.sampleTexture(texture, sampler, uvw[0], uvw[1], uvw[2], q, lod[i], dPdx, dPdy, offset, samplerFunction);
+				Vector4f sample = s.sampleTexture(texture, sampler, uvw[0], uvw[1], uvw[2], q, lod[i], dPdx, dPdy, offset, sampleId, samplerFunction);
 
 				Pointer<Float> rgba = out;
 				rgba[0 * SIMD::Width + i] = Pointer<Float>(&sample.x)[i];
@@ -210,7 +214,7 @@
 		}
 		else
 		{
-			Vector4f sample = s.sampleTexture(texture, sampler, uvw[0], uvw[1], uvw[2], q, lodOrBias.x, (dsx.x), (dsy.x), offset, samplerFunction);
+			Vector4f sample = s.sampleTexture(texture, sampler, uvw[0], uvw[1], uvw[2], q, lodOrBias.x, (dsx.x), (dsy.x), offset, sampleId, samplerFunction);
 
 			Pointer<SIMD::Float> rgba = out;
 			rgba[0] = sample.x;