Warning, /frameworks/kquickcharts/src/shaders/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 opacity; // inherited opacity of this item - offset 64
0017     lowp float lineWidth; // offset 68
0018     lowp float aspect; // offset 72
0019     lowp float smoothing; // offset 76
0020 } ubuf; // size 80
0021 
0022 #define MAXIMUM_POINT_COUNT 14
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 
0054     lowp float d = dot(point - points[0], point - points[0]);
0055     lowp float s = 1.0;
0056     for (int i = 0, j = count - 1; i < count && i < MAXIMUM_POINT_COUNT; j = i, i++)
0057     {
0058         lowp vec2 e = points[j] - points[i];
0059         lowp vec2 w = point - points[i];
0060         lowp float h = clamp( dot(w, e) / dot(e, e), 0.0, 1.0 );
0061         lowp vec2 b = w - e * h;
0062         d = min(d, dot(b, b));
0063 
0064         bvec3 c = bvec3(point.y >= points[i].y, point.y < points[j].y, e.x * w.y > e.y * w.x);
0065         if(all(c) || all(not(c))) s *= -1.0;
0066     }
0067     return s * sqrt(d);
0068 }
0069 
0070 void main()
0071 {
0072     lowp vec2 point = uv;
0073 
0074     lowp vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
0075 
0076     lowp float bounds_range = max(0.01, ubuf.lineWidth);
0077 
0078     // bounds.y contains the line segment's maximum value. If we are a bit above
0079     // that, we will never render anything, so just discard the pixel.
0080     if (point.y > bounds.y + bounds_range) {
0081         out_color = vec4(0.0);
0082         return;
0083     }
0084 
0085     // bounds.x contains the line segment's minimum value. If we are a bit below
0086     // that, we know we will always be inside the polygon described by points.
0087     // So just return a pixel with fillColor.
0088     if (point.y < bounds.x - bounds_range) {
0089         out_color = fillColor * ubuf.opacity;
0090         return;
0091     }
0092 
0093     lowp float polygon = sdf_polygon(point, int(pointCount));
0094 
0095     color = sdf_render(polygon, color, fillColor);
0096 
0097     if (ubuf.lineWidth > 0.0) {
0098         color = mix(color, lineColor, 1.0 - smoothstep(-ubuf.smoothing, ubuf.smoothing, sdf_annular(polygon, ubuf.lineWidth)));
0099     }
0100 
0101     out_color = color * ubuf.opacity;
0102 }