-
Notifications
You must be signed in to change notification settings - Fork 21
/
sharp_bilinear.dsd
67 lines (49 loc) · 1.87 KB
/
sharp_bilinear.dsd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// sharp_bilinear - This is an integer prescale filter that should be combined
// with bilinear hardware filtering (GL_LINEAR filter or some such) to achieve
// a smooth scaling result with minimum blur. This is good for pixel graphics
// that are scaled by non-integer factors.
//
// Original code copyright (C) rsn8887 & TheMaister and released into the
// public domain
//
// 'Ported' (i.e. copy/paste) to DraStic format by jdgleaver
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
=============================================
<vertex>
attribute vec2 a_vertex_coordinate;
attribute vec2 a_texture_coordinate;
uniform vec4 u_texture_size;
uniform vec2 u_target_size;
varying vec2 precalc_texel;
varying vec2 precalc_scale;
void main()
{
gl_Position = vec4(a_vertex_coordinate.xy, 0.0, 1.0);
precalc_texel = a_texture_coordinate * u_texture_size.zw;
precalc_scale = floor(u_target_size.xy / u_texture_size.zw) + 1.0;
}
</vertex>
<fragment>
uniform sampler2D u_texture;
uniform vec4 u_texture_size;
uniform vec2 u_target_size;
varying vec2 precalc_texel;
varying vec2 precalc_scale;
void main()
{
vec2 texel_floored = floor(precalc_texel);
vec2 s = fract(precalc_texel);
vec2 region_range = 0.5 - 0.5 / precalc_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.
vec2 center_dist = s - 0.5;
vec2 f = (center_dist - clamp(center_dist, -region_range, region_range)) * precalc_scale + 0.5;
vec2 mod_texel = texel_floored + f;
vec3 colour = texture2D(u_texture, mod_texel / u_texture_size.zw).rgb;
gl_FragColor = vec4(colour.rgb, 1.0);
}
</fragment>