Skip to content

Commit

Permalink
Add crt-pi shader from davej
Browse files Browse the repository at this point in the history
  • Loading branch information
gizmo98 committed Nov 14, 2015
1 parent c4a685d commit 7097eb6
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 0 deletions.
8 changes: 8 additions & 0 deletions crt-pi-barrel.glslp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shaders = "1"
shader0 = "shaders/crt-pi-barrel.glsl"
filter_linear0 = "false"
wrap_mode0 = "clamp_to_border"
mipmap_input0 = "false"
alias0 = ""
float_framebuffer0 = "false"
srgb_framebuffer0 = "false"
8 changes: 8 additions & 0 deletions crt-pi-mask.glslp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shaders = "1"
shader0 = "shaders/crt-pi-mask.glsl"
filter_linear0 = "true"
wrap_mode0 = "clamp_to_border"
mipmap_input0 = "false"
alias0 = ""
float_framebuffer0 = "false"
srgb_framebuffer0 = "false"
8 changes: 8 additions & 0 deletions crt-pi.glslp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
shaders = "1"
shader0 = "shaders/crt-pi.glsl"
filter_linear0 = "true"
wrap_mode0 = "clamp_to_border"
mipmap_input0 = "false"
alias0 = ""
float_framebuffer0 = "false"
srgb_framebuffer0 = "false"
78 changes: 78 additions & 0 deletions shaders/crt-pi-barrel.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#define BARREL_DISTORTION_X 0.15
#define BARREL_DISTORTION_Y 0.25

/* COMPATIBILITY
- GLSL compilers
*/

/*
crt-barrel
Copyright (C) 2015 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
*/

#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif

uniform vec2 InputSize;
uniform vec2 TextureSize;
varying COMPAT_PRECISION vec2 screenScale;
varying COMPAT_PRECISION vec2 TEX0;

#if defined(VERTEX)
uniform mediump mat4 MVPMatrix;
attribute mediump vec4 VertexCoord;
attribute mediump vec2 TexCoord;

void main()
{
screenScale = TextureSize / InputSize;
TEX0 = TexCoord;
gl_Position = MVPMatrix * VertexCoord;
}
#elif defined(FRAGMENT)
uniform sampler2D Texture;

#define BARREL_DISTORTION vec2(BARREL_DISTORTION_X, BARREL_DISTORTION_Y)
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
const vec2 barrelScale = 1.0 - (0.23 * BARREL_DISTORTION);

vec2 Distort(vec2 coord)
{
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (BARREL_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}

return coord;
}

void main()
{
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(0.0);
else
{
vec3 colour = texture2D(Texture, texcoord).rgb;

gl_FragColor = vec4(colour,1.0);
}
}
#endif
76 changes: 76 additions & 0 deletions shaders/crt-pi-mask.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#define BARREL_DISTORTION_X 0.15
#define BARREL_DISTORTION_Y 0.25

/* COMPATIBILITY
- GLSL compilers
*/

/*
crt-pi
Copyright (C) 2015 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
*/

#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif

uniform vec2 TextureSize;

varying COMPAT_PRECISION vec2 screenScale;
varying COMPAT_PRECISION vec2 TEX0;

#if defined(VERTEX)
uniform mat4 MVPMatrix;
attribute vec4 VertexCoord;
attribute vec2 TexCoord;
uniform vec2 InputSize;
uniform vec2 OutputSize;

void main()
{
screenScale = TextureSize / InputSize;
TEX0 = TexCoord;
gl_Position = MVPMatrix * VertexCoord;
}
#elif defined(FRAGMENT)

#define BARREL_DISTORTION vec2(BARREL_DISTORTION_X, BARREL_DISTORTION_Y)
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
const vec2 barrelScale = 1.0 - (0.23 * BARREL_DISTORTION);

vec2 Distort(vec2 coord)
{
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (BARREL_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}

return coord;
}

void main()
{
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(1.0);
else
gl_FragColor = vec4(0.0);

}
#endif
134 changes: 134 additions & 0 deletions shaders/crt-pi.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#define MULTISAMPLE
#define CURVATURE
#define BARREL_DISTORTION_X 0.15
#define BARREL_DISTORTION_Y 0.25
#define MASK_BRIGHTNESS 0.75
#define SCAN_LINE_WEIGHT 6.0
#define BLOOM_FACTOR 1.5
#define INPUT_GAMMA 2.4
#define OUTPUT_GAMMA 2.2

/* COMPATIBILITY
- GLSL compilers
*/

/*
crt-pi
Copyright (C) 2015 davej
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the Free
Software Foundation; either version 2 of the License, or (at your option)
any later version.
*/

#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif


uniform vec2 TextureSize;

varying COMPAT_PRECISION float pixelWidth;
varying COMPAT_PRECISION vec2 screenScale;
varying COMPAT_PRECISION vec2 TEX0;
varying COMPAT_PRECISION float filterWidth;

#if defined(VERTEX)
uniform mat4 MVPMatrix;
attribute vec4 VertexCoord;
attribute vec2 TexCoord;
uniform vec2 InputSize;
uniform vec2 OutputSize;

void main()
{
pixelWidth = 1.0 / TextureSize.x;
screenScale = TextureSize / InputSize;
filterWidth = InputSize.y / OutputSize.y;
TEX0 = TexCoord;
gl_Position = MVPMatrix * VertexCoord;
}
#elif defined(FRAGMENT)

uniform sampler2D Texture;

#define BARREL_DISTORTION vec2(BARREL_DISTORTION_X, BARREL_DISTORTION_Y)
// Barrel distortion shrinks the display area a bit, this will allow us to counteract that.
const vec2 barrelScale = 1.0 - (0.23 * BARREL_DISTORTION);

const float bloom = BLOOM_FACTOR / 3.0;

vec2 Distort(vec2 coord)
{
coord *= screenScale;
coord -= vec2(0.5);
float rsq = coord.x * coord.x + coord.y * coord.y;
coord += coord * (BARREL_DISTORTION * rsq);
coord *= barrelScale;
if (abs(coord.x) >= 0.5 || abs(coord.y) >= 0.5)
coord = vec2(-1.0); // If out of bounds, return an invalid value.
else
{
coord += vec2(0.5);
coord /= screenScale;
}

return coord;
}

float CalcScanLineWeight(float dist)
{
float invdist = 1.0 - dist;
return max(1.0-dist*dist*SCAN_LINE_WEIGHT, invdist*invdist/1.5);
}

void main()
{
#if defined(CURVATURE)
vec2 texcoord = Distort(TEX0);
if (texcoord.x < 0.0)
gl_FragColor = vec4(0.0);
else
#else
vec2 texcoord = TEX0;
#endif
{
vec2 texcoordInPixels = texcoord * TextureSize - vec2(0.5);
float yCoord = (floor(texcoordInPixels.y) + 0.5) / TextureSize.y;
vec2 tc1 = vec2(texcoord.x, yCoord);
vec3 colour1 = texture2D(Texture, tc1).rgb;
vec2 tc2 = tc1 + vec2(0.0, 1.0/TextureSize.y);
vec3 colour2 = texture2D(Texture, tc2).rgb;
colour1 = pow(colour1, vec3(INPUT_GAMMA));
colour2 = pow(colour2, vec3(INPUT_GAMMA));

int whichMask = int(floor(mod(gl_FragCoord.x, 2.0)));
vec3 mask = vec3(1.0, MASK_BRIGHTNESS, 1.0);
if (whichMask == 1)
mask = vec3(MASK_BRIGHTNESS, 1.0, MASK_BRIGHTNESS);

float dist = fract(texcoordInPixels.y);
float scanLineWeightsA = CalcScanLineWeight(dist);
float scanLineWeightsB = CalcScanLineWeight(1.0-dist);
#if defined(MULTISAMPLE)
dist = dist + 1.0/3.0*filterWidth;
scanLineWeightsA += CalcScanLineWeight(dist);
scanLineWeightsB += CalcScanLineWeight(abs(1.0-dist));
dist = dist - 2.0/3.0*filterWidth;
scanLineWeightsA += CalcScanLineWeight(abs(dist));
scanLineWeightsB += CalcScanLineWeight(abs(1.0-dist));
scanLineWeightsB *= bloom;
scanLineWeightsA *= bloom;
#endif
colour1 = colour1 * scanLineWeightsA + colour2 * scanLineWeightsB;

colour1 = pow(colour1, vec3(1.0 / OUTPUT_GAMMA));

gl_FragColor = vec4(colour1 * mask, 1.0);
}
}
#endif

0 comments on commit 7097eb6

Please sign in to comment.