‏إظهار الرسائل ذات التسميات pixel shader. إظهار كافة الرسائل
‏إظهار الرسائل ذات التسميات pixel shader. إظهار كافة الرسائل

الأحد، 5 يونيو 2011

Cg Shader Tutorial For Emulators

copyright Hans-Kristian "Themaister" Arntzen
04.06.2011 (reposted with permission)

Introduction

This document is for a (fresh) shader developer that wants to develop shader programs for use in various emulators. Shader programs run on your GPU, and thus enables very sophisticated effects to be performed on the picture which might not be possible in real-time on the CPU. Some introduction to shader programming in general is given, so more experienced developers that only need reference for the specification may just skip ahead.

Current emulators that supports the specification explained here to a certain degree are:
– SSNES
– Various PlayStation 3 emulators (SNES9x, Genesis GX, FCEU, VBA)
– SNES9x Win32

There are three popular shader languages in use today:
– HLSL (High-Level Shading Language, Direct3D)
– GLSL (GL Shading Language, OpenGL)
– Cg (HLSL/GLSL, nVidia)

The spec is for the Cg shading language developed by nVidia. It «wraps» around OpenGL and HLSL to make shaders written in Cg quite portable. It is also the shading language implemented on the PlayStation 3, thus increasing the popularity of it. The specification itself can be found here:

The Rendering Pipeline

With shaders you are able to take control over a large chunk of the GPUs inner workings by writing your own programs that are uploaded and run on the GPU. In the old days, GPUs were a big black box that was highly configurable using endless amount of API calls. In more modern times, rather than giving you endless amounts of buttons, you are expected to implement the few «buttons» you actually need, and have a streamlined API.

The rendering pipeline is somewhat complex, but we can in general simplify it to:
– Vertex processing
– Rasterization
– Fragment processing
– Framebuffer blend

We are allowed to take control of what happens during vertex processing, and fragment processing.

A Cg Program

If you were to process an image on a CPU, you would most likely do something like this:
for (unsigned y = 0; y < height; y++) {
for (unsigned x = 0; x < width; x++) {
out_pixel[y][x] = process_pixel(in_pixel[y][x], y, x);
}
}
We quickly realize that this is highly serial and slow. We see that out_pixel[y][x] isn't dependent on out_pixel[y + k][x + k], so we see that we can parallelize quite a bit.

Essentially, we only need to implement process_pixel() as a single function, which is called thousands, even millions of time every frame. The only purpose in life for process_pixel() is to process an input, and produce an output. No state is needed, thus, a «pure» function in CS terms.

For the Cg program, we need to implement two different functions.

main_vertex() takes care of transforming every incoming vertex from camera space down to clip space. This essentially means projection of 3D (coordinates on GPU) down to 2D (your screen). Since we're dealing with old school emulators here, which are already 2D, the vertex shading is very trivial.

Vertex shaders get various coordinates as input, and uniforms. Every vertex emitted by the emulator is run through main_vertex which calculates the final output position. For our emulators this is just 4 times, since we're rendering a quad on the screen :D 3D games would obviously have a lot more vertices.

While coordinates differ for each invocation, uniforms are constant throughout every call. Think of it as a global variable that you're not allowed to change.

Vertex shading can almost be ignored altogether, but since the vertex shader is run only 4 times, and the fragment shader is run millions of times per frame, it is a good idea to precalculate values in vertex shader that can later be used in fragment shader. There are some limitations to this which will be mentioned later.

main_fragment() takes care of calculating a pixel color for every single output pixel on the screen. If you're playing at 1080p, the fragment shader will have to be run 1920 * 1080 times! This is obviously straining on the GPU unless the shader is written efficiently.

Obviously, main_fragment is where the real action happens. For many shaders we can stick with a «dummy» vertex shader which does some very simple stuff.

The fragment shader receives a handle to a texture (the game frame itself), and the texture coordinate for the current pixel, and a bunch of uniforms.

A fragment shaders final output is a color, simple as that. Processing ends here.

Hello World!

We'll start off with the basic vertex shader. No fancy things are being done. You'll see a similiar vertex shader in most of the Cg programs out there in the wild.
void main_vertex(
float4 pos : POSITION,
out float4 out_pos : POSITION,

uniform float4x4 modelViewProj,

float4 color : COLOR,
out float4 out_color : COLOR,

float2 tex : TEXCOORD,
out float2 out_tex : TEXCOORD
)
{
out_pos = mul(modelViewProj, pos);
out_color = color;
out_tex = tex;
}
This looks vaguely familiar to C, and it is. Cg stands for «C for graphics» after all. We notice some things are happening, notable some new types.

float4 is a vector type. It contains 4 elements. It could be colors, positions, whatever. It's used for vector processing which the GPUs are extremely efficient at.

We see various semantics. The POSITION semantic means that the variable is tied to vertex coordinates. We see that we have an input POSITION, and an output (out) POSITION. We thus transform the input to the output with a matrix multiply with the current model-view projection. Since this matrix is the same for every vertex, it is a uniform. Remember that the variable names DO matter. modelViewProj has to be called exactly that, as the emulator will pass the MVP to this uniform. It is in the specification.

Since we have semantics for the POSITION, etc, we can call them whatever we want, as the Cg environment figures out what the variables mean.

The transformation happens here:
out_pos = mul(modelViewProj, pos);
The COLOR semantic isn't very interesting for us, but the example code in nVidias Cg documentation includes it, so we just follow along :)

TEXCOORD is the texture coordinate we get from the emulator, and generally we just pass it to the fragment shader directly. The coordinate will then be linearly interpolated across the fragments. More complex shaders can output (almost) as many variables they want, that will be linearly interpolated for free to the fragment shader.

We also need a fragment shader to go along with the vertex shader, and here's a basic shader that only outputs the pixel as-is. This is pretty much the result you'd get if you didn't run any shader (fixed-function) at all.

float4 main_fragment(uniform sampler2D s0 : TEXUNIT0, float2 tex : TEXCOORD) : COLOR
{
return tex2D(s0, tex);
}
This is arguably simpler than the vertex shader :D Important to notice are:

sampler2D is a handle to a texture in Cg. The semantic here is TEXUNIT0, which means that it refers to the texture in texture unit 0. This is also part of the specification.

float2 tex : TEXCOORD is the interpolated coordinate we received from the vertex shader.

tex2D(s0, tex); simply does texture lookup and returns a COLOR, which is emitted to the framebuffer. Simple enough. Practically every fragment does more than one texture lookup. For example, classic pixel shaders look at the neighbor pixels as well to determine the output. But where is the neighbor pixel? We'll revise the fragment shader and try to make a really blurry shader to demonstrate. We now need to pull up some uniforms. We need to know how to modify our tex coordinates so that it points to a neighbor pixel.
struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
float frame_count;
};

float4 main_fragment(uniform sampler2D s0 : TEXUNIT0, uniform input IN, float2 tex :
TEXCOORD) : COLOR
{
float4 result = float4(0.0);
float dx = 1.0 / IN.texture_size.x;
float dy = 1.0 / IN.texture_size.y;

// Grab some of the neighboring pixels and blend together for a very mushy blur.
result += tex2D(s0, tex + float2(-dx, -dy));
result += tex2D(s0, tex + float2(dx, -dy));
result += tex2D(s0, tex + float2(0.0, 0.0));
result += tex2D(s0, tex + float2(-dx, 0.0));
return result / 4.0;
}
Here we use IN.texture_size to determine the the size of the texture. Since GL maps the whole texture to the interval [0.0, 1.0], 1.0 / IN.texture_size means we get the offset for a single pixel, simple enough. Almost every shader uses this. We can calculate these offsets in vertex shader to improve performance since the coordinates are linearly interpolated anyways, but that is for another time ... ;)

Putting It Together

The final runnable product is a single .cg file with the main_vertex and main_fragment functions added together. Not very complicated. For the icing on the cake, you should add a license header. It's just a comment ;)
/*
Stupid blur shader.
Author: Your friendly neighbor.
License: We don't have those things! :(
*/

struct input
{
float2 video_size;
float2 texture_size;
float2 output_size;
float frame_count;
};

void main_vertex(
float4 pos : POSITION,
out float4 out_pos : POSITION,

uniform float4x4 modelViewProj,

float4 color : COLOR,
out float4 out_color : COLOR,

float2 tex : TEXCOORD,
out float2 out_tex : TEXCOORD
)
{
out_pos = mul(modelViewProj, pos);
out_color = color;
out_tex = tex;
}

float4 main_fragment(uniform sampler2D s0 : TEXUNIT0, uniform input IN, float2 tex :
TEXCOORD) : COLOR
{
float4 result = float4(0.0);
float dx = 1.0 / IN.texture_size.x;
float dy = 1.0 / IN.texture_size.y;

// Grab some of the neighboring pixels and blend together for a very mushy blur.
Result of the shader shown:
As you can see, it's not a practical shader, but it shows the blurring effect to the extreme :)

Expanding Further

It doesn't stop here. A single .cg shader like this is by far the most common way to distribute a shader, but it has some problems.

– There is no way to set the filtering used for the texture. Some filters require nearest filtering, some require linear, although nearest is by far the most common.
– No way to set custom output scale. We're forced to output to full viewport size, which hurts some filters like HQx and friends.
– No way to use multiple shaders. (multi-pass, FBO).
– No way to grab external lookup textures. (Borders, etc)

This has been solved in various ways for the different emulators. The way to do metadata like this varies for the Cg implementations. As of writing, SNES9x Win32 hasn't addressed this, but will hopefully in the near future.

SSNES and the PS3 emulators solve this with a config file.

Lookup textures

We'll first mention a very popular feature among the PS3 emulators, the ability to access external textures. This means we have several samplers available for use. In the config file, we define the textures as so:
textures = "foo;bar"
foo = path_foo.png # In SSNES this is relative, but PS3 emus are absolute path for now.
bar = bar_foo.png
foo_linear = true # Linear filtering for foo.
bar_linear = true # Linear filtering for bar.
PS3 emus use PNG as the main format, SSNES can use whatever if Imlib2 support is compiled in. If not, it's restricted to lop-left ordered, non-RLE TGA.

From here on, «foo» and «bar» can be found as uniforms in the shaders. The texture coordinates for the lookup texture will be found in TEXCOORD1. This can simply be passed along with TEXCOORD0 in the vertex shader as we did with TEXCOORD0. Here we make a fragment shader that blends in two background picture at a reduced opacity. Do NOT assign lookup textures to a certain TEXUNIT, Cg will assign a fitting texture unit to the sampler.
float4 main_fragment(uniform sampler2D s0 : TEXUNIT0,
uniform sampler2D foo, uniform sampler2D bar,
float2 tex : TEXCOORD0, float2 tex_lut : TEXCOORD1) : COLOR
{
float4 bg_sum = (tex2D(foo, tex_lut) + tex2D(bar, tex_lut)) * 0.15;
return lerp(tex2D(s0, tex), bg_sum, bg_sum.a); // Alpha blending. :D
}
In the PS3 emus there is a magic «bg» sampler that is assigned to the current border selected in the menu. It is not needed to define this in any config file.

Here's an example of what can be achieved using borders (which are just a simple lookup texture):

Multipass

It is sometimes feasible to process an effect in several steps. SSNES and PS3 emus differ here.
SSNES uses the same config file where you can do:
shaders = 2
shader0 = pass1.cg
shader1 = pass2.cg

scale_type0 = source
scale0 = 2.0
filter_linear0 = true
filter_linear1 = false
The PS3 emulators handle the same case in the menu generally. There is however a new config system on the way that will handle these kinds of configs.

Game-aware Shaders

This is a new and exiting feature. It allows shaders to grab data from the emulator state itself, such as RAM data. This is only implemented for SNES so far, but the idea is quite extendable and portable.

The basic idea is that we capture RAM data in a certain way (semantic if you will) from the SNES, and pass it as a uniform to the shader. The shader can thus act on game state in interesting ways.

As a tool to show this feature, we'll focus on replicating the simple tech demo shown on YouTube:

What happens is that when Mario jumps in the water, the screen gets a «watery» effect applied to it, with a rain lookup texture, and a wavy effect. When he jumps out of the water, the water effect slowly fades away.

We thus need to know two things:
– Is Mario currently in water or not?
– If not, how long time was it since he jumped out?

Since shaders do not have state associated with it, we have to let the environment provide the state we need in a certain way. We'll call this concept a semantic.

To capture a RAM value directly, we can use the «capture» semantic. To record the time when the RAM value last changed, we can use the «transition» semantic. We obviously also need to know where in RAM we can find this information. Luckily, the guys over at SMW Central know the answer:

We see:
$7E:0075, byte, Flag, Player is in water flag. #$00 = No; #$01 = Yes.

Bank $7E and $7F are mapped to WRAM $0000-$FFFF and $10000-$1FFFF respectively. Thus, our WRAM address is $0075.

In the config file, we can now set up the uniforms we'll want to be captured in the config file.
imports = "mario_water;mario_water_time"
mario_water_semantic = capture # Capture the RAM value as-is.
mario_water_wram = 0075 # This value is hex!
mario_water_time_semantic = transition # Capture the frame count when this variable last
changed. Use with IN.frame_count, to create a fade-out effect.
mario_water_time_wram = 0075
The amount of possible «semantics» are practically endless. It might be worthwhile to attempt some possibility to run custom code that keeps track of the shader uniforms in more sophisticated ways later on. Do note that there is also a %s_mask value which will let you bitmask the RAM value to check for bit-flags more easily.

Now that we got that part down, let's work on the shader design. In the fragment shader we simply render both the full water effect, and the «normal» texture, and let a «blend» variable decide. We can say that 1.0 is full water effect, 0.0 is no effect. We can start working on our vertex shader. We will do something useful here for once.
struct input
{
float frame_count; // We only need frame count. :D
};

void main_vertex(
float4 pos : POSITION,
out float4 out_pos : POSITION,
uniform float4x4 modelViewProj,
float4 color : COLOR,
out float4 out_color : COLOR,
float2 tex : TEXCOORD0,
out float2 out_tex : TEXCOORD0,
float2 tex1 : TEXCOORD1,
out float2 out_tex1 : TEXCOORD1,

uniform float mario_water, // Even if the data should have been int, Cg doesn't seem to
support integer uniforms :(
uniform float mario_water_time,
uniform input IN,
out float blend_factor // Blend factor is passed to fragment shader. We'll output the
same value in every vertex, so every fragment will get the same value for blend_factor
since there is nothing to interpolate.
)
{
out_pos = mul(modelViewProj, pos);
out_color = color;
out_tex = tex;
out_tex1 = tex1;
float transition_time = 0.5 * (IN.frame_count – mario_water_time) / 60.0;
if (mario_water > 0.0)
blend_factor = 1.0; // If Mario is in the water ($0075 != 0), it's always 1 ...
else
blend_factor = exp(-transition_time); // Fade out from 1.0 towards 0.0 as
transition_time grows larger.
}
All fine and dandy so far, now we just need to use this blend_factor in our fragment shader somehow ... Let's move on to the fragment shader where we blend :D
const float2 src0 = float2(0.6, 0.7);
const float2 src1 = float2(0.9, 0.9);
const float2 src2 = float2(-0.6, 0.3);
const float2 src3 = float2(0.1, 0.4);
const float2 src4 = float2(0.1, 0.4);
const float2 src5 = float2(0.5, 0.5);
const float2 src6 = float2(-1.0, 1.0);
float apply_wave(float2 pos, float2 src, float cnt)
{
float2 diff = pos - src;
float dist = 300.0 * sqrt(dot(diff, diff));
dist -= 0.15 * cnt;
return sin(dist);
}
// Fancy shizz to create a wave.
float4 water_texture(float4 output, float2 scale, float cnt)
{
float res = apply_wave(scale, src0, cnt);
res += apply_wave(scale, src1, cnt);
res += apply_wave(scale, src2, cnt);
res += apply_wave(scale, src3, cnt);
res += apply_wave(scale, src4, cnt);
res += apply_wave(scale, src5, cnt);
res += apply_wave(scale, src6, cnt);
return output * (0.95 + 0.012 * res);
}
float4 main_fragment
(
uniform input IN,
float2 tex : TEXCOORD0, uniform sampler2D s0 : TEXUNIT0,
uniform sampler2D rain, float2 tex1 : TEXCOORD1,
in float blend_factor // Passed from vertex
) : COLOR
{
float4 water_tex = water_texture(tex2D(s0, tex), tex1, IN.frame_count);
float4 normal_tex = tex2D(s0, tex);
float4 rain_tex = tex2D(rain, tex1);
// First, blend normal and water texture together,
// then add the rain texture with alpha blending on top :)
return lerp(lerp(normal_tex, water_tex, blend_factor), rain_tex, rain_tex.a *
blend_factor * 0.5);
}
The SNES9x-PS3 config file:
imports = "mario_water;mario_water_time"
mario_water_semantic = capture
mario_water_time_semantic = transition
mario_water_wram = 0075
mario_water_time_wram = 0075
textures = rain
rain = "/dev_hdd0/game/SNES90000/USRDIR/borders/Centered-1080p/overlays/none-overlay.png"
rain_linear = true
The shader itself could be added to this config, but it can also be used from the regular menus in case you want to test different approaches to the same RAM value.

SSNES config file:
shaders = 1
shader0 = mario.cg
filter_linear0 = true
imports = "mario_water;mario_water_time"
mario_water_semantic = capture
mario_water_time_semantic = transition
mario_water_wram = 0075
mario_water_time_wram = 0075
textures = rain
rain = rain.tga
rain_linear = true
As you can see, these formats are very similiar. The differences are mostly for historical reasons, and might become closer in the future.

How to test when developing for SNES9x-PS3?

To develop these kinds of shaders, I'd recommend using SSNES w/ Cg support, and bSNES as a SNES debugger to peek at RAM values (build it yourself with options=debugger). After written, the shader should translate nicely over to SNES9x-PS3 with some slight changes to the config.

Here are some screenshots of the mario effect we developed. Obviously this is a very simple example showing what this thing can do :) It's not confined to overlays. The imagination is the limit here.

Before water ...
In water :D

الأحد، 15 مايو 2011

More Emulator Pixel Shaders (CRT updated and more)

I'm just going to dive right in with the pictures in this post. If you want/need some backstory, read my previous CRT shader post. These shaders are all available in Screwtape's bsnes git repo, except for Beam4, which can be downloaded here, and NTSC, which can be downloaded here (both mirrored in my mediafire in case the pastebins expire before Screwtape adds them to his repo).

UPDATE (5/30/11): Added shots of caligari's phosphor21x shader (a.k.a. caligari's scanlines in Screwtape's repo). UPDATE (6/3/11): Added Themaister's Dot 'n' Bloom, available from Screwtape's repo. UPDATE (6/18/11): Added caligari's scanlines - rgb triad.

All images are taken at 3x scale, followed by that same image blown up 400% with no further interpolation. As always, click the thumbnails to embiggen.

4xBR

This one uses an algorithm similar to HQ4x and it shows.

Beam4

This is a multipass scanline shader from cgwg that also simulates an electron beam that takes longer to rise than to fall. It is perfect for producing the pseudo-hires transparency used in Kirby's Dreamland 3 and Jurassic Park. Sadly, it only works in emulators that support shader spec 1.1 (at the time of this writing, Themaister's SSNES and PS3 homebrew emus written by Dante Ali).

Bicubic

This one's pretty straightforward, using an algorithm similar to that of bilinear filtering (aka, 'smooth video'). It's a little sharper and preserves edges a little better.

CRT.OpenGL

This is the latest and greatest version of the collaborative CRT shader. The screen curvature is user-definable, based on changing a single value in the helpfully commented shader code. As you can see, it looks totally amazing. It also runs a lot faster than it used to, so give it a shot even if your machine couldn't handle it before.

CRT-simple.OpenGL

This one is very closely related to the aforementioned CRT shader, only this one is optimized for weaker cards while only sacrificing a tiny bit of accuracy. If your machine can't run the full-fledged CRT shader, this one is your last good chance.

Dot 'n' Bloom

Another cool one from Themaister, Dot 'n' Bloom treats each pixel like a single, distinct dot and then adjusts its size based on brightness, such that brighter dots appear larger than darker ones, similar to a CRT electron beam (though this shader is not attempting to reproduce a CRT, it resembles one). He then added a bit of bloom to blend things together some. The results look really nice in motion (especially on Super Metroid :D) and the shader runs extremely fast.

Lanczos 4-tap

Another fairly straightforward interpolation filter, this one runs quite fast even on weaker hardware. You can learn more about the Lanczos algorithm and its use in resampling at the Wikipedia.

Lanczos 6-tap

As you might have guessed, this one is very similar to the 4-tap Lanczos, but it is further sharpened, which leads to the ringing/halo effect around sprites.

NTSC

This multipass shader is the result of cgwg and Themaister porting blargg's awesome NTSC filter to GLSL. It looks quite nice and doesn't bog down the CPU like the regular filter version. Unfortunately, it only works on shader spec 1.1-compatible emus.

Phosphor21x
This is caligari's implementation of an algorithm originally written by xythen in Matlab, which served as the inspiration for the work that later became CRT.OpenGL. This shader builds on xythen's work to be essentially scale factor-independent, except it looks a little weird at 2x in my experience; everything larger looks great. An interesting note: the scanlines that appear with this shader are not simply added in. Instead, they are naturally occurring as a result of the phosphor simulation. Pretty cool stuff :D

By uncommenting some lines in caligari's code, you can enable one of three phosphor/shadow mask arrangements, of which the RGB triad option is the nicest (I recommend clicking these thumbnails to see in full size):

As you can see, this option causes some serious dimming of the picture brightness, which was also a technological obstacle for actual CRT engineers when shadow masks were first being designed and implemented. In bsnes, you should be able to just increase the gamma in the picture settings to compensate. You can download this shader variation from my mediafire account.

Quilez

This fast, crisp interpolation filter is totally new to the emulation scene. It's based on some fancy math demonstrated by Inigo Quilez and represents a good compromise between the pixelate2x and bicubic shaders.

Simple TV Blur


This one's rather subtle, but is intended to do the minimum amount of work required to reproduce the pseudo-hires transparency.

Beam4+NTSC

Just for fun, I used SSNES' shader stacking support to combine the new NTSC shader with the Beam4 shader. It's almost indistinguishable from Beam4 alone (except for some really slight changes to the color palette), so I wouldn't bother trying it yourself.

CRT+NTSC

As above, I tried stacking the NTSC shader with the new CRT shader and this was the result. It's kinda cool in a way, like newsprint. :P According to cgwg, this is happening because it's only picking up the first pass of the NTSC shader, which has no color values.

CRT-Flat+Caligari's Scanlines - RGB Triad
When the regular CRT shader (edited to have 0.0 for the distortion value) is paired with caligari's scanlines - RGB triad variant, you end up with a pretty neat result:

The colors are off by a good bit (I think because of the double gamma correction) and it's very dark, but the phosphor triads from caligari's are still clearly discernible over the CRT shader's already-awesome result. Still not perfect, obviously, but getting close in a lot of ways.

الثلاثاء، 8 مارس 2011

Filter and Shader Stacking In SSNES

Themaister just added support for multipass shader application--a.k.a. shader stacking--to SSNES, his excellent (and recently GUI-fied) frontend for libsnes. His implementation includes some interesting and fancy options, including configurable scale factors for the first pass (i.e., the first shader). This allows hq2x to be scaled to its expected scale factor during the first pass and then scaled up again to fit the desired resolution, resulting in much better retention of detail (see this post for a side-by-side comparison of the two scaling methods). Additionally, we can use the second pass to stack a scanline shader on top of the resulting image, or even cgwg's CRT shaders:
As you can see, this combination greatly smooths out the jaggies and rounds the curves without blasting out all of the details, like hq2x can often do on its own.

This two-pass shader implementation also allows users to control the amount of bloom applied by a bloom shader in the first pass simply by modifying the scale factor. Here are two examples in which I've stacked blargg's NTSC filter, cgwg's CRT-Flat v3 shader, and Whateverman's simplebloom shader (rendered at 2x and 1x respectively):
In my opinion, this combination of bloom+CRT shaders+NTSC filter is exceptionally close to the Photoshop renders that inspired the CRT shader effort.

Windows binaries for this latest version of SSNES are available here, while Linux users can download deb binaries from my PPA repo. Mac users can also get in on the fun, but they'll have to compile everything themselves, as no one is providing Mac packages of SSNES yet.

As always, any shaders covered here are available in my mediafire account.

الخميس، 3 مارس 2011

Themaister's Waterpaint and Scanline Pixel Shaders for SNES

Themaister, author of the excellent libsnes interface SSNES, has been working on some really fantastic GLSL pixel shaders for use with compatible emulators, such as bsnes and SNES9x.

First, we'll take a look at the waterpaint shader, which has an effect similar to smoothing filters, such as SuperSaI, but with a little extra fattening that gives it the appearance of a watercolor painting:
This looks great already, but text can sometimes get a bit hard to read and some details get flattened out, so he also made a version that includes light scanlines (both the standard horizontal scanlines, as well as faint vertical lines, as would be found in a phospher mask):
I think this version does a better job of maintaining fine details and really looks nice, especially on Zelda:LttP.

Next, he produced a beautiful (and easily customizable) resolution-agnostic scanline shader. In contrast with my own scanline shaders, Themaister's shader will work with any scale factor instead of having one hardcoded in. Here's how his default version looks without and with bilinear filtering (smooth video), respectively:
In addition to this default, I modified the settings to create medium and dark scanline settings, as well (each shown without and with bilinear filtering enabled, respectively):

You can download these shader's from my mediafire account. If you would like to modify the scaline shaders yourself, you can play around with these figures:
const float base_brightness = 0.95;
const vec2 sine_comp = vec2(0.05, 0.15);