]> git.sesse.net Git - movit/blob - flat_input.h
Remove a few unneeded shader program switches.
[movit] / flat_input.h
1 #ifndef _MOVIT_FLAT_INPUT_H
2 #define _MOVIT_FLAT_INPUT_H 1
3
4 #include <epoxy/gl.h>
5 #include <assert.h>
6 #include <string>
7
8 #include "effect.h"
9 #include "effect_chain.h"
10 #include "fp16.h"
11 #include "image_format.h"
12 #include "init.h"
13 #include "input.h"
14
15 namespace movit {
16
17 class ResourcePool;
18
19 // A FlatInput is the normal, “classic” case of an input, where everything
20 // comes from a single 2D array with chunky pixels.
21 class FlatInput : public Input {
22 public:
23         FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
24         ~FlatInput();
25
26         virtual std::string effect_type_id() const { return "FlatInput"; }
27
28         virtual bool can_output_linear_gamma() const {
29                 // On desktop OpenGL, there's also GL_SLUMINANCE8 which could give us
30                 // support for single-channel sRGB decoding, but it's not supported
31                 // on GLES, and we're already actively rewriting single-channel inputs
32                 // to GL_RED (even on desktop), so we stick to 3- and 4-channel inputs.
33                 return (movit_srgb_textures_supported &&
34                         type == GL_UNSIGNED_BYTE &&
35                         (pixel_format == FORMAT_RGB ||
36                          pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) &&
37                         (image_format.gamma_curve == GAMMA_LINEAR ||
38                          image_format.gamma_curve == GAMMA_sRGB));
39         }
40         virtual AlphaHandling alpha_handling() const {
41                 switch (pixel_format) {
42                 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
43                         return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
44                 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
45                         return OUTPUT_POSTMULTIPLIED_ALPHA;
46                 case FORMAT_R:
47                 case FORMAT_RG:
48                 case FORMAT_RGB:
49                         return OUTPUT_BLANK_ALPHA;
50                 default:
51                         assert(false);
52                 }
53         }
54
55         std::string output_fragment_shader();
56
57         // Uploads the texture if it has changed since last time.
58         void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
59
60         unsigned get_width() const { return width; }
61         unsigned get_height() const { return height; }
62         Colorspace get_color_space() const { return image_format.color_space; }
63         GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
64         virtual bool is_single_texture() const { return true; }
65
66         // Tells the input where to fetch the actual pixel data. Note that if you change
67         // this data, you must either call set_pixel_data() again (using the same pointer
68         // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
69         // on subsequent frames.
70         //
71         // The data can either be a regular pointer (if pbo==0), or a byte offset
72         // into a PBO. The latter will allow you to start uploading the texture data
73         // asynchronously to the GPU, if you have any CPU-intensive work between the
74         // call to set_pixel_data() and the actual rendering. In either case,
75         // the pointer (and PBO, if set) has to be valid at the time of the render call.
76         void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
77         {
78                 assert(this->type == GL_UNSIGNED_BYTE);
79                 this->pixel_data = pixel_data;
80                 this->pbo = pbo;
81                 invalidate_pixel_data();
82         }
83
84         void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
85         {
86                 assert(this->type == GL_UNSIGNED_SHORT);
87                 this->pixel_data = pixel_data;
88                 this->pbo = pbo;
89                 invalidate_pixel_data();
90         }
91
92         void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
93         {
94                 assert(this->type == GL_HALF_FLOAT);
95                 this->pixel_data = pixel_data;
96                 this->pbo = pbo;
97                 invalidate_pixel_data();
98         }
99
100         void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
101         {
102                 assert(this->type == GL_FLOAT);
103                 this->pixel_data = pixel_data;
104                 this->pbo = pbo;
105                 invalidate_pixel_data();
106         }
107
108         void invalidate_pixel_data();
109
110         // Note: Sets pitch to width, so even if your pitch is unchanged,
111         // you will need to re-set it after this call.
112         void set_width(unsigned width)
113         {
114                 this->pitch = this->width = width;
115                 invalidate_pixel_data();
116         }
117
118         void set_height(unsigned height)
119         {
120                 this->height = height;
121                 invalidate_pixel_data();
122         }
123
124         void set_pitch(unsigned pitch) {
125                 this->pitch = pitch;
126                 invalidate_pixel_data();
127         }
128
129         // Tells the input to use the specific OpenGL texture as pixel data.
130         // This is useful if you want to share the same texture between multiple
131         // EffectChain instances, or if you somehow can get the data into a texture more
132         // efficiently than through a normal upload (e.g. a video codec decoding straight
133         // into a texture). Note that you are responsible for setting the right sampler
134         // parameters (e.g. clamp-to-edge) yourself, as well as generate any mipmaps
135         // if they are needed.
136         //
137         // NOTE: The input does not take ownership of this texture; you are responsible
138         // for releasing it yourself. In particular, if you call invalidate_pixel_data()
139         // or anything calling it, the texture will silently be removed from the input.
140         void set_texture_num(GLuint texture_num)
141         {
142                 possibly_release_texture();
143                 this->texture_num = texture_num;
144                 this->owns_texture = false;
145         }
146
147         virtual void inform_added(EffectChain *chain)
148         {
149                 resource_pool = chain->get_resource_pool();
150         }
151
152 private:
153         // Release the texture if we have any, and it is owned by us.
154         void possibly_release_texture();
155
156         ImageFormat image_format;
157         MovitPixelFormat pixel_format;
158         GLenum type;
159         GLuint pbo, texture_num;
160         int output_linear_gamma, needs_mipmaps;
161         unsigned width, height, pitch;
162         bool owns_texture;
163         const void *pixel_data;
164         ResourcePool *resource_pool;
165         bool fixup_swap_rb, fixup_red_to_grayscale;
166         GLint uniform_tex;
167 };
168
169 }  // namespace movit
170
171 #endif // !defined(_MOVIT_FLAT_INPUT_H)