Warning, /frameworks/kdeclarative/src/qmlcontrols/graphicaleffects/lanczos2sharp_core.frag is written in an unsupported language. File is not indexed.
0001 #version 150 core 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 in highp vec2 qt_TexCoord0; 0041 out highp vec4 out_color; 0042 0043 // A=0.5, B=0.825 is the best jinc approximation for x<2.5. if B=1.0, it's a lanczos filter. 0044 // Increase A to get more blur. Decrease it to get a sharper picture. 0045 // B = 0.825 to get rid of dithering. Increase B to get a fine sharpness, though dithering returns. 0046 0047 #define wa (windowSinc * pi) 0048 #define wb (sinc * pi) 0049 0050 const highp float pi = 3.1415926535897932384626433832795; 0051 const highp vec3 dtt = vec3(65536.0, 255.0, 1.0); 0052 0053 highp vec4 reduce4(highp vec3 A, highp vec3 B, highp vec3 C, highp vec3 D) 0054 { 0055 return dtt * mat4x3(A, B, C, D); 0056 } 0057 0058 highp vec3 min4(highp vec3 a, highp vec3 b, highp vec3 c, highp vec3 d) 0059 { 0060 return min(a, min(b, min(c, d))); 0061 } 0062 0063 highp vec3 max4(highp vec3 a, highp vec3 b, highp vec3 c, highp vec3 d) 0064 { 0065 return max(a, max(b, max(c, d))); 0066 } 0067 0068 highp vec4 lanczos(highp vec4 x) 0069 { 0070 return (x == vec4(0.0)) ? vec4(wa * wb) : sin(x * wa) * sin(x * wb) / (x * x); 0071 } 0072 0073 void main() 0074 { 0075 // Discard any pixels that are outside the bounds of the texture. 0076 // This prevents artifacts when the texture doesn't have a full-alpha border. 0077 if (any(lessThan(qt_TexCoord0, vec2(0.0))) || any(greaterThan(qt_TexCoord0, vec2(1.0)))) { 0078 discard; 0079 } 0080 0081 highp vec2 dx = vec2(1.0, 0.0); 0082 highp vec2 dy = vec2(0.0, 1.0); 0083 0084 highp vec2 pixelCoord = qt_TexCoord0 * targetSize / resolution; 0085 highp vec2 texel = (floor(pixelCoord) + vec2(0.5, 0.5)) * resolution / targetSize; 0086 0087 highp vec2 texelCenter = (floor(pixelCoord - vec2(0.5, 0.5)) + vec2(0.5, 0.5)); 0088 0089 highp mat4 weights; 0090 weights[0] = lanczos(vec4(distance(pixelCoord, texelCenter - dx - dy), 0091 distance(pixelCoord, texelCenter - dy), 0092 distance(pixelCoord, texelCenter + dx - dy), 0093 distance(pixelCoord, texelCenter + 2.0 * dx - dy))); 0094 weights[1] = lanczos(vec4(distance(pixelCoord, texelCenter - dx), 0095 distance(pixelCoord, texelCenter), 0096 distance(pixelCoord, texelCenter + dx), 0097 distance(pixelCoord, texelCenter + 2.0 * dx))); 0098 weights[2] = lanczos(vec4(distance(pixelCoord, texelCenter - dx + dy), 0099 distance(pixelCoord, texelCenter + dy), 0100 distance(pixelCoord, texelCenter + dx + dy), 0101 distance(pixelCoord, texelCenter + 2.0 * dx + dy))); 0102 weights[3] = lanczos(vec4(distance(pixelCoord, texelCenter - dx + 2.0 * dy), 0103 distance(pixelCoord, texelCenter + 2.0 * dy), 0104 distance(pixelCoord, texelCenter + dx + 2.0 * dy), 0105 distance(pixelCoord, texelCenter + 2.0 * dx + 2.0 * dy))); 0106 0107 dx = dx * resolution / targetSize; 0108 dy = dy * resolution / targetSize; 0109 texelCenter = texelCenter * resolution / targetSize; 0110 0111 // reading the texels 0112 highp vec3 color = texture(source, qt_TexCoord0).xyz; 0113 0114 highp vec3 c00 = texture(source, texelCenter - dx - dy).xyz; 0115 highp vec3 c10 = texture(source, texelCenter - dy).xyz; 0116 highp vec3 c20 = texture(source, texelCenter + dx - dy).xyz; 0117 highp vec3 c30 = texture(source, texelCenter + 2.0 * dx - dy).xyz; 0118 highp vec3 c01 = texture(source, texelCenter - dx).xyz; 0119 highp vec3 c11 = texture(source, texelCenter).xyz; 0120 highp vec3 c21 = texture(source, texelCenter + dx).xyz; 0121 highp vec3 c31 = texture(source, texelCenter + 2.0 * dx).xyz; 0122 highp vec3 c02 = texture(source, texelCenter - dx + dy).xyz; 0123 highp vec3 c12 = texture(source, texelCenter + dy).xyz; 0124 highp vec3 c22 = texture(source, texelCenter + dx + dy).xyz; 0125 highp vec3 c32 = texture(source, texelCenter + 2.0 * dx + dy).xyz; 0126 highp vec3 c03 = texture(source, texelCenter - dx + 2.0 * dy).xyz; 0127 highp vec3 c13 = texture(source, texelCenter + 2.0 * dy).xyz; 0128 highp vec3 c23 = texture(source, texelCenter + dx + 2.0 * dy).xyz; 0129 highp vec3 c33 = texture(source, texelCenter + 2.0 * dx + 2.0 * dy).xyz; 0130 0131 highp vec3 F6 = texture(source, texel + dx + 0.25 * dx + 0.25 * dy).xyz; 0132 highp vec3 F7 = texture(source, texel + dx + 0.25 * dx - 0.25 * dy).xyz; 0133 highp vec3 F8 = texture(source, texel + dx - 0.25 * dx - 0.25 * dy).xyz; 0134 highp vec3 F9 = texture(source, texel + dx - 0.25 * dx + 0.25 * dy).xyz; 0135 0136 highp vec3 H6 = texture(source, texel + 0.25 * dx + 0.25 * dy + dy).xyz; 0137 highp vec3 H7 = texture(source, texel + 0.25 * dx - 0.25 * dy + dy).xyz; 0138 highp vec3 H8 = texture(source, texel - 0.25 * dx - 0.25 * dy + dy).xyz; 0139 highp vec3 H9 = texture(source, texel - 0.25 * dx + 0.25 * dy + dy).xyz; 0140 0141 highp vec4 f0 = reduce4(F6, F7, F8, F9); 0142 highp vec4 h0 = reduce4(H6, H7, H8, H9); 0143 0144 // Get min/max samples 0145 highp vec3 min_sample = min4(c11, c21, c12, c22); 0146 highp vec3 max_sample = max4(c11, c21, c12, c22); 0147 0148 color = weights[0] * transpose(mat4x3(c00, c10, c20, c30)); 0149 color += weights[1] * transpose(mat4x3(c01, c11, c21, c31)); 0150 color += weights[2] * transpose(mat4x3(c02, c12, c22, c32)); 0151 color += weights[3] * transpose(mat4x3(c03, c13, c23, c33)); 0152 color = color / dot(vec4(1.0) * weights, vec4(1.0)); 0153 0154 // Anti-ringing 0155 highp vec3 aux = color; 0156 color = clamp(color, min_sample, max_sample); 0157 0158 color = mix(aux, color, antiRingingStrength); 0159 0160 float alpha = texture(source, qt_TexCoord0).a * qt_Opacity; 0161 out_color = vec4(color, alpha); 0162 }