summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Arun <arun.demeure@imgtec.com> 2017-06-01 23:42:06 +0000
committer android-build-merger <android-build-merger@google.com> 2017-06-01 23:42:06 +0000
commit11e44273dcdf905b0cd82d4a75ff1fd3be5b9d21 (patch)
tree7282fc22b129b4f9b7dd8f0db5c9fa0cabc7a4e2
parent0956df6ed2dea408651a7a3e25e3a8ab2dc64ada (diff)
parent92ef0a59f84b339e1112e7365521a46b475ff24c (diff)
Merge "Optimised hwui rounded corners shader" am: d7287c96eb am: ed7bdd6549
am: 92ef0a59f8 Change-Id: I9f5e6ef057ba6049804226d65f2f2f06594efc69
-rw-r--r--libs/hwui/ProgramCache.cpp31
-rw-r--r--libs/hwui/renderstate/RenderState.cpp16
2 files changed, 29 insertions, 18 deletions
diff --git a/libs/hwui/ProgramCache.cpp b/libs/hwui/ProgramCache.cpp
index d0f0949d5e78..8cc0aa7b414c 100644
--- a/libs/hwui/ProgramCache.cpp
+++ b/libs/hwui/ProgramCache.cpp
@@ -58,7 +58,9 @@ const char* gVS_Header_Uniforms_HasBitmap =
"uniform mat4 textureTransform;\n"
"uniform mediump vec2 textureDimension;\n";
const char* gVS_Header_Uniforms_HasRoundRectClip =
- "uniform mat4 roundRectInvTransform;\n";
+ "uniform mat4 roundRectInvTransform;\n"
+ "uniform mediump vec4 roundRectInnerRectLTWH;\n"
+ "uniform mediump float roundRectRadius;\n";
const char* gVS_Header_Varyings_HasTexture =
"varying vec2 outTexCoords;\n";
const char* gVS_Header_Varyings_HasColors =
@@ -81,7 +83,7 @@ const char* gVS_Header_Varyings_HasGradient[6] = {
"varying highp vec2 sweep;\n",
};
const char* gVS_Header_Varyings_HasRoundRectClip =
- "varying highp vec2 roundRectPos;\n";
+ "varying mediump vec2 roundRectPos;\n";
const char* gVS_Main =
"\nvoid main(void) {\n";
const char* gVS_Main_OutTexCoords =
@@ -113,7 +115,7 @@ const char* gVS_Main_VertexAlpha =
" alpha = vtxAlpha;\n";
const char* gVS_Main_HasRoundRectClip =
- " roundRectPos = (roundRectInvTransform * transformedPosition).xy;\n";
+ " roundRectPos = ((roundRectInvTransform * transformedPosition).xy / roundRectRadius) - roundRectInnerRectLTWH.xy;\n";
const char* gVS_Footer =
"}\n\n";
@@ -158,8 +160,8 @@ const char* gFS_Uniforms_ColorOp[3] = {
};
const char* gFS_Uniforms_HasRoundRectClip =
- "uniform vec4 roundRectInnerRectLTRB;\n"
- "uniform float roundRectRadius;\n";
+ "uniform mediump vec4 roundRectInnerRectLTWH;\n"
+ "uniform mediump float roundRectRadius;\n";
const char* gFS_Uniforms_ColorSpaceConversion =
// TODO: Should we use a 3D LUT to combine the matrix and transfer functions?
@@ -431,15 +433,18 @@ const char* gFS_Main_ApplyColorOp[3] = {
" fragColor = blendColors(colorBlend, fragColor);\n"
};
-// Note: LTRB -> xyzw
+// Note: LTWH (left top width height) -> xyzw
+// roundRectPos is now divided by roundRectRadius in vertex shader
+// after we also subtract roundRectInnerRectLTWH.xy from roundRectPos
const char* gFS_Main_FragColor_HasRoundRectClip =
- " mediump vec2 fragToLT = roundRectInnerRectLTRB.xy - roundRectPos;\n"
- " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTRB.zw;\n"
-
- // divide + multiply by 128 to avoid falling out of range in length() function
- " mediump vec2 dist = max(max(fragToLT, fragFromRB), vec2(0.0, 0.0)) / 128.0;\n"
- " mediump float linearDist = roundRectRadius - (length(dist) * 128.0);\n"
- " gl_FragColor *= clamp(linearDist, 0.0, 1.0);\n";
+ " mediump vec2 fragToLT = -roundRectPos;\n"
+ " mediump vec2 fragFromRB = roundRectPos - roundRectInnerRectLTWH.zw;\n"
+
+ // since distance is divided by radius, it's in [0;1] so precision is not an issue
+ // this also lets us clamp(0.0, 1.0) instead of max() which is cheaper on GPUs
+ " mediump vec2 dist = clamp(max(fragToLT, fragFromRB), 0.0, 1.0);\n"
+ " mediump float linearDist = clamp(roundRectRadius - (length(dist) * roundRectRadius), 0.0, 1.0);\n"
+ " gl_FragColor *= linearDist;\n";
const char* gFS_Main_DebugHighlight =
" gl_FragColor.rgb = vec3(0.0, gl_FragColor.a, 0.0);\n";
diff --git a/libs/hwui/renderstate/RenderState.cpp b/libs/hwui/renderstate/RenderState.cpp
index 481932dd7967..2c92924cc12c 100644
--- a/libs/hwui/renderstate/RenderState.cpp
+++ b/libs/hwui/renderstate/RenderState.cpp
@@ -296,14 +296,20 @@ void RenderState::render(const Glop& glop, const Matrix4& orthoMatrix) {
// TODO: avoid query, and cache values (or RRCS ptr) in program
const RoundRectClipState* state = glop.roundRectClipState;
const Rect& innerRect = state->innerRect;
- glUniform4f(fill.program->getUniform("roundRectInnerRectLTRB"),
- innerRect.left, innerRect.top,
- innerRect.right, innerRect.bottom);
- glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
- 1, GL_FALSE, &state->matrix.data[0]);
// add half pixel to round out integer rect space to cover pixel centers
float roundedOutRadius = state->radius + 0.5f;
+
+ // Divide by the radius to simplify the calculations in the fragment shader
+ // roundRectPos is also passed from vertex shader relative to top/left & radius
+ glUniform4f(fill.program->getUniform("roundRectInnerRectLTWH"),
+ innerRect.left / roundedOutRadius, innerRect.top / roundedOutRadius,
+ (innerRect.right - innerRect.left) / roundedOutRadius,
+ (innerRect.bottom - innerRect.top) / roundedOutRadius);
+
+ glUniformMatrix4fv(fill.program->getUniform("roundRectInvTransform"),
+ 1, GL_FALSE, &state->matrix.data[0]);
+
glUniform1f(fill.program->getUniform("roundRectRadius"),
roundedOutRadius);
}