git.y1.nz

SameBoy

Accurate GB/GBC emulator
download: https://git.y1.nz/archives/sameboy.tar.gz
README | Files | Log | Refs | LICENSE

Shaders/OmniScale.fsh

      1 /* OmniScale is derived from the pattern based design of HQnx, but with the following general differences:
      2     - The actual output calculating was completely redesigned as resolution independent graphic generator. This allows
      3       scaling to any factor.
      4     - HQnx approximations that were good enough for a 2x/3x/4x factor were refined, creating smoother gradients.
      5     - "Quarters" can be interpolated in more ways than in the HQnx filters 
      6     - If a pattern does not provide enough information to determine the suitable scaling interpolation, up to 16 pixels 
      7       per quarter are sampled (in contrast to the usual 9) in order to determine the best interpolation. 
      8  */
      9 
     10 /* We use the same colorspace as the HQ algorithms. */
     11 STATIC vec3 rgb_to_hq_colospace(vec4 rgb)
     12 {
     13     return vec3( 0.250 * rgb.r + 0.250 * rgb.g + 0.250 * rgb.b,
     14                  0.250 * rgb.r - 0.000 * rgb.g - 0.250 * rgb.b,
     15                 -0.125 * rgb.r + 0.250 * rgb.g - 0.125 * rgb.b);
     16 }
     17 
     18 
     19 STATIC bool is_different(vec4 a, vec4 b)
     20 {
     21     vec3 diff = abs(rgb_to_hq_colospace(a) - rgb_to_hq_colospace(b));
     22     return diff.x > 0.018 || diff.y > 0.002 || diff.z > 0.005;
     23 }
     24 
     25 #define P(m, r) ((pattern & (m)) == (r))
     26 
     27 STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
     28 {
     29     // o = offset, the width of a pixel
     30     vec2 o = vec2(1, 1);
     31     
     32     /* We always calculate the top left quarter.  If we need a different quarter, we flip our co-ordinates */
     33 
     34     // p = the position within a pixel [0...1]
     35     vec2 p = fract(position * input_resolution);
     36 
     37     if (p.x > 0.5) {
     38         o.x = -o.x;
     39         p.x = 1.0 - p.x;
     40     }
     41     if (p.y > 0.5) {
     42         o.y = -o.y;
     43         p.y = 1.0 - p.y;
     44     }
     45 
     46     vec4 w0 = texture_relative(image, position, vec2( -o.x, -o.y));
     47     vec4 w1 = texture_relative(image, position, vec2(    0, -o.y));
     48     vec4 w2 = texture_relative(image, position, vec2(  o.x, -o.y));
     49     vec4 w3 = texture_relative(image, position, vec2( -o.x,    0));
     50     vec4 w4 = texture_relative(image, position, vec2(    0,    0));
     51     vec4 w5 = texture_relative(image, position, vec2(  o.x,    0));
     52     vec4 w6 = texture_relative(image, position, vec2( -o.x,  o.y));
     53     vec4 w7 = texture_relative(image, position, vec2(    0,  o.y));
     54     vec4 w8 = texture_relative(image, position, vec2(  o.x,  o.y));
     55 
     56     int pattern = 0;
     57     if (is_different(w0, w4)) pattern |= 1 << 0;
     58     if (is_different(w1, w4)) pattern |= 1 << 1;
     59     if (is_different(w2, w4)) pattern |= 1 << 2;
     60     if (is_different(w3, w4)) pattern |= 1 << 3;
     61     if (is_different(w5, w4)) pattern |= 1 << 4;
     62     if (is_different(w6, w4)) pattern |= 1 << 5;
     63     if (is_different(w7, w4)) pattern |= 1 << 6;
     64     if (is_different(w8, w4)) pattern |= 1 << 7;
     65 
     66     if ((P(0xBF,0x37) || P(0xDB,0x13)) && is_different(w1, w5)) {
     67         return mix(w4, w3, 0.5 - p.x);
     68     }
     69     if ((P(0xDB,0x49) || P(0xEF,0x6D)) && is_different(w7, w3)) {
     70         return mix(w4, w1, 0.5 - p.y);
     71     }
     72     if ((P(0x0B,0x0B) || P(0xFE,0x4A) || P(0xFE,0x1A)) && is_different(w3, w1)) {
     73         return w4;
     74     }
     75     if ((P(0x6F,0x2A) || P(0x5B,0x0A) || P(0xBF,0x3A) || P(0xDF,0x5A) ||
     76          P(0x9F,0x8A) || P(0xCF,0x8A) || P(0xEF,0x4E) || P(0x3F,0x0E) ||
     77          P(0xFB,0x5A) || P(0xBB,0x8A) || P(0x7F,0x5A) || P(0xAF,0x8A) ||
     78          P(0xEB,0x8A)) && is_different(w3, w1)) {
     79         return mix(w4, mix(w4, w0, 0.5 - p.x), 0.5 - p.y);
     80     }
     81     if (P(0x0B,0x08)) {
     82         return mix(mix(w0 * 0.375 + w1 * 0.25 + w4 * 0.375, w4 * 0.5 + w1 * 0.5, p.x * 2.0), w4, p.y * 2.0);
     83     }
     84     if (P(0x0B,0x02)) {
     85         return mix(mix(w0 * 0.375 + w3 * 0.25 + w4 * 0.375, w4 * 0.5 + w3 * 0.5, p.y * 2.0), w4, p.x * 2.0);
     86     }
     87     if (P(0x2F,0x2F)) {
     88         float dist = length(p - vec2(0.5));
     89         float pixel_size = length(1.0 / (output_resolution / input_resolution));
     90         if (dist < 0.5 - pixel_size / 2.0) {
     91             return w4;
     92         }
     93         vec4 r;
     94         if (is_different(w0, w1) || is_different(w0, w3)) {
     95             r = mix(w1, w3, p.y - p.x + 0.5);
     96         }
     97         else {
     98             r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
     99         }
    100 
    101         if (dist > 0.5 + pixel_size / 2.0) {
    102             return r;
    103         }
    104         return mix(w4, r, (dist - 0.5 + pixel_size / 2.0) / pixel_size);
    105     }
    106     if (P(0xBF,0x37) || P(0xDB,0x13)) {
    107         float dist = p.x - 2.0 * p.y;
    108         float pixel_size = length(1.0 / (output_resolution / input_resolution)) * sqrt(5.0);
    109         if (dist > pixel_size / 2.0) {
    110             return w1;
    111         }
    112         vec4 r = mix(w3, w4, p.x + 0.5);
    113         if (dist < -pixel_size / 2.0) {
    114             return r;
    115         }
    116         return mix(r, w1, (dist + pixel_size / 2.0) / pixel_size);
    117     }
    118     if (P(0xDB,0x49) || P(0xEF,0x6D)) {
    119         float dist = p.y - 2.0 * p.x;
    120         float pixel_size = length(1.0 / (output_resolution / input_resolution)) * sqrt(5.0);
    121         if (p.y - 2.0 * p.x > pixel_size / 2.0) {
    122             return w3;
    123         }
    124         vec4 r = mix(w1, w4, p.x + 0.5);
    125         if (dist < -pixel_size / 2.0) {
    126             return r;
    127         }
    128         return mix(r, w3, (dist + pixel_size / 2.0) / pixel_size);
    129     }
    130     if (P(0xBF,0x8F) || P(0x7E,0x0E)) {
    131         float dist = p.x + 2.0 * p.y;
    132         float pixel_size = length(1.0 / (output_resolution / input_resolution)) * sqrt(5.0);
    133 
    134         if (dist > 1.0 + pixel_size / 2.0) {
    135             return w4;
    136         }
    137 
    138         vec4 r;
    139         if (is_different(w0, w1) || is_different(w0, w3)) {
    140             r = mix(w1, w3, p.y - p.x + 0.5);
    141         }
    142         else {
    143             r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
    144         }
    145 
    146         if (dist < 1.0 - pixel_size / 2.0) {
    147             return r;
    148         }
    149 
    150         return mix(r, w4, (dist + pixel_size / 2.0 - 1.0) / pixel_size);
    151     }
    152 
    153     if (P(0x7E,0x2A) || P(0xEF,0xAB)) {
    154         float dist = p.y + 2.0 * p.x;
    155         float pixel_size = length(1.0 / (output_resolution / input_resolution)) * sqrt(5.0);
    156 
    157         if (p.y + 2.0 * p.x > 1.0 + pixel_size / 2.0) {
    158             return w4;
    159         }
    160 
    161         vec4 r;
    162 
    163         if (is_different(w0, w1) || is_different(w0, w3)) {
    164             r = mix(w1, w3, p.y - p.x + 0.5);
    165         }
    166         else {
    167             r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
    168         }
    169 
    170         if (dist < 1.0 - pixel_size / 2.0) {
    171             return r;
    172         }
    173 
    174         return mix(r, w4, (dist + pixel_size / 2.0 - 1.0) / pixel_size);
    175     }
    176 
    177     if (P(0x1B,0x03) || P(0x4F,0x43) || P(0x8B,0x83) || P(0x6B,0x43)) {
    178         return mix(w4, w3, 0.5 - p.x);
    179     }
    180 
    181     if (P(0x4B,0x09) || P(0x8B,0x89) || P(0x1F,0x19) || P(0x3B,0x19)) {
    182         return mix(w4, w1, 0.5 - p.y);
    183     }
    184 
    185     if (P(0xFB,0x6A) || P(0x6F,0x6E) || P(0x3F,0x3E) || P(0xFB,0xFA) ||
    186         P(0xDF,0xDE) || P(0xDF,0x1E)) {
    187         return mix(w4, w0, (1.0 - p.x - p.y) / 2.0);
    188     }
    189 
    190     if (P(0x4F,0x4B) || P(0x9F,0x1B) || P(0x2F,0x0B) ||
    191         P(0xBE,0x0A) || P(0xEE,0x0A) || P(0x7E,0x0A) || P(0xEB,0x4B) ||
    192         P(0x3B,0x1B)) {
    193         float dist = p.x + p.y;
    194         float pixel_size = length(1.0 / (output_resolution / input_resolution));
    195 
    196         if (dist > 0.5 + pixel_size / 2.0) {
    197             return w4;
    198         }
    199 
    200         vec4 r;
    201         if (is_different(w0, w1) || is_different(w0, w3)) {
    202             r = mix(w1, w3, p.y - p.x + 0.5);
    203         }
    204         else {
    205             r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
    206         }
    207 
    208         if (dist < 0.5 - pixel_size / 2.0) {
    209             return r;
    210         }
    211 
    212         return mix(r, w4, (dist + pixel_size / 2.0 - 0.5) / pixel_size);
    213     }
    214 
    215     if (P(0x0B,0x01)) {
    216         return mix(mix(w4, w3, 0.5 - p.x), mix(w1, (w1 + w3) / 2.0, 0.5 - p.x), 0.5 - p.y);
    217     }
    218 
    219     if (P(0x0B,0x00)) {
    220         return mix(mix(w4, w3, 0.5 - p.x), mix(w1, w0, 0.5 - p.x), 0.5 - p.y);
    221     }
    222 
    223     float dist = p.x + p.y;
    224     float pixel_size = length(1.0 / (output_resolution / input_resolution));
    225 
    226     if (dist > 0.5 + pixel_size / 2.0) {
    227         return w4;
    228     }
    229 
    230     /* We need more samples to "solve" this diagonal */
    231     vec4 x0 = texture_relative(image, position, vec2( -o.x * 2.0, -o.y * 2.0));
    232     vec4 x1 = texture_relative(image, position, vec2( -o.x      , -o.y * 2.0));
    233     vec4 x2 = texture_relative(image, position, vec2(  0.0      , -o.y * 2.0));
    234     vec4 x3 = texture_relative(image, position, vec2(  o.x      , -o.y * 2.0));
    235     vec4 x4 = texture_relative(image, position, vec2( -o.x * 2.0, -o.y      ));
    236     vec4 x5 = texture_relative(image, position, vec2( -o.x * 2.0,  0.0      ));
    237     vec4 x6 = texture_relative(image, position, vec2( -o.x * 2.0,  o.y      ));
    238 
    239     if (is_different(x0, w4)) pattern |= 1 << 8;
    240     if (is_different(x1, w4)) pattern |= 1 << 9;
    241     if (is_different(x2, w4)) pattern |= 1 << 10;
    242     if (is_different(x3, w4)) pattern |= 1 << 11;
    243     if (is_different(x4, w4)) pattern |= 1 << 12;
    244     if (is_different(x5, w4)) pattern |= 1 << 13;
    245     if (is_different(x6, w4)) pattern |= 1 << 14;
    246 
    247     int diagonal_bias = -7;
    248     while (pattern != 0) {
    249         diagonal_bias += pattern & 1;
    250         pattern >>= 1;
    251     }
    252 
    253     if (diagonal_bias <= 0) {
    254         vec4 r = mix(w1, w3, p.y - p.x + 0.5);
    255         if (dist < 0.5 - pixel_size / 2.0) {
    256             return r;
    257         }
    258         return mix(r, w4, (dist + pixel_size / 2.0 - 0.5) / pixel_size);
    259     }
    260     
    261     return w4;
    262 }

This webpage is intended to be an accessible preview of this repository. To get a fuller picture, clone it and use the git CLI.