Res: Port scale2x an scale4x shaders

This commit is contained in:
Vicki Pfau 2021-06-24 17:35:16 -07:00
parent 1d9bc4c3f7
commit fee40bc74d
5 changed files with 116 additions and 0 deletions

View File

@ -6,6 +6,7 @@ Features:
- Cheat code support in homebrew ports
- Support for combo "Super Game Boy Color" SGB + GBC ROM hacks
- Support for 64 kiB SRAM saves used in some bootlegs
- Additional scaling shaders
Emulation fixes:
- GB Memory: Add cursory cartridge open bus emulation (fixes mgba.io/i/2032)
- GB Video: Clear VRAM on reset (fixes mgba.io/i/2152)

View File

@ -0,0 +1,11 @@
[shader]
name=Scale2x
author=singron
description=AdvanceMAME's Scale2x algorithm
passes=1
[pass.0]
fragmentShader=scale2x.fs
blend=1
width=-2
height=-2

View File

@ -0,0 +1,39 @@
/* Shader implementation of Scale2x is adapted from https://gist.github.com/singron/3161079 */
varying vec2 texCoord;
uniform sampler2D tex;
uniform vec2 texSize;
void main() {
// o = offset, the width of a pixel
vec2 o = 1.0 / texSize;
// texel arrangement
// A B C
// D E F
// G H I
vec4 B = texture2D(tex, texCoord + vec2( 0.0, o.y));
vec4 D = texture2D(tex, texCoord + vec2( -o.x, 0.0));
vec4 E = texture2D(tex, texCoord + vec2( 0.0, 0.0));
vec4 F = texture2D(tex, texCoord + vec2( o.x, 0.0));
vec4 H = texture2D(tex, texCoord + vec2( 0.0, -o.y));
vec2 p = texCoord * texSize;
// p = the texCoord within a pixel [0...1]
p = fract(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;
}
}
}

View File

@ -0,0 +1,11 @@
[shader]
name=Scale4x
author=singron, endrift
description=AdvanceMAME's Scale4x algorithm
passes=1
[pass.0]
fragmentShader=scale4x.fs
blend=1
width=-4
height=-4

View File

@ -0,0 +1,54 @@
/* Shader implementation of Scale2x is adapted from https://gist.github.com/singron/3161079 */
varying vec2 texCoord;
uniform sampler2D tex;
uniform vec2 texSize;
vec4 scale2x(vec4 pixels[5], vec2 p) {
// texel arrangement
// x 0 x
// 1 2 3
// x 4 x
// p = the texCoord within a pixel [0...1]
p = fract(p);
if (p.x > .5) {
if (p.y > .5) {
// Top Right
return pixels[0] == pixels[3] && pixels[0] != pixels[1] && pixels[3] != pixels[4] ? pixels[3] : pixels[2];
} else {
// Bottom Right
return pixels[4] == pixels[3] && pixels[1] != pixels[4] && pixels[0] != pixels[3] ? pixels[3] : pixels[2];
}
} else {
if (p.y > .5) {
// Top Left
return pixels[1] == pixels[0] && pixels[0] != pixels[3] && pixels[1] != pixels[4] ? pixels[1] : pixels[2];
} else {
// Bottom Left
return pixels[1] == pixels[4] && pixels[1] != pixels[0] && pixels[4] != pixels[3] ? pixels[1] : pixels[2];
}
}
}
vec4 scaleNeighborhood(vec2 p, vec2 x, vec2 o) {
vec4 neighborhood[5];
neighborhood[0] = texture2D(tex, texCoord + x + vec2( 0.0, o.y));
neighborhood[1] = texture2D(tex, texCoord + x + vec2(-o.x, 0.0));
neighborhood[2] = texture2D(tex, texCoord + x + vec2( 0.0, 0.0));
neighborhood[3] = texture2D(tex, texCoord + x + vec2( o.x, 0.0));
neighborhood[4] = texture2D(tex, texCoord + x + vec2( 0.0, -o.y));
return scale2x(neighborhood, p + x * texSize);
}
void main() {
// o = offset, the width of a pixel
vec2 o = 1.0 / texSize;
vec2 p = texCoord * texSize;
vec4 pixels[5];
pixels[0] = scaleNeighborhood(p, vec2( 0.0, o.y / 2.0), o);
pixels[1] = scaleNeighborhood(p, vec2(-o.x / 2.0, 0.0), o);
pixels[2] = scaleNeighborhood(p, vec2( 0.0, 0.0), o);
pixels[3] = scaleNeighborhood(p, vec2( o.x / 2.0, 0.0), o);
pixels[4] = scaleNeighborhood(p, vec2( 0.0, -o.y / 2.0), o);
gl_FragColor = scale2x(pixels, p * 2.0);
}