]> git.sesse.net Git - movit/blob - resample_effect.h
Install compute shaders on make install.
[movit] / resample_effect.h
1 #ifndef _MOVIT_RESAMPLE_EFFECT_H
2 #define _MOVIT_RESAMPLE_EFFECT_H 1
3
4 // High-quality image resizing, either up or down.
5 //
6 // The default scaling offered by the GPU (and as used in ResizeEffect)
7 // is bilinear (optionally mipmapped), which is not the highest-quality
8 // choice, especially for upscaling. ResampleEffect offers the three-lobed
9 // Lanczos kernel, which is among the most popular choices in image
10 // processing. While it does have its weaknesses, in particular a certain
11 // ringing/sharpening effect with artifacts that accumulate over several
12 // consecutive resizings, it is generally regarded as the best tradeoff.
13 //
14 // Works in two passes; first horizontal, then vertical (ResampleEffect,
15 // which is what the user is intended to use, instantiates two copies of
16 // SingleResamplePassEffect behind the scenes).
17
18 #include <epoxy/gl.h>
19 #include <assert.h>
20 #include <stddef.h>
21 #include <memory>
22 #include <string>
23
24 #include "effect.h"
25 #include "fp16.h"
26
27 namespace movit {
28
29 class EffectChain;
30 class Node;
31 class SingleResamplePassEffect;
32
33 // Public so that it can be benchmarked externally.
34 template<class T>
35 struct Tap {
36         T weight;
37         T pos;
38 };
39 struct ScalingWeights {
40         unsigned src_bilinear_samples;
41         unsigned dst_samples, num_loops;
42
43         // Exactly one of these is set.
44         std::unique_ptr<Tap<fp16_int_t>[]> bilinear_weights_fp16;
45         std::unique_ptr<Tap<float>[]> bilinear_weights_fp32;
46 };
47 ScalingWeights calculate_scaling_weights(unsigned src_size, unsigned dst_size, float zoom, float offset);
48
49 class ResampleEffect : public Effect {
50 public:
51         ResampleEffect();
52         ~ResampleEffect();
53
54         virtual std::string effect_type_id() const { return "ResampleEffect"; }
55
56         // We want this for the same reason as ResizeEffect; we could end up scaling
57         // down quite a lot.
58         virtual bool needs_texture_bounce() const { return true; }
59         virtual bool needs_srgb_primaries() const { return false; }
60
61         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height);
62
63         virtual std::string output_fragment_shader() {
64                 assert(false);
65         }
66         virtual void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num) {
67                 assert(false);
68         }
69
70         virtual void rewrite_graph(EffectChain *graph, Node *self);
71         virtual bool set_float(const std::string &key, float value);
72         
73 private:
74         void update_size();
75         void update_offset_and_zoom();
76         
77         // Both of these are owned by us if owns_effects is true (before finalize()),
78         // and otherwise owned by the EffectChain.
79         bool owns_effects;
80         SingleResamplePassEffect *hpass, *vpass;
81         int input_width, input_height, output_width, output_height;
82
83         float offset_x, offset_y;
84         float zoom_x, zoom_y;
85         float zoom_center_x, zoom_center_y;
86         float unused;
87 };
88
89 class SingleResamplePassEffect : public Effect {
90 public:
91         // If parent is non-nullptr, calls to inform_input_size will be forwarded,
92         // so that it can inform both passes about the right input and output
93         // resolutions.
94         SingleResamplePassEffect(ResampleEffect *parent);
95         ~SingleResamplePassEffect();
96         virtual std::string effect_type_id() const { return "SingleResamplePassEffect"; }
97
98         std::string output_fragment_shader();
99
100         virtual bool needs_texture_bounce() const { return true; }
101         virtual bool needs_srgb_primaries() const { return false; }
102         virtual AlphaHandling alpha_handling() const { return INPUT_PREMULTIPLIED_ALPHA_KEEP_BLANK; }
103
104         virtual void inform_added(EffectChain *chain) { this->chain = chain; }
105         virtual void inform_input_size(unsigned input_num, unsigned width, unsigned height) {
106                 if (parent != nullptr) {
107                         parent->inform_input_size(input_num, width, height);
108                 }
109         }
110         virtual bool changes_output_size() const { return true; }
111         virtual bool sets_virtual_output_size() const { return false; }
112
113         virtual void get_output_size(unsigned *width, unsigned *height, unsigned *virtual_width, unsigned *virtual_height) const {
114                 *virtual_width = *width = this->output_width;
115                 *virtual_height = *height = this->output_height;
116         }
117
118         void set_gl_state(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
119         
120         enum Direction { HORIZONTAL = 0, VERTICAL = 1 };
121
122 private:
123         void update_texture(GLuint glsl_program_num, const std::string &prefix, unsigned *sampler_num);
124
125         ResampleEffect *parent;
126         EffectChain *chain;
127         Direction direction;
128         GLuint texnum;
129         GLint uniform_sample_tex;
130         float uniform_num_loops, uniform_slice_height, uniform_sample_x_scale, uniform_sample_x_offset;
131         float uniform_whole_pixel_offset;
132         int uniform_num_samples;
133
134         int input_width, input_height, output_width, output_height;
135         float offset, zoom;
136         float unused;
137         int last_input_width, last_input_height, last_output_width, last_output_height;
138         float last_offset, last_zoom;
139         int src_bilinear_samples, num_loops;
140         float slice_height;
141         int last_texture_width, last_texture_height;
142         GLuint last_texture_internal_format;
143 };
144
145 }  // namespace movit
146
147 #endif // !defined(_MOVIT_RESAMPLE_EFFECT_H)