Skip to content

Instantly share code, notes, and snippets.

@Sgeo
Created March 1, 2023 06:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sgeo/b44b89678879b3aa60b939bb3b985f99 to your computer and use it in GitHub Desktop.
Save Sgeo/b44b89678879b3aa60b939bb3b985f99 to your computer and use it in GitHub Desktop.
#version 120
#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
uniform sampler2D rubyTexture;
uniform vec2 rubyTextureSize;
uniform vec2 rubyInputSize;
uniform vec2 rubyOutputSize;
COMPAT_ATTRIBUTE vec4 a_position;
COMPAT_VARYING vec2 v_texCoord;
COMPAT_VARYING float left;
COMPAT_VARYING float right;
bool isBlack(in vec4 color) {
return color.r < 0.01 && color.g < 0.01 && color.b < 0.01;
}
bool isWhite(in vec4 color) {
return color.r > 0.99 && color.g > 0.99 && color.b > 0.99;
}
vec4 indicatorTexture(in sampler2D sampler, in vec2 uv)
{
vec2 texCoord = floor(uv * rubyTextureSize) - vec2(0.5); // Indicator is at the bottom, -0.5 so we don't fall off the bottom
vec2 invTexSize = 1.0 / rubyTextureSize;
return COMPAT_TEXTURE(rubyTexture, texCoord * invTexSize);
}
void main()
{
gl_Position = a_position;
v_texCoord = vec2(a_position.x + 1.0, 1.0 - a_position.y) / 2.0 * rubyInputSize / rubyTextureSize;
vec4 middleIndicatorColor = indicatorTexture(rubyTexture, vec2(0.5, 1.0) * rubyInputSize / rubyTextureSize);
vec4 leftIndicatorColor = indicatorTexture(rubyTexture, vec2(0.1, 1.0) * rubyInputSize / rubyTextureSize);
vec4 rightIndicatorColor = indicatorTexture(rubyTexture, vec2(0.9, 1.0) * rubyInputSize / rubyTextureSize);
bool leftIndicatorValid = isWhite(leftIndicatorColor);
bool rightIndicatorValid = isBlack(rightIndicatorColor);
left = 1.0;
right = 1.0;
if(leftIndicatorValid && rightIndicatorValid) {
if(isBlack(middleIndicatorColor)) {
right = 0.0;
} else if(isWhite(middleIndicatorColor)) {
left = 0.0;
}
}
}
#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 highp
#else
#define COMPAT_PRECISION
#endif
uniform vec2 rubyTextureSize;
uniform vec2 rubyInputSize;
uniform sampler2D rubyTexture;
/*
The following code allows the shader to override any texture filtering
configured in DOSBox. if 'output' is set to 'opengl', bilinear filtering
will be enabled and OPENGLNB will not be defined, if 'output' is set to
'openglnb', nearest neighbour filtering will be enabled and OPENGLNB will
be defined.
If you wish to use the default filtering method that is currently enabled
in DOSBox, use COMPAT_TEXTURE to lookup a texel from the input texture.
If you wish to force nearest-neighbor interpolation use NN_TEXTURE.
If you wish to force bilinear interpolation use BL_TEXTURE.
If DOSBox is configured to use the filtering method that is being forced,
the default hardware implementation will be used, otherwise the custom
implementations below will be used instead.
These custom implemenations rely on the `rubyTextureSize` uniform variable.
The code could calculate the texture size from the sampler using the
textureSize() GLSL function, but this would require a minimum of GLSL
version 130, which may prevent the shader from working on older systems.
*/
#if defined(OPENGLNB)
#define NN_TEXTURE COMPAT_TEXTURE
#define BL_TEXTURE blTexture
vec4 blTexture(in sampler2D sampler, in vec2 uv)
{
// subtract 0.5 here and add it again after the floor to centre the texel
vec2 texCoord = uv * rubyTextureSize - vec2(0.5);
vec2 s0t0 = floor(texCoord) + vec2(0.5);
vec2 s0t1 = s0t0 + vec2(0.0, 1.0);
vec2 s1t0 = s0t0 + vec2(1.0, 0.0);
vec2 s1t1 = s0t0 + vec2(1.0);
vec2 invTexSize = 1.0 / rubyTextureSize;
vec4 c_s0t0 = COMPAT_TEXTURE(sampler, s0t0 * invTexSize);
vec4 c_s0t1 = COMPAT_TEXTURE(sampler, s0t1 * invTexSize);
vec4 c_s1t0 = COMPAT_TEXTURE(sampler, s1t0 * invTexSize);
vec4 c_s1t1 = COMPAT_TEXTURE(sampler, s1t1 * invTexSize);
vec2 weight = fract(texCoord);
vec4 c0 = c_s0t0 + (c_s1t0 - c_s0t0) * weight.x;
vec4 c1 = c_s0t1 + (c_s1t1 - c_s0t1) * weight.x;
return (c0 + (c1 - c0) * weight.y);
}
#else
#define BL_TEXTURE COMPAT_TEXTURE
#define NN_TEXTURE nnTexture
vec4 nnTexture(in sampler2D sampler, in vec2 uv)
{
vec2 texCoord = floor(uv * rubyTextureSize) + vec2(0.5);
vec2 invTexSize = 1.0 / rubyTextureSize;
return COMPAT_TEXTURE(sampler, texCoord * invTexSize);
}
#endif
COMPAT_VARYING vec2 v_texCoord;
COMPAT_VARYING float left;
COMPAT_VARYING float right;
void main()
{
vec2 new_texCoord = v_texCoord;
new_texCoord.x *= 2.0;
float eyeMultiplier = left;
if(new_texCoord.x > (rubyInputSize.x / rubyTextureSize.x)) {
new_texCoord.x -= 1.0 * rubyInputSize.x / rubyTextureSize.x;
eyeMultiplier = right;
}
if(0.0 <= new_texCoord.x && new_texCoord.x <= 1.0) {
FragColor = COMPAT_TEXTURE(rubyTexture, new_texCoord) * eyeMultiplier;
} else {
FragColor = vec4(0.0, 0.0, 0.0, 0.0);
}
//if(new_texCoord.x > 0.5 * rubyInputSize.x / rubyTextureSize.x) {
//FragColor = vec4(0.0, 0.0, 1.0, 1.0);
//}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment