Mercurial > repos > blastem
comparison shaders/bandlimit_pixel.f.glsl @ 2495:d437b8e8ba62
Add xBRZ and bandlimit pixel footprint shaders ported by hunterk
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 28 Apr 2024 23:22:37 -0700 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2494:b62580dc6f30 | 2495:d437b8e8ba62 |
---|---|
1 /* | |
2 * Bandlimited pixel footprint shader. | |
3 * Author: Themaister | |
4 * License: MIT | |
5 * Adapted from: https://github.com/Themaister/Granite/blob/master/assets/shaders/inc/bandlimited_pixel_filter.h | |
6 * ported to blastem shader format by hunterk | |
7 */ | |
8 | |
9 // sensible values between 0.0 and 5.0 | |
10 #define SMOOTHNESS 0.5 | |
11 | |
12 uniform sampler2D textures[2]; | |
13 uniform highp vec2 texsize; | |
14 | |
15 varying highp vec2 texcoord; | |
16 | |
17 // The cosine filter convolved with rect has a support of 0.5 + d pixels. | |
18 // We can sample 4x4 regions, so we can deal with 2.0 pixel range in our filter, | |
19 // and the maximum extent value we can have is 1.5. | |
20 const highp float maximum_support_extent = 1.5; | |
21 | |
22 struct BandlimitedPixelInfo | |
23 { | |
24 highp vec2 uv0; | |
25 highp vec2 uv1; | |
26 highp vec2 uv2; | |
27 highp vec2 uv3; | |
28 mediump vec4 weights; | |
29 mediump float l; | |
30 }; | |
31 | |
32 // Our Taylor approximation is not exact, normalize so the peak is 1. | |
33 const highp float taylor_pi_half = 1.00452485553; | |
34 const highp float taylor_normalization = 1.0 / taylor_pi_half; | |
35 const highp float PI = 3.14159265359; | |
36 const highp float PI_half = 0.5 * PI; | |
37 | |
38 #define gen_taylor(T) \ | |
39 mediump T taylor_sin(mediump T p) \ | |
40 { \ | |
41 mediump T p2 = p * p; \ | |
42 mediump T p3 = p * p2; \ | |
43 mediump T p5 = p2 * p3; \ | |
44 return clamp(taylor_normalization * (p - p3 * (1.0 / 6.0) + p5 * (1.0 / 120.0)), -1.0, 1.0); \ | |
45 } | |
46 // No templates in GLSL. Stamp out macros. | |
47 gen_taylor(float) | |
48 gen_taylor(vec2) | |
49 gen_taylor(vec3) | |
50 gen_taylor(vec4) | |
51 | |
52 // Given weights, compute a bilinear filter which implements the weight. | |
53 // All weights are known to be non-negative, and separable. | |
54 mediump vec3 compute_uv_phase_weight(mediump vec2 weights_u, mediump vec2 weights_v) | |
55 { | |
56 // The sum of a bilinear sample has combined weight of 1, we will need to adjust the resulting sample | |
57 // to match our actual weight sum. | |
58 mediump float w = dot(weights_u.xyxy, weights_v.xxyy); | |
59 mediump float x = weights_u.y / max(weights_u.x + weights_u.y, 0.001); | |
60 mediump float y = weights_v.y / max(weights_v.x + weights_v.y, 0.001); | |
61 return vec3(x, y, w); | |
62 } | |
63 | |
64 BandlimitedPixelInfo compute_pixel_weights(vec2 uv, vec2 size, vec2 inv_size) | |
65 { | |
66 // Get derivatives in texel space. | |
67 // Need a non-zero derivative. | |
68 vec2 extent = max(fwidth(uv) * size * (SMOOTHNESS + 0.5), 1.0 / 256.0); | |
69 | |
70 // Get base pixel and phase, range [0, 1). | |
71 vec2 pixel = uv * size - 0.5; | |
72 vec2 base_pixel = floor(pixel); | |
73 vec2 phase = pixel - base_pixel; | |
74 | |
75 BandlimitedPixelInfo info; | |
76 | |
77 mediump vec2 inv_extent = 1.0 / extent; | |
78 if (any(greaterThan(extent, vec2(maximum_support_extent)))) | |
79 { | |
80 // We need to just do regular minimization filtering. | |
81 info = BandlimitedPixelInfo(vec2(0.0), vec2(0.0), vec2(0.0), vec2(0.0), | |
82 vec4(0.0, 0.0, 0.0, 0.0), 0.0); | |
83 } | |
84 else if (all(lessThanEqual(extent, vec2(0.5)))) | |
85 { | |
86 // We can resolve the filter by just sampling a single 2x2 block. | |
87 mediump vec2 shift = 0.5 + 0.5 * taylor_sin(PI_half * clamp(inv_extent * (phase - 0.5), -1.0, 1.0)); | |
88 info = BandlimitedPixelInfo((base_pixel + 0.5 + shift) * inv_size, vec2(0.0), vec2(0.0), vec2(0.0), | |
89 vec4(1.0, 0.0, 0.0, 0.0), 1.0); | |
90 } | |
91 else | |
92 { | |
93 // Full 4x4 sampling. | |
94 | |
95 // Fade between bandlimited and normal sampling. | |
96 // Fully use bandlimited filter at LOD 0, normal filtering at approx. LOD -0.5. | |
97 mediump float max_extent = max(extent.x, extent.y); | |
98 mediump float l = clamp(1.0 - (max_extent - 1.0) / (maximum_support_extent - 1.0), 0.0, 1.0); | |
99 | |
100 mediump vec4 sine_phases_x = PI_half * clamp(inv_extent.x * (phase.x + vec4(1.5, 0.5, -0.5, -1.5)), -1.0, 1.0); | |
101 mediump vec4 sines_x = taylor_sin(sine_phases_x); | |
102 | |
103 mediump vec4 sine_phases_y = PI_half * clamp(inv_extent.y * (phase.y + vec4(1.5, 0.5, -0.5, -1.5)), -1.0, 1.0); | |
104 mediump vec4 sines_y = taylor_sin(sine_phases_y); | |
105 | |
106 mediump vec2 sine_phases_end = PI_half * clamp(inv_extent * (phase - 2.5), -1.0, 1.0); | |
107 mediump vec2 sines_end = taylor_sin(sine_phases_end); | |
108 | |
109 mediump vec4 weights_x = 0.5 * (sines_x - vec4(sines_x.yzw, sines_end.x)); | |
110 mediump vec4 weights_y = 0.5 * (sines_y - vec4(sines_y.yzw, sines_end.y)); | |
111 | |
112 mediump vec3 w0 = compute_uv_phase_weight(weights_x.xy, weights_y.xy); | |
113 mediump vec3 w1 = compute_uv_phase_weight(weights_x.zw, weights_y.xy); | |
114 mediump vec3 w2 = compute_uv_phase_weight(weights_x.xy, weights_y.zw); | |
115 mediump vec3 w3 = compute_uv_phase_weight(weights_x.zw, weights_y.zw); | |
116 | |
117 info = BandlimitedPixelInfo((base_pixel - 0.5 + w0.xy) * inv_size, | |
118 (base_pixel + vec2(1.5, -0.5) + w1.xy) * inv_size, | |
119 (base_pixel + vec2(-0.5, 1.5) + w2.xy) * inv_size, | |
120 (base_pixel + 1.5 + w3.xy) * inv_size, | |
121 vec4(w0.z, w1.z, w2.z, w3.z), l); | |
122 } | |
123 | |
124 return info; | |
125 } | |
126 | |
127 mediump vec4 sample_bandlimited_pixel(sampler2D samp, vec2 uv, BandlimitedPixelInfo info, float lod) | |
128 { | |
129 mediump vec4 color = texture2D(samp, uv); | |
130 if (info.l > 0.0) | |
131 { | |
132 mediump vec4 bandlimited = info.weights.x * pow(texture2D(samp, info.uv0, lod), vec4(2.2)); | |
133 if (info.weights.x < 1.0) | |
134 { | |
135 bandlimited += info.weights.y * pow(texture2D(samp, info.uv1, lod), vec4(2.2)); | |
136 bandlimited += info.weights.z * pow(texture2D(samp, info.uv2, lod), vec4(2.2)); | |
137 bandlimited += info.weights.w * pow(texture2D(samp, info.uv3, lod), vec4(2.2)); | |
138 } | |
139 color = mix(color, bandlimited, info.l); | |
140 } | |
141 return color; | |
142 } | |
143 | |
144 void main() | |
145 { | |
146 BandlimitedPixelInfo info = compute_pixel_weights(texcoord, texsize.xy, 1.0 / texsize.xy); | |
147 mediump vec3 result = sample_bandlimited_pixel(textures[0], texcoord, info, 0.0).rgb; | |
148 gl_FragColor = vec4(sqrt(clamp(result, 0.0, 1.0)), 1.0); | |
149 } |