diff options
Diffstat (limited to 'hlsl/prescale.fx')
-rw-r--r-- | hlsl/prescale.fx | 40 |
1 files changed, 29 insertions, 11 deletions
diff --git a/hlsl/prescale.fx b/hlsl/prescale.fx index 6faa659e6d0..1cbcaa0f753 100644 --- a/hlsl/prescale.fx +++ b/hlsl/prescale.fx @@ -1,7 +1,11 @@ // license:BSD-3-Clause -// copyright-holders:Ryan Holtz +// copyright-holders:Ryan Holtz,Themaister,ImJezze //----------------------------------------------------------------------------- -// Prescale Effect +// Pre-scale Effect +// +// Uses the hardware bilinear interpolator to avoid having to sample 4 times manually. +// +// https://github.com/libretro/common-shaders/blob/master/retro/shaders/sharp-bilinear.cg //----------------------------------------------------------------------------- //----------------------------------------------------------------------------- @@ -13,9 +17,9 @@ texture Diffuse; sampler DiffuseSampler = sampler_state { Texture = <Diffuse>; - MipFilter = NONE; - MinFilter = NONE; - MagFilter = NONE; + MipFilter = LINEAR; + MinFilter = LINEAR; + MagFilter = LINEAR; AddressU = CLAMP; AddressV = CLAMP; AddressW = CLAMP; @@ -45,11 +49,12 @@ struct PS_INPUT }; //----------------------------------------------------------------------------- -// Prescale Vertex Shader +// Pre-scale Vertex Shader //----------------------------------------------------------------------------- uniform float2 ScreenDims; uniform float2 TargetDims; +uniform float2 SourceDims; VS_OUTPUT vs_main(VS_INPUT Input) { @@ -62,22 +67,35 @@ VS_OUTPUT vs_main(VS_INPUT Input) Output.Position.xy *= 2.0f; // zoom Output.TexCoord = Input.TexCoord; - Output.TexCoord += 0.5f / TargetDims; // half texel offset correction (DX9) + // Output.TexCoord += 0.5f / targetDims; // half texel offset correction (DX9) return Output; } //----------------------------------------------------------------------------- -// Prescale Pixel Shader +// Pre-scale Pixel Shader //----------------------------------------------------------------------------- float4 ps_main(PS_INPUT Input) : COLOR { - return tex2D(DiffuseSampler, Input.TexCoord); + float2 Scale = TargetDims / SourceDims; + + float2 TexelDims = Input.TexCoord * SourceDims; + float2 i = floor(TexelDims); + float2 s = frac(TexelDims); + + // Figure out where in the texel to sample to get the correct pre-scaled bilinear. + float2 CenterDistance = s - 0.5f; + float2 RegionRange = 0.5f - 0.5f / Scale; + float2 f = (CenterDistance - clamp(CenterDistance, -RegionRange, RegionRange)) * Scale + 0.5f; + + float2 TexCoord = (i + f) / SourceDims; + + return tex2D(DiffuseSampler, TexCoord); } //----------------------------------------------------------------------------- -// Prescale Technique +// Pre-scale Technique //----------------------------------------------------------------------------- technique DefaultTechnique @@ -89,4 +107,4 @@ technique DefaultTechnique VertexShader = compile vs_3_0 vs_main(); PixelShader = compile ps_3_0 ps_main(); } -} +}
\ No newline at end of file |