From 2adbf9a2f9550e751aa895889b1c5ff11659dd1d Mon Sep 17 00:00:00 2001 From: Ali Hatami Date: Mon, 16 Sep 2024 20:12:00 +0330 Subject: [PATCH] Add dm-v4 shaders --- bip.fs | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++++ bip.vs | 13 ++++ ellipse.fs | 0 ellipse.vs | 0 line.fs | 0 line.vs | 0 mline.fs | 0 mline.vs | 0 simple.fs | 13 ++++ simple.vs | 11 +++ target.fs | 0 target.vs | 0 text.fs | 0 text.vs | 0 zoomgate.fs | 0 zoomgate.vs | 0 16 files changed, 257 insertions(+) create mode 100644 bip.fs create mode 100644 bip.vs mode change 100755 => 100644 ellipse.fs mode change 100755 => 100644 ellipse.vs mode change 100755 => 100644 line.fs mode change 100755 => 100644 line.vs mode change 100755 => 100644 mline.fs mode change 100755 => 100644 mline.vs create mode 100644 simple.fs create mode 100644 simple.vs mode change 100755 => 100644 target.fs mode change 100755 => 100644 target.vs mode change 100755 => 100644 text.fs mode change 100755 => 100644 text.vs mode change 100755 => 100644 zoomgate.fs mode change 100755 => 100644 zoomgate.vs diff --git a/bip.fs b/bip.fs new file mode 100644 index 0000000..7b6e65d --- /dev/null +++ b/bip.fs @@ -0,0 +1,220 @@ +#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); +} diff --git a/bip.vs b/bip.vs new file mode 100644 index 0000000..e514830 --- /dev/null +++ b/bip.vs @@ -0,0 +1,13 @@ +#version 330 + +in vec2 v_vertex; +in vec2 v_texCoord; +out vec2 f_texCoord; + +uniform mat4 transform; + +void main(void) +{ + gl_Position = transform * vec4(v_vertex, 0.0, 1.0); + f_texCoord = v_texCoord; +} diff --git a/ellipse.fs b/ellipse.fs old mode 100755 new mode 100644 diff --git a/ellipse.vs b/ellipse.vs old mode 100755 new mode 100644 diff --git a/line.fs b/line.fs old mode 100755 new mode 100644 diff --git a/line.vs b/line.vs old mode 100755 new mode 100644 diff --git a/mline.fs b/mline.fs old mode 100755 new mode 100644 diff --git a/mline.vs b/mline.vs old mode 100755 new mode 100644 diff --git a/simple.fs b/simple.fs new file mode 100644 index 0000000..adb5a7d --- /dev/null +++ b/simple.fs @@ -0,0 +1,13 @@ +#version 330 + +in vec2 f_texCoord; +out vec4 fragColor; + +uniform sampler2D tex; + +void main(void) +{ + fragColor = texture2D(tex, f_texCoord.xy); +// fragColor = vec4(0.5, 0.7, 0.6, 1); + +} diff --git a/simple.vs b/simple.vs new file mode 100644 index 0000000..20ee174 --- /dev/null +++ b/simple.vs @@ -0,0 +1,11 @@ +#version 330 + +layout(location = 0) in vec2 v_vertex; +layout(location = 1) in vec2 v_texCoord; +out vec2 f_texCoord; + +void main(void) +{ + gl_Position = vec4(v_vertex, 0.0, 1.0); + f_texCoord = v_texCoord; +} diff --git a/target.fs b/target.fs old mode 100755 new mode 100644 diff --git a/target.vs b/target.vs old mode 100755 new mode 100644 diff --git a/text.fs b/text.fs old mode 100755 new mode 100644 diff --git a/text.vs b/text.vs old mode 100755 new mode 100644 diff --git a/zoomgate.fs b/zoomgate.fs old mode 100755 new mode 100644 diff --git a/zoomgate.vs b/zoomgate.vs old mode 100755 new mode 100644