Skip to content

Commit

Permalink
Merge pull request #2 from rsn8887/gtu
Browse files Browse the repository at this point in the history
add sharp_bilinear_simple (no scanlines, automatic largest integer presc…
  • Loading branch information
frangarcj authored Dec 19, 2016
2 parents 73874af + 85b81cb commit f5bb82e
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
49 changes: 49 additions & 0 deletions shaders/sharp_bilinear_simple_f.cg
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* COMPATIBILITY
- HLSL compilers
- Cg compilers
*/

/*
Author: rsn8887 (based on TheMaister)
License: Public domain

This is an integer prescale filter that should be combined
with a bilinear hardware filtering (GL_BILINEAR filter or some such) to achieve
a smooth scaling result with minimum blur. This is good for pixelgraphics
that are scaled by non-integer factors.

The prescale factor and texel coordinates are precalculated
in the vertex shader for speed.
*/

struct precalc_coord
{
float2 texel;
float2 prescale;
};

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

float4 main(in precalc_coord co : TEXCOORD2, uniform sampler2D s0 : TEXUNIT0, uniform input IN) : COLOR
{
//co.texel has already been multiplied by texture_size inside vertex shader
float2 texel_floored = floor(co.texel);
float2 s = frac(co.texel);
//prescale is the largest possible integer scale, calculated in vertex shader
float2 region_range = 0.5 - 0.5 / co.prescale;

// 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)) * co.prescale + 0.5;
float2 mod_texel = texel_floored + f;

return float4(tex2D(s0, mod_texel / IN.texture_size).rgb, 1.0);

}
50 changes: 50 additions & 0 deletions shaders/sharp_bilinear_simple_v.cg
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/* COMPATIBILITY
- HLSL compilers
- Cg compilers
*/

/*
Author: rsn8887 (based on TheMaister)
License: Public domain

This is an integer prescale filter that should be combined
with a bilinear hardware filtering (GL_BILINEAR filter or some such) to achieve
a smooth scaling result with minimum blur. This is good for pixelgraphics
that are scaled by non-integer factors.

The prescale factor and texel coordinates are precalculated
in the vertex shader for speed.
*/

struct precalc_coord
{
float2 texel;
float2 prescale;
};

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

void main
(
float4 aPosition : POSITION,
out float4 oPosition : POSITION,
uniform float4x4 wvp,
float2 aTexcoord : TEXCOORD,
out float2 oTex : TEXCOORD,

uniform input IN,
out precalc_coord co: TEXCOORD2
)
{
oPosition = mul(aPosition, wvp);
oTex = aTexcoord;

//precalculate some values to pass to the fragment shader
co.texel = aTexcoord * IN.texture_size;
co.prescale = floor(IN.output_size / IN.texture_size);
}

0 comments on commit f5bb82e

Please sign in to comment.