Unwanted black border on Raspberry Pi with Zfast LCD 720p


#1

Hello all. I’m trying to set up zfast_lcd_720p on my Raspberry Pi but for some reason this shader adds a rather large black border (not so on PC). Is there any explanation for this? Attached is a screenshot and the shader code, any help is appreciated.

/*
    zfast_lcd_720p - A very simple LCD shader (Integer Scaling Only version).

    Copyright (C) 2017 Greg Hogan (SoltanGris42)

    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.


Notes:  This shader just does nearest neighbor scaling of the game and then
		darkens the border pixels to imitate an LCD screen. You can change the
		amount of darkening and the thickness of the borders.  You can also 
		do basic gamma adjustment.
		
*/

//For testing compilation 
//#define FRAGMENT
//#define VERTEX
#define GBAGAMMA 

//Some drivers don't return black with texture coordinates out of bounds
//SNES Classic is too slow to black these areas out when using fullscreen
//overlays.  But you can uncomment the below to black them out if necessary
#define BLACK_OUT_BORDER

// Compatibility #ifdefs needed for parameters
#ifdef GL_ES
#define COMPAT_PRECISION mediump
#else
#define COMPAT_PRECISION
#endif

#pragma parameter BORDERTHICK "Border Thickness" 0.2 0 0.5 0.02
#pragma parameter BORDERMULT "Border Multiplier" 0.7 0.0 1.5 0.05
#ifdef PARAMETER_UNIFORM
uniform COMPAT_PRECISION float BORDERTHICK;
uniform COMPAT_PRECISION float BORDERMULT;
#else
#define BORDERTHICK 0.2
#define BORDERMULT 0.7
#endif

#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

/* COMPATIBILITY
   - GLSL compilers
*/

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;

// compatibility #defines
#define vTexCoord TEX0.xy
#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
#define OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
    gl_Position = MVPMatrix * VertexCoord;
	
	//Terrible adjustment to texture coordinates.
	//Trying to integer scale and center image...
	
	COMPAT_PRECISION float intScale = floor(OutSize.y/InputSize.y);
	COMPAT_PRECISION float adjustTexCoord = OutSize.y/InputSize.y/intScale;
	COMPAT_PRECISION vec2 pixelsToMove = vec2(0.5*(OutSize.y-intScale*InputSize.y)*OutSize.x/OutSize.y,0.5*(OutSize.y-intScale*InputSize.y));
    TEX0.xy = (TexCoord.xy)*adjustTexCoord - (pixelsToMove/TextureSize.xy)/intScale;
}

#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 texture(c, d) COMPAT_TEXTURE(c, d)
#define SourceSize vec4(TextureSize, 1.0 / TextureSize) //either TextureSize or InputSize
#define OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
	COMPAT_PRECISION vec2 texcoordInPixels = TEX0.xy * TextureSize.xy;
	COMPAT_PRECISION vec2 centerCoord = floor(texcoordInPixels.xy)+vec2(0.5,0.5);
	texcoordInPixels.xy = fract(texcoordInPixels.xy);

	float edgeDist = min(texcoordInPixels.x,texcoordInPixels.y);

	vec2 tc = vec2( SourceSize.z*centerCoord.x , SourceSize.w*centerCoord.y);

	vec3 colour = texture2D(Texture, tc).rgb;

#if defined(GBAGAMMA)
	colour.rgb*=0.6+0.4*(colour.rgb); //fake gamma because the pi3 is too slow!
#endif

	if(edgeDist<=BORDERTHICK)
		colour.rgb*=BORDERMULT;
		
#if defined(BLACK_OUT_BORDER)
colour.rgb*=float(tc.x > 0.0)*float(tc.y > 0.0); //why doesn't the driver do the right thing?
#endif

	FragColor.rgba = vec4(colour.rgb , 1.0);
}
#endif

#2

There’s your problem. It forces integer scaling within the shader, regardless of your own settings.


#3

Ok, thanks, I appreciate the reply. If that is the case then why is it integer scaling at 3x and adding a border until 960x640? It should scale 4x (which is 960x640) and add the border up to 1080x720.

Also I’m pretty sure it does not auto enforce integer scale on PC, but I have to check. Edit: yes it does.

Regardless, I managed to get it to look as expected on the Pi with integer scale off in the settings.


#4

Probably a rounding issue with the floor() calculation. You could probably multiply intScale by 1.0001 or so right after it’s calculated to fix it.


#5

Thanks for the tip. I finally decided to try and disable enforced integer scaling, it seems to work OK, here is the snippet that I modified:

void main()
{
    gl_Position = MVPMatrix * VertexCoord;
	
	//Terrible adjustment to texture coordinates.
	//Trying to integer scale and center image...
	
	//COMPAT_PRECISION float intScale = floor(OutSize.y/InputSize.y);
	//COMPAT_PRECISION float adjustTexCoord = OutSize.y/InputSize.y/intScale;
	//COMPAT_PRECISION vec2 pixelsToMove = vec2(0.5*(OutSize.y-intScale*InputSize.y)*OutSize.x/OutSize.y,0.5*(OutSize.y-intScale*InputSize.y));
    //TEX0.xy = (TexCoord.xy)*adjustTexCoord - (pixelsToMove/TextureSize.xy)/intScale;
    TEX0.xy = (TexCoord.xy);
}

As a comment, I found these Zfast 720p specific shaders in a RetroPie forum post after seeing a recommendation for the SNES Classic Mini. I find them quite useful for my setup. They didn’t make it into the RetroPie shader repository for some reason I couldn’t figure out.


#6

Ah, yeah, that should do it. Without that bit, I think it’s just regular zfast-crt.


#7

Not quite, it’s slightly different according to SoltanGris42’s post:

The 720p version of the lcd shader just has one pixel borders which the 1080p version can’t replicate (it can only darken two pixels at a minimum).

Similarly the 720p CRT shader has a 1px scanline and the regular zfast-crt has 2 (maybe configurable, though, I don’t know).