Horizontally, there should be no problems, many nice algorithms exist that get the job done, for example gaussian, lanczos, quilez, polynomial…even some sort of sharp bilinear.
In order to calculate scanlines it’s important that the sum of brightness is very similar, even if they consist from a different number of “subpixels”. The hard way to achieve this (minding additional parameters) is to use a complex function, which makes sure the above is happening. The easy way is to use some sort of integer scaling, a higher resolution display or vertical supersampling… Using algorithms like sharp-bilinear for scanline calculation is out of it’s scope since it doesn’t consider brightness. Or better said, it can’t calculate scanlines.
To simplify, here are the patterns that should definitely be included in the shader. If you can add these, it will be complete. The other patterns (the three-pixel wide red, blue, green and red, green, blue patterns) will result in uneven spacing of the LCD subpixels, but I guess they can still be included just for the sake of having more options (doesn’t really affect performance). For a more detailed explanation, see my previous post. Thanks again for this great shader!
Done, the shader is updated, but it’s better checked for possible errors.
Too bad i don’t have a 4k display at hand to test the mask. They sure look great.
I took a liberty to include the “aperture” RGB mask, sure it has it’s fans…
This is just nitpicking, but I might change the label
“(2, 3, 4 4k masks)”
to
“(3, 4 4k masks)”
Since mask 2 is really intended for 1080p displays that have the less common RBG subpixel arrangement.
And I might swap mask 1 for mask 2 for better ordering.
Then you could write in the shader comments something like this:
To obtain the best results with masks 0, 1, 3, 4: must leave “mask size” at 1 and the display must be set to its native resolution to result in evenly spaced “active” LCD subpixels.
Mask 0: Uses a magenta and green pattern for even spacing of the LCD subpixels.
Mask 1: Intended for displays that have RBG subpixels (as opposed to the more common RGB). Uses a yellow/blue pattern for even spacing of the LCD subpixels.
Mask 2: Common red/green/blue pattern.
Mask 3: This is useful for 4K displays, where masks 0 and 1 can look too fine. Uses a red/yellow/cyan/blue pattern to result in even spacing of the LCD subpixels.
Mask 4: Intended for displays that have the less common RBG subpixel pattern. This is useful for 4K displays, where masks 0 and 1 can look too fine. Uses a red/magenta/cyan/green pattern for even spacing of the LCD subpixels.
Or you could put what is currently mask 1 where mask 4 is, change 4 to 3 and change 3 to 2; then change the above accordingly. Again, just some nitpicking over the organization
Just wanted to say that the smart y integer scaling is fantastic! I had the same idea a while back but didn’t have the skills to make it happen. Very handy for PSX games that use multiple resolutions! Tried it with FFVII last night. You don’t even notice the resolution changing when you bring up the menu screen or switch to the overworld/map, because it happens so quickly. Perfect!
Christmas came early this year
When is GLSL going to have 2d arrays? If that’s coming soon, I’d say just wait…
Speaking of slang, how do I get glcore? Will this be automatically added if I update my drivers, if the graphics card supports it?
Hey man, thanks. Some content’s really look better with something like this…
The story started with the overscan shader for some PSX games and luckily i toyed with a (homemade) crt shader that simply needed integer scaling.
I played a little bit with crt-guest-sm-glow.glslp, I think this shader with the combination gtuv50 is really interesting for 16bit and arcade games (snes, genesis, mame …)
I still have to figure out which mask to use (1080p old LCD TV about 10/11 years old), and like @Nesguy said “the smart y integer scaling is fantastic”
I always found the scanlines too pronounced (heavy) I have to adjust all that.
I decrease the “scanline adjust” to 4 or 5 I don’t really know how to use the “scanline dark / bright”
I always have the impression that on arcade or CRT TV monitors these scanlines blend better with the image.
It can be the fact of being still in 1080p or so because I have never really seen a real PVM / BVM in action and my memories of the Sony Trinitron are wrong …
I played in the arcade for a long time in my youth and I never noticed such pronounced scanlines
Just a few questions:
If I increase the glow parameter I have the impression that the scanlines blend better is this true or is it my impression?
Will it be possible to have the color settings (lut and trinitron) as well as the curvature?
In the end for you, what are you missing to emulate a crt, a lot here talks about 4k or even 8k is it really necessary?
In any case, a big thank you to you. This new shader is really great.
The curvature should be pretty easy to do, it should mainly be copy paste…
The LUT is even easier as you just open the glslp for crt-guest-sm-glow and copy/paste the LUT shader stuff from crt-guest-dr-venom at the beginning so shader0 then renumber everything accordingly.
Lastly the scanline dark/bright should work like this, the dark setting adjusts how dark scanlines are over dark colors(pixels?), and the bright scanlines does the same over bright things. So turn them down for less noticable scanlines.
EDIT: Also I’m getting some weird (artifacting?) behavior from GTU.
To date, this is the best compromise I’ve found between mask/scanline strength and brightness. Still a solid 100 nits (max) on my display with the backlight @ 100%. Mask strength @ 75%.
As @Syh already correctly replied to some questions…
As @hunterk mentioned, adding the crt-royale last pass solves curvature and corner problems etc. As my shader has strong scanlines, curvature would produce an annoying moire pattern and slow the shader down.
CRT shaders only benefit from greater resolutions and higher brightness displays, but a 1080p one will do fine. Most problematic are masks, which print a pattern into the image. Clumsy patterns require higher resolutions and brightness, some may even require 8k. In general, 4k solves most problems with crt shaders. One might want to get at least a 120Hz capable model for black frame insertion though.
It seems to correct something for you, so glow it’s beneficial in more ways. It makes scanlines milder, for sure.
Thank you too. The composite effect is very dominant, it would take over the fine features of the shader, but once learned how to add it, it can be added in front of most shaders. But i guess there will be no official composite version.
Last but not least, recently i’ve been working very actively to remove some issues the shader has, or better said, had. Had to sleep over it first i guess.
mask artifacts on edges of bright pixels
oversaturated output
stiff/limited mask control
In the version below i managed to fix this. Scanline functions and parameters changed a bit too, just to mention it.
crt-guest-sm.glsl
/*
CRT - Guest - SM (Scanline Mask) Shader
Copyright (C) 2019 guest(r) - [email protected]
Big thanks to Nesguy from the Libretro forums for the masks and other ideas.
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/* README - MASKS GUIDE
To obtain the best results with masks 0, 1, 3, 4:
must leave “mask size” at 1 and the display must be set to its native resolution to result in evenly spaced “active” LCD subpixels.
Mask 0: Uses a magenta and green pattern for even spacing of the LCD subpixels.
Mask 1: Intended for displays that have RBG subpixels (as opposed to the more common RGB).
Uses a yellow/blue pattern for even spacing of the LCD subpixels.
Mask 2: Common red/green/blue pattern.
Mask 3: This is useful for 4K displays, where masks 0 and 1 can look too fine.
Uses a red/yellow/cyan/blue pattern to result in even spacing of the LCD subpixels.
Mask 4: Intended for displays that have the less common RBG subpixel pattern.
This is useful for 4K displays, where masks 0 and 1 can look too fine.
Uses a red/magenta/cyan/green pattern for even spacing of the LCD subpixels.
*/
// Parameter lines go here:
#pragma parameter smart "Smart Y Integer Scaling" 0.0 0.0 1.0 1.0
#pragma parameter brightboost "Bright boost" 1.40 0.5 2.0 0.05
#pragma parameter scanline "Scanline shape" 8.0 4.0 14.0 0.5
#pragma parameter beam_min "Scanline dark" 1.40 0.5 2.0 0.05
#pragma parameter beam_max "Scanline bright" 1.10 0.5 2.0 0.05
#pragma parameter s_gamma "Scanline gamma" 2.4 1.5 3.0 0.05
#pragma parameter h_sharp "Horizontal sharpness" 2.0 1.0 5.0 0.05
#pragma parameter mask "CRT Mask (3&4 are 4k masks)" 0.0 0.0 4.0 1.0
#pragma parameter maskdark "CRT Mask Strength Dark Pixels" 1.0 0.0 1.5 0.05
#pragma parameter maskbright "CRT Mask Strength Bright Pixels" 0.15 0.0 1.0 0.05
#pragma parameter masksize "CRT Mask Size" 1.0 1.0 2.0 1.0
#pragma parameter gamma_out "Gamma Out" 2.20 1.0 3.0 0.05
#if defined(VERTEX)
#if __VERSION__ >= 130
#define COMPAT_VARYING out
#define COMPAT_ATTRIBUTE in
#define COMPAT_TEXTURE texture
#else
#define COMPAT_VARYING varying
#define COMPAT_ATTRIBUTE attribute
#define COMPAT_TEXTURE texture2D
#endif
#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif
COMPAT_ATTRIBUTE vec4 VertexCoord;
COMPAT_ATTRIBUTE vec4 COLOR;
COMPAT_ATTRIBUTE vec4 TexCoord;
COMPAT_VARYING vec4 COL0;
COMPAT_VARYING vec4 TEX0;
vec4 _oPosition1;
uniform mat4 MVPMatrix;
uniform COMPAT_PRECISION int FrameDirection;
uniform COMPAT_PRECISION int FrameCount;
uniform COMPAT_PRECISION vec2 OutputSize;
uniform COMPAT_PRECISION vec2 TextureSize;
uniform COMPAT_PRECISION vec2 InputSize;
void main()
{
gl_Position = MVPMatrix * VertexCoord;
COL0 = COLOR;
TEX0.xy = TexCoord.xy * 1.00001;
}
#elif defined(FRAGMENT)
#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_TEXTURE texture
out vec4 FragColor;
#else
#define COMPAT_VARYING varying
#define FragColor gl_FragColor
#define COMPAT_TEXTURE texture2D
#endif
#ifdef GL_ES
#ifdef GL_FRAGMENT_PRECISION_HIGH
precision highp float;
#else
precision mediump float;
#endif
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif
uniform COMPAT_PRECISION int FrameDirection;
uniform COMPAT_PRECISION int FrameCount;
uniform COMPAT_PRECISION vec2 OutputSize;
uniform COMPAT_PRECISION vec2 TextureSize;
uniform COMPAT_PRECISION vec2 InputSize;
uniform sampler2D Texture;
COMPAT_VARYING vec4 TEX0;
// compatibility #defines
#define Source Texture
#define vTexCoord TEX0.xy
#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
#define OutputSize vec4(OutputSize, 1.0 / OutputSize)
#ifdef PARAMETER_UNIFORM
// All parameter floats need to have COMPAT_PRECISION in front of them
uniform COMPAT_PRECISION float smart;
uniform COMPAT_PRECISION float brightboost;
uniform COMPAT_PRECISION float scanline;
uniform COMPAT_PRECISION float beam_min;
uniform COMPAT_PRECISION float beam_max;
uniform COMPAT_PRECISION float s_gamma;
uniform COMPAT_PRECISION float h_sharp;
uniform COMPAT_PRECISION float mask;
uniform COMPAT_PRECISION float maskdark;
uniform COMPAT_PRECISION float maskbright;
uniform COMPAT_PRECISION float masksize;
uniform COMPAT_PRECISION float gamma_out;
#else
#define smart 0.00 // smart Y integer scaling
#define brightboost 1.40 // adjust brightness
#define scanline 8.00 // scanline param, vertical sharpness
#define beam_min 1.40 // dark area beam min - narrow
#define beam_max 1.10 // bright area beam max - wide
#define s_gamma 2.40 // scanline gamma
#define h_sharp 2.00 // pixel sharpness
#define mask 0.00 // crt mask type
#define maskdark 1.00 // crt mask strength dark pixels
#define maskbright 0.15 // crt mask strength bright pixels
#define masksize 1.00 // crt mask size
#define gamma_out 2.20 // gamma out
#endif
float st(float x)
{
return exp2(-10.0*x*x);
}
vec3 sw(float x, vec3 color)
{
vec3 tmp = mix(vec3(2.75*beam_min),vec3(beam_max), color);
tmp = mix(vec3(beam_max), tmp, pow(vec3(x), color + 0.25));
vec3 ex = vec3(x)*tmp;
return exp2(-scanline*ex*ex)/(0.65 + 0.35*color);
}
float Overscan(float pos, float dy){
pos=pos*2.0-1.0;
pos*=dy;
return pos*0.5+0.5;
}
void main()
{
vec2 tex = TEX0.xy * 1.000001;
if (smart == 1.0)
{
float factor = OutputSize.y/InputSize.y;
float intfactor = round(factor);
float diff = factor/intfactor;
tex.y = Overscan(tex.y*(SourceSize.y/InputSize.y), diff)*(InputSize.y/SourceSize.y);
}
vec2 OGL2Pos = tex * SourceSize.xy - vec2(0.5);
vec2 fp = fract(OGL2Pos);
vec2 pC4 = (floor(OGL2Pos) + vec2(0.5)) * SourceSize.zw;
// Reading the texels
vec3 ul = COMPAT_TEXTURE(Texture, pC4 ).xyz; ul*=ul;
vec3 ur = COMPAT_TEXTURE(Texture, pC4 + vec2(SourceSize.z,0.0)).xyz; ur*=ur;
vec3 dl = COMPAT_TEXTURE(Texture, pC4 + vec2(0.0,SourceSize.w)).xyz; dl*=dl;
vec3 dr = COMPAT_TEXTURE(Texture, pC4 + SourceSize.zw ).xyz; dr*=dr;
float lx = fp.x; lx = pow(lx, h_sharp);
float rx = 1.0 - fp.x; rx = pow(rx, h_sharp);
float w = 1.0/(lx+rx);
vec3 color1 = w*(ur*lx + ul*rx);
vec3 color2 = w*(dr*lx + dl*rx);
ul*=ul*ul; ul*=ul;
ur*=ur*ur; ur*=ur;
dl*=dl*dl; dl*=dl;
dr*=dr*dr; dr*=dr;
vec3 scolor1 = w*(ur*lx + ul*rx); scolor1 = pow(scolor1, vec3(s_gamma*(1.0/12.0)));
vec3 scolor2 = w*(dr*lx + dl*rx); scolor2 = pow(scolor2, vec3(s_gamma*(1.0/12.0)));
// calculating scanlines
float f = fp.y;
float t1 = st(f);
float t2 = st(1.0-f);
vec3 color = color1*t1 + color2*t2;
vec3 scolor = scolor1*t1 + scolor2*t2;
vec3 ctemp = color / (t1 + t2);
vec3 sctemp = scolor / (t1 + t2);
vec3 cref1 = mix(scolor1, sctemp, 0.35);
vec3 cref2 = mix(scolor2, sctemp, 0.35);
vec3 w1 = sw(f,cref1);
vec3 w2 = sw(1.0-f,cref2);
color = color1*w1 + color2*w2;
color = min(color, 1.0);
color = mix(color, normalize(ctemp + 1e-7)*length(color), 2.0*abs(f-0.5));
color*=brightboost;
color = min(color, 1.0);
vec3 scan3 = vec3(0.0);
float spos = floor((gl_FragCoord.x * 1.000001)/masksize); float spos1 = 0.0;
vec3 tmp1 = pow(sctemp, vec3(1.5/s_gamma));
if (mask == 0.0)
{
spos1 = fract(spos*0.5);
if (spos1 < 0.5) scan3.rb = color.rb;
else scan3.g = color.g;
}
else
if (mask == 1.0)
{
spos1 = fract(spos*0.5);
if (spos1 < 0.5) scan3.rg = color.rg;
else scan3.b = color.b;
}
else
if (mask == 2.0)
{
spos1 = fract(spos/3.0);
if (spos1 < 0.333) scan3.r = color.r;
else if (spos1 < 0.666) scan3.g = color.g;
else scan3.b = color.b;
}
else
if (mask == 3.0)
{
spos1 = fract(spos*0.25);
if (spos1 < 0.25) scan3.r = color.r;
else if (spos1 < 0.50) scan3.rg = color.rg;
else if (spos1 < 0.75) scan3.gb = color.gb;
else scan3.b = color.b;
}
else
{
spos1 = fract(spos*0.25);
if (spos1 < 0.25) scan3.r = color.r;
else if (spos1 < 0.50) scan3.rb = color.rb;
else if (spos1 < 0.75) scan3.gb = color.gb;
else scan3.g = color.g;
}
color = max(mix( mix(color, 1.25*scan3, maskdark), mix(color, scan3, maskbright), tmp1), 0.0);
color = pow(color, vec3(1.0/gamma_out));
FragColor = vec4(color, 1.0);
}
#endif
Ah, interesting. I don’t have that issue with the GLSL version but I do with the slang version (I haven’t pushed all of the updates to the slang repo because of it).