#version 330 in vec2 f_texCoord; out vec4 fragColor; uniform sampler2D tex; uniform vec2 min_uv; uniform vec2 max_uv; float c_x0 = -1.0; float c_x1 = 0.0; float c_x2 = 1.0; float c_x3 = 2.0; //======================================================================================= vec4 clampedSmaple(sampler2D tex, vec2 uv) { vec2 clmapedUv = clamp(uv, min_uv, max_uv); return texture2D(tex, clmapedUv); } //======================================================================================= vec3 CubicHermite (vec3 A, vec3 B, vec3 C, vec3 D, float t) { float t2 = t*t; float t3 = t*t*t; vec3 a = -A/2.0 + (3.0*B)/2.0 - (3.0*C)/2.0 + D/2.0; vec3 b = A - (5.0*B)/2.0 + 2.0*C - D / 2.0; vec3 c = -A/2.0 + C/2.0; vec3 d = B; return a*t3 + b*t2 + c*t + d; } //======================================================================================= vec3 BicubicHermiteTextureSample (sampler2D sam, vec2 P, float textureSize) { float onePixel = (1.0 / textureSize); float twoPixels = (2.0 / textureSize); vec2 pixel = P * textureSize + 0.5; vec2 frac = fract(pixel); pixel = floor(pixel) / textureSize - vec2(onePixel/2.0); vec3 C00 = clampedSmaple(sam, pixel + vec2(-onePixel ,-onePixel)).rgb; vec3 C10 = clampedSmaple(sam, pixel + vec2( 0.0 ,-onePixel)).rgb; vec3 C20 = clampedSmaple(sam, pixel + vec2( onePixel ,-onePixel)).rgb; vec3 C30 = clampedSmaple(sam, pixel + vec2( twoPixels,-onePixel)).rgb; vec3 C01 = clampedSmaple(sam, pixel + vec2(-onePixel , 0.0)).rgb; vec3 C11 = clampedSmaple(sam, pixel + vec2( 0.0 , 0.0)).rgb; vec3 C21 = clampedSmaple(sam, pixel + vec2( onePixel , 0.0)).rgb; vec3 C31 = clampedSmaple(sam, pixel + vec2( twoPixels, 0.0)).rgb; vec3 C02 = clampedSmaple(sam, pixel + vec2(-onePixel , onePixel)).rgb; vec3 C12 = clampedSmaple(sam, pixel + vec2( 0.0 , onePixel)).rgb; vec3 C22 = clampedSmaple(sam, pixel + vec2( onePixel , onePixel)).rgb; vec3 C32 = clampedSmaple(sam, pixel + vec2( twoPixels, onePixel)).rgb; vec3 C03 = clampedSmaple(sam, pixel + vec2(-onePixel , twoPixels)).rgb; vec3 C13 = clampedSmaple(sam, pixel + vec2( 0.0 , twoPixels)).rgb; vec3 C23 = clampedSmaple(sam, pixel + vec2( onePixel , twoPixels)).rgb; vec3 C33 = clampedSmaple(sam, pixel + vec2( twoPixels, twoPixels)).rgb; vec3 CP0X = CubicHermite(C00, C10, C20, C30, frac.x); vec3 CP1X = CubicHermite(C01, C11, C21, C31, frac.x); vec3 CP2X = CubicHermite(C02, C12, C22, C32, frac.x); vec3 CP3X = CubicHermite(C03, C13, C23, C33, frac.x); return CubicHermite(CP0X, CP1X, CP2X, CP3X, frac.y); } //======================================================================================= vec3 CubicLagrange (vec3 A, vec3 B, vec3 C, vec3 D, float t) { return A * ( (t - c_x1) / (c_x0 - c_x1) * (t - c_x2) / (c_x0 - c_x2) * (t - c_x3) / (c_x0 - c_x3) ) + B * ( (t - c_x0) / (c_x1 - c_x0) * (t - c_x2) / (c_x1 - c_x2) * (t - c_x3) / (c_x1 - c_x3) ) + C * ( (t - c_x0) / (c_x2 - c_x0) * (t - c_x1) / (c_x2 - c_x1) * (t - c_x3) / (c_x2 - c_x3) ) + D * ( (t - c_x0) / (c_x3 - c_x0) * (t - c_x1) / (c_x3 - c_x1) * (t - c_x2) / (c_x3 - c_x2) ); } //======================================================================================= vec3 BicubicLagrangeTextureSample (sampler2D sam, vec2 P, float textureSize) { float onePixel = (1.0 / textureSize); float twoPixels = (2.0 / textureSize); vec2 pixel = P * textureSize + 0.5; vec2 frac = fract(pixel); pixel = floor(pixel) / textureSize - vec2(onePixel/2.0); vec3 C00 = clampedSmaple(sam, pixel + vec2(-onePixel ,-onePixel)).rgb; vec3 C10 = clampedSmaple(sam, pixel + vec2( 0.0 ,-onePixel)).rgb; vec3 C20 = clampedSmaple(sam, pixel + vec2( onePixel ,-onePixel)).rgb; vec3 C30 = clampedSmaple(sam, pixel + vec2( twoPixels,-onePixel)).rgb; vec3 C01 = clampedSmaple(sam, pixel + vec2(-onePixel , 0.0)).rgb; vec3 C11 = clampedSmaple(sam, pixel + vec2( 0.0 , 0.0)).rgb; vec3 C21 = clampedSmaple(sam, pixel + vec2( onePixel , 0.0)).rgb; vec3 C31 = clampedSmaple(sam, pixel + vec2( twoPixels, 0.0)).rgb; vec3 C02 = clampedSmaple(sam, pixel + vec2(-onePixel , onePixel)).rgb; vec3 C12 = clampedSmaple(sam, pixel + vec2( 0.0 , onePixel)).rgb; vec3 C22 = clampedSmaple(sam, pixel + vec2( onePixel , onePixel)).rgb; vec3 C32 = clampedSmaple(sam, pixel + vec2( twoPixels, onePixel)).rgb; vec3 C03 = clampedSmaple(sam, pixel + vec2(-onePixel , twoPixels)).rgb; vec3 C13 = clampedSmaple(sam, pixel + vec2( 0.0 , twoPixels)).rgb; vec3 C23 = clampedSmaple(sam, pixel + vec2( onePixel , twoPixels)).rgb; vec3 C33 = clampedSmaple(sam, pixel + vec2( twoPixels, twoPixels)).rgb; vec3 CP0X = CubicLagrange(C00, C10, C20, C30, frac.x); vec3 CP1X = CubicLagrange(C01, C11, C21, C31, frac.x); vec3 CP2X = CubicLagrange(C02, C12, C22, C32, frac.x); vec3 CP3X = CubicLagrange(C03, C13, C23, C33, frac.x); return CubicLagrange(CP0X, CP1X, CP2X, CP3X, frac.y); } //======================================================================================= vec4 cubic(float v){ vec4 n = vec4(1.0, 2.0, 3.0, 4.0) - v; vec4 s = n * n * n; float x = s.x; float y = s.y - 4.0 * s.x; float z = s.z - 4.0 * s.y + 6.0 * s.x; float w = 6.0 - x - y - z; return vec4(x, y, z, w) * (1.0/6.0); } //======================================================================================= vec4 textureBicubic(sampler2D sampler, vec2 texCoords){ vec2 texSize = textureSize(sampler, 0); vec2 invTexSize = 1.0 / texSize; texCoords = texCoords * texSize - 0.5; vec2 fxy = fract(texCoords); texCoords -= fxy; vec4 xcubic = cubic(fxy.x); vec4 ycubic = cubic(fxy.y); vec4 c = texCoords.xxyy + vec2 (-0.5, +1.5).xyxy; vec4 s = vec4(xcubic.xz + xcubic.yw, ycubic.xz + ycubic.yw); vec4 offset = c + vec4 (xcubic.yw, ycubic.yw) / s; offset *= invTexSize.xxyy; vec4 sample0 = clampedSmaple(sampler, offset.xz); vec4 sample1 = clampedSmaple(sampler, offset.yz); vec4 sample2 = clampedSmaple(sampler, offset.xw); vec4 sample3 = clampedSmaple(sampler, offset.yw); float sx = s.x / (s.x + s.y); float sy = s.z / (s.z + s.w); return mix( mix(sample3, sample2, sx), mix(sample1, sample0, sx) , sy); } //======================================================================================= vec4 textureNice( sampler2D sam, vec2 uv ) { float textureResolution = float(textureSize(sam,0).x); uv = uv*textureResolution + 0.5; vec2 iuv = floor( uv ); vec2 fuv = fract( uv ); uv = iuv + fuv*fuv*(3.0-2.0*fuv); uv = (uv - 0.5)/textureResolution; return clampedSmaple( sam, uv ); } void main(void) { // fragColor = textureBicubic(tex, f_texCoord.xy); // fragColor = texture2D(tex, f_texCoord.xy); // fragColor = textureNice(tex, f_texCoord.xy); // vec3 color; // float textureResolution = float(textureSize(tex,0).x); // color = BicubicLagrangeTextureSample(tex, f_texCoord.xy, textureResolution); // fragColor = vec4(color, 1.0); vec3 color; float textureResolution = float(textureSize(tex,0).x); color = BicubicHermiteTextureSample(tex, f_texCoord.xy, textureResolution); fragColor = vec4(color, 1.0); //fragColor = vec4(0.5, 0.7, 0.6, 1); }