Fractional Bilinear Interpolation

Discuss emulation of the Nintendo Entertainment System and Famicom.

Moderator: Moderators

User avatar
James
Posts: 431
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Fractional Bilinear Interpolation

Post by James »

I spent a few minutes trying to fix it, but didn't have much luck. I'll try again later. In the meantime, here's a comparison of the two shaders in scale mode:

pixellate
Image

fbi
Image
get nemulator
http://nemulator.com
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Fractional Bilinear Interpolation

Post by tepples »

According to that screenshot, pixellate is equivalent to FBI with an interp_adjust_y equal to 0.5, that is, full nearest-neighbor vertically.

Some level of blurring is unavoidable unless you redraw everything as vector graphics.
Near
Founder of higan project
Posts: 1553
Joined: Mon Mar 27, 2006 5:23 pm

Re: Fractional Bilinear Interpolation

Post by Near »

Yeah, I believe the reason pixellate works that way is that higan tries to maintain even multiples in scaling by default.

It is primarily meant to help ease the pain of aspect ratio correction, which is done by changing only the width of the image.

If I recall, SNES Mega Man's energy bar was always the most extreme example of the downside of vertical interpolation. Damn near impossible to make that thing look nice at arbitrary scale factors. And yes I'm aware you could rotate that energy bar 90 degrees and have the same issue with horizontal interpolation (and you could correct aspect by stretching only the height by an uneven multiple.)
User avatar
mikejmoffitt
Posts: 1353
Joined: Sun May 27, 2012 8:43 pm

Re: Fractional Bilinear Interpolation

Post by mikejmoffitt »

One thing doesn't look right in both; it gets dark between the left edge of mario's hat and the background green.
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Fractional Bilinear Interpolation

Post by blargg »

Hah, I wonder if that's due to not converting to a linear RGB space before mixing.
tepples
Posts: 22708
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Fractional Bilinear Interpolation

Post by tepples »

The TVs of the time didn't convert to a linear RGB space either.
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Fractional Bilinear Interpolation

Post by blargg »

I didn't think we were simulating TVs here. I thought we were trying to expand a hard-edged pixel image to a non-integer scaling without some pixels being one unit wider/taller than others. When two different-color pixel edges fall next to each other and there's no mixing, it should look similar to when two fall half way across an output pixel such that they are mixed half and half. If this mixing is done in sRGB, the mixed version will look overly dark, which I think is what is happening here.

EDIT: indeed, that was the cause of that unexplained darkness (darker is naive mixing, lighter is correct mixing):

Image
User avatar
James
Posts: 431
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Fractional Bilinear Interpolation

Post by James »

mikejmoffitt wrote:One thing doesn't look right in both; it gets dark between the left edge of mario's hat and the background green.
blargg wrote:If this mixing is done in sRGB, the mixed version will look overly dark...EDIT: indeed, that was the cause of that unexplained darkness (darker is naive mixing, lighter is correct mixing):
Thanks, guys. Fixed in the WIP version of nemulator.
get nemulator
http://nemulator.com
User avatar
mikejmoffitt
Posts: 1353
Joined: Sun May 27, 2012 8:43 pm

Re: Fractional Bilinear Interpolation

Post by mikejmoffitt »

tepples wrote:The TVs of the time didn't convert to a linear RGB space either.
Fortunately, we're discussing scaling and filtering algorithms, not how televisions displayed. What a television does is absolutely irrelevant here.

As long as we're mentioning TVs, though, it should be pointed out that a lot of televisions do seem to implement this incorrect edge mixing that produces dark borders when any upscaling is being performed.
User avatar
clobber
Posts: 1
Joined: Tue Jan 07, 2014 8:30 pm

Re: Fractional Bilinear Interpolation

Post by clobber »

James wrote:Thanks, guys. Fixed in the WIP version of nemulator.
Hey James, thanks for this!

Would you be kind enough to open source your shader + the latest fixes so that I can see it gets ported to Cg and other formats for cross-platform use in OpenEmu or RetroArch?

There's a pretty good community built around the Cg format (https://github.com/libretro/common-shaders) and your shader looks very nice.
User avatar
James
Posts: 431
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Fractional Bilinear Interpolation

Post by James »

Hi clobber,
clobber wrote:your shader looks very nice.
Thanks!
clobber wrote:Would you be kind enough to open source your shader
Sure. I've updated the code in the first post of this thread. Have at it.

re: sRGB stuff -- that fix is implementation dependent. You can specify that your texture and back buffer are in sRGB color space and let DirectX/OpenGL handle the conversion to/from linear color space. I don't think you can fix this within the shader itself unless you write your own texture sampler.
get nemulator
http://nemulator.com
User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Fractional Bilinear Interpolation

Post by blargg »

I know nothing about shaders, but if you can apply a power function (gamma), do 2.2, mix, then 1/2.2. This will work for sRGB. If things are calculated in more than 8 bits per component, this won't cause any banding.
User avatar
James
Posts: 431
Joined: Sat Jan 22, 2005 8:51 am
Location: Chicago, IL
Contact:

Re: Fractional Bilinear Interpolation

Post by James »

blargg wrote:if you can apply a power function (gamma), do 2.2, mix, then 1/2.2.
Yeah, this is what I was referring to when I said you'd need to write your own texture sampler. If you wanted to do this, you wouldn't be able to use the built-in linear sampler because you can't modify the pre-interpolation values. You'd have to point sample the texture, apply gamma corrections, and manually interpolate.
get nemulator
http://nemulator.com
Post Reply