Warning, /frameworks/kquickcharts/src/shaders6/linechart.frag is written in an unsupported language. File is not indexed.

0001 /*
0002  * This file is part of KQuickCharts
0003  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
0004  * SPDX-FileCopyrightText: 2022 Łukasz Wojniłowicz <lukasz.wojnilowicz@gmail.com>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0007  */
0008 
0009 #version 440
0010 
0011 #extension GL_GOOGLE_include_directive: enable
0012 #include "sdf.glsl"
0013 
0014 layout(std140, binding = 0) uniform buf {
0015     highp mat4 matrix;
0016     lowp float lineWidth; // offset 64
0017     lowp float aspect; // offset 68
0018     lowp float opacity; // inherited opacity of this item - offset 72
0019     lowp float smoothing; // offset 76
0020 } ubuf; // size 80
0021 
0022 #define MAXIMUM_POINT_COUNT 18
0023 
0024 layout (location = 0) in mediump vec2 uv;
0025 layout (location = 1) in mediump vec4 pointTuples[MAXIMUM_POINT_COUNT / 2];
0026 layout (location = 19) in highp float pointCount;
0027 layout (location = 20) in mediump vec2 bounds;
0028 layout (location = 21) in mediump vec4 lineColor;
0029 layout (location = 22) in mediump vec4 fillColor;
0030 layout (location = 0) out lowp vec4 out_color;
0031 
0032 // ES2 does not support array function arguments. So instead we need to
0033 // reference the uniform array directly. So this copies the implementation of
0034 // sdf_polygon from sdf.glsl, changing it to refer to the points array directly.
0035 // For simplicity, we use the same function also for other APIs.
0036 lowp float sdf_polygon(in lowp vec2 point, in int count)
0037 {
0038     mediump vec2 points[MAXIMUM_POINT_COUNT];
0039     points[0] = pointTuples[0].xy;
0040     points[1] = pointTuples[0].zw;
0041     points[2] = pointTuples[1].xy;
0042     points[3] = pointTuples[1].zw;
0043     points[4] = pointTuples[2].xy;
0044     points[5] = pointTuples[2].zw;
0045     points[6] = pointTuples[3].xy;
0046     points[7] = pointTuples[3].zw;
0047     points[8] = pointTuples[4].xy;
0048     points[9] = pointTuples[4].zw;
0049     points[10] = pointTuples[5].xy;
0050     points[11] = pointTuples[5].zw;
0051     points[12] = pointTuples[6].xy;
0052     points[13] = pointTuples[6].zw;
0053     points[14] = pointTuples[7].xy;
0054     points[15] = pointTuples[7].zw;
0055     points[16] = pointTuples[8].xy;
0056     points[17] = pointTuples[8].zw;
0057 
0058     lowp float d = dot(point - points[0], point - points[0]);
0059     lowp float s = 1.0;
0060     for (int i = 0, j = count - 1; i < count && i < MAXIMUM_POINT_COUNT; j = i, i++)
0061     {
0062         lowp vec2 e = points[j] - points[i];
0063         lowp vec2 w = point - points[i];
0064         lowp float h = clamp( dot(w, e) / dot(e, e), 0.0, 1.0 );
0065         lowp vec2 b = w - e * h;
0066         d = min(d, dot(b, b));
0067 
0068         bvec3 c = bvec3(point.y >= points[i].y, point.y < points[j].y, e.x * w.y > e.y * w.x);
0069         if(all(c) || all(not(c))) s *= -1.0;
0070     }
0071     return s * sqrt(d);
0072 }
0073 
0074 void main()
0075 {
0076     lowp vec2 point = uv;
0077 
0078     lowp vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
0079 
0080     lowp float bounds_range = max(0.01, ubuf.lineWidth);
0081 
0082     // bounds.y contains the line segment's maximum value. If we are a bit above
0083     // that, we will never render anything, so just discard the pixel.
0084     if (point.y > bounds.y + bounds_range) {
0085         discard;
0086     }
0087 
0088     // bounds.x contains the line segment's minimum value. If we are a bit below
0089     // that, we know we will always be inside the polygon described by points.
0090     // So just return a pixel with fillColor.
0091     if (point.y < bounds.x - bounds_range) {
0092         out_color = fillColor * ubuf.opacity;
0093         return;
0094     }
0095 
0096     lowp float polygon = sdf_polygon(point, int(pointCount));
0097 
0098     color = sdf_render(polygon, color, fillColor);
0099 
0100     if (ubuf.lineWidth > 0.0) {
0101         color = mix(color, lineColor, 1.0 - smoothstep(-ubuf.smoothing, ubuf.smoothing, sdf_annular(polygon, ubuf.lineWidth)));
0102     }
0103 
0104     out_color = color * ubuf.opacity;
0105 }