diff --git a/retro/aann.slangp b/retro/aann.slangp new file mode 100644 index 0000000..9d3bd69 --- /dev/null +++ b/retro/aann.slangp @@ -0,0 +1,5 @@ +shaders = 1 + +shader0 = "shaders/aann.slang" +filter_linear0 = true +scale_type_0 = source diff --git a/retro/shaders/aann.slang b/retro/shaders/aann.slang new file mode 100644 index 0000000..5a83e45 --- /dev/null +++ b/retro/shaders/aann.slang @@ -0,0 +1,105 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +// AntiAliased Nearest Neighbor +// by jimbo1qaz and wareya + +#define NOT(fl) (1-fl) +#define YES(fl) fl + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +vec4 vpow(vec4 n, float e) +{ + return vec4(pow(n.x, e), pow(n.y, e), pow(n.z, e), pow(n.w, e)); +} + +vec4 getLQV(vec3 mine) { + return vec4 + ( mine.r + , mine.g + , mine.b + ,(mine.r + mine.g + mine.b)/3.0); +} + +vec3 fromLQV(vec4 mine) { + float f = mine.w/(mine.r + mine.g + mine.b)*3.0; + return vec3(mine.rgb)*f; +} + +vec3 percent(float ssize, float tsize, float coord) { + float minfull = (coord*tsize - 0.5) /tsize*ssize; + float maxfull = (coord*tsize + 0.5) /tsize*ssize; + + float realfull = floor(maxfull); + + if (minfull > realfull) { + return vec3(1, (realfull+0.5)/ssize, (realfull+0.5)/ssize); + } + + return vec3( + (maxfull - realfull) / (maxfull - minfull), + (realfull-0.5) / ssize, + (realfull+0.5) / ssize + ); +} + +void main() +{ + float cheapsrgb = 2.1; + float gamma = 3.0; + vec3 xstuff = percent(global.SourceSize.x, global.OutputSize.x, vTexCoord.x); + vec3 ystuff = percent(global.SourceSize.y, global.OutputSize.y, vTexCoord.y); + + float xkeep = xstuff[0]; + float ykeep = ystuff[0]; + + // get points to interpolate across, in linear rgb + vec4 a = getLQV(vpow(texture(Source,vec2(xstuff[1],ystuff[1])), cheapsrgb).rgb); + vec4 b = getLQV(vpow(texture(Source,vec2(xstuff[2],ystuff[1])), cheapsrgb).rgb); + vec4 c = getLQV(vpow(texture(Source,vec2(xstuff[1],ystuff[2])), cheapsrgb).rgb); + vec4 d = getLQV(vpow(texture(Source,vec2(xstuff[2],ystuff[2])), cheapsrgb).rgb); + + // use perceptual gamma for luminance component + a.w = pow(a.w, 1.0/gamma); + b.w = pow(b.w, 1.0/gamma); + c.w = pow(c.w, 1.0/gamma); + d.w = pow(d.w, 1.0/gamma); + + // interpolate + vec4 gammaLQVresult = + NOT(xkeep)*NOT(ykeep)*a + + YES(xkeep)*NOT(ykeep)*b + + NOT(xkeep)*YES(ykeep)*c + + YES(xkeep)*YES(ykeep)*d; + + // change luminance gamma back to linear + vec4 LQVresult = gammaLQVresult; + LQVresult.w = pow(gammaLQVresult.w, gamma); + + // convert back to srgb; lqv -> lrgb -> srgb + vec4 c1 = vpow(vec4(fromLQV(LQVresult), 1.0), 1.0/cheapsrgb); + + FragColor = vec4(c1); +} \ No newline at end of file diff --git a/retro/shaders/pixellate.slang b/retro/shaders/pixellate.slang new file mode 100644 index 0000000..86fd057 --- /dev/null +++ b/retro/shaders/pixellate.slang @@ -0,0 +1,55 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +void main() +{ + vec2 texelSize = 1.0 / global.SourceSize.xy; + + vec2 range = vec2(abs(global.SourceSize.z / (global.OutputSize.x * global.SourceSize.x)), abs(global.SourceSize.w / (global.OutputSize.y * global.SourceSize.y))); + range = range / 2.0 * 0.999; + + float left = vTexCoord.x - range.x; + float top = vTexCoord.y + range.y; + float right = vTexCoord.x + range.x; + float bottom = vTexCoord.y - range.y; + + vec3 topLeftColor = texture(Source, (floor(vec2(left, top) / texelSize) + 0.5) * texelSize).rgb; + vec3 bottomRightColor = texture(Source, (floor(vec2(right, bottom) / texelSize) + 0.5) * texelSize).rgb; + vec3 bottomLeftColor = texture(Source, (floor(vec2(left, bottom) / texelSize) + 0.5) * texelSize).rgb; + vec3 topRightColor = texture(Source, (floor(vec2(right, top) / texelSize) + 0.5) * texelSize).rgb; + + vec2 border = clamp(round(vTexCoord / texelSize) * texelSize, vec2(left, bottom), vec2(right, top)); + + float totalArea = 4.0 * range.x * range.y; + + vec3 averageColor; + averageColor = ((border.x - left) * (top - border.y) / totalArea) * topLeftColor; + averageColor += ((right - border.x) * (border.y - bottom) / totalArea) * bottomRightColor; + averageColor += ((border.x - left) * (border.y - bottom) / totalArea) * bottomLeftColor; + averageColor += ((right - border.x) * (top - border.y) / totalArea) * topRightColor; + + FragColor = vec4(averageColor, 1.0); +} \ No newline at end of file diff --git a/retro/shaders/sharp-bilinear.slang b/retro/shaders/sharp-bilinear.slang new file mode 100644 index 0000000..96c2112 --- /dev/null +++ b/retro/shaders/sharp-bilinear.slang @@ -0,0 +1,54 @@ +#version 450 + +layout(std140, set = 0, binding = 0) uniform UBO +{ + mat4 MVP; + vec4 OutputSize; + vec4 OriginalSize; + vec4 SourceSize; +} global; + +/* + * sharp-bilinear.cg + * Author: Themaister + * License: Public domain + * + * Does a bilinear stretch, with a preapplied Nx nearest-neighbor scale, giving a + * sharper image than plain bilinear. + */ + + #define SHARP_BILINEAR_PRE_SCALE 4.0 + +#pragma stage vertex +layout(location = 0) in vec4 Position; +layout(location = 1) in vec2 TexCoord; +layout(location = 0) out vec2 vTexCoord; + +void main() +{ + gl_Position = global.MVP * Position; + vTexCoord = TexCoord; +} + +#pragma stage fragment +layout(location = 0) in vec2 vTexCoord; +layout(location = 0) out vec4 FragColor; +layout(set = 0, binding = 2) uniform sampler2D Source; + +void main() +{ + vec2 texel = vTexCoord * global.SourceSize.xy; + vec2 texel_floored = floor(texel); + vec2 s = fract(texel); + float region_range = 0.5 - 0.5 / SHARP_BILINEAR_PRE_SCALE; + + // Figure out where in the texel to sample to get correct pre-scaled bilinear. + // Uses the hardware bilinear interpolator to avoid having to sample 4 times manually. + + vec2 center_dist = s - 0.5; + vec2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * SHARP_BILINEAR_PRE_SCALE + 0.5; + + vec2 mod_texel = texel_floored + f; + + FragColor = vec4(texture(Source, mod_texel / global.SourceSize.xy).rgb, 1.0); +} \ No newline at end of file diff --git a/retro/sharp-bilinear.slangp b/retro/sharp-bilinear.slangp new file mode 100644 index 0000000..636d857 --- /dev/null +++ b/retro/sharp-bilinear.slangp @@ -0,0 +1,5 @@ +shaders = 1 + +shader0 = "shaders/sharp-bilinear.slang" +filter_linear0 = true +scale_type_0 = source