DDT.glsl (2943B)
1 // Hyllian's DDT Shader 2 3 // Copyright (C) 2011-2024 Hyllian - sergiogdb@gmail.com 4 5 // Permission is hereby granted, free of charge, to any person obtaining a copy 6 // of this software and associated documentation files (the "Software"), to deal 7 // in the Software without restriction, including without limitation the rights 8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 // copies of the Software, and to permit persons to whom the Software is 10 // furnished to do so, subject to the following conditions: 11 12 // The above copyright notice and this permission notice shall be included in 13 // all copies or substantial portions of the Software. 14 15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 // THE SOFTWARE. 22 23 24 /* 25 [configuration] 26 27 [OptionRangeFloat] 28 GUIName = Bilinear Fallback Threshold 29 OptionName = BIL_FALLBACK 30 MinValue = 0.0 31 MaxValue = 1.0 32 StepAmount = 0.05 33 DefaultValue = 0.6 34 35 [/configuration] 36 */ 37 38 const vec3 Y = vec3(0.2126729, 0.7151522, 0.0721750); 39 40 float luma(vec3 color) 41 { 42 return dot(color, Y); 43 } 44 45 vec3 bilinear(float p, float q, vec3 A, vec3 B, vec3 C, vec3 D) 46 { 47 return ((1-p)*(1-q)*A + p*(1-q)*B + (1-p)*q*C + p*q*D); 48 } 49 50 void main() 51 { 52 vec2 texCoord = GetCoordinates(); 53 vec2 nativeSize = 1.0 / GetInvNativePixelSize(); 54 55 vec2 loc = texCoord*nativeSize; 56 vec2 pos = fract(loc) - vec2(0.5, 0.5); // pos = pixel position 57 vec2 dir = sign(pos); // dir = pixel direction 58 59 vec2 dx = vec2(1.0/nativeSize.x, 0.0); 60 vec2 dy = vec2(0.0, 1.0/nativeSize.y); 61 62 vec2 g1 = dir*dx; 63 vec2 g2 = dir*dy; 64 65 vec2 tc = (floor(loc)+vec2(0.5,0.5))/nativeSize; 66 67 vec3 A = SampleLocation(tc ).rgb; 68 vec3 B = SampleLocation(tc +g1 ).rgb; 69 vec3 C = SampleLocation(tc +g2).rgb; 70 vec3 D = SampleLocation(tc +g1+g2).rgb; 71 72 float a = luma(A); 73 float b = luma(B); 74 float c = luma(C); 75 float d = luma(D); 76 77 float p = abs(pos.x); 78 float q = abs(pos.y); 79 80 float k = distance(pos,g1); 81 float l = distance(pos,g2); 82 83 float wd1 = abs(a-d); 84 float wd2 = abs(b-c); 85 86 vec3 color = bilinear(p, q, A, B, C, D); 87 88 if ( wd1 < wd2 ) 89 { 90 if (k < l) 91 { 92 C = A + D - B; 93 } 94 else 95 { 96 B = A + D - C; 97 } 98 } 99 else if (wd1 > wd2) 100 { 101 D = B + C - A; 102 } 103 104 105 vec3 ddt = bilinear(p, q, A, B, C, D); 106 107 color = mix(color, ddt, smoothstep(0.0, BIL_FALLBACK, abs(wd2-wd1))); 108 109 SetOutput(vec4(color, 1.0)); 110 }