Create SamplerCore on demand.

Previously we dynamically allocated an array of SamplerCores. This
isn't necessary and we can just create one as a temporary object where
used. This has the added advantage that we could have Reactor variables
as class members and keep them short-lived.

Change-Id: Ifb2e6edbf275aa793bd7880bd35384e16000007d
Reviewed-on: https://swiftshader-review.googlesource.com/13548
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Shader/PixelPipeline.cpp b/src/Shader/PixelPipeline.cpp
index de594e7..15a12c2 100644
--- a/src/Shader/PixelPipeline.cpp
+++ b/src/Shader/PixelPipeline.cpp
@@ -1238,7 +1238,7 @@
 
 		if(!project)
 		{
-			sampler[stage]->sampleTexture(texture, c, u, v, w, q, dsx, dsy);
+			SamplerCore(constants, state.sampler[stage]).sampleTexture(texture, c, u, v, w, q, dsx, dsy);
 		}
 		else
 		{
@@ -1248,7 +1248,7 @@
 			Float4 v_q = v * rq;
 			Float4 w_q = w * rq;
 
-			sampler[stage]->sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy);
+			SamplerCore(constants, state.sampler[stage]).sampleTexture(texture, c, u_q, v_q, w_q, q, dsx, dsy);
 		}
 
 		#if PERF_PROFILE
diff --git a/src/Shader/PixelProgram.cpp b/src/Shader/PixelProgram.cpp
index 6816238..f4ee8a9 100644
--- a/src/Shader/PixelProgram.cpp
+++ b/src/Shader/PixelProgram.cpp
@@ -716,7 +716,7 @@
 		#endif
 
 		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + samplerIndex * sizeof(Texture);
-		sampler[samplerIndex]->sampleTexture(texture, c, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
+		SamplerCore(constants, state.sampler[samplerIndex]).sampleTexture(texture, c, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
 
 		#if PERF_PROFILE
 			cycles[PERF_TEX] += Ticks() - texTime;
@@ -1169,7 +1169,7 @@
 	void PixelProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
 		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + src1.index * sizeof(Texture);
-		sampler[src1.index]->textureSize(texture, dst, lod);
+		SamplerCore::textureSize(texture, dst, lod);
 	}
 
 	void PixelProgram::TEXKILL(Int cMask[4], Vector4f &src, unsigned char mask)
diff --git a/src/Shader/PixelRoutine.cpp b/src/Shader/PixelRoutine.cpp
index 975d3be..7694bd2 100644
--- a/src/Shader/PixelRoutine.cpp
+++ b/src/Shader/PixelRoutine.cpp
@@ -45,10 +45,6 @@
 
 	PixelRoutine::~PixelRoutine()
 	{
-		for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
-		{
-			delete sampler[i];
-		}
 	}
 
 	void PixelRoutine::quad(Pointer<Byte> cBuffer[RENDERTARGETS], Pointer<Byte> &zBuffer, Pointer<Byte> &sBuffer, Int cMask[4], Int &x, Int &y)
@@ -57,11 +53,6 @@
 			Long pipeTime = Ticks();
 		#endif
 
-		for(int i = 0; i < TEXTURE_IMAGE_UNITS; i++)
-		{
-			sampler[i] = new SamplerCore(constants, state.sampler[i]);
-		}
-
 		const bool earlyDepthTest = !state.depthOverride && !state.alphaTestActive();
 
 		Int zMask[4];   // Depth mask
diff --git a/src/Shader/PixelRoutine.hpp b/src/Shader/PixelRoutine.hpp
index e9cee79..bc558c2 100644
--- a/src/Shader/PixelRoutine.hpp
+++ b/src/Shader/PixelRoutine.hpp
@@ -65,8 +65,6 @@
 		UShort4 convertFixed16(Float4 &cf, bool saturate = true);
 		void linearToSRGB12_16(Vector4s &c);
 
-		SamplerCore *sampler[TEXTURE_IMAGE_UNITS];
-
 	private:
 		Float4 interpolateCentroid(Float4 &x, Float4 &y, Float4 &rhw, Pointer<Byte> planeEquation, bool flat, bool perspective);
 		void stencilTest(Pointer<Byte> &sBuffer, int q, Int &x, Int &sMask, Int &cMask);
diff --git a/src/Shader/SamplerCore.hpp b/src/Shader/SamplerCore.hpp
index 9f8e85b..085b2d3 100644
--- a/src/Shader/SamplerCore.hpp
+++ b/src/Shader/SamplerCore.hpp
@@ -47,11 +47,11 @@
 	class SamplerCore
 	{
 	public:
-		SamplerCore(Pointer<Byte> &r, const Sampler::State &state);
+		SamplerCore(Pointer<Byte> &constants, const Sampler::State &state);
 
 		void sampleTexture(Pointer<Byte> &texture, Vector4s &c, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy);
 		void sampleTexture(Pointer<Byte> &texture, Vector4f &c, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
-		void textureSize(Pointer<Byte> &mipmap, Vector4f &size, Float4 &lod);
+		static void textureSize(Pointer<Byte> &mipmap, Vector4f &size, Float4 &lod);
 
 	private:
 		void sampleTexture(Pointer<Byte> &texture, Vector4s &c, Float4 &u, Float4 &v, Float4 &w, Float4 &q, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function, bool fixed12);
diff --git a/src/Shader/VertexProgram.cpp b/src/Shader/VertexProgram.cpp
index 5197363..9db65ed 100644
--- a/src/Shader/VertexProgram.cpp
+++ b/src/Shader/VertexProgram.cpp
@@ -58,19 +58,10 @@
 
 	VertexProgram::~VertexProgram()
 	{
-		for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
-		{
-			delete sampler[i];
-		}
 	}
 
 	void VertexProgram::pipeline(UInt& index)
 	{
-		for(int i = 0; i < VERTEX_TEXTURE_IMAGE_UNITS; i++)
-		{
-			sampler[i] = new SamplerCore(constants, state.sampler[i]);
-		}
-
 		if(!state.preTransformed)
 		{
 			program(index);
@@ -1610,7 +1601,7 @@
 	void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
 		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[16]) + src1.index * sizeof(Texture);
-		sampler[src1.index]->textureSize(texture, dst, lod);
+		SamplerCore::textureSize(texture, dst, lod);
 	}
 
 	void VertexProgram::sampleTexture(Vector4f &c, const Src &s, Vector4f &uvwq, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function)
@@ -1620,7 +1611,7 @@
 		if(s.type == Shader::PARAMETER_SAMPLER && s.rel.type == Shader::PARAMETER_VOID)
 		{
 			Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + s.index * sizeof(Texture);
-			sampler[s.index]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
+			SamplerCore(constants, state.sampler[s.index]).sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
 		}
 		else
 		{
@@ -1633,7 +1624,7 @@
 					If(index == i)
 					{
 						Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + i * sizeof(Texture);
-						sampler[i]->sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
+						SamplerCore(constants, state.sampler[i]).sampleTexture(texture, tmp, uvwq.x, uvwq.y, uvwq.z, uvwq.w, dsx, dsy, offset, function);
 						// FIXME: When the sampler states are the same, we could use one sampler and just index the texture
 					}
 				}
diff --git a/src/Shader/VertexProgram.hpp b/src/Shader/VertexProgram.hpp
index f1c776a..54c15ff 100644
--- a/src/Shader/VertexProgram.hpp
+++ b/src/Shader/VertexProgram.hpp
@@ -119,8 +119,6 @@
 
 		void sampleTexture(Vector4f &c, const Src &s, Vector4f &uvwq, Vector4f &dsx, Vector4f &dsy, Vector4f &offset, SamplerFunction function);
 
-		SamplerCore *sampler[VERTEX_TEXTURE_IMAGE_UNITS];
-
 		int ifDepth;
 		int loopRepDepth;
 		int breakDepth;