Skip to content

Commit

Permalink
Added Sharp Bilinear (Scanlines) post-processing effect.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rinnegatamante committed Jul 24, 2020
1 parent a018d3a commit f721ed5
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 25 deletions.
25 changes: 12 additions & 13 deletions Data/DaedalusX64/Shaders/LCD 3x_f.cg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
Author: Gigaherz
License: Public domain
Author: Gigaherz
License: Public domain
*/

/* configuration (higher values mean brighter image but reduced effect depth) */
Expand All @@ -11,19 +11,18 @@ static const int brighten_lcd = 4;
static const float3 offsets = 3.141592654 * float3(1.0/2,1.0/2 - 2.0/3,1.0/2-4.0/3);

float4 main (
float2 tex : TEXCOORD,
float2 tex : TEXCOORD,
float2 omega : TEXCOORD1,
uniform sampler2D s0 : TEXUNIT0
) : COLOR
uniform sampler2D s0 : TEXUNIT0) : COLOR
{
float3 res = tex2D(s0, tex).xyz;
float3 res = tex2D(s0, tex).xyz;

float2 angle = tex * omega;
float2 angle = tex * omega;

float yfactor = (brighten_scanlines + sin(angle.y)) / (brighten_scanlines + 1);
float3 xfactors = (brighten_lcd + sin(angle.x + offsets)) / (brighten_lcd + 1);
float3 color = yfactor * xfactors * res;
return float4(color.x, color.y, color.z, 1.0);
float yfactor = (brighten_scanlines + sin(angle.y)) / (brighten_scanlines + 1);
float3 xfactors = (brighten_lcd + sin(angle.x + offsets)) / (brighten_lcd + 1);
float3 color = yfactor * xfactors * res;
return float4(color.x, color.y, color.z, 1.0);
}
23 changes: 11 additions & 12 deletions Data/DaedalusX64/Shaders/LCD 3x_v.cg
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
/*
Author: Gigaherz
License: Public domain
Author: Gigaherz
License: Public domain
*/

void main (
float3 position,
float2 texcoord,
uniform float4x4 wvp,
out float4 vPosition : POSITION,
out float2 vTexcoord : TEXCOORD0,
out float2 omega : TEXCOORD1
)
float3 position,
float2 texcoord,
uniform float4x4 wvp,
out float4 vPosition : POSITION,
out float2 vTexcoord : TEXCOORD0,
out float2 omega : TEXCOORD1)
{
float2 texture_size = float2(640.0f, 480.0f);

vPosition = mul(wvp,float4(position, 1.f));
vTexcoord = texcoord;
omega = 3.141592654 * 2 * texture_size;
vPosition = mul(wvp,float4(position, 1.f));
vTexcoord = texcoord;
omega = 3.141592654 * 2 * texture_size;
}
43 changes: 43 additions & 0 deletions Data/DaedalusX64/Shaders/Sharp Bilinear (Scanlines)_f.cg
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Author: Themaister
License: Public domain
*/

#define SCANLINE_BASE_BRIGHTNESS 1.0
#define SCANLINE_SINE_COMP_A 0.1
#define SCANLINE_SINE_COMP_B 0.15
#define SHARP_BILINEAR_PRE_SCALE 1.25

float4 main
(
float4 vPosition : POSITION,
float2 vTexcoord : TEXCOORD0,
float2 texel : TEXCOORD1,
uniform sampler2D s0 : TEXUNIT0
) : COLOR
{

float2 texture_size = float2(640.0f, 480.0f);
float2 output_size = float2(960.0f, 544.0f); // TODO: De-hardcode this for different aspect ratios

float2 texel_floored = floor(texel);
float2 s = frac(texel);
float region_range = 0.5 - 0.5 / SHARP_BILINEAR_PRE_SCALE;

// Figure out where in the texel to sample to get correct pre-scaled bilinear.
// Uses the hardware bilinear interpolator to avoid having to sample 4 times manually.

float2 center_dist = s - 0.5;
float2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * SHARP_BILINEAR_PRE_SCALE + 0.5;
float2 mod_texel = texel_floored + f;

float3 res = tex2D(s0, mod_texel / texture_size).rgb;

float2 sine_comp = float2(SCANLINE_SINE_COMP_A, SCANLINE_SINE_COMP_B);

float2 omega = float2(2.0 * 3.1415 * texture_size.x, 3.1415 * texture_size.y);

float3 scanline = res * (SCANLINE_BASE_BRIGHTNESS + dot(sine_comp * sin(vTexcoord * omega), float2(1.0, 1.0)));

return float4(scanline, 1.0);
}
20 changes: 20 additions & 0 deletions Data/DaedalusX64/Shaders/Sharp Bilinear (Scanlines)_v.cg
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
Author: Themaister
License: Public domain
*/

void main(
float3 position,
float2 texcoord,
uniform float4x4 wvp,
out float4 vPosition : POSITION,
out float2 vTexcoord : TEXCOORD0,
out float2 texel : TEXCOORD1)
{
float2 texture_size = float2(640.0f, 480.0f);

vPosition = mul(wvp,float4(position, 1.f));
vTexcoord = texcoord - float2(0.25 / texture_size.x, 0.0);

texel = texcoord * texture_size;
}

0 comments on commit f721ed5

Please sign in to comment.