الثلاثاء، 28 ديسمبر 2010

CRT Pixel Shader Filter for SNES Emulation

Update 05/18/2011: More screenshots for more new filters in my new post. :-)

Update 05/02/11
: After many changes, it looks like the CRT shader development has settled down, so there's less need for me to maintain the older versions of the shaders. From here on out, I recommend visiting Screwtape's git repo for all of your XML shader needs (my mediafire account will still be there, but Screwtape has all of mine and more).

CRT.OpenGL.shader and CRT-flat.OpenGL.shader are similar to the shader covered in this post, though they run slightly faster and have no visible artifacts. CRT-simple.OpenGL.shader is a simplified rewrite that should be usable on much older, slower machines.

Here is the original post for informational purposes:
This post covers the use of filters to upscale pixel art--specifically as it applies to SNES emulation--with special attention to CRT reproduction. If you just want the pictures and to download the filters, skip to the bottom of the post.

Background:

As everyone who dabbles in old-school emulation knows, artwork that was intended for a 480i CRT television that has been upscaled to an HD resolution looks like absolute garbage on an LCD monitor. The chunky sprites with their often thick, cartoony outlines just weren't designed to be reproduced with sharp edges resulting from nearest-neighbor upscaling.

To get around this ugly upscaling effect, many emulators now include upscaling interpolation filters, which apply complex mathematical algorithms to the original picture to fill in the gaps between things that are impossible to represent in chunky low-res, such as curves and smooth diagonal lines. You're probably familiar with some of the more common and popular interpolating filters, such as SuperEagle, SuperSaI and HQ2x. Unfortunately, none of these filters gets everything quite right, especially numbers and letters, which can look bubbly or overly smoothed (you can learn more about pixel art scaling algorithms here).

Purists have long been turned off by the inaccuracies of interpolating filters and have instead used scanline masks to try and capture the effect of an interlaced display, relying on the human brain's natural ability to recognize patterns and fill in the gaps between lines (you can learn everything you ever wanted to know about scanlines here). However, this too falls short from a true representation of a CRT display, as it ignores the existence of phosphors--the tiny red-, green- and blue-colored lenses that the electron gun in the back of a CRT tube shoots with a beam of electrons to recreate a colored pixel--and the color bleed that naturally occurs in these displays.

Recently, a number of determined individuals have set out to try and capture all of the different effects of a CRT display, warts and all, to truly reproduce classic pixel art the way it was meant to be viewed.

The Comparisons:
(Each of these images is presented as it would be displayed onscreen, at a resolution of approximately 800x600, then again at 400% scale without any interpolation used when scaling; as always, click the thumbnail to embiggen)

First, we should look at the baseline. This was scaled up to size using nearest-neighbor and is otherwise untouched:
Next, we'll add blargg's NTSC filter, which emulates the noise and color bleed of an NTSC video signal (this filter has several presets; I will only be showing the RGB preset, which reproduces the look of an SNES hooked up via RGB connection [not available in the U.S.], and the RF preset, which reproduces the look of the SNES RF modulator attachment, respectively):
(<- Look at that noisy RF signal!)
As a note, blargg's NTSC filter is so accurate that byuu, the author of bsnes, recommends its use along with bsnes' accuracy profile to achieve proper blending on games that use halftones to simulate transparency (Jurassic Park and Kirby's Dreamland, for example).

Next up, we'll look at cgwg's CRT shader, which includes a phosphor mask and barrel distortion to simulate the screen curvature of a CRT television (just look at those RGB phophors!):
Similarly, there is a version of cgwg's CRT shader, which doesn't include the barrel distortion and represents an idealized flat CRT (actual flat CRTs tended to have slight blurring at the edges where the tube curvature would normally be). Incidentally, this version also has no visible garbage pixels (the occasional black specs that are visible in the curved version):

Pixel Shaders vs. Software Filters

cgwg's CRT shader is a special kind of filter known as a pixel shader. Unlike regular filters, which rely on the CPU to do all of the complex upscaling calculations, pixel shaders draw on the awesome computing power of the video card to do the calculations, thereby leaving the CPU to focus on emulating the SNES. Additionally, since the pixel shader is calculated separately from the filter in bsnes, you can stack blargg's NTSC filter with cgwg's CRT shader:
Finally, for non-purists, we'll look at the combination of cgwg's CRT shader with the popular SuperSaI filter, which creates a pleasing--though not quite as accurate--output:
As amazing as cgwg's CRT shader is already, there is still some room for improvement. For example, the current implementation misses the intensity-based bloom effect on individual phosphors that can be seen in a true CRT. DOLLS (J) [!], one of the contributors to the CRT reproduction effort, intends to write a more complete CRT emulation shader in the future that will incorporate these and other idiosyncracies.

Click here to download cgwg's CRT shader (Also includes the 'flat' version for those who don't like the tube-style curvature; UPDATE: fixed dead link), which is compatible with bsnes and the newest release of snes9x. For more information on CRT emulation, you can check out this highly informative thread on the bsnes forum. For other bsnes-compatible shaders that are not included with the official download, check out my mediafire account.

UPDATE (3/4/2011): Themaister did a rewrite of the flat version of cgwg's CRT shader, moving many of the calculations from fragment to vertex, which provides a substantial ~20% increase in speed (making it usable on many older and less powerful video cards). This rewrite also appears to conform more rigidly to the GLSL shader spec, making it compatible with more cards from different vendors. I have labeled it v4 of cgwg's CRT Flat, and it is available in the aforemented mediafire account.

الجمعة، 24 ديسمبر 2010

bsnes Phoenix GUI Filter Changes

Byuu has added support for binary filters in his Phoenix GUI as of v073, so we can finally use cgwg's CRT Curved shader in conjunction with blargg's NTSC filter! These binary filters are loaded just like shaders, i.e., via a dialog box in the video options.

You can download the available filters, compiled for 64-bit Linux, here or compiled for 32-bit here. These filters do not appear to work on Windows systems.

السبت، 18 ديسمبر 2010

Bsnes Special Chips Dumped

Update (6/28/11): Another chip dumped and emulated! This time, the Cx4 chip (cx4.bin), which is used in Mega Man X2 and X3. The contents of this chip were really just math tables--naturally occurring relationships between numbers--and hence not protected by copyright, so byuu felt comfortable hosting the image on his own site. You can download it here (I'll also mirror it in my mediafire in case byuu's copy disappears for whatever reason).

Update (1/13/11): Dr. Decapitator has dumped and byuu has added low-level emulation for the NEC uPD96050 coprocessor, which is used by the ST-0010 and ST-0011 chips. This allows proper emulation of F1 Race of Champions II and playability for Hayazashi Nidan Morita Shougi. Click here to download the pair. Click here to download all DSP and ST-00* dumps.

Update (12/23/10): All DSP chips have been decapped and dumped. Click here to download the complete, unpadded (as per byuu's preference) set. Furthermore, thanks to the donations of several generous enthusiasts (and a particularly generous contribution from Mr. Krawczyk), all special chips have been paid for. Now, we wait for Dr. Decapitator and byuu to do their thing. ;)

Update (12/21/10): W00t! The DSP chips are all paid for and the decapping and emulation process has already begun. Click here to download a copy of the dsp3.bin file, which enables perfect emulation of the previously unplayable SD Gundam GX (J)!

While many individuals may think that emulating the SNES is long-since perfected, the truth of the matter is that a number of special auxiliary chips were never properly emulated and instead were approximated using what is known as High Level Emulation, or HLE.

To implement HLE, someone monitors what goes into a chip and then monitors what comes out in an attempt to infer the way the chip works. Using this strategy over the course of several years, 4 emulation heavy-hitters were able to deduce much of how these special chips work. However, they were never able to figure out any timing information, so to paraphrase bsnes author byuu, the chips were treated as magical black boxes that instantly convert input to output.

This is just beginning to change, though. Thanks to donations from emulation enthusiasts, renowned chip decapper Dr. Decapitator was able to shave down to the guts of the DSP-1B chip and see exactly what's going on inside. This breakthrough will eventually lead to the squashing of several bugs and will enable near-perfect emulation of both Super Mario Kart and Pilotwings.

However, to run any games that require the DSP-1B chip, you will now need to put a copy of the dumped image into the same directory as the game ROM image. Click here to download a copy of the dsp1b.bin file.

As awesome as this is, there are still a number of chips that need to be decapped (you can read all about it here) and byuu is running a collection to get things started in this thread. If you would like to contribute, visit byuu's official donations page. It's your chance to be a part of SNES emulation history.

الأربعاء، 1 ديسمبر 2010

First Impressions of Ubuntu 11.04 Natty Narwhal

I decided to try out the latest build of Ubuntu's next future release, version 11.04 Natty Narwhal, in a VirtualBox VM. I downloaded a pre-alpha daily build and so far have been impressed with the stability and ease of installation.

As with Maverick, the installer is clean, polished and easy, and continues the trend of asking most of the configuration questions once the actual installation process has started to save time. You also have the opportunity to install third party programs, such as Fluendo's MP3 codec, at the time of installation, which is nice.
At this point, I don't think they've switched over to the proposed Unity interface by default, so I can't say whether I'll have a better experience with it now than I did on my netbook a few months back, but I'll update this post with more information as soon as I have it.

As for its performance in a VM environment, everything is stable and the VBox Guest Additions installed just fine, imparting mouse pointer integration and support for arbitrary resolutions. The only thing lacking is 3D acceleration within the VM, but I assume this will be supported at a later date.

Overall, the experience is not significantly different from that of 10.10 Maverick so far, but I expect things to change rapidly in the near future. I'll keep this post updated with any changes I find.