Refactor mipmap selection

This change avoids the use of multiple Reactor variables passed by
non-const reference. While they are output variables, this is not
obvious at the function call site.

Bug: b/197760889
Change-Id: Iac42dc86b32bd44f4fd82ebea5570268a3c2c2ef
Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/60928
Kokoro-Result: kokoro <noreply+kokoro@google.com>
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Pipeline/SamplerCore.cpp b/src/Pipeline/SamplerCore.cpp
index 35d2731..b3585d5 100644
--- a/src/Pipeline/SamplerCore.cpp
+++ b/src/Pipeline/SamplerCore.cpp
@@ -452,9 +452,8 @@
 	int componentCount = textureComponentCount();
 	bool gather = (state.textureFilter == FILTER_GATHER);
 
-	Pointer<Byte> mipmap;
-	Pointer<Byte> buffer;
-	selectMipmap(texture, mipmap, buffer, lod, secondLOD);
+	Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
+	Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
 
 	Short4 uuuu = address(u, state.addressingModeU, mipmap);
 	Short4 vvvv = address(v, state.addressingModeV, mipmap);
@@ -654,9 +653,8 @@
 
 	int componentCount = textureComponentCount();
 
-	Pointer<Byte> mipmap;
-	Pointer<Byte> buffer;
-	selectMipmap(texture, mipmap, buffer, lod, secondLOD);
+	Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
+	Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
 
 	Short4 uuuu = address(u_, state.addressingModeU, mipmap);
 	Short4 vvvv = address(v_, state.addressingModeV, mipmap);
@@ -898,9 +896,8 @@
 	int componentCount = textureComponentCount();
 	bool gather = (state.textureFilter == FILTER_GATHER);
 
-	Pointer<Byte> mipmap;
-	Pointer<Byte> buffer;
-	selectMipmap(texture, mipmap, buffer, lod, secondLOD);
+	Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
+	Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
 
 	Int4 x0, x1, y0, y1;
 	Float4 fu, fv;
@@ -991,9 +988,8 @@
 
 	int componentCount = textureComponentCount();
 
-	Pointer<Byte> mipmap;
-	Pointer<Byte> buffer;
-	selectMipmap(texture, mipmap, buffer, lod, secondLOD);
+	Pointer<Byte> mipmap = selectMipmap(texture, lod, secondLOD);
+	Pointer<Byte> buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
 
 	Int4 x0, x1, y0, y1, z0, z1;
 	Float4 fu, fv, fw;
@@ -2134,32 +2130,28 @@
 	return out;
 }
 
-void SamplerCore::selectMipmap(const Pointer<Byte> &texture, Pointer<Byte> &mipmap, Pointer<Byte> &buffer, const Float &lod, bool secondLOD)
+Pointer<Byte> SamplerCore::selectMipmap(const Pointer<Byte> &texture, const Float &lod, bool secondLOD)
 {
 	Pointer<Byte> mipmap0 = texture + OFFSET(Texture, mipmap[0]);
 
 	if(state.mipmapFilter == MIPMAP_NONE)
 	{
-		mipmap = mipmap0;
+		return mipmap0;
 	}
-	else
+
+	Int ilod;
+
+	if(state.mipmapFilter == MIPMAP_POINT)
 	{
-		Int ilod;
-
-		if(state.mipmapFilter == MIPMAP_POINT)
-		{
-			// TODO: Preferred formula is ceil(lod + 0.5) - 1
-			ilod = RoundInt(lod);
-		}
-		else  // MIPMAP_LINEAR
-		{
-			ilod = Int(lod);
-		}
-
-		mipmap = mipmap0 + ilod * sizeof(Mipmap) + secondLOD * sizeof(Mipmap);
+		// TODO: Preferred formula is ceil(lod + 0.5) - 1
+		ilod = RoundInt(lod);
+	}
+	else  // MIPMAP_LINEAR
+	{
+		ilod = Int(lod);
 	}
 
-	buffer = *Pointer<Pointer<Byte>>(mipmap + OFFSET(Mipmap, buffer));
+	return mipmap0 + ilod * sizeof(Mipmap) + secondLOD * sizeof(Mipmap);
 }
 
 Int4 SamplerCore::computeFilterOffset(Float &lod)
diff --git a/src/Pipeline/SamplerCore.hpp b/src/Pipeline/SamplerCore.hpp
index cda7ca2..542dca1 100644
--- a/src/Pipeline/SamplerCore.hpp
+++ b/src/Pipeline/SamplerCore.hpp
@@ -88,7 +88,7 @@
 	Vector4s sampleTexel(UInt index[4], Pointer<Byte> buffer);
 	Vector4f sampleTexel(Int4 &u, Int4 &v, Int4 &w, Float4 &dRef, const Int4 &sample, Pointer<Byte> &mipmap, Pointer<Byte> buffer, SamplerFunction function);
 	Vector4f replaceBorderTexel(const Vector4f &c, Int4 valid);
-	void selectMipmap(const Pointer<Byte> &texture, Pointer<Byte> &mipmap, Pointer<Byte> &buffer, const Float &lod, bool secondLOD);
+	Pointer<Byte> selectMipmap(const Pointer<Byte> &texture, const Float &lod, bool secondLOD);
 	Short4 address(const Float4 &uvw, AddressingMode addressingMode, Pointer<Byte> &mipmap);
 	Short4 computeLayerIndex(const Float4 &a, Pointer<Byte> &mipmap);
 	void address(const Float4 &uvw, Int4 &xyz0, Int4 &xyz1, Float4 &f, Pointer<Byte> &mipmap, Int4 &offset, Int4 &filter, int whd, AddressingMode addressingMode, SamplerFunction function);