git.y1.nz

SameBoy

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

Shaders/LCD.fsh

      1 #define COLOR_LOW 0.6
      2 #define COLOR_HIGH 1.0
      3 #define SCANLINE_DEPTH 0.2
      4 
      5 STATIC vec4 scale(sampler2D image, vec2 position, vec2 input_resolution, vec2 output_resolution)
      6 {
      7     vec2 pos = fract(position * input_resolution);
      8     vec2 sub_pos = pos * 6.0;
      9     
     10     vec4 center = texture_relative(image, position, vec2(0, 0));
     11     vec4 left = texture_relative(image, position, vec2(-1, 0));
     12     vec4 right = texture_relative(image, position, vec2(1, 0));
     13     
     14     if (sub_pos.y < 1.0) {
     15         center = mix(center, texture_relative(image, position, vec2( 0, -1)), 0.5 - sub_pos.y / 2.0);
     16         left =   mix(left,   texture_relative(image, position, vec2(-1, -1)), 0.5 - sub_pos.y / 2.0);
     17         right =  mix(right,  texture_relative(image, position, vec2( 1, -1)), 0.5 - sub_pos.y / 2.0);
     18         center *= sub_pos.y * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     19         left *= sub_pos.y * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     20         right *= sub_pos.y * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     21     }
     22     else if (sub_pos.y > 5.0) {
     23         center = mix(center, texture_relative(image, position, vec2( 0, 1)), (sub_pos.y - 5.0) / 2.0);
     24         left =   mix(left,   texture_relative(image, position, vec2(-1, 1)), (sub_pos.y - 5.0) / 2.0);
     25         right =  mix(right,  texture_relative(image, position, vec2( 1, 1)), (sub_pos.y - 5.0) / 2.0);
     26         center *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     27         left *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     28         right *= (6.0 - sub_pos.y) * SCANLINE_DEPTH + (1.0 - SCANLINE_DEPTH);
     29     }
     30     
     31     
     32     vec4 midleft = mix(left, center, 0.5);
     33     vec4 midright = mix(right, center, 0.5);
     34     
     35     vec4 ret;
     36     if (sub_pos.x < 1.0) {
     37         ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_HIGH * left.b, 1),
     38                   vec4(COLOR_HIGH * center.r, COLOR_LOW * center.g, COLOR_LOW  * left.b, 1),
     39                   sub_pos.x);
     40     }
     41     else if (sub_pos.x < 2.0) {
     42         ret = mix(vec4(COLOR_HIGH * center.r, COLOR_LOW  * center.g, COLOR_LOW * left.b, 1),
     43                   vec4(COLOR_HIGH * center.r, COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
     44                   sub_pos.x - 1.0);
     45     }
     46     else if (sub_pos.x < 3.0) {
     47         ret = mix(vec4(COLOR_HIGH * center.r  , COLOR_HIGH * center.g, COLOR_LOW * midleft.b, 1),
     48                   vec4(COLOR_LOW  * midright.r, COLOR_HIGH * center.g, COLOR_LOW * center.b, 1),
     49                   sub_pos.x - 2.0);
     50     }
     51     else if (sub_pos.x < 4.0) {
     52         ret = mix(vec4(COLOR_LOW * midright.r, COLOR_HIGH * center.g , COLOR_LOW  * center.b, 1),
     53                   vec4(COLOR_LOW * right.r   , COLOR_HIGH  * center.g, COLOR_HIGH * center.b, 1),
     54                   sub_pos.x - 3.0);
     55     }
     56     else if (sub_pos.x < 5.0) {
     57         ret = mix(vec4(COLOR_LOW * right.r, COLOR_HIGH * center.g  , COLOR_HIGH * center.b, 1),
     58                   vec4(COLOR_LOW * right.r, COLOR_LOW  * midright.g, COLOR_HIGH * center.b, 1),
     59                   sub_pos.x - 4.0);
     60     }
     61     else {
     62         ret = mix(vec4(COLOR_LOW  * right.r, COLOR_LOW * midright.g, COLOR_HIGH * center.b, 1),
     63                   vec4(COLOR_HIGH * right.r, COLOR_LOW * right.g  ,  COLOR_HIGH * center.b, 1),
     64                   sub_pos.x - 5.0);
     65     }
     66     
     67     return ret;
     68 }

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