1#version 130 2 3/* 4 ScaleFX - Pass 0 5 by Sp00kyFox, 2017-03-01 6 7Filter: Nearest 8Scale: 1x 9 10ScaleFX is an edge interpolation algorithm specialized in pixel art. It was 11originally intended as an improvement upon Scale3x but became a new filter in 12its own right. 13ScaleFX interpolates edges up to level 6 and makes smooth transitions between 14different slopes. The filtered picture will only consist of colours present 15in the original. 16 17Pass 0 prepares metric data for the next pass. 18 19 20 21Copyright (c) 2016 Sp00kyFox - ScaleFX@web.de 22 23Permission is hereby granted, free of charge, to any person obtaining a copy 24of this software and associated documentation files (the "Software"), to deal 25in the Software without restriction, including without limitation the rights 26to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 27copies of the Software, and to permit persons to whom the Software is 28furnished to do so, subject to the following conditions: 29 30The above copyright notice and this permission notice shall be included in 31all copies or substantial portions of the Software. 32 33THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 34IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 35FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 36AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 37LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 38OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 39THE SOFTWARE. 40 41*/ 42 43#if defined(VERTEX) 44 45#if __VERSION__ >= 130 46#define COMPAT_VARYING out 47#define COMPAT_ATTRIBUTE in 48#define COMPAT_TEXTURE texture 49#else 50#define COMPAT_VARYING varying 51#define COMPAT_ATTRIBUTE attribute 52#define COMPAT_TEXTURE texture2D 53#endif 54 55#ifdef GL_ES 56#define COMPAT_PRECISION mediump 57#else 58#define COMPAT_PRECISION 59#endif 60 61COMPAT_ATTRIBUTE vec4 VertexCoord; 62COMPAT_ATTRIBUTE vec4 COLOR; 63COMPAT_ATTRIBUTE vec4 TexCoord; 64COMPAT_VARYING vec4 COL0; 65COMPAT_VARYING vec4 TEX0; 66COMPAT_VARYING vec4 t1; 67COMPAT_VARYING vec4 t2; 68 69uniform mat4 MVPMatrix; 70uniform COMPAT_PRECISION int FrameDirection; 71uniform COMPAT_PRECISION int FrameCount; 72uniform COMPAT_PRECISION vec2 OutputSize; 73uniform COMPAT_PRECISION vec2 TextureSize; 74uniform COMPAT_PRECISION vec2 InputSize; 75 76// vertex compatibility #defines 77#define vTexCoord TEX0.xy 78#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize 79#define outsize vec4(OutputSize, 1.0 / OutputSize) 80 81void main() 82{ 83 gl_Position = MVPMatrix * VertexCoord; 84 COL0 = COLOR; 85 TEX0.xy = TexCoord.xy; 86 float dx = SourceSize.z, dy = SourceSize.w; 87 88 t1 = TEX0.xxxy + vec4(-dx, 0., dx, -dy); // A, B, C 89 t2 = TEX0.xxxy + vec4(-dx, 0., dx, 0.); // D, E, F 90} 91 92#elif defined(FRAGMENT) 93 94#ifdef GL_ES 95#ifdef GL_FRAGMENT_PRECISION_HIGH 96precision highp float; 97#else 98precision mediump float; 99#endif 100#define COMPAT_PRECISION mediump 101#else 102#define COMPAT_PRECISION 103#endif 104 105#if __VERSION__ >= 130 106#define COMPAT_VARYING in 107#define COMPAT_TEXTURE texture 108out COMPAT_PRECISION vec4 FragColor; 109#else 110#define COMPAT_VARYING varying 111#define FragColor gl_FragColor 112#define COMPAT_TEXTURE texture2D 113#endif 114 115uniform COMPAT_PRECISION int FrameDirection; 116uniform COMPAT_PRECISION int FrameCount; 117uniform COMPAT_PRECISION vec2 OutputSize; 118uniform COMPAT_PRECISION vec2 TextureSize; 119uniform COMPAT_PRECISION vec2 InputSize; 120uniform sampler2D Texture; 121COMPAT_VARYING vec4 TEX0; 122COMPAT_VARYING vec4 t1; 123COMPAT_VARYING vec4 t2; 124 125// fragment compatibility #defines 126#define Source Texture 127#define vTexCoord TEX0.xy 128 129#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize 130#define outsize vec4(OutputSize, 1.0 / OutputSize) 131 132// Reference: http://www.compuphase.com/cmetric.htm 133float dist(vec3 A, vec3 B) 134{ 135 float r = 0.5 * (A.r + B.r); 136 vec3 d = A - B; 137 vec3 c = vec3(2. + r, 4., 3. - r); 138 139 return sqrt(dot(c*d, d)) / 3.; 140} 141 142void main() 143{ 144 /* grid metric 145 146 A B C x y z 147 E F o w 148 */ 149 150#ifdef GL_ES 151#define TEX(x) COMPAT_TEXTURE(Source, x) 152// read texels 153 vec3 A = TEX(t1.xw).rgb; 154 vec3 B = TEX(t1.yw).rgb; 155 vec3 C = TEX(t1.zw).rgb; 156 vec3 E = TEX(t2.yw).rgb; 157 vec3 F = TEX(t2.zw).rgb; 158#else 159#define TEX(x, y) textureOffset(Source, vTexCoord, ivec2(x, y)).rgb 160 // read texels 161 vec3 A = TEX(-1,-1); 162 vec3 B = TEX( 0,-1); 163 vec3 C = TEX( 1,-1); 164 vec3 E = TEX( 0, 0); 165 vec3 F = TEX( 1, 0); 166#endif 167 // output 168 FragColor = vec4(dist(E,A), dist(E,B), dist(E,C), dist(E,F)); 169} 170#endif 171