Fix textureSize for non-uniform samplers.

GLSL textureSize() calls that use a sampler that is not a uniform,
should not use the index of the register as the texture unit index.
Instead the index is the value stored in the register itself.

Bug chromium:904265

Change-Id: Iee1058e789daae15248ae5ffa940a0155dca61b5
Reviewed-on: https://swiftshader-review.googlesource.com/c/22910
Tested-by: Nicolas Capens <nicolascapens@google.com>
Reviewed-by: Alexis Hétu <sugoi@google.com>
diff --git a/src/Pipeline/PixelProgram.cpp b/src/Pipeline/PixelProgram.cpp
index df99196..5b5df45 100644
--- a/src/Pipeline/PixelProgram.cpp
+++ b/src/Pipeline/PixelProgram.cpp
@@ -1254,7 +1254,10 @@
 
 	void PixelProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
-		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + src1.index * sizeof(Texture);
+		bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
+		Int index = uniformSampler ? src1.index : As<Int>(Float(fetchRegister(src1).x.x));
+		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + index * sizeof(Texture);
+
 		dst = SamplerCore::textureSize(texture, lod);
 	}
 
diff --git a/src/Pipeline/VertexProgram.cpp b/src/Pipeline/VertexProgram.cpp
index 816af77..3ad2156 100644
--- a/src/Pipeline/VertexProgram.cpp
+++ b/src/Pipeline/VertexProgram.cpp
@@ -1524,7 +1524,10 @@
 
 	void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
-		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + src1.index * sizeof(Texture);
+		bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
+		Int index = uniformSampler ? src1.index : As<Int>(Float(fetchRegister(src1).x.x));
+		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + index * sizeof(Texture);
+
 		dst = SamplerCore::textureSize(texture, lod);
 	}
 
diff --git a/src/Shader/PixelProgram.cpp b/src/Shader/PixelProgram.cpp
index 7dff00b..06ece40 100644
--- a/src/Shader/PixelProgram.cpp
+++ b/src/Shader/PixelProgram.cpp
@@ -1279,7 +1279,10 @@
 
 	void PixelProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
-		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + src1.index * sizeof(Texture);
+		bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
+		Int index = uniformSampler ? src1.index : As<Int>(Float(fetchRegister(src1).x.x));
+		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap) + index * sizeof(Texture);
+
 		dst = SamplerCore::textureSize(texture, lod);
 	}
 
diff --git a/src/Shader/VertexProgram.cpp b/src/Shader/VertexProgram.cpp
index 55cc8c8..a74a37f 100644
--- a/src/Shader/VertexProgram.cpp
+++ b/src/Shader/VertexProgram.cpp
@@ -1608,7 +1608,10 @@
 
 	void VertexProgram::TEXSIZE(Vector4f &dst, Float4 &lod, const Src &src1)
 	{
-		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + src1.index * sizeof(Texture);
+		bool uniformSampler = (src1.type == Shader::PARAMETER_SAMPLER && src1.rel.type == Shader::PARAMETER_VOID);
+		Int index = uniformSampler ? src1.index : As<Int>(Float(fetchRegister(src1).x.x));
+		Pointer<Byte> texture = data + OFFSET(DrawData, mipmap[TEXTURE_IMAGE_UNITS]) + index * sizeof(Texture);
+
 		dst = SamplerCore::textureSize(texture, lod);
 	}