Converting MiSTer FPGA Composite Blending shader to GLSL

Can you share the source for it too please? Thank you!

1 Like

Sure, it’s very similar. Try this one:

// 50% horizontal blend; quick and dirty gamma correction
// by hunterk
// license: public domain

#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 TexCoord;
COMPAT_VARYING vec4 TEX0;

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;
   TEX0.xy = TexCoord.xy;
   TEX0.zw = TexCoord.xy - vec2(SourceSize.z, 0.);
}

#elif defined(FRAGMENT)

#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

#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_TEXTURE texture
out COMPAT_PRECISION vec4 FragColor;
#else
#define COMPAT_VARYING varying
#define FragColor gl_FragColor
#define COMPAT_TEXTURE texture2D
#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 OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
   vec3 curr_px = COMPAT_TEXTURE(Source, TEX0.xy).rgb;
   vec3 prev_px = COMPAT_TEXTURE(Source, TEX0.zw).rgb;

   curr_px *= curr_px;
   prev_px *= prev_px;

   FragColor = vec4(sqrt((curr_px + prev_px) / 2.), 1.0);
} 
#endif
3 Likes

Thanks! I don’t mind. That might be up to @hunterk and the rest of LibRetro team I suppose. It shouldn’t be too difficult as its just a few presets which attempt to improve on what’s already there, especially in terms of minimizing distracting artifacts, brightness improvements and most importantly finding the highest resolution where dither blending would still work.

I’d be honoured if they could be included.

You mean the ones in the “Video options”?

These don’t work. If i use GenesisPlusGX, i get a squished image that only takes half of my screen. If i use BlastEM, it just won’t do anything.

2 Likes

GPGX has it built-in and modified to work with the Genesis/MD video output.

2 Likes

That’s strange, they’ve always worked fine for me.

I was referring to the filters that are included in the Core Options–>Video.

It’s the ones made for SNES in the Video Filters Folder that used to do what you described on certain cores.

I haven’t used the ones in the Video Options in a while because I now use my custom Video Filter presets which work perfectly.

You can give them a try and see if they work for you. They’re in my CyberLab Mega Bezel Death To Pixels Shader Preset Pack.

1 Like

Ok i downloaded the new filters in the archive and these work.

The core options do work but the composite option creates some banding artifacts on Sonic’waterfalls when you scroll. These new video filter options though work nicely.

Thanks :slight_smile:

2 Likes

I second this. Just tried it with some random cores and they work and look marvelous.

1 Like

Is it just the filt presets that are changed? or do the libs need to have changes in the source?

2 Likes

Just the filt presets. blargg_ntsc_snes.dll remains the same.

That’s why I suggested @Cyber Genesis .filt presets to be bundled with Retroarch. Others have side effects (GPGX Comsposite), dont eliminate dithering (GPGX S-Video and RGB) or squish image (SNES presets from filters/video folder)

2 Likes

If it’s just the presets, yeah, that should be pretty straightforward to include. If it needed modifications to the lib, that would take some more due diligence.

I think we can move on this pretty quickly. If anyone wants to put in a PR for it, go for it. Otherwise, I’ll get one going later on.

3 Likes

You can go right ahead. How can I go about doing a PR though? Maybe this can be one of my first pull requests and additions to my so far unused GitHub Repo?

You fork the original libretro repo to your own account, make your changes locally, make a commit and push it up to your repo, then go to the ‘pull requests’ tab and make one against the parent repo (it chooses it automatically by default). Once submitted, we can check it out and review it and if everything looks good, we merge it into the main codebase.

3 Likes

Thanks a lot. I’ll try this over the next couple days. If I don’t get through, you can go ahead and add it.

One thing I noticed is that the built-in Genesis Composite Filter creates a rainbow effect but mine don’t, but they do de-dither and they do it cleanly. I use them in conjunction with presets with sharpness cranked to the highest point before unwanted artifacts.

I have those in my pack but users can also crank sharpness up in presets of their own and de-dithering will still work.

Update:

@hunterk Just created my first PR!

Do you need any additional information?

2 Likes

The two genesis filters in your pack both do de-dithering. Even the s-video one. I’m glad it does because the picture is cleaner than the composite one but also merges the dithering.

The built in s-video filter looks good but doesn’t completely merge the dithering, just slightly.

I imagine on the real machine the built in s-video filter is more accurate? Did s-video merge dithering as such?

Again, even if it’s not accurate, it would be better to rename it as “composite HQ” or something instead of fixing it. This is the best de-dithering filter i used so far.

2 Likes

I really don’t know. As a child I didn’t even notice things like the transparency on the waterfalls and I’ve never seen a Genesis with S-Video out.

I like the name as it is though. The terms Composite, S-Video and RGB are all relative here and my system is that consoles have their own characteristics and modules so that in the context of Genesis, which supposedly had degraded outputs compared to other systems (at least composite) this would be S-Video.

In the filter this is simply a resolution range with many possibilities in between the default Composite, S-Video and RGB settings.

So let’s pretend it’s S-Video for now. If users want “accurate” naming they can use the built-in filters.

I think 2 “Composite” presets for one system might be a bit confusing and messy.

Instead, my naming system is that Composite is lowest resolution, S-Video is higher than Composite resolution and RGB is higher than S-Video resolution.

I prefer things to be a bit more fun, casual, “dummy” friendly, commercial, marketing style than technical, nerdy and super precise so that even a caveman or baby can understand.

Users can also rename it internally to whatever is more meaningful to them.

I also wouldn’t want the naming to be out of sync with my internal development and naming.

I’m really glad you like it. You have to thank @Juandiegous who’s work showed me that it was possible to edit the filter presets.

You mentioned renaming as an alternative to fixing. I offer these presets simply as a way for others to enjoy what I experience, so there isn’t anything to fix because S-Video can de-dither better than the built-in. This is just the CyberLab experience at this point in time that more users can now experience for themselves without having to download my preset pack. That’s also why they’re called “CyberLab_Special-Edition”.

My unique twist on the way I do things is what makes them special.

1 Like

The real machine doesn’t have s-video without a mod. You can watch a video here, s-video makes it way cleaner.

3 Likes

@hunterk Is it possible to add 50% vertical blend to this shader? I always wanted simple and fast blur shader. I may as well use this one for that purpose.

// 50% horizontal blend; quick and dirty gamma correction
// by hunterk
// license: public domain

#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 TexCoord;
COMPAT_VARYING vec4 TEX0;

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;
   TEX0.xy = TexCoord.xy;
   TEX0.zw = TexCoord.xy - vec2(SourceSize.z, 0.);
}

#elif defined(FRAGMENT)

#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

#if __VERSION__ >= 130
#define COMPAT_VARYING in
#define COMPAT_TEXTURE texture
out COMPAT_PRECISION vec4 FragColor;
#else
#define COMPAT_VARYING varying
#define FragColor gl_FragColor
#define COMPAT_TEXTURE texture2D
#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 OutSize vec4(OutputSize, 1.0 / OutputSize)

void main()
{
   vec3 curr_px = COMPAT_TEXTURE(Source, TEX0.xy).rgb;
   vec3 prev_px = COMPAT_TEXTURE(Source, TEX0.zw).rgb;

   curr_px *= curr_px;
   prev_px *= prev_px;

   FragColor = vec4(sqrt((curr_px + prev_px) / 2.), 1.0);
} 
#endif

Sure, that’s possible, but I think you’re better off using the 3x3 blur from the ‘blurs’ directory.

3x3 is not available in GLSL. Also tried .slang version on pc and it doesnt blend genesis dithering unlike the one you converted.