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 // This requires "sdf.frag" which is included through SDFShader.
0010 
0011 uniform lowp float opacity; // inherited opacity of this item
0012 uniform lowp float lineWidth;
0013 uniform lowp float smoothing;
0014 
0015 #define MAXIMUM_POINT_COUNT 18
0016 
0017 #ifdef LEGACY_STAGE_INOUT
0018 varying mediump vec2 uv;
0019 varying mediump vec4 pointTuples[MAXIMUM_POINT_COUNT / 2];
0020 varying highp float pointCount;
0021 varying mediump vec2 bounds;
0022 varying mediump vec4 lineColor;
0023 varying mediump vec4 fillColor;
0024 #define out_color gl_FragColor
0025 #else
0026 in mediump vec2 uv;
0027 in mediump vec4 pointTuples[MAXIMUM_POINT_COUNT / 2];
0028 in highp float pointCount;
0029 in mediump vec2 bounds;
0030 in mediump vec4 lineColor;
0031 in mediump vec4 fillColor;
0032 out lowp vec4 out_color;
0033 #endif
0034 
0035 // ES2 does not support array function arguments. So instead we need to
0036 // reference the uniform array directly. So this copies the implementation of
0037 // sdf_polygon from sdf.glsl, changing it to refer to the points array directly.
0038 // For simplicity, we use the same function also for other APIs.
0039 lowp float sdf_polygon(in lowp vec2 point, in int count)
0040 {
0041     mediump vec2 points[MAXIMUM_POINT_COUNT];
0042     points[0] = pointTuples[0].xy;
0043     points[1] = pointTuples[0].zw;
0044     points[2] = pointTuples[1].xy;
0045     points[3] = pointTuples[1].zw;
0046     points[4] = pointTuples[2].xy;
0047     points[5] = pointTuples[2].zw;
0048     points[6] = pointTuples[3].xy;
0049     points[7] = pointTuples[3].zw;
0050     points[8] = pointTuples[4].xy;
0051     points[9] = pointTuples[4].zw;
0052     points[10] = pointTuples[5].xy;
0053     points[11] = pointTuples[5].zw;
0054     points[12] = pointTuples[6].xy;
0055     points[13] = pointTuples[6].zw;
0056     points[14] = pointTuples[7].xy;
0057     points[15] = pointTuples[7].zw;
0058     points[16] = pointTuples[8].xy;
0059     points[17] = pointTuples[8].zw;
0060 
0061     lowp float d = dot(point - points[0], point - points[0]);
0062     lowp float s = 1.0;
0063     for (int i = 0, j = count - 1; i < count && i < MAXIMUM_POINT_COUNT; j = i, i++)
0064     {
0065         lowp vec2 e = points[j] - points[i];
0066         lowp vec2 w = point - points[i];
0067         lowp float h = clamp( dot(w, e) / dot(e, e), 0.0, 1.0 );
0068         lowp vec2 b = w - e * h;
0069         d = min(d, dot(b, b));
0070 
0071         bvec3 c = bvec3(point.y >= points[i].y, point.y < points[j].y, e.x * w.y > e.y * w.x);
0072         if(all(c) || all(not(c))) s *= -1.0;
0073     }
0074     return s * sqrt(d);
0075 }
0076 
0077 void main()
0078 {
0079     lowp vec2 point = uv;
0080 
0081     lowp vec4 color = vec4(0.0, 0.0, 0.0, 0.0);
0082 
0083     lowp float bounds_range = max(0.01, lineWidth);
0084 
0085     // bounds.y contains the line segment's maximum value. If we are a bit above
0086     // that, we will never render anything, so just discard the pixel.
0087     if (point.y > bounds.y + bounds_range) {
0088         discard;
0089     }
0090 
0091     // bounds.x contains the line segment's minimum value. If we are a bit below
0092     // that, we know we will always be inside the polygon described by points.
0093     // So just return a pixel with fillColor.
0094     if (point.y < bounds.x - bounds_range) {
0095         out_color = fillColor * opacity;
0096         return;
0097     }
0098 
0099     lowp float polygon = sdf_polygon(point, int(pointCount));
0100 
0101     color = sdf_render(polygon, color, fillColor);
0102 
0103     if (lineWidth > 0.0) {
0104         color = mix(color, lineColor, 1.0 - smoothstep(-smoothing, smoothing, sdf_annular(polygon, lineWidth)));
0105     }
0106 
0107     out_color = color * opacity;
0108 }