Skip to content

Instantly share code, notes, and snippets.

@singron
Created July 22, 2012 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singron/3161079 to your computer and use it in GitHub Desktop.
Save singron/3161079 to your computer and use it in GitHub Desktop.
AdvMame2x shader program
/* Scale2x (aka AdvMame2x)
* Algorithm found at http://scale2x.sourceforge.net/algorithm.html
* Adapted for use by shaders
*/
#version 110
uniform sampler2D texture;
uniform vec2 textureDimensions;
varying vec2 texCoord;
void main() {
// o = offset, the width of a pixel
vec2 o = 1.0 / textureDimensions;
// texel arrangement
// A B C
// D E F
// G H I
vec4 A = texture2D(texture, texCoord + vec2( -o.x, o.y));
vec4 B = texture2D(texture, texCoord + vec2( 0, o.y));
vec4 C = texture2D(texture, texCoord + vec2( o.x, o.y));
vec4 D = texture2D(texture, texCoord + vec2( -o.x, 0));
vec4 E = texture2D(texture, texCoord + vec2( 0, 0));
vec4 F = texture2D(texture, texCoord + vec2( o.x, 0));
vec4 G = texture2D(texture, texCoord + vec2( -o.x, -o.y));
vec4 H = texture2D(texture, texCoord + vec2( 0, -o.y));
vec4 I = texture2D(texture, texCoord + vec2( o.x, -o.y));
vec2 p = texCoord * textureDimensions;
// p = the position within a pixel [0...1]
p = p - floor(p);
if (p.x > .5) {
if (p.y > .5) {
// Top Right
gl_FragColor = B == F && B != D && F != H ? F : E;
} else {
// Bottom Right
gl_FragColor = H == F && D != H && B != F ? F : E;
}
} else {
if (p.y > .5) {
// Top Left
gl_FragColor = D == B && B != F && D != H ? D : E;
} else {
// Bottom Left
gl_FragColor = D == H && D != B && H != F ? D : E;
}
}
}
// Simple pass-through vertex shader
#version 110
varying vec2 texCoord;
void main() {
texCoord = gl_MultiTexCoord0.xy;
gl_Position = gl_Vertex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment