blob: 146664ed6f9bfb255eefbe84b5e8d16231212728 [file] [log] [blame]
// Copyright 2020 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
precision mediump float;
uniform vec4 u_color;
varying vec4 v_rcorner;
varying vec2 v_texcoord;
// Return 0 if the given position is inside the rounded corner, or scale
// towards 1 as it goes outside a 1-pixel anti-aliasing border.
// |rcorner| is a vec4 representing (scaled.xy, 1 / radius.xy) with scaled.xy
// representing the offset of the current position in terms of radius.xy
// (i.e. offset.xy / radius.xy). The scaled.xy values can be negative if the
// current position is outside the corner start.
float IsOutsideRCorner(vec4 rcorner) {
// Estimate the distance to an implicit function using
// dist = f(x,y) / length(gradient(f(x,y)))
// For an ellipse, f(x,y) = x^2 / a^2 + y^2 / b^2 - 1.
highp vec2 scaled = max(rcorner.xy, 0.0);
highp float implicit = dot(scaled, scaled) - 1.0;
// NOTE: To accommodate large radius values using mediump floats, rcorner.zw
// was scaled by kRCornerGradientScale in the vertex attribute data.
// Multiply inv_gradient by kRCornerGradientScale to undo that scaling.
const highp float kRCornerGradientScale = 16.0;
highp vec2 gradient = 2.0 * scaled * rcorner.zw;
highp float inv_gradient = kRCornerGradientScale *
inversesqrt(max(dot(gradient, gradient), 0.0001));
return clamp(0.5 + implicit * inv_gradient, 0.0, 1.0);
}
uniform vec4 u_texcoord_clamp_y;
uniform vec4 u_texcoord_clamp_u;
uniform vec4 u_texcoord_clamp_v;
uniform sampler2D u_texture_y;
uniform sampler2D u_texture_u;
uniform sampler2D u_texture_v;
uniform mat4 u_color_transform_matrix;
#pragma array u_texcoord_clamp(u_texcoord_clamp_y, u_texcoord_clamp_u, u_texcoord_clamp_v);
#pragma array u_texture(u_texture_y, u_texture_u, u_texture_v);
vec4 GetRgba() {
float y = texture2D(u_texture_y,
clamp(v_texcoord, u_texcoord_clamp_y.xy, u_texcoord_clamp_y.zw)).a;
float u = texture2D(u_texture_u,
clamp(v_texcoord, u_texcoord_clamp_u.xy, u_texcoord_clamp_u.zw)).a;
float v = texture2D(u_texture_v,
clamp(v_texcoord, u_texcoord_clamp_v.xy, u_texcoord_clamp_v.zw)).a;
vec4 rgba = u_color_transform_matrix * vec4(y, u, v, 1);
return clamp(rgba, vec4(0, 0, 0, 0), vec4(1, 1, 1, 1));
}
void main() {
float scale = IsOutsideRCorner(v_rcorner);
gl_FragColor = u_color * (1.0 - scale) * GetRgba();
}