add omniscale-legacy shader and fix coord issue with ddt

This commit is contained in:
hunterk 2017-02-09 16:36:52 -06:00
parent 6d7617f9d8
commit f951ebabeb
4 changed files with 463 additions and 1 deletions

View file

@ -80,7 +80,7 @@ layout(location = 2) out vec2 loc;
void main()
{
gl_Position = global.MVP * Position;
vTexCoord = TexCoord * 1.00001;
vTexCoord = TexCoord * 1.0001;
float2 ps = float2(params.SourceSize.z, params.SourceSize.w);
float dx = ps.x;

View file

@ -0,0 +1,6 @@
shaders = 1
shader0 = shaders/omniscale-legacy.slang
scale_type0 = source
filter_linear0 = false
scale0 = 5.0

View file

@ -0,0 +1,180 @@
#version 450
// OmniScale
// by Lior Halphon
// ported to slang format by hunterk
/*
MIT License
Copyright (c) 2015-2016 Lior Halphon
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
layout(push_constant) uniform Push
{
vec4 SourceSize;
vec4 OriginalSize;
vec4 OutputSize;
uint FrameCount;
} params;
layout(std140, set = 0, binding = 0) uniform UBO
{
mat4 MVP;
} global;
float quickDistance(vec4 a, vec4 b)
{
return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z);
}
#define uResolution params.OutputSize.xy
#define textureDimensions params.SourceSize.xy
vec4 omniScale(sampler2D image, vec2 texCoord)
{
vec2 pixel = texCoord * textureDimensions - vec2(0.5, 0.5);
vec4 q11 = texture(image, vec2(floor(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y));
vec4 q12 = texture(image, vec2(floor(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y));
vec4 q21 = texture(image, vec2(ceil(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y));
vec4 q22 = texture(image, vec2(ceil(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y));
vec2 pos = fract(pixel);
/* Special handling for diaonals */
bool hasDownDiagonal = false;
bool hasUpDiagonal = false;
if (q12 == q21 && q11 != q22) hasUpDiagonal = true;
else if (q12 != q21 && q11 == q22) hasDownDiagonal = true;
else if (q12 == q21 && q11 == q22) {
if (q11 == q12) return q11;
int diagonalBias = 0;
for (float y = -1.0; y < 3.0; y++) {
for (float x = -1.0; x < 3.0; x++) {
vec4 color = texture(image, (pixel + vec2(x, y)) / textureDimensions);
if (color == q11) diagonalBias++;
if (color == q12) diagonalBias--;
}
}
if (diagonalBias <= 0) {
hasDownDiagonal = true;
}
if (diagonalBias >= 0) {
hasUpDiagonal = true;
}
}
if (hasUpDiagonal || hasDownDiagonal) {
vec4 downDiagonalResult, upDiagonalResult;
if (hasUpDiagonal) {
float diagonalPos = pos.x + pos.y;
if (diagonalPos < 0.5) {
upDiagonalResult = q11;
}
else if (diagonalPos > 1.5) {
upDiagonalResult = q22;
}
else {
upDiagonalResult = q12;
}
}
if (hasDownDiagonal) {
float diagonalPos = 1.0 - pos.x + pos.y;
if (diagonalPos < 0.5) {
downDiagonalResult = q21;
}
else if (diagonalPos > 1.5) {
downDiagonalResult = q12;
}
else {
downDiagonalResult = q11;
}
}
if (!hasUpDiagonal) return downDiagonalResult;
if (!hasDownDiagonal) return upDiagonalResult;
return mix(downDiagonalResult, upDiagonalResult, 0.5);
}
vec4 r1 = mix(q11, q21, fract(pos.x));
vec4 r2 = mix(q12, q22, fract(pos.x));
vec4 unqunatized = mix(r1, r2, fract(pos.y));
float q11d = quickDistance(unqunatized, q11);
float q21d = quickDistance(unqunatized, q21);
float q12d = quickDistance(unqunatized, q12);
float q22d = quickDistance(unqunatized, q22);
float best = min(q11d,
min(q21d,
min(q12d,
q22d)));
if (q11d == best) {
return q11;
}
if (q21d == best) {
return q21;
}
if (q12d == best) {
return q12;
}
return q22;
}
#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 * 1.0001;
}
#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 texCoord = vTexCoord;
vec2 pixel = vec2(1.0, 1.0) / uResolution;
// 4-pixel super sampling
vec4 q11 = omniScale(Source, texCoord + pixel * vec2(-0.25, -0.25));
vec4 q21 = omniScale(Source, texCoord + pixel * vec2(+0.25, -0.25));
vec4 q12 = omniScale(Source, texCoord + pixel * vec2(-0.25, +0.25));
vec4 q22 = omniScale(Source, texCoord + pixel * vec2(+0.25, +0.25));
FragColor = (q11 + q21 + q12 + q22) / 4.0;
}

View file

@ -0,0 +1,276 @@
#version 450
layout(push_constant) uniform Push
{
vec4 SourceSize;
vec4 OriginalSize;
vec4 OutputSize;
uint FrameCount;
} params;
layout(std140, set = 0, binding = 0) uniform UBO
{
mat4 MVP;
} global;
/* We use the same colorspace as the HQ algorithms. */
vec3 rgb_to_hq_colospace(vec4 rgb)
{
return vec3( 0.250 * rgb.r + 0.250 * rgb.g + 0.250 * rgb.b,
0.250 * rgb.r - 0.000 * rgb.g - 0.250 * rgb.b,
-0.125 * rgb.r + 0.250 * rgb.g - 0.125 * rgb.b);
}
bool is_different(vec4 a, vec4 b)
{
vec3 diff = abs(rgb_to_hq_colospace(a) - rgb_to_hq_colospace(b));
return diff.x > 0.125 || diff.y > 0.027 || diff.z > 0.031;
}
#define P(m, r) ((pattern & (m)) == (r))
#define uResolution params.OutputSize.xy
#define textureDimensions params.SourceSize.xy
#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 * 1.0001;
}
#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()
{
// o = offset, the width of a pixel
vec2 o = 1.0 / textureDimensions;
vec2 texCoord = vTexCoord;
/* We always calculate the top left quarter. If we need a different quarter, we flip our co-ordinates */
// p = the position within a pixel [0...1]
vec2 p = fract(texCoord * textureDimensions);
if (p.x > 0.5) {
o.x = -o.x;
p.x = 1.0 - p.x;
}
if (p.y > 0.5) {
o.y = -o.y;
p.y = 1.0 - p.y;
}
vec4 w0 = texture(Source, texCoord + vec2( -o.x, -o.y));
vec4 w1 = texture(Source, texCoord + vec2( 0, -o.y));
vec4 w2 = texture(Source, texCoord + vec2( o.x, -o.y));
vec4 w3 = texture(Source, texCoord + vec2( -o.x, 0));
vec4 w4 = texture(Source, texCoord + vec2( 0, 0));
vec4 w5 = texture(Source, texCoord + vec2( o.x, 0));
vec4 w6 = texture(Source, texCoord + vec2( -o.x, o.y));
vec4 w7 = texture(Source, texCoord + vec2( 0, o.y));
vec4 w8 = texture(Source, texCoord + vec2( o.x, o.y));
int pattern = 0;
if (is_different(w0, w4)) pattern |= 1 << 0;
if (is_different(w1, w4)) pattern |= 1 << 1;
if (is_different(w2, w4)) pattern |= 1 << 2;
if (is_different(w3, w4)) pattern |= 1 << 3;
if (is_different(w5, w4)) pattern |= 1 << 4;
if (is_different(w6, w4)) pattern |= 1 << 5;
if (is_different(w7, w4)) pattern |= 1 << 6;
if (is_different(w8, w4)) pattern |= 1 << 7;
if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5))
FragColor = mix(w4, w3, 0.5 - p.x);
if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3))
FragColor = mix(w4, w1, 0.5 - p.y);
if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1))
FragColor = w4;
if ((P(0x6f,0x2a) || P(0x5b,0x0a) || P(0xbf,0x3a) || P(0xdf,0x5a) ||
P(0x9f,0x8a) || P(0xcf,0x8a) || P(0xef,0x4e) || P(0x3f,0x0e) ||
P(0xfb,0x5a) || P(0xbb,0x8a) || P(0x7f,0x5a) || P(0xaf,0x8a) ||
P(0xeb,0x8a)) && is_different(w3, w1))
FragColor = mix(w4, mix(w4, w0, 0.5 - p.x), 0.5 - p.y);
if (P(0x0b,0x08))
FragColor = mix(mix(w0 * 0.375 + w1 * 0.25 + w4 * 0.375, w4 * 0.5 + w1 * 0.5, p.x * 2.0), w4, p.y * 2.0);
if (P(0x0b,0x02))
FragColor = mix(mix(w0 * 0.375 + w3 * 0.25 + w4 * 0.375, w4 * 0.5 + w3 * 0.5, p.y * 2.0), w4, p.x * 2.0);
if (P(0x2f,0x2f)) {
float dist = length(p - vec2(0.5));
float pixel_size = length(1.0 / (uResolution / textureDimensions));
if (dist < 0.5 - pixel_size / 2) {
FragColor = w4;
}
vec4 r;
if (is_different(w0, w1) || is_different(w0, w3)) {
r = mix(w1, w3, p.y - p.x + 0.5);
}
else {
r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
}
if (dist > 0.5 + pixel_size / 2) {
FragColor = r;
}
FragColor = mix(w4, r, (dist - 0.5 + pixel_size / 2) / pixel_size);
}
if (P(0xbf,0x37) || P(0xdb,0x13)) {
float dist = p.x - 2.0 * p.y;
float pixel_size = length(1.0 / (uResolution / textureDimensions)) * sqrt(5);
if (dist > pixel_size / 2) {
FragColor = w1;
}
vec4 r = mix(w3, w4, p.x + 0.5);
if (dist < -pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w1, (dist + pixel_size / 2) / pixel_size);
}
if (P(0xdb,0x49) || P(0xef,0x6d)) {
float dist = p.y - 2.0 * p.x;
float pixel_size = length(1.0 / (uResolution / textureDimensions)) * sqrt(5);
if (p.y - 2.0 * p.x > pixel_size / 2) {
FragColor = w3;
}
vec4 r = mix(w1, w4, p.x + 0.5);
if (dist < -pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w3, (dist + pixel_size / 2) / pixel_size);
}
if (P(0xbf,0x8f) || P(0x7e,0x0e)) {
float dist = p.x + 2.0 * p.y;
float pixel_size = length(1.0 / (uResolution / textureDimensions)) * sqrt(5);
if (dist > 1.0 + pixel_size / 2) {
FragColor = w4;
}
vec4 r;
if (is_different(w0, w1) || is_different(w0, w3)) {
r = mix(w1, w3, p.y - p.x + 0.5);
}
else {
r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
}
if (dist < 1.0 - pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w4, (dist + pixel_size / 2 - 1.0) / pixel_size);
}
if (P(0x7e,0x2a) || P(0xef,0xab)) {
float dist = p.y + 2.0 * p.x;
float pixel_size = length(1.0 / (uResolution / textureDimensions)) * sqrt(5);
if (p.y + 2.0 * p.x > 1.0 + pixel_size / 2) {
FragColor = w4;
}
vec4 r;
if (is_different(w0, w1) || is_different(w0, w3)) {
r = mix(w1, w3, p.y - p.x + 0.5);
}
else {
r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
}
if (dist < 1.0 - pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w4, (dist + pixel_size / 2 - 1.0) / pixel_size);
}
if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43))
FragColor = mix(w4, w3, 0.5 - p.x);
if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19))
FragColor = mix(w4, w1, 0.5 - p.y);
if (P(0xfb,0x6a) || P(0x6f,0x6e) || P(0x3f,0x3e) || P(0xfb,0xfa) ||
P(0xdf,0xde) || P(0xdf,0x1e))
FragColor = mix(w4, w0, (1.0 - p.x - p.y) / 2.0);
if (P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) ||
P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) ||
P(0x3b,0x1b)) {
float dist = p.x + p.y;
float pixel_size = length(1.0 / (uResolution / textureDimensions));
if (dist > 0.5 + pixel_size / 2) {
FragColor = w4;
}
vec4 r;
if (is_different(w0, w1) || is_different(w0, w3)) {
r = mix(w1, w3, p.y - p.x + 0.5);
}
else {
r = mix(mix(w1 * 0.375 + w0 * 0.25 + w3 * 0.375, w3, p.y * 2.0), w1, p.x * 2.0);
}
if (dist < 0.5 - pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w4, (dist + pixel_size / 2 - 0.5) / pixel_size);
}
if (P(0x0b,0x01))
FragColor = mix(mix(w4, w3, 0.5 - p.x), mix(w1, (w1 + w3) / 2.0, 0.5 - p.x), 0.5 - p.y);
if (P(0x0b,0x00))
FragColor = mix(mix(w4, w3, 0.5 - p.x), mix(w1, w0, 0.5 - p.x), 0.5 - p.y);
float dist = p.x + p.y;
float pixel_size = length(1.0 / (uResolution / textureDimensions));
if (dist > 0.5 + pixel_size / 2)
FragColor = w4;
/* We need more samples to "solve" this diagonal */
vec4 x0 = texture(Source, texCoord + vec2( -o.x * 2.0, -o.y * 2.0));
vec4 x1 = texture(Source, texCoord + vec2( -o.x , -o.y * 2.0));
vec4 x2 = texture(Source, texCoord + vec2( 0.0 , -o.y * 2.0));
vec4 x3 = texture(Source, texCoord + vec2( o.x , -o.y * 2.0));
vec4 x4 = texture(Source, texCoord + vec2( -o.x * 2.0, -o.y ));
vec4 x5 = texture(Source, texCoord + vec2( -o.x * 2.0, 0.0 ));
vec4 x6 = texture(Source, texCoord + vec2( -o.x * 2.0, o.y ));
if (is_different(x0, w4)) pattern |= 1 << 8;
if (is_different(x1, w4)) pattern |= 1 << 9;
if (is_different(x2, w4)) pattern |= 1 << 10;
if (is_different(x3, w4)) pattern |= 1 << 11;
if (is_different(x4, w4)) pattern |= 1 << 12;
if (is_different(x5, w4)) pattern |= 1 << 13;
if (is_different(x6, w4)) pattern |= 1 << 14;
int diagonal_bias = -7;
while (pattern != 0) {
diagonal_bias += pattern & 1;
pattern >>= 1;
}
if (diagonal_bias <= 0) {
vec4 r = mix(w1, w3, p.y - p.x + 0.5);
if (dist < 0.5 - pixel_size / 2) {
FragColor = r;
}
FragColor = mix(r, w4, (dist + pixel_size / 2 - 0.5) / pixel_size);
}
}