renderengine: avoid divide-by-zero in shaders
We have
max(1e-6, color.y)
to make sure the divisor is non-zero. But when mediump means
half-precision floats, 1e-6 is smaller than the minimum positive
normal number. The line might have no effect at all.
This is noticed when looking into the referenced bug.
Bug: 116864006
Test: manual
Change-Id: Ic39209e19ceeba6f51688dc9549746bf0f6a98e1
diff --git a/services/surfaceflinger/RenderEngine/gl/ProgramCache.cpp b/services/surfaceflinger/RenderEngine/gl/ProgramCache.cpp
index bc7d111..c118248 100644
--- a/services/surfaceflinger/RenderEngine/gl/ProgramCache.cpp
+++ b/services/surfaceflinger/RenderEngine/gl/ProgramCache.cpp
@@ -316,7 +316,7 @@
// scale [0.0, maxInLumi] to [0.0, maxOutLumi]
if (maxInLumi <= maxOutLumi) {
- nits *= maxOutLumi / maxInLumi;
+ return color * (maxOutLumi / maxInLumi);
} else {
// three control points
const float x0 = 10.0;
@@ -337,7 +337,7 @@
if (nits < x0) {
// scale [0.0, x0] to [0.0, y0] linearly
float slope = y0 / x0;
- nits *= slope;
+ return color * slope;
} else if (nits < x1) {
// scale [x0, x1] to [y0, y1] linearly
float slope = (y1 - y0) / (x1 - x0);
@@ -355,7 +355,8 @@
}
}
- return color * (nits / max(1e-6, color.y));
+ // color.y is greater than x0 and is thus non-zero
+ return color * (nits / color.y);
}
)__SHADER__";
break;
@@ -386,7 +387,7 @@
if (nits <= x0) {
// scale [0.0, x0] to [0.0, y0] linearly
const float slope = y0 / x0;
- nits *= slope;
+ return color * slope;
} else if (nits <= x1) {
// scale [x0, x1] to [y0, y1] using a curve
float t = (nits - x0) / (x1 - x0);
@@ -401,7 +402,8 @@
nits = (1.0 - t) * (1.0 - t) * y2 + 2.0 * (1.0 - t) * t * c3 + t * t * y3;
}
- return color * (nits / max(1e-6, color.y));
+ // color.y is greater than x0 and is thus non-zero
+ return color * (nits / color.y);
}
)__SHADER__";
break;