1 // Three-lobed Lanczos, the most common choice.
2 #define LANCZOS_RADIUS 3.0
7 #include "resample_effect.h"
8 #include "effect_chain.h"
17 return 1.0f - fabs(x);
23 float lanczos_weight(float x, float a)
28 return sinc(M_PI * x) * sinc(M_PI * x / a);
32 // Euclid's algorithm, from Wikipedia.
33 unsigned gcd(unsigned a, unsigned b)
43 unsigned combine_samples(float *src, float *dst, unsigned num_src_samples, unsigned max_samples_saved)
45 unsigned num_samples_saved = 0;
46 for (unsigned i = 0, j = 0; i < num_src_samples; ++i, ++j) {
47 // Copy the sample directly; it will be overwritten later if we can combine.
49 dst[j * 2 + 0] = src[i * 2 + 0];
50 dst[j * 2 + 1] = src[i * 2 + 1];
53 if (i == num_src_samples - 1) {
54 // Last sample; cannot combine.
57 if (num_samples_saved == max_samples_saved) {
58 // We could maybe save more here, but other rows can't, so don't bother.
62 float w1 = src[i * 2 + 0];
63 float w2 = src[(i + 1) * 2 + 0];
65 // Differing signs; cannot combine.
69 float pos1 = src[i * 2 + 1];
70 float pos2 = src[(i + 1) * 2 + 1];
73 float offset, total_weight;
74 combine_two_samples(w1, w2, &offset, &total_weight);
76 // OK, we can combine this and the next sample.
78 dst[j * 2 + 0] = total_weight;
79 dst[j * 2 + 1] = pos1 + offset * (pos2 - pos1);
82 ++i; // Skip the next sample.
85 return num_samples_saved;
90 ResampleEffect::ResampleEffect()
94 register_int("width", &output_width);
95 register_int("height", &output_height);
97 // The first blur pass will forward resolution information to us.
98 hpass = new SingleResamplePassEffect(this);
99 hpass->set_int("direction", SingleResamplePassEffect::HORIZONTAL);
100 vpass = new SingleResamplePassEffect(NULL);
101 vpass->set_int("direction", SingleResamplePassEffect::VERTICAL);
106 void ResampleEffect::rewrite_graph(EffectChain *graph, Node *self)
108 Node *hpass_node = graph->add_node(hpass);
109 Node *vpass_node = graph->add_node(vpass);
110 graph->connect_nodes(hpass_node, vpass_node);
111 graph->replace_receiver(self, hpass_node);
112 graph->replace_sender(self, vpass_node);
113 self->disabled = true;
116 // We get this information forwarded from the first blur pass,
117 // since we are not part of the chain ourselves.
118 void ResampleEffect::inform_input_size(unsigned input_num, unsigned width, unsigned height)
120 assert(input_num == 0);
124 input_height = height;
128 void ResampleEffect::update_size()
131 ok |= hpass->set_int("input_width", input_width);
132 ok |= hpass->set_int("input_height", input_height);
133 ok |= hpass->set_int("output_width", output_width);
134 ok |= hpass->set_int("output_height", input_height);
136 ok |= vpass->set_int("input_width", output_width);
137 ok |= vpass->set_int("input_height", input_height);
138 ok |= vpass->set_int("output_width", output_width);
139 ok |= vpass->set_int("output_height", output_height);
144 bool ResampleEffect::set_float(const std::string &key, float value) {
145 if (key == "width") {
146 output_width = value;
150 if (key == "height") {
151 output_height = value;
158 SingleResamplePassEffect::SingleResamplePassEffect(ResampleEffect *parent)
160 direction(HORIZONTAL),
163 last_input_width(-1),
164 last_input_height(-1),
165 last_output_width(-1),
166 last_output_height(-1)
168 register_int("direction", (int *)&direction);
169 register_int("input_width", &input_width);
170 register_int("input_height", &input_height);
171 register_int("output_width", &output_width);
172 register_int("output_height", &output_height);
174 glGenTextures(1, &texnum);
177 SingleResamplePassEffect::~SingleResamplePassEffect()
179 glDeleteTextures(1, &texnum);
182 std::string SingleResamplePassEffect::output_fragment_shader()
185 sprintf(buf, "#define DIRECTION_VERTICAL %d\n", (direction == VERTICAL));
186 return buf + read_file("resample_effect.frag");
189 // Using vertical scaling as an example:
191 // Generally out[y] = w0 * in[yi] + w1 * in[yi + 1] + w2 * in[yi + 2] + ...
193 // Obviously, yi will depend on y (in a not-quite-linear way), but so will
194 // the weights w0, w1, w2, etc.. The easiest way of doing this is to encode,
195 // for each sample, the weight and the yi value, e.g. <yi, w0>, <yi + 1, w1>,
196 // and so on. For each y, we encode these along the x-axis (since that is spare),
197 // so out[0] will read from parameters <x,y> = <0,0>, <1,0>, <2,0> and so on.
199 // For horizontal scaling, we fill in the exact same texture;
200 // the shader just interprets is differently.
201 void SingleResamplePassEffect::update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
203 unsigned src_size, dst_size;
204 if (direction == SingleResamplePassEffect::HORIZONTAL) {
205 assert(input_height == output_height);
206 src_size = input_width;
207 dst_size = output_width;
208 } else if (direction == SingleResamplePassEffect::VERTICAL) {
209 assert(input_width == output_width);
210 src_size = input_height;
211 dst_size = output_height;
217 // For many resamplings (e.g. 640 -> 1280), we will end up with the same
218 // set of samples over and over again in a loop. Thus, we can compute only
219 // the first such loop, and then ask the card to repeat the texture for us.
220 // This is both easier on the texture cache and lowers our CPU cost for
221 // generating the kernel somewhat.
222 num_loops = gcd(src_size, dst_size);
223 slice_height = 1.0f / num_loops;
224 unsigned dst_samples = dst_size / num_loops;
226 // Sample the kernel in the right place. A diagram with a triangular kernel
227 // (corresponding to linear filtering, and obviously with radius 1)
228 // for easier ASCII art drawing:
234 // x---x---x x x---x---x---x
236 // Scaling up (in this case, 2x) means sampling more densely:
242 // x-x-x-x-x-x x x x-x-x-x-x-x-x-x
244 // When scaling up, any destination pixel will only be influenced by a few
245 // (in this case, two) neighboring pixels, and more importantly, the number
246 // will not be influenced by the scaling factor. (Note, however, that the
247 // pixel centers have moved, due to OpenGL's center-pixel convention.)
248 // The only thing that changes is the weights themselves, as the sampling
249 // points are at different distances from the original pixels.
251 // Scaling down is a different story:
257 // --x------ x --x-------x--
259 // Again, the pixel centers have moved in a maybe unintuitive fashion,
260 // although when you consider that there are multiple source pixels around,
261 // it's not so bad as at first look:
267 // --x-------x-------x-------x--
269 // As you can see, the new pixels become averages of the two neighboring old
270 // ones (the situation for Lanczos is of course more complex).
272 // Anyhow, in this case we clearly need to look at more source pixels
273 // to compute the destination pixel, and how many depend on the scaling factor.
274 // Thus, the kernel width will vary with how much we scale.
275 float radius_scaling_factor = std::min(float(dst_size) / float(src_size), 1.0f);
276 int int_radius = lrintf(LANCZOS_RADIUS / radius_scaling_factor);
277 int src_samples = int_radius * 2 + 1;
278 float *weights = new float[dst_samples * src_samples * 2];
279 for (unsigned y = 0; y < dst_samples; ++y) {
280 // Find the point around which we want to sample the source image,
281 // compensating for differing pixel centers as the scale changes.
282 float center_src_y = (y + 0.5f) * float(src_size) / float(dst_size) - 0.5f;
283 int base_src_y = lrintf(center_src_y);
285 // Now sample <int_radius> pixels on each side around that point.
286 for (int i = 0; i < src_samples; ++i) {
287 int src_y = base_src_y + i - int_radius;
288 float weight = lanczos_weight(radius_scaling_factor * (src_y - center_src_y), LANCZOS_RADIUS);
289 weights[(y * src_samples + i) * 2 + 0] = weight * radius_scaling_factor;
290 weights[(y * src_samples + i) * 2 + 1] = (src_y + 0.5) / float(src_size);
294 // Now make use of the bilinear filtering in the GPU to reduce the number of samples
295 // we need to make. This is a bit more complex than BlurEffect since we cannot combine
296 // two neighboring samples if their weights have differing signs, so we first need to
297 // figure out the maximum number of samples. Then, we downconvert all the weights to
298 // that number -- we could have gone for a variable-length system, but this is simpler,
299 // and the gains would probably be offset by the extra cost of checking when to stop.
301 // The greedy strategy for combining samples is optimal.
302 src_bilinear_samples = 0;
303 for (unsigned y = 0; y < dst_samples; ++y) {
304 unsigned num_samples_saved = combine_samples(weights + (y * src_samples) * 2, NULL, src_samples, UINT_MAX);
305 src_bilinear_samples = std::max<int>(src_bilinear_samples, src_samples - num_samples_saved);
308 // Now that we know the right width, actually combine the samples.
309 float *bilinear_weights = new float[dst_samples * src_bilinear_samples * 2];
310 for (unsigned y = 0; y < dst_samples; ++y) {
311 unsigned num_samples_saved = combine_samples(
312 weights + (y * src_samples) * 2,
313 bilinear_weights + (y * src_bilinear_samples) * 2,
315 src_samples - src_bilinear_samples);
316 assert(int(src_samples) - int(num_samples_saved) == src_bilinear_samples);
319 // Encode as a two-component texture. Note the GL_REPEAT.
320 glActiveTexture(GL_TEXTURE0 + *sampler_num);
322 glBindTexture(GL_TEXTURE_2D, texnum);
324 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
326 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
328 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
330 glTexImage2D(GL_TEXTURE_2D, 0, GL_RG32F, src_bilinear_samples, dst_samples, 0, GL_RG, GL_FLOAT, bilinear_weights);
334 delete[] bilinear_weights;
337 void SingleResamplePassEffect::set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num)
339 Effect::set_gl_state(glsl_program_num, prefix, sampler_num);
341 if (input_width != last_input_width ||
342 input_height != last_input_height ||
343 output_width != last_output_width ||
344 output_height != last_output_height) {
345 update_texture(glsl_program_num, prefix, sampler_num);
346 last_input_width = input_width;
347 last_input_height = input_height;
348 last_output_width = output_width;
349 last_output_height = output_height;
352 glActiveTexture(GL_TEXTURE0 + *sampler_num);
354 glBindTexture(GL_TEXTURE_2D, texnum);
357 set_uniform_int(glsl_program_num, prefix, "sample_tex", *sampler_num);
359 set_uniform_int(glsl_program_num, prefix, "num_samples", src_bilinear_samples);
360 set_uniform_float(glsl_program_num, prefix, "num_loops", num_loops);
361 set_uniform_float(glsl_program_num, prefix, "slice_height", slice_height);
363 // Instructions for how to convert integer sample numbers to positions in the weight texture.
364 set_uniform_float(glsl_program_num, prefix, "sample_x_scale", 1.0f / src_bilinear_samples);
365 set_uniform_float(glsl_program_num, prefix, "sample_x_offset", 0.5f / src_bilinear_samples);
367 // We specifically do not want mipmaps on the input texture;
368 // they break minification.
369 glActiveTexture(GL_TEXTURE0);
371 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);