From d32bd63b67fc77e96589c88e389c2644f5dc6660 Mon Sep 17 00:00:00 2001 From: "Eunki, Hong" Date: Wed, 4 Sep 2024 17:06:51 +0900 Subject: [PATCH] Minor optimize render-effect shader Change-Id: Idfa87f93bddf48bba3adac84b96f9a6be984e634 Signed-off-by: Eunki, Hong --- .../internal/graphics/shaders/render-effect.frag | 42 +++++++++++++--------- .../internal/graphics/shaders/render-effect.vert | 16 ++++++--- 2 files changed, 37 insertions(+), 21 deletions(-) diff --git a/dali-toolkit/internal/graphics/shaders/render-effect.frag b/dali-toolkit/internal/graphics/shaders/render-effect.frag index 9ad5306..b8a06a8 100644 --- a/dali-toolkit/internal/graphics/shaders/render-effect.frag +++ b/dali-toolkit/internal/graphics/shaders/render-effect.frag @@ -1,7 +1,8 @@ precision highp float; -varying highp vec2 vFragCoord; varying highp vec2 vTexCoord; +varying highp vec2 vOptRectSize; varying highp vec4 vCornerRadius; + uniform highp vec3 uSize; uniform sampler2D sTexture; @@ -15,7 +16,7 @@ highp float nrand(const in vec2 uv) vec3 applyDithering( vec3 inColor ) { float rnd = nrand(vTexCoord) - 0.5; - inColor.rgb += rnd / 255.0; + inColor.rgb += rnd * 0.0039215686; return inColor; } @@ -31,20 +32,27 @@ float roundedBoxSDF(vec2 PixelPositionFromCenter, vec2 RectangleEdgePositionFrom void main() { gl_FragColor = texture2D(sTexture, vTexCoord); - - highp vec2 location = vTexCoord.xy - vec2(0.5); - float radius = - mix( - mix(vCornerRadius.x, vCornerRadius.y, sign(location.x)*0.5 + 0.5), - mix(vCornerRadius.w, vCornerRadius.z, sign(location.x)*0.5 + 0.5), - sign(location.y) * 0.5 + 0.5 - ); - - float edgeSoftness = min(1.0, radius); - float distance = roundedBoxSDF(vFragCoord.xy - (uSize.xy/2.0), uSize.xy/2.0, radius); - - float smoothedAlpha = 1.0 - smoothstep(-edgeSoftness, edgeSoftness, distance); - gl_FragColor.a *= smoothedAlpha; - gl_FragColor.rgb = applyDithering(gl_FragColor.rgb); + + highp vec2 location = (vTexCoord.xy - vec2(0.5)) * uSize.xy; + // skip most potential calculate for performance + if(abs(location.x) < vOptRectSize.x && abs(location.y) < vOptRectSize.y) + { + // Do nothing. + } + else + { + float radius = + mix( + mix(vCornerRadius.x, vCornerRadius.y, sign(location.x) * 0.5 + 0.5), + mix(vCornerRadius.w, vCornerRadius.z, sign(location.x) * 0.5 + 0.5), + sign(location.y) * 0.5 + 0.5 + ); + + float edgeSoftness = min(1.0, radius); + float distance = roundedBoxSDF(location, uSize.xy * 0.5, radius); + + float smoothedAlpha = 1.0 - smoothstep(-edgeSoftness, edgeSoftness, distance); + gl_FragColor.a *= smoothedAlpha; + } } diff --git a/dali-toolkit/internal/graphics/shaders/render-effect.vert b/dali-toolkit/internal/graphics/shaders/render-effect.vert index b8ae74d..c46d0dd 100644 --- a/dali-toolkit/internal/graphics/shaders/render-effect.vert +++ b/dali-toolkit/internal/graphics/shaders/render-effect.vert @@ -1,21 +1,29 @@ precision highp float; + attribute highp vec2 aPosition; -varying highp vec2 vFragCoord; + varying highp vec2 vTexCoord; +varying highp vec2 vOptRectSize; varying highp vec4 vCornerRadius; //output + uniform highp mat4 uMvpMatrix; -uniform highp vec3 uSize; uniform highp vec4 uCornerRadius; //input -uniform highp float uCornerRadiusPolicy; +uniform lowp float uCornerRadiusPolicy; +uniform highp vec3 uSize; void main() { highp vec4 vertexPosition = vec4(aPosition * uSize.xy, 0.0, 1.0); - vFragCoord = vertexPosition.xy + uSize.xy/2.0; vTexCoord = aPosition + vec2(0.5); gl_Position = uMvpMatrix * vertexPosition; highp float minSize = min(uSize.x, uSize.y); vCornerRadius = mix(uCornerRadius * minSize, uCornerRadius, uCornerRadiusPolicy); vCornerRadius = min(vCornerRadius, minSize * 0.5); + + vOptRectSize = uSize.xy * 0.5; + + // Optimize fragment shader. 0.2929 ~= 1.0 - sqrt(0.5) + highp float maxRadius = max(max(vCornerRadius.x, vCornerRadius.y), max(vCornerRadius.z, vCornerRadius.w)); + vOptRectSize -= 0.2929 * maxRadius + min(1.0, maxRadius); } -- 2.7.4