gpu_tonemapper: Adjust sample points to maintain linearity

Adjust texture sample points to maintain linearity throughout the
sample range

CRs-Fixed: 1110654
Change-Id: I2c198c9f330a0b6001d2eda28c0355f2c9ecbde4
diff --git a/gpu_tonemapper/Tonemapper.cpp b/gpu_tonemapper/Tonemapper.cpp
index ec74b80..38b4fe2 100644
--- a/gpu_tonemapper/Tonemapper.cpp
+++ b/gpu_tonemapper/Tonemapper.cpp
@@ -33,6 +33,12 @@
   lutXformTexture = 0;
   programID = 0;
   eglImageWrapper = new EGLImageWrapper();
+
+  lutXformScaleOffset[0] = 1.0f;
+  lutXformScaleOffset[1] = 0.0f;
+
+  tonemapScaleOffset[0] = 1.0f;
+  tonemapScaleOffset[1] = 0.0f;
 }
 
 //-----------------------------------------------------------------------------
@@ -72,9 +78,17 @@
 
   // load the 3d lut
   tonemapper->tonemapTexture = engine_load3DTexture(colorMap, colorMapSize, 0);
+  tonemapper->tonemapScaleOffset[0] = ((float)(colorMapSize-1))/((float)(colorMapSize));
+  tonemapper->tonemapScaleOffset[1] = 1.0f/(2.0f*colorMapSize);
+
   // load the non-uniform xform
   tonemapper->lutXformTexture = engine_load1DTexture(lutXform, lutXformSize, 0);
   bool bUseXform = (tonemapper->lutXformTexture != 0) && (lutXformSize != 0);
+  if( bUseXform )
+  {
+      tonemapper->lutXformScaleOffset[0] = ((float)(lutXformSize-1))/((float)(lutXformSize));
+      tonemapper->lutXformScaleOffset[1] = 1.0f/(2.0f*lutXformSize);
+  }
 
   // create the program
   const char *fragmentShaders[3];
@@ -115,6 +129,13 @@
   // bind the program
   engine_setProgram(programID);
 
+  engine_setData2f(3, tonemapScaleOffset);
+  bool bUseXform = (lutXformTexture != 0);
+  if( bUseXform )
+  {
+    engine_setData2f(4, lutXformScaleOffset);
+  }
+
   // set destination
   engine_setDestination(dst_buffer->getFramebuffer(), 0, 0, dst_buffer->getWidth(),
                         dst_buffer->getHeight());
diff --git a/gpu_tonemapper/Tonemapper.h b/gpu_tonemapper/Tonemapper.h
index 1d6f808..88866d5 100644
--- a/gpu_tonemapper/Tonemapper.h
+++ b/gpu_tonemapper/Tonemapper.h
@@ -32,6 +32,8 @@
   unsigned int tonemapTexture;
   unsigned int lutXformTexture;
   unsigned int programID;
+  float lutXformScaleOffset[2];
+  float tonemapScaleOffset[2];
   EGLImageWrapper* eglImageWrapper;
   Tonemapper();
 
diff --git a/gpu_tonemapper/engine.h b/gpu_tonemapper/engine.h
index ca914b2..c07f13e 100644
--- a/gpu_tonemapper/engine.h
+++ b/gpu_tonemapper/engine.h
@@ -36,6 +36,7 @@
 void engine_set3DInputBuffer(int binding, unsigned int textureID);
 void engine_setExternalInputBuffer(int binding, unsigned int textureID);
 void engine_setDestination(int id, int x, int y, int w, int h);
+void engine_setData2f(int loc, float* data);
 
 int engine_blit(int);
 
diff --git a/gpu_tonemapper/forward_tonemap.inl b/gpu_tonemapper/forward_tonemap.inl
index db23cf1..0d89a9e 100644
--- a/gpu_tonemapper/forward_tonemap.inl
+++ b/gpu_tonemapper/forward_tonemap.inl
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -24,22 +24,32 @@
     "layout(binding = 0) uniform samplerExternalOES externalTexture;            \n"
     "layout(binding = 1) uniform sampler3D tonemapper;                          \n"
     "layout(binding = 2) uniform sampler2D xform;                               \n"
+    "layout(location = 3) uniform vec2 tSO;                                     \n"
+    "#ifdef USE_NONUNIFORM_SAMPLING                                             \n"
+    "layout(location = 4) uniform vec2 xSO;                                     \n"
+    "#endif                                                                     \n"
     "in vec2 uv;                                                                \n"
     "out vec4 fs_color;                                                         \n"
+    "                                                                           \n"
+    "vec3 ScaleOffset(in vec3 samplePt, in vec2 so)                             \n"
+    "{                                                                          \n"
+    "   vec3 adjPt = so.x * samplePt + so.y;                                    \n"
+    "   return adjPt;                                                           \n"
+    "}                                                                          \n"
+    "                                                                           \n"
     "void main()                                                                \n"
     "{                                                                          \n"
-    "vec2 flipped = uv;                                                         \n"
-    "flipped.y = 1.0 - flipped.y;                                               \n"
-    "flipped.x = flipped.x;                                                     \n"
+    "vec2 flipped = vec2(uv.x, 1.0f - uv.y);                                    \n"
     "vec4 rgb = texture(externalTexture, flipped);                              \n"
     "#ifdef USE_NONUNIFORM_SAMPLING                                             \n"
-    "float r = texture(xform, vec2(r, 0.0f)).r;                                 \n"
-    "float g = texture(xform, vec2(g, 0.0f)).g;                                 \n"
-    "float b = texture(xform, vec2(b, 0.0f)).b;                                 \n"
+    "vec3 adj = ScaleOffset(rgb.xyz, xSO);                                      \n"
+    "float r = texture(xform, vec2(adj.r, 0.5f)).r;                             \n"
+    "float g = texture(xform, vec2(adj.g, 0.5f)).g;                             \n"
+    "float b = texture(xform, vec2(adj.b, 0.5f)).b;                             \n"
     "#else                                                                      \n"
     "float r = rgb.r;                                                           \n"
     "float g = rgb.g;                                                           \n"
     "float b = rgb.b;                                                           \n"
     "#endif                                                                     \n"
-    "fs_color.rgb = texture(tonemapper, vec3(r, g, b)).rgb;                     \n"
+    "fs_color.rgb = texture(tonemapper, ScaleOffset(vec3(r, g, b), tSO)).rgb;   \n"
     "}                                                                          \n";
diff --git a/gpu_tonemapper/glengine.cpp b/gpu_tonemapper/glengine.cpp
index eda4e6b..7a970c5 100644
--- a/gpu_tonemapper/glengine.cpp
+++ b/gpu_tonemapper/glengine.cpp
@@ -121,6 +121,13 @@
 }
 
 //-----------------------------------------------------------------------------
+void engine_setData2f(int location, float* data)
+//-----------------------------------------------------------------------------
+{
+    GL(glUniform2f(location, data[0], data[1]));
+}
+
+//-----------------------------------------------------------------------------
 unsigned int engine_load3DTexture(void *colorMapData, int sz, int format)
 //-----------------------------------------------------------------------------
 {
diff --git a/gpu_tonemapper/rgba_inverse_tonemap.inl b/gpu_tonemapper/rgba_inverse_tonemap.inl
index 399e2f8..2865fbe 100644
--- a/gpu_tonemapper/rgba_inverse_tonemap.inl
+++ b/gpu_tonemapper/rgba_inverse_tonemap.inl
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2016, The Linux Foundation. All rights reserved.
+ * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
  * Not a Contribution.
  *
  * Copyright 2015 The Android Open Source Project
@@ -24,27 +24,37 @@
     "layout(binding = 0) uniform samplerExternalOES externalTexture;                                    \n"
     "layout(binding = 1) uniform sampler3D tonemapper;                                                  \n"
     "layout(binding = 2) uniform sampler2D xform;                                                       \n"
+    "layout(location = 3) uniform vec2 tSO;                                                             \n"
+    "#if defined(USE_NONUNIFORM_SAMPLING)                                                               \n"
+    "layout(location = 4) uniform vec2 xSO;                                                             \n"
+    "#endif                                                                                             \n"
     "in vec2 uv;                                                                                        \n"
     "out vec4 fs_color;                                                                                 \n"
+    "                                                                                                   \n"
+    "vec3 ScaleOffset(in vec3 samplePt, in vec2 so)                                                     \n"
+    "{                                                                                                  \n"
+    "   vec3 adjPt = so.x * samplePt + so.y;                                                            \n"
+    "   return adjPt;                                                                                   \n"
+    "}                                                                                                  \n"
+    "                                                                                                   \n"
     "void main()                                                                                        \n"
     "{                                                                                                  \n"
-    "vec2 flipped = uv;                                                                                 \n"
-    "flipped.y = 1.0 - flipped.y;                                                                       \n"
-    "flipped.x = flipped.x;                                                                             \n"
+    "vec2 flipped = vec2(uv.x, 1.0f - uv.y);                                                            \n"
     "vec4 rgb_premulalpha = texture(externalTexture, flipped);                                          \n"
     "fs_color = rgb_premulalpha;                                                                        \n"
     "if( rgb_premulalpha.a > 0.0 ) {                                                                    \n"
     "vec3 rgb = rgb_premulalpha.rgb/rgb_premulalpha.a;                                                  \n"
     "#if defined(USE_NONUNIFORM_SAMPLING)                                                               \n"
-    "float r = texture(xform, vec2(rgb.r, 0.0f)).r;                                                     \n"
-    "float g = texture(xform, vec2(rgb.g, 0.0f)).g;                                                     \n"
-    "float b = texture(xform, vec2(rgb.b, 0.0f)).b;                                                     \n"
+    "vec3 adj = ScaleOffset(rgb.xyz, xSO);                                                              \n"
+    "float r = texture(xform, vec2(adj.r, 0.5f)).r;                                                     \n"
+    "float g = texture(xform, vec2(adj.g, 0.5f)).g;                                                     \n"
+    "float b = texture(xform, vec2(adj.b, 0.5f)).b;                                                     \n"
     "#else                                                                                              \n"
     "float r = rgb.r;                                                                                   \n"
     "float g = rgb.g;                                                                                   \n"
     "float b = rgb.b;                                                                                   \n"
     "#endif                                                                                             \n"
-    "fs_color.rgb = texture(tonemapper, vec3(r, g, b)).rgb * rgb_premulalpha.a;                         \n"
+    "fs_color.rgb = texture(tonemapper, ScaleOffset(vec3(r, g, b), tSO)).rgb * rgb_premulalpha.a;       \n"
     "fs_color.a = rgb_premulalpha.a;                                                                    \n"
     "}                                                                                                  \n"
     "}                                                                                                  \n";