~zsombi/ubuntu-ui-toolkit/03-styleditem-styleset

« back to all changes in this revision

Viewing changes to modules/Ubuntu/Components/plugin/shaders/shapeoverlay.frag

  • Committer: Zsombor Egri
  • Date: 2015-03-10 18:03:58 UTC
  • mfrom: (1434.1.13 02-styleset)
  • Revision ID: zsombor.egri@canonical.com-20150310180358-aunnni7a9z9k9c7h
prereq sync

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright © 2015 Canonical Ltd.
 
2
//
 
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.
 
6
//
 
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.
 
11
//
 
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/>.
 
14
//
 
15
// Author: Loïc Molinari <loic.molinari@canonical.com>
 
16
 
 
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.
 
22
 
 
23
uniform sampler2D shapeTexture;
 
24
uniform sampler2D sourceTexture;
 
25
uniform lowp float sourceOpacity;
 
26
uniform lowp float opacity;
 
27
uniform bool textured;
 
28
 
 
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;
 
34
 
 
35
void main(void)
 
36
{
 
37
    // Early texture fetch to cover latency as best as possible.
 
38
    lowp vec4 shapeData = texture2D(shapeTexture, shapeCoord);
 
39
 
 
40
    lowp vec4 color = backgroundColor;
 
41
 
 
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).
 
44
    if (textured) {
 
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;
 
50
    }
 
51
 
 
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;
 
57
 
 
58
    // Shape the current color with the mask.
 
59
    color *= vec4(shapeData.b);
 
60
 
 
61
    // Blend the border over the current color.
 
62
    color = vec4(1.0 - shapeData.r) * color + shapeData.gggr;
 
63
 
 
64
    gl_FragColor = color * vec4(opacity);
 
65
}