[retro-gtk] glsl-filters: Add the crt-simple filter
- From: Adrien Plazas <aplazas src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [retro-gtk] glsl-filters: Add the crt-simple filter
- Date: Thu, 16 Nov 2017 06:54:56 +0000 (UTC)
commit 9bd3f80b286d88b5bab4644d82b3f7376d41abed
Author: Adrien Plazas <kekun plazas laposte net>
Date: Sun Oct 15 09:36:05 2017 +0200
glsl-filters: Add the crt-simple filter
This will be used in the next commits to offer a video filter mimicking
CRT screens.
retro-gtk/glsl-filters/crt-simple.filter | 8 ++
retro-gtk/glsl-filters/crt-simple.fs | 127 ++++++++++++++++++++++++++++++
retro-gtk/glsl-filters/crt-simple.vs | 32 ++++++++
retro-gtk/retro-gtk.gresource.xml | 3 +
4 files changed, 170 insertions(+), 0 deletions(-)
---
diff --git a/retro-gtk/glsl-filters/crt-simple.filter b/retro-gtk/glsl-filters/crt-simple.filter
new file mode 100644
index 0000000..4ab6bc6
--- /dev/null
+++ b/retro-gtk/glsl-filters/crt-simple.filter
@@ -0,0 +1,8 @@
+[GLSL Filter]
+Filter=Linear
+Wrap=Border
+Vertex=crt-simple.vs
+Fragment=crt-simple.fs
+Authors=hunterk;
+License=GPL-2.0+;
+URL=https://github.com/hizzlekizzle/quark-shaders/tree/master/CRT-Simple.shader
diff --git a/retro-gtk/glsl-filters/crt-simple.fs b/retro-gtk/glsl-filters/crt-simple.fs
new file mode 100644
index 0000000..5fdccaa
--- /dev/null
+++ b/retro-gtk/glsl-filters/crt-simple.fs
@@ -0,0 +1,127 @@
+#version 150
+
+uniform sampler2D source[];
+uniform vec4 sourceSize[];
+
+in Vertex {
+ vec2 texCoord;
+ vec2 one;
+ float mod_factor;
+};
+
+out vec4 fragColor;
+
+ uniform vec4 targetSize;
+ uniform vec2 outputSize;
+
+ // Enable screen curvature.
+ #define CURVATURE
+
+ // Controls the intensity of the barrel distortion used to emulate the
+ // curvature of a CRT. 0.0 is perfectly flat, 1.0 is annoyingly
+ // distorted, higher values are increasingly ridiculous.
+ #define distortion 0.08
+
+ // Simulate a CRT gamma of 2.4.
+ #define inputGamma 2.4
+
+ // Compensate for the standard sRGB gamma of 2.2.
+ #define outputGamma 2.2
+
+ // Macros.
+ #define TEX2D(c) pow(texture2D(source[0], (c)), vec4(inputGamma))
+ #define PI 3.141592653589
+
+ // Apply radial distortion to the given coordinate.
+ vec2 radialDistortion(vec2 coord)
+ {
+ vec2 cc = coord - 0.5;
+ float dist = dot(cc, cc) * distortion;
+ return (coord + cc * (1.0 + dist) * dist);
+ }
+
+ // Calculate the influence of a scanline on the current pixel.
+ //
+ // 'distance' is the distance in texture coordinates from the current
+ // pixel to the scanline in question.
+ // 'color' is the colour of the scanline at the horizontal location of
+ // the current pixel.
+ vec4 scanlineWeights(float distance, vec4 color)
+ {
+ // The "width" of the scanline beam is set as 2*(1 + x^4) for
+ // each RGB channel.
+ vec4 wid = 2.0 + 2.0 * pow(color, vec4(4.0));
+
+ // The "weights" lines basically specify the formula that gives
+ // you the profile of the beam, i.e. the intensity as
+ // a function of distance from the vertical center of the
+ // scanline. In this case, it is gaussian if width=2, and
+ // becomes nongaussian for larger widths. Ideally this should
+ // be normalized so that the integral across the beam is
+ // independent of its width. That is, for a narrower beam
+ // "weights" should have a higher peak at the center of the
+ // scanline than for a wider beam.
+ vec4 weights = vec4(distance / 0.3);
+ return 1.4 * exp(-pow(weights * inversesqrt(0.5 * wid), wid)) / (0.6 + 0.2 * wid);
+ }
+
+void main() {
+ // Here's a helpful diagram to keep in mind while trying to
+ // understand the code:
+ //
+ // | | | | |
+ // -------------------------------
+ // | | | | |
+ // | 01 | 11 | 21 | 31 | <-- current scanline
+ // | | @ | | |
+ // -------------------------------
+ // | | | | |
+ // | 02 | 12 | 22 | 32 | <-- next scanline
+ // | | | | |
+ // -------------------------------
+ // | | | | |
+ //
+ // Each character-cell represents a pixel on the output
+ // surface, "@" represents the current pixel (always somewhere
+ // in the bottom half of the current scan-line, or the top-half
+ // of the next scanline). The grid of lines represents the
+ // edges of the texels of the underlying texture.
+
+ // Texture coordinates of the texel containing the active pixel.
+ #ifdef CURVATURE
+ vec2 xy = radialDistortion(texCoord);
+ #else
+ vec2 xy = texCoord;
+ #endif
+
+ // Of all the pixels that are mapped onto the texel we are
+ // currently rendering, which pixel are we currently rendering?
+ vec2 ratio_scale = xy * sourceSize[0].xy - vec2(0.5);
+ vec2 uv_ratio = fract(ratio_scale);
+
+ // Snap to the center of the underlying texel.
+ xy.y = (floor(ratio_scale.y) + 0.5) / sourceSize[0].y;
+
+ // Calculate the effective colour of the current and next
+ // scanlines at the horizontal location of the current pixel.
+ vec4 col = TEX2D(xy);
+ vec4 col2 = TEX2D(xy + vec2(0.0, one.y));
+
+ // Calculate the influence of the current and next scanlines on
+ // the current pixel.
+ vec4 weights = scanlineWeights(uv_ratio.y, col);
+ vec4 weights2 = scanlineWeights(1.0 - uv_ratio.y, col2);
+ vec3 mul_res = (col * weights + col2 * weights2).rgb;
+
+ // dot-mask emulation:
+ // Output pixels are alternately tinted green and magenta.
+ vec3 dotMaskWeights = mix(
+ vec3(1.0, 0.7, 1.0),
+ vec3(0.7, 1.0, 0.7),
+ floor(mod(mod_factor, 2.0))
+ );
+
+ mul_res *= dotMaskWeights;
+
+ fragColor = vec4(pow(mul_res, vec3(1.0 / outputGamma)), 1.0);
+}
diff --git a/retro-gtk/glsl-filters/crt-simple.vs b/retro-gtk/glsl-filters/crt-simple.vs
new file mode 100644
index 0000000..53957cd
--- /dev/null
+++ b/retro-gtk/glsl-filters/crt-simple.vs
@@ -0,0 +1,32 @@
+#version 150
+
+in vec4 position;
+in vec2 texCoord;
+
+out Vertex {
+ vec2 texCoord;
+ // Define some calculations that will be used in fragment shader.
+ vec2 one;
+ float mod_factor;
+} vertexOut;
+
+uniform float relative_aspect_ratio;
+uniform vec4 targetSize;
+uniform vec2 outputSize;
+uniform vec4 sourceSize[];
+
+void main() {
+ vec2 aspect_ratio = (relative_aspect_ratio > 1.0f) ?
+ vec2 (1.0 / relative_aspect_ratio, 1.0) :
+ vec2 (1.0, relative_aspect_ratio);
+
+ vertexOut.texCoord = texCoord;
+ gl_Position = vec4 (position.xy * aspect_ratio, 0.0, 1.0);
+ // gl_Position = vec4 (position);
+
+ // The size of one texel, in texture-coordinates.
+ vertexOut.one = 1.0 / sourceSize[0].xy;
+
+ // Resulting X pixel-coordinate of the pixel we're drawing.
+ vertexOut.mod_factor = texCoord.x * targetSize.x * aspect_ratio.x;
+}
diff --git a/retro-gtk/retro-gtk.gresource.xml b/retro-gtk/retro-gtk.gresource.xml
index 8d845ec..3ca75a1 100644
--- a/retro-gtk/retro-gtk.gresource.xml
+++ b/retro-gtk/retro-gtk.gresource.xml
@@ -3,6 +3,9 @@
<gresource prefix="/org/gnome/Retro">
<file>glsl-filters/bicubic.filter</file>
<file>glsl-filters/bicubic.fs</file>
+ <file>glsl-filters/crt-simple.filter</file>
+ <file>glsl-filters/crt-simple.fs</file>
+ <file>glsl-filters/crt-simple.vs</file>
<file>glsl-filters/sharp.filter</file>
<file>glsl-filters/sharp.fs</file>
<file>glsl-filters/sharp.vs</file>
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]