
#if GL_ES
precision highp float;
#endif

varying vec4 texCoordVarying[5];

uniform sampler2D sampler;
uniform vec2 textureSize;

void main() {
    vec4 colD, colF, colB, colH, col, tmp;
    vec2 sel;
    
    col  = texture2DProj(sampler, texCoordVarying[0]);  //central (can be E0-E3)
    colD = texture2DProj(sampler, texCoordVarying[1]);  //D (left)
    colF = texture2DProj(sampler, texCoordVarying[2]);  //F (right)
    colB = texture2DProj(sampler, texCoordVarying[3]);  //B (top)
    colH = texture2DProj(sampler, texCoordVarying[4]);  //H (bottom)
    
    sel = fract(texCoordVarying[0].xy * textureSize.xy);       //where are we (E0-E3)? E0 is default
    
    if(sel.y >= 0.5) { tmp = colB; colB = colH; colH = tmp; }  //E1 (or E3): swap B and H
    if(sel.x >= 0.5) { tmp = colF; colF = colD; colD = tmp; }  //E2 (or E3): swap D and F 
    
    if(colB == colD && colB != colF && colD != colH) {  //do the Scale2x rule
        col = colD;
    }
    
    gl_FragColor = col;
}
