1 #ifndef _MOVIT_FLAT_INPUT_H
2 #define _MOVIT_FLAT_INPUT_H 1
9 #include "effect_chain.h"
11 #include "image_format.h"
18 // A FlatInput is the normal, “classic” case of an input, where everything
19 // comes from a single 2D array with chunky pixels.
20 class FlatInput : public Input {
22 FlatInput(ImageFormat format, MovitPixelFormat pixel_format, GLenum type, unsigned width, unsigned height);
25 virtual std::string effect_type_id() const { return "FlatInput"; }
27 virtual bool can_output_linear_gamma() const {
28 // On desktop OpenGL, there's also GL_SLUMINANCE8 which could give us
29 // support for single-channel sRGB decoding, but it's not supported
30 // on GLES, and we're already actively rewriting single-channel inputs
31 // to GL_RED (even on desktop), so we stick to 3- and 4-channel inputs.
32 return (type == GL_UNSIGNED_BYTE &&
33 (pixel_format == FORMAT_RGB ||
34 pixel_format == FORMAT_RGBA_POSTMULTIPLIED_ALPHA) &&
35 (image_format.gamma_curve == GAMMA_LINEAR ||
36 image_format.gamma_curve == GAMMA_sRGB));
38 virtual AlphaHandling alpha_handling() const {
39 switch (pixel_format) {
40 case FORMAT_RGBA_PREMULTIPLIED_ALPHA:
41 return INPUT_AND_OUTPUT_PREMULTIPLIED_ALPHA;
42 case FORMAT_RGBA_POSTMULTIPLIED_ALPHA:
43 return OUTPUT_POSTMULTIPLIED_ALPHA;
47 return OUTPUT_BLANK_ALPHA;
53 std::string output_fragment_shader();
55 // Uploads the texture if it has changed since last time.
56 void set_gl_state(GLuint glsl_program_num, const std::string& prefix, unsigned *sampler_num);
58 unsigned get_width() const { return width; }
59 unsigned get_height() const { return height; }
60 Colorspace get_color_space() const { return image_format.color_space; }
61 GammaCurve get_gamma_curve() const { return image_format.gamma_curve; }
62 virtual bool is_single_texture() const { return true; }
64 // Tells the input where to fetch the actual pixel data. Note that if you change
65 // this data, you must either call set_pixel_data() again (using the same pointer
66 // is fine), or invalidate_pixel_data(). Otherwise, the texture won't be re-uploaded
67 // on subsequent frames.
69 // The data can either be a regular pointer (if pbo==0), or a byte offset
70 // into a PBO. The latter will allow you to start uploading the texture data
71 // asynchronously to the GPU, if you have any CPU-intensive work between the
72 // call to set_pixel_data() and the actual rendering. In either case,
73 // the pointer (and PBO, if set) has to be valid at the time of the render call.
74 void set_pixel_data(const unsigned char *pixel_data, GLuint pbo = 0)
76 assert(this->type == GL_UNSIGNED_BYTE);
77 this->pixel_data = pixel_data;
79 invalidate_pixel_data();
82 void set_pixel_data(const unsigned short *pixel_data, GLuint pbo = 0)
84 assert(this->type == GL_UNSIGNED_SHORT);
85 this->pixel_data = pixel_data;
87 invalidate_pixel_data();
90 void set_pixel_data_fp16(const fp16_int_t *pixel_data, GLuint pbo = 0)
92 assert(this->type == GL_HALF_FLOAT);
93 this->pixel_data = pixel_data;
95 invalidate_pixel_data();
98 void set_pixel_data(const float *pixel_data, GLuint pbo = 0)
100 assert(this->type == GL_FLOAT);
101 this->pixel_data = pixel_data;
103 invalidate_pixel_data();
106 void invalidate_pixel_data();
108 // Note: Sets pitch to width, so even if your pitch is unchanged,
109 // you will need to re-set it after this call.
110 void set_width(unsigned width)
113 this->pitch = this->width = width;
114 invalidate_pixel_data();
117 void set_height(unsigned height)
120 this->height = height;
121 invalidate_pixel_data();
124 void set_pitch(unsigned pitch) {
127 invalidate_pixel_data();
130 // Tells the input to use the specific OpenGL texture as pixel data.
131 // This is useful if you want to share the same texture between multiple
132 // EffectChain instances, or if you somehow can get the data into a texture more
133 // efficiently than through a normal upload (e.g. a video codec decoding straight
134 // into a texture). Note that you are responsible for setting the right sampler
135 // parameters (e.g. clamp-to-edge) yourself, as well as generate any mipmaps
136 // if they are needed.
138 // NOTE: The input does not take ownership of this texture; you are responsible
139 // for releasing it yourself. In particular, if you call invalidate_pixel_data()
140 // or anything calling it, the texture will silently be removed from the input.
142 // NOTE: Doing this in a situation where can_output_linear_gamma() is true
143 // can yield unexpected results, as the downstream effect can expect the texture
144 // to be uploaded with the sRGB flag on.
145 void set_texture_num(GLuint texture_num)
147 possibly_release_texture();
148 this->texture_num = texture_num;
149 this->owns_texture = false;
152 virtual void inform_added(EffectChain *chain)
154 resource_pool = chain->get_resource_pool();
158 // Release the texture if we have any, and it is owned by us.
159 void possibly_release_texture();
161 ImageFormat image_format;
162 MovitPixelFormat pixel_format;
164 GLuint pbo, texture_num;
165 int output_linear_gamma, needs_mipmaps;
166 unsigned width, height, pitch;
168 const void *pixel_data;
169 ResourcePool *resource_pool;
170 bool fixup_swap_rb, fixup_red_to_grayscale;
176 #endif // !defined(_MOVIT_FLAT_INPUT_H)