Warning, /frameworks/kdeclarative/src/qmlcontrols/graphicaleffects/lanczos2sharp.frag is written in an unsupported language. File is not indexed.
0001 #version 120 0002 0003 /* 0004 SPDX-FileCopyrightText: 2021 Arjen Hiemstra <ahiemstra@heimr.nl> 0005 0006 Hyllian's lanczos windowed-jinc 2-lobe sharper 3D with anti-ringing Shader 0007 0008 Copyright (C) 2011/2016 Hyllian - sergiogdb@gmail.com 0009 0010 Permission is hereby granted, free of charge, to any person obtaining a copy 0011 of this software and associated documentation files (the "Software"), to deal 0012 in the Software without restriction, including without limitation the rights 0013 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 0014 copies of the Software, and to permit persons to whom the Software is 0015 furnished to do so, subject to the following conditions: 0016 0017 The above copyright notice and this permission notice shall be included in 0018 all copies or substantial portions of the Software. 0019 0020 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 0021 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 0022 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 0023 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 0024 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 0025 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 0026 THE SOFTWARE. 0027 */ 0028 0029 uniform sampler2D source; 0030 uniform highp vec2 targetSize; 0031 0032 uniform highp float windowSinc; 0033 uniform highp float sinc; 0034 0035 uniform highp float antiRingingStrength; 0036 uniform highp float resolution; 0037 0038 uniform highp float qt_Opacity; 0039 0040 varying highp vec2 qt_TexCoord0; 0041 0042 // A=0.5, B=0.825 is the best jinc approximation for x<2.5. if B=1.0, it's a lanczos filter. 0043 // Increase A to get more blur. Decrease it to get a sharper picture. 0044 // B = 0.825 to get rid of dithering. Increase B to get a fine sharpness, though dithering returns. 0045 0046 #define wa (windowSinc * pi) 0047 #define wb (sinc * pi) 0048 0049 const highp float pi = 3.1415926535897932384626433832795; 0050 const highp vec3 dtt = vec3(65536.0, 255.0, 1.0); 0051 0052 highp vec4 reduce4(highp vec3 A, highp vec3 B, highp vec3 C, highp vec3 D) 0053 { 0054 return dtt * mat4x3(A, B, C, D); 0055 } 0056 0057 highp vec3 min4(highp vec3 a, highp vec3 b, highp vec3 c, highp vec3 d) 0058 { 0059 return min(a, min(b, min(c, d))); 0060 } 0061 0062 highp vec3 max4(highp vec3 a, highp vec3 b, highp vec3 c, highp vec3 d) 0063 { 0064 return max(a, max(b, max(c, d))); 0065 } 0066 0067 highp vec4 lanczos(highp vec4 x) 0068 { 0069 return (x == vec4(0.0)) ? vec4(wa * wb) : sin(x * wa) * sin(x * wb) / (x * x); 0070 } 0071 0072 void main() 0073 { 0074 // Discard any pixels that are outside the bounds of the texture. 0075 // This prevents artifacts when the texture doesn't have a full-alpha border. 0076 if (any(lessThan(qt_TexCoord0, vec2(0.0))) || any(greaterThan(qt_TexCoord0, vec2(1.0)))) { 0077 discard; 0078 } 0079 0080 highp vec2 dx = vec2(1.0, 0.0); 0081 highp vec2 dy = vec2(0.0, 1.0); 0082 0083 highp vec2 pixelCoord = qt_TexCoord0 * targetSize / resolution; 0084 highp vec2 texel = (floor(pixelCoord) + vec2(0.5, 0.5)) * resolution / targetSize; 0085 0086 highp vec2 texelCenter = (floor(pixelCoord - vec2(0.5, 0.5)) + vec2(0.5, 0.5)); 0087 0088 highp mat4 weights; 0089 weights[0] = lanczos(vec4(distance(pixelCoord, texelCenter - dx - dy), 0090 distance(pixelCoord, texelCenter - dy), 0091 distance(pixelCoord, texelCenter + dx - dy), 0092 distance(pixelCoord, texelCenter + 2.0 * dx - dy))); 0093 weights[1] = lanczos(vec4(distance(pixelCoord, texelCenter - dx), 0094 distance(pixelCoord, texelCenter), 0095 distance(pixelCoord, texelCenter + dx), 0096 distance(pixelCoord, texelCenter + 2.0 * dx))); 0097 weights[2] = lanczos(vec4(distance(pixelCoord, texelCenter - dx + dy), 0098 distance(pixelCoord, texelCenter + dy), 0099 distance(pixelCoord, texelCenter + dx + dy), 0100 distance(pixelCoord, texelCenter + 2.0 * dx + dy))); 0101 weights[3] = lanczos(vec4(distance(pixelCoord, texelCenter - dx + 2.0 * dy), 0102 distance(pixelCoord, texelCenter + 2.0 * dy), 0103 distance(pixelCoord, texelCenter + dx + 2.0 * dy), 0104 distance(pixelCoord, texelCenter + 2.0 * dx + 2.0 * dy))); 0105 0106 dx = dx * resolution / targetSize; 0107 dy = dy * resolution / targetSize; 0108 texelCenter = texelCenter * resolution / targetSize; 0109 0110 // reading the texels 0111 highp vec3 color = texture2D(source, qt_TexCoord0).xyz; 0112 0113 highp vec3 c00 = texture2D(source, texelCenter - dx - dy).xyz; 0114 highp vec3 c10 = texture2D(source, texelCenter - dy).xyz; 0115 highp vec3 c20 = texture2D(source, texelCenter + dx - dy).xyz; 0116 highp vec3 c30 = texture2D(source, texelCenter + 2.0 * dx - dy).xyz; 0117 highp vec3 c01 = texture2D(source, texelCenter - dx).xyz; 0118 highp vec3 c11 = texture2D(source, texelCenter).xyz; 0119 highp vec3 c21 = texture2D(source, texelCenter + dx).xyz; 0120 highp vec3 c31 = texture2D(source, texelCenter + 2.0 * dx).xyz; 0121 highp vec3 c02 = texture2D(source, texelCenter - dx + dy).xyz; 0122 highp vec3 c12 = texture2D(source, texelCenter + dy).xyz; 0123 highp vec3 c22 = texture2D(source, texelCenter + dx + dy).xyz; 0124 highp vec3 c32 = texture2D(source, texelCenter + 2.0 * dx + dy).xyz; 0125 highp vec3 c03 = texture2D(source, texelCenter - dx + 2.0 * dy).xyz; 0126 highp vec3 c13 = texture2D(source, texelCenter + 2.0 * dy).xyz; 0127 highp vec3 c23 = texture2D(source, texelCenter + dx + 2.0 * dy).xyz; 0128 highp vec3 c33 = texture2D(source, texelCenter + 2.0 * dx + 2.0 * dy).xyz; 0129 0130 highp vec3 F6 = texture2D(source, texel + dx + 0.25 * dx + 0.25 * dy).xyz; 0131 highp vec3 F7 = texture2D(source, texel + dx + 0.25 * dx - 0.25 * dy).xyz; 0132 highp vec3 F8 = texture2D(source, texel + dx - 0.25 * dx - 0.25 * dy).xyz; 0133 highp vec3 F9 = texture2D(source, texel + dx - 0.25 * dx + 0.25 * dy).xyz; 0134 0135 highp vec3 H6 = texture2D(source, texel + 0.25 * dx + 0.25 * dy + dy).xyz; 0136 highp vec3 H7 = texture2D(source, texel + 0.25 * dx - 0.25 * dy + dy).xyz; 0137 highp vec3 H8 = texture2D(source, texel - 0.25 * dx - 0.25 * dy + dy).xyz; 0138 highp vec3 H9 = texture2D(source, texel - 0.25 * dx + 0.25 * dy + dy).xyz; 0139 0140 highp vec4 f0 = reduce4(F6, F7, F8, F9); 0141 highp vec4 h0 = reduce4(H6, H7, H8, H9); 0142 0143 // Get min/max samples 0144 highp vec3 min_sample = min4(c11, c21, c12, c22); 0145 highp vec3 max_sample = max4(c11, c21, c12, c22); 0146 0147 color = weights[0] * transpose(mat4x3(c00, c10, c20, c30)); 0148 color += weights[1] * transpose(mat4x3(c01, c11, c21, c31)); 0149 color += weights[2] * transpose(mat4x3(c02, c12, c22, c32)); 0150 color += weights[3] * transpose(mat4x3(c03, c13, c23, c33)); 0151 color = color / dot(vec4(1.0) * weights, vec4(1.0)); 0152 0153 // Anti-ringing 0154 highp vec3 aux = color; 0155 color = clamp(color, min_sample, max_sample); 0156 0157 color = mix(aux, color, antiRingingStrength); 0158 0159 highp float alpha = texture2D(source, qt_TexCoord0).a * qt_Opacity; 0160 gl_FragColor = vec4(color, alpha); 0161 }