1
// Copyright © 2015 Canonical Ltd.
3
// This program is free software; you can redistribute it and/or modify
4
// it under the terms of the GNU Lesser General Public License as published by
5
// the Free Software Foundation; version 3.
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU Lesser General Public License for more details.
12
// You should have received a copy of the GNU Lesser General Public License
13
// along with this program. If not, see <http://www.gnu.org/licenses/>.
15
// Author: Loïc Molinari <loic.molinari@canonical.com>
17
// Static flow control (branching on a uniform value) is fast on most GPUs (including ultra-low
18
// power ones) because it allows to use the same shader execution path for an entire draw call. We
19
// rely on that technique here (also known as "uber-shader" solution) to avoid the complexity of
20
// dealing with a multiple shaders solution.
21
// FIXME(loicm) Validate GPU behavior with regards to static flow control.
23
uniform sampler2D shapeTexture;
24
uniform sampler2D sourceTexture;
25
uniform lowp float sourceOpacity;
26
uniform lowp float opacity;
27
uniform bool textured;
29
varying mediump vec2 shapeCoord;
30
varying mediump vec4 sourceCoord;
31
varying lowp vec4 backgroundColor;
32
varying mediump vec2 overlayCoord;
33
varying lowp vec4 overlayColor;
37
// Early texture fetch to cover latency as best as possible.
38
lowp vec4 shapeData = texture2D(shapeTexture, shapeCoord);
40
lowp vec4 color = backgroundColor;
42
// FIXME(loicm) Would be better to use a bitfield but bitwise ops have only been integrated in
43
// GLSL 1.3 (OpenGL 3) and GLSL ES 3 (OpenGL ES 3).
45
// Blend the source over the current color (static flow control prevents the texture fetch).
46
lowp vec2 axisMask = -sign((sourceCoord.zw * sourceCoord.zw) - vec2(1.0));
47
lowp float mask = clamp(axisMask.x + axisMask.y, 0.0, 1.0);
48
lowp vec4 source = texture2D(sourceTexture, sourceCoord.st) * vec4(sourceOpacity * mask);
49
color = vec4(1.0 - source.a) * color + source;
52
// Blend the overlay over the current color.
53
lowp vec2 overlayAxisMask = -sign((overlayCoord * overlayCoord) - vec2(1.0));
54
lowp float overlayMask = clamp(overlayAxisMask.x + overlayAxisMask.y, 0.0, 1.0);
55
lowp vec4 overlay = overlayColor * vec4(overlayMask);
56
color = vec4(1.0 - overlay.a) * color + overlay;
58
// Shape the current color with the mask.
59
color *= vec4(shapeData.b);
61
// Blend the border over the current color.
62
color = vec4(1.0 - shapeData.r) * color + shapeData.gggr;
64
gl_FragColor = color * vec4(opacity);