From 52afba21d10c1e244ba364249050baf381cc5e9a Mon Sep 17 00:00:00 2001 From: Lior Halphon Date: Tue, 14 Jun 2016 14:11:37 +0300 Subject: [PATCH] Added HQ2x shader, upgrading to OpenGL 3 --- Cocoa/GBPreferencesWindow.m | 1 + Cocoa/GBShader.m | 49 ++++++++++++------- Cocoa/GBView.m | 24 ++++++++++ Cocoa/Preferences.xib | 3 +- Shaders/AAOmniScale.fsh | 12 ++--- Shaders/AAScale2x.fsh | 22 ++++----- Shaders/AAScale4x.fsh | 22 ++++----- Shaders/Bilinear.fsh | 10 ++-- Shaders/HQ2x.fsh | 93 +++++++++++++++++++++++++++++++++++++ Shaders/MasterShader.fsh | 7 ++- Shaders/NearestNeighbor.fsh | 4 +- Shaders/OmniScale.fsh | 12 ++--- Shaders/Scale2x.fsh | 20 ++++---- Shaders/Scale4x.fsh | 20 ++++---- Shaders/SmoothBilinear.fsh | 10 ++-- 15 files changed, 224 insertions(+), 85 deletions(-) create mode 100644 Shaders/HQ2x.fsh diff --git a/Cocoa/GBPreferencesWindow.m b/Cocoa/GBPreferencesWindow.m index d7dd576..be32e50 100644 --- a/Cocoa/GBPreferencesWindow.m +++ b/Cocoa/GBPreferencesWindow.m @@ -23,6 +23,7 @@ @"Scale4x", @"AAScale2x", @"AAScale4x", + @"HQ2x", @"OmniScale", @"AAOmniScale", ]; diff --git a/Cocoa/GBShader.m b/Cocoa/GBShader.m index d0922e7..200d077 100644 --- a/Cocoa/GBShader.m +++ b/Cocoa/GBShader.m @@ -1,15 +1,18 @@ #import "GBShader.h" -#import +#import /* Loosely based of https://www.raywenderlich.com/70208/opengl-es-pixel-shaders-tutorial + + This code probably makes no sense after I upgraded it to OpenGL 3, since OpenGL makes aboslute no sense and has zero + helpful documentation. */ static NSString * const vertex_shader = @"\n\ -attribute vec2 aPosition;\n\ -\n\ +#version 150 \n\ +in vec4 aPosition;\n\ void main(void) {\n\ - gl_Position = vec4(aPosition, 0., 1.);\n\ + gl_Position = aPosition;\n\ }\n\ "; @@ -69,8 +72,9 @@ void main(void) {\n\ mix_previous_uniform = glGetUniformLocation(program, "uMixPrevious"); - // Configure OpenGL ES - [self configureOpenGLES]; + // Configure OpenGL + [self configureOpenGL]; + } return self; } @@ -90,23 +94,38 @@ void main(void) {\n\ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 160, 144, 0, GL_RGBA, GL_UNSIGNED_BYTE, previous); glUniform1i(previous_texture_uniform, 1); } + glBindFragDataLocation(program, 0, "frag_color"); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); } -- (void)configureOpenGLES +- (void)configureOpenGL { // Program + glUseProgram(program); + GLuint vao; + glGenVertexArrays(1, &vao); + glBindVertexArray(vao); + + GLuint vbo; + glGenBuffers(1, &vbo); + // Attributes - glEnableVertexAttribArray(position_attribute); - static GLfloat const quad[8] = { - -1.f, -1.f, - -1.f, +1.f, - +1.f, -1.f, - +1.f, +1.f, + + + static GLfloat const quad[16] = { + -1.f, -1.f, 0, 1, + -1.f, +1.f, 0, 1, + +1.f, -1.f, 0, 1, + +1.f, +1.f, 0, 1, }; - glVertexAttribPointer(position_attribute, 2, GL_FLOAT, GL_FALSE, 0, quad); + + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW); + glEnableVertexAttribArray(position_attribute); + glVertexAttribPointer(position_attribute, 4, GL_FLOAT, GL_FALSE, 0, 0); } + (GLuint)programWithVertexShader:(NSString*)vsh fragmentShader:(NSString*)fsh @@ -129,7 +148,6 @@ void main(void) {\n\ glGetProgramInfoLog(program, sizeof(messages), 0, &messages[0]); NSLog(@"%@:- GLSL Program Error: %s", self, messages); } - // Delete shaders glDeleteShader(vertex_shader); glDeleteShader(fragment_shader); @@ -162,7 +180,6 @@ void main(void) {\n\ glGetShaderInfoLog(shader, sizeof(messages), 0, &messages[0]); NSLog(@"%@:- GLSL Shader Error: %s", self, messages); } - return shader; } diff --git a/Cocoa/GBView.m b/Cocoa/GBView.m index 86adccc..b101634 100644 --- a/Cocoa/GBView.m +++ b/Cocoa/GBView.m @@ -12,6 +12,30 @@ static GBShader *shader = nil; unsigned char current_buffer; } +- (void) awakeFromNib +{ + NSOpenGLPixelFormatAttribute attrs[] = + { + NSOpenGLPFAOpenGLProfile, + NSOpenGLProfileVersion3_2Core, + 0 + }; + + NSOpenGLPixelFormat *pf = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs] ; + + if (!pf) + { + NSLog(@"No OpenGL pixel format"); + } + + NSOpenGLContext* context = [[NSOpenGLContext alloc] initWithFormat:pf shareContext:nil] ; + + [self setPixelFormat:pf]; + + [self setOpenGLContext:context]; +} + + - (void) _init { image_buffers[0] = malloc(160 * 144 * 4); diff --git a/Cocoa/Preferences.xib b/Cocoa/Preferences.xib index 99066ad..8f0a6b2 100644 --- a/Cocoa/Preferences.xib +++ b/Cocoa/Preferences.xib @@ -38,7 +38,7 @@ - + @@ -54,6 +54,7 @@ + diff --git a/Shaders/AAOmniScale.fsh b/Shaders/AAOmniScale.fsh index 53caa1f..6f325ac 100644 --- a/Shaders/AAOmniScale.fsh +++ b/Shaders/AAOmniScale.fsh @@ -8,10 +8,10 @@ vec4 omniScale(sampler2D image, vec2 texCoord) { vec2 pixel = texCoord * textureDimensions - vec2(0.5, 0.5); - vec4 q11 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q12 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); - vec4 q21 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q22 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); + 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); @@ -25,7 +25,7 @@ vec4 omniScale(sampler2D image, vec2 texCoord) int diagonalBias = 0; for (float y = -1.0; y < 3.0; y++) { for (float x = -1.0; x < 3.0; x++) { - vec4 color = texture2D(image, (pixel + vec2(x, y)) / textureDimensions); + vec4 color = texture(image, (pixel + vec2(x, y)) / textureDimensions); if (color == q11) diagonalBias++; if (color == q12) diagonalBias--; } @@ -104,7 +104,7 @@ vec4 omniScale(sampler2D image, vec2 texCoord) return q22; } -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; vec2 pixel = vec2(1.0, 1.0) / uResolution; diff --git a/Shaders/AAScale2x.fsh b/Shaders/AAScale2x.fsh index 6f4e46e..bdd8403 100644 --- a/Shaders/AAScale2x.fsh +++ b/Shaders/AAScale2x.fsh @@ -8,15 +8,15 @@ vec4 scale2x(sampler2D image) // A B C // D E F // G H I - vec4 A = texture2D(image, texCoord + vec2( -o.x, o.y)); - vec4 B = texture2D(image, texCoord + vec2( 0, o.y)); - vec4 C = texture2D(image, texCoord + vec2( o.x, o.y)); - vec4 D = texture2D(image, texCoord + vec2( -o.x, 0)); - vec4 E = texture2D(image, texCoord + vec2( 0, 0)); - vec4 F = texture2D(image, texCoord + vec2( o.x, 0)); - vec4 G = texture2D(image, texCoord + vec2( -o.x, -o.y)); - vec4 H = texture2D(image, texCoord + vec2( 0, -o.y)); - vec4 I = texture2D(image, texCoord + vec2( o.x, -o.y)); + vec4 A = texture(image, texCoord + vec2( -o.x, o.y)); + vec4 B = texture(image, texCoord + vec2( 0, o.y)); + vec4 C = texture(image, texCoord + vec2( o.x, o.y)); + vec4 D = texture(image, texCoord + vec2( -o.x, 0)); + vec4 E = texture(image, texCoord + vec2( 0, 0)); + vec4 F = texture(image, texCoord + vec2( o.x, 0)); + vec4 G = texture(image, texCoord + vec2( -o.x, -o.y)); + vec4 H = texture(image, texCoord + vec2( 0, -o.y)); + vec4 I = texture(image, texCoord + vec2( o.x, -o.y)); vec2 p = texCoord * textureDimensions; // p = the position within a pixel [0...1] p = fract(p); @@ -39,9 +39,9 @@ vec4 scale2x(sampler2D image) } } -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; - return mix(texture2D(image, texCoord), scale2x(image), 0.5); + return mix(texture(image, texCoord), scale2x(image), 0.5); } \ No newline at end of file diff --git a/Shaders/AAScale4x.fsh b/Shaders/AAScale4x.fsh index 993c319..83380d1 100644 --- a/Shaders/AAScale4x.fsh +++ b/Shaders/AAScale4x.fsh @@ -6,15 +6,15 @@ vec4 scale2x(sampler2D image, vec2 texCoord) // A B C // D E F // G H I - vec4 A = texture2D(image, texCoord + vec2( -o.x, o.y)); - vec4 B = texture2D(image, texCoord + vec2( 0, o.y)); - vec4 C = texture2D(image, texCoord + vec2( o.x, o.y)); - vec4 D = texture2D(image, texCoord + vec2( -o.x, 0)); - vec4 E = texture2D(image, texCoord + vec2( 0, 0)); - vec4 F = texture2D(image, texCoord + vec2( o.x, 0)); - vec4 G = texture2D(image, texCoord + vec2( -o.x, -o.y)); - vec4 H = texture2D(image, texCoord + vec2( 0, -o.y)); - vec4 I = texture2D(image, texCoord + vec2( o.x, -o.y)); + vec4 A = texture(image, texCoord + vec2( -o.x, o.y)); + vec4 B = texture(image, texCoord + vec2( 0, o.y)); + vec4 C = texture(image, texCoord + vec2( o.x, o.y)); + vec4 D = texture(image, texCoord + vec2( -o.x, 0)); + vec4 E = texture(image, texCoord + vec2( 0, 0)); + vec4 F = texture(image, texCoord + vec2( o.x, 0)); + vec4 G = texture(image, texCoord + vec2( -o.x, -o.y)); + vec4 H = texture(image, texCoord + vec2( 0, -o.y)); + vec4 I = texture(image, texCoord + vec2( o.x, -o.y)); vec2 p = texCoord * textureDimensions; // p = the position within a pixel [0...1] p = fract(p); @@ -39,10 +39,10 @@ vec4 scale2x(sampler2D image, vec2 texCoord) vec4 aaScale2x(sampler2D image, vec2 texCoord) { - return mix(texture2D(image, texCoord), scale2x(image, texCoord), 0.5); + return mix(texture(image, texCoord), scale2x(image, texCoord), 0.5); } -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { // o = offset, the width of a pixel vec2 o = 1.0 / (textureDimensions * 2.); diff --git a/Shaders/Bilinear.fsh b/Shaders/Bilinear.fsh index dfe1b1e..a519e12 100644 --- a/Shaders/Bilinear.fsh +++ b/Shaders/Bilinear.fsh @@ -1,13 +1,13 @@ -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; vec2 pixel = texCoord * textureDimensions - vec2(0.5, 0.5); - vec4 q11 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q12 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); - vec4 q21 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q22 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); + 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)); vec4 r1 = mix(q11, q21, fract(pixel.x)); vec4 r2 = mix(q12, q22, fract(pixel.x)); diff --git a/Shaders/HQ2x.fsh b/Shaders/HQ2x.fsh new file mode 100644 index 0000000..a729b5e --- /dev/null +++ b/Shaders/HQ2x.fsh @@ -0,0 +1,93 @@ +/* Based on this (really good) article: http://blog.pkh.me/p/19-butchering-hqx-scaling-filters.html */ + +/* Todo: Add the real YUV difference from HQ2x*/ +bool is_different(vec4 a, vec4 b) +{ + return length(a - b) > 0.15; +} + +#define P(m, r) ((pattern & (m)) == (r)) + +vec4 interp_2px(vec4 c1, float w1, vec4 c2, float w2) +{ + return (c1 * w1 + c2 * w2) / (w1 + w2); +} + +vec4 interp_3px(vec4 c1, float w1, vec4 c2, float w2, vec4 c3, float w3) +{ + return (c1 * w1 + c2 * w2 + c3 * w3) / (w1 + w2 + w3); +} + +vec4 scale(sampler2D image) +{ + // o = offset, the width of a pixel + vec2 o = 1.0 / textureDimensions; + vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; + + /* We always calculate the top left pixel. If we need a different pixel, we flip the image */ + + // p = the position within a pixel [0...1] + vec2 p = fract(texCoord * textureDimensions); + + if (p.x > 0.5) o.x = -o.x; + if (p.y > 0.5) o.y = -o.y; + + + + vec4 w0 = texture(image, texCoord + vec2( -o.x, -o.y)); + vec4 w1 = texture(image, texCoord + vec2( 0, -o.y)); + vec4 w2 = texture(image, texCoord + vec2( o.x, -o.y)); + vec4 w3 = texture(image, texCoord + vec2( -o.x, 0)); + vec4 w4 = texture(image, texCoord + vec2( 0, 0)); + vec4 w5 = texture(image, texCoord + vec2( o.x, 0)); + vec4 w6 = texture(image, texCoord + vec2( -o.x, o.y)); + vec4 w7 = texture(image, texCoord + vec2( 0, o.y)); + vec4 w8 = texture(image, texCoord + vec2( o.x, o.y)); + + int pattern = 0; + if (is_different(w0, w4)) pattern |= 1; + if (is_different(w1, w4)) pattern |= 2; + if (is_different(w2, w4)) pattern |= 4; + if (is_different(w3, w4)) pattern |= 8; + if (is_different(w5, w4)) pattern |= 16; + if (is_different(w6, w4)) pattern |= 32; + if (is_different(w7, w4)) pattern |= 64; + if (is_different(w8, w4)) pattern |= 128; + + if ((P(0xbf,0x37) || P(0xdb,0x13)) && is_different(w1, w5)) + return interp_2px(w4, 3.0, w3, 1.0); + if ((P(0xdb,0x49) || P(0xef,0x6d)) && is_different(w7, w3)) + return interp_2px(w4, 3.0, w1, 1.0); + if ((P(0x0b,0x0b) || P(0xfe,0x4a) || P(0xfe,0x1a)) && is_different(w3, w1)) + return 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)) + return interp_2px(w4, 3.0, w0, 1.0); + if (P(0x0b,0x08)) + return interp_3px(w4, 2.0, w0, 1.0, w1, 1.0); + if (P(0x0b,0x02)) + return interp_3px(w4, 2.0, w0, 1.0, w3, 1.0); + if (P(0x2f,0x2f)) + return interp_3px(w4, 1.04, w3, 1.0, w1, 1.0); + if (P(0xbf,0x37) || P(0xdb,0x13)) + return interp_3px(w4, 5.0, w1, 2.0, w3, 1.0); + if (P(0xdb,0x49) || P(0xef,0x6d)) + return interp_3px(w4, 5.0, w3, 2.0, w1, 1.0); + if (P(0x1b,0x03) || P(0x4f,0x43) || P(0x8b,0x83) || P(0x6b,0x43)) + return interp_2px(w4, 3.0, w3, 1.0); + if (P(0x4b,0x09) || P(0x8b,0x89) || P(0x1f,0x19) || P(0x3b,0x19)) + return interp_2px(w4, 3.0, w1, 1.0); + if (P(0x7e,0x2a) || P(0xef,0xab) || P(0xbf,0x8f) || P(0x7e,0x0e)) + return interp_3px(w4, 2.0, w3, 3.0, w1, 3.0); + if (P(0xfb,0x6a) || P(0x6f,0x6e) || P(0x3f,0x3e) || P(0xfb,0xfa) || + P(0xdf,0xde) || P(0xdf,0x1e)) + return interp_2px(w4, 3.0, w0, 1.0); + if (P(0x0a,0x00) || P(0x4f,0x4b) || P(0x9f,0x1b) || P(0x2f,0x0b) || + P(0xbe,0x0a) || P(0xee,0x0a) || P(0x7e,0x0a) || P(0xeb,0x4b) || + P(0x3b,0x1b)) + return interp_3px(w4, 2.0, w3, 1.0, w1, 1.0); + + return interp_3px(w4, 6.0, w3, 1.0, w1, 1.0); +} \ No newline at end of file diff --git a/Shaders/MasterShader.fsh b/Shaders/MasterShader.fsh index 11a714d..642026e 100644 --- a/Shaders/MasterShader.fsh +++ b/Shaders/MasterShader.fsh @@ -1,3 +1,4 @@ +#version 150 uniform sampler2D image; uniform sampler2D previousImage; uniform bool uMixPrevious; @@ -5,14 +6,16 @@ uniform bool uMixPrevious; uniform vec2 uResolution; const vec2 textureDimensions = vec2(160, 144); +out vec4 frag_color; + #line 1 {filter} void main() { if (uMixPrevious) { - gl_FragColor = mix(filter(image), filter(previousImage), 0.5); + frag_color = mix(scale(image), scale(previousImage), 0.5); } else { - gl_FragColor = filter(image); + frag_color = scale(image); } } \ No newline at end of file diff --git a/Shaders/NearestNeighbor.fsh b/Shaders/NearestNeighbor.fsh index 75a6fc1..661e5fb 100644 --- a/Shaders/NearestNeighbor.fsh +++ b/Shaders/NearestNeighbor.fsh @@ -1,6 +1,6 @@ -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; - return texture2D(image, texCoord); + return texture(image, texCoord); } \ No newline at end of file diff --git a/Shaders/OmniScale.fsh b/Shaders/OmniScale.fsh index 1bfdd6c..cd4257d 100644 --- a/Shaders/OmniScale.fsh +++ b/Shaders/OmniScale.fsh @@ -4,16 +4,16 @@ float quickDistance(vec4 a, vec4 b) return abs(a.x - b.x) + abs(a.y - b.y) + abs(a.z - b.z); } -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; vec2 pixel = texCoord * textureDimensions - vec2(0.5, 0.5); - vec4 q11 = texture2D(image, (pixel ) / textureDimensions); - vec4 q12 = texture2D(image, (pixel + vec2(0.0, 1.0)) / textureDimensions); - vec4 q21 = texture2D(image, (pixel + vec2(1.0, 0.0)) / textureDimensions); - vec4 q22 = texture2D(image, (pixel + vec2(1.0, 1.0)) / textureDimensions); + vec4 q11 = texture(image, (pixel ) / textureDimensions); + vec4 q12 = texture(image, (pixel + vec2(0.0, 1.0)) / textureDimensions); + vec4 q21 = texture(image, (pixel + vec2(1.0, 0.0)) / textureDimensions); + vec4 q22 = texture(image, (pixel + vec2(1.0, 1.0)) / textureDimensions); vec2 pos = fract(pixel); @@ -27,7 +27,7 @@ vec4 filter(sampler2D image) int diagonalBias = 0; for (float y = -1.0; y < 3.0; y++) { for (float x = -1.0; x < 3.0; x++) { - vec4 color = texture2D(image, (pixel + vec2(x, y)) / textureDimensions); + vec4 color = texture(image, (pixel + vec2(x, y)) / textureDimensions); if (color == q11) diagonalBias++; if (color == q12) diagonalBias--; } diff --git a/Shaders/Scale2x.fsh b/Shaders/Scale2x.fsh index 38efe20..3968057 100644 --- a/Shaders/Scale2x.fsh +++ b/Shaders/Scale2x.fsh @@ -1,6 +1,6 @@ /* Shader implementation of Scale2x is adapted from https://gist.github.com/singron/3161079 */ -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { // o = offset, the width of a pixel vec2 o = 1.0 / textureDimensions; @@ -10,15 +10,15 @@ vec4 filter(sampler2D image) // A B C // D E F // G H I - vec4 A = texture2D(image, texCoord + vec2( -o.x, o.y)); - vec4 B = texture2D(image, texCoord + vec2( 0, o.y)); - vec4 C = texture2D(image, texCoord + vec2( o.x, o.y)); - vec4 D = texture2D(image, texCoord + vec2( -o.x, 0)); - vec4 E = texture2D(image, texCoord + vec2( 0, 0)); - vec4 F = texture2D(image, texCoord + vec2( o.x, 0)); - vec4 G = texture2D(image, texCoord + vec2( -o.x, -o.y)); - vec4 H = texture2D(image, texCoord + vec2( 0, -o.y)); - vec4 I = texture2D(image, texCoord + vec2( o.x, -o.y)); + vec4 A = texture(image, texCoord + vec2( -o.x, o.y)); + vec4 B = texture(image, texCoord + vec2( 0, o.y)); + vec4 C = texture(image, texCoord + vec2( o.x, o.y)); + vec4 D = texture(image, texCoord + vec2( -o.x, 0)); + vec4 E = texture(image, texCoord + vec2( 0, 0)); + vec4 F = texture(image, texCoord + vec2( o.x, 0)); + vec4 G = texture(image, texCoord + vec2( -o.x, -o.y)); + vec4 H = texture(image, texCoord + vec2( 0, -o.y)); + vec4 I = texture(image, texCoord + vec2( o.x, -o.y)); vec2 p = texCoord * textureDimensions; // p = the position within a pixel [0...1] p = fract(p); diff --git a/Shaders/Scale4x.fsh b/Shaders/Scale4x.fsh index 6c5667f..1c763fc 100644 --- a/Shaders/Scale4x.fsh +++ b/Shaders/Scale4x.fsh @@ -6,15 +6,15 @@ vec4 scale2x(sampler2D image, vec2 texCoord) // A B C // D E F // G H I - vec4 A = texture2D(image, texCoord + vec2( -o.x, o.y)); - vec4 B = texture2D(image, texCoord + vec2( 0, o.y)); - vec4 C = texture2D(image, texCoord + vec2( o.x, o.y)); - vec4 D = texture2D(image, texCoord + vec2( -o.x, 0)); - vec4 E = texture2D(image, texCoord + vec2( 0, 0)); - vec4 F = texture2D(image, texCoord + vec2( o.x, 0)); - vec4 G = texture2D(image, texCoord + vec2( -o.x, -o.y)); - vec4 H = texture2D(image, texCoord + vec2( 0, -o.y)); - vec4 I = texture2D(image, texCoord + vec2( o.x, -o.y)); + vec4 A = texture(image, texCoord + vec2( -o.x, o.y)); + vec4 B = texture(image, texCoord + vec2( 0, o.y)); + vec4 C = texture(image, texCoord + vec2( o.x, o.y)); + vec4 D = texture(image, texCoord + vec2( -o.x, 0)); + vec4 E = texture(image, texCoord + vec2( 0, 0)); + vec4 F = texture(image, texCoord + vec2( o.x, 0)); + vec4 G = texture(image, texCoord + vec2( -o.x, -o.y)); + vec4 H = texture(image, texCoord + vec2( 0, -o.y)); + vec4 I = texture(image, texCoord + vec2( o.x, -o.y)); vec2 p = texCoord * textureDimensions; // p = the position within a pixel [0...1] vec4 R; @@ -38,7 +38,7 @@ vec4 scale2x(sampler2D image, vec2 texCoord) } } -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { // o = offset, the width of a pixel vec2 o = 1.0 / (textureDimensions * 2.); diff --git a/Shaders/SmoothBilinear.fsh b/Shaders/SmoothBilinear.fsh index 391cba4..443a10f 100644 --- a/Shaders/SmoothBilinear.fsh +++ b/Shaders/SmoothBilinear.fsh @@ -1,13 +1,13 @@ -vec4 filter(sampler2D image) +vec4 scale(sampler2D image) { vec2 texCoord = vec2(gl_FragCoord.x, uResolution.y - gl_FragCoord.y) / uResolution; vec2 pixel = texCoord * textureDimensions - vec2(0.5, 0.5); - vec4 q11 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q12 = texture2D(image, vec2(floor(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); - vec4 q21 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, floor(pixel.y) / textureDimensions.y)); - vec4 q22 = texture2D(image, vec2(ceil(pixel.x) / textureDimensions.x, ceil(pixel.y) / textureDimensions.y)); + 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 smooth = smoothstep(0., 1., fract(pixel));