duckstation

duckstation, but archived from the revision just before upstream changed it to a proprietary software project, this version is the libre one
git clone https://git.neptards.moe/u3shit/duckstation.git
Log | Files | Refs | README | LICENSE

XBR.glsl (8745B)


      1 //    Hyllian's xBR-lv2-standalone 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 = COLOR DISTINCTION THRESHOLD
     29 OptionName = XBR_EQ_THRESHOLD
     30 MinValue = 0.0
     31 MaxValue = 1.0
     32 StepAmount = 0.01
     33 DefaultValue = 0.32
     34 
     35 [OptionRangeFloat]
     36 GUIName = SMOOTHNESS THRESHOLD
     37 OptionName = XBR_LV2_COEFFICIENT
     38 MinValue = 0.0
     39 MaxValue = 1.0
     40 StepAmount = 0.1
     41 DefaultValue = 0.3
     42 
     43 [OptionRangeFloat]
     44 GUIName = COLOR BLENDING
     45 OptionName = XBR_BLENDING
     46 MinValue = 0.0
     47 MaxValue = 1.0
     48 StepAmount = 1.0
     49 DefaultValue = 1.0
     50 
     51 [/configuration]
     52 */
     53 
     54 // Uncomment just one of the three params below to choose the corner detection
     55 //#define CORNER_A
     56 //#define CORNER_B
     57 #define CORNER_C
     58 
     59 #define lv2_cf (GetOption(XBR_LV2_COEFFICIENT)+2.0)
     60 #define P(x,y) (vec2(x,y)*vec2(dx,dy))
     61 
     62 const  vec4 Ao = vec4( 1.0, -1.0, -1.0, 1.0 );
     63 const  vec4 Bo = vec4( 1.0,  1.0, -1.0,-1.0 );
     64 const  vec4 Co = vec4( 1.5,  0.5, -0.5, 0.5 );
     65 const  vec4 Ax = vec4( 1.0, -1.0, -1.0, 1.0 );
     66 const  vec4 Bx = vec4( 0.5,  2.0, -0.5,-2.0 );
     67 const  vec4 Cx = vec4( 1.0,  1.0, -0.5, 0.0 );
     68 const  vec4 Ay = vec4( 1.0, -1.0, -1.0, 1.0 );
     69 const  vec4 By = vec4( 2.0,  0.5, -2.0,-0.5 );
     70 const  vec4 Cy = vec4( 2.0,  0.0, -1.0, 0.5 );
     71 const  vec4 Ci = vec4(0.25, 0.25, 0.25, 0.25);
     72 
     73 const vec3 v2f = vec3( 65536, 256, 1); // vec to float encode
     74 const vec3 Y = vec3(0.2627, 0.6780, 0.0593);
     75 
     76 // Return if A components are less than or equal B ones.
     77 vec4 LTE(vec4 A, vec4 B)
     78 {
     79     return step(A, B);
     80 }
     81 
     82 // Return if A components are less than B ones.
     83 vec4 LT(vec4 A, vec4 B)
     84 {
     85     return vec4(lessThan(A, B));
     86 }
     87 
     88 // Return logically inverted vector components. BEWARE: Only works with 0.0 or 1.0 components.
     89 vec4 NOT(vec4 A)
     90 {
     91     return (vec4(1.0) - A);
     92 }
     93 
     94 // Compare two vectors and return their components are different.
     95 vec4 diff(vec4 A, vec4 B)
     96 {
     97     return vec4(notEqual(A, B));
     98 }
     99 
    100 float dist(vec3 A, vec3 B)
    101 {
    102     return dot(abs(A-B), Y);
    103 }
    104 
    105 // Calculate color distance between two vectors of four pixels
    106 vec4 dist4(mat4x3 A, mat4x3 B)
    107 {
    108     return vec4(dist(A[0],B[0]), dist(A[1],B[1]), dist(A[2],B[2]), dist(A[3],B[3]));
    109 }
    110 
    111 // Tests if color components are under a threshold. In this case they are considered 'equal'.
    112 vec4 eq(mat4x3 A, mat4x3 B)
    113 {
    114     return (step(dist4(A, B), vec4(GetOption(XBR_EQ_THRESHOLD))));
    115 }
    116 
    117 // Determine if two vector components are NOT equal based on a threshold.
    118 vec4 neq(mat4x3 A, mat4x3 B)
    119 {
    120     return (vec4(1.0, 1.0, 1.0, 1.0) - eq(A, B));
    121 }
    122 
    123 // Calculate weighted distance among pixels in some directions.
    124 vec4 weighted_distance(mat4x3 a, mat4x3 b, mat4x3 c, mat4x3 d, mat4x3 e, mat4x3 f, mat4x3 g, mat4x3 h)
    125 {
    126     return (dist4(a,b) + dist4(a,c) + dist4(d,e) + dist4(d,f) + 4.0*dist4(g,h));
    127 }
    128 
    129 
    130 
    131 void main()
    132 {
    133     vec2 texCoord = GetCoordinates();
    134     vec2 SourceSize = 1.0 / GetInvNativePixelSize();
    135     float aa_factor = 2.0* (1.0/GetWindowSize().x) * SourceSize.x;
    136 
    137     vec4 edri, edr, edr_l, edr_u, px; // px = pixel, edr = edge detection rule
    138     vec4 irlv0, irlv1, irlv2l, irlv2u;
    139     vec4 fx, fx_l, fx_u; // inequations of straight lines.
    140     vec3 res1, res2;
    141     vec4 fx45i, fx45, fx30, fx60;
    142 
    143     float dx = 1.0/SourceSize.x;
    144     float dy = 1.0/SourceSize.y;
    145 
    146     vec2 loc = texCoord*SourceSize.xy;
    147 
    148     vec2 fp  = fract(loc);
    149 
    150     vec2 tc = (floor(loc)+vec2(0.5,0.5))/SourceSize;
    151 
    152    //    A1 B1 C1
    153    // A0  A  B  C C4
    154    // D0  D  E  F F4
    155    // G0  G  H  I I4
    156    //    G5 H5 I5
    157 
    158     vec3 A1 = SampleLocation(tc+P(-1.0,-2.0)).xyz;
    159     vec3 B1 = SampleLocation(tc+P( 0.0,-2.0)).xyz;
    160     vec3 C1 = SampleLocation(tc+P( 1.0,-2.0)).xyz;
    161     vec3 A  = SampleLocation(tc+P(-1.0,-1.0)).xyz;
    162     vec3 B  = SampleLocation(tc+P( 0.0,-1.0)).xyz;
    163     vec3 C  = SampleLocation(tc+P( 1.0,-1.0)).xyz;
    164     vec3 D  = SampleLocation(tc+P(-1.0, 0.0)).xyz;
    165     vec3 E  = SampleLocation(tc+P( 0.0, 0.0)).xyz;
    166     vec3 F  = SampleLocation(tc+P( 1.0, 0.0)).xyz;
    167     vec3 G  = SampleLocation(tc+P(-1.0, 1.0)).xyz;
    168     vec3 H  = SampleLocation(tc+P( 0.0, 1.0)).xyz;
    169     vec3 I  = SampleLocation(tc+P( 1.0, 1.0)).xyz;
    170     vec3 G5 = SampleLocation(tc+P(-1.0, 2.0)).xyz;
    171     vec3 H5 = SampleLocation(tc+P( 0.0, 2.0)).xyz;
    172     vec3 I5 = SampleLocation(tc+P( 1.0, 2.0)).xyz;
    173     vec3 A0 = SampleLocation(tc+P(-2.0,-1.0)).xyz;
    174     vec3 D0 = SampleLocation(tc+P(-2.0, 0.0)).xyz;
    175     vec3 G0 = SampleLocation(tc+P(-2.0,-1.0)).xyz;
    176     vec3 C4 = SampleLocation(tc+P( 2.0,-1.0)).xyz;
    177     vec3 F4 = SampleLocation(tc+P( 2.0, 0.0)).xyz;
    178     vec3 I4 = SampleLocation(tc+P( 2.0, 1.0)).xyz;
    179 
    180     mat4x3 b  = mat4x3(B, D, H, F);
    181     mat4x3 c  = mat4x3(C, A, G, I);
    182     mat4x3 d  = mat4x3(D, H, F, B);
    183     mat4x3 e  = mat4x3(E, E, E, E);
    184     mat4x3 f  = mat4x3(F, B, D, H);
    185     mat4x3 g  = mat4x3(G, I, C, A);
    186     mat4x3 h  = mat4x3(H, F, B, D);
    187     mat4x3 i  = mat4x3(I, C, A, G);
    188 
    189     mat4x3 i4 = mat4x3(I4, C1, A0, G5);
    190     mat4x3 i5 = mat4x3(I5, C4, A1, G0);
    191     mat4x3 h5 = mat4x3(H5, F4, B1, D0);
    192     mat4x3 f4 = mat4x3(F4, B1, D0, H5);
    193 
    194     vec4 b_   = v2f * b;
    195     vec4 c_   = v2f * c;
    196     vec4 d_   = b_.yzwx;
    197     vec4 e_   = v2f * e;
    198     vec4 f_   = b_.wxyz;
    199     vec4 g_   = c_.zwxy;
    200     vec4 h_   = b_.zwxy;
    201     vec4 i_   = c_.wxyz;
    202 
    203     vec4 i4_  = v2f * i4;
    204     vec4 i5_  = v2f * i5;
    205     vec4 h5_  = v2f * h5;
    206     vec4 f4_  = h5_.yzwx;
    207 
    208     // These inequations define the line below which interpolation occurs.
    209     fx    = ( Ao*fp.y + Bo*fp.x );
    210     fx_l  = ( Ax*fp.y + Bx*fp.x );
    211     fx_u  = ( Ay*fp.y + By*fp.x );
    212 
    213     irlv0 = diff(e_,f_) * diff(e_,h_);
    214     irlv1 = irlv0;
    215 
    216 #ifdef CORNER_B
    217     irlv1      = saturate(irlv0 * ( neq(f,b) * neq(h,d) + eq(e,i) * neq(f,i4) * neq(h,i5) + eq(e,g) + eq(e,c) ) );
    218 #endif
    219 #ifdef CORNER_C
    220     irlv1     = saturate(irlv0  * ( neq(f,b) * neq(f,c) + neq(h,d) * neq(h,g) + eq(e,i) * (neq(f,f4) * neq(f,i4) + neq(h,h5) * neq(h,i5)) + eq(e,g) + eq(e,c)) );
    221 #endif
    222 
    223     irlv2l = diff(e_,g_) * diff( d_, g_);
    224     irlv2u = diff(e_,c_) * diff( b_, c_);
    225 
    226     if (GetOption(XBR_BLENDING) == 1.0) {
    227         vec4 delta  = vec4(aa_factor);
    228         vec4 deltaL = vec4(0.5, 1.0, 0.5, 1.0) * aa_factor;
    229         vec4 deltaU = deltaL.yxwz;
    230 
    231         fx45i = saturate( 0.5 + (fx   - Co - Ci) / delta  );
    232         fx45  = saturate( 0.5 + (fx   - Co     ) / delta  );
    233         fx30  = saturate( 0.5 + (fx_l - Cx     ) / deltaL );
    234         fx60  = saturate( 0.5 + (fx_u - Cy     ) / deltaU );
    235     }
    236     else {
    237         fx45i = LT( Co + Ci, fx   );
    238         fx45  = LT(      Co, fx   );
    239         fx30  = LT(      Cx, fx_l );
    240         fx60  = LT(      Cy, fx_u );
    241     }
    242        
    243     vec4 wd1 = weighted_distance( e, c,  g, i, h5, f4, h, f);
    244     vec4 wd2 = weighted_distance( h, d, i5, f, i4,  b, e, i);
    245 
    246     vec4 d_fg = dist4(f, g);
    247     vec4 d_hc = dist4(h, c);
    248 
    249     edri      = LTE(wd1, wd2) * irlv0;
    250     edr       = LT( wd1, wd2) * irlv1 * NOT(edri.yzwx * edri.wxyz);
    251     edr_l     = LTE( lv2_cf * d_fg, d_hc ) * irlv2l * edr * (NOT(edri.yzwx) * eq(e, c));
    252     edr_u     = LTE( lv2_cf * d_hc, d_fg ) * irlv2u * edr * (NOT(edri.wxyz) * eq(e, g));
    253 
    254     fx45i = edri   * fx45i;
    255     fx45  = edr    * fx45;
    256     fx30  = edr_l  * fx30;
    257     fx60  = edr_u  * fx60;
    258 
    259     px = LTE(dist4(e,f), dist4(e,h));
    260 
    261     vec4 maximos = max(max(fx30, fx60), max(fx45, fx45i));
    262 
    263     res1 = mix(E, mix(H, F, px.x), maximos.x);
    264     res2 = mix(E, mix(B, D, px.z), maximos.z);
    265 
    266     vec3 res1a = mix(res1, res2, step(dist(E, res1), dist(E, res2)));
    267 
    268     res1 = mix(E, mix(F, B, px.y), maximos.y);
    269     res2 = mix(E, mix(D, H, px.w), maximos.w);
    270 
    271     vec3 res1b = mix(res1, res2, step(dist(E, res1), dist(E, res2)));
    272 
    273     vec3 res = mix(res1a, res1b, step(dist(E, res1a), dist(E, res1b)));
    274 
    275     SetOutput(vec4(res, 1.0));
    276 }